DBA Data[Home] [Help]

PACKAGE BODY: APPS.PSB_POSITION_CONTROL_PVT

Source


1 PACKAGE BODY PSB_Position_Control_Pvt AS
2 /* $Header: PSBVWPCB.pls 120.30 2005/09/14 07:37:05 shtripat ship $ */
3 
4   G_PKG_NAME CONSTANT          VARCHAR2(30):= 'PSB_POSITION_CONTROL_PVT';
5 
6   -- for bug 4507389
7   g_year_start_date			   DATE;
8   g_year_end_date			   DATE;
9   -- for bug 4507389
10 
11   /* start bug 4545590 */
12   g_wks_no_date_overlap			BOOLEAN;
13   g_wks_new_hr_budget			BOOLEAN;
14   /* end bug 4545590 */
15 
16 
17   TYPE g_fte_rec_type IS RECORD
18      ( position_fte_line_id  NUMBER,
19        position_id           NUMBER,
20        start_date            DATE,
21        end_date              DATE,
22        fte                   NUMBER,
23        budget_revision_id    NUMBER,
24        base_line_version     VARCHAR2(1),
25        delete_flag           BOOLEAN);
26 
27   TYPE g_costs_rec_type IS RECORD
28      ( position_element_line_id  NUMBER,
29        position_id               NUMBER,
30        pay_element_id            NUMBER,
31        start_date                DATE,
32        end_date                  DATE,
33        currency_code             VARCHAR2(15),
34        element_cost              NUMBER,
35        budget_revision_id        NUMBER,
36        base_line_version         VARCHAR2(1),
37        delete_flag               BOOLEAN);
38 
39   TYPE g_accounts_rec_type IS RECORD
40      ( position_account_line_id  NUMBER,
41        position_id               NUMBER,
42        start_date                DATE,
43        end_date                  DATE,
44        code_combination_id       NUMBER,
45        budget_group_id           NUMBER,
46        currency_code             VARCHAR2(15),
47        amount                    NUMBER,
48        budget_revision_id        NUMBER,
49        base_line_version         VARCHAR2(1),
50        delete_flag               BOOLEAN);
51 
52   TYPE g_fte_tbl_type IS TABLE OF g_fte_rec_type
53      INDEX BY BINARY_INTEGER;
54 
55   TYPE g_costs_tbl_type IS TABLE OF g_costs_rec_type
56      INDEX BY BINARY_INTEGER;
57 
58   TYPE g_accounts_tbl_type IS TABLE OF g_accounts_rec_type
59      INDEX BY BINARY_INTEGER;
60 
61   TYPE g_map_rec_type IS RECORD
62     (gl_segment_name       VARCHAR2(30),
63      cost_segment_name     VARCHAR2(30),
64      segment_value         VARCHAR2(100));
65 
66   TYPE g_map_tbl_type IS TABLE OF g_map_rec_type
67     INDEX BY BINARY_INTEGER;
68 
69   TYPE g_element_cost_rec_type IS RECORD
70      ( budget_set_id           NUMBER,
71        pay_element_id          NUMBER,
72        element_cost            NUMBER );
73 
74   TYPE g_element_cost_tbl_type IS TABLE OF g_element_cost_rec_type
75       INDEX BY BINARY_INTEGER;
76 
77   g_element_costs              g_element_cost_tbl_type;
78   g_num_element_costs          NUMBER;
79 
80   TYPE g_element_dist_rec_type IS RECORD
81     ( pay_element_id NUMBER,
82       ccid           NUMBER,
83       percent        NUMBER );
84 
85   TYPE g_element_dist_tbl_type IS TABLE OF g_element_dist_rec_type
86       INDEX BY BINARY_INTEGER;
87 
88   g_element_dists              g_element_dist_tbl_type;
89   g_num_element_dists          NUMBER;
90 
91   TYPE g_budgetset_dist_rec_type IS RECORD
92     ( budget_set_id  NUMBER,
93       ccid           NUMBER,
94       amount         NUMBER,
95       percent        NUMBER );
96 
97   TYPE g_budgetset_dist_tbl_type IS TABLE OF g_budgetset_dist_rec_type
98       INDEX BY BINARY_INTEGER;
99 
100   g_budgetset_dists            g_budgetset_dist_tbl_type;
101   g_num_budgetset_dists        NUMBER;
102 
103   g_fte                        g_fte_tbl_type;
104   g_num_fte                    NUMBER;
105 
106   g_costs                      g_costs_tbl_type;
107   g_num_costs                  NUMBER;
108 
109   g_accounts                   g_accounts_tbl_type;
110   g_num_accounts               NUMBER;
111 
112   g_map_tab                    g_map_tbl_type;
113 
114   -- Added for Bug#2434152
115   g_map_str                    VARCHAR2(8000);
116 
117   /* bug no 3670254 */
118   -- These global variables will have the
119   -- profile and attribute id values
120   g_hrms_fte_upload_option     VARCHAR2(30);
121   g_fte_attribute_id	       NUMBER;
122   /* bug no 3670254 */
123 
124   -- TokNameArray contains names of all tokens
125   TYPE TokNameArray IS TABLE OF VARCHAR2(100) INDEX BY BINARY_INTEGER;
126 
127   -- TokValArray contains values for all tokens
128   TYPE TokValArray IS TABLE OF VARCHAR2(1000) INDEX BY BINARY_INTEGER;
129 
130   -- For Message Tokens
131   no_msg_tokens                NUMBER := 0;
132   msg_tok_names                TokNameArray;
133   msg_tok_val                  TokValArray;
134 
135   /* bug no 3670254 */
136   -- cursor for getting last active assignment FTE
137   -- cursor used in API's create_pqh_budget_version
138   -- and create_pqh_budget_detail to fetch FTE.
139   CURSOR g_ass_fte_csr (c_data_extract_id       IN  NUMBER ,
140                         c_position_id           IN  NUMBER ,
141                         c_worksheet_id          IN  NUMBER ,
142                         c_attribute_id          IN  NUMBER ,
143                         c_budget_year_end_date  IN  DATE
144                        ) IS
145   SELECT ppf.attribute_Value
146   FROM   psb_position_assignments ppf
147   WHERE  ppf.data_extract_id = c_data_extract_id
148   AND    ppf.position_id = c_position_id
149   AND    nvl(worksheet_id, -1)  = nvl(c_worksheet_id, -1)
150   AND    ppf.attribute_id = c_attribute_Id
151   AND    c_budget_year_end_date BETWEEN effective_start_date AND
152                                 nvl(effective_end_date, c_budget_year_end_date);
153   /* bug no 3670254 */
154 
155 
156 /*===========================================================================+
157  |                             PROCEDURE pd                                  |
158  +===========================================================================*/
159 --
160 -- API to print debug information, used during only development.
161 --
162 PROCEDURE pd( p_message   IN     VARCHAR2)
163 IS
164 BEGIN
165   NULL ;
166   --DBMS_OUTPUT.Put_Line(p_message) ;
167 END pd ;
168 /*---------------------------------------------------------------------------*/
169 
170 
171 /* ----------------------------------------------------------------------- */
172 
173 /*For Bug No : 1822364 Start*/
174 -- Upload Element Position Set Groups
175 PROCEDURE Copy_Position_Set_Groups
176 ( p_return_status           OUT  NOCOPY  VARCHAR2,
177   p_old_pay_element_id      IN   NUMBER,
178   p_new_pay_element_id      IN   NUMBER,
179   p_target_data_extract_id  IN   NUMBER,
180   p_budget_group_id         IN   NUMBER,
181   p_flex_mapping_set_id     IN   NUMBER,
182   p_follow_salary           IN   VARCHAR2
183 );
184 
185 
186 /*-------------------------------------------------------------------------*/
187 -- Upload Position Sets that belong to Position set group
188 
189 PROCEDURE Copy_Position_Sets
190 ( p_return_status           OUT  NOCOPY  VARCHAR2,
191   p_old_psg_id              IN   NUMBER,
192   p_new_psg_id              IN   NUMBER,
193   p_target_data_extract_id  IN   NUMBER,
194   p_budget_group_id         IN   NUMBER
195 );
196 
197 /*-------------------------------------------------------------------------*/
198 
199 PROCEDURE Copy_Position_Set_Lines
200 ( p_return_status           OUT  NOCOPY  VARCHAR2,
201   p_old_posset_id           IN   NUMBER,
202   p_new_posset_id           IN   NUMBER,
203   p_target_data_extract_id  IN   NUMBER
204 );
205 
206 /* ------------------------------------------------------------------------*/
207 
208 PROCEDURE Copy_Element_Distributions
209 ( p_return_status           OUT  NOCOPY  VARCHAR2,
210   p_old_psg_id              IN   NUMBER,
211   p_new_psg_id              IN   NUMBER,
212   p_flex_mapping_set_id     IN   NUMBER,
213   p_follow_salary           IN   VARCHAR2
214 );
215 
216 /*-------------------------------------------------------------------------*/
217 /*For Bug No : 1822364 End*/
218 
219 
220 -- Add Token and Value to the Message Token array
221 
222 PROCEDURE message_token(tokname IN VARCHAR2,
223 			tokval  IN VARCHAR2) IS
224 
225 BEGIN
226 
227   if no_msg_tokens is null then
228     no_msg_tokens := 1;
229   else
230     no_msg_tokens := no_msg_tokens + 1;
231   end if;
232 
233   msg_tok_names(no_msg_tokens) := tokname;
234   msg_tok_val(no_msg_tokens) := tokval;
235 
236 END message_token;
237 
238 /* ----------------------------------------------------------------------- */
239 
240 -- Define a Message Token with a Value and set the Message Name
241 
242 -- Calls FND_MESSAGE server package to set the Message Stack. This message is
243 -- retrieved by the calling program.
244 
245 PROCEDURE add_message(appname IN VARCHAR2,
246 		      msgname IN VARCHAR2) IS
247 
248   i  BINARY_INTEGER;
249 
250 BEGIN
251 
252   if ((appname is not null) and
253       (msgname is not null)) then
254 
255     FND_MESSAGE.SET_NAME(appname, msgname);
256 
257     if no_msg_tokens is not null then
258 
259       for i in 1..no_msg_tokens loop
260 	FND_MESSAGE.SET_TOKEN(msg_tok_names(i), msg_tok_val(i));
261       end loop;
262 
263     end if;
264 
265     FND_MSG_PUB.Add;
266 
267   end if;
268 
269   -- Clear Message Token stack
270 
271   no_msg_tokens := 0;
272 
273 END add_message;
274 
275 /* ----------------------------------------------------------------------- */
276 
277 PROCEDURE Upload_Attribute_Values
278 ( p_return_status             OUT  NOCOPY  VARCHAR2,
279   p_source_data_extract_id    IN   NUMBER,
280   p_source_business_group_id  IN   NUMBER,
281   p_target_data_extract_id    IN   NUMBER
282 ) IS
283 
284   l_attrval_already_exists    BOOLEAN;
285 
286   l_attribute_value_id        NUMBER;
287 
288   l_msg_count                 NUMBER;
289   l_msg_data                  VARCHAR2(2000);
290   l_rowid                     ROWID;
291 
292   l_return_status             VARCHAR2(1);
293 
294   cursor c_attrval_seq is
295     select PSB_ATTRIBUTE_VALUES_S.NEXTVAL seq
296       from dual;
297 
298   cursor c_attrval is
299     select *
300       from PSB_ATTRIBUTE_VALUES a
301      where data_extract_id = p_source_data_extract_id
302        and exists
303 	  (select 1 from PSB_ATTRIBUTES_VL b
304 	    where b.attribute_id = a.attribute_id
305 	      and b.business_group_id = p_source_business_group_id)
306        and attribute_value is not null;
307 
308   cursor c_attrval_exists (attrid NUMBER, attrval VARCHAR2) is
309     select attribute_value_id
310       from PSB_ATTRIBUTE_VALUES
311      where attribute_value = attrval
312        and attribute_id = attrid
313        and data_extract_id = p_target_data_extract_id;
314 
315 BEGIN
316 
317   -- Upload attribute values if required
318 
319   for c_attrval_rec in c_attrval loop
320 
321     l_attrval_already_exists := FALSE;
322 
323     -- Check if attribute value is already defined for target data extract
324 
325     for c_attrval_exists_rec in c_attrval_exists (c_attrval_rec.attribute_id, c_attrval_rec.attribute_value) loop
326       l_attribute_value_id := c_attrval_exists_rec.attribute_value_id;
327       l_attrval_already_exists := TRUE;
328     end loop;
329 
330     if not l_attrval_already_exists then
331     begin
332 
333       for c_attrval_seq_rec in c_attrval_seq loop
334 	l_attribute_value_id := c_attrval_seq_rec.seq;
335       end loop;
336 
337       PSB_ATTRIBUTE_VALUES_PVT.Insert_Row
338 	 (p_api_version => 1.0,
339 	  p_return_status => l_return_status,
340 	  p_msg_count => l_msg_count,
341 	  p_msg_data => l_msg_data,
342 	  p_rowid => l_rowid,
343 	  p_attribute_value_id => l_attribute_value_id,
344 	  p_attribute_id => c_attrval_rec.attribute_id,
345 	  p_attribute_value => c_attrval_rec.attribute_value,
346 	  p_description => c_attrval_rec.description,
347 	  p_hr_value_id => c_attrval_rec.hr_value_id,
348 	  p_data_extract_id => p_target_data_extract_id,
349 	  p_attribute1 => c_attrval_rec.attribute1,
350 	  p_attribute2 => c_attrval_rec.attribute2,
351 	  p_attribute3 => c_attrval_rec.attribute3,
352 	  p_attribute4 => c_attrval_rec.attribute4,
353 	  p_attribute5 => c_attrval_rec.attribute5,
354 	  p_attribute6 => c_attrval_rec.attribute6,
355 	  p_attribute7 => c_attrval_rec.attribute7,
356 	  p_attribute8 => c_attrval_rec.attribute8,
357 	  p_attribute9 => c_attrval_rec.attribute9,
358 	  p_attribute10 => c_attrval_rec.attribute10,
359 	  p_attribute11 => c_attrval_rec.attribute11,
360 	  p_attribute12 => c_attrval_rec.attribute12,
361 	  p_attribute13 => c_attrval_rec.attribute13,
362 	  p_attribute14 => c_attrval_rec.attribute14,
363 	  p_attribute15 => c_attrval_rec.attribute15,
364 	  p_attribute16 => c_attrval_rec.attribute16,
365 	  p_attribute17 => c_attrval_rec.attribute17,
366 	  p_attribute18 => c_attrval_rec.attribute18,
367 	  p_attribute19 => c_attrval_rec.attribute19,
368 	  p_attribute20 => c_attrval_rec.attribute20,
369 	  p_attribute21 => c_attrval_rec.attribute21,
370 	  p_attribute22 => c_attrval_rec.attribute22,
371 	  p_attribute23 => c_attrval_rec.attribute23,
372 	  p_attribute24 => c_attrval_rec.attribute24,
373 	  p_attribute25 => c_attrval_rec.attribute25,
374 	  p_attribute26 => c_attrval_rec.attribute26,
375 	  p_attribute27 => c_attrval_rec.attribute27,
376 	  p_attribute28 => c_attrval_rec.attribute28,
377 	  p_attribute29 => c_attrval_rec.attribute29,
378 	  p_attribute30 => c_attrval_rec.attribute30,
379 	  p_context => c_attrval_rec.context,
380 	  p_last_update_date => sysdate,
381 	  p_last_updated_by => FND_GLOBAL.USER_ID,
382 	  p_last_update_login => FND_GLOBAL.LOGIN_ID,
383 	  p_created_by => FND_GLOBAL.USER_ID,
384 	  p_creation_date => sysdate);
385 
386       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
387 	raise FND_API.G_EXC_ERROR;
388       end if;
389 
390     end;
391     else
392       /* Added the else part on 07/26/01. If the attribute value already exists
393 	 then update the with the current value. Changes done by Siva */
394     begin
395       PSB_ATTRIBUTE_VALUES_PVT.Update_Row
396 	 (p_api_version => 1.0,
397 	  p_init_msg_list => null,
398 	  p_commit => null,
399 	  p_validation_level => null,
400 	  p_return_status => l_return_status,
401 	  p_msg_count => l_msg_count,
402 	  p_msg_data => l_msg_data,
403 	  p_attribute_value_id => l_attribute_value_id,
404 	  p_attribute_id => c_attrval_rec.attribute_id,
405 	  p_attribute_value => c_attrval_rec.attribute_value,
406 	  p_description => c_attrval_rec.description,
407 	  p_hr_value_id => c_attrval_rec.hr_value_id,
408 	  p_data_extract_id => p_target_data_extract_id,
409 	  p_attribute1 => c_attrval_rec.attribute1,
410 	  p_attribute2 => c_attrval_rec.attribute2,
411 	  p_attribute3 => c_attrval_rec.attribute3,
412 	  p_attribute4 => c_attrval_rec.attribute4,
413 	  p_attribute5 => c_attrval_rec.attribute5,
414 	  p_attribute6 => c_attrval_rec.attribute6,
415 	  p_attribute7 => c_attrval_rec.attribute7,
416 	  p_attribute8 => c_attrval_rec.attribute8,
417 	  p_attribute9 => c_attrval_rec.attribute9,
418 	  p_attribute10 => c_attrval_rec.attribute10,
419 	  p_attribute11 => c_attrval_rec.attribute11,
420 	  p_attribute12 => c_attrval_rec.attribute12,
421 	  p_attribute13 => c_attrval_rec.attribute13,
422 	  p_attribute14 => c_attrval_rec.attribute14,
423 	  p_attribute15 => c_attrval_rec.attribute15,
424 	  p_attribute16 => c_attrval_rec.attribute16,
425 	  p_attribute17 => c_attrval_rec.attribute17,
426 	  p_attribute18 => c_attrval_rec.attribute18,
427 	  p_attribute19 => c_attrval_rec.attribute19,
428 	  p_attribute20 => c_attrval_rec.attribute20,
429 	  p_attribute21 => c_attrval_rec.attribute21,
430 	  p_attribute22 => c_attrval_rec.attribute22,
431 	  p_attribute23 => c_attrval_rec.attribute23,
432 	  p_attribute24 => c_attrval_rec.attribute24,
433 	  p_attribute25 => c_attrval_rec.attribute25,
434 	  p_attribute26 => c_attrval_rec.attribute26,
435 	  p_attribute27 => c_attrval_rec.attribute27,
436 	  p_attribute28 => c_attrval_rec.attribute28,
437 	  p_attribute29 => c_attrval_rec.attribute29,
438 	  p_attribute30 => c_attrval_rec.attribute30,
439 	  p_context => c_attrval_rec.context,
440 	  p_last_update_date => sysdate,
441 	  p_last_updated_by => FND_GLOBAL.USER_ID,
442 	  p_last_update_login => FND_GLOBAL.LOGIN_ID);
443 
444       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
445 	raise FND_API.G_EXC_ERROR;
446       end if;
447 
448     end;
449     end if;
450 
451   end loop;
452 
453   -- Initialize API return status to success
454   p_return_status := FND_API.G_RET_STS_SUCCESS;
455 
456 EXCEPTION
457 
458    when FND_API.G_EXC_ERROR then
459      p_return_status := FND_API.G_RET_STS_ERROR;
460 
461    when FND_API.G_EXC_UNEXPECTED_ERROR then
462      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
463 
464    when OTHERS then
465      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
466 
467 END Upload_Attribute_Values;
468 
469 /* ----------------------------------------------------------------------- */
470 
471 PROCEDURE Upload_Element
472 ( p_return_status           OUT  NOCOPY  VARCHAR2,
473   p_source_data_extract_id  IN   NUMBER,
474   p_target_data_extract_id  IN   NUMBER,
475   p_pay_element_id          IN   NUMBER
476 ) IS
477 
478   l_pay_element_id          NUMBER;
479   l_rowid                   ROWID;
480   lp_rowid                  ROWID;
481   l_pay_element_option_id   NUMBER;
482   l_attribute_value_id      NUMBER;
483   l_position_set_group_id   NUMBER;
484   l_position_set_id         NUMBER;
485   l_line_sequence_id        NUMBER;
486   l_value_sequence_id       NUMBER;
487   l_set_relation_id         NUMBER;
488   l_distribution_id         NUMBER;
489 
490   l_mapped_ccid             NUMBER;
491   l_budget_year_type_id     NUMBER;
492 
493   l_year_start_date         DATE;
494   l_year_end_date           DATE;
495 
496   l_element_exists          BOOLEAN;
497   l_element_option_exists   BOOLEAN;
498   l_position_set_exists     BOOLEAN;
499   l_line_sequence_exists    BOOLEAN;
500   l_value_sequence_exists   BOOLEAN;
501 
502   l_msg_count               NUMBER;
503   l_msg_data                VARCHAR2(2000);
504   l_return_status           VARCHAR2(1);
505 
506 /* Bug No 2579818 Start
507 -- moving this cursor to copy_elements procedure
508   l_business_group_id       NUMBER;
509 
510   cursor c_extract is
511     select business_group_id
512       from PSB_DATA_EXTRACTS
513      where data_extract_id = p_source_data_extract_id;
514  Bug No 2579818 End */
515 
516   cursor c_elem is
517     select *
518       from PSB_PAY_ELEMENTS
519      where pay_element_id = p_pay_element_id;
520 
521   cursor c_elemname_exists (elemname VARCHAR2) is
522     select pay_element_id, rowid
523       from PSB_PAY_ELEMENTS
524      where name = elemname
525        and data_extract_id = p_target_data_extract_id;
526 
527   cursor c_elemoptions (elemid NUMBER) is
528     select *
529       from PSB_PAY_ELEMENT_OPTIONS
530      where pay_element_id = elemid;
531 
532   cursor c_elemoptions_exists (elemid NUMBER, optname VARCHAR2) is
533     select pay_element_option_id
534       from PSB_PAY_ELEMENT_OPTIONS
535      where name = optname
536        and pay_element_id = elemid;
537 
538   cursor c_elemrates (elemid NUMBER, elemoptionid NUMBER) is
539     select *
540       from PSB_PAY_ELEMENT_RATES
541      where worksheet_id is null
542        and pay_element_option_id = elemoptionid
543        and pay_element_id = elemid;
544 
545   cursor c_elemrates_nooptions (elemid NUMBER) is
546     select *
547       from PSB_PAY_ELEMENT_RATES
548      where worksheet_id is null
549        and pay_element_id = elemid;
550 
551   cursor c_possetgrp (elemid NUMBER) is
552     select *
553       from PSB_ELEMENT_POS_SET_GROUPS
554      where pay_element_id = elemid;
555 
556   cursor c_setrel (possetgrpid NUMBER) is
557     select *
558       from PSB_SET_RELATIONS
559      where position_set_group_id = possetgrpid;
560 
561   cursor c_posset (possetid NUMBER) is
562     select *
563       from PSB_ACCOUNT_POSITION_SETS
564      where account_position_set_id = possetid;
565 
566   cursor c_possetline (possetid NUMBER) is
567     select *
568       from PSB_ACCOUNT_POSITION_SET_LINES
569      where account_position_set_id = possetid;
570 
571   cursor c_possetlineval (lineseqid NUMBER) is
572     select *
573       from PSB_POSITION_SET_LINE_VALUES
574      where line_sequence_id = lineseqid;
575 
576   cursor c_posset_exists (possetname VARCHAR2) is
577     select account_position_set_id, rowid
578       from PSB_ACCOUNT_POSITION_SETS
579      where data_extract_id = p_target_data_extract_id
580        and account_or_position_type = 'P'
581        and name = possetname;
582 
583   cursor c_possetlineval_exists (lineseqid NUMBER, attrval VARCHAR2) is
584     select value_sequence_id
585       from PSB_POSITION_SET_LINE_VALUES
586      where attribute_value = attrval
587        and line_sequence_id = lineseqid;
588 
589   cursor c_re_attrval (attrvalid NUMBER) is
590     select a.attribute_value_id
591       from PSB_ATTRIBUTE_VALUES a,
592 	   PSB_ATTRIBUTE_VALUES b
593      where a.data_extract_id = p_target_data_extract_id
594        and a.attribute_value = b.attribute_value
595        and a.attribute_id    = b.attribute_id -- added for Bug#4262388
596        and b.attribute_value_id = attrvalid;
597 
598   cursor c_possetline_exists (possetid NUMBER, attrid NUMBER) is
599     select line_sequence_id
600       from PSB_ACCOUNT_POSITION_SET_LINES
601      where attribute_id = attrid
602        and account_position_set_id = possetid;
603 
604   cursor c_elemdist (possetgrpid NUMBER) is
605     select *
606       from PSB_PAY_ELEMENT_DISTRIBUTIONS
607      where position_set_group_id = possetgrpid;
608 
609   cursor c_elem_seq is
610     select psb_pay_elements_s.nextval seq
611       from dual;
612 
613   cursor c_elemoptions_seq is
614     select psb_pay_element_options_s.nextval seq
615       from dual;
616 
617   cursor c_posset_seq is
618     select PSB_ACCOUNT_POSITION_SETS_S.NEXTVAL seq
619       from dual;
620 
621   cursor c_possetline_seq is
622     select PSB_ACCT_POSITION_SET_LINES_S.NEXTVAL seq
623       from dual;
624 
625   cursor c_possetlineval_seq is
626     select PSB_POSITION_SET_LINE_VALUES_S.NEXTVAL seq
627       from dual;
628 
629   cursor c_setrel_seq is
630     select PSB_SET_RELATIONS_S.NEXTVAL seq
631       from dual;
632 
633   cursor c_elempossetgrp_seq is
634     select PSB_ELEMENT_POS_SET_GROUPS_S.NEXTVAL seq
635       from dual;
636 
637   cursor c_elemdist_seq is
638     select PSB_PAY_ELEMENT_DISTRIBUTION_S.NEXTVAL seq
639       from dual;
640 
641 BEGIN
642 
643 /* Bug No 2579818 Start
644 -- commented since calling it from copy_elements
645   for c_extract_rec in c_extract loop
646     l_business_group_id := c_extract_rec.business_group_id;
647   end loop;
648 
649   Upload_Attribute_Values
650 	(p_return_status => l_return_status,
651 	 p_source_data_extract_id => p_source_data_extract_id,
652 	 p_source_business_group_id => l_business_group_id,
653 	 p_target_data_extract_id => p_target_data_extract_id);
654 
655   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
656     raise FND_API.G_EXC_ERROR;
657   end if;
658  Bug No 2579818 End */
659 
660   for c_elem_rec in c_elem loop
661 
662     l_element_exists := FALSE;
663 
664     -- Check if element name exists in the target data extract
665 
666     for c_elemname_exists_rec in c_elemname_exists (c_elem_rec.name) loop
667       l_pay_element_id := c_elemname_exists_rec.pay_element_id;
668       l_rowid := c_elemname_exists_rec.rowid;
669       l_element_exists := TRUE;
670     end loop;
671 
672     if not l_element_exists then
673     begin
674 
675       for c_elem_seq_rec in c_elem_seq loop
676 	l_pay_element_id := c_elem_seq_rec.seq;
677       end loop;
678 
679       PSB_PAY_ELEMENTS_PVT.INSERT_ROW
680 	 (p_api_version => 1.0,
681 	  p_return_status => l_return_status,
682 	  p_msg_count => l_msg_count,
683 	  p_msg_data => l_msg_data,
684 	  p_row_id => l_rowid,
685 	  p_pay_element_id => l_pay_element_id,
686 	  p_budget_set_id => c_elem_rec.budget_set_id,
687 	  p_business_group_id => c_elem_rec.business_group_id,
688 	  p_data_extract_id => p_target_data_extract_id,
689 	  p_name => c_elem_rec.name,
690 	  p_description => c_elem_rec.description,
691 	  p_element_value_type => c_elem_rec.element_value_type,
692 	  p_formula_id => c_elem_rec.formula_id,
693 	  p_overwrite_flag => c_elem_rec.overwrite_flag,
694 	  p_required_flag => c_elem_rec.required_flag,
695 	  p_follow_salary => c_elem_rec.follow_salary,
696 	  p_pay_basis => c_elem_rec.pay_basis,
697 	  p_start_date => c_elem_rec.start_date,
698 	  p_end_date => c_elem_rec.end_date,
699 	  p_processing_type => c_elem_rec.processing_type,
700 	  p_period_type => c_elem_rec.period_type,
701 	  p_process_period_type => c_elem_rec.process_period_type,
702 	  p_max_element_value_type => c_elem_rec.max_element_value_type,
703 	  p_max_element_value => c_elem_rec.max_element_value,
704 	  p_salary_flag => c_elem_rec.salary_flag,
705 	  p_salary_type => c_elem_rec.salary_type,
706 	  p_option_flag => c_elem_rec.option_flag,
707 	  p_hr_element_type_id => c_elem_rec.hr_element_type_id,
708 	  p_attribute_category => c_elem_rec.attribute_category,
709 	  p_attribute1 => c_elem_rec.attribute1,
710 	  p_attribute2 => c_elem_rec.attribute2,
711 	  p_attribute3 => c_elem_rec.attribute3,
712 	  p_attribute4 => c_elem_rec.attribute4,
713 	  p_attribute5 => c_elem_rec.attribute5,
714 	  p_attribute6 => c_elem_rec.attribute6,
715 	  p_attribute7 => c_elem_rec.attribute7,
716 	  p_attribute8 => c_elem_rec.attribute8,
717 	  p_attribute9 => c_elem_rec.attribute9,
718 	  p_attribute10 => c_elem_rec.attribute10,
719 	  p_last_update_date => sysdate,
720 	  p_last_updated_by => FND_GLOBAL.USER_ID,
721 	  p_last_update_login => FND_GLOBAL.LOGIN_ID,
722 	  p_created_by => FND_GLOBAL.USER_ID,
723 	  p_creation_date => sysdate);
724 
725       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
726 	raise FND_API.G_EXC_ERROR;
727       end if;
728 
729       if c_elem_rec.option_flag = 'Y' then
730       begin
731 
732 	for c_elemoptions_rec in c_elemoptions(c_elem_rec.pay_element_id) loop
733 
734 	  for c_elemoptions_seq_rec in c_elemoptions_seq loop
735 	    l_pay_element_option_id := c_elemoptions_seq_rec.seq;
736 	  end loop;
737 
738 	  PSB_PAY_ELEMENT_OPTIONS_PVT.INSERT_ROW
739 	     (p_api_version => 1.0,
740 	      p_return_status => l_return_status,
741 	      p_msg_count => l_msg_count,
742 	      p_msg_data => l_msg_data,
743 	      p_pay_element_option_id => l_pay_element_option_id,
744 	      p_pay_element_id => l_pay_element_id,
745 	      p_name => c_elemoptions_rec.name,
746 	      p_grade_step => c_elemoptions_rec.grade_step,
747 	      p_sequence_number => c_elemoptions_rec.sequence_number,
748 	      p_last_update_date => sysdate,
749 	      p_last_updated_by => FND_GLOBAL.USER_ID,
750 	      p_last_update_login => FND_GLOBAL.LOGIN_ID,
751 	      p_created_by => FND_GLOBAL.USER_ID,
752 	      p_creation_date => sysdate);
753 
754 	  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
755 	    raise FND_API.G_EXC_ERROR;
756 	  end if;
757 
758 	  for c_elemrates_rec in c_elemrates (c_elem_rec.pay_element_id, c_elemoptions_rec.pay_element_option_id) loop
759 	    PSB_PAY_ELEMENT_RATES_PVT.Modify_Element_Rates
760 	       (p_api_version => 1.0,
761 		p_return_status => l_return_status,
762 		p_msg_count => l_msg_count,
763 		p_msg_data => l_msg_data,
764 		p_pay_element_id => l_pay_element_id,
765 		p_pay_element_option_id => l_pay_element_option_id,
766 		p_effective_start_date => c_elemrates_rec.effective_start_date,
767 		p_effective_end_date => c_elemrates_rec.effective_end_date,
768 		p_worksheet_id => null,
769 		p_element_value_type => c_elemrates_rec.element_value_type,
770 		p_element_value => c_elemrates_rec.element_value,
771 		p_pay_basis  => c_elemrates_rec.pay_basis,
772 		p_formula_id => c_elemrates_rec.formula_id,
773 		p_maximum_value => c_elemrates_rec.maximum_value,
774 		p_mid_value => c_elemrates_rec.mid_value,
775 		p_minimum_value => c_elemrates_rec.minimum_value,
776 		p_currency_code => c_elemrates_rec.currency_code);
777 
778 	    if l_return_status <> FND_API.G_RET_STS_SUCCESS then
779 	      raise FND_API.G_EXC_ERROR;
780 	    end if;
781 
782 	  end loop;
783 
784 	end loop;
785 
786       end;
787       else /* Elements without options */
788       begin
789 
790 	for c_elemrates_rec in c_elemrates_nooptions (c_elem_rec.pay_element_id) loop
791 
792 	  PSB_PAY_ELEMENT_RATES_PVT.Modify_Element_Rates
793 	     (p_api_version => 1.0,
794 	      p_return_status => l_return_status,
795 	      p_msg_count => l_msg_count,
796 	      p_msg_data => l_msg_data,
797 	      p_pay_element_id => l_pay_element_id,
798 	      p_pay_element_option_id => NULL,
799 	      p_effective_start_date => c_elemrates_rec.effective_start_date,
800 	      p_effective_end_date => c_elemrates_rec.effective_end_date,
801 	      p_worksheet_id => null,
802 	      p_element_value_type => c_elemrates_rec.element_value_type,
803 	      p_element_value => c_elemrates_rec.element_value,
804 	      p_pay_basis  => c_elemrates_rec.pay_basis,
805 	      p_formula_id => c_elemrates_rec.formula_id,
806 	      p_maximum_value => c_elemrates_rec.maximum_value,
807 	      p_mid_value => c_elemrates_rec.mid_value,
808 	      p_minimum_value => c_elemrates_rec.minimum_value,
809 	      p_currency_code => c_elemrates_rec.currency_code);
810 
811 	  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
812 	    raise FND_API.G_EXC_ERROR;
813 	  end if;
814 
815 	end loop;
816 
817       end; /* Elements without options */
818       end if;
819 
820       -- Upload Element Position Set Groups
821 
822       for c_possetgrp_rec in c_possetgrp (c_elem_rec.pay_element_id) loop
823 
824 	for c_elempossetgrp_seq_rec in c_elempossetgrp_seq loop
825 	  l_position_set_group_id := c_elempossetgrp_seq_rec.seq;
826 	end loop;
827 
828 	PSB_ELEMENT_POS_SET_GROUPS_PVT.Insert_Row
829 	   (p_api_version => 1.0,
830 	    p_return_status => l_return_status,
831 	    p_msg_count => l_msg_count,
832 	    p_msg_data => l_msg_data,
833 	    p_position_set_group_id => l_position_set_group_id,
834 	    p_pay_element_id => l_pay_element_id,
835 	    p_name => c_possetgrp_rec.name,
836 	    p_last_update_date => sysdate,
837 	    p_last_updated_by => FND_GLOBAL.USER_ID,
838 	    p_last_update_login => FND_GLOBAL.LOGIN_ID,
839 	    p_created_by => FND_GLOBAL.USER_ID,
840 	    p_creation_date => sysdate);
841 
842 	if l_return_status <> FND_API.G_RET_STS_SUCCESS then
843 	  raise FND_API.G_EXC_ERROR;
844 	end if;
845 
846 	-- Upload all Set Relations for the Position Set Group
847 
848 	for c_setrel_rec in c_setrel (c_possetgrp_rec.position_set_group_id) loop
849 
850 	  for c_posset_rec in c_posset (c_setrel_rec.account_position_set_id) loop
851 
852 	    l_position_set_exists := FALSE;
853 
854 	    lp_rowid := null;
855 
856 	    for c_posset_exists_rec in c_posset_exists (c_posset_rec.name) loop
857 	      l_position_set_id := c_posset_exists_rec.account_position_set_id;
858 	      lp_rowid := c_posset_exists_rec.rowid;
859 	      l_position_set_exists := TRUE;
860 	    end loop;
861 
862 	    if not l_position_set_exists then
863 	    begin
864 
865 	      for c_posset_seq_rec in c_posset_seq loop
866 		l_position_set_id := c_posset_seq_rec.seq;
867 	      end loop;
868 
869 	      PSB_ACCOUNT_POSITION_SET_PVT.Insert_Row
870 		 (p_api_version => 1.0,
871 		  p_return_status => l_return_status,
872 		  p_msg_count => l_msg_count,
873 		  p_msg_data => l_msg_data,
874 		  p_row_id => l_rowid,
875 		  p_account_position_set_id => l_position_set_id,
876 		  p_name => c_posset_rec.name,
877 		  p_set_of_books_id => c_posset_rec.set_of_books_id,
878 		  p_use_in_budget_group_flag => c_posset_rec.use_in_budget_group_flag,
879 		  p_data_extract_id => p_target_data_extract_id,
880 		  p_global_or_local_type => c_posset_rec.global_or_local_type,
881 		  p_account_or_position_type =>c_posset_rec.account_or_position_type,
882 		  p_attribute_selection_type => c_posset_rec.attribute_selection_type,
883 		  p_business_group_id => c_posset_rec.business_group_id,
884 		  p_last_update_date => sysdate,
885 		  p_last_updated_by => FND_GLOBAL.USER_ID,
886 		  p_last_update_login => FND_GLOBAL.LOGIN_ID,
887 		  p_created_by => FND_GLOBAL.USER_ID,
888 		  p_creation_date => sysdate);
889 
890 	      if l_return_status <> FND_API.G_RET_STS_SUCCESS then
891 		raise FND_API.G_EXC_ERROR;
892 	      end if;
893 
894 	      for c_possetline_rec in c_possetline (c_posset_rec.account_position_set_id) loop
895 
896 		for c_possetline_seq_rec in c_possetline_seq loop
897 		  l_line_sequence_id := c_possetline_seq_rec.seq;
898 		end loop;
899 
900 		PSB_ACCT_POSITION_SET_LINE_PVT.Insert_Row
901 		   (p_api_version => 1.0,
902 		    p_return_status => l_return_status,
903 		    p_msg_count => l_msg_count,
904 		    p_msg_data => l_msg_data,
905 		    p_row_id => l_rowid,
906 		    p_line_sequence_id => l_line_sequence_id,
907 		    p_account_position_set_id => l_position_set_id,
908 		    p_description => c_possetline_rec.description,
909 		    p_business_group_id => c_possetline_rec.business_group_id,
910 		    p_attribute_id => c_possetline_rec.attribute_id,
911 		    p_include_or_exclude_type => c_possetline_rec.include_or_exclude_type,
912 		    p_segment1_low => c_possetline_rec.segment1_low,
913 		    p_segment2_low => c_possetline_rec.segment2_low,
914 		    p_segment3_low => c_possetline_rec.segment3_low,
915 		    p_segment4_low => c_possetline_rec.segment4_low,
916 		    p_segment5_low => c_possetline_rec.segment5_low,
917 		    p_segment6_low => c_possetline_rec.segment6_low,
918 		    p_segment7_low => c_possetline_rec.segment7_low,
919 		    p_segment8_low => c_possetline_rec.segment8_low,
920 		    p_segment9_low => c_possetline_rec.segment9_low,
921 		    p_segment10_low => c_possetline_rec.segment10_low,
922 		    p_segment11_low => c_possetline_rec.segment11_low,
923 		    p_segment12_low => c_possetline_rec.segment12_low,
924 		    p_segment13_low => c_possetline_rec.segment13_low,
925 		    p_segment14_low => c_possetline_rec.segment14_low,
926 		    p_segment15_low => c_possetline_rec.segment15_low,
927 		    p_segment16_low => c_possetline_rec.segment16_low,
928 		    p_segment17_low => c_possetline_rec.segment17_low,
929 		    p_segment18_low => c_possetline_rec.segment18_low,
930 		    p_segment19_low => c_possetline_rec.segment19_low,
931 		    p_segment20_low => c_possetline_rec.segment20_low,
932 		    p_segment21_low => c_possetline_rec.segment21_low,
933 		    p_segment22_low => c_possetline_rec.segment22_low,
934 		    p_segment23_low => c_possetline_rec.segment23_low,
935 		    p_segment24_low => c_possetline_rec.segment24_low,
936 		    p_segment25_low => c_possetline_rec.segment25_low,
937 		    p_segment26_low => c_possetline_rec.segment26_low,
938 		    p_segment27_low => c_possetline_rec.segment27_low,
939 		    p_segment28_low => c_possetline_rec.segment28_low,
940 		    p_segment29_low => c_possetline_rec.segment29_low,
941 		    p_segment30_low => c_possetline_rec.segment30_low,
942 		    p_segment1_high => c_possetline_rec.segment1_high,
943 		    p_segment2_high => c_possetline_rec.segment2_high,
944 		    p_segment3_high => c_possetline_rec.segment3_high,
945 		    p_segment4_high => c_possetline_rec.segment4_high,
946 		    p_segment5_high => c_possetline_rec.segment5_high,
947 		    p_segment6_high => c_possetline_rec.segment6_high,
948 		    p_segment7_high => c_possetline_rec.segment7_high,
949 		    p_segment8_high => c_possetline_rec.segment8_high,
950 		    p_segment9_high => c_possetline_rec.segment9_high,
951 		    p_segment10_high => c_possetline_rec.segment10_high,
952 		    p_segment11_high => c_possetline_rec.segment11_high,
953 		    p_segment12_high => c_possetline_rec.segment12_high,
954 		    p_segment13_high => c_possetline_rec.segment13_high,
955 		    p_segment14_high => c_possetline_rec.segment14_high,
956 		    p_segment15_high => c_possetline_rec.segment15_high,
957 		    p_segment16_high => c_possetline_rec.segment16_high,
958 		    p_segment17_high => c_possetline_rec.segment17_high,
959 		    p_segment18_high => c_possetline_rec.segment18_high,
960 		    p_segment19_high => c_possetline_rec.segment19_high,
961 		    p_segment20_high => c_possetline_rec.segment20_high,
962 		    p_segment21_high => c_possetline_rec.segment21_high,
963 		    p_segment22_high => c_possetline_rec.segment22_high,
964 		    p_segment23_high => c_possetline_rec.segment23_high,
965 		    p_segment24_high => c_possetline_rec.segment24_high,
966 		    p_segment25_high => c_possetline_rec.segment25_high,
967 		    p_segment26_high => c_possetline_rec.segment26_high,
968 		    p_segment27_high => c_possetline_rec.segment27_high,
969 		    p_segment28_high => c_possetline_rec.segment28_high,
970 		    p_segment29_high => c_possetline_rec.segment29_high,
971 		    p_segment30_high => c_possetline_rec.segment30_high,
972 		    p_context => c_possetline_rec.context,
973 		    p_attribute1 => c_possetline_rec.attribute1,
974 		    p_attribute2 => c_possetline_rec.attribute2,
975 		    p_attribute3 => c_possetline_rec.attribute3,
976 		    p_attribute4 => c_possetline_rec.attribute4,
977 		    p_attribute5 => c_possetline_rec.attribute5,
978 		    p_attribute6 => c_possetline_rec.attribute6,
979 		    p_attribute7 => c_possetline_rec.attribute7,
980 		    p_attribute8 => c_possetline_rec.attribute8,
981 		    p_attribute9 => c_possetline_rec.attribute9,
982 		    p_attribute10 => c_possetline_rec.attribute10,
983 		    p_last_update_date => sysdate,
984 		    p_last_updated_by => FND_GLOBAL.USER_ID,
985 		    p_last_update_login => FND_GLOBAL.LOGIN_ID,
986 		    p_created_by => FND_GLOBAL.USER_ID,
987 		    p_creation_date => sysdate);
988 
989 		if l_return_status <> FND_API.G_RET_STS_SUCCESS then
990 		  raise FND_API.G_EXC_ERROR;
991 		end if;
992 
993 		for c_possetlineval_rec in c_possetlineval (c_possetline_rec.line_sequence_id) loop
994 
995 		  for c_re_attrval_rec in c_re_attrval (c_possetlineval_rec.attribute_value_id) loop
996 		    l_attribute_value_id := c_re_attrval_rec.attribute_value_id;
997 		  end loop;
998 
999 		  for c_possetlineval_seq_rec in c_possetlineval_seq loop
1000 		    l_value_sequence_id := c_possetlineval_seq_rec.seq;
1001 		  end loop;
1002 
1003 		  PSB_POS_SET_LINE_VALUES_PVT.Insert_Row
1004 		     (p_api_version => 1.0,
1005 		      p_return_status => l_return_status,
1006 		      p_msg_count => l_msg_count,
1007 		      p_msg_data => l_msg_data,
1008 		      p_row_id => l_rowid,
1009 		      p_value_sequence_id => l_value_sequence_id,
1010 		      p_line_sequence_id => l_line_sequence_id,
1011 		      p_attribute_value_id => l_attribute_value_id,
1012 		      p_attribute_value => c_possetlineval_rec.attribute_value,
1013 		      p_last_update_date => sysdate,
1014 		      p_last_updated_by => FND_GLOBAL.USER_ID,
1015 		      p_last_update_login => FND_GLOBAL.LOGIN_ID,
1016 		      p_created_by => FND_GLOBAL.USER_ID,
1017 		      p_creation_date => sysdate);
1018 
1019 		  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1020 		    raise FND_API.G_EXC_ERROR;
1021 		  end if;
1022 
1023 		end loop; /* Loop for Position Set Line Val */
1024 
1025 	      end loop; /* For Position Set Line */
1026 
1027 	    end; /* Position Set does not exist */
1028 	    else
1029 	    begin /* Position Set exists; only upload position set lines and attribute values */
1030 
1031 	      for c_possetline_rec in c_possetline (c_posset_rec.account_position_set_id) loop
1032 
1033 		l_line_sequence_exists := FALSE;
1034 
1035 		for c_possetline_exists_rec in c_possetline_exists (l_position_set_id, c_possetline_rec.attribute_id) loop
1036 		  l_line_sequence_id := c_possetline_exists_rec.line_sequence_id;
1037 		  l_line_sequence_exists := TRUE;
1038 		end loop;
1039 
1040 		if not l_line_sequence_exists then
1041 		begin
1042 
1043 		  for c_possetline_seq_rec in c_possetline_seq loop
1044 		    l_line_sequence_id := c_possetline_seq_rec.seq;
1045 		  end loop;
1046 
1047 		  PSB_ACCT_POSITION_SET_LINE_PVT.Insert_Row
1048 		     (p_api_version => 1.0,
1049 		      p_return_status => l_return_status,
1050 		      p_msg_count => l_msg_count,
1051 		      p_msg_data => l_msg_data,
1052 		      p_row_id => l_rowid,
1053 		      p_line_sequence_id => l_line_sequence_id,
1054 		      p_account_position_set_id => l_position_set_id,
1055 		      p_description => c_possetline_rec.description,
1056 		      p_business_group_id => c_possetline_rec.business_group_id,
1057 		      p_attribute_id => c_possetline_rec.attribute_id,
1058 		      p_include_or_exclude_type => c_possetline_rec.include_or_exclude_type,
1059 		      p_segment1_low => c_possetline_rec.segment1_low,
1060 		      p_segment2_low => c_possetline_rec.segment2_low,
1061 		      p_segment3_low => c_possetline_rec.segment3_low,
1062 		      p_segment4_low => c_possetline_rec.segment4_low,
1063 		      p_segment5_low => c_possetline_rec.segment5_low,
1064 		      p_segment6_low => c_possetline_rec.segment6_low,
1065 		      p_segment7_low => c_possetline_rec.segment7_low,
1066 		      p_segment8_low => c_possetline_rec.segment8_low,
1067 		      p_segment9_low => c_possetline_rec.segment9_low,
1068 		      p_segment10_low => c_possetline_rec.segment10_low,
1069 		      p_segment11_low => c_possetline_rec.segment11_low,
1070 		      p_segment12_low => c_possetline_rec.segment12_low,
1071 		      p_segment13_low => c_possetline_rec.segment13_low,
1072 		      p_segment14_low => c_possetline_rec.segment14_low,
1073 		      p_segment15_low => c_possetline_rec.segment15_low,
1074 		      p_segment16_low => c_possetline_rec.segment16_low,
1075 		      p_segment17_low => c_possetline_rec.segment17_low,
1076 		      p_segment18_low => c_possetline_rec.segment18_low,
1077 		      p_segment19_low => c_possetline_rec.segment19_low,
1078 		      p_segment20_low => c_possetline_rec.segment20_low,
1079 		      p_segment21_low => c_possetline_rec.segment21_low,
1080 		      p_segment22_low => c_possetline_rec.segment22_low,
1081 		      p_segment23_low => c_possetline_rec.segment23_low,
1082 		      p_segment24_low => c_possetline_rec.segment24_low,
1083 		      p_segment25_low => c_possetline_rec.segment25_low,
1084 		      p_segment26_low => c_possetline_rec.segment26_low,
1085 		      p_segment27_low => c_possetline_rec.segment27_low,
1086 		      p_segment28_low => c_possetline_rec.segment28_low,
1087 		      p_segment29_low => c_possetline_rec.segment29_low,
1088 		      p_segment30_low => c_possetline_rec.segment30_low,
1089 		      p_segment1_high => c_possetline_rec.segment1_high,
1090 		      p_segment2_high => c_possetline_rec.segment2_high,
1091 		      p_segment3_high => c_possetline_rec.segment3_high,
1092 		      p_segment4_high => c_possetline_rec.segment4_high,
1093 		      p_segment5_high => c_possetline_rec.segment5_high,
1094 		      p_segment6_high => c_possetline_rec.segment6_high,
1095 		      p_segment7_high => c_possetline_rec.segment7_high,
1096 		      p_segment8_high => c_possetline_rec.segment8_high,
1097 		      p_segment9_high => c_possetline_rec.segment9_high,
1098 		      p_segment10_high => c_possetline_rec.segment10_high,
1099 		      p_segment11_high => c_possetline_rec.segment11_high,
1100 		      p_segment12_high => c_possetline_rec.segment12_high,
1101 		      p_segment13_high => c_possetline_rec.segment13_high,
1102 		      p_segment14_high => c_possetline_rec.segment14_high,
1103 		      p_segment15_high => c_possetline_rec.segment15_high,
1104 		      p_segment16_high => c_possetline_rec.segment16_high,
1105 		      p_segment17_high => c_possetline_rec.segment17_high,
1106 		      p_segment18_high => c_possetline_rec.segment18_high,
1107 		      p_segment19_high => c_possetline_rec.segment19_high,
1108 		      p_segment20_high => c_possetline_rec.segment20_high,
1109 		      p_segment21_high => c_possetline_rec.segment21_high,
1110 		      p_segment22_high => c_possetline_rec.segment22_high,
1111 		      p_segment23_high => c_possetline_rec.segment23_high,
1112 		      p_segment24_high => c_possetline_rec.segment24_high,
1113 		      p_segment25_high => c_possetline_rec.segment25_high,
1114 		      p_segment26_high => c_possetline_rec.segment26_high,
1115 		      p_segment27_high => c_possetline_rec.segment27_high,
1116 		      p_segment28_high => c_possetline_rec.segment28_high,
1117 		      p_segment29_high => c_possetline_rec.segment29_high,
1118 		      p_segment30_high => c_possetline_rec.segment30_high,
1119 		      p_context => c_possetline_rec.context,
1120 		      p_attribute1 => c_possetline_rec.attribute1,
1121 		      p_attribute2 => c_possetline_rec.attribute2,
1122 		      p_attribute3 => c_possetline_rec.attribute3,
1123 		      p_attribute4 => c_possetline_rec.attribute4,
1124 		      p_attribute5 => c_possetline_rec.attribute5,
1125 		      p_attribute6 => c_possetline_rec.attribute6,
1126 		      p_attribute7 => c_possetline_rec.attribute7,
1127 		      p_attribute8 => c_possetline_rec.attribute8,
1128 		      p_attribute9 => c_possetline_rec.attribute9,
1129 		      p_attribute10 => c_possetline_rec.attribute10,
1130 		      p_last_update_date => sysdate,
1131 		      p_last_updated_by => FND_GLOBAL.USER_ID,
1132 		      p_last_update_login => FND_GLOBAL.LOGIN_ID,
1133 		      p_created_by => FND_GLOBAL.USER_ID,
1134 		      p_creation_date => sysdate);
1135 
1136 		  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1137 		    raise FND_API.G_EXC_ERROR;
1138 		  end if;
1139 
1140 		  for c_possetlineval_rec in c_possetlineval (c_possetline_rec.line_sequence_id) loop
1141 
1142 		    for c_re_attrval_rec in c_re_attrval (c_possetlineval_rec.attribute_value_id) loop
1143 		      l_attribute_value_id := c_re_attrval_rec.attribute_value_id;
1144 		    end loop;
1145 
1146 		    for c_possetlineval_seq_rec in c_possetlineval_seq loop
1147 		      l_value_sequence_id := c_possetlineval_seq_rec.seq;
1148 		    end loop;
1149 
1150 		    PSB_POS_SET_LINE_VALUES_PVT.Insert_Row
1151 		       (p_api_version => 1.0,
1152 			p_return_status => l_return_status,
1153 			p_msg_count => l_msg_count,
1154 			p_msg_data => l_msg_data,
1155 			p_row_id => l_rowid,
1156 			p_value_sequence_id => l_value_sequence_id,
1157 			p_line_sequence_id => l_line_sequence_id,
1158 			p_attribute_value_id => l_attribute_value_id,
1159 			p_attribute_value => c_possetlineval_rec.attribute_value,
1160 			p_last_update_date => sysdate,
1161 			p_last_updated_by => FND_GLOBAL.USER_ID,
1162 			p_last_update_login => FND_GLOBAL.LOGIN_ID,
1163 			p_created_by => FND_GLOBAL.USER_ID,
1164 			p_creation_date => sysdate);
1165 
1166 		    if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1167 		      raise FND_API.G_EXC_ERROR;
1168 		    end if;
1169 
1170 		  end loop;
1171 
1172 		end; /* Position Set exists, Line Sequence does not exist */
1173 		else
1174 		begin /* Position Set exists, Line Sequence also exists */
1175 
1176 		  for c_possetlineval_rec in c_possetlineval (c_possetline_rec.line_sequence_id) loop
1177 
1178 		    for c_re_attrval_rec in c_re_attrval (c_possetlineval_rec.attribute_value_id) loop
1179 		      l_attribute_value_id := c_re_attrval_rec.attribute_value_id;
1180 		    end loop;
1181 
1182 		    for c_possetlineval_exists_rec in c_possetlineval_exists (l_line_sequence_id, c_possetlineval_rec.attribute_value) loop
1183 		      l_value_sequence_id := c_possetlineval_exists_rec.value_sequence_id;
1184 		      l_value_sequence_exists := TRUE;
1185 		    end loop;
1186 
1187 		    if not l_value_sequence_exists then
1188 		    begin
1189 
1190 		      for c_possetlineval_seq_rec in c_possetlineval_seq loop
1191 			l_value_sequence_id := c_possetlineval_seq_rec.seq;
1192 		      end loop;
1193 
1194 		      PSB_POS_SET_LINE_VALUES_PVT.Insert_Row
1195 			 (p_api_version => 1.0,
1196 			  p_return_status => l_return_status,
1197 			  p_msg_count => l_msg_count,
1198 			  p_msg_data => l_msg_data,
1199 			  p_row_id => l_rowid,
1200 			  p_value_sequence_id => l_value_sequence_id,
1201 			  p_line_sequence_id => l_line_sequence_id,
1202 			  p_attribute_value_id => l_attribute_value_id,
1203 			  p_attribute_value => c_possetlineval_rec.attribute_value,
1204 			  p_last_update_date => sysdate,
1205 			  p_last_updated_by => FND_GLOBAL.USER_ID,
1206 			  p_last_update_login => FND_GLOBAL.LOGIN_ID,
1207 			  p_created_by => FND_GLOBAL.USER_ID,
1208 			  p_creation_date => sysdate);
1209 
1210 		      if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1211 			raise FND_API.G_EXC_ERROR;
1212 		      end if;
1213 
1214 		    end;
1215 		    end if;
1216 
1217 		  end loop; /* For Position Set Line Val */
1218 
1219 		end; /* Position Set exists, Line Sequence also exists */
1220 		end if;
1221 	/*For Bug No : 1822371 Start*/
1222 	--This has been commented because of the attribute selection type
1223 	--should be same as the old Position Set.
1224 	--This has been taken care during the build for Bug No : 1822364
1225 	/*
1226 		-- Since we're adding attribute values set the attribute selection type to 'O'
1227 
1228 		if c_posset_rec.attribute_selection_type <> 'O' then
1229 		begin
1230 
1231 		  PSB_ACCOUNT_POSITION_SET_PVT.Update_Row
1232 		     (p_api_version => 1.0,
1233 		      p_return_status => l_return_status,
1234 		      p_msg_count => l_msg_count,
1235 		      p_msg_data => l_msg_data,
1236 		      p_row_id => lp_rowid,
1237 		      p_account_position_set_id => l_position_set_id,
1238 		      p_name => c_posset_rec.name,
1239 		      p_set_of_books_id => c_posset_rec.set_of_books_id,
1240 		      p_data_extract_id => p_target_data_extract_id,
1241 		      p_global_or_local_type => c_posset_rec.global_or_local_type,
1242 		      p_account_or_position_type => c_posset_rec.account_or_position_type,
1243 		      p_attribute_selection_type => 'O',
1244 		      p_business_group_id => c_posset_rec.business_group_id,
1245 		      p_last_update_date => sysdate,
1246 		      p_last_updated_by => FND_GLOBAL.USER_ID,
1247 		      p_last_update_login => FND_GLOBAL.LOGIN_ID);
1248 
1249 		  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1250 		    raise FND_API.G_EXC_ERROR;
1251 		  end if;
1252 
1253 		end;
1254 		end if;
1255 	 */
1256 	/*For Bug No : 1822371 End*/
1257 
1258 	      end loop;
1259 
1260 	    end; /* Position Set exists; only upload position set lines and attribute values */
1261 	    end if;
1262 
1263 	    -- Upload Set Relations
1264 
1265 	    for c_setrel_seq_rec in c_setrel_seq loop
1266 	      l_set_relation_id := c_setrel_seq_rec.seq;
1267 	    end loop;
1268 
1269 	    PSB_SET_RELATION_PVT.Insert_Row
1270 	       (p_api_version => 1.0,
1271 		p_return_status => l_return_status,
1272 		p_msg_count => l_msg_count,
1273 		p_msg_data => l_msg_data,
1274 		p_row_id => l_rowid,
1275 		p_set_relation_id => l_set_relation_id,
1276 		p_account_position_set_id => l_position_set_id,
1277 		p_allocation_rule_id => null,
1278 		p_budget_group_id => null,
1279 		p_budget_workflow_rule_id => null,
1280 		p_constraint_id => null,
1281 		p_default_rule_id => null,
1282 		p_parameter_id => null,
1283 		p_position_set_group_id => l_position_set_group_id,
1284 /* Budget Revision Rules Enhancement Start */
1285 		p_rule_id => null,
1286 		p_apply_balance_flag => null,
1287 /* Budget Revision Rules Enhancement End */
1288 		p_effective_start_date => null,
1289 		p_effective_end_date => null,
1290 		p_last_update_date => sysdate,
1291 		p_last_updated_by => FND_GLOBAL.USER_ID,
1292 		p_last_update_login => FND_GLOBAL.LOGIN_ID,
1293 		p_created_by => FND_GLOBAL.USER_ID,
1294 		p_creation_date => sysdate);
1295 
1296 	    if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1297 	      raise FND_API.G_EXC_ERROR;
1298 	    end if;
1299 
1300 	  end loop; /* Position Sets */
1301 
1302 	end loop; /* Set Relations */
1303 
1304 	for c_elemdist_rec in c_elemdist (c_possetgrp_rec.position_set_group_id) loop
1305 
1306 	  for c_elemdist_seq_rec in c_elemdist_seq loop
1307 	    l_distribution_id := c_elemdist_seq_rec.seq;
1308 	  end loop;
1309 
1310 	  PSB_ELEMENT_DISTRIBUTIONS_PVT.Insert_Row
1311 	     (p_api_version => 1.0,
1312 	      p_return_status => l_return_status,
1313 	      p_msg_count => l_msg_count,
1314 	      p_msg_data => l_msg_data,
1315 	      p_distribution_id => l_distribution_id,
1316 	      p_position_set_group_id => l_position_set_group_id,
1317 	      p_chart_of_accounts_id => c_elemdist_rec.chart_of_accounts_id,
1318 	      p_effective_start_date => c_elemdist_rec.effective_start_date,
1319 	      p_effective_end_date => c_elemdist_rec.effective_end_date,
1320 	      p_distribution_percent => c_elemdist_rec.distribution_percent,
1321 	      p_concatenated_segments => c_elemdist_rec.concatenated_segments,
1322 	      p_code_combination_id => c_elemdist_rec.code_combination_id,
1323 	      p_distribution_set_id => c_elemdist_rec.distribution_set_id,
1324 	      p_segment1 => c_elemdist_rec.segment1,
1325 	      p_segment2 => c_elemdist_rec.segment2,
1326 	      p_segment3 => c_elemdist_rec.segment3,
1327 	      p_segment4 => c_elemdist_rec.segment4,
1328 	      p_segment5 => c_elemdist_rec.segment5,
1329 	      p_segment6 => c_elemdist_rec.segment6,
1330 	      p_segment7 => c_elemdist_rec.segment7,
1331 	      p_segment8 => c_elemdist_rec.segment8,
1332 	      p_segment9 => c_elemdist_rec.segment9,
1333 	      p_segment10 => c_elemdist_rec.segment10,
1334 	      p_segment11 => c_elemdist_rec.segment11,
1335 	      p_segment12 => c_elemdist_rec.segment12,
1336 	      p_segment13 => c_elemdist_rec.segment13,
1337 	      p_segment14 => c_elemdist_rec.segment14,
1338 	      p_segment15 => c_elemdist_rec.segment15,
1339 	      p_segment16 => c_elemdist_rec.segment16,
1340 	      p_segment17 => c_elemdist_rec.segment17,
1341 	      p_segment18 => c_elemdist_rec.segment18,
1342 	      p_segment19 => c_elemdist_rec.segment19,
1343 	      p_segment20 => c_elemdist_rec.segment20,
1344 	      p_segment21 => c_elemdist_rec.segment21,
1345 	      p_segment22 => c_elemdist_rec.segment22,
1346 	      p_segment23 => c_elemdist_rec.segment23,
1347 	      p_segment24 => c_elemdist_rec.segment24,
1348 	      p_segment25 => c_elemdist_rec.segment25,
1349 	      p_segment26 => c_elemdist_rec.segment26,
1350 	      p_segment27 => c_elemdist_rec.segment27,
1351 	      p_segment28 => c_elemdist_rec.segment28,
1352 	      p_segment29 => c_elemdist_rec.segment29,
1353 	      p_segment30 => c_elemdist_rec.segment30,
1354 	      p_last_update_date => sysdate,
1355 	      p_last_updated_by => FND_GLOBAL.USER_ID,
1356 	      p_last_update_login => FND_GLOBAL.LOGIN_ID,
1357 	      p_created_by => FND_GLOBAL.USER_ID,
1358 	      p_creation_date => sysdate);
1359 
1360 	  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1361 	    raise FND_API.G_EXC_ERROR;
1362 	  end if;
1363 
1364 	end loop; /* Element Distribution */
1365 
1366       end loop; /* Element Position Set Group */
1367 
1368     end; /* Element does not exist */
1369     else
1370     begin /* Element already exists in the target data extract */
1371 
1372       -- update element definition
1373 
1374       PSB_PAY_ELEMENTS_PVT.UPDATE_ROW
1375 	(p_api_version => 1.0,
1376 	 p_return_status => l_return_status,
1377 	 p_msg_count => l_msg_count,
1378 	 p_msg_data => l_msg_data,
1379 	 P_ROW_ID => l_rowid,
1380 	 P_PAY_ELEMENT_ID => l_pay_element_id,
1381 	 P_BUSINESS_GROUP_ID => c_elem_rec.business_group_id,
1382 	 P_DATA_EXTRACT_ID => p_target_data_extract_id,
1383 	 P_BUDGET_SET_ID => c_elem_rec.budget_set_id,
1384 	 P_NAME => c_elem_rec.name,
1385 	 P_DESCRIPTION => c_elem_rec.description,
1386 	 P_ELEMENT_VALUE_TYPE => c_elem_rec.element_value_type,
1387 	 P_FORMULA_ID => c_elem_rec.formula_id,
1388 	 P_OVERWRITE_FLAG => c_elem_rec.overwrite_flag,
1389 	 P_REQUIRED_FLAG => c_elem_rec.required_flag,
1390 	 P_FOLLOW_SALARY => c_elem_rec.follow_salary,
1391 	 P_PAY_BASIS => c_elem_rec.pay_basis,
1392 	 P_START_DATE => c_elem_rec.start_date,
1393 	 P_END_DATE => c_elem_rec.end_date,
1394 	 P_PROCESSING_TYPE => c_elem_rec.processing_type,
1395 	 P_PERIOD_TYPE => c_elem_rec.period_type,
1396 	 P_PROCESS_PERIOD_TYPE => c_elem_rec.process_period_type,
1397 	 P_MAX_ELEMENT_VALUE_TYPE => c_elem_rec.max_element_value_type,
1398 	 P_MAX_ELEMENT_VALUE => c_elem_rec.max_element_value,
1399 	 P_SALARY_FLAG => c_elem_rec.salary_flag,
1400 	 P_SALARY_TYPE => c_elem_rec.salary_type,
1401 	 P_OPTION_FLAG => c_elem_rec.option_flag,
1402 	 P_HR_ELEMENT_TYPE_ID => c_elem_rec.hr_element_type_id,
1403 	 P_ATTRIBUTE_CATEGORY => c_elem_rec.attribute_category,
1404 	 P_ATTRIBUTE1 => c_elem_rec.attribute1,
1405 	 P_ATTRIBUTE2 => c_elem_rec.attribute2,
1406 	 P_ATTRIBUTE3 => c_elem_rec.attribute3,
1407 	 P_ATTRIBUTE4 => c_elem_rec.attribute4,
1408 	 P_ATTRIBUTE5 => c_elem_rec.attribute5,
1409 	 P_ATTRIBUTE6 => c_elem_rec.attribute6,
1410 	 P_ATTRIBUTE7 => c_elem_rec.attribute7,
1411 	 P_ATTRIBUTE8 => c_elem_rec.attribute8,
1412 	 P_ATTRIBUTE9 => c_elem_rec.attribute9,
1413 	 P_ATTRIBUTE10 => c_elem_rec.attribute10,
1414 	 P_LAST_UPDATE_DATE => sysdate,
1415 	 P_LAST_UPDATED_BY => FND_GLOBAL.USER_ID,
1416 	 P_LAST_UPDATE_LOGIN => FND_GLOBAL.LOGIN_ID);
1417 
1418       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1419 	raise FND_API.G_EXC_ERROR;
1420       end if;
1421 
1422       if c_elem_rec.option_flag = 'Y' then
1423       begin
1424 
1425 	for c_elemoptions_rec in c_elemoptions(c_elem_rec.pay_element_id) loop
1426 
1427 	  l_element_option_exists := FALSE;
1428 
1429 	  -- Check if Element Option already exists
1430          -- Changed the first parameter for bug 3476442
1431 	 for c_elemoptions_exists_rec
1432 	 in c_elemoptions_exists (l_pay_element_id, c_elemoptions_rec.name) loop
1433 	    l_element_option_exists := TRUE;
1434 	    l_pay_element_option_id :=
1435 	                         c_elemoptions_exists_rec.pay_element_option_id;
1436 	 end loop;
1437 
1438 	  if not l_element_option_exists then
1439 	  begin
1440 
1441 	    for c_elemoptions_seq_rec in c_elemoptions_seq loop
1442 	      l_pay_element_option_id := c_elemoptions_seq_rec.seq;
1443 	    end loop;
1444 
1445 	    PSB_PAY_ELEMENT_OPTIONS_PVT.INSERT_ROW
1446 	       (p_api_version => 1.0,
1447 		p_return_status => l_return_status,
1448 		p_msg_count => l_msg_count,
1449 		p_msg_data => l_msg_data,
1450 		p_pay_element_option_id => l_pay_element_option_id,
1451 		p_pay_element_id => l_pay_element_id,
1452 		p_name => c_elemoptions_rec.name,
1453 		p_grade_step => c_elemoptions_rec.grade_step,
1454 		p_sequence_number => c_elemoptions_rec.sequence_number,
1455 		p_last_update_date => sysdate,
1456 		p_last_updated_by => FND_GLOBAL.USER_ID,
1457 		p_last_update_login => FND_GLOBAL.LOGIN_ID,
1458 		p_created_by => FND_GLOBAL.USER_ID,
1459 		p_creation_date => sysdate);
1460 
1461 	    if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1462 	      raise FND_API.G_EXC_ERROR;
1463 	    end if;
1464 
1465 	    for c_elemrates_rec in c_elemrates(c_elem_rec.pay_element_id, c_elemoptions_rec.pay_element_option_id) loop
1466 	      PSB_PAY_ELEMENT_RATES_PVT.Modify_Element_Rates
1467 		 (p_api_version => 1.0,
1468 		  p_return_status => l_return_status,
1469 		  p_msg_count => l_msg_count,
1470 		  p_msg_data => l_msg_data,
1471 		  p_pay_element_id => l_pay_element_id,
1472 		  p_pay_element_option_id => l_pay_element_option_id,
1473 		  p_effective_start_date => c_elemrates_rec.effective_start_date,
1474 		  p_effective_end_date => c_elemrates_rec.effective_end_date,
1475 		  p_worksheet_id => null,
1476 		  p_element_value_type => c_elemrates_rec.element_value_type,
1477 		  p_element_value => c_elemrates_rec.element_value,
1478 		  p_pay_basis  => c_elemrates_rec.pay_basis,
1479 		  p_formula_id => c_elemrates_rec.formula_id,
1480 		  p_maximum_value => c_elemrates_rec.maximum_value,
1481 		  p_mid_value => c_elemrates_rec.mid_value,
1482 		  p_minimum_value => c_elemrates_rec.minimum_value,
1483 		  p_currency_code => c_elemrates_rec.currency_code);
1484 
1485 	      if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1486 		raise FND_API.G_EXC_ERROR;
1487 	      end if;
1488 
1489 	    end loop;
1490 
1491 	  end; /* Elemoption does not exist */
1492 	  end if;
1493 
1494 	end loop; /* Elemoptions */
1495 
1496       end; /* Option Flag = 'Y' */
1497       else
1498       begin
1499 
1500 	for c_elemrates_rec in c_elemrates_nooptions (c_elem_rec.pay_element_id) loop
1501 
1502 	  PSB_PAY_ELEMENT_RATES_PVT.Modify_Element_Rates
1503 	     (p_api_version => 1.0,
1504 	      p_return_status => l_return_status,
1505 	      p_msg_count => l_msg_count,
1506 	      p_msg_data => l_msg_data,
1507 	      p_pay_element_id => l_pay_element_id,
1508 	      p_pay_element_option_id => NULL,
1509 	      p_effective_start_date => c_elemrates_rec.effective_start_date,
1510 	      p_effective_end_date => c_elemrates_rec.effective_end_date,
1511 	      p_worksheet_id => null,
1512 	      p_element_value_type => c_elemrates_rec.element_value_type,
1513 	      p_element_value => c_elemrates_rec.element_value,
1514 	      p_pay_basis  => c_elemrates_rec.pay_basis,
1515 	      p_formula_id => c_elemrates_rec.formula_id,
1516 	      p_maximum_value => c_elemrates_rec.maximum_value,
1517 	      p_mid_value => c_elemrates_rec.mid_value,
1518 	      p_minimum_value => c_elemrates_rec.minimum_value,
1519 	      p_currency_code => c_elemrates_rec.currency_code);
1520 
1521 	  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1522 	    raise FND_API.G_EXC_ERROR;
1523 	  end if;
1524 
1525 	end loop;
1526 
1527       end;
1528       end if;
1529 
1530     end; /* Element already exists in the target data extract */
1531     end if;
1532 
1533   end loop;
1534 
1535   -- Initialize API return status to success
1536   p_return_status := FND_API.G_RET_STS_SUCCESS;
1537 
1538 EXCEPTION
1539 
1540    when FND_API.G_EXC_ERROR then
1541      p_return_status := FND_API.G_RET_STS_ERROR;
1542 
1543    when FND_API.G_EXC_UNEXPECTED_ERROR then
1544      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1545 
1546    when OTHERS then
1547      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1548 
1549 END Upload_Element;
1550 
1551 /* ----------------------------------------------------------------------- */
1552 
1553 PROCEDURE Upload_Elements
1554 ( p_return_status           OUT  NOCOPY  VARCHAR2,
1555   p_source_data_extract_id  IN   NUMBER,
1556   p_target_data_extract_id  IN   NUMBER,
1557   p_worksheet_id            IN   NUMBER,
1558   p_budget_calendar_id      IN   NUMBER,
1559   p_flex_mapping_set_id     IN   NUMBER,
1560   p_budget_group_id         IN   NUMBER
1561 ) IS
1562 
1563   l_pay_element_id          NUMBER;
1564   l_rowid                   ROWID;
1565   l_pay_element_option_id   NUMBER;
1566 
1567   l_element_exists          BOOLEAN;
1568   l_element_option_exists   BOOLEAN;
1569 
1570   l_msg_count               NUMBER;
1571   l_msg_data                VARCHAR2(2000);
1572   l_return_status           VARCHAR2(1);
1573 
1574   cursor c_elem is
1575     select *
1576       from PSB_PAY_ELEMENTS
1577      where data_extract_id = p_source_data_extract_id;
1578 
1579   cursor c_elemname_exists (elemname VARCHAR2) is
1580     select pay_element_id, rowid
1581       from PSB_PAY_ELEMENTS
1582      where name = elemname
1583        and data_extract_id = p_target_data_extract_id;
1584 
1585   cursor c_elemoptions (elemid NUMBER) is
1586     select *
1587       from PSB_PAY_ELEMENT_OPTIONS
1588      where pay_element_id = elemid;
1589 
1590   cursor c_elemoptions_exists (elemid NUMBER, optname VARCHAR2) is
1591     select pay_element_option_id
1592       from PSB_PAY_ELEMENT_OPTIONS
1593      where name = optname
1594        and pay_element_id = elemid;
1595 
1596   cursor c_elemrates (elemid NUMBER, elemoptionid NUMBER) is
1597     select *
1598       from PSB_PAY_ELEMENT_RATES a
1599      where a.pay_element_id = elemid
1600        and a.pay_element_option_id = elemoptionid
1601        and ((a.worksheet_id = p_worksheet_id)
1602 	 or (a.worksheet_id is null
1603        and not exists
1604 	  (select 1 from PSB_PAY_ELEMENT_RATES b
1605 	    where b.pay_element_id = a.pay_element_id
1606 	      and b.pay_element_option_id = a.pay_element_option_id
1607 	      and b.worksheet_id = p_worksheet_id)));
1608 
1609   cursor c_elemrates_nooptions (elemid NUMBER) is
1610     select *
1611       from PSB_PAY_ELEMENT_RATES a
1612      where a.pay_element_id = elemid
1613        and ((a.worksheet_id = p_worksheet_id)
1614 	 or (a.worksheet_id is null
1615        and not exists
1616 	  (select 1 from PSB_PAY_ELEMENT_RATES b
1617 	    where b.pay_element_id = a.pay_element_id
1618 	      and b.worksheet_id = p_worksheet_id)));
1619 
1620   cursor c_elem_seq is
1621     select psb_pay_elements_s.nextval seq
1622       from dual;
1623 
1624   cursor c_elemoptions_seq is
1625     select psb_pay_element_options_s.nextval seq
1626       from dual;
1627 
1628 BEGIN
1629 
1630   for c_elem_rec in c_elem loop
1631 
1632     l_element_exists := FALSE;
1633     l_rowid := null;
1634 
1635     -- Check if element name exists in the target data extract
1636 
1637     for c_elemname_exists_rec in c_elemname_exists (c_elem_rec.name) loop
1638       l_pay_element_id := c_elemname_exists_rec.pay_element_id;
1639       l_rowid := c_elemname_exists_rec.rowid;
1640       l_element_exists := TRUE;
1641     end loop;
1642 
1643     if not l_element_exists then
1644     begin
1645 
1646       for c_elem_seq_rec in c_elem_seq loop
1647 	l_pay_element_id := c_elem_seq_rec.seq;
1648       end loop;
1649 
1650       PSB_PAY_ELEMENTS_PVT.INSERT_ROW
1651 	 (p_api_version => 1.0,
1652 	  p_return_status => l_return_status,
1653 	  p_msg_count => l_msg_count,
1654 	  p_msg_data => l_msg_data,
1655 	  p_row_id => l_rowid,
1656 	  p_pay_element_id => l_pay_element_id,
1657 	  p_budget_set_id => c_elem_rec.budget_set_id,
1658 	  p_business_group_id => c_elem_rec.business_group_id,
1659 	  p_data_extract_id => p_target_data_extract_id,
1660 	  p_name => c_elem_rec.name,
1661 	  p_description => c_elem_rec.description,
1662 	  p_element_value_type => c_elem_rec.element_value_type,
1663 	  p_formula_id => c_elem_rec.formula_id,
1664 	  p_overwrite_flag => c_elem_rec.overwrite_flag,
1665 	  p_required_flag => c_elem_rec.required_flag,
1666 	  p_follow_salary => c_elem_rec.follow_salary,
1667 	  p_pay_basis => c_elem_rec.pay_basis,
1668 	  p_start_date => c_elem_rec.start_date,
1669 	  p_end_date => c_elem_rec.end_date,
1670 	  p_processing_type => c_elem_rec.processing_type,
1671 	  p_period_type => c_elem_rec.period_type,
1672 	  p_process_period_type => c_elem_rec.process_period_type,
1673 	  p_max_element_value_type => c_elem_rec.max_element_value_type,
1674 	  p_max_element_value => c_elem_rec.max_element_value,
1675 	  p_salary_flag => c_elem_rec.salary_flag,
1676 	  p_salary_type => c_elem_rec.salary_type,
1677 	  p_option_flag => c_elem_rec.option_flag,
1678 	  p_hr_element_type_id => c_elem_rec.hr_element_type_id,
1679 	  p_attribute_category => c_elem_rec.attribute_category,
1680 	  p_attribute1 => c_elem_rec.attribute1,
1681 	  p_attribute2 => c_elem_rec.attribute2,
1682 	  p_attribute3 => c_elem_rec.attribute3,
1683 	  p_attribute4 => c_elem_rec.attribute4,
1684 	  p_attribute5 => c_elem_rec.attribute5,
1685 	  p_attribute6 => c_elem_rec.attribute6,
1686 	  p_attribute7 => c_elem_rec.attribute7,
1687 	  p_attribute8 => c_elem_rec.attribute8,
1688 	  p_attribute9 => c_elem_rec.attribute9,
1689 	  p_attribute10 => c_elem_rec.attribute10,
1690 	  p_last_update_date => sysdate,
1691 	  p_last_updated_by => FND_GLOBAL.USER_ID,
1692 	  p_last_update_login => FND_GLOBAL.LOGIN_ID,
1693 	  p_created_by => FND_GLOBAL.USER_ID,
1694 	  p_creation_date => sysdate);
1695 
1696       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1697 	raise FND_API.G_EXC_ERROR;
1698       end if;
1699 
1700       if c_elem_rec.option_flag = 'Y' then
1701       begin
1702 
1703 	for c_elemoptions_rec in c_elemoptions(c_elem_rec.pay_element_id) loop
1704 
1705 	  for c_elemoptions_seq_rec in c_elemoptions_seq loop
1706 	    l_pay_element_option_id := c_elemoptions_seq_rec.seq;
1707 	  end loop;
1708 
1709 	  PSB_PAY_ELEMENT_OPTIONS_PVT.INSERT_ROW
1710 	     (p_api_version => 1.0,
1711 	      p_return_status => l_return_status,
1712 	      p_msg_count => l_msg_count,
1713 	      p_msg_data => l_msg_data,
1714 	      p_pay_element_option_id => l_pay_element_option_id,
1715 	      p_pay_element_id => l_pay_element_id,
1716 	      p_name => c_elemoptions_rec.name,
1717 	      p_grade_step => c_elemoptions_rec.grade_step,
1718 	      p_sequence_number => c_elemoptions_rec.sequence_number,
1719 	      p_last_update_date => sysdate,
1720 	      p_last_updated_by => FND_GLOBAL.USER_ID,
1721 	      p_last_update_login => FND_GLOBAL.LOGIN_ID,
1722 	      p_created_by => FND_GLOBAL.USER_ID,
1723 	      p_creation_date => sysdate);
1724 
1725 	  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1726 	    raise FND_API.G_EXC_ERROR;
1727 	  end if;
1728 
1729 	  for c_elemrates_rec in c_elemrates (c_elem_rec.pay_element_id, c_elemoptions_rec.pay_element_option_id) loop
1730 	    PSB_PAY_ELEMENT_RATES_PVT.Modify_Element_Rates
1731 	       (p_api_version => 1.0,
1732 		p_return_status => l_return_status,
1733 		p_msg_count => l_msg_count,
1734 		p_msg_data => l_msg_data,
1735 		p_pay_element_id => l_pay_element_id,
1736 		p_pay_element_option_id => l_pay_element_option_id,
1737 		p_effective_start_date => c_elemrates_rec.effective_start_date,
1738 		p_effective_end_date => c_elemrates_rec.effective_end_date,
1739 		p_worksheet_id => null,
1740 		p_element_value_type => c_elemrates_rec.element_value_type,
1741 		p_element_value => c_elemrates_rec.element_value,
1742 		p_pay_basis  => c_elemrates_rec.pay_basis,
1743 		p_formula_id => c_elemrates_rec.formula_id,
1744 		p_maximum_value => c_elemrates_rec.maximum_value,
1745 		p_mid_value => c_elemrates_rec.mid_value,
1746 		p_minimum_value => c_elemrates_rec.minimum_value,
1747 		p_currency_code => c_elemrates_rec.currency_code);
1748 
1749 	    if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1750 	      raise FND_API.G_EXC_ERROR;
1751 	    end if;
1752 
1753 	  end loop;
1754 
1755 	end loop;
1756 
1757       end;
1758       else /* Elements without options */
1759       begin
1760 
1761 	for c_elemrates_rec in c_elemrates_nooptions (c_elem_rec.pay_element_id) loop
1762 
1763 	  PSB_PAY_ELEMENT_RATES_PVT.Modify_Element_Rates
1764 	     (p_api_version => 1.0,
1765 	      p_return_status => l_return_status,
1766 	      p_msg_count => l_msg_count,
1767 	      p_msg_data => l_msg_data,
1768 	      p_pay_element_id => l_pay_element_id,
1769 	      p_pay_element_option_id => null,
1770 	      p_effective_start_date => c_elemrates_rec.effective_start_date,
1771 	      p_effective_end_date => c_elemrates_rec.effective_end_date,
1772 	      p_worksheet_id => null,
1773 	      p_element_value_type => c_elemrates_rec.element_value_type,
1774 	      p_element_value => c_elemrates_rec.element_value,
1775 	      p_pay_basis  => c_elemrates_rec.pay_basis,
1776 	      p_formula_id => c_elemrates_rec.formula_id,
1777 	      p_maximum_value => c_elemrates_rec.maximum_value,
1778 	      p_mid_value => c_elemrates_rec.mid_value,
1779 	      p_minimum_value => c_elemrates_rec.minimum_value,
1780 	      p_currency_code => c_elemrates_rec.currency_code);
1781 
1782 	  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1783 	    raise FND_API.G_EXC_ERROR;
1784 	  end if;
1785 
1786 	end loop;
1787 
1788       end; /* Elements without options */
1789       end if;
1790 
1791 	/*The code that was defined here has been removed and
1792 	  implemented in the Copy_Position_Set_Groups procedure
1793 	*/
1794 
1795     end; /* Element does not exist */
1796     else
1797     begin /* Element already exists in the target data extract */
1798 
1799       -- update element definition
1800 
1801       PSB_PAY_ELEMENTS_PVT.UPDATE_ROW
1802 	(p_api_version => 1.0,
1803 	 p_return_status => l_return_status,
1804 	 p_msg_count => l_msg_count,
1805 	 p_msg_data => l_msg_data,
1806 	 P_ROW_ID => l_rowid,
1807 	 P_PAY_ELEMENT_ID => l_pay_element_id,
1808 	 P_BUSINESS_GROUP_ID => c_elem_rec.business_group_id,
1809 	 P_DATA_EXTRACT_ID => p_target_data_extract_id,
1810 	 P_BUDGET_SET_ID => c_elem_rec.budget_set_id,
1811 	 P_NAME => c_elem_rec.name,
1812 	 P_DESCRIPTION => c_elem_rec.description,
1813 	 P_ELEMENT_VALUE_TYPE => c_elem_rec.element_value_type,
1814 	 P_FORMULA_ID => c_elem_rec.formula_id,
1815 	 P_OVERWRITE_FLAG => c_elem_rec.overwrite_flag,
1816 	 P_REQUIRED_FLAG => c_elem_rec.required_flag,
1817 	 P_FOLLOW_SALARY => c_elem_rec.follow_salary,
1818 	 P_PAY_BASIS => c_elem_rec.pay_basis,
1819 	 P_START_DATE => c_elem_rec.start_date,
1820 	 P_END_DATE => c_elem_rec.end_date,
1821 	 P_PROCESSING_TYPE => c_elem_rec.processing_type,
1822 	 P_PERIOD_TYPE => c_elem_rec.period_type,
1823 	 P_PROCESS_PERIOD_TYPE => c_elem_rec.process_period_type,
1824 	 P_MAX_ELEMENT_VALUE_TYPE => c_elem_rec.max_element_value_type,
1825 	 P_MAX_ELEMENT_VALUE => c_elem_rec.max_element_value,
1826 	 P_SALARY_FLAG => c_elem_rec.salary_flag,
1827 	 P_SALARY_TYPE => c_elem_rec.salary_type,
1828 	 P_OPTION_FLAG => c_elem_rec.option_flag,
1829 	 P_HR_ELEMENT_TYPE_ID => c_elem_rec.hr_element_type_id,
1830 	 P_ATTRIBUTE_CATEGORY => c_elem_rec.attribute_category,
1831 	 P_ATTRIBUTE1 => c_elem_rec.attribute1,
1832 	 P_ATTRIBUTE2 => c_elem_rec.attribute2,
1833 	 P_ATTRIBUTE3 => c_elem_rec.attribute3,
1834 	 P_ATTRIBUTE4 => c_elem_rec.attribute4,
1835 	 P_ATTRIBUTE5 => c_elem_rec.attribute5,
1836 	 P_ATTRIBUTE6 => c_elem_rec.attribute6,
1837 	 P_ATTRIBUTE7 => c_elem_rec.attribute7,
1838 	 P_ATTRIBUTE8 => c_elem_rec.attribute8,
1839 	 P_ATTRIBUTE9 => c_elem_rec.attribute9,
1840 	 P_ATTRIBUTE10 => c_elem_rec.attribute10,
1841 	 P_LAST_UPDATE_DATE => sysdate,
1842 	 P_LAST_UPDATED_BY => FND_GLOBAL.USER_ID,
1843 	 P_LAST_UPDATE_LOGIN => FND_GLOBAL.LOGIN_ID);
1844 
1845       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1846 	raise FND_API.G_EXC_ERROR;
1847       end if;
1848 
1849       if c_elem_rec.option_flag = 'Y' then
1850       begin
1851 
1852 	for c_elemoptions_rec in c_elemoptions(c_elem_rec.pay_element_id) loop
1853 
1854 	  l_element_option_exists := FALSE;
1855 
1856 	  -- Check if Element Option already exists
1857 
1858           -- Changed the first parameter for bug 3476442
1859 	  for c_elemoptions_exists_rec in c_elemoptions_exists (l_pay_element_id, c_elemoptions_rec.name) loop
1860 	    l_element_option_exists := TRUE;
1861 	    l_pay_element_option_id :=
1862 	                         c_elemoptions_exists_rec.pay_element_option_id;
1863 	  end loop;
1864 
1865 	  if not l_element_option_exists then
1866 	  begin
1867 
1868 	    for c_elemoptions_seq_rec in c_elemoptions_seq loop
1869 	      l_pay_element_option_id := c_elemoptions_seq_rec.seq;
1870 	    end loop;
1871 
1872 	    PSB_PAY_ELEMENT_OPTIONS_PVT.INSERT_ROW
1873 	       (p_api_version => 1.0,
1874 		p_return_status => l_return_status,
1875 		p_msg_count => l_msg_count,
1876 		p_msg_data => l_msg_data,
1877 		p_pay_element_option_id => l_pay_element_option_id,
1878 		p_pay_element_id => l_pay_element_id,
1879 		p_name => c_elemoptions_rec.name,
1880 		p_grade_step => c_elemoptions_rec.grade_step,
1881 		p_sequence_number => c_elemoptions_rec.sequence_number,
1882 		p_last_update_date => sysdate,
1883 		p_last_updated_by => FND_GLOBAL.USER_ID,
1884 		p_last_update_login => FND_GLOBAL.LOGIN_ID,
1885 		p_created_by => FND_GLOBAL.USER_ID,
1886 		p_creation_date => sysdate);
1887 
1888 	    if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1889 	      raise FND_API.G_EXC_ERROR;
1890 	    end if;
1891 
1892 	    for c_elemrates_rec in c_elemrates (c_elem_rec.pay_element_id, c_elemoptions_rec.pay_element_option_id) loop
1893 
1894 	      PSB_PAY_ELEMENT_RATES_PVT.Modify_Element_Rates
1895 		 (p_api_version => 1.0,
1896 		  p_return_status => l_return_status,
1897 		  p_msg_count => l_msg_count,
1898 		  p_msg_data => l_msg_data,
1899 		  p_pay_element_id => l_pay_element_id,
1900 		  p_pay_element_option_id => l_pay_element_option_id,
1901 		  p_effective_start_date => c_elemrates_rec.effective_start_date,
1902 		  p_effective_end_date => c_elemrates_rec.effective_end_date,
1903 		  p_worksheet_id => null,
1904 		  p_element_value_type => c_elemrates_rec.element_value_type,
1905 		  p_element_value => c_elemrates_rec.element_value,
1906 		  p_pay_basis  => c_elemrates_rec.pay_basis,
1907 		  p_formula_id => c_elemrates_rec.formula_id,
1908 		  p_maximum_value => c_elemrates_rec.maximum_value,
1909 		  p_mid_value => c_elemrates_rec.mid_value,
1910 		  p_minimum_value => c_elemrates_rec.minimum_value,
1911 		  p_currency_code => c_elemrates_rec.currency_code);
1912 
1913 	      if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1914 		raise FND_API.G_EXC_ERROR;
1915 	      end if;
1916 
1917 	    end loop;
1918 
1919 	  end; /* Elemoption does not exist */
1920 	  else
1921 	  begin /* Element Option exists; only need to refresh the rates */
1922 
1923 	    for c_elemrates_rec in c_elemrates (c_elem_rec.pay_element_id, c_elemoptions_rec.pay_element_option_id) loop
1924 
1925 	      PSB_PAY_ELEMENT_RATES_PVT.Modify_Element_Rates
1926 		 (p_api_version => 1.0,
1927 		  p_return_status => l_return_status,
1928 		  p_msg_count => l_msg_count,
1929 		  p_msg_data => l_msg_data,
1930 		  p_pay_element_id => l_pay_element_id,
1931 		  p_pay_element_option_id => l_pay_element_option_id,
1932 		  p_effective_start_date => c_elemrates_rec.effective_start_date,
1933 		  p_effective_end_date => c_elemrates_rec.effective_end_date,
1934 		  p_worksheet_id => null,
1935 		  p_element_value_type => c_elemrates_rec.element_value_type,
1936 		  p_element_value => c_elemrates_rec.element_value,
1937 		  p_pay_basis  => c_elemrates_rec.pay_basis,
1938 		  p_formula_id => c_elemrates_rec.formula_id,
1939 		  p_maximum_value => c_elemrates_rec.maximum_value,
1940 		  p_mid_value => c_elemrates_rec.mid_value,
1941 		  p_minimum_value => c_elemrates_rec.minimum_value,
1942 		  p_currency_code => c_elemrates_rec.currency_code);
1943 
1944 	      if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1945 		raise FND_API.G_EXC_ERROR;
1946 	      end if;
1947 
1948 	    end loop;
1949 
1950 	  end;
1951 	  end if;
1952 
1953 	end loop; /* Elemoptions */
1954 
1955       end; /* Option Flag = 'Y' */
1956       else
1957       begin
1958 
1959 	for c_elemrates_rec in c_elemrates_nooptions (c_elem_rec.pay_element_id) loop
1960 
1961 	  PSB_PAY_ELEMENT_RATES_PVT.Modify_Element_Rates
1962 	     (p_api_version => 1.0,
1963 	      p_return_status => l_return_status,
1964 	      p_msg_count => l_msg_count,
1965 	      p_msg_data => l_msg_data,
1966 	      p_pay_element_id => l_pay_element_id,
1967 	      p_pay_element_option_id => null,
1968 	      p_effective_start_date => c_elemrates_rec.effective_start_date,
1969 	      p_effective_end_date => c_elemrates_rec.effective_end_date,
1970 	      p_worksheet_id => null,
1971 	      p_element_value_type => c_elemrates_rec.element_value_type,
1972 	      p_element_value => c_elemrates_rec.element_value,
1973 	      p_pay_basis  => c_elemrates_rec.pay_basis,
1974 	      p_formula_id => c_elemrates_rec.formula_id,
1975 	      p_maximum_value => c_elemrates_rec.maximum_value,
1976 	      p_mid_value => c_elemrates_rec.mid_value,
1977 	      p_minimum_value => c_elemrates_rec.minimum_value,
1978 	      p_currency_code => c_elemrates_rec.currency_code);
1979 
1980 	  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1981 	    raise FND_API.G_EXC_ERROR;
1982 	  end if;
1983 
1984 	end loop;
1985 
1986       end;
1987       end if;
1988 
1989     end; /* Element already exists in the target data extract */
1990     end if;
1991 
1992     /*For Bug No : 1822364 Start*/
1993     --Copy all the position set groups and position set details and
1994     --account distributions that are related to it
1995     Copy_Position_Set_Groups
1996 	( p_return_status           =>  l_return_status,
1997 	  p_old_pay_element_id      =>  c_elem_rec.pay_element_id,
1998 	  p_new_pay_element_id      =>  l_pay_element_id,
1999 	  p_target_data_extract_id  =>  p_target_data_extract_id,
2000 	  p_budget_group_id         =>  p_budget_group_id,
2001 	  p_flex_mapping_set_id     =>  p_flex_mapping_set_id,
2002 	  p_follow_salary           =>  c_elem_rec.follow_salary
2003 	);
2004      /*For Bug No : 1822364 Start*/
2005 
2006   end loop;
2007 
2008   -- Initialize API return status to success
2009   p_return_status := FND_API.G_RET_STS_SUCCESS;
2010 
2011 EXCEPTION
2012 
2013    when FND_API.G_EXC_ERROR then
2014      p_return_status := FND_API.G_RET_STS_ERROR;
2015 
2016    when FND_API.G_EXC_UNEXPECTED_ERROR then
2017      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2018 
2019    when OTHERS then
2020      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2021 
2022 END Upload_Elements;
2023 
2024 /* ----------------------------------------------------------------------- */
2025 /*For Bug No : 2434152 Start*/
2026 --The following procedure has been converted to
2027 --BULK COLLECT
2028 
2029 PROCEDURE Upload_Employees
2030 ( p_return_status             OUT  NOCOPY  VARCHAR2,
2031   p_source_data_extract_id    IN   NUMBER,
2032   p_source_business_group_id  IN   NUMBER,
2033   p_target_data_extract_id    IN   NUMBER
2034 ) IS
2035 
2036   TYPE Number_tbl_type IS TABLE OF NUMBER        INDEX BY BINARY_INTEGER;
2037   TYPE Char_tbl_type   IS TABLE OF VARCHAR2(240) INDEX BY BINARY_INTEGER;
2038   TYPE Date_tbl_type   IS TABLE OF DATE          INDEX BY BINARY_INTEGER;
2039 
2040   l_emp_num       Char_tbl_type;
2041   l_hremp_id      Number_tbl_type;
2042   l_first_name    Char_tbl_type;
2043   l_full_name     Char_tbl_type;
2044   l_known_as      Char_tbl_type;
2045   l_last_name     Char_tbl_type;
2046   l_middle_names  Char_tbl_type;
2047   l_title         Char_tbl_type;
2048 
2049   /*For Bug No : 2594575 Start*/
2050   --Stop extracting secured data of employee
2051   --Removed the columns in psb_employees table
2052   /*For Bug No : 2594575 End*/
2053 
2054   CURSOR C_Emp_All IS
2055   SELECT EMPLOYEE_NUMBER        ,
2056 	 HR_EMPLOYEE_ID         ,
2057 	 FIRST_NAME             ,
2058 	 FULL_NAME              ,
2059 	 KNOWN_AS               ,
2060 	 LAST_NAME              ,
2061 	 MIDDLE_NAMES           ,
2062 	 TITLE
2063     FROM PSB_EMPLOYEES emp
2064    WHERE emp.data_extract_id = p_source_data_extract_id
2065      AND emp.business_group_id = p_source_business_group_id
2066      AND NOT EXISTS (
2067 	 SELECT 1
2068 	   FROM PSB_EMPLOYEES
2069 	  WHERE hr_employee_id = emp.hr_employee_id
2070 	    AND data_extract_id = p_target_data_extract_id
2071 	);
2072 
2073 BEGIN
2074 
2075   OPEN C_Emp_All;
2076 
2077   LOOP
2078     FETCH C_Emp_All
2079       BULK COLLECT INTO l_emp_num,l_hremp_id,l_first_name,l_full_name,
2080 			l_known_as,l_last_name,l_middle_names,l_title
2081       LIMIT g_limit_bulk_numrows;
2082 
2083     FORALL i IN 1..l_emp_num.count
2084       insert into PSB_EMPLOYEES
2085 	    (employee_id,
2086 	     data_extract_id,
2087 	     business_group_id,
2088 	     employee_number,
2089 	     hr_employee_id,
2090 	     first_name,
2091 	     full_name,
2092 	     known_as,
2093 	     last_name,
2094 	     middle_names,
2095 	     title,
2096 	     creation_date,
2097 	     created_by,
2098 	     last_update_date,
2099 	     last_updated_by,
2100 	     last_update_login)
2101       values (PSB_EMPLOYEES_S.NEXTVAL,
2102 	     p_target_data_extract_id,
2103 	     p_source_business_group_id,
2104 	     l_emp_num(i),
2105 	     l_hremp_id(i),
2106 	     l_first_name(i),
2107 	     l_full_name(i),
2108 	     l_known_as(i),
2109 	     l_last_name(i),
2110 	     l_middle_names(i),
2111 	     l_title(i),
2112 	     sysdate,
2113 	     FND_GLOBAL.USER_ID,
2114 	     sysdate,
2115 	     FND_GLOBAL.USER_ID,
2116 	     FND_GLOBAL.LOGIN_ID);
2117 
2118     EXIT WHEN C_Emp_All%NOTFOUND;
2119 
2120   END LOOP;
2121 
2122   close C_Emp_All;
2123 
2124   -- Initialize API return status to success
2125   p_return_status := FND_API.G_RET_STS_SUCCESS;
2126 
2127 EXCEPTION
2128 
2129    when FND_API.G_EXC_ERROR then
2130      if C_Emp_All%ISOPEN then
2131        close C_Emp_All;
2132      end if;
2133      p_return_status := FND_API.G_RET_STS_ERROR;
2134 
2135    when FND_API.G_EXC_UNEXPECTED_ERROR then
2136      if C_Emp_All%ISOPEN then
2137        close C_Emp_All;
2138      end if;
2139      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2140 
2141    when OTHERS then
2142      if C_Emp_All%ISOPEN then
2143        close C_Emp_All;
2144      end if;
2145      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2146 
2147 END Upload_Employees;
2148 
2149 /*For Bug No : 2434152 End*/
2150 
2151 /* ----------------------------------------------------------------------- */
2152 /* Bug 3867577 Start */
2153 -- This API will actually find out the budget year start date and
2154 -- end date for the input date. These values will be used to insert
2155 -- the distributions for each budget year.
2156 PROCEDURE Get_Budget_Boundaries
2157 ( x_return_status          OUT  NOCOPY  VARCHAR2,
2158   p_budget_year_start_date OUT  NOCOPY  DATE,
2159   p_budget_year_end_date   OUT  NOCOPY  DATE,
2160   p_input_date             IN           DATE
2161 ) IS
2162   l_year_index NUMBER;
2163 BEGIN
2164   x_return_status := FND_API.G_RET_STS_SUCCESS;
2165   FOR l_year_index in 1..PSB_WS_ACCT1.g_num_budget_years LOOP
2166     IF p_input_date >=
2167          PSB_WS_ACCT1.g_budget_years(l_year_index).start_date
2168       AND
2169        p_input_date <=
2170          PSB_WS_ACCT1.g_budget_years(l_year_index).end_date
2171     THEN
2172       p_budget_year_start_date
2173         := PSB_WS_ACCT1.g_budget_years(l_year_index).start_date;
2174       p_budget_year_end_date
2175         := PSB_WS_ACCT1.g_budget_years(l_year_index).end_date;
2176       /* Got the info so come out of the loop */
2177       EXIT;
2178      END IF;
2179    END LOOP;
2180    -- If any of the boundary dates are NULL, put p_return_status to FALSE
2181    IF p_budget_year_start_date IS NULL
2182        /* Bug 4060926 Start */
2183        OR p_budget_year_end_date IS NULL THEN
2184        /* Bug 4060926 End */
2185      x_return_status := FND_API.G_RET_STS_ERROR;
2186   END IF;
2187 END Get_Budget_Boundaries;
2188 /* Bug 3867577 End */
2189 /* ----------------------------------------------------------------------- */
2190 
2191 /* ----------------------------------------------------------------------- */
2192 /* Bug 3867577 Start */
2193 -- This API will first pick the SDE distributions those overlap with
2194 -- upload budget year(s). then it will modify these records in such a
2195 -- way that there should not be any overlapping. Now it will delete those
2196 -- overlapping distributions along with the insertion of modified
2197 -- distributions. finally it will pick WS/SDE distributions those lies in
2198 -- between upload year, and will insert them for SDE.
2199 PROCEDURE Upload_Salary_Distribution
2200 ( x_return_status          OUT  NOCOPY  VARCHAR2,
2201   p_worksheet_id           IN           NUMBER,
2202   p_source_data_extract_id IN           NUMBER,
2203   p_target_data_extract_id IN           NUMBER,
2204   p_position_id            IN           NUMBER,
2205   p_source_DE_position_id  IN           NUMBER,
2206   p_from_budget_year_id    IN           NUMBER,
2207   p_to_budget_year_id      IN           NUMBER,
2208   p_position_exists        IN           BOOLEAN
2209 ) IS
2210 
2211   l_year_start_date        DATE; --Will hold upload year start date
2212   l_year_end_date          DATE; --Will hold upload year end date
2213   l_budget_year_start_date DATE; --Will hold budget year start date
2214   l_budget_year_end_date   DATE; --Will hold budget year end date
2215   l_distr_start_date       DATE; --Will hold curr distribution start date
2216   l_distr_end_date         DATE; --Will hold curr distribution end date
2217   l_date_val               DATE; --Local var to hold the curr date value
2218   l_prev_distr_date        DATE;
2219 
2220   -- for bug 4507389
2221   l_calendar_start_date    DATE;
2222   -- for bug 4507389
2223 
2224   l_record_count           NUMBER :=0;
2225   l_chart_of_accounts_id   NUMBER;
2226   l_distribution_percent   NUMBER;
2227   l_code_combination_id    NUMBER;
2228   l_distribution_id        NUMBER;
2229   l_pos_line_id            NUMBER;
2230   l_rowid                  VARCHAR2(100);
2231   l_msg_count              NUMBER;
2232   l_msg_data               VARCHAR2(2000);
2233   l_return_status          VARCHAR2(1);
2234   l_count_ins_rec          NUMBER := 0;
2235   --l_pos_start_date         DATE; -- commented as part of bug fix 4507389
2236 
2237   /* Bug 4060926 Start */
2238   -- Variable used for the debugging.
2239   l_debug_info             VARCHAR2(1000);
2240   /* Bug 4060926 End */
2241 
2242   -------------------------------------------------------------------
2243   -- Declare three TYPES represnting required datatypes
2244   TYPE Number_tbl_type IS TABLE OF NUMBER        INDEX BY BINARY_INTEGER;
2245   TYPE Char_tbl_type   IS TABLE OF VARCHAR2(365) INDEX BY BINARY_INTEGER;
2246   TYPE Date_tbl_type   IS TABLE OF DATE          INDEX BY BINARY_INTEGER;
2247   -------------------------------------------------------------------
2248   --Record types those will hold the "existing" SDE distributions
2249   rec_distribution_id        Number_tbl_type;
2250   rec_position_id            Number_tbl_type;
2251   rec_data_extract_id        Number_tbl_type;
2252   rec_worksheet_id           Number_tbl_type;
2253   rec_start_date             Date_tbl_type;
2254   rec_end_date               Date_tbl_type;
2255   rec_chart_of_accounts_id   Number_tbl_type;
2256   rec_code_combination_id    Number_tbl_type;
2257   rec_distribution_percent   Number_tbl_type;
2258   rec_global_default_flag    Char_tbl_type;
2259   rec_dist_default_rule_id   Number_tbl_type;
2260   rec_project_id             Number_tbl_type;
2261   rec_task_id                Number_tbl_type;
2262   rec_award_id               Number_tbl_type;
2263   rec_exp_type               Char_tbl_type;
2264   rec_exp_org_id             Number_tbl_type;
2265   rec_cost_alloc_key_flex_id Number_tbl_type;
2266   rec_description            Char_tbl_type;
2267   -------------------------------------------------------------------
2268   -- Record types those will hold the "manipulated" SDE distributions
2269   ins_distribution_id        Number_tbl_type;
2270   ins_position_id            Number_tbl_type;
2271   ins_data_extract_id        Number_tbl_type;
2272   ins_worksheet_id           Number_tbl_type;
2273   ins_start_date             Date_tbl_type;
2274   ins_end_date               Date_tbl_type;
2275   ins_chart_of_accounts_id   Number_tbl_type;
2276   ins_code_combination_id    Number_tbl_type;
2277   ins_distribution_percent   Number_tbl_type;
2278   ins_global_default_flag    Char_tbl_type;
2279   ins_dist_default_rule_id   Number_tbl_type;
2280   ins_project_id             Number_tbl_type;
2281   ins_task_id                Number_tbl_type;
2282   ins_award_id               Number_tbl_type;
2283   ins_exp_type               Char_tbl_type;
2284   ins_exp_org_id             Number_tbl_type;
2285   ins_cost_alloc_key_flex_id Number_tbl_type;
2286   ins_description            Char_tbl_type;
2287   -------------------------------------------------------------------
2288   /* Bug 4060926 Start */
2289   -- Empty record types used for initialization
2290   /*
2291   -- No need for the empty record types.
2292   emp_distribution_id        Number_tbl_type;
2293   emp_position_id            Number_tbl_type;
2294   emp_data_extract_id        Number_tbl_type;
2295   emp_worksheet_id           Number_tbl_type;
2296   emp_start_date             Date_tbl_type;
2297   emp_end_date               Date_tbl_type;
2298   emp_chart_of_accounts_id   Number_tbl_type;
2299   emp_code_combination_id    Number_tbl_type;
2300   emp_distribution_percent   Number_tbl_type;
2301   emp_global_default_flag    Char_tbl_type;
2302   emp_dist_default_rule_id   Number_tbl_type;
2303   emp_project_id             Number_tbl_type;
2304   emp_task_id                Number_tbl_type;
2305   emp_award_id               Number_tbl_type;
2306   emp_exp_type               Char_tbl_type;
2307   emp_exp_org_id             Number_tbl_type;
2308   emp_cost_alloc_key_flex_id Number_tbl_type;
2309   emp_description            Char_tbl_type;
2310   */
2311 
2312   -- This cursor will fetch all the overlapping "existing" SDE
2313   -- distributions.
2314   CURSOR l_SDE_pos_distr_csr
2315   IS
2316   SELECT distribution_id, position_id, data_extract_id,
2317          worksheet_id, effective_start_date, effective_end_date,
2318          chart_of_accounts_id, code_combination_id, distribution_percent,
2319          global_default_flag, distribution_default_rule_id, project_id,
2320          task_id, award_id, expenditure_type, expenditure_organization_id,
2321          cost_allocation_key_flex_id, description
2322   FROM psb_position_pay_distributions b
2323   WHERE (((l_year_end_date IS NOT NULL)
2324            AND (((b.effective_start_date <= l_year_end_date)
2325            AND (b.effective_end_date IS NULL))
2326            OR ((b.effective_start_date
2327                   BETWEEN l_year_start_date AND l_year_end_date)
2328            OR (b.effective_end_date
2329                   BETWEEN l_year_start_date AND l_year_end_date)
2330            OR ((b.effective_start_date < l_year_start_date)
2331            AND (b.effective_end_date   > l_year_end_date)))))
2332            OR ((l_year_end_date IS NULL)
2333            AND (nvl(b.effective_end_date, l_year_start_date)
2334                       >= l_year_start_date)))
2335   AND position_id     = p_position_id
2336   AND worksheet_id IS NULL
2337   AND data_extract_id = p_target_data_extract_id;
2338 
2339   -- This cursor will pull all information from worksheet table
2340   -- only for above position_line_ids, SDE and worksheet_id.
2341   CURSOR l_WS_position_distr_csr(pos_id NUMBER, pos_line_id NUMBER,
2342                              start_date DATE, end_date DATE)
2343   IS
2344   SELECT DISTINCT
2345     pos_distr.distribution_id,     pos_distr.effective_start_date,
2346     pos_distr.effective_end_date,  pos_distr.chart_of_accounts_id,
2347     pos_distr.code_combination_id, pos_distr.distribution_percent
2348   FROM
2349     PSB_POSITION_PAY_DISTRIBUTIONS pos_distr
2350   WHERE EXISTS
2351     (
2352      SELECT 1
2353      FROM psb_ws_account_lines ws_lines
2354      WHERE pos_distr.code_combination_id = ws_lines.code_combination_id
2355      AND   ws_lines.salary_account_line  = 'Y'
2356      AND   ws_lines.ytd_amount           <> 0
2357      AND   ws_lines.budget_year_id BETWEEN p_from_budget_year_id
2358            AND p_to_budget_year_id
2359      AND ws_lines.position_line_id       = pos_line_id
2360     )
2361   AND pos_distr.position_id     = pos_id
2362   AND (
2363        (pos_distr.worksheet_id  = p_worksheet_id)
2364         OR (pos_distr.worksheet_id IS NULL
2365         AND NOT EXISTS
2366 	    (SELECT 1
2367          FROM PSB_POSITION_PAY_DISTRIBUTIONS pay_distr
2368 	     WHERE pay_distr.position_id = pos_distr.position_id
2369 	     AND pay_distr.worksheet_id  = p_worksheet_id
2370         )
2371        )
2372       )
2373 --  AND b.chart_of_accounts_id = p_gl_flex_code
2374   AND pos_distr.code_combination_id IS NOT NULL
2375   AND (((end_date IS NOT NULL)
2376   AND (((pos_distr.effective_start_date <= end_date)
2377   AND (pos_distr.effective_end_date IS NULL))
2378   OR ((pos_distr.effective_start_date BETWEEN start_date and end_date)
2379   OR (pos_distr.effective_end_date BETWEEN start_date and end_date)
2380   OR ((pos_distr.effective_start_date < start_date)
2381   AND (pos_distr.effective_end_date > end_date)))))
2382   OR ((end_date is null)
2383   AND (nvl(pos_distr.effective_end_date, start_date) >= start_date)))
2384   ORDER BY pos_distr.effective_start_date;
2385   -------------------------------------------------------------------
2386 BEGIN
2387   /* Bug 4060926 Start */
2388   SAVEPOINT Upload_Salary_Position;
2389   l_debug_info := ' Upload Salary Distr API starts';
2390 
2391   -- for bug 4507389
2392   -- get the calendar start date from the cache used in package PSB_WS_ACCT1
2393   l_calendar_start_date := PSB_WS_ACCT1.g_budget_years(1).start_date;
2394 
2395 
2396   /* Bug 4060926 End */
2397   -- Get the upload budget year start date and end date
2398 
2399   /*
2400   FOR l_year_index in 1..PSB_WS_ACCT1.g_num_budget_years LOOP
2401     IF PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id
2402         = p_from_budget_year_id THEN
2403       l_year_start_date
2404         := PSB_WS_ACCT1.g_budget_years(l_year_index).start_date;
2405     END IF;
2406 
2407     IF PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id
2408         = p_to_budget_year_id THEN
2409       l_year_end_date
2410         := PSB_WS_ACCT1.g_budget_years(l_year_index).end_date;
2411     END IF;
2412   END LOOP;
2413   */
2414 
2415   -- for bug 4507389
2416   -- The g_year_end_date and g_year_start_date are populated
2417   -- in API upload_worksheet where the budget calendar is cached.
2418   l_year_start_date := g_year_start_date;
2419   l_year_end_date := g_year_end_date;
2420 
2421 
2422   -- If position exists in SDE then we need to check the
2423   -- existing distributions
2424   IF p_position_exists THEN
2425     /* Bug 4060926 Start */
2426     l_debug_info := ' Position'||p_position_id||' exists';
2427     /* Bug 4060926 End */
2428     -- As the position exists in the target data extract, we need to
2429     -- manipulate the existing distributions. It's a data fix.
2430     OPEN l_SDE_pos_distr_csr;
2431     FETCH l_SDE_pos_distr_csr BULK COLLECT INTO
2432       rec_distribution_id, rec_position_id, rec_data_extract_id,
2433       rec_worksheet_id, rec_start_date, rec_end_date,
2434       rec_chart_of_accounts_id, rec_code_combination_id,
2435       rec_distribution_percent, rec_global_default_flag,
2436       rec_dist_default_rule_id, rec_project_id, rec_task_id,
2437       rec_award_id, rec_exp_type, rec_exp_org_id,
2438       rec_cost_alloc_key_flex_id, rec_description;
2439     Close l_SDE_pos_distr_csr;
2440 
2441     /* Bug 4060926 Start */
2442     l_debug_info := ' Starting the manipulation of the existing disr.';
2443     /* Bug 4060926 Start */
2444 
2445     FOR i in 1..rec_distribution_id.count
2446     LOOP
2447       -- If distribution is not end dated, manipulate
2448       -- them and populate them into record type.
2449       IF rec_end_date(i) IS NULL THEN
2450         IF rec_start_date(i) < l_year_start_date THEN
2451 
2452           SELECT PSB_POSITION_PAY_DISTR_S.NEXTVAL
2453           INTO l_distribution_id
2454           FROM dual;
2455 
2456           l_count_ins_rec                      := l_count_ins_rec + 1;
2457           ins_distribution_id(l_count_ins_rec) := l_distribution_id;
2458           ins_position_id(l_count_ins_rec)     := p_position_id;
2459           ins_data_extract_id(l_count_ins_rec)
2460             := p_target_data_extract_id;
2461           ins_worksheet_id(l_count_ins_rec) := NULL;
2462           ins_start_date(l_count_ins_rec)   := rec_start_date(i);
2463           ins_end_date(l_count_ins_rec)     := l_year_start_date - 1;
2464           ins_chart_of_accounts_id(l_count_ins_rec)
2465             := rec_chart_of_accounts_id(i);
2466           ins_code_combination_id(l_count_ins_rec)
2467             := rec_code_combination_id(i);
2468           ins_distribution_percent(l_count_ins_rec)
2469             := rec_distribution_percent(i);
2470           ins_global_default_flag(l_count_ins_rec)
2471             := rec_global_default_flag(i);
2472           ins_dist_default_rule_id(l_count_ins_rec)
2473             := rec_dist_default_rule_id(i);
2474           ins_project_id(l_count_ins_rec)  := rec_project_id(i);
2475           ins_task_id (l_count_ins_rec)    := rec_task_id(i);
2476           ins_award_id(l_count_ins_rec)    := rec_award_id(i);
2477           ins_exp_type(l_count_ins_rec)    := rec_exp_type(i);
2478           ins_exp_org_id(l_count_ins_rec)  := rec_exp_org_id(i);
2479           ins_cost_alloc_key_flex_id(l_count_ins_rec)
2480             := rec_cost_alloc_key_flex_id(i);
2481           ins_description(l_count_ins_rec) := rec_description(i);
2482         END IF;
2483 
2484         /*SELECT PSB_POSITION_PAY_DISTR_S.NEXTVAL
2485         INTO l_distribution_id
2486         FROM dual;
2487 
2488         l_count_ins_rec                      := l_count_ins_rec + 1;
2489         ins_distribution_id(l_count_ins_rec) := l_distribution_id;
2490         ins_position_id(l_count_ins_rec)     := p_position_id;
2491         ins_data_extract_id(l_count_ins_rec)
2492           := p_target_data_extract_id;
2493         ins_worksheet_id(l_count_ins_rec) := NULL;
2494         ins_start_date(l_count_ins_rec)   := l_year_end_date + 1;
2495         ins_end_date(l_count_ins_rec)     := NULL;
2496         ins_chart_of_accounts_id(l_count_ins_rec)
2497           := rec_chart_of_accounts_id(i);
2498         ins_code_combination_id(l_count_ins_rec)
2499           := rec_code_combination_id(i);
2500         ins_distribution_percent(l_count_ins_rec)
2501           := rec_distribution_percent(i);
2502         ins_global_default_flag(l_count_ins_rec)
2503           := rec_global_default_flag(i);
2504         ins_dist_default_rule_id(l_count_ins_rec)
2505           := rec_dist_default_rule_id(i);
2506         ins_project_id(l_count_ins_rec)  := rec_project_id(i);
2507         ins_task_id (l_count_ins_rec)    := rec_task_id(i);
2508         ins_award_id(l_count_ins_rec)    := rec_award_id(i);
2509         ins_exp_type(l_count_ins_rec)    := rec_exp_type(i);
2510         ins_exp_org_id(l_count_ins_rec)  := rec_exp_org_id(i);
2511         ins_cost_alloc_key_flex_id(l_count_ins_rec)
2512           := rec_cost_alloc_key_flex_id(i);
2513         ins_description(l_count_ins_rec) := rec_description(i);*/
2514 
2515       -- If distribution is end dated
2516       ELSE
2517         IF rec_start_date(i) < l_year_start_date THEN
2518 
2519           SELECT PSB_POSITION_PAY_DISTR_S.NEXTVAL
2520           INTO l_distribution_id
2521           FROM dual;
2522 
2523           l_count_ins_rec                      := l_count_ins_rec + 1;
2524           ins_distribution_id(l_count_ins_rec) := l_distribution_id;
2525           ins_position_id(l_count_ins_rec)     := p_position_id;
2526           ins_data_extract_id(l_count_ins_rec)
2527             := p_target_data_extract_id;
2528           ins_worksheet_id(l_count_ins_rec) := NULL;
2529           ins_start_date(l_count_ins_rec) := rec_start_date(i);
2530           ins_end_date(l_count_ins_rec)   := l_year_start_date - 1;
2531           ins_chart_of_accounts_id(l_count_ins_rec)
2532             := rec_chart_of_accounts_id(i);
2533           ins_code_combination_id(l_count_ins_rec)
2534             := rec_code_combination_id(i);
2535           ins_distribution_percent(l_count_ins_rec)
2536             := rec_distribution_percent(i);
2537           ins_global_default_flag(l_count_ins_rec)
2538             := rec_global_default_flag(i);
2539           ins_dist_default_rule_id(l_count_ins_rec)
2540             := rec_dist_default_rule_id(i);
2541           ins_project_id(l_count_ins_rec)  := rec_project_id(i);
2542           ins_task_id (l_count_ins_rec)    := rec_task_id(i);
2543           ins_award_id(l_count_ins_rec)    := rec_award_id(i);
2544           ins_exp_type(l_count_ins_rec)    := rec_exp_type(i);
2545           ins_exp_org_id(l_count_ins_rec)  := rec_exp_org_id(i);
2546           ins_cost_alloc_key_flex_id(l_count_ins_rec)
2547             := rec_cost_alloc_key_flex_id(i);
2548           ins_description(l_count_ins_rec) := rec_description(i);
2549 
2550           IF rec_end_date(i) > l_year_end_date THEN
2551 
2552             SELECT PSB_POSITION_PAY_DISTR_S.NEXTVAL
2553             INTO l_distribution_id
2554             FROM dual;
2555 
2556             l_count_ins_rec                      := l_count_ins_rec + 1;
2557             ins_distribution_id(l_count_ins_rec) := l_distribution_id;
2558             ins_position_id(l_count_ins_rec)     := p_position_id;
2559             ins_data_extract_id(l_count_ins_rec)
2560               := p_target_data_extract_id;
2561             ins_worksheet_id(l_count_ins_rec) := NULL;
2562             ins_start_date(l_count_ins_rec)   := l_year_end_date + 1;
2563             ins_end_date(l_count_ins_rec)     := rec_end_date(i);
2564             ins_chart_of_accounts_id(l_count_ins_rec)
2565               := rec_chart_of_accounts_id(i);
2566             ins_code_combination_id(l_count_ins_rec)
2567               := rec_code_combination_id(i);
2568             ins_distribution_percent(l_count_ins_rec)
2569               := rec_distribution_percent(i);
2570             ins_global_default_flag(l_count_ins_rec)
2571               := rec_global_default_flag(i);
2572             ins_dist_default_rule_id(l_count_ins_rec)
2573               := rec_dist_default_rule_id(i);
2574             ins_project_id(l_count_ins_rec)  := rec_project_id(i);
2575             ins_task_id (l_count_ins_rec)    := rec_task_id(i);
2576             ins_award_id(l_count_ins_rec)    := rec_award_id(i);
2577             ins_exp_type(l_count_ins_rec)    := rec_exp_type(i);
2578             ins_exp_org_id(l_count_ins_rec)  := rec_exp_org_id(i);
2579             ins_cost_alloc_key_flex_id(l_count_ins_rec)
2580               := rec_cost_alloc_key_flex_id(i);
2581             ins_description(l_count_ins_rec) := rec_description(i);
2582           END IF;
2583         ELSE
2584           -- Here we are only considering those records whose start date
2585           -- is in between upload start date and end date and end date is
2586           -- not between upload start date and end date. If so, then do
2587           -- nothing. These records will automatically be deleted.
2588           IF rec_end_date(i) > l_year_end_Date THEN
2589 
2590             SELECT PSB_POSITION_PAY_DISTR_S.NEXTVAL
2591             INTO l_distribution_id
2592             FROM dual;
2593 
2594             l_count_ins_rec                      := l_count_ins_rec + 1;
2595             ins_distribution_id(l_count_ins_rec) := l_distribution_id;
2596             ins_position_id(l_count_ins_rec)     := p_position_id;
2597             ins_data_extract_id(l_count_ins_rec)
2598               := p_target_data_extract_id;
2599             ins_worksheet_id(l_count_ins_rec) := NULL;
2600             ins_start_date(l_count_ins_rec)   := l_year_end_date + 1;
2601             ins_end_date(l_count_ins_rec)     := rec_end_date(i);
2602             ins_chart_of_accounts_id(l_count_ins_rec)
2603               := rec_chart_of_accounts_id(i);
2604             ins_code_combination_id(l_count_ins_rec)
2605               := rec_code_combination_id(i);
2606             ins_distribution_percent(l_count_ins_rec)
2607               := rec_distribution_percent(i);
2608             ins_global_default_flag(l_count_ins_rec)
2609               := rec_global_default_flag(i);
2610             ins_dist_default_rule_id(l_count_ins_rec)
2611               := rec_dist_default_rule_id(i);
2612             ins_project_id(l_count_ins_rec)  := rec_project_id(i);
2613             ins_task_id (l_count_ins_rec)    := rec_task_id(i);
2614             ins_award_id(l_count_ins_rec)    := rec_award_id(i);
2615             ins_exp_type(l_count_ins_rec)    := rec_exp_type(i);
2616             ins_exp_org_id(l_count_ins_rec)  := rec_exp_org_id(i);
2617             ins_cost_alloc_key_flex_id(l_count_ins_rec)
2618               := rec_cost_alloc_key_flex_id(i);
2619             ins_description(l_count_ins_rec) := rec_description(i);
2620           END IF;
2621         END IF;
2622       END IF;
2623     END LOOP;
2624 
2625     /* Bug 4060926 Start */
2626     l_debug_info := ' Manipulation of the existing disr completed.';
2627     /* Bug 4060926 Start */
2628 
2629     -- Now manipulated distributions are in the PL/SQL table.
2630     -- Delete c_pos_distr records and insert PL/SQL table records.
2631 
2632     FORALL i in 1..rec_distribution_id.COUNT
2633       DELETE FROM psb_position_pay_distributions
2634         WHERE distribution_id = rec_distribution_id(i);
2635 
2636     /* Bug 4060926 Start */
2637     l_debug_info := ' Bulk Deletion of the existing disr completed.';
2638     /* Bug 4060926 Start */
2639 
2640     FORALL i in 1..l_count_ins_rec
2641       INSERT INTO psb_position_pay_distributions(
2642         distribution_id, position_id, data_extract_id,
2643         worksheet_id, effective_start_date, effective_end_date,
2644         chart_of_accounts_id, code_combination_id, distribution_percent,
2645         global_default_flag, distribution_default_rule_id, project_id,
2646         task_id, award_id, expenditure_type, expenditure_organization_id,
2647         cost_allocation_key_flex_id, description, last_update_date,
2648         last_updated_by, last_update_login, created_by, creation_date)
2649      VALUES(
2650         ins_distribution_id(i), ins_position_id(i), ins_data_extract_id(i),
2651         ins_worksheet_id(i), ins_start_date(i), ins_end_date(i),
2652         ins_chart_of_accounts_id(i), ins_code_combination_id(i),
2653         ins_distribution_percent(i), ins_global_default_flag(i),
2654         ins_dist_default_rule_id(i), ins_project_id(i), ins_task_id(i),
2655         ins_award_id(i), ins_exp_type(i), ins_exp_org_id(i),
2656         ins_cost_alloc_key_flex_id(i), ins_description(i),
2657         SYSDATE, FND_GLOBAL.USER_ID, FND_GLOBAL.LOGIN_ID,
2658         FND_GLOBAL.USER_ID, SYSDATE
2659         );
2660     /* Bug 4060926 Start */
2661     l_debug_info := ' Bulk Insertion of the manipulated disr completed.';
2662     /* Bug 4060926 End */
2663   END IF;
2664 
2665     /* Bug 4060926 Start */
2666     l_debug_info := ' Out of the if block for existing positions.';
2667     /* Bug 4060926 End */
2668 
2669 
2670   -- Reinitialize the types
2671   l_count_ins_rec            := 0;
2672 
2673   /* Bug 4060926 Start */
2674   rec_distribution_id.DELETE;
2675   rec_position_id.DELETE;
2676   rec_data_extract_id.DELETE;
2677   rec_worksheet_id.DELETE;
2678   rec_start_date.DELETE;
2679   rec_end_date.DELETE;
2680   rec_chart_of_accounts_id.DELETE;
2681   rec_code_combination_id.DELETE;
2682   rec_distribution_percent.DELETE;
2683   rec_global_default_flag.DELETE;
2684   rec_dist_default_rule_id.DELETE;
2685   rec_project_id.DELETE;
2686   rec_task_id.DELETE;
2687   rec_award_id.DELETE;
2688   rec_exp_type.DELETE;
2689   rec_exp_org_id.DELETE;
2690   rec_cost_alloc_key_flex_id.DELETE;
2691   rec_description.DELETE;
2692 
2693   ins_distribution_id.DELETE;
2694   ins_position_id.DELETE;
2695   ins_data_extract_id.DELETE;
2696   ins_worksheet_id.DELETE;
2697   ins_start_date.DELETE;
2698   ins_end_date.DELETE;
2699   ins_chart_of_accounts_id.DELETE;
2700   ins_code_combination_id.DELETE;
2701   ins_distribution_percent.DELETE;
2702   ins_global_default_flag.DELETE;
2703   ins_dist_default_rule_id.DELETE;
2704   ins_project_id.DELETE;
2705   ins_task_id.DELETE;
2706   ins_award_id.DELETE;
2707   ins_exp_type.DELETE;
2708   ins_exp_org_id.DELETE;
2709   ins_cost_alloc_key_flex_id.DELETE;
2710   ins_description.DELETE;
2711 
2712   /* Bug 4060926 Start */
2713   -- No need of getting the position_start_date as this
2714   -- check no more exists.
2715   /*
2716   SELECT effective_start_date INTO l_pos_start_date
2717   FROM  psb_positions psb_pos
2718   WHERE psb_pos.position_id = p_source_DE_position_id;
2719   */
2720 
2721 
2722   -- fnd_file.put_line(fnd_file.log, ' Position start date : '||l_pos_start_date); -- commented as part of fix 4507389
2723 
2724   /* Bug 4060926 End */
2725 
2726   -- Now the SDE is containing the manipulated distributions. Fetch
2727   -- the relevent WS/SDE distributions and insert them directly after
2728   -- manipulating them.
2729 
2730   -- This statment will pull the position_line_ids from worksheet table
2731   -- those are existing in the worksheet to be uploaded.
2732   SELECT psb_pos_lines.position_line_id INTO l_pos_line_id
2733   FROM psb_ws_position_lines psb_pos_lines,
2734        psb_ws_lines_positions psb_lines_pos
2735   WHERE psb_pos_lines.position_line_id   = psb_lines_pos.position_line_id
2736   AND   psb_pos_lines.position_id        = p_source_DE_position_id
2737   AND   psb_lines_pos.worksheet_id       = p_worksheet_id;
2738 
2739   OPEN l_WS_position_distr_csr(p_source_DE_position_id, l_pos_line_id,
2740                                l_year_start_date, l_year_end_date
2741                               );
2742   FETCH l_WS_position_distr_csr BULK COLLECT INTO
2743     rec_distribution_id, rec_start_date,
2744     rec_end_date, rec_chart_of_accounts_id,
2745     rec_code_combination_id, rec_distribution_percent;
2746   CLOSE l_WS_position_distr_csr;
2747 
2748   /* Bug 4060926 Start */
2749   l_debug_info := ' Got all the distr existing in the ws to be uploaded';
2750   /* Bug 4060926 End */
2751 
2752   IF rec_distribution_id.FIRST IS NOT NULL
2753     AND rec_distribution_id.LAST IS NOT NULL
2754   THEN
2755     FOR indx IN rec_distribution_id.FIRST..rec_distribution_id.LAST
2756     LOOP
2757       BEGIN
2758 
2759         l_distr_start_date := rec_start_date(indx);
2760         l_distr_end_date   := rec_end_date(indx);
2761 
2762         -- If the position does not exist in the SDE, then
2763         -- we need to maintain the distribution from the start
2764         /*IF l_distr_start_date = l_pos_start_date
2765              AND
2766              p_position_exists = FALSE
2767         THEN
2768           SELECT PSB_POSITION_PAY_DISTR_S.NEXTVAL
2769           INTO l_distribution_id
2770           FROM dual;
2771 
2772           l_count_ins_rec                      := l_count_ins_rec + 1;
2773           ins_distribution_id(l_count_ins_rec) := l_distribution_id;
2774           ins_start_date(l_count_ins_rec)      := l_distr_start_date;
2775           ins_end_date(l_count_ins_rec)        := l_year_start_date - 1;
2776           ins_chart_of_accounts_id(l_count_ins_rec)
2777             := rec_chart_of_accounts_id(indx);
2778           ins_code_combination_id(l_count_ins_rec)
2779             := rec_code_combination_id(indx);
2780           ins_distribution_percent(l_count_ins_rec)
2781             := rec_distribution_percent(indx);
2782         END IF;*/
2783 
2784         -- IF end dating is done, we need to break them for budget
2785         -- year boundaries.
2786         IF l_distr_end_date is NOT NULL THEN
2787           -- If this is the first record then start with
2788           -- l_year_start_date otherwise start with l_distr_start_date.
2789           /* Bug 4060926 Start */
2790           --IF l_pos_start_date = l_distr_start_date THEN
2791 
2792 	  --for bug 4507389 -- used l_date_val instead of l_record_count
2793           --IF l_record_count = 1 THEN
2794  	  IF l_date_val IS NULL THEN
2795           /* Bug 4060926 End */
2796             -- This is the first distribution
2797             l_date_val             := l_year_start_date;
2798             l_budget_year_end_date := l_year_start_date;
2799           ELSE
2800             l_date_val             := l_distr_start_date;
2801             l_budget_year_end_date := l_distr_start_date;
2802           END IF;
2803 
2804           WHILE( l_budget_year_end_date < l_distr_end_date)
2805           LOOP
2806             Get_Budget_Boundaries
2807              (x_return_status          => l_return_status,
2808               p_budget_year_start_date => l_budget_year_start_date,
2809               p_budget_year_end_date   => l_budget_year_end_date,
2810               p_input_date             => l_date_val
2811              );
2812 
2813             IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2814               /* Bug 4060926 Start */
2815               l_debug_info := ' Error from 1st Get_Bud_bondaries API::>'
2816                                 ||l_date_val||','||l_budget_year_start_date
2817                                 ||l_budget_year_end_date;
2818               /* Bug 4060926 End */
2819               RAISE FND_API.G_EXC_ERROR;
2820             END IF;
2821 
2822             -- If current record is end dated in mid year then next
2823             -- iteration of above call will give l_budget_year_start_date as
2824             -- last_budget_year_start date, so we need to expand this value.
2825             IF l_budget_year_start_date < l_date_val THEN
2826               l_budget_year_start_date := l_date_val;
2827             END IF;
2828             -- If current record is again end dated in mid year then the
2829             -- current iteration of above call will give
2830             -- l_budget_year_end_date as last_budget_year_end date,
2831             -- so we need to cut down this value.
2832 
2833             IF l_budget_year_end_date > l_distr_end_date then
2834               l_budget_year_end_date  := l_distr_end_date;
2835              END IF;
2836 
2837             IF(l_budget_year_end_date <= l_distr_end_date) THEN
2838 
2839               SELECT PSB_POSITION_PAY_DISTR_S.NEXTVAL
2840               INTO l_distribution_id
2841               FROM dual;
2842               l_count_ins_rec                      := l_count_ins_rec + 1;
2843               ins_distribution_id(l_count_ins_rec) := l_distribution_id;
2844               ins_start_date(l_count_ins_rec)      := l_budget_year_start_date;
2845               ins_end_date(l_count_ins_rec)        := l_budget_year_end_date;
2846               ins_chart_of_accounts_id(l_count_ins_rec)
2847                 := rec_chart_of_accounts_id(indx);
2848               ins_code_combination_id(l_count_ins_rec)
2849                 := rec_code_combination_id(indx);
2850               ins_distribution_percent(l_count_ins_rec)
2851                 := rec_distribution_percent(indx);
2852 
2853               l_date_val := l_budget_year_end_date + 1;
2854 
2855             -- Insert the end dated distribution
2856             ELSE
2857               SELECT PSB_POSITION_PAY_DISTR_S.NEXTVAL
2858               INTO l_distribution_id
2859               FROM dual;
2860 
2861               l_count_ins_rec                      := l_count_ins_rec + 1;
2862               ins_distribution_id(l_count_ins_rec) := l_distribution_id;
2863               ins_start_date(l_count_ins_rec)      := l_date_val;
2864               ins_end_date(l_count_ins_rec)        := l_distr_end_date;
2865               ins_chart_of_accounts_id(l_count_ins_rec)
2866                 := rec_chart_of_accounts_id(indx);
2867               ins_code_combination_id(l_count_ins_rec)
2868                 := rec_code_combination_id(indx);
2869               ins_distribution_percent(l_count_ins_rec)
2870                 := rec_distribution_percent(indx);
2871 
2872               l_date_val := l_budget_year_end_date + 1;
2873             END IF;
2874           END LOOP;
2875         -- The current record is not end dated
2876         ELSE
2877           /* Bug 4060926 Start */
2878           --IF l_pos_start_date = l_distr_start_date THEN
2879 
2880 	  --for bug 4507389 -- used l_date_val instead of l_record_count
2881           --IF l_record_count = 1 THEN
2882 	    IF l_date_val IS NULL THEN
2883           /* Bug 4060926 End */
2884               -- This is the first distribution
2885               l_date_val := l_year_start_date;
2886             ELSE
2887 	      -- for bug 4507389
2888 	      -- Added an if condition which checks if the distributiuon
2889 	      -- start date falls outside the calendar start date. If yes
2890 	      -- push it to budget year start date
2891 	      IF l_distr_start_date < l_calendar_start_date THEN
2892 	        l_date_val := l_year_start_date;
2893 	      ELSE
2894                 l_date_val := l_distr_start_date;
2895 	      END IF;
2896             END IF;
2897 
2898           l_budget_year_start_date := l_date_val;
2899           WHILE(l_budget_year_start_date < l_year_end_date)
2900           LOOP
2901             Get_Budget_Boundaries
2902              (x_return_status          => l_return_status,
2903               p_budget_year_start_date => l_budget_year_start_date,
2904               p_budget_year_end_date   => l_budget_year_end_date,
2905               p_input_date             => l_date_val
2906              );
2907 
2908             IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2909               /* Bug 4060926 Start */
2910               l_debug_info := ' Error from 2nd Get_Bud_bondaries API::>'
2911                                 ||l_date_val||','||l_budget_year_start_date
2912                                 ||l_budget_year_end_date||','||p_position_id;
2913               /* Bug 4060926 End */
2914               RAISE FND_API.G_EXC_ERROR;
2915             END IF;
2916 
2917             SELECT PSB_POSITION_PAY_DISTR_S.NEXTVAL
2918             INTO l_distribution_id
2919             FROM dual;
2920             l_count_ins_rec                      := l_count_ins_rec + 1;
2921             ins_distribution_id(l_count_ins_rec) := l_distribution_id;
2922             ins_start_date(l_count_ins_rec)      := l_date_val;
2923             ins_end_date(l_count_ins_rec)        := l_budget_year_end_date;
2924             ins_chart_of_accounts_id(l_count_ins_rec)
2925               := rec_chart_of_accounts_id(indx);
2926             ins_code_combination_id(l_count_ins_rec)
2927               := rec_code_combination_id(indx);
2928             ins_distribution_percent(l_count_ins_rec)
2929               := rec_distribution_percent(indx);
2930 
2931             l_budget_year_start_date := l_budget_year_end_date + 1;
2932             l_date_val               := l_budget_year_start_date;
2933           END LOOP;
2934 
2935           /*-- Populate the PL/SQL table with a non end dated record
2936           -- for the open ws record.
2937           SELECT PSB_POSITION_PAY_DISTR_S.NEXTVAL
2938           INTO l_distribution_id
2939           FROM dual;
2940 
2941           l_count_ins_rec                      := l_count_ins_rec + 1;
2942           ins_distribution_id(l_count_ins_rec) := l_distribution_id;
2943           ins_start_date(l_count_ins_rec)      := l_year_end_date + 1;
2944           ins_end_date(l_count_ins_rec)        := NULL;
2945           ins_chart_of_accounts_id(l_count_ins_rec)
2946             := rec_chart_of_accounts_id(indx);
2947           ins_code_combination_id(l_count_ins_rec)
2948             := rec_code_combination_id(indx);
2949           ins_distribution_percent(l_count_ins_rec)
2950             := rec_distribution_percent(indx);*/
2951         END IF;
2952       END;
2953       l_prev_distr_date := rec_start_date(indx);
2954 
2955     END LOOP;
2956   END IF;
2957 
2958   /* Bug 4060926 Start */
2959   l_debug_info := ' The PL/SQL table is now ready for the final DML.';
2960   /* Bug 4060926 End */
2961 
2962   -- Finally do the DML for the PL/SQL table
2963   FOR indx IN 1..l_count_ins_rec
2964   LOOP
2965 
2966     PSB_POSITION_PAY_DISTR_PVT.Modify_Distribution
2967      (p_api_version                  => 1.0,
2968       p_return_status                => l_return_status,
2969       p_msg_count                    => l_msg_count,
2970       p_msg_data                     => l_msg_data,
2971       p_distribution_id              => ins_distribution_id(indx),
2972       p_position_id                  => p_position_id,
2973       p_data_extract_id              => p_target_data_extract_id,
2974       p_worksheet_id                 => NULL,
2975       p_effective_start_date         => ins_start_date(indx),
2976       p_effective_end_date           => ins_end_date(indx),
2977       p_chart_of_accounts_id         => ins_chart_of_accounts_id(indx),
2978       p_code_combination_id          => ins_code_combination_id(indx),
2979       p_distribution_percent         => ins_distribution_percent(indx),
2980       p_global_default_flag          => NULL,
2981       p_distribution_default_rule_id => NULL,
2982       p_rowid                        => l_rowid,
2983       p_project_id                   => NULL,
2984       p_task_id                      => NULL,
2985       p_award_id                     => NULL,
2986       p_expenditure_type             => NULL,
2987       p_expenditure_organization_id  => NULL,
2988       p_description                  => NULL
2989      );
2990 
2991      IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2992        /* Bug 4060926 Start */
2993        l_debug_info := ' Error from Mod_distr API for '||p_position_id;
2994        /* Bug 4060926 End */
2995        RAISE FND_API.G_EXC_ERROR;
2996      END IF;
2997   END LOOP;
2998 
2999 EXCEPTION
3000   WHEN FND_API.G_EXC_ERROR THEN
3001     /* Bug 4060926 Star */
3002     FND_FILE.PUT_LINE(FND_FILE.LOG, l_debug_info);
3003     /* Bug 4060926 End */
3004     ROLLBACK TO Upload_Salary_Position;
3005     x_return_status := FND_API.G_RET_STS_ERROR;
3006   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3007     /* Bug 4060926 Star */
3008     FND_FILE.PUT_LINE(FND_FILE.LOG, l_debug_info);
3009     /* Bug 4060926 End */
3010     ROLLBACK TO Upload_Salary_Position;
3011     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3012   WHEN OTHERS THEN
3013     /* Bug 4060926 Star */
3014     FND_FILE.PUT_LINE(FND_FILE.LOG, l_debug_info);
3015     /* Bug 4060926 End */
3016     ROLLBACK TO Upload_Salary_Position;
3017     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3018 END Upload_Salary_Distribution;
3019 /* ----------------------------------------------------------------------- */
3020 /* Bug 3867577 End */
3021 
3022 /* ----------------------------------------------------------------------- */
3023 PROCEDURE Upload_Positions
3024 ( p_return_status           OUT  NOCOPY  VARCHAR2,
3025   p_worksheet_id            IN   NUMBER,
3026   p_budget_calendar_id      IN   NUMBER,
3027   p_flex_mapping_set_id     IN   NUMBER,
3028   p_source_data_extract_id  IN   NUMBER,
3029   p_target_data_extract_id  IN   NUMBER,
3030   /* Bug 3867577 Start */
3031   -- Need to have the upload budget year ids.
3032   p_from_budget_year_id     IN   NUMBER,
3033   p_to_budget_year_id       IN   NUMBER
3034   /* Bug 3867577 End */
3035 ) IS
3036 
3037   l_position_assignment_id  NUMBER;
3038   l_pay_element_id          NUMBER;
3039   l_pay_element_option_id   NUMBER;
3040   l_position_id             NUMBER;
3041   l_attribute_value_id      NUMBER;
3042   l_distribution_id         NUMBER;
3043   l_employee_id             NUMBER;
3044 
3045   l_mapped_ccid             NUMBER;
3046   l_budget_year_type_id     NUMBER;
3047 
3048   l_year_start_date         DATE;
3049   l_year_end_date           DATE;
3050 
3051   l_position_exists         BOOLEAN;
3052 
3053   l_rowid                   VARCHAR2(100);
3054   l_msg_count               NUMBER;
3055   l_msg_data                VARCHAR2(2000);
3056   l_return_status           VARCHAR2(1);
3057 
3058   l_position_ctr            NUMBER := 0;
3059 
3060   /*For Bug No : 2434152 Start*/
3061   TYPE num20_arr    IS TABLE OF NUMBER(20);
3062   TYPE attr_value   IS TABLE OF PSB_ATTRIBUTE_VALUES.ATTRIBUTE_VALUE%TYPE;
3063   TYPE date_arr     IS TABLE OF DATE;
3064 
3065   TYPE attr_val_rec IS RECORD (attribute_id num20_arr, attribute_value_id num20_arr,attribute_value attr_value,effective_start_date date_arr,effective_end_date date_arr);
3066   l_attr_val        attr_val_rec;
3067   /*For Bug No : 2434152 End*/
3068 
3069   /* Bug 3867577 Start */
3070   -- This local variable will be used to determine the
3071   -- calling of API Upload_Salary_distribution.
3072   l_pos_distr_cnt  NUMBER;
3073   /* Bug 3867577 End */
3074 
3075 
3076 
3077   cursor c_positions is
3078     select pos.position_id, pos.position_definition_id, pos.hr_position_id, pos.hr_employee_id,pos.organization_id,
3079 	   pos.business_group_id, pos.effective_start_date, pos.effective_end_date,
3080 	   pos.set_of_books_id, pos.vacant_position_flag, pos.availability_status,
3081 	   pos.transaction_id, pos.transaction_status, pos.new_position_flag,
3082 	   pos.attribute1, pos.attribute2, pos.attribute3, pos.attribute4, pos.attribute5,
3083 	   pos.attribute6, pos.attribute7, pos.attribute8, pos.attribute9, pos.attribute10,
3084 	   pos.attribute11, pos.attribute12, pos.attribute13, pos.attribute14, pos.attribute15,
3085 	   pos.attribute16, pos.attribute17, pos.attribute18, pos.attribute19, pos.attribute20,
3086 	   pos.attribute_category, pos.name, wpl.budget_group_id
3087       from PSB_POSITIONS pos,
3088 	   PSB_WS_POSITION_LINES wpl,
3089 	   PSB_WS_LINES_POSITIONS wlp
3090      where data_extract_id = p_source_data_extract_id
3091        and wpl.position_id = pos.position_id
3092        and wlp.worksheet_id = p_worksheet_id
3093        and wlp.position_line_id = wpl.position_line_id;
3094 
3095  cursor c_position_exists (posname VARCHAR2, hrempid NUMBER, hrposid NUMBER) is
3096     select position_id
3097       from PSB_POSITIONS
3098      where name = posname
3099        and data_extract_id = p_target_data_extract_id
3100        and hr_position_id = hrposid
3101        -- For Bug number 2931727
3102        -- and (hrempid is null or hr_employee_id = hrempid);
3103        and (((hrempid is not null) and ((hr_employee_id = hrempid)
3104               or ((hr_employee_id is null) and
3105              not exists
3106              (select 1 from psb_positions
3107                where hr_position_id = hrposid
3108                  and hr_employee_id = hrempid
3109                  and data_extract_id = p_target_data_extract_id))))
3110             or ((hrempid is null) and (hr_employee_id is null)));
3111 
3112   cursor c_posassign_attr (positionid NUMBER) is
3113     select pas.attribute_id,
3114 	   pas.attribute_value_id,
3115 	   decode(pas.attribute_value_id,null,pas.attribute_value,patv.attribute_value) attribute_value,
3116 	   pas.effective_start_date,
3117 	   pas.effective_end_date
3118       from PSB_POSITION_ASSIGNMENTS pas,
3119 	   PSB_ATTRIBUTES pat,
3120 	   PSB_ATTRIBUTE_VALUES patv
3121      where pas.position_id = positionid
3122        and pas.assignment_type = 'ATTRIBUTE'
3123        and ((pas.worksheet_id = p_worksheet_id)
3124 	 or (worksheet_id is null
3125 	  and not exists
3126 	  (select 1 from psb_position_assignments c
3127 	    where c.position_id = positionid
3128 	      and c.attribute_id = pas.attribute_id
3129 	      and c.worksheet_id = p_worksheet_id)))
3130        and pas.attribute_id = pat.attribute_id
3131        and pas.attribute_value_id = patv.attribute_value_id(+);
3132 
3133   cursor c_re_attrval (attrvalid NUMBER) is
3134     select a.attribute_value_id
3135       from PSB_ATTRIBUTE_VALUES a,
3136 	   PSB_ATTRIBUTE_VALUES b
3137      where a.data_extract_id = p_target_data_extract_id
3138        and a.attribute_value = b.attribute_value
3139        and a.attribute_id    = b.attribute_id -- added for Bug#4262388
3140        and b.attribute_value_id = attrvalid;
3141 
3142   cursor c_posassign_elem (positionid NUMBER) is
3143     select pas.pay_element_id,
3144 	   pas.pay_element_option_id,
3145 	   pas.pay_element_rate_id,
3146 	   pas.effective_start_date,
3147 	   pas.effective_end_date,
3148 	   pas.element_value,
3149 	   pas.element_value_type,
3150 	   pas.currency_code,
3151 	   pas.pay_basis
3152       from PSB_POSITION_ASSIGNMENTS pas,
3153 	   PSB_PAY_ELEMENTS pe
3154      where pas.position_id = positionid
3155        and pas.assignment_type = 'ELEMENT'
3156        and ((pas.worksheet_id = p_worksheet_id)
3157 	 or (worksheet_id is null
3158 	  and not exists
3159 	     (select 1 from PSB_POSITION_ASSIGNMENTS c
3160 	       where c.position_id = positionid
3161 		 and c.pay_element_id = pas.pay_element_id
3162 		 and c.worksheet_id = p_worksheet_id)))
3163        and pas.pay_element_id = pe.pay_element_id;
3164 
3165   cursor c_re_elem (elemid NUMBER) is
3166     select a.pay_element_id
3167       from PSB_PAY_ELEMENTS a,
3168 	   PSB_PAY_ELEMENTS b
3169      where a.name = b.name
3170        and a.data_extract_id = p_target_data_extract_id
3171        and b.pay_element_id = elemid;
3172 
3173   cursor c_re_elemopt (elemid NUMBER, elemoptid NUMBER) is
3174     select a.pay_element_option_id
3175       from PSB_PAY_ELEMENT_OPTIONS a,
3176 	   PSB_PAY_ELEMENT_OPTIONS b
3177      where a.name = b.name
3178        and a.pay_element_id = elemid
3179        and b.pay_element_option_id = elemoptid
3180        and nvl(a.sequence_number, -1) = nvl(b.sequence_number, -1);
3181 
3182   cursor c_posassign_emp (positionid NUMBER) is
3183     select *
3184       from PSB_POSITION_ASSIGNMENTS pas
3185      where pas.position_id = positionid
3186        and pas.assignment_type = 'EMPLOYEE'
3187        and ((pas.worksheet_id = p_worksheet_id)
3188 	 or (pas.worksheet_id is null
3189        and not exists
3190 	  (select 1 from PSB_POSITION_ASSIGNMENTS c
3191 	    where c.position_id = positionid
3192 	      and c.primary_employee_flag = 'Y'
3193 	      and c.worksheet_id = p_worksheet_id)));
3194 
3195   cursor c_re_emp (empid NUMBER) is
3196     select a.employee_id
3197       from PSB_EMPLOYEES a,
3198 	   PSB_EMPLOYEES b
3199      /*For Bug No : 2434152 Start*/
3200      --where a.employee_number = b.employee_number
3201      where a.hr_employee_id = b.hr_employee_id
3202      /*For Bug No : 2434152 End*/
3203        and a.data_extract_id = p_target_data_extract_id
3204        and b.employee_id = empid;
3205 
3206   cursor c_position_distr (positionid NUMBER) is
3207     select *
3208       from PSB_POSITION_PAY_DISTRIBUTIONS a
3209      where a.position_id = positionid
3210        and ((a.worksheet_id = p_worksheet_id)
3211 	 or (a.worksheet_id is null
3212        and not exists
3213 	  (select 1 from PSB_POSITION_PAY_DISTRIBUTIONS c
3214 	    where c.position_id = positionid
3215 	      and c.worksheet_id = p_worksheet_id)));
3216 
3217   cursor c_pos_seq is
3218     select PSB_POSITIONS_S.NEXTVAL seq
3219       from dual;
3220 
3221   cursor c_posassign_seq is
3222     select PSB_POSITION_ASSIGNMENTS_S.NEXTVAL seq
3223       from dual;
3224 
3225   cursor c_posdistr_seq is
3226     select PSB_POSITION_PAY_DISTR_S.NEXTVAL seq
3227       from dual;
3228 
3229 BEGIN
3230 
3231   -- Loop for all positions in the source data extract
3232 
3233   for c_positions_rec in c_positions loop
3234     /* Bug 3867577 Start */
3235     l_pos_distr_cnt := 0;
3236     /* Bug 3867577 End */
3237 
3238     l_position_ctr := l_position_ctr + 1;
3239 
3240     if l_position_ctr = g_checkpoint_save THEN
3241       commit work;
3242       l_position_ctr := 0;
3243     end if;
3244 
3245     l_position_exists := FALSE;
3246 
3247     for c_position_exists_rec in c_position_exists (c_positions_rec.name, c_positions_rec.hr_employee_id, c_positions_rec.hr_position_id) loop
3248       l_position_exists := TRUE;
3249       l_position_id := c_position_exists_rec.position_id;
3250     end loop;
3251 
3252     -- If position does not exist in the target data extract create the position
3253 
3254     if not l_position_exists then
3255     begin
3256 
3257       for c_pos_seq_rec in c_pos_seq loop
3258 	l_position_id := c_pos_seq_rec.seq;
3259       end loop;
3260 
3261       PSB_POSITIONS_PVT.Insert_Row
3262 	 (p_api_version => 1.0,
3263 	  p_return_status => l_return_status,
3264 	  p_msg_count => l_msg_count,
3265 	  p_msg_data => l_msg_data,
3266 	  p_rowid => l_rowid,
3267 	  p_position_id => l_position_id,
3268 	  p_budget_group_id => c_positions_rec.budget_group_id,
3269 	  p_data_extract_id => p_target_data_extract_id,
3270 	  p_position_definition_id => c_positions_rec.position_definition_id,
3271 	  p_hr_position_id => c_positions_rec.hr_position_id,
3272 	  p_hr_employee_id => c_positions_rec.hr_employee_id,
3273 	  p_business_group_id => c_positions_rec.business_group_id,
3274 	  -- de by org
3275 	  p_organization_id => c_positions_rec.organization_id,
3276 	  p_effective_start_date => c_positions_rec.effective_start_date,
3277 	  p_effective_end_date => c_positions_rec.effective_end_date,
3278 	  p_set_of_books_id => c_positions_rec.set_of_books_id,
3279 	  p_vacant_position_flag => c_positions_rec.vacant_position_flag,
3280 	  p_availability_status => c_positions_rec.availability_status,
3281 	  p_transaction_id => c_positions_rec.transaction_id,
3282 	  p_transaction_status => c_positions_rec.transaction_status,
3283 	  p_attribute1 => c_positions_rec.attribute1,
3284 	  p_attribute2 => c_positions_rec.attribute2,
3285 	  p_attribute3 => c_positions_rec.attribute3,
3286 	  p_attribute4 => c_positions_rec.attribute4,
3287 	  p_attribute5 => c_positions_rec.attribute5,
3288 	  p_attribute6 => c_positions_rec.attribute6,
3289 	  p_attribute7 => c_positions_rec.attribute7,
3290 	  p_attribute8 => c_positions_rec.attribute8,
3291 	  p_attribute9 => c_positions_rec.attribute9,
3292 	  p_attribute10 => c_positions_rec.attribute10,
3293 	  p_attribute11 => c_positions_rec.attribute11,
3294 	  p_attribute12 => c_positions_rec.attribute12,
3295 	  p_attribute13 => c_positions_rec.attribute13,
3296 	  p_attribute14 => c_positions_rec.attribute14,
3297 	  p_attribute15 => c_positions_rec.attribute15,
3298 	  p_attribute16 => c_positions_rec.attribute16,
3299 	  p_attribute17 => c_positions_rec.attribute17,
3300 	  p_attribute18 => c_positions_rec.attribute18,
3301 	  p_attribute19 => c_positions_rec.attribute19,
3302 	  p_attribute20 => c_positions_rec.attribute20,
3303 	  p_attribute_category => c_positions_rec.attribute_category,
3304 	  p_name => c_positions_rec.name);
3305 
3306       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3307 	raise FND_API.G_EXC_ERROR;
3308       end if;
3309 
3310       -- Upload Position Attribute Assignments
3311       open c_posassign_attr(c_positions_rec.position_id);
3312       loop
3313 	fetch c_posassign_attr BULK COLLECT INTO l_attr_val.attribute_id,l_attr_val.attribute_value_id,
3314 						l_attr_val.attribute_value,l_attr_val.effective_start_date,
3315 						l_attr_val.effective_end_date LIMIT g_limit_bulk_numrows;
3316 
3317 	for l_attr_index in 1..l_attr_val.attribute_id.count loop
3318 
3319 	  if l_attr_val.attribute_value_id(l_attr_index) is not null then
3320 	  begin
3321 
3322 	    for c_re_attrval_rec in c_re_attrval (l_attr_val.attribute_value_id(l_attr_index)) loop
3323 	      l_attribute_value_id := c_re_attrval_rec.attribute_value_id;
3324 	    end loop;
3325 
3326 	  end;
3327 	  else
3328 	    l_attribute_value_id := null;
3329 	  end if;
3330 
3331 	  for c_posassign_seq_rec in c_posassign_seq loop
3332 	    l_position_assignment_id := c_posassign_seq_rec.seq;
3333 	  end loop;
3334 
3335 	  PSB_POSITION_ASSIGNMENTS_PVT.Insert_Row
3336 	   (p_api_version => 1.0,
3337 	    p_return_status => l_return_status,
3338 	    p_msg_count => l_msg_count,
3339 	    p_msg_data => l_msg_data,
3340 	    p_rowid => l_rowid,
3341 	    p_position_assignment_id => l_position_assignment_id,
3342 	    p_data_extract_id => p_target_data_extract_id,
3343 	    p_worksheet_id => null,
3344 	    p_position_id => l_position_id,
3345 	    p_assignment_type => 'ATTRIBUTE',
3346 	    p_attribute_id => l_attr_val.attribute_id(l_attr_index),
3347 	    p_attribute_value_id => l_attribute_value_id,
3348 	    p_attribute_value => l_attr_val.attribute_value(l_attr_index),
3349 	    p_pay_element_id => null,
3350 	    p_pay_element_option_id => null,
3351 	    p_effective_start_date => l_attr_val.effective_start_date(l_attr_index),
3352 	    p_effective_end_date => l_attr_val.effective_end_date(l_attr_index),
3353 	    p_element_value_type => null,
3354 	    p_element_value => null,
3355 	    p_currency_code => null,
3356 	    p_pay_basis => null,
3357 	    p_employee_id => null,
3358 	    p_primary_employee_flag => null,
3359 	    p_global_default_flag => null,
3360 	    p_assignment_default_rule_id => null,
3361 	    p_modify_flag => null);
3362 
3363 	  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3364 	    raise FND_API.G_EXC_ERROR;
3365 	  end if;
3366 
3367 	end loop;
3368 	exit when c_posassign_attr%NOTFOUND;
3369 
3370       end loop;
3371       close c_posassign_attr;
3372 
3373       -- Upload Position Element Assignments
3374 
3375       for c_posassign_elem_rec in c_posassign_elem (c_positions_rec.position_id) loop
3376 
3377 	for c_re_elem_rec in c_re_elem (c_posassign_elem_rec.pay_element_id) loop
3378 	  l_pay_element_id := c_re_elem_rec.pay_element_id;
3379 	end loop;
3380 
3381 	if c_posassign_elem_rec.pay_element_option_id is not null then
3382 	begin
3383 
3384 	  for c_re_elemopt_rec in c_re_elemopt (l_pay_element_id, c_posassign_elem_rec.pay_element_option_id) loop
3385 	    l_pay_element_option_id := c_re_elemopt_rec.pay_element_option_id;
3386 	  end loop;
3387 
3388 	end;
3389 	else
3390 	  l_pay_element_option_id := null;
3391 	end if;
3392 
3393 	for c_posassign_seq_rec in c_posassign_seq loop
3394 	  l_position_assignment_id := c_posassign_seq_rec.seq;
3395 	end loop;
3396 
3397 	PSB_POSITION_ASSIGNMENTS_PVT.Insert_Row
3398 	   (p_api_version => 1.0,
3399 	    p_return_status => l_return_status,
3400 	    p_msg_count => l_msg_count,
3401 	    p_msg_data => l_msg_data,
3402 	    p_rowid => l_rowid,
3403 	    p_position_assignment_id => l_position_assignment_id,
3404 	    p_data_extract_id => p_target_data_extract_id,
3405 	    p_worksheet_id => null,
3406 	    p_position_id => l_position_id,
3407 	    p_assignment_type => 'ELEMENT',
3408 	    p_attribute_id => null,
3409 	    p_attribute_value_id => null,
3410 	    p_attribute_value => null,
3411 	    p_pay_element_id => l_pay_element_id,
3412 	    p_pay_element_option_id => l_pay_element_option_id,
3413 	    p_effective_start_date => c_posassign_elem_rec.effective_start_date,
3414 	    p_effective_end_date => c_posassign_elem_rec.effective_end_date,
3415 	    p_element_value_type => c_posassign_elem_rec.element_value_type,
3416 	    p_element_value => c_posassign_elem_rec.element_value,
3417 	    p_currency_code => c_posassign_elem_rec.currency_code,
3418 	    p_pay_basis => c_posassign_elem_rec.pay_basis,
3419 	    p_employee_id => null,
3420 	    p_primary_employee_flag => null,
3421 	    p_global_default_flag => null,
3422 	    p_assignment_default_rule_id => null,
3423 	    p_modify_flag => null);
3424 
3425 	if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3426 	  raise FND_API.G_EXC_ERROR;
3427 	end if;
3428 
3429       end loop;
3430 
3431       -- Upload Position Employee Assignments
3432 
3433       for c_posassign_emp_rec in c_posassign_emp (c_positions_rec.position_id) loop
3434 
3435 	for c_re_emp_rec in c_re_emp (c_posassign_emp_rec.employee_id) loop
3436 	  l_employee_id := c_re_emp_rec.employee_id;
3437 	end loop;
3438 
3439 	for c_posassign_seq_rec in c_posassign_seq loop
3440 	  l_position_assignment_id := c_posassign_seq_rec.seq;
3441 	end loop;
3442 
3443 	PSB_POSITION_ASSIGNMENTS_PVT.Insert_Row
3444 	   (p_api_version => 1.0,
3445 	    p_return_status => l_return_status,
3446 	    p_msg_count => l_msg_count,
3447 	    p_msg_data => l_msg_data,
3448 	    p_rowid => l_rowid,
3449 	    p_position_assignment_id => l_position_assignment_id,
3450 	    p_data_extract_id => p_target_data_extract_id,
3451 	    p_worksheet_id => null,
3452 	    p_position_id => l_position_id,
3453 	    p_assignment_type => 'EMPLOYEE',
3454 	    p_attribute_id => null,
3455 	    p_attribute_value_id => null,
3456 	    p_attribute_value => null,
3457 	    p_pay_element_id => null,
3458 	    p_pay_element_option_id => null,
3459 	    p_effective_start_date => c_posassign_emp_rec.effective_start_date,
3460 	    p_effective_end_date => c_posassign_emp_rec.effective_end_date,
3461 	    p_element_value_type => null,
3462 	    p_element_value => null,
3463 	    p_currency_code => null,
3464 	    p_pay_basis => null,
3465 	    p_employee_id => l_employee_id,
3466 	    p_primary_employee_flag => c_posassign_emp_rec.primary_employee_flag,
3467 	    p_global_default_flag => null,
3468 	    p_assignment_default_rule_id => null,
3469 	    p_modify_flag => null);
3470 
3471 	if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3472 	  raise FND_API.G_EXC_ERROR;
3473 	end if;
3474 
3475       end loop;
3476 
3477       -- Upload Position Salary Distributions
3478 
3479       for c_position_distr_rec in c_position_distr (c_positions_rec.position_id) loop
3480         /* Bug 3867577 Start */
3481         l_pos_distr_cnt := l_pos_distr_cnt + 1;
3482         /* Bug 3867577 End */
3483 	-- if flex mapping was used in worksheet creation need to map the salary account distr
3484 	-- by effective dates
3485 
3486 	if p_flex_mapping_set_id is not null then
3487 	begin
3488 
3489 	  for l_year_index in 1..PSB_WS_ACCT1.g_num_budget_years loop
3490 
3491 	    if PSB_WS_ACCT1.g_budget_years(l_year_index).year_type in ('CY', 'PP') then
3492 	    begin
3493 
3494 	      l_budget_year_type_id := PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_type_id;
3495 	      l_year_start_date := PSB_WS_ACCT1.g_budget_years(l_year_index).start_date;
3496 	      l_year_end_date := PSB_WS_ACCT1.g_budget_years(l_year_index).end_date;
3497 
3498 	      if (((c_position_distr_rec.effective_start_date < l_year_end_date) and
3499 		   (c_position_distr_rec.effective_end_date is null)) or
3500 		  ((c_position_distr_rec.effective_start_date between l_year_start_date and l_year_end_date) or
3501 		   (c_position_distr_rec.effective_end_date between l_year_start_date and l_year_end_date) or
3502 		  ((c_position_distr_rec.effective_start_date < l_year_start_date) and
3503 		   (c_position_distr_rec.effective_end_date > l_year_end_date)))) then
3504 	      begin
3505 
3506 		l_mapped_ccid := PSB_FLEX_MAPPING_PVT.Get_Mapped_CCID
3507 				    (p_api_version => 1.0,
3508 				     p_ccid => c_position_distr_rec.code_combination_id,
3509 				     p_budget_year_type_id => l_budget_year_type_id,
3510 				     p_flexfield_mapping_set_id => p_flex_mapping_set_id,
3511 				     p_mapping_mode => 'GL_POSTING');
3512 
3513 		if l_mapped_ccid = 0 then
3514 		  raise FND_API.G_EXC_ERROR;
3515 		end if;
3516 
3517 		for c_posdistr_seq_rec in c_posdistr_seq loop
3518 		  l_distribution_id := c_posdistr_seq_rec.seq;
3519 		end loop;
3520 
3521 		PSB_POSITION_PAY_DISTR_PVT.Insert_Row
3522 		   (p_api_version => 1.0,
3523 		    p_return_status => l_return_status,
3524 		    p_msg_count => l_msg_count,
3525 		    p_msg_data => l_msg_data,
3526 		    p_rowid => l_rowid,
3527 		    p_distribution_id => l_distribution_id,
3528 		    p_position_id => l_position_id,
3529 		    p_data_extract_id => p_target_data_extract_id,
3530 		    p_worksheet_id => null,
3531 		    p_effective_start_date => l_year_start_date,
3532 		    p_effective_end_date => l_year_end_date,
3533 		    p_chart_of_accounts_id => c_position_distr_rec.chart_of_accounts_id,
3534 		    p_code_combination_id => l_mapped_ccid,
3535 		    p_distribution_percent => c_position_distr_rec.distribution_percent,
3536 		    p_global_default_flag => null,
3537 		    p_distribution_default_rule_id => null,
3538 		    p_project_id => null,
3539 		    p_task_id => null,
3540 		    p_award_id => null,
3541 		    p_expenditure_type => null,
3542 		    p_expenditure_organization_id => null,
3543 		    p_description => null);
3544 
3545 		if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3546 		  raise FND_API.G_EXC_ERROR;
3547 		end if;
3548 
3549 	      end;
3550 	      end if;
3551 
3552 	    end;
3553 	    end if;
3554 
3555 	  end loop;
3556 
3557 	end;
3558 	else
3559 	begin
3560       /* Bug 3867577 Start */
3561       -- Call the following API only once for each position.
3562       IF l_pos_distr_cnt = 1 THEN
3563         Upload_Salary_Distribution
3564           (
3565             x_return_status          => l_return_status,
3566             p_worksheet_id           => p_worksheet_id,
3567             p_source_data_extract_id => p_source_data_extract_id,
3568             p_target_data_extract_id => p_target_data_extract_id,
3569             p_position_id            => l_position_id,
3570             p_source_DE_position_id  => c_positions_rec.position_id,
3571             p_from_budget_year_id    => p_from_budget_year_id,
3572             p_to_budget_year_id      => p_to_budget_year_id,
3573             p_position_exists        => FALSE
3574           );
3575 
3576         IF  l_return_status <> FND_API.G_RET_STS_SUCCESS then
3577 	      RAISE FND_API.G_EXC_ERROR;
3578         END IF;
3579       END IF;
3580 
3581 
3582 
3583       -- Commenting out the existing bunch of code as no enddating
3584       -- scenario was handelled before. This issue will be handelled
3585       -- new API Upload_Salary_Distribution.
3586 
3587 	  /*for c_posdistr_seq_rec in c_posdistr_seq loop
3588 	    l_distribution_id := c_posdistr_seq_rec.seq;
3589 	  end loop;
3590 
3591 	  PSB_POSITION_PAY_DISTR_PVT.Insert_Row
3592 	     (p_api_version => 1.0,
3593 	      p_return_status => l_return_status,
3594 	      p_msg_count => l_msg_count,
3595 	      p_msg_data => l_msg_data,
3596 	      p_rowid => l_rowid,
3597 	      p_distribution_id => l_distribution_id,
3598 	      p_position_id => l_position_id,
3599 	      p_data_extract_id => p_target_data_extract_id,
3600 	      p_worksheet_id => null,
3601 	      p_effective_start_date => c_position_distr_rec.effective_start_date,
3602 	      p_effective_end_date => c_position_distr_rec.effective_end_date,
3603 	      p_chart_of_accounts_id => c_position_distr_rec.chart_of_accounts_id,
3604 	      p_code_combination_id => c_position_distr_rec.code_combination_id,
3605 	      p_distribution_percent => c_position_distr_rec.distribution_percent,
3606 	      p_global_default_flag => null,
3607 	      p_distribution_default_rule_id => null,
3608 	      p_project_id => null,
3609 	      p_task_id => null,
3610 	      p_award_id => null,
3611 	      p_expenditure_type => null,
3612 	      p_expenditure_organization_id => null,
3613 	      p_description => null);
3614 
3615 	  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3616 	    raise FND_API.G_EXC_ERROR;
3617 	  end if;*/
3618       /* Bug 3867577 End */
3619 
3620 	end;
3621 	end if;
3622 
3623       end loop;
3624 
3625     end; /* Position does not exist */
3626     else
3627 
3628 
3629     begin /* Position exists; do an incremental refresh in this case */
3630 
3631          PSB_POSITIONS_PVT.UPDATE_ROW
3632          (
3633 	  p_api_version            => 1.0,
3634 	  p_init_msg_lISt          => FND_API.G_FALSE,
3635 	  p_commit                 => FND_API.G_FALSE,
3636 	  p_validation_level       => FND_API.G_VALID_LEVEL_FULL,
3637 	  p_return_status          => l_return_status,
3638 	  p_msg_count              => l_msg_count,
3639 	  p_msg_data               => l_msg_data,
3640 	  p_position_id            => l_position_id,
3641 	  p_data_extract_id        => p_target_data_extract_id,
3642 	  p_budget_group_id        => c_positions_rec.budget_group_id,
3643 	  p_position_definition_id => c_positions_rec.position_definition_id,
3644 	  p_hr_position_id         => c_positions_rec.hr_position_id,
3645 	  p_hr_employee_id         => c_positions_rec.hr_employee_id,
3646 	  p_business_group_id      => c_positions_rec.business_group_id,
3647 	  p_effective_start_date   => c_positions_rec.effective_start_date,
3648 	  p_effective_end_date     => c_positions_rec.effective_end_date,
3649 	  p_set_of_books_id        => c_positions_rec.set_of_books_id,
3650 	  p_vacant_position_flag   => c_positions_rec.vacant_position_flag,
3651 	  p_availability_status    => c_positions_rec.availability_status,
3652 	  p_transaction_id         => c_positions_rec.transaction_id,
3653 	  p_transaction_status     => c_positions_rec.transaction_status,
3654 	  p_attribute1             => c_positions_rec.attribute1,
3655 	  p_attribute2             => c_positions_rec.attribute2,
3656 	  p_attribute3             => c_positions_rec.attribute3,
3657 	  p_attribute4             => c_positions_rec.attribute4,
3658 	  p_attribute5             => c_positions_rec.attribute5,
3659 	  p_attribute6             => c_positions_rec.attribute6,
3660 	  p_attribute7             => c_positions_rec.attribute7,
3661 	  p_attribute8             => c_positions_rec.attribute8,
3662 	  p_attribute9             => c_positions_rec.attribute9,
3663 	  p_attribute10            => c_positions_rec.attribute10,
3664 	  p_attribute11            => c_positions_rec.attribute11,
3665 	  p_attribute12            => c_positions_rec.attribute12,
3666 	  p_attribute13            => c_positions_rec.attribute13,
3667 	  p_attribute14            => c_positions_rec.attribute14,
3668 	  p_attribute15            => c_positions_rec.attribute15,
3669 	  p_attribute16            => c_positions_rec.attribute16,
3670 	  p_attribute17            => c_positions_rec.attribute17,
3671 	  p_attribute18            => c_positions_rec.attribute18,
3672 	  p_attribute19            => c_positions_rec.attribute19,
3673 	  p_attribute20            => c_positions_rec.attribute20,
3674 	  p_attribute_category     => c_positions_rec.attribute_category,
3675 	  p_name                   => c_positions_rec.name);
3676 
3677       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3678 	raise FND_API.G_EXC_ERROR;
3679       end if;
3680 
3681       -- Upload Position Attribute Assignments
3682 
3683       open c_posassign_attr(c_positions_rec.position_id);
3684       loop
3685 	fetch c_posassign_attr BULK COLLECT INTO l_attr_val.attribute_id,l_attr_val.attribute_value_id,
3686 						l_attr_val.attribute_value,l_attr_val.effective_start_date,
3687 						l_attr_val.effective_end_date LIMIT g_limit_bulk_numrows;
3688 
3689       for l_attr_index in 1..l_attr_val.attribute_id.count loop
3690 
3691 	  if l_attr_val.attribute_value_id(l_attr_index) is not null then
3692 	  begin
3693 
3694 	    for c_re_attrval_rec in c_re_attrval (l_attr_val.attribute_value_id(l_attr_index)) loop
3695 	      l_attribute_value_id := c_re_attrval_rec.attribute_value_id;
3696 	    end loop;
3697 
3698 	  end;
3699 	  else
3700 	    l_attribute_value_id := null;
3701 	  end if;
3702 
3703 	  PSB_POSITIONS_PVT.Modify_Assignment
3704 	   (p_api_version => 1.0,
3705 	    p_return_status => l_return_status,
3706 	    p_msg_count => l_msg_count,
3707 	    p_msg_data => l_msg_data,
3708 	    p_position_assignment_id => l_position_assignment_id,
3709 	    p_data_extract_id => p_target_data_extract_id,
3710 	    p_worksheet_id => null,
3711 	    p_position_id => l_position_id,
3712 	    p_assignment_type => 'ATTRIBUTE',
3713 	    p_attribute_id => l_attr_val.attribute_id(l_attr_index),
3714 	    p_attribute_value_id => l_attribute_value_id,
3715 	    p_attribute_value => l_attr_val.attribute_value(l_attr_index),
3716 	    p_pay_element_id => null,
3717 	    p_pay_element_option_id => null,
3718 	    p_effective_start_date => l_attr_val.effective_start_date(l_attr_index),
3719 	    p_effective_end_date => l_attr_val.effective_end_date(l_attr_index),
3720 	    p_element_value_type => null,
3721 	    p_element_value => null,
3722 	    p_currency_code => null,
3723 	    p_pay_basis => null,
3724 	    p_employee_id => null,
3725 	    p_primary_employee_flag => null,
3726 	    p_global_default_flag => null,
3727 	    p_assignment_default_rule_id => null,
3728 	    p_modify_flag => null,
3729 	    p_rowid => l_rowid);
3730 
3731 	  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3732 	    raise FND_API.G_EXC_ERROR;
3733 	  end if;
3734 
3735 	end loop;
3736 	exit when c_posassign_attr%NOTFOUND;
3737 
3738       end loop;
3739       close c_posassign_attr;
3740 
3741       -- Upload Position Element Assignments
3742 
3743       for c_posassign_elem_rec in c_posassign_elem (c_positions_rec.position_id) loop
3744 
3745 	for c_re_elem_rec in c_re_elem (c_posassign_elem_rec.pay_element_id) loop
3746 	  l_pay_element_id := c_re_elem_rec.pay_element_id;
3747 	end loop;
3748 
3749 	if c_posassign_elem_rec.pay_element_option_id is not null then
3750 	begin
3751 
3752 	  for c_re_elemopt_rec in c_re_elemopt (l_pay_element_id, c_posassign_elem_rec.pay_element_option_id) loop
3753 	    l_pay_element_option_id := c_re_elemopt_rec.pay_element_option_id;
3754 	  end loop;
3755 
3756 	end;
3757 	else
3758 	  l_pay_element_option_id := null;
3759 	end if;
3760 
3761 	PSB_POSITIONS_PVT.Modify_Assignment
3762 	   (p_api_version => 1.0,
3763 	    p_return_status => l_return_status,
3764 	    p_msg_count => l_msg_count,
3765 	    p_msg_data => l_msg_data,
3766 	    p_position_assignment_id => l_position_assignment_id,
3767 	    p_data_extract_id => p_target_data_extract_id,
3768 	    p_worksheet_id => null,
3769 	    p_position_id => l_position_id,
3770 	    p_assignment_type => 'ELEMENT',
3771 	    p_attribute_id => null,
3772 	    p_attribute_value_id => null,
3773 	    p_attribute_value => null,
3774 	    p_pay_element_id => l_pay_element_id,
3775 	    p_pay_element_option_id => l_pay_element_option_id,
3776 	    p_effective_start_date => c_posassign_elem_rec.effective_start_date,
3777 	    p_effective_end_date => c_posassign_elem_rec.effective_end_date,
3778 	    p_element_value_type => c_posassign_elem_rec.element_value_type,
3779 	    p_element_value => c_posassign_elem_rec.element_value,
3780 	    p_currency_code => c_posassign_elem_rec.currency_code,
3781 	    p_pay_basis => c_posassign_elem_rec.pay_basis,
3782 	    p_employee_id => null,
3783 	    p_primary_employee_flag => null,
3784 	    p_global_default_flag => null,
3785 	    p_assignment_default_rule_id => null,
3786 	    p_modify_flag => null,
3787 	    p_rowid => l_rowid);
3788 
3789 	if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3790 	  raise FND_API.G_EXC_ERROR;
3791 	end if;
3792 
3793       end loop;
3794 
3795       -- Upload Position Employee Assignments
3796 
3797       for c_posassign_emp_rec in c_posassign_emp (c_positions_rec.position_id) loop
3798 
3799 	for c_re_emp_rec in c_re_emp (c_posassign_emp_rec.employee_id) loop
3800 	  l_employee_id := c_re_emp_rec.employee_id;
3801 	end loop;
3802 
3803 	PSB_POSITIONS_PVT.Modify_Assignment
3804 	   (p_api_version => 1.0,
3805 	    p_return_status => l_return_status,
3806 	    p_msg_count => l_msg_count,
3807 	    p_msg_data => l_msg_data,
3808 	    p_position_assignment_id => l_position_assignment_id,
3809 	    p_data_extract_id => p_target_data_extract_id,
3810 	    p_worksheet_id => null,
3811 	    p_position_id => l_position_id,
3812 	    p_assignment_type => 'EMPLOYEE',
3813 	    p_attribute_id => null,
3814 	    p_attribute_value_id => null,
3815 	    p_attribute_value => null,
3816 	    p_pay_element_id => null,
3817 	    p_pay_element_option_id => null,
3818 	    p_effective_start_date => c_posassign_emp_rec.effective_start_date,
3819 	    p_effective_end_date => c_posassign_emp_rec.effective_end_date,
3820 	    p_element_value_type => null,
3821 	    p_element_value => null,
3822 	    p_currency_code => null,
3823 	    p_pay_basis => null,
3824 	    p_employee_id => l_employee_id,
3825 	    p_primary_employee_flag => c_posassign_emp_rec.primary_employee_flag,
3826 	    p_global_default_flag => null,
3827 	    p_assignment_default_rule_id => null,
3828 	    p_modify_flag => null,
3829 	    p_rowid => l_rowid);
3830 
3831 	if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3832 	  raise FND_API.G_EXC_ERROR;
3833 	end if;
3834 
3835       end loop;
3836 
3837       -- Upload Position Salary Distributions
3838 
3839       for c_position_distr_rec in c_position_distr (c_positions_rec.position_id) loop
3840         /* Bug 3867577 Start */
3841         l_pos_distr_cnt := l_pos_distr_cnt + 1;
3842         /* Bug 3867577 End */
3843 
3844 	-- if flex mapping was used in worksheet creation need to map the salary account distr
3845 	-- by effective dates
3846 
3847 	if p_flex_mapping_set_id is not null then
3848 	begin
3849 
3850 	  for l_year_index in 1..PSB_WS_ACCT1.g_num_budget_years loop
3851 
3852 	    if PSB_WS_ACCT1.g_budget_years(l_year_index).year_type in ('CY', 'PP') then
3853 	    begin
3854 
3855 	      l_budget_year_type_id := PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_type_id;
3856 	      l_year_start_date := PSB_WS_ACCT1.g_budget_years(l_year_index).start_date;
3857 	      l_year_end_date := PSB_WS_ACCT1.g_budget_years(l_year_index).end_date;
3858 
3859 	      if (((c_position_distr_rec.effective_start_date < l_year_end_date) and
3860 		   (c_position_distr_rec.effective_end_date is null)) or
3861 		  ((c_position_distr_rec.effective_start_date between l_year_start_date and l_year_end_date) or
3862 		   (c_position_distr_rec.effective_end_date between l_year_start_date and l_year_end_date) or
3863 		  ((c_position_distr_rec.effective_start_date < l_year_start_date) and
3864 		   (c_position_distr_rec.effective_end_date > l_year_end_date)))) then
3865 	      begin
3866 
3867 		l_mapped_ccid := PSB_FLEX_MAPPING_PVT.Get_Mapped_CCID
3868 				    (p_api_version => 1.0,
3869 				     p_ccid => c_position_distr_rec.code_combination_id,
3870 				     p_budget_year_type_id => l_budget_year_type_id,
3871 				     p_flexfield_mapping_set_id => p_flex_mapping_set_id,
3872 				     p_mapping_mode => 'GL_POSTING');
3873 
3874 		if l_mapped_ccid = 0 then
3875 		  raise FND_API.G_EXC_ERROR;
3876 		end if;
3877 
3878 		PSB_POSITION_PAY_DISTR_PVT.Modify_Distribution
3879 		   (p_api_version => 1.0,
3880 		    p_return_status => l_return_status,
3881 		    p_msg_count => l_msg_count,
3882 		    p_msg_data => l_msg_data,
3883 		    p_distribution_id => l_distribution_id,
3884 		    p_position_id => l_position_id,
3885 		    p_data_extract_id => p_target_data_extract_id,
3886 		    p_worksheet_id => null,
3887 		    p_effective_start_date => l_year_start_date,
3888 		    p_effective_end_date => l_year_end_date,
3889 		    p_chart_of_accounts_id => c_position_distr_rec.chart_of_accounts_id,
3890 		    p_code_combination_id => l_mapped_ccid,
3891 		    p_distribution_percent => c_position_distr_rec.distribution_percent,
3892 		    p_global_default_flag => null,
3893 		    p_distribution_default_rule_id => null,
3894 		    p_rowid => l_rowid,
3895 		    p_project_id => null,
3896 		    p_task_id => null,
3897 		    p_award_id => null,
3898 		    p_expenditure_type => null,
3899 		    p_expenditure_organization_id => null,
3900 		    p_description => null);
3901 
3902 		if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3903 		  raise FND_API.G_EXC_ERROR;
3904 		end if;
3905 
3906 	      end;
3907 	      end if;
3908 
3909 	    end;
3910 	    end if;
3911 
3912 	  end loop;
3913 
3914 	end;
3915 	else
3916 	begin
3917 
3918 	  /* Bug 3867577 Start */
3919       -- Call the following API only once for each position.
3920       IF l_pos_distr_cnt = 1 THEN
3921         Upload_Salary_Distribution
3922           (
3923             x_return_status          => l_return_status,
3924             p_worksheet_id           => p_worksheet_id,
3925             p_source_data_extract_id => p_source_data_extract_id,
3926             p_target_data_extract_id => p_target_data_extract_id,
3927             p_position_id            => l_position_id,
3928             p_source_DE_position_id  => c_positions_rec.position_id,
3929             p_from_budget_year_id    => p_from_budget_year_id,
3930             p_to_budget_year_id      => p_to_budget_year_id,
3931             p_position_exists        => TRUE
3932           );
3933 
3934         IF  l_return_status <> FND_API.G_RET_STS_SUCCESS then
3935 	      RAISE FND_API.G_EXC_ERROR;
3936         END IF;
3937       END IF;
3938 
3939       -- Commenting out the existing bunch of code as no enddating
3940       -- scenario was handelled before. This issue will be handelled
3941       -- new API Upload_Salary_Distribution.
3942 
3943       /*PSB_POSITION_PAY_DISTR_PVT.Modify_Distribution
3944 	     (p_api_version => 1.0,
3945 	      p_return_status => l_return_status,
3946 	      p_msg_count => l_msg_count,
3947 	      p_msg_data => l_msg_data,
3948 	      p_distribution_id => l_distribution_id,
3949 	      p_position_id => l_position_id,
3950 	      p_data_extract_id => p_target_data_extract_id,
3951 	      p_worksheet_id => null,
3952 	      p_effective_start_date => c_position_distr_rec.effective_start_date,
3953 	      p_effective_end_date => c_position_distr_rec.effective_end_date,
3954 	      p_chart_of_accounts_id => c_position_distr_rec.chart_of_accounts_id,
3955 	      p_code_combination_id => c_position_distr_rec.code_combination_id,
3956 	      p_distribution_percent => c_position_distr_rec.distribution_percent,
3957 	      p_global_default_flag => null,
3958 	      p_distribution_default_rule_id => null,
3959 	      p_rowid => l_rowid,
3960 	      p_project_id => null,
3961 	      p_task_id => null,
3962 	      p_award_id => null,
3963 	      p_expenditure_type => null,
3964 	      p_expenditure_organization_id => null,
3965 	      p_description => null);
3966 
3967 	  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3968 	    raise FND_API.G_EXC_ERROR;
3969 	  end if; */
3970       /* Bug 3867577 End */
3971 
3972 	end;
3973 	end if;
3974 
3975       end loop;
3976 
3977     end;
3978     end if;
3979 
3980   end loop;
3981 
3982   -- Initialize API return status to success
3983   p_return_status := FND_API.G_RET_STS_SUCCESS;
3984 
3985 EXCEPTION
3986 
3987    when FND_API.G_EXC_ERROR then
3988      if c_posassign_attr%ISOPEN then
3989        close c_posassign_attr;
3990      end if;
3991      p_return_status := FND_API.G_RET_STS_ERROR;
3992 
3993    when FND_API.G_EXC_UNEXPECTED_ERROR then
3994      if c_posassign_attr%ISOPEN then
3995        close c_posassign_attr;
3996      end if;
3997      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3998 
3999    when OTHERS then
4000      if c_posassign_attr%ISOPEN then
4001        close c_posassign_attr;
4002      end if;
4003 	p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4004 
4005 END Upload_Positions;
4006 
4007 /* ----------------------------------------------------------------------- */
4008 
4009 PROCEDURE Upload_Position_Worksheet
4010 ( p_return_status           OUT  NOCOPY  VARCHAR2,
4011   p_worksheet_id            IN   NUMBER,
4012   p_flex_mapping_set_id     IN   NUMBER,
4013   p_budget_calendar_id      IN   NUMBER,
4014   p_target_data_extract_id  IN   NUMBER,
4015   p_hr_budget_id            IN   NUMBER
4016 ) IS
4017 
4018   l_position_id             NUMBER;
4019   l_budget_group_id         NUMBER;
4020   l_budget_year_id          NUMBER;
4021   l_budget_year_type_id     NUMBER;
4022   l_element_set_id          NUMBER;
4023   l_currency_code           VARCHAR2(15);
4024   l_pay_element_id          NUMBER;
4025 
4026   l_year_start_date         DATE;
4027   l_year_end_date           DATE;
4028   l_period1_start_date      DATE;
4029   l_period1_end_date        DATE;
4030   l_period2_start_date      DATE;
4031   l_period2_end_date        DATE;
4032   l_period3_start_date      DATE;
4033   l_period3_end_date        DATE;
4034   l_period4_start_date      DATE;
4035   l_period4_end_date        DATE;
4036   l_period5_start_date      DATE;
4037   l_period5_end_date        DATE;
4038   l_period6_start_date      DATE;
4039   l_period6_end_date        DATE;
4040   l_period7_start_date      DATE;
4041   l_period7_end_date        DATE;
4042   l_period8_start_date      DATE;
4043   l_period8_end_date        DATE;
4044   l_period9_start_date      DATE;
4045   l_period9_end_date        DATE;
4046   l_period10_start_date     DATE;
4047   l_period10_end_date       DATE;
4048   l_period11_start_date     DATE;
4049   l_period11_end_date       DATE;
4050   l_period12_start_date     DATE;
4051   l_period12_end_date       DATE;
4052 
4053   l_period1_amount          NUMBER;
4054   l_period2_amount          NUMBER;
4055   l_period3_amount          NUMBER;
4056   l_period4_amount          NUMBER;
4057   l_period5_amount          NUMBER;
4058   l_period6_amount          NUMBER;
4059   l_period7_amount          NUMBER;
4060   l_period8_amount          NUMBER;
4061   l_period9_amount          NUMBER;
4062   l_period10_amount         NUMBER;
4063   l_period11_amount         NUMBER;
4064   l_period12_amount         NUMBER;
4065 
4066   l_mapped_ccid             NUMBER;
4067   l_period_amount           PSB_WS_ACCT1.g_prdamt_tbl_type;
4068 
4069   l_return_status           VARCHAR2(1);
4070   l_msg_count               NUMBER;
4071   l_msg_data                VARCHAR2(2000);
4072   /*For Bug No : 1808322 Start*/
4073   l_base_line_version       VARCHAR2(1);
4074   l_batch_size              NUMBER := g_limit_bulk_numrows;
4075   TYPE l_batch_pos_id_tbl         IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
4076   TYPE l_batch_start_date_tbl     IS TABLE OF DATE INDEX BY BINARY_INTEGER;
4077   TYPE l_batch_end_date_tbl       IS TABLE OF DATE INDEX BY BINARY_INTEGER;
4078   TYPE l_batch_cur_code_tbl       IS TABLE OF VARCHAR2(15) INDEX BY BINARY_INTEGER;
4079   TYPE l_batch_fte_tbl            IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
4080   TYPE l_batch_pay_element_id_tbl IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
4081   TYPE l_batch_element_cost_tbl   IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
4082   TYPE l_batch_cc_id_tbl          IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
4083   TYPE l_batch_amount_tbl         IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
4084   TYPE l_batch_bg_id_tbl          IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
4085   TYPE l_update_tbl               IS TABLE OF VARCHAR2(1) INDEX BY BINARY_INTEGER;
4086 
4087   l_fte_position_id       l_batch_pos_id_tbl;
4088   l_fte_start_date        l_batch_start_date_tbl;
4089   l_fte_end_date          l_batch_end_date_tbl;
4090   l_fte_fte               l_batch_fte_tbl;
4091   l_fte_update            l_update_tbl;
4092 
4093   l_cost_position_id      l_batch_pos_id_tbl;
4094   l_cost_start_date       l_batch_start_date_tbl;
4095   l_cost_end_date         l_batch_end_date_tbl;
4096   l_cost_element_id       l_batch_pay_element_id_tbl;
4097   l_cost_currency_code    l_batch_cur_code_tbl;
4098   l_cost_element_cost     l_batch_element_cost_tbl;
4099   l_cost_update           l_update_tbl;
4100 
4101   l_account_position_id   l_batch_pos_id_tbl;
4102   l_account_start_date    l_batch_start_date_tbl;
4103   l_account_end_date      l_batch_end_date_tbl;
4104   l_account_cc_id         l_batch_cc_id_tbl;
4105   l_account_currency_code l_batch_cur_code_tbl;
4106   l_account_amount        l_batch_amount_tbl;
4107   l_account_bg_id         l_batch_bg_id_tbl;
4108   l_account_update        l_update_tbl;
4109 
4110   l_count_fte             NUMBER := 0;
4111   l_count_costs           NUMBER := 0;
4112   l_count_accounts        NUMBER := 0;
4113   /*For Bug No : 1808322 End*/
4114 
4115   /*For Bug No : 2434152 Start*/
4116   l_position_ctr          NUMBER := 0;
4117   /*For Bug No : 2434152 End*/
4118 
4119   /* start bug 4545590 */
4120   TYPE l_num_tbl_type IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
4121 
4122   TYPE l_position_rec_type IS RECORD (
4123    position_id      l_num_tbl_type,
4124    position_line_id l_num_tbl_type);
4125 
4126   -- position record variable
4127   l_position_rec l_position_rec_type;
4128   /* end bug 4545590 */
4129 
4130 
4131   cursor c_wpl is
4132     /*For Bug No : 1808322 Start*/
4133       --The following sql statement has been commented because of
4134       --FTS on PSB_WS_POSITION_LINES table.
4135       --The same was achived by the next query
4136 
4137     /*select *
4138       from PSB_WS_POSITION_LINES a
4139      where exists
4140 	  (select 1
4141 	     from PSB_WS_LINES_POSITIONS b
4142 	    where b.position_line_id = a.position_line_id
4143 	      and b.worksheet_id = p_worksheet_id);*/
4144 
4145      -- for bug 4545590
4146      select a.position_id, a.position_line_id
4147        from PSB_WS_POSITION_LINES a,
4148 	    PSB_WS_LINES_POSITIONS b
4149       where a.position_line_id = b.position_line_id
4150 	and b.worksheet_id = p_worksheet_id;
4151     /*For Bug No : 1808322 End*/
4152 
4153   cursor c_wal_ps(poslineid NUMBER, budgetyearid NUMBER) is
4154     select code_combination_id, currency_code, sum(nvl(ytd_amount, 0)) annual_amount
4155       from PSB_WS_ACCOUNT_LINES
4156      where position_line_id = poslineid
4157        and budget_year_id = budgetyearid
4158        and end_stage_seq is null
4159      group by code_combination_id, currency_code;
4160 
4161   cursor c_wal_period(poslineid NUMBER, budgetyearid NUMBER) is
4162     select currency_code, element_set_id,
4163 	   sum(nvl(ytd_amount,0)) annual_amount,
4164 	   sum(nvl(period1_amount,0)) period1_amount,
4165 	   sum(nvl(period2_amount,0)) period2_amount,
4166 	   sum(nvl(period3_amount,0)) period3_amount,
4167 	   sum(nvl(period4_amount,0)) period4_amount,
4168 	   sum(nvl(period5_amount,0)) period5_amount,
4169 	   sum(nvl(period6_amount,0)) period6_amount,
4170 	   sum(nvl(period7_amount,0)) period7_amount,
4171 	   sum(nvl(period8_amount,0)) period8_amount,
4172 	   sum(nvl(period9_amount,0)) period9_amount,
4173 	   sum(nvl(period10_amount,0)) period10_amount,
4174 	   sum(nvl(period11_amount,0)) period11_amount,
4175 	   sum(nvl(period12_amount,0)) period12_amount
4176       from psb_ws_account_lines
4177      where position_line_id = poslineid
4178        and budget_year_id   = budgetyearid
4179        and end_stage_seq is null
4180     group by currency_code, element_set_id;
4181 
4182   cursor c_wfl(poslineid NUMBER, budgetyearid NUMBER) is
4183     select sum(nvl(period1_fte, 0)) period1_fte, sum(nvl(period2_fte, 0)) period2_fte,
4184 	   sum(nvl(period3_fte, 0)) period3_fte, sum(nvl(period4_fte, 0)) period4_fte,
4185 	   sum(nvl(period5_fte, 0)) period5_fte, sum(nvl(period6_fte, 0)) period6_fte,
4186 	   sum(nvl(period7_fte, 0)) period7_fte, sum(nvl(period8_fte, 0)) period8_fte,
4187 	   sum(nvl(period9_fte, 0)) period9_fte, sum(nvl(period10_fte, 0)) period10_fte,
4188 	   sum(nvl(period11_fte, 0)) period11_fte, sum(nvl(period12_fte, 0)) period12_fte
4189       from PSB_WS_FTE_LINES
4190      where position_line_id = poslineid
4191        and budget_year_id = budgetyearid
4192        and end_stage_seq is null;
4193 
4194   cursor c_wel(poslineid NUMBER, budgetyearid NUMBER, elemsetid NUMBER, currency VARCHAR2) is
4195     select pay_element_id, sum(nvl(element_cost, 0)) element_cost
4196       from PSB_WS_ELEMENT_LINES
4197      where position_line_id = poslineid
4198        and budget_year_id = budgetyearid
4199        and element_set_id = elemsetid
4200        and currency_code = currency
4201        and end_stage_seq is null
4202      group by pay_element_id;
4203 
4204   cursor c_re_elem (elemid NUMBER) is
4205     select a.pay_element_id
4206       from PSB_PAY_ELEMENTS a,
4207 	   PSB_PAY_ELEMENTS b
4208      where a.name = b.name
4209        and a.data_extract_id = p_target_data_extract_id
4210        and b.pay_element_id = elemid;
4211 
4212   cursor c_re_pos (posid NUMBER) is
4213     select a.position_id, a.budget_group_id
4214       from PSB_POSITIONS a,
4215 	   PSB_POSITIONS b
4216      where (((b.hr_employee_id is null) and
4217             (a.hr_employee_id is null))
4218              or ((b.hr_employee_id is not null )
4219                  and (a.hr_employee_id = b.hr_employee_id)))
4220        /* Start bug 3625364 */
4221        /*and a.name = b.name*/
4222        and a.hr_position_id = b.hr_position_id
4223        /* End bug 3625364 */
4224        and a.data_extract_id = p_target_data_extract_id
4225        and b.position_id = posid;
4226 
4227 BEGIN
4228 
4229   /*For Bug No : 2434152 Start*/
4230   -- Standard Start of API savepoint
4231   Savepoint Upload_Position_Worksheet_Pvt;
4232   /*For Bug No : 2434152 End*/
4233 
4234   /*For Bug No : 1808322 Start*/
4235   FOR i IN 1..l_count_fte LOOP
4236     l_fte_position_id(i) := null;
4237     l_fte_start_date(i) := null;
4238     l_fte_end_date(i) := null;
4239     l_fte_fte(i) := null;
4240   END LOOP;
4241 
4242   FOR i IN 1..l_count_costs LOOP
4243     l_cost_position_id(i) := null;
4244     l_cost_start_date(i) := null;
4245     l_cost_end_date(i) := null;
4246     l_cost_element_id(i) := null;
4247     l_cost_currency_code(i) := null;
4248     l_cost_element_cost(i) := null;
4249   END LOOP;
4250 
4251   FOR i IN 1..l_count_accounts LOOP
4252     l_account_position_id(i) := null;
4253     l_account_start_date(i) := null;
4254     l_account_end_date(i) := null;
4255     l_account_cc_id(i) := null;
4256     l_account_currency_code(i) := null;
4257     l_account_amount(i) := null;
4258     l_account_bg_id(i) := null;
4259   END LOOP;
4260   /*For Bug No : 1808322 End*/
4261 
4262   /* start bug 4545590 */
4263   l_position_rec.position_id.delete;
4264   l_position_rec.position_line_id.delete;
4265 
4266   OPEN c_wpl;
4267   FETCH c_wpl BULK COLLECT INTO l_position_rec.position_id, l_position_rec.position_line_id;
4268   CLOSE c_wpl;
4269   /* end bug 4545590 */
4270 
4271   -- for bug 4545590
4272   --for c_wpl_rec in c_wpl loop
4273   for loop_var IN 1..l_position_rec.position_id.count LOOP
4274 
4275     /*For Bug No : 2434152 Start*/
4276     l_position_ctr := l_position_ctr + 1;
4277     /*For Bug No : 2434152 End*/
4278 
4279     -- for bug 4545590
4280     --for c_re_pos_rec in c_re_pos (c_wpl_rec.position_id) loop
4281     for c_re_pos_rec in c_re_pos (l_position_rec.position_id(loop_var)) loop
4282       l_position_id := c_re_pos_rec.position_id;
4283       l_budget_group_id := c_re_pos_rec.budget_group_id;
4284     end loop;
4285 
4286     for l_year_index in 1..PSB_WS_ACCT1.g_num_budget_years loop
4287 
4288       l_budget_year_id := PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id;
4289       l_budget_year_type_id := PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_type_id;
4290       l_year_start_date := PSB_WS_ACCT1.g_budget_years(l_year_index).start_date;
4291       l_year_end_date := PSB_WS_ACCT1.g_budget_years(l_year_index).end_date;
4292 
4293       for l_period_index in 1..PSB_WS_ACCT1.g_num_budget_periods loop
4294 
4295 	if PSB_WS_ACCT1.g_budget_periods(l_period_index).budget_year_id = l_budget_year_id then
4296 	begin
4297 
4298 	  if PSB_WS_ACCT1.g_budget_periods(l_period_index).long_sequence_no = 1 then
4299 	    l_period1_start_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).start_date;
4300 	    l_period1_end_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).end_date;
4301 	  elsif PSB_WS_ACCT1.g_budget_periods(l_period_index).long_sequence_no = 2 then
4302 	    l_period2_start_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).start_date;
4303 	    l_period2_end_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).end_date;
4304 	  elsif PSB_WS_ACCT1.g_budget_periods(l_period_index).long_sequence_no = 3 then
4305 	    l_period3_start_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).start_date;
4306 	    l_period3_end_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).end_date;
4307 	  elsif PSB_WS_ACCT1.g_budget_periods(l_period_index).long_sequence_no = 4 then
4308 	    l_period4_start_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).start_date;
4309 	    l_period4_end_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).end_date;
4310 	  elsif PSB_WS_ACCT1.g_budget_periods(l_period_index).long_sequence_no = 5 then
4311 	    l_period5_start_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).start_date;
4312 	    l_period5_end_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).end_date;
4313 	  elsif PSB_WS_ACCT1.g_budget_periods(l_period_index).long_sequence_no = 6 then
4314 	    l_period6_start_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).start_date;
4315 	    l_period6_end_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).end_date;
4316 	  elsif PSB_WS_ACCT1.g_budget_periods(l_period_index).long_sequence_no = 7 then
4317 	    l_period7_start_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).start_date;
4318 	    l_period7_end_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).end_date;
4319 	  elsif PSB_WS_ACCT1.g_budget_periods(l_period_index).long_sequence_no = 8 then
4320 	    l_period8_start_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).start_date;
4321 	    l_period8_end_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).end_date;
4322 	  elsif PSB_WS_ACCT1.g_budget_periods(l_period_index).long_sequence_no = 9 then
4323 	    l_period9_start_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).start_date;
4324 	    l_period9_end_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).end_date;
4325 	  elsif PSB_WS_ACCT1.g_budget_periods(l_period_index).long_sequence_no = 10 then
4326 	    l_period10_start_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).start_date;
4327 	    l_period10_end_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).end_date;
4328 	  elsif PSB_WS_ACCT1.g_budget_periods(l_period_index).long_sequence_no = 11 then
4329 	    l_period11_start_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).start_date;
4330 	    l_period11_end_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).end_date;
4331 	  elsif PSB_WS_ACCT1.g_budget_periods(l_period_index).long_sequence_no = 12 then
4332 	    l_period12_start_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).start_date;
4333 	    l_period12_end_date := PSB_WS_ACCT1.g_budget_periods(l_period_index).end_date;
4334 	  end if;
4335 
4336 	end;
4337 	end if;
4338 
4339       end loop;
4340 
4341       -- for bug 4545590
4342       --for c_wfl_rec in c_wfl(c_wpl_rec.position_line_id, l_budget_year_id) loop
4343       for c_wfl_rec in c_wfl(l_position_rec.position_line_id(loop_var), l_budget_year_id) loop
4344        /*For Bug No : 1808322 Start*/
4345        if c_wfl_rec.period1_fte <> 0 then
4346        begin
4347 
4348 	l_count_fte := l_count_fte + 1;
4349 	l_fte_position_id(l_count_fte) := l_position_id;
4350 	l_fte_start_date(l_count_fte) := l_period1_start_date;
4351 	l_fte_end_date(l_count_fte) := l_period1_end_date;
4352 	l_fte_fte(l_count_fte) := c_wfl_rec.period1_fte;
4353 
4354        end;
4355        end if;
4356 
4357        if c_wfl_rec.period2_fte <> 0 then
4358        begin
4359 
4360 	l_count_fte := l_count_fte + 1;
4361 	l_fte_position_id(l_count_fte) := l_position_id;
4362 	l_fte_start_date(l_count_fte) := l_period2_start_date;
4363 	l_fte_end_date(l_count_fte) := l_period2_end_date;
4364 	l_fte_fte(l_count_fte) := c_wfl_rec.period2_fte;
4365 
4366        end;
4367        end if;
4368 
4369        if c_wfl_rec.period3_fte <> 0 then
4370        begin
4371 
4372 	l_count_fte := l_count_fte + 1;
4373 	l_fte_position_id(l_count_fte) := l_position_id;
4374 	l_fte_start_date(l_count_fte) := l_period3_start_date;
4375 	l_fte_end_date(l_count_fte) := l_period3_end_date;
4376 	l_fte_fte(l_count_fte) := c_wfl_rec.period3_fte;
4377 
4378        end;
4379        end if;
4380 
4381        if c_wfl_rec.period4_fte <> 0 then
4382        begin
4383 
4384 	l_count_fte := l_count_fte + 1;
4385 	l_fte_position_id(l_count_fte) := l_position_id;
4386 	l_fte_start_date(l_count_fte) := l_period4_start_date;
4387 	l_fte_end_date(l_count_fte) := l_period4_end_date;
4388 	l_fte_fte(l_count_fte) := c_wfl_rec.period4_fte;
4389 
4390        end;
4391        end if;
4392 
4393        if c_wfl_rec.period5_fte <> 0 then
4394        begin
4395 
4396 	l_count_fte := l_count_fte + 1;
4397 	l_fte_position_id(l_count_fte) := l_position_id;
4398 	l_fte_start_date(l_count_fte) := l_period5_start_date;
4399 	l_fte_end_date(l_count_fte) := l_period5_end_date;
4400 	l_fte_fte(l_count_fte) := c_wfl_rec.period5_fte;
4401 
4402        end;
4403        end if;
4404 
4405        if c_wfl_rec.period6_fte <> 0 then
4406        begin
4407 
4408 	l_count_fte := l_count_fte + 1;
4409 	l_fte_position_id(l_count_fte) := l_position_id;
4410 	l_fte_start_date(l_count_fte) := l_period6_start_date;
4411 	l_fte_end_date(l_count_fte) := l_period6_end_date;
4412 	l_fte_fte(l_count_fte) := c_wfl_rec.period6_fte;
4413 
4414        end;
4415        end if;
4416 
4417        if c_wfl_rec.period7_fte <> 0 then
4418        begin
4419 
4420 	l_count_fte := l_count_fte + 1;
4421 	l_fte_position_id(l_count_fte) := l_position_id;
4422 	l_fte_start_date(l_count_fte) := l_period7_start_date;
4423 	l_fte_end_date(l_count_fte) := l_period7_end_date;
4424 	l_fte_fte(l_count_fte) := c_wfl_rec.period7_fte;
4425 
4426        end;
4427        end if;
4428 
4429        if c_wfl_rec.period8_fte <> 0 then
4430        begin
4431 
4432 	l_count_fte := l_count_fte + 1;
4433 	l_fte_position_id(l_count_fte) := l_position_id;
4434 	l_fte_start_date(l_count_fte) := l_period8_start_date;
4435 	l_fte_end_date(l_count_fte) := l_period8_end_date;
4436 	l_fte_fte(l_count_fte) := c_wfl_rec.period8_fte;
4437 
4438        end;
4439        end if;
4440 
4441        if c_wfl_rec.period9_fte <> 0 then
4442        begin
4443 
4444 	l_count_fte := l_count_fte + 1;
4445 	l_fte_position_id(l_count_fte) := l_position_id;
4446 	l_fte_start_date(l_count_fte) := l_period9_start_date;
4447 	l_fte_end_date(l_count_fte) := l_period9_end_date;
4448 	l_fte_fte(l_count_fte) := c_wfl_rec.period9_fte;
4449 
4450        end;
4451        end if;
4452 
4453        if c_wfl_rec.period10_fte <> 0 then
4454        begin
4455 
4456 	l_count_fte := l_count_fte + 1;
4457 	l_fte_position_id(l_count_fte) := l_position_id;
4458 	l_fte_start_date(l_count_fte) := l_period10_start_date;
4459 	l_fte_end_date(l_count_fte) := l_period10_end_date;
4460 	l_fte_fte(l_count_fte) := c_wfl_rec.period10_fte;
4461 
4462        end;
4463        end if;
4464 
4465        if c_wfl_rec.period11_fte <> 0 then
4466        begin
4467 
4468 	l_count_fte := l_count_fte + 1;
4469 	l_fte_position_id(l_count_fte) := l_position_id;
4470 	l_fte_start_date(l_count_fte) := l_period11_start_date;
4471 	l_fte_end_date(l_count_fte) := l_period11_end_date;
4472 	l_fte_fte(l_count_fte) := c_wfl_rec.period11_fte;
4473 
4474        end;
4475        end if;
4476 
4477        if c_wfl_rec.period12_fte <> 0 then
4478        begin
4479 
4480 	l_count_fte := l_count_fte + 1;
4481 	l_fte_position_id(l_count_fte) := l_position_id;
4482 	l_fte_start_date(l_count_fte) := l_period12_start_date;
4483 	l_fte_end_date(l_count_fte) := l_period12_end_date;
4484 	l_fte_fte(l_count_fte) := c_wfl_rec.period12_fte;
4485 
4486        end;
4487        end if;
4488       /*For Bug No : 1808322 End*/
4489       end loop;
4490 
4491       -- for bug 4545590
4492       --for c_wal_period_rec in c_wal_period(c_wpl_rec.position_line_id, l_budget_year_id) loop
4493       for c_wal_period_rec in c_wal_period(l_position_rec.position_line_id(loop_var), l_budget_year_id) loop
4494 	l_element_set_id := c_wal_period_rec.element_set_id;
4495 	l_currency_code := c_wal_period_rec.currency_code;
4496 
4497 	if c_wal_period_rec.annual_amount <> 0 then
4498 	begin
4499 	/*For Bug No : 1808322 Start*/
4500 
4501           -- for bug 4545590
4502 	  --for c_wel_rec in c_wel(c_wpl_rec.position_line_id, l_budget_year_id, l_element_set_id, l_currency_code) loop
4503             for c_wel_rec in c_wel(l_position_rec.position_line_id(loop_var), l_budget_year_id, l_element_set_id, l_currency_code) loop
4504 
4505 	      for c_re_elem_rec in c_re_elem(c_wel_rec.pay_element_id) loop
4506 	        l_pay_element_id := c_re_elem_rec.pay_element_id;
4507 	      end loop;
4508 
4509 	    if c_wal_period_rec.period1_amount <> 0 then
4510 	    begin
4511 
4512 	      l_period1_amount := ((c_wel_rec.element_cost/c_wal_period_rec.annual_amount) * c_wal_period_rec.period1_amount);
4513 
4514 	      l_count_costs := l_count_costs + 1;
4515 	      l_cost_position_id(l_count_costs) := l_position_id;
4516 	      l_cost_element_id(l_count_costs) := l_pay_element_id;
4517 	      l_cost_start_date(l_count_costs) := l_period1_start_date;
4518 	      l_cost_end_date(l_count_costs) := l_period1_end_date;
4519 	      l_cost_currency_code(l_count_costs) := c_wal_period_rec.currency_code;
4520 	      l_cost_element_cost(l_count_costs) := l_period1_amount;
4521 
4522 	    end;
4523 	    end if;
4524 
4525 	    if c_wal_period_rec.period2_amount <> 0 then
4526 	    begin
4527 
4528 	      l_period2_amount := ((c_wel_rec.element_cost/c_wal_period_rec.annual_amount) * c_wal_period_rec.period2_amount);
4529 
4530 	      l_count_costs := l_count_costs + 1;
4531 	      l_cost_position_id(l_count_costs) := l_position_id;
4532 	      l_cost_element_id(l_count_costs) := l_pay_element_id;
4533 	      l_cost_start_date(l_count_costs) := l_period2_start_date;
4534 	      l_cost_end_date(l_count_costs) := l_period2_end_date;
4535 	      l_cost_currency_code(l_count_costs) := c_wal_period_rec.currency_code;
4536 	      l_cost_element_cost(l_count_costs) := l_period2_amount;
4537 
4538 	    end;
4539 	    end if;
4540 
4541 	    if c_wal_period_rec.period3_amount <> 0 then
4542 	    begin
4543 
4544 	      l_period3_amount := ((c_wel_rec.element_cost/c_wal_period_rec.annual_amount) * c_wal_period_rec.period3_amount);
4545 
4546 	      l_count_costs := l_count_costs + 1;
4547 	      l_cost_position_id(l_count_costs) := l_position_id;
4548 	      l_cost_element_id(l_count_costs) := l_pay_element_id;
4549 	      l_cost_start_date(l_count_costs) := l_period3_start_date;
4550 	      l_cost_end_date(l_count_costs) := l_period3_end_date;
4551 	      l_cost_currency_code(l_count_costs) := c_wal_period_rec.currency_code;
4552 	      l_cost_element_cost(l_count_costs) := l_period3_amount;
4553 
4554 	    end;
4555 	    end if;
4556 
4557 	    if c_wal_period_rec.period4_amount <> 0 then
4558 	    begin
4559 
4560 	      l_period4_amount := ((c_wel_rec.element_cost/c_wal_period_rec.annual_amount) * c_wal_period_rec.period4_amount);
4561 
4562 	      l_count_costs := l_count_costs + 1;
4563 	      l_cost_position_id(l_count_costs) := l_position_id;
4564 	      l_cost_element_id(l_count_costs) := l_pay_element_id;
4565 	      l_cost_start_date(l_count_costs) := l_period4_start_date;
4566 	      l_cost_end_date(l_count_costs) := l_period4_end_date;
4567 	      l_cost_currency_code(l_count_costs) := c_wal_period_rec.currency_code;
4568 	      l_cost_element_cost(l_count_costs) := l_period4_amount;
4569 
4570 	    end;
4571 	    end if;
4572 
4573 	    if c_wal_period_rec.period5_amount <> 0 then
4574 	    begin
4575 
4576 	      l_period5_amount := ((c_wel_rec.element_cost/c_wal_period_rec.annual_amount) * c_wal_period_rec.period5_amount);
4577 
4578 	      l_count_costs := l_count_costs + 1;
4579 	      l_cost_position_id(l_count_costs) := l_position_id;
4580 	      l_cost_element_id(l_count_costs) := l_pay_element_id;
4581 	      l_cost_start_date(l_count_costs) := l_period5_start_date;
4582 	      l_cost_end_date(l_count_costs) := l_period5_end_date;
4583 	      l_cost_currency_code(l_count_costs) := c_wal_period_rec.currency_code;
4584 	      l_cost_element_cost(l_count_costs) := l_period5_amount;
4585 
4586 	    end;
4587 	    end if;
4588 
4589 	    if c_wal_period_rec.period6_amount <> 0 then
4590 	    begin
4591 
4592 	      l_period6_amount := ((c_wel_rec.element_cost/c_wal_period_rec.annual_amount) * c_wal_period_rec.period6_amount);
4593 
4594 	      l_count_costs := l_count_costs + 1;
4595 	      l_cost_position_id(l_count_costs) := l_position_id;
4596 	      l_cost_element_id(l_count_costs) := l_pay_element_id;
4597 	      l_cost_start_date(l_count_costs) := l_period6_start_date;
4598 	      l_cost_end_date(l_count_costs) := l_period6_end_date;
4599 	      l_cost_currency_code(l_count_costs) := c_wal_period_rec.currency_code;
4600 	      l_cost_element_cost(l_count_costs) := l_period6_amount;
4601 
4602 	    end;
4603 	    end if;
4604 
4605 	    if c_wal_period_rec.period7_amount <> 0 then
4606 	    begin
4607 
4608 	      l_period7_amount := ((c_wel_rec.element_cost/c_wal_period_rec.annual_amount) * c_wal_period_rec.period7_amount);
4609 
4610 	      l_count_costs := l_count_costs + 1;
4611 	      l_cost_position_id(l_count_costs) := l_position_id;
4612 	      l_cost_element_id(l_count_costs) := l_pay_element_id;
4613 	      l_cost_start_date(l_count_costs) := l_period7_start_date;
4614 	      l_cost_end_date(l_count_costs) := l_period7_end_date;
4615 	      l_cost_currency_code(l_count_costs) := c_wal_period_rec.currency_code;
4616 	      l_cost_element_cost(l_count_costs) := l_period7_amount;
4617 
4618 	    end;
4619 	    end if;
4620 
4621 	    if c_wal_period_rec.period8_amount <> 0 then
4622 	    begin
4623 
4624 	      l_period8_amount := ((c_wel_rec.element_cost/c_wal_period_rec.annual_amount) * c_wal_period_rec.period8_amount);
4625 
4626 	      l_count_costs := l_count_costs + 1;
4627 	      l_cost_position_id(l_count_costs) := l_position_id;
4628 	      l_cost_element_id(l_count_costs) := l_pay_element_id;
4629 	      l_cost_start_date(l_count_costs) := l_period8_start_date;
4630 	      l_cost_end_date(l_count_costs) := l_period8_end_date;
4631 	      l_cost_currency_code(l_count_costs) := c_wal_period_rec.currency_code;
4632 	      l_cost_element_cost(l_count_costs) := l_period8_amount;
4633 
4634 	    end;
4635 	    end if;
4636 
4637 	    if c_wal_period_rec.period9_amount <> 0 then
4638 	    begin
4639 
4640 	      l_period9_amount := ((c_wel_rec.element_cost/c_wal_period_rec.annual_amount) * c_wal_period_rec.period9_amount);
4641 
4642 	      l_count_costs := l_count_costs + 1;
4643 	      l_cost_position_id(l_count_costs) := l_position_id;
4644 	      l_cost_element_id(l_count_costs) := l_pay_element_id;
4645 	      l_cost_start_date(l_count_costs) := l_period9_start_date;
4646 	      l_cost_end_date(l_count_costs) := l_period9_end_date;
4647 	      l_cost_currency_code(l_count_costs) := c_wal_period_rec.currency_code;
4648 	      l_cost_element_cost(l_count_costs) := l_period9_amount;
4649 
4650 	    end;
4651 	    end if;
4652 
4653 	    if c_wal_period_rec.period10_amount <> 0 then
4654 	    begin
4655 
4656 	      l_period10_amount := ((c_wel_rec.element_cost/c_wal_period_rec.annual_amount) * c_wal_period_rec.period10_amount);
4657 
4658 	      l_count_costs := l_count_costs + 1;
4659 	      l_cost_position_id(l_count_costs) := l_position_id;
4660 	      l_cost_element_id(l_count_costs) := l_pay_element_id;
4661 	      l_cost_start_date(l_count_costs) := l_period10_start_date;
4662 	      l_cost_end_date(l_count_costs) := l_period10_end_date;
4663 	      l_cost_currency_code(l_count_costs) := c_wal_period_rec.currency_code;
4664 	      l_cost_element_cost(l_count_costs) := l_period10_amount;
4665 
4666 	    end;
4667 	    end if;
4668 
4669 	    if c_wal_period_rec.period11_amount <> 0 then
4670 	    begin
4671 
4672 	      l_period11_amount := ((c_wel_rec.element_cost/c_wal_period_rec.annual_amount) * c_wal_period_rec.period11_amount);
4673 
4674 	      l_count_costs := l_count_costs + 1;
4675 	      l_cost_position_id(l_count_costs) := l_position_id;
4676 	      l_cost_element_id(l_count_costs) := l_pay_element_id;
4677 	      l_cost_start_date(l_count_costs) := l_period11_start_date;
4678 	      l_cost_end_date(l_count_costs) := l_period11_end_date;
4679 	      l_cost_currency_code(l_count_costs) := c_wal_period_rec.currency_code;
4680 	      l_cost_element_cost(l_count_costs) := l_period11_amount;
4681 
4682 	    end;
4683 	    end if;
4684 
4685 	    if c_wal_period_rec.period12_amount <> 0 then
4686 	    begin
4687 
4688 	      l_period12_amount := ((c_wel_rec.element_cost/c_wal_period_rec.annual_amount) * c_wal_period_rec.period12_amount);
4689 
4690 	      l_count_costs := l_count_costs + 1;
4691 	      l_cost_position_id(l_count_costs) := l_position_id;
4692 	      l_cost_element_id(l_count_costs) := l_pay_element_id;
4693 	      l_cost_start_date(l_count_costs) := l_period12_start_date;
4694 	      l_cost_end_date(l_count_costs) := l_period12_end_date;
4695 	      l_cost_currency_code(l_count_costs) := c_wal_period_rec.currency_code;
4696 	      l_cost_element_cost(l_count_costs) := l_period12_amount;
4697 
4698 	    end;
4699 	    end if;
4700 
4701 	  end loop; /* c_wel */
4702 	/*For Bug No : 1808322 End*/
4703 
4704 	end;
4705 	end if; /* annual_amount <> 0 */
4706 
4707       end loop; /* c_wal_period */
4708 
4709       -- for bug 4545590
4710       --for c_wal_rec in c_wal_ps(c_wpl_rec.position_line_id, l_budget_year_id) loop
4711       for c_wal_rec in c_wal_ps(l_position_rec.position_line_id(loop_var), l_budget_year_id) loop
4712 
4713 	if p_flex_mapping_set_id is not null then
4714 	begin
4715 
4716 	  l_mapped_ccid := PSB_FLEX_MAPPING_PVT.Get_Mapped_CCID
4717 			      (p_api_version => 1.0,
4718 			       p_ccid => c_wal_rec.code_combination_id,
4719 			       p_budget_year_type_id => l_budget_year_type_id,
4720 			       p_flexfield_mapping_set_id => p_flex_mapping_set_id,
4721 			       p_mapping_mode => 'GL_POSTING');
4722 
4723 	  if l_mapped_ccid = 0 then
4724 	    raise FND_API.G_EXC_ERROR;
4725 	  end if;
4726 
4727 	end;
4728 	else
4729 	  l_mapped_ccid := c_wal_rec.code_combination_id;
4730 	end if;
4731 
4732 	/*For Bug No : 1808322 Start*/
4733 	l_count_accounts := l_count_accounts + 1;
4734 	l_account_position_id(l_count_accounts) := l_position_id;
4735 	l_account_bg_id(l_count_accounts) := l_budget_group_id;
4736 	l_account_start_date(l_count_accounts) := l_year_start_date;
4737 	l_account_end_date(l_count_accounts) := l_year_end_date;
4738 	l_account_cc_id(l_count_accounts) := l_mapped_ccid;
4739 	l_account_currency_code(l_count_accounts) := c_wal_rec.currency_code;
4740 	l_account_amount(l_count_accounts) := c_wal_rec.annual_amount;
4741 	/*For Bug No : 1808322 End*/
4742 
4743       end loop; /* c_wal_ps */
4744 
4745     end loop; /* budget years */
4746 
4747     /*For Bug No : 1808322 Start*/
4748     --i. process the FTE lines
4749     IF ( l_count_fte >= l_batch_size) THEN
4750       FOR l_base_line IN 1..2 LOOP
4751 
4752 	IF l_base_line = 1 THEN
4753 	  l_base_line_version := 'O';
4754 	ELSE
4755 	  l_base_line_version := 'C';
4756 	END IF;
4757 
4758         -- for bug 4545590
4759        IF NOT g_wks_new_hr_budget THEN
4760 
4761 	FORALL i IN 1..l_count_fte
4762 	UPDATE PSB_POSITION_FTE
4763 	   SET fte =l_fte_fte(i),
4764 	     last_update_date = sysdate,
4765 	       last_updated_by = FND_GLOBAL.USER_ID,
4766 	       last_update_login = FND_GLOBAL.LOGIN_ID
4767 	 WHERE position_id = l_fte_position_id(i)
4768 	   AND nvl(hr_budget_id,-1) = nvl(p_hr_budget_id,-1)
4769 	   AND budget_revision_id is null
4770 	   AND base_line_version = l_base_line_version
4771 	   AND start_date = l_fte_start_date(i)
4772 	   AND end_date = l_fte_end_date(i);
4773 
4774         END IF;
4775 
4776 	FOR i IN 1..l_count_fte LOOP
4777 	  -- for bug 4545590
4778 	  IF (NOT g_wks_new_hr_budget) AND (SQL%BULK_ROWCOUNT(i) <> 0 )THEN
4779 	    l_fte_update(i) := 'Y';
4780 	  ELSE
4781 	    l_fte_update(i) := 'N';
4782 	  END IF;
4783 	END LOOP;
4784 
4785 	FOR i IN 1..l_count_fte LOOP
4786 	  IF l_fte_update(i) = 'N' THEN
4787 
4788 	    Modify_Position_FTE
4789 	      (p_api_version => 1.0,
4790 	       p_return_status => l_return_status,
4791 	       p_msg_count => l_msg_count,
4792 	       p_msg_data => l_msg_data,
4793 	       p_position_id => l_fte_position_id(i),
4794 	       p_hr_budget_id => p_hr_budget_id,
4795 	       p_budget_revision_id => null,
4796 	       p_base_line_version => l_base_line_version,
4797 	       p_fte => l_fte_fte(i),
4798 	       p_start_date => l_fte_start_date(i),
4799 	       p_end_date => l_fte_end_date(i));
4800 
4801 	    IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4802 	      raise FND_API.G_EXC_ERROR;
4803 	    END IF;
4804 
4805 	  END IF;
4806 	END LOOP;
4807       END LOOP;
4808 
4809       l_count_fte := 0;
4810       l_fte_position_id.delete;
4811       l_fte_start_date.delete;
4812       l_fte_end_date.delete;
4813       l_fte_fte.delete;
4814       l_fte_update.delete;
4815      END IF;
4816 
4817     --ii. Process the costs lines
4818     IF ( l_count_costs >= l_batch_size) THEN
4819       FOR l_base_line IN 1..2 LOOP
4820 
4821 	IF l_base_line = 1 THEN
4822 	  l_base_line_version := 'O';
4823 	ELSE
4824 	  l_base_line_version := 'C';
4825 	END IF;
4826 
4827         -- 4545590
4828         IF NOT g_wks_new_hr_budget THEN
4829 
4830 	FORALL i IN 1..l_count_costs
4831 	  UPDATE PSB_POSITION_COSTS
4832 	     SET element_cost = l_cost_element_cost(i),
4833 		 last_update_date = sysdate,
4834 		 last_updated_by = FND_GLOBAL.USER_ID,
4835 		 last_update_login = FND_GLOBAL.LOGIN_ID
4836 	   WHERE position_id = l_cost_position_id(i)
4837 	     AND nvl(hr_budget_id, -1) = nvl(p_hr_budget_id, -1)
4838 	     AND pay_element_id = l_cost_element_id(i)
4839 	     AND currency_code = l_cost_currency_code(i)
4840 	     AND budget_revision_id is null
4841 	     AND base_line_version = l_base_line_version
4842 	     AND start_date = l_cost_start_date(i)
4843 	     AND end_date = l_cost_end_date(i);
4844 
4845          END IF;
4846 
4847 	  FOR i IN 1..l_count_costs LOOP
4848             -- for bug 4545590
4849 	    IF (NOT g_wks_new_hr_budget) AND (SQL%BULK_ROWCOUNT(i) <> 0)  THEN
4850 	      l_cost_update(i) := 'Y';
4851 	    ELSE
4852 	      l_cost_update(i) := 'N';
4853 	    END IF;
4854 	  END LOOP;
4855 
4856 	  FOR i IN 1..l_count_costs LOOP
4857 	    IF l_cost_update(i) = 'N' THEN
4858 
4859 	     Modify_Position_Costs
4860 	       (p_api_version => 1.0,
4861 		p_return_status => l_return_status,
4862 		p_msg_count => l_msg_count,
4863 		p_msg_data => l_msg_data,
4864 		p_position_id => l_cost_position_id(i),
4865 		p_hr_budget_id => p_hr_budget_id,
4866 		p_pay_element_id => l_cost_element_id(i),
4867 		p_budget_revision_id => null,
4868 		p_base_line_version => l_base_line_version,
4869 		p_start_date => l_cost_start_date(i),
4870 		p_end_date => l_cost_end_date(i),
4871 		p_currency_code => l_cost_currency_code(i),
4872 		p_element_cost => l_cost_element_cost(i));
4873 
4874 	     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4875 		raise FND_API.G_EXC_ERROR;
4876 	     END IF;
4877 
4878 	    END IF;
4879 	END LOOP;
4880       END LOOP;
4881 
4882       l_count_costs := 0;
4883       l_cost_position_id.delete;
4884       l_cost_start_date.delete;
4885       l_cost_end_date.delete;
4886       l_cost_element_id.delete;
4887       l_cost_currency_code.delete;
4888       l_cost_element_cost.delete;
4889       l_cost_update.delete;
4890 
4891      END IF;
4892 
4893      --iii. process the account lines
4894      IF ( l_count_accounts >= l_batch_size) THEN
4895 
4896        FOR l_base_line IN 1..2 LOOP
4897 	 IF l_base_line = 1 THEN
4898 	   l_base_line_version := 'O';
4899 	 ELSE
4900 	   l_base_line_version := 'C';
4901 	 END IF;
4902 
4903 	-- for bug 4545590
4904          IF NOT g_wks_new_hr_budget THEN
4905 
4906 	 FORALL i IN 1..l_count_accounts
4907 	  UPDATE PSB_POSITION_ACCOUNTS
4908 	     SET amount = l_account_amount(i),
4909 		 budget_group_id = l_account_bg_id(i),
4910 		 last_update_date = sysdate,
4911 		 last_updated_by = FND_GLOBAL.USER_ID,
4912 		 last_update_login = FND_GLOBAL.LOGIN_ID
4913 	   WHERE position_id = l_account_position_id(i)
4914 	     AND nvl(hr_budget_id, -1) = nvl(p_hr_budget_id, -1)
4915 	     AND code_combination_id = l_account_cc_id(i)
4916 	     AND currency_code = l_account_currency_code(i)
4917 	     AND budget_revision_id is null
4918 	     AND base_line_version = l_base_line_version
4919 	     AND start_date = l_account_start_date(i)
4920 	     AND end_date = l_account_end_date(i);
4921 
4922 	  END IF;
4923 
4924 	   FOR i IN 1..l_count_accounts LOOP
4925 	    -- for bug 4545590
4926 	    IF (NOT g_wks_new_hr_budget) AND (SQL%BULK_ROWCOUNT(i) <> 0) THEN
4927 	      l_account_update(i) := 'Y';
4928 	    ELSE
4929 	      l_account_update(i) := 'N';
4930 	    END IF;
4931 	   END LOOP;
4932 
4933 	   FOR i IN 1..l_count_accounts LOOP
4934 
4935 	    IF l_account_update(i) = 'N' THEN
4936 	     Modify_Position_Accounts
4937 	      (p_api_version => 1.0,
4938 	       p_return_status => l_return_status,
4939 	       p_msg_count => l_msg_count,
4940 	       p_msg_data => l_msg_data,
4941 	       p_position_id => l_account_position_id(i),
4942 	       p_hr_budget_id => p_hr_budget_id,
4943 	       p_budget_revision_id => null,
4944 	       p_budget_group_id => l_account_bg_id(i),
4945 	       p_base_line_version => l_base_line_version,
4946 	       p_start_date => l_account_start_date(i),
4947 	       p_end_date => l_account_end_date(i),
4948 	       p_code_combination_id => l_account_cc_id(i),
4949 	       p_currency_code => l_account_currency_code(i),
4950 	       p_amount => l_account_amount(i));
4951 
4952 	     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4953 	       raise FND_API.G_EXC_ERROR;
4954 	     END IF;
4955 
4956 	   END IF;
4957 	 END LOOP;
4958        END LOOP;
4959 
4960        l_count_accounts := 0;
4961        l_account_position_id.delete;
4962        l_account_start_date.delete;
4963        l_account_end_date.delete;
4964        l_account_cc_id.delete;
4965        l_account_currency_code.delete;
4966        l_account_amount.delete;
4967        l_account_bg_id.delete;
4968        l_account_update.delete;
4969 
4970      END IF;
4971      /*For Bug No : 1808322 End*/
4972 
4973      /*For Bug No : 2434152 Start*/
4974      IF l_position_ctr = g_checkpoint_save THEN
4975        commit work;
4976        l_position_ctr := 0;
4977        Savepoint Upload_Position_Worksheet_Pvt;
4978      END IF;
4979      /*For Bug No : 2434152 End*/
4980 
4981   end loop; /* c_wpl */
4982 
4983  /*For Bug No : 1808322 Start*/
4984   --Process the remaining records existing in the PL/SQL tables
4985 
4986   --i. process the FTE lines
4987   IF ( l_count_fte > 0) THEN
4988     FOR l_base_line IN 1..2 LOOP
4989 
4990       IF l_base_line = 1 THEN
4991 	l_base_line_version := 'O';
4992       ELSE
4993 	l_base_line_version := 'C';
4994       END IF;
4995 
4996       -- for bug 4545590
4997       IF NOT g_wks_new_hr_budget THEN
4998 
4999       FORALL i IN 1..l_count_fte
5000       UPDATE PSB_POSITION_FTE
5001 	 SET fte = l_fte_fte(i),
5002 	     last_update_date = sysdate,
5003 	     last_updated_by = FND_GLOBAL.USER_ID,
5004 	     last_update_login = FND_GLOBAL.LOGIN_ID
5005        WHERE position_id = l_fte_position_id(i)
5006 	 AND nvl(hr_budget_id,-1) = nvl(p_hr_budget_id,-1)
5007 	 AND budget_revision_id is null
5008 	 AND base_line_version = l_base_line_version
5009 	 AND start_date = l_fte_start_date(i)
5010 	 AND end_date = l_fte_end_date(i);
5011 
5012       END IF;
5013 
5014 	FOR i IN 1..l_count_fte LOOP
5015 	  -- for bug 4545590
5016 	  IF (NOT g_wks_new_hr_budget) AND (SQL%BULK_ROWCOUNT(i) <> 0) THEN
5017 	    l_fte_update(i) := 'Y';
5018 	  ELSE
5019 	    l_fte_update(i) := 'N';
5020 	  END IF;
5021 	END LOOP;
5022 
5023 	FOR i IN 1..l_count_fte LOOP
5024 
5025 	  IF l_fte_update(i) = 'N' THEN
5026 	    Modify_Position_FTE
5027 	      (p_api_version => 1.0,
5028 	       p_return_status => l_return_status,
5029 	       p_msg_count => l_msg_count,
5030 	       p_msg_data => l_msg_data,
5031 	       p_position_id => l_fte_position_id(i),
5032 	       p_hr_budget_id => p_hr_budget_id,
5033 	       p_budget_revision_id => null,
5034 	       p_base_line_version => l_base_line_version,
5035 	       p_fte => l_fte_fte(i),
5036 	       p_start_date => l_fte_start_date(i),
5037 	       p_end_date => l_fte_end_date(i));
5038 
5039 	    IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5040 	      raise FND_API.G_EXC_ERROR;
5041 	    END IF;
5042 	  END IF;
5043 
5044 	END LOOP;
5045     END LOOP;
5046 
5047     l_count_fte := 0;
5048     l_fte_position_id.delete;
5049     l_fte_start_date.delete;
5050     l_fte_end_date.delete;
5051     l_fte_fte.delete;
5052     l_fte_update.delete;
5053 
5054   END IF;
5055 
5056   --ii. Process the costs lines
5057   IF ( l_count_costs > 0) THEN
5058     FOR l_base_line IN 1..2 LOOP
5059 
5060       IF l_base_line = 1 THEN
5061 	l_base_line_version := 'O';
5062       ELSE
5063 	l_base_line_version := 'C';
5064       END IF;
5065 
5066       -- for bug 4545590
5067       IF NOT g_wks_new_hr_budget THEN
5068 
5069       FORALL i IN 1..l_count_costs
5070 	  UPDATE PSB_POSITION_COSTS
5071 	     SET element_cost = l_cost_element_cost(i),
5072 		 last_update_date = sysdate,
5073 		 last_updated_by = FND_GLOBAL.USER_ID,
5074 		 last_update_login = FND_GLOBAL.LOGIN_ID
5075 	   WHERE position_id = l_cost_position_id(i)
5076 	     AND nvl(hr_budget_id, -1) = nvl(p_hr_budget_id, -1)
5077 	     AND pay_element_id = l_cost_element_id(i)
5078 	     AND currency_code = l_cost_currency_code(i)
5079 	     AND budget_revision_id is null
5080 	     AND base_line_version = l_base_line_version
5081 	     AND start_date = l_cost_start_date(i)
5082 	     AND end_date = l_cost_end_date(i);
5083 
5084        END IF;
5085 
5086 
5087 	FOR i IN 1..l_count_costs LOOP
5088 	  -- for bug 4545590
5089 	  IF (NOT g_wks_new_hr_budget) AND (SQL%BULK_ROWCOUNT(i) <> 0) THEN
5090 	    l_cost_update(i) := 'Y';
5091 	  ELSE
5092 	    l_cost_update(i) := 'N';
5093 	  END IF;
5094 	END LOOP;
5095 
5096 	FOR i IN 1..l_count_costs LOOP
5097 	  IF l_cost_update(i) = 'N' THEN
5098 
5099 	    Modify_Position_Costs
5100 	       (p_api_version => 1.0,
5101 		p_return_status => l_return_status,
5102 		p_msg_count => l_msg_count,
5103 		p_msg_data => l_msg_data,
5104 		p_position_id => l_cost_position_id(i),
5105 		p_hr_budget_id => p_hr_budget_id,
5106 		p_pay_element_id => l_cost_element_id(i),
5107 		p_budget_revision_id => null,
5108 		p_base_line_version => l_base_line_version,
5109 		p_start_date => l_cost_start_date(i),
5110 		p_end_date => l_cost_end_date(i),
5111 		p_currency_code => l_cost_currency_code(i),
5112 		p_element_cost => l_cost_element_cost(i));
5113 
5114 	    IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5115 		raise FND_API.G_EXC_ERROR;
5116 	    END IF;
5117 
5118 	    END IF;
5119       END LOOP;
5120     END LOOP;
5121 
5122     l_count_costs := 0;
5123     l_cost_position_id.delete;
5124     l_cost_start_date.delete;
5125     l_cost_end_date.delete;
5126     l_cost_element_id.delete;
5127     l_cost_currency_code.delete;
5128     l_cost_element_cost.delete;
5129     l_cost_update.delete;
5130 
5131    END IF;
5132 
5133    --iii. process the account lines
5134    IF ( l_count_accounts > 0) THEN
5135 
5136      FOR l_base_line IN 1..2 LOOP
5137 
5138        IF l_base_line = 1 THEN
5139 	 l_base_line_version := 'O';
5140        ELSE
5141 	 l_base_line_version := 'C';
5142        END IF;
5143 
5144 	-- FOR BUG 4545590
5145        IF NOT g_wks_new_hr_budget THEN
5146 
5147        FORALL i IN 1..l_count_accounts
5148 	  UPDATE PSB_POSITION_ACCOUNTS
5149 	     SET amount = l_account_amount(i),
5150 		 budget_group_id = l_account_bg_id(i),
5151 		 last_update_date = sysdate,
5152 		 last_updated_by = FND_GLOBAL.USER_ID,
5153 		 last_update_login = FND_GLOBAL.LOGIN_ID
5154 	   WHERE position_id = l_account_position_id(i)
5155 	     AND nvl(hr_budget_id, -1) = nvl(p_hr_budget_id, -1)
5156 	     AND code_combination_id = l_account_cc_id(i)
5157 	     AND currency_code = l_account_currency_code(i)
5158 	     AND budget_revision_id is null
5159 	     AND base_line_version = l_base_line_version
5160 	     AND start_date = l_account_start_date(i)
5161 	     AND end_date = l_account_end_date(i);
5162 
5163 	END IF;
5164 
5165 	 FOR i IN 1..l_count_accounts LOOP
5166 	  -- for bug 4545590
5167 	  IF (NOT g_wks_new_hr_budget) AND (SQL%BULK_ROWCOUNT(i) <> 0) THEN
5168 	    l_account_update(i) := 'Y';
5169 	  ELSE
5170 	    l_account_update(i) := 'N';
5171 	  END IF;
5172 	 END LOOP;
5173 
5174 	 FOR i IN 1..l_count_accounts LOOP
5175 
5176 	   IF l_account_update(i) = 'N' THEN
5177 	     Modify_Position_Accounts
5178 	      (p_api_version => 1.0,
5179 	       p_return_status => l_return_status,
5180 	       p_msg_count => l_msg_count,
5181 	       p_msg_data => l_msg_data,
5182 	       p_position_id => l_account_position_id(i),
5183 	       p_hr_budget_id => p_hr_budget_id,
5184 	       p_budget_revision_id => null,
5185 	       p_budget_group_id => l_account_bg_id(i),
5186 	       p_base_line_version => l_base_line_version,
5187 	       p_start_date => l_account_start_date(i),
5188 	       p_end_date => l_account_end_date(i),
5189 	       p_code_combination_id => l_account_cc_id(i),
5190 	       p_currency_code => l_account_currency_code(i),
5191 	       p_amount => l_account_amount(i));
5192 
5193 	   IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5194 	     raise FND_API.G_EXC_ERROR;
5195 	   END IF;
5196 
5197 	   END IF;
5198        END LOOP;
5199      END LOOP;
5200 
5201      l_count_accounts := 0;
5202      l_account_position_id.delete;
5203      l_account_start_date.delete;
5204      l_account_end_date.delete;
5205      l_account_cc_id.delete;
5206      l_account_currency_code.delete;
5207      l_account_amount.delete;
5208      l_account_bg_id.delete;
5209      l_account_update.delete;
5210 
5211    END IF;
5212  /*For Bug No : 1808322 End*/
5213 
5214   /*For Bug No : 2434152 Start*/
5215   --Perform commit for all unsaved records
5216   commit work;
5217   /*For Bug No : 2434152 End*/
5218 
5219   -- Initialize API return status to success
5220   p_return_status := FND_API.G_RET_STS_SUCCESS;
5221 
5222 EXCEPTION
5223 
5224    when FND_API.G_EXC_ERROR then
5225      /*For Bug No : 2434152 Start*/
5226      rollback to Upload_Position_Worksheet_Pvt;
5227      /*For Bug No : 2434152 End*/
5228      p_return_status := FND_API.G_RET_STS_ERROR;
5229 
5230    when FND_API.G_EXC_UNEXPECTED_ERROR then
5231      /*For Bug No : 2434152 Start*/
5232      rollback to Upload_Position_Worksheet_Pvt;
5233      /*For Bug No : 2434152 End*/
5234      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5235 
5236    when OTHERS then
5237      /*For Bug No : 2434152 Start*/
5238      rollback to Upload_Position_Worksheet_Pvt;
5239      /*For Bug No : 2434152 End*/
5240      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5241 
5242 END Upload_Position_Worksheet;
5243 
5244 /* ----------------------------------------------------------------------- */
5245 
5246 PROCEDURE Record_Position_Transaction
5247 ( p_return_status          OUT  NOCOPY  VARCHAR2,
5248   p_msg_count              OUT  NOCOPY  NUMBER,
5249   p_msg_data               OUT  NOCOPY  VARCHAR2,
5250   p_event_type             IN   VARCHAR2,
5251   p_source_id              IN   NUMBER,
5252   p_hr_budget_id           IN   NUMBER,
5253   p_from_budget_year_id    IN   NUMBER,
5254   p_to_budget_year_id      IN   NUMBER,
5255   p_transfer_to_interface  IN   VARCHAR2,
5256   p_transfer_to_hrms       IN   VARCHAR2
5257 ) IS
5258 
5259   l_position_event_id      NUMBER;
5260 
5261   l_api_name               CONSTANT VARCHAR2(30)   := 'Record_Position_Transaction';
5262 
5263 BEGIN
5264 
5265   if p_hr_budget_id is null then
5266   begin
5267 
5268     update PSB_POSITION_EVENTS_ALL
5269        set org_id = FND_PROFILE.VALUE('ORG_ID'),
5270 	   transfer_to_interface = p_transfer_to_interface,
5271 	   transfer_to_hrms = p_transfer_to_hrms,
5272 	   interface_last_update_date = sysdate,
5273 	   interface_last_updated_by = FND_GLOBAL.USER_ID,
5274 	   interface_last_update_login = FND_GLOBAL.LOGIN_ID,
5275 	   hrms_last_update_date = sysdate,
5276 	   hrms_last_updated_by = FND_GLOBAL.USER_ID,
5277 	   hrms_last_update_login = FND_GLOBAL.LOGIN_ID
5278      where event_type = p_event_type
5279        and source_id = p_source_id
5280        and from_budget_year_id = p_from_budget_year_id
5281        and to_budget_year_id = p_to_budget_year_id
5282        and hr_budget_id is null;
5283 
5284   end;
5285   else
5286   begin
5287 
5288     update PSB_POSITION_EVENTS_ALL
5289        set org_id = FND_PROFILE.VALUE('ORG_ID'),
5290 	   transfer_to_interface = p_transfer_to_interface,
5291 	   transfer_to_hrms = p_transfer_to_hrms,
5292 	   interface_last_update_date = sysdate,
5293 	   interface_last_updated_by = FND_GLOBAL.USER_ID,
5294 	   interface_last_update_login = FND_GLOBAL.LOGIN_ID,
5295 	   hrms_last_update_date = sysdate,
5296 	   hrms_last_updated_by = FND_GLOBAL.USER_ID,
5297 	   hrms_last_update_login = FND_GLOBAL.LOGIN_ID
5298      where event_type = p_event_type
5299        and source_id = p_source_id
5300        and from_budget_year_id = p_from_budget_year_id
5301        and to_budget_year_id = p_to_budget_year_id
5302        and hr_budget_id = p_hr_budget_id;
5303 
5304   end;
5305   end if;
5306 
5307   if SQL%NOTFOUND then
5308   begin
5309 
5310     INSERT INTO PSB_POSITION_EVENTS_ALL
5311 	  (position_event_id,
5312 	   event_type,
5313 	   source_id,
5314 	   org_id,
5315 	   hr_budget_id,
5316 	   from_budget_year_id,
5317 	   to_budget_year_id,
5318 	   transfer_to_interface,
5319 	   transfer_to_hrms,
5320 	   interface_created_by,
5321 	   interface_creation_date,
5322 	   interface_last_update_date,
5323 	   interface_last_updated_by,
5324 	   interface_last_update_login,
5325 	   hrms_created_by,
5326 	   hrms_creation_date,
5327 	   hrms_last_update_date,
5328 	   hrms_last_updated_by,
5329 	   hrms_last_update_login)
5330     VALUES (PSB_POSITION_EVENTS_ALL_S.NEXTVAL,
5331 	    p_event_type,
5332 	    p_source_id,
5333 	    FND_PROFILE.VALUE('ORG_ID'),
5334 	    p_hr_budget_id,
5335 	    p_from_budget_year_id,
5336 	    p_to_budget_year_id,
5337 	    p_transfer_to_interface,
5338 	    p_transfer_to_hrms,
5339 	    FND_GLOBAL.USER_ID,
5340 	    sysdate,
5341 	    sysdate,
5342 	    FND_GLOBAL.USER_ID,
5343 	    FND_GLOBAL.LOGIN_ID,
5344 	    FND_GLOBAL.USER_ID,
5345 	    sysdate,
5346 	    sysdate,
5347 	    FND_GLOBAL.USER_ID,
5348 	    FND_GLOBAL.LOGIN_ID) RETURNING position_event_id into l_position_event_id;
5349 
5350   end;
5351   end if;
5352 
5353   -- Initialize API return status to success
5354   p_return_status := FND_API.G_RET_STS_SUCCESS;
5355 
5356 EXCEPTION
5357 
5358    when FND_API.G_EXC_ERROR then
5359      p_return_status := FND_API.G_RET_STS_ERROR;
5360 
5361      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
5362 				p_data  => p_msg_data);
5363 
5364    when FND_API.G_EXC_UNEXPECTED_ERROR then
5365      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5366 
5367      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
5368 				p_data  => p_msg_data);
5369 
5370    when OTHERS then
5371      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5372 
5373      if FND_MSG_PUB.Check_Msg_Level
5374        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
5375        FND_MSG_PUB.Add_Exc_Msg
5376 	  (p_pkg_name => G_PKG_NAME,
5377 	   p_procedure_name => l_api_name);
5378      end if;
5379 
5380      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
5381 				p_data  => p_msg_data);
5382 END Record_Position_Transaction;
5383 
5384 /* ----------------------------------------------------------------------- */
5385 
5386 -- Check if position exists in Worksheet or Budget Revision
5387 
5388 FUNCTION Position_Exists
5389 ( p_event_type              VARCHAR2,
5390   p_source_id               NUMBER,
5391   p_position_id             NUMBER) RETURN BOOLEAN IS
5392 
5393   l_position_exists         BOOLEAN := FALSE;
5394 
5395   cursor c_position_exists_bp is
5396     select 'Exists'
5397       from dual
5398      where exists
5399 	  (select 'Exists'
5400 	     from psb_ws_position_lines wpl, psb_ws_lines_positions wlp
5401 	    where wpl.position_id = p_position_id
5402 	      and wlp.position_line_id = wpl.position_line_id
5403 	      and wlp.worksheet_id = p_source_id);
5404 
5405   cursor c_position_exists_br is
5406     select 'Exists'
5407       from dual
5408      where exists
5409 	  (select 'Exists'
5410 	     from psb_budget_revision_positions brp, psb_budget_revision_pos_lines brpl
5411 	    where brp.position_id = p_position_id
5412 	      and brpl.budget_revision_pos_line_id = brp.budget_revision_pos_line_id
5413 	      and brpl.budget_revision_id = p_source_id);
5414 
5415 BEGIN
5416 
5417   if p_event_type = 'BP' then
5418   begin
5419 
5420     for c_position_rec in c_position_exists_bp loop
5421       l_position_exists := TRUE;
5422     end loop;
5423 
5424   end;
5425   elsif p_event_type = 'BR' then
5426   begin
5427 
5428     for c_position_rec in c_position_exists_br loop
5429       l_position_exists := TRUE;
5430     end loop;
5431 
5432   end;
5433   end if;
5434 
5435   return l_position_exists;
5436 
5437 END Position_Exists;
5438 
5439 /* ----------------------------------------------------------------------- */
5440 
5441 -- Check if account distribution for position exists in worksheet or budget
5442 -- revision
5443 
5444 FUNCTION AcctDist_Exists
5445 ( p_event_type   VARCHAR2,
5446   p_source_id    NUMBER,
5447   p_position_id  NUMBER) RETURN BOOLEAN IS
5448 
5449   l_dist_exists  BOOLEAN := FALSE;
5450 
5451   cursor c_dist_exists_bp is
5452     select 'Exists'
5453       from dual
5454      where exists
5455 	  (select 'Exists'
5456 	     from psb_ws_position_lines wpl, psb_ws_lines_positions wlp, psb_ws_account_lines wal
5457 	    where wpl.position_id = p_position_id
5458 	      and wlp.position_line_id = wpl.position_line_id
5459 	      and wlp.worksheet_id = p_source_id
5460 	      and wal.position_line_id = wpl.position_line_id);
5461 
5462   cursor c_dist_exists_br is
5463     select 'Exists'
5464       from dual
5465      where exists
5466 	  (select 'Exists'
5467 	     from psb_position_accounts
5468 	    where budget_revision_id = p_source_id
5469 	      and position_id = p_position_id);
5470 
5471 BEGIN
5472 
5473   if p_event_type = 'BP' then
5474   begin
5475 
5476     for c_dist_rec in c_dist_exists_bp loop
5477       l_dist_exists := TRUE;
5478     end loop;
5479 
5480   end;
5481   elsif p_event_type = 'BR' then
5482   begin
5483 
5484     for c_dist_rec in c_dist_exists_br loop
5485       l_dist_exists := TRUE;
5486     end loop;
5487 
5488   end;
5489   end if;
5490 
5491   return l_dist_exists;
5492 
5493 END AcctDist_Exists;
5494 
5495 /* ----------------------------------------------------------------------- */
5496 
5497 -- Check that the position budget is complete : all position transactions in
5498 -- the worksheet or budget revision have been approved and all positions have
5499 -- an account distribution for charging the position cost
5500 
5501 PROCEDURE Validate_Position_Budget
5502 ( p_return_status  OUT  NOCOPY  VARCHAR2,
5503   --p_msg_count      OUT  NOCOPY  NUMBER,
5504   --p_msg_data       OUT  NOCOPY  VARCHAR2,
5505   p_event_type     IN   VARCHAR2,
5506   p_source_id      IN   NUMBER) IS
5507 
5508   l_data_extract_id     NUMBER;
5509   l_budget_group_id     NUMBER;
5510   l_no_hr_pos_count     NUMBER := 0;
5511   l_no_acct_dist        NUMBER := 0;
5512 
5513   l_msg_count           NUMBER;
5514   l_msg_data            VARCHAR2(2000);
5515 
5516   cursor c_bp is
5517     select data_extract_id from psb_worksheets where worksheet_id = p_source_id;
5518 
5519   cursor c_revision is
5520     select budget_group_id from psb_budget_revisions where budget_revision_id = p_source_id;
5521 
5522   cursor c_position is
5523     select position_id, name, hr_position_id, hr_employee_id
5524       from psb_positions
5525      where data_extract_id = l_data_extract_id;
5526 
5527 /* Fix for Bug #2642767 Start */
5528   cursor c_no_hr_position (positionid NUMBER) is
5529     select name, transaction_id
5530       from psb_positions
5531      where position_id = positionid
5532        and hr_position_id is null;
5533 /* Fix for Bug #2642767 End */
5534 
5535   l_api_name               CONSTANT VARCHAR2(30)   := 'Validate_Position_Budget';
5536 
5537 BEGIN
5538 
5539   if p_event_type = 'BP' then
5540   begin
5541 
5542     for c_worksheet_rec in c_bp loop
5543       l_data_extract_id := c_worksheet_rec.data_extract_id;
5544     end loop;
5545 
5546     for c_position_rec in c_position loop
5547 
5548       -- check if position exists in worksheet or budget revision
5549 
5550       if (Position_Exists(p_event_type => p_event_type, p_source_id => p_source_id,
5551 	  p_position_id => c_position_rec.position_id)) then
5552       begin
5553 
5554 	-- check that all positions are mapped to HR positions
5555 	for c_no_hr_position_rec in c_no_hr_position (c_position_rec.position_id) loop
5556 /* Fix for Bug #2642767 Start */
5557           if c_no_hr_position_rec.transaction_id is null then
5558      	      message_token('POSITION',c_no_hr_position_rec.name);
5559 	      add_message('PSB', 'PSB_PQH_NO_HR_POSITION');
5560 	  else
5561               message_token('POSITION',c_no_hr_position_rec.name);
5562               add_message('PSB', 'PSB_PQH_NO_HR_POSITION_TRX');
5563  	  end if;
5564 	  l_no_hr_pos_count := l_no_hr_pos_count + 1;
5565 	end loop;
5566 /* Fix for Bug #2642767 End*/
5567 
5568 	if not AcctDist_Exists (p_event_type => p_event_type, p_source_id => p_source_id,
5569 				p_position_id => c_position_rec.position_id) then
5570 	  message_token('POSITION', c_position_rec.name);
5571 	  add_message('PSB', 'PSB_PQH_NO_DIST_POSITION');
5572 	  l_no_acct_dist := l_no_acct_dist + 1;
5573 	end if;
5574 
5575       end;
5576       end if;
5577 
5578     end loop;
5579 
5580   end;
5581   elsif p_event_type = 'BR' then
5582   begin
5583 
5584     for c_revision_rec in c_revision loop
5585       l_budget_group_id := c_revision_rec.budget_group_id;
5586     end loop;
5587 
5588     l_data_extract_id := PSB_BUDGET_REVISIONS_PVT.Find_System_Data_Extract
5589 			    (p_budget_group_id => l_budget_group_id);
5590 
5591     for c_position_rec in c_position loop
5592 
5593       -- check if position exists in worksheet or budget revision
5594 
5595       if (Position_Exists(p_event_type => p_event_type, p_source_id => p_source_id,
5596 	  p_position_id => c_position_rec.position_id)) then
5597       begin
5598 
5599 	-- check that all positions are mapped to HR positions
5600 	for c_no_hr_position_rec in c_no_hr_position (c_position_rec.position_id) loop
5601 /* Fix for Bug #2642767 Start */
5602           if c_no_hr_position_rec.transaction_id is null then
5603               message_token('POSITION',c_no_hr_position_rec.name);
5604               add_message('PSB', 'PSB_PQH_NO_HR_POSITION');
5605           else
5606               message_token('POSITION',c_no_hr_position_rec.name);
5607               add_message('PSB', 'PSB_PQH_NO_HR_POSITION_TRX');
5608           end if;
5609 /* Fix for Bug #2642767 End*/
5610 	  l_no_hr_pos_count := l_no_hr_pos_count + 1;
5611 	end loop;
5612 
5613 	if not AcctDist_Exists (p_event_type => p_event_type, p_source_id => p_source_id,
5614 				p_position_id => c_position_rec.position_id) then
5615 	  message_token('POSITION', c_position_rec.name);
5616 	  add_message('PSB', 'PSB_PQH_NO_DIST_POSITION');
5617 	  l_no_acct_dist := l_no_acct_dist + 1;
5618 	end if;
5619 
5620       end;
5621       end if;
5622 
5623     end loop;
5624 
5625   end;
5626   end if;
5627 
5628   if ((l_no_hr_pos_count > 0) or (l_no_acct_dist > 0)) then
5629     FND_MESSAGE.SET_NAME('PSB','PSB_DEBUG_MESSAGE');
5630     FND_MESSAGE.SET_TOKEN('MESSAGE', 'HR Pos Count 0');
5631     FND_MSG_PUB.Add;
5632     raise FND_API.G_EXC_ERROR;
5633   end if;
5634 
5635   -- Initialize API return status to success
5636   p_return_status := FND_API.G_RET_STS_SUCCESS;
5637 
5638 EXCEPTION
5639 
5640    when FND_API.G_EXC_ERROR then
5641      p_return_status := FND_API.G_RET_STS_ERROR;
5642 
5643      FND_MSG_PUB.Add_Exc_Msg
5644 	  (p_pkg_name => G_PKG_NAME,
5645 	   p_procedure_name => l_api_name);
5646 
5647      FND_MSG_PUB.Count_And_Get (p_count => l_msg_count,
5648 				p_data  => l_msg_data);
5649 
5650    when FND_API.G_EXC_UNEXPECTED_ERROR then
5651      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5652 
5653      FND_MSG_PUB.Add_Exc_Msg
5654 	  (p_pkg_name => G_PKG_NAME,
5655 	   p_procedure_name => l_api_name);
5656 
5657      FND_MSG_PUB.Count_And_Get (p_count => l_msg_count,
5658 				p_data  => l_msg_data);
5659 
5660    when OTHERS then
5661      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5662 
5663      if FND_MSG_PUB.Check_Msg_Level
5664        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
5665        FND_MSG_PUB.Add_Exc_Msg
5666 	  (p_pkg_name => G_PKG_NAME,
5667 	   p_procedure_name => l_api_name);
5668      end if;
5669 
5670      FND_MSG_PUB.Count_And_Get (p_count => l_msg_count,
5671 				p_data  => l_msg_data);
5672 
5673 END Validate_Position_Budget;
5674 
5675 /* ----------------------------------------------------------------------- */
5676 
5677 PROCEDURE Upload_Worksheet
5678 ( p_api_version          IN   NUMBER,
5679   p_init_msg_list        IN   VARCHAR2 := FND_API.G_FALSE,
5680   p_commit               IN   VARCHAR2 := FND_API.G_FALSE,
5681   p_validation_level     IN   NUMBER := FND_API.G_VALID_LEVEL_NONE,
5682   p_return_status        OUT  NOCOPY  VARCHAR2,
5683   p_msg_count            OUT  NOCOPY  NUMBER,
5684   p_msg_data             OUT  NOCOPY  VARCHAR2,
5685   p_worksheet_id         IN   NUMBER,
5686   p_hr_budget_id         IN   NUMBER,
5687   p_from_budget_year_id  IN   NUMBER,
5688   p_to_budget_year_id    IN   NUMBER
5689 ) IS
5690 
5691   l_api_name               CONSTANT VARCHAR2(30)   := 'Upload_Worksheet';
5692   l_api_version            CONSTANT NUMBER         := 1.0;
5693 
5694   l_data_extract_id        NUMBER;
5695   l_business_group_id      NUMBER;
5696   l_budget_group_id        NUMBER;
5697   l_budget_calendar_id     NUMBER;
5698   l_system_data_extract_id NUMBER;
5699   l_flex_mapping_set_id    NUMBER;
5700   l_position_id_flex_num   NUMBER;
5701   l_root_budget_group_id   NUMBER;
5702   l_root_set_of_books_id   NUMBER;
5703   l_root_short_name        VARCHAR2(20);
5704   l_data_extract_name      VARCHAR2(30);
5705 
5706   l_msg_count              NUMBER;
5707   l_msg_data               VARCHAR2(2000);
5708 
5709   l_return_status          VARCHAR2(1);
5710 
5711   cursor c_WS is
5712     select a.data_extract_id,
5713 	   b.business_group_id,
5714 	   b.position_id_flex_num,
5715 	   a.budget_calendar_id,
5716 	   a.budget_group_id,
5717 	   a.flex_mapping_set_id
5718       from PSB_WORKSHEETS a,
5719 	   PSB_DATA_EXTRACTS b
5720      where a.worksheet_id = p_worksheet_id
5721        and b.data_extract_id = a.data_extract_id;
5722 
5723   cursor c_root_budget_group is
5724     Select nvl(root_budget_group_id, budget_group_id) budget_group_id,
5725 	   nvl(root_set_of_books_id, set_of_books_id) set_of_books_id,
5726 	   nvl(root_short_name, short_name) short_name,
5727 	   business_group_id
5728       from psb_budget_groups_v
5729      where budget_group_id = l_budget_group_id;
5730 
5731 BEGIN
5732 
5733   -- Standard Start of API savepoint
5734 
5735   SAVEPOINT     Upload_Worksheet_Pvt;
5736 
5737   -- Standard call to check for call compatibility.
5738 
5739   if not FND_API.Compatible_API_Call (l_api_version,
5740 				      p_api_version,
5741 				      l_api_name,
5742 				      G_PKG_NAME)
5743   then
5744     raise FND_API.G_EXC_UNEXPECTED_ERROR;
5745   end if;
5746 
5747   -- Initialize message list if p_init_msg_list is set to TRUE.
5748 
5749   if FND_API.to_Boolean (p_init_msg_list) then
5750     FND_MSG_PUB.initialize;
5751   end if;
5752 
5753   for c_WS_Rec in c_WS loop
5754     l_data_extract_id := c_WS_Rec.data_extract_id;
5755     l_business_group_id := c_WS_Rec.business_group_id;
5756     l_budget_group_id := c_WS_Rec.budget_group_id;
5757     l_budget_calendar_id := c_WS_Rec.budget_calendar_id;
5758     l_flex_mapping_set_id := c_WS_Rec.flex_mapping_set_id;
5759     l_position_id_flex_num := c_WS_Rec.position_id_flex_num;
5760   end loop;
5761 
5762   if l_budget_calendar_id <> nvl(PSB_WS_ACCT1.g_budget_calendar_id, FND_API.G_MISS_NUM) then
5763   begin
5764 
5765     PSB_WS_ACCT1.Cache_Budget_Calendar
5766        (p_return_status => l_return_status,
5767 	p_budget_calendar_id => l_budget_calendar_id);
5768 
5769     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
5770       raise FND_API.G_EXC_ERROR;
5771     end if;
5772 
5773   end;
5774   /* start bug 4545590 */
5775   else
5776     -- The same calendar for the worksheet
5777     -- If the calendar is same, then there cannot be overlapping of periods
5778     g_wks_no_date_overlap := TRUE;
5779   /* end bug 4545590 */
5780   end if;
5781 
5782   -- for bug 4507389
5783   -- get the budget year start date and the budget year end date
5784   -- this is used to upload salary distributions
5785   FOR l_year_index in 1..PSB_WS_ACCT1.g_num_budget_years LOOP
5786     IF PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id
5787         = p_from_budget_year_id THEN
5788       g_year_start_date
5789         := PSB_WS_ACCT1.g_budget_years(l_year_index).start_date;
5790     END IF;
5791 
5792     IF PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_id
5793         = p_to_budget_year_id THEN
5794       g_year_end_date
5795         := PSB_WS_ACCT1.g_budget_years(l_year_index).end_date;
5796     END IF;
5797   END LOOP;
5798   -- for bug 4507389
5799 
5800 
5801   l_system_data_extract_id := PSB_BUDGET_REVISIONS_PVT.Find_System_Data_Extract
5802 				  (p_budget_group_id => l_budget_group_id);
5803 
5804   if l_system_data_extract_id is null then
5805   begin
5806 
5807     for c_root_budget_group_rec in c_root_budget_group loop
5808       l_root_budget_group_id := c_root_budget_group_rec.budget_group_id;
5809       l_root_set_of_books_id := c_root_budget_group_rec.set_of_books_id;
5810       l_root_short_name      := c_root_budget_group_rec.short_name;
5811     end loop;
5812 
5813     l_data_extract_name := fnd_message.get_string( 'PSB',
5814 			   'PSB_SYSTEM_DATA_EXTRACT_NAME')||' '||l_root_short_name;
5815 
5816     INSERT INTO PSB_DATA_EXTRACTS
5817 	  (DATA_EXTRACT_ID, DATA_EXTRACT_NAME, DATA_EXTRACT_METHOD,
5818 	   SET_OF_BOOKS_ID, BUSINESS_GROUP_ID, BUDGET_GROUP_ID,
5819 	   POSITION_ID_FLEX_NUM, BASE_SALARY_DATE, REQ_DATA_AS_OF_DATE,
5820 	   LAST_EXTRACT_DATE, LAST_UPDATE_DATE, LAST_UPDATED_BY,
5821 	   LAST_UPDATE_LOGIN, CREATED_BY, CREATION_DATE, SYSTEM_DATA_EXTRACT)
5822     VALUES (PSB_DATA_EXTRACTS_S.NEXTVAL, l_data_extract_name, 'CREATE',
5823 	    l_root_set_of_books_id, l_business_group_id, l_root_budget_group_id,
5824 	    l_position_id_flex_num, null, sysdate,
5825 	    sysdate, sysdate, FND_GLOBAL.USER_ID,
5826 	    FND_GLOBAL.LOGIN_ID, FND_GLOBAL.USER_ID, sysdate, 'Y') RETURNING data_extract_id into l_system_data_extract_id;
5827 
5828   end;
5829   end if;
5830 
5831   -- check that all position transactions in the worksheet have been approved
5832 
5833   Validate_Position_Budget
5834 	 (p_return_status => l_return_status,
5835           --p_msg_count => l_msg_count,
5836           --p_msg_data => l_msg_data,
5837 	  p_event_type => 'BP',
5838 	  p_source_id => p_worksheet_id);
5839 
5840   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
5841     raise FND_API.G_EXC_ERROR;
5842   end if;
5843 
5844   -- Lock Worksheet in exclusive mode
5845 
5846   PSB_CONCURRENCY_CONTROL_PVT.Enforce_Concurrency_Control
5847      (p_api_version => 1.0,
5848       p_return_status => l_return_status,
5849       p_concurrency_class => 'WORKSHEET_CONSOLIDATION',
5850       p_concurrency_entity_name => 'DATA_EXTRACT',
5851       p_concurrency_entity_id => p_worksheet_id);
5852 
5853   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
5854     raise FND_API.G_EXC_ERROR;
5855   end if;
5856 
5857   Upload_Attribute_Values
5858        (p_return_status => l_return_status,
5859 	p_source_data_extract_id => l_data_extract_id,
5860 	p_source_business_group_id => l_business_group_id,
5861 	p_target_data_extract_id => l_system_data_extract_id);
5862 
5863   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
5864     raise FND_API.G_EXC_ERROR;
5865   end if;
5866 
5867   commit work;
5868 
5869   Upload_Elements
5870        (p_return_status => l_return_status,
5871 	p_source_data_extract_id => l_data_extract_id,
5872 	p_target_data_extract_id => l_system_data_extract_id,
5873 	p_worksheet_id => p_worksheet_id,
5874 	p_budget_calendar_id => l_budget_calendar_id,
5875 	p_flex_mapping_set_id => l_flex_mapping_set_id,
5876 	p_budget_group_id => l_budget_group_id);
5877 
5878   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
5879     raise FND_API.G_EXC_ERROR;
5880   end if;
5881 
5882   commit work;
5883 
5884   Upload_Employees
5885       (p_return_status => l_return_status,
5886        p_source_data_extract_id => l_data_extract_id,
5887        p_source_business_group_id => l_business_group_id,
5888        p_target_data_extract_id => l_system_data_extract_id);
5889 
5890   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
5891     raise FND_API.G_EXC_ERROR;
5892   end if;
5893 
5894   commit work;
5895 
5896   Upload_Positions
5897       (p_return_status => l_return_status,
5898        p_worksheet_id => p_worksheet_id,
5899        p_budget_calendar_id => l_budget_calendar_id,
5900        p_flex_mapping_set_id => l_flex_mapping_set_id,
5901        p_source_data_extract_id => l_data_extract_id,
5902        p_target_data_extract_id => l_system_data_extract_id,
5903        /* Bug 3867577 Start */
5904        -- To synchronize with the added parameters.
5905        p_from_budget_year_id    => p_from_budget_year_id,
5906        p_to_budget_year_id      => p_to_budget_year_id
5907        /* Bug 3867577 End */
5908 );
5909 
5910   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
5911     raise FND_API.G_EXC_ERROR;
5912   end if;
5913 
5914   commit work;
5915   /*For Bug No : 2434152 Start*/
5916   --Save point is not required since the COMMIT in
5917   --the following API call overrides the same.
5918   --SAVEPOINT     Upload_Worksheet_Pvt;
5919   /*For Bug No : 2434152 End*/
5920 
5921   -- Create Positions from the position sets for the system data extract
5922 
5923   PSB_BUDGET_POSITION_PVT.Populate_Budget_Positions
5924      (p_api_version => 1.0,
5925       p_return_status => l_return_status,
5926       p_msg_count => l_msg_count,
5927       p_msg_data => l_msg_data,
5928       p_data_extract_id => l_system_data_extract_id);
5929 
5930   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
5931     raise FND_API.G_EXC_ERROR;
5932   end if;
5933 
5934   commit work;
5935 
5936   -- Lock Worksheet in exclusive mode
5937 
5938   PSB_CONCURRENCY_CONTROL_PVT.Enforce_Concurrency_Control
5939      (p_api_version => 1.0,
5940       p_return_status => l_return_status,
5941       p_concurrency_class => 'WORKSHEET_CONSOLIDATION',
5942       p_concurrency_entity_name => 'WORKSHEET',
5943       p_concurrency_entity_id => p_worksheet_id);
5944 
5945   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
5946     raise FND_API.G_EXC_ERROR;
5947   end if;
5948 
5949   Upload_Position_Worksheet
5950       (p_return_status => l_return_status,
5951        p_worksheet_id => p_worksheet_id,
5952        p_flex_mapping_set_id => l_flex_mapping_set_id,
5953        p_budget_calendar_id => l_budget_calendar_id,
5954        p_target_data_extract_id => l_system_data_extract_id,
5955        p_hr_budget_id => p_hr_budget_id);
5956 
5957   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
5958     raise FND_API.G_EXC_ERROR;
5959   end if;
5960 
5961   Record_Position_Transaction
5962       (p_return_status => l_return_status,
5963        p_msg_count => l_msg_count,
5964        p_msg_data => l_msg_data,
5965        p_event_type => 'BP',
5966        p_source_id => p_worksheet_id,
5967        p_hr_budget_id => p_hr_budget_id,
5968        p_from_budget_year_id => p_from_budget_year_id,
5969        p_to_budget_year_id => p_to_budget_year_id,
5970        p_transfer_to_interface => 'Y',
5971        p_transfer_to_hrms => 'N');
5972 
5973   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
5974     raise FND_API.G_EXC_ERROR;
5975   end if;
5976 
5977   -- Standard check of p_commit.
5978   if FND_API.to_Boolean (p_commit) then
5979     commit work;
5980   end if;
5981 
5982   -- Standard call to get message count and if count is 1, get message info.
5983   FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
5984 			     p_data  => p_msg_data);
5985 
5986   -- Initialize API return status to success
5987   p_return_status := FND_API.G_RET_STS_SUCCESS;
5988 
5989 EXCEPTION
5990 
5991    when FND_API.G_EXC_ERROR then
5992      /*For Bug No : 2434152 Start*/
5993      --rollback to Upload_Worksheet_Pvt;
5994      rollback;
5995      /*For Bug No : 2434152 End*/
5996      p_return_status := FND_API.G_RET_STS_ERROR;
5997 
5998      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
5999 				p_data  => p_msg_data);
6000 
6001    when FND_API.G_EXC_UNEXPECTED_ERROR then
6002      /*For Bug No : 2434152 Start*/
6003      --rollback to Upload_Worksheet_Pvt;
6004      rollback;
6005      /*For Bug No : 2434152 End*/
6006      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6007 
6008      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
6009 				p_data  => p_msg_data);
6010 
6011    when OTHERS then
6012      /*For Bug No : 2434152 Start*/
6013      --rollback to Upload_Worksheet_Pvt;
6014      rollback;
6015      /*For Bug No : 2434152 End*/
6016      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6017 
6018      if FND_MSG_PUB.Check_Msg_Level
6019        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
6020        FND_MSG_PUB.Add_Exc_Msg
6021 	  (p_pkg_name => G_PKG_NAME,
6022 	   p_procedure_name => l_api_name);
6023      end if;
6024 
6025      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
6026 				p_data  => p_msg_data);
6027 
6028 END Upload_Worksheet;
6029 
6030 /* ----------------------------------------------------------------------- */
6031 
6032 -- This is the execution file for the concurrent program "Upload Worksheet
6033 -- to Position Control" through Standard Report Submissions
6034 
6035 PROCEDURE Upload_Worksheet_CP
6036 ( errbuf                 OUT  NOCOPY  VARCHAR2,
6037   retcode                OUT  NOCOPY  VARCHAR2,
6038   p_worksheet_id         IN   NUMBER,
6039   p_hr_budget_id         IN   NUMBER,
6040   p_from_budget_year_id  IN   NUMBER,
6041   p_to_budget_year_id    IN   NUMBER,
6042   p_run_mode             IN   VARCHAR2
6043 ) IS
6044 
6045   l_api_name             CONSTANT VARCHAR2(30)   := 'Upload_Worksheet_CP';
6046   l_api_version          CONSTANT NUMBER         :=  1.0;
6047 
6048   l_return_status        VARCHAR2(1);
6049   l_msg_count            NUMBER;
6050   l_msg_data             VARCHAR2(2000);
6051 
6052   /* bug no 3670254 */
6053   -- This variable stores the data extract used for worksheet
6054   l_data_extract_id 	 NUMBER;
6055   /* bug no 3670254 */
6056 
6057   -- for bug 4545590
6058   lp_num_var		 NUMBER := 0;
6059 
6060 begin
6061 
6062   /* bug no 3670254 */
6063   -- get the profile value for FTE upload
6064   fnd_profile.get('PSB_HRMS_FTE_UPLOAD_OPTION', g_hrms_fte_upload_option);
6065   g_hrms_fte_upload_option := NVL(g_hrms_fte_upload_option, 'PERIOD');
6066 
6067   IF g_hrms_fte_upload_option = 'ASSIGNMENT' THEN
6068     -- fetch the data extract used for position worksheet
6069     FOR l_data_extract_rec IN (SELECT data_extract_id
6070 	               	       FROM   psb_worksheets
6071 	               	       WHERE  worksheet_id = p_worksheet_id)
6072     LOOP
6073       l_data_extract_Id := l_data_extract_rec.data_extract_id;
6074     END LOOP;
6075 
6076     -- This cursor gets the Attribute id for the business group
6077     -- associated with the data extract.
6078     FOR l_att_fte_rec IN (SELECT attribute_id FROM psb_attributes a,
6079                                                    psb_data_extracts b
6080 			  WHERE a.business_group_id = b.business_group_id
6081 			  AND a.system_attribute_type = 'FTE'
6082 			  AND b.data_extract_id = l_data_extract_Id)
6083     LOOP
6084       g_fte_attribute_Id := l_att_fte_rec.attribute_id;
6085     END LOOP;
6086   END IF;
6087   /* Bug No 3670254 */
6088 
6089   /* Start Bug 4545590 */
6090   g_wks_no_date_overlap := FALSE;
6091   g_wks_new_hr_budget := FALSE;
6092 
6093   IF p_hr_budget_id IS NOT NULL THEN
6094     FOR li_csr IN ( SELECT 1 new_bud
6095 			  FROM   dual
6096 			  WHERE EXISTS (SELECT 1
6097 			  		  FROM psb_position_events_all
6098 			  		  WHERE event_type = 'BP'
6099 			                AND hr_budget_id = p_hr_budget_id)) LOOP
6100       lp_num_var := li_csr.new_bud;
6101     END LOOP;
6102 
6103     IF lp_num_var = 0 THEN
6104     	g_wks_no_date_overlap := TRUE;
6105 	g_wks_new_hr_budget := TRUE;
6106     END IF;
6107 
6108   END IF;
6109   /* End Bug 4545590 */
6110 
6111 
6112   Upload_Budget_HRMS
6113        (p_api_version => 1.0,
6114 	p_return_status => l_return_status,
6115 	p_validation_level => FND_API.G_VALID_LEVEL_FULL,
6116 	p_msg_count => l_msg_count,
6117 	p_msg_data => l_msg_data,
6118 	p_event_type => 'BP',
6119 	p_source_id => p_worksheet_id,
6120 	p_hr_budget_id => p_hr_budget_id,
6121 	p_from_budget_year_id => p_from_budget_year_id,
6122 	p_to_budget_year_id => p_to_budget_year_id);
6123 
6124   if l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6125     raise FND_API.G_EXC_ERROR;
6126   end if;
6127 
6128   Upload_Worksheet
6129 	(p_api_version => 1.0,
6130 	 p_commit => FND_API.G_TRUE,
6131 	 p_init_msg_list => FND_API.G_TRUE,
6132 	 p_return_status => l_return_status,
6133 	 p_msg_count => l_msg_count,
6134 	 p_msg_data => l_msg_data,
6135 	 p_worksheet_id => p_worksheet_id,
6136 	 p_hr_budget_id => p_hr_budget_id,
6137 	 p_from_budget_year_id => p_from_budget_year_id,
6138 	 p_to_budget_year_id => p_to_budget_year_id);
6139 
6140   if l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6141     raise FND_API.G_EXC_ERROR;
6142   end if;
6143 
6144   if ((p_run_mode = 'F') and (p_hr_budget_id is not null)) then
6145   begin
6146 
6147     Upload_Budget_HRMS
6148 	 (p_api_version => 1.0,
6149 	  p_return_status => l_return_status,
6150 	  p_validation_level => FND_API.G_VALID_LEVEL_NONE,
6151 	  p_msg_count => l_msg_count,
6152 	  p_msg_data => l_msg_data,
6153 	  p_event_type => 'BP',
6154 	  p_source_id => p_worksheet_id,
6155 	  p_hr_budget_id => p_hr_budget_id,
6156 	  p_from_budget_year_id => p_from_budget_year_id,
6157 	  p_to_budget_year_id => p_to_budget_year_id);
6158 
6159     if l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6160       raise FND_API.G_EXC_ERROR;
6161     end if;
6162 
6163   end;
6164   end if;
6165     /* Start Bug No. 2322856 */
6166 --  PSB_MESSAGE_S.Print_Success;
6167     /* End Bug No. 2322856 */
6168   retcode := 0;
6169 
6170   commit work;
6171 
6172 EXCEPTION
6173 
6174    when FND_API.G_EXC_ERROR then
6175      PSB_MESSAGE_S.Print_Error (p_mode => FND_FILE.LOG,
6176 				p_print_header => FND_API.G_TRUE);
6177      retcode := 2;
6178 
6179    when FND_API.G_EXC_UNEXPECTED_ERROR then
6180      PSB_MESSAGE_S.Print_Error (p_mode => FND_FILE.LOG,
6181 				p_print_header => FND_API.G_TRUE);
6182      retcode := 2;
6183 
6184    when OTHERS then
6185      if FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
6186        FND_MSG_PUB.Add_Exc_Msg (G_PKG_NAME,
6187 				l_api_name);
6188      end if;
6189 
6190      PSB_MESSAGE_S.Print_Error (p_mode => FND_FILE.LOG,
6191 				p_print_header => FND_API.G_TRUE);
6192      retcode := 2;
6193 
6194 END Upload_Worksheet_CP;
6195 
6196 /* ----------------------------------------------------------------------- */
6197 
6198 FUNCTION Prorate
6199 (p_original_amount      NUMBER,
6200  p_original_start_date  DATE,
6201  p_original_end_date    DATE,
6202  p_new_start_date       DATE,
6203  p_new_end_date         DATE
6204 ) RETURN NUMBER IS
6205 
6206   l_prorated_value       NUMBER;
6207 
6208 BEGIN
6209 
6210   l_prorated_value := p_original_amount
6211 		      * (p_new_end_date - p_new_start_date + 1)
6212 		      / (p_original_end_date - p_original_start_date + 1);
6213 
6214   return l_prorated_value;
6215 
6216 END Prorate;
6217 
6218 /* ----------------------------------------------------------------------- */
6219 
6220 PROCEDURE Insert_Position_Accounts
6221 ( p_return_status         OUT  NOCOPY  VARCHAR2,
6222   p_msg_count             OUT  NOCOPY  NUMBER,
6223   p_msg_data              OUT  NOCOPY  VARCHAR2,
6224   p_position_id           IN   NUMBER,
6225   p_hr_budget_id          IN   NUMBER,
6226   p_start_date            IN   DATE,
6227   p_end_date              IN   DATE,
6228   p_code_combination_id   IN   NUMBER,
6229   p_budget_group_id       IN   NUMBER,
6230   p_currency_code         IN   VARCHAR2,
6231   p_amount                IN   NUMBER,
6232   p_budget_revision_id    IN   NUMBER,
6233   p_base_line_version     IN   VARCHAR2
6234 ) IS
6235 
6236   l_api_name            CONSTANT VARCHAR2(30)   := 'Insert_Position_Accounts';
6237   l_position_account_line_id   NUMBER;
6238 
6239 BEGIN
6240 
6241   insert into PSB_POSITION_ACCOUNTS
6242 	(position_account_line_id, position_id, hr_budget_id, budget_revision_id, budget_group_id,
6243 	 base_line_version, start_date, end_date, code_combination_id, currency_code,
6244 	 amount, last_update_date, last_updated_by, last_update_login, created_by, creation_date)
6245    values (PSB_POSITION_ACCOUNTS_S.NEXTVAL, p_position_id, p_hr_budget_id, p_budget_revision_id, p_budget_group_id,
6246 	   p_base_line_version, p_start_date, p_end_date, p_code_combination_id, p_currency_code,
6247 	   p_amount, sysdate, FND_GLOBAL.USER_ID, FND_GLOBAL.LOGIN_ID, FND_GLOBAL.USER_ID, sysdate)
6248   RETURNING position_account_line_id into l_position_account_line_id;
6249 
6250   -- Initialize API return status to success
6251   p_return_status := FND_API.G_RET_STS_SUCCESS;
6252 
6253 EXCEPTION
6254 
6255    when FND_API.G_EXC_ERROR then
6256      p_return_status := FND_API.G_RET_STS_ERROR;
6257 
6258      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
6259 				p_data  => p_msg_data);
6260 
6261    when FND_API.G_EXC_UNEXPECTED_ERROR then
6262      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6263 
6264      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
6265 				p_data  => p_msg_data);
6266 
6267    when OTHERS then
6268      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6269 
6270      if FND_MSG_PUB.Check_Msg_Level
6271        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
6272        FND_MSG_PUB.Add_Exc_Msg
6273 	  (p_pkg_name => G_PKG_NAME,
6274 	   p_procedure_name => l_api_name);
6275      end if;
6276 
6277      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
6278 				p_data  => p_msg_data);
6279 
6280 END Insert_Position_Accounts;
6281 
6282 /* ----------------------------------------------------------------------- */
6283 
6284 PROCEDURE Update_Position_Accounts
6285 ( p_return_status             OUT  NOCOPY  VARCHAR2,
6286   p_msg_count                 OUT  NOCOPY  NUMBER,
6287   p_msg_data                  OUT  NOCOPY  VARCHAR2,
6288   p_position_account_line_id  IN   NUMBER,
6289   p_budget_group_id           IN   NUMBER,
6290   p_start_date                IN   DATE := FND_API.G_MISS_DATE,
6291   p_end_date                  IN   DATE := FND_API.G_MISS_DATE,
6292   p_amount                    IN   NUMBER
6293 ) IS
6294 
6295   l_api_name            CONSTANT VARCHAR2(30)   := 'Update_Position_Accounts';
6296 
6297 BEGIN
6298 
6299   update PSB_POSITION_ACCOUNTS
6300      set amount = p_amount,
6301 	 budget_group_id = p_budget_group_id,
6302 	 start_date = decode(p_start_date, FND_API.G_MISS_DATE, start_date, p_start_date),
6303 	 end_date = decode(p_end_date, FND_API.G_MISS_DATE, end_date, p_end_date),
6304 	 last_update_date = sysdate,
6305 	 last_updated_by = FND_GLOBAL.USER_ID,
6306 	 last_update_login = FND_GLOBAL.LOGIN_ID
6307     where position_account_line_id = p_position_account_line_id;
6308 
6309   -- Initialize API return status to success
6310   p_return_status := FND_API.G_RET_STS_SUCCESS;
6311 
6312 EXCEPTION
6313 
6314    when FND_API.G_EXC_ERROR then
6315      p_return_status := FND_API.G_RET_STS_ERROR;
6316 
6317      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
6318 				p_data  => p_msg_data);
6319 
6320    when FND_API.G_EXC_UNEXPECTED_ERROR then
6321      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6322 
6323      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
6324 				p_data  => p_msg_data);
6325 
6326    when OTHERS then
6327      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6328 
6329      if FND_MSG_PUB.Check_Msg_Level
6330        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
6331        FND_MSG_PUB.Add_Exc_Msg
6332 	  (p_pkg_name => G_PKG_NAME,
6333 	   p_procedure_name => l_api_name);
6334      end if;
6335 
6336      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
6337 				p_data  => p_msg_data);
6338 
6339 END Update_Position_Accounts;
6340 
6341 /* ----------------------------------------------------------------------- */
6342 
6343 PROCEDURE Delete_Position_Accounts
6344 ( p_return_status             OUT  NOCOPY  VARCHAR2,
6345   p_msg_count                 OUT  NOCOPY  NUMBER,
6346   p_msg_data                  OUT  NOCOPY  VARCHAR2,
6347   p_position_account_line_id  IN   NUMBER
6348 ) IS
6349 
6350   l_api_name            CONSTANT VARCHAR2(30)   := 'Delete_Position_Accounts';
6351 
6352 BEGIN
6353 
6354   delete from PSB_POSITION_ACCOUNTS
6355    where position_account_line_id = p_position_account_line_id;
6356 
6357   -- Initialize API return status to success
6358   p_return_status := FND_API.G_RET_STS_SUCCESS;
6359 
6360 EXCEPTION
6361 
6362    when FND_API.G_EXC_ERROR then
6363      p_return_status := FND_API.G_RET_STS_ERROR;
6364 
6365      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
6366 				p_data  => p_msg_data);
6367 
6368    when FND_API.G_EXC_UNEXPECTED_ERROR then
6369      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6370 
6371      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
6372 				p_data  => p_msg_data);
6373 
6374    when OTHERS then
6375      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6376 
6377      if FND_MSG_PUB.Check_Msg_Level
6378        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
6379        FND_MSG_PUB.Add_Exc_Msg
6380 	  (p_pkg_name => G_PKG_NAME,
6381 	   p_procedure_name => l_api_name);
6382      end if;
6383 
6384      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
6385 				p_data  => p_msg_data);
6386 
6387 END Delete_Position_Accounts;
6388 
6389 /* ----------------------------------------------------------------------- */
6390 
6391 PROCEDURE Modify_Position_Accounts
6392 ( p_api_version           IN   NUMBER,
6393   p_init_msg_list         IN   VARCHAR2 := FND_API.G_FALSE,
6394   p_commit                IN   VARCHAR2 := FND_API.G_FALSE,
6395   p_validation_level      IN   NUMBER := FND_API.G_VALID_LEVEL_NONE,
6396   p_return_status         OUT  NOCOPY  VARCHAR2,
6397   p_msg_count             OUT  NOCOPY  NUMBER,
6398   p_msg_data              OUT  NOCOPY  VARCHAR2,
6399   p_position_id           IN   NUMBER,
6400   p_hr_budget_id          IN   NUMBER,
6401   p_budget_revision_id    IN   NUMBER,
6402   p_budget_group_id       IN   NUMBER,
6403   p_base_line_version     IN   VARCHAR2,
6404   p_start_date            IN   DATE,
6405   p_end_date              IN   DATE,
6406   p_code_combination_id   IN   NUMBER,
6407   p_currency_code         IN   VARCHAR2,
6408   p_amount                IN   NUMBER
6409 ) IS
6410 
6411   l_api_name              CONSTANT VARCHAR2(30)   := 'Modify_Position_Accounts';
6412   l_api_version           CONSTANT NUMBER         := 1.0;
6413 
6414   l_msg_count              NUMBER;
6415   l_msg_data               VARCHAR2(2000);
6416 
6417   l_created_record        BOOLEAN := FALSE;
6418   l_updated_record        BOOLEAN;
6419 
6420   l_return_status         VARCHAR2(1);
6421   /*For Bug No : 1808322 Start*/
6422   l_update_flag           BOOLEAN := TRUE;
6423   /*For Bug No : 1808322 End*/
6424 
6425   cursor c_Overlap is
6426     select *
6427       from PSB_POSITION_ACCOUNTS
6428      where position_id = p_position_id
6429        and nvl(hr_budget_id, -1) = nvl(p_hr_budget_id, -1)
6430        and code_combination_id = p_code_combination_id
6431        and currency_code = p_currency_code
6432        and nvl(budget_revision_id, -1) = nvl(p_budget_revision_id, -1)
6433        and nvl(base_line_version, FND_API.G_MISS_CHAR) = nvl(p_base_line_version, FND_API.G_MISS_CHAR)
6434        and ((((p_end_date is not null)
6435 	 and ((start_date <= p_end_date)
6436 	  and (end_date is null))
6437 	  or ((start_date between p_start_date and p_end_date)
6438 	   or (end_date between p_start_date and p_end_date)
6439 	  or ((start_date < p_start_date)
6440 	  and (end_date > p_end_date)))))
6441 	  or ((p_end_date is null)
6442 	  and (nvl(end_date, p_start_date) >= p_start_date)));
6443 
6444 BEGIN
6445 
6446   -- Standard call to check for call compatibility.
6447 
6448   if not FND_API.Compatible_API_Call (l_api_version,
6449 				      p_api_version,
6450 				      l_api_name,
6451 				      G_PKG_NAME)
6452   then
6453     raise FND_API.G_EXC_UNEXPECTED_ERROR;
6454   end if;
6455 
6456   -- Initialize message list if p_init_msg_list is set to TRUE.
6457 
6458   if FND_API.to_Boolean (p_init_msg_list) then
6459     FND_MSG_PUB.initialize;
6460   end if;
6461 
6462   /*For Bug No : 1808322 Start*/
6463   IF p_budget_revision_id IS NOT null  THEN
6464   /*For Bug No : 1808322 End*/
6465 
6466   update PSB_POSITION_ACCOUNTS
6467      set amount = p_amount,
6468 	 budget_group_id = p_budget_group_id,
6469 	 last_update_date = sysdate,
6470 	 last_updated_by = FND_GLOBAL.USER_ID,
6471 	 last_update_login = FND_GLOBAL.LOGIN_ID
6472    where position_id = p_position_id
6473      and nvl(hr_budget_id, -1) = nvl(p_hr_budget_id, -1)
6474      and code_combination_id = p_code_combination_id
6475      and currency_code = p_currency_code
6476      and nvl(budget_revision_id, -1) = nvl(p_budget_revision_id, -1)
6477      and nvl(base_line_version, FND_API.G_MISS_CHAR) = nvl(p_base_line_version, FND_API.G_MISS_CHAR)
6478      and start_date = p_start_date
6479      and nvl(end_date, FND_API.G_MISS_DATE) = nvl(p_end_date, FND_API.G_MISS_DATE);
6480 
6481   /*For Bug No : 1808322 Start*/
6482     IF SQL%NOTFOUND THEN
6483       l_update_flag := FALSE;
6484     END IF;
6485 
6486   ELSE
6487       l_update_flag := FALSE;
6488   END IF;
6489 
6490   if not l_update_flag then --SQL%NOTFOUND then
6491   /*For Bug No : 1808322 End*/
6492   begin
6493 
6494     for l_init_index in 1..g_accounts.Count loop
6495       g_accounts(l_init_index).position_account_line_id := null;
6496       g_accounts(l_init_index).position_id := null;
6497       g_accounts(l_init_index).start_date := null;
6498       g_accounts(l_init_index).end_date := null;
6499       g_accounts(l_init_index).code_combination_id := null;
6500       g_accounts(l_init_index).budget_group_id := null;
6501       g_accounts(l_init_index).currency_code := null;
6502       g_accounts(l_init_index).amount := null;
6503       g_accounts(l_init_index).budget_revision_id := null;
6504       g_accounts(l_init_index).base_line_version := null;
6505       g_accounts(l_init_index).delete_flag := null;
6506     end loop;
6507 
6508     g_num_accounts := 0;
6509 
6510 
6511     -- for bug 4545590
6512     -- this performance improvement is only for budget upload and not
6513     -- for budget revisions. So we need to check if budget revision id is null
6514     IF NOT (g_wks_no_date_overlap AND p_budget_revision_id IS NULL) THEN
6515 
6516     for c_Overlap_Rec in c_Overlap loop
6517       g_num_accounts := g_num_accounts + 1;
6518 
6519       g_accounts(g_num_accounts).position_account_line_id := c_Overlap_Rec.position_account_line_id;
6520       g_accounts(g_num_accounts).position_id := c_Overlap_Rec.position_id;
6521       g_accounts(g_num_accounts).start_date := c_Overlap_Rec.start_date;
6522       g_accounts(g_num_accounts).end_date := c_Overlap_Rec.end_date;
6523       g_accounts(g_num_accounts).code_combination_id := c_Overlap_Rec.code_combination_id;
6524       g_accounts(g_num_accounts).budget_group_id := c_Overlap_Rec.budget_group_id;
6525       g_accounts(g_num_accounts).currency_code := c_Overlap_Rec.currency_code;
6526       g_accounts(g_num_accounts).amount := c_Overlap_Rec.amount;
6527       g_accounts(g_num_accounts).budget_revision_id := c_Overlap_Rec.budget_revision_id;
6528       g_accounts(g_num_accounts).base_line_version := c_Overlap_Rec.base_line_version;
6529       g_accounts(g_num_accounts).delete_flag := TRUE;
6530     end loop;
6531 
6532     END IF;
6533 
6534 
6535     if g_num_accounts = 0 then
6536     begin
6537 
6538       Insert_Position_Accounts
6539 	   (p_return_status => l_return_status,
6540 	    p_msg_count     => l_msg_count,
6541 	    p_msg_data      => l_msg_data,
6542 	    p_position_id => p_position_id,
6543 	    p_hr_budget_id => p_hr_budget_id,
6544 	    p_start_date => p_start_date,
6545 	    p_end_date => p_end_date,
6546 	    p_code_combination_id => p_code_combination_id,
6547 	    p_budget_group_id => p_budget_group_id,
6548 	    p_currency_code => p_currency_code,
6549 	    p_amount => p_amount,
6550 	    p_budget_revision_id => p_budget_revision_id,
6551 	    p_base_line_version => p_base_line_version);
6552 
6553 	if l_return_status <> FND_API.G_RET_STS_SUCCESS then
6554 	  raise FND_API.G_EXC_ERROR;
6555 	end if;
6556 
6557     end;
6558     else
6559     begin
6560 
6561       for l_account_index in 1..g_num_accounts loop
6562 
6563 	l_updated_record := FALSE;
6564 
6565 	/* Effective Start Date Matches */
6566 
6567 	if g_accounts(l_account_index).start_date = p_start_date then
6568 	begin
6569 
6570 	  Update_Position_Accounts
6571 		(p_return_status => l_return_status,
6572 	         p_msg_count     => l_msg_count,
6573 	         p_msg_data      => l_msg_data,
6574 		 p_position_account_line_id => g_accounts(l_account_index).position_account_line_id,
6575 		 p_budget_group_id => p_budget_group_id,
6576 		 p_end_date => p_end_date,
6577 		 p_amount => p_amount);
6578 
6579 	  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
6580 	    raise FND_API.G_EXC_ERROR;
6581 	  end if;
6582 
6583 	  g_accounts(l_account_index).delete_flag := FALSE;
6584 
6585 	end;
6586 
6587 	/* Effective Dates Overlap */
6588 	elsif (((g_accounts(l_account_index).start_date <= (p_start_date - 1)) and
6589 	       ((g_accounts(l_account_index).end_date is null) or
6590 		(g_accounts(l_account_index).end_date > (p_start_date - 1)))) or
6591 	       ((g_accounts(l_account_index).start_date > p_start_date) and
6592 	       ((g_accounts(l_account_index).end_date is null) or
6593 		(g_accounts(l_account_index).end_date > (p_end_date + 1))))) then
6594 	begin
6595 
6596 	  if ((g_accounts(l_account_index).start_date < (p_start_date - 1)) and
6597 	     ((g_accounts(l_account_index).end_date is null) or
6598 	      (g_accounts(l_account_index).end_date > (p_start_date - 1)))) then
6599 	  begin
6600 
6601 	    Update_Position_Accounts
6602 		  (p_return_status => l_return_status,
6603 	           p_msg_count     => l_msg_count,
6604 	           p_msg_data      => l_msg_data,
6605 		   p_position_account_line_id => g_accounts(l_account_index).position_account_line_id,
6606 		   p_budget_group_id => p_budget_group_id,
6607 		   p_end_date => p_start_date - 1,
6608 		   p_amount => Prorate (p_original_amount => g_accounts(l_account_index).amount,
6609 					p_original_start_date => g_accounts(l_account_index).start_date,
6610 					p_original_end_date => g_accounts(l_account_index).end_date,
6611 					p_new_start_date => g_accounts(l_account_index).start_date,
6612 					p_new_end_date => p_start_date - 1));
6613 
6614 	    if l_return_status <> FND_API.G_RET_STS_SUCCESS then
6615 	      raise FND_API.G_EXC_ERROR;
6616 	    else
6617 	      l_updated_record := TRUE;
6618 	    end if;
6619 
6620 	    g_accounts(l_account_index).delete_flag := FALSE;
6621 
6622 	  end;
6623 	  elsif ((g_accounts(l_account_index).start_date > p_start_date) and
6624 		((p_end_date is not null) and
6625 		((g_accounts(l_account_index).end_date is null) or
6626 		 (g_accounts(l_account_index).end_date > (p_end_date + 1))))) then
6627 	  begin
6628 
6629 	    Update_Position_Accounts
6630 		  (p_return_status => l_return_status,
6631 	           p_msg_count     => l_msg_count,
6632 	           p_msg_data      => l_msg_data,
6633 		   p_position_account_line_id => g_accounts(l_account_index).position_account_line_id,
6634 		   p_budget_group_id => p_budget_group_id,
6635 		   p_start_date => p_end_date + 1,
6636 		   p_amount => Prorate (p_original_amount => g_accounts(l_account_index).amount,
6637 					p_original_start_date => g_accounts(l_account_index).start_date,
6638 					p_original_end_date => g_accounts(l_account_index).end_date,
6639 					p_new_start_date => p_end_date + 1,
6640 					p_new_end_date => g_accounts(l_account_index).end_date));
6641 
6642 	    if l_return_status <> FND_API.G_RET_STS_SUCCESS then
6643 	      raise FND_API.G_EXC_ERROR;
6644 	    else
6645 	      l_updated_record := FALSE;
6646 	    end if;
6647 
6648 	    g_accounts(l_account_index).delete_flag := FALSE;
6649 
6650 	  end;
6651 	  end if;
6652 
6653 	  if not l_created_record then
6654 	  begin
6655 
6656 	    Insert_Position_Accounts
6657 		  (p_return_status => l_return_status,
6658 	           p_msg_count     => l_msg_count,
6659 	           p_msg_data      => l_msg_data,
6660 		   p_position_id => p_position_id,
6661 		   p_hr_budget_id => p_hr_budget_id,
6662 		   p_start_date => p_start_date,
6663 		   p_end_date => p_end_date,
6664 		   p_code_combination_id => p_code_combination_id,
6665 		   p_budget_group_id => p_budget_group_id,
6666 		   p_currency_code => p_currency_code,
6667 		   p_amount => p_amount,
6668 		   p_budget_revision_id => p_budget_revision_id,
6669 		   p_base_line_version => p_base_line_version);
6670 
6671 	    if l_return_status <> FND_API.G_RET_STS_SUCCESS then
6672 	      raise FND_API.G_EXC_ERROR;
6673 	    else
6674 	      l_created_record := TRUE;
6675 	    end if;
6676 
6677 	  end;
6678 	  end if;
6679 
6680 	  if p_end_date is not null then
6681 	  begin
6682 
6683 	    if nvl(g_accounts(l_account_index).end_date, (p_end_date + 1)) > (p_end_date + 1) then
6684 	    begin
6685 
6686 	      if l_updated_record then
6687 	      begin
6688 
6689 		Insert_Position_Accounts
6690 		      (p_return_status => l_return_status,
6691 	               p_msg_count     => l_msg_count,
6692 	               p_msg_data      => l_msg_data,
6693 		       p_position_id => g_accounts(l_account_index).position_id,
6694 		       p_hr_budget_id => p_hr_budget_id,
6695 		       p_start_date => p_end_date + 1,
6696 		       p_end_date => g_accounts(l_account_index).end_date,
6697 		       p_code_combination_id => g_accounts(l_account_index).code_combination_id,
6698 		       p_budget_group_id => g_accounts(l_account_index).budget_group_id,
6699 		       p_currency_code => g_accounts(l_account_index).currency_code,
6700 		       p_amount => Prorate (p_original_amount => g_accounts(l_account_index).amount,
6701 					    p_original_start_date => g_accounts(l_account_index).start_date,
6702 					    p_original_end_date => g_accounts(l_account_index).end_date,
6703 					    p_new_start_date => p_end_date + 1,
6704 					    p_new_end_date => g_accounts(l_account_index).end_date),
6705 		       p_budget_revision_id => g_accounts(l_account_index).budget_revision_id,
6706 		       p_base_line_version => g_accounts(l_account_index).base_line_version);
6707 
6708 		if l_return_status <> FND_API.G_RET_STS_SUCCESS then
6709 		  raise FND_API.G_EXC_ERROR;
6710 		end if;
6711 
6712 	      end;
6713 	      else
6714 	      begin
6715 
6716 		Update_Position_Accounts
6717 		      (p_return_status => l_return_status,
6718 	               p_msg_count     => l_msg_count,
6719 	               p_msg_data      => l_msg_data,
6720 		       p_position_account_line_id => g_accounts(l_account_index).position_account_line_id,
6721 		       p_budget_group_id => p_budget_group_id,
6722 		       p_start_date => p_end_date + 1,
6723 		       p_end_date => g_accounts(l_account_index).end_date,
6724 		       p_amount => Prorate (p_original_amount => g_accounts(l_account_index).amount,
6725 					    p_original_start_date => g_accounts(l_account_index).start_date,
6726 					    p_original_end_date => g_accounts(l_account_index).end_date,
6727 					    p_new_start_date => p_end_date + 1,
6728 					    p_new_end_date => g_accounts(l_account_index).end_date));
6729 
6730 		if l_return_status <> FND_API.G_RET_STS_SUCCESS then
6731 		  raise FND_API.G_EXC_ERROR;
6732 		end if;
6733 
6734 		g_accounts(l_account_index).delete_flag := FALSE;
6735 
6736 	      end;
6737 	      end if;
6738 
6739 	    end;
6740 	    end if;
6741 
6742 	  end;
6743 	  end if;
6744 
6745 	end;
6746 	end if;
6747 
6748       end loop;
6749 
6750     end;
6751     end if;
6752 
6753     for l_account_index in 1..g_num_accounts loop
6754 
6755       if g_accounts(l_account_index).delete_flag then
6756       begin
6757 
6758 	Delete_Position_Accounts
6759 	      (p_return_status => l_return_status,
6760 	       p_msg_count     => l_msg_count,
6761 	       p_msg_data      => l_msg_data,
6762 	       p_position_account_line_id => g_accounts(l_account_index).position_account_line_id);
6763 
6764 	if l_return_status <> FND_API.G_RET_STS_SUCCESS then
6765 	  raise FND_API.G_EXC_ERROR;
6766 	end if;
6767 
6768       end;
6769       end if;
6770 
6771     end loop;
6772 
6773   end;
6774   end if;
6775 
6776   -- Standard check of p_commit.
6777   if FND_API.to_Boolean (p_commit) then
6778     commit work;
6779   end if;
6780 
6781   -- Standard call to get message count and if count is 1, get message info.
6782   FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
6783 			     p_data  => p_msg_data);
6784 
6785   -- Initialize API return status to success
6786   p_return_status := FND_API.G_RET_STS_SUCCESS;
6787 
6788 EXCEPTION
6789 
6790    when FND_API.G_EXC_ERROR then
6791      p_return_status := FND_API.G_RET_STS_ERROR;
6792 
6793      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
6794 				p_data  => p_msg_data);
6795 
6796    when FND_API.G_EXC_UNEXPECTED_ERROR then
6797      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6798 
6799      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
6800 				p_data  => p_msg_data);
6801 
6802    when OTHERS then
6803      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6804 
6805      if FND_MSG_PUB.Check_Msg_Level
6806        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
6807        FND_MSG_PUB.Add_Exc_Msg
6808 	  (p_pkg_name => G_PKG_NAME,
6809 	   p_procedure_name => l_api_name);
6810      end if;
6811 
6812      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
6813 				p_data  => p_msg_data);
6814 
6815 END Modify_Position_Accounts;
6816 
6817 /* ----------------------------------------------------------------------- */
6818 
6819 PROCEDURE Insert_Position_Costs
6820 ( p_return_status        OUT  NOCOPY  VARCHAR2,
6821   p_msg_count            OUT  NOCOPY  NUMBER,
6822   p_msg_data             OUT  NOCOPY  VARCHAR2,
6823   p_position_id          IN   NUMBER,
6824   p_hr_budget_id         IN   NUMBER,
6825   p_pay_element_id       IN   NUMBER,
6826   p_budget_revision_id   IN   NUMBER,
6827   p_base_line_version    IN   VARCHAR2,
6828   p_start_date           IN   DATE,
6829   p_end_date             IN   DATE,
6830   p_currency_code        IN   VARCHAR2,
6831   p_element_cost         IN   NUMBER
6832 ) IS
6833 
6834   l_api_name               CONSTANT VARCHAR2(30)   := 'Insert_Position_Costs';
6835   l_position_element_line_id  NUMBER;
6836 
6837 BEGIN
6838 
6839   insert into PSB_POSITION_COSTS
6840 	(position_element_line_id, position_id, hr_budget_id, pay_element_id, budget_revision_id,
6841 	 base_line_version, currency_code, start_date, end_date,
6842 	 element_cost, last_update_date, last_updated_by, last_update_login, created_by, creation_date)
6843    values (PSB_POSITION_COSTS_S.NEXTVAL, p_position_id, p_hr_budget_id, p_pay_element_id, p_budget_revision_id,
6844 	   p_base_line_version, p_currency_code, p_start_date, p_end_date,
6845 	   p_element_cost, sysdate, FND_GLOBAL.USER_ID, FND_GLOBAL.LOGIN_ID, FND_GLOBAL.USER_ID, sysdate)
6846   RETURNING position_element_line_id INTO l_position_element_line_id;
6847 
6848   -- Initialize API return status to success
6849   p_return_status := FND_API.G_RET_STS_SUCCESS;
6850 
6851 EXCEPTION
6852 
6853    when FND_API.G_EXC_ERROR then
6854      p_return_status := FND_API.G_RET_STS_ERROR;
6855 
6856      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
6857 				p_data  => p_msg_data);
6858 
6859    when FND_API.G_EXC_UNEXPECTED_ERROR then
6860      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6861 
6862      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
6863 				p_data  => p_msg_data);
6864 
6865    when OTHERS then
6866      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6867 
6868      if FND_MSG_PUB.Check_Msg_Level
6869        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
6870        FND_MSG_PUB.Add_Exc_Msg
6871 	  (p_pkg_name => G_PKG_NAME,
6872 	   p_procedure_name => l_api_name);
6873      end if;
6874 
6875 END Insert_Position_Costs;
6876 
6877 /* ----------------------------------------------------------------------- */
6878 
6879 PROCEDURE Update_Position_Costs
6880 ( p_return_status             OUT  NOCOPY  VARCHAR2,
6881   p_msg_count                 OUT  NOCOPY  NUMBER,
6882   p_msg_data                  OUT  NOCOPY  VARCHAR2,
6883   p_position_element_line_id  IN   NUMBER,
6884   p_start_date                IN   DATE := FND_API.G_MISS_DATE,
6885   p_end_date                  IN   DATE := FND_API.G_MISS_DATE,
6886   p_element_cost              IN   NUMBER
6887 ) IS
6888 
6889   l_api_name               CONSTANT VARCHAR2(30)   := 'Update_Position_Costs';
6890 
6891 BEGIN
6892 
6893   update PSB_POSITION_COSTS
6894      set element_cost = p_element_cost,
6895 	 start_date = decode(p_start_date, FND_API.G_MISS_DATE, start_date, p_start_date),
6896 	 end_date = decode(p_end_date, FND_API.G_MISS_DATE, end_date, p_end_date),
6897 	 last_update_date = sysdate,
6898 	 last_updated_by = FND_GLOBAL.USER_ID,
6899 	 last_update_login = FND_GLOBAL.LOGIN_ID
6900    where position_element_line_id = p_position_element_line_id;
6901 
6902   -- Initialize API return status to success
6903   p_return_status := FND_API.G_RET_STS_SUCCESS;
6904 
6905 EXCEPTION
6906 
6907    when FND_API.G_EXC_ERROR then
6908      p_return_status := FND_API.G_RET_STS_ERROR;
6909 
6910      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
6911 				p_data  => p_msg_data);
6912 
6913    when FND_API.G_EXC_UNEXPECTED_ERROR then
6914      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6915 
6916      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
6917 				p_data  => p_msg_data);
6918 
6919    when OTHERS then
6920      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6921 
6922      if FND_MSG_PUB.Check_Msg_Level
6923        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
6924        FND_MSG_PUB.Add_Exc_Msg
6925 	  (p_pkg_name => G_PKG_NAME,
6926 	   p_procedure_name => l_api_name);
6927      end if;
6928 
6929      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
6930 				p_data  => p_msg_data);
6931 
6932 END Update_Position_Costs;
6933 
6934 /* ----------------------------------------------------------------------- */
6935 
6936 PROCEDURE Delete_Position_Costs
6937 ( p_return_status             OUT  NOCOPY  VARCHAR2,
6938   p_msg_count                 OUT  NOCOPY  NUMBER,
6939   p_msg_data                  OUT  NOCOPY  VARCHAR2,
6940   p_position_element_line_id  IN   NUMBER
6941 ) IS
6942 
6943  l_api_name               CONSTANT VARCHAR2(30)   := 'Delete_Position_Costs';
6944 
6945 BEGIN
6946 
6947   delete from PSB_POSITION_COSTS
6948    where position_element_line_id = p_position_element_line_id;
6949 
6950   -- Initialize API return status to success
6951   p_return_status := FND_API.G_RET_STS_SUCCESS;
6952 
6953 EXCEPTION
6954 
6955    when FND_API.G_EXC_ERROR then
6956      p_return_status := FND_API.G_RET_STS_ERROR;
6957 
6958      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
6959 				p_data  => p_msg_data);
6960 
6961    when FND_API.G_EXC_UNEXPECTED_ERROR then
6962      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6963 
6964      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
6965 				p_data  => p_msg_data);
6966 
6967    when OTHERS then
6968      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6969 
6970      if FND_MSG_PUB.Check_Msg_Level
6971        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
6972        FND_MSG_PUB.Add_Exc_Msg
6973 	  (p_pkg_name => G_PKG_NAME,
6974 	   p_procedure_name => l_api_name);
6975      end if;
6976 
6977      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
6978 				p_data  => p_msg_data);
6979 
6980 END Delete_Position_Costs;
6981 
6982 /* ----------------------------------------------------------------------- */
6983 
6984 PROCEDURE Modify_Position_Costs
6985 ( p_api_version          IN   NUMBER,
6986   p_init_msg_list        IN   VARCHAR2 := FND_API.G_FALSE,
6987   p_commit               IN   VARCHAR2 := FND_API.G_FALSE,
6988   p_validation_level     IN   NUMBER := FND_API.G_VALID_LEVEL_NONE,
6989   p_return_status        OUT  NOCOPY  VARCHAR2,
6990   p_msg_count            OUT  NOCOPY  NUMBER,
6991   p_msg_data             OUT  NOCOPY  VARCHAR2,
6992   p_position_id          IN   NUMBER,
6993   p_hr_budget_id         IN   NUMBER,
6994   p_pay_element_id       IN   NUMBER,
6995   p_budget_revision_id   IN   NUMBER,
6996   p_base_line_version    IN   VARCHAR2,
6997   p_start_date           IN   DATE,
6998   p_end_date             IN   DATE,
6999   p_currency_code        IN   VARCHAR2,
7000   p_element_cost         IN   NUMBER
7001 ) IS
7002 
7003   l_api_name             CONSTANT VARCHAR2(30)   := 'Modify_Position_Costs';
7004   l_api_version          CONSTANT NUMBER         := 1.0;
7005 
7006   l_msg_count              NUMBER;
7007   l_msg_data               VARCHAR2(2000);
7008 
7009   l_created_record       BOOLEAN := FALSE;
7010   l_updated_record       BOOLEAN;
7011 
7012   l_return_status        VARCHAR2(1);
7013   /*For Bug No : 1808322 Start*/
7014   l_update_flag           BOOLEAN := TRUE;
7015   /*For Bug No : 1808322 End*/
7016 
7017   cursor c_Overlap is
7018     select *
7019       from PSB_POSITION_COSTS
7020      where position_id = p_position_id
7021        and nvl(hr_budget_id, -1) = nvl(p_hr_budget_id, -1)
7022        and pay_element_id = p_pay_element_id
7023        and currency_code = p_currency_code
7024        and nvl(budget_revision_id, -1) = nvl(p_budget_revision_id, -1)
7025        and nvl(base_line_version, FND_API.G_MISS_CHAR) = nvl(p_base_line_version, FND_API.G_MISS_CHAR)
7026        and ((((p_end_date is not null)
7027 	 and ((start_date <= p_end_date)
7028 	  and (end_date is null))
7029 	  or ((start_date between p_start_date and p_end_date)
7030 	   or (end_date between p_start_date and p_end_date)
7031 	  or ((start_date < p_start_date)
7032 	  and (end_date > p_end_date)))))
7033 	  or ((p_end_date is null)
7034 	  and (nvl(end_date, p_start_date) >= p_start_date)));
7035 
7036 BEGIN
7037 
7038   -- Standard call to check for call compatibility.
7039 
7040   if not FND_API.Compatible_API_Call (l_api_version,
7041 				      p_api_version,
7042 				      l_api_name,
7043 				      G_PKG_NAME)
7044   then
7045     raise FND_API.G_EXC_UNEXPECTED_ERROR;
7046   end if;
7047 
7048   -- Initialize message list if p_init_msg_list is set to TRUE.
7049 
7050   if FND_API.to_Boolean (p_init_msg_list) then
7051     FND_MSG_PUB.initialize;
7052   end if;
7053 
7054   /*For Bug No : 1808322 Start*/
7055   IF p_budget_revision_id IS NOT null THEN
7056   /*For Bug No : 1808322 End*/
7057   update PSB_POSITION_COSTS
7058      set element_cost = p_element_cost,
7059 	 last_update_date = sysdate,
7060 	 last_updated_by = FND_GLOBAL.USER_ID,
7061 	 last_update_login = FND_GLOBAL.LOGIN_ID
7062    where position_id = p_position_id
7063      and nvl(hr_budget_id, -1) = nvl(p_hr_budget_id, -1)
7064      and pay_element_id = p_pay_element_id
7065      and currency_code = p_currency_code
7066      and nvl(budget_revision_id, -1) = nvl(p_budget_revision_id, -1)
7067      and nvl(base_line_version, FND_API.G_MISS_CHAR) = nvl(p_base_line_version, FND_API.G_MISS_CHAR)
7068      and start_date = p_start_date
7069      and nvl(end_date, FND_API.G_MISS_DATE) = nvl(p_end_date, FND_API.G_MISS_DATE);
7070 
7071   /*For Bug No : 1808322 Start*/
7072     IF SQL%NOTFOUND THEN
7073       l_update_flag := FALSE;
7074     END IF;
7075 
7076   ELSE
7077       l_update_flag := FALSE;
7078   END IF;
7079 
7080   if not l_update_flag then --if SQL%NOTFOUND then
7081   /*For Bug No : 1808322 End*/
7082   begin
7083 
7084     for l_init_index in 1..g_costs.Count loop
7085       g_costs(l_init_index).position_element_line_id := null;
7086       g_costs(l_init_index).position_id := null;
7087       g_costs(l_init_index).pay_element_id := null;
7088       g_costs(l_init_index).start_date := null;
7089       g_costs(l_init_index).end_date := null;
7090       g_costs(l_init_index).currency_code := null;
7091       g_costs(l_init_index).element_cost := null;
7092       g_costs(l_init_index).budget_revision_id := null;
7093       g_costs(l_init_index).base_line_version := null;
7094       g_costs(l_init_index).delete_flag := null;
7095     end loop;
7096 
7097     g_num_costs := 0;
7098 
7099     -- for bug 4545590
7100     -- this performance improvement is only for budget upload and not
7101     -- for budget revisions. So we need to check if budget revision id is null
7102     IF NOT (g_wks_no_date_overlap AND p_budget_revision_id IS NULL) THEN
7103 
7104     for c_Overlap_Rec in c_Overlap loop
7105       g_num_costs := g_num_costs + 1;
7106 
7107       g_costs(g_num_costs).position_element_line_id := c_Overlap_Rec.position_element_line_id;
7108       g_costs(g_num_costs).position_id := c_Overlap_Rec.position_id;
7109       g_costs(g_num_costs).start_date := c_Overlap_Rec.start_date;
7110       g_costs(g_num_costs).end_date := c_Overlap_Rec.end_date;
7111       g_costs(g_num_costs).currency_code := c_Overlap_Rec.currency_code;
7112       g_costs(g_num_costs).element_cost := c_Overlap_Rec.element_cost;
7113       g_costs(g_num_costs).budget_revision_id := c_Overlap_Rec.budget_revision_id;
7114       g_costs(g_num_costs).base_line_version := c_Overlap_Rec.base_line_version;
7115       g_costs(g_num_costs).delete_flag := TRUE;
7116     end loop;
7117 
7118     END IF;
7119 
7120 
7121     if g_num_costs = 0 then
7122     begin
7123 
7124       Insert_Position_Costs
7125 	   (p_return_status => l_return_status,
7126 	    p_msg_count     => l_msg_count,
7127 	    p_msg_data      => l_msg_data,
7128 	    p_position_id => p_position_id,
7129 	    p_hr_budget_id => p_hr_budget_id,
7130 	    p_pay_element_id => p_pay_element_id,
7131 	    p_start_date => p_start_date,
7132 	    p_end_date => p_end_date,
7133 	    p_currency_code => p_currency_code,
7134 	    p_element_cost => p_element_cost,
7135 	    p_budget_revision_id => p_budget_revision_id,
7136 	    p_base_line_version => p_base_line_version);
7137 
7138 	if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7139 	  raise FND_API.G_EXC_ERROR;
7140 	end if;
7141 
7142     end;
7143     else
7144     begin
7145 
7146       for l_cost_index in 1..g_num_costs loop
7147 
7148 	l_updated_record := FALSE;
7149 
7150 	/* Effective Start Date Matches */
7151 
7152 	if g_costs(l_cost_index).start_date = p_start_date then
7153 	begin
7154 
7155 	  Update_Position_Costs
7156 		(p_return_status => l_return_status,
7157 	         p_msg_count     => l_msg_count,
7158 	         p_msg_data      => l_msg_data,
7159 		 p_position_element_line_id => g_costs(l_cost_index).position_element_line_id,
7160 		 p_end_date => p_end_date,
7161 		 p_element_cost => p_element_cost);
7162 
7163 	  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7164 	    raise FND_API.G_EXC_ERROR;
7165 	  end if;
7166 
7167 	  g_costs(l_cost_index).delete_flag := FALSE;
7168 
7169 	end;
7170 
7171 	/* Effective Dates Overlap */
7172 	elsif (((g_costs(l_cost_index).start_date <= (p_start_date - 1)) and
7173 	       ((g_costs(l_cost_index).end_date is null) or
7174 		(g_costs(l_cost_index).end_date > (p_start_date - 1)))) or
7175 	       ((g_costs(l_cost_index).start_date > p_start_date) and
7176 	       ((g_costs(l_cost_index).end_date is null) or
7177 		(g_costs(l_cost_index).end_date > (p_end_date + 1))))) then
7178 	begin
7179 
7180 	  if ((g_costs(l_cost_index).start_date < (p_start_date - 1)) and
7181 	     ((g_costs(l_cost_index).end_date is null) or
7182 	      (g_costs(l_cost_index).end_date > (p_start_date - 1)))) then
7183 	  begin
7184 
7185 	    Update_Position_Costs
7186 		  (p_return_status => l_return_status,
7187 	           p_msg_count     => l_msg_count,
7188 	           p_msg_data      => l_msg_data,
7189 		   p_position_element_line_id => g_costs(l_cost_index).position_element_line_id,
7190 		   p_end_date => p_start_date - 1,
7191 		   p_element_cost => Prorate (p_original_amount => g_costs(l_cost_index).element_cost,
7192 					      p_original_start_date => g_costs(l_cost_index).start_date,
7193 					      p_original_end_date => g_costs(l_cost_index).end_date,
7194 					      p_new_start_date => g_costs(l_cost_index).start_date,
7195 					      p_new_end_date => p_start_date - 1));
7196 
7197 	    if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7198 	      raise FND_API.G_EXC_ERROR;
7199 	    else
7200 	      l_updated_record := TRUE;
7201 	    end if;
7202 
7203 	    g_costs(l_cost_index).delete_flag := FALSE;
7204 
7205 	  end;
7206 	  elsif ((g_costs(l_cost_index).start_date > p_start_date) and
7207 		((p_end_date is not null) and
7208 		((g_costs(l_cost_index).end_date is null) or
7209 		 (g_costs(l_cost_index).end_date > (p_end_date + 1))))) then
7210 	  begin
7211 
7212 	    Update_Position_Costs
7213 		  (p_return_status => l_return_status,
7214 	           p_msg_count     => l_msg_count,
7215 	           p_msg_data      => l_msg_data,
7216 		   p_position_element_line_id => g_costs(l_cost_index).position_element_line_id,
7217 		   p_start_date => p_end_date + 1,
7218 		   p_element_cost => Prorate (p_original_amount => g_costs(l_cost_index).element_cost,
7219 					      p_original_start_date => g_costs(l_cost_index).start_date,
7220 					      p_original_end_date => g_costs(l_cost_index).end_date,
7221 					      p_new_start_date => p_end_date + 1,
7222 					      p_new_end_date => g_costs(l_cost_index).end_date));
7223 
7224 	    if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7225 	      raise FND_API.G_EXC_ERROR;
7226 	    else
7227 	      l_updated_record := FALSE;
7228 	    end if;
7229 
7230 	    g_costs(l_cost_index).delete_flag := FALSE;
7231 
7232 	  end;
7233 	  end if;
7234 
7235 	  if not l_created_record then
7236 	  begin
7237 
7238 	    Insert_Position_Costs
7239 		  (p_return_status => l_return_status,
7240 	           p_msg_count     => l_msg_count,
7241 	           p_msg_data      => l_msg_data,
7242 		   p_position_id => p_position_id,
7243 		   p_hr_budget_id => p_hr_budget_id,
7244 		   p_pay_element_id => p_pay_element_id,
7245 		   p_start_date => p_start_date,
7246 		   p_end_date => p_end_date,
7247 		   p_currency_code => p_currency_code,
7248 		   p_element_cost => p_element_cost,
7249 		   p_budget_revision_id => p_budget_revision_id,
7250 		   p_base_line_version => p_base_line_version);
7251 
7252 	    if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7253 	      raise FND_API.G_EXC_ERROR;
7254 	    else
7255 	      l_created_record := TRUE;
7256 	    end if;
7257 
7258 	  end;
7259 	  end if;
7260 
7261 	  if p_end_date is not null then
7262 	  begin
7263 
7264 	    if nvl(g_costs(l_cost_index).end_date, (p_end_date + 1)) > (p_end_date + 1) then
7265 	    begin
7266 
7267 	      if l_updated_record then
7268 	      begin
7269 
7270 		Insert_Position_Costs
7271 		      (p_return_status => l_return_status,
7272 	               p_msg_count     => l_msg_count,
7273 	               p_msg_data      => l_msg_data,
7274 		       p_position_id => g_costs(l_cost_index).position_id,
7275 		       p_hr_budget_id => p_hr_budget_id,
7276 		       p_pay_element_id => g_costs(l_cost_index).pay_element_id,
7277 		       p_start_date => p_end_date + 1,
7278 		       p_end_date => g_costs(l_cost_index).end_date,
7279 		       p_currency_code => g_costs(l_cost_index).currency_code,
7280 		       p_element_cost => Prorate (p_original_amount => g_costs(l_cost_index).element_cost,
7281 						  p_original_start_date => g_costs(l_cost_index).start_date,
7282 						  p_original_end_date => g_costs(l_cost_index).end_date,
7283 						  p_new_start_date => p_end_date + 1,
7284 						  p_new_end_date => g_costs(l_cost_index).end_date),
7285 		       p_budget_revision_id => g_costs(l_cost_index).budget_revision_id,
7286 		       p_base_line_version => g_costs(l_cost_index).base_line_version);
7287 
7288 		if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7289 		  raise FND_API.G_EXC_ERROR;
7290 		end if;
7291 
7292 	      end;
7293 	      else
7294 	      begin
7295 
7296 		Update_Position_Costs
7297 		      (p_return_status => l_return_status,
7298 	               p_msg_count     => l_msg_count,
7299 	               p_msg_data      => l_msg_data,
7300 		       p_position_element_line_id => g_costs(l_cost_index).position_element_line_id,
7301 		       p_start_date => p_end_date + 1,
7302 		       p_end_date => g_costs(l_cost_index).end_date,
7303 		       p_element_cost => Prorate (p_original_amount => g_costs(l_cost_index).element_cost,
7304 						  p_original_start_date => g_costs(l_cost_index).start_date,
7305 						  p_original_end_date => g_costs(l_cost_index).end_date,
7306 						  p_new_start_date => p_end_date + 1,
7307 						  p_new_end_date => g_costs(l_cost_index).end_date));
7308 
7309 		if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7310 		  raise FND_API.G_EXC_ERROR;
7311 		end if;
7312 
7313 		g_costs(l_cost_index).delete_flag := FALSE;
7314 
7315 	      end;
7316 	      end if;
7317 
7318 	    end;
7319 	    end if;
7320 
7321 	  end;
7322 	  end if;
7323 
7324 	end;
7325 	end if;
7326 
7327       end loop;
7328 
7329     end;
7330     end if;
7331 
7332     for l_cost_index in 1..g_num_costs loop
7333 
7334       if g_costs(l_cost_index).delete_flag then
7335       begin
7336 
7337 	Delete_Position_Costs
7338 	      (p_return_status => l_return_status,
7339 	       p_msg_count     => l_msg_count,
7340 	       p_msg_data      => l_msg_data,
7341 	       p_position_element_line_id => g_costs(l_cost_index).position_element_line_id);
7342 
7343 	if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7344 	  raise FND_API.G_EXC_ERROR;
7345 	end if;
7346 
7347       end;
7348       end if;
7349 
7350     end loop;
7351 
7352   end;
7353   end if;
7354 
7355   -- Standard check of p_commit.
7356   if FND_API.to_Boolean (p_commit) then
7357     commit work;
7358   end if;
7359 
7360   -- Standard call to get message count and if count is 1, get message info.
7361   FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
7362 			     p_data  => p_msg_data);
7363 
7364   -- Initialize API return status to success
7365   p_return_status := FND_API.G_RET_STS_SUCCESS;
7366 
7367 EXCEPTION
7368 
7369    when FND_API.G_EXC_ERROR then
7370      p_return_status := FND_API.G_RET_STS_ERROR;
7371 
7372      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
7373 				p_data  => p_msg_data);
7374 
7375    when FND_API.G_EXC_UNEXPECTED_ERROR then
7376      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7377 
7378      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
7379 				p_data  => p_msg_data);
7380 
7381    when OTHERS then
7382      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7383 
7384      if FND_MSG_PUB.Check_Msg_Level
7385        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
7386        FND_MSG_PUB.Add_Exc_Msg
7387 	  (p_pkg_name => G_PKG_NAME,
7388 	   p_procedure_name => l_api_name);
7389      end if;
7390 
7391      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
7392 				p_data  => p_msg_data);
7393 
7394 END Modify_Position_Costs;
7395 
7396 /* ----------------------------------------------------------------------- */
7397 
7398 PROCEDURE Insert_Position_FTE
7399 ( p_return_status          OUT  NOCOPY  VARCHAR2,
7400   p_msg_count              OUT  NOCOPY  NUMBER,
7401   p_msg_data               OUT  NOCOPY  VARCHAR2,
7402   p_position_id            IN   NUMBER,
7403   p_hr_budget_id           IN   NUMBER,
7404   p_budget_revision_id     IN   NUMBER,
7405   p_base_line_version      IN   VARCHAR2,
7406   p_start_date             IN   DATE,
7407   p_end_date               IN   DATE,
7408   p_fte                    IN   NUMBER
7409 ) IS
7410 
7411   l_api_name               CONSTANT VARCHAR2(30)   := 'Insert_Position_FTE';
7412 
7413   l_position_fte_line_id   NUMBER;
7414 
7415 BEGIN
7416 
7417 
7418   insert into PSB_POSITION_FTE
7419 	(position_fte_line_id, position_id, hr_budget_id, budget_revision_id, base_line_version,
7420 	 start_date, end_date, fte, last_update_date, last_updated_by, last_update_login,
7421 	 created_by, creation_date)
7422    VALUES (PSB_POSITION_FTE_S.NEXTVAL, p_position_id, p_hr_budget_id, p_budget_revision_id, p_base_line_version,
7423 	   p_start_date, p_end_date, nvl(p_fte, 0), sysdate, FND_GLOBAL.USER_ID, FND_GLOBAL.LOGIN_ID,
7424 	   FND_GLOBAL.USER_ID, sysdate) RETURNING position_fte_line_id INTO l_position_fte_line_id;
7425 
7426   -- Initialize API return status to success
7427   p_return_status := FND_API.G_RET_STS_SUCCESS;
7428 
7429 EXCEPTION
7430 
7431    when FND_API.G_EXC_ERROR then
7432      p_return_status := FND_API.G_RET_STS_ERROR;
7433 
7434      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
7435 				p_data  => p_msg_data);
7436 
7437    when FND_API.G_EXC_UNEXPECTED_ERROR then
7438      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7439 
7440      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
7441 				p_data  => p_msg_data);
7442 
7443    when OTHERS then
7444      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7445 
7446      if FND_MSG_PUB.Check_Msg_Level
7447        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
7448        FND_MSG_PUB.Add_Exc_Msg
7449 	  (p_pkg_name => G_PKG_NAME,
7450 	   p_procedure_name => l_api_name);
7451      end if;
7452 
7453      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
7454 				p_data  => p_msg_data);
7455 
7456 END Insert_Position_FTE;
7457 
7458 /* ----------------------------------------------------------------------- */
7459 
7460 PROCEDURE Update_Position_FTE
7461 ( p_return_status         OUT  NOCOPY  VARCHAR2,
7462   p_msg_count             OUT  NOCOPY  NUMBER,
7463   p_msg_data              OUT  NOCOPY  VARCHAR2,
7464   p_position_fte_line_id  IN   NUMBER,
7465   p_start_date            IN   DATE := FND_API.G_MISS_DATE,
7466   p_end_date              IN   DATE := FND_API.G_MISS_DATE,
7467   p_fte                   IN   NUMBER
7468 ) IS
7469 
7470   l_api_name               CONSTANT VARCHAR2(30)   := 'Update_Position_FTE';
7471 
7472 BEGIN
7473 
7474   update PSB_POSITION_FTE
7475      set fte = nvl(p_fte, 0),
7476 	 start_date = decode(p_start_date, FND_API.G_MISS_DATE, start_date, p_start_date),
7477 	 end_date = decode(p_end_date, FND_API.G_MISS_DATE, end_date, p_end_date),
7478 	 last_update_date = sysdate,
7479 	 last_updated_by = FND_GLOBAL.USER_ID,
7480 	 last_update_login = FND_GLOBAL.LOGIN_ID
7481     where position_fte_line_id = p_position_fte_line_id;
7482 
7483   -- Initialize API return status to success
7484   p_return_status := FND_API.G_RET_STS_SUCCESS;
7485 
7486 EXCEPTION
7487 
7488    when FND_API.G_EXC_ERROR then
7489      p_return_status := FND_API.G_RET_STS_ERROR;
7490 
7491      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
7492 				p_data  => p_msg_data);
7493 
7494    when FND_API.G_EXC_UNEXPECTED_ERROR then
7495      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7496 
7497      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
7498 				p_data  => p_msg_data);
7499 
7500    when OTHERS then
7501      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7502 
7503      if FND_MSG_PUB.Check_Msg_Level
7504        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
7505        FND_MSG_PUB.Add_Exc_Msg
7506 	  (p_pkg_name => G_PKG_NAME,
7507 	   p_procedure_name => l_api_name);
7508      end if;
7509 
7510      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
7511 				p_data  => p_msg_data);
7512 
7513 END Update_Position_FTE;
7514 
7515 /* ----------------------------------------------------------------------- */
7516 
7517 PROCEDURE Delete_Position_FTE
7518 ( p_return_status         OUT  NOCOPY  VARCHAR2,
7519   p_msg_count             OUT  NOCOPY  NUMBER,
7520   p_msg_data              OUT  NOCOPY  VARCHAR2,
7521   p_position_fte_line_id  IN   NUMBER
7522 ) IS
7523 
7524   l_api_name               CONSTANT VARCHAR2(30)   := 'Delete_Position_FTE';
7525 
7526 BEGIN
7527 
7528   delete from PSB_POSITION_FTE
7529    where position_fte_line_id = p_position_fte_line_id;
7530 
7531   -- Initialize API return status to success
7532   p_return_status := FND_API.G_RET_STS_SUCCESS;
7533 
7534 EXCEPTION
7535 
7536    when FND_API.G_EXC_ERROR then
7537      p_return_status := FND_API.G_RET_STS_ERROR;
7538 
7539      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
7540 				p_data  => p_msg_data);
7541 
7542    when FND_API.G_EXC_UNEXPECTED_ERROR then
7543      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7544 
7545      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
7546 				p_data  => p_msg_data);
7547 
7548    when OTHERS then
7549      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7550 
7551      if FND_MSG_PUB.Check_Msg_Level
7552        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
7553        FND_MSG_PUB.Add_Exc_Msg
7554 	  (p_pkg_name => G_PKG_NAME,
7555 	   p_procedure_name => l_api_name);
7556      end if;
7557 
7558      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
7559 				p_data  => p_msg_data);
7560 
7561 END Delete_Position_FTE;
7562 
7563 /* ----------------------------------------------------------------------- */
7564 
7565 PROCEDURE Modify_Position_FTE
7566 ( p_api_version            IN   NUMBER,
7567   p_init_msg_list          IN   VARCHAR2 := FND_API.G_FALSE,
7568   p_commit                 IN   VARCHAR2 := FND_API.G_FALSE,
7569   p_validation_level       IN   NUMBER := FND_API.G_VALID_LEVEL_NONE,
7570   p_return_status          OUT  NOCOPY  VARCHAR2,
7571   p_msg_count              OUT  NOCOPY  NUMBER,
7572   p_msg_data               OUT  NOCOPY  VARCHAR2,
7573   p_position_id            IN   NUMBER,
7574   p_hr_budget_id           IN   NUMBER,
7575   p_budget_revision_id     IN   NUMBER,
7576   p_base_line_version      IN   VARCHAR2,
7577   p_start_date             IN   DATE,
7578   p_end_date               IN   DATE,
7579   p_fte                    IN   NUMBER
7580 ) IS
7581 
7582   l_api_name               CONSTANT VARCHAR2(30)   := 'Modify_Position_FTE';
7583   l_api_version            CONSTANT NUMBER         := 1.0;
7584 
7585   l_msg_count              NUMBER;
7586   l_msg_data               VARCHAR2(2000);
7587 
7588   l_created_record         BOOLEAN := FALSE;
7589   l_updated_record         BOOLEAN;
7590   /*For Bug No : 1808322 Start*/
7591   l_update_flag           BOOLEAN := TRUE;
7592   /*For Bug No : 1808322 End*/
7593 
7594   l_return_status          VARCHAR2(1);
7595 
7596   cursor c_Overlap is
7597     select *
7598       from PSB_POSITION_FTE
7599      where position_id = p_position_id
7600        and nvl(hr_budget_id, -1) = nvl(p_hr_budget_id, -1)
7601        and nvl(budget_revision_id, -1) = nvl(p_budget_revision_id, -1)
7602        and nvl(base_line_version, FND_API.G_MISS_CHAR) = nvl(p_base_line_version, FND_API.G_MISS_CHAR)
7603        and ((((p_end_date is not null)
7604 	 and ((start_date <= p_end_date)
7605 	  and (end_date is null))
7606 	  or ((start_date between p_start_date and p_end_date)
7607 	   or (end_date between p_start_date and p_end_date)
7608 	  or ((start_date < p_start_date)
7609 	  and (end_date > p_end_date)))))
7610 	  or ((p_end_date is null)
7611 	  and (nvl(end_date, p_start_date) >= p_start_date)));
7612 
7613 BEGIN
7614 
7615   -- Standard call to check for call compatibility.
7616 
7617   if not FND_API.Compatible_API_Call (l_api_version,
7618 				      p_api_version,
7619 				      l_api_name,
7620 				      G_PKG_NAME)
7621   then
7622     raise FND_API.G_EXC_UNEXPECTED_ERROR;
7623   end if;
7624 
7625   -- Initialize message list if p_init_msg_list is set to TRUE.
7626 
7627   if FND_API.to_Boolean (p_init_msg_list) then
7628     FND_MSG_PUB.initialize;
7629   end if;
7630 
7631   /*For Bug No : 1808322 Start*/
7632   IF p_budget_revision_id IS NOT null  THEN
7633   /*For Bug No : 1808322 End*/
7634 
7635   update PSB_POSITION_FTE
7636      set fte = nvl(p_fte, 0),
7637 	 last_update_date = sysdate,
7638 	 last_updated_by = FND_GLOBAL.USER_ID,
7639 	 last_update_login = FND_GLOBAL.LOGIN_ID
7640    where position_id = p_position_id
7641      and nvl(hr_budget_id, -1) = nvl(p_hr_budget_id, -1)
7642      and nvl(budget_revision_id, -1) = nvl(p_budget_revision_id, -1)
7643      and nvl(base_line_version, FND_API.G_MISS_CHAR) = nvl(p_base_line_version, FND_API.G_MISS_CHAR)
7644      and start_date = p_start_date
7645      and nvl(end_date, FND_API.G_MISS_DATE) = nvl(p_end_date, FND_API.G_MISS_DATE);
7646 
7647   /*For Bug No : 1808322 Start*/
7648     IF SQL%NOTFOUND THEN
7649       l_update_flag := FALSE;
7650     END IF;
7651 
7652   ELSE
7653       l_update_flag := FALSE;
7654   END IF;
7655 
7656   if not l_update_flag then --if SQL%NOTFOUND then
7657   /*For Bug No : 1808322 End*/
7658   begin
7659 
7660     for l_init_index in 1..g_fte.Count loop
7661       g_fte(l_init_index).position_fte_line_id := null;
7662       g_fte(l_init_index).position_id := null;
7663       g_fte(l_init_index).start_date := null;
7664       g_fte(l_init_index).end_date := null;
7665       g_fte(l_init_index).fte := null;
7666       g_fte(l_init_index).budget_revision_id := null;
7667       g_fte(l_init_index).base_line_version := null;
7668       g_fte(l_init_index).delete_flag := null;
7669     end loop;
7670 
7671     g_num_fte := 0;
7672 
7673     -- for bug 4545590
7674     -- this performance improvement is only for budget upload and not
7675     -- for budget revisions. So we need to check if budget revision id is null
7676     IF NOT (g_wks_no_date_overlap AND p_budget_revision_id IS NULL) THEN
7677 
7678     for c_Overlap_Rec in c_Overlap loop
7679       g_num_fte := g_num_fte + 1;
7680 
7681       g_fte(g_num_fte).position_fte_line_id := c_Overlap_Rec.position_fte_line_id;
7682       g_fte(g_num_fte).position_id := c_Overlap_Rec.position_id;
7683       g_fte(g_num_fte).start_date := c_Overlap_Rec.start_date;
7684       g_fte(g_num_fte).end_date := c_Overlap_Rec.end_date;
7685       g_fte(g_num_fte).budget_revision_id := c_Overlap_Rec.budget_revision_id;
7686       g_fte(g_num_fte).base_line_version := c_Overlap_Rec.base_line_version;
7687       g_fte(g_num_fte).delete_flag := TRUE;
7688     end loop;
7689 
7690     END IF;
7691 
7692     if g_num_fte = 0 then
7693     begin
7694 
7695       Insert_Position_FTE
7696 	   (p_return_status => l_return_status,
7697 	    p_msg_count     => l_msg_count,
7698 	    p_msg_data      => l_msg_data,
7699 	    p_position_id   => p_position_id,
7700 	    p_hr_budget_id  => p_hr_budget_id,
7701 	    p_start_date    => p_start_date,
7702 	    p_end_date      => p_end_date,
7703 	    p_fte           => p_fte,
7704 	    p_budget_revision_id => p_budget_revision_id,
7705 	    p_base_line_version  => p_base_line_version);
7706 
7707 	if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7708 	  raise FND_API.G_EXC_ERROR;
7709 	end if;
7710 
7711     end;
7712     else
7713     begin
7714 
7715       for l_fte_index in 1..g_num_fte loop
7716 
7717 	l_updated_record := FALSE;
7718 
7719 	/* Effective Start Date Matches */
7720 
7721 	if g_fte(l_fte_index).start_date = p_start_date then
7722 	begin
7723 
7724 	  Update_Position_FTE
7725 		(p_return_status => l_return_status,
7726 	         p_msg_count     => l_msg_count,
7727 	         p_msg_data      => l_msg_data,
7728 		 p_position_fte_line_id => g_fte(l_fte_index).position_fte_line_id,
7729 		 p_end_date => p_end_date,
7730 		 p_fte => p_fte);
7731 
7732 	  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7733 	    raise FND_API.G_EXC_ERROR;
7734 	  end if;
7735 
7736 	  g_fte(l_fte_index).delete_flag := FALSE;
7737 
7738 	end;
7739 
7740 	/* Effective Dates Overlap */
7741 	elsif (((g_fte(l_fte_index).start_date <= (p_start_date - 1)) and
7742 	       ((g_fte(l_fte_index).end_date is null) or
7743 		(g_fte(l_fte_index).end_date > (p_start_date - 1)))) or
7744 	       ((g_fte(l_fte_index).start_date > p_start_date) and
7745 	       ((g_fte(l_fte_index).end_date is null) or
7746 		(g_fte(l_fte_index).end_date > (p_end_date + 1))))) then
7747 	begin
7748 
7749 	  if ((g_fte(l_fte_index).start_date < (p_start_date - 1)) and
7750 	     ((g_fte(l_fte_index).end_date is null) or
7751 	      (g_fte(l_fte_index).end_date > (p_start_date - 1)))) then
7752 	  begin
7753 
7754 	    Update_Position_FTE
7755 		  (p_return_status => l_return_status,
7756 	           p_msg_count     => l_msg_count,
7757 	           p_msg_data      => l_msg_data,
7758 		   p_position_fte_line_id => g_fte(l_fte_index).position_fte_line_id,
7759 		   p_end_date => p_start_date - 1,
7760 		   p_fte => g_fte(l_fte_index).fte);
7761 
7762 	    if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7763 	      raise FND_API.G_EXC_ERROR;
7764 	    else
7765 	      l_updated_record := TRUE;
7766 	    end if;
7767 
7768 	    g_fte(l_fte_index).delete_flag := FALSE;
7769 
7770 	  end;
7771 	  elsif ((g_fte(l_fte_index).start_date > p_start_date) and
7772 		((p_end_date is not null) and
7773 		((g_fte(l_fte_index).end_date is null) or
7774 		 (g_fte(l_fte_index).end_date > (p_end_date + 1))))) then
7775 	  begin
7776 
7777 	    Update_Position_FTE
7778 		  (p_return_status => l_return_status,
7779 	           p_msg_count     => l_msg_count,
7780 	           p_msg_data      => l_msg_data,
7781 		   p_position_fte_line_id => g_fte(l_fte_index).position_fte_line_id,
7782 		   p_start_date => p_end_date + 1,
7783 		   p_fte => g_fte(l_fte_index).fte);
7784 
7785 	    if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7786 	      raise FND_API.G_EXC_ERROR;
7787 	    else
7788 	      l_updated_record := FALSE;
7789 	    end if;
7790 
7791 	    g_fte(l_fte_index).delete_flag := FALSE;
7792 
7793 	  end;
7794 	  end if;
7795 
7796 	  if not l_created_record then
7797 	  begin
7798 
7799 	    Insert_Position_FTE
7800 		  (p_return_status => l_return_status,
7801 	           p_msg_count     => l_msg_count,
7802 	           p_msg_data      => l_msg_data,
7803 		   p_position_id => p_position_id,
7804 		   p_hr_budget_id => p_hr_budget_id,
7805 		   p_start_date => p_start_date,
7806 		   p_end_date => p_end_date,
7807 		   p_fte => p_fte,
7808 		   p_budget_revision_id => p_budget_revision_id,
7809 		   p_base_line_version => p_base_line_version);
7810 
7811 	    if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7812 	      raise FND_API.G_EXC_ERROR;
7813 	    else
7814 	      l_created_record := TRUE;
7815 	    end if;
7816 
7817 	  end;
7818 	  end if;
7819 
7820 	  if p_end_date is not null then
7821 	  begin
7822 
7823 	    if nvl(g_fte(l_fte_index).end_date, (p_end_date + 1)) > (p_end_date + 1) then
7824 	    begin
7825 
7826 	      if l_updated_record then
7827 	      begin
7828 
7829 		Insert_Position_FTE
7830 		      (p_return_status => l_return_status,
7831 	               p_msg_count     => l_msg_count,
7832 	               p_msg_data      => l_msg_data,
7833 		       p_position_id => g_fte(l_fte_index).position_id,
7834 		       p_hr_budget_id => p_hr_budget_id,
7835 		       p_start_date => p_end_date + 1,
7836 		       p_end_date => g_fte(l_fte_index).end_date,
7837 		       p_fte => g_fte(l_fte_index).fte,
7838 		       p_budget_revision_id => g_fte(l_fte_index).budget_revision_id,
7839 		       p_base_line_version => g_fte(l_fte_index).base_line_version);
7840 
7841 		if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7842 		  raise FND_API.G_EXC_ERROR;
7843 		end if;
7844 
7845 	      end;
7846 	      else
7847 	      begin
7848 
7849 		Update_Position_FTE
7850 		      (p_return_status => l_return_status,
7851 	               p_msg_count     => l_msg_count,
7852 	               p_msg_data      => l_msg_data,
7853 		       p_position_fte_line_id => g_fte(l_fte_index).position_fte_line_id,
7854 		       p_start_date => p_end_date + 1,
7855 		       p_end_date => g_fte(l_fte_index).end_date,
7856 		       p_fte => g_fte(l_fte_index).fte);
7857 
7858 		if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7859 		  raise FND_API.G_EXC_ERROR;
7860 		end if;
7861 
7862 		g_fte(l_fte_index).delete_flag := FALSE;
7863 
7864 	      end;
7865 	      end if;
7866 
7867 	    end;
7868 	    end if;
7869 
7870 	  end;
7871 	  end if;
7872 
7873 	end;
7874 	end if;
7875 
7876       end loop;
7877 
7878     end;
7879     end if;
7880 
7881     for l_fte_index in 1..g_num_fte loop
7882 
7883       if g_fte(l_fte_index).delete_flag then
7884       begin
7885 
7886 	Delete_Position_FTE
7887 	      (p_return_status => l_return_status,
7888 	       p_msg_count     => l_msg_count,
7889 	       p_msg_data      => l_msg_data,
7890 	       p_position_fte_line_id => g_fte(l_fte_index).position_fte_line_id);
7891 
7892 	if l_return_status <> FND_API.G_RET_STS_SUCCESS then
7893 	  raise FND_API.G_EXC_ERROR;
7894 	end if;
7895 
7896       end;
7897       end if;
7898 
7899     end loop;
7900 
7901   end;
7902   end if;
7903 
7904   -- Standard check of p_commit.
7905   if FND_API.to_Boolean (p_commit) then
7906     commit work;
7907   end if;
7908 
7909   -- Standard call to get message count and if count is 1, get message info.
7910   FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
7911 			     p_data  => p_msg_data);
7912 
7913   -- Initialize API return status to success
7914   p_return_status := FND_API.G_RET_STS_SUCCESS;
7915 
7916 EXCEPTION
7917 
7918    when FND_API.G_EXC_ERROR then
7919      p_return_status := FND_API.G_RET_STS_ERROR;
7920 
7921      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
7922 				p_data  => p_msg_data);
7923 
7924    when FND_API.G_EXC_UNEXPECTED_ERROR then
7925      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7926 
7927      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
7928 				p_data  => p_msg_data);
7929 
7930    when OTHERS then
7931      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7932 
7933      if FND_MSG_PUB.Check_Msg_Level
7934        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
7935        FND_MSG_PUB.Add_Exc_Msg
7936 	  (p_pkg_name => G_PKG_NAME,
7937 	   p_procedure_name => l_api_name);
7938      end if;
7939 
7940      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
7941 				p_data  => p_msg_data);
7942 
7943 END Modify_Position_FTE;
7944 
7945 /* ----------------------------------------------------------------------- */
7946 
7947 -- Check that every PSB budget group is mapped to an HR organization
7948 
7949 PROCEDURE Validate_Budget_Group
7950 ( p_return_status    OUT  NOCOPY  VARCHAR2,
7951   p_msg_count        OUT  NOCOPY  NUMBER,
7952   p_msg_data         OUT  NOCOPY  VARCHAR2,
7953   p_event_type       IN   VARCHAR2,
7954   p_source_id        IN   NUMBER,
7955   p_budget_group_id  IN   NUMBER) IS
7956 
7957   -- Select all Budget Groups in the Budget Group Hierarchy; 'connect by' does a
7958   -- depth-first search
7959 
7960   cursor c_BG is
7961     select short_name, nvl(organization_id, business_group_id) organization_id,
7962 	   effective_start_date, effective_end_date
7963       from PSB_BUDGET_GROUPS
7964      where budget_group_type = 'R'
7965      start with budget_group_id = p_budget_group_id
7966    connect by prior budget_group_id = parent_budget_group_id;
7967 
7968   l_org_notmapped    BOOLEAN := FALSE;
7969 
7970   l_api_name               CONSTANT VARCHAR2(30)   := 'Validate_Budget_Group';
7971 
7972 BEGIN
7973 
7974   for c_BG_Rec in c_BG loop
7975 
7976     if ((p_event_type = 'BR') or
7977        ((p_event_type = 'BP') and ((c_BG_Rec.effective_start_date <= PSB_WS_ACCT1.g_startdate_pp)
7978 			       and (c_BG_Rec.effective_end_date is null or c_BG_Rec.effective_end_date >= PSB_WS_ACCT1.g_enddate_cy)))) then
7979       if c_BG_Rec.organization_id is null then
7980 	message_token('BUDGET_GROUP', c_BG_Rec.short_name);
7981 	add_message('PSB', 'PSB_PQH_NO_ORG_MAPPING');
7982 	l_org_notmapped := TRUE;
7983       end if;
7984 
7985     end if;
7986 
7987   end loop;
7988 
7989   if l_org_notmapped then
7990      FND_MESSAGE.SET_NAME('PSB','PSB_DEBUG_MESSAGE');
7991      FND_MESSAGE.SET_TOKEN('MESSAGE', ' Org Mapping Failed');
7992      FND_MSG_PUB.Add;
7993     raise FND_API.G_EXC_ERROR;
7994   end if;
7995 
7996   -- Initialize API return status to success
7997   p_return_status := FND_API.G_RET_STS_SUCCESS;
7998 
7999 EXCEPTION
8000 
8001    when FND_API.G_EXC_ERROR then
8002      p_return_status := FND_API.G_RET_STS_ERROR;
8003 
8004      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
8005 				p_data  => p_msg_data);
8006 
8007    when FND_API.G_EXC_UNEXPECTED_ERROR then
8008      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8009 
8010      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
8011 				p_data  => p_msg_data);
8012 
8013    when OTHERS then
8014      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8015 
8016      if FND_MSG_PUB.Check_Msg_Level
8017        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
8018        FND_MSG_PUB.Add_Exc_Msg
8019 	  (p_pkg_name => G_PKG_NAME,
8020 	   p_procedure_name => l_api_name);
8021      end if;
8022 
8023      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
8024 				p_data  => p_msg_data);
8025 
8026 END Validate_Budget_Group;
8027 
8028 /* ----------------------------------------------------------------------- */
8029 
8030 -- check that HRMS budget periods are more granular than PSB budget periods
8031 
8032 PROCEDURE Validate_Period_Granularity
8033 ( p_return_status        OUT  NOCOPY  VARCHAR2,
8034   p_msg_count            OUT  NOCOPY  NUMBER,
8035   p_msg_data             OUT  NOCOPY  VARCHAR2,
8036   p_hr_budget_id         IN   NUMBER,
8037   p_from_budget_year_id  IN   NUMBER,
8038   p_to_budget_year_id    IN   NUMBER) IS
8039 
8040   l_hrms_period_type          VARCHAR2(30);
8041   l_hrms_period               VARCHAR2(30);
8042   l_psb_period_type           VARCHAR2(10);
8043   /*For Bug No : 2556899 Start*/
8044   --Changed the folloiwng field size from 30 to 80
8045   l_psb_period                VARCHAR2(80);
8046   /*For Bug No : 2556899 End*/
8047   l_period_type_mismatch      BOOLEAN := FALSE;
8048 
8049   cursor c_hrms_period_type is
8050     select pc.proc_period_type
8051       from pqh_psb_budgets pb, pay_calendars pc
8052      where pb.budget_id = p_hr_budget_id
8053        and pc.period_set_name = pb.period_set_name;
8054 
8055   cursor c_psb_period_type is
8056     select min(period_distribution_type) psb_period_type
8057       from psb_budget_periods
8058      where budget_period_id between p_from_budget_year_id and p_to_budget_year_id
8059        and budget_period_type = 'Y';
8060 
8061   cursor c_psb_period is
8062     select meaning from fnd_lookups
8063      where lookup_type = 'PSB_PERIOD_DISTRIBUTION_TYPES'
8064        and lookup_code = l_psb_period_type;
8065 
8066   cursor c_hrms_period is
8067     select tpt.display_period_type
8068       from per_time_period_types tpt, per_time_period_rules tpr
8069      where tpr.number_per_fiscal_year = tpt.number_per_fiscal_year
8070        and tpr.proc_period_type = l_hrms_period_type;
8071 
8072   l_api_name               CONSTANT VARCHAR2(30)   := 'Validate_Period_Granularity';
8073 
8074 BEGIN
8075 
8076   for c_hrms_period_type_rec in c_hrms_period_type loop
8077     l_hrms_period_type := c_hrms_period_type_rec.proc_period_type;
8078   end loop;
8079 
8080   for c_psb_period_type_rec in c_psb_period_type loop
8081     l_psb_period_type := c_psb_period_type_rec.psb_period_type;
8082   end loop;
8083 
8084   if l_psb_period_type = 'M' then
8085     if l_hrms_period_type not in ('F', 'CM', 'LM', 'SM', 'W') then
8086       l_period_type_mismatch := TRUE;
8087     end if;
8088   elsif l_psb_period_type = 'Q' then
8089     if l_hrms_period_type not in ('BM', 'F', 'CM', 'LM', 'Q', 'SM', 'W') then
8090       l_period_type_mismatch := TRUE;
8091     end if;
8092   elsif l_psb_period_type = 'S' then
8093     if l_hrms_period_type not in ('BM', 'F', 'CM', 'LM', 'Q', 'SM', 'SY', 'W') then
8094       l_period_type_mismatch := TRUE;
8095     end if;
8096   elsif l_psb_period_type = 'Y' then
8097     if l_hrms_period_type not in ('BM', 'F', 'CM', 'LM', 'Q', 'SM', 'SY', 'W', 'Y') then
8098       l_period_type_mismatch := TRUE;
8099     end if;
8100   end if;
8101 
8102   if l_period_type_mismatch then
8103   begin
8104 
8105     for c_hrms_period_rec in c_hrms_period loop
8106       l_hrms_period := c_hrms_period_rec.display_period_type;
8107     end loop;
8108 
8109     for c_psb_period_rec in c_psb_period loop
8110       l_psb_period := c_psb_period_rec.meaning;
8111     end loop;
8112 
8113     message_token('PSB_PERIOD_TYPE', l_psb_period);
8114     message_token('HRMS_PERIOD_TYPE', l_hrms_period);
8115     add_message('PSB', 'PSB_PQH_PERIOD_TYPE_MISMATCH');
8116     raise FND_API.G_EXC_ERROR;
8117 
8118   end;
8119   end if;
8120 
8121   -- Initialize API return status to success
8122   p_return_status := FND_API.G_RET_STS_SUCCESS;
8123 
8124 EXCEPTION
8125 
8126    when FND_API.G_EXC_ERROR then
8127      p_return_status := FND_API.G_RET_STS_ERROR;
8128 
8129      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
8130 				p_data  => p_msg_data);
8131 
8132    when FND_API.G_EXC_UNEXPECTED_ERROR then
8133      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8134 
8135      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
8136 				p_data  => p_msg_data);
8137 
8138    when OTHERS then
8139      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8140 
8141      if FND_MSG_PUB.Check_Msg_Level
8142        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
8143        FND_MSG_PUB.Add_Exc_Msg
8144 	  (p_pkg_name => G_PKG_NAME,
8145 	   p_procedure_name => l_api_name);
8146      end if;
8147 
8148      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
8149 				p_data  => p_msg_data);
8150 
8151 END Validate_Period_Granularity;
8152 
8153 /* ----------------------------------------------------------------------- */
8154 
8155 -- Check if element exists in Worksheet or Budget Revision
8156 
8157 FUNCTION Element_Exists
8158 ( p_event_type       VARCHAR2,
8159   p_source_id        NUMBER,
8160   p_data_extract_id  NUMBER,
8161   p_element_id       NUMBER) RETURN BOOLEAN IS
8162 
8163   l_element_id       NUMBER;
8164   l_element_exists   BOOLEAN := FALSE;
8165 
8166   cursor c_re_element is
8167     select b.pay_element_id
8168       from PSB_PAY_ELEMENTS a,
8169 	   PSB_PAY_ELEMENTS b
8170      where a.pay_element_id = p_element_id
8171        and b.data_extract_id = p_data_extract_id
8172        and b.name = a.name;
8173 
8174   cursor c_element_exists_bp is
8175     select 'Exists'
8176       from dual
8177      where exists
8178 	  (select 'Exists'
8179 	     from psb_ws_lines_positions wlp, psb_ws_position_lines wpl, psb_position_assignments ppa
8180 	    where wlp.worksheet_id = p_source_id
8181 	      and wpl.position_line_id = wlp.position_line_id
8182 	      and ppa.position_id = wpl.position_id
8183 	      and ppa.pay_element_id = l_element_id
8184 	   /* For Bug No. 2599262 : Start */
8185 	      and (ppa.worksheet_id = p_source_id OR ppa.worksheet_id IS NULL)
8186 	   /* For Bug No. 2599262 : End */
8187 	   );
8188 
8189   cursor c_element_exists_br is
8190     select 'Exists'
8191       from dual
8192      where exists
8193 	  (select 'Exists'
8194 	     from psb_budget_revision_pos_lines brpl, psb_budget_revision_positions brp, psb_position_assignments ppa
8195 	    where brpl.budget_revision_id = p_source_id
8196 	      and brp.budget_revision_pos_line_id = brpl.budget_revision_pos_line_id
8197 	      and ppa.position_id = brp.position_id
8198 	      and ppa.pay_element_id = p_element_id
8199 	   /* For Bug No. 2599262 : Start */
8200 	      and (ppa.worksheet_id = p_source_id OR ppa.worksheet_id IS NULL)
8201 	   /* For Bug No. 2599262 : End */
8202 	   );
8203 
8204 BEGIN
8205 
8206   if p_event_type = 'BP' then
8207   begin
8208 
8209     for c_re_element_rec in c_re_element loop
8210       l_element_id := c_re_element_rec.pay_element_id;
8211     end loop;
8212 
8213     for c_element_rec in c_element_exists_bp loop
8214       l_element_exists := TRUE;
8215     end loop;
8216 
8217   end;
8218   elsif p_event_type = 'BR' then
8219   begin
8220 
8221     for c_element_rec in c_element_exists_br loop
8222       l_element_exists := TRUE;
8223     end loop;
8224 
8225   end;
8226   end if;
8227 
8228   return l_element_exists;
8229 
8230 END Element_Exists;
8231 
8232 /* ----------------------------------------------------------------------- */
8233 
8234 -- Get payroll_id for HRMS position
8235 
8236 FUNCTION Get_Payroll
8237 ( p_hr_position_id        NUMBER,
8238   p_data_extract_id       NUMBER,
8239   p_effective_start_date  DATE,
8240   p_effective_end_date    DATE) RETURN NUMBER IS
8241 
8242   l_payroll_id            NUMBER;
8243 
8244   cursor c_position_payroll is
8245     select pay_freq_payroll_id
8246       from hr_all_positions_f
8247      where position_id = p_hr_position_id
8248        /*For Bug No : 2292003 Start*/
8249        --and p_effective_start_date between effective_start_date and effective_end_date
8250        and ((p_effective_start_date between effective_start_date and effective_end_date) OR
8251 	    (p_effective_end_date between effective_start_date and effective_end_date)
8252 	   );
8253        /*For Bug No : 2292003 End*/
8254 
8255   cursor c_assign_payroll is
8256     select payroll_id
8257       from per_all_assignments_f
8258      where position_id = p_hr_position_id
8259        and person_id in
8260 	  (select hr_employee_id from PSB_POSITIONS
8261 	    where hr_position_id = p_hr_position_id
8262 	      and data_extract_id = p_data_extract_id)
8263        /*For Bug No : 2292003 Start*/
8264        --and p_effective_start_date between effective_start_date and effective_end_date
8265        and ((p_effective_start_date between effective_start_date and effective_end_date) OR
8266 	    (p_effective_end_date between effective_start_date and effective_end_date)
8267 	   )
8268        /*For Bug No : 2292003 End*/
8269        and assignment_type = 'E';
8270 
8271 BEGIN
8272 
8273   for c_payroll_rec in c_position_payroll loop
8274     l_payroll_id := c_payroll_rec.pay_freq_payroll_id;
8275   end loop;
8276 
8277   if l_payroll_id is null then
8278   begin
8279 
8280     for c_payroll_rec in c_assign_payroll loop
8281       l_payroll_id := c_payroll_rec.payroll_id;
8282     end loop;
8283 
8284   end;
8285   end if;
8286 
8287   return l_payroll_id;
8288 
8289 END Get_Payroll;
8290 
8291 /* ----------------------------------------------------------------------- */
8292 
8293 PROCEDURE Validate_Budget_Document
8294 ( p_return_status           OUT  NOCOPY  VARCHAR2,
8295   p_msg_count               OUT  NOCOPY  NUMBER,
8296   p_msg_data                OUT  NOCOPY  VARCHAR2,
8297   p_validation_level        IN   NUMBER := FND_API.G_VALID_LEVEL_FULL,
8298   p_event_type              IN   VARCHAR2,
8299   p_source_id               IN   NUMBER,
8300   p_data_extract_id         IN   NUMBER,
8301   p_system_data_extract_id  IN   NUMBER) IS
8302 
8303   /*For Bug No : 2292003 Start*/
8304   --l_payroll_id              NUMBER;
8305   --l_no_payroll_count        NUMBER := 0;
8306   /*For Bug No : 2292003 End*/
8307   l_no_hr_pos_count         NUMBER := 0;
8308 
8309   cursor c_bpposition is
8310     select position_id, name, hr_position_id, hr_employee_id, effective_start_date
8311       from psb_positions
8312      where data_extract_id = p_data_extract_id;
8313 
8314   cursor c_brposition is
8315     select position_id, name, hr_position_id, hr_employee_id, effective_start_date
8316       from psb_positions
8317      where data_extract_id = p_system_data_extract_id;
8318 
8319 /* Fix for Bug #2642767 Start */
8320   cursor c_no_hr_position (positionid NUMBER) is
8321     select name, transaction_id
8322       from psb_positions
8323      where position_id = positionid
8324        and hr_position_id is null;
8325 /* Fix for Bug #2642767 End*/
8326 
8327   l_api_name               CONSTANT VARCHAR2(30)   := 'Validate_Budget_Document';
8328 
8329 BEGIN
8330 
8331   -- find payroll for the position
8332 
8333   if p_event_type = 'BP' then
8334   begin
8335 
8336   for c_position_rec in c_bpposition loop
8337 
8338     -- check if position exists in worksheet or budget revision
8339 
8340     if (Position_Exists(p_event_type => p_event_type, p_source_id => p_source_id,
8341 	p_position_id => c_position_rec.position_id)) then
8342     begin
8343 
8344       /*For Bug No : 2292003 Start*/
8345       /*if p_validation_level = FND_API.G_VALID_LEVEL_NONE then
8346 	 --changed the c_position_rec.effective_start_date to g_de_as_of_date
8347 	 --in the following call of the procedure
8348 	 l_payroll_id := Get_Payroll(p_hr_position_id => c_position_rec.hr_position_id,
8349 				  p_data_extract_id => p_data_extract_id
8350 				  p_effective_start_date => g_de_as_of_date);
8351 
8352 	 if l_payroll_id is null then
8353 	   message_token('POSITION', c_position_rec.name);
8354 	   add_message('PSB', 'PSB_PQH_POSITION_NO_PAYROLL');
8355 	   l_no_payroll_count := l_no_payroll_count + 1;
8356 	 end if;
8357       end if;*/
8358       /*For Bug No : 2292003 End*/
8359 
8360       -- check that all positions are mapped to HR positions
8361       if p_validation_level = FND_API.G_VALID_LEVEL_FULL then
8362 	 for c_no_hr_position_rec in c_no_hr_position (c_position_rec.position_id) loop
8363 /* Fix for Bug #2642767 Start */
8364           if c_no_hr_position_rec.transaction_id is null then
8365               message_token('POSITION',c_no_hr_position_rec.name);
8366               add_message('PSB', 'PSB_PQH_NO_HR_POSITION');
8367           else
8368               message_token('POSITION',c_no_hr_position_rec.name);
8369               add_message('PSB', 'PSB_PQH_NO_HR_POSITION_TRX');
8370           end if;
8371 /* Fix for Bug #2642767 End*/
8372 	   l_no_hr_pos_count := l_no_hr_pos_count + 1;
8373 	 end loop;
8374       end if;
8375 
8376     end;
8377     end if;
8378 
8379   end loop;
8380 
8381   end;
8382   elsif p_event_type = 'BR' then
8383   begin
8384 
8385   for c_position_rec in c_brposition loop
8386 
8387     -- check if position exists in worksheet or budget revision
8388 
8389     if (Position_Exists(p_event_type => p_event_type, p_source_id => p_source_id,
8390 	p_position_id => c_position_rec.position_id)) then
8391     begin
8392 
8393       /*For Bug No : 2292003 Start*/
8394       /*if p_validation_level = FND_API.G_VALID_LEVEL_NONE then
8395 	 --changed the c_position_rec.effective_start_date to g_de_as_of_date
8396 	 --in the following call of the procedure
8397 	 l_payroll_id := Get_Payroll(p_hr_position_id => c_position_rec.hr_position_id,
8398 				  p_data_extract_id => p_system_data_extract_id,
8399 				  p_effective_start_date => g_de_as_of_date);
8400 
8401 	 if l_payroll_id is null then
8402 	   message_token('POSITION', c_position_rec.name);
8403 	   add_message('PSB', 'PSB_PQH_POSITION_NO_PAYROLL');
8404 	   l_no_payroll_count := l_no_payroll_count + 1;
8405 	 end if;
8406       end if;*/
8407       /*For Bug No : 2292003 End*/
8408 
8409       -- check that all positions are mapped to HR positions
8410       if p_validation_level = FND_API.G_VALID_LEVEL_FULL then
8411 	 for c_no_hr_position_rec in c_no_hr_position (c_position_rec.position_id) loop
8412 /* Fix for Bug #2642767 Start */
8413           if c_no_hr_position_rec.transaction_id is null then
8414               message_token('POSITION',c_no_hr_position_rec.name);
8415               add_message('PSB', 'PSB_PQH_NO_HR_POSITION');
8416           else
8417               message_token('POSITION',c_no_hr_position_rec.name);
8418               add_message('PSB', 'PSB_PQH_NO_HR_POSITION_TRX');
8419           end if;
8420 /* Fix for Bug #2642767 End*/
8421 	   l_no_hr_pos_count := l_no_hr_pos_count + 1;
8422 	 end loop;
8423       end if;
8424 
8425     end;
8426     end if;
8427 
8428   end loop;
8429 
8430   end;
8431   end if;
8432 
8433   if (l_no_hr_pos_count > 0) then
8434     FND_MESSAGE.SET_NAME('PSB','PSB_DEBUG_MESSAGE');
8435     FND_MESSAGE.SET_TOKEN('MESSAGE', 'No HR Pos Count');
8436     FND_MSG_PUB.Add;
8437     raise FND_API.G_EXC_ERROR;
8438   end if;
8439 
8440   -- Initialize API return status to success
8441   p_return_status := FND_API.G_RET_STS_SUCCESS;
8442 
8443 EXCEPTION
8444 
8445    when FND_API.G_EXC_ERROR then
8446      p_return_status := FND_API.G_RET_STS_ERROR;
8447 
8448      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
8449 				p_data  => p_msg_data);
8450 
8451    when FND_API.G_EXC_UNEXPECTED_ERROR then
8452      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8453 
8454      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
8455 				p_data  => p_msg_data);
8456 
8457    when OTHERS then
8458      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8459 
8460      if FND_MSG_PUB.Check_Msg_Level
8461        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
8462        FND_MSG_PUB.Add_Exc_Msg
8463 	  (p_pkg_name => G_PKG_NAME,
8464 	   p_procedure_name => l_api_name);
8465      end if;
8466 
8467      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
8468 				p_data  => p_msg_data);
8469 
8470 END Validate_Budget_Document;
8471 
8472 /* ----------------------------------------------------------------------- */
8473 
8474 PROCEDURE Validate_Budget_Set
8475 ( p_return_status           OUT  NOCOPY  VARCHAR2,
8476   p_msg_count               OUT  NOCOPY  NUMBER,
8477   p_msg_data                OUT  NOCOPY  VARCHAR2,
8478   p_event_type              IN   VARCHAR2,
8479   p_source_id               IN   NUMBER,
8480   p_data_extract_id         IN   NUMBER,
8481   p_system_data_extract_id  IN   NUMBER) IS
8482 
8483   l_no_elem_count           NUMBER := 0;
8484 
8485   cursor c_no_element_map is
8486     select pay_element_id, name
8487       from psb_pay_elements
8488      where data_extract_id = p_system_data_extract_id
8489        and budget_set_id is null;
8490 
8491   l_api_name               CONSTANT VARCHAR2(30)   := 'Validate_Budget_Set';
8492 
8493 BEGIN
8494 
8495   for c_no_element_map_rec in c_no_element_map loop
8496 
8497     if (Element_Exists(p_event_type => p_event_type, p_source_id => p_source_id, p_data_extract_id => p_data_extract_id, p_element_id => c_no_element_map_rec.pay_element_id)) then
8498       message_token('ELEMENT', c_no_element_map_rec.name);
8499       add_message('PSB', 'PSB_PQH_NO_ELEMENT_MAPPING');
8500       l_no_elem_count := l_no_elem_count + 1;
8501     end if;
8502 
8503   end loop;
8504 
8505   if l_no_elem_count > 0 then
8506     FND_MESSAGE.SET_NAME('PSB','PSB_DEBUG_MESSAGE');
8507     FND_MESSAGE.SET_TOKEN('MESSAGE', 'No Element Count');
8508     FND_MSG_PUB.Add;
8509     raise FND_API.G_EXC_ERROR;
8510   end if;
8511 
8512   -- Initialize API return status to success
8513   p_return_status := FND_API.G_RET_STS_SUCCESS;
8514 
8515 EXCEPTION
8516 
8517    when FND_API.G_EXC_ERROR then
8518      p_return_status := FND_API.G_RET_STS_ERROR;
8519 
8520      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
8521 				p_data  => p_msg_data);
8522 
8523    when FND_API.G_EXC_UNEXPECTED_ERROR then
8524      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8525 
8526      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
8527 				p_data  => p_msg_data);
8528 
8529    when OTHERS then
8530      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8531 
8532      if FND_MSG_PUB.Check_Msg_Level
8533        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
8534        FND_MSG_PUB.Add_Exc_Msg
8535 	  (p_pkg_name => G_PKG_NAME,
8536 	   p_procedure_name => l_api_name);
8537      end if;
8538 
8539      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
8540 				p_data  => p_msg_data);
8541 
8542 END Validate_Budget_Set;
8543 
8544 /* ----------------------------------------------------------------------- */
8545 
8546 PROCEDURE Cache_Position_Costs
8547 (p_return_status      OUT  NOCOPY VARCHAR2,
8548  p_event_type         IN  VARCHAR2,
8549  p_hr_budget_id       IN  NUMBER,
8550  p_hr_position_id     IN  NUMBER,
8551  p_source_id          IN  NUMBER,
8552  p_data_extract_id    IN  NUMBER,
8553  p_start_date         IN  DATE,
8554  p_end_date           IN  DATE,
8555  p_currency_code      IN  VARCHAR2) IS
8556 
8557   TYPE id_arr IS TABLE OF NUMBER(15);
8558   TYPE num_arr IS TABLE OF NUMBER;
8559   TYPE date_arr IS TABLE OF DATE;
8560 
8561   TYPE elem_costs IS RECORD (pay_element_id id_arr, budget_set_id id_arr, element_cost num_arr);
8562   l_element_costs     elem_costs;
8563 
8564   cursor c_Element_Costs is
8565     select ppe.pay_element_id, ppe.budget_set_id, sum(nvl(ppc.element_cost,0)) element_cost
8566       from psb_pay_elements ppe,
8567 	   psb_position_costs ppc,
8568 	   psb_positions pp
8569      where ppe.data_extract_id = p_data_extract_id
8570        and ppc.pay_element_id = ppe.pay_element_id
8571        and pp.data_extract_id = p_data_extract_id
8572        and pp.hr_position_id = p_hr_position_id
8573        and ppc.position_id = pp.position_id
8574        and nvl(ppc.hr_budget_id, -1) = nvl(p_hr_budget_id, -1)
8575        and ((p_event_type = 'BP' and ppc.base_line_version = 'C')
8576 	   or (p_event_type = 'BR' and ppc.budget_revision_id = p_source_id))
8577        and ppc.currency_code = p_currency_code
8578        and ((ppc.start_date between p_start_date and p_end_date)
8579 	 or (ppc.end_date between p_start_date and p_end_date)
8580 	 or ((ppc.start_date < p_start_date) and (ppc.end_date > p_end_date)))
8581     group by ppe.budget_set_id, ppe.pay_element_id;
8582 
8583 BEGIN
8584 
8585   for l_init_index in 1..g_element_costs.Count loop
8586     g_element_costs(l_init_index).budget_set_id := null;
8587     g_element_costs(l_init_index).pay_element_id := null;
8588     g_element_costs(l_init_index).element_cost := null;
8589   end loop;
8590 
8591   g_num_element_costs := 0;
8592 
8593   open c_Element_Costs;
8594   loop
8595 
8596     fetch c_Element_Costs BULK COLLECT INTO l_element_costs.pay_element_id, l_element_costs.budget_set_id, l_element_costs.element_cost LIMIT g_limit_bulk_numrows;
8597 
8598     for l_cost_index in 1..l_element_costs.pay_element_id.count loop
8599       g_num_element_costs := g_num_element_costs + 1;
8600       g_element_costs(g_num_element_costs).budget_set_id := l_element_costs.budget_set_id(l_cost_index);
8601       g_element_costs(g_num_element_costs).pay_element_id := l_element_costs.pay_element_id(l_cost_index);
8602       g_element_costs(g_num_element_costs).element_cost := l_element_costs.element_cost(l_cost_index);
8603     end loop;
8604     exit when c_Element_Costs%NOTFOUND;
8605 
8606   end loop;
8607   close c_Element_Costs;
8608 
8609   p_return_status := FND_API.G_RET_STS_SUCCESS;
8610 
8611 EXCEPTION
8612 
8613    when FND_API.G_EXC_ERROR then
8614      if c_Element_Costs%ISOPEN then
8615        close c_Element_Costs;
8616      end if;
8617      p_return_status := FND_API.G_RET_STS_ERROR;
8618 
8619    when FND_API.G_EXC_UNEXPECTED_ERROR then
8620      if c_Element_Costs%ISOPEN then
8621        close c_Element_Costs;
8622      end if;
8623      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8624 
8625    when OTHERS then
8626      if c_Element_Costs%ISOPEN then
8627        close c_Element_Costs;
8628      end if;
8629      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8630 
8631 End Cache_Position_Costs;
8632 
8633 /* ----------------------------------------------------------------------- */
8634 
8635 PROCEDURE Distribute_Salary
8636 ( p_return_status         OUT  NOCOPY  VARCHAR2,
8637   p_source_id             IN   VARCHAR2,
8638   p_gl_flex_code          IN   NUMBER,
8639   p_data_extract_id       IN   NUMBER,
8640   p_hr_position_id        IN   NUMBER,
8641   p_start_date            IN   DATE,
8642   p_end_date              IN   DATE
8643 ) IS
8644 
8645   TYPE id_arr IS TABLE OF NUMBER(15);
8646   TYPE num_arr IS TABLE OF NUMBER;
8647 
8648   TYPE dist_rec IS RECORD
8649       (pay_element_id id_arr, code_combination_id id_arr, distribution_percent num_arr);
8650   l_dist                  dist_rec;
8651 
8652   cursor c_Dist is
8653     select distinct c.pay_element_id, b.code_combination_id,
8654 	   b.distribution_percent
8655       from PSB_PAY_ELEMENTS c, PSB_POSITION_PAY_DISTRIBUTIONS b
8656      where c.data_extract_id = p_data_extract_id
8657        and c.salary_flag = 'Y'
8658        and b.position_id in
8659 	  (select a.position_id from PSB_POSITIONS a
8660 	    where a.hr_position_id = p_hr_position_id
8661 	      and a.data_extract_id = p_data_extract_id)
8662        and exists
8663 	  (select 1 from PSB_POSITION_ASSIGNMENTS d
8664 	    where d.position_id = b.position_id and d.pay_element_id = c.pay_element_id)
8665        and ((b.worksheet_id = p_source_id)
8666 	 or (b.worksheet_id is null
8667        and not exists
8668 	  (select 1 from PSB_POSITION_PAY_DISTRIBUTIONS c
8669 	    where c.position_id = b.position_id
8670 	      and c.worksheet_id = p_source_id)))
8671        and b.chart_of_accounts_id = p_gl_flex_code
8672        and b.code_combination_id is not null
8673        and (((p_end_date is not null)
8674 	 and (((b.effective_start_date <= p_end_date)
8675 	   and (b.effective_end_date is null))
8676 	   or ((b.effective_start_date between p_start_date and p_end_date)
8677 	   or (b.effective_end_date between p_start_date and p_end_date)
8678 	   or ((b.effective_start_date < p_start_date)
8679 	   and (b.effective_end_date > p_end_date)))))
8680 	or ((p_end_date is null)
8681 	and (nvl(b.effective_end_date, p_start_date) >= p_start_date)))
8682      order by b.distribution_percent desc;
8683 
8684   l_percent               NUMBER;
8685 
8686   l_return_status         VARCHAR2(1);
8687 
8688 BEGIN
8689 
8690   PSB_WS_POS1.g_salary_budget_group_id := null;
8691   PSB_WS_POS1.Initialize_Salary_Dist;
8692 
8693   open c_Dist;
8694   loop
8695 
8696     fetch c_Dist BULK COLLECT into
8697       l_dist.pay_element_id, l_dist.code_combination_id, l_dist.distribution_percent LIMIT g_limit_bulk_numrows;
8698 
8699     for l_index in 1..l_dist.pay_element_id.count loop
8700 
8701       PSB_WS_POS1.g_num_salary_dist := PSB_WS_POS1.g_num_salary_dist + 1;
8702 
8703       PSB_WS_POS1.g_salary_dist(PSB_WS_POS1.g_num_salary_dist).ccid := l_dist.code_combination_id(l_index);
8704       PSB_WS_POS1.g_salary_dist(PSB_WS_POS1.g_num_salary_dist).percent := l_dist.distribution_percent(l_index);
8705 
8706       -- commented for bug # 4502946
8707       /*if l_dist.distribution_percent(l_index) < 1 then
8708 	l_percent := l_dist.distribution_percent(l_index);
8709       else
8710 	l_percent := l_dist.distribution_percent(l_index) / 100;
8711       end if;*/
8712 
8713       -- added for bug # 4502946
8714       l_percent := l_dist.distribution_percent(l_index) / 100;
8715 
8716       g_num_element_dists := g_num_element_dists + 1;
8717 
8718       g_element_dists(g_num_element_dists).pay_element_id := l_dist.pay_element_id(l_index);
8719       g_element_dists(g_num_element_dists).ccid := l_dist.code_combination_id(l_index);
8720       g_element_dists(g_num_element_dists).percent := l_percent;
8721 
8722     end loop;
8723     exit when c_Dist%NOTFOUND;
8724 
8725   end loop;
8726   close c_Dist;
8727 
8728   if p_gl_flex_code <> nvl(PSB_WS_ACCT1.g_flex_code, FND_API.G_MISS_NUM) then
8729   begin
8730 
8731     PSB_WS_ACCT1.Flex_Info
8732        (p_flex_code => p_gl_flex_code,
8733 	p_return_status => l_return_status);
8734 
8735     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
8736       raise FND_API.G_EXC_ERROR;
8737     end if;
8738 
8739   end;
8740   end if;
8741 
8742   -- Initialize API return status to success
8743   p_return_status := FND_API.G_RET_STS_SUCCESS;
8744 
8745 EXCEPTION
8746 
8747    when FND_API.G_EXC_ERROR then
8748      if c_Dist%ISOPEN then
8749        close c_Dist;
8750      end if;
8751      p_return_status := FND_API.G_RET_STS_ERROR;
8752 
8753    when FND_API.G_EXC_UNEXPECTED_ERROR then
8754      if c_Dist%ISOPEN then
8755        close c_Dist;
8756      end if;
8757      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8758 
8759    when OTHERS then
8760      if c_Dist%ISOPEN then
8761        close c_Dist;
8762      end if;
8763      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8764 
8765 END Distribute_Salary;
8766 /*---------------------------------------------------------------------------*/
8767 
8768 
8769 /*===========================================================================+
8770  |                          PROCEDURE Distribute_Other_Elements              |
8771  +===========================================================================*/
8772 PROCEDURE Distribute_Other_Elements
8773 ( p_return_status         OUT  NOCOPY  VARCHAR2,
8774   p_gl_flex_code          IN   NUMBER,
8775   p_data_extract_id       IN   NUMBER,
8776   p_hr_position_id        IN   NUMBER,
8777   p_start_date            IN   DATE,
8778   p_end_date              IN   DATE
8779 )
8780 IS
8781   --
8782   l_ccid_val              FND_FLEX_EXT.SegmentArray;
8783   l_seg_val               FND_FLEX_EXT.SegmentArray;
8784   l_ccid                  NUMBER;
8785   l_start_date            DATE;
8786   l_end_date              DATE;
8787   l_dist_start_date       DATE;
8788   l_dist_end_date         DATE;
8789   l_amount                NUMBER;
8790   l_percent               NUMBER;
8791 
8792   /* start bug 3666828 */
8793   l_hr_position_id 		  NUMBER;
8794   l_element_name 		  VARCHAR2(80);
8795   /* end bug 3666828 */
8796 
8797   TYPE char_arr IS TABLE OF VARCHAR2(25);
8798 
8799   /* start bug 3666828 */
8800   TYPE l_char_arr IS TABLE OF VARCHAR2(80);
8801   /* end bug 3666828 */
8802 
8803   TYPE flag_arr IS TABLE OF VARCHAR2(1);
8804   TYPE id_arr IS TABLE OF NUMBER(15);
8805   TYPE num_arr IS TABLE OF NUMBER;
8806 
8807   TYPE dist_rec IS RECORD
8808   (segment1 char_arr,  segment2 char_arr, segment3 char_arr, segment4 char_arr,
8809    segment5 char_arr,  segment6 char_arr, segment7 char_arr, segment8 char_arr,
8810    segment9 char_arr,  segment10 char_arr, segment11 char_arr,
8811    segment12 char_arr, segment13 char_arr, segment14 char_arr,
8812    segment15 char_arr, segment16 char_arr, segment17 char_arr,
8813    segment18 char_arr, segment19 char_arr, segment20 char_arr,
8814    segment21 char_arr, segment22 char_arr, segment23 char_arr,
8815    segment24 char_arr, segment25 char_arr, segment26 char_arr,
8816    segment27 char_arr, segment28 char_arr, segment29 char_arr,
8817    segment30 char_arr, code_combination_id id_arr, distribution_percent num_arr,
8818    pay_element_id id_arr, follow_salary flag_arr,
8819    /* bug No : 3666828 */ name l_char_arr
8820   ) ;
8821   l_dist                  dist_rec;
8822   --
8823   cursor c_Dist is
8824     select distinct a.segment1, a.segment2, a.segment3, a.segment4,
8825 	   a.segment5, a.segment6, a.segment7, a.segment8,
8826 	   a.segment9, a.segment10, a.segment11, a.segment12,
8827 	   a.segment13, a.segment14, a.segment15, a.segment16,
8828 	   a.segment17, a.segment18, a.segment19, a.segment20,
8829 	   a.segment21, a.segment22, a.segment23, a.segment24,
8830 	   a.segment25, a.segment26, a.segment27, a.segment28,
8831 	   a.segment29, a.segment30,
8832 	   a.code_combination_id, a.distribution_percent, e.pay_element_id,
8833            e.follow_salary, /* bug No: 3666828 */ e.name
8834       from PSB_PAY_ELEMENTS e, PSB_PAY_ELEMENT_DISTRIBUTIONS a,
8835 	   PSB_ELEMENT_POS_SET_GROUPS b,
8836 	   PSB_SET_RELATIONS c,
8837 	   PSB_BUDGET_POSITIONS d
8838      where e.data_extract_id = p_data_extract_id
8839        and e.salary_flag <> 'Y'
8840        and a.chart_of_accounts_id = p_gl_flex_code
8841        and (((a.effective_start_date <= p_end_date)
8842 	 and (a.effective_end_date is null))
8843 	 or ((a.effective_start_date between p_start_date and p_end_date)
8844 	  or (a.effective_end_date between p_start_date and p_end_date)
8845 	 or ((a.effective_start_date < p_start_date)
8846 	 and (a.effective_end_date > p_end_date))))
8847        and a.position_set_group_id = b.position_set_group_id
8848        and b.position_set_group_id = c.position_set_group_id
8849        and b.pay_element_id = e.pay_element_id
8850        and c.account_position_set_id = d.account_position_set_id
8851        and exists
8852 	  (select 1 from PSB_POSITION_ASSIGNMENTS g
8853 	    where g.position_id = d.position_id
8854             and g.pay_element_id = e.pay_element_id)
8855        and d.position_id in
8856 	  (select f.position_id from PSB_POSITIONS f
8857 	    where f.hr_position_id = p_hr_position_id
8858 	      and f.data_extract_id = p_data_extract_id);
8859   --
8860 BEGIN
8861 
8862   open c_Dist;
8863 
8864   loop
8865 
8866     fetch c_Dist BULK COLLECT into
8867       l_dist.segment1, l_dist.segment2, l_dist.segment3, l_dist.segment4, l_dist.segment5, l_dist.segment6,
8868       l_dist.segment7, l_dist.segment8, l_dist.segment9, l_dist.segment10, l_dist.segment11, l_dist.segment12,
8869       l_dist.segment13, l_dist.segment14, l_dist.segment15, l_dist.segment16, l_dist.segment17, l_dist.segment18,
8870       l_dist.segment19, l_dist.segment20, l_dist.segment21, l_dist.segment22, l_dist.segment23, l_dist.segment24,
8871       l_dist.segment25, l_dist.segment26, l_dist.segment27, l_dist.segment28, l_dist.segment29, l_dist.segment30,
8872       l_dist.code_combination_id, l_dist.distribution_percent, l_dist.pay_element_id, l_dist.follow_salary,
8873       /* bug No : 3666828 */l_dist.name
8874       LIMIT g_limit_bulk_numrows;
8875 
8876     for l_dist_index in 1..l_dist.pay_element_id.count loop
8877 
8878 
8879       if l_dist.follow_salary(l_dist_index) = 'Y' then
8880       begin
8881 
8882 	for l_saldist_index in 1..PSB_WS_POS1.g_num_salary_dist loop
8883 
8884 	  l_dist_start_date := greatest(l_start_date, PSB_WS_POS1.g_salary_dist(l_saldist_index).start_date);
8885 	  l_dist_end_date := least(l_end_date, nvl(PSB_WS_POS1.g_salary_dist(l_saldist_index).end_date, l_end_date));
8886 
8887           -- commented for bug # 4502946
8888 	  /*if PSB_WS_POS1.g_salary_dist(l_saldist_index).percent < 1 then
8889 	    l_percent := PSB_WS_POS1.g_salary_dist(l_saldist_index).percent;
8890 	  else
8891 	    l_percent := PSB_WS_POS1.g_salary_dist(l_saldist_index).percent / 100;
8892 	  end if;*/
8893 
8894           -- added for bug # 4502946
8895           l_percent := PSB_WS_POS1.g_salary_dist(l_saldist_index).percent / 100;
8896 
8897 	  for l_init_index in 1..PSB_WS_ACCT1.g_num_segs loop
8898 	    l_ccid_val(l_init_index) := null;
8899 	    l_seg_val(l_init_index) := null;
8900 	  end loop;
8901 
8902 
8903 
8904 	  if not FND_FLEX_EXT.Get_Segments
8905 	    (application_short_name => 'SQLGL',
8906 	     key_flex_code => 'GL#',
8907 	     structure_number => p_gl_flex_code,
8908 	     combination_id => PSB_WS_POS1.g_salary_dist(l_saldist_index).ccid,
8909 	     n_segments => PSB_WS_ACCT1.g_num_segs,
8910 	     segments => l_ccid_val) then
8911 
8912                /* start bug 3666828 */
8913 	       l_hr_position_id := p_hr_position_id;
8914 	       l_element_name := l_dist.name(l_dist_index);
8915 	       fnd_file.put_line(fnd_file.log, ' Element : '||l_element_name||' , Position id : '|| l_hr_position_id);
8916 	       /* End bug 3666828 */
8917 
8918 	    FND_MSG_PUB.Add;
8919 	    raise FND_API.G_EXC_ERROR;
8920 	  end if;
8921 
8922 	  for l_index in 1..PSB_WS_ACCT1.g_num_segs loop
8923 
8924 	    if ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT1') and
8925 		(l_dist.segment1(l_dist_index) is not null)) then
8926 	      l_seg_val(l_index) := l_dist.segment1(l_dist_index);
8927 
8928 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT2') and
8929 		(l_dist.segment2(l_dist_index) is not null)) then
8930 	      l_seg_val(l_index) := l_dist.segment2(l_dist_index);
8931 
8932 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT3') and
8933 		(l_dist.segment3(l_dist_index) is not null)) then
8934 	      l_seg_val(l_index) := l_dist.segment3(l_dist_index);
8935 
8936 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT4') and
8937 		(l_dist.segment4(l_dist_index) is not null)) then
8938 	      l_seg_val(l_index) := l_dist.segment4(l_dist_index);
8939 
8940 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT5') and
8941 		(l_dist.segment5(l_dist_index) is not null)) then
8942 	      l_seg_val(l_index) := l_dist.segment5(l_dist_index);
8943 
8944 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT6') and
8945 		(l_dist.segment6(l_dist_index) is not null)) then
8946 	      l_seg_val(l_index) := l_dist.segment6(l_dist_index);
8947 
8948 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT7') and
8949 		(l_dist.segment7(l_dist_index) is not null)) then
8950 	      l_seg_val(l_index) := l_dist.segment7(l_dist_index);
8951 
8952 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT8') and
8953 		(l_dist.segment8(l_dist_index) is not null)) then
8954 	      l_seg_val(l_index) := l_dist.segment8(l_dist_index);
8955 
8956 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT9') and
8957 		(l_dist.segment9(l_dist_index) is not null)) then
8958 	      l_seg_val(l_index) := l_dist.segment9(l_dist_index);
8959 
8960 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT10') and
8961 		(l_dist.segment10(l_dist_index) is not null)) then
8962 	      l_seg_val(l_index) := l_dist.segment10(l_dist_index);
8963 
8964 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT11') and
8965 		(l_dist.segment11(l_dist_index) is not null)) then
8966 	      l_seg_val(l_index) := l_dist.segment11(l_dist_index);
8967 
8968 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT12') and
8969 		(l_dist.segment12(l_dist_index) is not null)) then
8970 	      l_seg_val(l_index) := l_dist.segment12(l_dist_index);
8971 
8972 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT13') and
8973 		(l_dist.segment13(l_dist_index) is not null)) then
8974 	      l_seg_val(l_index) := l_dist.segment13(l_dist_index);
8975 
8976 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT14') and
8977 		(l_dist.segment14(l_dist_index) is not null)) then
8978 	      l_seg_val(l_index) := l_dist.segment14(l_dist_index);
8979 
8980 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT15') and
8981 		(l_dist.segment15(l_dist_index) is not null)) then
8982 	      l_seg_val(l_index) := l_dist.segment15(l_dist_index);
8983 
8984 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT16') and
8985 		(l_dist.segment16(l_dist_index) is not null)) then
8986 	      l_seg_val(l_index) := l_dist.segment16(l_dist_index);
8987 
8988 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT17') and
8989 		(l_dist.segment17(l_dist_index) is not null)) then
8990 	      l_seg_val(l_index) := l_dist.segment17(l_dist_index);
8991 
8992 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT18') and
8993 		(l_dist.segment18(l_dist_index) is not null)) then
8994 	      l_seg_val(l_index) := l_dist.segment18(l_dist_index);
8995 
8996 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT19') and
8997 		(l_dist.segment19(l_dist_index) is not null)) then
8998 	      l_seg_val(l_index) := l_dist.segment19(l_dist_index);
8999 
9000 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT20') and
9001 		(l_dist.segment20(l_dist_index) is not null)) then
9002 	      l_seg_val(l_index) := l_dist.segment20(l_dist_index);
9003 
9004 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT21') and
9005 		(l_dist.segment21(l_dist_index) is not null)) then
9006 	      l_seg_val(l_index) := l_dist.segment21(l_dist_index);
9007 
9008 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT22') and
9009 		(l_dist.segment22(l_dist_index) is not null)) then
9010 	      l_seg_val(l_index) := l_dist.segment22(l_dist_index);
9011 
9012 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT23') and
9013 		(l_dist.segment23(l_dist_index) is not null)) then
9014 	      l_seg_val(l_index) := l_dist.segment23(l_dist_index);
9015 
9016 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT24') and
9017 		(l_dist.segment24(l_dist_index) is not null)) then
9018 	      l_seg_val(l_index) := l_dist.segment24(l_dist_index);
9019 
9020 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT25') and
9021 		(l_dist.segment25(l_dist_index) is not null)) then
9022 	      l_seg_val(l_index) := l_dist.segment25(l_dist_index);
9023 
9024 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT26') and
9025 		(l_dist.segment26(l_dist_index) is not null)) then
9026 	      l_seg_val(l_index) := l_dist.segment26(l_dist_index);
9027 
9028 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT27') and
9029 		(l_dist.segment27(l_dist_index) is not null)) then
9030 	      l_seg_val(l_index) := l_dist.segment27(l_dist_index);
9031 
9032 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT28') and
9033 		(l_dist.segment28(l_dist_index) is not null)) then
9034 	      l_seg_val(l_index) := l_dist.segment28(l_dist_index);
9035 
9036 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT29') and
9037 		(l_dist.segment29(l_dist_index) is not null)) then
9038 	      l_seg_val(l_index) := l_dist.segment29(l_dist_index);
9039 
9040 	    elsif ((PSB_WS_ACCT1.g_seg_name(l_index) = 'SEGMENT30') and
9041 		(l_dist.segment30(l_dist_index) is not null)) then
9042 	      l_seg_val(l_index) := l_dist.segment30(l_dist_index);
9043 
9044 	    else
9045 	      l_seg_val(l_index) := l_ccid_val(l_index);
9046 	    end if;
9047 
9048 	  end loop;
9049 
9050 
9051 	  if not FND_FLEX_EXT.Get_Combination_ID
9052 	    (application_short_name => 'SQLGL',
9053 	     key_flex_code => 'GL#',
9054 	     structure_number => p_gl_flex_code,
9055 	     validation_date => sysdate,
9056 	     n_segments => PSB_WS_ACCT1.g_num_segs,
9057 	     segments => l_seg_val,
9058 	     combination_id => l_ccid) then
9059 
9060               /* start bug 3666828 */
9061 	      l_hr_position_id := p_hr_position_id;
9062 	      l_element_name := l_dist.name(l_dist_index);
9063 	      fnd_file.put_line(fnd_file.log, 'Element : '||l_element_name||' , Position id : '|| l_hr_position_id);
9064 	      /* End bug 3666828 */
9065 
9066 	    FND_MSG_PUB.Add;
9067 	    raise FND_API.G_EXC_ERROR;
9068 	  end if;
9069 
9070 	  g_num_element_dists := g_num_element_dists + 1;
9071 
9072 	  g_element_dists(g_num_element_dists).pay_element_id := l_dist.pay_element_id(l_dist_index);
9073 	  g_element_dists(g_num_element_dists).ccid := l_ccid;
9074 	  g_element_dists(g_num_element_dists).percent := l_percent;
9075 
9076 	end loop;
9077 
9078       end;
9079       else
9080       begin
9081 
9082         -- commented for bug # 4502946
9083 	/*if l_dist.distribution_percent(l_dist_index) < 1 then
9084 	  l_percent := l_dist.distribution_percent(l_dist_index);
9085 	else
9086 	  l_percent := l_dist.distribution_percent(l_dist_index) / 100;
9087 	end if;*/
9088 
9089         -- added for bug # 4502946
9090         l_percent := l_dist.distribution_percent(l_dist_index) / 100;
9091 
9092 	g_num_element_dists := g_num_element_dists + 1;
9093 
9094 	g_element_dists(g_num_element_dists).pay_element_id := l_dist.pay_element_id(l_dist_index);
9095 	g_element_dists(g_num_element_dists).ccid := l_dist.code_combination_id(l_dist_index);
9096 	g_element_dists(g_num_element_dists).percent := l_percent;
9097 
9098       end;
9099       end if;
9100 
9101     end loop;
9102     exit when c_Dist%NOTFOUND;
9103 
9104   end loop;
9105   close c_Dist;
9106 
9107   -- Initialize API return status to success
9108   p_return_status := FND_API.G_RET_STS_SUCCESS;
9109 
9110 EXCEPTION
9111   /* start bug 3666828 */
9112   --
9113   /*WHEN FND_API.G_EXC_ERROR THEN
9114     IF c_Dist%ISOPEN THEN
9115       CLOSE c_Dist;
9116     END IF;
9117     p_return_status := FND_API.G_RET_STS_ERROR;
9118   --
9119   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
9120     IF c_Dist%ISOPEN THEN
9121       CLOSE c_Dist;
9122     END IF;
9123     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;*/
9124   --
9125   /* end bug 3666828 */
9126   WHEN OTHERS THEN
9127     IF c_Dist%ISOPEN THEN
9128       CLOSE c_Dist;
9129     END IF;
9130     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9131     --
9132     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
9133       FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME,
9134                                 'Distribute_Other_Elements');
9135     END IF;
9136     --
9137 END Distribute_Other_Elements;
9138 /*---------------------------------------------------------------------------*/
9139 
9140 
9141 /*===========================================================================+
9142  |                   PROCEDURE Distribute_Position_Cost                      |
9143  +===========================================================================*/
9144 PROCEDURE Distribute_Position_Cost
9145 ( p_return_status         OUT  NOCOPY  VARCHAR2,
9146   p_event_type            IN   VARCHAR2,
9147   p_source_id             IN   NUMBER,
9148   p_hr_budget_id          IN   NUMBER,
9149   p_data_extract_id       IN   NUMBER,
9150   p_gl_flex_code          IN   NUMBER,
9151   p_currency_code         IN   VARCHAR2,
9152   p_hr_position_id        IN   NUMBER,
9153   p_start_date            IN   DATE,
9154   p_end_date              IN   DATE
9155 )
9156 IS
9157   --
9158   l_return_status         VARCHAR2(1);
9159   l_setdist_exists_index  NUMBER;
9160   l_budget_set_id         NUMBER;
9161   l_start_date            DATE;
9162   l_budgetset_index       NUMBER := 0;
9163   l_budgetset_total       NUMBER := 0;
9164   --
9165 BEGIN
9166 
9167   for l_init_index in 1..g_element_dists.Count loop
9168     g_element_dists(l_init_index).pay_element_id := null;
9169     g_element_dists(l_init_index).ccid := null;
9170     g_element_dists(l_init_index).percent := null;
9171   end loop;
9172 
9173   g_num_element_dists := 0;
9174 
9175   for l_init_index in 1..g_budgetset_dists.Count loop
9176     g_budgetset_dists(l_init_index).budget_set_id := null;
9177     g_budgetset_dists(l_init_index).ccid := null;
9178     g_budgetset_dists(l_init_index).amount := null;
9179     g_budgetset_dists(l_init_index).percent := null;
9180   end loop;
9181 
9182   g_num_budgetset_dists := 0;
9183 
9184   Cache_Position_Costs
9185   ( p_return_status   => l_return_status,
9186     p_event_type      => p_event_type,
9187     p_hr_budget_id    => p_hr_budget_id,
9188     p_hr_position_id  => p_hr_position_id,
9189     p_source_id       => p_source_id,
9190     p_data_extract_id => p_data_extract_id,
9191     p_start_date      => p_start_date,
9192     p_end_date        => p_end_date,
9193     p_currency_code   => p_currency_code
9194   );
9195   --
9196   IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
9197     RAISE FND_API.G_EXC_ERROR;
9198   END IF ;
9199   --
9200 
9201   Distribute_Salary
9202   ( p_return_status   => l_return_status,
9203     p_source_id       => p_source_id,
9204     p_gl_flex_code    => p_gl_flex_code,
9205     p_data_extract_id => p_data_extract_id,
9206     p_hr_position_id  => p_hr_position_id,
9207     p_start_date      => p_start_date,
9208     p_end_date        => p_end_date
9209   );
9210   --
9211   IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
9212     RAISE FND_API.G_EXC_ERROR;
9213   END IF ;
9214   --
9215 
9216   Distribute_Other_Elements
9217   ( p_return_status   => l_return_status,
9218     p_gl_flex_code    => p_gl_flex_code,
9219     p_data_extract_id => p_data_extract_id,
9220     p_hr_position_id  => p_hr_position_id,
9221     p_start_date      => p_start_date,
9222     p_end_date        => p_end_date
9223   );
9224   --
9225   IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
9226     RAISE FND_API.G_EXC_ERROR;
9227   END IF ;
9228   --
9229 
9230   for l_cost_index in 1..g_num_element_costs loop
9231 
9232     for l_dist_index in 1..g_num_element_dists loop
9233 
9234       if g_element_dists(l_dist_index).pay_element_id = g_element_costs(l_cost_index).pay_element_id then
9235       begin
9236 
9237 	l_setdist_exists_index := null;
9238 
9239 	for l_setdist_index in 1..g_num_budgetset_dists loop
9240 
9241 	  if ((g_budgetset_dists(l_setdist_index).budget_set_id = g_element_costs(l_cost_index).budget_set_id) and
9242 	      (g_budgetset_dists(l_setdist_index).ccid = g_element_dists(l_dist_index).ccid)) then
9243 	    l_setdist_exists_index := l_setdist_index;
9244 	  end if;
9245 
9246 	end loop;
9247 
9248 	if l_setdist_exists_index is null then
9249 	begin
9250 
9251 	  g_num_budgetset_dists := g_num_budgetset_dists + 1;
9252 
9253 	  g_budgetset_dists(g_num_budgetset_dists).budget_set_id := g_element_costs(l_cost_index).budget_set_id;
9254 	  g_budgetset_dists(g_num_budgetset_dists).ccid := g_element_dists(l_dist_index).ccid;
9255 	  g_budgetset_dists(g_num_budgetset_dists).amount := g_element_costs(l_cost_index).element_cost * g_element_dists(l_dist_index).percent;
9256 
9257 	end;
9258 	else
9259 	  g_budgetset_dists(l_setdist_exists_index).amount := g_budgetset_dists(l_setdist_exists_index).amount +
9260 				g_element_costs(l_cost_index).element_cost * g_element_dists(l_dist_index).percent;
9261 	end if;
9262 
9263       end;
9264       end if;
9265 
9266     end loop;
9267 
9268   end loop;
9269 
9270   FOR l_setdist_index in 1..g_num_budgetset_dists LOOP
9271 
9272   -- Bug#3348467: Process elements only when the cost is non-zero.
9273   IF NVL(g_budgetset_dists(l_setdist_index).amount,0) <> 0 THEN
9274 
9275     if ( g_budgetset_dists(l_setdist_index).budget_set_id <>
9276          nvl(l_budget_set_id, -1)
9277          or
9278 	 l_setdist_index = g_num_budgetset_dists
9279        )
9280     then
9281 
9282       if l_setdist_index = g_num_budgetset_dists then
9283 
9284 	if g_budgetset_dists(l_setdist_index).budget_set_id <> nvl(l_budget_set_id, -1) then
9285 
9286 	  if ((l_budgetset_index > 0) and (l_setdist_index > 1)) then
9287 	    for l_dist_index in l_budgetset_index..(l_setdist_index - 1) loop
9288 	      g_budgetset_dists(l_dist_index).percent := g_budgetset_dists(l_dist_index).amount * 100 / l_budgetset_total;
9289 	    end loop;
9290 	  end if;
9291 
9292 	  l_budgetset_total := g_budgetset_dists(l_setdist_index).amount;
9293 
9294 	  for l_dist_index in g_num_budgetset_dists..g_num_budgetset_dists loop
9295 	    g_budgetset_dists(l_dist_index).percent := g_budgetset_dists(l_dist_index).amount * 100 / l_budgetset_total;
9296 	  end loop;
9297 
9298 	else
9299 
9300 	  l_budgetset_total := l_budgetset_total + g_budgetset_dists(l_setdist_index).amount;
9301 
9302 	  for l_dist_index in l_budgetset_index..l_setdist_index loop
9303 	    g_budgetset_dists(l_dist_index).percent := g_budgetset_dists(l_dist_index).amount * 100 / l_budgetset_total;
9304 	  end loop;
9305 
9306 	end if;
9307 
9308       else
9309 
9310 	if ((l_budgetset_index > 0) and (l_setdist_index > 1)) then
9311 	  for l_dist_index in l_budgetset_index..(l_setdist_index - 1) loop
9312 	    g_budgetset_dists(l_dist_index).percent := g_budgetset_dists(l_dist_index).amount * 100 / l_budgetset_total;
9313 	  end loop;
9314 	end if;
9315 
9316 	l_budgetset_total := g_budgetset_dists(l_setdist_index).amount;
9317 	l_budget_set_id := g_budgetset_dists(l_setdist_index).budget_set_id;
9318 	l_budgetset_index := l_setdist_index;
9319 
9320       end if;
9321 
9322     else
9323       l_budgetset_total := l_budgetset_total + g_budgetset_dists(l_setdist_index).amount;
9324     end if;
9325 
9326   END IF;
9327   -- End processing elements only when the cost is non-zero.
9328   END LOOP;
9329 
9330   -- Initialize API return status to success
9331   p_return_status := FND_API.G_RET_STS_SUCCESS;
9332 
9333 EXCEPTION
9334   /* start bug 3666828 */
9335   --
9336   /*WHEN FND_API.G_EXC_ERROR THEN
9337     p_return_status := FND_API.G_RET_STS_ERROR;
9338   --
9339   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
9340     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;*/
9341   --
9342   /* end bug 3666828 */
9343 
9344   WHEN OTHERS THEN
9345     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9346     --
9347     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
9348       FND_MSG_PUB.Add_Exc_Msg ( G_PKG_NAME,
9349                                 'Distribute_Position_Cost') ;
9350     END IF;
9351     --
9352 End Distribute_Position_Cost;
9353 /*---------------------------------------------------------------------------*/
9354 
9355 
9356 /*---------------------------------------------------------------------------*/
9357 PROCEDURE Create_Pqh_Budget_Version
9358  (p_return_status           OUT  NOCOPY  VARCHAR2,
9359   p_msg_count               OUT  NOCOPY  NUMBER,
9360   p_msg_data                OUT  NOCOPY  VARCHAR2,
9361   p_pqh_budget_version_id   OUT  NOCOPY  NUMBER,
9362   p_event_type              IN   VARCHAR2,
9363   p_source_id               IN   NUMBER,
9364   p_currency_code           IN   VARCHAR2,
9365   p_data_extract_id         IN   NUMBER,
9366   p_system_data_extract_id  IN   NUMBER,
9367   p_hr_budget_id            IN   NUMBER,
9368   p_pqh_budget_name         IN   VARCHAR2,
9369   p_start_date              IN   DATE,
9370   p_end_date                IN   DATE,
9371   p_budget_unit1            IN   VARCHAR2,
9372   p_budget_unit2            IN   VARCHAR2,
9373   p_budget_unit3            IN   VARCHAR2) IS
9374 
9375   l_version_found           BOOLEAN := FALSE;
9376   l_object_version_number   NUMBER;
9377   l_version_number          NUMBER;
9378   l_budget_version_id       NUMBER;
9379   l_budget_unit1_value      NUMBER;
9380   l_budget_unit2_value      NUMBER;
9381   l_budget_unit3_value      NUMBER;
9382   l_date_from               DATE;
9383   l_date_to                 DATE;
9384   l_original_fte            NUMBER;
9385   l_current_fte             NUMBER;
9386   l_revised_fte             NUMBER;
9387   l_revision_amount         NUMBER := 0;
9388   l_total_cost              NUMBER := 0;
9389   l_total_fte               NUMBER := 0;
9390 
9391   l_msg_count               NUMBER;
9392   l_msg_data                VARCHAR2(2000);
9393   l_return_status           VARCHAR2(1);
9394 
9395   /* bug no 3670254 */
9396   -- these local variables will hold the values
9397   -- of FTE and position ID
9398   TYPE l_position_id_tbl_type IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
9399   l_position_id_tbl         l_position_id_tbl_type;
9400 
9401   l_fte_attribute_value     NUMBER;
9402   l_position_id	    	    NUMBER;
9403   /* bug no 3670254 */
9404 
9405 
9406   cursor c_brversion is
9407     select version_number, budget_version_id, object_version_number, date_from, date_to,
9408 	   budget_unit1_value, budget_unit2_value, budget_unit3_value
9409       from pqh_psb_budget_versions
9410      where budget_id = p_hr_budget_id
9411        and version_number =
9412 	  (select max(version_number) from pqh_psb_budget_versions
9413 	    where budget_id = p_hr_budget_id);
9414 
9415   cursor c_bpversion is
9416     select version_number, budget_version_id, object_version_number
9417       from pqh_psb_budget_versions
9418      where budget_id = p_hr_budget_id
9419        and date_from = p_start_date
9420        and date_to = p_end_date;
9421 
9422   cursor c_total_cost_bp is
9423     select sum(ppc.element_cost) total_cost
9424       from psb_positions pp, psb_position_costs ppc
9425      where pp.data_extract_id = p_system_data_extract_id
9426        and exists
9427 	  (select 'Exists'
9428 	     from psb_positions pp1, psb_ws_position_lines wpl, psb_ws_lines_positions wlp
9429 	    where pp1.data_extract_id = p_data_extract_id
9430 	      and pp1.hr_position_id = pp.hr_position_id
9431 	      and wpl.position_id = pp1.position_id
9432 	      and wlp.position_line_id = wpl.position_line_id
9433 	      and wlp.worksheet_id = p_source_id)
9434        and ppc.position_id = pp.position_id
9435        and ppc.hr_budget_id = p_hr_budget_id
9436        and ppc.base_line_version = 'O'
9437        and ppc.currency_code = p_currency_code
9438        and ppc.budget_revision_id is null
9439        and ((ppc.start_date between p_start_date and p_end_date)
9440 	 or (ppc.end_date between p_start_date and p_end_date)
9441 	 or ((ppc.start_date < p_start_date) and (ppc.end_date > p_end_date)));
9442 
9443   cursor c_revision_amount is
9444     select sum(decode(b.revision_type, 'I', b.revision_amount,
9445 				       'D', -b.revision_amount)) revision_amount
9446       from psb_budget_revision_lines a,
9447 	   psb_budget_revision_accounts b
9448      where a.budget_revision_id = p_source_id
9449        and b.budget_revision_acct_line_id = a.budget_revision_acct_line_id
9450        and b.position_id is not null;
9451 
9452   cursor c_total_fte_bp is
9453     select sum(avg(ppf.fte)) total_fte
9454       from psb_positions pp, psb_position_fte ppf
9455      where pp.data_extract_id = p_system_data_extract_id
9456        and exists
9457 	  (select 'Exists'
9458 	     from psb_positions pp1, psb_ws_position_lines wpl, psb_ws_lines_positions wlp
9459 	    where pp1.data_extract_id = p_data_extract_id
9460 	      and pp1.hr_position_id = pp.hr_position_id
9461 	      and wpl.position_id = pp1.position_id
9462 	      and wlp.position_line_id = wpl.position_line_id
9463 	      and wlp.worksheet_id = p_source_id)
9464        and ppf.position_id = pp.position_id
9465        and ppf.hr_budget_id = p_hr_budget_id
9466        and ppf.base_line_version = 'O'
9467        and ppf.budget_revision_id is null
9468        and ((ppf.start_date between p_start_date and p_end_date)
9469 	 or (ppf.end_date between p_start_date and p_end_date)
9470 	 or ((ppf.start_date < p_start_date) and (ppf.end_date > p_end_date)))
9471      group by ppf.position_id;
9472 
9473   cursor c_revision_fte is
9474     select brp.position_id, brp.effective_start_date, brp.effective_end_date,
9475 	   brp.revision_type, brp.revision_value_type, brp.revision_value
9476       from psb_budget_revision_pos_lines brpl,
9477 	   psb_budget_revision_positions brp
9478      where brpl.budget_revision_id = p_source_id
9479        and brp.budget_revision_pos_line_id = brpl.budget_revision_pos_line_id;
9480 
9481   /* bug no 3670254 */
9482   -- This cursor is used to get the positions
9483   -- associated with the data extract.
9484   CURSOR l_position_csr
9485   IS
9486   SELECT position_id
9487   FROM   psb_ws_position_lines a, psb_ws_lines_positions b
9488   WHERE  worksheet_id = p_source_id
9489   AND    a.position_line_id = b.position_line_id;
9490   /* bug no 3670254 */
9491 
9492 
9493   l_api_name               CONSTANT VARCHAR2(30)   := 'Create_Pqh_Budget_Version';
9494 
9495 BEGIN
9496 
9497   if p_event_type = 'BP' then
9498   begin
9499 
9500     for c_version_rec in c_bpversion loop
9501       l_version_number := c_version_rec.version_number;
9502       l_budget_version_id := c_version_rec.budget_version_id;
9503       l_object_version_number := c_version_rec.object_version_number;
9504       l_date_from := p_start_date;
9505       l_date_to := p_end_date;
9506       l_version_found := TRUE;
9507     end loop;
9508 
9509     if not l_version_found then
9510       message_token('BUDGET', p_pqh_budget_name);
9511       message_token('STARTDATE', p_start_date);
9512       message_token('ENDDATE', p_end_date);
9513       add_message('PSB', 'PSB_PQH_BP_VERSION_NOTFOUND');
9514       raise FND_API.G_EXC_ERROR;
9515     end if;
9516 
9517     for c_total_cost_rec in c_total_cost_bp loop
9518       l_total_cost := c_total_cost_rec.total_cost;
9519     end loop;
9520 
9521     /* bug no 3670254 */
9522     -- This loop should execute only when the profile value
9523     -- is 'ASSIGNMENT'.
9524     IF g_hrms_fte_upload_option = 'ASSIGNMENT' THEN
9525       -- get the positions in worksheet
9526       OPEN l_position_csr;
9527       --FOR l_position_rec IN l_position_csr
9528       LOOP
9529         -- delete the table before the bulk fetch
9530         l_position_id_tbl.DELETE;
9531         FETCH l_position_csr BULK COLLECT INTO l_position_id_tbl LIMIT 1000;
9532 
9533         FOR l_pos_cnt IN 1..l_position_id_tbl.COUNT
9534         LOOP
9535           l_position_id := l_position_id_tbl(l_pos_cnt);
9536 	  l_fte_attribute_value := NULL;
9537 
9538           -- check is the position has worksheet level FTE
9539           FOR l_ass_fte_rec IN g_ass_fte_csr
9540           ( c_data_extract_id       => p_data_extract_id ,
9541             c_position_id           => l_position_id     ,
9542             c_worksheet_id          => p_source_id       ,
9543             c_attribute_id          => g_fte_attribute_id    ,
9544             c_budget_year_end_date  => p_end_date
9545           )
9546 	  LOOP
9547    	    l_fte_attribute_value := l_ass_fte_rec.attribute_value;
9548       	  END LOOP;
9549 
9550       	  IF l_fte_attribute_value IS NULL THEN
9551 
9552             -- check if the position has DE level FTE
9553       	    FOR l_ass_fte_rec IN g_ass_fte_csr
9554             ( c_data_extract_id       => p_data_extract_id ,
9555               c_position_id           => l_position_id     ,
9556               c_worksheet_id          => NULL              ,
9557               c_attribute_id          => g_fte_attribute_id    ,
9558               c_budget_year_end_date  => p_end_date
9559             )
9560 	    LOOP
9561     	      l_fte_attribute_value := l_ass_fte_rec.attribute_value;
9562       	    END LOOP;
9563       	  END IF;
9564 
9565           -- get the attribute value
9566       	  l_fte_attribute_value := Nvl(l_fte_attribute_value,0);
9567 
9568       	  -- fetch the FTE for all positions for that budget period
9569           l_total_fte := l_total_fte + l_fte_attribute_value;
9570         END LOOP;
9571         EXIT WHEN l_position_csr%NOTFOUND;
9572       END LOOP;
9573       CLOSE l_position_csr;
9574 
9575     ELSE
9576       FOR c_total_fte_rec in c_total_fte_bp
9577       LOOP
9578         l_total_fte := c_total_fte_rec.total_fte;
9579       END LOOP;
9580     END IF;
9581     /* bug no 3670254 */
9582 
9583     if nvl(p_budget_unit1, 'X') = 'FTE' then
9584        l_budget_unit1_value := l_total_fte;
9585     elsif nvl(p_budget_unit1, 'X') = 'MONEY' then
9586       l_budget_unit1_value := l_total_cost;
9587     end if;
9588 
9589     if nvl(p_budget_unit2, 'X') = 'FTE' then
9590       l_budget_unit2_value := l_total_fte;
9591     elsif nvl(p_budget_unit2, 'X') = 'MONEY' then
9592       l_budget_unit2_value := l_total_cost;
9593     end if;
9594 
9595     if nvl(p_budget_unit3, 'X') = 'FTE' then
9596       l_budget_unit3_value := l_total_fte;
9597     elsif nvl(p_budget_unit3, 'X') = 'MONEY' then
9598       l_budget_unit3_value := l_total_cost;
9599     end if;
9600 
9601   end;
9602   elsif p_event_type = 'BR' then
9603   begin
9604 
9605     for c_version_rec in c_brversion loop
9606       l_version_number := c_version_rec.version_number;
9607       l_budget_version_id := c_version_rec.budget_version_id;
9608       l_object_version_number := c_version_rec.object_version_number;
9609       l_budget_unit1_value := c_version_rec.budget_unit1_value;
9610       l_budget_unit2_value := c_version_rec.budget_unit2_value;
9611       l_budget_unit3_value := c_version_rec.budget_unit3_value;
9612       l_date_from := c_version_rec.date_from;
9613       l_date_to := c_version_rec.date_to;
9614       l_version_found := TRUE;
9615     end loop;
9616 
9617     if not l_version_found then
9618       message_token('BUDGET', p_pqh_budget_name);
9619       add_message('PSB', 'PSB_PQH_BR_VERSION_NOTFOUND');
9620       raise FND_API.G_EXC_ERROR;
9621     end if;
9622 
9623     for c_revision_amount_rec in c_revision_amount loop
9624       l_revision_amount := c_revision_amount_rec.revision_amount;
9625     end loop;
9626 
9627     l_total_fte := 0;
9628 
9629     for c_revision_fte_rec in c_revision_fte loop
9630 
9631       PSB_BUDGET_REVISIONS_PVT.Find_FTE
9632 	 (p_api_version => 1.0,
9633 	  p_return_status => l_return_status,
9634 	  p_msg_count => l_msg_count,
9635 	  p_msg_data => l_msg_data,
9636 	  p_position_id => c_revision_fte_rec.position_id,
9637 	  p_hr_budget_id => p_hr_budget_id,
9638 	  p_budget_revision_id => p_source_id,
9639 	  p_revision_type => c_revision_fte_rec.revision_type,
9640 	  p_revision_value_type => c_revision_fte_rec.revision_value_type,
9641 	  p_revision_value => c_revision_fte_rec.revision_value,
9642 	  p_effective_start_date => c_revision_fte_rec.effective_start_date,
9643 	  p_effective_end_date => c_revision_fte_rec.effective_end_date,
9644 	  p_original_fte => l_original_fte,
9645 	  p_current_fte => l_current_fte,
9646 	  p_revised_fte => l_revised_fte);
9647 
9648       l_total_fte := l_total_fte + (l_revised_fte - l_current_fte);
9649 
9650     end loop;
9651 
9652     if nvl(p_budget_unit1, 'X') = 'FTE' then
9653       l_budget_unit1_value := l_budget_unit1_value + l_total_fte;
9654     elsif nvl(p_budget_unit1, 'X') = 'MONEY' then
9655       l_budget_unit1_value := l_budget_unit1_value + l_revision_amount;
9656     end if;
9657 
9658     if nvl(p_budget_unit2, 'X') = 'FTE' then
9659       l_budget_unit2_value := l_budget_unit2_value + l_total_fte;
9660     elsif nvl(p_budget_unit2, 'X') = 'MONEY' then
9661       l_budget_unit2_value := l_budget_unit2_value + l_revision_amount;
9662     end if;
9663 
9664     if nvl(p_budget_unit3, 'X') = 'FTE' then
9665       l_budget_unit3_value := l_budget_unit3_value + l_total_fte;
9666     elsif nvl(p_budget_unit3, 'X') = 'MONEY' then
9667       l_budget_unit3_value := l_budget_unit3_value + l_revision_amount;
9668     end if;
9669 
9670   end;
9671   end if;
9672 
9673   begin
9674 
9675     pqh_psb_interface_api.update_budget_version
9676       (p_validate              => false,
9677        p_budget_id             => p_hr_budget_id,
9678        p_budget_version_id     => l_budget_version_id,
9679        p_version_number        => l_version_number,
9680        p_date_from             => l_date_from,
9681        p_date_to               => l_date_to,
9682        p_transfered_to_gl_flag => 'N',
9683        p_xfer_to_other_apps_cd => 'N',
9684        p_object_version_number => l_object_version_number,
9685        p_budget_unit1_value    => l_budget_unit1_value,
9686        p_budget_unit2_value    => l_budget_unit2_value,
9687        p_budget_unit3_value    => l_budget_unit3_value,
9688        p_effective_date        => sysdate);
9689 
9690   EXCEPTION
9691     when OTHERS then
9692       FND_MSG_PUB.Add;
9693       message_token('BUDGET', p_pqh_budget_name);
9694       message_token('VERSION', l_version_number);
9695       message_token('STARTDATE', p_start_date);
9696       message_token('ENDDATE', p_end_date);
9697       add_message('PSB', 'PSB_PQH_UPDATE_BUDGET_VERSION');
9698       raise FND_API.G_EXC_ERROR;
9699   end;
9700 
9701   p_pqh_budget_version_id := l_budget_version_id;
9702 
9703   -- Initialize API return status to success
9704   p_return_status := FND_API.G_RET_STS_SUCCESS;
9705 
9706 EXCEPTION
9707 
9708    when FND_API.G_EXC_ERROR then
9709      p_return_status := FND_API.G_RET_STS_ERROR;
9710 
9711      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
9712 				p_data  => p_msg_data);
9713 
9714    when FND_API.G_EXC_UNEXPECTED_ERROR then
9715      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9716 
9717      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
9718 				p_data  => p_msg_data);
9719 
9720    when OTHERS then
9721      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9722 
9723      if FND_MSG_PUB.Check_Msg_Level
9724        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
9725        FND_MSG_PUB.Add_Exc_Msg
9726 	  (p_pkg_name => G_PKG_NAME,
9727 	   p_procedure_name => l_api_name);
9728      end if;
9729 
9730      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
9731 				p_data  => p_msg_data);
9732 END Create_Pqh_Budget_Version;
9733 
9734 /* ----------------------------------------------------------------------- */
9735 
9736 FUNCTION Get_Segment_Val (p_index NUMBER) RETURN VARCHAR2 IS
9737   l_segment_value  VARCHAR2(25);
9738   l_index          BINARY_INTEGER;
9739 BEGIN
9740 
9741   for l_index in 1..g_map_tab.count loop
9742     if g_map_tab(l_index).cost_segment_name = 'SEGMENT' || p_index then
9743       l_segment_value := g_map_tab(l_index).segment_value;
9744       exit;
9745     end if;
9746   end loop;
9747 
9748   return l_segment_value;
9749 
9750 END Get_Segment_Val;
9751 
9752 /* ----------------------------------------------------------------------- */
9753 
9754 -- return cost allocation key flex ID for a GL CCID
9755 
9756 FUNCTION Get_Cost_Keyflex_ID
9757 (p_payroll_id           IN  NUMBER,
9758  p_gl_flex_code         IN  NUMBER,
9759  p_id_flex_num          IN  NUMBER,
9760  p_num_segments         IN  NUMBER,
9761  p_code_combination_id  IN  NUMBER) RETURN NUMBER IS
9762 
9763  l_cost_flex_id         NUMBER := 0;
9764  l_concat_segments      VARCHAR2(2000);
9765 
9766  l_dummy                BOOLEAN;
9767  l_no_of_segments       NUMBER;
9768  l_segment_array        FND_FLEX_EXT.SegmentArray;
9769 
9770 BEGIN
9771 
9772     if g_map_str IS NOT NULL then
9773     begin
9774       EXECUTE IMMEDIATE g_map_str into l_cost_flex_id USING p_id_flex_num, p_code_combination_id;
9775     exception
9776       when OTHERS then
9777 	l_cost_flex_id := 0;
9778     end;
9779     end if;
9780 
9781     if ((l_cost_flex_id = 0) and (nvl(g_map_tab.count, 0) <> 0)) then
9782     begin
9783 
9784       -- Fix for Bug: 3365072 - Start ...
9785       -- If the Cost Allocation KFF is not available then we have to
9786       -- create it. The PL/SQL table (g_map_tab) is populated with
9787       -- the segment values so that the call to Get_Segment_Val()
9788       -- returns the appropriate value which is used to call
9789       -- Hr_Entry.Maintain_Cost_Keyflex().
9790 
9791       l_dummy := fnd_flex_ext.get_segments( application_short_name => 'SQLGL',
9792                                             key_flex_code => 'GL#',
9793                                             structure_number => p_gl_flex_code,
9794                                             combination_id => p_code_combination_id,
9795                                             n_segments => l_no_of_segments,
9796                                             segments => l_segment_array );
9797 
9798       if not (l_dummy) then
9799         raise FND_API.G_EXC_ERROR;
9800       end if;
9801 
9802       for i in 1..30 loop
9803         g_map_tab(i).segment_value := null;
9804       end loop;
9805 
9806       for i in 1..l_no_of_segments loop
9807 
9808         for j in 1..30 loop
9809 
9810           if g_map_tab(j).gl_segment_name is not null
9811 	                 and g_map_tab(j).segment_value is null then
9812 
9813             g_map_tab(j).segment_value := l_segment_array(i);
9814 	    exit;
9815 
9816           end if;
9817 
9818 	end loop;
9819 
9820       end loop;
9821 
9822     -- Fix for Bug: 3365072 - ... End
9823 
9824       l_cost_flex_id := hr_entry.maintain_cost_keyflex(
9825 		   p_cost_keyflex_structure => p_id_flex_num,
9826 		   p_cost_allocation_keyflex_id => -1,
9827 		   p_concatenated_segments => null,
9828 		   p_summary_flag => 'N',
9829 		   p_start_date_active => null,
9830 		   p_end_date_active => null,
9831 		   p_segment1 => Get_Segment_Val(1),
9832 		   p_segment2 => Get_Segment_Val(2),
9833 		   p_segment3 => Get_Segment_Val(3),
9834 		   p_segment4 => Get_Segment_Val(4),
9835 		   p_segment5 => Get_Segment_Val(5),
9836 		   p_segment6 => Get_Segment_Val(6),
9837 		   p_segment7 => Get_Segment_Val(7),
9838 		   p_segment8 => Get_Segment_Val(8),
9839 		   p_segment9 => Get_Segment_Val(9),
9840 		   p_segment10 => Get_Segment_Val(10),
9841 		   p_segment11 => Get_Segment_Val(11),
9842 		   p_segment12 => Get_Segment_Val(12),
9843 		   p_segment13 => Get_Segment_Val(13),
9844 		   p_segment14 => Get_Segment_Val(14),
9845 		   p_segment15 => Get_Segment_Val(15),
9846 		   p_segment16 => Get_Segment_Val(16),
9847 		   p_segment17 => Get_Segment_Val(17),
9848 		   p_segment18 => Get_Segment_Val(18),
9849 		   p_segment19 => Get_Segment_Val(19),
9850 		   p_segment20 => Get_Segment_Val(20),
9851 		   p_segment21 => Get_Segment_Val(21),
9852 		   p_segment22 => Get_Segment_Val(22),
9853 		   p_segment23 => Get_Segment_Val(23),
9854 		   p_segment24 => Get_Segment_Val(24),
9855 		   p_segment25 => Get_Segment_Val(25),
9856 		   p_segment26 => Get_Segment_Val(26),
9857 		   p_segment27 => Get_Segment_Val(27),
9858 		   p_segment28 => Get_Segment_Val(28),
9859 		   p_segment29 => Get_Segment_Val(29),
9860 		   p_segment30 => Get_Segment_Val(30));
9861 
9862     EXCEPTION
9863       when OTHERS then
9864 	l_concat_segments := FND_FLEX_EXT.Get_Segs
9865 			       (application_short_name => 'SQLGL',
9866 				key_flex_code => 'GL#',
9867 				structure_number => p_gl_flex_code,
9868 				combination_id => p_code_combination_id);
9869 	message_token('GL_ACCOUNT', l_concat_segments);
9870 	add_message('PSB', 'PSB_PQH_MAINTAIN_COST_KEYFLEX');
9871 	l_cost_flex_id := 0;
9872 
9873     end;
9874     end if;
9875 
9876   return l_cost_flex_id;
9877 
9878 END Get_Cost_Keyflex_ID;
9879 
9880 /* ----------------------------------------------------------------------- */
9881 
9882 PROCEDURE Create_Pqh_Budget_Elements
9883 ( p_return_status           OUT  NOCOPY  VARCHAR2,
9884   p_msg_count               OUT  NOCOPY  NUMBER,
9885   p_msg_data                OUT  NOCOPY  VARCHAR2,
9886   p_event_type              IN   VARCHAR2,
9887   p_source_id               IN   NUMBER,
9888   p_business_group_id       IN   NUMBER,
9889   p_currency_code           IN   VARCHAR2,
9890   p_system_data_extract_id  IN   NUMBER,
9891   p_gl_flex_code            IN   NUMBER,
9892   p_pqh_budget_set_id       IN   NUMBER,
9893   p_dflt_budget_set_id      IN   NUMBER,
9894   p_hr_position_id          IN   NUMBER,
9895   p_payroll_id              IN   NUMBER,
9896   p_hr_budget_id            IN   NUMBER,
9897   p_position_name           IN   VARCHAR2,
9898   p_hr_employee_id          IN   NUMBER,
9899   p_effective_start_date    IN   DATE,
9900   p_period_start_date       IN   DATE,
9901   p_period_end_date         IN   DATE,
9902   p_num_segments            IN   NUMBER,
9903   p_id_flex_num             IN   NUMBER) IS
9904 
9905   l_pqh_budget_fund_src_id       NUMBER;
9906   l_pqh_budget_element_id        NUMBER;
9907   l_cost_keyflex_id              NUMBER;
9908   l_object_version_number        NUMBER;
9909   l_fund_object_version_number   NUMBER;
9910 
9911   cursor c_dflt_budget_elem is
9912     select dflt_budget_element_id,
9913 	   element_type_id,
9914 	   dflt_dist_percentage
9915       from pqh_psb_dflt_budget_elements
9916      where dflt_budget_set_id = p_dflt_budget_set_id;
9917 
9918   cursor c_pqh_budget_elements (elemtypeid NUMBER) is
9919     select *
9920       from pqh_psb_budget_elements
9921      where budget_set_id = p_pqh_budget_set_id
9922        and element_type_id = elemtypeid;
9923 
9924   cursor c_pqh_fund_srcs is
9925     select *
9926       from pqh_psb_budget_fund_srcs
9927      where budget_element_id = l_pqh_budget_element_id
9928        and cost_allocation_keyflex_id = l_cost_keyflex_id;
9929 
9930   c_pqh_budget_elements_rec      c_pqh_budget_elements%ROWTYPE;
9931   c_pqh_fund_srcs_rec            c_pqh_fund_srcs%ROWTYPE;
9932 
9933   l_api_name               CONSTANT VARCHAR2(30)   := 'Create_Pqh_Budget_Elements';
9934 
9935 BEGIN
9936 
9937   for c_dflt_budget_elem_rec in c_dflt_budget_elem loop
9938     open c_pqh_budget_elements (c_dflt_budget_elem_rec.element_type_id);
9939 
9940     fetch c_pqh_budget_elements into c_pqh_budget_elements_rec;
9941 
9942     if c_pqh_budget_elements%NOTFOUND then
9943     begin
9944 
9945       pqh_psb_interface_api.create_budget_element
9946 	(p_validate => false,
9947 	 p_budget_element_id => l_pqh_budget_element_id,
9948 	 p_budget_set_id => p_pqh_budget_set_id,
9949 	 p_element_type_id => c_dflt_budget_elem_rec.element_type_id,
9950 	 p_distribution_percentage => c_dflt_budget_elem_rec.dflt_dist_percentage,
9951 	 p_object_version_number => l_object_version_number);
9952 
9953     EXCEPTION
9954       when OTHERS then
9955 	FND_MSG_PUB.Add;
9956 	message_token('POSITION', p_position_name);
9957 	add_message('PSB', 'PSB_PQH_CREATE_BUDGET_ELEMENT');
9958 	raise FND_API.G_EXC_ERROR;
9959     end;
9960 
9961     else
9962       l_pqh_budget_element_id := c_pqh_budget_elements_rec.budget_element_id;
9963 
9964       begin
9965 	pqh_psb_interface_api.update_budget_element
9966 	  (p_validate => false,
9967 	   p_budget_element_id => c_pqh_budget_elements_rec.budget_element_id,
9968 	   p_budget_set_id => p_pqh_budget_set_id,
9969 	   p_element_type_id => c_dflt_budget_elem_rec.element_type_id,
9970 	   p_distribution_percentage => c_dflt_budget_elem_rec.dflt_dist_percentage,
9971 	   p_object_version_number => c_pqh_budget_elements_rec.object_version_number);
9972 
9973       EXCEPTION
9974 	 when OTHERS then
9975 	   FND_MSG_PUB.Add;
9976 	   message_token('POSITION', p_position_name);
9977 	   add_message('PSB', 'PSB_PQH_UPDATE_BUDGET_ELEMENT');
9978 	   raise FND_API.G_EXC_ERROR;
9979       end;
9980 
9981     end if;
9982 
9983     close c_pqh_budget_elements;
9984 
9985     for l_dist_index in 1..g_num_budgetset_dists loop
9986 
9987       if g_budgetset_dists(l_dist_index).budget_set_id = p_dflt_budget_set_id then
9988 
9989 	l_cost_keyflex_id := Get_Cost_Keyflex_ID (p_payroll_id => p_payroll_id,
9990 						  p_gl_flex_code => p_gl_flex_code,
9991 						  p_id_flex_num => p_id_flex_num,
9992 						  p_num_segments => p_num_segments,
9993 						  p_code_combination_id => g_budgetset_dists(l_dist_index).ccid);
9994 
9995 	if l_cost_keyflex_id = 0 then
9996       	  FND_MESSAGE.SET_NAME('PSB','PSB_DEBUG_MESSAGE');
9997       	  FND_MESSAGE.SET_TOKEN('MESSAGE', 'Cost Keyflex not found');
9998       	  FND_MSG_PUB.Add;
9999 	  raise FND_API.G_EXC_ERROR;
10000 	end if;
10001 
10002 	open c_pqh_fund_srcs;
10003 
10004 	fetch c_pqh_fund_srcs into c_pqh_fund_srcs_rec;
10005 
10006 	if c_pqh_fund_srcs%NOTFOUND then
10007 	begin
10008 
10009 	  pqh_psb_interface_api.create_budget_fund_src
10010 	     (p_validate                       =>  false,
10011 	      p_budget_fund_src_id             =>  l_pqh_budget_fund_src_id,
10012 	      p_budget_element_id              =>  l_pqh_budget_element_id,
10013 	      p_cost_allocation_keyflex_id     =>  l_cost_keyflex_id,
10014 	      p_distribution_percentage        =>  g_budgetset_dists(l_dist_index).percent,
10015 	      p_object_version_number          =>  l_fund_object_version_number);
10016 
10017 	EXCEPTION
10018 	  when OTHERS then
10019 	    FND_MSG_PUB.Add;
10020 	    message_token('POSITION', p_position_name);
10021 	    add_message('PSB', 'PSB_PQH_CREATE_BUDGET_FUND_SRC');
10022 	    raise FND_API.G_EXC_ERROR;
10023 	end;
10024 	else
10025 	begin
10026 
10027 	  l_pqh_budget_fund_src_id := c_pqh_fund_srcs_rec.budget_fund_src_id;
10028 
10029 	  pqh_psb_interface_api.update_budget_fund_src
10030 	     (p_validate                       =>  false,
10031 	      p_budget_fund_src_id             =>  c_pqh_fund_srcs_rec.budget_fund_src_id,
10032 	      p_budget_element_id              =>  l_pqh_budget_element_id,
10033 	      p_cost_allocation_keyflex_id     =>  l_cost_keyflex_id,
10034 	      p_distribution_percentage        =>  g_budgetset_dists(l_dist_index).percent,
10035 	      p_object_version_number          =>  c_pqh_fund_srcs_rec.object_version_number);
10036 
10037 	EXCEPTION
10038 	  when OTHERS then
10039 	    FND_MSG_PUB.Add;
10040 	     message_token('POSITION', p_position_name);
10041 	     add_message('PSB', 'PSB_PQH_UPDATE_BUDGET_FUND_SRC');
10042 	     raise FND_API.G_EXC_ERROR;
10043 
10044 	end;
10045 	end if;
10046 
10047 	close c_pqh_fund_srcs;
10048 
10049       end if;
10050 
10051     end loop;
10052 
10053   end loop;
10054 
10055   -- Initialize API return status to success
10056   p_return_status := FND_API.G_RET_STS_SUCCESS;
10057 
10058 EXCEPTION
10059 
10060    when FND_API.G_EXC_ERROR then
10061      p_return_status := FND_API.G_RET_STS_ERROR;
10062 
10063      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
10064 				p_data  => p_msg_data);
10065 
10066    when FND_API.G_EXC_UNEXPECTED_ERROR then
10067      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10068 
10069      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
10070 				p_data  => p_msg_data);
10071 
10072    when OTHERS then
10073      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10074 
10075      if FND_MSG_PUB.Check_Msg_Level
10076        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
10077        FND_MSG_PUB.Add_Exc_Msg
10078 	  (p_pkg_name => G_PKG_NAME,
10079 	   p_procedure_name => l_api_name);
10080      end if;
10081 
10082      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
10083 				p_data  => p_msg_data);
10084 
10085 END Create_Pqh_Budget_Elements;
10086 
10087 /* ----------------------------------------------------------------------- */
10088 
10089 PROCEDURE Create_Pqh_Budget_Periods
10090  (p_return_status           OUT  NOCOPY  VARCHAR2,
10091   p_msg_count               OUT  NOCOPY  NUMBER,
10092   p_msg_data                OUT  NOCOPY  VARCHAR2,
10093   p_event_type              IN   VARCHAR2,
10094   p_source_id               IN   NUMBER,
10095   p_hr_budget_id            IN   NUMBER,
10096   p_currency_code           IN   VARCHAR2,
10097   p_business_group_id       IN   NUMBER,
10098   p_system_data_extract_id  IN   NUMBER,
10099   p_gl_flex_code            IN   NUMBER,
10100   p_start_date              IN   DATE,
10101   p_end_date                IN   DATE,
10102   p_pqh_budget_detail_id    IN   NUMBER,
10103   p_hr_position_id          IN   NUMBER,
10104   p_payroll_id              IN   NUMBER,
10105   p_position_name           IN   VARCHAR2,
10106   p_hr_employee_id          IN   NUMBER,
10107   p_effective_start_date    IN   DATE,
10108   p_budget_unit1            IN   VARCHAR2,
10109   p_budget_unit2            IN   VARCHAR2,
10110   p_budget_unit3            IN   VARCHAR2,
10111   p_num_segments            IN   NUMBER,
10112   p_id_flex_num             IN   NUMBER ,
10113   /* bug no 3670254 */
10114   -- this parameter will have assignment level FTE
10115   -- which is used to populate periods
10116   p_assignment_fte	    IN 	 NUMBER DEFAULT NULL
10117   ) IS
10118 
10119   l_budget_unit1_value      NUMBER;
10120   l_budget_unit2_value      NUMBER;
10121   l_budget_unit3_value      NUMBER;
10122   l_pqh_budget_period_id    NUMBER;
10123   l_pqh_budget_set_id       NUMBER;
10124   l_start_time_period_id    NUMBER;
10125   l_end_time_period_id      NUMBER;
10126   l_period_cost             NUMBER;
10127   l_budget_set_cost         NUMBER;
10128   l_object_version_number   NUMBER;
10129   l_set_obj_ver_num         NUMBER;
10130   l_return_status           VARCHAR2(1);
10131 
10132   TYPE l_time_period_rec_type IS RECORD
10133      (time_period_id        NUMBER,
10134       start_date            DATE,
10135       end_date              DATE);
10136 
10137   TYPE l_time_period_tbl_type IS TABLE OF l_time_period_rec_type
10138      INDEX BY BINARY_INTEGER;
10139 
10140   l_time_period             l_time_period_tbl_type;
10141   l_num_time_period         NUMBER;
10142 
10143   /*For Bug No : 2434152 Start*/
10144   TYPE num_arr  IS TABLE OF NUMBER;
10145   TYPE date_arr IS TABLE OF DATE;
10146 
10147   TYPE period_rec IS RECORD (start_date date_arr, end_date date_arr,fte num_arr, cost num_arr);
10148   l_period_rec        period_rec;
10149 
10150   TYPE budget_set_cost_rec IS RECORD (hr_budget_set_id num_arr, budget_set_cost num_arr);
10151   l_bs_cost   budget_set_cost_rec;
10152 
10153   /*For Bug No : 2434152 End*/
10154 
10155   cursor c_period_rec is
10156     select a.start_date, a.end_date, a.fte, b.cost
10157       from
10158 	  (
10159 	   select ppf.start_date,
10160 		  ppf.end_date,
10161 		  sum(ppf.fte) fte
10162 	     from psb_position_fte ppf,
10163 		  psb_positions pp
10164 	    where pp.data_extract_id = p_system_data_extract_id
10165 	      and pp.hr_position_id = p_hr_position_id
10166 	      and ppf.position_id = pp.position_id
10167 	      and ppf.hr_budget_id = p_hr_budget_id
10168 	      and ((p_event_type = 'BP' and ppf.base_line_version = 'C')
10169 		   or (p_event_type = 'BR' and ppf.budget_revision_id = p_source_id))
10170 	      and ((ppf.start_date between p_start_date and p_end_date)
10171 		or (ppf.end_date between p_start_date and p_end_date)
10172 		or ((ppf.start_date < p_start_date) and (ppf.end_date > p_end_date)))
10173 	 group by ppf.start_date, ppf.end_date
10174 	  ) a,
10175 	  (select ppc.start_date, ppc.end_date, sum(ppc.element_cost) cost
10176 	     from psb_position_costs ppc, psb_positions pp
10177 	    where pp.data_extract_id = p_system_data_extract_id
10178 	      and pp.hr_position_id = p_hr_position_id
10179 	      and ppc.position_id = pp.position_id
10180 	      and ppc.hr_budget_id = p_hr_budget_id
10181 	      and ((p_event_type = 'BP' and ppc.base_line_version = 'C')
10182 		   or (p_event_type = 'BR' and ppc.budget_revision_id = p_source_id))
10183 	      and ppc.currency_code = p_currency_code
10184 	      and ((ppc.start_date between p_start_date and p_end_date)
10185 		or (ppc.end_date between p_start_date and p_end_date)
10186 		or ((ppc.start_date < p_start_date) and (ppc.end_date > p_end_date)))
10187 	 group by ppc.start_date, ppc.end_date
10188 	   ) b
10189        where b.start_date between a.start_date and a.end_date
10190 	   or b.end_date between a.start_date and a.end_date
10191 	   or ((b.start_date < a.start_date) and (b.end_date > a.end_date));
10192 
10193   cursor c_pqh_budget_periods (start_period_id NUMBER, end_period_id NUMBER) is
10194     select *
10195       from pqh_psb_budget_periods
10196      where budget_detail_id = p_pqh_budget_detail_id
10197        and start_time_period_id = start_period_id
10198        and end_time_period_id = end_period_id;
10199 
10200   cursor c_time_periods (startdate DATE) is
10201     select pt.time_period_id, pt.start_date, pt.end_date
10202       from per_time_periods pt, pqh_psb_budgets pb
10203      where pb.budget_id = p_hr_budget_id
10204        and pt.period_set_name = pb.period_set_name
10205        and startdate between pt.start_date and pt.end_date;
10206 
10207   cursor c_time_periods_br (startdate DATE, enddate DATE) is
10208     select pt.time_period_id, pt.start_date, pt.end_date
10209       from per_time_periods pt, pqh_psb_budgets pb
10210      where pb.budget_id = p_hr_budget_id
10211        and pt.period_set_name = pb.period_set_name
10212        and ((pt.start_date between startdate and enddate)
10213 	 or (pt.end_date between startdate and enddate)
10214 	 or ((pt.start_date < startdate) and (pt.end_date > enddate)));
10215 
10216   cursor c_budget_set_cost (startdate DATE, enddate DATE) is
10217     select ppe.budget_set_id hr_budget_set_id,
10218 	   sum(nvl(ppc.element_cost,0)) budget_set_cost
10219       from psb_pay_elements ppe,
10220 	   psb_position_costs ppc,
10221 	   psb_positions pp
10222      where ppe.data_extract_id = p_system_data_extract_id
10223        and pp.hr_position_id = p_hr_position_id
10224        and pp.data_extract_id = p_system_data_extract_id
10225        and ppc.position_id = pp.position_id
10226        and ppc.pay_element_id = ppe.pay_element_id
10227        and ppc.hr_budget_id = p_hr_budget_id
10228        and ((p_event_type = 'BP' and ppc.base_line_version = 'C')
10229 	   or (p_event_type = 'BR' and ppc.budget_revision_id = p_source_id))
10230        and ppc.currency_code = p_currency_code
10231        and ((ppc.start_date between startdate and enddate)
10232 	 or (ppc.end_date between startdate and enddate)
10233 	 or ((ppc.start_date < startdate) and (ppc.end_date > enddate)))
10234     group by ppe.budget_set_id;
10235 
10236   cursor c_pqh_budget_sets (hrbudgetsetid NUMBER) is
10237     select *
10238       from pqh_psb_budget_sets
10239      where budget_period_id = l_pqh_budget_period_id
10240        and dflt_budget_set_id = hrbudgetsetid;
10241 
10242   c_time_periods_rec        c_time_periods%ROWTYPE;
10243   c_pqh_budget_periods_rec  c_pqh_budget_periods%ROWTYPE;
10244   c_pqh_budget_sets_rec     c_pqh_budget_sets%ROWTYPE;
10245 
10246   l_msg_count                 NUMBER;
10247   l_msg_data                  VARCHAR2(2000);
10248 
10249   l_api_name               CONSTANT VARCHAR2(30)   := 'Create_Pqh_Budget_Periods';
10250 
10251 BEGIN
10252 
10253   open c_period_rec;
10254 
10255   loop
10256    fetch c_period_rec BULK COLLECT INTO l_period_rec.start_date,l_period_rec.end_date,l_period_rec.fte, l_period_rec.cost LIMIT g_limit_bulk_numrows;
10257 
10258    for l_fte_index in 1..l_period_rec.fte.count loop
10259 
10260     for l_init_index in 1..l_time_period.count loop
10261       l_time_period(l_init_index).time_period_id := null;
10262       l_time_period(l_init_index).start_date := null;
10263       l_time_period(l_init_index).end_date := null;
10264     end loop;
10265 
10266     l_num_time_period := 0;
10267 
10268     if p_event_type = 'BP' then
10269     begin
10270 
10271       for c_time_periods_rec in c_time_periods (l_period_rec.start_date(l_fte_index)) loop
10272 	l_num_time_period := l_num_time_period + 1;
10273 	l_time_period(l_num_time_period).time_period_id := c_time_periods_rec.time_period_id;
10274 	l_time_period(l_num_time_period).start_date := c_time_periods_rec.start_date;
10275 	l_time_period(l_num_time_period).end_date := c_time_periods_rec.end_date;
10276       end loop;
10277 
10278     end;
10279     elsif p_event_type = 'BR' then
10280     begin
10281 
10282       for c_time_periods_rec in c_time_periods_br(l_period_rec.start_date(l_fte_index), l_period_rec.end_date(l_fte_index)) loop
10283 	l_num_time_period := l_num_time_period + 1;
10284 	l_time_period(l_num_time_period).time_period_id := c_time_periods_rec.time_period_id;
10285 	l_time_period(l_num_time_period).start_date := c_time_periods_rec.start_date;
10286 	l_time_period(l_num_time_period).end_date := c_time_periods_rec.end_date;
10287       end loop;
10288 
10289     end;
10290     end if;
10291 
10292     if l_num_time_period = 0 then
10293       message_token('STARTDATE', l_period_rec.start_date(l_fte_index));
10294       add_message('PSB', 'PSB_PQH_TIME_PERIOD_NOT_FOUND');
10295       raise FND_API.G_EXC_ERROR;
10296     end if;
10297 
10298     l_period_cost := l_period_rec.cost(l_fte_index);
10299 
10300     if l_num_time_period > 1 then
10301        l_period_cost := l_period_cost / l_num_time_period;
10302     end if;
10303 
10304     for l_index in 1..l_num_time_period loop
10305 
10306       if nvl(p_budget_unit1, 'X') = 'FTE' then
10307         /* Bug No 3670254 */
10308         -- assign assignment level FTE to the variable
10309         IF p_assignment_fte IS NOT NULL THEN
10310           l_budget_unit1_value := p_assignment_fte;
10311         ELSE
10312 	  l_budget_unit1_value := l_period_rec.fte(l_fte_index);
10313 	END IF;
10314 	/* Bug No 3670254 */
10315       elsif nvl(p_budget_unit1, 'X') = 'MONEY' then
10316 	l_budget_unit1_value := l_period_cost;
10317       end if;
10318 
10319       if nvl(p_budget_unit2, 'X') = 'FTE' then
10320 
10321 	/* Bug No 3670254 */
10322         -- assign assignment level FTE to the variable
10323         IF p_assignment_fte IS NOT NULL THEN
10324           l_budget_unit2_value := p_assignment_fte;
10325         ELSE
10326 	  l_budget_unit2_value := l_period_rec.fte(l_fte_index);
10327 	END IF;
10328 	/* Bug No 3670254 */
10329 
10330       elsif nvl(p_budget_unit2, 'X') = 'MONEY' then
10331 	l_budget_unit2_value := l_period_cost;
10332       end if;
10333 
10334       if nvl(p_budget_unit3, 'X') = 'FTE' then
10335 
10336 	/* Bug No 3670254 */
10337         -- assign assignment level FTE to the variable
10338         IF p_assignment_fte IS NOT NULL THEN
10339           l_budget_unit3_value := p_assignment_fte;
10340         ELSE
10341 	  l_budget_unit3_value := l_period_rec.fte(l_fte_index);
10342 	END IF;
10343 	/* Bug No 3670254 */
10344 
10345       elsif nvl(p_budget_unit3, 'X') = 'MONEY' then
10346 	l_budget_unit3_value := l_period_cost;
10347       end if;
10348 
10349       l_start_time_period_id := l_time_period(l_index).time_period_id;
10350       l_end_time_period_id := l_time_period(l_index).time_period_id;
10351 
10352       open c_pqh_budget_periods(l_start_time_period_id, l_end_time_period_id);
10353 
10354       fetch c_pqh_budget_periods into c_pqh_budget_periods_rec;
10355 
10356       if c_pqh_budget_periods%NOTFOUND then
10357       begin
10358 
10359 	pqh_psb_interface_api.create_budget_period
10360 	  (p_validate => false,
10361 	   p_budget_period_id => l_pqh_budget_period_id,
10362 	   p_budget_detail_id => p_pqh_budget_detail_id,
10363 	   p_start_time_period_id => l_start_time_period_id,
10364 	   p_end_time_period_id => l_end_time_period_id,
10365 	   p_budget_unit1_value_type_cd => 'V',
10366 	   p_budget_unit1_value => l_budget_unit1_value,
10367 	   p_budget_unit2_value_type_cd => 'V',
10368 	   p_budget_unit2_value => l_budget_unit2_value,
10369 	   p_budget_unit3_value_type_cd => 'V',
10370 	   p_budget_unit3_value => l_budget_unit3_value,
10371 	   p_object_version_number => l_object_version_number);
10372 
10373       EXCEPTION
10374 	WHEN OTHERS THEN
10375 	  FND_MSG_PUB.Add;
10376 	  message_token('POSITION', p_position_name);
10377 	  add_message('PSB', 'PSB_PQH_CREATE_BUDGET_PERIOD');
10378 	  raise FND_API.G_EXC_ERROR;
10379       end;
10380       else
10381 	l_pqh_budget_period_id := c_pqh_budget_periods_rec.budget_period_id;
10382 
10383 	begin
10384 
10385 	  pqh_psb_interface_api.update_budget_period
10386 	    (p_validate => false,
10387 	     p_budget_period_id => c_pqh_budget_periods_rec.budget_period_id,
10388 	     p_budget_detail_id => c_pqh_budget_periods_rec.budget_detail_id,
10389 	     p_start_time_period_id => c_pqh_budget_periods_rec.start_time_period_id,
10390 	     p_end_time_period_id => c_pqh_budget_periods_rec.end_time_period_id,
10391 	     p_budget_unit1_value_type_cd => 'V',
10392 	     p_budget_unit1_value => l_budget_unit1_value,
10393 	     p_budget_unit2_value_type_cd => 'V',
10394 	     p_budget_unit2_value => l_budget_unit2_value,
10395 	     p_budget_unit3_value_type_cd => 'V',
10396 	     p_budget_unit3_value => l_budget_unit3_value,
10397 	     p_object_version_number => c_pqh_budget_periods_rec.object_version_number);
10398 
10399 	EXCEPTION
10400 	  when OTHERS then
10401 	    FND_MSG_PUB.Add;
10402 	    message_token('POSITION', p_position_name);
10403 	    add_message('PSB', 'PSB_PQH_UPDATE_BUDGET_PERIOD');
10404 	    raise FND_API.G_EXC_ERROR;
10405 	end;
10406       end if;
10407 
10408       close c_pqh_budget_periods;
10409 
10410       l_budget_unit1_value := null;
10411       l_budget_unit2_value := null;
10412       l_budget_unit3_value := null;
10413 
10414 	open c_budget_set_cost(l_period_rec.start_date(l_fte_index), l_period_rec.end_date(l_fte_index));
10415 	loop
10416 	 fetch c_budget_set_cost BULK COLLECT into l_bs_cost.hr_budget_set_id,l_bs_cost.budget_set_cost LIMIT g_limit_bulk_numrows;
10417 
10418        for l_bs_index in 1..l_bs_cost.hr_budget_set_id.count loop
10419 
10420 	l_budget_set_cost := l_bs_cost.budget_set_cost(l_bs_index);
10421 
10422 	if l_num_time_period > 1 then
10423 	  l_budget_set_cost := l_budget_set_cost / l_num_time_period;
10424 	end if;
10425 
10426 	if nvl(p_budget_unit1, 'X') = 'MONEY' then
10427 	  l_budget_unit1_value := l_budget_set_cost;
10428 	end if;
10429 
10430 	if nvl(p_budget_unit2, 'X') = 'MONEY' then
10431 	  l_budget_unit2_value := l_budget_set_cost;
10432 	end if;
10433 
10434 	if nvl(p_budget_unit3, 'X') = 'MONEY' then
10435 	  l_budget_unit3_value := l_budget_set_cost;
10436 	end if;
10437 
10438 	open c_pqh_budget_sets (l_bs_cost.hr_budget_set_id(l_bs_index));
10439 
10440 	fetch c_pqh_budget_sets into c_pqh_budget_sets_rec;
10441 
10442 	if c_pqh_budget_sets%NOTFOUND then
10443 	begin
10444 
10445 	  pqh_psb_interface_api.create_budget_set
10446 	    (p_validate => false,
10447 	     p_budget_set_id => l_pqh_budget_set_id,
10448 	     p_dflt_budget_set_id => l_bs_cost.hr_budget_set_id(l_bs_index),
10449 	     p_budget_period_id => l_pqh_budget_period_id,
10450 	     p_budget_unit1_value_type_cd => 'V',
10451 	     p_budget_unit1_value => l_budget_unit1_value,
10452 	     p_budget_unit2_value_type_cd => 'V',
10453 	     p_budget_unit2_value => l_budget_unit2_value,
10454 	     p_budget_unit3_value_type_cd => 'V',
10455 	     p_budget_unit3_value => l_budget_unit3_value,
10456 	     p_object_version_number => l_set_obj_ver_num,
10457 	     p_effective_date => sysdate);
10458 
10459 	EXCEPTION
10460 	  when OTHERS then
10461 	    FND_MSG_PUB.Add;
10462 	    message_token('POSITION', p_position_name);
10463 	    add_message('PSB', 'PSB_PQH_CREATE_BUDGET_SET');
10464 	    raise FND_API.G_EXC_ERROR;
10465 
10466 	end;
10467 	else
10468 
10469 	  l_pqh_budget_set_id := c_pqh_budget_sets_rec.budget_set_id;
10470 
10471 	  begin
10472 
10473 	    pqh_psb_interface_api.update_budget_set
10474 	       (p_validate => false,
10475 		p_budget_set_id => c_pqh_budget_sets_rec.budget_set_id,
10476 		p_dflt_budget_set_id => l_bs_cost.hr_budget_set_id(l_bs_index),
10477 		p_budget_period_id => l_pqh_budget_period_id,
10478 		p_budget_unit1_value_type_cd => 'V',
10479 		p_budget_unit1_value => l_budget_unit1_value,
10480 		p_budget_unit2_value_type_cd => 'V',
10481 		p_budget_unit2_value => l_budget_unit2_value,
10482 		p_budget_unit3_value_type_cd => 'V',
10483 		p_budget_unit3_value => l_budget_unit3_value,
10484 		p_object_version_number => c_pqh_budget_sets_rec.object_version_number,
10485 		p_effective_date => sysdate);
10486 
10487 	  EXCEPTION
10488 	    when OTHERS then
10489 	      FND_MSG_PUB.Add;
10490 	      message_token('POSITION', p_position_name);
10491 	      add_message('PSB', 'PSB_PQH_UPDATE_BUDGET_SET');
10492 	      raise FND_API.G_EXC_ERROR;
10493 	  end;
10494 	end if;
10495 
10496 	close c_pqh_budget_sets;
10497 
10498 	Create_Pqh_Budget_Elements
10499 	     (p_return_status => l_return_status,
10500               p_msg_count => l_msg_count,
10501               p_msg_data => l_msg_data,
10502 	      p_event_type => p_event_type,
10503 	      p_source_id => p_source_id,
10504 	      p_business_group_id => p_business_group_id,
10505 	      p_currency_code => p_currency_code,
10506 	      p_system_data_extract_id => p_system_data_extract_id,
10507 	      p_gl_flex_code => p_gl_flex_code,
10508 	      p_pqh_budget_set_id => l_pqh_budget_set_id,
10509 	      p_dflt_budget_set_id => l_bs_cost.hr_budget_set_id(l_bs_index),
10510 	      p_hr_position_id => p_hr_position_id,
10511 	      p_payroll_id => p_payroll_id,
10512 	      p_hr_budget_id => p_hr_budget_id,
10513 	      p_position_name => p_position_name,
10514 	      p_hr_employee_id => p_hr_employee_id,
10515 	      p_effective_start_date => p_effective_start_date,
10516 	      p_period_start_date => l_period_rec.start_date(l_fte_index),
10517 	      p_period_end_date => l_period_rec.end_date(l_fte_index),
10518 	      p_num_segments => p_num_segments,
10519 	      p_id_flex_num => p_id_flex_num);
10520 
10521 	if l_return_status <> FND_API.G_RET_STS_SUCCESS then
10522 	  raise FND_API.G_EXC_ERROR;
10523 	end if;
10524 
10525        end loop; /*End of Budget Cost index loop*/
10526 	 exit when c_budget_set_cost%NOTFOUND;
10527 
10528       end loop; /*End of Budget Cost Fetch*/
10529 	close c_budget_set_cost;
10530     end loop;
10531 
10532    end loop; /*End of fte Bulk fetch index loop*/
10533    exit when c_period_rec%NOTFOUND;
10534 
10535   end loop; /*End of fte Bulk fetch loop*/
10536   close c_period_rec;
10537 
10538   -- Initialize API return status to success
10539   p_return_status := FND_API.G_RET_STS_SUCCESS;
10540 
10541 EXCEPTION
10542 
10543    when FND_API.G_EXC_ERROR then
10544      if c_period_rec%ISOPEN then
10545        close c_period_rec;
10546      end if;
10547      if c_budget_set_cost%ISOPEN then
10548        close c_budget_set_cost;
10549      end if;
10550      p_return_status := FND_API.G_RET_STS_ERROR;
10551      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
10552 				p_data  => p_msg_data);
10553 
10554    when FND_API.G_EXC_UNEXPECTED_ERROR then
10555      if c_period_rec%ISOPEN then
10556        close c_period_rec;
10557      end if;
10558      if c_budget_set_cost%ISOPEN then
10559        close c_budget_set_cost;
10560      end if;
10561      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10562      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
10563 				p_data  => p_msg_data);
10564 
10565    when OTHERS then
10566      if c_period_rec%ISOPEN then
10567        close c_period_rec;
10568      end if;
10569      if c_budget_set_cost%ISOPEN then
10570        close c_budget_set_cost;
10571      end if;
10572      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10573 
10574      if FND_MSG_PUB.Check_Msg_Level
10575        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
10576        FND_MSG_PUB.Add_Exc_Msg
10577 	  (p_pkg_name => G_PKG_NAME,
10578 	   p_procedure_name => l_api_name);
10579      end if;
10580 
10581      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
10582 				p_data  => p_msg_data);
10583 
10584 END Create_Pqh_Budget_Periods;
10585 
10586 /* ----------------------------------------------------------------------- */
10587 
10588 PROCEDURE Map_Segments
10589 (p_return_status  OUT  NOCOPY  VARCHAR2,
10590  p_position_name  IN   VARCHAR2,
10591  p_payroll_id     IN   NUMBER,
10592  p_sob_id         IN   NUMBER) IS
10593 
10594  l_payroll_name   VARCHAR2(80);
10595  l_index          BINARY_INTEGER := 0;
10596 
10597   cursor c_map_segments is
10598     select *
10599       from pay_payroll_gl_flex_maps
10600      where payroll_id = p_payroll_id
10601        and gl_set_of_books_id = p_sob_id;
10602 
10603   cursor c_payroll_name is
10604     /* Changed to pay_all_payrolls_f as part of Bug 2519492 */
10605     select payroll_name from pay_all_payrolls_f where payroll_id = p_payroll_id;
10606 
10607 BEGIN
10608 
10609   g_map_str := NULL;
10610 
10611   for i in 1..30 loop
10612     g_map_tab(i).cost_segment_name := null;
10613   end loop;
10614 
10615   l_index := 0;
10616 
10617   for c_map_segments_rec in c_map_segments loop
10618 
10619     /*For Bug No : 2434152 Start*/
10620     IF c_map_segments_rec.payroll_cost_segment IS NOT NULL THEN
10621       IF g_map_str IS NULL THEN
10622 	g_map_str := 'SELECT cost_allocation_keyflex_id '||
10623 		      ' FROM pay_cost_allocation_keyflex cakf, gl_code_combinations glcc '||
10624 		     ' WHERE cakf.id_flex_num = :b1 '||
10625 		       ' AND cakf.'||c_map_segments_rec.payroll_cost_segment ||' = glcc.'||c_map_segments_rec.gl_account_segment ;
10626       ELSE
10627 	g_map_str := g_map_str || ' AND cakf.'||c_map_segments_rec.payroll_cost_segment ||' = glcc.'||c_map_segments_rec.gl_account_segment ;
10628       END IF ;
10629     END IF ;
10630     /*For Bug No : 2434152 End*/
10631 
10632     l_index := l_index + 1;
10633 
10634     -- Prepare for later query to get cff id from cost allocation FF table.
10635     IF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT1' THEN
10636       g_map_tab(1).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10637       g_map_tab(1).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10638     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT2' THEN
10639       g_map_tab(2).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10640       g_map_tab(2).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10641     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT3' THEN
10642       g_map_tab(3).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10643       g_map_tab(3).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10644     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT4' THEN
10645       g_map_tab(4).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10646       g_map_tab(4).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10647     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT5' THEN
10648       g_map_tab(5).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10649       g_map_tab(5).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10650     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT6' THEN
10651       g_map_tab(6).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10652       g_map_tab(6).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10653     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT7' THEN
10654       g_map_tab(7).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10655       g_map_tab(7).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10656     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT8' THEN
10657       g_map_tab(8).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10658       g_map_tab(8).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10659     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT9' THEN
10660       g_map_tab(9).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10661       g_map_tab(9).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10662     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT10' THEN
10663       g_map_tab(10).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10664       g_map_tab(10).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10665     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT11' THEN
10666       g_map_tab(11).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10667       g_map_tab(11).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10668     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT12' THEN
10669       g_map_tab(12).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10670       g_map_tab(12).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10671     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT13' THEN
10672       g_map_tab(13).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10673       g_map_tab(13).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10674     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT14' THEN
10675       g_map_tab(14).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10676       g_map_tab(14).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10677     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT15' THEN
10678       g_map_tab(15).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10679       g_map_tab(15).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10680     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT16' THEN
10681       g_map_tab(16).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10682       g_map_tab(16).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10683     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT17' THEN
10684       g_map_tab(17).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10685       g_map_tab(17).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10686     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT18' THEN
10687       g_map_tab(18).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10688       g_map_tab(18).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10689     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT19' THEN
10690       g_map_tab(19).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10691       g_map_tab(19).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10692     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT20' THEN
10693       g_map_tab(20).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10694       g_map_tab(20).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10695     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT21' THEN
10696       g_map_tab(21).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10697       g_map_tab(21).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10698     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT22' THEN
10699       g_map_tab(22).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10700       g_map_tab(22).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10701     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT23' THEN
10702       g_map_tab(23).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10703       g_map_tab(23).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10704     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT24' THEN
10705       g_map_tab(24).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10706       g_map_tab(24).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10707     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT25' THEN
10708       g_map_tab(25).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10709       g_map_tab(25).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10710     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT26' THEN
10711       g_map_tab(26).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10712       g_map_tab(26).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10713     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT27' THEN
10714       g_map_tab(27).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10715       g_map_tab(27).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10716     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT28' THEN
10717       g_map_tab(28).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10718       g_map_tab(28).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10719     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT29' THEN
10720       g_map_tab(29).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10721       g_map_tab(29).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10722     ELSIF c_map_segments_rec.PAYROLL_COST_SEGMENT = 'SEGMENT30' THEN
10723       g_map_tab(30).gl_segment_name   := c_map_segments_rec.GL_ACCOUNT_SEGMENT;
10724       g_map_tab(30).cost_segment_name := c_map_segments_rec.PAYROLL_COST_SEGMENT;
10725     END IF;
10726 
10727   end loop;
10728 
10729   if l_index = 0 then
10730   begin
10731 
10732     for c_payroll_rec in c_payroll_name loop
10733       l_payroll_name := c_payroll_rec.payroll_name;
10734       /* Bug 2519492 Start */
10735       exit;
10736       /* Bug 2519492 End */
10737     end loop;
10738 
10739     message_token('POSITION', p_position_name);
10740     message_token('PAYROLL', l_payroll_name);
10741     add_message('PSB', 'PSB_PQH_NO_PAYROLL_MAPPING');
10742     raise FND_API.G_EXC_ERROR;
10743 
10744   end;
10745   end if;
10746 
10747   /*For Bug No : 2434152 Start*/
10748   g_map_str := g_map_str||' AND glcc.code_combination_id= :b2' ;
10749   /*For Bug No : 2434152 End*/
10750 
10751   -- Initialize API return status to success
10752   p_return_status := FND_API.G_RET_STS_SUCCESS;
10753 
10754 EXCEPTION
10755 
10756    when FND_API.G_EXC_ERROR then
10757      p_return_status := FND_API.G_RET_STS_ERROR;
10758 
10759    when FND_API.G_EXC_UNEXPECTED_ERROR then
10760      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10761 
10762    when OTHERS then
10763      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10764 
10765 END Map_Segments;
10766 
10767 /* ----------------------------------------------------------------------- */
10768 
10769 PROCEDURE Create_Pqh_Budget_Detail
10770 ( p_return_status          OUT  NOCOPY  VARCHAR2,
10771   p_msg_count              OUT  NOCOPY  NUMBER,
10772   p_msg_data               OUT  NOCOPY  VARCHAR2,
10773   p_event_type             IN   VARCHAR2,
10774   p_source_id              IN   NUMBER,
10775   p_currency_code          IN   VARCHAR2,
10776   p_business_group_id      IN   NUMBER,
10777   p_data_extract_id        IN   NUMBER,
10778   p_system_data_extract_id IN   NUMBER,
10779   p_gl_flex_code           IN   NUMBER,
10780   p_hr_budget_id           IN   NUMBER,
10781   p_pqh_budget_name        IN   VARCHAR2,
10782   p_pqh_budget_version_id  IN   NUMBER,
10783   p_start_date             IN   DATE,
10784   p_end_date               IN   DATE,
10785   p_budget_unit1           IN   VARCHAR2,
10786   p_budget_unit2           IN   VARCHAR2,
10787   p_budget_unit3           IN   VARCHAR2,
10788   p_budget_unit1_aggregate IN   VARCHAR2,
10789   p_budget_unit2_aggregate IN   VARCHAR2,
10790   p_budget_unit3_aggregate IN   VARCHAR2,
10791   p_id_flex_num            IN   NUMBER) IS
10792 
10793   l_budget_unit1_value          NUMBER;
10794   l_budget_unit2_value          NUMBER;
10795   l_budget_unit3_value          NUMBER;
10796   l_orig_budget_unit1_value     NUMBER;
10797   l_orig_budget_unit2_value     NUMBER;
10798   l_orig_budget_unit3_value     NUMBER;
10799   l_period_avg_value            NUMBER;
10800   l_change_fte_value            NUMBER;
10801   l_object_version_number       pqh_psb_budget_details.object_version_number%TYPE;
10802   l_new_object_version_number   pqh_psb_budget_details.object_version_number%TYPE;
10803   l_pqh_budget_detail_id        NUMBER;
10804   l_total_cost                  NUMBER := 0;
10805   l_revision_amount             NUMBER := 0;
10806   l_position_id                 NUMBER;
10807   l_att_position_id             NUMBER;
10808   l_org_id                      NUMBER;
10809   l_job_id                      NUMBER;
10810   l_hr_employee_id              NUMBER;
10811   l_position_name               VARCHAR2(240);
10812   l_effective_start_date        DATE;
10813   l_ver_object_version_number   pqh_psb_budget_versions.object_version_number%TYPE;
10814   l_version_number              NUMBER;
10815   l_start_date                  DATE;
10816   l_end_date                    DATE;
10817   l_payroll_id                  NUMBER;
10818   l_return_status               VARCHAR2(1);
10819   /*For Bug No : 2434152 Start*/
10820   l_prev_payroll_id             NUMBER := -1;
10821   l_position_ctr                NUMBER := 0;
10822 
10823   TYPE id_arr IS TABLE OF NUMBER(15);
10824   TYPE num_arr IS TABLE OF NUMBER;
10825 
10826   TYPE pos_rec IS RECORD (hr_position_id id_arr, fte num_arr, cost num_arr);
10827   l_positions                   pos_rec;
10828   l_num_segments                NUMBER;
10829   l_sob_id                      NUMBER;
10830   /*For Bug No : 2434152 End*/
10831 
10832   cursor c_position is
10833     select a.fte_position_id, a.fte, b.cost
10834       from
10835 	  (
10836 	   select pp.hr_position_id fte_position_id, sum(avg_fte) fte
10837 	     from
10838 		 (select pp.position_id, avg(ppf.fte) avg_fte
10839 		    from psb_positions pp, psb_position_fte ppf
10840 		   where ppf.position_id = pp.position_id
10841 		     and ppf.base_line_version = 'C'
10842 		     and ppf.hr_budget_id = p_hr_budget_id
10843 		     and ((ppf.start_date between p_start_date and p_end_date)
10844 		       or (ppf.end_date between p_start_date and p_end_date)
10845 		       or ((ppf.start_date < p_start_date) and (ppf.end_date > p_end_date)))
10846 		   group by pp.position_id) pf, psb_positions pp
10847 	     where pp.data_extract_id = p_system_data_extract_id
10848 	       and pp.position_id = pf.position_id
10849 	     group by pp.hr_position_id
10850 	  ) a,
10851 	  (
10852 	   select pp.hr_position_id cost_position_id, sum(ppc.element_cost) cost
10853 	     from psb_position_costs ppc, psb_positions pp
10854 	    where pp.data_extract_id = p_system_data_extract_id
10855 	      and ppc.hr_budget_id = p_hr_budget_id
10856 	      and ppc.position_id = pp.position_id
10857 	      and ppc.base_line_version = 'C'
10858 	      and ppc.currency_code = p_currency_code
10859 	      and ((ppc.start_date between p_start_date and p_end_date)
10860 		or (ppc.end_date between p_start_date and p_end_date)
10861 		or ((ppc.start_date < p_start_date) and (ppc.end_date > p_end_date)))
10862 	     group by pp.hr_position_id
10863 	   ) b
10864      where a.fte_position_id = b.cost_position_id;
10865 
10866   cursor c_hr_position (positionid NUMBER) is
10867     select position_id, hr_employee_id, name, effective_start_date
10868       from psb_positions
10869      where data_extract_id = decode(p_event_type, 'BP', p_data_extract_id, p_system_data_extract_id)
10870        and hr_position_id = positionid
10871        and rownum < 2;
10872 
10873   cursor c_att_position (positionid NUMBER) is
10874     select position_id
10875       from psb_positions
10876      where data_extract_id = p_system_data_extract_id
10877        and hr_position_id = positionid
10878        and rownum < 2;
10879 
10880   cursor c_pqh_budget_details(positionid NUMBER) is
10881     select *
10882       from pqh_psb_budget_details
10883      where budget_version_id = p_pqh_budget_version_id
10884        and position_id = positionid;
10885 
10886   cursor c_pqh_budget_details_new is
10887     select *
10888       from pqh_psb_budget_details
10889      where budget_version_id = p_pqh_budget_version_id
10890        and budget_detail_id = l_pqh_budget_detail_id;
10891 
10892   cursor c_position_cost_br (positionid NUMBER) is
10893     select sum(decode(b.revision_type, 'I', b.revision_amount,
10894 				       'D', -b.revision_amount)) revision_amount
10895       from psb_budget_revision_lines a,
10896 	   psb_budget_revision_accounts b,
10897 	   psb_positions pp
10898      where a.budget_revision_id = p_source_id
10899        and b.budget_revision_acct_line_id = a.budget_revision_acct_line_id
10900        and pp.position_id = b.position_id
10901        and pp.data_extract_id = p_system_data_extract_id
10902        and pp.hr_position_id = positionid;
10903 
10904   cursor c_job(positionid NUMBER) is
10905     select a.attribute_id, a.attribute_value_id, c.attribute_value , hr_value_id
10906       from psb_position_assignments a,
10907 	   psb_attributes b,
10908 	   psb_attribute_values c
10909      where a.position_id  = positionid
10910        and a.data_extract_id = p_system_data_extract_id -- system_data_extract_id
10911        and a.attribute_id = b.attribute_id
10912        and b.system_attribute_type = 'JOB_CLASS'
10913        and c.attribute_value_id = a.attribute_value_id
10914        and c.data_extract_id = p_system_data_extract_id;
10915 
10916   cursor c_org(positionid NUMBER) is
10917     select a.attribute_id, a.attribute_value_id, c.attribute_value , hr_value_id
10918       from psb_position_assignments a,
10919 	   psb_attributes b,
10920 	   psb_attribute_values c
10921      where a.position_id  = positionid
10922        and a.data_extract_id = p_system_data_extract_id -- system_data_extract_id
10923        and a.attribute_id = b.attribute_id
10924        and b.system_attribute_type = 'ORG'
10925        and c.attribute_value_id = a.attribute_value_id
10926        and c.data_extract_id = p_system_data_extract_id;
10927 
10928   cursor c_version_details  is
10929     select *
10930       from pqh_psb_budget_versions
10931      where budget_version_id = p_pqh_budget_version_id;
10932 
10933   /*For Bug No : 2434152 Start*/
10934   cursor c_sob_id is
10935     select set_of_books_id
10936      from psb_data_extracts
10937     where data_extract_id = p_system_data_extract_id;
10938   /*For Bug No : 2434152 End*/
10939 
10940   c_pqh_budget_details_rec      c_pqh_budget_details%ROWTYPE;
10941 
10942   /* bug no 3670254 */
10943   -- local variable to hold assignment level FTE
10944   l_fte_attribute_value    NUMBER;
10945   /* bug no 3670254 */
10946 
10947   l_msg_count              NUMBER;
10948   l_msg_data               VARCHAR2(2000);
10949   l_api_name               CONSTANT VARCHAR2(30)   := 'Create_Pqh_Budget_Detail';
10950 
10951 BEGIN
10952 
10953   /*For Bug No : 2434152 Start*/
10954   for c_sob_id_rec in c_sob_id loop
10955     l_sob_id := c_sob_id_rec.set_of_books_id;
10956   end loop;
10957   /*For Bug No : 2434152 End*/
10958 
10959   l_change_fte_value := 0;
10960 
10961   open c_position;
10962   loop
10963 
10964     fetch c_position BULK COLLECT INTO l_positions.hr_position_id, l_positions.fte, l_positions.cost LIMIT g_limit_bulk_numrows;
10965 
10966     for l_position_index in 1..l_positions.hr_position_id.count loop
10967 
10968       /*For Bug No : 2434152 Start*/
10969       l_position_ctr := l_position_ctr + 1;
10970       /*For Bug No : 2434152 End*/
10971 
10972       for c_hr_position_rec in c_hr_position (l_positions.hr_position_id(l_position_index)) loop
10973 	l_position_id := c_hr_position_rec.position_id;
10974 	l_hr_employee_id := c_hr_position_rec.hr_employee_id;
10975 	l_position_name := c_hr_position_rec.name;
10976 	l_effective_start_date := c_hr_position_rec.effective_start_date;
10977        /*For Bug No : 2292003 Start*/
10978        --end loop is moved to end of the procedure
10979        --end loop;
10980 
10981        /*l_att_position_id := null;
10982        for c_att_position_rec in c_att_position(l_positions.hr_position_id(l_position_index)) loop
10983 	 l_att_position_id := c_att_position_rec.position_id;
10984        end loop;*/
10985        /*For Bug No : 2292003 End*/
10986 
10987        if (Position_Exists(p_event_type => p_event_type, p_source_id => p_source_id,
10988 	   p_position_id => l_position_id)) then
10989        begin
10990 
10991 	 /*For Bug No : 2292003 Start*/
10992 	 l_att_position_id := null;
10993 	 for c_att_position_rec in c_att_position(l_positions.hr_position_id(l_position_index)) loop
10994 	   l_att_position_id := c_att_position_rec.position_id;
10995 	 end loop;
10996 	 /*For Bug No : 2292003 End*/
10997 
10998 	 /*For Bug No : 2292003 Start*/
10999 	 --added the p_effective_end_date in the following
11000 	 --call of the procedure and changed the date parameters also
11001 	 l_payroll_id := Get_Payroll(p_hr_position_id => l_positions.hr_position_id(l_position_index), p_data_extract_id => p_system_data_extract_id,
11002 				     p_effective_start_date => p_start_date,
11003 				     p_effective_end_date   => p_end_date);
11004 	 /*For Bug No : 2292003 End*/
11005 
11006 	 if l_payroll_id is null then
11007 	   message_token('POSITION', l_position_name);
11008 	   add_message('PSB', 'PSB_PQH_POSITION_NO_PAYROLL');
11009 	   raise FND_API.G_EXC_ERROR;
11010 	 end if;
11011 
11012 	 /*For Bug No : 2434152 Start*/
11013 	 if ((l_prev_payroll_id = -1) or (l_prev_payroll_id <> l_payroll_id)) then
11014 	   l_prev_payroll_id := l_payroll_id;
11015 	   Map_Segments (p_payroll_id => l_payroll_id, p_sob_id => l_sob_id, p_position_name => l_position_name, p_return_status => l_return_status);
11016 	 end if;
11017 	 /*For Bug No : 2434152 End*/
11018 
11019 	 if l_return_status <> FND_API.G_RET_STS_SUCCESS then
11020 	   raise FND_API.G_EXC_ERROR;
11021 	 end if;
11022 
11023 	 Distribute_Position_Cost
11024 		   (p_return_status => l_return_status,
11025 		    p_event_type => p_event_type,
11026 		    p_source_id => p_source_id,
11027 		    p_hr_budget_id => p_hr_budget_id,
11028 		    p_data_extract_id => p_system_data_extract_id,
11029 		    p_gl_flex_code => p_gl_flex_code,
11030 		    p_currency_code => p_currency_code,
11031 		    p_hr_position_id => l_positions.hr_position_id(l_position_index),
11032 		    p_start_date => p_start_date,
11033 		    p_end_date => p_end_date);
11034 
11035 	 if l_return_status <> FND_API.G_RET_STS_SUCCESS then
11036            /* Start Bug 3666828 */
11037 	   FND_FILE.PUT_LINE(FND_FILE.LOG, 'Position Name : '||l_position_name||' Position ID :'||l_positions.hr_position_id(l_position_index));
11038 	   /* End Bug 3666828 */
11039 
11040 	   raise FND_API.G_EXC_ERROR;
11041 	 end if;
11042 
11043 	 l_org_id := null;
11044 	 l_job_id := null;
11045 
11046 	 for c_job_rec in c_job(l_att_position_id) loop
11047 	   l_job_id := c_job_rec.hr_value_id;
11048 	 end loop;
11049 
11050 	 for c_org_rec in c_org(l_att_position_id) loop
11051 	   l_org_id := c_org_rec.hr_value_id;
11052 	 end loop;
11053 
11054 	 if p_event_type = 'BP' then
11055 	 begin
11056 
11057 	   l_total_cost := l_positions.cost(l_position_index);
11058 
11059            /* bug no 3670254 */
11060            -- get assignment level FTE is the profile value is
11061            -- 'ASSIGNMENT'.
11062            IF g_hrms_fte_upload_option = 'ASSIGNMENT' THEN
11063              l_fte_attribute_value := NULL;
11064 
11065              FOR l_ass_fte_rec IN g_ass_fte_csr
11066                ( c_data_extract_id      => p_data_extract_id ,
11067                  c_position_id          => l_position_id     ,
11068                  c_worksheet_id         => p_source_id       ,
11069                  c_attribute_id         => g_fte_attribute_id    ,
11070                  c_budget_year_end_date => p_end_date
11071                )
11072 	     LOOP
11073    	       l_fte_attribute_value := l_ass_fte_rec.attribute_value;
11074       	     END LOOP;
11075 
11076       	     IF l_fte_attribute_value IS NULL THEN
11077       	       FOR l_ass_fte_rec IN g_ass_fte_csr
11078                  ( c_data_extract_id       => p_data_extract_id ,
11079                    c_position_id           => l_position_id     ,
11080                    c_worksheet_id          => NULL              ,
11081                    c_attribute_id          => g_fte_attribute_id    ,
11082                    c_budget_year_end_date  => p_end_date
11083                  )
11084 	       LOOP
11085     	         l_fte_attribute_value := l_ass_fte_rec.attribute_value;
11086       	       END LOOP;
11087       	     END IF;
11088              -- get the attribute value
11089       	     l_fte_attribute_value := Nvl(l_fte_attribute_value,0);
11090            END IF;
11091            /* bug no 3670254 */
11092 
11093 	   if nvl(p_budget_unit1, 'X') = 'FTE' then
11094              /* bug 3670254 */
11095              -- assign assignment level FTE to local variable
11096 	     IF g_hrms_fte_upload_option = 'ASSIGNMENT' THEN
11097                  l_budget_unit1_value := l_fte_attribute_value;
11098              ELSE
11099 	         l_budget_unit1_value := l_positions.fte(l_position_index);
11100              END IF;
11101              /* bug no 3670254 */
11102 
11103 	   elsif nvl(p_budget_unit1, 'X') = 'MONEY' then
11104 	     l_budget_unit1_value := l_total_cost;
11105 	   end if;
11106 
11107 	   if nvl(p_budget_unit2, 'X') = 'FTE' then
11108 	     /* bug 3670254 */
11109              -- assign assignment level FTE to local variable
11110 	     IF g_hrms_fte_upload_option = 'ASSIGNMENT' THEN
11111                  l_budget_unit2_value := l_fte_attribute_value;
11112              ELSE
11113 	         l_budget_unit2_value := l_positions.fte(l_position_index);
11114              END IF;
11115              /* bug no 3670254 */
11116 	   elsif nvl(p_budget_unit2, 'X') = 'MONEY' then
11117 	     l_budget_unit2_value := l_total_cost;
11118 	   end if;
11119 
11120 	   if nvl(p_budget_unit3, 'X') = 'FTE' then
11121 
11122 	     /* bug 3670254 */
11123              -- assign assignment level FTE to local variable
11124 	     IF g_hrms_fte_upload_option = 'ASSIGNMENT' THEN
11125                  l_budget_unit3_value := l_fte_attribute_value;
11126              ELSE
11127 	         l_budget_unit3_value := l_positions.fte(l_position_index);
11128              END IF;
11129              /* bug no 3670254 */
11130 
11131 	   elsif nvl(p_budget_unit3, 'X') = 'MONEY' then
11132 	     l_budget_unit3_value := l_total_cost;
11133 	   end if;
11134 
11135 	 end;
11136 	 elsif p_event_type = 'BR' then
11137 	 begin
11138 
11139 	   for c_position_cost_rec in c_position_cost_br(l_positions.hr_position_id(l_position_index)) loop
11140 	     l_revision_amount := c_position_cost_rec.revision_amount;
11141 	   end loop;
11142 
11143 	 end;
11144 	 end if;
11145 
11146 	 open c_pqh_budget_details(l_positions.hr_position_id(l_position_index));
11147 
11148 	 fetch c_pqh_budget_details into c_pqh_budget_details_rec;
11149 
11150 	 if c_pqh_budget_details%NOTFOUND then
11151 	 begin
11152 
11153 	   if p_event_type = 'BR' then
11154 	   begin
11155 
11156 	     if nvl(p_budget_unit1, 'X') = 'FTE' then
11157 	       l_budget_unit1_value := l_positions.fte(l_position_index);
11158 	     elsif nvl(p_budget_unit1, 'X') = 'MONEY' then
11159 	       l_budget_unit1_value := l_revision_amount;
11160 	     end if;
11161 
11162 	     if nvl(p_budget_unit2, 'X') = 'FTE' then
11163 	       l_budget_unit2_value := l_positions.fte(l_position_index);
11164 	     elsif nvl(p_budget_unit2, 'X') = 'MONEY' then
11165 	       l_budget_unit2_value := l_revision_amount;
11166 	     end if;
11167 
11168 	     if nvl(p_budget_unit3, 'X') = 'FTE' then
11169 	       l_budget_unit3_value := l_positions.fte(l_position_index);
11170 	     elsif nvl(p_budget_unit3, 'X') = 'MONEY' then
11171 	       l_budget_unit3_value := l_revision_amount;
11172 	     end if;
11173 
11174 	   end;
11175 	   end if;
11176 
11177 	   pqh_psb_interface_api.create_budget_detail
11178 	      (p_validate => false,
11179 	       p_budget_detail_id => l_pqh_budget_detail_id,
11180 	       p_position_id => l_positions.hr_position_id(l_position_index),
11181 	       p_job_id => l_job_id,
11182 	       p_organization_id => l_org_id,
11183 	       p_budget_version_id => p_pqh_budget_version_id,
11184 	       p_budget_unit1_value_type_cd => 'V',
11185 	       p_budget_unit1_value => l_budget_unit1_value,
11186 	       p_budget_unit2_value_type_cd => 'V',
11187 	       p_budget_unit2_value => l_budget_unit2_value,
11188 	       p_budget_unit3_value_type_cd => 'V',
11189 	       p_budget_unit3_value => l_budget_unit3_value,
11190 	       p_object_version_number => l_object_version_number);
11191 
11192 	 EXCEPTION
11193 	   WHEN OTHERS THEN
11194 	     FND_MSG_PUB.Add;
11195 	     message_token('POSITION', l_position_name);
11196 	     add_message('PSB', 'PSB_PQH_CREATE_BUDGET_DETAIL');
11197 	     raise FND_API.G_EXC_ERROR;
11198 
11199 	 end;
11200 	 else
11201 	 begin
11202 
11203 	   if p_event_type = 'BR' then
11204 	   begin
11205 
11206 	     if nvl(p_budget_unit1, 'X') = 'FTE' then
11207 	       l_budget_unit1_value := l_positions.fte(l_position_index);
11208 	     elsif nvl(p_budget_unit1, 'X') = 'MONEY' then
11209 	       l_budget_unit1_value := c_pqh_budget_details_rec.budget_unit1_value + l_revision_amount;
11210 	     end if;
11211 
11212 	     if nvl(p_budget_unit2, 'X') = 'FTE' then
11213 	       l_budget_unit2_value := l_positions.fte(l_position_index);
11214 	     elsif nvl(p_budget_unit2, 'X') = 'MONEY' then
11215 	       l_budget_unit2_value := c_pqh_budget_details_rec.budget_unit2_value + l_revision_amount;
11216 	     end if;
11217 
11218 	     if nvl(p_budget_unit3, 'X') = 'FTE' then
11219 	       l_budget_unit3_value := l_positions.fte(l_position_index);
11220 	     elsif nvl(p_budget_unit3, 'X') = 'MONEY' then
11221 	       l_budget_unit3_value := c_pqh_budget_details_rec.budget_unit3_value + l_revision_amount;
11222 	     end if;
11223 
11224 	   end;
11225 	   end if;
11226 
11227 	   l_pqh_budget_detail_id := c_pqh_budget_details_rec.budget_detail_id;
11228 	   l_object_version_number := c_pqh_budget_details_rec.object_version_number;
11229 
11230 	   pqh_psb_interface_api.update_budget_detail
11231 	     (p_validate => false,
11232 	      p_budget_detail_id => c_pqh_budget_details_rec.budget_detail_id,
11233 	      p_position_id => l_positions.hr_position_id(l_position_index),
11234 	      p_job_id => l_job_id,
11235 	      p_organization_id => l_org_id,
11236 	      p_budget_version_id => c_pqh_budget_details_rec.budget_version_id,
11237 	      p_budget_unit1_value_type_cd => 'V',
11238 	      p_budget_unit1_value => l_budget_unit1_value,
11239 	      p_budget_unit2_value_type_cd => 'V',
11240 	      p_budget_unit2_value => l_budget_unit2_value,
11241 	      p_budget_unit3_value_type_cd => 'V',
11242 	      p_budget_unit3_value => l_budget_unit3_value,
11243 	      p_object_version_number => c_pqh_budget_details_rec.object_version_number);
11244 
11245 	 EXCEPTION
11246 	   when OTHERS then
11247 	     FND_MSG_PUB.Add;
11248 	     message_token('POSITION', l_position_name);
11249 	     add_message('PSB', 'PSB_PQH_UPDATE_BUDGET_DETAIL');
11250 	     raise FND_API.G_EXC_ERROR;
11251 
11252 	 end;
11253 	 end if;
11254 
11255 	 close c_pqh_budget_details;
11256 
11257 	 Create_Pqh_Budget_Periods
11258 	       (p_return_status => l_return_status,
11259                 p_msg_count => l_msg_count,
11260                 p_msg_data => l_msg_data,
11261 		p_event_type => p_event_type,
11262 		p_source_id   => p_source_id,
11263 		p_hr_budget_id  => p_hr_budget_id,
11264 		p_business_group_id => p_business_group_id,
11265 		p_currency_code => p_currency_code,
11266 		p_system_data_extract_id => p_system_data_extract_id,
11267 		p_gl_flex_code => p_gl_flex_code,
11268 		p_start_date => p_start_date,
11269 		p_end_date => p_end_date,
11270 		p_pqh_budget_detail_id => l_pqh_budget_detail_id,
11271 		p_hr_position_id => l_positions.hr_position_id(l_position_index),
11272 		p_payroll_id => l_payroll_id,
11273 		p_position_name => l_position_name,
11274 		p_hr_employee_id => l_hr_employee_id,
11275 		p_effective_start_date => l_effective_start_date,
11276 		p_budget_unit1 => p_budget_unit1,
11277 		p_budget_unit2 => p_budget_unit2,
11278 		p_budget_unit3 => p_budget_unit3,
11279 		p_num_segments => l_num_segments,
11280 		p_id_flex_num => p_id_flex_num ,
11281                 /* bug no 3670254 */
11282                 -- passing assignment level FTE to parameter
11283                 -- if profile value is PERIOD, then attribute value is null
11284                 -- and data flows according to existing system functionality.
11285                 p_assignment_fte => l_fte_attribute_value);
11286 
11287 	 if l_return_status <> FND_API.G_RET_STS_SUCCESS then
11288 	   raise FND_API.G_EXC_ERROR;
11289 	 end if;
11290 
11291 	 if p_event_type = 'BR' then
11292 	 begin
11293 
11294 	   if ((nvl(p_budget_unit1_aggregate, 'X') = 'AVERAGE') or (nvl(p_budget_unit2_aggregate, 'X') = 'AVERAGE') or
11295 	       (nvl(p_budget_unit3_aggregate, 'X') = 'AVERAGE')) then
11296 	   begin
11297 
11298 	     l_period_avg_value := 0;
11299 
11300 	     for c_pqh_budget_details_rec_new in c_pqh_budget_details_new loop
11301 		 l_new_object_version_number := c_pqh_budget_details_rec_new.object_version_number;
11302 	     end loop;
11303 
11304 	     if (nvl(p_budget_unit1, 'X') = 'FTE' and nvl(p_budget_unit1_aggregate, 'X') = 'AVERAGE') then
11305 	     begin
11306 
11307 	       for c_period_avg_rec in
11308 		  (select avg(budget_unit1_value) period_avg_value
11309 		     from pqh_psb_budget_periods
11310 		    where budget_detail_id = l_pqh_budget_detail_id) loop
11311 		 l_period_avg_value := c_period_avg_rec.period_avg_value;
11312 	       end loop;
11313 
11314 	       begin
11315 
11316 		 pqh_psb_interface_api.update_budget_detail
11317 		    (p_validate => false,
11318 		     p_budget_detail_id => l_pqh_budget_detail_id,
11319 		     p_position_id => l_positions.hr_position_id(l_position_index),
11320 		     p_budget_version_id => p_pqh_budget_version_id,
11321 		     p_budget_unit1_value_type_cd => 'V',
11322 		     p_budget_unit1_value => l_period_avg_value,
11323 		     p_object_version_number => l_new_object_version_number);
11324 
11325 	       EXCEPTION
11326 		 when OTHERS then
11327 		   FND_MSG_PUB.Add;
11328 		   message_token('POSITION', l_position_name);
11329 		   add_message('PSB', 'PSB_PQH_UPDATE_BUDGET_DETAIL');
11330 		   raise FND_API.G_EXC_ERROR;
11331 
11332 	       end;
11333 
11334 	       l_change_fte_value := l_change_fte_value + (l_period_avg_value - l_budget_unit1_value);
11335 
11336 	     end;
11337 	     elsif (nvl(p_budget_unit2, 'X') = 'FTE' and nvl(p_budget_unit2_aggregate, 'X') = 'AVERAGE') then
11338 	     begin
11339 
11340 	       for c_period_avg_rec in
11341 		  (select avg(budget_unit2_value) period_avg_value
11342 		     from pqh_psb_budget_periods
11343 		    where budget_detail_id = l_pqh_budget_detail_id) loop
11344 		 l_period_avg_value := c_period_avg_rec.period_avg_value;
11345 	       end loop;
11346 
11347 	       begin
11348 
11349 		 pqh_psb_interface_api.update_budget_detail
11350 		    (p_validate => false,
11351 		     p_budget_detail_id => l_pqh_budget_detail_id,
11352 		     p_position_id => l_positions.hr_position_id(l_position_index),
11353 		     p_budget_version_id => p_pqh_budget_version_id,
11354 		     p_budget_unit2_value_type_cd => 'V',
11355 		     p_budget_unit2_value => l_period_avg_value,
11356 		     p_object_version_number => l_new_object_version_number);
11357 
11358 	       EXCEPTION
11359 		 when OTHERS then
11360 		   FND_MSG_PUB.Add;
11361 		   message_token('POSITION', l_position_name);
11362 		   add_message('PSB', 'PSB_PQH_UPDATE_BUDGET_DETAIL');
11363 		   raise FND_API.G_EXC_ERROR;
11364 
11365 	       end;
11366 
11367 	       l_change_fte_value := l_change_fte_value + (l_period_avg_value - l_budget_unit2_value);
11368 
11369 	     end;
11370 	     elsif (nvl(p_budget_unit3, 'X') = 'FTE' and nvl(p_budget_unit3_aggregate, 'X') = 'AVERAGE') then
11371 	     begin
11372 
11373 	       for c_period_avg_rec in
11374 		  (select avg(budget_unit3_value) period_avg_value
11375 		     from pqh_psb_budget_periods
11376 		    where budget_detail_id = l_pqh_budget_detail_id) loop
11377 		 l_period_avg_value := c_period_avg_rec.period_avg_value;
11378 	       end loop;
11379 
11380 	       begin
11381 
11382 		 pqh_psb_interface_api.update_budget_detail
11383 		    (p_validate => false,
11384 		     p_budget_detail_id => l_pqh_budget_detail_id,
11385 		     p_position_id => l_positions.hr_position_id(l_position_index),
11386 		     p_budget_version_id => p_pqh_budget_version_id,
11387 		     p_budget_unit3_value_type_cd => 'V',
11388 		     p_budget_unit3_value => l_period_avg_value,
11389 		     p_object_version_number => l_new_object_version_number);
11390 
11391 	       EXCEPTION
11392 		 when OTHERS then
11393 		   FND_MSG_PUB.Add;
11394 		   message_token('POSITION', l_position_name);
11395 		   add_message('PSB', 'PSB_PQH_UPDATE_BUDGET_DETAIL');
11396 		   raise FND_API.G_EXC_ERROR;
11397 
11398 	       end;
11399 
11400 	       l_change_fte_value := l_change_fte_value + (l_period_avg_value - l_budget_unit3_value);
11401 
11402 	     end;
11403 	     end if;
11404 
11405 	     for c_version_details_rec in c_version_details loop
11406 	       l_ver_object_version_number := c_version_details_rec.object_version_number;
11407 	       l_version_number := c_version_details_rec.version_number;
11408 	       l_start_date := c_version_details_rec.date_from;
11409 	       l_end_date := c_version_details_rec.date_to;
11410 	       l_orig_budget_unit1_value := c_version_details_rec.budget_unit1_value;
11411 	       l_orig_budget_unit2_value := c_version_details_rec.budget_unit2_value;
11412 	       l_orig_budget_unit3_value := c_version_details_rec.budget_unit3_value;
11413 	     end loop;
11414 
11415 	     if (nvl(p_budget_unit1, 'X') = 'FTE' and nvl(p_budget_unit1_aggregate, 'X') = 'AVERAGE') then
11416 	       l_orig_budget_unit1_value := l_orig_budget_unit1_value + l_change_fte_value;
11417 	     elsif (nvl(p_budget_unit2, 'X') = 'FTE' and nvl(p_budget_unit2_aggregate, 'X') = 'AVERAGE') then
11418 	       l_orig_budget_unit2_value := l_orig_budget_unit2_value + l_change_fte_value;
11419 	     elsif (nvl(p_budget_unit3, 'X') = 'FTE' and nvl(p_budget_unit3_aggregate, 'X') = 'AVERAGE') then
11420 	       l_orig_budget_unit3_value := l_orig_budget_unit3_value + l_change_fte_value;
11421 	     end if;
11422 
11423 	     begin
11424 
11425 	       pqh_psb_interface_api.update_budget_version
11426 		 (p_validate              => false,
11427 		  p_budget_id             => p_hr_budget_id,
11428 		  p_budget_version_id     => p_pqh_budget_version_id,
11429 		  p_version_number        => l_version_number,
11430 		  p_object_version_number => l_ver_object_version_number,
11431 		  p_date_from             => l_start_date,
11432 		  p_date_to               => l_end_date,
11433 		  p_transfered_to_gl_flag => 'N',
11434 		  p_xfer_to_other_apps_cd => 'N',
11435 		  p_budget_unit1_value    => l_orig_budget_unit1_value,
11436 		  p_budget_unit2_value    => l_orig_budget_unit2_value,
11437 		  p_budget_unit3_value    => l_orig_budget_unit3_value,
11438 		  p_effective_date        => sysdate);
11439 
11440 	     EXCEPTION
11441 	       when OTHERS then
11442 		 FND_MSG_PUB.Add;
11443 		 message_token('BUDGET', p_pqh_budget_name);
11444 		 message_token('VERSION', l_version_number);
11445 		 message_token('STARTDATE', l_start_date);
11446 		 message_token('ENDDATE', l_end_date);
11447 		 add_message('PSB', 'PSB_PQH_UPDATE_BUDGET_VERSION');
11448 		 raise FND_API.G_EXC_ERROR;
11449 
11450 	     end;
11451 
11452 	   end;
11453 	   end if;
11454 
11455 	 end;
11456 	 end if;
11457 
11458        end;
11459        end if;
11460       /*For Bug No : 2292003 Start*/
11461       end loop;
11462       /*For Bug No : 2292003 End*/
11463 
11464       /*For Bug No : 2434152 Start*/
11465       IF l_position_ctr = g_checkpoint_save THEN
11466 	commit work;
11467 	l_position_ctr := 0;
11468       END IF;
11469       /*For Bug No : 2434152 End*/
11470 
11471     end loop;
11472     exit when c_position%NOTFOUND;
11473 
11474   end loop;
11475   close c_position;
11476 
11477   /*For Bug No : 2434152 Start*/
11478   --commit all unsaved records
11479   commit work;
11480   /*For Bug No : 2434152 End*/
11481 
11482   -- Initialize API return status to success
11483   p_return_status := FND_API.G_RET_STS_SUCCESS;
11484 
11485 EXCEPTION
11486 
11487    when FND_API.G_EXC_ERROR then
11488      if c_position%ISOPEN then
11489        close c_position;
11490      end if;
11491     p_return_status := FND_API.G_RET_STS_ERROR;
11492     FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
11493  			p_data  => p_msg_data);
11494 
11495    when FND_API.G_EXC_UNEXPECTED_ERROR then
11496      if c_position%ISOPEN then
11497        close c_position;
11498      end if;
11499      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11500      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
11501  			p_data  => p_msg_data);
11502 
11503    when OTHERS then
11504      if c_position%ISOPEN then
11505        close c_position;
11506      end if;
11507      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11508 
11509      if FND_MSG_PUB.Check_Msg_Level
11510        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
11511        FND_MSG_PUB.Add_Exc_Msg
11512 	  (p_pkg_name => G_PKG_NAME,
11513 	   p_procedure_name => l_api_name);
11514      end if;
11515 
11516      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
11517 				p_data  => p_msg_data);
11518 
11519 END Create_Pqh_Budget_detail;
11520 
11521 /* ----------------------------------------------------------------------- */
11522 
11523 PROCEDURE Upload_Budget_HRMS
11524 ( p_api_version          IN   NUMBER,
11525   p_init_msg_list        IN   VARCHAR2 := FND_API.G_FALSE,
11526   p_commit               IN   VARCHAR2 := FND_API.G_FALSE,
11527   p_validation_level     IN   NUMBER := FND_API.G_VALID_LEVEL_FULL,
11528   p_return_status        OUT  NOCOPY  VARCHAR2,
11529   p_msg_count            OUT  NOCOPY  NUMBER,
11530   p_msg_data             OUT  NOCOPY  VARCHAR2,
11531   p_event_type           IN   VARCHAR2,
11532   p_source_id            IN   NUMBER,
11533   p_hr_budget_id         IN   NUMBER,
11534   p_from_budget_year_id  IN   NUMBER,
11535   p_to_budget_year_id    IN   NUMBER
11536 ) IS
11537 
11538   l_api_name                  CONSTANT VARCHAR2(30)   := 'Upload_Budget_HRMS';
11539   l_api_version               CONSTANT NUMBER         := 1.0;
11540 
11541   l_pqh_budget_name           VARCHAR2(30);
11542   l_currency_code             VARCHAR2(15);
11543   l_business_group_id         NUMBER;
11544   l_pqh_budget_version_id     NUMBER;
11545   l_pqh_budget_detail_id      NUMBER;
11546   l_budget_unit1_id           NUMBER;
11547   l_budget_unit2_id           NUMBER;
11548   l_budget_unit3_id           NUMBER;
11549   l_budget_unit1_value        NUMBER;
11550   l_budget_unit2_value        NUMBER;
11551   l_budget_unit3_value        NUMBER;
11552   l_fte_type_id               NUMBER;
11553   l_money_type_id             NUMBER;
11554   l_budget_unit1              VARCHAR2(10);
11555   l_budget_unit2              VARCHAR2(10);
11556   l_budget_unit3              VARCHAR2(10);
11557   l_budget_unit1_aggregate    VARCHAR2(30);
11558   l_budget_unit2_aggregate    VARCHAR2(30);
11559   l_budget_unit3_aggregate    VARCHAR2(30);
11560 
11561   l_data_extract_id           NUMBER;
11562   l_budget_group_id           NUMBER;
11563   l_flex_code                 NUMBER;
11564   l_root_budget_group_id      NUMBER;
11565   l_budget_calendar_id        NUMBER;
11566   l_system_data_extract_id    NUMBER;
11567   l_start_date                DATE;
11568   l_end_date                  DATE;
11569   l_version_number            NUMBER;
11570   l_object_version_number     NUMBER;
11571 
11572   l_return_status             VARCHAR2(1);
11573   l_msg_count                 NUMBER;
11574   l_msg_data                  VARCHAR2(2000);
11575   l_id_flex_num               NUMBER;
11576 
11577   cursor c_types is
11578     select shared_type_id, system_type_cd
11579       from per_shared_types_vl
11580      where system_type_cd in ('FTE', 'MONEY');
11581 
11582   cursor c_pqh_budget is
11583     select budget_name, currency_code, business_group_id, budget_unit1_id, budget_unit2_id,
11584 	   budget_unit3_id, budget_unit1_aggregate, budget_unit2_aggregate, budget_unit3_aggregate
11585       from pqh_psb_budgets
11586      where budget_id = p_hr_budget_id;
11587 
11588  /*For Bug No : 2292003 Start*/
11589  --changed the view_name from per_business_groups to
11590  --per_business_groups_perf due to perf issues
11591   cursor c_business_group is
11592     select currency_code, to_number(cost_allocation_structure) id_flex_num
11593       from per_business_groups_perf
11594      where business_group_id = l_business_group_id;
11595  /*For Bug No : 2292003 End*/
11596 
11597   cursor c_BG is
11598     select nvl(chart_of_accounts_id, root_chart_of_accounts_id) flex_code,
11599 	   nvl(root_budget_group_id, budget_group_id) root_budget_group_id
11600       from PSB_BUDGET_GROUPS_V
11601      where budget_group_id = l_budget_group_id;
11602 
11603   cursor c_WS is
11604     select budget_calendar_id, budget_group_id, data_extract_id
11605       from PSB_WORKSHEETS
11606      where worksheet_id = p_source_id;
11607 
11608   cursor c_BR is
11609     select budget_group_id
11610       from psb_budget_revisions
11611      where budget_revision_id = p_source_id;
11612 
11613   /*For Bug No : 2292003 Start*/
11614   --added the budget_calendar_id filter
11615   cursor c_budget_period is
11616     select min(start_date) start_date, max(end_date) end_date
11617       from psb_budget_periods
11618      where budget_period_id between p_from_budget_year_id and p_to_budget_year_id
11619        and budget_calendar_id = l_budget_calendar_id
11620        and budget_period_type = 'Y';
11621   /*For Bug No : 2292003 End*/
11622 
11623  cursor c_revision_dates is
11624    select min(start_date) start_date, max(end_date) end_date
11625      from psb_position_fte
11626     where budget_revision_id = p_source_id
11627       and hr_budget_id = p_hr_budget_id;
11628 
11629 BEGIN
11630 
11631   if p_event_type = 'BP' then
11632   begin
11633 
11634     for c_WS_Rec in c_WS loop
11635       l_budget_group_id := c_WS_Rec.budget_group_id;
11636       l_budget_calendar_id := c_WS_Rec.budget_calendar_id;
11637       l_data_extract_id := c_WS_Rec.data_extract_id;
11638     end loop;
11639 
11640   end;
11641   elsif p_event_type = 'BR' then
11642   begin
11643 
11644     for c_BR_Rec in c_BR loop
11645       l_budget_group_id := c_BR_Rec.budget_group_id;
11646     end loop;
11647 
11648   end;
11649   end if;
11650 
11651   l_system_data_extract_id := PSB_BUDGET_REVISIONS_PVT.Find_System_Data_Extract
11652 				  (p_budget_group_id => l_budget_group_id);
11653 
11654   -- check that all position transactions in the worksheet have been approved
11655 
11656   if p_event_type = 'BR' then
11657   begin
11658 
11659     Validate_Position_Budget
11660 	   (p_return_status => l_return_status,
11661             --p_msg_count => l_msg_count,
11662             --p_msg_data => l_msg_data,
11663 	    p_event_type => 'BR',
11664 	    p_source_id => p_source_id);
11665 
11666     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
11667       raise FND_API.G_EXC_ERROR;
11668     end if;
11669 
11670   end;
11671   end if;
11672 
11673   for c_BG_Rec in c_BG loop
11674     l_flex_code := c_BG_Rec.flex_code;
11675     l_root_budget_group_id := c_BG_Rec.root_budget_group_id;
11676   end loop;
11677 
11678   if p_event_type = 'BP' then
11679     for c_budget_period_rec in c_budget_period loop
11680       l_start_date := c_budget_period_rec.start_date;
11681       l_end_date := c_budget_period_rec.end_date;
11682     end loop;
11683   elsif p_event_type = 'BR' then
11684     for c_revision_dates_rec in c_revision_dates loop
11685       l_start_date := c_revision_dates_rec.start_date;
11686       l_end_date := c_revision_dates_rec.end_date;
11687     end loop;
11688   end if;
11689 
11690   if p_validation_level = FND_API.G_VALID_LEVEL_FULL then
11691   begin
11692 
11693     -- Validate the Budget Group Hierarchy
11694     Validate_Budget_Group
11695 	   (p_return_status => l_return_status,
11696             p_msg_count => l_msg_count,
11697             p_msg_data => l_msg_data,
11698 	    p_event_type => p_event_type,
11699 	    p_source_id => p_source_id,
11700 	    p_budget_group_id => l_budget_group_id);
11701 
11702     if l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
11703       raise FND_API.G_EXC_ERROR;
11704     end if;
11705 
11706     -- Validate Budget Period Granularity
11707     if p_hr_budget_id is not null then
11708        Validate_Period_Granularity
11709 	    (p_return_status => l_return_status,
11710              p_msg_count => l_msg_count,
11711              p_msg_data => l_msg_data,
11712 	     p_hr_budget_id => p_hr_budget_id,
11713 	     p_from_budget_year_id => p_from_budget_year_id,
11714 	     p_to_budget_year_id => p_to_budget_year_id);
11715 
11716 	if l_return_status <> FND_API.G_RET_STS_SUCCESS then
11717 	  raise FND_API.G_EXC_ERROR;
11718 	end if;
11719     end if;
11720 
11721     -- Validate budget document being uploaded
11722     Validate_Budget_Document
11723 	    (p_return_status => l_return_status,
11724              p_msg_count => l_msg_count,
11725              p_msg_data => l_msg_data,
11726 	     p_validation_level => p_validation_level,
11727 	     p_event_type => p_event_type,
11728 	     p_source_id => p_source_id,
11729 	     p_data_extract_id => l_data_extract_id,
11730 	     p_system_data_extract_id => l_system_data_extract_id);
11731 
11732     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
11733       raise FND_API.G_EXC_ERROR;
11734     end if;
11735 
11736   end;
11737   end if;
11738 
11739   if p_validation_level = FND_API.G_VALID_LEVEL_NONE then
11740   begin
11741 
11742     -- Validate budget document being uploaded
11743     Validate_Budget_Document
11744 	    (p_return_status => l_return_status,
11745              p_msg_count => l_msg_count,
11746              p_msg_data => l_msg_data,
11747 	     p_validation_level => p_validation_level,
11748 	     p_event_type => p_event_type,
11749 	     p_source_id => p_source_id,
11750 	     p_data_extract_id => l_data_extract_id,
11751 	     p_system_data_extract_id => l_system_data_extract_id);
11752 
11753     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
11754       raise FND_API.G_EXC_ERROR;
11755     end if;
11756 
11757     for c_types_rec in c_types loop
11758 
11759       if c_types_rec.system_type_cd = 'FTE' then
11760 	l_fte_type_id := c_types_rec.shared_type_id;
11761       elsif c_types_rec.system_type_cd = 'MONEY' then
11762 	l_money_type_id := c_types_rec.shared_type_id;
11763       end if;
11764 
11765     end loop;
11766 
11767     for c_pqh_budget_rec in c_pqh_budget loop
11768       l_pqh_budget_name := c_pqh_budget_rec.budget_name;
11769       l_business_group_id := c_pqh_budget_rec.business_group_id;
11770       l_currency_code := c_pqh_budget_rec.currency_code;
11771       l_budget_unit1_id := c_pqh_budget_rec.budget_unit1_id;
11772       l_budget_unit2_id := c_pqh_budget_rec.budget_unit2_id;
11773       l_budget_unit3_id := c_pqh_budget_rec.budget_unit3_id;
11774       l_budget_unit1_aggregate := c_pqh_budget_rec.budget_unit1_aggregate;
11775       l_budget_unit2_aggregate := c_pqh_budget_rec.budget_unit2_aggregate;
11776       l_budget_unit3_aggregate := c_pqh_budget_rec.budget_unit3_aggregate;
11777     end loop;
11778 
11779     for c_business_group_rec in c_business_group loop
11780       if l_currency_code is null then
11781 	l_currency_code := c_business_group_rec.currency_code;
11782       end if;
11783       l_id_flex_num := c_business_group_rec.id_flex_num;
11784     end loop;
11785 
11786     if nvl(l_budget_unit1_id, 0) = l_fte_type_id then
11787       l_budget_unit1 := 'FTE';
11788     elsif nvl(l_budget_unit1_id, 0) = l_money_type_id then
11789       l_budget_unit1 := 'MONEY';
11790     end if;
11791 
11792     if nvl(l_budget_unit2_id, 0) = l_fte_type_id then
11793       l_budget_unit2 := 'FTE';
11794     elsif nvl(l_budget_unit2_id, 0) = l_money_type_id then
11795       l_budget_unit2 := 'MONEY';
11796     end if;
11797 
11798     if nvl(l_budget_unit3_id, 0) = l_fte_type_id then
11799       l_budget_unit3 := 'FTE';
11800     elsif nvl(l_budget_unit3_id, 0) = l_money_type_id then
11801       l_budget_unit3 := 'MONEY';
11802     end if;
11803 
11804     -- Validate budget set for fringe benefit elements
11805     Validate_Budget_Set
11806 	    (p_return_status => l_return_status,
11807              p_msg_count => l_msg_count,
11808              p_msg_data => l_msg_data,
11809 	     p_event_type => p_event_type,
11810 	     p_source_id => p_source_id,
11811 	     p_data_extract_id => l_data_extract_id,
11812 	     p_system_data_extract_id => l_system_data_extract_id);
11813 
11814     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
11815       raise FND_API.G_EXC_ERROR;
11816     end if;
11817 
11818     Create_Pqh_Budget_Version
11819 	  (p_return_status => l_return_status,
11820            p_msg_count => l_msg_count,
11821            p_msg_data => l_msg_data,
11822 	   p_event_type => p_event_type,
11823 	   p_source_id => p_source_id,
11824 	   p_pqh_budget_version_id => l_pqh_budget_version_id,
11825 	   p_data_extract_id => l_data_extract_id,
11826 	   p_system_data_extract_id => l_system_data_extract_id,
11827 	   p_currency_code => l_currency_code,
11828 	   p_hr_budget_id => p_hr_budget_id,
11829 	   p_pqh_budget_name => l_pqh_budget_name,
11830 	   p_start_date => l_start_date,
11831 	   p_end_date => l_end_date,
11832 	   p_budget_unit1 => l_budget_unit1,
11833 	   p_budget_unit2 => l_budget_unit2,
11834 	   p_budget_unit3 => l_budget_unit3);
11835 
11836     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
11837       raise FND_API.G_EXC_ERROR;
11838     end if;
11839 
11840     commit work;
11841 
11842     Create_Pqh_Budget_Detail
11843 	  (p_return_status => l_return_status,
11844            p_msg_count => l_msg_count,
11845            p_msg_data => l_msg_data,
11846 	   p_event_type => p_event_type,
11847 	   p_source_id => p_source_id,
11848 	   p_currency_code => l_currency_code,
11849 	   p_business_group_id => l_business_group_id,
11850 	   p_system_data_extract_id => l_system_data_extract_id,
11851 	   p_data_extract_id => l_data_extract_id,
11852 	   p_gl_flex_code => l_flex_code,
11853 	   p_hr_budget_id  => p_hr_budget_id,
11854 	   p_pqh_budget_name => l_pqh_budget_name,
11855 	   p_pqh_budget_version_id => l_pqh_budget_version_id,
11856 	   p_start_date => l_start_date,
11857 	   p_end_date => l_end_date,
11858 	   p_budget_unit1 => l_budget_unit1,
11859 	   p_budget_unit2 => l_budget_unit2,
11860 	   p_budget_unit3 => l_budget_unit3,
11861 	   p_budget_unit1_aggregate => l_budget_unit1_aggregate,
11862 	   p_budget_unit2_aggregate => l_budget_unit2_aggregate,
11863 	   p_budget_unit3_aggregate => l_budget_unit3_aggregate,
11864 	   p_id_flex_num => l_id_flex_num);
11865 
11866     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
11867       raise FND_API.G_EXC_ERROR;
11868     end if;
11869 
11870     Record_Position_Transaction
11871 	 (p_return_status => l_return_status,
11872           p_msg_count => l_msg_count,
11873           p_msg_data => l_msg_data,
11874 	  p_event_type => p_event_type,
11875 	  p_source_id => p_source_id,
11876 	  p_hr_budget_id => p_hr_budget_id,
11877 	  p_from_budget_year_id => p_from_budget_year_id,
11878 	  p_to_budget_year_id => p_to_budget_year_id,
11879 	  p_transfer_to_interface => 'Y',
11880 	  p_transfer_to_hrms => 'Y');
11881 
11882     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
11883       raise FND_API.G_EXC_ERROR;
11884     end if;
11885 
11886   end;
11887   end if;
11888 
11889   -- Standard check of p_commit.
11890   IF FND_API.to_Boolean(p_commit) THEN
11891     commit work;
11892   END IF;
11893 
11894   -- Standard call to get message count and if count is 1, get message info.
11895   FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
11896 			     p_data  => p_msg_data);
11897 
11898 
11899   -- Initialize API return status to success
11900   p_return_status := FND_API.G_RET_STS_SUCCESS;
11901 
11902 EXCEPTION
11903 
11904    when FND_API.G_EXC_ERROR then
11905      p_return_status := FND_API.G_RET_STS_ERROR;
11906 
11907      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
11908 				p_data  => p_msg_data);
11909 
11910    when FND_API.G_EXC_UNEXPECTED_ERROR then
11911      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11912 
11913      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
11914 				p_data  => p_msg_data);
11915 
11916    when OTHERS then
11917      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11918 
11919      if FND_MSG_PUB.Check_Msg_Level
11920        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
11921        FND_MSG_PUB.Add_Exc_Msg
11922 	  (p_pkg_name => G_PKG_NAME,
11923 	   p_procedure_name => l_api_name);
11924      end if;
11925 
11926      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
11927 				p_data  => p_msg_data);
11928 
11929 END Upload_Budget_HRMS;
11930 
11931 /* ----------------------------------------------------------------------- */
11932 
11933 PROCEDURE Convert_Organization_Attr
11934 ( p_api_version        IN   NUMBER,
11935   p_init_msg_list      IN   VARCHAR2 := FND_API.G_FALSE,
11936   p_commit             IN   VARCHAR2 := FND_API.G_FALSE,
11937   p_validation_level   IN   NUMBER := FND_API.G_VALID_LEVEL_FULL,
11938   p_return_status      OUT  NOCOPY  VARCHAR2,
11939   p_msg_count          OUT  NOCOPY  NUMBER,
11940   p_msg_data           OUT  NOCOPY  VARCHAR2,
11941   p_business_group_id  IN   NUMBER,
11942   p_attribute_id       IN   NUMBER
11943 ) IS
11944 
11945   l_api_name           CONSTANT VARCHAR2(30)   := 'Convert_Organization_Attr';
11946   l_api_version        CONSTANT NUMBER         := 1.0;
11947 
11948   l_sysorg_id          NUMBER;
11949   l_syscount           NUMBER;
11950   l_usrcount           NUMBER;
11951 
11952   cursor c_sysorg is
11953     select attribute_id
11954       from psb_attributes_vl
11955      where business_group_id = p_business_group_id
11956        and system_attribute_type = 'ORG';
11957 
11958 BEGIN
11959 
11960   -- Standard Start of API savepoint
11961 
11962   SAVEPOINT     Convert_Organization_Attr;
11963 
11964   -- Standard call to check for call compatibility.
11965 
11966   if not FND_API.Compatible_API_Call (l_api_version,
11967 				      p_api_version,
11968 				      l_api_name,
11969 				      G_PKG_NAME)
11970   then
11971     raise FND_API.G_EXC_UNEXPECTED_ERROR;
11972   end if;
11973 
11974   -- Initialize message list if p_init_msg_list is set to TRUE.
11975 
11976   if FND_API.to_Boolean (p_init_msg_list) then
11977     FND_MSG_PUB.initialize;
11978   end if;
11979 
11980   l_sysorg_id := NULL;
11981 
11982   for c_sysorg_rec in c_sysorg loop
11983     l_sysorg_id := C_sysorg_rec.attribute_id;
11984   end loop;
11985 
11986   if l_sysorg_id IS NOT NULL then
11987   begin
11988 
11989     select count(*)
11990       into l_syscount
11991       from psb_attribute_values
11992      where attribute_id = l_sysorg_id;
11993 
11994     select count(*)
11995       into l_usrcount
11996       from psb_attribute_values
11997      where attribute_id = p_attribute_id;
11998 
11999     if l_syscount >= l_usrcount then
12000     begin
12001 
12002       /* Number of system org attribute records more than user defined org attribute records */
12003       FND_FILE.put_line(FND_FILE.LOG, 'Updating Table PSB_ATTRIBUTE_VALUES');
12004       Update psb_attribute_values
12005 	 set attribute_id = l_sysorg_id
12006        where attribute_id = p_attribute_id;
12007 
12008       FND_FILE.put_line(FND_FILE.LOG, 'Updating Table PSB_ATTRIBUTE_VALUES_I');
12009       Update psb_attribute_values_i
12010 	 set attribute_id = l_sysorg_id
12011        where attribute_id = p_attribute_id;
12012 
12013       FND_FILE.put_line(FND_FILE.LOG, 'Updating Table PSB_ACCOUNT_POSITION_SET_LINES');
12014       update psb_account_position_set_lines
12015 	 set attribute_id = l_sysorg_id
12016        where attribute_id = p_attribute_id;
12017 
12018       FND_FILE.put_line(FND_FILE.LOG, 'Updating Table PSB_PARAMETER_FORMULAS');
12019       Update psb_parameter_formulas
12020 	 set attribute_id = l_sysorg_id
12021        where attribute_id = p_attribute_id;
12022 
12023       -- Bug#4466903
12024       -- Removed call to update psb_dss_dimension_mappings.
12025 
12026       FND_FILE.put_line(FND_FILE.LOG, 'Updating Table PSB_DEFAULT_ASSIGNMENTS');
12027       Update psb_default_assignments
12028 	 set attribute_id = l_sysorg_id
12029        where attribute_id = p_attribute_id;
12030 
12031       FND_FILE.put_line(FND_FILE.LOG, 'Updating Table PSB_POSITION_ASSIGNMENTS');
12032       Update psb_position_assignments
12033 	 set attribute_id = l_sysorg_id
12034        where attribute_id = p_attribute_id;
12035 
12036       FND_FILE.put_line(FND_FILE.LOG, 'Updating Table PSB_ATTRIBUTES_TL');
12037       Delete from psb_attributes_tl
12038        where attribute_id = p_attribute_id;
12039 
12040       FND_FILE.put_line(FND_FILE.LOG, 'Updating Table PSB_ATTRIBUTES');
12041       Delete from psb_attributes
12042        where attribute_id = p_attribute_id;
12043 
12044     end;
12045     else
12046     begin
12047 
12048       /* Number of system org attribute records less than user defined org attribute records */
12049       FND_FILE.put_line(FND_FILE.LOG, 'Updating Table PSB_ATTRIBUTE_VALUES');
12050       Update psb_attribute_values
12051 	 set attribute_id = p_attribute_id
12052        where attribute_id = l_sysorg_id;
12053 
12054       FND_FILE.put_line(FND_FILE.LOG, 'Updating Table PSB_ATTRIBUTE_VALUES_I');
12055       Update psb_attribute_values_i
12056 	 set attribute_id = p_attribute_id
12057        where attribute_id = l_sysorg_id;
12058 
12059       FND_FILE.put_line(FND_FILE.LOG, 'Updating Table PSB_ACCOUNT_POSITION_SET_LINES');
12060       update psb_account_position_set_lines
12061 	 set attribute_id = p_attribute_id
12062        where attribute_id = l_sysorg_id;
12063 
12064       FND_FILE.put_line(FND_FILE.LOG, 'Updating Table PSB_PARAMETER_FORMULAS');
12065       Update psb_parameter_formulas
12066 	 set attribute_id = p_attribute_id
12067        where attribute_id = l_sysorg_id;
12068 
12069       -- Bug#4466903
12070       -- Removed call to update psb_dss_dimension_mappings.
12071 
12072       FND_FILE.put_line(FND_FILE.LOG, 'Updating Table PSB_DEFAULT_ASSIGNMENTS');
12073       Update psb_default_assignments
12074 	 set attribute_id = p_attribute_id
12075        where attribute_id = l_sysorg_id;
12076 
12077       FND_FILE.put_line(FND_FILE.LOG, 'Updating Table PSB_POSITION_ASSIGNMENTS');
12078       Update psb_position_assignments
12079 	 set attribute_id = p_attribute_id
12080        where attribute_id = l_sysorg_id;
12081 
12082       FND_FILE.put_line(FND_FILE.LOG, 'Updating Table PSB_ATTRIBUTES_TL');
12083       Delete from psb_attributes_tl
12084        where attribute_id = p_attribute_id;
12085 
12086       Update psb_attributes_tl
12087 	 set attribute_id = p_attribute_id
12088        where attribute_id = l_sysorg_id;
12089 
12090       FND_FILE.put_line(FND_FILE.LOG, 'Updating Table PSB_ATTRIBUTES');
12091       Delete from psb_attributes
12092        where attribute_id = p_attribute_id;
12093 
12094       Update psb_attributes
12095 	 set attribute_id = p_attribute_id
12096        where attribute_id = l_sysorg_id;
12097 
12098     end;
12099     end if;
12100 
12101   end;
12102   end if;
12103 
12104   -- Standard check of p_commit.
12105   IF FND_API.to_Boolean(p_commit) THEN
12106     commit work;
12107   END IF;
12108 
12109   -- Standard call to get message count and if count is 1, get message info.
12110   FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
12111 			     p_data  => p_msg_data);
12112 
12113 
12114   -- Initialize API return status to success
12115   p_return_status := FND_API.G_RET_STS_SUCCESS;
12116 
12117 EXCEPTION
12118 
12119    when FND_API.G_EXC_ERROR then
12120      rollback to Convert_Organization_Attr;
12121      p_return_status := FND_API.G_RET_STS_ERROR;
12122 
12123      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
12124 				p_data  => p_msg_data);
12125 
12126    when FND_API.G_EXC_UNEXPECTED_ERROR then
12127      rollback to Convert_Organization_Attr;
12128      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12129 
12130      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
12131 				p_data  => p_msg_data);
12132 
12133    when OTHERS then
12134      rollback to Convert_Organization_Attr;
12135      p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12136 
12137      if FND_MSG_PUB.Check_Msg_Level
12138        (p_message_level => FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
12139        FND_MSG_PUB.Add_Exc_Msg
12140 	  (p_pkg_name => G_PKG_NAME,
12141 	   p_procedure_name => l_api_name);
12142      end if;
12143 
12144      FND_MSG_PUB.Count_And_Get (p_count => p_msg_count,
12145 				p_data  => p_msg_data);
12146 
12147 END Convert_Organization_Attr;
12148 
12149 /* ----------------------------------------------------------------------- */
12150 
12151 -- This is the execution file for the concurrent program "Convert Organization
12152 -- Attribute" through Standard Report Submissions
12153 
12154 PROCEDURE Convert_Organization_Attr_CP
12155 ( errbuf               OUT  NOCOPY  VARCHAR2,
12156   retcode              OUT  NOCOPY  VARCHAR2,
12157   p_business_group_id  IN   NUMBER,
12158   p_attribute_id       IN   NUMBER
12159 ) IS
12160 
12161   l_api_name           CONSTANT VARCHAR2(30)   := 'Convert_Organization_Attr_CP';
12162   l_api_version        CONSTANT NUMBER         :=  1.0;
12163 
12164   l_return_status      VARCHAR2(1);
12165   l_msg_count          NUMBER;
12166   l_msg_data           VARCHAR2(2000);
12167 
12168 begin
12169 
12170   Convert_Organization_Attr
12171 	(p_api_version => 1.0,
12172 	 p_init_msg_list => FND_API.G_TRUE,
12173 	 p_return_status => l_return_status,
12174 	 p_msg_count => l_msg_count,
12175 	 p_msg_data => l_msg_data,
12176 	 p_business_group_id => p_business_group_id,
12177 	 p_attribute_id => p_attribute_id);
12178 
12179   if l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
12180     raise FND_API.G_EXC_ERROR;
12181   end if;
12182     /* Start Bug No. 2322856 */
12183 --  PSB_MESSAGE_S.Print_Success;
12184     /* End Bug No. 2322856 */
12185   retcode := 0;
12186 
12187   commit work;
12188 
12189 EXCEPTION
12190 
12191    when FND_API.G_EXC_ERROR then
12192      PSB_MESSAGE_S.Print_Error (p_mode => FND_FILE.LOG,
12193 				p_print_header => FND_API.G_TRUE);
12194      retcode := 2;
12195 
12196    when FND_API.G_EXC_UNEXPECTED_ERROR then
12197      PSB_MESSAGE_S.Print_Error (p_mode => FND_FILE.LOG,
12198 				p_print_header => FND_API.G_TRUE);
12199      retcode := 2;
12200 
12201    when OTHERS then
12202      if FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) then
12203        FND_MSG_PUB.Add_Exc_Msg (G_PKG_NAME,
12204 				l_api_name);
12205      end if;
12206 
12207      PSB_MESSAGE_S.Print_Error (p_mode => FND_FILE.LOG,
12208 				p_print_header => FND_API.G_TRUE);
12209      retcode := 2;
12210 
12211 END Convert_Organization_Attr_CP;
12212 
12213 /* ----------------------------------------------------------------------- */
12214 -- Upload Element Position Set Groups
12215 
12216 PROCEDURE Copy_Position_Set_Groups
12217 ( p_return_status           OUT  NOCOPY  VARCHAR2,
12218   p_old_pay_element_id      IN   NUMBER,
12219   p_new_pay_element_id      IN   NUMBER,
12220   p_target_data_extract_id  IN   NUMBER,
12221   p_budget_group_id         IN   NUMBER,
12222   p_flex_mapping_set_id     IN   NUMBER,
12223   p_follow_salary           IN   VARCHAR2
12224 )IS
12225 --
12226   l_possetgrp_id   NUMBER;
12227   l_possetgrp_exists     BOOLEAN;
12228 --
12229   l_msg_count               NUMBER;
12230   l_msg_data                VARCHAR2(2000);
12231   l_return_status           VARCHAR2(1);
12232 --
12233   cursor c_elempossetgrp_seq is
12234    select PSB_ELEMENT_POS_SET_GROUPS_S.NEXTVAL seq
12235     from dual;
12236 
12237   cursor c_possetgrp (elemid NUMBER) is
12238   select *  from PSB_ELEMENT_POS_SET_GROUPS
12239    where pay_element_id = elemid;
12240 
12241   cursor c_possetgrp_exists (psg_name VARCHAR2) is
12242     select position_set_group_id
12243       from PSB_ELEMENT_POS_SET_GROUPS
12244      where pay_element_id = p_new_pay_element_id
12245        and name = psg_name;
12246 --
12247 BEGIN
12248 --
12249   for c_possetgrp_rec in c_possetgrp(p_old_pay_element_id) loop
12250 
12251    l_possetgrp_exists := FALSE;
12252 
12253    for c_possetgrp_exists_rec in c_possetgrp_exists (c_possetgrp_rec.name) loop
12254       l_possetgrp_id := c_possetgrp_exists_rec.position_set_group_id;
12255       l_possetgrp_exists := TRUE;
12256    end loop;
12257 
12258    if not l_possetgrp_exists then
12259     begin
12260 	--Insert the position set group for new element
12261 	for c_elempossetgrp_seq_rec in c_elempossetgrp_seq loop
12262 	  l_possetgrp_id := c_elempossetgrp_seq_rec.seq;
12263 	end loop;
12264 
12265 	PSB_ELEMENT_POS_SET_GROUPS_PVT.Insert_Row
12266 	   (p_api_version => 1.0,
12267 	    p_return_status => l_return_status,
12268 	    p_msg_count => l_msg_count,
12269 	    p_msg_data => l_msg_data,
12270 	    p_position_set_group_id => l_possetgrp_id,
12271 	    p_pay_element_id => p_new_pay_element_id,
12272 	    p_name => c_possetgrp_rec.name,
12273 	    p_last_update_date => sysdate,
12274 	    p_last_updated_by => FND_GLOBAL.USER_ID,
12275 	    p_last_update_login => FND_GLOBAL.LOGIN_ID,
12276 	    p_created_by => FND_GLOBAL.USER_ID,
12277 	    p_creation_date => sysdate);
12278 
12279 	if l_return_status <> FND_API.G_RET_STS_SUCCESS then
12280 	  raise FND_API.G_EXC_ERROR;
12281 	end if;
12282    end;
12283    end if;
12284    --Copy the Position sets that belong to the Position Set Group
12285 
12286    Copy_Position_Sets
12287      ( p_return_status            =>  l_return_status,
12288        p_old_psg_id               =>  c_possetgrp_rec.position_set_group_id,
12289        p_new_psg_id               =>  l_possetgrp_id,
12290        p_target_data_extract_id   =>  p_target_data_extract_id,
12291        p_budget_group_id          =>  p_budget_group_id);
12292 
12293    if l_return_status <> FND_API.G_RET_STS_SUCCESS then
12294       raise FND_API.G_EXC_ERROR;
12295    end if;
12296 
12297    Copy_Element_Distributions
12298      (p_return_status           =>  l_return_status,
12299       p_old_psg_id              =>  c_possetgrp_rec.position_set_group_id,
12300       p_new_psg_id              =>  l_possetgrp_id,
12301       p_flex_mapping_set_id     =>  p_flex_mapping_set_id,
12302       p_follow_salary           =>  p_follow_salary);
12303 
12304    if l_return_status <> FND_API.G_RET_STS_SUCCESS then
12305       raise FND_API.G_EXC_ERROR;
12306    end if;
12307 
12308   end loop;
12309   p_return_status :=  FND_API.G_RET_STS_SUCCESS;
12310   --
12311   EXCEPTION
12312    when FND_API.G_EXC_ERROR then
12313      p_return_status := FND_API.G_RET_STS_ERROR;
12314 
12315 End Copy_Position_Set_Groups;
12316 
12317 /*---------------------------------------------------------------------------*/
12318 -- Upload Position Sets that belong to Position set group
12319 
12320 PROCEDURE Copy_Position_Sets
12321 ( p_return_status           OUT  NOCOPY  VARCHAR2,
12322   p_old_psg_id              IN   NUMBER,
12323   p_new_psg_id              IN   NUMBER,
12324   p_target_data_extract_id  IN   NUMBER,
12325   p_budget_group_id         IN   NUMBER
12326 ) IS
12327 --
12328   l_position_set_id         NUMBER;
12329   l_position_set_exists     BOOLEAN;
12330   l_attribute_selection_type      VARCHAR2(1);
12331   l_rowid                   ROWID;
12332   lp_rowid                  ROWID;
12333   l_set_relation_id         NUMBER;
12334   l_set_rel_exists     BOOLEAN;
12335 --
12336   l_msg_count               NUMBER;
12337   l_msg_data                VARCHAR2(2000);
12338   l_return_status           VARCHAR2(1);
12339 --
12340   cursor c_setrel (possetgrpid NUMBER) is
12341     select *
12342       from PSB_SET_RELATIONS
12343      where position_set_group_id = possetgrpid;
12344 
12345   cursor c_set_rel_exists (possetgrpid NUMBER, pos_set_id NUMBER) is
12346     select set_relation_id
12347       from PSB_SET_RELATIONS
12348      where position_set_group_id = possetgrpid
12349        and account_position_set_id = pos_set_id;
12350 
12351   cursor c_posset (possetid NUMBER) is
12352     select *
12353       from PSB_ACCOUNT_POSITION_SETS
12354      where account_position_set_id = possetid;
12355 
12356   cursor c_posset_exists (possetname VARCHAR2) is
12357     select account_position_set_id , attribute_selection_type, rowid
12358       from PSB_ACCOUNT_POSITION_SETS
12359      where data_extract_id = p_target_data_extract_id
12360        and account_or_position_type = 'P'
12361        and name = possetname;
12362 
12363   cursor c_posset_seq is
12364     select PSB_ACCOUNT_POSITION_SETS_S.NEXTVAL seq
12365       from dual;
12366 
12367   cursor c_setrel_seq is
12368     select PSB_SET_RELATIONS_S.NEXTVAL seq
12369       from dual;
12370 
12371 
12372 --
12373 BEGIN
12374 --
12375   for c_setrel_rec in c_setrel (p_old_psg_id) loop
12376 
12377     for c_posset_rec in c_posset (c_setrel_rec.account_position_set_id) loop
12378 
12379 	    l_position_set_exists := FALSE;
12380 	    lp_rowid := null;
12381 
12382 	    for c_posset_exists_rec in c_posset_exists (c_posset_rec.name) loop
12383 	      l_position_set_id := c_posset_exists_rec.account_position_set_id;
12384 	      lp_rowid := c_posset_exists_rec.rowid;
12385 	      l_attribute_selection_type := c_posset_rec.attribute_selection_type;
12386 	      l_position_set_exists := TRUE;
12387 	    end loop;
12388 
12389 	    if not l_position_set_exists then
12390 	    begin
12391 	      --Insert the position set
12392 	      for c_posset_seq_rec in c_posset_seq loop
12393 		l_position_set_id := c_posset_seq_rec.seq;
12394 	      end loop;
12395 
12396 	      PSB_ACCOUNT_POSITION_SET_PVT.Insert_Row
12397 		 (p_api_version => 1.0,
12398 		  p_return_status => l_return_status,
12399 		  p_msg_count => l_msg_count,
12400 		  p_msg_data => l_msg_data,
12401 		  p_row_id => l_rowid,
12402 		  p_account_position_set_id => l_position_set_id,
12403 		  p_name => c_posset_rec.name,
12404 		  p_set_of_books_id => c_posset_rec.set_of_books_id,
12405 		  p_use_in_budget_group_flag => c_posset_rec.use_in_budget_group_flag,
12406 		  p_data_extract_id => p_target_data_extract_id,
12407 		  p_budget_group_id => p_budget_group_id,
12408 		  p_global_or_local_type => c_posset_rec.global_or_local_type,
12409 		  p_account_or_position_type =>c_posset_rec.account_or_position_type,
12410 		  p_attribute_selection_type => c_posset_rec.attribute_selection_type,
12411 		  p_business_group_id => c_posset_rec.business_group_id,
12412 		  p_last_update_date => sysdate,
12413 		  p_last_updated_by => FND_GLOBAL.USER_ID,
12414 		  p_last_update_login => FND_GLOBAL.LOGIN_ID,
12415 		  p_created_by => FND_GLOBAL.USER_ID,
12416 		  p_creation_date => sysdate);
12417 
12418 		if l_return_status <> FND_API.G_RET_STS_SUCCESS then
12419 		  raise FND_API.G_EXC_ERROR;
12420 		end if;
12421 	       --Position set is inserted
12422 	    end;
12423 	    elsif c_posset_rec.attribute_selection_type <> l_attribute_selection_type then
12424 		begin
12425 	       --Update the Position Set with the new selection type
12426 		  PSB_ACCOUNT_POSITION_SET_PVT.Update_Row
12427 		     (p_api_version => 1.0,
12428 		      p_return_status => l_return_status,
12429 		      p_msg_count => l_msg_count,
12430 		      p_msg_data => l_msg_data,
12431 		      p_row_id => lp_rowid,
12432 		      p_account_position_set_id => l_position_set_id,
12433 		      p_name => c_posset_rec.name,
12434 		      p_set_of_books_id => c_posset_rec.set_of_books_id,
12435 		      p_data_extract_id => p_target_data_extract_id,
12436 		      p_global_or_local_type => c_posset_rec.global_or_local_type,
12437 		      p_account_or_position_type => c_posset_rec.account_or_position_type,
12438 		      p_attribute_selection_type =>c_posset_rec.attribute_selection_type,
12439 		      p_business_group_id => c_posset_rec.business_group_id,
12440 		      p_last_update_date => sysdate,
12441 		      p_last_updated_by => FND_GLOBAL.USER_ID,
12442 		      p_last_update_login => FND_GLOBAL.LOGIN_ID);
12443 
12444 		  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
12445 		    raise FND_API.G_EXC_ERROR;
12446 		  end if;
12447 		--Position set is updated
12448 		end;
12449 	     end if;
12450 	-- Upload Set Relations
12451 --
12452 	    l_set_rel_exists := FALSE;
12453 	    for c_set_rel_exists_rec in c_set_rel_exists (p_new_psg_id, l_position_set_id) loop
12454 	      l_set_rel_exists := TRUE;
12455 	    end loop;
12456 	 if not l_set_rel_exists then
12457 	    for c_setrel_seq_rec in c_setrel_seq loop
12458 	      l_set_relation_id := c_setrel_seq_rec.seq;
12459 	    end loop;
12460 
12461 	    PSB_SET_RELATION_PVT.Insert_Row
12462 	       (p_api_version => 1.0,
12463 		p_return_status => l_return_status,
12464 		p_msg_count => l_msg_count,
12465 		p_msg_data => l_msg_data,
12466 		p_row_id => l_rowid,
12467 		p_set_relation_id => l_set_relation_id,
12468 		p_account_position_set_id => l_position_set_id,
12469 		p_allocation_rule_id => null,
12470 		p_budget_group_id => null,
12471 		p_budget_workflow_rule_id => null,
12472 		p_constraint_id => null,
12473 		p_default_rule_id => null,
12474 		p_parameter_id => null,
12475 		p_position_set_group_id => p_new_psg_id,
12476 		/* Budget Revision Rules Enhancement Start */
12477 		p_rule_id => null,
12478 		p_apply_balance_flag => null,
12479 		/* Budget Revision Rules Enhancement End */
12480 		p_effective_start_date => null,
12481 		p_effective_end_date => null,
12482 		p_last_update_date => sysdate,
12483 		p_last_updated_by => FND_GLOBAL.USER_ID,
12484 		p_last_update_login => FND_GLOBAL.LOGIN_ID,
12485 		p_created_by => FND_GLOBAL.USER_ID,
12486 		p_creation_date => sysdate);
12487 
12488 	    if l_return_status <> FND_API.G_RET_STS_SUCCESS then
12489 	      raise FND_API.G_EXC_ERROR;
12490 	    end if;
12491 	  end if;
12492 
12493 	     --Copy all position set lines that belong to the position set
12494 	     Copy_Position_Set_Lines
12495 	       (p_return_status           =>  l_return_status,
12496 		p_old_posset_id           =>  c_posset_rec.account_position_set_id,
12497 		p_new_posset_id           =>  l_position_set_id,
12498 		p_target_data_extract_id  =>  p_target_data_extract_id
12499 	       );
12500 
12501 	  end loop; /* Position Sets */
12502 	end loop; /* Set Relations */
12503 
12504 	p_return_status :=  FND_API.G_RET_STS_SUCCESS;
12505   --
12506   EXCEPTION
12507   --
12508    when FND_API.G_EXC_ERROR then
12509      p_return_status := FND_API.G_RET_STS_ERROR;
12510 
12511 End Copy_Position_Sets;
12512 
12513 /*----------------------------------------------------------------------------*/
12514 
12515 PROCEDURE Copy_Position_Set_Lines
12516 ( p_return_status           OUT  NOCOPY  VARCHAR2,
12517   p_old_posset_id           IN   NUMBER,
12518   p_new_posset_id           IN   NUMBER,
12519   p_target_data_extract_id  IN   NUMBER
12520 )IS
12521 --
12522   l_position_set_id         NUMBER;
12523   l_position_set_exists     BOOLEAN;
12524   l_attribute_selection_type      VARCHAR2(1);
12525   l_rowid                   ROWID;
12526   lp_rowid                  ROWID;
12527   l_set_relation_id         NUMBER;
12528   l_line_sequence_id        NUMBER;
12529   l_value_sequence_id       NUMBER;
12530   l_line_sequence_exists    BOOLEAN;
12531   l_value_sequence_exists   BOOLEAN;
12532   l_attribute_value_id      NUMBER;
12533 --
12534   l_msg_count               NUMBER;
12535   l_msg_data                VARCHAR2(2000);
12536   l_return_status           VARCHAR2(1);
12537 --
12538   cursor c_possetline (possetid NUMBER) is
12539     select *
12540       from PSB_ACCOUNT_POSITION_SET_LINES
12541      where account_position_set_id = possetid;
12542 
12543   cursor c_possetlineval (lineseqid NUMBER) is
12544     select *
12545       from PSB_POSITION_SET_LINE_VALUES
12546      where line_sequence_id = lineseqid;
12547 
12548   cursor c_re_attrval (attrvalid NUMBER) is
12549     select a.attribute_value_id
12550       from PSB_ATTRIBUTE_VALUES a,
12551 	   PSB_ATTRIBUTE_VALUES b
12552      where a.data_extract_id = p_target_data_extract_id
12553        and a.attribute_value = b.attribute_value
12554        and a.attribute_id    = b.attribute_id -- added for Bug#4262388
12555        and b.attribute_value_id = attrvalid;
12556 
12557   cursor c_possetline_exists (possetid NUMBER, attrid NUMBER) is
12558     select line_sequence_id
12559       from PSB_ACCOUNT_POSITION_SET_LINES
12560      where attribute_id = attrid
12561        and account_position_set_id = possetid;
12562 
12563   cursor c_possetlineval_exists (lineseqid NUMBER, attr_val_id NUMBER) is
12564     select attribute_value_id
12565       from PSB_ATTRIBUTE_VALUES
12566      where attribute_value_id = attr_val_id
12567        and attribute_value in
12568 	   (select a.attribute_value
12569 	      from PSB_ATTRIBUTE_VALUES a,
12570 		   PSB_POSITION_SET_LINE_VALUES b
12571 	     where a.attribute_value_id = b.attribute_value_id
12572 	       and b.line_sequence_id = lineseqid);
12573 
12574   cursor c_possetline_seq is
12575     select PSB_ACCT_POSITION_SET_LINES_S.NEXTVAL seq
12576       from dual;
12577 
12578    cursor c_possetlineval_seq is
12579     select PSB_POSITION_SET_LINE_VALUES_S.NEXTVAL seq
12580       from dual;
12581 --
12582 BEGIN
12583 --
12584       for c_possetline_rec in c_possetline (p_old_posset_id) loop
12585 	  l_line_sequence_exists := FALSE;
12586 	  for c_possetline_exists_rec in c_possetline_exists (p_new_posset_id, c_possetline_rec.attribute_id) loop
12587 	    l_line_sequence_id := c_possetline_exists_rec.line_sequence_id;
12588 	    l_line_sequence_exists := TRUE;
12589 	  end loop;
12590 
12591 	     if not l_line_sequence_exists then
12592 		begin
12593 
12594 		  for c_possetline_seq_rec in c_possetline_seq loop
12595 		    l_line_sequence_id := c_possetline_seq_rec.seq;
12596 		  end loop;
12597 
12598 		  PSB_ACCT_POSITION_SET_LINE_PVT.Insert_Row
12599 		     (p_api_version => 1.0,
12600 		      p_return_status => l_return_status,
12601 		      p_msg_count => l_msg_count,
12602 		      p_msg_data => l_msg_data,
12603 		      p_row_id => l_rowid,
12604 		      p_line_sequence_id => l_line_sequence_id,
12605 		      p_account_position_set_id => p_new_posset_id,
12606 		      p_description => c_possetline_rec.description,
12607 		      p_business_group_id => c_possetline_rec.business_group_id,
12608 		      p_attribute_id => c_possetline_rec.attribute_id,
12609 		      p_include_or_exclude_type => c_possetline_rec.include_or_exclude_type,
12610 		      p_segment1_low => c_possetline_rec.segment1_low,
12611 		      p_segment2_low => c_possetline_rec.segment2_low,
12612 		      p_segment3_low => c_possetline_rec.segment3_low,
12613 		      p_segment4_low => c_possetline_rec.segment4_low,
12614 		      p_segment5_low => c_possetline_rec.segment5_low,
12615 		      p_segment6_low => c_possetline_rec.segment6_low,
12616 		      p_segment7_low => c_possetline_rec.segment7_low,
12617 		      p_segment8_low => c_possetline_rec.segment8_low,
12618 		      p_segment9_low => c_possetline_rec.segment9_low,
12619 		      p_segment10_low => c_possetline_rec.segment10_low,
12620 		      p_segment11_low => c_possetline_rec.segment11_low,
12621 		      p_segment12_low => c_possetline_rec.segment12_low,
12622 		      p_segment13_low => c_possetline_rec.segment13_low,
12623 		      p_segment14_low => c_possetline_rec.segment14_low,
12624 		      p_segment15_low => c_possetline_rec.segment15_low,
12625 		      p_segment16_low => c_possetline_rec.segment16_low,
12626 		      p_segment17_low => c_possetline_rec.segment17_low,
12627 		      p_segment18_low => c_possetline_rec.segment18_low,
12628 		      p_segment19_low => c_possetline_rec.segment19_low,
12629 		      p_segment20_low => c_possetline_rec.segment20_low,
12630 		      p_segment21_low => c_possetline_rec.segment21_low,
12631 		      p_segment22_low => c_possetline_rec.segment22_low,
12632 		      p_segment23_low => c_possetline_rec.segment23_low,
12633 		      p_segment24_low => c_possetline_rec.segment24_low,
12634 		      p_segment25_low => c_possetline_rec.segment25_low,
12635 		      p_segment26_low => c_possetline_rec.segment26_low,
12636 		      p_segment27_low => c_possetline_rec.segment27_low,
12637 		      p_segment28_low => c_possetline_rec.segment28_low,
12638 		      p_segment29_low => c_possetline_rec.segment29_low,
12639 		      p_segment30_low => c_possetline_rec.segment30_low,
12640 		      p_segment1_high => c_possetline_rec.segment1_high,
12641 		      p_segment2_high => c_possetline_rec.segment2_high,
12642 		      p_segment3_high => c_possetline_rec.segment3_high,
12643 		      p_segment4_high => c_possetline_rec.segment4_high,
12644 		      p_segment5_high => c_possetline_rec.segment5_high,
12645 		      p_segment6_high => c_possetline_rec.segment6_high,
12646 		      p_segment7_high => c_possetline_rec.segment7_high,
12647 		      p_segment8_high => c_possetline_rec.segment8_high,
12648 		      p_segment9_high => c_possetline_rec.segment9_high,
12649 		      p_segment10_high => c_possetline_rec.segment10_high,
12650 		      p_segment11_high => c_possetline_rec.segment11_high,
12651 		      p_segment12_high => c_possetline_rec.segment12_high,
12652 		      p_segment13_high => c_possetline_rec.segment13_high,
12653 		      p_segment14_high => c_possetline_rec.segment14_high,
12654 		      p_segment15_high => c_possetline_rec.segment15_high,
12655 		      p_segment16_high => c_possetline_rec.segment16_high,
12656 		      p_segment17_high => c_possetline_rec.segment17_high,
12657 		      p_segment18_high => c_possetline_rec.segment18_high,
12658 		      p_segment19_high => c_possetline_rec.segment19_high,
12659 		      p_segment20_high => c_possetline_rec.segment20_high,
12660 		      p_segment21_high => c_possetline_rec.segment21_high,
12661 		      p_segment22_high => c_possetline_rec.segment22_high,
12662 		      p_segment23_high => c_possetline_rec.segment23_high,
12663 		      p_segment24_high => c_possetline_rec.segment24_high,
12664 		      p_segment25_high => c_possetline_rec.segment25_high,
12665 		      p_segment26_high => c_possetline_rec.segment26_high,
12666 		      p_segment27_high => c_possetline_rec.segment27_high,
12667 		      p_segment28_high => c_possetline_rec.segment28_high,
12668 		      p_segment29_high => c_possetline_rec.segment29_high,
12669 		      p_segment30_high => c_possetline_rec.segment30_high,
12670 		      p_context => c_possetline_rec.context,
12671 		      p_attribute1 => c_possetline_rec.attribute1,
12672 		      p_attribute2 => c_possetline_rec.attribute2,
12673 		      p_attribute3 => c_possetline_rec.attribute3,
12674 		      p_attribute4 => c_possetline_rec.attribute4,
12675 		      p_attribute5 => c_possetline_rec.attribute5,
12676 		      p_attribute6 => c_possetline_rec.attribute6,
12677 		      p_attribute7 => c_possetline_rec.attribute7,
12678 		      p_attribute8 => c_possetline_rec.attribute8,
12679 		      p_attribute9 => c_possetline_rec.attribute9,
12680 		      p_attribute10 => c_possetline_rec.attribute10,
12681 		      p_last_update_date => sysdate,
12682 		      p_last_updated_by => FND_GLOBAL.USER_ID,
12683 		      p_last_update_login => FND_GLOBAL.LOGIN_ID,
12684 		      p_created_by => FND_GLOBAL.USER_ID,
12685 		      p_creation_date => sysdate);
12686 
12687 		  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
12688 		    raise FND_API.G_EXC_ERROR;
12689 		  end if;
12690 	      end;
12691 	      end if;
12692 
12693 
12694 		for c_possetlineval_rec in c_possetlineval (c_possetline_rec.line_sequence_id) loop
12695 
12696 		    for c_re_attrval_rec in c_re_attrval (c_possetlineval_rec.attribute_value_id) loop
12697 		      l_attribute_value_id := c_re_attrval_rec.attribute_value_id;
12698 		    end loop;
12699 		      l_value_sequence_exists := FALSE;
12700 		    for c_possetlineval_exists_rec in c_possetlineval_exists (l_line_sequence_id, c_possetlineval_rec.attribute_value_id) loop --c_possetlineval_rec.attribute_value) loop
12701 		      l_value_sequence_exists := TRUE;
12702 		    end loop;
12703 
12704 		    if not l_value_sequence_exists then
12705 		    begin
12706 
12707 		      for c_possetlineval_seq_rec in c_possetlineval_seq loop
12708 			l_value_sequence_id := c_possetlineval_seq_rec.seq;
12709 		      end loop;
12710 
12711 		      PSB_POS_SET_LINE_VALUES_PVT.Insert_Row
12712 			 (p_api_version => 1.0,
12713 			  p_return_status => l_return_status,
12714 			  p_msg_count => l_msg_count,
12715 			  p_msg_data => l_msg_data,
12716 			  p_row_id => l_rowid,
12717 			  p_value_sequence_id => l_value_sequence_id,
12718 			  p_line_sequence_id => l_line_sequence_id,
12719 			  p_attribute_value_id => l_attribute_value_id,
12720 			  p_attribute_value => c_possetlineval_rec.attribute_value,
12721 			  p_last_update_date => sysdate,
12722 			  p_last_updated_by => FND_GLOBAL.USER_ID,
12723 			  p_last_update_login => FND_GLOBAL.LOGIN_ID,
12724 			  p_created_by => FND_GLOBAL.USER_ID,
12725 			  p_creation_date => sysdate);
12726 
12727 		      if l_return_status <> FND_API.G_RET_STS_SUCCESS then
12728 			raise FND_API.G_EXC_ERROR;
12729 		      end if;
12730 
12731 		    end;
12732 		    end if;
12733 		end loop; /* For Position Set Line Val */
12734 	--
12735    end loop; /* For Position Set Lines */
12736 
12737   --
12738   EXCEPTION
12739   --
12740    when FND_API.G_EXC_ERROR then
12741      p_return_status := FND_API.G_RET_STS_ERROR;
12742 
12743 End Copy_Position_Set_Lines;
12744 
12745 /* ------------------------------------------------------------------------*/
12746 
12747 PROCEDURE Copy_Element_Distributions
12748 ( p_return_status           OUT  NOCOPY  VARCHAR2,
12749   p_old_psg_id              IN   NUMBER,
12750   p_new_psg_id              IN   NUMBER,
12751   p_flex_mapping_set_id     IN   NUMBER,
12752   p_follow_salary           IN   VARCHAR2
12753 )IS
12754 --
12755   l_msg_count               NUMBER;
12756   l_msg_data                VARCHAR2(2000);
12757   l_return_status           VARCHAR2(1);
12758 --
12759   l_distribution_id         NUMBER;
12760   l_mapped_ccid             NUMBER;
12761   l_budget_year_type_id     NUMBER;
12762   l_year_start_date         DATE;
12763   l_year_end_date           DATE;
12764   l_distribution_exists     BOOLEAN;
12765   l_distribution_percent    NUMBER;
12766 --
12767   cursor c_elemdist (possetgrpid NUMBER) is
12768     select *
12769       from PSB_PAY_ELEMENT_DISTRIBUTIONS
12770      where position_set_group_id = possetgrpid;
12771 
12772   cursor c_elemdist_seq is
12773     select PSB_PAY_ELEMENT_DISTRIBUTION_S.NEXTVAL seq
12774       from dual;
12775 
12776   cursor c_elemdist_exists (psg_id NUMBER, cc_id NUMBER, con_segments VARCHAR2) is
12777     select distribution_id, distribution_percent
12778       from PSB_PAY_ELEMENT_DISTRIBUTIONS
12779      where position_set_group_id = psg_id
12780        and ((cc_id is not null and code_combination_id = cc_id) or (concatenated_segments = con_segments));
12781 --
12782 BEGIN
12783 --
12784     for c_elemdist_rec in c_elemdist (p_old_psg_id) loop
12785 
12786 	  -- if flex mapping was used in worksheet creation need to map the salary account distr
12787 	  -- and distr for elements not following salary by effective dates
12788 
12789 	  if ((p_flex_mapping_set_id is not null) and (nvl(p_follow_salary, 'N') = 'N')) then
12790 	  begin
12791 
12792 	    for l_year_index in 1..PSB_WS_ACCT1.g_num_budget_years loop
12793 
12794 	      if PSB_WS_ACCT1.g_budget_years(l_year_index).year_type in ('CY', 'PP') then
12795 	      begin
12796 
12797 	       l_budget_year_type_id := PSB_WS_ACCT1.g_budget_years(l_year_index).budget_year_type_id;
12798 	       l_year_start_date := PSB_WS_ACCT1.g_budget_years(l_year_index).start_date;
12799 	       l_year_end_date := PSB_WS_ACCT1.g_budget_years(l_year_index).end_date;
12800 
12801 		if (((c_elemdist_rec.effective_start_date < l_year_end_date) and
12802 		     (c_elemdist_rec.effective_end_date is null)) or
12803 		    ((c_elemdist_rec.effective_start_date between l_year_start_date and l_year_end_date) or
12804 		     (c_elemdist_rec.effective_end_date between l_year_start_date and l_year_end_date) or
12805 		    ((c_elemdist_rec.effective_start_date < l_year_start_date) and
12806 		     (c_elemdist_rec.effective_end_date > l_year_end_date)))) then
12807 		begin
12808 		--
12809 		  l_mapped_ccid := PSB_FLEX_MAPPING_PVT.Get_Mapped_CCID
12810 				      (p_api_version => 1.0,
12811 				       p_ccid => c_elemdist_rec.code_combination_id,
12812 				       p_budget_year_type_id => l_budget_year_type_id,
12813 				       p_flexfield_mapping_set_id => p_flex_mapping_set_id,
12814 				       p_mapping_mode => 'GL_POSTING');
12815 
12816 		  if l_mapped_ccid = 0 then
12817 		    raise FND_API.G_EXC_ERROR;
12818 		  end if;
12819 		--Check whether the line is already exists or not, if exists update the line o/w insert new one
12820 
12821 		  l_distribution_exists  := FALSE;
12822 		  for c_elemdist_exists_rec in c_elemdist_exists (p_new_psg_id, l_mapped_ccid, c_elemdist_rec.concatenated_segments) loop
12823 		    l_distribution_exists  := TRUE;
12824 		    l_distribution_id := c_elemdist_exists_rec.distribution_id;
12825 		    l_distribution_percent := c_elemdist_exists_rec.distribution_percent;
12826 		  end loop;
12827 
12828 		 if not l_distribution_exists then
12829 		 begin
12830 		 --Insert the line
12831 		  for c_elemdist_seq_rec in c_elemdist_seq loop
12832 		    l_distribution_id := c_elemdist_seq_rec.seq;
12833 		  end loop;
12834 
12835 		  PSB_ELEMENT_DISTRIBUTIONS_PVT.Insert_Row
12836 		     (p_api_version => 1.0,
12837 		      p_return_status => l_return_status,
12838 		      p_msg_count => l_msg_count,
12839 		      p_msg_data => l_msg_data,
12840 		      p_distribution_id => l_distribution_id,
12841 		      p_position_set_group_id => p_new_psg_id,
12842 		      p_chart_of_accounts_id => c_elemdist_rec.chart_of_accounts_id,
12843 		      p_effective_start_date => l_year_start_date,
12844 		      p_effective_end_date => l_year_end_date,
12845 		      p_distribution_percent => c_elemdist_rec.distribution_percent,
12846 		      p_concatenated_segments => null,
12847 		      p_code_combination_id => l_mapped_ccid,
12848 		      p_distribution_set_id => c_elemdist_rec.distribution_set_id,
12849 		      p_segment1 => null,
12850 		      p_segment2 => null,
12851 		      p_segment3 => null,
12852 		      p_segment4 => null,
12853 		      p_segment5 => null,
12854 		      p_segment6 => null,
12855 		      p_segment7 => null,
12856 		      p_segment8 => null,
12857 		      p_segment9 => null,
12858 		      p_segment10 => null,
12859 		      p_segment11 => null,
12860 		      p_segment12 => null,
12861 		      p_segment13 => null,
12862 		      p_segment14 => null,
12863 		      p_segment15 => null,
12864 		      p_segment16 => null,
12865 		      p_segment17 => null,
12866 		      p_segment18 => null,
12867 		      p_segment19 => null,
12868 		      p_segment20 => null,
12869 		      p_segment21 => null,
12870 		      p_segment22 => null,
12871 		      p_segment23 => null,
12872 		      p_segment24 => null,
12873 		      p_segment25 => null,
12874 		      p_segment26 => null,
12875 		      p_segment27 => null,
12876 		      p_segment28 => null,
12877 		      p_segment29 => null,
12878 		      p_segment30 => null,
12879 		      p_last_update_date => sysdate,
12880 		      p_last_updated_by => FND_GLOBAL.USER_ID,
12881 		      p_last_update_login => FND_GLOBAL.LOGIN_ID,
12882 		      p_created_by => FND_GLOBAL.USER_ID,
12883 		      p_creation_date => sysdate);
12884 
12885 		  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
12886 		    raise FND_API.G_EXC_ERROR;
12887 		  end if;
12888 		end;    --End of insert line
12889 		else
12890 		begin   --Update line
12891 		  PSB_ELEMENT_DISTRIBUTIONS_PVT.Update_Row
12892 		     (p_api_version => 1.0,
12893 		      p_return_status => l_return_status,
12894 		      p_msg_count => l_msg_count,
12895 		      p_msg_data => l_msg_data,
12896 		      p_distribution_id => l_distribution_id,
12897 		      p_position_set_group_id => p_new_psg_id,
12898 		      p_chart_of_accounts_id => c_elemdist_rec.chart_of_accounts_id,
12899 		      p_effective_start_date => l_year_start_date,
12900 		      p_effective_end_date => l_year_end_date,
12901 		      p_distribution_percent => c_elemdist_rec.distribution_percent,
12902 		      p_concatenated_segments => null,
12903 		      p_code_combination_id => l_mapped_ccid,
12904 		      p_distribution_set_id => c_elemdist_rec.distribution_set_id,
12905 		      p_segment1 => null,
12906 		      p_segment2 => null,
12907 		      p_segment3 => null,
12908 		      p_segment4 => null,
12909 		      p_segment5 => null,
12910 		      p_segment6 => null,
12911 		      p_segment7 => null,
12912 		      p_segment8 => null,
12913 		      p_segment9 => null,
12914 		      p_segment10 => null,
12915 		      p_segment11 => null,
12916 		      p_segment12 => null,
12917 		      p_segment13 => null,
12918 		      p_segment14 => null,
12919 		      p_segment15 => null,
12920 		      p_segment16 => null,
12921 		      p_segment17 => null,
12922 		      p_segment18 => null,
12923 		      p_segment19 => null,
12924 		      p_segment20 => null,
12925 		      p_segment21 => null,
12926 		      p_segment22 => null,
12927 		      p_segment23 => null,
12928 		      p_segment24 => null,
12929 		      p_segment25 => null,
12930 		      p_segment26 => null,
12931 		      p_segment27 => null,
12932 		      p_segment28 => null,
12933 		      p_segment29 => null,
12934 		      p_segment30 => null,
12935 		      p_last_update_date => sysdate,
12936 		      p_last_updated_by => FND_GLOBAL.USER_ID,
12937 		      p_last_update_login => FND_GLOBAL.LOGIN_ID);
12938 
12939 		  if l_return_status <> FND_API.G_RET_STS_SUCCESS then
12940 		    raise FND_API.G_EXC_ERROR;
12941 		  end if;
12942 
12943 		end;
12944 		end if;         --End of update line
12945 
12946 	      end;
12947 	      end if;           --End of effective dates condition
12948 
12949 	   end;
12950 	   end if;              --End of Year condition for 'CY', 'PP'
12951 
12952 	 end loop;
12953 
12954        end;                     --End of If part for flex mapping exists
12955        else
12956        begin                    --If flex mapping doesn't exists use the direct code_combination
12957        --
12958 	 l_distribution_exists  := FALSE;
12959 	 for c_elemdist_exists_rec in c_elemdist_exists (p_new_psg_id, c_elemdist_rec.code_combination_id, c_elemdist_rec.concatenated_segments) loop
12960 	   l_distribution_exists  := TRUE;
12961 	   l_distribution_id := c_elemdist_exists_rec.distribution_id;
12962 	   l_distribution_percent := c_elemdist_exists_rec.distribution_percent;
12963 	 end loop;
12964 
12965 	 if  NOT l_distribution_exists   then
12966 	 begin  --Insert the line
12967 	 --
12968 	    for c_elemdist_seq_rec in c_elemdist_seq loop
12969 	      l_distribution_id := c_elemdist_seq_rec.seq;
12970 	    end loop;
12971 
12972 	    PSB_ELEMENT_DISTRIBUTIONS_PVT.Insert_Row
12973 	       (p_api_version => 1.0,
12974 		p_return_status => l_return_status,
12975 		p_msg_count => l_msg_count,
12976 		p_msg_data => l_msg_data,
12977 		p_distribution_id => l_distribution_id,
12978 		p_position_set_group_id => p_new_psg_id,
12979 		p_chart_of_accounts_id => c_elemdist_rec.chart_of_accounts_id,
12980 		p_effective_start_date => c_elemdist_rec.effective_start_date,
12981 		p_effective_end_date => c_elemdist_rec.effective_end_date,
12982 		p_distribution_percent => c_elemdist_rec.distribution_percent,
12983 		p_concatenated_segments => c_elemdist_rec.concatenated_segments,
12984 		p_code_combination_id => c_elemdist_rec.code_combination_id,
12985 		p_distribution_set_id => c_elemdist_rec.distribution_set_id,
12986 		p_segment1 => c_elemdist_rec.segment1,
12987 		p_segment2 => c_elemdist_rec.segment2,
12988 		p_segment3 => c_elemdist_rec.segment3,
12989 		p_segment4 => c_elemdist_rec.segment4,
12990 		p_segment5 => c_elemdist_rec.segment5,
12991 		p_segment6 => c_elemdist_rec.segment6,
12992 		p_segment7 => c_elemdist_rec.segment7,
12993 		p_segment8 => c_elemdist_rec.segment8,
12994 		p_segment9 => c_elemdist_rec.segment9,
12995 		p_segment10 => c_elemdist_rec.segment10,
12996 		p_segment11 => c_elemdist_rec.segment11,
12997 		p_segment12 => c_elemdist_rec.segment12,
12998 		p_segment13 => c_elemdist_rec.segment13,
12999 		p_segment14 => c_elemdist_rec.segment14,
13000 		p_segment15 => c_elemdist_rec.segment15,
13001 		p_segment16 => c_elemdist_rec.segment16,
13002 		p_segment17 => c_elemdist_rec.segment17,
13003 		p_segment18 => c_elemdist_rec.segment18,
13004 		p_segment19 => c_elemdist_rec.segment19,
13005 		p_segment20 => c_elemdist_rec.segment20,
13006 		p_segment21 => c_elemdist_rec.segment21,
13007 		p_segment22 => c_elemdist_rec.segment22,
13008 		p_segment23 => c_elemdist_rec.segment23,
13009 		p_segment24 => c_elemdist_rec.segment24,
13010 		p_segment25 => c_elemdist_rec.segment25,
13011 		p_segment26 => c_elemdist_rec.segment26,
13012 		p_segment27 => c_elemdist_rec.segment27,
13013 		p_segment28 => c_elemdist_rec.segment28,
13014 		p_segment29 => c_elemdist_rec.segment29,
13015 		p_segment30 => c_elemdist_rec.segment30,
13016 		p_last_update_date => sysdate,
13017 		p_last_updated_by => FND_GLOBAL.USER_ID,
13018 		p_last_update_login => FND_GLOBAL.LOGIN_ID,
13019 		p_created_by => FND_GLOBAL.USER_ID,
13020 		p_creation_date => sysdate);
13021 
13022 	    if l_return_status <> FND_API.G_RET_STS_SUCCESS then
13023 	      raise FND_API.G_EXC_ERROR;
13024 	    end if;
13025 	  end;          --End of Insert line where flex mapping doesn't exist
13026 	  else
13027 	  begin         --Update the line
13028 	  --
13029 	    PSB_ELEMENT_DISTRIBUTIONS_PVT.Update_Row
13030 	       (p_api_version => 1.0,
13031 		p_return_status => l_return_status,
13032 		p_msg_count => l_msg_count,
13033 		p_msg_data => l_msg_data,
13034 		p_distribution_id => l_distribution_id,
13035 		p_position_set_group_id => p_new_psg_id,
13036 		p_chart_of_accounts_id => c_elemdist_rec.chart_of_accounts_id,
13037 		p_effective_start_date => c_elemdist_rec.effective_start_date,
13038 		p_effective_end_date => c_elemdist_rec.effective_end_date,
13039 		p_distribution_percent => c_elemdist_rec.distribution_percent,
13040 		p_concatenated_segments => c_elemdist_rec.concatenated_segments,
13041 		p_code_combination_id => c_elemdist_rec.code_combination_id,
13042 		p_distribution_set_id => c_elemdist_rec.distribution_set_id,
13043 		p_segment1 => c_elemdist_rec.segment1,
13044 		p_segment2 => c_elemdist_rec.segment2,
13045 		p_segment3 => c_elemdist_rec.segment3,
13046 		p_segment4 => c_elemdist_rec.segment4,
13047 		p_segment5 => c_elemdist_rec.segment5,
13048 		p_segment6 => c_elemdist_rec.segment6,
13049 		p_segment7 => c_elemdist_rec.segment7,
13050 		p_segment8 => c_elemdist_rec.segment8,
13051 		p_segment9 => c_elemdist_rec.segment9,
13052 		p_segment10 => c_elemdist_rec.segment10,
13053 		p_segment11 => c_elemdist_rec.segment11,
13054 		p_segment12 => c_elemdist_rec.segment12,
13055 		p_segment13 => c_elemdist_rec.segment13,
13056 		p_segment14 => c_elemdist_rec.segment14,
13057 		p_segment15 => c_elemdist_rec.segment15,
13058 		p_segment16 => c_elemdist_rec.segment16,
13059 		p_segment17 => c_elemdist_rec.segment17,
13060 		p_segment18 => c_elemdist_rec.segment18,
13061 		p_segment19 => c_elemdist_rec.segment19,
13062 		p_segment20 => c_elemdist_rec.segment20,
13063 		p_segment21 => c_elemdist_rec.segment21,
13064 		p_segment22 => c_elemdist_rec.segment22,
13065 		p_segment23 => c_elemdist_rec.segment23,
13066 		p_segment24 => c_elemdist_rec.segment24,
13067 		p_segment25 => c_elemdist_rec.segment25,
13068 		p_segment26 => c_elemdist_rec.segment26,
13069 		p_segment27 => c_elemdist_rec.segment27,
13070 		p_segment28 => c_elemdist_rec.segment28,
13071 		p_segment29 => c_elemdist_rec.segment29,
13072 		p_segment30 => c_elemdist_rec.segment30,
13073 		p_last_update_date => sysdate,
13074 		p_last_updated_by => FND_GLOBAL.USER_ID,
13075 		p_last_update_login => FND_GLOBAL.LOGIN_ID);
13076 
13077 	    if l_return_status <> FND_API.G_RET_STS_SUCCESS then
13078 	      raise FND_API.G_EXC_ERROR;
13079 	    end if;
13080 	  end;
13081 	  end if;       --End of Insert line where flex mapping doesn't exist
13082 
13083 	end;
13084 	end if;
13085 
13086       end loop; /* Element Distribution */
13087     --
13088   EXCEPTION
13089     --
13090     when FND_API.G_EXC_ERROR then
13091       p_return_status := FND_API.G_RET_STS_ERROR;
13092 
13093 End Copy_Element_Distributions;
13094 /*---------------------------------------------------------------------------*/
13095 
13096 END PSB_Position_Control_Pvt;