DBA Data[Home] [Help]

PACKAGE BODY: APPS.PA_FP_COPY_FROM_PKG

Source


1 PACKAGE BODY pa_fp_copy_from_pkg AS
2 /* $Header: PAFPCPFB.pls 120.27.12020000.2 2013/03/27 05:16:35 bpottipa ship $*/
3 
4 g_plsql_max_array_size NUMBER := 200;
5 g_module_name VARCHAR2(100) := 'pa.plsql.pa_fp_copy_from_pkg';
6 
7 -- Bug Fix: 4569365. Removed MRC code.
8 -- g_mrc_exception  EXCEPTION;
9 
10 TYPE g_period_profile_tbl_typ IS TABLE OF
11 pa_budget_versions.period_profile_id%TYPE INDEX BY BINARY_INTEGER;
12 
13 g_source_period_profile_tbl  g_period_profile_tbl_typ;
14 g_target_period_profile_tbl  g_period_profile_tbl_typ;
15 
16 
17 --Constants used for mapping (bug 3354518)
18 ELEMENT_TASK_MAP             CONSTANT VARCHAR2(30):='Element_Task_Map';
19 ELEMENT_ELEMENT_MAP          CONSTANT VARCHAR2(30):='Element_Element_Map';
20 
21 
22 /*========================================================================
23   This procedure is used to acquie required locks for copy_plan
24  =======================================================================*/
25 
26  P_PA_DEBUG_MODE varchar2(1) := NVL(FND_PROFILE.value('PA_DEBUG_MODE'), 'N');
27 
28 PROCEDURE Acquire_Locks_For_Copy_Plan(
29              p_source_plan_version_id    IN     NUMBER
30              ,p_target_plan_version_id   IN     NUMBER
31              ,x_return_status            OUT    NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
32              ,x_msg_count                OUT    NOCOPY NUMBER --File.Sql.39 bug 4440895
33              ,x_msg_data                 OUT    NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
34  AS
35 
36        l_msg_count          NUMBER :=0;
37        l_data               VARCHAR2(2000);
38        l_msg_data           VARCHAR2(2000);
39        l_msg_index_out      NUMBER;
40        l_debug_mode         VARCHAR2(30);
41 
42        --rel_lock             NUMBER;
43        --l_locked_by_person_id     pa_budget_versions.locked_by_person_id%TYPE;
44        Resource_Busy             EXCEPTION;
45        PRAGMA exception_init(Resource_Busy,-00054);
46 
47        CURSOR source_fp_opt_cur IS
48             SELECT record_version_number
49             FROM  PA_PROJ_FP_OPTIONS
50             WHERE fin_plan_version_id = p_source_plan_version_id
51             FOR UPDATE NOWAIT;
52 
53        CURSOR source_bdgt_vers_cur IS
54             SELECT record_version_number
55             FROM   PA_BUDGET_VERSIONS
56             WHERE  budget_version_id = p_source_plan_version_id
57             FOR UPDATE NOWAIT;
58 
59        CURSOR target_fp_opt_cur IS
60             SELECT record_version_number
61             FROM  PA_PROJ_FP_OPTIONS
62             WHERE fin_plan_version_id = p_target_plan_version_id
63             FOR UPDATE NOWAIT;
64 
65        CURSOR target_bdgt_vers_cur IS
66             SELECT record_version_number
67             FROM   PA_BUDGET_VERSIONS
68             WHERE  budget_version_id = p_target_plan_version_id
69             FOR UPDATE NOWAIT;
70  BEGIN
71 
72       x_msg_count := 0;
73       x_return_status := FND_API.G_RET_STS_SUCCESS;
74 
75 IF P_PA_DEBUG_MODE = 'Y' THEN
76       pa_debug.set_err_stack('Acquire_Locks_For_Copy_Plan');
77 END IF;
78       fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
79       l_debug_mode := NVL(l_debug_mode, 'Y');
80 	IF P_PA_DEBUG_MODE = 'Y' THEN
81 	      pa_debug.set_process('PLSQL','LOG',l_debug_mode);
82 	END IF;
83      /*
84       * Acquire lock on pa_proj_fp_options and pa_budget_versions so that
85       * no other process would be able to modify these tables and all
86       * underlying child tables
87       */
88 
89        IF P_PA_DEBUG_MODE = 'Y' THEN
90             pa_debug.g_err_stage:='Acquiring lock on pa_proj_fp_options';
91             pa_debug.write('Acquire_Locks_For_Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,3);
92        END IF;
93 
94        OPEN source_fp_opt_cur;
95 
96        OPEN target_fp_opt_cur;
97 
98        IF P_PA_DEBUG_MODE = 'Y' THEN
99             pa_debug.g_err_stage:='Acquiring lock on pa_budget_versions';
100             pa_debug.write('Acquire_Locks_For_Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,3);
101        END IF;
102 
103        OPEN source_bdgt_vers_cur;
104 
105        OPEN target_bdgt_vers_cur;
106 
107    /*
108     * Increment the record_version_number of target version in
109     * pa_budget_versions and pa_proj_fp_options
110     */
111 
112    IF P_PA_DEBUG_MODE = 'Y' THEN
113         pa_debug.g_err_stage:='Incrementing record version number of target version in pa_proj_fp_options';
114         pa_debug.write('Acquire_Locks_For_Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,3);
115    END IF;
116 
117    UPDATE PA_PROJ_FP_OPTIONS
118    SET    record_version_number = record_version_number+1
119    WHERE  fin_plan_version_id=p_target_plan_version_id;
120 
121    IF P_PA_DEBUG_MODE = 'Y' THEN
122         pa_debug.g_err_stage:='Incrementing record version number of target version in pa_budget_versions';
123         pa_debug.write('Acquire_Locks_For_Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,3);
124    END IF;
125 
126    UPDATE PA_BUDGET_VERSIONS
127    SET    record_version_number = record_version_number+1
128    WHERE  budget_version_id = p_target_plan_version_id;
129 
130    IF P_PA_DEBUG_MODE = 'Y' THEN
131         pa_debug.g_err_stage:='Closing all the cursors ';
132         pa_debug.write('Acquire_Locks_For_Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,3);
133    END IF;
134 
135    CLOSE target_bdgt_vers_cur;
136    CLOSE source_bdgt_vers_cur;
137    CLOSE target_fp_opt_cur;
138    CLOSE source_fp_opt_cur;
139 IF P_PA_DEBUG_MODE = 'Y' THEN
140    pa_debug.reset_err_stack;
141 END IF;
142 EXCEPTION
143     WHEN Resource_Busy THEN
144           PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
145                                p_msg_name      => 'PA_UTIL_USER_LOCK_FAILED');
146 
147           IF P_PA_DEBUG_MODE = 'Y' THEN
148                pa_debug.g_err_stage:='Can not acquire lock.. exiting copy plan';
149                pa_debug.write('Acquire_Locks_For_Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,5);
150           END IF;
151 
152           IF source_fp_opt_cur%ISOPEN THEN
153               CLOSE source_fp_opt_cur;
154           END IF;
155 
156           IF source_bdgt_vers_cur%ISOPEN THEN
157               CLOSE source_bdgt_vers_cur;
158           END IF;
159 
160           IF target_fp_opt_cur%ISOPEN THEN
161              CLOSE target_fp_opt_cur;
162           END IF;
163 
164           IF target_bdgt_vers_cur%ISOPEN THEN
165               CLOSE target_bdgt_vers_cur;
166           END IF;
167 
168           l_msg_count := FND_MSG_PUB.count_msg;
169           IF l_msg_count = 1 THEN
170               PA_INTERFACE_UTILS_PUB.get_messages
171                    (p_encoded        => FND_API.G_TRUE
172                     ,p_msg_index      => 1
173                     ,p_msg_count      => l_msg_count
174                     ,p_msg_data       => l_msg_data
175                     ,p_data           => l_data
176                     ,p_msg_index_out  => l_msg_index_out);
177               x_msg_data := l_data;
178               x_msg_count := l_msg_count;
179           ELSE
180               x_msg_count := l_msg_count;
181           END IF;
182 
183           x_return_status:= FND_API.G_RET_STS_ERROR;
184 	IF P_PA_DEBUG_MODE = 'Y' THEN
185           pa_debug.reset_err_stack;
186 	END IF;
187           RAISE;
188 
189     WHEN OTHERS THEN
190 
191           IF P_PA_DEBUG_MODE = 'Y' THEN
192                pa_debug.g_err_stage:='Can not acquire lock.. exiting copy plan';
193                pa_debug.write('Acquire_Locks_For_Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,5);
194           END IF;
195 
196           IF source_fp_opt_cur%ISOPEN THEN
197               CLOSE source_fp_opt_cur;
198           END IF;
199 
200           IF source_bdgt_vers_cur%ISOPEN THEN
201               CLOSE source_bdgt_vers_cur;
202           END IF;
203 
204           IF target_fp_opt_cur%ISOPEN THEN
205              CLOSE target_fp_opt_cur;
206           END IF;
207 
208           IF target_bdgt_vers_cur%ISOPEN THEN
209               CLOSE target_bdgt_vers_cur;
210           END IF;
211 
212           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
213           x_msg_count     := 1;
214           x_msg_data      := SQLERRM;
215           FND_MSG_PUB.add_exc_msg( p_pkg_name=> 'PA_FP_COPY_FROM_PKG'
216                                    ,p_procedure_name  => 'ACQUIRE_LOCKS_FOR_COPY_PLAN');
217           IF P_PA_DEBUG_MODE = 'Y' THEN
218                pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
219                pa_debug.write('Acquire_Locks_For_Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,5);
220 
221           pa_debug.reset_err_stack;
222 	END IF;
223           RAISE;
224  END Acquire_Locks_For_Copy_Plan;
225 
226   /* =======================================================================
227      This is a main api which does the processing specific to copy
228      plan and then will call copy_version and delete version helper apis.
229 
230      4/16/2004 Raja FP M Phase II Copy Plan does not copy 'rate schedules',
231      and 'Generation Options' sub tab data.
232    ========================================================================*/
233 
234   PROCEDURE Copy_Plan(
235             p_source_plan_version_id    IN     NUMBER
236             ,p_target_plan_version_id   IN     NUMBER
237             ,p_adj_percentage           IN     NUMBER
238             ,x_return_status            OUT    NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
239             ,x_msg_count                OUT    NOCOPY NUMBER --File.Sql.39 bug 4440895
240             ,x_msg_data                 OUT    NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
241    AS
242 
243          l_adj_percentage     NUMBER;
244          l_msg_count          NUMBER :=0;
245          l_data               VARCHAR2(2000);
246          l_msg_data           VARCHAR2(2000);
247          l_error_msg_code     VARCHAR2(2000);
248          l_msg_index_out      NUMBER;
249          l_return_status      VARCHAR2(2000);
250          l_debug_mode         VARCHAR2(30);
251 
252          l_source_fp_pref_code pa_proj_fp_options.fin_plan_preference_code%TYPE ;
253          l_target_fp_pref_code pa_proj_fp_options.fin_plan_preference_code%TYPE ;
254          --l_locked_by_person_id pa_budget_versions.locked_by_person_id%TYPE;
255          l_project_id          pa_projects_all.project_id%TYPE;
256          l_source_resource_list_id    pa_budget_versions.resource_list_id%TYPE;
257          l_baselined_resource_list_id pa_budget_versions.resource_list_id%TYPE;
258 
259          --This variable contains the resource list id of baselined version
260 
261          l_baselined_version_id       pa_budget_versions.budget_version_id%TYPE;
262          l_target_plan_version_id     pa_budget_versions.budget_version_id%TYPE;
263          l_version_type               pa_budget_versions.version_type%TYPE;
264          l_fin_plan_type_id           pa_budget_versions.fin_plan_type_id%TYPE;
265          l_fp_options_id              pa_proj_fp_options.proj_fp_options_id%TYPE;
266          l_target_appr_rev_plan_flag  pa_budget_versions.approved_rev_plan_type_flag%TYPE;
267 
268          -- Start of Variables defined for bug 2729498
269 
270          l_target_fin_plan_type_id     pa_budget_versions.fin_plan_type_id%TYPE;
271          l_target_version_type        pa_budget_versions.version_type%TYPE;
272 
273          -- End of variables defined for bug 2729498
274 
275          Resource_Busy        EXCEPTION;
276          pragma exception_init(Resource_Busy,-00054);
277 
278          -- Start of plsql tables defined for bug#2729191
279 
280          TYPE txn_currency_code_tbl_typ  IS TABLE OF
281                     pa_fp_txn_currencies.txn_currency_code%TYPE INDEX BY BINARY_INTEGER;
282          TYPE default_rev_curr_flag_tbl_typ  IS TABLE OF
283                     pa_fp_txn_currencies.default_rev_curr_flag%TYPE INDEX BY BINARY_INTEGER;
284          TYPE default_cost_curr_flag_tbl_typ  IS TABLE OF
285                     pa_fp_txn_currencies.default_cost_curr_flag%TYPE INDEX BY BINARY_INTEGER;
286          TYPE default_all_curr_flag_tbl_typ  IS TABLE OF
287                     pa_fp_txn_currencies.default_all_curr_flag%TYPE INDEX BY BINARY_INTEGER;
288          TYPE project_currency_flag_tbl_typ  IS TABLE OF
289                     pa_fp_txn_currencies.project_currency_flag%TYPE INDEX BY BINARY_INTEGER;
290          TYPE projfunc_currency_flag_tbl_typ  IS TABLE OF
291                     pa_fp_txn_currencies.projfunc_currency_flag%TYPE INDEX BY BINARY_INTEGER;
292          TYPE pc_cost_exchange_rate_tbl_typ  IS TABLE OF
293                     pa_fp_txn_currencies.project_cost_exchange_rate%TYPE INDEX BY BINARY_INTEGER;
294          TYPE pc_rev_exchange_rate_tbl_typ  IS TABLE OF
295                     pa_fp_txn_currencies.project_rev_exchange_rate%TYPE INDEX BY BINARY_INTEGER;
296          TYPE pfc_cost_exchange_rate_tbl_typ  IS TABLE OF
297                     pa_fp_txn_currencies.projfunc_cost_exchange_rate%TYPE INDEX BY BINARY_INTEGER;
298          TYPE pfc_rev_exchange_rate_tbl_typ  IS TABLE OF
299                     pa_fp_txn_currencies.projfunc_rev_exchange_rate%TYPE INDEX BY BINARY_INTEGER;
300 
301          l_txn_currency_code_tbl        txn_currency_code_tbl_typ;
302          l_default_rev_curr_flag_tbl    default_rev_curr_flag_tbl_typ;
303          l_default_cost_curr_flag_tbl   default_cost_curr_flag_tbl_typ;
304          l_default_all_curr_flag_tbl    default_all_curr_flag_tbl_typ;
305          l_project_currency_flag_tbl    project_currency_flag_tbl_typ;
306          l_projfunc_currency_flag_tbl   projfunc_currency_flag_tbl_typ;
307          l_pc_cost_exchange_rate_tbl    pc_cost_exchange_rate_tbl_typ;
308          l_pc_rev_exchange_rate_tbl     pc_rev_exchange_rate_tbl_typ;
309          l_pfc_cost_exchange_rate_tbl   pfc_cost_exchange_rate_tbl_typ;
310          l_pfc_rev_exchange_rate_tbl    pfc_rev_exchange_rate_tbl_typ;
311 
312          l_source_plan_in_mc_flag       pa_proj_fp_options.plan_in_multi_curr_flag%TYPE;
313          l_source_appr_rev_plan_flag    pa_proj_fp_options.approved_rev_plan_type_flag%TYPE; /* Bug 3276128 */
314 
315          -- End of plsql tables defined for bug#2729191
316 
317          -- Start of cursors defined for bug#2729191
318 
319          CURSOR target_fp_options_cur IS
320          SELECT   proj_fp_options_id
321                  ,project_id
322                  ,fin_plan_type_id
323                  ,plan_in_multi_curr_flag
324                  ,projfunc_cost_rate_type
325                  ,projfunc_cost_rate_date_type
326                  ,projfunc_cost_rate_date
327                  ,projfunc_rev_rate_type
328                  ,projfunc_rev_rate_date_type
329                  ,projfunc_rev_rate_date
330                  ,project_cost_rate_type
331                  ,project_cost_rate_date_type
332                  ,project_cost_rate_date
333                  ,project_rev_rate_type
334                  ,project_rev_rate_date_type
335                  ,project_rev_rate_date
336          FROM    PA_PROJ_FP_OPTIONS
337          WHERE   fin_plan_version_id = p_target_plan_version_id;
338 
339          target_fp_options_rec target_fp_options_cur%ROWTYPE;
340 
341          CURSOR target_txn_currencies_cur IS
342          SELECT  txn_currency_code
343                 ,default_rev_curr_flag
344                 ,default_cost_curr_flag
345                 ,default_all_curr_flag
346                 ,project_currency_flag
347                 ,projfunc_currency_flag
348                 ,project_cost_exchange_rate
349                 ,project_rev_exchange_rate
350                 ,projfunc_cost_exchange_rate
351                 ,projfunc_rev_exchange_rate
352          FROM   PA_FP_TXN_CURRENCIES
353          WHERE  fin_plan_version_id = p_target_plan_version_id
354          AND    proj_fp_options_id = target_fp_options_rec.proj_fp_options_id; -- bug 2779637
355 
356          -- End of cursors defined for bug#2729191
357    BEGIN
358 
359       FND_MSG_PUB.INITIALIZE;
360       x_msg_count := 0;
361       x_return_status := FND_API.G_RET_STS_SUCCESS;
362 
363 IF P_PA_DEBUG_MODE = 'Y' THEN
364       pa_debug.init_err_stack('PA_FP_COPY_FROM_PKG.Copy_Plan');
365 END IF;
366       fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
367       l_debug_mode := NVL(l_debug_mode, 'Y');
368 IF P_PA_DEBUG_MODE = 'Y' THEN
369       pa_debug.set_process('PLSQL','LOG',l_debug_mode);
370 END IF;
371      /*
372       * Check if  source_verion_id, target_version_id are NULL, if so throw
373       * an error message
374       */
375 
376      IF P_PA_DEBUG_MODE = 'Y' THEN
377           pa_debug.g_err_stage := 'Checking for valid parameters:';
378           pa_debug.write('Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,3);
379      END IF;
380 
381      IF (p_source_plan_version_id IS NULL) OR
382         (p_target_plan_version_id IS NULL)
383      THEN
384          IF P_PA_DEBUG_MODE = 'Y' THEN
385               pa_debug.g_err_stage := 'Source_plan='||p_source_plan_version_id;
386               pa_debug.write('Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,5);
387               pa_debug.g_err_stage := 'Target_plan'||p_target_plan_version_id;
388               pa_debug.write('Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,5);
389          END IF;
390 
391          PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
392                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
393          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
394      END IF;
395 
396      l_adj_percentage := NVL(p_adj_percentage,0);
397 
398      --  Doing business validations before proceeding furthur
399 
400      IF P_PA_DEBUG_MODE = 'Y' THEN
401           pa_debug.g_err_stage:='Fetching the source plan preference code';
402           pa_debug.write('Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,3);
403      END IF;
404 
405      SELECT fin_plan_preference_code
406            ,plan_in_multi_curr_flag      -- Bug#2729191
407            ,nvl(approved_rev_plan_type_flag,'N') /* Bug#3276128 */
408      INTO   l_source_fp_pref_code
409            ,l_source_plan_in_mc_flag     -- Bug#2729191
410            ,l_source_appr_rev_plan_flag /* Bug#3276128 */
411      FROM   pa_proj_fp_options
412      WHERE  fin_plan_version_id=p_source_plan_version_id;
413 
414      IF P_PA_DEBUG_MODE = 'Y' THEN
415           pa_debug.g_err_stage:='Fetching the target plan preference code';
416           pa_debug.write('Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,3);
417      END IF;
418 
419      SELECT fin_plan_preference_code,nvl(approved_rev_plan_type_flag,'N')
420      INTO   l_target_fp_pref_code,l_target_appr_rev_plan_flag
421      FROM   pa_proj_fp_options
422      WHERE  fin_plan_version_id=p_target_plan_version_id;
423 
424      IF P_PA_DEBUG_MODE = 'Y' THEN
425           pa_debug.g_err_stage:='Checking the compatability of the plans';
426           pa_debug.write('Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,3);
427      END IF;
428 
429      IF (l_source_fp_pref_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY AND
430                                  l_target_fp_pref_code = PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY)
431          OR ( l_source_fp_pref_code = PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY AND
432                                  l_target_fp_pref_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY)
433          OR (l_target_fp_pref_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SEP)
434          OR (l_source_fp_pref_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SEP)
435      THEN
436            IF P_PA_DEBUG_MODE = 'Y' THEN
437                 pa_debug.g_err_stage:='Versions are incompatible';
438                 pa_debug.write('Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,5);
439            END IF;
440 
441            PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
442                                 p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
443            RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
444      END IF;
445 
446      /* Bug 3149010 - Included the following validation */
447      /* Bug 3276128 - Included source appr rev plan flag condition.
448         If source plan is appr rev, then, no issues in copying
449         from the source to any type of version */
450       /* 3156057: Commenting the following code as from FP.M it is allowed to copy
451          mc enabled budget version into approved revenue budget versions **
452 
453 
454      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
455            IF P_PA_DEBUG_MODE = 'Y' THEN
456                 pa_debug.g_err_stage:='Cannot copy a mc enabled version into a appr rev plan type version. Bug 3149010';
457                 pa_debug.write('Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,5);
458            END IF;
459 
460            PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
461                                 p_msg_name      => 'PA_FP_CP_INV_MC_TO_APPR_REV');
462            RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
463 
464      END IF;  */
465 
466      IF P_PA_DEBUG_MODE = 'Y' THEN
467           pa_debug.g_err_stage := 'Parameter validation complete';
468           pa_debug.write('Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,3);
469      END IF;
470 
471      /* Bug #2616445: Commented out the following code for checking the lock as the
472         user will be able to edit plan only if it is not locked by somebody else.
473 
474       --Checking if target version is locked or not
475 
476 IF P_PA_DEBUG_MODE = 'Y' THEN
477       pa_debug.g_err_stage:='Checking if pa_budget_versions is locked';
478 END IF;
479       IF P_PA_DEBUG_MODE = 'Y' THEN
480          pa_debug.write('Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,3);
481       END IF;
482 
483       SELECT locked_by_person_id
484              ,project_id
485       INTO   l_locked_by_person_id
486              ,l_project_id
487       FROM   PA_BUDGET_VERSIONS
488       WHERE  budget_version_id = p_target_plan_version_id;
489 
490       IF l_locked_by_person_id IS NOT NULL THEN
491            PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
492                                 p_msg_name      => 'PA_FP_VERSION_ALREADY_LOCKED');
493            RAISE Resource_Busy;
494       END IF; */
495 
496       IF P_PA_DEBUG_MODE = 'Y' THEN
497            pa_debug.g_err_stage:='Getting the project id';
498            pa_debug.write('Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,3);
499       END IF;
500 
501       SELECT project_id
502       INTO   l_project_id
503       FROM   PA_BUDGET_VERSIONS
504       WHERE  budget_version_id = p_target_plan_version_id;
505 
506       --Acquire lock on pa_proj_fp_options and pa_budget_versions so that
507       --no other process would be able to modify these tables and all
508       --underlying child tables
509 
510       Acquire_Locks_For_Copy_Plan(
511               p_source_plan_version_id  =>  p_source_plan_version_id
512               ,p_target_plan_version_id =>  p_target_plan_version_id
513               ,x_return_status          =>  l_return_status
514               ,x_msg_count              =>  l_msg_count
515               ,x_msg_data               =>  l_msg_data );
516 
517       -- Start of changes for bug 2729191
518 
519       -- target_fp_options_rec contains the mc flag and also the mc conversion
520       -- attributes stored in the options table
521 
522       OPEN   target_fp_options_cur;
523       FETCH  target_fp_options_cur INTO target_fp_options_rec;
524       CLOSE  target_fp_options_cur;
525 
526       /* If target version has MC enabled and the source version isn't MC
527          enabled then the target version should preserve its MC attributes
528          and txn currencies. But, delete_version_helper followed by copy_version
529          override the MC flag and txn currencies for the target version.
530          Since this being a specific case to copy plan, store the required data
531          locally and update the target version once copy_version is complete */
532 
533       IF ((target_fp_options_rec.plan_in_multi_curr_flag = 'Y' AND l_source_plan_in_mc_flag = 'N') OR
534          (l_source_plan_in_mc_flag = 'Y' and l_target_appr_rev_plan_flag = 'Y' )) -- added for 3156057
535       THEN
536 
537           OPEN target_txn_currencies_cur;
538                IF P_PA_DEBUG_MODE = 'Y' THEN
539                     pa_debug.g_err_stage:='target_txn_currencies_cur is opened';
540                     pa_debug.write('Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,3);
541                END IF;
542 
543                FETCH target_txn_currencies_cur BULK COLLECT INTO
544                       l_txn_currency_code_tbl
545                      ,l_default_rev_curr_flag_tbl
546                      ,l_default_cost_curr_flag_tbl
547                      ,l_default_all_curr_flag_tbl
548                      ,l_project_currency_flag_tbl
549                      ,l_projfunc_currency_flag_tbl
550                      ,l_pc_cost_exchange_rate_tbl
551                      ,l_pc_rev_exchange_rate_tbl
552                      ,l_pfc_cost_exchange_rate_tbl
553                      ,l_pfc_rev_exchange_rate_tbl;
554 
555                IF P_PA_DEBUG_MODE = 'Y' THEN
556                     pa_debug.g_err_stage:='no of txn currencies fetched are '||SQL%ROWCOUNT;
557                     pa_debug.write('Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,3);
558                END IF;
559           CLOSE target_txn_currencies_cur;
560 
561           IF P_PA_DEBUG_MODE = 'Y' THEN
562                pa_debug.g_err_stage:='target_txn_currencies_cur is closed';
563                pa_debug.write('Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,3);
564           END IF;
565 
566       END IF;
567       -- End of changes for bug 2729191
568 
569       -- Calling an api to delete the existing records of target_version in
570       -- pa_proj_periods_denorm, p_fin_plan_adj_lines, pa_fp_adj_elements,
571       -- pa_budget_lines,pa_resource_assignments
572 
573       IF P_PA_DEBUG_MODE = 'Y' THEN
574            pa_debug.g_err_stage:='Calling the delete version api';
575            pa_debug.write('Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,3);
576       END IF;
577 
578       PA_FIN_PLAN_PUB.DELETE_VERSION_HELPER(
579                      p_budget_version_id => p_target_plan_version_id
580                      ,x_return_status    => l_return_status
581                      ,x_msg_count        => l_msg_count
582                      ,x_msg_data         => l_msg_data );
583 
584       /* Bug# 2647047 - Raise if return status is not success */
585       IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
586         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
587       END IF;
588 
589       --Calling the api to copy source version to target version
590 
591       l_target_plan_version_id := p_target_plan_version_id;
592 
593       IF P_PA_DEBUG_MODE = 'Y' THEN
594            pa_debug.g_err_stage:='Calling the copy version api';
595            pa_debug.write('Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,3);
596       END IF;
597 
598       PA_FIN_PLAN_PUB.COPY_VERSION(
599                      p_project_id          => l_project_id
600                      ,p_source_version_id  => p_source_plan_version_id
601                      ,p_copy_mode          => PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_WORKING
602                      ,px_target_version_id => l_target_plan_version_id
603                      ,p_adj_percentage     => l_adj_percentage
604                      ,p_calling_module     => PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_FIN_PLAN
605                      ,x_return_status      => l_return_status
606                      ,x_msg_count          => l_msg_count
607                      ,x_msg_data           => l_msg_data);
608 
609       /* Bug# 2647047 - Raise if return status is not success */
610       IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
611         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
612       END IF;
613 
614       -- Start of changes for bug 2729191
615 
616       IF target_fp_options_rec.plan_in_multi_curr_flag = 'Y' AND
617          l_source_plan_in_mc_flag = 'N'
618       THEN
619 
620           -- Delete the txn currencies that are copied from source version to target version
621 
622           IF P_PA_DEBUG_MODE = 'Y' THEN
623                pa_debug.g_err_stage:='Deleting the txn currencies of the target version after copy_version';
624                pa_debug.write('Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,3);
625           END IF;
626 
627           DELETE FROM pa_fp_txn_currencies
628           WHERE fin_plan_version_id = p_target_plan_version_id
629           AND   proj_fp_options_id = target_fp_options_rec.proj_fp_options_id; -- bug 2779637
630 
631           -- Update the Multi_Curr_Flag and the MC attributes of the target fp option
632 
633           IF P_PA_DEBUG_MODE = 'Y' THEN
634                pa_debug.g_err_stage:='Updating the target proj fp option with the MC attributes';
635                pa_debug.write('Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,3);
636           END IF;
637 
638           UPDATE PA_PROJ_FP_OPTIONS
639           SET     plan_in_multi_curr_flag      =  target_fp_options_rec.plan_in_multi_curr_flag
640                  ,projfunc_cost_rate_type      =  target_fp_options_rec.projfunc_cost_rate_type
641                  ,projfunc_cost_rate_date_type =  target_fp_options_rec.projfunc_cost_rate_date_type
642                  ,projfunc_cost_rate_date      =  target_fp_options_rec.projfunc_cost_rate_date
643                  ,projfunc_rev_rate_type       =  target_fp_options_rec.projfunc_rev_rate_type
644                  ,projfunc_rev_rate_date_type  =  target_fp_options_rec.projfunc_rev_rate_date_type
645                  ,projfunc_rev_rate_date       =  target_fp_options_rec.projfunc_rev_rate_date
646                  ,project_cost_rate_type       =  target_fp_options_rec.project_cost_rate_type
647                  ,project_cost_rate_date_type  =  target_fp_options_rec.project_cost_rate_date_type
648                  ,project_cost_rate_date       =  target_fp_options_rec.project_cost_rate_date
649                  ,project_rev_rate_type        =  target_fp_options_rec.project_rev_rate_type
650                  ,project_rev_rate_date_type   =  target_fp_options_rec.project_rev_rate_date_type
651                  ,project_rev_rate_date        =  target_fp_options_rec.project_rev_rate_date
652           WHERE  fin_plan_version_id = p_target_plan_version_id;
653 
654           -- Insert the txn currencies of the target version present earlier to copy version
655           -- which are stored in the plsql tables
656 
657           IF NVL(l_txn_currency_code_tbl.last,0) > 0 THEN
658 
659                IF P_PA_DEBUG_MODE = 'Y' THEN
660                     pa_debug.g_err_stage:='Inserting the txn currencies of the target version
661                                            present earlier to copy version';
662                     pa_debug.write('Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,3);
663                END IF;
664 
665                FORALL i IN l_txn_currency_code_tbl.first..l_txn_currency_code_tbl.last
666                     INSERT INTO PA_FP_TXN_CURRENCIES (
667                           fp_txn_currency_id
668                           ,proj_fp_options_id
669                           ,project_id
670                           ,fin_plan_type_id
671                           ,fin_plan_version_id
672                           ,txn_currency_code
673                           ,default_rev_curr_flag
674                           ,default_cost_curr_flag
675                           ,default_all_curr_flag
676                           ,project_currency_flag
677                           ,projfunc_currency_flag
678                           ,last_update_date
679                           ,last_updated_by
680                           ,creation_date
681                           ,created_by
682                           ,last_update_login
683                           ,project_cost_exchange_rate
684                           ,project_rev_exchange_rate
685                           ,projfunc_cost_exchange_rate
686                           ,projfunc_rev_exchange_rate  )
687                     SELECT
688                            pa_fp_txn_currencies_s.NEXTVAL
689                            ,target_fp_options_rec.proj_fp_options_id
690                            ,target_fp_options_rec.project_id
691                            ,target_fp_options_rec.fin_plan_type_id
692                            ,p_target_plan_version_id
693                            ,l_txn_currency_code_tbl(i)
694                            ,l_default_rev_curr_flag_tbl(i)
695                            ,l_default_cost_curr_flag_tbl(i)
696                            ,l_default_all_curr_flag_tbl(i)
697                            ,l_project_currency_flag_tbl(i)
698                            ,l_projfunc_currency_flag_tbl(i)
699                            ,SYSDATE
700                            ,fnd_global.user_id
701                            ,SYSDATE
702                            ,fnd_global.user_id
703                            ,fnd_global.login_id
704                            ,l_pc_cost_exchange_rate_tbl(i)
705                            ,l_pc_rev_exchange_rate_tbl(i)
706                            ,l_pfc_cost_exchange_rate_tbl(i)
707                            ,l_pfc_rev_exchange_rate_tbl(i)
708                     FROM   DUAL;
709           END IF;
710       END IF;
711       -- End of changes for bug 2729191
712 
713       IF P_PA_DEBUG_MODE = 'Y' THEN
714            pa_debug.g_err_stage:='Exiting Copy_Plan';
715            pa_debug.write('Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,3);
716            pa_debug.reset_err_stack;
717       END IF;
718   EXCEPTION
719       WHEN resource_busy THEN
720           IF P_PA_DEBUG_MODE = 'Y' THEN
721                pa_debug.g_err_stage:='Can not acquire lock.. exiting copy plan';
722                pa_debug.write('Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,5);
723           END IF;
724 
725           l_msg_count := FND_MSG_PUB.count_msg;
726           IF l_msg_count = 1 THEN
727               PA_INTERFACE_UTILS_PUB.get_messages
728                    (p_encoded        => FND_API.G_TRUE
729                     ,p_msg_index      => 1
730                     ,p_msg_count      => l_msg_count
731                     ,p_msg_data       => l_msg_data
732                     ,p_data           => l_data
733                     ,p_msg_index_out  => l_msg_index_out);
734               x_msg_data := l_data;
735               x_msg_count := l_msg_count;
736           ELSE
737               x_msg_count := l_msg_count;
738           END IF;
739 
740           ROLLBACK;
741           x_return_status:= FND_API.G_RET_STS_ERROR;
742 IF P_PA_DEBUG_MODE = 'Y' THEN
743           pa_debug.reset_err_stack;
744 END IF;
745    WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
746         l_msg_count := FND_MSG_PUB.count_msg;
747         IF l_msg_count = 1 THEN
748              PA_INTERFACE_UTILS_PUB.get_messages
749                    (p_encoded        => FND_API.G_TRUE
750                     ,p_msg_index      => 1
751                     ,p_msg_count      => l_msg_count
752                     ,p_msg_data       => l_msg_data
753                     ,p_data           => l_data
754                     ,p_msg_index_out  => l_msg_index_out);
755              x_msg_data := l_data;
756              x_msg_count := l_msg_count;
757         ELSE
758             x_msg_count := l_msg_count;
759         END IF;
760 
761         ROLLBACK;
762         x_return_status:= FND_API.G_RET_STS_ERROR;
763 IF P_PA_DEBUG_MODE = 'Y' THEN
764         pa_debug.reset_err_stack;
765 END IF;
766    WHEN Others THEN
767         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
768         x_msg_count     := 1;
769         x_msg_data      := SQLERRM;
770         FND_MSG_PUB.add_exc_msg( p_pkg_name=> 'PA_FP_COPY_FROM_PKG'
771                         ,p_procedure_name  => 'COPY_PLAN');
772         IF P_PA_DEBUG_MODE = 'Y' THEN
773              pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
774              pa_debug.write('Copy_Plan: ' || g_module_name,pa_debug.g_err_stage,5);
775         pa_debug.reset_err_stack;
776 	END IF;
777         ROLLBACK;
778         RAISE;
779   END Copy_Plan;
780 
781   /*===========================================================================
782    This function is used to return fin plan amount type for given plan version
783   ===========================================================================*/
784   FUNCTION Get_Fin_Plan_Amount_Type (
785           p_fin_plan_version_id IN pa_proj_fp_options.fin_plan_version_id%TYPE)
786   RETURN  VARCHAR2
787   IS
788      l_amount_type  VARCHAR2(30);
789   BEGIN
790       SELECT DECODE(fin_plan_preference_code
791                      ,PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME, 'A'
792                      ,PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY,      'R'
793                      ,PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY,         'C')
794       INTO   l_amount_type
795       FROM   PA_PROJ_FP_OPTIONS
796       WHERE  fin_plan_version_id = p_fin_plan_version_id;
797 
798       RETURN l_amount_type;
799   END Get_Fin_Plan_Amount_Type;
800 
801 
802 /*=============================================================================
803   This procedure is used to acquire all the required locks for copy_actual
804 ==============================================================================*/
805 
806 PROCEDURE Acquire_Locks_For_Copy_Actual(
807           p_plan_version_id  IN pa_proj_fp_options.fin_plan_version_id%TYPE
808           ,x_return_status OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
809           ,x_msg_count     OUT NOCOPY NUMBER --File.Sql.39 bug 4440895
810           ,x_msg_data      OUT NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
811 AS
812        l_msg_count          NUMBER :=0;
813        l_data               VARCHAR2(2000);
814        l_msg_data           VARCHAR2(2000);
815        l_msg_index_out      NUMBER;
816        l_debug_mode         VARCHAR2(30);
817        Resource_Busy        EXCEPTION;
818        pragma exception_init(Resource_Busy,-00054);
819 
820        CURSOR fp_opt_cur IS
821            SELECT record_version_number
822            FROM  PA_PROJ_FP_OPTIONS
823            WHERE fin_plan_version_id = p_plan_version_id
824            FOR UPDATE NOWAIT;
825 
826        CURSOR bdgt_ver_cur IS
827            SELECT record_version_number
828            FROM   PA_BUDGET_VERSIONS
829            WHERE  budget_version_id = p_plan_version_id
830            FOR UPDATE NOWAIT;
831 BEGIN
832 
833     x_msg_count := 0;
834     x_return_status := FND_API.G_RET_STS_SUCCESS;
835 
836 IF P_PA_DEBUG_MODE = 'Y' THEN
837     pa_debug.set_err_stack('PA_FP_COPY_FROM_PKG.Acquire_Lock_For_Copy_Actual');
838 END IF;
839     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
840     l_debug_mode := NVL(l_debug_mode, 'Y');
841 
842    /*
843     * Acquire lock on pa_proj_fp_options and pa_budget_versions so that
844     * no other process would be able to modify these tables and all
845     * underlying child tables
846     */
847 
848      IF P_PA_DEBUG_MODE = 'Y' THEN
849          pa_debug.g_err_stage:='Acquiring lock on pa_proj_fp_options';
850          pa_debug.write('Acquire_Locks_For_Copy_Actual: ' || g_module_name,pa_debug.g_err_stage,3);
851      END IF;
852 
853      OPEN fp_opt_cur;
854 
855      IF P_PA_DEBUG_MODE = 'Y' THEN
856          pa_debug.g_err_stage:='Acquiring lock on pa_budget_versions';
857          pa_debug.write('Acquire_Locks_For_Copy_Actual: ' || g_module_name,pa_debug.g_err_stage,3);
858      END IF;
859 
860      OPEN bdgt_ver_cur;
861 
862      --Increment the record_version_number in pa_budget_versions and
863      --pa_proj_fp_options
864 
865      IF P_PA_DEBUG_MODE = 'Y' THEN
866          pa_debug.g_err_stage:='Incrementing record version number of plan version pa_proj_fp_options';
867          pa_debug.write('Acquire_Locks_For_Copy_Actual: ' || g_module_name,pa_debug.g_err_stage,3);
868      END IF;
869 
870      UPDATE PA_PROJ_FP_OPTIONS
871      SET    record_version_number = record_version_number+1
872      WHERE  fin_plan_version_id=p_plan_version_id;
873 
874      IF P_PA_DEBUG_MODE = 'Y' THEN
875          pa_debug.g_err_stage:='Incrementing record version number of plan version in pa_budget_versions';
876          pa_debug.write('Acquire_Locks_For_Copy_Actual: ' || g_module_name,pa_debug.g_err_stage,3);
877      END IF;
878 
879      UPDATE PA_BUDGET_VERSIONS
880      SET    record_version_number = record_version_number+1
881      WHERE  budget_version_id = p_plan_version_id ;
882 
883      IF P_PA_DEBUG_MODE = 'Y' THEN
884          pa_debug.g_err_stage:='Closing fp_opt_cur and bdgt_ver_cur cursors';
885          pa_debug.write('Acquire_Locks_For_Copy_Actual: ' || g_module_name,pa_debug.g_err_stage,3);
886      END IF;
887 
888      CLOSE fp_opt_cur;
889      CLOSE bdgt_ver_cur;
890 IF P_PA_DEBUG_MODE = 'Y' THEN
891      pa_debug.reset_err_stack;
892 END IF;
893 EXCEPTION
894 
895   WHEN Resource_Busy THEN
896         PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
897                              p_msg_name      => 'PA_UTIL_USER_LOCK_FAILED');
898 
899         IF fp_opt_cur%ISOPEN THEN
900             CLOSE fp_opt_cur;
901         END IF;
902 
903         IF bdgt_ver_cur%ISOPEN THEN
904              CLOSE bdgt_ver_cur;
905         END IF;
906 
907         IF P_PA_DEBUG_MODE = 'Y' THEN
908             pa_debug.g_err_stage:='Unable to acquire lock';
909             pa_debug.write('Acquire_Locks_For_Copy_Actual: ' || g_module_name,pa_debug.g_err_stage,5);
910         END IF;
911 
912         x_return_status := FND_API.G_RET_STS_ERROR;
913         l_msg_count := FND_MSG_PUB.count_msg;
914         IF l_msg_count = 1 THEN
915             PA_INTERFACE_UTILS_PUB.get_messages
916                  (p_encoded        => FND_API.G_TRUE
917                   ,p_msg_index      => 1
918                   ,p_msg_count      => l_msg_count
919                   ,p_msg_data       => l_msg_data
920                   ,p_data           => l_data
921                   ,p_msg_index_out  => l_msg_index_out);
922             x_msg_data := l_data;
923             x_msg_count := l_msg_count;
924         ELSE
925             x_msg_count := l_msg_count;
926         END IF;
927 IF P_PA_DEBUG_MODE = 'Y' THEN
928         pa_debug.reset_err_stack;
929 END IF;
930         RAISE;
931 
932    WHEN Others THEN
933         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
934         x_msg_count     := 1;
935         x_msg_data      := SQLERRM;
936         FND_MSG_PUB.add_exc_msg( p_pkg_name=> 'PA_FP_COPY_FROM_PKG'
937                         ,p_procedure_name  => 'ACQUIRE_LOCKS_FOR_COPY_ACTUAL');
938 
939         IF P_PA_DEBUG_MODE = 'Y' THEN
940             pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
941             pa_debug.write('Acquire_Locks_For_Copy_Actual: ' || g_module_name,pa_debug.g_err_stage,5);
942             pa_debug.reset_err_stack;
943 	END IF;
944         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
945 
946 END Acquire_Locks_For_Copy_Actual;
947 
948 
949 /*=========================================================================
950   This api will be used for two cases.
951   Case 1: When a new budget version is to be created from a source version.
952           px_target_version_id is passed as null in this case
953   Case 2: When the target budget version needs to be modified as the
954           specified  source budget version.
955           Non_null value for px_target_version_id is passed in this case.
956   If the adjustment percentage is zero, the amounts are copied as they are.
957   If adjustment percentage is non-zero, then amount coluns are copied as
958   null and rollup api takes care of them.
959 
960 --
961 --
962 -- 26-JUN-2003 jwhite        - Plannable Task Dev Effort:
963 --                             Make code changes Copy_Budget_Version
964 --                             procedure to
965 --                             enable population of new parameters on
966 --                             insert pa_budget_versions
967 --
968 -- 01-AUG-2003 jwhite        - Bug 3079891
969 --                             For Copy_Budget_Version, hardcoded
970 --                             the following columns to NULL:
971 --                             - request_id
972 --                             - plan_processing_code
973 --
974 
975 
976 
977  r11.5 FP.M Developement ----------------------------------
978 
979  08-JAN-2004 jwhite        Bug 3362316
980 
981                            Extensively rewrote Copy_Budget_Version
982                            -  CURSOR l_bv_csr IS
983                            -  INSERT INTO PA_BUDGET_VERSIONS (
984                            -  UPDATE pa_budget_versions
985 
986  29-JAN-2004 sgoteti       Bug 3354518: Added the parameter
987                            p_struct_elem_version_id
988 
989  16-APR-2004 rravipat      Bug 3354518 FP M Phase II Development
990                            When copy_budget_version is called during copy plan, amount
991                            generation related columnslike last_amt_gen_date would be
992                            updated as null. This is because none of the init columns are
993                            copied from source version to target.
994 
995 10-Jun-05 Bug 4337221: dbora
996  if the calling context is workplan, then derive the adjustment percentage
997  always as 0, so that the version level rolled up quantity and cost amounts get
998  copied from the source version, as it is to the target version.
999 --Bug 4290043.Included parameter to indicate whether to copy actual info or not.
1000 =========================================================================*/
1001 
1002 PROCEDURE Copy_Budget_Version(
1003         p_source_project_id      IN     NUMBER
1004        ,p_target_project_id      IN     NUMBER
1005        ,p_source_version_id      IN     NUMBER
1006        ,p_copy_mode              IN     VARCHAR2
1007        ,p_adj_percentage         IN     NUMBER
1008        ,p_calling_module         IN     VARCHAR2
1009        ,p_shift_days             IN     NUMBER
1010        ,p_copy_actuals_flag      IN     VARCHAR2
1011        ,px_target_version_id     IN OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
1012        ,p_struct_elem_version_id IN     pa_budget_versions.budget_version_id%TYPE --Bug 3354518
1013        ,x_return_status          OUT    NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
1014        ,x_msg_count              OUT    NOCOPY NUMBER --File.Sql.39 bug 4440895
1015        ,x_msg_data               OUT    NOCOPY VARCHAR2 ) --File.Sql.39 bug 4440895
1016 AS
1017 
1018     -- Variables to be used for debugging purpose
1019 
1020     l_msg_count       NUMBER := 0;
1021     l_data            VARCHAR2(2000);
1022     l_msg_data        VARCHAR2(2000);
1023     l_error_msg_code  VARCHAR2(2000);
1024     l_msg_index_out   NUMBER;
1025     l_return_status   VARCHAR2(2000);
1026     l_debug_mode      VARCHAR2(30);
1027 
1028     l_adj_percentage  NUMBER;
1029     l_source_project_id      pa_projects_all.project_id%TYPE;
1030     l_target_project_id      pa_projects_all.project_id%TYPE;
1031     l_version_name           pa_budget_versions.version_name%TYPE;
1032     l_fin_plan_type_id       pa_proj_fp_options.fin_plan_type_id%TYPE;
1033     l_current_profile_id     pa_budget_versions.period_profile_id%TYPE;
1034     l_max_version            pa_budget_versions.version_number%TYPE;
1035     l_version_number         pa_budget_versions.version_number%TYPE;
1036     l_version_type           pa_budget_versions.version_type%TYPE;
1037     l_budget_version_id      pa_budget_versions.budget_version_id%TYPE;
1038     l_budget_status_code     pa_budget_versions.budget_status_code%TYPE;
1039     l_current_flag           pa_budget_versions.current_flag%TYPE;
1040     l_current_working_flag   pa_budget_versions.current_working_flag%TYPE;
1041     l_baselined_by_person_id pa_budget_versions.baselined_by_person_id%TYPE;
1042     l_baselined_date         pa_budget_versions.baselined_date%TYPE;
1043 
1044     l_cost_flag      VARCHAR2(1);
1045     l_revenue_flag       VARCHAR2(1);
1046 
1047     l_ci_id                      NUMBER;
1048 
1049      -- jwhite, 26-JUN-2003: Added for Plannable Task Dev Effort ------------------
1050 
1051      l_refresh_required_flag          VARCHAR2(1)  := NULL;
1052      l_process_code                   VARCHAR2(30) := NULL;
1053 
1054      -- rravipat 3/26/2004 Added for FP M Phase II copy project impact
1055 
1056      l_wbs_struct_version_id          pa_budget_versions.project_structure_version_id%TYPE;
1057      l_source_cur_planning_period     pa_budget_versions.current_planning_period%TYPE;
1058      l_target_cur_planning_period     pa_budget_versions.current_planning_period%TYPE;
1059      l_time_phased_code               pa_proj_fp_options.cost_time_phased_code%TYPE;
1060 
1061      l_gl_start_period            gl_periods.period_name%TYPE;
1062      l_gl_end_period              gl_periods.period_name%TYPE;
1063      l_gl_start_Date              VARCHAR2(100);
1064      l_pa_start_period            pa_periods_all.period_name%TYPE;
1065      l_pa_end_period              pa_periods_all.period_name%TYPE;
1066      l_pa_start_date              VARCHAR2(100);
1067      l_plan_version_exists_flag   VARCHAR2(1);
1068      l_prj_start_date             VARCHAR2(100);
1069      l_prj_end_date               VARCHAR2(100);
1070 
1071      -- ---------------------------------------------------------------------------
1072 
1073 
1074 
1075     -- Bug 3362316, 08-JAN-2003: Added New FP.M Columns  --------------------------
1076 
1077     CURSOR l_bv_csr IS
1078           SELECT   resource_list_id /* Added for bug# 2757847 */
1079                   ,labor_quantity   /* Added for the bug #2645579. */
1080                   ,raw_cost
1081                   ,burdened_cost
1082                   ,revenue
1083                   ,pm_product_code
1084                   ,pm_budget_reference
1085                   ,wf_status_code
1086                   ,adw_notify_flag
1087                   ,NULL --prc_generated_flag  --Bug 5099353
1088                   ,plan_run_date
1089                   ,plan_processing_code
1090                   ,total_borrowed_revenue
1091                   ,total_revenue_adj
1092                   ,total_lent_resource_cost
1093                   ,total_cost_adj
1094                   ,total_unassigned_time_cost
1095                   ,total_utilization_percent
1096                   ,total_utilization_hours
1097                   ,total_utilization_adj
1098                   ,total_capacity
1099                   ,total_head_count
1100                   ,total_head_count_adj
1101                   ,total_tp_cost_in
1102                   ,total_tp_cost_out
1103                   ,total_tp_revenue_in
1104                   ,total_tp_revenue_out
1105                   ,total_project_raw_cost
1106                   ,total_project_burdened_cost
1107                   ,total_project_revenue
1108                   ,period_profile_id /* Added for #2587671 */
1109                   ,object_type_code
1110                   ,object_id
1111                --   ,primary_cost_forecast_flag   FP M Phase II Dev changes this column should not be updated
1112                --   ,primary_rev_forecast_flag    FP M Phase II Dev changes this column should not be updated
1113                --   ,rev_partially_impl_flag      FP M Phase II Dev changes this column should not be updated
1114                   ,equipment_quantity
1115                   ,pji_summarized_flag
1116                   ,wp_version_flag
1117                   ,current_planning_period
1118                   ,period_mask_id
1119                   ,last_amt_gen_date
1120                   ,actual_amts_thru_period
1121                   ,project_structure_version_id
1122                   ,etc_start_date --Bug 3927244
1123           FROM    pa_budget_versions
1124           WHERE   budget_version_id = p_source_version_id;
1125 
1126     -- ---------------------------------------------------------------------------
1127 
1128 
1129     l_bv_rec   l_bv_csr%ROWTYPE;
1130 
1131     -- Bug 3927244
1132     l_src_plan_class_code pa_fin_plan_types_b.plan_class_code%TYPE;
1133     l_trg_plan_class_code pa_fin_plan_types_b.plan_class_code%TYPE;
1134 
1135     CURSOR get_plan_class_code_csr(c_budget_version_id pa_budget_versions.budget_version_id%TYPE) IS
1136     SELECT pfb.plan_class_code,pbv.project_id
1137     FROM   pa_fin_plan_types_b pfb,
1138            pa_budget_versions  pbv
1139     WHERE  pbv.budget_version_id = c_budget_version_id
1140     AND    pbv.fin_plan_type_id  = pfb.fin_plan_type_id;
1141     -- Bug 3927244
1142 
1143 
1144 BEGIN
1145 
1146     x_msg_count := 0;
1147     x_return_status := FND_API.G_RET_STS_SUCCESS;
1148     IF p_pa_debug_mode = 'Y' THEN
1149             pa_debug.set_err_stack('PA_FP_COPY_FROM_PKG.Copy_Budget_Version');
1150             fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
1151             l_debug_mode := NVL(l_debug_mode, 'Y');
1152             pa_debug.set_process('PLSQL','LOG',p_pa_debug_mode);
1153     END IF;
1154 
1155     --Check if  source_verion_id is NULL, if so throw an error message
1156 
1157     IF P_PA_DEBUG_MODE = 'Y' THEN
1158             pa_debug.g_err_stage := 'Parameter Validation';
1159             pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
1160     END IF;
1161 
1162     IF (p_source_version_id IS NULL) OR
1163        (p_calling_module IS NULL)
1164     THEN
1165          IF P_PA_DEBUG_MODE = 'Y' THEN
1166                pa_debug.g_err_stage := 'Source_plan='||p_source_version_id;
1167                pa_debug.write(g_module_name,pa_debug.g_err_stage,5);
1168                pa_debug.g_err_stage := 'Calling_module='||p_calling_module;
1169                pa_debug.write(g_module_name,pa_debug.g_err_stage,5);
1170          END IF;
1171 
1172          PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
1173                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
1174 
1175          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
1176 
1177     END IF;
1178 
1179     --Throw an error if the struct element version id is not passed in workplan context
1180     IF p_calling_module=PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_WORKPLAN AND
1181        p_struct_elem_version_id IS NULL THEN
1182 
1183         IF P_PA_DEBUG_MODE = 'Y' THEN
1184                 pa_debug.g_err_stage := 'In workplan context p_struct_elem_version_id passed is '||p_struct_elem_version_id;
1185                 pa_debug.write(g_module_name,pa_debug.g_err_stage,5);
1186         END IF;
1187         PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
1188                              p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
1189 
1190         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
1191     END IF;
1192 
1193     IF P_PA_DEBUG_MODE = 'Y' THEN
1194             pa_debug.g_err_stage := 'Parameter validation complete';
1195             pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
1196     END IF;
1197 
1198     IF p_calling_module = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_WORKPLAN THEN
1199         -- Bug 4337221:
1200         l_adj_percentage := 0;
1201     ELSE
1202         -- Make the adjustment percentage zero if it is null
1203 
1204         l_adj_percentage := NVL(p_adj_percentage,0);
1205     END IF;
1206 
1207     IF p_pa_debug_mode = 'Y' THEN
1208             pa_debug.g_err_stage := 'Source_plan='||p_source_version_id;
1209             pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
1210             pa_debug.g_err_stage := 'Target_plan='||px_target_version_id;
1211             pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
1212             pa_debug.g_err_stage := 'Calling_module='||p_calling_module;
1213             pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
1214             pa_debug.g_err_stage := 'Adj_percentage='||l_adj_percentage;
1215             pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
1216     END IF;
1217 
1218     -- Get the fin plan type id, source version name, project id from
1219     -- pa_budget_versions using source_version_id
1220 
1221     IF px_target_version_id IS NULL THEN
1222 
1223          IF P_PA_DEBUG_MODE = 'Y' THEN
1224                  pa_debug.g_err_stage := 'Fetching version name,fin_plan_type_id of source version';
1225                  pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
1226          END IF;
1227 
1228          SELECT NVL(p_target_project_id,project_id)
1229                 ,NVL(p_source_project_id,project_id)
1230                 ,version_name
1231                 ,fin_plan_type_id
1232                 ,version_type
1233                 ,ci_id
1234                 ,current_planning_period
1235          INTO   l_target_project_id
1236                 ,l_source_project_id
1237                 ,l_version_name
1238                 ,l_fin_plan_type_id
1239                 ,l_version_type
1240                 ,l_ci_id
1241                 ,l_source_cur_planning_period
1242          FROM   pa_budget_versions
1243          WHERE  budget_version_id = p_source_version_id;
1244 
1245          -- 3/30/2004 Raja FP M Phase II Dev Changes
1246          -- If source project and target project are different do not copy
1247          -- the current planning period from souce version. They should be
1248          -- defaulted to PA/GL period inwhich nvl(project start date, sysdate)
1249          -- falls
1250 
1251          IF l_source_project_id <> l_target_project_id  THEN
1252 
1253               --Bug 4200168. Call Pa_Prj_Period_Profile_Utils.Get_Prj_Defaults only if source
1254               --current planning period is not null and target time phased code is P or G.
1255               l_time_phased_code := PA_FIN_PLAN_UTILS.Get_Time_Phased_code(p_source_version_id);
1256               IF ( (l_time_phased_code = 'P'  OR l_time_phased_code = 'G') AND l_source_cur_planning_period IS NOT NULL)  THEN
1257               Pa_Prj_Period_Profile_Utils.Get_Prj_Defaults(
1258                    p_project_id                 => p_target_project_id
1259                   ,p_info_flag                  => 'ALL'
1260                   ,p_create_defaults            => 'N'
1261                   ,x_gl_start_period            => l_gl_start_period
1262                   ,x_gl_end_period              => l_gl_end_period
1263                   ,x_gl_start_Date              => l_gl_start_Date
1264                   ,x_pa_start_period            => l_pa_start_period
1265                   ,x_pa_end_period              => l_pa_end_period
1266                   ,x_pa_start_date              => l_pa_start_date
1267                   ,x_plan_version_exists_flag   => l_plan_version_exists_flag
1268                   ,x_prj_start_date             => l_prj_start_date
1269                   ,x_prj_end_date               => l_prj_end_date             );
1270               END IF;
1271 
1272               --l_time_phased_code := PA_FIN_PLAN_UTILS.Get_Time_Phased_code(p_source_version_id);
1273 
1274               IF l_source_cur_planning_period IS NOT NULL THEN
1275 
1276                   IF  l_time_phased_code = 'P' THEN
1277                       l_target_cur_planning_period := l_pa_start_period;
1278                   ELSIF l_time_phased_code = 'G'  THEN
1279                       l_target_cur_planning_period := l_gl_start_period;
1280                   END IF;
1281 
1282              END IF;
1283          ELSE
1284               l_target_cur_planning_period := l_source_cur_planning_period;
1285          END IF;
1286 
1287          --Get the version_number, version_name
1288 
1289          IF P_PA_DEBUG_MODE = 'Y' THEN
1290                  pa_debug.g_err_stage := 'Fetch the maximum version number';
1291                  pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
1292          END IF;
1293 
1294          IF  p_copy_mode = PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_WORKING THEN
1295 
1296               -- Get the max version number of working versions for this plan type
1297               --start of changes for bug:- 2570250
1298               /*
1299               SELECT  NVL(MAX(version_number),0)
1300               INTO    l_max_version
1301               FROM    pa_budget_versions
1302               WHERE   project_id = l_project_id
1303                 AND   fin_plan_type_id = l_fin_plan_type_id
1304                 AND   budget_status_code IN (PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_WORKING,
1305                                                  PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_SUBMITTED); */
1306               PA_FIN_PLAN_UTILS.Get_Max_Budget_Version_Number
1307                         (p_project_id          =>   l_target_project_id
1308                         ,p_fin_plan_type_id    =>   l_fin_plan_type_id
1309                         ,p_version_type        =>   l_version_type
1310                         ,p_copy_mode           =>   PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_WORKING
1311                         ,p_ci_id               =>   l_ci_id /* FP M changes */
1312                         ,p_lock_required_flag  =>   'Y'
1313                         ,x_version_number      =>   l_max_version
1314                         ,x_return_status       =>   l_return_status
1315                         ,x_msg_count           =>   l_msg_count
1316                         ,x_msg_data            =>   l_msg_data );
1317 
1318               --end of changes for bug :- 2570250
1319 
1320               l_version_number := l_max_version + 1;
1321               l_budget_status_code := PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_WORKING;
1322               l_current_flag := 'N';
1323 
1324               -- baselined info should be null for a working version
1325 
1326               l_baselined_date := NULL;
1327               l_baselined_by_person_id := NULL;
1328 
1329               /* #2634622: The version name will not be appended with 'Copy' from now on.
1330                  From any calling place, where the version name is expected to be different,
1331                  an update would be done to the pa_budget_versions directly.
1332 
1333                  FND_MESSAGE.SET_NAME('PA','PA_FP_COPY_MESSAGE');
1334                  l_version_name:= l_version_name ||'-'||FND_MESSAGE.GET;
1335               */
1336 
1337               IF l_version_number = 1 AND l_ci_id is null THEN
1338                     l_current_working_flag := 'Y';
1339               ELSE
1340                     l_current_working_flag := 'N';
1341               END IF;
1342 
1343           ELSIF p_copy_mode = PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_BASELINED THEN
1344 
1345               -- Get the max version number of baselined versions for this plan type
1346 
1347               --start  of changes for bug :- 2570250
1348               /*
1349               SELECT  NVL(MAX(version_number),0)
1350               INTO    l_max_version
1351               FROM    pa_budget_versions
1352               WHERE   project_id = l_project_id
1353                 AND   fin_plan_type_id = l_fin_plan_type_id
1354                 AND   budget_status_code = PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_BASELINED;
1355               */
1356 
1357               PA_FIN_PLAN_UTILS.Get_Max_Budget_Version_Number
1358                         (p_project_id          =>   l_target_project_id
1359                         ,p_fin_plan_type_id    =>   l_fin_plan_type_id
1360                         ,p_version_type        =>   l_version_type
1361                         ,p_copy_mode           =>   PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_BASELINED
1362                         ,p_ci_id               =>   l_ci_id /* FP  M changes */
1363                         ,p_lock_required_flag  =>   'Y'
1364                         ,x_version_number      =>   l_max_version
1365                         ,x_return_status       =>   l_return_status
1366                         ,x_msg_count           =>   l_msg_count
1367                         ,x_msg_data            =>   l_msg_data );
1368 
1369               --end of changes for bug :- 2570250
1370 
1371               l_version_number := l_max_version+1;
1372               l_budget_status_code := PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_BASELINED;
1373               l_current_flag := 'Y';
1374 
1375               -- For bug 3858601
1376               -- Stamp employee_id in baseliend_by_person_id of pa_budget_versions table when
1377               -- a budget version is baselined instead of user id.
1378 
1379               l_baselined_date := SYSDATE;
1380               --l_baselined_by_person_id := FND_GLOBAL.USER_ID;
1381 
1382               SELECT employee_id
1383               INTO l_baselined_by_person_id
1384               FROM fnd_user
1385               where user_id = FND_GLOBAL.USER_ID;
1386               --End of bug 3858601
1387 
1388               -- Bug # 2615988. The message 'Copy' Should not be suffixed to
1389               -- the version name when the mode is 'B'.
1390               -- FND_MESSAGE.SET_NAME('PA','PA_FP_COPY_MESSAGE');
1391               -- l_version_name:= l_version_name ||'-'||FND_MESSAGE.GET;
1392 
1393           END IF;
1394 
1395           -- Fetch new budget_version_id
1396 
1397           IF P_PA_DEBUG_MODE = 'Y' THEN
1398                   pa_debug.g_err_stage := 'Fetch new budget version id';
1399                   pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
1400           END IF;
1401 
1402           SELECT pa_budget_versions_s.NEXTVAL
1403           INTO   px_target_version_id
1404           FROM   DUAL;
1405 
1406           -- For workplan context project structure version id should be populated
1407           -- For Finplan the column is not maintained
1408 
1409           IF  p_struct_elem_version_id IS NOT NULL  AND
1410               p_calling_module=PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_WORKPLAN
1411           THEN
1412                l_wbs_struct_version_id :=  p_struct_elem_version_id;
1413 
1414           ELSE
1415                l_wbs_struct_version_id := NULL;
1416           END IF;
1417 
1418      /* This fix is done during IB1 testing of FP M. There are some flows, which
1419  *  * are creation more than one budget version for the same workplan version.
1420  *  To
1421  *   * identify such flows, the following check is being made so that dev can
1422  *   fix
1423  *    * such issues */
1424 
1425      Declare
1426           l_exists varchar2(1);
1427      Begin
1428           Select 'Y'
1429           Into   l_exists
1430           From   pa_budget_versions a
1431           Where  project_structure_version_id = l_wbs_struct_version_id
1432           And    wp_version_flag = 'Y'
1433           And    exists (select 'x' from pa_budget_versions b where b.budget_version_id = p_source_version_id and b.wp_version_flag = 'Y') ;
1434 
1435           IF P_PA_DEBUG_MODE = 'Y' THEN
1436              pa_debug.g_err_stage:='proj sv id = ' || l_wbs_struct_version_id;
1437              pa_debug.write(g_module_name,pa_debug.g_err_stage,5);
1438              pa_debug.g_err_stage:='calling module = ' || p_calling_module;
1439              pa_debug.write(g_module_name,pa_debug.g_err_stage,5);
1440              pa_debug.g_err_stage:='copy mode = ' || p_copy_mode;
1441              pa_debug.write(g_module_name,pa_debug.g_err_stage,5);
1442              pa_debug.g_err_stage:='source version id = ' || p_source_version_id;
1443              pa_debug.write(g_module_name,pa_debug.g_err_stage,5);
1444              pa_debug.g_err_stage:='source project id / target project id = ' || l_source_project_id || ' / ' || l_target_project_id;
1445              pa_debug.write(g_module_name,pa_debug.g_err_stage,5);
1446          END IF;
1447           PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
1448                               p_msg_name      => 'DUPLICATE_WP_BEING_CREATED');
1449           IF P_PA_DEBUG_MODE = 'Y' THEN
1450              pa_debug.g_err_stage:='Invalid Arguments Passed';
1451              pa_debug.write(g_module_name,pa_debug.g_err_stage,5);
1452          END IF;
1453          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
1454      Exception
1455           When No_Data_Found Then
1456                Null;
1457      End;
1458 
1459           -- Insert a new row in pa_budget_versions
1460 
1461           -- Bug 3362316, 08-JAN-2003: Added New FP.M Columns  --------------------------
1462 
1463           INSERT INTO PA_BUDGET_VERSIONS (
1464                    budget_version_id
1465                    ,project_id
1466                    ,budget_type_code
1467                    ,version_number
1468                    ,budget_status_code
1469                    ,last_update_date
1470                    ,last_updated_by
1471                    ,creation_date
1472                    ,created_by
1473                    ,last_update_login
1474                    ,current_flag
1475                    ,original_flag
1476                    ,current_original_flag
1477                    ,resource_accumulated_flag
1478                    ,resource_list_id
1479                    ,version_name
1480                    ,budget_entry_method_code
1481                    ,baselined_by_person_id
1482                    ,baselined_date
1483                    ,change_reason_code
1484                    ,labor_quantity
1485                    ,labor_unit_of_measure
1486                    ,raw_cost
1487                    ,burdened_cost
1488                    ,revenue
1489                    ,description
1490                    ,attribute_category
1491                    ,attribute1
1492                    ,attribute2
1493                    ,attribute3
1494                    ,attribute4
1495                    ,attribute5
1496                    ,attribute6
1497                    ,attribute7
1498                    ,attribute8
1499                    ,attribute9
1500                    ,attribute10
1501                    ,attribute11
1502                    ,attribute12
1503                    ,attribute13
1504                    ,attribute14
1505                    ,attribute15
1506                    ,first_budget_period
1507                    ,pm_product_code
1508                    ,pm_budget_reference
1509                    ,wf_status_code
1510                    ,adw_notify_flag
1511                    ,prc_generated_flag
1512                    ,plan_run_date
1513                    ,plan_processing_code
1514                    ,period_profile_id
1515                    ,fin_plan_type_id
1516                    ,parent_plan_version_id
1517                    ,project_structure_version_id
1518                    ,current_working_flag
1519                    ,total_borrowed_revenue
1520                    ,total_revenue_adj
1521                    ,total_lent_resource_cost
1522                    ,total_cost_adj
1523                    ,total_unassigned_time_cost
1524                    ,total_utilization_percent
1525                    ,total_utilization_hours
1526                    ,total_utilization_adj
1527                    ,total_capacity
1528                    ,total_head_count
1529                    ,total_head_count_adj
1530                    ,version_type
1531                    ,total_tp_cost_in
1532                    ,total_tp_cost_out
1533                    ,total_tp_revenue_in
1534                    ,total_tp_revenue_out
1535                    ,record_version_number
1536                    ,request_id
1537                    ,total_project_raw_cost
1538                    ,total_project_burdened_cost
1539                    ,total_project_revenue
1540                    ,locked_by_person_id
1541                    ,approved_cost_plan_type_flag
1542                    ,approved_rev_plan_type_flag
1543                    ,process_update_wbs_flag
1544                    ,object_type_code
1545                    ,object_id
1546                    ,primary_cost_forecast_flag
1547                    ,primary_rev_forecast_flag
1548                    ,rev_partially_impl_flag
1549                    ,equipment_quantity
1550                    ,pji_summarized_flag
1551                    ,wp_version_flag
1552                    ,current_planning_period
1553                    ,period_mask_id
1554                    ,last_amt_gen_date
1555                    ,actual_amts_thru_period
1556                    ,ci_id -- Raja FP M 06 JUl 04 bug 3677924
1557                    ,etc_start_date -- Bug 3763322
1558           )
1559           SELECT    px_target_version_id
1560                     ,l_target_project_id
1561                     ,pbv.budget_type_code
1562                     ,l_version_number            --local_variable
1563                     ,l_budget_status_code        --local_variable
1564                     ,sysdate
1565                     ,fnd_global.user_id
1566                     ,sysdate
1567                     ,fnd_global.user_id
1568                     ,fnd_global.login_id
1569                     ,l_current_flag             --local_variable
1570                     ,DECODE(p_copy_mode, PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_WORKING ,'N',
1571                             DECODE (l_version_number,1,'Y','N')) --original_flag
1572                     ,DECODE(p_copy_mode, PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_WORKING ,'N',
1573                             DECODE (l_version_number,1,'Y','N')) --current_original_flag
1574                     ,'N'           --resource_accumulated_flag
1575                     ,pbv.resource_list_id
1576                     ,SUBSTR(l_version_name,1,60)  --local_variable
1577                     ,pbv.budget_entry_method_code
1578                     ,l_baselined_by_person_id  --local_variable
1579                     ,l_baselined_date          --local_variable
1580                     ,pbv.change_reason_code
1581                     ,pbv.labor_quantity
1582                     ,pbv.labor_unit_of_measure
1583                     ,DECODE(l_adj_percentage, 0, pbv.raw_cost, NULL)
1584                     ,DECODE(l_adj_percentage, 0, pbv.burdened_cost, NULL)
1585                     ,DECODE(l_adj_percentage, 0, pbv.revenue, NULL)
1586                     ,pbv.description
1587                     ,pbv.attribute_category
1588                     ,pbv.attribute1
1589                     ,pbv.attribute2
1590                     ,pbv.attribute3
1591                     ,pbv.attribute4
1592                     ,pbv.attribute5
1593                     ,pbv.attribute6
1594                     ,pbv.attribute7
1595                     ,pbv.attribute8
1596                     ,pbv.attribute9
1597                     ,pbv.attribute10
1598                     ,pbv.attribute11
1599                     ,pbv.attribute12
1600                     ,pbv.attribute13
1601                     ,pbv.attribute14
1602                     ,pbv.attribute15
1603                     ,pbv.first_budget_period
1604                     ,pbv.pm_product_code
1605                     ,pbv.pm_budget_reference
1606                     ,NULL                       --Bug 5532326 : wf_status_code is not copied
1607                     ,pbv.adw_notify_flag
1608                     ,NULL --pbv.prc_generated_flag --Bug 5099353
1609                     ,pbv.plan_run_date
1610                     ,NULL                          -- bug 3079891, 01-AUG-03, jwhite: replaced pbv.plan_processing_code
1611                     ,period_profile_id
1612                     ,pbv.fin_plan_type_id
1613                     ,pbv.parent_plan_version_id
1614                     ,nvl(l_wbs_struct_version_id,project_structure_version_id) -- Raja nvl should be removed post april 07
1615                     ,l_current_working_flag  --local_variable
1616                     ,pbv.total_borrowed_revenue
1617                     ,pbv.total_revenue_adj
1618                     ,pbv.total_lent_resource_cost
1619                     ,pbv.total_cost_adj
1620                     ,pbv.total_unassigned_time_cost
1621                     ,pbv.total_utilization_percent
1622                     ,pbv.total_utilization_hours
1623                     ,pbv.total_utilization_adj
1624                     ,pbv.total_capacity
1625                     ,pbv.total_head_count
1626                     ,pbv.total_head_count_adj
1627                     ,pbv.version_type
1628                     ,pbv.total_tp_cost_in
1629                     ,pbv.total_tp_cost_out
1630                     ,pbv.total_tp_revenue_in
1631                     ,pbv.total_tp_revenue_out
1632                     ,1       --record_version_number
1633                     ,NULL                            -- bug 3079891, 01-AUG-03, jwhite: replaced fnd_global.conc_request_id
1634                     ,DECODE(l_adj_percentage, 0,
1635                             pbv.total_project_raw_cost, NULL)
1636                     ,DECODE(l_adj_percentage, 0,
1637                             pbv.total_project_burdened_cost, NULL)
1638                     ,DECODE(l_adj_percentage, 0,
1639                             pbv.total_project_revenue, NULL)
1640                     ,NULL   --locked_by_person_id
1641                     ,approved_cost_plan_type_flag
1642                     ,approved_rev_plan_type_flag
1643                     ,l_refresh_required_flag
1644                     ,pbv.object_type_code
1645                     ,l_target_project_id  -- object_id  bug 3594111
1646                     ,pbv.primary_cost_forecast_flag
1647                     ,pbv.primary_rev_forecast_flag
1648                     ,pbv.rev_partially_impl_flag
1649                     ,pbv.equipment_quantity
1650                     ,'N'--This should always be N as the PJI API will be called later and that API will look at this flag
1651                     --Summarization will happen only if this flag has 'N' as value.
1652                     ,pbv.wp_version_flag
1653                     ,l_target_cur_planning_period -- 3/30/2004 FP M Phase II Dev Changes
1654                     ,pbv.period_mask_id
1655                     ,pbv.last_amt_gen_date --Bug 4228859
1656                     ,decode(p_copy_actuals_flag,'N',null,pbv.actual_amts_thru_period) -- Bug 3927244
1657                     ,l_ci_id -- Raja FP M 06 JUl 04 bug 3677924
1658                     ,decode(p_copy_actuals_flag,'N',null,pbv.etc_start_date) -- Bug 3927244
1659           FROM      PA_BUDGET_VERSIONS pbv
1660           WHERE     pbv.budget_version_id = p_source_version_id;
1661 
1662     -- End, jwhite, 26-JUN-2003: Plannable Task Effort --------------------------------
1663 
1664     -- End: Bug 3362316, 08-JAN-2003: Added New FP.M Columns  --------------------------
1665 
1666 
1667     ELSE --if target_version_id is passed then we update the version
1668 
1669          IF p_calling_module = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_FIN_PLAN THEN
1670 
1671                --To decide what amounts are to be copied from source to target
1672                --version set local flags using target fin plan preference code
1673 
1674 
1675                IF P_PA_DEBUG_MODE = 'Y' THEN
1676                        pa_debug.g_err_stage := 'Get values into local flags';
1677                        pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
1678                END IF;
1679 
1680 
1681 /* UT Fix : decoded G_PREF_COST_AND_REV_SAME as Y for revenue_flag */
1682 
1683                SELECT DECODE(fin_plan_preference_code,
1684                              PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY, 'Y',
1685                              PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY, 'N',
1686                              PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME, 'Y')  --cost_flag
1687                       ,DECODE(fin_plan_preference_code,
1688                               PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY, 'N',
1689                               PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY, 'Y',
1690                               PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME, 'Y') --revenue_flag
1691                INTO   l_cost_flag
1692                       ,l_revenue_flag
1693                FROM   pa_proj_fp_options
1694                WHERE  fin_plan_version_id = px_target_version_id;
1695 
1696                --Set the cost and revenue flags to 'N' if adj percentage is  nonzero.
1697 
1698                IF l_adj_percentage <> 0 THEN
1699 
1700                    l_cost_flag := 'N';
1701                    l_revenue_flag := 'N';
1702 
1703                END IF;
1704 
1705                --Update target version using source version values
1706 
1707 
1708                IF P_PA_DEBUG_MODE = 'Y' THEN
1709                        pa_debug.g_err_stage := 'Updating target version';
1710                        pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
1711                END IF;
1712 
1713                OPEN l_bv_csr;
1714 
1715 
1716                IF P_PA_DEBUG_MODE = 'Y' THEN
1717                        pa_debug.g_err_stage := 'l_bv_csr is opened';
1718                        pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
1719                END IF;
1720 
1721                FETCH l_bv_csr INTO l_bv_rec;
1722 
1723                -- Bug 3927244: Actuals need to be copied from budget to budget or forecast to forecast
1724                -- within the same project for FINPLAN versions
1725 
1726                OPEN  get_plan_class_code_csr(p_source_version_id);
1727                FETCH get_plan_class_code_csr
1728                INTO  l_src_plan_class_code,l_source_project_id;
1729                CLOSE get_plan_class_code_csr;
1730 
1731                OPEN  get_plan_class_code_csr(px_target_version_id);
1732                FETCH get_plan_class_code_csr
1733                INTO  l_trg_plan_class_code,l_target_project_id;
1734                CLOSE get_plan_class_code_csr;
1735 
1736                l_source_project_id := NVL(p_source_project_id,l_source_project_id);
1737                l_target_project_id := NVL(p_target_project_id,l_target_project_id);
1738 
1739                -- End: Bug 3927244
1740 
1741                -- Bug 3362316, 08-JAN-2003: Added New FP.M Columns  --------------------------
1742 
1743 
1744                IF l_bv_csr%FOUND THEN
1745 
1746                   UPDATE pa_budget_versions
1747                   SET     resource_list_id              = l_bv_rec.resource_list_id /* Added for bug# 2757847 */
1748                          ,labor_quantity                = l_bv_rec.labor_quantity   /* Added for bug# 2645579 */
1749                          ,raw_cost                      = DECODE(l_adj_percentage, 0, DECODE(l_cost_flag,'Y',l_bv_rec.raw_cost,NULL), NULL)
1750                          ,burdened_cost                 = DECODE(l_adj_percentage, 0, DECODE(l_cost_flag,'Y',l_bv_rec.burdened_cost,NULL), NULL)
1751                          ,revenue                       = DECODE(l_adj_percentage, 0, DECODE(l_revenue_flag,'Y',l_bv_rec.revenue,NULL), NULL)
1752                          ,pm_product_code               = l_bv_rec.pm_product_code
1753                          ,pm_budget_reference           = l_bv_rec.pm_budget_reference
1754                          -- Bug 5532326. This column should not be copied in copy flow
1755                          --,wf_status_code                = l_bv_rec.wf_status_code
1756                          ,adw_notify_flag               = l_bv_rec.adw_notify_flag
1757                          ,prc_generated_flag            = NULL --l_bv_rec.prc_generated_flag --Bug 5099353
1758                          ,plan_run_date                 = l_bv_rec.plan_run_date
1759                          ,plan_processing_code          = NULL --l_bv_rec.plan_processing_code  fix for bug 4463404
1760                          ,total_borrowed_revenue        = l_bv_rec.total_borrowed_revenue
1761                          ,total_revenue_adj             = l_bv_rec.total_revenue_adj
1762                          ,total_lent_resource_cost      = l_bv_rec.total_lent_resource_cost
1763                          ,total_cost_adj                = l_bv_rec.total_cost_adj
1764                          ,total_unassigned_time_cost    = l_bv_rec.total_unassigned_time_cost
1765                          ,total_utilization_percent     = l_bv_rec.total_utilization_percent
1766                          ,total_utilization_hours       = l_bv_rec.total_utilization_hours
1767                          ,total_utilization_adj         = l_bv_rec.total_utilization_adj
1768                          ,total_capacity                = l_bv_rec.total_capacity
1769                          ,total_head_count              = l_bv_rec.total_head_count
1770                          ,total_head_count_adj          = l_bv_rec.total_head_count_adj
1771                          ,total_tp_cost_in              = l_bv_rec.total_tp_cost_in
1772                          ,total_tp_cost_out             = l_bv_rec.total_tp_cost_out
1773                          ,total_tp_revenue_in           = l_bv_rec.total_tp_revenue_in
1774                          ,total_tp_revenue_out          = l_bv_rec.total_tp_revenue_out
1775                          ,record_version_number         = record_version_number + 1
1776                          ,request_id                    = NULL  --FND_GLOBAL.conc_request_id  fix for bug 4463404
1777                          ,total_project_raw_cost        = DECODE(l_adj_percentage, 0, DECODE(l_cost_flag,'Y',l_bv_rec.total_project_raw_cost,NULL), NULL)
1778                          ,total_project_burdened_cost   = DECODE(l_adj_percentage, 0, DECODE(l_cost_flag,'Y',l_bv_rec.total_project_burdened_cost,NULL), NULL)
1779                          ,total_project_revenue         = DECODE(l_adj_percentage, 0, DECODE(l_revenue_flag,'Y',l_bv_rec.total_project_revenue,NULL), NULL)
1780                          ,object_type_code              = l_bv_rec.object_type_code
1781                          ,object_id                     = l_bv_rec.object_id
1782               -- FP M Phase II           ,primary_cost_forecast_flag    = l_bv_rec.primary_cost_forecast_flag
1783               -- FP M Phase II           ,primary_rev_forecast_flag     = l_bv_rec.primary_rev_forecast_flag
1784               -- FP M Phase II           ,rev_partially_impl_flag       = l_bv_rec.rev_partially_impl_flag
1785                          ,equipment_quantity            = l_bv_rec.equipment_quantity
1786                          ,pji_summarized_flag           = l_bv_rec.pji_summarized_flag
1787                          ,wp_version_flag               = l_bv_rec.wp_version_flag
1788                          ,current_planning_period       = l_bv_rec.current_planning_period
1789                          ,period_mask_id                = l_bv_rec.period_mask_id
1790                          -- Bug 3927244
1791                          ,last_amt_gen_date             = l_bv_rec.last_amt_gen_date --Bug 4228859
1792                          ,actual_amts_thru_period       = decode(p_copy_actuals_flag,'N',null,l_bv_rec.actual_amts_thru_period)
1793                          ,etc_start_date                = decode(p_copy_actuals_flag,'N',null,l_bv_rec.etc_start_date)
1794                          -- End: Bug 3927244
1795                          ,project_structure_version_id  = l_bv_rec.project_structure_version_id
1796                   WHERE  budget_version_id = px_target_version_id;
1797 
1798                -- END: Bug 3362316, 08-JAN-2003: Added New FP.M Columns  --------------------------
1799 
1800 
1801                END IF;
1802 
1803                CLOSE l_bv_csr;
1804 
1805 
1806                IF P_PA_DEBUG_MODE = 'Y' THEN
1807                        pa_debug.g_err_stage := 'l_bv_csr is closed';
1808                        pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
1809                END IF;
1810 
1811                END IF; -- p_calling_module
1812 
1813     END IF;
1814           -- bug fix 2933695
1815           -- Copy attachments which are associated with the budget version
1816           fnd_attached_documents2_pkg.copy_attachments
1817             (X_from_entity_name        => 'PA_BUDGET_VERSIONS',
1818              X_from_pk1_value          => to_char(p_source_version_id),
1819              X_to_entity_name          => 'PA_BUDGET_VERSIONS',
1820              X_to_pk1_value            => px_target_version_id,
1821              X_created_by              => FND_GLOBAL.USER_ID,
1822              X_last_update_login       => FND_GLOBAL.LOGIN_ID,
1823              X_program_application_id  => FND_GLOBAL.PROG_APPL_ID);
1824 
1825 
1826     IF P_PA_DEBUG_MODE = 'Y' THEN
1827             pa_debug.g_err_stage := 'Exiting copy_budget_version';
1828             pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
1829             pa_debug.reset_err_stack;
1830     END IF;
1831 
1832 EXCEPTION
1833 
1834      WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
1835           l_msg_count := FND_MSG_PUB.count_msg;
1836           IF l_msg_count = 1 THEN
1837                PA_INTERFACE_UTILS_PUB.get_messages
1838                      (p_encoded        => FND_API.G_TRUE
1839                       ,p_msg_index      => 1
1840                       ,p_msg_count      => l_msg_count
1841                       ,p_msg_data       => l_msg_data
1842                       ,p_data           => l_data
1843                       ,p_msg_index_out  => l_msg_index_out);
1844                x_msg_data := l_data;
1845                x_msg_count := l_msg_count;
1846           ELSE
1847               x_msg_count := l_msg_count;
1848           END IF;
1849           x_return_status := FND_API.G_RET_STS_ERROR;
1850 
1851           IF P_PA_DEBUG_MODE = 'Y' THEN
1852                    pa_debug.g_err_stage:='Invalid Arguments Passed';
1853                    pa_debug.write(g_module_name,pa_debug.g_err_stage,5);
1854                    pa_debug.reset_err_stack;
1855           END IF;
1856           RAISE;
1857 
1858      WHEN Others THEN
1859           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1860           x_msg_count     := 1;
1861           x_msg_data      := SQLERRM;
1862           FND_MSG_PUB.add_exc_msg( p_pkg_name      => 'PA_FP_COPY_FROM_PKG'
1863                                   ,p_procedure_name  => 'COPY_BUDGET_VERSION');
1864 
1865           IF P_PA_DEBUG_MODE = 'Y' THEN
1866                   pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
1867                   pa_debug.write(g_module_name,pa_debug.g_err_stage,5);
1868                   pa_debug.reset_err_stack;
1869           END IF;
1870 
1871           RAISE;
1872 
1873 END Copy_Budget_Version;
1874 
1875 
1876 
1877 /*====================================================================================
1878    Bug 3354518 - FP M changes - This is an overloaded API. This API will be called
1879    from pa_fp_planning_transaction_pub.copy_planning_transactions.This API will be used
1880    to populate the global temporary table PA_FP_RA_MAP_TEMP which will be used for
1881    the creation of target resource assignment records.
1882 
1883    New columns in pa_fp_ra_map_tmp that will be used for FP M are given below
1884    planning_start_Date -> planning_start_date for target resource assignment id
1885    planning_end_Date   -> planning_end_date for target resource assignment id
1886    schedule_start_Date -> schedule_start_date for target resource assignment id .. For TA specifically..
1887    schedule_end_Date   -> schedule_end_date for target resource assignment id .. For TA specifically..
1888    system_reference1   -> source element version id
1889    system_reference2   -> target element version id
1890    system_reference3   -> project assignment id for the target resoruce assignment id
1891    system_reference4   -> resource list member id for the target resoruce assignment id
1892 
1893    p_src_ra_id_tbl          -> The tbl containing the source ra ids which should be copied into the target
1894    p_src_elem_ver_id_tbl    -> source element version ids which should be copied into the target
1895    p_targ_elem_ver_id_tbl   -> target element version ids corresponding to the source element version ids
1896    p_targ_proj_assmt_id_tbl -> target project assignment ids corresponding to the source resource assignment ids
1897    p_targ_rlm_id_tbl -> target resource list member ids corresponding to the source resource assignment ids
1898 
1899    Bug 3622134    May 12 2004  Raja
1900                   When target element version id is 0 target task id should be
1901                   initialised as 0. Select should not be fired
1902    Bug 3615617 - FP M IB2 changes - Raja
1903       For workplan context target rlm id would be passed for each source resource assignment
1904 =====================================================================================*/
1905 PROCEDURE create_res_task_maps(
1906           p_context                  IN      VARCHAR2  --Can be WORKPLAN, BUDGET
1907          ,p_src_ra_id_tbl            IN      SYSTEM.PA_NUM_TBL_TYPE
1908          ,p_src_elem_ver_id_tbl      IN      SYSTEM.PA_NUM_TBL_TYPE
1909          ,p_targ_elem_ver_id_tbl     IN      SYSTEM.PA_NUM_TBL_TYPE
1910          ,p_targ_proj_assmt_id_tbl   IN      SYSTEM.PA_NUM_TBL_TYPE
1911          ,p_targ_rlm_id_tbl          IN      SYSTEM.PA_NUM_TBL_TYPE -- Bug 3615617
1912          ,p_planning_start_date_tbl  IN      SYSTEM.PA_DATE_TBL_TYPE
1913          ,p_planning_end_date_tbl    IN      SYSTEM.PA_DATE_TBL_TYPE
1914          ,p_schedule_start_date_tbl  IN      SYSTEM.PA_DATE_TBL_TYPE
1915          ,p_schedule_end_date_tbl    IN      SYSTEM.PA_DATE_TBL_TYPE
1916          ,x_return_status            OUT     NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
1917          ,x_msg_count                OUT     NOCOPY NUMBER --File.Sql.39 bug 4440895
1918          ,x_msg_data                 OUT     NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
1919 IS
1920     i                                          NUMBER;
1921     l_targ_proj_element_id                     pa_proj_elements.proj_element_id%TYPE;
1922     --Start of variables used for debugging
1923     l_return_status                            VARCHAR2(1);
1924     l_msg_count                                NUMBER := 0;
1925     l_msg_data                                 VARCHAR2(2000);
1926     l_data                                     VARCHAR2(2000);
1927     l_msg_index_out                            NUMBER;
1928     l_debug_mode                               VARCHAR2(30);
1929     l_debug_level3                    CONSTANT NUMBER :=3;
1930     l_debug_level5                    CONSTANT NUMBER :=5;
1931     l_module_name                              VARCHAR2(200) :=  g_module_name || '.create_res_task_maps';
1932     l_schedule_start_date_tbl                  SYSTEM.PA_DATE_TBL_TYPE := SYSTEM.PA_DATE_TBL_TYPE();
1933     l_schedule_end_date_tbl                    SYSTEM.PA_DATE_TBL_TYPE := SYSTEM.PA_DATE_TBL_TYPE();
1934     --End of variables used for debugging
1935     --Bug 4201936
1936     l_temp                                     VARCHAR2(15);
1937     l_src_elem_ver_id                          pa_proj_element_versions.element_version_id%TYPE;
1938     l_targ_elem_ver_id_tbl                     SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
1939     TYPE src_elem_targ_elem_map_tbl_typ IS TABLE OF NUMBER  INDEX BY VARCHAR2(15);
1940     l_src_elem_targ_elem_map_tbl               src_elem_targ_elem_map_tbl_typ;
1941 
1942 BEGIN
1943 
1944     x_msg_count := 0;
1945     x_return_status := FND_API.G_RET_STS_SUCCESS;
1946 
1947     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
1948     l_debug_mode := NVL(l_debug_mode, 'Y');
1949 
1950     -- Set curr function
1951 IF l_debug_mode = 'Y' THEN
1952     pa_debug.set_curr_function(
1953                 p_function   =>'pa_fp_copy_from_pkg.create_res_task_maps'
1954                ,p_debug_mode => l_debug_mode );
1955 
1956     -- Check for business rules violations
1957         pa_debug.g_err_stage:='Validating input parameters';
1958         pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level3);
1959     END IF;
1960 
1961     IF p_context IS NULL THEN
1962 
1963         IF l_debug_mode = 'Y' THEN
1964             pa_debug.g_err_stage:='p_context is '||p_context;
1965             pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
1966         END IF;
1967 
1968         PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
1969                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
1970 
1971         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
1972 
1973     END IF;
1974 
1975     --If the no of elements in source and target element version id tbls are not same, throw error
1976     IF p_src_elem_ver_id_tbl.count <> p_targ_elem_ver_id_tbl.count THEN
1977 
1978         IF l_debug_mode = 'Y' THEN
1979             pa_debug.g_err_stage:='The count in source elem ver id tbl and targ elem ver id tbl is not equal';
1980             pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level3);
1981         END IF;
1982         PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
1983                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
1984 
1985         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
1986     END IF;
1987 
1988     --If the no of elements in source ra id and target project assignment id tbls are not same, throw error
1989     IF p_targ_proj_assmt_id_tbl.count <> p_src_ra_id_tbl.count  OR
1990        p_targ_rlm_id_tbl.count <> p_src_ra_id_tbl.count OR -- Bug  3615617
1991        p_planning_start_date_tbl.count <> p_src_ra_id_tbl.count OR
1992        p_planning_end_date_tbl.count <> p_src_ra_id_tbl.count  OR
1993        (p_schedule_start_date_tbl.count <> 0 AND
1994          p_schedule_start_date_tbl.count <> p_src_ra_id_tbl.count) OR
1995        p_schedule_start_date_tbl.count <> p_schedule_end_date_tbl.count THEN
1996 
1997         IF l_debug_mode = 'Y' THEN
1998             pa_debug.g_err_stage:='The count in source ra id tbl and targ proj assmt is not equal';
1999             pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level3);
2000         END IF;
2001         PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
2002                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
2003 
2004         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2005     END IF;
2006 
2007     IF p_src_ra_id_tbl.count=0 THEN
2008 
2009         IF l_debug_mode = 'Y' THEN
2010             pa_debug.g_err_stage:='The source resource assignment id table is emtpy. Returning';
2011             pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level3);
2012             pa_debug.reset_curr_function;
2013         END IF;
2014         RETURN;
2015     END IF;
2016 
2017     IF p_schedule_start_date_tbl.count = 0 THEN
2018          l_schedule_start_date_tbl := p_planning_start_date_tbl;
2019          l_schedule_end_date_tbl := p_planning_end_date_tbl;
2020     ELSE
2021          l_schedule_start_date_tbl := p_schedule_start_date_tbl;
2022          l_schedule_end_date_tbl := p_schedule_end_date_tbl;
2023     END IF;
2024 
2025     IF l_debug_mode = 'Y' THEN
2026         pa_debug.g_err_stage:='About to create the mapping between source and target element version ids';
2027         pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level3);
2028     END IF;
2029 
2030     DELETE FROM pa_fp_ra_map_tmp;
2031 
2032 
2033     --When p_context is BUDGET then the copy will not happen accorss projects. The task id in the source and
2034     --target would be same for the corresponding resource assignments. Hence the FOR loop which creates the tbl
2035     --for mapping the task ids need not be executed.
2036 
2037     --Create the mapping between the source element version id and target element version id
2038     --,target proj element id and target project assignment id
2039 
2040     --Bug 4201936. Prepare tbls for source/target element version id both having same no. of elements as in
2041     --p_src_ra_id_tbl so that they can be used below for BULK insert
2042     IF NVL(p_context,'-99')<>'BUDGET' THEN
2043 
2044         l_targ_elem_ver_id_tbl.extend(p_src_ra_id_tbl.count);
2045         FOR i IN p_src_ra_id_tbl.first..p_src_ra_id_tbl.last LOOP
2046 
2047             SELECT wbs_element_version_id
2048             INTO   l_src_elem_ver_id
2049             FROM   pa_resource_assignments
2050             WHERE  resource_assignment_id=p_src_ra_id_tbl(i);
2051 
2052             l_temp := TO_CHAR(l_src_elem_ver_id);
2053             IF l_src_elem_targ_elem_map_tbl.EXISTS(l_temp) THEN
2054 
2055                 l_targ_elem_ver_id_tbl(i):=l_src_elem_targ_elem_map_tbl(l_temp);
2056 
2057             ELSE
2058 
2059                 FOR j IN p_src_elem_ver_id_tbl.first..p_src_elem_ver_id_tbl.last LOOP
2060 
2061                     IF l_src_elem_ver_id=p_src_elem_ver_id_tbl(j) THEN
2062 
2063                         l_targ_elem_ver_id_tbl(i):=p_targ_elem_ver_id_tbl(j);
2064                         l_src_elem_targ_elem_map_tbl(l_temp):=p_targ_elem_ver_id_tbl(j);
2065                         EXIT;
2066 
2067                     END IF;
2068 
2069                 END LOOP;--FOR j IN p_src_elem_ver_id_tbl.first..p_src_elem_ver_id_tbl.last
2070 
2071             END IF;
2072 
2073         END LOOP;--FOR i IN p_src_ra_id_tbl.first..p_src_ra_id_tbl.last LOOP
2074 
2075     END IF;--IF NVL(p_context,'-99')<>'BUDGET' THEN
2076 
2077     IF l_debug_mode = 'Y' THEN
2078         pa_debug.g_err_stage:='About to bulk insert into pa_fp_ra_map_tmp';
2079         pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level3);
2080     END IF;
2081 
2082     --Bulk insert into the pa_fp_ra_map_tmp table
2083 
2084     -- Bug 4187294: performance fix-splitting the below insert into 2 statments
2085     -- depending upon the context to avoid inner decode statements
2086 
2087     IF p_context = 'BUDGET' THEN
2088         FORALL i in p_src_ra_id_tbl.first..p_src_ra_id_tbl.last
2089         INSERT INTO pa_fp_ra_map_tmp
2090              ( source_res_assignment_id
2091               ,target_res_assignment_id
2092               ,source_task_id
2093               ,target_task_id
2094               ,system_reference1
2095               ,system_reference2
2096               ,system_reference3
2097               ,planning_start_date
2098               ,planning_end_date
2099               ,schedule_start_date
2100               ,schedule_end_date
2101               ,system_reference4      -- Bug  3615617
2102              )
2103         SELECT pra.resource_assignment_id
2104               ,pa_resource_assignments_s.nextval
2105               ,pra.task_id
2106               ,pra.task_id
2107               ,NULL
2108               ,NULL
2109               ,p_targ_proj_assmt_id_tbl(i)
2110               ,p_planning_start_date_tbl(i)
2111               ,p_planning_end_date_tbl(i)
2112               ,l_schedule_start_date_tbl(i)
2113               ,l_schedule_end_date_tbl(i)
2114               ,p_targ_rlm_id_tbl(i)    -- Bug  3615617
2115         FROM   pa_resource_assignments pra
2116         WHERE  pra.resource_assignment_id = p_src_ra_id_tbl(i);
2117     ELSE
2118 
2119         --Bug 4187294: Removed the calls to get_mapped_id.Used the pl/sql tbl l_targ_elem_ver_id_tbl
2120         --derived above.
2121         FORALL i in p_src_ra_id_tbl.first..p_src_ra_id_tbl.last
2122         INSERT INTO pa_fp_ra_map_tmp
2123              ( source_res_assignment_id
2124               ,target_res_assignment_id
2125               ,source_task_id
2126               ,target_task_id
2127               ,system_reference1
2128               ,system_reference2
2129               ,system_reference3
2130               ,planning_start_date
2131               ,planning_end_date
2132               ,schedule_start_date
2133               ,schedule_end_date
2134               ,system_reference4      -- Bug  3615617
2135              )
2136         SELECT pra.resource_assignment_id
2137               ,pa_resource_assignments_s.nextval
2138               ,pra.task_id
2139               ,pelm.proj_element_id
2140               ,pra.wbs_element_version_id
2141               ,l_targ_elem_ver_id_tbl(i)
2142               ,p_targ_proj_assmt_id_tbl(i)
2143               ,p_planning_start_date_tbl(i)
2144               ,p_planning_end_date_tbl(i)
2145               ,l_schedule_start_date_tbl(i)
2146               ,l_schedule_end_date_tbl(i)
2147               ,p_targ_rlm_id_tbl(i)    -- Bug  3615617
2148         FROM   pa_resource_assignments pra
2149               ,pa_proj_element_versions pelm
2150         WHERE  pra.resource_assignment_id = p_src_ra_id_tbl(i)
2151         AND    pelm.element_version_id=l_targ_elem_ver_id_tbl(i);
2152     END IF; -- p_context
2153 
2154     IF l_debug_mode = 'Y' THEN
2155         pa_debug.g_err_stage:='Exiting create_res_task_maps';
2156         pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level3);
2157         pa_debug.reset_curr_function;
2158     END IF;
2159 EXCEPTION
2160 
2161     WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
2162         l_msg_count := FND_MSG_PUB.count_msg;
2163         IF l_msg_count = 1 THEN
2164            PA_INTERFACE_UTILS_PUB.get_messages
2165                  (p_encoded        => FND_API.G_TRUE
2166                   ,p_msg_index      => 1
2167                   ,p_msg_count      => l_msg_count
2168                   ,p_msg_data       => l_msg_data
2169                   ,p_data           => l_data
2170                   ,p_msg_index_out  => l_msg_index_out);
2171 
2172            x_msg_data := l_data;
2173            x_msg_count := l_msg_count;
2174         ELSE
2175            x_msg_count := l_msg_count;
2176         END IF;
2177 
2178         x_return_status := FND_API.G_RET_STS_ERROR;
2179 
2180         IF l_debug_mode = 'Y' THEN
2181            pa_debug.g_err_stage:='Invalid Arguments Passed Or called api raised an error';
2182            pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
2183         -- reset curr function
2184            pa_debug.reset_curr_function();
2185         END IF;
2186         RETURN;
2187 
2188     WHEN OTHERS THEN
2189         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2190         x_msg_count     := 1;
2191         x_msg_data      := SQLERRM;
2192 
2193         FND_MSG_PUB.add_exc_msg( p_pkg_name       => 'pa_fp_copy_from_pkg'
2194                                 ,p_procedure_name => 'create_res_task_maps');
2195 
2196         IF l_debug_mode = 'Y' THEN
2197            pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
2198            pa_debug.write( l_module_name,pa_debug.g_err_stage,5);
2199         -- reset curr function
2200            pa_debug.Reset_Curr_Function();
2201         END IF;
2202         RAISE;
2203 
2204 END create_res_task_maps;
2205 
2206 
2207 /*=========================================================================
2208     This api will insert resource_assignments for the target_version based
2209     upon the PA_FP_RA_MAP_TABLE which contains both source_res_assignment_id,
2210     its parent and the corresponding target_res_assignment_id.This api will
2211     populate appropriate amounts based upon the target version plan
2212     preference code.If adjustment percentage is not zero then all amounts
2213     will be copied as null and will be populated by the rollup api
2214 
2215 
2216     --r11.5 FP.M Developement ----------------------------------
2217     --
2218     --08-JAN-04 jwhite          - Bug 3362316
2219     --                            Rewrote Copy_Resource_Assignments
2220     --                            for new FP.M columns.
2221     --
2222 
2223      3/28/2004  Raja FP M Phase II Dev Effort Copy Project Impact
2224      If resource list is a project specific resource list, target resource
2225      list member id should be derived using resource alias and target project id
2226 
2227      5/13/2004 Raja FP M IB2 changes Bug 3615617
2228      Logic to derive target resource list memer id has been moved to
2229      create_res_task_maps api. Target resource list member id is part
2230      of pa_fp_ra_map_tmp table. System_reference4 column has the value.
2231 
2232      --Added parameter p_rbs_map_diff_flag for Bug 3974569. This parameter can be passed as Y if the RBS mapping of
2233      --the target resource assignments is different from that of the source resource assignments.If this is passed as Y then
2234      ---->1.copy resource assignments will look at pa_rbs_plans_out_tmp table for rbs_element_id and txn_accum_header_id
2235      ---->of target resource assignments and it assumes that source_id in pa_rbs_plans_out_tmp corresponds to the
2236      ----> resource_assignment_id in the source budget version.
2237 
2238      --Bug 3948128. Included scheduled_delay in the list of columns that would get copied.
2239      --Bug 4200168: Changed the logic so that the copy is not based on pa_fp_ra_map_tmp when
2240      --the API is called for copying resource assignments between budget versions
2241  ===========================================================================*/
2242 
2243   PROCEDURE Copy_Resource_Assignments(
2244            p_source_plan_version_id    IN     NUMBER
2245           ,p_target_plan_version_id   IN     NUMBER
2246           ,p_adj_percentage           IN     NUMBER
2247           ,p_rbs_map_diff_flag        IN     VARCHAR2 DEFAULT 'N'
2248           ,p_calling_context          IN     VARCHAR2 DEFAULT NULL -- Bug 4065314
2249           ,x_return_status            OUT    NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
2250           ,x_msg_count                OUT    NOCOPY NUMBER --File.Sql.39 bug 4440895
2251           ,x_msg_data                 OUT    NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
2252   AS
2253        l_msg_count           NUMBER :=0;
2254        l_data                VARCHAR2(2000);
2255        l_msg_data            VARCHAR2(2000);
2256        l_error_msg_code      VARCHAR2(2000);
2257        l_msg_index_out       NUMBER;
2258        l_return_status       VARCHAR2(2000);
2259        l_debug_mode          VARCHAR2(30);
2260 
2261        l_adj_percentage      NUMBER ;
2262        l_target_project_id   pa_projects.project_id%TYPE;
2263        l_cost_flag           pa_fin_plan_amount_sets.raw_cost_flag%TYPE;
2264        l_revenue_flag        pa_fin_plan_amount_sets.revenue_flag%TYPE;
2265 
2266        l_tmp                 NUMBER;
2267        l_source_project_id   pa_projects_all.project_id%TYPE;
2268        l_fin_plan_level_code pa_proj_fp_options.cost_fin_plan_level_code%TYPE;
2269        l_control_flag        pa_resource_lists_all_bg.control_flag%TYPE;
2270        l_resource_list_id    pa_proj_fp_options.cost_resource_list_id%TYPE;
2271 
2272        l_project_org         pa_projects_all.carrying_out_organization_id%TYPE; -- bug 6161031
2273 
2274 
2275   BEGIN
2276      x_msg_count := 0;
2277      x_return_status := FND_API.G_RET_STS_SUCCESS;
2278 IF P_PA_DEBUG_MODE = 'Y' THEN
2279      pa_debug.set_err_stack('PA_FP_COPY_FROM_PKG.Copy_Resource_Assignments');
2280 END IF;
2281      fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
2282      l_debug_mode := NVL(l_debug_mode, 'Y');
2283 IF P_PA_DEBUG_MODE = 'Y' THEN
2284      pa_debug.set_process('PLSQL','LOG',l_debug_mode);
2285 END IF;
2286 
2287 
2288      /*
2289       * Check if  source_verion_id, target_version_id are NULL, if so throw
2290       * an error message
2291       */
2292 
2293     IF P_PA_DEBUG_MODE = 'Y' THEN
2294         pa_debug.g_err_stage := 'Checking for valid parameters:';
2295         pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
2296     END IF;
2297 
2298     IF (p_source_plan_version_id IS NULL) OR
2299        (p_target_plan_version_id IS NULL)
2300     THEN
2301 
2302          IF P_PA_DEBUG_MODE = 'Y' THEN
2303              pa_debug.g_err_stage := 'Source_plan='||p_source_plan_version_id;
2304              pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,5);
2305 
2306              pa_debug.g_err_stage := 'Target_plan'||p_target_plan_version_id;
2307              pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,5);
2308          END IF;
2309 
2310          PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
2311                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
2312          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2313     END IF;
2314 
2315     IF P_PA_DEBUG_MODE = 'Y' THEN
2316         pa_debug.g_err_stage := 'Parameter validation complete';
2317         pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
2318     END IF;
2319 
2320     --If adj_percentage is null make it zero
2321 
2322     l_adj_percentage := NVL(p_adj_percentage,0);
2323 
2324      --Fetching the flags of target version using fin_plan_prefernce_code
2325 
2326      IF P_PA_DEBUG_MODE = 'Y' THEN
2327          pa_debug.g_err_stage:='Fetching the raw_cost,burdened_cost and revenue flags of target_version';
2328          pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
2329      END IF;
2330 
2331      SELECT DECODE(pfot.fin_plan_preference_code
2332                    ,PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY ,'Y'
2333                    ,PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME , 'Y','N') --cost_flag
2334             ,DECODE(pfot.fin_plan_preference_code
2335                      ,PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY ,'Y'
2336                      ,PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME ,'Y','N')--revenue_flag
2337             ,pfot.project_id
2338             ,pfos.project_id
2339             ,nvl(pfos.cost_fin_plan_level_code, nvl(pfos.revenue_fin_plan_level_code,pfos.all_fin_plan_level_code))
2340             ,nvl(pfos.cost_resource_list_id, nvl(pfos.revenue_resource_list_id,pfos.all_resource_list_id))
2341             ,nvl(rl.control_flag,'N')
2342      INTO   l_cost_flag
2343            ,l_revenue_flag
2344            ,l_target_project_id
2345            ,l_source_project_id
2346            ,l_fin_plan_level_code
2347            ,l_resource_list_id
2348            ,l_control_flag
2349      FROM   pa_proj_fp_options pfot,--target
2350             pa_proj_fp_options pfos,--source
2351             pa_resource_lists_all_bg rl
2352      WHERE  pfot.fin_plan_version_id=p_target_plan_version_id
2353      AND    pfos.fin_plan_version_id=p_source_plan_version_id
2354      AND    rl.resource_list_id=nvl(pfot.cost_resource_list_id, nvl(pfot.revenue_resource_list_id,pfot.all_resource_list_id));
2355 
2356 /*
2357     IF P_PA_DEBUG_MODE = 'Y' THEN
2358         pa_debug.g_err_stage:='l_cost_flag ='||l_cost_flag;
2359         pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
2360         pa_debug.g_err_stage:='l_revenue_flag ='||l_revenue_flag;
2361         pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
2362         pa_debug.g_err_stage:='l_target_project_id ='||l_target_project_id;
2363         pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
2364     END IF;
2365 */
2366     --Inserting records into pa_resource_assignments using pa_fp_ra_map_tmp
2367 
2368      IF P_PA_DEBUG_MODE = 'Y' THEN
2369          pa_debug.g_err_stage:='Copying the source version records as target version records';
2370          pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
2371      END IF;
2372      -- Bug 3362316, 08-JAN-2003: Added New FP.M Columns  --------------------------
2373 
2374      --Bug 3974569. Need not have pa_rbs_plans_out_tmp in the FROM clause if the parameter is N
2375      IF P_PA_DEBUG_MODE = 'Y' THEN
2376          pa_debug.g_err_stage:='p_rbs_map_diff_flag '||p_rbs_map_diff_flag;
2377          pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
2378      END IF;
2379 
2380      IF (p_calling_context='WORKPLAN' AND nvl(p_rbs_map_diff_flag,'N') ='N') OR
2381          p_calling_context='CI' THEN
2382 
2383          IF P_PA_DEBUG_MODE = 'Y' THEN
2384              pa_debug.g_err_stage:='Using the First RA Insert';
2385              pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
2386          END IF;
2387 
2388          INSERT INTO PA_RESOURCE_ASSIGNMENTS(
2389                   resource_assignment_id
2390                   ,budget_version_id
2391                   ,project_id
2392                   ,task_id
2393                   ,resource_list_member_id
2394                   ,last_update_date
2395                   ,last_updated_by
2396                   ,creation_date
2397                   ,created_by
2398                   ,last_update_login
2399                   ,unit_of_measure
2400                   ,track_as_labor_flag
2401                   ,total_plan_revenue
2402                   ,total_plan_raw_cost
2403                   ,total_plan_burdened_cost
2404                   ,total_plan_quantity
2405                   ,resource_assignment_type
2406                   ,total_project_raw_cost
2407                   ,total_project_burdened_cost
2408                   ,total_project_revenue
2409                   ,standard_bill_rate
2410                   ,average_bill_rate
2411                   ,average_cost_rate
2412                   ,project_assignment_id
2413                   ,plan_error_code
2414                   ,average_discount_percentage
2415                   ,total_borrowed_revenue
2416                   ,total_revenue_adj
2417                   ,total_lent_resource_cost
2418                   ,total_cost_adj
2419                   ,total_unassigned_time_cost
2420                   ,total_utilization_percent
2421                   ,total_utilization_hours
2422                   ,total_utilization_adj
2423                   ,total_capacity
2424                   ,total_head_count
2425                   ,total_head_count_adj
2426                   ,total_tp_revenue_in
2427                   ,total_tp_revenue_out
2428                   ,total_tp_cost_in
2429                   ,total_tp_cost_out
2430                   ,parent_assignment_id
2431                   ,wbs_element_version_id
2432                   ,rbs_element_id
2433                   ,planning_start_date
2434                   ,planning_end_date
2435                   ,schedule_start_date
2436                   ,schedule_end_date
2437                   ,spread_curve_id
2438                   ,etc_method_code
2439                   ,res_type_code
2440                   ,attribute_category
2441                   ,attribute1
2442                   ,attribute2
2443                   ,attribute3
2444                   ,attribute4
2445                   ,attribute5
2446                   ,attribute6
2447                   ,attribute7
2448                   ,attribute8
2449                   ,attribute9
2450                   ,attribute10
2451                   ,attribute11
2452                   ,attribute12
2453                   ,attribute13
2454                   ,attribute14
2455                   ,attribute15
2456                   ,attribute16
2457                   ,attribute17
2458                   ,attribute18
2459                   ,attribute19
2460                   ,attribute20
2461                   ,attribute21
2462                   ,attribute22
2463                   ,attribute23
2464                   ,attribute24
2465                   ,attribute25
2466                   ,attribute26
2467                   ,attribute27
2468                   ,attribute28
2469                   ,attribute29
2470                   ,attribute30
2471                   ,fc_res_type_code
2472                   ,resource_class_code
2473                   ,organization_id
2474                   ,job_id
2475                   ,person_id
2476                   ,expenditure_type
2477                   ,expenditure_category
2478                   ,revenue_category_code
2479                   ,event_type
2480                   ,supplier_id
2481                   ,non_labor_resource
2482                   ,bom_resource_id
2483                   ,inventory_item_id
2484                   ,item_category_id
2485                   ,record_version_number
2486                   ,transaction_source_code
2487                   ,mfc_cost_type_id
2488                   ,procure_resource_flag
2489                   ,assignment_description
2490                   ,incurred_by_res_flag
2491                   ,rate_job_id
2492                   ,rate_expenditure_type
2493                   ,ta_display_flag
2494                   ,sp_fixed_date
2495                   ,person_type_code
2496                   ,rate_based_flag
2497                   ,resource_rate_based_flag  --IPM Arch Enhancements Bug 4865563
2498                   ,use_task_schedule_flag
2499                   ,rate_exp_func_curr_code
2500                   ,rate_expenditure_org_id
2501                   ,incur_by_res_class_code
2502                   ,incur_by_role_id
2503                   ,project_role_id
2504                   ,resource_class_flag
2505                   ,named_role
2506                   ,txn_accum_header_id
2507                   ,scheduled_delay --For Bug 3948128
2508 				  ,CBS_ELEMENT_ID -- bug#16200605
2509                   )
2510          SELECT   /*+ ORDERED USE_NL(PFRMT,PRA) INDEX(PRA PA_RESOURCE_ASSIGNMENTS_U1)*/ pfrmt.target_res_assignment_id  --Bug 2814165
2511                   ,p_target_plan_version_id
2512                   ,l_target_project_id
2513                   ,pfrmt.target_task_id
2514                   ,pfrmt.system_reference4 -- Bug 3615617 resource_list_member_id
2515                   ,sysdate
2516                   ,fnd_global.user_id
2517                   ,sysdate
2518                   ,fnd_global.user_id
2519                   ,fnd_global.login_id
2520                   ,pra.unit_of_measure
2521                   ,pra.track_as_labor_flag
2522                   ,DECODE(l_adj_percentage,0,DECODE(l_revenue_flag,'Y',total_plan_revenue,NULL),NULL)
2523                   ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_plan_raw_cost,NULL),NULL)
2524                   ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_plan_burdened_cost,NULL),NULL)
2525                   ,DECODE(l_adj_percentage,0,total_plan_quantity,NULL)
2526                   ,pra.resource_assignment_type
2527                   ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_project_raw_cost,NULL),NULL)
2528                   ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_project_burdened_cost,NULL),NULL)
2529                   ,DECODE(l_adj_percentage,0,DECODE(l_revenue_flag,'Y',total_project_revenue,NULL),NULL)
2530                   ,standard_bill_rate
2531                   ,average_bill_rate
2532                   ,average_cost_rate
2533                   ,pfrmt.system_reference3 -- Project assignment id of the target (Bug 3354518)
2534                   ,plan_error_code
2535                   ,average_discount_percentage
2536                   ,total_borrowed_revenue
2537                   ,total_revenue_adj
2538                   ,total_lent_resource_cost
2539                   ,total_cost_adj
2540                   ,total_unassigned_time_cost
2541                   ,total_utilization_percent
2542                   ,total_utilization_hours
2543                   ,total_utilization_adj
2544                   ,total_capacity
2545                   ,total_head_count
2546                   ,total_head_count_adj
2547                   ,total_tp_revenue_in
2548                   ,total_tp_revenue_out
2549                   ,total_tp_cost_in
2550                   ,total_tp_cost_out
2551                   --parent assignment id in the target resource assignments contain source resource assignment id
2552                   --Bug 4200168
2553                   ,pra.resource_assignment_id
2554                   ,pfrmt.system_reference2 -- element version id of the target. (Bug 3354518)
2555                   ,pra.rbs_element_id
2556                   ,pfrmt.planning_start_date -- Planning start date of the target (Bug 3354518)
2557                   ,pfrmt.planning_end_date   -- Planning end date of the target  (Bug 3354518)
2558                   ,pfrmt.schedule_start_date
2559                   ,pfrmt.schedule_end_date
2560                   ,pra.spread_curve_id
2561                   ,pra.etc_method_code
2562                   ,pra.res_type_code
2563                   ,pra.attribute_category
2564                   ,pra.attribute1
2565                   ,pra.attribute2
2566                   ,pra.attribute3
2567                   ,pra.attribute4
2568                   ,pra.attribute5
2569                   ,pra.attribute6
2570                   ,pra.attribute7
2571                   ,pra.attribute8
2572                   ,pra.attribute9
2573                   ,pra.attribute10
2574                   ,pra.attribute11
2575                   ,pra.attribute12
2576                   ,pra.attribute13
2577                   ,pra.attribute14
2578                   ,pra.attribute15
2579                   ,pra.attribute16
2580                   ,pra.attribute17
2581                   ,pra.attribute18
2582                   ,pra.attribute19
2583                   ,pra.attribute20
2584                   ,pra.attribute21
2585                   ,pra.attribute22
2586                   ,pra.attribute23
2587                   ,pra.attribute24
2588                   ,pra.attribute25
2589                   ,pra.attribute26
2590                   ,pra.attribute27
2591                   ,pra.attribute28
2592                   ,pra.attribute29
2593                   ,pra.attribute30
2594                   ,pra.fc_res_type_code
2595                   ,pra.resource_class_code
2596                   ,pra.organization_id
2597                   ,pra.job_id
2598                   ,pra.person_id
2599                   ,pra.expenditure_type
2600                   ,pra.expenditure_category
2601                   ,pra.revenue_category_code
2602                   ,pra.event_type
2603                   ,pra.supplier_id
2604                   ,pra.non_labor_resource
2605                   ,pra.bom_resource_id
2606                   ,pra.inventory_item_id
2607                   ,pra.item_category_id
2608                   ,1    -- should be 1 in the target version being created
2609                   ,decode(p_calling_context, 'CREATE_VERSION', NULL, pra.transaction_source_code)
2610                   ,pra.mfc_cost_type_id
2611                   ,pra.procure_resource_flag
2612                   ,pra.assignment_description
2613                   ,pra.incurred_by_res_flag
2614                   ,pra.rate_job_id
2615                   ,pra.rate_expenditure_type
2616                   ,pra.ta_display_flag
2617                   -- Bug 3820625 sp_fixed_date should also move as per planning_start_date
2618                   -- Least and greatest are used to make sure that sp_fixed_date is with in planning start and end dates
2619                   ,greatest(least(pra.sp_fixed_date + (pfrmt.planning_start_date - pra.planning_start_date),
2620                                   pfrmt.planning_end_date),
2621                             pfrmt.planning_start_date)
2622                   ,pra.person_type_code
2623                   ,pra.rate_based_flag
2624                   ,pra.resource_rate_based_flag   --IPM Arch Enhacement Bug 4865563
2625                   ,pra.use_task_schedule_flag
2626                   ,pra.rate_exp_func_curr_code
2627                   ,pra.rate_expenditure_org_id
2628                   ,pra.incur_by_res_class_code
2629                   ,pra.incur_by_role_id
2630                   ,pra.project_role_id
2631                   ,pra.resource_class_flag
2632                   ,pra.named_role
2633                   ,pra.txn_accum_header_id
2634                   ,scheduled_delay --For Bug 3948128
2635 				  ,pra.CBS_ELEMENT_ID  -- bug#16200605
2636          FROM     PA_FP_RA_MAP_TMP pfrmt          --Bug 2814165
2637                  ,PA_RESOURCE_ASSIGNMENTS pra
2638          WHERE    pra.resource_assignment_id = pfrmt.source_res_assignment_id
2639          AND      pra.budget_version_id      = p_source_plan_version_id ;
2640 
2641 
2642      --For Bug 3974569. Take rbs_element_id and txn_accum_header_id from pa_rbs_plans_out_tmp
2643      --API is called for  in WORKPLAN CONTEXT for copying from source to target with different RBSs or Resource Lists
2644      ELSIF p_calling_context='WORKPLAN' AND p_rbs_map_diff_flag ='Y' THEN --IF p_rbs_map_diff_flag ='N' THEN
2645 
2646          IF P_PA_DEBUG_MODE = 'Y' THEN
2647 
2648              pa_debug.g_err_stage:='Using the Second RA Insert';
2649              pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
2650 
2651 
2652              SELECT COUNT(*)
2653              INTO   l_tmp
2654              FROM   PA_FP_RA_MAP_TMP;
2655 
2656              pa_debug.g_err_stage:='PA_FP_RA_MAP_TMP count '||l_tmp;
2657              pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
2658 
2659              SELECT COUNT(*)
2660              INTO   l_tmp
2661              FROM   pa_rbs_plans_out_tmp;
2662 
2663              pa_debug.g_err_stage:='pa_rbs_plans_out_tmp count '||l_tmp;
2664              pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
2665 
2666          END IF;
2667 
2668          INSERT INTO PA_RESOURCE_ASSIGNMENTS(
2669                   resource_assignment_id
2670                   ,budget_version_id
2671                   ,project_id
2672                   ,task_id
2673                   ,resource_list_member_id
2674                   ,last_update_date
2675                   ,last_updated_by
2676                   ,creation_date
2677                   ,created_by
2678                   ,last_update_login
2679                   ,unit_of_measure
2680                   ,track_as_labor_flag
2681                   ,total_plan_revenue
2682                   ,total_plan_raw_cost
2683                   ,total_plan_burdened_cost
2684                   ,total_plan_quantity
2685                   ,resource_assignment_type
2686                   ,total_project_raw_cost
2687                   ,total_project_burdened_cost
2688                   ,total_project_revenue
2689                   ,standard_bill_rate
2690                   ,average_bill_rate
2691                   ,average_cost_rate
2692                   ,project_assignment_id
2693                   ,plan_error_code
2694                   ,average_discount_percentage
2695                   ,total_borrowed_revenue
2696                   ,total_revenue_adj
2697                   ,total_lent_resource_cost
2698                   ,total_cost_adj
2699                   ,total_unassigned_time_cost
2700                   ,total_utilization_percent
2701                   ,total_utilization_hours
2702                   ,total_utilization_adj
2703                   ,total_capacity
2704                   ,total_head_count
2705                   ,total_head_count_adj
2706                   ,total_tp_revenue_in
2707                   ,total_tp_revenue_out
2708                   ,total_tp_cost_in
2709                   ,total_tp_cost_out
2710                   ,parent_assignment_id
2711                   ,wbs_element_version_id
2712                   ,rbs_element_id
2713                   ,planning_start_date
2714                   ,planning_end_date
2715                   ,schedule_start_date
2716                   ,schedule_end_date
2717                   ,spread_curve_id
2718                   ,etc_method_code
2719                   ,res_type_code
2720                   ,attribute_category
2721                   ,attribute1
2722                   ,attribute2
2723                   ,attribute3
2724                   ,attribute4
2725                   ,attribute5
2726                   ,attribute6
2727                   ,attribute7
2728                   ,attribute8
2729                   ,attribute9
2730                   ,attribute10
2731                   ,attribute11
2732                   ,attribute12
2733                   ,attribute13
2734                   ,attribute14
2735                   ,attribute15
2736                   ,attribute16
2737                   ,attribute17
2738                   ,attribute18
2739                   ,attribute19
2740                   ,attribute20
2741                   ,attribute21
2742                   ,attribute22
2743                   ,attribute23
2744                   ,attribute24
2745                   ,attribute25
2746                   ,attribute26
2747                   ,attribute27
2748                   ,attribute28
2749                   ,attribute29
2750                   ,attribute30
2751                   ,fc_res_type_code
2752                   ,resource_class_code
2753                   ,organization_id
2754                   ,job_id
2755                   ,person_id
2756                   ,expenditure_type
2757                   ,expenditure_category
2758                   ,revenue_category_code
2759                   ,event_type
2760                   ,supplier_id
2761                   ,non_labor_resource
2762                   ,bom_resource_id
2763                   ,inventory_item_id
2764                   ,item_category_id
2765                   ,record_version_number
2766                   ,transaction_source_code
2767                   ,mfc_cost_type_id
2768                   ,procure_resource_flag
2769                   ,assignment_description
2770                   ,incurred_by_res_flag
2771                   ,rate_job_id
2772                   ,rate_expenditure_type
2773                   ,ta_display_flag
2774                   ,sp_fixed_date
2775                   ,person_type_code
2776                   ,rate_based_flag
2777                   ,resource_rate_based_flag -- IPM Arch Enhacements Bug 4865563
2778                   ,use_task_schedule_flag
2779                   ,rate_exp_func_curr_code
2780                   ,rate_expenditure_org_id
2781                   ,incur_by_res_class_code
2782                   ,incur_by_role_id
2783                   ,project_role_id
2784                   ,resource_class_flag
2785                   ,named_role
2786                   ,txn_accum_header_id
2787                   ,scheduled_delay --For Bug 3948128
2788 				  ,CBS_ELEMENT_ID -- bug#16200605
2789                   )
2790          SELECT   /*+ ORDERED USE_NL(PFRMT,PRA,RMAP) INDEX(PRA PA_RESOURCE_ASSIGNMENTS_U1)*/ pfrmt.target_res_assignment_id  --Bug 2814165
2791                   ,p_target_plan_version_id
2792                   ,l_target_project_id
2793                   ,pfrmt.target_task_id
2794                   ,pfrmt.system_reference4 -- Bug 3615617 resource_list_member_id
2795                   ,sysdate
2796                   ,fnd_global.user_id
2797                   ,sysdate
2798                   ,fnd_global.user_id
2799                   ,fnd_global.login_id
2800                   ,pra.unit_of_measure
2801                   ,pra.track_as_labor_flag
2802                   ,DECODE(l_adj_percentage,0,DECODE(l_revenue_flag,'Y',total_plan_revenue,NULL),NULL)
2803                   ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_plan_raw_cost,NULL),NULL)
2804                   ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_plan_burdened_cost,NULL),NULL)
2805                   ,DECODE(l_adj_percentage,0,total_plan_quantity,NULL)
2806                   ,pra.resource_assignment_type
2807                   ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_project_raw_cost,NULL),NULL)
2808                   ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_project_burdened_cost,NULL),NULL)
2809                   ,DECODE(l_adj_percentage,0,DECODE(l_revenue_flag,'Y',total_project_revenue,NULL),NULL)
2810                   ,standard_bill_rate
2811                   ,average_bill_rate
2812                   ,average_cost_rate
2813                   ,pfrmt.system_reference3 -- Project assignment id of the target (Bug 3354518)
2814                   ,plan_error_code
2815                   ,average_discount_percentage
2816                   ,total_borrowed_revenue
2817                   ,total_revenue_adj
2818                   ,total_lent_resource_cost
2819                   ,total_cost_adj
2820                   ,total_unassigned_time_cost
2821                   ,total_utilization_percent
2822                   ,total_utilization_hours
2823                   ,total_utilization_adj
2824                   ,total_capacity
2825                   ,total_head_count
2826                   ,total_head_count_adj
2827                   ,total_tp_revenue_in
2828                   ,total_tp_revenue_out
2829                   ,total_tp_cost_in
2830                   ,total_tp_cost_out
2831                   --parent assignment id in the target resource assignments contain source resource assignment id
2832                   --Bug 4200168
2833                   ,pra.resource_assignment_id
2834                   ,pfrmt.system_reference2 -- element version id of the target. (Bug 3354518)
2835                   ,rmap.rbs_element_id
2836                   ,pfrmt.planning_start_date -- Planning start date of the target (Bug 3354518)
2837                   ,pfrmt.planning_end_date   -- Planning end date of the target  (Bug 3354518)
2838                   ,pfrmt.schedule_start_date
2839                   ,pfrmt.schedule_end_date
2840                   ,pra.spread_curve_id
2841                   ,pra.etc_method_code
2842                   ,pra.res_type_code
2843                   ,pra.attribute_category
2844                   ,pra.attribute1
2845                   ,pra.attribute2
2846                   ,pra.attribute3
2847                   ,pra.attribute4
2848                   ,pra.attribute5
2849                   ,pra.attribute6
2850                   ,pra.attribute7
2851                   ,pra.attribute8
2852                   ,pra.attribute9
2853                   ,pra.attribute10
2854                   ,pra.attribute11
2855                   ,pra.attribute12
2856                   ,pra.attribute13
2857                   ,pra.attribute14
2858                   ,pra.attribute15
2859                   ,pra.attribute16
2860                   ,pra.attribute17
2861                   ,pra.attribute18
2862                   ,pra.attribute19
2863                   ,pra.attribute20
2864                   ,pra.attribute21
2865                   ,pra.attribute22
2866                   ,pra.attribute23
2867                   ,pra.attribute24
2868                   ,pra.attribute25
2869                   ,pra.attribute26
2870                   ,pra.attribute27
2871                   ,pra.attribute28
2872                   ,pra.attribute29
2873                   ,pra.attribute30
2874                   ,pra.fc_res_type_code
2875                   ,pra.resource_class_code
2876                   ,pra.organization_id
2877                   ,pra.job_id
2878                   ,pra.person_id
2879                   ,pra.expenditure_type
2880                   ,pra.expenditure_category
2881                   ,pra.revenue_category_code
2882                   ,pra.event_type
2883                   ,pra.supplier_id
2884                   ,pra.non_labor_resource
2885                   ,pra.bom_resource_id
2886                   ,pra.inventory_item_id
2887                   ,pra.item_category_id
2888                   ,1    -- should be 1 in the target version being created
2889                   ,decode(p_calling_context, 'CREATE_VERSION', NULL, pra.transaction_source_code)
2890                   ,pra.mfc_cost_type_id
2891                   ,pra.procure_resource_flag
2892                   ,pra.assignment_description
2893                   ,pra.incurred_by_res_flag
2894                   ,pra.rate_job_id
2895                   ,pra.rate_expenditure_type
2896                   ,pra.ta_display_flag
2897                   -- Bug 3820625 sp_fixed_date should also move as per planning_start_date
2898                   -- Least and greatest are used to make sure that sp_fixed_date is with in planning start and end dates
2899                   ,greatest(least(pra.sp_fixed_date + (pfrmt.planning_start_date - pra.planning_start_date),
2900                                   pfrmt.planning_end_date),
2901                             pfrmt.planning_start_date)
2902                   ,pra.person_type_code
2903                   ,pra.rate_based_flag
2904                   ,pra.resource_rate_based_flag  --IPM Arch Enhancement Bug 4865563
2905                   ,pra.use_task_schedule_flag
2906                   ,pra.rate_exp_func_curr_code
2907                   ,pra.rate_expenditure_org_id
2908                   ,pra.incur_by_res_class_code
2909                   ,pra.incur_by_role_id
2910                   ,pra.project_role_id
2911                   ,pra.resource_class_flag
2912                   ,pra.named_role
2913                   ,rmap.txn_accum_header_id
2914                   ,scheduled_delay --For Bug 3948128,
2915 				  ,pra.CBS_ELEMENT_ID -- bug#16200605
2916          FROM     PA_FP_RA_MAP_TMP pfrmt          --Bug 2814165
2917                  ,PA_RESOURCE_ASSIGNMENTS pra
2918                  ,pa_rbs_plans_out_tmp rmap
2919          WHERE    pra.resource_assignment_id = pfrmt.source_res_assignment_id
2920          AND      pra.budget_version_id      = p_source_plan_version_id
2921          AND      rmap.source_id = pra.resource_assignment_id;
2922 
2923      --Copying BUDGET versions within a project. OR
2924      --Copying Budget versions across 2 projects with the planning level being project and
2925      ---->with the resource list being a centrally controlled one
2926      ELSIF ((p_calling_context IS NULL  OR p_calling_context='CREATE_VERSION') AND l_source_project_id=l_target_project_id) OR
2927         ((p_calling_context IS NULL  OR p_calling_context='CREATE_VERSION') AND l_source_project_id<>l_target_project_id
2928           AND l_fin_plan_level_code='P' AND l_control_flag='Y' ) THEN
2929 
2930              IF P_PA_DEBUG_MODE = 'Y' THEN
2931                  pa_debug.g_err_stage:='Using the Third RA Insert';
2932                  pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
2933              END IF;
2934 
2935              INSERT INTO PA_RESOURCE_ASSIGNMENTS(
2936                       resource_assignment_id
2937                       ,budget_version_id
2938                       ,project_id
2939                       ,task_id
2940                       ,resource_list_member_id
2941                       ,last_update_date
2942                       ,last_updated_by
2943                       ,creation_date
2944                       ,created_by
2945                       ,last_update_login
2946                       ,unit_of_measure
2947                       ,track_as_labor_flag
2948                       ,total_plan_revenue
2949                       ,total_plan_raw_cost
2950                       ,total_plan_burdened_cost
2951                       ,total_plan_quantity
2952                       ,resource_assignment_type
2953                       ,total_project_raw_cost
2954                       ,total_project_burdened_cost
2955                       ,total_project_revenue
2956                       ,standard_bill_rate
2957                       ,average_bill_rate
2958                       ,average_cost_rate
2959                       ,project_assignment_id
2960                       ,plan_error_code
2961                       ,average_discount_percentage
2962                       ,total_borrowed_revenue
2963                       ,total_revenue_adj
2964                       ,total_lent_resource_cost
2965                       ,total_cost_adj
2966                       ,total_unassigned_time_cost
2967                       ,total_utilization_percent
2968                       ,total_utilization_hours
2969                       ,total_utilization_adj
2970                       ,total_capacity
2971                       ,total_head_count
2972                       ,total_head_count_adj
2973                       ,total_tp_revenue_in
2974                       ,total_tp_revenue_out
2975                       ,total_tp_cost_in
2976                       ,total_tp_cost_out
2977                       ,parent_assignment_id
2978                       ,wbs_element_version_id
2979                       ,rbs_element_id
2980                       ,planning_start_date
2981                       ,planning_end_date
2982                       ,schedule_start_date
2983                       ,schedule_end_date
2984                       ,spread_curve_id
2985                       ,etc_method_code
2986                       ,res_type_code
2987                       ,attribute_category
2988                       ,attribute1
2989                       ,attribute2
2990                       ,attribute3
2991                       ,attribute4
2992                       ,attribute5
2993                       ,attribute6
2994                       ,attribute7
2995                       ,attribute8
2996                       ,attribute9
2997                       ,attribute10
2998                       ,attribute11
2999                       ,attribute12
3000                       ,attribute13
3001                       ,attribute14
3002                       ,attribute15
3003                       ,attribute16
3004                       ,attribute17
3005                       ,attribute18
3006                       ,attribute19
3007                       ,attribute20
3008                       ,attribute21
3009                       ,attribute22
3010                       ,attribute23
3011                       ,attribute24
3012                       ,attribute25
3013                       ,attribute26
3014                       ,attribute27
3015                       ,attribute28
3016                       ,attribute29
3017                       ,attribute30
3018                       ,fc_res_type_code
3019                       ,resource_class_code
3020                       ,organization_id
3021                       ,job_id
3022                       ,person_id
3023                       ,expenditure_type
3024                       ,expenditure_category
3025                       ,revenue_category_code
3026                       ,event_type
3027                       ,supplier_id
3028                       ,non_labor_resource
3029                       ,bom_resource_id
3030                       ,inventory_item_id
3031                       ,item_category_id
3032                       ,record_version_number
3033                       ,transaction_source_code
3034                       ,mfc_cost_type_id
3035                       ,procure_resource_flag
3036                       ,assignment_description
3037                       ,incurred_by_res_flag
3038                       ,rate_job_id
3039                       ,rate_expenditure_type
3040                       ,ta_display_flag
3041                       ,sp_fixed_date
3042                       ,person_type_code
3043                       ,rate_based_flag
3044                       ,resource_rate_based_flag --IPM Arch Enhacements Bug 4865563
3045                       ,use_task_schedule_flag
3046                       ,rate_exp_func_curr_code
3047                       ,rate_expenditure_org_id
3048                       ,incur_by_res_class_code
3049                       ,incur_by_role_id
3050                       ,project_role_id
3051                       ,resource_class_flag
3052                       ,named_role
3053                       ,txn_accum_header_id
3054                       ,scheduled_delay --For Bug 3948128
3055 					  ,CBS_ELEMENT_ID -- bug#16200605
3056                       )
3057              SELECT
3058                        pa_resource_assignments_s.nextval
3059                       ,p_target_plan_version_id
3060                       ,l_target_project_id
3061                       ,pra.task_id
3062                       ,pra.resource_list_member_id
3063                       ,sysdate
3064                       ,fnd_global.user_id
3065                       ,sysdate
3066                       ,fnd_global.user_id
3067                       ,fnd_global.login_id
3068                       ,pra.unit_of_measure
3069                       ,pra.track_as_labor_flag
3070                       ,DECODE(l_adj_percentage,0,DECODE(l_revenue_flag,'Y',total_plan_revenue,NULL),NULL)
3071                       ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_plan_raw_cost,NULL),NULL)
3072                       ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_plan_burdened_cost,NULL),NULL)
3073                       ,DECODE(l_adj_percentage,0,total_plan_quantity,NULL)
3074                       ,pra.resource_assignment_type
3075                       ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_project_raw_cost,NULL),NULL)
3076                       ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_project_burdened_cost,NULL),NULL)
3077                       ,DECODE(l_adj_percentage,0,DECODE(l_revenue_flag,'Y',total_project_revenue,NULL),NULL)
3078                       ,standard_bill_rate
3079                       ,average_bill_rate
3080                       ,average_cost_rate
3081                       ,pra.project_assignment_id
3082                       ,plan_error_code
3083                       ,average_discount_percentage
3084                       ,total_borrowed_revenue
3085                       ,total_revenue_adj
3086                       ,total_lent_resource_cost
3087                       ,total_cost_adj
3088                       ,total_unassigned_time_cost
3089                       ,total_utilization_percent
3090                       ,total_utilization_hours
3091                       ,total_utilization_adj
3092                       ,total_capacity
3093                       ,total_head_count
3094                       ,total_head_count_adj
3095                       ,total_tp_revenue_in
3096                       ,total_tp_revenue_out
3097                       ,total_tp_cost_in
3098                       ,total_tp_cost_out
3099                       --parent assignment id in the target resource assignments contain source resource assignment id
3100                       --Bug 4200168
3101                       ,pra.resource_assignment_id
3102                       ,pra.wbs_element_version_id
3103                       ,pra.rbs_element_id
3104                       ,pra.planning_start_date -- Planning start date of the target (Bug 3354518)
3105                       ,pra.planning_end_date   -- Planning end date of the target  (Bug 3354518)
3106                       ,pra.schedule_start_date
3107                       ,pra.schedule_end_date
3108                       ,pra.spread_curve_id
3109                       ,pra.etc_method_code
3110                       ,pra.res_type_code
3111                       ,pra.attribute_category
3112                       ,pra.attribute1
3113                       ,pra.attribute2
3114                       ,pra.attribute3
3115                       ,pra.attribute4
3116                       ,pra.attribute5
3117                       ,pra.attribute6
3118                       ,pra.attribute7
3119                       ,pra.attribute8
3120                       ,pra.attribute9
3121                       ,pra.attribute10
3122                       ,pra.attribute11
3123                       ,pra.attribute12
3124                       ,pra.attribute13
3125                       ,pra.attribute14
3126                       ,pra.attribute15
3127                       ,pra.attribute16
3128                       ,pra.attribute17
3129                       ,pra.attribute18
3130                       ,pra.attribute19
3131                       ,pra.attribute20
3132                       ,pra.attribute21
3133                       ,pra.attribute22
3134                       ,pra.attribute23
3135                       ,pra.attribute24
3136                       ,pra.attribute25
3137                       ,pra.attribute26
3138                       ,pra.attribute27
3139                       ,pra.attribute28
3140                       ,pra.attribute29
3141                       ,pra.attribute30
3142                       ,pra.fc_res_type_code
3143                       ,pra.resource_class_code
3144                       ,pra.organization_id
3145                       ,pra.job_id
3146                       ,pra.person_id
3147                       ,pra.expenditure_type
3148                       ,pra.expenditure_category
3149                       ,pra.revenue_category_code
3150                       ,pra.event_type
3151                       ,pra.supplier_id
3152                       ,pra.non_labor_resource
3153                       ,pra.bom_resource_id
3154                       ,pra.inventory_item_id
3155                       ,pra.item_category_id
3156                       ,1    -- should be 1 in the target version being created
3157                       ,decode(p_calling_context, 'CREATE_VERSION', NULL, pra.transaction_source_code)
3158                       ,pra.mfc_cost_type_id
3159                       ,pra.procure_resource_flag
3160                       ,pra.assignment_description
3161                       ,pra.incurred_by_res_flag
3162                       ,pra.rate_job_id
3163                       ,pra.rate_expenditure_type
3164                       ,pra.ta_display_flag
3165                       ,pra.sp_fixed_date
3166                       ,pra.person_type_code
3167                       ,pra.rate_based_flag
3168                       ,pra.resource_rate_based_flag  --IPM Arch Enhacement Bug 4865563
3169                       ,pra.use_task_schedule_flag
3170                       ,pra.rate_exp_func_curr_code
3171                       ,pra.rate_expenditure_org_id
3172                       ,pra.incur_by_res_class_code
3173                       ,pra.incur_by_role_id
3174                       ,pra.project_role_id
3175                       ,pra.resource_class_flag
3176                       ,pra.named_role
3177                       ,pra.txn_accum_header_id
3178                       ,scheduled_delay --For Bug 3948128
3179 					  ,pra.CBS_ELEMENT_ID -- bug#16200605
3180              FROM     PA_RESOURCE_ASSIGNMENTS pra
3181              WHERE    pra.budget_version_id = p_source_plan_version_id
3182              AND      pra.project_id = l_source_project_id ; -- Bug 4493425
3183 
3184      --API is called for copying resource assignments between plan versions that dont belong to the same project
3185      ELSIF (p_calling_context IS NULL OR p_calling_context='CREATE_VERSION') AND l_source_project_id<>l_target_project_id THEN
3186 
3187          IF l_fin_plan_level_code ='P' THEN
3188 
3189              IF l_control_flag ='N' THEN
3190 
3191 
3192                  IF P_PA_DEBUG_MODE = 'Y' THEN
3193                      pa_debug.g_err_stage:='Using the Fourth RA Insert';
3194                      pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
3195                  END IF;
3196 
3197                  INSERT INTO PA_RESOURCE_ASSIGNMENTS(
3198                           resource_assignment_id
3199                           ,budget_version_id
3200                           ,project_id
3201                           ,task_id
3202                           ,resource_list_member_id
3203                           ,last_update_date
3204                           ,last_updated_by
3205                           ,creation_date
3206                           ,created_by
3207                           ,last_update_login
3208                           ,unit_of_measure
3209                           ,track_as_labor_flag
3210                           ,total_plan_revenue
3211                           ,total_plan_raw_cost
3212                           ,total_plan_burdened_cost
3213                           ,total_plan_quantity
3214                           ,resource_assignment_type
3215                           ,total_project_raw_cost
3216                           ,total_project_burdened_cost
3217                           ,total_project_revenue
3218                           ,standard_bill_rate
3219                           ,average_bill_rate
3220                           ,average_cost_rate
3221                           ,project_assignment_id
3222                           ,plan_error_code
3223                           ,average_discount_percentage
3224                           ,total_borrowed_revenue
3225                           ,total_revenue_adj
3226                           ,total_lent_resource_cost
3227                           ,total_cost_adj
3228                           ,total_unassigned_time_cost
3229                           ,total_utilization_percent
3230                           ,total_utilization_hours
3231                           ,total_utilization_adj
3232                           ,total_capacity
3233                           ,total_head_count
3234                           ,total_head_count_adj
3235                           ,total_tp_revenue_in
3236                           ,total_tp_revenue_out
3237                           ,total_tp_cost_in
3238                           ,total_tp_cost_out
3239                           ,parent_assignment_id
3240                           ,wbs_element_version_id
3241                           ,rbs_element_id
3242                           ,planning_start_date
3243                           ,planning_end_date
3244                           ,schedule_start_date
3245                           ,schedule_end_date
3246                           ,spread_curve_id
3247                           ,etc_method_code
3248                           ,res_type_code
3249                           ,attribute_category
3250                           ,attribute1
3251                           ,attribute2
3252                           ,attribute3
3253                           ,attribute4
3254                           ,attribute5
3255                           ,attribute6
3256                           ,attribute7
3257                           ,attribute8
3258                           ,attribute9
3259                           ,attribute10
3260                           ,attribute11
3261                           ,attribute12
3262                           ,attribute13
3263                           ,attribute14
3264                           ,attribute15
3265                           ,attribute16
3266                           ,attribute17
3267                           ,attribute18
3268                           ,attribute19
3269                           ,attribute20
3270                           ,attribute21
3271                           ,attribute22
3272                           ,attribute23
3273                           ,attribute24
3274                           ,attribute25
3275                           ,attribute26
3276                           ,attribute27
3277                           ,attribute28
3278                           ,attribute29
3279                           ,attribute30
3280                           ,fc_res_type_code
3281                           ,resource_class_code
3282                           ,organization_id
3283                           ,job_id
3284                           ,person_id
3285                           ,expenditure_type
3286                           ,expenditure_category
3287                           ,revenue_category_code
3288                           ,event_type
3289                           ,supplier_id
3290                           ,non_labor_resource
3291                           ,bom_resource_id
3292                           ,inventory_item_id
3293                           ,item_category_id
3294                           ,record_version_number
3295                           ,transaction_source_code
3296                           ,mfc_cost_type_id
3297                           ,procure_resource_flag
3298                           ,assignment_description
3299                           ,incurred_by_res_flag
3300                           ,rate_job_id
3301                           ,rate_expenditure_type
3302                           ,ta_display_flag
3303                           ,sp_fixed_date
3304                           ,person_type_code
3305                           ,rate_based_flag
3306                           ,resource_rate_based_flag --IPM Arch Enhacements Bug 4865563
3307                           ,use_task_schedule_flag
3308                           ,rate_exp_func_curr_code
3309                           ,rate_expenditure_org_id
3310                           ,incur_by_res_class_code
3311                           ,incur_by_role_id
3312                           ,project_role_id
3313                           ,resource_class_flag
3314                           ,named_role
3315                           ,txn_accum_header_id
3316                           ,scheduled_delay --For Bug 3948128
3317 						  ,CBS_ELEMENT_ID -- bug#16200605
3318                           )
3319                  SELECT
3320                            pa_resource_assignments_s.nextval
3321                           ,p_target_plan_version_id
3322                           ,l_target_project_id
3323                           ,pra.task_id
3324                           ,prlmt.resource_list_member_id
3325                           ,sysdate
3326                           ,fnd_global.user_id
3327                           ,sysdate
3328                           ,fnd_global.user_id
3329                           ,fnd_global.login_id
3330                           ,pra.unit_of_measure
3331                           ,pra.track_as_labor_flag
3332                           ,DECODE(l_adj_percentage,0,DECODE(l_revenue_flag,'Y',total_plan_revenue,NULL),NULL)
3333                           ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_plan_raw_cost,NULL),NULL)
3334                           ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_plan_burdened_cost,NULL),NULL)
3335                           ,DECODE(l_adj_percentage,0,total_plan_quantity,NULL)
3336                           ,pra.resource_assignment_type
3337                           ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_project_raw_cost,NULL),NULL)
3338                           ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_project_burdened_cost,NULL),NULL)
3339                           ,DECODE(l_adj_percentage,0,DECODE(l_revenue_flag,'Y',total_project_revenue,NULL),NULL)
3340                           ,standard_bill_rate
3341                           ,average_bill_rate
3342                           ,average_cost_rate
3343                           ,pra.project_assignment_id
3344                           ,plan_error_code
3345                           ,average_discount_percentage
3346                           ,total_borrowed_revenue
3347                           ,total_revenue_adj
3348                           ,total_lent_resource_cost
3349                           ,total_cost_adj
3350                           ,total_unassigned_time_cost
3351                           ,total_utilization_percent
3352                           ,total_utilization_hours
3353                           ,total_utilization_adj
3354                           ,total_capacity
3355                           ,total_head_count
3356                           ,total_head_count_adj
3357                           ,total_tp_revenue_in
3358                           ,total_tp_revenue_out
3359                           ,total_tp_cost_in
3360                           ,total_tp_cost_out
3361                           --parent assignment id in the target resource assignments contain source resource assignment id
3362                           --Bug 4200168
3363                           ,pra.resource_assignment_id
3364                           ,pra.wbs_element_version_id
3365                           ,pra.rbs_element_id
3366                           ,pra.planning_start_date -- Planning start date of the target (Bug 3354518)
3367                           ,pra.planning_end_date   -- Planning end date of the target  (Bug 3354518)
3368                           ,pra.schedule_start_date
3369                           ,pra.schedule_end_date
3370                           ,pra.spread_curve_id
3371                           ,pra.etc_method_code
3372                           ,pra.res_type_code
3373                           ,pra.attribute_category
3374                           ,pra.attribute1
3375                           ,pra.attribute2
3376                           ,pra.attribute3
3377                           ,pra.attribute4
3378                           ,pra.attribute5
3379                           ,pra.attribute6
3380                           ,pra.attribute7
3381                           ,pra.attribute8
3382                           ,pra.attribute9
3383                           ,pra.attribute10
3384                           ,pra.attribute11
3385                           ,pra.attribute12
3386                           ,pra.attribute13
3387                           ,pra.attribute14
3388                           ,pra.attribute15
3389                           ,pra.attribute16
3390                           ,pra.attribute17
3391                           ,pra.attribute18
3392                           ,pra.attribute19
3393                           ,pra.attribute20
3394                           ,pra.attribute21
3395                           ,pra.attribute22
3396                           ,pra.attribute23
3397                           ,pra.attribute24
3398                           ,pra.attribute25
3399                           ,pra.attribute26
3400                           ,pra.attribute27
3401                           ,pra.attribute28
3402                           ,pra.attribute29
3403                           ,pra.attribute30
3404                           ,pra.fc_res_type_code
3405                           ,pra.resource_class_code
3406                           ,pra.organization_id
3407                           ,pra.job_id
3408                           ,pra.person_id
3409                           ,pra.expenditure_type
3410                           ,pra.expenditure_category
3411                           ,pra.revenue_category_code
3412                           ,pra.event_type
3413                           ,pra.supplier_id
3414                           ,pra.non_labor_resource
3415                           ,pra.bom_resource_id
3416                           ,pra.inventory_item_id
3417                           ,pra.item_category_id
3418                           ,1    -- should be 1 in the target version being created
3419                           ,decode(p_calling_context, 'CREATE_VERSION', NULL, pra.transaction_source_code)
3420                           ,pra.mfc_cost_type_id
3421                           ,pra.procure_resource_flag
3422                           ,pra.assignment_description
3423                           ,pra.incurred_by_res_flag
3424                           ,pra.rate_job_id
3425                           ,pra.rate_expenditure_type
3426                           ,pra.ta_display_flag
3427                           ,pra.sp_fixed_date
3428                           ,pra.person_type_code
3429                           ,pra.rate_based_flag
3430                           ,pra.resource_rate_based_flag  --IPM Arch Enhancement Bug 4865563
3431                           ,pra.use_task_schedule_flag
3432                           ,pra.rate_exp_func_curr_code
3433                           ,pra.rate_expenditure_org_id
3434                           ,pra.incur_by_res_class_code
3435                           ,pra.incur_by_role_id
3436                           ,pra.project_role_id
3437                           ,pra.resource_class_flag
3438                           ,pra.named_role
3439                           ,pra.txn_accum_header_id
3440                           ,scheduled_delay --For Bug 3948128
3441 						  ,pra.CBS_ELEMENT_ID -- bug#16200605
3442                  FROM     PA_RESOURCE_ASSIGNMENTS pra,
3443                           pa_resource_list_members prlms,
3444                           pa_resource_list_members prlmt
3445                  WHERE    pra.budget_version_id      = p_source_plan_version_id
3446                  AND      prlms.resource_list_member_id=pra.resource_list_member_id
3447                  AND      prlms.resource_list_id=l_resource_list_id
3448                  AND      prlms.object_id=l_source_project_id
3449                  AND      prlms.object_type='PROJECT'
3450                  AND      prlmt.resource_list_id=l_resource_list_id
3451                  AND      prlmt.object_id=l_target_project_id
3452                  AND      prlmt.object_type='PROJECT'
3453                  AND      prlmt.alias=prlms.alias;
3454 
3455              END IF;--IF l_control_flag ='N' THEN
3456 
3457          ELSIF l_fin_plan_level_code <> 'P' THEN
3458 
3459              IF l_control_flag ='N' THEN
3460 
3461 
3462                  IF P_PA_DEBUG_MODE = 'Y' THEN
3463                      pa_debug.g_err_stage:='Using the FIFTH RA Insert';
3464                      pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
3465                  END IF;
3466 
3467                  INSERT INTO PA_RESOURCE_ASSIGNMENTS(
3468                           resource_assignment_id
3469                           ,budget_version_id
3470                           ,project_id
3471                           ,task_id
3472                           ,resource_list_member_id
3473                           ,last_update_date
3474                           ,last_updated_by
3475                           ,creation_date
3476                           ,created_by
3477                           ,last_update_login
3478                           ,unit_of_measure
3479                           ,track_as_labor_flag
3480                           ,total_plan_revenue
3481                           ,total_plan_raw_cost
3482                           ,total_plan_burdened_cost
3483                           ,total_plan_quantity
3484                           ,resource_assignment_type
3485                           ,total_project_raw_cost
3486                           ,total_project_burdened_cost
3487                           ,total_project_revenue
3488                           ,standard_bill_rate
3489                           ,average_bill_rate
3490                           ,average_cost_rate
3491                           ,project_assignment_id
3492                           ,plan_error_code
3493                           ,average_discount_percentage
3494                           ,total_borrowed_revenue
3495                           ,total_revenue_adj
3496                           ,total_lent_resource_cost
3497                           ,total_cost_adj
3498                           ,total_unassigned_time_cost
3499                           ,total_utilization_percent
3500                           ,total_utilization_hours
3501                           ,total_utilization_adj
3502                           ,total_capacity
3503                           ,total_head_count
3504                           ,total_head_count_adj
3505                           ,total_tp_revenue_in
3506                           ,total_tp_revenue_out
3507                           ,total_tp_cost_in
3508                           ,total_tp_cost_out
3509                           ,parent_assignment_id
3510                           ,wbs_element_version_id
3511                           ,rbs_element_id
3512                           ,planning_start_date
3513                           ,planning_end_date
3514                           ,schedule_start_date
3515                           ,schedule_end_date
3516                           ,spread_curve_id
3517                           ,etc_method_code
3518                           ,res_type_code
3519                           ,attribute_category
3520                           ,attribute1
3521                           ,attribute2
3522                           ,attribute3
3523                           ,attribute4
3524                           ,attribute5
3525                           ,attribute6
3526                           ,attribute7
3527                           ,attribute8
3528                           ,attribute9
3529                           ,attribute10
3530                           ,attribute11
3531                           ,attribute12
3532                           ,attribute13
3533                           ,attribute14
3534                           ,attribute15
3535                           ,attribute16
3536                           ,attribute17
3537                           ,attribute18
3538                           ,attribute19
3539                           ,attribute20
3540                           ,attribute21
3541                           ,attribute22
3542                           ,attribute23
3543                           ,attribute24
3544                           ,attribute25
3545                           ,attribute26
3546                           ,attribute27
3547                           ,attribute28
3548                           ,attribute29
3549                           ,attribute30
3550                           ,fc_res_type_code
3551                           ,resource_class_code
3552                           ,organization_id
3553                           ,job_id
3554                           ,person_id
3555                           ,expenditure_type
3556                           ,expenditure_category
3557                           ,revenue_category_code
3558                           ,event_type
3559                           ,supplier_id
3560                           ,non_labor_resource
3561                           ,bom_resource_id
3562                           ,inventory_item_id
3563                           ,item_category_id
3564                           ,record_version_number
3565                           ,transaction_source_code
3566                           ,mfc_cost_type_id
3567                           ,procure_resource_flag
3568                           ,assignment_description
3569                           ,incurred_by_res_flag
3570                           ,rate_job_id
3571                           ,rate_expenditure_type
3572                           ,ta_display_flag
3573                           ,sp_fixed_date
3574                           ,person_type_code
3575                           ,rate_based_flag
3576                           ,resource_rate_based_flag --IPM Arch Enhancements Bug 4865563
3577                           ,use_task_schedule_flag
3578                           ,rate_exp_func_curr_code
3579                           ,rate_expenditure_org_id
3580                           ,incur_by_res_class_code
3581                           ,incur_by_role_id
3582                           ,project_role_id
3583                           ,resource_class_flag
3584                           ,named_role
3585                           ,txn_accum_header_id
3586                           ,scheduled_delay --For Bug 3948128
3587 						  ,CBS_ELEMENT_ID -- bug#16200605
3588                           )
3589                  SELECT
3590                            pa_resource_assignments_s.nextval
3591                           ,p_target_plan_version_id
3592                           ,l_target_project_id
3593                           ,pelm.target_task_id
3594                           ,prlmt.resource_list_member_id
3595                           ,sysdate
3596                           ,fnd_global.user_id
3597                           ,sysdate
3598                           ,fnd_global.user_id
3599                           ,fnd_global.login_id
3600                           ,pra.unit_of_measure
3601                           ,pra.track_as_labor_flag
3602                           ,DECODE(l_adj_percentage,0,DECODE(l_revenue_flag,'Y',total_plan_revenue,NULL),NULL)
3603                           ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_plan_raw_cost,NULL),NULL)
3604                           ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_plan_burdened_cost,NULL),NULL)
3605                           ,DECODE(l_adj_percentage,0,total_plan_quantity,NULL)
3606                           ,pra.resource_assignment_type
3607                           ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_project_raw_cost,NULL),NULL)
3608                           ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_project_burdened_cost,NULL),NULL)
3609                           ,DECODE(l_adj_percentage,0,DECODE(l_revenue_flag,'Y',total_project_revenue,NULL),NULL)
3610                           ,standard_bill_rate
3611                           ,average_bill_rate
3612                           ,average_cost_rate
3613                           ,pra.project_assignment_id
3614                           ,plan_error_code
3615                           ,average_discount_percentage
3616                           ,total_borrowed_revenue
3617                           ,total_revenue_adj
3618                           ,total_lent_resource_cost
3619                           ,total_cost_adj
3620                           ,total_unassigned_time_cost
3621                           ,total_utilization_percent
3622                           ,total_utilization_hours
3623                           ,total_utilization_adj
3624                           ,total_capacity
3625                           ,total_head_count
3626                           ,total_head_count_adj
3627                           ,total_tp_revenue_in
3628                           ,total_tp_revenue_out
3629                           ,total_tp_cost_in
3630                           ,total_tp_cost_out
3631                           --parent assignment id in the target resource assignments contain source resource assignment id
3632                           --Bug 4200168
3633                           ,pra.resource_assignment_id
3634                           ,pra.wbs_element_version_id
3635                           ,pra.rbs_element_id
3636                           ,pra.planning_start_date -- Planning start date of the target (Bug 3354518)
3637                           ,pra.planning_end_date   -- Planning end date of the target  (Bug 3354518)
3638                           ,pra.schedule_start_date
3639                           ,pra.schedule_end_date
3640                           ,pra.spread_curve_id
3641                           ,pra.etc_method_code
3642                           ,pra.res_type_code
3643                           ,pra.attribute_category
3644                           ,pra.attribute1
3645                           ,pra.attribute2
3646                           ,pra.attribute3
3647                           ,pra.attribute4
3648                           ,pra.attribute5
3649                           ,pra.attribute6
3650                           ,pra.attribute7
3651                           ,pra.attribute8
3652                           ,pra.attribute9
3653                           ,pra.attribute10
3654                           ,pra.attribute11
3655                           ,pra.attribute12
3656                           ,pra.attribute13
3657                           ,pra.attribute14
3658                           ,pra.attribute15
3659                           ,pra.attribute16
3660                           ,pra.attribute17
3661                           ,pra.attribute18
3662                           ,pra.attribute19
3663                           ,pra.attribute20
3664                           ,pra.attribute21
3665                           ,pra.attribute22
3666                           ,pra.attribute23
3667                           ,pra.attribute24
3668                           ,pra.attribute25
3669                           ,pra.attribute26
3670                           ,pra.attribute27
3671                           ,pra.attribute28
3672                           ,pra.attribute29
3673                           ,pra.attribute30
3674                           ,pra.fc_res_type_code
3675                           ,pra.resource_class_code
3676                           ,pra.organization_id
3677                           ,pra.job_id
3678                           ,pra.person_id
3679                           ,pra.expenditure_type
3680                           ,pra.expenditure_category
3681                           ,pra.revenue_category_code
3682                           ,pra.event_type
3683                           ,pra.supplier_id
3684                           ,pra.non_labor_resource
3685                           ,pra.bom_resource_id
3686                           ,pra.inventory_item_id
3687                           ,pra.item_category_id
3688                           ,1    -- should be 1 in the target version being created
3689                           ,decode(p_calling_context, 'CREATE_VERSION', NULL, pra.transaction_source_code)
3690                           ,pra.mfc_cost_type_id
3691                           ,pra.procure_resource_flag
3692                           ,pra.assignment_description
3693                           ,pra.incurred_by_res_flag
3694                           ,pra.rate_job_id
3695                           ,pra.rate_expenditure_type
3696                           ,pra.ta_display_flag
3697                           ,pra.sp_fixed_date
3698                           ,pra.person_type_code
3699                           ,pra.rate_based_flag
3700                           ,pra.resource_rate_based_flag --IPM Arch Enhancements Bug 4865563
3701                           ,pra.use_task_schedule_flag
3702                           ,pra.rate_exp_func_curr_code
3703                           ,pra.rate_expenditure_org_id
3704                           ,pra.incur_by_res_class_code
3705                           ,pra.incur_by_role_id
3706                           ,pra.project_role_id
3707                           ,pra.resource_class_flag
3708                           ,pra.named_role
3709                           ,pra.txn_accum_header_id
3710                           ,scheduled_delay --For Bug 3948128
3711 						  ,pra.CBS_ELEMENT_ID -- bug#16200605
3712                  FROM     PA_RESOURCE_ASSIGNMENTS pra,
3713                           (SELECT TO_NUMBER(attribute15) source_task_id,
3714                                   proj_element_id  target_task_id
3715                            FROM   pa_proj_elements
3716                            WHERE  project_id = l_target_project_id
3717                            AND    object_type = 'PA_TASKS'
3718                            UNION ALL
3719                            SELECT 0 source_task_id,
3720                                   0 target_task_id
3721                            FROM   dual) pelm,
3722                           pa_resource_list_members prlms,
3723                           pa_resource_list_members prlmt
3724                  WHERE    pra.budget_version_id      = p_source_plan_version_id
3725                  AND      prlms.resource_list_member_id=pra.resource_list_member_id
3726                  AND      prlms.resource_list_id=l_resource_list_id
3727                  AND      prlms.object_id=l_source_project_id
3728                  AND      prlms.object_type='PROJECT'
3729                  AND      prlmt.resource_list_id=l_resource_list_id
3730                  AND      prlmt.object_id=l_target_project_id
3731                  AND      prlmt.object_type='PROJECT'
3732                  AND      prlmt.alias=prlms.alias
3733                  AND      pelm.source_task_id=pra.task_id;
3734 
3735              ELSE --Control Flag is Y and Planning Level is not project
3736 
3737                  IF P_PA_DEBUG_MODE = 'Y' THEN
3738                      pa_debug.g_err_stage:='Using the Sixth RA Insert';
3739                      pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
3740                  END IF;
3741 
3742                  INSERT INTO PA_RESOURCE_ASSIGNMENTS(
3743                           resource_assignment_id
3744                           ,budget_version_id
3745                           ,project_id
3746                           ,task_id
3747                           ,resource_list_member_id
3748                           ,last_update_date
3749                           ,last_updated_by
3750                           ,creation_date
3751                           ,created_by
3752                           ,last_update_login
3753                           ,unit_of_measure
3754                           ,track_as_labor_flag
3755                           ,total_plan_revenue
3756                           ,total_plan_raw_cost
3757                           ,total_plan_burdened_cost
3758                           ,total_plan_quantity
3759                           ,resource_assignment_type
3760                           ,total_project_raw_cost
3761                           ,total_project_burdened_cost
3762                           ,total_project_revenue
3763                           ,standard_bill_rate
3764                           ,average_bill_rate
3765                           ,average_cost_rate
3766                           ,project_assignment_id
3767                           ,plan_error_code
3768                           ,average_discount_percentage
3769                           ,total_borrowed_revenue
3770                           ,total_revenue_adj
3771                           ,total_lent_resource_cost
3772                           ,total_cost_adj
3773                           ,total_unassigned_time_cost
3774                           ,total_utilization_percent
3775                           ,total_utilization_hours
3776                           ,total_utilization_adj
3777                           ,total_capacity
3778                           ,total_head_count
3779                           ,total_head_count_adj
3780                           ,total_tp_revenue_in
3781                           ,total_tp_revenue_out
3782                           ,total_tp_cost_in
3783                           ,total_tp_cost_out
3784                           ,parent_assignment_id
3785                           ,wbs_element_version_id
3786                           ,rbs_element_id
3787                           ,planning_start_date
3788                           ,planning_end_date
3789                           ,schedule_start_date
3790                           ,schedule_end_date
3791                           ,spread_curve_id
3792                           ,etc_method_code
3793                           ,res_type_code
3794                           ,attribute_category
3795                           ,attribute1
3796                           ,attribute2
3797                           ,attribute3
3798                           ,attribute4
3799                           ,attribute5
3800                           ,attribute6
3801                           ,attribute7
3802                           ,attribute8
3803                           ,attribute9
3804                           ,attribute10
3805                           ,attribute11
3806                           ,attribute12
3807                           ,attribute13
3808                           ,attribute14
3809                           ,attribute15
3810                           ,attribute16
3811                           ,attribute17
3812                           ,attribute18
3813                           ,attribute19
3814                           ,attribute20
3815                           ,attribute21
3816                           ,attribute22
3817                           ,attribute23
3818                           ,attribute24
3819                           ,attribute25
3820                           ,attribute26
3821                           ,attribute27
3822                           ,attribute28
3823                           ,attribute29
3824                           ,attribute30
3825                           ,fc_res_type_code
3826                           ,resource_class_code
3827                           ,organization_id
3828                           ,job_id
3829                           ,person_id
3830                           ,expenditure_type
3831                           ,expenditure_category
3832                           ,revenue_category_code
3833                           ,event_type
3834                           ,supplier_id
3835                           ,non_labor_resource
3836                           ,bom_resource_id
3837                           ,inventory_item_id
3838                           ,item_category_id
3839                           ,record_version_number
3840                           ,transaction_source_code
3841                           ,mfc_cost_type_id
3842                           ,procure_resource_flag
3843                           ,assignment_description
3844                           ,incurred_by_res_flag
3845                           ,rate_job_id
3846                           ,rate_expenditure_type
3847                           ,ta_display_flag
3848                           ,sp_fixed_date
3849                           ,person_type_code
3850                           ,rate_based_flag
3851                           ,resource_rate_based_flag --IPM Arch Enhancements Bug 4865563
3852                           ,use_task_schedule_flag
3853                           ,rate_exp_func_curr_code
3854                           ,rate_expenditure_org_id
3855                           ,incur_by_res_class_code
3856                           ,incur_by_role_id
3857                           ,project_role_id
3858                           ,resource_class_flag
3859                           ,named_role
3860                           ,txn_accum_header_id
3861                           ,scheduled_delay --For Bug 3948128
3862 						  ,CBS_ELEMENT_ID -- bug#16200605
3863                           )
3864                  SELECT
3865                            pa_resource_assignments_s.nextval
3866                           ,p_target_plan_version_id
3867                           ,l_target_project_id
3868                           ,pelm.target_task_id
3869                           ,pra.resource_list_member_id
3870                           ,sysdate
3871                           ,fnd_global.user_id
3872                           ,sysdate
3873                           ,fnd_global.user_id
3874                           ,fnd_global.login_id
3875                           ,pra.unit_of_measure
3876                           ,pra.track_as_labor_flag
3877                           ,DECODE(l_adj_percentage,0,DECODE(l_revenue_flag,'Y',total_plan_revenue,NULL),NULL)
3878                           ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_plan_raw_cost,NULL),NULL)
3879                           ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_plan_burdened_cost,NULL),NULL)
3880                           ,DECODE(l_adj_percentage,0,total_plan_quantity,NULL)
3881                           ,pra.resource_assignment_type
3882                           ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_project_raw_cost,NULL),NULL)
3883                           ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_project_burdened_cost,NULL),NULL)
3884                           ,DECODE(l_adj_percentage,0,DECODE(l_revenue_flag,'Y',total_project_revenue,NULL),NULL)
3885                           ,standard_bill_rate
3886                           ,average_bill_rate
3887                           ,average_cost_rate
3888                           ,pra.project_assignment_id
3889                           ,plan_error_code
3890                           ,average_discount_percentage
3891                           ,total_borrowed_revenue
3892                           ,total_revenue_adj
3893                           ,total_lent_resource_cost
3894                           ,total_cost_adj
3895                           ,total_unassigned_time_cost
3896                           ,total_utilization_percent
3897                           ,total_utilization_hours
3898                           ,total_utilization_adj
3899                           ,total_capacity
3900                           ,total_head_count
3901                           ,total_head_count_adj
3902                           ,total_tp_revenue_in
3903                           ,total_tp_revenue_out
3904                           ,total_tp_cost_in
3905                           ,total_tp_cost_out
3906                           --parent assignment id in the target resource assignments contain source resource assignment id
3907                           --Bug 4200168
3908                           ,pra.resource_assignment_id
3909                           ,pra.wbs_element_version_id
3910                           ,pra.rbs_element_id
3911                           ,pra.planning_start_date -- Planning start date of the target (Bug 3354518)
3912                           ,pra.planning_end_date   -- Planning end date of the target  (Bug 3354518)
3913                           ,pra.schedule_start_date
3914                           ,pra.schedule_end_date
3915                           ,pra.spread_curve_id
3916                           ,pra.etc_method_code
3917                           ,pra.res_type_code
3918                           ,pra.attribute_category
3919                           ,pra.attribute1
3920                           ,pra.attribute2
3921                           ,pra.attribute3
3922                           ,pra.attribute4
3923                           ,pra.attribute5
3924                           ,pra.attribute6
3925                           ,pra.attribute7
3926                           ,pra.attribute8
3927                           ,pra.attribute9
3928                           ,pra.attribute10
3929                           ,pra.attribute11
3930                           ,pra.attribute12
3931                           ,pra.attribute13
3932                           ,pra.attribute14
3933                           ,pra.attribute15
3934                           ,pra.attribute16
3935                           ,pra.attribute17
3936                           ,pra.attribute18
3937                           ,pra.attribute19
3938                           ,pra.attribute20
3939                           ,pra.attribute21
3940                           ,pra.attribute22
3941                           ,pra.attribute23
3942                           ,pra.attribute24
3943                           ,pra.attribute25
3944                           ,pra.attribute26
3945                           ,pra.attribute27
3946                           ,pra.attribute28
3947                           ,pra.attribute29
3948                           ,pra.attribute30
3949                           ,pra.fc_res_type_code
3950                           ,pra.resource_class_code
3951                           ,pra.organization_id
3952                           ,pra.job_id
3953                           ,pra.person_id
3954                           ,pra.expenditure_type
3955                           ,pra.expenditure_category
3956                           ,pra.revenue_category_code
3957                           ,pra.event_type
3958                           ,pra.supplier_id
3959                           ,pra.non_labor_resource
3960                           ,pra.bom_resource_id
3961                           ,pra.inventory_item_id
3962                           ,pra.item_category_id
3963                           ,1    -- should be 1 in the target version being created
3964                           ,decode(p_calling_context, 'CREATE_VERSION', NULL, pra.transaction_source_code)
3965                           ,pra.mfc_cost_type_id
3966                           ,pra.procure_resource_flag
3967                           ,pra.assignment_description
3968                           ,pra.incurred_by_res_flag
3969                           ,pra.rate_job_id
3970                           ,pra.rate_expenditure_type
3971                           ,pra.ta_display_flag
3972                           ,pra.sp_fixed_date
3973                           ,pra.person_type_code
3974                           ,pra.rate_based_flag
3975                           ,pra.resource_rate_based_flag --IPM Arch Enhancements Bug 4865563
3976                           ,pra.use_task_schedule_flag
3977                           ,pra.rate_exp_func_curr_code
3978                           ,pra.rate_expenditure_org_id
3979                           ,pra.incur_by_res_class_code
3980                           ,pra.incur_by_role_id
3981                           ,pra.project_role_id
3982                           ,pra.resource_class_flag
3983                           ,pra.named_role
3984                           ,pra.txn_accum_header_id
3985                           ,scheduled_delay --For Bug 3948128
3986 						  ,pra.CBS_ELEMENT_ID -- bug#16200605
3987                  FROM     PA_RESOURCE_ASSIGNMENTS pra,
3988                           (SELECT TO_NUMBER(attribute15) source_task_id,
3989                                   proj_element_id  target_task_id
3990                            FROM   pa_proj_elements
3991                            WHERE  project_id = l_target_project_id
3992                            AND    object_type='PA_TASKS'
3993                            UNION ALL
3994                            SELECT 0 source_task_id,
3995                                   0 target_task_id
3996                            FROM   dual) pelm
3997                  WHERE    pra.budget_version_id= p_source_plan_version_id
3998                  AND      pelm.source_task_id=pra.task_id;
3999 
4000              END IF;--IF l_control_flag ='N' THEN
4001 
4002          END IF;--ELSIF l_fin_plan_level_code <> 'P' THEN
4003 
4004      END IF;--IF p_rbs_map_diff_flag ='N' THEN
4005 
4006      l_tmp := SQL%ROWCOUNT;
4007      IF P_PA_DEBUG_MODE = 'Y' THEN
4008          pa_debug.g_err_stage:='No. of records inserted into PRA '||l_tmp;
4009          pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
4010      END IF;
4011      /** Bug 6161031 When we copy the resource assignments ideally we should call the
4012          PA_PLANNING_RESOURCE_UTILS.get_resource_defaults and use the new
4013          attributes. But organization_id is the only column which is depending
4014          on the project when the resource format contains job and doesn't contain organization_id.
4015          So for all resource assignments with resource format having job in workplan if the
4016          organiztion_id is null in the pa_resource_list_members we are overriding
4017          with current project carrying_out_organization_id.
4018      */
4019      IF p_calling_context='WORKPLAN' THEN
4020 
4021              select ppa.carrying_out_organization_id
4022              into l_project_org
4023              from pa_projects_all ppa,
4024                   pa_budget_versions pbv
4025              where pbv.budget_version_id = p_target_plan_version_id
4026              and pbv.project_id = ppa.project_id;
4027 
4028              update pa_resource_assignments
4029              set organization_id=l_project_org
4030              where resource_assignment_id in
4031                  (select pra.resource_assignment_id
4032                   from pa_resource_assignments pra,
4033                        pa_resource_list_members prlm
4034                   where pra.budget_version_id=p_target_plan_version_id
4035                   and   pra.resource_list_member_id=prlm.resource_list_member_id
4036                   and   pra.job_id  = prlm.job_id
4037                   and   prlm.organization_id is null);
4038      END IF;
4039 
4040      -- Bug 3362316, 08-JAN-2003: Added New FP.M Columns  --------------------------
4041      IF P_PA_DEBUG_MODE = 'Y' THEN
4042          pa_debug.g_err_stage:='Exiting Copy_Resource_Assignments';
4043          pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
4044          pa_debug.reset_err_stack;  -- bug:- 2815593
4045      END IF;
4046  EXCEPTION
4047 
4048     WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
4049          l_msg_count := FND_MSG_PUB.count_msg;
4050          IF l_msg_count = 1 THEN
4051               PA_INTERFACE_UTILS_PUB.get_messages
4052                    (p_encoded         => FND_API.G_TRUE
4053                     ,p_msg_index      => 1
4054                     ,p_msg_count      => l_msg_count
4055                     ,p_msg_data       => l_msg_data
4056                     ,p_data           => l_data
4057                     ,p_msg_index_out  => l_msg_index_out);
4058                x_msg_data := l_data;
4059                x_msg_count := l_msg_count;
4060           ELSE
4061                x_msg_count := l_msg_count;
4062           END IF;
4063           x_return_status := FND_API.G_RET_STS_ERROR;
4064 
4065           IF P_PA_DEBUG_MODE = 'Y' THEN
4066               pa_debug.g_err_stage:='Invalid Arguments Passed';
4067               pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,5);
4068               pa_debug.reset_err_stack;
4069 	END IF;
4070           RAISE;
4071 
4072    WHEN Others THEN
4073          x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4074          x_msg_count     := 1;
4075          x_msg_data      := SQLERRM;
4076          FND_MSG_PUB.add_exc_msg( p_pkg_name=> 'PA_FP_COPY_FROM_PKG'
4077                           ,p_procedure_name  => 'COPY_RESOURCE_ASSIGNMENTS');
4078 
4079          IF P_PA_DEBUG_MODE = 'Y' THEN
4080              pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
4081              pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,5);
4082              pa_debug.reset_err_stack;
4083 	 END IF;
4084          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4085 
4086   END Copy_Resource_Assignments;
4087 
4088   --Bug 4290043.This private API will be called from  copy_budget_lines and Copy_Budget_Lines_Appr_Rev in this package
4089   --This API will
4090   ----1. Call pa_fp_upgrade_pkg.Apply_Calculate_FPM_Rules to get the missing amounts/rates in the target version
4091   ----2. Call PA_FP_CALC_PLAN_PKG.CheckZeroQTyNegETC to check for the negative ETC/Qty in the target version
4092   ----3. Update rate based flag for RAs in the target version if any RAs have to be converted to non rate based
4093   ----4. Update the budget lines with the missing amounts
4094   PROCEDURE derv_missing_amts_chk_neg_qty
4095   (p_budget_version_id            IN  pa_budget_versions.budget_version_id%TYPE,
4096    p_targ_pref_code               IN  pa_proj_fp_options.fin_plan_preference_code%TYPE,
4097    p_source_version_type          IN  pa_budget_versions.version_type%TYPE,
4098    p_target_version_type          IN  pa_budget_versions.version_type%TYPE,
4099    p_src_plan_class_code          IN  pa_fin_plan_types_b.plan_class_code%TYPE,
4100    p_derv_rates_missing_amts_flag IN  VARCHAR2,
4101    p_adj_percentage               IN  NUMBER,
4102    x_return_status                OUT NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
4103    x_msg_count                    OUT NOCOPY NUMBER, --File.Sql.39 bug 4440895
4104    x_msg_data                     OUT NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
4105    IS
4106 
4107     l_msg_count                    NUMBER ;
4108     l_data                         VARCHAR2(2000);
4109     l_msg_data                     VARCHAR2(2000);
4110     l_error_msg_code               VARCHAR2(2000);
4111     l_msg_index_out                NUMBER;
4112     l_return_status                VARCHAR2(2000);
4113     l_debug_mode                   VARCHAR2(30);
4114     l_module_name                  VARCHAR2(100);
4115 
4116     l_bl_id_tbl                    SYSTEM.pa_num_tbl_type;
4117     l_ra_id_tbl                    SYSTEM.pa_num_tbl_type;
4118     l_quantity_tbl                 SYSTEM.pa_num_tbl_type;
4119     l_txn_raw_cost_tbl             SYSTEM.pa_num_tbl_type;
4120     l_txn_burdened_cost_tbl        SYSTEM.pa_num_tbl_type;
4121     l_txn_revenue_tbl              SYSTEM.pa_num_tbl_type;
4122     l_rate_based_flag_tbl          SYSTEM.pa_varchar2_1_tbl_type;
4123     l_raw_cost_override_rate_tbl   SYSTEM.pa_num_tbl_type;
4124     l_burd_cost_override_rate_tbl  SYSTEM.pa_num_tbl_type;
4125     l_bill_override_rate_tbl       SYSTEM.pa_num_tbl_type;
4126     l_non_rb_ra_id_tbl             SYSTEM.pa_num_tbl_type;
4127     l_bl_rb_flag_chg_tbl           SYSTEM.pa_varchar2_1_tbl_type;
4128     l_target_pref_code             pa_proj_fp_options.fin_plan_preference_code%TYPE;
4129     l_prev_ra_id                   pa_resource_assignments.resource_assignment_id%TYPE;
4130     l_temp_flag                    VARCHAR2(1);
4131     l_init_quantity_tbl            SYSTEM.pa_num_tbl_type;
4132     l_txn_currency_code_tbl        SYSTEM.pa_varchar2_15_tbl_type;
4133     l_temp                         NUMBER;
4134 
4135     /*  Bug 5078538 --Added the out variables to avoid the uninitialized collection
4136         error. Note that this error is caused when the same variables are passed as
4137         In and In Out parameters in a api call.
4138     */
4139     l_quantity_out_tbl                 SYSTEM.pa_num_tbl_type;
4140     l_txn_raw_cost_out_tbl             SYSTEM.pa_num_tbl_type;
4141     l_txn_burdened_cost_out_tbl        SYSTEM.pa_num_tbl_type;
4142     l_txn_revenue_out_tbl              SYSTEM.pa_num_tbl_type;
4143 
4144     BEGIN
4145 
4146         x_msg_count := 0;
4147         x_return_status := FND_API.G_RET_STS_SUCCESS;
4148 
4149         fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
4150         l_debug_mode := NVL(l_debug_mode, 'Y');
4151         l_module_name := 'PAFPCPFB.mis_amts_chk_neg_qty';
4152 
4153         -- Set curr function
4154         IF l_debug_mode='Y' THEN
4155             pa_debug.set_curr_function(
4156                     p_function   =>'PAFPCPFB.mis_amts_chk_neg_qty'
4157                    ,p_debug_mode => l_debug_mode );
4158 
4159             pa_debug.g_err_stage:= 'Validation Input Parameters';
4160             pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
4161 
4162         END IF;
4163 
4164 
4165         IF  p_budget_version_id     IS NULL OR
4166             p_targ_pref_code        IS NULL OR
4167             p_source_version_type   IS NULL OR
4168             p_target_version_type   IS NULL OR
4169             p_src_plan_class_code   IS NULL OR
4170             p_derv_rates_missing_amts_flag IS NULL THEN
4171 
4172             IF l_debug_mode='Y' THEN
4173 
4174                 pa_debug.g_err_stage:= 'p_budget_version_id '|| p_budget_version_id;
4175                 pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
4176 
4177                 pa_debug.g_err_stage:= 'p_targ_pref_code '|| p_targ_pref_code;
4178                 pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
4179 
4180                 pa_debug.g_err_stage:= 'p_source_version_type '|| p_source_version_type;
4181                 pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
4182 
4183                 pa_debug.g_err_stage:= 'p_target_version_type '|| p_target_version_type;
4184                 pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
4185 
4186                 pa_debug.g_err_stage:= 'p_src_plan_class_code '|| p_src_plan_class_code;
4187                 pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
4188 
4189                 pa_debug.g_err_stage:= 'p_derv_rates_missing_amts_flag '|| p_derv_rates_missing_amts_flag;
4190                 pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
4191 
4192             END IF;
4193 
4194             PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
4195                                 p_msg_name        => 'PA_FP_INV_PARAM_PASSED',
4196                                 p_token1          => 'PROCEDURENAME',
4197                                 p_value1          => l_module_name,
4198                                 p_token2          => 'STAGE',
4199                                 p_value2          => 'Invalid Input Params');
4200 
4201 
4202             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
4203 
4204         END IF;
4205 
4206 
4207         --Select the budget lines in the target that should be processed for missing amounts and negative ETC/Qty Check
4208         SELECT  bl.budget_line_id
4209             ,bl.resource_assignment_id
4210             ,nvl(bl.quantity,0)
4211             ,nvl(bl.txn_raw_cost,0)
4212             ,nvl(bl.txn_burdened_cost,0)
4213             ,nvl(bl.txn_revenue,0)
4214             ,nvl(ra.rate_based_flag,'N') rate_based_flag
4215             ,nvl(bl.init_quantity,0)
4216             ,bl.txn_currency_code
4217         BULK COLLECT INTO
4218              l_bl_id_tbl
4219             ,l_ra_id_tbl
4220             ,l_quantity_tbl
4221             ,l_txn_raw_cost_tbl
4222             ,l_txn_burdened_cost_tbl
4223             ,l_txn_revenue_tbl
4224             ,l_rate_based_flag_tbl
4225             ,l_init_quantity_tbl
4226             ,l_txn_currency_code_tbl
4227         FROM    pa_budget_lines bl
4228             ,pa_resource_assignments ra
4229         WHERE  bl.resource_assignment_id=ra.resource_assignment_id
4230         AND    bl.budget_version_id=p_budget_version_id
4231         AND    ra.budget_version_id=p_budget_version_id
4232         ORDER  BY bl.resource_assignment_id ,bl.quantity NULLS FIRST;
4233 
4234         --Retrun if no budget lines exist in the target
4235         IF l_bl_id_tbl.COUNT =0 THEN
4236 
4237             IF l_debug_mode='Y' THEN
4238 
4239                 pa_debug.g_err_stage:= 'Budget Line count is 0';
4240                 pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
4241 
4242                 pa_debug.reset_curr_function;
4243             END IF;
4244 
4245             RETURN;
4246 
4247         END IF;
4248 
4249         --Derive the missing amounts/rates in the target.
4250         IF p_derv_rates_missing_amts_flag='Y' THEN
4251 
4252             IF l_debug_mode='Y' THEN
4253 
4254                 pa_debug.g_err_stage:= 'Calling pa_fp_upgrade_pkg.Apply_Calculate_FPM_Rules';
4255                 pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
4256 
4257             END IF;
4258 
4259             pa_fp_upgrade_pkg.Apply_Calculate_FPM_Rules
4260             ( p_preference_code              => p_targ_pref_code
4261              ,p_resource_assignment_id_tbl   => l_ra_id_tbl
4262              ,p_rate_based_flag_tbl          => l_rate_based_flag_tbl
4263              ,p_quantity_tbl                 => l_quantity_tbl
4264              ,p_txn_raw_cost_tbl             => l_txn_raw_cost_tbl
4265              ,p_txn_burdened_cost_tbl        => l_txn_burdened_cost_tbl
4266              ,p_txn_revenue_tbl              => l_txn_revenue_tbl
4267              ,x_quantity_tbl                 => l_quantity_out_tbl           --Bug 5078538
4268              ,x_txn_raw_cost_tbl             => l_txn_raw_cost_out_tbl       --Bug 5078538
4269              ,x_txn_burdened_cost_tbl        => l_txn_burdened_cost_out_tbl  --Bug 5078538
4270              ,x_txn_revenue_tbl              => l_txn_revenue_out_tbl        --Bug 5078538
4271              ,x_raw_cost_override_rate_tbl   => l_raw_cost_override_rate_tbl
4272              ,x_burd_cost_override_rate_tbl  => l_burd_cost_override_rate_tbl
4273              ,x_bill_override_rate_tbl       => l_bill_override_rate_tbl
4274              ,x_non_rb_ra_id_tbl             => l_non_rb_ra_id_tbl
4275              ,x_return_status                => l_return_status
4276              ,x_msg_count                    => l_msg_count
4277              ,x_msg_data                     => l_msg_data);
4278 
4279              IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
4280                  IF l_debug_mode = 'Y' THEN
4281                       pa_debug.g_err_stage:= 'Error in pa_fp_upgrade_pkg.Apply_Calculate_FPM_Rules';
4282                       pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
4283                  END IF;
4284                  RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
4285              END IF;
4286 
4287             l_quantity_tbl              :=   l_quantity_out_tbl;            --Bug 5078538
4288             l_txn_raw_cost_tbl          :=   l_txn_raw_cost_out_tbl;        --Bug 5078538
4289             l_txn_burdened_cost_tbl     :=   l_txn_burdened_cost_out_tbl;   --Bug 5078538
4290             l_txn_revenue_tbl           :=   l_txn_revenue_out_tbl;         --Bug 5078538
4291 
4292              IF p_source_version_type<>'ALL' AND
4293                 p_src_plan_class_code <>'FORECAST' AND
4294                 l_non_rb_ra_id_tbl.COUNT > 0 THEN
4295 
4296 
4297                 PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
4298                                     p_msg_name        => 'PA_FP_INV_PARAM_PASSED',
4299                                     p_token1          => 'PROCEDURENAME',
4300                                     p_value1          => l_module_name,
4301                                     p_token2          => 'STAGE',
4302                                     p_value2          => 'l_non_rb_ra_id_tbl.count for Non All Fcst version is '||l_non_rb_ra_id_tbl.COUNT);
4303 
4304 
4305                 RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
4306 
4307             END IF;
4308 
4309 	    END IF;--IF p_derv_rates_missing_amts_flag='Y' THEN
4310 
4311  	 /*
4312  	   Bug 5726773: Commented out the below debug statement as the call to api PA_FP_CALC_PLAN_PKG.CheckZeroQTyNegETC
4313  	    has been commented out.
4314  	 */
4315  	 /*
4316 
4317             IF l_debug_mode='Y' THEN
4318 
4319                 pa_debug.g_err_stage:= 'Calling PA_FP_CALC_PLAN_PKG.CheckZeroQTyNegETC';
4320                 pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
4321 
4322             END IF;
4323 	*/
4324 
4325 	/* Bug 5726773:
4326  	    Start of coding done for Support of negative quantity/amounts enhancement.
4327  	    Call to the API CheckZeroQtyNegETC has been commented out below to allow
4328  	    copying of -ve quantity/amounts from the source version to target version.
4329  	*/
4330 
4331         --Negative ETC/Qty for the budget lines in the target will be performed whenever the amounts in the source
4332         --or not copied directly (i.e. if the amounts in the source are modified while copying into target either
4333         --for deriving missing amounts or because of applying adj % )
4334 	/*
4335         IF p_adj_percentage <> 0 OR
4336            p_derv_rates_missing_amts_flag ='Y' THEN
4337 
4338             --Call the API to check for Negative ETC. Bug 4290043
4339             PA_FP_CALC_PLAN_PKG.CheckZeroQTyNegETC(
4340                                 p_calling_context            => 'PLS_TBL'
4341                                ,p_budget_version_id          => p_budget_version_id
4342                                ,p_initialize                 => 'Y'
4343                                ,p_resource_assignment_id_tbl => l_ra_id_tbl
4344                                ,p_quantity_tbl               => l_quantity_tbl
4345                                ,p_init_quantity_tbl          => l_init_quantity_tbl
4346                                ,p_txn_currency_code_tbl      => l_txn_currency_code_tbl
4347                                ,x_return_status              => l_return_status
4348                                ,x_msg_count                  => l_msg_count
4349                                ,x_msg_data                   => l_msg_data);
4350 
4351             IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
4352                  IF l_debug_mode = 'Y' THEN
4353                       pa_debug.g_err_stage:= 'Error in PA_FP_CALC_PLAN_PKG.CheckZeroQTyNegETC';
4354                       pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
4355                  END IF;
4356                  RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
4357             END IF;
4358 
4359         END IF;--IF p_adj_percentage <> 0 OR
4360 	 */
4361  	/* Bug 5726773: End of coding done for Support of negative quantity/amounts enhancement. */
4362 
4363         --Update the budget lines with the missing amounts/rates derived above.In the update the amount/rate
4364         --returned by the Apply_Calculate_FPM_Rules will be stamped on budget lines only when the rejection
4365         --codes do not exist for those budget lines.
4366         IF p_derv_rates_missing_amts_flag='Y' THEN
4367 
4368             IF l_debug_mode='Y' THEN
4369 
4370                 pa_debug.g_err_stage:= 'p_source_version_type '||p_source_version_type;
4371                 pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
4372 
4373                 pa_debug.g_err_stage:= 'p_target_version_type '||p_target_version_type;
4374                 pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
4375 
4376             END IF;
4377 
4378             IF p_source_version_type='COST' THEN
4379 
4380                 IF p_target_version_type='COST' THEN
4381 
4382                     FORALL kk IN 1..l_bl_id_tbl.COUNT
4383 
4384                         UPDATE pa_budget_lines
4385                         SET    txn_cost_rate_override    = DECODE(cost_rejection_code, NULL,l_raw_cost_override_rate_tbl(kk),txn_cost_rate_override),
4386                                burden_cost_rate_override = DECODE(burden_rejection_code, NULL,l_burd_cost_override_rate_tbl(kk),burden_cost_rate_override)
4387                         WHERE  budget_line_id = l_bl_id_tbl(kk);
4388 
4389                 ELSIF p_target_version_type='ALL' THEN
4390 
4391                     FORALL kk IN 1..l_bl_id_tbl.COUNT
4392 
4393                         UPDATE pa_budget_lines
4394                         SET    txn_cost_rate_override    = DECODE(cost_rejection_code, NULL,l_raw_cost_override_rate_tbl(kk),txn_cost_rate_override),
4395                                burden_cost_rate_override = DECODE(burden_rejection_code, NULL,l_burd_cost_override_rate_tbl(kk),burden_cost_rate_override),
4396                                txn_revenue               = DECODE(revenue_rejection_code, NULL,l_txn_revenue_tbl(kk),txn_revenue),
4397                                txn_bill_rate_override    = DECODE(revenue_rejection_code, NULL,l_bill_override_rate_tbl(kk),txn_bill_rate_override)
4398                         WHERE  budget_line_id = l_bl_id_tbl(kk);
4399 
4400                 END IF;
4401 
4402             ELSIF p_source_version_type='REVENUE' THEN
4403 
4404                 IF p_target_version_type='REVENUE' THEN
4405 
4406                     FORALL kk IN 1..l_bl_id_tbl.COUNT
4407 
4408                         UPDATE pa_budget_lines
4409                         SET    txn_bill_rate_override    = DECODE(revenue_rejection_code, NULL,l_bill_override_rate_tbl(kk),txn_bill_rate_override)
4410                         WHERE  budget_line_id = l_bl_id_tbl(kk);
4411 
4412                 ELSIF p_target_version_type='ALL' THEN
4413 
4414                     FORALL kk IN 1..l_bl_id_tbl.COUNT
4415 
4416                         UPDATE pa_budget_lines
4417                         SET    quantity                  = l_quantity_tbl(kk),
4418 			       txn_raw_cost              = DECODE(cost_rejection_code, NULL,l_txn_raw_cost_tbl(kk),txn_raw_cost),
4419                                txn_burdened_cost         = DECODE(burden_rejection_code, NULL,l_txn_burdened_cost_tbl(kk),txn_burdened_cost),
4420                                txn_cost_rate_override    = DECODE(cost_rejection_code, NULL,l_raw_cost_override_rate_tbl(kk),txn_cost_rate_override),
4421                                burden_cost_rate_override = DECODE(burden_rejection_code, NULL,l_burd_cost_override_rate_tbl(kk),burden_cost_rate_override),
4422                                txn_bill_rate_override    = DECODE(revenue_rejection_code, NULL,l_bill_override_rate_tbl(kk),txn_bill_rate_override)
4423                         WHERE  budget_line_id = l_bl_id_tbl(kk);
4424 
4425                 END IF;
4426 
4427             ELSIF p_source_version_type='ALL' THEN
4428 
4429                 --In case of an All Forecast version, there can be budget lines with actuals in which revenue
4430                 --alone is populated and other amounts are null. When these budget lines are copied into a
4431                 --version which is not an All Forecats version then the corresponding resource assignments in the
4432                 --target will be made non rate based. The below DML updates all such resource assignments to non rate based
4433 
4434                 --Update the rate based flag to N for the ids in l_non_rb_ra_id_tbl
4435                 FORALL kk IN 1..l_non_rb_ra_id_tbl.COUNT
4436 
4437                     UPDATE pa_resource_assignments
4438                     SET    rate_based_flag = 'N'
4439                           ,unit_of_measure = 'DOLLARS'
4440                     WHERE  resource_assignment_id=l_non_rb_ra_id_tbl(kk);
4441 
4442                 --The pa_fp_upgrade_pkg.Apply_Calculate_FPM_Rules API will return a pl-sql tbl of RA ids which can
4443                 --be made non rate based. Note that this might not be equal in length to the input l_ra_id_tbl to that API
4444                 --The below block will prepare a pl-sql tbl l_bl_rb_flag_chg_tbl which will be equal in length to l_ra_id_tbl
4445                 --This tbl will have Y if the ra id is made non rate based . Otherwise it will contain N
4446                 l_bl_rb_flag_chg_tbl := SYSTEM.pa_varchar2_1_tbl_type();
4447                 IF p_src_plan_class_code = 'FORECAST' THEN
4448 
4449                     IF l_debug_mode='Y' THEN
4450 
4451                         FOR kk IN 1..l_non_rb_ra_id_tbl.COUNT LOOP
4452 
4453                             pa_debug.g_err_stage:= 'l_non_rb_ra_id_tbl('||kk||')'||l_non_rb_ra_id_tbl(kk);
4454                             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
4455 
4456                         END LOOP;
4457 
4458                         FOR kk IN 1..l_ra_id_tbl.COUNT LOOP
4459 
4460                             pa_debug.g_err_stage:= 'l_ra_id_tbl('||kk||')'||l_ra_id_tbl(kk);
4461                             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
4462                         END LOOP;
4463 
4464                     END IF;
4465 
4466                     l_temp:=1;
4467                     FOR kk IN 1..l_non_rb_ra_id_tbl.COUNT LOOP
4468 
4469                         LOOP
4470                             EXIT WHEN l_non_rb_ra_id_tbl(kk)= l_ra_id_tbl(l_temp);
4471                             l_bl_rb_flag_chg_tbl.extend;
4472                             l_bl_rb_flag_chg_tbl(l_temp):='N';
4473                             l_temp := l_temp+1;
4474                         END LOOP;
4475 
4476                         l_prev_ra_id := l_ra_id_tbl(l_temp);
4477                         LOOP
4478                             l_bl_rb_flag_chg_tbl.extend;
4479                             l_bl_rb_flag_chg_tbl(l_temp) := 'Y';
4480                             l_temp := l_temp + 1;
4481                             EXIT WHEN l_temp > l_ra_id_tbl.COUNT OR l_ra_id_tbl(l_temp) <> l_prev_ra_id;
4482 
4483                         END LOOP;
4484 
4485                     END LOOP;
4486 
4487                 END IF;
4488 
4489                 IF l_debug_mode='Y' THEN
4490 
4491                     pa_debug.g_err_stage:= 'l_bl_rb_flag_chg_tbl.COUNT '||l_bl_rb_flag_chg_tbl.COUNT;
4492                     pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
4493 
4494                 END IF;
4495 
4496                 FOR kk IN l_bl_rb_flag_chg_tbl.COUNT+1..l_ra_id_tbl.COUNT LOOP
4497 
4498                     l_bl_rb_flag_chg_tbl.extend;
4499                     l_bl_rb_flag_chg_tbl(kk):='N';
4500 
4501                 END LOOP;
4502 
4503                 IF l_debug_mode='Y' THEN
4504 
4505                     pa_debug.g_err_stage:= 'l_bl_rb_flag_chg_tbl.COUNT '||l_bl_rb_flag_chg_tbl.COUNT;
4506                     pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
4507 
4508                     pa_debug.g_err_stage:= 'l_ra_id_tbl.COUNT '||l_ra_id_tbl.COUNT;
4509                     pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
4510 
4511                     FOR kk IN 1..l_bl_rb_flag_chg_tbl.COUNT LOOP
4512 
4513                         pa_debug.g_err_stage:= 'l_bl_rb_flag_chg_tbl('||kk||')'||l_bl_rb_flag_chg_tbl(kk);
4514                         pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
4515                     END LOOP;
4516 
4517                 END IF;
4518 
4519 
4520                 --The below block will update the budget lines with missing amounts.
4521                 ----If the resource assignment assignment is made non rate based then the amount returned by the
4522                 ----Apply_Calculate_FPM_Rules will be stamped on budget lines. Also the rejection codes for that amount
4523                 ----will be NULLed out accordingly
4524                 IF p_target_version_type='COST' THEN
4525 
4526                     FORALL kk IN 1..l_bl_id_tbl.COUNT
4527 
4528                         UPDATE pa_budget_lines
4529                         SET    quantity                  = l_quantity_tbl(kk),
4530                                txn_raw_cost              = DECODE (l_bl_rb_flag_chg_tbl(kk),
4531                                                                    'Y',l_txn_raw_cost_tbl(kk),
4532                                                                    DECODE(cost_rejection_code,
4533                                                                           NULL,l_txn_raw_cost_tbl(kk),
4534                                                                           txn_raw_cost)),
4535                                txn_burdened_cost         = DECODE (l_bl_rb_flag_chg_tbl(kk),
4536                                                                    'Y',l_txn_burdened_cost_tbl(kk),
4537                                                                    DECODE(burden_rejection_code,
4538                                                                           NULL,l_txn_burdened_cost_tbl(kk),
4539                                                                           txn_burdened_cost)),
4540                                txn_cost_rate_override    = DECODE (l_bl_rb_flag_chg_tbl(kk),
4541                                                                    'Y',l_raw_cost_override_rate_tbl(kk),
4542                                                                    DECODE(cost_rejection_code,
4543                                                                           NULL,l_raw_cost_override_rate_tbl(kk),
4544                                                                           txn_cost_rate_override)),
4545                                burden_cost_rate_override = DECODE (l_bl_rb_flag_chg_tbl(kk),
4546                                                                    'Y',l_burd_cost_override_rate_tbl(kk),
4547                                                                    DECODE(burden_rejection_code,
4548                                                                           NULL,l_burd_cost_override_rate_tbl(kk),
4549                                                                           burden_cost_rate_override)),
4550                                cost_rejection_code       = DECODE (l_bl_rb_flag_chg_tbl(kk),
4551                                                                    'Y',NULL,
4552                                                                    cost_rejection_code),
4553                                burden_rejection_code     = DECODE (l_bl_rb_flag_chg_tbl(kk),
4554                                                                    'Y',NULL,
4555                                                                    burden_rejection_code)
4556                         WHERE  budget_line_id = l_bl_id_tbl(kk);
4557 
4558 
4559 
4560                 ELSIF p_target_version_type='REVENUE' THEN
4561 
4562                     FORALL kk IN 1..l_bl_id_tbl.COUNT
4563 
4564                         UPDATE pa_budget_lines
4565                         SET    quantity                 = l_quantity_tbl(kk),
4566                                txn_revenue              = DECODE (l_bl_rb_flag_chg_tbl(kk),
4567                                                                   'Y',l_txn_revenue_tbl(kk),
4568                                                                   DECODE(revenue_rejection_code,
4569                                                                          NULL,l_txn_revenue_tbl(kk),
4570                                                                          txn_revenue)),
4571                                txn_bill_rate_override   = DECODE (l_bl_rb_flag_chg_tbl(kk),
4572                                                                   'Y',l_bill_override_rate_tbl(kk),
4573                                                                   DECODE(revenue_rejection_code,
4574                                                                          NULL,l_bill_override_rate_tbl(kk),
4575                                                                          txn_bill_rate_override)),
4576                                revenue_rejection_code   = DECODE (l_bl_rb_flag_chg_tbl(kk),
4577                                                                   'Y',NULL,
4578                                                                   revenue_rejection_code)
4579                         WHERE  budget_line_id = l_bl_id_tbl(kk);
4580 
4581                 ELSIF p_target_version_type='ALL' THEN
4582 
4583                     FORALL kk IN 1..l_bl_id_tbl.COUNT
4584 
4585                         UPDATE pa_budget_lines
4586                         SET    quantity                  = l_quantity_tbl(kk),
4587                                txn_raw_cost              = DECODE (l_bl_rb_flag_chg_tbl(kk),
4588                                                                    'Y',l_txn_raw_cost_tbl(kk),
4589                                                                    DECODE(cost_rejection_code,
4590                                                                           NULL,l_txn_raw_cost_tbl(kk),
4591                                                                           txn_raw_cost)),
4592                                txn_burdened_cost         = DECODE (l_bl_rb_flag_chg_tbl(kk),
4593                                                                    'Y',l_txn_burdened_cost_tbl(kk),
4594                                                                    DECODE(burden_rejection_code,
4595                                                                           NULL,l_txn_burdened_cost_tbl(kk),
4596                                                                           txn_burdened_cost)),
4597                                txn_revenue              = DECODE (l_bl_rb_flag_chg_tbl(kk),
4598                                                                   'Y',l_txn_revenue_tbl(kk),
4599                                                                   DECODE(revenue_rejection_code,
4600                                                                          NULL,l_txn_revenue_tbl(kk),
4601                                                                          txn_revenue)),
4602                                txn_cost_rate_override    = DECODE (l_bl_rb_flag_chg_tbl(kk),
4603                                                                    'Y',l_raw_cost_override_rate_tbl(kk),
4604                                                                    DECODE(cost_rejection_code,
4605                                                                           NULL,l_raw_cost_override_rate_tbl(kk),
4606                                                                           txn_cost_rate_override)),
4607                                burden_cost_rate_override = DECODE (l_bl_rb_flag_chg_tbl(kk),
4608                                                                    'Y',l_burd_cost_override_rate_tbl(kk),
4609                                                                    DECODE(burden_rejection_code,
4610                                                                           NULL,l_burd_cost_override_rate_tbl(kk),
4611                                                                           burden_cost_rate_override)),
4612                                txn_bill_rate_override   = DECODE (l_bl_rb_flag_chg_tbl(kk),
4613                                                                   'Y',l_bill_override_rate_tbl(kk),
4614                                                                   DECODE(revenue_rejection_code,
4615                                                                          NULL,l_bill_override_rate_tbl(kk),
4616                                                                          txn_bill_rate_override)),
4617                                cost_rejection_code      = DECODE (l_bl_rb_flag_chg_tbl(kk),
4618                                                                    'Y',NULL,
4619                                                                    cost_rejection_code),
4620                                burden_rejection_code    = DECODE (l_bl_rb_flag_chg_tbl(kk),
4621                                                                    'Y',NULL,
4622                                                                    burden_rejection_code),
4623                                revenue_rejection_code   = DECODE (l_bl_rb_flag_chg_tbl(kk),
4624                                                                   'Y',NULL,
4625                                                                   revenue_rejection_code)
4626 
4627                         WHERE  budget_line_id = l_bl_id_tbl(kk);
4628 
4629                 END IF;
4630 
4631             END IF;--IF l_source_version_type='COST' THEN
4632 
4633         END IF;--IF p_derv_rates_missing_amts_flag='Y' THEN
4634 
4635         IF l_debug_mode='Y' THEN
4636 
4637             pa_debug.g_err_stage:= 'Leaving '||l_module_name;
4638             pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
4639 
4640             pa_debug.reset_curr_function;
4641         END IF;
4642 
4643     EXCEPTION
4644         WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
4645         l_msg_count := FND_MSG_PUB.count_msg;
4646         IF l_msg_count = 1 THEN
4647            PA_INTERFACE_UTILS_PUB.get_messages
4648                  (p_encoded        => FND_API.G_TRUE
4649                   ,p_msg_index      => 1
4650                   ,p_msg_count      => l_msg_count
4651                   ,p_msg_data       => l_msg_data
4652                   ,p_data           => l_data
4653                   ,p_msg_index_out  => l_msg_index_out);
4654 
4655            x_msg_data := l_data;
4656            x_msg_count := l_msg_count;
4657         ELSE
4658            x_msg_count := l_msg_count;
4659         END IF;
4660         x_return_status := FND_API.G_RET_STS_ERROR;
4661 
4662         IF l_debug_mode = 'Y' THEN
4663            pa_debug.g_err_stage:='Invalid Arguments Passed Or called api raised an error';
4664            pa_debug.write( l_module_name,pa_debug.g_err_stage,5);
4665         -- reset curr function
4666            pa_debug.reset_curr_function();
4667         END IF;
4668         RETURN;
4669         WHEN OTHERS THEN
4670         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4671         x_msg_count     := 1;
4672         x_msg_data      := SQLERRM;
4673 
4674         FND_MSG_PUB.add_exc_msg( p_pkg_name        => 'pa_fp_copy_from_pkg'
4675                                ,p_procedure_name  => 'derv_missing_amts_chk_neg_qty');
4676 
4677         IF l_debug_mode = 'Y' THEN
4678            pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
4679            pa_debug.write( l_module_name,pa_debug.g_err_stage,5);
4680         -- reset curr function
4681             pa_debug.reset_curr_function();
4682         END IF;
4683         RAISE;
4684 
4685     END derv_missing_amts_chk_neg_qty;
4686 
4687 
4688 
4689   /*=========================================================================
4690       This api inserts budget lines for target using source budget lines. If
4691       the adjustment percentage is zero, this api will copy from source to
4692       target version without modifying any amounts. If adjustment percentage is
4693       non-zero,then we don't copy project and project functional columns as
4694       these  need to be converted again and might cause rounding issues
4695       This is an overloaded procedure
4696 
4697 -- r11.5 FP.M Developement ----------------------------------
4698 --
4699 -- 08-JAN-04 jwhite          - Bug 3362316
4700 --                             Rewrote Copy_Budget_Lines
4701 --
4702     --Bug 4290043. Introduced the paramters p_copy_actuals_flag and p_derv_rates_missing_amts_flag.
4703     --These will be passed from copy_version API. p_copy_actuals_flag indicates whether to copy the
4704     --actuals from the source version or not. p_derv_rates_missing_amts_flag indicates whether the
4705     --target version contains missing amounts rates which should be derived after copy
4706    =========================================================================*/
4707 
4708   PROCEDURE Copy_Budget_Lines(
4709              p_source_plan_version_id         IN  NUMBER
4710              ,p_target_plan_version_id        IN  NUMBER
4711              ,p_adj_percentage                IN  NUMBER
4712              ,p_copy_actuals_flag             IN  VARCHAR2
4713              ,p_derv_rates_missing_amts_flag  IN  VARCHAR2
4714              ,x_return_status                 OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
4715              ,x_msg_count                     OUT NOCOPY NUMBER --File.Sql.39 bug 4440895
4716              ,x_msg_data                      OUT NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
4717    AS
4718 
4719          l_msg_count          NUMBER :=0;
4720          l_data               VARCHAR2(2000);
4721          l_msg_data           VARCHAR2(2000);
4722          l_error_msg_code     VARCHAR2(2000);
4723          l_msg_index_out      NUMBER;
4724          l_return_status      VARCHAR2(2000);
4725          l_debug_mode         VARCHAR2(30);
4726 
4727          l_source_period_profile_id  pa_budget_versions.period_profile_id%TYPE;
4728          l_target_period_profile_id  pa_budget_versions.period_profile_id%TYPE;
4729 
4730          l_revenue_flag       pa_fin_plan_amount_sets.revenue_flag%type;
4731          l_cost_flag          pa_fin_plan_amount_sets.raw_cost_flag%type;
4732 
4733          l_adj_percentage            NUMBER ;
4734          l_period_profiles_same_flag VARCHAR2(1);
4735 
4736          l_src_plan_class_code      pa_fin_plan_types_b.plan_class_code%TYPE;
4737          l_trg_plan_class_code      pa_fin_plan_types_b.plan_class_code%TYPE;
4738          l_wp_version_flag      pa_budget_versions.wp_version_flag%TYPE;
4739 
4740          l_etc_start_date       pa_budget_versions.etc_start_date%TYPE;
4741          l_temp                 NUMBER;
4742 
4743          l_source_version_type    pa_budget_versions.version_type%TYPE;
4744          l_target_version_type    pa_budget_versions.version_type%TYPE;
4745          l_source_project_id      pa_budget_versions.project_id%TYPE; -- Bug 4493425
4746          l_target_project_id      pa_budget_versions.project_id%TYPE; -- Bug 4493425
4747 
4748          -- Bug 4493425: Added pbv.project_id in the select.
4749          CURSOR get_plan_class_code_csr(c_budget_version_id pa_budget_versions.budget_version_id%TYPE) IS
4750          SELECT pfb.plan_class_code,nvl(pbv.wp_version_flag,'N'),etc_start_date,pbv.version_type,pbv.project_id
4751          FROM   pa_fin_plan_types_b pfb,
4752                 pa_budget_versions  pbv
4753          WHERE  pbv.budget_version_id = c_budget_version_id
4754          AND    pbv.fin_plan_type_id  = pfb.fin_plan_type_id;
4755          -- Bug 3927244
4756 
4757          --Bug 4290043
4758          l_target_pref_code             pa_proj_fp_options.fin_plan_preference_code%TYPE;
4759 
4760 
4761    BEGIN
4762 
4763       x_msg_count := 0;
4764       x_return_status := FND_API.G_RET_STS_SUCCESS;
4765 IF P_PA_DEBUG_MODE = 'Y' THEN
4766       pa_debug.set_err_stack('PA_FP_COPY_FROM_PKG.Copy_Budget_Lines');
4767 END IF;
4768       fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
4769       l_debug_mode := NVL(l_debug_mode, 'Y');
4770 IF P_PA_DEBUG_MODE = 'Y' THEN
4771       pa_debug.set_process('PLSQL','LOG',l_debug_mode);
4772 END IF;
4773       -- Checking for all valid input parametrs
4774 
4775       IF P_PA_DEBUG_MODE = 'Y' THEN
4776           pa_debug.g_err_stage := 'Checking for valid parameters:';
4777           pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
4778       END IF;
4779 
4780       IF (p_source_plan_version_id IS NULL) OR
4781          (p_target_plan_version_id IS NULL)
4782       THEN
4783 
4784            IF P_PA_DEBUG_MODE = 'Y' THEN
4785                pa_debug.g_err_stage := 'Source_plan='||p_source_plan_version_id;
4786                pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,5);
4787                pa_debug.g_err_stage := 'Target_plan'||p_target_plan_version_id;
4788                pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,5);
4789            END IF;
4790 
4791            PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
4792                                 p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
4793 
4794            RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
4795 
4796       END IF;
4797 
4798       IF P_PA_DEBUG_MODE = 'Y' THEN
4799           pa_debug.g_err_stage := 'Parameter validation complete';
4800           pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
4801       END IF;
4802 /*
4803       pa_debug.g_err_stage:='Source fin plan version id'||p_source_plan_version_id;
4804       IF P_PA_DEBUG_MODE = 'Y' THEN
4805          pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
4806       END IF;
4807       pa_debug.g_err_stage:='Target fin plan version id'||p_target_plan_version_id;
4808       IF P_PA_DEBUG_MODE = 'Y' THEN
4809          pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
4810       END IF;
4811 */
4812       --make adj percentage zero if passed as null
4813 
4814       l_adj_percentage := NVL(p_adj_percentage,0);
4815 /*
4816        pa_debug.g_err_stage:='Adj_percentage'||l_adj_percentage;
4817        IF P_PA_DEBUG_MODE = 'Y' THEN
4818           pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
4819        END IF;
4820 */
4821        -- Fetching the flags of target version using fin_plan_prefernce_code
4822 
4823 
4824        IF P_PA_DEBUG_MODE = 'Y' THEN
4825            pa_debug.g_err_stage:='Fetching the raw_cost,burdened_cost and revenue flags of target_version';
4826            pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
4827        END IF;
4828 
4829        --Bug 4290043. Selected preference code for the target version
4830        SELECT DECODE(fin_plan_preference_code          -- l_revenue_flag
4831                        ,PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY ,'Y'
4832                        ,PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME ,'Y','N')
4833               ,DECODE(fin_plan_preference_code          -- l_cost_flag
4834                       ,PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY ,'Y'
4835                       ,PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME , 'Y','N')
4836               ,fin_plan_preference_code
4837        INTO   l_revenue_flag
4838               ,l_cost_flag
4839               ,l_target_pref_code
4840        FROM   pa_proj_fp_options
4841        WHERE  fin_plan_version_id=p_target_plan_version_id;
4842 /*
4843        pa_debug.g_err_stage:='l_revenue_flag ='||l_revenue_flag;
4844        IF P_PA_DEBUG_MODE = 'Y' THEN
4845           pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
4846        END IF;
4847        pa_debug.g_err_stage:='l_cost_flag ='||l_cost_flag;
4848        IF P_PA_DEBUG_MODE = 'Y' THEN
4849           pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
4850        END IF;
4851 */
4852        -- Checking if source and target version period profiles match
4853 
4854        /* FPB2: REVIEW */
4855 
4856 
4857        IF P_PA_DEBUG_MODE = 'Y' THEN
4858            pa_debug.g_err_stage:='Inserting  budget_lines';
4859            pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
4860        END IF;
4861 
4862        -- Bug 3927244: Actuals need to be copied from forecast to forecast within the same project for FINPLAN versions
4863 
4864        -- Bug 4493425: Added l_source_project_id in the INTO clause.
4865        OPEN  get_plan_class_code_csr(p_source_plan_version_id);
4866        FETCH get_plan_class_code_csr
4867        INTO  l_src_plan_class_code,l_wp_version_flag,l_etc_start_date,l_source_version_type,l_source_project_id;
4868        CLOSE get_plan_class_code_csr;
4869 
4870        -- Bug 4493425: Added l_target_project_id in the INTO clause.
4871        OPEN  get_plan_class_code_csr(p_target_plan_version_id);
4872        FETCH get_plan_class_code_csr
4873        INTO  l_trg_plan_class_code,l_wp_version_flag,l_etc_start_date,l_target_version_type,l_target_project_id;
4874        CLOSE get_plan_class_code_csr;
4875 
4876 
4877        -- Bug 3362316, 08-JAN-2003: Added New FP.M Columns  --------------------------
4878        --Bug 4052403. For non rate-based transactions quantity should be same as raw cost if the version type is COST/ALL or
4879        --it should be revenue if the version type is REVENUE. This business rule will be taken care by the API
4880        --PA_FP_MULTI_CURRENCY_PKG.Round_Budget_Line_Amounts which is called after this INSERT. Note that this has to be done only
4881        --when adjustment% is not null since the amounts in the source will be altered only when the user enters some adj %
4882        --Bug 4188225. PC/PFC buckets will be copied unconditionally (Removed the condition that checks for l_adj_percentage
4883        --being greater than 0 in order to copy)
4884 
4885        --Bug 4290043.Used p_copy_actuals_flag and p_derv_rates_missing_amts_flag to decide on copying actuals/rates
4886        --If p_derv_rates_missing_amts_flag is Y then rates will not be copied and the derv_missing_amts_chk_neg_qty
4887        --API called later on will stamp them on budget lines
4888        --Display_quantity is being set in copy_version and copy_finplans_from_project api as well
4889        INSERT INTO PA_BUDGET_LINES(
4890                 budget_line_id             /* FPB2 */
4891                ,budget_version_id          /* FPB2 */
4892                ,resource_assignment_id
4893                ,start_date
4894                ,last_update_date
4895                ,last_updated_by
4896                ,creation_date
4897                ,created_by
4898                ,last_update_login
4899                ,end_date
4900                ,period_name
4901                ,quantity
4902                ,display_quantity   --IPM Arch Enhancement Bug 4865563.
4903                ,raw_cost
4904                ,burdened_cost
4905                ,revenue
4906                ,change_reason_code
4907                ,description
4908                ,attribute_category
4909                ,attribute1
4910                ,attribute2
4911                ,attribute3
4912                ,attribute4
4913                ,attribute5
4914                ,attribute6
4915                ,attribute7
4916                ,attribute8
4917                ,attribute9
4918                ,attribute10
4919                ,attribute11
4920                ,attribute12
4921                ,attribute13
4922                ,attribute14
4923                ,attribute15
4924                ,raw_cost_source
4925                ,burdened_cost_source
4926                ,quantity_source
4927                ,revenue_source
4928                ,pm_product_code
4929                ,pm_budget_line_reference
4930                ,cost_rejection_code
4931                ,revenue_rejection_code
4932                ,burden_rejection_code
4933                ,other_rejection_code
4934                ,code_combination_id
4935                ,ccid_gen_status_code
4936                ,ccid_gen_rej_message
4937                ,request_id
4938                ,borrowed_revenue
4939                ,tp_revenue_in
4940                ,tp_revenue_out
4941                ,revenue_adj
4942                ,lent_resource_cost
4943                ,tp_cost_in
4944                ,tp_cost_out
4945                ,cost_adj
4946                ,unassigned_time_cost
4947                ,utilization_percent
4948                ,utilization_hours
4949                ,utilization_adj
4950                ,capacity
4951                ,head_count
4952                ,head_count_adj
4953                ,projfunc_currency_code
4954                ,projfunc_cost_rate_type
4955                ,projfunc_cost_exchange_rate
4956                ,projfunc_cost_rate_date_type
4957                ,projfunc_cost_rate_date
4958                ,projfunc_rev_rate_type
4959                ,projfunc_rev_exchange_rate
4960                ,projfunc_rev_rate_date_type
4961                ,projfunc_rev_rate_date
4962                ,project_currency_code
4963                ,project_cost_rate_type
4964                ,project_cost_exchange_rate
4965                ,project_cost_rate_date_type
4966                ,project_cost_rate_date
4967                ,project_raw_cost
4968                ,project_burdened_cost
4969                ,project_rev_rate_type
4970                ,project_rev_exchange_rate
4971                ,project_rev_rate_date_type
4972                ,project_rev_rate_date
4973                ,project_revenue
4974                ,txn_raw_cost
4975                ,txn_burdened_cost
4976                ,txn_currency_code
4977                ,txn_revenue
4978                ,bucketing_period_code
4979                ,transfer_price_rate
4980                ,init_quantity
4981                ,init_quantity_source
4982                ,init_raw_cost
4983                ,init_burdened_cost
4984                ,init_revenue
4985                ,init_raw_cost_source
4986                ,init_burdened_cost_source
4987                ,init_revenue_source
4988                ,project_init_raw_cost
4989                ,project_init_burdened_cost
4990                ,project_init_revenue
4991                ,txn_init_raw_cost
4992                ,txn_init_burdened_cost
4993                ,txn_init_revenue
4994                ,txn_markup_percent
4995                ,txn_markup_percent_override
4996                ,txn_discount_percentage
4997                ,txn_standard_bill_rate
4998                ,txn_standard_cost_rate
4999                ,txn_cost_rate_override
5000                ,burden_cost_rate
5001                ,txn_bill_rate_override
5002                ,burden_cost_rate_override
5003                ,cost_ind_compiled_set_id
5004                ,pc_cur_conv_rejection_code
5005                ,pfc_cur_conv_rejection_code
5006 )
5007      SELECT     pa_budget_lines_s.nextval      /* FPB2 */
5008                ,p_target_plan_version_id     /* FPB2 */
5009                ,pra.resource_assignment_id
5010                ,pbl.start_date
5011                ,sysdate
5012                ,fnd_global.user_id
5013                ,sysdate
5014                ,fnd_global.user_id
5015                ,fnd_global.login_id
5016                ,pbl.end_date
5017                ,pbl.period_name
5018                ,pbl.quantity
5019                ,pbl.display_quantity    --IPM Arch Enhancement Bug 4865563.
5020                ,DECODE(l_cost_flag,'Y', raw_cost,NULL)
5021                ,DECODE(l_cost_flag,'Y', burdened_cost,NULL)
5022                ,DECODE(l_revenue_flag,'Y', revenue,NULL)
5023                ,pbl.change_reason_code
5024                ,description
5025                ,pbl.attribute_category
5026                ,pbl.attribute1
5027                ,pbl.attribute2
5028                ,pbl.attribute3
5029                ,pbl.attribute4
5030                ,pbl.attribute5
5031                ,pbl.attribute6
5032                ,pbl.attribute7
5033                ,pbl.attribute8
5034                ,pbl.attribute9
5035                ,pbl.attribute10
5036                ,pbl.attribute11
5037                ,pbl.attribute12
5038                ,pbl.attribute13
5039                ,pbl.attribute14
5040                ,pbl.attribute15
5041                ,DECODE(l_cost_flag,'Y',PA_FP_CONSTANTS_PKG.G_AMOUNT_SOURCE_COPY_P,NULL) --raw_cost_souce
5042                ,DECODE(l_cost_flag,'Y',PA_FP_CONSTANTS_PKG.G_AMOUNT_SOURCE_COPY_P,NULL) --burdened_cost_source
5043                ,PA_FP_CONSTANTS_PKG.G_AMOUNT_SOURCE_COPY_P  --quantity_source
5044                ,DECODE(l_revenue_flag,'Y',PA_FP_CONSTANTS_PKG.G_AMOUNT_SOURCE_COPY_P,NULL) --revenue source
5045                ,pbl.pm_product_code
5046                ,pbl.pm_budget_line_reference
5047                ,DECODE(l_cost_flag, 'Y',cost_rejection_code, NULL)
5048                ,DECODE(l_revenue_flag, 'Y',revenue_rejection_code, NULL)
5049                ,DECODE(l_cost_flag,'Y',burden_rejection_code, NULL)
5050                ,other_rejection_code
5051                ,code_combination_id
5052                ,ccid_gen_status_code
5053                ,ccid_gen_rej_message
5054                ,fnd_global.conc_request_id
5055                ,borrowed_revenue
5056                ,tp_revenue_in
5057                ,tp_revenue_out
5058                ,revenue_adj
5059                ,lent_resource_cost
5060                ,tp_cost_in
5061                ,tp_cost_out
5062                ,cost_adj
5063                ,unassigned_time_cost
5064                ,utilization_percent
5065                ,utilization_hours
5066                ,utilization_adj
5067                ,capacity
5068                ,head_count
5069                ,head_count_adj
5070                ,projfunc_currency_code
5071                ,DECODE(l_cost_flag,'Y',projfunc_cost_rate_type,NULL)
5072                ,DECODE(l_cost_flag,'Y',projfunc_cost_exchange_rate,NULL)
5073                ,DECODE(l_cost_flag,'Y',projfunc_cost_rate_date_type,NULL)
5074                ,DECODE(l_cost_flag,'Y',projfunc_cost_rate_date,NULL)
5075                ,DECODE(l_revenue_flag,'Y',projfunc_rev_rate_type,NULL)
5076                ,DECODE(l_revenue_flag,'Y',projfunc_rev_exchange_rate,NULL)
5077                ,DECODE(l_revenue_flag,'Y',projfunc_rev_rate_date_type,NULL)
5078                ,DECODE(l_revenue_flag,'Y',projfunc_rev_rate_date,NULL)
5079                ,project_currency_code
5080                ,DECODE(l_cost_flag,'Y',project_cost_rate_type,NULL)
5081                ,DECODE(l_cost_flag,'Y',project_cost_exchange_rate,NULL)
5082                ,DECODE(l_cost_flag,'Y',project_cost_rate_date_type,NULL)
5083                ,DECODE(l_cost_flag,'Y',project_cost_rate_date,NULL)
5084                ,DECODE(l_cost_flag,'Y', project_raw_cost,NULL)
5085                ,DECODE(l_cost_flag,'Y', project_burdened_cost,NULL)
5086                ,DECODE(l_revenue_flag,'Y',project_rev_rate_type,NULL)
5087                ,DECODE(l_revenue_flag,'Y',project_rev_exchange_rate,NULL)
5088                ,DECODE(l_revenue_flag,'Y',project_rev_rate_date_type,NULL)
5089                ,DECODE(l_revenue_flag,'Y',project_rev_rate_date,NULL)
5090                ,DECODE(l_revenue_flag,'Y', project_revenue,NULL)
5091                ,DECODE(l_cost_flag,'Y',
5092                        decode(GREATEST(pbl.start_date,NVL(l_etc_start_date,pbl.start_date)),pbl.start_date,txn_raw_cost*(1+l_adj_percentage),txn_raw_cost),NULL)
5093                ,DECODE(l_cost_flag,'Y',
5094                        decode(GREATEST(pbl.start_date,NVL(l_etc_start_date,pbl.start_date)),pbl.start_date,txn_burdened_cost*(1+l_adj_percentage),txn_burdened_cost),NULL)
5095                ,txn_currency_code
5096                ,DECODE(l_revenue_flag,'Y',
5097                         decode(GREATEST(pbl.start_date,NVL(l_etc_start_date,pbl.start_date)),pbl.start_date,txn_revenue*(1+l_adj_percentage),txn_revenue),NULL)
5098                ,DECODE(l_period_profiles_same_flag,'Y',bucketing_period_code,NULL)
5099                ,transfer_price_rate
5100                ,decode(p_copy_actuals_flag,'N',NULL,pbl.init_quantity)              --init_quantity
5101                ,decode(p_copy_actuals_flag,'N',NULL,pbl.init_quantity_source)       --init_quantity_source
5102                ,DECODE(l_cost_flag,'Y',decode(p_copy_actuals_flag,'N',NULL,pbl.init_raw_cost),NULL)                   --init_raw_cost
5103                ,DECODE(l_cost_flag,'Y',decode(p_copy_actuals_flag,'N',NULL,pbl.init_burdened_cost),NULL)         --init_burdened_cost
5104                ,DECODE(l_revenue_flag,'Y',decode(p_copy_actuals_flag,'N',NULL,pbl.init_revenue),NULL)                     --init_revenue
5105                ,DECODE(l_cost_flag,'Y',decode(p_copy_actuals_flag,'N',NULL,pbl.init_raw_cost_source),NULL)            --init_raw_cost_source
5106                ,DECODE(l_cost_flag,'Y',decode(p_copy_actuals_flag,'N',NULL,pbl.init_burdened_cost_source),NULL)  --init_burdened_cost_source
5107                ,DECODE(l_revenue_flag,'Y',decode(p_copy_actuals_flag,'N',NULL,pbl.init_revenue_source),NULL)              --init_revenue_source
5108                ,DECODE(l_cost_flag,'Y',decode(p_copy_actuals_flag,'N',NULL,pbl.project_init_raw_cost),NULL)           --project_init_raw_cost
5109                ,DECODE(l_cost_flag,'Y',decode(p_copy_actuals_flag,'N',NULL,pbl.project_init_burdened_cost),NULL) --project_init_burdened_cost
5110                ,DECODE(l_revenue_flag,'Y',decode(p_copy_actuals_flag,'N',NULL,pbl.project_init_revenue),NULL)             --project_init_revenue
5111                ,DECODE(l_cost_flag,'Y',decode(p_copy_actuals_flag,'N',NULL,pbl.txn_init_raw_cost),NULL)               --txn_init_raw_cost
5112                ,DECODE(l_cost_flag,'Y',decode(p_copy_actuals_flag,'N',NULL,pbl.txn_init_burdened_cost),NULL)     --txn_init_burdened_cost
5113                ,DECODE(l_revenue_flag,'Y',decode(p_copy_actuals_flag,'N',NULL,pbl.txn_init_revenue),NULL)                 --txn_init_revenue
5114                ,txn_markup_percent
5115                ,txn_markup_percent_override
5116                ,txn_discount_percentage
5117                ,Decode(l_revenue_flag,'Y',DECODE(p_derv_rates_missing_amts_flag,'N',txn_standard_bill_rate,NULL),NULL) --txn_standard_bill_rate
5118                ,Decode(l_cost_flag,'Y',DECODE(p_derv_rates_missing_amts_flag,'N',txn_standard_cost_rate,NULL),NULL) --txn_standard_cost_rate
5119                ,Decode(l_cost_flag,'Y',DECODE(p_derv_rates_missing_amts_flag,'N',txn_cost_rate_override,NULL),NULL) --txn_cost_rate_override
5120                ,Decode(l_cost_flag,'Y',DECODE(p_derv_rates_missing_amts_flag,'N',burden_cost_rate,NULL),NULL)       --burden_cost_rate
5121                ,Decode(l_revenue_flag,'Y',DECODE(p_derv_rates_missing_amts_flag,'N',txn_bill_rate_override,NULL),NULL) --txn_bill_rate_override
5122                ,Decode(l_cost_flag,'Y',DECODE(p_derv_rates_missing_amts_flag,'N',burden_cost_rate_override,NULL),NULL) --burden_cost_rate_override
5123                ,cost_ind_compiled_set_id
5124                ,Decode(l_adj_percentage,0,pc_cur_conv_rejection_code,null)
5125                ,Decode(l_adj_percentage,0,pfc_cur_conv_rejection_code,null)
5126        FROM PA_BUDGET_LINES  pbl
5127             ,pa_resource_assignments pra
5128        WHERE pbl.resource_assignment_id = pra.parent_assignment_id
5129          AND pbl.budget_version_id = p_source_plan_version_id
5130          AND pra.budget_version_id = p_target_plan_version_id
5131          AND pra.project_id = l_target_project_id; -- Bug 4493425.
5132 
5133         l_temp:=SQL%ROWCOUNT;
5134 
5135        IF P_PA_DEBUG_MODE = 'Y' THEN
5136            pa_debug.g_err_stage:='No. of Budget lines inserted '||l_temp;
5137            pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
5138        END IF;
5139 
5140        --Populate the pa_fp_bl_map_tmp table so that the MRC API can have the mapping readily defined.
5141        --The logic of inserting into pa_budget_lines using pa_fp_bl_map_tmp is removed for bug  4224703
5142        --The below table need not be popluated for worplan versions since MRC is not maintained for
5143        --workplan versions
5144        IF l_wp_version_flag='N' THEN
5145 
5146            INSERT INTO pa_fp_bl_map_tmp
5147            (source_budget_line_id,
5148             target_budget_line_id)
5149             SELECT pbls.budget_line_id,
5150                    pblt.budget_line_id
5151             FROM   pa_budget_lines pblt,
5152                    pa_budget_lines pbls,
5153                    pa_resource_assignments prat
5154             WHERE  pblt.budget_version_id=p_target_plan_version_id
5155             AND    prat.budget_version_id=p_target_plan_version_id
5156             AND    prat.project_id = l_target_project_id -- Bug 4493425.
5157             AND    prat.resource_assignment_id=pblt.resource_assignment_id
5158             AND    prat.parent_assignment_id=pbls.resource_assignment_id
5159             AND    pblt.start_date=pbls.start_date
5160             AND    pblt.txn_currency_code=pbls.txn_currency_code;
5161             l_temp:=SQL%ROWCOUNT;
5162 
5163             IF P_PA_DEBUG_MODE = 'Y' THEN
5164                 pa_debug.g_err_stage:='No. of mrc mappling lines inserted '||l_temp;
5165                 pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
5166             END IF;
5167 
5168       END IF;
5169 
5170 
5171        -- End, Bug 3362316, 08-JAN-2003: Added New FP.M Columns  --------------------------
5172 
5173        -- Bug 4035856 Call rounding api if l_adj_percentage is not zero
5174        IF l_adj_percentage <> 0 THEN
5175             PA_FP_MULTI_CURRENCY_PKG.Round_Budget_Line_Amounts
5176                    (  p_budget_version_id     => p_target_plan_version_id
5177                      ,p_calling_context       => 'COPY_VERSION'
5178                      ,x_return_status         => l_return_status
5179                      ,x_msg_count             => l_msg_count
5180                      ,x_msg_data              => l_msg_data);
5181 
5182              IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
5183                  IF P_PA_debug_mode = 'Y' THEN
5184                       pa_debug.g_err_stage:= 'Error in PA_FP_MULTI_CURRENCY_PKG.Round_Budget_Line_Amounts';
5185                       pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,5);
5186                  END IF;
5187                  RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5188              END IF;
5189        END IF;
5190 
5191        --Bug 4290043. Call the API to correct the missing amounts in the target version in case it can have
5192        --missing amounts/rates
5193        IF p_derv_rates_missing_amts_flag = 'Y' OR
5194           l_adj_percentage <> 0 THEN
5195 
5196 
5197             derv_missing_amts_chk_neg_qty
5198             (p_budget_version_id            => p_target_plan_version_id,
5199              p_targ_pref_code               => l_target_pref_code,
5200              p_source_version_type          => l_source_version_type,
5201              p_target_version_type          => l_target_version_type,
5202              p_src_plan_class_code          => l_src_plan_class_code,
5203              p_derv_rates_missing_amts_flag => p_derv_rates_missing_amts_flag,
5204              p_adj_percentage               => l_adj_percentage,
5205              x_return_status                => l_return_status,
5206              x_msg_count                    => l_msg_count,
5207              x_msg_data                     => l_msg_data);
5208 
5209              IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
5210                  IF P_PA_debug_mode = 'Y' THEN
5211                       pa_debug.g_err_stage:= 'Error in derv_missing_amts_chk_neg_qty';
5212                       pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,5);
5213                  END IF;
5214                  RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5215 
5216             END IF;
5217 
5218        END IF;--IF p_derv_rates_missing_amts_flag = 'Y' THEN
5219 
5220        IF P_PA_DEBUG_MODE = 'Y' THEN
5221            pa_debug.g_err_stage:='Exiting Copy_Budget_Lines';
5222            pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
5223            pa_debug.reset_err_stack;    -- bug:- 2815593
5224 	END IF;
5225   EXCEPTION
5226 
5227       WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
5228              l_msg_count := FND_MSG_PUB.count_msg;
5229              IF l_msg_count = 1 THEN
5230                PA_INTERFACE_UTILS_PUB.get_messages
5231                    (p_encoded        => FND_API.G_TRUE
5232                     ,p_msg_index      => 1
5233                     ,p_msg_count      => l_msg_count
5234                     ,p_msg_data       => l_msg_data
5235                     ,p_data           => l_data
5236                     ,p_msg_index_out  => l_msg_index_out);
5237                x_msg_data := l_data;
5238                x_msg_count := l_msg_count;
5239            ELSE
5240                x_msg_count := l_msg_count;
5241            END IF;
5242            x_return_status := FND_API.G_RET_STS_ERROR;
5243 
5244            IF P_PA_DEBUG_MODE = 'Y' THEN
5245                pa_debug.g_err_stage:='Invalid arguments passed';
5246                pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,5);
5247                 pa_debug.reset_err_stack;
5248 	END IF;
5249            RAISE;
5250 
5251     WHEN Others THEN
5252         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5253         x_msg_count     := 1;
5254         x_msg_data      := SQLERRM;
5255         FND_MSG_PUB.add_exc_msg( p_pkg_name=> 'PA_FP_COPY_FROM_PKG'
5256                             ,p_procedure_name  => 'COPY_BUDGET_LINES');
5257 
5258         IF P_PA_DEBUG_MODE = 'Y' THEN
5259             pa_debug.g_err_stage:='Unexpected error'||SQLERRM;
5260             pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,6);
5261             pa_debug.reset_err_stack;
5262 	END IF;
5263         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5264  END Copy_Budget_Lines;
5265 
5266 /*===========================================================================
5267   This api copies proj period denorm res_assignment records from source
5268   version to target version.This api would be called only when the adjustment
5269   percentage is zero and period profile ids of the source and target versions
5270   are same.
5271  ==========================================================================*/
5272 
5273  PROCEDURE Copy_Periods_Denorm(
5274            p_source_plan_version_id   IN NUMBER
5275            ,p_target_plan_version_id  IN NUMBER
5276            ,p_calling_module          IN VARCHAR2
5277            ,x_return_status           OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
5278            ,x_msg_count               OUT NOCOPY NUMBER --File.Sql.39 bug 4440895
5279            ,x_msg_data                OUT NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
5280  AS
5281 
5282        l_msg_count          NUMBER :=0;
5283        l_data               VARCHAR2(2000);
5284        l_msg_data           VARCHAR2(2000);
5285        l_error_msg_code     VARCHAR2(2000);
5286        l_msg_index_out      NUMBER;
5287        l_return_status      VARCHAR2(2000);
5288        l_debug_mode         VARCHAR2(30);
5289        l_ignore_amount_type pa_proj_periods_denorm.amount_type_code%TYPE;
5290        l_target_project_id  pa_projects.project_id%TYPE;
5291 
5292  BEGIN
5293 
5294     x_msg_count := 0;
5295     x_return_status := FND_API.G_RET_STS_SUCCESS;
5296 IF P_PA_DEBUG_MODE = 'Y' THEN
5297     pa_debug.set_err_stack('PA_FP_COPY_FROM_PKG.Copy_Periods_Denorm');
5298 END IF;
5299     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
5300     l_debug_mode := NVL(l_debug_mode, 'Y');
5301 IF P_PA_DEBUG_MODE = 'Y' THEN
5302     pa_debug.set_process('PLSQL','LOG',l_debug_mode);
5303 END IF;
5304     -- Checking for all valid input parametrs
5305 
5306 
5307     IF P_PA_DEBUG_MODE = 'Y' THEN
5308         pa_debug.g_err_stage := 'Checking for valid parameters:';
5309         pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
5310     END IF;
5311 
5312     IF (p_source_plan_version_id IS NULL) OR
5313        (p_target_plan_version_id IS NULL) OR
5314        (p_calling_module IS NULL)
5315     THEN
5316 
5317 
5318          IF P_PA_DEBUG_MODE = 'Y' THEN
5319              pa_debug.g_err_stage := 'Source_plan='||p_source_plan_version_id;
5320              pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,5);
5321              pa_debug.g_err_stage := 'Target_plan = '||p_target_plan_version_id;
5322              pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,5);
5323              pa_debug.g_err_stage := 'Calling_module = '||p_calling_module;
5324              pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,5);
5325          END IF;
5326 
5327          PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
5328                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
5329 
5330          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5331 
5332     END IF;
5333 
5334     IF P_PA_DEBUG_MODE = 'Y' THEN
5335         pa_debug.g_err_stage := 'Parameter validation complete';
5336         pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
5337     END IF;
5338 /*
5339     pa_debug.g_err_stage := 'Source_plan='||p_source_plan_version_id;
5340     IF P_PA_DEBUG_MODE = 'Y' THEN
5341        pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
5342     END IF;
5343     pa_debug.g_err_stage := 'Target_plan = '||p_target_plan_version_id;
5344     IF P_PA_DEBUG_MODE = 'Y' THEN
5345        pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
5346     END IF;
5347     pa_debug.g_err_stage := 'Calling_module = '||p_calling_module;
5348     IF P_PA_DEBUG_MODE = 'Y' THEN
5349        pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
5350     END IF;
5351 */
5352       -- Evaluating which records are to be copied using amount type code of
5353       -- pa_proj_periods_denorm and fin paln preference code of the target  version
5354 
5355      IF P_PA_DEBUG_MODE = 'Y' THEN
5356          pa_debug.g_err_stage:='Evaluating which records are to be copied';
5357          pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
5358      END IF;
5359 
5360      SELECT DECODE(fin_plan_preference_code
5361                    ,PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY ,PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE
5362                    ,PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY ,PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST
5363                    ,PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME ,'-99') --copy both cost and revenue
5364             ,project_id
5365      INTO   l_ignore_amount_type
5366             ,l_target_project_id
5367      FROM   pa_proj_fp_options
5368      WHERE  fin_plan_version_id=p_target_plan_version_id;
5369 /*
5370      pa_debug.g_err_stage:='l_ignore_amount_type = '||l_ignore_amount_type;
5371      IF P_PA_DEBUG_MODE = 'Y' THEN
5372         pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
5373      END IF;
5374      pa_debug.g_err_stage := 'l_target_project_id = '||l_target_project_id;
5375      IF P_PA_DEBUG_MODE = 'Y' THEN
5376         pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
5377      END IF;
5378 */
5379       INSERT INTO PA_PROJ_PERIODS_DENORM(
5380                 budget_version_id
5381                 ,project_id
5382                 ,resource_assignment_id
5383                 ,object_id
5384                 ,object_type_code
5385                 ,period_profile_id
5386                 ,amount_type_code
5387                 ,amount_subtype_code
5388                 ,amount_type_id
5389                 ,amount_subtype_id
5390                 ,currency_type
5391                 ,currency_code
5392                 ,preceding_periods_amount
5393                 ,succeeding_periods_amount
5394                 ,prior_period_amount
5395                 ,period_amount1
5396                 ,period_amount2
5397                 ,period_amount3
5398                 ,period_amount4
5399                 ,period_amount5
5400                 ,period_amount6
5401                 ,period_amount7
5402                 ,period_amount8
5403                 ,period_amount9
5404                 ,period_amount10
5405                 ,period_amount11
5406                 ,period_amount12
5407                 ,period_amount13
5408                 ,period_amount14
5409                 ,period_amount15
5410                 ,period_amount16
5411                 ,period_amount17
5412                 ,period_amount18
5413                 ,period_amount19
5414                 ,period_amount20
5415                 ,period_amount21
5416                 ,period_amount22
5417                 ,period_amount23
5418                 ,period_amount24
5419                 ,period_amount25
5420                 ,period_amount26
5421                 ,period_amount27
5422                 ,period_amount28
5423                 ,period_amount29
5424                 ,period_amount30
5425                 ,period_amount31
5426                 ,period_amount32
5427                 ,period_amount33
5428                 ,period_amount34
5429                 ,period_amount35
5430                 ,period_amount36
5431                 ,period_amount37
5432                 ,period_amount38
5433                 ,period_amount39
5434                 ,period_amount40
5435                 ,period_amount41
5436                 ,period_amount42
5437                 ,period_amount43
5438                 ,period_amount44
5439                 ,period_amount45
5440                 ,period_amount46
5441                 ,period_amount47
5442                 ,period_amount48
5443                 ,period_amount49
5444                 ,period_amount50
5445                 ,period_amount51
5446                 ,period_amount52
5447                 ,last_update_date
5448                 ,last_updated_by
5449                 ,creation_date
5450                 ,created_by
5451                 ,last_update_login
5452                 ,parent_assignment_id)
5453      SELECT     p_target_plan_version_id   --budget_version_id
5454                 ,l_target_project_id       --project_id
5455                 ,pfrmt.target_res_assignment_id  --resource_assignment_id
5456 /* Bug# 2677867 - Object_id shoudl always be res assgnmnt id irrespect of FP or ORG_FCST
5457                 ,DECODE(p_calling_module
5458                         ,PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_ORG_FORECAST ,pfrmt.target_res_assignment_id
5459                         ,PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_FIN_PLAN , -1)    --object_id
5460 */
5461                 ,pfrmt.target_res_assignment_id
5462                 ,PA_FP_CONSTANTS_PKG.G_OBJECT_TYPE_RES_ASSIGNMENT    --object_type_code
5463                 ,period_profile_id
5464                 ,amount_type_code
5465                 ,amount_subtype_code
5466                 ,amount_type_id
5467                 ,amount_subtype_id
5468                 ,currency_type
5469                 ,currency_code
5470                 ,preceding_periods_amount
5471                 ,succeeding_periods_amount
5472                 ,prior_period_amount
5473                 ,period_amount1
5474                 ,period_amount2
5475                 ,period_amount3
5476                 ,period_amount4
5477                 ,period_amount5
5478                 ,period_amount6
5479                 ,period_amount7
5480                 ,period_amount8
5481                 ,period_amount9
5482                 ,period_amount10
5483                 ,period_amount11
5484                 ,period_amount12
5485                 ,period_amount13
5486                 ,period_amount14
5487                 ,period_amount15
5488                 ,period_amount16
5489                 ,period_amount17
5490                 ,period_amount18
5491                 ,period_amount19
5492                 ,period_amount20
5493                 ,period_amount21
5494                 ,period_amount22
5495                 ,period_amount23
5496                 ,period_amount24
5497                 ,period_amount25
5498                 ,period_amount26
5499                 ,period_amount27
5500                 ,period_amount28
5501                 ,period_amount29
5502                 ,period_amount30
5503                 ,period_amount31
5504                 ,period_amount32
5505                 ,period_amount33
5506                 ,period_amount34
5507                 ,period_amount35
5508                 ,period_amount36
5509                 ,period_amount37
5510                 ,period_amount38
5511                 ,period_amount39
5512                 ,period_amount40
5513                 ,period_amount41
5514                 ,period_amount42
5515                 ,period_amount43
5516                 ,period_amount44
5517                 ,period_amount45
5518                 ,period_amount46
5519                 ,period_amount47
5520                 ,period_amount48
5521                 ,period_amount49
5522                 ,period_amount50
5523                 ,period_amount51
5524                 ,period_amount52
5525                 ,sysdate
5526                 ,fnd_global.user_id
5527                 ,sysdate
5528                 ,fnd_global.user_id
5529                 ,fnd_global.login_id
5530                 ,pfrmt.parent_assignment_id --parent_assignment_id
5531      FROM    PA_PROJ_PERIODS_DENORM pppd
5532              ,PA_FP_RA_MAP_TMP  pfrmt
5533      WHERE   budget_version_id = p_source_plan_version_id
5534        AND   pppd.resource_assignment_id = pfrmt.source_res_assignment_id
5535        AND   pppd.object_type_code = PA_FP_CONSTANTS_PKG.G_OBJECT_TYPE_RES_ASSIGNMENT
5536        AND   pppd.amount_type_code <> l_ignore_amount_type;
5537 
5538      IF P_PA_DEBUG_MODE = 'Y' THEN
5539          pa_debug.g_err_stage:='Exiting Copy_Periods_Denorm';
5540          pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
5541          pa_debug.reset_err_stack;  --bug:- 2815593
5542 	END IF;
5543   EXCEPTION
5544 
5545      WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
5546          l_msg_count := FND_MSG_PUB.count_msg;
5547          IF l_msg_count = 1 THEN
5548              PA_INTERFACE_UTILS_PUB.get_messages
5549                  (p_encoded        => FND_API.G_TRUE
5550                   ,p_msg_index      => 1
5551                   ,p_msg_count      => l_msg_count
5552                   ,p_msg_data       => l_msg_data
5553                   ,p_data           => l_data
5554                   ,p_msg_index_out  => l_msg_index_out);
5555              x_msg_data := l_data;
5556              x_msg_count := l_msg_count;
5557          ELSE
5558              x_msg_count := l_msg_count;
5559          END IF;
5560          x_return_status := FND_API.G_RET_STS_ERROR;
5561 
5562          IF P_PA_DEBUG_MODE = 'Y' THEN
5563              pa_debug.g_err_stage:='Invalid arguments passed';
5564              pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,5);
5565              pa_debug.reset_err_stack;
5566 	END IF;
5567          RAISE;
5568 
5569     WHEN Others THEN
5570         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5571         x_msg_count     := 1;
5572         x_msg_data      := SQLERRM;
5573         FND_MSG_PUB.add_exc_msg( p_pkg_name=> 'PA_FP_COPY_FROM_PKG'
5574                                  ,p_procedure_name  => 'Copy_Periods_Denorm');
5575 
5576         IF P_PA_DEBUG_MODE = 'Y' THEN
5577             pa_debug.g_err_stage:='Unexpected error'||SQLERRM;
5578             pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,6);
5579             pa_debug.reset_err_stack;
5580         END IF;
5581         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5582 
5583  END Copy_Periods_Denorm;
5584 
5585 
5586 
5587 
5588 /*===================================================================
5589   This api copies all the budgets from source project to target
5590   project.this api only take care of the non upgraded budget versions
5591   and is based on the existing logic in pa_project_core1.copy_project.
5592   The output error parameters are designed to be in sync with existing
5593   code in pa_project_core1.copy_project
5594 ===================================================================*/
5595 
5596 PROCEDURE Copy_Budgets_From_Project(
5597                    p_from_project_id        IN  NUMBER
5598                    ,p_to_project_id         IN  NUMBER
5599                    ,p_delta                 IN  NUMBER
5600                    ,p_orig_template_flag    IN  VARCHAR2
5601                    ,p_agreement_amount      IN  NUMBER   -- Added for bug 2986930
5602                    ,p_baseline_funding_flag IN  VARCHAR2 -- Added for bug 2986930
5603                    ,x_err_code              OUT NOCOPY NUMBER --File.Sql.39 bug 4440895
5604                    ,x_err_stage             OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
5605                    ,x_err_stack             OUT NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
5606 AS
5607 
5608     l_return_status             VARCHAR2(2000);
5609     l_msg_count                 NUMBER :=0;
5610     l_msg_data                  VARCHAR2(2000);
5611     x_delta                     NUMBER;
5612     x_new_project_id            pa_projects.project_id%TYPE;
5613     x_orig_project_id           pa_projects.project_id%TYPE;
5614     x_orig_template_flag        pa_projects.template_flag%TYPE;
5615     l_agreement_amount          NUMBER; -- Added for bug 2986930
5616     l_baseline_funding_flag     VARCHAR2(1); -- Added for bug 2986930
5617 BEGIN
5618 
5619   --Initialise the variables
5620 
5621   x_orig_project_id :=  p_from_project_id;
5622   x_new_project_id   :=  p_to_project_id;
5623   x_delta           :=  p_delta;
5624   x_orig_template_flag := p_orig_template_flag;
5625 
5626      /*=============================================================
5627        The following code has been moved from copy project
5628      =============================================================*/
5629 
5630    -- Copy budgets:  For each budget type, get baselined budget
5631    -- version id of the original project.  If baselined budget verion
5632    -- id is null, then get draft budget version id.  Copy the baselined
5633    -- budget or draft budget (if no baselined budget) of the original
5634    -- project into a new draft budget for the new project.
5635 
5636    --EH Changes
5637 
5638       BEGIN
5639         PA_BUDGET_FUND_PKG.Copy_Budgetary_Controls
5640                               (p_from_project_id    => x_orig_project_id,
5641                                p_to_project_id      => x_new_project_id,
5642                                x_return_status      => l_return_status,
5643                                x_msg_count          => l_msg_count,
5644                                x_msg_data           => l_msg_data);
5645 
5646           IF    (l_return_status <> 'S') Then
5647                   x_err_code := 725;
5648           --      x_err_stage := 'PA_NO_PROJ_CREATED';
5649                   x_err_stage := pa_project_core1.get_message_from_stack('PA_ERR_COPY_BUDGT_CONTRL');
5650 
5651                   x_err_stack   := x_err_stack||'->PA_BUDGET_FUND_PKG.Copy_Budgetary_Controls';
5652                  -- bug 3163280 rollback to copy_project;
5653                  return;
5654           END IF;
5655 
5656         EXCEPTION WHEN OTHERS THEN
5657 
5658              x_err_code := 725;
5659              x_err_stage := pa_project_core1.get_message_from_stack( null );
5660              IF x_err_stage IS NULL
5661              THEN
5662                 x_err_stage := 'API: '||'PA_BUDGET_FUND_PKG.Copy_Budgetary_Controls'||
5663                                  ' SQL error message: '||SUBSTR( SQLERRM,1,1900);
5664              END IF;
5665              -- bug 3163280 rollback to copy_project;
5666              return;
5667       END;
5668 
5669   DECLARE
5670         CURSOR agmt is
5671         SELECT 1
5672         FROM pa_project_fundings
5673         WHERE project_id = x_new_project_id;
5674 
5675         CURSOR c1 is
5676         /* Bug 3106741
5677            Reframed the select to use EXISTS instead of DISTINCT
5678         */
5679         SELECT t.budget_type_code
5680               ,t.budget_amount_code
5681           FROM  pa_budget_types t
5682          WHERE EXISTS ( SELECT 1
5683                           FROM pa_budget_versions v
5684                          WHERE v.project_id = x_orig_project_id
5685                            AND v.budget_type_code = t.budget_type_code)
5686         ORDER BY t.budget_type_code;
5687 
5688         c1_rec               c1%rowtype;
5689         x_budget_version_id  number;
5690         x_new_budget_ver_id  number;
5691         with_funding         number := 0;
5692         x_mark_as_original   varchar2(2);
5693 
5694   BEGIN
5695       l_baseline_funding_flag := NVL(p_baseline_funding_flag,'N'); -- Added for bug 2986930
5696       OPEN agmt;
5697            FETCH agmt into with_funding;
5698            IF agmt%notfound THEN
5699               with_funding := 0;
5700            END IF;
5701 
5702       CLOSE agmt;
5703 
5704       OPEN c1;
5705 
5706       LOOP
5707 
5708         x_err_stage := 'fetch budget type code';
5709 
5710         FETCH c1 INTO c1_rec;
5711         EXIT WHEN c1%notfound;
5712 
5713 /* Added the below if condition for bug 2986930 */
5714        --Bug 5378256: Prevent copy and baseline of AR budget when 'baseline funding without budget' is enabled for target.
5715        If NOT(l_baseline_funding_flag = 'Y' and c1_rec.budget_type_code = 'AR' )
5716        then
5717 
5718         x_budget_version_id := null;
5719         x_mark_as_original := 'N';
5720         -- get latest baselined version id of the original project
5721         -- with the budget type of c1_rec.budget_type_code
5722         x_err_stage :=
5723              'get latest baselined version id of project '||
5724               x_orig_project_id ||' with budget type of '||
5725               c1_rec.budget_type_code;
5726         --EH Changes
5727         BEGIN
5728               pa_budget_utils.get_baselined_version_id
5729                                 (x_project_id             => x_orig_project_id,
5730                                  x_budget_type_code       => c1_rec.budget_type_code,
5731                                  x_budget_version_id      => x_budget_version_id,
5732                                  x_err_code               => x_err_code,
5733                                  x_err_stage              => x_err_stage,
5734                                  x_err_stack              => x_err_stack);
5735 
5736 
5737               x_err_stage :=
5738                    'after get latest baselined version id of project '||
5739                     x_orig_project_id ||' with budget type of '||
5740                     c1_rec.budget_type_code;
5741 
5742               IF ( x_err_code < 0 ) THEN   -- Oracle error
5743                   x_err_code := 750;
5744                    IF x_err_stage IS NULL
5745                    THEN
5746                        x_err_stage := pa_project_core1.get_message_from_stack( 'PA_ERR_GET_BASLIN_VER_ID');
5747                    END IF;
5748                    x_err_stack := x_err_stack||'->pa_budget_utils.get_baselined_version_id';
5749                    -- bug 3163280 ROLLBACK TO copy_project;
5750                    RETURN;
5751               END IF;
5752         EXCEPTION WHEN OTHERS THEN
5753               x_err_code := 750;
5754 --              x_err_stage := pa_project_core1.get_message_from_stack( null );
5755               IF x_err_stage IS NULL
5756               THEN
5757                  x_err_stage := 'API: '||'pa_budget_utils.get_baselined_version_id'||
5758                                   ' SQL error message: '||SUBSTR( SQLERRM,1,1900);
5759               END IF;
5760               -- bug 3163280 rollback to copy_project;
5761               return;
5762         END;
5763 
5764         IF ( x_err_code = 0 ) THEN   -- got baselined budget
5765            x_mark_as_original := 'Y';
5766         END IF;
5767 
5768         IF (x_err_code = 10) THEN
5769            -- no baselined budget
5770            -- get draft version id of the original project
5771            -- with the budget type of c1_rec.budget_type_code
5772            x_err_stage :=
5773                'get draft version id of project '||
5774                 x_orig_project_id ||' with budget type of '||
5775                 c1_rec.budget_type_code;
5776            --EH Changes
5777            BEGIN
5778                 pa_budget_utils.get_draft_version_id
5779                                  (x_project_id             => x_orig_project_id,
5780                                   x_budget_type_code       => c1_rec.budget_type_code,
5781                                   x_budget_version_id      => x_budget_version_id,
5782                                   x_err_code               => x_err_code,
5783                                   x_err_stage              => x_err_stage,
5784                                   x_err_stack              => x_err_stack);
5785 
5786                 IF ( x_err_code < 0 ) THEN        -- Oracle error
5787                      x_err_code := 755;
5788                      IF x_err_stage IS NULL
5789                      THEN
5790                          x_err_stage := pa_project_core1.get_message_from_stack('PA_ERR_GET_DRFT_VER_ID');
5791                      END IF;
5792                      x_err_stack := x_err_stack||'->pa_budget_utils.get_draft_version_id';
5793                      -- bug 3163280 ROLLBACK TO copy_project;
5794                      RETURN;
5795                 END IF;
5796            EXCEPTION WHEN OTHERS THEN
5797                 x_err_code := 755;
5798            --  x_err_stage := pa_project_core1.get_message_from_stack( null );
5799                      IF x_err_stage IS NULL
5800                      THEN
5801                         x_err_stage := 'API: '||'pa_budget_utils.get_draft_version_id'||
5802                                          ' SQL error message: '||SUBSTR( SQLERRM,1,1900);
5803                      END IF;
5804                      -- bug 3163280 ROLLBACK TO copy_project;
5805                      RETURN;
5806            END;
5807         END IF;
5808 
5809         IF (x_err_code = 0) THEN
5810            IF (x_budget_version_id is not null) THEN
5811 
5812               -- copy budget for new project
5813                 x_err_stage := 'create draft budget for new project '||
5814                 x_new_project_id || ' with budget type of '||
5815                 c1_rec.budget_type_code;
5816               --EH Changes
5817 
5818               BEGIN
5819 
5820                   pa_budget_core.copy(
5821                          x_src_version_id          => x_budget_version_id,
5822                          x_amount_change_pct       => 1,
5823                          x_rounding_precision      => 5,
5824                          x_shift_days              => nvl(x_delta, 0),
5825                          x_dest_project_id         => x_new_project_id,
5826                          x_dest_budget_type_code   => c1_rec.budget_type_code,
5827                          x_err_code                => x_err_code,
5828                          x_err_stage               => x_err_stage,
5829                          x_err_stack               => x_err_stack );
5830 
5831                  if ( x_err_code > 0 or x_err_code < 0 ) then
5832                       x_err_code := 760;
5833                       IF x_err_stage IS NULL
5834                       THEN
5835                           x_err_stage := pa_project_core1.get_message_from_stack( 'PA_ERR_BUDGT_CORE_COPY');
5836                       END IF;
5837                       x_err_stack := x_err_stack||'->pa_budget_core.copy';
5838                       -- bug 3163280 rollback to copy_project;
5839                       return;         -- Application or Oracle error
5840                  end if;
5841               EXCEPTION WHEN OTHERS THEN
5842                  x_err_code := 760;
5843 --                 x_err_stage := pa_project_core1.get_message_from_stack( null );
5844                  IF x_err_stage IS NULL
5845                  THEN
5846                     x_err_stage := 'API: '||'pa_budget_core.copy'||
5847                                      ' SQL error message: '||SUBSTR( SQLERRM,1,1900);
5848                  END IF;
5849                  -- bug 3163280 rollback to copy_project;
5850                  return;
5851               END;
5852 
5853               IF NOT PA_BUDGET_FUND_PKG.Is_bdgt_intg_enabled (p_project_id => x_orig_project_id,p_mode => 'A') THEN
5854 
5855                      -- 1. Submit/Baseline budget if the original template
5856                      --    has baselined budget with the same budget type.
5857                      -- 2. Display warning message if no baselined cost
5858                      --    budget for PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST based revenue accrual project and
5859                      --    try to baseline its revenue budget.
5860 
5861                      IF (x_mark_as_original = 'Y' and x_orig_template_flag = 'Y' and
5862                          (with_funding = 1 or
5863                           c1_rec.budget_amount_code <> 'R')) THEN
5864 
5865                         x_err_stage :=
5866                              'get draft budget version for new project'
5867                              || x_new_project_id;
5868 
5869                         SELECT budget_version_id
5870                         INTO x_new_budget_ver_id
5871                         FROM pa_budget_versions
5872                         WHERE project_id = x_new_project_id
5873                         AND budget_status_code = PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_WORKING
5874                         AND budget_type_code = c1_rec.budget_type_code;
5875 
5876                         x_err_stage :=
5877                              'submit revenue budget for new project'
5878                              || x_new_project_id;
5879 
5880                         savepoint before_bill_baseline;
5881                         /*Bug 5378256: This condition added thru the bug 2986930, has been moved down
5882                          to prevent baseline alone when agreement amt is entered through quick agreement*/
5883                         -- If NOT(p_agreement_amount > 0 and c1_rec.budget_type_code = 'AR' )  -- bug 9652495
5884                         If NOT(NVL(p_agreement_amount,-999) > 0 and c1_rec.budget_type_code = 'AR')
5885                         then
5886 --EH Changes
5887                         BEGIN
5888 
5889                              pa_budget_utils2.submit_budget(x_budget_version_id  => x_new_budget_ver_id,
5890                                                             x_err_code           => x_err_code,
5891                                                             x_err_stage          => x_err_stage,
5892                                                             x_err_stack          => x_err_stack);
5893 
5894                              IF ( x_err_code <> 0 ) THEN
5895                                x_err_code := 785;
5896                                IF x_err_stage IS NULL
5897                                THEN
5898                                    x_err_stage := pa_project_core1.get_message_from_stack( 'PA_ERR_SUBMIT_BUDGT');
5899                                END IF;
5900                                x_err_stack := x_err_stack||'->pa_budget_utils2.submit_budget';
5901                                ROLLBACK TO before_bill_baseline;
5902                                RETURN;         -- Application or Oracle error
5903                              END IF;
5904                         EXCEPTION WHEN OTHERS THEN
5905                            x_err_code := 785;
5906 --                           x_err_stage := pa_project_core1.get_message_from_stack( null );
5907                            IF x_err_stage IS NULL
5908                            THEN
5909                               x_err_stage := 'API: '||'pa_budget_utils2.submit_budget'||
5910                                                ' SQL error message: '||SUBSTR( SQLERRM,1,1900);
5911                            END IF;
5912                            ROLLBACK TO before_bill_baseline;
5913                            RETURN;
5914                         END;
5915 
5916                         x_err_stage :=
5917                              'baseline revenue budget for new project'
5918                              || x_new_project_id;
5919 
5920                         pa_budget_core.baseline(
5921                              x_draft_version_id=> x_new_budget_ver_id,
5922                              x_mark_as_original=> x_mark_as_original,
5923                              x_verify_budget_rules => 'Y',
5924                              x_err_code   => x_err_code,
5925                              x_err_stage  => x_err_stage,
5926                              x_err_stack  => x_err_stack);
5927 
5928                         if ( x_err_code > 0 and
5929                              x_err_stage = 'PA_BU_NO_BASE_COST_BUDGET') then
5930                              rollback to before_bill_baseline;
5931                              x_err_code := 0;
5932                         elsif ( x_err_code <> 0 ) then
5933                              return;         -- Application or Oracle error
5934                         end if;
5935 
5936                         x_err_stage :=
5937                                 'reset revenue budget to working for new project'
5938                                 || x_new_project_id;
5939 --EH Changes
5940                         BEGIN
5941 
5942                            pa_budget_utils2.rework_budget(x_budget_version_id  => x_new_budget_ver_id,
5943                                                           x_err_code           => x_err_code,
5944                                                           x_err_stage          => x_err_stage,
5945                                                           x_err_stack          => x_err_stack);
5946 
5947                            IF ( x_err_code <> 0 ) THEN
5948                              x_err_code := 790;
5949                              IF x_err_stage IS NULL
5950                              THEN
5951                                  x_err_stage := pa_project_core1.get_message_from_stack( 'PA_ERR_REWORK_BUDGT');
5952                              END IF;
5953                              x_err_stack := x_err_stack||'->pa_budget_utils2.rework_budget';
5954                              ROLLBACK TO before_bill_baseline;
5955                              RETURN;         -- Application or Oracle error
5956                            END IF;
5957                         EXCEPTION WHEN OTHERS THEN
5958                            x_err_code := 790;
5959 --                           x_err_stage := pa_project_core1.get_message_from_stack( null );
5960                            IF x_err_stage IS NULL
5961                            THEN
5962                               x_err_stage := 'API: '||'pa_budget_utils2.rework_budget'||
5963                                                ' SQL error message: '||SUBSTR( SQLERRM,1,1900);
5964                            END IF;
5965                            ROLLBACK TO before_bill_baseline;
5966                            RETURN;
5967                         END;
5968 
5969                         End if; --Bug 5378256: Prevent baseline of AR budget when agreement amount is entered.
5970 
5971                         END IF;
5972 
5973       END IF;
5974 
5975                    END IF;
5976                   END IF;
5977         END IF; -- Added for bug 2986930
5978                  END LOOP;
5979                  CLOSE C1;
5980 
5981                  -- pa_budget_core.get_draft_version_id returns x_err_code = 10
5982                  -- when no budget version id is found, which is fine.
5983                  IF (x_err_code = 10) THEN
5984                         x_err_code := 0;
5985                  END IF;
5986             END;
5987 
5988 END Copy_Budgets_From_Project;
5989 
5990 /*===================================================================
5991   This is a main api used for copying the financila related entities
5992   fromsource project or template. This api takes care of the upgraded
5993   budget versions and takes care of all the business rules that relate
5994   to new financial planning module. This api would be called from
5995   PA_PROJECT_CORE!.COPY_PROJECT  after call to
5996   PA_BUDGET_CORE.COPY_BUDGETS_FROM_PROJECT
5997   Bug#  - 2981655 - Please see bug for the complete discussion about
5998   this bug. The core is, when copy_project is done with copy_budget_flag
5999   as N, we still have to copy the header level informations
6000   pa_proj_fp_options, pa_fp_txn_currencies, period profile information.
6001   Also, we should not be copying the planning elements for any of the
6002   copied options since when copy_budget_flag is N, tasks may not have
6003   been copied from the source project to the target project.
6004 
6005 --
6006 -- 14-JUL-2003 jwhite        - Bug 3045668
6007 --                             As directed by Venkatesh, added a
6008 --                             simple update near the end of the
6009 --                             Copy_Finplans_From_Project to set the
6010 --                             process_update_wbs_flag = 'N when
6011 --                             p_copy_version_and_elements = N.
6012 
6013 =====================================================================*/
6014 
6015 PROCEDURE Copy_Finplans_From_Project (
6016           p_source_project_id           IN      NUMBER
6017           ,p_target_project_id          IN      NUMBER
6018           ,p_shift_days                 IN      NUMBER
6019           ,p_copy_version_and_elements  IN      VARCHAR2
6020           ,p_agreement_amount           IN      NUMBER  -- Added for bug 2986930
6021           ,x_return_status              OUT     NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
6022           ,x_msg_count                  OUT     NOCOPY NUMBER --File.Sql.39 bug 4440895
6023           ,x_msg_data                   OUT     NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
6024 AS
6025       l_return_status      VARCHAR2(2000);
6026       l_msg_count          NUMBER :=0;
6027       l_msg_data           VARCHAR2(2000);
6028       l_data               VARCHAR2(2000);
6029       l_msg_index_out      NUMBER;
6030       l_err_code           NUMBER;
6031       l_err_stage          VARCHAR2(2000);
6032       l_err_stack          VARCHAR2(2000);
6033 
6034       l_index                     NUMBER;
6035       l_shift_days                NUMBER;
6036       l_period_type               VARCHAR2(15);
6037 
6038       l_proj_fp_options_id_tbl    PA_FP_COPY_FROM_PKG.PROJ_FP_OPTIONS_ID_TBL_TYP;
6039 
6040       l_source_template_flag      pa_projects_all.template_flag%TYPE;
6041       l_source_current_flag       pa_budget_versions.current_flag%TYPE;
6042       l_source_version_id         pa_budget_versions.budget_version_id%TYPE;
6043       l_source_fp_preference_code pa_proj_fp_options.fin_plan_preference_code%TYPE;
6044       l_source_record_version_num pa_budget_versions.record_version_number%TYPE;
6045       l_source_fin_plan_type_id   pa_proj_fp_options.fin_plan_type_id%TYPE;
6046 
6047       l_time_phased_code          pa_proj_fp_options.all_time_phased_code%TYPE;
6048 
6049       l_target_version_id         pa_budget_versions.budget_version_id%TYPE;
6050       l_target_profile_id         pa_budget_versions.period_profile_id%TYPE;
6051       l_target_proj_fp_options_id pa_proj_fp_options.proj_fp_options_id%TYPE;
6052       l_target_record_version_num pa_budget_versions.record_version_number%TYPE;
6053       l_version_type              pa_budget_versions.version_type%TYPE;
6054       l_funding_exists_flag       VARCHAR2(1);
6055 
6056 
6057       l_fp_option_level_code      pa_proj_fp_options.fin_plan_option_level_code%TYPE;
6058       l_plan_in_multi_curr_flag   pa_proj_fp_options.plan_in_multi_curr_flag%TYPE;
6059       l_appr_cost_plan_type_flag  pa_proj_fp_options.approved_cost_plan_type_flag%TYPE;
6060       l_appr_rev_plan_type_flag   pa_proj_fp_options.approved_rev_plan_type_flag%TYPE;
6061       l_struct_elem_version_id    pa_proj_element_versions.element_version_id%TYPE;
6062       l_budget_version_ids        SYSTEM.pa_num_tbl_type := SYSTEM.pa_num_tbl_type();
6063       l_src_budget_version_id_tbl SYSTEM.pa_num_tbl_type := SYSTEM.pa_num_tbl_type();
6064 
6065 
6066     /* Code addition for bug 2986930 starts */
6067         CURSOR c_ar_chk( p_source_ver_id pa_budget_versions.budget_version_id%TYPE) IS
6068         SELECT 'Y'
6069         FROM dual
6070         WHERE EXISTS(SELECT NULL
6071                          FROM   pa_budget_versions
6072                          WHERE budget_version_id = p_source_ver_id
6073                              AND approved_rev_plan_type_flag = 'Y');
6074 
6075         CURSOR c_bfl IS
6076         SELECT baseline_funding_flag
6077         FROM   pa_projects
6078         WHERE  project_id = p_source_project_id;
6079 
6080     l_ar_exists              VARCHAR2(1);
6081     l_baseline_funding_flag  VARCHAR2(1);
6082     l_fc_version_created_flag VARCHAR2(1);
6083     /* Code addition for bug 2986930 ends */
6084     -- IPM Arch Enhancement - Bug 4865563
6085     l_fp_cols_rec                   PA_FP_GEN_AMOUNT_UTILS.FP_COLS;  --This variable will be used to call pa_resource_asgn_curr maintenance api
6086     l_debug_level5           NUMBER:=5;
6087 
6088 BEGIN
6089 
6090     FND_MSG_PUB.INITIALIZE;
6091     x_msg_count := 0;
6092     x_return_status := FND_API.G_RET_STS_SUCCESS;
6093 
6094     IF P_PA_DEBUG_MODE = 'Y' THEN
6095         pa_debug.init_err_stack('PA_FP_COPY_FROM_PKG.Copy_Finplans_From_Project');
6096         pa_debug.set_process('PLSQL','LOG',p_pa_debug_mode);
6097     -- Check if  source project id is  NULL,if so throw an error message
6098         pa_debug.g_err_stage := 'Checking for valid parameters:';
6099         pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
6100     END IF;
6101 
6102     IF (p_source_project_id IS NULL)  OR
6103        (p_target_project_id IS NULL)  OR
6104        (p_copy_version_and_elements IS NULL)
6105     THEN
6106 
6107         IF P_PA_DEBUG_MODE = 'Y' THEN
6108            pa_debug.g_err_stage := 'Source_project='||p_source_project_id;
6109            pa_debug.write(g_module_name,pa_debug.g_err_stage,5);
6110            pa_debug.g_err_stage := 'Target_project='||p_target_project_id;
6111            pa_debug.write(g_module_name,pa_debug.g_err_stage,5);
6112            pa_debug.g_err_stage := 'p_copy_version_and_elements='||p_copy_version_and_elements;
6113            pa_debug.write(g_module_name,pa_debug.g_err_stage,5);
6114         END IF;
6115 
6116         PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
6117                              p_msg_name       => 'PA_FP_INV_PARAM_PASSED',
6118                              p_token1         => 'PROCEDURENAME',
6119                              p_value1         => 'PA_FP_COPY_FROM_PKG.Copy_Finplans_From_Project');
6120         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6121 
6122     END IF;
6123 
6124     IF P_PA_DEBUG_MODE = 'Y' THEN
6125        pa_debug.g_err_stage := 'Parameter validation complete';
6126        pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
6127     END IF;
6128 
6129     --IF shift_days i/p is NULL then make it zero.
6130 
6131     l_shift_days := NVL(p_shift_days,0);
6132 
6133     --Get the structure version id of the financial structure
6134     l_struct_elem_version_id := PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(p_project_id => p_target_project_id );
6135 
6136     --Checking if source project is template
6137 
6138     IF P_PA_DEBUG_MODE = 'Y' THEN
6139        pa_debug.g_err_stage := 'Fetching source project template flag';
6140        pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
6141     END IF;
6142 
6143     SELECT template_flag
6144     INTO   l_source_template_flag
6145     FROM   pa_projects_all
6146     WHERE  project_id = p_source_project_id;
6147 
6148     --Fetch project_level_funding_flag for target project to
6149     --baseline it or not
6150 
6151     IF P_PA_DEBUG_MODE = 'Y' THEN
6152        pa_debug.g_err_stage := 'Fetching target funding flag';
6153        pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
6154     END IF;
6155 
6156     /* 2616032: Modified the way the project level funding flag is got.
6157        This flag indicates if funding exists for the Target Project. */
6158 
6159     BEGIN
6160             SELECT 'Y'
6161             INTO   l_funding_exists_flag
6162             FROM   DUAL
6163             WHERE EXISTS (SELECT 1
6164                           FROM pa_project_fundings
6165                           WHERE project_id = p_target_project_id);
6166     EXCEPTION
6167       WHEN NO_DATA_FOUND THEN
6168            l_funding_exists_flag := 'N';
6169     END;
6170 
6171     /* Code addition for bug 2986930 starts */
6172     OPEN c_bfl;
6173     FETCH c_bfl into l_baseline_funding_flag;
6174     CLOSE c_bfl;
6175     /* Code addition for bug 2986930 ends */
6176 
6177     IF p_copy_version_and_elements = 'Y' THEN /* Bug 2981655 */
6178 
6179         --First Copy the budgets which aren't upgraded from source project to target project
6180 
6181          Copy_Budgets_From_Project(
6182                             p_from_project_id          =>  p_source_project_id
6183                             ,p_to_project_id           =>  p_target_project_id
6184                             ,p_delta                   =>  l_shift_days
6185                             ,p_orig_template_flag      =>  l_source_template_flag
6186                             ,p_agreement_amount        =>  p_agreement_amount  -- Added for bug 2986930
6187                             ,p_baseline_funding_flag   =>  l_baseline_funding_flag -- Added for bug 2986930
6188                             ,x_err_code                =>  l_err_code
6189                             ,x_err_stage               =>  l_err_stage
6190                             ,x_err_stack               =>  l_err_stack);
6191 
6192          IF l_err_code <> 0 THEN
6193 
6194                /* Bug# 2636723 - Error messages printed and "Raised" and not "Returned" */
6195 
6196             IF P_PA_DEBUG_MODE = 'Y' THEN
6197                   pa_debug.g_err_stage := 'Err code returned by copy_budgets_from_project api is ' || TO_CHAR(l_err_code);
6198                   pa_debug.write(g_module_name,pa_debug.g_err_stage,5);
6199                   pa_debug.g_err_stage := 'Err stage returned by copy_budgets_from_project api is ' || l_err_stage;
6200                   pa_debug.write(g_module_name,pa_debug.g_err_stage,5);
6201                   pa_debug.g_err_stage := 'Err stack returned by copy_budgets_from_project api is ' || l_err_stack;
6202                   pa_debug.write(g_module_name,pa_debug.g_err_stage,5);
6203             END IF;
6204 
6205             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6206 
6207           /* Bug# 2636723
6208                  RETURN; -- Application or Oracle error   */
6209 
6210          END IF; /* l_err_code <> 0 */
6211 
6212     END IF; /* p_copy_version_and_elements = 'Y' */
6213 
6214     --Fetch all the fp options ids to be copied from source project to
6215     --target into a plsql table.
6216     --For this call get_fp_options_to_be_copied api.
6217 
6218     IF P_PA_DEBUG_MODE = 'Y' THEN
6219         pa_debug.g_err_stage := 'Calling Get_Fp_Options_To_Be_Copied api';
6220         pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
6221     END IF;
6222 
6223     PA_FP_COPY_FROM_PKG.Get_Fp_Options_To_Be_Copied(
6224                p_source_project_id    => p_source_project_id
6225                ,p_copy_versions       => p_copy_version_and_elements  /* Bug 2981655 */
6226                ,x_fp_options_ids_tbl  => l_proj_fp_options_id_tbl
6227                ,x_return_status       => l_return_status
6228                ,x_msg_count           => l_msg_count
6229                ,x_msg_data            => l_msg_data );
6230 
6231     /* Added the following check for the NOCOPY changes. */
6232 
6233     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6234         raise PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6235     END IF;
6236 
6237     --Bug :- 2570874
6238     IF NVL(l_proj_fp_options_id_tbl.first,0) >0 THEN  --only if something to be copied
6239             FOR l_index IN  l_proj_fp_options_id_tbl.first..l_proj_fp_options_id_tbl.last
6240             LOOP
6241 
6242                  --Null out local variables used previously
6243 
6244                  l_target_version_id := NULL;
6245                  l_target_proj_fp_options_id := NULL;
6246 
6247                  --Fetch option level code of the options_id
6248 
6249                  IF P_PA_DEBUG_MODE = 'Y' THEN
6250                     pa_debug.g_err_stage := 'Fetching option level code ';
6251                     pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
6252                  END IF;
6253 
6254                  SELECT fin_plan_option_level_code
6255                         ,fin_plan_version_id
6256                         ,fin_plan_preference_code
6257                         ,fin_plan_type_id
6258                         ,plan_in_multi_curr_flag
6259                  INTO   l_fp_option_level_code
6260                         ,l_source_version_id
6261                         ,l_source_fp_preference_code
6262                         ,l_source_fin_plan_type_id
6263                         ,l_plan_in_multi_curr_flag
6264                  FROM   pa_proj_fp_options
6265                  WHERE  proj_fp_options_id = l_proj_fp_options_id_tbl(l_index);
6266 
6267                  IF P_PA_DEBUG_MODE = 'Y' THEN
6268                     pa_debug.g_err_stage := 'Option level code = '||l_fp_option_level_code;
6269                     pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
6270                  END IF;
6271 
6272 
6273                  --Copy the budget version if option level code is plan version
6274                  --We call the api with .99999 as adj_percentage to prevent
6275                  --population of amount columns
6276 
6277                  IF l_fp_option_level_code = PA_FP_CONSTANTS_PKG.G_OPTION_LEVEL_PLAN_VERSION THEN
6278 
6279 			  l_ar_exists := NULL;   -- Bug #12998107.This variable was not reset (before reading from cursor)previously for each iteration due to which non AR versions did not get copied to new project.
6280 
6281                       /* Code addition for Bug 2986930 starts */
6282                       OPEN c_ar_chk(l_source_version_id);
6283                       FETCH c_ar_chk INTO l_ar_exists;
6284                       CLOSE c_ar_chk;
6285 
6286                       IF ( NVL(p_agreement_amount,-1) > 0 and nvl(l_ar_exists,'N') <> 'Y' ) OR
6287                          ( NVL(p_agreement_amount,-1) < 0 and ( NVL(l_baseline_funding_flag,'N') = 'Y' AND NVl(l_ar_exists,'N') <> 'Y' ) ) OR
6288                          ( NVL(p_agreement_amount,-1) < 0 and NVL(l_baseline_funding_flag,'N') = 'N' ) OR
6289 			 ( NVL(p_agreement_amount,-1) > 0 and nvl(l_ar_exists,'N') = 'Y' and NVL(l_baseline_funding_flag,'N') = 'N')
6290                       THEN
6291                          /* Code addition for Bug 2986930 ends */
6292 
6293                                  IF P_PA_DEBUG_MODE = 'Y' THEN
6294                                      pa_debug.g_err_stage := 'Calling Copy_Budget_Version';
6295                                      pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
6296                                  END IF;
6297 
6298                                  l_target_version_id := NULL;
6299 
6300                                  PA_FP_COPY_FROM_PKG.Copy_Budget_Version(
6301                                           p_source_project_id       => p_source_project_id
6302                                           ,p_target_project_id      => p_target_project_id
6303                                           ,p_source_version_id      => l_source_version_id
6304                                           ,p_copy_mode              => PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_WORKING
6305                                           ,p_adj_percentage         => .99999
6306                                           ,p_calling_module         => PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_FIN_PLAN
6307                                           ,p_struct_elem_version_id => l_struct_elem_version_id
6308                                           ,p_shift_days             => l_shift_days
6309                                           ,px_target_version_id     => l_target_version_id
6310                                           ,x_return_status          => l_return_status
6311                                           ,x_msg_count              => l_msg_count
6312                                           ,x_msg_data               => l_msg_data );
6313 
6314                                  IF P_PA_DEBUG_MODE = 'Y' THEN
6315                                     pa_debug.g_err_stage := 'l_target_version_id = '||l_target_version_id;
6316                                     pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
6317                                  END IF;
6318 
6319                          END IF;/* Added for bug 2986930 */
6320                  END IF;
6321 
6322                  IF  l_fp_option_level_code <> PA_FP_CONSTANTS_PKG.G_OPTION_LEVEL_PLAN_VERSION   THEN
6323                      --Create equivalent fp option in pa_proj_fp_options for target
6324 
6325                      IF P_PA_DEBUG_MODE = 'Y' THEN
6326                              pa_debug.g_err_stage := 'Calling Create_Fp_Option';
6327                              pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
6328                      END IF;
6329 
6330                      PA_PROJ_FP_OPTIONS_PUB.Create_Fp_Option (
6331                                   px_target_proj_fp_option_id    => l_target_proj_fp_options_id
6332                                   ,p_source_proj_fp_option_id    => l_proj_fp_options_id_tbl(l_index)
6333                                   ,p_target_fp_option_level_code => l_fp_option_level_code       --same as source
6334                                   ,p_target_fp_preference_code   => l_source_fp_preference_code
6335                                   ,p_target_fin_plan_version_id  => l_target_version_id
6336                                   ,p_target_plan_type_id         => l_source_fin_plan_type_id    --same as source
6337                                   ,p_target_project_id           => p_target_project_id
6338                                   ,x_return_status               => l_return_status
6339                                   ,x_msg_count                   => l_msg_count
6340                                   ,x_msg_data                    => l_msg_data);
6341 
6342                      IF P_PA_DEBUG_MODE = 'Y' THEN
6343                           pa_debug.g_err_stage := 'Calling Copy_Fp_Txn_Currencies api';
6344                           pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
6345                      END IF;
6346 
6347                      PA_FP_TXN_CURRENCIES_PUB.Copy_Fp_Txn_Currencies(
6348                              p_source_fp_option_id        => l_proj_fp_options_id_tbl(l_index)
6349                              ,p_target_fp_option_id       => l_target_proj_fp_options_id
6350                              ,p_target_fp_preference_code => NULL
6351                              ,p_plan_in_multi_curr_flag   => l_plan_in_multi_curr_flag
6352                              ,x_return_status             => l_return_status
6353                              ,x_msg_count                 => l_msg_count
6354                              ,x_msg_data                  => l_msg_data);
6355 
6356                  ELSIF l_fp_option_level_code = PA_FP_CONSTANTS_PKG.G_OPTION_LEVEL_PLAN_VERSION THEN
6357 
6358                      /* Code addition for Bug 2986930 starts */
6359 
6360                        IF ( NVL(p_agreement_amount,-1) > 0 AND nvl(l_ar_exists,'N') <> 'Y' ) OR
6361                           ( NVL(p_agreement_amount,-1) < 0 AND ( NVL(l_baseline_funding_flag,'N') = 'Y' AND NVl(l_ar_exists,'N') <> 'Y' ) ) OR
6362                           ( NVL(p_agreement_amount,-1) < 0 AND NVL(l_baseline_funding_flag,'N') = 'N' ) OR
6363 			  ( NVL(p_agreement_amount,-1) > 0 and nvl(l_ar_exists,'N') = 'Y' and NVL(l_baseline_funding_flag,'N') = 'N')
6364                        THEN
6365                      /* Code addition for Bug 2986930 ends */
6366                              --Create equivalent fp option in pa_proj_fp_options for target
6367 
6368                              IF P_PA_DEBUG_MODE = 'Y' THEN
6369                                      pa_debug.g_err_stage := 'Calling Create_Fp_Option';
6370                                      pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
6371                              END IF;
6372 
6373                              PA_PROJ_FP_OPTIONS_PUB.Create_Fp_Option (
6374                                     px_target_proj_fp_option_id    => l_target_proj_fp_options_id
6375                                     ,p_source_proj_fp_option_id    => l_proj_fp_options_id_tbl(l_index)
6376                                     ,p_target_fp_option_level_code => l_fp_option_level_code       --same as source
6377                                     ,p_target_fp_preference_code   => l_source_fp_preference_code
6378                                     ,p_target_fin_plan_version_id  => l_target_version_id
6379                                     ,p_target_plan_type_id         => l_source_fin_plan_type_id    --same as source
6380                                     ,p_target_project_id           => p_target_project_id
6381                                     ,x_return_status               => l_return_status
6382                                     ,x_msg_count                   => l_msg_count
6383                                     ,x_msg_data                    => l_msg_data);
6384 
6385                              -- Call copy fp txn currencies api unconditionally
6386                              IF P_PA_DEBUG_MODE = 'Y' THEN
6387                                   pa_debug.g_err_stage := 'Calling Copy_Fp_Txn_Currencies api';
6388                                   pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
6389                              END IF;
6390 
6391                              PA_FP_TXN_CURRENCIES_PUB.Copy_Fp_Txn_Currencies(
6392                                      p_source_fp_option_id        => l_proj_fp_options_id_tbl(l_index)
6393                                      ,p_target_fp_option_id       => l_target_proj_fp_options_id
6394                                      ,p_target_fp_preference_code => NULL
6395                                      ,p_plan_in_multi_curr_flag   => l_plan_in_multi_curr_flag
6396                                      ,x_return_status             => l_return_status
6397                                      ,x_msg_count                 => l_msg_count
6398                                      ,x_msg_data                  => l_msg_data);
6399 
6400 
6401                              --Copy resource assignments for the target plan version
6402 
6403                              IF P_PA_DEBUG_MODE = 'Y' THEN
6404                                 pa_debug.g_err_stage := 'Calling Copy_Resource_Assignments';
6405                                 pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
6406                              END IF;
6407 
6408                              PA_FP_COPY_FROM_PKG.Copy_Resource_Assignments(
6409                                        p_source_plan_version_id  => l_source_version_id
6410                                        ,p_target_plan_version_id => l_target_version_id
6411                                        ,p_adj_percentage         => 0.99999
6412                                        ,x_return_status          => l_return_status
6413                                        ,x_msg_count              => l_msg_count
6414                                        ,x_msg_data               => l_msg_data);
6415 
6416                              IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6417 				IF P_PA_DEBUG_MODE = 'Y' THEN
6418 	                               pa_debug.write(g_module_name,' Return status from copy RA api is ' || l_Return_Status,3);
6419 				END IF;
6420         	                       raise PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6421                              END IF;
6422 
6423                          -- Copying budget_lines from source to target
6424 
6425                              IF P_PA_DEBUG_MODE = 'Y' THEN
6426                                 pa_debug.g_err_stage := 'Calling Copy_Budget_Lines';
6427                                 pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
6428                              END IF;
6429 
6430                              PA_FP_COPY_FROM_PKG.Copy_Budget_Lines(
6431                                         p_source_project_id       => p_source_project_id
6432                                         ,p_target_project_id      => p_target_project_id
6433                                         ,p_source_plan_version_id  => l_source_version_id
6434                                         ,p_target_plan_version_id  => l_target_version_id
6435                                         ,p_shift_days              => l_shift_days
6436                                         ,x_return_status           => l_return_status
6437                                         ,x_msg_count               => l_msg_count
6438                                         ,x_msg_data                => l_msg_data );
6439 
6440                              IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6441 				IF P_PA_DEBUG_MODE = 'Y' THEN
6442 	                               pa_debug.write(g_module_name,' Return status from copy bl api is ' || l_Return_Status,3);
6443 				END IF;
6444                                raise PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6445                              END IF;
6446 
6447                              --Calling Convert_Txn_Currency api to complete budget lines
6448                              --in all respects
6449 
6450                              IF P_PA_DEBUG_MODE = 'Y' THEN
6451                                 pa_debug.g_err_stage := 'Calling Convert_Txn_Currency';
6452                                 pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
6453                              END IF;
6454 
6455                              PA_FP_MULTI_CURRENCY_PKG.Convert_Txn_Currency(
6456                                        p_budget_version_id  => l_target_version_id
6457                                        ,p_entire_version    => 'Y'
6458                                        ,x_return_status     => l_return_status
6459                                        ,x_msg_count         => l_msg_count
6460                                        ,x_msg_data          => l_msg_data);
6461 
6462                              IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN -- Bug# 2634726
6463                                raise PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6464                              END IF;
6465 
6466                              -- Bug Fix: 4569365. Removed MRC code.
6467                              -- FPB2: MRC - Calling MRC APIs
6468                              /*
6469 
6470                              IF P_PA_DEBUG_MODE = 'Y' THEN
6471                                 pa_debug.g_err_stage:='Calling mrc api ........ ';
6472                                 pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
6473                              END IF;
6474 
6475                              IF PA_MRC_FINPLAN. g_mrc_enabled_for_budgets IS NULL THEN
6476                                     PA_MRC_FINPLAN.check_mrc_install
6477                                               (x_return_status      => l_return_status,
6478                                                x_msg_count          => l_msg_count,
6479                                                x_msg_data           => l_msg_data);
6480                              END IF;
6481 
6482                              IF PA_MRC_FINPLAN.g_mrc_enabled_for_budgets AND
6483                                     PA_MRC_FINPLAN.g_finplan_mrc_option_code = 'A' THEN
6484 
6485                                        PA_MRC_FINPLAN.g_calling_module := PA_MRC_FINPLAN.g_copy_projects; -- FPB2
6486 
6487                                        PA_MRC_FINPLAN.maintain_all_mc_budget_lines
6488                                               (p_fin_plan_version_id => l_target_version_id,
6489                                                p_entire_version      => 'Y',
6490                                                x_return_status       => x_return_status,
6491                                                x_msg_count           => x_msg_count,
6492                                                x_msg_data            => x_msg_data);
6493 
6494                                        PA_MRC_FINPLAN.g_calling_module := NULL;
6495                              END IF;
6496 
6497                              IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6498                                RAISE g_mrc_exception;
6499                              END IF;
6500                              */
6501 
6502                              --Calling copy_attachments api
6503                              --Copy all the source version attachments to target version
6504 
6505                              IF P_PA_DEBUG_MODE = 'Y' THEN
6506                                 pa_debug.g_err_stage := 'Calling Copy_Attachments api';
6507                                 pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
6508                              END IF;
6509 
6510                 /* BUG FIX 2955827
6511                  * copy_attachments is already done in PA_FP_COPY_FROM_PKG.Copy_Budget_Version
6512                               FND_ATTACHED_DOCUMENTS2_PKG.Copy_Attachments(
6513                                         x_from_entity_name        => 'PA_BUDGET_VERSIONS'
6514                                         ,x_from_pk1_value         => l_source_version_id
6515                                         ,x_from_pk2_value         => NULL
6516                                         ,x_from_pk3_value         => NULL
6517                                         ,x_from_pk4_value         => NULL
6518                                         ,x_from_pk5_value         => NULL
6519                                         ,x_to_entity_name         => 'PA_BUDGET_VERSIONS'
6520                                         ,x_to_pk1_value           => l_target_version_id
6521                                         ,x_to_pk2_value           => NULL
6522                                         ,x_to_pk3_value           => NULL
6523                                         ,x_to_pk4_value           => NULL
6524                                         ,x_to_pk5_value           => NULL
6525                                         ,x_created_by             => FND_GLOBAL.USER_ID
6526                                         ,x_last_update_login      => FND_GLOBAL.LOGIN_ID
6527                                         ,x_program_application_id => FND_GLOBAL.PROG_APPL_ID()
6528                                         ,x_program_id             => NULL
6529                                         ,x_request_id             => NULL
6530                                         ,x_automatically_added_flag => NULL);
6531                  END OF BUG FIX 2955827 */
6532 
6533        --IPM Architechture Enhancement Bug 4865563 - Start
6534 
6535                PA_FP_GEN_AMOUNT_UTILS.GET_PLAN_VERSION_DTLS
6536                    (P_BUDGET_VERSION_ID              => l_target_version_id,
6537                     X_FP_COLS_REC                    => l_fp_cols_rec,
6538                     X_RETURN_STATUS                  => l_return_status,
6539                     X_MSG_COUNT                      => l_msg_count,
6540                     X_MSG_DATA                       => l_msg_data);
6541 
6542                     IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
6543                                     IF P_PA_debug_mode = 'Y' THEN
6544                                        pa_debug.g_err_stage:= 'Error in PA_FP_GEN_AMOUNT_UTILS.GET_PLAN_VERSION_DETAILS';
6545                                        pa_debug.write(g_module_name,pa_debug.g_err_stage,l_debug_level5);
6546                                     END IF;
6547                     RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6548                     END IF;
6549 
6550                --Calling populate_display_qty for populating display_quantity in pa_budget_lines
6551                    PA_BUDGET_LINES_UTILS.populate_display_qty
6552                    (p_budget_version_id    => l_target_version_id,
6553                     p_context              => 'FINANCIAL',
6554                     p_use_temp_table_flag  => 'N',
6555                     x_return_status        => l_return_status);
6556 
6557                 IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
6558                                     IF P_PA_debug_mode = 'Y' THEN
6559                                        pa_debug.g_err_stage:= 'Error in PA_BUDGET_LINES_UTILS.populate_display_qty';
6560                                        pa_debug.write(g_module_name,pa_debug.g_err_stage,l_debug_level5);
6561                                     END IF;
6562                     RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6563                  END IF;
6564 
6565                            /*This piece of code calls maintain_data api with p_version_level_flag => 'N' i.e temp table mode for copying
6566                            overrides from the source version and with p_rollup_flag        => 'Y' for rolling up the amounts in the target
6567                            version */
6568 
6569                            DELETE pa_resource_asgn_curr_tmp;
6570 
6571                            /* Populating temp table with target resource_assignment_id along with txn_curr_code and
6572                            override rates from the source version of pa_resource_asgnc_curr */
6573                            /*Inserting into temp table */
6574                                        INSERT INTO pa_resource_asgn_curr_tmp
6575                                            (RESOURCE_ASSIGNMENT_ID,
6576                                            TXN_CURRENCY_CODE,
6577                                            txn_raw_cost_rate_override,
6578                                            txn_burden_cost_rate_override,
6579                                            txn_bill_rate_override)
6580                                            SELECT
6581                                              pra.resource_assignment_id,
6582                                              rac.txn_currency_code,
6583                                              rac.txn_raw_cost_rate_override,
6584                                              rac.txn_burden_cost_rate_override,
6585                                              rac.txn_bill_rate_override
6586                                            FROM
6587                                              pa_resource_asgn_curr rac,
6588                                              pa_resource_assignments pra
6589                                            WHERE
6590                                              pra.budget_version_id = l_target_version_id   and
6591                                              rac.budget_version_id = l_source_version_id  and
6592                                              pra.parent_assignment_id = rac.resource_assignment_id;
6593 
6594                            /*Calling the maintain_data api for the 2nd time to do the rollup from
6595                              pa_budget_lines. Note: This keeps the override rates copied in the
6596                              previous call, intact */
6597                            pa_res_asg_currency_pub.maintain_data
6598                            (p_fp_cols_rec        => l_fp_cols_rec,
6599                             p_calling_module     => 'COPY_PLAN',
6600                             p_rollup_flag        => 'Y', --rolling up
6601                             p_version_level_flag => 'N', --temp table mode
6602                             x_return_status      => l_return_status,
6603                             x_msg_data           => l_msg_count,
6604                             x_msg_count          => l_msg_data);
6605 
6606                                 IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
6607                                                 IF P_PA_debug_mode = 'Y' THEN
6608                                                    pa_debug.g_err_stage:= 'Error in PA_RES_ASG_CURRENCY_PUB.MAINTAIN_DATA while doing the rollup';
6609                                                    pa_debug.write(g_module_name,pa_debug.g_err_stage,l_debug_level5);
6610                                                 END IF;
6611                                 RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6612                                 END IF;
6613 
6614 
6615            --IPM Architechture Enhancement Bug 4865563 - End
6616 
6617                              -- Rollup the budget version
6618                              IF P_PA_DEBUG_MODE = 'Y' THEN
6619                                      pa_debug.g_err_stage := 'Calling Rollup_budget_version api';
6620                                      pa_debug.write( g_module_name,pa_debug.g_err_stage,3);
6621                              END IF;
6622 
6623 
6624                              PA_FP_ROLLUP_PKG.ROLLUP_BUDGET_VERSION(
6625                                           p_budget_version_id  => l_target_version_id
6626                                           ,p_entire_version    => 'Y'
6627                                           ,x_return_status     => l_return_status
6628                                           ,x_msg_count         => l_msg_count
6629                                           ,x_msg_data          => l_msg_data );
6630 
6631                              /* FP M - Reporting lines integration */
6632                              l_budget_version_ids.delete;
6633                              l_budget_version_ids   := SYSTEM.pa_num_tbl_type(l_target_version_id);
6634 
6635                              IF P_PA_DEBUG_MODE = 'Y' THEN
6636                                  pa_debug.write('Copy_Finplans_From_Project','Calling PJI_FM_XBS_ACCUM_MAINT.PLAN_CREATE ' ,5);
6637                                  pa_debug.write('Copy_Finplans_From_Project','p_fp_version_ids count '|| l_budget_version_ids.count(),5);
6638                              END IF;
6639 
6640                              /* We are sure that there is only one record. But just looping the std way */
6641                              FOR I in l_budget_version_ids.first..l_budget_version_ids.last LOOP
6642 				IF P_PA_DEBUG_MODE = 'Y' THEN
6643                                 pa_debug.write('Copy_Finplans_From_Project',''|| l_budget_version_ids(i),5);
6644 				END IF;
6645                              END LOOP;
6646 
6647                              l_src_budget_version_id_tbl.delete;
6648                              l_src_budget_version_id_tbl   := SYSTEM.pa_num_tbl_type(l_source_version_id);
6649                              -- This parameter will be used when the source project is not equal to the target project.
6650                              --This will be passed as null in the MSP flow other wise it will be defaulted to 'P'.
6651                              Declare
6652                                 l_copy_mode VARCHAR2(1);
6653                              Begin
6654                                   IF p_copy_version_and_elements = 'Y' THEN
6655                                        l_copy_mode := 'P';
6656                                   END IF;
6657 
6658                                   PJI_FM_XBS_ACCUM_MAINT.PLAN_CREATE (
6659                                          p_fp_version_ids   => l_budget_version_ids,
6660                                          p_fp_src_version_ids => l_src_budget_version_id_tbl,
6661                                          p_copy_mode   => l_copy_mode,
6662                                          x_return_status    => l_return_status,
6663                                          x_msg_code         => l_err_stack);
6664 
6665                                   IF l_return_status <> FND_API.G_RET_STS_SUCCESS then
6666                                        PA_UTILS.ADD_MESSAGE(p_app_short_name      => PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_PA,
6667                                                             p_msg_name            => l_err_stack);
6668 
6669                                        RAISE  PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6670                                   END IF;
6671 
6672                              End;
6673 
6674 
6675                              --If source plan version is baselined and if the source
6676                              --project is a template submit and baseline the target
6677                              --plan version also if it satisfies any of the two cases
6678                              --case 1: Funding is available for target project
6679                              --case 2: version type created is cost version
6680 
6681                              --Fetch current_flag of source plan version to check if
6682                              --source version is baselined
6683 
6684                              IF P_PA_DEBUG_MODE = 'Y' THEN
6685                                 pa_debug.g_err_stage := 'Fetching source version details';
6686                                 pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
6687                              END IF;
6688 
6689                              SELECT current_flag
6690                                     ,record_version_number
6691                              INTO   l_source_current_flag
6692                                     ,l_source_record_version_num
6693                              FROM   pa_budget_versions
6694                              WHERE  budget_version_id = l_source_version_id;
6695 
6696                              --Fetch target version properties for the api calls
6697 
6698                              IF P_PA_DEBUG_MODE = 'Y' THEN
6699                                 pa_debug.g_err_stage := 'Fetching target version details';
6700                                 pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
6701                              END IF;
6702 
6703                              SELECT record_version_number
6704                                     ,version_type
6705                              INTO   l_target_record_version_num
6706                                     ,l_version_type
6707                              FROM   pa_budget_versions
6708                              WHERE  budget_version_id = l_target_version_id;
6709 
6710                              IF (l_source_template_flag = 'Y') AND
6711                                 (l_source_current_flag = 'Y' )
6712                              THEN
6713 
6714                                   IF (nvl(l_funding_exists_flag,'N') = 'Y') OR
6715                                      (l_version_type = PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST)
6716                                   THEN
6717 
6718                                        --submit and baseline the the plan version
6719 
6720                                        IF P_PA_DEBUG_MODE = 'Y' THEN
6721                                           pa_debug.g_err_stage := 'Calling Set_Current_Working';
6722                                           pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
6723                                        END IF;
6724 
6725                                        PA_FIN_PLAN_PUB.Set_Current_Working(
6726                                               p_project_id                  => p_target_project_id
6727                                               ,p_budget_version_id          => l_target_version_id
6728                                               ,p_record_version_number      => NULL   --l_target_record_version_num
6729                                               ,p_orig_budget_version_id     => l_target_version_id  --as this is the initial creation
6730                                               ,p_orig_record_version_number => NULL
6731                                               ,x_return_status              => l_return_status
6732                                               ,x_msg_count                  => l_msg_count
6733                                               ,x_msg_data                   => l_msg_data );
6734 
6735                                        /* Bug# 2647047 - Raise if return status is not success */
6736                                        IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6737                                          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6738                                        END IF;
6739 
6740                                        IF P_PA_DEBUG_MODE = 'Y' THEN
6741                                                pa_debug.g_err_stage := 'Calling Submit_Current_Working';
6742                                                pa_debug.write( g_module_name,pa_debug.g_err_stage,3);
6743                                        END IF;
6744 
6745                                        --Bug 3964755. In copy project flow, the version need not be locked. Added the context parameter
6746                                        --to submit API to skip this check.
6747                                        PA_FIN_PLAN_PUB.Submit_Current_Working(
6748                                                p_calling_context       => 'COPY_PROJECT'
6749                                               ,p_project_id            => p_target_project_id
6750                                               ,p_budget_version_id     => l_target_version_id
6751                                               ,p_record_version_number => NULL --l_target_record_version_num
6752                                               ,x_return_status         => l_return_status
6753                                               ,x_msg_count             => l_msg_count
6754                                               ,x_msg_data              => l_msg_data );
6755 
6756                                        /* Bug# 2647047 - Raise if return status is not success */
6757                                        IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6758                                          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6759                                        END IF;
6760 
6761                                        IF P_PA_DEBUG_MODE = 'Y' THEN
6762                                                pa_debug.g_err_stage := 'Calling Baseline';
6763                                                pa_debug.write( g_module_name,pa_debug.g_err_stage,3);
6764                                        END IF;
6765 
6766                                        -- Bug Fix: 4569365. Removed MRC code.
6767 									   -- PA_MRC_FINPLAN.G_CALLING_MODULE :=  PA_MRC_FINPLAN.G_COPY_PROJECTS; /* FPB2 */
6768 
6769                                        PA_FIN_PLAN_PUB.Baseline(
6770                                                p_project_id                  => p_target_project_id
6771                                                ,p_budget_version_id          => l_target_version_id
6772                                                ,p_record_version_number      => NUll --l_target_record_version_num
6773                                                ,p_orig_budget_version_id     => NULL --l_target_version_id  Bug # 2680859
6774                                                ,p_orig_record_version_number => NULL
6775                                                ,x_fc_version_created_flag    => l_fc_version_created_flag
6776                                                ,x_return_status              => l_return_status
6777                                                ,x_msg_count                  => l_msg_count
6778                                                ,x_msg_data                   => l_msg_data );
6779 
6780                                        -- Bug Fix: 4569365. Removed MRC code.
6781 									   -- PA_MRC_FINPLAN.G_CALLING_MODULE := NULL; /* MRC */
6782 
6783                                        /* Bug# 2647047 - Raise if return status is not success */
6784                                        IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6785                                          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6786                                        END IF;
6787 
6788                                   END IF;
6789 
6790                              END IF;   --ifs used for baselining
6791                        END IF;  -- if version can be copied
6792 
6793                  END IF; -- if version
6794 
6795              END LOOP;  -- l_proj_fp_options_id_tbl
6796 
6797     END IF; --Bug :- 2570874
6798 
6799     IF P_PA_DEBUG_MODE = 'Y' THEN
6800             pa_debug.g_err_stage := 'Exiting Copy_Finplans_From_Project';
6801             pa_debug.write( g_module_name,pa_debug.g_err_stage,3);
6802 	    pa_debug.reset_err_stack;
6803     END IF;
6804 EXCEPTION
6805 
6806    WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
6807 
6808         l_msg_count := FND_MSG_PUB.count_msg;
6809         IF l_msg_count = 1 THEN
6810              PA_INTERFACE_UTILS_PUB.get_messages
6811                    (p_encoded         => FND_API.G_TRUE
6812                     ,p_msg_index      => 1
6813                     ,p_msg_count      => l_msg_count
6814                     ,p_msg_data       => l_msg_data
6815                     ,p_data           => l_data
6816                     ,p_msg_index_out  => l_msg_index_out);
6817              x_msg_data := l_data;
6818              x_msg_count := l_msg_count;
6819         ELSE
6820             x_msg_count := l_msg_count;
6821         END IF;
6822 
6823         IF P_PA_DEBUG_MODE = 'Y' THEN
6824            pa_debug.g_err_stage:='Invalid Arguments Passed';
6825            pa_debug.write( g_module_name,pa_debug.g_err_stage,5);
6826            pa_debug.reset_err_stack;
6827         END IF;
6828         x_return_status:= FND_API.G_RET_STS_ERROR;
6829         -- bug#2753123
6830         IF l_err_stage is NOT NULL THEN
6831             x_msg_data := l_err_stage ;
6832         END IF ;
6833         -- Bug Fix: 4569365. Removed MRC code.
6834 
6835         -- bug 3163280 ROLLBACK TO COPY_PROJECT;
6836         -- PA_MRC_FINPLAN.G_CALLING_MODULE := Null;
6837         RETURN ;
6838 
6839    WHEN Others THEN
6840 
6841         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6842         x_msg_count     := 1;
6843         x_msg_data      := SQLERRM;
6844         FND_MSG_PUB.add_exc_msg( p_pkg_name=> 'PA_FP_COPY_FROM_PKG'
6845                         ,p_procedure_name  => 'Copy_Finplans_From_Project');
6846 
6847         IF P_PA_DEBUG_MODE = 'Y' THEN
6848                 pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
6849                 pa_debug.write( g_module_name,pa_debug.g_err_stage,5);
6850                 pa_debug.reset_err_stack;
6851         END IF;
6852         -- Bug Fix: 4569365. Removed MRC code.
6853 
6854         -- bug 3163280 ROLLBACK TO COPY_PROJECT;
6855         -- PA_MRC_FINPLAN.G_CALLING_MODULE := Null;
6856         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6857 
6858 END Copy_Finplans_From_Project;
6859 
6860 
6861 /*===================================================================
6862   This is a private procedure called from Copy_Finplans_From_Project.
6863   This api populates a plsql table with all the proj_fp_options_id of
6864   source project that need to be copied to target project.
6865   Bug  2981655- Included new parameter p_copy_versions. If Y, versions
6866   will also be copied apart from plan type and project options. If N,
6867   only project and plan type options would be copied.
6868  ==================================================================*/
6869 PROCEDURE Get_Fp_Options_To_Be_Copied(
6870            p_source_project_id    IN  NUMBER
6871            ,p_copy_versions       IN  VARCHAR2
6872            ,x_fp_options_ids_tbl  OUT NOCOPY PROJ_FP_OPTIONS_ID_TBL_TYP
6873            ,x_return_status       OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
6874            ,x_msg_count           OUT NOCOPY NUMBER --File.Sql.39 bug 4440895
6875            ,x_msg_data            OUT NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
6876 AS
6877 
6878        l_return_status      VARCHAR2(2000);
6879        l_msg_count          NUMBER :=0;
6880        l_msg_data           VARCHAR2(2000);
6881        l_data               VARCHAR2(2000);
6882        l_msg_index_out      NUMBER;
6883        l_debug_mode         VARCHAR2(30);
6884 
6885        l_index                      NUMBER;
6886        l_fin_plan_preference_code   pa_proj_fp_options.fin_plan_preference_code%TYPE;
6887        l_fp_options_id              pa_proj_fp_options.proj_fp_options_id%TYPE;
6888        l_fin_plan_version_id        pa_proj_fp_options.fin_plan_version_id%TYPE;
6889        l_fin_plan_type_id           pa_proj_fp_options.fin_plan_type_id%TYPE;
6890 
6891        l_proj_fp_options_id_tbl     PROJ_FP_OPTIONS_ID_TBL_TYP;
6892 
6893        CURSOR cur_for_fp_options(c_level_code pa_proj_fp_options.fin_plan_option_level_code%TYPE)  IS
6894               SELECT pfo.proj_fp_options_id
6895                     ,pfo.fin_plan_type_id
6896                     ,pfo.fin_plan_preference_code
6897               FROM   pa_proj_fp_options pfo
6898                     ,pa_fin_plan_types_b fin
6899               WHERE  project_id = p_source_project_id
6900               AND    fin_plan_option_level_code = c_level_code
6901               AND    pfo.fin_plan_type_id = fin.fin_plan_type_id(+)
6902                           AND    nvl(fin.use_for_workplan_flag,'N')<>'Y';
6903 
6904 
6905 BEGIN
6906 
6907     x_msg_count := 0;
6908     x_return_status := FND_API.G_RET_STS_SUCCESS;
6909 
6910     IF p_pa_debug_mode = 'Y' THEN
6911             pa_debug.set_err_stack('PA_FP_COPY_FROM_PKG.Get_Fp_Options_To_Be_Copied');
6912             pa_debug.set_process('PLSQL','LOG',p_pa_debug_mode);
6913     END IF;
6914 
6915     -- Check if  source project id is  NULL,if so throw an error message
6916 
6917     IF P_PA_DEBUG_MODE = 'Y' THEN
6918         pa_debug.g_err_stage := 'Checking for valid parameters:';
6919         pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
6920     END IF;
6921 
6922     IF (p_source_project_id IS NULL) OR
6923        (p_copy_versions IS NULL) THEN
6924 
6925         IF P_PA_DEBUG_MODE = 'Y' THEN
6926            pa_debug.g_err_stage := 'Source_project='||p_source_project_id;
6927            pa_debug.write(g_module_name,pa_debug.g_err_stage,5);
6928            pa_debug.g_err_stage := 'p_copy_versions='||p_copy_versions;
6929            pa_debug.write(g_module_name,pa_debug.g_err_stage,5);
6930         END IF;
6931         PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
6932                              p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
6933         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6934 
6935     END IF;
6936 
6937     IF P_PA_DEBUG_MODE = 'Y' THEN
6938        pa_debug.g_err_stage := 'Parameter validation complete';
6939        pa_debug.write( g_module_name,pa_debug.g_err_stage,3);
6940     END IF;
6941 
6942     --Fetch and store project level fp option id in proj_fp_options_id_tbl.
6943 
6944     IF P_PA_DEBUG_MODE = 'Y' THEN
6945        pa_debug.g_err_stage := 'Fetching project level fp option id';
6946        pa_debug.write( g_module_name,pa_debug.g_err_stage,3);
6947     END IF;
6948 
6949     OPEN cur_for_fp_options(PA_FP_CONSTANTS_PKG.G_OPTION_LEVEL_PROJECT);
6950 
6951        FETCH cur_for_fp_options INTO
6952               l_proj_fp_options_id_tbl(1)
6953               ,l_fin_plan_type_id
6954               ,l_fin_plan_preference_code;
6955 
6956     CLOSE cur_for_fp_options;
6957 
6958     --Open and fetch fp options ids of all the plan types attached to project
6959 
6960     IF P_PA_DEBUG_MODE = 'Y' THEN
6961            pa_debug.g_err_stage := 'Opening cur_for_plan_type_fp_options';
6962            pa_debug.write(g_module_name,pa_debug.g_err_stage,3);
6963     END IF;
6964 
6965     OPEN cur_for_fp_options(PA_FP_CONSTANTS_PKG.G_OPTION_LEVEL_PLAN_TYPE);
6966     LOOP
6967 
6968        l_fp_options_id := NULL;
6969        l_fin_plan_version_id := NULL;
6970 
6971        FETCH cur_for_fp_options INTO
6972               l_proj_fp_options_id_tbl(nvl(l_proj_fp_options_id_tbl.last,0)+1)
6973               ,l_fin_plan_type_id
6974               ,l_fin_plan_preference_code;
6975 
6976        EXIT WHEN cur_for_fp_options%NOTFOUND;
6977 
6978        IF p_copy_versions = 'Y' THEN /* Bug 2981655 */
6979 
6980             --For each plan type fetched copy the options id of baselined or
6981             --current working version.
6982 
6983             IF P_PA_DEBUG_MODE = 'Y' THEN
6984                pa_debug.g_err_stage := 'Preference_code ='|| l_fin_plan_preference_code;
6985                pa_debug.write( g_module_name,pa_debug.g_err_stage,3);
6986             END IF;
6987 
6988             --For COST_AND REV_SEP plan type we have to copy both revenue and cost
6989             --versions.
6990 
6991             IF l_fin_plan_preference_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SEP THEN
6992 
6993                  IF P_PA_DEBUG_MODE = 'Y' THEN
6994                     pa_debug.g_err_stage := 'Fetching baselined cost plan version';
6995                     pa_debug.write( g_module_name,pa_debug.g_err_stage,3);
6996                  END IF;
6997 
6998                  PA_FIN_PLAN_UTILS.Get_Baselined_Version_Info(
6999                            p_project_id            => p_source_project_id
7000                            ,p_fin_plan_type_id     => l_fin_plan_type_id
7001                            ,p_version_type         => PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST
7002                            ,x_fp_options_id        => l_fp_options_id
7003                            ,x_fin_plan_version_id  => l_fin_plan_version_id
7004                            ,x_return_status        => l_return_status
7005                            ,x_msg_count            => l_msg_count
7006                            ,x_msg_data             => l_msg_data );
7007 
7008                  --IF there is no baselined version existing fetch the options
7009                  --id current working version
7010 
7011                  IF (l_fp_options_id IS NULL) THEN
7012 
7013                        IF P_PA_DEBUG_MODE = 'Y' THEN
7014                           pa_debug.g_err_stage := 'Baselined plan cost version does not exist';
7015                           pa_debug.write( g_module_name,pa_debug.g_err_stage,3);
7016 
7017                           pa_debug.g_err_stage := 'Fetching current cost woking plan version if any';
7018                           pa_debug.write( g_module_name,pa_debug.g_err_stage,3);
7019                        END IF;
7020 
7021                        PA_FIN_PLAN_UTILS.Get_Curr_Working_Version_Info(
7022                                  p_project_id           => p_source_project_id
7023                                  ,p_fin_plan_type_id    => l_fin_plan_type_id
7024                                  ,p_version_type        => PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST
7025                                  ,x_fp_options_id       => l_fp_options_id
7026                                  ,x_fin_plan_version_id => l_fin_plan_version_id
7027                                  ,x_return_status       => l_return_status
7028                                  ,x_msg_count           => l_msg_count
7029                                  ,x_msg_data            => l_msg_data );
7030 
7031                  END IF;
7032 
7033                  --Insert the fetched option id of plan version  if existing
7034 
7035                  IF (l_fp_options_id IS NOT NULL) THEN
7036                      IF P_PA_DEBUG_MODE = 'Y' THEN
7037                              pa_debug.g_err_stage := 'Storing option id of cost plan version fetched';
7038                              pa_debug.write( g_module_name,pa_debug.g_err_stage,3);
7039                      END IF;
7040 
7041                      l_proj_fp_options_id_tbl(nvl(l_proj_fp_options_id_tbl.last,0)+1) := l_fp_options_id;
7042                  ELSE
7043                      IF P_PA_DEBUG_MODE = 'Y' THEN
7044                          pa_debug.g_err_stage := 'Current working cost plan version does not exist for this plan type';
7045                          pa_debug.write( g_module_name,pa_debug.g_err_stage,3);
7046                      END IF;
7047                  END IF;
7048 
7049                  --Fetch revenue version id for the plan type
7050 
7051                  IF P_PA_DEBUG_MODE = 'Y' THEN
7052                     pa_debug.g_err_stage := 'Fetching baselined revenue plan version';
7053                     pa_debug.write( g_module_name,pa_debug.g_err_stage,3);
7054                  END IF;
7055 
7056                  PA_FIN_PLAN_UTILS.Get_Baselined_Version_Info(
7057                            p_project_id           => p_source_project_id
7058                            ,p_fin_plan_type_id    => l_fin_plan_type_id
7059                            ,p_version_type        => PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE
7060                            ,x_fp_options_id       => l_fp_options_id
7061                            ,x_fin_plan_version_id => l_fin_plan_version_id
7062                            ,x_return_status       => l_return_status
7063                            ,x_msg_count           => l_msg_count
7064                            ,x_msg_data            => l_msg_data );
7065 
7066                  --IF there is no baselined version existing fetch the options
7067                  --id current working version
7068 
7069                  IF (l_fp_options_id IS NULL) THEN
7070 
7071                        IF P_PA_DEBUG_MODE = 'Y' THEN
7072                           pa_debug.g_err_stage := 'Baselined plan revenue version does not exist';
7073                           pa_debug.write( g_module_name,pa_debug.g_err_stage,3);
7074 
7075                           pa_debug.g_err_stage := 'Fetching current revenue woking plan version if any';
7076                           pa_debug.write( g_module_name,pa_debug.g_err_stage,3);
7077                        END IF;
7078 
7079                        PA_FIN_PLAN_UTILS.Get_Curr_Working_Version_Info(
7080                                  p_project_id           => p_source_project_id
7081                                  ,p_fin_plan_type_id    => l_fin_plan_type_id
7082                                  ,p_version_type        => PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE
7083                                  ,x_fp_options_id       => l_fp_options_id
7084                                  ,x_fin_plan_version_id => l_fin_plan_version_id
7085                                  ,x_return_status       => l_return_status
7086                                  ,x_msg_count           => l_msg_count
7087                                  ,x_msg_data            => l_msg_data );
7088 
7089                  END IF;
7090 
7091                  --Insert the fetched option id of plan version  if existing
7092 
7093                  IF (l_fp_options_id IS NOT NULL) THEN
7094 
7095                      IF P_PA_DEBUG_MODE = 'Y' THEN
7096                              pa_debug.g_err_stage := 'Storing option id of revenue plan version fetched';
7097                              pa_debug.write( g_module_name,pa_debug.g_err_stage,3);
7098                      END IF;
7099 
7100                      l_proj_fp_options_id_tbl(nvl(l_proj_fp_options_id_tbl.last,0)+1) := l_fp_options_id;
7101 
7102                  ELSE
7103                      IF P_PA_DEBUG_MODE = 'Y' THEN
7104                              pa_debug.g_err_stage := 'Current working revenue plan version does not exist for this plan type';
7105                              pa_debug.write( g_module_name,pa_debug.g_err_stage,3);
7106                      END IF;
7107                  END IF;
7108 
7109             ELSE
7110 
7111                   --Fetch baselined plan version id  for the plan type
7112 
7113                   IF P_PA_DEBUG_MODE = 'Y' THEN
7114                           pa_debug.g_err_stage := 'Fetching baselined plan version if any';
7115                           pa_debug.write( g_module_name,pa_debug.g_err_stage,3);
7116                   END IF;
7117 
7118                   PA_FIN_PLAN_UTILS.Get_Baselined_Version_Info(
7119                             p_project_id           => p_source_project_id
7120                             ,p_fin_plan_type_id    => l_fin_plan_type_id
7121                             ,p_version_type        => NULL
7122                             ,x_fp_options_id       => l_fp_options_id
7123                             ,x_fin_plan_version_id => l_fin_plan_version_id
7124                             ,x_return_status       => l_return_status
7125                             ,x_msg_count           => l_msg_count
7126                             ,x_msg_data            => l_msg_data );
7127 
7128                   --IF there is no baselined version existing fetch the options
7129                   --id current working version
7130 
7131                   IF (l_fp_options_id IS NULL) THEN
7132 
7133                         IF P_PA_DEBUG_MODE = 'Y' THEN
7134                                 pa_debug.g_err_stage := 'Baselined plan version does not exist';
7135                                 pa_debug.write( g_module_name,pa_debug.g_err_stage,3);
7136 
7137                                 pa_debug.g_err_stage := 'Fetching current woking plan version if any';
7138                                 pa_debug.write( g_module_name,pa_debug.g_err_stage,3);
7139                         END IF;
7140 
7141                         PA_FIN_PLAN_UTILS.Get_Curr_Working_Version_Info(
7142                                   p_project_id            => p_source_project_id
7143                                   ,p_fin_plan_type_id     => l_fin_plan_type_id
7144                                   ,p_version_type         => NULL
7145                                   ,x_fp_options_id        => l_fp_options_id
7146                                   ,x_fin_plan_version_id  => l_fin_plan_version_id
7147                                   ,x_return_status        => l_return_status
7148                                   ,x_msg_count            => l_msg_count
7149                                   ,x_msg_data             => l_msg_data );
7150 
7151                   END IF;
7152 
7153                   --Insert the fetched option id of plan version if existing
7154 
7155                   IF (l_fp_options_id IS NOT NULL) THEN
7156 
7157                         IF P_PA_DEBUG_MODE = 'Y' THEN
7158                            pa_debug.g_err_stage := 'Storing option id of plan version fetched';
7159                            pa_debug.write( g_module_name,pa_debug.g_err_stage,3);
7160                         END IF;
7161 
7162                         l_proj_fp_options_id_tbl(nvl(l_proj_fp_options_id_tbl.last,0)+1) := l_fp_options_id;
7163 
7164                   ELSE
7165                         IF P_PA_DEBUG_MODE = 'Y' THEN
7166                               pa_debug.g_err_stage := 'Current working plan version does not exist for this plan type';
7167                               pa_debug.write( g_module_name,pa_debug.g_err_stage,3);
7168                         END IF;
7169                   END IF;
7170             END IF; --l_fin_plan_preference_code
7171 
7172        END IF; /* p_copy_versions = 'Y' */
7173 
7174     END LOOP;
7175     CLOSE cur_for_fp_options;
7176 
7177     --Return the fp_options_id tbl;
7178 
7179     x_fp_options_ids_tbl := l_proj_fp_options_id_tbl;
7180 
7181     IF P_PA_DEBUG_MODE = 'Y' THEN
7182             pa_debug.g_err_stage := 'Exiting Get_Fp_Options_To_Be_Copied';
7183             pa_debug.write( g_module_name,pa_debug.g_err_stage,3);
7184             pa_debug.reset_err_stack;
7185     END IF;
7186 
7187 EXCEPTION
7188 
7189    WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
7190         l_msg_count := FND_MSG_PUB.count_msg;
7191         IF l_msg_count = 1 THEN
7192              PA_INTERFACE_UTILS_PUB.get_messages
7193                    (p_encoded        => FND_API.G_TRUE
7194                     ,p_msg_index      => 1
7195                     ,p_msg_count      => l_msg_count
7196                     ,p_msg_data       => l_msg_data
7197                     ,p_data           => l_data
7198                     ,p_msg_index_out  => l_msg_index_out);
7199              x_msg_data := l_data;
7200              x_msg_count := l_msg_count;
7201         ELSE
7202             x_msg_count := l_msg_count;
7203         END IF;
7204 
7205         IF P_PA_DEBUG_MODE = 'Y' THEN
7206            pa_debug.g_err_stage:='Invalid Arguments Passed';
7207            pa_debug.write( g_module_name,pa_debug.g_err_stage,5);
7208            pa_debug.reset_err_stack;
7209         END IF;
7210         x_return_status:= FND_API.G_RET_STS_ERROR;
7211 
7212         RAISE;
7213 
7214    WHEN Others THEN
7215 
7216         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7217         x_msg_count     := 1;
7218         x_msg_data      := SQLERRM;
7219         FND_MSG_PUB.add_exc_msg( p_pkg_name=> 'PA_FP_COPY_FROM_PKG'
7220                         ,p_procedure_name  => 'Get_Fp_Options_To_Be_Copied');
7221 
7222         IF P_PA_DEBUG_MODE = 'Y' THEN
7223                 pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
7224                 pa_debug.write( g_module_name,pa_debug.g_err_stage,5);
7225                 pa_debug.reset_err_stack;
7226         END IF;
7227         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7228 
7229 END Get_Fp_Options_To_Be_Copied;
7230 
7231 
7232 /* Bug# 2634726 -
7233    Private procedure (not available in specification) used by COPY_BUDGET_LINES
7234    (with shift days logic) to insert shifted periods data into pa_fp_cpy_period_tmp */
7235 
7236 PROCEDURE populate_cpy_periods_tmp(p_budget_version_id PA_BUDGET_LINES.budget_version_id%type,
7237                                    p_period_type       PA_PROJ_FP_OPTIONS.cost_time_phased_code%TYPE,
7238                                    p_shift_periods     number) AS
7239 cursor bl_periods is
7240     SELECT distinct bl.period_name,bl.start_date
7241     FROM   pa_budget_lines bl
7242     WHERE  budget_version_id = p_budget_version_id;
7243 
7244 l_err_code    NUMBER;
7245 l_err_stage   VARCHAR2(2000);
7246 l_err_stack   VARCHAR2(2000);
7247 l_period_name PA_BUDGET_PERIODS_V.period_name%TYPE;
7248 l_start_date  DATE;
7249 l_end_date    DATE;
7250 
7251 BEGIN
7252 
7253 DELETE FROM pa_fp_cpy_periods_tmp;
7254 
7255 FOR i IN bl_periods LOOP
7256 
7257   l_period_name := NULL;
7258   l_start_date  := NULL;
7259   l_end_date    := NULL;
7260   l_err_code    := NULL;
7261   l_err_stage   := NULL;
7262   l_err_stack   := NULL;
7263 
7264   pa_budget_core.shift_periods(
7265                           x_start_period_date => i.start_date,
7266                           x_periods      => p_shift_periods,
7267                           x_period_name  => l_period_name,
7268                           x_period_type  => p_period_type,
7269                           x_start_date   => l_start_date,
7270                           x_end_date     => l_end_date,
7271                           x_err_code     => l_err_code,
7272                           x_err_stage    => l_err_stage,
7273                           x_err_stack    => l_err_stack);
7274   IF l_err_code <> 0 THEN
7275     IF P_PA_DEBUG_MODE = 'Y' THEN
7276        pa_debug.g_err_stage := 'Exception raised by pa_budget_core.shift_periods...';
7277        pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,5);
7278     END IF;
7279 
7280     PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
7281                          p_msg_name      => l_err_stage);
7282     RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
7283   END IF;
7284 
7285   If (l_period_name IS NOT NULL) then -- Added if condition for Bug 7556248  --Bug 9062715
7286   INSERT INTO pa_fp_cpy_periods_tmp
7287                       (PA_PERIOD_NAME
7288                       ,GL_PERIOD_NAME
7289                       ,PERIOD_NAME
7290                       ,START_DATE
7291                       ,END_DATE)
7292               VALUES
7293                       (decode(p_period_type,PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_P,i.period_name,'-99')
7294                       ,decode(p_period_type,PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_G,i.period_name,'-99')
7295                       ,l_period_name
7296                       ,l_start_date
7297                       ,l_end_date);
7298   END IF;
7299 END LOOP;
7300 
7301 END populate_cpy_periods_tmp;
7302 
7303   /*=========================================================================
7304    This api inserts budget lines for target using source budget lines. If
7305    the shift days are zero, this api will copy from source to target version
7306    without shifting any periods. If shift days are non-zero,then we shift
7307    periods according to the existing businees rules. project and projfunc
7308    currencies amounts copied as NULL and would be populated by
7309    convert_txn_currency api.
7310    This is an overloaded procedure as of now used during copying projects.
7311 
7312    21-Sep-04 Raja  Bug 3841942
7313                    During copy project flow, for non-time phased budgets
7314                    start and end date should be same as planning start and
7315                    end date of the resource assignment
7316 
7317                    2) If shift days i/p is not sufficient enough to cause
7318                       shift in periods changed the code to behave as if
7319                       shift days is zero.
7320    =========================================================================*/
7321 
7322   PROCEDURE Copy_Budget_Lines(
7323               p_source_project_id        IN  NUMBER
7324              ,p_target_project_id        IN  NUMBER
7325              ,p_source_plan_version_id   IN  NUMBER
7326              ,p_target_plan_version_id   IN  NUMBER
7327              ,p_shift_days               IN  NUMBER
7328              ,x_return_status            OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
7329              ,x_msg_count                OUT NOCOPY NUMBER --File.Sql.39 bug 4440895
7330              ,x_msg_data                 OUT NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
7331    AS
7332 
7333          l_msg_count          NUMBER :=0;
7334          l_data               VARCHAR2(2000);
7335          l_msg_data           VARCHAR2(2000);
7336          l_error_msg_code     VARCHAR2(2000);
7337          l_msg_index_out      NUMBER;
7338          l_return_status      VARCHAR2(2000);
7339          l_debug_mode         VARCHAR2(30);
7340 
7341          l_shift_days                   NUMBER;
7342          l_target_time_phased_code      pa_proj_fp_options.all_time_phased_code%TYPE;
7343          l_target_budget_entry_level    pa_proj_fp_options.all_fin_plan_level_code%TYPE; /* bug2726011 */
7344          l_target_proj_start_date       DATE; /*bug2726011*/
7345          l_target_proj_completion_date  DATE; /*bug2726011*/
7346 
7347          /* Bug# 2634726 */
7348 
7349          l_start_date                DATE;
7350          l_err_code                  NUMBER;
7351          l_err_stage                 VARCHAR2(2000);
7352          l_err_stack                 VARCHAR2(2000);
7353          l_periods                   NUMBER := 0;
7354 
7355          /* Bug# 2634726 */
7356 
7357    BEGIN
7358 
7359       x_msg_count := 0;
7360       x_return_status := FND_API.G_RET_STS_SUCCESS;
7361 IF P_PA_DEBUG_MODE = 'Y' THEN
7362       pa_debug.set_err_stack('PA_FP_COPY_FROM_PKG.Copy_Budget_Lines');
7363 END IF;
7364       fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
7365       l_debug_mode := NVL(l_debug_mode, 'Y');
7366 IF P_PA_DEBUG_MODE = 'Y' THEN
7367       pa_debug.set_process('PLSQL','LOG',l_debug_mode);
7368 END IF;
7369       -- Checking for all valid input parametrs
7370 
7371       IF P_PA_DEBUG_MODE = 'Y' THEN
7372            pa_debug.g_err_stage := 'Checking for valid parameters:';
7373            pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
7374       END IF;
7375 
7376       IF (p_source_plan_version_id IS NULL) OR
7377          (p_target_plan_version_id IS NULL) OR
7378          (p_source_project_id      IS NULL) OR
7379          (p_target_project_id      IS NULL)
7380       THEN
7381 
7382            IF P_PA_DEBUG_MODE = 'Y' THEN
7383                 pa_debug.g_err_stage := 'Source_plan='||p_source_plan_version_id;
7384                 pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,5);
7385                 pa_debug.g_err_stage := 'Target_plan'||p_target_plan_version_id;
7386                 pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,5);
7387                 pa_debug.g_err_stage := 'Source_project='||p_source_project_id;
7388                 pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,5);
7389                 pa_debug.g_err_stage := 'Target_project'||p_target_project_id;
7390                 pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,5);
7391            END IF;
7392 
7393            PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
7394                                 p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
7395            RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
7396 
7397       END IF;
7398 
7399       IF P_PA_DEBUG_MODE = 'Y' THEN
7400            pa_debug.g_err_stage := 'Parameter validation complete';
7401            pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
7402       END IF;
7403 
7404       --Make shift_days zero if passed as null
7405 
7406       l_shift_days := NVL(p_shift_days,0);
7407 
7408       IF P_PA_DEBUG_MODE = 'Y' THEN
7409            pa_debug.g_err_stage:='Shift_days ='|| l_shift_days;
7410            pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
7411       END IF;
7412 
7413       --Fetch the target versions time phased code
7414 
7415       l_target_time_phased_code := PA_FIN_PLAN_UTILS.get_time_phased_code(p_target_plan_version_id);
7416       l_target_budget_entry_level := PA_FIN_PLAN_UTILS.get_fin_plan_level_code(p_target_plan_version_id); /*bug2726011*/
7417 
7418       IF P_PA_DEBUG_MODE = 'Y' THEN
7419            pa_debug.g_err_stage:='Time Phased Code ='|| l_target_time_phased_code;
7420            pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
7421            pa_debug.g_err_stage:='Budget Entry Level ='|| l_target_budget_entry_level;
7422            pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
7423            pa_debug.g_err_stage:='Inserting into pa_budget_lines';
7424            pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
7425       END IF;
7426 
7427       IF P_PA_DEBUG_MODE = 'Y' THEN
7428            pa_debug.g_err_stage := 'Selecting project start and completion dates';
7429            pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
7430       END IF;
7431 
7432       SELECT start_date,
7433              completion_date
7434       INTO   l_target_proj_start_date,
7435              l_target_proj_completion_date
7436       FROM   pa_projects p
7437       WHERE  p.project_id = p_target_project_id;
7438 
7439       IF l_shift_days  <> 0
7440       AND l_target_time_phased_code IN (PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_G,
7441                                         PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_P)
7442       THEN
7443           BEGIN
7444 
7445                IF P_PA_DEBUG_MODE = 'Y' THEN
7446                     pa_debug.g_err_stage := 'Selecting project start date';
7447                     pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
7448                END IF;
7449 
7450                SELECT p.start_date
7451                INTO   l_start_date
7452                FROM   pa_projects p
7453                WHERE  p.project_id = p_source_project_id;
7454 
7455                IF l_start_date IS NULL THEN
7456 
7457                     IF P_PA_DEBUG_MODE = 'Y' THEN
7458                          pa_debug.g_err_stage := 'Selecting task mininum start date';
7459                          pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
7460                     END IF;
7461 
7462                     SELECt min(t.start_date)
7463                     INTO   l_start_date
7464                     FROM   pa_tasks t
7465                     WHERE  t.project_id = p_source_project_id;
7466 
7467                     IF l_start_date is NULL THEN
7468 
7469                          IF P_PA_DEBUG_MODE = 'Y' THEN
7470                               pa_debug.g_err_stage := 'Selecting budget lines minimum start date';
7471                               pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
7472                          END IF;
7473 
7474                          SELECT min(bl.start_date)
7475                          INTO   l_start_Date
7476                          FROM   pa_budget_lines bl
7477                          WHERE  bl.budget_version_id = p_source_plan_version_id;
7478 
7479                          -- If l_start_date is null after the above select it implies
7480                          -- there are no budget lines. So return immediately as nothing
7481                          -- needs to be copied
7482                          IF l_start_Date IS NULL THEN
7483                             pa_debug.reset_err_stack;
7484                             RETURN;
7485                          END IF;
7486 
7487                     END IF;  /* Mininum Task start date is null */
7488 
7489                END IF; /* Minimum Project start date is null */
7490           EXCEPTION
7491              WHEN OTHERS THEN
7492                  IF P_PA_DEBUG_MODE = 'Y' THEN
7493                       pa_debug.g_err_stage := 'Error while fetching start date ' || sqlerrm;
7494                       pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,5);
7495                  END IF;
7496                  RAISE;
7497           END;
7498 
7499           --Based on the shift_days check how much shift is required period wise
7500           pa_budget_core.get_periods(
7501                         x_start_date1 => l_start_date,
7502                         x_start_date2 => l_start_date + l_shift_days,
7503                         x_period_type => l_target_time_phased_code,
7504                         x_periods     => l_periods,
7505                         x_err_code    => l_err_code,
7506                         x_err_stage   => l_err_stage,
7507                         x_err_stack   => l_err_stack);
7508           IF l_err_code <> 0 THEN
7509                IF P_PA_DEBUG_MODE = 'Y' THEN
7510                     pa_debug.g_err_stage := 'Exception raised by pa_budget_core.get_periods...';
7511                     pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,5);
7512                END IF;
7513 
7514                PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
7515                                     p_msg_name      => l_err_stage);
7516                RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
7517           END IF;
7518       END IF; /* IF l_shift_days  <> 0 AND l_target_time_phased_code IN (PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_G,PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_P) */
7519 
7520       --If shift_days is zero or the timephasing is none.
7521 
7522       IF (l_shift_days = 0) OR  (l_periods = 0) OR
7523          (l_target_time_phased_code NOT IN (PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_G,
7524                                             PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_P))
7525       THEN
7526           /* If the time phasing is none, stamp the resource assignments' planning
7527              start and end dates for budget line start and end dates
7528            */
7529             --Display_quantity is being set in copy_version and copy_finplans_from_project api as well
7530 
7531            INSERT INTO PA_BUDGET_LINES(
7532                     budget_line_id              /* FPB2 */
7533                    ,budget_version_id           /* FPB2 */
7534                    ,resource_assignment_id
7535                    ,start_date
7536                    ,last_update_date
7537                    ,last_updated_by
7538                    ,creation_date
7539                    ,created_by
7540                    ,last_update_login
7541                    ,end_date
7542                    ,period_name
7543                    ,quantity
7544                    ,display_quantity  --IPM Arch Enhancement Bug 4865563.
7545                    ,raw_cost
7546                    ,burdened_cost
7547                    ,revenue
7548                    ,change_reason_code
7549                    ,description
7550                    ,attribute_category
7551                    ,attribute1
7552                    ,attribute2
7553                    ,attribute3
7554                    ,attribute4
7555                    ,attribute5
7556                    ,attribute6
7557                    ,attribute7
7558                    ,attribute8
7559                    ,attribute9
7560                    ,attribute10
7561                    ,attribute11
7562                    ,attribute12
7563                    ,attribute13
7564                    ,attribute14
7565                    ,attribute15
7566                    ,raw_cost_source
7567                    ,burdened_cost_source
7568                    ,quantity_source
7569                    ,revenue_source
7570                    ,pm_product_code
7571                    ,pm_budget_line_reference
7572                    ,cost_rejection_code
7573                    ,revenue_rejection_code
7574                    ,burden_rejection_code
7575                    ,other_rejection_code
7576                    ,code_combination_id
7577                    ,ccid_gen_status_code
7578                    ,ccid_gen_rej_message
7579                    ,request_id
7580                    ,borrowed_revenue
7581                    ,tp_revenue_in
7582                    ,tp_revenue_out
7583                    ,revenue_adj
7584                    ,lent_resource_cost
7585                    ,tp_cost_in
7586                    ,tp_cost_out
7587                    ,cost_adj
7588                    ,unassigned_time_cost
7589                    ,utilization_percent
7590                    ,utilization_hours
7591                    ,utilization_adj
7592                    ,capacity
7593                    ,head_count
7594                    ,head_count_adj
7595                    ,projfunc_currency_code
7596                    ,projfunc_cost_rate_type
7597                    ,projfunc_cost_exchange_rate
7598                    ,projfunc_cost_rate_date_type
7599                    ,projfunc_cost_rate_date
7600                    ,projfunc_rev_rate_type
7601                    ,projfunc_rev_exchange_rate
7602                    ,projfunc_rev_rate_date_type
7603                    ,projfunc_rev_rate_date
7604                    ,project_currency_code
7605                    ,project_cost_rate_type
7606                    ,project_cost_exchange_rate
7607                    ,project_cost_rate_date_type
7608                    ,project_cost_rate_date
7609                    ,project_raw_cost
7610                    ,project_burdened_cost
7611                    ,project_rev_rate_type
7612                    ,project_rev_exchange_rate
7613                    ,project_rev_rate_date_type
7614                    ,project_rev_rate_date
7615                    ,project_revenue
7616                    ,txn_raw_cost
7617                    ,txn_burdened_cost
7618                    ,txn_currency_code
7619                    ,txn_revenue
7620                    ,bucketing_period_code
7621                    -- 3/28/2004 FP M phase II Copy Project Impact
7622                    ,txn_standard_cost_rate
7623                    ,txn_cost_rate_override
7624                    ,cost_ind_compiled_set_id
7625                    ,txn_standard_bill_rate
7626                    ,txn_bill_rate_override
7627                    ,txn_markup_percent
7628                    ,txn_markup_percent_override
7629                    ,txn_discount_percentage
7630                    ,transfer_price_rate
7631                    ,init_quantity
7632                    ,init_quantity_source
7633                    ,init_raw_cost
7634                    ,init_burdened_cost
7635                    ,init_revenue
7636                    ,init_raw_cost_source
7637                    ,init_burdened_cost_source
7638                    ,init_revenue_source
7639                    ,project_init_raw_cost
7640                    ,project_init_burdened_cost
7641                    ,project_init_revenue
7642                    ,txn_init_raw_cost
7643                    ,txn_init_burdened_cost
7644                    ,txn_init_revenue
7645                    ,burden_cost_rate
7646                    ,burden_cost_rate_override
7647                    ,pc_cur_conv_rejection_code
7648                    ,pfc_cur_conv_rejection_code
7649 
7650                    )
7651            SELECT  pa_budget_lines_s.nextval            /* FPB2 */
7652                   ,p_target_plan_version_id             /* FPB2 */
7653                   ,pra.resource_assignment_id
7654                   ,DECODE(l_target_time_phased_code,
7655                           PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_N,nvl(l_target_proj_start_date , pbl.start_date + l_shift_days), --Bug 4739375,l_target_proj_start_date.--bug 3841942 l_target_proj_start_date,
7656                           pbl.start_date ) -- start_date
7657                   ,sysdate
7658                   ,fnd_global.user_id
7659                   ,sysdate
7660                   ,fnd_global.user_id
7661                   ,fnd_global.login_id
7662                  -- Commented by skkoppul for bug 7238582 and replaced this with the decode statement below
7663                  -- ,DECODE(l_target_time_phased_code,
7664                  --         PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_N, nvl(l_target_proj_completion_date, pbl.end_date + l_shift_days), --Bug 4739375,l_target_proj_completion_date,--bug 3841942 l_target_proj_completion_date,
7665                  --         pbl.end_date )   -- end_date
7666                  -- Default end date with start date if start date > end date else leave end date as is
7667                   ,DECODE(SIGN(DECODE(l_target_time_phased_code,PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_N,
7668                                       nvl(l_target_proj_start_date, pbl.start_date + l_shift_days), pbl.start_date)
7669                                -
7670                                DECODE(l_target_time_phased_code,PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_N,
7671                                       nvl(l_target_proj_completion_date, pbl.end_date + l_shift_days), pbl.end_date )),
7672                           1,
7673                           DECODE(l_target_time_phased_code,PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_N,
7674                                  nvl(l_target_proj_start_date, pbl.start_date + l_shift_days), pbl.start_date),
7675                           DECODE(l_target_time_phased_code,PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_N,
7676                                  nvl(l_target_proj_completion_date, pbl.end_date + l_shift_days), pbl.end_date))   -- end_date
7677                   ,pbl.period_name
7678                   ,pbl.quantity
7679                   ,pbl.display_quantity --IPM Arch Enhancement Bug 4865563.
7680                   ,NULL --raw_cost
7681                   ,NULL --burdened_cost
7682                   ,NULL --revenue
7683                   ,NULL --change_reason_code
7684                   ,pbl.description
7685                   ,pbl.attribute_category
7686                   ,pbl.attribute1
7687                   ,pbl.attribute2
7688                   ,pbl.attribute3
7689                   ,pbl.attribute4
7690                   ,pbl.attribute5
7691                   ,pbl.attribute6
7692                   ,pbl.attribute7
7693                   ,pbl.attribute8
7694                   ,pbl.attribute9
7695                   ,pbl.attribute10
7696                   ,pbl.attribute11
7697                   ,pbl.attribute12
7698                   ,pbl.attribute13
7699                   ,pbl.attribute14
7700                   ,pbl.attribute15
7701                   ,PA_FP_CONSTANTS_PKG.G_AMOUNT_SOURCE_MANUAL_M --raw_cost_souce
7702                   ,PA_FP_CONSTANTS_PKG.G_AMOUNT_SOURCE_MANUAL_M --burdened_cost_source
7703                   ,PA_FP_CONSTANTS_PKG.G_AMOUNT_SOURCE_MANUAL_M  --quantity_source
7704                   ,PA_FP_CONSTANTS_PKG.G_AMOUNT_SOURCE_MANUAL_M --revenue source
7705                   ,NULL --pm_product_code
7706                   ,NULL --pm_budget_line_reference
7707                   ,cost_rejection_code
7708                   ,revenue_rejection_code
7709                   ,burden_rejection_code
7710                   ,other_rejection_code
7711                   ,code_combination_id
7712                   ,ccid_gen_status_code
7713                   ,ccid_gen_rej_message
7714                   ,fnd_global.conc_request_id
7715                   ,borrowed_revenue
7716                   ,tp_revenue_in
7717                   ,tp_revenue_out
7718                   ,revenue_adj
7719                   ,lent_resource_cost
7720                   ,tp_cost_in
7721                   ,tp_cost_out
7722                   ,cost_adj
7723                   ,unassigned_time_cost
7724                   ,utilization_percent
7725                   ,utilization_hours
7726                   ,utilization_adj
7727                   ,capacity
7728                   ,head_count
7729                   ,head_count_adj
7730                   ,pbl.projfunc_currency_code
7731                   ,pbl.projfunc_cost_rate_type
7732                   ,pbl.projfunc_cost_exchange_rate
7733                   ,pbl.projfunc_cost_rate_date_type
7734                   ,pbl.projfunc_cost_rate_date
7735                   ,pbl.projfunc_rev_rate_type
7736                   ,pbl.projfunc_rev_exchange_rate
7737                   ,pbl.projfunc_rev_rate_date_type
7738                   ,pbl.projfunc_rev_rate_date
7739                   ,pbl.project_currency_code
7740                   ,pbl.project_cost_rate_type
7741                   ,pbl.project_cost_exchange_rate
7742                   ,pbl.project_cost_rate_date_type
7743                   ,pbl.project_cost_rate_date
7744                   ,NULL   --project_raw_cost
7745                   ,NULL   --project_burdened_cost
7746                   ,pbl.project_rev_rate_type
7747                   ,pbl.project_rev_exchange_rate
7748                   ,pbl.project_rev_rate_date_type
7749                   ,pbl.project_rev_rate_date
7750                   ,NULL  --project_revenue
7751                   ,txn_raw_cost
7752                   ,txn_burdened_cost
7753                   ,txn_currency_code
7754                   ,txn_revenue
7755                   ,NULL --bucketing_period_code
7756                    -- 3/28/2004 FP M phase II Copy Project Impact
7757                    ,NULL                                               -- txn_standard_cost_rate
7758                    ,nvl(txn_cost_rate_override,txn_standard_cost_rate) -- txn_cost_rate_override
7759                    ,cost_ind_compiled_set_id
7760                    ,NULL                                               -- txn_standard_bill_rate
7761                    ,nvl(txn_bill_rate_override,txn_standard_bill_rate) -- txn_bill_rate_override
7762                    ,NULL                                               -- txn_markup_percent
7763                    ,nvl(txn_markup_percent_override,txn_markup_percent)-- txn_markup_percent_override
7764                    ,txn_discount_percentage
7765                    ,transfer_price_rate
7766                    ,NULL                                               -- init_quantity
7767                    ,NULL                                               -- init_quantity_source
7768                    ,NULL                                               -- init_raw_cost
7769                    ,NULL                                               -- init_burdened_cost
7770                    ,NULL                                               -- init_revenue
7771                    ,NULL                                               -- init_raw_cost_source
7772                    ,NULL                                               -- init_burdened_cost_source
7773                    ,NULL                                               -- init_revenue_source
7774                    ,NULL                                               -- project_init_raw_cost
7775                    ,NULL                                               -- project_init_burdened_cost
7776                    ,NULL                                               -- project_init_revenue
7777                    ,NULL                                               -- txn_init_raw_cost
7778                    ,NULL                                               -- txn_init_burdened_cost
7779                    ,NULL                                               -- txn_init_revenue
7780                    ,NULL                                               -- burden_cost_rate
7781                    ,nvl(burden_cost_rate_override,burden_cost_rate)    -- burden_cost_rate_override
7782                    ,NULL                                               -- pc_cur_conv_rejection_code
7783                    ,NULL                                               -- pfc_cur_conv_rejection_code
7784 
7785            FROM   PA_BUDGET_LINES  pbl
7786                  ,pa_resource_assignments pra
7787            WHERE pbl.resource_assignment_id = pra.parent_assignment_id
7788            AND   pbl.budget_version_id = p_source_plan_version_id
7789            AND   pra.budget_version_id=p_target_plan_version_id;
7790 
7791 
7792        /* Start of Code Fix for Bug:4739375.*/
7793 
7794            IF (l_target_time_phased_code NOT IN(PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_G,
7795                                                 PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_P)) THEN
7796        /*
7797               UPDATE pa_resource_assignments pra
7798               SET (pra.planning_start_date , pra.planning_end_date , pra.sp_fixed_date)
7799                  = (SELECT least(pra.planning_start_date + l_shift_days,
7800                                 nvl(min(bl.start_date) , pra.planning_start_date + l_shift_days)),
7801                           greatest(pra.planning_end_date + l_shift_days,
7802                                    nvl(max(bl.end_date) , pra.planning_end_date + l_shift_days)),
7803                           greatest(least(pra.sp_fixed_date + l_shift_days , pra.planning_end_date + l_shift_days),
7804                                    pra.planning_start_date + l_shift_days)
7805                    FROM pa_budget_lines bl
7806                    WHERE bl.resource_assignment_id = pra.resource_assignment_id
7807                    )
7808               WHERE pra.budget_version_id = p_target_plan_version_id;
7809 	    */
7810             /* Bug 5846751: Commented the above update and added a new update to derive the Resource Assignment's
7811                dates similar to that of the Budget Lines start and end dates in the above INSERT statement. */
7812             update pa_resource_assignments pra
7813                set (pra.planning_start_date, pra.planning_end_date,pra.sp_fixed_date)
7814                    = ( select nvl(min(bl.start_date),nvl(l_target_proj_start_date, pra.planning_start_date + l_shift_days)),
7815                               -- skkoppul - bug 7626463 : commented the line below and added decode statement
7816                               -- Default end date with start date if start date > end date else leave end date as is
7817                               --nvl(min(bl.end_date),nvl(l_target_proj_completion_date, pra.planning_end_date + l_shift_days)),
7818                               DECODE(SIGN(nvl(min(bl.start_date),nvl(l_target_proj_start_date, pra.planning_start_date + l_shift_days))
7819                                           -
7820                                           nvl(min(bl.end_date),nvl(l_target_proj_completion_date, pra.planning_end_date + l_shift_days))),
7821                                      1,
7822                                      nvl(min(bl.start_date),nvl(l_target_proj_start_date, pra.planning_start_date + l_shift_days)),
7823                                      nvl(min(bl.end_date),nvl(l_target_proj_completion_date, pra.planning_end_date + l_shift_days))),   -- end_date
7824                               decode(pra.sp_fixed_date,null,null,nvl(min(bl.start_date),nvl(l_target_proj_start_date, pra.sp_fixed_date + l_shift_days)))
7825                        from   pa_budget_lines bl
7826                        where  bl.resource_assignment_id = pra.resource_assignment_id
7827                      )
7828              where  pra.budget_version_id = p_target_plan_version_id;
7829 
7830            END IF;
7831 
7832        /*End of Code Fix for Bug:4739375*/
7833 
7834 
7835        ELSE
7836 
7837            /* Start of code fix for bug# 2634726 */
7838 
7839            -- Call a private api to populate pa_fp_cpy_periods_tmp table
7840            populate_cpy_periods_tmp(p_budget_version_id => p_source_plan_version_id,
7841                                     p_period_type       => l_target_time_phased_code,
7842                                     p_shift_periods     => l_periods);
7843 
7844            -- Shift the pa_periods by l_periods
7845            -- Bug# 2634726- The two individual inserts which read pa_periods or gl_period_statuses based
7846            -- on the l_target_time_phased_code condition have been merged into one insert which reads
7847            -- pa_fp_cpy_periods_tmp
7848 
7849            INSERT INTO PA_BUDGET_LINES(
7850                     budget_line_id              /* FPB2 */
7851                    ,budget_version_id           /* FPB2 */
7852                    ,resource_assignment_id
7853                    ,start_date
7854                    ,last_update_date
7855                    ,last_updated_by
7856                    ,creation_date
7857                    ,created_by
7858                    ,last_update_login
7859                    ,end_date
7860                    ,period_name
7861                    ,quantity
7862                    ,display_quantity  --IPM Arch Enhancement Bug 4865563
7863                    ,raw_cost
7864                    ,burdened_cost
7865                    ,revenue
7866                    ,change_reason_code
7867                    ,description
7868                    ,attribute_category
7869                    ,attribute1
7870                    ,attribute2
7871                    ,attribute3
7872                    ,attribute4
7873                    ,attribute5
7874                    ,attribute6
7875                    ,attribute7
7876                    ,attribute8
7877                    ,attribute9
7878                    ,attribute10
7879                    ,attribute11
7880                    ,attribute12
7881                    ,attribute13
7882                    ,attribute14
7883                    ,attribute15
7884                    ,raw_cost_source
7885                    ,burdened_cost_source
7886                    ,quantity_source
7887                    ,revenue_source
7888                    ,pm_product_code
7889                    ,pm_budget_line_reference
7890                    ,cost_rejection_code
7891                    ,revenue_rejection_code
7892                    ,burden_rejection_code
7893                    ,other_rejection_code
7894                    ,code_combination_id
7895                    ,ccid_gen_status_code
7896                    ,ccid_gen_rej_message
7897                    ,request_id
7898                    ,borrowed_revenue
7899                    ,tp_revenue_in
7900                    ,tp_revenue_out
7901                    ,revenue_adj
7902                    ,lent_resource_cost
7903                    ,tp_cost_in
7904                    ,tp_cost_out
7905                    ,cost_adj
7906                    ,unassigned_time_cost
7907                    ,utilization_percent
7908                    ,utilization_hours
7909                    ,utilization_adj
7910                    ,capacity
7911                    ,head_count
7912                    ,head_count_adj
7913                    ,projfunc_currency_code
7914                    ,projfunc_cost_rate_type
7915                    ,projfunc_cost_exchange_rate
7916                    ,projfunc_cost_rate_date_type
7917                    ,projfunc_cost_rate_date
7918                    ,projfunc_rev_rate_type
7919                    ,projfunc_rev_exchange_rate
7920                    ,projfunc_rev_rate_date_type
7921                    ,projfunc_rev_rate_date
7922                    ,project_currency_code
7923                    ,project_cost_rate_type
7924                    ,project_cost_exchange_rate
7925                    ,project_cost_rate_date_type
7926                    ,project_cost_rate_date
7927                    ,project_raw_cost
7928                    ,project_burdened_cost
7929                    ,project_rev_rate_type
7930                    ,project_rev_exchange_rate
7931                    ,project_rev_rate_date_type
7932                    ,project_rev_rate_date
7933                    ,project_revenue
7934                    ,txn_raw_cost
7935                    ,txn_burdened_cost
7936                    ,txn_currency_code
7937                    ,txn_revenue
7938                    ,bucketing_period_code
7939                    -- 3/28/2004 FP M phase II Copy Project Impact
7940                    ,txn_standard_cost_rate
7941                    ,txn_cost_rate_override
7942                    ,cost_ind_compiled_set_id
7943                    ,txn_standard_bill_rate
7944                    ,txn_bill_rate_override
7945                    ,txn_markup_percent
7946                    ,txn_markup_percent_override
7947                    ,txn_discount_percentage
7948                    ,transfer_price_rate
7949                    ,init_quantity
7950                    ,init_quantity_source
7951                    ,init_raw_cost
7952                    ,init_burdened_cost
7953                    ,init_revenue
7954                    ,init_raw_cost_source
7955                    ,init_burdened_cost_source
7956                    ,init_revenue_source
7957                    ,project_init_raw_cost
7958                    ,project_init_burdened_cost
7959                    ,project_init_revenue
7960                    ,txn_init_raw_cost
7961                    ,txn_init_burdened_cost
7962                    ,txn_init_revenue
7963                    ,burden_cost_rate
7964                    ,burden_cost_rate_override
7965                    ,pc_cur_conv_rejection_code
7966                    ,pfc_cur_conv_rejection_code
7967                    )
7968            SELECT  pa_budget_lines_s.nextval            /* FPB2 */
7969                   ,p_target_plan_version_id             /* FPB2 */
7970                   ,pra.resource_assignment_id
7971                   ,pptmp.start_date
7972                   ,sysdate
7973                   ,fnd_global.user_id
7974                   ,sysdate
7975                   ,fnd_global.user_id
7976                   ,fnd_global.login_id
7977                   ,pptmp.end_date
7978                   ,pptmp.period_name
7979                   ,pbl.quantity
7980                   ,pbl.display_quantity    --IPM Arch Enhancement Bug 4865563
7981                   ,NULL --raw_cost
7982                   ,NULL --burdened_cost
7983                   ,NULL --revenue
7984                   ,NULL --change_reason_code
7985                   ,pbl.description
7986                   ,pbl.attribute_category
7987                   ,pbl.attribute1
7988                   ,pbl.attribute2
7989                   ,pbl.attribute3
7990                   ,pbl.attribute4
7991                   ,pbl.attribute5
7992                   ,pbl.attribute6
7993                   ,pbl.attribute7
7994                   ,pbl.attribute8
7995                   ,pbl.attribute9
7996                   ,pbl.attribute10
7997                   ,pbl.attribute11
7998                   ,pbl.attribute12
7999                   ,pbl.attribute13
8000                   ,pbl.attribute14
8001                   ,pbl.attribute15
8002                   ,PA_FP_CONSTANTS_PKG.G_AMOUNT_SOURCE_MANUAL_M --raw_cost_souce
8003                   ,PA_FP_CONSTANTS_PKG.G_AMOUNT_SOURCE_MANUAL_M --burdened_cost_source
8004                   ,PA_FP_CONSTANTS_PKG.G_AMOUNT_SOURCE_MANUAL_M  --quantity_source
8005                   ,PA_FP_CONSTANTS_PKG.G_AMOUNT_SOURCE_MANUAL_M --revenue source
8006                   ,NULL --pm_product_code
8007                   ,NULL --pm_budget_line_reference
8008                   ,cost_rejection_code
8009                   ,revenue_rejection_code
8010                   ,burden_rejection_code
8011                   ,other_rejection_code
8012                   ,code_combination_id
8013                   ,ccid_gen_status_code
8014                   ,ccid_gen_rej_message
8015                   ,fnd_global.conc_request_id
8016                   ,borrowed_revenue
8017                   ,tp_revenue_in
8018                   ,tp_revenue_out
8019                   ,revenue_adj
8020                   ,lent_resource_cost
8021                   ,tp_cost_in
8022                   ,tp_cost_out
8023                   ,cost_adj
8024                   ,unassigned_time_cost
8025                   ,utilization_percent
8026                   ,utilization_hours
8027                   ,utilization_adj
8028                   ,capacity
8029                   ,head_count
8030                   ,head_count_adj
8031                   ,pbl.projfunc_currency_code
8032                   ,pbl.projfunc_cost_rate_type
8033                   ,pbl.projfunc_cost_exchange_rate
8034                   ,pbl.projfunc_cost_rate_date_type
8035                   ,pbl.projfunc_cost_rate_date
8036                   ,pbl.projfunc_rev_rate_type
8037                   ,pbl.projfunc_rev_exchange_rate
8038                   ,pbl.projfunc_rev_rate_date_type
8039                   ,pbl.projfunc_rev_rate_date
8040                   ,pbl.project_currency_code
8041                   ,pbl.project_cost_rate_type
8042                   ,pbl.project_cost_exchange_rate
8043                   ,pbl.project_cost_rate_date_type
8044                   ,pbl.project_cost_rate_date
8045                   ,NULL   --project_raw_cost
8046                   ,NULL   --project_burdened_cost
8047                   ,pbl.project_rev_rate_type
8048                   ,pbl.project_rev_exchange_rate
8049                   ,pbl.project_rev_rate_date_type
8050                   ,pbl.project_rev_rate_date
8051                   ,NULL  --project_revenue
8052                   ,txn_raw_cost
8053                   ,txn_burdened_cost
8054                   ,txn_currency_code
8055                   ,txn_revenue
8056                   ,NULL --bucketing_period_code
8057                   ,NULL                                               -- txn_standard_cost_rate
8058                   ,nvl(txn_cost_rate_override,txn_standard_cost_rate) -- txn_cost_rate_override
8059                   ,cost_ind_compiled_set_id
8060                   ,NULL                                               -- txn_standard_bill_rate
8061                   ,nvl(txn_bill_rate_override,txn_standard_bill_rate) -- txn_bill_rate_override
8062                   ,NULL                                               -- txn_markup_percent
8063                   ,nvl(txn_markup_percent_override,txn_markup_percent)-- txn_markup_percent_override
8064                   ,txn_discount_percentage
8065                   ,transfer_price_rate
8066                   ,NULL                                               -- init_quantity
8067                   ,NULL                                               -- init_quantity_source
8068                   ,NULL                                               -- init_raw_cost
8069                   ,NULL                                               -- init_burdened_cost
8070                   ,NULL                                               -- init_revenue
8071                   ,NULL                                               -- init_raw_cost_source
8072                   ,NULL                                               -- init_burdened_cost_source
8073                   ,NULL                                               -- init_revenue_source
8074                   ,NULL                                               -- project_init_raw_cost
8075                   ,NULL                                               -- project_init_burdened_cost
8076                   ,NULL                                               -- project_init_revenue
8077                   ,NULL                                               -- txn_init_raw_cost
8078                   ,NULL                                               -- txn_init_burdened_cost
8079                   ,NULL                                               -- txn_init_revenue
8080                   ,NULL                                               -- burden_cost_rate
8081                   ,nvl(burden_cost_rate_override,burden_cost_rate)    -- burden_cost_rate_override
8082                   ,NULL                                               -- pc_cur_conv_rejection_code
8083                   ,NULL                                               -- pfc_cur_conv_rejection_code
8084            FROM   PA_BUDGET_LINES  pbl
8085                   ,pa_resource_assignments pra
8086                   ,PA_FP_CPY_PERIODS_TMP pptmp /* Bug# 2634726 */
8087            WHERE pra.parent_assignment_id = pbl.resource_assignment_id
8088            AND   decode(l_target_time_phased_code,
8089                     PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_P, pptmp.pa_period_name,
8090                     PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_G, pptmp.gl_period_name) = pbl.period_name
8091            AND   pbl.budget_version_id = p_source_plan_version_id
8092            AND   pra.budget_version_id=p_target_plan_version_id;
8093 
8094            /* End of code fix for bug# 2634726 */
8095 
8096             -- Bug 3841942 this update is required to make sure that planning start/end dates
8097             -- encompass budget line start and end dates after the shift
8098             --Bug 4200168, The logic for deriving sp fixed date is transferred to this previous. Previously
8099             --it was there in copy resource assignments.
8100             update pa_resource_assignments pra
8101             set    (pra.planning_start_date, pra.planning_end_date,pra.sp_fixed_date)
8102                    = ( select least(pra.planning_start_date+l_shift_days,
8103                                        nvl(min(bl.start_date),pra.planning_start_date+l_shift_days)),
8104                               greatest(pra.planning_end_date+l_shift_days,
8105                                        nvl(max(bl.end_date),pra.planning_end_date+l_shift_days)),
8106                               greatest(least(pra.sp_fixed_date + l_shift_days, pra.planning_end_date+ l_shift_days),
8107                                         pra.planning_start_date+ l_shift_days)
8108                        from   pa_budget_lines bl
8109                        where  bl.resource_assignment_id = pra.resource_assignment_id
8110                      )
8111             where  pra.budget_version_id = p_target_plan_version_id;
8112 
8113       END IF; --l_target_time_phased_code
8114 
8115       IF P_PA_DEBUG_MODE = 'Y' THEN
8116            pa_debug.g_err_stage:='Exiting Copy_Budget_Lines';
8117            pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
8118            pa_debug.reset_err_stack;
8119      END IF;
8120  EXCEPTION
8121 
8122       WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
8123            l_msg_count := FND_MSG_PUB.count_msg;
8124            IF l_msg_count = 1 THEN
8125                PA_INTERFACE_UTILS_PUB.get_messages
8126                    (p_encoded        => FND_API.G_TRUE
8127                     ,p_msg_index      => 1
8128                     ,p_msg_count      => l_msg_count
8129                     ,p_msg_data       => l_msg_data
8130                     ,p_data           => l_data
8131                     ,p_msg_index_out  => l_msg_index_out);
8132 
8133                x_msg_data := l_data;
8134                x_msg_count := l_msg_count;
8135            ELSE
8136                x_msg_count := l_msg_count;
8137            END IF;
8138 
8139            x_return_status := FND_API.G_RET_STS_ERROR;
8140            IF P_PA_DEBUG_MODE = 'Y' THEN
8141                 pa_debug.g_err_stage:='Invalid arguments passed';
8142                 pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,5);
8143                 pa_debug.reset_err_stack;
8144           END IF;
8145            RAISE;
8146 
8147       WHEN Others THEN
8148 
8149            x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8150            x_msg_count     := 1;
8151            x_msg_data      := SQLERRM;
8152            FND_MSG_PUB.add_exc_msg( p_pkg_name=> 'PA_FP_COPY_FROM_PKG'
8153                                ,p_procedure_name  => 'COPY_BUDGET_LINES');
8154            IF P_PA_DEBUG_MODE = 'Y' THEN
8155                 pa_debug.g_err_stage:='Unexpected error'||SQLERRM;
8156                 pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,6);
8157                 pa_debug.reset_err_stack;
8158 	   END IF;
8159            RAISE;
8160 
8161 END Copy_Budget_Lines;
8162 
8163 /*==================================================================
8164    This api is called during copy_project for the creation of new
8165    period profiles for the target project. The api creates new current
8166    period profiles for the target project using the current period
8167    profiles of the source project.
8168  ===================================================================*/
8169 
8170 PROCEDURE Copy_Current_Period_Profiles
8171    (  p_target_project_id     IN   pa_projects.project_id%TYPE
8172      ,p_source_project_id     IN   pa_projects.project_id%TYPE
8173      ,p_shift_days            IN   NUMBER
8174      ,x_return_status         OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
8175      ,x_msg_count             OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
8176      ,x_msg_data              OUT  NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
8177 AS
8178 
8179 l_msg_count                     NUMBER := 0;
8180 l_data                          VARCHAR2(2000);
8181 l_msg_data                      VARCHAR2(2000);
8182 l_msg_index_out                 NUMBER;
8183 l_return_status                 VARCHAR2(2000);
8184 
8185 l_target_period_profile_id      pa_budget_versions.period_profile_id%TYPE;
8186 l_source_period_profile_id      pa_budget_versions.period_profile_id%TYPE;
8187 l_dummy                         NUMBER;
8188 
8189 CURSOR current_period_profiles_cur IS
8190 SELECT period_profile_id
8191 FROM   pa_proj_period_profiles pp
8192 WHERE  pp.project_id = p_source_project_id
8193 AND    pp.current_flag = 'Y';
8194 
8195 BEGIN
8196 
8197       x_msg_count := 0;
8198       x_return_status := FND_API.G_RET_STS_SUCCESS;
8199 
8200       IF p_pa_debug_mode = 'Y' THEN
8201            pa_debug.set_err_stack('PA_FP_COPY_FROM_PKG.Copy_Current_Period_Profiles');
8202            pa_debug.set_process('PLSQL','LOG',p_pa_debug_mode);
8203       END IF;
8204 
8205       -- Check for NOT NULL parameters
8206 
8207       IF (p_source_project_id IS NULL)  OR
8208          (p_target_project_id IS NULL)  OR
8209          (p_shift_days        IS NULL )
8210       THEN
8211           IF p_pa_debug_mode = 'Y' THEN
8212               pa_debug.g_err_stage := 'Source_project ='||p_source_project_id;
8213               pa_debug.write(g_module_name,pa_debug.g_err_stage,5);
8214               pa_debug.g_err_stage := 'Target_project ='||p_target_project_id;
8215               pa_debug.write(g_module_name,pa_debug.g_err_stage,5);
8216               pa_debug.g_err_stage := 'p_shift_days ='||p_shift_days;
8217               pa_debug.write(g_module_name,pa_debug.g_err_stage,5);
8218           END IF;
8219           PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
8220                                p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
8221           RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
8222 
8223       END IF;
8224 
8225       -- For the copied budget versions, create a new period profile
8226       -- by shifting existing period profile
8227 
8228       OPEN current_period_profiles_cur;
8229       LOOP
8230 
8231           FETCH current_period_profiles_cur INTO l_source_period_profile_id;
8232           EXIT WHEN current_period_profiles_cur%NOTFOUND;
8233 
8234           /* Bug 2987076 - Period profile would not be created if periods are not available for the shifted dates */
8235 
8236           PA_FP_COPY_FROM_PKG.Get_Create_Shifted_PD_Profile (
8237                    p_target_project_id            =>  p_target_project_id
8238                   ,p_source_period_profile_id     =>  l_source_period_profile_id
8239                   ,p_shift_days                   =>  p_shift_days
8240                   ,x_target_period_profile_id     =>  l_target_period_profile_id
8241                   ,x_return_status                =>  l_return_status
8242                   ,x_msg_count                    =>  l_msg_count
8243                   ,x_msg_data                     =>  l_msg_data    );
8244 
8245           IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
8246               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
8247           END IF;
8248 
8249       END LOOP;
8250       CLOSE current_period_profiles_cur;
8251 
8252       IF p_pa_debug_mode = 'Y' THEN
8253               pa_debug.g_err_stage:= 'Exiting Copy_Current_Period_Profiles';
8254               pa_debug.write(g_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
8255               pa_debug.reset_err_stack;
8256       END IF;
8257 
8258   EXCEPTION
8259 
8260      WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
8261 
8262            x_return_status := FND_API.G_RET_STS_ERROR;
8263            l_msg_count := FND_MSG_PUB.count_msg;
8264            IF l_msg_count = 1 THEN
8265                 PA_INTERFACE_UTILS_PUB.get_messages
8266                       (p_encoded        => FND_API.G_TRUE
8267                       ,p_msg_index      => 1
8268                       ,p_msg_count      => l_msg_count
8269                       ,p_msg_data       => l_msg_data
8270                       ,p_data           => l_data
8271                       ,p_msg_index_out  => l_msg_index_out);
8272                 x_msg_data := l_data;
8273                 x_msg_count := l_msg_count;
8274            ELSE
8275                 x_msg_count := l_msg_count;
8276            END IF;
8277            IF p_pa_debug_mode = 'Y' THEN
8278                    pa_debug.reset_err_stack;
8279            END IF;
8280            RAISE;
8281 
8282    WHEN others THEN
8283 
8284           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8285           x_msg_count     := 1;
8286           x_msg_data      := SQLERRM;
8287           FND_MSG_PUB.add_exc_msg
8288                           ( p_pkg_name        => 'PA_FP_COPY_FROM_PKG'
8289                            ,p_procedure_name  => 'Copy_Current_Period_Profiles'
8290                            ,p_error_text      => sqlerrm);
8291           IF p_pa_debug_mode = 'Y' THEN
8292               pa_debug.g_err_stage:= 'Unexpected Error'||SQLERRM;
8293               pa_debug.write(g_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
8294               pa_debug.reset_err_stack;
8295           END IF;
8296           RAISE;
8297 
8298 END Copy_Current_Period_Profiles;
8299 
8300 /*==============================================================================
8301    This api creates new period profile for the new project created during
8302    copy_project. The api does the following :
8303 
8304    a -> source profile duration
8305    b -> shift_days
8306 
8307    1) The source period profile is shifted by p_shift_days
8308    2) If the shifted period profile goes ahead of the target project end date then,
8309        2.1) the project end date is set as period profile end date
8310        2.2) the profile start date is fetched by moving 'a' periods back.
8311        2.3) if the profile start date goes beyond the target project start date ,
8312             we make the target project start date as period profile start date.
8313  ================================================================================*/
8314 
8315 PROCEDURE Get_Create_Shifted_PD_Profile
8316    (  p_target_project_id               IN      pa_projects.project_id%TYPE
8317      ,p_source_period_profile_id        IN      pa_proj_period_profiles.period_profile_id%TYPE
8318      ,p_shift_days                      IN      NUMBER
8319      ,x_target_period_profile_id        OUT     NOCOPY pa_proj_period_profiles.period_profile_id%TYPE --File.Sql.39 bug 4440895
8320      ,x_return_status                   OUT     NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
8321      ,x_msg_count                       OUT     NOCOPY NUMBER --File.Sql.39 bug 4440895
8322      ,x_msg_data                        OUT     NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
8323 AS
8324 
8325 l_msg_count                     NUMBER := 0;
8326 l_data                          VARCHAR2(2000);
8327 l_msg_data                      VARCHAR2(2000);
8328 l_msg_index_out                 NUMBER;
8329 l_return_status                 VARCHAR2(2000);
8330 l_index                         NUMBER;
8331 
8332 l_period_set_name               gl_sets_of_books.period_set_name%TYPE;
8333 l_accounted_period_type         gl_sets_of_books.accounted_period_type%TYPE;
8334 l_pa_period_type                pa_implementations.pa_period_type%TYPE;
8335 
8336 l_number_of_periods             NUMBER;
8337 l_plan_start_date               pa_periods.start_date%TYPE;
8338 l_plan_end_date                 pa_periods.end_date%TYPE;
8339 
8340 l_start_period                  pa_periods.period_name%TYPE;
8341 l_start_period_start_date       pa_periods.start_date%TYPE;
8342 l_start_period_end_date         pa_periods.end_date%TYPE;
8343 
8344 l_end_period                    pa_periods.period_name%TYPE;
8345 l_end_period_start_date         pa_periods.start_date%TYPE;
8346 l_end_period_end_date           pa_periods.end_date%TYPE;
8347 
8348 l_target_proj_gl_start_period   gl_periods.period_name%TYPE;
8349 l_target_proj_gl_end_period     gl_periods.period_name%TYPE;
8350 l_target_proj_pa_start_period   pa_periods.period_name%TYPE;
8351 l_target_proj_pa_end_period     pa_periods.period_name%TYPE;
8352 
8353 l_target_proj_start_period      pa_periods.period_name%TYPE;
8354 l_target_proj_end_period        pa_periods.period_name%TYPE;
8355 
8356 l_dummy_flag                    VARCHAR2(1);
8357 l_dummy1                        VARCHAR2(30);
8358 l_dummy2                        VARCHAR2(30);
8359 
8360 CURSOR  source_profile_info_cur (c_period_profile_id pa_proj_period_profiles.period_profile_id%TYPE)
8361 IS
8362 SELECT   number_of_periods
8363         ,plan_period_type
8364         ,period_profile_type
8365         ,period_name1          --  profile_start_period
8366         ,profile_end_period_name
8367         ,period1_start_date    -- profile start date
8368         ,current_flag
8369 FROM    pa_proj_period_profiles
8370 WHERE   period_profile_id  = c_period_profile_id ;
8371 
8372 source_profile_info_rec  source_profile_info_cur%ROWTYPE;
8373 
8374 /* Bug# 2987076 */
8375 l_create_period_profile         VARCHAR2(1) := 'Y';
8376 l_prj_start_date                VARCHAR2(12);
8377 l_prj_end_date                  VARCHAR2(12);
8378 /* Bug# 2987076 */
8379 
8380 BEGIN
8381 
8382       x_msg_count := 0;
8383       x_return_status := FND_API.G_RET_STS_SUCCESS;
8384 IF P_PA_DEBUG_MODE = 'Y' THEN
8385       pa_debug.set_err_stack('PA_FP_COPY_FROM_PKG.Get_Create_Shifted_PD_Profile');
8386       pa_debug.set_process('PLSQL','LOG',p_pa_debug_mode);
8387 END IF;
8388       -- Check for NOT NULL parameters
8389 
8390       IF (p_target_project_id IS NULL)  OR
8391          (p_source_period_profile_id IS NULL) OR
8392          (p_shift_days IS NULL)
8393       THEN
8394           IF p_pa_debug_mode = 'Y' THEN
8395                 pa_debug.g_err_stage:= 'p_target_project_id = '|| p_target_project_id;
8396                 pa_debug.write(g_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
8397                 pa_debug.g_err_stage:= 'p_source_period_profile_id = '|| p_source_period_profile_id;
8398                 pa_debug.write(g_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
8399                 pa_debug.g_err_stage:= 'p_shift_days = '|| p_shift_days;
8400                 pa_debug.write(g_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
8401                 pa_debug.g_err_stage:= 'Invalid Arguments Passed';
8402                 pa_debug.write(g_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
8403           END IF;
8404           PA_UTILS.ADD_MESSAGE
8405                  (p_app_short_name => 'PA',
8406                   p_msg_name       => 'PA_FP_INV_PARAM_PASSED');
8407           RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
8408       END IF;
8409 
8410       -- Check for the target project if a period profile has been already created
8411       -- for the passed source period profile
8412 
8413       IF NVL(g_source_period_profile_tbl.last,0) > 0 then
8414               FOR i IN g_source_period_profile_tbl.first .. g_source_period_profile_tbl.last
8415               LOOP
8416                    -- If found then return the target period profile to the calling program
8417 
8418                    IF  g_source_period_profile_tbl(i) = p_source_period_profile_id
8419                    THEN
8420                        x_target_period_profile_id := g_target_period_profile_tbl(i);
8421 IF P_PA_DEBUG_MODE = 'Y' THEN
8422                        pa_debug.reset_err_stack;
8423 END IF;
8424                        RETURN;
8425                    END IF;
8426               END LOOP;
8427       END IF;
8428 
8429       -- If no corresponding target period profile has been already created,
8430       -- then create one.
8431 
8432       -- Fetch the source period profile details
8433       IF p_pa_debug_mode = 'Y' THEN
8434               pa_debug.g_err_stage:= 'Opening source_profile_info_cur';
8435               pa_debug.write(g_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
8436       END IF;
8437 
8438       OPEN source_profile_info_cur(p_source_period_profile_id);
8439       FETCH source_profile_info_cur INTO source_profile_info_rec;
8440       CLOSE source_profile_info_cur;
8441 
8442       BEGIN
8443               -- Fetching the details required to create period profile for the target project
8444 
8445               SELECT  b.period_set_name
8446                      ,DECODE(source_profile_info_rec.plan_period_type,
8447                              PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_PA ,pa_period_type,
8448                              PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_GL ,accounted_period_type) --accounted_period_type
8449                      ,DECODE(source_profile_info_rec.plan_period_type,
8450                              PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_PA ,pa_period_type,
8451                              PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_GL ,NULL) --pa_period_type
8452               INTO   l_period_set_name
8453                      ,l_accounted_period_type
8454                      ,l_pa_period_type
8455               FROM   pa_projects_all    p
8456                      -- MOAC changes
8457                      -- replaced with pa_implementations_all table.
8458                      --,pa_implementations  a
8459                      ,pa_implementations_all  a
8460                      ,gl_sets_of_books  b
8461               WHERE  p.project_id = p_target_project_id
8462               -- MOAC changes
8463               -- removed the nvl around the org_id.
8464               -- AND    NVL(p.Org_Id,-99) = NVL(a.Org_Id,-99)
8465               AND    p.Org_Id =a.Org_Id
8466               AND    a.set_of_books_id = b.set_of_books_id;
8467       EXCEPTION
8468            WHEN OTHERS THEN
8469                 IF p_pa_debug_mode = 'Y' THEN
8470                         pa_debug.g_err_stage:= 'Unexp Error while fetching the accounted period type||SQLERRM';
8471                         pa_debug.write(g_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
8472                 END IF;
8473                 RAISE;
8474       END;
8475 
8476 
8477       -- Fetch the target project start and completion dates
8478       -- Fetch the GL/PA periods into which the target project start and completion dates fall.
8479       IF p_pa_debug_mode = 'Y' THEN
8480              pa_debug.g_err_stage:= 'Calling Pa_Prj_Period_Profile_Utils.Get_Prj_Defaults';
8481              pa_debug.write(g_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
8482       END IF;
8483 
8484       Pa_Prj_Period_Profile_Utils.Get_Prj_Defaults(
8485                p_project_id                 =>   p_target_project_id
8486               ,p_info_flag                  =>   NULL
8487               ,p_create_defaults            =>   'N'
8488               ,x_gl_start_period            =>   l_target_proj_gl_start_period
8489               ,x_gl_end_period              =>   l_target_proj_gl_end_period
8490               ,x_gl_start_Date              =>   l_dummy1     -- varchar2
8491               ,x_pa_start_period            =>   l_target_proj_pa_start_period
8492               ,x_pa_end_period              =>   l_target_proj_pa_end_period
8493               ,x_pa_start_date              =>   l_dummy2      -- varchar2
8494               ,x_plan_version_exists_flag   =>   l_dummy_flag
8495               ,x_prj_start_date             =>   l_prj_start_date
8496               ,x_prj_end_date               =>   l_prj_end_date);
8497 
8498       IF source_profile_info_rec.plan_period_type = 'PA' THEN
8499 
8500             l_target_proj_start_period :=  l_target_proj_pa_start_period;
8501             l_target_proj_end_period   :=  l_target_proj_pa_end_period;
8502 
8503       ELSIF source_profile_info_rec.plan_period_type = 'GL' THEN
8504 
8505             l_target_proj_start_period :=  l_target_proj_gl_start_period;
8506             l_target_proj_end_period   :=  l_target_proj_gl_end_period;
8507 
8508       END IF;
8509 
8510       -- Shift the source period profile start date by p_shift_days input.
8511       IF p_pa_debug_mode = 'Y' THEN
8512               pa_debug.g_err_stage:= 'Shifting source period profile by shift days i/p';
8513               pa_debug.write(g_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
8514       END IF;
8515 
8516       BEGIN /* Bug 2987076 - If shifted period is not available then return without creating the period profile */
8517 
8518            IF    source_profile_info_rec.plan_period_type = 'PA' THEN
8519 
8520                     SELECT period_name
8521                            ,start_date
8522                     INTO   l_start_period
8523                            ,l_start_period_start_date
8524                     FROM   pa_periods
8525                     WHERE  TRUNC(source_profile_info_rec.period1_start_date + p_shift_days) BETWEEN start_date AND end_date;
8526 
8527            ELSIF  source_profile_info_rec.plan_period_type = 'GL' THEN
8528 
8529                     SELECT period_name
8530                            ,start_date
8531                     INTO   l_start_period
8532                            ,l_start_period_start_date
8533                     FROM   gl_period_statuses g
8534                           ,pa_implementations i
8535                     WHERE  g.application_id = pa_period_process_pkg.application_id
8536                     AND    g.set_of_books_id = i.set_of_books_id
8537                     AND    g.adjustment_period_flag = 'N'
8538                     AND    TRUNC(source_profile_info_rec.period1_start_date + p_shift_days) BETWEEN start_date AND end_date;
8539 
8540            END IF;
8541       EXCEPTION
8542       WHEN NO_DATA_FOUND THEN
8543 
8544            IF p_pa_debug_mode = 'Y' THEN
8545                    pa_debug.g_err_stage:= 'Periods not available in system for the shifted dates!!!!';
8546                    pa_debug.write(g_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
8547                    pa_debug.g_err_stage:= 'Period profile is not created.....';
8548                    pa_debug.write(g_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
8549            END IF;
8550 
8551            l_create_period_profile := 'N';
8552 
8553       END;
8554 
8555       IF l_create_period_profile = 'Y' THEN /* Bug 2987076 */
8556 
8557            -- Fetch the target profile end period by shifting the fetching the target profile start period forward
8558            -- by the number of periods of the source period profile
8559            IF p_pa_debug_mode = 'Y' THEN
8560                    pa_debug.g_err_stage:= 'Calling get_Shifted_period';
8561                    pa_debug.write(g_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
8562            END IF;
8563 
8564            PA_FIN_PLAN_UTILS.Get_Shifted_Period (
8565                      p_period_name                   =>   l_start_period
8566                     ,p_plan_period_type              =>   source_profile_info_rec.plan_period_type
8567                     ,p_number_of_periods             =>   (source_profile_info_rec.number_of_periods -1)
8568                     ,x_shifted_period                =>   l_end_period
8569                     ,x_shifted_period_start_date     =>   l_end_period_start_date
8570                     ,x_shifted_period_end_date       =>   l_end_period_end_date
8571                     ,x_return_status                 =>   l_return_status
8572                     ,x_msg_count                     =>   l_msg_count
8573                     ,x_msg_data                      =>   l_msg_data );
8574 
8575            IF p_pa_debug_mode = 'Y' THEN
8576                    pa_debug.g_err_stage:= 'l_end_period_start_date ='||l_end_period_start_date;
8577                    pa_debug.write(g_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
8578                    pa_debug.g_err_stage:= 'l_prj_end_date=' || l_prj_end_date;
8579                    pa_debug.write(g_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
8580            END IF;
8581 
8582            IF  (l_prj_end_date IS NOT NULL) AND
8583                (to_date(l_prj_end_date,'rrrr/mm/dd') < l_end_period_start_date)
8584            THEN
8585 
8586                  -- If the fetched end period has gone beyond the project completion date then
8587                  -- set the target project end period as the target profile end period
8588 
8589                  l_end_period := l_target_proj_end_period;
8590 
8591                  -- Fetch the end date of the above period
8592 
8593                  PA_FIN_PLAN_UTILS.Get_Period_Details(
8594                               p_period_name       =>   l_end_period
8595                              ,p_plan_period_type  =>   source_profile_info_rec.plan_period_type
8596                              ,x_start_date        =>   l_end_period_start_date
8597                              ,x_end_date          =>   l_end_period_end_date
8598                              ,x_return_status     =>   l_return_status
8599                              ,x_msg_count         =>   l_msg_count
8600                              ,x_msg_data          =>   l_msg_data );
8601 
8602                  -- Fetch the start period of the target period profile by moving backward from the end period
8603                  -- by the number of periods of the source period profile
8604 
8605                  PA_FIN_PLAN_UTILS.Get_Shifted_Period (
8606                              p_period_name                   =>   l_end_period
8607                             ,p_plan_period_type              =>   source_profile_info_rec.plan_period_type
8608                             ,p_number_of_periods             =>   (-source_profile_info_rec.number_of_periods+1)
8609                             ,x_shifted_period                =>   l_start_period
8610                             ,x_shifted_period_start_date     =>   l_start_period_start_date
8611                             ,x_shifted_period_end_date       =>   l_start_period_end_date
8612                             ,x_return_status                 =>   l_return_status
8613                             ,x_msg_count                     =>   l_msg_count
8614                             ,x_msg_data                      =>   l_msg_data );
8615 
8616                 -- check if the fetched start period has gone beyond the target project start date.
8617                 -- if so make the target project start period as the profile start period
8618 
8619                 IF  (l_start_period_end_date < to_date(l_prj_start_date,'rrrr/mm/dd'))
8620                 THEN
8621                          l_start_period := l_target_proj_start_period;
8622 
8623                          -- If the fetched start period doesn't fall in the project duration, then
8624                          -- set the project start period as the period profile start period
8625 
8626                          Pa_Fin_Plan_Utils.Get_Period_Details(
8627                                       p_period_name       =>   l_start_period
8628                                      ,p_plan_period_type  =>   source_profile_info_rec.plan_period_type
8629                                      ,x_start_date        =>   l_start_period_start_date
8630                                      ,x_end_date          =>   l_start_period_end_date
8631                                      ,x_return_status     =>   l_return_status
8632                                      ,x_msg_count         =>   l_msg_count
8633                                      ,x_msg_data          =>   l_msg_data );
8634                 END IF;
8635            END IF;
8636 
8637            -- Call Maintain_Prj_Period_Profile
8638 
8639            IF p_pa_debug_mode = 'Y' THEN
8640                    pa_debug.g_err_stage:= 'Calling  Pa_Prj_Period_Profile_Utils.Maintain_Prj_Period_Profile';
8641                    pa_debug.write(g_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
8642            END IF;
8643 
8644            Pa_Prj_Period_Profile_Utils.Maintain_Prj_Period_Profile(
8645                       p_project_id              =>   p_target_project_id
8646                      ,p_period_profile_type     =>   source_profile_info_rec.period_profile_type
8647                      ,p_plan_period_type        =>   source_profile_info_rec.plan_period_type
8648                      ,p_period_set_name         =>   l_period_set_name
8649                      ,p_gl_period_type          =>   l_accounted_period_type
8650                      ,p_pa_period_type          =>   l_pa_period_type
8651                      ,p_start_date              =>   l_start_period_start_date
8652                      ,px_end_date               =>   l_end_period_end_date
8653                      ,px_period_profile_id      =>   x_target_period_profile_id
8654                      ,p_commit_flag             =>   'N'
8655                      ,px_number_of_periods      =>   l_number_of_periods
8656                      ,x_plan_start_date         =>   l_plan_start_date
8657                      ,x_plan_end_date           =>   l_plan_end_date
8658                      ,x_return_status           =>   l_return_status
8659                      ,x_msg_count               =>   l_msg_count
8660                      ,x_msg_data                =>   l_msg_data );
8661 
8662            -- update the global pl/sqls with the source period profile and
8663            -- equivalent target period profile
8664 
8665            l_index := NVL(g_source_period_profile_tbl.last,0)+1;
8666 
8667            g_source_period_profile_tbl(l_index) := p_source_period_profile_id;
8668            g_target_period_profile_tbl(l_index) := x_target_period_profile_id;
8669 
8670       END IF; /* l_create_period_profile = 'Y' */
8671 
8672       IF p_pa_debug_mode = 'Y' THEN
8673               pa_debug.g_err_stage:= 'Exiting Get_Create_Shifted_PD_Profile';
8674               pa_debug.write(g_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
8675               pa_debug.reset_err_stack;
8676 	END IF;
8677   EXCEPTION
8678 
8679      WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
8680 
8681            x_return_status := FND_API.G_RET_STS_ERROR;
8682            l_msg_count := FND_MSG_PUB.count_msg;
8683            IF l_msg_count = 1 THEN
8684                 PA_INTERFACE_UTILS_PUB.get_messages
8685                       (p_encoded        => FND_API.G_TRUE
8686                       ,p_msg_index      => 1
8687                       ,p_msg_count      => l_msg_count
8688                       ,p_msg_data       => l_msg_data
8689                       ,p_data           => l_data
8690                       ,p_msg_index_out  => l_msg_index_out);
8691                 x_msg_data := l_data;
8692                 x_msg_count := l_msg_count;
8693            ELSE
8694                 x_msg_count := l_msg_count;
8695            END IF;
8696 IF P_PA_DEBUG_MODE = 'Y' THEN
8697            pa_debug.reset_err_stack;
8698 END IF;
8699            RAISE;
8700 
8701    WHEN others THEN
8702 
8703           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8704           x_msg_count     := 1;
8705           x_msg_data      := SQLERRM;
8706           FND_MSG_PUB.add_exc_msg
8707                           ( p_pkg_name        => 'PA_FP_COPY_FROM_PKG'
8708                            ,p_procedure_name  => 'Get_Create_Shifted_PD_Profile'
8709                            ,p_error_text      => sqlerrm);
8710           IF p_pa_debug_mode = 'Y' THEN
8711                   pa_debug.g_err_stage:= 'Unexpected Error'||SQLERRM;
8712                   pa_debug.write(g_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
8713                   pa_debug.reset_err_stack;
8714 	END IF;
8715           RAISE;
8716 END Get_Create_Shifted_PD_Profile;
8717 
8718 
8719 /*============================================================================================================
8720  * This procedure should be called to copy a workplan version. Copies budget
8721  * versions, resource assignments  and budget lines as required for the workplan
8722  * version. This is added for FP M (Bug 3354518)
8723 
8724  * 04-Jun-04  Bug 3619687  Raja
8725  * When a working version is created from a published workplan version,
8726  * all the additional workplan settings data (plan settings, txn currencies
8727  * and rate schedules) should be inherited from workplan plan type.
8728 
8729  * 24-Sep-2004 Bug 3847386  Raja
8730  * Added a new parameter p_copy_act_from_str_ids_tbl. The table would contain
8731  * from which version actuals should be copied for the target versions if they
8732  * need to be copied
8733 
8734  ============================================================================================================*/
8735 
8736 PROCEDURE copy_wp_budget_versions
8737 (
8738        p_source_project_id            IN       pa_proj_element_versions.project_id%TYPE
8739       ,p_target_project_id            IN       pa_proj_element_versions.element_version_id%TYPE
8740       ,p_src_sv_ids_tbl               IN       SYSTEM.pa_num_tbl_type
8741       ,p_target_sv_ids_tbl            IN       SYSTEM.pa_num_tbl_type
8742       ,p_copy_act_from_str_ids_tbl    IN       SYSTEM.pa_num_tbl_type -- bug 3847386
8743       ,p_copy_people_flag             IN       VARCHAR2
8744       ,p_copy_equip_flag              IN       VARCHAR2
8745       ,p_copy_mat_item_flag           IN       VARCHAR2
8746       ,p_copy_fin_elem_flag           IN       VARCHAR2
8747       ,p_copy_mode                    IN       VARCHAR2             --bug 4277801
8748       ,x_return_status                OUT      NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
8749       ,x_msg_count                    OUT      NOCOPY NUMBER --File.Sql.39 bug 4440895
8750       ,x_msg_data                     OUT      NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
8751 ) AS
8752 
8753     --Start of variables used for debugging
8754     l_return_status                            VARCHAR2(1);
8755     l_msg_count                                NUMBER := 0;
8756     l_msg_data                                 VARCHAR2(2000);
8757     l_data                                     VARCHAR2(2000);
8758     l_msg_index_out                            NUMBER;
8759     l_debug_mode                               VARCHAR2(30);
8760     l_debug_level3                    CONSTANT NUMBER :=3;
8761     l_debug_level5                    CONSTANT NUMBER :=5;
8762 
8763     --End of variables used for debugging
8764     l_module_name                              VARCHAR2(200) :=  g_module_name || '.copy_wp_budget_versions';
8765     i                                          NUMBER;
8766     l_src_budget_version_id                    pa_budget_versions.budget_version_id%TYPE;
8767     --l_src_resource_list_id                     pa_resource_lists_all_bg.resource_list_id%TYPE;  //Commented out for Bug 4200168.
8768     --l_targ_resource_list_id                    pa_resource_lists_all_bg.resource_list_id%TYPE;  //Commented out for Bug 4200168.
8769     l_adj_percentage                           NUMBER;
8770     l_copy_mode                                VARCHAR2(1) := p_copy_mode;
8771     l_shift_days                               NUMBER :=0;--SHOULD BE REMOVED LATER
8772     l_targ_budget_version_id                   pa_budget_versions.budget_version_id%TYPE;
8773     l_src_proj_fp_options_id                   pa_proj_fp_options.proj_fp_options_id%TYPE;
8774     l_targ_proj_fp_options_id                  pa_proj_fp_options.proj_fp_options_id%TYPE;
8775     l_wp_plan_type_id                          pa_fin_plan_types_b.fin_plan_type_id%TYPE;
8776     l_targ_multi_curr_flag                     pa_proj_fp_options.plan_in_multi_curr_flag%TYPE;
8777     l_pji_rollup_required                     VARCHAR(1);
8778 
8779   -- pjdvdsn1 compile issues, 03-FEB-2004, jwhite -------------------------
8780 
8781 /*
8782     l_src_elem_version_id_tbl                  PA_PLSQL_DATATYPES.IdTabTyp;
8783     l_targ_elem_version_id_tbl                 PA_PLSQL_DATATYPES.IdTabTyp;
8784 */
8785 
8786 
8787     l_src_elem_version_id_tbl                  SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
8788     l_targ_elem_version_id_tbl                 SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
8789 
8790     -- ----------------------------------------------------------------------
8791 
8792     l_copy_external_flag                       VARCHAR2(1);
8793 
8794     --This table will be used in the PJI API call
8795     l_budget_version_ids                       SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
8796     l_src_budget_version_id_tbl                SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
8797 
8798     --Cursor for getting the source and target element version ids. This will be used in calling the
8799     --Copy Planning Txn API
8800     CURSOR l_prep_plan_txn_csr(c_source_plan_version_id pa_budget_versions.budget_version_id%TYPE
8801                                ,c_src_struct_ver_id pa_proj_element_versions.element_version_id%TYPE
8802                                ,c_targ_struct_ver_id pa_proj_element_versions.element_version_id%TYPE)
8803     IS
8804     SELECT selv.element_version_id
8805           ,telv.element_version_id
8806     FROM   pa_proj_element_versions telv
8807           ,pa_proj_element_versions selv
8808           ,pa_proj_elements spe
8809           ,pa_proj_elements tpe
8810     WHERE  spe.project_id=p_source_project_id
8811     AND    tpe.project_id=p_target_project_id
8812     AND    spe.element_number=tpe.element_number
8813     AND    tpe.object_type='PA_TASKS'
8814     AND    spe.object_type='PA_TASKS'
8815     AND    telv.proj_element_id = tpe.proj_element_id
8816     AND    selv.proj_element_id =spe.proj_element_id
8817     AND    selv.parent_structure_version_id=c_src_struct_ver_id
8818     AND    telv.parent_structure_version_id=c_targ_struct_ver_id
8819     AND     EXISTS  (SELECT task_id
8820                      FROM   pa_resource_assignments pra
8821                      WHERE  pra.budget_version_id=c_source_plan_version_id
8822                      AND    pra.task_id=spe.proj_element_id);
8823 
8824 BEGIN
8825     x_msg_count := 0;
8826     x_return_status := FND_API.G_RET_STS_SUCCESS;
8827     SAVEPOINT copy_wp_budget_versions;
8828 
8829     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
8830     l_debug_mode := NVL(l_debug_mode, 'Y');
8831 
8832     -- Set curr function
8833 
8834     IF l_debug_mode = 'Y' THEN
8835     pa_debug.set_curr_function(
8836                 p_function   =>'pafpcpfb.copy_wp_budget_versions'
8837                ,p_debug_mode => l_debug_mode );
8838 
8839         pa_debug.g_err_stage:='Validating input parameters';
8840         pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level3);
8841     END IF;
8842 
8843 
8844     -- Source and target sv id tables should have same number of records
8845     -- Bug 3847386 if copy actuals from structure table is passed it
8846     -- should have same number of records as target sv ids table
8847     IF  (p_src_sv_ids_tbl.count<>p_target_sv_ids_tbl.count) OR
8848         (p_copy_act_from_str_ids_tbl IS NOT NULL
8849          AND p_src_sv_ids_tbl.count <> p_copy_act_from_str_ids_tbl.count)
8850     THEN
8851 
8852         IF l_debug_mode = 'Y' THEN
8853             pa_debug.g_err_stage:='p_src_sv_ids_tbl.count is'|| p_src_sv_ids_tbl.count ;
8854             pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
8855 
8856             pa_debug.g_err_stage:='p_target_sv_ids_tbl.count is'|| p_target_sv_ids_tbl.count ;
8857             pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
8858 
8859             IF p_copy_act_from_str_ids_tbl IS NOT NULL THEN
8860                 pa_debug.g_err_stage:='p_copy_act_from_str_ids_tbl.count is'
8861                               || p_copy_act_from_str_ids_tbl.count ;
8862                 pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
8863             END IF;
8864         END IF;
8865 
8866         PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
8867                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
8868 
8869         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
8870 
8871     END IF;
8872 
8873 
8874     --If the tables are empty then return
8875     IF p_src_sv_ids_tbl.count=0 THEN
8876 
8877         IF l_debug_mode = 'Y' THEN
8878             pa_debug.g_err_stage:='The input tables are empty' ;
8879             pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
8880             pa_debug.reset_curr_function;
8881         END IF;
8882         RETURN;
8883 
8884     END IF;
8885 
8886 
8887     --Source and target project ids should never be null
8888     IF p_source_project_id IS NULL OR
8889        p_target_project_id IS NULL THEN
8890 
8891         IF l_debug_mode = 'Y' THEN
8892             pa_debug.g_err_stage:='p_source_project_id is '||p_source_project_id;
8893             pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
8894 
8895             pa_debug.g_err_stage:='p_target_project_id is '||p_target_project_id;
8896             pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
8897         END IF;
8898 
8899         PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
8900                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
8901 
8902         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
8903 
8904     END IF;
8905 
8906     --Derive the workplan fin plan type id
8907     pa_fp_planning_transaction_pub.add_wp_plan_type
8908          (p_src_project_id    =>  p_source_project_id
8909          ,p_targ_project_id   =>  p_target_project_id
8910          ,x_return_status     =>  x_return_status
8911          ,x_msg_count         =>  x_msg_count
8912          ,x_msg_data          =>  x_msg_data );
8913 
8914     IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
8915 
8916         IF l_debug_mode = 'Y' THEN
8917             pa_debug.g_err_stage:='pa_fp_planning_transaction_pub.add_wp_plan_type returned error';
8918             pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
8919         END IF;
8920         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
8921 
8922     END IF;
8923 
8924     BEGIN
8925         SELECT fin_plan_type_id
8926         INTO   l_wp_plan_type_id
8927         FROM   pa_fin_plan_types_b
8928         WHERE  use_for_workplan_flag='Y';
8929     EXCEPTION
8930     WHEN NO_DATA_FOUND THEN
8931         IF l_debug_mode = 'Y' THEN
8932             pa_debug.g_err_stage:='Adding an error message as the wp plan type is not defined';
8933             pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level3);
8934         END IF;
8935         PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
8936                               p_msg_name      => 'PA_FP_NO_WP_PLAN_TYPE');
8937         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
8938     END;
8939 
8940     IF l_debug_mode = 'Y' THEN
8941         pa_debug.g_err_stage:='About to loop thru the source version id tbl '||p_src_sv_ids_tbl.count;
8942         pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level3);
8943     END IF;
8944 
8945     --Loop thru the source structure version ids table and process each structure version id
8946     FOR i IN p_src_sv_ids_tbl.first..p_src_sv_ids_tbl.last LOOP
8947 
8948 
8949         --Assign null to the variables that will be used as the out parameters while calling various APIs(bug 3437643)
8950         l_targ_budget_version_id  :=NULL;
8951         l_targ_proj_fp_options_id :=NULL;
8952 
8953         l_src_budget_version_id  := PA_PLANNING_TRANSACTION_UTILS.Get_Wp_Budget_Version_Id(p_src_sv_ids_tbl(i));
8954 
8955         IF p_source_project_id <> p_target_project_id THEN
8956 
8957             --l_src_resource_list_id := pa_fin_plan_utils.get_resource_list_id(l_src_budget_version_id); //Commented out for bug 4200168.
8958             --l_targ_resource_list_id := l_src_resource_list_id;--RESOURCE FOUNDATION api TO GET THE TARGET RESOURCE LIST ID; //Commented out for bug 4200168.
8959             l_adj_percentage:= 0.9999;
8960         ELSE
8961 
8962             --l_src_resource_list_id := pa_fin_plan_utils.get_resource_list_id(l_src_budget_version_id); //Commented out for bug 4200168.
8963             --l_targ_resource_list_id := l_src_resource_list_id; //Commented out for bug 4200168.
8964             l_adj_percentage:= 0;
8965         END IF;
8966 
8967         --Bug 3841130. As told by sheenie copy external flag should always be N in copy_wp_budget_versions.
8968         l_copy_external_flag := 'N';
8969 
8970         IF l_debug_mode = 'Y' THEN
8971             pa_debug.g_err_stage:='Calling copy budget version';
8972             pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level3);
8973         END IF;
8974 
8975         --Call the method that copies the budget version
8976         pa_fp_copy_from_pkg.copy_budget_version
8977             ( p_source_project_id        =>     p_source_project_id
8978              ,p_target_project_id        =>     p_target_project_id
8979              ,p_source_version_id        =>     l_src_budget_version_id
8980              ,p_copy_mode                =>     'W'
8981              ,p_adj_percentage           =>     l_adj_percentage
8982              ,p_calling_module           =>     PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_WORKPLAN
8983              ,p_shift_days               =>     l_shift_days
8984              ,px_target_version_id       =>     l_targ_budget_version_id
8985              ,p_struct_elem_version_id   =>     p_target_sv_ids_tbl(i)
8986              ,x_return_status            =>     x_return_status
8987              ,x_msg_count                =>     x_msg_count
8988              ,x_msg_data                 =>     x_msg_data);
8989 
8990         IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
8991 
8992             IF l_debug_mode = 'Y' THEN
8993                 pa_debug.g_err_stage:='Copy Budget version returned error';
8994                 pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
8995             END IF;
8996             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
8997 
8998         END IF;
8999         --Get the details required to call the Create FP Option API
9000         BEGIN
9001 
9002             SELECT proj_fp_options_id
9003             INTO   l_src_proj_fp_options_id
9004             FROM   pa_proj_fp_options
9005             WHERE  fin_plan_version_id=l_src_budget_version_id;
9006 
9007         EXCEPTION
9008         WHEN NO_DATA_FOUND THEN
9009 
9010             IF l_debug_mode = 'Y' THEN
9011                 pa_debug.g_err_stage:='No data found in pa_proj_fp_options for  fin_plan_version_id '
9012                                     ||l_src_budget_version_id;
9013                 pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
9014             END IF;
9015             RAISE;
9016 
9017         END;
9018 
9019         IF l_debug_mode = 'Y' THEN
9020             pa_debug.g_err_stage:='Create FP Option';
9021             pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level3);
9022         END IF;
9023 
9024         --Call the method to create the FP Option
9025         PA_PROJ_FP_OPTIONS_PUB.Create_Fp_Option
9026          (
9027            px_target_proj_fp_option_id      =>  l_targ_proj_fp_options_id
9028           ,p_source_proj_fp_option_id       =>  l_src_proj_fp_options_id
9029           ,p_target_fp_option_level_code    =>  PA_FP_CONSTANTS_PKG.G_OPTION_LEVEL_PLAN_VERSION
9030           ,p_target_fp_preference_code      =>  PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY
9031           ,p_target_fin_plan_version_id     =>  l_targ_budget_version_id
9032           ,p_target_plan_type_id            =>  l_wp_plan_type_id
9033           ,p_target_project_id              =>  p_target_project_id
9034           ,x_return_status                  =>  x_return_status
9035           ,x_msg_count                      =>  x_msg_count
9036           ,x_msg_data                       =>  x_msg_data);
9037 
9038         IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
9039 
9040             IF l_debug_mode = 'Y' THEN
9041                 pa_debug.g_err_stage:='Create_Fp_Option returned error';
9042                 pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
9043             END IF;
9044             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9045 
9046         END IF;
9047 
9048         --Derive the multi currency flag so as to call the copy planning txn currencies
9049         SELECT plan_in_multi_curr_flag
9050         INTO   l_targ_multi_curr_flag
9051         FROM   pa_proj_fp_options
9052         WHERE  proj_fp_options_id=l_targ_proj_fp_options_id;
9053 
9054         IF l_debug_mode = 'Y' THEN
9055             pa_debug.g_err_stage:='Calling copy fp txn currencies';
9056             pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level3);
9057         END IF;
9058 
9059         --Call the API to create the txn currencies
9060         PA_FP_TXN_CURRENCIES_PUB.Copy_Fp_Txn_Currencies(
9061               p_source_fp_option_id         =>  l_src_proj_fp_options_id
9062              ,p_target_fp_option_id         =>  l_targ_proj_fp_options_id
9063              ,p_target_fp_preference_code   =>  NULL
9064              ,p_plan_in_multi_curr_flag     =>  l_targ_multi_curr_flag
9065              ,x_return_status               =>  x_return_status
9066              ,x_msg_count                   =>  x_msg_count
9067              ,x_msg_data                    =>  x_msg_data);
9068 
9069         IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
9070 
9071             IF l_debug_mode = 'Y' THEN
9072                 pa_debug.g_err_stage:='Copy_Fp_Txn_Currencies returned error';
9073                 pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
9074             END IF;
9075             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9076 
9077         END IF;
9078 
9079         --Call the PJI API to register the creation of plan version
9080 
9081         -- FP M - Reporting lines integration. The create API is being called even before calling the copy
9082         --planning txn as the copy planning txn will take care of later calling the PJI plan update API for
9083         --the new budget lines created
9084         l_budget_version_ids.delete;
9085         l_budget_version_ids   := SYSTEM.pa_num_tbl_type(l_targ_budget_version_id);
9086 
9087         l_src_budget_version_id_tbl.delete;
9088         l_src_budget_version_id_tbl   := SYSTEM.pa_num_tbl_type(l_src_budget_version_id);
9089 
9090         IF l_debug_mode = 'Y' THEN
9091             pa_debug.write(l_module_name,'Calling PJI_FM_XBS_ACCUM_MAINT.PLAN_CREATE ' ,5);
9092             pa_debug.write(l_module_name,'p_fp_version_ids count '|| l_budget_version_ids.count(),5);
9093         END IF;
9094 
9095         /* We are sure that there is only one record. But just looping the std way */
9096         FOR I in l_budget_version_ids.first..l_budget_version_ids.last LOOP
9097             pa_debug.write(l_module_name,''|| l_budget_version_ids(i),5);
9098         END LOOP;
9099          -- This parameter p_copy_mode will be used when the source project is not equal to the target project.
9100         --This will be passed as null in the MSP flow other wise it will be defaulted to 'P'.
9101         IF p_source_project_id <> p_target_project_id THEN
9102         PJI_FM_XBS_ACCUM_MAINT.PLAN_CREATE (
9103             p_fp_version_ids   => l_budget_version_ids,
9104             p_fp_src_version_ids => l_src_budget_version_id_tbl,
9105             p_copy_mode       => l_copy_mode,
9106             x_return_status    => l_return_status,
9107             x_msg_code         => l_msg_data);
9108         ELSE
9109         PJI_FM_XBS_ACCUM_MAINT.PLAN_CREATE (
9110             p_fp_version_ids   => l_budget_version_ids,
9111             x_return_status    => l_return_status,
9112             x_msg_code         => l_msg_data);
9113         END IF;
9114 
9115         IF l_return_status <> FND_API.G_RET_STS_SUCCESS then
9116             PA_UTILS.ADD_MESSAGE(p_app_short_name      => PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_PA,
9117                                p_msg_name            => l_msg_data);
9118             RAISE  PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9119         END IF;
9120 
9121 
9122 
9123         IF l_debug_mode = 'Y' THEN
9124             pa_debug.g_err_stage:='Bulk fetching the Ids for calling copy planning txn'||' '||p_src_sv_ids_tbl(i)||' '||p_target_sv_ids_tbl(i);
9125             pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level3);
9126         END IF;
9127 
9128         OPEN l_prep_plan_txn_csr( l_src_budget_version_id
9129                                  ,p_src_sv_ids_tbl(i)
9130                                  ,p_target_sv_ids_tbl(i));
9131         FETCH l_prep_plan_txn_csr
9132         BULK COLLECT INTO
9133         l_src_elem_version_id_tbl
9134        ,l_targ_elem_version_id_tbl;
9135         CLOSE l_prep_plan_txn_csr;
9136 
9137         IF l_debug_mode = 'Y' THEN
9138             pa_debug.g_err_stage:='Calling copy planning txns after uncommenting';
9139             pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level3);
9140         END IF;
9141 
9142 
9143 
9144         --Calling the  copy planning txn API
9145         pa_fp_planning_transaction_pub.copy_planning_transactions(
9146               p_context                 =>  PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_WORKPLAN
9147              ,p_copy_external_flag      =>  l_copy_external_flag
9148              ,p_src_project_id          =>  p_source_project_id
9149              ,p_target_project_id       =>  p_target_project_id
9150              ,p_src_budget_version_id   =>  l_src_budget_version_id
9151              ,p_targ_budget_version_id  =>  l_targ_budget_version_id
9152              ,p_src_version_id_tbl      =>  l_src_elem_version_id_tbl
9153              ,p_targ_version_id_tbl     =>  l_targ_elem_version_id_tbl
9154              ,p_copy_people_flag        =>  p_copy_people_flag
9155              ,p_copy_equip_flag         =>  p_copy_equip_flag
9156              ,p_copy_mat_item_flag      =>  p_copy_mat_item_flag
9157              ,p_copy_fin_elem_flag      =>  p_copy_fin_elem_flag
9158              ,p_pji_rollup_required     =>  'N'
9159              ,x_return_status           =>  x_return_status
9160              ,x_msg_count               =>  x_msg_count
9161              ,x_msg_data                =>  x_msg_data);
9162 
9163         IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
9164 
9165             IF l_debug_mode = 'Y' THEN
9166                 pa_debug.g_err_stage:='Copy_Fp_Txn_Currencies returned error';
9167                 pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
9168             END IF;
9169             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9170 
9171         END IF;
9172 
9173         -- Bug 3619687
9174         -- When this api is called to create a working workplan structure version from
9175         -- a published version, all the data in 'Additional Workplan settings' pages
9176         -- should be synchronised with workplan plan type data
9177 
9178         PA_FP_COPY_FROM_PKG.Update_Plan_Setup_For_WP_Copy(
9179                  p_project_id            =>  p_target_project_id
9180                  ,p_wp_version_id        =>  l_targ_budget_version_id
9181                  ,x_return_status        =>  x_return_status
9182                  ,x_msg_count            =>  x_msg_count
9183                  ,x_msg_data             =>  x_msg_data);
9184 
9185         IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
9186 
9187             IF l_debug_mode = 'Y' THEN
9188                 pa_debug.g_err_stage:='Update_Plan_Setup_For_WP_Copy returned error';
9189                 pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
9190             END IF;
9191             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9192         END IF;
9193 
9194         -- If copy actuals from structure id is passed progress actuals should be copied
9195         -- for the target version
9196         IF p_copy_act_from_str_ids_tbl IS NOT NULL AND
9197            p_copy_act_from_str_ids_tbl.EXISTS(i)  AND
9198            p_copy_act_from_str_ids_tbl(i) IS NOT NULL AND
9199            p_source_project_id = p_target_project_id
9200         THEN
9201             -- Added for bug 3850488.
9202             -- Copy the missing unplanned assignments.
9203             BEGIN
9204                       PA_TASK_ASSIGNMENTS_PVT.Copy_Missing_Unplanned_Asgmts(
9205                          p_project_id               => p_target_project_id
9206                         ,p_old_structure_version_id => p_copy_act_from_str_ids_tbl(i)
9207                         ,p_new_structure_version_id => p_target_sv_ids_tbl(i)
9208                         ,x_msg_count                => x_msg_count
9209                         ,x_msg_data                 => x_msg_data
9210                         ,x_return_status            => x_return_status
9211                       );
9212             EXCEPTION
9213                   WHEN OTHERS THEN
9214                   IF l_debug_mode = 'Y' THEN
9215                       pa_debug.g_err_stage:='PA_TASK_ASSIGNMENTS_PVT.Copy_Missing_Unplanned_Asgmts returned error';
9216                       pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
9217                   END IF;
9218                   RAISE FND_API.G_EXC_ERROR;
9219             END;
9220             IF (x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
9221               RAISE FND_API.G_EXC_ERROR;
9222             END IF;
9223             -- End bug 3850488.
9224 
9225             -- Call copy_actuals_for_workplan
9226             PA_PROGRESS_PVT.copy_actuals_for_workplan(
9227                 p_project_id              =>  p_target_project_id
9228                ,p_source_struct_ver_id    =>  p_copy_act_from_str_ids_tbl(i)
9229                ,p_target_struct_ver_id    =>  p_target_sv_ids_tbl(i)
9230                ,x_return_status           =>  x_return_status
9231                ,x_msg_count               =>  x_msg_count
9232                ,x_msg_data                =>  x_msg_data);
9233 
9234             IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
9235 
9236                 IF l_debug_mode = 'Y' THEN
9237                     pa_debug.g_err_stage:='Update_Plan_Setup_For_WP_Copy returned error';
9238                     pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
9239                 END IF;
9240                 RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9241             END IF;
9242         END IF;
9243 
9244         PA_PLANNING_TRANSACTION_UTILS.call_update_rep_lines_api
9245                 ( p_source                  => 'PA_FP_RA_MAP_TMP'
9246                  ,p_budget_version_id       => l_targ_budget_version_id
9247                  ,x_return_status           => x_return_status
9248                  ,x_msg_data                => x_msg_data
9249                  ,x_msg_count               => x_msg_count);
9250 
9251         IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
9252 
9253              IF l_debug_mode = 'Y' THEN
9254                  pa_debug.g_err_stage:='PA_PLANNING_TRANSACTION_UTILS.call_update_rep_lines_api returned error';
9255                  pa_debug.write( l_module_name,pa_debug.g_err_stage,5);
9256              END IF;
9257              RAISE PA_FP_CONSTANTS_PKG.INVALID_ARG_EXC;
9258         END IF;
9259 
9260 
9261     END LOOP;
9262 
9263     IF l_debug_mode = 'Y' THEN
9264         pa_debug.g_err_stage:='Exiting copy_wp_budget_versions';
9265         pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level3);
9266     -- reset curr function
9267         pa_debug.reset_curr_function;
9268     END IF;
9269 EXCEPTION
9270 
9271    WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
9272        l_msg_count := FND_MSG_PUB.count_msg;
9273        IF l_msg_count = 1 THEN
9274            PA_INTERFACE_UTILS_PUB.get_messages
9275                  (p_encoded        => FND_API.G_TRUE
9276                   ,p_msg_index      => 1
9277                   ,p_msg_count      => l_msg_count
9278                   ,p_msg_data       => l_msg_data
9279                   ,p_data           => l_data
9280                   ,p_msg_index_out  => l_msg_index_out);
9281 
9282            x_msg_data := l_data;
9283            x_msg_count := l_msg_count;
9284        ELSE
9285            x_msg_count := l_msg_count;
9286        END IF;
9287        ROLLBACK TO copy_wp_budget_versions;
9288        x_return_status := FND_API.G_RET_STS_ERROR;
9289 
9290        IF l_debug_mode = 'Y' THEN
9291            pa_debug.g_err_stage:='Invalid Arguments Passed Or called api raised an error';
9292            pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
9293        -- reset curr function
9294           pa_debug.reset_curr_function();
9295        END IF;
9296        RETURN;
9297    WHEN OTHERS THEN
9298        ROLLBACK TO copy_wp_budget_versions;
9299        x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9300        x_msg_count     := 1;
9301        x_msg_data      := SQLERRM;
9302 
9303        FND_MSG_PUB.add_exc_msg( p_pkg_name        => 'pa_fp_copy_from_pkg'
9304                                ,p_procedure_name  => 'copy_wp_budget_versions');
9305 
9306        IF l_debug_mode = 'Y' THEN
9307            pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
9308            pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
9309        -- reset curr function
9310           pa_debug.Reset_Curr_Function();
9311        END IF;
9312        RAISE;
9313 
9314 END copy_wp_budget_versions;
9315 
9316   /*=========================================================================
9317       3156057: FP.M changes:
9318       If source plan is mc enabled but not appr rev and the target is appr rev,
9319       then this api will be called to group the source budget lines by PFC for creating target budget lines
9320       Logic:
9321       1) Get all the distinct resource_assignment_id and start_date combination for the source plan version id
9322       2) Fetch all the source budget lines for the given resource_assignment_id and start_date combination, ordering
9323           by transaction currency code (filtering out the records which have some rejection code populated,
9324           and sum up the amounts and quantities.
9325       3) For the DFF attributes, change reason and description, copy the attributes if any of the source lines for the
9326           given resource_assignment_id and start_date combination has the transaction currency same as the project
9327           functional currency. If not, then copy the attributes from the first listed source line,
9328           ordering by transaction currency.
9329       4) This api is dependant on PA_FP_RA_MAP_TMP being populated before it is called and it populates
9330            PA_FP_BL_MAP_TMP on which the api COPY_MC_BUDGET_LINES_APPR_REV would be dependant.
9331    =========================================================================*/
9332 
9333   --Bug 4290043. Added p_derv_rates_missing_amts_flag to indicate whether the missing amounts in the target version
9334   --should be derived or not after copy
9335    /* Bug 4865563 IPM Arch Enhancement. Display_quantity is being set in copy_version and copy_finplans_from_project apis using
9336         populate_display_qty api. */
9337 
9338   PROCEDURE Copy_Budget_Lines_Appr_Rev(
9339              p_source_plan_version_id        IN  NUMBER
9340              ,p_target_plan_version_id       IN  NUMBER
9341              ,p_adj_percentage               IN  NUMBER
9342              ,p_derv_rates_missing_amts_flag IN  VARCHAR2
9343              ,x_return_status                OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
9344              ,x_msg_count                    OUT NOCOPY NUMBER --File.Sql.39 bug 4440895
9345              ,x_msg_data                     OUT NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
9346    AS
9347 
9348          l_msg_count          NUMBER :=0;
9349          l_data               VARCHAR2(2000);
9350          l_msg_data           VARCHAR2(2000);
9351          l_error_msg_code     VARCHAR2(2000);
9352          l_msg_index_out      NUMBER;
9353          l_return_status      VARCHAR2(2000);
9354          l_debug_mode         VARCHAR2(30);
9355 
9356          l_revenue_flag       pa_fin_plan_amount_sets.revenue_flag%type;
9357          l_cost_flag          pa_fin_plan_amount_sets.raw_cost_flag%type;
9358          l_rate_based_flag    pa_resource_assignments.rate_based_flag%type;
9359 
9360 
9361          l_adj_percentage            NUMBER ;
9362 
9363          l_quantity_tot  NUMBER;
9364          l_raw_cost_tot NUMBER;
9365          l_burdened_cost_tot NUMBER;
9366          l_revenue_tot NUMBER;
9367          l_project_raw_cost_tot NUMBER;
9368          l_project_burdened_cost_tot NUMBER;
9369          l_project_revenue_tot NUMBER;
9370          l_ref NUMBER ;
9371          l_target_budget_line_id NUMBER;
9372          l_project_cost_exchange_rate NUMBER;
9373          l_proj_rev_ex_rate NUMBER;
9374          l_txn_cost_rate_override NUMBER;
9375          l_txn_bill_rate_override NUMBER;
9376          l_burden_cost_rate_override NUMBER;
9377 
9378          /* Initialized plsql tables for bug 3709036 */
9379 
9380          l_budget_line_idTab                 SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE() ;
9381          l_quantityTab                          SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE() ;
9382          l_raw_costTab                           SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE() ;
9383          l_burdened_costTab                 SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE() ;
9384          l_revenueTab                           SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE() ;
9385          l_change_reason_codeTab       SYSTEM.PA_VARCHAR2_30_TBL_TYPE := SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
9386          l_descriptionTab                      SYSTEM.PA_VARCHAR2_2000_TBL_TYPE := SYSTEM.PA_VARCHAR2_2000_TBL_TYPE();
9387          l_attribute_categoryTab            SYSTEM.PA_VARCHAR2_30_TBL_TYPE := SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
9388          l_attribute1Tab                        SYSTEM.PA_VARCHAR2_240_TBL_TYPE := SYSTEM.PA_VARCHAR2_240_TBL_TYPE();
9389          l_attribute2Tab                        SYSTEM.PA_VARCHAR2_240_TBL_TYPE := SYSTEM.PA_VARCHAR2_240_TBL_TYPE();
9390          l_attribute3Tab                        SYSTEM.PA_VARCHAR2_240_TBL_TYPE := SYSTEM.PA_VARCHAR2_240_TBL_TYPE();
9391          l_attribute4Tab                        SYSTEM.PA_VARCHAR2_240_TBL_TYPE := SYSTEM.PA_VARCHAR2_240_TBL_TYPE();
9392          l_attribute5Tab                        SYSTEM.PA_VARCHAR2_240_TBL_TYPE := SYSTEM.PA_VARCHAR2_240_TBL_TYPE();
9393          l_attribute6Tab                        SYSTEM.PA_VARCHAR2_240_TBL_TYPE := SYSTEM.PA_VARCHAR2_240_TBL_TYPE();
9394          l_attribute7Tab                        SYSTEM.PA_VARCHAR2_240_TBL_TYPE := SYSTEM.PA_VARCHAR2_240_TBL_TYPE();
9395          l_attribute8Tab                        SYSTEM.PA_VARCHAR2_240_TBL_TYPE := SYSTEM.PA_VARCHAR2_240_TBL_TYPE();
9396          l_attribute9Tab                        SYSTEM.PA_VARCHAR2_240_TBL_TYPE := SYSTEM.PA_VARCHAR2_240_TBL_TYPE();
9397          l_attribute10Tab                      SYSTEM.PA_VARCHAR2_240_TBL_TYPE := SYSTEM.PA_VARCHAR2_240_TBL_TYPE();
9398          l_attribute11Tab                      SYSTEM.PA_VARCHAR2_240_TBL_TYPE := SYSTEM.PA_VARCHAR2_240_TBL_TYPE();
9399          l_attribute12Tab                      SYSTEM.PA_VARCHAR2_240_TBL_TYPE := SYSTEM.PA_VARCHAR2_240_TBL_TYPE();
9400          l_attribute13Tab                      SYSTEM.PA_VARCHAR2_240_TBL_TYPE := SYSTEM.PA_VARCHAR2_240_TBL_TYPE();
9401          l_attribute14Tab                      SYSTEM.PA_VARCHAR2_240_TBL_TYPE := SYSTEM.PA_VARCHAR2_240_TBL_TYPE();
9402          l_attribute15Tab                      SYSTEM.PA_VARCHAR2_240_TBL_TYPE := SYSTEM.PA_VARCHAR2_240_TBL_TYPE();
9403          l_project_raw_costTab             SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
9404          l_project_burdened_costTab         SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
9405          l_project_revenueTab               SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
9406          l_txn_currency_codeTab          SYSTEM.PA_VARCHAR2_30_TBL_TYPE := SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
9407          l_projfunc_currency_codeTab   SYSTEM.PA_VARCHAR2_30_TBL_TYPE := SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
9408          l_project_currency_codeTab     SYSTEM.PA_VARCHAR2_30_TBL_TYPE := SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
9409          l_end_dateTab                        SYSTEM.PA_DATE_TBL_TYPE := SYSTEM.PA_DATE_TBL_TYPE ();
9410          l_period_nameTab                   SYSTEM.PA_VARCHAR2_30_TBL_TYPE := SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
9411 
9412          CURSOR Cur_group_source_budget_lines IS
9413          SELECT distinct pbl.resource_assignment_id resource_assignment_id, pbl.start_date start_date,pra.rate_based_flag
9414          FROM pa_budget_lines pbl, pa_resource_assignments pra
9415          WHERE pbl.budget_version_id = p_source_plan_version_id
9416          AND pra.resource_assignment_id = pbl.resource_assignment_id;
9417 
9418          CURSOR Cur_source_budget_lines(p_resource_assignment_id IN NUMBER, p_start_date IN DATE
9419          ,p_rate_based_flag IN VARCHAR2 , p_cost_flag IN VARCHAR2 , p_revenue_flag IN VARCHAR2) IS
9420          SELECT
9421                 budget_line_id
9422                ,nvl(DECODE(p_rate_based_flag,'N',DECODE(p_cost_flag,'Y',nvl(raw_cost,0),nvl(revenue,0)),quantity),0) quantity
9423                ,nvl(raw_cost,0)
9424                ,nvl(burdened_cost,0)
9425                ,nvl(revenue,0)
9426                ,change_reason_code
9427                ,description
9428                ,attribute_category
9429                ,attribute1
9430                ,attribute2
9431                ,attribute3
9432                ,attribute4
9433                ,attribute5
9434                ,attribute6
9435                ,attribute7
9436                ,attribute8
9437                ,attribute9
9438                ,attribute10
9439                ,attribute11
9440                ,attribute12
9441                ,attribute13
9442                ,attribute14
9443                ,attribute15
9444                ,nvl(project_raw_cost,0)
9445                ,nvl(project_burdened_cost,0)
9446                ,nvl(project_revenue,0)
9447                ,txn_currency_code
9448                ,projfunc_currency_code
9449                ,project_currency_code
9450                ,end_date
9451                ,period_name
9452          FROM pa_budget_lines
9453          WHERE resource_assignment_id = p_resource_assignment_id
9454          AND start_date = p_start_date
9455          AND cost_rejection_code is null
9456          AND burden_rejection_code is null
9457          AND revenue_rejection_code is null
9458          AND other_rejection_code is null
9459          AND pc_cur_conv_rejection_code is null
9460          AND pfc_cur_conv_rejection_code is null
9461          ORDER BY txn_currency_code;
9462 
9463   /* PROCEDURE InitPLSQLTab IS
9464   BEGIN
9465                 l_budget_line_idTab.delete;
9466                 l_quantityTab.delete;
9467                 l_raw_costTab.delete;
9468                 l_burdened_costTab.delete;
9469                 l_revenueTab.delete;
9470                 l_change_reason_codeTab.delete;
9471                 l_descriptionTab.delete;
9472                 l_attribute_categoryTab.delete;
9473                 l_attribute1Tab.delete;
9474                 l_attribute2Tab.delete;
9475                 l_attribute3Tab.delete;
9476                 l_attribute4Tab.delete;
9477                 l_attribute5Tab.delete;
9478                 l_attribute6Tab.delete;
9479                 l_attribute7Tab.delete;
9480                 l_attribute8Tab.delete;
9481                 l_attribute9Tab.delete;
9482                 l_attribute10Tab.delete;
9483                 l_attribute11Tab.delete;
9484                 l_attribute12Tab.delete;
9485                 l_attribute13Tab.delete;
9486                 l_attribute14Tab.delete;
9487                 l_attribute15Tab.delete;
9488                 l_project_raw_costTab.delete;
9489                 l_project_burdened_costTab.delete;
9490                 l_project_revenueTab.delete;
9491                 l_txn_currency_codeTab.delete;
9492                 l_projfunc_currency_codeTab.delete;
9493                 l_project_currency_codeTab.delete;
9494                 l_end_dateTab.delete;
9495                 l_period_nameTab.delete;
9496 
9497   END InitPLSQLTab;  ** bug 3709036 */
9498 
9499   l_etc_start_date  pa_budget_versions.etc_start_date%TYPE;
9500 
9501   --Bug 4290043.
9502   l_targ_pref_code         pa_proj_fp_options.fin_plan_preference_code%TYPE;
9503   l_source_version_type    pa_budget_versions.version_type%TYPE;
9504   l_target_version_type    pa_budget_versions.version_type%TYPE;
9505   l_src_plan_class_code    pa_fin_plan_types_b.plan_class_code%TYPE;
9506 
9507     BEGIN
9508 
9509       x_msg_count := 0;
9510       x_return_status := FND_API.G_RET_STS_SUCCESS;
9511 IF P_PA_DEBUG_MODE = 'Y' THEN
9512       pa_debug.set_err_stack('PA_FP_COPY_FROM_PKG.Copy_Budget_Lines_Appr_Rev');
9513 END IF;
9514       fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
9515       l_debug_mode := NVL(l_debug_mode, 'Y');
9516 IF P_PA_DEBUG_MODE = 'Y' THEN
9517       pa_debug.set_process('PLSQL','LOG',l_debug_mode);
9518 END IF;
9519       -- Checking for all valid input parametrs
9520 
9521       IF P_PA_DEBUG_MODE = 'Y' THEN
9522           pa_debug.g_err_stage := 'Checking for valid parameters:';
9523           pa_debug.write('Copy_Budget_Lines_Appr_Rev: ' || g_module_name,pa_debug.g_err_stage,3);
9524       END IF;
9525 
9526       IF (p_source_plan_version_id IS NULL) OR
9527          (p_target_plan_version_id IS NULL)
9528       THEN
9529 
9530            IF P_PA_DEBUG_MODE = 'Y' THEN
9531                pa_debug.g_err_stage := 'Source_plan='||p_source_plan_version_id;
9532                pa_debug.write('Copy_Budget_Lines_Appr_Rev: ' || g_module_name,pa_debug.g_err_stage,5);
9533                pa_debug.g_err_stage := 'Target_plan'||p_target_plan_version_id;
9534                pa_debug.write('Copy_Budget_Lines_Appr_Rev: ' || g_module_name,pa_debug.g_err_stage,5);
9535            END IF;
9536 
9537            PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
9538                                 p_msg_name      => 'PA_FP_INV_PARAM_PASSED',
9539                                 p_token1=>'PROCEDURENAME',
9540                                 p_value1=>'COPY_BUDGET_LINES_APPR_REV');
9541 
9542            RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9543 
9544       END IF;
9545 
9546       IF P_PA_DEBUG_MODE = 'Y' THEN
9547           pa_debug.g_err_stage := 'Parameter validation complete';
9548           pa_debug.write('Copy_Budget_Lines_Appr_Rev: ' || g_module_name,pa_debug.g_err_stage,3);
9549       END IF;
9550 
9551       --make adj percentage zero if passed as null
9552 
9553       l_adj_percentage := NVL(p_adj_percentage,0);
9554 
9555        -- Fetching the flags of target version using fin_plan_prefernce_code
9556 
9557 
9558        IF P_PA_DEBUG_MODE = 'Y' THEN
9559            pa_debug.g_err_stage:='Fetching the raw_cost,burdened_cost and revenue flags of target_version';
9560            pa_debug.write('Copy_Budget_Lines_Appr_Rev: ' || g_module_name,pa_debug.g_err_stage,3);
9561        END IF;
9562 
9563        SELECT DECODE(fin_plan_preference_code          -- l_revenue_flag
9564                        ,PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY ,'Y'
9565                        ,PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME ,'Y','N')
9566               ,DECODE(fin_plan_preference_code          -- l_cost_flag
9567                       ,PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME , 'Y','N')
9568               ,fin_plan_preference_code
9569        INTO   l_revenue_flag
9570               ,l_cost_flag
9571               ,l_targ_pref_code
9572        FROM   pa_proj_fp_options
9573        WHERE  fin_plan_version_id=p_target_plan_version_id;
9574 
9575        --Bug 3927244
9576        --Bug 4290043. Selected version type too
9577        SELECT etc_start_date,
9578               version_type
9579        INTO   l_etc_start_date,
9580               l_target_version_type
9581        FROM   pa_budget_versions
9582        WHERE  budget_version_id=p_target_plan_version_id;
9583        --Bug 3927244
9584 
9585        --Bug 4290043.
9586        SELECT pbv.version_type,
9587               fin.plan_class_code
9588        INTO   l_source_version_type,
9589               l_src_plan_class_code
9590        FROM   pa_budget_versions pbv,
9591               pa_fin_plan_types_b fin
9592        WHERE  pbv.fin_plan_type_id=fin.fin_plan_type_id
9593        AND    pbv.budget_version_id = p_source_plan_version_id;
9594 
9595        DELETE FROM  PA_FP_BL_MAP_TMP;
9596 
9597        FOR rec_group_source_budget_lines IN cur_group_source_budget_lines LOOP
9598 
9599        -- InitPLSQLTab;  ** bug 3709036
9600 
9601          l_ref := 1;
9602          l_quantity_tot := 0;
9603          l_raw_cost_tot := 0;
9604          l_burdened_cost_tot := 0;
9605          l_revenue_tot := 0;
9606          l_project_raw_cost_tot := 0;
9607          l_project_burdened_cost_tot := 0;
9608          l_project_revenue_tot := 0;
9609          l_rate_based_flag := rec_group_source_budget_lines.rate_based_flag;
9610 
9611        SELECT pa_budget_lines_s.nextval
9612        INTO l_target_budget_line_id
9613        FROM dual;
9614 
9615        Open Cur_source_budget_lines(rec_group_source_budget_lines.resource_assignment_id,
9616                                                      rec_group_source_budget_lines.start_date,
9617                                                      l_rate_based_flag,
9618                                                      l_cost_flag,
9619                                                      l_revenue_flag);
9620 
9621        Fetch Cur_source_budget_lines bulk collect into
9622                 l_budget_line_idTab
9623                ,l_quantityTab
9624                ,l_raw_costTab
9625                ,l_burdened_costTab
9626                ,l_revenueTab
9627                ,l_change_reason_codeTab
9628                ,l_descriptionTab
9629                ,l_attribute_categoryTab
9630                ,l_attribute1Tab
9631                ,l_attribute2Tab
9632                ,l_attribute3Tab
9633                ,l_attribute4Tab
9634                ,l_attribute5Tab
9635                ,l_attribute6Tab
9636                ,l_attribute7Tab
9637                ,l_attribute8Tab
9638                ,l_attribute9Tab
9639                ,l_attribute10Tab
9640                ,l_attribute11Tab
9641                ,l_attribute12Tab
9642                ,l_attribute13Tab
9643                ,l_attribute14Tab
9644                ,l_attribute15Tab
9645                ,l_project_raw_costTab
9646                ,l_project_burdened_costTab
9647                ,l_project_revenueTab
9648                ,l_txn_currency_codeTab
9649                ,l_projfunc_currency_codeTab
9650                ,l_project_currency_codeTab
9651                ,l_end_dateTab
9652                ,l_period_nameTab;
9653 
9654          Close Cur_source_budget_lines; -- bug 3709036
9655 
9656        IF l_budget_line_idTab.count > 0  THEN   -- bug 3709036
9657 
9658         FOR J in 1..l_budget_line_idTab.count LOOP  ---{
9659 
9660                l_quantity_tot := l_quantity_tot + l_quantityTab(j);
9661                l_raw_cost_tot := l_raw_cost_tot + l_raw_costTab(j);
9662                l_burdened_cost_tot := l_burdened_cost_tot + l_burdened_costTab(j);
9663                l_revenue_tot := l_revenue_tot + l_revenueTab(j);
9664                l_project_raw_cost_tot := l_project_raw_cost_tot + l_project_raw_costTab(j);
9665                l_project_burdened_cost_tot := l_project_burdened_cost_tot + l_project_burdened_costTab(j);
9666                l_project_revenue_tot := l_project_revenue_tot + l_project_revenueTab(j);
9667 
9668         IF (l_txn_currency_codeTab(j) = l_projfunc_currency_codeTab(j)
9669         and ( l_change_reason_codeTab(j) IS NOT NULL
9670                OR l_descriptionTab(j) IS NOT NULL
9671                OR l_attribute_categoryTab(j) IS NOT NULL
9672                OR l_attribute1Tab(j) IS NOT NULL
9673                OR l_attribute2Tab(j) IS NOT NULL
9674                OR l_attribute3Tab(j) IS NOT NULL
9675                OR l_attribute4Tab(j) IS NOT NULL
9676                OR l_attribute5Tab(j) IS NOT NULL
9677                OR l_attribute6Tab(j) IS NOT NULL
9678                OR l_attribute7Tab(j) IS NOT NULL
9679                OR l_attribute8Tab(j) IS NOT NULL
9680                OR l_attribute9Tab(j) IS NOT NULL
9681                OR l_attribute10Tab(j) IS NOT NULL
9682                OR l_attribute11Tab(j) IS NOT NULL
9683                OR l_attribute12Tab(j) IS NOT NULL
9684                OR l_attribute13Tab(j) IS NOT NULL
9685                OR l_attribute14Tab(j) IS NOT NULL
9686                OR l_attribute15Tab(j) IS NOT NULL )) THEN
9687 
9688         l_ref := j;
9689 
9690         END IF;
9691 
9692         INSERT INTO PA_FP_BL_MAP_TMP
9693                           ( source_budget_line_id
9694                            ,target_budget_line_id
9695                            )
9696                   VALUES (l_budget_line_idTab(j),
9697                                  l_target_budget_line_id
9698                                  );
9699 
9700         END LOOP;   ---}
9701 
9702                  IF l_raw_cost_tot = 0 THEN
9703                      l_project_cost_exchange_rate :=  1;
9704                  ELSE
9705                      l_project_cost_exchange_rate :=  l_project_raw_cost_tot/l_raw_cost_tot;
9706                  END IF;
9707 
9708                  IF l_revenue_tot = 0 THEN
9709                      l_proj_rev_ex_rate :=  1;
9710                  ELSE
9711                       l_proj_rev_ex_rate :=  l_project_revenue_tot/l_revenue_tot;
9712                  END IF;
9713 
9714                  IF l_quantity_tot = 0 THEN
9715                      l_txn_cost_rate_override :=  null;
9716                      l_txn_bill_rate_override :=  null;
9717                      l_burden_cost_rate_override := null;
9718                  ELSE
9719                      l_txn_cost_rate_override :=  l_raw_cost_tot/l_quantity_tot;
9720                      l_txn_bill_rate_override :=  l_revenue_tot/l_quantity_tot;
9721                      l_burden_cost_rate_override := l_burdened_cost_tot/l_quantity_tot;
9722                  END IF;
9723 
9724        INSERT INTO PA_BUDGET_LINES(
9725                 budget_line_id
9726                ,budget_version_id
9727                ,resource_assignment_id
9728                ,start_date
9729                ,last_update_date
9730                ,last_updated_by
9731                ,creation_date
9732                ,created_by
9733                ,last_update_login
9734                ,end_date
9735                ,period_name
9736                ,quantity
9737                ,raw_cost
9738                ,burdened_cost
9739                ,revenue
9740                ,change_reason_code
9741                ,description
9742                ,attribute_category
9743                ,attribute1
9744                ,attribute2
9745                ,attribute3
9746                ,attribute4
9747                ,attribute5
9748                ,attribute6
9749                ,attribute7
9750                ,attribute8
9751                ,attribute9
9752                ,attribute10
9753                ,attribute11
9754                ,attribute12
9755                ,attribute13
9756                ,attribute14
9757                ,attribute15
9758                ,raw_cost_source
9759                ,burdened_cost_source
9760                ,quantity_source
9761                ,revenue_source
9762                ,pm_product_code
9763                ,pm_budget_line_reference
9764                ,cost_rejection_code
9765                ,revenue_rejection_code
9766                ,burden_rejection_code
9767                ,other_rejection_code
9768                ,code_combination_id
9769                ,ccid_gen_status_code
9770                ,ccid_gen_rej_message
9771                ,request_id
9772                ,borrowed_revenue
9773                ,tp_revenue_in
9774                ,tp_revenue_out
9775                ,revenue_adj
9776                ,lent_resource_cost
9777                ,tp_cost_in
9778                ,tp_cost_out
9779                ,cost_adj
9780                ,unassigned_time_cost
9781                ,utilization_percent
9782                ,utilization_hours
9783                ,utilization_adj
9784                ,capacity
9785                ,head_count
9786                ,head_count_adj
9787                ,projfunc_currency_code
9788                ,projfunc_cost_rate_type
9789                ,projfunc_cost_exchange_rate
9790                ,projfunc_cost_rate_date_type
9791                ,projfunc_cost_rate_date
9792                ,projfunc_rev_rate_type
9793                ,projfunc_rev_exchange_rate
9794                ,projfunc_rev_rate_date_type
9795                ,projfunc_rev_rate_date
9796                ,project_currency_code
9797                ,project_cost_rate_type
9798                ,project_cost_exchange_rate
9799                ,project_cost_rate_date_type
9800                ,project_cost_rate_date
9801                ,project_raw_cost
9802                ,project_burdened_cost
9803                ,project_rev_rate_type
9804                ,project_rev_exchange_rate
9805                ,project_rev_rate_date_type
9806                ,project_rev_rate_date
9807                ,project_revenue
9808                ,txn_raw_cost
9809                ,txn_burdened_cost
9810                ,txn_currency_code
9811                ,txn_revenue
9812                ,bucketing_period_code
9813                ,transfer_price_rate
9814                ,init_quantity
9815                ,init_quantity_source
9816                ,init_raw_cost
9817                ,init_burdened_cost
9818                ,init_revenue
9819                ,init_raw_cost_source
9820                ,init_burdened_cost_source
9821                ,init_revenue_source
9822                ,project_init_raw_cost
9823                ,project_init_burdened_cost
9824                ,project_init_revenue
9825                ,txn_init_raw_cost
9826                ,txn_init_burdened_cost
9827                ,txn_init_revenue
9828                ,txn_markup_percent
9829                ,txn_markup_percent_override
9830                ,txn_discount_percentage
9831                ,txn_standard_bill_rate
9832                ,txn_standard_cost_rate
9833                ,txn_cost_rate_override
9834                ,burden_cost_rate
9835                ,txn_bill_rate_override
9836                ,burden_cost_rate_override
9837                ,cost_ind_compiled_set_id
9838                ,pc_cur_conv_rejection_code
9839                ,pfc_cur_conv_rejection_code
9840 )
9841      SELECT     l_target_budget_line_id
9842                ,p_target_plan_version_id
9843                ,pra.resource_assignment_id
9844                ,rec_group_source_budget_lines.start_date
9845                ,sysdate -- last_update_date
9846                ,fnd_global.user_id -- last_updated_by
9847                ,sysdate  -- creation_date
9848                ,fnd_global.user_id -- created_by
9849                ,fnd_global.login_id -- last_update_login
9850                ,l_end_dateTab(l_ref)
9851                ,l_period_nameTab(l_ref)
9852                ,l_quantity_tot
9853                ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y', l_raw_cost_tot,NULL),NULL) -- raw_cost
9854                ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y', l_burdened_cost_tot,NULL),NULL) -- burdened_cost
9855                ,DECODE(l_adj_percentage,0,DECODE(l_revenue_flag,'Y', l_revenue_tot,NULL),NULL) -- revenue
9856                ,l_change_reason_codeTab(l_ref) -- change_reason_code
9857                ,l_descriptionTab(l_ref)-- description
9858                ,l_attribute_categoryTab(l_ref)
9859                ,l_attribute1Tab(l_ref)
9860                ,l_attribute2Tab(l_ref)
9861                ,l_attribute3Tab(l_ref)
9862                ,l_attribute4Tab(l_ref)
9863                ,l_attribute5Tab(l_ref)
9864                ,l_attribute6Tab(l_ref)
9865                ,l_attribute7Tab(l_ref)
9866                ,l_attribute8Tab(l_ref)
9867                ,l_attribute9Tab(l_ref)
9868                ,l_attribute10Tab(l_ref)
9869                ,l_attribute11Tab(l_ref)
9870                ,l_attribute12Tab(l_ref)
9871                ,l_attribute13Tab(l_ref)
9872                ,l_attribute14Tab(l_ref)
9873                ,l_attribute15Tab(l_ref)
9874                ,DECODE(l_cost_flag,'Y',PA_FP_CONSTANTS_PKG.G_AMOUNT_SOURCE_COPY_P,NULL) --raw_cost_souce
9875                ,DECODE(l_cost_flag,'Y',PA_FP_CONSTANTS_PKG.G_AMOUNT_SOURCE_COPY_P,NULL) --burdened_cost_source
9876                ,PA_FP_CONSTANTS_PKG.G_AMOUNT_SOURCE_COPY_P  --quantity_source
9877                ,DECODE(l_revenue_flag,'Y',PA_FP_CONSTANTS_PKG.G_AMOUNT_SOURCE_COPY_P,NULL) --revenue source
9878                ,null -- pm_product_code
9879                ,null
9880                ,null
9881                ,null
9882                ,null
9883                ,null
9884                ,null
9885                ,null
9886                ,null
9887                ,null
9888                ,null
9889                ,null
9890                ,null
9891                ,null
9892                ,null
9893                ,null
9894                ,null
9895                ,null
9896                ,null
9897                ,null
9898                ,null
9899                ,null
9900                ,null
9901                ,null
9902                ,null  -- head_count_adj
9903                ,l_projfunc_currency_codeTab(l_ref)
9904                ,DECODE(l_cost_flag,'Y','User',NULL) -- projfunc_cost_rate_type
9905                ,DECODE(l_cost_flag,'Y',1,NULL) -- projfunc_cost_exchange_rate
9906                ,null -- projfunc_cost_rate_date_type
9907                ,null -- projfunc_cost_rate_date
9908                ,'User' -- projfunc_rev_rate_type
9909                ,1 -- projfunc_rev_exchange_rate
9910                ,null -- projfunc_rev_rate_date_type
9911                ,null -- projfunc_rev_rate_date
9912                ,l_project_currency_codeTab(l_ref)
9913                ,DECODE(l_cost_flag,'Y','User',NULL) -- project_cost_rate_type
9914                ,DECODE(l_cost_flag,'Y',l_project_cost_exchange_rate,NULL) -- project_cost_exchange_rate
9915                ,null -- project_cost_rate_date_type
9916                ,null  -- project_cost_rate_date
9917                ,DECODE(l_adj_percentage,0,
9918                                 DECODE(l_cost_flag,'Y', l_project_raw_cost_tot,NULL),NULL) --project_raw_cost
9919                ,DECODE(l_adj_percentage,0,
9920                               DECODE(l_cost_flag,'Y', l_project_burdened_cost_tot,NULL),NULL) -- project_burdened_cost
9921                ,'User' -- project_rev_rate_type
9922                ,l_proj_rev_ex_rate -- project_rev_exchange_rate
9923                ,null -- project_rev_rate_date_type
9924                ,null -- project_rev_rate_date
9925                ,DECODE(l_adj_percentage,0,
9926                                DECODE(l_revenue_flag,'Y', l_project_revenue_tot,NULL),NULL) -- project_revenue
9927                ,DECODE(l_cost_flag,'Y',
9928                         decode(GREATEST(rec_group_source_budget_lines.start_date,NVL(l_etc_start_date,rec_group_source_budget_lines.start_date)),rec_group_source_budget_lines.start_date
9929                         ,l_raw_cost_tot*(1+l_adj_percentage),l_raw_cost_tot),NULL) -- txn_raw_cost
9930                ,DECODE(l_cost_flag,'Y',
9931                         decode(GREATEST(rec_group_source_budget_lines.start_date,NVL(l_etc_start_date,rec_group_source_budget_lines.start_date)),rec_group_source_budget_lines.start_date
9932                         ,l_burdened_cost_tot*(1+l_adj_percentage),l_burdened_cost_tot),NULL) -- txn_burdened_cost
9933                ,l_projfunc_currency_codeTab(l_ref) -- txn_currency_code
9934                ,DECODE(l_revenue_flag,'Y',
9935                         decode(GREATEST(rec_group_source_budget_lines.start_date,NVL(l_etc_start_date,rec_group_source_budget_lines.start_date)),rec_group_source_budget_lines.start_date
9936                         ,l_revenue_tot*(1+l_adj_percentage),l_revenue_tot),NULL) -- txn_revenue
9937                ,null -- bucketing_period_code
9938                ,null -- transfer_price_rate
9939                ,NULL --init_quantity
9940                ,NULL --init_quantity_source
9941                ,NULL --init_raw_cost
9942                ,NULL --init_burdened_cost
9943                ,NULL --init_revenue
9944                ,NULL --init_raw_cost_source
9945                ,NULL --init_burdened_cost_source
9946                ,NULL --init_revenue_source
9947                ,NULL --project_init_raw_cost
9948                ,NULL --project_init_burdened_cost
9949                ,NULL --project_init_revenue
9950                ,NULL --txn_init_raw_cost
9951                ,NULL --txn_init_burdened_cost
9952                ,NULL --txn_init_revenue
9953                ,null  -- txn_markup_percent
9954                ,null -- txn_markup_percent_override
9955                ,null -- txn_discount_percentage
9956                ,null -- txn_standard_bill_rate
9957                ,null -- txn_standard_cost_rate
9958                ,DECODE(l_cost_flag,'Y',DECODE(p_derv_rates_missing_amts_flag,'N',l_txn_cost_rate_override,NULL),NULL) -- txn_cost_rate_override
9959                ,null -- burden_cost_rate
9960                ,DECODE(l_revenue_flag,'Y',DECODE(p_derv_rates_missing_amts_flag,'N',l_txn_bill_rate_override,NULL),NULL) -- txn_bill_rate_override
9961                ,DECODE(l_cost_flag,'Y',DECODE(p_derv_rates_missing_amts_flag,'N',l_burden_cost_rate_override,NULL),NULL) -- burden_cost_rate_override
9962                ,null -- cost_ind_compiled_set_id
9963                ,null -- pc_cur_conv_rejection_code
9964                ,null -- pfc_cur_conv_rejection_code
9965        FROM pa_resource_assignments pra
9966        WHERE rec_group_source_budget_lines.resource_assignment_id = pra.parent_assignment_id
9967        AND   pra.budget_version_id=p_target_plan_version_id;
9968 
9969        END IF; -- l_budget_line_idTab.count
9970 
9971        END LOOP;
9972 
9973        -- Bug 4035856 Call rounding api if l_adj_percentage is not zero
9974        IF l_adj_percentage <> 0 THEN
9975             PA_FP_MULTI_CURRENCY_PKG.Round_Budget_Line_Amounts
9976                    (  p_budget_version_id     => p_target_plan_version_id
9977                      ,p_calling_context       => 'COPY_VERSION'
9978                      ,x_return_status         => l_return_status
9979                      ,x_msg_count             => l_msg_count
9980                      ,x_msg_data              => l_msg_data);
9981 
9982              IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
9983                  IF P_PA_debug_mode = 'Y' THEN
9984                       pa_debug.g_err_stage:= 'Error in PA_FP_MULTI_CURRENCY_PKG.Round_Budget_Line_Amounts';
9985                       pa_debug.write('Copy_Budget_Lines_Appr_Rev: ' || g_module_name,pa_debug.g_err_stage,5);
9986                  END IF;
9987                  RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9988              END IF;
9989        END IF;
9990 
9991        --Bug 4290043. Call the API to correct the missing amounts in the target version in case it can have
9992        --missing amounts/rates
9993        IF p_derv_rates_missing_amts_flag = 'Y' OR
9994           l_adj_percentage <> 0 THEN
9995 
9996 
9997             derv_missing_amts_chk_neg_qty
9998             (p_budget_version_id            => p_target_plan_version_id,
9999              p_targ_pref_code               => l_targ_pref_code,
10000              p_source_version_type          => l_source_version_type,
10001              p_target_version_type          => l_target_version_type,
10002              p_src_plan_class_code          => l_src_plan_class_code,
10003              p_derv_rates_missing_amts_flag => p_derv_rates_missing_amts_flag,
10004              p_adj_percentage               => l_adj_percentage,
10005              x_return_status                => l_return_status,
10006              x_msg_count                    => l_msg_count,
10007              x_msg_data                     => l_msg_data);
10008 
10009              IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
10010                  IF P_PA_debug_mode = 'Y' THEN
10011                       pa_debug.g_err_stage:= 'Error in derv_missing_amts_chk_neg_qty';
10012                       pa_debug.write('Copy_Budget_Lines_Appr_Rev: ' || g_module_name,pa_debug.g_err_stage,5);
10013                  END IF;
10014                  RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
10015 
10016             END IF;
10017 
10018        END IF;--IF p_derv_rates_missing_amts_flag = 'Y' THEN
10019 
10020        IF P_PA_DEBUG_MODE = 'Y' THEN
10021            pa_debug.g_err_stage:='Exiting Copy_Budget_Lines_Appr_Rev';
10022            pa_debug.write('Copy_Budget_Lines_Appr_Rev: ' || g_module_name,pa_debug.g_err_stage,3);
10023            pa_debug.reset_err_stack;    -- bug:- 2815593
10024 	END IF;
10025   EXCEPTION
10026 
10027       WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
10028              l_msg_count := FND_MSG_PUB.count_msg;
10029              IF l_msg_count = 1 THEN
10030                PA_INTERFACE_UTILS_PUB.get_messages
10031                    (p_encoded        => FND_API.G_TRUE
10032                     ,p_msg_index      => 1
10033                     ,p_msg_count      => l_msg_count
10034                     ,p_msg_data       => l_msg_data
10035                     ,p_data           => l_data
10036                     ,p_msg_index_out  => l_msg_index_out);
10037                x_msg_data := l_data;
10038                x_msg_count := l_msg_count;
10039            ELSE
10040                x_msg_count := l_msg_count;
10041            END IF;
10042            x_return_status := FND_API.G_RET_STS_ERROR;
10043 
10044            IF P_PA_DEBUG_MODE = 'Y' THEN
10045                pa_debug.g_err_stage:='Invalid arguments passed';
10046                pa_debug.write('Copy_Budget_Lines_Appr_Rev: ' || g_module_name,pa_debug.g_err_stage,5);
10047                pa_debug.reset_err_stack;
10048 	  END IF;
10049            RAISE;
10050 
10051     WHEN Others THEN
10052         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10053         x_msg_count     := 1;
10054         x_msg_data      := SQLERRM;
10055         FND_MSG_PUB.add_exc_msg( p_pkg_name=> 'PA_FP_COPY_FROM_PKG'
10056                             ,p_procedure_name  => 'COPY_BUDGET_LINES_APPR_REV');
10057 
10058         IF P_PA_DEBUG_MODE = 'Y' THEN
10059             pa_debug.g_err_stage:='Unexpected error'||SQLERRM;
10060             pa_debug.write('Copy_Budget_Lines_Appr_Rev: ' || g_module_name,pa_debug.g_err_stage,6);
10061              pa_debug.reset_err_stack;
10062 	END IF;
10063         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10064  END Copy_Budget_Lines_Appr_Rev;
10065 
10066 /*=============================================================================
10067  Bug 3619687: PJ.M:B5:BF:DEV:TRACKING BUG FOR PLAN SETTINGS CHANGE REQUEST
10068  When a new workplan structure version is created from a published version, this
10069  api is called to synchronise all the additional workplan settings related data.
10070  This api is called from copy_wp_budget_versions api at the end of copying all
10071  the budgets related data from source published version.
10072  Synchronisation involves:
10073   1) pa_fp_txn_currencies
10074   2) rate schedules, generation options and plan settings data
10075      (pa_proj_fp_options)
10076 
10077  Stating some of the business rules for clarity:
10078       i) If there is a published version, time phasing can not be changed
10079      ii) Planning resource list can change only if existing resource list is
10080          'None'. To handle this case, we would re-map the resource assignments
10081          data. Please note that in this case, only 'PEOPLE' resource class assignments
10082          would be present.
10083     iii) RBS can be different only if existing RBS is null.
10084 
10085  Bug 3725414: In update to pa_proj_fp_options, rbs_version_id column is missing
10086 
10087  Bug 4101153: Current Planning period should always get the value from the source version and
10088  not from the workplan plan type option. Removed the update to current planning period
10089 
10090  Bug 4337221: dbora- Excluded the quantity and cost amount columns from the update
10091  statement on pa_budget_versions, so that the quantity and cost amount columns gets
10092  copied as it is from the source version retaining the version level rolled up figures.
10093 ==============================================================================*/
10094 
10095 PROCEDURE Update_Plan_Setup_For_WP_Copy(
10096            p_project_id           IN   pa_projects_all.project_id%TYPE
10097           ,p_wp_version_id        IN   pa_budget_versions.fin_plan_type_id%TYPE
10098           ,x_return_status        OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
10099           ,x_msg_count            OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
10100           ,x_msg_data             OUT  NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
10101 AS
10102 
10103     --Start of variables used for debugging
10104 
10105     l_return_status      VARCHAR2(1);
10106     l_msg_count          NUMBER := 0;
10107     l_msg_data           VARCHAR2(2000);
10108     l_data               VARCHAR2(2000);
10109     l_msg_index_out      NUMBER;
10110     l_debug_mode         VARCHAR2(30);
10111     l_error_msg_code     VARCHAR2(30);
10112 
10113 
10114     --End of variables used for debugging
10115 
10116     l_resource_list_change_flag  VARCHAR2(1);
10117     l_rbs_version_change_flag    VARCHAR2(1);
10118     l_people_res_class_rlm_id    pa_resource_list_members.resource_list_member_id%TYPE;
10119     l_equip_res_class_rlm_id     pa_resource_list_members.resource_list_member_id%TYPE;
10120     l_fin_res_class_rlm_id       pa_resource_list_members.resource_list_member_id%TYPE;
10121     l_mat_res_class_rlm_id       pa_resource_list_members.resource_list_member_id%TYPE;
10122 
10123     l_txn_source_id_tbl          SYSTEM.pa_num_tbl_type := SYSTEM.pa_num_tbl_type();
10124     l_res_list_member_id_tbl     SYSTEM.pa_num_tbl_type := SYSTEM.pa_num_tbl_type();
10125     l_rbs_element_id_tbl         SYSTEM.pa_num_tbl_type := SYSTEM.pa_num_tbl_type();
10126     l_txn_accum_header_id_tbl    SYSTEM.pa_num_tbl_type := SYSTEM.pa_num_tbl_type();
10127 
10128     l_budget_version_id_tbl      SYSTEM.pa_num_tbl_type := SYSTEM.pa_num_tbl_type();
10129 
10130     CURSOR parent_plan_type_cur IS
10131       SELECT  pfo.proj_fp_options_id
10132              ,pfo.track_workplan_costs_flag
10133              ,pfo.plan_in_multi_curr_flag
10134              ,pfo.rbs_version_id
10135              ,pfo.margin_derived_from_code
10136              ,pfo.factor_by_code
10137              ,pfo.cost_resource_list_id
10138              ,pfo.select_cost_res_auto_flag
10139              ,pfo.cost_time_phased_code
10140              ,pfo.cost_period_mask_id
10141              ,pfo.projfunc_cost_rate_type
10142              ,pfo.projfunc_cost_rate_date_type
10143              ,pfo.projfunc_cost_rate_date
10144              ,pfo.project_cost_rate_type
10145              ,pfo.project_cost_rate_date_type
10146              ,pfo.project_cost_rate_date
10147              ,pfo.use_planning_rates_flag
10148              ,pfo.res_class_raw_cost_sch_id
10149              ,pfo.cost_emp_rate_sch_id
10150              ,pfo.cost_job_rate_sch_id
10151              ,pfo.cost_non_labor_res_rate_sch_id
10152              ,pfo.cost_res_class_rate_sch_id
10153              ,pfo.cost_burden_rate_sch_id
10154       FROM   pa_proj_fp_options pfo
10155              ,pa_fin_plan_types_b fpt
10156       WHERE  pfo.project_id = p_project_id
10157       AND    pfo.fin_plan_type_id = fpt.fin_plan_type_id
10158       AND    fpt.use_for_workplan_flag = 'Y'
10159       AND    pfo.fin_plan_option_level_code = 'PLAN_TYPE';
10160 
10161     parent_plan_type_rec    parent_plan_type_cur%ROWTYPE;
10162 
10163     CURSOR wp_version_options_cur IS
10164       SELECT pfo.proj_fp_options_id
10165              ,pfo.rbs_version_id
10166              ,pfo.cost_resource_list_id
10167       FROM   pa_proj_fp_options  pfo
10168              ,pa_budget_versions bv
10169       WHERE  bv.budget_version_id =  p_wp_version_id
10170       AND    bv.project_id = pfo.project_id
10171       AND    pfo.fin_plan_version_id = bv.budget_version_id;
10172 
10173     wp_version_options_rec  wp_version_options_cur%ROWTYPE;
10174 
10175 
10176 BEGIN
10177 
10178     x_msg_count := 0;
10179     x_return_status := FND_API.G_RET_STS_SUCCESS;
10180 
10181     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
10182     l_debug_mode := NVL(l_debug_mode, 'Y');
10183 
10184     -- Set curr function
10185    IF l_debug_mode = 'Y' THEN
10186     pa_debug.set_curr_function(
10187                 p_function   =>'PA_FP_COPY_FROM_PKG.Update_Plan_Setup_For_WP_Copy'
10188                ,p_debug_mode => l_debug_mode );
10189 
10190     -- Check for business rules violations
10191         pa_debug.g_err_stage:='Validating input parameters';
10192         pa_debug.write('Update_Plan_Setup_For_WP_Copy: ' || g_module_name,pa_debug.g_err_stage,3);
10193     END IF;
10194 
10195     IF (p_project_id IS NULL) OR
10196        (p_wp_version_id IS NULL)
10197     THEN
10198 
10199         IF l_debug_mode = 'Y' THEN
10200            pa_debug.g_err_stage:='p_project_id = '||p_project_id;
10201            pa_debug.write('Update_Plan_Setup_For_WP_Copy: ' || g_module_name,pa_debug.g_err_stage,5);
10202 
10203            pa_debug.g_err_stage:='p_wp_version_id = '||p_wp_version_id;
10204            pa_debug.write('Update_Plan_Setup_For_WP_Copy: ' || g_module_name,pa_debug.g_err_stage,5);
10205         END IF;
10206 
10207         PA_UTILS.ADD_MESSAGE( p_app_short_name => 'PA',
10208                               p_msg_name       => 'PA_FP_INV_PARAM_PASSED',
10209                               p_token1         => 'PROCEDURENAME',
10210                               p_value1         => 'PA_FP_COPY_FROM_PKG.Update_Plan_Setup_For_WP_Copy');
10211 
10212         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
10213 
10214     END IF;
10215 
10216     -- Fetch all the plan type values that could have changed
10217     OPEN  parent_plan_type_cur;
10218     FETCH parent_plan_type_cur INTO parent_plan_type_rec;
10219     CLOSE parent_plan_type_cur;
10220 
10221     -- Fetch options id for the workplan version
10222     OPEN  wp_version_options_cur;
10223     FETCH wp_version_options_cur INTO wp_version_options_rec;
10224     CLOSE wp_version_options_cur;
10225 
10226     -- Check if resource list has changed
10227     IF wp_version_options_rec.cost_resource_list_id <> parent_plan_type_rec.cost_resource_list_id
10228     THEN
10229         l_resource_list_change_flag := 'Y';
10230     ELSE
10231         l_resource_list_change_flag  := 'N';
10232     END IF;
10233 
10234     -- Check if rbs version has changed
10235     IF  nvl(wp_version_options_rec.rbs_version_id,-99) <> nvl(parent_plan_type_rec.rbs_version_id,-99)
10236     THEN
10237         l_rbs_version_change_flag := 'Y';
10238     ELSE
10239         l_rbs_version_change_flag := 'N';
10240     END IF;
10241 
10242     -- Update pa_budget_versions table data
10243     -- Note that period mask and planning period could have changed so this update
10244     -- is necessary even if resource list is not changed
10245 
10246     UPDATE pa_budget_versions
10247     SET   resource_list_id            = parent_plan_type_rec.cost_resource_list_id
10248          ,period_mask_id              = parent_plan_type_rec.cost_period_mask_id
10249 /* Bug 4337221: removed from the update
10250          ,raw_cost                    = 0
10251          ,burdened_cost               = 0
10252          ,total_project_raw_cost      = 0
10253          ,total_project_burdened_cost = 0
10254          ,labor_quantity              = 0
10255          ,equipment_quantity          = 0
10256 */
10257          ,last_update_date            = SYSDATE
10258          ,last_updated_by             = FND_GLOBAL.user_id
10259          ,last_update_login           = FND_GLOBAL.login_id
10260          ,record_version_number       = record_version_number + 1
10261     WHERE budget_version_id =  p_wp_version_id;
10262 
10263     -- Update pa_proj_fp_options entity
10264     UPDATE pa_proj_fp_options
10265     SET   track_workplan_costs_flag           =  parent_plan_type_rec.track_workplan_costs_flag
10266          ,plan_in_multi_curr_flag             =  parent_plan_type_rec.plan_in_multi_curr_flag
10267          ,margin_derived_from_code            =  parent_plan_type_rec.margin_derived_from_code
10268          ,factor_by_code                      =  parent_plan_type_rec.factor_by_code
10269          ,cost_resource_list_id               =  parent_plan_type_rec.cost_resource_list_id
10270          ,select_cost_res_auto_flag           =  parent_plan_type_rec.select_cost_res_auto_flag
10271          ,cost_time_phased_code               =  parent_plan_type_rec.cost_time_phased_code
10272          ,cost_period_mask_id                 =  parent_plan_type_rec.cost_period_mask_id
10273          ,projfunc_cost_rate_type             =  parent_plan_type_rec.projfunc_cost_rate_type
10274          ,projfunc_cost_rate_date_type        =  parent_plan_type_rec.projfunc_cost_rate_date_type
10275          ,projfunc_cost_rate_date             =  parent_plan_type_rec.projfunc_cost_rate_date
10276          ,project_cost_rate_type              =  parent_plan_type_rec.project_cost_rate_type
10277          ,project_cost_rate_date_type         =  parent_plan_type_rec.project_cost_rate_date_type
10278          ,project_cost_rate_date              =  parent_plan_type_rec.project_cost_rate_date
10279          ,use_planning_rates_flag             =  parent_plan_type_rec.use_planning_rates_flag
10280          ,res_class_raw_cost_sch_id           =  parent_plan_type_rec.res_class_raw_cost_sch_id
10281          ,cost_emp_rate_sch_id                =  parent_plan_type_rec.cost_emp_rate_sch_id
10282          ,cost_job_rate_sch_id                =  parent_plan_type_rec.cost_job_rate_sch_id
10283          ,cost_non_labor_res_rate_sch_id      =  parent_plan_type_rec.cost_non_labor_res_rate_sch_id
10284          ,cost_res_class_rate_sch_id          =  parent_plan_type_rec.cost_res_class_rate_sch_id
10285          ,cost_burden_rate_sch_id             =  parent_plan_type_rec.cost_burden_rate_sch_id
10286          ,rbs_version_id                      =  parent_plan_type_rec.rbs_version_id -- Bug 3725414
10287          ,record_version_number               =  record_version_number + 1
10288          ,last_update_date                    =  SYSDATE
10289          ,last_updated_by                     =  FND_GLOBAL.user_id
10290          ,last_update_login                   =  FND_GLOBAL.login_id
10291     WHERE proj_fp_options_id  = wp_version_options_rec.proj_fp_options_id;
10292 
10293     -- Copy MC currencies from plan type
10294 
10295     PA_FP_TXN_CURRENCIES_PUB.copy_fp_txn_currencies (
10296              p_source_fp_option_id        => parent_plan_type_rec.proj_fp_options_id
10297              ,p_target_fp_option_id       => wp_version_options_rec.proj_fp_options_id
10298              ,p_target_fp_preference_code => NULL
10299              ,p_plan_in_multi_curr_flag   => parent_plan_type_rec.plan_in_multi_curr_flag
10300              ,x_return_status             => x_return_status
10301              ,x_msg_count                 => x_msg_count
10302              ,x_msg_data                  => x_msg_data );
10303 
10304     IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10305         IF l_debug_mode = 'Y' THEN
10306            pa_debug.g_err_stage:='Called API PA_FP_TXN_CURRENCIES_PUB.copy_fp_txn_currencies
10307                                   api returned error';
10308            pa_debug.write('Update_Plan_Setup_For_WP_Copy:  ' || g_module_name,pa_debug.g_err_stage,5);
10309         END IF;
10310         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
10311     END IF;
10312 
10313     -- If resource list has changed, resource assingments data should be re-mapped as per
10314     -- the new resource list
10315     IF l_resource_list_change_flag = 'Y' THEN
10316         PA_PLANNING_TRANSACTION_UTILS.Get_Res_Class_Rlm_Ids
10317              ( p_project_id                   =>    p_project_id
10318               ,p_resource_list_id             =>    parent_plan_type_rec.cost_resource_list_id
10319               ,x_people_res_class_rlm_id      =>    l_people_res_class_rlm_id
10320               ,x_equip_res_class_rlm_id       =>    l_equip_res_class_rlm_id
10321               ,x_fin_res_class_rlm_id         =>    l_fin_res_class_rlm_id
10322               ,x_mat_res_class_rlm_id         =>    l_mat_res_class_rlm_id
10323               ,x_return_status                =>    x_return_status
10324               ,x_msg_count                    =>    x_msg_count
10325               ,x_msg_data                     =>    x_msg_data);
10326 
10327         IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10328             IF l_debug_mode = 'Y' THEN
10329                pa_debug.g_err_stage:='Called API PA_PLANNING_TRANSACTION_UTILS.Get_Res_Class_Rlm_Ids
10330                                       api returned error';
10331                pa_debug.write('Update_Plan_Setup_For_WP_Copy:  ' || g_module_name,pa_debug.g_err_stage,5);
10332             END IF;
10333             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
10334         END IF;
10335 
10336         -- Update all the task planning elements with new PEOPLE rlmid
10337         IF l_debug_mode = 'Y' THEN
10338             pa_debug.g_err_stage:='Updaing res assignments with new PEOPLE rlmid : '
10339                                   || l_people_res_class_rlm_id;
10340             pa_debug.write('Update_Plan_Setup_For_WP_Copy: ' || g_module_name,pa_debug.g_err_stage,3);
10341         END IF;
10342 
10343         UPDATE pa_resource_assignments
10344         SET    resource_list_member_id  = l_people_res_class_rlm_id
10345         WHERE  budget_version_id = p_wp_version_id
10346         AND    resource_class_code = 'PEOPLE'
10347         AND    resource_class_flag = 'Y';
10348     END IF;
10349 
10350     -- If rbs version has changed, call pji_create for summarising the data
10351     IF l_rbs_version_change_flag = 'Y'
10352     THEN
10353          -- Call RBS mapping api for the entire version
10354          -- The api returns rbs element id, txn accum header id for each
10355          -- resource assignment id in the form of plsql tables
10356          PA_RLMI_RBS_MAP_PUB.Map_Rlmi_Rbs(
10357               p_budget_version_id            =>   p_wp_version_id
10358              ,p_resource_list_id             =>   parent_plan_type_rec.cost_resource_list_id
10359              ,p_rbs_version_id               =>   parent_plan_type_rec.rbs_version_id
10360              ,p_calling_process              =>   'RBS_REFRESH'
10361              ,p_calling_context              =>   'PLSQL'
10362              ,p_process_code                 =>   'RBS_MAP'
10363              ,p_calling_mode                 =>   'BUDGET_VERSION'
10364              ,p_init_msg_list_flag           =>   'N'
10365              ,p_commit_flag                  =>   'N'
10366              ,x_txn_source_id_tab            =>   l_txn_source_id_tbl
10367              ,x_res_list_member_id_tab       =>   l_res_list_member_id_tbl
10368              ,x_rbs_element_id_tab           =>   l_rbs_element_id_tbl
10369              ,x_txn_accum_header_id_tab      =>   l_txn_accum_header_id_tbl
10370              ,x_return_status                =>   x_return_status
10371              ,x_msg_count                    =>   x_msg_count
10372              ,x_msg_data                     =>   x_msg_data);
10373 
10374          -- Bug 3579153 Check return status
10375          IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10376              IF l_debug_mode = 'Y' THEN
10377                 pa_debug.g_err_stage:='Called API PA_PLANNING_TRANSACTION_UTILS.Map_Rlmi_Rbs api returned error';
10378                 pa_debug.write('Update_Plan_Setup_For_WP_Copy:  ' || g_module_name,pa_debug.g_err_stage,5);
10379              END IF;
10380              RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
10381          ELSE
10382              -- Check count of the required out tables to be the same
10383              IF l_txn_source_id_tbl.count <> l_rbs_element_id_tbl.count OR
10384                 l_txn_source_id_tbl.count <> l_txn_accum_header_id_tbl.count
10385              THEN
10386                  IF l_debug_mode = 'Y' THEN
10387                     pa_debug.g_err_stage:='Called API PA_PLANNING_TRANSACTION_UTILS.Map_Rlmi_Rbs api
10388                                            returned out tables with different count';
10389                     pa_debug.write('Update_Plan_Setup_For_WP_Copy:  ' || g_module_name,pa_debug.g_err_stage,5);
10390                     pa_debug.g_err_stage:='l_txn_source_id_tbl.count = ' || l_txn_source_id_tbl.count;
10391                     pa_debug.write('Update_Plan_Setup_For_WP_Copy:  ' || g_module_name,pa_debug.g_err_stage,5);
10392                     pa_debug.g_err_stage:='l_rbs_element_id_tbl.count = ' || l_rbs_element_id_tbl.count;
10393                     pa_debug.write('Update_Plan_Setup_For_WP_Copy:  ' || g_module_name,pa_debug.g_err_stage,5);
10394                     pa_debug.g_err_stage:=
10395                          'l_txn_accum_header_id_tbl.count = ' || l_txn_accum_header_id_tbl.count;
10396                     pa_debug.write('Update_Plan_Setup_For_WP_Copy:  ' || g_module_name,pa_debug.g_err_stage,5);
10397                  END IF;
10398                  RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
10399              END IF;
10400          END IF;
10401 
10402          -- Check if out table has any records first
10403          IF nvl(l_txn_source_id_tbl.last,0) >= 1 THEN
10404 
10405              -- Update resource assignments data for the version
10406              FORALL i IN l_txn_source_id_tbl.first .. l_txn_source_id_tbl.last
10407                   UPDATE pa_resource_assignments
10408                   SET     rbs_element_id          =  l_rbs_element_id_tbl(i)
10409                          ,txn_accum_header_id     =  l_txn_accum_header_id_tbl(i)
10410                          ,record_version_number   =  record_version_number + 1
10411                          ,last_update_date        =  SYSDATE
10412                          ,last_updated_by         =  FND_GLOBAL.user_id
10413                          ,last_update_login       =  FND_GLOBAL.login_id
10414                   WHERE  budget_version_id = p_wp_version_id
10415                   AND    resource_assignment_id = l_txn_source_id_tbl(i);
10416          END IF;
10417 
10418          -- populating the l_budget_version_id_tbl with p_budget_version_id
10419          l_budget_version_id_tbl := SYSTEM.pa_num_tbl_type(p_wp_version_id);
10420 
10421 
10422     END IF;
10423 
10424     IF l_debug_mode = 'Y' THEN
10425         pa_debug.g_err_stage:='Exiting Update_Plan_Setup_For_WP_Copy';
10426         pa_debug.write('Update_Plan_Setup_For_WP_Copy: ' || g_module_name,pa_debug.g_err_stage,3);
10427     -- reset curr function
10428         pa_debug.reset_curr_function();
10429     END IF;
10430 
10431 EXCEPTION
10432 
10433    WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
10434        l_msg_count := FND_MSG_PUB.count_msg;
10435        IF l_msg_count = 1 THEN
10436            PA_INTERFACE_UTILS_PUB.get_messages
10437                  (p_encoded        => FND_API.G_TRUE
10438                   ,p_msg_index      => 1
10439                   ,p_msg_count      => l_msg_count
10440                   ,p_msg_data       => l_msg_data
10441                   ,p_data           => l_data
10442                   ,p_msg_index_out  => l_msg_index_out);
10443 
10444            x_msg_data := l_data;
10445            x_msg_count := l_msg_count;
10446        ELSE
10447            x_msg_count := l_msg_count;
10448        END IF;
10449 
10450        x_return_status := FND_API.G_RET_STS_ERROR;
10451 
10452        IF l_debug_mode = 'Y' THEN
10453            pa_debug.g_err_stage:='Invalid Arguments Passed Or called api raised an error';
10454            pa_debug.write('Update_Plan_Setup_For_WP_Copy: ' || g_module_name,pa_debug.g_err_stage,5);
10455           -- reset curr function
10456            pa_debug.reset_curr_function();
10457        END IF;
10458        RETURN;
10459    WHEN Others THEN
10460        x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10461        x_msg_count     := 1;
10462        x_msg_data      := SQLERRM;
10463 
10464        FND_MSG_PUB.add_exc_msg( p_pkg_name        => 'PA_FP_COPY_FROM_PKG'
10465                                ,p_procedure_name  => 'Update_Plan_Setup_For_WP_Copy');
10466 
10467        IF l_debug_mode = 'Y' THEN
10468            pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
10469            pa_debug.write('Update_Plan_Setup_For_WP_Copy: ' || g_module_name,pa_debug.g_err_stage,5);
10470        -- reset curr function
10471        pa_debug.Reset_Curr_Function();
10472        END IF;
10473 
10474        RAISE;
10475 END Update_Plan_Setup_For_WP_Copy;
10476 
10477 END pa_fp_copy_from_pkg;