DBA Data[Home] [Help]

PACKAGE BODY: APPS.PSB_WS_OPS_PVT

Source


1 PACKAGE BODY PSB_WS_Ops_Pvt AS
2 /* $Header: PSBVWLOB.pls 120.14 2005/09/02 16:37:06 shtripat ship $ */
3 
4   G_PKG_NAME CONSTANT VARCHAR2(30):= 'PSB_WS_Ops_Pvt';
5 
6 /*--------------------------- Global variables -----------------------------*/
7   g_current_date           DATE   := sysdate;
8   g_current_user_id        NUMBER := FND_GLOBAL.USER_ID;
9   g_current_login_id       NUMBER := FND_GLOBAL.LOGIN_ID;
10 /*----------------------- End Global variables -----------------------------*/
11 
12 
13 /*----------------------- Private Routine prototypes  -----------------------*/
14 
15   PROCEDURE Create_Local_Dist_Pvt
16   (
17     p_account_line_id       IN   psb_ws_lines.account_line_id%TYPE            ,
18     p_new_worksheet_id      IN   psb_worksheets.worksheet_id%TYPE             ,
19     p_new_position_line_id  IN   psb_ws_lines_positions.position_line_id%TYPE ,
20     p_return_status         OUT  NOCOPY  VARCHAR2
21   ) ;
22 
23   PROCEDURE Insert_WS_Lines_Pvt
24   (
25     p_worksheet_id          IN   psb_ws_lines.worksheet_id%TYPE,
26     p_account_line_id       IN   psb_ws_lines.account_line_id%TYPE,
27     p_freeze_flag           IN   psb_ws_lines.freeze_flag%TYPE,
28     p_view_line_flag        IN   psb_ws_lines.view_line_flag%TYPE,
29     p_last_update_date      IN   psb_ws_lines.last_update_date%TYPE,
30     p_last_updated_by       IN   psb_ws_lines.last_updated_by%TYPE,
31     p_last_update_login     IN   psb_ws_lines.last_update_login%TYPE,
32     p_created_by            IN   psb_ws_lines.created_by%TYPE,
33     p_creation_date         IN   psb_ws_lines.creation_date%TYPE,
34     p_return_status         OUT  NOCOPY  VARCHAR2
35   ) ;
36 
37   PROCEDURE Delete_Worksheet_Pvt
38   (
39     p_worksheet_id          IN   psb_worksheets.worksheet_id%TYPE ,
40     p_budget_by_position    IN   psb_worksheets.budget_by_position%TYPE ,
41     p_delete_lines_flag     IN   VARCHAR2 ,
42     p_return_status         OUT  NOCOPY  VARCHAR2
43   ) ;
44 
45 /*------------------- End Private Routines prototypes  ----------------------*/
46 
47 
48 /*===========================================================================+
49  |                             PROCEDURE pd                                  |
50  +===========================================================================*/
51 PROCEDURE pd( p_message   IN     VARCHAR2)
52 IS
53 BEGIN
54   NULL;
55   --DBMS_OUTPUT.Put_Line(p_message) ;
56 END pd ;
57 /*---------------------------------------------------------------------------*/
58 
59 
60 /*===========================================================================+
61  |                   PROCEDURE Enforce_WS_Concurrency                        |
62  +===========================================================================*/
63 --
64 -- The worksheet operations may affect one or more worksheets depending on
65 -- the type of the operation. This API locks all the relevent worksheets
66 -- required for a worksheet operation.
67 --
68 PROCEDURE Enforce_WS_Concurrency
69 (
70   p_api_version               IN       NUMBER   ,
71   p_init_msg_list             IN       VARCHAR2 := FND_API.G_FALSE ,
72   p_commit                    IN       VARCHAR2 := FND_API.G_FALSE ,
73   p_validation_level          IN       NUMBER   := FND_API.G_VALID_LEVEL_FULL ,
74   p_return_status             OUT  NOCOPY      VARCHAR2 ,
75   p_msg_count                 OUT  NOCOPY      NUMBER   ,
76   p_msg_data                  OUT  NOCOPY      VARCHAR2 ,
77   --
78   p_worksheet_id              IN       psb_worksheets.worksheet_id%TYPE  ,
79   p_parent_or_child_mode      IN       VARCHAR2 ,
80   p_maintenance_mode          IN       VARCHAR2 := 'MAINTENANCE'
81 )
82 IS
83   --
84   l_api_name                CONSTANT VARCHAR2(30) := 'Enforce_WS_Concurrency' ;
85   l_api_version             CONSTANT NUMBER       :=  1.0 ;
86   --
87   l_return_status           VARCHAR2(1) ;
88   l_msg_count               NUMBER ;
89   l_msg_data                VARCHAR2(2000) ;
90   --
91   l_worksheets_tab          PSB_WS_Ops_Pvt.Worksheet_Tbl_Type ;
92   --
93 BEGIN
94   --
95   SAVEPOINT Enforce_WS_Concurrency_Pvt ;
96   --
97   IF NOT FND_API.Compatible_API_Call ( l_api_version,
98 				       p_api_version,
99 				       l_api_name,
100 				       G_PKG_NAME )
101   THEN
102     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
103   END IF;
104   --
105 
106   IF FND_API.To_Boolean ( p_init_msg_list ) THEN
107     FND_MSG_PUB.initialize ;
108   END IF;
109   --
110   p_return_status := FND_API.G_RET_STS_SUCCESS ;
111   --
112 
113   --
114   -- First lock the current worksheet p_worksheet_id
115   --
116   PSB_Concurrency_Control_Pub.Enforce_Concurrency_Control
117   (
118      p_api_version                 => 1.0 ,
119      p_init_msg_list               => FND_API.G_FALSE ,
120      p_validation_level            => FND_API.G_VALID_LEVEL_NONE ,
121      p_return_status               => l_return_status ,
122      p_msg_count                   => l_msg_count ,
123      p_msg_data                    => l_msg_data ,
124      --
125      p_concurrency_class           => NVL( p_maintenance_mode, 'MAINTENANCE'),
126      p_concurrency_entity_name     => 'WORKSHEET',
127      p_concurrency_entity_id       => p_worksheet_id
128   );
129   --
130   IF l_return_status = FND_API.G_RET_STS_ERROR THEN
131     RAISE FND_API.G_EXC_ERROR ;
132   ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
133     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
134   END IF;
135 
136   --
137   -- Find parent or child worksheets depending on p_parent_or_child_mode
138   -- parameter.
139   --
140   IF p_parent_or_child_mode = 'PARENT' THEN
141     --
142     PSB_WS_Ops_Pvt.Find_Parent_Worksheets
143     (
144        p_api_version             =>   1.0 ,
145        p_init_msg_list           =>   FND_API.G_FALSE,
146        p_commit                  =>   FND_API.G_FALSE,
147        p_validation_level        =>   FND_API.G_VALID_LEVEL_FULL,
148        p_return_status           =>   l_return_status,
149        p_msg_count               =>   l_msg_count,
150        p_msg_data                =>   l_msg_data,
151        --
152        p_worksheet_id            =>   p_worksheet_id,
153        p_worksheet_tbl           =>   l_worksheets_tab
154     );
155     --
156   ELSIF p_parent_or_child_mode = 'CHILD' THEN
157     --
158     PSB_WS_Ops_Pvt.Find_Child_Worksheets
159     (
160        p_api_version        =>   1.0 ,
161        p_init_msg_list      =>   FND_API.G_FALSE,
162        p_commit             =>   FND_API.G_FALSE,
163        p_validation_level   =>   FND_API.G_VALID_LEVEL_FULL,
164        p_return_status      =>   l_return_status,
165        p_msg_count          =>   l_msg_count,
166        p_msg_data           =>   l_msg_data,
167        --
168        p_worksheet_id       =>   p_worksheet_id,
169        p_worksheet_tbl      =>   l_worksheets_tab
170     );
171     --
172   END IF ;
173 
174   IF l_return_status = FND_API.G_RET_STS_ERROR THEN
175     RAISE FND_API.G_EXC_ERROR ;
176   ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
177     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
178   END IF;
179 
180   FOR i IN 1..l_worksheets_tab.COUNT
181   LOOP
182     --
183     -- Lock parent or child worksheets retrieved in the previous step.
184     --
185     PSB_Concurrency_Control_Pub.Enforce_Concurrency_Control
186     (
187        p_api_version              => 1.0 ,
188        p_init_msg_list            => FND_API.G_FALSE ,
189        p_validation_level         => FND_API.G_VALID_LEVEL_NONE ,
190        p_return_status            => l_return_status ,
191        p_msg_count                => l_msg_count ,
192        p_msg_data                 => l_msg_data ,
193        --
194        p_concurrency_class        => NVL( p_maintenance_mode, 'MAINTENANCE'),
195        p_concurrency_entity_name  => 'WORKSHEET',
196        p_concurrency_entity_id    => l_worksheets_tab(i)
197     );
198     --
199     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
200       RAISE FND_API.G_EXC_ERROR ;
201     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
202       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
203     END IF;
204 
205     --
206   END LOOP ;
207 
208   --
209   IF FND_API.To_Boolean ( p_commit ) THEN
210     COMMIT WORK;
211   END IF;
212   --
213   FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
214 			      p_data  => p_msg_data );
215   --
216 EXCEPTION
217   --
218   WHEN FND_API.G_EXC_ERROR THEN
219     --
220     -- For Bug 4337768: Commenting out Rollack.
221     -- ROLLBACK TO Enforce_WS_Concurrency_Pvt ;
222     p_return_status := FND_API.G_RET_STS_ERROR;
223     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
224 				p_data  => p_msg_data );
225   --
226   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
227     --
228     ROLLBACK TO Enforce_WS_Concurrency_Pvt ;
229     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
230     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
231 				p_data  => p_msg_data );
232   --
233   WHEN OTHERS THEN
234     --
235     ROLLBACK TO Enforce_WS_Concurrency_Pvt ;
236     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
237     --
238     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
239       FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME,
240 				l_api_name);
241     END if;
242     --
243     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
244 				p_data  => p_msg_data );
245      --
246 END Enforce_WS_Concurrency ;
247 /*---------------------------------------------------------------------------*/
248 
249 
250 /*===========================================================================+
251  |                   PROCEDURE Check_WS_Ops_Concurrency                      |
252  +===========================================================================*/
253 --
254 -- The API checks for the operation type to invoke appropriate concurrency
255 -- control routines.
256 --
257 PROCEDURE Check_WS_Ops_Concurrency
258 (
259   p_api_version               IN       NUMBER   ,
260   p_init_msg_list             IN       VARCHAR2 := FND_API.G_FALSE ,
261   p_commit                    IN       VARCHAR2 := FND_API.G_FALSE ,
262   p_validation_level          IN       NUMBER   := FND_API.G_VALID_LEVEL_FULL ,
263   p_return_status             OUT  NOCOPY      VARCHAR2 ,
264   p_msg_count                 OUT  NOCOPY      NUMBER   ,
265   p_msg_data                  OUT  NOCOPY      VARCHAR2 ,
266   --
267   p_worksheet_id              IN       psb_worksheets.worksheet_id%TYPE ,
268   p_operation_type            IN       VARCHAR2
269 )
270 IS
271   --
272   l_api_name                CONSTANT VARCHAR2(30) := 'Check_WS_Ops_Concurrency';
273   l_api_version             CONSTANT NUMBER       :=  1.0 ;
274   --
275   l_return_status           VARCHAR2(1) ;
276   l_msg_count               NUMBER ;
277   l_msg_data                VARCHAR2(2000) ;
278   --
279 BEGIN
280   --
281   SAVEPOINT Check_WS_Ops_Concurrency_Pvt ;
282   --
283   IF NOT FND_API.Compatible_API_Call ( l_api_version,
284 				       p_api_version,
285 				       l_api_name,
286 				       G_PKG_NAME )
287   THEN
288     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
289   END IF;
290   --
291 
292   IF FND_API.To_Boolean ( p_init_msg_list ) THEN
293     FND_MSG_PUB.initialize ;
294   END IF;
295   --
296   p_return_status := FND_API.G_RET_STS_SUCCESS ;
297   --
298 
299   IF p_operation_type IN ('VALIDATE', 'COPY') THEN
300     --
301     -- No locks are required for 'VALIDATE' and 'COPY' operations as
302     -- these perform read-only operations on the worksheets.
303     --
304     NULL ;
305     --
306   ELSIF p_operation_type IN ('FREEZE', 'SUBMIT' ) THEN
307     --
308     -- Lock in 'CHILD' mode as the child worksheets also need to be frozen.
309     --
310     PSB_WS_Ops_Pvt.Enforce_WS_Concurrency
311     (
312        p_api_version              =>  1.0,
313        p_init_msg_list            =>  FND_API.G_FALSE ,
314        p_commit                   =>  FND_API.G_FALSE ,
315        p_validation_level         =>  FND_API.G_VALID_LEVEL_FULL,
316        p_return_status            =>  l_return_status,
317        p_msg_count                =>  l_msg_count,
318        p_msg_data                 =>  l_msg_data,
319        --
320        p_worksheet_id             =>  p_worksheet_id ,
321        p_parent_or_child_mode     =>  'CHILD' ,
322        p_maintenance_mode         =>  'MAINTENANCE'
323     );
324     --
325     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
326       RAISE FND_API.G_EXC_ERROR ;
327     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
328       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
329     END IF;
330     --
331   ELSIF p_operation_type IN ('MOVE') THEN
332     --
333     -- Lock in 'CHILD' mode as the child worksheets are frozen.
334     --
335     PSB_WS_Ops_Pvt.Enforce_WS_Concurrency
336     (
337        p_api_version              =>  1.0,
338        p_init_msg_list            =>  FND_API.G_FALSE ,
339        p_commit                   =>  FND_API.G_FALSE ,
340        p_validation_level         =>  FND_API.G_VALID_LEVEL_FULL,
341        p_return_status            =>  l_return_status,
342        p_msg_count                =>  l_msg_count,
343        p_msg_data                 =>  l_msg_data,
344        --
345        p_worksheet_id             =>  p_worksheet_id ,
346        p_parent_or_child_mode     =>  'CHILD' ,
347        p_maintenance_mode         =>  'MAINTENANCE'
348     );
349     --
350     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
351       RAISE FND_API.G_EXC_ERROR ;
352     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
353       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
354     END IF;
355     --
356 
357     --
358     -- Also lock in 'PARENT' mode to update their view_flag as per the
359     -- service package selection.
360     --
361     PSB_WS_Ops_Pvt.Enforce_WS_Concurrency
362     (
363        p_api_version              =>  1.0,
364        p_init_msg_list            =>  FND_API.G_FALSE ,
365        p_commit                   =>  FND_API.G_FALSE ,
366        p_validation_level         =>  FND_API.G_VALID_LEVEL_FULL,
367        p_return_status            =>  l_return_status,
368        p_msg_count                =>  l_msg_count,
369        p_msg_data                 =>  l_msg_data,
370        --
371        p_worksheet_id             =>  p_worksheet_id ,
372        p_parent_or_child_mode     =>  'PARENT' ,
373        p_maintenance_mode         =>  'MAINTENANCE'
374     );
375     --
376     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
377       RAISE FND_API.G_EXC_ERROR ;
378     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
379       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
380     END IF;
381     --
382   ELSIF p_operation_type IN ('MERGE' ) THEN
383     --
384     -- Lock only the target worksheet in 'PARENT' mode as the changes in
385     -- the source worksheet are to be applied to the target and parent
386     -- worksheets. The targer worksheet is passed as p_worksheet_id.
387     --
388     PSB_WS_Ops_Pvt.Enforce_WS_Concurrency
389     (
390        p_api_version              =>  1.0,
391        p_init_msg_list            =>  FND_API.G_FALSE,
392        p_commit                   =>  FND_API.G_FALSE,
393        p_validation_level         =>  FND_API.G_VALID_LEVEL_FULL,
394        p_return_status            =>  l_return_status,
395        p_msg_count                =>  l_msg_count,
396        p_msg_data                 =>  l_msg_data,
397        --
398        p_worksheet_id             =>  p_worksheet_id ,
399        p_parent_or_child_mode     =>  'PARENT'   ,
400        p_maintenance_mode         =>  'MAINTENANCE'
401     );
402     --
403     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
404       RAISE FND_API.G_EXC_ERROR ;
405     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
406       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
407     END IF;
408     --
409   ELSIF p_operation_type IN ('UNFREEZE' ) THEN
410     --
411     -- Lock only the current worksheet.
412     --
413     PSB_Concurrency_Control_Pub.Enforce_Concurrency_Control
414     (
415        p_api_version              =>  1.0,
416        p_init_msg_list            =>  FND_API.G_FALSE,
417        p_validation_level         =>  FND_API.G_VALID_LEVEL_FULL,
418        p_return_status            =>  l_return_status,
419        p_msg_count                =>  l_msg_count,
420        p_msg_data                 =>  l_msg_data,
421        --
422        p_concurrency_class        =>  'MAINTENANCE' ,
423        p_concurrency_entity_name  =>  'WORKSHEET'   ,
424        p_concurrency_entity_id    =>  p_worksheet_id
425     );
426     --
427     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
428       RAISE FND_API.G_EXC_ERROR ;
429     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
430       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
431     END IF;
432     --
433   ELSE
434     --
435     Fnd_Message.Set_Name ('PSB', 'PSB_INVALID_ARGUMENT') ;
436     Fnd_Message.Set_Token('ROUTINE', l_api_name ) ;
437     FND_MSG_PUB.Add;
438     RAISE FND_API.G_EXC_ERROR ;
439     --
440   END IF ;
441 
442   --
443   IF FND_API.To_Boolean ( p_commit ) THEN
444     COMMIT WORK;
445   END IF;
446   --
447   FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
448 			      p_data  => p_msg_data );
449   --
450 EXCEPTION
451   --
452   WHEN FND_API.G_EXC_ERROR THEN
453     --
454     -- For Bug 4337768: Commenting out Rollback.
455     -- ROLLBACK TO Check_WS_Ops_Concurrency_Pvt ;
456     p_return_status := FND_API.G_RET_STS_ERROR;
457     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
458 				p_data  => p_msg_data );
459   --
460   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
461     --
462     ROLLBACK TO Check_WS_Ops_Concurrency_Pvt ;
463     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
464     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
465 				p_data  => p_msg_data );
466   --
467   WHEN OTHERS THEN
468     --
469     ROLLBACK TO Check_WS_Ops_Concurrency_Pvt ;
470     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
471     --
472     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
473       FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME,
474 				l_api_name );
475     END if;
476     --
477     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
478 				p_data  => p_msg_data );
479      --
480 END Check_WS_Ops_Concurrency ;
481 /*---------------------------------------------------------------------------*/
482 
483 
484 /*===========================================================================+
485  |          PROCEDURE Create_Worksheet ( From account/position set table )   |
486  +===========================================================================*/
487 --
488 -- The overloaded API creates a worksheet from a PL/SQL table of sets.
489 -- It also considers service packages. By default all the service packages
490 -- are considered. When a new worksheet needs to be created during submit
491 -- operation, the user can pick which service packages to be considered.
492 -- ( Note that we do not need to consider effective dates as the matching is
493 --   to be performed against the given account/position set.)
494 --
495 PROCEDURE Create_Worksheet
496 (
497   p_api_version               IN       NUMBER   ,
498   p_init_msg_list             IN       VARCHAR2 := FND_API.G_FALSE ,
499   p_commit                    IN       VARCHAR2 := FND_API.G_FALSE ,
500   p_validation_level          IN       NUMBER   := FND_API.G_VALID_LEVEL_FULL ,
501   p_return_status             OUT  NOCOPY      VARCHAR2 ,
502   p_msg_count                 OUT  NOCOPY      NUMBER   ,
503   p_msg_data                  OUT  NOCOPY      VARCHAR2 ,
504   --
505   p_worksheet_id              IN       psb_worksheets.worksheet_id%TYPE    ,
506   p_budget_group_id           IN       psb_worksheets.budget_group_id%TYPE ,
507   p_account_position_set_tbl  IN       account_position_set_tbl_type ,
508   p_service_package_operation_id
509 			      IN       NUMBER := FND_API.G_MISS_NUM ,
510   p_worksheet_id_OUT          OUT  NOCOPY      psb_worksheets.worksheet_id%TYPE
511 )
512 IS
513   --
514   l_api_name                CONSTANT VARCHAR2(30) := 'Create_Worksheet(Set)' ;
515   l_api_version             CONSTANT NUMBER       :=  1.0 ;
516   --
517   l_return_status           VARCHAR2(1) ;
518   l_msg_count               NUMBER ;
519   l_msg_data                VARCHAR2(2000) ;
520   --
521   l_worksheet_name          psb_worksheets.name%TYPE ;
522   l_budget_by_position      psb_worksheets.budget_by_position%TYPE ;
523   l_worksheet_description   psb_worksheets.description%TYPE ;
524   l_main_worksheet_name     psb_worksheets.name%TYPE ;
525   l_main_budget_group_id    psb_worksheets.budget_group_id%TYPE ;
526   l_main_budget_group_name  psb_budget_groups.name%TYPE ;
527   l_main_budget_calendar_id psb_worksheets.budget_calendar_id%TYPE ;
528   l_new_worksheet_id        psb_worksheets.worksheet_id%TYPE ;
529   l_global_worksheet_id     psb_worksheets.worksheet_id%TYPE ;
530   l_service_package_count   NUMBER ;
531   --
532   l_tmp_char                VARCHAR2(1) ;
533   l_lines_added             NUMBER := 0 ;
534   --
535   CURSOR l_worksheets_csr IS
536 	 SELECT *
537 	 FROM psb_worksheets
538 	 WHERE worksheet_id = p_worksheet_id ;
539   --
540   l_worksheets_rec   l_worksheets_csr%ROWTYPE ;
541   --
542   CURSOR l_budget_accounts_csr
543 	 ( c_current_account_set_id
544 		    psb_account_position_sets.account_position_set_id%TYPE,
545 	   c_code_combination_id
546 		    psb_budget_accounts.code_combination_id%TYPE
547 	  )
548 	 IS
549 	 SELECT '1'
550 	 FROM   psb_budget_accounts
551 	 WHERE  account_position_set_id = c_current_account_set_id
552 	 AND    code_combination_id     = c_code_combination_id     ;
553   --
554 
555   CURSOR l_budget_positions_csr
556 	 ( c_current_account_set_id
557 		    psb_account_position_sets.account_position_set_id%TYPE,
558 	   c_position_id
559 		    psb_budget_positions.position_id%TYPE
560 	  )
561 	 IS
562 	 SELECT '1'
563 	 FROM   psb_budget_positions
564 	 WHERE  account_position_set_id = c_current_account_set_id
565 	 AND    position_id             = c_position_id     ;
566   --
567 BEGIN
568   --
569   SAVEPOINT Create_Worksheet_Pvt ;
570   --
571   IF NOT FND_API.Compatible_API_Call ( l_api_version,
572 				       p_api_version,
573 				       l_api_name,
574 				       G_PKG_NAME )
575   THEN
576     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
577   END IF;
578   --
579 
580   IF FND_API.To_Boolean ( p_init_msg_list ) THEN
581     FND_MSG_PUB.initialize ;
582   END IF;
583   --
584   p_return_status := FND_API.G_RET_STS_SUCCESS ;
585   p_worksheet_id_OUT := 0 ;
586   --
587 
588   --
589   -- Validating p_budget_group_id.
590   --
591   SELECT '1' INTO l_tmp_char
592   FROM   psb_budget_groups
593   WHERE  budget_group_id = p_budget_group_id ;
594 
595   --
596   -- Validating p_account_position_set_tbl table.
597   --
598   IF p_account_position_set_tbl.COUNT = 0 THEN
599     --
600     Fnd_Message.Set_Name ('PSB',     'PSB_INVALID_ARGUMENT') ;
601     Fnd_Message.Set_Token('ROUTINE', l_api_name ) ;
602     FND_MSG_PUB.Add;
603     RAISE FND_API.G_EXC_ERROR ;
604   END IF ;
605 
606 
607   --
608   -- Finding the worksheet information.
609   --
610   OPEN  l_worksheets_csr ;
611 
612   FETCH l_worksheets_csr INTO l_worksheets_rec ;
613 
614   IF ( l_worksheets_csr%NOTFOUND ) THEN
615     --
616     Fnd_Message.Set_Name ('PSB',     'PSB_INVALID_WORKSHEET_ID') ;
617     Fnd_Message.Set_Token('ROUTINE', l_api_name ) ;
618     FND_MSG_PUB.Add;
619     RAISE FND_API.G_EXC_ERROR ;
620   END IF ;
621 
622   -- l_budget_by_position defines whether worksheet contains positions or not.
623   l_budget_by_position      := NVL(l_worksheets_rec.budget_by_position, 'N') ;
624 
625   l_main_worksheet_name     := l_worksheets_rec.name ;
626   l_main_budget_group_id    := l_worksheets_rec.budget_group_id ;
627   l_main_budget_calendar_id := l_worksheets_rec.budget_calendar_id ;
628 
629   --
630   -- Finding the main budget group name.
631   --
632   SELECT name INTO l_main_budget_group_name
633   FROM   psb_budget_groups
634   WHERE  budget_group_id = l_main_budget_group_id ;
635 
636   --
637   -- Get translated messages for the new worksheet.
638   --
639   Fnd_Message.Set_Name ( 'PSB', 'PSB_WORKSHEET_CREATION_INFO') ;
640   Fnd_Message.Set_Token( 'WORKSHEET_ID',      p_worksheet_id ) ;
641   Fnd_Message.Set_Token( 'BUDGET_GROUP_NAME', l_main_budget_group_name ) ;
642   l_worksheet_description := Fnd_Message.Get ;
643 
644   --
645   -- Find global worksheet related information, use by Create_Worksheet API.
646   --
647   IF NVL(l_worksheets_rec.global_worksheet_flag, 'N') = 'Y' THEN
648     l_global_worksheet_id := p_worksheet_id ;
649   ELSE
650     l_global_worksheet_id := l_worksheets_rec.global_worksheet_id ;
651   END IF ;
652 
653   --
654   -- Create the new worksheet in psb_worksheets table.
655   --
656   PSB_Worksheet_Pvt.Create_Worksheet
657   (
658      p_api_version               => 1.0 ,
659      p_init_msg_list             => FND_API.G_FALSE,
660      p_commit                    => FND_API.G_FALSE,
661      p_validation_level          => FND_API.G_VALID_LEVEL_NONE,
662      p_return_status             => l_return_status,
663      p_msg_count                 => l_msg_count,
664      p_msg_data                  => l_msg_data ,
665      --
666      p_budget_group_id           => p_budget_group_id,
667      p_budget_calendar_id        => l_worksheets_rec.budget_calendar_id,
668      p_worksheet_type            => 'R',
669      p_name                      => NULL ,
670      p_description               => l_worksheet_description ,
671      p_ws_creation_complete      => l_worksheets_rec.ws_creation_complete ,
672      p_stage_set_id              => l_worksheets_rec.stage_set_id ,
673      p_current_stage_seq         => l_worksheets_rec.current_stage_seq ,
674      p_global_worksheet_id       => l_global_worksheet_id ,
675      p_global_worksheet_flag     => 'N' ,
676      p_global_worksheet_option   => l_worksheets_rec.global_worksheet_option,
677      p_local_copy_flag           => l_worksheets_rec.local_copy_flag,
678      p_copy_of_worksheet_id      => l_worksheets_rec.copy_of_worksheet_id,
679      p_freeze_flag               => l_worksheets_rec.freeze_flag,
680      p_budget_by_position        => l_worksheets_rec.budget_by_position,
681      p_use_revised_element_rates => l_worksheets_rec.use_revised_element_rates,
682      p_num_proposed_years        => l_worksheets_rec.num_proposed_years,
683      p_num_years_to_allocate     => l_worksheets_rec.num_years_to_allocate,
684      p_rounding_factor           => l_worksheets_rec.rounding_factor,
685      p_gl_cutoff_period          => l_worksheets_rec.gl_cutoff_period,
686      p_include_stat_balance      => l_worksheets_rec.include_stat_balance,
687      p_include_trans_balance     => l_worksheets_rec.include_translated_balance,
688      p_include_adj_period        => l_worksheets_rec.include_adjustment_periods,
689      p_data_extract_id           => l_worksheets_rec.data_extract_id,
690      p_parameter_set_id          => NULL,
691      p_constraint_set_id         => NULL,
692      p_allocrule_set_id          => NULL,
693      p_date_submitted            => l_worksheets_rec.date_submitted,
694      p_submitted_by              => l_worksheets_rec.submitted_by,
695      p_attribute1                => l_worksheets_rec.attribute1,
696      p_attribute2                => l_worksheets_rec.attribute2,
697      p_attribute3                => l_worksheets_rec.attribute3,
698      p_attribute4                => l_worksheets_rec.attribute4,
699      p_attribute5                => l_worksheets_rec.attribute5,
700      p_attribute6                => l_worksheets_rec.attribute6,
701      p_attribute7                => l_worksheets_rec.attribute7,
702      p_attribute8                => l_worksheets_rec.attribute8,
703      p_attribute9                => l_worksheets_rec.attribute9,
704      p_attribute10               => l_worksheets_rec.attribute10,
705      p_context                   => l_worksheets_rec.context,
706      /* Included federal_ws_flag for Bug 3157960 */
707      p_federal_ws_flag           => l_worksheets_rec.federal_ws_flag,
708      p_worksheet_id              => l_new_worksheet_id
709   );
710   --
711   CLOSE l_worksheets_csr ;
712   IF l_return_status = FND_API.G_RET_STS_ERROR THEN
713     RAISE FND_API.G_EXC_ERROR ;
714   ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
715     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
716   END IF;
717 
718   --
719   -- Get budget calendar related info to find all the budget groups down in the
720   -- current hierarchy to get all the CCIDs for the current budget group.
721   --
722   IF NVL(PSB_WS_Acct1.g_budget_calendar_id, -99) <> l_main_budget_calendar_id
723   THEN
724     --
725     PSB_WS_Acct1.Cache_Budget_Calendar
726     (
727        p_return_status         =>  l_return_status ,
728        p_budget_calendar_id    =>  l_main_budget_calendar_id
729     );
730     --
731     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
732       RAISE FND_API.G_EXC_ERROR ;
733     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
734       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
735     END IF;
736     --
737   END IF ;
738 
739   --
740   -- Check whether service packages were selected for the worksheet.
741   -- If yes, then we need to consider only those account lines which are
742   -- related to the service package selection.
743   --
744   SELECT count(*) INTO l_service_package_count
745   FROM   psb_ws_submit_service_packages
746   WHERE  worksheet_id = p_worksheet_id
747   AND    operation_id = NVL( p_service_package_operation_id ,
748 			     FND_API.G_MISS_NUM ) ;
749   FOR l_lines_rec IN
750   (
751      SELECT lines.* ,
752 	    accts.code_combination_id ,
753 	    accts.budget_group_id
754      FROM   psb_ws_lines         lines ,
755 	    psb_ws_account_lines accts
756      WHERE  lines.worksheet_id    = p_worksheet_id
757      AND    lines.account_line_id = accts.account_line_id
758      AND    ( l_service_package_count = 0
759 	      OR
760 	      accts.service_package_id IN
761 		      ( SELECT service_package_id
762 			FROM   psb_ws_submit_service_packages
763 			WHERE  worksheet_id = p_worksheet_id
764 			AND    operation_id = p_service_package_operation_id )
765 	    )
766   )
767   LOOP
768     --
769     -- Search l_lines_rec.code_combination_id in the
770     -- p_account_position_set_tbl table.
771     --
772     FOR i IN 1..p_account_position_set_tbl.COUNT
773     LOOP
774 
775       -- Process only account sets first.  Using GOTO as PL/SQL lacks
776       -- CONTINUE statement.
777       IF p_account_position_set_tbl(i).account_or_position_type = 'P' THEN
778 	GOTO end_account_loop ;
779       END IF;
780 
781       --
782       OPEN l_budget_accounts_csr
783 	   (  p_account_position_set_tbl(i).account_position_set_id ,
784 	      l_lines_rec.code_combination_id ) ;
785 
786       FETCH l_budget_accounts_csr INTO l_tmp_char;
787       --
788       IF ( l_budget_accounts_csr%FOUND ) THEN
789 
790 	-- At least one line should get created for the worksheet.
791 	l_lines_added := l_lines_added + 1 ;
792 
793 	--
794 	-- Put the CCID in the psb_ws_lines table for the new worksheet.
795 	--
796 	Insert_WS_Lines_Pvt
797 	(
798 	  p_worksheet_id       =>  l_new_worksheet_id,
799 	  p_account_line_id    =>  l_lines_rec.account_line_id ,
800 	  p_freeze_flag        =>  l_lines_rec.freeze_flag ,
801 	  p_view_line_flag     =>  l_lines_rec.view_line_flag ,
802 	  p_last_update_date   =>  g_current_date,
803 	  p_last_updated_by    =>  g_current_user_id,
804 	  p_last_update_login  =>  g_current_login_id,
805 	  p_created_by         =>  g_current_user_id,
806 	  p_creation_date      =>  g_current_date,
807 	  p_return_status      =>  l_return_status
808 	) ;
809 	--
810 	CLOSE l_budget_accounts_csr;
811 	IF l_return_status = FND_API.G_RET_STS_ERROR THEN
812 	  RAISE FND_API.G_EXC_ERROR ;
813 	ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
814 	  RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
815 	END IF;
816 	--
817 	EXIT;
818 	--
819       ELSE
820 	--
821 	CLOSE l_budget_accounts_csr ;
822       END IF;
823       --
824       <<end_account_loop>>
825       NULL;
826     END LOOP;
827     --
828   END LOOP;
829 
830   --
831   -- Maintain psb_ws_lines_positions table if worksheet contains positions.
832   --
833   IF l_budget_by_position = 'Y' THEN
834 
835     --
836     -- This loop gets all the position for the given worksheet. To maintain
837     -- psb_ws_lines_positions matrix table for the new worksheet, we will
838     -- consider all the positions falling in the given table of position sets.
839     --
840     FOR l_lines_pos_rec IN
841     (
842        SELECT pos_lines.*     ,
843 	      pos.position_id
844        FROM   psb_ws_lines_positions   pos_lines ,
845 	      psb_ws_position_lines    pos
846        WHERE  pos_lines.worksheet_id = p_worksheet_id
847        AND    pos.position_line_id   = pos_lines.position_line_id
848        AND    (
849 		l_service_package_count = 0
850 		OR
851 		pos_lines.position_line_id IN
852 		(
853 		  SELECT accts.position_line_id
854 		  FROM   psb_ws_account_lines  accts
855 		  WHERE  accts.position_line_id = pos_lines.position_line_id
856 		  AND    accts.service_package_id IN
857 			 (
858 			   SELECT sp.service_package_id
859 			   FROM   psb_ws_submit_service_packages  sp
860 			   WHERE  worksheet_id = p_worksheet_id
861 			   AND    operation_id = p_service_package_operation_id
862 			 )
863 		)
864 	      )
865     )
866     LOOP
867       --
868       -- Search l_lines_pos_rec.position_id in the p_account_position_set_tbl
869       -- table.
870       --
871       FOR i IN 1..p_account_position_set_tbl.COUNT
872       LOOP
873 
874 	-- Process only position sets now.  Using GOTO as PL/SQL lacks
875 	-- CONTINUE statement.
876 	IF p_account_position_set_tbl(i).account_or_position_type = 'A' THEN
877 	  GOTO end_position_loop ;
878 	END IF;
879 
880 	OPEN l_budget_positions_csr
881 	     (  p_account_position_set_tbl(i).account_position_set_id ,
882 		l_lines_pos_rec.position_id ) ;
883 	--
884 	FETCH l_budget_positions_csr INTO l_tmp_char;
885 	--
886 	IF ( l_budget_positions_csr%FOUND ) THEN
887 
888 	  -- At least one line should get created for the worksheet.
889 	  l_lines_added := l_lines_added + 1 ;
890 
891 	  --
892 	  -- Put the position_line in the psb_ws_lines_position table.
893 	  --
894 	  PSB_WS_Pos_Pvt.Create_Position_Matrix
895 	  (
896 	    p_api_version        =>  1.0 ,
897 	    p_init_msg_list      =>  FND_API.G_FALSE ,
898 	    p_validation_level   =>  FND_API.G_VALID_LEVEL_NONE ,
899 	    p_return_status      =>  l_return_status ,
900 	    p_msg_count          =>  l_msg_count ,
901 	    p_msg_data           =>  l_msg_data ,
902 	    --
903 	    p_worksheet_id       =>  l_new_worksheet_id ,
904 	    p_position_line_id   =>  l_lines_pos_rec.position_line_id ,
905 	    p_freeze_flag        =>  l_lines_pos_rec.freeze_flag ,
906 	    p_view_line_flag     =>  l_lines_pos_rec.view_line_flag
907 	  ) ;
908 	  --
909 	  CLOSE l_budget_positions_csr ;
910 	  IF l_return_status = FND_API.G_RET_STS_ERROR THEN
911 	    RAISE FND_API.G_EXC_ERROR ;
912 	  ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
913 	    RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
914 	  END IF;
915 	  --
916 
917 	  --
918 	  -- Maintain psb_ws_lines matrix for the account lines related to
919 	  -- current position_line_id (l_lines_pos_rec.position_line_id).
920 	  --
921 	  FOR l_lines_rec IN
922 	  (
923 	    SELECT lines.*
924 	    FROM   psb_ws_lines          lines,
925 		   psb_ws_account_lines  accts
926 	    WHERE  accts.position_line_id = l_lines_pos_rec.position_line_id
927 	    AND    lines.worksheet_id     = p_worksheet_id
928 	    AND    lines.account_line_id  = accts.account_line_id
929 	  )
930 	  LOOP
931 	    --
932 	    Insert_WS_Lines_Pvt
933 	    (
934 	      p_worksheet_id       =>  l_new_worksheet_id,
935 	      p_account_line_id    =>  l_lines_rec.account_line_id ,
936 	      p_freeze_flag        =>  l_lines_rec.freeze_flag ,
937 	      p_view_line_flag     =>  l_lines_rec.view_line_flag ,
938 	      p_last_update_date   =>  g_current_date,
939 	      p_last_updated_by    =>  g_current_user_id,
940 	      p_last_update_login  =>  g_current_login_id,
941 	      p_created_by         =>  g_current_user_id,
942 	      p_creation_date      =>  g_current_date,
943 	      p_return_status      =>  l_return_status
944 	    ) ;
945 	    --
946 	    IF l_return_status = FND_API.G_RET_STS_ERROR THEN
947 	      RAISE FND_API.G_EXC_ERROR ;
948 	    ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
949 	      RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
950 	    END IF;
951 	    --
952 	  END LOOP ;
953 
954 	  EXIT;
955 	  --
956 	ELSE
957 	  CLOSE l_budget_positions_csr ;
958 	END IF ;
959 	--
960 	<<end_position_loop>>
961 	NULL;
962       END LOOP ;  --/ To process position_set table for the current position
963       --
964     END LOOP ;    -- /To process all the positions in the parent worksheet
965 
966   END IF ;
967 
968   --
969   -- Check whether at least one line got created or not.
970   --
971   IF l_lines_added = 0 THEN
972     p_worksheet_id_OUT := 0 ;
973     ROLLBACK TO Create_Worksheet_Pvt ;
974   ELSE
975     p_worksheet_id_OUT := l_new_worksheet_id ;
976   END IF ;
977   --
978   IF FND_API.To_Boolean ( p_commit ) THEN
979     COMMIT WORK;
980   END IF;
981   --
982   FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
983 			      p_data  => p_msg_data );
984   --
985 EXCEPTION
986   --
987   WHEN FND_API.G_EXC_ERROR THEN
988     --
989     ROLLBACK TO Create_Worksheet_Pvt ;
990     p_return_status := FND_API.G_RET_STS_ERROR;
991     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
992 				p_data  => p_msg_data );
993   --
994   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
995     --
996     ROLLBACK TO Create_Worksheet_Pvt ;
997     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
998     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
999 				p_data  => p_msg_data );
1000   --
1001   WHEN OTHERS THEN
1002     --
1003     IF ( l_worksheets_csr%ISOPEN ) THEN
1004       CLOSE l_worksheets_csr ;
1005     END IF ;
1006     --
1007     IF ( l_budget_accounts_csr%ISOPEN ) THEN
1008       CLOSE l_budget_accounts_csr ;
1009     END IF ;
1010     --
1011     ROLLBACK TO Create_Worksheet_Pvt ;
1012     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1013     --
1014     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
1015       FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME,
1016 				l_api_name);
1017     END if;
1018     --
1019     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
1020 				p_data  => p_msg_data );
1021      --
1022 END Create_Worksheet;
1023 /*---------------------------------------------------------------------------*/
1024 
1025 
1026 /*===========================================================================+
1027  |                PROCEDURE Create_Worksheet ( From a worksheet )            |
1028  +===========================================================================*/
1029 --
1030 -- This overloaded API creates a new worksheet for a given budget group.
1031 --
1032 PROCEDURE Create_Worksheet
1033 (
1034   p_api_version               IN       NUMBER   ,
1035   p_init_msg_list             IN       VARCHAR2 := FND_API.G_FALSE ,
1036   p_commit                    IN       VARCHAR2 := FND_API.G_FALSE ,
1037   p_validation_level          IN       NUMBER   := FND_API.G_VALID_LEVEL_FULL ,
1038   p_return_status             OUT  NOCOPY      VARCHAR2 ,
1039   p_msg_count                 OUT  NOCOPY      NUMBER   ,
1040   p_msg_data                  OUT  NOCOPY      VARCHAR2 ,
1041   --
1042   p_worksheet_id              IN       psb_worksheets.worksheet_id%TYPE    ,
1043   p_budget_group_id           IN       psb_worksheets.budget_group_id%TYPE ,
1044   p_worksheet_id_OUT          OUT  NOCOPY      psb_worksheets.worksheet_id%TYPE
1045 )
1046 
1047 IS
1048   --
1049   l_api_name                CONSTANT VARCHAR2(30)   := 'Create_Worksheet' ;
1050   l_api_version             CONSTANT NUMBER         :=  1.0 ;
1051   --
1052   l_return_status           VARCHAR2(1) ;
1053   l_msg_count               NUMBER ;
1054   l_msg_data                VARCHAR2(2000) ;
1055   --
1056   l_worksheet_name          psb_worksheets.name%TYPE ;
1057   l_budget_by_position      psb_worksheets.budget_by_position%TYPE ;
1058   l_worksheet_description   psb_worksheets.description%TYPE ;
1059   l_main_worksheet_name     psb_worksheets.name%TYPE ;
1060   l_main_budget_group_id    psb_worksheets.budget_group_id%TYPE ;
1061   l_main_budget_group_name  psb_budget_groups.name%TYPE ;
1062   l_main_budget_calendar_id psb_worksheets.budget_calendar_id%TYPE ;
1063   l_new_worksheet_id        psb_worksheets.worksheet_id%TYPE ;
1064   l_global_worksheet_id     psb_worksheets.worksheet_id%TYPE ;
1065   --
1066   l_tmp_char                VARCHAR2(1) ;
1067   --
1068   CURSOR l_worksheets_csr IS
1069 	 SELECT *
1070 	 FROM   psb_worksheets
1071 	 WHERE  worksheet_id = p_worksheet_id ;
1072   --
1073   l_ws_row_type l_worksheets_csr%ROWTYPE ;
1074   --
1075 BEGIN
1076   --
1077   SAVEPOINT Create_Worksheet_Pvt ;
1078   --
1079   IF NOT FND_API.Compatible_API_Call ( l_api_version,
1080 				       p_api_version,
1081 				       l_api_name,
1082 				       G_PKG_NAME )
1083   THEN
1084     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
1085   END IF;
1086   --
1087 
1088   IF FND_API.To_Boolean ( p_init_msg_list ) THEN
1089     FND_MSG_PUB.initialize ;
1090   END IF;
1091   --
1092   p_return_status := FND_API.G_RET_STS_SUCCESS ;
1093   --
1094 
1095   --
1096   -- Validating p_budget_group_id.
1097   --
1098   SELECT '1' INTO l_tmp_char
1099   FROM   psb_budget_groups
1100   WHERE  budget_group_id = p_budget_group_id ;
1101 
1102   --
1103   -- Finding the worksheet information.
1104   --
1105   OPEN  l_worksheets_csr ;
1106 
1107   FETCH l_worksheets_csr INTO l_ws_row_type ;
1108 
1109   IF ( l_worksheets_csr%NOTFOUND ) THEN
1110     --
1111     Fnd_Message.Set_Name ('PSB',     'PSB_INVALID_WORKSHEET_ID') ;
1112     Fnd_Message.Set_Token('ROUTINE', l_api_name ) ;
1113     FND_MSG_PUB.Add;
1114     RAISE FND_API.G_EXC_ERROR ;
1115   END IF ;
1116 
1117   -- l_budget_by_position defines whether worksheet contains positions or not.
1118   l_budget_by_position      := NVL(l_ws_row_type.budget_by_position, 'N') ;
1119 
1120   l_main_worksheet_name     := l_ws_row_type.name ;
1121   l_main_budget_group_id    := l_ws_row_type.budget_group_id ;
1122   l_main_budget_calendar_id := l_ws_row_type.budget_calendar_id ;
1123 
1124   --
1125   -- Finding the main budget group name.
1126   --
1127   SELECT name INTO l_main_budget_group_name
1128   FROM   psb_budget_groups
1129   WHERE  budget_group_id = l_main_budget_group_id ;
1130 
1131   --
1132   -- Get translated messages for the new worksheet.
1133   --
1134   Fnd_Message.Set_Name ( 'PSB', 'PSB_WORKSHEET_CREATION_INFO') ;
1135   Fnd_Message.Set_Token( 'WORKSHEET_ID',      p_worksheet_id ) ;
1136   Fnd_Message.Set_Token( 'BUDGET_GROUP_NAME', l_main_budget_group_name ) ;
1137   l_worksheet_description := Fnd_Message.Get ;
1138 
1139   --
1140   -- Find global worksheet related information, use by Create_Worksheet API.
1141   --
1142   IF NVL(l_ws_row_type.global_worksheet_flag, 'N') = 'Y' THEN
1143     l_global_worksheet_id := p_worksheet_id ;
1144   ELSE
1145     l_global_worksheet_id := l_ws_row_type.global_worksheet_id ;
1146   END IF ;
1147 
1148   --
1149   -- Create the new worksheet in psb_worksheets table.
1150   --
1151   PSB_Worksheet_Pvt.Create_Worksheet
1152   (
1153    p_api_version                 => 1.0 ,
1154    p_init_msg_list               => FND_API.G_FALSE,
1155    p_commit                      => FND_API.G_FALSE,
1156    p_validation_level            => FND_API.G_VALID_LEVEL_NONE,
1157    p_return_status               => l_return_status,
1158    p_msg_count                   => l_msg_count,
1159    p_msg_data                    => l_msg_data ,
1160    --
1161    p_budget_group_id             => p_budget_group_id,
1162    p_budget_calendar_id          => l_ws_row_type.budget_calendar_id,
1163    p_worksheet_type              => 'O',
1164    p_name                        => NULL ,
1165    p_description                 => l_worksheet_description ,
1166    p_ws_creation_complete        => l_ws_row_type.ws_creation_complete ,
1167    p_stage_set_id                => l_ws_row_type.stage_set_id,
1168    p_current_stage_seq           => l_ws_row_type.current_stage_seq,
1169    p_global_worksheet_id         => l_global_worksheet_id ,
1170    p_global_worksheet_flag       => 'N' ,
1171    p_global_worksheet_option     => l_ws_row_type.global_worksheet_option,
1172    p_local_copy_flag             => l_ws_row_type.local_copy_flag,
1173    p_copy_of_worksheet_id        => l_ws_row_type.copy_of_worksheet_id,
1174    p_freeze_flag                 => l_ws_row_type.freeze_flag,
1175    p_budget_by_position          => l_ws_row_type.budget_by_position,
1176    p_use_revised_element_rates   => l_ws_row_type.use_revised_element_rates,
1177    p_num_proposed_years          => l_ws_row_type.num_proposed_years,
1178    p_num_years_to_allocate       => l_ws_row_type.num_years_to_allocate,
1179    p_rounding_factor             => l_ws_row_type.rounding_factor,
1180    p_gl_cutoff_period            => l_ws_row_type.gl_cutoff_period,
1181    p_include_stat_balance        => l_ws_row_type.include_stat_balance,
1182    p_include_trans_balance       => l_ws_row_type.include_translated_balance,
1183    p_include_adj_period          => l_ws_row_type.include_adjustment_periods,
1184    p_data_extract_id             => l_ws_row_type.data_extract_id,
1185    p_parameter_set_id            => NULL,
1186    p_constraint_set_id           => NULL,
1187    p_allocrule_set_id            => NULL,
1188    p_date_submitted              => l_ws_row_type.date_submitted,
1189    p_submitted_by                => l_ws_row_type.submitted_by,
1190    p_attribute1                  => l_ws_row_type.attribute1,
1191    p_attribute2                  => l_ws_row_type.attribute2,
1192    p_attribute3                  => l_ws_row_type.attribute3,
1193    p_attribute4                  => l_ws_row_type.attribute4,
1194    p_attribute5                  => l_ws_row_type.attribute5,
1195    p_attribute6                  => l_ws_row_type.attribute6,
1196    p_attribute7                  => l_ws_row_type.attribute7,
1197    p_attribute8                  => l_ws_row_type.attribute8,
1198    p_attribute9                  => l_ws_row_type.attribute9,
1199    p_attribute10                 => l_ws_row_type.attribute10,
1200    p_context                     => l_ws_row_type.context,
1201    p_worksheet_id                => l_new_worksheet_id
1202   );
1203   --
1204   CLOSE l_worksheets_csr ;
1205   --
1206   IF l_return_status = FND_API.G_RET_STS_ERROR THEN
1207     RAISE FND_API.G_EXC_ERROR ;
1208   ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1209     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
1210   END IF;
1211   --
1212 
1213   --
1214   -- Get budget calendar related info to find all the budget groups down in the
1215   -- current hierarchy to get all the CCIDs for the current budget group.
1216   --
1217   IF NVL(PSB_WS_Acct1.g_budget_calendar_id, -99) <> l_main_budget_calendar_id
1218   THEN
1219     --
1220     PSB_WS_Acct1.Cache_Budget_Calendar
1221     (
1222        p_return_status         =>  l_return_status ,
1223        p_budget_calendar_id    =>  l_main_budget_calendar_id
1224     );
1225     --
1226     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
1227       RAISE FND_API.G_EXC_ERROR ;
1228     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1229       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
1230     END IF;
1231     --
1232   END IF ;
1233 
1234   --
1235   -- This LOOP gets all the account_line_id for the new worksheet which will
1236   -- be used to maintain psb_ws_lines table.
1237   --
1238   FOR l_lines_rec IN
1239   (
1240      SELECT lines.*
1241      FROM   psb_ws_lines          lines ,
1242 	    psb_ws_account_lines  accts
1243      WHERE  lines.worksheet_id    = p_worksheet_id
1244      AND    lines.account_line_id = accts.account_line_id
1245      /*For Bug No : 2236283 Start*/
1246      /*
1247      AND    accts.budget_group_id  IN
1248 	       (  SELECT budget_group_id
1249 		    FROM psb_budget_groups
1250 		   WHERE budget_group_type = 'R'
1251 		     AND effective_start_date <= PSB_WS_Acct1.g_startdate_pp
1252 		     AND ((effective_end_date IS NULL)
1253 			   OR
1254 			  (effective_end_date >= PSB_WS_Acct1.g_enddate_cy))
1255 		  START WITH budget_group_id       = p_budget_group_id
1256 		  CONNECT BY PRIOR budget_group_id = parent_budget_group_id
1257 	       )
1258      */
1259      AND EXISTS
1260 	       (  SELECT 1
1261 		    FROM psb_budget_groups
1262 		   WHERE budget_group_type = 'R'
1263 		     AND budget_group_id = accts.budget_group_id
1264 		     AND effective_start_date <= PSB_WS_Acct1.g_startdate_pp
1265 		     AND ((effective_end_date IS NULL)
1266 			   OR
1267 			  (effective_end_date >= PSB_WS_Acct1.g_enddate_cy))
1268 		  START WITH budget_group_id       = p_budget_group_id
1269 		  CONNECT BY PRIOR budget_group_id = parent_budget_group_id
1270 	       )
1271      /*For Bug No : 2236283 End*/
1272   )
1273   LOOP
1274 
1275     --
1276     -- Put the CCID in the psb_ws_lines table for the new worksheet.
1277     --
1278     Insert_WS_Lines_Pvt
1279     (
1280       p_worksheet_id       =>  l_new_worksheet_id,
1281       p_account_line_id    =>  l_lines_rec.account_line_id ,
1282       p_freeze_flag        =>  l_lines_rec.freeze_flag ,
1283       p_view_line_flag     =>  l_lines_rec.view_line_flag ,
1284       p_last_update_date   =>  g_current_date,
1285       p_last_updated_by    =>  g_current_user_id,
1286       p_last_update_login  =>  g_current_login_id,
1287       p_created_by         =>  g_current_user_id,
1288       p_creation_date      =>  g_current_date,
1289       p_return_status      =>  l_return_status
1290     ) ;
1291     --
1292     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
1293       RAISE FND_API.G_EXC_ERROR ;
1294     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1295       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
1296     END IF;
1297     --
1298   END LOOP;
1299   --
1300 
1301   --
1302   -- Maintain psb_ws_lines_positions table if worksheet contains positions.
1303   -- ( This also means the worksheet includes position budgeting. )
1304   --
1305   IF l_budget_by_position = 'Y' THEN
1306 
1307     --
1308     -- This loop gets all the position_line_id for the new worksheet which will
1309     -- be used to maintain psb_ws_lines_positions table.
1310     --
1311     FOR l_lines_rec IN
1312     (
1313        SELECT lines.*
1314        FROM   psb_ws_lines_positions   lines ,
1315 	      psb_ws_position_lines    pos
1316        WHERE  lines.worksheet_id     = p_worksheet_id
1317        AND    lines.position_line_id = pos.position_line_id
1318        /*For Bug No : 2236283 Start*/
1319        /*
1320        AND    lines.position_line_id IN
1321 	      (
1322 		SELECT acct_lines.position_line_id
1323 		FROM   psb_ws_account_lines acct_lines
1324 		WHERE  acct_lines.budget_group_id IN
1325 		       (
1326 			 SELECT bg.budget_group_id
1327 			   FROM psb_budget_groups bg
1328 			  WHERE bg.budget_group_type = 'R'
1329 			    AND effective_start_date <= PSB_WS_Acct1.g_startdate_pp
1330 			    AND ((effective_end_date IS NULL)
1331 				  OR
1332 				 (effective_end_date >= PSB_WS_Acct1.g_enddate_cy )
1333 			       )
1334 			 START WITH bg.budget_group_id = p_budget_group_id
1335 			 CONNECT BY PRIOR bg.budget_group_id =
1336 						     bg.parent_budget_group_id
1337 		       )
1338 	      )
1339        */
1340        AND    EXISTS
1341 	      (
1342 		SELECT 1
1343 		  FROM psb_ws_account_lines acct_lines
1344 		 WHERE acct_lines.position_line_id = lines.position_line_id
1345 		   AND EXISTS
1346 		       (
1347 			 SELECT bg.budget_group_id
1348 			   FROM psb_budget_groups bg
1349 			  WHERE bg.budget_group_type = 'R'
1350 			    AND budget_group_id = acct_lines.budget_group_id
1351 			    AND effective_start_date <= PSB_WS_Acct1.g_startdate_pp
1352 			    AND ((effective_end_date IS NULL)
1353 				  OR
1354 				 (effective_end_date >= PSB_WS_Acct1.g_enddate_cy )
1355 			       )
1356 			 START WITH bg.budget_group_id = p_budget_group_id
1357 			 CONNECT BY PRIOR bg.budget_group_id =
1358 						     bg.parent_budget_group_id
1359 		       )
1360 	      )
1361      /*For Bug No : 2236283 End*/
1362     )
1363     LOOP
1364       --
1365       -- Put the position_line_id in the psb_ws_lines_positions table for
1366       -- the new worksheet.
1367       --
1368       PSB_WS_Pos_Pvt.Create_Position_Matrix
1369       (
1370 	p_api_version        =>  1.0 ,
1371 	p_init_msg_list      =>  FND_API.G_FALSE ,
1372 	p_validation_level   =>  FND_API.G_VALID_LEVEL_NONE ,
1373 	p_return_status      =>  l_return_status ,
1374 	p_msg_count          =>  l_msg_count ,
1375 	p_msg_data           =>  l_msg_data ,
1376 	--
1377 	p_worksheet_id       =>  l_new_worksheet_id ,
1378 	p_position_line_id   =>  l_lines_rec.position_line_id ,
1379 	p_freeze_flag        =>  l_lines_rec.freeze_flag ,
1380 	p_view_line_flag     =>  l_lines_rec.view_line_flag
1381       ) ;
1382       --
1383       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
1384 	RAISE FND_API.G_EXC_ERROR ;
1385       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1386 	RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
1387       END IF;
1388       --
1389     END LOOP;
1390 
1391   END IF;
1392 
1393   p_worksheet_id_OUT := l_new_worksheet_id;
1394 
1395   --
1396   IF FND_API.To_Boolean ( p_commit ) THEN
1397     COMMIT WORK;
1398   END IF;
1399   --
1400   FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
1401 			      p_data  => p_msg_data );
1402   --
1403 EXCEPTION
1404   --
1405   WHEN FND_API.G_EXC_ERROR THEN
1406     --
1407     ROLLBACK TO Create_Worksheet_Pvt ;
1408     p_return_status := FND_API.G_RET_STS_ERROR;
1409     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
1410 				p_data  => p_msg_data );
1411   --
1412   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1413     --
1414     ROLLBACK TO Create_Worksheet_Pvt ;
1415     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1416     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
1417 				p_data  => p_msg_data );
1418   --
1419   WHEN OTHERS THEN
1420     --
1421     IF ( l_worksheets_csr%ISOPEN ) THEN
1422       CLOSE l_worksheets_csr ;
1423     END IF ;
1424     --
1425     ROLLBACK TO Create_Worksheet_Pvt ;
1426     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1427     --
1428     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
1429       FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME,
1430 				l_api_name);
1431     END if;
1432     --
1433     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
1434 				p_data  => p_msg_data );
1435     --
1436 
1437 END Create_Worksheet;
1438 /*---------------------------------------------------------------------------*/
1439 
1440 
1441 /*===========================================================================+
1442  |                     PROCEDURE Copy_Worksheet                              |
1443  +===========================================================================*/
1444 --
1445 -- The API is to copy a given worksheet.
1446 --
1447 PROCEDURE Copy_Worksheet
1448 (
1449   p_api_version               IN       NUMBER   ,
1450   p_init_msg_list             IN       VARCHAR2 := FND_API.G_FALSE ,
1451   p_commit                    IN       VARCHAR2 := FND_API.G_FALSE ,
1452   p_validation_level          IN       NUMBER   := FND_API.G_VALID_LEVEL_FULL ,
1453   p_return_status             OUT  NOCOPY      VARCHAR2 ,
1454   p_msg_count                 OUT  NOCOPY      NUMBER   ,
1455   p_msg_data                  OUT  NOCOPY      VARCHAR2 ,
1456   --
1457   p_worksheet_id              IN       psb_worksheets.worksheet_id%TYPE ,
1458   p_worksheet_id_OUT          OUT  NOCOPY      psb_worksheets.worksheet_id%TYPE
1459 )
1460 
1461 IS
1462   --
1463   l_api_name                      CONSTANT VARCHAR2(30)   := 'Copy_Worksheet' ;
1464   l_api_version                   CONSTANT NUMBER         :=  1.0 ;
1465   --
1466   l_return_status                 VARCHAR2(1) ;
1467   l_msg_count                     NUMBER ;
1468   l_msg_data                      VARCHAR2(2000) ;
1469   --
1470   l_worksheet_name                psb_worksheets.name%TYPE ;
1471   l_budget_by_position            psb_worksheets.budget_by_position%TYPE ;
1472   l_worksheet_description         psb_worksheets.description%TYPE ;
1473   l_main_budget_group_name        psb_budget_groups.name%TYPE ;
1474   l_new_worksheet_id              psb_worksheets.worksheet_id%TYPE ;
1475   l_new_position_line_id          psb_ws_lines_positions.position_line_id%TYPE ;
1476   l_new_fte_line_id               psb_ws_fte_lines.fte_line_id%TYPE ;
1477   l_new_element_line_id           psb_ws_element_lines.element_line_id%TYPE ;
1478   l_new_position_assignment_id
1479 			psb_position_assignments.position_assignment_id%TYPE ;
1480   l_rowid                         VARCHAR2(2000);
1481   l_period_amount_tbl             PSB_WS_Acct1.g_prdamt_tbl_type ;
1482   l_period_fte_tbl                PSB_WS_Acct1.g_prdamt_tbl_type ;
1483   l_segment_values_tbl            FND_FLEX_EXT.SegmentArray ;
1484   --
1485   l_dummy_account_line_id         psb_ws_account_lines.account_line_id%TYPE ;
1486   --
1487   CURSOR l_worksheets_csr IS
1488 	 SELECT *
1489 	 FROM   psb_worksheets
1490 	 WHERE  worksheet_id = p_worksheet_id ;
1491   --
1492   l_ws_row_type l_worksheets_csr%ROWTYPE ;
1493   --
1494 BEGIN
1495   --
1496   SAVEPOINT Copy_Worksheet_Pvt ;
1497   --
1498   IF NOT FND_API.Compatible_API_Call ( l_api_version,
1499 				       p_api_version,
1500 				       l_api_name,
1501 				       G_PKG_NAME )
1502   THEN
1503     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
1504   END IF;
1505   --
1506 
1507   IF FND_API.To_Boolean ( p_init_msg_list ) THEN
1508     FND_MSG_PUB.initialize ;
1509   END IF;
1510   --
1511   p_return_status := FND_API.G_RET_STS_SUCCESS ;
1512   --
1513 
1514   --
1515   -- Finding the worksheet information.
1516   --
1517   OPEN  l_worksheets_csr ;
1518 
1519   FETCH l_worksheets_csr INTO l_ws_row_type ;
1520 
1521   IF ( l_worksheets_csr%NOTFOUND ) THEN
1522     --
1523     Fnd_Message.Set_Name ('PSB',     'PSB_INVALID_WORKSHEET_ID') ;
1524     Fnd_Message.Set_Token('ROUTINE', l_api_name ) ;
1525     FND_MSG_PUB.Add;
1526     RAISE FND_API.G_EXC_ERROR ;
1527   END IF ;
1528 
1529   -- l_budget_by_position defines whether worksheet contains positions or not.
1530   l_budget_by_position := NVL(l_ws_row_type.budget_by_position, 'N') ;
1531 
1532   --
1533   -- Only official worksheet can be made copy of.
1534   --
1535   IF l_ws_row_type.worksheet_type <> 'O'  THEN
1536     Fnd_Message.Set_Name ('PSB', 'PSB_INVALID_WORKSHEET_FOR_COPY') ;
1537     FND_MSG_PUB.Add;
1538     RAISE FND_API.G_EXC_ERROR ;
1539   END IF ;
1540 
1541   --
1542   -- Finding the main budget group name.
1543   --
1544   SELECT name INTO l_main_budget_group_name
1545   FROM   psb_budget_groups
1546   WHERE  budget_group_id = l_ws_row_type.budget_group_id ;
1547 
1548   --
1549   -- Get worksheet description.
1550   --
1551   Fnd_Message.Set_Name ( 'PSB', 'PSB_WORKSHEET_COPIED_INFO') ;
1552   Fnd_Message.Set_Token( 'WORKSHEET_ID',      p_worksheet_id ) ;
1553   Fnd_Message.Set_Token( 'BUDGET_GROUP_NAME', l_main_budget_group_name ) ;
1554   l_worksheet_description := Fnd_Message.Get ;
1555 
1556   --
1557   -- Create the new worksheet in psb_worksheets table.
1558   --
1559   PSB_Worksheet_Pvt.Create_Worksheet
1560   (
1561    p_api_version                 => 1.0 ,
1562    p_init_msg_list               => FND_API.G_FALSE,
1563    p_commit                      => FND_API.G_FALSE,
1564    p_validation_level            => FND_API.G_VALID_LEVEL_NONE,
1565    p_return_status               => l_return_status,
1566    p_msg_count                   => l_msg_count,
1567    p_msg_data                    => l_msg_data ,
1568    --
1569    p_budget_group_id             => l_ws_row_type.budget_group_id,
1570    p_budget_calendar_id          => l_ws_row_type.budget_calendar_id,
1571    p_worksheet_type              => 'L',
1572    p_name                        => NULL ,
1573    p_description                 => l_worksheet_description ,
1574    p_ws_creation_complete        => l_ws_row_type.ws_creation_complete ,
1575    p_stage_set_id                => l_ws_row_type.stage_set_id,
1576    p_current_stage_seq           => l_ws_row_type.current_stage_seq,
1577    -- Bug 4310415
1578    -- If this itself is a global worksheet, pass the worksheet_id.
1579    p_global_worksheet_id
1580      => NVL(l_ws_row_type.global_worksheet_id, l_ws_row_type.worksheet_id),
1581    p_global_worksheet_flag       => 'N',
1582    p_global_worksheet_option     => l_ws_row_type.global_worksheet_option,
1583    p_local_copy_flag             => 'Y',
1584    p_copy_of_worksheet_id        => p_worksheet_id,
1585    p_freeze_flag                 => l_ws_row_type.freeze_flag,
1586    p_budget_by_position          => l_ws_row_type.budget_by_position,
1587    p_use_revised_element_rates   => l_ws_row_type.use_revised_element_rates,
1588    p_num_proposed_years          => l_ws_row_type.num_proposed_years,
1589    p_num_years_to_allocate       => l_ws_row_type.num_years_to_allocate,
1590    p_rounding_factor             => l_ws_row_type.rounding_factor,
1591    p_gl_cutoff_period            => l_ws_row_type.gl_cutoff_period,
1592    p_include_stat_balance        => l_ws_row_type.include_stat_balance,
1593    p_include_trans_balance       => l_ws_row_type.include_translated_balance,
1594    p_include_adj_period          => l_ws_row_type.include_adjustment_periods,
1595    p_data_extract_id             => l_ws_row_type.data_extract_id,
1596    p_parameter_set_id            => NULL,
1597    p_constraint_set_id           => NULL,
1598    p_allocrule_set_id            => NULL,
1599    p_date_submitted              => l_ws_row_type.date_submitted,
1600    p_submitted_by                => l_ws_row_type.submitted_by,
1601    p_attribute1                  => l_ws_row_type.attribute1,
1602    p_attribute2                  => l_ws_row_type.attribute2,
1603    p_attribute3                  => l_ws_row_type.attribute3,
1604    p_attribute4                  => l_ws_row_type.attribute4,
1605    p_attribute5                  => l_ws_row_type.attribute5,
1606    p_attribute6                  => l_ws_row_type.attribute6,
1607    p_attribute7                  => l_ws_row_type.attribute7,
1608    p_attribute8                  => l_ws_row_type.attribute8,
1609    p_attribute9                  => l_ws_row_type.attribute9,
1610    p_attribute10                 => l_ws_row_type.attribute10,
1611    p_context                     => l_ws_row_type.context,
1612    p_worksheet_id                => l_new_worksheet_id
1613   );
1614   --
1615   IF l_return_status = FND_API.G_RET_STS_ERROR THEN
1616     CLOSE l_worksheets_csr ;
1617     RAISE FND_API.G_EXC_ERROR ;
1618   ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1619     CLOSE l_worksheets_csr ;
1620     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
1621   END IF;
1622 
1623   --
1624   -- Get account_line related info for the current worksheet to be copied in
1625   -- psb_ws_lines and psb_ws_account_lines table for the target worksheet.
1626   -- The positon related account lines will be updated by position phase.
1627   --
1628   FOR l_lines_accts_rec IN
1629   (
1630      SELECT accts.*
1631      FROM   psb_ws_lines          lines ,
1632 	    psb_ws_account_lines  accts
1633      WHERE  lines.worksheet_id     = p_worksheet_id
1634      AND    lines.account_line_id  = accts.account_line_id
1635      AND    accts.position_line_id IS NULL
1636   )
1637   LOOP
1638     --
1639     -- Create records in psb_ws_lines and psb_ws_account_lines for the
1640     -- new worksheet.
1641     --
1642     PSB_WS_Ops_Pvt.Create_Local_Dist_Pvt
1643     (
1644        p_account_line_id        =>   l_lines_accts_rec.account_line_id  ,
1645        p_new_worksheet_id       =>   l_new_worksheet_id                 ,
1646        p_new_position_line_id   =>   NULL                               ,
1647        /*For Bug No : 2440100 Start*/
1648        p_return_status          =>   l_return_status
1649     ) ;
1650     --
1651     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
1652       RAISE FND_API.G_EXC_ERROR ;
1653     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1654       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
1655     END IF;
1656     --
1657   END LOOP;
1658 
1659   --
1660   -- Copy position related information.
1661   --
1662   IF l_budget_by_position = 'Y' THEN
1663 
1664     --
1665     -- We need to find all the position_line_id for the worksheet and each of
1666     -- the position_line_id needs to be copied in psb_ws_position_lines table.
1667     -- The related rows in psb_ws_fte_lines and psb_ws_element_lines will
1668     -- also be copied and assigned new position_line_id as created in the
1669     -- psb_ws_position_lines table.
1670     --
1671 
1672     FOR l_lines_pos_rec IN
1673     (
1674        SELECT positions.*
1675        FROM   psb_ws_lines_positions   lines ,
1676 	      psb_ws_position_lines    positions
1677        WHERE  lines.worksheet_id     = p_worksheet_id
1678        AND    lines.position_line_id = positions.position_line_id
1679     )
1680     LOOP
1681 
1682       --
1683       -- API creates records in psb_ws_lines_positions and
1684       -- psb_ws_position_lines for the new worksheet.
1685       --
1686       PSB_WS_Pos_Pvt.Create_Position_Lines
1687       (
1688 	 p_api_version               =>   1.0 ,
1689 	 p_init_msg_list             =>   FND_API.G_FALSE ,
1690 	 p_commit                    =>   FND_API.G_FALSE ,
1691 	 p_validation_level          =>   FND_API.G_VALID_LEVEL_FULL ,
1692 	 p_return_status             =>   l_return_status ,
1693 	 p_msg_count                 =>   l_msg_count ,
1694 	 p_msg_data                  =>   l_msg_data ,
1695 	 --
1696 	 p_position_line_id          =>   l_new_position_line_id ,
1697 	 p_worksheet_id              =>   l_new_worksheet_id ,
1698 	 p_position_id               =>   l_lines_pos_rec.position_id ,
1699 	 p_budget_group_id           =>   l_lines_pos_rec.budget_group_id ,
1700 	 p_copy_of_position_line_id  =>   l_lines_pos_rec.position_line_id
1701       );
1702       --
1703       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
1704 	RAISE FND_API.G_EXC_ERROR ;
1705       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1706 	RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
1707       END IF;
1708       --
1709 
1710       --
1711       -- Create new account distribution for the new position_line_id by
1712       -- using original position_line_id related information. Note in the
1713       -- account phase, we did not consider position related account lines.
1714       --
1715       FOR l_accts_rec IN
1716       (
1717 	 SELECT account_line_id
1718 	 FROM   psb_ws_account_lines
1719 	 WHERE  position_line_id  = l_lines_pos_rec.position_line_id
1720       )
1721       LOOP
1722 	--
1723 	PSB_WS_Ops_Pvt.Create_Local_Dist_Pvt
1724 	(
1725 	   p_account_line_id        =>   l_accts_rec.account_line_id  ,
1726 	   p_new_worksheet_id       =>   l_new_worksheet_id           ,
1727 	   p_new_position_line_id   =>   l_new_position_line_id       ,
1728 	   p_return_status          =>   l_return_status
1729 	) ;
1730 
1731 	--
1732 	IF l_return_status = FND_API.G_RET_STS_ERROR THEN
1733 	  RAISE FND_API.G_EXC_ERROR ;
1734 	ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1735 	  RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
1736 	END IF;
1737 	--
1738       END LOOP;
1739 
1740 /*
1741       --
1742       -- Update the old position_line_id with new position_line_id in
1743       -- psb_ws_account_lines table.
1744       --
1745       UPDATE psb_ws_account_lines
1746       SET    position_line_id = l_new_position_line_id
1747       WHERE  position_line_id = l_lines_pos_rec.position_line_id
1748       AND    account_line_id IN
1749 	     (
1750 	       SELECT account_line_id
1751 	       FROM   psb_ws_lines
1752 	       WHERE  worksheet_id = l_new_worksheet_id
1753 	     ) ;
1754 */
1755 
1756       --
1757       -- Copy each record in psb_ws_fte_lines table for the current
1758       -- l_lines_pos_rec.position_line_id. The new l_new_position_line_id
1759       -- will replace the position_line_id column in new created records.
1760       --
1761       FOR l_fte_rec IN
1762       (
1763 	 SELECT *
1764 	 FROM   psb_ws_fte_lines
1765 	 WHERE  position_line_id = l_lines_pos_rec.position_line_id
1766       )
1767       LOOP
1768 
1769 	--
1770 	-- Populate the l_period_fte_tbl ( used by PSB_WS_Acct1 API )
1771 	--
1772 	l_period_fte_tbl(1)  := l_fte_rec.period1_fte ;
1773 	l_period_fte_tbl(2)  := l_fte_rec.period2_fte ;
1774 	l_period_fte_tbl(3)  := l_fte_rec.period3_fte ;
1775 	l_period_fte_tbl(4)  := l_fte_rec.period4_fte ;
1776 	l_period_fte_tbl(5)  := l_fte_rec.period5_fte ;
1777 	l_period_fte_tbl(6)  := l_fte_rec.period6_fte ;
1778 	l_period_fte_tbl(7)  := l_fte_rec.period7_fte ;
1779 	l_period_fte_tbl(8)  := l_fte_rec.period8_fte ;
1780 	l_period_fte_tbl(9)  := l_fte_rec.period9_fte ;
1781 	l_period_fte_tbl(10) := l_fte_rec.period10_fte ;
1782 	l_period_fte_tbl(11) := l_fte_rec.period11_fte ;
1783 	l_period_fte_tbl(12) := l_fte_rec.period12_fte ;
1784 	l_period_fte_tbl(13) := l_fte_rec.period13_fte ;
1785 	l_period_fte_tbl(14) := l_fte_rec.period14_fte ;
1786 	l_period_fte_tbl(15) := l_fte_rec.period15_fte ;
1787 	l_period_fte_tbl(16) := l_fte_rec.period16_fte ;
1788 	l_period_fte_tbl(17) := l_fte_rec.period17_fte ;
1789 	l_period_fte_tbl(18) := l_fte_rec.period18_fte ;
1790 	l_period_fte_tbl(19) := l_fte_rec.period19_fte ;
1791 	l_period_fte_tbl(20) := l_fte_rec.period20_fte ;
1792 	l_period_fte_tbl(21) := l_fte_rec.period21_fte ;
1793 	l_period_fte_tbl(22) := l_fte_rec.period22_fte ;
1794 	l_period_fte_tbl(23) := l_fte_rec.period23_fte ;
1795 	l_period_fte_tbl(24) := l_fte_rec.period24_fte ;
1796 	l_period_fte_tbl(25) := l_fte_rec.period25_fte ;
1797 	l_period_fte_tbl(26) := l_fte_rec.period26_fte ;
1798 	l_period_fte_tbl(27) := l_fte_rec.period27_fte ;
1799 	l_period_fte_tbl(28) := l_fte_rec.period28_fte ;
1800 	l_period_fte_tbl(29) := l_fte_rec.period29_fte ;
1801 	l_period_fte_tbl(30) := l_fte_rec.period30_fte ;
1802 	l_period_fte_tbl(31) := l_fte_rec.period31_fte ;
1803 	l_period_fte_tbl(32) := l_fte_rec.period32_fte ;
1804 	l_period_fte_tbl(33) := l_fte_rec.period33_fte ;
1805 	l_period_fte_tbl(34) := l_fte_rec.period34_fte ;
1806 	l_period_fte_tbl(35) := l_fte_rec.period35_fte ;
1807 	l_period_fte_tbl(36) := l_fte_rec.period36_fte ;
1808 	l_period_fte_tbl(37) := l_fte_rec.period37_fte ;
1809 	l_period_fte_tbl(38) := l_fte_rec.period38_fte ;
1810 	l_period_fte_tbl(39) := l_fte_rec.period39_fte ;
1811 	l_period_fte_tbl(40) := l_fte_rec.period40_fte ;
1812 	l_period_fte_tbl(41) := l_fte_rec.period41_fte ;
1813 	l_period_fte_tbl(42) := l_fte_rec.period42_fte ;
1814 	l_period_fte_tbl(43) := l_fte_rec.period43_fte ;
1815 	l_period_fte_tbl(44) := l_fte_rec.period44_fte ;
1816 	l_period_fte_tbl(45) := l_fte_rec.period45_fte ;
1817 	l_period_fte_tbl(46) := l_fte_rec.period46_fte ;
1818 	l_period_fte_tbl(47) := l_fte_rec.period47_fte ;
1819 	l_period_fte_tbl(48) := l_fte_rec.period48_fte ;
1820 	l_period_fte_tbl(49) := l_fte_rec.period49_fte ;
1821 	l_period_fte_tbl(50) := l_fte_rec.period50_fte ;
1822 	l_period_fte_tbl(51) := l_fte_rec.period51_fte ;
1823 	l_period_fte_tbl(52) := l_fte_rec.period52_fte ;
1824 	l_period_fte_tbl(53) := l_fte_rec.period53_fte ;
1825 	l_period_fte_tbl(54) := l_fte_rec.period54_fte ;
1826 	l_period_fte_tbl(55) := l_fte_rec.period55_fte ;
1827 	l_period_fte_tbl(56) := l_fte_rec.period56_fte ;
1828 	l_period_fte_tbl(57) := l_fte_rec.period57_fte ;
1829 	l_period_fte_tbl(58) := l_fte_rec.period58_fte ;
1830 	l_period_fte_tbl(59) := l_fte_rec.period59_fte ;
1831 	l_period_fte_tbl(60) := l_fte_rec.period60_fte ;
1832 
1833 	-- API to create new fte lines in psb_ws_fte_lines.
1834 	PSB_WS_Pos_Pvt.Create_FTE_Lines
1835 	(
1836 	   p_api_version              =>   1.0 ,
1837 	   p_init_msg_list            =>   FND_API.G_FALSE ,
1838 	   p_commit                   =>   FND_API.G_FALSE ,
1839 	   p_validation_level         =>   FND_API.G_VALID_LEVEL_FULL ,
1840 	   p_return_status            =>   l_return_status ,
1841 	   p_msg_count                =>   l_msg_count ,
1842 	   p_msg_data                 =>   l_msg_data ,
1843 	   --
1844 	   p_fte_line_id              =>   l_new_fte_line_id ,
1845 	   p_check_spfl_exists        =>   FND_API.G_FALSE,
1846 	   p_worksheet_id             =>   l_new_worksheet_id ,
1847 	   p_position_line_id         =>   l_new_position_line_id ,
1848 	   p_budget_year_id           =>   l_fte_rec.budget_year_id ,
1849 	   p_annual_fte               =>   l_fte_rec.annual_fte ,
1850 	   p_service_package_id       =>   l_fte_rec.service_package_id ,
1851 	   p_stage_set_id             =>   l_fte_rec.stage_set_id ,
1852 	   p_start_stage_seq          =>   l_fte_rec.start_stage_seq ,
1853 	   p_current_stage_seq        =>   l_fte_rec.current_stage_seq ,
1854 	   p_end_stage_seq            =>   nvl(l_fte_rec.end_stage_seq, FND_API.G_MISS_NUM),
1855 	   p_period_fte               =>   l_period_fte_tbl
1856 	);
1857 	--
1858 	IF l_return_status = FND_API.G_RET_STS_ERROR THEN
1859 	  RAISE FND_API.G_EXC_ERROR ;
1860 	ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1861 	  RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
1862 	END IF;
1863 	--
1864       END LOOP;   -- To process fte_lines in psb_ws_element_lines.
1865 
1866       --
1867       -- Copy each record in psb_ws_element_lines table for the current
1868       -- l_lines_pos_rec.position_line_id. The new l_new_position_line_id
1869       -- will replace the position_line_id column in new created records.
1870       --
1871       FOR l_element_rec IN
1872       (
1873 	 SELECT *
1874 	 FROM   psb_ws_element_lines
1875 	 WHERE  position_line_id = l_lines_pos_rec.position_line_id
1876       )
1877       LOOP
1878 
1879 	-- API to create new element lines in psb_ws_element_lines.
1880 	PSB_WS_Pos_Pvt.Create_Element_Lines
1881 	(
1882 	   p_api_version              =>   1.0 ,
1883 	   p_init_msg_list            =>   FND_API.G_FALSE ,
1884 	   p_commit                   =>   FND_API.G_FALSE ,
1885 	   p_validation_level         =>   FND_API.G_VALID_LEVEL_FULL ,
1886 	   p_return_status            =>   l_return_status ,
1887 	   p_msg_count                =>   l_msg_count ,
1888 	   p_msg_data                 =>   l_msg_data ,
1889 	   --
1890 	   p_element_line_id          =>   l_new_element_line_id ,
1891 	   p_position_line_id         =>   l_new_position_line_id ,
1892 	   p_budget_year_id           =>   l_element_rec.budget_year_id ,
1893 	   p_pay_element_id           =>   l_element_rec.pay_element_id ,
1894 	   p_currency_code            =>   l_element_rec.currency_code ,
1895 	   p_element_cost             =>   l_element_rec.element_cost ,
1896 	   p_element_set_id           =>   l_element_rec.element_set_id ,
1897 	   p_service_package_id       =>   l_element_rec.service_package_id ,
1898 	   p_stage_set_id             =>   l_element_rec.stage_set_id ,
1899 	   p_start_stage_seq          =>   l_element_rec.start_stage_seq ,
1900 	   p_current_stage_seq        =>   l_element_rec.current_stage_seq,
1901 	   p_end_stage_seq            =>   nvl(l_element_rec.end_stage_seq, FND_API.G_MISS_NUM)
1902 	);
1903 	--
1904 	IF l_return_status = FND_API.G_RET_STS_ERROR THEN
1905 	  RAISE FND_API.G_EXC_ERROR ;
1906 	ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1907 	  RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
1908 	END IF;
1909 	--
1910       END LOOP;   -- To process element_lines in psb_ws_element_lines.
1911 
1912       --
1913       -- Copy position assignment information related to the worksheet.
1914       -- ( Note that psb_position_assignments will have values specific to
1915       --   the global worksheet for the official ones.)
1916       --
1917       FOR l_asgn_rec IN
1918       (
1919 	 SELECT asgn.*
1920 	 FROM   psb_ws_position_lines    pos  ,
1921 		psb_position_assignments asgn
1922 	 WHERE  pos.position_line_id = l_lines_pos_rec.position_line_id
1923 	 AND    asgn.worksheet_id    = l_ws_row_type.global_worksheet_id
1924 	 AND    asgn.position_id     = pos.position_id
1925       )
1926       LOOP
1927 
1928 	--
1929 	-- API will create new position assignments.
1930 	--
1931 	PSB_Positions_Pvt.Modify_Assignment
1932 	(
1933 	   p_api_version                 => 1.0 ,
1934 	   p_init_msg_list               => FND_API.G_FALSE ,
1935 	   p_commit                      => FND_API.G_FALSE ,
1936 	   p_validation_level            => FND_API.G_VALID_LEVEL_NONE ,
1937 	   p_return_status               => l_return_status ,
1938 	   p_msg_count                   => l_msg_count ,
1939 	   p_msg_data                    => l_msg_data ,
1940 	   --
1941 	   p_position_assignment_id      => l_new_position_assignment_id ,
1942 	   p_element_value_type          => l_asgn_rec.element_value_type ,
1943 	   p_data_extract_id             => l_asgn_rec.data_extract_id ,
1944 	   p_worksheet_id                => l_new_worksheet_id ,
1945 	   p_position_id                 => l_asgn_rec.position_id ,
1946 	   p_assignment_type             => l_asgn_rec.assignment_type ,
1947 	   p_attribute_id                => l_asgn_rec.attribute_id ,
1948 	   p_attribute_value_id          => l_asgn_rec.attribute_value_id ,
1949 	   p_attribute_value             => l_asgn_rec.attribute_value ,
1950 	   p_pay_element_id              => l_asgn_rec.pay_element_id ,
1951 	   p_pay_element_option_id       => l_asgn_rec.pay_element_option_id ,
1952 	   p_effective_start_date        => l_asgn_rec.effective_start_date ,
1953 	   p_effective_end_date          => l_asgn_rec.effective_end_date ,
1954 	   p_element_value               => l_asgn_rec.element_value ,
1955 	   p_global_default_flag         => l_asgn_rec.global_default_flag ,
1956 	   p_assignment_default_rule_id  =>
1957 				      l_asgn_rec.assignment_default_rule_id ,
1958 	   p_modify_flag                 => l_asgn_rec.modify_flag ,
1959 	   p_rowid                       => l_rowid ,
1960 	   p_currency_code               => l_asgn_rec.currency_code ,
1961 	   p_pay_basis                   => l_asgn_rec.pay_basis ,
1962 	   p_employee_id                 => l_asgn_rec.employee_id ,
1963 	   p_primary_employee_flag       => l_asgn_rec.primary_employee_flag ,
1964 	   p_mode                        => 'R'
1965 	) ;
1966 	--
1967 	IF l_return_status = FND_API.G_RET_STS_ERROR THEN
1968 	  RAISE FND_API.G_EXC_ERROR ;
1969 	ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1970 	  RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
1971 	END IF;
1972 	--
1973       END LOOP; -- To process assignments in psb_position_assignments for
1974 		-- current position line l_lines_pos_rec.position_line_id.
1975 
1976     END LOOP;   -- To process position_lines in psb_ws_position_lines.
1977 
1978 
1979   END IF;
1980 
1981   CLOSE l_worksheets_csr ;
1982   --
1983   p_worksheet_id_OUT := l_new_worksheet_id;
1984   --
1985   IF FND_API.To_Boolean ( p_commit ) THEN
1986     COMMIT WORK;
1987   END IF;
1988   --
1989   FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
1990 			      p_data  => p_msg_data );
1991   --
1992 EXCEPTION
1993   --
1994   WHEN FND_API.G_EXC_ERROR THEN
1995     --
1996     ROLLBACK TO Copy_Worksheet_Pvt ;
1997     p_return_status := FND_API.G_RET_STS_ERROR;
1998     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
1999 				p_data  => p_msg_data );
2000   --
2001   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2002     --
2003     ROLLBACK TO Copy_Worksheet_Pvt ;
2004     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2005     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
2006 				p_data  => p_msg_data );
2007   --
2008   WHEN OTHERS THEN
2009     --
2010     IF ( l_worksheets_csr%ISOPEN ) THEN
2011       CLOSE l_worksheets_csr ;
2012     END IF ;
2013     --
2014     ROLLBACK TO Copy_Worksheet_Pvt ;
2015     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2016     --
2017     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
2018       FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME,
2019 				l_api_name);
2020     END if;
2021     --
2022     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
2023 				p_data  => p_msg_data );
2024      --
2025 END Copy_Worksheet;
2026 /*---------------------------------------------------------------------------*/
2027 
2028 
2029 /*===========================================================================+
2030  |                     PROCEDURE Merge_Worksheets                            |
2031  +===========================================================================*/
2032 --
2033 -- The API merges a local copy onto an official worksheet. The source worksheet
2034 -- is merged onto the target worksheet.
2035 --
2036 PROCEDURE Merge_Worksheets
2037 (
2038   p_api_version               IN       NUMBER   ,
2039   p_init_msg_list             IN       VARCHAR2 := FND_API.G_FALSE ,
2040   p_commit                    IN       VARCHAR2 := FND_API.G_FALSE ,
2041   p_validation_level          IN       NUMBER   := FND_API.G_VALID_LEVEL_FULL ,
2042   p_return_status             OUT  NOCOPY      VARCHAR2 ,
2043   p_msg_count                 OUT  NOCOPY      NUMBER   ,
2044   p_msg_data                  OUT  NOCOPY      VARCHAR2 ,
2045   --
2046   p_source_worksheet_id       IN       psb_worksheets.worksheet_id%TYPE ,
2047   p_target_worksheet_id       IN       psb_worksheets.worksheet_id%TYPE
2048 )
2049 IS
2050   --
2051   l_api_name                       CONSTANT VARCHAR2(30) := 'Merge_Worksheets';
2052   l_api_version                    CONSTANT NUMBER       :=  1.0 ;
2053   --
2054   l_return_status                  VARCHAR2(1) ;
2055   l_msg_count                      NUMBER ;
2056   l_msg_data                       VARCHAR2(2000) ;
2057   --
2058   l_source_budget_group_id         psb_worksheets.budget_group_id%TYPE ;
2059   l_source_copy_of_worksheet_id    psb_worksheets.budget_group_id%TYPE ;
2060   l_source_local_copy_flag         psb_worksheets.local_copy_flag%TYPE ;
2061   l_source_budget_by_position      psb_worksheets.budget_by_position%TYPE ;
2062   l_source_current_stage_seq       psb_worksheets.current_stage_seq%TYPE ;
2063   --
2064   l_target_budget_group_id         psb_worksheets.budget_group_id%TYPE ;
2065   l_target_freeze_flag             psb_worksheets.freeze_flag%TYPE ;
2066   l_target_local_copy_flag         psb_worksheets.local_copy_flag%TYPE ;
2067   l_target_current_stage_seq       psb_worksheets.current_stage_seq%TYPE ;
2068   l_target_account_line_id         psb_ws_account_lines.account_line_id%TYPE ;
2069   l_target_position_line_id        psb_ws_lines_positions.position_line_id%TYPE;
2070   l_target_position_id             psb_ws_position_lines.position_id%TYPE;
2071   --
2072   l_new_position_line_id           psb_ws_lines_positions.position_line_id%TYPE;
2073   l_new_fte_line_id                psb_ws_fte_lines.fte_line_id%TYPE ;
2074   l_new_element_line_id            psb_ws_element_lines.element_line_id%TYPE ;
2075   l_new_position_assignment_id
2076 			psb_position_assignments.position_assignment_id%TYPE ;
2077   l_rowid                         VARCHAR2(2000);
2078   --
2079   l_parent_worksheet_id            psb_worksheets.worksheet_id%TYPE ;
2080   l_global_worksheet_id            psb_worksheets.worksheet_id%TYPE ;
2081   l_global_worksheet_flag          psb_worksheets.global_worksheet_flag%TYPE ;
2082   l_period_amount_tbl              PSB_WS_Acct1.g_prdamt_tbl_type ;
2083   l_period_fte_tbl                 PSB_WS_Acct1.g_prdamt_tbl_type ;
2084   l_segment_values_tbl             FND_FLEX_EXT.SegmentArray ;
2085   --
2086   CURSOR l_ws_account_lines_csr
2087 	 (
2088 	   c_copy_of_account_line_id
2089 			psb_ws_account_lines.copy_of_account_line_id%TYPE
2090 	 )
2091 	 IS
2092 	 SELECT account_line_id
2093 	 FROM   psb_ws_account_lines
2094 	 WHERE  account_line_id  = NVL( c_copy_of_account_line_id , -99)
2095 	 AND    ROWNUM                < 2 ;
2096   --
2097   l_new_account_line_id           psb_ws_account_lines.account_line_id%TYPE ;
2098   --
2099   CURSOR l_ws_position_lines_csr
2100 	 (
2101 	   c_copy_of_position_line_id
2102 			psb_ws_position_lines.copy_of_position_line_id%TYPE
2103 	 )
2104 	 IS
2105 	 SELECT position_line_id, position_id
2106 	 FROM   psb_ws_position_lines
2107 	 WHERE  position_line_id  = NVL( c_copy_of_position_line_id , -99)
2108 	 AND    ROWNUM                < 2 ;
2109   --
2110 BEGIN
2111   --
2112   SAVEPOINT Merge_Worksheets_Pvt ;
2113   --
2114   IF NOT FND_API.Compatible_API_Call ( l_api_version,
2115 				       p_api_version,
2116 				       l_api_name,
2117 				       G_PKG_NAME )
2118   THEN
2119     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
2120   END IF;
2121   --
2122 
2123   IF FND_API.To_Boolean ( p_init_msg_list ) THEN
2124     FND_MSG_PUB.initialize ;
2125   END IF;
2126   --
2127   p_return_status := FND_API.G_RET_STS_SUCCESS ;
2128   --
2129 
2130   --
2131   -- Find information about worksheets.
2132   --
2133 
2134   SELECT budget_group_id                  ,
2135 	 NVL( budget_by_position, 'N' )   ,
2136 	 NVL( copy_of_worksheet_id, -99 ) ,
2137 	 current_stage_seq
2138        INTO
2139 	 l_source_budget_group_id    ,
2140 	 l_source_budget_by_position ,
2141 	 l_source_copy_of_worksheet_id    ,
2142 	 l_source_current_stage_seq
2143   FROM   psb_worksheets
2144   WHERE  worksheet_id = p_source_worksheet_id ;
2145 
2146 
2147   SELECT budget_group_id                  ,
2148 	 current_stage_seq                ,
2149 	 NVL( freeze_flag , 'N' )         ,
2150 	 global_worksheet_id              ,
2151 	 global_worksheet_flag
2152        INTO
2153 	 l_target_budget_group_id         ,
2154 	 l_target_current_stage_seq       ,
2155 	 l_target_freeze_flag             ,
2156 	 l_global_worksheet_id            ,
2157 	 l_global_worksheet_flag
2158   FROM   psb_worksheets
2159   WHERE  worksheet_id = p_target_worksheet_id ;
2160 
2161   --
2162   -- Find global worksheet information for the target worksheet, used
2163   -- for position budgeting.
2164   --
2165   IF NVL(l_global_worksheet_flag, 'N') = 'Y' THEN
2166     l_global_worksheet_id := p_target_worksheet_id ;
2167   ELSE
2168     l_global_worksheet_id := l_global_worksheet_id ;
2169   END IF ;
2170 
2171   --
2172   -- Check whether the p_target_worksheet_id has been frozen.
2173   --
2174   IF l_target_freeze_flag = 'Y'  THEN
2175     Fnd_Message.Set_Name('PSB', 'PSB_TARGET_WORKSHEET_IS_FROZEN') ;
2176     FND_MSG_PUB.Add;
2177     RAISE FND_API.G_EXC_ERROR ;
2178   END IF ;
2179 
2180   --
2181   -- Validating that p_source_worksheet_id is a copy of p_target_worksheet_id
2182   -- as merge can only be performed from a copied worksheet to the original one.
2183   -- Also the p_source_worksheet_id and p_target_worksheet_id worksheets must
2184   -- be on the same stage.
2185   --
2186   IF NOT (
2187 	   ( l_source_copy_of_worksheet_id = p_target_worksheet_id )
2188 	   AND (l_source_current_stage_seq = l_target_current_stage_seq )
2189 	  )
2190   THEN
2191     Fnd_Message.Set_Name ('PSB', 'PSB_INCOMPATIBLE_WORKSHEETS') ;
2192     FND_MSG_PUB.Add;
2193     RAISE FND_API.G_EXC_ERROR ;
2194   END IF ;
2195 
2196   --
2197   -- Merging those account lines which are not related to positions.
2198   -- The positon related account lines will be updated by position phase.
2199   --
2200   /*For Bug No : 2440100 Start*/
2201   --added the last three filters
2202   FOR l_lines_accts_rec IN
2203   (
2204      SELECT accts.*
2205      FROM   psb_ws_lines          lines ,
2206 	    psb_ws_account_lines  accts
2207      WHERE  lines.worksheet_id    = p_source_worksheet_id
2208      AND    lines.account_line_id = accts.account_line_id
2209      AND    accts.position_line_id IS NULL
2210      AND    accts.end_stage_seq IS NULL
2211      AND    accts.template_id IS NULL
2212      AND    accts.balance_type = 'E'
2213   )
2214   LOOP
2215     --
2216     -- Finding p_target_worksheet_id in psb_ws_account_lines.
2217     --
2218     OPEN l_ws_account_lines_csr ( l_lines_accts_rec.copy_of_account_line_id ) ;
2219 
2220     FETCH l_ws_account_lines_csr INTO l_target_account_line_id;
2221 
2222     --
2223     -- Populate the l_period_amount_tbl ( used by PSB_WS_Acct1 API )
2224     --
2225     l_period_amount_tbl(1)  := l_lines_accts_rec.period1_amount ;
2226     l_period_amount_tbl(2)  := l_lines_accts_rec.period2_amount ;
2227     l_period_amount_tbl(3)  := l_lines_accts_rec.period3_amount ;
2228     l_period_amount_tbl(4)  := l_lines_accts_rec.period4_amount ;
2229     l_period_amount_tbl(5)  := l_lines_accts_rec.period5_amount ;
2230     l_period_amount_tbl(6)  := l_lines_accts_rec.period6_amount ;
2231     l_period_amount_tbl(7)  := l_lines_accts_rec.period7_amount ;
2232     l_period_amount_tbl(8)  := l_lines_accts_rec.period8_amount ;
2233     l_period_amount_tbl(9)  := l_lines_accts_rec.period9_amount ;
2234     l_period_amount_tbl(10) := l_lines_accts_rec.period10_amount ;
2235     l_period_amount_tbl(11) := l_lines_accts_rec.period11_amount ;
2236     l_period_amount_tbl(12) := l_lines_accts_rec.period12_amount ;
2237     l_period_amount_tbl(13) := l_lines_accts_rec.period13_amount ;
2238     l_period_amount_tbl(14) := l_lines_accts_rec.period14_amount ;
2239     l_period_amount_tbl(15) := l_lines_accts_rec.period15_amount ;
2240     l_period_amount_tbl(16) := l_lines_accts_rec.period16_amount ;
2241     l_period_amount_tbl(17) := l_lines_accts_rec.period17_amount ;
2242     l_period_amount_tbl(18) := l_lines_accts_rec.period18_amount ;
2243     l_period_amount_tbl(19) := l_lines_accts_rec.period19_amount ;
2244     l_period_amount_tbl(20) := l_lines_accts_rec.period20_amount ;
2245     l_period_amount_tbl(21) := l_lines_accts_rec.period21_amount ;
2246     l_period_amount_tbl(22) := l_lines_accts_rec.period22_amount ;
2247     l_period_amount_tbl(23) := l_lines_accts_rec.period23_amount ;
2248     l_period_amount_tbl(24) := l_lines_accts_rec.period24_amount ;
2249     l_period_amount_tbl(25) := l_lines_accts_rec.period25_amount ;
2250     l_period_amount_tbl(26) := l_lines_accts_rec.period26_amount ;
2251     l_period_amount_tbl(27) := l_lines_accts_rec.period27_amount ;
2252     l_period_amount_tbl(28) := l_lines_accts_rec.period28_amount ;
2253     l_period_amount_tbl(29) := l_lines_accts_rec.period29_amount ;
2254     l_period_amount_tbl(30) := l_lines_accts_rec.period30_amount ;
2255     l_period_amount_tbl(31) := l_lines_accts_rec.period31_amount ;
2256     l_period_amount_tbl(32) := l_lines_accts_rec.period32_amount ;
2257     l_period_amount_tbl(33) := l_lines_accts_rec.period33_amount ;
2258     l_period_amount_tbl(34) := l_lines_accts_rec.period34_amount ;
2259     l_period_amount_tbl(35) := l_lines_accts_rec.period35_amount ;
2260     l_period_amount_tbl(36) := l_lines_accts_rec.period36_amount ;
2261     l_period_amount_tbl(37) := l_lines_accts_rec.period37_amount ;
2262     l_period_amount_tbl(38) := l_lines_accts_rec.period38_amount ;
2263     l_period_amount_tbl(39) := l_lines_accts_rec.period39_amount ;
2264     l_period_amount_tbl(40) := l_lines_accts_rec.period40_amount ;
2265     l_period_amount_tbl(41) := l_lines_accts_rec.period41_amount ;
2266     l_period_amount_tbl(42) := l_lines_accts_rec.period42_amount ;
2267     l_period_amount_tbl(43) := l_lines_accts_rec.period43_amount ;
2268     l_period_amount_tbl(44) := l_lines_accts_rec.period44_amount ;
2269     l_period_amount_tbl(45) := l_lines_accts_rec.period45_amount ;
2270     l_period_amount_tbl(46) := l_lines_accts_rec.period46_amount ;
2271     l_period_amount_tbl(47) := l_lines_accts_rec.period47_amount ;
2272     l_period_amount_tbl(48) := l_lines_accts_rec.period48_amount ;
2273     l_period_amount_tbl(49) := l_lines_accts_rec.period49_amount ;
2274     l_period_amount_tbl(50) := l_lines_accts_rec.period50_amount ;
2275     l_period_amount_tbl(51) := l_lines_accts_rec.period51_amount ;
2276     l_period_amount_tbl(52) := l_lines_accts_rec.period52_amount ;
2277     l_period_amount_tbl(53) := l_lines_accts_rec.period53_amount ;
2278     l_period_amount_tbl(54) := l_lines_accts_rec.period54_amount ;
2279     l_period_amount_tbl(55) := l_lines_accts_rec.period55_amount ;
2280     l_period_amount_tbl(56) := l_lines_accts_rec.period56_amount ;
2281     l_period_amount_tbl(57) := l_lines_accts_rec.period57_amount ;
2282     l_period_amount_tbl(58) := l_lines_accts_rec.period58_amount ;
2283     l_period_amount_tbl(59) := l_lines_accts_rec.period59_amount ;
2284     l_period_amount_tbl(60) := l_lines_accts_rec.period60_amount ;
2285 
2286     IF ( l_ws_account_lines_csr%NOTFOUND ) THEN
2287       --
2288       -- It means a new line was created in the p_source_worksheet_id.
2289       -- Putting this line in the p_target_worksheet_id. The PSB_WS_Acct_Pvt
2290       -- API will also maintain psb_ws_lines.
2291       --
2292       PSB_WS_Acct_Pvt.Create_Account_Dist
2293       (
2294 	p_api_version             =>   1.0 ,
2295 	p_init_msg_list           =>   FND_API.G_FALSE,
2296 	p_commit                  =>   FND_API.G_FALSE,
2297 	p_validation_level        =>   FND_API.G_VALID_LEVEL_FULL,
2298 	p_return_status           =>   l_return_status,
2299 	p_account_line_id         =>   l_new_account_line_id,
2300 	p_check_spal_exists        =>   FND_API.G_FALSE,
2301 	p_msg_count               =>   l_msg_count,
2302 	p_msg_data                =>   l_msg_data,
2303 	--
2304 	p_worksheet_id            =>   p_target_worksheet_id,
2305 	p_budget_year_id          =>   l_lines_accts_rec.budget_year_id,
2306 	p_budget_group_id         =>   l_lines_accts_rec.budget_group_id,
2307 	p_ccid                    =>   l_lines_accts_rec.code_combination_id,
2308 	p_template_id             =>   NVL(l_lines_accts_rec.template_id ,
2309 					    FND_API.G_MISS_NUM ) ,
2310 	p_currency_code           =>   l_lines_accts_rec.currency_code ,
2311 	p_balance_type            =>   l_lines_accts_rec.balance_type ,
2312 	p_ytd_amount              =>   l_lines_accts_rec.ytd_amount  ,
2313 	p_distribute_flag         =>   FND_API.G_FALSE ,
2314 	p_annual_fte              =>   NVL ( l_lines_accts_rec.annual_fte,
2315 					     FND_API.G_MISS_NUM ) ,
2316 	p_period_amount           =>   l_period_amount_tbl ,
2317 	p_position_line_id        =>   NVL( l_lines_accts_rec.position_line_id,
2318 					    FND_API.G_MISS_NUM ) ,
2319 	p_element_set_id          =>   NVL( l_lines_accts_rec.element_set_id,
2320 					    FND_API.G_MISS_NUM ) ,
2321 	p_salary_account_line     =>  NVL(l_lines_accts_rec.salary_account_line,
2322 					  FND_API.G_FALSE ) ,
2323 	p_service_package_id      =>   l_lines_accts_rec.service_package_id ,
2324 	p_start_stage_seq         =>   l_lines_accts_rec.start_stage_seq ,
2325 	p_current_stage_seq       =>   l_lines_accts_rec.current_stage_seq ,
2326 	p_end_stage_seq           =>   NVL( l_lines_accts_rec.end_stage_seq,
2327 					    FND_API.G_MISS_NUM )
2328       );
2329       --
2330       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2331 	RAISE FND_API.G_EXC_ERROR ;
2332       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2333 	RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
2334       END IF;
2335 
2336       --
2337       -- The new_account_line_id will be null if the desired account lines are
2338       -- already there in the target worksheet. If not null, add the new
2339       -- account_line_id to all the worksheet up in the hierarchy.
2340       --
2341       IF l_new_account_line_id IS NOT NULL THEN
2342 
2343 	--
2344 	-- Add the account_line to all the worksheets up in the hierarchy.
2345 	-- ( The line has already been added in the target worksheet.)
2346 	--
2347 	PSB_WS_Ops_Pvt.Add_Worksheet_Line
2348 	(
2349 	   p_api_version               => 1.0 ,
2350 	   p_init_msg_list             => FND_API.G_FALSE,
2351 	   p_commit                    => FND_API.G_FALSE,
2352 	   p_validation_level          => FND_API.G_VALID_LEVEL_NONE,
2353 	   p_return_status             => l_return_status,
2354 	   p_msg_count                 => l_msg_count,
2355 	   p_msg_data                  => l_msg_data ,
2356 	   --
2357 	   p_worksheet_id              => p_target_worksheet_id ,
2358 	   p_account_line_id           => l_new_account_line_id ,
2359 	   p_add_in_current_worksheet  => FND_API.G_FALSE
2360 	) ;
2361 	--
2362 	IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2363 	  RAISE FND_API.G_EXC_ERROR ;
2364 	ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2365 	  RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
2366 	END IF;
2367 	--
2368       END IF;
2369       --
2370     ELSIF ( l_ws_account_lines_csr%FOUND ) THEN
2371       --
2372       -- It means it is not a new line, but the amount information may be
2373       -- different in the p_source_worksheet_id.
2374       --
2375 
2376       --
2377       -- Updating ytd_amount in the p_target_worksheet_id.
2378       --
2379 
2380       PSB_WS_Acct_Pvt.Create_Account_Dist
2381       (
2382 	p_api_version             =>   1.0 ,
2383 	p_init_msg_list           =>   FND_API.G_FALSE,
2384 	p_commit                  =>   FND_API.G_FALSE,
2385 	p_validation_level        =>   FND_API.G_VALID_LEVEL_FULL,
2386 	p_return_status           =>   l_return_status,
2387 	p_msg_count               =>   l_msg_count,
2388 	p_msg_data                =>   l_msg_data,
2389         -- comment out the following line for bug 3419241
2390         -- p_check_stages must be set to FND_API.G_TRUE to automatically
2391         -- create new Stage for the Account Line. p_check_stages is
2392         -- FND_API.G_TRUE by default.
2393 	--p_check_stages            =>   FND_API.G_FALSE,
2394 	p_worksheet_id            =>   p_target_worksheet_id,
2395 	p_account_line_id         =>   l_target_account_line_id,
2396 	p_ytd_amount              =>   l_lines_accts_rec.ytd_amount,
2397 	p_period_amount           =>   l_period_amount_tbl,
2398 	p_service_package_id      =>   l_lines_accts_rec.service_package_id,
2399 	p_current_stage_seq       =>   l_lines_accts_rec.current_stage_seq,
2400 	p_annual_fte              =>   l_lines_accts_rec.annual_fte
2401       ) ;
2402       --
2403       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2404 	RAISE FND_API.G_EXC_ERROR ;
2405       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2406 	RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
2407       END IF;
2408       --
2409     END IF ;  -- For l_ws_account_lines_csr%NOTFOUND.
2410     --
2411     CLOSE l_ws_account_lines_csr ;
2412     --
2413   END LOOP ;
2414 
2415 
2416   --
2417   -- Merging position related information.
2418   --
2419   IF l_source_budget_by_position = 'Y' THEN
2420 
2421     FOR l_lines_pos_rec IN
2422     (
2423        SELECT positions.*
2424        FROM   psb_ws_lines_positions   lines ,
2425 	      psb_ws_position_lines    positions
2426        WHERE  lines.worksheet_id     = p_source_worksheet_id
2427        AND    lines.position_line_id = positions.position_line_id
2428 
2429     )
2430     LOOP
2431 
2432       --
2433       -- Finding p_target_worksheet_id in psb_ws_position_lines.
2434       --
2435       OPEN l_ws_position_lines_csr( l_lines_pos_rec.copy_of_position_line_id );
2436 
2437       FETCH l_ws_position_lines_csr INTO l_target_position_line_id ,
2438 					 l_target_position_id ;
2439 
2440       IF ( l_ws_position_lines_csr%NOTFOUND ) THEN
2441 	--
2442 	-- It means a new line was created in the p_source_worksheet_id.
2443 	-- Putting this line in the p_target_worksheet_id. The API will
2444 	-- also maintain psb_ws_lines_positions.
2445 	--
2446 	PSB_WS_Pos_Pvt.Create_Position_Lines
2447 	(
2448 	   p_api_version               =>   1.0 ,
2449 	   p_init_msg_list             =>   FND_API.G_FALSE ,
2450 	   p_commit                    =>   FND_API.G_FALSE ,
2451 	   p_validation_level          =>   FND_API.G_VALID_LEVEL_FULL ,
2452 	   p_return_status             =>   l_return_status ,
2453 	   p_msg_count                 =>   l_msg_count ,
2454 	   p_msg_data                  =>   l_msg_data ,
2455 	   --
2456 	   p_position_line_id          =>   l_new_position_line_id ,
2457 	   p_worksheet_id              =>   p_target_worksheet_id ,
2458 	   p_position_id               =>   l_lines_pos_rec.position_id ,
2459 	   p_budget_group_id           =>   l_lines_pos_rec.budget_group_id ,
2460 	   p_copy_of_position_line_id  =>   NULL
2461 	);
2462 	--
2463 	IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2464 	  RAISE FND_API.G_EXC_ERROR ;
2465 	ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2466 	  RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
2467 	END IF;
2468 
2469 	--
2470 	-- Create new account distribution for the new position_line_id by
2471 	-- using original position_line_id related information. Note in the
2472 	-- account phase, we did not consider position related account lines.
2473 	--
2474 	FOR l_accts_rec IN
2475 	(
2476 	   SELECT account_line_id
2477 	   FROM   psb_ws_account_lines
2478 	   WHERE  position_line_id  = l_lines_pos_rec.position_line_id
2479 	)
2480 	LOOP
2481 	  --
2482 	  PSB_WS_Ops_Pvt.Create_Local_Dist_Pvt
2483 	  (
2484 	     p_account_line_id      =>   l_accts_rec.account_line_id  ,
2485 	     p_new_worksheet_id     =>   p_target_worksheet_id        ,
2486 	     p_new_position_line_id =>   l_new_position_line_id       ,
2487 	     p_return_status        =>   l_return_status
2488 	  ) ;
2489 	  --
2490 	  IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2491 	    RAISE FND_API.G_EXC_ERROR ;
2492 	  ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2493 	    RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
2494 	  END IF;
2495 	  --
2496 	END LOOP;
2497 
2498 /*
2499 	--
2500 	-- Update the old position_line_id with new position_line_id in
2501 	-- psb_ws_account_lines table.
2502 	--
2503 	UPDATE psb_ws_account_lines
2504 	SET    position_line_id = l_new_position_line_id
2505 	WHERE  position_line_id = l_lines_pos_rec.position_line_id
2506 	AND    account_line_id IN
2507 	       (
2508 		 SELECT account_line_id
2509 		 FROM   psb_ws_lines
2510 		 WHERE  worksheet_id = p_target_worksheet_id
2511 	       ) ;
2512 */
2513 
2514 	--
2515 	-- Add the new position line to all the worksheets up in the hierarchy.
2516 	-- ( The line has already been added in the target worksheet.)
2517 	--
2518 	PSB_WS_Ops_Pvt.Add_Worksheet_Position_Line
2519 	(
2520 	  p_api_version               => 1.0 ,
2521 	  p_init_msg_list             => FND_API.G_FALSE ,
2522 	  p_commit                    => FND_API.G_FALSE ,
2523 	  p_validation_level          => FND_API.G_VALID_LEVEL_NONE ,
2524 	  p_return_status             => l_return_status ,
2525 	  p_msg_count                 => l_msg_count ,
2526 	  p_msg_data                  => l_msg_data ,
2527 	  --
2528 	  p_worksheet_id              => p_target_worksheet_id  ,
2529 	  p_position_line_id          => l_new_position_line_id ,
2530 	  p_add_in_current_worksheet  => FND_API.G_FALSE
2531 	) ;
2532 	--
2533 	IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2534 	  RAISE FND_API.G_EXC_ERROR ;
2535 	ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2536 	  RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
2537 	END IF;
2538 
2539 
2540 	--
2541 	-- Create new records in psb_ws_fte_lines table for each occurance of
2542 	-- l_lines_pos_rec.position_line_id. The new l_new_position_line_id
2543 	-- will replace the position_line_id column in new created records.
2544 	--
2545 	FOR l_fte_rec IN
2546 	(
2547 	   SELECT *
2548 	   FROM   psb_ws_fte_lines
2549 	   WHERE  position_line_id = l_lines_pos_rec.position_line_id
2550 	)
2551 	LOOP
2552 
2553 	  --
2554 	  -- Populate the l_period_amount_tbl ( used by PSB_WS_Acct1 API )
2555 	  --
2556 	  l_period_fte_tbl(1)  := l_fte_rec.period1_fte ;
2557 	  l_period_fte_tbl(2)  := l_fte_rec.period2_fte ;
2558 	  l_period_fte_tbl(3)  := l_fte_rec.period3_fte ;
2559 	  l_period_fte_tbl(4)  := l_fte_rec.period4_fte ;
2560 	  l_period_fte_tbl(5)  := l_fte_rec.period5_fte ;
2561 	  l_period_fte_tbl(6)  := l_fte_rec.period6_fte ;
2562 	  l_period_fte_tbl(7)  := l_fte_rec.period7_fte ;
2563 	  l_period_fte_tbl(8)  := l_fte_rec.period8_fte ;
2564 	  l_period_fte_tbl(9)  := l_fte_rec.period9_fte ;
2565 	  l_period_fte_tbl(10) := l_fte_rec.period10_fte ;
2566 	  l_period_fte_tbl(11) := l_fte_rec.period11_fte ;
2567 	  l_period_fte_tbl(12) := l_fte_rec.period12_fte ;
2568 	  l_period_fte_tbl(13) := l_fte_rec.period13_fte ;
2569 	  l_period_fte_tbl(14) := l_fte_rec.period14_fte ;
2570 	  l_period_fte_tbl(15) := l_fte_rec.period15_fte ;
2571 	  l_period_fte_tbl(16) := l_fte_rec.period16_fte ;
2572 	  l_period_fte_tbl(17) := l_fte_rec.period17_fte ;
2573 	  l_period_fte_tbl(18) := l_fte_rec.period18_fte ;
2574 	  l_period_fte_tbl(19) := l_fte_rec.period19_fte ;
2575 	  l_period_fte_tbl(20) := l_fte_rec.period20_fte ;
2576 	  l_period_fte_tbl(21) := l_fte_rec.period21_fte ;
2577 	  l_period_fte_tbl(22) := l_fte_rec.period22_fte ;
2578 	  l_period_fte_tbl(23) := l_fte_rec.period23_fte ;
2579 	  l_period_fte_tbl(24) := l_fte_rec.period24_fte ;
2580 	  l_period_fte_tbl(25) := l_fte_rec.period25_fte ;
2581 	  l_period_fte_tbl(26) := l_fte_rec.period26_fte ;
2582 	  l_period_fte_tbl(27) := l_fte_rec.period27_fte ;
2583 	  l_period_fte_tbl(28) := l_fte_rec.period28_fte ;
2584 	  l_period_fte_tbl(29) := l_fte_rec.period29_fte ;
2585 	  l_period_fte_tbl(30) := l_fte_rec.period30_fte ;
2586 	  l_period_fte_tbl(31) := l_fte_rec.period31_fte ;
2587 	  l_period_fte_tbl(32) := l_fte_rec.period32_fte ;
2588 	  l_period_fte_tbl(33) := l_fte_rec.period33_fte ;
2589 	  l_period_fte_tbl(34) := l_fte_rec.period34_fte ;
2590 	  l_period_fte_tbl(35) := l_fte_rec.period35_fte ;
2591 	  l_period_fte_tbl(36) := l_fte_rec.period36_fte ;
2592 	  l_period_fte_tbl(37) := l_fte_rec.period37_fte ;
2593 	  l_period_fte_tbl(38) := l_fte_rec.period38_fte ;
2594 	  l_period_fte_tbl(39) := l_fte_rec.period39_fte ;
2595 	  l_period_fte_tbl(40) := l_fte_rec.period40_fte ;
2596 	  l_period_fte_tbl(41) := l_fte_rec.period41_fte ;
2597 	  l_period_fte_tbl(42) := l_fte_rec.period42_fte ;
2598 	  l_period_fte_tbl(43) := l_fte_rec.period43_fte ;
2599 	  l_period_fte_tbl(44) := l_fte_rec.period44_fte ;
2600 	  l_period_fte_tbl(45) := l_fte_rec.period45_fte ;
2601 	  l_period_fte_tbl(46) := l_fte_rec.period46_fte ;
2602 	  l_period_fte_tbl(47) := l_fte_rec.period47_fte ;
2603 	  l_period_fte_tbl(48) := l_fte_rec.period48_fte ;
2604 	  l_period_fte_tbl(49) := l_fte_rec.period49_fte ;
2605 	  l_period_fte_tbl(50) := l_fte_rec.period50_fte ;
2606 	  l_period_fte_tbl(51) := l_fte_rec.period51_fte ;
2607 	  l_period_fte_tbl(52) := l_fte_rec.period52_fte ;
2608 	  l_period_fte_tbl(53) := l_fte_rec.period53_fte ;
2609 	  l_period_fte_tbl(54) := l_fte_rec.period54_fte ;
2610 	  l_period_fte_tbl(55) := l_fte_rec.period55_fte ;
2611 	  l_period_fte_tbl(56) := l_fte_rec.period56_fte ;
2612 	  l_period_fte_tbl(57) := l_fte_rec.period57_fte ;
2613 	  l_period_fte_tbl(58) := l_fte_rec.period58_fte ;
2614 	  l_period_fte_tbl(59) := l_fte_rec.period59_fte ;
2615 	  l_period_fte_tbl(60) := l_fte_rec.period60_fte ;
2616 
2617 	  -- API to create new fte lines in psb_ws_fte_lines.
2618 	  PSB_WS_Pos_Pvt.Create_FTE_Lines
2619 	  (
2620 	     p_api_version              =>   1.0 ,
2621 	     p_init_msg_list            =>   FND_API.G_FALSE ,
2622 	     p_commit                   =>   FND_API.G_FALSE ,
2623 	     p_validation_level         =>   FND_API.G_VALID_LEVEL_FULL ,
2624 	     p_return_status            =>   l_return_status ,
2625 	     p_msg_count                =>   l_msg_count ,
2626 	     p_msg_data                 =>   l_msg_data ,
2627 	     --
2628 	     p_fte_line_id              =>   l_new_fte_line_id ,
2629 	     p_check_spfl_exists        =>   FND_API.G_FALSE,
2630 	     p_worksheet_id             =>   p_target_worksheet_id ,
2631 	     p_position_line_id         =>   l_new_position_line_id ,
2632 	     p_budget_year_id           =>   l_fte_rec.budget_year_id ,
2633 	     p_annual_fte               =>   l_fte_rec.annual_fte ,
2634 	     p_service_package_id       =>   l_fte_rec.service_package_id ,
2635 	     p_stage_set_id             =>   l_fte_rec.stage_set_id ,
2636 	     p_start_stage_seq          =>   l_fte_rec.start_stage_seq ,
2637 	     p_current_stage_seq        =>   l_fte_rec.current_stage_seq ,
2638 	     p_end_stage_seq            =>   NVL( l_fte_rec.end_stage_seq ,
2639 						  FND_API.G_MISS_NUM),
2640 	     p_period_fte               =>   l_period_fte_tbl
2641 	  );
2642 	  --
2643 	  IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2644 	    RAISE FND_API.G_EXC_ERROR ;
2645 	  ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2646 	    RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
2647 	  END IF;
2648 	  --
2649 	END LOOP;   -- To process fte_lines in psb_ws_element_lines
2650 
2651 
2652 	--
2653 	-- Create new records in psb_ws_element_lines table for each occurance
2654 	-- of l_lines_pos_rec.position_line_id. The new l_new_position_line_id
2655 	-- will replace the position_line_id column in new created records.
2656 	--
2657 	FOR l_element_rec IN
2658 	(
2659 	   SELECT *
2660 	   FROM   psb_ws_element_lines
2661 	   WHERE  position_line_id = l_lines_pos_rec.position_line_id
2662 	)
2663 	LOOP
2664 
2665 	  -- API to create new element lines in psb_ws_element_lines.
2666 	  PSB_WS_Pos_Pvt.Create_Element_Lines
2667 	  (
2668 	     p_api_version              =>   1.0 ,
2669 	     p_init_msg_list            =>   FND_API.G_FALSE ,
2670 	     p_commit                   =>   FND_API.G_FALSE ,
2671 	     p_validation_level         =>   FND_API.G_VALID_LEVEL_FULL ,
2672 	     p_return_status            =>   l_return_status ,
2673 	     p_msg_count                =>   l_msg_count ,
2674 	     p_msg_data                 =>   l_msg_data ,
2675 	     --
2676 	     p_element_line_id          =>   l_new_element_line_id ,
2677 	     p_position_line_id         =>   l_new_position_line_id ,
2678 	     p_budget_year_id           =>   l_element_rec.budget_year_id ,
2679 	     p_pay_element_id           =>   l_element_rec.pay_element_id ,
2680 	     p_currency_code            =>   l_element_rec.currency_code ,
2681 	     p_element_cost             =>   l_element_rec.element_cost ,
2682 	     p_element_set_id           =>   l_element_rec.element_set_id ,
2683 	     p_service_package_id       =>   l_element_rec.service_package_id ,
2684 	     p_stage_set_id             =>   l_element_rec.stage_set_id ,
2685 	     p_start_stage_seq          =>   l_element_rec.start_stage_seq ,
2686 	     p_current_stage_seq        =>   l_element_rec.current_stage_seq,
2687 	     p_end_stage_seq            =>   NVL( l_element_rec.end_stage_seq ,
2688 						  FND_API.G_MISS_NUM )
2689 	  );
2690 
2691 	  --
2692 	  IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2693 	    RAISE FND_API.G_EXC_ERROR ;
2694 	  ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2695 	    RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
2696 	  END IF;
2697 	  --
2698 	END LOOP;   -- To process element_lines in psb_ws_element_lines.
2699 
2700 	--
2701 	-- Create new records in psb_position_assignments for each occurance
2702 	-- of l_lines_pos_rec.position_id. The global_worksheet_id
2703 	-- will replace the worksheet_id column in new created records.
2704 	--
2705 	FOR l_asgn_rec IN
2706 	(
2707 	   SELECT *
2708 	   FROM   psb_position_assignments
2709 	   WHERE  position_id  = l_lines_pos_rec.position_id
2710 	   AND    worksheet_id = p_source_worksheet_id
2711 	)
2712 	LOOP
2713 
2714 	  --
2715 	  -- API will create a new position assignments.
2716 	  --
2717 	  PSB_Positions_Pvt.Modify_Assignment
2718 	  (
2719 	     p_api_version                 => 1.0 ,
2720 	     p_init_msg_list               => FND_API.G_FALSE ,
2721 	     p_commit                      => FND_API.G_FALSE ,
2722 	     p_validation_level            => FND_API.G_VALID_LEVEL_NONE ,
2723 	     p_return_status               => l_return_status ,
2724 	     p_msg_count                   => l_msg_count ,
2725 	     p_msg_data                    => l_msg_data ,
2726 	     --
2727 	     p_position_assignment_id      => l_new_position_assignment_id ,
2728 	     p_element_value_type          => l_asgn_rec.element_value_type ,
2729 	     p_data_extract_id             => l_asgn_rec.data_extract_id ,
2730 	     p_worksheet_id                => l_global_worksheet_id ,
2731 	     p_position_id                 => l_asgn_rec.position_id ,
2732 	     p_assignment_type             => l_asgn_rec.assignment_type ,
2733 	     p_attribute_id                => l_asgn_rec.attribute_id ,
2734 	     p_attribute_value_id          => l_asgn_rec.attribute_value_id ,
2735 	     p_attribute_value             => l_asgn_rec.attribute_value ,
2736 	     p_pay_element_id              => l_asgn_rec.pay_element_id ,
2737 	     p_pay_element_option_id       => l_asgn_rec.pay_element_option_id ,
2738 	     p_effective_start_date        => l_asgn_rec.effective_start_date ,
2739 	     p_effective_end_date          => l_asgn_rec.effective_end_date ,
2740 	     p_element_value               => l_asgn_rec.element_value ,
2741 	     p_global_default_flag         => l_asgn_rec.global_default_flag ,
2742 	     p_assignment_default_rule_id  =>
2743 				       l_asgn_rec.assignment_default_rule_id ,
2744 	     p_modify_flag                 => l_asgn_rec.modify_flag ,
2745 	     p_rowid                       => l_rowid ,
2746 	     p_currency_code               => l_asgn_rec.currency_code ,
2747 	     p_pay_basis                   => l_asgn_rec.pay_basis ,
2748 	     p_employee_id                 => l_asgn_rec.employee_id ,
2749 	     p_primary_employee_flag       => l_asgn_rec.primary_employee_flag ,
2750 	     p_mode                        => 'R'
2751 	  ) ;
2752 	  --
2753 	  IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2754 	    RAISE FND_API.G_EXC_ERROR ;
2755 	  ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2756 	    RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
2757 	  END IF;
2758 	  --
2759 	END LOOP ;
2760 
2761       ELSIF ( l_ws_position_lines_csr%FOUND ) THEN
2762 
2763 	--
2764 	-- It means it is an old copied line in the source(local) worksheet,
2765 	-- we need to overwrite information from the local worksheet.
2766 	--
2767 
2768 	--
2769 	-- Update the old position_line_id with new position_line_id in
2770 	-- psb_ws_account_lines table.
2771 	--
2772 	/*For Bug No : 2534088 Start*/
2773 	--commented the following code as the complete code to be implemented
2774 	--for inserting/updating the account lines for target worksheet
2775 
2776 	/*
2777 	UPDATE psb_ws_account_lines
2778 	SET    position_line_id = l_target_position_line_id
2779 	WHERE  position_line_id = l_lines_pos_rec.position_line_id
2780 	AND    account_line_id IN
2781 	       (
2782 		 SELECT account_line_id
2783 		 FROM   psb_ws_lines
2784 		 WHERE  worksheet_id = p_target_worksheet_id
2785 	       ) ;
2786 	*/
2787 
2788 	FOR l_lines_accts_rec IN
2789 	(
2790 	     SELECT accts.*
2791 	     FROM   psb_ws_lines          lines ,
2792 		    psb_ws_account_lines  accts
2793 	     WHERE  lines.worksheet_id    = p_source_worksheet_id
2794 	     AND    lines.account_line_id = accts.account_line_id
2795 	     AND    accts.position_line_id = l_lines_pos_rec.position_line_id
2796 	     AND    accts.end_stage_seq IS NULL
2797 	     AND    accts.template_id IS NULL
2798 	)
2799 	LOOP
2800 	--
2801 	-- Finding p_target_worksheet_id in psb_ws_account_lines.
2802 	--
2803 	  OPEN l_ws_account_lines_csr ( l_lines_accts_rec.copy_of_account_line_id ) ;
2804 
2805 	  FETCH l_ws_account_lines_csr INTO l_target_account_line_id;
2806 
2807 	  --
2808 	  -- Populate the l_period_amount_tbl ( used by PSB_WS_Acct1 API )
2809 	  --
2810 	  l_period_amount_tbl(1)  := l_lines_accts_rec.period1_amount ;
2811 	  l_period_amount_tbl(2)  := l_lines_accts_rec.period2_amount ;
2812 	  l_period_amount_tbl(3)  := l_lines_accts_rec.period3_amount ;
2813 	  l_period_amount_tbl(4)  := l_lines_accts_rec.period4_amount ;
2814 	  l_period_amount_tbl(5)  := l_lines_accts_rec.period5_amount ;
2815 	  l_period_amount_tbl(6)  := l_lines_accts_rec.period6_amount ;
2816 	  l_period_amount_tbl(7)  := l_lines_accts_rec.period7_amount ;
2817 	  l_period_amount_tbl(8)  := l_lines_accts_rec.period8_amount ;
2818 	  l_period_amount_tbl(9)  := l_lines_accts_rec.period9_amount ;
2819 	  l_period_amount_tbl(10) := l_lines_accts_rec.period10_amount ;
2820 	  l_period_amount_tbl(11) := l_lines_accts_rec.period11_amount ;
2821 	  l_period_amount_tbl(12) := l_lines_accts_rec.period12_amount ;
2822 	  l_period_amount_tbl(13) := l_lines_accts_rec.period13_amount ;
2823 	  l_period_amount_tbl(14) := l_lines_accts_rec.period14_amount ;
2824 	  l_period_amount_tbl(15) := l_lines_accts_rec.period15_amount ;
2825 	  l_period_amount_tbl(16) := l_lines_accts_rec.period16_amount ;
2826 	  l_period_amount_tbl(17) := l_lines_accts_rec.period17_amount ;
2827 	  l_period_amount_tbl(18) := l_lines_accts_rec.period18_amount ;
2828 	  l_period_amount_tbl(19) := l_lines_accts_rec.period19_amount ;
2829 	  l_period_amount_tbl(20) := l_lines_accts_rec.period20_amount ;
2830 	  l_period_amount_tbl(21) := l_lines_accts_rec.period21_amount ;
2831 	  l_period_amount_tbl(22) := l_lines_accts_rec.period22_amount ;
2832 	  l_period_amount_tbl(23) := l_lines_accts_rec.period23_amount ;
2833 	  l_period_amount_tbl(24) := l_lines_accts_rec.period24_amount ;
2834 	  l_period_amount_tbl(25) := l_lines_accts_rec.period25_amount ;
2835 	  l_period_amount_tbl(26) := l_lines_accts_rec.period26_amount ;
2836 	  l_period_amount_tbl(27) := l_lines_accts_rec.period27_amount ;
2837 	  l_period_amount_tbl(28) := l_lines_accts_rec.period28_amount ;
2838 	  l_period_amount_tbl(29) := l_lines_accts_rec.period29_amount ;
2839 	  l_period_amount_tbl(30) := l_lines_accts_rec.period30_amount ;
2840 	  l_period_amount_tbl(31) := l_lines_accts_rec.period31_amount ;
2841 	  l_period_amount_tbl(32) := l_lines_accts_rec.period32_amount ;
2842 	  l_period_amount_tbl(33) := l_lines_accts_rec.period33_amount ;
2843 	  l_period_amount_tbl(34) := l_lines_accts_rec.period34_amount ;
2844 	  l_period_amount_tbl(35) := l_lines_accts_rec.period35_amount ;
2845 	  l_period_amount_tbl(36) := l_lines_accts_rec.period36_amount ;
2846 	  l_period_amount_tbl(37) := l_lines_accts_rec.period37_amount ;
2847 	  l_period_amount_tbl(38) := l_lines_accts_rec.period38_amount ;
2848 	  l_period_amount_tbl(39) := l_lines_accts_rec.period39_amount ;
2849 	  l_period_amount_tbl(40) := l_lines_accts_rec.period40_amount ;
2850 	  l_period_amount_tbl(41) := l_lines_accts_rec.period41_amount ;
2851 	  l_period_amount_tbl(42) := l_lines_accts_rec.period42_amount ;
2852 	  l_period_amount_tbl(43) := l_lines_accts_rec.period43_amount ;
2853 	  l_period_amount_tbl(44) := l_lines_accts_rec.period44_amount ;
2854 	  l_period_amount_tbl(45) := l_lines_accts_rec.period45_amount ;
2855 	  l_period_amount_tbl(46) := l_lines_accts_rec.period46_amount ;
2856 	  l_period_amount_tbl(47) := l_lines_accts_rec.period47_amount ;
2857 	  l_period_amount_tbl(48) := l_lines_accts_rec.period48_amount ;
2858 	  l_period_amount_tbl(49) := l_lines_accts_rec.period49_amount ;
2859 	  l_period_amount_tbl(50) := l_lines_accts_rec.period50_amount ;
2860 	  l_period_amount_tbl(51) := l_lines_accts_rec.period51_amount ;
2861 	  l_period_amount_tbl(52) := l_lines_accts_rec.period52_amount ;
2862 	  l_period_amount_tbl(53) := l_lines_accts_rec.period53_amount ;
2863 	  l_period_amount_tbl(54) := l_lines_accts_rec.period54_amount ;
2864 	  l_period_amount_tbl(55) := l_lines_accts_rec.period55_amount ;
2865 	  l_period_amount_tbl(56) := l_lines_accts_rec.period56_amount ;
2866 	  l_period_amount_tbl(57) := l_lines_accts_rec.period57_amount ;
2867 	  l_period_amount_tbl(58) := l_lines_accts_rec.period58_amount ;
2868 	  l_period_amount_tbl(59) := l_lines_accts_rec.period59_amount ;
2869 	  l_period_amount_tbl(60) := l_lines_accts_rec.period60_amount ;
2870 
2871 	  IF ( l_ws_account_lines_csr%NOTFOUND ) THEN
2872 	  --
2873 	  -- It means a new line was created in the p_source_worksheet_id.
2874 	  -- Putting this line in the p_target_worksheet_id. The PSB_WS_Acct_Pvt
2875 	  -- API will also maintain psb_ws_lines.
2876 	  --
2877 
2878 	    PSB_WS_Acct_Pvt.Create_Account_Dist
2879 	    (
2880 	      p_api_version             =>   1.0 ,
2881 	      p_init_msg_list           =>   FND_API.G_FALSE,
2882 	      p_commit                  =>   FND_API.G_FALSE,
2883 	      p_validation_level        =>   FND_API.G_VALID_LEVEL_FULL,
2884 	      p_return_status           =>   l_return_status,
2885 	      p_account_line_id         =>   l_new_account_line_id,
2886 	      p_check_spal_exists       =>   FND_API.G_FALSE,
2887 	      p_msg_count               =>   l_msg_count,
2888 	      p_msg_data                =>   l_msg_data,
2889 	      --
2890 	      p_worksheet_id            =>   p_target_worksheet_id,
2891 	      p_budget_year_id          =>   l_lines_accts_rec.budget_year_id,
2892 	      p_budget_group_id         =>   l_lines_accts_rec.budget_group_id,
2893 	      p_ccid                    =>   l_lines_accts_rec.code_combination_id,
2894 	      p_template_id             =>   NVL(l_lines_accts_rec.template_id ,
2895 						  FND_API.G_MISS_NUM ) ,
2896 	      p_currency_code           =>   l_lines_accts_rec.currency_code ,
2897 	      p_balance_type            =>   l_lines_accts_rec.balance_type ,
2898 	      p_ytd_amount              =>   l_lines_accts_rec.ytd_amount  ,
2899 	      p_distribute_flag         =>   FND_API.G_FALSE ,
2900 	      p_annual_fte              =>   NVL ( l_lines_accts_rec.annual_fte,
2901 						  FND_API.G_MISS_NUM ) ,
2902 	      p_period_amount           =>   l_period_amount_tbl ,
2903 	      p_position_line_id        =>   l_target_position_line_id,
2904 	      p_element_set_id          =>   NVL( l_lines_accts_rec.element_set_id,
2905 						  FND_API.G_MISS_NUM ) ,
2906 	      p_salary_account_line     =>   NVL(l_lines_accts_rec.salary_account_line,
2907 						  FND_API.G_FALSE ) ,
2908 	      p_service_package_id      =>   l_lines_accts_rec.service_package_id ,
2909 	      p_start_stage_seq         =>   l_lines_accts_rec.start_stage_seq ,
2910 	      p_current_stage_seq       =>   l_lines_accts_rec.current_stage_seq ,
2911 	      p_end_stage_seq           =>   NVL( l_lines_accts_rec.end_stage_seq,
2912 						  FND_API.G_MISS_NUM )
2913 	    );
2914 	    --
2915 	    IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2916 	      RAISE FND_API.G_EXC_ERROR ;
2917 	    ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2918 	      RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
2919 	    END IF;
2920 	    --
2921 	    -- The new_account_line_id will be null if the desired account lines are
2922 	    -- already there in the target worksheet. If not null, add the new
2923 	    -- account_line_id to all the worksheet up in the hierarchy.
2924 	    --
2925 	    IF l_new_account_line_id IS NOT NULL THEN
2926 	      --
2927 	      -- Add the account_line to all the worksheets up in the hierarchy.
2928 	      -- ( The line has already been added in the target worksheet.)
2929 	      --
2930 	      PSB_WS_Ops_Pvt.Add_Worksheet_Line
2931 	      (
2932 		 p_api_version               => 1.0 ,
2933 		 p_init_msg_list             => FND_API.G_FALSE,
2934 		 p_commit                    => FND_API.G_FALSE,
2935 		 p_validation_level          => FND_API.G_VALID_LEVEL_NONE,
2936 		 p_return_status             => l_return_status,
2937 		 p_msg_count                 => l_msg_count,
2938 		 p_msg_data                  => l_msg_data ,
2939 		 --
2940 		 p_worksheet_id              => p_target_worksheet_id ,
2941 		 p_account_line_id           => l_new_account_line_id ,
2942 		 p_add_in_current_worksheet  => FND_API.G_FALSE
2943 	      ) ;
2944 	      --
2945 	      IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2946 		RAISE FND_API.G_EXC_ERROR ;
2947 	      ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2948 		RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
2949 	      END IF;
2950 	      --
2951 	    END IF;
2952 	  --
2953 	  ELSIF ( l_ws_account_lines_csr%FOUND ) THEN
2954 	    --
2955 	    -- It means it is not a new line, but the amount information may be
2956 	    -- different in the p_source_worksheet_id.
2957 	    --
2958 	      -- Updating ytd_amount in the p_target_worksheet_id.
2959 	    --
2960 
2961 	    PSB_WS_Acct_Pvt.Create_Account_Dist
2962 	     (
2963 		p_api_version             =>   1.0 ,
2964 		p_init_msg_list           =>   FND_API.G_FALSE,
2965 		p_commit                  =>   FND_API.G_FALSE,
2966 		p_validation_level        =>   FND_API.G_VALID_LEVEL_FULL,
2967 		p_return_status           =>   l_return_status,
2968 		p_msg_count               =>   l_msg_count,
2969 		p_msg_data                =>   l_msg_data,
2970                 -- comment out the following line for bug 3419241
2971                 -- p_check_stages must be set to FND_API.G_TRUE to
2972                 -- automatically create new Stage for the Account Line.
2973                 -- p_check_stages is FND_API.G_TRUE by default.
2974 		--p_check_stages            =>   FND_API.G_FALSE,
2975 		p_worksheet_id            =>   p_target_worksheet_id,
2976 		p_account_line_id         =>   l_target_account_line_id,
2977 		p_ytd_amount              =>   l_lines_accts_rec.ytd_amount,
2978 		p_period_amount           =>   l_period_amount_tbl,
2979 		p_service_package_id      =>   l_lines_accts_rec.service_package_id,
2980 		p_current_stage_seq       =>   l_lines_accts_rec.current_stage_seq,
2981 		p_annual_fte              =>   l_lines_accts_rec.annual_fte
2982 	      ) ;
2983 
2984 	      IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2985 		RAISE FND_API.G_EXC_ERROR ;
2986 	      ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2987 		RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
2988 	      END IF;
2989 	  --
2990 	  END IF ;  -- For l_ws_account_lines_csr%NOTFOUND.
2991 	--
2992 	CLOSE l_ws_account_lines_csr ;
2993 	--
2994 	END LOOP ;
2995 	/*For Bug No : 2534088 End*/
2996 
2997 	--
2998 	-- Wipe out records in psb_ws_fte_lines related to the original line
2999 	-- l_target_position_line_id and create new records from the local
3000 	-- l_lines_pos_rec.position_line_id . The l_target_position_line_id
3001 	-- will replace the position_line_id column in new created records.
3002 	--
3003 
3004 	DELETE psb_ws_fte_lines
3005 	WHERE  position_line_id = l_target_position_line_id ;
3006 
3007 	FOR l_fte_rec IN
3008 	(
3009 	   SELECT *
3010 	   FROM   psb_ws_fte_lines
3011 	   WHERE  position_line_id = l_lines_pos_rec.position_line_id
3012 	)
3013 	LOOP
3014 
3015 	  --
3016 	  -- Populate the l_period_amount_tbl ( used by PSB_WS_Acct1 API )
3017 	  --
3018 	  l_period_fte_tbl(1)  := l_fte_rec.period1_fte ;
3019 	  l_period_fte_tbl(2)  := l_fte_rec.period2_fte ;
3020 	  l_period_fte_tbl(3)  := l_fte_rec.period3_fte ;
3021 	  l_period_fte_tbl(4)  := l_fte_rec.period4_fte ;
3022 	  l_period_fte_tbl(5)  := l_fte_rec.period5_fte ;
3023 	  l_period_fte_tbl(6)  := l_fte_rec.period6_fte ;
3024 	  l_period_fte_tbl(7)  := l_fte_rec.period7_fte ;
3025 	  l_period_fte_tbl(8)  := l_fte_rec.period8_fte ;
3026 	  l_period_fte_tbl(9)  := l_fte_rec.period9_fte ;
3027 	  l_period_fte_tbl(10) := l_fte_rec.period10_fte ;
3028 	  l_period_fte_tbl(11) := l_fte_rec.period11_fte ;
3029 	  l_period_fte_tbl(12) := l_fte_rec.period12_fte ;
3030 	  l_period_fte_tbl(13) := l_fte_rec.period13_fte ;
3031 	  l_period_fte_tbl(14) := l_fte_rec.period14_fte ;
3032 	  l_period_fte_tbl(15) := l_fte_rec.period15_fte ;
3033 	  l_period_fte_tbl(16) := l_fte_rec.period16_fte ;
3034 	  l_period_fte_tbl(17) := l_fte_rec.period17_fte ;
3035 	  l_period_fte_tbl(18) := l_fte_rec.period18_fte ;
3036 	  l_period_fte_tbl(19) := l_fte_rec.period19_fte ;
3037 	  l_period_fte_tbl(20) := l_fte_rec.period20_fte ;
3038 	  l_period_fte_tbl(21) := l_fte_rec.period21_fte ;
3039 	  l_period_fte_tbl(22) := l_fte_rec.period22_fte ;
3040 	  l_period_fte_tbl(23) := l_fte_rec.period23_fte ;
3041 	  l_period_fte_tbl(24) := l_fte_rec.period24_fte ;
3042 	  l_period_fte_tbl(25) := l_fte_rec.period25_fte ;
3043 	  l_period_fte_tbl(26) := l_fte_rec.period26_fte ;
3044 	  l_period_fte_tbl(27) := l_fte_rec.period27_fte ;
3045 	  l_period_fte_tbl(28) := l_fte_rec.period28_fte ;
3046 	  l_period_fte_tbl(29) := l_fte_rec.period29_fte ;
3047 	  l_period_fte_tbl(30) := l_fte_rec.period30_fte ;
3048 	  l_period_fte_tbl(31) := l_fte_rec.period31_fte ;
3049 	  l_period_fte_tbl(32) := l_fte_rec.period32_fte ;
3050 	  l_period_fte_tbl(33) := l_fte_rec.period33_fte ;
3051 	  l_period_fte_tbl(34) := l_fte_rec.period34_fte ;
3052 	  l_period_fte_tbl(35) := l_fte_rec.period35_fte ;
3053 	  l_period_fte_tbl(36) := l_fte_rec.period36_fte ;
3054 	  l_period_fte_tbl(37) := l_fte_rec.period37_fte ;
3055 	  l_period_fte_tbl(38) := l_fte_rec.period38_fte ;
3056 	  l_period_fte_tbl(39) := l_fte_rec.period39_fte ;
3057 	  l_period_fte_tbl(40) := l_fte_rec.period40_fte ;
3058 	  l_period_fte_tbl(41) := l_fte_rec.period41_fte ;
3059 	  l_period_fte_tbl(42) := l_fte_rec.period42_fte ;
3060 	  l_period_fte_tbl(43) := l_fte_rec.period43_fte ;
3061 	  l_period_fte_tbl(44) := l_fte_rec.period44_fte ;
3062 	  l_period_fte_tbl(45) := l_fte_rec.period45_fte ;
3063 	  l_period_fte_tbl(46) := l_fte_rec.period46_fte ;
3064 	  l_period_fte_tbl(47) := l_fte_rec.period47_fte ;
3065 	  l_period_fte_tbl(48) := l_fte_rec.period48_fte ;
3066 	  l_period_fte_tbl(49) := l_fte_rec.period49_fte ;
3067 	  l_period_fte_tbl(50) := l_fte_rec.period50_fte ;
3068 	  l_period_fte_tbl(51) := l_fte_rec.period51_fte ;
3069 	  l_period_fte_tbl(52) := l_fte_rec.period52_fte ;
3070 	  l_period_fte_tbl(53) := l_fte_rec.period53_fte ;
3071 	  l_period_fte_tbl(54) := l_fte_rec.period54_fte ;
3072 	  l_period_fte_tbl(55) := l_fte_rec.period55_fte ;
3073 	  l_period_fte_tbl(56) := l_fte_rec.period56_fte ;
3074 	  l_period_fte_tbl(57) := l_fte_rec.period57_fte ;
3075 	  l_period_fte_tbl(58) := l_fte_rec.period58_fte ;
3076 	  l_period_fte_tbl(59) := l_fte_rec.period59_fte ;
3077 	  l_period_fte_tbl(60) := l_fte_rec.period60_fte ;
3078 
3079 	  -- API to create new fte lines in psb_ws_fte_lines.
3080 	  PSB_WS_Pos_Pvt.Create_FTE_Lines
3081 	  (
3082 	     p_api_version              =>   1.0 ,
3083 	     p_init_msg_list            =>   FND_API.G_FALSE ,
3084 	     p_commit                   =>   FND_API.G_FALSE ,
3085 	     p_validation_level         =>   FND_API.G_VALID_LEVEL_FULL ,
3086 	     p_return_status            =>   l_return_status ,
3087 	     p_msg_count                =>   l_msg_count ,
3088 	     p_msg_data                 =>   l_msg_data ,
3089 	     --
3090 	     p_fte_line_id              =>   l_new_fte_line_id ,
3091 	     p_check_spfl_exists        =>   FND_API.G_FALSE,
3092 	     p_worksheet_id             =>   p_target_worksheet_id ,
3093 	     p_position_line_id         =>   l_target_position_line_id ,
3094 	     p_budget_year_id           =>   l_fte_rec.budget_year_id ,
3095 	     p_annual_fte               =>   l_fte_rec.annual_fte ,
3096 	     p_service_package_id       =>   l_fte_rec.service_package_id ,
3097 	     p_stage_set_id             =>   l_fte_rec.stage_set_id ,
3098 	     p_start_stage_seq          =>   l_fte_rec.start_stage_seq ,
3099 	     p_current_stage_seq        =>   l_fte_rec.current_stage_seq  ,
3100 	     p_end_stage_seq            =>   NVL( l_fte_rec.end_stage_seq ,
3101 						  FND_API.G_MISS_NUM ),
3102 	     p_period_fte               =>   l_period_fte_tbl
3103 	  );
3104 
3105 	  --
3106 	  IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3107 	    RAISE FND_API.G_EXC_ERROR ;
3108 	  ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3109 	    RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
3110 	  END IF;
3111 	  --
3112 
3113 	END LOOP;   -- To process fte_lines in psb_ws_element_lines.
3114 
3115 
3116 	--
3117 	-- Wipe out records in psb_ws_element_lines related to the original line
3118 	-- l_target_position_line_id and create new records from the local
3119 	-- l_lines_pos_rec.position_line_id . The l_target_position_line_id
3120 	-- will replace the position_line_id column in new created records.
3121 	--
3122 
3123 	DELETE psb_ws_element_lines
3124 	WHERE  position_line_id = l_target_position_line_id ;
3125 
3126 	FOR l_element_rec IN
3127 	(
3128 	   SELECT *
3129 	   FROM   psb_ws_element_lines
3130 	   WHERE  position_line_id = l_lines_pos_rec.position_line_id
3131 	)
3132 	LOOP
3133 
3134 	  -- API to create new element lines in psb_ws_element_lines.
3135 	  PSB_WS_Pos_Pvt.Create_Element_Lines
3136 	  (
3137 	     p_api_version              =>   1.0 ,
3138 	     p_init_msg_list            =>   FND_API.G_FALSE ,
3139 	     p_commit                   =>   FND_API.G_FALSE ,
3140 	     p_validation_level         =>   FND_API.G_VALID_LEVEL_FULL ,
3141 	     p_return_status            =>   l_return_status ,
3142 	     p_msg_count                =>   l_msg_count ,
3143 	     p_msg_data                 =>   l_msg_data ,
3144 	     --
3145 	     p_element_line_id          =>   l_new_element_line_id ,
3146 	     p_position_line_id         =>   l_target_position_line_id ,
3147 	     p_budget_year_id           =>   l_element_rec.budget_year_id ,
3148 	     p_pay_element_id           =>   l_element_rec.pay_element_id ,
3149 	     p_currency_code            =>   l_element_rec.currency_code ,
3150 	     p_element_cost             =>   l_element_rec.element_cost ,
3151 	     p_element_set_id           =>   l_element_rec.element_set_id ,
3152 	     p_service_package_id       =>   l_element_rec.service_package_id ,
3153 	     p_stage_set_id             =>   l_element_rec.stage_set_id ,
3154 	     p_start_stage_seq          =>   l_element_rec.start_stage_seq ,
3155 	     p_current_stage_seq        =>   l_element_rec.current_stage_seq,
3156 	     p_end_stage_seq            =>   NVL( l_element_rec.end_stage_seq,
3157 						  FND_API.G_MISS_NUM )
3158 	  );
3159 
3160 	  --
3161 	  IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3162 	    RAISE FND_API.G_EXC_ERROR ;
3163 	  ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3164 	    RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
3165 	  END IF;
3166 	  --
3167 	END LOOP;   -- To process element_lines in psb_ws_element_lines.
3168 
3169 	--
3170 	-- Wipe out records in psb_position_assignments related to the original
3171 	-- l_target_position_id and create new records from the
3172 	-- local l_lines_pos_rec.position_id. The global_worksheet_id will
3173 	-- replace the worksheet_id column in new created records.
3174 	--
3175 	DELETE psb_position_assignments
3176 	WHERE  position_id  = l_target_position_id
3177 	AND    worksheet_id = l_global_worksheet_id ;
3178 
3179 	--
3180 	-- Create new records in psb_position_assignments for each occurance
3181 	-- of l_lines_pos_rec.position_id. The global_worksheet_id
3182 	-- will replace the worksheet_id column in new created records.
3183 	--
3184 	FOR l_asgn_rec IN
3185 	(
3186 	   SELECT *
3187 	   FROM   psb_position_assignments
3188 	   WHERE  position_id  = l_lines_pos_rec.position_id
3189 	   AND    worksheet_id = p_source_worksheet_id
3190 	)
3191 	LOOP
3192 
3193 	  --
3194 	  -- API will create a new position assignments.
3195 	  --
3196 	  PSB_Positions_Pvt.Modify_Assignment
3197 	  (
3198 	     p_api_version                 => 1.0 ,
3199 	     p_init_msg_list               => FND_API.G_FALSE ,
3200 	     p_commit                      => FND_API.G_FALSE ,
3201 	     p_validation_level            => FND_API.G_VALID_LEVEL_NONE ,
3202 	     p_return_status               => l_return_status ,
3203 	     p_msg_count                   => l_msg_count ,
3204 	     p_msg_data                    => l_msg_data ,
3205 	     --
3206 	     p_position_assignment_id      => l_new_position_assignment_id ,
3207 	     p_element_value_type          => l_asgn_rec.element_value_type ,
3208 	     p_data_extract_id             => l_asgn_rec.data_extract_id ,
3209 	     p_worksheet_id                => l_global_worksheet_id ,
3210 	     p_position_id                 => l_asgn_rec.position_id ,
3211 	     p_assignment_type             => l_asgn_rec.assignment_type ,
3212 	     p_attribute_id                => l_asgn_rec.attribute_id ,
3213 	     p_attribute_value_id          => l_asgn_rec.attribute_value_id ,
3214 	     p_attribute_value             => l_asgn_rec.attribute_value ,
3215 	     p_pay_element_id              => l_asgn_rec.pay_element_id ,
3216 	     p_pay_element_option_id       => l_asgn_rec.pay_element_option_id ,
3217 	     p_effective_start_date        => l_asgn_rec.effective_start_date ,
3218 	     p_effective_end_date          => l_asgn_rec.effective_end_date ,
3219 	     p_element_value               => l_asgn_rec.element_value ,
3220 	     p_global_default_flag         => l_asgn_rec.global_default_flag ,
3221 	     p_assignment_default_rule_id  =>
3222 				       l_asgn_rec.assignment_default_rule_id ,
3223 	     p_modify_flag                 => l_asgn_rec.modify_flag ,
3224 	     p_rowid                       => l_rowid ,
3225 	     p_currency_code               => l_asgn_rec.currency_code ,
3226 	     p_pay_basis                   => l_asgn_rec.pay_basis ,
3227 	     p_employee_id                 => l_asgn_rec.employee_id ,
3228 	     p_primary_employee_flag       => l_asgn_rec.primary_employee_flag ,
3229 	     p_mode                        => 'R'
3230 	  ) ;
3231 	  --
3232 	  IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3233 	    RAISE FND_API.G_EXC_ERROR ;
3234 	  ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3235 	    RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
3236 	  END IF;
3237 	  --
3238 	END LOOP ;  -- To process position assignments.
3239 
3240       END IF ;  -- For l_ws_position_lines_csr%NOTFOUND
3241       --
3242       CLOSE l_ws_position_lines_csr ;
3243       --
3244     END LOOP ;
3245 
3246   END IF ;  -- l_source_budget_by_position = 'Y' THEN
3247 
3248   --
3249   IF FND_API.To_Boolean ( p_commit ) THEN
3250     COMMIT WORK;
3251   END IF;
3252   --
3253   FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
3254 			      p_data  => p_msg_data );
3255   --
3256 EXCEPTION
3257   --
3258   WHEN FND_API.G_EXC_ERROR THEN
3259     --
3260     ROLLBACK TO Merge_Worksheets_Pvt ;
3261     p_return_status := FND_API.G_RET_STS_ERROR;
3262     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
3263 				p_data  => p_msg_data );
3264   --
3265   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3266     --
3267     ROLLBACK TO Merge_Worksheets_Pvt ;
3268     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3269     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
3270 				p_data  => p_msg_data );
3271   --
3272   WHEN OTHERS THEN
3273     --
3274     IF ( l_ws_account_lines_csr%ISOPEN ) THEN
3275       CLOSE l_ws_account_lines_csr ;
3276     END IF ;
3277     --
3278     ROLLBACK TO Merge_Worksheets_Pvt ;
3279     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3280     --
3281     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
3282       FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME,
3283 				l_api_name);
3284     END if;
3285     --
3286     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
3287 				p_data  => p_msg_data );
3288      --
3289 END Merge_Worksheets;
3290 /*---------------------------------------------------------------------------*/
3291 
3292 
3293 /*===========================================================================+
3294  |                     PROCEDURE Delete_Worksheet                            |
3295  +===========================================================================*/
3296 --
3297 -- The API This API deletes a local or global worksheet.
3298 --
3299 PROCEDURE Delete_Worksheet
3300 (
3301   p_api_version               IN       NUMBER   ,
3302   p_init_msg_list             IN       VARCHAR2 := FND_API.G_FALSE ,
3303   p_commit                    IN       VARCHAR2 := FND_API.G_FALSE ,
3304   p_validation_level          IN       NUMBER   := FND_API.G_VALID_LEVEL_FULL ,
3305   p_return_status             OUT  NOCOPY      VARCHAR2 ,
3306   p_msg_count                 OUT  NOCOPY      NUMBER   ,
3307   p_msg_data                  OUT  NOCOPY      VARCHAR2 ,
3308   --
3309   p_worksheet_id              IN       psb_worksheets.worksheet_id%TYPE ,
3310   p_keep_local_copy_flag      IN       VARCHAR2 := 'N'
3311 )
3312 IS
3313   --
3314   l_api_name                CONSTANT VARCHAR2(30)   := 'Delete_Worksheet' ;
3315   l_api_version             CONSTANT NUMBER         :=  1.0 ;
3316   --
3317   l_return_status           VARCHAR2(1) ;
3318   l_msg_count               NUMBER ;
3319   l_msg_data                VARCHAR2(2000) ;
3320   --
3321   l_local_copy_flag         psb_worksheets.local_copy_flag%TYPE ;
3322   l_budget_by_position      psb_worksheets.budget_by_position%TYPE ;
3323   l_global_worksheet_flag   psb_worksheets.global_worksheet_flag%TYPE ;
3324   l_worksheets_tab          PSB_WS_Ops_Pvt.Worksheet_Tbl_Type ;
3325   --
3326 BEGIN
3327   --
3328   SAVEPOINT Delete_Worksheet ;
3329   --
3330   IF NOT FND_API.Compatible_API_Call ( l_api_version,
3331 				       p_api_version,
3332 				       l_api_name,
3333 				       G_PKG_NAME )
3334   THEN
3335     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
3336   END IF;
3337   --
3338 
3339   IF FND_API.To_Boolean ( p_init_msg_list ) THEN
3340     FND_MSG_PUB.initialize ;
3341   END IF;
3342   --
3343   p_return_status := FND_API.G_RET_STS_SUCCESS ;
3344   --
3345 
3346   SELECT NVL( global_worksheet_flag, 'N') ,
3347 	 NVL( local_copy_flag, 'N')       ,
3348 	 NVL( budget_by_position, 'N')
3349        INTO
3350 	 l_global_worksheet_flag ,
3351 	 l_local_copy_flag ,
3352 	 l_budget_by_position
3353   FROM   psb_worksheets
3354   WHERE  worksheet_id = p_worksheet_id ;
3355 
3356   --
3357   -- Take action bases on the type of the worksheet.
3358   --
3359   IF l_global_worksheet_flag = 'Y' THEN
3360     --
3361     -- ( It means it is a global worksheet.)
3362     -- Lock all the child official, review group and local worksheets.
3363     -- You have to lock local worksheets either for deletion or for updation
3364     -- depening on p_keep_local_copy_flag parameter.
3365     --
3366 
3367     -- Find all related worksheets.
3368     FOR l_worksheet_rec IN
3369     (
3370        SELECT worksheet_id
3371        FROM   psb_worksheets
3372        WHERE  global_worksheet_id               = p_worksheet_id
3373        AND    NVL( global_worksheet_flag, 'N' ) = 'N'
3374     )
3375     LOOP
3376       --
3377       PSB_Concurrency_Control_Pub.Enforce_Concurrency_Control
3378       (
3379 	 p_api_version              => 1.0 ,
3380 	 p_init_msg_list            => FND_API.G_FALSE ,
3381 	 p_validation_level         => FND_API.G_VALID_LEVEL_NONE ,
3382 	 p_return_status            => l_return_status ,
3383 	 p_msg_count                => l_msg_count ,
3384 	 p_msg_data                 => l_msg_data  ,
3385 	 --
3386 	 p_concurrency_class        => 'MAINTENANCE' ,
3387 	 p_concurrency_entity_name  => 'WORKSHEET',
3388 	 p_concurrency_entity_id    => l_worksheet_rec.worksheet_id
3389       );
3390       --
3391       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3392 	RAISE FND_API.G_EXC_ERROR ;
3393       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3394 	RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
3395       END IF;
3396       --
3397     END LOOP ;  -- Lock child official, review group and local worksheets.
3398 
3399     -- Delete all the child official worksheets.
3400     FOR l_worksheet_rec IN
3401     (
3402        SELECT worksheet_id, budget_by_position
3403        FROM   psb_worksheets
3404        WHERE  global_worksheet_id    = p_worksheet_id
3405        AND    NVL( global_worksheet_flag, 'N' ) = 'N'
3406        AND    NVL( local_copy_flag,       'N' ) = 'N'
3407     )
3408     LOOP
3409       --
3410       Delete_Worksheet_Pvt
3411       (
3412 	 p_worksheet_id        =>  l_worksheet_rec.worksheet_id       ,
3413 	 p_budget_by_position  =>  l_worksheet_rec.budget_by_position ,
3414 	 p_delete_lines_flag   =>  'N'                                ,
3415 	 p_return_status       =>  l_return_status
3416       ) ;
3417       --
3418       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3419 	RAISE FND_API.G_EXC_ERROR ;
3420       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3421 	RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
3422       END IF;
3423       --
3424     END LOOP;
3425 
3426     -- Delete or update all the child local worksheets.
3427     FOR l_worksheet_rec IN
3428     (
3429        SELECT worksheet_id, budget_by_position
3430        FROM   psb_worksheets
3431        WHERE  global_worksheet_id    = p_worksheet_id
3432        AND    NVL( global_worksheet_flag, 'N' ) = 'N'
3433        AND    NVL( local_copy_flag,       'N' ) = 'Y'
3434     )
3435     LOOP
3436       --
3437       IF p_keep_local_copy_flag = 'N' THEN
3438 	--
3439 	Delete_Worksheet_Pvt
3440 	(
3441 	   p_worksheet_id        =>  l_worksheet_rec.worksheet_id       ,
3442 	   p_budget_by_position  =>  l_worksheet_rec.budget_by_position ,
3443 	   p_delete_lines_flag   =>  'Y'                                ,
3444 	   p_return_status       =>  l_return_status
3445 	) ;
3446 	--
3447 	IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3448 	  RAISE FND_API.G_EXC_ERROR ;
3449 	ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3450 	  RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
3451 	END IF;
3452 	--
3453       ELSE
3454 	--
3455 	-- As global worksheet is being deleted, set global_worksheet_id
3456 	-- column to null in the local worksheet.
3457 	--
3458 	PSB_Worksheet_Pvt.Update_Worksheet
3459 	(
3460 	  p_api_version           => 1.0 ,
3461 	  p_init_msg_list         => FND_API.G_FALSE ,
3462 	  p_commit                => FND_API.G_FALSE ,
3463 	  p_validation_level      => FND_API.G_VALID_LEVEL_NONE ,
3464 	  p_return_status         => l_return_status ,
3465 	  p_msg_count             => l_msg_count ,
3466 	  p_msg_data              => l_msg_data ,
3467 	  --
3468 	  p_worksheet_id          => l_worksheet_rec.worksheet_id ,
3469 	  p_global_worksheet_id   => NULL
3470 	) ;
3471 	--
3472 	IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3473 	  RAISE FND_API.G_EXC_ERROR ;
3474 	ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3475 	  RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
3476 	END IF;
3477 	--
3478       END IF ;   -- if p_keep_local_copy_flag is 'Y'.
3479       --
3480     END LOOP ;   -- Delete or update all child local worksheets.
3481     --
3482 
3483     -- Delete the global worksheet now.
3484     Delete_Worksheet_Pvt
3485     (
3486        p_worksheet_id        =>  p_worksheet_id        ,
3487        p_budget_by_position  =>  l_budget_by_position  ,
3488        p_delete_lines_flag   =>  'Y'                   ,
3489        p_return_status       =>  l_return_status
3490     ) ;
3491     --
3492     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3493       RAISE FND_API.G_EXC_ERROR ;
3494     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3495       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
3496     END IF;
3497     --
3498 
3499   ELSIF l_local_copy_flag = 'Y' THEN
3500 
3501     --
3502     -- It Means it is a local worksheet.
3503     --
3504     Delete_Worksheet_Pvt
3505     (
3506        p_worksheet_id        =>  p_worksheet_id         ,
3507        p_budget_by_position  =>  l_budget_by_position   ,
3508        p_delete_lines_flag   =>  'Y'                    ,
3509        p_return_status       =>  l_return_status
3510     ) ;
3511     --
3512     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3513       RAISE FND_API.G_EXC_ERROR ;
3514     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3515       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
3516     END IF;
3517     --
3518 
3519   ELSE
3520 
3521     --
3522     -- ( It means it is either an official or review group worksheet. )
3523     -- We need to delete all child worksheets. Also their local copies if
3524     -- p_keep_local_copy_flag parameter is 'N'.
3525     --
3526 
3527     -- Find all the child worksheets.
3528     PSB_WS_Ops_Pvt.Find_Child_Worksheets
3529     (
3530        p_api_version        =>   1.0 ,
3531        p_init_msg_list      =>   FND_API.G_FALSE,
3532        p_commit             =>   FND_API.G_FALSE,
3533        p_validation_level   =>   FND_API.G_VALID_LEVEL_FULL,
3534        p_return_status      =>   l_return_status,
3535        p_msg_count          =>   l_msg_count,
3536        p_msg_data           =>   l_msg_data,
3537        --
3538        p_worksheet_id       =>   p_worksheet_id,
3539        p_worksheet_tbl      =>   l_worksheets_tab
3540     );
3541     --
3542     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3543       RAISE FND_API.G_EXC_ERROR ;
3544     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3545       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
3546     END IF;
3547     --
3548 
3549     -- Adding the current worksheet in the table as it has to go through
3550     -- the same processing
3551     l_worksheets_tab(0) := p_worksheet_id ;
3552 
3553     --
3554     -- Process the current and all the child worksheets for locking.
3555     -- (Use 0 and COUNT-1 now).
3556     --
3557     FOR i IN 0..l_worksheets_tab.COUNT-1
3558     LOOP
3559 
3560       -- Lock the current worksheet.
3561       PSB_Concurrency_Control_Pub.Enforce_Concurrency_Control
3562       (
3563 	 p_api_version              => 1.0 ,
3564 	 p_init_msg_list            => FND_API.G_FALSE ,
3565 	 p_validation_level         => FND_API.G_VALID_LEVEL_NONE ,
3566 	 p_return_status            => l_return_status ,
3567 	 p_msg_count                => l_msg_count ,
3568 	 p_msg_data                 => l_msg_data  ,
3569 	 --
3570 	 p_concurrency_class        => 'MAINTENANCE' ,
3571 	 p_concurrency_entity_name  => 'WORKSHEET',
3572 	 p_concurrency_entity_id    => l_worksheets_tab(i)
3573       );
3574       --
3575       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3576 	RAISE FND_API.G_EXC_ERROR ;
3577       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3578 	RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
3579       END IF;
3580       --
3581 
3582       -- Lock local copies of the current worksheet as per the parameter.
3583       IF p_keep_local_copy_flag = 'N' THEN
3584 
3585 	FOR l_local_ws_rec IN
3586 	(
3587 	   SELECT worksheet_id
3588 	   FROM   psb_worksheets
3589 	   WHERE  copy_of_worksheet_id = l_worksheets_tab(i)
3590 	)
3591 	LOOP
3592 
3593 	  -- Lock the current worksheet.
3594 	  PSB_Concurrency_Control_Pub.Enforce_Concurrency_Control
3595 	  (
3596 	     p_api_version              => 1.0 ,
3597 	     p_init_msg_list            => FND_API.G_FALSE ,
3598 	     p_validation_level         => FND_API.G_VALID_LEVEL_NONE ,
3599 	     p_return_status            => l_return_status ,
3600 	     p_msg_count                => l_msg_count ,
3601 	     p_msg_data                 => l_msg_data  ,
3602 	     --
3603 	     p_concurrency_class        => 'MAINTENANCE' ,
3604 	     p_concurrency_entity_name  => 'WORKSHEET',
3605 	     p_concurrency_entity_id    => l_local_ws_rec.worksheet_id
3606 	  );
3607 	  --
3608 	  IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3609 	    RAISE FND_API.G_EXC_ERROR ;
3610 	  ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3611 	    RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
3612 	  END IF;
3613 	  --
3614 
3615 	END LOOP ;
3616 
3617       END IF; -- FOR p_keep_local_copy_flag = 'N' clause.
3618 
3619     END LOOP; -- For locking phase.
3620 
3621     --
3622     -- Process the current and all the child worksheets for deletion.
3623     -- (Use 0 and COUNT-1 now).
3624     --
3625     FOR i IN 0..l_worksheets_tab.COUNT-1
3626     LOOP
3627 
3628       -- Delete the current worksheet.
3629       Delete_Worksheet_Pvt
3630       (
3631 	 p_worksheet_id        =>  l_worksheets_tab(i)      ,
3632 	 p_budget_by_position  =>  l_budget_by_position     ,
3633 	 p_delete_lines_flag   =>  'N'                      ,
3634 	 p_return_status       =>  l_return_status
3635       ) ;
3636       --
3637       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3638 	RAISE FND_API.G_EXC_ERROR ;
3639       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3640 	RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
3641       END IF;
3642 
3643       -- Delete local copies of the current worksheet as per the parameter.
3644       IF p_keep_local_copy_flag = 'N' THEN
3645 
3646 	FOR l_local_ws_rec IN
3647 	(
3648 	   SELECT worksheet_id
3649 	   FROM   psb_worksheets
3650 	   WHERE  copy_of_worksheet_id = l_worksheets_tab(i)
3651 	)
3652 	LOOP
3653 
3654 	  Delete_Worksheet_Pvt
3655 	  (
3656 	     p_worksheet_id        =>  l_local_ws_rec.worksheet_id  ,
3657 	     p_budget_by_position  =>  l_budget_by_position         ,
3658 	     p_delete_lines_flag   =>  'Y'                          ,
3659 	     p_return_status       =>  l_return_status
3660 	  ) ;
3661 	  --
3662 	  IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3663 	    RAISE FND_API.G_EXC_ERROR ;
3664 	  ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3665 	    RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
3666 	  END IF;
3667 
3668 	END LOOP ;
3669 
3670       END IF; -- FOR p_keep_local_copy_flag = 'N' clause.
3671 
3672     END LOOP; -- For deletion phase.
3673 
3674   END IF; -- For the main IF statement.
3675 
3676   IF  FND_API.To_Boolean ( p_commit ) THEN
3677     COMMIT WORK;
3678   END IF;
3679   --
3680   FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
3681 			      p_data  => p_msg_data );
3682   --
3683 EXCEPTION
3684   --
3685   WHEN FND_API.G_EXC_ERROR THEN
3686     --
3687   /*For Bug No : 2266309 Start*/
3688     --ROLLBACK TO Delete_Worksheet ;
3689     ROLLBACK;
3690   /*For Bug No : 2266309 End*/
3691     p_return_status := FND_API.G_RET_STS_ERROR;
3692     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
3693 				p_data  => p_msg_data );
3694   --
3695   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3696     --
3697   /*For Bug No : 2266309 Start*/
3698     --ROLLBACK TO Delete_Worksheet ;
3699     ROLLBACK;
3700   /*For Bug No : 2266309 End*/
3701     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3702     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
3703 				p_data  => p_msg_data );
3704   --
3705   WHEN OTHERS THEN
3706     --
3707   /*For Bug No : 2266309 Start*/
3708     --ROLLBACK TO Delete_Worksheet ;
3709     ROLLBACK;
3710   /*For Bug No : 2266309 End*/
3711     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3712     --
3713     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
3714       FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME,
3715 				l_api_name);
3716     END if;
3717     --
3718     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
3719 				p_data  => p_msg_data );
3720      --
3721 END Delete_Worksheet ;
3722 /*---------------------------------------------------------------------------*/
3723 
3724 
3725 /*===========================================================================+
3726  |                     PROCEDURE Add_Worksheet_Line                          |
3727  +===========================================================================*/
3728 --
3729 -- This API adds a given account line to a worksheet. The addition is
3730 -- propagated to all the higher worksheets. The operation is performed only
3731 -- on the psb_ws_lines table.
3732 --
3733 PROCEDURE Add_Worksheet_Line
3734 (
3735   p_api_version               IN       NUMBER   ,
3736   p_init_msg_list             IN       VARCHAR2 := FND_API.G_FALSE ,
3737   p_commit                    IN       VARCHAR2 := FND_API.G_FALSE ,
3738   p_validation_level          IN       NUMBER   := FND_API.G_VALID_LEVEL_FULL ,
3739   p_return_status             OUT  NOCOPY      VARCHAR2 ,
3740   p_msg_count                 OUT  NOCOPY      NUMBER   ,
3741   p_msg_data                  OUT  NOCOPY      VARCHAR2 ,
3742   --
3743   p_worksheet_id              IN      psb_worksheets.worksheet_id%TYPE   ,
3744   p_account_line_id           IN      psb_ws_account_lines.account_line_id%TYPE,
3745   p_add_in_current_worksheet  IN      VARCHAR2 := FND_API.G_FALSE
3746 )
3747 IS
3748   --
3749   l_api_name                CONSTANT VARCHAR2(30)  := 'Add_Worksheet_Line' ;
3750   l_api_version             CONSTANT NUMBER        :=  1.0 ;
3751   --
3752   l_return_status           VARCHAR2(1) ;
3753   l_msg_count               NUMBER ;
3754   l_msg_data                VARCHAR2(2000) ;
3755   --
3756   CURSOR l_ws_lines_csr
3757 	 IS
3758 	 SELECT *
3759 	 FROM   psb_ws_lines
3760 	 WHERE  account_line_id = p_account_line_id ;
3761   --
3762   l_ws_lines_row_type l_ws_lines_csr%ROWTYPE ;
3763   --
3764   l_current_worksheet_id    psb_worksheets.worksheet_id%TYPE ;
3765   l_current_budget_group_id psb_worksheets.budget_group_id%TYPE ;
3766   l_budget_group_id         psb_worksheets.budget_group_id%TYPE ;
3767   l_parent_budget_group_id  psb_worksheets.budget_group_id%TYPE ;
3768   l_parent_worksheet_id     psb_worksheets.worksheet_id%TYPE ;
3769   l_distribution_id         psb_ws_distribution_details.distribution_id%TYPE ;
3770 
3771 BEGIN
3772   --
3773   SAVEPOINT Add_Worksheet_Line_Pvt ;
3774   --
3775   IF NOT FND_API.Compatible_API_Call ( l_api_version,
3776 				       p_api_version,
3777 				       l_api_name,
3778 				       G_PKG_NAME )
3779   THEN
3780     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
3781   END IF;
3782   --
3783 
3784   IF FND_API.To_Boolean ( p_init_msg_list ) THEN
3785     FND_MSG_PUB.initialize ;
3786   END IF;
3787   --
3788   p_return_status := FND_API.G_RET_STS_SUCCESS ;
3789 
3790   --
3791   -- Finding the account line information.
3792   --
3793 
3794   OPEN l_ws_lines_csr ;
3795 
3796   FETCH l_ws_lines_csr INTO l_ws_lines_row_type ;
3797 
3798   IF ( l_ws_lines_csr%NOTFOUND ) THEN
3799     --
3800     CLOSE l_ws_lines_csr ;
3801     Fnd_Message.Set_Name ('PSB', 'PSB_INVALID_ACCOUNT_LINE_ID') ;
3802     FND_MSG_PUB.Add;
3803     RAISE FND_API.G_EXC_ERROR ;
3804   END IF ;
3805 
3806   l_current_worksheet_id := p_worksheet_id ;
3807 
3808   -- Updating the current worksheet (the one which is passed).
3809   IF p_add_in_current_worksheet = FND_API.G_TRUE THEN
3810 
3811     --
3812     -- Add the new account line to the current worksheet. All we need to do
3813     -- is make an entry in the account-line matrix.
3814     --
3815     Insert_WS_Lines_Pvt
3816     (
3817       p_worksheet_id       =>  l_current_worksheet_id ,
3818       p_account_line_id    =>  p_account_line_id ,
3819       p_freeze_flag        =>  l_ws_lines_row_type.freeze_flag ,
3820       p_view_line_flag     =>  l_ws_lines_row_type.view_line_flag ,
3821       p_last_update_date   =>  g_current_date,
3822       p_last_updated_by    =>  g_current_user_id,
3823       p_last_update_login  =>  g_current_login_id,
3824       p_created_by         =>  g_current_user_id,
3825       p_creation_date      =>  g_current_date,
3826       p_return_status      =>  l_return_status
3827     ) ;
3828     --
3829     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3830       RAISE FND_API.G_EXC_ERROR ;
3831     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3832       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
3833     END IF;
3834     --
3835   END IF ;
3836 
3837   --
3838   -- Updating the top worksheets in the hierarchy.
3839   --
3840   LOOP
3841     --
3842     PSB_WS_Ops_Pvt.Find_Parent_Worksheet
3843     (
3844       p_api_version             => 1.0 ,
3845       p_init_msg_list           => FND_API.G_FALSE,
3846       p_commit                  => FND_API.G_FALSE,
3847       p_validation_level        => FND_API.G_VALID_LEVEL_NONE,
3848       p_return_status           => l_return_status,
3849       p_msg_count               => l_msg_count,
3850       p_msg_data                => l_msg_data ,
3851       --
3852       p_worksheet_id            => l_current_worksheet_id ,
3853       p_worksheet_id_OUT        => l_parent_worksheet_id
3854     ) ;
3855     --
3856     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3857       RAISE FND_API.G_EXC_ERROR ;
3858     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3859       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
3860     ELSIF l_parent_worksheet_id = 0 THEN
3861       -- It means all the top worksheets have been processed.
3862       EXIT ;
3863     END IF ;
3864 
3865     l_current_worksheet_id := l_parent_worksheet_id ;
3866 
3867     --
3868     -- Add the new account line to the current worksheet. All we need to do
3869     -- is make an entry in the account-line matrix.
3870     --
3871     Insert_WS_Lines_Pvt
3872     (
3873       p_worksheet_id       =>  l_current_worksheet_id ,
3874       p_account_line_id    =>  p_account_line_id ,
3875       p_freeze_flag        =>  l_ws_lines_row_type.freeze_flag ,
3876       p_view_line_flag     =>  l_ws_lines_row_type.view_line_flag ,
3877       p_last_update_date   =>  g_current_date,
3878       p_last_updated_by    =>  g_current_user_id,
3879       p_last_update_login  =>  g_current_login_id,
3880       p_created_by         =>  g_current_user_id,
3881       p_creation_date      =>  g_current_date,
3882       p_return_status      =>  l_return_status
3883     ) ;
3884     --
3885     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3886       RAISE FND_API.G_EXC_ERROR ;
3887     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3888       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
3889     END IF;
3890     --
3891   END LOOP;
3892   --
3893   CLOSE l_ws_lines_csr ;
3894   --
3895   IF  FND_API.To_Boolean ( p_commit ) THEN
3896     COMMIT WORK;
3897   END IF;
3898   --
3899   FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
3900 			      p_data  => p_msg_data );
3901   --
3902 EXCEPTION
3903   --
3904   WHEN FND_API.G_EXC_ERROR THEN
3905     --
3906     ROLLBACK TO Add_Worksheet_Line_Pvt ;
3907     p_return_status := FND_API.G_RET_STS_ERROR;
3908     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
3909 				p_data  => p_msg_data );
3910   --
3911   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3912     --
3913     ROLLBACK TO Add_Worksheet_Line_Pvt ;
3914     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3915     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
3916 				p_data  => p_msg_data );
3917   --
3918   WHEN OTHERS THEN
3919     --
3920     IF ( l_ws_lines_csr%ISOPEN ) THEN
3921       CLOSE l_ws_lines_csr ;
3922     END IF ;
3923     --
3924     ROLLBACK TO Add_Worksheet_Line_Pvt ;
3925     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3926     --
3927     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
3928       FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME,
3929 				l_api_name);
3930     END if;
3931     --
3932     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
3933 				p_data  => p_msg_data );
3934      --
3935 END Add_Worksheet_Line;
3936 /*---------------------------------------------------------------------------*/
3937 
3938 
3939 /*===========================================================================+
3940  |                     PROCEDURE Add_Worksheet_Line                          |
3941  +===========================================================================*/
3942 --
3943 -- This API propagates all account lines for a Position instance to all the
3944 -- higher worksheets. The operation is performed only on the psb_ws_lines table.
3945 --
3946 PROCEDURE Add_Worksheet_Line
3947 ( p_api_version       IN   NUMBER,
3948   p_init_msg_list     IN   VARCHAR2 := FND_API.G_FALSE,
3949   p_commit            IN   VARCHAR2 := FND_API.G_FALSE,
3950   p_validation_level  IN   NUMBER := FND_API.G_VALID_LEVEL_FULL,
3951   p_return_status     OUT  NOCOPY  VARCHAR2,
3952   p_msg_count         OUT  NOCOPY  NUMBER,
3953   p_msg_data          OUT  NOCOPY  VARCHAR2,
3954   p_worksheet_id      IN   NUMBER,
3955   p_position_line_id  IN   NUMBER
3956 )
3957 IS
3958 
3959   l_api_name          CONSTANT VARCHAR2(30)  := 'Add_Worksheet_Line';
3960   l_api_version       CONSTANT NUMBER        :=  1.0;
3961 
3962   l_return_status     VARCHAR2(1);
3963   l_msg_count         NUMBER;
3964   l_msg_data          VARCHAR2(2000);
3965 
3966   cursor c_WL is
3967     select b.account_line_id,
3968 	   a.view_line_flag,
3969 	   a.freeze_flag
3970       from psb_ws_lines         a,
3971 	   psb_ws_account_lines b
3972      where a.account_line_id  = b.account_line_id
3973        and a.worksheet_id     = p_worksheet_id
3974        and b.position_line_id = p_position_line_id;
3975 
3976 BEGIN
3977 
3978   SAVEPOINT Add_Worksheet_Line_Pvt;
3979 
3980   if not FND_API.Compatible_API_Call (l_api_version,
3981 				      p_api_version,
3982 				      l_api_name,
3983 				      G_PKG_NAME)
3984   then
3985     raise FND_API.G_EXC_UNEXPECTED_ERROR;
3986   end if;
3987 
3988   if FND_API.To_Boolean (p_init_msg_list) then
3989     FND_MSG_PUB.initialize;
3990   end if;
3991 
3992   --
3993   -- Loop to find out all the account_line_id(s) which are associated with
3994   -- the p_position_line_id.
3995   --
3996   for c_WL_Rec in c_WL loop
3997 
3998     --
3999     -- Each account_line_id related to the p_position_line_id needs to be
4000     -- added to all the worksheets up in the hierarchy.
4001     -- ( The line has already been added in the target worksheet.)
4002     --
4003     PSB_WS_Ops_Pvt.Add_Worksheet_Line
4004     (
4005       p_api_version               => 1.0 ,
4006       p_init_msg_list             => FND_API.G_FALSE,
4007       p_commit                    => FND_API.G_FALSE,
4008       p_validation_level          => FND_API.G_VALID_LEVEL_NONE,
4009       p_return_status             => l_return_status,
4010       p_msg_count                 => l_msg_count,
4011       p_msg_data                  => l_msg_data ,
4012       --
4013       p_worksheet_id              => p_worksheet_id ,
4014       p_account_line_id           => c_WL_Rec.account_line_id,
4015       p_add_in_current_worksheet  => FND_API.G_FALSE
4016     ) ;
4017     --
4018     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
4019       RAISE FND_API.G_EXC_ERROR ;
4020     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
4021       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
4022     END IF;
4023     --
4024 
4025   END LOOP; -- End finding the account_line_id(s).
4026 
4027   --
4028   IF  FND_API.To_Boolean (p_commit) THEN
4029     COMMIT WORK;
4030   END IF;
4031   --
4032   FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
4033 			      p_data  => p_msg_data );
4034   --
4035   p_return_status := FND_API.G_RET_STS_SUCCESS;
4036 
4037 EXCEPTION
4038   --
4039   WHEN FND_API.G_EXC_ERROR THEN
4040     --
4041     ROLLBACK TO Add_Worksheet_Line_Pvt ;
4042     p_return_status := FND_API.G_RET_STS_ERROR;
4043     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
4044 				p_data  => p_msg_data );
4045   --
4046   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4047     --
4048     ROLLBACK TO Add_Worksheet_Line_Pvt ;
4049     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4050     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
4051 				p_data  => p_msg_data );
4052   --
4053   WHEN OTHERS THEN
4054     --
4055     ROLLBACK TO Add_Worksheet_Line_Pvt ;
4056     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4057     --
4058     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
4059       FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME,
4060 				l_api_name);
4061     END if;
4062     --
4063     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
4064 				p_data  => p_msg_data );
4065      --
4066 END Add_Worksheet_Line;
4067 /*---------------------------------------------------------------------------*/
4068 
4069 
4070 /*===========================================================================+
4071  |                     PROCEDURE Add_Line_To_Worksheets                      |
4072  +===========================================================================*/
4073 --
4074 -- This API adds a line to a set of worksheets.
4075 --
4076 PROCEDURE Add_Line_To_Worksheets
4077 (
4078   p_api_version               IN       NUMBER   ,
4079   p_init_msg_list             IN       VARCHAR2 := FND_API.G_FALSE ,
4080   p_commit                    IN       VARCHAR2 := FND_API.G_FALSE ,
4081   p_validation_level          IN       NUMBER   := FND_API.G_VALID_LEVEL_FULL ,
4082   p_return_status             OUT  NOCOPY      VARCHAR2 ,
4083   p_msg_count                 OUT  NOCOPY      NUMBER   ,
4084   p_msg_data                  OUT  NOCOPY      VARCHAR2 ,
4085   --
4086   p_account_line_id           IN      psb_ws_account_lines.account_line_id%TYPE,
4087   p_worksheet_tbl             IN      Worksheet_Tbl_Type
4088 )
4089 IS
4090   --
4091   l_api_name                CONSTANT VARCHAR2(30) := 'Add_Line_To_Worksheets';
4092   l_api_version             CONSTANT NUMBER       :=  1.0 ;
4093   --
4094   l_return_status           VARCHAR2(1) ;
4095   l_msg_count               NUMBER ;
4096   l_msg_data                VARCHAR2(2000) ;
4097   --
4098   l_current_worksheet_id    psb_worksheets.worksheet_id%TYPE ;
4099   --
4100   CURSOR l_ws_lines_csr
4101 	 IS
4102 	 SELECT *
4103 	 FROM   psb_ws_lines
4104 	 WHERE  account_line_id = p_account_line_id ;
4105   --
4106   l_ws_lines_row_type l_ws_lines_csr%ROWTYPE ;
4107   --
4108 BEGIN
4109   --
4110   SAVEPOINT Add_Line_To_Worksheets_Pvt ;
4111   --
4112   IF NOT FND_API.Compatible_API_Call ( l_api_version,
4113 				       p_api_version,
4114 				       l_api_name,
4115 				       G_PKG_NAME )
4116   THEN
4117     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
4118   END IF;
4119   --
4120 
4121   IF FND_API.To_Boolean ( p_init_msg_list ) THEN
4122     FND_MSG_PUB.initialize ;
4123   END IF;
4124   --
4125   p_return_status := FND_API.G_RET_STS_SUCCESS ;
4126 
4127   OPEN l_ws_lines_csr ;
4128 
4129   FETCH l_ws_lines_csr INTO l_ws_lines_row_type ;
4130 
4131   IF ( l_ws_lines_csr%NOTFOUND ) THEN
4132     --
4133     CLOSE l_ws_lines_csr ;
4134     Fnd_Message.Set_Name ('PSB', 'PSB_INVALID_ACCOUNT_LINE_ID') ;
4135     FND_MSG_PUB.Add;
4136     RAISE FND_API.G_EXC_ERROR ;
4137   END IF ;
4138 
4139   -- Process all the worksheets in the table.
4140   FOR i IN 1..p_worksheet_tbl.COUNT
4141   LOOP
4142     --
4143     -- API to add line to the current worksheet only.
4144     --
4145     Insert_WS_Lines_Pvt
4146     (
4147        p_worksheet_id       =>  p_worksheet_tbl(i) ,
4148        p_account_line_id    =>  p_account_line_id ,
4149        p_freeze_flag        =>  l_ws_lines_row_type.freeze_flag ,
4150        p_view_line_flag     =>  l_ws_lines_row_type.view_line_flag ,
4151        p_last_update_date   =>  g_current_date,
4152        p_last_updated_by    =>  g_current_user_id,
4153        p_last_update_login  =>  g_current_login_id,
4154        p_created_by         =>  g_current_user_id,
4155        p_creation_date      =>  g_current_date,
4156        p_return_status      =>  l_return_status
4157     ) ;
4158     --
4159     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
4160       RAISE FND_API.G_EXC_ERROR ;
4161     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
4162       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
4163     END IF;
4164     --
4165   END LOOP;
4166 
4167   --
4168   IF  FND_API.To_Boolean ( p_commit ) THEN
4169     COMMIT WORK;
4170   END IF;
4171   --
4172   FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
4173 			      p_data  => p_msg_data );
4174   --
4175 EXCEPTION
4176   --
4177   WHEN FND_API.G_EXC_ERROR THEN
4178     --
4179     ROLLBACK TO Add_Line_To_Worksheets_Pvt ;
4180     p_return_status := FND_API.G_RET_STS_ERROR;
4181     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
4182 				p_data  => p_msg_data );
4183   --
4184   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4185     --
4186     ROLLBACK TO Add_Line_To_Worksheets_Pvt ;
4187     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4188     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
4189 				p_data  => p_msg_data );
4190   --
4191   WHEN OTHERS THEN
4192     --
4193     ROLLBACK TO Add_Line_To_Worksheets_Pvt ;
4194     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4195     --
4196     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
4197       FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME,
4198 				l_api_name);
4199     END if;
4200     --
4201     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
4202 				p_data  => p_msg_data );
4203      --
4204 END Add_Line_To_Worksheets ;
4205 /*---------------------------------------------------------------------------*/
4206 
4207 
4208 /*===========================================================================+
4209  |                     PROCEDURE Add_Worksheet_Position_Line                 |
4210  +===========================================================================*/
4211 --
4212 -- This API adds a given position line to a worksheet. The addition is
4213 -- propagated to all the higher worksheets. The operation is performed only
4214 -- on the psb_ws_lines_positions table.
4215 --
4216 PROCEDURE Add_Worksheet_Position_Line
4217 (
4218   p_api_version               IN    NUMBER   ,
4219   p_init_msg_list             IN    VARCHAR2 := FND_API.G_FALSE ,
4220   p_commit                    IN    VARCHAR2 := FND_API.G_FALSE ,
4221   p_validation_level          IN    NUMBER   := FND_API.G_VALID_LEVEL_FULL ,
4222   p_return_status             OUT  NOCOPY   VARCHAR2 ,
4223   p_msg_count                 OUT  NOCOPY   NUMBER   ,
4224   p_msg_data                  OUT  NOCOPY   VARCHAR2 ,
4225   --
4226   p_worksheet_id              IN    psb_worksheets.worksheet_id%TYPE   ,
4227   p_position_line_id          IN    psb_ws_position_lines.position_line_id%TYPE,
4228   p_add_in_current_worksheet  IN    VARCHAR2 := FND_API.G_FALSE
4229 )
4230 IS
4231   --
4232   l_api_name          CONSTANT VARCHAR2(30)  := 'Add_Worksheet_Position_Line' ;
4233   l_api_version       CONSTANT NUMBER        :=  1.0 ;
4234   --
4235   l_return_status     VARCHAR2(1) ;
4236   l_msg_count         NUMBER ;
4237   l_msg_data          VARCHAR2(2000) ;
4238   --
4239   CURSOR l_ws_lines_positions_csr
4240 	 IS
4241 	 SELECT *
4242 	 FROM   psb_ws_lines_positions
4243 	 WHERE  position_line_id = p_position_line_id ;
4244   --
4245   l_ws_lines_positions_row_type
4246 			      l_ws_lines_positions_csr%ROWTYPE ;
4247   --
4248   l_current_worksheet_id      psb_worksheets.worksheet_id%TYPE ;
4249   l_current_budget_group_id   psb_worksheets.budget_group_id%TYPE ;
4250   l_budget_group_id           psb_worksheets.budget_group_id%TYPE ;
4251   l_parent_budget_group_id    psb_worksheets.budget_group_id%TYPE ;
4252   l_parent_worksheet_id       psb_worksheets.worksheet_id%TYPE ;
4253   l_distribution_id           psb_ws_distribution_details.distribution_id%TYPE;
4254 
4255 BEGIN
4256   --
4257   SAVEPOINT Add_Worksheet_Pos_Line_Pvt ;
4258   --
4259   IF NOT FND_API.Compatible_API_Call ( l_api_version,
4260 				       p_api_version,
4261 				       l_api_name,
4262 				       G_PKG_NAME )
4263   THEN
4264     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
4265   END IF;
4266   --
4267 
4268   IF FND_API.To_Boolean ( p_init_msg_list ) THEN
4269     FND_MSG_PUB.initialize ;
4270   END IF;
4271   --
4272   p_return_status := FND_API.G_RET_STS_SUCCESS ;
4273 
4274   --
4275   -- Finding the account line information.
4276   --
4277 
4278   OPEN l_ws_lines_positions_csr ;
4279 
4280   FETCH l_ws_lines_positions_csr INTO l_ws_lines_positions_row_type ;
4281 
4282   IF ( l_ws_lines_positions_csr%NOTFOUND ) THEN
4283     --
4284     CLOSE l_ws_lines_positions_csr ;
4285     Fnd_Message.Set_Name ('PSB', 'PSB_INVALID_POSITION_LINE_ID') ;
4286     FND_MSG_PUB.Add;
4287     RAISE FND_API.G_EXC_ERROR ;
4288   END IF ;
4289 
4290   l_current_worksheet_id := p_worksheet_id ;
4291 
4292   -- Updating the current worksheet (the one which is passed).
4293   IF p_add_in_current_worksheet = FND_API.G_TRUE THEN
4294 
4295     --
4296     -- Add the new position line to the current worksheet. All we need to do
4297     -- is make an entry in the position matrix.
4298     --
4299     PSB_WS_Pos_Pvt.Create_Position_Matrix
4300     (
4301        p_api_version        =>  1.0 ,
4302        p_init_msg_list      =>  FND_API.G_FALSE ,
4303        p_validation_level   =>  FND_API.G_VALID_LEVEL_NONE ,
4304        p_return_status      =>  l_return_status ,
4305        p_msg_count          =>  l_msg_count ,
4306        p_msg_data           =>  l_msg_data ,
4307        --
4308        p_worksheet_id       =>  l_current_worksheet_id ,
4309        p_position_line_id   =>  p_position_line_id ,
4310        p_freeze_flag        =>  l_ws_lines_positions_row_type.freeze_flag ,
4311        p_view_line_flag     =>  l_ws_lines_positions_row_type.view_line_flag
4312     ) ;
4313     --
4314     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
4315       RAISE FND_API.G_EXC_ERROR ;
4316     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
4317       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
4318     END IF;
4319     --
4320   END IF ;
4321 
4322   --
4323   -- Updating the top worksheets in the hierarchy.
4324   --
4325   LOOP
4326     --
4327     PSB_WS_Ops_Pvt.Find_Parent_Worksheet
4328     (
4329       p_api_version             => 1.0 ,
4330       p_init_msg_list           => FND_API.G_FALSE,
4331       p_commit                  => FND_API.G_FALSE,
4332       p_validation_level        => FND_API.G_VALID_LEVEL_NONE,
4333       p_return_status           => l_return_status,
4334       p_msg_count               => l_msg_count,
4335       p_msg_data                => l_msg_data ,
4336       --
4337       p_worksheet_id            => l_current_worksheet_id ,
4338       p_worksheet_id_OUT        => l_parent_worksheet_id
4339     ) ;
4340     --
4341     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
4342       RAISE FND_API.G_EXC_ERROR ;
4343     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
4344       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
4345     ELSIF l_parent_worksheet_id = 0 THEN
4346       -- It means all the top worksheets have been processed.
4347       EXIT ;
4348     END IF ;
4349 
4350     l_current_worksheet_id := l_parent_worksheet_id ;
4351 
4352     --
4353     -- Add the new position line to the current worksheet.
4354     --
4355     PSB_WS_Pos_Pvt.Create_Position_Matrix
4356     (
4357        p_api_version        =>  1.0 ,
4358        p_init_msg_list      =>  FND_API.G_FALSE ,
4359        p_validation_level   =>  FND_API.G_VALID_LEVEL_NONE ,
4360        p_return_status      =>  l_return_status ,
4361        p_msg_count          =>  l_msg_count ,
4362        p_msg_data           =>  l_msg_data ,
4363        --
4364        p_worksheet_id       =>  l_current_worksheet_id ,
4365        p_position_line_id   =>  p_position_line_id ,
4366        p_freeze_flag        =>  l_ws_lines_positions_row_type.freeze_flag ,
4367        p_view_line_flag     =>  l_ws_lines_positions_row_type.view_line_flag
4368     ) ;
4369     --
4370     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
4371       RAISE FND_API.G_EXC_ERROR ;
4372     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
4373       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
4374     END IF;
4375     --
4376   END LOOP;
4377   --
4378   CLOSE l_ws_lines_positions_csr ;
4379 
4380   --
4381   IF  FND_API.To_Boolean ( p_commit ) THEN
4382     COMMIT WORK;
4383   END IF;
4384   --
4385   FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
4386 			      p_data  => p_msg_data );
4387   --
4388 EXCEPTION
4389   --
4390   WHEN FND_API.G_EXC_ERROR THEN
4391     --
4392     ROLLBACK TO Add_Worksheet_Pos_Line_Pvt ;
4393     p_return_status := FND_API.G_RET_STS_ERROR;
4394     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
4395 				p_data  => p_msg_data );
4396   --
4397   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4398     --
4399     ROLLBACK TO Add_Worksheet_Pos_Line_Pvt ;
4400     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4401     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
4402 				p_data  => p_msg_data );
4403   --
4404   WHEN OTHERS THEN
4405     --
4406     IF ( l_ws_lines_positions_csr%ISOPEN ) THEN
4407       CLOSE l_ws_lines_positions_csr ;
4408     END IF ;
4409     --
4410     ROLLBACK TO Add_Worksheet_Pos_Line_Pvt ;
4411     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4412     --
4413     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
4414       FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME,
4415 				l_api_name);
4416     END if;
4417     --
4418     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
4419 				p_data  => p_msg_data );
4420      --
4421 END Add_Worksheet_Position_Line ;
4422 /*---------------------------------------------------------------------------*/
4423 
4424 
4425 /*===========================================================================+
4426  |                     PROCEDURE Add_Pos_Line_To_Worksheets                  |
4427  +===========================================================================*/
4428 --
4429 -- This API adds a line to a set of worksheets.
4430 --
4431 PROCEDURE Add_Pos_Line_To_Worksheets
4432 (
4433   p_api_version               IN   NUMBER   ,
4434   p_init_msg_list             IN   VARCHAR2 := FND_API.G_FALSE ,
4435   p_commit                    IN   VARCHAR2 := FND_API.G_FALSE ,
4436   p_validation_level          IN   NUMBER   := FND_API.G_VALID_LEVEL_FULL ,
4437   p_return_status             OUT  NOCOPY  VARCHAR2 ,
4438   p_msg_count                 OUT  NOCOPY  NUMBER   ,
4439   p_msg_data                  OUT  NOCOPY  VARCHAR2 ,
4440   --
4441   p_position_line_id          IN   psb_ws_position_lines.position_line_id%TYPE,
4442   p_worksheet_tbl             IN   Worksheet_Tbl_Type
4443 )
4444 IS
4445   --
4446   l_api_name              CONSTANT VARCHAR2(30) := 'Add_Pos_Line_To_Worksheets';
4447   l_api_version           CONSTANT NUMBER       :=  1.0 ;
4448   --
4449   l_return_status         VARCHAR2(1) ;
4450   l_msg_count             NUMBER ;
4451   l_msg_data              VARCHAR2(2000) ;
4452   --
4453   l_current_worksheet_id psb_worksheets.worksheet_id%TYPE ;
4454   --
4455   CURSOR l_ws_lines_positions_csr
4456 	 IS
4457 	 SELECT *
4458 	 FROM   psb_ws_lines_positions
4459 	 WHERE  position_line_id = p_position_line_id ;
4460   --
4461   l_ws_lines_positions_row_type   l_ws_lines_positions_csr%ROWTYPE ;
4462   --
4463 BEGIN
4464   --
4465   SAVEPOINT Add_Pos_Line_To_Worksheets_Pvt ;
4466   --
4467   IF NOT FND_API.Compatible_API_Call ( l_api_version,
4468 				       p_api_version,
4469 				       l_api_name,
4470 				       G_PKG_NAME )
4471   THEN
4472     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
4473   END IF;
4474   --
4475 
4476   IF FND_API.To_Boolean ( p_init_msg_list ) THEN
4477     FND_MSG_PUB.initialize ;
4478   END IF;
4479   --
4480   p_return_status := FND_API.G_RET_STS_SUCCESS ;
4481 
4482   OPEN l_ws_lines_positions_csr ;
4483 
4484   FETCH l_ws_lines_positions_csr INTO l_ws_lines_positions_row_type ;
4485 
4486   IF ( l_ws_lines_positions_csr%NOTFOUND ) THEN
4487     --
4488     CLOSE l_ws_lines_positions_csr ;
4489     Fnd_Message.Set_Name ('PSB', 'PSB_INVALID_POSITION_LINE_ID') ;
4490     FND_MSG_PUB.Add;
4491     RAISE FND_API.G_EXC_ERROR ;
4492   END IF ;
4493 
4494   -- Process all the worksheets in the table.
4495   FOR i IN 1..p_worksheet_tbl.COUNT
4496   LOOP
4497     --
4498     -- API to add line to the current worksheet only.
4499     --
4500     PSB_WS_Pos_Pvt.Create_Position_Matrix
4501     (
4502        p_api_version        =>  1.0 ,
4503        p_init_msg_list      =>  FND_API.G_FALSE ,
4504        p_validation_level   =>  FND_API.G_VALID_LEVEL_NONE ,
4505        p_return_status      =>  l_return_status ,
4506        p_msg_count          =>  l_msg_count ,
4507        p_msg_data           =>  l_msg_data ,
4508        --
4509        p_worksheet_id       =>  p_worksheet_tbl(i) ,
4510        p_position_line_id   =>  p_position_line_id ,
4511        p_freeze_flag        =>  l_ws_lines_positions_row_type.freeze_flag ,
4512        p_view_line_flag     =>  l_ws_lines_positions_row_type.view_line_flag
4513     ) ;
4514     --
4515     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
4516       RAISE FND_API.G_EXC_ERROR ;
4517     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
4518       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
4519     END IF;
4520     --
4521   END LOOP;
4522 
4523   --
4524   IF  FND_API.To_Boolean ( p_commit ) THEN
4525     COMMIT WORK;
4526   END IF;
4527   --
4528   FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
4529 			      p_data  => p_msg_data );
4530   --
4531 EXCEPTION
4532   --
4533   WHEN FND_API.G_EXC_ERROR THEN
4534     --
4535     ROLLBACK TO Add_Pos_Line_To_Worksheets_Pvt ;
4536     p_return_status := FND_API.G_RET_STS_ERROR;
4537     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
4538 				p_data  => p_msg_data );
4539   --
4540   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4541     --
4542     ROLLBACK TO Add_Pos_Line_To_Worksheets_Pvt ;
4543     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4544     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
4545 				p_data  => p_msg_data );
4546   --
4547   WHEN OTHERS THEN
4548     --
4549     ROLLBACK TO Add_Pos_Line_To_Worksheets_Pvt ;
4550     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4551     --
4552     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
4553       FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME,
4554 				l_api_name);
4555     END if;
4556     --
4557     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
4558 				p_data  => p_msg_data );
4559      --
4560 END Add_Pos_Line_To_Worksheets ;
4561 /*---------------------------------------------------------------------------*/
4562 
4563 
4564 /*===========================================================================+
4565  |                       PROCEDURE Freeze_Worksheet                          |
4566  +===========================================================================*/
4567 --
4568 -- This API freezes a given worksheet.
4569 --
4570 PROCEDURE Freeze_Worksheet
4571 (
4572   p_api_version               IN       NUMBER   ,
4573   p_init_msg_list             IN       VARCHAR2 := FND_API.G_FALSE ,
4574   p_commit                    IN       VARCHAR2 := FND_API.G_FALSE ,
4575   p_validation_level          IN       NUMBER   := FND_API.G_VALID_LEVEL_FULL ,
4576   p_return_status             OUT  NOCOPY      VARCHAR2 ,
4577   p_msg_count                 OUT  NOCOPY      NUMBER   ,
4578   p_msg_data                  OUT  NOCOPY      VARCHAR2 ,
4579   --
4580   p_worksheet_id              IN       psb_worksheets.worksheet_id%TYPE ,
4581   p_freeze_flag               IN       psb_ws_lines.freeze_flag%TYPE
4582 )
4583 IS
4584   --
4585   l_api_name                CONSTANT VARCHAR2(30)  := 'Freeze_Worksheet' ;
4586   l_api_version             CONSTANT NUMBER        :=  1.0 ;
4587   --
4588   l_budget_by_position      psb_worksheets.budget_by_position%TYPE ;
4589   l_return_status           VARCHAR2(1) ;
4590   l_msg_count               NUMBER ;
4591   l_msg_data                VARCHAR2(2000) ;
4592   --
4593   l_parent_worksheet_id     psb_worksheets.worksheet_id%TYPE ;
4594   l_parent_freeze_flag      psb_worksheets.freeze_flag%TYPE ;
4595   l_worksheet_type          VARCHAR2(1);
4596   --
4597 
4598   -- bug start 3970347
4599   l_global_worksheet_flag	psb_worksheets.global_worksheet_flag%TYPE;
4600   l_gl_cutoff_period		psb_worksheets.gl_cutoff_period%TYPE := NULL;
4601   -- bug end   3970347
4602 
4603 BEGIN
4604   --
4605   SAVEPOINT Freeze_Worksheet_Pvt ;
4606   --
4607   IF NOT FND_API.Compatible_API_Call ( l_api_version,
4608 				       p_api_version,
4609 				       l_api_name,
4610 				       G_PKG_NAME )
4611   THEN
4612     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
4613   END IF;
4614   --
4615 
4616   IF FND_API.To_Boolean ( p_init_msg_list ) THEN
4617     FND_MSG_PUB.initialize ;
4618   END IF;
4619   --
4620   p_return_status := FND_API.G_RET_STS_SUCCESS ;
4621   --
4622   /* Bug 3664027 Start */
4623   -- A worksheet(other than local worksheet) can only be unfrozen
4624   -- if the parent worksheet is not frozen, if exists( p_freeze_flag = 'N'
4625   -- identifies an unfreeze operation). Local worksheet can be
4626   -- freezed/unfreezed without any check since it has no relation
4627   -- with the parent worksheet.
4628   --
4629 
4630   SELECT NVL(worksheet_type, '9'),
4631   -- bug start 3970347
4632   -- get the global worksheet flag from psb_worksheet table
4633          global_worksheet_flag
4634   -- bug end 3970347
4635   INTO l_worksheet_type,
4636   -- bug start 3970347
4637        l_global_worksheet_flag
4638   -- bug end 3970347
4639   FROM psb_worksheets
4640   WHERE worksheet_id = p_worksheet_id;
4641 
4642   IF p_freeze_flag = 'N' AND l_worksheet_type <> 'L' THEN
4643   /* Bug 3664027 End */
4644 
4645     -- Find parent worksheet, if exists.
4646     PSB_WS_Ops_Pvt.Find_Parent_Worksheet
4647     (
4648       p_api_version             => 1.0 ,
4649       p_init_msg_list           => FND_API.G_FALSE,
4650       p_commit                  => FND_API.G_FALSE,
4651       p_validation_level        => FND_API.G_VALID_LEVEL_NONE,
4652       p_return_status           => l_return_status,
4653       p_msg_count               => l_msg_count,
4654       p_msg_data                => l_msg_data ,
4655       --
4656       p_worksheet_id            => p_worksheet_id ,
4657       p_worksheet_id_OUT        => l_parent_worksheet_id
4658     ) ;
4659     --
4660     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
4661       RAISE FND_API.G_EXC_ERROR ;
4662     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
4663       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
4664     END IF;
4665 
4666     -- Check the freeze_flag for the parent worksheet.
4667     IF l_parent_worksheet_id <> 0 THEN
4668 
4669       SELECT NVL(freeze_flag, 'N') INTO l_parent_freeze_flag
4670       FROM   psb_worksheets
4671       WHERE  worksheet_id = l_parent_worksheet_id ;
4672 
4673       IF l_parent_freeze_flag = 'Y' THEN
4674 
4675 	Fnd_Message.Set_Name('PSB','PSB_CANNOT_UNFREEZE_WORKSHEET') ;
4676 	FND_MSG_PUB.Add;
4677 	RAISE FND_API.G_EXC_ERROR ;
4678 
4679       END IF ;
4680 
4681     END IF ;
4682 
4683   END IF ;
4684   -- End validating the parent worksheet for unfreeze operation.
4685 
4686   --
4687   -- Update freeze_flag in psb_worksheets.
4688   --
4689 
4690   /* bug start 3970347 */
4691   -- check if the worksheet is global worksheet. If global, then
4692   -- pass the gl cutoff date otherwise pass null
4693   IF l_global_worksheet_flag = 'Y' THEN
4694     FOR l_gl_cutoff_period_rec IN (SELECT gl_cutoff_period
4695     						   FROM   psb_worksheets
4696     						   WHERE  worksheet_id = p_worksheet_id)
4697     LOOP
4698       l_gl_cutoff_period := l_gl_cutoff_period_rec.gl_cutoff_period;
4699     END LOOP;
4700   END IF;
4701   /* bug end 3970347 */
4702 
4703   PSB_Worksheet_Pvt.Update_Worksheet
4704   (
4705      p_api_version                 => 1.0 ,
4706      p_init_msg_list               => FND_API.G_FALSE,
4707      p_commit                      => FND_API.G_FALSE,
4708      p_validation_level            => FND_API.G_VALID_LEVEL_NONE,
4709      p_return_status               => l_return_status,
4710      p_msg_count                   => l_msg_count,
4711      p_msg_data                    => l_msg_data ,
4712      --
4713      p_worksheet_id                => p_worksheet_id ,
4714      p_freeze_flag                 => p_freeze_flag ,
4715      -- bug start 3970347
4716      -- pass the gl cutoff period to the update_worksheet API
4717      p_gl_cutoff_period 		   => l_gl_cutoff_period
4718      -- bug end 3970347
4719   );
4720   --
4721   IF l_return_status = FND_API.G_RET_STS_ERROR THEN
4722     RAISE FND_API.G_EXC_ERROR ;
4723   ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
4724     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
4725   END IF;
4726   --
4727 
4728 
4729   -- Update freeze_flag in psb_ws_lines.
4730   UPDATE psb_ws_lines
4731   SET    freeze_flag  = p_freeze_flag
4732   WHERE  worksheet_id = p_worksheet_id;
4733 
4734 
4735   -- Get position budgeting flag.
4736   SELECT NVL(budget_by_position, 'N')  INTO l_budget_by_position
4737   FROM   psb_worksheets
4738   WHERE  worksheet_id = p_worksheet_id ;
4739 
4740 
4741   IF l_budget_by_position = 'Y' THEN
4742     --
4743     -- Update freeze_flag in psb_ws_lines_positions.
4744     --
4745     UPDATE psb_ws_lines_positions
4746     SET    freeze_flag  = p_freeze_flag
4747     WHERE  worksheet_id = p_worksheet_id;
4748     --
4749   END IF;
4750 
4751   IF  FND_API.To_Boolean ( p_commit ) THEN
4752     COMMIT WORK;
4753   END IF;
4754   --
4755   FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
4756 			      p_data  => p_msg_data );
4757   --
4758 EXCEPTION
4759   --
4760   WHEN FND_API.G_EXC_ERROR THEN
4761     --
4762     ROLLBACK TO Freeze_Worksheet_Pvt ;
4763     p_return_status := FND_API.G_RET_STS_ERROR;
4764     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
4765 				p_data  => p_msg_data );
4766   --
4767   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4768     --
4769     ROLLBACK TO Freeze_Worksheet_Pvt ;
4770     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4771     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
4772 				p_data  => p_msg_data );
4773   --
4774   WHEN OTHERS THEN
4775     --
4776     ROLLBACK TO Freeze_Worksheet_Pvt ;
4777     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4778     --
4779     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
4780       FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME,
4781 				l_api_name);
4782     END if;
4783     --
4784     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
4785 				p_data  => p_msg_data );
4786      --
4787 END Freeze_Worksheet ;
4788 /*---------------------------------------------------------------------------*/
4789 
4790 
4791 /*===========================================================================+
4792  |                       PROCEDURE Change_Worksheet_Stage                    |
4793  +===========================================================================*/
4794 --
4795 -- This API moves a worksheet to its next stage (version).
4796 --
4797 PROCEDURE Change_Worksheet_Stage
4798 (
4799   p_api_version           IN           NUMBER   ,
4800   p_init_msg_list         IN           VARCHAR2 := FND_API.G_FALSE ,
4801   p_commit                IN           VARCHAR2 := FND_API.G_FALSE ,
4802   p_validation_level      IN           NUMBER   := FND_API.G_VALID_LEVEL_FULL ,
4803   p_return_status         OUT  NOCOPY  VARCHAR2 ,
4804   p_msg_count             OUT  NOCOPY  NUMBER   ,
4805   p_msg_data              OUT  NOCOPY  VARCHAR2 ,
4806   --
4807   p_worksheet_id          IN           psb_worksheets.worksheet_id%TYPE  ,
4808   p_stage_seq             IN           psb_worksheets.current_stage_seq%TYPE
4809 				       := FND_API.G_MISS_NUM ,
4810   p_operation_id          IN           NUMBER := FND_API.G_MISS_NUM
4811 )
4812 IS
4813   --
4814   l_api_name                CONSTANT VARCHAR2(30) := 'Change_Worksheet_Stage' ;
4815   l_api_version             CONSTANT NUMBER       :=  1.0 ;
4816   --
4817   l_return_status           VARCHAR2(1) ;
4818   l_msg_count               NUMBER      ;
4819   l_msg_data                VARCHAR2(2000) ;
4820   --
4821   l_stage_set_id            psb_worksheets.stage_set_id%TYPE ;
4822   l_target_stage_seq        psb_worksheets.current_stage_seq%TYPE ;
4823   l_current_stage_seq       psb_worksheets.current_stage_seq%TYPE ;
4824   l_budget_by_position      psb_worksheets.budget_by_position%TYPE ;
4825   l_service_package_count   NUMBER ;
4826   --
4827 
4828   -- bug start 3970347
4829   l_global_worksheet_flag	psb_worksheets.global_worksheet_flag%TYPE;
4830   l_gl_cutoff_period		psb_worksheets.gl_cutoff_period%TYPE;
4831   -- bug end 3970347
4832 
4833 BEGIN
4834   --
4835   SAVEPOINT Change_Worksheet_Stage_Pvt ;
4836   --
4837   IF NOT FND_API.Compatible_API_Call ( l_api_version,
4838 				       p_api_version,
4839 				       l_api_name,
4840 				       G_PKG_NAME )
4841   THEN
4842     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
4843   END IF;
4844   --
4845   IF FND_API.To_Boolean ( p_init_msg_list ) THEN
4846     FND_MSG_PUB.initialize ;
4847   END IF;
4848   --
4849   p_return_status := FND_API.G_RET_STS_SUCCESS ;
4850   --
4851 
4852   -- Get position budgeting flag.
4853 
4854   SELECT NVL( budget_by_position, 'N') ,
4855   /* bug start 3970347 */
4856   -- get the global worksheet flag from psb_worksheets table
4857          global_worksheet_flag
4858   /* bug end 3970347 */
4859   INTO l_budget_by_position ,
4860   /* bug start 3970347 */
4861        l_global_worksheet_flag
4862   /* bug end 3970347 */
4863   FROM   psb_worksheets
4864   WHERE  worksheet_id = p_worksheet_id ;
4865 
4866   IF ( p_stage_seq = FND_API.G_MISS_NUM ) OR ( p_stage_seq IS NULL) THEN
4867 
4868     --
4869     -- Find next stage_id for the worksheet.
4870     --
4871     SELECT stage_set_id                  ,
4872 	   current_stage_seq
4873        INTO
4874 	   l_stage_set_id                ,
4875 	   l_current_stage_seq
4876     FROM   psb_worksheets
4877     WHERE  worksheet_id = p_worksheet_id ;
4878 
4879     SELECT MIN (sequence_number) INTO l_target_stage_seq
4880     FROM   psb_budget_stages
4881     WHERE  budget_stage_set_id = l_stage_set_id
4882     AND    sequence_number     > l_current_stage_seq
4883     ORDER  BY sequence_number ;
4884 
4885   ELSE
4886     l_target_stage_seq := p_stage_seq ;
4887   END IF ;
4888 
4889   --
4890   -- If l_target_stage_seq is NULL means the worksheet is already at
4891   -- its highest stage. Simply return back.
4892   --
4893   IF l_target_stage_seq IS NULL THEN
4894     RETURN ;
4895   END IF ;
4896 
4897   --
4898   -- Updating the psb_worksheets.
4899   --
4900 
4901   /* bug start 3970347 */
4902   -- check if the worksheet is global worksheet. If global, then
4903   -- pass the gl cutoff date.
4904   IF l_global_worksheet_flag = 'Y' THEN
4905     FOR l_gl_cutoff_period_rec IN (SELECT gl_cutoff_period
4906     						   FROM   psb_worksheets
4907     						   WHERE  worksheet_id = p_worksheet_id)
4908     LOOP
4909       l_gl_cutoff_period := l_gl_cutoff_period_rec.gl_cutoff_period;
4910     END LOOP;
4911   END IF;
4912   /* bug end 3970347 */
4913 
4914   PSB_Worksheet_Pvt.Update_Worksheet
4915   (
4916     p_api_version           => 1.0 ,
4917     p_init_msg_list         => FND_API.G_FALSE,
4918     p_commit                => FND_API.G_FALSE,
4919     p_validation_level      => FND_API.G_VALID_LEVEL_NONE,
4920     p_return_status         => l_return_status,
4921     p_msg_count             => l_msg_count,
4922     p_msg_data              => l_msg_data,
4923     --
4924     p_worksheet_id          => p_worksheet_id,
4925     p_current_stage_seq     => l_target_stage_seq ,
4926     -- bug start 3970347
4927     p_gl_cutoff_period 		=> l_gl_cutoff_period
4928     -- bug end 3970347
4929   ) ;
4930   --
4931   IF l_return_status = FND_API.G_RET_STS_ERROR THEN
4932     RAISE FND_API.G_EXC_ERROR ;
4933   ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
4934     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
4935   END IF;
4936 
4937   --
4938   -- Check whether service packages were selected for the worksheet.
4939   -- If yes, then we need to move only those account lines which are
4940   -- related to the service package selection.
4941   --
4942   SELECT COUNT(*) INTO l_service_package_count
4943   FROM   dual
4944   WHERE  EXISTS
4945          ( SELECT 1
4946            FROM   psb_ws_submit_service_packages
4947            WHERE  worksheet_id = p_worksheet_id
4948            AND    operation_id = p_operation_id ) ;
4949 
4950   IF l_service_package_count = 0 THEN
4951 
4952     --
4953     -- Update all the lines in psb_ws_account_lines as no service package
4954     -- selection exists.
4955     --
4956     UPDATE psb_ws_account_lines
4957     SET    current_stage_seq = l_target_stage_seq
4958     WHERE  l_target_stage_seq > current_stage_seq
4959     AND    end_stage_seq is null
4960     AND    account_line_id IN
4961 			 ( SELECT account_line_id
4962 			   FROM   psb_ws_lines
4963 			   WHERE  worksheet_id = p_worksheet_id ) ;
4964 
4965     IF l_budget_by_position = 'Y' THEN
4966       --
4967       -- Update all the lines in psb_ws_fte_lines as no service package
4968       -- selection exists.
4969       --
4970       UPDATE psb_ws_fte_lines
4971       SET    current_stage_seq = l_target_stage_seq
4972       WHERE  l_target_stage_seq > current_stage_seq
4973       AND    end_stage_seq is null
4974       AND    position_line_id IN
4975 		       ( SELECT position_line_id
4976 			 FROM   psb_ws_lines_positions
4977 			 WHERE  worksheet_id = p_worksheet_id ) ;
4978 
4979       --
4980       -- Update all the lines in psb_ws_element_lines as no service package
4981       -- selection exists.
4982       --
4983       UPDATE psb_ws_element_lines
4984       SET    current_stage_seq = l_target_stage_seq
4985       WHERE  l_target_stage_seq > current_stage_seq
4986       AND    end_stage_seq is null
4987       AND    position_line_id IN
4988 		       ( SELECT position_line_id
4989 			 FROM   psb_ws_lines_positions
4990 			 WHERE  worksheet_id = p_worksheet_id ) ;
4991       --
4992     END IF;
4993 
4994   ELSE
4995 
4996     --
4997     -- Update psb_ws_account_lines as per the service package selection.
4998     --
4999     UPDATE psb_ws_account_lines
5000     SET    current_stage_seq = l_target_stage_seq
5001     WHERE  l_target_stage_seq > current_stage_seq
5002     AND    end_stage_seq is null
5003     AND    account_line_id IN
5004 		     (  SELECT account_line_id
5005 			FROM   psb_ws_lines
5006 			WHERE  worksheet_id = p_worksheet_id
5007 		      )
5008     AND    service_package_id IN
5009 		     (  SELECT service_package_id
5010 			FROM   psb_ws_submit_service_packages
5011 			WHERE  worksheet_id = p_worksheet_id
5012 			AND    operation_id = p_operation_id
5013 		      ) ;
5014     --
5015     IF l_budget_by_position = 'Y' THEN
5016 
5017       --
5018       -- Update psb_ws_fte_lines as per the service package selection.
5019       --
5020       UPDATE psb_ws_fte_lines
5021       SET    current_stage_seq = l_target_stage_seq
5022       WHERE  l_target_stage_seq > current_stage_seq
5023       AND    end_stage_seq is null
5024       AND    position_line_id IN
5025 		       (  SELECT position_line_id
5026 			  FROM   psb_ws_lines_positions
5027 			  WHERE  worksheet_id = p_worksheet_id
5028 			)
5029       AND    service_package_id IN
5030 		       (  SELECT service_package_id
5031 			  FROM   psb_ws_submit_service_packages
5032 			  WHERE  worksheet_id = p_worksheet_id
5033 			  AND    operation_id = p_operation_id
5034 			) ;
5035 
5036       --
5037       -- Update psb_ws_element_lines as per the service package selection.
5038       --
5039       UPDATE psb_ws_element_lines
5040       SET    current_stage_seq = l_target_stage_seq
5041       WHERE  l_target_stage_seq > current_stage_seq
5042       AND    end_stage_seq is null
5043       AND    position_line_id IN
5044 		       (  SELECT position_line_id
5045 			  FROM   psb_ws_lines_positions
5046 			  WHERE  worksheet_id = p_worksheet_id
5047 			)
5048       AND    service_package_id IN
5049 		       (  SELECT service_package_id
5050 			  FROM   psb_ws_submit_service_packages
5051 			  WHERE  worksheet_id = p_worksheet_id
5052 			  AND    operation_id = p_operation_id
5053 			) ;
5054       --
5055     END IF;
5056     --
5057   END IF ;
5058 
5059   --
5060   IF  FND_API.To_Boolean ( p_commit ) THEN
5061     COMMIT WORK;
5062   END IF;
5063   --
5064   FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
5065 			      p_data  => p_msg_data );
5066   --
5067 EXCEPTION
5068   --
5069   WHEN FND_API.G_EXC_ERROR THEN
5070     --
5071     ROLLBACK TO Change_Worksheet_Stage_Pvt ;
5072     p_return_status := FND_API.G_RET_STS_ERROR;
5073     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
5074 				p_data  => p_msg_data );
5075   --
5076   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5077     --
5078     ROLLBACK TO Change_Worksheet_Stage_Pvt ;
5079     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5080     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
5081 				p_data  => p_msg_data );
5082   --
5083   WHEN OTHERS THEN
5084     --
5085     ROLLBACK TO Change_Worksheet_Stage_Pvt ;
5086     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5087     --
5088     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
5089       FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME,
5090 				l_api_name);
5091     END if;
5092     --
5093     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
5094 				p_data  => p_msg_data );
5095      --
5096 END Change_Worksheet_Stage ;
5097 /*---------------------------------------------------------------------------*/
5098 
5099 
5100 /*===========================================================================+
5101  |                   PROCEDURE Find_Parent_Worksheet                         |
5102  +===========================================================================*/
5103 --
5104 -- The API finds parent worksheet of a given worksheet.
5105 --
5106 PROCEDURE Find_Parent_Worksheet
5107 (
5108   p_api_version               IN       NUMBER   ,
5109   p_init_msg_list             IN       VARCHAR2 := FND_API.G_FALSE ,
5110   p_commit                    IN       VARCHAR2 := FND_API.G_FALSE ,
5111   p_validation_level          IN       NUMBER   := FND_API.G_VALID_LEVEL_FULL ,
5112   p_return_status             OUT  NOCOPY      VARCHAR2 ,
5113   p_msg_count                 OUT  NOCOPY      NUMBER   ,
5114   p_msg_data                  OUT  NOCOPY      VARCHAR2 ,
5115   --
5116   p_worksheet_id              IN       psb_worksheets.worksheet_id%TYPE ,
5117   p_worksheet_id_OUT          OUT  NOCOPY      psb_worksheets.worksheet_id%TYPE
5118 )
5119 IS
5120   --
5121   l_api_name                  CONSTANT VARCHAR2(30) := 'Find_Parent_Worksheet' ;
5122   l_api_version               CONSTANT NUMBER       :=  1.0 ;
5123   l_return_status             VARCHAR2(1) ;
5124   l_msg_count                 NUMBER      ;
5125   l_msg_data                  VARCHAR2(2000) ;
5126   --
5127   l_global_worksheet_id       psb_worksheets.global_worksheet_id%TYPE ;
5128   l_global_worksheet_flag     psb_worksheets.global_worksheet_flag%TYPE ;
5129   l_budget_group_id           psb_worksheets.budget_group_id%TYPE ;
5130   l_global_budget_group_id    psb_worksheets.budget_group_id%TYPE ;
5131   --
5132   l_parent_budget_group_id    psb_worksheets.budget_group_id%TYPE ;
5133   --
5134 BEGIN
5135   --
5136   SAVEPOINT Find_Parent_Worksheet_Pvt ;
5137   --
5138   IF NOT FND_API.Compatible_API_Call ( l_api_version,
5139 				       p_api_version,
5140 				       l_api_name,
5141 				       G_PKG_NAME )
5142   THEN
5143     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
5144   END IF;
5145   --
5146 
5147   IF FND_API.To_Boolean ( p_init_msg_list ) THEN
5148     FND_MSG_PUB.initialize ;
5149   END IF;
5150   --
5151   p_return_status    := FND_API.G_RET_STS_SUCCESS ;
5152   p_worksheet_id_OUT := -99 ;
5153   --
5154 
5155   --
5156   -- Finding the worksheet information.
5157   --
5158   SELECT ws.budget_group_id         ,
5159 	 ws.global_worksheet_id     ,
5160 	 ws.global_worksheet_flag   ,
5161 	 bg.parent_budget_group_id
5162       INTO
5163 	 l_budget_group_id          ,
5164 	 l_global_worksheet_id      ,
5165 	 l_global_worksheet_flag    ,
5166 	 l_parent_budget_group_id
5167   FROM   psb_worksheets    ws,
5168 	 psb_budget_groups bg
5169   WHERE  ws.worksheet_id    = p_worksheet_id
5170   AND    ws.budget_group_id = bg.budget_group_id ;
5171 
5172   IF (l_global_worksheet_flag = 'Y') OR (l_parent_budget_group_id IS NULL) THEN
5173     --
5174 /*  Commenting as this message is not required. The p_worksheet_id_OUT
5175     parameter should be used to determine whether parent worksheet exists.
5176 
5177     Fnd_Message.Set_Name('PSB','PSB_NO_PARENT_WORKSHEET' ) ;
5178     FND_MSG_PUB.Add;
5179 */
5180     p_worksheet_id_OUT := 0 ;
5181     RETURN ;
5182   END IF ;
5183 
5184   --
5185   -- Find global budget_group_id for the global worksheet.
5186   --
5187   SELECT budget_group_id INTO l_global_budget_group_id
5188   FROM   psb_worksheets
5189   WHERE  worksheet_id = l_global_worksheet_id ;
5190 
5191   --
5192   -- If parent budget group for the current worksheet is same as the budget
5193   -- group for the global worksheet, then the global worksheet is the parent
5194   -- worksheet for the given worksheet.
5195   --
5196   IF l_global_budget_group_id = l_parent_budget_group_id THEN
5197     p_worksheet_id_OUT := l_global_worksheet_id ;
5198     RETURN ;
5199   END IF ;
5200 
5201 
5202   --
5203   -- Get the desired parent worksheet at the l_parent_budget_group_id level.
5204   --
5205   BEGIN
5206 
5207     --
5208     -- New way to find if a worksheet has been created for a budget group.
5209     -- ( Bug#2832148 )
5210     --
5211     SELECT worksheet_id INTO p_worksheet_id_OUT
5212     FROM   psb_worksheets
5213     WHERE  global_worksheet_id = l_global_worksheet_id
5214     AND    budget_group_id     = l_parent_budget_group_id
5215     AND    worksheet_type      = 'O' ;
5216 
5217     /*
5218     SELECT DISTINCT child_worksheet_id
5219       INTO p_worksheet_id_OUT
5220       FROM psb_ws_distribution_details details, psb_ws_distributions distr
5221      WHERE distr.worksheet_id = details.worksheet_id
5222     -- Bug No 2297742 Start
5223     --    AND    distr.distribution_option_flag   = 'W'
5224        AND nvl(distr.distribution_option_flag, 'W') = 'W'
5225     -- Bug No 2297742 End
5226        AND global_worksheet_id = l_global_worksheet_id
5227        AND child_budget_group_id = l_parent_budget_group_id;
5228     */
5229     --
5230   EXCEPTION
5231     WHEN no_data_found THEN
5232       --
5233       -- Cannot use FND_API.G_MISS_NUM as worksheet_id is NUMBER(20) only.
5234       --
5235       p_worksheet_id_OUT := 0 ;
5236     --
5237   END ;
5238 
5239   --
5240   IF FND_API.To_Boolean ( p_commit ) THEN
5241     COMMIT WORK;
5242   END IF;
5243   --
5244   FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
5245 			      p_data  => p_msg_data );
5246   --
5247 EXCEPTION
5248   --
5249   WHEN FND_API.G_EXC_ERROR THEN
5250     --
5251     ROLLBACK TO Find_Parent_Worksheet_Pvt ;
5252     p_return_status := FND_API.G_RET_STS_ERROR;
5253     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
5254 				p_data  => p_msg_data );
5255   --
5256   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5257     --
5258     ROLLBACK TO Find_Parent_Worksheet_Pvt ;
5259     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5260     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
5261 				p_data  => p_msg_data );
5262   --
5263   WHEN OTHERS THEN
5264     --
5265     ROLLBACK TO Find_Parent_Worksheet_Pvt ;
5266     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5267     --
5268     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
5269       FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME,
5270 				l_api_name);
5271     END if;
5272     --
5273     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
5274 				p_data  => p_msg_data );
5275   --
5276 END Find_Parent_Worksheet ;
5277 /*---------------------------------------------------------------------------*/
5278 
5279 
5280 /*===========================================================================+
5281  |                   PROCEDURE Find_Parent_Worksheets                        |
5282  +===========================================================================*/
5283 --
5284 -- The API finds parent worksheets of a given worksheet in a PL/SQL table.
5285 --
5286 PROCEDURE Find_Parent_Worksheets
5287 (
5288   p_api_version               IN       NUMBER   ,
5289   p_init_msg_list             IN       VARCHAR2 := FND_API.G_FALSE ,
5290   p_commit                    IN       VARCHAR2 := FND_API.G_FALSE ,
5291   p_validation_level          IN       NUMBER   := FND_API.G_VALID_LEVEL_FULL ,
5292   p_return_status             OUT  NOCOPY      VARCHAR2 ,
5293   p_msg_count                 OUT  NOCOPY      NUMBER   ,
5294   p_msg_data                  OUT  NOCOPY      VARCHAR2 ,
5295   --
5296   p_worksheet_id              IN       psb_worksheets.worksheet_id%TYPE ,
5297   p_worksheet_tbl             IN OUT  NOCOPY   Worksheet_Tbl_Type
5298 )
5299 IS
5300   --
5301   l_api_name                  CONSTANT VARCHAR2(30) := 'Find_Parent_Worksheets';
5302   l_api_version               CONSTANT NUMBER       :=  1.0 ;
5303   l_return_status             VARCHAR2(1) ;
5304   l_msg_count                 NUMBER      ;
5305   l_msg_data                  VARCHAR2(2000) ;
5306   --
5307   l_current_worksheet_id      psb_worksheets.global_worksheet_id%TYPE ;
5308   l_parent_worksheet_id       psb_worksheets.global_worksheet_id%TYPE ;
5309   --
5310   l_count                     NUMBER ;
5311   --
5312 BEGIN
5313   --
5314   SAVEPOINT Find_Parent_Worksheets_Pvt ;
5315   --
5316   IF NOT FND_API.Compatible_API_Call ( l_api_version,
5317 				       p_api_version,
5318 				       l_api_name,
5319 				       G_PKG_NAME )
5320   THEN
5321     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
5322   END IF;
5323   --
5324 
5325   IF FND_API.To_Boolean ( p_init_msg_list ) THEN
5326     FND_MSG_PUB.initialize ;
5327   END IF;
5328   --
5329   p_return_status := FND_API.G_RET_STS_SUCCESS ;
5330   --
5331 
5332   --
5333   -- Perform initialization
5334   --
5335   l_count := 0 ;
5336   p_worksheet_tbl.DELETE ;
5337   l_current_worksheet_id := p_worksheet_id ;
5338 
5339   LOOP
5340     --
5341     -- Find the parent worksheet for the current worksheet.
5342     --
5343     PSB_WS_Ops_Pvt.Find_Parent_Worksheet
5344     (
5345        p_api_version             =>   1.0 ,
5346        p_init_msg_list           =>   FND_API.G_FALSE,
5347        p_commit                  =>   FND_API.G_FALSE,
5348        p_validation_level        =>   FND_API.G_VALID_LEVEL_FULL,
5349        p_return_status           =>   l_return_status,
5350        p_msg_count               =>   l_msg_count,
5351        p_msg_data                =>   l_msg_data,
5352        --
5353        p_worksheet_id            =>   l_current_worksheet_id ,
5354        p_worksheet_id_OUT        =>   l_parent_worksheet_id
5355     );
5356     --
5357     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
5358       RAISE FND_API.G_EXC_ERROR ;
5359     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5360       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
5361     END IF;
5362     --
5363     IF l_parent_worksheet_id = 0 THEN
5364 
5365       -- It means all the parent worksheets has been retrieved.
5366       EXIT ;
5367       --
5368     ELSE
5369       --
5370       -- Insert the worksheet in the table.
5371       --
5372       l_count                  := l_count + 1 ;
5373       p_worksheet_tbl(l_count) := l_parent_worksheet_id ;
5374       l_current_worksheet_id   := l_parent_worksheet_id ;
5375     END IF ;
5376     --
5377   END LOOP ;
5378 
5379   --
5380   IF  FND_API.To_Boolean ( p_commit ) THEN
5381     COMMIT WORK;
5382   END IF;
5383   --
5384   FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
5385 			      p_data  => p_msg_data );
5386   --
5387 EXCEPTION
5388   --
5389   WHEN FND_API.G_EXC_ERROR THEN
5390     --
5391     ROLLBACK TO Find_Parent_Worksheets_Pvt ;
5392     p_return_status := FND_API.G_RET_STS_ERROR;
5393     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
5394 				p_data  => p_msg_data );
5395   --
5396   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5397     --
5398     ROLLBACK TO Find_Parent_Worksheets_Pvt ;
5399     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5400     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
5401 				p_data  => p_msg_data );
5402   --
5403   WHEN OTHERS THEN
5404     --
5405     ROLLBACK TO Find_Parent_Worksheets_Pvt ;
5406     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5407     --
5408     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
5409       FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME,
5410 				l_api_name);
5411     END if;
5412     --
5413     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
5414 				p_data  => p_msg_data );
5415   --
5416 END Find_Parent_Worksheets ;
5417 /*---------------------------------------------------------------------------*/
5418 
5419 
5420 /*===========================================================================+
5421  |                   PROCEDURE Find_Child_Worksheets                         |
5422  +===========================================================================*/
5423 --
5424 -- The API finds all the child worksheets of a worksheet in a PL/SQL table.
5425 --
5426 PROCEDURE Find_Child_Worksheets
5427 (
5428   p_api_version               IN       NUMBER   ,
5429   p_init_msg_list             IN       VARCHAR2 := FND_API.G_FALSE ,
5430   p_commit                    IN       VARCHAR2 := FND_API.G_FALSE ,
5431   p_validation_level          IN       NUMBER   := FND_API.G_VALID_LEVEL_FULL ,
5432   p_return_status             OUT  NOCOPY      VARCHAR2 ,
5433   p_msg_count                 OUT  NOCOPY      NUMBER   ,
5434   p_msg_data                  OUT  NOCOPY      VARCHAR2 ,
5435   --
5436   p_worksheet_id              IN       psb_worksheets.worksheet_id%TYPE ,
5437   p_worksheet_tbl             IN OUT  NOCOPY   Worksheet_Tbl_Type
5438 )
5439 IS
5440   --
5441   l_api_name                  CONSTANT VARCHAR2(30) := 'Find_Child_Worksheets';
5442   l_api_version               CONSTANT NUMBER       :=  1.0 ;
5443   l_return_status             VARCHAR2(1) ;
5444   l_msg_count                 NUMBER      ;
5445   l_msg_data                  VARCHAR2(2000) ;
5446   --
5447   l_child_worksheet_id        psb_worksheets.global_worksheet_id%TYPE ;
5448   l_global_worksheet_id       psb_worksheets.global_worksheet_id%TYPE ;
5449   l_global_worksheet_flag     psb_worksheets.global_worksheet_flag%TYPE ;
5450   l_budget_group_id           psb_worksheets.budget_group_id%TYPE ;
5451   l_budget_calendar_id        psb_worksheets.budget_calendar_id%TYPE ;
5452   --
5453   l_count                     NUMBER ;
5454   --
5455 BEGIN
5456   --
5457   SAVEPOINT Find_Child_Worksheets_Pvt ;
5458   --
5459   IF NOT FND_API.Compatible_API_Call ( l_api_version,
5460 				       p_api_version,
5461 				       l_api_name,
5462 				       G_PKG_NAME )
5463   THEN
5464     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
5465   END IF;
5466   --
5467 
5468   IF FND_API.To_Boolean ( p_init_msg_list ) THEN
5469     FND_MSG_PUB.initialize ;
5470   END IF;
5471   --
5472   p_return_status := FND_API.G_RET_STS_SUCCESS ;
5473   --
5474 
5475   --
5476   -- Perform initialization
5477   --
5478   l_count := 0 ;
5479   p_worksheet_tbl.DELETE ;
5480   --
5481 
5482   --
5483   -- Get worksheet information for the p_worksheet_id .
5484   --
5485   SELECT budget_group_id                       ,
5486 	 budget_calendar_id                    ,
5487 	 global_worksheet_id                   ,
5488 	 NVL( global_worksheet_flag ,  'N' )
5489     INTO
5490 	 l_budget_group_id                     ,
5491 	 l_budget_calendar_id                  ,
5492 	 l_global_worksheet_id                 ,
5493 	 l_global_worksheet_flag
5494   FROM   psb_worksheets
5495   WHERE  worksheet_id = p_worksheet_id ;
5496 
5497   IF l_global_worksheet_flag = 'Y' THEN
5498     l_global_worksheet_id := p_worksheet_id ;
5499   END IF ;
5500 
5501   --
5502   -- Use budget calendar related info to find all the budget groups down
5503   -- in the current hierarchy .
5504   --
5505   IF NVL(PSB_WS_Acct1.g_budget_calendar_id, -99) <> l_budget_calendar_id
5506   THEN
5507     --
5508     PSB_WS_Acct1.Cache_Budget_Calendar
5509     (
5510        p_return_status         =>  l_return_status ,
5511        p_budget_calendar_id    =>  l_budget_calendar_id
5512     );
5513     --
5514     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
5515       RAISE FND_API.G_EXC_ERROR ;
5516     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5517       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
5518     END IF;
5519     --
5520   END IF ;
5521 
5522   l_count  := 0 ;
5523 
5524   --
5525   -- Porcess all the lower level budget groups to fine worksheets.
5526   --
5527   FOR l_budget_group_rec IN
5528   (
5529      SELECT budget_group_id
5530        FROM psb_budget_groups
5531       WHERE budget_group_type          = 'R'
5532 	AND effective_start_date       <= PSB_WS_Acct1.g_startdate_pp
5533 	AND ((effective_end_date IS NULL)
5534 	      OR
5535 	     (effective_end_date >= PSB_WS_Acct1.g_enddate_cy))
5536      START WITH budget_group_id       = l_budget_group_id
5537      CONNECT BY PRIOR budget_group_id = parent_budget_group_id
5538   )
5539   LOOP
5540 
5541     --
5542     -- The hierarchial query will also return the l_budget_group_id.
5543     -- Do not consider it.
5544     --
5545     IF l_budget_group_rec.budget_group_id <> l_budget_group_id THEN
5546 
5547       l_child_worksheet_id := NULL ;
5548 
5549       --
5550       -- Get the worksheet_id at the current budget_group_level.
5551       --
5552       BEGIN
5553 
5554         --
5555         -- New way to find if a worksheet has been created for a budget group.
5556         -- ( Bug#2832148 )
5557         --
5558         SELECT worksheet_id INTO l_child_worksheet_id
5559         FROM   psb_worksheets
5560         WHERE  global_worksheet_id = l_global_worksheet_id
5561         AND    budget_group_id     = l_budget_group_rec.budget_group_id
5562         AND    worksheet_type      = 'O' ;
5563 
5564         /*
5565 	SELECT child_worksheet_id
5566 	  INTO l_child_worksheet_id
5567 	FROM psb_ws_distribution_details details, psb_ws_distributions distr
5568 	WHERE distr.worksheet_id = details.worksheet_id
5569              -- Bug No 2297742 Start
5570         --    AND    distr.distribution_option_flag   = 'W'
5571 	   AND nvl(distr.distribution_option_flag, 'W') = 'W'
5572         -- Bug No 2297742 End
5573 	   AND global_worksheet_id = l_global_worksheet_id
5574 	   AND child_budget_group_id = l_budget_group_rec.budget_group_id
5575 	   AND ROWNUM < 2 ;
5576         */
5577 
5578       EXCEPTION
5579 	WHEN no_data_found THEN
5580 	  --
5581 	  -- Means the worksheet has not been distributed to this level.
5582 	  -- Simply ignore it.
5583 	  --
5584 	  NULL ;
5585       END ;
5586 
5587       --
5588       -- Insert the worksheet in the p_worksheet_tbl table
5589       --
5590       IF l_child_worksheet_id IS NOT NULL THEN
5591 	l_count := l_count + 1 ;
5592 	p_worksheet_tbl( l_count ) := l_child_worksheet_id ;
5593       END IF ;
5594 
5595     END IF ;
5596 
5597   END LOOP ;
5598 
5599   --
5600   IF  FND_API.To_Boolean ( p_commit ) THEN
5601     COMMIT WORK;
5602   END IF;
5603   --
5604   FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
5605 			      p_data  => p_msg_data );
5606   --
5607 EXCEPTION
5608   --
5609   WHEN FND_API.G_EXC_ERROR THEN
5610     --
5611     ROLLBACK TO Find_Child_Worksheets_Pvt ;
5612     p_return_status := FND_API.G_RET_STS_ERROR;
5613     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
5614 				p_data  => p_msg_data );
5615   --
5616   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5617     --
5618     ROLLBACK TO Find_Child_Worksheets_Pvt ;
5619     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5620     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
5621 				p_data  => p_msg_data );
5622   --
5623   WHEN OTHERS THEN
5624     --
5625     ROLLBACK TO Find_Child_Worksheets_Pvt ;
5626     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5627     --
5628     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
5629       FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME,
5630 				l_api_name);
5631     END if;
5632     --
5633     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
5634 				p_data  => p_msg_data );
5635   --
5636 END Find_Child_Worksheets ;
5637 /*---------------------------------------------------------------------------*/
5638 
5639 
5640 /*===========================================================================+
5641  |                     PROCEDURE Update_Worksheet                            |
5642  +===========================================================================*/
5643 --
5644 -- The API takes 2 worksheets, source and target. It updates target worksheet
5645 -- by adding new account or position lines if they are their in the source
5646 -- worksheet and not in the target worksheet. It also updates the worksheet
5647 -- submission related columns in the source worksheet.
5648 --
5649 PROCEDURE Update_Worksheet
5650 (
5651   p_api_version               IN       NUMBER   ,
5652   p_init_msg_list             IN       VARCHAR2 := FND_API.G_FALSE ,
5653   p_commit                    IN       VARCHAR2 := FND_API.G_FALSE ,
5654   p_validation_level          IN       NUMBER   := FND_API.G_VALID_LEVEL_FULL ,
5655   p_return_status             OUT  NOCOPY      VARCHAR2 ,
5656   p_msg_count                 OUT  NOCOPY      NUMBER   ,
5657   p_msg_data                  OUT  NOCOPY      VARCHAR2 ,
5658   --
5659   p_source_worksheet_id       IN       psb_worksheets.worksheet_id%TYPE  ,
5660   p_target_worksheet_id       IN       psb_worksheets.worksheet_id%TYPE
5661 )
5662 IS
5663   --
5664   l_api_name                     CONSTANT VARCHAR2(30) := 'Update_Worksheet' ;
5665   l_api_version                  CONSTANT NUMBER       :=  1.0 ;
5666   --
5667   l_return_status                VARCHAR2(1) ;
5668   l_msg_count                    NUMBER ;
5669   l_msg_data                     VARCHAR2(2000) ;
5670   --
5671   l_source_budget_group_id       psb_worksheets.budget_group_id%TYPE ;
5672   l_source_local_copy_flag       psb_worksheets.local_copy_flag%TYPE ;
5673   l_source_global_worksheet_id   psb_worksheets.worksheet_id%TYPE ;
5674   l_source_global_worksheet_flag psb_worksheets.global_worksheet_flag%TYPE ;
5675   l_source_budget_by_position    psb_worksheets.budget_by_position%TYPE ;
5676   --
5677   l_target_budget_group_id       psb_worksheets.budget_group_id%TYPE ;
5678   l_target_local_copy_flag       psb_worksheets.local_copy_flag%TYPE ;
5679   l_target_global_worksheet_id   psb_worksheets.worksheet_id%TYPE ;
5680   --
5681   l_budget_calendar_id           psb_worksheets.budget_calendar_id%TYPE ;
5682   l_ws_lines_rec                 psb_ws_lines%ROWTYPE ;
5683   l_ws_lines_positions_rec       psb_ws_lines_positions%ROWTYPE ;
5684 /* Bug No 2378285 Start */
5685   l_gl_cutoff_period             DATE;
5686 /* Bug No 2378285 End */
5687 
5688 BEGIN
5689   --
5690   SAVEPOINT Update_Worksheet_Pvt ;
5691   --
5692   IF NOT FND_API.Compatible_API_Call ( l_api_version,
5693 				       p_api_version,
5694 				       l_api_name,
5695 				       G_PKG_NAME )
5696   THEN
5697     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
5698   END IF;
5699   --
5700 
5701   IF FND_API.To_Boolean ( p_init_msg_list ) THEN
5702     FND_MSG_PUB.initialize ;
5703   END IF;
5704   --
5705   p_return_status := FND_API.G_RET_STS_SUCCESS ;
5706   --
5707 
5708   --
5709   -- Find the source worksheet information.
5710   --
5711   SELECT budget_group_id                      ,
5712 	 global_worksheet_id                  ,
5713 	 NVL( global_worksheet_flag ,  'N' )  ,
5714 	 NVL( local_copy_flag ,        'N' )  ,
5715 	 NVL( budget_by_position ,     'N' ),
5716 /* Bug No 2378285 Start */
5717 	 gl_cutoff_period
5718 /* Bug No 2378285 End */
5719        INTO
5720 	 l_source_budget_group_id             ,
5721 	 l_source_global_worksheet_id         ,
5722 	 l_source_global_worksheet_flag       ,
5723 	 l_source_local_copy_flag             ,
5724 	 l_source_budget_by_position,
5725 /* Bug No 2378285 Start */
5726 	 l_gl_cutoff_period
5727 /* Bug No 2378285 End */
5728   FROM   psb_worksheets
5729   WHERE  worksheet_id = p_source_worksheet_id ;
5730 
5731   IF l_source_global_worksheet_flag = 'Y' THEN
5732     l_source_global_worksheet_id := p_source_worksheet_id ;
5733   END IF ;
5734 
5735 /* Bug No 2378285 Start */
5736 -- Moved the 'update_worksheet' after the select statement
5737 -- and added gl_cutoff_period as another input parameter
5738 
5739   --
5740   -- Update worksheet submission related columns in the target worksheet.
5741   --
5742   PSB_Worksheet_Pvt.Update_Worksheet
5743   (
5744      p_api_version                 => 1.0 ,
5745      p_init_msg_list               => FND_API.G_FALSE,
5746      p_commit                      => FND_API.G_FALSE,
5747      p_validation_level            => FND_API.G_VALID_LEVEL_NONE,
5748      p_return_status               => l_return_status,
5749      p_msg_count                   => l_msg_count,
5750      p_msg_data                    => l_msg_data ,
5751      --
5752      p_worksheet_id                => p_target_worksheet_id ,
5753      p_gl_cutoff_period            => l_gl_cutoff_period,
5754      p_date_submitted              => NULL ,
5755      p_submitted_by                => NULL
5756   );
5757   --
5758   IF l_return_status = FND_API.G_RET_STS_ERROR THEN
5759     RAISE FND_API.G_EXC_ERROR ;
5760   ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5761     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
5762   END IF;
5763 
5764 /* Bug No 2378285 End */
5765 
5766   --
5767   -- Find the target worksheet information. The target worksheet will
5768   -- never be the top worksheet i.e. global_worksheet_flag is always 'N'.
5769   --
5770   SELECT budget_group_id         ,
5771 	 global_worksheet_id     ,
5772 	 NVL( local_copy_flag ,  'N' )
5773       INTO
5774 	 l_target_budget_group_id    ,
5775 	 l_target_global_worksheet_id       ,
5776 	 l_target_local_copy_flag
5777   FROM   psb_worksheets
5778   WHERE  worksheet_id = p_target_worksheet_id ;
5779 
5780   --
5781   -- The source and target worksheet must be part of same global worksheet.
5782   -- Both should be non-global worksheets also.
5783   --
5784   IF NOT (
5785 	   (l_source_local_copy_flag='N') AND ( l_target_local_copy_flag='N')
5786 	   AND ( l_source_global_worksheet_id = l_target_global_worksheet_id )
5787 	  )
5788   THEN
5789     Fnd_Message.Set_Name ('PSB', 'PSB_INCOMPATIBLE_WORKSHEETS') ;
5790     FND_MSG_PUB.Add;
5791     RAISE FND_API.G_EXC_ERROR ;
5792   END IF ;
5793 
5794   --
5795   -- Get budget calendar for the global worksheet.
5796   --
5797   SELECT budget_calendar_id INTO l_budget_calendar_id
5798   FROM   psb_worksheets
5799   WHERE  worksheet_id = l_source_global_worksheet_id ;
5800 
5801   --
5802   -- Get budget calendar related info to find all the budget groups down in
5803   -- the current hierarchy to get all the CCIDs for the current budget group.
5804   --
5805   IF NVL(PSB_WS_Acct1.g_budget_calendar_id, -99) <> l_budget_calendar_id
5806   THEN
5807     --
5808     PSB_WS_Acct1.Cache_Budget_Calendar
5809     (
5810        p_return_status         =>  l_return_status ,
5811        p_budget_calendar_id    =>  l_budget_calendar_id
5812     );
5813     --
5814     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
5815       RAISE FND_API.G_EXC_ERROR ;
5816     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5817       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
5818     END IF;
5819     --
5820   END IF ;
5821 
5822   --
5823   -- Find account_line_id to be inserted into target worksheet.
5824   -- ( The hierarchial query will select lines falling in the subtreee, the
5825   --   target worksheet belongs. We will not consider other lines. )
5826   --
5827   FOR l_account_line_id_rec IN
5828   (
5829      SELECT lines.account_line_id
5830      FROM   psb_ws_lines         lines ,
5831 	    psb_ws_account_lines accts
5832      WHERE  lines.worksheet_id    = p_source_worksheet_id
5833      AND    lines.account_line_id = accts.account_line_id
5834      /*For Bug No : 2236283 Start*/
5835      /*
5836      AND    accts.budget_group_id  IN
5837 	       (  SELECT budget_group_id
5838 		    FROM psb_budget_groups
5839 		   WHERE budget_group_type = 'R'
5840 		     AND effective_start_date <= PSB_WS_Acct1.g_startdate_pp
5841 		     AND ((effective_end_date IS NULL)
5842 			   OR
5843 			  (effective_end_date >= PSB_WS_Acct1.g_enddate_cy))
5844 		  START WITH budget_group_id       = l_target_budget_group_id
5845 		  CONNECT BY PRIOR budget_group_id = parent_budget_group_id
5846 	       )
5847      */
5848      AND EXISTS
5849 	       (  SELECT 1
5850 		    FROM psb_budget_groups
5851 		   WHERE budget_group_type = 'R'
5852 		     AND budget_group_id = accts.budget_group_id
5853 		     AND effective_start_date <= PSB_WS_Acct1.g_startdate_pp
5854 		     AND ((effective_end_date IS NULL)
5855 			   OR
5856 			  (effective_end_date >= PSB_WS_Acct1.g_enddate_cy))
5857 		  START WITH budget_group_id       = l_target_budget_group_id
5858 		  CONNECT BY PRIOR budget_group_id = parent_budget_group_id
5859 	       )
5860      /*For Bug No : 2236283 End*/
5861      MINUS
5862      SELECT lines.account_line_id
5863      FROM   psb_ws_lines lines
5864      WHERE  worksheet_id = p_target_worksheet_id
5865   )
5866   LOOP
5867 
5868     SELECT * INTO l_ws_lines_rec
5869     FROM   psb_ws_lines
5870     WHERE  worksheet_id    = p_source_worksheet_id
5871     AND    account_line_id = l_account_line_id_rec.account_line_id ;
5872 
5873     --
5874     -- Each account_line_id found is the account_line_id missing in the
5875     -- target worksheet. Add the account_line_id to the target worksheet.
5876     --
5877     Insert_WS_Lines_Pvt
5878     (
5879        p_worksheet_id       =>  p_target_worksheet_id,
5880        p_account_line_id    =>  l_ws_lines_rec.account_line_id ,
5881        p_freeze_flag        =>  l_ws_lines_rec.freeze_flag ,
5882        p_view_line_flag     =>  l_ws_lines_rec.view_line_flag ,
5883        p_last_update_date   =>  g_current_date,
5884        p_last_updated_by    =>  g_current_user_id,
5885        p_last_update_login  =>  g_current_login_id,
5886        p_created_by         =>  g_current_user_id,
5887        p_creation_date      =>  g_current_date,
5888        p_return_status      =>  l_return_status
5889     ) ;
5890     --
5891     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
5892       RAISE FND_API.G_EXC_ERROR ;
5893     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5894       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
5895     END IF;
5896     --
5897   END LOOP ;
5898 
5899   --
5900   -- Find position_line_id to be inserted into target worksheet.
5901   -- ( The hierarchial query will select lines falling in the sub-tree,
5902   --   the target worksheet belongs. We will not consider other lines. )
5903   --
5904   IF l_source_budget_by_position = 'Y' THEN
5905     --
5906     FOR l_lines_pos_rec IN
5907     (
5908        SELECT lines.position_line_id
5909        FROM   psb_ws_lines_positions lines ,
5910 	      psb_ws_position_lines  pos
5911        WHERE  lines.worksheet_id     = p_source_worksheet_id
5912        AND    lines.position_line_id = pos.position_line_id
5913        /*For Bug No : 2236283 Start*/
5914        /*
5915        AND    lines.position_line_id IN
5916 	      (
5917 		SELECT acct_lines.position_line_id
5918 		FROM   psb_ws_account_lines acct_lines
5919 		WHERE  acct_lines.budget_group_id IN
5920 		       (
5921 			 SELECT bg.budget_group_id
5922 			   FROM psb_budget_groups bg
5923 			  WHERE bg.budget_group_type = 'R'
5924 			    AND effective_start_date <= PSB_WS_Acct1.g_startdate_pp
5925 			    AND ((effective_end_date IS NULL)
5926 				  OR
5927 				 (effective_end_date >= PSB_WS_Acct1.g_enddate_cy))
5928 			 START WITH bg.budget_group_id = l_target_budget_group_id
5929 			 CONNECT BY PRIOR bg.budget_group_id = bg.parent_budget_group_id
5930 		       )
5931 	      )
5932        */
5933        AND    EXISTS
5934 	      (
5935 		SELECT 1
5936 		  FROM psb_ws_account_lines acct_lines
5937 		 WHERE acct_lines.position_line_id = lines.position_line_id
5938 		   AND EXISTS
5939 		       (
5940 			 SELECT 1
5941 			   FROM psb_budget_groups bg
5942 			  WHERE bg.budget_group_type = 'R'
5943 			    AND bg.budget_group_id = acct_lines.budget_group_id
5944 			    AND effective_start_date <= PSB_WS_Acct1.g_startdate_pp
5945 			    AND ((effective_end_date IS NULL)
5946 				  OR
5947 				 (effective_end_date >= PSB_WS_Acct1.g_enddate_cy))
5948 			 START WITH bg.budget_group_id = l_target_budget_group_id
5949 			 CONNECT BY PRIOR bg.budget_group_id = bg.parent_budget_group_id
5950 		       )
5951 	      )
5952        /*For Bug No : 2236283 End*/
5953        MINUS
5954        SELECT position_line_id
5955        FROM   psb_ws_lines_positions
5956        WHERE  worksheet_id = p_target_worksheet_id
5957     )
5958     LOOP
5959 
5960       SELECT * INTO l_ws_lines_positions_rec
5961       FROM   psb_ws_lines_positions
5962       WHERE  worksheet_id     = p_source_worksheet_id
5963       AND    position_line_id = l_lines_pos_rec.position_line_id ;
5964 
5965       --
5966       -- Each position_line_id found is the one missing in the target
5967       -- worksheet. Add it to the target worksheet.
5968       --
5969       PSB_WS_Pos_Pvt.Create_Position_Matrix
5970       (
5971 	 p_api_version        =>  1.0 ,
5972 	 p_init_msg_list      =>  FND_API.G_FALSE ,
5973 	 p_validation_level   =>  FND_API.G_VALID_LEVEL_NONE ,
5974 	 p_return_status      =>  l_return_status ,
5975 	 p_msg_count          =>  l_msg_count ,
5976 	 p_msg_data           =>  l_msg_data ,
5977 	 --
5978 	 p_worksheet_id       =>  p_target_worksheet_id ,
5979 	 p_position_line_id   =>  l_ws_lines_positions_rec.position_line_id ,
5980 	 p_freeze_flag        =>  l_ws_lines_positions_rec.freeze_flag ,
5981 	 p_view_line_flag     =>  l_ws_lines_positions_rec.view_line_flag
5982       ) ;
5983       --
5984       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
5985 	RAISE FND_API.G_EXC_ERROR ;
5986       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5987 	RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
5988       END IF;
5989       --
5990     END LOOP ;
5991 
5992   END IF ;
5993 
5994   --
5995   IF  FND_API.To_Boolean ( p_commit ) THEN
5996     COMMIT WORK;
5997   END IF;
5998   --
5999   FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
6000 			      p_data  => p_msg_data );
6001   --
6002 EXCEPTION
6003   --
6004   WHEN FND_API.G_EXC_ERROR THEN
6005     --
6006     ROLLBACK TO Update_Worksheet_Pvt ;
6007     p_return_status := FND_API.G_RET_STS_ERROR;
6008     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
6009 				p_data  => p_msg_data );
6010   --
6011   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6012     --
6013     ROLLBACK TO Update_Worksheet_Pvt ;
6014     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6015     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
6016 				p_data  => p_msg_data );
6017   --
6018   WHEN OTHERS THEN
6019     --
6020     ROLLBACK TO Update_Worksheet_Pvt ;
6021     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6022     --
6023 
6024     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
6025       FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME,
6026 				l_api_name);
6027     END if;
6028     --
6029     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
6030 				p_data  => p_msg_data );
6031     --
6032 END Update_Worksheet ;
6033 /*---------------------------------------------------------------------------*/
6034 
6035 
6036 /*===========================================================================+
6037  |               PROCEDURE Create_Local_Dist_Pvt (Private)                   |
6038  +===========================================================================*/
6039 --
6040 -- Private procedure to print debug info. The name is tried to keep as
6041 -- short as possible for better documentaion.
6042 --
6043 PROCEDURE Create_Local_Dist_Pvt
6044 (
6045   p_account_line_id        IN   psb_ws_lines.account_line_id%TYPE            ,
6046   p_new_worksheet_id       IN   psb_worksheets.worksheet_id%TYPE             ,
6047   p_new_position_line_id   IN   psb_ws_lines_positions.position_line_id%TYPE ,
6048   p_return_status          OUT  NOCOPY  VARCHAR2
6049  )
6050 IS
6051   --
6052   l_api_name                  CONSTANT VARCHAR2(30) := 'Create_Local_Dist' ;
6053   --
6054   l_return_status             VARCHAR2(1) ;
6055   l_msg_count                 NUMBER ;
6056   l_msg_data                  VARCHAR2(2000) ;
6057   --
6058   l_dummy_account_line_id       psb_ws_account_lines.account_line_id%TYPE ;
6059   l_period_amount_tbl         PSB_WS_Acct1.g_prdamt_tbl_type ;
6060   --
6061 BEGIN
6062 
6063   SAVEPOINT Create_Local_Dist_Pvt ;
6064 
6065   p_return_status := FND_API.G_RET_STS_SUCCESS ;
6066 
6067   FOR l_accts_rec IN
6068   (
6069      SELECT *
6070      FROM   psb_ws_account_lines
6071      WHERE  account_line_id  = p_account_line_id
6072   )
6073   LOOP
6074     --
6075     -- Populate the l_period_amount_tbl ( used by PSB_WS_Acct1 API )
6076     --
6077     l_period_amount_tbl(1)  := l_accts_rec.period1_amount ;
6078     l_period_amount_tbl(2)  := l_accts_rec.period2_amount ;
6079     l_period_amount_tbl(3)  := l_accts_rec.period3_amount ;
6080     l_period_amount_tbl(4)  := l_accts_rec.period4_amount ;
6081     l_period_amount_tbl(5)  := l_accts_rec.period5_amount ;
6082     l_period_amount_tbl(6)  := l_accts_rec.period6_amount ;
6083     l_period_amount_tbl(7)  := l_accts_rec.period7_amount ;
6084     l_period_amount_tbl(8)  := l_accts_rec.period8_amount ;
6085     l_period_amount_tbl(9)  := l_accts_rec.period9_amount ;
6086     l_period_amount_tbl(10) := l_accts_rec.period10_amount ;
6087     l_period_amount_tbl(11) := l_accts_rec.period11_amount ;
6088     l_period_amount_tbl(12) := l_accts_rec.period12_amount ;
6089     l_period_amount_tbl(13) := l_accts_rec.period13_amount ;
6090     l_period_amount_tbl(14) := l_accts_rec.period14_amount ;
6091     l_period_amount_tbl(15) := l_accts_rec.period15_amount ;
6092     l_period_amount_tbl(16) := l_accts_rec.period16_amount ;
6093     l_period_amount_tbl(17) := l_accts_rec.period17_amount ;
6094     l_period_amount_tbl(18) := l_accts_rec.period18_amount ;
6095     l_period_amount_tbl(19) := l_accts_rec.period19_amount ;
6096     l_period_amount_tbl(20) := l_accts_rec.period20_amount ;
6097     l_period_amount_tbl(21) := l_accts_rec.period21_amount ;
6098     l_period_amount_tbl(22) := l_accts_rec.period22_amount ;
6099     l_period_amount_tbl(23) := l_accts_rec.period23_amount ;
6100     l_period_amount_tbl(24) := l_accts_rec.period24_amount ;
6101     l_period_amount_tbl(25) := l_accts_rec.period25_amount ;
6102     l_period_amount_tbl(26) := l_accts_rec.period26_amount ;
6103     l_period_amount_tbl(27) := l_accts_rec.period27_amount ;
6104     l_period_amount_tbl(28) := l_accts_rec.period28_amount ;
6105     l_period_amount_tbl(29) := l_accts_rec.period29_amount ;
6106     l_period_amount_tbl(30) := l_accts_rec.period30_amount ;
6107     l_period_amount_tbl(31) := l_accts_rec.period31_amount ;
6108     l_period_amount_tbl(32) := l_accts_rec.period32_amount ;
6109     l_period_amount_tbl(33) := l_accts_rec.period33_amount ;
6110     l_period_amount_tbl(34) := l_accts_rec.period34_amount ;
6111     l_period_amount_tbl(35) := l_accts_rec.period35_amount ;
6112     l_period_amount_tbl(36) := l_accts_rec.period36_amount ;
6113     l_period_amount_tbl(37) := l_accts_rec.period37_amount ;
6114     l_period_amount_tbl(38) := l_accts_rec.period38_amount ;
6115     l_period_amount_tbl(39) := l_accts_rec.period39_amount ;
6116     l_period_amount_tbl(40) := l_accts_rec.period40_amount ;
6117     l_period_amount_tbl(41) := l_accts_rec.period41_amount ;
6118     l_period_amount_tbl(42) := l_accts_rec.period42_amount ;
6119     l_period_amount_tbl(43) := l_accts_rec.period43_amount ;
6120     l_period_amount_tbl(44) := l_accts_rec.period44_amount ;
6121     l_period_amount_tbl(45) := l_accts_rec.period45_amount ;
6122     l_period_amount_tbl(46) := l_accts_rec.period46_amount ;
6123     l_period_amount_tbl(47) := l_accts_rec.period47_amount ;
6124     l_period_amount_tbl(48) := l_accts_rec.period48_amount ;
6125     l_period_amount_tbl(49) := l_accts_rec.period49_amount ;
6126     l_period_amount_tbl(50) := l_accts_rec.period50_amount ;
6127     l_period_amount_tbl(51) := l_accts_rec.period51_amount ;
6128     l_period_amount_tbl(52) := l_accts_rec.period52_amount ;
6129     l_period_amount_tbl(53) := l_accts_rec.period53_amount ;
6130     l_period_amount_tbl(54) := l_accts_rec.period54_amount ;
6131     l_period_amount_tbl(55) := l_accts_rec.period55_amount ;
6132     l_period_amount_tbl(56) := l_accts_rec.period56_amount ;
6133     l_period_amount_tbl(57) := l_accts_rec.period57_amount ;
6134     l_period_amount_tbl(58) := l_accts_rec.period58_amount ;
6135     l_period_amount_tbl(59) := l_accts_rec.period59_amount ;
6136     l_period_amount_tbl(60) := l_accts_rec.period60_amount ;
6137 
6138     --
6139     -- Create records in psb_ws_lines and psb_ws_account_lines for the
6140     -- new worksheet.
6141     --
6142     PSB_WS_Acct_Pvt.Create_Account_Dist
6143     (
6144       p_api_version             =>   1.0 ,
6145       p_init_msg_list           =>   FND_API.G_FALSE,
6146       p_commit                  =>   FND_API.G_FALSE,
6147       p_validation_level        =>   FND_API.G_VALID_LEVEL_FULL,
6148       p_return_status           =>   l_return_status,
6149       p_account_line_id         =>   l_dummy_account_line_id,
6150       p_msg_count               =>   l_msg_count,
6151       p_msg_data                =>   l_msg_data,
6152       --
6153       p_worksheet_id            =>   p_new_worksheet_id,
6154       p_budget_year_id          =>   l_accts_rec.budget_year_id,
6155       p_budget_group_id         =>   l_accts_rec.budget_group_id,
6156       p_ccid                    =>   l_accts_rec.code_combination_id,
6157       p_template_id             =>   NVL(l_accts_rec.template_id ,
6158 					  FND_API.G_MISS_NUM ) ,
6159       p_currency_code           =>   l_accts_rec.currency_code ,
6160       p_balance_type            =>   l_accts_rec.balance_type ,
6161       p_ytd_amount              =>   l_accts_rec.ytd_amount  ,
6162       p_distribute_flag         =>   FND_API.G_FALSE ,
6163       p_annual_fte              =>   NVL ( l_accts_rec.annual_fte,
6164 					   FND_API.G_MISS_NUM ) ,
6165       p_period_amount           =>   l_period_amount_tbl ,
6166       p_position_line_id        =>   NVL( p_new_position_line_id ,
6167 					  FND_API.G_MISS_NUM ) ,
6168       p_element_set_id          =>   NVL( l_accts_rec.element_set_id,
6169 					  FND_API.G_MISS_NUM ) ,
6170       p_salary_account_line     =>   NVL( l_accts_rec.salary_account_line,
6171 					  FND_API.G_FALSE ) ,
6172       p_service_package_id      =>   l_accts_rec.service_package_id ,
6173       p_start_stage_seq         =>   l_accts_rec.start_stage_seq ,
6174       p_current_stage_seq       =>   l_accts_rec.current_stage_seq ,
6175       p_end_stage_seq           =>   NVL( l_accts_rec.end_stage_seq,
6176 					  FND_API.G_MISS_NUM ) ,
6177       p_copy_of_account_line_id =>   l_accts_rec.account_line_id
6178     );
6179     --
6180     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6181       --
6182       ROLLBACK TO Create_Local_Dist_Pvt ;
6183       p_return_status := l_return_status ;
6184       --
6185     END IF;
6186     --
6187   END LOOP;
6188 
6189 EXCEPTION
6190   --
6191  WHEN OTHERS THEN
6192     --
6193     ROLLBACK TO Create_Local_Dist_Pvt ;
6194     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6195     --
6196     --
6197     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
6198       FND_MSG_PUB.Add_Exc_Msg (  G_PKG_NAME,
6199 				 l_api_name );
6200     END IF;
6201     --
6202 END Create_Local_Dist_Pvt ;
6203 /*---------------------------------------------------------------------------*/
6204 
6205 
6206 /*===========================================================================+
6207  |                 PROCEDURE Insert_WS_Lines_Pvt ( Private )                 |
6208  +===========================================================================*/
6209 --
6210 -- The private procedure inserts a new record in psb_ws_lines table.
6211 --
6212 PROCEDURE Insert_WS_Lines_Pvt
6213 (
6214   p_worksheet_id              IN       psb_ws_lines.worksheet_id%TYPE,
6215   p_account_line_id           IN       psb_ws_lines.account_line_id%TYPE,
6216   p_freeze_flag               IN       psb_ws_lines.freeze_flag%TYPE,
6217   p_view_line_flag            IN       psb_ws_lines.view_line_flag%TYPE,
6218   p_last_update_date          IN       psb_ws_lines.last_update_date%TYPE,
6219   p_last_updated_by           IN       psb_ws_lines.last_updated_by%TYPE,
6220   p_last_update_login         IN       psb_ws_lines.last_update_login%TYPE,
6221   p_created_by                IN       psb_ws_lines.created_by%TYPE,
6222   p_creation_date             IN       psb_ws_lines.creation_date%TYPE,
6223   p_return_status             OUT  NOCOPY      VARCHAR2
6224 )
6225 IS
6226   --
6227   l_api_name                  CONSTANT VARCHAR2(30) := 'Insert_WS_Lines_Pvt' ;
6228   --
6229 BEGIN
6230   --
6231   p_return_status := FND_API.G_RET_STS_SUCCESS ;
6232   --
6233   update psb_ws_lines
6234      set freeze_flag       = p_freeze_flag,
6235 	 view_line_flag    = p_view_line_flag,
6236 	 last_update_date  = g_current_date,
6237 	 last_updated_by   = g_current_user_id,
6238 	 last_update_login = g_current_login_id
6239    where account_line_id   = p_account_line_id
6240      and worksheet_id      = p_worksheet_id;
6241 
6242   IF SQL%NOTFOUND THEN
6243 
6244     INSERT INTO psb_ws_lines
6245 	   (
6246 	     worksheet_id,
6247 	     account_line_id,
6248 	     freeze_flag,
6249 	     view_line_flag,
6250 	     last_update_date,
6251 	     last_updated_by,
6252 	     last_update_login,
6253 	     created_by,
6254 	     creation_date
6255 	   )
6256 	 VALUES
6257 	   ( p_worksheet_id,
6258 	     p_account_line_id,
6259 	     p_freeze_flag,
6260 	     p_view_line_flag,
6261 	     g_current_date,
6262 	     g_current_user_id,
6263 	     g_current_login_id,
6264 	     g_current_user_id,
6265 	     g_current_date
6266 	   );
6267 
6268   END IF;
6269   --
6270 EXCEPTION
6271   --
6272   WHEN OTHERS THEN
6273     --
6274     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6275     --
6276     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
6277       FND_MSG_PUB.Add_Exc_Msg (  G_PKG_NAME,
6278 				 l_api_name );
6279     END IF;
6280     --
6281 END Insert_WS_Lines_Pvt ;
6282 /*---------------------------------------------------------------------------*/
6283 
6284 
6285 /*===========================================================================+
6286  |                 PROCEDURE Delete_Worksheet_Pvt ( Private )                |
6287  +===========================================================================*/
6288 --
6289 -- This API deletes an official worksheet by performing deletes on
6290 -- psb_worksheets and matrix tables (psb_ws_lines and psb_ws_lines_positions).
6291 -- It also deletes worksheet related data from other tables.
6292 --
6293 PROCEDURE Delete_Worksheet_Pvt
6294 (
6295   p_worksheet_id              IN      psb_worksheets.worksheet_id%TYPE       ,
6296   p_budget_by_position        IN      psb_worksheets.budget_by_position%TYPE ,
6297   p_delete_lines_flag         IN      VARCHAR2 ,
6298   p_return_status             OUT  NOCOPY     VARCHAR2
6299 )
6300 IS
6301   --
6302   l_api_name              CONSTANT    VARCHAR2(30)  := 'Delete_Worksheet_Pvt' ;
6303   --
6304   l_account_line_id       psb_ws_lines.account_line_id%TYPE;
6305   l_position_line_id      psb_ws_lines_positions.position_line_id%TYPE ;
6306   l_local_copy_flag       psb_worksheets.local_copy_flag%TYPE;
6307   l_budget_by_position    psb_worksheets.budget_by_position%TYPE ;
6308   l_return_status         VARCHAR2(1) ;
6309   /*For Bug No : 2266309 Start*/
6310   l_record_ctr            NUMBER := 0;
6311   /*For Bug No : 2266309 End*/
6312 
6313   --
6314   CURSOR l_ws_account_lines_csr
6315     IS
6316     SELECT account_line_id
6317     FROM   psb_ws_lines
6318     WHERE  worksheet_id = p_worksheet_id;
6319 
6320   CURSOR l_ws_position_lines_csr
6321     IS
6322     SELECT position_line_id
6323     FROM   psb_ws_lines_positions
6324     WHERE  worksheet_id = p_worksheet_id;
6325 
6326 BEGIN
6327   /*For Bug No : 2266309 Start*/
6328   SAVEPOINT Delete_Worksheet_Pvt ;
6329   /*For Bug No : 2266309 Start*/
6330   --
6331   p_return_status := FND_API.G_RET_STS_SUCCESS ;
6332 
6333   --
6334   -- p_delete_lines_flag specifies whether psb_ws_account_lines and
6335   -- psb_ws_position_lines related tables will be deleted or not.
6336   --
6337   IF p_delete_lines_flag = 'Y' THEN
6338 
6339     --
6340     -- Deleting account related information.
6341     --
6342     OPEN l_ws_account_lines_csr;
6343 
6344     LOOP
6345       --
6346       FETCH l_ws_account_lines_csr INTO l_account_line_id;
6347 
6348       IF (l_ws_account_lines_csr%NOTFOUND) THEN
6349 	EXIT;
6350       END IF;
6351 
6352       --Deleting records from psb_ws_account_lines.
6353       DELETE psb_ws_account_lines
6354       WHERE  account_line_id = l_account_line_id;
6355       --
6356       /*For Bug No : 2266309 Start*/
6357       l_record_ctr := l_record_ctr + 1;
6358       IF l_record_ctr = PSB_WS_ACCT1.g_checkpoint_save THEN
6359 	COMMIT WORK;
6360 	l_record_ctr := 0;
6361 	SAVEPOINT Delete_Worksheet_Pvt;
6362       END IF;
6363       /*For Bug No : 2266309 End*/
6364 
6365     END LOOP;
6366 
6367     CLOSE l_ws_account_lines_csr;
6368 
6369     --
6370     -- Deleting position related information.
6371     --
6372 
6373     IF ( p_budget_by_position = 'Y' ) THEN
6374       --
6375       OPEN l_ws_position_lines_csr ;
6376 
6377       LOOP
6378 	--
6379 	FETCH l_ws_position_lines_csr INTO l_position_line_id;
6380 
6381 	IF ( l_ws_position_lines_csr%NOTFOUND ) THEN
6382 	  EXIT;
6383 	END IF;
6384 
6385 	-- Deleting records from psb_ws_position_lines.
6386 	DELETE psb_ws_position_lines
6387 	WHERE  position_line_id = l_position_line_id;
6388 
6389 	-- Deleting records from psb_ws_fte_lines.
6390 	DELETE psb_ws_fte_lines
6391 	WHERE  position_line_id = l_position_line_id;
6392 
6393 	-- Deleting records from psb_ws_element_lines.
6394 	DELETE psb_ws_element_lines
6395 	WHERE  position_line_id = l_position_line_id;
6396 
6397 	/*For Bug No : 2266309 Start*/
6398 	l_record_ctr := l_record_ctr + 3;
6399 	IF l_record_ctr >= PSB_WS_ACCT1.g_checkpoint_save THEN
6400 	  COMMIT WORK;
6401 	  l_record_ctr := 0;
6402 	  SAVEPOINT Delete_Worksheet_Pvt;
6403 	END IF;
6404 	/*For Bug No : 2266309 End*/
6405 
6406       END LOOP;
6407 
6408       CLOSE l_ws_position_lines_csr ;
6409 
6410     END IF ;    -- if p_budget_by_position is 'Y'.
6411     --
6412   END IF;       -- if p_delete_lines_flag is 'Y'.
6413 
6414   /*For Bug No : 2266309 Start*/
6415   IF l_record_ctr > 0 THEN
6416     COMMIT WORK;
6417     SAVEPOINT Delete_Worksheet_Pvt;
6418   END IF;
6419   /*For Bug No : 2266309 End*/
6420 
6421   --
6422   -- Delete worksheet related account lines from psb_ws_lines ( Account Matrix).
6423   --
6424   DELETE psb_ws_lines
6425   WHERE  worksheet_id = p_worksheet_id ;
6426 
6427   /*For Bug No : 2266309 Start*/
6428   COMMIT WORK;
6429   SAVEPOINT Delete_Worksheet_Pvt;
6430   /*For Bug No : 2266309 End*/
6431 
6432   --
6433   -- Delete worksheet related position lines from psb_ws_lines_positions
6434   -- ( Position Matrix).
6435   --
6436   IF p_budget_by_position = 'Y' THEN
6437     --
6438     DELETE psb_ws_lines_positions
6439     WHERE  worksheet_id = p_worksheet_id ;
6440     --
6441     /*For Bug No : 2266309 Start*/
6442     COMMIT WORK;
6443     SAVEPOINT Delete_Worksheet_Pvt;
6444     /*For Bug No : 2266309 End*/
6445 
6446   END IF;
6447 
6448   -- Delete from psb_ws_distribution_details.
6449   DELETE psb_ws_distribution_details
6450   WHERE  child_worksheet_id = p_worksheet_id ;
6451 
6452   -- Delete from psb_workflow_processes.
6453   DELETE psb_workflow_processes
6454   WHERE  worksheet_id = p_worksheet_id ;
6455 
6456   -- Delete from psb_ws_distributions.
6457   DELETE psb_ws_distributions
6458   WHERE  worksheet_id = p_worksheet_id ;
6459 
6460   -- Delete from psb_ws_submit_service_packages.
6461   DELETE psb_ws_submit_service_packages
6462   WHERE  worksheet_id = p_worksheet_id ;
6463 
6464   -- Delete from psb_ws_user_profiles.
6465   DELETE psb_ws_user_profiles
6466   WHERE  worksheet_id = p_worksheet_id ;
6467 
6468   -- Delete from psb_position_assignments.
6469   DELETE psb_position_assignments
6470   WHERE  worksheet_id = p_worksheet_id ;
6471 
6472   -- Delete from psb_pay_element_rates.
6473   DELETE psb_pay_element_rates
6474   WHERE  worksheet_id = p_worksheet_id ;
6475 
6476   -- Delete from psb_ws_submit_comments.
6477   DELETE psb_ws_submit_comments
6478   WHERE  worksheet_id = p_worksheet_id ;
6479 
6480   -- Delete from psb_worksheets.
6481   DELETE psb_worksheets
6482   WHERE  worksheet_id = p_worksheet_id ;
6483 
6484   /*For Bug No : 2613269 Start*/
6485   fnd_attached_documents2_pkg.delete_attachments
6486              (X_entity_name => 'PSB_WORKSHEETS',
6487               X_pk1_value => p_worksheet_id,
6488               X_delete_document_flag => 'Y'
6489              );
6490   /*For Bug No : 2613269 End*/
6491 
6492   /*For Bug No : 2266309 Start*/
6493   COMMIT WORK;
6494   /*For Bug No : 2266309 End*/
6495   --
6496 EXCEPTION
6497   --
6498  WHEN OTHERS THEN
6499     --
6500     /*For Bug No : 2266309 Start*/
6501     ROLLBACK TO Delete_Worksheet_Pvt ;
6502     /*For Bug No : 2266309 End*/
6503   --
6504     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6505     --
6506     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
6507       FND_MSG_PUB.Add_Exc_Msg (  G_PKG_NAME,
6508 				 l_api_name );
6509     END IF;
6510     --
6511 END Delete_Worksheet_Pvt ;
6512 /*---------------------------------------------------------------------------*/
6513 
6514 
6515 /*===========================================================================+
6516  |                      PROCEDURE Delete_Worksheet_CP                        |
6517  +===========================================================================*/
6518 --
6519 -- This is the execution file for the concurrent program 'Maintain Budget
6520 -- Account Codes'.
6521 --
6522 PROCEDURE Delete_Worksheet_CP
6523 (
6524   errbuf                      OUT  NOCOPY      VARCHAR2,
6525   retcode                     OUT  NOCOPY      VARCHAR2,
6526   --
6527   p_worksheet_id              IN       NUMBER  ,
6528   p_keep_local_copy_flag      IN       VARCHAR2
6529 )
6530 IS
6531   --
6532   l_api_name         CONSTANT VARCHAR2(30)   := 'Delete_Worksheet_CP' ;
6533   l_api_version      CONSTANT NUMBER         :=  1.0 ;
6534   --
6535   l_return_status             VARCHAR2(1) ;
6536   l_msg_count                 NUMBER ;
6537   l_msg_data                  VARCHAR2(2000) ;
6538   --
6539 BEGIN
6540   --
6541   SAVEPOINT Delete_Worksheet_CP_Pvt ;
6542   --
6543   PSB_WS_Ops_Pvt.Delete_Worksheet
6544   (
6545      p_api_version             =>   1.0 ,
6546      p_init_msg_list           =>   FND_API.G_TRUE,
6547    /*For Bug No : 2266309 Start*/
6548      --p_commit                =>   FND_API.G_FALSE,
6549      p_commit                  =>   FND_API.G_TRUE,
6550    /*For Bug No : 2266309 End*/
6551      p_validation_level        =>   FND_API.G_VALID_LEVEL_FULL,
6552      p_return_status           =>   l_return_status,
6553      p_msg_count               =>   l_msg_count,
6554      p_msg_data                =>   l_msg_data,
6555      --
6556      p_worksheet_id            =>   p_worksheet_id,
6557      p_keep_local_copy_flag    =>   p_keep_local_copy_flag
6558   );
6559   --
6560   IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6561     RAISE FND_API.G_EXC_ERROR;
6562   END IF;
6563   --
6564 
6565   --
6566   retcode := 0 ;
6567   COMMIT WORK;
6568   --
6569 EXCEPTION
6570   --
6571   WHEN FND_API.G_EXC_ERROR THEN
6572     --
6573   /*For Bug No : 2266309 Start*/
6574     --ROLLBACK TO Delete_Worksheet_CP_Pvt ;
6575     ROLLBACK;
6576   /*For Bug No : 2266309 End*/
6577     PSB_MESSAGE_S.Print_Error ( p_mode         => FND_FILE.LOG ,
6578 				p_print_header =>  FND_API.G_TRUE ) ;
6579     retcode := 2 ;
6580     --
6581   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6582     --
6583   /*For Bug No : 2266309 Start*/
6584     --ROLLBACK TO Delete_Worksheet_CP_Pvt ;
6585     ROLLBACK;
6586   /*For Bug No : 2266309 End*/
6587     PSB_MESSAGE_S.Print_Error ( p_mode         => FND_FILE.LOG ,
6588 				p_print_header =>  FND_API.G_TRUE ) ;
6589     retcode := 2 ;
6590     --
6591   WHEN OTHERS THEN
6592     --
6593   /*For Bug No : 2266309 Start*/
6594     --ROLLBACK TO Delete_Worksheet_CP_Pvt ;
6595     ROLLBACK;
6596   /*For Bug No : 2266309 End*/
6597     IF FND_MSG_PUB.Check_Msg_Level( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR ) THEN
6598       --
6599       FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME ,
6600 			       l_api_name  ) ;
6601     END IF ;
6602     --
6603     PSB_MESSAGE_S.Print_Error ( p_mode         =>  FND_FILE.LOG ,
6604 				p_print_header =>  FND_API.G_TRUE ) ;
6605     retcode := 2 ;
6606     --
6607 END Delete_Worksheet_CP ;
6608 /*---------------------------------------------------------------------------*/
6609 
6610 
6611 /*===========================================================================+
6612  |                   PROCEDURE  Create_New_Position_Worksheet                |
6613  +===========================================================================*/
6614 --
6615 -- The API creates a worksheet from a given worksheet while cosidering only
6616 -- new positions in the given worksheet.
6617 --
6618 PROCEDURE Create_New_Position_Worksheet
6619 (
6620   p_api_version               IN       NUMBER ,
6621   p_init_msg_list             IN       VARCHAR2 := FND_API.G_FALSE ,
6622   p_commit                    IN       VARCHAR2 := FND_API.G_FALSE ,
6623   p_validation_level          IN       NUMBER   := FND_API.G_VALID_LEVEL_FULL ,
6624   p_return_status             OUT  NOCOPY      VARCHAR2 ,
6625   p_msg_count                 OUT  NOCOPY      NUMBER   ,
6626   p_msg_data                  OUT  NOCOPY      VARCHAR2 ,
6627   --
6628   p_worksheet_id              IN       psb_worksheets.worksheet_id%TYPE    ,
6629   p_budget_group_id           IN       psb_worksheets.budget_group_id%TYPE ,
6630   p_service_package_operation_id
6631 			      IN       NUMBER := FND_API.G_MISS_NUM ,
6632   p_worksheet_id_OUT          OUT  NOCOPY      psb_worksheets.worksheet_id%TYPE
6633 )
6634 IS
6635   --
6636   l_api_name       CONSTANT VARCHAR2(30) := 'Create_New_Position_Worksheet' ;
6637   l_api_version    CONSTANT NUMBER       :=  1.0 ;
6638   --
6639   l_return_status           VARCHAR2(1) ;
6640   l_msg_count               NUMBER ;
6641   l_msg_data                VARCHAR2(2000) ;
6642   --
6643   l_worksheet_name          psb_worksheets.name%TYPE ;
6644   l_budget_by_position      psb_worksheets.budget_by_position%TYPE ;
6645   l_worksheet_description   psb_worksheets.description%TYPE ;
6646   l_main_budget_group_id    psb_worksheets.budget_group_id%TYPE ;
6647   l_main_budget_group_name  psb_budget_groups.name%TYPE ;
6648   l_main_budget_calendar_id psb_worksheets.budget_calendar_id%TYPE ;
6649   l_new_worksheet_id        psb_worksheets.worksheet_id%TYPE ;
6650   l_global_worksheet_id     psb_worksheets.worksheet_id%TYPE ;
6651   l_service_package_count   NUMBER ;
6652   --
6653   l_tmp_char                VARCHAR2(1) ;
6654   l_lines_added             NUMBER := 0 ;
6655   --
6656   CURSOR l_worksheets_csr IS
6657 	 SELECT *
6658 	 FROM psb_worksheets
6659 	 WHERE worksheet_id = p_worksheet_id ;
6660   --
6661   l_worksheets_rec   l_worksheets_csr%ROWTYPE ;
6662   --
6663 BEGIN
6664   --
6665   SAVEPOINT Create_New_Position_WS_Pvt ;
6666   --
6667   IF NOT FND_API.Compatible_API_Call ( l_api_version,
6668 				       p_api_version,
6669 				       l_api_name,
6670 				       G_PKG_NAME )
6671   THEN
6672     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
6673   END IF;
6674   --
6675   IF FND_API.To_Boolean ( p_init_msg_list ) THEN
6676     FND_MSG_PUB.initialize ;
6677   END IF;
6678   --
6679 
6680   p_return_status := FND_API.G_RET_STS_SUCCESS ;
6681   p_worksheet_id_OUT := 0 ;
6682 
6683   --
6684   -- Finding the worksheet information.
6685   --
6686   OPEN  l_worksheets_csr ;
6687   FETCH l_worksheets_csr INTO l_worksheets_rec ;
6688   CLOSE l_worksheets_csr ;
6689 
6690   IF ( l_worksheets_rec.worksheet_Id IS NULL ) THEN
6691     --
6692     Fnd_Message.Set_Name ('PSB',     'PSB_INVALID_WORKSHEET_ID') ;
6693     Fnd_Message.Set_Token('ROUTINE', l_api_name ) ;
6694     FND_MSG_PUB.Add;
6695     RAISE FND_API.G_EXC_ERROR ;
6696   END IF ;
6697 
6698   -- l_budget_by_position defines whether worksheet contains positions or not.
6699   l_budget_by_position := NVL(l_worksheets_rec.budget_by_position, 'N') ;
6700 
6701   l_main_budget_group_id      := l_worksheets_rec.budget_group_id ;
6702   l_main_budget_calendar_id   := l_worksheets_rec.budget_calendar_id ;
6703 
6704   --
6705   -- Finding the budget group name for the worksheet.
6706   --
6707   SELECT name INTO l_main_budget_group_name
6708   FROM   psb_budget_groups
6709   WHERE  budget_group_id = l_main_budget_group_id ;
6710 
6711   --
6712   -- Get translated messages for the new worksheet.
6713   --
6714   Fnd_Message.Set_Name ( 'PSB', 'PSB_WORKSHEET_CREATION_INFO') ;
6715   Fnd_Message.Set_Token( 'WORKSHEET_ID',      p_worksheet_id ) ;
6716   Fnd_Message.Set_Token( 'BUDGET_GROUP_NAME', l_main_budget_group_name ) ;
6717   l_worksheet_description := Fnd_Message.Get ;
6718 
6719   --
6720   -- Find global worksheet related information, use by Create_Worksheet API.
6721   --
6722   IF NVL(l_worksheets_rec.global_worksheet_flag, 'N') = 'Y' THEN
6723     l_global_worksheet_id := p_worksheet_id ;
6724   ELSE
6725     l_global_worksheet_id := l_worksheets_rec.global_worksheet_id ;
6726   END IF ;
6727 
6728   --
6729   -- Create the new worksheet in psb_worksheets table.
6730   --
6731   PSB_Worksheet_Pvt.Create_Worksheet
6732   (
6733      p_api_version               => 1.0 ,
6734      p_init_msg_list             => FND_API.G_FALSE,
6735      p_commit                    => FND_API.G_FALSE,
6736      p_validation_level          => FND_API.G_VALID_LEVEL_NONE,
6737      p_return_status             => l_return_status,
6738      p_msg_count                 => l_msg_count,
6739      p_msg_data                  => l_msg_data ,
6740      --
6741      p_budget_group_id           => p_budget_group_id,
6742      p_budget_calendar_id        => l_worksheets_rec.budget_calendar_id,
6743      p_worksheet_type            => 'R',
6744      p_name                      => NULL ,
6745      p_description               => l_worksheet_description ,
6746      p_ws_creation_complete      => l_worksheets_rec.ws_creation_complete ,
6747      p_stage_set_id              => l_worksheets_rec.stage_set_id ,
6748      p_current_stage_seq         => l_worksheets_rec.current_stage_seq ,
6749      p_global_worksheet_id       => l_global_worksheet_id ,
6750      p_global_worksheet_flag     => 'N' ,
6751      p_global_worksheet_option   => l_worksheets_rec.global_worksheet_option,
6752      p_local_copy_flag           => l_worksheets_rec.local_copy_flag,
6753      p_copy_of_worksheet_id      => l_worksheets_rec.copy_of_worksheet_id,
6754      p_freeze_flag               => l_worksheets_rec.freeze_flag,
6755      p_budget_by_position        => l_worksheets_rec.budget_by_position,
6756      p_use_revised_element_rates => l_worksheets_rec.use_revised_element_rates,
6757      p_num_proposed_years        => l_worksheets_rec.num_proposed_years,
6758      p_num_years_to_allocate     => l_worksheets_rec.num_years_to_allocate,
6759      p_rounding_factor           => l_worksheets_rec.rounding_factor,
6760      p_gl_cutoff_period          => l_worksheets_rec.gl_cutoff_period,
6761      p_include_stat_balance      => l_worksheets_rec.include_stat_balance,
6762      p_include_trans_balance     => l_worksheets_rec.include_translated_balance,
6763      p_include_adj_period        => l_worksheets_rec.include_adjustment_periods,
6764      p_data_extract_id           => l_worksheets_rec.data_extract_id,
6765      p_parameter_set_id          => NULL,
6766      p_constraint_set_id         => NULL,
6767      p_allocrule_set_id          => NULL,
6768      p_date_submitted            => l_worksheets_rec.date_submitted,
6769      p_submitted_by              => l_worksheets_rec.submitted_by,
6770      p_attribute1                => l_worksheets_rec.attribute1,
6771      p_attribute2                => l_worksheets_rec.attribute2,
6772      p_attribute3                => l_worksheets_rec.attribute3,
6773      p_attribute4                => l_worksheets_rec.attribute4,
6774      p_attribute5                => l_worksheets_rec.attribute5,
6775      p_attribute6                => l_worksheets_rec.attribute6,
6776      p_attribute7                => l_worksheets_rec.attribute7,
6777      p_attribute8                => l_worksheets_rec.attribute8,
6778      p_attribute9                => l_worksheets_rec.attribute9,
6779      p_attribute10               => l_worksheets_rec.attribute10,
6780      p_context                   => l_worksheets_rec.context,
6781      p_worksheet_id              => l_new_worksheet_id
6782   );
6783   --
6784   IF l_return_status = FND_API.G_RET_STS_ERROR THEN
6785     RAISE FND_API.G_EXC_ERROR ;
6786   ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6787     RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
6788   END IF;
6789 
6790   --
6791   -- Get budget calendar related info to find all the budget groups down in the
6792   -- current hierarchy to get all the CCIDs for the current budget group.
6793   --
6794   IF NVL(PSB_WS_Acct1.g_budget_calendar_id, -99) <> l_main_budget_calendar_id
6795   THEN
6796     --
6797     PSB_WS_Acct1.Cache_Budget_Calendar
6798     (
6799        p_return_status         =>  l_return_status ,
6800        p_budget_calendar_id    =>  l_main_budget_calendar_id
6801     );
6802     --
6803     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
6804       RAISE FND_API.G_EXC_ERROR ;
6805     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6806       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
6807     END IF;
6808     --
6809   END IF ;
6810 
6811   --
6812   -- Check whether service packages were selected for the worksheet.
6813   -- If yes, then we need to consider only those account lines which are
6814   -- related to the service package selection.
6815   --
6816   SELECT count(*) INTO l_service_package_count
6817   FROM   psb_ws_submit_service_packages
6818   WHERE  worksheet_id = p_worksheet_id
6819   AND    operation_id = NVL( p_service_package_operation_id ,
6820 			     FND_API.G_MISS_NUM ) ;
6821   --
6822   -- Maintain psb_ws_lines_positions table if worksheet contains positions.
6823   --
6824   IF l_budget_by_position = 'Y' THEN
6825 
6826     --
6827     -- This loop gets all the newly created position for the given worksheet.
6828     -- belonging to the given budget group.
6829     --
6830     FOR l_lines_pos_rec IN
6831     (
6832        SELECT pos_lines.*     ,
6833 	      pos.position_id
6834        FROM   psb_ws_lines_positions   pos_lines ,
6835 	      psb_ws_position_lines    pos       ,
6836 	      psb_positions            positions
6837        WHERE  pos_lines.worksheet_id = p_worksheet_id
6838        AND    pos.position_line_id   = pos_lines.position_line_id
6839        AND    positions.position_id  = pos.position_id
6840        --AND    positions.hr_position_id IS NULL
6841        AND    positions.new_position_flag = 'Y'
6842        AND    (
6843 		l_service_package_count = 0
6844 		OR
6845 		pos_lines.position_line_id IN
6846 		(
6847 		  SELECT accts.position_line_id
6848 		  FROM   psb_ws_account_lines  accts
6849 		  WHERE  accts.position_line_id = pos_lines.position_line_id
6850 		  AND    accts.service_package_id IN
6851 			 (
6852 			   SELECT sp.service_package_id
6853 			   FROM   psb_ws_submit_service_packages sp
6854 			   WHERE  worksheet_id = p_worksheet_id
6855 			   AND    operation_id = p_service_package_operation_id
6856 			 )
6857 		)
6858 	      )
6859     )
6860     LOOP
6861 
6862       -- At least one line should get created for the worksheet.
6863       l_lines_added := l_lines_added + 1 ;
6864 
6865       --
6866       -- Put the position_line in the psb_ws_lines_position table.
6867       --
6868       PSB_WS_Pos_Pvt.Create_Position_Matrix
6869       (
6870 	p_api_version        =>  1.0 ,
6871 	p_init_msg_list      =>  FND_API.G_FALSE ,
6872 	p_validation_level   =>  FND_API.G_VALID_LEVEL_NONE ,
6873 	p_return_status      =>  l_return_status ,
6874 	p_msg_count          =>  l_msg_count ,
6875 	p_msg_data           =>  l_msg_data ,
6876 	--
6877 	p_worksheet_id       =>  l_new_worksheet_id ,
6878 	p_position_line_id   =>  l_lines_pos_rec.position_line_id ,
6879 	p_freeze_flag        =>  l_lines_pos_rec.freeze_flag ,
6880 	p_view_line_flag     =>  l_lines_pos_rec.view_line_flag
6881       ) ;
6882       --
6883       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
6884 	RAISE FND_API.G_EXC_ERROR ;
6885       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6886 	RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
6887       END IF;
6888       --
6889 
6890       --
6891       -- Maintain psb_ws_lines matrix for the account lines related to
6892       -- current position_line_id (l_lines_pos_rec.position_line_id).
6893       --
6894       FOR l_lines_rec IN
6895       (
6896 	SELECT lines.*
6897 	FROM   psb_ws_lines          lines,
6898 	       psb_ws_account_lines  accts
6899 	WHERE  accts.position_line_id = l_lines_pos_rec.position_line_id
6900 	AND    lines.worksheet_id     = p_worksheet_id
6901 	AND    lines.account_line_id  = accts.account_line_id
6902       )
6903       LOOP
6904 	--
6905 	Insert_WS_Lines_Pvt
6906 	(
6907 	  p_worksheet_id       =>  l_new_worksheet_id,
6908 	  p_account_line_id    =>  l_lines_rec.account_line_id ,
6909 	  p_freeze_flag        =>  l_lines_rec.freeze_flag ,
6910 	  p_view_line_flag     =>  l_lines_rec.view_line_flag ,
6911 	  p_last_update_date   =>  g_current_date,
6912 	  p_last_updated_by    =>  g_current_user_id,
6913 	  p_last_update_login  =>  g_current_login_id,
6914 	  p_created_by         =>  g_current_user_id,
6915 	  p_creation_date      =>  g_current_date,
6916 	  p_return_status      =>  l_return_status
6917 	) ;
6918 	--
6919 	IF l_return_status = FND_API.G_RET_STS_ERROR THEN
6920 	  RAISE FND_API.G_EXC_ERROR ;
6921 	ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6922 	  RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
6923 	END IF;
6924 	--
6925 
6926       END LOOP ;    --/ Maintain position_line_id related account lines.
6927 
6928     END LOOP ;    --/ To process all new positions in the worksheet.
6929 
6930   END IF ;  --/ Check for l_budget_by_position = 'Y'
6931 
6932   --
6933   -- Check whether at least one line got created or not.
6934   --
6935   IF l_lines_added = 0 THEN
6936     p_worksheet_id_OUT := 0 ;
6937     ROLLBACK TO Create_New_Position_WS_Pvt ;
6938   ELSE
6939     p_worksheet_id_OUT := l_new_worksheet_id ;
6940   END IF ;
6941 
6942   --
6943   IF FND_API.To_Boolean ( p_commit ) THEN
6944     COMMIT WORK;
6945   END IF;
6946   --
6947   FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
6948 			      p_data  => p_msg_data );
6949   --
6950 EXCEPTION
6951   --
6952   WHEN FND_API.G_EXC_ERROR THEN
6953     --
6954     ROLLBACK TO Create_New_Position_WS_Pvt ;
6955     p_return_status := FND_API.G_RET_STS_ERROR;
6956     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
6957 				p_data  => p_msg_data );
6958   --
6959   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6960     --
6961     ROLLBACK TO Create_New_Position_WS_Pvt ;
6962     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6963     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
6964 				p_data  => p_msg_data );
6965   --
6966   WHEN OTHERS THEN
6967     --
6968     IF ( l_worksheets_csr%ISOPEN ) THEN
6969       CLOSE l_worksheets_csr ;
6970     END IF ;
6971     --
6972     ROLLBACK TO Create_New_Position_WS_Pvt ;
6973     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6974     --
6975     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
6976       FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME,
6977 				l_api_name);
6978     END if;
6979     --
6980     FND_MSG_PUB.Count_And_Get ( p_count => p_msg_count,
6981 				p_data  => p_msg_data );
6982      --
6983 END Create_New_Position_Worksheet ;
6984 /*---------------------------------------------------------------------------*/
6985 
6986 
6987 END PSB_WS_Ops_Pvt ;