DBA Data[Home] [Help]

PACKAGE BODY: APPS.PSP_SUM_TRANS

Source


1 PACKAGE BODY PSP_SUM_TRANS as
2 /* $Header: PSPTRSTB.pls 120.24.12020000.6 2013/04/13 04:32:12 lkodaman ship $ */
3 --- g_gms_avail	VARCHAR2(1) := 'N'; removed the usage of this variable, because
4 ---        transfer_to_gms has checks on GMS phase. bug 2444657
5 /* Following three global variables introduced to faciliate calling mark_batch_end procedure
6    from GL_TIE_BACK procedure: Bug 1929317 */
7  g_source_code Varchar2(30);
8  g_batch_name  Varchar2(30);
9  g_time_period_id number;
10  g_payroll_id integer;    --- added for ER enhancment.
11 -- g_currency_code   Varchar2(15); -- added for bug no 2478000 Commented for Bug 2916848
12 
13 -- Introduced the following for bug 2259310
14   g_enable_enc_summ_gl        VARCHAR2(1) DEFAULT NVL(fnd_profile.value('PSP_ENABLE_ENC_SUMM_GL'), 'N');
15 
16    g_insert_str	VARCHAR2(5000); -- Introduced for Bug fix 2935850
17 
18 g_dff_grouping_option	CHAR(1);			-- Introduced for bug fix 2908859
19  g_suspense_autopop  varchar2(1);  -- 5080403
20 
21   -- Introduced the following for Bug 6902514
22  g_create_stat_batch_in_gms  VARCHAR(1);      -- change
23  g_skip_flag_gms	     varchar(1);       -- change
24 
25 
26  PROCEDURE sum_and_transfer(errbuf	         OUT NOCOPY VARCHAR2,
27                             retcode	         OUT NOCOPY VARCHAR2,
28                             p_source_type     	 IN VARCHAR2,
29                             p_source_code     	 IN VARCHAR2,
30 			    p_payroll_id	 IN NUMBER,
31                             p_time_period_id  	 IN NUMBER,
32                             p_batch_name      	 IN VARCHAR2,
33 			    p_business_group_id  	 IN NUMBER,
34 			    p_set_of_books_id      	 IN NUMBER) IS
35 
36     l_return_status	VARCHAR2(10);
37     l_msg_count		NUMBER;
38     l_msg_data		VARCHAR2(2000);
39   --- introduced for 5080403
40   cursor autopop_config_cur is
41   select pcv_information7 suspense_account
42     from pqp_configuration_values
43    where pcv_information_category = 'PSP_ENABLE_AUTOPOPULATION'
44      and legislation_code is null
45      and nvl(business_group_id, p_business_group_id) = p_business_group_id;
46 
47 
48    /* Added for bug 6902514    -- change
49       Introduced "Transfer and Create STAT Batch in GMS" configuration type and the flexfield is named
50       PSP_CREATE_STAT_BATCH_IN_GMS,
51 
52       If the config type value = N,
53       STAT batch is not created on GMS side but tieback happens on LD tables as if GMS batch has been created for STAT data
54 
55       If the config type value = Y,
56       STAT batch gets created on GMS side, but the data cannot be distributed/costed as Grants product does not support STAT
57       transactions.
58     */
59 
60    CURSOR	create_stat_batch_in_gms_cur IS
61    SELECT	NVL(pcv_information1,'N')
62    FROM	pqp_configuration_values
63    WHERE	pcv_information_category = 'PSP_CREATE_STAT_BATCH_IN_GMS'
64    AND	legislation_code IS NULL
65    AND	NVL(business_group_id, p_business_group_id) = p_business_group_id;      -- change
66 
67  BEGIN
68 		fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Entering sum_and_transfer ');
69 
70 ---hr_utility.trace_on('Y','SandT');
71    g_error_api_path := '';
72    fnd_msg_pub.initialize;
73    psp_general.TRANSACTION_CHANGE_PURGEBLE;
74 
75 /* Following three variables initialized to faciliate calling mark_batch_end procedure
76    from GL_TIE_BACK procedure: Bug 1929317 */
77 
78  g_source_code :=  p_source_code;
79  g_batch_name  :=  p_batch_name;
80  g_time_period_id := p_time_period_id;
81  g_payroll_id := p_payroll_id;
82  -- g_currency_code := psp_general.get_currency_code(p_business_group_id); -- Commented the following code for Bug 2916848
83 
84  open autopop_config_cur; -- 5080403
85  fetch autopop_config_cur into g_suspense_autopop;
86  close autopop_config_cur;
87 
88 /*Added the following cursor open-fetch-close for Bug 6902514*/  -- change
89  open create_stat_batch_in_gms_cur;
90  fetch create_stat_batch_in_gms_cur into g_create_stat_batch_in_gms;
91  close create_stat_batch_in_gms_cur;  -- change
92 
93 
94 	g_dff_grouping_option := psp_general.get_act_dff_grouping_option(p_business_group_id);	-- Introduced for bug fix 2908859
95 
96 	 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE)  || ' calling mark_batch_begin');
97 
98  mark_batch_begin(p_source_type,
99                     p_source_code,
100          	    p_payroll_id,
101                     p_time_period_id,
102                     p_batch_name,
103 		    p_business_group_id,
104 		    p_set_of_books_id,
105                     l_return_status);
106 
107     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
108       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
109     END IF;
110 
111 	   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' FIRST NORMAL RUN');
112 		fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Calling create_gl_sum_lines');
113 
114 
115     -- FIRST NORMAL RUN
116     -- initiate the gl summarization and transfer
117     create_gl_sum_lines(p_source_type,
118                         p_source_code,
119                         p_time_period_id,
120                         p_batch_name,
121 			p_business_group_id,
122 			p_set_of_books_id,
123                         l_return_status);
124     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
125       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
126     END IF;
127 
128     -- call the user extension to populate attribute1 through attribute30
129     IF FND_PROFILE.VALUE('PSP_ST_EXTENSION_ENABLE') = 'Y' THEN
130       --- 2968684 added params and exception handlers to the following proc.
131       psp_st_ext.summary_ext_actual(p_source_type,
132                               p_source_code     ,
133                               p_payroll_id      ,
134                               p_time_period_id  ,
135                               p_batch_name      ,
136                               p_business_group_id,
137                               p_set_of_books_id  );
138     END IF;
139 
140 		 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Calling  transfer_to_gl_interface');
141 
142     transfer_to_gl_interface(p_source_type,
143                              p_source_code,
144                              p_time_period_id,
145                              p_batch_name,
146 			     p_business_group_id,
147 			     p_set_of_books_id,
148                              l_return_status);
149 
150     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
151       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
152     END IF;
153 
154 		   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Calling create_gms_sum_lines');
155 
156     create_gms_sum_lines(p_source_type,
157                          p_source_code,
158                          p_time_period_id,
159                          p_batch_name,
160 			 p_business_group_id,
161 			 p_set_of_books_id,
162                          l_return_status);
163     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
164       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
165     END IF;
166 
167     -- Call the user extension to populate  attribute1 thru attribute 30.
168 	IF FND_PROFILE.VALUE('PSP_ST_EXTENSION_ENABLE') = 'Y' THEN
169              --- 2968684 added params and exception handlers to the following proc.
170              psp_st_ext.summary_ext_actual(p_source_type,
171                               p_source_code     ,
172                               p_payroll_id      ,
173                               p_time_period_id  ,
174                               p_batch_name      ,
175                               p_business_group_id,
176                               p_set_of_books_id  );
177 	END IF;
178 
179            fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Calling  transfer_to_gms_interface');
180    transfer_to_gms_interface(p_source_type,
181                               p_source_code,
182                               p_time_period_id,
183                               p_batch_name,
184 			      p_business_group_id,
185 			      p_set_of_books_id,
186                               l_return_status);
187 
188     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
189       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
190     END IF;
191 
192 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' SECOND RUN TO TAKE CARE OF TIE-BACK');
193 
194 
195     -- SECOND RUN TO TAKE CARE OF TIE-BACK
196     -- initiate the gl summarization
197 	   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' calling create_gl_sum_lines');
198 
199     create_gl_sum_lines(p_source_type,
200                         p_source_code,
201                         p_time_period_id,
202                         p_batch_name,
203 			p_business_group_id,
204 			p_set_of_books_id,
205                         l_return_status);
206 
207     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
208       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
209     END IF;
210 
211     -- Call the user extension to populate  attribute1 thru attribute 30.
212 	IF FND_PROFILE.VALUE('PSP_ST_EXTENSION_ENABLE') = 'Y' THEN
213              --- 2968684 added params and exception handlers to the following proc.
214             psp_st_ext.summary_ext_actual(p_source_type,
215                               p_source_code     ,
216                               p_payroll_id      ,
217                               p_time_period_id  ,
218                               p_batch_name      ,
219                               p_business_group_id,
220                               p_set_of_books_id  );
221 	END IF;
222 
223          fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' calling  transfer_to_gl_interface');
224     transfer_to_gl_interface(p_source_type,
225                              p_source_code,
226                              p_time_period_id,
227                              p_batch_name,
228 			     p_business_group_id,
229 			     p_set_of_books_id,
230                              l_return_status);
231 
232     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
233       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
234     END IF;
235 
236 
237 		 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' calling create_gms_sum_lines');
238 
239     create_gms_sum_lines(p_source_type,
240                          p_source_code,
241                          p_time_period_id,
242                          p_batch_name,
243 			 p_business_group_id,
244 			 p_set_of_books_id,
245                          l_return_status);
246 
247     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
248       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
249     END IF;
250 
251     -- call the user extension to populate attribute1 through attribute30
252     IF FND_PROFILE.VALUE('PSP_ST_EXTENSION_ENABLE') = 'Y' THEN
253        --- 2968684 added params and exception handlers to the following proc.
254       psp_st_ext.summary_ext_actual(p_source_type,
255                               p_source_code     ,
256                               p_payroll_id      ,
257                               p_time_period_id  ,
258                               p_batch_name      ,
259                               p_business_group_id,
260                               p_set_of_books_id  );
261     END IF;
262 
263 		  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' calling  transfer_to_gms_interface');
264 
265     transfer_to_gms_interface(p_source_type,
266                               p_source_code,
267                               p_time_period_id,
268                               p_batch_name,
269 			      p_business_group_id,
270 			      p_set_of_books_id,
271                               l_return_status);
272 
273     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
274       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
275     END IF;
276 		   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' calling mark_batch_end');
277 
278 
279     mark_batch_end(p_source_type,
280                    p_source_code,
281                    p_time_period_id,
282                    p_batch_name,
283 		   p_business_group_id,
284 		   p_set_of_books_id,
285                    l_return_status);
286 
287     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
288       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
289     END IF;
290 
291           PSP_MESSAGE_S.Print_success;
292 
293  EXCEPTION
294     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
295 
296 	/* Introduced as part of bug fix #1776606 */
297 	ROLLBACK;
298 	/* Commented as part of Bug fix #1776606
299       mark_batch_end(p_source_type,
300                      p_source_code,
301                      p_time_period_id,
302                      p_batch_name,
303 		     p_business_group_id,
304 		     p_set_of_books_id,
305                      l_return_status);
306 
307       IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
308        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
309       END IF;
310 
311       fnd_msg_pub.get(p_msg_index     =>  FND_MSG_PUB.G_FIRST,
312                       p_encoded       =>  FND_API.G_FALSE,
313                       p_data          =>  l_msg_data,
314                       p_msg_index_out =>  l_msg_count); */
315 
316       g_error_api_path := 'SUM_AND_TRANSFER:'||g_error_api_path;
317       errbuf := SUBSTR(l_msg_data || fnd_global.local_chr(10) ||fnd_global.local_chr(10)||g_error_api_path,1,230);
318           psp_message_s.print_error(p_mode => FND_FILE.LOG,
319                                   p_print_header => FND_API.G_TRUE);
320       retcode := 2;
321     WHEN OTHERS THEN
322 			  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
323 
324 	/* Introduced as part of Bug fix #1776606 */
325 	ROLLBACK;
326 	/* Commented as part of Bug fix #1776606
327 
328       mark_batch_end(p_source_type,
329                      p_source_code,
330                      p_time_period_id,
331                      p_batch_name,
332 		     p_business_group_id,
333 		     p_set_of_books_id,
334                      l_return_status);
335 
336       IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
337        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
338       END IF;
339 
340       fnd_msg_pub.add_exc_msg('PSP_SUM_TRANS','SUM_AND_TRANSFER');
341       fnd_msg_pub.get(p_msg_index     =>  FND_MSG_PUB.G_FIRST,
342                       p_encoded       =>  FND_API.G_FALSE,
343                       p_data          =>  l_msg_data,
344                       p_msg_index_out =>  l_msg_count); */
345 
346       g_error_api_path := 'SUM_AND_TRANSFER:'||g_error_api_path;
347       errbuf := SUBSTR(l_msg_data || fnd_global.local_chr(10) ||fnd_global.local_chr(10)||g_error_api_path,1,230);
348           psp_message_s.print_error(p_mode => FND_FILE.LOG,
349                                   p_print_header => FND_API.G_TRUE);
350       retcode := 2;
351  END;
352 
353 -------------------- MARK BATCH BEGIN --------------------------------------------
354 
355  PROCEDURE mark_batch_begin(p_source_type     IN VARCHAR2,
356                             p_source_code     IN VARCHAR2,
357                             p_payroll_id      IN NUMBER,
358                             p_time_period_id  IN NUMBER,
359       			    p_batch_name      IN VARCHAR2,
360 			    p_business_group_id    IN NUMBER,
361 			    p_set_of_books_id      IN NUMBER,
362                             p_return_status  OUT NOCOPY VARCHAR2) IS
363 
364    CURSOR payroll_control_cur2 IS
365    SELECT payroll_control_id
366    FROM   psp_payroll_controls
367    WHERE  source_type = nvl(p_source_type,source_type)
368    AND    payroll_source_code = nvl(p_source_code,payroll_source_code)
369    AND    payroll_id    =  NVL (p_payroll_id, payroll_id)     -- Bug 7137063
370    AND    time_period_id = nvl(p_time_period_id,time_period_id)
371    AND    nvl(batch_name,'N') = nvl(nvl(p_batch_name,batch_name),'N')
372    AND    (dist_dr_amount IS NOT NULL OR dist_cr_amount IS NOT NULL)
373    AND    status_code in ('N','I')  --- added 'I' for 2444657
374    AND    business_group_id = p_business_group_id
375    AND    set_of_books_id = p_set_of_books_id
376    AND    source_type <> 'A'  -- Adjustments are delinked.
377    AND    parent_payroll_control_id is null;
378 
379   l_payroll_control_id integer;
380 
381    CURSOR payroll_control_cur IS
382    SELECT payroll_control_id,
383           source_type,
384           payroll_source_code,
385           time_period_id,
386           batch_name,
387           phase
388    FROM   psp_payroll_controls
389     where status_code in ('N','I')
390     and   source_type <> 'A'
391     and   payroll_control_id = l_payroll_control_id
392     or    parent_payroll_control_id = l_payroll_control_id;
393 
394    payroll_control_rec		payroll_control_cur%ROWTYPE;
395    l_gms_batch_name		NUMBER(15);
396    l_ti_not_complete		NUMBER(15);
397 
398  BEGIN
399   SELECT psp_st_run_id_s.nextval
400   INTO g_run_id
401   FROM dual;
402  open payroll_control_cur2;
403  loop
404   fetch payroll_control_cur2 into l_payroll_control_id;
405   if payroll_control_cur2%notfound then
406     close payroll_control_cur2;
407     exit;
408   end if;
409   OPEN payroll_control_cur;
410   LOOP
411    FETCH payroll_control_cur INTO payroll_control_rec;
412    IF payroll_control_cur%NOTFOUND THEN
413      CLOSE payroll_control_cur;
414      EXIT;
415    END IF;
416    UPDATE psp_payroll_controls
417    SET status_code = 'I',
418        run_id = g_run_id
419    WHERE payroll_control_id = payroll_control_rec.payroll_control_id;
420 
421    if (payroll_control_rec.phase = 'Submitted_TI_Request') then
422      select max(gms_batch_name)
423        into l_gms_batch_name
424        from psp_summary_lines
425       where payroll_control_id = payroll_control_rec.payroll_control_id;
426 
427       select count(*)
428 	into l_ti_not_complete
429 	from pa_transaction_interface_all
430        where transaction_status_code = 'I'
431 	 and batch_name = to_char(l_gms_batch_name)
432 	 and transaction_source in ('GOLD', 'OLD');
433 
434      if (l_ti_not_complete > 0) then
435          fnd_message.set_name('PSP','PSP_TI_DID_NOT_COMPLETE');
436          fnd_message.set_token('PAYROLL_CONTROL_ID', payroll_control_rec.payroll_control_id);
437          fnd_message.set_token('GMS_BATCH_NAME', l_gms_batch_name);
438          fnd_msg_pub.add;
439          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
440      end if;
441 
442    end if;
443 
444   END LOOP;
445  end loop;
446   /* Introduced as part of Bug fix #1776606 */
447  EXCEPTION
448 	WHEN OTHERS THEN
449 			 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
450 
451 		g_error_api_path := 'MARK_BATCH_BEGIN: ' || g_error_api_path;
452 		fnd_msg_pub.add_exc_msg('PSP_SUM_TRANS','MARK_BATCH_BEGIN');
453 		p_return_status := fnd_api.g_ret_sts_unexp_error;
454 		RAISE;
455  END;
456 -------------------- MARK BATCH END ---------------------------------------------
457 
458  PROCEDURE mark_batch_end(p_source_type     IN VARCHAR2,
459                           p_source_code     IN VARCHAR2,
460                           p_time_period_id  IN NUMBER,
461                           p_batch_name      IN VARCHAR2,
462 			  p_business_group_id IN NUMBER,
463 			  p_set_of_books_id   IN NUMBER,
464                           p_return_status  OUT NOCOPY VARCHAR2) IS
465 
466    CURSOR payroll_control_cur IS
467    SELECT payroll_control_id,
468           source_type,
469           payroll_source_code,
470           time_period_id,
471           batch_name,
472           phase
473    FROM   psp_payroll_controls
474    WHERE  status_code = 'I'
475    AND    run_id = g_run_id
476    AND    source_type <> 'A';
477 
478    payroll_control_rec		payroll_control_cur%ROWTYPE;
479    l_summary_line_id		NUMBER(10);
480    l_phase_counter             NUMBER (10) DEFAULT 0 ;
481 
482 --**************************************************************
483    l_line_counter			NUMBER DEFAULT 0;
484 --**************************************************************
485   l_return_status varchar2(10);
486   errbuf varchar2(1000);
487   retcode varchar2(1000);
488 
489 /* Start of Changes to check for migration before Supercedence */
490 
491   l_migration_status BOOLEAN:= psp_general.is_effort_report_migrated;
492 
493 
494   /*  End of Changes to check for migration before Supercedence   */
495 
496  BEGIN
497   --- call to supercede Effort reports
498 
499 
500 IF l_migration_status THEN
501 
502 
503 -- Introduced the above for checking on migration status
504 
505   SUPERCEDE_ER(g_run_id,
506                errbuf,
507                retcode,
508                p_source_type   ,
509                p_source_code   ,
510                p_time_period_id,
511                p_batch_name,
512                l_return_status);
513   IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
514       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
515   END IF;
516 
517 
518 End IF;
519 
520 
521 
522 
523 -- raise no_data_found;			Commented for bug fix 408937
524   OPEN payroll_control_cur;
525   LOOP
526    FETCH payroll_control_cur INTO payroll_control_rec;
527    IF payroll_control_cur%NOTFOUND THEN
528      CLOSE payroll_control_cur;
529      EXIT;
530    END IF;
531 
532 
533 	---- Bug 2444657: removed the code that was trying to reset the
534 	--- distribution lines status code to N for summary lines with status=N
535 	 --- becos this is redundant, anyway the dist lines will be with 'N'
536 	 --- Also removed update statment that makes summary lines status = R
537 	  -- whenever status = 'N', becos there is a new phase Summarize
538 
539    -- This part is used to delete the rejected summary lines, mark the status_code
540    -- in psp_payroll_controls to 'P' or 'N',delete the zero line reversal entry records
541 /* Bug 1929317.. Did some changes here...deleted the original code.....Look into previous version
542    to compare ..  Basically to check the number of lines in dist lines ...instead of summing on dist amount
543    for deciding wether to change control rec status to either 'P' or 'N'  */
544    IF payroll_control_rec.source_type = 'O' OR payroll_control_rec.source_type = 'N' THEN
545 
546       DELETE FROM psp_distribution_lines
547         WHERE distribution_amount = 0
548         AND payroll_sub_line_id IN (
549           select payroll_sub_line_id from psp_payroll_sub_lines where payroll_line_id IN (
550           select payroll_line_id from psp_payroll_lines where payroll_control_id IN (
551           select payroll_control_id from psp_payroll_controls where payroll_control_id=
552            payroll_control_rec.payroll_control_id)));
553 
554      SELECT count(*)
555      INTO l_line_counter
556      FROM psp_distribution_lines  pdl,
557           psp_payroll_sub_lines   ppsl,
558           psp_payroll_lines       ppl
559      WHERE ppl.payroll_control_id = payroll_control_rec.payroll_control_id
560      AND   ppl.payroll_line_id = ppsl.payroll_line_id
561      AND   ppsl.payroll_sub_line_id = pdl.payroll_sub_line_id;
562 
563      If l_line_counter = 0 then
564 
565        /* commented for 2445196
566         -- delete any rejected summary lines
567         DELETE FROM psp_summary_lines
568         WHERE payroll_control_id = payroll_control_rec.payroll_control_id
569         AND status_code = 'R';
570        */
571 
572         -- update status_code to 'P' in psp_payroll_controls
573         UPDATE psp_payroll_controls
574         SET status_code = 'P',
575             run_id = NULL,
576 	    phase = NULL
577         WHERE payroll_control_id = payroll_control_rec.payroll_control_id;
578      ELSE
579         UPDATE psp_payroll_controls
580         SET status_code = 'N',
581             run_id = NULL
582         WHERE payroll_control_id = payroll_control_rec.payroll_control_id;
583      END IF;
584    ELSIF payroll_control_rec.source_type = 'P' THEN
585 
586         -- delete the zero amount reversal lines
587         DELETE FROM psp_pre_gen_dist_lines
588         WHERE distribution_amount = 0
589         AND reversal_entry_flag = 'Y'
590         AND payroll_control_id = payroll_control_rec.payroll_control_id;
591 
592      SELECT count(*)
593      INTO l_line_counter
594      FROM psp_pre_gen_dist_lines ppgd
595      WHERE ppgd.payroll_control_id = payroll_control_rec.payroll_control_id;
596 
597      IF l_line_counter = 0 THEN
598        /* commented for  2445196
599         DELETE FROM psp_summary_lines
600         WHERE payroll_control_id = payroll_control_rec.payroll_control_id
601         AND status_code = 'R';
602        */
603 
604         UPDATE psp_payroll_controls
605         SET status_code = 'P',
606             run_id = NULL,
607 	    phase = NULL
608         WHERE payroll_control_id = payroll_control_rec.payroll_control_id;
609      ELSE
610         UPDATE psp_payroll_controls
611         SET status_code = 'N',
612             run_id = NULL
613         WHERE payroll_control_id = payroll_control_rec.payroll_control_id;
614      END IF;
615    END IF;
616   END LOOP;
617   COMMIT;
618 
619   /* Introduced as part of Bug fix #1776606 */
620  EXCEPTION
621 	WHEN OTHERS THEN
622 			 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
623 
624 		g_error_api_path := 'MARK_BATCH_END: ' || g_error_api_path;
625 		fnd_msg_pub.add_exc_msg('PSP_SUM_TRANS','MARK_BATCH_END');
626 		p_return_status := fnd_api.g_ret_sts_unexp_error;
627 		RAISE;
628  END;
629 -------------------- CREATE GL SUM LINES -----------------------------------------------
630 
631  PROCEDURE create_gl_sum_lines(p_source_type     IN VARCHAR2,
632                                p_source_code     IN VARCHAR2,
633                                p_time_period_id  IN NUMBER,
634                                p_batch_name      IN VARCHAR2,
635 			       p_business_group_id IN NUMBER,
636 			       p_set_of_books_id   IN NUMBER,
637                                p_return_status  OUT NOCOPY VARCHAR2) IS
638 
639    CURSOR payroll_control_cur IS
640    SELECT payroll_control_id,
641           source_type,
642           payroll_source_code,
643           time_period_id,
644           batch_name,
645           gl_posting_override_date,
646           business_group_id,
647           set_of_books_id,
648           currency_code -- Introduced for Bug 2916848 Ilo Mrc
649    FROM   psp_payroll_controls
650    WHERE source_type <> 'A'
651    AND    run_id = g_run_id
652    AND    (phase is null or
653            phase in ('GMS_Tie_Back', 'GL_Tie_Back'));  ---- added for 2444657
654 
655    CURSOR gl_sum_lines_cursor(P_PAYROLL_CONTROL_ID  IN  NUMBER)  IS
656    SELECT ppl.person_id,
657           ppl.assignment_id,
658           decode(pdl.reversal_entry_flag,'Y',ppl.gl_code_combination_id,NULL,
659            nvl(pdl.suspense_auto_glccid,     --- 5080403
660               nvl(pos.gl_code_combination_id,
661                   nvl(pdl.auto_gl_code_combination_id,   --- added for 2663344
662                   nvl(psl.gl_code_combination_id,
663                   nvl(pod.gl_code_combination_id,
664                   nvl(pea.gl_code_combination_id,
665                       pdls.gl_code_combination_id))))))) gl_ccid,
666           decode(pdl.reversal_entry_flag,'Y',decode(ppl.dr_cr_flag,'D','C','C','D'),NULL,ppl.dr_cr_flag) dr_cr_flag,
667           pdl.effective_date,
668           nvl(ppc.gl_posting_override_date, ppl.accounting_date)  accounting_date,
669           nvl(ppl.exchange_rate_type,ppc.exchange_rate_type) exchange_rate_type, --- added for 3108109
670           pdl.distribution_amount,
671           pdl.distribution_line_id,
672 	  pdl.auto_gl_code_combination_id,
673           'D' tab_flag,
674 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute_category, pos.attribute_category), NULL) attribute_category,	-- Introduced DFF columns for bug fix 2908859
675 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute1, pos.attribute8), NULL) attribute1,
676 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute2, pos.attribute8), NULL) attribute2,
677 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute3, pos.attribute8), NULL) attribute3,
678 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute4, pos.attribute8), NULL) attribute4,
679 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute5, pos.attribute8), NULL) attribute5,
680 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute6, pos.attribute8), NULL) attribute6,
681 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute7, pos.attribute8), NULL) attribute7,
682 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute8, pos.attribute8), NULL) attribute8,
683 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute9, pos.attribute9), NULL) attribute9,
684 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute10, pos.attribute10), NULL) attribute10
685           ---decode(pdl.suspense_org_account_id, NULL, 'N', 'Y') Suspense_Flag  commented for 2663344
686    FROM   psp_schedule_lines              psl,
687           psp_organization_accounts       pod,
688           psp_organization_accounts       pos,
689           psp_element_type_accounts       pea,
690           psp_default_labor_schedules     pdls,
691           psp_payroll_controls            ppc,
692           psp_payroll_lines               ppl,
693           psp_payroll_sub_lines           ppsl,
694           psp_distribution_lines          pdl
695    WHERE  pdl.status_code = 'N'
696    AND    pdl.payroll_sub_line_id = ppsl.payroll_sub_line_id
697    AND    ppsl.payroll_line_id = ppl.payroll_line_id
698    AND    ppl.payroll_control_id = ppc.payroll_control_id
699    AND    pdl.gl_project_flag = 'G'
700    AND    pdl.distribution_amount <> 0
701    AND    ppc.payroll_control_id = p_payroll_control_id
702    AND    ppc.business_group_id = p_business_group_id
703    AND	  ppc.set_of_books_id = p_set_of_books_id
704    AND    pdl.schedule_line_id = psl.schedule_line_id(+)
705    AND    pdl.default_org_account_id = pod.organization_account_id(+)
706    AND    pdl.element_account_id = pea.element_account_id(+)
707    AND    pdl.org_schedule_id = pdls.org_schedule_id(+)
708    AND    pdl.suspense_org_account_id = pos.organization_account_id(+)
709    AND    pdl.cap_excess_glccid is null
710    UNION
711    SELECT ppg.person_id,
712           ppg.assignment_id,
713            nvl(ppg.suspense_auto_glccid,
714           nvl(pos.gl_code_combination_id,
715               ppg.gl_code_combination_id)) gl_ccid,
716 	  decode(ppg.reversal_entry_flag, 'Y', decode(ppg.dr_cr_flag, 'C', 'D', 'D', 'C'), ppg.dr_cr_flag) dr_cr_flag,
717           ppg.effective_date,
718           ppc.gl_posting_override_date accounting_date, --- 3108109
719           ppc.exchange_rate_type, --- added for 3108109
720           ppg.distribution_amount,
721           ppg.pre_gen_dist_line_id distribution_line_id,
722 	  to_number(NULL)  auto_gl_code_combination_id, -- Place holder for auto pop details
723           'P' tab_flag,
724 	DECODE(g_dff_grouping_option, 'Y', DECODE(ppg.suspense_org_account_id, NULL, ppg.attribute_category, pos.attribute_category), NULL) attribute_category,	-- Introduced DFF columns for bug fix 2908859
725 	DECODE(g_dff_grouping_option, 'Y', DECODE(ppg.suspense_org_account_id, NULL, ppg.attribute1, pos.attribute1), NULL) attribute1,
726 	DECODE(g_dff_grouping_option, 'Y', DECODE(ppg.suspense_org_account_id, NULL, ppg.attribute2, pos.attribute2), NULL) attribute2,
727 	DECODE(g_dff_grouping_option, 'Y', DECODE(ppg.suspense_org_account_id, NULL, ppg.attribute3, pos.attribute3), NULL) attribute3,
728 	DECODE(g_dff_grouping_option, 'Y', DECODE(ppg.suspense_org_account_id, NULL, ppg.attribute4, pos.attribute4), NULL) attribute4,
729 	DECODE(g_dff_grouping_option, 'Y', DECODE(ppg.suspense_org_account_id, NULL, ppg.attribute5, pos.attribute5), NULL) attribute5,
730 	DECODE(g_dff_grouping_option, 'Y', DECODE(ppg.suspense_org_account_id, NULL, ppg.attribute6, pos.attribute6), NULL) attribute6,
731 	DECODE(g_dff_grouping_option, 'Y', DECODE(ppg.suspense_org_account_id, NULL, ppg.attribute7, pos.attribute7), NULL) attribute7,
732 	DECODE(g_dff_grouping_option, 'Y', DECODE(ppg.suspense_org_account_id, NULL, ppg.attribute8, pos.attribute8), NULL) attribute8,
733 	DECODE(g_dff_grouping_option, 'Y', DECODE(ppg.suspense_org_account_id, NULL, ppg.attribute9, pos.attribute9), NULL) attribute9,
734 	DECODE(g_dff_grouping_option, 'Y', DECODE(ppg.suspense_org_account_id, NULL, ppg.attribute10, pos.attribute10), NULL) attribute10
735           ---decode(ppg.suspense_org_Account_id, NULL, 'N', 'Y') Suspense_Flag   commented for 2663344
736    FROM   psp_pre_gen_dist_lines     ppg,
737           psp_organization_accounts  pos,
738           psp_payroll_controls       ppc
739    WHERE  ppg.status_code = 'N'
740    /* changed following condition for bug 2007521 */
741    AND    ((ppg.gl_code_combination_id IS NOT NULL  and pos.project_id is null)
742           OR pos.gl_code_combination_id IS NOT NULL)
743    AND    ppg.suspense_org_account_id = pos.organization_account_id(+)
744    AND    ppg.payroll_control_id = p_payroll_control_id
745    AND    ppg.business_group_id = p_business_group_id
746    AND	  ppg.set_of_books_id = p_set_of_books_id
747    AND    ppc.payroll_control_id = p_payroll_control_id
748    UNION
749    SELECT ppl.person_id,
750           ppl.assignment_id,
751           decode(pdl.reversal_entry_flag,'Y',ppl.gl_code_combination_id,NULL,
752               nvl(pos.gl_code_combination_id,
753                   nvl(pdl.auto_gl_code_combination_id, pdl.cap_excess_glccid))) gl_ccid,
754           decode(pdl.reversal_entry_flag,'Y',decode(ppl.dr_cr_flag,'D','C','C','D'),NULL,ppl.dr_cr_flag) dr_cr_flag,
755           pdl.effective_date,
756           nvl(ppc.gl_posting_override_date, ppl.accounting_date)  accounting_date,
757           nvl(ppl.exchange_rate_type,ppc.exchange_rate_type) exchange_rate_type, --- added for 3108109
758           pdl.distribution_amount,
759           pdl.distribution_line_id,
760 	  pdl.auto_gl_code_combination_id,
761           'D' tab_flag,
762 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute_category, pos.attribute_category), NULL) attribute_category,
763 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute1, pos.attribute8), NULL) attribute1,
764 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute2, pos.attribute8), NULL) attribute2,
765 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute3, pos.attribute8), NULL) attribute3,
766 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute4, pos.attribute8), NULL) attribute4,
767 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute5, pos.attribute8), NULL) attribute5,
768 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute6, pos.attribute8), NULL) attribute6,
769 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute7, pos.attribute8), NULL) attribute7,
770 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute8, pos.attribute8), NULL) attribute8,
771 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute9, pos.attribute9), NULL) attribute9,
772 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute10, pos.attribute10), NULL) attribute10
773    FROM   psp_payroll_controls            ppc,
774           psp_payroll_lines               ppl,
775           psp_payroll_sub_lines           ppsl,
776           psp_organization_accounts       pos,
777           psp_distribution_lines          pdl
778    WHERE  pdl.status_code = 'N'
779    AND    pdl.payroll_sub_line_id = ppsl.payroll_sub_line_id
780    AND    ppsl.payroll_line_id = ppl.payroll_line_id
781    AND    ppl.payroll_control_id = ppc.payroll_control_id
782    AND    pdl.gl_project_flag = 'G'
783    AND    pdl.distribution_amount <> 0
784    AND    ppc.payroll_control_id = p_payroll_control_id
785    AND    ppc.business_group_id = p_business_group_id
786    AND	  ppc.set_of_books_id = p_set_of_books_id
787    AND    pdl.suspense_org_account_id = pos.organization_account_id(+)
788    AND    pdl.cap_excess_glccid is not null
789    AND    pdl.reversal_entry_flag is null
790    ORDER BY 1,2,3,4,6,7,12,13,14,15,16,17,18,19,20,21,22,5;
791       --- order by accounting_date, exchange_rate_type for 3108109
792       --- order by attribute_category, attribute1 through attribute 10 for BUG 6007017
793 
794 
795 --   l_sob_id			NUMBER(15) := FND_PROFILE.VALUE('PSP_SET_OF_BOOKS');  -- This comes as a param
796    l_person_id			NUMBER(9);
797    l_assignment_id		NUMBER(9);
798    l_gl_ccid			NUMBER(15);
799    l_dr_cr_flag			VARCHAR2(1);
800    l_effective_date		DATE;
801    l_distribution_amount	NUMBER;
802    l_rec_count			NUMBER := 0;
803    l_summary_amount		NUMBER := 0;
804 
805    l_summary_line_id		NUMBER(10);
806    gl_sum_lines_rec		gl_sum_lines_cursor%ROWTYPE;
807 
808    l_attribute_category			VARCHAR2(30);			-- Introduced variables for storing DFF values for bug fix 2908859
809    l_attribute1				VARCHAR2(150);
810    l_attribute2				VARCHAR2(150);
811    l_attribute3				VARCHAR2(150);
812    l_attribute4				VARCHAR2(150);
813    l_attribute5				VARCHAR2(150);
814    l_attribute6				VARCHAR2(150);
815    l_attribute7				VARCHAR2(150);
816    l_attribute8				VARCHAR2(150);
817    l_attribute9				VARCHAR2(150);
818    l_attribute10			VARCHAR2(150);
819 
820    TYPE dist_id IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
821    dist_line_id			dist_id;
822    l_dist_line_id			NUMBER;
823    i					BINARY_INTEGER := 0;
824    j					NUMBER;
825    l_return_status		VARCHAR2(10);
826    payroll_control_rec		payroll_control_cur%ROWTYPE;
827    l_precision			NUMBER;
828    l_ext_precision		NUMBER;
829    l_accounting_date            DATE;  --- added 6 variables for 3108109
830    l_exchange_rate_type         VARCHAR2(30);
831    l_value                      VARCHAR2(200);
832    l_table                      VARCHAR2(100);
833    l_period_end_date            DATE;
834    l_begin_of_time              DATE := to_date('01/01/1900','dd/mm/yyyy');
835 
836  BEGIN
837 	   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Entered create_gl_summary_lines');
838 
839   OPEN payroll_control_cur;
840   LOOP
841    FETCH payroll_control_cur INTO payroll_control_rec;
842    IF payroll_control_cur%NOTFOUND THEN
843      CLOSE payroll_control_cur;
844      EXIT;
845    END IF;
846 
847       -- added for 3108109
848      BEGIN
849        SELECT end_date
850        INTO l_period_end_date
851        FROM per_time_periods
852        WHERE time_period_id = payroll_control_rec.time_period_id;
853      EXCEPTION
854        WHEN NO_DATA_FOUND THEN
855          l_value := 'Time Period Id = '||to_char(payroll_control_rec.time_period_id);
856          l_table := 'PER_TIME_PERIODS';
857          fnd_message.set_name('PSP','PSP_TR_VALUE_NOT_FOUND');
858          fnd_message.set_token('VALUE',l_value);
859          fnd_message.set_token('TABLE',l_table);
860          fnd_msg_pub.add;
861          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
862      END;
863 
864 
865 -- Made the following call for Bug 2916848 to populate l_precision , l_ext_precision
866    psp_general.get_currency_precision(payroll_control_rec.currency_code,l_precision,l_ext_precision);
867 
868    -- create balancing transactions for GL
869    gl_balance_transaction(payroll_control_rec.source_type,
870                           payroll_control_rec.payroll_control_id,
871                           p_business_group_id,
872                           p_set_of_books_id,
873 			  l_precision,-- Introduced for Bug 2916848
874                           l_return_status);
875 
876    IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
877      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
878    END IF;
879 
880 			 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' openinging gl_sum_lines_cursor');
881 
882    OPEN gl_sum_lines_cursor(payroll_control_rec.payroll_control_id);
883    l_rec_count := 0;
884    l_summary_amount := 0;
885    i := 0;
886    LOOP
887      FETCH gl_sum_lines_cursor INTO gl_sum_lines_rec;
888      l_rec_count := l_rec_count + 1;
889 
890      IF gl_sum_lines_cursor%ROWCOUNT = 0 THEN
891        CLOSE gl_sum_lines_cursor;
892        EXIT;
893      ELSIF gl_sum_lines_cursor%NOTFOUND THEN
894       update psp_payroll_controls set phase = 'Summarize_GL_Lines' ---2444657:changed from NULL
895        where payroll_control_id = payroll_control_rec.payroll_control_id;
896        CLOSE gl_sum_lines_cursor;
897        EXIT;
898      END IF;
899      --
900 
901      IF l_rec_count = 1 THEN
902 			   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || '   l_rec_count = 1');
903 
904        l_person_id		:= gl_sum_lines_rec.person_id;
905        l_assignment_id		:= gl_sum_lines_rec.assignment_id;
906        l_gl_ccid		:= gl_sum_lines_rec.gl_ccid;
907        l_dr_cr_flag		:= gl_sum_lines_rec.dr_cr_flag;
908        l_effective_date		:= nvl(payroll_control_rec.gl_posting_override_date,gl_sum_lines_rec.effective_date);
909        l_accounting_date        := gl_sum_lines_rec.accounting_date;   -- added for 3108109
910        l_exchange_rate_type     := gl_sum_lines_rec.exchange_rate_type;    --- added for 3108109
911 	l_attribute_category	:= gl_sum_lines_rec.attribute_category;		-- Introduced DFF columns for bug fix 2908859
912 	l_attribute1		:= gl_sum_lines_rec.attribute1;
913 	l_attribute2		:= gl_sum_lines_rec.attribute2;
914 	l_attribute3		:= gl_sum_lines_rec.attribute3;
915 	l_attribute4		:= gl_sum_lines_rec.attribute4;
916 	l_attribute5		:= gl_sum_lines_rec.attribute5;
917 	l_attribute6		:= gl_sum_lines_rec.attribute6;
918 	l_attribute7		:= gl_sum_lines_rec.attribute7;
919 	l_attribute8		:= gl_sum_lines_rec.attribute8;
920 	l_attribute9		:= gl_sum_lines_rec.attribute9;
921 	l_attribute10		:= gl_sum_lines_rec.attribute10;
922      END IF;
923 
924      IF l_person_id <> gl_sum_lines_rec.person_id OR
925         l_assignment_id <> gl_sum_lines_rec.assignment_id OR
926         l_gl_ccid <> gl_sum_lines_rec.gl_ccid OR
927         l_dr_cr_flag <> gl_sum_lines_rec.dr_cr_flag OR
928 	(NVL(l_attribute_category, 'NULL') <> NVL(gl_sum_lines_rec.attribute_category, 'NULL')) OR	-- Introduced DFF columns checks for bug fix 2908859
929 	(NVL(l_attribute1, 'NULL') <> NVL(gl_sum_lines_rec.attribute1, 'NULL')) OR
930 	(NVL(l_attribute2, 'NULL') <> NVL(gl_sum_lines_rec.attribute2, 'NULL')) OR
931 	(NVL(l_attribute3, 'NULL') <> NVL(gl_sum_lines_rec.attribute3, 'NULL')) OR
932 	(NVL(l_attribute4, 'NULL') <> NVL(gl_sum_lines_rec.attribute4, 'NULL')) OR
933 	(NVL(l_attribute5, 'NULL') <> NVL(gl_sum_lines_rec.attribute5, 'NULL')) OR
934 	(NVL(l_attribute6, 'NULL') <> NVL(gl_sum_lines_rec.attribute6, 'NULL')) OR
935 	(NVL(l_attribute7, 'NULL') <> NVL(gl_sum_lines_rec.attribute7, 'NULL')) OR
936 	(NVL(l_attribute8, 'NULL') <> NVL(gl_sum_lines_rec.attribute8, 'NULL')) OR
937 	(NVL(l_attribute9, 'NULL') <> NVL(gl_sum_lines_rec.attribute9, 'NULL')) OR
938 	(NVL(l_attribute10, 'NULL') <> NVL(gl_sum_lines_rec.attribute10, 'NULL')) OR
939         nvl(l_accounting_date,l_begin_of_time) <>
940              nvl(gl_sum_lines_rec.accounting_date,l_begin_of_time) OR  --------- added two condns for 3108109
941         nvl(l_exchange_rate_type,'-999') <>
942              nvl(gl_sum_lines_rec.exchange_rate_type,'-999') THEN
943 
944            fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' calling insert_into_summary_lines');
945           -- insert into summary lines
946         insert_into_summary_lines(
947             l_summary_line_id,
948 		l_person_id,
949 		l_assignment_id,
950             payroll_control_rec.time_period_id,
951  		l_effective_date,
952                 nvl(l_accounting_date,l_period_end_date),
953                 l_exchange_rate_type,  --- added 2 vars for 3108109
954             payroll_control_rec.source_type,
955  		payroll_control_rec.payroll_source_code,
956 	    payroll_control_rec.set_of_books_id,
957  		l_gl_ccid,
958  		NULL,
959  		NULL,
960  		NULL,
961  		NULL,
962  		NULL,
963  		round(l_summary_amount,l_precision), -- For Bug 2916848 Ilo Mrc Ehn
964  		l_dr_cr_flag,
965  		'N',
966             payroll_control_rec.batch_name,
967             payroll_control_rec.payroll_control_id,
968 	    payroll_control_rec.business_group_id,
969 		l_attribute_category,			-- Introduced DFF columns for bug fix 2908859
970 		l_attribute1,
971 		l_attribute2,
972 		l_attribute3,
973 		l_attribute4,
974 		l_attribute5,
975 		l_attribute6,
976 		l_attribute7,
977 		l_attribute8,
978 		l_attribute9,
979 		l_attribute10,
980             l_return_status);
981 
982        IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
983          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
984        END IF;
985 
986 
987        FOR j IN 1 .. dist_line_id.COUNT LOOP
988          l_dist_line_id := dist_line_id(j);
989 
990          IF gl_sum_lines_rec.tab_flag = 'D' THEN
991 
992                 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || '   updating psp_distribution_lines setting summary_line_id : '||l_summary_line_id);
993 
994            UPDATE psp_distribution_lines
995            SET summary_line_id = l_summary_line_id WHERE distribution_line_id = l_dist_line_id;
996          ELSIF gl_sum_lines_rec.tab_flag = 'P' THEN
997 
998 					fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || '   updating psp_pre_gen_dist_lines setting summary_line_id : '||l_summary_line_id);
999 
1000            UPDATE psp_pre_gen_dist_lines
1001            SET summary_line_id = l_summary_line_id WHERE pre_gen_dist_line_id = l_dist_line_id;
1002          END IF;
1003        END LOOP;
1004 
1005        -- initialise the summary amount and dist_line_id
1006        l_summary_amount := 0;
1007        dist_line_id.delete;
1008        i := 0;
1009      END IF;
1010 
1011      l_person_id			:= gl_sum_lines_rec.person_id;
1012      l_assignment_id		:= gl_sum_lines_rec.assignment_id;
1013      l_gl_ccid			:= gl_sum_lines_rec.gl_ccid;
1014      l_dr_cr_flag			:= gl_sum_lines_rec.dr_cr_flag;
1015      l_exchange_rate_type       := gl_sum_lines_rec.exchange_rate_type; --- added for 3108109
1016      l_accounting_date          := gl_sum_lines_rec.accounting_date;
1017 	l_attribute_category	:= gl_sum_lines_rec.attribute_category;		-- Introduced DFF columns for bug fix 2908859
1018 	l_attribute1		:= gl_sum_lines_rec.attribute1;
1019 	l_attribute2		:= gl_sum_lines_rec.attribute2;
1020 	l_attribute3		:= gl_sum_lines_rec.attribute3;
1021 	l_attribute4		:= gl_sum_lines_rec.attribute4;
1022 	l_attribute5		:= gl_sum_lines_rec.attribute5;
1023 	l_attribute6		:= gl_sum_lines_rec.attribute6;
1024 	l_attribute7		:= gl_sum_lines_rec.attribute7;
1025 	l_attribute8		:= gl_sum_lines_rec.attribute8;
1026 	l_attribute9		:= gl_sum_lines_rec.attribute9;
1027 	l_attribute10		:= gl_sum_lines_rec.attribute10;
1028 -- If gl_posting_override_date is given, use it first
1029      l_effective_date		:= nvl(payroll_control_rec.gl_posting_override_date,gl_sum_lines_rec.effective_date);
1030 -- If gl_posting_override_date is given, use it first
1031 
1032      l_summary_amount := l_summary_amount + gl_sum_lines_rec.distribution_amount;
1033      i := i + 1;
1034      dist_line_id(i) := gl_sum_lines_rec.distribution_line_id;
1035 
1036    END LOOP;
1037 
1038    IF l_rec_count > 1 THEN
1039 
1040 	fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || '   l_rec_count: '||l_rec_count);
1041 	fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || '   calling insert_into_summary_lines');
1042 
1043      -- insert into summary lines
1044      insert_into_summary_lines(
1045             l_summary_line_id,
1046 		l_person_id,
1047 		l_assignment_id,
1048             payroll_control_rec.time_period_id,
1049  		l_effective_date,
1050                 nvl(l_accounting_date,l_period_end_date), --- added for 3108109
1051                 l_exchange_rate_type,
1052             payroll_control_rec.source_type,
1053  		payroll_control_rec.payroll_source_code,
1054 	    payroll_control_rec.set_of_books_id,
1055  		l_gl_ccid,
1056  		NULL,
1057  		NULL,
1058  		NULL,
1059  		NULL,
1060  		NULL,
1061  		round(l_summary_amount, l_precision),
1062  		l_dr_cr_flag,
1063  		'N',
1064             payroll_control_rec.batch_name,
1065             payroll_control_rec.payroll_control_id,
1066             payroll_control_rec.business_group_id,
1067 		l_attribute_category,			-- Introduced DFF columns for bug fix 2908859
1068 		l_attribute1,
1069 		l_attribute2,
1070 		l_attribute3,
1071 		l_attribute4,
1072 		l_attribute5,
1073 		l_attribute6,
1074 		l_attribute7,
1075 		l_attribute8,
1076 		l_attribute9,
1077 		l_attribute10,
1078             l_return_status);
1079 
1080 
1081      IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1082        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1083      END IF;
1084 
1085 
1086 
1087      FOR j IN 1 .. dist_line_id.COUNT LOOP
1088        l_dist_line_id := dist_line_id(j);
1089 
1090        IF gl_sum_lines_rec.tab_flag = 'D' THEN
1091 			fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || '   updating psp_distribution_lines status_code to N and summary_line_id to '||l_summary_line_id||' for dist_line_id: '||l_dist_line_id);
1092 
1093          UPDATE psp_distribution_lines
1094          SET summary_line_id = l_summary_line_id,
1095              status_code = 'N'
1096          WHERE distribution_line_id = l_dist_line_id;
1097        ELSIF gl_sum_lines_rec.tab_flag = 'P' THEN
1098 
1099 					fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || '   updating psp_distribution_lines status_code to N and summary_line_id to '||l_summary_line_id||' for dist_line_id: '||l_dist_line_id);
1100 
1101          UPDATE psp_pre_gen_dist_lines
1102          SET summary_line_id = l_summary_line_id,
1103              status_code = 'N'
1104          WHERE pre_gen_dist_line_id = l_dist_line_id;
1105        END IF;
1106      END LOOP;
1107 
1108      dist_line_id.delete;
1109    END IF;
1110 
1111   END LOOP;
1112   --
1113   p_return_status := fnd_api.g_ret_sts_success;
1114 
1115 			 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Leaving create_gl_sum_lines');
1116 
1117  EXCEPTION
1118 
1119    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1120      g_error_api_path := 'CREATE_GL_SUM_LINES:'||g_error_api_path;
1121      p_return_status := fnd_api.g_ret_sts_unexp_error;
1122    WHEN OTHERS THEN
1123 				   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
1124 
1125      g_error_api_path := 'CREATE_GL_SUM_LINES:'||g_error_api_path;
1126      fnd_msg_pub.add_exc_msg('PSP_SUM_TRANS','CREATE_GL_SUM_LINES');
1127      p_return_status := fnd_api.g_ret_sts_unexp_error;
1128 
1129  END;
1130 
1131 
1132 ---------------------- I N S E R T   S T A T E M E N T  ------------------------------------
1133  PROCEDURE insert_into_summary_lines(
1134 		P_SUMMARY_LINE_ID			OUT NOCOPY	NUMBER,
1135 		P_PERSON_ID				IN	NUMBER,
1136 		P_ASSIGNMENT_ID			IN	NUMBER,
1137 		P_TIME_PERIOD_ID			IN	NUMBER,
1138  		P_EFFECTIVE_DATE			IN	DATE,
1139                 P_ACCOUNTING_DATE               IN      DATE,   --- added 2 vars for 3108109
1140                 P_EXCHANGE_RATE_TYPE            IN      VARCHAR2,
1141             P_SOURCE_TYPE			IN	VARCHAR2,
1142  		P_SOURCE_CODE			IN	VARCHAR2,
1143 		P_SET_OF_BOOKS_ID			IN	NUMBER,
1144  		P_GL_CODE_COMBINATION_ID	IN	NUMBER,
1145  		P_PROJECT_ID			IN	NUMBER,
1146  		P_EXPENDITURE_ORGANIZATION_ID	IN	NUMBER,
1147  		P_EXPENDITURE_TYPE		IN	VARCHAR2,
1148  		P_TASK_ID				IN	NUMBER,
1149  		P_AWARD_ID				IN	NUMBER,
1150  		P_SUMMARY_AMOUNT			IN	NUMBER,
1151  		P_DR_CR_FLAG			IN	VARCHAR2,
1152  		P_STATUS_CODE			IN	VARCHAR2,
1153             P_INTERFACE_BATCH_NAME		IN	VARCHAR2,
1154 		P_PAYROLL_CONTROL_ID		IN	NUMBER,
1155 		P_BUSINESS_GROUP_ID		IN	NUMBER,
1156 		p_attribute_category		IN	VARCHAR2,			-- Introduced DFF parameters for bug fix 2908859
1157 		p_attribute1			IN	VARCHAR2,
1158 		p_attribute2			IN	VARCHAR2,
1159 		p_attribute3			IN	VARCHAR2,
1160 		p_attribute4			IN	VARCHAR2,
1161 		p_attribute5			IN	VARCHAR2,
1162 		p_attribute6			IN	VARCHAR2,
1163 		p_attribute7			IN	VARCHAR2,
1164 		p_attribute8			IN	VARCHAR2,
1165 		p_attribute9			IN	VARCHAR2,
1166 		p_attribute10			IN	VARCHAR2,
1167         P_RETURN_STATUS			OUT NOCOPY   VARCHAR2,
1168 		P_ORG_ID				IN	NUMBER DEFAULT NULL			-- R12 MOAc uptake
1169 		) IS
1170             l_gms_posting_effective_date	DATE;
1171  BEGIN
1172 
1173 -- Code added for Enhancement Employee Assignment with Zero Work Days
1174  IF P_PROJECT_ID IS NOT NULL THEN
1175  	l_gms_posting_effective_date:= p_effective_date;
1176  	psp_general.get_gms_effective_date(p_person_id,l_gms_posting_effective_date);
1177  END IF;
1178 -- Code ended for Enhancement Employee Assignment with Zero Work Days
1179 
1180     SELECT PSP_SUMMARY_LINES_S.NEXTVAL
1181     INTO P_SUMMARY_LINE_ID
1182     FROM DUAL;
1183     INSERT INTO PSP_SUMMARY_LINES(
1184 		SUMMARY_LINE_ID,
1185 		PERSON_ID,
1186 		ASSIGNMENT_ID,
1187 		TIME_PERIOD_ID,
1188  		EFFECTIVE_DATE,
1189                 ACCOUNTING_DATE, --- added 2 vars for 3108109
1190                 EXCHANGE_RATE_TYPE,
1191  		GMS_POSTING_EFFECTIVE_DATE, /* New column added for Enhancement Employee Assignment with Zero Work Days */
1192             	SOURCE_TYPE,
1193  		SOURCE_CODE,
1194 		SET_OF_BOOKS_ID,
1195  		GL_CODE_COMBINATION_ID,
1196  		PROJECT_ID,
1197  		EXPENDITURE_ORGANIZATION_ID,
1198  		EXPENDITURE_TYPE,
1199  		TASK_ID,
1200  		AWARD_ID,
1201  		SUMMARY_AMOUNT,
1202  		DR_CR_FLAG,
1203  		STATUS_CODE,
1204             	INTERFACE_BATCH_NAME,
1205             	PAYROLL_CONTROL_ID,
1206 		BUSINESS_GROUP_ID,
1207 		LAST_UPDATE_DATE,
1208 		LAST_UPDATED_BY,
1209 		LAST_UPDATE_LOGIN,
1210 		CREATED_BY,
1211 		CREATION_DATE,
1212 		ACTUAL_SUMMARY_AMOUNT,   --For Bug 2496661
1213 		attribute_category,				-- Introduced for bug fix 2908859
1214 		attribute1,
1215 		attribute2,
1216 		attribute3,
1217 		attribute4,
1218 		attribute5,
1219 		attribute6,
1220 		attribute7,
1221 		attribute8,
1222 		attribute9,
1223 		attribute10,
1224 		org_id			-- R12 MOAc uptake
1225 		)
1226     VALUES(
1227             P_SUMMARY_LINE_ID,
1228 		P_PERSON_ID,
1229 		P_ASSIGNMENT_ID,
1230 		P_TIME_PERIOD_ID,
1231  		P_EFFECTIVE_DATE,
1232                 P_ACCOUNTING_DATE,  -- added 2 vars for 3108109
1233                 P_EXCHANGE_RATE_TYPE,
1234  		L_GMS_POSTING_EFFECTIVE_DATE, /* New column added for Enhancement Employee Assignment with Zero Work Days */
1235             	P_SOURCE_TYPE,
1236  		P_SOURCE_CODE,
1237 		P_SET_OF_BOOKS_ID,
1238  		P_GL_CODE_COMBINATION_ID,
1239  		P_PROJECT_ID,
1240  		P_EXPENDITURE_ORGANIZATION_ID,
1241  		P_EXPENDITURE_TYPE,
1242  		P_TASK_ID,
1243  		P_AWARD_ID,
1244  		P_SUMMARY_AMOUNT,
1245  		P_DR_CR_FLAG,
1246  		P_STATUS_CODE,
1247             P_INTERFACE_BATCH_NAME,
1248             P_PAYROLL_CONTROL_ID,
1249 		P_BUSINESS_GROUP_ID,
1250 		SYSDATE,
1251 		FND_GLOBAL.USER_ID,
1252 		FND_GLOBAL.LOGIN_ID,
1253 		FND_GLOBAL.USER_ID,
1254 		SYSDATE,
1255 		DECODE(P_PROJECT_ID, NULL,P_SUMMARY_AMOUNT, DECODE(P_DR_CR_FLAG,'C',0 - P_SUMMARY_AMOUNT,P_SUMMARY_AMOUNT)), --For Bug 2496661
1256 		p_attribute_category,				-- Introduced for bug fix 2908859
1257 		p_attribute1,
1258 		p_attribute2,
1259 		p_attribute3,
1260 		p_attribute4,
1261 		p_attribute5,
1262 		p_attribute6,
1263 		p_attribute7,
1264 		p_attribute8,
1265 		p_attribute9,
1266 		p_attribute10,
1267 		p_org_id			-- R12 MOAC uptake
1268 		);
1269     --
1270     p_return_status := fnd_api.g_ret_sts_success;
1271  EXCEPTION
1272    WHEN OTHERS THEN
1273 			 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
1274 
1275       --dbms_output.put_line('Errrrrrrrrrrrrrrrrrrrrrrrrrrorrrrrrrrrrr.........');
1276       g_error_api_path := 'INSERT_INTO_SUMMARY_LINES:'||g_error_api_path;
1277       fnd_msg_pub.add_exc_msg('PSP_SUM_TRANS','INSERT_INTO_SUMMARY_LINES');
1278       p_return_status := fnd_api.g_ret_sts_unexp_error;
1279  END;
1280 
1281 ------------------------ GL INTERFACE --------------------------------------------------
1282 
1283  PROCEDURE transfer_to_gl_interface(p_source_type     IN VARCHAR2,
1284                                     p_source_code     IN VARCHAR2,
1285                                     p_time_period_id  IN NUMBER,
1286                                     p_batch_name      IN VARCHAR2,
1287 				    p_business_group_id IN NUMBER,
1288 				    p_set_of_books_id   IN NUMBER,
1289                                     p_return_status  OUT NOCOPY VARCHAR2) IS
1290 
1291  /* Broke the gl_batch_cursor into gl_batch_cursor,gl_payroll_control_cursor  for Bug 3112053 */
1292    CURSOR gl_batch_cursor IS
1293    SELECT DISTINCT source_type,
1294           payroll_source_code,
1295           time_period_id,
1296           batch_name,
1297           phase  --- 2444657, to separate the old and new gl imports
1298                  --- S and T can pull control recs with statuses 'I' and 'N' now
1299    FROM   psp_payroll_controls
1300    WHERE  status_code = 'I'
1301    AND    source_type <> 'A'
1302    AND    run_id = g_run_id
1303    AND    phase in ('Summarize_GL_Lines', 'Submitted_JI_Request');  --- 2444657: added the condition
1304 
1305 
1306    CURSOR gl_payroll_control_cursor(p_source_type in varchar2, p_source_code in varchar2,
1307 				     p_time_period_id in number, p_batch_name in varchar2,
1308                                      p_phase varchar2) IS
1309 
1310    SELECT payroll_control_id,
1311 	  currency_code,
1312 	  exchange_rate_type,
1313           phase    --- 2444657: added
1314    FROM   psp_payroll_controls
1315    WHERE  source_type = p_source_type
1316    AND    payroll_source_code = p_source_code
1317    AND    time_period_id = p_time_period_id
1318    AND    nvl(batch_name,'N') = nvl(nvl(p_batch_name,batch_name),'N')
1319    AND    (dist_dr_amount IS NOT NULL OR dist_cr_amount IS NOT NULL)
1320    AND    status_code = 'I'
1321    AND    source_type <> 'A'
1322    AND    business_group_id = p_business_group_id
1323    AND    set_of_books_id = p_set_of_books_id
1324    AND    run_id = g_run_id
1325    AND    phase = p_phase;  --- 2444657:  added the condition;
1326 
1327 /* End of code changes for bug 3112053 */
1328 
1329    CURSOR gl_interface_cursor(l_payroll_control_id	IN	NUMBER) IS
1330    SELECT psl.summary_line_id,
1331           psl.source_code,
1332           psl.effective_date,
1333           psl.accounting_date, -- added 2 cols for 3108109
1334           psl.exchange_Rate_type,
1335           psl.set_of_books_id,
1336           psl.gl_code_combination_id,
1337           psl.summary_amount,
1338           psl.dr_cr_flag,
1339           psl.attribute1,
1340           psl.attribute2,
1341           psl.attribute3,
1342           psl.attribute4,
1343           psl.attribute5,
1344           psl.attribute6,
1345           psl.attribute7,
1346           psl.attribute8,
1347           psl.attribute9,
1348           psl.attribute10,
1349           psl.attribute11,
1350           psl.attribute12,
1351           psl.attribute13,
1352           psl.attribute14,
1353           psl.attribute15,
1354           psl.attribute16,
1355           psl.attribute17,
1356           psl.attribute18,
1357           psl.attribute19,
1358           psl.attribute20,
1359           psl.attribute21,
1360           psl.attribute22,
1361           psl.attribute23,
1362           psl.attribute24,
1363           psl.attribute25,
1364           psl.attribute26,
1365           psl.attribute27,
1366           psl.attribute28,
1367           psl.attribute29,
1368           psl.attribute30
1369    FROM  psp_summary_lines  psl
1370    WHERE psl.status_code = 'N'
1371    AND   psl.gl_code_combination_id IS NOT NULL
1372    AND   psl.payroll_control_id = l_payroll_control_id;
1373 
1374    gl_batch_rec			gl_batch_cursor%ROWTYPE;
1375    gl_payroll_control_rec	gl_payroll_control_cursor%ROWTYPE; -- Introduced for Bug 3112053
1376    gl_interface_rec		gl_interface_cursor%ROWTYPE;
1377 
1378    l_user_je_source_name	VARCHAR2(25);
1379    l_user_je_category_name	VARCHAR2(25);
1380    l_period_name		VARCHAR2(35);
1381    l_period_end_date            DATE; ------ Bug 2663344: reverted commenting
1382    l_encumbrance_type_id	NUMBER(15);
1383    l_entered_dr			NUMBER;
1384    l_entered_cr			NUMBER;
1385    l_group_id			NUMBER;
1386    l_int_run_id			NUMBER;
1387    l_reference1			VARCHAR2(100);
1388    l_reference4			VARCHAR2(100);
1389    l_return_status		VARCHAR2(10);
1390    req_id			NUMBER(15);
1391    call_status			BOOLEAN;
1392    rphase			VARCHAR2(30);
1393    rstatus			VARCHAR2(30);
1394    dphase			VARCHAR2(30);
1395    dstatus			VARCHAR2(30);
1396    message			VARCHAR2(240);
1397    p_errbuf			VARCHAR2(32767);
1398    p_retcode			VARCHAR2(32767);
1399    return_back			EXCEPTION;
1400    l_rec_count			NUMBER := 0;
1401    l_error			VARCHAR2(100);
1402    l_product			VARCHAR2(3);
1403    l_value			VARCHAR2(200);
1404    l_table			VARCHAR2(100);
1405  BEGIN
1406 
1407    -- get the source name
1408         fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' entered transfer_to_gl_interface');
1409    get_gl_je_sources(l_user_je_source_name,
1410                      l_return_status);
1411    IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1412      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1413    END IF;
1414 
1415    -- get the category name
1416    get_gl_je_categories(l_user_je_category_name,
1417                         l_return_status);
1418    IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1419      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1420    END IF;
1421 
1422    /* Changed the following code for Bug 3112053 , so as to submit one journal import for a batch having same
1423       source_code,source_type,time_period_id,batch_name combination */
1424 		 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' open g_batch_cursor');
1425 
1426    OPEN gl_batch_cursor;
1427    LOOP
1428 
1429      FETCH gl_batch_cursor INTO gl_batch_rec;
1430 
1431      IF gl_batch_cursor%NOTFOUND THEN
1432         CLOSE gl_batch_cursor;
1433         EXIT;
1434      END IF;
1435 
1436      -- get the group_id
1437 
1438      SELECT 	gl_interface_control_s.nextval
1439      INTO 	l_group_id
1440      FROM 	dual;
1441 
1442      BEGIN
1443        --- uncommented the end date for 2663344
1444        SELECT substr(period_name,1,35),end_date
1445        INTO l_period_name, l_period_end_date
1446        FROM per_time_periods
1447        WHERE time_period_id = gl_batch_rec.time_period_id;
1448      EXCEPTION
1449        WHEN NO_DATA_FOUND THEN
1450          l_value := 'Time Period Id = '||to_char(gl_batch_rec.time_period_id);
1451          l_table := 'PER_TIME_PERIODS';
1452          fnd_message.set_name('PSP','PSP_TR_VALUE_NOT_FOUND');
1453          fnd_message.set_token('VALUE',l_value);
1454          fnd_message.set_token('TABLE',l_table);
1455          fnd_msg_pub.add;
1456          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1457      END;
1458 
1459    If gl_batch_rec.phase = 'Summarize_GL_Lines' then --- added for 2444657
1460      l_reference1 := gl_batch_rec.source_type||':'||gl_batch_rec.payroll_source_code||':'||l_period_name||':'||gl_batch_rec.batch_name;
1461 
1462        l_reference4 := 'LD ACTUALS DISTRIBUTION';
1463 
1464                 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' open gl_payroll_control_cursor');
1465 
1466      OPEN gl_payroll_control_cursor(gl_batch_rec.source_type, gl_batch_rec.payroll_source_code,
1467 				    gl_batch_rec.time_period_id, gl_batch_rec.batch_name, gl_batch_rec.phase);
1468 
1469 	l_rec_count := 0;
1470 
1471 	LOOP
1472 		FETCH gl_payroll_control_cursor INTO gl_payroll_control_rec;
1473 		EXIT WHEN gl_payroll_control_cursor%NOTFOUND;
1474 		-- update psp_summary_lines with group_id
1475 
1476                   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' update psp_summary_lines with group_id : '||l_group_id);
1477 
1478 		UPDATE psp_summary_lines
1479      		SET group_id = l_group_id
1480      		WHERE status_code = 'N'
1481      		AND   gl_code_combination_id IS NOT NULL
1482      		AND   payroll_control_id = gl_payroll_control_rec.payroll_control_id;
1483 
1484 					 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' open gl_interface_cursor');
1485 
1486       		OPEN gl_interface_cursor(gl_payroll_control_rec.payroll_control_id);
1487      		LOOP
1488 		       FETCH gl_interface_cursor INTO gl_interface_rec;
1489 		       IF gl_interface_cursor%NOTFOUND THEN
1490 		          CLOSE gl_interface_cursor;
1491          		  EXIT;
1492 		       END IF;
1493 
1494        		       l_rec_count := l_rec_count + 1;
1495 
1496        			IF gl_interface_rec.dr_cr_flag = 'D' THEN
1497 		           l_entered_dr := gl_interface_rec.summary_amount;
1498                  	   l_entered_cr := NULL;
1499        			ELSIF gl_interface_rec.dr_cr_flag = 'C' THEN
1500          		   l_entered_dr := NULL;
1501          		   l_entered_cr := gl_interface_rec.summary_amount;
1502        			END IF;
1503 
1504       -- dbms_output.put_line('Inserting into gl interface .............');
1505 
1506       -- Changed The parameter g_currency_code to gl_batch_rec.currency_code, Introduced exchange_rate_type
1507       -- and conversion_date for Bug fix 2916848
1508 
1509 					  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' calling insert_into_gl_interface GL_INTERFACE_REC.SUMMARY_LINE_ID: '||GL_INTERFACE_REC.SUMMARY_LINE_ID);
1510 
1511        			insert_into_gl_interface(
1512 		    	  P_SET_OF_BOOKS_ID, GL_INTERFACE_REC.EFFECTIVE_DATE,
1513 			  gl_payroll_control_rec.CURRENCY_CODE,
1514                 	  L_USER_JE_CATEGORY_NAME,L_USER_JE_SOURCE_NAME,L_ENCUMBRANCE_TYPE_ID,
1515 		          GL_INTERFACE_REC.GL_CODE_COMBINATION_ID,L_ENTERED_DR,L_ENTERED_CR,
1516                           L_GROUP_ID,L_REFERENCE1,L_REFERENCE1,L_REFERENCE4,
1517                           GL_INTERFACE_REC.SUMMARY_LINE_ID,L_REFERENCE4,
1518                 	  GL_INTERFACE_REC.ATTRIBUTE1,GL_INTERFACE_REC.ATTRIBUTE2,
1519                 	  GL_INTERFACE_REC.ATTRIBUTE3,GL_INTERFACE_REC.ATTRIBUTE4,
1520                 	  GL_INTERFACE_REC.ATTRIBUTE5,GL_INTERFACE_REC.ATTRIBUTE6,
1521 		          GL_INTERFACE_REC.ATTRIBUTE7,GL_INTERFACE_REC.ATTRIBUTE8,
1522                  	  GL_INTERFACE_REC.ATTRIBUTE9,GL_INTERFACE_REC.ATTRIBUTE10,
1523                 	  GL_INTERFACE_REC.ATTRIBUTE11,GL_INTERFACE_REC.ATTRIBUTE12,
1524                 	  GL_INTERFACE_REC.ATTRIBUTE13,GL_INTERFACE_REC.ATTRIBUTE14,
1525                 	  GL_INTERFACE_REC.ATTRIBUTE15,GL_INTERFACE_REC.ATTRIBUTE16,
1526 		    	  GL_INTERFACE_REC.ATTRIBUTE17,GL_INTERFACE_REC.ATTRIBUTE18,
1527                 	  GL_INTERFACE_REC.ATTRIBUTE19,GL_INTERFACE_REC.ATTRIBUTE20,
1528                 	  GL_INTERFACE_REC.ATTRIBUTE21,GL_INTERFACE_REC.ATTRIBUTE22,
1529                 	  GL_INTERFACE_REC.ATTRIBUTE23,GL_INTERFACE_REC.ATTRIBUTE24,
1530                 	  GL_INTERFACE_REC.ATTRIBUTE25,GL_INTERFACE_REC.ATTRIBUTE26,
1531 		    	  GL_INTERFACE_REC.ATTRIBUTE27,GL_INTERFACE_REC.ATTRIBUTE28,
1532                 	  GL_INTERFACE_REC.ATTRIBUTE29,GL_INTERFACE_REC.ATTRIBUTE30,
1533 			  GL_INTERFACE_REC.EXCHANGE_RATE_TYPE,  -- modified for 3108109
1534                           GL_INTERFACE_REC.ACCOUNTING_DATE,
1535 			-- Introduced for Bug 2916848
1536                 	  L_RETURN_STATUS);
1537 
1538        			IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1539          		--dbms_output.put_line('Faaaaaaiiiiiiilllllllleeeeeeeeeddddddd......');
1540          	   	   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1541 	        	END IF;
1542 
1543      		END LOOP; -- End of gl_interface_cursor
1544 
1545 	END LOOP; -- End of gl_payroll_control_cursor
1546 	CLOSE gl_payroll_control_cursor;
1547 
1548 	else    --- 2444657 ...phase = Submitted_JI_Request
1549                  select group_id
1550                  into l_group_id
1551                  from psp_summary_lines
1552                  where payroll_control_id in
1553                      (SELECT payroll_control_id
1554                        FROM   psp_payroll_controls
1555                       WHERE  source_type = gl_batch_rec.source_type
1556                         AND    payroll_source_code = gl_batch_rec.payroll_source_code
1557                         AND    time_period_id = gl_batch_rec.time_period_id
1558                         AND    nvl(batch_name,'N') = nvl(nvl(gl_batch_rec.batch_name,batch_name),'N')
1559                         AND    (dist_dr_amount IS NOT NULL OR dist_cr_amount IS NOT NULL)
1560                         AND    status_code = 'I'
1561                         AND    business_group_id = p_business_group_id
1562                         AND    set_of_books_id = p_set_of_books_id
1563                         AND    run_id = g_run_id
1564                         AND    phase =  'Submitted_JI_Request')
1565                    and gl_code_combination_id is not null
1566                    and rownum = 1;
1567     end if;  --- 2444657
1568 
1569     IF l_rec_count > 0  and gl_batch_rec.phase = 'Summarize_GL_Lines' THEN   ---added phase for 2444657
1570 
1571      	-- Call the gather table statistics here....
1572   /*   	BEGIN
1573 
1574        		FND_STATS.Gather_Table_Stats(ownname => 'GL',
1575 				    	     tabname => 'GL_INTERFACE');
1576         	 commented for , 2476829
1577 			  		     percent => 10,
1578 				    	     tmode   => 'TEMPORARY');
1579 
1580      	EXCEPTION
1581 
1582        	WHEN others THEN
1583 	          NULL;
1584      	END;
1585 	*/
1586 
1587     	 -- insert into gl_interface_control
1588 
1589      	SELECT 	GL_JOURNAL_IMPORT_S.NEXTVAL
1590      	INTO 	l_int_run_id
1591      	FROM 	dual;
1592             fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || '  insert into gl_interface_control');
1593      	INSERT into gl_interface_control(je_source_name,status,interface_run_id,
1594         				 group_id,set_of_books_id)
1595        		VALUES	(l_user_je_source_name, 'S',l_int_run_id,
1596                   	 l_group_id,p_set_of_books_id);
1597 
1598 	  --dbms_output.put_line('Calling GL .............');
1599 			  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || '  submitting request for journal import');
1600 
1601      	req_id := fnd_request.submit_request(
1602 	    			'SQLGL',
1603          			'GLLEZL',
1604          			'',
1605          			'',
1606          			FALSE,
1607            			to_char(l_int_run_id),
1608 --     	       			to_char(l_sob_id), -- Changed for MO
1609             			to_char(p_set_of_books_id),
1610            			'N',
1611           			'',
1612           			'', -- added for bug fix 2365769
1613             		      	g_enable_enc_summ_gl,   --      changed from '' for bug 2259310
1614           			'W'    -- removed extra 'N' for bug 2365769	Changed 'N' tp 'W' for bug fix 2908859
1615            			);
1616 
1617      	IF req_id = 0 THEN
1618 
1619        		fnd_message.set_name('PSP','PSP_TR_GL_IMP_FAILED');
1620        		fnd_msg_pub.add;
1621        		RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1622 
1623      	ELSE
1624 
1625 		-- Added the update for Rollback purposes.
1626        		UPDATE 	psp_payroll_controls
1627        		SET 	phase = 'Submitted_JI_Request'
1628 --	Introduced selective payroll control filter for bug fix 3157895
1629 		WHERE  source_type = NVL(gl_batch_rec.source_type, source_type)
1630 		AND    payroll_source_code = NVL(gl_batch_rec.payroll_source_code, payroll_source_code)
1631 		AND    time_period_id = NVL(gl_batch_rec.time_period_id, time_period_id)
1632 		AND    NVL(batch_name,'N') = NVL(NVL(gl_batch_rec.batch_name, batch_name), 'N')
1633 		AND    status_code = 'I'
1634 		AND    business_group_id = p_business_group_id
1635 		AND    set_of_books_id = p_set_of_books_id
1636 		AND    run_id = g_run_id;
1637 		commit;
1638 
1639 			   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Committed after updating psp_payroll_controls SET 	phase = Submitted_JI_Request count: '||sql%rowcount);
1640 
1641             gl_batch_rec.phase := 'Submitted_JI_Request';   ---2444657
1642        		call_status := fnd_concurrent.wait_for_request(req_id, 20, 0,
1643                 		rphase, rstatus, dphase, dstatus, message);
1644 
1645        		IF call_status = FALSE THEN
1646 
1647          	  fnd_message.set_name('PSP','PSP_TR_GL_IMP_FAILED');
1648          	  fnd_msg_pub.add;
1649          	  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1650 
1651        		END IF;
1652 
1653      	END IF; -- End for check req_id = 0
1654        END IF; --- 2444657
1655 
1656 	/* Performing Gl tie Back for a set of control_ids having the same source_type,
1657 	  payroll_source_code, time_period_id, batch_name combination */
1658 
1659 
1660 		OPEN gl_payroll_control_cursor(gl_batch_rec.source_type,
1661 		     gl_batch_rec.payroll_source_code, gl_batch_rec.time_period_id,
1662 		     gl_batch_rec.batch_name, gl_batch_rec.phase);
1663 		LOOP
1664 			FETCH gl_payroll_control_cursor INTO gl_payroll_control_rec;
1665 			EXIT WHEN gl_payroll_control_cursor%NOTFOUND;
1666 
1667 
1668     	-- mark the successfully transferred records as 'A' in psp_summary_lines and psp_distribution_lines
1669     	-- and transfer the successful records to the history table
1670   	-- 1874696:changed l_period_end_date to NULL in tie back call
1671   	-- Bug 22663344 reverted null value to l_period_end_date
1672 				   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Calling gl_tie_back');
1673 
1674      			gl_tie_back(gl_payroll_control_rec.payroll_control_id,
1675 		    	    	gl_batch_rec.source_type,
1676                     	    	l_period_end_date,
1677 		            	l_group_id,
1678 		    	    	p_business_group_id,
1679 		      	    	p_set_of_books_id,
1680 		    	    	'N',
1681 		    	    	l_return_status);
1682 
1683      		/* Bug 1617846 LD Recovery LOV not showing up fialed S and T */
1684      		/* introduced ELSE clause */
1685 
1686      			IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1687 				CLOSE gl_payroll_control_cursor;
1688         			RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1689 		     	END IF;
1690 
1691 		END LOOP;
1692 		CLOSE gl_payroll_control_cursor;
1693 
1694      		COMMIT;
1695 
1696    END LOOP; -- End of gl_batch_csr
1697 
1698    /* End of code changes for Bug 3112053 */
1699 
1700    p_return_status := fnd_api.g_ret_sts_success;
1701 
1702 EXCEPTION
1703    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1704      --dbms_output.put_line('Gone to one level top ..................');
1705      g_error_api_path := 'TRANSFER_TO_GL_INTERFACE:'||g_error_api_path;
1706      p_return_status := fnd_api.g_ret_sts_unexp_error;
1707 
1708    WHEN RETURN_BACK THEN
1709      p_return_status := fnd_api.g_ret_sts_success;
1710 
1711    WHEN OTHERS THEN
1712 				  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
1713 
1714      g_error_api_path := 'TRANSFER_TO_GL_INTERFACE:'||g_error_api_path;
1715      fnd_msg_pub.add_exc_msg('PSP_SUM_TRANS','TRANSFER_TO_GL_INTERFACE');
1716      p_return_status := fnd_api.g_ret_sts_unexp_error;
1717  END;
1718 
1719 ---------------------- GET_GL_JE_SOURCES --------------------------------------------------
1720  PROCEDURE get_gl_je_sources(P_USER_JE_SOURCE_NAME  OUT NOCOPY  VARCHAR2,
1721                              P_RETURN_STATUS        OUT NOCOPY  VARCHAR2) IS
1722    l_error		VARCHAR2(100);
1723    l_product	VARCHAR2(3);
1724  BEGIN
1725    SELECT user_je_source_name
1726    INTO   p_user_je_source_name
1727    FROM   gl_je_sources
1728    WHERE  je_source_name = 'OLD';
1729  EXCEPTION
1730   WHEN NO_DATA_FOUND THEN
1731    l_error := 'JE SOURCES = OLD';
1732    l_product := 'GL';
1733    fnd_message.set_name('PSP','PSP_TR_NOT_SET_UP');
1734    fnd_message.set_token('ERROR',l_error);
1735    fnd_message.set_token('PRODUCT',l_product);
1736    fnd_msg_pub.add;
1737    p_return_status := fnd_api.g_ret_sts_unexp_error;
1738   WHEN OTHERS THEN
1739 			 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
1740 
1741     g_error_api_path := 'GL_JE_SOURCES:'||g_error_api_path;
1742     fnd_msg_pub.add_exc_msg('PSP_SUM_TRANS','GL_JE_SOURCES');
1743     p_return_status := fnd_api.g_ret_sts_unexp_error;
1744  END;
1745 
1746 ---------------------- GET_GL_CATEGORIES --------------------------------------------------
1747  PROCEDURE get_gl_je_categories(P_USER_JE_CATEGORY_NAME  OUT NOCOPY  VARCHAR2,
1748                                 P_RETURN_STATUS          OUT NOCOPY  VARCHAR2) IS
1749    l_error		VARCHAR2(100);
1750    l_product	VARCHAR2(3);
1751  BEGIN
1752    SELECT user_je_category_name
1753    INTO   p_user_je_category_name
1754    FROM   gl_je_categories
1755    WHERE  je_category_name = 'OLD';
1756  EXCEPTION
1757   WHEN NO_DATA_FOUND THEN
1758    l_error := 'JE CATEGORY = OLD';
1759    l_product := 'GL';
1760    fnd_message.set_name('PSP','PSP_TR_NOT_SET_UP');
1761    fnd_message.set_token('ERROR',l_error);
1762    fnd_message.set_token('PRODUCT',l_product);
1763    fnd_msg_pub.add;
1764    p_return_status := fnd_api.g_ret_sts_unexp_error;
1765   WHEN OTHERS THEN
1766 			  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
1767 
1768     g_error_api_path := 'GL_JE_CATEGORY_NAME:'||g_error_api_path;
1769     fnd_msg_pub.add_exc_msg('PSP_SUM_TRANS','GL_JE_CATEGORY_NAME');
1770     p_return_status := fnd_api.g_ret_sts_unexp_error;
1771  END;
1772 
1773 ---------------------- GET_ENCUM_TYPE_ID -----------------------------------------------
1774  PROCEDURE get_encum_type_id(P_ENCUMBRANCE_TYPE_ID  OUT NOCOPY  VARCHAR2,
1775                              P_RETURN_STATUS        OUT NOCOPY  VARCHAR2) IS
1776    l_error		VARCHAR2(100);
1777    l_product	VARCHAR2(3);
1778  BEGIN
1779    SELECT encumbrance_type_id
1780    INTO   p_encumbrance_type_id
1781    FROM   gl_encumbrance_types
1782    WHERE  encumbrance_type = 'OLD'
1783    AND    enabled_flag = 'Y';
1784 
1785  EXCEPTION
1786   WHEN NO_DATA_FOUND THEN
1787    l_error := 'ENCUMBRANCE TYPE = OLD';
1788    l_product := 'GL';
1789    fnd_message.set_name('PSP','PSP_TR_NOT_SET_UP');
1790    fnd_message.set_token('ERROR',l_error);
1791    fnd_message.set_token('PRODUCT',l_product);
1792    fnd_msg_pub.add;
1793    p_return_status := fnd_api.g_ret_sts_unexp_error;
1794   WHEN OTHERS THEN
1795 			 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
1796 
1797     g_error_api_path := 'ENCUMBRANCE_TYPE_ID:'||g_error_api_path;
1798     fnd_msg_pub.add_exc_msg('PSP_SUM_TRANS','ENCUMBRANCE_TYPE_ID');
1799     p_return_status := fnd_api.g_ret_sts_unexp_error;
1800  END;
1801 
1802 
1803 -------------------- GL TIE BACK -----------------------------------------------------
1804  PROCEDURE gl_tie_back(p_payroll_control_id IN	NUMBER,
1805                        p_source_type        IN  VARCHAR2,
1806                        p_period_end_date    IN  DATE,
1807                        p_group_id		  IN	NUMBER,
1808 		       p_business_group_id  IN NUMBER,
1809 		       p_set_of_books_id    IN NUMBER,
1810 		       p_mode               IN VARCHAR2,	--Introduced as part of Bug fix #1776606
1811                        p_return_status	 OUT NOCOPY	VARCHAR2) IS
1812    CURSOR gl_tie_back_success_cur IS
1813    SELECT summary_line_id,
1814           dr_cr_flag,summary_amount
1815    FROM   psp_summary_lines
1816    WHERE  group_id = p_group_id
1817      AND  payroll_control_id = p_payroll_control_id;
1818 
1819    CURSOR gl_tie_back_reject_cur IS
1820    SELECT status,
1821           reference6
1822    FROM   gl_interface
1823    WHERE  group_id = p_group_id
1824      AND  set_of_books_id = p_set_of_books_id
1825      AND  user_je_source_name = 'OLD'
1826      AND reference6 in(select summary_line_id FROM psp_summary_lines   -- Bug 7376898
1827                               WHERE GROUP_ID = p_group_id
1828                               AND payroll_control_id = p_payroll_control_id);
1829 
1830 
1831    CURSOR assign_susp_ac_cur(P_SUMMARY_LINE_ID	IN	NUMBER) IS
1832    SELECT pdl.rowid,
1833 	  pdl.distribution_line_id line_id,
1834           pdl.distribution_date,
1835           pdl.suspense_org_account_id,
1836           pdl.reversal_entry_flag,
1837           pdl.effective_date	-- Bug 7040943 Added
1838    FROM   psp_distribution_lines pdl
1839    WHERE  pdl.summary_line_id = p_summary_line_id
1840    UNION
1841    SELECT ppgd.rowid,
1842 	  ppgd.pre_gen_dist_line_id line_id,
1843           ppgd.distribution_date,
1844           ppgd.suspense_org_account_id,
1845           ppgd.reversal_entry_flag,
1846           ppgd.effective_date    -- Bug 7040943 Added
1847    FROM   psp_pre_gen_dist_lines ppgd
1848    WHERE  ppgd.summary_line_id = p_summary_line_id;
1849 
1850 -- Get the Organization details ...
1851 
1852    CURSOR get_susp_org_cur(P_ORG_ID	IN	VARCHAR2) IS
1853    SELECT hou.organization_id, hou.name, poa.gl_code_combination_id
1854      FROM hr_all_organization_units hou, psp_organization_accounts poa
1855     WHERE hou.organization_id = poa.organization_id
1856       AND poa.business_group_id = p_business_group_id
1857       AND poa.set_of_books_id = p_set_of_books_id
1858       AND poa.organization_account_id = p_org_id;
1859 
1860    CURSOR get_org_id_cur(P_LINE_ID	IN	NUMBER) IS
1861    SELECT hou.organization_id, hou.name
1862    FROM   hr_all_organization_units hou,
1863   	  per_assignments_f paf,
1864   	  psp_payroll_lines ppl,
1865   	  psp_payroll_sub_lines ppsl,
1866           psp_distribution_lines pdl
1867    WHERE  pdl.payroll_sub_line_id = ppsl.payroll_sub_line_id
1868    AND    ppsl.payroll_line_id = ppl.payroll_line_id
1869    AND    pdl.distribution_line_id = p_line_id
1870    AND    ppl.assignment_id = paf.assignment_id
1871    AND    pdl.distribution_date BETWEEN paf.effective_start_date AND paf.effective_end_date
1872    AND    paf.organization_id = hou.organization_id
1873    AND    pdl.distribution_date between
1874 		hou.date_from and nvl(hou.date_to,pdl.distribution_date)
1875    UNION
1876    SELECT hou.organization_id, hou.name
1877    FROM   hr_all_organization_units hou,
1878           per_assignments_f paf,
1879           psp_pre_gen_dist_lines ppgd
1880    WHERE  ppgd.pre_gen_dist_line_id = p_line_id
1881    AND    ppgd.assignment_id = paf.assignment_id
1882    AND    ppgd.distribution_date BETWEEN paf.effective_start_date AND paf.effective_end_date
1883    AND	  paf.organization_id = hou.organization_id;
1884 
1885   l_orig_org_name	hr_all_organization_units_tl.name%TYPE;	-- Bug 2447912: Modified declaration
1886   l_orig_org_id		number;
1887 
1888 -- End of Get org id cursor  Ravindra
1889 
1890    CURSOR org_susp_ac_cur(P_ORGANIZATION_ID	IN	NUMBER,
1891                           P_DISTRIBUTION_DATE	IN	DATE) IS
1892    SELECT poa.organization_account_id,
1893           poa.gl_code_combination_id,
1894           poa.project_id,
1895           poa.award_id,
1896           poa.task_id,
1897           poa.expenditure_organization_id,
1898           poa.expenditure_type
1899    FROM   psp_organization_accounts poa
1900    WHERE  poa.organization_id = p_organization_id
1901    AND    poa.account_type_code = 'S'
1902    AND    poa.business_group_id = p_business_group_id
1903    AND    poa.set_of_books_id = p_set_of_books_id
1904    AND    p_distribution_date BETWEEN poa.start_date_active AND
1905                                       nvl(poa.end_date_active,p_distribution_date);
1906 
1907 
1908 -- CURSOR global_susp_ac_cur(P_DISTRIBUTION_DATE	IN	DATE) IS
1909    CURSOR global_susp_ac_cur(P_ORGANIZATION_ACCOUNT_ID  IN	NUMBER) IS  --BUG 2056877
1910    SELECT poa.organization_account_id,
1911           poa.gl_code_combination_id,
1912           poa.project_id,
1913           poa.award_id,
1914           poa.task_id,
1915           poa.expenditure_organization_id,
1916           poa.expenditure_type
1917    FROM   psp_organization_accounts poa
1918    WHERE
1919 /* poa.account_type_code = 'G'
1920    AND    poa.business_group_id = p_business_group_id
1921    AND    poa.set_of_books_id = p_set_of_books_id
1922    AND    p_distribution_date BETWEEN poa.start_date_active AND
1923                                       nvl(poa.end_date_active,p_distribution_date);  Bug 2056877.*/
1924           organization_account_id = p_organization_account_id; --Added for bug 2056877.
1925 
1926    l_organization_name		hr_all_organization_units_tl.name%TYPE;	-- Bug 2447912: Modified declaration
1927    l_organization_id		NUMBER(15);
1928    l_rowid				ROWID;
1929    l_assignment_id		NUMBER(15);
1930    l_distribution_date		DATE;
1931    l_suspense_org_account_id  NUMBER(9);
1932    l_reversal_entry_flag	VARCHAR2(1);
1933    l_lines_glccid			NUMBER(15);
1934    --
1935    l_organization_account_id	NUMBER(9);
1936    l_susp_glccid			NUMBER(15);
1937    l_project_id			NUMBER(15);
1938    l_award_id			NUMBER(15);
1939    l_task_id                    NUMBER(15);
1940    --
1941    l_status				VARCHAR2(50);
1942    l_reference6			VARCHAR2(100);
1943    --
1944    l_cnt_gl_interface		NUMBER;
1945    l_summary_line_id		NUMBER(10);
1946    l_gl_project_flag		VARCHAR2(1);
1947    l_suspense_ac_failed		VARCHAR2(1) := 'N';
1948    l_reversal_ac_failed		VARCHAR2(1) := 'N';
1949    l_suspense_ac_not_found	VARCHAR2(1) := 'N';
1950    l_susp_ac_found		VARCHAR2(10) := 'TRUE';
1951    l_summary_amount		NUMBER;
1952    l_dr_summary_amount		NUMBER := 0;
1953    l_cr_summary_amount		NUMBER := 0;
1954    l_dr_cr_flag			VARCHAR2(1);
1955    l_effective_date		DATE;
1956    x_susp_failed_org_name	hr_all_organization_units_tl.name%TYPE;	-- Bug 2447912: Modified declaration
1957    x_susp_failed_status		VARCHAR2(50);
1958    x_susp_failed_date		DATE;
1959    x_lines_glccid			NUMBER(15);
1960    x_susp_nf_org_name		hr_all_organization_units_tl.name%TYPE;	-- Bug 2447912: Modified declaration
1961    x_susp_nf_date			DATE;
1962    l_return_status		VARCHAR2(10);
1963    x_line_id			NUMBER;
1964    l_no_run			number;
1965    l_return_value               VARCHAR2(30); --Added for bug 2056877.
1966    no_profile_exists            EXCEPTION;    --Added for bug 2056877.
1967    no_val_date_matches          EXCEPTION;    --Added for bug 2056877.
1968    no_global_acct_exists        EXCEPTION;    --Added for bug 2056877.
1969    l_user_je_source_name             varchar2(25); ---added for 2445196
1970    l_susp_exception               varchar2(50); -- 2479579
1971    l_expenditure_type           varchar2(100);  -- introduced vars for 5080403
1972    l_exp_org_id                 number;
1973    l_new_expenditure_type       varchar2(100);
1974    l_new_glccid                 number;
1975    l_acct_type                  varchar2(1);
1976    l_auto_pop_status            varchar2(100);
1977    l_auto_status                varchar2(100);
1978    l_person_id                  number;
1979    l_element_type_id            number;
1980    l_assignment_number          varchar2(100);
1981    l_element_type               varchar2(200);
1982    l_person_name                varchar2(300);
1983    l_account                    varchar2(1000);
1984    l_auto_org_name              hr_all_organization_units_tl.name%TYPE; -- Bug 2447912: Modified declaration
1985 
1986    l_pay_action_type           psp_payroll_lines.payroll_action_type%TYPE; -- Bug 7040943
1987 
1988  cursor get_element_type is
1989    select ppl.element_type_id,
1990           ppl.assignment_id,
1991           ppl.person_id
1992     from  psp_payroll_lines ppl,
1993           psp_payroll_sub_lines ppsl,
1994           psp_distribution_lines pdl
1995     where pdl.distribution_line_id = x_line_id
1996       and pdl.payroll_sub_line_id = ppsl.payroll_sub_line_id
1997       and ppsl.payroll_line_id = ppl.payroll_line_id
1998    union all
1999    select ppg.element_type_id,
2000           ppg.assignment_id,
2001           ppg.person_id
2002      from psp_pre_gen_dist_lines ppg
2003     where pre_gen_dist_line_id = x_line_id;
2004 
2005  cursor get_asg_details is
2006    select ppf.full_name,
2007           paf.assignment_number,
2008           pet.element_name,
2009           hou.name
2010      from per_all_people_f ppf,
2011           per_all_assignments_f paf,
2012           pay_element_types_f pet,
2013           hr_all_organization_units hou
2014     where ppf.person_id = l_person_id
2015       and l_distribution_date between ppf.effective_start_date and ppf.effective_end_date
2016       and paf.assignment_id = l_assignment_id
2017       and l_distribution_date between paf.effective_start_date and paf.effective_end_date
2018       and pet.element_type_id = l_element_type_id
2019       and l_distribution_date between pet.effective_start_date and pet.effective_end_date
2020       and hou.organization_id = paf.organization_id;
2021 
2022  BEGIN
2023 
2024 	   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Entered gl_tie_back');
2025 
2026    select count(*)
2027      into l_no_run
2028      from gl_interface
2029     where status = 'NEW'
2030       and group_id = p_group_id
2031       and user_je_source_name = 'OLD'
2032      AND reference6 in(select summary_line_id FROM psp_summary_lines   -- Bug 7376898
2033                         WHERE GROUP_ID = p_group_id
2034                         AND payroll_control_id = p_payroll_control_id);
2035 
2036 			fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' l_no_run: '||l_no_run);
2037 
2038    if l_no_run > 0 then
2039 
2040      delete from gl_interface
2041       where group_id = p_group_id
2042 	and user_je_source_name = 'OLD'
2043         AND reference6 in(select summary_line_id FROM psp_summary_lines   -- Bug 7376898
2044                            WHERE GROUP_ID = p_group_id
2045                            AND payroll_control_id = p_payroll_control_id);
2046 
2047 		   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Deleted from gl_interface count: '||sql%rowcount);
2048 
2049 
2050      delete from psp_summary_lines
2051       where payroll_control_id = p_payroll_control_id
2052 	and group_id = p_group_id;
2053 			 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Deleted from psp_summary_lines count: '||sql%rowcount);
2054 
2055      --- bug 4328598 -- fix begin
2056 
2057         -- get the source name
2058 			 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Calling get_gl_je_source');
2059 
2060    get_gl_je_sources(l_user_je_source_name,
2061                      l_return_status);
2062    IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2063      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2064    END IF;
2065 
2066      delete gl_interface_Control
2067      where group_id = p_group_id
2068        and je_source_name = l_user_je_source_name;
2069 
2070 			fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Deleted from gl_interface_control count: '||sql%rowcount);
2071 
2072      update psp_payroll_controls
2073         set phase = null
2074       where payroll_control_id = p_payroll_control_id;
2075 
2076      commit;
2077 		  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' updated psp_payroll_controls count: '||sql%rowcount);
2078 
2079      --- bug 4328598--- fix end
2080       		fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Commit');
2081 
2082 
2083 	fnd_message.set_name('PSP','PSP_JI_DID_NOT_RUN');
2084 	fnd_message.set_token('PAYROLL_CONTROL_ID',p_payroll_control_id);
2085         fnd_message.set_token('GROUP_ID',p_group_id);
2086         fnd_msg_pub.add;
2087         -- uncommented following statement for 2444657
2088         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2089 
2090    end if;
2091 
2092    SELECT count(*)
2093    INTO l_cnt_gl_interface
2094    FROM gl_interface
2095    WHERE group_id = p_group_id
2096      AND user_je_source_name = 'OLD'
2097      AND set_of_books_id = p_set_of_books_id
2098      AND reference6 in(select summary_line_id FROM psp_summary_lines   -- Bug 7376898
2099                        WHERE GROUP_ID = p_group_id
2100                        AND payroll_control_id = p_payroll_control_id);
2101 
2102 
2103 --- moved this statement from transfer_to_gl procedure to the beginning of this
2104 -- procedure, becuase it not being set when user defined exceptions are occuring
2105 --- like invalid suspense FATAL error. Since there is a single commit for TIE-BACK
2106 -- this will work fine... for Bug fix 2444657
2107 	UPDATE 	psp_payroll_controls
2108 	SET    	phase = 'GL_Tie_Back'
2109 	WHERE	payroll_control_id = p_payroll_control_id;
2110 
2111    IF l_cnt_gl_interface > 0 THEN
2112      --
2113      OPEN gl_tie_back_reject_cur;
2114      LOOP
2115        FETCH gl_tie_back_reject_cur INTO l_status,l_reference6;
2116        IF gl_tie_back_reject_cur%NOTFOUND THEN
2117          CLOSE gl_tie_back_reject_cur;
2118          EXIT;
2119        END IF;
2120        -- update summary_lines with the reject status code
2121        UPDATE psp_summary_lines
2122        SET interface_status = l_status, status_code = 'R'
2123        WHERE summary_line_id = to_number(l_reference6);
2124 
2125        OPEN assign_susp_ac_cur(to_number(l_reference6));
2126        LOOP
2127 
2128          FETCH assign_susp_ac_cur INTO l_rowid, x_line_id, l_distribution_date,
2129 		l_suspense_org_account_id, l_reversal_entry_flag,
2130 		l_effective_date;  --Bug 7040943;
2131 
2132          IF assign_susp_ac_cur%NOTFOUND THEN
2133            CLOSE assign_susp_ac_cur;
2134            EXIT;
2135          END IF;
2136 
2137 	-- Bug 9307730
2138 	IF p_source_type IN('O', 'N') THEN
2139          -- Bug 7376898
2140          SELECT payroll_action_type
2141            INTO l_pay_action_type
2142            FROM psp_payroll_lines
2143           WHERE payroll_control_id = p_payroll_control_id
2144             and payroll_line_id = (select payroll_line_id from psp_payroll_sub_lines
2145                                     where payroll_sub_line_id = (select payroll_sub_line_id
2146                                                                    from psp_distribution_lines
2147                                                                   where distribution_line_id = x_line_id));
2148 	END IF;
2149 
2150 -- Get the name and gl_ccid of the suspense account used
2151 	if l_suspense_org_account_id is not null  then
2152 	 OPEN get_susp_org_cur(l_suspense_org_account_id);
2153 	 FETCH get_susp_org_cur into l_organization_id, l_organization_name,
2154 				     l_lines_glccid;
2155 	 CLOSE get_susp_org_cur;
2156 	end if;
2157 
2158 
2159       IF  (l_status <> 'P' AND substr(l_status,1,1) <> 'W')  THEN -- moved this condition from elseif:Bug2205224
2160 
2161          IF l_suspense_org_account_id IS NOT NULL THEN
2162 
2163            x_susp_failed_org_name := l_organization_name;
2164            x_susp_failed_status   := l_status;
2165            x_susp_failed_date     := l_distribution_date;
2166            l_suspense_ac_failed := 'Y';
2167 
2168          -- if the reversing a/c failed,update the status of the whole batch and display the error
2169          ELSIF l_reversal_entry_flag = 'Y' THEN
2170 
2171           ---x_lines_glccid := l_lines_glccid;  commented this line and introduced
2172                                         ---      if-endif statment below for 2663344
2173           l_reversal_ac_failed := 'Y';
2174 
2175           if x_lines_glccid is null then
2176             select gl_code_combination_id into x_lines_glccid
2177             from psp_summary_lines where summary_line_id = to_number(l_reference6);
2178           end if;
2179 
2180          ELSE
2181 
2182            l_susp_ac_found := 'TRUE';
2183 	   OPEN get_org_id_cur(x_line_id);
2184 	   FETCH get_org_id_cur into l_orig_org_id, l_orig_org_name;
2185 	   CLOSE get_org_id_cur;
2186 
2187 	   --Bug 7040943 Starts
2188 	   IF l_pay_action_type = 'L' THEN
2189 	    	l_distribution_date := l_effective_date;
2190 	   END IF;
2191            --Bug 7040943 End
2192 
2193            OPEN org_susp_ac_cur(l_orig_org_id,l_distribution_date);
2194            FETCH org_susp_ac_cur INTO l_organization_account_id,l_susp_glccid,l_project_id,l_award_id,l_task_id,
2195                                      l_exp_org_id, l_expenditure_type;
2196            IF org_susp_ac_cur%NOTFOUND  THEN
2197            /* Following code is added for bug 2056877 ,Added validation for generic suspense account */
2198               l_return_value := psp_general.find_global_suspense(l_distribution_date,
2199 							  p_business_group_id,
2200                                                           p_set_of_books_id,
2201                                                           l_organization_account_id );
2202       	  /* --------------------------------------------------------------------
2203       	   Valid return values are
2204       	   PROFILE_VAL_DATE_MATCHES       Profile and Value and Date matching 'G'
2205       	   NO_PROFILE_EXISTS              No Profile
2206        	   NO_VAL_DATE_MATCHES            Profile and Either Value/date do not
2207             		                  match with 'G'
2208    	   NO_GLOBAL_ACCT_EXISTS          No 'G' exists
2209      	    ---------------------------------------------------------------------- */
2210               IF  l_return_value = 'PROFILE_VAL_DATE_MATCHES' THEN
2211                 --  OPEN global_susp_ac_cur(l_distribution_date);
2212                     OPEN global_susp_ac_cur(l_organization_account_id); --Bug 2056877
2213                     FETCH global_susp_ac_cur INTO l_organization_account_id,l_susp_glccid,l_project_id,l_award_id,l_task_id,
2214                                                   l_exp_org_id, l_expenditure_type;
2215 	              IF global_susp_ac_cur%NOTFOUND THEN
2216         		 /*      l_susp_ac_found := 'FALSE';
2217 		                 l_suspense_ac_not_found := 'Y';
2218                 		 x_susp_nf_org_name := l_orig_org_name;
2219 		                 x_susp_nf_date     := l_distribution_date;  Commented for bug 2056877 */
2220 		          --- commented for 2479579      RAISE no_global_acct_exists; --Added for bug 2056877
2221                            -- added following lines for 2479579
2222                               l_susp_ac_found := 'NO_G_AC';
2223 		              l_suspense_ac_not_found := 'Y';
2224 		      END IF;
2225               	      CLOSE global_susp_ac_cur;
2226 	      ELSIF l_return_value = 'NO_GLOBAL_ACCT_EXISTS' THEN
2227     		     --- RAISE no_global_acct_exists; commented this line added following 2 lines for 2479579
2228 	             l_suspense_ac_not_found := 'Y';
2229                      l_susp_ac_found := 'NO_G_AC';
2230               ELSIF l_return_value = 'NO_VAL_DATE_MATCHES' THEN
2231          	    --- RAISE no_val_date_matches;  commented this line added following 2 lines for 2479579
2232 	            l_suspense_ac_not_found := 'Y';
2233                     l_susp_ac_found := 'NO_DT_MCH';
2234               ELSIF l_return_value = 'NO_PROFILE_EXISTS' THEN
2235          	    --- RAISE no_profile_exists;  commented this line added following 2 lines for 2479579
2236 	             l_suspense_ac_not_found := 'Y';
2237                      l_susp_ac_found := 'NO_PROFL';
2238               END IF; -- Bug 2056877.
2239         END IF;
2240        CLOSE org_susp_ac_cur;
2241   -- introduced for 5080403
2242        if g_suspense_autopop = 'Y' and l_organization_account_id is not null then
2243             if l_susp_glccid is null then
2244                 l_acct_type:='E';
2245             else
2246                 l_acct_type:='N';
2247             end if;
2248             open get_element_type;
2249             fetch get_element_type into l_element_type_id, l_assignment_id, l_person_id;
2250             close get_element_type;
2251               psp_autopop.main( p_acct_type                   => l_acct_type,
2252                                 p_person_id                   => l_person_id,
2253                                 p_assignment_id               => l_assignment_id,
2254                                 p_element_type_id             => l_element_type_id,
2255                                 p_project_id                  => l_project_id,
2256                                 p_expenditure_organization_id => l_exp_org_id,
2257                                 p_task_id                     => l_task_id,
2258                                 p_award_id                    => l_award_id,
2259                                 p_expenditure_type            => l_expenditure_type,
2260                                 p_gl_code_combination_id      => l_susp_glccid,
2261                                 p_payroll_date                => l_distribution_date,
2262                                 p_set_of_books_id             => p_set_of_books_id,
2263                                 p_business_group_id           => p_business_group_id,
2264                                 ret_expenditure_type          => l_new_expenditure_type,
2265                                 ret_gl_code_combination_id    => l_new_glccid,
2266                                 retcode                       => l_auto_pop_status);
2267            IF (l_auto_pop_status = FND_API.G_RET_STS_UNEXP_ERROR) OR
2268              (l_auto_pop_status = FND_API.G_RET_STS_ERROR) THEN
2269              IF l_auto_pop_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2270                if l_acct_type ='N'  then
2271                     l_auto_status := 'AUTO_POP_NA_ERROR';
2272                else
2273                     l_auto_status :='AUTO_POP_EXP_ERROR';
2274                end if;
2275              elsif l_auto_pop_status = FND_API.G_RET_STS_ERROR THEN
2276                l_auto_status := 'AUTO_POP_NO_VALUE';
2277              end if;
2278              open get_asg_details;
2279              fetch get_asg_details into l_person_name, l_assignment_number, l_element_type, l_auto_org_name;
2280              close get_asg_details;
2281              psp_enc_crt_xml.p_set_of_books_id := p_set_of_books_id;
2282              psp_enc_crt_xml.p_business_group_id := p_business_group_id;
2283              if l_acct_type = 'N' then
2284                  l_account :=
2285                      psp_enc_crt_xml.cf_charging_instformula(l_susp_glccid,
2286                                                              null,
2287                                                              null,
2288                                                              null,
2289                                                              null,
2290                                                              null);
2291               else
2292                  l_account :=
2293                      psp_enc_crt_xml.cf_charging_instformula(null,
2294                                                              l_project_id,
2295                                                              l_task_id,
2296                                                              l_award_id,
2297                                                              l_expenditure_type,
2298                                                              l_exp_org_id);
2299               end if;
2300                    fnd_message.set_name('PSP','PSP_SUSPENSE_AUTOPOP_FAIL');
2301                    fnd_message.set_token('ORG_NAME',l_auto_org_name);
2302                    fnd_message.set_token('EMPLOYEE_NAME',l_person_name);
2303                    fnd_message.set_token('ASG_NUM',l_assignment_number);
2304                    fnd_message.set_token('CHARGING_ACCOUNT',l_account);
2305                    fnd_message.set_token('AUTOPOP_ERROR',l_auto_status);
2306                    fnd_message.set_token('EFF_DATE',l_distribution_date);
2307                    fnd_msg_pub.add;
2308          else
2309            if l_acct_type = 'E' then
2310               l_expenditure_type := l_new_expenditure_type;
2311            else
2312               l_susp_glccid := l_new_glccid;
2313            end if;
2314          end if;
2315         end if;
2316 
2317            IF l_susp_ac_found = 'TRUE' THEN
2318 
2319              IF l_susp_glccid IS NOT NULL THEN
2320                l_gl_project_flag := 'G';
2321                -- l_effective_date := p_period_end_date;   ---  uncommented for 22663344  --Bug 7040943
2322 
2323              ELSE
2324                l_gl_project_flag := 'P';
2325 
2326            /* Bug 1874696: deleted the procedure call for psp_general.poeta_effective_date
2327            */
2328                 l_effective_date := l_distribution_date;   --- added for Bug 2663344
2329 
2330              END IF;
2331 
2332              -- assign the organization suspense account and gl status
2333              --dbms_output.put_line('Updating distribution_lines ....NULL..');
2334 
2335             IF p_source_type = 'O' OR p_source_type = 'N' THEN
2336 
2337 	     /* Added the following for Bug 3065866 */
2338 
2339 	     UPDATE PSP_DISTRIBUTION_LINES
2340 	     SET    pre_distribution_run_flag = gl_project_flag
2341       	     WHERE  rowid = l_rowid;
2342 
2343 	     /* End of code changes for Bug 3065866 */
2344 
2345              UPDATE psp_distribution_lines
2346               SET suspense_org_account_id = l_organization_account_id,
2347                   suspense_reason_code = 'ST:' || l_status,
2348                   gl_project_flag = l_gl_project_flag,
2349                   status_code = 'N',
2350                    effective_date = l_effective_date,  --- for  Bug 2663344
2351                suspense_auto_glccid = l_new_glccid,    --- added suspense_auto for 5080403
2352                   suspense_auto_exp_type = l_new_expenditure_type
2353               WHERE rowid = l_rowid;
2354 			  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' 1 updated psp_distribution_lines count: '||sql%rowcount);
2355 					fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' l_effective_date: '||l_effective_date);
2356 
2357             ELSIF p_source_type = 'P' THEN
2358                UPDATE psp_pre_gen_dist_lines
2359                  SET suspense_org_account_id = l_organization_account_id,
2360                      suspense_reason_code = 'ST:' || l_status,
2361                      status_code = 'N',
2362                       effective_date = l_effective_date,  --- for  Bug 2663344
2363                suspense_auto_glccid = l_new_glccid,    --- added suspense_auto for 5080403
2364                   suspense_auto_exp_type = l_new_expenditure_type
2365                  WHERE rowid = l_rowid;
2366 				  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' 1 updated psp_pre_gen_dist_lines count: '||sql%rowcount);
2367 					fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' l_effective_date: '||l_effective_date);
2368 
2369             END IF;
2370            ELSE  -- introduced for 2479579
2371               l_susp_exception := l_susp_ac_found;
2372            END IF;
2373          END IF;
2374      END IF;  -- Bug 2205224, status not in (P,W)
2375 
2376 
2377        END LOOP;
2378      END LOOP;
2379       ---2445196: cleanup gl interface b'cos rejection is available in rejected summary lines
2380          get_gl_je_sources(l_user_je_source_name,
2381                      l_return_status);
2382         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2383           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2384         END IF;
2385 
2386       delete gl_interface
2387       where group_id = p_group_id
2388        and set_of_books_id = p_set_of_books_id
2389        AND user_je_source_name = l_user_je_source_name
2390        and reference6 in(select summary_line_id FROM psp_summary_lines   -- Bug 7376898
2391                          WHERE GROUP_ID = p_group_id
2392                          AND payroll_control_id = p_payroll_control_id);
2393 			   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' deleted from gl_interface count: '||sql%rowcount);
2394 
2395      IF l_reversal_ac_failed = 'Y' THEN
2396        fnd_message.set_name('PSP','PSP_GL_REVERSE_AC_REJECT');
2397        fnd_message.set_token('GLCCID',x_lines_glccid);
2398        fnd_msg_pub.add;
2399 
2400      /* Added the following for the Bug 3065866 */
2401 
2402 	IF p_source_type = 'O' OR p_source_type = 'N' THEN
2403 		UPDATE 	psp_distribution_lines
2404 		SET 	suspense_org_account_id = NULL,
2405 			suspense_reason_code = NULL,
2406                         gl_project_flag = pre_distribution_run_flag,
2407                         effective_date = decode(pre_distribution_run_flag,'G',
2408 						p_period_end_date,distribution_date)
2409 		WHERE	suspense_reason_code like 'ST:%'
2410 		AND	summary_line_id
2411 			IN (SELECT 	summary_line_id
2412 			    FROM 	psp_summary_lines
2413 			    WHERE 	payroll_control_id = p_payroll_control_id);
2414 
2415 				fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' 2 updated psp_distribution_lines count: '||sql%rowcount);
2416 				fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' l_effective_date: '||l_effective_date);
2417 
2418 	ELSIF p_source_type = 'P' THEN
2419 
2420 		UPDATE	psp_pre_gen_dist_lines
2421 		SET	suspense_org_account_id = NULL,
2422 			suspense_reason_code = NULL,
2423                        effective_date = decode(NVL(gl_code_combination_id,-999),gl_code_combination_id,
2424 					p_period_end_date,distribution_date)
2425 		WHERE	suspense_reason_code like 'ST:%'
2426 		AND	summary_line_id
2427 			IN (SELECT 	summary_line_id
2428 			    FROM	psp_summary_lines
2429 			    WHERE	payroll_control_id = p_payroll_control_id);
2430 			   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' 2 updated psp_pre_gen_dist_lines count: '||sql%rowcount);
2431 			fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' l_effective_date: '||l_effective_date);
2432 
2433 	END IF;
2434 
2435 	/* End of code for Bug 3065856 */
2436 
2437 
2438 	/* Introduced the following check as part of Bug fix #1776606 */
2439        if p_mode = 'N' then
2440           /* introduced mark batch end Bug: 1929317 */
2441 				 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' calling mark_batch_end');
2442 
2443           mark_batch_end(p_source_type,
2444                    g_source_code,
2445                    g_time_period_id,
2446                    g_batch_name,
2447                    p_business_group_id,
2448                    p_set_of_books_id,
2449                    l_return_status);
2450           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2451        end if;
2452      END IF;
2453 
2454      IF l_suspense_ac_failed = 'Y' or
2455         nvl(l_auto_status,'X') in ('AUTO_POP_NA_ERROR', 'AUTO_POP_EXP_ERROR', 'AUTO_POP_NO_VALUE') then
2456                    --- above check for autopop error 5080403
2457 			   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' l_suspense_ac_failed = Y ');
2458 
2459        if nvl(l_suspense_ac_failed,'N') = 'Y' then
2460        fnd_message.set_name('PSP','PSP_TR_GL_SUSP_AC_REJECT');
2461        fnd_message.set_token('ORG_NAME',x_susp_failed_org_name);
2462        fnd_message.set_token('PAYROLL_DATE',x_susp_failed_date);
2463        fnd_message.set_token('ERROR_MSG',x_susp_failed_status);
2464        fnd_msg_pub.add;
2465        end if;
2466 
2467 	/* Added the following for Bug 3065866 */
2468 
2469 	IF p_source_type = 'O' OR p_source_type = 'N' THEN
2470 
2471 
2472 		UPDATE 	psp_distribution_lines
2473 	 	SET	suspense_org_account_id = NULL,
2474 			suspense_reason_code = NULL,
2475 			gl_project_flag = pre_distribution_run_flag,
2476                         effective_date = decode(pre_distribution_run_flag,'G',
2477                                                 p_period_end_date,distribution_date)
2478 		WHERE	suspense_reason_code like 'ST:%'
2479 		AND	summary_line_id
2480 			IN (SELECT	summary_line_id
2481 			    FROM	psp_summary_lines
2482 			    WHERE	payroll_control_id = p_payroll_control_id );
2483 
2484                 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' 3 updated psp_distribution_lines count: '||sql%rowcount);
2485 			--fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' l_effective_date: '||l_effective_date);
2486 
2487 	ELSIF p_source_type = 'P' THEN
2488 		UPDATE	psp_pre_gen_dist_lines
2489 		SET	suspense_org_account_id = NULL,
2490 			suspense_reason_code = NULL,
2491 			effective_date = decode(NVL(gl_code_combination_id,-999),gl_code_combination_id,
2492                                         p_period_end_date,distribution_date)
2493 		WHERE	suspense_reason_code like 'ST:%'
2494 		AND	summary_line_id
2495 			IN ( SELECT	summary_line_id
2496 			     FROM 	psp_summary_lines
2497 			     WHERE	payroll_control_id = p_payroll_control_id );
2498 
2499 			 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' 3 updated psp_pre_gen_dist_lines count: '||sql%rowcount);
2500 				--fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' l_effective_date: '||l_effective_date);
2501 
2502 	END IF;
2503 
2504 	/* End of code for Bug 3065866 */
2505 
2506 	/* Introduced the following check as part of Bug fix #1776606 */
2507        if p_mode = 'N' then
2508          /* introduced mark batch end Bug: 1929317 */
2509 				 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' calling mark_batch_end');
2510 
2511          mark_batch_end(p_source_type,
2512                    g_source_code,
2513                    g_time_period_id,
2514                    g_batch_name,
2515                    p_business_group_id,
2516                    p_set_of_books_id,
2517                    l_return_status);
2518           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2519        end if;
2520      END IF;
2521 
2522      IF l_suspense_ac_not_found = 'Y' THEN
2523 			 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' l_suspense_ac_not_found = Y ');
2524 
2525        /* commented following code for 2479579
2526        fnd_message.set_name('PSP','PSP_LD_SUSPENSE_AC_NOT_EXIST');
2527        fnd_message.set_token('ORG_NAME',x_susp_nf_org_name);
2528        fnd_message.set_token('PAYROLL_DATE',x_susp_nf_date);
2529        fnd_msg_pub.add; */
2530 
2531 	/* Introduced the following check as part of Bug fix #1776606 */
2532        if p_mode = 'N' then
2533          /* introduced mark batch end Bug: 1929317 */
2534 			 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || 'calling mark_batch_end');
2535 
2536          mark_batch_end(p_source_type,
2537                    g_source_code,
2538                    g_time_period_id,
2539                    g_batch_name,
2540                    p_business_group_id,
2541                    p_set_of_books_id,
2542                    l_return_status);
2543           --- RAISE FND_API.G_EXC_UNEXPECTED_ERROR; commented for  2479579
2544           -- introduced following if stmnt  for  2479579
2545           if l_susp_exception = 'NO_G_AC' then
2546                      RAISE no_global_acct_exists;
2547           elsif  l_susp_exception = 'NO_DT_MCH' then
2548                      RAISE no_val_date_matches;
2549           elsif l_susp_exception =  'NO_PROFL' then
2550                      RAISE no_profile_exists;
2551           end if;
2552 
2553        end if;
2554      END IF;
2555 
2556 
2557    ELSIF l_cnt_gl_interface = 0  and l_no_run = 0 THEN
2558      --
2559 			 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Open gl_tie_back_success_cur');
2560 
2561      OPEN gl_tie_back_success_cur;
2562      LOOP
2563        FETCH gl_tie_back_success_cur INTO l_summary_line_id,
2564        l_dr_cr_flag,l_summary_amount;
2565        IF gl_tie_back_success_cur%NOTFOUND THEN
2566          CLOSE gl_tie_back_success_cur;
2567          EXIT;
2568        END IF;
2569        -- update records in psp_summary_lines as 'A'
2570        UPDATE psp_summary_lines
2571        SET status_code = 'A'
2572        WHERE summary_line_id = l_summary_line_id;
2573 
2574 			  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' updated psp_summary_lines set status_code = A count: '||sql%rowcount);
2575 
2576        IF l_dr_cr_flag = 'D' THEN
2577          l_dr_summary_amount := l_dr_summary_amount + l_summary_amount;
2578        ELSIF l_dr_cr_flag = 'C' THEN
2579          l_cr_summary_amount := l_cr_summary_amount + l_summary_amount;
2580        END IF;
2581 
2582        IF p_source_type = 'O' OR p_source_type = 'N' THEN
2583 
2584          UPDATE psp_distribution_lines
2585          SET status_code = 'A' WHERE summary_line_id = l_summary_line_id;
2586 					 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' updated psp_distribution_lines set status_code = A count: '||sql%rowcount);
2587 
2588          -- move the transferred records to psp_distribution_lines_history
2589          INSERT INTO psp_distribution_lines_history
2590          (distribution_line_id,payroll_sub_line_id,distribution_date,
2591           effective_date,distribution_amount,status_code,suspense_reason_code,
2592           effort_report_id,version_num,schedule_line_id,
2593           summary_line_id,default_org_account_id,suspense_org_account_id,
2594           element_account_id,org_schedule_id,
2595           user_defined_field,default_reason_code,reversal_entry_flag,gl_project_flag,
2596 	  auto_gl_code_combination_id, business_group_id, set_of_books_id,
2597 	attribute_category,	attribute1,	attribute2,	attribute3,
2598 	attribute4,		attribute5,	attribute6,	attribute7,
2599 	attribute8,		attribute9,	attribute10,
2600          cap_excess_glccid, cap_excess_award_id, cap_excess_task_id,
2601         cap_excess_project_id,    cap_excess_exp_type, cap_excess_exp_org_id,
2602         funding_source_code, annual_salary_cap, cap_excess_dist_line_id,
2603         suspense_auto_exp_type, suspense_auto_glccid, adj_account_flag)   --- added 2 cols for 508040
2604          SELECT distribution_line_id,payroll_sub_line_id,distribution_date,
2605           effective_date,distribution_amount,status_code,suspense_reason_code,
2606           effort_report_id,version_num,schedule_line_id,
2607           summary_line_id,default_org_account_id,suspense_org_account_id,
2608           element_account_id,org_schedule_id,
2609           user_defined_field,default_reason_code,reversal_entry_flag,gl_project_flag,
2610 	  auto_gl_code_combination_id, business_group_id, set_of_books_id,
2611 	attribute_category,	attribute1,	attribute2,	attribute3,
2612 	attribute4,		attribute5,	attribute6,	attribute7,
2613 	attribute8,		attribute9,	attribute10,
2614          cap_excess_glccid, cap_excess_award_id, cap_excess_task_id,
2615         cap_excess_project_id,    cap_excess_exp_type, cap_excess_exp_org_id,
2616         funding_source_code, annual_salary_cap, cap_excess_dist_line_id,
2617           suspense_auto_exp_type, suspense_auto_glccid, adj_account_flag
2618          FROM psp_distribution_lines
2619          WHERE status_code = 'A'
2620          AND  summary_line_id = l_summary_line_id;
2621 
2622 				fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' inserted into psp_distribution_lines_history count: '||sql%rowcount);
2623 
2624          DELETE FROM psp_distribution_lines
2625          WHERE status_code = 'A'
2626          AND summary_line_id = l_summary_line_id;
2627 
2628 					 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || '  deleted from psp_distribution_lines count: '||sql%rowcount);
2629 
2630        ELSIF p_source_type = 'P' THEN
2631 
2632          UPDATE psp_pre_gen_dist_lines
2633          SET status_code = 'A' WHERE summary_line_id = l_summary_line_id;
2634 				 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' 4 updated psp_pre_gen_distribution_lines set status_code = A count: '||sql%rowcount);
2635 
2636 
2637          -- move the transferred records to psp_pre_gen_dist_lines_history
2638 	-- Introduced DFF columns for bug fix 2908859
2639          INSERT INTO psp_pre_gen_dist_lines_history
2640          (pre_gen_dist_line_id,distribution_interface_id,person_id,assignment_id,
2641           element_type_id,distribution_date,effective_date,distribution_amount,
2642           dr_cr_flag,payroll_control_id,source_type,source_code,time_period_id,
2643           batch_name,status_code,set_of_books_id,
2644           gl_code_combination_id,project_id,expenditure_organization_id,
2645           expenditure_type,task_id,award_id,suspense_reason_code,
2646           effort_report_id,version_num,summary_line_id,suspense_org_account_id,
2647           user_defined_field,reversal_entry_flag, business_group_id,
2648 	attribute_category,	attribute1,	attribute2,	attribute3,
2649 	attribute4,		attribute5,	attribute6,	attribute7,
2650 	attribute8,		attribute9,	attribute10,
2651          suspense_auto_exp_type, suspense_auto_glccid)    -- added 2 cols for 5080403
2652          SELECT pre_gen_dist_line_id,distribution_interface_id,person_id,assignment_id,
2653           element_type_id,distribution_date,effective_date,distribution_amount,
2654           dr_cr_flag,payroll_control_id,source_type,source_code,time_period_id,
2655           batch_name,status_code,set_of_books_id,
2656           gl_code_combination_id,project_id,expenditure_organization_id,
2657           expenditure_type,task_id,award_id,suspense_reason_code,
2658           effort_report_id,version_num,summary_line_id,suspense_org_account_id,
2659           user_defined_field,reversal_entry_flag, business_group_id,
2660 	attribute_category,	attribute1,	attribute2,	attribute3,
2661 	attribute4,		attribute5,	attribute6,	attribute7,
2662         attribute8,             attribute9,     attribute10,
2663          suspense_auto_exp_type, suspense_auto_glccid
2664          FROM psp_pre_gen_dist_lines
2665          WHERE status_code = 'A'
2666          AND summary_line_id = l_summary_line_id;
2667 
2668 			 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' inserted into psp_pre_gen_dist_lines_history count: '||sql%rowcount);
2669 
2670          DELETE FROM psp_pre_gen_dist_lines
2671          WHERE status_code = 'A'
2672          AND summary_line_id = l_summary_line_id;
2673 			fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || '  deleted from psp_pre_gen_dist_lines count: '||sql%rowcount);
2674 
2675        END IF;
2676 
2677      END LOOP;
2678 
2679        UPDATE psp_payroll_controls
2680        SET gl_dr_amount = nvl(gl_dr_amount,0) + l_dr_summary_amount,
2681            gl_cr_amount = nvl(gl_cr_amount,0) + l_cr_summary_amount
2682        WHERE payroll_control_id = p_payroll_control_id;
2683 
2684         fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || '  updated psp_payroll_controls count: '||sql%rowcount);
2685 
2686 
2687    END IF;
2688    --
2689    p_return_status := fnd_api.g_ret_sts_success;
2690  EXCEPTION
2691 
2692    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2693      --dbms_output.put_line('Gone to one level top ..................');
2694      g_error_api_path := 'GL_TIE_BACK:'||g_error_api_path;
2695      p_return_status := fnd_api.g_ret_sts_unexp_error;
2696 
2697      /* Added Exceptions for bug 2056877 */
2698      WHEN NO_PROFILE_EXISTS THEN
2699       g_error_api_path := SUBSTR('GL_TIE_BACK:'||g_error_api_path,1,230);
2700       fnd_message.set_name('PSP','PSP_NO_PROFILE_EXISTS');
2701       fnd_msg_pub.add;
2702       p_return_status := fnd_api.g_ret_sts_unexp_error;
2703 
2704    WHEN NO_VAL_DATE_MATCHES THEN
2705       g_error_api_path := SUBSTR('GL_TIE_BACK:'||g_error_api_path,1,230);
2706       fnd_message.set_name('PSP','PSP_NO_VAL_DATE_MATCHES');
2707       fnd_message.set_token('ORG_NAME',l_orig_org_name);
2708       fnd_message.set_token('PAYROLL_DATE',l_distribution_date);
2709       fnd_msg_pub.add;
2710       p_return_status := fnd_api.g_ret_sts_unexp_error;
2711 
2712    WHEN NO_GLOBAL_ACCT_EXISTS THEN
2713       g_error_api_path := SUBSTR('GL_TIE_BACK:'||g_error_api_path,1,230);
2714       fnd_message.set_name('PSP','PSP_NO_GLOBAL_ACCT_EXISTS');
2715       fnd_message.set_token('ORG_NAME',l_orig_org_name);
2716       fnd_message.set_token('PAYROLL_DATE',l_distribution_date);
2717       fnd_msg_pub.add;
2718       p_return_status := fnd_api.g_ret_sts_unexp_error;  --End of Changes for Bug 2056877.
2719 
2720    WHEN OTHERS THEN
2721 			 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
2722 
2723       g_error_api_path := 'GL_TIE_BACK:'||g_error_api_path;
2724       fnd_msg_pub.add_exc_msg('PSP_SUM_TRANS','GL_TIE_BACK');
2725       p_return_status := fnd_api.g_ret_sts_unexp_error;
2726  END;
2727 
2728 
2729 ------------------ CREATE BALANCING TRANSACTIONS FOR GL ------------------------------
2730 
2731  PROCEDURE gl_balance_transaction(
2732 			P_SOURCE_TYPE 		IN	VARCHAR2,
2733 			P_PAYROLL_CONTROL_ID	IN	NUMBER,
2734 			P_BUSINESS_GROUP_ID	IN	NUMBER,
2735 			P_SET_OF_BOOKS_ID	IN	NUMBER,
2736 			P_PRECISION		IN	NUMBER,-- Introduced this parameter For Bug 2916848
2737                   P_RETURN_STATUS        OUT NOCOPY VARCHAR2) IS
2738 
2739    CURSOR dist_reversal_entry_cur(P_PAYROLL_CONTROL_ID IN NUMBER) IS
2740    SELECT pdl.payroll_sub_line_id,pdl.effective_date,
2741           round(sum(pdl.distribution_amount), p_precision) reversal_dist_amount,-- Changed rounding from 2 to p_precision
2742 -- For Bug 2916848
2743 	  pdl.business_group_id,
2744 	  pdl.set_of_books_id
2745    FROM   psp_distribution_lines  pdl,
2746           psp_payroll_sub_lines   ppsl,
2747           psp_payroll_lines       ppl,
2748           psp_payroll_controls    ppc
2749    WHERE  ppc.payroll_control_id = p_payroll_control_id
2750    AND    ppc.payroll_control_id = ppl.payroll_control_id
2751    AND    ppl.payroll_line_id = ppsl.payroll_line_id
2752    AND    ppsl.payroll_sub_line_id = pdl.payroll_sub_line_id
2753    AND    nvl(pdl.reversal_entry_flag,'N') = 'N'
2754    AND    pdl.gl_project_flag = 'G'
2755    AND    pdl.status_code = 'N'
2756    GROUP BY pdl.payroll_sub_line_id, pdl.effective_date, pdl.business_group_id, pdl.set_of_books_id;
2757 
2758    CURSOR pg_reversal_entry_cur(P_PAYROLL_CONTROL_ID IN NUMBER) IS
2759    SELECT ppgd.distribution_interface_id,
2760 	  ppgd.person_id,
2761 	  ppgd.assignment_id,
2762 	  ppgd.element_type_id,
2763 	  ppgd.dr_cr_flag,
2764 	  ppgd.distribution_date,
2765 	  ppgd.effective_date,
2766 	  ppgd.source_type,
2767 	  ppgd.source_code,
2768 	  ppgd.time_period_id,
2769 	  ppgd.batch_name,
2770 	  ppgd.set_of_books_id,
2771 	  ppgd.business_group_id,
2772 	  round(sum(ppgd.distribution_amount), p_precision) reversal_dist_amount -- For Bug 2916848 changed to p_precision
2773      FROM psp_pre_gen_dist_lines ppgd,
2774 	  psp_organization_accounts pos
2775     WHERE ppgd.payroll_control_id = p_payroll_control_id
2776       AND nvl(ppgd.reversal_entry_flag,'N') = 'N'
2777       AND ((ppgd.gl_code_combination_id IS NOT NULL and pos.project_id is null ) OR  -- Bug 2007521 Changed condn
2778             pos.gl_code_combination_id IS NOT NULL)
2779       AND ppgd.suspense_org_account_id = pos.organization_account_id(+)
2780       AND ppgd.status_code = 'N'
2781 --      AND ppgd.business_group_id = p_business_group_id
2782 --      AND ppgd.set_of_books_id = p_set_of_books_id
2783  GROUP BY ppgd.distribution_interface_id, ppgd.person_id,
2784 	  ppgd.assignment_id, ppgd.element_type_id,
2785 	  ppgd.dr_cr_flag, ppgd.distribution_date, ppgd.effective_date,
2786 	  ppgd.source_type, ppgd.source_code,
2787 	  ppgd.time_period_id, ppgd.batch_name, ppgd.set_of_books_id, ppgd.business_group_id;
2788 
2789    dist_reversal_entry_rec	dist_reversal_entry_cur%ROWTYPE;
2790    pg_reversal_entry_rec	pg_reversal_entry_cur%ROWTYPE;
2791    l_reversal_dist_amount	NUMBER;
2792    l_clrg_account_glccid	NUMBER(15);
2793    l_cr_amount			NUMBER;
2794    l_dr_amount			NUMBER;
2795 
2796    l_payroll_id                 NUMBER(9);   -- Added for bug 5592964
2797 
2798  BEGIN
2799 
2800      IF p_source_type = 'O' OR p_source_type = 'N' THEN
2801 
2802        DELETE FROM psp_distribution_lines
2803        WHERE reversal_entry_flag = 'Y'
2804        AND status_code = 'N'
2805        AND payroll_sub_line_id IN (
2806         select payroll_sub_line_id from psp_payroll_sub_lines where payroll_line_id IN (
2807         select payroll_line_id from psp_payroll_lines where payroll_control_id IN (
2808         select payroll_control_id from psp_payroll_controls where payroll_control_id=
2809          p_payroll_control_id)));
2810 
2811 
2812        -- recalculate and update the reversal sub-line amounts
2813        OPEN dist_reversal_entry_cur(p_payroll_control_id);
2814        LOOP
2815          FETCH dist_reversal_entry_cur INTO dist_reversal_entry_rec;
2816          IF dist_reversal_entry_cur%NOTFOUND THEN
2817            CLOSE dist_reversal_entry_cur;
2818            EXIT;
2819          END IF;
2820 
2821          -- insert the reversal entry record into distribution lines
2822          insert into psp_distribution_lines
2823            (distribution_line_id,payroll_sub_line_id,distribution_date,effective_date,
2824             distribution_amount,status_code,gl_project_flag,reversal_entry_flag,
2825 	    business_group_id, set_of_books_id)
2826          values
2827            (PSP_DISTRIBUTION_LINES_S.NEXTVAL,dist_reversal_entry_rec.payroll_sub_line_id,
2828             dist_reversal_entry_rec.effective_date,dist_reversal_entry_rec.effective_date,
2829             dist_reversal_entry_rec.reversal_dist_amount,'N','G','Y',
2830 	    dist_reversal_entry_rec.business_group_id, dist_reversal_entry_rec.set_of_books_id);
2831 
2832        END LOOP;
2833 
2834        select sum(decode(reversal_entry_flag, 'Y', distribution_amount, 0)) cr_amount,
2835 	      sum(decode(reversal_entry_flag, 'Y', 0, distribution_amount)) dr_amount
2836 	 INTO l_cr_amount, l_dr_amount
2837 	 FROM psp_distribution_lines  pdl,
2838 	      psp_payroll_sub_lines   ppsl,
2839 	      psp_payroll_lines       ppl,
2840 	      psp_payroll_controls    ppc
2841         WHERE ppc.payroll_control_id = p_payroll_control_id
2842 	  AND ppc.payroll_control_id = ppl.payroll_control_id
2843 	  AND ppl.payroll_line_id = ppsl.payroll_line_id
2844 	  AND ppsl.payroll_sub_line_id = pdl.payroll_sub_line_id
2845 	  AND pdl.gl_project_flag = 'G'
2846 	  AND pdl.status_code = 'N';
2847 
2848 	IF l_cr_amount <> l_dr_amount then
2849 	  fnd_message.set_name('PSP','PSP_GL_REV_AMT_NOT_EQUAL');
2850 	  fnd_msg_pub.add;
2851 	  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2852 	END IF;
2853 
2854      ELSIF p_source_type = 'P' THEN
2855        SELECT payroll_id INTO l_payroll_id
2856        FROM  psp_payroll_controls
2857        WHERE payroll_control_id = p_payroll_control_id;
2858 
2859        BEGIN
2860 	 SELECT reversing_gl_ccid
2861 	   INTO l_clrg_account_glccid
2862 	   FROM PSP_CLEARING_ACCOUNT
2863 	  WHERE set_of_books_id = p_set_of_books_id
2864 	    AND business_group_id = p_business_group_id -- Changed for MO.
2865 	    AND payroll_id = l_payroll_id;              -- Added for bug 5592964
2866 	EXCEPTION
2867 	WHEN NO_DATA_FOUND THEN
2868 	  fnd_message.set_name('PSP','PSP_TR_CLRG_AC_NOT_SET_UP');
2869 	  fnd_msg_pub.add;
2870 	 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2871        END;
2872 
2873 	delete from psp_pre_gen_dist_lines
2874 	 where reversal_entry_flag = 'Y'
2875 	   and status_code = 'N'
2876 	   and source_type = 'P'
2877 	   and payroll_control_id = p_payroll_control_id;
2878 
2879        -- recalculate and update the reversal amounts
2880        OPEN pg_reversal_entry_cur(p_payroll_control_id);
2881        LOOP
2882          FETCH pg_reversal_entry_cur INTO pg_reversal_entry_rec;
2883          IF pg_reversal_entry_cur%NOTFOUND THEN
2884            CLOSE pg_reversal_entry_cur;
2885            EXIT;
2886          END IF;
2887 
2888 -- Bug# 1140217
2889 --         UPDATE psp_pre_gen_dist_lines
2890 --         SET distribution_amount = pg_reversal_entry_rec.reversal_dist_amount
2891 --         WHERE distribution_interface_id = pg_reversal_entry_rec.distribution_interface_id
2892 --         AND   reversal_entry_flag = 'Y';
2893 
2894 
2895 	insert into psp_pre_gen_dist_lines
2896 		(pre_gen_dist_line_id, distribution_interface_id, person_id, assignment_id,
2897 		element_type_id, distribution_date, effective_date, distribution_amount,
2898 		dr_cr_flag, payroll_control_id, source_type, source_code, time_period_id,
2899 		batch_name, status_code, set_of_books_id, business_group_id, gl_code_combination_id,
2900 		reversal_entry_flag)
2901 	values (psp_distribution_lines_s.nextval, pg_reversal_entry_rec.distribution_interface_id,
2902 		pg_reversal_entry_rec.person_id, pg_reversal_entry_rec.assignment_id,
2903 		pg_reversal_entry_rec.element_type_id, pg_reversal_entry_rec.distribution_date,
2904 		pg_reversal_entry_rec.effective_date, pg_reversal_entry_rec.reversal_dist_amount,
2905 		pg_reversal_entry_rec.dr_cr_flag, p_payroll_control_id, pg_reversal_entry_rec.source_type,
2906 		pg_reversal_entry_rec.source_code, pg_reversal_entry_rec.time_period_id,
2907 		pg_reversal_entry_rec.batch_name, 'N', pg_reversal_entry_rec.set_of_books_id,
2908 		pg_reversal_entry_rec.business_group_id, l_clrg_account_glccid, 'Y');
2909 
2910        END LOOP;
2911 
2912         select sum(decode(reversal_entry_flag, 'Y', distribution_amount, 0)),
2913 	       sum(decode(reversal_entry_flag, 'Y', 0, distribution_amount))
2914 	  into l_cr_amount, l_dr_amount
2915 	  from psp_pre_gen_dist_lines ppgd, psp_organization_accounts pos
2916 	 where payroll_control_id = p_payroll_control_id
2917 	   and ppgd.status_code = 'N'
2918 	   and ppgd.suspense_org_account_id = pos.organization_account_id(+)
2919             AND ((ppgd.gl_code_combination_id IS NOT NULL and ppgd.suspense_org_account_id is null ) OR
2920             pos.gl_code_combination_id IS NOT NULL);
2921            /* Bug 2007521: replaced following condn with above condn
2922 	   and (ppgd.gl_code_combination_id is not null or (ppgd.suspense_org_account_id is not null
2923 	   						    and pos.gl_code_combination_id is not null)); */
2924 
2925 	IF l_cr_amount <> l_dr_amount then
2926 	   fnd_message.set_name('PSP','PSP_GL_UNBALANCED_BATCH');
2927 	   fnd_msg_pub.add;
2928 	   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2929 	END IF;
2930 
2931      END IF;
2932      --
2933      p_return_status := fnd_api.g_ret_sts_success;
2934  EXCEPTION
2935    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2936      g_error_api_path := 'GL_BALANCE_TRANSACTION:'||g_error_api_path;
2937      p_return_status := fnd_api.g_ret_sts_unexp_error;
2938 
2939    WHEN OTHERS THEN
2940 			  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
2941 
2942       g_error_api_path := 'GL_BALANCE_TRANSACTION:'||g_error_api_path;
2943       fnd_msg_pub.add_exc_msg('PSP_SUM_TRANS','GL_BALANCE_TRANSACTION');
2944       p_return_status := fnd_api.g_ret_sts_unexp_error;
2945  END;
2946 
2947 ------------------ INSERT INTO GL INTERFACE -----------------------------------------------
2948 
2949  PROCEDURE insert_into_gl_interface(
2950 			P_SET_OF_BOOKS_ID 		IN	NUMBER,
2951 			P_ACCOUNTING_DATE			IN	DATE,
2952 			P_CURRENCY_CODE			IN	VARCHAR2,
2953 			P_USER_JE_CATEGORY_NAME		IN	VARCHAR2,
2954 			P_USER_JE_SOURCE_NAME		IN	VARCHAR2,
2955 			P_ENCUMBRANCE_TYPE_ID		IN	NUMBER,
2956 			P_CODE_COMBINATION_ID		IN	NUMBER,
2957 			P_ENTERED_DR			IN	NUMBER,
2958 			P_ENTERED_CR			IN	NUMBER,
2959 			P_GROUP_ID				IN	NUMBER,
2960 			P_REFERENCE1			IN	VARCHAR2,
2961 			P_REFERENCE2			IN	VARCHAR2,
2962 			P_REFERENCE4			IN	VARCHAR2,
2963 			P_REFERENCE6			IN	VARCHAR2,
2964 			P_REFERENCE10			IN	VARCHAR2,
2965 			P_ATTRIBUTE1			IN	VARCHAR2,
2966 			P_ATTRIBUTE2			IN	VARCHAR2,
2967 			P_ATTRIBUTE3			IN	VARCHAR2,
2968 			P_ATTRIBUTE4			IN	VARCHAR2,
2969 			P_ATTRIBUTE5			IN	VARCHAR2,
2970 			P_ATTRIBUTE6			IN	VARCHAR2,
2971 			P_ATTRIBUTE7			IN	VARCHAR2,
2972 			P_ATTRIBUTE8			IN	VARCHAR2,
2973 			P_ATTRIBUTE9			IN	VARCHAR2,
2974 			P_ATTRIBUTE10			IN	VARCHAR2,
2975 			P_ATTRIBUTE11			IN	VARCHAR2,
2976 			P_ATTRIBUTE12			IN	VARCHAR2,
2977 			P_ATTRIBUTE13			IN	VARCHAR2,
2978 			P_ATTRIBUTE14			IN	VARCHAR2,
2979 			P_ATTRIBUTE15			IN	VARCHAR2,
2980 			P_ATTRIBUTE16			IN	VARCHAR2,
2981 			P_ATTRIBUTE17			IN	VARCHAR2,
2982 			P_ATTRIBUTE18			IN	VARCHAR2,
2983 			P_ATTRIBUTE19			IN	VARCHAR2,
2984 			P_ATTRIBUTE20			IN	VARCHAR2,
2985 			P_ATTRIBUTE21			IN	VARCHAR2,
2986 			P_ATTRIBUTE22			IN	VARCHAR2,
2987 			P_ATTRIBUTE23			IN	VARCHAR2,
2988 			P_ATTRIBUTE24			IN	VARCHAR2,
2989 			P_ATTRIBUTE25			IN	VARCHAR2,
2990 			P_ATTRIBUTE26			IN	VARCHAR2,
2991 			P_ATTRIBUTE27			IN	VARCHAR2,
2992 			P_ATTRIBUTE28			IN	VARCHAR2,
2993 			P_ATTRIBUTE29			IN	VARCHAR2,
2994 			P_ATTRIBUTE30			IN	VARCHAR2,
2995 			P_CURRENCY_CONVERSION_TYPE	IN	VARCHAR2, -- Introduced for bug fix 2916848
2996 			P_CURRENCY_CONVERSION_DATE	IN	DATE, -- Introduced for bug fix 2916848
2997 			P_RETURN_STATUS			OUT NOCOPY	VARCHAR2) IS
2998  BEGIN
2999 
3000    --dbms_output.put_line('sob id='||to_char(p_set_of_books_id));
3001   -- dbms_output.put_line('a date='||to_char(p_accounting_date));
3002   -- dbms_output.put_line('curren='||p_currency_code);
3003    --dbms_output.put_line('category='||p_user_je_category_name);
3004    --dbms_output.put_line('source='||p_user_je_source_name);
3005 
3006    INSERT INTO GL_INTERFACE(
3007 	STATUS,
3008 	SET_OF_BOOKS_ID,
3009 	ACCOUNTING_DATE,
3010 	CURRENCY_CODE,
3011 	DATE_CREATED,
3012 	CREATED_BY,
3013 	ACTUAL_FLAG,
3014 	USER_JE_CATEGORY_NAME,
3015 	USER_JE_SOURCE_NAME,
3016 	ENCUMBRANCE_TYPE_ID,
3017 	CODE_COMBINATION_ID,
3018 	ENTERED_DR,
3019 	ENTERED_CR,
3020 	GROUP_ID,
3021 	REFERENCE1,
3022 	REFERENCE2,
3023 	REFERENCE4,
3024 	REFERENCE6,
3025 	REFERENCE10,
3026 	ATTRIBUTE1,
3027 	ATTRIBUTE2,
3028 	ATTRIBUTE3,
3029 	ATTRIBUTE4,
3030 	ATTRIBUTE5,
3031 	ATTRIBUTE6,
3032 	ATTRIBUTE7,
3033 	ATTRIBUTE8,
3034 	ATTRIBUTE9,
3035 	ATTRIBUTE10,
3036 	ATTRIBUTE11,
3037 	ATTRIBUTE12,
3038 	ATTRIBUTE13,
3039 	ATTRIBUTE14,
3040 	ATTRIBUTE15,
3041 	ATTRIBUTE16,
3042 	ATTRIBUTE17,
3043 	ATTRIBUTE18,
3044 	ATTRIBUTE19,
3045 	ATTRIBUTE20,
3046 	REFERENCE21,
3047 	REFERENCE22,
3048 	REFERENCE23,
3049 	REFERENCE24,
3050 	REFERENCE25,
3051 	REFERENCE26,
3052 	REFERENCE27,
3053 	REFERENCE28,
3054 	REFERENCE29,
3055 	REFERENCE30,
3056 --      Introduced the following columns for bug fix 2916848
3057 	USER_CURRENCY_CONVERSION_TYPE,
3058 	CURRENCY_CONVERSION_DATE)
3059    VALUES(
3060 	'NEW',
3061 	P_SET_OF_BOOKS_ID,
3062 	P_ACCOUNTING_DATE,
3063 	P_CURRENCY_CODE,
3064 	SYSDATE,
3065 	FND_GLOBAL.USER_ID,
3066 	'A',
3067 	P_USER_JE_CATEGORY_NAME,
3068 	P_USER_JE_SOURCE_NAME,
3069 	P_ENCUMBRANCE_TYPE_ID,
3070 	P_CODE_COMBINATION_ID,
3071 	P_ENTERED_DR,
3072 	P_ENTERED_CR,
3073 	P_GROUP_ID,
3074 	P_REFERENCE1,
3075 	P_REFERENCE2,
3076 	P_REFERENCE4,
3077 	P_REFERENCE6,
3078 	P_REFERENCE10,
3079 	P_ATTRIBUTE1,
3080 	P_ATTRIBUTE2,
3081 	P_ATTRIBUTE3,
3082 	P_ATTRIBUTE4,
3083 	P_ATTRIBUTE5,
3084 	P_ATTRIBUTE6,
3085 	P_ATTRIBUTE7,
3086 	P_ATTRIBUTE8,
3087 	P_ATTRIBUTE9,
3088 	P_ATTRIBUTE10,
3089 	P_ATTRIBUTE11,
3090 	P_ATTRIBUTE12,
3091 	P_ATTRIBUTE13,
3092 	P_ATTRIBUTE14,
3093 	P_ATTRIBUTE15,
3094 	P_ATTRIBUTE16,
3095 	P_ATTRIBUTE17,
3096 	P_ATTRIBUTE18,
3097 	P_ATTRIBUTE19,
3098 	P_ATTRIBUTE20,
3099 	P_ATTRIBUTE21,
3100 	P_ATTRIBUTE22,
3101 	P_ATTRIBUTE23,
3102 	P_ATTRIBUTE24,
3103 	P_ATTRIBUTE25,
3104 	P_ATTRIBUTE26,
3105 	P_ATTRIBUTE27,
3106 	P_ATTRIBUTE28,
3107 	P_ATTRIBUTE29,
3108 	P_ATTRIBUTE30,
3109 	P_CURRENCY_CONVERSION_TYPE,-- Introduced the following code for Bug fix 2916848
3110 	DECODE(p_currency_conversion_type, NULL, NULL, P_CURRENCY_CONVERSION_DATE));-- Introduced the following code for Bug fix 2916848
3111     --dbms_output.put_line('DDDDDDDDDDDDDDDDDGL Interface Inserted ....................');
3112     p_return_status := fnd_api.g_ret_sts_success;
3113  EXCEPTION
3114    WHEN OTHERS THEN
3115 				fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
3116 
3117       --dbms_output.put_line('Error while inserting .........................');
3118       g_error_api_path := 'INSERT_INTO_GL_INTERFACE:'||g_error_api_path;
3119       fnd_msg_pub.add_exc_msg('PSP_SUM_TRANS','INSERT_INTO_GL_INTERFACE');
3120       p_return_status := fnd_api.g_ret_sts_unexp_error;
3121  END;
3122 
3123 -------------------- CREATE GMS SUM LINES -----------------------------------------------
3124  PROCEDURE create_gms_sum_lines(p_source_type     IN VARCHAR2,
3125                                 p_source_code     IN VARCHAR2,
3126                                 p_time_period_id  IN NUMBER,
3127                                 p_batch_name      IN VARCHAR2,
3128 				p_business_group_id IN NUMBER,
3129 				p_set_of_books_id   IN NUMBER,
3130                                 p_return_status  OUT NOCOPY VARCHAR2) IS
3131 
3132    CURSOR payroll_control_cur IS
3133    SELECT payroll_control_id,
3134           source_type,
3135           payroll_source_code,
3136           time_period_id,
3137           batch_name,
3138           business_group_id,
3139           set_of_books_id
3140    FROM   psp_payroll_controls
3141    WHERE  status_code = 'I'
3142    AND    source_type <> 'A'
3143    AND    run_id = g_run_id
3144    AND    (phase is null or
3145            phase in ('GMS_Tie_Back', 'GL_Tie_Back'));  ---  added for 2444657
3146 
3147    CURSOR gms_sum_lines_cursor(P_PAYROLL_CONTROL_ID  IN  NUMBER) IS
3148    SELECT ppl.person_id,
3149           ppl.assignment_id,
3150           nvl(pos.project_id,
3151               nvl(psl.project_id,
3152               nvl(pod.project_id,
3153               nvl(pea.project_id,
3154                   pdls.project_id)))) project_id,
3155           nvl(pos.expenditure_organization_id,
3156               nvl(psl.expenditure_organization_id,
3157               nvl(pod.expenditure_organization_id,
3158               nvl(pea.expenditure_organization_id,
3159                   pdls.expenditure_organization_id)))) expenditure_organization_id,
3160           nvl(pdl.suspense_auto_exp_type,   --- added for 5080403
3161           nvl(pos.expenditure_type,
3162               nvl(pdl.auto_expenditure_type,   --- added for 2663344
3163               nvl(psl.expenditure_type,
3164               nvl(pod.expenditure_type,
3165               nvl(pea.expenditure_type,
3166                   pdls.expenditure_type)))))) expenditure_type,
3167           nvl(pos.task_id,
3168               nvl(psl.task_id,
3169               nvl(pod.task_id,
3170               nvl(pea.task_id,
3171                   pdls.task_id)))) task_id,
3172           nvl(pos.award_id,
3173               nvl(psl.award_id,
3174               nvl(pod.award_id,
3175               nvl(pea.award_id,
3176                   pdls.award_id)))) award_id,
3177           ppl.dr_cr_flag,
3178           pdl.effective_date,
3179           nvl(ppc.gl_posting_override_date,ppl.accounting_date) accounting_date, --- added for 3108109
3180           nvl(ppc.exchange_rate_type,ppl.exchange_rate_type) exchange_rate_type,   --- added for 3108109
3181           pdl.distribution_amount,
3182           pdl.distribution_line_id  distribution_line_id,
3183 	  pdl.auto_expenditure_type,
3184           'D' tab_flag,
3185 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute_category, pos.attribute_category), NULL) attribute_category,	-- Introduced DFF columns for bug fix 2908859
3186 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute1, pos.attribute8), NULL) attribute1,
3187 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute2, pos.attribute8), NULL) attribute2,
3188 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute3, pos.attribute8), NULL) attribute3,
3189 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute4, pos.attribute8), NULL) attribute4,
3190 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute5, pos.attribute8), NULL) attribute5,
3191 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute6, pos.attribute8), NULL) attribute6,
3192 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute7, pos.attribute8), NULL) attribute7,
3193 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute8, pos.attribute8), NULL) attribute8,
3194 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute9, pos.attribute9), NULL) attribute9,
3195 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute10, pos.attribute10), NULL) attribute10
3196           ---decode(pdl.suspense_org_account_id, NULL, 'N', 'Y') Suspense_Flag uncommented for 2663344
3197    FROM   psp_schedule_lines              psl,
3198           psp_organization_accounts       pod,
3199           psp_organization_accounts       pos,
3200           psp_element_type_accounts       pea,
3201           psp_default_labor_schedules     pdls,
3202           psp_payroll_controls            ppc,
3203           psp_payroll_lines               ppl,
3204           psp_payroll_sub_lines           ppsl,
3205           psp_distribution_lines          pdl
3206    WHERE  pdl.status_code = 'N'
3207    AND    pdl.payroll_sub_line_id = ppsl.payroll_sub_line_id
3208    AND    ppsl.payroll_line_id = ppl.payroll_line_id
3209    AND    ppl.payroll_control_id = ppc.payroll_control_id
3210    AND    pdl.schedule_line_id = psl.schedule_line_id(+)
3211    AND    pdl.default_org_account_id = pod.organization_account_id(+)
3212    AND    pdl.element_account_id = pea.element_account_id(+)
3213    AND    pdl.org_schedule_id = pdls.org_schedule_id(+)
3214    AND    pdl.suspense_org_account_id = pos.organization_account_id(+)
3215    AND    pdl.gl_project_flag = 'P'
3216    AND    ppc.business_group_id = p_business_group_id
3217    AND    ppc.set_of_books_id = p_set_of_books_id
3218    AND    ppc.payroll_control_id = p_payroll_control_id
3219    ANd    pdl.cap_excess_project_id is null
3220    UNION
3221    SELECT ppg.person_id,
3222           ppg.assignment_id,
3223           nvl(pos.project_id,
3224               ppg.project_id) project_id,
3225           nvl(pos.expenditure_organization_id,
3226               ppg.expenditure_organization_id) expenditure_organization_id,
3227           nvl(ppg.suspense_auto_exp_type,    --- 5080403
3228           nvl(pos.expenditure_type,
3229               ppg.expenditure_type)) expenditure_type,
3230           nvl(pos.task_id,
3231               ppg.task_id) task_id,
3232           nvl(pos.award_id,
3233               ppg.award_id) award_id,
3234           ppg.dr_cr_flag,
3235           ppg.effective_date,
3236           ppc.gl_posting_override_date accounting_date,   -- for 3108109
3237           ppc.exchange_rate_type, -- added for 3108109
3238           ppg.distribution_amount,
3239           ppg.pre_gen_dist_line_id distribution_line_id,
3240 	  NULL, -- Place holder for autopop details
3241           'P' tab_flag,
3242 	DECODE(g_dff_grouping_option, 'Y', DECODE(ppg.suspense_org_account_id, NULL, ppg.attribute_category, pos.attribute_category), NULL) attribute_category,	-- Introduced DFF columns for bug fix 2908859
3243 	DECODE(g_dff_grouping_option, 'Y', DECODE(ppg.suspense_org_account_id, NULL, ppg.attribute1, pos.attribute8), NULL) attribute1,
3244 	DECODE(g_dff_grouping_option, 'Y', DECODE(ppg.suspense_org_account_id, NULL, ppg.attribute2, pos.attribute8), NULL) attribute2,
3245 	DECODE(g_dff_grouping_option, 'Y', DECODE(ppg.suspense_org_account_id, NULL, ppg.attribute3, pos.attribute8), NULL) attribute3,
3246 	DECODE(g_dff_grouping_option, 'Y', DECODE(ppg.suspense_org_account_id, NULL, ppg.attribute4, pos.attribute8), NULL) attribute4,
3247 	DECODE(g_dff_grouping_option, 'Y', DECODE(ppg.suspense_org_account_id, NULL, ppg.attribute5, pos.attribute8), NULL) attribute5,
3248 	DECODE(g_dff_grouping_option, 'Y', DECODE(ppg.suspense_org_account_id, NULL, ppg.attribute6, pos.attribute8), NULL) attribute6,
3249 	DECODE(g_dff_grouping_option, 'Y', DECODE(ppg.suspense_org_account_id, NULL, ppg.attribute7, pos.attribute8), NULL) attribute7,
3250 	DECODE(g_dff_grouping_option, 'Y', DECODE(ppg.suspense_org_account_id, NULL, ppg.attribute8, pos.attribute8), NULL) attribute8,
3251 	DECODE(g_dff_grouping_option, 'Y', DECODE(ppg.suspense_org_account_id, NULL, ppg.attribute9, pos.attribute9), NULL) attribute9,
3252 	DECODE(g_dff_grouping_option, 'Y', DECODE(ppg.suspense_org_account_id, NULL, ppg.attribute10, pos.attribute10), NULL) attribute10
3253           ---decode(ppg.suspense_org_Account_id, NULL, 'N', 'Y') Suspense_Flag   commented for 2663344
3254    FROM   psp_pre_gen_dist_lines     ppg,
3255           psp_organization_accounts  pos,
3256           psp_payroll_controls       ppc
3257    WHERE  ppg.status_code = 'N'
3258    /* changed following condn. First Pass can have suspense: Bug 2007521 */
3259    AND    ((ppg.gl_code_combination_id IS NULL  and pos.gl_code_combination_id is null) OR
3260 		pos.project_id is not null)
3261    AND    ppc.payroll_control_id = p_payroll_control_id  --- 3108109
3262    AND    ppg.suspense_org_account_id = pos.organization_account_id(+)
3263    AND    ppg.payroll_control_id = p_payroll_control_id
3264    AND    ppg.set_of_books_id = p_set_of_books_id
3265    AND    ppg.payroll_control_id = p_payroll_control_id
3266    union
3267    SELECT ppl.person_id,
3268           ppl.assignment_id,
3269           nvl(pos.project_id, pdl.cap_excess_project_id) project_id,
3270           nvl(pos.expenditure_organization_id, pdl.cap_excess_exp_org_id) expenditure_organization_id,
3271           nvl(pos.expenditure_type, nvl(pdl.auto_expenditure_type, pdl.cap_excess_exp_type)) expenditure_type,
3272           nvl(pos.task_id, pdl.cap_excess_task_id) task_id,
3273           nvl(pos.award_id, pdl.cap_excess_award_id) award_id,
3274           ppl.dr_cr_flag,
3275           pdl.effective_date,
3276           nvl(ppc.gl_posting_override_date,ppl.accounting_date) accounting_date, --- added for 3108109
3277           nvl(ppc.exchange_rate_type,ppl.exchange_rate_type) exchange_rate_type,   --- added for 3108109
3278           pdl.distribution_amount,
3279           pdl.distribution_line_id  distribution_line_id,
3280 	  pdl.auto_expenditure_type,
3281           'D' tab_flag,
3282 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute_category, pos.attribute_category), NULL) attribute_category,	-- Introduced DFF columns for bug fix 2908859
3283 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute1, pos.attribute8), NULL) attribute1,
3284 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute2, pos.attribute8), NULL) attribute2,
3285 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute3, pos.attribute8), NULL) attribute3,
3286 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute4, pos.attribute8), NULL) attribute4,
3287 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute5, pos.attribute8), NULL) attribute5,
3288 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute6, pos.attribute8), NULL) attribute6,
3289 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute7, pos.attribute8), NULL) attribute7,
3290 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute8, pos.attribute8), NULL) attribute8,
3291 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute9, pos.attribute9), NULL) attribute9,
3292 	DECODE(g_dff_grouping_option, 'Y', DECODE(pdl.suspense_org_account_id, NULL, pdl.attribute10, pos.attribute10), NULL) attribute10
3293           ---decode(pdl.suspense_org_account_id, NULL, 'N', 'Y') Suspense_Flag uncommented for 2663344
3294    FROM   psp_organization_accounts       pos,
3295           psp_payroll_controls            ppc,
3296           psp_payroll_lines               ppl,
3297           psp_payroll_sub_lines           ppsl,
3298           psp_distribution_lines          pdl
3299    WHERE  pdl.status_code = 'N'
3300    AND    pdl.payroll_sub_line_id = ppsl.payroll_sub_line_id
3301    AND    ppsl.payroll_line_id = ppl.payroll_line_id
3302    AND    ppl.payroll_control_id = ppc.payroll_control_id
3303    AND    pdl.suspense_org_account_id = pos.organization_account_id(+)
3304    AND    pdl.gl_project_flag = 'P'
3305    AND    ppc.business_group_id = p_business_group_id
3306    AND    ppc.set_of_books_id = p_set_of_books_id
3307    AND    ppc.payroll_control_id = p_payroll_control_id
3308    AND    pdl.cap_excess_project_id is not null
3309    ORDER BY 1,2,3,4,5,6,7,8,10,11,16,17,18,19,20,21,22,23,24,25,26,9;
3310 
3311       --- added the order by attribute_category, attribute1 through attribute10 columns for BUG 6007017
3312 
3313 
3314    gms_sum_lines_rec			gms_sum_lines_cursor%ROWTYPE;
3315    payroll_control_rec			payroll_control_cur%ROWTYPE;
3316 
3317    l_person_id				NUMBER(9);
3318    l_assignment_id			NUMBER(9);
3319    l_project_id				NUMBER(15);
3320    l_expenditure_organization_id	NUMBER(15);
3321    l_expenditure_type			VARCHAR2(30);
3322    l_task_id				NUMBER(15);
3323    l_award_id				NUMBER(15);
3324    l_dr_cr_flag				VARCHAR2(1);
3325    l_effective_date			DATE;
3326    l_distribution_amount		NUMBER;
3327    l_rec_count				NUMBER := 0;
3328    l_summary_amount			NUMBER := 0;
3329    l_summary_line_id			NUMBER(10);
3330 
3331    l_attribute_category			VARCHAR2(30);			-- Introduced variables for storing DFF values for bug fix 2908859
3332    l_attribute1				VARCHAR2(150);
3333    l_attribute2				VARCHAR2(150);
3334    l_attribute3				VARCHAR2(150);
3335    l_attribute4				VARCHAR2(150);
3336    l_attribute5				VARCHAR2(150);
3337    l_attribute6				VARCHAR2(150);
3338    l_attribute7				VARCHAR2(150);
3339    l_attribute8				VARCHAR2(150);
3340    l_attribute9				VARCHAR2(150);
3341    l_attribute10			VARCHAR2(150);
3342 
3343    TYPE dist_id IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
3344    dist_line_id				dist_id;
3345    l_dist_line_id			      NUMBER;
3346    i					      BINARY_INTEGER := 0;
3347    j					      NUMBER;
3348    l_return_status			VARCHAR2(10);
3349    --- added following variables for 3108109
3350    l_exchange_rate_type                 VARCHAR2(30);
3351    l_accounting_date            DATE;
3352    l_value                      VARCHAR2(200);
3353    l_table                      VARCHAR2(100);
3354    l_period_end_date            DATE;
3355    l_begin_of_time              DATE := to_date('01/01/1900','dd/mm/yyyy');
3356 
3357   -- R12 MOAC Uptake
3358    l_org_id Number(15);
3359 
3360  BEGIN
3361 
3362          fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Entering create_gms_sum_lines');
3363 
3364   OPEN payroll_control_cur;
3365   LOOP
3366    FETCH payroll_control_cur INTO payroll_control_rec;
3367    IF payroll_control_cur%NOTFOUND THEN
3368      CLOSE payroll_control_cur;
3369      EXIT;
3370    END IF;
3371    -- added for 3108109
3372      BEGIN
3373        SELECT end_date
3374        INTO l_period_end_date
3375        FROM per_time_periods
3376        WHERE time_period_id = payroll_control_rec.time_period_id;
3377      EXCEPTION
3378        WHEN NO_DATA_FOUND THEN
3379          l_value := 'Time Period Id = '||to_char(payroll_control_rec.time_period_id);
3380          l_table := 'PER_TIME_PERIODS';
3381          fnd_message.set_name('PSP','PSP_TR_VALUE_NOT_FOUND');
3382          fnd_message.set_token('VALUE',l_value);
3383          fnd_message.set_token('TABLE',l_table);
3384          fnd_msg_pub.add;
3385          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3386      END;
3387 
3388 		   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' opening gms_sum_lines_cursor');
3389 
3390 
3391    OPEN gms_sum_lines_cursor(payroll_control_rec.payroll_control_id);
3392    l_rec_count := 0;
3393    l_summary_amount := 0;
3394    i := 0;
3395    LOOP
3396      FETCH gms_sum_lines_cursor INTO gms_sum_lines_rec;
3397      l_rec_count := l_rec_count + 1;
3398      IF gms_sum_lines_cursor%ROWCOUNT = 0 THEN
3399        CLOSE gms_sum_lines_cursor;
3400        EXIT;
3401      ELSIF gms_sum_lines_cursor%NOTFOUND THEN
3402        if (l_rec_count > 1) then
3403          update psp_payroll_controls
3404 	    set phase =  'Summarize_GMS_Lines' ----2444657: Replaced NULL
3405           where payroll_control_id = payroll_control_rec.payroll_control_id;
3406        end if;
3407        CLOSE gms_sum_lines_cursor;
3408        EXIT;
3409      END IF;
3410      --
3411 
3412      IF l_rec_count = 1 THEN
3413        l_person_id		:= gms_sum_lines_rec.person_id;
3414        l_assignment_id		:= gms_sum_lines_rec.assignment_id;
3415        l_project_id             := gms_sum_lines_rec.project_id;
3416        l_expenditure_organization_id := gms_sum_lines_rec.expenditure_organization_id;
3417        l_expenditure_type       := gms_sum_lines_rec.expenditure_type;
3418        l_task_id                := gms_sum_lines_rec.task_id;
3419        l_award_id               := gms_sum_lines_rec.award_id;
3420        l_dr_cr_flag		:= gms_sum_lines_rec.dr_cr_flag;
3421        l_effective_date		:= gms_sum_lines_rec.effective_date;
3422        l_accounting_date        := gms_sum_lines_rec.accounting_date;  -- added 2 vars for 3108109
3423        l_exchange_rate_type     := gms_sum_lines_rec.exchange_rate_type;
3424 	l_attribute_category	:= gms_sum_lines_rec.attribute_category;	-- Introduced DFF variable mapping for bug fix 2908859
3425 	l_attribute1		:= gms_sum_lines_rec.attribute1;
3426 	l_attribute2		:= gms_sum_lines_rec.attribute2;
3427 	l_attribute3		:= gms_sum_lines_rec.attribute3;
3428 	l_attribute4		:= gms_sum_lines_rec.attribute4;
3429 	l_attribute5		:= gms_sum_lines_rec.attribute5;
3430 	l_attribute6		:= gms_sum_lines_rec.attribute6;
3431 	l_attribute7		:= gms_sum_lines_rec.attribute7;
3432 	l_attribute8		:= gms_sum_lines_rec.attribute8;
3433 	l_attribute9		:= gms_sum_lines_rec.attribute9;
3434 	l_attribute10		:= gms_sum_lines_rec.attribute10;
3435      END IF;
3436 
3437      IF l_person_id <> gms_sum_lines_rec.person_id OR
3438         l_assignment_id <> gms_sum_lines_rec.assignment_id OR
3439         l_project_id <> gms_sum_lines_rec.project_id OR
3440         l_expenditure_organization_id <> gms_sum_lines_rec.expenditure_organization_id OR
3441         l_expenditure_type <> gms_sum_lines_rec.expenditure_type OR
3442         l_task_id <> gms_sum_lines_rec.task_id OR
3443         l_award_id <> gms_sum_lines_rec.award_id OR
3444         l_dr_cr_flag <> gms_sum_lines_rec.dr_cr_flag  OR
3445 	(NVL(l_attribute_category, 'NULL') <> NVL(gms_sum_lines_rec.attribute_category, 'NULL')) OR	-- Introduced DFF columns checks for bug fix 2908859
3446 	(NVL(l_attribute1, 'NULL') <> NVL(gms_sum_lines_rec.attribute1, 'NULL')) OR
3447 	(NVL(l_attribute2, 'NULL') <> NVL(gms_sum_lines_rec.attribute2, 'NULL')) OR
3448 	(NVL(l_attribute3, 'NULL') <> NVL(gms_sum_lines_rec.attribute3, 'NULL')) OR
3449 	(NVL(l_attribute4, 'NULL') <> NVL(gms_sum_lines_rec.attribute4, 'NULL')) OR
3450 	(NVL(l_attribute5, 'NULL') <> NVL(gms_sum_lines_rec.attribute5, 'NULL')) OR
3451 	(NVL(l_attribute6, 'NULL') <> NVL(gms_sum_lines_rec.attribute6, 'NULL')) OR
3452 	(NVL(l_attribute7, 'NULL') <> NVL(gms_sum_lines_rec.attribute7, 'NULL')) OR
3453 	(NVL(l_attribute8, 'NULL') <> NVL(gms_sum_lines_rec.attribute8, 'NULL')) OR
3454 	(NVL(l_attribute9, 'NULL') <> NVL(gms_sum_lines_rec.attribute9, 'NULL')) OR
3455 	(NVL(l_attribute10, 'NULL') <> NVL(gms_sum_lines_rec.attribute10, 'NULL')) OR
3456         nvl(l_accounting_date,l_begin_of_time) <>
3457               nvl(gms_sum_lines_rec.accounting_date,l_begin_of_time) OR
3458         nvl(l_exchange_rate_type,'-999') <>
3459               nvl(gms_sum_lines_rec.exchange_rate_type,'-999')
3460 	THEN
3461 
3462         -- If it is a Debit entry, it is passed as a +ve entry to Oracle Projects
3463         -- If it is a Credit entry, it is passed as a -ve entry to Oracle Projects
3464         IF l_dr_cr_flag = 'C' THEN
3465           l_summary_amount := 0 - l_summary_amount;
3466         END IF;
3467 
3468 -- R12 MOAC Uptake
3469 	l_org_id := psp_general.Get_transaction_org_id (l_project_id,l_expenditure_organization_id);
3470 
3471 		-- insert into summary lines
3472 		   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Calling insert_into_summary_lines');
3473 
3474         insert_into_summary_lines(
3475             l_summary_line_id,
3476 		l_person_id,
3477 		l_assignment_id,
3478             payroll_control_rec.time_period_id,
3479  		l_effective_date,
3480                 nvl(l_accounting_date,l_period_end_date), --- added for 3108109
3481                 l_exchange_rate_type,
3482             payroll_control_rec.source_type,
3483  		payroll_control_rec.payroll_source_code,
3484             payroll_control_rec.set_of_books_id,
3485             NULL,
3486  		l_project_id,
3487  		l_expenditure_organization_id,
3488  		l_expenditure_type,
3489  		l_task_id,
3490  		l_award_id,
3491  		l_summary_amount,
3492  		l_dr_cr_flag,
3493  		'N',
3494             payroll_control_rec.batch_name,
3495             payroll_control_rec.payroll_control_id,
3496 	    payroll_control_rec.business_group_id,
3497 		l_attribute_category,			-- Introduced DFF columns for bug fix 2908859
3498 		l_attribute1,
3499 		l_attribute2,
3500 		l_attribute3,
3501 		l_attribute4,
3502 		l_attribute5,
3503 		l_attribute6,
3504 		l_attribute7,
3505 		l_attribute8,
3506 		l_attribute9,
3507 		l_attribute10,
3508         l_return_status,
3509 		l_org_id       -- R12 MOAC Uptake
3510 		);
3511 
3512        IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3513          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3514        END IF;
3515 
3516        FOR j IN 1 .. dist_line_id.COUNT LOOP
3517          l_dist_line_id := dist_line_id(j);
3518          --dbms_output.put_line('Dist_line_id to be updated = '||to_char(l_dist_line_id));
3519 
3520          IF gms_sum_lines_rec.tab_flag = 'D' THEN
3521            UPDATE psp_distribution_lines
3522            SET summary_line_id = l_summary_line_id WHERE distribution_line_id = l_dist_line_id;
3523          ELSIF gms_sum_lines_rec.tab_flag = 'P' THEN
3524            UPDATE psp_pre_gen_dist_lines
3525            SET summary_line_id = l_summary_line_id WHERE pre_gen_dist_line_id = l_dist_line_id;
3526          END IF;
3527        END LOOP;
3528 
3529        -- initialise the summary amount and dist_line_id
3530        l_summary_amount := 0;
3531        dist_line_id.delete;
3532        i := 0;
3533      END IF;
3534 
3535      l_person_id			:= gms_sum_lines_rec.person_id;
3536      l_assignment_id		:= gms_sum_lines_rec.assignment_id;
3537      l_project_id               := gms_sum_lines_rec.project_id;
3538      l_expenditure_organization_id := gms_sum_lines_rec.expenditure_organization_id;
3539      l_expenditure_type         := gms_sum_lines_rec.expenditure_type;
3540      l_task_id                  := gms_sum_lines_rec.task_id;
3541      l_award_id                 := gms_sum_lines_rec.award_id;
3542      l_dr_cr_flag			:= gms_sum_lines_rec.dr_cr_flag;
3543      l_effective_date		:= gms_sum_lines_rec.effective_date;
3544      l_accounting_date          := gms_sum_lines_rec.accounting_date;  --- 3108109
3545      l_exchange_rate_type       := gms_sum_lines_rec.exchange_rate_type;
3546 	l_attribute_category	:= gms_sum_lines_rec.attribute_category;	-- Introduced DFF variable mapping for bug fix 2908859
3547 	l_attribute1		:= gms_sum_lines_rec.attribute1;
3548 	l_attribute2		:= gms_sum_lines_rec.attribute2;
3549 	l_attribute3		:= gms_sum_lines_rec.attribute3;
3550 	l_attribute4		:= gms_sum_lines_rec.attribute4;
3551 	l_attribute5		:= gms_sum_lines_rec.attribute5;
3552 	l_attribute6		:= gms_sum_lines_rec.attribute6;
3553 	l_attribute7		:= gms_sum_lines_rec.attribute7;
3554 	l_attribute8		:= gms_sum_lines_rec.attribute8;
3555 	l_attribute9		:= gms_sum_lines_rec.attribute9;
3556 	l_attribute10		:= gms_sum_lines_rec.attribute10;
3557 
3558      l_summary_amount := l_summary_amount + gms_sum_lines_rec.distribution_amount;
3559      i := i + 1;
3560      dist_line_id(i) := gms_sum_lines_rec.distribution_line_id;
3561 
3562 
3563    END LOOP;
3564 
3565    IF l_dr_cr_flag = 'C' Then
3566 	l_summary_amount := 0 - l_summary_amount;
3567    END IF;
3568 
3569    IF l_rec_count > 1 THEN
3570      -- insert into summary lines
3571 
3572 -- R12 MOAC Uptake
3573 	l_org_id := psp_general.Get_transaction_org_id (l_project_id,l_expenditure_organization_id);
3574 
3575 	 insert_into_summary_lines(
3576             l_summary_line_id,
3577 		l_person_id,
3578 		l_assignment_id,
3579             payroll_control_rec.time_period_id,
3580  		l_effective_date,
3581                 nvl(l_accounting_date,l_period_end_date),  --- 3108109
3582                 l_exchange_rate_type,
3583             payroll_control_rec.source_type,
3584  		payroll_control_rec.payroll_source_code,
3585             payroll_control_rec.set_of_books_id,
3586  		NULL,
3587  		l_project_id,
3588  		l_expenditure_organization_id,
3589  		l_expenditure_type,
3590  		l_task_id,
3591  		l_award_id,
3592  		l_summary_amount,
3593  		l_dr_cr_flag,
3594  		'N',
3595             payroll_control_rec.batch_name,
3596             payroll_control_rec.payroll_control_id,
3597 	    payroll_control_rec.business_group_id,
3598 		l_attribute_category,			-- Introduced DFF columns for bug fix 2908859
3599 		l_attribute1,
3600 		l_attribute2,
3601 		l_attribute3,
3602 		l_attribute4,
3603 		l_attribute5,
3604 		l_attribute6,
3605 		l_attribute7,
3606 		l_attribute8,
3607 		l_attribute9,
3608 		l_attribute10,
3609         l_return_status,
3610 		l_org_id		-- R12 MOAC uptake
3611 		);
3612 
3613      IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3614        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3615      END IF;
3616 
3617      FOR j IN 1 .. dist_line_id.COUNT LOOP
3618        l_dist_line_id := dist_line_id(j);
3619        --dbms_output.put_line('Dist_line_id to be updated = '||to_char(l_dist_line_id));
3620 
3621        IF gms_sum_lines_rec.tab_flag = 'D' THEN
3622          UPDATE psp_distribution_lines
3623          SET summary_line_id = l_summary_line_id,
3624              status_code = 'N'
3625          WHERE distribution_line_id = l_dist_line_id;
3626        ELSIF gms_sum_lines_rec.tab_flag = 'P' THEN
3627          UPDATE psp_pre_gen_dist_lines
3628          SET summary_line_id = l_summary_line_id,
3629              status_code = 'N'
3630          WHERE pre_gen_dist_line_id = l_dist_line_id;
3631        END IF;
3632      END LOOP;
3633      dist_line_id.delete;
3634    END IF;
3635   END LOOP;
3636   --
3637   p_return_status := fnd_api.g_ret_sts_success;
3638 
3639 
3640  EXCEPTION
3641 
3642    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3643      g_error_api_path := 'CREATE_GMS_SUM_LINES:'||g_error_api_path;
3644      p_return_status := fnd_api.g_ret_sts_unexp_error;
3645    WHEN OTHERS THEN
3646 			   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
3647 
3648      g_error_api_path := 'CREATE_GMS_SUM_LINES:'||g_error_api_path;
3649      fnd_msg_pub.add_exc_msg('PSP_SUM_TRANS','CREATE_GMS_SUM_LINES');
3650      p_return_status := fnd_api.g_ret_sts_unexp_error;
3651 
3652  END;
3653 
3654 ----------------------------- GMS INTERFACE ---------------------------------------------
3655  PROCEDURE transfer_to_gms_interface(p_source_type     IN VARCHAR2,
3656                                      p_source_code     IN VARCHAR2,
3657                                      p_time_period_id  IN NUMBER,
3658                                      p_batch_name      IN VARCHAR2,
3659 				     p_business_group_id IN NUMBER,
3660 				     p_set_of_books_id   IN NUMBER,
3661                                      p_return_status  OUT NOCOPY VARCHAR2) IS
3662 
3663    CURSOR gms_batch_cursor IS
3664    SELECT payroll_control_id,
3665           source_type,
3666           payroll_source_code,
3667           time_period_id,
3668           batch_name,
3669 --      Introduced the following currency_code,exchange_rate_type  for bug 2916848
3670           Currency_code,
3671           exchange_rate_type,
3672           phase                   --- added for 2444657
3673    FROM   psp_payroll_controls
3674    WHERE status_code = 'I'
3675    AND    source_type <> 'A'
3676    AND    run_id = g_run_id
3677    AND    phase in ('Summarize_GMS_Lines','Submitted_TI_Request');
3678 
3679    CURSOR gms_interface_cursor(P_PAYROLL_CONTROL_ID  IN  NUMBER) IS
3680    SELECT psl.summary_line_id,
3681           psl.source_code,
3682           psl.person_id,
3683           psl.assignment_id,
3684           NVL(psl.gms_posting_effective_date,psl.effective_date) effective_date, /* Column modified for Enhancement Employee Assignment with Zero Work Days */
3685           psl.accounting_date, --- added 2 cols for 3108109
3686           psl.exchange_Rate_type,
3687           psl.project_id,
3688           psl.expenditure_organization_id,
3689           psl.expenditure_type,
3690           psl.task_id,
3691           psl.award_id,
3692           psl.summary_amount,
3693           psl.dr_cr_flag,
3694           psl.attribute1,		-- Introduced attributes 1, 4 and 5 for bug fix 2908859
3695           psl.attribute2,
3696           psl.attribute3,
3697           psl.attribute4,
3698           psl.attribute5,
3699           psl.attribute6,
3700           psl.attribute7,
3701           psl.attribute8,
3702           psl.attribute9,
3703           psl.attribute10,
3704 		  org_id
3705    FROM  psp_summary_lines  psl
3706    WHERE psl.status_code = 'N'
3707    AND   psl.gl_code_combination_id IS NULL
3708    AND   psl.payroll_control_id = p_payroll_control_id;
3709 
3710    gms_batch_rec			gms_batch_cursor%ROWTYPE;
3711    gms_interface_rec		gms_interface_cursor%ROWTYPE;
3712    l_transaction_source		VARCHAR2(30);
3713    l_expenditure_comment	VARCHAR2(240);
3714    l_employee_number		VARCHAR2(30);
3715    l_org_name			hr_all_organization_units_tl.name%TYPE;	-- Bug 2447912: Modified declaration
3716    l_segment1			VARCHAR2(25);
3717    l_task_number			VARCHAR2(300);		-- Bug 16391366 (27/03/2013)
3718    l_gms_batch_name		VARCHAR2(10);
3719    l_expenditure_ending_date	DATE;
3720    l_period_name			VARCHAR2(35);
3721    l_period_end_date		DATE;               ----uncommented for 2663344
3722    l_return_status		VARCHAR2(50);  --- increased the size from 10 for 2671594
3723    req_id				NUMBER(15);
3724    call_status			BOOLEAN;
3725    rphase				VARCHAR2(30);
3726    rstatus				VARCHAR2(30);
3727    dphase				VARCHAR2(30);
3728    dstatus				VARCHAR2(30);
3729    message				VARCHAR2(240);
3730    p_errbuf				VARCHAR2(32767);
3731    p_retcode			VARCHAR2(32767);
3732    return_back			EXCEPTION;
3733    l_rec_count			NUMBER := 0;
3734    l_error				VARCHAR2(100);
3735    l_product			VARCHAR2(3);
3736    l_value				VARCHAR2(200);
3737    l_table				VARCHAR2(100);
3738 -- Populate the GMS_TRANSACTION_INTERFACE table also.
3739    gms_rec			gms_transaction_interface_all%ROWTYPE;
3740    l_gms_transaction_source	VARCHAR2(20);
3741    l_txn_source			VARCHAR2(30);
3742 --   l_org_id			NUMBER(15);       -- Commented for R12 MOAC uptake
3743    l_txn_interface_id		NUMBER(15);
3744 	l_gms_install		BOOLEAN	DEFAULT gms_install.enabled;
3745 
3746  -- R12 MOAC Uptake
3747 	TYPE ORG_ID_TYPE IS TABLE OF NUMBER(15) INDEX BY BINARY_INTEGER;
3748     ORG_ID_TAB  ORG_ID_TYPE;
3749 
3750 	TYPE gms_batch_name_TYPE IS TABLE OF varchar2(10) INDEX BY BINARY_INTEGER;
3751     gms_batch_name_TAB gms_batch_name_TYPE;
3752 
3753  	TYPE req_id_TYPE is TABLE OF 	NUMBER(15) INDEX BY BINARY_INTEGER;
3754     req_id_TAB req_id_TYPE;
3755 
3756 	TYPE call_status_TYPE IS TABLE OF BOOLEAN INDEX BY BINARY_INTEGER;
3757     call_status_TAB call_status_TYPE;
3758 
3759 
3760 	Cursor operating_unit_csr(p_payroll_control_id  IN  NUMBER) IS  -- change
3761 	SELECT Distinct org_id
3762 	FROM  psp_summary_lines  psl
3763 	WHERE psl.status_code = 'N'
3764 	AND   psl.gl_code_combination_id IS NULL
3765 	AND   psl.payroll_control_id = p_payroll_control_id;
3766 
3767 	temp_org_id Number(15);
3768 
3769  BEGIN
3770 
3771    -- get the source name
3772 		  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Entered transfer_to_gms_interface');
3773 
3774    BEGIN
3775      SELECT transaction_source
3776      INTO   l_transaction_source
3777      FROM   pa_transaction_sources
3778      WHERE  transaction_source = 'OLD';
3779    EXCEPTION
3780      WHEN NO_DATA_FOUND THEN
3781        l_error := 'TRANSACTION SOURCE = OLD';
3782        l_product := 'PA';
3783        fnd_message.set_name('PSP','PSP_TR_NOT_SET_UP');
3784        fnd_message.set_token('ERROR',l_error);
3785        fnd_message.set_token('PRODUCT',l_product);
3786        fnd_msg_pub.add;
3787        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3788    END;
3789 
3790    if (l_gms_install) then			-- Changed site_enabled check to l_gms_install check for bug fix 2908859
3791    BEGIN
3792      SELECT transaction_source
3793      INTO   l_gms_transaction_source
3794      FROM   pa_transaction_sources
3795      WHERE  transaction_source = 'GOLD';
3796    EXCEPTION
3797      WHEN NO_DATA_FOUND THEN
3798        l_error := 'TRANSACTION SOURCE = GOLD';
3799        l_product := 'GMS';
3800        fnd_message.set_name('PSP','PSP_TR_NOT_SET_UP');
3801        fnd_message.set_token('ERROR',l_error);
3802        fnd_message.set_token('PRODUCT',l_product);
3803        fnd_msg_pub.add;
3804        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3805    END;
3806    end if;
3807 
3808 		   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' open gms_batch_cursor');
3809 
3810    OPEN gms_batch_cursor;
3811    LOOP
3812      FETCH gms_batch_cursor INTO gms_batch_rec;
3813      IF gms_batch_cursor%NOTFOUND THEN
3814        CLOSE gms_batch_cursor;
3815        EXIT;
3816      END IF ;
3817 
3818 
3819 	 -- get the period_name (moved this statement from below for 2444657)
3820      BEGIN
3821       -- uncommented period end date for 2663344
3822        SELECT substr(period_name ,1,35),end_date
3823        INTO l_period_name ,l_period_end_date
3824        FROM per_time_periods
3825        WHERE time_period_id = gms_batch_rec.time_period_id;
3826      EXCEPTION
3827        WHEN NO_DATA_FOUND THEN
3828          l_value := 'Time Period Id = '||to_char(gms_batch_rec.time_period_id);
3829          l_table := 'PER_TIME_PERIODS';
3830          fnd_message.set_name('PSP','PSP_TR_VALUE_NOT_FOUND');
3831          fnd_message.set_token('VALUE',l_value);
3832          fnd_message.set_token('TABLE',l_table);
3833          fnd_msg_pub.add;
3834          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3835      END;
3836 
3837 	-- R12 MOAC Uptake .. org_id array to always populate 5090047
3838         -- moved If condin  below
3839 	ORG_ID_TAB.delete;
3840 	gms_batch_name_TAB.delete;
3841 	req_id_TAB.delete;
3842 	call_status_TAB.delete;
3843 
3844 	OPEN  operating_unit_csr(gms_batch_rec.payroll_control_id);  -- change
3845 	FETCH operating_unit_csr BULK COLLECT INTO org_id_tab;
3846 	CLOSE operating_unit_csr ;
3847 
3848     IF gms_batch_rec.phase = 'Summarize_GMS_Lines' then  --2444657
3849 
3850 	FOR I in 1..org_id_tab.count
3851 	LOOP
3852 	 SELECT to_char(psp_gms_batch_name_s.nextval)
3853      INTO gms_batch_name_tab(i)
3854      FROM dual;
3855 	END LOOP;
3856 /*
3857 	 -- get the group_id
3858 	 SELECT to_char(psp_gms_batch_name_s.nextval)
3859      INTO l_gms_batch_name
3860      FROM dual;
3861 */
3862 	-- R12 MOAC Uptake. Moved this code to loop
3863 	 -- update psp_summary_lines with gms batch name
3864     FOR I in 1..org_id_tab.count
3865 	LOOP
3866 		 UPDATE psp_summary_lines
3867 		 SET gms_batch_name = gms_batch_name_tab(i)				 -- R12 MOAC uptake. changed from l_gms_batch_name
3868 		 WHERE payroll_control_id = gms_batch_rec.payroll_control_id
3869 		 AND   status_code = 'N'
3870 		 AND   gl_code_combination_id IS NULL
3871 		 AND   org_id = org_id_tab(i);  	-- R12 MOAC uptake
3872 
3873 				  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' updated psp_summary_lines set status_code for org_id'||org_id_tab(i)||' to N count: '||sql%rowcount);
3874 
3875 	END LOOP;
3876 
3877 
3878      l_expenditure_comment := gms_batch_rec.source_type||':'||gms_batch_rec.payroll_source_code||':'||l_period_name||':'||gms_batch_rec.batch_name;
3879 
3880 --     l_interface_id := pa_txn_interface_s.nextval;
3881 
3882 			 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' open gms_interface_cursor');
3883 
3884      OPEN gms_interface_cursor(gms_batch_rec.payroll_control_id);
3885      l_rec_count := 0;
3886      LOOP
3887        FETCH gms_interface_cursor INTO gms_interface_rec;
3888        IF gms_interface_cursor%NOTFOUND THEN
3889          CLOSE gms_interface_cursor;
3890          EXIT;
3891        END IF;
3892        l_rec_count := l_rec_count + 1;
3893 
3894        -- get the employee number
3895        BEGIN
3896          SELECT employee_number
3897          INTO l_employee_number
3898          FROM per_people_f
3899          WHERE person_id = gms_interface_rec.person_id
3900          AND gms_interface_rec.effective_date BETWEEN effective_start_date AND effective_end_date;
3901        EXCEPTION
3902          WHEN NO_DATA_FOUND THEN
3903            l_value := 'Person Id = '||to_char(gms_interface_rec.person_id);
3904            l_table := 'PER_PEOPLE_F';
3905            fnd_message.set_name('PSP','PSP_TR_VALUE_NOT_FOUND');
3906            fnd_message.set_token('VALUE',l_value);
3907            fnd_message.set_token('TABLE',l_table);
3908            fnd_msg_pub.add;
3909            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3910        END;
3911 
3912        -- get the employee's organization name
3913        BEGIN
3914 --        SELECT substr(name,1,60)	Commented for bug fix 2447912
3915         SELECT name			-- Removed SUBSTR for bug fix 2447912
3916         INTO  l_org_name
3917         FROM  hr_all_organization_units hou
3918         WHERE organization_id = gms_interface_rec.expenditure_organization_id;
3919        EXCEPTION
3920          WHEN NO_DATA_FOUND THEN
3921            l_value := 'Organization Id = '||to_char(gms_interface_rec.expenditure_organization_id);
3922            l_table := 'HR_ORGANIZATION_UNITS';
3923            fnd_message.set_name('PSP','PSP_TR_VALUE_NOT_FOUND');
3924            fnd_message.set_token('VALUE',l_value);
3925            fnd_message.set_token('TABLE',l_table);
3926            fnd_msg_pub.add;
3927            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3928            --l_org_name := NULL;
3929            --l_org_name := 'LDM_ORG_NAME_INVALID';
3930        END;
3931 
3932        -- get the project number
3933 	 BEGIN
3934          SELECT segment1
3935          INTO   l_segment1
3936          FROM   pa_projects_all
3937          WHERE  project_id = gms_interface_rec.project_Id;
3938        EXCEPTION
3939          WHEN NO_DATA_FOUND THEN
3940            l_value := 'Project Id = '||to_char(gms_interface_rec.project_Id);
3941            l_table := 'PA_PROJECTS_ALL';
3942            fnd_message.set_name('PSP','PSP_TR_VALUE_NOT_FOUND');
3943            fnd_message.set_token('VALUE',l_value);
3944            fnd_message.set_token('TABLE',l_table);
3945            fnd_msg_pub.add;
3946            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3947        END;
3948 
3949 -- R12 MOAC Uptake. Org_id is stored in psp_summary_line Table
3950 /*
3951 -- get the org_id for the project..
3952 	 BEGIN
3953          SELECT org_id
3954          INTO   l_org_id
3955          FROM   pa_projects_all
3956          WHERE  project_id = gms_interface_rec.project_Id;
3957        EXCEPTION
3958          WHEN NO_DATA_FOUND THEN
3959            l_value := 'Project Id = '||to_char(gms_interface_rec.project_Id);
3960            l_table := 'PA_PROJECTS_ALL';
3961            fnd_message.set_name('PSP','PSP_ORG_VALUE_NOT_FOUND');
3962            fnd_message.set_token('VALUE',l_value);
3963            fnd_message.set_token('TABLE',l_table);
3964            fnd_msg_pub.add;
3965            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3966        END;
3967 */
3968 
3969        -- get the task number
3970        BEGIN
3971          SELECT task_number
3972          INTO  l_task_number
3973          FROM  pa_tasks_expend_v  -- Bug : 16391366  (20/03/2013)
3974          WHERE task_id = gms_interface_rec.task_id;
3975        EXCEPTION
3976          WHEN NO_DATA_FOUND THEN
3977            l_value := 'Task Id = '||to_char(gms_interface_rec.task_id);
3978            l_table := 'PA_TASKS';
3979            fnd_message.set_name('PSP','PSP_TR_VALUE_NOT_FOUND');
3980            fnd_message.set_token('VALUE',l_value);
3981            fnd_message.set_token('TABLE',l_table);
3982            fnd_msg_pub.add;
3983            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3984        END;
3985 
3986 --  Get the transaction_interface_id. We need this to populate the gms_interface table.
3987 
3988      select pa_txn_interface_s.nextval
3989        into l_txn_interface_id
3990        from dual;
3991 
3992        -- get the expenditure week ending date
3993 
3994 	-- set the context to single to call pa_utils function
3995 	mo_global.set_policy_context('S', gms_interface_rec.org_id );
3996 
3997        l_expenditure_ending_date := pa_utils.GetWeekending(gms_interface_rec.effective_date);
3998 
3999 	-- set the context again to multiple
4000 	mo_global.set_policy_context('M', null);
4001 
4002 
4003 --
4004 
4005  --      dbms_output.put_line('Inserting into pa interface.............');
4006 
4007 --	if gms_interface_rec.award_id is not null then		Commented for bug fix 2908859
4008 	if (l_gms_install) then		-- Introduced for bug fix 2908859
4009 	   l_txn_source := l_gms_transaction_source;
4010 	else
4011 	   l_txn_source := l_transaction_source;
4012 	end if;
4013 
4014  --R12 MOAC Uptake
4015 	FOR I in 1..org_id_tab.count
4016 	LOOP
4017 		IF org_id_tab(I) = gms_interface_rec.org_id THEN
4018 			l_gms_batch_name := gms_batch_name_tab(I);
4019 			EXIT;
4020 		END IF;
4021 	END LOOP;
4022 
4023 
4024 
4025 --  Modified g_currency_code to gms_batch_rec.currency_code,Introduced Actual_exchange_rate_type
4026 --  actual rate_date for Bug 2916848
4027 
4028 IF (gms_batch_rec.currency_code <> 'STAT') THEN
4029 				 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Calling insert_into_pa_interface ');
4030 
4031        insert_into_pa_interface(l_txn_interface_id,
4032 	l_txn_source,
4033 	L_GMS_BATCH_NAME,
4034 	L_EXPENDITURE_ENDING_DATE,
4035 	L_EMPLOYEE_NUMBER,
4036 	L_ORG_NAME,
4037 	GMS_INTERFACE_REC.EFFECTIVE_DATE,
4038 	L_SEGMENT1,
4039 	--L_TASK_NUMBER,	-- Commented for Bug : 16591849
4040 	GMS_INTERFACE_REC.TASK_ID,		-- Added for Bug : 16591849
4041 	GMS_INTERFACE_REC.EXPENDITURE_TYPE,
4042 	1,
4043 	GMS_INTERFACE_REC.SUMMARY_AMOUNT,
4044 	L_EXPENDITURE_COMMENT,
4045 --	'P',GMS_INTERFACE_REC.SUMMARY_LINE_ID,GMS_INTERFACE_REC.AWARD_ID,
4046 -- Award details are populated into GMS_INT
4047 	'P',
4048 	GMS_INTERFACE_REC.SUMMARY_LINE_ID,
4049 	GMS_INTERFACE_REC.ORG_ID,
4050 	gms_batch_rec.CURRENCY_CODE ,
4051 	GMS_INTERFACE_REC.SUMMARY_AMOUNT,
4052 	gms_interface_rec.attribute1, 	--Changed NULL vale for Attribute1 to interface value for bug fix 2908859
4053 	GMS_INTERFACE_REC.ATTRIBUTE2,
4054 	GMS_INTERFACE_REC.ATTRIBUTE3,
4055 	GMS_INTERFACE_REC.ATTRIBUTE4,
4056 	GMS_INTERFACE_REC.ATTRIBUTE5,		-- Introduced attributes 4 and 5 for bug fix 2908859
4057 	GMS_INTERFACE_REC.ATTRIBUTE6,
4058 	GMS_INTERFACE_REC.ATTRIBUTE7,
4059 	GMS_INTERFACE_REC.ATTRIBUTE8,
4060 	GMS_INTERFACE_REC.ATTRIBUTE9,
4061 	GMS_INTERFACE_REC.ATTRIBUTE10,
4062         GMS_INTERFACE_REC.EXCHANGE_RATE_TYPE,
4063         gms_interface_rec.accounting_date,-- Introduced for bug 2916848 Ilo Ehn Mrc
4064 	p_business_group_id,--Introduced for bug 2935850
4065 	L_RETURN_STATUS);
4066 
4067        IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4068          --dbms_output.put_line('Faaaaaaiiiiiiilllllllleeeeeeeeeddddddd......');
4069          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4070        END IF;
4071 
4072   ELSE
4073     insert_into_pa_interface(l_txn_interface_id,
4074 	l_txn_source,
4075 	L_GMS_BATCH_NAME,
4076 	L_EXPENDITURE_ENDING_DATE,
4077 	L_EMPLOYEE_NUMBER,
4078 	L_ORG_NAME,
4079 	GMS_INTERFACE_REC.EFFECTIVE_DATE,
4080 	L_SEGMENT1,
4081 	--L_TASK_NUMBER,		-- Commented for Bug 16591849
4082 	GMS_INTERFACE_REC.TASK_ID,	-- Added for Bug 16591849
4083 	GMS_INTERFACE_REC.EXPENDITURE_TYPE,
4084 	GMS_INTERFACE_REC.SUMMARY_AMOUNT,
4085 	0,
4086 	L_EXPENDITURE_COMMENT,
4087 	'P',
4088 	GMS_INTERFACE_REC.SUMMARY_LINE_ID,
4089 	GMS_INTERFACE_REC.ORG_ID,
4090 	gms_batch_rec.CURRENCY_CODE ,
4091 	GMS_INTERFACE_REC.SUMMARY_AMOUNT,
4092 	gms_interface_rec.attribute1, 	--Changed NULL vale for Attribute1 to interface value for bug fix 2908859
4093 	GMS_INTERFACE_REC.ATTRIBUTE2,
4094 	GMS_INTERFACE_REC.ATTRIBUTE3,
4095 	GMS_INTERFACE_REC.ATTRIBUTE4,
4096 	GMS_INTERFACE_REC.ATTRIBUTE5,		-- Introduced attributes 4 and 5 for bug fix 2908859
4097 	GMS_INTERFACE_REC.ATTRIBUTE6,
4098 	GMS_INTERFACE_REC.ATTRIBUTE7,
4099 	GMS_INTERFACE_REC.ATTRIBUTE8,
4100 	GMS_INTERFACE_REC.ATTRIBUTE9,
4101 	GMS_INTERFACE_REC.ATTRIBUTE10,
4102         GMS_INTERFACE_REC.EXCHANGE_RATE_TYPE,
4103         gms_interface_rec.accounting_date,-- Introduced for bug 2916848 Ilo Ehn Mrc
4104 	p_business_group_id,--Introduced for bug 2935850
4105 	L_RETURN_STATUS);
4106 
4107 	 IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4108 			RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4109 	 END IF;
4110 
4111   END IF;
4112 
4113 
4114 	if (gms_interface_rec.award_id is not null) then
4115 
4116         GMS_REC.TXN_INTERFACE_ID 	    :=  l_txn_interface_id;
4117 	GMS_REC.BATCH_NAME 	            := l_gms_batch_name;
4118 	GMS_REC.TRANSACTION_SOURCE 	    := l_gms_transaction_source;
4119 	GMS_REC.EXPENDITURE_ENDING_DATE     := l_expenditure_ending_date;
4120 	GMS_REC.EXPENDITURE_ITEM_DATE 	    := gms_interface_rec.effective_date;
4121 	GMS_REC.PROJECT_NUMBER 	  	    := l_segment1;
4122 	GMS_REC.TASK_NUMBER 	  	    := l_task_number;
4123 	GMS_REC.AWARD_ID 	    	    := gms_interface_rec.award_id;
4124 	GMS_REC.EXPENDITURE_TYPE 	    := gms_interface_rec.expenditure_type;
4125 	GMS_REC.TRANSACTION_STATUS_CODE     := 'P';
4126 	GMS_REC.ORIG_TRANSACTION_REFERENCE  := gms_interface_rec.summary_line_id;
4127 	GMS_REC.ORG_ID 	  		    := GMS_INTERFACE_REC.org_id;
4128 	GMS_REC.SYSTEM_LINKAGE		    := NULL;
4129 	GMS_REC.USER_TRANSACTION_SOURCE     := NULL;
4130 	GMS_REC.TRANSACTION_TYPE 	    := NULL;
4131 	GMS_REC.BURDENABLE_RAW_COST 	    := gms_interface_rec.summary_amount;
4132 	GMS_REC.FUNDING_PATTERN_ID 	    := NULL;
4133 
4134 	gms_transactions_pub.LOAD_GMS_XFACE_API(gms_rec, l_return_status);
4135 
4136        IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4137          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4138        END IF;
4139 
4140        end if;
4141 
4142      END LOOP;
4143     else --- 2444657   --- phase = 'Submitted_TI_Request'
4144      for i in 1..org_id_tab.count
4145      loop
4146      select gms_batch_name
4147      into gms_batch_name_tab(i)  -- change
4148      from psp_summary_lines
4149      where payroll_control_id = gms_batch_rec.payroll_control_id
4150        and org_id = org_id_tab(i)
4151        and project_id is not null
4152        and rownum = 1;
4153      end loop;
4154        if (l_gms_install) then   --- fix for 5090047
4155           l_txn_source := l_gms_transaction_source;
4156        else
4157           l_txn_source := l_transaction_source;
4158        end if;
4159        hr_utility.trace(' deriving l_txn_source = '|| l_txn_source);
4160     end if; --- phase = 'Summarize_GL_Lines' ..2444657
4161     IF l_rec_count > 0 and gms_batch_rec.phase = 'Summarize_GMS_Lines' THEN ---2444657  -- change
4162 
4163     g_skip_flag_gms := 'N';
4164 
4165     IF (g_create_stat_batch_in_gms = 'N' and gms_batch_rec.CURRENCY_CODE = 'STAT') then
4166                 -- Added this IF for bug 6902514
4167                g_skip_flag_gms := 'Y';
4168            req_id := 1;
4169 
4170     ELSE
4171 
4172     		g_skip_flag_gms := 'N'; -- added for bug 6902514
4173 
4174      		FOR I in 1..org_id_tab.count
4175 		LOOP
4176 			l_gms_batch_name := gms_batch_name_tab(I);
4177 
4178 			-- set the context to single to submit_request
4179 			mo_global.set_policy_context('S', org_id_tab(I) );
4180 			fnd_request.set_org_id (org_id_tab(I) );
4181 
4182 					  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' submitting request for Transaction_Import for org_id: '||org_id_tab(I));
4183 
4184 			 req_id_tab(i) := fnd_request.submit_request(
4185                                  'PA',
4186                                  'PAXTRTRX',
4187                                  NULL,
4188                                  NULL,
4189                                  FALSE,
4190                                  l_txn_source, -- should be  l_txn_source rather than l_transaction_source,
4191                                  l_gms_batch_name);
4192 
4193 		    IF req_id = 0 THEN
4194 		       fnd_message.set_name('PSP','PSP_TR_GMS_IMP_FAILED');
4195 		       fnd_msg_pub.add;
4196 		       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4197 			END IF;
4198 		END LOOP;
4199 
4200    END IF; -- g_create_stat_batch_in_gms  -- change
4201 
4202 		update psp_payroll_controls
4203 		set phase = 'Submitted_TI_Request'
4204 		WHERE payroll_control_id = gms_batch_rec.payroll_control_id;
4205 
4206 		commit;
4207 				  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Committed after updating psp_payroll_controls SET 	phase = Submitted_TI_Request count: '||sql%rowcount);
4208 
4209 
4210 		IF g_skip_flag_gms = 'N' THEN  -- bug 6902514  -- change
4211 
4212 		 FOR I in 1..org_id_tab.count
4213 		 LOOP
4214 			call_status_tab(I) := fnd_concurrent.wait_for_request(req_id_tab(I), 20, 0,
4215 				                rphase, rstatus, dphase, dstatus, message);
4216 			IF call_status_tab(I) = FALSE then
4217 				 fnd_message.set_name('PSP','PSP_TR_GMS_IMP_FAILED');
4218 		         fnd_msg_pub.add;
4219 				 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4220 			END IF;
4221 		 END LOOP;
4222 
4223 		END IF; -- g_skip_flag_gms  -- change
4224 
4225        gms_batch_rec.phase := 'Submitted_TI_Request'; ---2444657
4226     END IF;   --- 2444657 end rec_count > 0
4227 
4228 	-- set the context again to multiple
4229 	mo_global.set_policy_context('M', null);
4230 
4231    IF gms_batch_rec.phase =  'Submitted_TI_Request' then  --2444657
4232              ---- added below for 5742525
4233 	IF FND_PROFILE.VALUE('PSP_ST_EXTENSION_ENABLE') = 'Y' THEN
4234               psp_st_ext.tieback_actual(gms_batch_rec.payroll_control_id ,
4235                            gms_batch_rec.source_type        ,
4236                            l_period_end_date    ,
4237                            l_gms_batch_name     ,
4238                            l_txn_source,
4239                            p_business_group_id  ,
4240                            p_set_of_books_id    );
4241          ENd if;
4242 
4243     -- mark the successfully transferred records as 'A' in psp_summary_lines and psp_distribution_lines
4244     -- and transfer the successful records to the history table
4245 --- Bug 2663344 reverted  NULL to l_period_end_date in tie back call
4246           hr_utility.trace(' before loop for gms_tie_back');
4247 		FOR I in 1..org_id_tab.count
4248 		LOOP
4249           hr_utility.trace(' tab_count ='||i);
4250 			l_gms_batch_name := gms_batch_name_tab(I);
4251           hr_utility.trace(' tab_count 2nd time='||i);
4252 
4253 					fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Calling gl_tie_back for org_id: '||org_id_tab(I));
4254 
4255 			gms_tie_back(gms_batch_rec.payroll_control_id,
4256 						gms_batch_rec.source_type,
4257 						l_period_end_date,
4258 						l_gms_batch_name,
4259 						p_business_group_id,
4260 						p_set_of_books_id,
4261 						l_txn_source,
4262 						'N',		--Introduced as part of Bug fix #1776606
4263 						l_return_status);
4264           hr_utility.trace(' after tie back call to  gms_tie_back');
4265 
4266      /* Bug 1617846 LD Recovery LOV not showing up fialed S and T */
4267      /* introduced ELSE clause */
4268 
4269 			IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4270 				RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4271 --			ELSE   --Commented for R12 MOAc uptake;
4272         --- moved update psp_payroll_control phase = Tie back inside gms_tie_back
4273          --- for 2444657
4274 --				commit;    --Commented for R12 MOAc uptake
4275 		     END IF;
4276 		END LOOP;
4277 		commit; -- R12 MOAc uptake; moved commit from above
4278 	END IF;
4279    END LOOP;
4280 
4281    --
4282    p_return_status := fnd_api.g_ret_sts_success;
4283  EXCEPTION
4284    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4285      g_error_api_path := 'TRANSFER_TO_GMS_INTERFACE:'||g_error_api_path;
4286      p_return_status := fnd_api.g_ret_sts_unexp_error;
4287 
4288    WHEN RETURN_BACK THEN
4289      p_return_status := fnd_api.g_ret_sts_success;
4290 
4291    WHEN OTHERS THEN
4292 			 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
4293 
4294      g_error_api_path := 'TRANSFER_TO_GMS_INTERFACE:'||g_error_api_path;
4295      fnd_msg_pub.add_exc_msg('PSP_SUM_TRANS','TRANSFER_TO_GMS_INTERFACE');
4296      p_return_status := fnd_api.g_ret_sts_unexp_error;
4297  END;
4298 
4299 
4300 ------------------------- GMS TIE BACK ---------------------------------------------------
4301  PROCEDURE gms_tie_back(p_payroll_control_id  IN  NUMBER,
4302                         p_source_type         IN  VARCHAR2,
4303                         p_period_end_date     IN  DATE,
4304                         p_gms_batch_name	    IN  VARCHAR2,
4305 			p_business_group_id  IN NUMBER,
4306 			p_set_of_books_id    IN NUMBER,
4307 			p_txn_source	     IN VARCHAR2,
4308 			p_mode 		     IN VARCHAR2,	--Introduced as part of Bug fix #1776606
4309                         p_return_status	   OUT NOCOPY  VARCHAR2) IS
4310    CURSOR gms_tie_back_success_cur IS
4311    SELECT summary_line_id,
4312           dr_cr_flag,summary_amount
4313    FROM   psp_summary_lines
4314    WHERE  gms_batch_name = p_gms_batch_name;
4315 
4316 
4317    CURSOR gms_tie_back_reject_cur IS
4318 --- SELECT nvl(transaction_rejection_code,'P'),  *****Bug 1866362***
4319    select transaction_rejection_code,
4320           orig_transaction_reference,
4321           transaction_status_code,
4322           expenditure_ending_date,   -- added following five columns for 2445196
4323           expenditure_id,
4324           interface_id,
4325           expenditure_item_id,
4326           txn_interface_id
4327    FROM   pa_transaction_interface_all
4328    WHERE  batch_name = p_gms_batch_name
4329      AND  transaction_source = p_txn_source;
4330 
4331    CURSOR assign_susp_ac_cur(P_SUMMARY_LINE_ID	IN	NUMBER) IS
4332    SELECT pdl.rowid,
4333 	  pdl.distribution_line_id line_id,
4334           pdl.distribution_date,
4335           pdl.suspense_org_account_id
4336    FROM   psp_distribution_lines pdl
4337    WHERE  pdl.summary_line_id = p_summary_line_id
4338    UNION
4339    SELECT ppgd.rowid,
4340 	  ppgd.pre_gen_dist_line_id line_id,
4341           ppgd.distribution_date,
4342           ppgd.suspense_org_account_id
4343    FROM   psp_pre_gen_dist_lines ppgd
4344    WHERE  ppgd.summary_line_id = p_summary_line_id;
4345 
4346    CURSOR get_susp_org_cur1 (P_ORG_ID	IN	VARCHAR2) IS
4347 	  SELECT hou.organization_id, hou.name
4348 	    FROM hr_all_organization_units hou, psp_organization_accounts poa
4349 	   WHERE hou.organization_id = poa.organization_id
4350 	     AND poa.business_group_id = p_business_group_id
4351 	     AND poa.set_of_books_id = p_set_of_books_id
4352 	     AND poa.organization_account_id = p_org_id;
4353 
4354    CURSOR get_org_id_cur1 (P_LINE_ID	IN	NUMBER) IS
4355 	  SELECT hou.organization_id, hou.name
4356 	    FROM hr_all_organization_units hou,
4357 		 per_assignments_f paf,
4358 		 psp_payroll_lines ppl,
4359 		 psp_payroll_sub_lines ppsl,
4360 		 psp_distribution_lines pdl
4361 	   WHERE paf.assignment_id = ppl.assignment_id
4362 	     AND hou.organization_id = paf.organization_id
4363 	     AND pdl.distribution_line_id = p_line_id
4364 	     AND ppsl.payroll_sub_line_id = pdl.payroll_sub_line_id
4365 	     AND ppl.payroll_line_id = ppsl.payroll_line_id
4366 	     AND pdl.distribution_date between paf.effective_start_date and paf.effective_end_date
4367 	 UNION
4368 	  SELECT hou.organization_id, hou.name
4369 	    FROM hr_all_organization_units hou,
4370 		 per_assignments_f paf,
4371 		 psp_pre_gen_dist_lines ppgdl
4372 	   WHERE paf.assignment_id = ppgdl.assignment_id
4373 	     AND hou.organization_id = paf.organization_id
4374 	     AND ppgdl.pre_gen_dist_line_id = p_line_id
4375 	     AND ppgdl.distribution_date between paf.effective_start_date and paf.effective_end_date;
4376 
4377 l_orig_org_name1	hr_all_organization_units_tl.name%TYPE;	-- Bug 2447912: Modified declaration
4378 l_orig_org_id1		number;
4379 
4380    CURSOR org_susp_ac_cur(P_ORGANIZATION_ID	IN	NUMBER,
4381                           P_DISTRIBUTION_DATE	IN	DATE) IS
4382    SELECT poa.organization_account_id,
4383           poa.gl_code_combination_id,
4384           poa.project_id,
4385           poa.award_id,
4386           poa.task_id,   -- Line added by pvelamur for bug fix 897553
4387           poa.expenditure_organization_id,
4388           poa.expenditure_type
4389    FROM   psp_organization_accounts poa
4390    WHERE  poa.organization_id = p_organization_id
4391    AND    poa.account_type_code = 'S'
4392    AND    poa.business_group_id = p_business_group_id
4393    AND    poa.set_of_books_id = p_set_of_books_id
4394    AND    p_distribution_date BETWEEN poa.start_date_active AND
4395                                       nvl(poa.end_date_active,p_distribution_date);
4396 
4397 
4398 -- CURSOR global_susp_ac_cur(P_DISTRIBUTION_DATE	IN	DATE) IS
4399    CURSOR global_susp_ac_cur(P_ORGANIZATION_ACCOUNT_ID	IN	NUMBER) IS --BUG 2056877.
4400    SELECT poa.organization_account_id,
4401           poa.gl_code_combination_id,
4402           poa.project_id,
4403           poa.award_id,
4404           poa.task_id,       -- Line added by pvelamur for bugfix 897553
4405           poa.expenditure_organization_id,
4406           poa.expenditure_type
4407    FROM   psp_organization_accounts poa
4408    WHERE
4409         /* poa.account_type_code = 'G'
4410    AND    poa.business_group_id = p_business_group_id
4411    AND    poa.set_of_books_id = p_set_of_books_id
4412    AND    p_distribution_date BETWEEN poa.start_date_active AND
4413                                       nvl(poa.end_date_active,p_distribution_date);Bug 2056877 */
4414           organization_account_id = p_organization_account_id;   --Added for bug 2056877.
4415 
4416    l_organization_name		hr_all_organization_units_tl.name%TYPE;	-- Bug 2447912: Modified declaration
4417    l_organization_id		NUMBER(15);
4418    l_rowid				ROWID;
4419    l_assignment_id		NUMBER(15);
4420    l_distribution_date		DATE;
4421    l_suspense_org_account_id  NUMBER(9);
4422    --
4423    l_organization_account_id	NUMBER(9);
4424    l_gl_code_combination_id   NUMBER(15);
4425    l_project_id			NUMBER(15);
4426    l_award_id			NUMBER(15);
4427    l_task_id                    NUMBER(15);
4428    --
4429    l_cnt_gms_interface		NUMBER;
4430    l_summary_line_id		NUMBER(10);
4431    l_gl_project_flag		VARCHAR2(1);
4432    l_suspense_ac_failed		VARCHAR2(1) := 'N';
4433    l_suspense_ac_not_found	VARCHAR2(1) := 'N';
4434    l_susp_ac_found		VARCHAR2(10) := 'TRUE';
4435    l_summary_amount		NUMBER;
4436    l_dr_summary_amount		NUMBER := 0;
4437    l_cr_summary_amount		NUMBER := 0;
4438    l_dr_cr_flag			VARCHAR2(1);
4439    --
4440    l_trx_status_code            VARCHAR2(2); /* Bug 1938458: Increased size from 1 to 2*/
4441    l_trx_reject_code		VARCHAR2(30);
4442    l_orig_trx_reference		VARCHAR2(30);
4443    l_effective_date		DATE;
4444 
4445    x_susp_failed_org_name	hr_all_organization_units_tl.name%TYPE;	-- Bug 2447912: Modified declaration
4446    x_susp_failed_reject_code	VARCHAR2(30);
4447    x_susp_failed_date		DATE;
4448    x_susp_nf_org_name		hr_all_organization_units_tl.name%TYPE;	-- Bug 2447912: Modified declaration
4449    x_susp_nf_date			DATE;
4450    l_return_status		VARCHAR2(10);
4451    x_line_id			NUMBER;
4452    l_no_run			NUMBER;
4453    l_trx_source			VARCHAR2(40);
4454    l_no_run_status		VARCHAR2(2);
4455    l_return_value               VARCHAR2(30);  --Added for bug 2056877.
4456    no_profile_exists            EXCEPTION;     --Added for bug 2056877.
4457    no_val_date_matches          EXCEPTION;     --Added for bug 2056877.
4458    no_global_acct_exists        EXCEPTION;     --Added for bug 2056877.
4459    l_expenditure_ending_date      date; -- added five variables for 2445196
4460    l_expenditure_id               number;
4461    l_interface_id                 number;
4462    l_expenditure_item_id          number;
4463    l_txn_interface_id             number;
4464    l_susp_exception               varchar2(50); -- 2479579
4465 
4466    l_expenditure_type           varchar2(100);  -- introduced vars for 5080403
4467    l_exp_org_id                 number;
4468    l_new_expenditure_type       varchar2(100);
4469    l_new_glccid                 number;
4470    l_acct_type                  varchar2(1);
4471    l_auto_pop_status            varchar2(100);
4472    l_auto_status                varchar2(100);
4473    l_person_id                  number;
4474    l_element_type_id            number;
4475    l_assignment_number          varchar2(100);
4476    l_element_type               varchar2(200);
4477    l_person_name                varchar2(300);
4478    l_account                    varchar2(1000);
4479    l_auto_org_name              hr_all_organization_units_tl.name%TYPE;
4480    l_pay_action_type           psp_payroll_lines.payroll_action_type%TYPE; -- Bug 7040943
4481 
4482  cursor get_element_type is
4483    select ppl.element_type_id,
4484           ppl.assignment_id,
4485           ppl.person_id
4486     from  psp_payroll_lines ppl,
4487           psp_payroll_sub_lines ppsl,
4488           psp_distribution_lines pdl
4489     where pdl.distribution_line_id = x_line_id
4490       and pdl.payroll_sub_line_id = ppsl.payroll_sub_line_id
4491       and ppsl.payroll_line_id = ppl.payroll_line_id
4492    union all
4493    select ppg.element_type_id,
4494           ppg.assignment_id,
4495           ppg.person_id
4496      from psp_pre_gen_dist_lines ppg
4497     where pre_gen_dist_line_id = x_line_id;
4498 
4499  cursor get_asg_details is
4500    select ppf.full_name,
4501           paf.assignment_number,
4502           pet.element_name,
4503           hou.name
4504      from per_all_people_f ppf,
4505           per_all_assignments_f paf,
4506           pay_element_types_f pet,
4507           hr_all_organization_units hou
4508     where ppf.person_id = l_person_id
4509       and l_distribution_date between ppf.effective_start_date and ppf.effective_end_date
4510       and paf.assignment_id = l_assignment_id
4511       and l_distribution_date between paf.effective_start_date and paf.effective_end_date
4512       and pet.element_type_id = l_element_type_id
4513       and l_distribution_date between pet.effective_start_date and pet.effective_end_date
4514       and hou.organization_id = paf.organization_id;
4515 
4516 
4517 -- Bug 4369939: Performance fix START
4518 TYPE t_varchar2_10_type IS TABLE OF varchar2(10)  INDEX BY BINARY_INTEGER;
4519 TYPE t_varchar2_30_type IS TABLE OF varchar2(30)  INDEX BY BINARY_INTEGER;
4520 TYPE t_number_type IS TABLE OF NUMBER  INDEX BY BINARY_INTEGER;
4521 type SUMMARY_LINES_TYPE  is record
4522   (
4523     L_SUMMARY_LINE_ID t_number_type,
4524     L_SUMMARY_LINE_ID_CHAR t_varchar2_30_type,
4525     L_GMS_BATCH_NAME t_varchar2_10_type
4526   );
4527 
4528 summary_lines_rec   SUMMARY_LINES_TYPE;
4529 
4530 Cursor SUMMARY_LINES_csr IS
4531 Select SUMMARY_LINE_ID, to_CHAR(SUMMARY_LINE_ID) , GMS_BATCH_NAME
4532 From PSP_SUMMARY_LINES PSL
4533 where PSL.GMS_BATCH_NAME = p_gms_batch_name;
4534 -- Bug 4369939: Performance fix END
4535 
4536 
4537  FUNCTION PROCESS_COMPLETE RETURN BOOLEAN IS
4538 
4539    cursor get_completion is
4540    select count(*), transaction_status_code
4541      from pa_transaction_interface_all
4542     where batch_name = p_gms_batch_name
4543       and transaction_source = p_txn_source
4544       and transaction_status_code in ('P', 'I')
4545     group by transaction_status_code  ;
4546 
4547   get_completion_rec	get_completion%ROWTYPE;
4548 
4549  begin
4550 
4551 		   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Entering process_complete');
4552 
4553    open get_completion;
4554    loop
4555    fetch get_completion into get_completion_rec;
4556 
4557    if get_completion%ROWCOUNT = 0 then
4558      close get_completion;
4559      return TRUE;
4560    elsif get_completion%NOTFOUND then
4561      close get_completion;
4562      return FALSE;
4563    end if;
4564 
4565              fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' get_completion_rec.transaction_status_code = P');
4566   if (get_completion_rec.transaction_status_code = 'P' AND  -- change
4567        g_skip_flag_gms = 'N') then -- bug 6902514
4568 
4569 -- -------------------------------------------------------------------------------------------
4570 -- If transaction_status_code = 'P' then the transaction import process did not kick off
4571 -- for some reason. Return 'NOT_RUN' in this case. So cleanup the tables and try to transfer
4572 -- again after summarization in the second pass.
4573 -- -------------------------------------------------------------------------------------------
4574 
4575      delete from pa_transaction_interface_all
4576       where batch_name = p_gms_batch_name
4577 	and transaction_source = p_txn_source;
4578 
4579             fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' deleted from pa_transaction_interface_all count:'||sql%rowcount);
4580 
4581 
4582      if p_txn_source = 'GOLD' then
4583        delete from gms_transaction_interface_all
4584         where batch_name = p_gms_batch_name
4585 	  and transaction_source = 'GOLD';
4586 
4587                    fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' deleted from gms_transaction_interface_all count:'||sql%rowcount);
4588 
4589      end if;
4590 
4591      delete from psp_summary_lines
4592       where gms_batch_name = to_number(p_gms_batch_name)
4593 	and payroll_control_id = p_payroll_control_id;
4594 					fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' deleted from psp_summary_lines count:'||sql%rowcount);
4595 
4596 
4597 
4598    elsif (get_completion_rec.transaction_status_code = 'I' AND  -- change
4599           g_skip_flag_gms = 'N') then -- Bug 6902514
4600 
4601 -- -------------------------------------------------------------------------------------------
4602 -- If transaction_status_code = 'I' then the transaction import process did not complete
4603 -- the Post Processing extension. So return 'NOT_COMPLETE' in this case. User needs to complete
4604 -- this process by running the transaction import manually and re-start the LD process.
4605 -- -------------------------------------------------------------------------------------------
4606 
4607      l_no_run_status := 'I';
4608 
4609      NULL; --- Return False here.
4610 
4611    end if;
4612 
4613    end loop;
4614 
4615  exception
4616  when others then
4617 			fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
4618 
4619    return FALSE;
4620  end PROCESS_COMPLETE;
4621 
4622  BEGIN
4623 
4624 hr_utility.trace('entered gms_tie_back');
4625 		   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' entered gms_tie_back');
4626 
4627 
4628      if NOT PROCESS_COMPLETE then
4629 	if (l_no_run_status = 'I') then
4630      	   fnd_message.set_name('PSP','PSP_PRC_DID_NOT_RUN');
4631      	   fnd_message.set_token('PAYROLL_CONTROL_ID',p_payroll_control_id);
4632      	   fnd_message.set_token('GMS_BATCH_NAME',p_gms_batch_name);
4633      	   fnd_msg_pub.add;
4634      	   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4635 	end if;
4636      end if;
4637 hr_utility.trace('entered gms_tie_back2');
4638 		   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' entered gms_tie_back2');
4639 
4640 --  Status codes have been added to transaction import process,
4641 --  to accomodate pre and post import extensions in 11i
4642 --  to following select statement
4643 
4644    SELECT count(*)
4645      INTO l_cnt_gms_interface
4646      FROM pa_transaction_interface_all
4647     WHERE batch_name = p_gms_batch_name
4648       AND transaction_source = p_txn_source
4649       AND transaction_status_code in ('R', 'PO', 'PI', 'PR');
4650 
4651 			   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || '  selected pa_transaction_interface_all with status_code in R,PO,PI,PR  l_cnt_gms_interface  :'||l_cnt_gms_interface);
4652 
4653      /* moved this statement to beginning for 2444657 */
4654      UPDATE 	psp_payroll_controls
4655         SET    	phase = 'GL_Tie_Back'
4656       WHERE	payroll_control_id = p_payroll_control_id;
4657 hr_utility.trace('entered gms_tie_back3');
4658 				 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' update psp_payroll_controls SQL%ROWCOUNT :'||SQL%ROWCOUNT);
4659 				fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' entered gms_tie_back3');
4660 
4661 
4662    IF l_cnt_gms_interface > 0 THEN
4663      --
4664 			   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Opening gms_tie_back_reject_cur ');
4665 
4666      OPEN gms_tie_back_reject_cur;
4667      LOOP
4668        FETCH gms_tie_back_reject_cur INTO l_trx_reject_code,l_orig_trx_reference,l_trx_status_code,
4669                                           l_expenditure_ending_date,l_expenditure_id,  -- added 5 vars for 2445196
4670                                           l_interface_id,l_expenditure_item_id, l_txn_interface_id;
4671 
4672 
4673 
4674        IF gms_tie_back_reject_cur%NOTFOUND THEN
4675          CLOSE gms_tie_back_reject_cur;
4676          EXIT;
4677        END IF;
4678 
4679        fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' l_trx_reject_code,l_orig_trx_reference(Summary line id),l_trx_status_code, l_expenditure_ending_date,l_expenditure_id,
4680       l_interface_id,l_expenditure_item_id, l_txn_interface_id'||l_trx_reject_code||','||l_orig_trx_reference||','||l_trx_status_code||','||
4681                              l_expenditure_ending_date||','||l_expenditure_id||','||l_interface_id||','||l_expenditure_item_id||','|| l_txn_interface_id);
4682         -- update summary_lines with the reject status code
4683        IF l_trx_status_code in ('R', 'PO', 'PI', 'PR') THEN
4684 				  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' status_code in R, PO,PI,PR');
4685 
4686           UPDATE psp_summary_lines
4687           SET interface_status = l_trx_reject_code, status_code = 'R',
4688               expenditure_ending_date = l_expenditure_ending_date,  -- added 5 fields for 2445196
4689               expenditure_id = l_expenditure_id, interface_id=l_interface_id,
4690               expenditure_item_id=l_expenditure_item_id, txn_interface_id=l_txn_interface_id
4691           WHERE summary_line_id = to_number(l_orig_trx_reference);
4692 				 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Updated psp_summary_lines for rejected records count: '||sql%rowcount);
4693 
4694        ELSIF l_trx_status_code = 'A' THEN
4695 			  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' status_code A');
4696 
4697           UPDATE psp_summary_lines
4698           SET interface_status = l_trx_reject_code, status_code = 'A',
4699               expenditure_ending_date = l_expenditure_ending_date,  -- added 5 fields for 2445196
4700               expenditure_id = l_expenditure_id, interface_id=l_interface_id,
4701               expenditure_item_id=l_expenditure_item_id, txn_interface_id=l_txn_interface_id
4702           WHERE summary_line_id = to_number(l_orig_trx_reference);
4703 
4704 				 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Updated psp_summary_lines for accepted records count: '||sql%rowcount);
4705 
4706        SELECT summary_amount,dr_cr_flag
4707        INTO l_summary_amount,l_dr_cr_flag
4708        FROM psp_summary_lines
4709        WHERE summary_line_id = to_number(l_orig_trx_reference) ;
4710                 hr_utility.trace('entered gms_tie_back4');
4711 				fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' entered gms_tie_back4');
4712 
4713 
4714          IF l_dr_cr_flag = 'D' THEN
4715            l_dr_summary_amount := l_dr_summary_amount + l_summary_amount;
4716          ELSIF l_dr_cr_flag = 'C' THEN
4717            -- credit is marked as -ve for posting to Oracle Projects
4718            l_cr_summary_amount := l_cr_summary_amount - l_summary_amount;
4719          END IF;
4720        END IF;
4721            hr_utility.trace('before open assign_susp_ac_cur');
4722              fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' before open assign_susp_ac_cur');
4723 
4724        OPEN assign_susp_ac_cur(l_orig_trx_reference);
4725        LOOP
4726 
4727          FETCH assign_susp_ac_cur INTO l_rowid, x_line_id, l_distribution_date, l_suspense_org_account_id;
4728 
4729          IF assign_susp_ac_cur%NOTFOUND THEN
4730            CLOSE assign_susp_ac_cur;
4731            EXIT;
4732          END IF;
4733 
4734                fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' From (Pre Gen) Distribution lines Summary_line_id :'||l_orig_trx_reference||
4735 				'row id :'||l_rowid||'distribution line id :'||l_distribution_date||'suspence_org_acc_id: '||l_suspense_org_account_id);
4736 
4737 
4738 	  -- Bug 9307730
4739 	 IF p_source_type IN('O', 'N') THEN
4740          /*Bug 7376898*/
4741 	   SELECT payroll_action_type, effective_date
4742 	   INTO l_pay_action_type, l_effective_date
4743 	   FROM psp_payroll_lines
4744 	   WHERE payroll_control_id = p_payroll_control_id
4745 	   and payroll_line_id = (select payroll_line_id from psp_payroll_sub_lines
4746 	                           where payroll_sub_line_id = (select payroll_sub_line_id
4747 	                                                        from psp_distribution_lines
4748 	                                                        where distribution_line_id = x_line_id));
4749 				  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' from psp_payroll_lines: l_pay_action_type, l_effective_date'||l_pay_action_type||l_effective_date);
4750 
4751 	 /*Bug 7376898 End*/
4752 	 END IF;
4753 
4754           hr_utility.trace('entered gms_tie_back5');
4755 				 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' entered gms_tie_back5');
4756 
4757          IF l_trx_status_code = 'A'  THEN
4758           IF p_source_type = 'O' OR p_source_type = 'N' THEN
4759 				  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Payroll type O or N');
4760 
4761            UPDATE psp_distribution_lines
4762             SET status_code = 'A'
4763             WHERE rowid = l_rowid;
4764 
4765 			   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Updated psp_distribution_lines set status_code A count: '||sql%rowcount);
4766 					fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Updated status_code in psp_distribution_lines to A for rowid :'||l_rowid);
4767 
4768             INSERT INTO psp_distribution_lines_history
4769              (distribution_line_id,payroll_sub_line_id,distribution_date,
4770           effective_date,distribution_amount,status_code,suspense_reason_code,
4771           effort_report_id,version_num,schedule_line_id,summary_line_id,
4772           default_org_account_id,suspense_org_account_id,
4773           element_account_id,org_schedule_id,user_defined_field,
4774           default_reason_code,reversal_entry_flag,gl_project_flag,
4775 	  auto_expenditure_type, business_group_id, set_of_books_id,
4776 	attribute_category,	attribute1,	attribute2,	attribute3,
4777 	attribute4,		attribute5,	attribute6,	attribute7,
4778 	attribute8,		attribute9,	attribute10,
4779          cap_excess_glccid, cap_excess_award_id, cap_excess_task_id,
4780         cap_excess_project_id,    cap_excess_exp_type, cap_excess_exp_org_id,
4781         funding_source_code, annual_salary_cap, cap_excess_dist_line_id,
4782           suspense_auto_exp_type, suspense_auto_glccid, adj_account_flag)
4783          SELECT distribution_line_id,payroll_sub_line_id,distribution_date,
4784           effective_date,distribution_amount,status_code,suspense_reason_code,
4785           effort_report_id,version_num,schedule_line_id,summary_line_id,
4786           default_org_account_id,suspense_org_account_id,
4787           element_account_id,org_schedule_id,user_defined_field,
4788           default_reason_code,reversal_entry_flag,gl_project_flag,
4789 	  auto_expenditure_type, business_group_id, set_of_books_id,
4790 	attribute_category,	attribute1,	attribute2,	attribute3,
4791 	attribute4,		attribute5,	attribute6,	attribute7,
4792 	attribute8,		attribute9,	attribute10,
4793          cap_excess_glccid, cap_excess_award_id, cap_excess_task_id,
4794         cap_excess_project_id,    cap_excess_exp_type, cap_excess_exp_org_id,
4795         funding_source_code, annual_salary_cap, cap_excess_dist_line_id,
4796           suspense_auto_exp_type, suspense_auto_glccid, adj_account_flag
4797          FROM psp_distribution_lines
4798          WHERE status_code = 'A'
4799          AND  summary_line_id = to_number(l_orig_trx_reference);
4800 
4801 					  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Inserting into psp_distribution_lines_history for summary_line_id: '||l_orig_trx_reference
4802 				||'number of rows SQL%ROWCOUNT: '||SQL%ROWCOUNT);
4803  				fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' after insert into dist lines');
4804 
4805  hr_utility.trace('after insert into dist lines');
4806          DELETE FROM psp_distribution_lines
4807          WHERE status_code = 'A'
4808          AND summary_line_id = to_number(l_orig_trx_reference);
4809 
4810 				fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Deleting from psp_distribution_lines for summary_line_id: '||l_orig_trx_reference
4811 					||'number of rows SQL%ROWCOUNT: '||SQL%ROWCOUNT);
4812 
4813          --moved  the two del stmnts above for 2445196
4814 
4815            ELSIF p_source_type = 'P' THEN
4816              UPDATE psp_pre_gen_dist_lines
4817               SET status_code = 'A'
4818               WHERE rowid = l_rowid;
4819 
4820 					fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Payroll type P');
4821 
4822 -- move the transferred records to psp_pre_gen_dist_lines_history
4823          INSERT INTO psp_pre_gen_dist_lines_history
4824          (pre_gen_dist_line_id,distribution_interface_id,person_id,assignment_id,
4825           element_type_id,distribution_date,effective_date,distribution_amount,
4826           dr_cr_flag,payroll_control_id,source_type,source_code,time_period_id,
4827           batch_name,status_code,set_of_books_id,
4828           gl_code_combination_id,project_id,expenditure_organization_id,
4829           expenditure_type,task_id,award_id,suspense_reason_code,
4830           effort_report_id,version_num,summary_line_id,suspense_org_account_id,
4831           user_defined_field,reversal_entry_flag, business_group_id,
4832 	attribute_category,	attribute1,	attribute2,	attribute3,
4833 	attribute4,		attribute5,	attribute6,	attribute7,
4834         attribute8,             attribute9,     attribute10,
4835           suspense_auto_exp_type, suspense_auto_glccid)
4836          SELECT pre_gen_dist_line_id,distribution_interface_id,person_id,assignment_id,
4837           element_type_id,distribution_date,effective_date,distribution_amount,
4838           dr_cr_flag,payroll_control_id,source_type,source_code,time_period_id,
4839           batch_name,status_code,set_of_books_id,
4840           gl_code_combination_id,project_id,expenditure_organization_id,
4841           expenditure_type,task_id,award_id,suspense_reason_code,
4842           effort_report_id,version_num,summary_line_id,suspense_org_account_id,
4843           user_defined_field,reversal_entry_flag, business_group_id,
4844 	attribute_category,	attribute1,	attribute2,	attribute3,
4845 	attribute4,		attribute5,	attribute6,	attribute7,
4846         attribute8,             attribute9,     attribute10,
4847           suspense_auto_exp_type, suspense_auto_glccid
4848          FROM psp_pre_gen_dist_lines
4849          WHERE status_code = 'A'
4850          AND summary_line_id = to_number(l_orig_trx_reference);
4851 
4852 				 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Inserting into psp_pre_gen_distribution_lines_history for summary_line_id: '||l_orig_trx_reference
4853 					||'number of rows SQL%ROWCOUNT: '||SQL%ROWCOUNT);
4854 				 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' after insert into pregen lines');
4855 
4856                   hr_utility.trace('after insert into pregen lines');
4857          DELETE FROM psp_pre_gen_dist_lines
4858          WHERE status_code = 'A'
4859          AND summary_line_id = to_number(l_orig_trx_reference);
4860 			   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Deleting from psp_pre_gen_distribution_lines for summary_line_id: '||l_orig_trx_reference
4861 					||'number of rows SQL%ROWCOUNT: '||SQL%ROWCOUNT);
4862 
4863         END IF;
4864          --- moved  the two del stmnts below for 2445196, earlier commented for 2290051
4865 
4866              /* Bug 1866362, S and T Failing with Fatal error Suspense A/C invalid, eventhough Suspense A/C is valid.
4867                Introduced reject_code not null condition, stick suspense only if this condition satisified */
4868          -- if a suspense a/c failed,update the status of the whole batch and display the error
4869          ELSIF l_trx_status_code <> 'A' and  l_trx_reject_code is not NULL then
4870               fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' l_trx_status_code <> A and l_trx_reject_code not null');
4871          -- introduced following IF by splitting from above ELSIF stmt: 2428953.
4872          If l_suspense_org_account_id is NOT NULL then
4873 				fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' suspence_account_id not null');
4874 
4875 	OPEN get_susp_org_cur1(l_suspense_org_account_id);
4876 	FETCH get_susp_org_cur1 into l_organization_id, l_organization_name;
4877 	CLOSE get_susp_org_cur1;
4878 
4879            x_susp_failed_org_name    := l_organization_name;
4880            x_susp_failed_reject_code := l_trx_reject_code;
4881            x_susp_failed_date        := l_distribution_date;
4882            l_suspense_ac_failed := 'Y';
4883 
4884          /*  Commented For Bug 3065866
4885 
4886 	 IF p_source_type = 'O' OR p_source_type = 'N' THEN
4887            UPDATE psp_distribution_lines
4888             SET suspense_reason_code = l_trx_reject_code,
4889                 status_code = 'N'
4890             WHERE rowid = l_rowid;
4891           ELSIF p_source_type = 'P' THEN
4892              UPDATE psp_pre_gen_dist_lines
4893                SET suspense_reason_code = l_trx_reject_code,
4894                    status_code = 'N'
4895                WHERE rowid = l_rowid;
4896           END IF;   Enf of commenting for BUg 3065866 */
4897 
4898          ELSE  -- suspense org is null and rejected xface rec
4899             l_susp_ac_found := 'TRUE';
4900 
4901 	   OPEN get_org_id_cur1(x_line_id);
4902 	   FETCH get_org_id_cur1 into l_orig_org_id1, l_orig_org_name1;
4903 	  --dbms_output.put_line('orig ord id is ' || l_orig_org_id1);
4904 	   IF get_org_id_cur1%NOTFOUND then
4905 	--	dbms_output.put_line('Didnot get any data');
4906            null;
4907 	   END IF;
4908 	   CLOSE get_org_id_cur1;
4909 
4910            --Bug 7040943 Starts
4911 	   IF l_pay_action_type = 'L' THEN
4912 		l_distribution_date := l_effective_date;
4913 	   END IF;
4914            --Bug 7040943 End
4915 					   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Opening org_susp_ac_cur');
4916 
4917            OPEN org_susp_ac_cur(l_orig_org_id1, l_distribution_date);
4918            FETCH org_susp_ac_cur INTO l_organization_account_id,l_gl_code_combination_id,l_project_id,l_award_id,l_task_id,
4919                                       l_exp_org_id, l_expenditure_type;
4920 
4921            IF org_susp_ac_cur%NOTFOUND  THEN
4922 					  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' org_susp_ac_cur not found for l_orig_org_id1, l_distribution_date'||l_orig_org_id1||','|| l_distribution_date);
4923 
4924              /* Following code is added for bug 2056877 ,Added validation for generic suspense account */
4925 		l_return_value := psp_general.find_global_suspense(l_distribution_date,
4926 							  p_business_group_id,
4927                                                           p_set_of_books_id,
4928                                                           l_organization_account_id);
4929       	  /* --------------------------------------------------------------------
4930       	   Valid return values are
4931       	   PROFILE_VAL_DATE_MATCHES       Profile and Value and Date matching 'G'
4932       	   NO_PROFILE_EXISTS              No Profile
4933        	   NO_VAL_DATE_MATCHES            Profile and Either Value/date do not
4934             		                  match with 'G'
4935    	   NO_GLOBAL_ACCT_EXISTS          No 'G' exists
4936      	    ---------------------------------------------------------------------- */
4937                IF  l_return_value = 'PROFILE_VAL_DATE_MATCHES' THEN
4938             --	   OPEN global_susp_ac_cur(l_distribution_date);
4939             	   OPEN global_susp_ac_cur(l_organization_account_id); -- Bug 2056877.
4940                    FETCH global_susp_ac_cur INTO l_organization_account_id,l_gl_code_combination_id,l_project_id,l_award_id,
4941                          l_task_id, l_exp_org_id, l_expenditure_type;
4942 
4943              IF   global_susp_ac_cur%NOTFOUND THEN
4944 						 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Global suspence account not found');
4945 
4946               	   /*  l_susp_ac_found := 'FALSE';
4947                        l_suspense_ac_not_found := 'Y';
4948                        x_susp_nf_org_name := l_organization_name;
4949                        x_susp_nf_org_name := l_orig_org_name1;
4950                        x_susp_nf_date     := l_distribution_date; */ --Commented for bug 2056877.
4951                        --- commented for 2479579      RAISE no_global_acct_exists; --Added for bug 2056877
4952                        -- added following lines for 2479579
4953                               l_susp_ac_found := 'NO_G_AC';
4954                               l_suspense_ac_not_found := 'Y';
4955                       END IF;
4956                       CLOSE global_susp_ac_cur;
4957               ELSIF l_return_value = 'NO_GLOBAL_ACCT_EXISTS' THEN
4958                      --- RAISE no_global_acct_exists; commented this line added following 2 lines for 2479579
4959                      l_suspense_ac_not_found := 'Y';
4960                      l_susp_ac_found := 'NO_G_AC';
4961               ELSIF l_return_value = 'NO_VAL_DATE_MATCHES' THEN
4962                     --- RAISE no_val_date_matches;  commented this line added following 2 lines for 2479579
4963                     l_suspense_ac_not_found := 'Y';
4964                     l_susp_ac_found := 'NO_DT_MCH';
4965               ELSIF l_return_value = 'NO_PROFILE_EXISTS' THEN
4966                     --- RAISE no_profile_exists;  commented this line added following 2 lines for 2479579
4967                      l_suspense_ac_not_found := 'Y';
4968                      l_susp_ac_found := 'NO_PROFL';
4969               END IF; -- Bug 2056877.
4970           END IF;
4971           CLOSE org_susp_ac_cur;
4972        -- introduced for 5080403
4973        if g_suspense_autopop = 'Y' and l_organization_account_id is not null then
4974 					 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' suspence autopop set and org_acc_id not null');
4975 
4976             if l_gl_code_combination_id is null then
4977                 l_acct_type:='E';
4978             else
4979                 l_acct_type:='N';
4980             end if;
4981             open get_element_type;
4982             fetch get_element_type into l_element_type_id, l_assignment_id, l_person_id;
4983             close get_element_type;
4984               psp_autopop.main( p_acct_type                   => l_acct_type,
4985                                 p_person_id                   => l_person_id,
4986                                 p_assignment_id               => l_assignment_id,
4987                                 p_element_type_id             => l_element_type_id,
4988                                 p_project_id                  => l_project_id,
4989                                 p_expenditure_organization_id => l_exp_org_id,
4990                                 p_task_id                     => l_task_id,
4991                                 p_award_id                    => l_award_id,
4992                                 p_expenditure_type            => l_expenditure_type,
4993                                 p_gl_code_combination_id      => l_gl_code_combination_id,
4994                                 p_payroll_date                => l_distribution_date,
4995                                 p_set_of_books_id             => p_set_of_books_id,
4996                                 p_business_group_id           => p_business_group_id,
4997                                 ret_expenditure_type          => l_new_expenditure_type,
4998                                 ret_gl_code_combination_id    => l_new_glccid,
4999                                 retcode                       => l_auto_pop_status);
5000            IF (l_auto_pop_status = FND_API.G_RET_STS_UNEXP_ERROR) OR
5001              (l_auto_pop_status = FND_API.G_RET_STS_ERROR) THEN
5002              IF l_auto_pop_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5003                if l_acct_type ='N'  then
5004                     l_auto_status := 'AUTO_POP_NA_ERROR';
5005                else
5006                     l_auto_status :='AUTO_POP_EXP_ERROR';
5007                end if;
5008              elsif l_auto_pop_status = FND_API.G_RET_STS_ERROR THEN
5009                l_auto_status := 'AUTO_POP_NO_VALUE';
5010              end if;
5011              open get_asg_details;
5012              fetch get_asg_details into l_person_name, l_assignment_number, l_element_type, l_auto_org_name;
5013 					fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' l_person_name, l_assignment_number, l_element_type, l_auto_org_name'||l_person_name||','|| l_assignment_number||','|| l_element_type||','|| l_auto_org_name);
5014 
5015              close get_asg_details;
5016              psp_enc_crt_xml.p_set_of_books_id := p_set_of_books_id;
5017              psp_enc_crt_xml.p_business_group_id := p_business_group_id;
5018              if l_acct_type = 'N' then
5019                  l_account :=
5020                      psp_enc_crt_xml.cf_charging_instformula(l_new_glccid,
5021                                                              null,
5022                                                              null,
5023                                                              null,
5024                                                              null,
5025                                                              null);
5026               else
5027                  l_account :=
5028                      psp_enc_crt_xml.cf_charging_instformula(null,
5029                                                              l_project_id,
5030                                                              l_task_id,
5031                                                              l_award_id,
5032                                                              l_new_expenditure_type,
5033                                                              l_exp_org_id);
5034               end if;
5035                    fnd_message.set_name('PSP','PSP_SUSPENSE_AUTOPOP_FAIL');
5036                    fnd_message.set_token('ORG_NAME',l_auto_org_name);
5037                    fnd_message.set_token('EMPLOYEE_NAME',l_person_name);
5038                    fnd_message.set_token('ASG_NUM',l_assignment_number);
5039                    fnd_message.set_token('CHARGING_ACCOUNT',l_account);
5040                    fnd_message.set_token('AUTOPOP_ERROR',l_auto_status);
5041                    fnd_message.set_token('EFF_DATE',l_distribution_date);
5042                    fnd_msg_pub.add;
5043          else
5044            if l_acct_type = 'E' then
5045               l_expenditure_type := l_new_expenditure_type;
5046            else
5047               l_gl_code_combination_id := l_new_glccid;
5048            end if;
5049            end if;
5050          end if;
5051 
5052           if l_susp_ac_found = 'TRUE' then
5053 					 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Suspence account found');
5054 
5055             IF l_gl_code_combination_id IS NOT NULL THEN
5056               l_gl_project_flag := 'G';
5057 				  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' p_period_end_date: '||p_period_end_date);
5058 
5059                -- l_effective_date := p_period_end_date; --- uncommented for Bug 2663344  COMMENTED by Bug 7040943
5060             ELSE
5061               l_gl_project_flag := 'P';
5062 					   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' l_distribution_date: '||l_distribution_date);
5063 
5064                l_effective_date := l_distribution_date;   --- added for Bug 2663344
5065             END IF;
5066 
5067 
5068             IF p_source_type = 'O' OR p_source_type = 'N' THEN
5069 
5070 	/* Added for Bug 3065866 */
5071 
5072 	     UPDATE psp_distribution_lines
5073 	     SET    pre_distribution_run_flag = gl_project_flag
5074 	     WHERE rowid = l_rowid;
5075 
5076        /* End of changes for Bug 3065866*/
5077                  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Update psp_distribution_lines for rowid: '||l_rowid||' with  l_effective_date'||l_effective_date);
5078              UPDATE psp_distribution_lines
5079               SET suspense_org_account_id = l_organization_account_id,
5080                   suspense_reason_code = 'ST:' || l_trx_reject_code,
5081                   gl_project_flag = l_gl_project_flag,
5082                   status_code = 'N',
5083                 --   effective_date = l_effective_date, ---uncommented this line for Bug 2663344  -- Commented for Bug 10181561
5084                   suspense_auto_glccid = l_new_glccid,    --- added suspense_auto for 5080403
5085                   suspense_auto_exp_type = l_new_expenditure_type
5086               WHERE rowid = l_rowid;
5087 					fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' updated psp_distribution_lines set pre_distribution_run_flag count: '||sql%rowcount);
5088 
5089             ELSIF p_source_type = 'P' THEN
5090                UPDATE psp_pre_gen_dist_lines
5091                  SET suspense_org_account_id = l_organization_account_id,
5092                      suspense_reason_code = 'ST:' || l_trx_reject_code,
5093                      status_code = 'N',
5094                   --      effective_date = l_effective_date, ---uncommented this line for Bug 2663344 -- Commented for Bug 10181561
5095                   suspense_auto_glccid = l_new_glccid,
5096                   suspense_auto_exp_type = l_new_expenditure_type
5097                  WHERE rowid = l_rowid;
5098 					fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Update psp_pre_gen_dist_lines for rowid: '||l_rowid||' with  l_effective_date'||l_effective_date||' count: '||sql%rowcount);
5099 
5100             END IF;
5101            else -- added for 2479579
5102                 l_susp_exception := l_susp_ac_found;
5103            end if;
5104           END IF;   --- for 2428953
5105          END IF;
5106 
5107        END LOOP;
5108      END LOOP;
5109  hr_utility.trace('update control record');
5110 			 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' update control record');
5111 
5112      UPDATE psp_payroll_controls
5113      SET ogm_dr_amount = nvl(ogm_dr_amount,0) + l_dr_summary_amount,
5114          ogm_cr_amount = nvl(ogm_cr_amount,0) + l_cr_summary_amount
5115      WHERE payroll_control_id = p_payroll_control_id;
5116 			  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' updated psp_payroll_controls count: '||sql%rowcount);
5117 
5118 
5119 --	Introduced for bug fix 2643228
5120 	DELETE	pa_transaction_interface_all
5121 	WHERE	transaction_source = p_txn_source
5122 	AND	batch_name = p_gms_batch_name;
5123 		   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' deleted from pa_transaction_interface_all count: '||sql%rowcount);
5124 
5125 
5126 	DELETE	gms_transaction_interface_all
5127 	WHERE	transaction_source = p_txn_source
5128 	AND	batch_name = p_gms_batch_name;
5129 			  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' deleted from gms_transaction_interface_all count: '||sql%rowcount);
5130 
5131 --	End of bug fix 2643228
5132      IF l_suspense_ac_failed = 'Y' or
5133         nvl(l_auto_status,'X') in ('AUTO_POP_NA_ERROR', 'AUTO_POP_EXP_ERROR', 'AUTO_POP_NO_VALUE') then
5134                    --- above check for autopop error 5080403
5135        if nvl(l_suspense_ac_failed,'N') = 'Y' then
5136        fnd_message.set_name('PSP','PSP_TR_GMS_SUSP_AC_REJECT');
5137        fnd_message.set_token('ORG_NAME',x_susp_failed_org_name);
5138        fnd_message.set_token('PAYROLL_DATE',x_susp_failed_date);
5139        fnd_message.set_token('ERROR_MSG',x_susp_failed_reject_code);
5140        fnd_msg_pub.add;
5141        end if;
5142 
5143 	/* Added this code for Bug 3065866 */
5144 		IF p_source_type = 'O' OR p_source_type = 'N' THEN
5145 
5146 
5147  --			fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' updating psp_distribution_lines for p_payroll_control_id: '||p_payroll_control_id);
5148 
5149 		 	UPDATE psp_distribution_lines
5150 		 	SET	suspense_org_account_id = NULL,
5151 				suspense_reason_code = NULL,
5152 				gl_project_flag = pre_distribution_run_flag,
5153                         	effective_date = decode(pre_distribution_run_flag,'G',
5154                                                 p_period_end_date,distribution_date)
5155 			WHERE	suspense_reason_code like 'ST:%'
5156 			AND	summary_line_id
5157 				IN ( SELECT 	summary_line_id
5158 				     FROM	psp_summary_lines
5159 				     WHERE	payroll_control_id = p_payroll_control_id);
5160 				 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' updated psp_distribution_lines count: '||sql%rowcount);
5161 
5162 		ELSIF p_source_type = 'P' THEN
5163 		  --	fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' updating psp_pre_gen_distribution_lines for p_payroll_control_id: '||p_payroll_control_id);
5164 
5165 			UPDATE 	psp_pre_gen_dist_lines
5166 			SET	suspense_org_account_id = NULL,
5167 				suspense_reason_code = NULL,
5168 				effective_date = decode(NVL(gl_code_combination_id,-999),gl_code_combination_id,
5169                                         p_period_end_date,distribution_date)
5170 			WHERE	suspense_reason_code like 'ST:%'
5171 			AND	summary_line_id
5172 				IN (SELECT	summary_line_id
5173 				    FROM	psp_summary_lines
5174 			   	    WHERE	payroll_control_id = p_payroll_control_id);
5175 			   			fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' updated psp_pre_gen_dist_lines count: '||sql%rowcount);
5176 
5177 
5178 		END IF;
5179 	/* End of code for Bug 3065866 */
5180 
5181 	/* Introduced the followinbg check as part of Bug fix #1776606 */
5182 	if p_mode = 'N' then
5183           /* introduced mark batch end Bug: 1929317 */
5184 			   	fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Calling mark_batch_end ');
5185 
5186           mark_batch_end(p_source_type,
5187                    g_source_code,
5188                    g_time_period_id,
5189                    g_batch_name,
5190                    p_business_group_id,
5191                    p_set_of_books_id,
5192                    l_return_status);
5193        	    RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5194 	end if;
5195      END IF;
5196 
5197      IF l_suspense_ac_not_found = 'Y' THEN
5198 			 	fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Suspence account not found');
5199 
5200        /* commented for 2479579
5201         fnd_message.set_name('PSP','PSP_LD_SUSPENSE_AC_NOT_EXIST');
5202         fnd_message.set_token('ORG_NAME',x_susp_nf_org_name);
5203         fnd_message.set_token('PAYROLL_DATE',x_susp_nf_date);
5204         fnd_msg_pub.add; */
5205 
5206 	/* Added the following code for Bug 3065866 */
5207 
5208 		IF p_source_type = 'O' OR p_source_type = 'N' THEN
5209 				fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' p_source_type O or N');
5210 
5211 			UPDATE	psp_distribution_lines
5212 			SET	suspense_org_account_id = NULL,
5213 				suspense_reason_code = NULL
5214 			WHERE	suspense_reason_code like 'ST:%'
5215 			AND	summary_line_id
5216 				IN( SELECT	summary_line_id
5217 				    FROM	psp_summary_lines
5218 				    WHERE	payroll_control_id = p_payroll_control_id);
5219 			  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' updating psp_distribution_lines for p_payroll_control_id: '||p_payroll_control_id);
5220 
5221 		ELSIF p_source_type = 'P' THEN
5222 
5223                   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' p_source_type P');
5224 
5225 			UPDATE	psp_pre_gen_dist_lines
5226 			SET	suspense_org_account_id = NULL,
5227 				suspense_reason_code = NULL
5228 			WHERE	suspense_reason_code like 'ST:%'
5229                         AND     summary_line_id
5230                                 IN( SELECT      summary_line_id
5231                                     FROM        psp_summary_lines
5232                                     WHERE       payroll_control_id = p_payroll_control_id);
5233 		END IF;
5234 	/* End of Bug 3065866 */
5235 
5236 
5237 	/* Introduced the followinbg check as part of Bug fix #1776606 */
5238 	if p_mode = 'N' then
5239           /* introduced mark batch end Bug: 1929317 */
5240 			   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' calling mark_batch_end');
5241 
5242              mark_batch_end(p_source_type,
5243                    g_source_code,
5244                    g_time_period_id,
5245                    g_batch_name,
5246                    p_business_group_id,
5247                    p_set_of_books_id,
5248                    l_return_status);
5249            --- RAISE FND_API.G_EXC_UNEXPECTED_ERROR; commented for  2479579
5250           -- introduced following if stmnt  for  2479579
5251           if l_susp_exception = 'NO_G_AC' then
5252                      RAISE no_global_acct_exists;
5253           elsif  l_susp_exception = 'NO_DT_MCH' then
5254                      RAISE no_val_date_matches;
5255           elsif l_susp_exception =  'NO_PROFL' then
5256                      RAISE no_profile_exists;
5257           end if;
5258 
5259 	end if;
5260      END IF;
5261 
5262    ELSIF l_cnt_gms_interface = 0  THEN
5263 
5264 -- Bug 4369939: Performance fix START
5265 			   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Opening summary_lines_csr');
5266 
5267 	OPEN SUMMARY_LINES_CSR;
5268 	FETCH SUMMARY_LINES_CSR bulk Collect into SUMMARY_LINES_REC.L_SUMMARY_LINE_ID,
5269 	SUMMARY_LINES_rec.L_SUMMARY_LINE_ID_CHAR, SUMMARY_LINES_REC.L_GMS_BATCH_NAME;
5270 	CLOSE SUMMARY_LINES_CSR;
5271 
5272 	FOR i in 1.. SUMMARY_LINES_REC.L_SUMMARY_LINE_ID.count
5273 LOOP
5274 	UPDATE PSP_SUMMARY_LINES PSL
5275 	Set (PSL.STATUS_CODE, PSL.EXPENDITURE_ENDING_DATE,PSL.EXPENDITURE_ID,
5276 	   PSL.INTERFACE_ID,PSL.EXPENDITURE_ITEM_ID,PSL.TXN_INTERFACE_ID) =
5277 	   ( SELECT 'A', PTXN.EXPENDITURE_ENDING_DATE,PTXN.EXPENDITURE_ID, PTXN.INTERFACE_ID,
5278 		PTXN.EXPENDITURE_ITEM_ID,PTXN.TXN_INTERFACE_ID
5279 		FROM PA_TRANSACTION_INTERFACE_ALL PTXN
5280 		WHERE PTXN.TRANSACTION_SOURCE = p_txn_source
5281 		AND PTXN.ORIG_TRANSACTION_REFERENCE= SUMMARY_LINES_REC.L_SUMMARY_LINE_ID_CHAR(i)
5282 		AND PTXN.BATCH_NAME = SUMMARY_LINES_REC.L_GMS_BATCH_NAME(i)
5283 	   )
5284 	  WHERE --GMS_BATCH_NAME = SUMMARY_LINES_REC.L_GMS_BATCH_NAME(i)  AND
5285 		 PSL.SUMMARY_LINE_ID = SUMMARY_LINES_REC.L_SUMMARY_LINE_ID(i);
5286 
5287 	  				fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Updated psp_summary_lines table SQL%ROWCOUNT: '||SQL%ROWCOUNT);
5288 END LOOP;
5289 
5290 /*
5291       -- changed to update all sum lines at one shot for 2445196
5292        UPDATE psp_summary_lines  PSL
5293        SET (PSL.status_code, PSL.expenditure_ending_date,PSL.expenditure_id,
5294                PSL.interface_id,PSL.expenditure_item_id,PSL.txn_interface_id)  =
5295             (select 'A', PTXN.expenditure_ending_date,PTXN.expenditure_id,
5296                PTXN.interface_id,PTXN.expenditure_item_id,PTXN.txn_interface_id
5297              from pa_transaction_interface_all PTXN
5298              where PTXN.transaction_source = p_txn_source
5299                and PTXN.orig_transaction_reference= to_char(PSL.summary_line_id)
5300                and PTXN.batch_name = p_gms_batch_name)
5301        WHERE gms_batch_name = p_gms_batch_name;
5302 */
5303 -- Bug 4369939: Performance fix END
5304 
5305 		 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Opening gms_tie_back_success_cur ');
5306 
5307      OPEN gms_tie_back_success_cur;
5308      LOOP
5309        FETCH gms_tie_back_success_cur INTO l_summary_line_id,
5310         l_dr_cr_flag,l_summary_amount;
5311 
5312        IF gms_tie_back_success_cur%NOTFOUND THEN
5313          CLOSE gms_tie_back_success_cur;
5314          EXIT;
5315        END IF;
5316 					fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Summary line id: '||l_summary_line_id);
5317       -- update records in psp_summary_lines as 'A' moved this stmnt from here to above for 2445196.
5318 
5319        IF l_dr_cr_flag = 'D' THEN
5320          l_dr_summary_amount := l_dr_summary_amount + l_summary_amount;
5321        ELSIF l_dr_cr_flag = 'C' THEN
5322          -- credit is marked as -ve for posting to Oracle Projects
5323          l_cr_summary_amount := l_cr_summary_amount - l_summary_amount;
5324        END IF;
5325 
5326        IF p_source_type = 'O' OR p_source_type = 'N' THEN
5327 
5328 			 			fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Update psp_distribution_lines for summary_line_id'||l_summary_line_id);
5329 
5330          UPDATE psp_distribution_lines
5331          SET status_code = 'A' WHERE summary_line_id = l_summary_line_id;
5332 				 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' updated psp_distribution_lines set status_code to A count: '||sql%rowcount);
5333 
5334 
5335          -- move the transferred records to psp_distribution_lines_history
5336          INSERT INTO psp_distribution_lines_history
5337          (distribution_line_id,payroll_sub_line_id,distribution_date,
5338           effective_date,distribution_amount,status_code,suspense_reason_code,
5339           effort_report_id,version_num,schedule_line_id,summary_line_id,
5340           default_org_account_id,suspense_org_account_id,
5341           element_account_id,org_schedule_id,user_defined_field,
5342           default_reason_code,reversal_entry_flag,gl_project_flag,
5343 	  auto_expenditure_type, business_group_id, set_of_books_id,
5344         attribute_category,     attribute1,     attribute2,     attribute3,
5345         attribute4,             attribute5,     attribute6,     attribute7,
5346         attribute8,             attribute9,     attribute10,
5347          cap_excess_glccid, cap_excess_award_id, cap_excess_task_id,
5348         cap_excess_project_id,    cap_excess_exp_type, cap_excess_exp_org_id,
5349         funding_source_code, annual_salary_cap, cap_excess_dist_line_id,
5350         suspense_auto_exp_type, suspense_auto_glccid, adj_account_flag)
5351          SELECT distribution_line_id,payroll_sub_line_id,distribution_date,
5352           effective_date,distribution_amount,status_code,suspense_reason_code,
5353           effort_report_id,version_num,schedule_line_id,summary_line_id,
5354           default_org_account_id,suspense_org_account_id,
5355           element_account_id,org_schedule_id,user_defined_field,
5356           default_reason_code,reversal_entry_flag,gl_project_flag,
5357 	  auto_expenditure_type, business_group_id, set_of_books_id,
5358         attribute_category,     attribute1,     attribute2,     attribute3,
5359         attribute4,             attribute5,     attribute6,     attribute7,
5360         attribute8,             attribute9,     attribute10,
5361          cap_excess_glccid, cap_excess_award_id, cap_excess_task_id,
5362         cap_excess_project_id,    cap_excess_exp_type, cap_excess_exp_org_id,
5363         funding_source_code, annual_salary_cap, cap_excess_dist_line_id,
5364           suspense_auto_exp_type, suspense_auto_glccid, adj_account_flag
5365          FROM psp_distribution_lines
5366          WHERE status_code = 'A'
5367          AND  summary_line_id = l_summary_line_id;
5368 
5369 				  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Inserting into psp_distribution_lines_history for summary_line_id: '||l_orig_trx_reference
5370 				||'number of rows SQL%ROWCOUNT: '||SQL%ROWCOUNT);
5371 
5372          DELETE FROM psp_distribution_lines
5373          WHERE status_code = 'A'
5374          AND summary_line_id = l_summary_line_id;
5375 			   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Deleting from psp_distribution_lines for summary_line_id: '||l_orig_trx_reference
5376 				||'number of rows SQL%ROWCOUNT: '||SQL%ROWCOUNT);
5377 
5378          -- Moved the purging of xface lines from here below LOOP, for Bug 2445196
5379        ELSIF p_source_type = 'P' THEN
5380 
5381 				   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Payroll type P');
5382 
5383          UPDATE psp_pre_gen_dist_lines
5384          SET status_code = 'A' WHERE summary_line_id = l_summary_line_id;
5385 
5386 				   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Update psp_pre_gen_distribution_lines for summary_line_id'||l_summary_line_id);
5387 
5388          -- move the transferred records to psp_pre_gen_dist_lines_history
5389          INSERT INTO psp_pre_gen_dist_lines_history
5390          (pre_gen_dist_line_id,distribution_interface_id,person_id,assignment_id,
5391           element_type_id,distribution_date,effective_date,distribution_amount,
5392           dr_cr_flag,payroll_control_id,source_type,source_code,time_period_id,
5393           batch_name,status_code,set_of_books_id,
5394           gl_code_combination_id,project_id,expenditure_organization_id,
5395           expenditure_type,task_id,award_id,suspense_reason_code,
5396           effort_report_id,version_num,summary_line_id,suspense_org_account_id,
5397           user_defined_field,reversal_entry_flag, business_group_id,
5398         attribute_category,     attribute1,     attribute2,     attribute3,
5399         attribute4,             attribute5,     attribute6,     attribute7,
5400         attribute8,             attribute9,     attribute10,
5401          suspense_auto_exp_type, suspense_auto_glccid)
5402          SELECT pre_gen_dist_line_id,distribution_interface_id,person_id,assignment_id,
5403           element_type_id,distribution_date,effective_date,distribution_amount,
5404           dr_cr_flag,payroll_control_id,source_type,source_code,time_period_id,
5405           batch_name,status_code,set_of_books_id,
5406           gl_code_combination_id,project_id,expenditure_organization_id,
5407           expenditure_type,task_id,award_id,suspense_reason_code,
5408           effort_report_id,version_num,summary_line_id,suspense_org_account_id,
5409           user_defined_field,reversal_entry_flag, business_group_id,
5410         attribute_category,     attribute1,     attribute2,     attribute3,
5411         attribute4,             attribute5,     attribute6,     attribute7,
5412         attribute8,             attribute9,     attribute10,
5413          suspense_auto_exp_type, suspense_auto_glccid
5414          FROM psp_pre_gen_dist_lines
5415          WHERE status_code = 'A'
5416          AND summary_line_id = l_summary_line_id;
5417 
5418 			   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Inserting into psp_pre_gen_distribution_lines_history for summary_line_id: '||l_orig_trx_reference
5419 				||'number of rows SQL%ROWCOUNT: '||SQL%ROWCOUNT);
5420 
5421          DELETE FROM psp_pre_gen_dist_lines
5422          WHERE status_code = 'A'
5423          AND summary_line_id = l_summary_line_id;
5424 				  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' Deleting from psp_distribution_lines for summary_line_id: '||l_orig_trx_reference
5425 				||'number of rows SQL%ROWCOUNT: '||SQL%ROWCOUNT);
5426 
5427          --Moved delete xface stmnts from here to below for bug 2445196:
5428        END IF;
5429      END LOOP;
5430 
5431      UPDATE psp_payroll_controls
5432      SET ogm_dr_amount = nvl(ogm_dr_amount,0) + l_dr_summary_amount,
5433          ogm_cr_amount = nvl(ogm_cr_amount,0) + l_cr_summary_amount
5434      WHERE payroll_control_id = p_payroll_control_id;
5435 				   fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' updated psp_payroll_controls count: '||sql%rowcount);
5436 
5437 
5438 --	Introduced for bug fix 2643228
5439 	DELETE	pa_transaction_interface_all
5440 	WHERE	transaction_source = p_txn_source
5441 	AND	batch_name = p_gms_batch_name;
5442 				  fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' delete from pa_transaction_interface_all for batch_name: '||p_gms_batch_name);
5443 
5444 
5445 	DELETE	gms_transaction_interface_all
5446 	WHERE	transaction_source = p_txn_source
5447 	AND	batch_name = p_gms_batch_name;
5448 
5449 		fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' delete from gms_transaction_interface_all for batch_name: '||p_gms_batch_name);
5450 
5451 --	End of bug fix 2643228
5452    END IF;
5453 /*****	Commented for bug fix 2643228
5454     -- introduced for 2445196
5455     delete pa_transaction_interface_all
5456      where transaction_source = p_txn_source
5457        and batch_name = p_gms_batch_name;
5458 
5459      delete gms_transaction_interface_all
5460      where transaction_source = p_txn_source
5461        and batch_name = p_gms_batch_name;
5462 	End of bug fix 2643228	*****/
5463    p_return_status := fnd_api.g_ret_sts_success;
5464  EXCEPTION
5465    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5466      --dbms_output.put_line('Gone to one level top ..................');
5467      g_error_api_path := 'GMS_TIE_BACK:'||g_error_api_path;
5468      p_return_status := fnd_api.g_ret_sts_unexp_error;
5469    /* Added Exceptions for bug 2056877 */
5470    WHEN NO_PROFILE_EXISTS THEN
5471       g_error_api_path := SUBSTR('GMS_TIE_BACK:'||g_error_api_path,1,230);
5472       fnd_message.set_name('PSP','PSP_NO_PROFILE_EXISTS');
5473       fnd_msg_pub.add;
5474       p_return_status := fnd_api.g_ret_sts_unexp_error;
5475 
5476    WHEN NO_VAL_DATE_MATCHES THEN
5477       g_error_api_path := SUBSTR('GMS_TIE_BACK:'||g_error_api_path,1,230);
5478       fnd_message.set_name('PSP','PSP_NO_VAL_DATE_MATCHES');
5479       fnd_message.set_token('ORG_NAME',l_orig_org_name1);
5480       fnd_message.set_token('PAYROLL_DATE',l_distribution_date);
5481       fnd_msg_pub.add;
5482       p_return_status := fnd_api.g_ret_sts_unexp_error;
5483 
5484    WHEN NO_GLOBAL_ACCT_EXISTS THEN
5485       g_error_api_path := SUBSTR('GMS_TIE_BACK:'||g_error_api_path,1,230);
5486       fnd_message.set_name('PSP','PSP_NO_GLOBAL_ACCT_EXISTS');
5487       fnd_message.set_token('ORG_NAME',l_orig_org_name1);
5488       fnd_message.set_token('PAYROLL_DATE',l_distribution_date);
5489       fnd_msg_pub.add;
5490       p_return_status := fnd_api.g_ret_sts_unexp_error;  --End of Modification for Bug 2056877.
5491 
5492    WHEN OTHERS THEN
5493                 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
5494       g_error_api_path := 'GMS_TIE_BACK:'||g_error_api_path;
5495       fnd_msg_pub.add_exc_msg('PSP_SUM_TRANS','GMS_TIE_BACK');
5496       p_return_status := fnd_api.g_ret_sts_unexp_error;
5497  END;
5498 
5499 ------------------ INSERT INTO GMS INTERFACE -----------------------------------------------
5500 
5501  PROCEDURE insert_into_pa_interface(
5502 	P_TRANSACTION_INTERFACE_ID	IN	NUMBER,
5503 	P_TRANSACTION_SOURCE		IN	VARCHAR2,
5504 	P_BATCH_NAME			IN	VARCHAR2,
5505 	P_EXPENDITURE_ENDING_DATE	IN	DATE,
5506 	P_EMPLOYEE_NUMBER			IN	VARCHAR2,
5507 	P_ORGANIZATION_NAME		IN	VARCHAR2,
5508 	P_EXPENDITURE_ITEM_DATE		IN	DATE,
5509 	P_PROJECT_NUMBER			IN	VARCHAR2,
5510 	--P_TASK_NUMBER			IN	VARCHAR2, 			-- Commented for Bug 16591849
5511 	P_TASK_ID				IN  NUMBER,				-- Added for Bug 16591849
5512 	P_EXPENDITURE_TYPE		IN	VARCHAR2,
5513 	P_QUANTITY				IN	NUMBER,
5514 	P_RAW_COST				IN	NUMBER,
5515 	P_EXPENDITURE_COMMENT		IN	VARCHAR2,
5516 	P_TRANSACTION_STATUS_CODE	IN	VARCHAR2,
5517 	P_ORIG_TRANSACTION_REFERENCE	IN	VARCHAR2,
5518 	P_ORG_ID			IN	NUMBER,
5519 	P_DENOM_CURRENCY_CODE		IN	VARCHAR2,
5520 	P_DENOM_RAW_COST		IN	NUMBER,
5521 	P_ATTRIBUTE1			IN	VARCHAR2,
5522 	P_ATTRIBUTE2			IN	VARCHAR2,
5523 	P_ATTRIBUTE3			IN	VARCHAR2,
5524 	P_ATTRIBUTE4			IN	VARCHAR2,		-- Introduced attributes 4 and 5 for bug fix 2908859
5525 	P_ATTRIBUTE5			IN	VARCHAR2,
5526 	P_ATTRIBUTE6			IN	VARCHAR2,
5527 	P_ATTRIBUTE7			IN	VARCHAR2,
5528 	P_ATTRIBUTE8			IN	VARCHAR2,
5529 	P_ATTRIBUTE9			IN	VARCHAR2,
5530 	P_ATTRIBUTE10			IN	VARCHAR2,
5531 	P_ACCT_RATE_TYPE                IN      VARCHAR2,       -- Introduced for bug fix 2916848
5532         P_ACCT_RATE_DATE                IN      DATE,           -- Introduced for bug fix 2916848
5533 	P_PERSON_BUSINESS_GROUP_ID	IN	NUMBER,		-- Introduced for bug 2935850
5534 	P_RETURN_STATUS			OUT NOCOPY	VARCHAR2) IS
5535 l_unmatched_nve_txn_flag	char(1);  -- Bug 8984069
5536  BEGIN
5537 
5538 -- Bug 8984069
5539 	IF (p_quantity < 0) THEN
5540 		l_unmatched_nve_txn_flag := 'Y';
5541 	END IF;
5542 
5543 --   dbms_output.put_line('batch name='||p_batch_name);
5544 
5545    INSERT INTO PA_TRANSACTION_INTERFACE_ALL(
5546 	TXN_INTERFACE_ID,
5547 	TRANSACTION_SOURCE,
5548 	BATCH_NAME,
5549 	EXPENDITURE_ENDING_DATE,
5550 	EMPLOYEE_NUMBER,
5551 	ORGANIZATION_NAME,
5552 	EXPENDITURE_ITEM_DATE,
5553 	PROJECT_NUMBER,
5554 	--TASK_NUMBER,		-- Commented for Bug 16591849
5555 	TASK_ID,			-- Added for Bug 16591849
5556 	EXPENDITURE_TYPE,
5557 	QUANTITY,
5558 	RAW_COST,
5559 	EXPENDITURE_COMMENT,
5560 	TRANSACTION_STATUS_CODE,
5561 	ORIG_TRANSACTION_REFERENCE,
5562 	ORG_ID,
5563 	DENOM_CURRENCY_CODE,
5564 	DENOM_RAW_COST,
5565 	ATTRIBUTE1,
5566 	ATTRIBUTE2,
5567 	ATTRIBUTE3,
5568 	ATTRIBUTE4,		-- Introduced attributes 4 and 5 for bug fix 2908859
5569 	ATTRIBUTE5,
5570 	ATTRIBUTE6,
5571 	ATTRIBUTE7,
5572 	ATTRIBUTE8,
5573 	ATTRIBUTE9,
5574 	ATTRIBUTE10,
5575 	PERSON_BUSINESS_GROUP_ID,
5576 	--  Introduced the following columns for bug fix 2916848
5577         ACCT_RATE_TYPE,
5578         ACCT_RATE_DATE,
5579         -- Bug 8984069
5580         UNMATCHED_NEGATIVE_TXN_FLAG)
5581    VALUES(
5582 	P_TRANSACTION_INTERFACE_ID,
5583 	P_TRANSACTION_SOURCE,
5584 	P_BATCH_NAME,
5585 	P_EXPENDITURE_ENDING_DATE,
5586 	P_EMPLOYEE_NUMBER,
5587 	P_ORGANIZATION_NAME,
5588 	P_EXPENDITURE_ITEM_DATE,
5589 	P_PROJECT_NUMBER,
5590 	--P_TASK_NUMBER,	-- Commented For Bug 16591849
5591 	P_TASK_ID,			-- Added For Bug 16591849
5592 	P_EXPENDITURE_TYPE,
5593 	P_QUANTITY,
5594 	P_RAW_COST,
5595 	P_EXPENDITURE_COMMENT,
5596 	P_TRANSACTION_STATUS_CODE,
5597 	P_ORIG_TRANSACTION_REFERENCE,
5598 	P_ORG_ID,
5599 	P_DENOM_CURRENCY_CODE,
5600 	P_DENOM_RAW_COST,
5601 	P_ATTRIBUTE1,
5602 	P_ATTRIBUTE2,
5603 	P_ATTRIBUTE3,
5604 	P_ATTRIBUTE4,		-- Introduced attributes 4 and 5 for bug fix 2908859
5605 	P_ATTRIBUTE5,
5606 	P_ATTRIBUTE6,
5607 	P_ATTRIBUTE7,
5608 	P_ATTRIBUTE8,
5609 	P_ATTRIBUTE9,
5610 	P_ATTRIBUTE10,
5611 	P_PERSON_BUSINESS_GROUP_ID,
5612 --      Introduced the following columns for bug fix 2916848
5613         P_ACCT_RATE_TYPE,
5614         DECODE(p_acct_rate_type, NULL, NULL, P_ACCT_RATE_DATE),
5615         -- Bug 8984069
5616         l_unmatched_nve_txn_flag);
5617 
5618     p_return_status := fnd_api.g_ret_sts_success;
5619  EXCEPTION
5620 
5621    WHEN OTHERS THEN
5622              fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
5623   --   dbms_output.put_line('Error while inserting .........................');
5624       g_error_api_path := 'INSERT_INTO_PA_INTERFACE:'||g_error_api_path;
5625       fnd_msg_pub.add_exc_msg('PSP_SUM_TRANS','INSERT_INTO_PA_INTERFACE');
5626       p_return_status := fnd_api.g_ret_sts_unexp_error;
5627  END;
5628 
5629 /* Created this procedure on 4-Nov-2004.
5630  Following procedure is introduced for Effort reports Enhancement.
5631  Purpose of this procedure: Supercede employee effort report(s) if this
5632  S_AND_T processes distributions for  persons/time periods
5633  for which effort reports already exist.
5634 
5635  There is no COMMIT statement in this procedure, it will be called
5636  from mark_batch_end, and the calling procedure will COMMIT the data.
5637 
5638  The volume of ERs superceded is expected to be low, normally
5639  customer generates ER only when all transactions are imported,
5640  and hence employees superceded are low.
5641 
5642   Logic:
5643     - get all the ER requests that can potentially be superceded,
5644       by comparing the time periods of this process with ER periods.
5645     - get all persons for this ER request, for whom this S_AND_T has
5646       distributions
5647     - if no tolerance is setup, then all emps processed by S_and_T
5648       are superceded.
5649     - If tolerance is set as zero, then supercede all those persons
5650       whose new ER changed w.r.t to old (compare old and Fresh ER)
5651     - If tolerance is non-zero, but specified as either % or
5652       absolute amount, then re-create ER, and compare. If
5653       difference exceeds tolerance then supercede
5654 */
5655  PROCEDURE SUPERCEDE_ER(p_run_id        in  integer,
5656                         errbuf          in out nocopy varchar2,
5657                         retcode         in out nocopy varchar2,
5658                         p_source_type   in varchar2,
5659                         p_source_code   in varchar2,
5660                         p_time_period_id in integer,
5661                         p_batch_name     in varchar2,
5662                         p_return_status out nocopy varchar2) is
5663 
5664   l_supercede_reqid_str varchar2(9000); --Bug 6037692
5665   l_run_id          integer;
5666   i                 integer;
5667   j                 integer;
5668   t_person_table    t_num_15_type;
5669   t_efforts_table   t_num_15_type;
5670   t_sum_line_id     t_num_15_type;
5671   t_tolerance_per   t_num_15_type;
5672   t_tolerance_req   t_num_15_type;
5673   TYPE t_num_15_type_ind   IS TABLE OF NUMBER(15) INDEX BY BINARY_INTEGER;
5674   t_tolerance_sline t_num_15_type_ind;
5675   t_request_id      t_num_15_type_ind;
5676   t_template_id     t_num_15_type_ind;
5677   t_tolerance_erid  t_num_15_type_ind;
5678   l_S_and_T_reqid integer;
5679   l_sql_string    varchar2(12000);   --Bug 6037692
5680   l_tolerance_amt number;
5681   l_tolerance_percent number;
5682   l_superceded_flag boolean := false ;
5683 
5684   --- get all ER template runs, that overlapp S_AND_T time periods.
5685   cursor check_er_template_h is
5686    Select distinct pth.template_id,
5687           pth.request_id
5688    From psp_payroll_controls ppc,
5689         Psp_report_templates_h pth,
5690         Per_time_periods ptp
5691    Where ppc.run_id = p_run_id
5692      And ppc.time_period_id = ptp.time_period_id
5693      And  fnd_date.canonical_to_date(fnd_date.date_to_canonical(pth.parameter_value_3)) >= ptp.start_date
5694      And  fnd_date.canonical_to_date(fnd_date.date_to_canonical(pth.parameter_value_2)) <= ptp.end_date;
5695 
5696   cursor supercede_tolerance(p_template_id in integer) is
5697   select nvl(sprcd_tolerance_amt,-999),
5698          nvl(sprcd_tolerance_percent,-999)
5699     from psp_report_templates
5700    where template_id = p_template_id;
5701 
5702 
5703 
5704    cursor adj_er_supercede is
5705           Select per.effort_report_id,
5706                 per.person_id,
5707                 min(pal.summary_line_id) summary_line_id
5708           From psp_adjustment_lines_history pal,
5709                Psp_eff_reports per,
5710                Psp_payroll_controls ppc
5711           Where pal.distribution_date between per.start_date and per.end_date
5712             And per.request_id in ( select -person_id from psp_supercede_persons_gt  where person_id < 0)
5713             And ppc.run_id = p_run_id
5714             And pal.person_id = per.person_id
5715             And ppc.payroll_control_id = pal.payroll_control_id
5716             And per.status_code not in ('S','R')
5717             And pal.element_type_id in
5718                 ((select petr.element_type_id from pay_element_type_rules petr, psp_report_template_details_h prth where
5719                   petr.include_or_exclude ='I' and
5720                   prth.request_id =per.request_id and prth.criteria_lookup_code='EST' and
5721                   petr.element_Set_id = prth.criteria_value1 )
5722                   union all (select pet1.element_type_id from
5723                   pay_element_types_f pet1, pay_ele_classification_rules pecr,
5724                   psp_report_template_details_h prth1
5725                   where pet1.classification_id = pecr.classification_id and
5726                   prth1.criteria_lookup_code='EST'  and prth1.request_id =per.request_id and
5727                         pecr.element_Set_id = prth1.criteria_value1 ))
5728             And pal.element_type_id not in
5729                  (select petr1.element_type_id from pay_element_type_rules petr1,
5730                   psp_report_template_Details_h prth2
5731                   where petr1.include_or_exclude='E'
5732                   and prth2.request_id = per.request_id
5733                   and prth2.criteria_lookup_code='EST' and prth2.criteria_value1= petr1.element_Set_id )
5734           group by per.person_id, per.effort_report_id;
5735 
5736   cursor pregen_er_supercede is
5737         Select  per.effort_report_id,
5738                 per.person_id,
5739                 min(pregen.summary_line_id)
5740           From psp_pre_gen_dist_lines_history pregen,
5741                Psp_eff_reports per,
5742                Psp_payroll_controls ppc
5743           Where pregen.distribution_date between per.start_date and per.end_date
5744             And per.request_id in (select -person_id from psp_supercede_persons_gt where person_id < 0)
5745             And ppc.run_id = p_run_id
5746             And pregen.person_id = per.person_id
5747             And ppc.payroll_control_id = pregen.payroll_control_id
5748             And per.status_code not in ('S','R')
5749             And pregen.element_type_id in
5750                 ((select petr.element_type_id from pay_element_type_rules petr, psp_report_template_details_h prth where
5751                   petr.include_or_exclude ='I' and
5752                   prth.request_id =per.request_id and prth.criteria_lookup_code='EST' and
5753                   petr.element_Set_id = prth.criteria_value1
5754                   )
5755                   union all
5756                   (select pet1.element_type_id from
5757                   pay_element_types_f pet1, pay_ele_classification_rules pecr,
5758                   psp_report_template_details_h prth1
5759                   where pet1.classification_id = pecr.classification_id and
5760                   prth1.criteria_lookup_code='EST'  and prth1.request_id =per.request_id and
5761                   pecr.element_Set_id = prth1.criteria_value1
5762                   ))
5763             And pregen.element_type_id not in
5764                  (select petr1.element_type_id from pay_element_type_rules petr1,
5765                   psp_report_template_Details_h prth2
5766                   where petr1.include_or_exclude='E'
5767                   and prth2.request_id = per.request_id
5768                   and prth2.criteria_lookup_code='EST' and prth2.criteria_value1= petr1.element_Set_id
5769                   )
5770           group by per.person_id,per.effort_report_id;
5771 
5772    cursor payroll_er_supercede is
5773         Select  per.effort_report_id,
5774                 per.person_id,
5775                 MIN(dlh.summary_line_id) summary_line_id
5776         From psp_eff_reports per,
5777              Psp_distribution_lines_history DLH,
5778              Psp_payroll_controls ppc,
5779              psp_payroll_sub_lines psub,
5780              psp_payroll_lines ppl
5781         Where dlh.payroll_sub_line_id = psub.payroll_sub_line_id
5782           And per.request_id in (select -person_id from psp_supercede_persons_gt where person_id < 0)
5783           And psub.payroll_line_id = ppl.payroll_line_id
5784           And ppl.person_id = per.person_id
5785           And dlh.distribution_date between per.start_date and per.end_date
5786           And ppc.run_id =   p_run_id
5787           And ppc.payroll_control_id = ppl.payroll_control_id
5788           And per.status_code not in ('S','R')
5789           And ppl.element_type_id in
5790               ((select petr.element_type_id from pay_element_type_rules petr, psp_report_template_details_h prth where
5791                   petr.include_or_exclude ='I' and
5792                   prth.request_id =per.request_id and prth.criteria_lookup_code='EST' and
5793                   petr.element_Set_id = prth.criteria_value1
5794                   )
5795                   union all
5796                   (select pet1.element_type_id from
5797                   pay_element_types_f pet1, pay_ele_classification_rules pecr,
5798                   psp_report_template_details_h prth1
5799                   where pet1.classification_id = pecr.classification_id and
5800                   prth1.criteria_lookup_code='EST' and prth1.request_id =per.request_id and
5801                   pecr.element_Set_id = prth1.criteria_value1
5802                   ))
5803           And ppl.element_type_id not in
5804                  (select petr1.element_type_id from pay_element_type_rules petr1,
5805                   psp_report_template_Details_h prth2
5806                   where petr1.include_or_exclude='E'
5807                   and prth2.request_id = per.request_id
5808                   and prth2.criteria_lookup_code='EST' and prth2.criteria_value1= petr1.element_Set_id
5809                   )
5810           group by per.person_id,per.effort_report_id;
5811 
5812    cursor all_er_supercede is
5813         select effort_report_id,
5814                person_id,
5815                min(summary_line_id) summary_line_id
5816         from (
5817         Select  per.effort_report_id,
5818                 per.person_id,
5819                 dlh.summary_line_id summary_line_id
5820         From psp_eff_reports per,
5821              Psp_distribution_lines_history DLH,
5822              Psp_payroll_controls ppc,
5823              psp_payroll_sub_lines psub,
5824              psp_payroll_lines ppl
5825         Where dlh.payroll_sub_line_id = psub.payroll_sub_line_id
5826           And per.request_id in (select -person_id from psp_supercede_persons_gt)
5827           And psub.payroll_line_id = ppl.payroll_line_id
5828           And ppl.person_id = per.person_id
5829           And dlh.distribution_date between per.start_date and per.end_date
5830           And ppc.run_id =   p_run_id
5831           And ppc.payroll_control_id = ppl.payroll_control_id
5832           And per.status_code not in ('S','R')
5833           And ppl.element_type_id in
5834               ((select petr.element_type_id from pay_element_type_rules petr, psp_report_template_details_h prth where
5835                   petr.include_or_exclude ='I' and
5836                   prth.request_id =per.request_id and prth.criteria_lookup_code='EST' and
5837                   petr.element_Set_id = prth.criteria_value1
5838                   )
5839                   union all
5840                   (select pet1.element_type_id from
5841                   pay_element_types_f pet1, pay_ele_classification_rules pecr,
5842                   psp_report_template_details_h prth1
5843                   where pet1.classification_id = pecr.classification_id and
5844                   prth1.criteria_lookup_code='EST'  and prth1.request_id =per.request_id
5845                   and pecr.element_Set_id = prth1.criteria_value1
5846                   ))
5847           And ppl.element_type_id not in
5848                  (select petr1.element_type_id from pay_element_type_rules petr1,
5849                   psp_report_template_Details_h prth2
5850                   where petr1.include_or_exclude='E'
5851                   and prth2.request_id = per.request_id
5852                   and prth2.criteria_lookup_code='EST' and prth2.criteria_value1= petr1.element_Set_id
5853                   )
5854         UNION ALL
5855         Select  per.effort_report_id,
5856                 per.person_id,
5857                 pregen.summary_line_id summary_line_id
5858           From psp_pre_gen_dist_lines_history pregen,
5859                Psp_eff_reports per,
5860                Psp_payroll_controls ppc
5861           Where pregen.distribution_date between per.start_date and per.end_date
5862             And per.request_id in ( select -person_id from psp_supercede_persons_gt )
5863             And ppc.run_id = p_run_id
5864             And pregen.person_id = per.person_id
5865             And ppc.payroll_control_id = pregen.payroll_control_id
5866             And per.status_code not in ('S','R')
5867             And pregen.element_type_id in
5868                 ((select petr.element_type_id from pay_element_type_rules petr, psp_report_template_details_h prth where
5869                   petr.include_or_exclude ='I' and
5870                   prth.request_id =per.request_id and prth.criteria_lookup_code='EST'
5871                   and petr.element_Set_id = prth.criteria_value1
5872                   )
5873                   union all
5874                   (select pet1.element_type_id from
5875                   pay_element_types_f pet1, pay_ele_classification_rules pecr,
5876                   psp_report_template_details_h prth1
5877                   where pet1.classification_id = pecr.classification_id and
5878                   prth1.criteria_lookup_code='EST'  and prth1.request_id =per.request_id
5879                   and pecr.element_Set_id = prth1.criteria_value1
5880                   ))
5881             And pregen.element_type_id not in
5882                  (select petr1.element_type_id from pay_element_type_rules petr1,
5883                   psp_report_template_Details_h prth2
5884                   where petr1.include_or_exclude='E'
5885                   and prth2.request_id = per.request_id
5886                   and prth2.criteria_lookup_code='EST' and prth2.criteria_value1= petr1.element_Set_id
5887                   ))
5888           group by person_id,effort_report_id;
5889 
5890  --- included asg check for uva asg matching
5891    cursor adj_asg_er_supercede is
5892           Select per.effort_report_id,
5893                 per.person_id,
5894                 min(pal.summary_line_id) summary_line_id
5895           From psp_adjustment_lines_history pal,
5896                Psp_eff_reports per,
5897                Psp_payroll_controls ppc,
5898                psp_eff_report_details perd
5899           Where pal.distribution_date between per.start_date and per.end_date
5900             and perd.effort_report_id = per.effort_report_id
5901             and perd.assignment_id = pal.assignment_id
5902             And per.request_id in ( select -person_id from psp_supercede_persons_gt  where person_id < 0)
5903             And ppc.run_id = p_run_id
5904             And pal.person_id = per.person_id
5905             And ppc.payroll_control_id = pal.payroll_control_id
5906             And per.status_code not in ('S','R')
5907             And pal.element_type_id in
5908                 ((select petr.element_type_id from pay_element_type_rules petr, psp_report_template_details_h prth where
5909                   petr.include_or_exclude ='I'
5910                   and prth.request_id =per.request_id and prth.criteria_lookup_code='EST'
5911                   and petr.element_Set_id = prth.criteria_value1 )
5912                   union all (select pet1.element_type_id from
5913                   pay_element_types_f pet1, pay_ele_classification_rules pecr,
5914                   psp_report_template_details_h prth1
5915                   where pet1.classification_id = pecr.classification_id
5916                   and prth1.criteria_lookup_code='EST'  and prth1.request_id =per.request_id
5917                   and pecr.element_Set_id = prth1.criteria_value1 ))
5918             And pal.element_type_id not in
5919                  (select petr1.element_type_id from pay_element_type_rules petr1,
5920                   psp_report_template_Details_h prth2
5921                   where petr1.include_or_exclude='E'
5922                   and prth2.request_id = per.request_id
5923                   and prth2.criteria_lookup_code='EST' and prth2.criteria_value1= petr1.element_Set_id )
5924           group by per.person_id, per.effort_report_id;
5925 
5926   cursor pregen_asg_er_supercede is
5927         Select  per.effort_report_id,
5928                 per.person_id,
5929                 min(pregen.summary_line_id)
5930           From psp_pre_gen_dist_lines_history pregen,
5931                Psp_eff_reports per,
5932                Psp_payroll_controls ppc,
5933                psp_eff_report_details perd
5934           Where pregen.distribution_date between per.start_date and per.end_date
5935             and perd.effort_report_id = per.effort_report_id
5936             and perd.assignment_id = pregen.assignment_id
5937             And per.request_id in (select -person_id from psp_supercede_persons_gt where person_id < 0)
5938             And ppc.run_id = p_run_id
5939             And pregen.person_id = per.person_id
5940             And ppc.payroll_control_id = pregen.payroll_control_id
5941             And per.status_code not in ('S','R')
5942             And pregen.element_type_id in
5943                 ((select petr.element_type_id from pay_element_type_rules petr, psp_report_template_details_h prth where
5944                   petr.include_or_exclude ='I' and
5945                   prth.request_id =per.request_id and prth.criteria_lookup_code='EST'
5946                   and petr.element_Set_id = prth.criteria_value1
5947                   )
5948                   union all
5949                   (select pet1.element_type_id from
5950                   pay_element_types_f pet1, pay_ele_classification_rules pecr,
5951                   psp_report_template_details_h prth1
5952                   where pet1.classification_id = pecr.classification_id and
5953                   prth1.criteria_lookup_code='EST'  and prth1.request_id =per.request_id
5954                   and pecr.element_Set_id = prth1.criteria_value1
5955                   ))
5956             And pregen.element_type_id not in
5957                  (select petr1.element_type_id from pay_element_type_rules petr1,
5958                   psp_report_template_Details_h prth2
5959                   where petr1.include_or_exclude='E'
5960                   and prth2.request_id = per.request_id
5961                   and prth2.criteria_lookup_code='EST' and prth2.criteria_value1= petr1.element_Set_id
5962                   )
5963           group by per.person_id,per.effort_report_id;
5964 
5965    cursor payroll_asg_er_supercede is
5966         Select  per.effort_report_id,
5967                 per.person_id,
5968                 MIN(dlh.summary_line_id) summary_line_id
5969         From psp_eff_reports per,
5970              Psp_distribution_lines_history DLH,
5971              Psp_payroll_controls ppc,
5972              psp_payroll_sub_lines psub,
5973              psp_payroll_lines ppl,
5974              psp_eff_report_details perd
5975         Where dlh.payroll_sub_line_id = psub.payroll_sub_line_id
5976           and perd.effort_report_id = per.effort_report_id
5977           and perd.assignment_id = ppl.assignment_id
5978           And per.request_id in (select -person_id from psp_supercede_persons_gt where person_id < 0)
5979           And psub.payroll_line_id = ppl.payroll_line_id
5980           And ppl.person_id = per.person_id
5981           And dlh.distribution_date between per.start_date and per.end_date
5982           And ppc.run_id =   p_run_id
5983           And ppc.payroll_control_id = ppl.payroll_control_id
5984           And per.status_code not in ('S','R')
5985           And ppl.element_type_id in
5986               ((select petr.element_type_id from pay_element_type_rules petr, psp_report_template_details_h prth where
5987                   petr.include_or_exclude ='I'
5988                    and prth.request_id =per.request_id and prth.criteria_lookup_code='EST'
5989                   and petr.element_Set_id = prth.criteria_value1
5990                   )
5991                   union all
5992                   (select pet1.element_type_id from
5993                   pay_element_types_f pet1, pay_ele_classification_rules pecr,
5994                   psp_report_template_details_h prth1
5995                   where pet1.classification_id = pecr.classification_id and
5996                   prth1.criteria_lookup_code='EST'  and prth1.request_id =per.request_id
5997                   and pecr.element_Set_id = prth1.criteria_value1
5998                   ))
5999           And ppl.element_type_id not in
6000                  (select petr1.element_type_id from pay_element_type_rules petr1,
6001                   psp_report_template_Details_h prth2
6002                   where petr1.include_or_exclude='E'
6003                   and prth2.request_id = per.request_id
6004                   and prth2.criteria_lookup_code='EST' and prth2.criteria_value1= petr1.element_Set_id
6005                   )
6006           group by per.person_id,per.effort_report_id;
6007 
6008 
6009    cursor all_asg_er_supercede is
6010         select effort_report_id,
6011                person_id,
6012                min(summary_line_id) summary_line_id
6013         from (
6014         Select  per.effort_report_id,
6015                 per.person_id,
6016                 dlh.summary_line_id summary_line_id
6017         From psp_eff_reports per,
6018              Psp_distribution_lines_history DLH,
6019              Psp_payroll_controls ppc,
6020              psp_payroll_sub_lines psub,
6021              psp_payroll_lines ppl,
6022              psp_eff_report_details perd
6023         Where dlh.payroll_sub_line_id = psub.payroll_sub_line_id
6024           and perd.effort_report_id = per.effort_report_id
6025           and perd.assignment_id = ppl.assignment_id
6026           And per.request_id in (select -person_id from psp_supercede_persons_gt)
6027           And psub.payroll_line_id = ppl.payroll_line_id
6028           And ppl.person_id = per.person_id
6029           And dlh.distribution_date between per.start_date and per.end_date
6030           And ppc.run_id =   p_run_id
6031           And ppc.payroll_control_id = ppl.payroll_control_id
6032           And per.status_code not in ('S','R')
6033           And ppl.element_type_id in
6034               ((select petr.element_type_id from pay_element_type_rules petr, psp_report_template_details_h prth where
6035                   petr.include_or_exclude ='I' and
6036                   prth.request_id =per.request_id and prth.criteria_lookup_code='EST'
6037                   and petr.element_Set_id = prth.criteria_value1
6038                   )
6039                   union all
6040                   (select pet1.element_type_id from
6041                   pay_element_types_f pet1, pay_ele_classification_rules pecr,
6042                   psp_report_template_details_h prth1
6043                   where pet1.classification_id = pecr.classification_id
6044                   and prth1.criteria_lookup_code='EST'  and prth1.request_id =per.request_id
6045                   and pecr.element_Set_id = prth1.criteria_value1
6046                   ))
6047           And ppl.element_type_id not in
6048                  (select petr1.element_type_id from pay_element_type_rules petr1,
6049                   psp_report_template_Details_h prth2
6050                   where petr1.include_or_exclude='E'
6051                   and prth2.request_id = per.request_id
6052                   and prth2.criteria_lookup_code='EST' and prth2.criteria_value1= petr1.element_Set_id
6053                   )
6054         UNION ALL
6055         Select  per.effort_report_id,
6056                 per.person_id,
6057                 pregen.summary_line_id summary_line_id
6058           From psp_pre_gen_dist_lines_history pregen,
6059                Psp_eff_reports per,
6060                Psp_payroll_controls ppc,
6061              psp_eff_report_details perd
6062           Where pregen.distribution_date between per.start_date and per.end_date
6063             and perd.effort_report_id = per.effort_report_id
6064             and perd.assignment_id = pregen.assignment_id
6065             And per.request_id in ( select -person_id from psp_supercede_persons_gt )
6066             And ppc.run_id = p_run_id
6067             And pregen.person_id = per.person_id
6068             And ppc.payroll_control_id = pregen.payroll_control_id
6069             And per.status_code not in ('S','R')
6070             And pregen.element_type_id in
6071                 ((select petr.element_type_id from pay_element_type_rules petr, psp_report_template_details_h prth where
6072                   petr.include_or_exclude ='I'
6073                   and prth.request_id =per.request_id and prth.criteria_lookup_code='EST'
6074                   and petr.element_Set_id = prth.criteria_value1
6075                   )
6076                   union all
6077                   (select pet1.element_type_id from
6078                   pay_element_types_f pet1, pay_ele_classification_rules pecr,
6079                   psp_report_template_details_h prth1
6080                   where pet1.classification_id = pecr.classification_id
6081                   and prth1.criteria_lookup_code='EST'  and prth1.request_id =per.request_id
6082                   and pecr.element_Set_id = prth1.criteria_value1
6083                   ))
6084             And pregen.element_type_id not in
6085                  (select petr1.element_type_id from pay_element_type_rules petr1,
6086                   psp_report_template_Details_h prth2
6087                   where petr1.include_or_exclude='E'
6088                   and prth2.request_id = per.request_id
6089                   and prth2.criteria_lookup_code='EST' and prth2.criteria_value1= petr1.element_Set_id
6090                   ))
6091           group by person_id,effort_report_id;
6092   cursor check_emp_overlap is
6093   select count(*)
6094     from psp_Eff_reports per,
6095          psp_summary_lines psl
6096    where per.person_id = psl.person_id
6097      and psl.payroll_control_id in
6098             (select payroll_control_id
6099                from psp_payroll_controls
6100               where run_id = p_run_id)
6101      and rownum = 1
6102      and per.request_id in
6103             (select abs(person_id)
6104                from psp_supercede_persons_gt
6105               where person_id < 0);
6106    l_emp_overlap_count integer;
6107 
6108  -- uva isue, supercede for matching asg option
6109     cursor check_emp_match_option is
6110     select count(*)
6111       from psp_report_templates_h
6112      where  request_id in  ( select -person_id from psp_supercede_persons_gt )
6113        and selection_match_level  =  'EMP';
6114 
6115  l_count_emp_match integer;
6116 
6117   cursor get_eff_id(p_req_id in integer, p_per_id in integer) is
6118   select effort_report_id
6119     from psp_eff_reports
6120    where request_id = p_req_id and person_id = p_per_id;
6121   l_eff_id integer;
6122 
6123     l_proj_segment varchar2(30);
6124     l_tsk_segment varchar2(30);
6125     l_awd_sgement varchar2(30);
6126     l_exp_org_segment varchar2(30);
6127     l_exp_type_segment varchar2(30);
6128     l_profile_bg_id Number   := FND_PROFILE.VALUE('PER_BUSINESS_GROUP_ID');
6129 
6130   CURSOR get_Con_Program_name_csr IS
6131   SELECT USER_CONCURRENT_PROGRAM_NAME
6132   FROM  fnd_concurrent_programs_vl fcp
6133   WHERE CONCURRENT_PROGRAM_ID = fnd_global.CONC_PROGRAM_ID;
6134   l_Con_Program_name varchar2(240);
6135 
6136 
6137   PROCEDURE SEND_NOTIFICATIONS(p_supercede_reqid_str in varchar2,
6138                                p_template_id         in integer,
6139                                p_SandT_reqid         in integer,
6140                                p_batch_name          in varchar2,
6141                                p_source_type         in varchar2,
6142                                p_source_code         in varchar2,
6143                                p_time_period_id      in varchar2) is
6144 
6145 
6146    l_role_display varchar2(500);
6147    l_member_rname varchar2(500);
6148    l_approver_notify boolean;
6149    l_employee_notify boolean;
6150    l_init_notify boolean;
6151    l_frp_notify boolean;
6152    t_wf_roles  t_varchar2_400_type;
6153    t_person_id t_num_15_type;
6154    l_template_name varchar2(240);
6155    l_distribution_source varchar2(260);
6156    l_time_period_name    varchar2(100);
6157 
6158    cursor get_template_name is
6159    select template_name
6160      from psp_report_templates
6161     where template_id = p_template_id;
6162 
6163    cursor get_superceding_source is
6164    select description
6165     from  psp_payroll_sources
6166    where source_type = p_source_type
6167      and source_code = p_source_code;
6168 
6169    cursor get_time_period_name is
6170    select period_name
6171      from per_time_periods
6172     where time_period_id = p_time_period_id;
6173 
6174    cursor get_frp_wf_names is
6175            select distinct wf.name,
6176                   wf.orig_system_id
6177             from wf_roles wf,
6178                  psp_report_templates_h h,
6179                  psp_report_template_details_h dtl
6180            where wf.orig_system = 'PER'
6181              and to_char(wf.orig_system_id) = dtl.criteria_value1
6182              and dtl.criteria_lookup_type = 'PSP_SELECTION_CRITERIA'
6183              and dtl.criteria_lookup_code = 'FRP'
6184              and dtl.request_id = h.request_id
6185              and h.request_id in ( select -person_id
6186                                      from psp_supercede_persons_gt
6187                                     where person_id < 0)
6188              and h.final_recip_notified_flag = 'Y';
6189 
6190    cursor get_init_wf_names is
6191            select distinct wf.name,
6192                   wf.orig_system_id
6193             from wf_roles wf,
6194                  psp_report_templates_h h
6195            where wf.orig_system = 'PER'
6196              and wf.orig_system_id = h.initiator_person_id
6197              and h.request_id in ( select -person_id
6198                                      from psp_supercede_persons_gt
6199                                     where person_id < 0)
6200               and exists (select 1 from psp_eff_reports per
6201                           where per.request_id = h.request_id
6202                             and per.superceding_request_id = p_SandT_reqid);
6203 
6204    cursor get_per_wf_names is
6205            select distinct wf.name,
6206                   er.person_id
6207             from wf_roles wf,
6208                  psp_eff_reports er,
6209                  psp_report_templates_h h
6210            where wf.orig_system = 'PER'
6211              and wf.orig_system_id = er.person_id
6212              and h.request_id = er.request_id
6213              and h.initiator_accept_flag = 'Y'
6214              and er.request_id in ( select -person_id
6215                                       from psp_supercede_persons_gt
6216                                      where person_id < 0)
6217              and er.superceding_request_id = p_SandT_reqid;
6218 
6219    cursor get_app_wf_names is
6220           select distinct era.wf_role_name
6221             from psp_eff_report_approvals era,
6222                  psp_eff_report_details erd,
6223                  psp_eff_reports er,
6224                  psp_report_templates_h h
6225            where erd.effort_report_detail_id = era.effort_report_detail_id
6226             and erd.effort_report_id = er.effort_report_id
6227             and er.request_id in ( select -person_id from psp_supercede_persons_gt where person_id < 0 )
6228             and er.superceding_request_id  = p_SandT_reqid
6229             and era.approval_status <> 'R'
6230             and er.request_id = h.request_id
6231             and h.initiator_accept_flag = 'Y';
6232 
6233   PROCEDURE      GET_RECEIVER_TYPES(p_template_id         in integer,
6234                                     p_initiator_notify    out nocopy boolean,
6235                                     p_final_recip_notify  out nocopy boolean,
6236                                     p_approver_notify     out nocopy boolean,
6237                                     p_employee_notify     out nocopy boolean) is
6238     cursor get_notify_types is
6239     select criteria_lookup_code
6240       from psp_report_template_details
6241      where template_id = p_template_id
6242        and criteria_lookup_type = 'PSP_SUPERCEDED_NOTIFICATION';
6243 
6244    notify_type        varchar2(10);
6245 
6246   BEGIN
6247      p_approver_notify    := false;
6248      p_initiator_notify   := false;
6249      p_final_recip_notify := false;
6250      p_employee_notify  := false;
6251     open get_notify_types;
6252     loop
6253       fetch get_notify_types into notify_type;
6254       if get_notify_types%notfound then
6255          if get_notify_types%rowcount = 0 then
6256            close get_notify_types;
6257            hr_utility.trace( 'SandT_Supercede--> GET_RECIVER_TYPES--> Superceded notifier types not found');
6258            return;
6259          end if;
6260          close get_notify_types;
6261          exit;
6262        end if;
6263        if notify_type = 'A' then
6264            p_approver_notify := true;
6265            hr_utility.trace( 'SandT_Supercede--> GET_RECIVER_TYPES--> p_apporver_notify');
6266        elsif notify_type = 'I' then
6267            p_initiator_notify := true;
6268            hr_utility.trace( 'SandT_Supercede--> GET_RECIVER_TYPES--> initiator_notify');
6269        elsif notify_type = 'F' then
6270            p_final_recip_notify  := true;
6271            hr_utility.trace( 'SandT_Supercede--> GET_RECIVER_TYPES--> final_recip_notify');
6272        elsif notify_type = 'E' then
6273            p_employee_notify := true;
6274            hr_utility.trace( 'SandT_Supercede--> GET_RECIVER_TYPES--> p_employee_notify');
6275        end if;
6276     end loop;
6277 
6278    EXCEPTION
6279    when others then
6280                 fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
6281       hr_utility.trace( 'SandT_Supercede-->send notifications --> GET_RECEIVER_TYPES when others '||sqlerrm);
6282      fnd_msg_pub.add_exc_msg('PSP_SUM_TRANS','GET_RECEIVER_TYPES');
6283      raise;
6284   END GET_RECEIVER_TYPES;
6285 
6286   PROCEDURE CALL_WF(p_supercede_reqid_str in varchar2,
6287                     p_wf_user          in varchar2,
6288                     p_SandT_reqid      in integer,
6289                     p_person_id        in integer,
6290                     p_approver_type    in varchar2,
6291                     p_template_name    in varchar2,
6292                     p_batch_name       in varchar2,
6293                     p_superceding_source in varchar2,
6294                     p_time_period_name   in varchar2,
6295                     p_source_type        in varchar2,
6296                     p_source_code        in varchar2,
6297                     p_time_period_id     in varchar2) is
6298 
6299      l_wf_itemkey integer;
6300      l_superceding_process varchar2(200);
6301      l_superceding_proc_param varchar2(500);
6302 
6303    BEGIN
6304 
6305        hr_utility.trace('SandT_Supercede--> send_notifications --> call_wf Parameters: p_supercede_reqid_str = '||
6306                     p_supercede_reqid_str||',  p_wf_user ='|| p_wf_user||',  p_SandT_reqid='||
6307                      p_SandT_reqid||
6308                     ', p_person_id='||p_person_id||', p_approver_type = '||p_approver_type||
6309                     ', p_source_type='|| p_source_type ||', p_source_code='|| p_source_code||
6310                     ', p_time_period_id='||p_time_period_id);
6311 
6312        select psp_wf_item_key_s.nextval
6313        into  l_wf_itemkey
6314        from dual;
6315 
6316        if p_source_type = 'A' then
6317            l_superceding_process := 'PSP: Summarize And Transfer Adjustments';
6318            l_superceding_proc_param := p_batch_name;
6319        else
6320            l_superceding_process := 'PSP: Summarize And Transfer Payroll Distributions';
6321            l_superceding_proc_param := p_source_type||', '||p_source_code||', '||
6322                          g_payroll_id||', '||p_time_period_id||', '||p_batch_name;
6323        end if;
6324 
6325        wf_engine.CreateProcess(itemtype => 'PSPERAVL',
6326                                itemkey  => l_wf_itemkey,
6327                                process  => 'SUPERCEDE_PROCESS');
6328 
6329 
6330        /*Added for bug 7004679 */
6331         wf_engine.setitemowner(itemtype => 'PSPERAVL',
6332                                itemkey  => l_wf_itemkey,
6333                                owner    => p_wf_user);
6334 
6335 
6336 
6337        wf_engine.SetItemAttrText(itemtype => 'PSPERAVL',
6338                                  itemkey  => l_wf_itemkey,
6339                                  aname    => 'SUPERCEDING_PROC_NAME',
6340                                  avalue   => l_superceding_process);
6341 
6342        wf_engine.SetItemAttrText(itemtype => 'PSPERAVL',
6343                                  itemkey  => l_wf_itemkey,
6344                                  aname    => 'SUPERCEDING_PAYROLL_SOURCE',
6345                                  avalue   => p_source_type);
6346 
6347        wf_engine.SetItemAttrText(itemtype => 'PSPERAVL',
6348                                  itemkey  => l_wf_itemkey,
6349                                  aname    => 'SUPERCEDING_PROC_PARAM',
6350                                  avalue   => l_superceding_proc_param);
6351 
6352        wf_engine.SetItemAttrText(itemtype => 'PSPERAVL',
6353                                  itemkey  => l_wf_itemkey,
6354                                  aname    => 'SUPERCEDED_ER_REQ_ID_STR',
6355                                  avalue   => p_supercede_reqid_str);
6356 
6357        wf_engine.SetItemAttrNumber(itemtype => 'PSPERAVL',
6358                                   itemkey  => l_wf_itemkey,
6359                                   aname    => 'SUPERCEDING_S_AND_T_REQID',
6360                                   avalue   => p_SandT_reqid);
6361 
6362        wf_engine.SetItemAttrText(itemtype => 'PSPERAVL',
6363                                  itemkey  => l_wf_itemkey,
6364                                  aname    => 'SUPERCEDE_RECIPIENT_TYPE',
6365                                  avalue   => p_approver_type);   --- initiatior, final recipients,
6366                                                                 ---- employee, past approvers
6367        wf_engine.SetItemAttrText(itemType => 'PSPERAVL',
6368                                  itemkey  => l_wf_itemkey,
6369                                  aname    => 'TEMPLATE_NAME',
6370                                  avalue   => l_template_name);
6371 
6372           wf_engine.SetItemAttrNumber(itemtype => 'PSPERAVL',
6373                                     itemkey  => l_wf_itemkey,
6374                                     aname    => 'PERSON_ID',
6375                                     avalue   => p_person_id);
6376 
6377           wf_engine.SetItemAttrText(itemtype => 'PSPERAVL',
6378                                     itemkey  => l_wf_itemkey,
6379                                     aname    => 'APPROVER_ROLE_NAME',
6380                                     avalue   => p_wf_user);
6381 
6382       wf_engine.StartProcess(itemtype => 'PSPERAVL',
6383                              itemkey  => l_wf_itemkey);
6384    EXCEPTION
6385    when others then
6386  			fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
6387 
6388      hr_utility.trace( 'SandT_Supercede-->send notifications --> call_wf  when others '||sqlerrm);
6389      fnd_msg_pub.add_exc_msg('PSP_SUM_TRANS','CALL_WF');
6390      raise;
6391    END CALL_WF;
6392 
6393 
6394   BEGIN  -- send notifications
6395     hr_utility.trace( 'SandT_Supercede--> send_notifications-->  Begin params : p_supercede_reqid_str= '||
6396                     p_supercede_reqid_str ||', p_template_id        = '||p_template_id ||
6397                      ', p_SandT_reqid        = '||p_SandT_reqid ||
6398                      ', p_batch_name         = '||p_batch_name ||
6399                      ', p_source_type        = '||p_source_type ||
6400                      ', p_source_code        = '|| p_source_code ||
6401                      ', p_time_period_id   = '|| p_time_period_id );
6402    hr_utility.trace( 'SandT_Supercede--> send_notifications-->get_receiver_types');
6403     get_receiver_types(p_template_id,
6404                        l_init_notify,
6405                        l_frp_notify,
6406                        l_approver_notify,
6407                        l_employee_notify);
6408 
6409     open get_template_name;
6410     fetch get_template_name into l_template_name;
6411     close get_template_name;
6412 
6413     open get_superceding_source;
6414     fetch get_superceding_source into l_distribution_source;
6415     close get_superceding_source;
6416 
6417     if l_init_notify then
6418             open get_init_wf_names;
6419             fetch get_init_wf_names bulk collect into t_wf_roles, t_person_id;
6420             close get_init_wf_names;
6421 
6422              for i in 1..t_wf_roles.count
6423              loop
6424                CALL_WF(p_supercede_reqid_str,
6425                        t_wf_roles(i),
6426                        p_SandT_reqid,
6427                        t_person_id(i),
6428                        'INIT',
6429                        l_template_name,
6430                        p_batch_name,
6431                        l_distribution_source,
6432                        l_time_period_name,
6433                        p_source_type,
6434                        p_source_code,
6435                        p_time_period_id);
6436              end loop;
6437     end if;
6438 
6439     if l_frp_notify then
6440             open  get_frp_wf_names;
6441             fetch get_frp_wf_names bulk collect into t_wf_roles, t_person_id;
6442             close get_frp_wf_names;
6443 
6444              for i in 1..t_wf_roles.count
6445              loop
6446                CALL_WF(p_supercede_reqid_str,
6447                        t_wf_roles(i),
6448                        p_SandT_reqid,
6449                        t_person_id(i),
6450                        'FRP',
6451                        l_template_name,
6452                        p_batch_name,
6453                        l_distribution_source,
6454                        l_time_period_name,
6455                        p_source_type,
6456                        p_source_code,
6457                        p_time_period_id);
6458              end loop;
6459      end if;
6460 
6461 
6462        if l_employee_notify then
6463             open get_per_wf_names;
6464             fetch get_per_wf_names bulk collect into t_wf_roles, t_person_id;
6465             close get_per_wf_names;
6466 
6467              for i in 1..t_wf_roles.count
6468              loop
6469                  CALL_WF(p_supercede_reqid_str,
6470                          t_wf_roles(i),
6471                          p_SandT_reqid,
6472                          t_person_id(i),
6473                          'EMP',
6474                           l_template_name,
6475                           p_batch_name,
6476                           l_distribution_source,
6477                           l_time_period_name,
6478                           p_source_type,
6479                           p_source_code,
6480                           p_time_period_id);
6481              end loop;
6482        end if;
6483 
6484     hr_utility.trace( 'SandT_Supercede-->send_notifications--> has dynamic part for approver start');
6485        if l_approver_notify then
6486             open get_app_wf_names;
6487             fetch get_app_wf_names bulk collect into t_wf_roles;
6488             close get_app_wf_names;
6489              for i in 1..t_wf_roles.count
6490              loop
6491                  CALL_WF(p_supercede_reqid_str,
6492                          t_wf_roles(i),
6493                          p_SandT_reqid,
6494                          null,
6495                          'APP',
6496                          l_template_name,
6497                          p_batch_name,
6498                          l_distribution_source,
6499                          l_time_period_name,
6500                          p_source_type,
6501                          p_source_code,
6502                          p_time_period_id);
6503              end loop;
6504        end if;
6505   EXCEPTION
6506    when others then
6507 			fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
6508 
6509      hr_utility.trace( 'SandT_Supercede-->send notifications when others '||sqlerrm);
6510      fnd_msg_pub.add_exc_msg('PSP_SUM_TRANS','SEND_NOTIFICATIONS');
6511      raise;
6512   END SEND_NOTIFICATIONS;
6513  BEGIN   ----SUPERCEDE_ER
6514 --- comment trace.. debug
6515 ---hr_utility.trace_on('Y','ZX');
6516  hr_utility.trace( 'SandT_Supercede--> Begin  p_run_id='||p_run_id);
6517  fnd_signon.set_session(to_char(sysdate,'dd-mon-yyyy'));
6518   p_return_status := fnd_api.g_ret_sts_success;
6519   l_S_and_T_reqid := fnd_global.conc_request_id;
6520       fnd_stats.gather_table_stats(ownname => 'PSP',
6521                                    tabname => 'PSP_SUPERCEDE_PERSONS_GT');
6522   open check_er_template_h;
6523   fetch check_er_template_h bulk collect into t_template_id, t_request_id;
6524   close check_er_template_h;
6525 
6526   if t_template_id.count = 0 then
6527     return;  --- no template found to supercede
6528   end if;
6529 
6530   hr_utility.trace( 'SandT_Supercede--> template_id.count='||t_template_id.count);
6531 
6532   l_supercede_reqid_str := null;
6533 
6534   delete psp_supercede_persons_gt;
6535   for k in 1..t_template_id.count
6536   loop
6537     if t_template_id.count = k or   --- supercede for distinct template
6538        (t_template_id.count >  k  and t_template_id(k) <> t_template_id(k+1)) then
6539        if l_supercede_reqid_str is null then
6540           l_supercede_reqid_str := t_request_id(k);
6541        else
6542           l_supercede_reqid_str := l_supercede_reqid_str ||', '|| t_request_id(k);
6543        end if;
6544        insert into psp_supercede_persons_gt
6545 		(person_id)
6546 	values	( -t_request_id(k));
6547 
6548        open check_emp_overlap;
6549        fetch check_emp_overlap into l_emp_overlap_count;
6550        close check_emp_overlap;
6551       ---fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' before emp count');
6552   if l_emp_overlap_count > 0 then
6553 
6554       open check_emp_match_option;
6555       fetch check_emp_match_option into l_count_emp_match;
6556       close check_emp_match_option;
6557       ---fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' l_count_emp_match = '||l_count_emp_match);
6558        hr_utility.trace( 'SandT_Supercede--> Main Loop Entered');
6559        hr_utility.trace( 'SandT_Supercede-->  reqid string, p_source_type, template_id ='||
6560                   l_supercede_reqid_str||':'||p_source_type||':'||t_template_id(k));
6561      if l_count_emp_match > 0 then
6562      if p_source_type = 'A' then    --- from adjustments s_and_t process.
6563         open adj_er_supercede;
6564         fetch adj_er_supercede bulk collect into t_efforts_table, t_person_table, t_sum_line_id;
6565         close adj_er_Supercede;
6566      elsif p_source_type = 'P' then
6567         open pregen_er_supercede;
6568         fetch pregen_er_supercede bulk collect into t_efforts_table, t_person_table, t_sum_line_id;
6569         close pregen_er_Supercede;
6570      elsif p_source_type in ('N','O') then	-- Introduced MIN() for bug fix 4024794
6571         open payroll_er_supercede;
6572         fetch payroll_er_supercede bulk collect into t_efforts_table, t_person_table, t_sum_line_id;
6573         close payroll_er_Supercede;
6574      else
6575         open all_er_supercede;
6576         fetch all_er_supercede bulk collect into t_efforts_table, t_person_table, t_sum_line_id;
6577         close all_er_Supercede;
6578       end if; --- source type adjustments
6579      else
6580      if p_source_type = 'A' then    --- from adjustments s_and_t process.
6581         open adj_asg_er_supercede;
6582         fetch adj_asg_er_supercede bulk collect into t_efforts_table, t_person_table, t_sum_line_id;
6583         close adj_asg_er_Supercede;
6584      elsif p_source_type = 'P' then
6585         open pregen_asg_er_supercede;
6586         fetch pregen_asg_er_supercede bulk collect into t_efforts_table, t_person_table, t_sum_line_id;
6587         close pregen_asg_er_Supercede;
6588      elsif p_source_type in ('N','O') then
6589         open payroll_asg_er_supercede;
6590         fetch payroll_asg_er_supercede bulk collect into t_efforts_table, t_person_table, t_sum_line_id;
6591         close payroll_asg_er_Supercede;
6592     ---fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || ' t_efforts_table '||t_efforts_table.count);
6593        else
6594         open all_asg_er_supercede;
6595         fetch all_asg_er_supercede bulk collect into t_efforts_table, t_person_table, t_sum_line_id;
6596         close all_asg_er_Supercede;
6597       end if; --- source type adjustments
6598      end if;
6599     hr_utility.trace( 'SandT_Supercede-->  loaded the superceded persons, count ='|| t_efforts_table.count);
6600         --fnd_file.put_line(fnd_file.log, 'SandT_Supercede-->  loaded the superceded persons, count ='|| t_efforts_table.count);
6601     open supercede_tolerance(t_template_id(k));
6602     fetch supercede_tolerance into l_tolerance_amt, l_tolerance_percent;
6603     close supercede_tolerance;
6604     hr_utility.trace( 'SandT_Supercede--> get_tolerance limits AMT, %'||
6605              nvl(l_tolerance_amt,0)||','|| nvl(l_tolerance_percent,0));
6606 
6607     if t_efforts_table.count > 0 then
6608        hr_utility.trace( 'SandT_Supercede-->  t_efforts_table.count > 0');
6609        if l_tolerance_amt = -999 and l_tolerance_percent = -999 then
6610          --- if tolerance is not setup then all emps are superceded
6611          --- Summarize/Transfer run will be superceeded.
6612 
6613          hr_utility.trace( 'SandT_Supercede--> Before update Eff table 1');
6614           i := 1;
6615          loop
6616             if i >  t_efforts_table.count then
6617               exit;
6618             end if;
6619             hr_utility.trace( 'SandT_Supercede-->  Effort report id ='||t_efforts_table(i));
6620 
6621             OPEN get_Con_Program_name_csr;
6622             FETCH get_Con_Program_name_csr into l_Con_Program_name;
6623             CLOSE get_Con_Program_name_csr;
6624 
6625 --Bug 4339561
6626 -- Pupulating Column superceding_program_name
6627             update psp_eff_reports
6628                set status_code = 'S',
6629                    superceding_summary_line_id = t_sum_line_id(i),
6630                    superceding_request_id      = l_S_and_T_reqid,
6631                    superceding_program_name = l_Con_Program_name
6632              where effort_report_id = t_efforts_table(i);
6633 
6634              if sql%rowcount > 0 then
6635                 l_superceded_flag := true;
6636              end if;
6637             hr_utility.trace( 'SandT_Supercede--> Updated the ER table, to put S');
6638                  i := i + 1;
6639          end loop;
6640        else  -- tolerance set to non null value
6641           --- load session temp table
6642           hr_utility.trace( 'SandT_Supercede--> purge GT table - 1');
6643           forall i in 1..t_person_table.count
6644           insert into psp_supercede_persons_gt
6645 			(person_id)
6646           values	(t_person_table(i));
6647           hr_utility.trace( 'SandT_Supercede--> insert into GT potential persons Number of recodrs=' ||t_person_table.count||'person_id =' ||t_person_table(1));
6648           savepoint populate_er_tables;
6649 
6650            -- create fresh ER for comparison
6651           hr_utility.trace( 'SandT_Supercede--> before call to psp_create_eff_reports.populate_eff_tables');
6652           for n in 1..t_template_id.count
6653           loop
6654              if t_template_id(n) = t_template_id(k) then
6655                 hr_utility.trace('SandT_Supercede--> call CREATE_EFF params : superced_request='||
6656                   t_request_id(n)||' , template_id = '||t_template_id(n));
6657                 psp_create_eff_reports.populate_eff_tables(errBuf ,
6658                                                            retCode,
6659                                                            null,
6660                                                            t_request_id(n),
6661                                                            null,
6662                                                            'Y');
6663                 hr_utility.trace('SandT_Supercede--> g_summarization_criteria = '|| PSP_CREATE_EFF_REPORTS.g_summarization_criteria);
6664 
6665              end if;
6666           end loop;
6667           hr_utility.trace( 'SandT_Supercede--> after call to psp_create_eff_reports.populate_eff_tables');
6668 
6669           if l_tolerance_amt <> -999 then  --- tolerance_amt is not null then
6670              l_sql_string := ' begin
6671                               select distinct er.request_id, er.person_id
6672                                 bulk collect into :reqtable, :pertable
6673                                 from psp_eff_report_details erd,
6674                                      psp_supercede_persons_gt gt,
6675                                      psp_eff_reports er
6676                                where er.status_code not in
6677                                       ('||''''||'S'||''''||','||''''||'R'||''''||')
6678                                  and er.request_id in ('||l_supercede_reqid_str||')
6679                                  and er.effort_report_id = erd.effort_report_id
6680                                  and gt.person_id = er.person_id
6681                                group by er.request_id, er.person_id '|| PSP_CREATE_EFF_REPORTS.g_summarization_criteria ||
6682                               ' having abs(sum(decode(er.status_code,'||''''|| 'T'||''''||',
6683                                            -erd.actual_salary_amt,
6684                                             erd.actual_salary_amt))) > :l_tolerance_amt;
6685                               end;';
6686 
6687 --Bug 4319874 START
6688 -- Replacing the Dynamic Sql based on Configuration value
6689 		IF PSP_GENERAL.GET_CONFIGURATION_OPTION_VALUE(l_profile_bg_id,'PSP_USE_GL_PTAOE_MAPPING') = 'Y' THEN
6690 				PSP_GENERAL.GET_GL_PTAOE_MAPPING(p_business_group_id => l_profile_bg_id,
6691 					      p_proj_segment => l_proj_segment,
6692 					      p_tsk_segment => l_tsk_segment,
6693 					      p_awd_sgement => l_awd_sgement,
6694 					      p_exp_org_segment=> l_exp_org_segment,
6695 					      p_exp_type_segment => l_exp_type_segment);
6696 			IF (l_proj_segment is null) OR   (l_tsk_segment is null) OR (l_awd_sgement is null) OR
6697 			    (l_exp_org_segment is null) OR (l_exp_type_segment is null) THEN
6698 			     fnd_message.set_name('PSP', 'PSP_GL_PTAOE_NOT_MAPPED');
6699 			      raise fnd_api.g_exc_unexpected_error;
6700 			END IF;
6701 				      l_sql_string := replace(l_sql_string,'psl','erd');
6702 				      l_sql_string := replace(l_sql_string,'gcc','erd');
6703 				      l_sql_string := replace(l_sql_string,l_proj_segment,'PROJECT_ID');
6704 				      l_sql_string := replace(l_sql_string,l_tsk_segment,'TASK_ID');
6705 				      l_sql_string := replace(l_sql_string,l_awd_sgement,'AWARD_ID');
6706 				      l_sql_string := replace(l_sql_string,l_exp_org_segment,'EXPENDITURE_ORGANIZATION_ID');
6707 				      l_sql_string := replace(l_sql_string,l_exp_type_segment,'EXPENDITURE_TYPE');
6708 		ELSE
6709 				      l_sql_string := replace(l_sql_string,'psl','erd');
6710 				      l_sql_string := replace(l_sql_string,'gcc','erd');
6711 				      l_sql_string := replace(l_sql_string,'SEGMENT','GL_SEGMENT');
6712 		END IF;
6713 -- Bug 4319874 : END
6714 
6715 
6716               hr_utility.trace( 'SandT_Supercede--> before exec immediate for tol amt'||l_sql_string);
6717               hr_utility.trace( 'SandT_Supercede--> sql_string ='||l_sql_string);
6718               execute immediate l_sql_string
6719                 using out t_tolerance_req,
6720                       out t_tolerance_per,
6721                        in abs(l_tolerance_amt);
6722               hr_utility.trace( 'SandT_Supercede--> after exec immediate for tol amt = '||abs(l_tolerance_amt));
6723            else
6724              l_sql_string := ' begin
6725                               select distinct er.request_id, er.person_id
6726                                 bulk collect into :reqtable, :pertable
6727                                 from psp_eff_report_details erd,
6728                                      psp_supercede_persons_gt gt,
6729                                      psp_eff_reports er
6730                                where er.status_code not in
6731                                       ('||''''||'S'||''''||','||''''||'R'||''''||')
6732                                  and er.request_id in ('||l_supercede_reqid_str||')
6733                                  and gt.person_id = er.person_id
6734                                  and er.effort_report_id = erd.effort_report_id
6735                                group by er.request_id, er.person_id '||PSP_CREATE_EFF_REPORTS.g_summarization_criteria ||
6736                               ' having abs(sum(decode(er.status_code,'||''''|| 'T'||''''||',
6737                                            -erd.payroll_percent,
6738                                             erd.payroll_percent))) > :l_tolerance_percent;
6739                                end;';
6740 --Bug 4319874 START
6741 -- Replacing the Dynamic Sql based on Configuration value
6742 		IF PSP_GENERAL.GET_CONFIGURATION_OPTION_VALUE(l_profile_bg_id,'PSP_USE_GL_PTAOE_MAPPING') = 'Y' THEN
6743 				PSP_GENERAL.GET_GL_PTAOE_MAPPING(p_business_group_id => l_profile_bg_id,
6744 					      p_proj_segment => l_proj_segment,
6745 					      p_tsk_segment => l_tsk_segment,
6746 					      p_awd_sgement => l_awd_sgement,
6747 					      p_exp_org_segment=> l_exp_org_segment,
6748 					      p_exp_type_segment => l_exp_type_segment);
6749 			IF (l_proj_segment is null) OR   (l_tsk_segment is null) OR (l_awd_sgement is null) OR
6750 			    (l_exp_org_segment is null) OR (l_exp_type_segment is null) THEN
6751 			     fnd_message.set_name('PSP', 'PSP_GL_PTAOE_NOT_MAPPED');
6752 			      raise fnd_api.g_exc_unexpected_error;
6753 			END IF;
6754 				      l_sql_string := replace(l_sql_string,'psl','erd');
6755 				      l_sql_string := replace(l_sql_string,'gcc','erd');
6756 				      l_sql_string := replace(l_sql_string,l_proj_segment,'PROJECT_ID');
6757 				      l_sql_string := replace(l_sql_string,l_tsk_segment,'TASK_ID');
6758 				      l_sql_string := replace(l_sql_string,l_awd_sgement,'AWARD_ID');
6759 				      l_sql_string := replace(l_sql_string,l_exp_org_segment,'EXPENDITURE_ORGANIZATION_ID');
6760 				      l_sql_string := replace(l_sql_string,l_exp_type_segment,'EXPENDITURE_TYPE');
6761 		ELSE
6762 				      l_sql_string := replace(l_sql_string,'psl','erd');
6763 				      l_sql_string := replace(l_sql_string,'gcc','erd');
6764 				      l_sql_string := replace(l_sql_string,'SEGMENT','GL_SEGMENT');
6765 		END IF;
6766 -- Bug 4319874 : END
6767 
6768 
6769               hr_utility.trace( 'SandT_Supercede--> before exec immediate for tol %');
6770               hr_utility.trace( 'SandT_Supercede--> sql_string ='||l_sql_string);
6771               execute immediate l_sql_string
6772                 using out t_tolerance_req,
6773                       out t_tolerance_per,
6774                        in abs(l_tolerance_percent);
6775               hr_utility.trace( 'SandT_Supercede--> after exec immediate for tol %');
6776            end if;
6777               ---- debug;
6778               ---commit;  --- remove commit;
6779               rollback to populate_er_tables;
6780               hr_utility.trace( 'SandT_Supercede--> before tolerance_req.count = '||t_tolerance_req.count);
6781             for i in 1..t_tolerance_req.count
6782             loop
6783               hr_utility.trace( 'SandT999_Supercede--> before tolerance_req.count = '||t_tolerance_req.count);
6784               open get_eff_id(t_tolerance_req(i), t_tolerance_per(i));
6785               fetch get_eff_id into l_eff_id;
6786               close get_eff_id;
6787               hr_utility.trace( 'SandTTTTTTupercede--> before tolerance_req.count = '||t_tolerance_req.count);
6788               t_tolerance_erid(i) := l_eff_id;
6789               hr_utility.trace( 'Sand%%%%%%%%%%cede--> before tolerance_req.count = '||t_tolerance_req.count);
6790             end loop;
6791 
6792               hr_utility.trace( 'SandT_Supercede--> before tolerance_erid.count = '||t_tolerance_erid.count);
6793            if t_tolerance_erid.count > 0 then
6794               for i in 1..t_tolerance_erid.count
6795               loop
6796                 hr_utility.trace( 'SandT_Supercede--> tolerance_erid='||t_tolerance_erid(i));
6797                 for j in 1..t_efforts_table.count
6798                 loop
6799                    if t_tolerance_erid(i) = t_efforts_table(j) then
6800                       hr_utility.trace( 'SandT_Supercede--> MATCH t_Efforts_Table ='|| t_efforts_table(j));
6801                       t_tolerance_sline(i) := t_sum_line_id(j);
6802                    end if;
6803                 end loop;
6804               end loop;
6805                 -- found ERs to be superceded.
6806               i := 1;
6807               loop
6808                 if i > t_tolerance_erid.count then
6809                   exit;
6810                 end if;
6811                 hr_utility.trace( 'SandT_Supercede--> erid='|| t_tolerance_erid(i));
6812                 if t_tolerance_sline.exists(i) then
6813                 -- found ERs to be superceded.
6814 
6815             OPEN get_Con_Program_name_csr;
6816             FETCH get_Con_Program_name_csr into l_Con_Program_name;
6817             CLOSE get_Con_Program_name_csr;
6818 --Bug 4339561
6819 -- Pupulating Column superceding_program_name
6820 
6821                 update psp_eff_reports
6822                    set status_code = 'S',
6823                        superceding_summary_line_id = t_tolerance_sline(i),
6824                        superceding_request_id      = l_S_and_T_reqid,
6825                        superceding_program_name = l_Con_Program_name
6826                  where effort_report_id = t_tolerance_erid(i);
6827                 hr_utility.trace( 'SandT_Supercede--> Updated erid='|| t_tolerance_erid(i));
6828                 if sql%rowcount > 0 then
6829                    l_superceded_flag := true;
6830                 end if;
6831                 end if;
6832                  i := i + 1;
6833              end loop;
6834            end if; --- non zero tolerance persons count > 0
6835        end if;  -- tolerance = 0
6836        --- delete persons .. retain -persons_ids.. the request_ids.
6837        delete psp_supercede_persons_gt where person_id > 0;
6838        if l_superceded_flag then
6839        -- send superceding notifications
6840        send_notifications(l_supercede_reqid_str,
6841                           t_template_id(k),
6842                           l_S_and_T_reqid,
6843                           p_batch_name ,
6844                           p_source_type,
6845                           p_source_code,
6846                           p_time_period_id);
6847         l_superceded_flag := false;
6848       end if;
6849     end if;  -- supercede persons count > 0
6850     l_supercede_reqid_str := null;
6851     delete psp_supercede_persons_gt;
6852    else
6853     l_supercede_reqid_str := null;
6854     delete psp_supercede_persons_gt;
6855    end if;   --- only if s_and_t processes emps in er
6856    else   ---  more than on reqid for Template_id
6857        insert into psp_supercede_persons_gt (person_id) values ( -t_request_id(k));
6858        if l_supercede_reqid_str is null then
6859           l_supercede_reqid_str := t_request_id(k);
6860        else
6861           l_supercede_reqid_str := l_supercede_reqid_str ||', '|| t_request_id(k);
6862        end if;
6863    end if; -- new template
6864   end loop;
6865   delete psp_supercede_persons_gt;
6866   t_person_table    := t_num_15_type(null);
6867   t_efforts_table   := t_num_15_type(null);
6868   t_sum_line_id     := t_num_15_type(null);
6869   t_person_table.delete;
6870   t_efforts_table.delete;
6871   t_sum_line_id.delete ;
6872   t_tolerance_erid.delete;
6873   t_request_id.delete;
6874   t_template_id.delete;
6875   t_tolerance_sline.delete;
6876   hr_utility.trace( 'SandT_Supercede--> EXITING');
6877  EXCEPTION
6878   when others then
6879           fnd_file.put_line(fnd_file.log, fnd_date.date_to_canonical(SYSDATE) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
6880        hr_utility.trace( 'SandT_Supercede--> when others in supercede_er'||sqlerrm) ;
6881      fnd_msg_pub.add_exc_msg('PSP_SUM_TRANS', 'SUPERCEDE_ER');
6882      p_return_status := fnd_api.g_ret_sts_unexp_error;
6883  END supercede_er;
6884 
6885 END PSP_SUM_TRANS;