DBA Data[Home] [Help]

PACKAGE BODY: APPS.PSB_WS_ACCT1

Source


1 PACKAGE BODY PSB_WS_ACCT1 AS
2 /* $Header: PSBVWA1B.pls 120.56 2006/04/07 11:17:38 shtripat ship $ */
3 
4   G_PKG_NAME CONSTANT   VARCHAR2(30):= 'PSB_WS_ACCT1';
5 
6   TYPE TokNameArray IS TABLE OF VARCHAR2(100)
7     INDEX BY BINARY_INTEGER;
8 
9   TYPE TokValArray IS TABLE OF VARCHAR2(1000)
10     INDEX BY BINARY_INTEGER;
11 
12 /* Bug No 2719865 Start */
13   g_first_ccid		BOOLEAN := TRUE;
14 /* Bug No 2719865 End */
15 
16   -- Number of Message Tokens
17 
18   no_msg_tokens         NUMBER := 0;
19 
20   -- Message Token Name
21 
22   msg_tok_names         TokNameArray;
23 
24   -- Message Token Value
25 
26   msg_tok_val           TokValArray;
27 
28   g_dbug                VARCHAR2(1000);
29   g_create_zero_bal     VARCHAR2(1);
30   g_budget_group_id     NUMBER;
31 
32 
33 /*===========================================================================+
34  |                             PROCEDURE pd                                  |
35  +===========================================================================*/
36 --
37 -- API to print debug information, used during only development.
38 --
39 PROCEDURE pd( p_message   IN     VARCHAR2)
40 IS
41 BEGIN
42   NULL ;
43   --DBMS_OUTPUT.Put_Line(p_message) ;
44   --FND_FILE.put_line(FND_FILE.LOG, p_message);
45 END pd ;
46 /*---------------------------------------------------------------------------*/
47 
48 
49 /* ----------------------------------------------------------------------- */
50 
51 -- Add Token and Value to the Message Token array
52 
53 PROCEDURE message_token(tokname IN VARCHAR2,
54 			tokval  IN VARCHAR2) IS
55 
56 BEGIN
57 
58   if no_msg_tokens is null then
59     no_msg_tokens := 1;
60   else
61     no_msg_tokens := no_msg_tokens + 1;
62   end if;
63 
64   msg_tok_names(no_msg_tokens) := tokname;
65   msg_tok_val(no_msg_tokens) := tokval;
66 
67 END message_token;
68 
69 /* ----------------------------------------------------------------------- */
70 
71 -- Define a Message Token with a Value and set the Message Name
72 
73 -- Calls FND_MESSAGE server package to set the Message Stack. This message is
74 -- retrieved by the calling program.
75 
76 PROCEDURE add_message(appname IN VARCHAR2,
77 		      msgname IN VARCHAR2) IS
78 
79   i  PLS_INTEGER;
80 
81 BEGIN
82 
83   if ((appname is not null) and
84       (msgname is not null)) then
85 
86     FND_MESSAGE.SET_NAME(appname, msgname);
87 
88     if no_msg_tokens is not null then
89       for i in 1..no_msg_tokens loop
90 	FND_MESSAGE.SET_TOKEN(msg_tok_names(i), msg_tok_val(i));
91       end loop;
92     end if;
93 
94     FND_MSG_PUB.Add;
95 
96   end if;
97 
98   -- Clear Message Token stack
99 
100   no_msg_tokens := 0;
101 
102 END add_message;
103 
104 /* ----------------------------------------------------------------------- */
105 
106 -- Cache Budget Years, Budget Periods and Calculation Periods, after sorting
107 -- by Start Date, so that it can be reused across modules in the Worksheet
108 -- Creation process
109 
110 PROCEDURE Cache_Budget_Calendar
111 ( p_return_status       OUT  NOCOPY  VARCHAR2,
112   p_budget_calendar_id  IN   NUMBER
113 ) IS
114 
115   l_init_index          PLS_INTEGER;
116   l_year_index          PLS_INTEGER := 1;
117   l_period_index        PLS_INTEGER := 1;
118   l_calcp_index         PLS_INTEGER := 1;
119   l_long_index          PLS_INTEGER;
120 
121   l_num_budget_periods  NUMBER;
122   l_num_calc_periods    NUMBER;
123 
124   l_firstpp             BOOLEAN := TRUE;
125 
126   cursor c_BudYr is
127     select a.budget_period_id,
128 	   a.budget_year_type_id,
129 	   b.year_category_type,
130 	   period_distribution_type,
131 	   calculation_period_type,
132 	   a.name,
133 	   a.start_date,
134 	   a.end_date
135       from PSB_BUDGET_YEAR_TYPES b,
136 	   PSB_BUDGET_PERIODS a
137      where b.budget_year_type_id = a.budget_year_type_id
138        and a.budget_period_type = 'Y'
139        and a.budget_calendar_id = p_budget_calendar_id
140      order by a.start_date;
141 
142   cursor c_BudPrd (budyr_id NUMBER) is
143     select budget_period_id,
144 	   start_date,
145 	   end_date
146       from PSB_BUDGET_PERIODS
147      where budget_period_type = 'P'
148        and parent_budget_period_id = budyr_id
149        and budget_calendar_id = p_budget_calendar_id
150      order by start_date;
151 
152   cursor c_CalcPrd (budyr_id NUMBER,
153 		    startdate DATE,
154 		    enddate DATE) is
155     select budget_period_id,
156 	   start_date,
157 	   end_date
158       from PSB_BUDGET_PERIODS
159      where end_date <= enddate
160        and start_date >= startdate
161        and budget_period_type = 'C'
162        and parent_budget_period_id = budyr_id
163        and budget_calendar_id = p_budget_calendar_id
164      order by start_date;
165 
166 BEGIN
167 
168   for l_init_index in 1..g_budget_years.Count loop
169     g_budget_years(l_init_index).budget_year_id := null;
170     g_budget_years(l_init_index).budget_year_type_id := null;
171     g_budget_years(l_init_index).year_type := null;
172     g_budget_years(l_init_index).year_name := null;
173     g_budget_years(l_init_index).start_date := null;
174     g_budget_years(l_init_index).end_date := null;
175     g_budget_years(l_init_index).num_budget_periods := null;
176     g_budget_years(l_init_index).last_period_index := null;
177   end loop;
178 
179   for l_init_index in 1..g_budget_periods.Count loop
180     g_budget_periods(l_init_index).budget_period_id := null;
181     g_budget_periods(l_init_index).budget_period_type := null;
182     g_budget_periods(l_init_index).long_sequence_no := null;
183     g_budget_periods(l_init_index).start_date := null;
184     g_budget_periods(l_init_index).end_date := null;
185     g_budget_periods(l_init_index).budget_year_id := null;
186     g_budget_periods(l_init_index).num_calc_periods := null;
187   end loop;
188 
189   for l_init_index in 1..g_calculation_periods.Count loop
190     g_calculation_periods(l_init_index).calc_period_id := null;
191     g_calculation_periods(l_init_index).calc_period_type := null;
192     g_calculation_periods(l_init_index).start_date := null;
193     g_calculation_periods(l_init_index).end_date := null;
194     g_calculation_periods(l_init_index).budget_period_id := null;
195   end loop;
196 
197   g_num_budget_years := 0;
198   g_max_num_years := 0;
199   g_num_budget_periods := 0;
200   g_num_calc_periods := 0;
201 
202   g_budget_calendar_id := p_budget_calendar_id;
203 
204   for c_BudYr_Rec in c_BudYr loop
205 
206     g_num_budget_years := g_num_budget_years + 1;
207 
208     g_budget_years(l_year_index).budget_year_id := c_BudYr_Rec.budget_period_id;
209     g_budget_years(l_year_index).budget_year_type_id := c_BudYr_Rec.budget_year_type_id;
210     g_budget_years(l_year_index).year_type := c_BudYr_Rec.year_category_type;
211     g_budget_years(l_year_index).year_name := c_BudYr_Rec.name;
212     g_budget_years(l_year_index).start_date := c_BudYr_Rec.start_date;
213     g_budget_years(l_year_index).end_date := c_BudYr_Rec.end_date;
214 
215     if c_BudYr_Rec.year_category_type = 'PP' then
216     begin
217 
218       g_max_num_years := g_max_num_years + 1;
219 
220       if l_firstpp then
221 
222 	l_firstpp := FALSE;
223 
224 	g_startdate_pp := c_BudYr_Rec.Start_Date;
225 	g_end_est_date := c_BudYr_Rec.End_Date;
226 
227       end if;
228 
229       if c_BudYr_Rec.end_date > g_end_est_date then
230 	g_end_est_date := c_BudYr_Rec.end_date;
231       end if;
232 
233     end;
234     end if;
235 
236     if c_BudYr_Rec.year_category_type = 'CY' then
237       g_startdate_cy := c_BudYr_Rec.Start_Date;
238       g_enddate_cy := c_BudYr_Rec.End_Date;
239     end if;
240 
241     l_long_index := 1;
242 
243     l_num_budget_periods := 0;
244 
245     for c_BudPrd_Rec in c_BudPrd (c_BudYr_Rec.budget_period_id) loop
246 
247       g_num_budget_periods := g_num_budget_periods + 1;
248       l_num_budget_periods := l_num_budget_periods + 1;
249 
250       g_budget_periods(l_period_index).budget_period_id := c_BudPrd_Rec.budget_period_id;
251       g_budget_periods(l_period_index).budget_period_type := c_BudYr_Rec.period_distribution_type;
252       g_budget_periods(l_period_index).long_sequence_no := l_long_index;
253       g_budget_periods(l_period_index).start_date := c_BudPrd_Rec.start_date;
254       g_budget_periods(l_period_index).end_date := c_BudPrd_Rec.end_date;
255       g_budget_periods(l_period_index).budget_year_id := c_BudYr_Rec.budget_period_id;
256 
257       l_num_calc_periods := 0;
258 
259       for c_CalcPrd_Rec in c_CalcPrd (c_BudYr_Rec.budget_period_id,
260 				      c_BudPrd_Rec.start_date,
261 				      c_BudPrd_Rec.end_date) loop
262 	g_num_calc_periods := g_num_calc_periods + 1;
263 	l_num_calc_periods := l_num_calc_periods + 1;
264 
265 	g_calculation_periods(l_calcp_index).calc_period_id := c_CalcPrd_Rec.budget_period_id;
266 	g_calculation_periods(l_calcp_index).calc_period_type := c_BudYr_Rec.calculation_period_type;
267 	g_calculation_periods(l_calcp_index).start_date := c_CalcPrd_Rec.start_date;
268 	g_calculation_periods(l_calcp_index).end_date := c_CalcPrd_Rec.end_date;
269 	g_calculation_periods(l_calcp_index).budget_period_id := c_BudPrd_Rec.budget_period_id;
270 
271 	l_calcp_index := l_calcp_index + 1;
272       end loop;
273 
274       g_budget_periods(l_period_index).num_calc_periods := l_num_calc_periods;
275 
276       l_period_index := l_period_index + 1;
277 
278       l_long_index := l_long_index + 1;
279 
280       if l_long_index > g_max_num_amounts then
281 	add_message('PSB', 'NUM_BUDGET_PERIODS_CEILING');
282 	raise FND_API.G_EXC_ERROR;
283       end if;
284 
285     end loop;
286 
287     g_budget_years(l_year_index).num_budget_periods := l_num_budget_periods;
288     g_budget_years(l_year_index).last_period_index := l_long_index - 1;
289 
290     -- Bug#3126462: Support Percent type allocation rules for CY estimates
291     -- We need to cache number of periods in CY. This info used when processing
292     -- allocation rules for CY later on.
293     IF c_BudYr_Rec.year_category_type = 'CY' THEN
294       g_cy_num_periods := l_num_budget_periods ;
295     END IF;
296 
297     l_year_index := l_year_index + 1;
298 
299   end loop;
300 
301   p_return_status := FND_API.G_RET_STS_SUCCESS;
302 
303 
304 EXCEPTION
305 
306    when FND_API.G_EXC_ERROR then
307      p_return_status := FND_API.G_RET_STS_ERROR;
308 
309    when FND_API.G_EXC_UNEXPECTED_ERROR then
310      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
311 
312    when OTHERS then
313      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
314      FND_MSG_PUB.Add_Exc_Msg
315 	  (p_pkg_name => G_PKG_NAME,
316 	   p_procedure_name => 'Cache_Budget_Calendar');
317 
318 END Cache_Budget_Calendar;
319 
320 /* ----------------------------------------------------------------------- */
321 
322 PROCEDURE Get_Budget_Calendar_Info
323 ( p_return_status       OUT  NOCOPY  VARCHAR2,
324   p_budget_calendar_id  IN   NUMBER,
325   p_startdate_pp        OUT  NOCOPY  DATE,
326   p_enddate_cy          OUT  NOCOPY  DATE
327 ) IS
328   --
329   l_return_status       VARCHAR2(1);
330   --
331 BEGIN
332 
333   p_return_status := FND_API.G_RET_STS_SUCCESS;
334 
335   --
336   -- Get budget calendar related info.
337   --
338   IF NVL(g_budget_calendar_id, -99) <> p_budget_calendar_id THEN
339     --
340     Cache_Budget_Calendar
341     (
342        p_return_status         =>  l_return_status ,
343        p_budget_calendar_id    =>  p_budget_calendar_id
344     );
345     --
346     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
347       RAISE FND_API.G_EXC_ERROR ;
348     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
349       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
350     END IF;
351     --
352   END IF ;
353 
354   -- Set the out parameters.
355   p_startdate_pp := g_startdate_pp ;
356   p_enddate_cy   := g_enddate_cy ;
357 
358 EXCEPTION
359   --
360   WHEN FND_API.G_EXC_ERROR THEN
361     p_return_status := FND_API.G_RET_STS_ERROR;
362 
363   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
364     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
365 
366   WHEN OTHERS THEN
367     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
368   --
369 END Get_Budget_Calendar_Info;
370 
371 /* ----------------------------------------------------------------------- */
372 
373 -- Map Account based on flex mapping set
374 
375 FUNCTION Map_Account
376 ( p_flex_mapping_set_id  IN  NUMBER,
377   p_ccid                 IN  NUMBER,
378   p_budget_year_type_id  IN  NUMBER
379 ) RETURN NUMBER IS
380 
381   l_mapped_ccid          NUMBER;
382 
383 BEGIN
384 
385   if p_flex_mapping_set_id is not null then
386   begin
387 
388     l_mapped_ccid := PSB_FLEX_MAPPING_PVT.Get_Mapped_CCID
389 			(p_api_version => 1.0,
390 			 p_ccid => p_ccid,
391 			 p_budget_year_type_id => p_budget_year_type_id,
392 			 p_flexfield_mapping_set_id => p_flex_mapping_set_id,
393 			 p_mapping_mode => 'WORKSHEET');
394   end;
395   else
396     l_mapped_ccid := p_ccid;
397   end if;
398 
399   RETURN l_mapped_ccid;
400 
401 END Map_Account;
402 
403 /* ----------------------------------------------------------------------- */
404 
405 -- Check CCID Type by matching the CCID against the Personnel and Non-Personnel
406 -- Account Sets defined for the Budget Group
407 
408 PROCEDURE Check_CCID_Type
409 ( p_api_version       IN   NUMBER,
410   p_validation_level  IN   NUMBER := FND_API.G_VALID_LEVEL_NONE,
411   p_return_status     OUT  NOCOPY  VARCHAR2,
412   p_ccid_type         OUT  NOCOPY  VARCHAR2,
413   p_flex_code         IN   NUMBER,
414   p_ccid              IN   NUMBER,
415   p_budget_group_id   IN   NUMBER
416 ) IS
417 
418   l_api_name          CONSTANT VARCHAR2(30)   := 'Check_CCID_Type';
419   l_api_version       CONSTANT NUMBER         := 1.0;
420 
421   l_concat_segments   VARCHAR2(2000);
422 
423   l_ccid_type         VARCHAR2(30);
424 
425   /* Bug 3543845 start: add the following variables */
426   l_root_budget_group_id NUMBER;
427   l_root_budget_group    VARCHAR2(1);
428   l_ps_acct_pos_set_id   NUMBER;
429   l_nps_acct_pos_set_id  NUMBER;
430 
431   -- comment out the following cursor since the query is in the body now.
432   /*
433   cursor c_CCID1 is
434     select root_budget_group_id,
435 	   root_budget_group,
436 	   ps_account_position_set_id psid,
437 	   nps_account_position_set_id npsid
438       from PSB_BUDGET_GROUPS
439      where budget_group_id = p_budget_group_id;
440 
441   cursor c_CCID2 (BudgetGroupID NUMBER) is
442     select ps_account_position_set_id psid,
443 	   nps_account_position_set_id npsid
444       from PSB_BUDGET_GROUPS
445     where budget_group_id = BudgetGroupID;
446   */
447   /* Bug 3543845 end */
448 
449   cursor c_CCID_Type (AccSet_ID NUMBER,
450 		      CCID NUMBER) is
451     select 1
452       from PSB_BUDGET_ACCOUNTS
453      where account_position_set_id = AccSet_ID
454        and code_combination_id = CCID;
455 
456 BEGIN
457 
458   -- Standard call to check for call compatibility
459 
460   if not FND_API.Compatible_API_Call (l_api_version,
461 				      p_api_version,
462 				      l_api_name,
463 				      G_PKG_NAME)
464   then
465     raise FND_API.G_EXC_UNEXPECTED_ERROR;
466   end if;
467 
468   /* Bug 3543845 start: Use caching when available */
469 
470   IF PSB_WORKSHEET.g_ws_creation_flag
471   THEN
472 
473     l_ps_acct_pos_set_id := PSB_WORKSHEET.g_ps_acct_pos_set_id;
474     l_nps_acct_pos_set_id := PSB_WORKSHEET.g_nps_acct_pos_set_id;
475 
476   ELSE
477 
478     SELECT root_budget_group_id,
479            root_budget_group,
480            ps_account_position_set_id,
481            nps_account_position_set_id
482          INTO
483            l_root_budget_group_id,
484            l_root_budget_group,
485            l_ps_acct_pos_set_id,
486            l_nps_acct_pos_set_id
487     FROM   PSB_BUDGET_GROUPS
488     WHERE  budget_group_id = p_budget_group_id;
489 
490     IF l_root_budget_group is NULL OR l_root_budget_group = 'N'
491     THEN
492       SELECT ps_account_position_set_id,
493              nps_account_position_set_id
494            INTO
495              l_ps_acct_pos_set_id,
496              l_nps_acct_pos_set_id
497       FROM   PSB_BUDGET_GROUPS
498       WHERE  budget_group_id = l_root_budget_group_id;
499     END IF;
500 
501 
502   END IF;
503 
504   /* Comment out the following lines
505   for c_CCID1_Rec in c_CCID1 loop
506 
507     if c_CCID1_Rec.root_budget_group = 'Y' then
508     begin
509 
510       for c_CCID_Type_Rec in c_CCID_Type (c_CCID1_REC.psid, p_ccid) loop
511 	l_ccid_type := 'PERSONNEL_SERVICES';
512       end loop;
513 
514       if l_ccid_type is null then
515       begin
516 
517 	for c_CCID_Type_Rec in c_CCID_Type (c_CCID1_REC.npsid, p_ccid) loop
518 	  l_ccid_type := 'NON_PERSONNEL_SERVICES';
519 	end loop;
520 
521       end;
522       end if;
523 
524     end;
525     else
526     begin
527 
528       for c_CCID2_Rec in c_CCID2 (c_CCID1_Rec.root_budget_group_id) loop
529 
530 	for c_CCID_Type_Rec in c_CCID_Type (c_CCID2_REC.psid, p_ccid) loop
531 	  l_ccid_type := 'PERSONNEL_SERVICES';
532 	end loop;
533 
534 	if l_ccid_type is null then
535 	begin
536 
537 	  for c_CCID_Type_Rec in c_CCID_Type (c_CCID2_REC.npsid, p_ccid) loop
538 	    l_ccid_type := 'NON_PERSONNEL_SERVICES';
539 	  end loop;
540 
541 	end;
542 	end if;
543 
544       end loop;
545 
546     end;
547     end if;
548 
549   end loop;
550   */
551   -- Add the following to find out the l_ccid_type
552   l_ccid_type := NULL;
553 
554   for l_CCID_Type_Rec in c_CCID_Type (l_ps_acct_pos_set_id, p_ccid)
555   loop
556     l_ccid_type := 'PERSONNEL_SERVICES';
557   end loop;
558 
559   if l_ccid_type is null then
560   begin
561     for l_CCID_Type_Rec in c_CCID_Type (l_nps_acct_pos_set_id, p_ccid)
562     loop
563       l_ccid_type := 'NON_PERSONNEL_SERVICES';
564     end loop;
565   end;
566   end if;
567 
568   /* Bug 3543845 End */
569 
570   if l_ccid_type is null then
571   begin
572 
573     l_concat_segments := FND_FLEX_EXT.Get_Segs
574 			    (application_short_name => 'SQLGL',
575 			     key_flex_code => 'GL#',
576 			     structure_number => p_flex_code,
577 			     combination_id => p_ccid);
578 
579     message_token('CCID', l_concat_segments);
580     add_message('PSB', 'INVALID_ACCOUNT_TYPE');
581     raise FND_API.G_EXC_ERROR;
582 
583   end;
584   else
585     p_ccid_type := l_ccid_type;
586   end if;
587 
588   -- Initialize API return status to success
589 
590   p_return_status := FND_API.G_RET_STS_SUCCESS;
591 
592 
593 EXCEPTION
594 
595    when FND_API.G_EXC_ERROR then
596      p_return_status := FND_API.G_RET_STS_ERROR;
597 
598    when FND_API.G_EXC_UNEXPECTED_ERROR then
599      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
600 
601    when OTHERS then
602      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
603 
604      if FND_MSG_PUB.Check_Msg_Level
605        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
606      then
607        FND_MSG_PUB.Add_Exc_Msg
608 	  (p_pkg_name => G_PKG_NAME,
609 	   p_procedure_name => l_api_name);
610      end if;
611 
612 END Check_CCID_Type;
613 
614 /* ----------------------------------------------------------------------- */
615 
616 -- Cache CCIDs for the Account Set
617 
618 PROCEDURE Find_CCIDs
619 ( p_return_status   OUT  NOCOPY  VARCHAR2,
620   p_account_set_id  IN   NUMBER
621 ) IS
622 
623   cursor c_ccids is
624     select a.code_combination_id,
625 	   b.start_date_active,
626 	   b.end_date_active
627        from GL_CODE_COMBINATIONS b,
628 	   PSB_BUDGET_ACCOUNTS a
629        where b.enabled_flag = 'Y'
630        /* Bug 3692601 Start */
631        AND b.detail_budgeting_allowed_flag = 'Y'
632        /* Bug 3692601 End */
633        and b.code_combination_id = a.code_combination_id
634        and a.account_position_set_id = p_account_set_id;
635 
636   l_init_index      PLS_INTEGER;
637   l_ccid_index      PLS_INTEGER := 1;
638 
639 BEGIN
640 
641   for l_init_index in 1..g_ccids.Count loop
642     g_ccids(l_init_index).ccid := null;
643     g_ccids(l_init_index).start_date := null;
644     g_ccids(l_init_index).end_date := null;
645   end loop;
646 
647   g_num_ccids := 0;
648 
649   g_account_set_id := p_account_set_id;
650 
651   for c_ccids_rec in c_ccids loop
652 
653     g_ccids(l_ccid_index).ccid := c_ccids_rec.code_combination_id;
654     g_ccids(l_ccid_index).start_date := c_ccids_rec.start_date_active;
655     g_ccids(l_ccid_index).end_date := c_ccids_rec.end_date_active;
656 
657     g_num_ccids := g_num_ccids + 1;
658     l_ccid_index := l_ccid_index + 1;
659 
660   end loop;
661 
662 
663   -- Initialize API return status to success
664 
665   p_return_status := FND_API.G_RET_STS_SUCCESS;
666 
667 
668 EXCEPTION
669 
670    when FND_API.G_EXC_ERROR then
671      p_return_status := FND_API.G_RET_STS_ERROR;
672 
673    when FND_API.G_EXC_UNEXPECTED_ERROR then
674      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
675 
676    when OTHERS then
677      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
678      if FND_MSG_PUB.Check_Msg_Level
679        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
680      then
681        FND_MSG_PUB.Add_Exc_Msg
682 	  (p_pkg_name => G_PKG_NAME,
683 	   p_procedure_name => 'Find_CCIDs');
684      end if;
685 
686 END Find_CCIDs;
687 
688 /* ----------------------------------------------------------------------- */
689 
690 -- Create Worksheet Account Distribution in PSB_WS_ACCOUNT_LINES. If
691 -- entry already exists, it updates existing entry (if p_check_spal_exists
692 -- is FND_API.G_FALSE); otherwise, it increments the existing entry (if
693 -- p_check_spal_exists is FND_API.G_TRUE)
694 
695 -- This API must be called when creating a new Account Distribution
696 
697 PROCEDURE Create_Account_Dist
698 ( p_api_version              IN   NUMBER,
699   p_validation_level         IN   NUMBER := FND_API.G_VALID_LEVEL_NONE,
700   p_return_status            OUT  NOCOPY  VARCHAR2,
701   p_account_line_id          OUT  NOCOPY  NUMBER,
702   p_worksheet_id             IN   NUMBER,
703   p_check_spal_exists        IN   VARCHAR2 := FND_API.G_TRUE,
704   p_gl_cutoff_period         IN   DATE := FND_API.G_MISS_DATE,
705   p_allocrule_set_id         IN   NUMBER := FND_API.G_MISS_NUM,
706   p_budget_calendar_id       IN   NUMBER := FND_API.G_MISS_NUM,
707   p_rounding_factor          IN   NUMBER := FND_API.G_MISS_NUM,
708   p_stage_set_id             IN   NUMBER := FND_API.G_MISS_NUM,
709   p_budget_year_id           IN   NUMBER,
710   p_budget_group_id          IN   NUMBER,
711   p_ccid                     IN   NUMBER := FND_API.G_MISS_NUM,
712   p_flex_mapping_set_id      IN   NUMBER := FND_API.G_MISS_NUM,
713   p_map_accounts             IN   BOOLEAN := FALSE,
714   p_functional_transaction   IN   VARCHAR2 := NULL,
715   p_flex_code                IN   NUMBER := FND_API.G_MISS_NUM,
716   p_concatenated_segments    IN   VARCHAR2 := FND_API.G_MISS_CHAR,
717   p_startdate_pp             IN   DATE := FND_API.G_MISS_DATE,
718   p_template_id              IN   NUMBER := FND_API.G_MISS_NUM,
719   p_currency_code            IN   VARCHAR2,
720   p_balance_type             IN   VARCHAR2,
721   p_ytd_amount               IN   NUMBER,
722   p_distribute_flag          IN   VARCHAR2 := FND_API.G_FALSE,
723   p_annual_fte               IN   NUMBER := FND_API.G_MISS_NUM,
724   p_period_amount            IN   g_prdamt_tbl_type,
725   p_position_line_id         IN   NUMBER := FND_API.G_MISS_NUM,
726   p_element_set_id           IN   NUMBER := FND_API.G_MISS_NUM,
727   p_salary_account_line      IN   VARCHAR2 := FND_API.G_FALSE,
728   p_service_package_id       IN   NUMBER := FND_API.G_MISS_NUM,
729   p_start_stage_seq          IN   NUMBER := FND_API.G_MISS_NUM,
730   p_current_stage_seq        IN   NUMBER := FND_API.G_MISS_NUM,
731   p_end_stage_seq            IN   NUMBER := FND_API.G_MISS_NUM,
732   p_copy_of_account_line_id  IN   NUMBER := FND_API.G_MISS_NUM,
733   /* bug start 3996052 */
734   p_update_cy_estimate       IN   VARCHAR2 := 'N'
735   /* bug end 3996052 */
736 ) IS
737 
738   l_api_name                 CONSTANT VARCHAR2(30)   := 'Create_Account_Dist';
739   l_api_version              CONSTANT NUMBER         := 1.0;
740 
741   l_ytd_amount               NUMBER;
742   l_rounded_ytd_amount       NUMBER;
743   l_rounding_difference      NUMBER;
744   l_period_amount            NUMBER;
745   l_period_amount_tbl        g_prdamt_tbl_type;
746   l_period_amounts           g_prdamt_tbl_type;
747 
748   l_userid                   NUMBER;
749   l_loginid                  NUMBER;
750   l_requestid                NUMBER;
751 
752   -- Bug 3347507
753   l_original_start_stage_seq NUMBER;
754   l_start_stage_seq          NUMBER;
755   l_current_stage_seq        NUMBER;
756   l_acclineid                NUMBER;
757   l_ccid                     NUMBER;
758 
759   l_service_package_id       NUMBER;
760 
761   l_account_type             VARCHAR2(1);
762   l_template_id              NUMBER;
763 
764   cur_wal                    PLS_INTEGER;
765   sql_wal                    VARCHAR2(1000);
766   num_wal                    PLS_INTEGER;
767 
768   l_gl_cutoff_period         DATE;
769   l_global_worksheet_id      NUMBER;
770   l_allocrule_set_id         NUMBER;
771   l_budget_calendar_id       NUMBER;
772   l_rounding_factor          NUMBER;
773   l_stage_set_id             NUMBER;
774   l_flex_mapping_set_id      NUMBER;
775   l_local_copy_flag          VARCHAR2(1);
776 
777   l_budget_periods           g_budgetperiod_tbl_type;
778   l_year_index               PLS_INTEGER;
779   l_init_index               PLS_INTEGER;
780   l_period_index             PLS_INTEGER;
781   l_last_period_index        NUMBER;
782   l_budget_year_type_id      NUMBER;
783 
784   l_start_date               DATE;
785   l_end_date                 DATE;
786   l_ccid_start_date          DATE;
787   l_ccid_end_date            DATE;
788 
789   l_spal_id                  NUMBER;
790   l_spal_budget_group_id     NUMBER;
791   l_spytd_amount             NUMBER;
792   l_spal_exists              BOOLEAN := FALSE;
793 
794   l_index                    PLS_INTEGER;
795 
796   l_account_line_id          NUMBER;
797   l_budget_group_id          NUMBER;
798   l_budget_group_changed     BOOLEAN := FALSE;
799   l_current_requestid        NUMBER;
800   l_current_fte              NUMBER;
801   l_current_ytdamt           NUMBER;
802   l_current_prdamt           g_prdamt_tbl_type;
803   l_flexmap_increment        BOOLEAN := FALSE;
804 
805   l_set_of_books_id          NUMBER;
806 
807   l_return_status            VARCHAR2(1);
808 
809   /* Bug 3458191: Remove the following cursor and replace it by two queries
810      inside the body.
811   cursor c_WS is
812     select gl_cutoff_period,
813            nvl(global_worksheet_id, worksheet_id) global_worksheet_id,
814            nvl(allocrule_set_id, global_allocrule_set_id) allocrule_set_id,
815            budget_calendar_id,
816            rounding_factor,
817            stage_set_id,
818            flex_mapping_set_id,
819            current_stage_seq,
820            local_copy_flag
821       from PSB_WORKSHEETS_V
822      where worksheet_id = p_worksheet_id;
823   */
824 
825   cursor c_sp is
826     select service_package_id
827       from PSB_SERVICE_PACKAGES
828      where base_service_package = 'Y'
829        and global_worksheet_id = l_global_worksheet_id;
830 
831   cursor c_CCID is
832     select start_date_active,
833 	   end_date_active
834       from GL_CODE_COMBINATIONS
835      where code_combination_id = l_ccid;
836 
837   cursor c_sp_seq is
838     select psb_service_packages_s.nextval ServicePackageID
839       from dual;
840 
841   cursor c_wal1 is
842     select account_line_id, budget_group_id, request_id, annual_fte, ytd_amount,
843 	   period1_amount, period2_amount, period3_amount, period4_amount,
844 	   period5_amount, period6_amount, period7_amount, period8_amount,
845 	   period9_amount, period10_amount, period11_amount, period12_amount,
846 	   period13_amount, period14_amount, period15_amount, period16_amount,
847 	   period17_amount, period18_amount, period19_amount, period20_amount,
848 	   period21_amount, period22_amount, period23_amount, period24_amount,
849 	   period25_amount, period26_amount, period27_amount, period28_amount,
850 	   period29_amount, period30_amount, period31_amount, period32_amount,
851 	   period33_amount, period34_amount, period35_amount, period36_amount,
852 	   period37_amount, period38_amount, period39_amount, period40_amount,
853 	   period41_amount, period42_amount, period43_amount, period44_amount,
854 	   period45_amount, period46_amount, period47_amount, period48_amount,
855 	   period49_amount, period50_amount, period51_amount, period52_amount,
856 	   period53_amount, period54_amount, period55_amount, period56_amount,
857 	   period57_amount, period58_amount, period59_amount, period60_amount
858       from PSB_WS_ACCOUNT_LINES a
859      where currency_code = p_currency_code
860        and l_current_stage_seq between start_stage_seq and current_stage_seq
861        and balance_type = p_balance_type
862        and template_id = p_template_id
863        and position_line_id is null
864        and exists
865 	  (select 1
866 	     from PSB_WS_LINES b
867 	    where b.account_line_id = a.account_line_id
868 	      and b.worksheet_id = decode(nvl(l_local_copy_flag, 'N'), 'Y', p_worksheet_id, l_global_worksheet_id))
869        and stage_set_id = l_stage_set_id
870        and service_package_id = l_service_package_id
871        and budget_year_id = p_budget_year_id
872        and code_combination_id = l_ccid;
873 
874   cursor c_wal2 is
875     select account_line_id, budget_group_id, request_id, annual_fte, ytd_amount,
876 	   period1_amount, period2_amount, period3_amount, period4_amount,
877 	   period5_amount, period6_amount, period7_amount, period8_amount,
878 	   period9_amount, period10_amount, period11_amount, period12_amount,
879 	   period13_amount, period14_amount, period15_amount, period16_amount,
880 	   period17_amount, period18_amount, period19_amount, period20_amount,
881 	   period21_amount, period22_amount, period23_amount, period24_amount,
882 	   period25_amount, period26_amount, period27_amount, period28_amount,
883 	   period29_amount, period30_amount, period31_amount, period32_amount,
884 	   period33_amount, period34_amount, period35_amount, period36_amount,
885 	   period37_amount, period38_amount, period39_amount, period40_amount,
886 	   period41_amount, period42_amount, period43_amount, period44_amount,
887 	   period45_amount, period46_amount, period47_amount, period48_amount,
888 	   period49_amount, period50_amount, period51_amount, period52_amount,
889 	   period53_amount, period54_amount, period55_amount, period56_amount,
890 	   period57_amount, period58_amount, period59_amount, period60_amount
891       from PSB_WS_ACCOUNT_LINES a
892      where currency_code = p_currency_code
893        and l_current_stage_seq between start_stage_seq and current_stage_seq
894        and balance_type = p_balance_type
895        and template_id is null
896        and position_line_id = p_position_line_id
897        and element_set_id = p_element_set_id
898        and stage_set_id = l_stage_set_id
899        and service_package_id = l_service_package_id
900        and budget_year_id = p_budget_year_id
901        and code_combination_id = l_ccid;
902 
903   cursor c_wal3 is
904     select account_line_id, budget_group_id, request_id, annual_fte, ytd_amount,
905 	   period1_amount, period2_amount, period3_amount, period4_amount,
906 	   period5_amount, period6_amount, period7_amount, period8_amount,
907 	   period9_amount, period10_amount, period11_amount, period12_amount,
908 	   period13_amount, period14_amount, period15_amount, period16_amount,
909 	   period17_amount, period18_amount, period19_amount, period20_amount,
910 	   period21_amount, period22_amount, period23_amount, period24_amount,
911 	   period25_amount, period26_amount, period27_amount, period28_amount,
912 	   period29_amount, period30_amount, period31_amount, period32_amount,
913 	   period33_amount, period34_amount, period35_amount, period36_amount,
914 	   period37_amount, period38_amount, period39_amount, period40_amount,
915 	   period41_amount, period42_amount, period43_amount, period44_amount,
916 	   period45_amount, period46_amount, period47_amount, period48_amount,
917 	   period49_amount, period50_amount, period51_amount, period52_amount,
918 	   period53_amount, period54_amount, period55_amount, period56_amount,
919 	   period57_amount, period58_amount, period59_amount, period60_amount
920       from PSB_WS_ACCOUNT_LINES a
921      where currency_code = p_currency_code
922        and l_current_stage_seq between start_stage_seq and current_stage_seq
923        and balance_type = p_balance_type
924        and template_id is null
925        and position_line_id is null
926        and exists
927 	  (select 1
928 	     from PSB_WS_LINES b
929 	    where b.account_line_id = a.account_line_id
930 	      and b.worksheet_id = decode(nvl(l_local_copy_flag, 'N'), 'Y', p_worksheet_id, l_global_worksheet_id))
931        and stage_set_id = l_stage_set_id
932        and service_package_id = l_service_package_id
933        and budget_year_id = p_budget_year_id
934        and code_combination_id = l_ccid;
935 
936 BEGIN
937 
938   -- Standard call to check for call compatibility
939 
940   if not FND_API.Compatible_API_Call (l_api_version,
941 				      p_api_version,
942 				      l_api_name,
943 				      G_PKG_NAME)
944   then
945     raise FND_API.G_EXC_UNEXPECTED_ERROR;
946   end if;
947 
948   l_userid := FND_GLOBAL.USER_ID;
949   l_loginid := FND_GLOBAL.LOGIN_ID;
950   l_requestid := FND_GLOBAL.CONC_REQUEST_ID;
951 
952   -- Substitute default values for parameters that were not passed in
953 
954   if ((nvl(p_gl_cutoff_period, FND_API.G_MISS_DATE) = FND_API.G_MISS_DATE) or
955       (nvl(p_allocrule_set_id, FND_API.G_MISS_NUM) = FND_API.G_MISS_NUM) or
956       (nvl(p_budget_calendar_id, FND_API.G_MISS_NUM) = FND_API.G_MISS_NUM) or
957       (nvl(p_rounding_factor, FND_API.G_MISS_NUM) = FND_API.G_MISS_NUM) or
958       (nvl(p_flex_mapping_set_id, FND_API.G_MISS_NUM) = FND_API.G_MISS_NUM) or
959       (nvl(p_stage_set_id, FND_API.G_MISS_NUM) = FND_API.G_MISS_NUM) or
960       (nvl(p_current_stage_seq, FND_API.G_MISS_NUM) = FND_API.G_MISS_NUM)) then
961   begin
962     /* Bug 3458191: The following is removed using new conditions instead
963     for c_WS_Rec in c_WS loop
964       l_gl_cutoff_period := c_WS_Rec.gl_cutoff_period;
965       l_allocrule_set_id := c_WS_Rec.allocrule_set_id;
966       l_budget_calendar_id := c_WS_Rec.budget_calendar_id;
967       l_rounding_factor := c_WS_Rec.rounding_factor;
968       l_flex_mapping_set_id := c_WS_Rec.flex_mapping_set_id;
969       l_stage_set_id := c_WS_Rec.stage_set_id;
970       l_current_stage_seq := c_WS_Rec.current_stage_seq;
971     end loop;
972     */
973 
974     /* Bug 3458191 start */
975     -- Check the g_ws_creation_flag to determine whether to use the
976     -- cached worksheet values.
977     IF PSB_WORKSHEET.g_ws_creation_flag
978        AND
979        PSB_WORKSHEET.g_worksheet_id = p_worksheet_id
980     THEN
981       -- Retrieve the worksheet values from cache to avoiding extra query
982       l_global_worksheet_id := PSB_WORKSHEET.g_global_worksheet_id;
983       l_local_copy_flag := PSB_WORKSHEET.g_local_copy_flag;
984       l_gl_cutoff_period := PSB_WORKSHEET.g_gl_cutoff_period;
985       l_allocrule_set_id := PSB_WORKSHEET.g_allocrule_set_id;
986       l_budget_calendar_id := PSB_WORKSHEET.g_budget_calendar_id;
987       l_rounding_factor := PSB_WORKSHEET.g_rounding_factor;
988       l_flex_mapping_set_id := PSB_WORKSHEET.g_flex_mapping_set_id;
989       l_stage_set_id := PSB_WORKSHEET.g_stage_set_id;
990       l_current_stage_seq := PSB_WORKSHEET.g_current_stage_seq;
991 
992     ELSE
993 
994       SELECT DECODE(global_worksheet_flag, 'Y', worksheet_id,
995                     global_worksheet_id) global_worksheet_id,
996              local_copy_flag,
997 	     /* start bug 3871839 */
998 	     current_stage_seq
999 	     /* End bug 3871839 */
1000            INTO
1001              l_global_worksheet_id,
1002              l_local_copy_flag,
1003 	     /* start bug 3871839 */
1004 	     l_current_stage_seq
1005 	     /* end bug 3871839 */
1006       FROM   psb_worksheets
1007       WHERE  worksheet_id = p_worksheet_id;
1008 
1009       SELECT gl_cutoff_period,
1010              allocrule_set_id allocrule_set_id,
1011              budget_calendar_id,
1012              rounding_factor,
1013              stage_set_id,
1014              flex_mapping_set_id
1015              /* Bug no :3871839 commented out stage sequence*/
1016 	     /* current_stage_seq */
1017            INTO
1018              l_gl_cutoff_period,
1019              l_allocrule_set_id,
1020              l_budget_calendar_id,
1021              l_rounding_factor,
1022              l_stage_set_id,
1023              l_flex_mapping_set_id
1024 	     /* Bug No :3871839 Commented out l_current_stage_sequence */
1025             /*l_current_stage_seq*/
1026       FROM   psb_worksheets
1027       WHERE  worksheet_id = l_global_worksheet_id;
1028 
1029     END IF;
1030     /* Bug 3458191 end */
1031   end;
1032   end if;
1033 
1034   -- Override default values for parameters that were passed in
1035 
1036   if p_gl_cutoff_period <> FND_API.G_MISS_DATE then
1037     l_gl_cutoff_period := p_gl_cutoff_period;
1038   end if;
1039 
1040   if p_allocrule_set_id <> FND_API.G_MISS_NUM then
1041     l_allocrule_set_id := p_allocrule_set_id;
1042   end if;
1043 
1044   if p_budget_calendar_id <> FND_API.G_MISS_NUM then
1045     l_budget_calendar_id := p_budget_calendar_id;
1046   end if;
1047 
1048   if p_rounding_factor <> FND_API.G_MISS_NUM then
1049     l_rounding_factor := p_rounding_factor;
1050   end if;
1051 
1052   if p_stage_set_id <> FND_API.G_MISS_NUM then
1053     l_stage_set_id := p_stage_set_id;
1054   end if;
1055 
1056   if p_flex_mapping_set_id <> FND_API.G_MISS_NUM then
1057     l_flex_mapping_set_id := p_flex_mapping_set_id;
1058   end if;
1059 
1060   if p_current_stage_seq <> FND_API.G_MISS_NUM then
1061     l_current_stage_seq := p_current_stage_seq;
1062   end if;
1063 
1064   if p_start_stage_seq = FND_API.G_MISS_NUM then
1065     l_start_stage_seq := l_current_stage_seq;
1066   else
1067     l_start_stage_seq := p_start_stage_seq;
1068   end if;
1069 
1070   /* Bug 3458191: Change condition since global_worksheet_id and
1071      local_copy_flag have cached.
1072   if ((PSB_WS_POS1.g_global_worksheet_id is null) or
1073       (PSB_WS_POS1.g_local_copy_flag is null)) then
1074   begin
1075 
1076     for c_WS_Rec in c_WS loop
1077       l_global_worksheet_id := c_WS_Rec.global_worksheet_id;
1078       l_local_copy_flag := c_WS_Rec.local_copy_flag;
1079     end loop;
1080 
1081   end;
1082   else
1083   */
1084 
1085   -- Bug 3458191: New condition to replace the above ELSE condition
1086   if ((PSB_WS_POS1.g_global_worksheet_id is not null)
1087       and
1088       (PSB_WS_POS1.g_local_copy_flag is not null))
1089   then
1090     l_global_worksheet_id := PSB_WS_POS1.g_global_worksheet_id;
1091     l_local_copy_flag := PSB_WS_POS1.g_local_copy_flag;
1092   end if;
1093 
1094   if p_service_package_id = FND_API.G_MISS_NUM then
1095   begin
1096 
1097     for c_sp_rec in c_sp loop
1098       l_service_package_id := c_sp_rec.service_package_id;
1099     end loop;
1100 
1101   end;
1102   else
1103     l_service_package_id := p_service_package_id;
1104   end if;
1105 
1106   -- Either CCID or (Chart of Accounts ID and Concatenated Segments) must
1107   -- be entered
1108 
1109   if p_ccid = FND_API.G_MISS_NUM then
1110   begin
1111 
1112     if ((p_flex_code = FND_API.G_MISS_NUM) or
1113 	(p_concatenated_segments = FND_API.G_MISS_CHAR)) then
1114 
1115       message_token('ROUTINE', 'PSB_WS_ACCT1.Create_Account_Dist');
1116       add_message('PSB', 'PSB_INVALID_ARGUMENT');
1117       raise FND_API.G_EXC_ERROR;
1118 
1119     end if;
1120 
1121     l_ccid := FND_FLEX_EXT.Get_CCID
1122 		 (application_short_name => 'SQLGL',
1123 		  key_flex_code => 'GL#',
1124 		  structure_number => p_flex_code,
1125 		  validation_date => to_char(sysdate, 'YYYY/MM/DD HH24:MI:SS'),
1126 		  concatenated_segments => p_concatenated_segments);
1127 
1128     if l_ccid = 0 then
1129       FND_MSG_PUB.Add;
1130       raise FND_API.G_EXC_ERROR;
1131     end if;
1132 
1133   end;
1134   else
1135     l_ccid := p_ccid;
1136   end if;
1137 
1138   -- Cache Budget Calendar so that it can be reused across the Worksheet Creation
1139   -- Modules
1140 
1141   if l_budget_calendar_id <> nvl(g_budget_calendar_id, FND_API.G_MISS_NUM) then
1142   begin
1143 
1144     Cache_Budget_Calendar
1145 	 (p_return_status => l_return_status,
1146 	  p_budget_calendar_id => l_budget_calendar_id);
1147 
1148     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1149       raise FND_API.G_EXC_ERROR;
1150     end if;
1151 
1152   end;
1153   end if;
1154 
1155   -- Find last Budget Period for the Budget Year. The rounding difference is adjusted
1156   -- to the last period
1157 
1158   for l_year_index in 1..g_num_budget_years loop
1159 
1160     if g_budget_years(l_year_index).budget_year_id = p_budget_year_id then
1161       l_last_period_index := g_budget_years(l_year_index).last_period_index;
1162       l_budget_year_type_id := g_budget_years(l_year_index).budget_year_type_id;
1163       exit;
1164     end if;
1165 
1166   end loop;
1167 
1168   -- If FlexMapping Set has been defined map the ccid based on the flex mapping
1169   -- Flex Mapping is done only for detailed non-position accounts that have an annual balance
1170 
1171   if ((p_map_accounts) and
1172       (nvl(p_template_id, FND_API.G_MISS_NUM) = FND_API.G_MISS_NUM) and (p_ytd_amount <> 0)) then
1173   begin
1174 
1175     l_ccid := Map_Account
1176 		 (p_flex_mapping_set_id => l_flex_mapping_set_id,
1177 		  p_ccid => l_ccid,
1178 		  p_budget_year_type_id => l_budget_year_type_id);
1179 
1180     l_flexmap_increment := TRUE;
1181 
1182   end;
1183   end if;
1184 
1185   -- p_check_spal_exists must be set to FND_API.G_TRUE if called from any Worksheet
1186   -- Modification module (Form, Spreadsheet, OFA)
1187 
1188   if FND_API.to_boolean(p_check_spal_exists) then
1189   begin
1190 
1191     -- Find existing Account Line that matches the Account Line specified by the input
1192     -- parameters
1193 
1194     sql_wal := 'select account_line_id, ' ||
1195 		      'budget_group_id, ' ||
1196 		      'ytd_amount, ' ||
1197                       /* Bug 3347507 start */
1198                       'start_stage_seq ' ||
1199                       /* Bug 3347507 end */
1200 		 'from PSB_WS_ACCOUNT_LINES a ' ||
1201 		'where ' || l_current_stage_seq || ' between start_stage_seq and current_stage_seq ';
1202 
1203     if p_template_id <> FND_API.G_MISS_NUM then
1204       sql_wal := sql_wal ||
1205 		'and template_id = ' || p_template_id || ' ';
1206     else
1207       sql_wal := sql_wal ||
1208 		'and template_id is null ';
1209     end if;
1210 
1211     -- Position Line ID and Element Set ID must be matched for Position Account Lines
1212 
1213     if p_position_line_id <> FND_API.G_MISS_NUM then
1214       sql_wal := sql_wal ||
1215 		'and position_line_id = ' || p_position_line_id || ' ';
1216     else
1217       sql_wal := sql_wal ||
1218 		'and position_line_id is null ';
1219     end if;
1220 
1221     if p_element_set_id <> FND_API.G_MISS_NUM then
1222       sql_wal := sql_wal ||
1223 		'and element_set_id = ' || p_element_set_id || ' ';
1224     else
1225       sql_wal := sql_wal ||
1226 		'and element_set_id is null ';
1227     end if;
1228 
1229     sql_wal := sql_wal ||
1230 	      'and currency_code = ''' || p_currency_code || ''' ' ||
1231 	      'and balance_type = ''' || p_balance_type || ''' ';
1232 
1233     -- The Account Line must be unique across Global Worksheets and Local Copies
1234     -- of Worksheets. Position Line IDs are anyway unique across Global Worksheets
1235     -- and Local Copies of Worksheets; hence, this is enforced only for non-Position
1236     -- Account Lines
1237 
1238     if p_position_line_id = FND_API.G_MISS_NUM then
1239     begin
1240 
1241       sql_wal := sql_wal ||
1242 		'and exists ' ||
1243 		   '(select 1 ' ||
1244 		      'from PSB_WS_LINES b ' ||
1245 		     'where b.account_line_id = a.account_line_id ' ||
1246 		       'and b.worksheet_id = ' || p_worksheet_id || ') ';
1247 
1248     end;
1249     end if;
1250 
1251     sql_wal := sql_wal ||
1252 	      'and stage_set_id = ' || l_stage_set_id || ' ' ||
1253 	      'and service_package_id = ' || l_service_package_id || ' ' ||
1254 	      'and budget_year_id = ' || p_budget_year_id || ' ' ||
1255 	      'and code_combination_id = ' || l_ccid;
1256 
1257     cur_wal := dbms_sql.open_cursor;
1258     dbms_sql.parse(cur_wal, sql_wal, dbms_sql.v7);
1259 
1260     dbms_sql.define_column(cur_wal, 1, l_spal_id);
1261     dbms_sql.define_column(cur_wal, 2, l_spal_budget_group_id);
1262     dbms_sql.define_column(cur_wal, 3, l_spytd_amount);
1263     -- Bug 3347507
1264     dbms_sql.define_column(cur_wal, 4, l_original_start_stage_seq);
1265 
1266     num_wal := dbms_sql.execute(cur_wal);
1267 
1268     loop
1269 
1270       if dbms_sql.fetch_rows(cur_wal) = 0 then
1271 	exit;
1272       end if;
1273 
1274       dbms_sql.column_value(cur_wal, 1, l_spal_id);
1275       dbms_sql.column_value(cur_wal, 2, l_spal_budget_group_id);
1276       dbms_sql.column_value(cur_wal, 3, l_spytd_amount);
1277       -- Bug 3347507
1278       dbms_sql.column_value(cur_wal, 4, l_original_start_stage_seq);
1279 
1280       l_spal_exists := TRUE;
1281 
1282     end loop;
1283 
1284     dbms_sql.close_cursor(cur_wal);
1285 
1286     if l_spal_budget_group_id <> p_budget_group_id then
1287       l_budget_group_changed := TRUE;
1288     end if;
1289 
1290   end;
1291   end if;
1292 
1293   -- p_distribute_flag must be set to FND_API.G_TRUE to automatically
1294   -- distribute YTD Amounts into Period Amounts for CY and PP Budget
1295   -- Years. This may be set to FND_API.G_TRUE when called from any
1296   -- Worksheet Modification module (Form, Spreadsheet, OFA)
1297 
1298   if FND_API.to_Boolean(p_distribute_flag) then
1299   begin
1300 
1301     for c_CCID_Rec in c_CCID loop
1302       l_ccid_start_date := c_CCID_Rec.start_date_active;
1303       l_ccid_end_date := c_CCID_Rec.end_date_active;
1304     end loop;
1305 
1306     for l_year_index in 1..g_num_budget_years loop
1307 
1308       if g_budget_years(l_year_index).budget_year_id = p_budget_year_id then
1309       begin
1310 
1311 	l_start_date := g_budget_years(l_year_index).start_date;
1312 	l_end_date := g_budget_years(l_year_index).end_date;
1313 
1314 	for l_init_index in 1..l_budget_periods.Count loop
1315 	  l_budget_periods(l_init_index).budget_period_id := null;
1316 	  l_budget_periods(l_init_index).start_date := null;
1317 	  l_budget_periods(l_init_index).end_date := null;
1318 	  l_budget_periods(l_init_index).long_sequence_no := null;
1319 	  l_budget_periods(l_init_index).budget_year_id := null;
1320 	end loop;
1321 
1322 	l_init_index := 1;
1323 
1324 	for l_period_index in 1..g_num_budget_periods loop
1325 
1326 	  if g_budget_periods(l_period_index).budget_year_id = p_budget_year_id then
1327 	  begin
1328 
1329 	    -- Pick up all Budget Periods for a PP Budget Year or all Budget Periods after the
1330 	    -- GL Cutoff Date for a CY Budget Year
1331 
1332 	    if (((l_ccid_start_date is null) or
1333 		 (l_ccid_start_date <= g_budget_periods(l_period_index).start_date)) and
1334 		((l_ccid_end_date is null) or
1335 		 (l_ccid_end_date >= g_budget_periods(l_period_index).end_date)) and
1336 		((g_budget_years(l_year_index).year_type = 'PP') or
1337 		((g_budget_years(l_year_index).year_type = 'CY') and
1338 		((l_gl_cutoff_period is null) or (l_gl_cutoff_period < g_budget_periods(l_period_index).start_date))))) then
1339 	    begin
1340 
1341 	      l_budget_periods(l_init_index).budget_period_id := g_budget_periods(l_period_index).budget_period_id;
1342 	      l_budget_periods(l_init_index).long_sequence_no := g_budget_periods(l_period_index).long_sequence_no;
1343 	      l_budget_periods(l_init_index).start_date := g_budget_periods(l_period_index).start_date;
1344 	      l_budget_periods(l_init_index).end_date := g_budget_periods(l_period_index).end_date;
1345 	      l_budget_periods(l_init_index).budget_year_id := p_budget_year_id;
1346 
1347 	      l_init_index := l_init_index + 1;
1348 
1349 	    end;
1350 	    end if;
1351 
1352 	  end;
1353 	  end if;
1354 
1355 	end loop;
1356 
1357       end;
1358       end if;
1359 
1360     end loop;
1361 
1362     for l_index in 1..g_max_num_amounts loop
1363       l_period_amount_tbl(l_index) := null;
1364     end loop;
1365 
1366     /* Bug 3352171 start */
1367     -- Comment out the following two lines. The allocation logic will be
1368     -- handled in the PSB_WS_ACCT2.Distribute_Account_Lines function.
1369     --if nvl(l_allocrule_set_id, FND_API.G_MISS_NUM) <> FND_API.G_MISS_NUM then
1370     --begin
1371     /* Bug 3352171 end */
1372 
1373     -- If existing Account Line was found, add YTD Amount to YTD Amount of
1374     -- existing line
1375 
1376     if l_spal_exists then
1377       /* bug start 3996052 */
1378       IF NVL(p_update_cy_estimate, 'N') = 'Y' THEN
1379 	l_ytd_amount := nvl(p_ytd_amount, 0);
1380       ELSE
1381         l_ytd_amount := nvl(p_ytd_amount, 0) + l_spytd_amount;
1382       END IF;
1383       /* bug end 3996052 */
1384     else
1385       l_ytd_amount := nvl(p_ytd_amount, 0);
1386     end if;
1387 
1388     PSB_WS_ACCT2.Distribute_Account_Lines
1389     ( p_return_status => l_return_status,
1390       p_worksheet_id => p_worksheet_id,
1391       p_flex_mapping_set_id => l_flex_mapping_set_id,
1392       p_budget_year_type_id => l_budget_year_type_id,
1393       p_allocrule_set_id => l_allocrule_set_id,
1394       p_budget_calendar_id => l_budget_calendar_id,
1395       p_currency_code => p_currency_code,
1396       p_ccid => l_ccid,
1397       p_ytd_amount => l_ytd_amount,
1398       p_allocation_type => 'PERCENT',
1399       /* Bug No 2342169 Start */
1400       p_rounding_factor => l_rounding_factor,
1401       /* Bug No 2342169 End */
1402       p_effective_start_date => l_start_date,
1403       p_effective_end_date => l_end_date,
1404       p_budget_periods => l_budget_periods,
1405       p_period_amount => l_period_amount_tbl);
1406 
1407     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1408       raise FND_API.G_EXC_ERROR;
1409     end if;
1410 
1411     /* Bug 3352171 start */
1412     -- comment out the following two lines. The allocation logic should not
1413     -- be handling here.
1414     --end;
1415     --end if;
1416     /* Bug 3352171 end */
1417 
1418   end;
1419   end if;
1420 
1421   l_ytd_amount := 0;
1422 
1423   -- First, try to update existing Account Line to determine if it exists; if it does not exist
1424   -- insert the new Account Line
1425 
1426   for l_index in 1..g_max_num_amounts loop
1427 
1428     if FND_API.to_Boolean(p_distribute_flag) then
1429     begin
1430 
1431       if l_rounding_factor is null then
1432 	l_period_amount := l_period_amount_tbl(l_index);
1433       else
1434 	l_period_amount := ROUND(l_period_amount_tbl(l_index)/l_rounding_factor) * l_rounding_factor;
1435       end if;
1436 
1437       l_ytd_amount := l_ytd_amount + nvl(l_period_amount, 0);
1438 
1439     end;
1440     else
1441     begin
1442 
1443       if l_rounding_factor is null then
1444 	l_period_amount := p_period_amount(l_index);
1445       else
1446 	l_period_amount := ROUND(p_period_amount(l_index)/l_rounding_factor) * l_rounding_factor;
1447       end if;
1448 
1449       l_ytd_amount := l_ytd_amount + nvl(l_period_amount, 0);
1450 
1451     end;
1452     end if;
1453 
1454   end loop;
1455 
1456   if l_rounding_factor is null then
1457   begin
1458 
1459     if l_spal_exists then
1460       /* bug start 3996052 */
1461       IF NVL(p_update_cy_estimate, 'N') = 'Y' THEN
1462         l_rounded_ytd_amount := nvl(p_ytd_amount, 0);
1463       ELSE
1464         l_rounded_ytd_amount := nvl(p_ytd_amount, 0) + l_spytd_amount;
1465       END IF;
1466       /* bug end 3996052 */
1467     else
1468       l_rounded_ytd_amount := nvl(p_ytd_amount, 0);
1469     end if;
1470 
1471     l_rounding_difference := 0;
1472 
1473   end;
1474   else
1475   begin
1476 
1477     l_rounded_ytd_amount := ROUND(nvl(p_ytd_amount, 0)/l_rounding_factor) * l_rounding_factor;
1478 
1479     if l_spal_exists then
1480       /* bug start 3996052 */
1481       IF NVL(p_update_cy_estimate, 'N') = 'Y' THEN
1482         l_rounded_ytd_amount := l_rounded_ytd_amount;
1483       ELSE
1484         l_rounded_ytd_amount := l_rounded_ytd_amount + l_spytd_amount;
1485       END IF;
1486       /* bug end 3996052 */
1487     end if;
1488 
1489 /* Bug No 2379695 Start */
1490 -- Commented the IF condition
1491 --    if ((l_ytd_amount > 0) and (l_rounded_ytd_amount > 0)) then
1492       l_rounding_difference := l_rounded_ytd_amount - l_ytd_amount;
1493 --    end if;
1494 /* Bug No 2379695 End */
1495 
1496   end;
1497   end if;
1498 
1499   for l_index in 1..g_max_num_amounts loop
1500 
1501     if FND_API.to_Boolean(p_distribute_flag) then
1502     begin
1503 
1504       if l_rounding_factor is null then
1505 	l_period_amount := l_period_amount_tbl(l_index);
1506       else
1507 	l_period_amount := ROUND(l_period_amount_tbl(l_index)/l_rounding_factor) * l_rounding_factor;
1508       end if;
1509 
1510     end;
1511     else
1512     begin
1513 
1514       if l_rounding_factor is null then
1515 	l_period_amount := p_period_amount(l_index);
1516       else
1517 	l_period_amount := ROUND(p_period_amount(l_index)/l_rounding_factor) * l_rounding_factor;
1518       end if;
1519 
1520     end;
1521     end if;
1522 
1523     if l_period_amount is null then
1524     begin
1525 
1526       if ((l_index = l_last_period_index) and (l_ytd_amount <> 0)) then
1527 	l_period_amounts(l_index) := nvl(l_rounding_difference, 0);
1528       else
1529 	l_period_amounts(l_index) := null;
1530       end if;
1531 
1532     end;
1533     else
1534     begin
1535 
1536       if ((l_index = l_last_period_index) and (l_ytd_amount <> 0)) then
1537 	l_period_amounts(l_index) := l_period_amount + nvl(l_rounding_difference, 0);
1538       else
1539 	l_period_amounts(l_index) := l_period_amount;
1540       end if;
1541 
1542     end;
1543     end if;
1544 
1545   end loop;
1546 
1547   /* Bug 3347507 start */
1548   -- If an existing account line was found, check whether the start_stage_seq
1549   -- is the same as current stage seq. If start_stage_seq is differ from the
1550   -- current stage seq, the program should create a new account line with the
1551   -- incremented values for the current stage seq and end stage the current
1552   -- account line.
1553   -- if l_spal_exists then
1554   if l_spal_exists and l_original_start_stage_seq <> l_current_stage_seq then
1555   begin
1556 
1557     Create_Account_Dist
1558     (
1559       p_api_version                 => 1.0,
1560       p_validation_level            => FND_API.G_VALID_LEVEL_FULL,
1561       p_return_status               => l_return_status,
1562       p_worksheet_id                => p_worksheet_id,
1563       p_distribute_flag             => p_distribute_flag,
1564       p_account_line_id             => l_spal_id,
1565       p_ytd_amount                  => l_rounded_ytd_amount,
1566       p_service_package_id          => l_service_package_id,
1567       p_period_amount               => p_period_amount,
1568       /* start bug 4128196 */
1569       p_update_cy_estimate          => NVL(p_update_cy_estimate, 'N')
1570       /* end bug  4128196 */
1571     );
1572   end;
1573 
1574   -- If existing Account Line was found but the start stage is the same as the current stage,
1575   -- increment the values for the account line with the values passed in by the input parameters
1576   elsif l_spal_exists then
1577   /* Bug 3347507 end */
1578   begin
1579 
1580 
1581     update PSB_WS_ACCOUNT_LINES a
1582        set budget_group_id = p_budget_group_id,
1583 	   current_stage_seq = l_current_stage_seq,
1584 	   end_stage_seq = decode(p_end_stage_seq, FND_API.G_MISS_NUM, end_stage_seq, p_end_stage_seq),
1585 	   annual_fte = decode(nvl(p_annual_fte, FND_API.G_MISS_NUM), FND_API.G_MISS_NUM, annual_fte, nvl(p_annual_fte, FND_API.G_MISS_NUM)),
1586 	   copy_of_account_line_id = decode(p_copy_of_account_line_id, FND_API.G_MISS_NUM, copy_of_account_line_id, p_copy_of_account_line_id),
1587 	   period1_amount = l_period_amounts(1), period2_amount = l_period_amounts(2),
1588 	   period3_amount = l_period_amounts(3), period4_amount = l_period_amounts(4),
1589 	   period5_amount = l_period_amounts(5), period6_amount = l_period_amounts(6),
1590 	   period7_amount = l_period_amounts(7), period8_amount = l_period_amounts(8),
1591 	   period9_amount = l_period_amounts(9), period10_amount = l_period_amounts(10),
1592 	   period11_amount = l_period_amounts(11), period12_amount = l_period_amounts(12),
1593 	   period13_amount = l_period_amounts(13), period14_amount = l_period_amounts(14),
1594 	   period15_amount = l_period_amounts(15), period16_amount = l_period_amounts(16),
1595 	   period17_amount = l_period_amounts(17), period18_amount = l_period_amounts(18),
1596 	   period19_amount = l_period_amounts(19), period20_amount = l_period_amounts(20),
1597 	   period21_amount = l_period_amounts(21), period22_amount = l_period_amounts(22),
1598 	   period23_amount = l_period_amounts(23), period24_amount = l_period_amounts(24),
1599 	   period25_amount = l_period_amounts(25), period26_amount = l_period_amounts(26),
1600 	   period27_amount = l_period_amounts(27), period28_amount = l_period_amounts(28),
1601 	   period29_amount = l_period_amounts(29), period30_amount = l_period_amounts(30),
1602 	   period31_amount = l_period_amounts(31), period32_amount = l_period_amounts(32),
1603 	   period33_amount = l_period_amounts(33), period34_amount = l_period_amounts(34),
1604 	   period35_amount = l_period_amounts(35), period36_amount = l_period_amounts(36),
1605 	   period37_amount = l_period_amounts(37), period38_amount = l_period_amounts(38),
1606 	   period39_amount = l_period_amounts(39), period40_amount = l_period_amounts(40),
1607 	   period41_amount = l_period_amounts(41), period42_amount = l_period_amounts(42),
1608 	   period43_amount = l_period_amounts(43), period44_amount = l_period_amounts(44),
1609 	   period45_amount = l_period_amounts(45), period46_amount = l_period_amounts(46),
1610 	   period47_amount = l_period_amounts(47), period48_amount = l_period_amounts(48),
1611 	   period49_amount = l_period_amounts(49), period50_amount = l_period_amounts(50),
1612 	   period51_amount = l_period_amounts(51), period52_amount = l_period_amounts(52),
1613 	   period53_amount = l_period_amounts(53), period54_amount = l_period_amounts(54),
1614 	   period55_amount = l_period_amounts(55), period56_amount = l_period_amounts(56),
1615 	   period57_amount = l_period_amounts(57), period58_amount = l_period_amounts(58),
1616 	   period59_amount = l_period_amounts(59), period60_amount = l_period_amounts(60),
1617 	   ytd_amount = l_rounded_ytd_amount,
1618 	   last_update_date = sysdate,
1619 	   last_updated_by = l_userid,
1620 	   last_update_login = l_loginid
1621      where account_line_id = l_spal_id;
1622 
1623       l_account_line_id := l_spal_id;
1624 
1625 
1626   end;
1627   else
1628   begin
1629 
1630 
1631     if p_template_id <> FND_API.G_MISS_NUM then
1632     begin
1633 
1634       -- Non-Position Account Line must be unique across Global Worksheets and
1635       -- Local Copies of Worksheets; Position Line IDs are anyway unique across
1636       -- Global Worksheets and Local Copies of Worksheets
1637 
1638 
1639       for c_wal_rec in c_wal1 loop
1640 	l_account_line_id := c_wal_rec.account_line_id;
1641 	l_budget_group_id := c_wal_rec.budget_group_id;
1642 	l_current_requestid := c_wal_rec.request_id;
1643 	l_current_fte := c_wal_rec.annual_fte; l_current_ytdamt := c_wal_rec.ytd_amount;
1644 	l_current_prdamt(1) := c_wal_rec.period1_amount; l_current_prdamt(2) := c_wal_rec.period2_amount;
1645 	l_current_prdamt(3) := c_wal_rec.period3_amount; l_current_prdamt(4) := c_wal_rec.period4_amount;
1646 	l_current_prdamt(5) := c_wal_rec.period5_amount; l_current_prdamt(6) := c_wal_rec.period6_amount;
1647 	l_current_prdamt(7) := c_wal_rec.period7_amount; l_current_prdamt(8) := c_wal_rec.period8_amount;
1648 	l_current_prdamt(9) := c_wal_rec.period9_amount; l_current_prdamt(10) := c_wal_rec.period10_amount;
1649 	l_current_prdamt(11) := c_wal_rec.period11_amount; l_current_prdamt(12) := c_wal_rec.period12_amount;
1650 	l_current_prdamt(13) := c_wal_rec.period13_amount; l_current_prdamt(14) := c_wal_rec.period14_amount;
1651 	l_current_prdamt(15) := c_wal_rec.period15_amount; l_current_prdamt(16) := c_wal_rec.period16_amount;
1652 	l_current_prdamt(17) := c_wal_rec.period17_amount; l_current_prdamt(18) := c_wal_rec.period18_amount;
1653 	l_current_prdamt(19) := c_wal_rec.period19_amount; l_current_prdamt(20) := c_wal_rec.period20_amount;
1654 	l_current_prdamt(21) := c_wal_rec.period21_amount; l_current_prdamt(22) := c_wal_rec.period22_amount;
1655 	l_current_prdamt(23) := c_wal_rec.period23_amount; l_current_prdamt(24) := c_wal_rec.period24_amount;
1656 	l_current_prdamt(25) := c_wal_rec.period25_amount; l_current_prdamt(26) := c_wal_rec.period26_amount;
1657 	l_current_prdamt(27) := c_wal_rec.period27_amount; l_current_prdamt(28) := c_wal_rec.period28_amount;
1658 	l_current_prdamt(29) := c_wal_rec.period29_amount; l_current_prdamt(30) := c_wal_rec.period30_amount;
1659 	l_current_prdamt(31) := c_wal_rec.period31_amount; l_current_prdamt(32) := c_wal_rec.period32_amount;
1660 	l_current_prdamt(33) := c_wal_rec.period33_amount; l_current_prdamt(34) := c_wal_rec.period34_amount;
1661 	l_current_prdamt(35) := c_wal_rec.period35_amount; l_current_prdamt(36) := c_wal_rec.period36_amount;
1662 	l_current_prdamt(37) := c_wal_rec.period37_amount; l_current_prdamt(38) := c_wal_rec.period38_amount;
1663 	l_current_prdamt(39) := c_wal_rec.period39_amount; l_current_prdamt(40) := c_wal_rec.period40_amount;
1664 	l_current_prdamt(41) := c_wal_rec.period41_amount; l_current_prdamt(42) := c_wal_rec.period42_amount;
1665 	l_current_prdamt(43) := c_wal_rec.period43_amount; l_current_prdamt(44) := c_wal_rec.period44_amount;
1666 	l_current_prdamt(45) := c_wal_rec.period45_amount; l_current_prdamt(46) := c_wal_rec.period46_amount;
1667 	l_current_prdamt(47) := c_wal_rec.period47_amount; l_current_prdamt(48) := c_wal_rec.period48_amount;
1668 	l_current_prdamt(49) := c_wal_rec.period49_amount; l_current_prdamt(50) := c_wal_rec.period50_amount;
1669 	l_current_prdamt(51) := c_wal_rec.period51_amount; l_current_prdamt(52) := c_wal_rec.period52_amount;
1670 	l_current_prdamt(53) := c_wal_rec.period53_amount; l_current_prdamt(54) := c_wal_rec.period54_amount;
1671 	l_current_prdamt(55) := c_wal_rec.period55_amount; l_current_prdamt(56) := c_wal_rec.period56_amount;
1672 	l_current_prdamt(57) := c_wal_rec.period57_amount; l_current_prdamt(58) := c_wal_rec.period58_amount;
1673 	l_current_prdamt(59) := c_wal_rec.period59_amount; l_current_prdamt(60) := c_wal_rec.period60_amount;
1674       end loop;
1675 
1676       if ((nvl(l_current_requestid, FND_API.G_MISS_NUM) <> nvl(l_requestid, FND_API.G_MISS_NUM)) or
1677 	  (p_balance_type = 'E')) then
1678 	l_flexmap_increment := FALSE;
1679       end if;
1680 
1681       if l_flexmap_increment then
1682       begin
1683 
1684 	update PSB_WS_ACCOUNT_LINES
1685 	   set budget_group_id = p_budget_group_id,
1686 	       current_stage_seq = l_current_stage_seq,
1687 	       end_stage_seq = decode(p_end_stage_seq, FND_API.G_MISS_NUM, end_stage_seq, p_end_stage_seq),
1688 	       annual_fte = decode(nvl(p_annual_fte, FND_API.G_MISS_NUM), FND_API.G_MISS_NUM, annual_fte, p_annual_fte + nvl(l_current_fte, 0)),
1689 	       copy_of_account_line_id = decode(p_copy_of_account_line_id, FND_API.G_MISS_NUM, copy_of_account_line_id, p_copy_of_account_line_id),
1690 	       period1_amount = l_period_amounts(1) + l_current_prdamt(1),
1691 	       period2_amount = l_period_amounts(2) + l_current_prdamt(2),
1692 	       period3_amount = l_period_amounts(3) + l_current_prdamt(3),
1693 	       period4_amount = l_period_amounts(4) + l_current_prdamt(4),
1694 	       period5_amount = l_period_amounts(5) + l_current_prdamt(5),
1695 	       period6_amount = l_period_amounts(6) + l_current_prdamt(6),
1696 	       period7_amount = l_period_amounts(7) + l_current_prdamt(7),
1697 	       period8_amount = l_period_amounts(8) + l_current_prdamt(8),
1698 	       period9_amount = l_period_amounts(9) + l_current_prdamt(9),
1699 	       period10_amount = l_period_amounts(10) + l_current_prdamt(10),
1700 	       period11_amount = l_period_amounts(11) + l_current_prdamt(11),
1701 	       period12_amount = l_period_amounts(12) + l_current_prdamt(12),
1702 	       period13_amount = l_period_amounts(13) + l_current_prdamt(13),
1703 	       period14_amount = l_period_amounts(14) + l_current_prdamt(14),
1704 	       period15_amount = l_period_amounts(15) + l_current_prdamt(15),
1705 	       period16_amount = l_period_amounts(16) + l_current_prdamt(16),
1706 	       period17_amount = l_period_amounts(17) + l_current_prdamt(17),
1707 	       period18_amount = l_period_amounts(18) + l_current_prdamt(18),
1708 	       period19_amount = l_period_amounts(19) + l_current_prdamt(19),
1709 	       period20_amount = l_period_amounts(20) + l_current_prdamt(20),
1710 	       period21_amount = l_period_amounts(21) + l_current_prdamt(21),
1711 	       period22_amount = l_period_amounts(22) + l_current_prdamt(22),
1712 	       period23_amount = l_period_amounts(23) + l_current_prdamt(23),
1713 	       period24_amount = l_period_amounts(24) + l_current_prdamt(24),
1714 	       period25_amount = l_period_amounts(25) + l_current_prdamt(25),
1715 	       period26_amount = l_period_amounts(26) + l_current_prdamt(26),
1716 	       period27_amount = l_period_amounts(27) + l_current_prdamt(27),
1717 	       period28_amount = l_period_amounts(28) + l_current_prdamt(28),
1718 	       period29_amount = l_period_amounts(29) + l_current_prdamt(29),
1719 	       period30_amount = l_period_amounts(30) + l_current_prdamt(30),
1720 	       period31_amount = l_period_amounts(31) + l_current_prdamt(31),
1721 	       period32_amount = l_period_amounts(32) + l_current_prdamt(32),
1722 	       period33_amount = l_period_amounts(33) + l_current_prdamt(33),
1723 	       period34_amount = l_period_amounts(34) + l_current_prdamt(34),
1724 	       period35_amount = l_period_amounts(35) + l_current_prdamt(35),
1725 	       period36_amount = l_period_amounts(36) + l_current_prdamt(36),
1726 	       period37_amount = l_period_amounts(37) + l_current_prdamt(37),
1727 	       period38_amount = l_period_amounts(38) + l_current_prdamt(38),
1728 	       period39_amount = l_period_amounts(39) + l_current_prdamt(39),
1729 	       period40_amount = l_period_amounts(40) + l_current_prdamt(40),
1730 	       period41_amount = l_period_amounts(41) + l_current_prdamt(41),
1731 	       period42_amount = l_period_amounts(42) + l_current_prdamt(42),
1732 	       period43_amount = l_period_amounts(43) + l_current_prdamt(43),
1733 	       period44_amount = l_period_amounts(44) + l_current_prdamt(44),
1734 	       period45_amount = l_period_amounts(45) + l_current_prdamt(45),
1735 	       period46_amount = l_period_amounts(46) + l_current_prdamt(46),
1736 	       period47_amount = l_period_amounts(47) + l_current_prdamt(47),
1737 	       period48_amount = l_period_amounts(48) + l_current_prdamt(48),
1738 	       period49_amount = l_period_amounts(49) + l_current_prdamt(49),
1739 	       period50_amount = l_period_amounts(50) + l_current_prdamt(50),
1740 	       period51_amount = l_period_amounts(51) + l_current_prdamt(51),
1741 	       period52_amount = l_period_amounts(52) + l_current_prdamt(52),
1742 	       period53_amount = l_period_amounts(53) + l_current_prdamt(53),
1743 	       period54_amount = l_period_amounts(54) + l_current_prdamt(54),
1744 	       period55_amount = l_period_amounts(55) + l_current_prdamt(55),
1745 	       period56_amount = l_period_amounts(56) + l_current_prdamt(56),
1746 	       period57_amount = l_period_amounts(57) + l_current_prdamt(57),
1747 	       period58_amount = l_period_amounts(58) + l_current_prdamt(58),
1748 	       period59_amount = l_period_amounts(59) + l_current_prdamt(59),
1749 	       period60_amount = l_period_amounts(60) + l_current_prdamt(60),
1750 	       ytd_amount = l_rounded_ytd_amount + l_current_ytdamt,
1751 	       last_update_date = sysdate,
1752 	       last_updated_by = l_userid,
1753 	       last_update_login = l_loginid,
1754 	       request_id = l_requestid
1755 	 where account_line_id = l_account_line_id;
1756 
1757       end;
1758       else
1759       begin
1760 
1761 	update PSB_WS_ACCOUNT_LINES
1762 	   set budget_group_id = p_budget_group_id,
1763 	       current_stage_seq = l_current_stage_seq,
1764 	       end_stage_seq = decode(p_end_stage_seq, FND_API.G_MISS_NUM, end_stage_seq, p_end_stage_seq),
1765 	       annual_fte = decode(nvl(p_annual_fte, FND_API.G_MISS_NUM), FND_API.G_MISS_NUM, annual_fte, p_annual_fte),
1766 	       copy_of_account_line_id = decode(p_copy_of_account_line_id, FND_API.G_MISS_NUM, copy_of_account_line_id, p_copy_of_account_line_id),
1767 	       period1_amount = l_period_amounts(1), period2_amount = l_period_amounts(2),
1768 	       period3_amount = l_period_amounts(3), period4_amount = l_period_amounts(4),
1769 	       period5_amount = l_period_amounts(5), period6_amount = l_period_amounts(6),
1770 	       period7_amount = l_period_amounts(7), period8_amount = l_period_amounts(8),
1771 	       period9_amount = l_period_amounts(9), period10_amount = l_period_amounts(10),
1772 	       period11_amount = l_period_amounts(11), period12_amount = l_period_amounts(12),
1773 	       period13_amount = l_period_amounts(13), period14_amount = l_period_amounts(14),
1774 	       period15_amount = l_period_amounts(15), period16_amount = l_period_amounts(16),
1775 	       period17_amount = l_period_amounts(17), period18_amount = l_period_amounts(18),
1776 	       period19_amount = l_period_amounts(19), period20_amount = l_period_amounts(20),
1777 	       period21_amount = l_period_amounts(21), period22_amount = l_period_amounts(22),
1778 	       period23_amount = l_period_amounts(23), period24_amount = l_period_amounts(24),
1779 	       period25_amount = l_period_amounts(25), period26_amount = l_period_amounts(26),
1780 	       period27_amount = l_period_amounts(27), period28_amount = l_period_amounts(28),
1781 	       period29_amount = l_period_amounts(29), period30_amount = l_period_amounts(30),
1782 	       period31_amount = l_period_amounts(31), period32_amount = l_period_amounts(32),
1783 	       period33_amount = l_period_amounts(33), period34_amount = l_period_amounts(34),
1784 	       period35_amount = l_period_amounts(35), period36_amount = l_period_amounts(36),
1785 	       period37_amount = l_period_amounts(37), period38_amount = l_period_amounts(38),
1786 	       period39_amount = l_period_amounts(39), period40_amount = l_period_amounts(40),
1787 	       period41_amount = l_period_amounts(41), period42_amount = l_period_amounts(42),
1788 	       period43_amount = l_period_amounts(43), period44_amount = l_period_amounts(44),
1789 	       period45_amount = l_period_amounts(45), period46_amount = l_period_amounts(46),
1790 	       period47_amount = l_period_amounts(47), period48_amount = l_period_amounts(48),
1791 	       period49_amount = l_period_amounts(49), period50_amount = l_period_amounts(50),
1792 	       period51_amount = l_period_amounts(51), period52_amount = l_period_amounts(52),
1793 	       period53_amount = l_period_amounts(53), period54_amount = l_period_amounts(54),
1794 	       period55_amount = l_period_amounts(55), period56_amount = l_period_amounts(56),
1795 	       period57_amount = l_period_amounts(57), period58_amount = l_period_amounts(58),
1796 	       period59_amount = l_period_amounts(59), period60_amount = l_period_amounts(60),
1797 	       ytd_amount = l_rounded_ytd_amount,
1798 	       last_update_date = sysdate,
1799 	       last_updated_by = l_userid,
1800 	       last_update_login = l_loginid,
1801 	       request_id = l_requestid
1802 	 where account_line_id = l_account_line_id;
1803 
1804       end;
1805       end if;
1806 
1807         if l_budget_group_id <> p_budget_group_id then
1808 	  l_budget_group_changed := TRUE;
1809         end if;
1810 
1811 
1812     end;
1813     else
1814     begin
1815 
1816       if p_position_line_id <> FND_API.G_MISS_NUM then
1817       begin
1818 
1819 
1820 	for c_wal_rec in c_wal2 loop
1821 	  l_account_line_id := c_wal_rec.account_line_id;
1822 	  l_budget_group_id := c_wal_rec.budget_group_id;
1823 	  l_current_requestid := c_wal_rec.request_id;
1824 	  l_current_fte := c_wal_rec.annual_fte; l_current_ytdamt := c_wal_rec.ytd_amount;
1825 	  l_current_prdamt(1) := c_wal_rec.period1_amount; l_current_prdamt(2) := c_wal_rec.period2_amount;
1826 	  l_current_prdamt(3) := c_wal_rec.period3_amount; l_current_prdamt(4) := c_wal_rec.period4_amount;
1827 	  l_current_prdamt(5) := c_wal_rec.period5_amount; l_current_prdamt(6) := c_wal_rec.period6_amount;
1828 	  l_current_prdamt(7) := c_wal_rec.period7_amount; l_current_prdamt(8) := c_wal_rec.period8_amount;
1829 	  l_current_prdamt(9) := c_wal_rec.period9_amount; l_current_prdamt(10) := c_wal_rec.period10_amount;
1830 	  l_current_prdamt(11) := c_wal_rec.period11_amount; l_current_prdamt(12) := c_wal_rec.period12_amount;
1831 	  l_current_prdamt(13) := c_wal_rec.period13_amount; l_current_prdamt(14) := c_wal_rec.period14_amount;
1832 	  l_current_prdamt(15) := c_wal_rec.period15_amount; l_current_prdamt(16) := c_wal_rec.period16_amount;
1833 	  l_current_prdamt(17) := c_wal_rec.period17_amount; l_current_prdamt(18) := c_wal_rec.period18_amount;
1834 	  l_current_prdamt(19) := c_wal_rec.period19_amount; l_current_prdamt(20) := c_wal_rec.period20_amount;
1835 	  l_current_prdamt(21) := c_wal_rec.period21_amount; l_current_prdamt(22) := c_wal_rec.period22_amount;
1836 	  l_current_prdamt(23) := c_wal_rec.period23_amount; l_current_prdamt(24) := c_wal_rec.period24_amount;
1837 	  l_current_prdamt(25) := c_wal_rec.period25_amount; l_current_prdamt(26) := c_wal_rec.period26_amount;
1838 	  l_current_prdamt(27) := c_wal_rec.period27_amount; l_current_prdamt(28) := c_wal_rec.period28_amount;
1839 	  l_current_prdamt(29) := c_wal_rec.period29_amount; l_current_prdamt(30) := c_wal_rec.period30_amount;
1840 	  l_current_prdamt(31) := c_wal_rec.period31_amount; l_current_prdamt(32) := c_wal_rec.period32_amount;
1841 	  l_current_prdamt(33) := c_wal_rec.period33_amount; l_current_prdamt(34) := c_wal_rec.period34_amount;
1842 	  l_current_prdamt(35) := c_wal_rec.period35_amount; l_current_prdamt(36) := c_wal_rec.period36_amount;
1843 	  l_current_prdamt(37) := c_wal_rec.period37_amount; l_current_prdamt(38) := c_wal_rec.period38_amount;
1844 	  l_current_prdamt(39) := c_wal_rec.period39_amount; l_current_prdamt(40) := c_wal_rec.period40_amount;
1845 	  l_current_prdamt(41) := c_wal_rec.period41_amount; l_current_prdamt(42) := c_wal_rec.period42_amount;
1846 	  l_current_prdamt(43) := c_wal_rec.period43_amount; l_current_prdamt(44) := c_wal_rec.period44_amount;
1847 	  l_current_prdamt(45) := c_wal_rec.period45_amount; l_current_prdamt(46) := c_wal_rec.period46_amount;
1848 	  l_current_prdamt(47) := c_wal_rec.period47_amount; l_current_prdamt(48) := c_wal_rec.period48_amount;
1849 	  l_current_prdamt(49) := c_wal_rec.period49_amount; l_current_prdamt(50) := c_wal_rec.period50_amount;
1850 	  l_current_prdamt(51) := c_wal_rec.period51_amount; l_current_prdamt(52) := c_wal_rec.period52_amount;
1851 	  l_current_prdamt(53) := c_wal_rec.period53_amount; l_current_prdamt(54) := c_wal_rec.period54_amount;
1852 	  l_current_prdamt(55) := c_wal_rec.period55_amount; l_current_prdamt(56) := c_wal_rec.period56_amount;
1853 	  l_current_prdamt(57) := c_wal_rec.period57_amount; l_current_prdamt(58) := c_wal_rec.period58_amount;
1854 	  l_current_prdamt(59) := c_wal_rec.period59_amount; l_current_prdamt(60) := c_wal_rec.period60_amount;
1855 	end loop;
1856 
1857 	if ((nvl(l_current_requestid, FND_API.G_MISS_NUM) <> nvl(l_requestid, FND_API.G_MISS_NUM)) or
1858 	    (p_balance_type = 'E')) then
1859 	  l_flexmap_increment := FALSE;
1860 	end if;
1861 
1862 	if l_flexmap_increment then
1863 	begin
1864 
1865 	  update PSB_WS_ACCOUNT_LINES
1866 	     set budget_group_id = p_budget_group_id,
1867 		 current_stage_seq = l_current_stage_seq,
1868 		 end_stage_seq = decode(p_end_stage_seq, FND_API.G_MISS_NUM, end_stage_seq, p_end_stage_seq),
1869 		 annual_fte = decode(nvl(p_annual_fte, FND_API.G_MISS_NUM), FND_API.G_MISS_NUM, annual_fte, p_annual_fte + nvl(l_current_fte, 0)),
1870 		 copy_of_account_line_id = decode(p_copy_of_account_line_id, FND_API.G_MISS_NUM, copy_of_account_line_id, p_copy_of_account_line_id),
1871 		 period1_amount = l_period_amounts(1) + l_current_prdamt(1),
1872 		 period2_amount = l_period_amounts(2) + l_current_prdamt(2),
1873 		 period3_amount = l_period_amounts(3) + l_current_prdamt(3),
1874 		 period4_amount = l_period_amounts(4) + l_current_prdamt(4),
1875 		 period5_amount = l_period_amounts(5) + l_current_prdamt(5),
1876 		 period6_amount = l_period_amounts(6) + l_current_prdamt(6),
1877 		 period7_amount = l_period_amounts(7) + l_current_prdamt(7),
1878 		 period8_amount = l_period_amounts(8) + l_current_prdamt(8),
1879 		 period9_amount = l_period_amounts(9) + l_current_prdamt(9),
1880 		 period10_amount = l_period_amounts(10) + l_current_prdamt(10),
1881 		 period11_amount = l_period_amounts(11) + l_current_prdamt(11),
1882 		 period12_amount = l_period_amounts(12) + l_current_prdamt(12),
1883 		 period13_amount = l_period_amounts(13) + l_current_prdamt(13),
1884 		 period14_amount = l_period_amounts(14) + l_current_prdamt(14),
1885 		 period15_amount = l_period_amounts(15) + l_current_prdamt(15),
1886 		 period16_amount = l_period_amounts(16) + l_current_prdamt(16),
1887 		 period17_amount = l_period_amounts(17) + l_current_prdamt(17),
1888 		 period18_amount = l_period_amounts(18) + l_current_prdamt(18),
1889 		 period19_amount = l_period_amounts(19) + l_current_prdamt(19),
1890 		 period20_amount = l_period_amounts(20) + l_current_prdamt(20),
1891 		 period21_amount = l_period_amounts(21) + l_current_prdamt(21),
1892 		 period22_amount = l_period_amounts(22) + l_current_prdamt(22),
1893 		 period23_amount = l_period_amounts(23) + l_current_prdamt(23),
1894 		 period24_amount = l_period_amounts(24) + l_current_prdamt(24),
1895 		 period25_amount = l_period_amounts(25) + l_current_prdamt(25),
1896 		 period26_amount = l_period_amounts(26) + l_current_prdamt(26),
1897 		 period27_amount = l_period_amounts(27) + l_current_prdamt(27),
1898 		 period28_amount = l_period_amounts(28) + l_current_prdamt(28),
1899 		 period29_amount = l_period_amounts(29) + l_current_prdamt(29),
1900 		 period30_amount = l_period_amounts(30) + l_current_prdamt(30),
1901 		 period31_amount = l_period_amounts(31) + l_current_prdamt(31),
1902 		 period32_amount = l_period_amounts(32) + l_current_prdamt(32),
1903 		 period33_amount = l_period_amounts(33) + l_current_prdamt(33),
1904 		 period34_amount = l_period_amounts(34) + l_current_prdamt(34),
1905 		 period35_amount = l_period_amounts(35) + l_current_prdamt(35),
1906 		 period36_amount = l_period_amounts(36) + l_current_prdamt(36),
1907 		 period37_amount = l_period_amounts(37) + l_current_prdamt(37),
1908 		 period38_amount = l_period_amounts(38) + l_current_prdamt(38),
1909 		 period39_amount = l_period_amounts(39) + l_current_prdamt(39),
1910 		 period40_amount = l_period_amounts(40) + l_current_prdamt(40),
1911 		 period41_amount = l_period_amounts(41) + l_current_prdamt(41),
1912 		 period42_amount = l_period_amounts(42) + l_current_prdamt(42),
1913 		 period43_amount = l_period_amounts(43) + l_current_prdamt(43),
1914 		 period44_amount = l_period_amounts(44) + l_current_prdamt(44),
1915 		 period45_amount = l_period_amounts(45) + l_current_prdamt(45),
1916 		 period46_amount = l_period_amounts(46) + l_current_prdamt(46),
1917 		 period47_amount = l_period_amounts(47) + l_current_prdamt(47),
1918 		 period48_amount = l_period_amounts(48) + l_current_prdamt(48),
1919 		 period49_amount = l_period_amounts(49) + l_current_prdamt(49),
1920 		 period50_amount = l_period_amounts(50) + l_current_prdamt(50),
1921 		 period51_amount = l_period_amounts(51) + l_current_prdamt(51),
1922 		 period52_amount = l_period_amounts(52) + l_current_prdamt(52),
1923 		 period53_amount = l_period_amounts(53) + l_current_prdamt(53),
1924 		 period54_amount = l_period_amounts(54) + l_current_prdamt(54),
1925 		 period55_amount = l_period_amounts(55) + l_current_prdamt(55),
1926 		 period56_amount = l_period_amounts(56) + l_current_prdamt(56),
1927 		 period57_amount = l_period_amounts(57) + l_current_prdamt(57),
1928 		 period58_amount = l_period_amounts(58) + l_current_prdamt(58),
1929 		 period59_amount = l_period_amounts(59) + l_current_prdamt(59),
1930 		 period60_amount = l_period_amounts(60) + l_current_prdamt(60),
1931 		 ytd_amount = l_rounded_ytd_amount + l_current_ytdamt,
1932 		 last_update_date = sysdate,
1933 		 last_updated_by = l_userid,
1934 		 last_update_login = l_loginid,
1935 		 request_id = l_requestid
1936 	   where account_line_id = l_account_line_id;
1937 
1938 	end;
1939 	else
1940 	begin
1941 
1942 	  update PSB_WS_ACCOUNT_LINES
1943 	     set budget_group_id = p_budget_group_id,
1944 		 current_stage_seq = l_current_stage_seq,
1945 		 end_stage_seq = decode(p_end_stage_seq, FND_API.G_MISS_NUM, end_stage_seq, p_end_stage_seq),
1946 		 annual_fte = decode(nvl(p_annual_fte, FND_API.G_MISS_NUM), FND_API.G_MISS_NUM, annual_fte, p_annual_fte),
1947 		 copy_of_account_line_id = decode(p_copy_of_account_line_id, FND_API.G_MISS_NUM, copy_of_account_line_id, p_copy_of_account_line_id),
1948 		 period1_amount = l_period_amounts(1), period2_amount = l_period_amounts(2),
1949 		 period3_amount = l_period_amounts(3), period4_amount = l_period_amounts(4),
1950 		 period5_amount = l_period_amounts(5), period6_amount = l_period_amounts(6),
1951 		 period7_amount = l_period_amounts(7), period8_amount = l_period_amounts(8),
1952 		 period9_amount = l_period_amounts(9), period10_amount = l_period_amounts(10),
1953 		 period11_amount = l_period_amounts(11), period12_amount = l_period_amounts(12),
1954 		 period13_amount = l_period_amounts(13), period14_amount = l_period_amounts(14),
1955 		 period15_amount = l_period_amounts(15), period16_amount = l_period_amounts(16),
1956 		 period17_amount = l_period_amounts(17), period18_amount = l_period_amounts(18),
1957 		 period19_amount = l_period_amounts(19), period20_amount = l_period_amounts(20),
1958 		 period21_amount = l_period_amounts(21), period22_amount = l_period_amounts(22),
1959 		 period23_amount = l_period_amounts(23), period24_amount = l_period_amounts(24),
1960 		 period25_amount = l_period_amounts(25), period26_amount = l_period_amounts(26),
1961 		 period27_amount = l_period_amounts(27), period28_amount = l_period_amounts(28),
1962 		 period29_amount = l_period_amounts(29), period30_amount = l_period_amounts(30),
1963 		 period31_amount = l_period_amounts(31), period32_amount = l_period_amounts(32),
1964 		 period33_amount = l_period_amounts(33), period34_amount = l_period_amounts(34),
1965 		 period35_amount = l_period_amounts(35), period36_amount = l_period_amounts(36),
1966 		 period37_amount = l_period_amounts(37), period38_amount = l_period_amounts(38),
1967 		 period39_amount = l_period_amounts(39), period40_amount = l_period_amounts(40),
1968 		 period41_amount = l_period_amounts(41), period42_amount = l_period_amounts(42),
1969 		 period43_amount = l_period_amounts(43), period44_amount = l_period_amounts(44),
1970 		 period45_amount = l_period_amounts(45), period46_amount = l_period_amounts(46),
1971 		 period47_amount = l_period_amounts(47), period48_amount = l_period_amounts(48),
1972 		 period49_amount = l_period_amounts(49), period50_amount = l_period_amounts(50),
1973 		 period51_amount = l_period_amounts(51), period52_amount = l_period_amounts(52),
1974 		 period53_amount = l_period_amounts(53), period54_amount = l_period_amounts(54),
1975 		 period55_amount = l_period_amounts(55), period56_amount = l_period_amounts(56),
1976 		 period57_amount = l_period_amounts(57), period58_amount = l_period_amounts(58),
1977 		 period59_amount = l_period_amounts(59), period60_amount = l_period_amounts(60),
1978 		 ytd_amount = l_rounded_ytd_amount,
1979 		 last_update_date = sysdate,
1980 		 last_updated_by = l_userid,
1981 		 last_update_login = l_loginid,
1982 		 request_id = l_requestid
1983 	   where account_line_id = l_account_line_id;
1984 
1985 	end;
1986 	end if;
1987 
1988 	if l_budget_group_id <> p_budget_group_id then
1989 	  l_budget_group_changed := TRUE;
1990 	end if;
1991 
1992 
1993 
1994 
1995       end;
1996       else
1997       begin
1998 
1999 	-- Non-Position Account Line must be unique across Global Worksheets and
2000 	-- Local Copies of Worksheets; Position Line IDs are anyway unique across
2001 	-- Global Worksheets and Local Copies of Worksheets
2002 
2003 
2004 	for c_wal_rec in c_wal3 loop
2005 	  l_account_line_id := c_wal_rec.account_line_id;
2006 	  l_budget_group_id := c_wal_rec.budget_group_id;
2007 	  l_current_requestid := c_wal_rec.request_id;
2008 	  l_current_fte := c_wal_rec.annual_fte; l_current_ytdamt := c_wal_rec.ytd_amount;
2009 	  l_current_prdamt(1) := c_wal_rec.period1_amount; l_current_prdamt(2) := c_wal_rec.period2_amount;
2010 	  l_current_prdamt(3) := c_wal_rec.period3_amount; l_current_prdamt(4) := c_wal_rec.period4_amount;
2011 	  l_current_prdamt(5) := c_wal_rec.period5_amount; l_current_prdamt(6) := c_wal_rec.period6_amount;
2012 	  l_current_prdamt(7) := c_wal_rec.period7_amount; l_current_prdamt(8) := c_wal_rec.period8_amount;
2013 	  l_current_prdamt(9) := c_wal_rec.period9_amount; l_current_prdamt(10) := c_wal_rec.period10_amount;
2014 	  l_current_prdamt(11) := c_wal_rec.period11_amount; l_current_prdamt(12) := c_wal_rec.period12_amount;
2015 	  l_current_prdamt(13) := c_wal_rec.period13_amount; l_current_prdamt(14) := c_wal_rec.period14_amount;
2016 	  l_current_prdamt(15) := c_wal_rec.period15_amount; l_current_prdamt(16) := c_wal_rec.period16_amount;
2017 	  l_current_prdamt(17) := c_wal_rec.period17_amount; l_current_prdamt(18) := c_wal_rec.period18_amount;
2018 	  l_current_prdamt(19) := c_wal_rec.period19_amount; l_current_prdamt(20) := c_wal_rec.period20_amount;
2019 	  l_current_prdamt(21) := c_wal_rec.period21_amount; l_current_prdamt(22) := c_wal_rec.period22_amount;
2020 	  l_current_prdamt(23) := c_wal_rec.period23_amount; l_current_prdamt(24) := c_wal_rec.period24_amount;
2021 	  l_current_prdamt(25) := c_wal_rec.period25_amount; l_current_prdamt(26) := c_wal_rec.period26_amount;
2022 	  l_current_prdamt(27) := c_wal_rec.period27_amount; l_current_prdamt(28) := c_wal_rec.period28_amount;
2023 	  l_current_prdamt(29) := c_wal_rec.period29_amount; l_current_prdamt(30) := c_wal_rec.period30_amount;
2024 	  l_current_prdamt(31) := c_wal_rec.period31_amount; l_current_prdamt(32) := c_wal_rec.period32_amount;
2025 	  l_current_prdamt(33) := c_wal_rec.period33_amount; l_current_prdamt(34) := c_wal_rec.period34_amount;
2026 	  l_current_prdamt(35) := c_wal_rec.period35_amount; l_current_prdamt(36) := c_wal_rec.period36_amount;
2027 	  l_current_prdamt(37) := c_wal_rec.period37_amount; l_current_prdamt(38) := c_wal_rec.period38_amount;
2028 	  l_current_prdamt(39) := c_wal_rec.period39_amount; l_current_prdamt(40) := c_wal_rec.period40_amount;
2029 	  l_current_prdamt(41) := c_wal_rec.period41_amount; l_current_prdamt(42) := c_wal_rec.period42_amount;
2030 	  l_current_prdamt(43) := c_wal_rec.period43_amount; l_current_prdamt(44) := c_wal_rec.period44_amount;
2031 	  l_current_prdamt(45) := c_wal_rec.period45_amount; l_current_prdamt(46) := c_wal_rec.period46_amount;
2032 	  l_current_prdamt(47) := c_wal_rec.period47_amount; l_current_prdamt(48) := c_wal_rec.period48_amount;
2033 	  l_current_prdamt(49) := c_wal_rec.period49_amount; l_current_prdamt(50) := c_wal_rec.period50_amount;
2034 	  l_current_prdamt(51) := c_wal_rec.period51_amount; l_current_prdamt(52) := c_wal_rec.period52_amount;
2035 	  l_current_prdamt(53) := c_wal_rec.period53_amount; l_current_prdamt(54) := c_wal_rec.period54_amount;
2036 	  l_current_prdamt(55) := c_wal_rec.period55_amount; l_current_prdamt(56) := c_wal_rec.period56_amount;
2037 	  l_current_prdamt(57) := c_wal_rec.period57_amount; l_current_prdamt(58) := c_wal_rec.period58_amount;
2038 	  l_current_prdamt(59) := c_wal_rec.period59_amount; l_current_prdamt(60) := c_wal_rec.period60_amount;
2039 	end loop;
2040 
2041 	if ((nvl(l_current_requestid, FND_API.G_MISS_NUM) <> nvl(l_requestid, FND_API.G_MISS_NUM)) or
2042 	    (p_balance_type = 'E')) then
2043 	  l_flexmap_increment := FALSE;
2044 	end if;
2045 
2046 	if l_flexmap_increment then
2047 	begin
2048 
2049 	  update PSB_WS_ACCOUNT_LINES
2050 	     set budget_group_id = p_budget_group_id,
2051 		 current_stage_seq = l_current_stage_seq,
2052 		 end_stage_seq = decode(p_end_stage_seq, FND_API.G_MISS_NUM, end_stage_seq, p_end_stage_seq),
2053 		 annual_fte = decode(nvl(p_annual_fte, FND_API.G_MISS_NUM), FND_API.G_MISS_NUM, annual_fte, p_annual_fte + nvl(l_current_fte, 0)),
2054 		 copy_of_account_line_id = decode(p_copy_of_account_line_id, FND_API.G_MISS_NUM, copy_of_account_line_id, p_copy_of_account_line_id),
2055 		 period1_amount = l_period_amounts(1) + l_current_prdamt(1),
2056 		 period2_amount = l_period_amounts(2) + l_current_prdamt(2),
2057 		 period3_amount = l_period_amounts(3) + l_current_prdamt(3),
2058 		 period4_amount = l_period_amounts(4) + l_current_prdamt(4),
2059 		 period5_amount = l_period_amounts(5) + l_current_prdamt(5),
2060 		 period6_amount = l_period_amounts(6) + l_current_prdamt(6),
2061 		 period7_amount = l_period_amounts(7) + l_current_prdamt(7),
2062 		 period8_amount = l_period_amounts(8) + l_current_prdamt(8),
2063 		 period9_amount = l_period_amounts(9) + l_current_prdamt(9),
2064 		 period10_amount = l_period_amounts(10) + l_current_prdamt(10),
2065 		 period11_amount = l_period_amounts(11) + l_current_prdamt(11),
2066 		 period12_amount = l_period_amounts(12) + l_current_prdamt(12),
2067 		 period13_amount = l_period_amounts(13) + l_current_prdamt(13),
2068 		 period14_amount = l_period_amounts(14) + l_current_prdamt(14),
2069 		 period15_amount = l_period_amounts(15) + l_current_prdamt(15),
2070 		 period16_amount = l_period_amounts(16) + l_current_prdamt(16),
2071 		 period17_amount = l_period_amounts(17) + l_current_prdamt(17),
2072 		 period18_amount = l_period_amounts(18) + l_current_prdamt(18),
2073 		 period19_amount = l_period_amounts(19) + l_current_prdamt(19),
2074 		 period20_amount = l_period_amounts(20) + l_current_prdamt(20),
2075 		 period21_amount = l_period_amounts(21) + l_current_prdamt(21),
2076 		 period22_amount = l_period_amounts(22) + l_current_prdamt(22),
2077 		 period23_amount = l_period_amounts(23) + l_current_prdamt(23),
2078 		 period24_amount = l_period_amounts(24) + l_current_prdamt(24),
2079 		 period25_amount = l_period_amounts(25) + l_current_prdamt(25),
2080 		 period26_amount = l_period_amounts(26) + l_current_prdamt(26),
2081 		 period27_amount = l_period_amounts(27) + l_current_prdamt(27),
2082 		 period28_amount = l_period_amounts(28) + l_current_prdamt(28),
2083 		 period29_amount = l_period_amounts(29) + l_current_prdamt(29),
2084 		 period30_amount = l_period_amounts(30) + l_current_prdamt(30),
2085 		 period31_amount = l_period_amounts(31) + l_current_prdamt(31),
2086 		 period32_amount = l_period_amounts(32) + l_current_prdamt(32),
2087 		 period33_amount = l_period_amounts(33) + l_current_prdamt(33),
2088 		 period34_amount = l_period_amounts(34) + l_current_prdamt(34),
2089 		 period35_amount = l_period_amounts(35) + l_current_prdamt(35),
2090 		 period36_amount = l_period_amounts(36) + l_current_prdamt(36),
2091 		 period37_amount = l_period_amounts(37) + l_current_prdamt(37),
2092 		 period38_amount = l_period_amounts(38) + l_current_prdamt(38),
2093 		 period39_amount = l_period_amounts(39) + l_current_prdamt(39),
2094 		 period40_amount = l_period_amounts(40) + l_current_prdamt(40),
2095 		 period41_amount = l_period_amounts(41) + l_current_prdamt(41),
2096 		 period42_amount = l_period_amounts(42) + l_current_prdamt(42),
2097 		 period43_amount = l_period_amounts(43) + l_current_prdamt(43),
2098 		 period44_amount = l_period_amounts(44) + l_current_prdamt(44),
2099 		 period45_amount = l_period_amounts(45) + l_current_prdamt(45),
2100 		 period46_amount = l_period_amounts(46) + l_current_prdamt(46),
2101 		 period47_amount = l_period_amounts(47) + l_current_prdamt(47),
2102 		 period48_amount = l_period_amounts(48) + l_current_prdamt(48),
2103 		 period49_amount = l_period_amounts(49) + l_current_prdamt(49),
2104 		 period50_amount = l_period_amounts(50) + l_current_prdamt(50),
2105 		 period51_amount = l_period_amounts(51) + l_current_prdamt(51),
2106 		 period52_amount = l_period_amounts(52) + l_current_prdamt(52),
2107 		 period53_amount = l_period_amounts(53) + l_current_prdamt(53),
2108 		 period54_amount = l_period_amounts(54) + l_current_prdamt(54),
2109 		 period55_amount = l_period_amounts(55) + l_current_prdamt(55),
2110 		 period56_amount = l_period_amounts(56) + l_current_prdamt(56),
2111 		 period57_amount = l_period_amounts(57) + l_current_prdamt(57),
2112 		 period58_amount = l_period_amounts(58) + l_current_prdamt(58),
2113 		 period59_amount = l_period_amounts(59) + l_current_prdamt(59),
2114 		 period60_amount = l_period_amounts(60) + l_current_prdamt(60),
2115 		 ytd_amount = l_rounded_ytd_amount + l_current_ytdamt,
2116 		 last_update_date = sysdate,
2117 		 last_updated_by = l_userid,
2118 		 last_update_login = l_loginid,
2119 		 request_id = l_requestid
2120 	   where account_line_id = l_account_line_id;
2121 
2122 	end;
2123 	else
2124 	begin
2125 
2126 	  update PSB_WS_ACCOUNT_LINES
2127 	     set budget_group_id = p_budget_group_id,
2128 		 current_stage_seq = l_current_stage_seq,
2129 		 end_stage_seq = decode(p_end_stage_seq, FND_API.G_MISS_NUM, end_stage_seq, p_end_stage_seq),
2130 		 annual_fte = decode(nvl(p_annual_fte, FND_API.G_MISS_NUM), FND_API.G_MISS_NUM, annual_fte, p_annual_fte),
2131 		 copy_of_account_line_id = decode(p_copy_of_account_line_id, FND_API.G_MISS_NUM, copy_of_account_line_id, p_copy_of_account_line_id),
2132 		 period1_amount = l_period_amounts(1), period2_amount = l_period_amounts(2),
2133 		 period3_amount = l_period_amounts(3), period4_amount = l_period_amounts(4),
2134 		 period5_amount = l_period_amounts(5), period6_amount = l_period_amounts(6),
2135 		 period7_amount = l_period_amounts(7), period8_amount = l_period_amounts(8),
2136 		 period9_amount = l_period_amounts(9), period10_amount = l_period_amounts(10),
2137 		 period11_amount = l_period_amounts(11), period12_amount = l_period_amounts(12),
2138 		 period13_amount = l_period_amounts(13), period14_amount = l_period_amounts(14),
2139 		 period15_amount = l_period_amounts(15), period16_amount = l_period_amounts(16),
2140 		 period17_amount = l_period_amounts(17), period18_amount = l_period_amounts(18),
2141 		 period19_amount = l_period_amounts(19), period20_amount = l_period_amounts(20),
2142 		 period21_amount = l_period_amounts(21), period22_amount = l_period_amounts(22),
2143 		 period23_amount = l_period_amounts(23), period24_amount = l_period_amounts(24),
2144 		 period25_amount = l_period_amounts(25), period26_amount = l_period_amounts(26),
2145 		 period27_amount = l_period_amounts(27), period28_amount = l_period_amounts(28),
2146 		 period29_amount = l_period_amounts(29), period30_amount = l_period_amounts(30),
2147 		 period31_amount = l_period_amounts(31), period32_amount = l_period_amounts(32),
2148 		 period33_amount = l_period_amounts(33), period34_amount = l_period_amounts(34),
2149 		 period35_amount = l_period_amounts(35), period36_amount = l_period_amounts(36),
2150 		 period37_amount = l_period_amounts(37), period38_amount = l_period_amounts(38),
2151 		 period39_amount = l_period_amounts(39), period40_amount = l_period_amounts(40),
2152 		 period41_amount = l_period_amounts(41), period42_amount = l_period_amounts(42),
2153 		 period43_amount = l_period_amounts(43), period44_amount = l_period_amounts(44),
2154 		 period45_amount = l_period_amounts(45), period46_amount = l_period_amounts(46),
2155 		 period47_amount = l_period_amounts(47), period48_amount = l_period_amounts(48),
2156 		 period49_amount = l_period_amounts(49), period50_amount = l_period_amounts(50),
2157 		 period51_amount = l_period_amounts(51), period52_amount = l_period_amounts(52),
2158 		 period53_amount = l_period_amounts(53), period54_amount = l_period_amounts(54),
2159 		 period55_amount = l_period_amounts(55), period56_amount = l_period_amounts(56),
2160 		 period57_amount = l_period_amounts(57), period58_amount = l_period_amounts(58),
2161 		 period59_amount = l_period_amounts(59), period60_amount = l_period_amounts(60),
2162 		 ytd_amount = l_rounded_ytd_amount,
2163 		 last_update_date = sysdate,
2164 		 last_updated_by = l_userid,
2165 		 last_update_login = l_loginid,
2166 		 request_id = l_requestid
2167 	   where account_line_id = l_account_line_id;
2168 
2169 	end;
2170 	end if;
2171 
2172 	if l_budget_group_id <> p_budget_group_id then
2173 	  l_budget_group_changed := TRUE;
2174 	end if;
2175 
2176 
2177       end;
2178       end if;
2179 
2180     end;
2181     end if;
2182 
2183   end;
2184   end if;
2185 
2186 
2187    /* Bug 3305778 Start */
2188 
2189   IF g_create_zero_bal is null THEN
2190    BEGIN
2191 
2192     FND_PROFILE.GET
2193        (name => 'PSB_CREATE_ZERO_BALANCE_ACCT',
2194 	val => g_create_zero_bal);
2195 
2196     IF g_create_zero_bal is null THEN
2197       -- Bug 3543845: Change default behavior to not creating zero balance
2198       g_create_zero_bal := 'N';
2199     END IF;
2200 
2201    END;
2202   END IF;
2203 
2204   -- Try to Insert if Update was unsuccessful
2205   --if SQL%NOTFOUND then
2206 
2207   IF (SQL%NOTFOUND) AND ((p_ytd_amount <> 0) OR ((p_ytd_amount = 0) AND (g_create_zero_bal = 'Y'))) THEN
2208 
2209    /* Bug 3305778 End */
2210 
2211   begin
2212 
2213     GL_CODE_COMBINATIONS_PKG.Select_Columns
2214       (X_code_combination_id => l_ccid,
2215        X_account_type => l_account_type,
2216        X_template_id => l_template_id);
2217 
2218     for l_index in 1..g_max_num_amounts loop
2219 
2220       if FND_API.to_Boolean(p_distribute_flag) then
2221       begin
2222 
2223 	if l_rounding_factor is null then
2224 	  l_period_amount := l_period_amount_tbl(l_index);
2225 	else
2226 	  l_period_amount := ROUND(l_period_amount_tbl(l_index)/l_rounding_factor) * l_rounding_factor;
2227 	end if;
2228 
2229       end;
2230       else
2231       begin
2232 
2233 	if l_rounding_factor is null then
2234 	  l_period_amount := p_period_amount(l_index);
2235 	else
2236 	  l_period_amount := ROUND(p_period_amount(l_index)/l_rounding_factor) * l_rounding_factor;
2237 	end if;
2238 
2239       end;
2240       end if;
2241 
2242       if l_period_amount is null then
2243       begin
2244 	/* Bug 3247563 Start */
2245 	--if ((l_index = l_last_period_index) and (l_ytd_amount <> 0)) then
2246 	if (l_index = l_last_period_index) then
2247 	/* Bug 3247563 End */
2248 
2249 	  l_period_amounts(l_index) := nvl(l_rounding_difference, 0);
2250 	else
2251 	  l_period_amounts(l_index) := null;
2252 	end if;
2253 
2254       end;
2255       else
2256       begin
2257 
2258 	-- Adjust rounding difference to the last Budget Period
2259 
2260 	/* Bug 3247563 Start */
2261 	--if ((l_index = l_last_period_index) and (l_ytd_amount <> 0)) then
2262 	if (l_index = l_last_period_index) then
2263 	/* Bug 3247563 End */
2264 
2265 	  l_period_amounts(l_index) := l_period_amount + nvl(l_rounding_difference, 0);
2266 	else
2267 	  l_period_amounts(l_index) := l_period_amount;
2268 	end if;
2269 
2270       end;
2271       end if;
2272 
2273     end loop;
2274 
2275     insert into PSB_WS_ACCOUNT_LINES
2276 	  (account_line_id, code_combination_id, position_line_id, service_package_id, budget_group_id,
2277 	   element_set_id, salary_account_line, stage_set_id, start_stage_seq, current_stage_seq,
2278 	   end_stage_seq, copy_of_account_line_id, last_update_date, last_updated_by,
2279 	   last_update_login, created_by, creation_date, template_id, budget_year_id, annual_fte,
2280 	   currency_code, account_type, balance_type,
2281 	   period1_amount, period2_amount, period3_amount, period4_amount, period5_amount, period6_amount,
2282 	   period7_amount, period8_amount, period9_amount, period10_amount, period11_amount, period12_amount,
2283 	   period13_amount, period14_amount, period15_amount, period16_amount, period17_amount, period18_amount,
2284 	   period19_amount, period20_amount, period21_amount, period22_amount, period23_amount, period24_amount,
2285 	   period25_amount, period26_amount, period27_amount, period28_amount, period29_amount, period30_amount,
2286 	   period31_amount, period32_amount, period33_amount, period34_amount, period35_amount, period36_amount,
2287 	   period37_amount, period38_amount, period39_amount, period40_amount, period41_amount, period42_amount,
2288 	   period43_amount, period44_amount, period45_amount, period46_amount, period47_amount, period48_amount,
2289 	   period49_amount, period50_amount, period51_amount, period52_amount, period53_amount, period54_amount,
2290 	   period55_amount, period56_amount, period57_amount, period58_amount, period59_amount, period60_amount,
2291 	   ytd_amount, request_id, functional_transaction)
2292     values (psb_ws_account_lines_s.nextval,
2293 	    l_ccid,
2294 	    decode(p_position_line_id, FND_API.G_MISS_NUM, null, p_position_line_id),
2295 	    l_service_package_id,
2296 	    p_budget_group_id,
2297 	    decode(p_element_set_id, FND_API.G_MISS_NUM, null, p_element_set_id),
2298 	    decode(p_salary_account_line, FND_API.G_FALSE, null, 'Y'),
2299 	    l_stage_set_id,
2300 	    decode(p_start_stage_seq, FND_API.G_MISS_NUM, l_start_stage_seq, p_start_stage_seq),
2301 	    l_current_stage_seq,
2302 	    decode(p_end_stage_seq, FND_API.G_MISS_NUM, null, p_end_stage_seq),
2303 	    decode(p_copy_of_account_line_id, FND_API.G_MISS_NUM, null, p_copy_of_account_line_id),
2304 	    sysdate, l_userid, l_loginid, l_userid, sysdate,
2305 	    decode(p_template_id, FND_API.G_MISS_NUM, null, p_template_id),
2306 	    p_budget_year_id,
2307 	    decode(nvl(p_annual_fte, FND_API.G_MISS_NUM), FND_API.G_MISS_NUM, null, nvl(p_annual_fte, FND_API.G_MISS_NUM)),
2308 	    p_currency_code, l_account_type, p_balance_type,
2309 	    l_period_amounts(1), l_period_amounts(2), l_period_amounts(3), l_period_amounts(4), l_period_amounts(5),
2310 	    l_period_amounts(6), l_period_amounts(7), l_period_amounts(8), l_period_amounts(9), l_period_amounts(10),
2311 	    l_period_amounts(11), l_period_amounts(12), l_period_amounts(13), l_period_amounts(14), l_period_amounts(15),
2312 	    l_period_amounts(16), l_period_amounts(17), l_period_amounts(18), l_period_amounts(19), l_period_amounts(20),
2313 	    l_period_amounts(21), l_period_amounts(22), l_period_amounts(23), l_period_amounts(24), l_period_amounts(25),
2314 	    l_period_amounts(26), l_period_amounts(27), l_period_amounts(28), l_period_amounts(29), l_period_amounts(30),
2315 	    l_period_amounts(31), l_period_amounts(32), l_period_amounts(33), l_period_amounts(34), l_period_amounts(35),
2316 	    l_period_amounts(36), l_period_amounts(37), l_period_amounts(38), l_period_amounts(39), l_period_amounts(40),
2317 	    l_period_amounts(41), l_period_amounts(42), l_period_amounts(43), l_period_amounts(44), l_period_amounts(45),
2318 	    l_period_amounts(46), l_period_amounts(47), l_period_amounts(48), l_period_amounts(49), l_period_amounts(50),
2319 	    l_period_amounts(51), l_period_amounts(52), l_period_amounts(53), l_period_amounts(54), l_period_amounts(55),
2320 	    l_period_amounts(56), l_period_amounts(57), l_period_amounts(58), l_period_amounts(59), l_period_amounts(60),
2321 	    l_rounded_ytd_amount, l_requestid, p_functional_transaction)
2322     returning account_line_id into l_acclineid;
2323 
2324     -- Create an entry in PSB_WS_LINES for all worksheets to which the CCID or Position belongs
2325 
2326     if p_position_line_id <> FND_API.G_MISS_NUM then
2327     begin
2328 
2329       insert into PSB_WS_LINES
2330 	    (worksheet_id, account_line_id, freeze_flag,
2331 	     view_line_flag, last_update_date, last_updated_by,
2332 	     last_update_login, created_by, creation_date)
2333       select worksheet_id, l_acclineid, freeze_flag,
2334 	     view_line_flag, sysdate, l_userid,
2335 	     l_loginid, l_userid, sysdate
2336 	from PSB_WS_LINES_POSITIONS
2337        where position_line_id = p_position_line_id;
2338 
2339     end;
2340     else
2341     begin
2342 
2343       -- Bug 3543845: During ws creation, distributed ws does not exist.
2344       -- if l_local_copy_flag = 'Y' then
2345       if PSB_WORKSHEET.g_ws_creation_flag OR
2346          l_local_copy_flag = 'Y'
2347       then
2348       begin
2349 
2350 	insert into PSB_WS_LINES
2351 	      (worksheet_id, account_line_id, freeze_flag,
2352 	       view_line_flag, last_update_date, last_updated_by,
2353 	       last_update_login, created_by, creation_date)
2354 	 values (p_worksheet_id, l_acclineid, null,
2355 		 'Y', sysdate, l_userid,
2356 		 l_loginid, l_userid, sysdate);
2357       end;
2358       else
2359       begin
2360 	for c_Distribute_WS_Rec in c_Distribute_WS (l_global_worksheet_id, p_budget_group_id,
2361 						    g_startdate_pp,
2362 						    g_enddate_cy) loop
2363           insert into PSB_WS_LINES
2364 		(worksheet_id, account_line_id, freeze_flag,
2365 		 view_line_flag, last_update_date, last_updated_by,
2366 		 last_update_login, created_by, creation_date)
2367 	   values (c_Distribute_WS_Rec.worksheet_id, l_acclineid, null,
2368 		   'Y', sysdate, l_userid,
2369 		   l_loginid, l_userid, sysdate);
2370 
2371 	end loop;
2372 
2373       end;
2374       end if;
2375 
2376     end;
2377     end if;
2378 
2379     p_account_line_id := l_acclineid;
2380 
2381   end;
2382   else
2383   begin
2384 
2385     -- Update was successful; if budget group was changed reassign worksheets
2386 
2387     if l_budget_group_changed then
2388     begin
2389 
2390       delete from psb_ws_lines
2391        where account_line_id = l_account_line_id;
2392 
2393       -- Create an entry in PSB_WS_LINES for all worksheets to which the CCID or Position belongs
2394 
2395       if p_position_line_id <> FND_API.G_MISS_NUM then
2396       begin
2397 
2398 	insert into PSB_WS_LINES
2399 	      (worksheet_id, account_line_id, freeze_flag,
2400 	       view_line_flag, last_update_date, last_updated_by,
2401 	       last_update_login, created_by, creation_date)
2402 	select worksheet_id, l_account_line_id, freeze_flag,
2403 	       view_line_flag, sysdate, l_userid,
2404 	       l_loginid, l_userid, sysdate
2405 	  from PSB_WS_LINES_POSITIONS
2406 	 where position_line_id = p_position_line_id;
2407 
2408       end;
2409       else
2410       begin
2411 
2412         -- Bug 3543845: During ws creation, distributed ws does not exist.
2413         -- Also flip the condition to make it readable.
2414         -- if nvl(l_local_copy_flag, 'N') <> 'Y' then
2415         if PSB_WORKSHEET.g_ws_creation_flag OR
2416            l_local_copy_flag = 'Y'
2417         then
2418 	begin
2419 
2420 	  insert into PSB_WS_LINES
2421 		(worksheet_id, account_line_id, freeze_flag,
2422 		 view_line_flag, last_update_date, last_updated_by,
2423 		 last_update_login, created_by, creation_date)
2424 	   values (p_worksheet_id, l_account_line_id, null,
2425 		   'Y', sysdate, l_userid,
2426 		   l_loginid, l_userid, sysdate);
2427 
2428 	end;
2429 	else
2430         begin
2431 
2432 	  for c_Distribute_WS_Rec in c_Distribute_WS (l_global_worksheet_id, p_budget_group_id,
2433 						      g_startdate_pp,
2434 						      g_enddate_cy) loop
2435             insert into PSB_WS_LINES
2436 		  (worksheet_id, account_line_id, freeze_flag,
2437 		   view_line_flag, last_update_date, last_updated_by,
2438 		   last_update_login, created_by, creation_date)
2439 	     values (c_Distribute_WS_Rec.worksheet_id, l_account_line_id, null,
2440 		     'Y', sysdate, l_userid,
2441 		     l_loginid, l_userid, sysdate);
2442 
2443 	  end loop;
2444 
2445 	end;
2446 	end if;
2447 
2448       end;
2449       end if;
2450 
2451     end;
2452     end if;
2453 
2454     p_account_line_id := l_account_line_id;
2455 
2456   end;
2457   end if;
2458 
2459   -- Initialize API return status to success
2460 
2461   p_return_status := FND_API.G_RET_STS_SUCCESS;
2462 
2463 
2464 EXCEPTION
2465 
2466    when FND_API.G_EXC_ERROR then
2467      p_return_status := FND_API.G_RET_STS_ERROR;
2468 
2469    when FND_API.G_EXC_UNEXPECTED_ERROR then
2470      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2471 
2472    when OTHERS then
2473      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2474 
2475      if FND_MSG_PUB.Check_Msg_Level
2476        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
2477      then
2478        FND_MSG_PUB.Add_Exc_Msg
2479 	  (p_pkg_name => G_PKG_NAME,
2480 	   p_procedure_name => l_api_name);
2481      end if;
2482 
2483 END Create_Account_Dist;
2484 /*---------------------------------------------------------------------------*/
2485 
2486 
2487 /*===========================================================================+
2488  |                      PROCEDURE Create_Account_Dist                        |
2489  +===========================================================================*/
2490 --
2491 -- This API must be called when updating an existing Account Line.
2492 -- p_distribute_flag must be set to FND_API.G_TRUE to automatically distribute
2493 -- the YTD Amount into Period Amounts. If existing YTD Amount is 0 and
2494 -- p_ytd_amount is not 0, distribution is done using the Period Allocation
2495 -- rules; otherwise, the existing period amounts are prorated in the ratio of
2496 -- the YTD Amount p_check_stages must be set to FND_API.G_TRUE to automatically
2497 -- create new Stage for the Account Line
2498 --
2499 PROCEDURE Create_Account_Dist
2500 ( p_api_version              IN   NUMBER,
2501   p_validation_level         IN   NUMBER := FND_API.G_VALID_LEVEL_NONE,
2502   p_return_status            OUT  NOCOPY  VARCHAR2,
2503   p_worksheet_id             IN   NUMBER,
2504   p_distribute_flag          IN   VARCHAR2 := FND_API.G_FALSE,
2505   p_account_line_id          IN   NUMBER,
2506   p_check_stages             IN   VARCHAR2 := FND_API.G_TRUE,
2507   p_ytd_amount               IN   NUMBER := FND_API.G_MISS_NUM,
2508   p_annual_fte               IN   NUMBER := FND_API.G_MISS_NUM,
2509   p_period_amount            IN   g_prdamt_tbl_type,
2510   p_budget_group_id          IN   NUMBER := FND_API.G_MISS_NUM,
2511   p_service_package_id       IN   NUMBER := FND_API.G_MISS_NUM,
2512   p_current_stage_seq        IN   NUMBER := FND_API.G_MISS_NUM,
2513   p_copy_of_account_line_id  IN   NUMBER := FND_API.G_MISS_NUM,
2514   /* start bug 4128196 */
2515   p_update_cy_estimate       IN   VARCHAR2 := 'N'
2516   /* end bug 4128196   */
2517 )
2518 IS
2519   --
2520   l_api_name                 CONSTANT VARCHAR2(30)   := 'Create_Account_Dist';
2521   l_api_version              CONSTANT NUMBER         := 1.0;
2522   l_userid                   NUMBER;
2523   l_loginid                  NUMBER;
2524   --
2525   cur_wal                    PLS_INTEGER;
2526   sql_wal                    VARCHAR2(1000);
2527   num_wal                    PLS_INTEGER;
2528   cur_wsacc                  PLS_INTEGER;
2529   sql_wsacc                  VARCHAR2(6000);
2530   num_wsacc                  PLS_INTEGER;
2531 
2532   -- For bug#2440100
2533   l_period_empty             BOOLEAN := TRUE;
2534 
2535   l_stage_set_id             NUMBER;
2536   l_new_stage                BOOLEAN := FALSE;
2537   l_previous_stage           NUMBER;
2538   l_acclineid                NUMBER;
2539   l_period_amount            NUMBER;
2540   l_rounding_factor          NUMBER;
2541   l_allocrule_set_id         NUMBER;
2542   l_global_worksheet_id      NUMBER;
2543   l_budget_calendar_id       NUMBER;
2544   l_gl_cutoff_period         DATE;
2545   l_local_copy_flag          VARCHAR2(1);
2546   l_period_amount_tbl        g_prdamt_tbl_type;
2547   l_ccid                     NUMBER;
2548   l_budget_group_id          NUMBER;
2549   l_template_id              NUMBER;
2550   l_position_line_id         NUMBER;
2551   l_element_set_id           NUMBER;
2552   l_budget_year_id           NUMBER;
2553   l_currency_code            VARCHAR2(15);
2554   l_start_stage_seq          NUMBER;
2555   l_current_stage_seq        NUMBER;
2556   l_service_package_id       NUMBER;
2557   l_balance_type             VARCHAR2(1);
2558   l_prdamt_tbl               g_prdamt_tbl_type;
2559   --
2560   l_running_ytd_amount       NUMBER := 0;
2561   l_old_ytd_amount           NUMBER;
2562   l_new_ytd_amount           NUMBER;
2563   l_rounding_difference      NUMBER;
2564   l_allocate_ytd_amount      NUMBER;
2565 
2566   /* Bug No 2342169 Start */
2567   l_redist_palloc            VARCHAR2(1) := null;
2568   l_budget_year_type         VARCHAR2(30);
2569   l_cy_ytd_amount            NUMBER ;
2570   /* Bug No 2342169 End */
2571 
2572   l_spal_id                  NUMBER;
2573   l_spytd_amount             NUMBER;
2574   l_spal_exists              BOOLEAN := FALSE;
2575   --
2576   l_budget_periods           g_budgetperiod_tbl_type;
2577   l_distribute1_flag         BOOLEAN := FALSE;
2578   l_distribute2_flag         BOOLEAN := FALSE;
2579   --
2580   l_start_date               DATE;
2581   l_end_date                 DATE;
2582   l_ccid_start_date          DATE;
2583   l_ccid_end_date            DATE;
2584 
2585   l_index                    PLS_INTEGER;
2586   l_init_index               PLS_INTEGER;
2587   l_year_index               PLS_INTEGER;
2588   l_period_index             PLS_INTEGER;
2589   l_last_period_index        NUMBER;
2590   --
2591 
2592   -- Bug#3258892
2593   l_cy_start_index           NUMBER;
2594 
2595   l_budget_group_changed     BOOLEAN := FALSE;
2596   l_root_budget_group_id     NUMBER;
2597   l_new_budget_group_id      NUMBER;
2598   --
2599   l_set_of_books_id          NUMBER;
2600   l_flex_mapping_set_id      NUMBER;
2601   l_budget_year_type_id      NUMBER;
2602   l_return_status            VARCHAR2(1);
2603 
2604   -- bug no 4128196
2605   l_running_total			 NUMBER := 0;
2606 
2607   --
2608   cursor c_WS is
2609     select a.stage_set_id,
2610 	   a.rounding_factor,
2611 	   nvl(a.allocrule_set_id, a.global_allocrule_set_id) allocrule_set_id,
2612 	   nvl(a.global_worksheet_id, a.worksheet_id) global_worksheet_id,
2613 	   a.budget_calendar_id,
2614 	   a.flex_mapping_set_id,
2615 	   a.gl_cutoff_period,
2616 	   nvl(b.root_budget_group_id, b.budget_group_id) root_budget_group_id,
2617 	   local_copy_flag
2618       from PSB_WORKSHEETS_V a, PSB_BUDGET_GROUPS b
2619      where a.worksheet_id = p_worksheet_id
2620        and b.budget_group_id = a.budget_group_id;
2621   --
2622   cursor c_CCID is
2623     select start_date_active,
2624 	   end_date_active
2625       from GL_CODE_COMBINATIONS
2626      where code_combination_id = l_ccid;
2627   --
2628   cursor c_Stage is
2629     select Max(sequence_number) sequence_number
2630       from PSB_BUDGET_STAGES
2631      where sequence_number < l_current_stage_seq
2632        and budget_stage_set_id = l_stage_set_id;
2633   --
2634   cursor c_Budget_Group is
2635     select a.budget_group_id,
2636 	   b.num_proposed_years
2637       from PSB_SET_RELATIONS a,
2638 	   PSB_BUDGET_GROUPS b,
2639 	   PSB_BUDGET_ACCOUNTS c
2640      where a.budget_group_id = b.budget_group_id
2641 	and (((b.effective_start_date <= l_end_date)
2642 	  and (b.effective_end_date is null))
2643 	  or ((b.effective_start_date between l_start_date and l_end_date)
2644 	   or (b.effective_end_date between l_start_date and l_end_date)
2645 	  or ((b.effective_start_date < l_start_date)
2646 	  and (b.effective_end_date > l_end_date))))
2647        and b.budget_group_type = 'R'
2648        and ((b.budget_group_id = l_root_budget_group_id) or
2649 	    (b.root_budget_group_id = l_root_budget_group_id))
2650        and a.account_position_set_id = c.account_position_set_id
2651        and c.code_combination_id = l_ccid;
2652   --
2653   cursor c_seq is
2654     select PSB_WS_ACCOUNT_LINES_S.NEXTVAL seq
2655       from dual;
2656 
2657   -- Bug No 2354918 Start
2658   cursor c_WAL is
2659     select period1_amount, period2_amount, period3_amount, period4_amount,
2660 	   period5_amount, period6_amount, period7_amount, period8_amount,
2661 	   period9_amount, period10_amount, period11_amount, period12_amount,
2662 	   period13_amount, period14_amount, period15_amount, period16_amount,
2663 	   period17_amount, period18_amount, period19_amount, period20_amount,
2664 	   period21_amount, period22_amount, period23_amount, period24_amount,
2665 	   period25_amount, period26_amount, period27_amount, period28_amount,
2666 	   period29_amount, period30_amount, period31_amount, period32_amount,
2667 	   period33_amount, period34_amount, period35_amount, period36_amount,
2668 	   period37_amount, period38_amount, period39_amount, period40_amount,
2669 	   period41_amount, period42_amount, period43_amount, period44_amount,
2670 	   period45_amount, period46_amount, period47_amount, period48_amount,
2671 	   period49_amount, period50_amount, period51_amount, period52_amount,
2672 	   period53_amount, period54_amount, period55_amount, period56_amount,
2673 	   period57_amount, period58_amount, period59_amount, period60_amount,
2674 	   budget_year_id
2675       from PSB_WS_ACCOUNT_LINES
2676      where account_line_id = p_account_line_id
2677        and position_line_id is null;
2678   --
2679 BEGIN
2680 
2681   -- Standard call to check for call compatibility
2682   if not FND_API.Compatible_API_Call (l_api_version,
2683 				      p_api_version,
2684 				      l_api_name,
2685 				      G_PKG_NAME)
2686   then
2687     raise FND_API.G_EXC_UNEXPECTED_ERROR;
2688   end if;
2689 
2690   for l_index in 1..g_max_num_amounts loop
2691     l_prdamt_tbl(l_index) := null;
2692     l_period_amount_tbl(l_index) := null;
2693   end loop;
2694 
2695   for c_WS_Rec in c_WS loop
2696     l_stage_set_id := c_WS_Rec.stage_set_id;
2697     l_rounding_factor := c_WS_Rec.rounding_factor;
2698     l_allocrule_set_id := c_WS_Rec.allocrule_set_id;
2699     l_global_worksheet_id := c_WS_Rec.global_worksheet_id;
2700     l_budget_calendar_id := c_WS_Rec.budget_calendar_id;
2701     l_flex_mapping_set_id := c_WS_Rec.flex_mapping_set_id;
2702     l_gl_cutoff_period := c_WS_Rec.gl_cutoff_period;
2703     l_root_budget_group_id := c_WS_Rec.root_budget_group_id;
2704     l_local_copy_flag := c_WS_Rec.local_copy_flag;
2705   end loop;
2706 
2707   sql_wsacc := 'select code_combination_id, budget_group_id, template_id, position_line_id, ' ||
2708 	       'element_set_id, budget_year_id, currency_code, stage_set_id, start_stage_seq, ' ||
2709 	       'current_stage_seq, service_package_id, balance_type, ' ||
2710 	       'period1_amount, period2_amount, period3_amount, period4_amount, ' ||
2711 	       'period5_amount, period6_amount, period7_amount, period8_amount, ' ||
2712 	       'period9_amount, period10_amount, period11_amount, period12_amount, ' ||
2713 	       'period13_amount, period14_amount, period15_amount, period16_amount, ' ||
2714 	       'period17_amount, period18_amount, period19_amount, period20_amount, ' ||
2715 	       'period21_amount, period22_amount, period23_amount, period24_amount, ' ||
2716 	       'period25_amount, period26_amount, period27_amount, period28_amount, ' ||
2717 	       'period29_amount, period30_amount, period31_amount, period32_amount, ' ||
2718 	       'period33_amount, period34_amount, period35_amount, period36_amount, ' ||
2719 	       'period37_amount, period38_amount, period39_amount, period40_amount, ' ||
2720 	       'period41_amount, period42_amount, period43_amount, period44_amount, ' ||
2721 	       'period45_amount, period46_amount, period47_amount, period48_amount, ' ||
2722 	       'period49_amount, period50_amount, period51_amount, period52_amount, ' ||
2723 	       'period53_amount, period54_amount, period55_amount, period56_amount, ' ||
2724 	       'period57_amount, period58_amount, period59_amount, period60_amount, ' ||
2725 	       'ytd_amount ' ||
2726 	       'from PSB_WS_ACCOUNT_LINES ' ||
2727 	      'where account_line_id = :AccLineID';
2728 
2729   execute immediate sql_wsacc into
2730     l_ccid, l_budget_group_id, l_template_id, l_position_line_id, l_element_set_id, l_budget_year_id,
2731     l_currency_code, l_stage_set_id, l_start_stage_seq, l_current_stage_seq, l_service_package_id,
2732     l_balance_type, l_prdamt_tbl(1), l_prdamt_tbl(2), l_prdamt_tbl(3), l_prdamt_tbl(4),
2733     l_prdamt_tbl(5), l_prdamt_tbl(6), l_prdamt_tbl(7), l_prdamt_tbl(8), l_prdamt_tbl(9), l_prdamt_tbl(10),
2734     l_prdamt_tbl(11), l_prdamt_tbl(12), l_prdamt_tbl(13), l_prdamt_tbl(14), l_prdamt_tbl(15), l_prdamt_tbl(16),
2735     l_prdamt_tbl(17), l_prdamt_tbl(18), l_prdamt_tbl(19), l_prdamt_tbl(20), l_prdamt_tbl(21), l_prdamt_tbl(22),
2736     l_prdamt_tbl(23), l_prdamt_tbl(24), l_prdamt_tbl(25), l_prdamt_tbl(26), l_prdamt_tbl(27), l_prdamt_tbl(28),
2737     l_prdamt_tbl(29), l_prdamt_tbl(30), l_prdamt_tbl(31), l_prdamt_tbl(32), l_prdamt_tbl(33), l_prdamt_tbl(34),
2738     l_prdamt_tbl(35), l_prdamt_tbl(36), l_prdamt_tbl(37), l_prdamt_tbl(38), l_prdamt_tbl(39), l_prdamt_tbl(40),
2739     l_prdamt_tbl(41), l_prdamt_tbl(42), l_prdamt_tbl(43), l_prdamt_tbl(44), l_prdamt_tbl(45), l_prdamt_tbl(46),
2740     l_prdamt_tbl(47), l_prdamt_tbl(48), l_prdamt_tbl(49), l_prdamt_tbl(50), l_prdamt_tbl(51), l_prdamt_tbl(52),
2741     l_prdamt_tbl(53), l_prdamt_tbl(54), l_prdamt_tbl(55), l_prdamt_tbl(56), l_prdamt_tbl(57), l_prdamt_tbl(58),
2742     l_prdamt_tbl(59), l_prdamt_tbl(60), l_old_ytd_amount
2743   using p_account_line_id;
2744 
2745   -- If Service Package is being modified, check whether the target account line exists
2746 
2747   if ((p_service_package_id <> FND_API.G_MISS_NUM) and
2748       (p_service_package_id <> l_service_package_id)) then
2749   begin
2750 
2751     sql_wal := 'select account_line_id, ' ||
2752 		      'ytd_amount ' ||
2753 		 'from PSB_WS_ACCOUNT_LINES a ' ||
2754 		'where currency_code = ''' || l_currency_code || ''' ' ||
2755 		  'and ' || l_current_stage_seq || ' between start_stage_seq and current_stage_seq ' ||
2756 		  'and balance_type = ''' || l_balance_type || ''' ';
2757 
2758     if l_template_id is not null then
2759       sql_wal := sql_wal ||
2760 		'and template_id = ' || l_template_id || ' ';
2761     else
2762       sql_wal := sql_wal ||
2763 		'and template_id is null ';
2764     end if;
2765 
2766     -- For Position Account Lines, must match the Position Line ID and Element Set ID
2767 
2768     if l_position_line_id is not null then
2769       sql_wal := sql_wal ||
2770 		'and position_line_id = ' || l_position_line_id || ' ';
2771     else
2772       sql_wal := sql_wal ||
2773 		'and position_line_id is null ';
2774     end if;
2775 
2776     if l_element_set_id is not null then
2777       sql_wal := sql_wal ||
2778 		'and element_set_id = ' || l_element_set_id || ' ';
2779     else
2780       sql_wal := sql_wal ||
2781 		'and element_set_id is null ';
2782     end if;
2783 
2784     if l_position_line_id is null then
2785     begin
2786 
2787       sql_wal := sql_wal ||
2788 		'and exists ' ||
2789 		    '(select 1 ' ||
2790 		       'from PSB_WS_LINES b ' ||
2791 		      'where b.account_line_id = a.account_line_id ' ||
2792 			'and b.worksheet_id = ' || p_worksheet_id || ') ';
2793 
2794     end;
2795     end if;
2796 
2797     sql_wal := sql_wal ||
2798 	      'and stage_set_id = ' || l_stage_set_id || ' ' ||
2799 	      'and service_package_id = ' || p_service_package_id || ' ' ||
2800 	      'and budget_year_id = ' || l_budget_year_id || ' ' ||
2801 	      'and budget_group_id = ' || l_budget_group_id || ' ' ||
2802 	      'and code_combination_id = ' || l_ccid;
2803 
2804     cur_wal := dbms_sql.open_cursor;
2805     dbms_sql.parse(cur_wal, sql_wal, dbms_sql.v7);
2806 
2807     dbms_sql.define_column(cur_wal, 1, l_spal_id);
2808     dbms_sql.define_column(cur_wal, 2, l_spytd_amount);
2809 
2810     num_wal := dbms_sql.execute(cur_wal);
2811 
2812     loop
2813 
2814       if dbms_sql.fetch_rows(cur_wal) = 0 then
2815 	exit;
2816       end if;
2817 
2818       dbms_sql.column_value(cur_wal, 1, l_spal_id);
2819       dbms_sql.column_value(cur_wal, 2, l_spytd_amount);
2820 
2821       l_spal_exists := TRUE;
2822 
2823     end loop;
2824 
2825     dbms_sql.close_cursor(cur_wal);
2826 
2827   end;
2828   end if;
2829 
2830   -- Now determine if new Stage is to be inserted
2831 
2832   if (((p_service_package_id <> FND_API.G_MISS_NUM) and
2833        (p_service_package_id <> l_service_package_id)) or
2834       ((p_ytd_amount <> FND_API.G_MISS_NUM) and
2835        (p_ytd_amount <> l_old_ytd_amount))) then
2836     l_new_stage := TRUE;
2837   end if;
2838 
2839  /*For Bug No : 2440100 Start*/
2840   --the following code has been implemented since the p_period_amount
2841   --doen't hold any values if the API call happens from Form
2842   if not l_new_stage then
2843 
2844     for l_index in 1..g_max_num_amounts loop
2845       if p_period_amount(l_index) is not null then
2846 	l_period_empty := FALSE;
2847 	exit;
2848       end if;
2849     end loop;
2850 
2851     if not l_period_empty then
2852       for l_index in 1..g_max_num_amounts loop
2853 
2854        if nvl(p_period_amount(l_index), FND_API.G_MISS_NUM) <> nvl(l_prdamt_tbl(l_index), FND_API.G_MISS_NUM) then
2855 	l_new_stage := TRUE;
2856 	exit;
2857        end if;
2858 
2859       end loop;
2860     end if;
2861   end if;
2862   --the following code is now available in the above block
2863   /*for l_index in 1..g_max_num_amounts loop
2864 
2865     if nvl(p_period_amount(l_index), FND_API.G_MISS_NUM) <> nvl(l_prdamt_tbl(l_index), FND_API.G_MISS_NUM) then
2866       l_new_stage := TRUE;
2867       exit;
2868     end if;
2869 
2870   end loop;*/
2871   /*For Bug No : 2440100 End*/
2872 
2873   if ((l_new_stage) and
2874       (l_start_stage_seq = l_current_stage_seq)) then
2875     l_new_stage := FALSE;
2876   end if;
2877 
2878   l_userid := FND_GLOBAL.USER_ID;
2879   l_loginid := FND_GLOBAL.LOGIN_ID;
2880 
2881   if ((FND_API.to_Boolean(p_check_stages)) and
2882       (l_new_stage) and
2883       (l_start_stage_seq < l_current_stage_seq)) then
2884   begin
2885 
2886     -- Find Previous Stage for the Budget Stage Set
2887 
2888     for c_Stage_Rec in c_Stage loop
2889       l_previous_stage := c_Stage_Rec.sequence_number;
2890     end loop;
2891 
2892     for c_seq_rec in c_seq loop
2893       l_acclineid := c_seq_rec.seq;
2894     end loop;
2895 
2896     insert into PSB_WS_ACCOUNT_LINES
2897 	  (account_line_id, code_combination_id, position_line_id, service_package_id, budget_group_id,
2898 	   element_set_id, salary_account_line, stage_set_id, start_stage_seq, current_stage_seq,
2899 	   end_stage_seq, copy_of_account_line_id, last_update_date, last_updated_by,
2900 	   last_update_login, created_by, creation_date, template_id, budget_year_id, annual_fte,
2901 	   currency_code, account_type, balance_type,
2902 	   period1_amount, period2_amount, period3_amount, period4_amount, period5_amount, period6_amount,
2903 	   period7_amount, period8_amount, period9_amount, period10_amount, period11_amount, period12_amount,
2904 	   period13_amount, period14_amount, period15_amount, period16_amount, period17_amount, period18_amount,
2905 	   period19_amount, period20_amount, period21_amount, period22_amount, period23_amount, period24_amount,
2906 	   period25_amount, period26_amount, period27_amount, period28_amount, period29_amount, period30_amount,
2907 	   period31_amount, period32_amount, period33_amount, period34_amount, period35_amount, period36_amount,
2908 	   period37_amount, period38_amount, period39_amount, period40_amount, period41_amount, period42_amount,
2909 	   period43_amount, period44_amount, period45_amount, period46_amount, period47_amount, period48_amount,
2910 	   period49_amount, period50_amount, period51_amount, period52_amount, period53_amount, period54_amount,
2911 	   period55_amount, period56_amount, period57_amount, period58_amount, period59_amount, period60_amount,
2912 	   ytd_amount, functional_transaction)
2913     select l_acclineid,
2914 	   code_combination_id, position_line_id, service_package_id, budget_group_id,
2915 	   element_set_id, salary_account_line, stage_set_id, start_stage_seq,
2916 	   l_previous_stage,
2917 	   l_previous_stage,
2918 	   copy_of_account_line_id, sysdate,
2919 	   l_userid, l_loginid, l_userid,
2920 	   sysdate, template_id, budget_year_id, annual_fte, currency_code, account_type, balance_type,
2921 	   period1_amount, period2_amount, period3_amount, period4_amount, period5_amount, period6_amount,
2922 	   period7_amount, period8_amount, period9_amount, period10_amount, period11_amount, period12_amount,
2923 	   period13_amount, period14_amount, period15_amount, period16_amount, period17_amount, period18_amount,
2924 	   period19_amount, period20_amount, period21_amount, period22_amount, period23_amount, period24_amount,
2925 	   period25_amount, period26_amount, period27_amount, period28_amount, period29_amount, period30_amount,
2926 	   period31_amount, period32_amount, period33_amount, period34_amount, period35_amount, period36_amount,
2927 	   period37_amount, period38_amount, period39_amount, period40_amount, period41_amount, period42_amount,
2928 	   period43_amount, period44_amount, period45_amount, period46_amount, period47_amount, period48_amount,
2929 	   period49_amount, period50_amount, period51_amount, period52_amount, period53_amount, period54_amount,
2930 	   period55_amount, period56_amount, period57_amount, period58_amount, period59_amount, period60_amount,
2931 	   ytd_amount, functional_transaction
2932       from PSB_WS_ACCOUNT_LINES
2933      where account_line_id = p_account_line_id;
2934 
2935     -- Create an entry for all the worksheets assigned to the current account line
2936 
2937     insert into PSB_WS_LINES
2938 	  (worksheet_id, account_line_id, freeze_flag,
2939 	   view_line_flag, last_update_date, last_updated_by,
2940 	   last_update_login, created_by, creation_date)
2941     select worksheet_id, l_acclineid, freeze_flag,
2942 	   view_line_flag, sysdate, l_userid,
2943 	   l_loginid, l_userid, sysdate
2944       from PSB_WS_LINES
2945      where account_line_id = p_account_line_id;
2946 
2947   end;
2948   end if;
2949 
2950   -- If Service package is being modified and the target Account Line already exists,
2951   -- delete the target Account Line and increment values to the current Account Line
2952 
2953   if l_spal_exists then
2954   begin
2955 
2956     PSB_WORKSHEET.Delete_WAL
2957        (p_api_version => 1.0,
2958 	p_return_status => l_return_status,
2959 	p_account_line_id => l_spal_id);
2960 
2961     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
2962       raise FND_API.G_EXC_ERROR;
2963     end if;
2964 
2965   end;
2966   end if;
2967 
2968   -- Cache Budget Calendar
2969 
2970   if l_budget_calendar_id <> nvl(g_budget_calendar_id, FND_API.G_MISS_NUM) then
2971   begin
2972 
2973     Cache_Budget_Calendar
2974 	 (p_return_status => l_return_status,
2975 	  p_budget_calendar_id => l_budget_calendar_id);
2976 
2977     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
2978       raise FND_API.G_EXC_ERROR;
2979     end if;
2980 
2981   end;
2982   end if;
2983 
2984   -- Find last Period index for the Budget Year to adjust the rounding difference
2985 /* Bug No 2354918 Start */
2986    g_cy_start_index := 0;
2987 /* Bug No 2354918 End */
2988 
2989   for l_year_index in 1..g_num_budget_years loop
2990 
2991     if g_budget_years(l_year_index).budget_year_id = l_budget_year_id then
2992       l_last_period_index := g_budget_years(l_year_index).last_period_index;
2993       l_start_date := g_budget_years(l_year_index).start_date;
2994       l_end_date := g_budget_years(l_year_index).end_date;
2995       l_budget_year_type_id := g_budget_years(l_year_index).budget_year_type_id;
2996 
2997 /* Bug No 2354918 Start */
2998       l_budget_year_type := g_budget_years(l_year_index).year_type;
2999 
3000       if l_budget_year_type = 'CY' then
3001 	for l_period_index in 1..g_num_budget_periods loop
3002 	  if (g_budget_periods(l_period_index).budget_year_id = l_budget_year_id
3003 	     and g_budget_periods(l_period_index).end_date <=
3004 		 nvl(l_gl_cutoff_period, g_budget_years(l_year_index).end_date)) then
3005 	  begin
3006 
3007 	    g_cy_start_index := g_cy_start_index + 1;
3008 
3009 	  end;
3010 	  end if;
3011 	end loop;
3012       end if;
3013 /* Bug No 2354918 End */
3014 
3015       exit;
3016     end if;
3017 
3018   end loop;
3019 
3020 /* Bug No 2342169 Start */
3021   g_cy_start_index := g_cy_start_index + 1;
3022 /* Bug No 2342169 End */
3023 
3024   -- Bug#3128597: Support prorated allocation during annual amount updation
3025   -- The following is not needed. Proration logic should not use allocation
3026   -- if any period amount is not null.
3027   /*
3028   for i in g_cy_start_index..l_last_period_index loop
3029     if (nvl(l_prdamt_tbl(i), 0) = 0) then
3030 	l_redist_palloc := 'Y';
3031 	exit;
3032     end if;
3033   end loop;
3034   */
3035   -- Bug#3128597: End
3036 
3037   if p_budget_group_id <> FND_API.G_MISS_NUM then
3038     l_new_budget_group_id := p_budget_group_id;
3039   else
3040   begin
3041 
3042     for c_Budget_Group_Rec in c_Budget_Group loop
3043       l_new_budget_group_id := c_Budget_Group_Rec.budget_group_id;
3044     end loop;
3045 
3046   end;
3047   end if;
3048 
3049   if l_budget_group_id <> nvl(l_new_budget_group_id, l_budget_group_id) then
3050     l_budget_group_changed := TRUE;
3051   else
3052     l_new_budget_group_id := l_budget_group_id;
3053   end if;
3054 
3055 /* Bug No 2354918 Start */
3056   l_cy_ytd_amount := 0;
3057 
3058   if l_budget_year_type = 'CY' then
3059 
3060   for c_WAL_Rec in c_WAL loop
3061   if c_WAL_Rec.budget_year_id = l_budget_year_id then
3062 
3063     if 1 between 1 and g_cy_start_index - 1 then
3064       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period1_amount, 0);
3065     end if;
3066 
3067     if 2 between 1 and g_cy_start_index - 1 then
3068       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period2_amount, 0);
3069     end if;
3070 
3071     if 3 between 1 and g_cy_start_index - 1 then
3072       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period3_amount, 0);
3073     end if;
3074 
3075     if 4 between 1 and g_cy_start_index - 1 then
3076       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period4_amount, 0);
3077     end if;
3078 
3079     if 5 between 1 and g_cy_start_index - 1 then
3080       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period5_amount, 0);
3081     end if;
3082 
3083     if 6 between 1 and g_cy_start_index - 1 then
3084       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period6_amount, 0);
3085     end if;
3086 
3087     if 7 between 1 and g_cy_start_index - 1 then
3088       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period7_amount, 0);
3089     end if;
3090 
3091     if 8 between 1 and g_cy_start_index - 1 then
3092       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period8_amount, 0);
3093     end if;
3094 
3095     if 9 between 1 and g_cy_start_index - 1 then
3096       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period9_amount, 0);
3097     end if;
3098 
3099     if 10 between 1 and g_cy_start_index - 1 then
3100       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period10_amount, 0);
3101     end if;
3102 
3103     if 11 between 1 and g_cy_start_index - 1 then
3104       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period11_amount, 0);
3105     end if;
3106 
3107     if 12 between 1 and g_cy_start_index - 1 then
3108       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period12_amount, 0);
3109     end if;
3110 
3111     if 13 between 1 and g_cy_start_index - 1 then
3112       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period13_amount, 0);
3113     end if;
3114 
3115     if 14 between 1 and g_cy_start_index - 1 then
3116       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period14_amount, 0);
3117     end if;
3118 
3119     if 15 between 1 and g_cy_start_index - 1 then
3120       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period15_amount, 0);
3121     end if;
3122 
3123     if 16 between 1 and g_cy_start_index - 1 then
3124       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period16_amount, 0);
3125     end if;
3126 
3127     if 17 between 1 and g_cy_start_index - 1 then
3128       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period17_amount, 0);
3129     end if;
3130 
3131     if 18 between 1 and g_cy_start_index - 1 then
3132       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period18_amount, 0);
3133     end if;
3134 
3135     if 19 between 1 and g_cy_start_index - 1 then
3136       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period19_amount, 0);
3137     end if;
3138 
3139     if 20 between 1 and g_cy_start_index - 1 then
3140       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period20_amount, 0);
3141     end if;
3142 
3143     if 21 between 1 and g_cy_start_index - 1 then
3144       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period21_amount, 0);
3145     end if;
3146 
3147     if 22 between 1 and g_cy_start_index - 1 then
3148       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period22_amount, 0);
3149     end if;
3150 
3151     if 23 between 1 and g_cy_start_index - 1 then
3152       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period23_amount, 0);
3153     end if;
3154 
3155     if 24 between 1 and g_cy_start_index - 1 then
3156       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period24_amount, 0);
3157     end if;
3158 
3159     if 25 between 1 and g_cy_start_index - 1 then
3160       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period25_amount, 0);
3161     end if;
3162 
3163     if 26 between 1 and g_cy_start_index - 1 then
3164       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period26_amount, 0);
3165     end if;
3166 
3167     if 27 between 1 and g_cy_start_index - 1 then
3168       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period27_amount, 0);
3169     end if;
3170 
3171     if 28 between 1 and g_cy_start_index - 1 then
3172       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period28_amount, 0);
3173     end if;
3174 
3175     if 29 between 1 and g_cy_start_index - 1 then
3176       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period29_amount, 0);
3177     end if;
3178 
3179     if 30 between 1 and g_cy_start_index - 1 then
3180       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period30_amount, 0);
3181     end if;
3182 
3183     if 31 between 1 and g_cy_start_index - 1 then
3184       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period31_amount, 0);
3185     end if;
3186 
3187     if 32 between 1 and g_cy_start_index - 1 then
3188       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period32_amount, 0);
3189     end if;
3190 
3191     if 33 between 1 and g_cy_start_index - 1 then
3192       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period33_amount, 0);
3193     end if;
3194 
3195     if 34 between 1 and g_cy_start_index - 1 then
3196       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period34_amount, 0);
3197     end if;
3198 
3199     if 35 between 1 and g_cy_start_index - 1 then
3200       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period35_amount, 0);
3201     end if;
3202 
3203     if 36 between 1 and g_cy_start_index - 1 then
3204       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period36_amount, 0);
3205     end if;
3206 
3207     if 37 between 1 and g_cy_start_index - 1 then
3208       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period37_amount, 0);
3209     end if;
3210 
3211     if 38 between 1 and g_cy_start_index - 1 then
3212       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period38_amount, 0);
3213     end if;
3214 
3215     if 39 between 1 and g_cy_start_index - 1 then
3216       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period39_amount, 0);
3217     end if;
3218 
3219     if 40 between 1 and g_cy_start_index - 1 then
3220       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period40_amount, 0);
3221     end if;
3222 
3223     if 41 between 1 and g_cy_start_index - 1 then
3224       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period41_amount, 0);
3225     end if;
3226 
3227     if 42 between 1 and g_cy_start_index - 1 then
3228       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period42_amount, 0);
3229     end if;
3230 
3231     if 43 between 1 and g_cy_start_index - 1 then
3232       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period43_amount, 0);
3233     end if;
3234 
3235     if 44 between 1 and g_cy_start_index - 1 then
3236       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period44_amount, 0);
3237     end if;
3238 
3239     if 45 between 1 and g_cy_start_index - 1 then
3240       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period45_amount, 0);
3241     end if;
3242 
3243     if 46 between 1 and g_cy_start_index - 1 then
3244       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period46_amount, 0);
3245     end if;
3246 
3247     if 47 between 1 and g_cy_start_index - 1 then
3248       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period47_amount, 0);
3249     end if;
3250 
3251     if 48 between 1 and g_cy_start_index - 1 then
3252       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period48_amount, 0);
3253     end if;
3254 
3255     if 49 between 1 and g_cy_start_index - 1 then
3256       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period49_amount, 0);
3257     end if;
3258 
3259     if 50 between 1 and g_cy_start_index - 1 then
3260       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period50_amount, 0);
3261     end if;
3262 
3263     if 51 between 1 and g_cy_start_index - 1 then
3264       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period51_amount, 0);
3265     end if;
3266 
3267     if 52 between 1 and g_cy_start_index - 1 then
3268       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period52_amount, 0);
3269     end if;
3270 
3271     if 53 between 1 and g_cy_start_index - 1 then
3272       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period53_amount, 0);
3273     end if;
3274 
3275     if 54 between 1 and g_cy_start_index - 1 then
3276       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period54_amount, 0);
3277     end if;
3278 
3279     if 55 between 1 and g_cy_start_index - 1 then
3280       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period55_amount, 0);
3281     end if;
3282 
3283     if 56 between 1 and g_cy_start_index - 1 then
3284       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period56_amount, 0);
3285     end if;
3286 
3287     if 57 between 1 and g_cy_start_index - 1 then
3288       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period57_amount, 0);
3289     end if;
3290 
3291     if 58 between 1 and g_cy_start_index - 1 then
3292       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period58_amount, 0);
3293     end if;
3294 
3295     if 59 between 1 and g_cy_start_index - 1 then
3296       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period59_amount, 0);
3297     end if;
3298 
3299     if 60 between 1 and g_cy_start_index - 1 then
3300       l_cy_ytd_amount := l_cy_ytd_amount + nvl(c_WAL_Rec.period60_amount, 0);
3301     end if;
3302 
3303 
3304   end if;
3305   end loop;
3306 
3307   end if;
3308 /* Bug No 2354918 End */
3309 
3310   if FND_API.to_Boolean(p_distribute_flag) then
3311   begin
3312 
3313     -- If current YTD Amount is 0 and new YTD Amount is not 0, distribute
3314     -- using the Period Allocation rules.
3315 
3316 /* Bug No 2342169 Start */
3317     -- if (((l_old_ytd_amount = 0) or (nvl(l_redist_palloc, 'N') = 'Y') or
3318     -- (l_budget_year_type = 'CY')) and
3319 /* Bug No 2342169 End */
3320 
3321     -- Bug#3128597: Support prorated allocation during annual amount updation
3322     if ( ( l_old_ytd_amount = 0 or nvl(l_redist_palloc, 'N') = 'Y'
3323            or (l_old_ytd_amount = l_cy_ytd_amount)
3324          )
3325          and
3326          ( nvl(p_ytd_amount, FND_API.G_MISS_NUM) <> FND_API.G_MISS_NUM)
3327        )
3328     then
3329     -- Bug#3128597: End
3330     begin
3331       for c_CCID_Rec in c_CCID loop
3332 	l_ccid_start_date := c_CCID_Rec.start_date_active;
3333 	l_ccid_end_date := c_CCID_Rec.end_date_active;
3334       end loop;
3335 
3336       for l_year_index in 1..g_num_budget_years loop
3337 
3338 	if g_budget_years(l_year_index).budget_year_id = l_budget_year_id then
3339 	begin
3340 
3341 	  l_start_date := g_budget_years(l_year_index).start_date;
3342 	  l_end_date := g_budget_years(l_year_index).end_date;
3343 
3344 	  for l_init_index in 1..l_budget_periods.Count loop
3345 	    l_budget_periods(l_init_index).budget_period_id := null;
3346 	    l_budget_periods(l_init_index).start_date := null;
3347 	    l_budget_periods(l_init_index).end_date := null;
3348 	    l_budget_periods(l_init_index).long_sequence_no := null;
3349 	    l_budget_periods(l_init_index).budget_year_id := null;
3350 	  end loop;
3351 
3352 	  l_init_index := 1;
3353 
3354 	  for l_period_index in 1..g_num_budget_periods loop
3355 
3356 	    if g_budget_periods(l_period_index).budget_year_id = l_budget_year_id then
3357 	    begin
3358 
3359 	      -- Get all Budget Periods for the PP Budget Year or all Budget Periods beyond the
3360 	      -- GL Cutoff Date for the CY Budget Year
3361 
3362 	      if (((l_ccid_start_date is null) or
3363 		   (l_ccid_start_date <= g_budget_periods(l_period_index).start_date)) and
3364 		  ((l_ccid_end_date is null) or
3365 		   (l_ccid_end_date >= g_budget_periods(l_period_index).end_date)) and
3366 		  ((g_budget_years(l_year_index).year_type = 'PP') or ((g_budget_years(l_year_index).year_type = 'CY') and
3367 		  ((l_gl_cutoff_period is null) or (l_gl_cutoff_period < g_budget_periods(l_period_index).start_date))))) then
3368 	      begin
3369 
3370 		l_budget_periods(l_init_index).budget_period_id := g_budget_periods(l_period_index).budget_period_id;
3371 		l_budget_periods(l_init_index).long_sequence_no := g_budget_periods(l_period_index).long_sequence_no;
3372 		l_budget_periods(l_init_index).start_date := g_budget_periods(l_period_index).start_date;
3373 		l_budget_periods(l_init_index).end_date := g_budget_periods(l_period_index).end_date;
3374 		l_budget_periods(l_init_index).budget_year_id := l_budget_year_id;
3375 
3376 		l_init_index := l_init_index + 1;
3377 
3378 	      end;
3379 	      end if;
3380 
3381 	    end;
3382 	    end if;
3383 
3384 	  end loop;
3385 
3386 	end;
3387 	end if;
3388 
3389       end loop;
3390 
3391       /* Bug 3352171 start */
3392       -- Comment out the following two lines. The allocation logic will be
3393       -- handled in the PSB_WS_ACCT2.Distribute_Account_Lines function.
3394       --if nvl(l_allocrule_set_id, FND_API.G_MISS_NUM) <> FND_API.G_MISS_NUM
3395       -- then
3396       --begin
3397       /* Bug 3352171 end */
3398 
3399       if l_spal_exists then
3400         l_allocate_ytd_amount := nvl(p_ytd_amount, 0) + l_spytd_amount;
3401       else
3402         l_allocate_ytd_amount := nvl(p_ytd_amount, 0);
3403       end if;
3404 
3405       PSB_WS_ACCT2.Distribute_Account_Lines
3406       ( p_return_status => l_return_status,
3407         p_worksheet_id => p_worksheet_id,
3408         p_flex_mapping_set_id => l_flex_mapping_set_id,
3409         p_budget_year_type_id => l_budget_year_type_id,
3410         p_allocrule_set_id => l_allocrule_set_id,
3411         p_budget_calendar_id => l_budget_calendar_id,
3412         p_currency_code => l_currency_code,
3413         p_ccid => l_ccid,
3414         /* Bug No 2354918 Start */
3415         -- p_ytd_amount => l_allocate_ytd_amount,
3416         p_ytd_amount => (l_allocate_ytd_amount - l_cy_ytd_amount),
3417         /* Bug No 2354918 End */
3418         p_allocation_type => 'PERCENT',
3419         /* Bug No 2342169 Start */
3420         p_rounding_factor => l_rounding_factor,
3421         /* Bug No 2342169 End */
3422         p_effective_start_date => l_start_date,
3423         p_effective_end_date => l_end_date,
3424         p_budget_periods => l_budget_periods,
3425         p_period_amount => l_period_amount_tbl);
3426 
3427       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3428         raise FND_API.G_EXC_ERROR;
3429       end if;
3430 
3431       /* Bug 3352171 start */
3432       -- comment out the following two lines. The allocation logic should not
3433       -- be handling here.
3434       --end;
3435       --end if;
3436       /* Bug 3352171 end */
3437 
3438       l_distribute1_flag := TRUE;
3439 
3440     end;
3441 
3442     -- If current YTD Amount is not 0, prorate the period amounts in the ratio
3443     -- of the YTD Amounts
3444 
3445     /* Bug No 2354918 Start */
3446     --elsif ((l_old_ytd_amount <> 0) and (l_budget_year_type <> 'CY') and
3447     /* Bug No 2354918 End */
3448 
3449     -- Bug#3128597: Support prorated allocation during annual amount updation
3450     elsif ( (l_old_ytd_amount <> 0) and
3451 	    (nvl(p_ytd_amount, FND_API.G_MISS_NUM) <> FND_API.G_MISS_NUM))
3452     then
3453     -- Bug#3128597: End
3454     begin
3455 
3456       if l_spal_exists then
3457 	l_allocate_ytd_amount := nvl(p_ytd_amount, 0) + l_spytd_amount;
3458       else
3459 	l_allocate_ytd_amount := nvl(p_ytd_amount, 0);
3460       end if;
3461 
3462       PSB_WS_ACCT2.Distribute_Account_Lines
3463 	 (p_return_status => l_return_status,
3464 	  p_worksheet_id => p_worksheet_id,
3465 	  p_service_package_id => l_service_package_id,
3466 	  p_stage_set_id => l_stage_set_id,
3467 	  p_current_stage_seq => l_current_stage_seq,
3468 	  p_account_line_id => p_account_line_id,
3469 	  p_rounding_factor => l_rounding_factor,
3470 	  p_old_ytd_amount => l_old_ytd_amount,
3471 	  p_new_ytd_amount => l_allocate_ytd_amount,
3472 	  p_cy_ytd_amount  => l_cy_ytd_amount,
3473 	  p_budget_group_id => l_new_budget_group_id);
3474 
3475       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3476 	raise FND_API.G_EXC_ERROR;
3477       end if;
3478 
3479       l_distribute2_flag := TRUE;
3480 
3481     end;
3482     end if;
3483 
3484   end;
3485   end if;
3486 
3487   -- Update PSB_WS_ACCOUNT_LINES with input parameter values passed in.
3488   -- Bug#3132485: Replaced references to G_MISS_NUM by bind ones.
3489 
3490   -- Bug#5030613
3491   -- Replaced l_new_budget_group_id by b_budget_group_id bind variable.
3492   sql_wsacc :=
3493     'update PSB_WS_ACCOUNT_LINES ' ||
3494     'set service_package_id = decode(:p_service_package_id1, :gmn1, service_package_id, :p_service_package_id2), ' ||
3495     'budget_group_id = :b_budget_group_id, ' ||
3496     'copy_of_account_line_id = decode(:p_copy_of_account_line_id1, :gmn2 , copy_of_account_line_id, :p_copy_of_account_line_id2), annual_fte = decode(' ||
3497     'nvl(:p_annual_fte1, :gmn3), :gmn4, annual_fte, nvl(:p_annual_fte2, :gmn5) ), ' ;
3498 
3499   -- Bug#3128597: Support prorated allocation during annual amount updation
3500   -- We need to process only estimate periods in this loop for CY/PP years.
3501   /* for l_index in 1..g_max_num_amounts loop */
3502   /* for l_index in g_cy_start_index..l_last_period_index loop */
3503   -- Bug#3128597: End
3504 
3505   -- Bug#3258892: For position account lines, we calculate estimate balances
3506   -- for entire CY from position costs and ignore GL cut-off date.
3507   IF l_position_line_id IS NOT NULL THEN
3508     l_cy_start_index := 1 ;
3509   ELSE
3510     l_cy_start_index := g_cy_start_index ;
3511   END IF;
3512   --
3513   FOR l_index IN l_cy_start_index..l_last_period_index LOOP
3514 
3515     if FND_API.to_Boolean(p_distribute_flag) then
3516     begin
3517 
3518       IF l_distribute1_flag THEN
3519       /* Bug 3197852 Start */
3520         IF l_rounding_factor is null THEN
3521           l_period_amount := l_period_amount_tbl(l_index);
3522         ELSE
3523           l_period_amount := ROUND(l_period_amount_tbl(l_index)/l_rounding_factor) * l_rounding_factor;
3524         END IF;
3525       /* Bug 3197852 End */
3526 	l_running_ytd_amount := l_running_ytd_amount + nvl(l_period_amount, 0);
3527 
3528       END IF;
3529 
3530     end;
3531     else
3532     begin
3533 
3534       if l_rounding_factor is null then
3535 	l_period_amount := p_period_amount(l_index);
3536       else
3537 	l_period_amount := ROUND(p_period_amount(l_index)/l_rounding_factor) * l_rounding_factor;
3538       end if;
3539 
3540       l_running_ytd_amount := l_running_ytd_amount + nvl(l_period_amount, 0);
3541 
3542     end;
3543     end if;
3544 
3545   end loop;
3546 
3547 /* Bug No 2354918 Start */
3548   if l_cy_ytd_amount <> 0 then
3549      l_running_ytd_amount := l_running_ytd_amount + l_cy_ytd_amount;
3550   end if;
3551 /* Bug No 2354918 End */
3552 
3553   if l_rounding_factor is null then
3554   begin
3555 
3556     if l_spal_exists then
3557       l_new_ytd_amount := nvl(p_ytd_amount, 0) + l_spytd_amount;
3558     else
3559       l_new_ytd_amount := nvl(p_ytd_amount, 0);
3560     end if;
3561 
3562     l_rounding_difference := 0;
3563 
3564   end;
3565   else
3566   begin
3567 
3568     l_new_ytd_amount := ROUND(nvl(p_ytd_amount, 0) / l_rounding_factor) * l_rounding_factor;
3569 
3570     if l_spal_exists then
3571       l_new_ytd_amount := l_new_ytd_amount + l_spytd_amount;
3572     end if;
3573 
3574     /* Bug No 2379695 Start */
3575     -- Commented the IF condition
3576     --    if ((l_running_ytd_amount > 0) and (l_new_ytd_amount > 0)) then
3577     --    end if;
3578 
3579     /* start bug 4128196 */
3580     IF NVL(p_update_cy_estimate, 'N') = 'Y' THEN
3581       l_rounding_difference := 0;
3582     ELSE
3583       l_rounding_difference := l_new_ytd_amount - l_running_ytd_amount;
3584     END IF;
3585     /* end bug 4128196 */
3586 
3587 
3588     /* Bug No 2379695 End */
3589 
3590   end;
3591   end if;
3592 
3593   if not FND_API.to_Boolean(p_distribute_flag) then
3594   begin
3595 
3596     for l_index in 1..g_max_num_amounts loop
3597 
3598       if l_rounding_factor is null then
3599 	l_period_amount := p_period_amount(l_index);
3600       else
3601 	l_period_amount := ROUND(p_period_amount(l_index)/l_rounding_factor) * l_rounding_factor;
3602       end if;
3603 
3604      /* start bug no 4128196 */
3605 	IF NVL(p_update_cy_estimate, 'N') = 'Y' THEN
3606 	  l_running_total := NVL(l_running_total, 0) + NVL(l_period_amount, 0);
3607 	END IF;
3608      /* end bug no 4128196 */
3609 
3610 
3611       if l_period_amount is null then
3612       begin
3613 
3614 	if l_index = l_last_period_index then
3615           /* Bug 3663044: Use binding variables
3616 	  sql_wsacc := sql_wsacc ||
3617 		      'period' || l_index || '_amount = ' || nvl(l_rounding_difference, 0)  || ', ';
3618           */
3619           l_prdamt_tbl(l_index) := nvl(l_rounding_difference, 0);
3620 	else
3621           /* Bug 3663044: Use binding variables
3622 	  sql_wsacc := sql_wsacc ||
3623 		      'period' || l_index || '_amount = null, ';
3624           */
3625           l_prdamt_tbl(l_index) := null;
3626 	end if;
3627 
3628       end;
3629       else
3630       begin
3631 
3632 	if l_index = l_last_period_index then
3633           /* Bug 3663044: Use binding variables
3634 	  sql_wsacc := sql_wsacc ||
3635 		      'period' || l_index || '_amount = ' || (l_period_amount + nvl(l_rounding_difference, 0)) || ', ';
3636           */
3637 
3638            /* start bug no 4128196 */
3639 	   IF NVL(p_update_cy_estimate, 'N') = 'Y' THEN
3640              l_prdamt_tbl(l_index) := l_period_amount +
3641                                       (l_new_ytd_amount - l_running_total) +
3642                                       NVL(l_rounding_difference, 0);
3643 
3644 	  ELSE
3645              l_prdamt_tbl(l_index) := l_period_amount +
3646                                    nvl(l_rounding_difference, 0);
3647           END IF;
3648 	  /* end bug no 4128196 */
3649 
3650 	else
3651           /* Bug 3663044: Use binding variables
3652 	  sql_wsacc := sql_wsacc ||
3653 		      'period' || l_index || '_amount = ' || l_period_amount || ', ';
3654           */
3655           l_prdamt_tbl(l_index) := l_period_amount;
3656 	end if;
3657 
3658       end;
3659       end if;
3660 
3661       -- Bug 3663044: Use binding variables
3662       sql_wsacc := sql_wsacc ||
3663                    'period' || l_index || '_amount = :prdamt' || l_index ||
3664                    ', ';
3665     end loop;
3666 
3667 
3668   end;
3669   else
3670   begin
3671 
3672     if l_distribute1_flag then
3673     begin
3674 
3675       for l_index in 1..g_max_num_amounts loop
3676 
3677 	if l_rounding_factor is null then
3678 	  l_period_amount := l_period_amount_tbl(l_index);
3679 	else
3680 	  l_period_amount := ROUND(l_period_amount_tbl(l_index)/l_rounding_factor) * l_rounding_factor;
3681 	end if;
3682 
3683        /* start bug no 4128196 */
3684 	IF NVL(p_update_cy_estimate, 'N') = 'Y' THEN
3685 	  l_running_total := NVL(l_running_total, 0) + NVL(l_period_amount, 0);
3686 	END IF;
3687       /* end bug no 4128196 */
3688 
3689 	if l_period_amount is null then
3690 	begin
3691 
3692 	  if l_index = l_last_period_index then
3693             /* Bug 3663044: Use binding variables
3694 	    sql_wsacc := sql_wsacc ||
3695 			'period' || l_index || '_amount = ' || nvl(l_rounding_difference, 0) || ', ';
3696             */
3697             l_prdamt_tbl(l_index) := nvl(l_rounding_difference, 0);
3698 /* Bug No 2354918 Start */
3699 --          else
3700 	  elsif l_index >= g_cy_start_index then
3701 /* Bug No 2354918 End */
3702             /* Bug 3663044: Use binding variables
3703 	    sql_wsacc := sql_wsacc ||
3704 			'period' || l_index || '_amount = null, ';
3705             */
3706             l_prdamt_tbl(l_index) := null;
3707 	  end if;
3708 
3709 	end;
3710 	else
3711 	begin
3712 
3713 	  if l_index = l_last_period_index then
3714             /* Bug 3663044: Use binding variables
3715 	    sql_wsacc := sql_wsacc ||
3716 			'period' || l_index || '_amount = ' || (l_period_amount + nvl(l_rounding_difference, 0)) || ', ';
3717             */
3718 
3719           /* start bug no 4128196 */
3720 	  IF NVL(p_update_cy_estimate, 'N') = 'Y' THEN
3721              l_prdamt_tbl(l_index) := l_period_amount +
3722                                       (l_new_ytd_amount - l_running_total) +
3723                                       NVL(l_rounding_difference, 0);
3724 
3725 	  ELSE
3726             l_prdamt_tbl(l_index) := l_period_amount +
3727                                      nvl(l_rounding_difference, 0);
3728           END IF;
3729           /* end bug no 4128196 */
3730 
3731 /* Bug No 2354918 Start */
3732 --          else
3733 	  elsif l_index >= g_cy_start_index then
3734 /* Bug No 2354918 End */
3735             /* Bug 3663044: Use binding variables
3736 	    sql_wsacc := sql_wsacc ||
3737 			'period' || l_index || '_amount = ' || l_period_amount || ', ';
3738             */
3739             l_prdamt_tbl(l_index) := l_period_amount;
3740 	  end if;
3741 
3742 	end;
3743 	end if;
3744 
3745 
3746         -- Bug 3663044: Use binding variables
3747         sql_wsacc := sql_wsacc ||
3748                      'period' || l_index || '_amount = :prdamt' || l_index ||
3749                      ', ';
3750       end loop;
3751 
3752     end;
3753     end if;
3754 
3755   end;
3756   end if;
3757 
3758   if not FND_API.to_Boolean(p_distribute_flag) then
3759     -- Bug#5030613
3760     -- Replaced literal by bind variable.
3761     sql_wsacc := sql_wsacc ||
3762 		'ytd_amount = :b_ytd_amount, ';
3763   else
3764   begin
3765 
3766     if l_distribute1_flag then
3767       -- Bug#5030613
3768       -- Replaced literal by bind variable.
3769       sql_wsacc := sql_wsacc ||
3770 		  'ytd_amount = :b_ytd_amount, ';
3771     end if;
3772 
3773   end;
3774   end if;
3775 
3776   -- Bug#5030613
3777   -- Replaced literals by bind variables.
3778   sql_wsacc := sql_wsacc ||
3779 	      'last_update_date = :b_last_update_date, ' ||
3780 	      'last_updated_by = :b_last_updated_by, ' ||
3781 	      'last_update_login = :b_last_update_login, ';
3782 
3783   --
3784   -- If new Stage has been created, update Start Stage and Current Stage
3785   -- Sequences for the current Account Line; otherwise, update Current Stage
3786   -- Sequence only if passed in as input parameter value
3787   --
3788   IF (     FND_API.To_Boolean( p_check_stages )
3789        AND l_new_stage
3790        AND ( l_start_stage_seq < l_current_stage_seq )
3791      )
3792   THEN
3793     -- Bug#5030613
3794     -- Replaced literals by bind variables.
3795     sql_wsacc := sql_wsacc ||
3796 		'start_stage_seq = :b_start_stage_seq, ' ||
3797 		'current_stage_seq = :b_current_stage_seq ' || ' ';
3798 
3799   ELSE
3800     sql_wsacc := sql_wsacc ||
3801     'current_stage_seq = decode(:p_current_stage_seq1, :gmn6 , current_stage_seq, :p_current_stage_seq2) ';
3802 
3803   END IF ;
3804 
3805   -- Bug#5030613
3806   -- Replaced literals by bind variables.
3807   sql_wsacc := sql_wsacc ||
3808 	      'where account_line_id = :b_account_line_id ' ;
3809 
3810   -- Bug#3132485: Replaced references to G_MISS_NUM by bind ones.
3811   /*
3812   -- num_wsacc := dsql_execute(sql_wsacc);
3813   if num_wsacc < 0 then
3814     raise FND_API.G_EXC_ERROR;
3815   end if;
3816   */
3817 
3818   -- Note there are 2 variations of the statement requiring different number
3819   -- of bind variables. The following condition causes the variations.
3820   IF (     FND_API.To_Boolean( p_check_stages )
3821        AND l_new_stage
3822        AND ( l_start_stage_seq < l_current_stage_seq )
3823      )
3824   THEN
3825     -- Bug 3663044: Add the condition for NOT l_distribute1_flag
3826     IF FND_API.to_Boolean(p_distribute_flag) and NOT l_distribute1_flag then
3827 
3828       -- Bug#5030613
3829       -- Supplied values for newly added bind variables.
3830       EXECUTE IMMEDIATE sql_wsacc
3831       USING p_service_package_id, FND_API.G_MISS_NUM,
3832       p_service_package_id, l_new_budget_group_id, p_copy_of_account_line_id, FND_API.G_MISS_NUM,
3833       p_copy_of_account_line_id, p_annual_fte, FND_API.G_MISS_NUM,
3834       FND_API.G_MISS_NUM, p_annual_fte, FND_API.G_MISS_NUM,
3835       SYSDATE, l_userid, l_loginid, l_current_stage_seq, l_current_stage_seq, p_account_line_id;
3836 
3837     ELSE
3838 
3839       -- Bug 3663044: Add binding l_prdamt_tbl
3840       -- Bug#5030613
3841       -- Supplied values for newly added bind variables.
3842       EXECUTE IMMEDIATE
3843 	sql_wsacc
3844       USING p_service_package_id, FND_API.G_MISS_NUM,
3845       p_service_package_id, l_new_budget_group_id, p_copy_of_account_line_id, FND_API.G_MISS_NUM,
3846       p_copy_of_account_line_id, p_annual_fte, FND_API.G_MISS_NUM,
3847       FND_API.G_MISS_NUM, p_annual_fte, FND_API.G_MISS_NUM,
3848       l_prdamt_tbl(1), l_prdamt_tbl(2), l_prdamt_tbl(3), l_prdamt_tbl(4),
3849       l_prdamt_tbl(5), l_prdamt_tbl(6), l_prdamt_tbl(7), l_prdamt_tbl(8),
3850       l_prdamt_tbl(9), l_prdamt_tbl(10), l_prdamt_tbl(11), l_prdamt_tbl(12),
3851       l_prdamt_tbl(13), l_prdamt_tbl(14), l_prdamt_tbl(15), l_prdamt_tbl(16),
3852       l_prdamt_tbl(17), l_prdamt_tbl(18), l_prdamt_tbl(19), l_prdamt_tbl(20),
3853       l_prdamt_tbl(21), l_prdamt_tbl(22), l_prdamt_tbl(23), l_prdamt_tbl(24),
3854       l_prdamt_tbl(25), l_prdamt_tbl(26), l_prdamt_tbl(27), l_prdamt_tbl(28),
3855       l_prdamt_tbl(29), l_prdamt_tbl(30), l_prdamt_tbl(31), l_prdamt_tbl(32),
3856       l_prdamt_tbl(33), l_prdamt_tbl(34), l_prdamt_tbl(35), l_prdamt_tbl(36),
3857       l_prdamt_tbl(37), l_prdamt_tbl(38), l_prdamt_tbl(39), l_prdamt_tbl(40),
3858       l_prdamt_tbl(41), l_prdamt_tbl(42), l_prdamt_tbl(43), l_prdamt_tbl(44),
3859       l_prdamt_tbl(45), l_prdamt_tbl(46), l_prdamt_tbl(47), l_prdamt_tbl(48),
3860       l_prdamt_tbl(49), l_prdamt_tbl(50), l_prdamt_tbl(51), l_prdamt_tbl(52),
3861       l_prdamt_tbl(53), l_prdamt_tbl(54), l_prdamt_tbl(55), l_prdamt_tbl(56),
3862       l_prdamt_tbl(57), l_prdamt_tbl(58), l_prdamt_tbl(59), l_prdamt_tbl(60),
3863       l_new_ytd_amount, SYSDATE, l_userid, l_loginid,
3864       l_current_stage_seq, l_current_stage_seq, p_account_line_id;
3865     END IF ;
3866   ELSE
3867 
3868     -- Bug 3663044: Add the condition for NOT l_distribute1_flag
3869     IF FND_API.to_Boolean(p_distribute_flag) and NOT l_distribute1_flag then
3870       -- Bug#5030613
3871       -- Supplied values for newly added bind variables.
3872       EXECUTE IMMEDIATE
3873         sql_wsacc
3874       USING p_service_package_id, FND_API.G_MISS_NUM,
3875       p_service_package_id, l_new_budget_group_id, p_copy_of_account_line_id, FND_API.G_MISS_NUM,
3876       p_copy_of_account_line_id, p_annual_fte, FND_API.G_MISS_NUM,
3877       FND_API.G_MISS_NUM, p_annual_fte, FND_API.G_MISS_NUM,
3878       SYSDATE, l_userid, l_loginid,
3879       p_current_stage_seq, FND_API.G_MISS_NUM, p_current_stage_seq, p_account_line_id ;
3880 
3881     ELSE
3882 
3883       -- Bug 3663044: Add binding l_prdamt_tbl
3884 
3885       -- Bug#5030613
3886       -- Supplied values for newly added bind variables.
3887       EXECUTE IMMEDIATE
3888         sql_wsacc
3889       USING p_service_package_id, FND_API.G_MISS_NUM,
3890       p_service_package_id, l_new_budget_group_id, p_copy_of_account_line_id, FND_API.G_MISS_NUM,
3891       p_copy_of_account_line_id, p_annual_fte, FND_API.G_MISS_NUM,
3892       FND_API.G_MISS_NUM, p_annual_fte, FND_API.G_MISS_NUM,
3893       l_prdamt_tbl(1), l_prdamt_tbl(2), l_prdamt_tbl(3), l_prdamt_tbl(4),
3894       l_prdamt_tbl(5), l_prdamt_tbl(6), l_prdamt_tbl(7), l_prdamt_tbl(8),
3895       l_prdamt_tbl(9), l_prdamt_tbl(10), l_prdamt_tbl(11), l_prdamt_tbl(12),
3896       l_prdamt_tbl(13), l_prdamt_tbl(14), l_prdamt_tbl(15), l_prdamt_tbl(16),
3897       l_prdamt_tbl(17), l_prdamt_tbl(18), l_prdamt_tbl(19), l_prdamt_tbl(20),
3898       l_prdamt_tbl(21), l_prdamt_tbl(22), l_prdamt_tbl(23), l_prdamt_tbl(24),
3899       l_prdamt_tbl(25), l_prdamt_tbl(26), l_prdamt_tbl(27), l_prdamt_tbl(28),
3900       l_prdamt_tbl(29), l_prdamt_tbl(30), l_prdamt_tbl(31), l_prdamt_tbl(32),
3901       l_prdamt_tbl(33), l_prdamt_tbl(34), l_prdamt_tbl(35), l_prdamt_tbl(36),
3902       l_prdamt_tbl(37), l_prdamt_tbl(38), l_prdamt_tbl(39), l_prdamt_tbl(40),
3903       l_prdamt_tbl(41), l_prdamt_tbl(42), l_prdamt_tbl(43), l_prdamt_tbl(44),
3904       l_prdamt_tbl(45), l_prdamt_tbl(46), l_prdamt_tbl(47), l_prdamt_tbl(48),
3905       l_prdamt_tbl(49), l_prdamt_tbl(50), l_prdamt_tbl(51), l_prdamt_tbl(52),
3906       l_prdamt_tbl(53), l_prdamt_tbl(54), l_prdamt_tbl(55), l_prdamt_tbl(56),
3907       l_prdamt_tbl(57), l_prdamt_tbl(58), l_prdamt_tbl(59), l_prdamt_tbl(60),
3908       l_new_ytd_amount, SYSDATE, l_userid, l_loginid,
3909       p_current_stage_seq, FND_API.G_MISS_NUM, p_current_stage_seq,
3910       p_account_line_id;
3911     END IF;
3912 
3913   END IF ;
3914 
3915   -- Update was successful; if budget group was changed reassign worksheets
3916   IF l_budget_group_changed
3917   THEN
3918     BEGIN
3919       DELETE FROM psb_ws_lines
3920        WHERE account_line_id = p_account_line_id;
3921 
3922       -- Create an entry in PSB_WS_LINES for all worksheets to which the CCID or Position belongs
3923 
3924       IF l_position_line_id IS NOT NULL
3925       THEN
3926         BEGIN
3927           INSERT INTO PSB_WS_LINES
3928           (worksheet_id, account_line_id, freeze_flag, view_line_flag,
3929            last_update_date, last_updated_by, last_update_login, created_by,
3930            creation_date)
3931           SELECT worksheet_id, p_account_line_id, freeze_flag, view_line_flag,
3932                  sysdate, l_userid,l_loginid, l_userid, sysdate
3933             FROM PSB_WS_LINES_POSITIONS
3934            WHERE position_line_id = l_position_line_id;
3935 
3936           /* Start bug #4167811 */
3937           UPDATE psb_ws_account_lines SET budget_group_changed = 'Y'
3938            WHERE account_line_id = p_account_line_id;
3939           /* End bug #4167811 */
3940         END;
3941 
3942       ELSE
3943         BEGIN
3944 
3945           -- Bug 3543845: During ws creation, distributed ws does not exist.
3946           -- Also flip the condition to make it readable.
3947           -- if nvl(l_local_copy_flag, 'N') <> 'Y' then
3948           IF PSB_WORKSHEET.g_ws_creation_flag OR l_local_copy_flag = 'Y'
3949           THEN
3950             BEGIN
3951 
3952               INSERT INTO PSB_WS_LINES
3953               (worksheet_id, account_line_id, freeze_flag, view_line_flag,
3954                last_update_date, last_updated_by, last_update_login,
3955                created_by, creation_date)
3956               VALUES (p_worksheet_id, p_account_line_id, null, 'Y', sysdate,
3957               l_userid, l_loginid, l_userid, sysdate);
3958             END;
3959 
3960           ELSE
3961             BEGIN
3962               FOR c_Distribute_WS_Rec IN c_Distribute_WS(l_global_worksheet_id,
3963                 p_budget_group_id, g_startdate_pp, g_enddate_cy)
3964               LOOP
3965                 INSERT INTO PSB_WS_LINES
3966                 (worksheet_id, account_line_id, freeze_flag, view_line_flag,
3967                  last_update_date, last_updated_by, last_update_login,
3968                  created_by, creation_date)
3969                 VALUES (c_Distribute_WS_Rec.worksheet_id, p_account_line_id,
3970                 null,'Y', sysdate, l_userid, l_loginid, l_userid, sysdate);
3971               END LOOP;
3972             END;
3973 
3974           END IF; -- End of : IF PSB_WORKSHEET.g_ws_creation_flag OR ...
3975         END;
3976 
3977       END IF; -- End of : IF l_position_line_id IS NOT NULL
3978     END;
3979 
3980   END IF; -- End of : IF l_budget_group_changed
3981 
3982   -- Initialize API return status to success
3983 
3984   p_return_status := FND_API.G_RET_STS_SUCCESS;
3985 
3986 
3987 EXCEPTION
3988 
3989    when FND_API.G_EXC_ERROR then
3990      p_return_status := FND_API.G_RET_STS_ERROR;
3991 
3992    when FND_API.G_EXC_UNEXPECTED_ERROR then
3993      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3994 
3995    when OTHERS then
3996      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3997 
3998      if dbms_sql.is_open(cur_wsacc) then
3999        dbms_sql.close_cursor(cur_wsacc);
4000      end if;
4001 
4002      if FND_MSG_PUB.Check_Msg_Level
4003        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4004      then
4005        FND_MSG_PUB.Add_Exc_Msg
4006 	  (p_pkg_name => G_PKG_NAME,
4007 	   p_procedure_name => l_api_name);
4008      end if;
4009 
4010 END Create_Account_Dist;
4011 
4012 /* ----------------------------------------------------------------------- */
4013 
4014 -- Copy Actual Balances for Current Year as Estimate Balances up to the
4015 -- GL Cutoff Period. This is needed because Actual and Estimate Balances
4016 -- are stored as separate entries in PSB_WS_ACCOUNT_LINES
4017 
4018 PROCEDURE Copy_CY_Estimates
4019 ( p_return_status       OUT  NOCOPY  VARCHAR2,
4020   p_worksheet_id        IN   NUMBER,
4021   p_service_package_id  IN   NUMBER,
4022   p_rounding_factor     IN   NUMBER,
4023   p_start_stage_seq     IN   NUMBER,
4024   p_budget_group_id     IN   NUMBER,
4025   p_stage_set_id        IN   NUMBER,
4026   p_budget_year_id      IN   NUMBER,
4027   p_ccid                IN   NUMBER,
4028   p_currency_code       IN   VARCHAR2
4029 ) IS
4030 
4031   l_return_status       VARCHAR2(1);
4032 
4033   l_init_index          PLS_INTEGER;
4034 
4035   l_account_line_id     NUMBER;
4036 
4037   l_ytd_amount          NUMBER := 0;
4038   l_period_amount       g_prdamt_tbl_type;
4039   -- Bug 3543845
4040   -- l_create_zero_bal     VARCHAR2(1);
4041 
4042   -- There are 2 separate cursors c_WAL_Act (for Actuals) and c_WAL_Est
4043   -- (for Estimates) to avoid static binding of cursors
4044 
4045   cursor c_WAL_Act is
4046     select ytd_amount,
4047 	   period1_amount, period2_amount, period3_amount, period4_amount,
4048 	   period5_amount, period6_amount, period7_amount, period8_amount,
4049 	   period9_amount, period10_amount, period11_amount, period12_amount,
4050 	   period13_amount, period14_amount, period15_amount, period16_amount,
4051 	   period17_amount, period18_amount, period19_amount, period20_amount,
4052 	   period21_amount, period22_amount, period23_amount, period24_amount,
4053 	   period25_amount, period26_amount, period27_amount, period28_amount,
4054 	   period29_amount, period30_amount, period31_amount, period32_amount,
4055 	   period33_amount, period34_amount, period35_amount, period36_amount,
4056 	   period37_amount, period38_amount, period39_amount, period40_amount,
4057 	   period41_amount, period42_amount, period43_amount, period44_amount,
4058 	   period45_amount, period46_amount, period47_amount, period48_amount,
4059 	   period49_amount, period50_amount, period51_amount, period52_amount,
4060 	   period53_amount, period54_amount, period55_amount, period56_amount,
4061 	   period57_amount, period58_amount, period59_amount, period60_amount
4062       from PSB_WS_ACCOUNT_LINES a
4063      where template_id is null
4064        and position_line_id is null
4065        and currency_code = p_currency_code
4066        and p_start_stage_seq between start_stage_seq and current_stage_seq
4067        and balance_type = 'A'
4068        and exists
4069 	  (select 1
4070 	     from PSB_WS_LINES b
4071 	    where b.account_line_id = a.account_line_id
4072 	      and b.worksheet_id = p_worksheet_id)
4073        and stage_set_id = p_stage_set_id
4074        and service_package_id = p_service_package_id
4075        and budget_year_id = p_budget_year_id
4076        and budget_group_id = p_budget_group_id
4077        and code_combination_id = p_ccid;
4078 
4079   cursor c_WAL_Est is
4080     select ytd_amount,
4081 	   period1_amount, period2_amount, period3_amount, period4_amount,
4082 	   period5_amount, period6_amount, period7_amount, period8_amount,
4083 	   period9_amount, period10_amount, period11_amount, period12_amount,
4084 	   period13_amount, period14_amount, period15_amount, period16_amount,
4085 	   period17_amount, period18_amount, period19_amount, period20_amount,
4086 	   period21_amount, period22_amount, period23_amount, period24_amount,
4087 	   period25_amount, period26_amount, period27_amount, period28_amount,
4088 	   period29_amount, period30_amount, period31_amount, period32_amount,
4089 	   period33_amount, period34_amount, period35_amount, period36_amount,
4090 	   period37_amount, period38_amount, period39_amount, period40_amount,
4091 	   period41_amount, period42_amount, period43_amount, period44_amount,
4092 	   period45_amount, period46_amount, period47_amount, period48_amount,
4093 	   period49_amount, period50_amount, period51_amount, period52_amount,
4094 	   period53_amount, period54_amount, period55_amount, period56_amount,
4095 	   period57_amount, period58_amount, period59_amount, period60_amount
4096       from PSB_WS_ACCOUNT_LINES a
4097      where template_id is null
4098        and position_line_id is null
4099        and currency_code = p_currency_code
4100        and p_start_stage_seq between start_stage_seq and current_stage_seq
4101        and balance_type = 'E'
4102        and exists
4103 	  (select 1
4104 	     from PSB_WS_LINES b
4105 	    where b.account_line_id = a.account_line_id
4106 	      and b.worksheet_id = p_worksheet_id)
4107        and stage_set_id = p_stage_set_id
4108        and service_package_id = p_service_package_id
4109        and budget_year_id = p_budget_year_id
4110        and budget_group_id = p_budget_group_id
4111        and code_combination_id = p_ccid;
4112 
4113 BEGIN
4114 
4115   for l_init_index in 1..g_max_num_amounts loop
4116     l_period_amount(l_init_index) := null;
4117   end loop;
4118 
4119   for c_WAL_Rec in c_WAL_Act loop
4120 
4121     l_ytd_amount := c_WAL_Rec.ytd_amount;
4122 
4123     if c_WAL_Rec.period1_amount is not null then
4124       l_period_amount(1) := c_WAL_Rec.period1_amount;
4125     end if;
4126 
4127     if c_WAL_Rec.period2_amount is not null then
4128       l_period_amount(2) := c_WAL_Rec.period2_amount;
4129     end if;
4130 
4131     if c_WAL_Rec.period3_amount is not null then
4132       l_period_amount(3) := c_WAL_Rec.period3_amount;
4133     end if;
4134 
4135     if c_WAL_Rec.period4_amount is not null then
4136       l_period_amount(4) := c_WAL_Rec.period4_amount;
4137     end if;
4138 
4139     if c_WAL_Rec.period5_amount is not null then
4140       l_period_amount(5) := c_WAL_Rec.period5_amount;
4141     end if;
4142 
4143     if c_WAL_Rec.period6_amount is not null then
4144       l_period_amount(6) := c_WAL_Rec.period6_amount;
4145     end if;
4146 
4147     if c_WAL_Rec.period7_amount is not null then
4148       l_period_amount(7) := c_WAL_Rec.period7_amount;
4149     end if;
4150 
4151     if c_WAL_Rec.period8_amount is not null then
4152       l_period_amount(8) := c_WAL_Rec.period8_amount;
4153     end if;
4154 
4155     if c_WAL_Rec.period9_amount is not null then
4156       l_period_amount(9) := c_WAL_Rec.period9_amount;
4157     end if;
4158 
4159     if c_WAL_Rec.period10_amount is not null then
4160       l_period_amount(10) := c_WAL_Rec.period10_amount;
4161     end if;
4162 
4163     if c_WAL_Rec.period11_amount is not null then
4164       l_period_amount(11) := c_WAL_Rec.period11_amount;
4165     end if;
4166 
4167     if c_WAL_Rec.period12_amount is not null then
4168       l_period_amount(12) := c_WAL_Rec.period12_amount;
4169     end if;
4170 
4171     if c_WAL_Rec.period13_amount is not null then
4172       l_period_amount(13) := c_WAL_Rec.period13_amount;
4173     end if;
4174 
4175     if c_WAL_Rec.period14_amount is not null then
4176       l_period_amount(14) := c_WAL_Rec.period14_amount;
4177     end if;
4178 
4179     if c_WAL_Rec.period15_amount is not null then
4180       l_period_amount(15) := c_WAL_Rec.period15_amount;
4181     end if;
4182 
4183     if c_WAL_Rec.period16_amount is not null then
4184       l_period_amount(16) := c_WAL_Rec.period16_amount;
4185     end if;
4186 
4187     if c_WAL_Rec.period17_amount is not null then
4188       l_period_amount(17) := c_WAL_Rec.period17_amount;
4189     end if;
4190 
4191     if c_WAL_Rec.period18_amount is not null then
4192       l_period_amount(18) := c_WAL_Rec.period18_amount;
4193     end if;
4194 
4195     if c_WAL_Rec.period19_amount is not null then
4196       l_period_amount(19) := c_WAL_Rec.period19_amount;
4197     end if;
4198 
4199     if c_WAL_Rec.period20_amount is not null then
4200       l_period_amount(20) := c_WAL_Rec.period20_amount;
4201     end if;
4202 
4203     if c_WAL_Rec.period21_amount is not null then
4204       l_period_amount(21) := c_WAL_Rec.period21_amount;
4205     end if;
4206 
4207     if c_WAL_Rec.period22_amount is not null then
4208       l_period_amount(22) := c_WAL_Rec.period22_amount;
4209     end if;
4210 
4211     if c_WAL_Rec.period23_amount is not null then
4212       l_period_amount(23) := c_WAL_Rec.period23_amount;
4213     end if;
4214 
4215     if c_WAL_Rec.period24_amount is not null then
4216       l_period_amount(24) := c_WAL_Rec.period24_amount;
4217     end if;
4218 
4219     if c_WAL_Rec.period25_amount is not null then
4220       l_period_amount(25) := c_WAL_Rec.period25_amount;
4221     end if;
4222 
4223     if c_WAL_Rec.period26_amount is not null then
4224       l_period_amount(26) := c_WAL_Rec.period26_amount;
4225     end if;
4226 
4227     if c_WAL_Rec.period27_amount is not null then
4228       l_period_amount(27) := c_WAL_Rec.period27_amount;
4229     end if;
4230 
4231     if c_WAL_Rec.period28_amount is not null then
4232       l_period_amount(28) := c_WAL_Rec.period28_amount;
4233     end if;
4234 
4235     if c_WAL_Rec.period29_amount is not null then
4236       l_period_amount(29) := c_WAL_Rec.period29_amount;
4237     end if;
4238 
4239     if c_WAL_Rec.period30_amount is not null then
4240       l_period_amount(30) := c_WAL_Rec.period30_amount;
4241     end if;
4242 
4243     if c_WAL_Rec.period31_amount is not null then
4244       l_period_amount(31) := c_WAL_Rec.period31_amount;
4245     end if;
4246 
4247     if c_WAL_Rec.period32_amount is not null then
4248       l_period_amount(32) := c_WAL_Rec.period32_amount;
4249     end if;
4250 
4251     if c_WAL_Rec.period33_amount is not null then
4252       l_period_amount(33) := c_WAL_Rec.period33_amount;
4253     end if;
4254 
4255     if c_WAL_Rec.period34_amount is not null then
4256       l_period_amount(34) := c_WAL_Rec.period34_amount;
4257     end if;
4258 
4259     if c_WAL_Rec.period35_amount is not null then
4260       l_period_amount(35) := c_WAL_Rec.period35_amount;
4261     end if;
4262 
4263     if c_WAL_Rec.period36_amount is not null then
4264       l_period_amount(36) := c_WAL_Rec.period36_amount;
4265     end if;
4266 
4267     if c_WAL_Rec.period37_amount is not null then
4268       l_period_amount(37) := c_WAL_Rec.period37_amount;
4269     end if;
4270 
4271     if c_WAL_Rec.period38_amount is not null then
4272       l_period_amount(38) := c_WAL_Rec.period38_amount;
4273     end if;
4274 
4275     if c_WAL_Rec.period39_amount is not null then
4276       l_period_amount(39) := c_WAL_Rec.period39_amount;
4277     end if;
4278 
4279     if c_WAL_Rec.period40_amount is not null then
4280       l_period_amount(40) := c_WAL_Rec.period40_amount;
4281     end if;
4282 
4283     if c_WAL_Rec.period41_amount is not null then
4284       l_period_amount(41) := c_WAL_Rec.period41_amount;
4285     end if;
4286 
4287     if c_WAL_Rec.period42_amount is not null then
4288       l_period_amount(42) := c_WAL_Rec.period42_amount;
4289     end if;
4290 
4291     if c_WAL_Rec.period43_amount is not null then
4292       l_period_amount(43) := c_WAL_Rec.period43_amount;
4293     end if;
4294 
4295     if c_WAL_Rec.period44_amount is not null then
4296       l_period_amount(44) := c_WAL_Rec.period44_amount;
4297     end if;
4298 
4299     if c_WAL_Rec.period45_amount is not null then
4300       l_period_amount(45) := c_WAL_Rec.period45_amount;
4301     end if;
4302 
4303     if c_WAL_Rec.period46_amount is not null then
4304       l_period_amount(46) := c_WAL_Rec.period46_amount;
4305     end if;
4306 
4307     if c_WAL_Rec.period47_amount is not null then
4308       l_period_amount(47) := c_WAL_Rec.period47_amount;
4309     end if;
4310 
4311     if c_WAL_Rec.period48_amount is not null then
4312       l_period_amount(48) := c_WAL_Rec.period48_amount;
4313     end if;
4314 
4315     if c_WAL_Rec.period49_amount is not null then
4316       l_period_amount(49) := c_WAL_Rec.period49_amount;
4317     end if;
4318 
4319     if c_WAL_Rec.period50_amount is not null then
4320       l_period_amount(50) := c_WAL_Rec.period50_amount;
4321     end if;
4322 
4323     if c_WAL_Rec.period51_amount is not null then
4324       l_period_amount(51) := c_WAL_Rec.period51_amount;
4325     end if;
4326 
4327     if c_WAL_Rec.period52_amount is not null then
4328       l_period_amount(52) := c_WAL_Rec.period52_amount;
4329     end if;
4330 
4331     if c_WAL_Rec.period53_amount is not null then
4332       l_period_amount(53) := c_WAL_Rec.period53_amount;
4333     end if;
4334 
4335     if c_WAL_Rec.period54_amount is not null then
4336       l_period_amount(54) := c_WAL_Rec.period54_amount;
4337     end if;
4338 
4339     if c_WAL_Rec.period55_amount is not null then
4340       l_period_amount(55) := c_WAL_Rec.period55_amount;
4341     end if;
4342 
4343     if c_WAL_Rec.period56_amount is not null then
4344       l_period_amount(56) := c_WAL_Rec.period56_amount;
4345     end if;
4346 
4347     if c_WAL_Rec.period57_amount is not null then
4348       l_period_amount(57) := c_WAL_Rec.period57_amount;
4349     end if;
4350 
4351     if c_WAL_Rec.period58_amount is not null then
4352       l_period_amount(58) := c_WAL_Rec.period58_amount;
4353     end if;
4354 
4355     if c_WAL_Rec.period59_amount is not null then
4356       l_period_amount(59) := c_WAL_Rec.period59_amount;
4357     end if;
4358 
4359     if c_WAL_Rec.period60_amount is not null then
4360       l_period_amount(60) := c_WAL_Rec.period60_amount;
4361     end if;
4362 
4363     exit;
4364 
4365   end loop;
4366 
4367   -- Added the following condition as part of bug fix 3469514
4368   IF g_enddate_cy <> NVL(PSB_WORKSHEET.g_gl_cutoff_period,FND_API.G_MISS_DATE) THEN
4369 
4370   for c_WAL_Rec in c_WAL_Est loop
4371 
4372     l_ytd_amount := l_ytd_amount + c_WAL_Rec.ytd_amount;
4373 
4374     if ((c_WAL_Rec.period1_amount is not null) and
4375 	(c_WAL_Rec.period1_amount <> 0)) then
4376       l_period_amount(1) := c_WAL_Rec.period1_amount;
4377     end if;
4378 
4379     if ((c_WAL_Rec.period2_amount is not null) and
4380 	(c_WAL_Rec.period2_amount <> 0)) then
4381       l_period_amount(2) := c_WAL_Rec.period2_amount;
4382     end if;
4383 
4384     if ((c_WAL_Rec.period3_amount is not null) and
4385 	(c_WAL_Rec.period3_amount <> 0)) then
4386       l_period_amount(3) := c_WAL_Rec.period3_amount;
4387     end if;
4388 
4389     if ((c_WAL_Rec.period4_amount is not null) and
4390 	(c_WAL_Rec.period4_amount <> 0)) then
4391       l_period_amount(4) := c_WAL_Rec.period4_amount;
4392     end if;
4393 
4394     if ((c_WAL_Rec.period5_amount is not null) and
4395 	(c_WAL_Rec.period5_amount <> 0)) then
4396       l_period_amount(5) := c_WAL_Rec.period5_amount;
4397     end if;
4398 
4399     if ((c_WAL_Rec.period6_amount is not null) and
4400 	(c_WAL_Rec.period6_amount <> 0)) then
4401       l_period_amount(6) := c_WAL_Rec.period6_amount;
4402     end if;
4403 
4404     if ((c_WAL_Rec.period7_amount is not null) and
4405 	(c_WAL_Rec.period7_amount <> 0)) then
4406       l_period_amount(7) := c_WAL_Rec.period7_amount;
4407     end if;
4408 
4409     if ((c_WAL_Rec.period8_amount is not null) and
4410 	(c_WAL_Rec.period8_amount <> 0)) then
4411       l_period_amount(8) := c_WAL_Rec.period8_amount;
4412     end if;
4413 
4414     if ((c_WAL_Rec.period9_amount is not null) and
4415 	(c_WAL_Rec.period9_amount <> 0)) then
4416       l_period_amount(9) := c_WAL_Rec.period9_amount;
4417     end if;
4418 
4419     if ((c_WAL_Rec.period10_amount is not null) and
4420 	(c_WAL_Rec.period10_amount <> 0)) then
4421       l_period_amount(10) := c_WAL_Rec.period10_amount;
4422     end if;
4423 
4424     if ((c_WAL_Rec.period11_amount is not null) and
4425 	(c_WAL_Rec.period11_amount <> 0)) then
4426       l_period_amount(11) := c_WAL_Rec.period11_amount;
4427     end if;
4428 
4429     if ((c_WAL_Rec.period12_amount is not null) and
4430 	(c_WAL_Rec.period12_amount <> 0)) then
4431       l_period_amount(12) := c_WAL_Rec.period12_amount;
4432     end if;
4433 
4434     if ((c_WAL_Rec.period13_amount is not null) and
4435 	(c_WAL_Rec.period13_amount <> 0)) then
4436       l_period_amount(13) := c_WAL_Rec.period13_amount;
4437     end if;
4438 
4439     if ((c_WAL_Rec.period14_amount is not null) and
4440 	(c_WAL_Rec.period14_amount <> 0)) then
4441       l_period_amount(14) := c_WAL_Rec.period14_amount;
4442     end if;
4443 
4444     if ((c_WAL_Rec.period15_amount is not null) and
4445 	(c_WAL_Rec.period15_amount <> 0)) then
4446       l_period_amount(15) := c_WAL_Rec.period15_amount;
4447     end if;
4448 
4449     if ((c_WAL_Rec.period16_amount is not null) and
4450 	(c_WAL_Rec.period16_amount <> 0)) then
4451       l_period_amount(16) := c_WAL_Rec.period16_amount;
4452     end if;
4453 
4454     if ((c_WAL_Rec.period17_amount is not null) and
4455 	(c_WAL_Rec.period17_amount <> 0)) then
4456       l_period_amount(17) := c_WAL_Rec.period17_amount;
4457     end if;
4458 
4459     if ((c_WAL_Rec.period18_amount is not null) and
4460 	(c_WAL_Rec.period18_amount <> 0)) then
4461       l_period_amount(18) := c_WAL_Rec.period18_amount;
4462     end if;
4463 
4464     if ((c_WAL_Rec.period19_amount is not null) and
4465 	(c_WAL_Rec.period19_amount <> 0)) then
4466       l_period_amount(19) := c_WAL_Rec.period19_amount;
4467     end if;
4468 
4469     if ((c_WAL_Rec.period20_amount is not null) and
4470 	(c_WAL_Rec.period20_amount <> 0)) then
4471       l_period_amount(20) := c_WAL_Rec.period20_amount;
4472     end if;
4473 
4474     if ((c_WAL_Rec.period21_amount is not null) and
4475 	(c_WAL_Rec.period21_amount <> 0)) then
4476       l_period_amount(21) := c_WAL_Rec.period21_amount;
4477     end if;
4478 
4479     if ((c_WAL_Rec.period22_amount is not null) and
4480 	(c_WAL_Rec.period22_amount <> 0)) then
4481       l_period_amount(22) := c_WAL_Rec.period22_amount;
4482     end if;
4483 
4484     if ((c_WAL_Rec.period23_amount is not null) and
4485 	(c_WAL_Rec.period23_amount <> 0)) then
4486       l_period_amount(23) := c_WAL_Rec.period23_amount;
4487     end if;
4488 
4489     if ((c_WAL_Rec.period24_amount is not null) and
4490 	(c_WAL_Rec.period24_amount <> 0)) then
4491       l_period_amount(24) := c_WAL_Rec.period24_amount;
4492     end if;
4493 
4494     if ((c_WAL_Rec.period25_amount is not null) and
4495 	(c_WAL_Rec.period25_amount <> 0)) then
4496       l_period_amount(25) := c_WAL_Rec.period25_amount;
4497     end if;
4498 
4499     if ((c_WAL_Rec.period26_amount is not null) and
4500 	(c_WAL_Rec.period26_amount <> 0)) then
4501       l_period_amount(26) := c_WAL_Rec.period26_amount;
4502     end if;
4503 
4504     if ((c_WAL_Rec.period27_amount is not null) and
4505 	(c_WAL_Rec.period27_amount <> 0)) then
4506       l_period_amount(27) := c_WAL_Rec.period27_amount;
4507     end if;
4508 
4509     if ((c_WAL_Rec.period28_amount is not null) and
4510 	(c_WAL_Rec.period28_amount <> 0)) then
4511       l_period_amount(28) := c_WAL_Rec.period28_amount;
4512     end if;
4513 
4514     if ((c_WAL_Rec.period29_amount is not null) and
4515 	(c_WAL_Rec.period29_amount <> 0)) then
4516       l_period_amount(29) := c_WAL_Rec.period29_amount;
4517     end if;
4518 
4519     if ((c_WAL_Rec.period30_amount is not null) and
4520 	(c_WAL_Rec.period30_amount <> 0)) then
4521       l_period_amount(30) := c_WAL_Rec.period30_amount;
4522     end if;
4523 
4524     if ((c_WAL_Rec.period31_amount is not null) and
4525 	(c_WAL_Rec.period31_amount <> 0)) then
4526       l_period_amount(31) := c_WAL_Rec.period31_amount;
4527     end if;
4528 
4529     if ((c_WAL_Rec.period32_amount is not null) and
4530 	(c_WAL_Rec.period32_amount <> 0)) then
4531       l_period_amount(32) := c_WAL_Rec.period32_amount;
4532     end if;
4533 
4534     if ((c_WAL_Rec.period33_amount is not null) and
4535 	(c_WAL_Rec.period33_amount <> 0)) then
4536       l_period_amount(33) := c_WAL_Rec.period33_amount;
4537     end if;
4538 
4539     if ((c_WAL_Rec.period34_amount is not null) and
4540 	(c_WAL_Rec.period34_amount <> 0)) then
4541       l_period_amount(34) := c_WAL_Rec.period34_amount;
4542     end if;
4543 
4544     if ((c_WAL_Rec.period35_amount is not null) and
4545 	(c_WAL_Rec.period35_amount <> 0)) then
4546       l_period_amount(35) := c_WAL_Rec.period35_amount;
4547     end if;
4548 
4549     if ((c_WAL_Rec.period36_amount is not null) and
4550 	(c_WAL_Rec.period36_amount <> 0)) then
4551       l_period_amount(36) := c_WAL_Rec.period36_amount;
4552     end if;
4553 
4554     if ((c_WAL_Rec.period37_amount is not null) and
4555 	(c_WAL_Rec.period37_amount <> 0)) then
4556       l_period_amount(37) := c_WAL_Rec.period37_amount;
4557     end if;
4558 
4559     if ((c_WAL_Rec.period38_amount is not null) and
4560 	(c_WAL_Rec.period38_amount <> 0)) then
4561       l_period_amount(38) := c_WAL_Rec.period38_amount;
4562     end if;
4563 
4564     if ((c_WAL_Rec.period39_amount is not null) and
4565 	(c_WAL_Rec.period39_amount <> 0)) then
4566       l_period_amount(39) := c_WAL_Rec.period39_amount;
4567     end if;
4568 
4569     if ((c_WAL_Rec.period40_amount is not null) and
4570 	(c_WAL_Rec.period40_amount <> 0)) then
4571       l_period_amount(40) := c_WAL_Rec.period40_amount;
4572     end if;
4573 
4574     if ((c_WAL_Rec.period41_amount is not null) and
4575 	(c_WAL_Rec.period41_amount <> 0)) then
4576       l_period_amount(41) := c_WAL_Rec.period41_amount;
4577     end if;
4578 
4579     if ((c_WAL_Rec.period42_amount is not null) and
4580 	(c_WAL_Rec.period42_amount <> 0)) then
4581       l_period_amount(42) := c_WAL_Rec.period42_amount;
4582     end if;
4583 
4584     if ((c_WAL_Rec.period43_amount is not null) and
4585 	(c_WAL_Rec.period43_amount <> 0)) then
4586       l_period_amount(43) := c_WAL_Rec.period43_amount;
4587     end if;
4588 
4589     if ((c_WAL_Rec.period44_amount is not null) and
4590 	(c_WAL_Rec.period44_amount <> 0)) then
4591       l_period_amount(44) := c_WAL_Rec.period44_amount;
4592     end if;
4593 
4594     if ((c_WAL_Rec.period45_amount is not null) and
4595 	(c_WAL_Rec.period45_amount <> 0)) then
4596       l_period_amount(45) := c_WAL_Rec.period45_amount;
4597     end if;
4598 
4599     if ((c_WAL_Rec.period46_amount is not null) and
4600 	(c_WAL_Rec.period46_amount <> 0)) then
4601       l_period_amount(46) := c_WAL_Rec.period46_amount;
4602     end if;
4603 
4604     if ((c_WAL_Rec.period47_amount is not null) and
4605 	(c_WAL_Rec.period47_amount <> 0)) then
4606       l_period_amount(47) := c_WAL_Rec.period47_amount;
4607     end if;
4608 
4609     if ((c_WAL_Rec.period48_amount is not null) and
4610 	(c_WAL_Rec.period48_amount <> 0)) then
4611       l_period_amount(48) := c_WAL_Rec.period48_amount;
4612     end if;
4613 
4614     if ((c_WAL_Rec.period49_amount is not null) and
4615 	(c_WAL_Rec.period49_amount <> 0)) then
4616       l_period_amount(49) := c_WAL_Rec.period49_amount;
4617     end if;
4618 
4619     if ((c_WAL_Rec.period50_amount is not null) and
4620 	(c_WAL_Rec.period50_amount <> 0)) then
4621       l_period_amount(50) := c_WAL_Rec.period50_amount;
4622     end if;
4623 
4624     if ((c_WAL_Rec.period51_amount is not null) and
4625 	(c_WAL_Rec.period51_amount <> 0)) then
4626       l_period_amount(51) := c_WAL_Rec.period51_amount;
4627     end if;
4628 
4629     if ((c_WAL_Rec.period52_amount is not null) and
4630 	(c_WAL_Rec.period52_amount <> 0)) then
4631       l_period_amount(52) := c_WAL_Rec.period52_amount;
4632     end if;
4633 
4634     if ((c_WAL_Rec.period53_amount is not null) and
4635 	(c_WAL_Rec.period53_amount <> 0)) then
4636       l_period_amount(53) := c_WAL_Rec.period53_amount;
4637     end if;
4638 
4639     if ((c_WAL_Rec.period54_amount is not null) and
4640 	(c_WAL_Rec.period54_amount <> 0)) then
4641       l_period_amount(54) := c_WAL_Rec.period54_amount;
4642     end if;
4643 
4644     if ((c_WAL_Rec.period55_amount is not null) and
4645 	(c_WAL_Rec.period55_amount <> 0)) then
4646       l_period_amount(55) := c_WAL_Rec.period55_amount;
4647     end if;
4648 
4649     if ((c_WAL_Rec.period56_amount is not null) and
4650 	(c_WAL_Rec.period56_amount <> 0)) then
4651       l_period_amount(56) := c_WAL_Rec.period56_amount;
4652     end if;
4653 
4654     if ((c_WAL_Rec.period57_amount is not null) and
4655 	(c_WAL_Rec.period57_amount <> 0)) then
4656       l_period_amount(57) := c_WAL_Rec.period57_amount;
4657     end if;
4658 
4659     if ((c_WAL_Rec.period58_amount is not null) and
4660 	(c_WAL_Rec.period58_amount <> 0)) then
4661       l_period_amount(58) := c_WAL_Rec.period58_amount;
4662     end if;
4663 
4664     if ((c_WAL_Rec.period59_amount is not null) and
4665 	(c_WAL_Rec.period59_amount <> 0)) then
4666       l_period_amount(59) := c_WAL_Rec.period59_amount;
4667     end if;
4668 
4669     if ((c_WAL_Rec.period60_amount is not null) and
4670 	(c_WAL_Rec.period60_amount <> 0)) then
4671       l_period_amount(60) := c_WAL_Rec.period60_amount;
4672     end if;
4673 
4674     exit;
4675 
4676   end loop;
4677   END IF;
4678 
4679   -- Create Zero Balances Profile Option : this specifies whether non-Position
4680   -- CCIDs with zero YTD Amounts should be created in PSB_WS_ACCOUNT_LINES
4681 
4682   --commented the following statements for bug 3305778
4683   /* Bug 3543845: Reactivate the following statements, use g_create_zero_bal
4684      and add a new condition to improve performance
4685   */
4686   IF g_create_zero_bal is null THEN
4687 
4688     FND_PROFILE.GET
4689       (name => 'PSB_CREATE_ZERO_BALANCE_ACCT',
4690        val => g_create_zero_bal);
4691 
4692     if g_create_zero_bal is null then
4693       -- Bug 3543845: Change default behavior to not creating zero balance
4694       g_create_zero_bal := 'N';
4695     end if;
4696   END IF;
4697 
4698   -- Bug 3543845: Check whether the worksheet creation process is executed for
4699   -- the first time. If it is the first time, then check the ytd_amount and
4700   -- create zero balance profile. Otherwise, call the create_account_Dist
4701   -- without any filtering.
4702   IF ( PSB_WORKSHEET.g_ws_first_time_creation_flag
4703        and
4704          (  l_ytd_amount <> 0
4705             OR
4706             PSB_WS_ACCT2.g_running_total <> 0  --bug 3704360. added this clause.
4707             OR
4708           ( l_ytd_amount = 0 and g_create_zero_bal = 'Y' )
4709          )
4710      )
4711      OR
4712      NOT PSB_WORKSHEET.g_ws_first_time_creation_flag
4713   THEN
4714   /*  comment out by bug 3305778
4715   if ((l_ytd_amount <> 0) or
4716      ((l_ytd_amount = 0) and (l_create_zero_bal = 'Y'))) then */
4717 
4718   begin
4719 
4720     --pd('4: Call Create_Account_Dist=> ccid=' || TO_CHAR(p_ccid) ||
4721     --   ', p_budget_year_id=' || TO_CHAR(p_budget_year_id) ||
4722     --   ', p_ytd_amount=' || TO_CHAR(l_ytd_amount));
4723 
4724     Create_Account_Dist
4725        (p_api_version => 1.0,
4726 	p_return_status => l_return_status,
4727 	p_account_line_id => l_account_line_id,
4728 	p_worksheet_id => p_worksheet_id,
4729 	p_service_package_id => p_service_package_id,
4730 	p_check_spal_exists => FND_API.G_FALSE,
4731 	p_gl_cutoff_period => null,
4732 	p_allocrule_set_id => null,
4733 	p_budget_calendar_id => null,
4734 	p_rounding_factor => p_rounding_factor,
4735 	p_stage_set_id => p_stage_set_id,
4736 	p_budget_year_id => p_budget_year_id,
4737 	p_budget_group_id => p_budget_group_id,
4738 	p_ccid => p_ccid,
4739 	p_currency_code => p_currency_code,
4740 	p_balance_type => 'E',
4741 	p_ytd_amount => l_ytd_amount,
4742 	p_period_amount => l_period_amount,
4743 	p_start_stage_seq => p_start_stage_seq,
4744 	p_current_stage_seq => p_start_stage_seq);
4745 
4746     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4747       raise FND_API.G_EXC_ERROR;
4748     end if;
4749 
4750   end;
4751   end if;
4752 
4753   -- Initialize API return status to success
4754 
4755   p_return_status := FND_API.G_RET_STS_SUCCESS;
4756 
4757 
4758 EXCEPTION
4759 
4760    when FND_API.G_EXC_ERROR then
4761      p_return_status := FND_API.G_RET_STS_ERROR;
4762 
4763    when FND_API.G_EXC_UNEXPECTED_ERROR then
4764      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4765 
4766    when OTHERS then
4767      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4768      if FND_MSG_PUB.Check_Msg_Level
4769        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4770      then
4771        FND_MSG_PUB.Add_Exc_Msg
4772 	  (p_pkg_name => G_PKG_NAME,
4773 	   p_procedure_name => 'Copy_CY_Estimates');
4774      end if;
4775 
4776 END Copy_CY_Estimates;
4777 
4778 /* ----------------------------------------------------------------------- */
4779 
4780 PROCEDURE Update_YTD_Amount
4781 ( p_api_version       IN   NUMBER,
4782   p_validation_level  IN   NUMBER := FND_API.G_VALID_LEVEL_NONE,
4783   p_return_status     OUT  NOCOPY  VARCHAR2,
4784   p_account_line_id   IN   NUMBER
4785 ) IS
4786 
4787   l_api_name          CONSTANT VARCHAR2(30)     := 'Update_YTD_Amount';
4788   l_api_version       CONSTANT NUMBER           := 1.0;
4789 
4790 BEGIN
4791 
4792   -- Standard call to check for call compatibility
4793 
4794   if not FND_API.Compatible_API_Call (l_api_version,
4795 				      p_api_version,
4796 				      l_api_name,
4797 				      G_PKG_NAME)
4798   then
4799     raise FND_API.G_EXC_UNEXPECTED_ERROR;
4800   end if;
4801 
4802   update PSB_WS_ACCOUNT_LINES
4803      set ytd_amount = nvl(period1_amount, 0) + nvl(period2_amount, 0) + nvl(period3_amount, 0) +
4804 		      nvl(period4_amount, 0) + nvl(period5_amount, 0) + nvl(period6_amount, 0) +
4805 		      nvl(period7_amount, 0) + nvl(period8_amount, 0) + nvl(period9_amount, 0) +
4806 		      nvl(period10_amount, 0) + nvl(period11_amount, 0) + nvl(period12_amount, 0) +
4807 		      nvl(period13_amount, 0) + nvl(period14_amount, 0) + nvl(period15_amount, 0) +
4808 		      nvl(period16_amount, 0) + nvl(period17_amount, 0) + nvl(period18_amount, 0) +
4809 		      nvl(period19_amount, 0) + nvl(period20_amount, 0) + nvl(period21_amount, 0) +
4810 		      nvl(period22_amount, 0) + nvl(period23_amount, 0) + nvl(period24_amount, 0) +
4811 		      nvl(period25_amount, 0) + nvl(period26_amount, 0) + nvl(period27_amount, 0) +
4812 		      nvl(period28_amount, 0) + nvl(period29_amount, 0) + nvl(period30_amount, 0) +
4813 		      nvl(period31_amount, 0) + nvl(period32_amount, 0) + nvl(period33_amount, 0) +
4814 		      nvl(period34_amount, 0) + nvl(period35_amount, 0) + nvl(period36_amount, 0) +
4815 		      nvl(period37_amount, 0) + nvl(period38_amount, 0) + nvl(period39_amount, 0) +
4816 		      nvl(period40_amount, 0) + nvl(period41_amount, 0) + nvl(period42_amount, 0) +
4817 		      nvl(period43_amount, 0) + nvl(period44_amount, 0) + nvl(period45_amount, 0) +
4818 		      nvl(period46_amount, 0) + nvl(period47_amount, 0) + nvl(period48_amount, 0) +
4819 		      nvl(period49_amount, 0) + nvl(period50_amount, 0) + nvl(period51_amount, 0) +
4820 		      nvl(period52_amount, 0) + nvl(period53_amount, 0) + nvl(period54_amount, 0) +
4821 		      nvl(period55_amount, 0) + nvl(period56_amount, 0) + nvl(period57_amount, 0) +
4822 		      nvl(period58_amount, 0) + nvl(period59_amount, 0) + nvl(period60_amount, 0)
4823    where account_line_id = p_account_line_id;
4824 
4825 
4826   -- Initialize API return status to success
4827 
4828   p_return_status := FND_API.G_RET_STS_SUCCESS;
4829 
4830 
4831 EXCEPTION
4832 
4833    when FND_API.G_EXC_ERROR then
4834      p_return_status := FND_API.G_RET_STS_ERROR;
4835 
4836    when FND_API.G_EXC_UNEXPECTED_ERROR then
4837      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4838 
4839    when OTHERS then
4840      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4841 
4842      if FND_MSG_PUB.Check_Msg_Level
4843        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4844      then
4845        FND_MSG_PUB.Add_Exc_Msg
4846 	  (p_pkg_name => G_PKG_NAME,
4847 	   p_procedure_name => l_api_name);
4848      end if;
4849 
4850 END Update_YTD_Amount;
4851 
4852 /* ----------------------------------------------------------------------- */
4853 
4854 -- Cache AFF Column Names for the Chart of Accounts
4855 
4856 PROCEDURE Flex_Info
4857 ( p_return_status  OUT  NOCOPY  VARCHAR2,
4858   p_flex_code      IN   NUMBER
4859 ) IS
4860 
4861   cursor c_seginfo is
4862     select application_column_name
4863       from fnd_id_flex_segments
4864      where application_id = 101
4865        and id_flex_code = 'GL#'
4866        and id_flex_num = p_flex_code
4867        and enabled_flag = 'Y'
4868      order by segment_num;
4869 
4870 BEGIN
4871 
4872   for l_init_index in 1..g_seg_name.Count loop
4873     g_seg_name(l_init_index) := null;
4874   end loop;
4875 
4876   g_num_segs := 0;
4877 
4878   g_flex_code := p_flex_code;
4879 
4880   for c_Seginfo_Rec in c_seginfo loop
4881     g_num_segs := g_num_segs + 1;
4882     g_seg_name(g_num_segs) := c_Seginfo_Rec.application_column_name;
4883   end loop;
4884 
4885   p_return_status := FND_API.G_RET_STS_SUCCESS;
4886 
4887 
4888 EXCEPTION
4889 
4890    when FND_API.G_EXC_ERROR then
4891      p_return_status := FND_API.G_RET_STS_ERROR;
4892 
4893    when FND_API.G_EXC_UNEXPECTED_ERROR then
4894      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4895 
4896    when OTHERS then
4897      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4898      if FND_MSG_PUB.Check_Msg_Level
4899        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4900      then
4901        FND_MSG_PUB.Add_Exc_Msg
4902 	  (p_pkg_name => G_PKG_NAME,
4903 	   p_procedure_name => 'Flex_Info');
4904      end if;
4905 
4906 END Flex_Info;
4907 
4908 /* ------------------------------------------------------------------------- */
4909 
4910   -- Execute a Dynamic SQL Statement with no Bind Variables
4911 
4912   -- Returns number of rows processed or -1 if error (add_message)
4913   -- Return Value is valid only for insert, update and delete statements
4914 
4915 FUNCTION dsql_execute
4916 ( sql_statement  IN  VARCHAR2
4917 ) RETURN NUMBER IS
4918 
4919   cursornum   INTEGER;
4920   nprocessed  INTEGER;
4921 
4922 BEGIN
4923 
4924   cursornum := dbms_sql.open_cursor;
4925   dbms_sql.parse(cursornum, sql_statement, dbms_sql.v7);
4926   nprocessed := dbms_sql.execute(cursornum);
4927   dbms_sql.close_cursor(cursornum);
4928   return(nprocessed);
4929 
4930 
4931 EXCEPTION
4932 
4933   when OTHERS then
4934 
4935     if dbms_sql.is_open(cursornum) then
4936       dbms_sql.close_cursor(cursornum);
4937     end if;
4938 
4939     -- Dynamic SQL Exception
4940 
4941     message_token('ROUTINE', 'PSB_WS_ACCT1.dsql_execute');
4942     message_token('ERROR', SQLERRM);
4943     add_message('PSB', 'PSB_UNHANDLED_EXCEPTION');
4944 
4945     return(-1);
4946 
4947 END dsql_execute;
4948 
4949 /* ------------------------------------------------------------------------- */
4950 
4951 PROCEDURE DSQL_Budget_Balance
4952 ( p_return_status      OUT  NOCOPY  VARCHAR2,
4953   p_sql_statement      OUT  NOCOPY  VARCHAR2,
4954   p_set_of_books_id    IN   NUMBER,
4955   p_budgetary_control  IN   VARCHAR2,
4956   p_budget_version_id  IN   NUMBER,
4957   p_gl_budget_set_id   IN   NUMBER,
4958   p_incl_adj_period    IN   VARCHAR2,
4959   p_map_criteria       IN   VARCHAR2
4960 ) IS
4961 
4962   sql_budget_balance   VARCHAR2(1000);
4963 
4964 BEGIN
4965 
4966   -- Bug#3317262: Added missing budgetary debit and credit account types.
4967 
4968   -- removed the pipe condition and sum function from the query
4969   -- for bug 4256345
4970 
4971   /* bug no 4725091 --> Modified the query to include the carry forward balance */
4972 
4973   sql_budget_balance := 'select gs.start_date, gs.end_date, ' ||
4974              'decode(:ACCOUNT_TYPE, ''A'', 1, ''E'', 1, ''D'', 1, ''L'', -1, ''O'', -1, ''R'', -1, ''C'', -1 ) * ' ||
4975              '(nvl(gb.period_net_dr, 0) - nvl(gb.period_net_cr, 0)), ' ||
4976              '(nvl(gb.BEGIN_BALANCE_DR, 0) - nvl(gb.BEGIN_BALANCE_CR,0)) '||
4977 	     'from GL_BALANCES gb, ' ||
4978 	     'GL_PERIOD_STATUSES gs ';
4979 
4980   if p_gl_budget_set_id is null then
4981   begin
4982 
4983     if FND_API.to_Boolean(p_budgetary_control) then
4984       /* for bug 4866848 --> added the table gl_budorg_bc_options */
4985       sql_budget_balance := sql_budget_balance ||
4986 			   ', GL_BUDGET_ASSIGNMENTS ga
4987                             , GL_BUDORG_BC_OPTIONS  gc ';
4988     end if;
4989 
4990   end;
4991   end if;
4992 
4993   -- Bug#4310414
4994   -- gb.set_of_books_id to gb.ledger_id
4995 
4996   -- Bug#5030613
4997   -- Replaced literal by bind variable.
4998   sql_budget_balance := sql_budget_balance ||
4999 		       'where gb.ledger_id = :B_SET_OF_BOOKS_ID ' ||
5000 			 'and gb.currency_code = :CURRENCY_CODE ' ||
5001 			 'and gb.code_combination_id = :CCID ' ||
5002 			 'and ((gb.translated_flag is null) or (gb.translated_flag = ''Y'')) ' ||
5003 			 'and gb.actual_flag = ''B'' ';
5004 
5005   -- If a budget set is assigned to the worksheet find the funding budget for the CCID from
5006   -- the budget set. If a budget set is not assigned to the worksheet :
5007   -- (i) if budgetary control is enabled, get the balance for the funding budget assigned to the CCID
5008   -- in the GL Budget Org
5009   -- (ii) if budgetary control is disabled, get the balance for the funding budget in the worksheet definition
5010 
5011   if p_gl_budget_set_id is null then
5012   begin
5013 
5014     if FND_API.to_Boolean(p_budgetary_control) then
5015       /* for bug --> 4866848 Add additional join condition for the range_id */
5016       sql_budget_balance := sql_budget_balance ||
5017 			   'and ga.code_combination_id = :CCID ' ||
5018 			   'and gb.budget_version_id = gc.funding_budget_version_id
5019                             and ga.range_id = gc.range_id ';
5020     end if;
5021 
5022   end;
5023   else
5024     sql_budget_balance := sql_budget_balance ||
5025 			 'and gb.budget_version_id = :BUDGET_VERSION_ID ';
5026   end if;
5027 
5028   -- for bug 4256345
5029   -- removing the pipe
5030 
5031   -- Bug#5030613
5032   -- Replaced literal by bind variable.
5033   sql_budget_balance := sql_budget_balance ||
5034 		       'and gb.period_name = gs.period_name ' ||
5035 		       'and gb.period_type = gs.period_type ' ||
5036 		       'and gb.period_year = gs.period_year ' ||
5037 		       'and gb.period_num = gs.period_num ' ||
5038 		       'and gs.set_of_books_id = :B_SET_OF_BOOKS_ID ' ||
5039 		       'and gs.application_id = 101 ';
5040 
5041   -- Map the GL Period Start Date or End Date to the PSB Budget Period based on the
5042   -- Profile Option
5043 
5044   if p_map_criteria = 'S' then
5045     sql_budget_balance := sql_budget_balance ||
5046 			 'and gs.start_date between :START_DATE and :END_DATE ';
5047   else
5048     sql_budget_balance := sql_budget_balance ||
5049 			 'and gs.end_date between :START_DATE and :END_DATE ';
5050   end if;
5051 
5052   -- Include GL Adjustment Periods if specified in the Worksheet definition
5053 
5054   if not FND_API.to_Boolean(p_incl_adj_period) then
5055     sql_budget_balance := sql_budget_balance ||
5056 			 'and gs.adjustment_period_flag = ''N'' ';
5057   end if;
5058 
5059   -- for bug 4256345
5060   -- since sum will not be there, no need for group by
5061   /*
5062   sql_budget_balance := sql_budget_balance ||
5063 		       'group by gs.start_date, gs.end_date'; */
5064 
5065 
5066   -- Initialize API return status to success
5067 
5068   p_sql_statement := sql_budget_balance;
5069   p_return_status := FND_API.G_RET_STS_SUCCESS;
5070 
5071 
5072 EXCEPTION
5073 
5074    when FND_API.G_EXC_ERROR then
5075      p_return_status := FND_API.G_RET_STS_ERROR;
5076 
5077    when FND_API.G_EXC_UNEXPECTED_ERROR then
5078      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5079 
5080    when OTHERS then
5081      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5082      if FND_MSG_PUB.Check_Msg_Level
5083        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
5084      then
5085        FND_MSG_PUB.Add_Exc_Msg
5086 	  (p_pkg_name => G_PKG_NAME,
5087 	   p_procedure_name => 'DSQL_Budget_Balance');
5088      end if;
5089 
5090 END DSQL_Budget_Balance;
5091 
5092 /* ------------------------------------------------------------------------- */
5093 
5094 PROCEDURE DSQL_Actual_Balance
5095 ( p_return_status    OUT  NOCOPY  VARCHAR2,
5096   p_sql_statement    OUT  NOCOPY  VARCHAR2,
5097   p_set_of_books_id  IN   NUMBER,
5098   p_incl_adj_period  IN   VARCHAR2,
5099   p_map_criteria     IN   VARCHAR2
5100 ) IS
5101 
5102   sql_actual_balance      VARCHAR2(1000);
5103 
5104 BEGIN
5105 
5106   -- Bug#3317262: Added missing budgetary debit and credit account types.
5107 
5108   -- for bug 4256345
5109   -- removing sum function and pipe function
5110   -- Bug#4310414
5111   -- gb.set_of_books_id to gb.ledger_id
5112 
5113   -- Bug#5030613
5114   -- Replaced literal by bind variable.
5115   sql_actual_balance := 'select gs.start_date, gs.end_date, ' ||
5116        'decode(:ACCOUNT_TYPE, ''A'', 1, ''E'', 1, ''D'', 1, ''L'', -1, ''O'', -1, ''R'', -1, ''C'', -1 ) * ' ||
5117        '(nvl(gb.period_net_dr, 0) - nvl(gb.period_net_cr, 0)) ' ||
5118        'from GL_BALANCES gb, ' ||
5119        'GL_PERIOD_STATUSES gs ' ||
5120        'where gb.ledger_id = :B_SET_OF_BOOKS_ID ' ||
5121        'and gb.currency_code = :CURRENCY_CODE ' ||
5122        'and gb.code_combination_id = :CCID ' ||
5123        'and ((gb.translated_flag is null) or (gb.translated_flag = ''Y'')) ' ||
5124        'and gb.actual_flag = ''A'' ' ||
5125        'and gb.period_name = gs.period_name ' ||
5126        'and gb.period_type = gs.period_type ' ||
5127        'and gb.period_year = gs.period_year ' ||
5128        'and gb.period_num = gs.period_num ' ||
5129        'and gs.set_of_books_id = :B_SET_OF_BOOKS_ID ' ||
5130        'and gs.application_id = 101 ';
5131 
5132   -- Map the GL Period Start Date or End Date to the PSB Budget Period based on the
5133   -- Profile Option
5134 
5135   if p_map_criteria = 'S' then
5136     sql_actual_balance := sql_actual_balance ||
5137 			 'and gs.start_date between :START_DATE and :END_DATE ';
5138   else
5139     sql_actual_balance := sql_actual_balance ||
5140 			 'and gs.end_date between :START_DATE and :END_DATE ';
5141   end if;
5142 
5143   -- Include GL Adjustment Periods if specified in the Worksheet definition
5144 
5145   if not FND_API.to_Boolean(p_incl_adj_period) then
5146     sql_actual_balance := sql_actual_balance ||
5147 			 'and gs.adjustment_period_flag = ''N'' ';
5148   end if;
5149 
5150   -- for bug 4256345
5151   -- commenting out the group function as sum is removed
5152   /*
5153   sql_actual_balance := sql_actual_balance ||
5154 		       'group by gs.start_date, gs.end_date'; */
5155 
5156 
5157   -- Initialize API return status to success
5158 
5159   p_sql_statement := sql_actual_balance;
5160   p_return_status := FND_API.G_RET_STS_SUCCESS;
5161 
5162 
5163 EXCEPTION
5164 
5165    when FND_API.G_EXC_ERROR then
5166      p_return_status := FND_API.G_RET_STS_ERROR;
5167 
5168    when FND_API.G_EXC_UNEXPECTED_ERROR then
5169      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5170 
5171    when OTHERS then
5172      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5173      if FND_MSG_PUB.Check_Msg_Level
5174        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
5175      then
5176        FND_MSG_PUB.Add_Exc_Msg
5177 	  (p_pkg_name => G_PKG_NAME,
5178 	   p_procedure_name => 'DSQL_Actual_Balance');
5179      end if;
5180 
5181 END DSQL_Actual_Balance;
5182 
5183 /* ------------------------------------------------------------------------- */
5184 
5185 PROCEDURE DSQL_Encum_Balance
5186 ( p_return_status              OUT  NOCOPY  VARCHAR2,
5187   p_sql_statement              OUT  NOCOPY  VARCHAR2,
5188   p_set_of_books_id            IN   NUMBER,
5189   p_incl_adj_period            IN   VARCHAR2,
5190   p_map_criteria               IN   VARCHAR2,
5191   p_include_gl_commit_balance  IN   VARCHAR2,
5192   p_include_gl_oblig_balance   IN   VARCHAR2,
5193   p_include_gl_other_balance   IN   VARCHAR2
5194 ) IS
5195 
5196   l_commit_enc_type_id         NUMBER;
5197   l_oblig_enc_type_id          NUMBER;
5198 
5199   sql_encum_balance            VARCHAR2(1000);
5200 
5201   -- Bug#5030613 Start
5202   -- Commenting the cursor. The same check will now be done in
5203   -- PSB_WS_ACCT2.Map_GL_Balances API.
5204   /*cursor c_fin is
5205     select purch_encumbrance_type_id, req_encumbrance_type_id
5206       from financials_system_parameters; */
5207   -- Bug#5030613 End
5208 
5209 BEGIN
5210   -- Bug#5030613 Start
5211   -- Commenting the cursor. The same check will now be done in
5212   -- PSB_WS_ACCT2.Map_GL_Balances API.
5213   /*for c_fin_rec in c_fin loop
5214     l_commit_enc_type_id := c_fin_rec.req_encumbrance_type_id;
5215     l_oblig_enc_type_id := c_fin_rec.purch_encumbrance_type_id;
5216   end loop; */
5217   -- Bug#5030613 End
5218 
5219   -- Bug#3317262: Added missing budgetary debit and credit account types.
5220 
5221   -- for bug 4256345
5222   -- removing sum and pipe function from the query
5223 
5224   /* bug no 4725091 --> Modified the query to include the carry forward balance */
5225 
5226   -- Bug#5030613
5227   -- Replaced literals by bind variables.
5228   sql_encum_balance := 'select gs.start_date, gs.end_date, ' ||
5229        'decode(:ACCOUNT_TYPE, ''A'', 1, ''E'', 1, ''D'', 1, ''L'', -1, ''O'', -1, ''R'', -1, ''C'', -1 ) * ' ||
5230        '(nvl(gb.period_net_dr, 0) - nvl(gb.period_net_cr, 0)), ' ||
5231        '(nvl(gb.BEGIN_BALANCE_DR, 0) - nvl(gb.BEGIN_BALANCE_CR, 0)) '||
5232        'from GL_BALANCES gb, ' ||
5233        'GL_PERIOD_STATUSES gs ' ||
5234        'where gb.ledger_id = :B_SET_OF_BOOKS_ID ' ||
5235        'and gb.currency_code = :CURRENCY_CODE ' ||
5236        'and gb.code_combination_id = :CCID ' ||
5237        'and ((gb.translated_flag is null) or (gb.translated_flag = ''Y'')) ' ||
5238        'and gb.actual_flag = ''E'' ' ||
5239        'and gb.period_name = gs.period_name ' ||
5240        'and gb.period_type = gs.period_type ' ||
5241        'and gb.period_year = gs.period_year ' ||
5242        'and gb.period_num = gs.period_num ' ||
5243        'and gs.set_of_books_id = :B_SET_OF_BOOKS_ID ' ||
5244        'and gs.application_id = 101 ';
5245 
5246   -- Bug#5030613 Start
5247   -- Commenting the cursor. The same check will now be done in
5248   -- PSB_WS_ACCT2.Map_GL_Balances API.
5249 
5250   -- Extract encumbrance balances, Include Other Encum Balances.
5251   /* if FND_API.to_Boolean(p_include_gl_other_balance) then
5252   begin
5253 
5254     if not FND_API.to_Boolean(p_include_gl_commit_balance) then
5255     begin
5256 
5257       if not FND_API.to_Boolean(p_include_gl_oblig_balance) then
5258 	sql_encum_balance := sql_encum_balance ||
5259 			    'and gb.encumbrance_type_id not in (' || l_oblig_enc_type_id || ',' ||
5260 								     l_commit_enc_type_id || ') ';
5261       else
5262 	sql_encum_balance := sql_encum_balance ||
5263 			    'and gb.encumbrance_type_id not in (' || l_commit_enc_type_id || ') ';
5264       end if;
5265 
5266     end;
5267     else
5268     begin
5269 
5270       if not FND_API.to_Boolean(p_include_gl_oblig_balance) then
5271 	sql_encum_balance := sql_encum_balance ||
5272 			    'and gb.encumbrance_type_id not in (' || l_oblig_enc_type_id || ') ';
5273       end if;
5274 
5275     end;
5276     end if;
5277 
5278   end;
5279   else
5280   begin
5281 
5282     if not FND_API.to_Boolean(p_include_gl_commit_balance) then
5283     begin
5284 
5285       if FND_API.to_Boolean(p_include_gl_oblig_balance) then
5286 	sql_encum_balance := sql_encum_balance ||
5287 			    'and gb.encumbrance_type_id in (' || l_oblig_enc_type_id || ') ';
5288       end if;
5289 
5290     end;
5291     else
5292     begin
5293 
5294       if not FND_API.to_Boolean(p_include_gl_oblig_balance) then
5295 	sql_encum_balance := sql_encum_balance ||
5296 			    'and gb.encumbrance_type_id in (' || l_commit_enc_type_id || ') ';
5297       else
5298 	sql_encum_balance := sql_encum_balance ||
5299 			    'and gb.encumbrance_type_id in (' || l_commit_enc_type_id || ',' || l_oblig_enc_type_id || ') ';
5300       end if;
5301 
5302     end;
5303     end if;
5304 
5305   end;
5306   end if;*/
5307   -- Bug#5030613 End
5308 
5309   -- Map the GL Period Start Date or End Date to the PSB Budget Period based on the
5310   -- Profile Option
5311 
5312   if p_map_criteria = 'S' then
5313     sql_encum_balance := sql_encum_balance ||
5314 			'and gs.start_date between :START_DATE and :END_DATE ';
5315   else
5316     sql_encum_balance := sql_encum_balance ||
5317 			'and gs.end_date between :START_DATE and :END_DATE ';
5318   end if;
5319 
5320   -- Include GL Adjustment Periods if specified in the Worksheet definition
5321 
5322   if not FND_API.to_Boolean(p_incl_adj_period) then
5323     sql_encum_balance := sql_encum_balance ||
5324 			'and gs.adjustment_period_flag = ''N'' ';
5325   end if;
5326 
5327   -- for bug 4256345
5328   -- commenting out the group by function as sum is commented out
5329   /*
5330   sql_encum_balance := sql_encum_balance ||
5331 		      'group by gs.start_date, gs.end_date'; */
5332 
5333 
5334   -- Initialize API return status to success
5335 
5336   p_sql_statement := sql_encum_balance;
5337   p_return_status := FND_API.G_RET_STS_SUCCESS;
5338 
5339 
5340 EXCEPTION
5341 
5342    when FND_API.G_EXC_ERROR then
5343      p_return_status := FND_API.G_RET_STS_ERROR;
5344 
5345    when FND_API.G_EXC_UNEXPECTED_ERROR then
5346      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5347 
5348    when OTHERS then
5349      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5350      if FND_MSG_PUB.Check_Msg_Level
5351        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
5352      then
5353        FND_MSG_PUB.Add_Exc_Msg
5354 	  (p_pkg_name => G_PKG_NAME,
5355 	   p_procedure_name => 'DSQL_Encum_Balance');
5356      end if;
5357 
5358 END DSQL_Encum_Balance;
5359 
5360 /* ----------------------------------------------------------------------- */
5361 
5362 -- Compute Line Total for Constraint
5363 
5364 FUNCTION Compute_Line_Total
5365 ( p_worksheet_id         IN  NUMBER,
5366   p_sp_exists            IN  BOOLEAN,
5367   p_flex_mapping_set_id  IN  NUMBER,
5368   p_budget_calendar_id   IN  NUMBER,
5369   p_ccid                 IN  NUMBER,
5370   p_budget_year_type_id  IN  NUMBER,
5371   p_balance_type         IN  VARCHAR2,
5372   p_currency_code        IN  VARCHAR2,
5373   /* start bug 4256345 */
5374   p_budget_period_id      IN   NUMBER,
5375   p_stage_set_id          IN   NUMBER,
5376   p_current_stage_seq     IN   NUMBER
5377   /* start bug 4256345 */
5378 ) RETURN NUMBER IS
5379 
5380   l_mapped_ccid          NUMBER;
5381   l_ytd_amount           NUMBER := 0;
5382 
5383   -- Compute sum of WS Account Lines for a Constraint Formula of type 1 or type 2.
5384   -- This is applicable for all Service Packages
5385 
5386 /* For Bug No. 2214715 : Start  */
5387 /*  Existing Cursor definition is commented and modified one is added as follows :
5388   cursor c_Type12 is
5389     select nvl(a.ytd_amount, 0) YTD_Amount
5390       from PSB_WS_ACCOUNT_LINES a,
5391 	   PSB_WORKSHEETS b,
5392 	   PSB_BUDGET_PERIODS c
5393      where a.code_combination_id = l_mapped_ccid
5394        and a.balance_type = p_balance_type
5395        and a.currency_code = p_currency_code
5396        and exists
5397 	  (select 1
5398 	     from PSB_WORKSHEETS b,
5399 		  PSB_BUDGET_PERIODS c
5400 	    where b.worksheet_id = p_worksheet_id
5401 	      and a.stage_set_id = b.stage_set_id
5402 	      and b.current_stage_seq between a.start_stage_seq and a.current_stage_seq
5403 	      and c.budget_calendar_id = p_budget_calendar_id
5404 	      and a.budget_year_id = c.budget_period_id
5405 	      and c.budget_period_type = 'Y'
5406 	      and c.budget_year_type_id = p_budget_year_type_id)
5407        and exists
5408 	  (select 1
5409 	     from PSB_WS_LINES
5410 	    where account_line_id = a.account_line_id
5411 	      and worksheet_id = p_worksheet_id);
5412 */
5413 
5414   -- for bug 4256345
5415   -- commenting the cursor
5416 /*
5417   cursor c_Type12 is
5418   select sum(nvl(a.ytd_amount, 0)) YTD_Amount
5419       from PSB_WORKSHEETS b,
5420 	   PSB_WS_LINES d,
5421 	   PSB_WS_ACCOUNT_LINES a,
5422 	   PSB_BUDGET_PERIODS c
5423      where b.worksheet_id = p_worksheet_id
5424        and d.worksheet_id = b.worksheet_id
5425        and d.account_line_id = a.account_line_id
5426        and a.code_combination_id = l_mapped_ccid
5427        and a.balance_type = p_balance_type
5428        and a.currency_code = p_currency_code
5429        and a.stage_set_id = b.stage_set_id
5430        and b.current_stage_seq between a.start_stage_seq and a.current_stage_seq
5431        and c.budget_calendar_id = p_budget_calendar_id
5432        and a.budget_year_id = c.budget_period_id
5433        and c.budget_period_type = 'Y'
5434        and c.budget_year_type_id = p_budget_year_type_id; */
5435 
5436 /* For Bug No. 2214715 : End  */
5437 
5438   -- Compute sum of WS Account Lines for a Constraint Formula of type 1 or type 2.
5439   -- This is applicable for all Service Packages that have been selected for
5440   -- submission in Worksheet Operations
5441 
5442 /* For Bug No. 2214715 : Start  */
5443 /*  Existing Cursor definition is commented and modified one is added as follows :
5444   cursor c_Type12SP is
5445     select nvl(a.ytd_amount, 0) YTD_Amount
5446       from PSB_WS_ACCOUNT_LINES a,
5447 	   PSB_WORKSHEETS b,
5448 	   PSB_BUDGET_PERIODS c
5449      where a.code_combination_id = l_mapped_ccid
5450        and a.balance_type = p_balance_type
5451        and a.currency_code = p_currency_code
5452        and exists
5453 	  (select 1
5454 	     from PSB_WORKSHEETS b,
5455 		  PSB_BUDGET_PERIODS c
5456 	    where b.worksheet_id = p_worksheet_id
5457 	      and a.stage_set_id = b.stage_set_id
5458 	      and b.current_stage_seq between a.start_stage_seq and a.current_stage_seq
5459 	      and c.budget_calendar_id = p_budget_calendar_id
5460 	      and a.budget_year_id = c.budget_period_id
5461 	      and c.budget_period_type = 'Y'
5462 	      and c.budget_year_type_id = p_budget_year_type_id)
5463        and exists
5464 	  (select 1
5465 	     from PSB_WS_LINES
5466 	    where account_line_id = a.account_line_id
5467 	      and worksheet_id = p_worksheet_id)
5468        and exists
5469 	  (select 1
5470 	     from PSB_WS_SUBMIT_SERVICE_PACKAGES d
5471 	    where d.service_package_id = a.service_package_id
5472 	      and d.worksheet_id = p_worksheet_id);
5473 */
5474 
5475 
5476   -- for bug 4256345
5477   -- commenting the cursor
5478 
5479   /*
5480   cursor c_Type12SP is
5481   select sum(nvl(a.ytd_amount, 0)) YTD_Amount
5482       from PSB_WORKSHEETS b,
5483 	   PSB_WS_LINES d,
5484 	   PSB_WS_ACCOUNT_LINES a,
5485 	   PSB_BUDGET_PERIODS c
5486      where b.worksheet_id = p_worksheet_id
5487        and d.worksheet_id = b.worksheet_id
5488        and d.account_line_id = a.account_line_id
5489        and a.code_combination_id = l_mapped_ccid
5490        and a.balance_type = p_balance_type
5491        and a.currency_code = p_currency_code
5492        and a.stage_set_id = b.stage_set_id
5493        and b.current_stage_seq between a.start_stage_seq and a.current_stage_seq
5494        and c.budget_calendar_id = p_budget_calendar_id
5495        and a.budget_year_id = c.budget_period_id
5496        and c.budget_period_type = 'Y'
5497        and c.budget_year_type_id = p_budget_year_type_id
5498        and exists
5499 	  (select 1
5500 	     from PSB_WS_SUBMIT_SERVICE_PACKAGES e
5501 	    where e.service_package_id = a.service_package_id
5502 	      and e.worksheet_id = p_worksheet_id); */
5503 
5504 /* For Bug No. 2214715 : End  */
5505 
5506 BEGIN
5507 
5508   l_mapped_ccid := Map_Account
5509 		      (p_flex_mapping_set_id => p_flex_mapping_set_id,
5510 		       p_ccid => p_ccid,
5511 		       p_budget_year_type_id => p_budget_year_type_id);
5512 
5513 
5514   if p_sp_exists then
5515   begin
5516 
5517    -- commenting the cursor for Bug No : 4256345
5518    /*
5519     for c_Type12_Rec in c_Type12SP loop
5520       l_ytd_amount := c_Type12_Rec.YTD_Amount;
5521     end loop;
5522    */
5523 
5524    FOR l_ytd_rec IN (SELECT NVL(A.YTD_AMOUNT, 0) YTD_AMOUNT
5525     			 FROM PSB_WS_LINES WSL,
5526                           PSB_WS_ACCOUNT_LINES A
5527                      WHERE A.CODE_COMBINATION_ID = l_mapped_ccid
5528                      AND   A.BUDGET_YEAR_ID = p_budget_period_id
5529                      AND   A.BALANCE_TYPE = p_balance_type
5530                      AND   A.CURRENCY_CODE = p_currency_code
5531                      AND   A.STAGE_SET_ID = p_stage_set_id
5532                      AND   p_current_stage_seq
5533                      BETWEEN A.START_STAGE_SEQ AND A.CURRENT_STAGE_SEQ
5534                      AND   WSL.WORKSHEET_ID = p_worksheet_id
5535                      AND   WSL.ACCOUNT_LINE_ID = A.ACCOUNT_LINE_ID
5536                      AND   EXISTS( SELECT 1
5537             					   FROM	PSB_WS_SUBMIT_SERVICE_PACKAGES S
5538             					   WHERE S.SERVICE_PACKAGE_ID = A.SERVICE_PACKAGE_ID
5539             					   AND	S.WORKSHEET_ID = p_worksheet_id))
5540 			 LOOP
5541 			   l_ytd_amount := l_ytd_amount + l_ytd_rec.ytd_amount;
5542 			 END LOOP;
5543 
5544   end;
5545   else
5546   begin
5547 
5548     -- commenting the cursor for Bug No : 4256345
5549     /*
5550     for c_Type12_Rec in c_Type12 loop
5551       l_ytd_amount := c_Type12_Rec.YTD_Amount;
5552     end loop;
5553     */
5554 
5555     FOR l_ytd_rec IN (SELECT NVL(A.YTD_AMOUNT, 0) YTD_AMOUNT
5556     			  FROM PSB_WS_LINES WSL,
5557                            PSB_WS_ACCOUNT_LINES A
5558     			  WHERE A.CODE_COMBINATION_ID = l_mapped_ccid
5559     			  AND   A.BUDGET_YEAR_ID = p_budget_period_id
5560     			  AND   A.BALANCE_TYPE = p_balance_type
5561     			  AND   A.CURRENCY_CODE = p_currency_code
5562     			  AND   A.STAGE_SET_ID = p_stage_set_id
5563     			  AND   p_current_stage_seq
5564     			  BETWEEN A.START_STAGE_SEQ AND A.CURRENT_STAGE_SEQ
5565     			  AND   WSL.WORKSHEET_ID = p_worksheet_id
5566     			  AND   WSL.ACCOUNT_LINE_ID = A.ACCOUNT_LINE_ID)
5567      LOOP
5568        l_ytd_amount := l_ytd_amount + l_ytd_rec.ytd_amount;
5569      END LOOP;
5570 
5571   end;
5572   end if;
5573 
5574   RETURN l_ytd_amount;
5575 
5576 END Compute_Line_Total;
5577 
5578 /* ----------------------------------------------------------------------- */
5579 
5580 -- Compute Sum Total for Account
5581 
5582 FUNCTION Compute_Account_Total
5583 ( p_worksheet_id         IN  NUMBER,
5584   p_sp_exists            IN  BOOLEAN,
5585   p_flex_mapping_set_id  IN  NUMBER,
5586   p_ccid                 IN  NUMBER,
5587   p_budget_year_id       IN  NUMBER,
5588   p_budget_year_type_id  IN  NUMBER,
5589   p_currency_code        IN  VARCHAR2
5590 ) RETURN NUMBER IS
5591 
5592   l_mapped_ccid          NUMBER;
5593   l_ytd_amount           NUMBER := 0;
5594 
5595   -- Compute Sum of WS Account Lines for individual CCIDs.
5596   -- This is applicable when the Detailed Flag is Set and is for all Service Packages
5597 
5598   cursor c_Sum is
5599     select sum(nvl(a.ytd_amount,0)) Sum_Acc
5600       from PSB_WS_ACCOUNT_LINES a,
5601 	   PSB_WORKSHEETS b
5602      where exists
5603 	  (select 1
5604 	     from PSB_WS_LINES
5605 	    where account_line_id = a.account_line_id
5606 	      and worksheet_id = p_worksheet_id)
5607        and b.current_stage_seq between a.start_stage_seq and a.current_stage_seq
5608        and a.balance_type = 'E'
5609        and a.stage_set_id = b.stage_set_id
5610        and a.currency_code = p_currency_code
5611        and a.budget_year_id = p_budget_year_id
5612        and a.code_combination_id = l_mapped_ccid
5613        and b.worksheet_id = p_worksheet_id;
5614 
5615   -- Compute Sum of WS Account Lines for individual CCIDs.
5616   -- This is applicable when the Detailed Flag is Set and is for all Service Packages
5617   -- that have been selected for submission in Worksheet Operations
5618 
5619   cursor c_SumSP is
5620     select sum(nvl(a.ytd_amount,0)) Sum_Acc
5621       from PSB_WS_ACCOUNT_LINES a,
5622 	   PSB_WORKSHEETS b
5623      where exists
5624 	  (select 1
5625 	     from PSB_WS_SUBMIT_SERVICE_PACKAGES c
5626 	    where c.service_package_id = a.service_package_id
5627 	      and c.worksheet_id = p_worksheet_id)
5628        and exists
5629 	  (select 1
5630 	     from PSB_WS_LINES
5631 	    where account_line_id = a.account_line_id
5632 	      and worksheet_id = p_worksheet_id)
5633        and b.current_stage_seq between a.start_stage_seq and a.current_stage_seq
5634        and a.balance_type = 'E'
5635        and a.stage_set_id = b.stage_set_id
5636        and a.currency_code = p_currency_code
5637        and a.budget_year_id = p_budget_year_id
5638        and a.code_combination_id = l_mapped_ccid
5639        and b.worksheet_id = p_worksheet_id;
5640 
5641 BEGIN
5642 
5643   l_mapped_ccid := Map_Account
5644 		      (p_flex_mapping_set_id => p_flex_mapping_set_id,
5645 		       p_ccid => p_ccid,
5646 		       p_budget_year_type_id => p_budget_year_type_id);
5647 
5648   if p_sp_exists then
5649   begin
5650 
5651     for c_Sum_Rec in c_SumSP loop
5652       l_ytd_amount := c_Sum_Rec.Sum_Acc;
5653     end loop;
5654 
5655   end;
5656   else
5657   begin
5658 
5659     for c_Sum_Rec in c_Sum loop
5660       l_ytd_amount := c_Sum_Rec.Sum_Acc;
5661     end loop;
5662 
5663   end;
5664   end if;
5665 
5666   -- For Bug No. 2854288 added the following if condition
5667   if l_ytd_amount is NULL then
5668     RETURN 0;
5669   else
5670     RETURN l_ytd_amount;
5671   end if;
5672 
5673 END Compute_Account_Total;
5674 
5675 /* ----------------------------------------------------------------------- */
5676 
5677 -- Added the following function for Bug No. 3206445
5678 
5679 -- Determine whether a Summary Constraint Set contains positive only , negative
5680 -- only or mixture of positive and negative account types. The function returns
5681 -- the correction factor required, if any, for the Summary Total i.e. the
5682 -- function returns:
5683 
5684 -- (i) 1 if there is no correction required in the Summary Total for a
5685 -- Constraint Account Set. This happens when positive only or mixture of
5686 -- positive and negative account types are chosen.
5687 
5688 -- (ii) -1 if there is correction required in the Summary Total for a
5689 -- Constraint Account Set. This happens when negative only account types
5690 -- are chosen.
5691 
5692 FUNCTION Compute_Correction_Factor
5693 (   p_constraint_id  IN NUMBER,
5694     p_worksheet_id   IN NUMBER
5695 ) RETURN NUMBER IS
5696 
5697  -- Find distinct account types of the accounts assigned to the account sets
5698  -- included in the constraint.
5699 
5700   cursor c_Account_types is
5701     select distinct f.account_type
5702       from psb_budget_accounts d,
5703 	   psb_set_relations_v e,
5704            psb_ws_account_lines f,
5705            psb_ws_lines g
5706      where d.account_position_set_id = e.account_position_set_id
5707        and e.account_or_position_type = 'A'
5708        and e.constraint_id = p_constraint_id
5709        and d.code_combination_id = f.code_combination_id
5710        and f.account_line_id = g.account_line_id
5711        and g.worksheet_id = p_worksheet_id;
5712 
5713 BEGIN
5714 
5715     FOR C_Account_types_rec in C_account_types LOOP
5716        IF C_Account_types_rec.account_type in ('C','L','O','R') THEN
5717        -- If there is one positive account type, then no correction required.
5718           RETURN 1;
5719        END IF;
5720     END LOOP;
5721 
5722    -- No positive account types in the account set. Hence make the summary total
5723    -- positive.
5724    RETURN -1;
5725 
5726 END Compute_Correction_Factor;
5727 
5728 /* ----------------------------------------------------------------------- */
5729 
5730 -- Added the following function for Bug No. 3206445
5731 
5732 -- Determine whether a Account Type is positive or negative. The
5733 -- function returns:
5734 
5735 -- (i) 1 if the account type is positive. This happens when the account
5736 -- type is one of Budgetary CR (C),Liabilities (L),Owners Equity (O) or
5737 -- Revenues (R).
5738 
5739 -- (ii) -1 if the account type is negative. This happens when the account
5740 -- type is one of Assets (A),Budgetary Debit (D) or Expenses (E).
5741 
5742 
5743 FUNCTION Compute_Account_Type_Factor
5744 ( p_ccid   IN NUMBER,
5745   p_worksheet_id IN NUMBER
5746 ) RETURN NUMBER IS
5747 
5748   cursor c_Account_types is
5749     select distinct account_type
5750       from gl_code_combinations
5751        where code_combination_id = p_ccid;
5752 
5753 BEGIN
5754 
5755     FOR C_Account_types_rec in C_account_types LOOP
5756        IF C_Account_types_rec.account_type in ('L','C','R','O') THEN
5757        -- positive account type
5758           RETURN 1;
5759        ELSE
5760        -- negative account type
5761           RETURN -1;
5762        END IF;
5763     END LOOP;
5764     -- If account line not in GLC,return 1
5765     RETURN 1;
5766 
5767 END Compute_Account_Type_Factor;
5768 
5769 
5770 
5771 /* ------------------------------------------------------------------------ */
5772 -- Compute Total for Summary Constraint Account Sets
5773 
5774 FUNCTION Compute_Sum_Total
5775 ( p_worksheet_id         IN  NUMBER,
5776   p_sp_exists            IN  BOOLEAN,
5777   p_constraint_id        IN  NUMBER,
5778   p_flex_mapping_set_id  IN  NUMBER,
5779   p_budget_year_id       IN  NUMBER,
5780   p_budget_year_type_id  IN  NUMBER,
5781   p_currency_code        IN  VARCHAR2,
5782   p_budget_group_id 	 IN  NUMBER
5783 ) RETURN NUMBER IS
5784 
5785   /* Bug 3608191 Start */
5786   l_budget_group_id NUMBER;
5787 
5788   CURSOR l_Budget_Group_csr (c_budgetgroup_id NUMBER)
5789   IS
5790   SELECT budget_group_id
5791   FROM   psb_budget_groups
5792   WHERE  budget_group_type    = 'R'
5793   START  WITH budget_group_id      = c_budgetgroup_id
5794   CONNECT BY prior budget_group_id = parent_budget_group_id;
5795 
5796   -- Find individual accounts assigned to the Constraint
5797   CURSOR l_Accounts_csr
5798   IS
5799   SELECT d.code_combination_id, e.account_position_set_id
5800   FROM    psb_budget_accounts d,
5801           psb_set_relations e
5802   WHERE d.account_position_set_id = e.account_position_set_id
5803   AND e.constraint_id = p_constraint_id
5804   AND EXISTS
5805   (SELECT 1
5806   FROM    psb_budget_accounts a,
5807           psb_set_relations b
5808   WHERE a.account_position_set_id = b.account_position_set_id
5809   AND b.budget_group_id = l_budget_group_id
5810   AND a.code_combination_id = d.code_combination_id);
5811 
5812   CURSOR l_Accounts_global_csr
5813   IS
5814   SELECT d.code_combination_id, e.account_position_set_id
5815   FROM    psb_budget_accounts d,
5816           psb_set_relations e
5817   WHERE d.account_position_set_id = e.account_position_set_id
5818   --AND e.account_or_position_type = 'A'
5819   AND e.constraint_id = p_constraint_id;
5820   /* Bug 3608191 End */
5821 
5822   l_ytd_amount           NUMBER;
5823   l_sum_amount           NUMBER := 0;
5824   l_correction_factor    NUMBER;
5825   l_account_type_factor  NUMBER;
5826 
5827   /* Bug 3608191 Start */
5828   l_processed_flag 		 VARCHAR2(1) := 'N';
5829   l_global_ws_flag		 VARCHAR2(1) := 'N';
5830   /* Bug 3608191 End */
5831 
5832 
5833 BEGIN
5834 
5835   /* Bug 3608191 : Start */
5836   BEGIN
5837     SELECT global_worksheet_flag
5838       INTO l_global_ws_flag
5839       FROM psb_worksheets
5840      WHERE worksheet_id = p_worksheet_id;
5841   END;
5842 
5843   l_processed_flag := 'N';
5844 
5845 IF l_global_ws_flag = 'Y' THEN
5846 
5847   FOR l_Accounts_global_csr_Rec in l_Accounts_global_csr loop
5848 
5849     l_processed_flag := 'Y';
5850 
5851     l_ytd_amount := Compute_Account_Total (p_worksheet_id => p_worksheet_id,
5852 					   p_sp_exists => p_sp_exists,
5853 					   p_flex_mapping_set_id => p_flex_mapping_set_id,
5854 					   p_ccid => l_Accounts_global_csr_rec.code_combination_id,
5855 					   p_budget_year_id => p_budget_year_id,
5856 					   p_budget_year_type_id => p_budget_year_type_id,
5857 					   p_currency_code => p_currency_code);
5858 
5859     -- For Bug No. 2854288 added the following IF condition
5860     if l_ytd_amount is not NULL then
5861       -- Added l_account_type_factor for Bug No. 3206445
5862       l_account_type_factor := Compute_Account_Type_Factor(
5863                                    p_ccid => l_Accounts_global_csr_rec.code_combination_id,
5864                                    p_worksheet_id => p_worksheet_id);
5865       l_sum_amount := l_sum_amount + l_account_type_factor * l_ytd_amount;
5866     end if;
5867 
5868   END LOOP;
5869 
5870 ELSE
5871 
5872 
5873   l_processed_flag := 'N';
5874 
5875   FOR l_Budget_Group_csr_Rec IN l_Budget_Group_csr (p_budget_group_id) LOOP
5876     -- Find Account Sets for the Budget Group
5877     l_budget_group_id  := l_Budget_Group_csr_Rec.budget_group_id;
5878 
5879     FOR l_Accounts_csr_Rec in l_Accounts_csr
5880     LOOP
5881 
5882       l_processed_flag := 'Y';
5883 
5884       l_ytd_amount := Compute_Account_Total (p_worksheet_id => p_worksheet_id,
5885 					   p_sp_exists => p_sp_exists,
5886 					   p_flex_mapping_set_id => p_flex_mapping_set_id,
5887 					   p_ccid => l_Accounts_csr_Rec.code_combination_id,
5888 					   p_budget_year_id => p_budget_year_id,
5889 					   p_budget_year_type_id => p_budget_year_type_id,
5890 					   p_currency_code => p_currency_code);
5891 
5892       -- For Bug No. 2854288 added the following IF condition
5893       if l_ytd_amount is not NULL then
5894         -- Added l_account_type_factor for Bug No. 3206445
5895         l_account_type_factor := Compute_Account_Type_Factor(
5896                                    p_ccid => l_Accounts_csr_Rec.code_combination_id,
5897                                    p_worksheet_id => p_worksheet_id);
5898         l_sum_amount := l_sum_amount + l_account_type_factor * l_ytd_amount;
5899       end if;
5900 
5901     END LOOP;
5902 
5903   END LOOP;
5904 
5905 END IF;
5906 /* Bug 3608191 : End */
5907 
5908 
5909   -- For Bug No. 3206445 Start
5910   l_correction_factor := Compute_Correction_Factor(
5911                                       p_worksheet_id => p_worksheet_id,
5912                                       p_constraint_id => p_constraint_id);
5913 
5914   /* Bug 3608191 : Added IF statement */
5915   IF l_processed_flag = 'Y' THEN
5916     RETURN l_correction_factor * l_sum_amount;
5917   ELSE
5918     RETURN NULL;
5919   END IF;
5920 
5921 END Compute_Sum_Total;
5922 
5923 /* ----------------------------------------------------------------------- */
5924 
5925 -- Process Detailed Constraint for an individual CCID or Summary Constraint
5926 -- for all CCIDs
5927 
5928 PROCEDURE Process_Constraint
5929 ( p_return_status                 OUT  NOCOPY  VARCHAR2,
5930   p_constraint_validation_status  OUT  NOCOPY  VARCHAR2,
5931   p_worksheet_id                  IN   NUMBER,
5932   p_sp_exists                     IN   BOOLEAN,
5933   p_constraint_set_name           IN   VARCHAR2,
5934   p_constraint_threshold          IN   NUMBER,
5935   p_constraint_id                 IN   NUMBER,
5936   p_constraint_name               IN   VARCHAR2,
5937   p_flex_code                     IN   NUMBER,
5938   p_flex_delimiter                IN   VARCHAR2,
5939   p_ccid                          IN   NUMBER := 0,
5940   p_budget_calendar_id            IN   NUMBER,
5941   p_budget_year_id                IN   NUMBER,
5942   p_budget_year_name              IN   VARCHAR2,
5943   p_currency_code                 IN   VARCHAR2,
5944   p_severity_level                IN   NUMBER,
5945   p_summ_flag                     IN   BOOLEAN,
5946   p_flex_mapping_set_id           IN   NUMBER,
5947   p_budget_year_type_id           IN   NUMBER,
5948   p_budget_group_id		  		  IN   NUMBER,
5949   /* start bug 4256345 */
5950   p_budget_period_id      		  IN   NUMBER,
5951   p_stage_set_id          		  IN   NUMBER,
5952   p_current_stage_seq     		  IN   NUMBER
5953   /* end bug 4256345 */
5954 
5955 ) IS
5956 
5957   cursor c_Formula is
5958     select step_number,
5959 	   prefix_operator,
5960 	   budget_year_type_id,
5961 	   balance_type,
5962 	   currency_code,
5963 	   nvl(amount, 0) amount,
5964 	   postfix_operator,
5965 	   segment1, segment2, segment3,
5966 	   segment4, segment5, segment6,
5967 	   segment7, segment8, segment9,
5968 	   segment10, segment11, segment12,
5969 	   segment13, segment14, segment15,
5970 	   segment16, segment17, segment18,
5971 	   segment19, segment20, segment21,
5972 	   segment22, segment23, segment24,
5973 	   segment25, segment26, segment27,
5974 	   segment28, segment29, segment30
5975       from PSB_CONSTRAINT_FORMULAS
5976      where constraint_id = p_constraint_id
5977      order by step_number;
5978 
5979   /* Bug 2321186 Start */
5980   CURSOR c_Accounts IS
5981     SELECT d.code_combination_id
5982       FROM psb_budget_accounts d,
5983 	   psb_set_relations e
5984      WHERE d.account_position_set_id  = e.account_position_set_id
5985        AND e.constraint_id            = p_constraint_id;
5986 
5987   l_sum_amount          NUMBER := 0;
5988   l_account_type_factor NUMBER;
5989   l_correction_factor   NUMBER;
5990   /* Bug 2321186 End */
5991 
5992   l_first_line       BOOLEAN := TRUE;
5993 
5994 /* Bug No 2719865 Start */
5995   l_ccid_defined     BOOLEAN := TRUE;
5996 /* Bug No 2719865 End */
5997 
5998   l_cons_failed      BOOLEAN := FALSE;
5999   l_type1            BOOLEAN;
6000   l_type2            BOOLEAN;
6001   l_type3            BOOLEAN;
6002   l_type4            BOOLEAN;
6003   l_type5            BOOLEAN;
6004 
6005   l_ytd_amount       NUMBER;
6006   l_line_total       NUMBER := 0;
6007   l_accset_total     NUMBER := 0;
6008   l_cons_total       NUMBER := 0;
6009 
6010   l_operator         VARCHAR2(2);
6011 
6012   l_reqid            NUMBER;
6013   l_userid           NUMBER;
6014   l_description      VARCHAR2(2000) := null;
6015 
6016   l_init_index       PLS_INTEGER;
6017   l_index            PLS_INTEGER;
6018 
6019   l_ccid             NUMBER;
6020   l_seg_val          FND_FLEX_EXT.SegmentArray;
6021   l_ccid_val         FND_FLEX_EXT.SegmentArray;
6022 
6023   l_concat_segments  VARCHAR2(2000) := null;
6024 
6025   l_mapped_ccid      NUMBER;
6026 
6027   l_return_status    VARCHAR2(1);
6028   l_msg_data         VARCHAR2(2000);
6029   l_msg_count        NUMBER;
6030 
6031   l_api_name          CONSTANT VARCHAR2(30)   := 'Process_Constraint';
6032 
6033 BEGIN
6034 
6035   -- Cache number of Segments and the Application Column Names for the
6036   -- Segments
6037 
6038   if p_flex_code <> nvl(g_flex_code, 0) then
6039   begin
6040 
6041     Flex_Info
6042        (p_flex_code => p_flex_code,
6043 	p_return_status => l_return_status);
6044 
6045     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
6046       raise FND_API.G_EXC_ERROR;
6047     end if;
6048 
6049   end;
6050   end if;
6051 
6052   -- Get the individual Segment values for the CCID
6053 
6054   if p_ccid <> 0 then
6055   begin
6056 
6057     if not FND_FLEX_EXT.Get_Segments
6058       (application_short_name => 'SQLGL',
6059        key_flex_code => 'GL#',
6060        structure_number => p_flex_code,
6061        combination_id => p_ccid,
6062        n_segments => g_num_segs,
6063        segments => l_ccid_val) then
6064 
6065       FND_MSG_PUB.Add;
6066       raise FND_API.G_EXC_ERROR;
6067     end if;
6068 
6069   end;
6070   end if;
6071 
6072   -- Parse the Constraint Formula
6073 
6074   for c_Formula_Rec in c_Formula loop
6075 
6076     -- Each Formula Line is of the following types :
6077     --
6078     -- Type1: Depends on Account Set Assignments
6079     --       (Step, Prefix Operator, Postfix Operator, Period, Balance Type, Currency, Amount have values; Account is blank; this is valid only if 'Detailed' flag is set for the Constraint)
6080     --
6081     -- Type2: Depends on Account defined in Formula Line
6082     --       (Step, Prefix Operator, Period, Balance Type, Account, Currency have values; Amount and Postfix Operator are optional; all the Segment Values should be entered if 'Detailed' flag is not set for the Constraint)
6083     --
6084     -- Type3: Flat Amount assignment
6085     --       (Step, Prefix Operator, Amount have values; Period, Balance Type, Account, Currency, Postfix Operator are blank)
6086     --
6087     -- Type4: Depends on Account Set Assignments (Only for budget revisions)
6088     --       (Step, Prefix Operator, Postfix Operator, Balance Type, Currency, Amount have values; Account, Period are blank; this is valid only if 'Detailed' flag is set for the Constraint)
6089     --
6090     -- Type5: Depends on Account Set Assignments
6091     --       (Step, Prefix Operator, Postfix Operator, Period, Balance Type, Currency, Amount have values; Account is blank;
6092     --        Detailed flag is unchecked for the constarint.
6093     --
6094     l_type1 := FALSE;
6095     l_type2 := FALSE;
6096     l_type3 := FALSE;
6097     l_type4 := FALSE;
6098     l_type5 := FALSE;
6099 
6100     for l_init_index in 1..g_num_segs loop
6101       l_seg_val(l_init_index) := null;
6102     end loop;
6103 
6104     if l_first_line then
6105 
6106       l_first_line := FALSE;
6107 
6108       -- Prefix Operator for the 1st line of a Constraint Formula should be either of :
6109       -- '<=', '>=', '<', '>', '=', '<>'
6110 
6111       if c_Formula_Rec.prefix_operator not in ('<=', '>=', '<', '>', '=', '<>') then
6112 	message_token('CONSTRAINT', p_constraint_name);
6113 	message_token('STEPID', c_Formula_Rec.step_number);
6114 	message_token('OPERATOR', '[<=, >=, <, >, =, <>]');
6115 	add_message('PSB', 'PSB_INVALID_CONS_OPR');
6116 	raise FND_API.G_EXC_ERROR;
6117       else
6118 	l_operator := c_Formula_Rec.prefix_operator;
6119       end if;
6120     else
6121 
6122       -- Prefix Operator for the other lines of a Constraint Formula should be either of :
6123       -- '+', '-', '*', '/'
6124 
6125       if c_Formula_Rec.prefix_operator not in ('+', '-', '*', '/') then
6126 	message_token('CONSTRAINT', p_constraint_name);
6127 	message_token('STEPID', c_Formula_Rec.step_number);
6128 	message_token('OPERATOR', '[+, -, *, /]');
6129 	add_message('PSB', 'PSB_INVALID_CONS_OPR');
6130 	raise FND_API.G_EXC_ERROR;
6131       end if;
6132     end if;
6133 
6134 
6135     -- Check Formula Type
6136 
6137     if ((c_Formula_Rec.prefix_operator is not null) and
6138 	(c_Formula_Rec.postfix_operator is not null) and
6139 	(c_Formula_Rec.budget_year_type_id is not null) and
6140 	(c_Formula_Rec.balance_type is not null) and
6141 	(c_Formula_Rec.currency_code is not null) and
6142 	(c_Formula_Rec.amount is not null) and
6143        ((c_Formula_Rec.segment1 is null) and (c_Formula_Rec.segment2 is null) and (c_Formula_Rec.segment3 is null) and
6144 	(c_Formula_Rec.segment4 is null) and (c_Formula_Rec.segment5 is null) and (c_Formula_Rec.segment6 is null) and
6145 	(c_Formula_Rec.segment7 is null) and (c_Formula_Rec.segment8 is null) and (c_Formula_Rec.segment9 is null) and
6146 	(c_Formula_Rec.segment10 is null) and (c_Formula_Rec.segment11 is null) and (c_Formula_Rec.segment12 is null) and
6147 	(c_Formula_Rec.segment13 is null) and (c_Formula_Rec.segment14 is null) and (c_Formula_Rec.segment15 is null) and
6148 	(c_Formula_Rec.segment16 is null) and (c_Formula_Rec.segment17 is null) and (c_Formula_Rec.segment18 is null) and
6149 	(c_Formula_Rec.segment19 is null) and (c_Formula_Rec.segment20 is null) and (c_Formula_Rec.segment21 is null) and
6150 	(c_Formula_Rec.segment22 is null) and (c_Formula_Rec.segment23 is null) and (c_Formula_Rec.segment24 is null) and
6151 	(c_Formula_Rec.segment25 is null) and (c_Formula_Rec.segment26 is null) and (c_Formula_Rec.segment27 is null) and
6152 	(c_Formula_Rec.segment28 is null) and (c_Formula_Rec.segment29 is null) and (c_Formula_Rec.segment30 is null))) then
6153     begin
6154 
6155       if p_summ_flag then
6156 
6157      -- commented out the following statements as part of bug fix 2321186
6158      /* begin
6159 	  message_token('CONSTRAINT', p_constraint_name);
6160 	  add_message('PSB', 'PSB_INVALID_CONS_FORMULA');
6161 	  raise FND_API.G_EXC_ERROR;
6162         end; */
6163 
6164         /* Bug 2321186 Start */
6165         l_type5 := TRUE;
6166         /* Bug 2321186 End */
6167 
6168       else
6169 	l_type1 := TRUE;
6170       end if;
6171 
6172     end;
6173     elsif ((c_Formula_Rec.prefix_operator is not null) and
6174 	   (c_Formula_Rec.budget_year_type_id is not null) and
6175 	   (c_Formula_Rec.balance_type is not null) and
6176 	   (c_Formula_Rec.currency_code is not null) and
6177 	  ((c_Formula_Rec.segment1 is not null) or (c_Formula_Rec.segment2 is not null) or (c_Formula_Rec.segment3 is not null) or
6178 	   (c_Formula_Rec.segment4 is not null) or (c_Formula_Rec.segment5 is not null) or (c_Formula_Rec.segment6 is not null) or
6179 	   (c_Formula_Rec.segment7 is not null) or (c_Formula_Rec.segment8 is not null) or (c_Formula_Rec.segment9 is not null) or
6180 	   (c_Formula_Rec.segment10 is not null) or (c_Formula_Rec.segment11 is not null) or (c_Formula_Rec.segment12 is not null) or
6181 	   (c_Formula_Rec.segment13 is not null) or (c_Formula_Rec.segment14 is not null) or (c_Formula_Rec.segment15 is not null) or
6182 	   (c_Formula_Rec.segment16 is not null) or (c_Formula_Rec.segment17 is not null) or (c_Formula_Rec.segment18 is not null) or
6183 	   (c_Formula_Rec.segment19 is not null) or (c_Formula_Rec.segment20 is not null) or (c_Formula_Rec.segment21 is not null) or
6184 	   (c_Formula_Rec.segment22 is not null) or (c_Formula_Rec.segment23 is not null) or (c_Formula_Rec.segment24 is not null) or
6185 	   (c_Formula_Rec.segment25 is not null) or (c_Formula_Rec.segment26 is not null) or (c_Formula_Rec.segment27 is not null) or
6186 	   (c_Formula_Rec.segment28 is not null) or (c_Formula_Rec.segment29 is not null) or (c_Formula_Rec.segment30 is not null))) then
6187     begin
6188       l_type2 := TRUE;
6189     end;
6190     elsif ((c_Formula_Rec.prefix_operator is not null) and
6191 	   (c_Formula_Rec.amount is not null) and
6192 	   (c_Formula_Rec.budget_year_type_id is null) and
6193 	   (c_Formula_Rec.balance_type is null) and
6194 	   (c_Formula_Rec.currency_code is null) and
6195 	   (c_Formula_Rec.postfix_operator is null) and
6196 	  ((c_Formula_Rec.segment1 is null) and (c_Formula_Rec.segment2 is null) and (c_Formula_Rec.segment3 is null) and
6197 	   (c_Formula_Rec.segment4 is null) and (c_Formula_Rec.segment5 is null) and (c_Formula_Rec.segment6 is null) and
6198 	   (c_Formula_Rec.segment7 is null) and (c_Formula_Rec.segment8 is null) and (c_Formula_Rec.segment9 is null) and
6199 	   (c_Formula_Rec.segment10 is null) and (c_Formula_Rec.segment11 is null) and (c_Formula_Rec.segment12 is null) and
6200 	   (c_Formula_Rec.segment13 is null) and (c_Formula_Rec.segment14 is null) and (c_Formula_Rec.segment15 is null) and
6201 	   (c_Formula_Rec.segment16 is null) and (c_Formula_Rec.segment17 is null) and (c_Formula_Rec.segment18 is null) and
6202 	   (c_Formula_Rec.segment19 is null) and (c_Formula_Rec.segment20 is null) and (c_Formula_Rec.segment21 is null) and
6203 	   (c_Formula_Rec.segment22 is null) and (c_Formula_Rec.segment23 is null) and (c_Formula_Rec.segment24 is null) and
6204 	   (c_Formula_Rec.segment25 is null) and (c_Formula_Rec.segment26 is null) and (c_Formula_Rec.segment27 is null) and
6205 	   (c_Formula_Rec.segment28 is null) and (c_Formula_Rec.segment29 is null) and (c_Formula_Rec.segment30 is null))) then
6206     begin
6207       l_type3 := TRUE;
6208     end;
6209     elsif ((c_Formula_Rec.prefix_operator is not null) and
6210 	   (c_Formula_Rec.postfix_operator is not null) and
6211 	   (c_Formula_Rec.balance_type in ('O', 'C')) and
6212 	   (c_Formula_Rec.currency_code is not null) and
6213 	   (c_Formula_Rec.amount is not null) and
6214 	  ((c_Formula_Rec.budget_year_type_id is null) and
6215 	   (c_Formula_Rec.segment1 is null) and (c_Formula_Rec.segment2 is null) and (c_Formula_Rec.segment3 is null) and
6216 	   (c_Formula_Rec.segment4 is null) and (c_Formula_Rec.segment5 is null) and (c_Formula_Rec.segment6 is null) and
6217 	   (c_Formula_Rec.segment7 is null) and (c_Formula_Rec.segment8 is null) and (c_Formula_Rec.segment9 is null) and
6218 	   (c_Formula_Rec.segment10 is null) and (c_Formula_Rec.segment11 is null) and (c_Formula_Rec.segment12 is null) and
6219 	   (c_Formula_Rec.segment13 is null) and (c_Formula_Rec.segment14 is null) and (c_Formula_Rec.segment15 is null) and
6220 	   (c_Formula_Rec.segment16 is null) and (c_Formula_Rec.segment17 is null) and (c_Formula_Rec.segment18 is null) and
6221 	   (c_Formula_Rec.segment19 is null) and (c_Formula_Rec.segment20 is null) and (c_Formula_Rec.segment21 is null) and
6222 	   (c_Formula_Rec.segment22 is null) and (c_Formula_Rec.segment23 is null) and (c_Formula_Rec.segment24 is null) and
6223 	   (c_Formula_Rec.segment25 is null) and (c_Formula_Rec.segment26 is null) and (c_Formula_Rec.segment27 is null) and
6224 	   (c_Formula_Rec.segment28 is null) and (c_Formula_Rec.segment29 is null) and (c_Formula_Rec.segment30 is null))) then
6225     begin
6226 
6227       if p_summ_flag then
6228       begin
6229 	message_token('CONSTRAINT', p_constraint_name);
6230 	add_message('PSB', 'PSB_INVALID_CONS_FORMULA');
6231 	raise FND_API.G_EXC_ERROR;
6232       end;
6233       else
6234 	l_type4 := TRUE;
6235       end if;
6236 
6237     end;
6238     else
6239     begin
6240       message_token('CONSTRAINT', p_constraint_name);
6241       add_message('PSB', 'PSB_INVALID_CONS_FORMULA');
6242       raise FND_API.G_EXC_ERROR;
6243     end;
6244     end if;
6245 
6246     if l_type1 then
6247     begin
6248 
6249       l_ytd_amount := Compute_Line_Total (p_worksheet_id => p_worksheet_id,
6250 					  p_sp_exists => p_sp_exists,
6251 					  p_flex_mapping_set_id => p_flex_mapping_set_id,
6252 					  p_budget_calendar_id => p_budget_calendar_id,
6253 					  p_ccid => p_ccid,
6254 					  p_budget_year_type_id => c_Formula_Rec.budget_year_type_id,
6255 					  p_balance_type => c_Formula_Rec.balance_type,
6256 					  p_currency_code => c_Formula_Rec.currency_code,
6257                               /* start bug 4256345 */
6258 	  				  p_budget_period_id => p_budget_period_id,
6259   					  p_stage_set_id     => p_stage_Set_id,
6260   					  p_current_stage_seq => p_current_stage_seq
6261   					  /* end bug   4256345 */
6262                               );
6263 
6264       if c_Formula_Rec.postfix_operator = '+' then
6265 	l_line_total := l_ytd_amount + c_Formula_Rec.amount;
6266       elsif c_Formula_Rec.postfix_operator = '-' then
6267 	l_line_total := l_ytd_Amount - c_Formula_Rec.amount;
6268       elsif c_Formula_Rec.postfix_operator = '*' then
6269 	l_line_total := l_ytd_amount * c_Formula_Rec.amount;
6270       elsif c_Formula_Rec.postfix_operator = '/' then
6271       begin
6272 
6273 	-- Avoid divide-by-zero error
6274 
6275 	if nvl(c_Formula_Rec.amount, 0) = 0 then
6276 	  l_line_total := 0;
6277 	else
6278 	  l_line_total := l_ytd_amount / c_Formula_Rec.amount;
6279 	end if;
6280 
6281       end;
6282       else
6283       begin
6284 	message_token('CONSTRAINT', p_constraint_name);
6285 	add_message('PSB', 'PSB_INVALID_CONS_FORMULA');
6286 	raise FND_API.G_EXC_ERROR;
6287       end;
6288       end if;
6289 
6290     end;
6291     elsif l_type2 then
6292     begin
6293 
6294       for l_index in 1..g_num_segs loop
6295 
6296 	if ((g_seg_name(l_index) = 'SEGMENT1') and
6297 	    (c_Formula_Rec.segment1 is not null)) then
6298 	  l_seg_val(l_index) := c_Formula_Rec.segment1;
6299 
6300 	elsif ((g_seg_name(l_index) = 'SEGMENT2') and
6301 	    (c_Formula_Rec.segment2 is not null)) then
6302 	  l_seg_val(l_index) := c_Formula_Rec.segment2;
6303 
6304 	elsif ((g_seg_name(l_index) = 'SEGMENT3') and
6305 	    (c_Formula_Rec.segment3 is not null)) then
6306 	  l_seg_val(l_index) := c_Formula_Rec.segment3;
6307 
6308 	elsif ((g_seg_name(l_index) = 'SEGMENT4') and
6309 	    (c_Formula_Rec.segment4 is not null)) then
6310 	  l_seg_val(l_index) := c_Formula_Rec.segment4;
6311 
6312 	elsif ((g_seg_name(l_index) = 'SEGMENT5') and
6313 	    (c_Formula_Rec.segment5 is not null)) then
6314 	  l_seg_val(l_index) := c_Formula_Rec.segment5;
6315 
6316 	elsif ((g_seg_name(l_index) = 'SEGMENT6') and
6317 	    (c_Formula_Rec.segment6 is not null)) then
6318 	  l_seg_val(l_index) := c_Formula_Rec.segment6;
6319 
6320 	elsif ((g_seg_name(l_index) = 'SEGMENT7') and
6321 	    (c_Formula_Rec.segment7 is not null)) then
6322 	  l_seg_val(l_index) := c_Formula_Rec.segment7;
6323 
6324 	elsif ((g_seg_name(l_index) = 'SEGMENT8') and
6325 	    (c_Formula_Rec.segment8 is not null)) then
6326 	  l_seg_val(l_index) := c_Formula_Rec.segment8;
6327 
6328 	elsif ((g_seg_name(l_index) = 'SEGMENT9') and
6329 	    (c_Formula_Rec.segment9 is not null)) then
6330 	  l_seg_val(l_index) := c_Formula_Rec.segment9;
6331 
6332 	elsif ((g_seg_name(l_index) = 'SEGMENT10') and
6333 	    (c_Formula_Rec.segment10 is not null)) then
6334 	  l_seg_val(l_index) := c_Formula_Rec.segment10;
6335 
6336 	elsif ((g_seg_name(l_index) = 'SEGMENT11') and
6337 	    (c_Formula_Rec.segment11 is not null)) then
6338 	  l_seg_val(l_index) := c_Formula_Rec.segment11;
6339 
6340 	elsif ((g_seg_name(l_index) = 'SEGMENT12') and
6341 	    (c_Formula_Rec.segment12 is not null)) then
6342 	  l_seg_val(l_index) := c_Formula_Rec.segment12;
6343 
6344 	elsif ((g_seg_name(l_index) = 'SEGMENT13') and
6345 	    (c_Formula_Rec.segment13 is not null)) then
6346 	  l_seg_val(l_index) := c_Formula_Rec.segment13;
6347 
6348 	elsif ((g_seg_name(l_index) = 'SEGMENT14') and
6349 	    (c_Formula_Rec.segment14 is not null)) then
6350 	  l_seg_val(l_index) := c_Formula_Rec.segment14;
6351 
6352 	elsif ((g_seg_name(l_index) = 'SEGMENT15') and
6353 	    (c_Formula_Rec.segment15 is not null)) then
6354 	  l_seg_val(l_index) := c_Formula_Rec.segment15;
6355 
6356 	elsif ((g_seg_name(l_index) = 'SEGMENT16') and
6357 	    (c_Formula_Rec.segment16 is not null)) then
6358 	  l_seg_val(l_index) := c_Formula_Rec.segment16;
6359 
6360 	elsif ((g_seg_name(l_index) = 'SEGMENT17') and
6361 	    (c_Formula_Rec.segment17 is not null)) then
6362 	  l_seg_val(l_index) := c_Formula_Rec.segment17;
6363 
6364 	elsif ((g_seg_name(l_index) = 'SEGMENT18') and
6365 	    (c_Formula_Rec.segment18 is not null)) then
6366 	  l_seg_val(l_index) := c_Formula_Rec.segment18;
6367 
6368 	elsif ((g_seg_name(l_index) = 'SEGMENT19') and
6369 	    (c_Formula_Rec.segment19 is not null)) then
6370 	  l_seg_val(l_index) := c_Formula_Rec.segment19;
6371 
6372 	elsif ((g_seg_name(l_index) = 'SEGMENT20') and
6373 	    (c_Formula_Rec.segment20 is not null)) then
6374 	  l_seg_val(l_index) := c_Formula_Rec.segment20;
6375 
6376 	elsif ((g_seg_name(l_index) = 'SEGMENT21') and
6377 	    (c_Formula_Rec.segment21 is not null)) then
6378 	  l_seg_val(l_index) := c_Formula_Rec.segment21;
6379 
6380 	elsif ((g_seg_name(l_index) = 'SEGMENT22') and
6381 	    (c_Formula_Rec.segment22 is not null)) then
6382 	  l_seg_val(l_index) := c_Formula_Rec.segment22;
6383 
6384 	elsif ((g_seg_name(l_index) = 'SEGMENT23') and
6385 	    (c_Formula_Rec.segment23 is not null)) then
6386 	  l_seg_val(l_index) := c_Formula_Rec.segment23;
6387 
6388 	elsif ((g_seg_name(l_index) = 'SEGMENT24') and
6389 	    (c_Formula_Rec.segment24 is not null)) then
6390 	  l_seg_val(l_index) := c_Formula_Rec.segment24;
6391 
6392 	elsif ((g_seg_name(l_index) = 'SEGMENT25') and
6393 	    (c_Formula_Rec.segment25 is not null)) then
6394 	  l_seg_val(l_index) := c_Formula_Rec.segment25;
6395 
6396 	elsif ((g_seg_name(l_index) = 'SEGMENT26') and
6397 	    (c_Formula_Rec.segment26 is not null)) then
6398 	  l_seg_val(l_index) := c_Formula_Rec.segment26;
6399 
6400 	elsif ((g_seg_name(l_index) = 'SEGMENT27') and
6401 	    (c_Formula_Rec.segment27 is not null)) then
6402 	  l_seg_val(l_index) := c_Formula_Rec.segment27;
6403 
6404 	elsif ((g_seg_name(l_index) = 'SEGMENT28') and
6405 	    (c_Formula_Rec.segment28 is not null)) then
6406 	  l_seg_val(l_index) := c_Formula_Rec.segment28;
6407 
6408 	elsif ((g_seg_name(l_index) = 'SEGMENT29') and
6409 	    (c_Formula_Rec.segment29 is not null)) then
6410 	  l_seg_val(l_index) := c_Formula_Rec.segment29;
6411 
6412 	elsif ((g_seg_name(l_index) = 'SEGMENT30') and
6413 	    (c_Formula_Rec.segment30 is not null)) then
6414 	  l_seg_val(l_index) := c_Formula_Rec.segment30;
6415 
6416 	else
6417 	begin
6418 
6419 	  if p_summ_flag then
6420 	    l_type2 := FALSE;
6421 	  else
6422 	    l_seg_val(l_index) := l_ccid_val(l_index);
6423 
6424 /* Bug No 2719865 Start */
6425 	  l_ccid_defined := FALSE;
6426 /* Bug No 2719865 End */
6427 
6428 	  end if;
6429 
6430 	end;
6431 	end if;
6432 
6433       end loop;
6434 
6435       if l_type2 then
6436       begin
6437 
6438 	-- No new Code Combinations are created from the Constraints Module. If a
6439 	-- composed Code Combination does not already exist in GL, it is not created
6440 	-- dynamically
6441 
6442 	l_concat_segments := FND_FLEX_EXT.Concatenate_Segments
6443 				(n_segments => g_num_segs,
6444 				 segments => l_seg_val,
6445 				 delimiter => p_flex_delimiter);
6446 
6447 	if not FND_FLEX_KEYVAL.Validate_Segs
6448 		  (operation => 'FIND_COMBINATION',
6449 		   appl_short_name => 'SQLGL',
6450 		   key_flex_code => 'GL#',
6451 		   structure_number => p_flex_code,
6452 		   concat_segments => l_concat_segments) then
6453 
6454 /* Bug No 2719865 Start */
6455 	  if (g_first_ccid) then
6456   	    FND_MSG_PUB.Add;
6457 	    g_first_ccid := FALSE;
6458 	    message_token('CONSTRAINT', p_constraint_name);
6459 	    message_token('CCID', l_concat_segments);
6460 	    add_message('PSB', 'PSB_DISABLED_ACCT_CONSTRAINT');
6461 	  end if;
6462 
6463 	  if ((not l_ccid_defined) and (not g_first_ccid)) then
6464   	    FND_MSG_PUB.Add;
6465 	    message_token('CONSTRAINT', p_constraint_name);
6466 	    message_token('CCID', l_concat_segments);
6467 	    add_message('PSB', 'PSB_DISABLED_ACCT_CONSTRAINT');
6468 	  end if;
6469 /* Bug No 2719865 End */
6470 
6471 	else
6472 	begin
6473 
6474 	  l_ccid := FND_FLEX_KEYVAL.Combination_ID;
6475 
6476 	  if p_sp_exists then
6477 	  begin
6478 
6479 	    l_ytd_amount := Compute_Line_Total (p_worksheet_id => p_worksheet_id,
6480 						p_sp_exists => p_sp_exists,
6481 						p_flex_mapping_set_id => p_flex_mapping_set_id,
6482 						p_budget_calendar_id => p_budget_calendar_id,
6483 						p_ccid => l_ccid,
6484 						p_budget_year_type_id => c_Formula_Rec.budget_year_type_id,
6485 						p_balance_type => c_Formula_Rec.balance_type,
6486 						p_currency_code => c_Formula_Rec.currency_code,
6487 					    /* bug no 4256345 */
6488 	  				    p_budget_period_id => p_budget_period_id,
6489   					    p_stage_set_id     => p_stage_Set_id,
6490   					    p_current_stage_seq => p_current_stage_seq
6491   					    /* bug no 4256345 */
6492 					    );
6493 
6494 	    if c_Formula_Rec.postfix_operator = '+' then
6495 	      l_line_total := l_ytd_amount + c_Formula_Rec.amount;
6496 	    elsif c_Formula_Rec.postfix_operator = '-' then
6497 	      l_line_total := l_ytd_amount - c_Formula_Rec.amount;
6498 	    elsif c_Formula_Rec.postfix_operator = '*' then
6499 	      l_line_total := l_ytd_amount * c_Formula_Rec.amount;
6500 	    elsif c_Formula_Rec.postfix_operator = '/' then
6501 	    begin
6502 
6503 	      -- Avoid divide-by-zero error
6504 
6505 	      if nvl(c_Formula_Rec.amount, 0) = 0 then
6506 		l_line_total := 0;
6507 	      else
6508 		l_line_total := l_ytd_amount / c_Formula_Rec.amount;
6509 	      end if;
6510 
6511 	    end;
6512 	    else
6513 	      l_line_total := l_ytd_amount;
6514 	    end if;
6515 
6516 	  end;
6517 	  end if;
6518 
6519 	end;
6520 	end if;
6521 
6522       end;
6523       else
6524       begin
6525 	message_token('CONSTRAINT', p_constraint_name);
6526 	add_message('PSB', 'PSB_INVALID_CONS_FORMULA');
6527 	raise FND_API.G_EXC_ERROR;
6528       end;
6529       end if;
6530 
6531     end;
6532     elsif l_type3 then
6533       l_line_total := c_Formula_Rec.amount;
6534 
6535     /* Bug 2321186 Start*/
6536     ELSIF l_type5 then
6537 
6538     FOR c_Accounts_Rec in c_Accounts LOOP
6539 
6540       l_ytd_amount := Compute_Line_Total (p_worksheet_id => p_worksheet_id,
6541 						p_sp_exists => p_sp_exists,
6542 						p_flex_mapping_set_id => p_flex_mapping_set_id,
6543 						p_budget_calendar_id => p_budget_calendar_id,
6544 						p_ccid => c_Accounts_Rec.code_combination_id,
6545 						p_budget_year_type_id => c_Formula_Rec.budget_year_type_id,
6546 						p_balance_type => c_Formula_Rec.balance_type,
6547 						p_currency_code => c_Formula_Rec.currency_code,
6548 						/* start bug 4256345 */
6549 	  				    p_budget_period_id => p_budget_period_id,
6550   					    p_stage_set_id     => p_stage_Set_id,
6551   					    p_current_stage_seq => p_current_stage_seq
6552   					    /* end bug 4256345 */
6553 					    );
6554 
6555 
6556       IF l_ytd_amount IS NOT NULL THEN
6557          l_account_type_factor := Compute_Account_Type_Factor(
6558                                    p_ccid         => c_Accounts_Rec.code_combination_id,
6559                                    p_worksheet_id => p_worksheet_id
6560                                                              );
6561 
6562          l_sum_amount := l_sum_amount + l_account_type_factor * l_ytd_amount;
6563       END IF;
6564 
6565     END LOOP;
6566 
6567     IF l_sum_amount is NOT NULL THEN
6568       l_correction_factor := Compute_Correction_Factor(
6569                                       p_worksheet_id => p_worksheet_id,
6570                                       p_constraint_id => p_constraint_id);
6571 
6572       l_sum_amount        := l_correction_factor * l_sum_amount;
6573     END IF;
6574 
6575       IF c_Formula_Rec.postfix_operator = '+' THEN
6576 	l_line_total := l_sum_amount + c_Formula_Rec.amount;
6577       ELSIF c_Formula_Rec.postfix_operator = '-' THEN
6578 	l_line_total := l_sum_Amount - c_Formula_Rec.amount;
6579       ELSIF c_Formula_Rec.postfix_operator = '*' THEN
6580 	l_line_total := l_sum_amount * c_Formula_Rec.amount;
6581       ELSIF c_Formula_Rec.postfix_operator = '/' THEN
6582 
6583         BEGIN
6584 
6585 	-- Avoid divide-by-zero error
6586 	IF nvl(c_Formula_Rec.amount, 0) = 0 THEN
6587 	  l_line_total := 0;
6588 	ELSE
6589 	  l_line_total := l_sum_amount / c_Formula_Rec.amount;
6590 	END IF;
6591 
6592         END;
6593       ELSE
6594         BEGIN
6595 	  message_token('CONSTRAINT', p_constraint_name);
6596 	  add_message('PSB', 'PSB_INVALID_CONS_FORMULA');
6597 	  raise FND_API.G_EXC_ERROR;
6598         END;
6599       END IF;
6600       /* Bug 2321186 End */
6601     END IF;
6602 
6603     if c_Formula_Rec.prefix_operator in ('=', '<>', '<=', '>=', '<', '>') then
6604       l_cons_total := l_line_total;
6605     elsif c_Formula_Rec.prefix_operator = '+' then
6606       l_cons_total := l_cons_total + l_line_total;
6607     elsif c_Formula_Rec.prefix_operator = '-' then
6608       l_cons_total := l_cons_total - l_line_total;
6609     elsif c_Formula_Rec.prefix_operator = '*' then
6610       l_cons_total := l_cons_total * l_line_total;
6611     elsif c_Formula_Rec.prefix_operator = '/' then
6612     begin
6613 
6614       -- Avoid divide-by-zero error
6615 
6616       if nvl(l_line_total, 0) = 0 then
6617 	l_cons_total := 0;
6618       else
6619 	l_cons_total := l_cons_total / l_line_total;
6620       end if;
6621 
6622     end;
6623     end if;
6624 
6625   end loop;
6626 
6627 
6628   -- Compute Sum of Account Sets or CCID assigned to the Constraint
6629 
6630   if not p_summ_flag then
6631   begin
6632 
6633     l_ytd_amount := Compute_Account_Total (p_worksheet_id => p_worksheet_id,
6634 					   p_sp_exists => p_sp_exists,
6635 					   p_flex_mapping_set_id => p_flex_mapping_set_id,
6636 					   p_ccid => p_ccid,
6637 					   p_budget_year_id => p_budget_year_id,
6638 					   p_budget_year_type_id => p_budget_year_type_id,
6639 					   p_currency_code => p_currency_code);
6640 
6641     l_accset_total := l_ytd_amount;
6642 
6643   end;
6644   else
6645   begin
6646 
6647     l_accset_total := Compute_Sum_Total (p_worksheet_id => p_worksheet_id,
6648 					 p_sp_exists => p_sp_exists,
6649 					 p_constraint_id => p_constraint_id,
6650 					 p_flex_mapping_set_id => p_flex_mapping_set_id,
6651 					 p_budget_year_id => p_budget_year_id,
6652 					 p_budget_year_type_id => p_budget_year_type_id,
6653 					 p_currency_code => p_currency_code,
6654 					 p_budget_group_id => p_budget_group_id);
6655 
6656   end;
6657   end if;
6658 
6659   if l_accset_total is not null then
6660   begin
6661 
6662     if l_operator = '<=' then
6663 
6664       if l_accset_total <= l_cons_total then
6665 	l_cons_failed := TRUE;
6666       end if;
6667 
6668     elsif l_operator = '>=' then
6669 
6670       if l_accset_total >= l_cons_total then
6671 	l_cons_failed := TRUE;
6672       end if;
6673 
6674     elsif l_operator = '<' then
6675 
6676       if l_accset_total < l_cons_total then
6677 	l_cons_failed := TRUE;
6678       end if;
6679 
6680     elsif l_operator = '>' then
6681 
6682       if l_accset_total > l_cons_total then
6683 	l_cons_failed := TRUE;
6684       end if;
6685 
6686     elsif l_operator = '=' then
6687 
6688       if l_accset_total = l_cons_total then
6689 	l_cons_failed := TRUE;
6690       end if;
6691 
6692     elsif l_operator = '<>' then
6693 
6694       if l_accset_total <> l_cons_total then
6695 	l_cons_failed := TRUE;
6696       end if;
6697 
6698     end if;
6699 
6700     if l_cons_failed then
6701     begin
6702 
6703       if nvl(p_severity_level, -1) >= p_constraint_threshold then
6704 	p_constraint_validation_status := 'F';
6705       else
6706 	p_constraint_validation_status := 'E';
6707       end if;
6708 
6709       l_userid := FND_GLOBAL.USER_ID;
6710       l_reqid := FND_GLOBAL.CONC_REQUEST_ID;
6711 
6712       message_token('CONSTRAINT_SET', p_constraint_set_name);
6713       message_token('THRESHOLD', p_constraint_threshold);
6714       message_token('CONSTRAINT', p_constraint_name);
6715       message_token('SEVERITY_LEVEL', p_severity_level);
6716       message_token('ASSIGNMENT_VALUE', l_accset_total);
6717       message_token('OPERATOR', l_operator);
6718       message_token('FORMULA_VALUE', l_cons_total);
6719 
6720       if p_summ_flag then
6721 	message_token('NAME', p_constraint_name);
6722       else
6723       begin
6724 
6725 	l_mapped_ccid := Map_Account
6726 			    (p_flex_mapping_set_id => p_flex_mapping_set_id,
6727 			     p_ccid => p_ccid,
6728 			     p_budget_year_type_id => p_budget_year_type_id);
6729 
6730 	l_concat_segments := FND_FLEX_EXT.Get_Segs
6731 				(application_short_name => 'SQLGL',
6732 				 key_flex_code => 'GL#',
6733 				 structure_number => p_flex_code,
6734 				 combination_id => l_mapped_ccid);
6735 
6736 	message_token('NAME', l_concat_segments);
6737 
6738       end;
6739       end if;
6740 
6741       message_token('YEAR', p_budget_year_name);
6742       add_message('PSB', 'PSB_CONSTRAINT_FAILURE');
6743 
6744       l_description := FND_MSG_PUB.Get
6745 			  (p_encoded => FND_API.G_FALSE);
6746       FND_MSG_PUB.Delete_Msg;
6747 
6748       -- Constraint Validation failures are logged in PSB_ERROR_MESSAGES and
6749       -- viewed using a Form called from the Modify Worksheet module
6750 
6751       insert into PSB_ERROR_MESSAGES
6752 		 (Concurrent_Request_ID,
6753 		  Process_ID,
6754 		  Source_Process,
6755 		  Description,
6756 		  Creation_Date,
6757 		  Created_By)
6758 	  values (l_reqid,
6759 		  p_worksheet_id,
6760 		  'WORKSHEET_CREATION',
6761 		  l_description,
6762 		  sysdate,
6763 		  l_userid);
6764 
6765     end;
6766     else
6767       p_constraint_validation_status := 'S';
6768     end if;
6769 
6770   end;
6771   end if;
6772 
6773 
6774   -- Initialize API return status to success
6775 
6776   p_return_status := FND_API.G_RET_STS_SUCCESS;
6777 
6778 
6779 EXCEPTION
6780 
6781    when FND_API.G_EXC_ERROR then
6782      p_return_status := FND_API.G_RET_STS_ERROR;
6783      FND_MSG_PUB.Count_And_Get (p_count => l_msg_count,
6784 				p_data => l_msg_data);
6785 
6786 
6787    when FND_API.G_EXC_UNEXPECTED_ERROR then
6788      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6789      FND_MSG_PUB.Count_And_Get (p_count => l_msg_count,
6790 				p_data => l_msg_data);
6791 
6792    when OTHERS then
6793      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6794 
6795      if FND_MSG_PUB.Check_Msg_Level
6796        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
6797      then
6798        FND_MSG_PUB.Add_Exc_Msg
6799 	  (p_pkg_name => G_PKG_NAME,
6800 	   p_procedure_name => l_api_name);
6801      end if;
6802 
6803 END Process_Constraint;
6804 
6805 /* ----------------------------------------------------------------------- */
6806 
6807 -- Process Constraint that has the detailed flag set for individual CCIDs
6808 
6809 PROCEDURE Apply_Detailed_Account
6810 ( p_return_status                 OUT  NOCOPY  VARCHAR2,
6811   p_constraint_validation_status  OUT  NOCOPY  VARCHAR2,
6812   p_worksheet_id                  IN   NUMBER,
6813   p_budget_group_id               IN   NUMBER,
6814   p_sp_exists                     IN   BOOLEAN,
6815   p_constraint_set_name           IN   VARCHAR2,
6816   p_constraint_threshold          IN   NUMBER,
6817   p_constraint_id                 IN   NUMBER,
6818   p_constraint_name               IN   VARCHAR2,
6819   p_flex_code                     IN   NUMBER,
6820   p_flex_delimiter                IN   VARCHAR2,
6821   p_budget_calendar_id            IN   NUMBER,
6822   p_budget_year_id                IN   NUMBER,
6823   p_budget_year_name              IN   VARCHAR2,
6824   p_currency_code                 IN   VARCHAR2,
6825   p_severity_level                IN   NUMBER,
6826   p_flex_mapping_set_id           IN   NUMBER,
6827   p_budget_year_type_id           IN   NUMBER,
6828   /* start bug 4256345 */
6829   p_budget_period_id			  IN   NUMBER,
6830   p_stage_set_id				  IN   NUMBER,
6831   p_current_stage_seq			  IN   NUMBER
6832   /* end bug 4256345 */
6833 ) IS
6834 
6835   l_return_status                 VARCHAR2(1);
6836   l_msg_data                      VARCHAR2(2000);
6837   l_msg_count                     NUMBER;
6838 
6839   l_cons_validation_status        VARCHAR2(1) := 'S';
6840   l_detailed_status               VARCHAR2(1);
6841 
6842   -- CCIDs assigned to the Constraint : select CCIDs that also belong to the Budget Group Hierarchy
6843 
6844   cursor c_CCID is
6845     select a.code_combination_id ccid
6846       from PSB_BUDGET_ACCOUNTS a,
6847 	   PSB_SET_RELATIONS_V b
6848      where exists
6849 	  (select 1
6850 	     from PSB_BUDGET_ACCOUNTS c,
6851 		  PSB_SET_RELATIONS_V d
6852 	    where c.account_position_set_id = d.account_position_set_id
6853 	      and c.code_combination_id = a.code_combination_id
6854 	      and d.account_or_position_type = 'A'
6855 	      and exists
6856 		 (select 1
6857 		    from psb_budget_groups e
6858 		   where e.budget_group_type = 'R'
6859 		     and e.effective_start_date <= g_startdate_pp
6860 		     and (e.effective_end_date is null or e.effective_end_date >= g_enddate_cy)
6861 		     and e.budget_group_id = d.budget_group_id
6862 		   start with e.budget_group_id = p_budget_group_id
6863 		 connect by prior e.budget_group_id = e.parent_budget_group_id))
6864        and a.account_position_set_id = b.account_position_set_id
6865        and b.account_or_position_type = 'A'
6866        and b.constraint_id = p_constraint_id;
6867 
6868   l_api_name          CONSTANT VARCHAR2(30)   := 'Apply_Detailed_Account';
6869 
6870 BEGIN
6871 
6872   for c_CCID_Rec in c_CCID loop
6873 
6874     Process_Constraint
6875 	   (p_worksheet_id => p_worksheet_id,
6876 	    p_sp_exists => p_sp_exists,
6877 	    p_constraint_set_name => p_constraint_set_name,
6878 	    p_constraint_threshold => p_constraint_threshold,
6879 	    p_constraint_id => p_constraint_id,
6880 	    p_constraint_name => p_constraint_name,
6881 	    p_flex_code => p_flex_code,
6882 	    p_flex_delimiter => p_flex_delimiter,
6883 	    p_ccid => c_CCID_Rec.ccid,
6884 	    p_budget_calendar_id => p_budget_calendar_id,
6885 	    p_budget_year_id => p_budget_year_id,
6886 	    p_budget_year_name => p_budget_year_name,
6887 	    p_currency_code => p_currency_code,
6888 	    p_severity_level => p_severity_level,
6889 	    p_summ_flag => FALSE,
6890 	    p_constraint_validation_status => l_detailed_status,
6891 	    p_flex_mapping_set_id => p_flex_mapping_set_id,
6892 	    p_budget_year_type_id => p_budget_year_type_id,
6893     	    p_budget_group_id => p_budget_group_id,
6894 	    p_return_status => l_return_status,
6895             /* bug no 4256345 */
6896     	    p_budget_period_id  => p_budget_period_id,
6897     	    p_stage_set_id      => p_stage_set_id,
6898     	    p_current_Stage_seq => p_current_stage_seq
6899     	    /* bug no 4256345 */
6900             );
6901 
6902     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
6903       raise FND_API.G_EXC_ERROR;
6904     end if;
6905 
6906     if ((l_cons_validation_status = 'S') and
6907 	(l_detailed_status <> 'S')) then
6908       l_cons_validation_status := l_detailed_status;
6909     elsif ((l_cons_validation_status = 'E') and
6910 	   (l_detailed_status = 'F')) then
6911       l_cons_validation_status := l_detailed_status;
6912     end if;
6913 
6914   end loop;
6915 
6916 
6917   -- Initialize API return status to success
6918 
6919   p_constraint_validation_status := l_cons_validation_status;
6920   p_return_status := FND_API.G_RET_STS_SUCCESS;
6921 
6922 
6923 EXCEPTION
6924 
6925    when FND_API.G_EXC_ERROR then
6926      p_return_status := FND_API.G_RET_STS_ERROR;
6927      FND_MSG_PUB.Count_And_Get (p_count => l_msg_count,
6928 				p_data => l_msg_data);
6929 
6930 
6931    when FND_API.G_EXC_UNEXPECTED_ERROR then
6932      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6933      FND_MSG_PUB.Count_And_Get (p_count => l_msg_count,
6934 				p_data => l_msg_data);
6935 
6936    when OTHERS then
6937      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6938 
6939      if FND_MSG_PUB.Check_Msg_Level
6940        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
6941      then
6942        FND_MSG_PUB.Add_Exc_Msg
6943 	  (p_pkg_name => G_PKG_NAME,
6944 	   p_procedure_name => l_api_name);
6945      end if;
6946 
6947 END Apply_Detailed_Account;
6948 
6949 /* ----------------------------------------------------------------------- */
6950 
6951 -- Constraint applies to the 'E'stimate Line Items for 'CY' and 'PP' Budget
6952 -- Years for the Currency in the Constraint Definition
6953 
6954 -- The following restrictions apply to Constraints :
6955 --
6956 -- For Type1 Lines, the 'Detailed' Flag must be set
6957 --
6958 -- For Type2 Lines, the 'Detailed' Flag must be set for partial Code Combinations
6959 -- to be entered in the Formula Section. If the 'Detailed' Flag is not set, the
6960 -- full Code Combination must be entered in the Formula Section
6961 
6962 -- p_validation_status has the following values : 'F'atal, 'E'rror, 'W'arning,
6963 -- 'S'uccess
6964 
6965 PROCEDURE Apply_Account_Constraints
6966 ( p_return_status         OUT  NOCOPY  VARCHAR2,
6967   p_validation_status     OUT  NOCOPY  VARCHAR2,
6968   p_worksheet_id          IN   NUMBER,
6969   p_flex_mapping_set_id   IN   NUMBER,
6970   p_budget_group_id       IN   NUMBER,
6971   p_flex_code             IN   NUMBER,
6972   p_func_currency         IN   VARCHAR2,
6973   p_constraint_set_id     IN   NUMBER,
6974   p_constraint_set_name   IN   VARCHAR2,
6975   p_constraint_threshold  IN   NUMBER,
6976   p_budget_calendar_id    IN   NUMBER
6977 ) IS
6978 
6979   l_return_status              VARCHAR2(1);
6980   l_msg_data                   VARCHAR2(2000);
6981   l_msg_count                  NUMBER;
6982 
6983   l_year_index                 PLS_INTEGER;
6984 
6985   l_cons_validation_status     VARCHAR2(1);
6986   l_consset_validation_status  VARCHAR2(1) := 'S';
6987 
6988   l_flex_delimiter             VARCHAR2(1);
6989 
6990   l_sp_exists                  BOOLEAN := FALSE;
6991 
6992   cursor c_Constraint (Year_Start_Date DATE,
6993 		       Year_End_Date DATE) is
6994     select constraint_id,
6995 	   name,
6996 	   currency_code,
6997 	   severity_level,
6998 	   effective_start_date,
6999 	   effective_end_date,
7000 	   constraint_detailed_flag
7001       from PSB_CONSTRAINT_ASSIGNMENTS_V
7002      where constraint_type = 'ACCOUNT'
7003        and (((effective_start_date <= Year_End_Date)
7004 	 and (effective_end_date is null))
7005 	 or ((effective_start_date between Year_Start_Date and Year_End_Date)
7006 	  or (effective_end_date between Year_Start_Date and Year_End_Date)
7007 	  or ((effective_start_date < Year_Start_Date)
7008 	  and (effective_end_date > Year_End_Date))))
7009        and constraint_set_id = p_constraint_set_id
7010      order by severity_level desc,
7011 	      effective_start_date,
7012 	      effective_end_date;
7013 
7014   -- Check whether Constraints should be applied for specific Service Packages
7015 
7016   cursor c_SP is
7017     select 'x'
7018       from dual
7019      where exists
7020 	  (select 'Service Package Exists'
7021 	     from PSB_WS_SUBMIT_SERVICE_PACKAGES
7022 	    where worksheet_id = p_worksheet_id);
7023 
7024 
7025   l_api_name          CONSTANT VARCHAR2(30)   := 'Apply_Account_Constraints';
7026 
7027   /* start bug 4256345 */
7028   l_stage_set_id        NUMBER;
7029   l_current_stage_seq   NUMBER;
7030   TYPE l_budget_period_tbl_type IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
7031   l_budget_period_tbl l_budget_period_tbl_type;
7032   /* end bug  4256345 */
7033 
7034 
7035 BEGIN
7036 
7037   l_flex_delimiter := FND_FLEX_EXT.Get_Delimiter
7038 			 (application_short_name => 'SQLGL',
7039 			  key_flex_code => 'GL#',
7040 			  structure_number => p_flex_code);
7041 
7042   for c_SP_Rec in c_SP loop
7043     l_sp_exists := TRUE;
7044   end loop;
7045 
7046   /* start bug 4256345 */
7047   FOR l_stage_rec IN ( SELECT stage_set_id, current_stage_seq
7048   					   FROM   psb_worksheets
7049   					   WHERE  worksheet_id = p_worksheet_id)
7050   LOOP
7051     l_stage_set_id := l_stage_rec.stage_set_id;
7052     l_current_stage_seq := l_stage_rec.current_Stage_seq;
7053   END LOOP;
7054 
7055   FOR l_budget_period_rec IN (select budget_period_id, budget_year_type_id
7056        						  from psb_budget_periods
7057        					      where budget_calendar_id = p_budget_calendar_id
7058        					      and budget_period_type = 'Y'
7059        					      and parent_budget_period_id is null
7060   						     )
7061   LOOP
7062     l_budget_period_tbl(l_budget_period_rec.budget_year_type_id) :=
7063                     l_budget_period_rec.budget_period_id;
7064   END LOOP;
7065   /* end bug 4256345 */
7066 
7067 
7068   for l_year_index in 1..g_num_budget_years loop
7069 
7070     if (g_budget_years(l_year_index).year_type in ('CY', 'PP')) then
7071     begin
7072 
7073       for c_Constraint_Rec in c_Constraint (g_budget_years(l_year_index).start_date,
7074 					    g_budget_years(l_year_index).end_date) loop
7075 
7076 	if ((c_Constraint_Rec.constraint_detailed_flag is null) or
7077 	    (c_Constraint_Rec.constraint_detailed_flag = 'N')) then
7078 	begin
7079 
7080 	  Process_Constraint
7081 		 (p_worksheet_id => p_worksheet_id,
7082 		  p_sp_exists => l_sp_exists,
7083 		  p_constraint_set_name => p_constraint_set_name,
7084 		  p_constraint_threshold => p_constraint_threshold,
7085 		  p_constraint_id => c_Constraint_Rec.constraint_id,
7086 		  p_constraint_name => c_Constraint_Rec.name,
7087 		  p_flex_code => p_flex_code,
7088 		  p_flex_delimiter => l_flex_delimiter,
7089 		  p_budget_calendar_id => p_budget_calendar_id,
7090 		  p_budget_year_id => g_budget_years(l_year_index).budget_year_id,
7091 		  p_budget_year_name => g_budget_years(l_year_index).year_name,
7092 		  p_currency_code => nvl(c_Constraint_Rec.currency_code, p_func_currency),
7093 		  p_severity_level => c_Constraint_Rec.severity_level,
7094 		  p_summ_flag => TRUE,
7095 		  p_constraint_validation_status => l_cons_validation_status,
7096 		  p_flex_mapping_set_id => p_flex_mapping_set_id,
7097 		  p_budget_year_type_id => g_budget_years(l_year_index).budget_year_type_id,
7098   		  p_budget_group_id => p_budget_group_id,
7099                   /* start bug 4256345 */
7100 		  p_budget_period_id    => l_budget_period_tbl(g_budget_years(l_year_index).budget_year_type_id),
7101 		  p_stage_set_id        => l_stage_set_id,
7102 		  p_current_stage_seq   => l_current_stage_seq,
7103 		  /* end bug  4256345 */
7104 		  p_return_status => l_return_status);
7105 
7106 	  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7107 	    raise FND_API.G_EXC_ERROR;
7108 	  end if;
7109 
7110 	  -- Assign a proper validation status for the Constraint Set based on the validation
7111 	  -- status for the individual Constraints
7112 
7113 	  if ((l_consset_validation_status = 'S') and
7114 	      (l_cons_validation_status <> 'S')) then
7115 	    l_consset_validation_status := l_cons_validation_status;
7116 	  elsif ((l_consset_validation_status = 'E') and
7117 		 (l_cons_validation_status = 'F')) then
7118 	    l_consset_validation_status := l_cons_validation_status;
7119 	  elsif ((l_consset_validation_status = 'W') and
7120 		 (l_cons_validation_status in ('F', 'E'))) then
7121 	    l_consset_validation_status := l_cons_validation_status;
7122 	  end if;
7123 
7124 	end;
7125 	else
7126 	begin
7127 
7128 	  -- For a Constraint with the detailed flag set, call this procedure which
7129 	  -- processes constraints for individual CCIDs. This is to avoid static
7130 	  -- binding
7131 
7132 	  Apply_Detailed_Account
7133 	       (p_return_status => l_return_status,
7134 		p_constraint_validation_status => l_cons_validation_status,
7135 		p_worksheet_id => p_worksheet_id,
7136 		p_budget_group_id => p_budget_group_id,
7137 		p_sp_exists => l_sp_exists,
7138 		p_constraint_set_name => p_constraint_set_name,
7139 		p_constraint_threshold => p_constraint_threshold,
7140 		p_constraint_id => c_Constraint_Rec.constraint_id,
7141 		p_constraint_name => c_Constraint_Rec.name,
7142 		p_flex_code => p_flex_code,
7143 		p_flex_delimiter => l_flex_delimiter,
7144 		p_budget_calendar_id => p_budget_calendar_id,
7145 		p_budget_year_id => g_budget_years(l_year_index).budget_year_id,
7146 		p_budget_year_name => g_budget_years(l_year_index).year_name,
7147 		p_currency_code => nvl(c_Constraint_Rec.currency_code, p_func_currency),
7148 		p_severity_level => c_Constraint_Rec.severity_level,
7149 		p_flex_mapping_set_id => p_flex_mapping_set_id,
7150 		p_budget_year_type_id => g_budget_years(l_year_index).budget_year_type_id,
7151     		/* start bug 4256345 */
7152 		p_budget_period_id    => l_budget_period_tbl(g_budget_years(l_year_index).budget_year_type_id),
7153 		p_stage_set_id        => l_stage_set_id,
7154 		p_current_stage_seq   => l_current_stage_seq
7155 		/* end bug  4256345 */
7156 		);
7157 
7158 	  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7159 	    raise FND_API.G_EXC_ERROR;
7160 	  end if;
7161 
7162 	  -- Assign a proper validation status for the Constraint Set based on the validation
7163 	  -- status for the individual Constraints
7164 
7165 	  if ((l_consset_validation_status = 'S') and
7166 	      (l_cons_validation_status <> 'S')) then
7167 	    l_consset_validation_status := l_cons_validation_status;
7168 	  elsif ((l_consset_validation_status = 'E') and
7169 		 (l_cons_validation_status = 'F')) then
7170 	    l_consset_validation_status := l_cons_validation_status;
7171 	  elsif ((l_consset_validation_status = 'W') and
7172 		 (l_cons_validation_status in ('F', 'E'))) then
7173 	    l_consset_validation_status := l_cons_validation_status;
7174 	  end if;
7175 
7176 	end;
7177 	end if;
7178 
7179       end loop;
7180 
7181     end;
7182     end if;
7183 
7184   end loop;
7185 
7186 
7187   -- Initialize API return status to success
7188 
7189   p_validation_status := l_consset_validation_status;
7190   p_return_status := FND_API.G_RET_STS_SUCCESS;
7191 
7192 
7193 EXCEPTION
7194 
7195 
7196    when FND_API.G_EXC_ERROR then
7197      p_return_status := FND_API.G_RET_STS_ERROR;
7198      FND_MSG_PUB.Count_And_Get (p_count => l_msg_count,
7199 				p_data => l_msg_data);
7200 
7201 
7202    when FND_API.G_EXC_UNEXPECTED_ERROR then
7203      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7204      FND_MSG_PUB.Count_And_Get (p_count => l_msg_count,
7205 				p_data => l_msg_data);
7206 
7207    when OTHERS then
7208      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7209 
7210      if FND_MSG_PUB.Check_Msg_Level
7211        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
7212      then
7213        FND_MSG_PUB.Add_Exc_Msg
7214 	  (p_pkg_name => G_PKG_NAME,
7215 	   p_procedure_name => l_api_name);
7216      end if;
7217 
7218      FND_MSG_PUB.Count_And_Get (p_count => l_msg_count,
7219 				p_data => l_msg_data);
7220 
7221 END Apply_Account_Constraints;
7222 /*---------------------------------------------------------------------------*/
7223 
7224 
7225 /*---------------------------------------------------------------------------*/
7226 
7227 -- Bug#4571412
7228 -- Added parameters p_char_of_accounts_id, p_budget_year, p_cc_id
7229 -- to explain newly created message if update statment for
7230 -- PSB_WS_ACCOUNT_LINE_NOTES fails.
7231 
7232 -- API created for Bug#1584464
7233 PROCEDURE Create_Note
7234 ( p_return_status         OUT NOCOPY VARCHAR2,
7235   p_account_line_id       IN         NUMBER,
7236   p_note                  IN         VARCHAR2,
7237   p_chart_of_accounts_id  IN         NUMBER,
7238   p_budget_year           IN         VARCHAR2,
7239   p_cc_id                 IN         NUMBER,
7240   p_concatenated_segments IN        VARCHAR2
7241 )
7242 IS
7243   --
7244   l_change_note      VARCHAR2(1);
7245   l_note_id          NUMBER;
7246   --
7247   -- Bug#4571412
7248   l_message_text    VARCHAR2(4000);
7249   l_concat_segments VARCHAR2(2000);
7250 
7251   cursor c_note_id is
7252   select note_id
7253   from PSB_WS_ACCOUNT_LINES
7254   where account_line_id = p_account_line_id;
7255   --
7256 BEGIN
7257   --
7258   FND_PROFILE.GET
7259   ( name => 'PSB_EDIT_CREATE_NOTES',
7260     val => l_change_note);
7261 
7262   if nvl(l_change_note, 'Y') = 'Y' then
7263   begin
7264 
7265     for c_note_rec in c_note_id loop
7266       l_note_id := c_note_rec.note_id;
7267     end loop;
7268 
7269     if l_note_id is null then
7270     begin
7271 
7272       Insert into PSB_WS_ACCOUNT_LINE_NOTES
7273 	(note_id, note, last_update_date, last_updated_by, last_update_login, created_by, creation_date)
7274       values (psb_ws_account_line_notes_s.nextval, p_note, sysdate, FND_GLOBAL.USER_ID, FND_GLOBAL.LOGIN_ID, FND_GLOBAL.USER_ID, sysdate)
7275       returning note_id into l_note_id;
7276 
7277       update PSB_WS_ACCOUNT_LINES
7278 	 set note_id = l_note_id
7279        where account_line_id = p_account_line_id;
7280 
7281     end;
7282     else
7283       BEGIN
7284         UPDATE PSB_WS_ACCOUNT_LINE_NOTES
7285 	SET note              = note || FND_GLOBAL.NewLine || p_note,
7286 	    last_update_date  = sysdate,
7287 	    last_updated_by   = FND_GLOBAL.USER_ID,
7288 	    last_update_login = FND_GLOBAL.LOGIN_ID,
7289 	    created_by        = FND_GLOBAL.USER_ID,
7290 	    creation_date     = sysdate
7291         WHERE note_id         = l_note_id;
7292 
7293       -- Bug#4571412
7294       EXCEPTION
7295         WHEN others THEN
7296 
7297 	  -- Bug#4675858
7298 	  -- Set packaged variable to TRUE. This variable
7299 	  -- will be checked to set the CP status to warning
7300 	  -- if holding TRUE value.
7301 	  PSB_WS_ACCT1.g_soft_error_flag := TRUE ;
7302 
7303           IF p_concatenated_segments IS NULL THEN
7304             l_concat_segments
7305               := FND_FLEX_EXT.Get_Segs
7306                  (application_short_name => 'SQLGL',
7307                   key_flex_code          => 'GL#',
7308                   structure_number       => p_chart_of_accounts_id,
7309                   combination_id         => p_cc_id
7310                  );
7311           ELSE
7312             l_concat_segments := p_concatenated_segments;
7313           END IF;
7314 
7315           FND_MESSAGE.SET_NAME('PSB', 'PSB_WS_NOTES_EXCEEDED_LIMIT');
7316           FND_MESSAGE.SET_TOKEN('BUDGET_YEAR', p_budget_year);
7317           FND_MESSAGE.SET_TOKEN('ACCOUNTING_FLEXFIELD', l_concat_segments);
7318           --FND_MSG_PUB.Add;
7319           l_message_text := FND_MESSAGE.GET;
7320           FND_FILE.PUT_LINE(FND_FILE.LOG, l_message_text);
7321           --
7322       END;
7323     end if;
7324   end;
7325   end if;
7326 
7327   -- Initialize API return status to success
7328   p_return_status := FND_API.G_RET_STS_SUCCESS;
7329 
7330 EXCEPTION
7331 
7332    when FND_API.G_EXC_ERROR then
7333      p_return_status := FND_API.G_RET_STS_ERROR;
7334 
7335    when FND_API.G_EXC_UNEXPECTED_ERROR then
7336      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7337 
7338    when OTHERS then
7339      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7340 
7341 END Create_Note;
7342 /*---------------------------------------------------------------------------*/
7343 
7344 
7345 /*---------------------------------------------------------------------------*/
7346 -- Get Debug Information
7347 FUNCTION Get_Debug RETURN VARCHAR2 IS
7348 BEGIN
7349   return(g_dbug);
7350 END Get_Debug;
7351 /*---------------------------------------------------------------------------*/
7352 
7353 
7354 END PSB_WS_ACCT1;