DBA Data[Home] [Help]

PACKAGE BODY: APPS.PSB_WS_ACCT2

Source


1 PACKAGE BODY PSB_WS_ACCT2 AS
2 /* $Header: PSBVWA2B.pls 120.46 2006/02/24 13:25:05 shtripat ship $ */
3 
4   G_PKG_NAME CONSTANT   VARCHAR2(30):= 'PSB_WS_ACCT2';
5 
6   -- Select all Account Sets assigned to a Budget Group
7   CURSOR c_AccSet (budgetgroup_id NUMBER)
8   IS
9   SELECT account_position_set_id,
10          effective_start_date,
11          effective_end_date
12   FROM   psb_set_relations_v
13   WHERE  account_or_position_type = 'A'
14   AND    budget_group_id          = budgetgroup_id ;
15 
16   -- Select Budget Groups in Hierarchy; 'connect by' does a depth-first search
17   CURSOR c_BudGrp (budgetgroup_id NUMBER)
18   IS
19   SELECT budget_group_id,
20          num_proposed_years
21   FROM   psb_budget_groups
22   WHERE  budget_group_type    = 'R'
23   AND    effective_start_date <= PSB_WS_ACCT1.g_startdate_pp
24   AND    ( effective_end_date is null or effective_end_date >=
25            PSB_WS_ACCT1.g_enddate_cy )
26   START  WITH budget_group_id      = budgetgroup_id
27   CONNECT BY prior budget_group_id = parent_budget_group_id;
28 
29   TYPE g_currency_tbl_type IS TABLE OF VARCHAR2(15)
30     INDEX BY BINARY_INTEGER;
31   --
32   g_currency                g_currency_tbl_type;
33 
34   TYPE g_baltyp_tbl_type IS TABLE OF VARCHAR2(1)
35     INDEX BY BINARY_INTEGER;
36   --
37   g_balance_type            g_baltyp_tbl_type;
38 
39   TYPE g_budgetgroup_tbl_type IS TABLE OF NUMBER
40     INDEX BY BINARY_INTEGER;
41 
42   TYPE TokNameArray IS TABLE OF VARCHAR2(100)
43     INDEX BY BINARY_INTEGER;
44 
45   TYPE TokValArray IS TABLE OF VARCHAR2(1000)
46     INDEX BY BINARY_INTEGER;
47 
48   g_gl_actual_periods       PSB_WS_ACCT1.g_budgetperiod_tbl_type;
49   g_num_actual_periods      NUMBER;
50   g_gl_budget_periods       PSB_WS_ACCT1.g_budgetperiod_tbl_type;
51   g_num_budget_periods      NUMBER;
52   g_alloc_periods           PSB_WS_ACCT1.g_budgetperiod_tbl_type;
53 
54   -- Bug#3514350: To check if CY estimates need to be processed.
55   g_process_cy_estimates    BOOLEAN := TRUE;
56 
57   g_sql_budget_balance      VARCHAR2(1000);
58   g_cur_budget_balance      PLS_INTEGER;
59   g_sql_actual_balance      VARCHAR2(1000);
60   g_cur_actual_balance      PLS_INTEGER;
61   g_sql_encum_balance       VARCHAR2(1000);
62   g_cur_encum_balance       PLS_INTEGER;
63 
64   g_map_criteria            VARCHAR2(1);
65   g_create_zero_bal         VARCHAR2(1);
66 
67   g_actuals_func_total      NUMBER;
68   g_actuals_stat_total      NUMBER;
69 
70   --bug 3704360 made the following variable as public by declaring it in the spec
71   --g_running_total           NUMBER;
72   g_period_amount           PSB_WS_ACCT1.g_prdamt_tbl_type;
73 
74   g_summary_ccid            NUMBER;
75   g_summ_bgroup_id          NUMBER;
76 
77   -- Bug#2719865
78   g_first_ccid		    BOOLEAN := TRUE;
79 
80   -- Number of Message Tokens
81   no_msg_tokens             NUMBER := 0;
82   msg_tok_names             TokNameArray;
83   msg_tok_val               TokValArray;
84 
85   g_dbug                    VARCHAR2(1000);
86 
87   -- Bug#5030613 Start
88   g_include_gl_commit_balance VARCHAR2(1000) ;
89   g_include_gl_oblig_balance  VARCHAR2(1000) ;
90   g_include_gl_other_balance  VARCHAR2(1000) ;
91   -- Bug#5030613 End
92 
93   /* start bug 4256345 */
94   TYPE g_bud_ccid_tbl_type IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
95   g_ws_gl_budget_set_ccids g_bud_ccid_tbl_type;
96 
97   -- forward declaration
98   PROCEDURE check_ccid_bal (x_return_status      out nocopy varchar2,
99                             p_gl_budget_set_id   in  number);
100   /* end bug 4256345 */
101 
102 
103 /*===========================================================================+
104  |                             PROCEDURE pd                                  |
105  +===========================================================================*/
106 PROCEDURE pd( p_message   IN     VARCHAR2)
107 IS
108 BEGIN
109   NULL ;
110   --DBMS_OUTPUT.Put_Line(p_message) ;
111   --FND_FILE.put_line(FND_FILE.LOG, p_message);
112 END pd ;
113 /*---------------------------------------------------------------------------*/
114 
115 
116 /*---------------------------------------------------------------------------*/
117 -- Add Token and Value to the Message Token array
118 PROCEDURE message_token(tokname IN VARCHAR2,
119 			tokval  IN VARCHAR2)
120 IS
121 BEGIN
122 
123   if no_msg_tokens is null then
124     no_msg_tokens := 1;
125   else
126     no_msg_tokens := no_msg_tokens + 1;
127   end if;
128 
129   msg_tok_names(no_msg_tokens) := tokname;
130   msg_tok_val(no_msg_tokens) := tokval;
131 
132 END message_token;
133 /*---------------------------------------------------------------------------*/
134 
135 
136 /*---------------------------------------------------------------------------*/
137 PROCEDURE add_message(appname IN VARCHAR2,
138 		      msgname IN VARCHAR2) IS
139   i  PLS_INTEGER;
140 BEGIN
141 
142   if ((appname is not null) and
143       (msgname is not null)) then
144 
145     FND_MESSAGE.SET_NAME(appname, msgname);
146 
147     if no_msg_tokens is not null then
148       for i in 1..no_msg_tokens loop
149 	FND_MESSAGE.SET_TOKEN(msg_tok_names(i), msg_tok_val(i));
150       end loop;
151     end if;
152     FND_MSG_PUB.Add;
153 
154   end if;
155 
156   -- Clear Message Token stack
157   no_msg_tokens := 0;
158 END add_message;
159 /* ----------------------------------------------------------------------- */
160 
161 
162 --
163 -- Find GL Balances for a specific PSB Budget Period identified by the
164 -- Start and End Dates (p_start_date, p_end_date). This PSB Budget Period
165 -- is mapped to the corresponding GL Period based on the profile option
166 -- 'PSB : GL Map Criteria'
167 --
168 PROCEDURE Map_GL_Balances
169 ( p_return_status      OUT  NOCOPY  VARCHAR2,
170   p_ytd_amount         OUT  NOCOPY  NUMBER,
171   p_ccid               IN   NUMBER,
172   p_account_type       IN   VARCHAR2,
173   p_set_of_books_id    IN   NUMBER,
174   p_balance_type       IN   VARCHAR2,
175   p_currency_code      IN   VARCHAR2,
176   p_budgetary_control  IN   VARCHAR2,
177   p_budget_version_id  IN   NUMBER,
178   p_gl_budget_set_id   IN   NUMBER,
179   p_incl_trans_bal     IN   VARCHAR2,
180   p_incl_adj_period    IN   VARCHAR2,
181   p_start_date         IN   DATE,
182   p_end_date           IN   DATE,
183   /* bug no 4725091 */
184   p_incl_gl_fwd_balance IN  VARCHAR2,
185   p_create_wks_flag     IN  VARCHAR2
186 ) IS
187 
188   l_period_index       PLS_INTEGER;
189   l_ytd_amount         NUMBER;
190 
191   l_start_date         DATE;
192   l_end_date           DATE;
193   l_period_amount      NUMBER;
194 
195   num_balances         INTEGER;
196 
197   /* START BUG 4256345 */
198   l_start_date_tbl    dbms_sql.date_table;
199   l_end_date_tbl      dbms_sql.date_table;
200   l_period_amount_tbl dbms_sql.number_table;
201   /* end bug 4256345 */
202 
203   /* Bug No 4725091 */
204   l_fwd_bal_amt_tbl   dbms_sql.number_table;
205 
206   -- Bug#5030613 Start
207   l_commit_enc_type_id         NUMBER;
208   l_oblig_enc_type_id          NUMBER;
209 
210   cursor c_fin is
211     select purch_encumbrance_type_id, req_encumbrance_type_id
212       from financials_system_parameters;
213   -- Bug#5030613 End
214 
215 BEGIN
216 
217   l_ytd_amount := 0;
218 
219   /* start bug 4256345 */
220   l_Start_date_tbl.delete;
221   l_end_date_tbl.delete;
222   l_period_amount_tbl.delete;
223   /* end bug 4256345 */
224 
225   /* Bug No 4725091 */
226   l_fwd_bal_amt_tbl.delete;
227 
228 
229   if p_balance_type = 'B' then
230   begin
231 
232     /* start bug 4256345 */
233      if p_gl_budget_set_id is not null then
234 		if not g_ws_gl_budget_set_ccids.exists(p_ccid) then
235 			p_ytd_amount := l_ytd_amount;
236   			p_return_status := FND_API.G_RET_STS_SUCCESS;
237   			return;
238 		end if;
239      end if;
240      /* end bug 4256345 */
241 
242 
243     dbms_sql.bind_variable(g_cur_budget_balance, ':CCID', p_ccid);
244     dbms_sql.bind_variable(g_cur_budget_balance, ':B_SET_OF_BOOKS_ID', p_set_of_books_id) ; -- Bug#5030613
245     dbms_sql.bind_variable(g_cur_budget_balance, ':ACCOUNT_TYPE', p_account_type);
246     dbms_sql.bind_variable(g_cur_budget_balance, ':CURRENCY_CODE', p_currency_code);
247     dbms_sql.bind_variable(g_cur_budget_balance, ':START_DATE', p_start_date);
248     dbms_sql.bind_variable(g_cur_budget_balance, ':END_DATE', p_end_date);
249 
250     if p_gl_budget_set_id is not null then
251       dbms_sql.bind_variable(g_cur_budget_balance, ':BUDGET_VERSION_ID', p_budget_version_id);
252     end if;
253 
254     /* start bug 4256345 */
255     /* dbms_sql.define_column(g_cur_budget_balance, 1, l_start_date);
256     dbms_sql.define_column(g_cur_budget_balance, 2, l_end_date);
257     dbms_sql.define_column(g_cur_budget_balance, 3, l_period_amount); */
258 
259     dbms_sql.define_array(g_cur_budget_balance, 1, l_Start_date_tbl, 100, 1);
260     dbms_sql.define_array(g_cur_budget_balance, 2, l_end_date_tbl,   100, 1);
261     dbms_sql.define_array(g_cur_budget_balance, 3, l_period_amount_tbl, 100, 1);
262 
263     /* end bug 4256345 */
264 
265     /* bug no 4725091 */
266     dbms_sql.define_array(g_cur_budget_balance, 4, l_fwd_bal_amt_tbl, 100, 1);
267 
268     num_balances := dbms_sql.execute(g_cur_budget_balance);
269 
270     loop
271       /* start bug 4256345 */
272       /* if dbms_sql.fetch_rows(g_cur_budget_balance) = 0 then
273 	exit;
274       end if; */
275       num_balances := dbms_sql.fetch_rows(g_cur_budget_balance);
276 
277 
278       /* dbms_sql.column_value(g_cur_budget_balance, 1, l_start_date);
279       dbms_sql.column_value(g_cur_budget_balance, 2, l_end_date);
280       dbms_sql.column_value(g_cur_budget_balance, 3, l_period_amount); */
281 
282       dbms_sql.column_value(g_cur_budget_balance, 1, l_Start_date_tbl);
283       dbms_sql.column_value(g_cur_budget_balance, 2, l_end_date_tbl);
284       dbms_sql.column_value(g_cur_budget_balance, 3, l_period_amount_tbl);
285 
286       /* bug no 4725091 */
287       dbms_sql.column_value(g_cur_budget_balance, 4, l_fwd_bal_amt_tbl);
288 
289       for l_date_index in 1..l_start_date_tbl.count loop
290         for l_period_index in 1..g_num_budget_periods loop
291 
292 	  if (((g_map_criteria = 'S') and
293 	       (l_start_date_tbl(l_date_index) between g_gl_budget_periods(l_period_index).start_date and g_gl_budget_periods(l_period_index).end_date)) or
294 	      ((g_map_criteria = 'E') and
295 	       (l_end_date_tbl(l_date_index) between g_gl_budget_periods(l_period_index).start_date and g_gl_budget_periods(l_period_index).end_date))) then
296 	  begin
297 
298             /* start bug no 4725091 */
299             if l_period_index = 1 and fnd_api.to_boolean(p_incl_gl_fwd_balance) and fnd_api.to_boolean(p_create_wks_flag) then
300 
301               g_period_amount(g_gl_budget_periods(l_period_index).long_sequence_no) :=
302 		   nvl(g_period_amount(g_gl_budget_periods(l_period_index).long_sequence_no), 0) +
303                    nvl(l_period_amount_tbl(l_date_index), 0) +
304                    nvl(l_fwd_bal_amt_tbl(l_date_index), 0);
305 	      l_ytd_amount := l_ytd_amount + nvl(l_period_amount_tbl(l_date_index), 0) + nvl(l_fwd_bal_amt_tbl(l_date_index), 0);
306 
307             else
308 
309 	      g_period_amount(g_gl_budget_periods(l_period_index).long_sequence_no) :=
310 		   nvl(g_period_amount(g_gl_budget_periods(l_period_index).long_sequence_no), 0) + nvl(l_period_amount_tbl(l_date_index) , 0);
311 	      l_ytd_amount := l_ytd_amount + nvl(l_period_amount_tbl(l_date_index) , 0);
312 
313             end if;
314             /* end bug no 4725091 */
315 
316 	  end;
317 	  end if;
318 
319         end loop;
320 
321       -- for bug 4256345
322       end loop;
323 
324       -- for bug 4256345
325       exit when num_balances <> 100;
326 
327     end loop;
328 
329   end;
330   elsif p_balance_type = 'A' then
331   begin
332 
333     dbms_sql.bind_variable(g_cur_actual_balance, ':CCID', p_ccid);
334     dbms_sql.bind_variable(g_cur_actual_balance, ':B_SET_OF_BOOKS_ID', p_set_of_books_id) ; -- Bug#5030613
335     dbms_sql.bind_variable(g_cur_actual_balance, ':ACCOUNT_TYPE', p_account_type);
336     dbms_sql.bind_variable(g_cur_actual_balance, ':CURRENCY_CODE', p_currency_code);
337     dbms_sql.bind_variable(g_cur_actual_balance, ':START_DATE', p_start_date);
338     dbms_sql.bind_variable(g_cur_actual_balance, ':END_DATE', p_end_date);
339 
340     /* start bug 4256345 */
341     /* dbms_sql.define_column(g_cur_actual_balance, 1, l_start_date);
342     dbms_sql.define_column(g_cur_actual_balance, 2, l_end_date);
343     dbms_sql.define_column(g_cur_actual_balance, 3, l_period_amount); */
344 
345     dbms_sql.define_array(g_cur_actual_balance, 1, l_start_date_tbl, 100, 1);
346     dbms_sql.define_array(g_cur_actual_balance, 2, l_end_date_tbl, 100, 1);
347     dbms_sql.define_array(g_cur_actual_balance, 3, l_period_amount_tbl, 100, 1);
348     /* end bug 4256345 */
349 
350 
351     num_balances := dbms_sql.execute(g_cur_actual_balance);
352 
353     loop
354       /* start bug 4256345 */
355       /* if dbms_sql.fetch_rows(g_cur_actual_balance) = 0 then
356 	exit;
357       end if; */
358 
359       num_balances := dbms_sql.fetch_rows(g_cur_actual_balance);
360 
361       /* dbms_sql.column_value(g_cur_actual_balance, 1, l_start_date);
362       dbms_sql.column_value(g_cur_actual_balance, 2, l_end_date);
363       dbms_sql.column_value(g_cur_actual_balance, 3, l_period_amount); */
364 
365       dbms_sql.column_value(g_cur_actual_balance, 1, l_start_date_tbl);
366       dbms_sql.column_value(g_cur_actual_balance, 2, l_end_date_tbl);
367       dbms_sql.column_value(g_cur_actual_balance, 3, l_period_amount_tbl);
368 
369       for l_date_index in 1..l_start_date_tbl.count loop
370         for l_period_index in 1..g_num_actual_periods loop
371 
372 	  if (((g_map_criteria = 'S') and
373 	       (l_start_date_tbl(l_date_index) between g_gl_actual_periods(l_period_index).start_date and g_gl_actual_periods(l_period_index).end_date)) or
374 	      ((g_map_criteria = 'E') and
375 	       (l_end_date_tbl(l_date_index) between g_gl_actual_periods(l_period_index).start_date and g_gl_actual_periods(l_period_index).end_date))) then
376 	  begin
377 
378 	    g_period_amount(g_gl_actual_periods(l_period_index).long_sequence_no) :=
379 		   nvl(g_period_amount(g_gl_actual_periods(l_period_index).long_sequence_no), 0) + nvl(l_period_amount_tbl(l_date_index) , 0);
380 	    l_ytd_amount := l_ytd_amount + nvl(l_period_amount_tbl(l_date_index) , 0);
381 
382 	  end;
383 	  end if;
384 
385         end loop;
386 
387       -- for bug 4256345
388       end loop;
389 
390       -- for bug 4256345
391       exit when num_balances <> 100;
392 
393     end loop;
394 
395   end;
396   elsif p_balance_type = 'E' then
397   begin
398 
399     dbms_sql.bind_variable(g_cur_encum_balance, ':CCID', p_ccid);
400     dbms_sql.bind_variable(g_cur_encum_balance, ':B_SET_OF_BOOKS_ID', p_set_of_books_id) ; -- Bug#5030613
401     dbms_sql.bind_variable(g_cur_encum_balance, ':ACCOUNT_TYPE', p_account_type);
402     dbms_sql.bind_variable(g_cur_encum_balance, ':CURRENCY_CODE', p_currency_code);
403     dbms_sql.bind_variable(g_cur_encum_balance, ':START_DATE', p_start_date);
404     dbms_sql.bind_variable(g_cur_encum_balance, ':END_DATE', p_end_date);
405 
406     -- Bug#5030613 Start
407     for c_fin_rec in c_fin loop
408       l_commit_enc_type_id := c_fin_rec.req_encumbrance_type_id;
409       l_oblig_enc_type_id  := c_fin_rec.purch_encumbrance_type_id;
410     end loop;
411 
412     -- Extract encumbrance balances, Include Other Encum Balances.
413     IF FND_API.to_Boolean(g_include_gl_other_balance)
414     THEN
415       IF NOT FND_API.to_Boolean(g_include_gl_commit_balance)
416       THEN
417         IF NOT FND_API.to_Boolean(g_include_gl_oblig_balance)
418 	THEN
419           dbms_sql.bind_variable(g_cur_encum_balance, ':B_OBLIG_ENC_TYPE_ID', l_oblig_enc_type_id) ;
420           dbms_sql.bind_variable(g_cur_encum_balance, ':B_COMMIT_ENC_TYPE_ID', l_commit_enc_type_id) ;
421         ELSE
422           dbms_sql.bind_variable(g_cur_encum_balance, ':B_COMMIT_ENC_TYPE_ID', l_commit_enc_type_id) ;
423 	END IF;
424       ELSE
425         IF not FND_API.to_Boolean(g_include_gl_oblig_balance)
426 	THEN
427           dbms_sql.bind_variable(g_cur_encum_balance, ':B_OBLIG_ENC_TYPE_ID', l_oblig_enc_type_id) ;
428         END IF ;
429       END IF;
430     ELSE
431       IF NOT FND_API.to_Boolean(g_include_gl_commit_balance)
432       THEN
433         IF FND_API.to_Boolean(g_include_gl_oblig_balance)
434 	THEN
435           dbms_sql.bind_variable(g_cur_encum_balance, ':B_OBLIG_ENC_TYPE_ID', l_oblig_enc_type_id) ;
436         END IF;
437       ELSE
438         IF NOT FND_API.to_Boolean(g_include_gl_oblig_balance)
439 	THEN
440           dbms_sql.bind_variable(g_cur_encum_balance, ':B_COMMIT_ENC_TYPE_ID', l_commit_enc_type_id) ;
441         ELSE
442           dbms_sql.bind_variable(g_cur_encum_balance, ':B_OBLIG_ENC_TYPE_ID', l_oblig_enc_type_id) ;
443           dbms_sql.bind_variable(g_cur_encum_balance, ':B_COMMIT_ENC_TYPE_ID', l_commit_enc_type_id) ;
444         END IF;
445       END IF;
446     END IF;
447     -- Bug#5030613 End
448 
449     /* start bug 4256345 */
450     /* dbms_sql.define_column(g_cur_encum_balance, 1, l_start_date);
451     dbms_sql.define_column(g_cur_encum_balance, 2, l_end_date);
452     dbms_sql.define_column(g_cur_encum_balance, 3, l_period_amount); */
453 
454     dbms_sql.define_array(g_cur_encum_balance, 1, l_start_date_tbl, 100, 1);
455     dbms_sql.define_array(g_cur_encum_balance, 2, l_end_date_tbl, 100, 1);
456     dbms_sql.define_array(g_cur_encum_balance, 3, l_period_amount_tbl, 100, 1);
457     /* end bug 4256345 */
458 
459     /* bug no 4725091 */
460     dbms_sql.define_array(g_cur_encum_balance, 4, l_fwd_bal_amt_tbl, 100, 1);
461 
462 
463     num_balances := dbms_sql.execute(g_cur_encum_balance);
464 
465     loop
466       /* start bug 4256345 */
467       /* if dbms_sql.fetch_rows(g_cur_encum_balance) = 0 then
468 	exit;
469       end if; */
470 
471       num_balances := dbms_sql.fetch_rows(g_cur_encum_balance);
472 
473       /*dbms_sql.column_value(g_cur_encum_balance, 1, l_start_date);
474       dbms_sql.column_value(g_cur_encum_balance, 2, l_end_date);
475       dbms_sql.column_value(g_cur_encum_balance, 3, l_period_amount); */
476 
477       dbms_sql.column_value(g_cur_encum_balance, 1, l_start_date_tbl);
478       dbms_sql.column_value(g_cur_encum_balance, 2, l_end_date_tbl);
479       dbms_sql.column_value(g_cur_encum_balance, 3, l_period_amount_tbl);
480 
481       /* bug no 4725091 */
482       dbms_sql.column_value(g_cur_encum_balance, 4, l_fwd_bal_amt_tbl);
483 
484 
485       for l_date_index in 1..l_start_date_tbl.count loop
486         for l_period_index in 1..g_num_actual_periods loop
487 
488 	  if (((g_map_criteria = 'S') and
489 	       (l_start_date_tbl(l_date_index) between g_gl_actual_periods(l_period_index).start_date and g_gl_actual_periods(l_period_index).end_date)) or
490 	      ((g_map_criteria = 'E') and
491 	       (l_end_date_tbl(l_date_index) between g_gl_actual_periods(l_period_index).start_date and g_gl_actual_periods(l_period_index).end_date))) then
492 	  begin
493 
494             /* start bug no 4725091 */
495             if l_period_index = 1 and fnd_api.to_boolean(p_incl_gl_fwd_balance) and fnd_api.to_boolean(p_create_wks_flag) then
496               g_period_amount(g_gl_actual_periods(l_period_index).long_sequence_no) :=
497 		   nvl(g_period_amount(g_gl_actual_periods(l_period_index).long_sequence_no), 0) +
498                    nvl(l_period_amount_tbl(l_date_index), 0) +
499                    nvl(l_fwd_bal_amt_tbl(l_date_index), 0);
500 
501 	      l_ytd_amount := l_ytd_amount + nvl(l_period_amount_tbl(l_date_index), 0) + + nvl(l_fwd_bal_amt_tbl(l_date_index), 0);
502 
503             else
504 	      g_period_amount(g_gl_actual_periods(l_period_index).long_sequence_no) :=
505 		   nvl(g_period_amount(g_gl_actual_periods(l_period_index).long_sequence_no), 0) + nvl(l_period_amount_tbl(l_date_index), 0);
506 	      l_ytd_amount := l_ytd_amount + nvl(l_period_amount_tbl(l_date_index), 0);
507 
508             end if;
509             /* end bug no 4725091 */
510 
511 	  end;
512 	  end if;
513 
514         end loop;
515 
516       -- for bug 4256345
517       end loop;
518 
519       -- for bug 4256345
520       exit when num_balances <> 100;
521 
522     end loop;
523 
524   end;
525   end if;
526 
527 
528   -- Initialize API return status to success
529 
530   p_ytd_amount := l_ytd_amount;
531   p_return_status := FND_API.G_RET_STS_SUCCESS;
532 
533 
534 EXCEPTION
535 
536    when FND_API.G_EXC_ERROR then
537      p_return_status := FND_API.G_RET_STS_ERROR;
538 
539    when FND_API.G_EXC_UNEXPECTED_ERROR then
540      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
541 
542    when OTHERS then
543      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
544      if FND_MSG_PUB.Check_Msg_Level
545        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
546      then
547        FND_MSG_PUB.Add_Exc_Msg
548 	  (p_pkg_name => G_PKG_NAME,
549 	   p_procedure_name => 'Map_GL_Balances');
550      end if;
551 
552 END Map_GL_Balances;
553 
554 /* ----------------------------------------------------------------------- */
555 
556 -- Get GL Balances for a Budget Year by mapping individual Budget Periods in
557 -- the Budget Year to GL Periods
558 
559 PROCEDURE Get_Balances
560 ( p_return_status       OUT  NOCOPY  VARCHAR2,
561   p_ccid                IN   NUMBER,
562   p_account_type        IN   VARCHAR2,
563   p_budget_year_id      IN   NUMBER,
564   p_year_name           IN   VARCHAR2,
565   p_year_start_date     IN   DATE,
566   p_year_end_date       IN   DATE,
567   p_budget_year_type    IN   VARCHAR2,
568   p_incl_stat_bal       IN   VARCHAR2,
569   p_incl_trans_bal      IN   VARCHAR2,
570   p_incl_adj_period     IN   VARCHAR2,
571   p_set_of_books_id     IN   NUMBER,
572   p_budget_group_id     IN   NUMBER,
573   p_stage_set_id        IN   NUMBER,
574   p_func_currency       IN   VARCHAR2,
575   p_budgetary_control   IN   VARCHAR2,
576   p_budget_version_id   IN   NUMBER,
577   p_flex_mapping_set_id IN   NUMBER,
578   p_gl_budget_set_id    IN   NUMBER,
579   p_flex_code           IN   NUMBER,
580   p_worksheet_id        IN   NUMBER,
581   p_service_package_id  IN   NUMBER,
582   p_sequence_number     IN   NUMBER,
583   p_rounding_factor     IN   NUMBER,
584   /* bug no 4725091 */
585   p_incl_gl_fwd_balance IN   VARCHAR2,
586   p_create_wks_flag     IN   VARCHAR2
587 ) IS
588 
589   l_return_status       VARCHAR2(1);
590 
591   l_init_index          PLS_INTEGER;
592   l_type_index          PLS_INTEGER;
593   l_currency_index      PLS_INTEGER;
594 
595   l_ytd_amount          NUMBER;
596 
597   l_msg_count           NUMBER;
598   l_msg_data            VARCHAR2(2000);
599 
600   l_budget_version_id   NUMBER;
601 
602   l_concat_segments     VARCHAR2(2000);
603 
604   l_account_line_id     NUMBER;
605   l_balance_type        VARCHAR2(1);
606 
607 BEGIN
608 
609   for l_init_index in 1..PSB_WS_ACCT1.g_max_num_amounts loop
610     g_period_amount(l_init_index) := null;
611   end loop;
612 
613   l_ytd_amount := 0;
614 
615   for l_init_index in 1..g_currency.Count loop
616     g_currency(l_init_index) := null;
617   end loop;
618 
619   if p_func_currency <> 'STAT' then
620     g_currency(1) := p_func_currency;
621 
622     if FND_API.to_Boolean(p_incl_stat_bal) then
623       g_currency(2) := 'STAT';
624     end if;
625   else
626     g_currency(1) := 'STAT';
627   end if;
628 
629   -- Get the budget for the CCID if a budget set is assigned to the worksheet
630 
631   if p_gl_budget_set_id is not null then
632   begin
633 
634     PSB_GL_BUDGET_PVT.Find_GL_Budget
635        (p_api_version => 1.0,
636 	p_return_status => l_return_status,
637 	p_msg_count => l_msg_count,
638 	p_msg_data => l_msg_data,
639 	p_gl_budget_set_id => p_gl_budget_set_id,
640 	p_code_combination_id => p_ccid,
641 	p_start_date => p_year_start_date,
642 	p_gl_budget_version_id => l_budget_version_id);
643 
644     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
645       raise FND_API.G_EXC_ERROR;
646     end if;
647 
648     if l_budget_version_id is null then
649     begin
650 
651       PSB_GL_BUDGET_PVT.Find_GL_Budget
652 	 (p_api_version => 1.0,
653 	  p_return_status => l_return_status,
654 	  p_msg_count => l_msg_count,
655 	  p_msg_data => l_msg_data,
656 	  p_gl_budget_set_id => p_gl_budget_set_id,
657 	  p_code_combination_id => p_ccid,
658 	  p_start_date => p_year_start_date,
659 	  p_gl_budget_version_id => l_budget_version_id,
660 	  p_dual_posting_type => 'A');
661 
662       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
663 	raise FND_API.G_EXC_ERROR;
664       end if;
665 
666     end;
667     end if;
668 
669   end;
670   end if;
671 
672   for l_type_index in 1..g_balance_type.Count loop
673 
674     for l_currency_index in 1..g_currency.Count loop
675 
676       if g_currency(l_currency_index) is not null then
677       begin
678 
679 	Map_GL_Balances
680 	   (p_ccid => p_ccid,
681 	    p_account_type => p_account_type,
682 	    p_set_of_books_id => p_set_of_books_id,
683 	    p_balance_type => g_balance_type(l_type_index),
684 	    p_currency_code => g_currency(l_currency_index),
685 	    p_budgetary_control => p_budgetary_control,
686 	    p_budget_version_id => l_budget_version_id,
687 	    p_gl_budget_set_id => p_gl_budget_set_id,
688 	    p_incl_trans_bal => p_incl_trans_bal,
689 	    p_incl_adj_period => p_incl_adj_period,
690 	    p_start_date => p_year_start_date,
691 	    p_end_date => p_year_end_date,
692 	    p_return_status => l_return_status,
693 	    p_ytd_amount => l_ytd_amount,
694             /* bug no 4725091 */
695             p_incl_gl_fwd_balance => p_incl_gl_fwd_balance,
696             p_create_wks_flag     => p_create_wks_flag);
697 
698 	if l_return_status <> FND_API.G_RET_STS_SUCCESS then
699 	  raise FND_API.G_EXC_ERROR;
700 	end if;
701 
702 	-- Store the Actual Balances for Functional Currency and STAT so that allocation of
703 	-- CY Balances only includes the Estimate part
704 
705 	if ((p_budget_year_type = 'CY') and (g_balance_type(l_type_index) = 'A')) then
706 	begin
707 
708 	  if g_currency(l_currency_index) = p_func_currency then
709 	    g_actuals_func_total := l_ytd_amount;
710 	  else
711 	    g_actuals_stat_total := l_ytd_amount;
712 	  end if;
713 
714 	end;
715 	end if;
716 
717 	-- Create Account Distribution if YTD Amount > 0 or YTD Amount is 0 and allowed by
718 	-- the profile option
719 
720 
721        --Commented the following line for bug 3305778
722        -- Bug 3543845: Reactivate the following line
723 
724        -- Bug 4250468 added the variable g_ws_first_time_creation_flag
725        -- in the IF condition
726         IF ( PSB_WORKSHEET.g_ws_first_time_creation_flag AND
727             ((l_ytd_amount <> 0)
728              OR
729              ( l_ytd_amount = 0 and g_create_zero_bal = 'Y' )
730             )
731            )
732         OR NOT PSB_WORKSHEET.g_ws_first_time_creation_flag THEN
733 
734 	begin
735 
736 	  if g_balance_type(l_type_index) = 'E' then
737 	    l_balance_type := 'X';
738 	  else
739 	    l_balance_type := g_balance_type(l_type_index);
740 	  end if;
741 
742           --pd('1: Call Create_Account_Dist=> ccid=' || TO_CHAR(p_ccid) ||
743           --   ', p_budget_year_id=' || TO_CHAR(p_budget_year_id) ||
744           --   ', l_balance_type=' || l_balance_type ||
745           --   ', p_ytd_amount=' || TO_CHAR(l_ytd_amount));
746 
747 	  PSB_WS_ACCT1.Create_Account_Dist
748 	     (p_api_version => 1.0,
749 	      p_return_status => l_return_status,
750 	      p_account_line_id => l_account_line_id,
751 	      p_worksheet_id => p_worksheet_id,
752 	      p_service_package_id => p_service_package_id,
753 	      p_check_spal_exists => FND_API.G_FALSE,
754 	      p_gl_cutoff_period => null,
755 	      p_allocrule_set_id => null,
756 	      p_budget_calendar_id => null,
757 	      p_rounding_factor => p_rounding_factor,
758 	      p_stage_set_id => p_stage_set_id,
759 	      p_start_stage_seq => p_sequence_number,
760 	      p_current_stage_seq => p_sequence_number,
761 	      p_budget_group_id => p_budget_group_id,
762 	      p_budget_year_id => p_budget_year_id,
763 	      p_ccid => p_ccid,
764 	      p_flex_mapping_set_id => p_flex_mapping_set_id,
765 	      p_map_accounts => TRUE,
766 	      p_currency_code => g_currency(l_currency_index),
767 	      p_balance_type => l_balance_type,
768 	      p_ytd_amount => l_ytd_amount,
769 	      p_period_amount => g_period_amount);
770 
771 	  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
772 	    raise FND_API.G_EXC_ERROR;
773 	  end if;
774 
775 	  for l_init_index in 1..PSB_WS_ACCT1.g_max_num_amounts loop
776 	    g_period_amount(l_init_index) := null;
777 	  end loop;
778 
779 	  l_ytd_amount := 0;
780 
781 	end;
782 	end if;
783 
784       end;
785       end if;
786 
787     end loop; /* Currency */
788 
789   end loop; /* Balance Type */
790 
791 
792   -- Initialize API return status to success
793 
794   p_return_status := FND_API.G_RET_STS_SUCCESS;
795 
796 
797 EXCEPTION
798 
799    when FND_API.G_EXC_ERROR then
800      p_return_status := FND_API.G_RET_STS_ERROR;
801 
802    when FND_API.G_EXC_UNEXPECTED_ERROR then
803      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
804 
805    when OTHERS then
806      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
807      if FND_MSG_PUB.Check_Msg_Level
808        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
809      then
810        FND_MSG_PUB.Add_Exc_Msg
811 	  (p_pkg_name => G_PKG_NAME,
812 	   p_procedure_name => 'Get_Balances');
813      end if;
814 
815 END Get_Balances;
816 /*---------------------------------------------------------------------------*/
817 
818 
819 /*===========================================================================+
820  |                        PROCEDURE Create_Worksheet_Accounts                |
821  +===========================================================================*/
822 PROCEDURE Create_Worksheet_Accounts
823 ( p_return_status              OUT  NOCOPY  VARCHAR2,
824   p_worksheet_id               IN   NUMBER,
825   p_rounding_factor            IN   NUMBER,
826   p_stage_set_id               IN   NUMBER,
827   p_service_package_id         IN   NUMBER,
828   p_start_stage_seq            IN   NUMBER,
829   p_allocrule_set_id           IN   NUMBER,
830   p_budget_group_id            IN   NUMBER,
831   p_flex_code                  IN   NUMBER,
832   p_parameter_set_id           IN   NUMBER,
833   p_budget_calendar_id         IN   NUMBER,
834   p_gl_cutoff_period           IN   DATE,
835   p_include_gl_commit_balance  IN   VARCHAR2,
836   p_include_gl_oblig_balance   IN   VARCHAR2,
837   p_include_gl_other_balance   IN   VARCHAR2,
838   p_budget_version_id          IN   NUMBER,
839   p_flex_mapping_set_id        IN   NUMBER,
840   p_gl_budget_set_id           IN   NUMBER,
841   p_set_of_books_id            IN   NUMBER,
842   p_set_of_books_name          IN   VARCHAR2,
843   p_func_currency              IN   VARCHAR2,
844   p_budgetary_control          IN   VARCHAR2,
845   p_incl_stat_bal              IN   VARCHAR2,
846   p_incl_trans_bal             IN   VARCHAR2,
847   p_incl_adj_period            IN   VARCHAR2,
848   p_num_proposed_years         IN   NUMBER,
849   p_num_years_to_allocate      IN   NUMBER,
850   p_budget_by_position         IN   VARCHAR2,
851   /* Bug No 4725091 */
852   P_incl_gl_fwd_balance        IN   VARCHAR2
853 )
854 IS
855   --
856 
857   l_ccid_index              PLS_INTEGER;
858   l_year_index              PLS_INTEGER;
859   l_period_index            PLS_INTEGER;
860   l_init_index              PLS_INTEGER;
861 
862   l_userid                  NUMBER;
863   l_loginid                 NUMBER;
864 
865   l_account_type            VARCHAR2(1);
866   l_template_id             NUMBER;
867 
868   l_ccid_type               VARCHAR2(30);
869   l_ccid_start_period       DATE;
870   l_ccid_end_period         DATE;
871 
872   l_num_projected_years     NUMBER;
873   l_year_start_date         DATE;
874   l_year_end_date           DATE;
875 
876 
877   l_num_accounts            NUMBER := 0;
878   l_return_status           VARCHAR2(1);
879 
880   TYPE ccid_arr IS TABLE OF NUMBER(15);
881   TYPE date_arr IS TABLE OF DATE;
882 
883   -- for bug 4256345
884   /*
885   TYPE ccid_rec IS RECORD   ( ccid       ccid_arr ,
886                               start_date date_arr ,
887                               end_date   date_arr ) ;
888   --
889   l_ccids                   ccid_rec;
890   */
891 
892   -- for bug 4256345
893   --CURSOR c_ccids (AccSetID NUMBER) is
894   --SELECT a.code_combination_id, b.start_date_active, b.end_date_active
895   --FROM   PSB_BUDGET_ACCOUNTS a, GL_CODE_COMBINATIONS b
896   --WHERE  a.account_position_set_id = AccSetID
897   /* Bug 3692601 Start */
898   --AND b.detail_budgeting_allowed_flag = 'Y'
899   /* Bug 3692601 End */
900   --AND    b.code_combination_id = a.code_combination_id
901   --AND    b.enabled_flag = 'Y';
902   --
903 
904   /* start bug 4256345 */
905   TYPE ccid_rec IS RECORD   ( ccid       ccid_arr);
906   l_ccids                   ccid_rec;
907 
908   CURSOR l_ccid_csr (AcctsetId NUMBER)
909   IS
910     SELECT code_combination_id
911     FROM psb_budget_accounts
912     WHERE account_position_set_id = AcctsetId;
913 
914   lt_start_date_active	DATE;
915   lt_end_date_active	DATE;
916   lt_valid_ccid			VARCHAR2(30) := 'FALSE';
917   /* end bug 4256345 */
918 
919 BEGIN
920 
921   l_userid := FND_GLOBAL.USER_ID;
922   l_loginid := FND_GLOBAL.LOGIN_ID;
923 
924   -- g_deferred_ccids holds CCIDs that are deferred for parameter processing;
925   -- these CCIDs are deferred because they depend on CCID(s) that have not been
926   -- computed yet. This problem arises as we follow a depth first search of
927   -- the Budget Group Hierarchy and (consequently) the Account Sets that are
928   -- assigned to Budget Groups in the Hierarchy
929 
930   for l_init_index in 1..g_deferred_ccids.Count loop
931     g_deferred_ccids(l_init_index).budget_group_id := null;
932     g_deferred_ccids(l_init_index).num_proposed_years := null;
933     g_deferred_ccids(l_init_index).ccid := null;
934     g_deferred_ccids(l_init_index).ccid_start_period := null;
935     g_deferred_ccids(l_init_index).ccid_end_period := null;
936   end loop;
937 
938   g_num_defccids := 0;
939 
940   -- g_dependent_ccids stores dependency between CCIDs; this is used to sort
941   -- CCIDs to avoid circular loops; e.g CCID-1 depends on CCID-2 that depends
942   -- on CCID-3 : we sort the CCIDs so that CCID-3 is processed first, followed
943   -- by CCID-2 followed by CCID-1
944 
945   for l_init_index in 1..g_dependent_ccids.Count loop
946     g_dependent_ccids(l_init_index).ccid := null;
947     g_dependent_ccids(l_init_index).dependent_ccid := null;
948   end loop;
949 
950   g_num_depccids := 0;
951 
952   -- g_sorted_ccids stores the deferred CCIDs sorted so that there are no
953   -- dependencies between the sorted CCIDs
954   for l_init_index in 1..g_sorted_ccids.Count loop
955     g_sorted_ccids(l_init_index).budget_group_id := null;
956     g_sorted_ccids(l_init_index).num_proposed_years := null;
957     g_sorted_ccids(l_init_index).ccid := null;
958     g_sorted_ccids(l_init_index).ccid_start_period := null;
959     g_sorted_ccids(l_init_index).ccid_end_period := null;
960   end loop;
961 
962   g_num_sortccids := 0;
963 
964   --
965   -- If Budget Set is not defined for the worksheet, the funding budget must
966   -- be selected in the 'Define Worksheet' form for Set of Books that do not
967   -- have budgetary control turned on.
968   --
969   if p_gl_budget_set_id is null then
970 
971     if (not FND_API.to_Boolean(p_budgetary_control)) then
972       message_token('SOB', p_set_of_books_name);
973       message_token('WORKSHEET_ID', p_worksheet_id);
974       add_message('PSB', 'PSB_BUDGET_VERSION');
975       raise FND_API.G_EXC_ERROR;
976     end if;
977 
978   end if;
979 
980 
981   -- GL Mapping Criteria Profile Option : this specifies how to map the PSB
982   -- Budget Periods to the GL Budget Periods.  'S' indicates that the Start
983   -- Date for a GL Period should be within a PSB Budget Period specified by
984   -- Start and End Dates; 'E' indicates that the End Date for a GL Period
985   -- should be within a PSB Budget Period specified by Start and End Dates.
986 
987   FND_PROFILE.GET( name => 'PSB_GL_MAP_CRITERIA' ,
988                    val => g_map_criteria         ) ;
989   if g_map_criteria is null then
990     g_map_criteria := 'S';
991   end if;
992 
993 
994   -- Create Zero Balances Profile Option : this specifies whether non-Position
995   -- CCIDs with zero YTD Amounts should be created in PSB_WS_ACCOUNT_LINES
996 
997   FND_PROFILE.GET
998      (name => 'PSB_CREATE_ZERO_BALANCE_ACCT',
999       val => g_create_zero_bal);
1000 
1001   if g_create_zero_bal is null then
1002     -- Bug 3543845: Change default behavior to not creating zero balance
1003     g_create_zero_bal := 'N';
1004   end if;
1005 
1006   -- Create Dynamic SQL Statements for extracting GL Balances
1007 
1008   PSB_WS_ACCT1.DSQL_Budget_Balance
1009      (p_return_status => l_return_status,
1010       p_sql_statement => g_sql_budget_balance,
1011       p_set_of_books_id => p_set_of_books_id,
1012       p_budgetary_control => p_budgetary_control,
1013       p_budget_version_id => p_budget_version_id,
1014       p_gl_budget_set_id => p_gl_budget_set_id,
1015       p_incl_adj_period => p_incl_adj_period,
1016       p_map_criteria => g_map_criteria);
1017 
1018   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1019     raise FND_API.G_EXC_ERROR;
1020   end if;
1021 
1022   PSB_WS_ACCT1.DSQL_Actual_Balance
1023      (p_return_status => l_return_status,
1024       p_sql_statement => g_sql_actual_balance,
1025       p_set_of_books_id => p_set_of_books_id,
1026       p_incl_adj_period => p_incl_adj_period,
1027       p_map_criteria => g_map_criteria);
1028 
1029   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1030     raise FND_API.G_EXC_ERROR;
1031   end if;
1032 
1033   -- Bug#5030613 Start
1034   g_include_gl_commit_balance := p_include_gl_commit_balance ;
1035   g_include_gl_oblig_balance  := p_include_gl_oblig_balance ;
1036   g_include_gl_other_balance  := p_include_gl_other_balance ;
1037   -- Bug#5030613 End
1038 
1039   PSB_WS_ACCT1.DSQL_Encum_Balance
1040      (p_return_status => l_return_status,
1041       p_sql_statement => g_sql_encum_balance,
1042       p_set_of_books_id => p_set_of_books_id,
1043       p_incl_adj_period => p_incl_adj_period,
1044       p_map_criteria => g_map_criteria,
1045       p_include_gl_commit_balance => p_include_gl_commit_balance,
1046       p_include_gl_oblig_balance => p_include_gl_oblig_balance,
1047       p_include_gl_other_balance => p_include_gl_other_balance);
1048 
1049     -- Bug#5030613 Start
1050     -- Extract encumbrance balances, Include Other Encum Balances.
1051     IF FND_API.to_Boolean(g_include_gl_other_balance)
1052     THEN
1053       IF not FND_API.to_Boolean(g_include_gl_commit_balance)
1054       THEN
1055         IF not FND_API.to_Boolean(g_include_gl_oblig_balance)
1056         THEN
1057           g_sql_encum_balance := g_sql_encum_balance ||
1058                                  'and gb.encumbrance_type_id not in ( :b_oblig_enc_type_id, :b_commit_enc_type_id ) ';
1059         ELSE
1060           g_sql_encum_balance := g_sql_encum_balance ||
1061                                'and gb.encumbrance_type_id not in ( :b_commit_enc_type_id ) ';
1062         END IF;
1063       ELSE
1064         IF NOT FND_API.to_Boolean(g_include_gl_oblig_balance)
1065         THEN
1066           g_sql_encum_balance := g_sql_encum_balance ||
1067                                  'and gb.encumbrance_type_id not in ( :b_oblig_enc_type_id ) ';
1068         END IF;
1069       END IF;
1070     ELSE
1071       IF NOT FND_API.to_Boolean(g_include_gl_commit_balance)
1072       THEN
1073         IF FND_API.to_Boolean(g_include_gl_oblig_balance)
1074 	THEN
1075           g_sql_encum_balance := g_sql_encum_balance ||
1076                                  'and gb.encumbrance_type_id in ( :b_oblig_enc_type_id ) ';
1077         END IF;
1078       ELSE
1079         IF not FND_API.to_Boolean(g_include_gl_oblig_balance)
1080 	THEN
1081           g_sql_encum_balance := g_sql_encum_balance ||
1082                                  'and gb.encumbrance_type_id in ( :b_commit_enc_type_id ) ';
1083         ELSE
1084           g_sql_encum_balance := g_sql_encum_balance ||
1085                                  'and gb.encumbrance_type_id in ( :b_commit_enc_type_id, :b_oblig_enc_type_id ) ';
1086         END IF;
1087       END IF;
1088     END IF;
1089     -- Bug#5030613 End
1090 
1091   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1092     raise FND_API.G_EXC_ERROR;
1093   end if;
1094 
1095   /* start bug 4256345 */
1096   IF p_gl_budget_set_id IS NOT NULL THEN
1097     check_ccid_bal (x_return_status  		=> l_return_status,
1098                     p_gl_budget_set_id    => p_gl_budget_set_id);
1099 
1100     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1101       RAISE FND_API.G_EXC_ERROR;
1102     END IF;
1103   END IF;
1104   /* end bug 4256345 */
1105 
1106 
1107   g_cur_budget_balance := dbms_sql.open_cursor;
1108   dbms_sql.parse(g_cur_budget_balance, g_sql_budget_balance, dbms_sql.v7);
1109 
1110   g_cur_actual_balance := dbms_sql.open_cursor;
1111   dbms_sql.parse(g_cur_actual_balance, g_sql_actual_balance, dbms_sql.v7);
1112 
1113   g_cur_encum_balance := dbms_sql.open_cursor;
1114   dbms_sql.parse(g_cur_encum_balance, g_sql_encum_balance, dbms_sql.v7);
1115 
1116   -- Currently, only Actual and Budget Balances are extracted from GL
1117 
1118   g_balance_type(1) := 'A';
1119   g_balance_type(2) := 'B';
1120 
1121   if ((FND_API.to_Boolean(p_include_gl_commit_balance)) or
1122       (FND_API.to_Boolean(p_include_gl_oblig_balance)) or
1123       (FND_API.to_Boolean(p_include_gl_other_balance))) then
1124     g_balance_type(3) := 'E';
1125   end if;
1126 
1127   for c_BudGrp_Rec in c_BudGrp (p_budget_group_id) loop
1128     -- Find Account Sets for the Budget Group
1129 
1130     for c_AccSet_Rec in c_AccSet (c_BudGrp_Rec.budget_group_id) loop
1131 
1132       -- for bug 4256345
1133       --open c_ccids(c_AccSet_Rec.account_position_set_id);
1134         open l_ccid_csr(c_AccSet_Rec.account_position_set_id);
1135 
1136       loop
1137 
1138         -- for bug 4256345
1139 	/* fetch c_ccids BULK COLLECT INTO l_ccids.ccid, l_ccids.start_date, l_ccids.end_date
1140 				  LIMIT PSB_WS_ACCT1.g_limit_bulk_numrows; */
1141            fetch l_ccid_csr BULK COLLECT INTO l_ccids.ccid LIMIT PSB_WS_ACCT1.g_limit_bulk_numrows;
1142 
1143 
1144 	for l_ccid_index in 1..l_ccids.ccid.count loop
1145           /* start bug 4256345 */
1146 	      BEGIN
1147 	        SELECT 'TRUE', start_date_active, end_date_active
1148 		INTO lt_valid_ccid, lt_start_date_active, lt_end_date_active
1149 		FROM gl_code_combinations
1150 		WHERE code_combination_id = l_ccids.ccid(l_ccid_index)
1151 		AND enabled_flag = 'Y'
1152 		AND detail_budgeting_allowed_flag = 'Y';
1153 	      EXCEPTION
1154 	        WHEN NO_DATA_FOUND then
1155 		  lt_valid_ccid := 'FALSE';
1156 		WHEN TOO_MANY_ROWS then
1157 		  lt_valid_ccid := 'FALSE';
1158 	       END;
1159 
1160           IF lt_valid_ccid = 'TRUE' THEN
1161 	 /* end bug 4256345 */
1162 
1163 	  -- Check whether CCID belongs to Personnel or Non Personnel Services.
1164 	  PSB_WS_ACCT1.Check_CCID_Type
1165 	  ( p_api_version     => 1.0,
1166 	    p_return_status   => l_return_status,
1167 	    p_ccid_type       => l_ccid_type,
1168 	    p_flex_code       => p_flex_code,
1169 	    p_ccid            => l_ccids.ccid(l_ccid_index),
1170 	    p_budget_group_id => c_BudGrp_Rec.budget_group_id
1171           ) ;
1172           --
1173 	  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1174 	    raise FND_API.G_EXC_ERROR;
1175 	  end if;
1176 
1177 	  g_actuals_func_total := 0;
1178 	  g_actuals_stat_total := 0;
1179 
1180 	  GL_CODE_COMBINATIONS_PKG.Select_Columns
1181 	  ( X_code_combination_id => l_ccids.ccid(l_ccid_index) ,
1182 	    X_account_type        => l_account_type             ,
1183 	    X_template_id         => l_template_id              ) ;
1184 
1185            /* start bug 4256345 */
1186 	   /*
1187 	  l_ccid_start_period := greatest(NVL(l_ccids.start_date(l_ccid_index),
1188 				          c_AccSet_Rec.effective_start_date),
1189                                           c_AccSet_Rec.effective_start_date); */
1190 
1191           l_ccid_start_period := greatest(NVL(lt_start_date_active,
1192 				          			  c_AccSet_Rec.effective_start_date),
1193                                               c_AccSet_Rec.effective_start_date);
1194           /* end bug 4256345 */
1195 
1196           /* Bug No 2640277 Start */
1197           -- l_ccid_end_period := least(nvl(l_ccids.end_date(l_ccid_index),
1198           --                            c_AccSet_Rec.effective_end_date),
1199           --                            c_AccSet_Rec.effective_end_date);
1200 
1201           /* start bug 4256345 */
1202 	  /* l_ccid_end_period := least(nvl(l_ccids.end_date(l_ccid_index),
1203 					 c_AccSet_Rec.effective_end_date),
1204 					 nvl(c_AccSet_Rec.effective_end_date,
1205                                              l_ccids.end_date(l_ccid_index))); */
1206 	     l_ccid_end_period := least(nvl(lt_end_date_active,
1207 					 				c_AccSet_Rec.effective_end_date),
1208 					 			nvl(c_AccSet_Rec.effective_end_date,
1209                                             lt_end_date_active));
1210 	   /* end bug 4256345 */
1211 
1212           /* Bug No 2640277 End */
1213 
1214           -- Process all budget years for the CCID.
1215 	  for l_year_index in 1..PSB_WS_ACCT1.g_num_budget_years loop
1216 
1217 	    l_year_start_date :=
1218                         PSB_WS_ACCT1.g_budget_years(l_year_index).start_date;
1219 	    l_year_end_date :=
1220                           PSB_WS_ACCT1.g_budget_years(l_year_index).end_date;
1221 
1222             -- Check if CCID is valid for the year being processed.
1223 	    if (((l_ccid_start_period <= l_year_end_date) and (l_ccid_end_period is null))
1224 	     or ((l_ccid_start_period between l_year_start_date and l_year_end_date)
1225 	      or (l_ccid_end_period between l_year_start_date and l_year_end_date)
1226 	      or ((l_ccid_start_period < l_year_start_date)
1227 	      and (l_ccid_end_period > l_year_end_date))))
1228             then
1229 
1230               -- Process PY and CY balances.
1231               if PSB_WS_ACCT1.g_budget_years(l_year_index).year_type
1232                  IN ('PY', 'CY')
1233               then
1234 
1235                 for l_init_index in 1..g_gl_actual_periods.Count loop
1236 		  g_gl_actual_periods(l_init_index).budget_period_id := null;
1237 		  g_gl_actual_periods(l_init_index).long_sequence_no := null;
1238 		  g_gl_actual_periods(l_init_index).start_date := null;
1239 		  g_gl_actual_periods(l_init_index).end_date := null;
1240 		  g_gl_actual_periods(l_init_index).budget_year_id := null;
1241                 end loop;
1242 
1243                 for l_init_index in 1..g_gl_budget_periods.Count loop
1244 		  g_gl_budget_periods(l_init_index).budget_period_id := null;
1245 		  g_gl_budget_periods(l_init_index).long_sequence_no := null;
1246 		  g_gl_budget_periods(l_init_index).start_date := null;
1247 		  g_gl_budget_periods(l_init_index).end_date := null;
1248 		  g_gl_budget_periods(l_init_index).budget_year_id := null;
1249                 end loop;
1250 
1251 		l_init_index := 1;
1252 
1253 		g_num_actual_periods := 0;
1254 		g_num_budget_periods := 0;
1255 
1256                 -- Process budget periods for the year being processed.
1257 		for l_period_index in 1..PSB_WS_ACCT1.g_num_budget_periods loop
1258 
1259 		  -- Extract Budget Balances for Budget Periods in CY
1260 
1261 		  if (PSB_WS_ACCT1.g_budget_periods(l_period_index).budget_year_id =
1262 		    PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id)
1263                   then
1264 
1265 		    g_gl_budget_periods(l_init_index).budget_period_id :=
1266 						      PSB_WS_ACCT1.g_budget_periods(l_period_index).budget_period_id;
1267 		    g_gl_budget_periods(l_init_index).long_sequence_no :=
1268 						      PSB_WS_ACCT1.g_budget_periods(l_period_index).long_sequence_no;
1269 		    g_gl_budget_periods(l_init_index).start_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).start_date;
1270 		    g_gl_budget_periods(l_init_index).end_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).end_date;
1271 		    g_gl_budget_periods(l_init_index).budget_year_id := PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id;
1272 
1273 		    g_num_budget_periods := g_num_budget_periods + 1;
1274 
1275 		    -- Extract Actuals for Budget Periods in CY up to GL
1276                     -- Cutoff Date
1277 
1278 		    if ((p_gl_cutoff_period is null) or
1279 			(p_gl_cutoff_period > PSB_WS_ACCT1.g_budget_periods(l_period_index).start_date))
1280                     then
1281 		      g_gl_actual_periods(l_init_index).budget_period_id :=
1282 							PSB_WS_ACCT1.g_budget_periods(l_period_index).budget_period_id;
1283 		      g_gl_actual_periods(l_init_index).long_sequence_no :=
1284 							PSB_WS_ACCT1.g_budget_periods(l_period_index).long_sequence_no;
1285 		      g_gl_actual_periods(l_init_index).start_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).start_date;
1286 		      g_gl_actual_periods(l_init_index).end_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).end_date;
1287 		      g_gl_actual_periods(l_init_index).budget_year_id := PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id;
1288 
1289 		      g_num_actual_periods := g_num_actual_periods + 1;
1290 
1291 		    end if;
1292 
1293 		    l_init_index := l_init_index + 1;
1294 
1295 		  end if;
1296 
1297 		end loop;
1298                 -- End processing budget periods for the year being processed.
1299 
1300 		-- Get GL Balances for CCID
1301 		Get_Balances
1302 		( p_return_status => l_return_status,
1303 		  p_ccid => l_ccids.ccid(l_ccid_index),
1304 		  p_account_type => l_account_type,
1305 		  p_budget_year_id =>
1306                       PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id,
1307 		  p_year_name =>
1308                            PSB_WS_ACCT1.g_budget_years(l_year_index).year_name,
1309 		  p_year_start_date =>
1310                           PSB_WS_ACCT1.g_budget_years(l_year_index).start_date,
1311 		  p_year_end_date =>
1312                             PSB_WS_ACCT1.g_budget_years(l_year_index).end_date,
1313 		  p_budget_year_type =>
1314                            PSB_WS_ACCT1.g_budget_years(l_year_index).year_type,
1315 		  p_incl_stat_bal => p_incl_stat_bal,
1316 		  p_incl_trans_bal => p_incl_trans_bal,
1317 		  p_incl_adj_period => p_incl_adj_period,
1318 		  p_set_of_books_id => p_set_of_books_id,
1319 		  p_budget_group_id => c_BudGrp_Rec.budget_group_id,
1320 		  p_stage_set_id => p_stage_set_id,
1321 		  p_func_currency => p_func_currency,
1322 		  p_budgetary_control => p_budgetary_control,
1323 		  p_budget_version_id => p_budget_version_id,
1324 		  p_flex_mapping_set_id => p_flex_mapping_set_id,
1325 		  p_gl_budget_set_id => p_gl_budget_set_id,
1326 		  p_flex_code => p_flex_code,
1327 		  p_worksheet_id => p_worksheet_id,
1328 		  p_service_package_id => p_service_package_id,
1329 		  p_sequence_number => p_start_stage_seq,
1330 		  p_rounding_factor => p_rounding_factor,
1331                   /* bug no 4725091 */
1332                   p_incl_gl_fwd_balance => p_incl_gl_fwd_balance,
1333                   p_create_wks_flag     => fnd_api.g_true ) ;
1334 
1335                 --
1336                 if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1337 		  raise FND_API.G_EXC_ERROR;
1338 	        end if;
1339 
1340 	      end if;
1341               -- End processing PY and CY balances.
1342 
1343 	    end if;
1344             -- Check if CCID is valid for the year being processed.
1345 
1346 	  end loop;
1347           -- End processing all budget years for the CCID.
1348 
1349 	  IF p_parameter_set_id IS NOT NULL THEN
1350 
1351 	    -- Number of Years to project a CCID is max of number of years
1352             -- specified for Budget Group to which CCID is assigned (through
1353             -- Account Set) and number of years specified in WS definition
1354 	    l_num_projected_years :=
1355                    greatest( nvl(c_BudGrp_Rec.num_proposed_years, 0),
1356                              nvl(p_num_proposed_years, 0) );
1357 
1358 	    if l_num_projected_years = 0 then
1359 	      l_num_projected_years := null;
1360 	    end if;
1361 
1362 	    -- Compute CY and PP Balances for CCID by applying Parameters
1363             IF ( l_ccid_type <> 'PERSONNEL_SERVICES'
1364                  AND
1365                  fnd_api.to_boolean(p_budget_by_position)
1366                )
1367                OR
1368                NOT fnd_api.to_boolean(p_budget_by_position)
1369             THEN
1370 
1371 	      Apply_Account_Parameters
1372 	      ( p_api_version           => 1.0,
1373                 p_return_status         => l_return_status,
1374                 p_worksheet_id          => p_worksheet_id,
1375                 p_service_package_id    => p_service_package_id,
1376                 p_start_stage_seq       => p_start_stage_seq,
1377                 p_current_stage_seq     => p_start_stage_seq,
1378                 p_rounding_factor       => p_rounding_factor,
1379                 p_stage_set_id          => p_stage_set_id,
1380                 p_budget_group_id       => c_BudGrp_Rec.budget_group_id,
1381                 p_allocrule_set_id      => p_allocrule_set_id,
1382                 p_gl_cutoff_period      => p_gl_cutoff_period,
1383                 p_flex_code             => p_flex_code,
1384                 p_func_currency         => p_func_currency,
1385                 p_flex_mapping_set_id   => p_flex_mapping_set_id,
1386                 p_ccid                  => l_ccids.ccid(l_ccid_index),
1387                 p_ccid_start_period     => l_ccid_start_period,
1388                 p_ccid_end_period       => l_ccid_end_period,
1389                 p_num_proposed_years    => l_num_projected_years,
1390                 p_num_years_to_allocate => p_num_years_to_allocate,
1391                 p_parameter_set_id      => p_parameter_set_id,
1392                 p_budget_calendar_id    => p_budget_calendar_id,
1393                 p_budget_by_position    => p_budget_by_position
1394               ) ;
1395               --
1396 	      IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1397                 RAISE FND_API.G_EXC_ERROR;
1398 	      END IF ;
1399               --
1400 	    END IF ;
1401 
1402 	  END IF ;
1403 	  -- End computing CY and PP Balances for CCID by applying Parameters
1404 
1405 	  l_num_accounts := l_num_accounts + 1;
1406 	  if l_num_accounts > PSB_WS_ACCT1.g_checkpoint_save then
1407 	    commit work;
1408 	    l_num_accounts := 0;
1409 	  end if;
1410 
1411           -- valid ccid check ends here
1412           -- for bug 4256345
1413            END IF;
1414 
1415 	end loop; /* CCID */
1416 
1417 	-- for bug 4256345
1418 	-- exit when c_ccids%NOTFOUND;
1419            exit when l_ccid_csr%NOTFOUND;
1420 
1421       end loop; /* c_ccid */
1422       -- for bug 4256345
1423       -- close c_ccids;
1424          close l_ccid_csr;
1425 
1426     end loop; /* Account Set */
1427 
1428     commit work;
1429 
1430   end loop; /* Budget Group */
1431 
1432   -- Process CCIDs that were deferred for processing during the initial phase
1433 
1434   if g_num_defccids <> 0 then
1435 
1436     Process_Deferred_CCIDs
1437     ( p_return_status         => l_return_status,
1438       p_worksheet_id          => p_worksheet_id,
1439       p_service_package_id    => p_service_package_id,
1440       p_sequence_number       => p_start_stage_seq,
1441       p_gl_cutoff_period      => p_gl_cutoff_period,
1442       p_allocrule_set_id      => p_allocrule_set_id,
1443       p_rounding_factor       => p_rounding_factor,
1444       p_stage_set_id          => p_stage_set_id,
1445       p_flex_mapping_set_id   => p_flex_mapping_set_id,
1446       p_flex_code             => p_flex_code,
1447       p_func_currency         => p_func_currency,
1448       p_num_years_to_allocate => p_num_years_to_allocate,
1449       p_parameter_set_id      => p_parameter_set_id,
1450       p_budget_calendar_id    => p_budget_calendar_id,
1451       p_budget_by_position    => p_budget_by_position
1452     ) ;
1453 
1454     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1455       raise FND_API.G_EXC_ERROR;
1456     end if;
1457 
1458   end if;
1459 
1460   dbms_sql.close_cursor(g_cur_budget_balance);
1461   dbms_sql.close_cursor(g_cur_actual_balance);
1462   dbms_sql.close_cursor(g_cur_encum_balance);
1463 
1464 
1465   -- Initialize API return status to success
1466 
1467   p_return_status := FND_API.G_RET_STS_SUCCESS;
1468 
1469 
1470 EXCEPTION
1471 
1472    when FND_API.G_EXC_ERROR then
1473      p_return_status := FND_API.G_RET_STS_ERROR;
1474 
1475      if dbms_sql.is_open(g_cur_budget_balance) then
1476        dbms_sql.close_cursor(g_cur_budget_balance);
1477      end if;
1478 
1479      if dbms_sql.is_open(g_cur_actual_balance) then
1480        dbms_sql.close_cursor(g_cur_actual_balance);
1481      end if;
1482 
1483      if dbms_sql.is_open(g_cur_encum_balance) then
1484        dbms_sql.close_cursor(g_cur_encum_balance);
1485      end if;
1486 
1487      -- FOR BUG 4256345
1488      /* if c_ccids%ISOPEN then
1489        close c_ccids;
1490      end if; */
1491 
1492      if l_ccid_csr%ISOPEN then
1493        close l_ccid_csr;
1494      end if;
1495 
1496 
1497    when FND_API.G_EXC_UNEXPECTED_ERROR then
1498      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1499 
1500      if dbms_sql.is_open(g_cur_budget_balance) then
1501        dbms_sql.close_cursor(g_cur_budget_balance);
1502      end if;
1503 
1504      if dbms_sql.is_open(g_cur_actual_balance) then
1505        dbms_sql.close_cursor(g_cur_actual_balance);
1506      end if;
1507 
1508      if dbms_sql.is_open(g_cur_encum_balance) then
1509        dbms_sql.close_cursor(g_cur_encum_balance);
1510      end if;
1511 
1512      -- FOR BUG 4256345
1513      /* if c_ccids%ISOPEN then
1514        close c_ccids;
1515      end if; */
1516 
1517      if l_ccid_csr%ISOPEN then
1518        close l_ccid_csr;
1519      end if;
1520 
1521 
1522    when OTHERS then
1523      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1524 
1525      if dbms_sql.is_open(g_cur_budget_balance) then
1526        dbms_sql.close_cursor(g_cur_budget_balance);
1527      end if;
1528 
1529      if dbms_sql.is_open(g_cur_actual_balance) then
1530        dbms_sql.close_cursor(g_cur_actual_balance);
1531      end if;
1532 
1533      if dbms_sql.is_open(g_cur_encum_balance) then
1534        dbms_sql.close_cursor(g_cur_encum_balance);
1535      end if;
1536 
1537      -- FOR BUG 4256345
1538      /* if c_ccids%ISOPEN then
1539        close c_ccids;
1540      end if; */
1541 
1542      if l_ccid_csr%ISOPEN then
1543        close l_ccid_csr;
1544      end if;
1545 
1546      if FND_MSG_PUB.Check_Msg_Level
1547        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
1548      then
1549        FND_MSG_PUB.Add_Exc_Msg
1550 	  (p_pkg_name => G_PKG_NAME,
1551 	   p_procedure_name => 'Create_Worksheet_Accounts');
1552      end if;
1553 
1554 END Create_Worksheet_Accounts;
1555 /* ----------------------------------------------------------------------- */
1556 
1557 
1558 /*===========================================================================+
1559  |                  PROCEDURE Distribute_Prorated_Amount_Pvt                 |
1560  +===========================================================================*/
1561 --
1562 -- This API distributes a given amount over a set of periods using a prorated
1563 -- basis. The prorated basis is dervied based on existing amounts in periods.
1564 --
1565 PROCEDURE Distribute_Prorated_Amount_Pvt
1566 (
1567   x_return_status            OUT    NOCOPY  VARCHAR2,
1568   --
1569   p_spread_amount            IN             NUMBER,
1570   x_period_amount            IN OUT NOCOPY  PSB_WS_Acct1.g_prdamt_tbl_type
1571 )
1572 IS
1573   --
1574   l_api_name       CONSTANT  VARCHAR2(30) := 'Distribute_Prorated_Amount_Pvt';
1575   l_return_status            VARCHAR2(1);
1576   --
1577   l_start_index              NUMBER;
1578   l_end_index                NUMBER;
1579   l_non_zero_periods_count   NUMBER;
1580   l_non_zero_periods_total   NUMBER;
1581   l_spread_amount            NUMBER;
1582   --
1583 BEGIN
1584   --
1585   x_return_status := FND_API.G_RET_STS_SUCCESS ;
1586   --
1587 
1588   -- Initialize variables.
1589   l_spread_amount          := p_spread_amount ;
1590   l_non_zero_periods_count := 0 ;
1591   l_non_zero_periods_total := 0 ;
1592 
1593   -- Return if there are no periods to process.
1594   IF g_alloc_periods.COUNT <= 0 THEN
1595     RETURN ;
1596   END IF ;
1597 
1598   -- Get indexes related to start and end periods.
1599   l_start_index := g_alloc_periods.FIRST;
1600   l_end_index   := g_alloc_periods.LAST ;
1601 
1602   -- Find number of periods having null or zero amounts.
1603   FOR i IN l_start_index..l_end_index
1604   LOOP
1605     --pd('Before :' || i || ':' || x_period_amount(i) ) ;
1606     IF NVL(x_period_amount(i),0) <> 0 THEN
1607       l_non_zero_periods_count := l_non_zero_periods_count + 1 ;
1608       l_non_zero_periods_total := l_non_zero_periods_total +
1609                                   NVL(x_period_amount(i),0) ;
1610     END IF ;
1611   END LOOP ;
1612 
1613   --pd('l_non_zero_periods_count:'||l_non_zero_periods_count) ;
1614   --pd('l_non_zero_periods_total:'||l_non_zero_periods_total) ;
1615 
1616   --
1617   -- We need to consider negative spread and period amounts. The following
1618   -- table illustrates such combinations.
1619   --
1620   -- Net Period Amt   Spread   Make Spread
1621   -- +ve              +ve      no change
1622   -- +ve              -ve      no change
1623   -- -ve              +ve      spread * -1
1624   -- -ve              -ve      spread * -1
1625   --
1626   IF l_non_zero_periods_total < 0 THEN
1627     l_spread_amount := l_spread_amount * -1 ;
1628   END IF ;
1629 
1630   -- Get absolute divide by factor.
1631   l_non_zero_periods_total := ABS(l_non_zero_periods_total) ;
1632 
1633   --
1634   -- If l_non_zero_periods_total is 0, this means we cannot use existing period
1635   -- amounts for proration as the end result will always be 0. For this case we
1636   -- will do uniform distribution by resetting l_non_zero_periods_count to 0.
1637   --
1638   IF l_non_zero_periods_total = 0 THEN
1639     l_non_zero_periods_count := 0 ;
1640   END IF ;
1641 
1642   -- Check different spreading conditions.
1643   IF l_spread_amount = 0 THEN
1644 
1645     -- Check if there is any need to reset period amounts.
1646     IF l_non_zero_periods_total <> 0 THEN
1647       FOR i IN l_start_index..l_end_index
1648       LOOP
1649         x_period_amount(i) := 0 ;
1650       END LOOP ;
1651     END IF ;
1652 
1653   -- If all the periods are 0s, do the uniform distribution.
1654   ELSIF l_non_zero_periods_count = 0 THEN
1655 
1656     FOR i IN l_start_index..l_end_index
1657     LOOP
1658       x_period_amount(i) := l_spread_amount/g_alloc_periods.COUNT ;
1659     END LOOP ;
1660 
1661   -- Othewise do prorate distribution based on existing amounts.
1662   ELSE
1663 
1664    FOR i IN l_start_index..l_end_index
1665    LOOP
1666       x_period_amount(i) := ( x_period_amount(i) / l_non_zero_periods_total ) *
1667                             l_spread_amount ;
1668     END LOOP ;
1669 
1670   END IF ;
1671   -- End checking different spreading conditions.
1672 
1673 EXCEPTION
1674   WHEN OTHERS THEN
1675     --
1676     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1677     --
1678     IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR ) THEN
1679       FND_MSG_PUB.Add_Exc_Msg (  G_PKG_NAME,
1680                                  l_api_name ) ;
1681     END IF;
1682     --
1683 END Distribute_Prorated_Amount_Pvt ;
1684 /*---------------------------------------------------------------------------*/
1685 
1686 
1687 /*===========================================================================+
1688  |                     PROCEDURE Update_CY_Estimates                         |
1689  +===========================================================================*/
1690 --
1691 -- This API updates CY estimates amounts. It copies actuals from CY Actual
1692 -- balances upto GL cutoff date and then spreads 'CY Estimates - CY Actuals'
1693 -- over post GL cutoff periods.
1694 --
1695 PROCEDURE Update_CY_Estimates
1696 ( p_return_status       OUT  NOCOPY  VARCHAR2,
1697   p_worksheet_id        IN           NUMBER,
1698   p_service_package_id  IN           NUMBER,
1699   p_rounding_factor     IN           NUMBER,
1700   p_start_stage_seq     IN           NUMBER,
1701   p_budget_group_id     IN           NUMBER,
1702   p_stage_set_id        IN           NUMBER,
1703   p_budget_year_id      IN           NUMBER,
1704   p_ccid                IN           NUMBER,
1705   p_currency_code       IN           VARCHAR2
1706 )
1707 IS
1708   --
1709   l_return_status       VARCHAR2(1);
1710   l_start_index         PLS_INTEGER;
1711   l_end_index           PLS_INTEGER;
1712   l_account_line_id     NUMBER;
1713   l_period_amount       PSB_WS_Acct1.g_prdamt_tbl_type;
1714   -- Bug 3548345
1715   -- l_create_zero_bal     VARCHAR2(1);
1716   --
1717   l_ytd_actual_amount   NUMBER := 0;
1718   l_ytd_estimate_amount NUMBER := 0;
1719   l_ytd_spread_amount   NUMBER := 0;
1720   --
1721 
1722   --
1723   -- There are 2 separate cursors c_WAL_Act (for Actuals) and c_WAL_Est
1724   -- (for Estimates) to avoid static binding of cursors.
1725   --
1726   CURSOR c_WAL_Act IS
1727     SELECT ytd_amount,
1728            period1_amount,  period2_amount,  period3_amount,  period4_amount,
1729            period5_amount,  period6_amount,  period7_amount,  period8_amount,
1730            period9_amount,  period10_amount, period11_amount, period12_amount,
1731            period13_amount, period14_amount, period15_amount, period16_amount,
1732            period17_amount, period18_amount, period19_amount, period20_amount,
1733            period21_amount, period22_amount, period23_amount, period24_amount,
1734            period25_amount, period26_amount, period27_amount, period28_amount,
1735            period29_amount, period30_amount, period31_amount, period32_amount,
1736            period33_amount, period34_amount, period35_amount, period36_amount,
1737            period37_amount, period38_amount, period39_amount, period40_amount,
1738            period41_amount, period42_amount, period43_amount, period44_amount,
1739            period45_amount, period46_amount, period47_amount, period48_amount,
1740            period49_amount, period50_amount, period51_amount, period52_amount,
1741            period53_amount, period54_amount, period55_amount, period56_amount,
1742            period57_amount, period58_amount, period59_amount, period60_amount
1743       FROM PSB_WS_ACCOUNT_LINES a
1744      WHERE template_id IS NULL
1745        AND position_line_id IS NULL
1746        AND currency_code = p_currency_code
1747        AND p_start_stage_seq BETWEEN start_stage_seq AND current_stage_seq
1748        AND balance_type = 'A'
1749        AND EXISTS
1750           (SELECT 1
1751              FROM PSB_WS_LINES b
1752             WHERE b.account_line_id = a.account_line_id
1753               AND b.worksheet_id = p_worksheet_id)
1754        AND stage_set_id        = p_stage_set_id
1755        AND service_package_id  = p_service_package_id
1756        AND budget_year_id      = p_budget_year_id
1757        AND budget_group_id     = p_budget_group_id
1758        AND code_combination_id = p_ccid;
1759   --
1760   CURSOR c_WAL_Est IS
1761     SELECT code_combination_id,ytd_amount,
1762            period1_amount, period2_amount, period3_amount, period4_amount,
1763            period5_amount, period6_amount, period7_amount, period8_amount,
1764            period9_amount, period10_amount, period11_amount, period12_amount,
1765            period13_amount, period14_amount, period15_amount, period16_amount,
1766            period17_amount, period18_amount, period19_amount, period20_amount,
1767            period21_amount, period22_amount, period23_amount, period24_amount,
1768            period25_amount, period26_amount, period27_amount, period28_amount,
1769            period29_amount, period30_amount, period31_amount, period32_amount,
1770            period33_amount, period34_amount, period35_amount, period36_amount,
1771            period37_amount, period38_amount, period39_amount, period40_amount,
1772            period41_amount, period42_amount, period43_amount, period44_amount,
1773            period45_amount, period46_amount, period47_amount, period48_amount,
1774            period49_amount, period50_amount, period51_amount, period52_amount,
1775            period53_amount, period54_amount, period55_amount, period56_amount,
1776            period57_amount, period58_amount, period59_amount, period60_amount
1777       FROM PSB_WS_ACCOUNT_LINES a
1778      WHERE template_id IS NULL
1779        AND position_line_id IS NULL
1780        AND currency_code = p_currency_code
1781        AND p_start_stage_seq BETWEEN start_stage_seq AND current_stage_seq
1782        AND balance_type = 'E'
1783        AND EXISTS
1784           (SELECT 1
1785              FROM PSB_WS_LINES b
1786             WHERE b.account_line_id = a.account_line_id
1787               AND b.worksheet_id = p_worksheet_id)
1788        AND stage_set_id        = p_stage_set_id
1789        AND service_package_id  = p_service_package_id
1790        AND budget_year_id      = p_budget_year_id
1791        AND budget_group_id     = p_budget_group_id
1792        AND code_combination_id = p_ccid;
1793   --
1794 BEGIN
1795 
1796   -- Initialize period table for the current account line.
1797   FOR i IN 1..PSB_WS_ACCT1.g_max_num_amounts LOOP
1798     l_period_amount(i) := NULL ;
1799   END LOOP ;
1800 
1801   --
1802   -- Copy CY estimate periods from CY actuals till GL cutoff date. Note these
1803   -- have been populated by Get_Balances API in main Update_GL_Balances API.
1804   --
1805   FOR c_WAL_Rec in c_WAL_Act LOOP
1806 
1807     l_ytd_actual_amount := NVL(c_WAL_Rec.ytd_amount,0);
1808 
1809     if c_WAL_Rec.period1_amount is not null then
1810       l_period_amount(1) := c_WAL_Rec.period1_amount;
1811     end if;
1812 
1813     if c_WAL_Rec.period2_amount is not null then
1814       l_period_amount(2) := c_WAL_Rec.period2_amount;
1815     end if;
1816 
1817     if c_WAL_Rec.period3_amount is not null then
1818       l_period_amount(3) := c_WAL_Rec.period3_amount;
1819     end if;
1820 
1821     if c_WAL_Rec.period4_amount is not null then
1822       l_period_amount(4) := c_WAL_Rec.period4_amount;
1823     end if;
1824 
1825     if c_WAL_Rec.period5_amount is not null then
1826       l_period_amount(5) := c_WAL_Rec.period5_amount;
1827     end if;
1828 
1829     if c_WAL_Rec.period6_amount is not null then
1830       l_period_amount(6) := c_WAL_Rec.period6_amount;
1831     end if;
1832 
1833     if c_WAL_Rec.period7_amount is not null then
1834       l_period_amount(7) := c_WAL_Rec.period7_amount;
1835     end if;
1836 
1837     if c_WAL_Rec.period8_amount is not null then
1838       l_period_amount(8) := c_WAL_Rec.period8_amount;
1839     end if;
1840 
1841     if c_WAL_Rec.period9_amount is not null then
1842       l_period_amount(9) := c_WAL_Rec.period9_amount;
1843     end if;
1844 
1845     if c_WAL_Rec.period10_amount is not null then
1846       l_period_amount(10) := c_WAL_Rec.period10_amount;
1847     end if;
1848 
1849     if c_WAL_Rec.period11_amount is not null then
1850       l_period_amount(11) := c_WAL_Rec.period11_amount;
1851     end if;
1852 
1853     if c_WAL_Rec.period12_amount is not null then
1854       l_period_amount(12) := c_WAL_Rec.period12_amount;
1855     end if;
1856 
1857     if c_WAL_Rec.period13_amount is not null then
1858       l_period_amount(13) := c_WAL_Rec.period13_amount;
1859     end if;
1860 
1861     if c_WAL_Rec.period14_amount is not null then
1862       l_period_amount(14) := c_WAL_Rec.period14_amount;
1863     end if;
1864 
1865     if c_WAL_Rec.period15_amount is not null then
1866       l_period_amount(15) := c_WAL_Rec.period15_amount;
1867     end if;
1868 
1869     if c_WAL_Rec.period16_amount is not null then
1870       l_period_amount(16) := c_WAL_Rec.period16_amount;
1871     end if;
1872 
1873     if c_WAL_Rec.period17_amount is not null then
1874       l_period_amount(17) := c_WAL_Rec.period17_amount;
1875     end if;
1876 
1877     if c_WAL_Rec.period18_amount is not null then
1878       l_period_amount(18) := c_WAL_Rec.period18_amount;
1879     end if;
1880 
1881     if c_WAL_Rec.period19_amount is not null then
1882       l_period_amount(19) := c_WAL_Rec.period19_amount;
1883     end if;
1884 
1885     if c_WAL_Rec.period20_amount is not null then
1886       l_period_amount(20) := c_WAL_Rec.period20_amount;
1887     end if;
1888 
1889     if c_WAL_Rec.period21_amount is not null then
1890       l_period_amount(21) := c_WAL_Rec.period21_amount;
1891     end if;
1892 
1893     if c_WAL_Rec.period22_amount is not null then
1894       l_period_amount(22) := c_WAL_Rec.period22_amount;
1895     end if;
1896 
1897     if c_WAL_Rec.period23_amount is not null then
1898       l_period_amount(23) := c_WAL_Rec.period23_amount;
1899     end if;
1900 
1901     if c_WAL_Rec.period24_amount is not null then
1902       l_period_amount(24) := c_WAL_Rec.period24_amount;
1903     end if;
1904 
1905     if c_WAL_Rec.period25_amount is not null then
1906       l_period_amount(25) := c_WAL_Rec.period25_amount;
1907     end if;
1908 
1909     if c_WAL_Rec.period26_amount is not null then
1910       l_period_amount(26) := c_WAL_Rec.period26_amount;
1911     end if;
1912 
1913     if c_WAL_Rec.period27_amount is not null then
1914       l_period_amount(27) := c_WAL_Rec.period27_amount;
1915     end if;
1916 
1917     if c_WAL_Rec.period28_amount is not null then
1918       l_period_amount(28) := c_WAL_Rec.period28_amount;
1919     end if;
1920 
1921     if c_WAL_Rec.period29_amount is not null then
1922       l_period_amount(29) := c_WAL_Rec.period29_amount;
1923     end if;
1924 
1925     if c_WAL_Rec.period30_amount is not null then
1926       l_period_amount(30) := c_WAL_Rec.period30_amount;
1927     end if;
1928 
1929     if c_WAL_Rec.period31_amount is not null then
1930       l_period_amount(31) := c_WAL_Rec.period31_amount;
1931     end if;
1932 
1933     if c_WAL_Rec.period32_amount is not null then
1934       l_period_amount(32) := c_WAL_Rec.period32_amount;
1935     end if;
1936 
1937     if c_WAL_Rec.period33_amount is not null then
1938       l_period_amount(33) := c_WAL_Rec.period33_amount;
1939     end if;
1940 
1941     if c_WAL_Rec.period34_amount is not null then
1942       l_period_amount(34) := c_WAL_Rec.period34_amount;
1943     end if;
1944 
1945     if c_WAL_Rec.period35_amount is not null then
1946       l_period_amount(35) := c_WAL_Rec.period35_amount;
1947     end if;
1948 
1949     if c_WAL_Rec.period36_amount is not null then
1950       l_period_amount(36) := c_WAL_Rec.period36_amount;
1951     end if;
1952 
1953     if c_WAL_Rec.period37_amount is not null then
1954       l_period_amount(37) := c_WAL_Rec.period37_amount;
1955     end if;
1956 
1957     if c_WAL_Rec.period38_amount is not null then
1958       l_period_amount(38) := c_WAL_Rec.period38_amount;
1959     end if;
1960 
1961     if c_WAL_Rec.period39_amount is not null then
1962       l_period_amount(39) := c_WAL_Rec.period39_amount;
1963     end if;
1964 
1965     if c_WAL_Rec.period40_amount is not null then
1966       l_period_amount(40) := c_WAL_Rec.period40_amount;
1967     end if;
1968 
1969     if c_WAL_Rec.period41_amount is not null then
1970       l_period_amount(41) := c_WAL_Rec.period41_amount;
1971     end if;
1972 
1973     if c_WAL_Rec.period42_amount is not null then
1974       l_period_amount(42) := c_WAL_Rec.period42_amount;
1975     end if;
1976 
1977     if c_WAL_Rec.period43_amount is not null then
1978       l_period_amount(43) := c_WAL_Rec.period43_amount;
1979     end if;
1980 
1981     if c_WAL_Rec.period44_amount is not null then
1982       l_period_amount(44) := c_WAL_Rec.period44_amount;
1983     end if;
1984 
1985     if c_WAL_Rec.period45_amount is not null then
1986       l_period_amount(45) := c_WAL_Rec.period45_amount;
1987     end if;
1988 
1989     if c_WAL_Rec.period46_amount is not null then
1990       l_period_amount(46) := c_WAL_Rec.period46_amount;
1991     end if;
1992 
1993     if c_WAL_Rec.period47_amount is not null then
1994       l_period_amount(47) := c_WAL_Rec.period47_amount;
1995     end if;
1996 
1997     if c_WAL_Rec.period48_amount is not null then
1998       l_period_amount(48) := c_WAL_Rec.period48_amount;
1999     end if;
2000 
2001     if c_WAL_Rec.period49_amount is not null then
2002       l_period_amount(49) := c_WAL_Rec.period49_amount;
2003     end if;
2004 
2005     if c_WAL_Rec.period50_amount is not null then
2006       l_period_amount(50) := c_WAL_Rec.period50_amount;
2007     end if;
2008 
2009     if c_WAL_Rec.period51_amount is not null then
2010       l_period_amount(51) := c_WAL_Rec.period51_amount;
2011     end if;
2012 
2013     if c_WAL_Rec.period52_amount is not null then
2014       l_period_amount(52) := c_WAL_Rec.period52_amount;
2015     end if;
2016 
2017     if c_WAL_Rec.period53_amount is not null then
2018       l_period_amount(53) := c_WAL_Rec.period53_amount;
2019     end if;
2020 
2021     if c_WAL_Rec.period54_amount is not null then
2022       l_period_amount(54) := c_WAL_Rec.period54_amount;
2023     end if;
2024 
2025     if c_WAL_Rec.period55_amount is not null then
2026       l_period_amount(55) := c_WAL_Rec.period55_amount;
2027     end if;
2028 
2029     if c_WAL_Rec.period56_amount is not null then
2030       l_period_amount(56) := c_WAL_Rec.period56_amount;
2031     end if;
2032 
2033     if c_WAL_Rec.period57_amount is not null then
2034       l_period_amount(57) := c_WAL_Rec.period57_amount;
2035     end if;
2036 
2037     if c_WAL_Rec.period58_amount is not null then
2038       l_period_amount(58) := c_WAL_Rec.period58_amount;
2039     end if;
2040 
2041     if c_WAL_Rec.period59_amount is not null then
2042       l_period_amount(59) := c_WAL_Rec.period59_amount;
2043     end if;
2044 
2045     if c_WAL_Rec.period60_amount is not null then
2046       l_period_amount(60) := c_WAL_Rec.period60_amount;
2047     end if;
2048 
2049     EXIT;
2050 
2051   END LOOP ;
2052 
2053   -- Get indexes related to start and end periods.
2054   l_start_index := g_alloc_periods.FIRST;
2055   l_end_index   := g_alloc_periods.LAST;
2056 
2057   --
2058   -- Now copy existing CY estimate periods beyond the GL cutoff date from
2059   -- the existing account line.
2060   --
2061 
2062   -- Bug#3514350: Check if CY estimates need to be processed.
2063   IF NOT g_process_cy_estimates THEN
2064 
2065     l_ytd_estimate_amount := l_ytd_actual_amount ;
2066 
2067   ELSE
2068 
2069     FOR c_WAL_Rec IN c_WAL_Est LOOP
2070 
2071     l_ytd_estimate_amount := NVL(c_WAL_Rec.ytd_amount,0) ;
2072 
2073     if 1 between l_start_index and l_end_index then
2074       l_period_amount(1) := c_WAL_Rec.period1_amount;
2075     end if;
2076 
2077     if 2 between l_start_index and l_end_index then
2078       l_period_amount(2) := c_WAL_Rec.period2_amount;
2079     end if;
2080 
2081     if 3 between l_start_index and l_end_index then
2082       l_period_amount(3) := c_WAL_Rec.period3_amount;
2083     end if;
2084 
2085     if 4 between l_start_index and l_end_index then
2086       l_period_amount(4) := c_WAL_Rec.period4_amount;
2087     end if;
2088 
2089     if 5 between l_start_index and l_end_index then
2090       l_period_amount(5) := c_WAL_Rec.period5_amount;
2091     end if;
2092 
2093     if 6 between l_start_index and l_end_index then
2094       l_period_amount(6) := c_WAL_Rec.period6_amount;
2095     end if;
2096 
2097     if 7 between l_start_index and l_end_index then
2098       l_period_amount(7) := c_WAL_Rec.period7_amount;
2099     end if;
2100 
2101     if 8 between l_start_index and l_end_index then
2102       l_period_amount(8) := c_WAL_Rec.period8_amount;
2103     end if;
2104 
2105     if 9 between l_start_index and l_end_index then
2106       l_period_amount(9) := c_WAL_Rec.period9_amount;
2107     end if;
2108 
2109     if 10 between l_start_index and l_end_index then
2110       l_period_amount(10) := c_WAL_Rec.period10_amount;
2111     end if;
2112 
2113     if 11 between l_start_index and l_end_index then
2114       l_period_amount(11) := c_WAL_Rec.period11_amount;
2115     end if;
2116 
2117     if 12 between l_start_index and l_end_index then
2118       l_period_amount(12) := c_WAL_Rec.period12_amount;
2119     end if;
2120 
2121     if 13 between l_start_index and l_end_index then
2122       l_period_amount(13) := c_WAL_Rec.period13_amount;
2123     end if;
2124 
2125     if 14 between l_start_index and l_end_index then
2126       l_period_amount(14) := c_WAL_Rec.period14_amount;
2127     end if;
2128 
2129     if 15 between l_start_index and l_end_index then
2130       l_period_amount(15) := c_WAL_Rec.period15_amount;
2131     end if;
2132 
2133     if 16 between l_start_index and l_end_index then
2134       l_period_amount(16) := c_WAL_Rec.period16_amount;
2135     end if;
2136 
2137     if 17 between l_start_index and l_end_index then
2138       l_period_amount(17) := c_WAL_Rec.period17_amount;
2139     end if;
2140 
2141     if 18 between l_start_index and l_end_index then
2142       l_period_amount(18) := c_WAL_Rec.period18_amount;
2143     end if;
2144 
2145     if 19 between l_start_index and l_end_index then
2146       l_period_amount(19) := c_WAL_Rec.period19_amount;
2147     end if;
2148 
2149     if 20 between l_start_index and l_end_index then
2150       l_period_amount(20) := c_WAL_Rec.period20_amount;
2151     end if;
2152 
2153     if 21 between l_start_index and l_end_index then
2154       l_period_amount(21) := c_WAL_Rec.period21_amount;
2155     end if;
2156 
2157     if 22 between l_start_index and l_end_index then
2158       l_period_amount(22) := c_WAL_Rec.period22_amount;
2159     end if;
2160 
2161     if 23 between l_start_index and l_end_index then
2162       l_period_amount(23) := c_WAL_Rec.period23_amount;
2163     end if;
2164 
2165     if 24 between l_start_index and l_end_index then
2166       l_period_amount(24) := c_WAL_Rec.period24_amount;
2167     end if;
2168 
2169     if 25 between l_start_index and l_end_index then
2170       l_period_amount(25) := c_WAL_Rec.period25_amount;
2171     end if;
2172 
2173     if 26 between l_start_index and l_end_index then
2174       l_period_amount(26) := c_WAL_Rec.period26_amount;
2175     end if;
2176 
2177     if 27 between l_start_index and l_end_index then
2178       l_period_amount(27) := c_WAL_Rec.period27_amount;
2179     end if;
2180 
2181     if 28 between l_start_index and l_end_index then
2182       l_period_amount(28) := c_WAL_Rec.period28_amount;
2183     end if;
2184 
2185     if 29 between l_start_index and l_end_index then
2186       l_period_amount(29) := c_WAL_Rec.period29_amount;
2187     end if;
2188 
2189     if 30 between l_start_index and l_end_index then
2190       l_period_amount(30) := c_WAL_Rec.period30_amount;
2191     end if;
2192 
2193     if 31 between l_start_index and l_end_index then
2194       l_period_amount(31) := c_WAL_Rec.period31_amount;
2195     end if;
2196 
2197     if 32 between l_start_index and l_end_index then
2198       l_period_amount(32) := c_WAL_Rec.period32_amount;
2199     end if;
2200 
2201     if 33 between l_start_index and l_end_index then
2202       l_period_amount(33) := c_WAL_Rec.period33_amount;
2203     end if;
2204 
2205     if 34 between l_start_index and l_end_index then
2206       l_period_amount(34) := c_WAL_Rec.period34_amount;
2207     end if;
2208 
2209     if 35 between l_start_index and l_end_index then
2210       l_period_amount(35) := c_WAL_Rec.period35_amount;
2211     end if;
2212 
2213     if 36 between l_start_index and l_end_index then
2214       l_period_amount(36) := c_WAL_Rec.period36_amount;
2215     end if;
2216 
2217     if 37 between l_start_index and l_end_index then
2218       l_period_amount(37) := c_WAL_Rec.period37_amount;
2219     end if;
2220 
2221     if 38 between l_start_index and l_end_index then
2222       l_period_amount(38) := c_WAL_Rec.period38_amount;
2223     end if;
2224 
2225     if 39 between l_start_index and l_end_index then
2226       l_period_amount(39) := c_WAL_Rec.period39_amount;
2227     end if;
2228 
2229     if 40 between l_start_index and l_end_index then
2230       l_period_amount(40) := c_WAL_Rec.period40_amount;
2231     end if;
2232 
2233     if 41 between l_start_index and l_end_index then
2234       l_period_amount(41) := c_WAL_Rec.period41_amount;
2235     end if;
2236 
2237     if 42 between l_start_index and l_end_index then
2238       l_period_amount(42) := c_WAL_Rec.period42_amount;
2239     end if;
2240 
2241     if 43 between l_start_index and l_end_index then
2242       l_period_amount(43) := c_WAL_Rec.period43_amount;
2243     end if;
2244 
2245     if 44 between l_start_index and l_end_index then
2246       l_period_amount(44) := c_WAL_Rec.period44_amount;
2247     end if;
2248 
2249     if 45 between l_start_index and l_end_index then
2250       l_period_amount(45) := c_WAL_Rec.period45_amount;
2251     end if;
2252 
2253     if 46 between l_start_index and l_end_index then
2254       l_period_amount(46) := c_WAL_Rec.period46_amount;
2255     end if;
2256 
2257     if 47 between l_start_index and l_end_index then
2258       l_period_amount(47) := c_WAL_Rec.period47_amount;
2259     end if;
2260 
2261     if 48 between l_start_index and l_end_index then
2262       l_period_amount(48) := c_WAL_Rec.period48_amount;
2263     end if;
2264 
2265     if 49 between l_start_index and l_end_index then
2266       l_period_amount(49) := c_WAL_Rec.period49_amount;
2267     end if;
2268 
2269     if 50 between l_start_index and l_end_index then
2270       l_period_amount(50) := c_WAL_Rec.period50_amount;
2271     end if;
2272 
2273     if 51 between l_start_index and l_end_index then
2274       l_period_amount(51) := c_WAL_Rec.period51_amount;
2275     end if;
2276 
2277     if 52 between l_start_index and l_end_index then
2278       l_period_amount(52) := c_WAL_Rec.period52_amount;
2279     end if;
2280 
2281     if 53 between l_start_index and l_end_index then
2282       l_period_amount(53) := c_WAL_Rec.period53_amount;
2283     end if;
2284 
2285     if 54 between l_start_index and l_end_index then
2286       l_period_amount(54) := c_WAL_Rec.period54_amount;
2287     end if;
2288 
2289     if 55 between l_start_index and l_end_index then
2290       l_period_amount(55) := c_WAL_Rec.period55_amount;
2291     end if;
2292 
2293     if 56 between l_start_index and l_end_index then
2294       l_period_amount(56) := c_WAL_Rec.period56_amount;
2295     end if;
2296 
2297     if 57 between l_start_index and l_end_index then
2298       l_period_amount(57) := c_WAL_Rec.period57_amount;
2299     end if;
2300 
2301     if 58 between l_start_index and l_end_index then
2302       l_period_amount(58) := c_WAL_Rec.period58_amount;
2303     end if;
2304 
2305     if 59 between l_start_index and l_end_index then
2306       l_period_amount(59) := c_WAL_Rec.period59_amount;
2307     end if;
2308 
2309     if 60 between l_start_index and l_end_index then
2310       l_period_amount(60) := c_WAL_Rec.period60_amount;
2311     end if;
2312 
2313     EXIT;
2314 
2315     END LOOP ;
2316 
2317     --l_ytd_actual_amount := NVL(g_actuals_func_total,0) ;
2318     l_ytd_spread_amount := l_ytd_estimate_amount - l_ytd_actual_amount ;
2319 
2320     -- Now prorate CY estimate amounts beyond post GL cutoff periods using
2321     -- new spread computed.
2322     Distribute_Prorated_Amount_Pvt
2323     (
2324       x_return_status          => l_return_status     ,
2325       --
2326       p_spread_amount          => l_ytd_spread_amount ,
2327       x_period_amount          => l_period_amount
2328     ) ;
2329     --
2330     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2331       RAISE FND_API.G_EXC_UNEXPECTED_ERROR ;
2332     END IF ;
2333     --
2334 
2335   END IF ;
2336   -- End checking if  CY estimates need to be processed.
2337 
2338   -- Commented out the following statements for bug 3305778
2339   /* Bug 3543845: Reactivate the following statements, use g_create_zero_bal
2340      and add a new condition to improve performance
2341   */
2342   IF g_create_zero_bal is null THEN
2343     FND_PROFILE.GET
2344       ( name => 'PSB_CREATE_ZERO_BALANCE_ACCT' ,
2345         val  => g_create_zero_bal              ) ;
2346 
2347     if g_create_zero_bal is null then
2348       -- Bug 3543845: Change default behavior to not creating zero balance
2349       g_create_zero_bal := 'N';
2350     end if;
2351   END IF;
2352 
2353   -- Bug 4250468 added the variable g_ws_first_time_creation_flag
2354   -- in the IF condition
2355   IF (PSB_WORKSHEET.g_ws_first_time_creation_flag AND
2356       ( l_ytd_estimate_amount <> 0
2357         OR
2358        (l_ytd_estimate_amount = 0 and g_create_zero_bal = 'Y')
2359        )
2360       )
2361    OR NOT PSB_WORKSHEET.g_ws_first_time_creation_flag THEN
2362 
2363 
2364     --pd('2: Call Create_Account_Dist=> ccid=' || TO_CHAR(p_ccid) ||
2365     --   ', p_budget_year_id=' || TO_CHAR(p_budget_year_id) ||
2366     --   ', p_ytd_amount=' || TO_CHAR(l_ytd_estimate_amount));
2367 
2368     PSB_WS_Acct1.Create_Account_Dist
2369     ( p_api_version        => 1.0,
2370       p_return_status      => l_return_status,
2371       p_account_line_id    => l_account_line_id,
2372       p_worksheet_id       => p_worksheet_id,
2373       p_service_package_id => p_service_package_id,
2374       /* bug start 3996052 */
2375       -- changing the check spal line to true
2376       p_check_spal_exists  => FND_API.G_TRUE,
2377       /* bug end : 3996052 */
2378       p_gl_cutoff_period   => null,
2379       p_allocrule_set_id   => null,
2380       p_budget_calendar_id => null,
2381       p_rounding_factor    => p_rounding_factor,
2382       p_stage_set_id       => p_stage_set_id,
2383       p_budget_year_id     => p_budget_year_id,
2384       p_budget_group_id    => p_budget_group_id,
2385       p_ccid               => p_ccid,
2386       p_currency_code      => p_currency_code,
2387       p_balance_type       => 'E',
2388       p_ytd_amount         => l_ytd_estimate_amount,
2389       p_period_amount      => l_period_amount,
2390       p_start_stage_seq    => p_start_stage_seq,
2391       p_current_stage_seq  => p_start_stage_seq,
2392       /* bug start 3996052 */
2393       p_update_cy_estimate => 'Y'
2394       /* bug end 3996052 */
2395     ) ;
2396     --
2397     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
2398       raise FND_API.G_EXC_ERROR;
2399     end if;
2400 
2401   end if;
2402 
2403   -- Initialize API return status to success
2404   p_return_status := FND_API.G_RET_STS_SUCCESS;
2405 
2406 EXCEPTION
2407   --
2408   WHEN FND_API.G_EXC_ERROR THEN
2409     p_return_status := FND_API.G_RET_STS_ERROR;
2410   --
2411   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2412     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2413   --
2414   WHEN OTHERS THEN
2415     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2416     --
2417     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
2418       FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME,
2419                                 'Update_CY_Estimates') ;
2420     END IF;
2421     --
2422 END Update_CY_Estimates ;
2423 /*---------------------------------------------------------------------------*/
2424 
2425 
2426 /*===========================================================================+
2427  |                     PROCEDURE Update_GL_Balances                          |
2428  +===========================================================================*/
2429 --
2430 -- API updates CY actuals and budget from GL. It also updated CY estimates as
2431 -- per GL cut off date.
2432 --
2433 PROCEDURE Update_GL_Balances
2434 ( p_api_version       IN   NUMBER,
2435   p_validation_level  IN   NUMBER := FND_API.G_VALID_LEVEL_NONE,
2436   p_return_status     OUT  NOCOPY  VARCHAR2,
2437   p_worksheet_id      IN   NUMBER
2438 )
2439 IS
2440   --
2441   l_api_name          CONSTANT VARCHAR2(30)     := 'Update_GL_Balances';
2442   l_api_version       CONSTANT NUMBER           := 1.0;
2443 
2444   l_ccid_index                 PLS_INTEGER;
2445   l_year_index                 PLS_INTEGER;
2446   l_period_index               PLS_INTEGER;
2447   l_init_index                 PLS_INTEGER;
2448 
2449   -- Bug#3545533: Added
2450   l_budget_by_position         VARCHAR2(1);
2451   l_ps_set_id                  NUMBER ;
2452   l_process_cy_estimates       VARCHAR2(1);
2453 
2454   l_stage_set_id               NUMBER;
2455   l_current_stage_seq          NUMBER;
2456   l_budget_version_id          NUMBER;
2457   l_flex_mapping_set_id        NUMBER;
2458   l_gl_budget_set_id           NUMBER;
2459   l_gl_cutoff_period           DATE;
2460   l_incl_stat_bal              VARCHAR2(1);
2461   l_incl_trans_bal             VARCHAR2(1);
2462   l_incl_adj_period            VARCHAR2(1);
2463   l_rounding_factor            NUMBER;
2464   l_budget_group_id            NUMBER;
2465   l_budget_calendar_id         NUMBER;
2466   l_include_gl_commit_balance  VARCHAR2(1);
2467   l_include_gl_oblig_balance   VARCHAR2(1);
2468   l_include_gl_other_balance   VARCHAR2(1);
2469   l_service_package_id         NUMBER;
2470 
2471   l_flex_code                  NUMBER;
2472   l_set_of_books_id            NUMBER;
2473   l_set_of_books_name          VARCHAR2(30);
2474   l_budgetary_control          VARCHAR2(1);
2475   l_func_currency              VARCHAR2(15);
2476 
2477   l_account_type               VARCHAR2(1);
2478   l_template_id                NUMBER;
2479 
2480   l_ccid_start_period          DATE;
2481   l_ccid_end_period            DATE;
2482   l_year_start_date            DATE;
2483   l_year_end_date              DATE;
2484 
2485   l_num_accounts               NUMBER := 0;
2486   l_return_status              VARCHAR2(1);
2487 
2488   TYPE ccid_arr IS TABLE OF NUMBER(15);
2489   TYPE date_arr IS TABLE OF DATE;
2490   TYPE ccid_rec IS RECORD   (  ccid       ccid_arr ,
2491                                start_date date_arr ,
2492                                end_date   date_arr ) ;
2493   --
2494   l_ccids                      ccid_rec;
2495   --
2496   CURSOR c_ccids ( c_account_set_id NUMBER)
2497   IS
2498   SELECT a.code_combination_id, b.start_date_active, b.end_date_active
2499   FROM   psb_budget_accounts  a,
2500          gl_code_combinations b
2501   WHERE  a.account_position_set_id = c_account_set_id
2502   /* Bug 3692601 Start */
2503   AND b.detail_budgeting_allowed_flag = 'Y'
2504   /* Bug 3692601 End */
2505   AND    b.code_combination_id     = a.code_combination_id
2506   AND    b.enabled_flag            = 'Y' ;
2507 
2508   -- Bug#3545533: Added
2509   CURSOR l_check_acct_type ( c_code_combination_id  NUMBER)
2510   IS
2511   SELECT '1'
2512   FROM   dual
2513   WHERE  EXISTS
2514          ( SELECT 1
2515            FROM   psb_budget_accounts
2516            WHERE  code_combination_id     = c_code_combination_id
2517            AND    account_position_set_id = l_ps_set_id ) ;
2518  --
2519 BEGIN
2520 
2521   -- Standard call to check for call compatibility
2522   if not FND_API.Compatible_API_Call (l_api_version,
2523 				      p_api_version,
2524 				      l_api_name,
2525 				      G_PKG_NAME)
2526   then
2527     raise FND_API.G_EXC_UNEXPECTED_ERROR;
2528   end if;
2529 
2530   -- Get worksheet info.
2531   FOR l_ws_rec IN
2532   (
2533     SELECT NVL(budget_by_position, 'N')  budget_by_position,
2534            stage_set_id                 ,
2535            current_stage_seq            ,
2536            budget_version_id            ,
2537            flex_mapping_set_id          ,
2538            gl_budget_set_id             ,
2539            gl_cutoff_period             ,
2540            include_stat_balance         ,
2541            include_translated_balance   ,
2542            include_adjustment_periods   ,
2543            rounding_factor              ,
2544            budget_group_id              ,
2545            budget_calendar_id           ,
2546            include_gl_commit_balance    ,
2547            include_gl_oblig_balance     ,
2548            include_gl_other_balance
2549     FROM   psb_worksheets_v
2550     WHERE  worksheet_id = p_worksheet_id
2551   )
2552   LOOP
2553     l_budget_by_position        := l_ws_rec.budget_by_position;
2554     l_stage_set_id              := l_ws_rec.stage_set_id;
2555     l_current_stage_seq         := l_ws_rec.current_stage_seq;
2556     l_budget_version_id         := l_ws_rec.budget_version_id;
2557     l_flex_mapping_set_id       := l_ws_rec.flex_mapping_set_id;
2558     l_gl_budget_set_id          := l_ws_rec.gl_budget_set_id;
2559     l_gl_cutoff_period          := l_ws_rec.gl_cutoff_period;
2560     l_incl_stat_bal             := l_ws_rec.include_stat_balance;
2561     l_incl_trans_bal            := l_ws_rec.include_translated_balance;
2562     l_incl_adj_period           := l_ws_rec.include_adjustment_periods;
2563     l_rounding_factor           := l_ws_rec.rounding_factor;
2564     l_budget_group_id           := l_ws_rec.budget_group_id;
2565     l_budget_calendar_id        := l_ws_rec.budget_calendar_id;
2566     l_include_gl_commit_balance := l_ws_rec.include_gl_commit_balance;
2567     l_include_gl_oblig_balance  := l_ws_rec.include_gl_oblig_balance;
2568     l_include_gl_other_balance  := l_ws_rec.include_gl_other_balance;
2569   END LOOP;
2570 
2571   -- Substitute parameter input values that were passed in
2572 
2573   if (l_incl_stat_bal is null or l_incl_stat_bal = 'N') then
2574     l_incl_stat_bal := FND_API.G_FALSE;
2575   else
2576     l_incl_stat_bal := FND_API.G_TRUE;
2577   end if;
2578 
2579   if (l_incl_trans_bal is null or l_incl_trans_bal = 'N') then
2580     l_incl_trans_bal := FND_API.G_FALSE;
2581   else
2582     l_incl_trans_bal := FND_API.G_TRUE;
2583   end if;
2584 
2585   if (l_incl_adj_period is null or l_incl_adj_period = 'N') then
2586     l_incl_adj_period := FND_API.G_FALSE;
2587   else
2588     l_incl_adj_period := FND_API.G_TRUE;
2589   end if;
2590 
2591   if (l_include_gl_commit_balance is null or l_include_gl_commit_balance = 'N')
2592   then
2593     l_include_gl_commit_balance := FND_API.G_FALSE;
2594   else
2595     l_include_gl_commit_balance := FND_API.G_TRUE;
2596   end if;
2597 
2598   if (l_include_gl_oblig_balance is null or l_include_gl_oblig_balance = 'N')
2599   then
2600     l_include_gl_oblig_balance := FND_API.G_FALSE;
2601   else
2602     l_include_gl_oblig_balance := FND_API.G_TRUE;
2603   end if;
2604 
2605   if (l_include_gl_other_balance is null or l_include_gl_other_balance = 'N')
2606   then
2607     l_include_gl_other_balance := FND_API.G_FALSE;
2608   else
2609     l_include_gl_other_balance := FND_API.G_TRUE;
2610   end if;
2611 
2612   -- Get service package info.
2613   FOR l_sp_rec IN
2614   (
2615     SELECT a.service_package_id
2616     FROM   psb_service_packages a,
2617            psb_worksheets_v     b
2618     WHERE  a.base_service_package = 'Y'
2619     AND    ( a.global_worksheet_id = b.worksheet_id
2620              OR
2621              a.global_worksheet_id = b.global_worksheet_id
2622            )
2623     AND    b.worksheet_id = p_worksheet_id
2624   )
2625   LOOP
2626     l_service_package_id := l_sp_rec.service_package_id;
2627   END LOOP;
2628 
2629   -- Bug#3545533: Added ps_account_position_set_id in the statement.
2630   FOR l_bg_rec IN
2631   (
2632     SELECT NVL(set_of_books_id, root_set_of_books_id)           set_of_books_id,
2633            NVL(currency_code, root_currency_code)               currency_code,
2634            NVL(chart_of_accounts_id, root_chart_of_accounts_id) flex_code,
2635            ps_account_position_set_id
2636     FROM   psb_budget_groups_v
2637     WHERE  budget_group_id = l_budget_group_id
2638   )
2639   LOOP
2640     l_set_of_books_id := l_bg_rec.set_of_books_id;
2641     l_func_currency   := l_bg_rec.currency_code;
2642     l_flex_code       := l_bg_rec.flex_code;
2643     l_ps_set_id       := l_bg_rec.ps_account_position_set_id;
2644   END LOOP;
2645 
2646   -- Get budgetary control info.
2647   FOR l_sob_rec IN
2648   (
2649     SELECT name, enable_budgetary_control_flag
2650     FROM   gl_sets_of_books
2651     WHERE  set_of_books_id = l_set_of_books_id
2652   )
2653   LOOP
2654     l_set_of_books_name := l_sob_rec.name;
2655     l_budgetary_control := l_sob_rec.enable_budgetary_control_flag;
2656   END LOOP;
2657 
2658   if (l_budgetary_control is null or l_budgetary_control = 'N') then
2659     l_budgetary_control := FND_API.G_FALSE;
2660   else
2661     l_budgetary_control := FND_API.G_TRUE;
2662   end if;
2663 
2664   -- If a budget set is not assigned to worksheet, a funding budget must be
2665   -- entered when Budgetary Control is not enabled for the Set of Books.
2666   if l_gl_budget_set_id is null then
2667 
2668     if (not FND_API.to_Boolean(l_budgetary_control)) then
2669       message_token('SOB', l_set_of_books_name);
2670       message_token('WORKSHEET_ID', p_worksheet_id);
2671       add_message('PSB', 'PSB_BUDGET_VERSION');
2672       raise FND_API.G_EXC_ERROR;
2673     end if;
2674 
2675   end if;
2676 
2677   /* start bug 4256345 */
2678   IF l_gl_budget_set_id IS NOT NULL THEN
2679     check_ccid_bal (x_return_status  		=> l_return_status,
2680                     p_gl_budget_set_id      => l_gl_budget_set_id);
2681 
2682     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2683       RAISE FND_API.G_EXC_ERROR;
2684     END IF;
2685   END IF;
2686   /* end bug 4256345 */
2687 
2688 
2689   --
2690   -- GL Mapping Criteria Profile Option : This specifies how to map the PSB
2691   -- Budget Periods to the GL Budget Periods.  Value 'S' indicates that Start
2692   -- Date for a GL Period should be within a PSB Budget Period specified by
2693   -- Start and End Dates; 'E' indicates that the End Date for a GL Period
2694   -- should be within a PSB Budget Period specified by Start and End Dates.
2695   --
2696   FND_PROFILE.GET( name => 'PSB_GL_MAP_CRITERIA',
2697                    val => g_map_criteria ) ;
2698   --
2699   if g_map_criteria is null then
2700     g_map_criteria := 'S';
2701   end if;
2702 
2703   --
2704   -- Create Zero Balances Profile: this specifies whether CCIDs with Zero
2705   -- YTD Balances must be created
2706   --
2707   FND_PROFILE.GET ( name => 'PSB_CREATE_ZERO_BALANCE_ACCT',
2708                     val => g_create_zero_bal ) ;
2709   --
2710   if g_create_zero_bal is null then
2711     -- Bug 3548345: Change efault behavior to not creating zero balance
2712     g_create_zero_bal := 'N';
2713   end if;
2714 
2715   -- Create Dynamic SQL Statements for extracting GL Balances
2716 
2717   PSB_WS_ACCT1.DSQL_Budget_Balance
2718      (p_return_status => l_return_status,
2719       p_sql_statement => g_sql_budget_balance,
2720       p_set_of_books_id => l_set_of_books_id,
2721       p_budgetary_control => l_budgetary_control,
2722       p_budget_version_id => l_budget_version_id,
2723       p_gl_budget_set_id => l_gl_budget_set_id,
2724       p_incl_adj_period => l_incl_adj_period,
2725       p_map_criteria => g_map_criteria);
2726 
2727   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
2728     raise FND_API.G_EXC_ERROR;
2729   end if;
2730 
2731   PSB_WS_ACCT1.DSQL_Actual_Balance
2732      (p_return_status => l_return_status,
2733       p_sql_statement => g_sql_actual_balance,
2734       p_set_of_books_id => l_set_of_books_id,
2735       p_incl_adj_period => l_incl_adj_period,
2736       p_map_criteria => g_map_criteria);
2737 
2738   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
2739     raise FND_API.G_EXC_ERROR;
2740   end if;
2741 
2742   -- Bug#5030613 Start
2743   g_include_gl_commit_balance := l_include_gl_commit_balance ;
2744   g_include_gl_oblig_balance  := l_include_gl_oblig_balance ;
2745   g_include_gl_other_balance  := l_include_gl_other_balance ;
2746   -- Bug#5030613 End
2747 
2748   PSB_WS_ACCT1.DSQL_Encum_Balance
2749      (p_return_status => l_return_status,
2750       p_sql_statement => g_sql_encum_balance,
2751       p_set_of_books_id => l_set_of_books_id,
2752       p_incl_adj_period => l_incl_adj_period,
2753       p_map_criteria => g_map_criteria,
2754       p_include_gl_commit_balance => l_include_gl_commit_balance,
2755       p_include_gl_oblig_balance => l_include_gl_oblig_balance,
2756       p_include_gl_other_balance => l_include_gl_other_balance);
2757 
2758   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
2759     raise FND_API.G_EXC_ERROR;
2760   end if;
2761 
2762   -- Bug#5030613 Start
2763   -- Extract encumbrance balances, Include Other Encum Balances.
2764   IF FND_API.to_Boolean(g_include_gl_other_balance)
2765   THEN
2766     IF not FND_API.to_Boolean(g_include_gl_commit_balance)
2767     THEN
2768       IF not FND_API.to_Boolean(g_include_gl_oblig_balance)
2769       THEN
2770         g_sql_encum_balance := g_sql_encum_balance ||
2771                                'and gb.encumbrance_type_id not in ( :b_oblig_enc_type_id, :b_commit_enc_type_id ) ';
2772       ELSE
2773         g_sql_encum_balance := g_sql_encum_balance ||
2774                              'and gb.encumbrance_type_id not in ( :b_commit_enc_type_id ) ';
2775       END IF;
2776     ELSE
2777       IF NOT FND_API.to_Boolean(g_include_gl_oblig_balance)
2778       THEN
2779         g_sql_encum_balance := g_sql_encum_balance ||
2780                                'and gb.encumbrance_type_id not in ( :b_oblig_enc_type_id ) ';
2781       END IF;
2782     END IF;
2783   ELSE
2784     IF NOT FND_API.to_Boolean(g_include_gl_commit_balance)
2785     THEN
2786       IF FND_API.to_Boolean(g_include_gl_oblig_balance)
2787       THEN
2788         g_sql_encum_balance := g_sql_encum_balance ||
2789                                'and gb.encumbrance_type_id in ( :b_oblig_enc_type_id ) ';
2790       END IF;
2791     ELSE
2792       IF not FND_API.to_Boolean(g_include_gl_oblig_balance)
2793       THEN
2794         g_sql_encum_balance := g_sql_encum_balance ||
2795                                'and gb.encumbrance_type_id in ( :b_commit_enc_type_id ) ';
2796       ELSE
2797         g_sql_encum_balance := g_sql_encum_balance ||
2798                                'and gb.encumbrance_type_id in ( :b_commit_enc_type_id, :b_oblig_enc_type_id ) ';
2799       END IF;
2800     END IF;
2801   END IF;
2802   -- Bug#5030613 End
2803 
2804   g_cur_budget_balance := dbms_sql.open_cursor;
2805   dbms_sql.parse(g_cur_budget_balance, g_sql_budget_balance, dbms_sql.v7);
2806 
2807   g_cur_actual_balance := dbms_sql.open_cursor;
2808   dbms_sql.parse(g_cur_actual_balance, g_sql_actual_balance, dbms_sql.v7);
2809 
2810   g_cur_encum_balance := dbms_sql.open_cursor;
2811   dbms_sql.parse(g_cur_encum_balance, g_sql_encum_balance, dbms_sql.v7);
2812 
2813   if l_budget_calendar_id <> NVL( PSB_WS_ACCT1.g_budget_calendar_id,
2814                                   FND_API.G_MISS_NUM)
2815   then
2816 
2817     PSB_WS_ACCT1.Cache_Budget_Calendar
2818     ( p_return_status      => l_return_status,
2819       p_budget_calendar_id => l_budget_calendar_id
2820     ) ;
2821     --
2822     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
2823       raise FND_API.G_EXC_ERROR;
2824     end if;
2825 
2826   end if;
2827 
2828   -- Bug#3514350: Check if CY estimates need to be processed or not.
2829   IF l_gl_cutoff_period = PSB_WS_ACCT1.g_enddate_cy THEN
2830     g_process_cy_estimates := FALSE;
2831   ELSE
2832     g_process_cy_estimates := TRUE;
2833   END IF;
2834 
2835   -- Extract only Actuals and Budget Balances from GL
2836 
2837   g_balance_type(1) := 'A';
2838   g_balance_type(2) := 'B';
2839 
2840   if ( FND_API.to_Boolean(l_include_gl_commit_balance) or
2841        FND_API.to_Boolean(l_include_gl_oblig_balance)  or
2842        FND_API.to_Boolean(l_include_gl_other_balance)  )
2843   then
2844     g_balance_type(3) := 'E';
2845   end if;
2846 
2847   -- Process budget groups associated with the worksheet.
2848   for c_BudGrp_Rec in c_BudGrp (l_budget_group_id) loop
2849 
2850     -- Process account sets associated with budget group being processed.
2851     for c_AccSet_Rec in c_AccSet (c_BudGrp_Rec.budget_group_id) loop
2852 
2853       -- Process account codes associated with account set being processed.
2854       open c_ccids(c_AccSet_Rec.account_position_set_id);
2855 
2856       loop
2857 
2858 	fetch c_ccids BULK COLLECT INTO l_ccids.ccid, l_ccids.start_date,
2859                                         l_ccids.end_date
2860                                    LIMIT PSB_WS_ACCT1.g_limit_bulk_numrows;
2861 
2862         -- Process given set of accounts for the curreng fetch.
2863 	for l_ccid_index in 1..l_ccids.ccid.count loop
2864 
2865 	  l_ccid_start_period := greatest(nvl(l_ccids.start_date(l_ccid_index),
2866                                           c_AccSet_Rec.effective_start_date),
2867                                           c_AccSet_Rec.effective_start_date);
2868 	  l_ccid_end_period   := least(nvl(l_ccids.end_date(l_ccid_index),
2869                                        c_AccSet_Rec.effective_end_date),
2870                                        c_AccSet_Rec.effective_end_date);
2871 
2872 	  g_actuals_func_total := 0;
2873 	  g_actuals_stat_total := 0;
2874 
2875 	  GL_CODE_COMBINATIONS_PKG.Select_Columns
2876 	  ( X_code_combination_id => l_ccids.ccid(l_ccid_index) ,
2877 	    X_account_type        => l_account_type             ,
2878 	    X_template_id         => l_template_id              ) ;
2879 
2880 
2881           -- Process all budget years for the CCID.
2882 	  for l_year_index in 1..PSB_WS_ACCT1.g_num_budget_years loop
2883 
2884 	    l_year_start_date :=
2885                          PSB_WS_ACCT1.g_budget_years(l_year_index).start_date;
2886 	    l_year_end_date :=
2887                            PSB_WS_ACCT1.g_budget_years(l_year_index).end_date;
2888 
2889             -- Check if CCID is valid for the year being processed.
2890 	    if (((l_ccid_start_period <= l_year_end_date) and (l_ccid_end_period is null))
2891 	     or ((l_ccid_start_period between l_year_start_date and l_year_end_date)
2892 	      or (l_ccid_end_period between l_year_start_date and l_year_end_date)
2893 	      or ((l_ccid_start_period < l_year_start_date)
2894 	      and (l_ccid_end_period > l_year_end_date))))
2895             then
2896 
2897               -- Process CY balances.
2898 	      if PSB_WS_ACCT1.g_budget_years(l_year_index).year_type = 'CY' then
2899 
2900                 for l_init_index in 1..g_gl_actual_periods.Count loop
2901                   g_gl_actual_periods(l_init_index).budget_period_id := null;
2902                   g_gl_actual_periods(l_init_index).long_sequence_no := null;
2903                   g_gl_actual_periods(l_init_index).start_date := null;
2904                   g_gl_actual_periods(l_init_index).end_date := null;
2905                   g_gl_actual_periods(l_init_index).budget_year_id := null;
2906                 end loop;
2907 
2908                 for l_init_index in 1..g_gl_budget_periods.Count loop
2909                   g_gl_budget_periods(l_init_index).budget_period_id := null;
2910                   g_gl_budget_periods(l_init_index).long_sequence_no := null;
2911                   g_gl_budget_periods(l_init_index).start_date := null;
2912                   g_gl_budget_periods(l_init_index).end_date := null;
2913                   g_gl_budget_periods(l_init_index).budget_year_id := null;
2914                 end loop;
2915 
2916                 -- Bug#2529886: When we update GL balances, we need to update
2917                 -- CY Estimate balances as well.
2918                 g_alloc_periods.DELETE;
2919 
2920 		l_init_index := 1;
2921 		g_num_actual_periods := 0;
2922 		g_num_budget_periods := 0;
2923 
2924                 -- Process budget periods for the year being processed.
2925 		for l_period_index in 1..PSB_WS_ACCT1.g_num_budget_periods loop
2926 
2927 		  if (PSB_WS_ACCT1.g_budget_periods(l_period_index).budget_year_id =
2928 		      PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id)                   then
2929 
2930 		    -- Extract Budget Balances for all Periods in Current Year
2931 
2932 		    g_gl_budget_periods(l_init_index).budget_period_id :=
2933 						      PSB_WS_ACCT1.g_budget_periods(l_period_index).budget_period_id;
2934 		    g_gl_budget_periods(l_init_index).long_sequence_no :=
2935 						      PSB_WS_ACCT1.g_budget_periods(l_period_index).long_sequence_no;
2936 		    g_gl_budget_periods(l_init_index).start_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).start_date;
2937 		    g_gl_budget_periods(l_init_index).end_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).end_date;
2938 		    g_gl_budget_periods(l_init_index).budget_year_id := PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id;
2939 
2940 		    g_num_budget_periods := g_num_budget_periods + 1;
2941 
2942 		    -- Extract Actual Balances for all Periods in Current Year
2943                     -- upto the GL Cutoff Date
2944 
2945 		    if ((l_gl_cutoff_period is null) or
2946 			(l_gl_cutoff_period > PSB_WS_ACCT1.g_budget_periods(l_period_index).start_date))
2947                     then
2948 
2949 		      g_gl_actual_periods(l_init_index).budget_period_id :=
2950 							PSB_WS_ACCT1.g_budget_periods(l_period_index).budget_period_id;
2951 		      g_gl_actual_periods(l_init_index).long_sequence_no :=
2952 							PSB_WS_ACCT1.g_budget_periods(l_period_index).long_sequence_no;
2953 		      g_gl_actual_periods(l_init_index).start_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).start_date;
2954 		      g_gl_actual_periods(l_init_index).end_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).end_date;
2955 		      g_gl_actual_periods(l_init_index).budget_year_id := PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id;
2956 
2957 		      g_num_actual_periods := g_num_actual_periods + 1;
2958 
2959                     else
2960 
2961                       -- Bug#2529886: Get information about CY period beyond
2962                       -- the GL Cutoff Date.
2963 		      g_alloc_periods(l_init_index).budget_period_id := PSB_WS_ACCT1.g_budget_periods(l_period_index).budget_period_id;
2964 		      g_alloc_periods(l_init_index).long_sequence_no := PSB_WS_ACCT1.g_budget_periods(l_period_index).long_sequence_no;
2965 		      g_alloc_periods(l_init_index).start_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).start_date;
2966 		      g_alloc_periods(l_init_index).end_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).end_date;
2967 		      g_alloc_periods(l_init_index).budget_year_id := PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id;
2968                       -- Bug#2529886: End
2969 
2970 		    end if;
2971 
2972 		    l_init_index := l_init_index + 1;
2973 
2974 		  end if;
2975 
2976 		end loop;
2977                 -- End processing budget periods for the year being processed.
2978 
2979 		-- Get Balances from GL
2980 		Get_Balances
2981 		( p_return_status => l_return_status,
2982 		  p_ccid => l_ccids.ccid(l_ccid_index),
2983 		  p_account_type => l_account_type,
2984 		  p_budget_year_id =>
2985                       PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id,
2986 		  p_year_name =>
2987                       PSB_WS_ACCT1.g_budget_years(l_year_index).year_name,
2988 		  p_year_start_date =>
2989                       PSB_WS_ACCT1.g_budget_years(l_year_index).start_date,
2990 		  p_year_end_date =>
2991                       PSB_WS_ACCT1.g_budget_years(l_year_index).end_date,
2992 		  p_budget_year_type =>
2993                       PSB_WS_ACCT1.g_budget_years(l_year_index).year_type,
2994 		  p_incl_stat_bal => l_incl_stat_bal,
2995 		  p_incl_trans_bal => l_incl_trans_bal,
2996 		  p_incl_adj_period => l_incl_adj_period,
2997 		  p_set_of_books_id => l_set_of_books_id,
2998 		  p_budget_group_id => c_BudGrp_Rec.budget_group_id,
2999 		  p_stage_set_id => l_stage_set_id,
3000 		  p_func_currency => l_func_currency,
3001 		  p_budgetary_control => l_budgetary_control,
3002 		  p_budget_version_id => l_budget_version_id,
3003 		  p_flex_mapping_set_id => l_flex_mapping_set_id,
3004 		  p_gl_budget_set_id => l_gl_budget_set_id,
3005 		  p_flex_code => l_flex_code,
3006 		  p_worksheet_id => p_worksheet_id,
3007 		  p_service_package_id => l_service_package_id,
3008 		  p_sequence_number => l_current_stage_seq,
3009 		  p_rounding_factor => l_rounding_factor,
3010                   /* bug no 4725091 */
3011                   p_incl_gl_fwd_balance => fnd_api.g_false,
3012                   p_create_wks_flag     => fnd_api.g_false
3013                 ) ;
3014                 --
3015 		if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3016 		  raise FND_API.G_EXC_ERROR;
3017 		end if;
3018 
3019                 -- Bug#3545533: Update CY estimates only for non-pos accounts.
3020                 l_process_cy_estimates := 'Y' ;
3021 
3022                 IF l_budget_by_position = 'Y' THEN
3023 
3024                   -- Check if current ccid is for position or not. This will
3025                   -- reset l_process_cy_estimates if for position.
3026                   OPEN  l_check_acct_type ( l_ccids.ccid(l_ccid_index) ) ;
3027                   FETCH l_check_acct_type INTO l_process_cy_estimates ;
3028                   CLOSE l_check_acct_type ;
3029 
3030                 END IF ;
3031 
3032                 IF l_process_cy_estimates = 'Y' THEN
3033                   --
3034                   -- Bug#2529886: When updating GL balances, we need to update
3035                   -- CY Estimate balances as well.
3036                   --
3037                   Update_CY_Estimates
3038                   ( p_return_status      => l_return_status,
3039                     p_worksheet_id       => p_worksheet_id,
3040                     p_service_package_id => l_service_package_id,
3041                     p_rounding_factor    => l_rounding_factor,
3042                     p_start_stage_seq    => l_current_stage_seq,
3043                     p_budget_group_id    => c_BudGrp_Rec.budget_group_id,
3044                     p_stage_set_id       => l_stage_set_id,
3045                     p_budget_year_id     =>
3046                       PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id,
3047                     p_ccid               => l_ccids.ccid(l_ccid_index),
3048                     p_currency_code      => l_func_currency
3049                   ) ;
3050                   --
3051                   IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3052                     RAISE FND_API.G_EXC_ERROR;
3053                   END IF ;
3054                   -- Bug#2529886: End
3055 
3056                 END IF ;
3057 
3058 	      end if;
3059 
3060 	    end if;
3061             -- Check if CCID is valid for the year being processed.
3062 
3063 	  end loop;
3064           -- End processing all budget years for the CCID.
3065 
3066 	  l_num_accounts := l_num_accounts + 1;
3067 
3068 	  if l_num_accounts > PSB_WS_ACCT1.g_checkpoint_save then
3069 	    commit work;
3070 	    l_num_accounts := 0;
3071 	  end if;
3072 
3073 	end loop;
3074         -- End processing given set of accounts for the curreng fetch.
3075 
3076 	exit when c_ccids%NOTFOUND;
3077 
3078       end loop;
3079       close c_ccids;
3080       -- End processing accounts associated with account set being processed.
3081 
3082     end loop;
3083     -- End processing account sets associated with budget group being processed.
3084 
3085     commit work;
3086 
3087   end loop;
3088   -- End processing budget groups associated with the worksheet.
3089 
3090   dbms_sql.close_cursor(g_cur_budget_balance);
3091   dbms_sql.close_cursor(g_cur_actual_balance);
3092   dbms_sql.close_cursor(g_cur_encum_balance);
3093 
3094   -- Initialize API return status to success
3095   p_return_status := FND_API.G_RET_STS_SUCCESS;
3096 
3097 EXCEPTION
3098 
3099    when FND_API.G_EXC_ERROR then
3100      p_return_status := FND_API.G_RET_STS_ERROR;
3101 
3102      if dbms_sql.is_open(g_cur_budget_balance) then
3103        dbms_sql.close_cursor(g_cur_budget_balance);
3104      end if;
3105 
3106      if dbms_sql.is_open(g_cur_actual_balance) then
3107        dbms_sql.close_cursor(g_cur_actual_balance);
3108      end if;
3109 
3110      if dbms_sql.is_open(g_cur_encum_balance) then
3111        dbms_sql.close_cursor(g_cur_encum_balance);
3112      end if;
3113 
3114      if c_ccids%ISOPEN then
3115        close c_ccids;
3116      end if;
3117 
3118    when FND_API.G_EXC_UNEXPECTED_ERROR then
3119      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3120 
3121      if dbms_sql.is_open(g_cur_budget_balance) then
3122        dbms_sql.close_cursor(g_cur_budget_balance);
3123      end if;
3124 
3125      if dbms_sql.is_open(g_cur_actual_balance) then
3126        dbms_sql.close_cursor(g_cur_actual_balance);
3127      end if;
3128 
3129      if dbms_sql.is_open(g_cur_encum_balance) then
3130        dbms_sql.close_cursor(g_cur_encum_balance);
3131      end if;
3132 
3133      if c_ccids%ISOPEN then
3134        close c_ccids;
3135      end if;
3136 
3137    when OTHERS then
3138      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3139 
3140      if dbms_sql.is_open(g_cur_budget_balance) then
3141        dbms_sql.close_cursor(g_cur_budget_balance);
3142      end if;
3143 
3144      if dbms_sql.is_open(g_cur_actual_balance) then
3145        dbms_sql.close_cursor(g_cur_actual_balance);
3146      end if;
3147 
3148      if dbms_sql.is_open(g_cur_encum_balance) then
3149        dbms_sql.close_cursor(g_cur_encum_balance);
3150      end if;
3151 
3152      if c_ccids%ISOPEN then
3153        close c_ccids;
3154      end if;
3155 
3156      if FND_MSG_PUB.Check_Msg_Level
3157        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
3158      then
3159        FND_MSG_PUB.Add_Exc_Msg
3160 	  (p_pkg_name => G_PKG_NAME,
3161 	   p_procedure_name => l_api_name);
3162      end if;
3163 
3164 END Update_GL_Balances;
3165 
3166 /* ----------------------------------------------------------------------- */
3167 
3168 -- Compute Line Total for Parameter
3169 
3170 FUNCTION Compute_Line_Total
3171 ( p_worksheet_id         IN  NUMBER,
3172   p_flex_mapping_set_id  IN  NUMBER,
3173   p_budget_calendar_id   IN  NUMBER,
3174   p_ccid                 IN  NUMBER,
3175   p_budget_year_type_id  IN  NUMBER,
3176   p_balance_type         IN  VARCHAR2,
3177   p_currency_code        IN  VARCHAR2,
3178   p_service_package_id   IN  NUMBER,
3179   /* start bug no 4256345 */
3180   p_stage_set_id        IN   NUMBER,
3181   p_current_stage_seq   IN   NUMBER
3182   /* end bug no 4256345 */
3183 ) RETURN NUMBER IS
3184 
3185   l_mapped_ccid          NUMBER;
3186   l_ytd_amount           NUMBER := FND_API.G_MISS_NUM;
3187 
3188   /* start bug  4256345 */
3189   l_budget_year_id NUMBER;
3190  /* end bug  4256345 */
3191 
3192 
3193   -- Compute Line Total for Formula
3194 
3195 /* For Bug No. 2214715 : Start  */
3196 /*  Existing Cursor definition is commented and modified one is added as follows :
3197   cursor c_Type12 is
3198     select nvl(a.ytd_amount, 0) YTD_Amount
3199       from PSB_WS_ACCOUNT_LINES a
3200      where a.code_combination_id = l_mapped_ccid
3201        and a.currency_code = p_currency_code
3202        and a.balance_type = p_balance_type
3203        and a.service_package_id = p_service_package_id
3204        and a.end_stage_seq is null
3205        and a.template_id is null
3206        and exists
3207 	  (select 1
3208 	     from PSB_WORKSHEETS b,
3209 		  PSB_BUDGET_PERIODS c
3210 	    where b.worksheet_id = p_worksheet_id
3211 	      and a.stage_set_id = b.stage_set_id
3212 	      and b.current_stage_seq between a.start_stage_seq and a.current_stage_seq
3213 	      and c.budget_calendar_id = p_budget_calendar_id
3214 	      and a.budget_year_id = c.budget_period_id
3215 	      and c.budget_period_type = 'Y'
3216 	      and c.budget_year_type_id = p_budget_year_type_id)
3217        and exists
3218 	  (select 1
3219 	     from PSB_WS_LINES
3220 	    where account_line_id = a.account_line_id
3221 	      and worksheet_id = p_worksheet_id);
3222 */
3223 
3224     -- commented as a part of bug fix 4256345
3225     /* cursor c_Type12 is
3226     select sum(nvl(a.ytd_amount, 0)) YTD_Amount
3227       from PSB_WORKSHEETS b,
3228 	   PSB_WS_LINES d,
3229 	   PSB_WS_ACCOUNT_LINES a,
3230 	   PSB_BUDGET_PERIODS c
3231      where b.worksheet_id = p_worksheet_id
3232        and d.worksheet_id = b.worksheet_id
3233        and d.account_line_id = a.account_line_id
3234        and a.code_combination_id = l_mapped_ccid
3235        and a.balance_type = p_balance_type
3236        and a.currency_code = p_currency_code
3237        and a.service_package_id = p_service_package_id
3238        and a.end_stage_seq is null
3239        and a.template_id is null
3240        and a.stage_set_id = b.stage_set_id
3241        and b.current_stage_seq between a.start_stage_seq and a.current_stage_seq
3242        and c.budget_calendar_id = p_budget_calendar_id
3243        and a.budget_year_id = c.budget_period_id
3244        and c.budget_period_type = 'Y'
3245        and c.budget_year_type_id = p_budget_year_type_id;              */
3246 
3247 /* For Bug No. 2214715 : End  */
3248 
3249 
3250 BEGIN
3251 
3252   l_mapped_ccid := PSB_WS_ACCT1.Map_Account
3253 		      (p_flex_mapping_set_id => p_flex_mapping_set_id,
3254 		       p_ccid => p_ccid,
3255 		       p_budget_year_type_id => p_budget_year_type_id);
3256 
3257   -- commented as a part of bug fix 4256345
3258   /* for c_Type12_Rec in c_Type12 loop
3259     l_ytd_amount := c_Type12_Rec.YTD_Amount;
3260   end loop; */
3261 
3262 /* start bug 4256345 */
3263 
3264   FOR l_budget_period_rec IN (select budget_period_id
3265        						  from psb_budget_periods
3266        						  where budget_calendar_id = p_budget_calendar_id
3267                               and budget_period_type = 'Y'
3268                               and parent_budget_period_id is null
3269                               and budget_year_type_id = p_budget_year_type_id)
3270   LOOP
3271     l_budget_year_id := l_budget_period_rec.budget_period_id;
3272   END LOOP;
3273 
3274 
3275   SELECT
3276     SUM(NVL(A.YTD_AMOUNT, 0)) YTD_AMOUNT
3277   INTO
3278     l_ytd_amount
3279   FROM
3280     PSB_WS_LINES WSL
3281   , PSB_WS_ACCOUNT_LINES A
3282   WHERE A.CODE_COMBINATION_ID = l_mapped_ccid
3283   AND   A.BUDGET_YEAR_ID = l_budget_year_id
3284   AND   A.SERVICE_PACKAGE_ID = p_service_package_id
3285   AND   A.BALANCE_TYPE = p_balance_type
3286   AND   A.CURRENCY_CODE = p_currency_code
3287   AND   A.END_STAGE_SEQ IS NULL
3288   AND   A.TEMPLATE_ID IS NULL
3289   AND   A.STAGE_SET_ID = p_stage_set_id
3290   AND   p_current_stage_seq
3291   BETWEEN A.START_STAGE_SEQ AND A.CURRENT_STAGE_SEQ
3292   AND   WSL.WORKSHEET_ID = p_worksheet_id
3293   AND   WSL.ACCOUNT_LINE_ID = A.ACCOUNT_LINE_ID;
3294 
3295 /* end bug 4256345 */
3296 
3297   RETURN l_ytd_amount;
3298 
3299 END Compute_Line_Total;
3300 
3301 /* ----------------------------------------------------------------------- */
3302 
3303 -- Parse Parameter Formulae and compute a YTD total
3304 
3305 PROCEDURE Process_Parameter
3306 ( p_return_status       OUT  NOCOPY  VARCHAR2,
3307   p_worksheet_id        IN   NUMBER,
3308   p_service_package_id  IN   NUMBER,
3309   p_parameter_id        IN   NUMBER,
3310   p_parameter_name      IN   VARCHAR2,
3311   p_compound_annually   IN   VARCHAR2,
3312   p_compound_factor     IN   NUMBER,
3313   p_budget_calendar_id  IN   NUMBER,
3314   p_flex_code           IN   NUMBER,
3315   p_flex_mapping_set_id IN   NUMBER,
3316   p_ccid                IN   NUMBER,
3317   p_ccid_start_period   IN   DATE,
3318   p_ccid_end_period     IN   DATE,
3319   p_budget_group_id     IN   NUMBER,
3320   p_num_proposed_years  IN   NUMBER,
3321   p_defer_ccids         IN   VARCHAR2,
3322   p_deferred            OUT  NOCOPY  BOOLEAN,
3323   /* bug no 4256345 */
3324   p_stage_set_id        IN   NUMBER,
3325   p_current_stage_seq   IN   NUMBER
3326   /* bug no 4256345 */
3327 ) IS
3328 
3329   -- Formulae is sorted by step number and processed
3330 
3331   cursor c_Formula is
3332     select step_number,
3333 	   prefix_operator,
3334 	   budget_year_type_id,
3335 	   balance_type,
3336 	   segment1, segment2, segment3,
3337 	   segment4, segment5, segment6,
3338 	   segment7, segment8, segment9,
3339 	   segment10, segment11, segment12,
3340 	   segment13, segment14, segment15,
3341 	   segment16, segment17, segment18,
3342 	   segment19, segment20, segment21,
3343 	   segment22, segment23, segment24,
3344 	   segment25, segment26, segment27,
3345 	   segment28, segment29, segment30,
3346 	   currency_code,
3347 	   nvl(amount, 0) amount,
3348 	   postfix_operator
3349       from PSB_PARAMETER_FORMULAS
3350      where parameter_id = p_parameter_id
3351      order by step_number;
3352 
3353   l_first_line          BOOLEAN := TRUE;
3354   l_first_time          BOOLEAN := TRUE;
3355 
3356 /* Bug No 2719865 Start */
3357   l_ccid_defined        BOOLEAN := TRUE;
3358 /* Bug No 2719865 End */
3359 
3360   l_num_lines           NUMBER := 0;
3361   l_compound_total      NUMBER;
3362 
3363   l_type1               BOOLEAN;
3364   l_type2               BOOLEAN;
3365   l_type3               BOOLEAN;
3366   l_type4               BOOLEAN;
3367 
3368   l_line_total          NUMBER;
3369   l_ytd_amount          NUMBER;
3370 
3371   l_index               PLS_INTEGER;
3372   l_init_index          PLS_INTEGER;
3373 
3374   l_ccid                NUMBER;
3375   l_seg_val             FND_FLEX_EXT.SegmentArray;
3376   l_ccid_val            FND_FLEX_EXT.SegmentArray;
3377 
3378   l_flex_delimiter      VARCHAR2(1);
3379   l_concat_segments     VARCHAR2(2000);
3380 
3381   l_deferred            BOOLEAN := FALSE;
3382   l_ccid_found          BOOLEAN;
3383   l_return_status       VARCHAR2(1);
3384 
3385 BEGIN
3386 
3387   -- Cache the number of Segments and the application column names for the
3388   -- Segments for the Chart of Accounts
3389 
3390   if p_flex_code <> nvl(PSB_WS_ACCT1.g_flex_code, 0) then
3391   begin
3392 
3393     PSB_WS_ACCT1.Flex_Info
3394        (p_flex_code => p_flex_code,
3395 	p_return_status => l_return_status);
3396 
3397     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3398       raise FND_API.G_EXC_ERROR;
3399     end if;
3400 
3401   end;
3402   end if;
3403 
3404   -- Get the individual Segment values for the Code Combination
3405 
3406   if not FND_FLEX_EXT.Get_Segments
3407     (application_short_name => 'SQLGL',
3408      key_flex_code => 'GL#',
3409      structure_number => p_flex_code,
3410      combination_id => p_ccid,
3411      n_segments => PSB_WS_ACCT1.g_num_segs,
3412      segments => l_ccid_val) then
3413 
3414     FND_MSG_PUB.Add;
3415     raise FND_API.G_EXC_ERROR;
3416   end if;
3417 
3418   for c_Formula_Rec in c_Formula loop
3419 
3420     -- Each Formula Line is of the following types :
3421     --
3422     -- Type1 : Depends on Account Set Assignments
3423     --        (Step, Prefix Operator, Postfix Operator, Period, Balance Type, Currency, Amount have values; Account is blank)
3424     --
3425     -- Type2 : Depends on Account defined in Formula Line
3426     --        (Step, Prefix Operator, Period, Balance Type, Account, Currency have values;
3427     --         Amount and Postfix Operator are optional)
3428     --
3429     -- Type3 : Flat Amount assignment
3430     --        (Step, Prefix Operator, Amount have values; Period, Balance Type, Account, Currency, Postfix Operator are blank)
3431     --
3432 
3433     -- Type4 : Depends on Account Set Assignments (valid only for budget revisions)
3434     --        (Step, Prefix Operator, Postfix Operator, Balance Type, Currency, Amount have values; Account, Period are blank)
3435     --
3436     l_type1 := FALSE;
3437     l_type2 := FALSE;
3438     l_type3 := FALSE;
3439     l_type4 := FALSE;
3440     l_line_total := 0;
3441 
3442     l_ccid_found := FALSE;
3443 
3444     for l_init_index in 1..PSB_WS_ACCT1.g_num_segs loop
3445       l_seg_val(l_init_index) := null;
3446     end loop;
3447 
3448     l_num_lines := l_num_lines + 1;
3449 
3450     -- The prefix operator for the 1st Formula line must be '='; for the other Formula lines, the
3451     -- prefix operator can be '+', '-', '*', '/'
3452 
3453     if l_first_line then
3454     begin
3455 
3456       l_first_line := FALSE;
3457 
3458       if c_Formula_Rec.prefix_operator <> '=' then
3459 	message_token('PARAMETER', p_parameter_name);
3460 	message_token('STEPID', c_Formula_Rec.step_number);
3461 	message_token('OPERATOR', '[=]');
3462 	add_message('PSB', 'PSB_INVALID_PARAM_OPR');
3463 	raise FND_API.G_EXC_ERROR;
3464       end if;
3465 
3466     end;
3467     else
3468     begin
3469 
3470       if c_Formula_Rec.prefix_operator not in ('+', '-', '*', '/') then
3471 	message_token('PARAMETER', p_parameter_name);
3472 	message_token('STEPID', c_Formula_Rec.step_number);
3473 	message_token('OPERATOR', '[+, -, *, /]');
3474 	add_message('PSB', 'PSB_INVALID_PARAM_OPR');
3475 	raise FND_API.G_EXC_ERROR;
3476       end if;
3477 
3478     end;
3479     end if;
3480 
3481     -- Check Formula Type :
3482 
3483     if ((c_Formula_Rec.prefix_operator is not null) and
3484 	(c_Formula_Rec.postfix_operator is not null) and
3485 	(c_Formula_Rec.budget_year_type_id is not null) and
3486 	(c_Formula_Rec.balance_type is not null) and
3487 	(c_Formula_Rec.currency_code is not null) and
3488 	(c_Formula_Rec.amount is not null) and
3489        ((c_Formula_Rec.segment1 is null) and (c_Formula_Rec.segment2 is null) and (c_Formula_Rec.segment3 is null) and
3490 	(c_Formula_Rec.segment4 is null) and (c_Formula_Rec.segment5 is null) and (c_Formula_Rec.segment6 is null) and
3491 	(c_Formula_Rec.segment7 is null) and (c_Formula_Rec.segment8 is null) and (c_Formula_Rec.segment9 is null) and
3492 	(c_Formula_Rec.segment10 is null) and (c_Formula_Rec.segment11 is null) and (c_Formula_Rec.segment12 is null) and
3493 	(c_Formula_Rec.segment13 is null) and (c_Formula_Rec.segment14 is null) and (c_Formula_Rec.segment15 is null) and
3494 	(c_Formula_Rec.segment16 is null) and (c_Formula_Rec.segment17 is null) and (c_Formula_Rec.segment18 is null) and
3495 	(c_Formula_Rec.segment19 is null) and (c_Formula_Rec.segment20 is null) and (c_Formula_Rec.segment21 is null) and
3496 	(c_Formula_Rec.segment22 is null) and (c_Formula_Rec.segment23 is null) and (c_Formula_Rec.segment24 is null) and
3497 	(c_Formula_Rec.segment25 is null) and (c_Formula_Rec.segment26 is null) and (c_Formula_Rec.segment27 is null) and
3498 	(c_Formula_Rec.segment28 is null) and (c_Formula_Rec.segment29 is null) and (c_Formula_Rec.segment30 is null))) then
3499     begin
3500       l_type1 := TRUE;
3501     end;
3502     elsif ((c_Formula_Rec.prefix_operator is not null) and
3503 	   (c_Formula_Rec.budget_year_type_id is not null) and
3504 	   (c_Formula_Rec.balance_type is not null) and
3505 	   (c_Formula_Rec.currency_code is not null) and
3506 	  ((c_Formula_Rec.segment1 is not null) or (c_Formula_Rec.segment2 is not null) or (c_Formula_Rec.segment3 is not null) or
3507 	   (c_Formula_Rec.segment4 is not null) or (c_Formula_Rec.segment5 is not null) or (c_Formula_Rec.segment6 is not null) or
3508 	   (c_Formula_Rec.segment7 is not null) or (c_Formula_Rec.segment8 is not null) or (c_Formula_Rec.segment9 is not null) or
3509 	   (c_Formula_Rec.segment10 is not null) or (c_Formula_Rec.segment11 is not null) or (c_Formula_Rec.segment12 is not null) or
3510 	   (c_Formula_Rec.segment13 is not null) or (c_Formula_Rec.segment14 is not null) or (c_Formula_Rec.segment15 is not null) or
3511 	   (c_Formula_Rec.segment16 is not null) or (c_Formula_Rec.segment17 is not null) or (c_Formula_Rec.segment18 is not null) or
3512 	   (c_Formula_Rec.segment19 is not null) or (c_Formula_Rec.segment20 is not null) or (c_Formula_Rec.segment21 is not null) or
3513 	   (c_Formula_Rec.segment22 is not null) or (c_Formula_Rec.segment23 is not null) or (c_Formula_Rec.segment24 is not null) or
3514 	   (c_Formula_Rec.segment25 is not null) or (c_Formula_Rec.segment26 is not null) or (c_Formula_Rec.segment27 is not null) or
3515 	   (c_Formula_Rec.segment28 is not null) or (c_Formula_Rec.segment29 is not null) or (c_Formula_Rec.segment30 is not null))) then
3516     begin
3517       l_type2 := TRUE;
3518     end;
3519     elsif ((c_Formula_Rec.prefix_operator is not null) and
3520 	   (c_Formula_Rec.amount is not null) and
3521 	   (c_Formula_Rec.budget_year_type_id is null) and
3522 	   (c_Formula_Rec.balance_type is null) and
3523 	   (c_Formula_Rec.currency_code is null) and
3524 	   (c_Formula_Rec.postfix_operator is null) and
3525 	  ((c_Formula_Rec.segment1 is null) and (c_Formula_Rec.segment2 is null) and (c_Formula_Rec.segment3 is null) and
3526 	   (c_Formula_Rec.segment4 is null) and (c_Formula_Rec.segment5 is null) and (c_Formula_Rec.segment6 is null) and
3527 	   (c_Formula_Rec.segment7 is null) and (c_Formula_Rec.segment8 is null) and (c_Formula_Rec.segment9 is null) and
3528 	   (c_Formula_Rec.segment10 is null) and (c_Formula_Rec.segment11 is null) and (c_Formula_Rec.segment12 is null) and
3529 	   (c_Formula_Rec.segment13 is null) and (c_Formula_Rec.segment14 is null) and (c_Formula_Rec.segment15 is null) and
3530 	   (c_Formula_Rec.segment16 is null) and (c_Formula_Rec.segment17 is null) and (c_Formula_Rec.segment18 is null) and
3531 	   (c_Formula_Rec.segment19 is null) and (c_Formula_Rec.segment20 is null) and (c_Formula_Rec.segment21 is null) and
3532 	   (c_Formula_Rec.segment22 is null) and (c_Formula_Rec.segment23 is null) and (c_Formula_Rec.segment24 is null) and
3533 	   (c_Formula_Rec.segment25 is null) and (c_Formula_Rec.segment26 is null) and (c_Formula_Rec.segment27 is null) and
3534 	   (c_Formula_Rec.segment28 is null) and (c_Formula_Rec.segment29 is null) and (c_Formula_Rec.segment30 is null))) then
3535     begin
3536       l_type3 := TRUE;
3537     end;
3538     elsif ((c_Formula_Rec.prefix_operator is not null) and
3539 	   (c_Formula_Rec.postfix_operator is not null) and
3540 	   (c_Formula_Rec.balance_type in ('O', 'C')) and
3541 	   (c_Formula_Rec.currency_code is not null) and
3542 	   (c_Formula_Rec.amount is not null) and
3543 	  ((c_Formula_Rec.budget_year_type_id is null) and
3544 	   (c_Formula_Rec.segment1 is null) and (c_Formula_Rec.segment2 is null) and (c_Formula_Rec.segment3 is null) and
3545 	   (c_Formula_Rec.segment4 is null) and (c_Formula_Rec.segment5 is null) and (c_Formula_Rec.segment6 is null) and
3546 	   (c_Formula_Rec.segment7 is null) and (c_Formula_Rec.segment8 is null) and (c_Formula_Rec.segment9 is null) and
3547 	   (c_Formula_Rec.segment10 is null) and (c_Formula_Rec.segment11 is null) and (c_Formula_Rec.segment12 is null) and
3548 	   (c_Formula_Rec.segment13 is null) and (c_Formula_Rec.segment14 is null) and (c_Formula_Rec.segment15 is null) and
3549 	   (c_Formula_Rec.segment16 is null) and (c_Formula_Rec.segment17 is null) and (c_Formula_Rec.segment18 is null) and
3550 	   (c_Formula_Rec.segment19 is null) and (c_Formula_Rec.segment20 is null) and (c_Formula_Rec.segment21 is null) and
3551 	   (c_Formula_Rec.segment22 is null) and (c_Formula_Rec.segment23 is null) and (c_Formula_Rec.segment24 is null) and
3552 	   (c_Formula_Rec.segment25 is null) and (c_Formula_Rec.segment26 is null) and (c_Formula_Rec.segment27 is null) and
3553 	   (c_Formula_Rec.segment28 is null) and (c_Formula_Rec.segment29 is null) and (c_Formula_Rec.segment30 is null))) then
3554     begin
3555       l_type4 := TRUE;
3556     end;
3557     else
3558     begin
3559       message_token('PARAMETER', p_parameter_name);
3560       add_message('PSB', 'PSB_INVALID_PARAM_FORMULA');
3561       raise FND_API.G_EXC_ERROR;
3562     end;
3563     end if;
3564 
3565     if l_type1 then
3566     begin
3567 
3568       l_ytd_amount:=Compute_Line_Total(
3569                       p_worksheet_id => p_worksheet_id,
3570 		      p_flex_mapping_set_id => p_flex_mapping_set_id,
3571 		      p_budget_calendar_id => p_budget_calendar_id,
3572 		      p_ccid => p_ccid,
3573 		      p_budget_year_type_id => c_Formula_Rec.budget_year_type_id,
3574 		      p_balance_type => c_Formula_Rec.balance_type,
3575 		      p_currency_code => c_Formula_Rec.currency_code,
3576 		      p_service_package_id => p_service_package_id,
3577 			  /* bug no 4256345 */
3578 			  p_stage_set_id        => p_stage_set_id,
3579 			  p_current_stage_seq   => p_current_stage_seq
3580 			  /* bug no 4256345 */);
3581 
3582       /* Bug 3499337 start */
3583       -- Reset the l_ytd_amount to 0 when no line found
3584       if l_ytd_amount = FND_API.G_MISS_NUM OR l_ytd_amount IS NULL then
3585         l_ytd_amount := 0;
3586       end if;
3587 
3588       -- Comment out the following condition since the case will never existed.
3589       -- if l_ytd_amount <> FND_API.G_MISS_NUM then
3590       -- begin
3591       /* Bug 3499337 end */
3592 
3593 	if c_Formula_Rec.postfix_operator = '+' then
3594 	  l_line_total := l_ytd_amount + c_Formula_Rec.amount;
3595 	elsif c_Formula_Rec.postfix_operator = '-' then
3596 	  l_line_total := l_ytd_amount - c_Formula_Rec.amount;
3597 	elsif c_Formula_Rec.postfix_operator = '*' then
3598 	begin
3599 
3600 	  l_line_total := l_ytd_amount * c_Formula_Rec.amount;
3601 
3602 	  if FND_API.to_Boolean(p_compound_annually) then
3603 	    l_compound_total := l_ytd_amount * POWER(c_Formula_Rec.amount, p_compound_factor);
3604 	  end if;
3605 
3606 	end;
3607 	elsif c_Formula_Rec.postfix_operator = '/' then
3608 	begin
3609 
3610 	  -- Avoid a divide-by-zero error
3611 
3612 	  if c_Formula_Rec.amount = 0 then
3613 	    l_line_total := 0;
3614 	  else
3615 	    l_line_total := l_ytd_amount / c_Formula_Rec.amount;
3616 	  end if;
3617 
3618 	end;
3619 	else
3620 	begin
3621 	  message_token('PARAMETER', p_parameter_name);
3622 	  add_message('PSB', 'PSB_INVALID_PARAM_FORMULA');
3623 	  raise FND_API.G_EXC_ERROR;
3624 	end;
3625 	end if;
3626 
3627       -- Bug 3499337: comment out the following lines
3628       -- end;
3629       -- end if;
3630 
3631     end;
3632 
3633     -- For this type of formula, compose the Segment Combo by merging Segment Values
3634     -- from the Account field of the Formula with the Segment Values from the CCID itself
3635 
3636     elsif l_type2 then
3637     begin
3638 
3639       for l_index in 1..PSB_WS_ACCT1.g_num_segs loop
3640 
3641 	if ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT1') and
3642 	    (c_Formula_Rec.segment1 is not null)) then
3643 	  l_seg_val(l_index) := c_Formula_Rec.segment1;
3644 
3645 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT2') and
3646 	    (c_Formula_Rec.segment2 is not null)) then
3647 	  l_seg_val(l_index) := c_Formula_Rec.segment2;
3648 
3649 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT3') and
3650 	    (c_Formula_Rec.segment3 is not null)) then
3651 	  l_seg_val(l_index) := c_Formula_Rec.segment3;
3652 
3653 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT4') and
3654 	    (c_Formula_Rec.segment4 is not null)) then
3655 	  l_seg_val(l_index) := c_Formula_Rec.segment4;
3656 
3657 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT5') and
3658 	    (c_Formula_Rec.segment5 is not null)) then
3659 	  l_seg_val(l_index) := c_Formula_Rec.segment5;
3660 
3661 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT6') and
3662 	    (c_Formula_Rec.segment6 is not null)) then
3663 	  l_seg_val(l_index) := c_Formula_Rec.segment6;
3664 
3665 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT7') and
3666 	    (c_Formula_Rec.segment7 is not null)) then
3667 	  l_seg_val(l_index) := c_Formula_Rec.segment7;
3668 
3669 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT8') and
3670 	    (c_Formula_Rec.segment8 is not null)) then
3671 	  l_seg_val(l_index) := c_Formula_Rec.segment8;
3672 
3673 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT9') and
3674 	    (c_Formula_Rec.segment9 is not null)) then
3675 	  l_seg_val(l_index) := c_Formula_Rec.segment9;
3676 
3677 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT10') and
3678 	    (c_Formula_Rec.segment10 is not null)) then
3679 	  l_seg_val(l_index) := c_Formula_Rec.segment10;
3680 
3681 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT11') and
3682 	    (c_Formula_Rec.segment11 is not null)) then
3683 	  l_seg_val(l_index) := c_Formula_Rec.segment11;
3684 
3685 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT12') and
3686 	    (c_Formula_Rec.segment12 is not null)) then
3687 	  l_seg_val(l_index) := c_Formula_Rec.segment12;
3688 
3689 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT13') and
3690 	    (c_Formula_Rec.segment13 is not null)) then
3691 	  l_seg_val(l_index) := c_Formula_Rec.segment13;
3692 
3693 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT14') and
3694 	    (c_Formula_Rec.segment14 is not null)) then
3695 	  l_seg_val(l_index) := c_Formula_Rec.segment14;
3696 
3697 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT15') and
3698 	    (c_Formula_Rec.segment15 is not null)) then
3699 	  l_seg_val(l_index) := c_Formula_Rec.segment15;
3700 
3701 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT16') and
3702 	    (c_Formula_Rec.segment16 is not null)) then
3703 	  l_seg_val(l_index) := c_Formula_Rec.segment16;
3704 
3705 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT17') and
3706 	    (c_Formula_Rec.segment17 is not null)) then
3707 	  l_seg_val(l_index) := c_Formula_Rec.segment17;
3708 
3709 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT18') and
3710 	    (c_Formula_Rec.segment18 is not null)) then
3711 	  l_seg_val(l_index) := c_Formula_Rec.segment18;
3712 
3713 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT19') and
3714 	    (c_Formula_Rec.segment19 is not null)) then
3715 	  l_seg_val(l_index) := c_Formula_Rec.segment19;
3716 
3717 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT20') and
3718 	    (c_Formula_Rec.segment20 is not null)) then
3719 	  l_seg_val(l_index) := c_Formula_Rec.segment20;
3720 
3721 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT21') and
3722 	    (c_Formula_Rec.segment21 is not null)) then
3723 	  l_seg_val(l_index) := c_Formula_Rec.segment21;
3724 
3725 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT22') and
3726 	    (c_Formula_Rec.segment22 is not null)) then
3727 	  l_seg_val(l_index) := c_Formula_Rec.segment22;
3728 
3729 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT23') and
3730 	    (c_Formula_Rec.segment23 is not null)) then
3731 	  l_seg_val(l_index) := c_Formula_Rec.segment23;
3732 
3733 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT24') and
3734 	    (c_Formula_Rec.segment24 is not null)) then
3735 	  l_seg_val(l_index) := c_Formula_Rec.segment24;
3736 
3737 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT25') and
3738 	    (c_Formula_Rec.segment25 is not null)) then
3739 	  l_seg_val(l_index) := c_Formula_Rec.segment25;
3740 
3741 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT26') and
3742 	    (c_Formula_Rec.segment26 is not null)) then
3743 	  l_seg_val(l_index) := c_Formula_Rec.segment26;
3744 
3745 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT27') and
3746 	    (c_Formula_Rec.segment27 is not null)) then
3747 	  l_seg_val(l_index) := c_Formula_Rec.segment27;
3748 
3749 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT28') and
3750 	    (c_Formula_Rec.segment28 is not null)) then
3751 	  l_seg_val(l_index) := c_Formula_Rec.segment28;
3752 
3753 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT29') and
3754 	    (c_Formula_Rec.segment29 is not null)) then
3755 	  l_seg_val(l_index) := c_Formula_Rec.segment29;
3756 
3757 	elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT30') and
3758 	    (c_Formula_Rec.segment30 is not null)) then
3759 	  l_seg_val(l_index) := c_Formula_Rec.segment30;
3760 
3761 	else
3762 	  l_seg_val(l_index) := l_ccid_val(l_index);
3763 
3764 /* Bug No 2719865 Start */
3765 	  l_ccid_defined := FALSE;
3766 /* Bug No 2719865 End */
3767 	end if;
3768 
3769       end loop;
3770 
3771       l_flex_delimiter := FND_FLEX_EXT.Get_Delimiter
3772 			    (application_short_name => 'SQLGL',
3773 			     key_flex_code => 'GL#',
3774 			     structure_number => p_flex_code);
3775 
3776       l_concat_segments := FND_FLEX_EXT.Concatenate_Segments
3777 			     (n_segments => PSB_WS_ACCT1.g_num_segs,
3778 			      segments => l_seg_val,
3779 			      delimiter => l_flex_delimiter);
3780 
3781       if not FND_FLEX_KEYVAL.Validate_Segs
3782 	(operation => 'FIND_COMBINATION',
3783 	 appl_short_name => 'SQLGL',
3784 	 key_flex_code => 'GL#',
3785 	 structure_number => p_flex_code,
3786 	 concat_segments => l_concat_segments) then
3787 
3788 /* Bug No 2719865 Start */
3789 	  if (g_first_ccid) then
3790   	    FND_MSG_PUB.Add;
3791 	    g_first_ccid := FALSE;
3792 	    message_token('PARAMETER', p_parameter_name);
3793 	    message_token('CCID', l_concat_segments);
3794 	    add_message('PSB', 'PSB_DISABLED_ACCT_PARAMETER');
3795 	  end if;
3796 
3797 	  if ((not l_ccid_defined) and (not g_first_ccid)) then
3798   	    FND_MSG_PUB.Add;
3799 	    message_token('PARAMETER', p_parameter_name);
3800 	    message_token('CCID', l_concat_segments);
3801 	    add_message('PSB', 'PSB_DISABLED_ACCT_PARAMETER');
3802 	  end if;
3803 /* Bug No 2719865 End */
3804 
3805       else
3806       begin
3807 
3808 	l_ccid := FND_FLEX_KEYVAL.Combination_ID;
3809 
3810 	l_ytd_amount:=Compute_Line_Total(
3811                         p_worksheet_id => p_worksheet_id,
3812 			p_flex_mapping_set_id => p_flex_mapping_set_id,
3813 			p_budget_calendar_id => p_budget_calendar_id,
3814 			p_ccid => l_ccid,
3815 			p_budget_year_type_id =>c_Formula_Rec.budget_year_type_id,
3816 			p_balance_type => c_Formula_Rec.balance_type,
3817 			p_currency_code => c_Formula_Rec.currency_code,
3818 			p_service_package_id => p_service_package_id,
3819 			/* bug no 4256345 */
3820 			p_stage_set_id        => p_stage_set_id,
3821 			p_current_stage_seq   => p_current_stage_seq
3822 			/* bug no 4256345 */);
3823 
3824         /* Bug 3499337 start */
3825         -- Reset the l_ytd_amount to 0 when no line found
3826         if l_ytd_amount = FND_API.G_MISS_NUM OR l_ytd_amount IS NULL then
3827           l_ytd_amount := 0;
3828         end if;
3829 
3830         -- Comment out the following condition since the case will never existed.
3831   	-- if l_ytd_amount <> FND_API.G_MISS_NUM then
3832 	-- begin
3833         /* Bug 3499337 end */
3834 
3835 	  l_ccid_found := TRUE;
3836 
3837 	  if c_Formula_Rec.postfix_operator = '+' then
3838 	    l_line_total := l_ytd_amount + c_Formula_Rec.amount;
3839 	  elsif c_Formula_Rec.postfix_operator = '-' then
3840 	    l_line_total := l_ytd_amount - c_Formula_Rec.amount;
3841 	  elsif c_Formula_Rec.postfix_operator = '*' then
3842 	  begin
3843 
3844 	    l_line_total := l_ytd_amount * c_Formula_Rec.amount;
3845 
3846 	    if FND_API.to_Boolean(p_compound_annually) then
3847 	      l_compound_total := l_ytd_amount * POWER(c_Formula_Rec.amount, p_compound_factor);
3848 	    end if;
3849 
3850 	  end;
3851 	  elsif c_Formula_Rec.postfix_operator = '/' then
3852 	  begin
3853 
3854 	    -- Avoid a divide-by-zero error
3855 
3856 	    if c_Formula_Rec.amount = 0 then
3857 	      l_line_total := 0;
3858 	    else
3859 	      l_line_total := l_ytd_amount / c_Formula_Rec.amount;
3860 	    end if;
3861 
3862 	  end;
3863 	  else
3864 	    l_line_total := l_ytd_amount;
3865 	  end if;
3866 
3867         -- Bug 3499337: Comment out the following lines.
3868         -- end;
3869 	-- end if;
3870 
3871 	-- If YTD Balance for the dependent CCID is not computed as yet, populate the structures
3872 	-- for the deferred CCIDs
3873 
3874 	if ((not l_ccid_found) and
3875 	    (FND_API.to_Boolean(p_defer_ccids))) then
3876 	begin
3877 
3878 	  if not l_deferred then
3879 	    l_deferred := TRUE;
3880 	  end if;
3881 
3882 	  if l_first_time then
3883 	  begin
3884 
3885 	    l_first_time := FALSE;
3886 
3887 	    g_num_defccids := g_num_defccids + 1;
3888 
3889 	    g_deferred_ccids(g_num_defccids).budget_group_id := p_budget_group_id;
3890 	    g_deferred_ccids(g_num_defccids).num_proposed_years := p_num_proposed_years;
3891 	    g_deferred_ccids(g_num_defccids).ccid := p_ccid;
3892 	    g_deferred_ccids(g_num_defccids).ccid_start_period := p_ccid_start_period;
3893 	    g_deferred_ccids(g_num_defccids).ccid_end_period := p_ccid_end_period;
3894 
3895 	  end;
3896 	  end if;
3897 
3898 	  g_num_depccids := g_num_depccids + 1;
3899 
3900 	  g_dependent_ccids(g_num_depccids).ccid := p_ccid;
3901 	  g_dependent_ccids(g_num_depccids).dependent_ccid := l_ccid;
3902 
3903 	end;
3904 	end if;
3905 
3906       end;
3907       end if;
3908 
3909     end;
3910     elsif l_type3 then
3911       l_line_total := c_Formula_Rec.amount;
3912     end if;
3913 
3914     if c_Formula_Rec.prefix_operator = '=' then
3915       g_running_total := l_line_total;
3916     elsif c_Formula_Rec.prefix_operator = '+' then
3917       g_running_total := g_running_total + l_line_total;
3918     elsif c_Formula_Rec.prefix_operator = '-' then
3919       g_running_total := g_running_total - l_line_total;
3920     elsif c_Formula_Rec.prefix_operator = '*' then
3921       g_running_total := g_running_total * l_line_total;
3922     elsif c_Formula_Rec.prefix_operator = '/' then
3923     begin
3924 
3925       -- Avoid divide-by-zero error
3926 
3927       if l_line_total = 0 then
3928 	g_running_total := 0;
3929       else
3930 	g_running_total := g_running_total / l_line_total;
3931       end if;
3932 
3933     end;
3934     end if;
3935 
3936   end loop;
3937 
3938   -- Compound Annually is applicable only for a single line Type1 or Type2 Formula
3939 
3940   if ((l_num_lines = 1) and
3941      ((l_type1) or (l_type2)) and
3942       (FND_API.to_Boolean(p_compound_annually))) then
3943     g_running_total := l_compound_total;
3944   end if;
3945 
3946   -- Initialize API return status to success
3947 
3948   p_return_status := FND_API.G_RET_STS_SUCCESS;
3949   p_deferred := l_deferred;
3950 
3951 
3952 EXCEPTION
3953 
3954    when FND_API.G_EXC_ERROR then
3955      p_return_status := FND_API.G_RET_STS_ERROR;
3956 
3957    when FND_API.G_EXC_UNEXPECTED_ERROR then
3958      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3959 
3960    when OTHERS then
3961      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3962      if FND_MSG_PUB.Check_Msg_Level
3963        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
3964      then
3965        FND_MSG_PUB.Add_Exc_Msg
3966 	  (p_pkg_name => G_PKG_NAME,
3967 	   p_procedure_name => 'Process_Parameter');
3968      end if;
3969 
3970 END Process_Parameter;
3971 
3972 /* ----------------------------------------------------------------------- */
3973 
3974 -- Check if parameter exists for account for this period
3975 
3976 FUNCTION AccParam_Exists
3977 (p_parameter_id        NUMBER,
3978  p_ccid                NUMBER,
3979  p_year_start_date     DATE,
3980  p_year_end_date       DATE
3981 ) RETURN BOOLEAN IS
3982 
3983   l_parameter_exists   BOOLEAN := FALSE;
3984 
3985   /* Bug 3543845: Eliminate PSB_SET_RELATIONS_V */
3986 
3987   cursor c_Exists is
3988     select 'Exists'
3989       from PSB_ENTITY a
3990      where entity_id = p_parameter_id
3991        and exists
3992 	  (select 1
3993 	     from PSB_SET_RELATIONS b,
3994 		  PSB_BUDGET_ACCOUNTS c
3995 	    where b.account_position_set_id = c.account_position_set_id
3996 	      and b.parameter_id = p_parameter_id
3997 	      and c.code_combination_id = p_ccid)
3998        and a.entity_subtype = 'ACCOUNT'
3999        and (((a.effective_start_date <= p_year_end_date)
4000 	 and (a.effective_end_date is null))
4001 	 or ((a.effective_start_date between p_year_start_date and p_year_end_date)
4002 	  or (a.effective_end_date between p_year_start_date and p_year_end_date)
4003 	  or ((effective_start_date < p_year_start_date)
4004 	  and (effective_end_date > p_year_end_date))));
4005 BEGIN
4006 
4007   for c_Exists_Rec in c_Exists loop
4008     l_parameter_exists := TRUE;
4009   end loop;
4010 
4011   return l_parameter_exists;
4012 
4013 END AccParam_Exists;
4014 /*---------------------------------------------------------------------------*/
4015 
4016 
4017 /*===========================================================================+
4018  |                      PROCEDURE Revise_Account_Projections                 |
4019  +===========================================================================*/
4020 --
4021 -- Revise Projections for Line Items by applying account parameter
4022 --
4023 PROCEDURE Revise_Account_Projections
4024 ( p_api_version       IN   NUMBER,
4025   p_validation_level  IN   NUMBER := FND_API.G_VALID_LEVEL_NONE,
4026   p_return_status     OUT  NOCOPY  VARCHAR2,
4027   p_worksheet_id      IN   NUMBER,
4028   p_parameter_id      IN   NUMBER
4029 ) IS
4030 
4031   l_api_name          CONSTANT VARCHAR2(30) := 'Revise_Account_Projections';
4032   l_api_version       CONSTANT NUMBER       := 1.0;
4033 
4034   l_return_status          VARCHAR2(1);
4035 
4036   l_budget_group_id        NUMBER;
4037   l_budget_calendar_id     NUMBER;
4038   l_flex_mapping_set_id    NUMBER;
4039   l_budget_by_position     BOOLEAN;
4040   l_flex_code              NUMBER;
4041 
4042   l_parameter_name         VARCHAR2(30);
4043   l_currency_code          VARCHAR2(15);
4044   l_effective_start_date   DATE;
4045   l_effective_end_date     DATE;
4046 
4047   l_parameter_exists       BOOLEAN;
4048   l_code_combination_id    NUMBER;
4049   l_ccid_type              VARCHAR2(30);
4050 
4051   l_compound_annually      VARCHAR2(1);
4052   l_compound_factor        NUMBER;
4053 
4054   l_deferred               BOOLEAN;
4055 
4056   l_init_index             PLS_INTEGER;
4057 
4058   l_year_start_date        DATE;
4059   l_year_end_date          DATE;
4060   l_gl_cutoff_period       DATE;
4061 
4062   -- Bug#1584464: Added
4063   l_note                   VARCHAR2(4000); -- Bug#4571412
4064 
4065   /* start bug no 4256345 */
4066   l_stage_set_id        NUMBER;
4067   l_current_stage_seq   NUMBER;
4068   /* start bug no 4256345 */
4069 
4070   cursor c_WS is
4071     select a.budget_group_id, a.budget_calendar_id, a.flex_mapping_set_id,
4072 	   a.budget_by_position, nvl(b.chart_of_accounts_id, b.root_chart_of_accounts_id) chart_of_accounts_id,
4073            a.gl_cutoff_period,
4074 	   /* bug no 4256345 */
4075            a.stage_set_id,
4076            a.current_stage_seq
4077            /* bug no 4256345 */
4078       from PSB_WORKSHEETS_V a,
4079 	   PSB_BUDGET_GROUPS_V b
4080      where a.worksheet_id = p_worksheet_id
4081        and b.budget_group_id = a.budget_group_id;
4082 
4083   cursor c_Parameter is
4084     select name,
4085 	   currency_code,
4086 	   effective_start_date,
4087 	   effective_end_date,
4088 	   parameter_compound_annually
4089       from PSB_ENTITY
4090      where entity_id = p_parameter_id
4091        and entity_subtype = 'ACCOUNT';
4092 
4093   -- Bug#1584464: Note_id column added.
4094   -- Bug#3237740: Added join to PSB_BUDGET_YEAR_TYPES.
4095   -- Bug#3570461: currency_code column added.
4096   CURSOR c_AcctLine IS
4097     select b.account_line_id,
4098            b.code_combination_id,
4099            b.service_package_id,
4100            b.budget_group_id,
4101            b.budget_year_id,
4102            b.currency_code,
4103 	   c.start_date,
4104            c.end_date,
4105            b.note_id,
4106            d.year_category_type,
4107            c.name -- Bug#4571412
4108       FROM PSB_WS_LINES a,
4109            PSB_WS_ACCOUNT_LINES b,
4110            PSB_BUDGET_PERIODS c,
4111            PSB_BUDGET_YEAR_TYPES d
4112      WHERE a.worksheet_id        = p_worksheet_id
4113        AND b.account_line_id     = a.account_line_id
4114        AND b.end_stage_seq is null
4115        AND b.balance_type        = 'E'
4116        AND b.template_id is null
4117        AND c.budget_period_id    = b.budget_year_id
4118        AND c.budget_year_type_id = d.budget_year_type_id
4119        AND c.budget_period_type  = 'Y'
4120   ORDER BY b.code_combination_id,
4121            b.budget_year_id;
4122   --
4123 BEGIN
4124 
4125   -- Standard call to check for call compatibility
4126   if not FND_API.Compatible_API_Call (l_api_version,
4127 				      p_api_version,
4128 				      l_api_name,
4129 				      G_PKG_NAME)
4130   then
4131     raise FND_API.G_EXC_UNEXPECTED_ERROR;
4132   end if;
4133 
4134   for c_WS_Rec in c_WS loop
4135     l_budget_group_id       := c_WS_Rec.budget_group_id;
4136     l_budget_calendar_id    := c_WS_Rec.budget_calendar_id;
4137     l_flex_mapping_set_id   := c_WS_Rec.flex_mapping_set_id;
4138     l_flex_code             := c_WS_Rec.chart_of_accounts_id;
4139 
4140     /* bug no 4256345 */
4141     l_stage_set_id          := c_WS_Rec.stage_set_id;
4142     l_current_stage_seq     := c_WS_Rec.current_stage_seq;
4143     /* bug no 4256345 */
4144 
4145 
4146     -- Bug#3237740: Added the following.
4147     l_gl_cutoff_period      := c_WS_Rec.gl_cutoff_period;
4148 
4149     if c_WS_Rec.budget_by_position = 'Y' then
4150       l_budget_by_position := TRUE;
4151     else
4152       l_budget_by_position := FALSE;
4153     end if;
4154 
4155   end loop;
4156 
4157   for c_Parameter_Rec in c_Parameter loop
4158     l_parameter_name := c_Parameter_Rec.name;
4159     l_currency_code := c_Parameter_Rec.currency_code;
4160     l_effective_start_date := c_Parameter_Rec.effective_start_date;
4161     l_effective_end_date := c_Parameter_Rec.effective_end_date;
4162 
4163     if nvl(c_Parameter_Rec.parameter_compound_annually, 'N') = 'N' then
4164       l_compound_annually := FND_API.G_FALSE;
4165     else
4166       l_compound_annually := FND_API.G_TRUE;
4167     end if;
4168   end loop;
4169 
4170   for l_init_index in 1..PSB_WS_ACCT1.g_max_num_amounts loop
4171     g_period_amount(l_init_index) := null;
4172   end loop;
4173 
4174   for c_AcctLine_Rec in c_AcctLine loop
4175 
4176     /* Bug 3570461 Start */
4177     IF c_AcctLine_Rec.currency_code = l_currency_code
4178        OR l_currency_code IS NULL THEN
4179     /* Bug 3570461 End */
4180 
4181     if c_AcctLine_Rec.code_combination_id <> nvl(l_code_combination_id, 0) then
4182     begin
4183 
4184       l_code_combination_id := c_AcctLine_Rec.code_combination_id;
4185 
4186       PSB_WS_ACCT1.Check_CCID_Type
4187 	 (p_api_version => 1.0,
4188 	  p_return_status => l_return_status,
4189 	  p_ccid_type => l_ccid_type,
4190 	  p_flex_code => l_flex_code,
4191 	  p_ccid => c_AcctLine_Rec.code_combination_id,
4192 	  p_budget_group_id => l_budget_group_id);
4193 
4194       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4195 	raise FND_API.G_EXC_ERROR;
4196       end if;
4197 
4198     end;
4199     end if;
4200 
4201     l_parameter_exists := AccParam_Exists
4202                           ( p_parameter_id => p_parameter_id,
4203                             p_ccid => c_AcctLine_Rec.code_combination_id,
4204                             p_year_start_date => c_AcctLine_Rec.start_date,
4205                             p_year_end_date => c_AcctLine_Rec.end_date ) ;
4206 
4207     /* Bug 3237740 Start */
4208     IF  (c_AcctLine_Rec.year_category_type = 'CY'
4209          AND  c_AcctLine_Rec.end_date <> l_gl_cutoff_period )
4210       OR
4211         (c_AcctLine_Rec.year_category_type = 'PP')
4212     THEN
4213     /* Bug 3237740 End */
4214 
4215     if ((l_parameter_exists) and
4216        (((l_ccid_type <> 'PERSONNEL_SERVICES') and (l_budget_by_position))
4217        or (not(l_budget_by_position))))
4218     then
4219 
4220       -- Compute Compound Factor for each year if Compound Annually is set.
4221       if FND_API.to_Boolean(l_compound_annually) then
4222         --
4223         /* Bug No 2627277 Start */
4224         -- l_compound_factor := greatest(ceil(months_between(l_year_start_date, l_effective_start_date) / 12), 0) + 1;
4225 	l_compound_factor := greatest(ceil(months_between(c_AcctLine_Rec.start_date, l_effective_start_date) / 12), 0) + 1;
4226         /* Bug No 2627277 End */
4227         --
4228       end if;
4229 
4230       -- Parse Parameter Formulae and compute a YTD running total to allocate
4231       -- across Budget Periods
4232       Process_Parameter
4233 	     (p_worksheet_id => p_worksheet_id,
4234 	      p_service_package_id => c_AcctLine_Rec.service_package_id,
4235 	      p_parameter_id => p_parameter_id,
4236 	      p_parameter_name => l_parameter_name,
4237 	      p_compound_annually => l_compound_annually,
4238 	      p_compound_factor => l_compound_factor,
4239 	      p_budget_calendar_id => l_budget_calendar_id,
4240 	      p_flex_code => l_flex_code,
4241 	      p_flex_mapping_set_id => l_flex_mapping_set_id,
4242 	      p_ccid => c_AcctLine_Rec.code_combination_id,
4243 	      p_ccid_start_period => c_AcctLine_Rec.start_date,
4244 	      p_ccid_end_period => c_AcctLine_Rec.end_date,
4245 	      p_budget_group_id => c_AcctLine_Rec.budget_group_id,
4246 	      p_num_proposed_years => null,
4247 	      p_return_status => l_return_status,
4248 	      p_defer_ccids => FND_API.G_FALSE,
4249 	      p_deferred => l_deferred,
4250 	      /* bug no 4256345 */
4251               p_stage_set_id          => l_stage_set_id,
4252 	      p_current_stage_seq     => l_current_Stage_seq
4253   	      /* bug no 4256345 */);
4254 
4255       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4256 	raise FND_API.G_EXC_ERROR;
4257       end if;
4258 
4259       -- Create Account Distribution for the CCID
4260 
4261       PSB_WS_ACCT1.Create_Account_Dist
4262 	 (p_api_version => 1.0,
4263 	  p_return_status => l_return_status,
4264 	  p_worksheet_id => p_worksheet_id,
4265 	  p_distribute_flag => FND_API.G_TRUE,
4266 	  p_account_line_id => c_AcctLine_Rec.account_line_id,
4267 	  p_ytd_amount => g_running_total,
4268 	  p_period_amount => g_period_amount,
4269 	  p_budget_group_id => c_AcctLine_Rec.budget_group_id);
4270 
4271       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4272 	raise FND_API.G_EXC_ERROR;
4273       end if;
4274 
4275       /* Bug No 1584464 Start */
4276       -- Insert note record in PSB_WS_ACCOUNT_LINE_NOTES table
4277       FND_MESSAGE.SET_NAME('PSB', 'PSB_PARAMETER_NOTE_CREATION');
4278       FND_MESSAGE.SET_TOKEN('NAME', l_parameter_name);
4279       FND_MESSAGE.SET_TOKEN('DATE', sysdate);
4280       l_note := FND_MESSAGE.GET;
4281 
4282       -- Bug#4571412
4283       -- Added parameters to make the call in
4284       -- sync wih it's definition.
4285       PSB_WS_ACCT1.Create_Note
4286       ( p_return_status         => l_return_status,
4287         p_account_line_id       => c_AcctLine_Rec.account_line_id,
4288         p_note                  => l_note,
4289         p_chart_of_accounts_id  => l_flex_code,
4290         p_budget_year           => c_AcctLine_Rec.Name,
4291         p_cc_id                 => c_AcctLine_Rec.code_combination_id,
4292         p_concatenated_segments => NULL
4293       ) ;
4294 
4295       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4296 	raise FND_API.G_EXC_ERROR;
4297       end if;
4298       /* Bug No 1584464 End */
4299 
4300     end if;
4301 
4302     END IF;
4303     END IF;
4304 
4305   end loop;
4306 
4307   -- Initialize API return status to success
4308   p_return_status := FND_API.G_RET_STS_SUCCESS;
4309 
4310 EXCEPTION
4311 
4312    when FND_API.G_EXC_ERROR then
4313      p_return_status := FND_API.G_RET_STS_ERROR;
4314 
4315    when FND_API.G_EXC_UNEXPECTED_ERROR then
4316      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4317 
4318    when OTHERS then
4319      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4320 
4321      if FND_MSG_PUB.Check_Msg_Level
4322        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4323      then
4324        FND_MSG_PUB.Add_Exc_Msg
4325 	  (p_pkg_name => G_PKG_NAME,
4326 	   p_procedure_name => l_api_name);
4327      end if;
4328 
4329 END Revise_Account_Projections;
4330 /*---------------------------------------------------------------------------*/
4331 
4332 
4333 /*===========================================================================+
4334  |                      PROCEDURE Apply_Account_Parameters                   |
4335  +===========================================================================*/
4336 --
4337 -- Apply Account Parameters for a CCID for the CY and PP Budget Years
4338 --
4339 PROCEDURE Apply_Account_Parameters
4340 ( p_api_version            IN   NUMBER,
4341   p_validation_level       IN   NUMBER := FND_API.G_VALID_LEVEL_NONE,
4342   p_return_status          OUT  NOCOPY  VARCHAR2,
4343   p_worksheet_id           IN   NUMBER,
4344   p_service_package_id     IN   NUMBER,
4345   p_start_stage_seq        IN   NUMBER := FND_API.G_MISS_NUM,
4346   p_current_stage_seq      IN   NUMBER,
4347   p_rounding_factor        IN   NUMBER := FND_API.G_MISS_NUM,
4348   p_stage_set_id           IN   NUMBER,
4349   p_budget_group_id        IN   NUMBER,
4350   p_allocrule_set_id       IN   NUMBER := FND_API.G_MISS_NUM,
4351   p_gl_cutoff_period       IN   DATE := FND_API.G_MISS_DATE,
4352   p_flex_code              IN   NUMBER := FND_API.G_MISS_NUM,
4353   p_func_currency          IN   VARCHAR2 := FND_API.G_MISS_CHAR,
4354   p_flex_mapping_set_id    IN   NUMBER := FND_API.G_MISS_NUM,
4355   p_ccid                   IN   NUMBER,
4356   p_ccid_start_period      IN   DATE,
4357   p_ccid_end_period        IN   DATE,
4358   p_num_proposed_years     IN   NUMBER,
4359   p_num_years_to_allocate  IN   NUMBER := FND_API.G_MISS_NUM,
4360   p_parameter_set_id       IN   NUMBER := FND_API.G_MISS_NUM,
4361   p_budget_calendar_id     IN   NUMBER := FND_API.G_MISS_NUM,
4362   p_budget_by_position     IN   VARCHAR2 := FND_API.G_MISS_CHAR,
4363   p_defer_ccids            IN   VARCHAR2 := FND_API.G_TRUE
4364 ) IS
4365 
4366   l_api_name               CONSTANT VARCHAR2(30) := 'Apply_Account_Parameters';
4367   l_api_version            CONSTANT NUMBER       := 1.0;
4368 
4369   l_return_status          VARCHAR2(1);
4370   l_init_index             PLS_INTEGER;
4371   l_year_index             PLS_INTEGER;
4372   l_period_index           PLS_INTEGER;
4373   l_num_budget_years       NUMBER;
4374   l_num_proposed_years     NUMBER := 0;
4375 
4376   -- Bug 3543845
4377   l_global_worksheet_id    NUMBER;
4378 
4379   l_allocrule_set_id       NUMBER;
4380   l_flex_mapping_set_id    NUMBER;
4381   l_parameter_set_id       NUMBER;
4382   l_budget_calendar_id     NUMBER;
4383   l_rounding_factor        NUMBER;
4384   l_gl_cutoff_period       DATE;
4385   l_num_years_to_allocate  NUMBER;
4386   l_budget_by_position     VARCHAR2(1);
4387 
4388   /* Bug No 2640277 Start */
4389   l_year_start_date        DATE;
4390   l_year_end_date          DATE;
4391   /* Bug No 2640277 End */
4392 
4393   l_func_currency          VARCHAR2(15);
4394   l_flex_code              NUMBER;
4395 
4396   l_start_stage_seq        NUMBER;
4397   l_num_allocated_years    NUMBER := 0;
4398   l_currency_code          VARCHAR2(15);
4399   l_compound_annually      VARCHAR2(1);
4400   l_allocation_type        VARCHAR2(10);
4401   l_compound_factor        NUMBER;
4402   l_account_line_id        NUMBER;
4403   l_include_calc_periods   VARCHAR2(1);
4404   l_mapped_ccid            NUMBER;
4405   l_deferred               BOOLEAN;
4406   -- bug 4308904
4407   l_ccid_param_exists	   BOOLEAN;
4408 
4409   -- Local variable to determine whether at least a parameter exists for the
4410   -- account code being processed.
4411   l_cy_parameter_exists    BOOLEAN := FALSE;
4412 
4413   /* Bug 3543845: Comment out since the query is spilted into two in the body
4414   cursor c_WS is
4415     select nvl(allocrule_set_id, global_allocrule_set_id) allocrule_set_id,
4416 	   flex_mapping_set_id,
4417 	   parameter_set_id,
4418 	   budget_calendar_id,
4419 	   rounding_factor,
4420 	   gl_cutoff_period,
4421 	   num_years_to_allocate,
4422 	   budget_by_position
4423       from PSB_WORKSHEETS_V
4424      where worksheet_id = p_worksheet_id;
4425   */
4426 
4427   cursor c_SOB is
4428     select nvl(currency_code, root_currency_code) currency_code,
4429 	   nvl(chart_of_accounts_id, root_chart_of_accounts_id) chart_of_accounts_id
4430       from PSB_BUDGET_GROUPS_V
4431      where budget_group_id = p_budget_group_id;
4432 
4433   -- Find all Parameters that are applicable for this CCID for the Budget Year
4434   -- identified by Year_Start_Date, Year_End_Date. The CCID should be valid for
4435   -- the duration of the parameter. Parameters are sorted by the Parameter Start
4436   -- Date and Priority within a Budget Year
4437 
4438   -- Bug 3543845: Avoid using PSB_SET_RELATIONS_V
4439   cursor c_Parameter (Year_Start_Date DATE,
4440                       Year_End_Date DATE) is
4441     select parameter_id,
4442            name,
4443            priority,
4444            currency_code,
4445            effective_start_date,
4446            parameter_compound_annually
4447       from PSB_PARAMETER_ASSIGNMENTS_V a
4448      where exists
4449            (select 1
4450             from   PSB_SET_RELATIONS b,
4451                    PSB_BUDGET_ACCOUNTS c
4452             where  b.account_position_set_id = c.account_position_set_id
4453                    and b.parameter_id = a.parameter_id
4454                    and c.code_combination_id = p_ccid
4455            )
4456            and parameter_type = 'ACCOUNT'
4457            and ( ( effective_start_date
4458                    <= nvl(p_ccid_end_period, Year_End_Date)
4459                    and effective_end_date is null
4460                  )
4461                  or ( ( effective_start_date
4462                         between nvl(p_ccid_start_period, Year_Start_Date)
4463                         and nvl(p_ccid_end_period, Year_End_Date)
4464                       )
4465                       or
4466                       ( effective_end_date
4467                         between nvl(p_ccid_start_period, Year_Start_Date)
4468                         and nvl(p_ccid_end_period, Year_End_Date)
4469                       )
4470                       or
4471                       ( effective_start_date
4472                         < nvl(p_ccid_start_period, Year_Start_Date)
4473                       and
4474                       effective_end_date
4475                       > nvl(p_ccid_end_period, Year_End_Date)
4476                     )
4477                  )
4478                )
4479            and ( ( effective_start_date <= Year_End_Date
4480                    and effective_end_date is null)
4481                  or
4482                  ( ( effective_start_date
4483                      between Year_Start_Date and Year_End_Date
4484                    )
4485                    or ( effective_end_date
4486                         between Year_Start_Date and Year_End_Date
4487                       )
4488                    or ( effective_start_date < Year_Start_Date
4489                         and effective_end_date > Year_End_Date
4490                       )
4491                  )
4492                )
4493            and parameter_set_id = l_parameter_set_id
4494      order by effective_start_date, priority;
4495 BEGIN
4496 
4497   -- Standard call to check for call compatibility
4498   if not FND_API.Compatible_API_Call (l_api_version,
4499 				      p_api_version,
4500 				      l_api_name,
4501 				      G_PKG_NAME)
4502   then
4503     raise FND_API.G_EXC_UNEXPECTED_ERROR;
4504   end if;
4505 
4506   -- Select default values for input parameters that were not passed in
4507   if ((p_allocrule_set_id = FND_API.G_MISS_NUM) or
4508       (p_flex_mapping_set_id = FND_API.G_MISS_NUM) or
4509       (p_parameter_set_id = FND_API.G_MISS_NUM) or
4510       (p_budget_calendar_id = FND_API.G_MISS_NUM) or
4511       (p_rounding_factor = FND_API.G_MISS_NUM) or
4512       (p_gl_cutoff_period = FND_API.G_MISS_DATE) or
4513       (p_num_years_to_allocate = FND_API.G_MISS_NUM) or
4514       (p_budget_by_position = FND_API.G_MISS_CHAR))
4515   then
4516     /* Bug 3543845 start */
4517     /* comment out the following query and use cached value when possible
4518     for c_WS_Rec in c_WS loop
4519       l_allocrule_set_id := c_WS_Rec.allocrule_set_id;
4520       l_flex_mapping_set_id := c_WS_Rec.flex_mapping_set_id;
4521       l_parameter_set_id := c_WS_Rec.parameter_set_id;
4522       l_budget_calendar_id := c_WS_Rec.budget_calendar_id;
4523       l_rounding_factor := c_WS_Rec.rounding_factor;
4524       l_gl_cutoff_period := c_WS_Rec.gl_cutoff_period;
4525       l_num_years_to_allocate := c_WS_Rec.num_years_to_allocate;
4526       l_budget_by_position := c_WS_Rec.budget_by_position;
4527     end loop;
4528     */
4529     -- Check the g_ws_creation_flag to determine whether to use the
4530     -- cached worksheet values.
4531     IF PSB_WORKSHEET.g_ws_creation_flag
4532        AND
4533        PSB_WORKSHEET.g_worksheet_id = p_worksheet_id
4534     THEN
4535       -- Retrieve the worksheet values from cache to avoiding extra query
4536       l_global_worksheet_id := PSB_WORKSHEET.g_global_worksheet_id;
4537       l_gl_cutoff_period := PSB_WORKSHEET.g_gl_cutoff_period;
4538       l_allocrule_set_id := PSB_WORKSHEET.g_allocrule_set_id;
4539       l_budget_calendar_id := PSB_WORKSHEET.g_budget_calendar_id;
4540       l_rounding_factor := PSB_WORKSHEET.g_rounding_factor;
4541       l_flex_mapping_set_id := PSB_WORKSHEET.g_flex_mapping_set_id;
4542       l_parameter_set_id := PSB_WORKSHEET.g_parameter_set_id;
4543       l_num_years_to_allocate := PSB_WORKSHEET.g_num_years_to_allocate;
4544       l_budget_by_position := PSB_WORKSHEET.g_budget_by_position;
4545 
4546     ELSE
4547 
4548       SELECT DECODE(global_worksheet_flag, 'Y', worksheet_id,
4549                     global_worksheet_id) global_worksheet_id
4550            INTO
4551              l_global_worksheet_id
4552       FROM   psb_worksheets
4553       WHERE  worksheet_id = p_worksheet_id;
4554 
4555       SELECT gl_cutoff_period,
4556              allocrule_set_id allocrule_set_id,
4557              budget_calendar_id,
4558              rounding_factor,
4559              flex_mapping_set_id,
4560              parameter_set_id,
4561              num_years_to_allocate,
4562              budget_by_position
4563            INTO
4564              l_gl_cutoff_period,
4565              l_allocrule_set_id,
4566              l_budget_calendar_id,
4567              l_rounding_factor,
4568              l_flex_mapping_set_id,
4569              l_parameter_set_id,
4570              l_num_years_to_allocate,
4571              l_budget_by_position
4572       FROM   psb_worksheets
4573       WHERE  worksheet_id = l_global_worksheet_id;
4574 
4575     END IF;
4576     /* Bug 3543845 End */
4577   end if;
4578 
4579   -- Substitute values from input parameters that were passed in
4580 
4581   if p_allocrule_set_id <> FND_API.G_MISS_NUM then
4582     l_allocrule_set_id := p_allocrule_set_id;
4583   end if;
4584 
4585   if p_flex_mapping_set_id <> FND_API.G_MISS_NUM then
4586     l_flex_mapping_set_id := p_flex_mapping_set_id;
4587   end if;
4588 
4589   if p_parameter_set_id <> FND_API.G_MISS_NUM then
4590     l_parameter_set_id := p_parameter_set_id;
4591   end if;
4592 
4593   if p_budget_calendar_id <> FND_API.G_MISS_NUM then
4594     l_budget_calendar_id := p_budget_calendar_id;
4595   end if;
4596 
4597   if p_rounding_factor <> FND_API.G_MISS_NUM then
4598     l_rounding_factor := p_rounding_factor;
4599   end if;
4600 
4601   if p_gl_cutoff_period <> FND_API.G_MISS_DATE then
4602     l_gl_cutoff_period := p_gl_cutoff_period;
4603   end if;
4604 
4605   if p_num_years_to_allocate <> FND_API.G_MISS_NUM then
4606     l_num_years_to_allocate := p_num_years_to_allocate;
4607   end if;
4608 
4609   if ((l_budget_by_position is null) or (l_budget_by_position = 'N')) then
4610     l_budget_by_position := FND_API.G_FALSE;
4611   else
4612     l_budget_by_position := FND_API.G_TRUE;
4613   end if;
4614 
4615   if p_budget_by_position <> FND_API.G_MISS_CHAR then
4616     l_budget_by_position := p_budget_by_position;
4617   end if;
4618 
4619   if ((p_func_currency = FND_API.G_MISS_CHAR) or
4620       (p_flex_code = FND_API.G_MISS_NUM))
4621   then
4622 
4623     for c_SOB_Rec in c_SOB loop
4624       l_func_currency := c_SOB_Rec.currency_code;
4625       l_flex_code := c_SOB_Rec.chart_of_accounts_id;
4626     end loop;
4627 
4628   end if;
4629 
4630   if p_func_currency <> FND_API.G_MISS_CHAR then
4631     l_func_currency := p_func_currency;
4632   end if;
4633 
4634   if p_flex_code <> FND_API.G_MISS_NUM then
4635     l_flex_code := p_flex_code;
4636   end if;
4637 
4638   if p_start_stage_seq = FND_API.G_MISS_NUM then
4639     l_start_stage_seq := p_current_stage_seq;
4640   else
4641     l_start_stage_seq := p_start_stage_seq;
4642   end if;
4643 
4644   -- Create Zero Balances Profile Option
4645   if g_create_zero_bal is null then
4646 
4647     FND_PROFILE.GET
4648        (name => 'PSB_CREATE_ZERO_BALANCE_ACCT',
4649 	val => g_create_zero_bal);
4650 
4651     if g_create_zero_bal is null then
4652       -- Bug 3548345: Change default behavior to not creating zero balance
4653       g_create_zero_bal := 'N';
4654     end if;
4655 
4656   end if;
4657 
4658   if l_budget_calendar_id <> nvl(PSB_WS_ACCT1.g_budget_calendar_id,
4659                                  FND_API.G_MISS_NUM)
4660   then
4661 
4662     PSB_WS_ACCT1.Cache_Budget_Calendar
4663     ( p_return_status      => l_return_status,
4664       p_budget_calendar_id => l_budget_calendar_id
4665     ) ;
4666 
4667     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4668       raise FND_API.G_EXC_ERROR;
4669     end if;
4670 
4671   end if;
4672 
4673   -- Number of Budget Years to project
4674   l_num_budget_years := least(ceil(months_between(nvl(p_ccid_end_period, PSB_WS_ACCT1.g_end_est_date),
4675 						  nvl(p_ccid_start_period, PSB_WS_ACCT1.g_startdate_pp)) / 12),
4676 				nvl(p_num_proposed_years, PSB_WS_ACCT1.g_max_num_years));
4677 
4678   for l_year_index in 1..PSB_WS_ACCT1.g_num_budget_years loop
4679 
4680     /* Bug No 2640277 Start */
4681     -- Following condition has been changed
4682 
4683     l_year_start_date := PSB_WS_ACCT1.g_budget_years(l_year_index).start_date;
4684     l_year_end_date := PSB_WS_ACCT1.g_budget_years(l_year_index).end_date;
4685 
4686     -- for bug 4308904
4687     l_ccid_param_exists := false;
4688 
4689     /*
4690     if ((PSB_WS_ACCT1.g_budget_years(l_year_index).year_type in ('CY', 'PP')) and
4691 	(PSB_WS_ACCT1.g_budget_years(l_year_index).start_date >= nvl(p_ccid_start_period, PSB_WS_ACCT1.g_startdate_cy)) and
4692 	(PSB_WS_ACCT1.g_budget_years(l_year_index).end_date <= nvl(p_ccid_end_period, PSB_WS_ACCT1.g_end_est_date)))  then
4693     */
4694 
4695     if (PSB_WS_ACCT1.g_budget_years(l_year_index).year_type in ('CY', 'PP'))
4696 	    and (((nvl(p_ccid_start_period, PSB_WS_ACCT1.g_startdate_cy) between l_year_start_date and l_year_end_date)
4697 	       or (nvl(p_ccid_end_period, PSB_WS_ACCT1.g_end_est_date) between l_year_start_date and l_year_end_date)
4698 	       or ((nvl(p_ccid_start_period, PSB_WS_ACCT1.g_startdate_cy) < l_year_start_date)
4699 		 and (nvl(p_ccid_end_period, PSB_WS_ACCT1.g_end_est_date) > l_year_end_date))))
4700     then
4701     /* Bug No 2640277 End */
4702 
4703     begin
4704 
4705       g_running_total := 0;
4706 
4707       for l_init_index in 1..PSB_WS_ACCT1.g_max_num_amounts loop
4708 	g_period_amount(l_init_index) := null;
4709       end loop;
4710 
4711       for l_init_index in 1..g_alloc_periods.Count loop
4712 	g_alloc_periods(l_init_index).budget_period_id := null;
4713 	g_alloc_periods(l_init_index).long_sequence_no := null;
4714 	g_alloc_periods(l_init_index).start_date := null;
4715 	g_alloc_periods(l_init_index).end_date := null;
4716 	g_alloc_periods(l_init_index).budget_year_id := null;
4717       end loop;
4718 
4719       l_init_index := 1;
4720 
4721       for l_period_index in 1..PSB_WS_ACCT1.g_num_budget_periods loop
4722 
4723 	if PSB_WS_ACCT1.g_budget_periods(l_period_index).budget_year_id =
4724 	   PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id
4725         then
4726 
4727 	  -- Periods over which to allocate should include all Budget Periods
4728           -- in the PP Budget Years and all Budget Periods upto the GL Cutoff
4729           -- Period in the CY Budget Year
4730 
4731 	  if ((PSB_WS_ACCT1.g_budget_years(l_year_index).year_type = 'PP') or
4732 	     ((PSB_WS_ACCT1.g_budget_years(l_year_index).year_type = 'CY') and
4733 	     ((l_gl_cutoff_period is null) or
4734 	      (l_gl_cutoff_period < PSB_WS_ACCT1.g_budget_periods(l_period_index).start_date))))
4735           then
4736 	    g_alloc_periods(l_init_index).budget_period_id :=
4737                PSB_WS_ACCT1.g_budget_periods(l_period_index).budget_period_id;
4738 	    g_alloc_periods(l_init_index).long_sequence_no :=
4739                PSB_WS_ACCT1.g_budget_periods(l_period_index).long_sequence_no;
4740 	    g_alloc_periods(l_init_index).start_date :=
4741                PSB_WS_ACCT1.g_budget_periods(l_period_index).start_date;
4742 	    g_alloc_periods(l_init_index).end_date :=
4743                PSB_WS_ACCT1.g_budget_periods(l_period_index).end_date;
4744 	    g_alloc_periods(l_init_index).budget_year_id :=
4745                PSB_WS_ACCT1.g_budget_periods(l_period_index).budget_year_id;
4746 
4747 	    l_init_index := l_init_index + 1;
4748 	  end if;
4749 
4750 	end if;
4751 
4752       end loop; /* Budget Period */
4753 
4754       /* Bug 3603538 Start */
4755       IF PSB_WS_ACCT1.g_budget_years(l_year_index).year_type = 'CY'
4756          AND
4757          l_year_end_date = l_gl_cutoff_period
4758       THEN
4759         FOR c_Parameter_Rec IN c_Parameter (PSB_WS_ACCT1.g_budget_years(l_year_index).start_date,
4760 					    PSB_WS_ACCT1.g_budget_years(l_year_index).end_date) LOOP
4761 
4762           l_currency_code := c_Parameter_Rec.currency_code;
4763 
4764         END LOOP;
4765 
4766       END IF;
4767       /* Bug 3603538 End */
4768 
4769       IF PSB_WS_ACCT1.g_budget_years(l_year_index).year_type = 'CY'
4770          AND
4771          l_year_end_date <> l_gl_cutoff_period
4772       THEN
4773          -- Added the above condition as part of Bug fix 3469514
4774       begin
4775 
4776 	-- Find all CCIDs that are applicable for the CY
4777 	for c_Parameter_Rec in c_Parameter (PSB_WS_ACCT1.g_budget_years(l_year_index).start_date,
4778 					    PSB_WS_ACCT1.g_budget_years(l_year_index).end_date) loop
4779 	  -- Set the local variable.
4780 	  l_cy_parameter_exists := TRUE;
4781 
4782           -- for bug 4308904
4783           l_ccid_param_exists := TRUE;
4784 
4785 	  l_currency_code := c_Parameter_Rec.currency_code;
4786 
4787 	  -- Compute a Compound Factor for each Budget Year if Compound Annually is set
4788 
4789 	  if ((c_Parameter_Rec.parameter_compound_annually is null) or
4790 	      (c_Parameter_Rec.parameter_compound_annually = 'N')) then
4791 	    l_compound_annually := FND_API.G_FALSE;
4792 	  else
4793 	    l_compound_annually := FND_API.G_TRUE;
4794 	    l_compound_factor := greatest(ceil(months_between(PSB_WS_ACCT1.g_budget_years(l_year_index).start_date,
4795 							      c_Parameter_Rec.effective_start_date) / 12), 0) + 1;
4796 	  end if;
4797 
4798 	  -- Parse the Parameter Formulae and compute a YTD running total to
4799           -- allocate across Budget Periods. If the CCID was deferred because
4800           -- of dependency on another yet-to-be-computed CCID, the parameter
4801           -- l_deferred is set to FND_API.G_TRUE
4802 
4803 	  l_deferred := FALSE;
4804 
4805 	  Process_Parameter
4806 		 (p_worksheet_id => p_worksheet_id,
4807 		  p_service_package_id => p_service_package_id,
4808 		  p_parameter_id => c_Parameter_Rec.parameter_id,
4809 		  p_parameter_name => c_Parameter_Rec.name,
4810 		  p_compound_annually => l_compound_annually,
4811 		  p_compound_factor => l_compound_factor,
4812 		  p_budget_calendar_id => l_budget_calendar_id,
4813 		  p_flex_code => l_flex_code,
4814 		  p_flex_mapping_set_id => l_flex_mapping_set_id,
4815 		  p_ccid => p_ccid,
4816 		  p_ccid_start_period => p_ccid_start_period,
4817 		  p_ccid_end_period => p_ccid_end_period,
4818 		  p_budget_group_id => p_budget_group_id,
4819 		  p_num_proposed_years => p_num_proposed_years,
4820 		  p_return_status => l_return_status,
4821 		  p_defer_ccids => p_defer_ccids,
4822 		  p_deferred => l_deferred,
4823 		  /* bug no 4256345 */
4824   		  p_stage_set_id          => p_stage_set_id,
4825   		  p_current_stage_seq     => p_current_stage_seq
4826           	 /* bug no 4256345 */);
4827 
4828 	  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4829 	    raise FND_API.G_EXC_ERROR;
4830 	  end if;
4831 
4832 	  if l_deferred then
4833 	    raise FND_API.G_EXC_ERROR;
4834 	  end if;
4835 
4836           /* Bug 4308904 Start */
4837           -- Bug#3126462: Support Percent type allocation rules for CY estimates
4838           -- No need to hardcode allocation type to Profile. Process all types.
4839 	  --l_allocation_type := 'PROFILE';
4840 	  l_allocation_type := NULL;
4841 
4842 	  -- Subtract the Actual Balances for the CY since g_running_total
4843           -- computes the Estimate Balance for the full year. Also, Estimate
4844           -- Balances for CY includes the Actual Balances upto GL Cutoff Period
4845 
4846 	  IF l_cy_parameter_exists THEN
4847 	  BEGIN
4848 
4849 	    IF nvl(l_currency_code, p_func_currency) = p_func_currency THEN
4850 	      g_running_total := g_running_total - nvl(g_actuals_func_total, 0);
4851 	    ELSE
4852 	      g_running_total := g_running_total - nvl(g_actuals_stat_total, 0);
4853 	    END IF;
4854 
4855 	  END;
4856 	  END IF;
4857 
4858 
4859 
4860 	-- If an Allocation Rule Set has been specified in the WS definition,
4861         -- distribute using the applicable Allocation Rule; otherwise, the YTD
4862         -- Amount is evenly distributed to the individual Period Amounts
4863 
4864 
4865         Distribute_Account_Lines
4866         ( p_return_status => l_return_status,
4867           p_worksheet_id => p_worksheet_id,
4868           p_flex_mapping_set_id => l_flex_mapping_set_id,
4869           p_budget_year_type_id =>
4870             PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_type_id,
4871           p_allocrule_set_id => l_allocrule_set_id,
4872           p_budget_calendar_id => l_budget_calendar_id,
4873           p_ccid => p_ccid,
4874           p_ytd_amount => g_running_total,
4875           -- Bug#3401175: Defaulting currency code to functional currency.
4876           p_currency_code => NVL(l_currency_code, l_func_currency),
4877           p_allocation_type => l_allocation_type,
4878           -- Bug#2342169: Added the following parameter.
4879           p_rounding_factor => l_rounding_factor,
4880           p_effective_start_date =>
4881             PSB_WS_ACCT1.g_budget_years(l_year_index).start_date,
4882           p_effective_end_date =>
4883             PSB_WS_ACCT1.g_budget_years(l_year_index).end_date,
4884           p_budget_periods => g_alloc_periods,
4885           p_period_amount => g_period_amount
4886         ) ;
4887 
4888         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4889           raise FND_API.G_EXC_ERROR;
4890         END IF;
4891 
4892         -- Bug 3543845: Check whether the worksheet creation process is
4893         -- executed for the first time. If it is the first time, then check the
4894         -- running total and create zero balance profile. Otherwise, call the
4895         -- create_account_Dist without any filtering.
4896         IF ( PSB_WORKSHEET.g_ws_first_time_creation_flag
4897              and
4898              ( g_running_total <> 0
4899                or
4900                (g_running_total = 0 and g_create_zero_bal = 'Y')
4901              )
4902            )
4903            OR
4904            NOT PSB_WORKSHEET.g_ws_first_time_creation_flag
4905         THEN
4906         BEGIN
4907 
4908 
4909 
4910 	  -- Create Account Distribution for the CCID
4911 	  PSB_WS_ACCT1.Create_Account_Dist
4912 	     (p_api_version => 1.0,
4913 	      p_return_status => l_return_status,
4914 	      p_account_line_id => l_account_line_id,
4915 	      p_worksheet_id => p_worksheet_id,
4916 	      p_service_package_id => p_service_package_id,
4917 	      p_check_spal_exists => FND_API.G_FALSE,
4918 	      p_gl_cutoff_period => null,
4919 	      p_allocrule_set_id => null,
4920 	      p_budget_calendar_id => null,
4921 	      p_rounding_factor => l_rounding_factor,
4922 	      p_stage_set_id => p_stage_set_id,
4923 	      p_start_stage_seq => l_start_stage_seq,
4924 	      p_current_stage_seq => p_current_stage_seq,
4925 	      p_budget_group_id => p_budget_group_id,
4926 	      p_budget_year_id => PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id,
4927 	      p_flex_mapping_set_id => l_flex_mapping_set_id,
4928 	      p_map_accounts => TRUE,
4929 	      p_ccid => p_ccid,
4930 	      p_currency_code => nvl(l_currency_code, l_func_currency),
4931 	      p_balance_type => 'E',
4932 	      p_ytd_amount => g_running_total,
4933 	      p_period_amount => g_period_amount);
4934 
4935 	  IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4936 	    raise FND_API.G_EXC_ERROR;
4937 	  END IF;
4938 
4939         END;
4940 	END IF;
4941 
4942 
4943 
4944         IF nvl(l_flex_mapping_set_id, FND_API.G_MISS_NUM) <> FND_API.G_MISS_NUM THEN
4945         BEGIN
4946 
4947 	l_mapped_ccid := PSB_WS_ACCT1.Map_Account
4948 			    (p_flex_mapping_set_id => l_flex_mapping_set_id,
4949 			     p_ccid => p_ccid,
4950 			     p_budget_year_type_id => PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_type_id);
4951 
4952         END;
4953         ELSE
4954           l_mapped_ccid := p_ccid;
4955         END IF;
4956 
4957         PSB_WS_ACCT1.Copy_CY_Estimates
4958 	   (p_return_status => l_return_status,
4959 	    p_worksheet_id => p_worksheet_id,
4960 	    p_service_package_id => p_service_package_id,
4961 	    p_rounding_factor => l_rounding_factor,
4962 	    p_start_stage_seq => p_current_stage_seq,
4963 	    p_budget_group_id => p_budget_group_id,
4964 	    p_stage_set_id => p_stage_set_id,
4965 	    p_budget_year_id => PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id,
4966 	    p_ccid => l_mapped_ccid,
4967 	    p_currency_code => nvl(l_currency_code, l_func_currency));
4968 
4969         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4970 	  raise FND_API.G_EXC_ERROR;
4971         END IF;
4972 
4973         /* Bug 4308904 End */
4974 
4975 	END LOOP;
4976 
4977         IF l_ccid_param_exists THEN
4978           l_num_allocated_years := l_num_allocated_years + 1;
4979         END IF;
4980 
4981         END;
4982 
4983       -- If Pos Budgeting is enabled, CCID is of type 'NON_PERSONNEL_SERVICES'
4984 
4985       ELSIF (PSB_WS_ACCT1.g_budget_years(l_year_index).year_type = 'PP') THEN
4986       BEGIN
4987 
4988 	IF l_num_proposed_years < l_num_budget_years THEN
4989 	BEGIN
4990 
4991 	  -- Find all Parameters that are applicable for the PP Budget Year
4992 
4993 	  for c_Parameter_Rec in c_Parameter (PSB_WS_ACCT1.g_budget_years(l_year_index).start_date,
4994 					      PSB_WS_ACCT1.g_budget_years(l_year_index).end_date) loop
4995 
4996 	    l_currency_code := c_Parameter_Rec.currency_code;
4997 
4998             -- for bug 4308904
4999             l_ccid_param_exists := TRUE;
5000 
5001 	    -- Compute a Compound Factor for the PP Budget Year if Compound Annually is set
5002 
5003 	    IF ((c_Parameter_Rec.parameter_compound_annually is null) or
5004 		(c_Parameter_Rec.parameter_compound_annually = 'N')) THEN
5005 	      l_compound_annually := FND_API.G_FALSE;
5006 	    ELSE
5007 	      l_compound_annually := FND_API.G_TRUE;
5008 	      l_compound_factor := greatest(ceil(months_between(PSB_WS_ACCT1.g_budget_years(l_year_index).start_date,
5009 								c_Parameter_Rec.effective_start_date) / 12), 0) + 1;
5010 	    END IF;
5011 
5012 	    -- Parse the Parameter Formulae and compute a running total for the
5013             -- PP Budget Year. If the CCID is deferred for processing, the
5014             -- parameter l_deferred is set to FND_API.G_TRUE
5015 
5016 	    l_deferred := FALSE;
5017 
5018 	    Process_Parameter
5019 		   (p_worksheet_id => p_worksheet_id,
5020 		    p_service_package_id => p_service_package_id,
5021 		    p_parameter_id => c_Parameter_Rec.parameter_id,
5022 		    p_parameter_name => c_Parameter_Rec.name,
5023 		    p_compound_annually => l_compound_annually,
5024 		    p_compound_factor => l_compound_factor,
5025 		    p_budget_calendar_id => l_budget_calendar_id,
5026 		    p_flex_code => l_flex_code,
5027 		    p_flex_mapping_set_id => l_flex_mapping_set_id,
5028 		    p_ccid => p_ccid,
5029 		    p_ccid_start_period => p_ccid_start_period,
5030 		    p_ccid_end_period => p_ccid_end_period,
5031 		    p_budget_group_id => p_budget_group_id,
5032 		    p_num_proposed_years => p_num_proposed_years,
5033 		    p_return_status => l_return_status,
5034 		    p_defer_ccids => p_defer_ccids,
5035 		    p_deferred => l_deferred,
5036 			/* bug no 4256345 */
5037   			p_stage_set_id		    => p_stage_set_id,
5038   			p_current_stage_seq     => p_current_stage_seq
5039                     /* bug no 4256345 */);
5040 
5041 	    IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5042 	      raise FND_API.G_EXC_ERROR;
5043 	    END IF;
5044 
5045 	    IF l_deferred THEN
5046 	      raise FND_API.G_EXC_ERROR;
5047 	    END IF;
5048 
5049            /* Bug 4308904 Start */
5050 
5051            IF   (l_num_allocated_years <= least(l_num_budget_years, nvl(l_num_years_to_allocate, PSB_WS_ACCT1.g_max_num_years)))
5052            THEN
5053            begin
5054 
5055             -- Bug#3401175: Support Profile type allocation rules for PP years.
5056 	    --l_allocation_type := 'PERCENT';
5057 	    l_allocation_type := NULL;
5058 
5059 
5060             -- If an Allocation Rule Set has been specified in the WS definition,
5061             -- distribute using the applicable Allocation Rule; otherwise, the YTD
5062             -- Amount is evenly distributed to the individual Period Amounts
5063 
5064             /* Bug 3352171 start */
5065             -- Comment out the following two lines. The allocation logic will be
5066             -- handled in the PSB_WS_ACCT2.Distribute_Account_Lines function.
5067             --if nvl(l_allocrule_set_id, FND_API.G_MISS_NUM) <> FND_API.G_MISS_NUM
5068             --then
5069             /* Bug 3352171 end */
5070 
5071             Distribute_Account_Lines
5072             ( p_return_status => l_return_status,
5073               p_worksheet_id => p_worksheet_id,
5074               p_flex_mapping_set_id => l_flex_mapping_set_id,
5075               p_budget_year_type_id =>
5076                PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_type_id,
5077               p_allocrule_set_id => l_allocrule_set_id,
5078               p_budget_calendar_id => l_budget_calendar_id,
5079               p_ccid => p_ccid,
5080               p_ytd_amount => g_running_total,
5081               -- Bug#3401175: Defaulting currency code to functional currency.
5082               p_currency_code => NVL(l_currency_code, l_func_currency),
5083               p_allocation_type => l_allocation_type,
5084               -- Bug#2342169: Added the following parameter.
5085               p_rounding_factor => l_rounding_factor,
5086               p_effective_start_date =>
5087                PSB_WS_ACCT1.g_budget_years(l_year_index).start_date,
5088               p_effective_end_date =>
5089                PSB_WS_ACCT1.g_budget_years(l_year_index).end_date,
5090               p_budget_periods => g_alloc_periods,
5091               p_period_amount => g_period_amount
5092             ) ;
5093 
5094             IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5095               raise FND_API.G_EXC_ERROR;
5096             END IF;
5097 
5098             -- l_num_allocated_years := l_num_allocated_years + 1;
5099 
5100          END;
5101          END IF;
5102 
5103 
5104          -- Bug 3543845: Check whether the worksheet creation process is
5105          -- executed for the first time. If it is the first time, then check the
5106          -- running total and create zero balance profile. Otherwise, call the
5107          -- create_account_Dist without any filtering.
5108          IF ( PSB_WORKSHEET.g_ws_first_time_creation_flag
5109               and
5110               ( g_running_total <> 0
5111                 or
5112                 (g_running_total = 0 and g_create_zero_bal = 'Y')
5113               )
5114             )
5115             OR
5116             NOT PSB_WORKSHEET.g_ws_first_time_creation_flag
5117          THEN
5118          BEGIN
5119 
5120 	  -- Create Account Distribution for the CCID
5121 	  PSB_WS_ACCT1.Create_Account_Dist
5122 	     (p_api_version => 1.0,
5123 	      p_return_status => l_return_status,
5124 	      p_account_line_id => l_account_line_id,
5125 	      p_worksheet_id => p_worksheet_id,
5126 	      p_service_package_id => p_service_package_id,
5127 	      p_check_spal_exists => FND_API.G_FALSE,
5128 	      p_gl_cutoff_period => null,
5129 	      p_allocrule_set_id => null,
5130 	      p_budget_calendar_id => null,
5131 	      p_rounding_factor => l_rounding_factor,
5132 	      p_stage_set_id => p_stage_set_id,
5133 	      p_start_stage_seq => l_start_stage_seq,
5134 	      p_current_stage_seq => p_current_stage_seq,
5135 	      p_budget_group_id => p_budget_group_id,
5136 	      p_budget_year_id => PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id,
5137 	      p_flex_mapping_set_id => l_flex_mapping_set_id,
5138 	      p_map_accounts => TRUE,
5139 	      p_ccid => p_ccid,
5140 	      p_currency_code => nvl(l_currency_code, l_func_currency),
5141 	      p_balance_type => 'E',
5142 	      p_ytd_amount => g_running_total,
5143 	      p_period_amount => g_period_amount);
5144 
5145 	  IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5146 	    raise FND_API.G_EXC_ERROR;
5147 	  END IF;
5148         END;
5149 	END IF;
5150         /* Bug 4308904 End*/
5151 
5152 	END LOOP;
5153 
5154           -- for bug 4308904
5155           IF l_ccid_param_exists THEN
5156             l_num_allocated_years := l_num_allocated_years + 1;
5157           END IF;
5158 
5159 	  l_num_proposed_years := l_num_proposed_years + 1;
5160 
5161 	end;
5162 	end if;
5163 
5164       end;
5165       end if;
5166 
5167 
5168       -- for bug 4308904
5169       -- This check is there for CCIDS falling outside
5170       -- the parameter range or for CCIDS where parameter is
5171       -- not applicable.
5172 
5173       IF not l_ccid_param_exists THEN
5174       IF (
5175            (
5176             (PSB_WS_ACCT1.g_budget_years(l_year_index).year_type = 'CY')
5177              AND
5178              l_year_end_date <> l_gl_cutoff_period
5179             -- Added the above condition as part of Bug fix 3469514
5180            )
5181              OR
5182 	   (
5183             (PSB_WS_ACCT1.g_budget_years(l_year_index).year_type = 'PP')
5184              AND
5185             (l_num_allocated_years <= least(l_num_budget_years, nvl(l_num_years_to_allocate, PSB_WS_ACCT1.g_max_num_years)))
5186            )
5187          ) THEN
5188       begin
5189 
5190 	if PSB_WS_ACCT1.g_budget_years(l_year_index).year_type = 'CY' then
5191 
5192           -- Bug#3126462: Support Percent type allocation rules for CY estimates
5193           -- No need to hardcode allocation type to Profile. Process all types.
5194 	  --l_allocation_type := 'PROFILE';
5195 	  l_allocation_type := NULL;
5196 
5197 	  -- Subtract the Actual Balances for the CY since g_running_total
5198           -- computes the Estimate Balance for the full year. Also, Estimate
5199           -- Balances for CY includes the Actual Balances upto GL Cutoff Period
5200 
5201 	  if l_cy_parameter_exists then
5202 	  begin
5203 
5204 	    if nvl(l_currency_code, p_func_currency) = p_func_currency then
5205 	      g_running_total := g_running_total - nvl(g_actuals_func_total, 0);
5206 	    else
5207 	      g_running_total := g_running_total - nvl(g_actuals_stat_total, 0);
5208 	    end if;
5209 
5210 	  end;
5211 	  end if;
5212 
5213 	else
5214           -- Bug#3401175: Support Profile type allocation rules for PP years.
5215 	  --l_allocation_type := 'PERCENT';
5216 	  l_allocation_type := NULL;
5217 	end if;
5218 
5219 	-- If an Allocation Rule Set has been specified in the WS definition,
5220         -- distribute using the applicable Allocation Rule; otherwise, the YTD
5221         -- Amount is evenly distributed to the individual Period Amounts
5222 
5223 
5224         Distribute_Account_Lines
5225         ( p_return_status => l_return_status,
5226           p_worksheet_id => p_worksheet_id,
5227           p_flex_mapping_set_id => l_flex_mapping_set_id,
5228           p_budget_year_type_id =>
5229             PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_type_id,
5230           p_allocrule_set_id => l_allocrule_set_id,
5231           p_budget_calendar_id => l_budget_calendar_id,
5232           p_ccid => p_ccid,
5233           p_ytd_amount => g_running_total,
5234           --
5235           -- Bug#3401175: Defaulting currency code to functional currency.
5236           p_currency_code => NVL(l_currency_code, l_func_currency),
5237           --
5238           p_allocation_type => l_allocation_type,
5239           --
5240           -- Bug#2342169: Added the following parameter.
5241           p_rounding_factor => l_rounding_factor,
5242           --
5243           p_effective_start_date =>
5244             PSB_WS_ACCT1.g_budget_years(l_year_index).start_date,
5245           p_effective_end_date =>
5246             PSB_WS_ACCT1.g_budget_years(l_year_index).end_date,
5247           p_budget_periods => g_alloc_periods,
5248           p_period_amount => g_period_amount
5249         ) ;
5250         --
5251 
5252         if l_return_status <> FND_API.G_RET_STS_SUCCESS then
5253           raise FND_API.G_EXC_ERROR;
5254         end if;
5255 
5256         l_num_allocated_years := l_num_allocated_years + 1;
5257 
5258 
5259 
5260       end;
5261       end if;
5262 
5263 
5264 
5265       --if (PSB_WS_ACCT1.g_budget_years(l_year_index).year_type in ('CY', 'PP')) then
5266       if ( ((PSB_WS_ACCT1.g_budget_years(l_year_index).year_type = 'CY') AND
5267 	   (PSB_WS_ACCT1.g_budget_years(l_year_index).end_date <> l_gl_cutoff_period)) OR
5268 	   (PSB_WS_ACCT1.g_budget_years(l_year_index).year_type = 'PP') ) then
5269 
5270 
5271       begin
5272 
5273         -- Commented the following line for bug 3305778
5274 
5275 
5276         -- Bug 3543845: Check whether the worksheet creation process is
5277         -- executed for the first time. If it is the first time, then check the
5278         -- running total and create zero balance profile. Otherwise, call the
5279         -- create_account_Dist without any filtering.
5280         if ( PSB_WORKSHEET.g_ws_first_time_creation_flag
5281              and
5282              ( g_running_total <> 0
5283                or
5284                (g_running_total = 0 and g_create_zero_bal = 'Y')
5285              )
5286            )
5287            OR
5288            NOT PSB_WORKSHEET.g_ws_first_time_creation_flag
5289         then
5290         begin
5291 
5292           --pd('3: Call Create_Account_Dist=> ccid=' || TO_CHAR(p_ccid) ||
5293           --   ', p_budget_year_id=' ||
5294           --   TO_CHAR(PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id)
5295           --   || ', p_ytd_amount=' || TO_CHAR(g_running_total));
5296 
5297 	  -- Create Account Distribution for the CCID
5298 	  PSB_WS_ACCT1.Create_Account_Dist
5299 	     (p_api_version => 1.0,
5300 	      p_return_status => l_return_status,
5301 	      p_account_line_id => l_account_line_id,
5302 	      p_worksheet_id => p_worksheet_id,
5303 	      p_service_package_id => p_service_package_id,
5304 	      p_check_spal_exists => FND_API.G_FALSE,
5305 	      p_gl_cutoff_period => null,
5306 	      p_allocrule_set_id => null,
5307 	      p_budget_calendar_id => null,
5308 	      p_rounding_factor => l_rounding_factor,
5309 	      p_stage_set_id => p_stage_set_id,
5310 	      p_start_stage_seq => l_start_stage_seq,
5311 	      p_current_stage_seq => p_current_stage_seq,
5312 	      p_budget_group_id => p_budget_group_id,
5313 	      p_budget_year_id => PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id,
5314 	      p_flex_mapping_set_id => l_flex_mapping_set_id,
5315 	      p_map_accounts => TRUE,
5316 	      p_ccid => p_ccid,
5317 	      p_currency_code => nvl(l_currency_code, l_func_currency),
5318 	      p_balance_type => 'E',
5319 	      p_ytd_amount => g_running_total,
5320 	      p_period_amount => g_period_amount);
5321 
5322 	  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
5323 	    raise FND_API.G_EXC_ERROR;
5324 	  end if;
5325 
5326         end;
5327 	end if;
5328 
5329       end;
5330       end if;
5331 
5332     end if;
5333 
5334     end;
5335     end if;
5336 
5337     -- For the CY the Estimate Balances include the Actual Balances upto the
5338     -- GL Cutoff Period. This API copies the GL Actual Balances upto the GL
5339     -- Cutoff Period into the Estimate Balance
5340 
5341   -- for bug 4308904
5342   IF not l_ccid_param_exists THEN
5343 
5344 
5345     IF (PSB_WS_ACCT1.g_budget_years(l_year_index).year_type = 'CY') THEN
5346     -- AND l_cy_parameter_exists THEN
5347     -- Commented the above condition as part of bug fix 3469514
5348 
5349     begin
5350 
5351       if nvl(l_flex_mapping_set_id, FND_API.G_MISS_NUM) <> FND_API.G_MISS_NUM then
5352       begin
5353 
5354 	l_mapped_ccid := PSB_WS_ACCT1.Map_Account
5355 			    (p_flex_mapping_set_id => l_flex_mapping_set_id,
5356 			     p_ccid => p_ccid,
5357 			     p_budget_year_type_id => PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_type_id);
5358 
5359       end;
5360       else
5361 	l_mapped_ccid := p_ccid;
5362       end if;
5363 
5364       PSB_WS_ACCT1.Copy_CY_Estimates
5365 	 (p_return_status => l_return_status,
5366 	  p_worksheet_id => p_worksheet_id,
5367 	  p_service_package_id => p_service_package_id,
5368 	  p_rounding_factor => l_rounding_factor,
5369 	  p_start_stage_seq => p_current_stage_seq,
5370 	  p_budget_group_id => p_budget_group_id,
5371 	  p_stage_set_id => p_stage_set_id,
5372 	  p_budget_year_id => PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id,
5373 	  p_ccid => l_mapped_ccid,
5374 	  p_currency_code => nvl(l_currency_code, l_func_currency));
5375 
5376       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
5377 	raise FND_API.G_EXC_ERROR;
5378       end if;
5379 
5380     end;
5381     end if;
5382 
5383   -- for bug 4308904
5384   end if;
5385 
5386   end loop;
5387 
5388   -- Initialize API return status to success
5389   p_return_status := FND_API.G_RET_STS_SUCCESS;
5390 
5391 EXCEPTION
5392 
5393    when FND_API.G_EXC_ERROR then
5394 
5395      -- Return Success for a deferred CCID because that will be processed
5396      -- in a later phase
5397 
5398      if l_deferred then
5399        p_return_status := FND_API.G_RET_STS_SUCCESS;
5400      else
5401        p_return_status := FND_API.G_RET_STS_ERROR;
5402      end if;
5403 
5404    when FND_API.G_EXC_UNEXPECTED_ERROR then
5405      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5406 
5407    when OTHERS then
5408      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5409 
5410      if FND_MSG_PUB.Check_Msg_Level
5411        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
5412      then
5413        FND_MSG_PUB.Add_Exc_Msg
5414 	  (p_pkg_name => G_PKG_NAME,
5415 	   p_procedure_name => l_api_name);
5416      end if;
5417 
5418 END Apply_Account_Parameters;
5419 /*---------------------------------------------------------------------------*/
5420 
5421 
5422 /*---------------------------------------------------------------------------*/
5423 --
5424 -- Sort the deferred CCIDs to avoid circular dependencies, e.g CCID-1 is
5425 -- dependent on CCID-2 which depends on CCID-3 : sort the CCIDs so that
5426 -- CCID-3 is processed first, followed by CCID-2, followed by CCID-1. The
5427 -- CCIDs are sorted and stored in g_sorted_ccids
5428 --
5429 PROCEDURE Process_Deferred_CCIDs
5430 ( p_return_status          OUT  NOCOPY  VARCHAR2,
5431   p_worksheet_id           IN   NUMBER,
5432   p_service_package_id     IN   NUMBER,
5433   p_sequence_number        IN   NUMBER,
5434   p_gl_cutoff_period       IN   DATE,
5435   p_allocrule_set_id       IN   NUMBER,
5436   p_rounding_factor        IN   NUMBER,
5437   p_stage_set_id           IN   NUMBER,
5438   p_flex_code              IN   NUMBER,
5439   p_flex_mapping_set_id    IN   NUMBER,
5440   p_func_currency          IN   VARCHAR2,
5441   p_num_years_to_allocate  IN   NUMBER,
5442   p_parameter_set_id       IN   NUMBER,
5443   p_budget_calendar_id     IN   NUMBER,
5444   p_budget_by_position     IN   VARCHAR2
5445 ) IS
5446 
5447   l_ccid_insert            BOOLEAN;
5448   l_depccid_exists         BOOLEAN;
5449 
5450   l_budget_group_id        NUMBER;
5451   l_num_proposed_years     NUMBER;
5452   l_ccid_start_period      DATE;
5453   l_ccid_end_period        DATE;
5454 
5455   l_index                  PLS_INTEGER;
5456   l_defccid_index          PLS_INTEGER;
5457   l_depccid_index          PLS_INTEGER;
5458   l_sortccid_index         PLS_INTEGER;
5459 
5460   l_ccid_exists_index      PLS_INTEGER;
5461   l_depccid_exists_index   PLS_INTEGER;
5462 
5463   l_return_status          VARCHAR2(1);
5464 
5465 BEGIN
5466 
5467   for l_defccid_index in 1..g_num_defccids loop
5468 
5469     l_ccid_exists_index := null;
5470     l_ccid_insert := FALSE;
5471 
5472     -- For each deferred CCID, loop thru the dependency list
5473 
5474     for l_depccid_index in 1..g_num_depccids loop
5475 
5476       if g_dependent_ccids(l_depccid_index).ccid = g_deferred_ccids(l_defccid_index).ccid then
5477       begin
5478 
5479 	l_depccid_exists_index := null;
5480 	l_depccid_exists := FALSE;
5481 
5482 	-- Check if any of the dependent CCIDs are also in the deferred list
5483 
5484 	for l_index in 1..g_num_defccids loop
5485 
5486 	  if g_deferred_ccids(l_index).ccid = g_dependent_ccids(l_depccid_index).dependent_ccid then
5487 	  begin
5488 
5489 	    l_budget_group_id := g_deferred_ccids(l_index).budget_group_id;
5490 	    l_num_proposed_years := g_deferred_ccids(l_index).num_proposed_years;
5491 	    l_ccid_start_period := g_deferred_ccids(l_index).ccid_start_period;
5492 	    l_ccid_end_period := g_deferred_ccids(l_index).ccid_end_period;
5493 	    l_depccid_exists := TRUE;
5494 	    exit;
5495 
5496 	  end;
5497 	  end if;
5498 
5499 	end loop;
5500 
5501 	-- If dependent CCID is also in the deferred list, check if dependent CCID is already in
5502 	-- in the sorted list g_sorted_ccids
5503 
5504 	if l_depccid_exists then
5505 	begin
5506 
5507 	  if g_num_sortccids <> 0 then
5508 	  begin
5509 
5510 	    for l_sortccid_index in 1..g_num_sortccids loop
5511 
5512 	      if g_sorted_ccids(l_sortccid_index).ccid = g_dependent_ccids(l_depccid_index).dependent_ccid then
5513 		l_depccid_exists_index := l_sortccid_index;
5514 		exit;
5515 	      end if;
5516 
5517 	    end loop;
5518 
5519 	  end;
5520 	  end if;
5521 
5522 	end;
5523 	end if;
5524 
5525 	-- Check if deferred CCID is already in the sorted list g_sorted_ccids
5526 
5527 	if g_num_sortccids <> 0 then
5528 	begin
5529 
5530 	  for l_sortccid_index in 1..g_num_sortccids loop
5531 
5532 	    if g_sorted_ccids(l_sortccid_index).ccid = g_deferred_ccids(l_defccid_index).ccid then
5533 	      l_ccid_exists_index := l_sortccid_index;
5534 	      exit;
5535 	    end if;
5536 
5537 	  end loop;
5538 
5539 	end;
5540 	end if;
5541 
5542 	-- If dependent CCID also exists in the deferred list, arrange the sorted list to avoid
5543 	-- circular dependencies
5544 
5545 	if l_depccid_exists then
5546 	begin
5547 
5548 	  if nvl(l_depccid_exists_index, g_num_sortccids + 1) > nvl(l_ccid_exists_index, g_num_sortccids + 1) then
5549 	  begin
5550 
5551 	    for l_sortccid_index in REVERSE l_ccid_exists_index + 1..nvl(l_depccid_exists_index, g_num_sortccids) loop
5552 	      g_sorted_ccids(l_sortccid_index).budget_group_id := g_sorted_ccids(l_sortccid_index -1).budget_group_id;
5553 	      g_sorted_ccids(l_sortccid_index).num_proposed_years := g_sorted_ccids(l_sortccid_index -1).num_proposed_years;
5554 	      g_sorted_ccids(l_sortccid_index).ccid := g_sorted_ccids(l_sortccid_index -1).ccid;
5555 	      g_sorted_ccids(l_sortccid_index).ccid_start_period := g_sorted_ccids(l_sortccid_index -1).ccid_start_period;
5556 	      g_sorted_ccids(l_sortccid_index).ccid_end_period := g_sorted_ccids(l_sortccid_index -1).ccid_end_period;
5557 	    end loop;
5558 
5559 	    g_sorted_ccids(l_ccid_exists_index).budget_group_id := l_budget_group_id;
5560 	    g_sorted_ccids(l_ccid_exists_index).num_proposed_years := l_num_proposed_years;
5561 	    g_sorted_ccids(l_ccid_exists_index).ccid := g_dependent_ccids(l_depccid_index).dependent_ccid;
5562 	    g_sorted_ccids(l_ccid_exists_index).ccid_start_period := l_ccid_start_period;
5563 	    g_sorted_ccids(l_ccid_exists_index).ccid_end_period := l_ccid_end_period;
5564 
5565 	    if nvl(l_depccid_exists_index, 0) = 0 then
5566 	      g_num_sortccids := g_num_sortccids + 1;
5567 	    end if;
5568 
5569 	  end;
5570 	  else
5571 	  begin
5572 
5573 	    if nvl(l_depccid_exists_index, 0) = 0 then
5574 	    begin
5575 
5576 	      g_num_sortccids := g_num_sortccids + 1;
5577 
5578 	      g_sorted_ccids(g_num_sortccids).budget_group_id := l_budget_group_id;
5579 	      g_sorted_ccids(g_num_sortccids).num_proposed_years := l_num_proposed_years;
5580 	      g_sorted_ccids(g_num_sortccids).ccid := g_dependent_ccids(l_depccid_index).dependent_ccid;
5581 	      g_sorted_ccids(g_num_sortccids).ccid_start_period := l_ccid_start_period;
5582 	      g_sorted_ccids(g_num_sortccids).ccid_end_period := l_ccid_end_period;
5583 
5584 	    end;
5585 	    end if;
5586 
5587 	    if nvl(l_ccid_exists_index, 0) = 0 then
5588 	      l_ccid_insert := TRUE;
5589 	    end if;
5590 
5591 	  end;
5592 	  end if;
5593 
5594 	end;
5595 	else
5596 	begin
5597 
5598 	  if nvl(l_ccid_exists_index, 0) = 0 then
5599 	    l_ccid_insert := TRUE;
5600 	  end if;
5601 
5602 	end;
5603 	end if;
5604 
5605       end;
5606       end if;
5607 
5608     end loop;
5609 
5610     -- Insert deferred CCID into the sorted list
5611 
5612     if l_ccid_insert then
5613 
5614       g_num_sortccids := g_num_sortccids + 1;
5615 
5616       g_sorted_ccids(g_num_sortccids).budget_group_id := g_deferred_ccids(l_defccid_index).budget_group_id;
5617       g_sorted_ccids(g_num_sortccids).num_proposed_years := g_deferred_ccids(l_defccid_index).num_proposed_years;
5618       g_sorted_ccids(g_num_sortccids).ccid := g_deferred_ccids(l_defccid_index).ccid;
5619       g_sorted_ccids(g_num_sortccids).ccid_start_period := g_deferred_ccids(l_defccid_index).ccid_start_period;
5620       g_sorted_ccids(g_num_sortccids).ccid_end_period := g_deferred_ccids(l_defccid_index).ccid_end_period;
5621 
5622     end if;
5623 
5624   end loop;
5625 
5626   -- For each deferred CCID in the sorted list, apply Account Parameters and
5627   -- compute YTD totals. The parameter p_defer_ccids is set to FND_API.G_FALSE
5628   -- because this is the last phase of processing parameters.
5629 
5630   for l_index in 1..g_num_sortccids loop
5631 
5632     Apply_Account_Parameters
5633 	 (p_api_version => 1.0,
5634 	  p_return_status => l_return_status,
5635 	  p_worksheet_id => p_worksheet_id,
5636 	  p_service_package_id => p_service_package_id,
5637 	  p_start_stage_seq => p_sequence_number,
5638 	  p_current_stage_seq => p_sequence_number,
5639 	  p_rounding_factor => p_rounding_factor,
5640 	  p_stage_set_id => p_stage_set_id,
5641 	  p_budget_group_id => g_sorted_ccids(l_index).budget_group_id,
5642 	  p_allocrule_set_id => p_allocrule_set_id,
5643 	  p_gl_cutoff_period => p_gl_cutoff_period,
5644 	  p_flex_code => p_flex_code,
5645 	  p_func_currency => p_func_currency,
5646 	  p_flex_mapping_set_id => p_flex_mapping_set_id,
5647 	  p_ccid => g_sorted_ccids(l_index).ccid,
5648 	  p_ccid_start_period => g_sorted_ccids(l_index).ccid_start_period,
5649 	  p_ccid_end_period => g_sorted_ccids(l_index).ccid_end_period,
5650 	  p_num_proposed_years => g_sorted_ccids(l_index).num_proposed_years,
5651 	  p_num_years_to_allocate => p_num_years_to_allocate,
5652 	  p_parameter_set_id => p_parameter_set_id,
5653 	  p_budget_calendar_id => p_budget_calendar_id,
5654 	  p_budget_by_position => p_budget_by_position,
5655 	  p_defer_ccids => FND_API.G_FALSE);
5656 
5657     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
5658       raise FND_API.G_EXC_ERROR;
5659     end if;
5660 
5661   end loop;
5662 
5663   -- Initialize API return status to success
5664   p_return_status := FND_API.G_RET_STS_SUCCESS;
5665 
5666 EXCEPTION
5667 
5668    when FND_API.G_EXC_ERROR then
5669      p_return_status := FND_API.G_RET_STS_ERROR;
5670 
5671    when FND_API.G_EXC_UNEXPECTED_ERROR then
5672      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5673 
5674    when OTHERS then
5675      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5676      if FND_MSG_PUB.Check_Msg_Level
5677        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
5678      then
5679        FND_MSG_PUB.Add_Exc_Msg
5680 	  (p_pkg_name => G_PKG_NAME,
5681 	   p_procedure_name => 'Process_Deferred_CCIDs');
5682      end if;
5683 
5684 END Process_Deferred_CCIDs;
5685 
5686 /* ----------------------------------------------------------------------- */
5687 
5688 -- Distribute YTD Amount using the Allocation Rules specified in the Allocation
5689 -- Rule Set. All PY balances are extracted from GL by individual periods, all CY
5690 -- balances are distributed by the profile specified in the Allocation Rule and
5691 -- all PP balances are distributed by percentages as specified in the Allocation
5692 -- Rule
5693 
5694 PROCEDURE Distribute_Account_Lines
5695 ( p_return_status         OUT  NOCOPY  VARCHAR2,
5696   p_worksheet_id          IN   NUMBER,
5697   p_flex_mapping_set_id   IN   NUMBER,
5698   p_budget_year_type_id   IN   NUMBER,
5699   p_allocrule_set_id      IN   NUMBER,
5700   p_budget_calendar_id    IN   NUMBER,
5701   p_currency_code         IN   VARCHAR2 := FND_API.G_MISS_CHAR,
5702   p_ccid                  IN   NUMBER,
5703   p_ytd_amount            IN   NUMBER,
5704   p_allocation_type       IN   VARCHAR2,
5705   /* Bug No 2342169 Start */
5706   p_rounding_factor       IN   NUMBER,
5707   /* Bug No 2342169 End */
5708   p_effective_start_date  IN   DATE,
5709   p_effective_end_date    IN   DATE,
5710   p_budget_periods        IN   PSB_WS_ACCT1.g_budgetperiod_tbl_type,
5711   p_period_amount         OUT  NOCOPY  PSB_WS_ACCT1.g_prdamt_tbl_type
5712 ) IS
5713 
5714   cursor c_AllocRule is
5715     /* Bug 3458191
5716     -- Remove the usage of PSB_ALLOCRULE_ASSIGNMENTS_V and PSB_SET_RELATIONS_V
5717     -- Replace PSB_ALLOCRULE_ASSIGNMENTS_V by PSB_ENTITY_ASSIGNMENT, PSB_ENTITY
5718     -- Replace PSB_SET_RELATIONS_V by PSB_SET_RELATIONS
5719     */
5720     select pea.entity_id as allocrule_id,
5721            pe.budget_year_type_id,
5722            pe.balance_type
5723       from PSB_ENTITY_ASSIGNMENT pea, PSB_ENTITY pe
5724      where pe.entity_type = 'ALLOCRULE'
5725            and pe.entity_id = pea.entity_id
5726            and exists
5727                (select 1
5728                   from PSB_SET_RELATIONS b,
5729                        PSB_BUDGET_ACCOUNTS c
5730                  where b.account_position_set_id = c.account_position_set_id
5731                        and b.allocation_rule_id = pea.entity_id
5732                        and c.code_combination_id = p_ccid)
5733            and pe.entity_subtype = 'ACCOUNT'
5734            and pe.allocation_type = NVL(p_allocation_type,allocation_type)
5735            and (((pea.effective_start_date <= p_effective_end_date)
5736                  and (pea.effective_end_date is null))
5737                 or ((pea.effective_start_date
5738                      between p_effective_start_date
5739                              and p_effective_end_date)
5740                     or (pea.effective_end_date
5741                         between p_effective_start_date
5742                                 and p_effective_end_date)
5743                     or ((pea.effective_start_date < p_effective_start_date)
5744                     and (pea.effective_end_date > p_effective_end_date))))
5745            and pea.entity_set_id = p_allocrule_set_id
5746      order by pea.effective_start_date,
5747               pea.priority;
5748 
5749   cursor c_AllocPct (AllocRule_ID NUMBER,
5750 		     Num_Periods NUMBER) is
5751     select period_num,
5752 	   percent
5753       from PSB_ALLOCRULE_PERCENTS
5754      where number_of_periods = Num_Periods
5755        and allocation_rule_id = AllocRule_ID
5756      order by period_num;
5757 
5758 /* Bug No 2354918 Start */
5759   CURSOR c_budyr_type IS
5760     SELECT year_category_type
5761       FROM PSB_BUDGET_YEAR_TYPES
5762      WHERE budget_year_type_id = p_budget_year_type_id;
5763 /* Bug No 2354918 End */
5764 
5765   l_running_total         NUMBER := 0;
5766 /* Bug No 2342169 Start */
5767   l_rounded_running_total NUMBER := 0;
5768   l_perc                  NUMBER;
5769   l_rounding_difference   NUMBER;
5770   l_year_category_type    VARCHAR2(30);
5771 /* Bug No 2342169 End */
5772 
5773   l_allocated             BOOLEAN := FALSE;
5774 
5775   l_index                 PLS_INTEGER;
5776 
5777   sql_alloc               VARCHAR2(3000);
5778   cur_alloc               INTEGER;
5779   num_alloc               INTEGER;
5780 
5781   l_period_amount         PSB_WS_ACCT1.g_prdamt_tbl_type;
5782   l_num_periods           NUMBER := 0;
5783 
5784   -- Bug#3126462: Support Percent type allocation rules for CY estimates
5785   l_weight                NUMBER := 0;
5786 
5787   l_amount                NUMBER;
5788   l_mapped_ccid           NUMBER;
5789 
5790 BEGIN
5791 
5792   for l_index in 1..PSB_WS_ACCT1.g_max_num_amounts loop
5793     l_period_amount(l_index) := null;
5794     p_period_amount(l_index) := null;
5795   end loop;
5796 
5797   for l_index in 1..p_budget_periods.Count loop
5798 
5799     if p_budget_periods(l_index).long_sequence_no is not null then
5800       l_num_periods := l_num_periods + 1;
5801     end if;
5802 
5803   end loop;
5804 
5805 /* Bug No 2354918 Start */
5806   for c_budyr_type_rec in c_budyr_type loop
5807       l_year_category_type := c_budyr_type_rec.year_category_type;
5808   end loop;
5809 /* Bug No 2354918 End */
5810 
5811   l_mapped_ccid := PSB_WS_ACCT1.Map_Account
5812 		      (p_flex_mapping_set_id => p_flex_mapping_set_id,
5813 		       p_ccid => p_ccid,
5814 		       p_budget_year_type_id => p_budget_year_type_id);
5815 
5816   if ((p_allocation_type = 'PROFILE') and
5817       (p_currency_code = FND_API.G_MISS_CHAR)) then
5818 
5819     message_token('ROUTINE', 'PSB_WS_ACCT2.Distribute_Account_Lines');
5820     add_message('PSB', 'PSB_INVALID_ARGUMENT');
5821     raise FND_API.G_EXC_ERROR;
5822   end if;
5823 
5824   if l_num_periods > 0 then
5825   begin
5826 
5827     -- Bug 3352171: To improving performance, following condition is added to
5828     -- skip the query and distribute year amount to periods evenly.
5829     if (p_allocrule_set_id is not NULL) then
5830 
5831     -- Loop thru the Allocation Rules and pick up a percentage profile and period profile
5832     -- for the PP and CY Budget Years respectively
5833 
5834     -- Loop to process both types of allocation rules.
5835     for c_AllocRule_Rec in c_AllocRule loop
5836 
5837       -- Begin processing allocation type 'PERCENT'.
5838       if c_AllocRule_Rec.budget_year_type_id is null then
5839       begin
5840 
5841 /* Bug No 2354918 Start */
5842 	if l_year_category_type <> 'CY' then
5843 /* Bug No 2354918 End */
5844 
5845 	for c_AllocPct_Rec in c_AllocPct (c_AllocRule_Rec.allocrule_id, l_num_periods) loop
5846 /* Bug No 2342169 Start */
5847 -- Commented the following 1 line and added the other 4 lines
5848 --          p_period_amount(c_AllocPct_Rec.period_num) := p_ytd_amount * c_AllocPct_Rec.percent / 100;
5849 
5850 	if p_rounding_factor is null then
5851 	  p_period_amount(c_AllocPct_Rec.period_num) := p_ytd_amount * nvl(c_AllocPct_Rec.percent,0) / 100;
5852 
5853 	  l_running_total := l_running_total + p_period_amount(c_AllocPct_Rec.period_num);
5854 	else
5855 	  l_period_amount(c_AllocPct_Rec.period_num) := (p_ytd_amount * nvl(c_AllocPct_Rec.percent,0) / 100)
5856 							   + (l_running_total - l_rounded_running_total);
5857 
5858 	  l_running_total := l_running_total + (p_ytd_amount * nvl(c_AllocPct_Rec.percent,0) / 100);
5859 
5860 /* Bug No 2379695 Start */
5861 	  if nvl(c_AllocPct_Rec.percent, 0) <> 0 then
5862 	    p_period_amount(c_AllocPct_Rec.period_num) := Round(l_period_amount(c_AllocPct_Rec.period_num));
5863 	  else
5864 	    p_period_amount(c_AllocPct_Rec.period_num) := 0;
5865 	  end if;
5866 /* Bug No 2379695 End */
5867 
5868 	  l_rounded_running_total := l_rounded_running_total + p_period_amount(c_AllocPct_Rec.period_num);
5869 	end if;
5870 /* Bug No 2342169 End */
5871 
5872 	  l_allocated := TRUE;
5873 	end loop;
5874 
5875 /* Bug No 2354918 Start */
5876 
5877         -- Bug#3126462: Support Percent type allocation rules for CY estimates
5878 	ELSIF l_year_category_type = 'CY' THEN
5879 
5880           --
5881           -- For the CY estimates, we need to consider period percents for the
5882           -- periods beyond the GL cutoff date. For such (estimate) periods we
5883           -- find weighted or prorated ratio from applicable period percents.
5884           --
5885           -- Also note we if get to this point, this means we have at least
5886           -- one estimate period applicable. See "l_num_periods > 0" clause.
5887           --
5888 
5889           -- Find weight for the estimate periods from the allocation rule.
5890           FOR c_AllocPct_Rec IN c_AllocPct ( c_AllocRule_Rec.allocrule_id,
5891                                              PSB_WS_Acct1.g_cy_num_periods )
5892           LOOP
5893 
5894             -- Check if period percent is applicable for estimate periods.
5895             IF c_AllocPct_Rec.period_num >= p_budget_periods(1).long_sequence_no
5896             THEN
5897               l_weight := l_weight + NVL(c_AllocPct_Rec.percent,0) ;
5898             END IF ;
5899 
5900           END LOOP ;
5901           -- End finding weight for the estimate periods.
5902 
5903           -- Check for zero period percent allocation.
5904           IF l_weight = 0 THEN
5905 
5906             -- For zero allocation, use uniform allocation done later in API.
5907             l_allocated := FALSE;
5908 
5909           ELSE
5910 
5911             -- Now process period allocation for the current allocation rule.
5912             FOR c_AllocPct_Rec IN c_AllocPct (c_AllocRule_Rec.allocrule_id,
5913                                               PSB_WS_Acct1.g_cy_num_periods)
5914             LOOP
5915 
5916             -- Check if period allocation is for estimate periods.
5917             IF c_AllocPct_Rec.period_num >= p_budget_periods(1).long_sequence_no
5918             THEN
5919 
5920               -- Check p_rounding_factor clause.
5921               IF p_rounding_factor IS NULL THEN
5922                 p_period_amount(c_AllocPct_Rec.period_num) := p_ytd_amount *
5923                                      nvl(c_AllocPct_Rec.percent,0) / l_weight ;
5924 
5925                 l_running_total := l_running_total +
5926                                    p_period_amount(c_AllocPct_Rec.period_num) ;
5927               ELSE
5928                 l_period_amount(c_AllocPct_Rec.period_num) :=
5929                      (p_ytd_amount * nvl(c_AllocPct_Rec.percent,0) / l_weight)
5930                      + (l_running_total - l_rounded_running_total);
5931 
5932                 l_running_total := l_running_total +
5933                     (p_ytd_amount * nvl(c_AllocPct_Rec.percent,0) / l_weight) ;
5934 
5935                 IF nvl(c_AllocPct_Rec.percent, 0) <> 0 THEN
5936                   p_period_amount(c_AllocPct_Rec.period_num) :=
5937                             Round(l_period_amount(c_AllocPct_Rec.period_num)) ;
5938                 ELSE
5939                   p_period_amount(c_AllocPct_Rec.period_num) := 0 ;
5940                 END IF;
5941 
5942                 l_rounded_running_total := l_rounded_running_total +
5943                                    p_period_amount(c_AllocPct_Rec.period_num) ;
5944               END IF;
5945               -- End checking p_rounding_factor clause.
5946 
5947             END IF;
5948             -- End checking if period allocation is for estimate periods.
5949 
5950             l_allocated := TRUE;
5951 
5952             END LOOP ;
5953             -- End processing period allocation for the current allocation rule.
5954 
5955           END IF ;
5956           -- End checking for zero period percent allocation.
5957           -- Bug#3126462: End
5958 
5959 	end if;
5960 /* Bug No 2354918 End */
5961 
5962 /* Bug No 2342169 Start */
5963 	if p_rounding_factor is null then
5964 	  p_period_amount(l_num_periods) := p_period_amount(l_num_periods) + p_ytd_amount - l_running_total;
5965 	end if;
5966 /* Bug No 2342169 End */
5967 
5968       end;
5969       -- End processing allocation type 'PERCENT'.
5970       else
5971       -- Begin processing allocation type 'PROFILE'.
5972       begin
5973 
5974 	sql_alloc := 'select ';
5975 
5976 	for l_index in 1..l_num_periods loop
5977 	  sql_alloc := sql_alloc ||
5978 		      'period' || p_budget_periods(l_index).long_sequence_no || '_amount' || ', ';
5979 	end loop;
5980 
5981 	sql_alloc := substr(sql_alloc, 1, length(sql_alloc) -2) || ' ' ||
5982 		     'from PSB_WS_ACCOUNT_LINES a ' ||
5983 		    'where a.template_id is null ' ||
5984 		      'and a.position_line_id is null ' ||
5985 		      'and a.balance_type = ''' || c_AllocRule_Rec.balance_type || ''' ' ||
5986 		      'and a.code_combination_id = ' || l_mapped_ccid || ' ' ||
5987 		      'and a.currency_code = ''' || p_currency_code || ''' ' ||
5988 		      'and exists ' ||
5989 			  '(select 1 ' ||
5990 			     'from PSB_WS_LINES ' ||
5991 			    'where account_line_id = a.account_line_id ' ||
5992 			      'and worksheet_id = ' || p_worksheet_id || ') ' ||
5993 		      'and exists ' ||
5994 			  '(select 1 ' ||
5995 			     'from PSB_BUDGET_PERIODS b ' ||
5996 			    'where b.budget_calendar_id = ' || p_budget_calendar_id || ' ' ||
5997 			      'and b.budget_year_type_id = ' || c_AllocRule_Rec.budget_year_type_id || ' ' ||
5998 			      'and a.budget_year_id = b.budget_period_id)';
5999 
6000 	cur_alloc := dbms_sql.open_cursor;
6001 	dbms_sql.parse(cur_alloc, sql_alloc, dbms_sql.v7);
6002 
6003 	for l_index in 1..l_num_periods loop
6004 	  dbms_sql.define_column(cur_alloc, l_index, l_amount);
6005 	end loop;
6006 
6007 	num_alloc := dbms_sql.execute(cur_alloc);
6008 
6009 	loop
6010 
6011 	  if dbms_sql.fetch_rows(cur_alloc) = 0 then
6012 	    exit;
6013 	  end if;
6014 
6015 	  l_allocated := TRUE;
6016 
6017 	  for l_index in 1..l_num_periods loop
6018 	    dbms_sql.column_value(cur_alloc, l_index, l_period_amount(p_budget_periods(l_index).long_sequence_no));
6019 	    l_running_total := l_running_total + nvl(l_period_amount(p_budget_periods(l_index).long_sequence_no), 0);
6020 	  end loop;
6021 
6022 	end loop;
6023 
6024 	dbms_sql.close_cursor(cur_alloc);
6025 
6026 	for l_index in 1..l_num_periods loop
6027 
6028 	  if l_running_total = 0 then
6029 	    p_period_amount(p_budget_periods(l_index).long_sequence_no) := null;
6030 	  else
6031 	    p_period_amount(p_budget_periods(l_index).long_sequence_no) := p_ytd_amount *
6032 		     l_period_amount(p_budget_periods(l_index).long_sequence_no) / l_running_total;
6033 	  end if;
6034 
6035 	end loop;
6036 
6037       end;
6038       end if;
6039       -- End processing allocation type 'PROFILE'.
6040 
6041     end loop;
6042     -- End loop to process both types of allocation rules.
6043 
6044     -- Bug 3352171: The following line is added to end a condition that used to
6045     -- improve performance.
6046     end if;
6047 
6048     -- Allocate Uniformly if there are no allocation rules found : this happens only when an
6049     -- Allocation Rule Set is specified for the Worksheet but there are no Allocation Rules
6050 
6051     if not l_allocated then
6052     begin
6053 
6054 /* Bug No 2342169 Start */
6055 	if p_rounding_factor is null then
6056 	  l_perc := Round(100 / l_num_periods, 2) / 100;
6057 	end if;
6058 /* Bug No 2342169 End */
6059 
6060       for l_index in 1..l_num_periods loop
6061 /* Bug No 2342169 Start */
6062 -- Commented the following 1 line and added the other 4 lines
6063 --        p_period_amount(p_budget_periods(l_index).long_sequence_no) := p_ytd_amount / l_num_periods;
6064 
6065 	if p_rounding_factor is null then
6066 	  p_period_amount(p_budget_periods(l_index).long_sequence_no) := p_ytd_amount * l_perc;
6067 
6068 	  l_running_total := l_running_total + p_period_amount(p_budget_periods(l_index).long_sequence_no);
6069 	else
6070 	  l_period_amount(p_budget_periods(l_index).long_sequence_no) := (p_ytd_amount / l_num_periods)
6071 							   + (l_running_total - l_rounded_running_total);
6072 
6073 	  l_running_total := l_running_total + (p_ytd_amount / l_num_periods);
6074 
6075 	  p_period_amount(p_budget_periods(l_index).long_sequence_no) := Round(l_period_amount(p_budget_periods(l_index).long_sequence_no));
6076 
6077 	  l_rounded_running_total := l_rounded_running_total + p_period_amount(p_budget_periods(l_index).long_sequence_no);
6078 
6079 	end if;
6080 /* Bug No 2342169 End */
6081 
6082       end loop;
6083 
6084 /* Bug No 2342169 Start */
6085 	if p_rounding_factor is null then
6086 	  l_rounding_difference := p_ytd_amount - l_running_total;
6087 
6088 	  p_period_amount(p_budget_periods(l_num_periods).long_sequence_no) := p_period_amount(p_budget_periods(l_num_periods).long_sequence_no)
6089 											+ l_rounding_difference;
6090 	end if;
6091 /* Bug No 2342169 End */
6092 
6093     end;
6094     end if;
6095 
6096 
6097   end;
6098   end if;
6099 
6100 
6101   -- Initialize API return status to success
6102 
6103   p_return_status := FND_API.G_RET_STS_SUCCESS;
6104 
6105 
6106 EXCEPTION
6107 
6108    when FND_API.G_EXC_ERROR then
6109      p_return_status := FND_API.G_RET_STS_ERROR;
6110 
6111    when FND_API.G_EXC_UNEXPECTED_ERROR then
6112      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6113 
6114    when OTHERS then
6115      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6116      if FND_MSG_PUB.Check_Msg_Level
6117        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
6118      then
6119        FND_MSG_PUB.Add_Exc_Msg
6120 	  (p_pkg_name => G_PKG_NAME,
6121 	   p_procedure_name => 'Distribute_Account_Lines');
6122      end if;
6123 
6124 END Distribute_Account_Lines;
6125 
6126 /* ----------------------------------------------------------------------- */
6127 
6128 -- Redistribute existing period amounts for an Account Line. Redistribution
6129 -- is done by prorating the ratios of the new YTD Amount and the old YTD
6130 -- Amount, e.g new period1_amount = current period1_amount * p_new_ytd_amount /
6131 -- p_old_ytd_amount
6132 
6133 PROCEDURE Distribute_Account_Lines
6134 ( p_return_status       OUT  NOCOPY  VARCHAR2,
6135   p_worksheet_id        IN   NUMBER,
6136   p_service_package_id  IN   NUMBER,
6137   p_stage_set_id        IN   NUMBER,
6138   p_current_stage_seq   IN   NUMBER,
6139   p_account_line_id     IN   NUMBER,
6140   p_rounding_factor     IN   NUMBER,
6141   p_old_ytd_amount      IN   NUMBER,
6142   p_new_ytd_amount      IN   NUMBER,
6143   -- Bug#3128597: Support prorated allocation during annual amount updation
6144   p_cy_ytd_amount       IN   NUMBER := NULL,
6145   -- Bug#3128597: End
6146   p_budget_group_id     IN   NUMBER := FND_API.G_MISS_NUM
6147 ) IS
6148 
6149   l_ytd_amount          NUMBER;
6150   l_period_amount       PSB_WS_ACCT1.g_prdamt_tbl_type;
6151   l_init_index          PLS_INTEGER;
6152 
6153 /* Bug No 2354918 Start */
6154   l_running_total       NUMBER := 0;
6155   l_budget_year_id      NUMBER;
6156   l_last_period_index   NUMBER;
6157 /* Bug No 2354918 End */
6158 
6159   -- Bug#3128597: Support prorated allocation during annual amount updation
6160   l_old_ytd_amount      NUMBER;
6161 
6162   l_return_status       VARCHAR2(1);
6163 
6164   cursor c_WAL is
6165     select period1_amount, period2_amount, period3_amount, period4_amount,
6166 	   period5_amount, period6_amount, period7_amount, period8_amount,
6167 	   period9_amount, period10_amount, period11_amount, period12_amount,
6168 	   period13_amount, period14_amount, period15_amount, period16_amount,
6169 	   period17_amount, period18_amount, period19_amount, period20_amount,
6170 	   period21_amount, period22_amount, period23_amount, period24_amount,
6171 	   period25_amount, period26_amount, period27_amount, period28_amount,
6172 	   period29_amount, period30_amount, period31_amount, period32_amount,
6173 	   period33_amount, period34_amount, period35_amount, period36_amount,
6174 	   period37_amount, period38_amount, period39_amount, period40_amount,
6175 	   period41_amount, period42_amount, period43_amount, period44_amount,
6176 	   period45_amount, period46_amount, period47_amount, period48_amount,
6177 	   period49_amount, period50_amount, period51_amount, period52_amount,
6178 	   period53_amount, period54_amount, period55_amount, period56_amount,
6179 	   period57_amount, period58_amount, period59_amount, period60_amount,
6180 /* Bug No 2354918 Start */
6181 	   budget_year_id
6182 /* Bug No 2354918 End */
6183       from PSB_WS_ACCOUNT_LINES
6184      where account_line_id = p_account_line_id;
6185 
6186 BEGIN
6187 
6188   -- Bug#3128597: Support prorated allocation during annual amount updation
6189   -- For CY, we need to consider only estimate periods and balances for
6190   -- proration. Note for PP years, p_cy_ytd_amount will always be 0.
6191   l_old_ytd_amount := p_old_ytd_amount - p_cy_ytd_amount ;
6192   -- Bug#3128597: End
6193 
6194   for l_init_index in 1..PSB_WS_ACCT1.g_max_num_amounts loop
6195     l_period_amount(l_init_index) := null;
6196   end loop;
6197 
6198 /* Bug No 2354918 Start */
6199 -- Added the logic for adjusting the rounding difference
6200 
6201   if p_rounding_factor is null then
6202   begin
6203 
6204     l_ytd_amount := p_new_ytd_amount;
6205 
6206     for c_WAL_Rec in c_WAL loop
6207 /* Bug No 2354918 Start */
6208       l_budget_year_id := c_WAL_Rec.budget_year_id;
6209 
6210       for l_year_index in 1..PSB_WS_ACCT1.g_num_budget_years loop
6211       if PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id = l_budget_year_id then
6212 
6213 	l_last_period_index := PSB_WS_ACCT1.g_budget_years(l_year_index).last_period_index;
6214 
6215       end if;
6216       end loop;
6217 /* Bug No 2354918 End */
6218 
6219       if c_WAL_Rec.period1_amount is null then
6220 	l_period_amount(1) := null;
6221       else
6222 	if 1 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6223 	l_period_amount(1) := c_WAL_Rec.period1_amount +
6224 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period1_amount / l_old_ytd_amount);
6225 	else
6226 	l_period_amount(1) := c_WAL_Rec.period1_amount;
6227 	end if;
6228       end if;
6229 
6230       if c_WAL_Rec.period2_amount is null then
6231 	l_period_amount(2) := null;
6232       else
6233 	if 2 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6234 	l_period_amount(2) := c_WAL_Rec.period2_amount +
6235 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period2_amount / l_old_ytd_amount);
6236 	else
6237 	l_period_amount(2) := c_WAL_Rec.period2_amount;
6238 	end if;
6239       end if;
6240 
6241       if c_WAL_Rec.period3_amount is null then
6242 	l_period_amount(3) := null;
6243       else
6244 	if 3 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6245 	l_period_amount(3) := c_WAL_Rec.period3_amount +
6246 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period3_amount / l_old_ytd_amount);
6247 	else
6248 	l_period_amount(3) := c_WAL_Rec.period3_amount;
6249 	end if;
6250       end if;
6251 
6252       if c_WAL_Rec.period4_amount is null then
6253 	l_period_amount(4) := null;
6254       else
6255 	if 4 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6256 	l_period_amount(4) := c_WAL_Rec.period4_amount +
6257 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period4_amount / l_old_ytd_amount);
6258 	else
6259 	l_period_amount(4) := c_WAL_Rec.period4_amount;
6260 	end if;
6261       end if;
6262 
6263       if c_WAL_Rec.period5_amount is null then
6264 	l_period_amount(5) := null;
6265       else
6266 	if 5 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6267 	l_period_amount(5) := c_WAL_Rec.period5_amount +
6268 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period5_amount / l_old_ytd_amount);
6269 	else
6270 	l_period_amount(5) := c_WAL_Rec.period5_amount;
6271 	end if;
6272       end if;
6273 
6274       if c_WAL_Rec.period6_amount is null then
6275 	l_period_amount(6) := null;
6276       else
6277 	if 6 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6278 	l_period_amount(6) := c_WAL_Rec.period6_amount +
6279 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period6_amount / l_old_ytd_amount);
6280 	else
6281 	l_period_amount(6) := c_WAL_Rec.period6_amount;
6282 	end if;
6283       end if;
6284 
6285       if c_WAL_Rec.period7_amount is null then
6286 	l_period_amount(7) := null;
6287       else
6288 	if 7 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6289 	l_period_amount(7) := c_WAL_Rec.period7_amount +
6290 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period7_amount / l_old_ytd_amount);
6291 	else
6292 	l_period_amount(7) := c_WAL_Rec.period7_amount;
6293 	end if;
6294       end if;
6295 
6296       if c_WAL_Rec.period8_amount is null then
6297 	l_period_amount(8) := null;
6298       else
6299 	if 8 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6300 	l_period_amount(8) := c_WAL_Rec.period8_amount +
6301 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period8_amount / l_old_ytd_amount);
6302 	else
6303 	l_period_amount(8) := c_WAL_Rec.period8_amount;
6304 	end if;
6305       end if;
6306 
6307       if c_WAL_Rec.period9_amount is null then
6308 	l_period_amount(9) := null;
6309       else
6310 	if 9 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6311 	l_period_amount(9) := c_WAL_Rec.period9_amount +
6312 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period9_amount / l_old_ytd_amount);
6313 	else
6314 	l_period_amount(9) := c_WAL_Rec.period9_amount;
6315 	end if;
6316       end if;
6317 
6318       if c_WAL_Rec.period10_amount is null then
6319 	l_period_amount(10) := null;
6320       else
6321 	if 10 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6322 	l_period_amount(10) := c_WAL_Rec.period10_amount +
6323 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period10_amount / l_old_ytd_amount);
6324 	else
6325 	l_period_amount(10) := c_WAL_Rec.period10_amount;
6326 	end if;
6327       end if;
6328 
6329       if c_WAL_Rec.period11_amount is null then
6330 	l_period_amount(11) := null;
6331       else
6332 	if 11 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6333 	l_period_amount(11) := c_WAL_Rec.period11_amount +
6334 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period11_amount / l_old_ytd_amount);
6335 	else
6336 	l_period_amount(11) := c_WAL_Rec.period11_amount;
6337 	end if;
6338       end if;
6339 
6340       if c_WAL_Rec.period12_amount is null then
6341 	l_period_amount(12) := null;
6342       else
6343 	if 12 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6344 	l_period_amount(12) := c_WAL_Rec.period12_amount +
6345 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period12_amount / l_old_ytd_amount);
6346 	else
6347 	l_period_amount(12) := c_WAL_Rec.period12_amount;
6348 	end if;
6349       end if;
6350 
6351       if c_WAL_Rec.period13_amount is null then
6352 	l_period_amount(13) := null;
6353       else
6354 	if 13 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6355 	l_period_amount(13) := c_WAL_Rec.period13_amount +
6356 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period13_amount / l_old_ytd_amount);
6357 	else
6358 	l_period_amount(13) := c_WAL_Rec.period13_amount;
6359 	end if;
6360       end if;
6361 
6362       if c_WAL_Rec.period14_amount is null then
6363 	l_period_amount(14) := null;
6364       else
6365 	if 14 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6366 	l_period_amount(14) := c_WAL_Rec.period14_amount +
6367 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period14_amount / l_old_ytd_amount);
6368 	else
6369 	l_period_amount(14) := c_WAL_Rec.period14_amount;
6370 	end if;
6371       end if;
6372 
6373       if c_WAL_Rec.period15_amount is null then
6374 	l_period_amount(15) := null;
6375       else
6376 	if 15 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6377 	l_period_amount(15) := c_WAL_Rec.period15_amount +
6378 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period15_amount / l_old_ytd_amount);
6379 	else
6380 	l_period_amount(15) := c_WAL_Rec.period15_amount;
6381 	end if;
6382       end if;
6383 
6384       if c_WAL_Rec.period16_amount is null then
6385 	l_period_amount(16) := null;
6386       else
6387 	if 16 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6388 	l_period_amount(16) := c_WAL_Rec.period16_amount +
6389 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period16_amount / l_old_ytd_amount);
6390 	else
6391 	l_period_amount(16) := c_WAL_Rec.period16_amount;
6392 	end if;
6393       end if;
6394 
6395       if c_WAL_Rec.period17_amount is null then
6396 	l_period_amount(17) := null;
6397       else
6398 	if 17 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6399 	l_period_amount(17) := c_WAL_Rec.period17_amount +
6400 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period17_amount / l_old_ytd_amount);
6401 	else
6402 	l_period_amount(17) := c_WAL_Rec.period17_amount;
6403 	end if;
6404       end if;
6405 
6406       if c_WAL_Rec.period18_amount is null then
6407 	l_period_amount(18) := null;
6408       else
6409 	if 18 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6410 	l_period_amount(18) := c_WAL_Rec.period18_amount +
6411 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period18_amount / l_old_ytd_amount);
6412 	else
6413 	l_period_amount(18) := c_WAL_Rec.period18_amount;
6414 	end if;
6415       end if;
6416 
6417       if c_WAL_Rec.period19_amount is null then
6418 	l_period_amount(19) := null;
6419       else
6420 	if 19 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6421 	l_period_amount(19) := c_WAL_Rec.period19_amount +
6422 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period19_amount / l_old_ytd_amount);
6423 	else
6424 	l_period_amount(19) := c_WAL_Rec.period19_amount;
6425 	end if;
6426       end if;
6427 
6428       if c_WAL_Rec.period20_amount is null then
6429 	l_period_amount(20) := null;
6430       else
6431 	if 20 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6432 	l_period_amount(20) := c_WAL_Rec.period20_amount +
6433 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period20_amount / l_old_ytd_amount);
6434 	else
6435 	l_period_amount(20) := c_WAL_Rec.period20_amount;
6436 	end if;
6437       end if;
6438 
6439       if c_WAL_Rec.period21_amount is null then
6440 	l_period_amount(21) := null;
6441       else
6442 	if 21 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6443 	l_period_amount(21) := c_WAL_Rec.period21_amount +
6444 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period21_amount / l_old_ytd_amount);
6445 	else
6446 	l_period_amount(21) := c_WAL_Rec.period21_amount;
6447 	end if;
6448       end if;
6449 
6450       if c_WAL_Rec.period22_amount is null then
6451 	l_period_amount(22) := null;
6452       else
6453 	if 22 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6454 	l_period_amount(22) := c_WAL_Rec.period22_amount +
6455 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period22_amount / l_old_ytd_amount);
6456 	else
6457 	l_period_amount(22) := c_WAL_Rec.period22_amount;
6458 	end if;
6459       end if;
6460 
6461       if c_WAL_Rec.period23_amount is null then
6462 	l_period_amount(23) := null;
6463       else
6464 	if 23 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6465 	l_period_amount(23) := c_WAL_Rec.period23_amount +
6466 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period23_amount / l_old_ytd_amount);
6467 	else
6468 	l_period_amount(23) := c_WAL_Rec.period23_amount;
6469 	end if;
6470       end if;
6471 
6472       if c_WAL_Rec.period24_amount is null then
6473 	l_period_amount(24) := null;
6474       else
6475 	if 24 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6476 	l_period_amount(24) := c_WAL_Rec.period24_amount +
6477 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period24_amount / l_old_ytd_amount);
6478 	else
6479 	l_period_amount(24) := c_WAL_Rec.period24_amount;
6480 	end if;
6481       end if;
6482 
6483       if c_WAL_Rec.period25_amount is null then
6484 	l_period_amount(25) := null;
6485       else
6486 	if 25 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6487 	l_period_amount(25) := c_WAL_Rec.period25_amount +
6488 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period25_amount / l_old_ytd_amount);
6489 	else
6490 	l_period_amount(25) := c_WAL_Rec.period25_amount;
6491 	end if;
6492       end if;
6493 
6494       if c_WAL_Rec.period26_amount is null then
6495 	l_period_amount(26) := null;
6496       else
6497 	if 26 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6498 	l_period_amount(26) := c_WAL_Rec.period26_amount +
6499 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period26_amount / l_old_ytd_amount);
6500 	else
6501 	l_period_amount(26) := c_WAL_Rec.period26_amount;
6502 	end if;
6503       end if;
6504 
6505       if c_WAL_Rec.period27_amount is null then
6506 	l_period_amount(27) := null;
6507       else
6508 	if 27 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6509 	l_period_amount(27) := c_WAL_Rec.period27_amount +
6510 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period27_amount / l_old_ytd_amount);
6511 	else
6512 	l_period_amount(27) := c_WAL_Rec.period27_amount;
6513 	end if;
6514       end if;
6515 
6516       if c_WAL_Rec.period28_amount is null then
6517 	l_period_amount(28) := null;
6518       else
6519 	if 28 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6520 	l_period_amount(28) := c_WAL_Rec.period28_amount +
6521 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period28_amount / l_old_ytd_amount);
6522 	else
6523 	l_period_amount(28) := c_WAL_Rec.period28_amount;
6524 	end if;
6525       end if;
6526 
6527       if c_WAL_Rec.period29_amount is null then
6528 	l_period_amount(29) := null;
6529       else
6530 	if 29 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6531 	l_period_amount(29) := c_WAL_Rec.period29_amount +
6532 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period29_amount / l_old_ytd_amount);
6533 	else
6534 	l_period_amount(29) := c_WAL_Rec.period29_amount;
6535 	end if;
6536       end if;
6537 
6538       if c_WAL_Rec.period30_amount is null then
6539 	l_period_amount(30) := null;
6540       else
6541 	if 30 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6542 	l_period_amount(30) := c_WAL_Rec.period30_amount +
6543 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period30_amount / l_old_ytd_amount);
6544 	else
6545 	l_period_amount(30) := c_WAL_Rec.period30_amount;
6546 	end if;
6547       end if;
6548 
6549       if c_WAL_Rec.period31_amount is null then
6550 	l_period_amount(31) := null;
6551       else
6552 	if 31 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6553 	l_period_amount(31) := c_WAL_Rec.period31_amount +
6554 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period31_amount / l_old_ytd_amount);
6555 	else
6556 	l_period_amount(31) := c_WAL_Rec.period31_amount;
6557 	end if;
6558       end if;
6559 
6560       if c_WAL_Rec.period32_amount is null then
6561 	l_period_amount(32) := null;
6562       else
6563 	if 32 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6564 	l_period_amount(32) := c_WAL_Rec.period32_amount +
6565 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period32_amount / l_old_ytd_amount);
6566 	else
6567 	l_period_amount(32) := c_WAL_Rec.period32_amount;
6568 	end if;
6569       end if;
6570 
6571       if c_WAL_Rec.period33_amount is null then
6572 	l_period_amount(33) := null;
6573       else
6574 	if 33 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6575 	l_period_amount(33) := c_WAL_Rec.period33_amount +
6576 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period33_amount / l_old_ytd_amount);
6577 	else
6578 	l_period_amount(33) := c_WAL_Rec.period33_amount;
6579 	end if;
6580       end if;
6581 
6582       if c_WAL_Rec.period34_amount is null then
6583 	l_period_amount(34) := null;
6584       else
6585 	if 34 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6586 	l_period_amount(34) := c_WAL_Rec.period34_amount +
6587 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period34_amount / l_old_ytd_amount);
6588 	else
6589 	l_period_amount(34) := c_WAL_Rec.period34_amount;
6590 	end if;
6591       end if;
6592 
6593       if c_WAL_Rec.period35_amount is null then
6594 	l_period_amount(35) := null;
6595       else
6596 	if 35 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6597 	l_period_amount(35) := c_WAL_Rec.period35_amount +
6598 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period35_amount / l_old_ytd_amount);
6599 	else
6600 	l_period_amount(35) := c_WAL_Rec.period35_amount;
6601 	end if;
6602       end if;
6603 
6604       if c_WAL_Rec.period36_amount is null then
6605 	l_period_amount(36) := null;
6606       else
6607 	if 36 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6608 	l_period_amount(36) := c_WAL_Rec.period36_amount +
6609 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period36_amount / l_old_ytd_amount);
6610 	else
6611 	l_period_amount(36) := c_WAL_Rec.period36_amount;
6612 	end if;
6613       end if;
6614 
6615       if c_WAL_Rec.period37_amount is null then
6616 	l_period_amount(37) := null;
6617       else
6618 	if 37 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6619 	l_period_amount(37) := c_WAL_Rec.period37_amount +
6620 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period37_amount / l_old_ytd_amount);
6621 	else
6622 	l_period_amount(37) := c_WAL_Rec.period37_amount;
6623 	end if;
6624       end if;
6625 
6626       if c_WAL_Rec.period38_amount is null then
6627 	l_period_amount(38) := null;
6628       else
6629 	if 38 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6630 	l_period_amount(38) := c_WAL_Rec.period38_amount +
6631 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period38_amount / l_old_ytd_amount);
6632 	else
6633 	l_period_amount(38) := c_WAL_Rec.period38_amount;
6634 	end if;
6635       end if;
6636 
6637       if c_WAL_Rec.period39_amount is null then
6638 	l_period_amount(39) := null;
6639       else
6640 	if 39 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6641 	l_period_amount(39) := c_WAL_Rec.period39_amount +
6642 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period39_amount / l_old_ytd_amount);
6643 	else
6644 	l_period_amount(39) := c_WAL_Rec.period39_amount;
6645 	end if;
6646       end if;
6647 
6648       if c_WAL_Rec.period40_amount is null then
6649 	l_period_amount(40) := null;
6650       else
6651 	if 40 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6652 	l_period_amount(40) := c_WAL_Rec.period40_amount +
6653 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period40_amount / l_old_ytd_amount);
6654 	else
6655 	l_period_amount(40) := c_WAL_Rec.period40_amount;
6656 	end if;
6657       end if;
6658 
6659       if c_WAL_Rec.period41_amount is null then
6660 	l_period_amount(41) := null;
6661       else
6662 	if 41 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6663 	l_period_amount(41) := c_WAL_Rec.period41_amount +
6664 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period41_amount / l_old_ytd_amount);
6665 	else
6666 	l_period_amount(41) := c_WAL_Rec.period41_amount;
6667 	end if;
6668       end if;
6669 
6670       if c_WAL_Rec.period42_amount is null then
6671 	l_period_amount(42) := null;
6672       else
6673 	if 42 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6674 	l_period_amount(42) := c_WAL_Rec.period42_amount +
6675 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period42_amount / l_old_ytd_amount);
6676 	else
6677 	l_period_amount(42) := c_WAL_Rec.period42_amount;
6678 	end if;
6679       end if;
6680 
6681       if c_WAL_Rec.period43_amount is null then
6682 	l_period_amount(43) := null;
6683       else
6684 	if 43 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6685 	l_period_amount(43) := c_WAL_Rec.period43_amount +
6686 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period43_amount / l_old_ytd_amount);
6687 	else
6688 	l_period_amount(43) := c_WAL_Rec.period43_amount;
6689 	end if;
6690       end if;
6691 
6692       if c_WAL_Rec.period44_amount is null then
6693 	l_period_amount(44) := null;
6694       else
6695 	if 44 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6696 	l_period_amount(44) := c_WAL_Rec.period44_amount +
6697 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period44_amount / l_old_ytd_amount);
6698 	else
6699 	l_period_amount(44) := c_WAL_Rec.period44_amount;
6700 	end if;
6701       end if;
6702 
6703       if c_WAL_Rec.period45_amount is null then
6704 	l_period_amount(45) := null;
6705       else
6706 	if 45 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6707 	l_period_amount(45) := c_WAL_Rec.period45_amount +
6708 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period45_amount / l_old_ytd_amount);
6709 	else
6710 	l_period_amount(45) := c_WAL_Rec.period45_amount;
6711 	end if;
6712       end if;
6713 
6714       if c_WAL_Rec.period46_amount is null then
6715 	l_period_amount(46) := null;
6716       else
6717 	if 46 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6718 	l_period_amount(46) := c_WAL_Rec.period46_amount +
6719 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period46_amount / l_old_ytd_amount);
6720 	else
6721 	l_period_amount(46) := c_WAL_Rec.period46_amount;
6722 	end if;
6723       end if;
6724 
6725       if c_WAL_Rec.period47_amount is null then
6726 	l_period_amount(47) := null;
6727       else
6728 	if 47 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6729 	l_period_amount(47) := c_WAL_Rec.period47_amount +
6730 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period47_amount / l_old_ytd_amount);
6731 	else
6732 	l_period_amount(47) := c_WAL_Rec.period47_amount;
6733 	end if;
6734       end if;
6735 
6736       if c_WAL_Rec.period48_amount is null then
6737 	l_period_amount(48) := null;
6738       else
6739 	if 48 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6740 	l_period_amount(48) := c_WAL_Rec.period48_amount +
6741 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period48_amount / l_old_ytd_amount);
6742 	else
6743 	l_period_amount(48) := c_WAL_Rec.period48_amount;
6744 	end if;
6745       end if;
6746 
6747       if c_WAL_Rec.period49_amount is null then
6748 	l_period_amount(49) := null;
6749       else
6750 	if 49 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6751 	l_period_amount(49) := c_WAL_Rec.period49_amount +
6752 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period49_amount / l_old_ytd_amount);
6753 	else
6754 	l_period_amount(49) := c_WAL_Rec.period49_amount;
6755 	end if;
6756       end if;
6757 
6758       if c_WAL_Rec.period50_amount is null then
6759 	l_period_amount(50) := null;
6760       else
6761 	if 50 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6762 	l_period_amount(50) := c_WAL_Rec.period50_amount +
6763 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period50_amount / l_old_ytd_amount);
6764 	else
6765 	l_period_amount(50) := c_WAL_Rec.period50_amount;
6766 	end if;
6767       end if;
6768 
6769       if c_WAL_Rec.period51_amount is null then
6770 	l_period_amount(51) := null;
6771       else
6772 	if 51 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6773 	l_period_amount(51) := c_WAL_Rec.period51_amount +
6774 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period51_amount / l_old_ytd_amount);
6775 	else
6776 	l_period_amount(51) := c_WAL_Rec.period51_amount;
6777 	end if;
6778       end if;
6779 
6780       if c_WAL_Rec.period52_amount is null then
6781 	l_period_amount(52) := null;
6782       else
6783 	if 52 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6784 	l_period_amount(52) := c_WAL_Rec.period52_amount +
6785 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period52_amount / l_old_ytd_amount);
6786 	else
6787 	l_period_amount(52) := c_WAL_Rec.period52_amount;
6788 	end if;
6789       end if;
6790 
6791       if c_WAL_Rec.period53_amount is null then
6792 	l_period_amount(53) := null;
6793       else
6794 	if 53 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6795 	l_period_amount(53) := c_WAL_Rec.period53_amount +
6796 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period53_amount / l_old_ytd_amount);
6797 	else
6798 	l_period_amount(53) := c_WAL_Rec.period53_amount;
6799 	end if;
6800       end if;
6801 
6802       if c_WAL_Rec.period54_amount is null then
6803 	l_period_amount(54) := null;
6804       else
6805 	if 54 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6806 	l_period_amount(54) := c_WAL_Rec.period54_amount +
6807 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period54_amount / l_old_ytd_amount);
6808 	else
6809 	l_period_amount(54) := c_WAL_Rec.period54_amount;
6810 	end if;
6811       end if;
6812 
6813       if c_WAL_Rec.period55_amount is null then
6814 	l_period_amount(55) := null;
6815       else
6816 	if 55 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6817 	l_period_amount(55) := c_WAL_Rec.period55_amount +
6818 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period55_amount / l_old_ytd_amount);
6819 	else
6820 	l_period_amount(55) := c_WAL_Rec.period55_amount;
6821 	end if;
6822       end if;
6823 
6824       if c_WAL_Rec.period56_amount is null then
6825 	l_period_amount(56) := null;
6826       else
6827 	if 56 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6828 	l_period_amount(56) := c_WAL_Rec.period56_amount +
6829 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period56_amount / l_old_ytd_amount);
6830 	else
6831 	l_period_amount(56) := c_WAL_Rec.period56_amount;
6832 	end if;
6833       end if;
6834 
6835       if c_WAL_Rec.period57_amount is null then
6836 	l_period_amount(57) := null;
6837       else
6838 	if 57 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6839 	l_period_amount(57) := c_WAL_Rec.period57_amount +
6840 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period57_amount / l_old_ytd_amount);
6841 	else
6842 	l_period_amount(57) := c_WAL_Rec.period57_amount;
6843 	end if;
6844       end if;
6845 
6846       if c_WAL_Rec.period58_amount is null then
6847 	l_period_amount(58) := null;
6848       else
6849 	if 58 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6850 	l_period_amount(58) := c_WAL_Rec.period58_amount +
6851 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period58_amount / l_old_ytd_amount);
6852 	else
6853 	l_period_amount(58) := c_WAL_Rec.period58_amount;
6854 	end if;
6855       end if;
6856 
6857       if c_WAL_Rec.period59_amount is null then
6858 	l_period_amount(59) := null;
6859       else
6860 	if 59 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6861 	l_period_amount(59) := c_WAL_Rec.period59_amount +
6862 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period59_amount / l_old_ytd_amount);
6863 	else
6864 	l_period_amount(59) := c_WAL_Rec.period59_amount;
6865 	end if;
6866       end if;
6867 
6868       if c_WAL_Rec.period60_amount is null then
6869 	l_period_amount(60) := null;
6870       else
6871 	if 60 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6872 	l_period_amount(60) := c_WAL_Rec.period60_amount +
6873 			     ((p_new_ytd_amount - p_old_ytd_amount) * c_WAL_Rec.period60_amount / l_old_ytd_amount);
6874 	else
6875 	l_period_amount(60) := c_WAL_Rec.period60_amount;
6876 	end if;
6877       end if;
6878 
6879       l_running_total := l_running_total
6880 		+ nvl(l_period_amount(1),0)   + nvl(l_period_amount(2),0)   + nvl(l_period_amount(3),0)   + nvl(l_period_amount(4),0)   + nvl(l_period_amount(5),0)
6881 		+ nvl(l_period_amount(6),0)   + nvl(l_period_amount(7),0)   + nvl(l_period_amount(8),0)   + nvl(l_period_amount(9),0)   + nvl(l_period_amount(10),0)
6882 		+ nvl(l_period_amount(11),0)  + nvl(l_period_amount(12),0)  + nvl(l_period_amount(13),0)  + nvl(l_period_amount(14),0)  + nvl(l_period_amount(15),0)
6883 		+ nvl(l_period_amount(16),0)  + nvl(l_period_amount(17),0)  + nvl(l_period_amount(18),0)  + nvl(l_period_amount(19),0)  + nvl(l_period_amount(20),0)
6884 		+ nvl(l_period_amount(21),0)  + nvl(l_period_amount(22),0)  + nvl(l_period_amount(23),0)  + nvl(l_period_amount(24),0)  + nvl(l_period_amount(25),0)
6885 		+ nvl(l_period_amount(26),0)  + nvl(l_period_amount(27),0)  + nvl(l_period_amount(28),0)  + nvl(l_period_amount(29),0)  + nvl(l_period_amount(30),0)
6886 		+ nvl(l_period_amount(31),0)  + nvl(l_period_amount(32),0)  + nvl(l_period_amount(33),0)  + nvl(l_period_amount(34),0)  + nvl(l_period_amount(35),0)
6887 		+ nvl(l_period_amount(36),0)  + nvl(l_period_amount(37),0)  + nvl(l_period_amount(38),0)  + nvl(l_period_amount(39),0)  + nvl(l_period_amount(40),0)
6888 		+ nvl(l_period_amount(41),0)  + nvl(l_period_amount(42),0)  + nvl(l_period_amount(43),0)  + nvl(l_period_amount(44),0)  + nvl(l_period_amount(45),0)
6889 		+ nvl(l_period_amount(46),0)  + nvl(l_period_amount(47),0)  + nvl(l_period_amount(48),0)  + nvl(l_period_amount(49),0)  + nvl(l_period_amount(50),0)
6890 		+ nvl(l_period_amount(51),0)  + nvl(l_period_amount(52),0)  + nvl(l_period_amount(53),0)  + nvl(l_period_amount(54),0)  + nvl(l_period_amount(55),0)
6891 		+ nvl(l_period_amount(56),0)  + nvl(l_period_amount(57),0)  + nvl(l_period_amount(58),0)  + nvl(l_period_amount(59),0)  + nvl(l_period_amount(60),0);
6892 
6893     end loop;
6894 
6895     /* Bug 3133240 Start */
6896     --Added the IF condition
6897 
6898     IF l_last_period_index > 0 THEN
6899     	l_period_amount(l_last_period_index) := l_period_amount(l_last_period_index) + p_new_ytd_amount - l_running_total;
6900     END IF;
6901 
6902     /* Bug 3133240 End */
6903 
6904   end;
6905   else
6906   begin
6907 
6908     l_ytd_amount := ROUND(p_new_ytd_amount/p_rounding_factor) * p_rounding_factor;
6909 
6910     for c_WAL_Rec in c_WAL loop
6911 
6912 /* Bug No 2354918 Start */
6913       l_budget_year_id := c_WAL_Rec.budget_year_id;
6914 
6915       for l_year_index in 1..PSB_WS_ACCT1.g_num_budget_years loop
6916       if PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id = l_budget_year_id then
6917 
6918 	l_last_period_index := PSB_WS_ACCT1.g_budget_years(l_year_index).last_period_index;
6919 
6920       end if;
6921       end loop;
6922 /* Bug No 2354918 End */
6923 
6924       if c_WAL_Rec.period1_amount is null then
6925 	l_period_amount(1) := null;
6926       else
6927 	if 1 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6928 	l_period_amount(1) := ROUND((c_WAL_Rec.period1_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
6929 				     c_WAL_Rec.Period1_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
6930 	else
6931 	l_period_amount(1) := c_WAL_Rec.period1_amount;
6932 	end if;
6933       end if;
6934 
6935       if c_WAL_Rec.period2_amount is null then
6936 	l_period_amount(2) := null;
6937       else
6938 	if 2 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6939 	l_period_amount(2) := ROUND((c_WAL_Rec.period2_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
6940 				     c_WAL_Rec.Period2_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
6941 	else
6942 	l_period_amount(2) := c_WAL_Rec.period2_amount;
6943 	end if;
6944       end if;
6945 
6946       if c_WAL_Rec.period3_amount is null then
6947 	l_period_amount(3) := null;
6948       else
6949 	if 3 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6950 	l_period_amount(3) := ROUND((c_WAL_Rec.period3_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
6951 				     c_WAL_Rec.Period3_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
6952 	else
6953 	l_period_amount(3) := c_WAL_Rec.period3_amount;
6954 	end if;
6955       end if;
6956 
6957       if c_WAL_Rec.period4_amount is null then
6958 	l_period_amount(4) := null;
6959       else
6960 	if 4 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6961 	l_period_amount(4) := ROUND((c_WAL_Rec.period4_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
6962 				     c_WAL_Rec.Period4_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
6963 	else
6964 	l_period_amount(4) := c_WAL_Rec.period4_amount;
6965 	end if;
6966       end if;
6967 
6968       if c_WAL_Rec.period5_amount is null then
6969 	l_period_amount(5) := null;
6970       else
6971 	if 5 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6972 	l_period_amount(5) := ROUND((c_WAL_Rec.period5_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
6973 				     c_WAL_Rec.Period5_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
6974 	else
6975 	l_period_amount(5) := c_WAL_Rec.period5_amount;
6976 	end if;
6977       end if;
6978 
6979       if c_WAL_Rec.period6_amount is null then
6980 	l_period_amount(6) := null;
6981       else
6982 	if 6 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6983 	l_period_amount(6) := ROUND((c_WAL_Rec.period6_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
6984 				     c_WAL_Rec.Period6_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
6985 	else
6986 	l_period_amount(6) := c_WAL_Rec.period6_amount;
6987 	end if;
6988       end if;
6989 
6990       if c_WAL_Rec.period7_amount is null then
6991 	l_period_amount(7) := null;
6992       else
6993 	if 7 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
6994 	l_period_amount(7) := ROUND((c_WAL_Rec.period7_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
6995 				     c_WAL_Rec.Period7_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
6996 	else
6997 	l_period_amount(7) := c_WAL_Rec.period7_amount;
6998 	end if;
6999       end if;
7000 
7001       if c_WAL_Rec.period8_amount is null then
7002 	l_period_amount(8) := null;
7003       else
7004 	if 8 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7005 	l_period_amount(8) := ROUND((c_WAL_Rec.period8_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7006 				     c_WAL_Rec.Period8_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7007 	else
7008 	l_period_amount(8) := c_WAL_Rec.period8_amount;
7009 	end if;
7010       end if;
7011 
7012       if c_WAL_Rec.period9_amount is null then
7013 	l_period_amount(9) := null;
7014       else
7015 	if 9 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7016 	l_period_amount(9) := ROUND((c_WAL_Rec.period9_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7017 				     c_WAL_Rec.Period9_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7018 	else
7019 	l_period_amount(9) := c_WAL_Rec.period9_amount;
7020 	end if;
7021       end if;
7022 
7023       if c_WAL_Rec.period10_amount is null then
7024 	l_period_amount(10) := null;
7025       else
7026 	if 10 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7027 	l_period_amount(10) := ROUND((c_WAL_Rec.period10_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7028 				      c_WAL_Rec.Period10_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7029 	else
7030 	l_period_amount(10) := c_WAL_Rec.period10_amount;
7031 	end if;
7032       end if;
7033 
7034       if c_WAL_Rec.period11_amount is null then
7035 	l_period_amount(11) := null;
7036       else
7037 	if 11 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7038 	l_period_amount(11) := ROUND((c_WAL_Rec.period11_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7039 				      c_WAL_Rec.Period11_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7040 	else
7041 	l_period_amount(11) := c_WAL_Rec.period11_amount;
7042 	end if;
7043       end if;
7044 
7045       if c_WAL_Rec.period12_amount is null then
7046 	l_period_amount(12) := null;
7047       else
7048 	if 12 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7049 	l_period_amount(12) := ROUND((c_WAL_Rec.period12_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7050 				      c_WAL_Rec.Period12_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7051 	else
7052 	l_period_amount(12) := c_WAL_Rec.period12_amount;
7053 	end if;
7054       end if;
7055 
7056       if c_WAL_Rec.period13_amount is null then
7057 	l_period_amount(13) := null;
7058       else
7059 	if 13 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7060 	l_period_amount(13) := ROUND((c_WAL_Rec.period13_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7061 				      c_WAL_Rec.Period13_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7062 	else
7063 	l_period_amount(13) := c_WAL_Rec.period13_amount;
7064 	end if;
7065       end if;
7066 
7067       if c_WAL_Rec.period14_amount is null then
7068 	l_period_amount(14) := null;
7069       else
7070 	if 14 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7071 	l_period_amount(14) := ROUND((c_WAL_Rec.period14_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7072 				      c_WAL_Rec.Period14_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7073 	else
7074 	l_period_amount(14) := c_WAL_Rec.period14_amount;
7075 	end if;
7076       end if;
7077 
7078       if c_WAL_Rec.period15_amount is null then
7079 	l_period_amount(15) := null;
7080       else
7081 	if 15 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7082 	l_period_amount(15) := ROUND((c_WAL_Rec.period15_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7083 				      c_WAL_Rec.Period15_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7084 	else
7085 	l_period_amount(15) := c_WAL_Rec.period15_amount;
7086 	end if;
7087       end if;
7088 
7089       if c_WAL_Rec.period16_amount is null then
7090 	l_period_amount(16) := null;
7091       else
7092 	if 16 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7093 	l_period_amount(16) := ROUND((c_WAL_Rec.period16_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7094 				      c_WAL_Rec.Period16_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7095 	else
7096 	l_period_amount(16) := c_WAL_Rec.period16_amount;
7097 	end if;
7098       end if;
7099 
7100       if c_WAL_Rec.period17_amount is null then
7101 	l_period_amount(17) := null;
7102       else
7103 	if 17 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7104 	l_period_amount(17) := ROUND((c_WAL_Rec.period17_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7105 				      c_WAL_Rec.Period17_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7106 	else
7107 	l_period_amount(17) := c_WAL_Rec.period17_amount;
7108 	end if;
7109       end if;
7110 
7111       if c_WAL_Rec.period18_amount is null then
7112 	l_period_amount(18) := null;
7113       else
7114 	if 18 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7115 	l_period_amount(18) := ROUND((c_WAL_Rec.period18_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7116 				      c_WAL_Rec.Period18_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7117 	else
7118 	l_period_amount(18) := c_WAL_Rec.period18_amount;
7119 	end if;
7120       end if;
7121 
7122       if c_WAL_Rec.period19_amount is null then
7123 	l_period_amount(19) := null;
7124       else
7125 	if 19 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7126 	l_period_amount(19) := ROUND((c_WAL_Rec.period19_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7127 				      c_WAL_Rec.Period19_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7128 	else
7129 	l_period_amount(19) := c_WAL_Rec.period19_amount;
7130 	end if;
7131       end if;
7132 
7133       if c_WAL_Rec.period20_amount is null then
7134 	l_period_amount(20) := null;
7135       else
7136 	if 20 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7137 	l_period_amount(20) := ROUND((c_WAL_Rec.period20_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7138 				      c_WAL_Rec.Period20_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7139 	else
7140 	l_period_amount(20) := c_WAL_Rec.period20_amount;
7141 	end if;
7142       end if;
7143 
7144       if c_WAL_Rec.period21_amount is null then
7145 	l_period_amount(21) := null;
7146       else
7147 	if 21 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7148 	l_period_amount(21) := ROUND((c_WAL_Rec.period21_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7149 				      c_WAL_Rec.Period21_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7150 	else
7151 	l_period_amount(21) := c_WAL_Rec.period21_amount;
7152 	end if;
7153       end if;
7154 
7155       if c_WAL_Rec.period22_amount is null then
7156 	l_period_amount(22) := null;
7157       else
7158 	if 22 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7159 	l_period_amount(22) := ROUND((c_WAL_Rec.period22_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7160 				      c_WAL_Rec.Period22_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7161 	else
7162 	l_period_amount(22) := c_WAL_Rec.period22_amount;
7163 	end if;
7164       end if;
7165 
7166       if c_WAL_Rec.period23_amount is null then
7167 	l_period_amount(23) := null;
7168       else
7169 	if 23 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7170 	l_period_amount(23) := ROUND((c_WAL_Rec.period23_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7171 				      c_WAL_Rec.Period23_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7172 	else
7173 	l_period_amount(23) := c_WAL_Rec.period23_amount;
7174 	end if;
7175       end if;
7176 
7177       if c_WAL_Rec.period24_amount is null then
7178 	l_period_amount(24) := null;
7179       else
7180 	if 24 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7181 	l_period_amount(24) := ROUND((c_WAL_Rec.period24_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7182 				      c_WAL_Rec.Period24_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7183 	else
7184 	l_period_amount(24) := c_WAL_Rec.period24_amount;
7185 	end if;
7186       end if;
7187 
7188       if c_WAL_Rec.period25_amount is null then
7189 	l_period_amount(25) := null;
7190       else
7191 	if 25 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7192 	l_period_amount(25) := ROUND((c_WAL_Rec.period25_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7193 				      c_WAL_Rec.Period25_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7194 	else
7195 	l_period_amount(25) := c_WAL_Rec.period25_amount;
7196 	end if;
7197       end if;
7198 
7199       if c_WAL_Rec.period26_amount is null then
7200 	l_period_amount(26) := null;
7201       else
7202 	if 26 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7203 	l_period_amount(26) := ROUND((c_WAL_Rec.period26_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7204 				      c_WAL_Rec.Period26_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7205 	else
7206 	l_period_amount(26) := c_WAL_Rec.period26_amount;
7207 	end if;
7208       end if;
7209 
7210       if c_WAL_Rec.period27_amount is null then
7211 	l_period_amount(27) := null;
7212       else
7213 	if 27 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7214 	l_period_amount(27) := ROUND((c_WAL_Rec.period27_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7215 				      c_WAL_Rec.Period27_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7216 	else
7217 	l_period_amount(27) := c_WAL_Rec.period27_amount;
7218 	end if;
7219       end if;
7220 
7221       if c_WAL_Rec.period28_amount is null then
7222 	l_period_amount(28) := null;
7223       else
7224 	if 28 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7225 	l_period_amount(28) := ROUND((c_WAL_Rec.period28_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7226 				      c_WAL_Rec.Period28_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7227 	else
7228 	l_period_amount(28) := c_WAL_Rec.period28_amount;
7229 	end if;
7230       end if;
7231 
7232       if c_WAL_Rec.period29_amount is null then
7233 	l_period_amount(29) := null;
7234       else
7235 	if 29 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7236 	l_period_amount(29) := ROUND((c_WAL_Rec.period29_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7237 				      c_WAL_Rec.Period29_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7238 	else
7239 	l_period_amount(29) := c_WAL_Rec.period29_amount;
7240 	end if;
7241       end if;
7242 
7243       if c_WAL_Rec.period30_amount is null then
7244 	l_period_amount(30) := null;
7245       else
7246 	if 30 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7247 	l_period_amount(30) := ROUND((c_WAL_Rec.period30_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7248 				      c_WAL_Rec.Period30_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7249 	else
7250 	l_period_amount(30) := c_WAL_Rec.period30_amount;
7251 	end if;
7252       end if;
7253 
7254       if c_WAL_Rec.period31_amount is null then
7255 	l_period_amount(31) := null;
7256       else
7257 	if 31 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7258 	l_period_amount(31) := ROUND((c_WAL_Rec.period31_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7259 				      c_WAL_Rec.Period31_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7260 	else
7261 	l_period_amount(31) := c_WAL_Rec.period31_amount;
7262 	end if;
7263       end if;
7264 
7265       if c_WAL_Rec.period32_amount is null then
7266 	l_period_amount(32) := null;
7267       else
7268 	if 32 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7269 	l_period_amount(32) := ROUND((c_WAL_Rec.period32_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7270 				      c_WAL_Rec.Period32_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7271 	else
7272 	l_period_amount(32) := c_WAL_Rec.period32_amount;
7273 	end if;
7274       end if;
7275 
7276       if c_WAL_Rec.period33_amount is null then
7277 	l_period_amount(33) := null;
7278       else
7279 	if 33 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7280 	l_period_amount(33) := ROUND((c_WAL_Rec.period33_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7281 				      c_WAL_Rec.Period33_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7282 	else
7283 	l_period_amount(33) := c_WAL_Rec.period33_amount;
7284 	end if;
7285       end if;
7286 
7287       if c_WAL_Rec.period34_amount is null then
7288 	l_period_amount(34) := null;
7289       else
7290 	if 34 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7291 	l_period_amount(34) := ROUND((c_WAL_Rec.period34_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7292 				      c_WAL_Rec.Period34_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7293 	else
7294 	l_period_amount(34) := c_WAL_Rec.period34_amount;
7295 	end if;
7296       end if;
7297 
7298       if c_WAL_Rec.period35_amount is null then
7299 	l_period_amount(35) := null;
7300       else
7301 	if 35 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7302 	l_period_amount(35) := ROUND((c_WAL_Rec.period35_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7303 				      c_WAL_Rec.Period35_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7304 	else
7305 	l_period_amount(35) := c_WAL_Rec.period35_amount;
7306 	end if;
7307       end if;
7308 
7309       if c_WAL_Rec.period36_amount is null then
7310 	l_period_amount(36) := null;
7311       else
7312 	if 36 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7313 	l_period_amount(36) := ROUND((c_WAL_Rec.period36_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7314 				      c_WAL_Rec.Period36_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7315 	else
7316 	l_period_amount(36) := c_WAL_Rec.period36_amount;
7317 	end if;
7318       end if;
7319 
7320       if c_WAL_Rec.period37_amount is null then
7321 	l_period_amount(37) := null;
7322       else
7323 	if 37 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7324 	l_period_amount(37) := ROUND((c_WAL_Rec.period37_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7325 				      c_WAL_Rec.Period37_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7326 	else
7327 	l_period_amount(37) := c_WAL_Rec.period37_amount;
7328 	end if;
7329       end if;
7330 
7331       if c_WAL_Rec.period38_amount is null then
7332 	l_period_amount(38) := null;
7333       else
7334 	if 38 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7335 	l_period_amount(38) := ROUND((c_WAL_Rec.period38_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7336 				      c_WAL_Rec.Period38_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7337 	else
7338 	l_period_amount(38) := c_WAL_Rec.period38_amount;
7339 	end if;
7340       end if;
7341 
7342       if c_WAL_Rec.period39_amount is null then
7343 	l_period_amount(39) := null;
7344       else
7345 	if 39 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7346 	l_period_amount(39) := ROUND((c_WAL_Rec.period39_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7347 				      c_WAL_Rec.Period39_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7348 	else
7349 	l_period_amount(39) := c_WAL_Rec.period39_amount;
7350 	end if;
7351       end if;
7352 
7353       if c_WAL_Rec.period40_amount is null then
7354 	l_period_amount(40) := null;
7355       else
7356 	if 40 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7357 	l_period_amount(40) := ROUND((c_WAL_Rec.period40_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7358 				      c_WAL_Rec.Period40_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7359 	else
7360 	l_period_amount(40) := c_WAL_Rec.period40_amount;
7361 	end if;
7362       end if;
7363 
7364       if c_WAL_Rec.period41_amount is null then
7365 	l_period_amount(41) := null;
7366       else
7367 	if 41 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7368 	l_period_amount(41) := ROUND((c_WAL_Rec.period41_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7369 				      c_WAL_Rec.Period41_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7370 	else
7371 	l_period_amount(41) := c_WAL_Rec.period41_amount;
7372 	end if;
7373       end if;
7374 
7375       if c_WAL_Rec.period42_amount is null then
7376 	l_period_amount(42) := null;
7377       else
7378 	if 42 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7379 	l_period_amount(42) := ROUND((c_WAL_Rec.period42_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7380 				      c_WAL_Rec.Period42_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7381 	else
7382 	l_period_amount(42) := c_WAL_Rec.period42_amount;
7383 	end if;
7384       end if;
7385 
7386       if c_WAL_Rec.period43_amount is null then
7387 	l_period_amount(43) := null;
7388       else
7389 	if 43 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7390 	l_period_amount(43) := ROUND((c_WAL_Rec.period43_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7391 				      c_WAL_Rec.Period43_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7392 	else
7393 	l_period_amount(43) := c_WAL_Rec.period43_amount;
7394 	end if;
7395       end if;
7396 
7397       if c_WAL_Rec.period44_amount is null then
7398 	l_period_amount(44) := null;
7399       else
7400 	if 44 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7401 	l_period_amount(44) := ROUND((c_WAL_Rec.period44_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7402 				      c_WAL_Rec.Period44_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7403 	else
7404 	l_period_amount(44) := c_WAL_Rec.period44_amount;
7405 	end if;
7406       end if;
7407 
7408       if c_WAL_Rec.period45_amount is null then
7409 	l_period_amount(45) := null;
7410       else
7411 	if 45 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7412 	l_period_amount(45) := ROUND((c_WAL_Rec.period45_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7413 				      c_WAL_Rec.Period45_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7414 	else
7415 	l_period_amount(45) := c_WAL_Rec.period45_amount;
7416 	end if;
7417       end if;
7418 
7419       if c_WAL_Rec.period46_amount is null then
7420 	l_period_amount(46) := null;
7421       else
7422 	if 46 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7423 	l_period_amount(46) := ROUND((c_WAL_Rec.period46_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7424 				      c_WAL_Rec.Period46_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7425 	else
7426 	l_period_amount(46) := c_WAL_Rec.period46_amount;
7427 	end if;
7428       end if;
7429 
7430       if c_WAL_Rec.period47_amount is null then
7431 	l_period_amount(47) := null;
7432       else
7433 	if 47 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7434 	l_period_amount(47) := ROUND((c_WAL_Rec.period47_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7435 				      c_WAL_Rec.Period47_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7436 	else
7437 	l_period_amount(47) := c_WAL_Rec.period47_amount;
7438 	end if;
7439       end if;
7440 
7441       if c_WAL_Rec.period48_amount is null then
7442 	l_period_amount(48) := null;
7443       else
7444 	if 48 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7445 	l_period_amount(48) := ROUND((c_WAL_Rec.period48_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7446 				      c_WAL_Rec.Period48_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7447 	else
7448 	l_period_amount(48) := c_WAL_Rec.period48_amount;
7449 	end if;
7450       end if;
7451 
7452       if c_WAL_Rec.period49_amount is null then
7453 	l_period_amount(49) := null;
7454       else
7455 	if 49 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7456 	l_period_amount(49) := ROUND((c_WAL_Rec.period49_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7457 				      c_WAL_Rec.Period49_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7458 	else
7459 	l_period_amount(49) := c_WAL_Rec.period49_amount;
7460 	end if;
7461       end if;
7462 
7463       if c_WAL_Rec.period50_amount is null then
7464 	l_period_amount(50) := null;
7465       else
7466 	if 50 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7467 	l_period_amount(50) := ROUND((c_WAL_Rec.period50_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7468 				      c_WAL_Rec.Period50_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7469 	else
7470 	l_period_amount(50) := c_WAL_Rec.period50_amount;
7471 	end if;
7472       end if;
7473 
7474       if c_WAL_Rec.period51_amount is null then
7475 	l_period_amount(51) := null;
7476       else
7477 	if 51 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7478 	l_period_amount(51) := ROUND((c_WAL_Rec.period51_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7479 				      c_WAL_Rec.Period51_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7480 	else
7481 	l_period_amount(51) := c_WAL_Rec.period51_amount;
7482 	end if;
7483       end if;
7484 
7485       if c_WAL_Rec.period52_amount is null then
7486 	l_period_amount(52) := null;
7487       else
7488 	if 52 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7489 	l_period_amount(52) := ROUND((c_WAL_Rec.period52_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7490 				      c_WAL_Rec.Period52_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7491 	else
7492 	l_period_amount(52) := c_WAL_Rec.period52_amount;
7493 	end if;
7494       end if;
7495 
7496       if c_WAL_Rec.period53_amount is null then
7497 	l_period_amount(53) := null;
7498       else
7499 	if 53 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7500 	l_period_amount(53) := ROUND((c_WAL_Rec.period53_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7501 				      c_WAL_Rec.Period53_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7502 	else
7503 	l_period_amount(53) := c_WAL_Rec.period53_amount;
7504 	end if;
7505       end if;
7506 
7507       if c_WAL_Rec.period54_amount is null then
7508 	l_period_amount(54) := null;
7509       else
7510 	if 54 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7511 	l_period_amount(54) := ROUND((c_WAL_Rec.period54_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7512 				      c_WAL_Rec.Period54_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7513 	else
7514 	l_period_amount(54) := c_WAL_Rec.period54_amount;
7515 	end if;
7516       end if;
7517 
7518       if c_WAL_Rec.period55_amount is null then
7519 	l_period_amount(55) := null;
7520       else
7521 	if 55 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7522 	l_period_amount(55) := ROUND((c_WAL_Rec.period55_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7523 				      c_WAL_Rec.Period55_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7524 	else
7525 	l_period_amount(55) := c_WAL_Rec.period55_amount;
7526 	end if;
7527       end if;
7528 
7529       if c_WAL_Rec.period56_amount is null then
7530 	l_period_amount(56) := null;
7531       else
7532 	if 56 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7533 	l_period_amount(56) := ROUND((c_WAL_Rec.period56_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7534 				      c_WAL_Rec.Period56_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7535 	else
7536 	l_period_amount(56) := c_WAL_Rec.period56_amount;
7537 	end if;
7538       end if;
7539 
7540       if c_WAL_Rec.period57_amount is null then
7541 	l_period_amount(57) := null;
7542       else
7543 	if 57 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7544 	l_period_amount(57) := ROUND((c_WAL_Rec.period57_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7545 				      c_WAL_Rec.Period57_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7546 	else
7547 	l_period_amount(57) := c_WAL_Rec.period57_amount;
7548 	end if;
7549       end if;
7550 
7551       if c_WAL_Rec.period58_amount is null then
7552 	l_period_amount(58) := null;
7553       else
7554 	if 58 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7555 	l_period_amount(58) := ROUND((c_WAL_Rec.period58_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7556 				      c_WAL_Rec.Period58_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7557 	else
7558 	l_period_amount(58) := c_WAL_Rec.period58_amount;
7559 	end if;
7560       end if;
7561 
7562       if c_WAL_Rec.period59_amount is null then
7563 	l_period_amount(59) := null;
7564       else
7565 	if 59 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7566 	l_period_amount(59) := ROUND((c_WAL_Rec.period59_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7567 				      c_WAL_Rec.Period59_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7568 	else
7569 	l_period_amount(59) := c_WAL_Rec.period59_amount;
7570 	end if;
7571       end if;
7572 
7573       if c_WAL_Rec.period60_amount is null then
7574 	l_period_amount(60) := null;
7575       else
7576 	if 60 between PSB_WS_ACCT1.g_cy_start_index and l_last_period_index then
7577 	l_period_amount(60) := ROUND((c_WAL_Rec.period60_amount + ((p_new_ytd_amount - p_old_ytd_amount) *
7578 				      c_WAL_Rec.Period60_Amount / l_old_ytd_amount)) / p_rounding_factor) * p_rounding_factor;
7579 	else
7580 	l_period_amount(60) := c_WAL_Rec.period60_amount;
7581 	end if;
7582       end if;
7583 
7584       l_running_total := l_running_total
7585 		+ nvl(l_period_amount(1),0)   + nvl(l_period_amount(2),0)   + nvl(l_period_amount(3),0)   + nvl(l_period_amount(4),0)   + nvl(l_period_amount(5),0)
7586 		+ nvl(l_period_amount(6),0)   + nvl(l_period_amount(7),0)   + nvl(l_period_amount(8),0)   + nvl(l_period_amount(9),0)   + nvl(l_period_amount(10),0)
7587 		+ nvl(l_period_amount(11),0)  + nvl(l_period_amount(12),0)  + nvl(l_period_amount(13),0)  + nvl(l_period_amount(14),0)  + nvl(l_period_amount(15),0)
7588 		+ nvl(l_period_amount(16),0)  + nvl(l_period_amount(17),0)  + nvl(l_period_amount(18),0)  + nvl(l_period_amount(19),0)  + nvl(l_period_amount(20),0)
7589 		+ nvl(l_period_amount(21),0)  + nvl(l_period_amount(22),0)  + nvl(l_period_amount(23),0)  + nvl(l_period_amount(24),0)  + nvl(l_period_amount(25),0)
7590 		+ nvl(l_period_amount(26),0)  + nvl(l_period_amount(27),0)  + nvl(l_period_amount(28),0)  + nvl(l_period_amount(29),0)  + nvl(l_period_amount(30),0)
7591 		+ nvl(l_period_amount(31),0)  + nvl(l_period_amount(32),0)  + nvl(l_period_amount(33),0)  + nvl(l_period_amount(34),0)  + nvl(l_period_amount(35),0)
7592 		+ nvl(l_period_amount(36),0)  + nvl(l_period_amount(37),0)  + nvl(l_period_amount(38),0)  + nvl(l_period_amount(39),0)  + nvl(l_period_amount(40),0)
7593 		+ nvl(l_period_amount(41),0)  + nvl(l_period_amount(42),0)  + nvl(l_period_amount(43),0)  + nvl(l_period_amount(44),0)  + nvl(l_period_amount(45),0)
7594 		+ nvl(l_period_amount(46),0)  + nvl(l_period_amount(47),0)  + nvl(l_period_amount(48),0)  + nvl(l_period_amount(49),0)  + nvl(l_period_amount(50),0)
7595 		+ nvl(l_period_amount(51),0)  + nvl(l_period_amount(52),0)  + nvl(l_period_amount(53),0)  + nvl(l_period_amount(54),0)  + nvl(l_period_amount(55),0)
7596 		+ nvl(l_period_amount(56),0)  + nvl(l_period_amount(57),0)  + nvl(l_period_amount(58),0)  + nvl(l_period_amount(59),0)  + nvl(l_period_amount(60),0);
7597 
7598     end loop;
7599 
7600     /* Bug 3133240 Start */
7601     --Added the IF condition
7602 
7603     IF l_last_period_index > 0 THEN
7604     	l_period_amount(l_last_period_index) := l_period_amount(l_last_period_index) + p_new_ytd_amount - l_running_total;
7605     END IF;
7606 
7607     /* Bug 3133240 End */
7608 
7609   end;
7610   end if;
7611 
7612   -- Call Create_Account_Dist to update Period Amounts for the Account Line
7613 
7614   PSB_WS_ACCT1.Create_Account_Dist
7615      (p_api_version => 1.0,
7616       p_return_status => l_return_status,
7617       p_worksheet_id => p_worksheet_id,
7618       p_account_line_id => p_account_line_id,
7619       p_check_stages => FND_API.G_FALSE,
7620       p_ytd_amount => l_ytd_amount,
7621       p_period_amount => l_period_amount,
7622       p_service_package_id => p_service_package_id,
7623       p_current_stage_seq => p_current_stage_seq);
7624 
7625   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7626     raise FND_API.G_EXC_ERROR;
7627   end if;
7628 
7629   p_return_status := FND_API.G_RET_STS_SUCCESS;
7630 
7631 
7632 EXCEPTION
7633 
7634    when FND_API.G_EXC_ERROR then
7635      p_return_status := FND_API.G_RET_STS_ERROR;
7636 
7637 
7638    when FND_API.G_EXC_UNEXPECTED_ERROR then
7639      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7640 
7641 
7642    when OTHERS then
7643      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7644      if FND_MSG_PUB.Check_Msg_Level
7645        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
7646      then
7647        FND_MSG_PUB.Add_Exc_Msg
7648 	  (p_pkg_name => G_PKG_NAME,
7649 	   p_procedure_name => 'Distribute_Account_Lines');
7650      end if;
7651 
7652 END Distribute_Account_Lines;
7653 
7654 /* ----------------------------------------------------------------------- */
7655 
7656 PROCEDURE Insert_Summary_Accounts
7657 ( p_return_status       OUT  NOCOPY  VARCHAR2,
7658   p_worksheet_id        IN   NUMBER,
7659   p_rounding_factor     IN   NUMBER,
7660   p_stage_set_id        IN   NUMBER,
7661   p_current_stage_seq   IN   NUMBER,
7662   p_set_of_books_id     IN   NUMBER,
7663   p_flex_code           IN   NUMBER,
7664   p_budget_group_id     IN   NUMBER,
7665   p_budget_calendar_id  IN   NUMBER
7666 ) IS
7667 
7668   first_time            BOOLEAN;
7669 
7670   l_period_amount       PSB_WS_ACCT1.g_prdamt_tbl_type;
7671 
7672   sql_sumbgroup         VARCHAR2(1000);
7673   cur_sumbgroup         INTEGER;
7674   num_sumbgroup         INTEGER;
7675 
7676   l_bgroup_id           NUMBER;
7677   l_parentbgroup_id     NUMBER;
7678   l_root_budget_group   VARCHAR2(1);
7679 
7680   l_bgroups_tbl         g_budgetgroup_tbl_type;
7681   l_num_pbgroups        NUMBER;
7682 
7683   l_index               PLS_INTEGER;
7684   l_init_index          PLS_INTEGER;
7685   l_bgroups_index       PLS_INTEGER;
7686   l_search_index        PLS_INTEGER;
7687 
7688   l_parent_found        BOOLEAN;
7689 
7690   l_account_line_id     NUMBER;
7691 
7692   l_num_accounts        NUMBER := 0;
7693   l_return_status       VARCHAR2(1);
7694   l_msg_data            VARCHAR2(2000);
7695   l_msg_count           NUMBER;
7696 
7697   l_api_name       CONSTANT  VARCHAR2(30) := 'Insert_Summary_Accounts';
7698 
7699   -- This cursor sums up the detailed account balances for each summary account
7700   -- following the mapping in GL_ACCOUNT_HIERARCHIES
7701 
7702 
7703   -- Bug 4474717.
7704   -- Replaced set_of_books_id with ledger_id
7705   -- in the following cursor.
7706 
7707   cursor c_SumCCID is
7708     select a.summary_code_combination_id,
7709 	   a.template_id,
7710 	   b.service_package_id,
7711 	   b.start_stage_seq,
7712 	   b.budget_year_id,
7713 	   b.currency_code,
7714 	   b.balance_type,
7715 	   sum(nvl(b.annual_fte, 0)) annual_fte,
7716 	   sum(nvl(b.ytd_amount, 0)) ytd_amount,
7717 	   sum(nvl(b.period1_amount, 0)) p1_amt,
7718 	   sum(nvl(b.period2_amount, 0)) p2_amt,
7719 	   sum(nvl(b.period3_amount, 0)) p3_amt,
7720 	   sum(nvl(b.period4_amount, 0)) p4_amt,
7721 	   sum(nvl(b.period5_amount, 0)) p5_amt,
7722 	   sum(nvl(b.period6_amount, 0)) p6_amt,
7723 	   sum(nvl(b.period7_amount, 0)) p7_amt,
7724 	   sum(nvl(b.period8_amount, 0)) p8_amt,
7725 	   sum(nvl(b.period9_amount, 0)) p9_amt,
7726 	   sum(nvl(b.period10_amount, 0)) p10_amt,
7727 	   sum(nvl(b.period11_amount, 0)) p11_amt,
7728 	   sum(nvl(b.period12_amount, 0)) p12_amt
7729       from GL_ACCOUNT_HIERARCHIES a,
7730 	   PSB_WS_ACCOUNT_LINES b,
7731 	   PSB_SUMMARY_TEMPLATES c
7732      where a.detail_code_combination_id = b.code_combination_id
7733        and a.template_id = c.template_id
7734        and a.ledger_id       = p_set_of_books_id
7735        and p_current_stage_seq between b.start_stage_seq and b.current_stage_seq
7736        and b.template_id is null
7737        and c.set_of_books_id = p_set_of_books_id
7738        and exists
7739 	  (select 1
7740 	     from PSB_BUDGET_GROUPS f
7741 	    where f.budget_group_id = b.budget_group_id
7742 	      and (f.budget_group_id = p_budget_group_id or f.root_budget_group_id = p_budget_group_id))
7743        and exists
7744 	  (select 1
7745 	     from PSB_WS_LINES
7746 	    where account_line_id = b.account_line_id
7747 	      and worksheet_id = p_worksheet_id)
7748        and exists
7749 	  (select 1
7750 	     from PSB_BUDGET_PERIODS d
7751 	    where d.budget_calendar_id = p_budget_calendar_id
7752 	      and d.budget_period_type = 'Y'
7753 	      and d.start_date >= c.effective_start_date
7754 	      and b.budget_year_id = d.budget_period_id)
7755      group by a.summary_code_combination_id,
7756 	      a.template_id,
7757 	      b.service_package_id,
7758 	      b.start_stage_seq,
7759 	      b.budget_year_id,
7760 	      b.currency_code,
7761 	      b.balance_type;
7762 
7763   -- Find the distinct budget groups for the detailed CCIDs that map to the
7764   -- summary CCID
7765 
7766   cursor c_DtlCCID (SumCCID NUMBER,
7767 		    TemplateID NUMBER,
7768 		    YearID NUMBER) is
7769     select distinct a.budget_group_id
7770       from PSB_WS_ACCOUNT_LINES a,
7771 	   GL_ACCOUNT_HIERARCHIES b
7772      where p_current_stage_seq between a.start_stage_seq and a.current_stage_seq
7773        and a.code_combination_id = b.detail_code_combination_id
7774        and a.budget_year_id = YearID
7775        /*For Bug No : 2586618 Start*/
7776        and a.template_id is null
7777        and exists (select 1
7778 		     from PSB_WS_LINES
7779 		    where account_line_id = a.account_line_id
7780 		      and worksheet_id = p_worksheet_id)
7781        /*For Bug No : 2586618 End*/
7782        and b.summary_code_combination_id = SumCCID
7783        and b.template_id = TemplateID;
7784 
7785 BEGIN
7786 
7787   for c_SumCCID_Rec in c_SumCCID loop
7788 
7789     -- Determine Budget Group for each Summary CCID. This loop assumes that the Budget Group
7790     -- for a Summary CCID will remain the same for all Budget Years. Need an efficient way
7791     -- to determine if Budget Group for Summary CCID changes across Budget Years
7792 
7793     if c_SumCCID_Rec.summary_code_combination_id <> nvl(g_summary_ccid, FND_API.G_MISS_NUM) then
7794     begin
7795 
7796       g_summary_ccid := c_SumCCID_Rec.summary_code_combination_id;
7797 
7798       first_time := TRUE;
7799 
7800       for l_init_index in 1..PSB_WS_ACCT1.g_max_num_amounts loop
7801 	l_period_amount(l_init_index) := 0;
7802       end loop;
7803 
7804       for l_init_index in 1..l_bgroups_tbl.Count loop
7805 	l_bgroups_tbl(l_init_index) := null;
7806       end loop;
7807 
7808       l_bgroups_index := 0;
7809 
7810       -- Populate l_bgroups_tbl with the distinct Budget Groups for all the detailed CCIDs
7811       -- that are mapped to a summary CCID. The Budget Group for the summary CCID is the
7812       -- Budget Group at the lowest common level in the Budget Group Hierarchy that covers
7813       -- the Budget Groups for the detailed CCIDs
7814 
7815       for c_DtlCCID_Rec in c_DtlCCID (c_SumCCID_Rec.summary_code_combination_id,
7816 				      c_SumCCID_Rec.template_id,
7817 				      c_SumCCID_Rec.budget_year_id) loop
7818 
7819 	if first_time then
7820 	  l_bgroup_id := c_DtlCCID_Rec.budget_group_id;
7821 	  first_time := FALSE;
7822 	end if;
7823 
7824 	if c_DtlCCID_Rec.budget_group_id <> l_bgroup_id then
7825 	  l_bgroups_index := l_bgroups_index + 1;
7826 	  l_bgroups_tbl(l_bgroups_index) := c_DtlCCID_Rec.budget_group_id;
7827 	end if;
7828 
7829       end loop;
7830 
7831       -- Loop thru l_bgroups_tbl to find the lowest common Budget Group in the
7832       -- Budget Group Hierarchy that covers the Budget Groups for all the
7833       -- detailed CCIDs. If no such Budget Group is found, the root Budget Group
7834       -- is assigned to the Summary CCID
7835 
7836       if l_bgroups_index > 0 then
7837       begin
7838 
7839 	l_bgroups_index := l_bgroups_index + 1;
7840 	l_bgroups_tbl(l_bgroups_index) := l_bgroup_id;
7841 
7842 	sql_sumbgroup := 'select parent_budget_group_id, ' ||
7843 				'root_budget_group ' ||
7844 			   'from psb_budget_groups ' ||
7845 			  'where budget_group_id = :budget_group_id';
7846 
7847 	cur_sumbgroup := dbms_sql.open_cursor;
7848 	dbms_sql.parse(cur_sumbgroup, sql_sumbgroup, dbms_sql.v7);
7849 
7850 	l_parent_found := FALSE;
7851 	l_num_pbgroups := l_bgroups_index;
7852 
7853 	while l_num_pbgroups > 1 loop
7854 
7855 	  for l_index in 1..l_bgroups_index loop
7856 
7857 	    if l_bgroups_tbl(l_index) is not null then
7858 	    begin
7859 
7860 	      dbms_sql.bind_variable(cur_sumbgroup, ':budget_group_id', l_bgroups_tbl(l_index));
7861 
7862 	      dbms_sql.define_column(cur_sumbgroup, 1, l_parentbgroup_id);
7863 	      dbms_sql.define_column(cur_sumbgroup, 2, l_root_budget_group, 1);
7864 
7865 	      num_sumbgroup := dbms_sql.execute(cur_sumbgroup);
7866 
7867 	      loop
7868 
7869 		if dbms_sql.fetch_rows(cur_sumbgroup) = 0 then
7870 		  exit;
7871 		end if;
7872 
7873 		dbms_sql.column_value(cur_sumbgroup, 1, l_parentbgroup_id);
7874 		dbms_sql.column_value(cur_sumbgroup, 2, l_root_budget_group);
7875 
7876 		if l_root_budget_group = 'Y' then
7877 		  l_bgroup_id := l_bgroups_tbl(l_index);
7878 		  exit;
7879 		else
7880 		begin
7881 
7882 		  for l_search_index in 1..l_bgroups_index loop
7883 
7884 		    if ((l_bgroups_tbl(l_search_index) is not null) and
7885 			(l_bgroups_tbl(l_search_index) = l_parentbgroup_id)) then
7886 		      l_parent_found := TRUE;
7887 		      exit;
7888 		    end if;
7889 
7890 		  end loop;
7891 
7892 		  if l_parent_found then
7893 		    l_bgroups_tbl(l_index) := null;
7894 		    l_num_pbgroups := l_num_pbgroups - 1;
7895 		  else
7896 		    l_bgroups_tbl(l_index) := l_parentbgroup_id;
7897 		  end if;
7898 
7899 		end;
7900 		end if;
7901 
7902 	      end loop;
7903 
7904 	      if l_root_budget_group = 'Y' then
7905 		l_num_pbgroups := 1;
7906 		exit;
7907 	      end if;
7908 
7909 	    end;
7910 	    end if;
7911 
7912 	  end loop;
7913 
7914 	end loop;
7915 
7916 	for l_index in 1..l_bgroups_tbl.Count loop
7917 
7918 	  if l_bgroups_tbl(l_index) is not null then
7919 	    l_bgroup_id := l_bgroups_tbl(l_index);
7920 	  end if;
7921 
7922 	end loop;
7923 
7924 	dbms_sql.close_cursor(cur_sumbgroup);
7925 
7926       end;
7927       end if;
7928 
7929       g_summ_bgroup_id := l_bgroup_id;
7930 
7931     end;
7932     end if;
7933 
7934 
7935     -- Insert into WS Account Distributions
7936 
7937     l_period_amount(1) := c_SumCCID_Rec.p1_amt; l_period_amount(2) := c_SumCCID_Rec.p2_amt;
7938     l_period_amount(3) := c_SumCCID_Rec.p3_amt; l_period_amount(4) := c_SumCCID_Rec.p4_amt;
7939     l_period_amount(5) := c_SumCCID_Rec.p5_amt; l_period_amount(6) := c_SumCCID_Rec.p6_amt;
7940     l_period_amount(7) := c_SumCCID_Rec.p7_amt; l_period_amount(8) := c_SumCCID_Rec.p8_amt;
7941     l_period_amount(9) := c_SumCCID_Rec.p9_amt; l_period_amount(10) := c_SumCCID_Rec.p10_amt;
7942     l_period_amount(11) := c_SumCCID_Rec.p11_amt; l_period_amount(12) := c_SumCCID_Rec.p12_amt;
7943 
7944     -- Create Account Distributions for the Summary CCID
7945     PSB_WS_ACCT1.Create_Account_Dist
7946        (p_api_version => 1.0,
7947 	p_return_status => l_return_status,
7948 	p_account_line_id => l_account_line_id,
7949 	p_worksheet_id => p_worksheet_id,
7950 	p_service_package_id => c_SumCCID_Rec.service_package_id,
7951 	p_check_spal_exists => FND_API.G_FALSE,
7952 	p_gl_cutoff_period => null,
7953 	p_allocrule_set_id => null,
7954 	p_budget_calendar_id => null,
7955 	p_rounding_factor => p_rounding_factor,
7956 	p_stage_set_id => p_stage_set_id,
7957 	p_start_stage_seq => c_SumCCID_Rec.start_stage_seq,
7958 	p_current_stage_seq => p_current_stage_seq,
7959 	p_budget_group_id => g_summ_bgroup_id,
7960 	p_budget_year_id => c_SumCCID_Rec.budget_year_id,
7961 	p_ccid => c_SumCCID_Rec.summary_code_combination_id,
7962 	p_template_id => c_SumCCID_Rec.template_id,
7963 	p_currency_code => c_SumCCID_Rec.currency_code,
7964 	p_balance_type => c_SumCCID_Rec.balance_type,
7965 	p_annual_fte => c_SumCCID_Rec.annual_fte,
7966 	p_ytd_amount => c_SumCCID_Rec.ytd_amount,
7967 	p_period_amount => l_period_amount);
7968 
7969     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7970       raise FND_API.G_EXC_ERROR;
7971     end if;
7972 
7973     l_num_accounts := l_num_accounts + 1;
7974 
7975     if l_num_accounts > PSB_WS_ACCT1.g_checkpoint_save then
7976       commit work;
7977       l_num_accounts := 0;
7978     end if;
7979 
7980   end loop;
7981 
7982 
7983   -- Initialize API return status to success
7984 
7985   p_return_status := FND_API.G_RET_STS_SUCCESS;
7986 
7987 
7988 EXCEPTION
7989 
7990 
7991    when FND_API.G_EXC_ERROR then
7992      p_return_status := FND_API.G_RET_STS_ERROR;
7993      FND_MSG_PUB.Count_And_Get (p_count => l_msg_count,
7994 				p_data => l_msg_data);
7995 
7996 
7997    when FND_API.G_EXC_UNEXPECTED_ERROR then
7998      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7999      FND_MSG_PUB.Count_And_Get (p_count => l_msg_count,
8000 				p_data => l_msg_data);
8001 
8002 
8003    when OTHERS then
8004      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8005      if dbms_sql.is_open(cur_sumbgroup) then
8006        dbms_sql.close_cursor(cur_sumbgroup);
8007      end if;
8008 
8009      if FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
8010        FND_MSG_PUB.Add_Exc_Msg (G_PKG_NAME,
8011 				l_api_name);
8012      end if;
8013 
8014      FND_MSG_PUB.Count_And_Get (p_count => l_msg_count,
8015 				p_data  => l_msg_data);
8016 
8017 END Insert_Summary_Accounts;
8018 
8019 /* ----------------------------------------------------------------------- */
8020 
8021 -- Create Summary Totals for the detailed Accounting Lines using the
8022 -- templates specified in PSB
8023 
8024 PROCEDURE Create_Rollup_Totals
8025 ( p_api_version         IN   NUMBER,
8026   p_validation_level    IN   NUMBER := FND_API.G_VALID_LEVEL_NONE,
8027   p_return_status       OUT  NOCOPY  VARCHAR2,
8028   p_worksheet_id        IN   NUMBER,
8029   p_rounding_factor     IN   NUMBER := FND_API.G_MISS_NUM,
8030   p_stage_set_id        IN   NUMBER := FND_API.G_MISS_NUM,
8031   p_current_stage_seq   IN   NUMBER := FND_API.G_MISS_NUM,
8032   p_set_of_books_id     IN   NUMBER := FND_API.G_MISS_NUM,
8033   p_flex_code           IN   NUMBER := FND_API.G_MISS_NUM,
8034   p_budget_group_id     IN   NUMBER := FND_API.G_MISS_NUM,
8035   p_budget_calendar_id  IN   NUMBER := FND_API.G_MISS_NUM
8036 ) IS
8037 
8038   l_api_name            CONSTANT VARCHAR2(30)   := 'Create_Rollup_Totals';
8039   l_api_version         CONSTANT NUMBER         := 1.0;
8040 
8041   l_year_index          PLS_INTEGER;
8042 
8043   l_budget_group_id     NUMBER;
8044   l_stage_set_id        NUMBER;
8045   l_current_stage_seq   NUMBER;
8046   l_budget_calendar_id  NUMBER;
8047   l_rounding_factor     NUMBER;
8048 
8049   l_set_of_books_id     NUMBER;
8050   l_flex_code           NUMBER;
8051 
8052   l_return_status       VARCHAR2(1);
8053   l_msg_data            VARCHAR2(2000);
8054   l_msg_count           NUMBER;
8055 
8056   cursor c_WS is
8057     select budget_group_id,
8058 	   stage_set_id,
8059 	   current_stage_seq,
8060 	   budget_calendar_id,
8061 	   rounding_factor
8062       from PSB_WORKSHEETS
8063      where worksheet_id = p_worksheet_id;
8064 
8065   cursor c_BG is
8066     select nvl(set_of_books_id, root_set_of_books_id) set_of_books_id,
8067 	   nvl(chart_of_accounts_id, root_chart_of_accounts_id) chart_of_accounts_id
8068       from PSB_BUDGET_GROUPS_V
8069      where budget_group_id = l_budget_group_id;
8070 
8071 BEGIN
8072 
8073   -- Standard call to check for call compatibility
8074 
8075   if not FND_API.Compatible_API_Call (l_api_version,
8076 				      p_api_version,
8077 				      l_api_name,
8078 				      G_PKG_NAME)
8079   then
8080     raise FND_API.G_EXC_UNEXPECTED_ERROR;
8081   end if;
8082 
8083   -- Delete all Summary Account Lines from PSB_WS_ACCOUNT_LINES to obviate any changes
8084   -- to the Summary Account - Detailed Account mappings in GL
8085 
8086   PSB_WORKSHEET.Delete_Summary_Lines
8087      (p_api_version => 1.0,
8088       p_return_status => l_return_status,
8089       p_worksheet_id => p_worksheet_id);
8090 
8091   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
8092     raise FND_API.G_EXC_ERROR;
8093   end if;
8094 
8095   -- Select default values for input parameters that were not passed in
8096 
8097   if ((p_budget_group_id = FND_API.G_MISS_NUM) or
8098       (p_stage_set_id = FND_API.G_MISS_NUM) or
8099       (p_current_stage_seq = FND_API.G_MISS_NUM) or
8100       (p_budget_calendar_id = FND_API.G_MISS_NUM) or
8101       (p_rounding_factor = FND_API.G_MISS_NUM)) then
8102   begin
8103 
8104     for c_WS_Rec in c_WS loop
8105       l_budget_group_id := c_WS_Rec.budget_group_id;
8106       l_stage_set_id := c_WS_Rec.stage_set_id;
8107       l_current_stage_seq := c_WS_Rec.current_stage_seq;
8108       l_budget_calendar_id := c_WS_Rec.budget_calendar_id;
8109       l_rounding_factor := c_WS_Rec.rounding_factor;
8110     end loop;
8111 
8112   end;
8113   end if;
8114 
8115   if p_budget_group_id <> FND_API.G_MISS_NUM then
8116     l_budget_group_id := p_budget_group_id;
8117   end if;
8118 
8119   if p_stage_set_id <> FND_API.G_MISS_NUM then
8120     l_stage_set_id := p_stage_set_id;
8121   end if;
8122 
8123   if p_current_stage_seq <> FND_API.G_MISS_NUM then
8124     l_current_stage_seq := p_current_stage_seq;
8125   end if;
8126 
8127   if p_budget_calendar_id <> FND_API.G_MISS_NUM then
8128     l_budget_calendar_id := p_budget_calendar_id;
8129   end if;
8130 
8131   if p_rounding_factor <> FND_API.G_MISS_NUM then
8132     l_rounding_factor := p_rounding_factor;
8133   end if;
8134 
8135   if ((p_set_of_books_id = FND_API.G_MISS_NUM) or
8136       (p_flex_code = FND_API.G_MISS_NUM)) then
8137   begin
8138 
8139     for c_BG_Rec in c_BG loop
8140       l_set_of_books_id := c_BG_Rec.set_of_books_id;
8141       l_flex_code := c_BG_Rec.chart_of_accounts_id;
8142     end loop;
8143 
8144   end;
8145   end if;
8146 
8147   if p_set_of_books_id <> FND_API.G_MISS_NUM then
8148     l_set_of_books_id := p_set_of_books_id;
8149   end if;
8150 
8151   if p_flex_code <> FND_API.G_MISS_NUM then
8152     l_flex_code := p_flex_code;
8153   end if;
8154 
8155   Insert_Summary_Accounts
8156 	(p_worksheet_id => p_worksheet_id,
8157 	 p_rounding_factor => l_rounding_factor,
8158 	 p_stage_set_id => l_stage_set_id,
8159 	 p_current_stage_seq => l_current_stage_seq,
8160 	 p_set_of_books_id => l_set_of_books_id,
8161 	 p_flex_code => l_flex_code,
8162 	 p_budget_group_id => l_budget_group_id,
8163 	 p_budget_calendar_id => l_budget_calendar_id,
8164 	 p_return_status => l_return_status);
8165 
8166   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
8167     raise FND_API.G_EXC_ERROR;
8168   end if;
8169 
8170   -- Initialize API return status to success
8171   p_return_status := FND_API.G_RET_STS_SUCCESS;
8172 
8173 EXCEPTION
8174 
8175    when FND_API.G_EXC_ERROR then
8176      p_return_status := FND_API.G_RET_STS_ERROR;
8177      FND_MSG_PUB.Count_And_Get (p_count => l_msg_count,
8178 				p_data => l_msg_data);
8179 
8180    when FND_API.G_EXC_UNEXPECTED_ERROR then
8181      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8182      FND_MSG_PUB.Count_And_Get (p_count => l_msg_count,
8183 				p_data => l_msg_data);
8184 
8185    when OTHERS then
8186      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8187      if FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
8188        FND_MSG_PUB.Add_Exc_Msg (G_PKG_NAME,
8189 				l_api_name);
8190      end if;
8191      --
8192      FND_MSG_PUB.Count_And_Get (p_count => l_msg_count,
8193 				p_data => l_msg_data);
8194 END Create_Rollup_Totals;
8195 /*---------------------------------------------------------------------------*/
8196 
8197 
8198 /*---------------------------------------------------------------------------*/
8199 FUNCTION Get_Debug RETURN VARCHAR2 IS
8200 BEGIN
8201   return(g_dbug);
8202 END Get_Debug;
8203 /*---------------------------------------------------------------------------*/
8204 
8205 /* start bug 4256345 */
8206 PROCEDURE check_ccid_bal (x_return_status      out nocopy varchar2,
8207                           p_gl_budget_set_id   in  number)
8208 IS
8209 
8210 CURSOR l_ccid_csr
8211 IS
8212   SELECT code_combination_id
8213   FROM  psb_budget_accounts a,
8214         psb_set_relations r,
8215         psb_gl_budgets b,
8216         psb_gl_budget_sets c
8217   WHERE c.gl_budget_set_id  = p_gl_budget_set_id
8218   AND   c.gl_budget_set_id = b.gl_budget_set_id
8219   AND   b.gl_budget_id = r.gl_budget_id
8220   AND   r.account_position_set_id = a.account_position_set_id;
8221 
8222   -- declate a local table variable
8223   -- populate it using bulk fetch and then populate the global pl/sql table variable
8224   -- using a loop
8225   -- local variable (pl/sql table)
8226   l_ws_gl_budget_set_ccids g_bud_ccid_tbl_type;
8227 
8228 BEGIN
8229   x_return_status := FND_API.G_RET_STS_SUCCESS;
8230 
8231   -- clear the table before data is stored into the table
8232   IF g_ws_gl_budget_set_ccids.count > 0 THEN
8233     g_ws_gl_budget_set_ccids.delete;
8234   END IF;
8235 
8236   -- clear the local table also
8237   IF l_ws_gl_budget_set_ccids.COUNT > 0 THEN
8238     l_ws_gl_budget_set_ccids.DELETE;
8239   END IF;
8240 
8241   -- check if the cursor is open
8242   IF l_ccid_csr%ISOPEN THEN
8243     CLOSE l_ccid_csr;
8244   END IF;
8245 
8246   -- bulk collect the CCIDs into the pl/sql table
8247   OPEN l_ccid_csr;
8248   FETCH l_ccid_csr BULK COLLECT INTO l_ws_gl_budget_set_ccids;
8249   CLOSE l_ccid_csr;
8250 
8251   -- store ccids as index to the pl/sql table
8252   FOR loop_var in 1..l_ws_gl_budget_set_ccids.COUNT LOOP
8253     g_ws_gl_budget_set_ccids(l_ws_gl_budget_set_ccids(loop_var)) := l_ws_gl_budget_set_ccids(loop_var);
8254   END LOOP;
8255 
8256   -- commented as we are using a local pl/sql tabe
8257   /* FOR l_ccid_rec in l_ccid_csr LOOP
8258       g_ws_gl_budget_set_ccids(l_ccid_rec.code_combination_id) := l_ccid_rec.code_combination_id;
8259      END LOOP; */
8260 
8261 
8262 EXCEPTION
8263   WHEN others THEN
8264     fnd_file.put_line(fnd_file.log, sqlerrm);
8265     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8266 
8267     -- close the cursor if open
8268     IF l_ccid_csr%ISOPEN THEN
8269       CLOSE l_ccid_csr;
8270     END IF;
8271 END;
8272 /* end bug 4256345 */
8273 
8274 END PSB_WS_ACCT2;