DBA Data[Home] [Help]

PACKAGE BODY: APPS.PA_FUNDS_CONTROL_PKG1

Source


1 package body PA_FUNDS_CONTROL_PKG1 as
2 -- $Header: PABCPKTB.pls 120.67.12020000.7 2013/03/06 09:13:35 admarath ship $
3 
4 -------------------------------------------------------------------------------------
5 -- Declare Global variables
6 --------------------------------------------------------------------------------------
7 	g_error_stage 		varchar2(1000);
8 	g_doc_type		varchar2(1000);
9 	g_base_pre_task_id   	number := null;
10 	g_base_pre_exp_type  	varchar2(100) := null;
11 	g_base_pre_ei_date   	date   := null;
12 	g_pre_base       	varchar2(100) := null;
13 	g_pre_cp_structure      varchar2(100) := null;
14 	g_base_error_msg_code 	varchar2(100) := null;
15 	g_base_return_status  	varchar2(100) := null;
16 	g_acct_currency_code  	varchar2(100);
17 	g_cache_period_sob	number := null;
18 	g_cache_period_pa_date	date  := null;
19 	g_cache_period_name	varchar2(100) := null;
20 
21 	/** added these variables to cache the start and end dates  bug fix : 1992734 **/
22 	g_sd_project_id         number := null;
23 	g_sd_bdgt_version_id    number := null;
24 	g_sd_tm_phase_code      varchar2(100) := null;
25 	g_sd_amt_type           varchar2(100) := null;
26 	g_sd_boundary_code      varchar2(100) := null;
27 	g_sd_sob                number  := null;
28 	g_start_date            date := null;
29 	g_end_date            date := null;
30 	g_sd_result_code        varchar2(100);
31 	g_var_invoice_id        Number := NUll;
32         g_var_dist_line_num     Number := NUll;
33 	g_amt_var                Number := Null;
34 	g_amt_base_var          Number := Null;
35 
36 ---------------------------------------------------------------------------------
37 -- declare plsql tables to hold values during the funds check process
38 ---------------------------------------------------------------------
39         g_tab_budget_version_id                 pa_plsql_datatypes.IdTabTyp;
40         g_tab_budget_line_id                    pa_plsql_datatypes.IdTabTyp;
41         g_tab_budget_ccid                       pa_plsql_datatypes.NumTabTyp;
42         g_tab_project_id                        pa_plsql_datatypes.IdTabTyp;
43         g_tab_task_id                           pa_plsql_datatypes.IdTabTyp;
44         g_tab_exp_type                          pa_plsql_datatypes.char50TabTyp;
45         g_tab_exp_org_id                        pa_plsql_datatypes.IdTabTyp;
46         g_tab_exp_item_date                     pa_plsql_datatypes.DateTabTyp;
47         g_tab_set_of_books_id 			pa_plsql_datatypes.Idtabtyp;
48         g_tab_je_source_name 			pa_plsql_datatypes.char50TabTyp;
49         g_tab_je_category_name   		pa_plsql_datatypes.char50TabTyp;
50         g_tab_doc_type                          pa_plsql_datatypes.Char50TabTyp;
51         g_tab_doc_header_id                     pa_plsql_datatypes.IdTabTyp;
52         g_tab_doc_line_id                       pa_plsql_datatypes.IdTabTyp;
53         g_tab_doc_distribution_id               pa_plsql_datatypes.IdTabTyp;
54         g_tab_inv_distribution_id               pa_plsql_datatypes.IdTabTyp;
55         g_tab_actual_flag                       pa_plsql_datatypes.char50TabTyp;
56         g_tab_result_code                       pa_plsql_datatypes.char50TabTyp;
57         g_tab_status_code                       pa_plsql_datatypes.char50TabTyp;
58         g_tab_entered_dr                        pa_plsql_datatypes.NumTabTyp;
59         g_tab_entered_cr                        pa_plsql_datatypes.NumTabTyp;
60         g_tab_accounted_dr                      pa_plsql_datatypes.NumTabTyp;
61         g_tab_accounted_cr                      pa_plsql_datatypes.NumTabTyp;
62         g_tab_balance_posted_flag		pa_plsql_datatypes.char50TabTyp;
63         g_tab_funds_process_mode		pa_plsql_datatypes.char50TabTyp;
64         g_tab_burden_cost_flag			pa_plsql_datatypes.char50TabTyp;
65         g_tab_org_id				pa_plsql_datatypes.Idtabtyp;
66 
67         -------->6599207 ------As part of CC Enhancements
68 	------------------------------------------------------------------------
69 	/*  Added the following Global Variables which are used in
70 	    populate_plsql_tabs_CBC, create_CBC_pkt_lines          */
71         ------------------------------------------------------------------------
72 	g_tab_reference1                        pa_plsql_datatypes.char80TabTyp;
73         g_tab_reference2                        pa_plsql_datatypes.char80TabTyp;
74         g_tab_reference3                        pa_plsql_datatypes.char80TabTyp;
75 	g_tab_period_year                       pa_plsql_datatypes.NumTabTyp;
76 	g_tab_period_num                        pa_plsql_datatypes.NumTabTyp;
77         g_tab_reference5                        pa_plsql_datatypes.char80TabTyp;
78         g_tab_reference4                        pa_plsql_datatypes.char80TabTyp;
79 	g_tab_rowid                             pa_plsql_datatypes.RowidTabTyp;
80 	g_tab_last_update_date			pa_plsql_datatypes.datetabtyp;
81         g_tab_last_updated_by			pa_plsql_datatypes.NumTabTyp;
82 	------------------------------------------------------------------------
83 	-------->6599207 ------END
84 
85         g_tab_pkt_reference1             	pa_plsql_datatypes.char80TabTyp;
86         g_tab_pkt_reference2             	pa_plsql_datatypes.char80TabTyp;
87         g_tab_pkt_reference3             	pa_plsql_datatypes.char80TabTyp;
88         g_tab_event_id                          pa_plsql_datatypes.Idtabtyp;
89 	g_tab_vendor_id                         pa_plsql_datatypes.Idtabtyp;
90         g_tab_burden_method_code                pa_plsql_datatypes.char30TabTyp;
91         g_tab_main_or_backing_code              pa_plsql_datatypes.char30TabTyp;
92         g_tab_source_event_id                   pa_plsql_datatypes.Idtabtyp;
93         g_tab_trxn_ccid                         pa_plsql_datatypes.Idtabtyp;
94         g_tab_p_bc_packet_id                    pa_plsql_datatypes.IdTabTyp;
95 	g_tab_fck_reqd_flag			pa_plsql_datatypes.char50TabTyp;
96         g_tab_ap_quantity_variance              pa_plsql_datatypes.NumTabTyp;
97         g_tab_ap_amount_variance                pa_plsql_datatypes.NumTabTyp;
98         g_tab_ap_base_qty_variance              pa_plsql_datatypes.NumTabTyp;
99         g_tab_ap_base_amount_variance           pa_plsql_datatypes.NumTabTyp;
100         g_tab_ap_po_distribution_id             pa_plsql_datatypes.IdTabTyp;
101 	g_tab_gl_date                           pa_plsql_datatypes.DateTabTyp;
102 	g_tab_period_name                       pa_plsql_datatypes.char15TabTyp;
103 	g_tab_entered_amount                    pa_plsql_datatypes.NumTabTyp;
104 	g_tab_accted_amount                     pa_plsql_datatypes.NumTabTyp;
105 	g_tab_event_type_code                   pa_plsql_datatypes.char30TabTyp;
106 	g_tab_po_release_id                     pa_plsql_datatypes.IdTabTyp;
107 	g_tab_distribution_type                 pa_plsql_datatypes.char30TabTyp;
108 	g_tab_enc_type_id                       pa_plsql_datatypes.IdTabTyp;
109 	g_line_type_lookup_code                 pa_plsql_datatypes.char30TabTyp;
110 	g_tab_rate                              pa_plsql_datatypes.NumTabTyp; -- Bug 5665232
111         g_tab_bc_packet_id                      pa_plsql_datatypes.IdTabTyp; -- Bug 5406690
112 	g_tab_parent_reversal_id                pa_plsql_datatypes.IdTabTyp; -- Bug 5406690
113 
114         -- Bug 5403775 : Added below columns to derive pa bc pkts reference columns for PO backing docs
115 	g_tab_orig_sequence_num                 pa_plsql_datatypes.NumTabTyp;
116 	g_tab_applied_to_dist_id_2              pa_plsql_datatypes.NumTabTyp;
117 
118         -- Bug 5169409 : R12 Funds management Uptake : This nested table introduced
119         -- in patype01.sql
120 	g_ap_inv_dist_id                        T_PROJ_BC_AP_DIST := T_PROJ_BC_AP_DIST();
121         g_ap_line_type_lkup                     pa_plsql_datatypes.char30TabTyp;
122 
123 	-- Bug : 3703180 changes.
124 	g_cwk_po_unreserve                      varchar2(1) ;
125 	g_doc_line_id_tab                       pa_plsql_datatypes.IdTabTyp;
126 	g_bdamt_balance_tab                     pa_plsql_datatypes.NumTabTyp;
127 	g_project_id_tab                        pa_plsql_datatypes.IdTabTyp ;
128 	g_task_id_tab                           pa_plsql_datatypes.IdTabTyp ;
129 	g_burden_type_tab                       pa_plsql_datatypes.char15TabTyp;
130 
131 --------------------------------------------------------------------------
132 -- This api initializes the pl/sql tables
133 --------------------------------------------------------------------------
134 PROCEDURE init_plsql_tabs  IS
135 
136 BEGIN
137         g_tab_budget_version_id.delete;
138         g_tab_budget_line_id.delete;
139         g_tab_budget_ccid.delete;
140         g_tab_project_id.delete;
141         g_tab_task_id.delete;
142         g_tab_exp_type.delete;
143         g_tab_exp_org_id.delete;
144         g_tab_exp_item_date.delete;
145         g_tab_set_of_books_id.delete;
146         g_tab_je_source_name.delete;
147         g_tab_je_category_name.delete;
148         g_tab_doc_type.delete;
149         g_tab_doc_header_id.delete;
150         g_tab_doc_line_id.delete;
151         g_tab_doc_distribution_id.delete;
152 	g_tab_inv_distribution_id.delete;
153         g_tab_actual_flag.delete;
154         g_tab_result_code.delete;
155         g_tab_status_code.delete;
156         g_tab_entered_dr.delete;
157         g_tab_entered_cr.delete;
158         g_tab_accounted_dr.delete;
159         g_tab_accounted_cr.delete;
160         g_tab_balance_posted_flag.delete;
161         g_tab_funds_process_mode.delete;
162         g_tab_burden_cost_flag.delete;
163         g_tab_org_id.delete;
164         g_tab_pkt_reference1.delete;
165         g_tab_pkt_reference2.delete;
166         g_tab_pkt_reference3.delete;
167         g_tab_event_id.delete;
168 	g_tab_vendor_id.delete;
169         g_tab_burden_method_code.delete;
170         g_tab_main_or_backing_code.delete;
171         g_tab_source_event_id.delete;
172         g_tab_trxn_ccid.delete;
173         g_tab_p_bc_packet_id.delete;
174 	g_tab_fck_reqd_flag.delete;
175         g_tab_ap_quantity_variance.delete;
176         g_tab_ap_amount_variance.delete;
177         g_tab_ap_base_qty_variance.delete;
178         g_tab_ap_base_amount_variance.delete;
179         g_tab_ap_po_distribution_id.delete;
180 	g_tab_gl_date.delete;
181 	g_tab_period_name.delete;
182  	g_tab_entered_amount.delete;
183 	g_tab_accted_amount.delete;
184 	g_tab_event_type_code.delete;
185         g_tab_po_release_id.delete;
186 	g_tab_distribution_type.delete;
187         g_tab_enc_type_id.delete;
188 	g_line_type_lookup_code.delete;
189 	g_ap_line_type_lkup.delete;
190 	g_tab_orig_sequence_num.delete;  -- Bug 5403775
191 	g_tab_applied_to_dist_id_2.delete;
192 	g_tab_rate.delete; -- Bug 5665232
193         g_tab_bc_packet_id.delete; -- Bug 5406690
194 	g_tab_parent_reversal_id.delete; -- Bug 5406690
195 
196         -------->6599207 ------As part of CC Enhancements
197 	g_tab_reference1.delete;
198 	g_tab_reference2.delete;
199 	g_tab_reference3.delete;
200 	g_tab_reference4.delete;
201 	g_tab_reference5.delete;
202 	g_tab_rowid.delete;
203 	g_tab_period_num.delete;
204 	g_tab_period_year.delete;
205 	g_tab_last_update_date.delete;
206 	g_tab_last_updated_by.delete;
207         -------->6599207 ------END
208 
209 EXCEPTION
210 
211         WHEN OTHERS THEN
212                 --commit;
213                 RAISE;
214 END init_plsql_tabs;
215 
216 PROCEDURE init_globals IS
217 
218 BEGIN
219 
220 	g_base_pre_task_id    := null;
221 	g_base_pre_exp_type   := null;
222 	g_base_pre_ei_date    := null;
223 	g_pre_base            := null;
224 	g_pre_cp_structure    := null;
225 	g_base_error_msg_code := null;
226 	g_base_return_status  := null;
227         g_cache_period_sob    := null;
228         g_cache_period_pa_date := null;
229         g_cache_period_name    := null;
230 
231 	g_sd_project_id         := null;
232 	g_sd_bdgt_version_id    := null;
233 	g_sd_tm_phase_code      := null;
234 	g_sd_amt_type           := null;
235 	g_sd_boundary_code      := null;
236 	g_sd_sob                := null;
237 	g_start_date            := null;
238 	g_end_date              := null;
239 	g_sd_result_code        := null;
240 
241 END init_globals;
242 
243 procedure print_msg(p_msg_token1 in varchar2) IS
244 
245 begin
246 	--dbms_output.put_line(p_msg_token1);
247 	null;
248 end;
249 
250 -------->6599207 ------As part of CC Enhancements -- Added parameter p_calling_module
251 -- forward declaration
252 PROCEDURE Load_pkts (p_packet_id IN NUMBER,
253                      p_bc_mode   IN VARCHAR2,
254 		     p_calling_module IN VARCHAR2 DEFAULT NULL) ;
255 -------->6599207 ------END
256 
257 PROCEDURE update_cwk_pkt_lines(p_calling_module   IN varchar2,
258                                p_packet_id        IN NUMBER);
259 
260 -----------------------------------------------------------------------------------------------------
261 -- R12:Funds Managment Uptake: Deleting obsolete function get_period_name definition
262 
263 -- R12:Funds Managment Uptake: Deleting obsolete PROCEDURE checkCWKbdExp definition
264 
265 
266 /* The APi will return the compiled multiplier that needs to be stamped on the summary
267  * record line for the contingent worker transactions
268  */
269 FUNCTION get_cwk_multiplier(p_project_id 	IN Number
270 			,p_task_id              IN Number
271 			,p_budget_version_id    IN Number
272 			,p_document_line_id     IN Number
273 			,p_document_type        IN Varchar2
274 			,p_expenditure_type     IN Varchar2
275 			,p_bd_disp_method       IN Varchar2
276 			,p_reference 		IN Varchar2  default 'GL'
277 			) Return Number IS
278 
279 	l_cwk_multiplier  Number :=0;
280 	l_combdamt        Number :=0;
281 	l_comrawamt       Number :=0;
282 	l_pktbdamt        Number :=0;
283 	l_pktrawamt       Number :=0;
284 	l_tot_rawamt      Number :=0;
285 	l_tot_bdamt       Number :=0;
286 BEGIN
287 	pa_funds_control_pkg.log_message(p_msg_token1 => 'Inside get_cwk_multiplier API');
288 	If p_reference = 'GL' Then
289 
290          	select sum(decode(com.parent_bc_packet_id, NULL, 0
291                                 ,decode(p_bd_disp_method,'D', decode(com.expenditure_type,p_expenditure_type
292                                                         ,(nvl(com.accounted_dr,0) - nvl(com.accounted_cr,0)),0)
293                                              ,(nvl(com.accounted_dr,0) - nvl(com.accounted_cr,0)))
294                           ))   ComBdAmt
295                       ,sum(decode(com.parent_bc_packet_id, NULL,(nvl(com.accounted_dr,0) - nvl(com.accounted_cr,0)),0)
296                           )   ComRawAmt
297 		Into 	l_combdamt
298 			,l_comrawamt
299 		from pa_bc_commitments_all com
300 		where com.project_id = p_project_id
301 		and   com.task_id   = p_task_id
302 		and   com.budget_version_id = p_budget_version_id
303 		and   com.document_line_id = p_document_line_id
304 		and   com.document_type =  p_document_type ;
305 
306                 select sum(decode(pkt.parent_bc_packet_id, NULL, 0
307 				,decode(p_bd_disp_method,'D', decode(pkt.expenditure_type,p_expenditure_type
308                     				 	,(nvl(pkt.accounted_dr,0) - nvl(pkt.accounted_cr,0)),0)
309 		  			     ,(nvl(pkt.accounted_dr,0) - nvl(pkt.accounted_cr,0)))
310            		  ))   pktBdAmt
311         	      ,sum(decode(pkt.parent_bc_packet_id, NULL,(nvl(pkt.accounted_dr,0) - nvl(pkt.accounted_cr,0)),0)
312                           )   pktRawAmt
313                 Into    l_pktbdamt
314                         ,l_pktrawamt
315                 from pa_bc_packets pkt
316 		Where pkt.project_id = p_project_id
317                 and   pkt.task_id = p_task_id
318                 and   pkt.budget_version_id = p_budget_version_id
319                 and   pkt.document_line_id = p_document_line_id
320                 and   pkt.document_type = p_document_type
321                 and   pkt.status_code in ('A','C')
322                 and   nvl(pkt.balance_posted_flag,'N') <> 'Y'
323 		and   nvl(pkt.funds_process_mode,'N') = 'T'
324                 and   substr(nvl(pkt.result_code,'P'),1,1) = 'P';
325 
326 		l_tot_rawamt := NVL(l_comrawamt,0) + nvl(l_pktrawamt,0);
327 		l_tot_bdamt  := nvl(l_combdamt,0)  + nvl(l_pktbdamt,0) ;
328 
329 		pa_funds_control_pkg.log_message(p_msg_token1 => 'l_comrawamt['||l_comrawamt||']l_pktrawamt['||l_pktrawamt||
330 				']l_combdamt['||l_combdamt||']l_pktbdamt['||l_pktbdamt||']' );
331 		If l_tot_rawamt = 0 Then
332 			--divisor is zero so return the multiplier zero
333 			l_cwk_multiplier := 0;
334 		Else
335 			l_cwk_multiplier := l_tot_bdamt / l_tot_rawamt ;
336 		End If;
337 
338 	END If;
339 
340 	pa_funds_control_pkg.log_message(p_msg_token1 => 'End of cwk Multiplier Value['||l_cwk_multiplier||']');
341 
342 	RETURN l_cwk_multiplier;
343 EXCEPTION
344 
345 	WHEN OTHERS THEN
346 		pa_funds_control_pkg.log_message(p_msg_token1 => 'Failed in cwk Multiplier API');
347 		RAISE;
348 
349 END get_cwk_multiplier;
350 -----------------------------------------------------------------------------
351 -- This API checks whether the given expenditure is part of the
352 -- the cost base or not if not then Expenditure is no burdening
353 -- hence the burden lines will not populated.
354 -- The Out parmas x_return_status = 'S' for Success
355 ----------------------------------------------------------------------------
356 PROCEDURE   check_exp_of_cost_base(p_task_id    IN  number,
357                                    p_exp_type   IN  varchar2,
358                                    p_ei_date    IN  date,
359                                    p_sch_type   IN  varchar2 default 'C',
360                                    x_base             OUT NOCOPY varchar2,
361                                    x_cp_structure     OUT NOCOPY varchar2,
362                                    x_return_status    OUT NOCOPY varchar2,
363                                    x_error_msg_code   OUT NOCOPY varchar2) IS
364 
365         l_sch_id    NUMBER;
366         l_sch_date  date;
367         l_base      VARCHAR2(100);
368         l_rate_sch_rev_id NUMBER;
369         l_cp_structure  varchar2(100) ;
370         l_return_status  varchar2(1000);
371         l_stage   varchar2(100);
372 
373 BEGIN
374 
375         x_base  := NULL;
376         x_return_status := 'S';
377         x_error_msg_code  := null;
378 
379         pa_funds_control_pkg.log_message(p_msg_token1 =>
380                 'Inside check_exp_of_cost_base api In parms are  task id ['||
381                  p_task_id||']exp_type ['||p_exp_type||'] ei_date[ '||p_ei_date||']' );
382 
383         IF (g_base_pre_task_id is null or g_base_pre_task_id <> p_task_id) OR
384            (g_base_pre_exp_type is null or g_base_pre_exp_type <> p_exp_type) OR
385            (g_base_pre_ei_date is null or trunc(g_base_pre_ei_date) <> trunc(p_ei_date) ) THEN
386                 pa_funds_control_pkg.log_message(p_msg_token1 =>'Inside if condition differnt ');
387 	      pa_funds_control_pkg.log_message(p_msg_token1 =>'Value of p_sch_type= '||p_sch_type);
388          If p_sch_type  = 'C' then
389                 BEGIN
390 			-- Select the Task level schedule override if not found
391                         -- then select the Project level override
392                         SELECT irs.ind_rate_sch_id,
393                                 t.cost_ind_sch_fixed_date
394                         INTO   l_sch_id,l_sch_date
395                         FROM   pa_tasks t,
396                                 pa_ind_rate_schedules irs
397                         WHERE  t.task_id = p_task_id
398                         AND    t.task_id = irs.task_id
399                         AND    irs.cost_ovr_sch_flag = 'Y';
400 
401                 EXCEPTION
402 
403                         WHEN NO_DATA_FOUND then
404                                 -- Select the project level sch override
405                                 BEGIN
406                                         SELECT irs.ind_rate_sch_id,
407                                                 p.cost_ind_sch_fixed_date
408                                         INTO   l_sch_id,l_sch_date
409                                         FROM   pa_tasks t,
410                                                 pa_projects_all p,
411                                                 pa_ind_rate_schedules irs
412                                         WHERE  t.task_id = p_task_id
413                                         AND    t.project_id = p.project_id
414                                         AND    t.project_id = irs.project_id
415                                         AND    irs.cost_ovr_sch_flag = 'Y'
416                                         AND    irs.task_id is null;
417                                 EXCEPTION
418 
419                                         WHEN NO_DATA_FOUND THEN
420                                                 -- select the schedule at the task
421                                                 BEGIN
422                                                     SELECT  t.cost_ind_rate_sch_id,
423                                                         t.cost_ind_sch_fixed_date
424                                                     INTO    l_sch_id ,l_sch_date
425                                                     FROM    pa_tasks t,
426                                                         pa_ind_rate_schedules irs
427                                                     WHERE   t.task_id = p_task_id
428                                                     AND     t.cost_ind_rate_sch_id = irs.ind_rate_sch_id;
429                                                 EXCEPTION
430 
431                                                         WHEN OTHERS THEN
432 								x_error_msg_code := 'NO_IND_RATE_SCH_ID';
433 								x_base  := NULL;
434 								x_return_status := 'F';
435 
436                                                 END;
437                                 END;
438 
439                         WHEN OTHERS THEN
440 
441                                 x_error_msg_code := 'NO_IND_RATE_SCH_ID';
442                                 x_base  := NULL;
443                                 x_return_status := 'F';
444                 END;
445 
446                 pa_funds_control_pkg.log_message(p_msg_token1=>'l_sch_id = '||l_sch_id);
447                 If  l_sch_id is NOT NULL then
448                         pa_funds_control_pkg.log_message(p_msg_token1=>
449                                 'calling pa_cost_plus.get_revision_by_date');
450 
451                         pa_cost_plus.get_revision_by_date
452                                 (l_sch_id
453                                 ,l_sch_date
454                                 ,p_ei_date
455                                 ,l_rate_sch_rev_id
456                                 ,l_return_status
457                                 ,l_stage);
458                          pa_funds_control_pkg.log_message(p_msg_token1 =>
459                                 'After Calling pa_cost_plus.get_revision_by_date, l_return_status '||l_return_status);
460                         IF l_rate_sch_rev_id is NULL then
461                                 x_error_msg_code :=  'NO_IND_RATE_SCH_REVISION';
462                                 x_base  := NULL;
463                                 x_return_status := 'F';
464                         END IF;
465                 END IF;
466                 pa_funds_control_pkg.log_message(p_msg_token1=>'l_rate_sch_rev_id = '||l_rate_sch_rev_id);
467                 IF l_rate_sch_rev_id is NOT NULL then
468 
469 			pa_funds_control_pkg.log_message(p_msg_token1=>
470                                 'calling pa_cost_plus.get_cost_plus_structure');
471 
472                         pa_cost_plus.get_cost_plus_structure
473                         (rate_sch_rev_id  =>l_rate_sch_rev_id
474                          ,cp_structure    =>l_cp_structure
475                          ,status           =>l_return_status
476                          ,stage            =>l_stage);
477 
478                          pa_funds_control_pkg.log_message(p_msg_token1 =>
479                                 'After Calling pa_cost_plus.get_cost_plus_structure, l_return_status '||l_return_status);
480 
481                         IF l_cp_structure is NULL then
482                                 x_error_msg_code := 'NO_COST_PLUS_STRUCTURE';
483                                 x_base  := NULL;
484                                 x_return_status := 'F';
485 
486                         End if;
487                 End IF;
488                 pa_funds_control_pkg.log_message(p_msg_token1=>'cost plus structure ='||l_cp_structure);
489                 l_base := null;
490                 IF l_cp_structure is NOT NULL and p_exp_type is NOT NULL then
491 
492 			pa_funds_control_pkg.log_message(p_msg_token1=>
493                                 'calling pa_cost_plus.get_cost_base');
494 
495                         pa_cost_plus.get_cost_base
496                         (exp_type         => p_exp_type
497                          ,cp_structure     => l_cp_structure
498                          ,c_base           => l_base
499                          ,status           => l_return_status
500                          ,stage            => l_stage);
501                         pa_funds_control_pkg.log_message(p_msg_token1=>'l_base ='||l_base);
502 
503                          pa_funds_control_pkg.log_message(p_msg_token1 =>
504                                 'After Calling pa_cost_plus.get_cost_base, l_return_status and l_base are '||l_return_status||','||l_base);
505 
506                         x_base := l_base;
507 			x_cp_structure := l_cp_structure;
508                         If l_base is NULL then
509                                 -- the expenditure type is not part of the
510                                 -- cost base so burdened cost is same as raw cost
511                                 -- or burden cost is zero
512                                 x_return_status := 'S';
513                                 x_error_msg_code := null;
514                         End if;
515 
516                 END IF;
517 
518                 End if; --end of schedule type
519                 g_base_return_status := x_return_status;
520                 g_base_error_msg_code := x_error_msg_code;
521                 g_base_pre_task_id := p_task_id;
522                 g_base_pre_ei_date := p_ei_date;
523                 g_base_pre_exp_type := p_exp_type;
524                 g_pre_base := x_base;
525 		g_pre_cp_structure := x_cp_structure;
526 
527         Else  -- pre cache
528                 pa_funds_control_pkg.log_message(p_msg_token1=>'Pre cached values');
529                 x_return_status := g_base_return_status;
530                 x_error_msg_code := g_base_error_msg_code;
531                 x_base   := g_pre_base;
532 		x_cp_structure := g_pre_cp_structure;
533         End if;
534 
535                 pa_funds_control_pkg.log_message(p_msg_token1=>'x_msg_code[ '||x_error_msg_code||
536                 ']x_retun status [ '||x_return_status||']x_base ['||x_base||
537                 ']g_base_return_status ['||g_base_return_status|| ']g_base_error_msg_code['||g_base_error_msg_code||
538                 ']g_base_pre_task_id ['||g_base_pre_task_id||']g_base_pre_ei_date['||g_base_pre_ei_date||
539                 ']g_base_pre_exp_type['||g_base_pre_exp_type||'] cp structure['||g_pre_cp_structure||']' );
540 
541         RETURN;
542 
543 EXCEPTION
544 
545         WHEN OTHERS THEN
546                 pa_funds_control_pkg.log_message
547                 (p_msg_token2 => 'Failed in check_exp_of_cost_base api');
548                 pa_funds_control_pkg.log_message
549                 (p_msg_token2 => sqlcode||sqlerrm);
550 
551                 --R12: NOCOPY changes
552 	        x_base  := NULL;
553 		x_cp_structure := NULL;
554                 x_return_status := 'F';
555                 x_error_msg_code  := SQLCODE||SQLERRM;
556 
557                 RAISE;
558 END check_exp_of_cost_base;
559 
560 /* This api will update the summary level flag, compiled multiplier etc
561  * attributes required for contingent worker related transactions
562  */
563 PROCEDURE upd_cwk_attributes(p_calling_module  varchar2
564 			,p_packet_id   number
565 			,p_mode        varchar2
566 			,p_reference   varchar2
567 			,x_return_status OUT NOCOPY varchar2 )
568 
569 
570 IS
571 	--PRAGMA AUTONOMOUS_TRANSACTION;
572         l_rows_updated   Number := 0;
573         l_commsummrec    Varchar2(1) := 'N';
574         l_pktsummrec     Varchar2(1) := 'N';
575 	l_cwk_multiplier Number;
576 	l_po_exists      Varchar2(1) := 'N';
577 	l_stage          varchar2(1000);
578 	l_comm_raw_amt       	Number ;
579         l_comm_bd_amt		Number ;
580         l_relvd_comm_raw_amt	Number ;
581         l_relvd_comm_bd_amt	Number ;
582 
583 	-- curosr to check po record exists in packets before updating the summary record flag
584         cursor cur_potrxs IS
585 	select 'Y'
586 	from dual
587 	where exists (select null
588 		      from pa_bc_packets pkt
589 		      where pkt.packet_id = p_packet_id
590 		      and  pkt.document_type = 'PO'
591 		     );
592 
593 	--cursor to select distinct cwk records in current pkt
594 	cursor cur_cwkRecs IS
595 	select  pkt.project_id
596 		,pkt.task_id
597 		,pkt.budget_version_id
598 		,pkt.document_line_id
599 		,NVL(pt.burden_amt_display_method,'N') burden_amt_display_method
600 		,decode(pt.burden_amt_display_method,'D'
601 			,decode(pkt.parent_bc_packet_id,NULL,NULL,pkt.expenditure_type)
602 				,NULL) expenditure_type
603 		,decode(pt.burden_amt_display_method,'D'
604 			,decode(pkt.parent_bc_packet_id,NULL,'RAW','BURDEN')
605 			 	,'RAW')  line_type
606 	from pa_bc_packets pkt
607 	    ,pa_projects_all pp
608 	    ,pa_project_types_all pt
609 	where pkt.packet_id = p_packet_id
610 	and   pkt.document_line_id is NOT NULL -- with R12 this check is not sufficient to find if the PO is an CWK PO
611 	and   pa_funds_control_utils2.is_CWK_PO(pkt.document_header_id,pkt.document_line_id
612 						  ,pkt.document_distribution_id,pkt.org_id) = 'Y' -- R12 Funds management uptake
613 	and   pkt.document_type in ('PO') --,'EXP')
614 	and   pkt.status_code in ('P','A','C','I')
615 	and   substr(NVL(pkt.result_code,'P'),1,1) = 'P'
616 	and   pt.project_type = pp.project_type
617 	and   pt.org_id = pp.org_id  --R12 Ledger change : Removed NVL clause
618 	and   pp.project_id = pkt.project_id
619 	Group By
620 		pkt.project_id
621                 ,pkt.task_id
622                 ,pkt.budget_version_id
623                 ,pkt.document_line_id
624                 ,NVL(pt.burden_amt_display_method,'N')
625                 ,decode(pt.burden_amt_display_method,'D'
626                         ,decode(pkt.parent_bc_packet_id,NULL,NULL,pkt.expenditure_type)
627                                 ,NULL)
628                 ,decode(pt.burden_amt_display_method,'D'
629                         ,decode(pkt.parent_bc_packet_id,NULL,'RAW','BURDEN')
630                                 ,'RAW');
631 
632 	--cursor to check summary record exists in bc_commitments
633 	cursor cur_commsummrec (l_project_id Number
634 				,l_task_id Number
635 				,l_budget_version_id Number
636 				,l_document_line_id  Number
637 				,l_expenditure_type varchar2
638 				,l_bd_disp_method varchar2
639 				,l_line_type      varchar2 ) IS
640 	select 'Y'
641 	from dual
642 	Where exists
643 		(select null
644 		from pa_bc_commitments_all comm
645 		where comm.project_id = l_project_id
646 		and   comm.task_id = l_task_id
647 		and   comm.budget_version_id = l_budget_version_id
648 		and   comm.document_line_id = l_document_line_id
649 		and   comm.summary_record_flag = 'Y'
650 		and   comm.document_type = 'PO'
651 		and   ((l_bd_disp_method = 'D'
652 			and comm.expenditure_type = l_expenditure_type
653 			and comm.parent_bc_packet_id is NOT NULL)
654 			OR ( l_bd_disp_method = 'D'
655 			    and comm.parent_bc_packet_id is NULL
656 			    and l_line_type = 'RAW' )
657 			OR
658 			( l_bd_disp_method <> 'D')
659 		      )
660 		);
661 
662 	--cursor to check summary record exists in pkts which is not yet swept
663 	cursor cur_pktsummrec (l_project_id Number
664                                 ,l_task_id Number
665                                 ,l_budget_version_id Number
666 				,l_document_line_id  Number
667 				,l_expenditure_type varchar2
668                                 ,l_bd_disp_method varchar2
669 				,l_line_type  varchar2 ) IS
670 	select 'Y'
671 	from dual
672 	where exists
673                 (select null
674                 from pa_bc_packets pkts1
675                 where pkts1.document_line_id is NOT NULL
676 		and   pkts1.status_code in ('A','P','C','I')
677 		and   substr(NVL(pkts1.result_code,'P'),1,1) = 'P'
678 		and   nvl(pkts1.funds_process_mode,'T') <> 'B'
679 		and   nvl(pkts1.balance_posted_flag,'N') <> 'Y'
680 		and   pkts1.project_id = l_project_id
681 		and   pkts1.task_id = l_task_id
682 		and   pkts1.budget_version_id = l_budget_version_id
683 		and   pkts1.document_line_id = l_document_line_id
684 		and   pkts1.document_type = 'PO'
685 		and   pkts1.summary_record_flag = 'Y'
686 		and   ((l_bd_disp_method = 'D'
687                         and pkts1.expenditure_type = l_expenditure_type
688 			and pkts1.parent_bc_packet_id is NOT NULL)
689                         OR ( l_bd_disp_method = 'D'
690                             and pkts1.parent_bc_packet_id is NULL
691 			    and l_line_type = 'RAW' )
692                         OR
693                         (l_bd_disp_method <> 'D')
694                       )
695                 );
696 
697 	-- cursor brings the commitments amounts for the given summary record, which needs to be stamped if the
698 	-- packet staus is success.
699 	CURSOR cur_cwk_amts(lv_project_id Number
700 			,lv_budget_version_id Number
701 			,lv_task_id  Number
702 			,lv_document_line_id Number
703 			,lv_expenditure_type varchar2
704 			,lv_bd_disp_method  varchar2) IS
705 	SELECT 	sum((nvl(accounted_dr,0) - nvl(accounted_cr,0)) *
706 			decode(pkt.document_type,'PO',decode(pkt.parent_bc_packet_id,NULL,1,0),0)) comm_raw_amt
707 		,sum((nvl(accounted_dr,0) - nvl(accounted_cr,0)) *
708 			decode(pkt.document_type,'PO'
709 			  ,decode(pkt.parent_bc_packet_id, NULL ,0
710 			    ,decode(lv_bd_disp_method, 'D'
711 			      ,decode(pkt.expenditure_type,lv_expenditure_type,1,0),1)),0)) comm_bd_amt
712 		,sum((nvl(accounted_dr,0) - nvl(accounted_cr,0)) *
713 			decode(pkt.document_type,'PO',decode(pkt.parent_bc_packet_id,NULL,1,0),0)) relevd_comm_raw_amt
714                 ,sum((nvl(accounted_dr,0) - nvl(accounted_cr,0)) *
715                         decode(pkt.document_type,'PO'
716                           ,decode(pkt.parent_bc_packet_id,NULL ,0
717                             ,decode(lv_bd_disp_method, 'D'
718                               ,decode(pkt.expenditure_type,lv_expenditure_type,1,0),1)),0)) relevd_comm_bd_amt
719         FROM   pa_bc_packets pkt
720         WHERE  pkt.project_id = lv_project_id
721         AND    pkt.budget_version_id = lv_budget_version_id
722         AND    pkt.task_id = lv_task_id
723         AND    pkt.document_line_id = lv_document_line_id
724         AND    substr(nvl(pkt.result_code,'P'),1,1) = 'P'
725         AND    pkt.status_code in ('A','C','B')
726         AND    pkt.document_type in ('PO') --,'EXP')
727         AND    nvl(pkt.balance_posted_flag,'N') = 'N'
728         AND    nvl(pkt.funds_process_mode,'N') = 'T'
729         AND    pkt.packet_id = p_packet_id ;
730 
731 BEGIN
732 	x_return_status := 'S';
733 
734 	/* Logic: if the summary level record already exists in bc_commitments_all table
735 	 * then update the record with amt columns, If no record exists in bc_commiemtns or
736 	 * the record exists in bc_packets which is not yet swept then update the amts only
737 	 * If the summary record is creating first time then update all the relevent columns
738 	 */
739  	pa_funds_control_pkg.log_message(p_msg_token1 =>'Inside upd_cwk_attributes API params:packetId['||p_packet_id||
740 			']mode['||p_mode||']callingModule['||p_calling_module||']Reference['||p_reference||']');
741 
742 	IF p_calling_module NOT IN ('CBC','CHECK_BASELINE','RESERVE_BASELINE') Then
743 
744         -- initialize the accounting currency code,
745         pa_multi_currency.init;
746 
747         --Get the accounting currency into a global variable.
748         g_acct_currency_code := pa_multi_currency.g_accounting_currency_code;
749 
750 	  --loop through each project, task, document line and expenditure type and update the cwk attributes
751 	  FOR cwk IN cur_cwkRecs LOOP
752 
753 
754 		OPEN cur_commsummrec(l_project_id  => cwk.project_id
755                                 ,l_task_id 	   => cwk.task_id
756                                 ,l_budget_version_id => cwk.budget_version_id
757                                 ,l_document_line_id  => cwk.document_line_id
758                                 ,l_expenditure_type  => cwk.expenditure_type
759                                 ,l_bd_disp_method    => cwk.burden_amt_display_method
760 				,l_line_type         => cwk.line_type );
761 		FETCH cur_commsummrec INTO l_commsummrec;
762 		IF cur_commsummrec%NOTFOUND Then
763 			OPEN cur_pktsummrec(l_project_id  => cwk.project_id
764                                 ,l_task_id         => cwk.task_id
765                                 ,l_budget_version_id => cwk.budget_version_id
766                                 ,l_document_line_id  => cwk.document_line_id
767                                 ,l_expenditure_type  => cwk.expenditure_type
768                                 ,l_bd_disp_method    => cwk.burden_amt_display_method
769 				,l_line_type         => cwk.line_type ) ;
770 			FETCH cur_pktsummrec INTO l_pktsummrec;
771 			IF cur_pktsummrec%NOTFOUND Then
772 				l_pktsummrec := 'N';
773 			End If;
774 			CLOSE cur_pktsummrec;
775 		End If;
776 		CLOSE cur_commsummrec;
777 
778 		OPEN cur_potrxs;
779 		FETCH cur_potrxs INTO l_po_exists;
780 		CLOSE cur_potrxs;
781 
782 	    pa_funds_control_pkg.log_message(p_msg_token1 =>'Project['||cwk.project_id||']Task['||cwk.task_id||
783 						']Budgetver['||cwk.budget_version_id||']DocLineid['||cwk.document_line_id||
784 						']ExpType['||cwk.expenditure_type||']BurdDispMethod['||cwk.burden_amt_display_method||
785 						']CommSumRecflag['||l_commsummrec||']PktSumRecflag['||l_pktsummrec||
786 						']poexistsflag['||l_po_exists||']line_type['||cwk.line_type||']');
787 
788 	    IF p_calling_module in ('GL','DISTCWKST') and p_reference  = 'UPD_AMTS'
789 		and p_mode in ('R','F','U') Then
790 
791 		l_rows_updated := 0;
792 		IF l_commsummrec = 'Y' Then
793 			l_stage := 'Updating commitments cwk amounts';
794 			OPEN cur_cwk_amts(cwk.project_id,cwk.budget_version_id,cwk.task_id,cwk.document_line_id
795 					,cwk.expenditure_type,cwk.burden_amt_display_method);
796 			FETCH cur_cwk_amts INTO l_comm_raw_amt
797 						,l_comm_bd_amt
798 						,l_relvd_comm_raw_amt
799 						,l_relvd_comm_bd_amt ;
800 			CLOSE cur_cwk_amts;
801 			pa_funds_control_pkg.log_message(p_msg_token1 => 'CommRawAmt['||l_comm_raw_amt||']CommbdAmt['||l_comm_bd_amt||
802 						']RelvdComm['||l_relvd_comm_raw_amt||']RelvdBd['||l_relvd_comm_bd_amt||']');
803 			l_rows_updated := 0;
804 		     	UPDATE pa_bc_commitments_all com
805 		      	SET com.comm_tot_raw_amt = nvl(com.comm_tot_raw_amt,0) +
806 						decode(p_calling_module,'GL',decode(cwk.line_type,'RAW',nvl(l_comm_raw_amt,0),0),0)
807 		   	,com.comm_tot_bd_amt = nvl(com.comm_tot_bd_amt,0) +
808 						decode(p_calling_module,'GL'
809                                                  ,decode(cwk.burden_amt_display_method,'D'
810                                                         ,decode(cwk.line_type,'BURDEN',nvl(l_comm_bd_amt,0),0)
811                                                                 ,nvl(l_comm_bd_amt,0)),0)
812 		   	,com.comm_raw_amt_relieved = nvl(com.comm_raw_amt_relieved,0) -
813 						   decode(p_calling_module,'DISTCWKST'
814                                                          ,decode(cwk.line_type,'RAW',nvl(l_relvd_comm_raw_amt,0),0),0)
815 		   	,com.comm_bd_amt_relieved = nvl(com.comm_bd_amt_relieved,0) -
816 							decode(p_calling_module,'DISTCWKST'
817                                                  	 ,decode(cwk.burden_amt_display_method,'D'
818                                                         	,decode(cwk.line_type,'BURDEN',nvl(l_relvd_comm_bd_amt,0),0)
819                                                          	   ,nvl(l_relvd_comm_bd_amt,0)),0)
820 			WHERE com.summary_record_flag = 'Y'
821 			AND   com.document_type = 'PO'
822 			AND   com.document_line_id is not null
823 			AND   com.project_id = cwk.project_id
824 			AND   com.task_id = cwk.task_id
825 			AND   com.budget_version_id = cwk.budget_version_id
826 			AND   com.document_line_id = cwk.document_line_id
827                         AND (( -- burden lines should be stamped with summary record info if display method is different
828                                 com.parent_bc_packet_id is NOT NULL
829                                 and com.expenditure_type = cwk.expenditure_type
830                                 and cwk.line_type = 'BURDEN'
831                                 and cwk.burden_amt_display_method = 'D'
832                              )
833                               OR
834                              ( -- Sep line burden raw(only one line) should be stamped with summary record info
835                                 com.parent_bc_packet_id is NULL
836                                 and cwk.burden_amt_display_method = 'D'
837                                 and cwk.line_type = 'RAW'
838                                 and com.summary_record_flag = 'Y'
839                              )
840                               OR
841                                ( -- same line burden raw line should be stamped with summary record info if display method is same
842                                 com.parent_bc_packet_id is NULL
843                                 and cwk.burden_amt_display_method  <> 'D'
844                                )
845                             );
846 
847 			l_rows_updated := sql%rowcount;
848 
849 
850 		ELSIF l_pktsummrec = 'Y' Then
851 
852 			l_stage := 'Updatng packets cwk amount';
853                         OPEN cur_cwk_amts(cwk.project_id,cwk.budget_version_id,cwk.task_id,cwk.document_line_id
854                                         ,cwk.expenditure_type,cwk.burden_amt_display_method);
855                         FETCH cur_cwk_amts INTO l_comm_raw_amt
856                                                 ,l_comm_bd_amt
857                                                 ,l_relvd_comm_raw_amt
858                                                 ,l_relvd_comm_bd_amt ;
859                         CLOSE cur_cwk_amts;
860  			pa_funds_control_pkg.log_message(p_msg_token1 => 'CommRawAmt['||l_comm_raw_amt||']CommbdAmt['||l_comm_bd_amt||
861                                                 ']RelvdComm['||l_relvd_comm_raw_amt||']RelvdBd['||l_relvd_comm_bd_amt||']');
862 			l_rows_updated := 0;
863 			UPDATE pa_bc_packets pkt
864 			SET pkt.comm_tot_raw_amt = nvl(pkt.comm_tot_raw_amt,0) +
865                                                 decode(p_calling_module,'GL',decode(cwk.line_type,'RAW',nvl(l_comm_raw_amt,0),0),0)
866                         ,pkt.comm_tot_bd_amt = nvl(pkt.comm_tot_bd_amt,0) +
867                                                 decode(p_calling_module,'GL'
868 						 ,decode(cwk.burden_amt_display_method,'D'
869 							,decode(cwk.line_type,'BURDEN',nvl(l_comm_bd_amt,0),0)
870 								,nvl(l_comm_bd_amt,0)),0)
871                         ,pkt.comm_raw_amt_relieved = nvl(pkt.comm_raw_amt_relieved,0) -
872                                                 decode(p_calling_module,'DISTCWKST'
873 							 ,decode(cwk.line_type,'RAW',nvl(l_relvd_comm_raw_amt,0),0),0)
874                         ,pkt.comm_bd_amt_relieved = nvl(pkt.comm_bd_amt_relieved,0) -
875                                                 decode(p_calling_module,'DISTCWKST'
876 						 ,decode(cwk.burden_amt_display_method,'D'
877                                                         ,decode(cwk.line_type,'BURDEN',nvl(l_relvd_comm_bd_amt,0),0)
878 							 ,nvl(l_relvd_comm_bd_amt,0)),0)
879 			WHERE pkt.document_line_id is NOT NULL
880 			AND   pkt.document_type = 'PO'
881 			AND   NVL(pkt.summary_record_flag,'N') = 'Y'
882 			AND   substr(NVL(pkt.result_code,'P'),1,1) = 'P'
883 			AND   nvl(pkt.balance_posted_flag,'N' ) <> 'Y'
884 			AND   nvl(pkt.funds_process_mode,'N') = 'T'
885 			AND   pkt.status_code in ('A','B','C')
886 			AND   pkt.project_id = cwk.project_id
887                         AND   pkt.task_id = cwk.task_id
888                         AND   pkt.budget_version_id = cwk.budget_version_id
889                         AND   pkt.document_line_id = cwk.document_line_id
890                         AND (( -- burden lines should be stamped with summary record info if display method is different
891                                 pkt.parent_bc_packet_id is NOT NULL
892                                 and pkt.expenditure_type = cwk.expenditure_type
893 				and cwk.line_type = 'BURDEN'
894                                 and cwk.burden_amt_display_method = 'D'
895                              )
896 			      OR
897                              ( -- Sep line burden raw(only one line) should be stamped with summary record info
898                                 pkt.parent_bc_packet_id is NULL
899                                 and cwk.burden_amt_display_method = 'D'
900 				and cwk.line_type = 'RAW'
901 				and pkt.summary_record_flag = 'Y'
902                              )
903                               OR
904                                ( -- same line burden raw line should be stamped with summary record info if display method is same
905                                 pkt.parent_bc_packet_id is NULL
906                                 and cwk.burden_amt_display_method  <> 'D'
907                                )
908                             );
909 			l_rows_updated := sql%rowcount;
910 		END IF; -- end of summrecamt
911 
912 	ELSIF p_reference  = 'UPD_FLAG' and p_calling_module = 'GL' and p_mode in ('R','F','U') Then
913 
914 	    l_rows_updated := 0;
915 	    IF nvl(l_commsummrec,'N') = 'N' and nvl(l_pktsummrec,'N') = 'N' and l_po_exists = 'Y' Then
916 		l_stage := 'Updating packets summary record flag';
917 		pa_funds_control_pkg.log_message(p_msg_token1=> l_stage);
918 		UPDATE pa_bc_packets pkt
919 		SET pkt.summary_record_flag = decode (pkt.summary_record_flag,NULL,'Y',pkt.summary_record_flag)
920 		WHERE pkt.packet_id = p_packet_id
921 		AND   pkt.document_type = 'PO'
922 		AND   nvl(pkt.funds_process_mode,'N') = 'T'
923 		AND   nvl(pkt.balance_posted_flag,'N') <> 'Y'
924 		AND   pkt.summary_record_flag is NULL
925 		AND   pkt.project_id = cwk.project_id
926                 AND   pkt.task_id = cwk.task_id
927                 AND   pkt.budget_version_id = cwk.budget_version_id
928                 AND   pkt.document_line_id = cwk.document_line_id
929 		AND   decode(pkt.parent_bc_packet_id,NULL,'RAW','BURDEN') = cwk.line_type
930                 AND (( -- sep line burden cost codes should be stamped with summary record info if display method is different
931                 	pkt.parent_bc_packet_id is NOT NULL
932                 	and pkt.expenditure_type = cwk.expenditure_type
933                 	and cwk.burden_amt_display_method = 'D'
934 			and pkt.bc_packet_id = (select min(pkt1.bc_packet_id)
935 					        from pa_bc_packets pkt1
936 						where pkt1.packet_id = pkt.packet_id
937 						and   pkt1.project_id = pkt.project_id
938 						and   pkt1.task_id = pkt.task_id
939 						and   pkt1.budget_version_id = pkt.budget_version_id
940 						and   pkt1.document_line_id = pkt.document_line_id
941 						and   pkt1.expenditure_type = pkt.expenditure_type
942 						and   pkt1.document_type = pkt.document_type
943 						and   pkt1.parent_bc_packet_id is NOT NULL
944 					       )
945                      )
946 		      OR
947 			( -- sep line burden lines only one raw line should be stamped with summary record info
948                         pkt.parent_bc_packet_id is NULL
949                         and cwk.burden_amt_display_method = 'D'
950                         and pkt.bc_packet_id = (select min(pkt1.bc_packet_id)
951                                                 from pa_bc_packets pkt1
952                                                 where pkt1.packet_id = pkt.packet_id
953                                                 and   pkt1.project_id = pkt.project_id
954                                                 and   pkt1.task_id = pkt.task_id
955                                                 and   pkt1.budget_version_id = pkt.budget_version_id
956                                                 and   pkt1.document_line_id = pkt.document_line_id
957                                                 --and   pkt1.expenditure_type = pkt.expenditure_type
958                                                 and   pkt1.document_type = pkt.document_type
959                                                 and   pkt1.parent_bc_packet_id is NULL
960                                                )
961                      )
962                       OR
963                      ( -- raw line should be stamped with summary record info if display method is same
964                 	pkt.parent_bc_packet_id is NULL
965                 	and cwk.burden_amt_display_method  <> 'D'
966 			and pkt.bc_packet_id = (select min(pkt1.bc_packet_id)
967                                                 from pa_bc_packets pkt1
968                                                 where pkt1.packet_id = pkt.packet_id
969                                                 and   pkt1.project_id = pkt.project_id
970                                                 and   pkt1.task_id = pkt.task_id
971                                                 and   pkt1.budget_version_id = pkt.budget_version_id
972                                                 and   pkt1.document_line_id = pkt.document_line_id
973                                                 and   pkt1.document_type = pkt.document_type
974 						and   pkt1.parent_bc_packet_id is NULL
975                                                )
976                      )
977                    );
978 			l_rows_updated := sql%rowcount;
979 	     END IF;
980 
981 	   ELSIF p_reference  = 'UPD_MULTIPLIER' and p_calling_module = 'GL' and p_mode in ('R','F','U') Then
982 		l_rows_updated := 0;
983 		IF l_commsummrec = 'Y' Then
984 			l_stage  := 'Updating commitments cwk multiplier';
985 			l_cwk_multiplier := get_cwk_multiplier(cwk.project_id
986                                             ,cwk.task_id
987                                             ,cwk.budget_version_id
988                                             ,cwk.document_line_id
989                                             ,'PO'
990                                             ,cwk.expenditure_type
991 					    ,cwk.burden_amt_display_method
992                                             ,'GL');
993                         If l_cwk_multiplier is NOT NULL Then
994                                  l_cwk_multiplier := pa_currency.round_trans_currency_amt
995                                                     (l_cwk_multiplier,g_acct_currency_code);
996                         End If;
997 			UPDATE pa_bc_commitments_all cmt
998 			SET cmt.compiled_multiplier = decode (cmt.document_line_id,NULL,cmt.compiled_multiplier,
999 							l_cwk_multiplier)
1000 			WHERE cmt.summary_record_flag  = 'Y'
1001         		AND  cmt.document_line_id is NOT NULL
1002         		AND  cmt.document_type = 'PO'
1003         		AND  cmt.project_id = cwk.project_id
1004         		ANd  cmt.task_id = cwk.task_id
1005         		AND  cmt.budget_version_id = cwk.budget_version_id
1006         		AND  cmt.document_line_id = cwk.document_line_id
1007         		AND (( -- burden lines should be stamped with summary record info if display method is different
1008                 		cmt.parent_bc_packet_id is NOT NULL
1009                 		and cmt.expenditure_type = cwk.expenditure_type
1010                 		and cwk.burden_amt_display_method = 'D'
1011              			)
1012             			OR
1013              			( -- raw line should be stamped with summary record info if display method is same
1014                 			cmt.parent_bc_packet_id is NULL
1015                 			and  cwk.burden_amt_display_method <> 'D'
1016              			)
1017            		   );
1018 		       l_rows_updated := sql%rowcount;
1019 
1020 		ELsif l_pktsummrec = 'Y' Then
1021 			l_stage  := 'Updating packets cwk multiplier';
1022                         l_cwk_multiplier := get_cwk_multiplier(cwk.project_id
1023                                              ,cwk.task_id
1024                                              ,cwk.budget_version_id
1025                                              ,cwk.document_line_id
1026                                              ,'PO'
1027                                              ,cwk.expenditure_type
1028 					     ,cwk.burden_amt_display_method
1029                                              ,'GL');
1030 			If l_cwk_multiplier is NOT NULL Then
1031 				l_cwk_multiplier := pa_currency.round_trans_currency_amt
1032 							(l_cwk_multiplier,g_acct_currency_code);
1033 			 End If;
1034                         UPDATE pa_bc_packets cmt
1035                         SET cmt.compiled_multiplier = decode (cmt.document_line_id,NULL,cmt.compiled_multiplier
1036                                                                 	,l_cwk_multiplier)
1037                         WHERE cmt.summary_record_flag  = 'Y'
1038                         AND  cmt.document_line_id is NOT NULL
1039                         AND  cmt.document_type = 'PO'
1040                         AND  cmt.project_id = cwk.project_id
1041                         ANd  cmt.task_id = cwk.task_id
1042                         AND  cmt.budget_version_id = cwk.budget_version_id
1043                         AND  cmt.document_line_id = cwk.document_line_id
1044 			AND  NVL(cmt.balance_posted_flag,'N') <> 'Y'
1045         		AND  nvl(cmt.funds_process_mode,'N') = 'T'
1046 			AND  cmt.status_code IN ('A','C')
1047         		AND  substr(nvl(cmt.result_code,'P'),1,1) = 'P'
1048                         AND (( -- burden lines should be stamped with summary record info if display method is different
1049                                 cmt.parent_bc_packet_id is NOT NULL
1050                                 and cmt.expenditure_type = cwk.expenditure_type
1051                                 and cwk.burden_amt_display_method = 'D'
1052                                 )
1053                                 OR
1054                                 ( -- raw line should be stamped with summary record info if display method is same
1055                                         cmt.parent_bc_packet_id is NULL
1056                                         and  cwk.burden_amt_display_method <> 'D'
1057                                 )
1058                            );
1059 
1060 		       l_rows_updated := sql%rowcount;
1061 
1062 		End if ; -- end of sum record flag
1063 
1064 	   END IF; -- end of p_mode
1065 
1066 	   pa_funds_control_pkg.log_message(p_msg_token1 =>l_stage||'- Num of Rows cwk attribute Updated['||l_rows_updated||']');
1067 
1068 	 END LOOP; -- end of cur_cwkRecs cursor
1069 
1070 	END IF;  -- end of calling module
1071 
1072 	--COMMIT;
1073 
1074 EXCEPTION
1075 
1076 	WHEN OTHERS THEN
1077 		x_return_status := 'T';
1078 		ROLLBACK;
1079 		RAISE;
1080 
1081 
1082 
1083 END upd_cwk_attributes;
1084 
1085 ----------------------------------------------------------------------------------------
1086 -- This Function checks whether the given invoice line is based on PO or NOT
1087 -- the return paramter is varchar2 of PO in case of PO or INV in case of invoice
1088 ----------------------------------------------------------------------------------------
1089 -- Obsolete FUNCTION check_encum_type
1090 
1091 ----------------------------------------------------------------------------------------
1092 --This api copies the unreserved transaction into to the packet.
1093 -- when the calling mode is unreserved then copy all the transactions from pa_bc_packets
1094 -- for the old packet id(which is funds cheked and approved) to new packet by swapping the amount
1095 -- columns and all other columns values remain same. Approve the packets with status Approved
1096 -- donot create encumbrance liquidation as GL funds checker will create reversing lines
1097 -- for the old packet id and donot populate burden rows / donot check for the unreserved packet
1098 --------------------------------------------------------------------------------------------------------
1099 FUNCTION create_unrsvd_lines
1100         ( x_packet_id           IN OUT NOCOPY    NUMBER
1101 	,p_mode			IN      VARCHAR2
1102         ,p_calling_module       IN      VARCHAR2
1103         ,p_reference1           IN      varchar2 default null
1104         ,p_reference2           IN      varchar2 default null
1105         )  RETURN BOOLEAN AS
1106 
1107         PRAGMA AUTONOMOUS_TRANSACTION;
1108 
1109 	l_packet_id 	number;
1110 
1111 	CURSOR cur_packet IS
1112 	SELECT gl_bc_packets_s.nextval
1113 	FROM dual;
1114 
1115         l_request_id      NUMBER := fnd_global.conc_request_id();
1116         l_program_id      NUMBER := fnd_global.conc_program_id();
1117         l_program_application_id NUMBER:= fnd_global.prog_appl_id();
1118         l_update_login    NUMBER := FND_GLOBAL.login_id;
1119         l_num_rows        NUMBER := 0;
1120         l_return_status    VARCHAR2(1);
1121 	l_debug_mode      VARCHAR2(10);
1122 
1123 
1124  BEGIN
1125 
1126         PA_DEBUG.set_curr_function
1127         ('PA_FUNDS_CONTROL_PKG1.create_unrrsvd_lines');
1128 
1129         fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
1130         l_debug_mode := NVL(l_debug_mode, 'N');
1131 
1132         PA_DEBUG.SET_PROCESS( x_process => 'PLSQL'
1133                  ,x_write_file     => 'LOG'
1134                  ,x_debug_mode      => l_debug_mode
1135                   );
1136 
1137 	pa_funds_control_pkg.log_message(p_msg_token1 => 'Inside the create_unreserve_pkt api');
1138 
1139         If l_request_id is null then
1140                 l_request_id := -1;
1141         End if;
1142 
1143         if l_program_id is null then
1144                 l_program_id := -1;
1145         End if;
1146         If l_program_application_id is null then
1147                 l_program_application_id := -1;
1148         End if;
1149 
1150         If l_update_login is null then
1151                 l_update_login := -1;
1152         End if;
1153 
1154 	pa_funds_control_pkg.log_message(p_msg_token1 => 'p_calling_module='||p_calling_module||' p_mode='||p_mode);
1155 
1156 	If p_calling_module = 'GL' and p_mode in ('U')  then -- unreserved
1157 		OPEN cur_packet ;
1158 		FETCH cur_packet INTO l_packet_id;
1159 		CLOSE cur_packet;
1160 
1161 	pa_funds_control_pkg.log_message(p_msg_token1 => 'Inserting records in pa_bc_packets');
1162 
1163                 INSERT INTO pa_bc_packets
1164                         ( ---- who columns------
1165                         request_id,
1166                         program_id,
1167                         program_application_id,
1168                         program_update_date,
1169                         last_update_date,
1170                         last_updated_by,
1171                         created_by,
1172                         creation_date,
1173                         last_update_login,
1174                         ------ main columns-----------
1175                         packet_id,
1176                         bc_packet_id,
1177                         budget_version_id,
1178                         project_id,
1179                         task_id,
1180                         expenditure_type,
1181                         expenditure_organization_id,
1182                         expenditure_item_date,
1183                         set_of_books_id,
1184                         je_source_name,
1185                         je_category_name,
1186                         document_type,
1187                         document_header_id,
1188                         document_distribution_id,
1189                         actual_flag,
1190                         period_name,
1191                         period_year,
1192                         period_num,
1193                         result_code,
1194                         status_code,
1195                         entered_dr,
1196                         entered_cr,
1197                         accounted_dr,
1198                         accounted_cr,
1199                         gl_row_number,
1200                         balance_posted_flag,
1201                         funds_process_mode,
1202                         txn_ccid,
1203                         encumbrance_type_id,
1204                         burden_cost_flag,
1205                         org_id,
1206 			parent_resource_id,
1207 			resource_list_member_id,
1208 			proj_encumbrance_type_id,
1209 			budget_ccid,
1210 			document_line_id,
1211 			reference1,
1212 			reference2,
1213 			reference3,
1214 			-- R12 Funds Management Uptake: Newly added columns
1215 			bc_event_id,
1216 			budget_line_id,
1217 			session_id,
1218 			serial_id,
1219 			vendor_id,
1220 			main_or_backing_code,
1221                         burden_method_code,
1222                         source_event_id,
1223                         document_distribution_type,
1224 			document_header_id_2
1225                         )
1226                 SELECT
1227                         l_request_id,
1228                         l_program_id,
1229                         l_program_application_id,
1230                         sysdate,
1231                         sysdate,
1232                         l_update_login,
1233                         l_update_login,
1234                         sysdate,
1235                         l_update_login,
1236                         l_packet_id,
1237                         pa_bc_packets_s.nextval,
1238                         pbc.budget_version_id,
1239                         pbc.project_id,
1240                         pbc.task_id,
1241                         pbc.expenditure_type,
1242                         pbc.expenditure_organization_id,
1243                         trunc(pbc.expenditure_item_date),
1244                         pbc.set_of_books_id,
1245                         pbc.je_source_name,
1246                         pbc.je_category_name,
1247                         pbc.document_type,
1248                         pbc.document_header_id,
1249                         pbc.document_distribution_id,
1250                         pbc.actual_flag,
1251                         pbc.period_name,
1252                         pbc.period_year,
1253                         pbc.period_num,
1254                         pbc.result_code,
1255                         'P', -- status_code,
1256                         NVL(pbc.entered_cr,0),
1257                         NVL(pbc.entered_dr,0),
1258                         NVL(pbc.accounted_cr,0),
1259                         NVL(pbc.accounted_dr,0),
1260                         pbc.gl_row_number,
1261                         pbc.balance_posted_flag,
1262                         pbc.funds_process_mode ,
1263                         pbc.txn_ccid,
1264                         pbc.encumbrance_type_id,
1265                         pbc.burden_cost_flag,
1266                         pbc.org_id,
1267                         pbc.parent_resource_id,
1268                         pbc.resource_list_member_id,
1269                         pbc.proj_encumbrance_type_id,
1270                         pbc.budget_ccid,
1271 			pbc.document_line_id,
1272 	                pbc.reference1,
1273                         pbc.reference2,
1274                         pbc.reference3,
1275 			-- R12 Funds Management Uptake: Newly added columns
1276 			pbc.bc_event_id,
1277 			pbc.budget_line_id,
1278 			pbc.session_id,
1279 			pbc.serial_id,
1280 			pbc.vendor_id,
1281 			pbc.main_or_backing_code,
1282                         pbc.burden_method_code,
1283                         pbc.source_event_id,
1284                         pbc.document_distribution_type,
1285 			pbc.document_header_id_2
1286 		FROM
1287 			pa_bc_packets pbc
1288 		WHERE   pbc.packet_id = x_packet_id;
1289 
1290 		If sql%rowcount > 0 then
1291 			--assign the new packet id to out parameter
1292 			pa_funds_control_pkg.log_message(p_msg_token1 => 'New packet id ='|| x_packet_id);
1293 			x_packet_id := l_packet_id;
1294 		End if;
1295 
1296         Elsif p_calling_module = 'CBC' and p_mode in ('U')
1297 		and p_reference1 is not  null and  p_reference2 is not null then -- unreserved
1298                 OPEN cur_packet ;
1299                 FETCH cur_packet INTO l_packet_id;
1300                 CLOSE cur_packet;
1301 	pa_funds_control_pkg.log_message(p_msg_token1 => 'Inserting records in pa_bc_packets');
1302 
1303                 INSERT INTO pa_bc_packets
1304                         ( ---- who columns------
1305                         request_id,
1306                         program_id,
1307                         program_application_id,
1308                         program_update_date,
1309                         last_update_date,
1310                         last_updated_by,
1311                         created_by,
1312                         creation_date,
1313                         last_update_login,
1314                         ------ main columns-----------
1315                         packet_id,
1316                         bc_packet_id,
1317                         budget_version_id,
1318                         project_id,
1319                         task_id,
1320                         expenditure_type,
1321                         expenditure_organization_id,
1322                         expenditure_item_date,
1323                         set_of_books_id,
1324                         je_source_name,
1325                         je_category_name,
1326                         document_type,
1327                         document_header_id,
1328                         document_distribution_id,
1329                         actual_flag,
1330                         period_name,
1331                         period_year,
1332                         period_num,
1333                         result_code,
1334                         status_code,
1335                         entered_dr,
1336                         entered_cr,
1337                         accounted_dr,
1338                         accounted_cr,
1339                         gl_row_number,    --gl_row_bc_packet_row_id
1340                         balance_posted_flag,
1341                         funds_process_mode,
1342                         txn_ccid,
1343                         encumbrance_type_id,
1344                         burden_cost_flag,
1345                         org_id,
1346                         parent_resource_id,
1347                         resource_list_member_id,
1348                         proj_encumbrance_type_id,
1349                         budget_ccid,
1350 			document_line_id,
1351                         reference1,
1352                         reference2,
1353                         reference3,
1354 			-- R12 Funds Management Uptake: Newly added columns
1355 			bc_event_id,
1356 			budget_line_id,
1357 			session_id,
1358 			serial_id,
1359 			vendor_id,
1360 			main_or_backing_code,
1361                         burden_method_code,
1362                         source_event_id,
1363                         document_distribution_type,
1364 			document_header_id_2
1365                         )
1366                 SELECT
1367                         l_request_id,
1368                         l_program_id,
1369                         l_program_application_id,
1370                         sysdate,
1371                         sysdate,
1372                         l_update_login,
1373                         l_update_login,
1374                         sysdate,
1375                         l_update_login,
1376                         l_packet_id,
1377                         pa_bc_packets_s.nextval,
1378                         pbc.budget_version_id,
1379                         pbc.project_id,
1380                         pbc.task_id,
1381                         pbc.expenditure_type,
1382                         pbc.expenditure_organization_id,
1383                         trunc(pbc.expenditure_item_date),
1384                         pbc.set_of_books_id,
1385                         pbc.je_source_name,
1386                         pbc.je_category_name,
1387                         pbc.document_type,
1388                         pbc.document_header_id,
1389                         pbc.document_distribution_id,
1390                         pbc.actual_flag,
1391                         pbc.period_name,
1392                         pbc.period_year,
1393                         pbc.period_num,
1394                         pbc.result_code,
1395                         'P', -- status_code,
1396                         NVL(pbc.entered_cr,0),
1397                         NVL(pbc.entered_dr,0),
1398                         NVL(pbc.accounted_cr,0),
1399                         NVL(pbc.accounted_dr,0),
1400                         pbc.gl_row_number,
1401                         pbc.balance_posted_flag,
1402                         pbc.funds_process_mode ,
1403                         pbc.txn_ccid,
1404                         pbc.encumbrance_type_id,
1405                         pbc.burden_cost_flag,
1406                         pbc.org_id,
1407                         pbc.parent_resource_id,
1408                         pbc.resource_list_member_id,
1409                         pbc.proj_encumbrance_type_id,
1410                         pbc.budget_ccid,
1411 			pbc.document_line_id,
1412                         pbc.reference1,
1413                         pbc.reference2,
1414                         pbc.reference3,
1415 			-- R12 Funds Management Uptake: Newly added columns
1416 			pbc.bc_event_id,
1417 			pbc.budget_line_id,
1418 			pbc.session_id,
1419 			pbc.serial_id,
1420 			pbc.vendor_id,
1421 			pbc.main_or_backing_code,
1422                         pbc.burden_method_code,
1423                         pbc.source_event_id,
1424                         pbc.document_distribution_type,
1425 			pbc.document_header_id_2
1426                 FROM
1427                         pa_bc_packets pbc
1428                 WHERE   pbc.packet_id = x_packet_id
1429 		AND	document_type in ('CC_C_CO','CC_P_CO')
1430 		AND	document_header_id = p_reference2;
1431 
1432                 If sql%rowcount > 0 then
1433                         --assign the new packet id to out parameter
1434                         x_packet_id := l_packet_id;
1435                 End if;
1436 
1437 	END IF;
1438 	IF cur_packet%isopen then
1439 		close cur_packet;
1440 	End if;
1441 	commit; -- autonmous transaction to end
1442 	RETURN TRUE;
1443 EXCEPTION
1444 	WHEN OTHERS THEN
1445 		IF cur_packet%isopen then
1446 			close cur_packet;
1447 		End if;
1448 		RAISE;
1449 END create_unrsvd_lines;
1450 
1451 ------------------------------------------------------------------------------------
1452 --This Api checks whether the Purchase order is based on the requisiton if
1453 -- the period and commitment id from pa_bc_commitments
1454 ------------------------------------------------------------------------------------
1455 FUNCTION   is_req_based_po( p_req_distribution_id    IN NUMBER
1456 			   ,p_req_header_id          IN NUMBER
1457 			   ,p_req_prevent_enc_flipped IN VARCHAR2
1458 			   ,x_result_code	     IN OUT NOCOPY varchar2
1459 			   ,x_status_code	     IN OUT NOCOPY varchar2
1460 	  		   ,x_reference1   	     OUT NOCOPY varchar2
1461 			   ,x_reference2             OUT NOCOPY varchar2
1462 			   ,x_reference3             OUT NOCOPY varchar2
1463 			)
1464 			RETURN VARCHAR2 IS
1465 
1466 	l_return_flag           varchar2(10) := 'N';
1467 	l_req_found_flag        varchar2(1) := 'N';
1468 	l_parent_bc_packet_id   PA_BC_PACKETS.PARENT_BC_PACKET_ID%TYPE;
1469 
1470 	-- This cursor picks the po details for the given requisition
1471 	CURSOR get_podetails Is
1472 	SELECT 'PO'
1473 	      ,po_header_id
1474 	      ,po_distribution_id
1475 	FROM po_distributions_all pod
1476 	WHERE pod.req_distribution_id = p_req_distribution_id ;
1477 
1478 	CURSOR c_req_raw_burden IS
1479 	SELECT parent_bc_packet_id
1480         FROM   ( SELECT comm.bc_commitment_id,
1481 	                comm.parent_bc_packet_id
1482                    FROM pa_bc_commitments comm
1483                   WHERE comm.document_distribution_id = p_req_distribution_id
1484                     AND comm.document_header_id = p_req_header_id
1485                     AND comm.document_type = 'REQ'
1486         	UNION ALL
1487                 SELECT null bc_commitment_id,
1488 	               pbc.parent_bc_packet_id
1489                   FROM pa_bc_packets pbc
1490                  WHERE pbc.document_distribution_id = p_req_distribution_id
1491                    AND pbc.document_header_id = p_req_header_id
1492                    AND pbc.document_type = 'REQ'
1493                    AND pbc.balance_posted_flag = 'N'
1494                    AND pbc.status_code in ('A','C')
1495                    AND substr(nvl(result_code,'P'),1,1) = 'P');
1496 
1497 BEGIN
1498 
1499         IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1500            pa_funds_control_pkg.log_message(p_msg_token1 => ' IS_REQ_BASED_PO - Start');
1501            pa_funds_control_pkg.log_message(p_msg_token1 => ' IS_REQ_BASED_PO - Value of p_req_distribution_id '||p_req_distribution_id );
1502            pa_funds_control_pkg.log_message(p_msg_token1 => ' IS_REQ_BASED_PO - Value of p_req_prevent_enc_flipped '||p_req_prevent_enc_flipped );
1503            pa_funds_control_pkg.log_message(p_msg_token1 => ' IS_REQ_BASED_PO - Value of x_status_code '||x_status_code );
1504            pa_funds_control_pkg.log_message(p_msg_token1 => ' IS_REQ_BASED_PO - Value of x_result_code '||x_result_code );
1505         End if;
1506 
1507         l_return_flag :='N';
1508 
1509         -- Check if its backing rquisition
1510 
1511 	OPEN get_podetails;
1512 	FETCH get_podetails INTO
1513 			x_reference1
1514 			,x_reference2
1515 			,x_reference3;
1516 	CLOSE get_podetails;
1517 
1518         l_req_found_flag := 'N';
1519 
1520         IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1521            pa_funds_control_pkg.log_message(p_msg_token1 => ' x_reference1 = '||x_reference1);
1522            pa_funds_control_pkg.log_message(p_msg_token1 => ' x_reference2 = '||x_reference2);
1523            pa_funds_control_pkg.log_message(p_msg_token1 => ' x_reference3 = '||x_reference3);
1524         End if;
1525 
1526 	OPEN c_req_raw_burden;
1527 	LOOP
1528 	   FETCH c_req_raw_burden INTO l_parent_bc_packet_id;
1529            IF c_req_raw_burden%notfound then
1530 	      IF l_req_found_flag = 'N' THEN
1531 	       IF p_req_prevent_enc_flipped = 'Y' THEN
1532 	          -- Bug 5475128 : IF Requisition was sourced to BPA then it was never Fundscheck
1533 		  -- IN this scenario return with success status.
1534 	          NULL;
1535                ELSE
1536 	         x_result_code := 'F137' ; -- No matching requisition record found
1537 	         x_status_code := 'R';
1538 	       END IF;
1539               END IF;
1540               IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1541                  pa_funds_control_pkg.log_message(p_msg_token1 => 'No matching requisition record found ');
1542               End if;
1543 	      EXIT;
1544            END IF;
1545 
1546 	   -- Found Requisition Record
1547 	   l_req_found_flag := 'Y';
1548 
1549            -- If burden record is found then intialize x_bc_commitment_id OUT variable
1550 	   IF l_parent_bc_packet_id IS NOT NULL THEN
1551 	      l_return_flag := 'Y';
1552               IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1553                  pa_funds_control_pkg.log_message(p_msg_token1 => 'l_parent_bc_packet_id = '||l_parent_bc_packet_id);
1554               End if;
1555 	      EXIT;
1556 	   END IF;
1557 
1558         END LOOP;
1559 	CLOSE c_req_raw_burden;
1560 
1561       IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1562            pa_funds_control_pkg.log_message(p_msg_token1 => ' IS_REQ_BASED_PO - End l_return_flag ='||l_return_flag);
1563            pa_funds_control_pkg.log_message(p_msg_token1 => ' IS_REQ_BASED_PO - End  x_status_code '||x_status_code );
1564            pa_funds_control_pkg.log_message(p_msg_token1 => ' IS_REQ_BASED_PO - End  x_result_code '||x_result_code );
1565       End if;
1566 
1567       return l_return_flag;
1568 
1569 EXCEPTION
1570 WHEN OTHERS THEN
1571       IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1572            pa_funds_control_pkg.log_message(p_msg_token1 => ' IS_REQ_BASED_PO - Exception'||SQLERRM);
1573       End if;
1574 
1575       Raise;
1576 END is_req_based_po;
1577 
1578 --R12 Ledger Changes : Obsoleted FUNCTION GetAmtVariance as its a dangling procedure
1579 
1580 --------------------------------------------------------------------------------
1581 --This api checks whether the Invoice is based on the Purchase order
1582 -- if so then it takes the po_header_id,po_distribution_id,period_name
1583 -- and bc_commitment_id from pa_bc_commitments
1584 --------------------------------------------------------------------------------
1585 FUNCTION  is_po_based_invoice( p_po_distribution_id          IN NUMBER
1586                                 ,p_po_header_id              IN NUMBER
1587                                 ,p_po_release_id             IN NUMBER
1588 			        ,x_result_code               IN OUT NOCOPY varchar2
1589 			        ,x_status_code               IN OUT NOCOPY varchar2
1590 			     ) RETURN VARCHAR2 IS
1591 
1592 	l_po_found_flag         VARCHAR2(1) := 'N';
1593 	l_cc_found_flag         VARCHAR2(1) := 'N';
1594 	l_return_flag           VARCHAR2(1) := 'N';
1595 	l_cc_det_pf_line_id     varchar2(30); --Bug 6393954 changed from number to varchar2
1596 	L_PARENT_BC_PACKET_ID   PA_BC_PACKETS.PARENT_BC_PACKET_ID%TYPE;
1597 	L_CC_HEADER_ID          varchar2(30); --Bug 6393954 changed from PO_DISTRIBUTIONS_ALL.PO_HEADER_ID%TYPE <number> to varchar2
1598 	L_PO_DESTINATION_TYPE   PO_DISTRIBUTIONS_ALL.DESTINATION_TYPE_CODE%TYPE;
1599 	L_CC_DISTRIBUTION_ID NUMBER;
1600 
1601         -- this cursor checks whehter the AP is based on Purchase Order  if so then
1602         -- take the calculated burden amount from pa_bc_commitments table
1603         CURSOR po_cur is
1604         SELECT po.req_header_reference_num
1605 	       ,po.req_line_reference_num
1606 	       ,po.destination_type_code
1607         FROM  po_distributions_all po
1608         WHERE po.po_distribution_id = p_po_distribution_id;
1609 
1610         -- Added  for 13903042
1611 	CURSOR cc_cur (p_det_pf_line_id NUMBER) IS
1612 	SELECT DISTINCT cc_acct_line_id
1613    	FROM igc_cc_det_pf
1614 	WHERE cc_det_pf_line_id = p_det_pf_line_id;
1615 
1616 
1617 	CURSOR c_po_raw_burden(p_distribution_id NUMBER,
1618 	                       p_header_id       NUMBER,
1619 			       p_document_type   VARCHAR2) IS
1620 	SELECT parent_bc_packet_id
1621         FROM   ( SELECT comm.parent_bc_packet_id
1622                    FROM pa_bc_commitments comm
1623                   WHERE comm.document_distribution_id = p_distribution_id
1624                     AND comm.document_header_id = p_header_id
1625                     AND comm.document_type = p_document_type
1626                     AND NVL(comm.document_header_id_2 ,-99) = NVL(p_po_release_id,-99)
1627         	UNION ALL
1628                 SELECT pbc.parent_bc_packet_id
1629                   FROM pa_bc_packets pbc
1630                  WHERE pbc.document_distribution_id = p_distribution_id
1631                    AND pbc.document_header_id = p_header_id
1632                    AND pbc.document_type = p_document_type
1633                    AND pbc.balance_posted_flag = 'N'
1634                    AND pbc.status_code in ('A','C')
1635                    AND substr(nvl(result_code,'P'),1,1) = 'P'
1636                    AND NVL(pbc.document_header_id_2 ,-99) = NVL(p_po_release_id,-99));
1637 
1638 BEGIN
1639 
1640         IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1641            pa_funds_control_pkg.log_message(p_msg_token1 => ' IS_PO_BASED_INVOICE - Start');
1642         End if;
1643 
1644 	OPEN  po_cur;
1645 	FETCH po_cur INTO
1646 		l_cc_header_id,
1647 		l_cc_det_pf_line_id,
1648 		l_po_destination_type;
1649         CLOSE po_cur;
1650 
1651 	-- Added for 13903042
1652 
1653 	OPEN cc_cur(l_cc_det_pf_line_id);
1654 	FETCH cc_cur into
1655 	      l_cc_distribution_id;
1656 	CLOSE cc_cur;
1657 
1658         IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1659            pa_funds_control_pkg.log_message(p_msg_token1 => ' l_cc_header_id ='||l_cc_header_id);
1660            pa_funds_control_pkg.log_message(p_msg_token1 => ' l_cc_det_pf_line_id ='||l_cc_det_pf_line_id);
1661            pa_funds_control_pkg.log_message(p_msg_token1 => ' l_po_destination_type ='||l_po_destination_type);
1662 	   pa_funds_control_pkg.log_message(p_msg_token1 => ' l_cc_distribution_id ='||l_cc_distribution_id);
1663 	   pa_funds_control_pkg.log_message(p_msg_token1 => ' p_po_release_id ='||p_po_release_id);
1664         End if;
1665 
1666         IF nvl(l_po_destination_type,'EXPENSE') in('INVENTORY','SHOP FLOOR') then
1667 
1668             IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1669               pa_funds_control_pkg.log_message(p_msg_token1 => ' PO is Inventory based so update status code to S');
1670             End if;
1671 
1672             x_result_code := 'P113';
1673             x_status_code := 'V';
1674 	    return 'N';
1675 
1676 	ElSE
1677 
1678              l_po_found_flag := 'N';
1679 
1680              -- Added for 13903042
1681 
1682 	     IF l_cc_header_id IS NULL and l_cc_det_pf_line_id IS NULL THEN
1683 
1684 		     OPEN c_po_raw_burden(p_po_distribution_id,p_po_header_id,'PO') ;
1685 		     LOOP
1686 			 FETCH c_po_raw_burden INTO l_parent_bc_packet_id;
1687 			 IF c_po_raw_burden%notfound then
1688 			    IF l_po_found_flag = 'N' THEN
1689 			       x_result_code := 'F138'; -- No matching PO record found for this Invoice
1690 			       x_status_code := 'R';
1691 			  END IF;
1692 			  IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1693 			     pa_funds_control_pkg.log_message(p_msg_token1 => ' No matching PO record found for this Invoice - F138');
1694 			  End if;
1695 			  EXIT;
1696 			 END IF;
1697 
1698 			 -- Found PO Record
1699 			 l_po_found_flag := 'Y';
1700 
1701 			 -- If burden record is found then intialize x_bc_commitment_id OUT variable
1702 			 IF l_parent_bc_packet_id IS NOT NULL THEN
1703 			    l_return_flag := 'Y';
1704 			    IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1705 			     pa_funds_control_pkg.log_message(p_msg_token1 => ' l_parent_bc_packet_id ='||l_parent_bc_packet_id);
1706 			    End if;
1707 			    EXIT;
1708 			 END IF;
1709 
1710 		     END LOOP;
1711 		     CLOSE c_po_raw_burden;
1712              END IF;
1713 
1714              -- Code supporting existing CC flow
1715              IF l_po_found_flag = 'N' and l_cc_header_id is not null and l_cc_det_pf_line_id is not null then
1716 
1717 		   pa_funds_control_pkg.log_message(p_msg_token1 => ' In not null condition =' );
1718 		   l_cc_found_flag := 'N';
1719 
1720 		   --OPEN c_po_raw_burden(p_po_distribution_id,p_po_header_id,'CC_C_PAY') ;
1721 		   -- Changed for 13903042
1722 		   OPEN c_po_raw_burden(l_cc_distribution_id,l_cc_header_id,'CC_C_PAY') ;
1723 		     LOOP
1724 			 FETCH c_po_raw_burden INTO l_parent_bc_packet_id;
1725 			 IF c_po_raw_burden%notfound then
1726 			    IF l_cc_found_flag = 'N' THEN
1727 			       x_result_code := 'F138'; -- No matching CC record found for this Invoice
1728 			       x_status_code := 'R';
1729 			    END IF;
1730                             IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1731                                pa_funds_control_pkg.log_message(p_msg_token1 => 'No matching CC record found for this Invoice');
1732                             End if;
1733 			  EXIT;
1734 			 END IF;
1735 
1736 			 -- Found cc Record
1737 			 l_cc_found_flag := 'Y';
1738 
1739 			 -- If burden record is found then intialize x_bc_commitment_id OUT variable
1740 			 IF l_parent_bc_packet_id IS NOT NULL THEN
1741 			    l_return_flag := 'Y';
1742                             IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1743                                pa_funds_control_pkg.log_message(p_msg_token1 => ' Parent PO burden record found');
1744                             End if;
1745 			    EXIT;
1746 			 END IF;
1747 
1748 		     END LOOP;
1749 		     CLOSE c_po_raw_burden;
1750              END IF;  --IF l_po_found_flag = 'N' and l_cc_header_id is not null and l_cc_det_pf_line_id is not null then
1751         END IF;    --IF nvl(l_po_destination_type,'EXPENSE') in('INVENTORY','SHOP FLOOR') then
1752 
1753         IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1754            pa_funds_control_pkg.log_message(p_msg_token1 => ' IS_PO_BASED_INVOICE - End l_return_flag =' ||l_return_flag);
1755         End if;
1756 
1757 	pa_funds_control_pkg.log_message(p_msg_token1 => ' l_return_flag  ' ||l_return_flag);
1758 
1759 	Return l_return_flag;
1760 
1761 EXCEPTION
1762 WHEN OTHERS THEN
1763 
1764         IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1765            pa_funds_control_pkg.log_message(p_msg_token1 => ' IS_PO_BASED_INVOICE - Exception'||SQLERRM);
1766         End if;
1767 
1768 	Raise;
1769 
1770 END is_po_based_invoice;
1771 
1772 PROCEDURE COPY_AP_RECORD (p_copy_from_index    IN NUMBER,
1773                           p_new_rec_index      IN NUMBER) IS
1774 BEGIN
1775 
1776        IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1777          pa_funds_control_pkg.log_message(p_msg_token1 => 'COPY_AP_RECORD - Start');
1778        End if;
1779 
1780         g_tab_budget_version_id(p_new_rec_index)        :=  g_tab_budget_version_id(p_copy_from_index);
1781         g_tab_budget_line_id(p_new_rec_index)           :=  g_tab_budget_line_id(p_copy_from_index);
1782         g_tab_budget_ccid(p_new_rec_index)              :=  g_tab_budget_ccid(p_copy_from_index);
1783         g_tab_project_id(p_new_rec_index)               :=  g_tab_project_id(p_copy_from_index);
1784         g_tab_task_id(p_new_rec_index)                  :=  g_tab_task_id(p_copy_from_index);
1785         g_tab_exp_type(p_new_rec_index)                 :=  g_tab_exp_type(p_copy_from_index);
1786         g_tab_exp_org_id(p_new_rec_index)               :=  g_tab_exp_org_id(p_copy_from_index);
1787         g_tab_exp_item_date(p_new_rec_index)            :=  g_tab_exp_item_date(p_copy_from_index) ;
1788         g_tab_set_of_books_id(p_new_rec_index)          :=  g_tab_set_of_books_id(p_copy_from_index);
1789         g_tab_je_source_name(p_new_rec_index)           :=  g_tab_je_source_name(p_copy_from_index);
1790         g_tab_je_category_name(p_new_rec_index)         :=  g_tab_je_category_name(p_copy_from_index);
1791         g_tab_doc_type(p_new_rec_index)                 :=  g_tab_doc_type(p_copy_from_index);
1792         g_tab_doc_header_id(p_new_rec_index)            :=  g_tab_doc_header_id(p_copy_from_index);
1793         g_tab_doc_line_id(p_new_rec_index)              :=  g_tab_doc_line_id(p_copy_from_index);
1794         g_tab_doc_distribution_id(p_new_rec_index)      :=  g_tab_doc_distribution_id(p_copy_from_index);
1795 	g_tab_inv_distribution_id(p_new_rec_index)      :=  g_tab_inv_distribution_id(p_copy_from_index);
1796         g_tab_actual_flag(p_new_rec_index)              :=  g_tab_actual_flag(p_copy_from_index);
1797         g_tab_result_code(p_new_rec_index)              :=  g_tab_result_code(p_copy_from_index);
1798         g_tab_status_code(p_new_rec_index)              :=  g_tab_status_code(p_copy_from_index);
1799         g_tab_balance_posted_flag(p_new_rec_index)      :=  g_tab_balance_posted_flag(p_copy_from_index);
1800         g_tab_funds_process_mode(p_new_rec_index)       :=  g_tab_funds_process_mode(p_copy_from_index);
1801         g_tab_burden_cost_flag(p_new_rec_index)         :=  g_tab_burden_cost_flag(p_copy_from_index);
1802         g_tab_org_id(p_new_rec_index)                   :=  g_tab_org_id(p_copy_from_index) ;
1803         g_tab_pkt_reference1(p_new_rec_index)           :=  g_tab_pkt_reference1(p_copy_from_index);
1804         g_tab_pkt_reference2(p_new_rec_index)           :=  g_tab_pkt_reference2(p_copy_from_index);
1805         g_tab_pkt_reference3(p_new_rec_index)           :=  g_tab_pkt_reference3(p_copy_from_index);
1806         g_tab_event_id(p_new_rec_index)                 :=  g_tab_event_id(p_copy_from_index);
1807 	g_tab_vendor_id(p_new_rec_index)                :=  g_tab_vendor_id(p_copy_from_index);
1808         g_tab_burden_method_code(p_new_rec_index)       :=  g_tab_burden_method_code(p_copy_from_index);
1809         g_tab_main_or_backing_code(p_new_rec_index)     :=  g_tab_main_or_backing_code(p_copy_from_index);
1810         g_tab_source_event_id(p_new_rec_index)          :=  g_tab_source_event_id(p_copy_from_index);
1811         g_tab_trxn_ccid(p_new_rec_index)                :=  g_tab_trxn_ccid(p_copy_from_index);
1812         g_tab_p_bc_packet_id(p_new_rec_index)           :=  g_tab_p_bc_packet_id(p_copy_from_index);
1813 	g_tab_fck_reqd_flag(p_new_rec_index)            :=  g_tab_fck_reqd_flag(p_copy_from_index);
1814 	g_tab_entered_amount(p_new_rec_index)           :=  g_tab_entered_amount(p_copy_from_index);
1815 	g_tab_accted_amount(p_new_rec_index)            :=  g_tab_accted_amount(p_copy_from_index);
1816         g_tab_ap_quantity_variance(p_new_rec_index)     :=  NULL;
1817         g_tab_ap_amount_variance(p_new_rec_index)       :=  NULL;
1818         g_tab_ap_base_qty_variance(p_new_rec_index)     :=  NULL;
1819         g_tab_ap_base_amount_variance(p_new_rec_index)  :=  NULL;
1820         g_tab_ap_po_distribution_id(p_new_rec_index)    :=  g_tab_ap_po_distribution_id(p_copy_from_index);
1821 	g_tab_gl_date(p_new_rec_index)                  :=  g_tab_gl_date(p_copy_from_index);
1822 	g_tab_distribution_type(p_new_rec_index)        :=  g_tab_distribution_type(p_copy_from_index);
1823 	g_tab_po_release_id(p_new_rec_index)            :=  g_tab_po_release_id(p_copy_from_index);
1824 	g_tab_enc_type_id(p_new_rec_index)              :=  g_tab_enc_type_id(p_copy_from_index);
1825 	g_tab_period_name(p_new_rec_index)              :=  g_tab_period_name(p_copy_from_index);
1826         g_tab_parent_reversal_id(p_new_rec_index)       :=  g_tab_parent_reversal_id(p_copy_from_index); -- Bug 5406690
1827 
1828         -- Bug 5406690
1829 	select pa_bc_packets_s.nextval
1830 	into g_tab_bc_packet_id(p_new_rec_index)
1831 	from dual;
1832 
1833        IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1834          pa_funds_control_pkg.log_message(p_msg_token1 => 'COPY_AP_RECORD - End');
1835        End if;
1836 
1837 EXCEPTION
1838 WHEN OTHERS THEN
1839 
1840   IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1841       pa_funds_control_pkg.log_message(p_msg_token1 => 'COPY_AP_RECORD - EXception'||SQLERRM);
1842   End if;
1843 
1844   RAISE;
1845 
1846 END COPY_AP_RECORD;
1847 
1848 
1849 PROCEDURE CREATE_AP_PO_RECORD (p_copy_from_index          IN NUMBER,
1850                                p_new_rec_index            IN NUMBER ) IS
1851 
1852         -- Cursor to fetch PO details for PO relieving record
1853         CURSOR c_po_details_cur(p_po_distribution_id  NUMBER) IS
1854         SELECT po.po_line_id                        po_line_id,
1855                po.po_header_id ,
1856 	       (SELECT encumbrance_type_id
1857 	          FROM gl_encumbrance_types
1858 		 WHERE encumbrance_type_KEY = 'Obligation') po_encumbrance_type_id ,
1859                po.rate -- Bug 5665232
1860           FROM po_distributions_all po
1861          WHERE po.po_distribution_id = p_po_distribution_id;
1862 
1863 
1864         CURSOR c_po_dist_type (p_po_header_id  NUMBER,
1865 	                       p_po_release_id NUMBER) IS
1866         SELECT type_lookup_code
1867           FROM po_headers_all po
1868          WHERE po.po_header_id = p_po_header_id
1869 	   AND p_po_release_id IS NULL
1870 	UNION
1871         SELECT release_type
1872           FROM po_releases_all po
1873          WHERE po.po_release_id = p_po_release_id
1874 	   AND p_po_release_id IS NOT NULL;
1875 
1876 BEGIN
1877 
1878 	 IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1879             pa_funds_control_pkg.log_message(p_msg_token1 => 'CREATE_AP_PO_RECORD - Start');
1880             pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of parameter p_copy_from_index ='||p_copy_from_index);
1881             pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of parameter p_new_rec_index ='||p_new_rec_index);
1882             pa_funds_control_pkg.log_message(p_msg_token1 => 'Calling COPY_AP_RECORD');
1883          End if;
1884 
1885          COPY_AP_RECORD (p_copy_from_index,p_new_rec_index);
1886 
1887 	 IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1888             pa_funds_control_pkg.log_message(p_msg_token1 => 'After Calling COPY_AP_RECORD');
1889             pa_funds_control_pkg.log_message(p_msg_token1 => 'Overwriting few of PO column values');
1890          End if;
1891 
1892          g_tab_doc_distribution_id(p_new_rec_index) :=  g_tab_ap_po_distribution_id(p_copy_from_index);
1893          g_tab_je_source_name(p_new_rec_index)      := 'Purchasing';
1894          IF g_tab_po_release_id(p_new_rec_index) IS NOT NULL THEN
1895             g_tab_je_category_name(p_new_rec_index)    := 'Release';
1896          ELSE
1897             g_tab_je_category_name(p_new_rec_index)    := 'Purchases';
1898          END IF;
1899          g_tab_doc_type(p_new_rec_index)            := 'PO';
1900          g_tab_pkt_reference1(p_new_rec_index)      := 'AP';
1901          g_tab_pkt_reference2(p_new_rec_index)      := g_tab_doc_header_id(p_copy_from_index); --Ap invoice associated with this backing PO
1902          g_tab_pkt_reference3(p_new_rec_index)      := g_tab_doc_distribution_id(p_copy_from_index); --Ap distribution associated with this backing PO
1903 
1904          OPEN  c_po_details_cur(g_tab_ap_po_distribution_id(p_new_rec_index));
1905          FETCH c_po_details_cur INTO g_tab_doc_line_id(p_new_rec_index) ,g_tab_doc_header_id(p_new_rec_index) ,
1906 				     g_tab_enc_type_id(p_new_rec_index) ,g_tab_rate(p_new_rec_index) ; -- Bug 5665232
1907          CLOSE c_po_details_cur;
1908 
1909 	 OPEN c_po_dist_type(g_tab_doc_header_id(p_new_rec_index),g_tab_po_release_id(p_copy_from_index));
1910 	 FETCH c_po_dist_type INTO g_tab_distribution_type(p_new_rec_index) ;
1911 	 CLOSE c_po_dist_type;
1912 
1913          -- Open issue for variance and ovematch AP-PO cases.
1914 
1915          g_tab_entered_dr(p_new_rec_index)              := g_tab_entered_cr(p_copy_from_index);
1916          g_tab_entered_cr(p_new_rec_index)              := g_tab_entered_dr(p_copy_from_index);
1917 
1918          /* Bug 5665232 :
1919 	    If the rate on the PO distribution is not null then
1920 	      Calculate accounted amounts for PO relieving record from its entered amounts and rate
1921             Else
1922 	      Copy accounted amounts for PO relieving record from those on the invoice */
1923 
1924          If g_tab_rate(p_new_rec_index) is NOT NULL then
1925 	   IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1926              pa_funds_control_pkg.log_message(p_msg_token1 => 'Calculating accounted amounts for PO relieving record from its entered amounts and rate');
1927 	     pa_funds_control_pkg.log_message(p_msg_token1 => 'Rate : '||g_tab_rate(p_new_rec_index));
1928            End if;
1929            g_tab_accounted_dr(p_new_rec_index)  := g_tab_entered_dr(p_new_rec_index) * g_tab_rate(p_new_rec_index);
1930 	   g_tab_accounted_cr(p_new_rec_index)  := g_tab_entered_cr(p_new_rec_index) * g_tab_rate(p_new_rec_index);
1931          else
1932 	   IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1933              pa_funds_control_pkg.log_message(p_msg_token1 => 'Copying accounted amounts for PO relieving record from those on the invoice');
1934            End if;
1935            g_tab_accounted_dr(p_new_rec_index)            := g_tab_accounted_cr(p_copy_from_index);
1936            g_tab_accounted_cr(p_new_rec_index)            := g_tab_accounted_dr(p_copy_from_index);
1937          end if;
1938 	 IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1939            pa_funds_control_pkg.log_message(p_msg_token1 => 'Accounted_dr : '||g_tab_accounted_dr(p_new_rec_index));
1940 	   pa_funds_control_pkg.log_message(p_msg_token1 => 'Accounted_cr : '||g_tab_accounted_cr(p_new_rec_index));
1941          End if;
1942 
1943          g_tab_ap_po_distribution_id(p_new_rec_index)   := NULL;
1944 
1945 	 IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1946             pa_funds_control_pkg.log_message(p_msg_token1 => 'After overwriting few of PO column values');
1947             pa_funds_control_pkg.log_message(p_msg_token1 => 'Before calling is_po_based_invoice');
1948          End if;
1949 
1950          -- Fetch parent_bc_packet_id for PO record
1951          IF  is_po_based_invoice (p_po_distribution_id             => g_tab_doc_distribution_id(p_new_rec_index)
1952                                   ,p_po_header_id                  => g_tab_doc_header_id(p_new_rec_index)
1953                                   ,p_po_release_id                 => g_tab_po_release_id(p_copy_from_index)
1954 		                  ,x_result_code	           => g_tab_result_code(p_new_rec_index)
1955 			          ,x_status_code		   => g_tab_status_code(p_new_rec_index))
1956                                   = 'Y' THEN
1957 
1958            IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1959               pa_funds_control_pkg.log_message(p_msg_token1 => 'In is_po_based_invoice ');
1960            End if;
1961 
1962            g_tab_p_bc_packet_id(p_new_rec_index) := -1 ;
1963 
1964          END IF;
1965 
1966 	 IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1967             pa_funds_control_pkg.log_message(p_msg_token1 => 'After calling is_po_based_invoice');
1968             pa_funds_control_pkg.log_message(p_msg_token1 => 'CREATE_AP_PO_RECORD - End');
1969          End if;
1970 
1971 EXCEPTION
1972 WHEN OTHERS THEN
1973 
1974   IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
1975       pa_funds_control_pkg.log_message(p_msg_token1 => 'CREATE_AP_PO_RECORD - EXception'||SQLERRM);
1976   End if;
1977 
1978   RAISE;
1979 
1980 END CREATE_AP_PO_RECORD;
1981 
1982 
1983 PROCEDURE CREATE_APVAR_RECORD (p_copy_from_index          IN NUMBER,
1984                                p_new_rec_index            IN OUT NOCOPY NUMBER,
1985 			       p_cwk_po_flag              IN VARCHAR2,
1986 			       p_accrue_on_receipt_flag   IN VARCHAR2,
1987 			       p_variance                 IN NUMBER,
1988 			       p_base_variance            IN NUMBER ) IS
1989 
1990         -- Cursor to fetch AP variance details for AP variance record
1991         CURSOR ap_var_amt_cur IS
1992         SELECT DECODE(SIGN(p_variance),-1,0, p_variance) entered_dr,
1993 	       DECODE(SIGN(p_variance),-1,ABS(p_variance),0)  entered_cr,
1994                DECODE(SIGN(p_base_variance),-1,0, p_base_variance) accounted_dr,
1995 	       DECODE(SIGN(p_base_variance),-1,ABS(p_base_variance),0)  accounted_cr
1996           FROM dual;
1997 
1998 BEGIN
1999 
2000 	 IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2001             pa_funds_control_pkg.log_message(p_msg_token1 => 'CREATE_APVAR_RECORD - Start');
2002             pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of parameter p_copy_from_index ='||p_copy_from_index);
2003             pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of parameter p_new_rec_index ='||p_new_rec_index);
2004             pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of parameter p_cwk_po_flag ='||p_cwk_po_flag);
2005             pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of parameter p_accrue_on_receipt_flag ='||p_accrue_on_receipt_flag);
2006             pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of parameter p_variance ='||p_variance);
2007             pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of parameter p_base_variance ='||p_base_variance);
2008             pa_funds_control_pkg.log_message(p_msg_token1 => 'Check if its a CWK OR accrure on receipt PO');
2009          End if;
2010 
2011          IF (p_cwk_po_flag = 'Y' OR p_accrue_on_receipt_flag = 'Y') THEN
2012 
2013             IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2014               pa_funds_control_pkg.log_message(p_msg_token1 => 'Its a CWK OR accrue on receipt PO hence relieve only the variance for AP');
2015               pa_funds_control_pkg.log_message(p_msg_token1 => 'Overwriting main AP record amount columns ');
2016             End if;
2017 
2018             -- As only AP variance should be fundschecked , updating AP distribution amount columns with
2019 	    -- varaince amount
2020 
2021 	    OPEN ap_var_amt_cur;
2022             FETCH ap_var_amt_cur INTO g_tab_entered_dr(p_copy_from_index),
2023 	                              g_tab_entered_cr(p_copy_from_index),
2024                                       g_tab_accounted_dr(p_copy_from_index),
2025 				      g_tab_accounted_cr(p_copy_from_index);
2026             CLOSE ap_var_amt_cur;
2027 
2028           ELSE -- IF (p_cwk_po_flag = 'Y' OR p_accrue_on_receipt_flag = 'Y') THEN
2029 
2030             IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2031               pa_funds_control_pkg.log_message(p_msg_token1 => 'Its NOT a CWK OR accrue on receipt PO hence create new record for ap variance');
2032               pa_funds_control_pkg.log_message(p_msg_token1 => 'Calling COPY_AP_RECORD');
2033             End if;
2034 
2035  	     -- As AP variance should be fundschecked in addition to AP distribution,
2036 	     -- creating new record for AP varaince line
2037 
2038        	    COPY_AP_RECORD (p_copy_from_index,p_new_rec_index );
2039 
2040             g_tab_entered_amount(p_new_rec_index)           :=  p_variance;
2041 	    g_tab_accted_amount(p_new_rec_index)            :=  p_base_variance;
2042 
2043 	    OPEN ap_var_amt_cur;
2044             FETCH ap_var_amt_cur INTO g_tab_entered_dr(p_new_rec_index),
2045 	                              g_tab_entered_cr(p_new_rec_index),
2046                                       g_tab_accounted_dr(p_new_rec_index),
2047 				      g_tab_accounted_cr(p_new_rec_index);
2048             CLOSE ap_var_amt_cur;
2049 
2050           END IF; -- IF (p_cwk_po_flag = 'Y' OR p_accrue_on_receipt_flag = 'Y') THEN
2051 
2052 	 IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2053             pa_funds_control_pkg.log_message(p_msg_token1 => 'CREATE_APVAR_RECORD - End');
2054          End if;
2055 
2056 EXCEPTION
2057 WHEN OTHERS THEN
2058 
2059   IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2060       pa_funds_control_pkg.log_message(p_msg_token1 => 'CREATE_APVAR_RECORD - Exception'||SQLERRM);
2061   End if;
2062 
2063   RAISE;
2064 
2065 END CREATE_APVAR_RECORD;
2066 
2067 -----------------------------------------------------------------------------------
2068 -- R12 Funds management Uptake : New procedure to create additional records
2069 -- for associated PO/RELEASE/AMOUNT variance/Quantity Variance while
2070 -- fundschecking AP records.
2071 -----------------------------------------------------------------------------------
2072 
2073 PROCEDURE CREATE_BACKING_PO_APVAR_REC (p_copy_from_index    IN NUMBER) IS
2074 
2075        l_cwk_po_flag               VARCHAR2(1);
2076        l_accrue_on_receipt_flag    VARCHAR2(1);
2077        l_new_rec_index             NUMBER;
2078 
2079         -- Cursor to fetch PO details for PO relieving record
2080         CURSOR c_po_cwk_accrue_details_cur(p_po_distribution_id  NUMBER) IS
2081         SELECT pa_funds_control_utils2.is_CWK_PO
2082 	                (po.po_header_id,
2083 			 po.po_line_id,
2084 			 po.po_distribution_id,
2085 			 po.org_id)                 cwk_po_flag,
2086                NVL(po.accrue_on_receipt_flag,'N')   accrue_on_receipt_flag
2087           FROM po_distributions_all po
2088          WHERE po.po_distribution_id = p_po_distribution_id;
2089 
2090 BEGIN
2091 
2092        IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2093          pa_funds_control_pkg.log_message(p_msg_token1 => 'CREATE_BACKING_PO_APVAR_REC - p_copy_from_index = '
2094             ||p_copy_from_index);
2095          pa_funds_control_pkg.log_message(p_msg_token1 => 'CREATE_BACKING_PO_APVAR_REC - Start');
2096        End if;
2097 
2098        -- Below is the logic of splitting AP line into multiple bc records
2099        -- 1.IF po_distribution id populated then create PO relieving record
2100        --   a. IF amount variance exists then
2101        --         IF (CWK PO and interface CWK PO to projects is enabled at implementation level)  OR
2102        --            (accrue on receipt is YES) THEN
2103        --           update AP line amounts to variance amount as only variance is eligible for FC
2104        --         ELSE
2105        --           Create new BC record for variance line since both AP line and variance
2106        --           are eligible for FC
2107        --         END IF;
2108        --   b. IF quantity varaince exists then
2109        --           Create new BC record for variance line since both main AP line and variance
2110        --           are eligible for FC .-- Functionality to be verified.
2111 
2112        OPEN  c_po_cwk_accrue_details_cur(g_tab_ap_po_distribution_id(p_copy_from_index));
2113        FETCH c_po_cwk_accrue_details_cur INTO l_cwk_po_flag,
2114 				              l_accrue_on_receipt_flag;
2115        CLOSE c_po_cwk_accrue_details_cur;
2116 
2117        IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2118             pa_funds_control_pkg.log_message(p_msg_token1 => 'CREATE_BACKING_PO_APVAR_REC - l_cwk_po_flag ='||l_cwk_po_flag );
2119             pa_funds_control_pkg.log_message(p_msg_token1 => 'CREATE_BACKING_PO_APVAR_REC - l_accrue_on_receipt_flag ='||l_accrue_on_receipt_flag );
2120        End if;
2121 
2122        -- Calling procedure to relieve PO record only if non-CWK PO/ accrue on recipt is unchecked
2123        IF l_cwk_po_flag <> 'Y' AND l_accrue_on_receipt_flag <> 'Y' THEN
2124 
2125           IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2126              pa_funds_control_pkg.log_message(p_msg_token1 => 'CREATE_BACKING_PO_APVAR_REC - before CREATE_AP_PO_RECORD');
2127           End if;
2128 
2129           l_new_rec_index  := g_tab_doc_header_id.count + 1  ;
2130 
2131           CREATE_AP_PO_RECORD (p_copy_from_index,
2132                                l_new_rec_index );
2133 
2134           IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2135              pa_funds_control_pkg.log_message(p_msg_token1 => 'CREATE_BACKING_PO_APVAR_REC - After CREATE_AP_PO_RECORD');
2136           End if;
2137 
2138        END IF; --IF l_cwk_po_flag <> 'Y' AND l_accrue_on_receipt_flag <> 'Y' THEN
2139 
2140 
2141        IF NVL(g_tab_ap_amount_variance(p_copy_from_index),0) <> 0 THEN
2142 
2143           IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2144              pa_funds_control_pkg.log_message(p_msg_token1 => 'CREATE_BACKING_PO_APVAR_REC - before CREATE_APVAR_RECORD for amount variance');
2145           End if;
2146 
2147           l_new_rec_index  := g_tab_doc_header_id.count + 1  ;
2148 
2149           CREATE_APVAR_RECORD (p_copy_from_index          => p_copy_from_index,
2150                                p_new_rec_index            => l_new_rec_index,
2151 			       p_cwk_po_flag              => l_cwk_po_flag ,
2152 			       p_accrue_on_receipt_flag   => l_accrue_on_receipt_flag,
2153 			       p_variance                 => g_tab_ap_amount_variance(p_copy_from_index),
2154 			       p_base_variance            => g_tab_ap_base_amount_variance(p_copy_from_index));
2155 
2156           IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2157              pa_funds_control_pkg.log_message(p_msg_token1 => 'CREATE_BACKING_PO_APVAR_REC - After CREATE_APVAR_RECORD');
2158           End if;
2159 
2160        END IF; --IF NVL(g_tab_ap_amount_variance(p_copy_from_index),0) <> 0 THEN
2161 
2162 
2163        IF NVL(g_tab_ap_quantity_variance(p_copy_from_index),0) <> 0 THEN
2164 
2165           IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2166              pa_funds_control_pkg.log_message(p_msg_token1 => 'CREATE_BACKING_PO_APVAR_REC - before CREATE_APVAR_RECORD for QTY variance');
2167           End if;
2168 
2169           l_new_rec_index  := g_tab_doc_header_id.count + 1  ;
2170 
2171           CREATE_APVAR_RECORD (p_copy_from_index          => p_copy_from_index,
2172                                p_new_rec_index            => l_new_rec_index,
2173 			       p_cwk_po_flag              => l_cwk_po_flag ,
2174 			       p_accrue_on_receipt_flag   => l_accrue_on_receipt_flag,
2175 			       p_variance                 => g_tab_ap_quantity_variance(p_copy_from_index),
2176 			       p_base_variance            => g_tab_ap_base_qty_variance(p_copy_from_index));
2177 
2178           IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2179              pa_funds_control_pkg.log_message(p_msg_token1 => 'CREATE_BACKING_PO_APVAR_REC - After CREATE_APVAR_RECORD');
2180           End if;
2181 
2182        END IF;
2183 
2184    IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2185       pa_funds_control_pkg.log_message(p_msg_token1 => ' CREATE_BACKING_PO_APVAR_REC - End  ');
2186    End if;
2187 
2188 EXCEPTION
2189 WHEN OTHERS THEN
2190 
2191   IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2192       pa_funds_control_pkg.log_message(p_msg_token1 => 'CREATE_BACKING_PO_APVAR_REC - Exception'||SQLERRM);
2193   End if;
2194 
2195   RAISE;
2196 
2197 END CREATE_BACKING_PO_APVAR_REC;
2198 
2199 -----------------------------------------------------------------------------------
2200 -- R12 Funds Management Uptake
2201 -- Procedure to derive budget_ccid and budget_line_id for AP/PO/REQ trnsactions
2202 -------------------------------------------------------------------------------------
2203 
2204 PROCEDURE DERIVE_PKT_RLMI_BUDGET_CCID (p_packet_id IN NUMBER,
2205                                        p_bc_mode IN VARCHAR2) IS
2206 
2207  PRAGMA AUTONOMOUS_TRANSACTION;
2208 
2209  CURSOR c_pkt_SOB IS
2210  SELECT DISTINCT pbc.set_of_books_id
2211   FROM  pa_bc_packets pbc
2212  WHERE  pbc.packet_id = p_packet_id
2213    AND pbc.status_code = 'I'
2214    AND substr(nvl(pbc.result_code,'P'),1,1) not in ('R','F');
2215 
2216  CURSOR c_pkt_details IS
2217  SELECT DISTINCT
2218         pbc.project_id,
2219         pbc.task_id,
2220         pbc.period_name,
2221         pbc.gl_date,
2222         pbc.set_of_books_id,
2223         pbc.budget_version_id,
2224         pm.entry_level_code,
2225         DECODE(pm.entry_level_code,'P',0,pt.top_task_id) top_task_id,
2226         pbc.resource_list_member_id
2227   FROM  pa_bc_packets pbc,
2228         pa_tasks pt,
2229         pa_budget_versions bv,
2230         pa_budget_entry_methods pm
2231  WHERE  pbc.packet_id = p_packet_id
2232    AND pbc.budget_version_id = bv.budget_version_id
2233    AND bv.budget_entry_method_code = pm.budget_entry_method_code
2234    AND pbc.status_code = 'I'
2235    --AND substr(nvl(pbc.result_code,'P'),1,1) not in ('R','F')
2236    AND pt.task_id = pbc.task_id
2237    and nvl(ext_bdgt_flag,'N') = 'Y';
2238 
2239 
2240 CURSOR c_get_gl_start_date (p_period_name VARCHAR2,
2241                             p_sob_id  NUMBER ) IS
2242 SELECT gl.start_date
2243  FROM  gl_period_statuses gl
2244  WHERE gl.application_id = 101
2245    AND gl.set_of_books_id = p_sob_id
2246    AND gl.period_name  = p_period_name;
2247 
2248 l_budget_line_id     pa_bc_packets.budget_line_id%TYPE;
2249 l_budget_ccid        pa_bc_packets.budget_ccid%TYPE;
2250 l_return_status      VARCHAR2(10) := 'S';
2251 l_error_message_code VARCHAR2(200) := NULL;
2252 l_gl_start_date      DATE;
2253 
2254 BEGIN
2255 
2256   IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2257       pa_funds_control_pkg.log_message(p_msg_token1 => 'DERIVE_PKT_RLMI_BUDGET_CCID - Start');
2258   End if;
2259 
2260   -- This will loop only once as a packet_id will have only one distinct SOB
2261   FOR c_sob IN c_pkt_SOB LOOP
2262 
2263       IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2264          pa_funds_control_pkg.log_message(p_msg_token1 => 'Calling pa_funds_control_pkg.derive_rlmi for c_sob.set_of_books_id '||c_sob.set_of_books_id);
2265       End if;
2266 
2267       pa_funds_control_pkg.derive_rlmi
2268            ( p_packet_id      => p_packet_id,
2269              p_mode           => 'R',
2270              p_sob            => c_sob.set_of_books_id,
2271        	     p_calling_module => 'GL');
2272 
2273   END LOOP;
2274 
2275 
2276   FOR c_pkt in c_pkt_details LOOP
2277 
2278       IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2279          pa_funds_control_pkg.log_message(p_msg_token1 => 'Calling PA_FUNDS_CONTROL_UTILS.Get_Budget_CCID ');
2280       End if;
2281 
2282       OPEN c_get_gl_start_date(c_pkt.period_name,c_pkt.set_of_books_id);
2283       FETCH c_get_gl_start_date INTO l_gl_start_date;
2284       CLOSE c_get_gl_start_date;
2285 
2286 
2287       PA_FUNDS_CONTROL_UTILS.Get_Budget_CCID (
2288                  p_project_id 		=> c_pkt.project_id,
2289                  p_task_id    		=> c_pkt.task_id,
2290                  p_res_list_mem_id 	=> c_pkt.resource_list_member_id,
2291                  --p_period_name  	=> c_pkt.period_name,
2292 		 p_start_date		=> l_gl_start_date,
2293                  p_budget_version_id 	=> c_pkt.budget_version_id,
2294 		 p_top_task_id		=> c_pkt.top_task_id,
2295 		 p_entry_level_code     => c_pkt.entry_level_code,
2296                  x_budget_ccid  	=> l_budget_ccid,
2297                  x_budget_line_id       => l_budget_line_id,
2298                  x_return_status 	=> l_return_status,
2299                  x_error_message_code 	=> l_error_message_code);
2300 
2301       IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2302          pa_funds_control_pkg.log_message(p_msg_token1 => 'After PA_FUNDS_CONTROL_UTILS.Get_Budget_CCID ');
2303          pa_funds_control_pkg.log_message(p_msg_token1 => 'c_pkt.project_id = '||c_pkt.project_id);
2304          pa_funds_control_pkg.log_message(p_msg_token1 => 'c_pkt.task_id = '||c_pkt.task_id);
2305          pa_funds_control_pkg.log_message(p_msg_token1 => 'c_pkt.resource_list_member_id = '||c_pkt.resource_list_member_id);
2306          pa_funds_control_pkg.log_message(p_msg_token1 => 'c_pkt.gl_date = '||c_pkt.gl_date);
2307          pa_funds_control_pkg.log_message(p_msg_token1 => 'c_pkt.budget_version_id = '||c_pkt.budget_version_id);
2308          pa_funds_control_pkg.log_message(p_msg_token1 => 'c_pkt.top_task_id = '||c_pkt.top_task_id);
2309          pa_funds_control_pkg.log_message(p_msg_token1 => 'c_pkt.entry_level_code = '||c_pkt.entry_level_code);
2310          pa_funds_control_pkg.log_message(p_msg_token1 => 'l_budget_ccid = '||l_budget_ccid);
2311          pa_funds_control_pkg.log_message(p_msg_token1 => 'l_budget_line_id = '||l_budget_line_id);
2312          pa_funds_control_pkg.log_message(p_msg_token1 => 'l_return_status = '||l_return_status);
2313          pa_funds_control_pkg.log_message(p_msg_token1 => 'l_error_message_code = '||l_error_message_code);
2314       End if;
2315 
2316       -- Fail pa bc packets if there is any error while deriving the budget ccid value
2317       IF l_return_status = 'E' OR NVL(l_budget_ccid,-999) = -999 OR NVL(l_budget_line_id,-999) = -999  THEN
2318 
2319 
2320          UPDATE  pa_bc_packets
2321             set  budget_ccid              = l_budget_ccid,
2322                  budget_line_id           = l_budget_line_id,
2323                  status_code              = DECODE(status_code,'F',status_code,'R',status_code,'T',status_code,DECODE(p_bc_mode,'C','F','R')),
2324                  result_code              = DECODE(substr(result_code,1,1),'F',result_code,'F132'),
2325                  PROJECT_RESULT_CODE      = DECODE(substr(PROJECT_RESULT_CODE,1,1),'F',PROJECT_RESULT_CODE,'F132'),
2326                  TASK_RESULT_CODE         = DECODE(substr(TASK_RESULT_CODE,1,1),'F',TASK_RESULT_CODE,'F132'),
2327                  RES_GRP_RESULT_CODE      = DECODE(substr(RES_GRP_RESULT_CODE,1,1),'F',RES_GRP_RESULT_CODE,'F132'),
2328                  RES_RESULT_CODE          = DECODE(substr(RES_RESULT_CODE,1,1),'F',RES_RESULT_CODE,'F132'),
2329                  TOP_TASK_RESULT_CODE     = DECODE(substr(TOP_TASK_RESULT_CODE,1,1),'F',TOP_TASK_RESULT_CODE,'F132'),
2330                  PROJECT_ACCT_RESULT_CODE = DECODE(substr(PROJECT_ACCT_RESULT_CODE,1,1),'F',PROJECT_ACCT_RESULT_CODE,'F132')
2331            WHERE packet_id = p_packet_id
2332              AND status_code ='I'
2333              AND project_id = c_pkt.project_id
2334              AND task_id = c_pkt.task_id
2335              AND resource_list_member_id = c_pkt.resource_list_member_id
2336              AND NVL(period_name,'X')  = NVL(c_pkt.period_name,'X')
2337              AND gl_date = c_pkt.gl_date ;
2338 
2339           IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2340              pa_funds_control_pkg.log_message(p_msg_token1 => 'DERIVE_PKT_RLMI_BUDGET_CCID - # of records updated to F132='||SQL%ROWCOUNT);
2341           End if;
2342 
2343       ELSE
2344 
2345 
2346          UPDATE  pa_bc_packets
2347             set  budget_ccid              = l_budget_ccid,
2348                  budget_line_id           = l_budget_line_id
2349            WHERE packet_id = p_packet_id
2350              AND status_code ='I'
2351              AND project_id = c_pkt.project_id
2352              AND task_id = c_pkt.task_id
2353              AND resource_list_member_id = c_pkt.resource_list_member_id
2354              AND NVL(period_name,'X')  = NVL(c_pkt.period_name,'X')
2355              AND gl_date = c_pkt.gl_date ;
2356 
2357           IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2358              pa_funds_control_pkg.log_message(p_msg_token1 => 'DERIVE_PKT_RLMI_BUDGET_CCID - # of records updated with budget ccid info='||SQL%ROWCOUNT);
2359           End if;
2360 
2361       END IF;
2362 
2363    END LOOP;
2364 
2365   COMMIT;
2366 
2367   IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2368       pa_funds_control_pkg.log_message(p_msg_token1 => 'DERIVE_PKT_RLMI_BUDGET_CCID - End');
2369   End if;
2370 
2371 EXCEPTION
2372 WHEN OTHERS THEN
2373 
2374   IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2375       pa_funds_control_pkg.log_message(p_msg_token1 => 'DERIVE_PKT_RLMI_BUDGET_CCID - EXception'||SQLERRM);
2376   End if;
2377 
2378   RAISE;
2379 
2380 END DERIVE_PKT_RLMI_BUDGET_CCID;
2381 
2382 -----------------------------------------------------------------------------------
2383 -- R12 Funds Management Uptake
2384 -- Procedure to derive entered_dr,entered_cr,accounted_dr,accounted_cr columns
2385 -- for PO and REQ  distributions
2386 -------------------------------------------------------------------------------------
2387 PROCEDURE DERIVE_DR_CR IS
2388 BEGIN
2389 
2390     IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2391        pa_funds_control_pkg.log_message(p_msg_token1 => 'Start of DERIVE_DR_CR ');
2392     End if;
2393 
2394    FOR l_index IN 1..g_tab_set_of_books_id.Last LOOP
2395 
2396      g_tab_entered_dr(l_index)   := 0;
2397      g_tab_entered_cr(l_index)   := 0 ;
2398      g_tab_accounted_dr(l_index) := 0 ;
2399      g_tab_accounted_cr(l_index) := 0 ;
2400 
2401      IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2402        pa_funds_control_pkg.log_message(p_msg_token1 => 'value of  g_tab_main_or_backing_code '||g_tab_main_or_backing_code(l_index));
2403        pa_funds_control_pkg.log_message(p_msg_token1 => 'value of  g_tab_event_type_code '||g_tab_event_type_code(l_index));
2404        pa_funds_control_pkg.log_message(p_msg_token1 => 'value of  g_tab_distribution_type '||g_tab_distribution_type(l_index));
2405      End if;
2406 
2407      -- Below function returns 1 if the amount should be populated in debit columns
2408      -- and -1 if amounts should be populated in credit columns
2409 
2410      IF PA_FUNDS_CONTROL_UTILS.DERIVE_PO_REQ_AMT_SIDE
2411                                (g_tab_event_type_code(l_index) ,
2412                                 g_tab_main_or_backing_code(l_index),
2413                                 g_tab_distribution_type(l_index) ) = 1 THEN
2414 
2415    	g_tab_entered_dr(l_index)   := g_tab_entered_amount(l_index);
2416     	g_tab_accounted_dr(l_index) := g_tab_accted_amount (l_index);
2417 
2418     ELSE
2419     	g_tab_entered_cr(l_index)   := g_tab_entered_amount(l_index);
2420      	g_tab_accounted_cr(l_index) := g_tab_accted_amount (l_index);
2421     END IF;
2422 
2423     IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2424        pa_funds_control_pkg.log_message(p_msg_token1 => 'value of  g_tab_entered_dr '||g_tab_entered_dr(l_index));
2425        pa_funds_control_pkg.log_message(p_msg_token1 => 'value of  g_tab_entered_cr '||g_tab_entered_cr(l_index));
2426        pa_funds_control_pkg.log_message(p_msg_token1 => 'value of  g_tab_accounted_dr '||g_tab_accounted_dr(l_index));
2427        pa_funds_control_pkg.log_message(p_msg_token1 => 'value of  g_tab_accounted_cr '||g_tab_accounted_cr(l_index));
2428     End if;
2429 
2430   END LOOP;
2431 
2432     IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2433        pa_funds_control_pkg.log_message(p_msg_token1 => 'End of DERIVE_DR_CR ');
2434     End if;
2435 
2436 END DERIVE_DR_CR;
2437 
2438 -----------------------------------------------------------------------------------
2439 -- R12 Funds Management Uptake
2440 -- Procedure to fail records in FULL mode if any error during insert API
2441 -------------------------------------------------------------------------------------
2442 
2443 PROCEDURE FULL_MODE_FAILURE (p_packet_id     IN NUMBER,
2444                              p_bc_mode       IN VARCHAR2,
2445                              x_return_code   OUT NOCOPY VARCHAR2) IS
2446 
2447 PRAGMA AUTONOMOUS_TRANSACTION;
2448 
2449 CURSOR c_pkt_status IS
2450 SELECT 1
2451   FROM pa_bc_packets
2452  WHERE packet_id = p_packet_id
2453    AND (status_code in ('F','T','R')
2454         OR SUBSTR (result_code,1,1) = 'F');
2455 
2456 l_counter                   NUMBER := 0;
2457 
2458 BEGIN
2459 
2460  IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2461     pa_funds_control_pkg.log_message(p_msg_token1 => 'FULL_MODE_FAILURE : Start ');
2462  END IF;
2463 
2464  x_return_code := 'S' ;
2465 
2466  OPEN c_pkt_status;
2467  FETCH c_pkt_status INTO l_counter;
2468  CLOSE c_pkt_status;
2469 
2470  IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2471     pa_funds_control_pkg.log_message(p_msg_token1 => 'FULL_MODE_FAILURE : l_counter  '||l_counter);
2472  END IF;
2473 
2474  IF l_counter > 0 THEN
2475 
2476      x_return_code := 'F' ;
2477 
2478      UPDATE pa_bc_packets a
2479         SET  a.status_code            = DECODE(p_bc_mode,'C','F','R'),
2480              a.result_code            = DECODE( SUBSTR (result_code,1,1),'F',result_code,'F170'),
2481              res_result_code          = DECODE(substr(res_result_code,1,1),'F',res_result_code,'F170'),
2482              res_grp_result_code      = DECODE(substr(res_grp_result_code,1,1),'F',res_grp_result_code,'F170'),
2483              task_result_code         = DECODE(substr(task_result_code,1,1),'F',task_result_code,'F170'),
2484              top_task_result_code     = DECODE(substr(top_task_result_code,1,1),'F',top_task_result_code,'F170'),
2485              project_result_code      = DECODE(substr(project_result_code,1,1),'F',project_result_code,'F170'),
2486              project_acct_result_code = DECODE(substr(project_acct_result_code,1,1),'F',project_acct_result_code,'F170')
2487       WHERE  a.status_code = 'I'
2488         AND  a.status_code <> 'F'
2489         AND  a.packet_id = p_packet_id;
2490 
2491      IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2492         pa_funds_control_pkg.log_message(p_msg_token1 => 'FULL_MODE_FAILURE : number of records failed in full mode  '||SQL%ROWCOUNT);
2493      END IF;
2494 
2495  END IF;
2496 
2497  IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2498     pa_funds_control_pkg.log_message(p_msg_token1 => 'FULL_MODE_FAILURE : End');
2499  END IF;
2500 
2501  COMMIT;
2502 
2503 END;
2504 
2505 -----------------------------------------------------------------------------------
2506 -- R12 Funds Management Uptake
2507 -- Autonomous Procedure to fail bc records which have no bc event generated
2508 -------------------------------------------------------------------------------------
2509 
2510 PROCEDURE FAIL_NULL_EVENT_PKTS (p_packet_id     IN NUMBER,
2511                                 p_bc_mode       IN VARCHAR2) IS
2512 
2513 PRAGMA AUTONOMOUS_TRANSACTION;
2514 
2515 BEGIN
2516 
2517  IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2518     pa_funds_control_pkg.log_message(p_msg_token1 => 'FAIL_NULL_EVENT_PKTS : Start ');
2519  END IF;
2520 
2521 UPDATE pa_bc_packets a
2522    SET a.status_code = DECODE(p_bc_mode,'C','F','R'),
2523        a.result_code = 'F168' ,
2524        res_result_code          = DECODE(substr(res_result_code,1,1),'F',res_result_code,'F168'),
2525        res_grp_result_code      = DECODE(substr(res_grp_result_code,1,1),'F',res_grp_result_code,'F168'),
2526        task_result_code         = DECODE(substr(task_result_code,1,1),'F',task_result_code,'F168'),
2527        top_task_result_code     = DECODE(substr(top_task_result_code,1,1),'F',top_task_result_code,'F168'),
2528        project_result_code      = DECODE(substr(project_result_code,1,1),'F',project_result_code,'F168'),
2529        project_acct_result_code = DECODE(substr(project_acct_result_code,1,1),'F',project_acct_result_code,'F168')
2530  WHERE a.status_code = 'I'
2531    AND a.ext_bdgt_flag = 'Y'
2532    AND a.packet_id = p_packet_id
2533    AND a.bc_event_id is null;
2534 
2535  COMMIT;
2536 
2537  IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2538     pa_funds_control_pkg.log_message(p_msg_token1 => 'FAIL_NULL_EVENT_PKTS : End');
2539  END IF;
2540 
2541 END;
2542 
2543 
2544 -----------------------------------------------------------------------------------
2545 -- R12 Funds Management Uptake
2546 -- Autonomous Procedure to fail all dangling bc records created in previous run
2547 -------------------------------------------------------------------------------------
2548 
2549 PROCEDURE FAIL_DANGLING_PKTS (p_packet_id     IN NUMBER) IS
2550 
2551 PRAGMA AUTONOMOUS_TRANSACTION;
2552 
2553 BEGIN
2554 
2555  IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2556     pa_funds_control_pkg.log_message(p_msg_token1 => 'FAIL_DANGLING_PKTS : Start ');
2557  END IF;
2558 
2559 UPDATE pa_bc_packets
2560    SET status_code = 'T',
2561        result_code = 'F142',
2562        res_result_code          = DECODE(substr(res_result_code,1,1),'F',res_result_code,'F142'),
2563        res_grp_result_code      = DECODE(substr(res_grp_result_code,1,1),'F',res_grp_result_code,'F142'),
2564        task_result_code         = DECODE(substr(task_result_code,1,1),'F',task_result_code,'F142'),
2565        top_task_result_code     = DECODE(substr(top_task_result_code,1,1),'F',top_task_result_code,'F142'),
2566        project_result_code      = DECODE(substr(project_result_code,1,1),'F',project_result_code,'F142'),
2567        project_acct_result_code = DECODE(substr(project_acct_result_code,1,1),'F',project_acct_result_code,'F142')
2568 WHERE  packet_id <> p_packet_id
2569   AND  status_code = 'I';
2570 
2571  COMMIT;
2572 
2573  IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
2574     pa_funds_control_pkg.log_message(p_msg_token1 => 'FAIL_DANGLING_PKTS : End');
2575  END IF;
2576 
2577 END;
2578 
2579 -----------------------------------------------------------------------------------
2580 -- R12 Funds Management Uptake --rshaik
2581 -- ----------------------------
2582 -- Called from PSA_BC_XLA_PVT.Budgetary_control after creating events for AP/PO/REQ.
2583 -- and before It performs following tasks :
2584 -- 1. Driving table for this procedure is psa_bc_xla_events_gt .Picks all AP/PO/REQ events
2585 --    created by BCPSA FC engine.
2586 -- 2. Inserts raw records into pa_bc_packets by fetching PO/REQ data from po_bc_distributions
2587 --    and AP data from ap_invoice_distributions_all table for all events in psa_bc_xla_events_gt.
2588 -- 3. Fires populate_burden_cost procedure to Insert burden records for above raw components
2589 -- 4. Fires pa_funds_control_pkg.derive_rlmi to derive resource_list_member_id on bc packets
2590 -- 5. Fires pa_funds_control_utils.get_budegt_ccid to derive budget_ccid and budget_line_id
2591 -- 6. Finally calls pa_xla_interface_pkg.create_events to create events for project
2592 --    encumbrance data and populate BC GT table.
2593 -------------------------------------------------------------------------------------
2594 
2595 PROCEDURE CREATE_PROJ_ENCUMBRANCE_EVENTS  (p_application_id      IN         NUMBER ,
2596                                		   p_partial_flag        IN         VARCHAR2 DEFAULT 'N' ,
2597                                            p_bc_mode             IN         VARCHAR2 DEFAULT 'C' ,
2598                                            x_return_code         OUT NOCOPY VARCHAR2 ) IS
2599 
2600         l_return_status    VARCHAR2(100);
2601 	l_debug_mode       VARCHAR2(10);
2602 	l_err_msg_code      VARCHAR2(2000);
2603 
2604         -- R12 funds management uptake : Cursor to fetch PO data from po_bc_distributions.
2605 	-- This global temporary table has records for PO and also for requisition which
2606 	-- needs to be unreserved
2607 
2608 	CURSOR cur_po_bc_dist IS
2609 	    SELECT NULL                                                      budget_version_id,
2610 	           NULL                                                      budget_line_id,
2611 	           NULL                                                      budget_ccid,
2612  		   pobc.pa_project_id                                        project_id,
2613 		   pobc.pa_task_id                                           task_id,
2614 		   pobc.pa_exp_type                                          expenditure_type,
2615 		   pobc.pa_exp_org_id                                        expenditure_organization_id,
2616 		   pobc.pa_exp_item_date                                     expenditure_item_date,
2617 		   pobc.ledger_id                                            set_of_books_id,
2618  		   -- The below hardcoded values for Je_source_name and Je_category_name columns
2619 		   -- are based on SLA seed data for PO and REQ. These values are later
2620 		   -- updated by pa_funds_check API to synch up with gl_bc_packets if different
2621                    'Purchasing'                                              je_source_name,
2622    		   DECODE(pobc.distribution_type,
2623 			       'REQUISITION','Requisitions',
2624                                'BLANKET'   ,'Release',
2625         		       'SCHEDULED' ,'Release',
2626 			       'Purchases')                                  je_category_name,
2627                    DECODE(pobc.distribution_type,'REQUISITION','REQ','PO')   document_type,
2628                    pobc.header_id	                                     document_header_id,
2629 		   -- Populated for PA Purchasing extracts.And with this change document_line_id
2630 		   -- is no longer a unique way of identifying if the BC packet is associated with CWK
2631                    pobc.line_id                                              document_line_id,
2632 		   pobc.distribution_id                                      document_distribution_id,
2633 		   'E'                                                       actual_flag,
2634 		   NULL                                                      result_code,
2635 		   'I'                                                       status_code,
2636 		   pobc.event_type_code                                      event_type_code,
2637                    pobc.entered_amt                                          entered_amount,
2638                    pobc.accounted_amt                                        accounted_amount,
2639                    'N'                                                       balance_posted_flag,
2640                    'T'                                                       funds_process_mode,
2641                    'N'                                                       burden_cost_flag,
2642   		   NULL                                                      org_id,
2643 		   DECODE(pobc.distribution_type,'REQUISITION','REQ','PO')   reference1,
2644 		   pobc.header_id                                            reference2,
2645 		   pobc.distribution_id                                      reference3,
2646                    NULL                                                      bc_event_id,
2647 		   NULL                                                      vendor_id,
2648 		   pa_funds_control_pkg.check_bdn_on_sep_item
2649 		                                 (pobc.pa_project_id)        burden_method_code,
2650 		   pobc.main_or_backing_code                                 main_or_backing_code,
2651                    pobc.ae_event_id                                          source_event_id,
2652 		   pobc.CODE_COMBINATION_ID                                  txn_ccid,
2653 		   NULL                                                      parent_bc_packet_id,
2654                    pa_funds_control_utils.get_fnd_reqd_flag
2655 			           (pobc.pa_project_id,'STD')                fck_reqd_flag,
2656                    pobc.gl_date                                              gl_date,
2657                    pogt.period_name                                          period_name,
2658                    -- Below code is added to handle Requisition adjusment scenario :
2659                    -- If REQ adjusted line has adjustment type as 'OLD' then amount previously reserved should be relieved
2660                    -- else if REQ adjusted line has adjustment type as 'NEW' then new amount should be reserved
2661                    -- i.e. IF distribution_type = 'REQUISITION_ADJUSTED_OLD' then CR
2662                    --      IF distribution_type = 'REQUISITION_ADJUSTED_NEW' then DR
2663                    DECODE(event_type_code, 'REQ_ADJUSTED',
2664 		           DECODE(pobc.main_or_backing_code,'M',
2665 		                  DECODE(pobc.adjustment_status
2666 				         ,'OLD',pobc.distribution_type||'_ADJUSTED_OLD'
2667 		                         ,'NEW',pobc.distribution_type||'_ADJUSTED_NEW'
2668 		                         ,pobc.distribution_type),
2669 		                  pobc.distribution_type),
2670 		          pobc.distribution_type)                            distribution_type,
2671                    DECODE(pobc.distribution_type,'SCHEDULED',pobc.po_release_id
2672 		                                ,'BLANKET',pobc.po_release_id
2673 						,NULL)                       release_id,
2674                    DECODE(pobc.distribution_type,'REQUISITION'
2675 		                                ,1000
2676 						,decode(pobc.main_or_backing_code, 'B_REQ', 1000, 1001)) encumbrance_type_id,
2677 		   -- Bug 5403775 : Added below columns to derive pkt reference columns for backing docs such that
2678 		   -- they will point to the main doc
2679 		   POBC.origin_sequence_num,
2680 		   pobc.applied_to_dist_id_2,
2681 		   pa_bc_packets_s.nextval                                   bc_packet_id -- Bug 5406690
2682              FROM  po_bc_distributions pobc ,
2683                    po_encumbrance_gt   pogt,
2684 	           psa_bc_xla_events_gt xlaevt
2685             WHERE  pobc.ae_event_id = xlaevt.event_id
2686     	      AND  pobc.pa_project_id IS NOT NULL
2687     	      AND  pogt.distribution_id = pobc.distribution_id
2688     	      AND  pogt.distribution_type = pobc.distribution_type
2689 	      AND  nvl(pogt.reference15,-999999)= nvl(pobc.reference15,-999999) --Bug 12984297
2690               AND  EXISTS ( SELECT 1
2691 		                FROM  po_requisition_lines_all porl
2692 				WHERE nvl(porl.DESTINATION_TYPE_CODE,'EXPENSE') = 'EXPENSE'
2693 				  AND porl.requisition_line_id = pobc.line_id
2694 				  AND pobc.distribution_type = 'REQUISITION'
2695                                UNION ALL
2696 			       SELECT 1
2697 		                FROM  po_distributions_all pord
2698 				WHERE nvl(pord.DESTINATION_TYPE_CODE,'EXPENSE') = 'EXPENSE'
2699 				  AND pord.po_distribution_id = pobc.distribution_id
2700 				  AND pobc.distribution_type <> 'REQUISITION' )
2701               /** Bug fix : 2347699 added this check to make sure that project is not cross charged **/
2702 	      /** BugFix : If grants and projects enables in the same ou then errors with no data found in cursors*/
2703        	      AND  EXISTS (SELECT  1
2704                                 FROM  pa_projects_all pp,
2705  				      pa_implementations_all imp
2706                 	       WHERE  pp.project_id = pobc.pa_project_id
2707 	        	         AND  pp.org_id  = imp.org_id
2708 			         AND  imp.set_of_books_id = pobc.ledger_id )
2709               AND EXISTS ( SELECT 'Project Bdgt Ctrl enabled'
2710                                  FROM pa_budget_types bdgttype
2711                                       ,pa_budgetary_control_options pbct
2712                                 WHERE pbct.project_id = pobc.pa_project_id
2713                                   AND pbct.BDGT_CNTRL_FLAG = 'Y'
2714                                   AND (pbct.EXTERNAL_BUDGET_CODE = 'GL' OR
2715                                        pbct.EXTERNAL_BUDGET_CODE is NULL
2716                                       )
2717                                   AND pbct.BUDGET_TYPE_CODE = bdgttype.budget_type_code
2718                                   AND bdgttype.budget_amount_code = 'C'
2719                               );
2720 
2721         -- R12 funds management uptake : This cursor is introduced for performance reasons.
2722 	-- This is used to fetch all eligible project related ap invoice distributions getting
2723 	-- processed in this run and then fetch data from bulky AP extracts (cursor cur_ap_bc_dist)
2724 	-- based on the invoice_distribution_id's
2725 	-- Note : This cursor fetches only the AP dist record ,it doesnt fetch associated
2726 	-- PO/RELEASE that has to be unreserved.
2727 
2728         CURSOR c_proj_ap_dist IS
2729 	   SELECT  apd.invoice_distribution_id,
2730 	           DECODE(apd.line_type_lookup_code -- Bug 5490378
2731 		          ,'NONREC_TAX',DECODE(apd.prepay_distribution_id,NULL,apd.line_type_lookup_code,'PREPAY')
2732 			  ,apd.line_type_lookup_code)
2733              FROM  ap_invoice_distributions_all apd ,
2734 	           psa_bc_xla_events_gt xlaevt,
2735 		   ap_invoices_all apinv
2736             WHERE  apd.bc_event_id = xlaevt.event_id
2737 	      AND  apd.project_id IS NOT NULL
2738 	      AND  NVL(apd.pa_addition_flag, 'X' ) <> 'T'
2739 	      AND  apinv.invoice_id = apd.invoice_id
2740 	      AND  apinv.invoice_type_lookup_code <> 'EXPENSE REPORT'
2741 	      AND  apd.line_type_lookup_code <> 'REC_TAX'
2742 	      -- R12 : Prepayments mathed to PO will not be fundschecked
2743 	      AND  ((apinv.invoice_type_lookup_code = 'PREPAYMENT'
2744 	             AND apd.po_distribution_id IS NULL )
2745 		     OR apinv.invoice_type_lookup_code <> 'PREPAYMENT')
2746 	      --R12 : Application of Prepayment matched to PO will not be fundschecked
2747 	      --Bug 5490378 : NONREC_TAX associated with prepay line should be filtered out
2748               AND  NOT EXISTS
2749 	           ( SELECT 1
2750 		       FROM dual
2751 		      WHERE apd.line_type_lookup_code IN ('PREPAY','NONREC_TAX')
2752 			AND apd.prepay_distribution_id IS NOT NULL
2753 			AND apd.po_distribution_id IS NOT NULL)
2754               -- Bug 5562245 : As part of PSA bug 5563122 fix ,code logic has been modified such that
2755 	      -- Variances on AP matched to PO with accrue on receipt will not be fundschecked.
2756               -- Bug 5494476 : AP ITEM Distribution matched to CWK PO will be fundschecked only for the
2757               -- the amout/quantity variance amount.If no variance stamped on the ITEM distribution then
2758 	      -- filter out those distributions.
2759 	      -- Bug 5533290 : AP TAX Distribution matched to CWK PO will be fundschecked only for the
2760               -- the variance amount.Even tough AP TAX is eligible for interface to projects,the commitment amount
2761 	      -- will remain with PO and during interface will be relieved from PO bucket
2762 	      AND NOT EXISTS ( SELECT 1
2763 	                         FROM po_distributions_all pod
2764 				WHERE pod.po_distribution_id = apd.po_distribution_id
2765 				  AND apd.po_distribution_id IS NOT NULL
2766 				  AND ((NVL(pod.accrue_on_receipt_flag,'N') = 'Y' -- Bug 5348212
2767 				        AND apd.line_type_lookup_code IN ('ITEM','ACCRUAL','NONREC_TAX'))
2768 				        OR
2769                                         (pa_funds_control_utils2.is_CWK_PO -- Bug 5494476
2770 				                (pod.po_header_id,pod.po_line_id,pod.po_distribution_id,pod.org_id) = 'Y'
2771 				         AND apd.line_type_lookup_code IN ('ITEM','NONREC_TAX') -- Bug 5533290
2772                                          AND NVL(apd.amount_variance,0)= 0
2773  	     			         AND NVL(apd.base_amount_variance,0)=0
2774 				         AND NVL(apd.quantity_variance,0)=0
2775 				         AND NVL(apd.base_quantity_variance,0)=0
2776 					 )
2777 				       )
2778              		      )
2779               /** Bug fix : 2347699 added this check to make sure that project is not cross charged **/
2780 	      /** BugFix : If grants and projects enables in the same ou then errors with no data found in cursors*/
2781        	      AND  EXISTS (SELECT  1
2782                                 FROM  pa_projects_all pp,
2783  				      pa_implementations_all imp
2784                 	       WHERE  pp.project_id = apd.project_id
2785 	        	         AND  pp.org_id  = imp.org_id
2786 			         AND  imp.set_of_books_id = apd.set_of_books_id )
2787               AND  EXISTS ( SELECT 'Project Bdgt Ctrl enabled'
2788                                  FROM pa_budget_types bdgttype
2789                                       ,pa_budgetary_control_options pbct
2790                                 WHERE pbct.project_id = apd.project_id
2791                                   AND pbct.BDGT_CNTRL_FLAG = 'Y'
2792                                   AND (pbct.EXTERNAL_BUDGET_CODE = 'GL' OR
2793                                        pbct.EXTERNAL_BUDGET_CODE is NULL
2794                                       )
2795                                   AND pbct.BUDGET_TYPE_CODE = bdgttype.budget_type_code
2796                                   AND bdgttype.budget_amount_code = 'C'
2797                               )
2798 		/*Bug 13602288 :Added the below union for the self assessed tax changes,If there exists pre-payments  we need to  handle for SAT also*/
2799 		UNION
2800 			   SELECT  apd.invoice_distribution_id,
2801 	           DECODE(apd.line_type_lookup_code
2802 		          ,'NONREC_TAX',DECODE(apd.prepay_distribution_id,NULL,decode(apd.line_type_lookup_code,'NONREC_TAX','SELF_ASSESSED_TAX',apd.line_type_lookup_code),'PREPAY')
2803 			  ,decode(apd.line_type_lookup_code,'NONREC_TAX','SELF_ASSESSED_TAX',apd.line_type_lookup_code))
2804              FROM  ap_self_assessed_tax_dist_all apd ,
2805 	           psa_bc_xla_events_gt xlaevt,
2806 		   ap_invoices_all apinv
2807             WHERE  apd.bc_event_id = xlaevt.event_id
2808 	      AND  apd.project_id IS NOT NULL
2809 	      AND  NVL(apd.pa_addition_flag, 'X' ) <> 'T'
2810 	      AND  apinv.invoice_id = apd.invoice_id
2811 	      AND  apinv.invoice_type_lookup_code <> 'EXPENSE REPORT'
2812 	      AND  apd.line_type_lookup_code <> 'REC_TAX'
2813 	      	      AND  ((apinv.invoice_type_lookup_code = 'PREPAYMENT'
2814 	             AND apd.po_distribution_id IS NULL )
2815 		     OR apinv.invoice_type_lookup_code <> 'PREPAYMENT')
2816 	      AND  NOT EXISTS
2817 	           ( SELECT 1
2818 		       FROM dual
2819 		      WHERE apd.line_type_lookup_code IN ('PREPAY','NONREC_TAX')
2820 			AND apd.prepay_distribution_id IS NOT NULL
2821 			AND apd.po_distribution_id IS NOT NULL)
2822               AND NOT EXISTS ( SELECT 1
2823 	                         FROM po_distributions_all pod
2824 				WHERE pod.po_distribution_id = apd.po_distribution_id
2825 				  AND apd.po_distribution_id IS NOT NULL
2826 				  AND ((NVL(pod.accrue_on_receipt_flag,'N') = 'Y'
2827 				        AND apd.line_type_lookup_code IN ('ITEM','ACCRUAL','NONREC_TAX'))
2828 				        OR
2829                                         (pa_funds_control_utils2.is_CWK_PO
2830 				                (pod.po_header_id,pod.po_line_id,pod.po_distribution_id,pod.org_id) = 'Y'
2831 				         AND apd.line_type_lookup_code IN ('ITEM','NONREC_TAX')
2832                                          AND NVL(apd.amount_variance,0)= 0
2833  	     			         AND NVL(apd.base_amount_variance,0)=0
2834 				         AND NVL(apd.quantity_variance,0)=0
2835 				         AND NVL(apd.base_quantity_variance,0)=0
2836 					 )
2837 				       )
2838              		      )
2839               AND  EXISTS (SELECT  1
2840                                 FROM  pa_projects_all pp,
2841  				      pa_implementations_all imp
2842                 	       WHERE  pp.project_id = apd.project_id
2843 	        	         AND  pp.org_id  = imp.org_id
2844 			         AND  imp.set_of_books_id = apd.set_of_books_id )
2845               AND  EXISTS ( SELECT 'Project Bdgt Ctrl enabled'
2846                                  FROM pa_budget_types bdgttype
2847                                       ,pa_budgetary_control_options pbct
2848                                 WHERE pbct.project_id = apd.project_id
2849                                   AND pbct.BDGT_CNTRL_FLAG = 'Y'
2850                                   AND (pbct.EXTERNAL_BUDGET_CODE = 'GL' OR
2851                                        pbct.EXTERNAL_BUDGET_CODE is NULL
2852                                       )
2853                                   AND pbct.BUDGET_TYPE_CODE = bdgttype.budget_type_code
2854                                   AND bdgttype.budget_amount_code = 'C'
2855                               );
2856 
2857         -- R12 Funds Management Uptake : This is the main cursor to fetch records from ap extracts
2858 	-- for all eligible invoice distribution id's. This cursor fetches data for Standard Invoices
2859 	-- and prepayments. Note : For prepayments there will be multiple lines for each invoice
2860 	-- distribution as data is fetched from AP_PREPAY_APP_DISTS.
2861 	-- Note : This cursor fetches only the AP dist record ,it doesnt fetch associated
2862 	-- PO/RELEASE that has to be unreserved.
2863 
2864                    /* Bug 5203226 : AP's amount calculation logic for all type of invoices
2865 		      (except prepay application) for populating entered and accounted amounts
2866 
2867 		   distribution type  accounted_amt             entered_amt
2868 		   ----------------------------------------------------------------
2869 		   ERV                ENCUMBRANCE_BASE_AMOUNT    ENCUMBRANCE_AMOUNT
2870 		   TERV               AID_EXTRA_PO_ERV/          ENCUMBRANCE_AMOUNT
2871 		                      ENCUMBRANCE_BASE_AMOUNT
2872 		   ITEM               ENCUMBRANCE_BASE_AMOUNT    ENCUMBRANCE_AMOUNT
2873 		   IPV                ENCUMBRANCE_BASE_AMOUNT    ENCUMBRANCE_AMOUNT
2874 		   MISCELLANEOUS      ENCUMBRANCE_BASE_AMOUNT    ENCUMBRANCE_AMOUNT
2875 		   FREIGHT            ENCUMBRANCE_BASE_AMOUNT    ENCUMBRANCE_AMOUNT
2876 		   NONREC_TAX         ENCUMBRANCE_BASE_AMOUNT    ENCUMBRANCE_AMOUNT
2877 		   TIPV               ENCUMBRANCE_BASE_AMOUNT    ENCUMBRANCE_AMOUNT
2878 		   TRV                ENCUMBRANCE_BASE_AMOUNT    ENCUMBRANCE_AMOUNT
2879 		   FREIGHT            ENCUMBRANCE_BASE_AMOUNT    ENCUMBRANCE_AMOUNT
2880 		   Item/tax amt var   AID_BASE_AMOUNT_VARIANCE   AID_AMOUNT_VARIANCE
2881 		   Item/tax qty var   AID_BASE_QUANTITY_VARIANCE AID_QUANTITY_VARIANCE
2882                    backing PO         ENCUMBRANCE_BASE_AMOUNT    ENCUMBRANCE_AMOUNT
2883 
2884 		   Note:
2885 		   ENCUMBRANCE_BASE_AMOUNT = NVL(AID.base_amount,AID.amount) - NVL
2886 		   (AID.base_amount_variance,nvl(AID.amount_variance,0)) - NVL(AID.
2887 		   base_quantity_variance,NVL(AID.quantity_variance,0))
2888 
2889                    ENCUMBRANCE_AMOUNT =NVL(AID.amount,0) - NVL(AID.amount_variance,0) -
2890 		   NVL(AID.quantity_variance,0)
2891 		   *//*Added SAT_FLAG for Bug 13622390 in decode for NONREC_TAX for reference1,2,3,ap_po_dist_id,release_id*/
2892 
2893 	CURSOR cur_ap_bc_dist (p_stdinvoice_exists VARCHAR2,
2894 	                       p_prepay_exists     VARCHAR2) IS
2895 	    SELECT /*+ leading(PBXEG, APEXT.AID ) USE_NL(APEXT.AID) cardinality(PBXEG,1) */ NULL      budget_version_id,    --Added hint for bug 14752984
2896 	           NULL                                                        budget_line_id,
2897 	           NULL                                                        budget_ccid,
2898  		   apext.aid_project_id                                        project_id,
2899 		   apext.aid_task_id                                           task_id,
2900 		   apext.aid_expenditure_type                                  expenditure_type,
2901 		   apext.aid_expenditure_org_id                                expenditure_organization_id,
2902  		   -- The below hardcoded values for Je_source_name and Je_category_name columns
2903 		   -- are based on SLA seed data for PO and REQ. These values are later
2904 		   -- updated by pa_funds_check API to synch up with gl_bc_packets if different
2905                    'Payables'                                                  je_source_name,
2906    		   'Purchase Invoices'                                         je_category_name,
2907                    'AP'                                                        document_type,
2908                    apext.bus_flow_inv_id                                       document_header_id,
2909                    apext.aid_invoice_line_number                               document_line_id,
2910 		   apext.aid_invoice_dist_id                                   document_distribution_id,
2911 		   -- For standard invoice this is always same as document_distribution_id
2912 		   apext.aid_invoice_dist_id                                   invoice_distribution_id,
2913 		   'E'                                                         actual_flag,
2914 		   NULL                                                        result_code,
2915 		   'I'                                                         status_code,
2916 		   ENCUMBRANCE_AMOUNT                                          entered_amount,	--Bug 5203226/5498978
2917 		   ENCUMBRANCE_BASE_AMOUNT                                     accounted_amount, --Bug 5203226/5498978
2918                    'N'                                                         balance_posted_flag,
2919                    'T'                                                         funds_process_mode,
2920                    'N'                                                         burden_cost_flag,
2921                    --Below decode ensures that PO is relieved only when attached to ITEM/ACCRUAL line
2922 		   DECODE(apext.AID_LINE_TYPE_LOOKUP_CODE
2923 		          ,'ITEM',DECODE(apext.po_distribution_id,NULL,'AP','PO')
2924 		          ,'ACCRUAL',DECODE(apext.po_distribution_id,NULL,'AP','PO')
2925                           ,'NONREC_TAX',DECODE(apext.SELF_ASSESSED_TAX_FLAG,'Y',NULL,DECODE(apext.po_distribution_id,NULL,'AP','PO')) -- Bug 5523570
2926 		          ,NULL)                                               reference1,
2927 		   DECODE(apext.AID_LINE_TYPE_LOOKUP_CODE
2928 		          ,'ITEM', DECODE(apext.po_distribution_id
2929 			           ,NULL,apext.bus_flow_inv_id
2930 				   ,apext.bus_flow_po_doc_id)
2931 		          ,'ACCRUAL',DECODE(apext.po_distribution_id
2932 			                    ,NULL,apext.bus_flow_inv_id
2933 					    ,apext.bus_flow_po_doc_id)
2934                           ,'NONREC_TAX',DECODE(apext.SELF_ASSESSED_TAX_FLAG,'Y',NULL,DECODE(apext.po_distribution_id   -- Bug 5523570
2935 			                    ,NULL,apext.bus_flow_inv_id
2936 					    ,apext.bus_flow_po_doc_id))
2937 		          , NULL)                                              reference2,
2938 		   DECODE(apext.AID_LINE_TYPE_LOOKUP_CODE
2939 		          ,'ITEM', DECODE(apext.po_distribution_id
2940 			                  ,NULL,apext.aid_invoice_dist_id
2941 					  ,apext.po_distribution_id)
2942                           ,'ACCRUAL',DECODE(apext.po_distribution_id
2943 			                    ,NULL,apext.aid_invoice_dist_id
2944 					    ,apext.po_distribution_id)
2945                           ,'NONREC_TAX',DECODE(apext.SELF_ASSESSED_TAX_FLAG,'Y',NULL,DECODE(apext.po_distribution_id    -- Bug 5523570
2946 			                    ,NULL,apext.aid_invoice_dist_id
2947 					    ,apext.po_distribution_id))
2948                           , NULL)                                              reference3,
2949                    NULL                                                        bc_event_id,
2950 		   pa_funds_control_pkg.check_bdn_on_sep_item
2951 		                                 (apext.aid_project_id)        burden_method_code,
2952 		   NULL                                                        main_or_backing_code,
2953                    apext.event_id                                              source_event_id,
2954 		   apext.aid_dist_ccid                                         txn_ccid,
2955 		   NULL                                                        parent_bc_packet_id,
2956                    pa_funds_control_utils.get_fnd_reqd_flag
2957 			                      (apext.aid_project_id ,'STD')    fck_reqd_flag,
2958 		   apext.aid_quantity_variance                                 ap_quantity_variance,
2959 		   apext.aid_amount_variance                                   ap_amount_variance,
2960 		   apext.aid_base_quantity_variance                            ap_base_quantity_variance,
2961 		   apext.aid_base_amount_variance                              ap_base_amount_variance,
2962 		   /* Bug 5406564 : Below decode ensures that PO is relieved only when attached to ITEM/ACCRUAL/NONREC_TAX line
2963 		      and not for variance records. Added SAT_FLAG for Bug 13622390 */
2964 		   DECODE(apext.AID_LINE_TYPE_LOOKUP_CODE
2965 		          ,'ITEM',apext.po_distribution_id
2966 		          ,'ACCRUAL',apext.po_distribution_id
2967 			  ,'NONREC_TAX',DECODE(apext.SELF_ASSESSED_TAX_FLAG,'Y', NULL,apext.po_distribution_id)
2968 		          , NULL)                                              ap_po_distribution_id,
2969 		   apext.aid_accounting_date                                   gl_date,
2970             	   -- Bug 5238282 : Prepayment application will be treated as standard invoice line for check funds
2971 		   -- as there will be no data in ap_prepay_app_dists table.This table is populated during invoice
2972 		   -- validation.
2973 		   DECODE(p_bc_mode,'C'
2974 		                   ,DECODE(apext.AID_LINE_TYPE_LOOKUP_CODE
2975 				           ,'PREPAY','STANDARD'
2976 					   ,apext.AID_LINE_TYPE_LOOKUP_CODE)
2977                                    ,DECODE(apext.SELF_ASSESSED_TAX_FLAG, 'Y','SELF_ASSESSED_TAX', apext.AID_LINE_TYPE_LOOKUP_CODE))  distribution_type,
2978 		   DECODE(apext.AID_LINE_TYPE_LOOKUP_CODE
2979 		          ,'ITEM',DECODE(apext.bus_flow_po_entity_code
2980 			                 ,'RELEASE',apext.bus_flow_po_doc_id,NULL)
2981                           ,'ACCRUAL',DECODE(apext.bus_flow_po_entity_code
2982 			                 ,'RELEASE',apext.bus_flow_po_doc_id,NULL)
2983                           ,'NONREC_TAX',DECODE(apext.SELF_ASSESSED_TAX_FLAG,'Y',NULL,DECODE(apext.bus_flow_po_entity_code -- Bug 5523570
2984                                          ,'RELEASE',apext.bus_flow_po_doc_id,NULL))
2985                           , NULL)                                              release_id,
2986 		   -- R12 Funds Management Uptake: Currently this column is not available in AP extract
2987 		   -- hence added below logic to fetch encumbrance_type_id for invoices
2988 		   (SELECT encumbrance_type_id
2989 		      FROM gl_encumbrance_types
2990 		     WHERE encumbrance_type_KEY = 'Invoices')                  encumbrance_type_id,
2991                    --pa_utils2.get_gl_period_name(apd.accounting_date,apd.org_id)
2992                    apext.aid_period_name                                       period_name,
2993 		   apext.AID_PARENT_REVERSAL_ID                                parent_reversal_id -- Bug 5406690
2994 		/*	FROM  ap_extract_invoice_dtls_bc_v apext -- Bug 5500126 replaced the view with the below inline view for bug 14752984 */
2995              FROM  (    SELECT  AID.bc_event_id                           event_id
2996            ,AID.accounting_date                       aid_accounting_date
2997            ,AID.award_id                              aid_award_id
2998            ,AID.dist_code_combination_id              aid_dist_ccid
2999            ,AID.expenditure_type                      aid_expenditure_type
3000            ,AID.period_name                           aid_period_name
3001            ,AID.line_type_lookup_code                 aid_line_type_lookup_code
3002            ,AID.invoice_line_number                   aid_invoice_line_number
3003            ,AID.invoice_distribution_id               aid_invoice_dist_id
3004            ,AID.parent_reversal_id                    aid_parent_reversal_id
3005            ,AID.po_distribution_id                    po_distribution_id
3006            ,AID.invoice_id                            bus_flow_inv_id
3007            ,Decode (IGC.CC_LINE, null, 'PO_DISTRIBUTIONS_ALL', IGC.CC_DIST_TYPE)   Bus_Flow_PO_Dist_Type
3008            ,Decode (IGC.CC_LINE, null, Decode (pod.po_release_id, NULL, 'PURCHASE_ORDER', 'RELEASE') , IGC.CC_ENTITY_CODE)  Bus_Flow_PO_Entity_Code
3009            ,Decode (IGC.CC_LINE, null, Decode (pod.po_release_id, NULL, pod.po_header_id,pod.po_release_id)
3010                                       ,IGC.cc_doc_id)   Bus_Flow_PO_Doc_Id
3011            ,AID.project_id                              aid_project_id
3012            ,AID.task_id                                 aid_task_id
3013            ,AID.Expenditure_Organization_ID             aid_expenditure_org_id
3014            ,AID.quantity_variance                       aid_quantity_variance
3015            ,nvl(AID.base_quantity_variance, AID.quantity_variance)           aid_base_quantity_variance
3016            ,AID.amount_variance                                              aid_amount_variance
3017            ,nvl(AID.base_amount_variance, AID.amount_variance)               aid_base_amount_variance
3018 	         ,NVL(AID.base_amount,AID.amount)
3019              - NVL(AID.base_amount_variance,nvl(AID.amount_variance,0))
3020              - NVL(AID.base_quantity_variance,NVL(AID.quantity_variance,0))  encumbrance_base_amount
3021            ,NVL(AID.amount,0)  - NVL(AID.amount_variance,0)
3022                 - NVL(AID.quantity_variance,0)                               encumbrance_amount
3023            ,AID.CHARGE_APPLICABLE_TO_DIST_ID                                 charge_applicable_to_dist_id
3024 	   ,'N'   self_assessed_tax_flag
3025      FROM   AP_INVOICE_DISTRIBUTIONS_ALL AID,
3026             PO_DISTRIBUTIONS_ALL POD,
3027             FINANCIALS_SYSTEM_PARAMS_ALL FSP,
3028 	          IGC_CC_SLA_BFLOW_PRIOR_V IGC,
3029 	          ap_system_parameters_all ASP
3030      WHERE  nvl(AID.posted_flag,'N') <> 'Y'
3031      AND    AID.po_distribution_id = POD.po_distribution_id(+)
3032      AND    AID.po_distribution_id = IGC.po_distribution_id(+)
3033      AND    AID.ORG_ID = FSP.ORG_ID
3034      AND    aid.set_of_books_id = asp.set_of_books_id
3035      AND    aid.org_id = asp.org_id
3036      UNION
3037      SELECT  AID.bc_event_id                           event_id
3038            ,AID.accounting_date                       aid_accounting_date
3039            ,AID.award_id                              aid_award_id
3040            ,AID.dist_code_combination_id              aid_dist_ccid
3041            ,AID.expenditure_type                      aid_expenditure_type
3042            ,AID.period_name                           aid_period_name
3043            ,AID.line_type_lookup_code                 aid_line_type_lookup_code
3044            ,AID.invoice_line_number                   aid_invoice_line_number
3045            ,AID.invoice_distribution_id               aid_invoice_dist_id
3046            ,AID.parent_reversal_id                    aid_parent_reversal_id
3047            ,AID.po_distribution_id                    po_distribution_id
3048            ,AID.invoice_id                            bus_flow_inv_id
3049            ,Decode (IGC.CC_LINE, null, 'PO_DISTRIBUTIONS_ALL', IGC.CC_DIST_TYPE)   Bus_Flow_PO_Dist_Type
3050            ,Decode (IGC.CC_LINE, null, Decode (pod.po_release_id, NULL, 'PURCHASE_ORDER', 'RELEASE') , IGC.CC_ENTITY_CODE)  Bus_Flow_PO_Entity_Code
3051            ,Decode (IGC.CC_LINE, null, Decode (pod.po_release_id, NULL, pod.po_header_id,pod.po_release_id)
3052                                       ,IGC.cc_doc_id)   Bus_Flow_PO_Doc_Id
3053            ,AID.project_id                              aid_project_id
3054            ,AID.task_id                                 aid_task_id
3055            ,AID.Expenditure_Organization_ID             aid_expenditure_org_id
3056            ,AID.quantity_variance                       aid_quantity_variance
3057            ,nvl(AID.base_quantity_variance, AID.quantity_variance)           aid_base_quantity_variance
3058            ,AID.amount_variance                                              aid_amount_variance
3059            ,nvl(AID.base_amount_variance, AID.amount_variance)               aid_base_amount_variance
3060 	         ,NVL(AID.base_amount,AID.amount)
3061              - NVL(AID.base_amount_variance,nvl(AID.amount_variance,0))
3062              - NVL(AID.base_quantity_variance,NVL(AID.quantity_variance,0))  encumbrance_base_amount
3063            ,NVL(AID.amount,0)  - NVL(AID.amount_variance,0)
3064                 - NVL(AID.quantity_variance,0)                               encumbrance_amount
3065            ,AID.CHARGE_APPLICABLE_TO_DIST_ID                                 charge_applicable_to_dist_id
3066 	   ,AID.self_assessed_flag  self_assessed_tax_flag
3067      FROM   AP_SELF_ASSESSED_TAX_DIST_ALL AID,
3068             PO_DISTRIBUTIONS_ALL POD,
3069             FINANCIALS_SYSTEM_PARAMS_ALL FSP,
3070 	          IGC_CC_SLA_BFLOW_PRIOR_V IGC,
3071 	          ap_system_parameters_all ASP
3072      WHERE  nvl(AID.posted_flag,'N') <> 'Y'
3073      AND    AID.po_distribution_id = POD.po_distribution_id(+)
3074      AND    AID.po_distribution_id = IGC.po_distribution_id(+)
3075      AND    AID.ORG_ID = FSP.ORG_ID
3076      AND    aid.set_of_books_id = asp.set_of_books_id
3077      AND    aid.org_id = asp.org_id
3078      		    )  apext ,
3079 PSA_BC_XLA_EVENTS_GT PBXEG -- Added for bug 14752984
3080             WHERE  apext.aid_invoice_dist_id IN (select Column_Value from Table(g_ap_inv_dist_id))
3081 	      /*AND  apext.event_id in ( SELECT event_id FROM psa_bc_xla_events_gt) removed the existing inline view and added below join*/
3082 	      AND   apext.event_id = PBXEG.EVENT_ID  -- Added for bug 14752984
3083   	      AND  (p_bc_mode ='C'
3084 	            OR (apext.aid_line_type_lookup_code <> 'PREPAY' AND p_bc_mode <>'C')) -- Bug 5238282
3085               AND  NOT EXISTS ( --Bug 5490378 : Filter out Tax associated with prepay lines for reserve action
3086 	            SELECT 1
3087 		      FROM ap_invoice_distributions_all apd1
3088 		     WHERE apd1.invoice_distribution_id = apext.charge_applicable_to_dist_id
3089 		       AND apext.aid_line_type_lookup_code = 'NONREC_TAX'
3090 		       AND apext.charge_applicable_to_dist_id IS NOT NULL
3091 		       AND p_bc_mode <> 'C'
3092 		       AND apd1.line_type_lookup_code = 'PREPAY')
3093 	      AND  NOT EXISTS (  /*Bug 13602288 :Self Assessed Tax Changes*/
3094 	            SELECT 1
3095 		      FROM ap_self_assessed_tax_dist_all apd2
3096 		     WHERE apd2.invoice_distribution_id = apext.charge_applicable_to_dist_id
3097 		       AND apext.aid_line_type_lookup_code = 'NONREC_TAX'
3098 		       AND apext.charge_applicable_to_dist_id IS NOT NULL
3099 		       AND p_bc_mode <> 'C'
3100 		       AND apd2.line_type_lookup_code = 'PREPAY')
3101 	      AND  p_stdinvoice_exists = 'Y'
3102             UNION ALL
3103             SELECT  /*+ leading(PSA_BC_XLA_EVENTS_GT)  cardinality(PSA_BC_XLA_EVENTS_GT,1) */ NULL     budget_version_id,    -- Added for bug 14752984
3104 	           NULL                                                        budget_line_id,
3105 	           NULL                                                        budget_ccid,
3106                    AID.project_id                                              project_id,
3107                    AID.task_id                                                 task_id,
3108                    AID.expenditure_type                                        expenditure_type,
3109                    AID.expenditure_organization_id                             expenditure_organization_id,
3110  		   -- The below hardcoded values for Je_source_name and Je_category_name columns
3111 		   -- are based on SLA seed data for PO and REQ. These values are later
3112 		   -- updated by pa_funds_check API to synch up with gl_bc_packets if different
3113                    'Payables'                                                  je_source_name,
3114 	           'Purchase Invoices'                                         je_category_name,
3115                    'AP'                                                        document_type,
3116                    AID.invoice_id                                              document_header_id,
3117                    AID.INVOICE_LINE_NUMBER                                     document_line_id,
3118                    APAD.Prepay_App_Distribution_ID                             document_distribution_id,
3119                    AID.invoice_distribution_id                                 invoice_distribution_id,
3120 	       	   'E'                                                         actual_flag,
3121 		   NULL                                                        result_code,
3122 		   'I'                                                         status_code,
3123                    APAD.AMOUNT                                                  entered_amount,
3124                    nvl(APAD.Base_amount, APAD.amount)                          accounted_amount,
3125                    'N'                                                         balance_posted_flag,
3126                    'T'                                                         funds_process_mode,
3127                    'N'                                                         burden_cost_flag,
3128                    -- For prepayment application reference columns will refer prepayment dist which
3129                    -- needs to be reversed.There wont be any fundscheck for prepayments matched to PO
3130                    'AP'                                                        reference1,
3131                    APPH.PREPAY_INVOICE_ID                                      reference2,
3132                    AID.PREPAY_DISTRIBUTION_ID                                  reference3,
3133                    NULL                                                        bc_event_id,
3134  		   pa_funds_control_pkg.check_bdn_on_sep_item
3135 		                             (AID.project_id)                  burden_method_code,
3136 		   NULL                                                        main_or_backing_code,
3137                    APPH.bc_event_id                                            source_event_id,
3138                    AID.Dist_code_combination_id                                txn_ccid,
3139 		   NULL                                                        parent_bc_packet_id,
3140                    pa_funds_control_utils.get_fnd_reqd_flag(AID.project_id
3141 			                                 ,'STD')               fck_reqd_flag,
3142        	           NULL                                                        ap_quantity_variance,
3143 		   NULL                                                        ap_amount_variance,
3144 		   NULL                                                        ap_base_quantity_variance,
3145 		   NULL                                                        ap_base_amount_variance,
3146                    AID.po_distribution_id                                      ap_po_distribution_id,
3147                    AID.ACCOUNTING_DATE                                         gl_date,
3148                    APAD.prepay_dist_lookup_code                                distribution_type,
3149                    AIL.po_release_id                                           release_id,
3150                    -- R12 Funds Management Uptake: Currently this column
3151                    -- is not available in AP extract
3152                    -- hence added below logic to fetch encumbrance_type_id for invoices
3153                    (SELECT encumbrance_type_id
3154                       FROM gl_encumbrance_types
3155                      WHERE encumbrance_type_KEY = 'Invoices')                  encumbrance_type_id,
3156                    AID.PERIOD_NAME                                             period_name,
3157 		   AID.parent_reversal_id                                      parent_reversal_id -- Bug 5406690
3158               FROM AP_PREPAY_HISTORY_ALL APPH,
3159 	      PSA_BC_XLA_EVENTS_GT PBXEG, --Added for bug 14752984
3160                    AP_PREPAY_APP_DISTS APAD,
3161                    AP_INVOICE_LINES_ALL AIL,
3162                    AP_INVOICE_DISTRIBUTIONS_ALL AID
3163              WHERE AID.bc_event_id = APPH.bc_Event_id
3164                AND APPH.prepay_history_id = APAD.prepay_history_id
3165                AND AID.invoice_line_number = AIL.line_number
3166                AND AID.invoice_id = AIL.invoice_id
3167                AND AID.line_type_lookup_code IN ( 'PREPAY' ,'NONREC_TAX' ) --Bug 5490378
3168                /* and APPH.bc_Event_id IN ( SELECT event_id FROM psa_bc_xla_events_gt)removed the inline view for bug 14752984 and added below join */
3169                and AID.invoice_distribution_id IN (select Column_Value from Table(g_ap_inv_dist_id))
3170 			   AND APPH.bc_Event_id = PBXEG.event_id --Added for bug 14752984
3171                AND p_prepay_exists = 'Y'
3172                and aid.invoice_distribution_id = apad.prepay_app_distribution_id
3173 	       /*Bug 13602288 :Self Assessed Tax Changes - We need to have SAT data to apply funds check*/
3174 	       UNION ALL
3175             SELECT NULL                                                        budget_version_id,
3176 	           NULL                                                        budget_line_id,
3177 	           NULL                                                        budget_ccid,
3178                    AID.project_id                                              project_id,
3179                    AID.task_id                                                 task_id,
3180                    AID.expenditure_type                                        expenditure_type,
3181                    AID.expenditure_organization_id                             expenditure_organization_id,
3182                    'Payables'                                                  je_source_name,
3183 	           'Purchase Invoices'                                         je_category_name,
3184                    'AP'                                                        document_type,
3185                    AID.invoice_id                                              document_header_id,
3186                    AID.INVOICE_LINE_NUMBER                                     document_line_id,
3187                    APAD.Prepay_App_Distribution_ID                             document_distribution_id,
3188                    AID.invoice_distribution_id                                 invoice_distribution_id,
3189 	       	   'E'                                                         actual_flag,
3190 		   NULL                                                        result_code,
3191 		   'I'                                                         status_code,
3192                    APAD.AMOUNT                                                  entered_amount,
3193                    nvl(APAD.Base_amount, APAD.amount)                          accounted_amount,
3194                    'N'                                                         balance_posted_flag,
3195                    'T'                                                         funds_process_mode,
3196                    'N'                                                         burden_cost_flag,
3197                    'AP'                                                        reference1,
3198                    APPH.PREPAY_INVOICE_ID                                      reference2,
3199                    AID.PREPAY_DISTRIBUTION_ID                                  reference3,
3200                    NULL                                                        bc_event_id,
3201  		   pa_funds_control_pkg.check_bdn_on_sep_item
3202 		                             (AID.project_id)                  burden_method_code,
3203 		   NULL                                                        main_or_backing_code,
3204                    APPH.bc_event_id                                            source_event_id,
3205                    AID.Dist_code_combination_id                                txn_ccid,
3206 		   NULL                                                        parent_bc_packet_id,
3207                    pa_funds_control_utils.get_fnd_reqd_flag(AID.project_id
3208 			                                 ,'STD')               fck_reqd_flag,
3209        	           NULL                                                        ap_quantity_variance,
3210 		   NULL                                                        ap_amount_variance,
3211 		   NULL                                                        ap_base_quantity_variance,
3212 		   NULL                                                        ap_base_amount_variance,
3213                    AID.po_distribution_id                                      ap_po_distribution_id,
3214                    AID.ACCOUNTING_DATE                                         gl_date,
3215                    APAD.prepay_dist_lookup_code                                distribution_type,
3216                    AIL.po_release_id                                           release_id,
3217                    (SELECT encumbrance_type_id
3218                       FROM gl_encumbrance_types
3219                      WHERE encumbrance_type_KEY = 'Invoices')                  encumbrance_type_id,
3220                    AID.PERIOD_NAME                                             period_name,
3221 		   AID.parent_reversal_id                                      parent_reversal_id
3222               FROM AP_PREPAY_HISTORY_ALL APPH,
3223                    AP_PREPAY_APP_DISTS APAD,
3224                    AP_INVOICE_LINES_ALL AIL,
3225                    AP_SELF_ASSESSED_TAX_DIST_ALL AID
3226              WHERE AID.bc_event_id = APPH.bc_Event_id
3227                AND APPH.prepay_history_id = APAD.prepay_history_id
3228                AND AID.invoice_line_number = AIL.line_number
3229                AND AID.invoice_id = AIL.invoice_id
3230                AND AID.line_type_lookup_code IN ( 'PREPAY' ,'NONREC_TAX' )
3231                and APPH.bc_Event_id IN ( SELECT event_id FROM psa_bc_xla_events_gt)
3232                and AID.invoice_distribution_id IN (select Column_Value from Table(g_ap_inv_dist_id))
3233                AND p_prepay_exists = 'Y'
3234                and aid.invoice_distribution_id = apad.prepay_app_distribution_id;
3235 
3236         -- R12 Funds Management Uptake : This cursor fetches additional required information
3237 	-- which is not provided by AP extracts ap_invoice_extract_details_v/
3238 	-- AP_PREPAYAPP_EXTRACT_DETAILS_V.
3239 
3240         CURSOR c_ap_details (p_inv_dist_id NUMBER) IS
3241         SELECT apd.set_of_books_id,
3242 	       apd.expenditure_item_date,
3243 	       apd.org_id,
3244 	       apinv.vendor_id
3245           FROM ap_invoice_distributions_all apd,
3246 	       ap_invoices_all apinv
3247          WHERE apd.invoice_distribution_id = p_inv_dist_id
3248 	   AND apinv.invoice_id = apd.invoice_id
3249 	   UNION /*Bug 13602288 :Self Assessed Tax Changes*/
3250 	   SELECT apd.set_of_books_id,
3251 	       apd.expenditure_item_date,
3252 	       apd.org_id,
3253 	       apinv.vendor_id
3254           FROM ap_self_assessed_tax_dist_all apd,
3255 	       ap_invoices_all apinv
3256          WHERE apd.invoice_distribution_id = p_inv_dist_id
3257 	   AND apinv.invoice_id = apd.invoice_id;
3258 
3259         --R12 Funds Management Uptake:Deleted CBC related logic
3260 
3261         -- Cursor to fetch Vendor and organization details for REQ line
3262     	CURSOR cur_req_vend_org_details (p_req_line_id  IN NUMBER )IS
3263 	SELECT  pov.vendor_id,
3264 	        porl.org_id
3265           FROM  po_vendors pov,
3266 	        po_requisition_lines_all porl
3267          WHERE  pov.vendor_name (+) = porl.suggested_vendor_name
3268 	   AND  porl.REQUISITION_LINE_ID =p_req_line_id;
3269 
3270         -- Cursor to fetch Vendor and organization details for PO line
3271 	CURSOR cur_po_vend_org_details (p_header_id  NUMBER )IS
3272 	SELECT  poh.vendor_id,
3273 	        poh.org_id
3274           FROM  po_headers_all poh
3275          WHERE  poh.po_header_id = p_header_id;
3276 
3277 
3278 	CURSOR c_count_success_recs(p_packet_id NUMBER) IS
3279 	SELECT count(*)
3280 	  FROM pa_bc_packets
3281 	 WHERE packet_id = p_packet_id
3282 	   AND (NVL(status_code,'I') NOT IN ('F','T','R')
3283 		AND SUBSTR (NVL(result_code,'P'),1,1) <> 'F');
3284 
3285         -- Bug 5403775: Cursor to fetch main document details  associated with backing document
3286     	CURSOR cur_po_main_doc_details (p_req_event_id IN NUMBER,
3287 	                                p_req_origin_seq_num IN NUMBER )IS
3288 	SELECT  DECODE(ORIG.distribution_type ,'SCHEDULED','REL'
3289                                               ,'BLANKET','REL'
3290                                               ,'PO'),
3291 	        DECODE(ORIG.distribution_type ,'SCHEDULED',ORIG.po_release_id
3292                                               ,'BLANKET',ORIG.po_release_id
3293                                               ,ORIG.header_id)
3294           FROM  PO_BC_DISTRIBUTIONS ORIG
3295          WHERE  ORIG.sequence_number= p_req_origin_seq_num
3296 	   AND  ORIG.ae_event_id = p_req_event_id;
3297 
3298        l_index                     NUMBER;
3299        l_count_success_rec         NUMBER;
3300 
3301        l_old_req_line_id           PO_REQUISITION_LINES_ALL.REQUISITION_LINE_ID%TYPE;
3302        l_old_po_header_id          PO_HEADERS_ALL.po_HEADER_ID%TYPE;
3303        l_req_vendor_id             PO_HEADERS_ALL.VENDOR_ID%TYPE;
3304        l_req_org_id                PO_HEADERS_ALL.ORG_ID%TYPE;
3305        l_Po_vendor_id              PO_HEADERS_ALL.VENDOR_ID%TYPE;
3306        l_po_org_id                 PO_HEADERS_ALL.ORG_ID%TYPE;
3307        l_packet_id                 pa_bc_packets.packet_id%TYPE;
3308        l_prepay_exists             VARCHAR2(1);
3309        l_stdinvoice_exists         VARCHAR2(1);
3310 
3311 BEGIN
3312 
3313         --- Initialize the error statck
3314         PA_DEBUG.init_err_stack ('PA_FUNDS_CONTROL_PKG1.CREATE_PROJ_ENCUMBRANCE_EVENTS');
3315 
3316         fnd_profile.get('PA_DEBUG_MODE',PA_FUNDS_CONTROL_PKG.g_debug_mode);
3317         PA_FUNDS_CONTROL_PKG.g_debug_mode := NVL(PA_FUNDS_CONTROL_PKG.g_debug_mode, 'N');
3318 
3319         PA_DEBUG.SET_PROCESS( x_process        => 'PLSQL'
3320                              ,x_write_file     => 'LOG'
3321                              ,x_debug_mode     => PA_FUNDS_CONTROL_PKG.g_debug_mode);
3322 
3323 
3324 	-- Bug 5354715 : Added the "PA IMPLEMENTED IN OU" check.
3325         IF PA_FUNDS_CONTROL_PKG.IS_PA_INSTALL_IN_OU = 'N' then
3326                 x_return_code := 'S';
3327 
3328 		If pa_funds_control_pkg.g_debug_mode = 'Y' then
3329 			pa_funds_control_pkg.log_message(p_msg_token1=>'PA NOT INSTALLED IN THIS OU.return code='
3330 				||x_return_code);
3331 		end if;
3332                 PA_DEBUG.Reset_err_stack;
3333 		Return;
3334         END IF;
3335 
3336     -- Bug#6645995 Made this code applicable only for the applications
3337     -- Purchasing / Payables/ Contract Commitments
3338     -- as we do funds check for only transactions coming from the above applications.
3339 
3340     -- Moved this initialization before the if statement to ensure that any call made
3341     -- from other than Purchasing / Payables/ Contract Commitments
3342     -- will get 'S'uccess status code.
3343 
3344     x_return_code := 'S';
3345 
3346     IF p_application_id in (200, 201, 8407) THEN
3347 
3348 	IF pa_budget_fund_pkg.g_processing_mode IN ('YEAR_END','CHECK_FUNDS','BASELINE') THEN
3349            RETURN;
3350         END IF;
3351 
3352 	IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3353  	  pa_funds_control_pkg.log_message(p_msg_token1 => 'CREATE_PROJ_ENCUMBRANCE_EVENTS : In start ');
3354  	  pa_funds_control_pkg.log_message(p_msg_token1 => 'CREATE_PROJ_ENCUMBRANCE_EVENTS : p_application_id = '||p_application_id);
3355  	  pa_funds_control_pkg.log_message(p_msg_token1 => 'CREATE_PROJ_ENCUMBRANCE_EVENTS : p_partial_flag = '||p_partial_flag);
3356  	  pa_funds_control_pkg.log_message(p_msg_token1 => 'Calling init_plsql_tabs to initialize the pl/sql tabs ');
3357 	End if;
3358 
3359 	-- Initialize the pl/sql table which stores pa_bc_packets records
3360 	init_plsql_tabs;
3361 
3362 	IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3363  	  pa_funds_control_pkg.log_message(p_msg_token1 => 'Calling PA_FUNDS_CONTROL_UTILS.init_util_variables');
3364 	End if;
3365 
3366 	--Initialize the funds control util package global variables
3367 	PA_FUNDS_CONTROL_UTILS.init_util_variables;
3368 
3369 	IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3370  	  pa_funds_control_pkg.log_message(p_msg_token1 => 'Calling pa_multi_currency.init');
3371 	End if;
3372 	-- initialize the accounting currency code,
3373         pa_multi_currency.init;
3374 
3375 
3376 	IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3377  	  pa_funds_control_pkg.log_message(p_msg_token1 => 'Calling pa_multi_currency.g_accounting_currency_code');
3378 	End if;
3379        --Get the accounting currency into a global variable.
3380         g_acct_currency_code := pa_multi_currency.g_accounting_currency_code;
3381 
3382         -------->6599207 ------As part of CC Enhancements
3383 	-- The following call is to create bcpackets for GL budget.
3384 
3385 	IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3386  	  pa_funds_control_pkg.log_message(p_msg_token1 => 'Calling populate_plsql_tabs_CBC');
3387 	End if;
3388 
3389 	IF p_application_id = 8407 THEN --Calling application is Contract Commitments
3390 	   populate_plsql_tabs_CBC(NULL,'GL','','',p_bc_mode);
3391 	END IF;
3392 
3393 	IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3394  	  pa_funds_control_pkg.log_message(p_msg_token1 => 'After Calling populate_plsql_tabs_CBC');
3395 	End if;
3396 	-------->6599207 ------END
3397 
3398 	IF p_application_id = 200 THEN --Calling application is Payables
3399 
3400            	IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3401  	           pa_funds_control_pkg.log_message(p_msg_token1 => 'Fetching eligible distributions from ap_invoice_distributions_all');
3402          	End if;
3403 
3404                 OPEN  c_proj_ap_dist;
3405 		FETCH c_proj_ap_dist BULK COLLECT INTO  g_ap_inv_dist_id,g_ap_line_type_lkup;
3406                 CLOSE c_proj_ap_dist;
3407 
3408            	IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3409  	           pa_funds_control_pkg.log_message(p_msg_token1 => 'Number of AP distribuions fetched ='||g_ap_inv_dist_id.count);
3410          	End if;
3411 
3412 		IF g_ap_inv_dist_id.count <> 0 THEN
3413 
3414 		   l_prepay_exists := 'N';
3415                    l_stdinvoice_exists := 'N';
3416 
3417                    FOR i in 1..g_ap_line_type_lkup.count LOOP
3418 		     -- Bug 5238282 : Prepayment application will be treated as standard invoice line for check funds
3419 		     -- as there will be no data in ap_prepay_app_dists table.This table is populated during invoice
3420 		     -- validation.
3421 		     IF g_ap_line_type_lkup(i) = 'PREPAY' AND p_bc_mode <> 'C' THEN
3422 		        l_prepay_exists := 'Y';
3423                      ELSE
3424 		        l_stdinvoice_exists := 'Y';
3425 		     END IF;
3426                      -- Exit the loop if both prepay and standard invoices exists.
3427 		     IF l_prepay_exists= 'Y' AND l_stdinvoice_exists = 'Y' THEN
3428 		        EXIT;
3429                      END IF;
3430 
3431 		   END LOOP;
3432 
3433                    IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3434    	              pa_funds_control_pkg.log_message(p_msg_token1 => 'For current run there exists PREPAY distribution ? '||l_prepay_exists);
3435  	              pa_funds_control_pkg.log_message(p_msg_token1 => 'For current run there exists Std Invoice distribution ? '||l_stdinvoice_exists);
3436                    End if;
3437 
3438            	   IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3439  	              pa_funds_control_pkg.log_message(p_msg_token1 => 'Fetching required data from AP extract into plsql tables');
3440          	   End if;
3441 
3442 		   OPEN cur_ap_bc_dist(l_stdinvoice_exists,l_prepay_exists);
3443 		   FETCH cur_ap_bc_dist BULK COLLECT INTO
3444 					g_tab_budget_version_id,
3445 					g_tab_budget_line_id,
3446 					g_tab_budget_ccid,
3447 					g_tab_project_id,
3448 					g_tab_task_id,
3449 					g_tab_exp_type,
3450 					g_tab_exp_org_id,
3451 					g_tab_je_source_name,
3452 					g_tab_je_category_name,
3453 					g_tab_doc_type,
3454 					g_tab_doc_header_id,
3455 					g_tab_doc_line_id,
3456 					g_tab_doc_distribution_id,
3457 					g_tab_inv_distribution_id,
3458 					g_tab_actual_flag,
3459 					g_tab_result_code,
3460 					g_tab_status_code,
3461                                      	g_tab_entered_amount,
3462                 	                g_tab_accted_amount,
3463 					g_tab_balance_posted_flag,
3464 					g_tab_funds_process_mode,
3465 					g_tab_burden_cost_flag,
3466 					g_tab_pkt_reference1,
3467 					g_tab_pkt_reference2,
3468 					g_tab_pkt_reference3,
3469 					g_tab_event_id,
3470 					g_tab_burden_method_code,
3471 					g_tab_main_or_backing_code,
3472 					g_tab_source_event_id,
3473 					g_tab_trxn_ccid,
3474 					g_tab_p_bc_packet_id,
3475 					g_tab_fck_reqd_flag,
3476 					g_tab_ap_quantity_variance,
3477 					g_tab_ap_amount_variance,
3478 					g_tab_ap_base_qty_variance,
3479 					g_tab_ap_base_amount_variance,
3480 					g_tab_ap_po_distribution_id,
3481                                         g_tab_gl_date,
3482 					g_tab_distribution_type,
3483                                         g_tab_po_release_id,
3484 					g_tab_enc_type_id,
3485                                         g_tab_period_name,
3486 					g_tab_parent_reversal_id; -- Bug 5406690
3487 					/***** Any additional columns added here should also be added in COPY_AP_RECORD procedure
3488 					       as COPY_AP_RECOR proc creates new records in plsql tables for backing PO and
3489 					       variance records and if any new plsql variables are not initialized for these new
3490 					       records the code will raise exception *****/
3491                    CLOSE cur_ap_bc_dist ;
3492 
3493                    -- Bug 5406690
3494                    FOR i IN 1..g_tab_doc_header_id.count LOOP
3495 		      select pa_bc_packets_s.nextval
3496 		      into g_tab_bc_packet_id(i)
3497 		      from dual;
3498                    END LOOP;
3499 
3500            	   IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3501  	              pa_funds_control_pkg.log_message(p_msg_token1 => 'Number of records fetched from AP extract into plsql tables'||g_tab_doc_header_id.count);
3502          	   End if;
3503 
3504                    -- AP extract returns one record for an AP distribution with PO/quantity variance and amount
3505 	  	   -- variance columns populated. Hence AP line with this information populated should be split into
3506 		   -- multiple bc packet records.
3507 
3508                    IF pa_funds_control_pkg.g_debug_mode = 'Y' AND g_tab_doc_header_id.count<>0  THEN
3509 
3510   		      FOR l_index IN 1..g_tab_doc_header_id.last LOOP
3511 
3512  	                pa_funds_control_pkg.log_message(p_msg_token1 => '***Start of record-'||l_index||' fetched from AP extract***');
3513  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_budget_version_id = '||g_tab_budget_version_id(l_index));
3514  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_budget_line_id = '||g_tab_budget_line_id(l_index));
3515  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_budget_ccid = '||g_tab_budget_ccid(l_index));
3516  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_project_id = '||g_tab_project_id(l_index));
3517  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_task_id = '||g_tab_task_id(l_index));
3518  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_exp_type = '||g_tab_exp_type(l_index));
3519  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_exp_org_id = '||g_tab_exp_org_id(l_index));
3520  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_je_source_name = '||g_tab_je_source_name(l_index));
3521  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_je_category_name = '||g_tab_je_category_name(l_index));
3522  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_doc_type = '||g_tab_doc_type(l_index));
3523  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_doc_header_id = '||g_tab_doc_header_id(l_index));
3524  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_doc_line_id = '||g_tab_doc_line_id(l_index));
3525  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_doc_distribution_id = '||g_tab_doc_distribution_id(l_index));
3526  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_inv_distribution_id = '||g_tab_inv_distribution_id(l_index));
3527  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_actual_flag = '||g_tab_actual_flag(l_index));
3528  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_result_code = '||g_tab_result_code(l_index));
3529  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_status_code = '||g_tab_status_code(l_index));
3530  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_entered_amount = '||g_tab_entered_amount(l_index));
3531  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_accted_amount = '||g_tab_accted_amount(l_index));
3532  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_balance_posted_flag = '||g_tab_balance_posted_flag(l_index));
3533  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_funds_process_mode = '||g_tab_funds_process_mode(l_index));
3534  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_burden_cost_flag = '||g_tab_burden_cost_flag(l_index));
3535  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_pkt_reference1 = '||g_tab_pkt_reference1(l_index));
3536  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_pkt_reference2 = '||g_tab_pkt_reference2(l_index));
3537  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_pkt_reference3 = '||g_tab_pkt_reference3(l_index));
3538  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_event_id = '||g_tab_event_id(l_index));
3539  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_burden_method_code = '||g_tab_burden_method_code(l_index));
3540  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_main_or_backing_code = '||g_tab_main_or_backing_code(l_index));
3541  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_source_event_id = '||g_tab_source_event_id(l_index));
3542  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_trxn_ccid = '||g_tab_trxn_ccid(l_index));
3543  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_p_bc_packet_id = '||g_tab_p_bc_packet_id(l_index));
3544  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_fck_reqd_flag = '||g_tab_fck_reqd_flag(l_index));
3545  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_ap_quantity_variance = '||g_tab_ap_quantity_variance(l_index));
3546  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_ap_amount_variance = '||g_tab_ap_amount_variance(l_index));
3547  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_ap_base_qty_variance = '||g_tab_ap_base_qty_variance(l_index));
3548  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_ap_base_amount_variance = '||g_tab_ap_base_amount_variance(l_index));
3549  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_ap_po_distribution_id = '||g_tab_ap_po_distribution_id(l_index));
3550  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_gl_date = '||g_tab_gl_date(l_index));
3551  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_distribution_type = '||g_tab_distribution_type(l_index));
3552  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_po_release_id = '||g_tab_po_release_id(l_index));
3553  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_enc_type_id = '||g_tab_enc_type_id(l_index));
3554  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_period_name = '||g_tab_period_name(l_index));
3555                         pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_bc_packet_id = '||g_tab_bc_packet_id(l_index));
3556 			pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_parent_reversal_id = '||g_tab_parent_reversal_id(l_index));
3557  	                pa_funds_control_pkg.log_message(p_msg_token1 => '****End of record-'||l_index||' fetched from AP extract***');
3558 
3559   		      END LOOP;
3560 
3561         	    End if;
3562 
3563   		  IF g_tab_doc_header_id.count <> 0 THEN
3564 
3565                    FOR l_index IN 1..g_tab_doc_header_id.last LOOP
3566 
3567              	     IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3568  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Before fetching c_ap_details');
3569          	     End if;
3570 
3571 		     OPEN  c_ap_details (g_tab_inv_distribution_id(l_index));
3572 		     FETCH c_ap_details INTO g_tab_set_of_books_id(l_index),
3573 		                             g_tab_exp_item_date(l_index),
3574 					     g_tab_org_id(l_index),
3575 					     g_tab_vendor_id(l_index);
3576 		     CLOSE c_ap_details;
3577 
3578              	     IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3579  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'After fetching c_ap_details');
3580  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Deriving DR and CR column ');
3581          	     End if;
3582 
3583 		     IF NVL(g_tab_entered_amount(l_index),0) <0 THEN
3584 		        g_tab_entered_dr(l_index) := 0;
3585 		        g_tab_entered_cr(l_index) := ABS(NVL(g_tab_entered_amount(l_index),0));
3586                      ELSE
3587 		        g_tab_entered_cr(l_index) := 0;
3588 		        g_tab_entered_dr(l_index) := NVL(g_tab_entered_amount(l_index),0);
3589 		     END IF;
3590 
3591 		     IF NVL(g_tab_accted_amount(l_index),0) <0 THEN
3592 		        g_tab_accounted_dr(l_index) := 0;
3593 		        g_tab_accounted_cr(l_index) := ABS(NVL(g_tab_accted_amount(l_index),0));
3594                      ELSE
3595 		        g_tab_accounted_cr(l_index) := 0;
3596 		        g_tab_accounted_dr(l_index) := NVL(g_tab_accted_amount(l_index),0);
3597 		     END IF;
3598 
3599              	     IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3600  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'After deriving DR and CR column ');
3601  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Calling pa_funds_control_utils2.GET_DRAFTORBASELINE_BDGTVER');
3602          	     End if;
3603 
3604 		    -- Code to populate budget_version_id
3605                     g_tab_budget_version_id(l_index)
3606 			                 := pa_funds_control_utils2.GET_DRAFTORBASELINE_BDGTVER
3607                                                                      (g_tab_project_id(l_index),'GL','BASELINE');
3608                     If (g_tab_budget_version_id(l_index) is NULL ) Then
3609 
3610                            IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3611 	                        pa_funds_control_pkg.log_message(p_msg_token1 => 'Budget derivation failed marking to F166 ');
3612         	           End if;
3613                            g_tab_result_code(l_index)   := 'F166';
3614                            g_tab_status_code(l_index)   := 'R';
3615                            g_tab_fck_reqd_flag(l_index) := 'Y';
3616                            g_tab_budget_version_id(l_index) := NVL(pa_funds_control_utils2.GET_DRAFTORBASELINE_BDGTVER
3617                                                 (g_tab_project_id(l_index),'GL','DRAFT'),-9999);
3618                            -- Added as part of bug 13638904
3619                            fnd_message.set_name('PA','PA_BC_NO_BASELINE_BGT');
3620                            PSA_BC_XLA_PVT.psa_xla_error('PA_BC_NO_BASELINE_BGT',g_tab_source_event_id(l_index));
3621 
3622                            GOTO END_OF_AP_LOOP; -- process next record
3623 
3624                     End If; --If (g_tab_budget_version_id(l_index) is NULL ) Then
3625 		    --End of code to populate budget_version_id
3626 
3627                    IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3628 	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of derived Budget Version Id  ='||g_tab_budget_version_id(l_index));
3629         	   End if;
3630 
3631                    -- Code to check if there exists associated PO/Release and if exists create records
3632 		   -- to relieve amount variance/quantitiy variance AND PO .
3633 
3634   	           IF g_tab_ap_po_distribution_id(l_index) IS NOT NULL THEN
3635 
3636                        IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3637 	                  pa_funds_control_pkg.log_message(p_msg_token1 => 'Calling CREATE_BACKING_PO_APVAR_REC ');
3638             	       End if;
3639 
3640                        -- Creating PO relieving record by copying AP line record and overwriting required column values
3641 
3642 		       CREATE_BACKING_PO_APVAR_REC(p_copy_from_index  => l_index);
3643 
3644   	            END IF; --IF g_tab_ap_po_distribution_id(l_index) IS NOT NULL THEN
3645 
3646                    << END_OF_AP_LOOP>>
3647 		   NULL;
3648 		   END LOOP;
3649 
3650 		END IF;
3651 
3652                 IF pa_funds_control_pkg.g_debug_mode = 'Y' AND g_tab_doc_header_id.count<>0  THEN
3653 
3654   		      FOR l_index IN 1..g_tab_doc_header_id.last LOOP
3655 
3656  	                pa_funds_control_pkg.log_message(p_msg_token1 => '***Start of record-'||l_index||' records after firing AP fetching logic ***');
3657  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_budget_version_id = '||g_tab_budget_version_id(l_index));
3658  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_budget_line_id = '||g_tab_budget_line_id(l_index));
3659  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_budget_ccid = '||g_tab_budget_ccid(l_index));
3660  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_project_id = '||g_tab_project_id(l_index));
3661  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_task_id = '||g_tab_task_id(l_index));
3662  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_exp_type = '||g_tab_exp_type(l_index));
3663  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_exp_org_id = '||g_tab_exp_org_id(l_index));
3664  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_je_source_name = '||g_tab_je_source_name(l_index));
3665  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_je_category_name = '||g_tab_je_category_name(l_index));
3666  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_doc_type = '||g_tab_doc_type(l_index));
3667  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_doc_header_id = '||g_tab_doc_header_id(l_index));
3668  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_doc_line_id = '||g_tab_doc_line_id(l_index));
3669  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_doc_distribution_id = '||g_tab_doc_distribution_id(l_index));
3670  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_inv_distribution_id = '||g_tab_inv_distribution_id(l_index));
3671  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_actual_flag = '||g_tab_actual_flag(l_index));
3672  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_result_code = '||g_tab_result_code(l_index));
3673  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_status_code = '||g_tab_status_code(l_index));
3674  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_entered_amount = '||g_tab_entered_amount(l_index));
3675  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_accted_amount = '||g_tab_accted_amount(l_index));
3676                         pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_entered_dr = '||g_tab_entered_dr(l_index));
3677  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_entered_cr = '||g_tab_entered_cr(l_index));
3678  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_accounted_dr = '||g_tab_accounted_dr(l_index));
3679  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_accounted_cr = '||g_tab_accounted_cr(l_index));
3680  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_balance_posted_flag = '||g_tab_balance_posted_flag(l_index));
3681  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_funds_process_mode = '||g_tab_funds_process_mode(l_index));
3682  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_burden_cost_flag = '||g_tab_burden_cost_flag(l_index));
3683  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_pkt_reference1 = '||g_tab_pkt_reference1(l_index));
3684  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_pkt_reference2 = '||g_tab_pkt_reference2(l_index));
3685  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_pkt_reference3 = '||g_tab_pkt_reference3(l_index));
3686  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_event_id = '||g_tab_event_id(l_index));
3687  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_burden_method_code = '||g_tab_burden_method_code(l_index));
3688  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_main_or_backing_code = '||g_tab_main_or_backing_code(l_index));
3689  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_source_event_id = '||g_tab_source_event_id(l_index));
3690  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_trxn_ccid = '||g_tab_trxn_ccid(l_index));
3691  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_p_bc_packet_id = '||g_tab_p_bc_packet_id(l_index));
3692  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_fck_reqd_flag = '||g_tab_fck_reqd_flag(l_index));
3693  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_ap_quantity_variance = '||g_tab_ap_quantity_variance(l_index));
3694  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_ap_amount_variance = '||g_tab_ap_amount_variance(l_index));
3695  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_ap_base_qty_variance = '||g_tab_ap_base_qty_variance(l_index));
3696  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_ap_base_amount_variance = '||g_tab_ap_base_amount_variance(l_index));
3697  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_ap_po_distribution_id = '||g_tab_ap_po_distribution_id(l_index));
3698  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_gl_date = '||g_tab_gl_date(l_index));
3699  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_distribution_type = '||g_tab_distribution_type(l_index));
3700  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_po_release_id = '||g_tab_po_release_id(l_index));
3701  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_enc_type_id = '||g_tab_enc_type_id(l_index));
3702  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_period_name = '||g_tab_period_name(l_index));
3703  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_set_of_books_id = '||g_tab_set_of_books_id(l_index));
3704  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_exp_item_date = '||g_tab_exp_item_date(l_index));
3705  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_org_id = '||g_tab_org_id(l_index));
3706  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_vendor_id = '||g_tab_vendor_id(l_index));
3707                         pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_bc_packet_id = '||g_tab_bc_packet_id(l_index));
3708 			pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_parent_reversal_id = '||g_tab_parent_reversal_id(l_index));
3709  	                pa_funds_control_pkg.log_message(p_msg_token1 => '****End of record-'||l_index||' records after firing AP fetching logic***');
3710 
3711   		      END LOOP;
3712 
3713         	 End if;
3714 
3715 		END IF; --IF g_ap_inv_dist_id.count <> 0 THEN
3716 
3717         END IF; -- IF p_application_id = 200 THEN
3718 
3719 	IF p_application_id = 201 THEN -- Calling application is Purchasing
3720 
3721            	IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3722  	           pa_funds_control_pkg.log_message(p_msg_token1 => 'Fetching PO/REQ data ');
3723          	End if;
3724 
3725 		OPEN cur_po_bc_dist ;
3726 		FETCH cur_po_bc_dist BULK COLLECT INTO
3727 					g_tab_budget_version_id,
3728 					g_tab_budget_line_id,
3729 					g_tab_budget_ccid,
3730 					g_tab_project_id,
3731 					g_tab_task_id,
3732 					g_tab_exp_type,
3733 					g_tab_exp_org_id,
3734 					g_tab_exp_item_date,
3735 					g_tab_set_of_books_id,
3736 					g_tab_je_source_name,
3737 					g_tab_je_category_name,
3738 					g_tab_doc_type,
3739 					g_tab_doc_header_id,
3740 					g_tab_doc_line_id,
3741 					g_tab_doc_distribution_id,
3742 					g_tab_actual_flag,
3743 					g_tab_result_code,
3744 					g_tab_status_code,
3745 					g_tab_event_type_code,
3746                                      	g_tab_entered_amount,
3747                 	                g_tab_accted_amount,
3748 					g_tab_balance_posted_flag,
3749 					g_tab_funds_process_mode,
3750 					g_tab_burden_cost_flag,
3751 					g_tab_org_id,
3752 					g_tab_pkt_reference1,
3753 					g_tab_pkt_reference2,
3754 					g_tab_pkt_reference3,
3755 					g_tab_event_id,
3756 					g_tab_vendor_id,
3757 					g_tab_burden_method_code,
3758 					g_tab_main_or_backing_code,
3759 					g_tab_source_event_id,
3760 					g_tab_trxn_ccid,
3761 					g_tab_p_bc_packet_id,
3762 					g_tab_fck_reqd_flag,
3763 					/*g_tab_bc_commitment_id,
3764 					g_tab_ap_quantity_variance,
3765 					g_tab_ap_amount_variance,
3766 					g_tab_ap_base_qty_variance,
3767 					g_tab_ap_base_amount_variance,
3768 					g_tab_ap_po_distribution_id,*/
3769 					g_tab_gl_date,
3770                                         g_tab_period_name,
3771 					g_tab_distribution_type,
3772 					g_tab_po_release_id,
3773 					g_tab_enc_type_id,
3774                                      	g_tab_orig_sequence_num,
3775                                  	g_tab_applied_to_dist_id_2,
3776 					g_tab_bc_packet_id; -- Bug 5406690
3777 
3778 		CLOSE cur_po_bc_dist ;
3779 
3780            	IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3781  	           pa_funds_control_pkg.log_message(p_msg_token1 => 'Number of AP distributions fetched for FC ='||g_tab_set_of_books_id.count);
3782          	End if;
3783 
3784                IF pa_funds_control_pkg.g_debug_mode = 'Y' AND g_tab_doc_header_id.count<>0 THEN
3785 
3786   		      FOR l_index IN 1..g_tab_doc_header_id.last LOOP
3787 
3788  	                pa_funds_control_pkg.log_message(p_msg_token1 => '***Start of record-'||l_index||' records after fetching data from po_bc_distributions ***');
3789  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_budget_version_id = '||g_tab_budget_version_id(l_index));
3790  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_budget_line_id = '||g_tab_budget_line_id(l_index));
3791  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_budget_ccid = '||g_tab_budget_ccid(l_index));
3792  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_project_id = '||g_tab_project_id(l_index));
3793  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_task_id = '||g_tab_task_id(l_index));
3794  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_exp_type = '||g_tab_exp_type(l_index));
3795  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_exp_org_id = '||g_tab_exp_org_id(l_index));
3796  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_je_source_name = '||g_tab_je_source_name(l_index));
3797  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_je_category_name = '||g_tab_je_category_name(l_index));
3798  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_doc_type = '||g_tab_doc_type(l_index));
3799  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_doc_header_id = '||g_tab_doc_header_id(l_index));
3800  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_doc_line_id = '||g_tab_doc_line_id(l_index));
3801  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_doc_distribution_id = '||g_tab_doc_distribution_id(l_index));
3802  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_actual_flag = '||g_tab_actual_flag(l_index));
3803  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_result_code = '||g_tab_result_code(l_index));
3804  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_status_code = '||g_tab_status_code(l_index));
3805  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_entered_amount = '||g_tab_entered_amount(l_index));
3806  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_accted_amount = '||g_tab_accted_amount(l_index));
3807  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_balance_posted_flag = '||g_tab_balance_posted_flag(l_index));
3808  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_funds_process_mode = '||g_tab_funds_process_mode(l_index));
3809  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_burden_cost_flag = '||g_tab_burden_cost_flag(l_index));
3810  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_pkt_reference1 = '||g_tab_pkt_reference1(l_index));
3811  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_pkt_reference2 = '||g_tab_pkt_reference2(l_index));
3812  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_pkt_reference3 = '||g_tab_pkt_reference3(l_index));
3813  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_event_id = '||g_tab_event_id(l_index));
3814  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_burden_method_code = '||g_tab_burden_method_code(l_index));
3815  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_main_or_backing_code = '||g_tab_main_or_backing_code(l_index));
3816  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_source_event_id = '||g_tab_source_event_id(l_index));
3817  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_trxn_ccid = '||g_tab_trxn_ccid(l_index));
3818  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_p_bc_packet_id = '||g_tab_p_bc_packet_id(l_index));
3819  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_fck_reqd_flag = '||g_tab_fck_reqd_flag(l_index));
3820  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_gl_date = '||g_tab_gl_date(l_index));
3821  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_distribution_type = '||g_tab_distribution_type(l_index));
3822  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_po_release_id = '||g_tab_po_release_id(l_index));
3823  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_enc_type_id = '||g_tab_enc_type_id(l_index));
3824  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_period_name = '||g_tab_period_name(l_index));
3825  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_set_of_books_id = '||g_tab_set_of_books_id(l_index));
3826  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_exp_item_date = '||g_tab_exp_item_date(l_index));
3827  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_org_id = '||g_tab_org_id(l_index));
3828  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_vendor_id = '||g_tab_vendor_id(l_index));
3829  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_orig_sequence_num = '||g_tab_orig_sequence_num(l_index));
3830  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_applied_to_dist_id_2 = '||g_tab_applied_to_dist_id_2(l_index));
3831                         pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_bc_packet_id = '||g_tab_bc_packet_id(l_index));
3832  	                pa_funds_control_pkg.log_message(p_msg_token1 => '****End of record-'||l_index||' records after fetching data from po_bc_distributions***');
3833   		      END LOOP;
3834 
3835         	 End if;
3836 
3837 		-- Code to fetch Vendor_id and Org_id for PO/REQ records
3838 		IF g_tab_set_of_books_id.count <> 0 THEN
3839 
3840                    l_old_req_line_id  := 0;
3841 		   l_req_vendor_id    := 0;
3842 		   l_req_org_id       := 0;
3843 
3844                    l_old_po_header_id := 0;
3845 		   l_po_vendor_id     := 0;
3846 		   l_po_org_id        := 0;
3847 
3848                    IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3849                       pa_funds_control_pkg.log_message(p_msg_token1 => 'Calling DERIVE_DR_CR ');
3850                    End if;
3851 
3852 		   DERIVE_DR_CR;
3853 
3854 		   FOR l_index IN 1..g_tab_set_of_books_id.Last LOOP
3855 
3856                     IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3857  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Inside LOOP - document type = '||g_tab_doc_type(l_index));
3858  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Inside LOOP - distribution Id = '||g_tab_doc_distribution_id(l_index));
3859  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Inside LOOP - Entered Dr = '||g_tab_entered_dr(l_index));
3860 	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Inside LOOP - Entered Cr = '||g_tab_entered_cr(l_index));
3861         	    End if;
3862 
3863 		    -- Code to populate budget_version_id
3864                     g_tab_budget_version_id(l_index)
3865 			                 := pa_funds_control_utils2.GET_DRAFTORBASELINE_BDGTVER
3866                                                                      (g_tab_project_id(l_index),'GL','BASELINE');
3867                     If (g_tab_budget_version_id(l_index) is NULL ) Then
3868                            IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3869 	                        pa_funds_control_pkg.log_message(p_msg_token1 => 'Budget derivation failed marking to F166 ');
3870         	           End if;
3871                            g_tab_result_code(l_index)   := 'F166';
3872                            g_tab_status_code(l_index)   := 'R';
3873                            g_tab_fck_reqd_flag(l_index) := 'Y';
3874                            g_tab_budget_version_id(l_index) := NVL(pa_funds_control_utils2.GET_DRAFTORBASELINE_BDGTVER
3875                                                 (g_tab_project_id(l_index),'GL','DRAFT'),-9999);
3876 			    -- Added as part of bug 13638904
3877                            fnd_message.set_name('PA','PA_BC_NO_BASELINE_BGT');
3878                            PSA_BC_XLA_PVT.psa_xla_error('PA_BC_NO_BASELINE_BGT',g_tab_source_event_id(l_index));
3879 
3880                            GOTO END_OF_REQ_LOOP;
3881 
3882                     End If; --If (g_tab_budget_version_id(l_index) is NULL ) Then
3883 		    --End of code to populate budget_version_id
3884 
3885                    IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3886 	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Budget Version Id derived = '||g_tab_budget_version_id(l_index));
3887         	   End if;
3888 
3889                     IF g_tab_doc_type(l_index) ='REQ' THEN
3890 
3891 			 -- Code to populate vendor_id and org_id
3892 
3893                          IF l_old_req_line_id <> g_tab_doc_line_id(l_index) THEN
3894 
3895 	  	            OPEN cur_req_vend_org_details(g_tab_doc_line_id(l_index));
3896 		            FETCH cur_req_vend_org_details INTO
3897 			                 l_req_vendor_id,
3898 					 l_req_org_id;
3899                             CLOSE cur_req_vend_org_details;
3900                             l_old_req_line_id := g_tab_doc_line_id(l_index);
3901 
3902                             IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3903 	                       pa_funds_control_pkg.log_message(p_msg_token1 => 'Vendor detials : l_req_vendor_id  = '||l_req_vendor_id||' l_req_org_id = '||l_req_org_id);
3904         	            End if;
3905 
3906                          END IF; --IF l_old_req_line_id <> g_tab_doc_line_id(l_index) THEN
3907 
3908                          g_tab_vendor_id(l_index) := l_req_vendor_id;
3909 			 g_tab_org_id(l_index) := l_req_org_id ;
3910 
3911 			 -- End of Code to populate vendor_id and org_id
3912 
3913                          IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3914 	                   pa_funds_control_pkg.log_message(p_msg_token1 => 'main_or_backing_code = '||g_tab_main_or_backing_code(l_index));
3915            	         End if;
3916 
3917 			 -- Code to populate information for backing requisition
3918 		         IF g_tab_main_or_backing_code(l_index) = 'B_REQ' THEN
3919 
3920 			   IF is_req_based_po   (p_req_distribution_id => g_tab_doc_distribution_id(l_index),
3921 						 p_req_header_id => g_tab_doc_header_id(l_index),
3922 						 p_req_prevent_enc_flipped => PO_DOCUMENT_FUNDS_PVT.is_req_enc_flipped
3923 						                              (g_tab_doc_distribution_id(l_index),g_tab_source_event_id(l_index)),
3924 						 x_result_code => g_tab_result_code(l_index),
3925 						 x_status_code => g_tab_status_code(l_index),
3926 						 x_reference1  => g_tab_pkt_reference1(l_index),
3927 						 x_reference2  => g_tab_pkt_reference2(l_index),
3928 						 x_reference3  => g_tab_pkt_reference3(l_index)
3929 						) = 'Y' then
3930 				g_tab_p_bc_packet_id(l_index)  := -1;
3931 		           END IF;
3932                          END IF;
3933 			 -- End of Code to populate information for backing requisition
3934 
3935                     ELSIF g_tab_doc_type(l_index) ='PO' THEN
3936 
3937 			 -- Code to populate vendor_id and org_id
3938                          IF l_old_po_header_id <> g_tab_doc_header_id(l_index) THEN
3939 
3940 	  	            OPEN cur_po_vend_org_details(g_tab_doc_header_id(l_index));
3941 		            FETCH cur_po_vend_org_details INTO
3942 			                 l_po_vendor_id,
3943 					 l_po_org_id;
3944                             CLOSE cur_po_vend_org_details;
3945                             l_old_po_header_id := g_tab_doc_header_id(l_index);
3946 
3947                             IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
3948 	                       pa_funds_control_pkg.log_message(p_msg_token1 => 'Vendor detials : l_po_vendor_id  = '||l_po_vendor_id||' l_po_org_id = '||l_po_org_id);
3949         	            End if;
3950 
3951                          END IF; --IF l_old_po_header_id <> g_tab_doc_header_id(l_index) THEN
3952 
3953                          g_tab_vendor_id(l_index) := l_po_vendor_id;
3954 			 g_tab_org_id(l_index) := l_po_org_id ;
3955 
3956 			 -- End of Code to populate vendor_id and org_id
3957 
3958                     END IF; --IF g_pa_bc_packets_tab(l_index).document_type ='REQ' THEN
3959 
3960 		   -- Bug 5403775 : Below logic derives reference columns on backing documents such that
3961 		   -- they will point to main document
3962 		   -- Eg :
3963 		   -- a. for autocreated PO , PO record will have reference column mapping to
3964 		   -- itself and the B_REQ record will have reference columns mapping to PO .
3965 		   -- b. for release matched to PO, Release will have reference column mapping to
3966 		   -- itself and the B_PO record will have reference columns mapping to Release .
3967 
3968 		    IF g_tab_main_or_backing_code(l_index) <> 'M' AND NVL(g_tab_orig_sequence_num(l_index),0) <> 0 THEN
3969 
3970          		OPEN cur_po_main_doc_details(g_tab_source_event_id(l_index),g_tab_orig_sequence_num(l_index));
3971 			FETCH cur_po_main_doc_details INTO g_tab_pkt_reference1(l_index),g_tab_pkt_reference2(l_index);
3972 			CLOSE cur_po_main_doc_details;
3973 
3974 			g_tab_pkt_reference3(l_index) := g_tab_applied_to_dist_id_2(l_index);
3975 
3976                     END IF;
3977 
3978 	          <<END_OF_REQ_LOOP>>
3979 		  NULL;
3980                   END LOOP;
3981 
3982               END IF; --IF g_tab_set_of_books_id.count <> 0 THEN
3983 
3984               IF pa_funds_control_pkg.g_debug_mode = 'Y' AND g_tab_doc_header_id.count<>0  THEN
3985 
3986   		      FOR l_index IN 1..g_tab_doc_header_id.last LOOP
3987 
3988  	                pa_funds_control_pkg.log_message(p_msg_token1 => '***Start of record-'||l_index||' records after firing PO fetching logic ***');
3989  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_budget_version_id = '||g_tab_budget_version_id(l_index));
3990  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_budget_line_id = '||g_tab_budget_line_id(l_index));
3991  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_budget_ccid = '||g_tab_budget_ccid(l_index));
3992  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_project_id = '||g_tab_project_id(l_index));
3993  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_task_id = '||g_tab_task_id(l_index));
3994  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_exp_type = '||g_tab_exp_type(l_index));
3995  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_exp_org_id = '||g_tab_exp_org_id(l_index));
3996  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_je_source_name = '||g_tab_je_source_name(l_index));
3997  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_je_category_name = '||g_tab_je_category_name(l_index));
3998  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_doc_type = '||g_tab_doc_type(l_index));
3999  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_doc_header_id = '||g_tab_doc_header_id(l_index));
4000  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_doc_line_id = '||g_tab_doc_line_id(l_index));
4001  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_doc_distribution_id = '||g_tab_doc_distribution_id(l_index));
4002  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_actual_flag = '||g_tab_actual_flag(l_index));
4003  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_result_code = '||g_tab_result_code(l_index));
4004  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_status_code = '||g_tab_status_code(l_index));
4005  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_entered_amount = '||g_tab_entered_amount(l_index));
4006  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_accted_amount = '||g_tab_accted_amount(l_index));
4007                         pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_entered_dr = '||g_tab_entered_dr(l_index));
4008  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_entered_cr = '||g_tab_entered_cr(l_index));
4009  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_accounted_dr = '||g_tab_accounted_dr(l_index));
4010  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_accounted_cr = '||g_tab_accounted_cr(l_index));
4011  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_balance_posted_flag = '||g_tab_balance_posted_flag(l_index));
4012  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_funds_process_mode = '||g_tab_funds_process_mode(l_index));
4013  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_burden_cost_flag = '||g_tab_burden_cost_flag(l_index));
4014  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_pkt_reference1 = '||g_tab_pkt_reference1(l_index));
4015  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_pkt_reference2 = '||g_tab_pkt_reference2(l_index));
4016  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_pkt_reference3 = '||g_tab_pkt_reference3(l_index));
4017  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_event_id = '||g_tab_event_id(l_index));
4018  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_burden_method_code = '||g_tab_burden_method_code(l_index));
4019  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_main_or_backing_code = '||g_tab_main_or_backing_code(l_index));
4020  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_source_event_id = '||g_tab_source_event_id(l_index));
4021  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_trxn_ccid = '||g_tab_trxn_ccid(l_index));
4022  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_p_bc_packet_id = '||g_tab_p_bc_packet_id(l_index));
4023  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_fck_reqd_flag = '||g_tab_fck_reqd_flag(l_index));
4024  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_gl_date = '||g_tab_gl_date(l_index));
4025  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_distribution_type = '||g_tab_distribution_type(l_index));
4026  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_po_release_id = '||g_tab_po_release_id(l_index));
4027  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_enc_type_id = '||g_tab_enc_type_id(l_index));
4028  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_period_name = '||g_tab_period_name(l_index));
4029  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_set_of_books_id = '||g_tab_set_of_books_id(l_index));
4030  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_exp_item_date = '||g_tab_exp_item_date(l_index));
4031  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_org_id = '||g_tab_org_id(l_index));
4032  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_vendor_id = '||g_tab_vendor_id(l_index));
4033  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_orig_sequence_num = '||g_tab_orig_sequence_num(l_index));
4034  	                pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_applied_to_dist_id_2 = '||g_tab_applied_to_dist_id_2(l_index));
4035                         pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of g_tab_bc_packet_id = '||g_tab_bc_packet_id(l_index));
4036  	                pa_funds_control_pkg.log_message(p_msg_token1 => '****End of record-'||l_index||' records after firing PO fetching logic***');
4037   		      END LOOP;
4038 
4039         	End if;
4040 
4041         END IF; --IF p_application_id = 201 THEN
4042 
4043 	IF g_tab_set_of_books_id.count <> 0 THEN
4044 
4045 		SELECT gl_bc_packets_s.nextval
4046 		INTO l_packet_id
4047 		FROM dual;
4048 
4049                 IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
4050 	           pa_funds_control_pkg.log_message(p_msg_token1 => 'l_packet_id = '||l_packet_id);
4051                 End if;
4052 
4053 /* Commented for Bug 5726535
4054                -- Updating dangling records created in previous run to 'T' status
4055                FAIL_DANGLING_PKTS(l_packet_id);
4056 
4057                 IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
4058 	           pa_funds_control_pkg.log_message(p_msg_token1 => 'Updated I status records created in last run to T status '||SQL%ROWCOUNT);
4059 	           pa_funds_control_pkg.log_message(p_msg_token1 => 'Calling Load_pkts ');
4060                 End if;
4061 */
4062 
4063 	        Load_pkts(l_packet_id,p_bc_mode); -- to create bc records in autonomous mode;
4064 
4065                 IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
4066 	           pa_funds_control_pkg.log_message(p_msg_token1 => 'Calling update_cwk_pkt_lines ');
4067                 End if;
4068 
4069 		-- To update CWK related columns of pa_bc_packets PO records
4070 		update_cwk_pkt_lines (p_calling_module   => 'GL',
4071 				      p_packet_id        =>  l_packet_id);
4072 
4073                 IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
4074 	           pa_funds_control_pkg.log_message(p_msg_token1 => 'Calling Populate_burden_cost');
4075                 End if;
4076 
4077 		Populate_burden_cost
4078 			(p_packet_id            => l_packet_id
4079 			,p_calling_module       => 'GL'
4080 			,x_return_status        => l_return_status
4081 			,x_err_msg_code         => l_err_msg_code);
4082 
4083                 IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
4084 	           pa_funds_control_pkg.log_message(p_msg_token1 => 'After Populate_burden_cost l_return_status ='||l_return_status);
4085 	           pa_funds_control_pkg.log_message(p_msg_token1 => 'After Populate_burden_cost l_err_msg_code ='||l_err_msg_code);
4086 	           pa_funds_control_pkg.log_message(p_msg_token1 => 'Calling DERIVE_PKT_RLMI_BUDGET_CCID ');
4087                 End if;
4088 
4089 		-- Add code to handle exceptions
4090 
4091 		DERIVE_PKT_RLMI_BUDGET_CCID(p_packet_id => l_packet_id,
4092                                             p_bc_mode   => p_bc_mode);
4093 
4094                 -- When there is a failure in PA processing in full mode,
4095 		-- then do not call create_events and return a failure status
4096 		-- back to the calling program.
4097 
4098                 IF p_partial_flag = 'N' THEN
4099                    FULL_MODE_FAILURE (p_packet_id   => l_packet_id,
4100                                       p_bc_mode     => p_bc_mode,
4101                                       x_return_code => x_return_code);
4102 	        ELSE
4103 
4104 		   OPEN c_count_success_recs(l_packet_id);
4105 		   FETCH c_count_success_recs INTO l_count_success_rec;
4106 		   CLOSE c_count_success_recs;
4107 
4108 		   IF l_count_success_rec = 0 THEN
4109 		      IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
4110                          pa_funds_control_pkg.log_message(p_msg_token1 => 'In partial mode returning F status as all records have failed insert validation');
4111                       End if;
4112                       x_return_code := 'F';
4113 		   END IF;
4114 
4115                 END IF;
4116 
4117 	        IF x_return_code <> 'F' THEN
4118 
4119                   -------->6599207 ------As part of CC Enhancements -- Imposed the if condition.
4120 		  IF p_application_id <> 8407 THEN
4121 
4122                    IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
4123                       pa_funds_control_pkg.log_message(p_msg_token1 => 'Calling pa_xla_interface_pkg.create_events ');
4124                    End if;
4125 
4126 		   pa_xla_interface_pkg.create_events(p_calling_module  => 'FUNDS_CHECK',
4127 		               			      p_data_set_id     => l_packet_id,
4128 						      x_result_code     => l_return_status);
4129 
4130                    IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
4131 	              pa_funds_control_pkg.log_message(p_msg_token1 => 'After pa_xla_interface_pkg.create_events l_return_status ='||l_return_status);
4132                    End if;
4133 
4134                    --Failing records with null bc_event_id
4135                    FAIL_NULL_EVENT_PKTS (l_packet_id,p_bc_mode);
4136 
4137                   END IF;
4138 		  -------->6599207 ------END
4139 
4140                    IF p_partial_flag = 'N' THEN
4141                       FULL_MODE_FAILURE (p_packet_id   => l_packet_id,
4142                                          p_bc_mode     => p_bc_mode,
4143                                          x_return_code => x_return_code);
4144 	           ELSE
4145 
4146 		      OPEN c_count_success_recs(l_packet_id);
4147 		      FETCH c_count_success_recs INTO l_count_success_rec;
4148 		      CLOSE c_count_success_recs;
4149 
4150 		      IF l_count_success_rec = 0 THEN
4151 		         IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
4152                             pa_funds_control_pkg.log_message(p_msg_token1 => 'In partial mode returning F status as all records have failed insert validation');
4153                          End if;
4154                          x_return_code := 'F';
4155 		      END IF;
4156 
4157                    END IF;
4158 
4159                END IF; --IF x_return_code <> 'F' THEN
4160 
4161 	END IF; -- IF g_tab_set_of_books_id.count THEN
4162 
4163       END IF; -- IF p_application_id in (200, 201, 8407) THEN
4164 
4165         IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
4166             pa_funds_control_pkg.log_message(p_msg_token1 => 'CREATE_PROJ_ENCUMBRANCE_EVENTS : End ;Value of x_return_code '||x_return_code);
4167         End if;
4168 
4169  	RETURN;
4170 
4171 EXCEPTION
4172 	WHEN OTHERS THEN
4173 
4174                 IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
4175                   pa_funds_control_pkg.log_message(p_msg_token1 => 'CREATE_PROJ_ENCUMBRANCE_EVENTS : EXCEPTION '||SQLERRM);
4176                 End if;
4177 
4178                 -- have to put proper result code
4179  		PA_FUNDS_CONTROL_PKG.result_status_code_update
4180 			( p_status_code            => 'T',
4181              	      	p_result_code              => 'F09',
4182              		p_res_result_code          => 'F09',
4183              		p_res_grp_result_code      => 'F09',
4184              		p_task_result_code         => 'F09',
4185              		p_top_task_result_code     => 'F09',
4186              		p_project_result_code      => 'F09',
4187 			p_proj_acct_result_code    => 'F09',
4188              		p_packet_id                => l_packet_id
4189   			);
4190 
4191 		x_return_code := 'F';
4192 
4193 		IF CUR_AP_BC_DIST%ISOPEN THEN
4194 			CLOSE CUR_AP_BC_DIST;
4195 		END IF;
4196 
4197 		IF CUR_PO_BC_DIST%ISOPEN THEN
4198 			CLOSE CUR_PO_BC_DIST;
4199 		END IF;
4200 
4201 		RAISE;
4202 
4203 END CREATE_PROJ_ENCUMBRANCE_EVENTS;
4204 
4205 ----------------------------------------------------------------------------------
4206 --This is an Autonmous api which inserts records into the pa bc packets from
4207 -- plsql tables and commits
4208 ---------------------------------------------------------------------------------
4209 PROCEDURE Load_pkts (p_packet_id IN NUMBER,
4210                      p_bc_mode   IN VARCHAR2,
4211 		     p_calling_module IN VARCHAR2 DEFAULT NULL) IS
4212 
4213 	PRAGMA AUTONOMOUS_TRANSACTION;
4214         l_request_id      NUMBER := fnd_global.conc_request_id();
4215         l_program_id      NUMBER := fnd_global.conc_program_id();
4216         l_program_application_id NUMBER:= fnd_global.prog_appl_id();
4217         l_update_login    NUMBER := FND_GLOBAL.login_id;
4218         l_num_rows        NUMBER := 0;
4219         l_return_status    VARCHAR2(1);
4220 
4221  BEGIN
4222 
4223         IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
4224             pa_funds_control_pkg.log_message(p_msg_token1 => 'LOAD_PKTS : Start ');
4225         End if;
4226 
4227         If l_request_id is null then
4228                 l_request_id := -1;
4229         End if;
4230 
4231         if l_program_id is null then
4232                 l_program_id := -1;
4233         End if;
4234         If l_program_application_id is null then
4235                 l_program_application_id := -1;
4236         End if;
4237 
4238         If l_update_login is null then
4239                 l_update_login := -1;
4240         End if;
4241 
4242 	FORALL i in 1 .. g_tab_set_of_books_id.count
4243 		INSERT INTO PA_BC_PACKETS (
4244                         request_id,
4245                         program_id,
4246                         program_application_id,
4247                         program_update_date,
4248                         last_update_date,
4249                         last_updated_by,
4250                         created_by,
4251                         creation_date,
4252                         last_update_login,
4253                         ------ main columns-----------
4254                         packet_id,
4255                         bc_packet_id,
4256                         budget_version_id,
4257                         project_id,
4258                         task_id,
4259                         expenditure_type,
4260                         expenditure_organization_id,
4261                         expenditure_item_date,
4262                         set_of_books_id,
4263                         je_source_name,
4264                         je_category_name,
4265                         document_type,
4266                         document_header_id,
4267                         document_distribution_id,
4268                         actual_flag,
4269                         result_code,
4270                         status_code,
4271                         entered_dr,
4272                         entered_cr,
4273                         accounted_dr,
4274                         accounted_cr,
4275                         balance_posted_flag,
4276                         funds_process_mode,
4277                         txn_ccid,
4278                         burden_cost_flag,
4279                         org_id,
4280 			parent_bc_packet_id
4281 			,document_line_id
4282 			,reference1
4283 			,reference2
4284 			,reference3
4285 			-- R12 Funds management Uptake : Newly added columns
4286 			,bc_event_id
4287 			,vendor_id
4288 			,main_or_backing_code
4289 			,burden_method_code
4290 			,budget_line_id
4291 			,source_event_id
4292 			,ext_bdgt_flag
4293 			,gl_date
4294 			,period_name
4295 			,document_distribution_type
4296 			,DOCUMENT_HEADER_ID_2
4297 			,encumbrance_type_id
4298 			,proj_encumbrance_type_id
4299 			)
4300                 SELECT
4301                         l_request_id,
4302                         l_program_id,
4303                         l_program_application_id,
4304                         sysdate,
4305                         sysdate,
4306                         l_update_login,
4307                         l_update_login,
4308                         sysdate,
4309                         l_update_login,
4310                         p_packet_id,
4311                         g_tab_bc_packet_id(i), -- Bug 5406690
4312                         g_tab_budget_version_id(i),
4313                         g_tab_project_id(i),
4314                         g_tab_task_id(i),
4315                         g_tab_exp_type(i),
4316                         g_tab_exp_org_id(i),
4317                         g_tab_exp_item_date(i),
4318 			g_tab_set_of_books_id(i),
4319 			g_tab_je_source_name(i),
4320 			g_tab_je_category_name(i),
4321 			g_tab_doc_type(i),
4322 			g_tab_doc_header_id(i),
4323 			g_tab_doc_distribution_id(i),
4324 			g_tab_actual_flag(i),
4325 			g_tab_result_code(i),
4326 			g_tab_status_code(i),
4327                         NVL(pa_currency.round_trans_currency_amt(g_tab_entered_dr(i),g_acct_currency_code),0),
4328                         NVL(pa_currency.round_trans_currency_amt(g_tab_entered_cr(i),g_acct_currency_code),0),
4329                         NVL(pa_currency.round_trans_currency_amt(g_tab_accounted_dr(i),g_acct_currency_code),0),
4330                         NVL(pa_currency.round_trans_currency_amt(g_tab_accounted_cr(i),g_acct_currency_code),0),
4331 			g_tab_balance_posted_flag(i),
4332 			g_tab_funds_process_mode(i),
4333 			g_tab_trxn_ccid(i),
4334 			g_tab_burden_cost_flag(i),
4335 			g_tab_org_id(i),
4336                         g_tab_p_bc_packet_id(i),
4337 			g_tab_doc_line_id(i),
4338 			g_tab_pkt_reference1(i),
4339 			g_tab_pkt_reference2(i),
4340 			g_tab_pkt_reference3(i),
4341 			g_tab_event_id(i),
4342 			g_tab_vendor_id(i),
4343 			g_tab_main_or_backing_code(i),
4344 			g_tab_burden_method_code(i),
4345                         g_tab_budget_line_id(i),
4346 			g_tab_source_event_id(i),
4347 			PA_FUNDS_CONTROL_UTILS.get_bdgt_link(g_tab_project_id(i),'STD' ),
4348                         g_tab_gl_date(i),
4349 			g_tab_period_name(i),
4350                      	g_tab_distribution_type(i),
4351 			DECODE(g_tab_doc_type(i),'PO',g_tab_po_release_id(i),NULL),
4352 			g_tab_enc_type_id(i),
4353 			PA_FUNDS_CONTROL_UTILS.get_encum_type_id(g_tab_project_id(i),'STD')
4354 		FROM
4355 			dual
4356 		WHERE   g_tab_fck_reqd_flag(i) in ('R','Y')
4357 			-- fck_reqd_flag R - year end rollover
4358 		AND     ( nvl(g_tab_status_code(i),'P') <> 'V'
4359 			 and nvl(g_tab_result_code(i),'P') <> 'P113'
4360 		        );
4361 
4362 
4363         /* Added for Bug fix: 3086398 */
4364 	Update pa_bc_packets
4365            set status_code              = DECODE(status_code,'F',status_code,'R',status_code,'T',status_code,DECODE(p_bc_mode,'C','F','R')),
4366 	       res_result_code          = DECODE(substr(res_result_code,1,1),'F',res_result_code,result_code),
4367                res_grp_result_code      = DECODE(substr(res_grp_result_code,1,1),'F',res_grp_result_code,result_code),
4368                task_result_code         = DECODE(substr(task_result_code,1,1),'F',task_result_code,result_code),
4369                top_task_result_code     = DECODE(substr(top_task_result_code,1,1),'F',top_task_result_code,result_code),
4370                project_result_code      = DECODE(substr(project_result_code,1,1),'F',project_result_code,result_code),
4371                project_acct_result_code = DECODE(substr(project_acct_result_code,1,1),'F',project_acct_result_code,result_code)
4372         Where packet_id = p_packet_id
4373         AND   SUBSTR(result_code,1,1) = 'F'
4374         ANd   status_code in ( 'I' ,'R'); -- Bug 12597774 Added status_code R
4375 
4376         IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
4377             pa_funds_control_pkg.log_message(p_msg_token1 => 'NUmber of records updated to Failed status ='||SQL%ROWCOUNT);
4378         End if;
4379 
4380         -------->6599207 ------As part of CC Enhancements
4381 	---- Updating the ROWID on reference column so as to use
4382 	---- the same while creating liquidation entries in IGC interface table.
4383 
4384 	IF p_calling_module = 'CBC' THEN
4385 	FORALL I in 1..g_tab_rowid.count
4386 	Update pa_bc_packets set gl_row_number = g_tab_rowid(i)
4387 	where bc_packet_id = g_tab_bc_packet_id (i); END IF;
4388 
4389         -------->6599207 ------END
4390 
4391         /* End of bug fix :3086398 */
4392 
4393 	commit;
4394 
4395         IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
4396             pa_funds_control_pkg.log_message(p_msg_token1 => 'LOAD_PKTS : End ');
4397         End if;
4398 
4399 EXCEPTION
4400 WHEN OTHERS THEN
4401         IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
4402             pa_funds_control_pkg.log_message(p_msg_token1 => 'LOAD_PKTS : Exception '||SQLERRM);
4403         End if;
4404 
4405 	RAISE;
4406 
4407 END Load_pkts;
4408 
4409 -------->6599207 ------As part of CC Enhancements
4410 PROCEDURE assign_plsql_tabs(p_counter IN NUMBER,
4411                             p_fck_reqd_flag  varchar2 default null);
4412 -------->6599207 ------END
4413 
4414 
4415 -----------------------------------------------------------------------------
4416 --This is the main api which derives the project attributes such as
4417 -- project id, task id, exp item date, exp type, exp org for the
4418 -- transaction entered in gl bc packets or igc interface table
4419 -- R12 funds management uptake : renamed create_pkt_lines to update_cwk_pkt_lines
4420 -- as with r12 this is used to only process and update CWK related PO records
4421 ----------------------------------------------------------------------------
4422 PROCEDURE update_cwk_pkt_lines(p_calling_module   IN varchar2 ,
4423                                p_packet_id        IN NUMBER) IS
4424 
4425 	l_counter   NUMBER := 0;
4426 	l_doc_type  varchar2(100);
4427 	l_doc_dist_id  number;
4428 	l_doc_header_id number;
4429 	l_period_name   varchar2(100);
4430 	l_bc_commitment_id   number;
4431 	l_return_status  varchar2(1000);
4432         l_project_id            number;
4433         l_task_id               number;
4434         l_exp_type              varchar2(50);
4435         l_exp_item_date         date;
4436         l_exp_org_id            number;
4437         l_fck_reqd_flag         varchar2(10);
4438 	l_gl_amt_flag 		varchar2(10);
4439 	l_result_code		varchar2(10);
4440 	l_status_code		varchar2(10);
4441 	l_pkt_reference1        varchar2(100);
4442         l_pkt_reference2        varchar2(100);
4443         l_pkt_reference3        varchar2(100);
4444         /* Bug 3703180 **/
4445         l_doc_line_id_tab        pa_plsql_datatypes.IdTabTyp;
4446 	l_po_header_id_tab       pa_plsql_datatypes.IdTabTyp ;
4447 	l_po_distribution_id_tab pa_plsql_datatypes.IdTabTyp ;
4448 	l_bc_packet_id_tab       pa_plsql_datatypes.IdTabTyp ;
4449 	l_raw_amount_tab         pa_plsql_datatypes.NumTabTyp ;
4450 	l_amt_balance_tab        pa_plsql_datatypes.NumTabTyp;
4451 	l_entered_dr_tab        pa_plsql_datatypes.NumTabTyp;
4452 	l_entered_cr_tab        pa_plsql_datatypes.NumTabTyp;
4453 	l_accounted_dr_tab        pa_plsql_datatypes.NumTabTyp;
4454 	l_accounted_cr_tab        pa_plsql_datatypes.NumTabTyp;
4455 	l_cr_amount               number ;
4456         l_burd_exists_flag         VARCHAR2(1); --Bug 3749551
4457 
4458         -- R12 Funds management uptake: commented CBC related code
4459 
4460         /* Bug 3703180 **/
4461 	CURSOR is_cwk_po_unreserve is
4462 	  select distinct pbc.document_header_id,
4463 	                  pbc.document_line_id,
4464 			  org_id
4465             from pa_bc_packets pbc
4466            where packet_id     = p_packet_id
4467 	     and document_type = 'PO'
4468 	     and (nvl(accounted_dr,0) - nvl(accounted_cr,0)) < 0
4469 	     and not exists ( select 1
4470                                 from pa_bc_packets
4471                                where packet_id     = p_packet_id
4472 	                         and document_type <> 'PO') ;
4473 
4474        PROCEDURE Rate_PO_Unreserve( p_packet_id        number,
4475                                     p_bc_packet_id_tab pa_plsql_datatypes.IdTabTyp,
4476                                     p_entered_cr_tab   pa_plsql_datatypes.NumTabTyp,
4477                                     p_accounted_cr_tab pa_plsql_datatypes.NumTabTyp) is
4478 	PRAGMA AUTONOMOUS_TRANSACTION;
4479         begin
4480 	     FORALL i IN p_bc_packet_id_tab.first .. p_bc_packet_id_tab.last
4481 	        update pa_bc_packets
4482 		       set entered_cr   = p_entered_cr_tab(i),
4483 		           accounted_cr = p_accounted_cr_tab(i),
4484 			   entered_dr   = 0,
4485 			   accounted_dr = 0
4486                  where packet_id    = p_packet_id
4487 		   and bc_packet_id = p_bc_packet_id_tab(i) ;
4488 
4489              commit ;
4490         end Rate_PO_Unreserve ;
4491 
4492 BEGIN
4493 
4494         IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
4495             pa_funds_control_pkg.log_message(p_msg_token1 => 'update_cwk_pkt_lines : Start ');
4496         End if;
4497 
4498         -- R12 Funds management uptake: commented CBC related code
4499 
4500         /**** 3703180 ***/
4501 	/*
4502 	/* Issue : CWK PO was created for a project and using a web adi timecard was brought in
4503 	**         as actuals. In this case po receipt is not created and po cancellation
4504 	**         would liquidate without considering the cwk timecards. This is resulting
4505 	**         into -ve balance.
4506 	** Resolution :
4507 	**         We are finding out the summary amounts from pa bc packets or bc commitments
4508 	**         for a po line. Summary record has total raw amount and total amout relieved
4509 	**         we are checking if the po credit is creted in bc packets and comparing the
4510 	**         credit amount with the po balance at line level in the summary record.
4511 	**         BC packet record is updated with the amount ( whichever is less credit amount or
4512 	**         summary balance ) for a project, task and a po line.
4513 	** ***************************************************************************************/
4514 	g_cwk_po_unreserve := 'N' ;
4515         FOR c_rate_po in is_cwk_po_unreserve LOOP
4516 
4517 	   IF pa_funds_control_utils2.is_cwk_po(c_rate_po.document_header_id,
4518                                                 c_rate_po.document_line_id,
4519                                                 NULL,
4520                                                 c_rate_po.org_id) = 'Y' THEN
4521 		g_project_id_tab.delete ;
4522 		g_task_id_tab.delete ;
4523 		l_raw_amount_tab.delete ;
4524 		l_amt_balance_tab.delete ;
4525 		g_doc_line_id_tab.delete ;
4526 		g_bdamt_balance_tab.delete ;
4527 		g_burden_type_tab.delete ;
4528 
4529 		g_cwk_po_unreserve := 'Y' ;
4530                 /*
4531 		** 3703180 : Determine the summary record amounts.. for a line
4532 		*/
4533 		select distinct pbc.project_id,
4534 				pbc.task_id,
4535 				(nvl(pbc.comm_tot_raw_amt,0) - nvl(pbc.comm_raw_amt_relieved,0)) amount,
4536 				(nvl(pbc.comm_tot_bd_amt,0)  - nvl(pbc.comm_bd_amt_relieved,0) ) bd_amount,
4537 				pbc.document_line_id,
4538                                 decode(NVL(ppt.burden_cost_flag, 'N'),
4539                                            'Y',
4540                                            decode(NVL(ppt.burden_amt_display_method,'S'), 'S','SAME','D','DIFFERENT'),'NONE')
4541                   bulk collect into g_project_id_tab,
4542 		                    g_task_id_tab,
4543 				    l_amt_balance_tab,
4544 				    g_bdamt_balance_tab,
4545 				    g_doc_line_id_tab,
4546                                     g_burden_type_tab
4547 		  from pa_bc_packets     pbc ,
4548 		       pa_projects_all   pp,
4549 		       pa_project_types  ppt
4550 		 where pbc.document_type       = 'PO'
4551 		   and pbc.summary_record_flag = 'Y'
4552 		   and pbc.document_line_id    = c_rate_po.document_line_id
4553 		   and pbc.document_header_id  = c_rate_po.document_header_id
4554 		   and pbc.status_code         in ('A', 'C')
4555 		   and pbc.parent_bc_packet_id is NULL
4556 		   and pbc.project_id          = pp.project_id
4557 		   and pp.project_type         = ppt.project_type
4558 		   and (pbc.project_id, task_id)  in  ( select distinct project_id, task_id
4559 		                                          from pa_bc_packets
4560                                                          where packet_id           = p_packet_id
4561 							   and document_header_id  = c_rate_po.document_header_id
4562 							   and document_line_id    = c_rate_po.document_line_id )
4563 		   and pbc.packet_id           < p_packet_id ;
4564 
4565 		 IF g_project_id_tab.count = 0 THEN
4566 			select distinct pbc.project_id,
4567 					pbc.task_id,
4568 					(nvl(pbc.comm_tot_raw_amt,0) - nvl(pbc.comm_raw_amt_relieved,0)) amount,
4569 				        (nvl(pbc.comm_tot_bd_amt,0)  - nvl(pbc.comm_bd_amt_relieved,0) ) bd_amount,
4570 					pbc.document_line_id,
4571                                         decode(NVL(ppt.burden_cost_flag, 'N'),
4572                                            'Y',
4573                                            decode(NVL(ppt.burden_amt_display_method,'S'), 'S','SAME','D','DIFFERENT'),'NONE')
4574                           bulk collect into g_project_id_tab,
4575 		                            g_task_id_tab,
4576 				            l_amt_balance_tab,
4577 				            g_bdamt_balance_tab,
4578 				            g_doc_line_id_tab,
4579 					    g_burden_type_tab
4580 			  from pa_bc_commitments pbc,
4581 		               pa_projects_all   pp,
4582 		               pa_project_types  ppt
4583 			 where pbc.document_type       = 'PO'
4584 			   and pbc.summary_record_flag = 'Y'
4585 		           --and pbc.burden_cost_flag  = 'N'
4586 		           and pbc.parent_bc_packet_id is NULL
4587 			   and pbc.document_line_id    = c_rate_po.document_line_id
4588 		           and pbc.document_header_id  = c_rate_po.document_header_id
4589 		           and pbc.project_id          = pp.project_id
4590 		           and pp.project_type         = ppt.project_type
4591 		           and (pbc.project_id, pbc.task_id)  in  ( select distinct project_id, task_id
4592 		                                                      from pa_bc_packets
4593                                                                      where packet_id           = p_packet_id
4594 								       and document_header_id  = c_rate_po.document_header_id
4595 							               and document_line_id    = c_rate_po.document_line_id )
4596 			   and pbc.packet_id           < p_packet_id ;
4597 
4598 		 END IF ;
4599 
4600 
4601 		 IF g_project_id_tab.count > 0 THEN
4602 		    for line_indx in 1..g_project_id_tab.count LOOP
4603 
4604 		         l_bc_packet_id_tab.delete ;
4605 			 l_entered_dr_tab.delete ;
4606 			 l_entered_cr_tab.delete ;
4607 			 l_accounted_dr_tab.delete ;
4608 			 l_accounted_cr_tab.delete ;
4609 
4610 			/*
4611 			** 3703180 : fetch bc packet records for a project, task and po line.
4612 			*/
4613 			 select bc_packet_id,
4614 				entered_dr,
4615 				entered_cr,
4616 				accounted_dr,
4617 				accounted_cr
4618                            bulk collect into  l_bc_packet_id_tab,
4619 			                      l_entered_dr_tab,
4620 					      l_entered_cr_tab,
4621 					      l_accounted_dr_tab,
4622 					      l_accounted_cr_tab
4623 			   from pa_bc_packets
4624 			  where packet_id          = p_packet_id
4625 			    and document_line_id   = g_doc_line_id_tab(line_indx)
4626 			    and document_header_id = c_rate_po.document_header_id
4627 			    and project_id         = g_project_id_tab(line_indx)
4628 			    and task_id            = g_task_id_tab(line_indx) ;
4629 
4630 			/*
4631 			** 3703180 : Determine the credit amounts for a bc packets based on the summary
4632 			**           record amount relieved and balance amount.
4633 			*/
4634                           IF l_bc_packet_id_tab.count > 0 THEN
4635 
4636 			     for pkt_rec in 1..l_bc_packet_id_tab.count loop
4637 
4638 			       l_cr_amount := nvl(l_accounted_dr_tab(pkt_rec), 0) - nvl(l_accounted_cr_tab(pkt_rec),0) ;
4639 			       --
4640 			       -- Make sure signs are correct so that accounted_cr can be populated with the correct signs.
4641 			       --
4642 			       l_cr_amount := -1 * nvl(l_cr_amount,0) ;
4643 
4644 			       if l_amt_balance_tab(line_indx) <= 0 then
4645 
4646 				  l_entered_cr_tab(pkt_rec)   := 0 ;
4647 				  l_entered_dr_tab(line_indx) := 0 ;
4648 
4649 			       elsif l_cr_amount < l_amt_balance_tab(line_indx) then
4650 
4651 				  l_amt_balance_tab(line_indx)  := l_amt_balance_tab(line_indx) - nvl(l_cr_amount,0) ;
4652 				  l_entered_cr_tab(pkt_rec)     := l_cr_amount ;
4653 				  l_accounted_cr_tab(pkt_rec)   := l_cr_amount ;
4654 
4655 			       elsif l_cr_amount >= l_amt_balance_tab(line_indx) then
4656 				  l_entered_cr_tab(pkt_rec)   := l_amt_balance_tab(line_indx) ;
4657 				  l_accounted_cr_tab(pkt_rec) := l_amt_balance_tab(line_indx);
4658 				  l_amt_balance_tab(line_indx)  := 0 ;
4659 			       end if ;
4660 			     end loop ;
4661 			     /*
4662 			     ** 3703180 : Update the bc packet with the correct credit amount.
4663 			     */
4664                              rate_po_unreserve ( p_packet_id,
4665 			                         l_bc_packet_id_tab,
4666                                                  l_entered_cr_tab,
4667                                                  l_accounted_cr_tab ) ;
4668 
4669 			  end if ;
4670 		    end loop ; -- Line level summary record
4671 		 END IF ;  -- Summary record found.
4672 	   END IF ; -- Rate Based PO end if.
4673        END LOOP ; -- End of PO credit cursor.
4674       /*
4675       ** 3703180 : End of Changes
4676       */
4677 
4678        -- This object will be obsolete
4679 	If p_calling_module = 'GL' and g_doc_type = 'AP' then
4680 		pa_funds_control_pkg.log_message(p_msg_token1 => 'check invoice is interfaced');
4681                 PA_FUNDS_CONTROL_PKG.is_ap_from_project
4682                 (p_packet_id        => p_packet_id,
4683                 p_calling_module    => 'GL',
4684                 x_return_status     => l_return_status);
4685         End if;
4686 
4687         IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
4688             pa_funds_control_pkg.log_message(p_msg_token1 => 'update_cwk_pkt_lines : End ');
4689         End if;
4690 
4691 
4692 EXCEPTION
4693 
4694 	when others then
4695 
4696                 PA_FUNDS_CONTROL_PKG.log_message
4697                 (p_msg_token1 => 'failed in create bc pkt lines SQLERR :'||sqlcode||sqlerrm);
4698 		Raise;
4699 
4700 END update_cwk_pkt_lines;
4701 
4702 ------------------------------------------------------------------------------
4703 --/* This procedure creates burden transaction for the calling module
4704 -- * distribute Expense report and Transaction Import programs
4705 -- * this api uses the two Insert into select based on the project
4706 -- * type which is of burden on same ei or burden on different ei
4707 -- * the following api is created for the performance issue
4708 -- */
4709 ------------------------------------------------------------------------------
4710 PROCEDURE trxn_dister_burden_lines
4711           (p_packet_id  IN number,
4712 	   p_calling_module  IN varchar2,
4713 	   p_mode       IN varchar2 default 'R' )  IS
4714 
4715 	 PRAGMA AUTONOMOUS_TRANSACTION;
4716 
4717         l_request_id      NUMBER := fnd_global.conc_request_id();
4718         l_program_id      NUMBER := fnd_global.conc_program_id();
4719         l_program_application_id NUMBER := fnd_global.prog_appl_id();
4720         l_update_login    NUMBER := NVL(FND_GLOBAL.login_id,-1);
4721         l_userid          NUMBER := NVL(fnd_global.user_id,-1);
4722 	l_return_status   Varchar2(1000);
4723 
4724 
4725 BEGIN
4726 
4727         IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
4728             pa_funds_control_pkg.log_message(p_msg_token1 => 'trxn_dister_burden_lines : Start ');
4729             pa_funds_control_pkg.log_message(p_msg_token1 => 'p_calling_module is= '||p_calling_module);
4730             pa_funds_control_pkg.log_message(p_msg_token1 => 'p_mode is= '||p_mode);
4731 	End if;
4732 
4733 
4734 	If p_calling_module in ('DISTBTC','TRXNIMPORT','DISTVIADJ','DISTERADJ','TRXIMPORT','DISTCWKST')
4735 	     AND p_mode not in ('A','U') then
4736 
4737 		/* PA.M changes for contingent worker functionality */
4738 		/* This check is not required as this is done even before inserting the record into pa_bc_packets
4739 		 * during distribute process. Having this check is redudant
4740 		If p_calling_module = 'DISTCWKST' then
4741 			pa_funds_control_pkg.log_message(p_msg_token1 => 'Calling checkCWKbdExp Api to check burden cost codes');
4742 			-- check for the burden cost codes changed if so error out the transactions
4743 			checkCWKbdExp(p_packet_id  => p_packet_id
4744                         	,p_calling_module  => p_calling_module
4745                         	,x_return_status   => l_return_status
4746                         	);
4747 			pa_funds_control_pkg.log_message(p_msg_token1 => 'End of checkCWKbdExp Api');
4748 		End If;
4749 	       ***/
4750                                 /* This Query insert records into pa_bc_packets
4751                                  * for the projects which is of burden on same
4752                                  * expenditure item
4753                                  */
4754 
4755                                 INSERT INTO pa_bc_packets
4756                                         ( ---- who columns------
4757                                         request_id,
4758                                         program_id,
4759                                         program_application_id,
4760                                         program_update_date,
4761                                         last_update_date,
4762                                         last_updated_by,
4763                                         created_by,
4764                                         creation_date,
4765                                         last_update_login,
4766                                         ------ main columns-----------
4767                                         packet_id,
4768                                         bc_packet_id,
4769                                         budget_version_id,
4770                                         project_id,
4771                                         task_id,
4772                                         expenditure_type,
4773                                         expenditure_organization_id,
4774                                         expenditure_item_date,
4775                                         set_of_books_id,
4776                                         je_source_name,
4777                                         je_category_name,
4778                                         document_type,
4779                                         document_header_id,
4780                                         document_distribution_id,
4781                                         actual_flag,
4782                                         period_name,
4783                                         period_year,
4784                                         period_num,
4785                                         result_code,
4786                                         status_code,
4787                                         entered_dr,
4788                                         entered_cr,
4789                                         accounted_dr,
4790                                         accounted_cr,
4791                                         gl_row_number,    --gl_row_bc_packet_row_id
4792                                         balance_posted_flag,
4793                                         funds_process_mode,
4794                                         txn_ccid,
4795                                         parent_bc_packet_id,
4796                                         encumbrance_type_id,
4797                                         burden_cost_flag,
4798                                         org_id,
4799                                         gl_date,
4800                                         pa_date,
4801 					document_line_id,
4802 					compiled_multiplier,
4803 					reference1,
4804 					reference2,
4805 					reference3,
4806 					exp_item_id
4807                                         )
4808                                 SELECT
4809                                         l_request_id,
4810                                         l_program_id,
4811                                         l_program_application_id,
4812                                         sysdate,
4813                                         sysdate,
4814                                         l_userid,
4815                                         l_userid,
4816                                         sysdate,
4817                                         l_update_login,
4818                                         ------ main columns-----------
4819                                         pbc.packet_id,
4820                                         pa_bc_packets_s.nextval,
4821                                         pbc.budget_version_id,
4822                                         pbc.project_id,
4823                                         pbc.task_id,
4824                                         pbc.expenditure_type,
4825                                         pbc.expenditure_organization_id,
4826                                         pbc.expenditure_item_date,
4827                                         pbc.set_of_books_id,
4828                                         pbc.je_source_name,
4829                                         pbc.je_category_name,
4830                                         pbc.document_type,
4831                                         pbc.document_header_id,
4832                                         pbc.document_distribution_id,
4833                                         pbc.actual_flag,
4834                                         --decode(pbc.document_type,'AP',(
4835                                         /** pagl period enhancement changes instead of passing pa date
4836                                             pass transaction date to derive the period name
4837                                         pa_funds_control_pkg1.get_period_name(pa_utils2.get_pa_date
4838                                           (pbc.expenditure_item_date,NULL,pbc.org_id),pbc.set_of_books_id),
4839                                                 -- ),pbc.period_name),
4840                                         **/
4841 					/** Bug fix:2905892 As per discussions with Barbara , Dinakar, Prithi
4842                                          *  for Transaction import process the period name is to derived
4843                                          * based on the orginal raw line for the burden transactions
4844                                          * so reverting back to changes made earlier
4845                                          *pa_funds_control_pkg1.get_period_name
4846                                          * (pbc.expenditure_item_date,pbc.set_of_books_id),**/
4847 					pbc.period_name,
4848 					/** End of bug fix: 2905892 ***/
4849                                         pbc.period_year,
4850                                         pbc.period_num,
4851                                         pbc.result_code,
4852                                         pbc.status_code,
4853                                         pa_currency.round_trans_currency_amt(
4854                                         DECODE ( NVL ( pbc.entered_dr, 0 ), 0, 0,
4855                                                 (( NVL (pbc.entered_dr ,0) *
4856                                                 NVL (pa_funds_control_utils.get_fc_compiled_multiplier
4857                                                         (  pbc.expenditure_organization_id,
4858                                                            pbc.task_id,
4859                                                           pbc.expenditure_item_date,
4860                                                            'C',
4861                                                           pbc.expenditure_type
4862                                                         ), 0)))),g_acct_currency_code),
4863                                         pa_currency.round_trans_currency_amt(
4864                                         DECODE ( NVL ( pbc.entered_cr, 0 ), 0, 0,
4865                                                 (( NVL (pbc.entered_cr ,0) *
4866                                                 NVL (pa_funds_control_utils.get_fc_compiled_multiplier
4867                                                         (  pbc.expenditure_organization_id,
4868                                                            pbc.task_id,
4869                                                           pbc.expenditure_item_date,
4870                                                            'C',
4871                                                            pbc.expenditure_type
4872                                                 ), 0)))),g_acct_currency_code),
4873                                         pa_currency.round_trans_currency_amt(
4874                                         DECODE ( NVL ( pbc.accounted_dr, 0 ), 0, 0,
4875                                                 (( NVL (pbc.accounted_dr ,0) *
4876                                                 NVL (pa_funds_control_utils.get_fc_compiled_multiplier
4877                                                         (  pbc.expenditure_organization_id,
4878                                                            pbc.task_id,
4879                                                           pbc.expenditure_item_date,
4880                                                            'C',
4881                                                           pbc.expenditure_type
4882                                                         ), 0)))),g_acct_currency_code),
4883                                         pa_currency.round_trans_currency_amt(
4884                                         DECODE ( NVL ( pbc.accounted_cr, 0 ), 0, 0,
4885                                                 (( NVL (pbc.accounted_cr ,0) *
4886                                                 NVL (pa_funds_control_utils.get_fc_compiled_multiplier
4887                                                         (  pbc.expenditure_organization_id,
4888                                                            pbc.task_id,
4889                                                           pbc.expenditure_item_date,
4890                                                            'C',
4891                                                            pbc.expenditure_type
4892                                                 ), 0)))),g_acct_currency_code),
4893 
4894                                         NULL,    --gl_row_bc_packet_row_id
4895                                         pbc.balance_posted_flag,
4896                                         pbc.funds_process_mode,
4897                                         pbc.txn_ccid,
4898                                         pbc.bc_packet_id,
4899                                         pbc.encumbrance_type_id,
4900                                         'O',
4901                                         pbc.org_id,
4902                                         pbc.gl_date,
4903                                         pbc.pa_date,
4904 					pbc.document_line_id,
4905 					pa_funds_control_utils.get_fc_compiled_multiplier
4906                                                         (  pbc.expenditure_organization_id,
4907                                                            pbc.task_id,
4908                                                            pbc.expenditure_item_date,
4909                                                            'C',
4910                                                           pbc.expenditure_type
4911                                                         )
4912 				,pbc.reference1
4913 				,pbc.reference2
4914 				,pbc.reference3
4915 				,pbc.exp_item_id
4916                                 FROM pa_bc_packets pbc
4917                                 WHERE  pbc.packet_id = p_packet_id
4918                                 AND pbc.parent_bc_packet_id = -1
4919                                 AND substr(nvl(pbc.result_code,'X'),1,1) <> 'F'
4920                                 AND pa_funds_control_pkg.check_bdn_on_sep_item
4921                                         (pbc.project_id ) = 'S'
4922                                 AND NVL (pa_funds_control_utils.get_fc_compiled_multiplier
4923                                                         (  pbc.expenditure_organization_id,
4924                                                            pbc.task_id,
4925                                                           pbc.expenditure_item_date,
4926                                                            'C',
4927                                                            pbc.expenditure_type
4928                                                 ), 0) <> 0;
4929 
4930 				pa_funds_control_pkg.log_message(p_msg_token1 =>
4931 					'Num of records inserted ='||sql%rowcount);
4932 
4933 				/* This Query insert records into pa_bc_packets
4934 				 * for the projects which is of burden on different
4935 				 * expenditure item
4936 				 */
4937 
4938                                 INSERT INTO pa_bc_packets
4939                                         ( ---- who columns------
4940                                         request_id,
4941                                         program_id,
4942                                         program_application_id,
4943                                         program_update_date,
4944                                         last_update_date,
4945                                         last_updated_by,
4946                                         created_by,
4947                                         creation_date,
4948                                         last_update_login,
4949                                         ------ main columns-----------
4950                                         packet_id,
4951                                         bc_packet_id,
4952                                         budget_version_id,
4953                                         project_id,
4954                                         task_id,
4955                                         expenditure_type,
4956                                         expenditure_organization_id,
4957                                         expenditure_item_date,
4958                                         set_of_books_id,
4959                                         je_source_name,
4960                                         je_category_name,
4961                                         document_type,
4962                                         document_header_id,
4963                                         document_distribution_id,
4964                                         actual_flag,
4965                                         period_name,
4966                                         period_year,
4967                                         period_num,
4968                                         result_code,
4969                                         status_code,
4970                                         entered_dr,
4971                                         entered_cr,
4972                                         accounted_dr,
4973                                         accounted_cr,
4974                                         gl_row_number,    --gl_row_bc_packet_row_id
4975                                         balance_posted_flag,
4976                                         funds_process_mode,
4977                                         txn_ccid,
4978                                         parent_bc_packet_id,
4979                                         encumbrance_type_id,
4980                                         burden_cost_flag,
4981                                         org_id,
4982                                         gl_date,
4983                                         pa_date,
4984 					document_line_id,
4985 					compiled_multiplier,
4986 					reference1,
4987 					reference2,
4988 					reference3,
4989 					exp_item_id
4990                                         )
4991                                 SELECT
4992                                         l_request_id,
4993                                         l_program_id,
4994                                         l_program_application_id,
4995                                         sysdate,
4996                                         sysdate,
4997                                         l_userid,
4998                                         l_userid,
4999                                         sysdate,
5000                                         l_update_login,
5001                                         ------ main columns-----------
5002                                         pbc.packet_id,
5003                                         pa_bc_packets_s.nextval,
5004                                         pbc.budget_version_id,
5005                                         pbc.project_id,
5006                                         pbc.task_id,
5007                                         et.expenditure_type,
5008                                         pbc.expenditure_organization_id,
5009                                         pbc.expenditure_item_date,
5010                                         pbc.set_of_books_id,
5011                                         pbc.je_source_name,
5012                                         pbc.je_category_name,
5013                                         pbc.document_type,
5014                                         pbc.document_header_id,
5015                                         pbc.document_distribution_id,
5016                                         pbc.actual_flag,
5017                                         /** pagl period enhancement changes instead of passing pa date
5018                                             pass transaction date to derive the period name
5019                                         pa_funds_control_pkg1.get_period_name(pa_utils2.get_pa_date
5020                                           (pbc.expenditure_item_date,NULL,pbc.org_id),pbc.set_of_books_id),
5021                                         **/
5022                                         /** Bug fix:2905892 As per discussions with Barbara , Dinakar, Prithi
5023                                          *  for Transaction import process the period name is to derived
5024                                          * based on the orginal raw line for the burden transactions
5025                                          * so reverting back to changes made earlier
5026                                          *  pa_funds_control_pkg1.get_period_name
5027                                          * (pbc.expenditure_item_date,pbc.set_of_books_id), --pbc.period_name, **/
5028                                         pbc.period_name,
5029                                         /** End of bug fix: 2905892 **/
5030                                         pbc.period_year,
5031                                         pbc.period_num,
5032                                         pbc.result_code,
5033                                         pbc.status_code,
5034                                         pa_currency.round_trans_currency_amt(
5035                                         decode(nvl(pbc.entered_dr,0),0,0,(nvl(pbc.entered_dr,0)*
5036                                                 cm.compiled_multiplier)),g_acct_currency_code),
5037                                         pa_currency.round_trans_currency_amt(
5038                                         decode(nvl(pbc.entered_cr,0),0,0,(nvl(pbc.entered_cr,0)*
5039                                                 cm.compiled_multiplier)),g_acct_currency_code),
5040                                         pa_currency.round_trans_currency_amt(
5041                                         decode(nvl(pbc.accounted_dr,0),0,0,(nvl(pbc.accounted_dr,0)*
5042                                                 cm.compiled_multiplier)),g_acct_currency_code),
5043                                         pa_currency.round_trans_currency_amt(
5044                                         decode(nvl(pbc.accounted_cr,0),0,0,(nvl(pbc.accounted_cr,0)*
5045                                                 cm.compiled_multiplier)),g_acct_currency_code),
5046                                         NULL,    --gl_row_bc_packet_row_id
5047                                         pbc.balance_posted_flag,
5048                                         pbc.funds_process_mode,
5049                                         pbc.txn_ccid,
5050                                         pbc.bc_packet_id,
5051                                         pbc.encumbrance_type_id,
5052                                         'O',
5053                                         pbc.org_id,
5054                                         pbc.gl_date,
5055                                         pbc.pa_date,
5056 					pbc.document_line_id,
5057 					cm.compiled_multiplier,
5058 					pbc.reference1,
5059 					pbc.reference2,
5060 					pbc.reference3,
5061 					pbc.exp_item_id
5062                                 FROM
5063                                         pa_ind_rate_sch_revisions irsr,
5064                                         pa_cost_bases cb,
5065                                         pa_expenditure_types et,
5066                                         pa_ind_cost_codes icc,
5067                                         pa_cost_base_exp_types cbet,
5068                                         pa_ind_rate_schedules_all_bg irs,
5069                                         pa_ind_compiled_sets ics,
5070                                         pa_compiled_multipliers cm,
5071                                         pa_bc_packets pbc
5072                                 WHERE irsr.cost_plus_structure = cbet.cost_plus_structure
5073                                 AND cb.cost_base = cbet.cost_base
5074                                 AND cb.cost_base_type = cbet.cost_base_type
5075                                 AND et.expenditure_type = icc.expenditure_type
5076                                 AND icc.ind_cost_code = cm.ind_cost_code
5077                                 AND cbet.cost_base = cm.cost_base
5078                                 AND cbet.cost_base_type = 'INDIRECT COST'
5079                                 AND cbet.expenditure_type = pbc.expenditure_type
5080                                 AND irs.ind_rate_sch_id = irsr.ind_rate_sch_id
5081                                 AND ics.ind_rate_sch_revision_id = irsr.ind_rate_sch_revision_id
5082                                 AND ics.organization_id = pbc.expenditure_organization_id
5083                                 AND ics.ind_compiled_set_id =
5084                                         pa_funds_control_utils.get_fc_compiled_set_id
5085                                           (pbc.task_id,
5086                                           pbc.expenditure_item_date,
5087                                           pbc.expenditure_organization_id,
5088                                           'C'
5089 					  , 'COMPILE_SET_ID'
5090 					  ,pbc.expenditure_type)  -- Added for burdening changes
5091 				AND ics.cost_base = cb.cost_base -- Added for burdening changes
5092                                 AND cm.ind_compiled_set_id = ics.ind_compiled_set_id
5093                                 AND cm.compiled_multiplier <> 0
5094                                 AND pbc.packet_id = p_packet_id
5095                                 AND pbc.parent_bc_packet_id  = -1
5096                                 AND substr(nvl(pbc.result_code,'X'),1,1) <> 'F'
5097 				AND pa_funds_control_pkg.check_bdn_on_sep_item
5098 					(pbc.project_id ) = 'D';
5099 
5100                                 pa_funds_control_pkg.log_message(p_msg_token1 =>
5101                                         'Num of records inserted ='||sql%rowcount);
5102 
5103 
5104 	End if;
5105 
5106         IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
5107             pa_funds_control_pkg.log_message(p_msg_token1 => 'trxn_dister_burden_lines : End ');
5108 	End if;
5109 
5110 	commit;
5111 	return;
5112 
5113 EXCEPTION
5114 
5115 	WHEN OTHERS THEN
5116 		RAISE;
5117 
5118 END trxn_dister_burden_lines;
5119 
5120 --
5121 -- Bug : 3703180
5122 -- PJ.M:B5:P1:QA:CWK: PAXBLRSL- -VE COMMITMENT CREATED WHEN PO CANCELLED AFTER
5123 -- Resolution : Compare the burden cost calculated with the summary record in
5124 -- pa_bc_packets /pa_bc_commitments_all. We are using the amounts in the summary
5125 -- table if summary has amounts less than the calculated in pa bc packets.
5126 -- If we have zero burden in pa bc packets then we use the entire amounts in
5127 -- the summary record.
5128 --
5129 PROCEDURE update_cwk_po_burden(p_packet_id NUMBER ) is
5130 	   PRAGMA AUTONOMOUS_TRANSACTION;
5131 
5132         /* Bug 3703180 **/
5133 	l_project_id        NUMBER ;
5134 	l_task_id           NUMBER ;
5135 	l_doc_line_id       NUMBER ;
5136 	l_bdamt_balance     NUMBER ;
5137 	l_index             NUMBER ;
5138 	l_cr_amount         NUMBER ;
5139 	l_bc_packet_id_tab  pa_plsql_datatypes.IdTabTyp ;
5140 	l_entered_dr_tab    pa_plsql_datatypes.NumTabTyp;
5141 	l_entered_cr_tab    pa_plsql_datatypes.NumTabTyp;
5142 	l_accounted_dr_tab  pa_plsql_datatypes.NumTabTyp;
5143 	l_accounted_cr_tab  pa_plsql_datatypes.NumTabTyp;
5144 	l_exp_type_tab      pa_plsql_datatypes.char50TabTyp;
5145 
5146 BEGIN
5147 
5148 
5149         IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
5150             pa_funds_control_pkg.log_message(p_msg_token1 => 'update_cwk_po_burden : Start ');
5151 	End if;
5152 
5153     IF  g_project_id_tab.count > 0 THEN
5154         for line_indx in 1..g_project_id_tab.count LOOP
5155 
5156 	    if g_burden_type_tab(line_indx) = 'SAME' then
5157 	       l_bdamt_balance := g_bdamt_balance_tab(line_indx) ;
5158 	       l_bc_packet_id_tab.delete ;
5159 	       l_entered_dr_tab.delete ;
5160 	       l_entered_cr_tab.delete ;
5161 	       l_accounted_dr_tab.delete ;
5162 	       l_accounted_cr_tab.delete ;
5163 
5164                /* Bug 3703180
5165 	          spool the descending burden amounts in pl sql array.
5166 	       **/
5167 	       select bc_packet_id,
5168 		      entered_dr,
5169 		      entered_cr,
5170 		      accounted_dr,
5171 		      accounted_cr
5172 		 bulk collect into  l_bc_packet_id_tab,
5173 		                    l_entered_dr_tab,
5174 				    l_entered_cr_tab,
5175 				    l_accounted_dr_tab,
5176 				    l_accounted_cr_tab
5177 	         from pa_bc_packets
5178 		where packet_id        = p_packet_id
5179 		  and document_line_id = g_doc_line_id_tab(line_indx)
5180 		  and project_id       = g_project_id_tab(line_indx)
5181 		  and task_id          = g_task_id_tab(line_indx)
5182 		  and parent_bc_packet_id is not NULL
5183 		 order by abs(nvl(accounted_dr,0) - nvl(accounted_cr,0))  desc;
5184 
5185 	       IF l_bc_packet_id_tab.count > 0 THEN
5186 	       --
5187 	       -- bug 3703180
5188 	       -- compare the burden cost with the summary record and use the summary burden cost
5189 	       -- if calculated burden is ZERO or less then the pa bc packets burden.
5190 	       --
5191 
5192 	          for pkt_rec in 1..l_bc_packet_id_tab.count loop
5193 
5194 		      l_cr_amount := NVL(l_accounted_dr_tab(pkt_rec),0) - NVL(l_accounted_cr_tab(pkt_rec),0)  ;
5195 		      -- Make sure signs are correct ...
5196 		      l_cr_amount := -1 * NVL(l_cr_amount,0) ;
5197 
5198 		      if l_bdamt_balance <= 0 then
5199 
5200 			 l_entered_cr_tab(pkt_rec)   := 0 ;
5201 			 l_accounted_cr_tab(pkt_rec) := 0 ;
5202 
5203 		      elsif l_cr_amount <= l_bdamt_balance then
5204 
5205 			    l_bdamt_balance             := l_bdamt_balance - l_cr_amount ;
5206 			    l_entered_cr_tab(pkt_rec)   := l_cr_amount ;
5207 			    l_accounted_cr_tab(pkt_rec) := l_cr_amount ;
5208 
5209 		      elsif l_cr_amount > l_bdamt_balance then
5210 		            l_entered_cr_tab(pkt_rec)   := l_bdamt_balance ;
5211 			    l_accounted_cr_tab(pkt_rec) := l_bdamt_balance;
5212 			    l_bdamt_balance             := 0 ;
5213 		      end if ;
5214 		      if l_cr_amount = 0 and l_bdamt_balance > 0 THEN
5215 		         l_entered_cr_tab(pkt_rec)   := l_bdamt_balance ;
5216 			 l_accounted_cr_tab(pkt_rec) := l_bdamt_balance;
5217 			 l_bdamt_balance  := 0 ;
5218 		      end if ;
5219 	          end loop ;
5220 
5221 	          --
5222 	          -- BUG 3703180
5223 	          -- Update the calculated burden cost to pa bc packets.
5224 	          --
5225 		  FORALL i IN l_bc_packet_id_tab.first .. l_bc_packet_id_tab.last
5226 		       update pa_bc_packets
5227 		          set entered_cr   = l_entered_cr_tab(i),
5228 			      accounted_cr = l_accounted_cr_tab(i),
5229 			      entered_dr   = 0,
5230 			      accounted_dr = 0
5231 			where packet_id    = p_packet_id
5232 			  and bc_packet_id = l_bc_packet_id_tab(i) ;
5233 	       END IF ; -- l_bc_packet_id_tab.count > 0
5234 
5235 	    --
5236 	    -- BUG 3703180
5237 	    -- Different line burdening setup.
5238 	    -- We determine the summary record from pa bc packets or pa bc commitments all table.
5239 	    -- compare the summary amounts with the pa bc packets burden and use the one less than
5240 	    -- the other. If pa bc packets burden cost is ZERO than we use the burden cost from the
5241 	    -- summary table record.
5242 	    --
5243 	    elsif g_burden_type_tab(line_indx) = 'DIFFERENT' then
5244 
5245               l_exp_type_tab.delete ;
5246 
5247 	      --
5248 	      -- BUG 3703180
5249 	      -- Determine the burden expenditure type from pa bc packets.
5250 	      --
5251               select distinct pbc.expenditure_type
5252                 bulk collect into  l_exp_type_tab
5253                 from pa_bc_packets pbc
5254                 where packet_id     = p_packet_id
5255                   and document_type = 'PO'
5256                   and parent_bc_packet_id is not NULL
5257 		  and project_id    = g_project_id_tab(line_indx)
5258 		  and task_id       = g_task_id_tab(line_indx)
5259 		  and document_line_id = g_doc_line_id_tab(line_indx) ;
5260 
5261               IF l_exp_type_tab.count > 0 THEN
5262 	         --
5263 	         -- BUG 3703180
5264 		 -- Determine the summary amounts from pa bc packets or pa bc commitments table.
5265 		 --
5266                  for indx in 1..l_exp_type_tab.count loop
5267 
5268 		    g_bdamt_balance_tab.delete ;
5269                     /*
5270                     ** 3703180 : Determine the summary record amounts.. for a line
5271                     */
5272                     select (nvl(comm_tot_bd_amt,0)  - nvl(comm_bd_amt_relieved,0) ) bd_amount
5273                       bulk collect into g_bdamt_balance_tab
5274                       from pa_bc_packets
5275                      where document_type       = 'PO'
5276                        and summary_record_flag = 'Y'
5277                        and document_line_id    = g_doc_line_id_tab(line_indx)
5278                        and status_code         in ('A', 'C')
5279                        --and burden_cost_flag    = 'O'
5280 		       and parent_bc_packet_id is not NULL
5281                        and project_id          = g_project_id_tab(line_indx)
5282                        and task_id             = g_task_id_tab(line_indx)
5283                        and expenditure_type    = l_exp_type_tab(indx)
5284                        and packet_id           < p_packet_id ;
5285 
5286                     IF g_bdamt_balance_tab.count = 0 THEN
5287                        select (nvl(comm_tot_bd_amt,0)  - nvl(comm_bd_amt_relieved,0) ) bd_amount
5288                          bulk collect into g_bdamt_balance_tab
5289                          from pa_bc_commitments
5290                         where document_type       = 'PO'
5291 			  and summary_record_flag = 'Y'
5292 			   -- and burden_cost_flag    = 'O'
5293 		          and parent_bc_packet_id is not NULL
5294 			  and document_line_id    = g_doc_line_id_tab(line_indx)
5295 			  and project_id          = g_project_id_tab(line_indx)
5296 			  and task_id             = g_task_id_tab(line_indx)
5297 			  and expenditure_type    = l_exp_type_tab(indx)
5298 			  and packet_id           < p_packet_id ;
5299                     END IF ;
5300 
5301                     if g_bdamt_balance_tab.count > 0 then
5302 	               l_bdamt_balance := g_bdamt_balance_tab(1) ;
5303                     else
5304 	               l_bdamt_balance := 0 ;
5305                     end if ;
5306 
5307 		    l_bc_packet_id_tab.delete ;
5308 		    l_entered_dr_tab.delete ;
5309 		    l_entered_cr_tab.delete ;
5310 		    l_accounted_dr_tab.delete ;
5311 		    l_accounted_cr_tab.delete ;
5312 
5313 	            --
5314 	            -- BUG 3703180
5315 		    -- Get the pa bc packets burden cost in the descending order
5316 		    --
5317 		    select bc_packet_id,
5318 		           entered_dr,
5319 			   entered_cr,
5320 			   accounted_dr,
5321 			   accounted_cr
5322 		      bulk collect into  l_bc_packet_id_tab,
5323 			   l_entered_dr_tab,
5324 			   l_entered_cr_tab,
5325 			   l_accounted_dr_tab,
5326 			   l_accounted_cr_tab
5327 		      from pa_bc_packets
5328 		     where packet_id         = p_packet_id
5329 		       and document_line_id  = g_doc_line_id_tab(line_indx)
5330 		       and project_id        = g_project_id_tab(line_indx)
5331 		       and task_id           = g_task_id_tab(line_indx)
5332 		       and parent_bc_packet_id is not NULL
5333 		       and expenditure_type  = l_exp_type_tab(indx)
5334 		     order by abs(nvl(entered_dr,0) - nvl(entered_cr,0))  desc;
5335 
5336 	            --
5337 	            -- BUG 3703180
5338 		    -- Compare the burden cost with the summary table record.
5339 		    --
5340                     IF l_bc_packet_id_tab.count > 0 THEN
5341                        for pkt_rec in 1..l_bc_packet_id_tab.count loop
5342 
5343 			   l_cr_amount := nvl(l_accounted_dr_tab(pkt_rec),0) - nvl(l_accounted_cr_tab(pkt_rec),0) ;
5344 			   -- Make sure that signs are correct.
5345 			   --
5346 			   l_cr_amount := nvl(l_cr_amount,0) * -1 ;
5347 
5348                            if l_bdamt_balance <= 0 then
5349                               l_entered_cr_tab(pkt_rec)   := 0 ;
5350                               l_accounted_cr_tab(pkt_rec) := 0 ;
5351                            elsif l_cr_amount <= l_bdamt_balance then
5352 
5353                               l_bdamt_balance            := l_bdamt_balance - l_cr_amount ;
5354 			      l_entered_cr_tab(pkt_rec)  := l_cr_amount ;
5355 			      l_accounted_cr_tab(pkt_rec):= l_cr_amount ;
5356 
5357                            elsif l_cr_amount > l_bdamt_balance then
5358 
5359                               l_entered_cr_tab(pkt_rec)   := l_bdamt_balance ;
5360                               l_accounted_cr_tab(pkt_rec) := l_bdamt_balance;
5361                               l_bdamt_balance             := 0 ;
5362                            end if ;
5363 
5364 		           if l_cr_amount = 0 and l_bdamt_balance > 0 THEN
5365 
5366 			      l_entered_cr_tab(pkt_rec)     := l_bdamt_balance ;
5367 			      l_accounted_cr_tab(pkt_rec)   := l_bdamt_balance;
5368 			      l_bdamt_balance               := 0 ;
5369 
5370 		           end if ;
5371                        end loop ;
5372 
5373 	               --
5374 	               -- BUG 3703180
5375 		       -- Update the burden cost to pa bc packets.
5376 		       --
5377                        FORALL i IN l_bc_packet_id_tab.first .. l_bc_packet_id_tab.last
5378 			  update pa_bc_packets
5379 			     set entered_cr   = l_entered_cr_tab(i),
5380 			         accounted_cr = l_accounted_cr_tab(i),
5381 				 entered_dr   = 0,
5382 				 accounted_dr = 0
5383 			  where packet_id    = p_packet_id
5384 			    and bc_packet_id = l_bc_packet_id_tab(i) ;
5385                     END IF ;
5386 
5387 		 end loop ; -- l_exp_type_tab loop
5388 	      end if ; --  l_exp_type_tab.count
5389 
5390 	    end if ;   -- g_burden_type_tab(line_indx)
5391 
5392 	end loop ; --g_project_id_tab.count LOOP
5393     END IF ;       -- g_project_id_tab.count
5394 
5395         IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
5396             pa_funds_control_pkg.log_message(p_msg_token1 => 'update_cwk_po_burden : End ');
5397 	End if;
5398 
5399     COMMIT ;
5400 
5401 END update_cwk_po_burden ;
5402 --
5403 -- 3703180 : end of changes
5404 
5405 
5406 ---------------------------------------------------------------------
5407 -- this api creates the burden lines for the purchase order and
5408 -- supplier invoice lines in pa_bc_packets
5409 ---------------------------------------------------------------------
5410 FUNCTION  create_ap_po_bdn_lines
5411 		(p_packet_id  	  IN  NUMBER,
5412 		 p_bc_packet_id   IN  NUMBER,
5413 		 p_burden_type    IN  VARCHAR2,
5414 		 P_entered_dr     IN  NUMBER,
5415 		 P_entered_cr     IN  NUMBER,
5416 		 P_period_name    IN  VARCHAR2,
5417 		 p_doc_type       IN  VARCHAR2,
5418 		 p_related_link   IN  VARCHAR2,
5419                  p_exp_type       IN  VARCHAR2,
5420 		 p_accounted_dr   IN  NUMBER,
5421 		 p_accounted_cr   IN  NUMBER,
5422 		 p_compiled_multiplier IN NUMBER
5423 		) RETURN boolean is
5424 	PRAGMA AUTONOMOUS_TRANSACTION;
5425 
5426         l_request_id      NUMBER := fnd_global.conc_request_id();
5427         l_program_id      NUMBER := fnd_global.conc_program_id();
5428         l_program_application_id NUMBER := fnd_global.prog_appl_id();
5429         l_update_login    NUMBER := NVL(FND_GLOBAL.login_id,-1);
5430         l_userid          NUMBER := NVL(fnd_global.user_id,-1);
5431 	-- bug : 3717214
5432 	-- Declare variables and cursors to determine if existing multipliers should be used
5433 	--
5434 	l_count            NUMBER  ;
5435 	l_max_packet_id    NUMBER ;
5436 	l_max_packet_id_b  NUMBER ;
5437 	l_amount           NUMBER ;
5438 	l_prev_multiplier  varchar2(1) ;
5439 	l_tab_multiplier   pa_plsql_datatypes.NumTabTyp ;
5440 	l_tab_icc_exp_type pa_plsql_datatypes.char50TabTyp;
5441 	l_doc_header_id       pa_bc_packets.document_header_id%TYPE ;
5442 	l_doc_distribution_id pa_bc_packets.document_distribution_id%TYPE ;
5443 
5444          /* Commented as part of Bug 5406690
5445 	 CURSOR C_ap_parent_reversal_dist IS --Bug 5515095
5446 	 SELECT parent_reversal_id
5447 	   FROM ap_invoice_distributions_all ap
5448 	  WHERE ap.invoice_distribution_id = l_doc_distribution_id
5449 	    AND parent_reversal_id IS NOT NULL;  */
5450 
5451          l_ap_parent_dist_id  ap_invoice_distributions_all.parent_reversal_id%TYPE;
5452 
5453 BEGIN
5454 	PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>
5455 	' burden type for ap po = '||p_burden_type||' - related link -'||p_related_link);
5456 
5457 	-- bug : 3717214
5458 	-- Determine if we need to use existing idc multipliers for PO or REQs
5459 	-- Following scenarios are covered
5460 	-- Requisition cancellations
5461 	-- PO funds reservations ( PO autocreated from REQ )
5462 	-- PO cancellations
5463 	-- PO Credits/Debits ( AP matching to PO approvals )
5464 	-- ==
5465         l_prev_multiplier := 'N' ;
5466        l_tab_multiplier.DELETE ;
5467        l_tab_icc_exp_type.DELETE ;
5468 
5469         pa_funds_control_pkg.log_message(p_msg_token1 =>'p_doc_type :'||p_doc_type);
5470         pa_funds_control_pkg.log_message(p_msg_token1 =>'Use Existing Multiplier init :'||l_prev_multiplier);
5471 	IF p_doc_type in ( 'PO', 'REQ' , 'AP' ) THEN
5472 
5473               select document_header_id ,
5474                      document_distribution_id ,
5475 		     (nvl(entered_dr,0) - NVL(entered_cr,0)) amount
5476 	        into l_doc_header_id,
5477 	             l_doc_distribution_id,
5478 		     l_amount
5479                 from pa_bc_packets
5480                where packet_id     = p_packet_id
5481 	         and bc_packet_id  = p_bc_packet_id ;
5482 
5483 	      IF p_doc_type = 'AP' THEN
5484 
5485                  IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
5486                     pa_funds_control_pkg.log_message(p_msg_token1 =>'Checking if AP distribution is reversal/cancelled distribution') ;
5487    	         END IF ;
5488 
5489                  /* Bug 5406690 : Changed the logic for checking if AP distribution is reversal/cancelled distribution.
5490 		                  The old logic i.e cursor C_ap_parent_reversal_dist was not able to read uncommitted
5491 				  data from the main session. Now the logic is changed to use global plsql tables
5492 				  that are populated from the main session (i.e PROCEDURE CREATE_PROJ_ENCUMBRANCE_EVENTS). */
5493 		 for i in 1 .. g_tab_bc_packet_id.count loop
5494                       if ((g_tab_bc_packet_id(i)=p_bc_packet_id) and (g_tab_parent_reversal_id(i) IS NOT NULL)) then
5495 		         l_doc_distribution_id := g_tab_parent_reversal_id(i);
5496                          l_prev_multiplier     := 'Y' ;
5497 			 IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
5498                            pa_funds_control_pkg.log_message(p_msg_token1 =>'Parent AP distribution id '||l_doc_distribution_id) ;
5499    	                 END IF ;
5500 		      end if;
5501 		 end loop;
5502 
5503                  /* Commented as part of Bug 5406690
5504                  --Bug 5515095: Below code added to fetch burden multiplier from parent transaction
5505 	         OPEN  C_ap_parent_reversal_dist;
5506 		 FETCH C_ap_parent_reversal_dist INTO l_ap_parent_dist_id;
5507 		 IF C_ap_parent_reversal_dist%FOUND THEN
5508 		    l_doc_distribution_id := l_ap_parent_dist_id;
5509                     l_prev_multiplier     := 'Y' ;
5510                     IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
5511                        pa_funds_control_pkg.log_message(p_msg_token1 =>'Parent AP distribution id '||l_doc_distribution_id) ;
5512    	            END IF ;
5513 		 END IF;
5514 		 CLOSE C_ap_parent_reversal_dist; */
5515 
5516               END IF;
5517 
5518 	END IF ;
5519 
5520 	-- Bug 5511059 : For AP, below logic should get fired only if its reversal/cancel scenario (i.e l_prev_multiplier
5521 	-- is set to 'Y' by above if condition) Note : For credit and debit memos l_amount will be negative
5522 
5523         pa_funds_control_pkg.log_message(p_msg_token1 =>'p_doc_type :'||p_doc_type);
5524         pa_funds_control_pkg.log_message(p_msg_token1 =>'l_amount :'||l_amount);
5525         pa_funds_control_pkg.log_message(p_msg_token1 =>'p_related_link :'||p_related_link);
5526         pa_funds_control_pkg.log_message(p_msg_token1 =>'l_prev_multiplier :'||l_prev_multiplier);
5527 
5528 	IF ( (p_doc_type in ( 'PO', 'REQ' ) AND (l_amount < 0 and p_related_link = 'N')) OR
5529 	     (p_doc_type = 'AP' AND (l_prev_multiplier = 'Y' and p_related_link = 'N')))   then
5530 
5531               l_prev_multiplier     := 'Y' ;
5532               pa_funds_control_pkg.log_message(p_msg_token1 =>'Use Existing Multiplier Credit found :'||l_prev_multiplier);
5533 
5534               select NVL(max(pbc.packet_id) ,0)
5535                 into l_max_packet_id
5536                 from pa_bc_commitments pbc
5537                where pbc.document_header_id       = l_doc_header_id
5538                  and pbc.document_distribution_id = l_doc_distribution_id
5539                  and pbc.document_type            = p_doc_type
5540                  and pbc.packet_id                <> p_packet_id ;
5541 
5542               pa_funds_control_pkg.log_message(p_msg_token1 =>'OLD Multiplier: pa_bc_commitments packet id '||l_max_packet_id);
5543 
5544               select max(packet_id)
5545                 into l_max_packet_id_b
5546                 from pa_bc_packets gbc1
5547                where packet_id               <> p_packet_id
5548                  and packet_id                > NVL(l_max_packet_id,0)
5549                  and document_type            = p_doc_type
5550                  and document_header_id       = l_doc_header_id
5551                  and document_distribution_id = l_doc_distribution_id
5552                  and status_code in ( 'A','C') ;
5553 
5554               pa_funds_control_pkg.log_message(p_msg_token1 =>'OLD Multiplier: pa_bc_packets packet id '||l_max_packet_id_b);
5555 	      -- bug : 3717214
5556 	      -- Spool the existing multipliers.
5557               BEGIN
5558 	         IF NVL(l_max_packet_id,0) >= nvl(l_max_packet_id_b,0) THEN
5559                     select compiled_multiplier ,
5560 	                   expenditure_type
5561                       bulk collect into l_tab_multiplier,
5562 	                                l_tab_icc_exp_type
5563                       from pa_bc_commitments
5564                      where packet_id                = l_max_packet_id
5565 		       and document_header_id       = l_doc_header_id
5566 		       and document_distribution_id = l_doc_distribution_id
5567 		       and document_type            = p_doc_type
5568                        -- and parent_bc_packet_id is not NULL ;
5569 		       -- Bug 5514074 : For AP with qty/amount variance there will be multiple RAW records in
5570 		       -- pa_bc_packets/bc commitments with same header_id and distribution_id.In such scenario
5571 		       -- we should pick burden lines against one of the raw line else it will result in burden
5572 		       -- duplication
5573                        and parent_bc_packet_id      IN  ( SELECT bc1.bc_packet_id -- SQL to fetch single raw record
5574                                                             FROM pa_bc_commitments bc1
5575                                                            WHERE bc1.packet_id                = l_max_packet_id
5576 		                                             AND bc1.document_header_id       = l_doc_header_id
5577 		                                             AND bc1.document_distribution_id = l_doc_distribution_id
5578 		                                             AND bc1.document_type            = p_doc_type
5579                                                              AND bc1.parent_bc_packet_id IS NULL
5580 							     AND ROWNUM = 1 );
5581 
5582                      pa_funds_control_pkg.log_message(p_msg_token1 =>'OLD Multiplier: pa_bc_commitments multiplier used');
5583 		 ELSE
5584 
5585                     select compiled_multiplier ,
5586 	                   expenditure_type
5587                       bulk collect into l_tab_multiplier,
5588 	                                l_tab_icc_exp_type
5589                       from pa_bc_packets
5590                      where packet_id                = l_max_packet_id_b
5591 		       and document_header_id       = l_doc_header_id
5592 		       and document_distribution_id = l_doc_distribution_id
5593 		       and document_type            = p_doc_type
5594                        -- and parent_bc_packet_id is not NULL ;
5595 		       -- Bug 5514074 : For AP with qty/amount variance there will be multiple RAW records in
5596 		       -- pa_bc_packets/bc commitments with same header_id and distribution_id.In such scenario
5597 		       -- we should pick burden lines against one of the raw line else it will result in burden
5598 		       -- duplication
5599                        and parent_bc_packet_id      IN  ( SELECT bc1.bc_packet_id -- SQL to fetch single raw record
5600                                                             FROM pa_bc_packets bc1
5601                                                            WHERE bc1.packet_id                = l_max_packet_id_b
5602 		                                             AND bc1.document_header_id       = l_doc_header_id
5603 		                                             AND bc1.document_distribution_id = l_doc_distribution_id
5604 		                                             AND bc1.document_type            = p_doc_type
5605                                                              AND bc1.parent_bc_packet_id IS NULL
5606 							     AND ROWNUM = 1 );
5607 
5608                      pa_funds_control_pkg.log_message(p_msg_token1 =>'OLD Multiplier: pa_bc_packets multiplier used');
5609 
5610 		 END IF ;
5611               EXCEPTION
5612 	       when no_data_found then
5613 	            NULL ;
5614 	      END ;
5615 
5616 	      -- bug : 3717214
5617 	      -- Prior to Patchset M compiled multiplier was not stored so we can not use
5618 	      -- previous multiplier for data created prior to patch sets M.
5619 	      --
5620 	      IF l_tab_multiplier.count > 0 THEN
5621 	         IF l_tab_multiplier(1) is NULL THEN
5622 	            l_prev_multiplier := 'N' ;
5623                     pa_funds_control_pkg.log_message(p_msg_token1 =>'OLD Multiplier: Prior to PAM Data');
5624                     pa_funds_control_pkg.log_message(p_msg_token1 =>'OLD Multiplier: Calculate Multiplier');
5625 	         END IF ; -- l_tab_multiplier(1) is null
5626 	      END IF ;    -- l_tab_multiplier.count
5627 	END IF; -- IF ( (p_doc_type in ( 'PO', 'REQ' ) AND (l_amount < 0 and p_related_link = 'N')) OR ..
5628 
5629 
5630 
5631     -- bug : 3717214
5632     -- Calculate the new multipliers...
5633     --
5634     IF l_prev_multiplier = 'N' THEN
5635        pa_funds_control_pkg.log_message(p_msg_token1 =>'OLD Multiplier: Calculate Multiplier for p_burden_type '||p_burden_type);
5636        IF p_burden_type = 'SAME' THEN
5637 
5638           SELECT  NVL (pa_funds_control_utils.get_fc_compiled_multiplier
5639                     (  pbc.expenditure_organization_id,
5640                        pbc.task_id,
5641                        pbc.expenditure_item_date,
5642                        'C',
5643                        pbc.expenditure_type
5644                       ), 0),
5645                   expenditure_type
5646             BULK COLLECT into l_tab_multiplier ,
5647                               l_tab_icc_exp_type
5648             FROM pa_bc_packets pbc
5649            WHERE pbc.packet_id = p_packet_id
5650              AND pbc.bc_packet_id = p_bc_packet_id
5651              AND pbc.document_type = p_doc_type
5652              AND substr(nvl(pbc.result_code,'X'),1,1) <> 'F'
5653   	     AND NVL (pa_funds_control_utils.get_fc_compiled_multiplier
5654                     (  pbc.expenditure_organization_id,
5655                        pbc.task_id,
5656                        pbc.expenditure_item_date,
5657                        'C',
5658                        pbc.expenditure_type
5659                       ), 0) <> 0;
5660        ELSE
5661           select et.expenditure_type,
5662                  cm.compiled_multiplier
5663            bulk  collect into l_tab_icc_exp_type,
5664                               l_tab_multiplier
5665            FROM
5666                  pa_ind_rate_sch_revisions irsr,
5667                  pa_cost_bases cb,
5668                  pa_expenditure_types et,
5669                  pa_ind_cost_codes icc,
5670                  pa_cost_base_exp_types cbet,
5671                  pa_ind_rate_schedules_all_bg irs,
5672                  pa_ind_compiled_sets ics,
5673                  pa_compiled_multipliers cm,
5674                  pa_bc_packets pbc
5675            WHERE irsr.cost_plus_structure = cbet.cost_plus_structure
5676              AND cb.cost_base             = cbet.cost_base
5677              AND cb.cost_base_type        = cbet.cost_base_type
5678              AND et.expenditure_type      = icc.expenditure_type
5679              AND icc.ind_cost_code        = cm.ind_cost_code
5680              AND cbet.cost_base           = cm.cost_base
5681              AND cbet.cost_base_type      = 'INDIRECT COST'
5682              AND cbet.expenditure_type    = pbc.expenditure_type
5683              AND irs.ind_rate_sch_id      = irsr.ind_rate_sch_id
5684              AND ics.ind_rate_sch_revision_id = irsr.ind_rate_sch_revision_id
5685              AND ics.organization_id      = pbc.expenditure_organization_id
5686              AND ics.ind_compiled_set_id  =
5687 				pa_funds_control_utils.get_fc_compiled_set_id
5688                                           (pbc.task_id,
5689                                           pbc.expenditure_item_date,
5690 					  pbc.expenditure_organization_id,
5691                                           'C'
5692 					  , 'COMPILE_SET_ID'
5693 					  ,pbc.expenditure_type)  -- Added for burdening changes
5694   		   AND ics.cost_base = cb.cost_base -- Added for burdening changes
5695            AND cm.ind_compiled_set_id = ics.ind_compiled_set_id
5696            AND cm.compiled_multiplier <> 0
5697            AND pbc.packet_id = p_packet_id
5698            AND pbc.bc_packet_id  = p_bc_packet_id
5699            AND substr(nvl(pbc.result_code,'X'),1,1) <> 'F';
5700        END IF ; -- p_burden_type = 'SAME'
5701 
5702     END IF ; -- l_prev_multiplier = 'N'
5703 
5704         pa_funds_control_pkg.log_message(p_msg_token1 =>'p_burden_type :'||p_burden_type);
5705         pa_funds_control_pkg.log_message(p_msg_token1 =>'p_related_link :'||p_related_link);
5706 
5707 IF p_burden_type = 'SAME' THEN
5708 		If p_related_link = 'N' then
5709                    IF l_tab_multiplier.count > 0 THEN
5710 		      forall indx in 1..l_tab_multiplier.count
5711                                 INSERT INTO pa_bc_packets
5712                                         ( ---- who columns------
5713                                         request_id,
5714                                         program_id,
5715                                         program_application_id,
5716                                         program_update_date,
5717                                         last_update_date,
5718                                         last_updated_by,
5719                                         created_by,
5720                                         creation_date,
5721                                         last_update_login,
5722                                         ------ main columns-----------
5723                                         packet_id,
5724                                         bc_packet_id,
5725                                         budget_version_id,
5726                                         project_id,
5727                                         task_id,
5728                                         expenditure_type,
5729                                         expenditure_organization_id,
5730                                         expenditure_item_date,
5731                                         set_of_books_id,
5732                                         je_source_name,
5733                                         je_category_name,
5734                                         document_type,
5735                                         document_header_id,
5736                                         document_distribution_id,
5737                                         actual_flag,
5738                                         period_name,
5739                                         period_year,
5740                                         period_num,
5741                                         result_code,
5742                                         status_code,
5743                                         entered_dr,
5744                                         entered_cr,
5745 					accounted_dr,
5746 					accounted_cr,
5747                                         gl_row_number,    --gl_row_bc_packet_row_id
5748                                         balance_posted_flag,
5749                                         funds_process_mode,
5750                                         txn_ccid,
5751                                         parent_bc_packet_id,
5752 					encumbrance_type_id,
5753 					burden_cost_flag,
5754 					org_id,
5755 					gl_date,
5756 					pa_date,
5757 					document_line_id,
5758 					compiled_multiplier,
5759 					reference1,
5760 					reference2,
5761 					reference3,
5762 					exp_item_id,
5763 					bc_event_id,
5764 					budget_line_id,
5765 					vendor_id,
5766 					main_or_backing_code,
5767 					burden_method_code,
5768 					source_event_id,
5769 					ext_bdgt_flag,
5770 					document_distribution_type,
5771 					document_header_id_2,
5772 					proj_encumbrance_type_id
5773                                         )
5774                                 SELECT
5775                                         l_request_id,
5776                                         l_program_id,
5777                                         l_program_application_id,
5778                                         sysdate,
5779                                         sysdate,
5780                                         l_userid,
5781                                         l_userid,
5782                                         sysdate,
5783                                         l_update_login,
5784                                         ------ main columns-----------
5785                                         pbc.packet_id,
5786                                         pa_bc_packets_s.nextval,
5787                                         pbc.budget_version_id,
5788                                         pbc.project_id,
5789                                         pbc.task_id,
5790                                         pbc.expenditure_type,
5791                                         pbc.expenditure_organization_id,
5792                                         pbc.expenditure_item_date,
5793                                         pbc.set_of_books_id,
5794                                         pbc.je_source_name,
5795                                         pbc.je_category_name,
5796                                         pbc.document_type,
5797                                         pbc.document_header_id,
5798                                         pbc.document_distribution_id,
5799                                         pbc.actual_flag,
5800                                         /** pagl period enhancement changes instead of passing pa date
5801                                             pass transaction date to derive the period name
5802                                         --decode(pbc.document_type,'AP',(
5803 					pa_funds_control_pkg1.get_period_name(pa_utils2.get_pa_date
5804 					  (pbc.expenditure_item_date,NULL,pbc.org_id),pbc.set_of_books_id),
5805 						-- ),pbc.period_name),
5806                                         **/
5807                                         /** Bug fix:2905892 As per discussions with Barbara , Dinakar, Prithi
5808                                          *  the period name should be derived
5809                                          * based on the orginal raw line for the burden transactions
5810                                          * so reverting back to changes made earlier
5811                                          *pa_funds_control_pkg1.get_period_name
5812                                          * (pbc.expenditure_item_date,pbc.set_of_books_id),**/
5813                                         pbc.period_name,
5814                                         /** End of bug fix: 2905892 ***/
5815                                         pbc.period_year,
5816                                         pbc.period_num,
5817                                         pbc.result_code,
5818                                         pbc.status_code,
5819 					pa_currency.round_trans_currency_amt(
5820                                         DECODE ( NVL ( pbc.entered_dr, 0 ), 0, 0,
5821                                                 (( NVL (pbc.entered_dr ,0) *
5822                                                 l_tab_multiplier(indx)))),g_acct_currency_code),
5823 					pa_currency.round_trans_currency_amt(
5824                                         DECODE ( NVL ( pbc.entered_cr, 0 ), 0, 0,
5825                                                 (( NVL (pbc.entered_cr ,0) *
5826 						l_tab_multiplier(indx) ))),g_acct_currency_code),
5827 					pa_currency.round_trans_currency_amt(
5828                                         DECODE ( NVL ( pbc.accounted_dr, 0 ), 0, 0,
5829                                                 (( NVL (pbc.accounted_dr ,0) *
5830 						   l_tab_multiplier(indx)))),g_acct_currency_code),
5831 					pa_currency.round_trans_currency_amt(
5832                                         DECODE ( NVL ( pbc.accounted_cr, 0 ), 0, 0,
5833                                                 (( NVL (pbc.accounted_cr ,0) *
5834 						   l_tab_multiplier(indx)))),g_acct_currency_code),
5835                                         NULL,    --gl_row_bc_packet_row_id
5836                                         pbc.balance_posted_flag,
5837                                         pbc.funds_process_mode,
5838                                         pbc.txn_ccid,
5839                                         pbc.bc_packet_id,
5840 					pbc.encumbrance_type_id,
5841 					'O',
5842 					pbc.org_id,
5843 					pbc.gl_date,
5844 					pbc.pa_date,
5845 					pbc.document_line_id,
5846 					l_tab_multiplier(indx)
5847 					,pbc.reference1
5848 					,pbc.reference2
5849 					,pbc.reference3
5850 					,pbc.exp_item_id
5851 					,pbc.bc_event_id
5852 					,pbc.budget_line_id
5853 					,pbc.vendor_id
5854 					,pbc.main_or_backing_code
5855 					,pbc.burden_method_code
5856 					,pbc.source_event_id
5857 					,pbc.ext_bdgt_flag
5858 					,pbc.document_distribution_type
5859 					,pbc.document_header_id_2
5860 					,pbc.proj_encumbrance_type_id
5861                                 FROM pa_bc_packets pbc
5862                                 WHERE  pbc.packet_id = p_packet_id
5863                                 AND pbc.bc_packet_id = p_bc_packet_id
5864                                 AND pbc.document_type = p_doc_type
5865                                 AND substr(nvl(pbc.result_code,'X'),1,1) <> 'F' ;
5866                                 --If sql%Notfound then
5867 				IF SQL%BULK_ROWCOUNT(1) = 0 THEN
5868                                         PA_FUNDS_CONTROL_PKG.log_message
5869 					(p_msg_token1 => 'Transaction failed to populate burden cost',
5870                                           p_msg_token2 => 'bc_packet_id = '||to_char(p_bc_packet_id));
5871 
5872                                         -- Error msg : 'F114 = Transaction Failed to populate burden cost'
5873                                         PA_FUNDS_CONTROL_PKG.result_status_code_update
5874 						(p_packet_id => p_packet_id,
5875                                                 p_bc_packet_id => p_bc_packet_id,
5876                                                 p_result_code   => 'F114',
5877                                                 p_res_result_code => 'F114',
5878                                                 p_res_grp_result_code => 'F114',
5879                                                 p_task_result_code => 'F114',
5880                                                 p_top_task_result_code => 'F114',
5881                                                 p_project_result_code => 'F114',
5882                                                 p_proj_acct_result_code => 'F114');
5883                                 End if;
5884 				IF SQL%BULK_ROWCOUNT(1) > 0 THEN
5885                                 --IF sql%found then
5886                                         PA_FUNDS_CONTROL_PKG.log_message
5887 					(p_msg_token1 =>'No of bdn lines created ='||sql%bulk_rowcount(1)  );
5888                                 End if;
5889 
5890 		   END IF ; -- l_tab_multiplier.count endif
5891 		Elsif p_related_link = 'Y' then
5892                                 INSERT INTO pa_bc_packets
5893                                         ( ---- who columns------
5894                                         request_id,
5895                                         program_id,
5896                                         program_application_id,
5897                                         program_update_date,
5898                                         last_update_date,
5899                                         last_updated_by,
5900                                         created_by,
5901                                         creation_date,
5902                                         last_update_login,
5903                                         ------ main columns-----------
5904                                         packet_id,
5905                                         bc_packet_id,
5906                                         budget_version_id,
5907                                         project_id,
5908                                         task_id,
5909                                         expenditure_type,
5910                                         expenditure_organization_id,
5911                                         expenditure_item_date,
5912                                         set_of_books_id,
5913                                         je_source_name,
5914                                         je_category_name,
5915                                         document_type,
5916                                         document_header_id,
5917                                         document_distribution_id,
5918                                         actual_flag,
5919                                         period_name,
5920                                         period_year,
5921                                         period_num,
5922                                         result_code,
5923                                         status_code,
5924                                         entered_dr,
5925                                         entered_cr,
5926 					accounted_dr,
5927 					accounted_cr,
5928                                         gl_row_number,    --gl_row_bc_packet_row_id
5929                                         balance_posted_flag,
5930                                         funds_process_mode,
5931                                         txn_ccid,
5932                                         parent_bc_packet_id,
5933 					encumbrance_type_id,
5934 					burden_cost_flag,
5935 					org_id,
5936 					gl_date,
5937 					pa_date,
5938 					document_line_id,
5939 					compiled_multiplier,
5940 					reference1,
5941 					reference2,
5942 					reference3,
5943 					exp_item_id,
5944 					bc_event_id,
5945 					budget_line_id,
5946 					vendor_id,
5947 					main_or_backing_code,
5948 					burden_method_code,
5949 					source_event_id,
5950 					ext_bdgt_flag,
5951 					document_distribution_type,
5952 					document_header_id_2,
5953 					proj_encumbrance_type_id)
5954                                 SELECT
5955                                         l_request_id,
5956                                         l_program_id,
5957                                         l_program_application_id,
5958                                         sysdate,
5959                                         sysdate,
5960                                         l_userid,
5961                                         l_userid,
5962                                         sysdate,
5963                                         l_update_login,
5964                                         ------ main columns-----------
5965                                         pbc.packet_id,
5966                                         pa_bc_packets_s.nextval,
5967                                         pbc.budget_version_id,
5968                                         pbc.project_id,
5969                                         pbc.task_id,
5970                                         pbc.expenditure_type,  --- p_exp_type
5971                                         pbc.expenditure_organization_id,
5972                                         pbc.expenditure_item_date,
5973                                         pbc.set_of_books_id,
5974                                         pbc.je_source_name,
5975                                         pbc.je_category_name,
5976                                         pbc.document_type,
5977                                         pbc.document_header_id,
5978                                         pbc.document_distribution_id,
5979                                         pbc.actual_flag,
5980                                         /** pagl period enhancement changes instead of passing pa date
5981                                             pass transaction date to derive the period name
5982                                         pa_funds_control_pkg1.get_period_name(pa_utils2.get_pa_date
5983                                           (pbc.expenditure_item_date,NULL,pbc.org_id),pbc.set_of_books_id),
5984                                         --P_period_name,
5985                                         **/
5986                                         /** Bug fix:2905892 As per discussions with Barbara , Dinakar, Prithi
5987                                          *  the period name should be derived
5988                                          * based on the orginal raw line for the burden transactions
5989                                          * so reverting back to changes made earlier
5990                                          *pa_funds_control_pkg1.get_period_name
5991                                          * (pbc.expenditure_item_date,pbc.set_of_books_id),**/
5992                                         pbc.period_name,
5993                                         /** End of bug fix: 2905892 ***/
5994                                         pbc.period_year,
5995                                         pbc.period_num,
5996                                         pbc.result_code,
5997                                         pbc.status_code,
5998 					/* Incorrect Burden amts Bug fix:
5999                                         pa_currency.round_trans_currency_amt
6000 					(p_entered_cr,g_acct_currency_code),
6001 						  -- amount from pa_bc_commitments (flip the amts)
6002 					pa_currency.round_trans_currency_amt
6003                                         (p_entered_dr,g_acct_currency_code),  -- amount from pa_bc_commitments
6004 					pa_currency.round_trans_currency_amt
6005 					(p_accounted_cr,g_acct_currency_code),-- amount from pa_bc_commitments
6006 					pa_currency.round_trans_currency_amt
6007 					(p_accounted_dr,g_acct_currency_code), -- amount from pa_bc_commitments
6008 					End Of bug fix:  */
6009                                         pa_currency.round_trans_currency_amt
6010                                         (p_entered_dr,g_acct_currency_code),
6011                                         pa_currency.round_trans_currency_amt
6012                                         (p_entered_cr,g_acct_currency_code),
6013                                         pa_currency.round_trans_currency_amt
6014                                         (p_accounted_dr,g_acct_currency_code),
6015                                         pa_currency.round_trans_currency_amt
6016                                         (p_accounted_cr,g_acct_currency_code),
6017                                         NULL,    --gl_row_bc_packet_row_id to be updated later
6018                                         pbc.balance_posted_flag,
6019                                         pbc.funds_process_mode,
6020                                         pbc.txn_ccid,
6021                                         pbc.bc_packet_id,
6022 					pbc.encumbrance_type_id,
6023 					'O',
6024 					pbc.org_id,
6025 					pbc.gl_date,
6026 					pbc.pa_date,
6027 					pbc.document_line_id,
6028 					p_compiled_multiplier,
6029 					pbc.reference1,
6030 					pbc.reference2,
6031 					pbc.reference3,
6032 					pbc.exp_item_id,
6033 					pbc.bc_event_id,
6034 					pbc.budget_line_id,
6035 					pbc.vendor_id,
6036 					pbc.main_or_backing_code,
6037 					pbc.burden_method_code,
6038 					pbc.source_event_id,
6039 					pbc.ext_bdgt_flag,
6040 					pbc.document_distribution_type,
6041 					pbc.document_header_id_2,
6042 					pbc.proj_encumbrance_type_id
6043                                 FROM pa_bc_packets pbc
6044                                 WHERE pbc.packet_id = p_packet_id
6045                                 AND pbc.bc_packet_id = p_bc_packet_id
6046                                 AND pbc.document_type = p_doc_type
6047                                 AND substr(nvl(pbc.result_code,'X'),1,1) <> 'F';
6048                                 If sql%Notfound then
6049                                         PA_FUNDS_CONTROL_PKG.log_message
6050 					  (p_msg_token1 => 'Transaction failed to populate burden cost',
6051                                            p_msg_token2 => 'bc_packet_id = '||to_char(p_bc_packet_id));
6052 
6053                                         -- Error msg : 'F114 = Transaction Failed to populate burden cost'
6054                                         PA_FUNDS_CONTROL_PKG.result_status_code_update
6055 						(p_packet_id => p_packet_id,
6056                                                 p_bc_packet_id => p_bc_packet_id,
6057                                                 p_result_code   => 'F114',
6058                                                 p_res_result_code => 'F114',
6059                                                 p_res_grp_result_code => 'F114',
6060                                                 p_task_result_code => 'F114',
6061                                                 p_top_task_result_code => 'F114',
6062                                                 p_project_result_code => 'F114',
6063                                                 p_proj_acct_result_code => 'F114');
6064                                 End if;
6065                                 IF sql%found then
6066                                         PA_FUNDS_CONTROL_PKG.log_message
6067 					(p_msg_token1 =>'No of bdn lines created ='||sql%rowcount);
6068                                 End if;
6069 
6070 
6071 		End if;
6072 
6073 
6074 	ELSIF p_burden_type = 'DIFFERENT' then
6075 		If p_related_link = 'N' then
6076 		   IF l_tab_multiplier.COUNT > 0 THEN
6077 		      forall indx in 1..l_tab_multiplier.COUNT
6078                                 INSERT INTO pa_bc_packets
6079                                         ( ---- who columns------
6080                                         request_id,
6081                                         program_id,
6082                                         program_application_id,
6083                                         program_update_date,
6084                                         last_update_date,
6085                                         last_updated_by,
6086                                         created_by,
6087                                         creation_date,
6088                                         last_update_login,
6089                                         ------ main columns-----------
6090                                         packet_id,
6091                                         bc_packet_id,
6092                                         budget_version_id,
6093                                         project_id,
6094                                         task_id,
6095                                         expenditure_type,
6096                                         expenditure_organization_id,
6097                                         expenditure_item_date,
6098                                         set_of_books_id,
6099                                         je_source_name,
6100                                         je_category_name,
6101                                         document_type,
6102                                         document_header_id,
6103                                         document_distribution_id,
6104                                         actual_flag,
6105                                         period_name,
6106                                         period_year,
6107                                         period_num,
6108                                         result_code,
6109                                         status_code,
6110                                         entered_dr,
6111                                         entered_cr,
6112 					accounted_dr,
6113 					accounted_cr,
6114                                         gl_row_number,    --gl_row_bc_packet_row_id
6115                                         balance_posted_flag,
6116                                         funds_process_mode,
6117                                         txn_ccid,
6118                                         parent_bc_packet_id,
6119 					encumbrance_type_id,
6120 					burden_cost_flag,
6121 					org_id,
6122 					gl_date,
6123 					pa_date,
6124 					document_line_id,
6125 					compiled_multiplier
6126 					,reference1
6127 					,reference2
6128 					,reference3
6129 					,exp_item_id
6130 					,bc_event_id
6131 					,budget_line_id
6132 					,vendor_id
6133 					,main_or_backing_code
6134 					,burden_method_code
6135 					,source_event_id
6136 					,ext_bdgt_flag
6137 					,document_distribution_type
6138 					,document_header_id_2
6139 					,proj_encumbrance_type_id
6140                                         )
6141                                 SELECT
6142                                         l_request_id,
6143                                         l_program_id,
6144                                         l_program_application_id,
6145                                         sysdate,
6146                                         sysdate,
6147                                         l_userid,
6148                                         l_userid,
6149                                         sysdate,
6150                                         l_update_login,
6151                                         ------ main columns-----------
6152                                         pbc.packet_id,
6153                                         pa_bc_packets_s.nextval,
6154                                         pbc.budget_version_id,
6155                                         pbc.project_id,
6156                                         pbc.task_id,
6157 					l_tab_icc_exp_type(indx),
6158                                         --et.expenditure_type,
6159                                         pbc.expenditure_organization_id,
6160                                         pbc.expenditure_item_date,
6161                                         pbc.set_of_books_id,
6162                                         pbc.je_source_name,
6163                                         pbc.je_category_name,
6164                                         pbc.document_type,
6165                                         pbc.document_header_id,
6166                                         pbc.document_distribution_id,
6167                                         pbc.actual_flag,
6168                                         /** added the pagl enhancement changes pass ei date instead of pa date
6169                                          *  to get glperiod name
6170                                         pa_funds_control_pkg1.get_period_name(pa_utils2.get_pa_date
6171                                           (pbc.expenditure_item_date,NULL,pbc.org_id),pbc.set_of_books_id),
6172                                         --pbc.period_name,
6173                                          */
6174                                         /** Bug fix:2905892 As per discussions with Barbara , Dinakar, Prithi
6175                                          *  the period name should be derived
6176                                          * based on the orginal raw line for the burden transactions
6177                                          * so reverting back to changes made earlier
6178                                          *pa_funds_control_pkg1.get_period_name
6179                                          * (pbc.expenditure_item_date,pbc.set_of_books_id),**/
6180                                         pbc.period_name,
6181                                         /** End of bug fix: 2905892 ***/
6182                                         pbc.period_year,
6183                                         pbc.period_num,
6184                                         pbc.result_code,
6185                                         pbc.status_code,
6186 					pa_currency.round_trans_currency_amt(
6187                                         decode(nvl(pbc.entered_dr,0),0,0,(nvl(pbc.entered_dr,0)*
6188                                                 l_tab_multiplier(indx))),g_acct_currency_code),
6189 					pa_currency.round_trans_currency_amt(
6190                                         decode(nvl(pbc.entered_cr,0),0,0,(nvl(pbc.entered_cr,0)*
6191                                                 l_tab_multiplier(indx))),g_acct_currency_code),
6192 					pa_currency.round_trans_currency_amt(
6193                                         decode(nvl(pbc.accounted_dr,0),0,0,(nvl(pbc.accounted_dr,0)*
6194                                                 l_tab_multiplier(indx))),g_acct_currency_code),
6195 					pa_currency.round_trans_currency_amt(
6196                                         decode(nvl(pbc.accounted_cr,0),0,0,(nvl(pbc.accounted_cr,0)*
6197                                                 l_tab_multiplier(indx))),g_acct_currency_code),
6198                                         NULL,    --gl_row_bc_packet_row_id
6199                                         pbc.balance_posted_flag,
6200                                         pbc.funds_process_mode,
6201                                         pbc.txn_ccid,
6202                                         pbc.bc_packet_id,
6203 					pbc.encumbrance_type_id,
6204 					'O',
6205 					pbc.org_id,
6206 					pbc.gl_date,
6207 					pbc.pa_date,
6208 					pbc.document_line_id,
6209 					--cm.compiled_multiplier
6210 					l_tab_multiplier(indx)
6211 					,pbc.reference1
6212 					,pbc.reference2
6213 					,pbc.reference3
6214 					,pbc.exp_item_id
6215 					,pbc.bc_event_id
6216 					,pbc.budget_line_id
6217 					,pbc.vendor_id
6218 					,pbc.main_or_backing_code
6219 					,pbc.burden_method_code
6220 					,pbc.source_event_id
6221 					,ext_bdgt_flag
6222 					,pbc.document_distribution_type
6223 					,pbc.document_header_id_2
6224 					,pbc.proj_encumbrance_type_id
6225                                 FROM pa_bc_packets pbc
6226                                 WHERE pbc.packet_id = p_packet_id
6227                                 AND pbc.bc_packet_id  = p_bc_packet_id
6228                                 AND substr(nvl(pbc.result_code,'X'),1,1) <> 'F';
6229 
6230 				FOR indx in 1..l_tab_multiplier.count LOOP
6231 				    IF SQL%BULK_ROWCOUNT(indx) = 0 THEN
6232                                         PA_FUNDS_CONTROL_PKG.log_message
6233 					   (p_msg_token1 => 'Transaction failed to populate burden cost',
6234                                            p_msg_token2 => 'bc_packet_id = '||to_char(p_bc_packet_id));
6235 
6236                                         -- Error msg : 'F114 = Transaction Failed to populate burden cost'
6237                                         PA_FUNDS_CONTROL_PKG.result_status_code_update
6238 						(p_packet_id => p_packet_id,
6239                                                 p_bc_packet_id => p_bc_packet_id,
6240                                                 p_result_code   => 'F114',
6241                                                 p_res_result_code => 'F114',
6242                                                 p_res_grp_result_code => 'F114',
6243                                                 p_task_result_code => 'F114',
6244                                                 p_top_task_result_code => 'F114',
6245                                                 p_project_result_code => 'F114',
6246                                                 p_proj_acct_result_code => 'F114');
6247                                     End if;
6248 				    l_count := l_count + SQL%BULK_ROWCOUNT(indx) ;
6249                                 END LOOP ;
6250 
6251                                 IF l_count > 0  then
6252                                         PA_FUNDS_CONTROL_PKG.log_message
6253 					(p_msg_token1 =>'No of bdn lines created ='||l_count);
6254                                 End if;
6255 
6256 
6257 		   END IF ; -- endif for  l_tab_multiplier.COUNT
6258 		Elsif p_related_link = 'Y' then
6259                                 INSERT INTO pa_bc_packets
6260                                         ( ---- who columns------
6261                                         request_id,
6262                                         program_id,
6263                                         program_application_id,
6264                                         program_update_date,
6265                                         last_update_date,
6266                                         last_updated_by,
6267                                         created_by,
6268                                         creation_date,
6269                                         last_update_login,
6270                                         ------ main columns-----------
6271                                         packet_id,
6272                                         bc_packet_id,
6273                                         budget_version_id,
6274                                         project_id,
6275                                         task_id,
6276                                         expenditure_type,
6277                                         expenditure_organization_id,
6278                                         expenditure_item_date,
6279                                         set_of_books_id,
6280                                         je_source_name,
6281                                         je_category_name,
6282                                         document_type,
6283                                         document_header_id,
6284                                         document_distribution_id,
6285                                         actual_flag,
6286                                         period_name,
6287                                         period_year,
6288                                         period_num,
6289                                         result_code,
6290                                         status_code,
6291                                         entered_dr,
6292                                         entered_cr,
6293 					accounted_dr,
6294 					accounted_cr,
6295                                         gl_row_number,    --gl_row_bc_packet_row_id
6296                                         balance_posted_flag,
6297                                         funds_process_mode,
6298                                         txn_ccid,
6299                                         parent_bc_packet_id,
6300 					encumbrance_type_id,
6301 					burden_cost_flag,
6302 					org_id,
6303 					gl_date,
6304 					pa_date,
6305 					document_line_id,
6306 					compiled_multiplier
6307 					,reference1
6308 					,reference2
6309 					,reference3
6310 					,bc_event_id
6311 					,budget_line_id
6312 					,vendor_id
6313 					,main_or_backing_code
6314 					,burden_method_code
6315 					,source_event_id
6316 					,ext_bdgt_flag
6317 					,document_distribution_type
6318 					,document_header_id_2
6319 					,proj_encumbrance_type_id
6320                                         )
6321                                 SELECT
6322                                         l_request_id,
6323                                         l_program_id,
6324                                         l_program_application_id,
6325                                         sysdate,
6326                                         sysdate,
6327                                         l_userid,
6328                                         l_userid,
6329                                         sysdate,
6330                                         l_update_login,
6331                                         ------ main columns-----------
6332                                         pbc.packet_id,
6333                                         pa_bc_packets_s.nextval,
6334                                         pbc.budget_version_id,
6335                                         pbc.project_id,
6336                                         pbc.task_id,
6337 					/* Bug fix:3026988
6338 					 --when REQ becomes PO, the Exp type for reversing REQ is getting
6339 					 --exp type from raw line instead of icc.cost_codes exp type from original
6340                                          --burden line
6341                                         --pbc.expenditure_type,
6342 					-- to be confirmed with sandeep to consider the old exp type
6343 					*/
6344 					p_exp_type,
6345 					/* end of bug fix:3026988 */
6346                                         pbc.expenditure_organization_id,
6347                                         pbc.expenditure_item_date,
6348                                         pbc.set_of_books_id,
6349                                         pbc.je_source_name,
6350                                         pbc.je_category_name,
6351                                         pbc.document_type,
6352                                         pbc.document_header_id,
6353                                         pbc.document_distribution_id,
6354                                         pbc.actual_flag,
6355                                         /** pagl period enhancement changes instead of passing pa date
6356                                             pass transaction date to derive the period name
6357                                         pa_funds_control_pkg1.get_period_name(pa_utils2.get_pa_date
6358                                           (pbc.expenditure_item_date,NULL,pbc.org_id),pbc.set_of_books_id),
6359                                         **/
6360                                         /** Bug fix:2905892 As per discussions with Barbara , Dinakar, Prithi
6361                                          *  the period name should be derived
6362                                          * based on the orginal raw line for the burden transactions
6363                                          * so reverting back to changes made earlier
6364                                          *pa_funds_control_pkg1.get_period_name
6365                                          * (pbc.expenditure_item_date,pbc.set_of_books_id),**/
6366                                         pbc.period_name,
6367                                         /** End of bug fix: 2905892 ***/
6368                                         pbc.period_year,
6369                                         pbc.period_num,
6370                                         pbc.result_code,
6371                                         pbc.status_code,
6372 					/* Incorrect Burden amts Bug fix:
6373                                         pa_currency.round_trans_currency_amt
6374 					(p_entered_cr,g_acct_currency_code),
6375 					  -- amount from pa_bc_commitments (flip amts)
6376                                         pa_currency.round_trans_currency_amt
6377 					(p_entered_dr,g_acct_currency_code),  -- amount from pa_bc_commitments
6378 					pa_currency.round_trans_currency_amt
6379 					(p_accounted_cr,g_acct_currency_code), -- amount from pa_bc_commitments
6380 					pa_currency.round_trans_currency_amt
6381 					(p_accounted_dr,g_acct_currency_code), -- amount from pa_bc_commitments
6382 					End of Bug fix:  */
6383                                         pa_currency.round_trans_currency_amt
6384                                         (p_entered_dr,g_acct_currency_code),
6385                                         pa_currency.round_trans_currency_amt
6386                                         (p_entered_cr,g_acct_currency_code),
6387                                         pa_currency.round_trans_currency_amt
6388                                         (p_accounted_dr,g_acct_currency_code),
6389                                         pa_currency.round_trans_currency_amt
6390                                         (p_accounted_cr,g_acct_currency_code),
6391                                         NULL,    --gl_row_bc_packet_row_id to be updated later
6392                                         pbc.balance_posted_flag,
6393                                         pbc.funds_process_mode,
6394                                         pbc.txn_ccid,
6395                                         pbc.bc_packet_id,
6396 					pbc.encumbrance_type_id,
6397 					'O',
6398 					pbc.org_id,
6399 					pbc.gl_date,
6400 					pbc.pa_date,
6401 					pbc.document_line_id,
6402 					p_compiled_multiplier,
6403 					pbc.reference1,
6404 					pbc.reference2,
6405 					pbc.reference3,
6406 					pbc.bc_event_id,
6407 					pbc.budget_line_id,
6408 					pbc.vendor_id,
6409 					pbc.main_or_backing_code,
6410 					pbc.burden_method_code,
6411 					pbc.source_event_id,
6412 					ext_bdgt_flag,
6413 					pbc.document_distribution_type,
6414 					pbc.document_header_id_2,
6415 					pbc.proj_encumbrance_type_id
6416                                 FROM pa_bc_packets pbc
6417                                 WHERE  pbc.packet_id = p_packet_id
6418                                 AND pbc.bc_packet_id = p_bc_packet_id
6419                                 AND pbc.document_type = p_doc_type
6420                                 AND substr(nvl(pbc.result_code,'X'),1,1) <> 'F';
6421                                 If sql%Notfound then
6422                                         PA_FUNDS_CONTROL_PKG.log_message
6423 					  (p_msg_token1 => 'Transaction failed to populate burden cost',
6424                                            p_msg_token2 => 'bc_packet_id = '||to_char(p_bc_packet_id));
6425 
6426                                         -- Error msg : 'F114 = Transaction Failed to populate burden cost'
6427                                         PA_FUNDS_CONTROL_PKG.result_status_code_update
6428 						(p_packet_id => p_packet_id,
6429                                                 p_bc_packet_id => p_bc_packet_id,
6430                                                 p_result_code   => 'F114',
6431                                                 p_res_result_code => 'F114',
6432                                                 p_res_grp_result_code => 'F114',
6433                                                 p_task_result_code => 'F114',
6434                                                 p_top_task_result_code => 'F114',
6435                                                 p_project_result_code => 'F114',
6436                                                 p_proj_acct_result_code => 'F114');
6437                                 End if;
6438                                 IF sql%found then
6439                                         PA_FUNDS_CONTROL_PKG.log_message
6440 					(p_msg_token1 =>'No of bdn lines created ='||sql%rowcount);
6441                                 End if;
6442 
6443 
6444 
6445 		End if;
6446 	END IF;
6447 	commit;  -- to end an active autonmous transaction
6448 	Return True;
6449 EXCEPTION
6450 	WHEN OTHERS THEN
6451                 PA_FUNDS_CONTROL_PKG.log_message
6452 			(p_msg_token1 => 'failed in create ap po bdn lines api SQLERR :'||sqlcode||sqlerrm);
6453 		commit;
6454 		RAISE;
6455 
6456 END create_ap_po_bdn_lines;
6457 
6458 ------------------------------------------------------------------------------------------------------------------
6459 --This  Api insert new records into pa bc packets if the project type is burdened.If the PO is based on REQ, or
6460 --Invoice is based on PO  then  it takes the burden amount for the REQ or PO from pa_bc_commitments table
6461 --and ensures that for  req or po the old burden amount is used when reversing lines are passed in gl_bc_packets
6462 ------------------------------------------------------------------------------------------------------------------
6463 PROCEDURE   Populate_burden_cost
6464  	(p_packet_id     	IN NUMBER
6465 	,p_calling_module	IN  VARCHAR2
6466  	,x_return_status	OUT  NOCOPY VARCHAR2
6467  	,x_err_msg_code  	OUT  NOCOPY VARCHAR2
6468  	)  IS
6469 
6470 	PRAGMA AUTONOMOUS_TRANSACTION;
6471 
6472  	l_burden_method     	VARCHAR2(20) := 'NONE';
6473  	l_bc_packet_id         	NUMBER;
6474 	l_parent_bc_packet_id	NUMBER;
6475  	l_doc_type        		VARCHAR2(30);
6476         l_request_id      NUMBER := fnd_global.conc_request_id();
6477         l_program_id      NUMBER := fnd_global.conc_program_id();
6478         l_program_application_id NUMBER:= fnd_global.prog_appl_id();
6479         l_update_login    NUMBER := NVL(FND_GLOBAL.login_id,-1);
6480         l_num_rows        NUMBER := 0;
6481 	l_userid	  NUMBER := NVL(fnd_global.user_id,-1);
6482 	l_exp_type	  VARCHAR2(100);
6483 	l_expenditure_type  VARCHAR2(100);
6484 	l_entered_dr	  NUMBER;
6485 	l_entered_cr	  NUMBER;
6486         l_accounted_dr    NUMBER;
6487         l_accounted_cr    NUMBER;
6488         l_compiled_multiplier NUMBER;
6489 	l_period_name	  VARCHAR2(30);
6490 	l_req_id	  NUMBER;
6491 	l_related_link    VARCHAR2(1);
6492 	l_req_header_id   NUMBER;
6493 	l_po_header_id    NUMBER;
6494 	l_status_flag     VARCHAR2(1)  := 'N';
6495 	l_commitment_rows_flag  VARCHAR2(10) := 'N';
6496 	l_pkt_rows_flag   VARCHAR2(10) := 'N';
6497 	l_doc_header_id	  PA_BC_PACKETS.document_header_id%type;
6498 	l_doc_distribution_id PA_BC_PACKETS.document_header_id%type;
6499 	l_task_id	  NUMBER;
6500 	l_ei_date	  date;
6501 	l_base		  VARCHAR2(100);
6502 	l_cp_structure	  VARCHAR2(100);
6503 	l_return_status   VARCHAR2(100);
6504 	l_err_msg_code    VARCHAR2(100);
6505 	l_debug_mode	  VARCHAR2(10);
6506 
6507 	/*** Bug Fix : 1904319 added this for burden proportional calculations
6508 	 *   cursor po_amount and pkt_po_amount is modified to calculate burden cost
6509 	 *   if the invoice line is partially matched to purchase order
6510          **/
6511         -- this cursor picks up the burden amount and details from pa_bc_commitments table
6512         -- for the given  distribution id ,document type and document header id
6513         CURSOR pkt_po_amount(l_req_id        NUMBER,
6514                          l_bc_packet_id  NUMBER,
6515                          l_po_header_id  NUMBER,
6516                          l_comm_doc_type VARCHAR2) is
6517         SELECT pktburd.period_name,
6518 		/* Commenting as part of 13911609
6519               nvl(pkttrx.entered_dr,0) * decode(nvl(pkttrx.entered_dr,0),0,0,
6520                                                           get_ratio(pkttrx.document_header_id,
6521                                                           pkttrx.document_distribution_id,
6522                                                           pkttrx.document_type,
6523                                                           'BCCMT',
6524                                                           'E')) entered_dr,
6525               nvl(pkttrx.entered_cr,0) * decode(nvl(pkttrx.entered_cr,0),0,0,
6526                                                           get_ratio(pkttrx.document_header_id,
6527                                                           pkttrx.document_distribution_id,
6528                                                           pkttrx.document_type,
6529                                                           'BCCMT',
6530                                                           'E')) entered_cr,
6531               nvl(pkttrx.accounted_dr,0) * decode(nvl(pkttrx.accounted_dr,0),0,0,
6532                                                           get_ratio(pkttrx.document_header_id,
6533                                                           pkttrx.document_distribution_id,
6534                                                           pkttrx.document_type,
6535                                                           'BCCMT',
6536                                                           'A')) accounted_dr,
6537               nvl(pkttrx.accounted_cr,0) * decode(nvl(pkttrx.accounted_cr,0),0,0,
6538                                                 get_ratio(pkttrx.document_header_id,
6539                                                           pkttrx.document_distribution_id,
6540                                                           pkttrx.document_type,
6541                                                           'BCCMT',
6542                                                           'A')) accounted_cr, */
6543                /* Start : Bug 13911609*/
6544                 (ABS(nvl(pktburd.entered_dr,0)-nvl(pktburd.entered_cr,0))/decode(ABS(nvl(pktraw.entered_dr,0) - nvl(pktraw.entered_cr,0)),0,1,ABS(nvl(pktraw.entered_dr,0) - nvl(pktraw.entered_cr,0))))
6545                         * nvl(pkttrx.entered_dr,0) entered_dr,
6546                 (ABS(nvl(pktburd.entered_dr,0)-nvl(pktburd.entered_cr,0))/decode(ABS(nvl(pktraw.entered_dr,0) - nvl(pktraw.entered_cr,0)),0,1,ABS(nvl(pktraw.entered_dr,0) - nvl(pktraw.entered_cr,0))))
6547                         * nvl(pkttrx.entered_cr,0)  entered_cr,
6548                 (ABS(nvl(pktburd.accounted_dr,0)-nvl(pktburd.accounted_cr,0))/decode(ABS(nvl(pktraw.accounted_dr,0) - nvl(pktraw.accounted_cr,0)),0,1,ABS(nvl(pktraw.accounted_dr,0) - nvl(pktraw.accounted_cr,0))))
6549                         * nvl(pkttrx.accounted_dr,0) accounted_dr,
6550                 (ABS(nvl(pktburd.accounted_dr,0)-nvl(pktburd.accounted_cr,0))/decode(ABS(nvl(pktraw.accounted_dr,0) - nvl(pktraw.accounted_cr,0)),0,1,ABS(nvl(pktraw.accounted_dr,0) - nvl(pktraw.accounted_cr,0))))
6551                         * nvl(pkttrx.accounted_cr,0) accounted_cr,
6552                /* End : Bug 13911609*/
6553                 pktburd.expenditure_type,
6554 		pktburd.compiled_multiplier
6555         FROM    pa_bc_commitments_all pktburd
6556                 ,pa_bc_commitments_all pktraw
6557                 ,pa_bc_packets pkttrx
6558         WHERE  pktburd.document_distribution_id = l_req_id
6559         AND    pktburd.document_header_id = l_po_header_id
6560         AND    pktburd.document_type = l_comm_doc_type
6561         AND    pktburd.parent_bc_packet_id is NOT NULL
6562         AND    (pktburd.packet_id ,pktburd.parent_bc_packet_id ) in
6563                 ( SELECT max(comm.packet_id),max(comm.bc_packet_id)
6564                   FROM  pa_bc_commitments comm
6565                   WHERE  comm.document_distribution_id = pktburd.document_distribution_id
6566                   ANd    comm.document_header_id = pktburd.document_header_id
6567                   AND    comm.document_type = pktburd.document_type
6568                   AND    comm.parent_bc_packet_id is NULL
6569                 )
6570         AND   pktburd.packet_id = pktraw.packet_id
6571         AND   pktraw.parent_bc_packet_id is null
6572         AND   pktraw.document_distribution_id = pktburd.document_distribution_id
6573         AND   pktraw.document_header_id = pktburd.document_header_id
6574         AND   pktraw.document_type = pktburd.document_type
6575         AND   pktburd.parent_bc_packet_id = pktraw.bc_packet_id
6576         AND   pkttrx.packet_id = p_packet_id
6577         AND   pkttrx.bc_packet_id = l_bc_packet_id;
6578 
6579 
6580         -- this cursor picks up the burden amount and details from pa_bc_packets table
6581         -- for the given  distribution id ,document type and document header id
6582         -- the transactions which are approved but not yet swept
6583         CURSOR po_amount(l_req_id        NUMBER,
6584                          l_bc_packet_id  NUMBER,
6585                          l_po_header_id  NUMBER,
6586                          l_pkt_doc_type VARCHAR2) is
6587         SELECT pktburd.period_name,
6588 		      /* Commenting as part of 13911609
6589               nvl(pkttrx.entered_dr,0) * decode(nvl(pkttrx.entered_dr,0),0,0,
6590                                                           get_ratio(pkttrx.document_header_id,
6591                                                           pkttrx.document_distribution_id,
6592                                                           pkttrx.document_type,
6593                                                           'BCPKT',
6594                                                           'E')) entered_dr,
6595               nvl(pkttrx.entered_cr,0) * decode(nvl(pkttrx.entered_cr,0),0,0,
6596                                                           get_ratio(pkttrx.document_header_id,
6597                                                           pkttrx.document_distribution_id,
6598                                                           pkttrx.document_type,
6599                                                           'BCPKT',
6600                                                           'E')) entered_cr,
6601               nvl(pkttrx.accounted_dr,0) * decode(nvl(pkttrx.accounted_dr,0),0,0,
6602                                                           get_ratio(pkttrx.document_header_id,
6603                                                           pkttrx.document_distribution_id,
6604                                                           pkttrx.document_type,
6605                                                           'BCPKT',
6606                                                           'A')) accounted_dr,
6607               nvl(pkttrx.accounted_cr,0) * decode(nvl(pkttrx.accounted_cr,0),0,0,
6608                                                 get_ratio(pkttrx.document_header_id,
6609                                                           pkttrx.document_distribution_id,
6610                                                           pkttrx.document_type,
6611                                                           'BCPKT',
6612                                                           'A')) accounted_cr, */
6613              /* Start : Bug 13911609*/
6614 			   (ABS(nvl(pktburd.entered_dr,0)-nvl(pktburd.entered_cr,0))/decode(ABS(nvl(pktraw.entered_dr,0) - nvl(pktraw.entered_cr,0)),0,1,ABS(nvl(pktraw.entered_dr,0) - nvl(pktraw.entered_cr,0))))
6615                         * nvl(pkttrx.entered_dr,0) entered_dr,
6616                 (ABS(nvl(pktburd.entered_dr,0)-nvl(pktburd.entered_cr,0))/decode(ABS(nvl(pktraw.entered_dr,0) - nvl(pktraw.entered_cr,0)),0,1,ABS(nvl(pktraw.entered_dr,0) - nvl(pktraw.entered_cr,0))))
6617                         * nvl(pkttrx.entered_cr,0)  entered_cr,
6618                 (ABS(nvl(pktburd.accounted_dr,0)-nvl(pktburd.accounted_cr,0))/decode(ABS(nvl(pktraw.accounted_dr,0) - nvl(pktraw.accounted_cr,0)),0,1,ABS(nvl(pktraw.accounted_dr,0) - nvl(pktraw.accounted_cr,0))))
6619                         * nvl(pkttrx.accounted_dr,0) accounted_dr,
6620                 (ABS(nvl(pktburd.accounted_dr,0)-nvl(pktburd.accounted_cr,0))/decode(ABS(nvl(pktraw.accounted_dr,0) - nvl(pktraw.accounted_cr,0)),0,1,ABS(nvl(pktraw.accounted_dr,0) - nvl(pktraw.accounted_cr,0))))
6621                         * nvl(pkttrx.accounted_cr,0) accounted_cr,
6622              /* End : Bug 13911609*/
6623                 pktburd.expenditure_type,
6624 		pktburd.compiled_multiplier
6625         FROM    pa_bc_packets pktburd
6626                 ,pa_bc_packets pktraw
6627                 ,pa_bc_packets pkttrx
6628         WHERE  pktburd.document_distribution_id = l_req_id
6629         AND    pktburd.document_header_id = l_po_header_id
6630         AND    pktburd.document_type = l_pkt_doc_type
6631         AND    pktburd.parent_bc_packet_id is NOT NULL
6632         AND    pktburd.balance_posted_flag in ('N')
6633         AND    pktburd.status_code in ('A','C')
6634         AND    substr(nvl(pktburd.result_code,'P'),1,1) = 'P'
6635         AND    (pktburd.packet_id,pktburd.parent_bc_packet_id) in
6636                 ( SELECT MAX(pbc.packet_id),max(bc_packet_id)
6637                   FROM  pa_bc_packets pbc
6638                   WHERE  pbc.document_distribution_id = pktburd.document_distribution_id
6639                   AND    pbc.document_header_id = pktburd.document_header_id
6640                   AND    pbc.document_type = pktburd.document_type
6641                   AND    pbc.parent_bc_packet_id is NULL
6642                   AND    pbc.balance_posted_flag in ('N')
6643                   AND    pbc.status_code in ('A','C')
6644                   AND    substr(nvl(pbc.result_code,'P'),1,1) = 'P'
6645                )
6646         AND   pktburd.packet_id = pktraw.packet_id
6647         AND   pktraw.parent_bc_packet_id is null
6648         AND   pktraw.document_distribution_id = pktburd.document_distribution_id
6649         AND   pktraw.document_header_id = pktburd.document_header_id
6650         AND   pktraw.document_type = pktburd.document_type
6651         and   pktburd.parent_bc_packet_id = pktraw.bc_packet_id
6652         AND   pkttrx.packet_id = p_packet_id
6653         AND   pkttrx.bc_packet_id = l_bc_packet_id;
6654 
6655 
6656 
6657  	CURSOR  burden_type  is
6658  	SELECT decode(NVL(ppt.burden_cost_flag, 'N'),'Y',
6659  			decode(NVL(burden_amt_display_method,'S'), 'S','SAME','D','DIFFERENT'),'NONE'),
6660  		pbc.bc_packet_id ,
6661  		pbc.document_type,
6662 		pbc.parent_bc_packet_id,
6663 		pbc.document_header_id,
6664 		pbc.document_distribution_id ,
6665 		pbc.task_id,
6666 		pbc.expenditure_item_date,
6667 		pbc.expenditure_type
6668  	FROM    pa_project_types  ppt,
6669  		 pa_projects_all  pp,
6670  		 pa_tasks  ptk,
6671  		 pa_bc_packets  pbc
6672  	WHERE
6673  		ppt.project_type = pp.project_type
6674  	AND	pp.project_id  = pbc.project_id
6675  	AND     ptk.project_id = pbc.project_id
6676  	AND   	ptk.task_id     =  pbc.task_id
6677  	AND      pbc.packet_id = p_packet_id
6678 	AND     ((pbc.parent_bc_packet_id is null
6679                   and p_calling_module IN ('GL','CBC','EXPENDITURE'))
6680                  OR (pbc.parent_bc_packet_id = -1 and p_calling_module
6681                    in  ('DISTBTC','GL','TRXIMPORT','DISTERADJ','DISTVIADJ','INTERFACER'
6682 			,'INTERFACVI','TRXNIMPORT','DISTCWKST'))
6683 		)
6684 	AND    pbc.status_code IN ('P', 'I');
6685 
6686 BEGIN
6687  	-- Initialize the error stack
6688  	PA_DEBUG.init_err_stack('PA_FUNDS_CONTROL_PKG1.populate_burden_cost');
6689 
6690         fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
6691         l_debug_mode := NVL(l_debug_mode, 'N');
6692 
6693         PA_DEBUG.SET_PROCESS( x_process => 'PLSQL'
6694                              ,x_write_file     => 'LOG'
6695                             ,x_debug_mode      => l_debug_mode
6696                             );
6697 
6698  	--Initialize the return status to success
6699  	x_return_status :=  'S';
6700 
6701 	pa_funds_control_pkg.log_message(p_msg_token1 => 'Before Calling pa_multi_currency.init');
6702         -- initialize the accounting currency code,
6703         pa_multi_currency.init;
6704 
6705 	pa_funds_control_pkg.log_message(p_msg_token1 => 'Before Calling pa_multi_currency.g_accounting_currency_code');
6706         --Get the accounting currency into a global variable.
6707         g_acct_currency_code := pa_multi_currency.g_accounting_currency_code;
6708 
6709 	pa_funds_control_pkg.log_message(p_msg_token1 => 'Inside Populate burden api');
6710 
6711 	-- Initialize global variables
6712 	init_globals;
6713 
6714 	pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of p_calling_module= '||p_calling_module);
6715 
6716         IF p_calling_module IN ('GL','CBC','EXPENDITURE') then
6717 
6718  	OPEN  burden_type;
6719 	PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'opened cursor');
6720  	LOOP
6721  	     FETCH burden_type  INTO
6722 			l_burden_method
6723 			,l_bc_packet_id
6724 			,l_doc_type
6725 			,l_parent_bc_packet_id
6726 			,l_doc_header_id
6727 			,l_doc_distribution_id
6728 			,l_task_id
6729 			,l_ei_date
6730 			,l_expenditure_type;
6731 	     --dbms_output.put_line('burden type '||l_burden_method);
6732  	     EXIT WHEN burden_type%NOTFOUND;
6733 		-- Check whether the expenditure type is part of burdening if not
6734 		-- then donot call populate burden lines
6735 		-- if the expenditure type is part of burden but fails to populate
6736 		-- burden lines then raise error
6737 		IF l_burden_method in ('SAME','DIFFERENT') then
6738 				check_exp_of_cost_base
6739 				  (p_task_id    	=> l_task_id
6740                                    ,p_exp_type   	=> l_expenditure_type
6741                                    ,p_ei_date    	=> l_ei_date
6742                                    ,p_sch_type   	=> 'C'
6743                                    ,x_base       	=> l_base
6744 				   ,x_cp_structure      => l_cp_structure
6745                                    ,x_return_status 	=> l_return_status
6746                                    ,x_error_msg_code  	=> l_err_msg_code);
6747 
6748 			IF l_base is NULL and l_return_status = 'S' then
6749 				-- expenditure type is not part of the burdening
6750 				-- so raw cost = burdened cose
6751 				l_status_flag := 'N';
6752 				l_related_link := 'N';
6753 				GOTO END_OF_BURDEN;
6754 
6755 			Elsif l_base is NULL and l_return_status <> 'S' then
6756                                 -- expenditure type is  part of the burdening
6757                                 -- but there is error for this expenditure type
6758 				-- either the schedule id is not found or
6759 				-- schedule revision id is null so assign the error
6760 				pa_funds_control_pkg.log_message(p_msg_token2 =>
6761 					'Burden error '||l_err_msg_code);
6762                                 l_status_flag := 'Y';
6763                                 l_related_link := 'Y';
6764                                 GOTO END_OF_BURDEN;
6765 			Elsif l_base is NOT NULL then
6766 				pa_funds_control_pkg.log_message(p_msg_token2 =>
6767 					'cost Base type ='||l_base);
6768                                 l_status_flag := 'N';
6769                                 l_related_link := 'N';
6770 					null;
6771 			END IF;
6772 		END IF;
6773 
6774 	     PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 => 'burden type ['||l_burden_method||
6775 	     ']bc_packet id ['||l_bc_packet_id||']' );
6776 
6777  		IF l_burden_method  = 'SAME'  and  l_bc_packet_id is NOT NULL then
6778 
6779  			IF l_doc_type  IN ( 'CC_C_PAY','CC_P_PAY','CC','CC_C_CO','CC_P_CO' )  THEN
6780 		----------------------------------------------------------------------------------
6781  		--- insert into pa_bc_packets for the burden cost  as a separate bc packet record
6782 		--- after the funds check these records may be inserted into gl_bc_packets
6783  		-----------------------------------------------------------------------------
6784                                 If l_parent_bc_packet_id = -1 then
6785                                         l_related_link := 'Y';
6786                                         OPEN po_amount(l_doc_distribution_id
6787                                                         ,l_bc_packet_id
6788                                                         ,l_doc_header_id
6789 							,l_doc_type);
6790                                         FETCH po_amount into l_period_name
6791                                                 ,l_entered_dr,l_entered_cr
6792                                                 ,l_accounted_dr,l_accounted_cr,l_exp_type,l_compiled_multiplier;
6793                                         If po_amount%NOTFOUND then
6794                                                 OPEN pkt_po_amount(l_doc_distribution_id
6795                                                                    ,l_bc_packet_id
6796                                                                    ,l_doc_header_id
6797 								   ,l_doc_type);
6798                                                 FETCH pkt_po_amount into l_period_name
6799                                                 ,l_entered_dr,l_entered_cr
6800                                                 ,l_accounted_dr,l_accounted_cr,l_exp_type,l_compiled_multiplier;
6801                                                 If pkt_po_amount%found then
6802                                                        PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 => '
6803                                                             cc amount got from bc packets');
6804                                                 End if;
6805                                                 If pkt_po_amount%NOTFOUND then
6806                                                         l_status_flag := 'Y';
6807                                                         GOTO END_OF_BURDEN;
6808                                                 End if;
6809                                                 CLOSE pkt_po_amount;
6810                                         End if;
6811                                           If po_amount%found then
6812                                                 PA_FUNDS_CONTROL_PKG.log_message
6813                                                 (p_msg_token1 => 'cc amount got from commitments');
6814                                           End if;
6815                                         CLOSE po_amount;
6816 
6817                                 Else
6818                                         PA_FUNDS_CONTROL_PKG.log_message
6819                                         (p_msg_token1 => 'Not related link N');
6820                                         l_related_link := 'N';
6821                                 End if;
6822                                 If Not create_ap_po_bdn_lines
6823                                         (p_packet_id      => p_packet_id
6824                                         ,p_bc_packet_id   => l_bc_packet_id
6825                                         ,p_burden_type    => 'SAME'
6826                                         ,P_entered_dr     => l_entered_dr
6827                                         ,P_entered_cr     => l_entered_cr
6828                                         ,P_period_name    => l_period_name
6829                                         ,p_doc_type       => l_doc_type
6830                                         ,p_related_link   => l_related_link
6831                                         ,p_exp_type       => l_exp_type
6832                                         ,p_accounted_dr   => l_accounted_dr
6833                                         ,p_accounted_cr   => l_accounted_cr
6834 					,p_compiled_multiplier => l_compiled_multiplier
6835                                         ) then
6836                                         --RETURN FALSE;
6837                                         NULL;
6838                                 end If;
6839 			ELSIF l_doc_type  =  'REQ' then
6840 
6841 				If l_parent_bc_packet_id = -1 then
6842                                         l_related_link := 'Y';
6843                                         OPEN po_amount(l_doc_distribution_id
6844 							,l_bc_packet_id
6845 							,l_doc_header_id
6846 							,l_doc_type);
6847                                         FETCH po_amount into l_period_name
6848                                                 ,l_entered_dr,l_entered_cr
6849                                                 ,l_accounted_dr,l_accounted_cr,l_exp_type,l_compiled_multiplier;
6850                                         If po_amount%NOTFOUND then
6851                                                 OPEN pkt_po_amount(l_doc_distribution_id
6852 								   ,l_bc_packet_id
6853 								   ,l_doc_header_id
6854 								   ,l_doc_type);
6855                                                 FETCH pkt_po_amount into l_period_name
6856                                                 ,l_entered_dr,l_entered_cr
6857                                                 ,l_accounted_dr,l_accounted_cr,l_exp_type,l_compiled_multiplier;
6858                                                 If pkt_po_amount%found then
6859                                                        PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 => '
6860                                                             burden amount got from bc packets');
6861                                                 End if;
6862                                                 If pkt_po_amount%NOTFOUND then
6863                                                         l_status_flag := 'Y';
6864                                                         GOTO END_OF_BURDEN;
6865                                                 End if;
6866                                                 CLOSE pkt_po_amount;
6867                                         End if;
6868                                           If po_amount%found then
6869                                                 PA_FUNDS_CONTROL_PKG.log_message
6870                                                 (p_msg_token1 => ' amount got from commitments');
6871                                           End if;
6872                                         CLOSE po_amount;
6873 
6874                                 Else
6875                                         PA_FUNDS_CONTROL_PKG.log_message
6876                                         (p_msg_token1 => 'Not related link N');
6877                                         l_related_link := 'N';
6878                                 End if;
6879                                 If Not create_ap_po_bdn_lines
6880                                         (p_packet_id      => p_packet_id
6881                                         ,p_bc_packet_id   => l_bc_packet_id
6882                                         ,p_burden_type    => 'SAME'
6883                                         ,P_entered_dr     => l_entered_dr
6884                                         ,P_entered_cr     => l_entered_cr
6885                                         ,P_period_name    => l_period_name
6886                                         ,p_doc_type       => l_doc_type
6887                                         ,p_related_link   => l_related_link
6888                                         ,p_exp_type       => l_exp_type
6889                                         ,p_accounted_dr   => l_accounted_dr
6890                                         ,p_accounted_cr   => l_accounted_cr
6891 					,p_compiled_multiplier => l_compiled_multiplier
6892                                         ) then
6893                                         --RETURN FALSE;
6894                                         NULL;
6895                                 end If;
6896 
6897 
6898 	----------------------------------------------------------------------------------------------------
6899 	-- if the document is po and this po is based on Requistions then take the burden amount
6900 	-- for the requisition from the pa_bc_commitment table for the given po_distribution_id
6901 	-- if the burden lines not found in commitment table then look at pa bc packet table where
6902 	-- status code is approved and balance posted flag is  N ie is not yet swept if not found then
6903 	-- error out the transaction
6904 	----------------------------------------------------------------------------------------------------------
6905 			Elsif  l_doc_type = 'PO' then
6906                                 If l_parent_bc_packet_id = -1  THEN
6907                                         l_related_link := 'Y';
6908                                         OPEN po_amount( l_doc_distribution_id
6909 							,l_bc_packet_id
6910 							,l_doc_header_id
6911 						        ,l_doc_type);
6912                                         FETCH po_amount into l_period_name,l_entered_dr,l_entered_cr,
6913                                                        l_accounted_dr,l_accounted_cr,l_exp_type,l_compiled_multiplier;
6914                                         If po_amount%found then
6915                                            PA_FUNDS_CONTROL_PKG.log_message
6916                                                 (p_msg_token1 => 'amounts got from commitments ');
6917                                         End if;
6918                                         If po_amount%NOTFOUND then
6919                                                 OPEN pkt_po_amount(l_doc_distribution_id
6920                                                         	  ,l_bc_packet_id
6921                                                         	  ,l_doc_header_id
6922 								  ,l_doc_type);
6923                                                 FETCH pkt_po_amount into l_period_name,l_entered_dr,l_entered_cr,
6924                                                        l_accounted_dr,l_accounted_cr,l_exp_type,l_compiled_multiplier;
6925                                                 If pkt_po_amount%found then
6926                                                 PA_FUNDS_CONTROL_PKG.log_message
6927                                                 (p_msg_token1 => 'amounts got from bc packets ');
6928                                                 End if;
6929                                                 If pkt_po_amount%NOTFOUND then
6930                                                         l_status_flag := 'Y';
6931                                                         GOTO END_OF_BURDEN;
6932                                                 End if;
6933                                                 CLOSE pkt_po_amount;
6934                                         End IF;
6935                                         CLOSE po_amount;
6936                                 Else
6937                                         PA_FUNDS_CONTROL_PKG.log_message
6938                                         (p_msg_token1 => 'Not related link N');
6939                                         l_related_link := 'N';
6940                                 End if;
6941 				If Not create_ap_po_bdn_lines
6942                 			(p_packet_id      => p_packet_id
6943                  		 	,p_bc_packet_id   => l_bc_packet_id
6944                  			,p_burden_type    => 'SAME'
6945                  			,P_entered_dr     => l_entered_dr
6946                  			,P_entered_cr     => l_entered_cr
6947                  			,P_period_name    => l_period_name
6948                  			,p_doc_type       => l_doc_type
6949                  			,p_related_link   => l_related_link
6950                                         ,p_exp_type       => l_exp_type
6951 					,p_accounted_dr   => l_accounted_dr
6952 					,p_accounted_cr   => l_accounted_cr
6953 					,p_compiled_multiplier => l_compiled_multiplier
6954                 			) then
6955 					--RETURN FALSE;
6956 					NULL;
6957 				end If;
6958 
6959 			Elsif  l_doc_type = 'AP' then
6960                                         l_related_link := 'N';
6961                                 If Not create_ap_po_bdn_lines
6962                                         (p_packet_id      => p_packet_id
6963                                         ,p_bc_packet_id   => l_bc_packet_id
6964                                         ,p_burden_type    => 'SAME'
6965                                         ,P_entered_dr     => l_entered_dr
6966                                         ,P_entered_cr     => l_entered_cr
6967                                         ,P_period_name    => l_period_name
6968                                         ,p_doc_type       => l_doc_type
6969                                         ,p_related_link   => l_related_link
6970                                         ,p_exp_type       => l_exp_type
6971 					,p_accounted_dr   => l_accounted_dr
6972 					,p_accounted_cr   => l_accounted_cr
6973 					,p_compiled_multiplier => l_compiled_multiplier
6974                                         ) then
6975                                         --RETURN FALSE;
6976 					NULL;
6977                                 end If;
6978 
6979 
6980  			END IF;
6981 
6982  		ELSIF l_burden_method  = 'DIFFERENT' and  l_bc_packet_id is NOT NULL then
6983  			IF l_doc_type  IN ( 'CC_C_PAY','CC_P_PAY','CC','CC_C_CO','CC_P_CO' )  THEN
6984                                 If l_parent_bc_packet_id = -1 then
6985                                         l_related_link := 'Y';
6986                                         l_commitment_rows_flag := 'N';
6987                                         l_pkt_rows_flag  := 'N';
6988                                         -- open the cursor check whether the records exits  if so
6989                                         -- update the flag to y else check at the pa bc packet cursor
6990                                         -- if no rows found at pabc packets then mark the transaction
6991                                         -- as failed
6992                                         OPEN po_amount(l_doc_distribution_id
6993                                                         ,l_bc_packet_id
6994                                                         ,l_doc_header_id
6995 							,l_doc_type);
6996                                         FETCH po_amount into l_period_name,l_entered_dr,l_entered_cr
6997                                                 ,l_accounted_dr,l_accounted_cr,l_exp_type,l_compiled_multiplier;
6998                                         If po_amount%FOUND then
6999                                                 l_commitment_rows_flag  := 'Y';
7000                                         End if;
7001                                         CLOSE po_amount;
7002 
7003                                         If l_commitment_rows_flag  = 'N' then
7004                                            OPEN pkt_po_amount(l_doc_distribution_id
7005                                                                 ,l_bc_packet_id
7006                                                                 ,l_doc_header_id
7007 								,l_doc_type);
7008                                            FETCH pkt_po_amount into l_period_name,l_entered_dr,l_entered_cr
7009                                                 ,l_accounted_dr,l_accounted_cr,l_exp_type,l_compiled_multiplier;
7010                                             If pkt_po_amount%FOUND then
7011                                                   l_pkt_rows_flag := 'Y';
7012                                             Else
7013                                                   l_status_flag := 'Y';
7014                                                   GOTO END_OF_BURDEN;
7015                                             End if;
7016                                             CLOSE pkt_po_amount;
7017                                         End if;
7018 
7019                                         If l_commitment_rows_flag  = 'Y' then
7020                                            OPEN po_amount(l_doc_distribution_id
7021                                                         ,l_bc_packet_id
7022                                                         ,l_doc_header_id
7023 							,l_doc_type);
7024                                            LOOP
7025                                                 FETCH po_amount into l_period_name
7026                                                         ,l_entered_dr,l_entered_cr
7027                                                         ,l_accounted_dr,l_accounted_cr,l_exp_type,l_compiled_multiplier;
7028                                                 EXIT when po_amount%NOTFOUND;
7029                                                 PA_FUNDS_CONTROL_PKG.log_message
7030                                                 (p_msg_token1 => 'amount found from commitment');
7031                                                 If Not create_ap_po_bdn_lines
7032                                                         (p_packet_id      => p_packet_id
7033                                                         ,p_bc_packet_id   => l_bc_packet_id
7034                                                         ,p_burden_type    => 'DIFFERENT'
7035                                                         ,P_entered_dr     => l_entered_dr
7036                                                         ,P_entered_cr     => l_entered_cr
7037                                                         ,P_period_name    => l_period_name
7038                                                         ,p_doc_type       => l_doc_type
7039                                                         ,p_related_link   => l_related_link
7040                                                         ,p_exp_type       => l_exp_type
7041                                                         ,p_accounted_dr   => l_accounted_dr
7042                                                         ,p_accounted_cr   => l_accounted_cr
7043 							,p_compiled_multiplier => l_compiled_multiplier
7044                                                         ) then
7045                                                         --RETURN FALSE;
7046                                                         NULL;
7047                                                 End if;
7048                                             END LOOP;
7049                                             CLOSE po_amount;
7050                                          End if;
7051                                         If l_pkt_rows_flag  = 'Y' then
7052                                            OPEN pkt_po_amount(l_doc_distribution_id
7053                                                                 ,l_bc_packet_id
7054                                                                 ,l_doc_header_id
7055 								,l_doc_type);
7056                                            LOOP
7057                                                 FETCH pkt_po_amount into l_period_name
7058                                                         ,l_entered_dr,l_entered_cr
7059                                                         ,l_accounted_dr,l_accounted_cr,l_exp_type,l_compiled_multiplier;
7060                                                 EXIT when pkt_po_amount%NOTFOUND;
7061                                                 If Not create_ap_po_bdn_lines
7062                                                         (p_packet_id      => p_packet_id
7063                                                         ,p_bc_packet_id   => l_bc_packet_id
7064                                                         ,p_burden_type    => 'DIFFERENT'
7065                                                         ,P_entered_dr     => l_entered_dr
7066                                                         ,P_entered_cr     => l_entered_cr
7067                                                         ,P_period_name    => l_period_name
7068                                                         ,p_doc_type       => l_doc_type
7069                                                         ,p_related_link   => l_related_link
7070                                                         ,p_exp_type       => l_exp_type
7071                                                         ,p_accounted_dr   => l_accounted_dr
7072                                                         ,p_accounted_cr   => l_accounted_cr
7073 							,p_compiled_multiplier => l_compiled_multiplier
7074                                                         ) then
7075                                                         --RETURN FALSE;
7076                                                         NULL;
7077                                                 End if;
7078                                             END LOOP;
7079                                             CLOSE pkt_po_amount;
7080                                          End if;
7081                                 Else
7082                                         PA_FUNDS_CONTROL_PKG.log_message
7083                                         (p_msg_token1 => 'not related link = N ');
7084                                         l_related_link := 'N';
7085 
7086                                 End if;
7087                                 If l_related_link = 'N'  then
7088                                         If Not create_ap_po_bdn_lines
7089                                         (p_packet_id      => p_packet_id
7090                                         ,p_bc_packet_id   => l_bc_packet_id
7091                                         ,p_burden_type    => 'DIFFERENT'
7092                                         ,P_entered_dr     => l_entered_dr
7093                                         ,P_entered_cr     => l_entered_cr
7094                                         ,P_period_name    => l_period_name
7095                                         ,p_doc_type       => l_doc_type
7096                                         ,p_related_link   => l_related_link
7097                                         ,p_exp_type       => l_exp_type
7098                                         ,p_accounted_dr   => l_accounted_dr
7099                                         ,p_accounted_cr   => l_accounted_cr
7100 					,p_compiled_multiplier => l_compiled_multiplier
7101                                         ) then
7102                                         --RETURN FALSE;
7103                                         NULL;
7104                                         end If;
7105                                 End if;
7106 
7107 
7108 
7109 			Elsif  l_doc_type = 'REQ' then
7110                                 If l_parent_bc_packet_id = -1 then
7111                                         l_related_link := 'Y';
7112                                         l_commitment_rows_flag := 'N';
7113                                         l_pkt_rows_flag  := 'N';
7114                                         -- open the cursor check whether the records exits  if so
7115                                         -- update the flag to y else check at the pa bc packet cursor
7116                                         -- if no rows found at pabc packets then mark the transaction
7117                                         -- as failed
7118                                         OPEN po_amount(l_doc_distribution_id
7119 							,l_bc_packet_id
7120 							,l_doc_header_id
7121 							,l_doc_type);
7122                                         FETCH po_amount into l_period_name,l_entered_dr,l_entered_cr
7123                                                 ,l_accounted_dr,l_accounted_cr,l_exp_type,l_compiled_multiplier;
7124                                         If po_amount%FOUND then
7125                                                 l_commitment_rows_flag  := 'Y';
7126                                         End if;
7127                                         CLOSE po_amount;
7128 
7129                                         If l_commitment_rows_flag  = 'N' then
7130                                            OPEN pkt_po_amount(l_doc_distribution_id
7131 								,l_bc_packet_id
7132 								,l_doc_header_id
7133 								,l_doc_type);
7134                                            FETCH pkt_po_amount into l_period_name,l_entered_dr,l_entered_cr
7135                                                 ,l_accounted_dr,l_accounted_cr,l_exp_type,l_compiled_multiplier;
7136                                             If pkt_po_amount%FOUND then
7137                                                   l_pkt_rows_flag := 'Y';
7138                                             Else
7139                                                   l_status_flag := 'Y';
7140                                                   GOTO END_OF_BURDEN;
7141                                             End if;
7142                                             CLOSE pkt_po_amount;
7143                                         End if;
7144 
7145                                         If l_commitment_rows_flag  = 'Y' then
7146                                            OPEN po_amount(l_doc_distribution_id
7147                                                         ,l_bc_packet_id
7148                                                         ,l_doc_header_id
7149 							,l_doc_type);
7150                                            LOOP
7151                                                 FETCH po_amount into l_period_name
7152                                                         ,l_entered_dr,l_entered_cr
7153                                                         ,l_accounted_dr,l_accounted_cr,l_exp_type,l_compiled_multiplier;
7154                                                 EXIT when po_amount%NOTFOUND;
7155                                                 PA_FUNDS_CONTROL_PKG.log_message
7156                                                 (p_msg_token1 => 'amount found from commitment');
7157                                                 If Not create_ap_po_bdn_lines
7158                                                         (p_packet_id      => p_packet_id
7159                                                         ,p_bc_packet_id   => l_bc_packet_id
7160                                                         ,p_burden_type    => 'DIFFERENT'
7161                                                         ,P_entered_dr     => l_entered_dr
7162                                                         ,P_entered_cr     => l_entered_cr
7163                                                         ,P_period_name    => l_period_name
7164                                                         ,p_doc_type       => l_doc_type
7165                                                         ,p_related_link   => l_related_link
7166                                                         ,p_exp_type       => l_exp_type
7167                                                         ,p_accounted_dr   => l_accounted_dr
7168                                                         ,p_accounted_cr   => l_accounted_cr
7169 							,p_compiled_multiplier => l_compiled_multiplier
7170                                                         ) then
7171                                                         --RETURN FALSE;
7172                                                         NULL;
7173                                                 End if;
7174                                             END LOOP;
7175                                             CLOSE po_amount;
7176                                          End if;
7177                                         If l_pkt_rows_flag  = 'Y' then
7178                                            OPEN pkt_po_amount(l_doc_distribution_id
7179                                                                 ,l_bc_packet_id
7180                                                                 ,l_doc_header_id
7181 								,l_doc_type);
7182                                            LOOP
7183                                                 FETCH pkt_po_amount into l_period_name
7184                                                         ,l_entered_dr,l_entered_cr
7185                                                         ,l_accounted_dr,l_accounted_cr,l_exp_type,l_compiled_multiplier;
7186                                                 EXIT when pkt_po_amount%NOTFOUND;
7187                                                 PA_FUNDS_CONTROL_PKG.log_message
7188                                                 (p_msg_token1 => 'amount found from packets');
7189                                                 If Not create_ap_po_bdn_lines
7190                                                         (p_packet_id      => p_packet_id
7191                                                         ,p_bc_packet_id   => l_bc_packet_id
7192                                                         ,p_burden_type    => 'DIFFERENT'
7193                                                         ,P_entered_dr     => l_entered_dr
7194                                                         ,P_entered_cr     => l_entered_cr
7195                                                         ,P_period_name    => l_period_name
7196                                                         ,p_doc_type       => l_doc_type
7197                                                         ,p_related_link   => l_related_link
7198                                                         ,p_exp_type       => l_exp_type
7199                                                         ,p_accounted_dr   => l_accounted_dr
7200                                                         ,p_accounted_cr   => l_accounted_cr
7201 							,p_compiled_multiplier => l_compiled_multiplier
7202                                                         ) then
7203                                                         --RETURN FALSE;
7204                                                         NULL;
7205                                                 End if;
7206                                             END LOOP;
7207                                             CLOSE pkt_po_amount;
7208                                          End if;
7209                                 Else
7210                                         PA_FUNDS_CONTROL_PKG.log_message
7211                                         (p_msg_token1 => ' not related link = N ');
7212                                         l_related_link := 'N';
7213 
7214                                 End if;
7215                                 If l_related_link = 'N'  then
7216                                         If Not create_ap_po_bdn_lines
7217                                         (p_packet_id      => p_packet_id
7218                                         ,p_bc_packet_id   => l_bc_packet_id
7219                                         ,p_burden_type    => 'DIFFERENT'
7220                                         ,P_entered_dr     => l_entered_dr
7221                                         ,P_entered_cr     => l_entered_cr
7222                                         ,P_period_name    => l_period_name
7223                                         ,p_doc_type       => l_doc_type
7224                                         ,p_related_link   => l_related_link
7225                                         ,p_exp_type       => l_exp_type
7226                                         ,p_accounted_dr   => l_accounted_dr
7227                                         ,p_accounted_cr   => l_accounted_cr
7228 					,p_compiled_multiplier => l_compiled_multiplier
7229                                         ) then
7230                                         --RETURN FALSE;
7231                                         NULL;
7232                                         end If;
7233                                 End if;
7234 
7235 
7236 
7237                         Elsif  l_doc_type = 'PO' then
7238                                 If l_parent_bc_packet_id = -1  THEN
7239                                         l_related_link := 'Y';
7240                                         l_commitment_rows_flag := 'N';
7241                                         l_pkt_rows_flag  := 'N';
7242                                         -- open the cursor check whether the records exits  if so
7243                                         -- update the flag to y else check at the pa bc packet cursor
7244                                         -- if no rows found at pabc packets then mark the transaction
7245                                         -- as failed
7246                                         OPEN po_amount(l_doc_distribution_id
7247 						      ,l_bc_packet_id
7248 						      ,l_doc_header_id
7249 						      ,l_doc_type);
7250                                         FETCH po_amount into l_period_name,l_entered_dr,l_entered_cr
7251                                                 ,l_accounted_dr,l_accounted_cr,l_exp_type,l_compiled_multiplier;
7252                                                 If po_amount%FOUND THEN
7253                                                         l_commitment_rows_flag := 'Y';
7254                                                 End if;
7255                                         CLOSE po_amount;
7256                                         If l_commitment_rows_flag = 'N' then
7257                                                 OPEN pkt_po_amount(l_doc_distribution_id
7258                                                       		  ,l_bc_packet_id
7259                                                       		  ,l_doc_header_id
7260 								  ,l_doc_type);
7261                                                 FETCH pkt_po_amount into l_period_name,l_entered_dr,l_entered_cr
7262                                                       ,l_accounted_dr,l_accounted_cr,l_exp_type,l_compiled_multiplier;
7263                                                 If pkt_po_amount%FOUND THEN
7264                                                         l_pkt_rows_flag := 'Y';
7265                                                 Else
7266                                                         l_status_flag := 'Y';
7267                                                         GOTO END_OF_BURDEN;
7268                                                 End if;
7269                                                 CLOSE pkt_po_amount;
7270                                         End if;
7271 
7272 
7273                                         If l_commitment_rows_flag = 'Y' then
7274 
7275                                              OPEN po_amount(l_doc_distribution_id
7276                                                       	   ,l_bc_packet_id
7277                                                       	   ,l_doc_header_id
7278 							   ,l_doc_type);
7279                                              LOOP
7280                                                 FETCH po_amount into l_period_name
7281                                                         ,l_entered_dr,l_entered_cr
7282                                                         ,l_accounted_dr,l_accounted_cr,l_exp_type,l_compiled_multiplier;
7283                                                 EXIT when po_amount%NOTFOUND;
7284                                                 PA_FUNDS_CONTROL_PKG.log_message
7285                                                 (p_msg_token1 => 'amount found from commitments');
7286                                                 If Not create_ap_po_bdn_lines
7287                                                         (p_packet_id      => p_packet_id
7288                                                         ,p_bc_packet_id   => l_bc_packet_id
7289                                                         ,p_burden_type    => 'DIFFERENT'
7290                                                         ,P_entered_dr     => l_entered_dr
7291                                                         ,P_entered_cr     => l_entered_cr
7292                                                         ,P_period_name    => l_period_name
7293                                                         ,p_doc_type       => l_doc_type
7294                                                         ,p_related_link   => l_related_link
7295                                                         ,p_exp_type       => l_exp_type
7296                                                         ,p_accounted_dr   => l_accounted_dr
7297                                                         ,p_accounted_cr   => l_accounted_cr
7298 							,p_compiled_multiplier => l_compiled_multiplier
7299                                                         ) then
7300                                                         NULL;
7301                                                         --RETURN FALSE;
7302                                                 End if;
7303                                              END LOOP;
7304                                              CLOSE po_amount;
7305                                         End if;
7306                                         If l_pkt_rows_flag = 'Y' then
7307 
7308                                              OPEN pkt_po_amount(l_doc_distribution_id
7309                                                       		,l_bc_packet_id
7310                                                       		,l_doc_header_id
7311 								,l_doc_type);
7312                                              LOOP
7313                                                 FETCH pkt_po_amount into l_period_name
7314                                                         ,l_entered_dr,l_entered_cr
7315                                                         ,l_accounted_dr,l_accounted_cr,l_exp_type,l_compiled_multiplier;
7316                                                 EXIT when pkt_po_amount%NOTFOUND;
7317                                                 PA_FUNDS_CONTROL_PKG.log_message
7318                                                 (p_msg_token1 => 'amount found from commitments');
7319                                                 If Not create_ap_po_bdn_lines
7320                                                         (p_packet_id      => p_packet_id
7321                                                         ,p_bc_packet_id   => l_bc_packet_id
7322                                                         ,p_burden_type    => 'DIFFERENT'
7323                                                         ,P_entered_dr     => l_entered_dr
7324                                                         ,P_entered_cr     => l_entered_cr
7325                                                         ,P_period_name    => l_period_name
7326                                                         ,p_doc_type       => l_doc_type
7327                                                         ,p_related_link   => l_related_link
7328                                                         ,p_exp_type       => l_exp_type
7329                                                         ,p_accounted_dr   => l_accounted_dr
7330                                                         ,p_accounted_cr   => l_accounted_cr
7331 							,p_compiled_multiplier => l_compiled_multiplier
7332                                                         ) then
7333                                                         NULL;
7334                                                         --RETURN FALSE;
7335                                                 End if;
7336                                              END LOOP;
7337                                              CLOSE pkt_po_amount;
7338                                         End if;
7339 
7340                                 Else
7341                                          PA_FUNDS_CONTROL_PKG.log_message
7342                                         (p_msg_token1 => ' Not invoice based po ');
7343                                         l_related_link := 'N';
7344 
7345                                 End if;
7346 
7347 				If l_related_link = 'N'  then
7348                                 	If Not create_ap_po_bdn_lines
7349                                         (p_packet_id      => p_packet_id
7350                                         ,p_bc_packet_id   => l_bc_packet_id
7351                                         ,p_burden_type    => 'DIFFERENT'
7352                                         ,P_entered_dr     => l_entered_dr
7353                                         ,P_entered_cr     => l_entered_cr
7354                                         ,P_period_name    => l_period_name
7355                                         ,p_doc_type       => l_doc_type
7356                                         ,p_related_link   => l_related_link
7357                                         ,p_exp_type       => l_exp_type
7358 					,p_accounted_dr   => l_accounted_dr
7359 					,p_accounted_cr   => l_accounted_cr
7360 					,p_compiled_multiplier => l_compiled_multiplier
7361                                         ) then
7362                                         --RETURN FALSE;
7363 					NULL;
7364                                 	end If;
7365 				End if;
7366 
7367                         Elsif  l_doc_type = 'AP' then
7368 				 PA_FUNDS_CONTROL_PKG.log_message
7369 				(p_msg_token1 => ' doc type is ap related  link N');
7370                                    l_related_link := 'N';
7371 				If l_related_link = 'N' then
7372                                 	If Not create_ap_po_bdn_lines
7373                                         (p_packet_id      => p_packet_id
7374                                         ,p_bc_packet_id   => l_bc_packet_id
7375                                         ,p_burden_type    => 'DIFFERENT'
7376                                         ,P_entered_dr     => l_entered_dr
7377                                         ,P_entered_cr     => l_entered_cr
7378                                         ,P_period_name    => l_period_name
7379                                         ,p_doc_type       => l_doc_type
7380                                         ,p_related_link   => l_related_link
7381                                         ,p_exp_type       => l_exp_type
7382 					,p_accounted_dr   => l_accounted_dr
7383 					,p_accounted_cr   => l_accounted_cr
7384 					,p_compiled_multiplier => l_compiled_multiplier
7385                                         ) then
7386                                         --RETURN FALSE;
7387 					NULL;
7388                                 	end If;
7389 				End if;
7390 
7391  			END IF;  --- for document type
7392 
7393  		END IF; --- for  burden method type
7394 
7395 
7396 	   <<END_OF_BURDEN>>
7397 		PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 => 'End of Burden Process');
7398 		-- if the burden rows are not found for the po based on requision
7399 		-- or invoice based on purchase orders then update the transaction
7400 		-- with error status transaction failed to populate burden cost
7401 		If l_status_flag = 'Y' and l_related_link = 'Y' then
7402  			PA_FUNDS_CONTROL_PKG.result_status_code_update
7403 				(p_packet_id => p_packet_id,
7404                                  p_bc_packet_id => l_bc_packet_id,
7405                                  p_result_code   => 'F114',
7406                                  p_res_result_code => 'F114',
7407                                  p_res_grp_result_code => 'F114',
7408                                  p_task_result_code => 'F114',
7409                                  p_top_task_result_code => 'F114',
7410                                  p_project_result_code => 'F114',
7411                                  p_proj_acct_result_code => 'F114');
7412 			l_status_flag := 'N';
7413 			l_related_link := 'N';
7414 			If po_amount%isopen then
7415 				close po_amount;
7416 			end if;
7417 			If pkt_po_amount%isopen then
7418 				close pkt_po_amount;
7419 			End if;
7420 		End if;
7421  	END LOOP;
7422  	CLOSE burden_type;
7423 
7424         ELSIF p_calling_module in ('DISTBTC','TRXNIMPORT','DISTVIADJ','DISTERADJ','TRXIMPORT','DISTCWKST') then
7425         ---------------------------------------------------------------------------------------
7426         -- pick all the records where the parent bc packet id is minus one (-1)
7427         -- and populate the burden lines with document type as EXP
7428         -- check if the burden display method is same or different
7429         -- if the calling module is TRXNIMPORT then burden components should be calculated
7430         -- as the funds check process is called before the creation of cdls and eis
7431         -- if the calling module is DISTERADJ, OR DISTVIADJ then if the bdn is on same line
7432         -- burden amount can be get it from cdl as the funds check process is called after the
7433         -- creation of cdls
7434         ----------------------------------------------------------------------------------------
7435 		trxn_dister_burden_lines
7436           		(p_packet_id  		=> p_packet_id
7437            		 ,p_calling_module  	=>p_calling_module);
7438 
7439 	END IF;
7440 	IF p_calling_module in ('DISTBTC','TRXNIMPORT','DISTVIADJ','DISTERADJ','GL','TRXIMPORT','DISTCWKST') then
7441 		-- update the bc packets set the parent bc packet id to null
7442 		-- after derving the burden components for the raw lines
7443 		UPDATE pa_bc_packets
7444 		SET parent_bc_packet_id = null
7445 		WHERE packet_id = p_packet_id
7446 		AND   parent_bc_packet_id = -1;
7447 	END IF;
7448 
7449 	/* CWK changes for patchset PAM */
7450 	--update the cwk summary record flag info for contingent worker transactions only
7451 	--for the first occurance of the record ie. project, task and po line
7452 	IF p_calling_module = 'GL' Then
7453 
7454                 /* 3703180 */
7455 		IF g_cwk_po_unreserve = 'Y' THEN
7456 		   update_cwk_po_burden(p_packet_id) ;
7457 		END IF ;
7458 
7459 		pa_funds_control_pkg.log_message(p_msg_token1=>'Calling upd_cwk_attributes for Summary record flag');
7460 		pa_funds_control_pkg1.upd_cwk_attributes(
7461 		        p_calling_module  => p_calling_module
7462                         ,p_packet_id      => p_packet_id
7463                         ,p_mode           => 'R'
7464                         ,p_reference      => 'UPD_FLAG'
7465                         ,x_return_status  => x_return_status
7466 			);
7467 		 pa_funds_control_pkg.log_message(p_msg_token1=>'end of upd_cwk_attributes for Summary record flag'||
7468 			'RetSts['||x_return_status||']');
7469 
7470 
7471 	End If;
7472  	--re set the error stack
7473  	PA_DEBUG.reset_err_stack;
7474 	commit; -- to end an active autonmous transaction
7475 	return;
7476 
7477  EXCEPTION
7478  	WHEN OTHERS THEN
7479  		If  burden_type%ISOPEN then
7480  			close burden_type;
7481  		End if;
7482 
7483  		PA_FUNDS_CONTROL_PKG.log_message
7484 		(p_msg_token1 => 'PA_FUNDS_CONTROL.populate_burden_cost UNEXPECTED ERROR');
7485 		--error msg : 'F140 = Funds check failed because of insert burden cost'
7486  		PA_FUNDS_CONTROL_PKG.result_status_code_update
7487 			( p_status_code=> 'T',
7488              	      	p_result_code              => 'F140',
7489              		p_res_result_code          => 'F140',
7490              		p_res_grp_result_code      => 'F140',
7491              		p_task_result_code         => 'F140',
7492              		p_top_task_result_code     => 'F140',
7493              		p_project_result_code        => 'F140',
7494 			p_proj_acct_result_code    => 'F140',
7495              		p_packet_id                => p_packet_id
7496   			);
7497 
7498                         If po_amount%isopen then
7499                                 close po_amount;
7500                         end if;
7501                         If pkt_po_amount%isopen then
7502                                 close pkt_po_amount;
7503                         End if;
7504 
7505  		x_return_status := 'T';
7506                 PA_FUNDS_CONTROL_PKG.log_message
7507 			(p_msg_token1 => 'failed in populate burden cost api SQLERR :'||sqlcode||sqlerrm);
7508 		PA_FUNDS_CONTROL_PKG.log_message
7509 			(p_error_msg => sqlcode ||sqlerrm);
7510 		--commit;
7511  		Raise;
7512 END Populate_burden_cost;
7513 
7514 ----------------------------------------------------------------------------------------
7515 -- This Api gets the Start and End date based on amount type and boundary code
7516 -- set up for the project. funds availability will be checked based on this
7517 -- start and end dates.
7518 -- The following combinations are supported
7519 -- Amount Type       Boundary Code
7520 -- ==============   ==============
7521 -- Project           Project to Date
7522 -- Project           Year to Date
7523 -- Project           Period to Date
7524 -- Year              Year to Date
7525 -- Year              Period to Date
7526 -- Period            Period to Date
7527 -- ========================================
7528 PROCEDURE setup_start_end_date (
7529         p_packet_id                IN       NUMBER,
7530         p_bc_packet_id             IN       NUMBER,
7531         p_project_id               IN       pa_bc_packets.project_id%TYPE,
7532         p_budget_version_id        IN       pa_bc_packets.budget_version_id%TYPE,
7533         p_time_phase_type_code     IN       pa_budget_entry_methods.time_phased_type_code%TYPE,
7534         p_expenditure_item_date    IN       DATE,
7535         p_amount_type              IN       pa_budgetary_control_options.amount_type%TYPE,
7536         p_boundary_code            IN       pa_budgetary_control_options.boundary_code%TYPE,
7537         p_set_of_books_id          IN       pa_bc_packets.set_of_books_id%TYPE,
7538         x_start_date               OUT      NOCOPY DATE,
7539         x_end_date                 OUT      NOCOPY DATE,
7540         x_error_code               OUT      NOCOPY NUMBER,
7541         x_err_buff                 OUT      NOCOPY VARCHAR2,
7542         x_return_status            OUT      NOCOPY VARCHAR2,
7543         x_result_code              OUT      NOCOPY VARCHAR2 ) IS
7544 
7545 
7546         l_project_start_date     DATE;
7547         l_project_end_date       DATE;
7548         l_year_start_date        DATE;
7549         l_year_end_date          DATE;
7550         l_pa_period_start_date   DATE;
7551         l_pa_period_end_date     DATE;
7552         l_gl_period_start_date   DATE;
7553         l_gl_period_end_date     DATE;
7554         l_dr_period_start_date   DATE;
7555         l_dr_period_end_date     DATE;
7556         l_gs_start_date          DATE;
7557         l_gs_end_date            DATE;
7558         l_gb_end_date            DATE;
7559         l_pa_date                DATE;
7560         l_bal_end_date           DATE;
7561 	l_debug_mode		 VARCHAR2(10);
7562 BEGIN
7563 
7564         PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 => 'Inside the setup start and end date api');
7565 
7566         -- Inialize the error stack
7567         PA_DEBUG.init_err_stack('PA_FUNDS_CONTROL_PKG1.setup_start_end_dates');
7568         -- set the return status to success
7569         x_return_status := 'S';
7570 
7571         fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
7572         l_debug_mode := NVL(l_debug_mode, 'N');
7573 
7574         PA_DEBUG.SET_PROCESS( x_process => 'PLSQL'
7575                              ,x_write_file     => 'LOG'
7576                              ,x_debug_mode      => l_debug_mode
7577                              );
7578         PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'amount_type ['||p_amount_type||']boundary_code ['
7579 			||p_boundary_code||'] time_phase_type_code['||p_time_phase_type_code||']' );
7580 
7581         PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'p_set_of_books_id ['||p_set_of_books_id||']p_expenditure_item_date ['
7582 			||p_expenditure_item_date||'] p_budget_version_id['||p_budget_version_id||']' );
7583 
7584 
7585         x_error_code := 0;  -- initialize error code
7586         IF (    p_time_phase_type_code = 'N'
7587                 OR p_amount_type = 'PJTD'
7588                 OR p_boundary_code = 'J' ) THEN
7589                 g_error_stage := 'Project Start and End Date';
7590                 PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'Project Start and End Date');
7591                 SELECT start_date,
7592                         completion_date
7593                 INTO    l_project_start_date,
7594                         l_project_end_date
7595                 FROM pa_projects_all
7596                 WHERE project_id = p_project_id;
7597 
7598 		IF p_time_phase_type_code in ('P','G') and l_project_start_date is NULL then
7599                         SELECT MIN ( start_date )
7600                         INTO l_project_start_date
7601                         FROM pa_bc_balances
7602                         WHERE budget_version_id = p_budget_version_id
7603                         AND project_id = p_project_id
7604                         AND balance_type = 'BGT';
7605 
7606 		End if;
7607 
7608                 IF ( l_project_end_date IS NULL ) THEN
7609                         SELECT MAX ( end_date )
7610                         INTO l_bal_end_date
7611                         FROM pa_bc_balances
7612                         WHERE budget_version_id = p_budget_version_id
7613                         AND project_id = p_project_id
7614                         AND balance_type = 'BGT';
7615 
7616                         IF p_expenditure_item_date  > l_bal_end_date THEN
7617                                 l_project_end_date := p_expenditure_item_date ;
7618                         ELSE
7619                                 l_project_end_date := l_bal_end_date ;
7620                         END IF;
7621                 END IF;
7622         END IF;
7623 
7624         -- get Financial year start and end dates
7625 
7626         IF (    p_amount_type = 'YTD'
7627                 OR p_boundary_code = 'Y' ) THEN
7628                 g_error_stage := 'Year Start and End Date';
7629                 PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'Year Start and End Date');
7630 
7631                 SELECT gps.year_start_date
7632                 INTO l_year_start_date
7633                 FROM gl_period_statuses gps
7634                 WHERE gps.application_id = 101
7635                 AND gps.set_of_books_id = p_set_of_books_id
7636                 AND p_expenditure_item_date BETWEEN gps.start_date AND gps.end_date
7637                 AND gps.adjustment_period_flag = 'N';
7638 
7639 		PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'l_year_start_date is = '||l_year_start_date);
7640                 l_year_end_date := ADD_MONTHS ( l_year_start_date, 12 ) - 1;
7641         END IF;
7642 
7643         -- get period start and end dates
7644         PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'get period start and end dates');
7645 
7646         IF p_time_phase_type_code = 'G' THEN
7647                                     -- FOR GL period
7648                 g_error_stage := 'Time Phase = G';
7649                 PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'Time Phase = G');
7650                 SELECT TRUNC ( gps.start_date ),
7651                         TRUNC ( gps.end_date )
7652                 INTO    l_gl_period_start_date,
7653                         l_gl_period_end_date
7654                 FROM gl_period_statuses gps
7655                 WHERE gps.application_id = 101
7656                 AND gps.set_of_books_id = p_set_of_books_id
7657                 AND p_expenditure_item_date BETWEEN gps.start_date AND gps.end_date
7658                 AND gps.adjustment_period_flag = 'N';
7659 
7660 		PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'l_gl_period_start_date is = '||l_gl_period_start_date);
7661 		PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'l_gl_period_end_date is = '||l_gl_period_end_date);
7662 
7663         ELSIF p_time_phase_type_code = 'P' THEN
7664                                     -- FOR PA period
7665                 g_error_stage := 'Time Phase = P';
7666                 PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'Time Phase =  P');
7667                 SELECT TRUNC ( start_date ),
7668                         TRUNC ( end_date )
7669                 INTO    l_pa_period_start_date,
7670                         l_pa_period_end_date
7671                 FROM pa_periods gpa
7672                 WHERE p_expenditure_item_date BETWEEN gpa.start_date AND gpa.end_date;
7673 
7674 		PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'l_pa_period_start_date is = '||l_pa_period_start_date);
7675 		PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'l_pa_period_end_date is = '||l_pa_period_end_date);
7676 
7677         ELSIF p_time_phase_type_code = 'R' THEN
7678                                    -- FOR DATE RANGE
7679                 g_error_stage := 'Time Phase R';
7680                  PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'Time Phase =  R');
7681                 SELECT TRUNC ( MAX ( start_date ))
7682                 INTO l_dr_period_start_date
7683                 FROM pa_bc_balances
7684                 WHERE project_id = p_project_id
7685                 AND budget_version_id = p_budget_version_id
7686                 AND balance_type = 'BGT'
7687                 AND start_date <= p_expenditure_item_date;
7688 
7689                 SELECT TRUNC ( MIN ( end_date ))
7690                 INTO l_dr_period_end_date
7691                 FROM pa_bc_balances
7692                 WHERE project_id = p_project_id
7693                 AND budget_version_id = p_budget_version_id
7694                 AND balance_type = 'BGT'
7695                 AND end_date >= p_expenditure_item_date;
7696 
7697 		PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'l_dr_period_start_date is = '||l_dr_period_start_date);
7698 		PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'l_dr_period_end_date is = '||l_dr_period_end_date);
7699         END IF;
7700 
7701         -- Find the p_start_date and x_end_date
7702 
7703         IF p_time_phase_type_code = 'N' THEN   -- for no time phase
7704                 IF (    p_amount_type <> 'PJTD'
7705                         OR p_boundary_code <> 'J' ) THEN
7706                         -- Error msg : F123 = Invalid Amount type and Boundary code for No time phase
7707                         PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'Invalid amount type boundary code');
7708 
7709                         x_result_code := 'F123';
7710                         x_return_status := 'F';
7711 			PA_DEBUG.reset_err_stack;
7712                         Return;
7713 
7714                 ELSE
7715                         x_start_date := l_project_start_date;
7716                         x_end_date := l_project_end_date;
7717                 END IF;
7718         ELSIF p_time_phase_type_code IN ( 'P', 'G', 'R' ) THEN
7719                 --Project to Date Start and End Date Calculations
7720                 -- start date calc - PJTD
7721 
7722                 IF p_amount_type = 'PJTD' THEN
7723                         IF p_time_phase_type_code = 'P' THEN
7724                                 g_error_stage := 'PJTD- P';
7725                                 PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>' PJTD- P');
7726                                 BEGIN
7727                                         SELECT TRUNC ( start_date )
7728                                         INTO l_gs_start_date
7729                                         FROM pa_periods gpa
7730                                         WHERE l_project_start_date BETWEEN gpa.start_date AND gpa.end_date;
7731 				PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'l_gs_start_date is = '||l_gs_start_date);
7732 
7733                                 EXCEPTION
7734                                         WHEN NO_DATA_FOUND THEN
7735                                                 SELECT TRUNC ( MIN ( start_date ))
7736                                                 INTO l_gs_start_date
7737                                                 FROM pa_bc_balances
7738                                                 WHERE project_id = p_project_id
7739                                                 AND budget_version_id = p_budget_version_id
7740                                                 AND balance_type = 'BGT';
7741 				PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'l_gs_start_date is = '||l_gs_start_date);
7742                                 END;
7743                         ELSIF p_time_phase_type_code = 'G' THEN
7744                                 g_error_stage := 'PJTD-G';
7745                                 PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'PJTD-G');
7746                                 BEGIN
7747                                         SELECT TRUNC ( gps.start_date )
7748                                         INTO l_gs_start_date
7749                                         FROM gl_period_statuses gps
7750                                         WHERE gps.application_id = 101
7751                                         AND gps.set_of_books_id = p_set_of_books_id
7752                                         AND l_project_start_date BETWEEN gps.start_date AND gps.end_date
7753                                         AND gps.adjustment_period_flag = 'N';
7754 				PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'l_gs_start_date is = '||l_gs_start_date);
7755                                 EXCEPTION
7756                                         WHEN NO_DATA_FOUND THEN
7757                                                 SELECT TRUNC ( MIN ( start_date ))
7758                                                 INTO l_gs_start_date
7759                                                 FROM pa_bc_balances
7760                                                 WHERE project_id = p_project_id
7761                                                 AND budget_version_id = p_budget_version_id
7762                                                 AND balance_type = 'BGT';
7763 				PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'l_gs_start_date is = '||l_gs_start_date);
7764                                 END;
7765                         ELSIF p_time_phase_type_code = 'R' THEN
7766                                 g_error_stage := 'PJTD-R';
7767                                 PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'PJTD-R');
7768                                 SELECT TRUNC ( MIN ( start_date ))
7769                                 INTO l_gs_start_date
7770                                 FROM pa_bc_balances
7771                                 WHERE project_id = p_project_id
7772                                 AND budget_version_id = p_budget_version_id
7773                                 AND balance_type = 'BGT';
7774 				PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'l_gs_start_date is = '||l_gs_start_date);
7775                         END IF;
7776                         IF l_gs_start_date < l_project_start_date THEN
7777                                 x_start_date := l_gs_start_date;
7778                         ELSE
7779                                 x_start_date := l_project_start_date;
7780                         END IF;
7781 
7782                         -- end date calc for PJTD - Project
7783                          PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>' end date calc for PJTD ');
7784 
7785                         IF p_boundary_code = 'J' THEN
7786                                 IF p_time_phase_type_code = 'P' THEN
7787                                         g_error_stage := 'PJTD_J-P';
7788                                         PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'PJTD_J-P');
7789                                         BEGIN
7790                                                 SELECT TRUNC ( end_date )
7791                                                 INTO l_gs_end_date
7792                                                 FROM pa_periods gpa
7793                                                 WHERE l_project_end_date BETWEEN gpa.start_date AND gpa.end_date;
7794 				PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'l_gs_end_date is = '||l_gs_end_date);
7795                                         EXCEPTION
7796                                                 WHEN NO_DATA_FOUND THEN
7797                                                         SELECT TRUNC ( MAX ( end_date ))
7798                                                         INTO l_gs_end_date
7799                                                         FROM pa_bc_balances
7800                                                         WHERE project_id = p_project_id
7801                                                         AND budget_version_id = p_budget_version_id
7802                                                         AND balance_type = 'BGT';
7803 				PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'l_gs_end_date is = '||l_gs_end_date);
7804 					END;
7805                                         IF l_gs_end_date > l_project_end_date THEN
7806                                                 x_end_date := l_gs_end_date;
7807                                         ELSE
7808                                                 x_end_date := l_project_end_date;
7809                                         END IF;
7810                                 ELSIF p_time_phase_type_code = 'G' THEN
7811                                         g_error_stage := 'PJTD_J-G';
7812                                          PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'PJTD_J-G');
7813                                         BEGIN
7814                                                 SELECT TRUNC ( gps.end_date )
7815                                                 INTO l_gs_end_date
7816                                                 FROM gl_period_statuses gps
7817                                                 WHERE gps.application_id = 101
7818                                                 AND gps.set_of_books_id = p_set_of_books_id
7819                                                 AND l_project_end_date BETWEEN gps.start_date AND gps.end_date
7820                                                 AND gps.adjustment_period_flag = 'N';
7821 				PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'l_gs_end_date is = '||l_gs_end_date);
7822                                         EXCEPTION
7823                                                 WHEN NO_DATA_FOUND THEN
7824                                                         SELECT TRUNC ( MAX ( end_date ))
7825                                                         INTO l_gs_end_date
7826                                                         FROM pa_bc_balances
7827                                                         WHERE project_id = p_project_id
7828                                                         AND budget_version_id = p_budget_version_id
7829                                                         AND balance_type = 'BGT';
7830 				PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'l_gs_end_date is = '||l_gs_end_date);
7831                                         END;
7832                                         IF l_gs_end_date > l_project_end_date THEN
7833                                                 x_end_date := l_gs_end_date;
7834                                         ELSE
7835                                                 x_end_date := l_project_end_date;
7836                                         END IF;
7837                                 ELSIF p_time_phase_type_code = 'R' THEN
7838                                         g_error_stage := 'PJTD_J-R';
7839                                          PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'PJTD_J-R');
7840                                         x_end_date := l_project_end_date;
7841                                 END IF;
7842                         -- end date calc for PJTD - Year
7843 
7844                         ELSIF p_boundary_code = 'Y' THEN
7845                                 IF p_time_phase_type_code = 'P' THEN
7846                                         g_error_stage := 'PJTD-Y-P';
7847                                         PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'PJTD-Y-P');
7848                                         BEGIN
7849                                                 SELECT p.end_date
7850                                                 INTO l_gs_end_date
7851                                                 FROM pa_periods p
7852                                                 WHERE l_year_end_date BETWEEN p.start_date AND p.end_date;
7853                                                 x_end_date := l_gs_end_date;
7854 				PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'l_gs_end_date is = '||l_gs_end_date);
7855                                         EXCEPTION
7856                                                 WHEN NO_DATA_FOUND THEN
7857                                                         x_end_date := l_year_end_date;
7858                                         END;
7859                                 ELSIF p_time_phase_type_code = 'G' THEN
7860                                         g_error_stage := 'PJTD-Y-G';
7861                                         PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'PJTD-Y-G');
7862                                         x_end_date := l_year_end_date;
7863                                 ELSIF p_time_phase_type_code = 'R' THEN
7864                                         g_error_stage := 'PJTD-Y-R';
7865                                          PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'PJTD-Y-R');
7866                                         x_end_date := l_year_end_date;
7867                                 END IF;
7868                         -- end date calc for PJTD - period
7869                           PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'end date calc for PJTD - period');
7870 
7871                         ELSIF p_boundary_code = 'P' THEN
7872                                 g_error_stage := 'PJTD-P';
7873                                 PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'PJTD-P');
7874                                 IF p_time_phase_type_code = 'P' THEN
7875                                         x_end_date := l_pa_period_end_date;
7876                                 ELSIF p_time_phase_type_code = 'G' THEN
7877                                         x_end_date := l_gl_period_end_date;
7878                                 ELSIF p_time_phase_type_code = 'R' THEN
7879                                         x_end_date := l_dr_period_end_date;
7880                                 END IF;
7881                         ELSE  -- end date calc for PJTD - period
7882                                 -- error msg : F124 = Invalid Boundary code for Project to Date
7883                                  PA_FUNDS_CONTROL_PKG.log_message
7884                                 (p_msg_token1 =>'Invalid Boundary code for Project to Date');
7885                                 x_result_code := 'F124';
7886                                 x_return_status := 'F';
7887 				PA_DEBUG.reset_err_stack;
7888                                 Return;
7889                                 NULL;
7890                         END IF;
7891                 -- Year to Date - start and End date calculation
7892                 -- start date calc - YTD
7893                  PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'start date calc - YTD');
7894 
7895                 ELSIF p_amount_type = 'YTD' THEN
7896 
7897                         IF p_time_phase_type_code = 'P' THEN
7898                                 g_error_stage := 'YTD1-P';
7899                                 PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'YTD1-P');
7900                                 BEGIN
7901                                         SELECT p.start_date
7902                                         INTO l_gs_start_date
7903                                         FROM pa_periods p
7904                                         WHERE l_year_start_date BETWEEN p.start_date AND p.end_date;
7905                                         x_start_date := l_gs_start_date;
7906                                 EXCEPTION
7907                                         WHEN NO_DATA_FOUND THEN
7908                                                 x_start_date := l_year_start_date;
7909                                 END;
7910                         ELSIF p_time_phase_type_code = 'G' THEN
7911                                 g_error_stage := 'YTD2-G';
7912                                 PA_FUNDS_CONTROL_PKG.log_message
7913                                         (p_msg_token1 =>'YTD2-G');
7914                                 x_start_date := l_year_start_date;
7915                         ELSIF p_time_phase_type_code = 'R' THEN
7916                                 g_error_stage := 'YTD3-R';
7917                                 PA_FUNDS_CONTROL_PKG.log_message
7918                                                 (p_msg_token1 =>'YTD3-R');
7919                                 x_start_date := l_year_start_date;
7920                         END IF;
7921 
7922                         -- end date calc for YTD - year
7923                         PA_FUNDS_CONTROL_PKG.log_message
7924                                         (p_msg_token1 =>'end date calc for YTD - year');
7925                         IF p_boundary_code = 'Y' THEN
7926                                 IF p_time_phase_type_code = 'P' THEN
7927                                         g_error_stage := 'YTD-Y-P';
7928                                         PA_FUNDS_CONTROL_PKG.log_message
7929                                         (p_msg_token1 =>'YTD-Y-P');
7930                                         BEGIN
7931                                                 SELECT p.end_date
7932                                                 INTO l_gs_end_date
7933                                                 FROM pa_periods p
7934                                                 WHERE l_year_end_date BETWEEN p.start_date AND p.end_date;
7935                                                 x_end_date := l_gs_end_date;
7936                                         EXCEPTION
7937                                                 WHEN NO_DATA_FOUND THEN
7938                                                         x_end_date := l_year_end_date;
7939                                         END;
7940                                 ELSIF p_time_phase_type_code = 'G' THEN
7941                                         g_error_stage := 'YTD-Y-G';
7942                                         PA_FUNDS_CONTROL_PKG.log_message
7943                                                 (p_msg_token1 =>'YTD-Y-G');
7944                                         x_end_date := l_year_end_date;
7945                                 ELSIF p_time_phase_type_code = 'R' THEN
7946                                         g_error_stage := 'YTD-Y-R';
7947                                         PA_FUNDS_CONTROL_PKG.log_message
7948                                                 (p_msg_token1 =>'YTD-Y-R');
7949                                         x_end_date := l_year_end_date;
7950                                 END IF;
7951                                 -- end date calc for YTD - period
7952                         ELSIF p_boundary_code = 'P' THEN
7953                                 g_error_stage := 'YTD-P';
7954                                  PA_FUNDS_CONTROL_PKG.log_message
7955                                         (p_msg_token1 => 'YTD-P');
7956                                 IF p_time_phase_type_code = 'P' THEN
7957                                         x_end_date := l_pa_period_end_date;
7958                                 ELSIF p_time_phase_type_code = 'G' THEN
7959                                         x_end_date := l_gl_period_end_date;
7960                                 ELSIF p_time_phase_type_code = 'R' THEN
7961                                         x_end_date := l_dr_period_end_date;
7962                                 END IF;
7963 
7964                         ELSE
7965                                 -- Error msg : F125 = Invalid boundary code for YTD
7966                                  PA_FUNDS_CONTROL_PKG.log_message
7967                                         (p_msg_token1 =>'Invalid boundary code for YTD');
7968                                 x_result_code := 'F125';
7969                                 x_return_status := 'F';
7970 				PA_DEBUG.reset_err_stack;
7971                                 Return;
7972                                 NULL;
7973 
7974                         END IF;
7975                         --For Period to Date Period
7976 
7977                 ELSIF p_amount_type = 'PTD' THEN
7978                         IF p_boundary_code = 'P' THEN
7979                                 g_error_stage := 'PTD-P';
7980                                 PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'PTD-P');
7981                                 IF p_time_phase_type_code = 'P' THEN
7982                                         x_start_date := l_pa_period_start_date;
7983                                         x_end_date := l_pa_period_end_date;
7984                                 ELSIF p_time_phase_type_code = 'G' THEN
7985                                         x_start_date := l_gl_period_start_date;
7986                                         x_end_date := l_gl_period_end_date;
7987                                 ELSIF p_time_phase_type_code = 'R' THEN
7988                                         x_start_date := l_dr_period_start_date;
7989                                         x_end_date := l_dr_period_end_date;
7990                                 END IF;
7991                         ELSE
7992                                 -- error msg : F127 = Invalid boundary code for PTD
7993                                 PA_FUNDS_CONTROL_PKG.log_message
7994                                 (p_msg_token1 =>'Invalid boundary code for PTD');
7995                                 x_result_code := 'F127';
7996                                 x_return_status := 'F';
7997 				PA_DEBUG.reset_err_stack;
7998                                 Return;
7999                                 NULL;
8000                         END IF;
8001                 ELSE
8002                                 -- error msg : F122 = Invalid Amount type boundary code
8003                                 PA_FUNDS_CONTROL_PKG.log_message
8004                                 (p_msg_token1 =>'Invalid Amount type boundary code');
8005                                 x_result_code := 'F122';
8006                                 x_return_status := 'F';
8007 				PA_DEBUG.reset_err_stack;
8008                                 Return;
8009                         NULL;
8010 
8011                 END IF;
8012 
8013         ELSE
8014                  -- error msg : F122 = Invalid Amount type boundary code
8015                 PA_FUNDS_CONTROL_PKG.log_message
8016                 (p_msg_token1 =>'Invalid Amount type boundary code');
8017                 x_result_code := 'F122';
8018                 x_return_status := 'F';
8019 		PA_DEBUG.reset_err_stack;
8020                 Return;
8021                 NULL;
8022         END IF;
8023 
8024         --('After Date Check Process');
8025 
8026         IF    x_start_date IS NULL OR x_end_date IS NULL THEN
8027                 PA_FUNDS_CONTROL_PKG.log_message
8028                 (p_msg_token1 =>'x_start_date IS NULL OR x_end_date IS NULL');
8029                 IF p_time_phase_type_code = 'R' THEN
8030                         --error msg : F129 = Start and end date is null for Date range
8031                         x_result_code     := 'F129';
8032                         x_return_status := 'F';
8033                 ELSIF p_time_phase_type_code = 'G' THEN
8034                         -- Error msg : F134 Start and end date is null for GL period
8035                         x_result_code  := 'F134';
8036                         x_return_status := 'F';
8037                 ELSIF p_time_phase_type_code = 'P' THEN
8038                         -- Error msg : F130 Start and end date is null for PA period
8039                         x_result_code := 'F130';
8040                         x_return_status := 'F';
8041                 END IF;
8042 
8043                 x_error_code := 2;
8044         END IF;
8045         PA_FUNDS_CONTROL_PKG.log_message
8046         (p_msg_token1 =>'End of setup start and end date');
8047 
8048 	PA_DEBUG.reset_err_stack;
8049         Return;
8050 EXCEPTION
8051 
8052         WHEN NO_DATA_FOUND THEN
8053                 PA_FUNDS_CONTROL_PKG.log_message(p_msg_token1 =>'Exception: No Data Found - F136');
8054                 --Error msg : F136 = Funds check failed at Start and End Date Calculations;
8055                 x_result_code := 'F136';
8056 		PA_DEBUG.reset_err_stack;
8057                 Return;
8058 
8059         WHEN OTHERS THEN
8060 		PA_DEBUG.reset_err_stack;
8061                 x_result_code := 'T';
8062          	RAISE;
8063 END setup_start_end_date;
8064 
8065 /** This api returns the start date or end date for the given amount type boundary code
8066  *  conditions, this in turn make calls the setup_start_end_date api
8067  *  if p_type param is START_DATE then this api returns start-date if p_type param is END_DATE
8068  *  then this api returns end date  bug fix :1992734
8069  */
8070 
8071 FUNCTION get_start_or_end_date(
8072         p_packet_id                IN       NUMBER,
8073         p_bc_packet_id             IN       NUMBER,
8074         p_project_id               IN       pa_bc_packets.project_id%TYPE,
8075         p_budget_version_id        IN       pa_bc_packets.budget_version_id%TYPE,
8076         p_time_phase_type_code     IN       pa_budget_entry_methods.time_phased_type_code%TYPE,
8077         p_expenditure_item_date    IN       DATE,
8078         p_amount_type              IN       pa_budgetary_control_options.amount_type%TYPE,
8079         p_boundary_code            IN       pa_budgetary_control_options.boundary_code%TYPE,
8080         p_set_of_books_id          IN       pa_bc_packets.set_of_books_id%TYPE,
8081 	p_type                     IN       varchar2  -- START_DATE or END_DATE OR RESULT_CODE
8082 	) return DATE  is
8083 
8084 	l_start_date       date;
8085 	l_end_date         date;
8086         x_error_code       varchar2(1000);
8087         x_err_buff         varchar2(1000);
8088         x_return_status    varchar2(100);
8089         x_result_code      varchar2(100);
8090 
8091 
8092 BEGIN
8093 
8094 	If  g_sd_project_id  is null or g_sd_project_id <> p_project_id OR
8095 	    g_sd_bdgt_version_id  is null  or g_sd_bdgt_version_id <> p_budget_version_id  OR
8096             g_sd_tm_phase_code    is null or g_sd_tm_phase_code <> p_time_phase_type_code OR
8097             g_sd_amt_type  is  null or g_sd_amt_type <> p_amount_type OR
8098             g_sd_boundary_code  is  null or g_sd_boundary_code <> p_boundary_code OR
8099             g_sd_sob  is null or g_sd_sob <> p_set_of_books_id then
8100 
8101 		pa_funds_control_pkg.log_message(p_msg_token1 => 'Different 1 '||p_type);
8102 		pa_funds_control_pkg.log_message(p_msg_token1 => 'Calling setup_start_end_date');
8103 			setup_start_end_date (
8104         		p_packet_id                => p_packet_id
8105         		,p_bc_packet_id             => p_bc_packet_id
8106         		,p_project_id               => p_project_id
8107         		,p_budget_version_id        => p_budget_version_id
8108         		,p_time_phase_type_code     => p_time_phase_type_code
8109         		,p_expenditure_item_date    => p_expenditure_item_date
8110         		,p_amount_type              => p_amount_type
8111         		,p_boundary_code            => p_boundary_code
8112         		,p_set_of_books_id          => p_set_of_books_id
8113         		,x_start_date               => l_start_date
8114         		,x_end_date                 => l_end_date
8115         		,x_error_code               => x_error_code
8116         		,x_err_buff                 => x_err_buff
8117         		,x_return_status            => x_return_status
8118         		,x_result_code              => x_result_code);
8119 
8120 			g_start_date  := l_start_date;
8121 			g_end_date    := l_end_date;
8122 			g_sd_result_code := x_result_code;
8123 			g_sd_project_id := p_project_id;
8124 			g_sd_bdgt_version_id := p_budget_version_id;
8125 			g_sd_amt_type  := p_amount_type;
8126 			g_sd_boundary_code := p_boundary_code;
8127 			g_sd_sob      := p_set_of_books_id;
8128 			g_sd_tm_phase_code := p_time_phase_type_code;
8129 
8130 	Else
8131 		If g_start_date is not null and g_end_date is not null and
8132 		trunc(p_expenditure_item_date) between trunc(g_start_date) and trunc(g_end_date) then
8133 			l_start_date := g_start_date;
8134 			l_end_date   := g_end_date;
8135 			x_result_code := g_sd_result_code;
8136 			pa_funds_control_pkg.log_message(p_msg_token1 => 'Same '||p_type);
8137 
8138 		Else
8139 			pa_funds_control_pkg.log_message(p_msg_token1 => 'Different 2');
8140 			pa_funds_control_pkg.log_message(p_msg_token1 => 'Calling setup_start_end_date');
8141                         setup_start_end_date (
8142                         p_packet_id                => p_packet_id
8143                         ,p_bc_packet_id             => p_bc_packet_id
8144                         ,p_project_id               => p_project_id
8145                         ,p_budget_version_id        => p_budget_version_id
8146                         ,p_time_phase_type_code     => p_time_phase_type_code
8147                         ,p_expenditure_item_date    => p_expenditure_item_date
8148                         ,p_amount_type              => p_amount_type
8149                         ,p_boundary_code            => p_boundary_code
8150                         ,p_set_of_books_id          => p_set_of_books_id
8151                         ,x_start_date               => l_start_date
8152                         ,x_end_date                 => l_end_date
8153                         ,x_error_code               => x_error_code
8154                         ,x_err_buff                 => x_err_buff
8155                         ,x_return_status            => x_return_status
8156                         ,x_result_code              => x_result_code);
8157 
8158 			g_start_date := l_start_date;
8159 			g_end_date   := l_end_date;
8160 			g_sd_result_code := x_result_code;
8161 		End if;
8162 
8163 	End if;
8164 
8165 	IF p_type = 'START_DATE' then
8166 		Return l_start_date;
8167 	Elsif p_type = 'END_DATE' then
8168 		Return l_end_date;
8169 	End if;
8170 
8171 EXCEPTION
8172 
8173 	when others then
8174 		pa_funds_control_pkg.log_message(p_msg_token1 =>
8175 			'Failed in the get_start_date api');
8176 		raise;
8177 
8178 END get_start_or_end_date;
8179 
8180 -- R12 Funds Management Uptake : This tieback procedure is called from PSA_BC_XLA_PVT.Budgetary_control
8181 -- if SLA accounting fails.This API will mark the pa_bc_packet records to failed status.
8182 
8183 PROCEDURE TIEBACK_FAILED_ACCT_STATUS (p_bc_mode  IN VARCHAR2 DEFAULT 'C') IS
8184 
8185  CURSOR C_PRINT_EVENT_STATUS IS
8186  SELECT event_id, b.result_code
8187    FROM PSA_BC_XLA_EVENTS_GT b;
8188 
8189  BEGIN
8190 
8191 	If pa_funds_control_pkg.g_debug_mode = 'Y' then
8192 		pa_funds_control_pkg.log_message(p_msg_token1=>'Inside TIEBACK_FAILED_ACCT_STATUS procedure');
8193 	end if;
8194 
8195         -- Bug 5354715 : Added the "PA IMPLEMENTED IN OU" check.
8196         IF PA_FUNDS_CONTROL_PKG.IS_PA_INSTALL_IN_OU = 'N' then
8197 
8198 		If pa_funds_control_pkg.g_debug_mode = 'Y' then
8199 			pa_funds_control_pkg.log_message(p_msg_token1=>'PA NOT INSTALLED IN THIS OU');
8200 		end if;
8201                 Return;
8202         END IF;
8203 
8204    IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
8205       pa_funds_control_pkg.log_message(p_msg_token1 => 'TIEBACK_FAILED_ACCT_STATUS : Start ');
8206    End if;
8207 
8208    IF pa_budget_fund_pkg.g_processing_mode IN ('YEAR_END','CHECK_FUNDS','BASELINE') THEN
8209       RETURN;
8210    END IF;
8211 
8212    IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
8213       FOR i IN C_PRINT_EVENT_STATUS LOOP
8214         pa_funds_control_pkg.log_message(p_msg_token1 => 'TIEBACK_FAILED_ACCT_STATUS : Result Code of event_id '||i.event_id||' = '||i.result_code);
8215       END LOOP;
8216    End if;
8217 
8218 
8219    UPDATE pa_bc_packets a
8220       SET  status_code = DECODE(p_bc_mode,'C','F','R'),
8221           result_code  = DECODE(substr(result_code,1,1),'F',result_code,'F172')
8222    WHERE  status_code in ('P','I','A','S')
8223      AND  source_event_id IN
8224             (SELECT  event_id
8225                FROM  PSA_BC_XLA_EVENTS_GT
8226               WHERE upper(result_code) in ('XLA_ERROR','FATAL','XLA_UNPROCESSED','XLA_NO_JOURNAL'));
8227 
8228    UPDATE pa_bc_packets a
8229       SET  status_code  = DECODE(p_bc_mode,'C','F','R'),
8230            result_code  = DECODE(substr(result_code,1,1),'F',result_code,'F172')
8231    WHERE  status_code in ('P','I','A','S')
8232      AND  bc_event_id IN
8233             (SELECT  event_id
8234                FROM  PSA_BC_XLA_EVENTS_GT
8235               WHERE upper(result_code) in ('XLA_ERROR','FATAL','XLA_UNPROCESSED','XLA_NO_JOURNAL'));
8236 
8237    IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
8238           pa_funds_control_pkg.log_message(p_msg_token1 => 'TIEBACK_FAILED_ACCT_STATUS : End ');
8239    End if;
8240 
8241 END TIEBACK_FAILED_ACCT_STATUS;
8242 
8243 -- R12 Funds Management Uptake : This Procedure is called from "PRC: Generate Cost accounting events"
8244 -- Process. After events are generated for BTC/TBC actuals which are eligible to get interfaced to SLA ,
8245 -- The below procedure stamps these events on the associated commitments .
8246 
8247 PROCEDURE INTERFACE_TBC_BTC_COMT_UPDATE (p_calling_module IN VARCHAR2,
8248                                          P_request_id     IN NUMBER  ,
8249 					 x_result_code    OUT NOCOPY VARCHAR2) IS
8250 
8251 -- Variables to store eligible CDL data
8252  l_Exp_Item_Id_tbl              PA_PLSQL_DATATYPES.IDTabTyp;
8253  l_cdl_gl_Date_tbl              PA_PLSQL_DATATYPES.DateTabTyp;
8254  l_Burd_Sum_Dest_Run_Id_tbl     PA_PLSQL_DATATYPES.IDTabTyp;
8255  l_doc_header_id_tbl            PA_PLSQL_DATATYPES.IDTabTyp;
8256  l_doc_distribution_id_tbl      PA_PLSQL_DATATYPES.IDTabTyp;
8257  l_cdl_line_type_tbl            PA_PLSQL_DATATYPES.Char1TabTyp;
8258  l_system_linkage_function_tbl  PA_PLSQL_DATATYPES.Char3TabTyp;
8259  l_expenditure_type_tbl         PA_PLSQL_DATATYPES.Char30TabTyp;
8260  l_cdl_acct_event_id_tbl        PA_PLSQL_DATATYPES.IDTabTyp;
8261  l_acct_source_code_tbl         PA_PLSQL_DATATYPES.Char30TabTyp;
8262  l_count_of_records             NUMBER;
8263  l_Return_Status                VARCHAR2(100);
8264  l_Msg_Data                     VARCHAR2(2000);
8265 
8266  -- Below variables added for bug 5263721
8267  l_billable_flag_tbl		PA_PLSQL_DATATYPES.Char1TabTyp;
8268  l_project_id_tbl		PA_PLSQL_DATATYPES.IDTabTyp;
8269  l_task_id_tbl			PA_PLSQL_DATATYPES.IDTabTyp;
8270  l_pa_period_name_tbl		PA_PLSQL_DATATYPES.Char30TabTyp;
8271  l_denom_currency_code_tbl	PA_PLSQL_DATATYPES.Char30TabTyp;
8272  l_acct_currency_code_tbl	PA_PLSQL_DATATYPES.Char30TabTyp;
8273  l_project_currency_code_tbl	PA_PLSQL_DATATYPES.Char30TabTyp;
8274  l_projfunc_currency_code_tbl	PA_PLSQL_DATATYPES.Char30TabTyp;
8275  l_system_reference1_tbl	PA_PLSQL_DATATYPES.Char30TabTyp;
8276  l_person_type_tbl		PA_PLSQL_DATATYPES.Char30TabTyp;
8277  l_po_line_id_tbl		PA_PLSQL_DATATYPES.IDTabTyp;
8278  l_attribute1_tbl		PA_PLSQL_DATATYPES.Char150TabTyp;
8279  l_attribute2_tbl		PA_PLSQL_DATATYPES.Char150TabTyp;
8280  l_attribute3_tbl		PA_PLSQL_DATATYPES.Char150TabTyp;
8281  l_attribute4_tbl		PA_PLSQL_DATATYPES.Char150TabTyp;
8282  l_attribute5_tbl		PA_PLSQL_DATATYPES.Char150TabTyp;
8283  l_attribute6_tbl		PA_PLSQL_DATATYPES.Char150TabTyp;
8284  l_attribute7_tbl		PA_PLSQL_DATATYPES.Char150TabTyp;
8285  l_attribute8_tbl		PA_PLSQL_DATATYPES.Char150TabTyp;
8286  l_attribute9_tbl		PA_PLSQL_DATATYPES.Char150TabTyp;
8287  l_attribute10_tbl		PA_PLSQL_DATATYPES.Char150TabTyp;
8288  l_attribute_category_tbl	PA_PLSQL_DATATYPES.Char30TabTyp;
8289  l_expenditure_item_date_tbl	PA_PLSQL_DATATYPES.DateTabTyp;
8290  l_ACCT_RATE_DATE_tbl		PA_PLSQL_DATATYPES.DateTabTyp;
8291  l_ACCT_RATE_TYPE_tbl		PA_PLSQL_DATATYPES.Char30TabTyp;
8292  l_ACCT_EXCHANGE_RATE_tbl	PA_PLSQL_DATATYPES.NumTabTyp;
8293  l_PROJECT_RATE_DATE_tbl	PA_PLSQL_DATATYPES.DateTabTyp;
8294  l_PROJECT_RATE_TYPE_tbl	PA_PLSQL_DATATYPES.Char30TabTyp;
8295  l_PROJECT_EXCHANGE_RATE_tbl	PA_PLSQL_DATATYPES.NumTabTyp;
8296  l_PROJFUNC_COST_RATE_DATE_tbl	PA_PLSQL_DATATYPES.DateTabTyp;
8297  l_PROJFUNC_COST_RATE_TYPE_tbl	PA_PLSQL_DATATYPES.Char30TabTyp;
8298  l_pfc_ex_rate_tbl		PA_PLSQL_DATATYPES.NumTabTyp;
8299  l_job_id_tbl			PA_PLSQL_DATATYPES.IDTabTyp;
8300  l_non_labor_resource_tbl	PA_PLSQL_DATATYPES.Char30TabTyp;
8301  l_nl_res_orgn_id_tbl		PA_PLSQL_DATATYPES.IDTabTyp;
8302  l_wip_resource_id_tbl		PA_PLSQL_DATATYPES.IDTabTyp;
8303  l_incurred_by_person_id_tbl	PA_PLSQL_DATATYPES.IDTabTyp;
8304  l_inventory_item_id_tbl	PA_PLSQL_DATATYPES.IDTabTyp;
8305 
8306  -- Bug 5680236 : Added below new variables
8307  l_budget_line_id_tbl	        PA_PLSQL_DATATYPES.IDTabTyp;
8308  l_budget_version_id_tbl	PA_PLSQL_DATATYPES.IDTabTyp;
8309  l_doc_line_number_tbl          PA_PLSQL_DATATYPES.IDTabTyp;
8310  l_transaction_source_tbl       PA_PLSQL_DATATYPES.Char30TabTyp;
8311  l_cdl_rowid_tbl                PA_PLSQL_DATATYPES.RowidTabTyp;
8312 
8313  l_bc_commitment_id_tbl         PA_PLSQL_DATATYPES.IDTabTyp;
8314  l_bc_project_id_tbl            PA_PLSQL_DATATYPES.IDTabTyp;
8315  l_bc_task_id_tbl               PA_PLSQL_DATATYPES.IDTabTyp;
8316  l_bc_res_list_mem_id_tbl       PA_PLSQL_DATATYPES.IDTabTyp;
8317  l_bc_top_task_id_tbl           PA_PLSQL_DATATYPES.IDTabTyp;
8318  l_bc_budget_version_id_tbl	PA_PLSQL_DATATYPES.IDTabTyp;
8319  l_bc_budget_line_id_tbl	PA_PLSQL_DATATYPES.IDTabTyp;
8320  l_bc_entry_level_code_tbl      PA_PLSQL_DATATYPES.Char30TabTyp;
8321  l_bc_gl_start_date_tbl         PA_PLSQL_DATATYPES.DateTabTyp;
8322  l_bc_exp_item_id_tbl	        PA_PLSQL_DATATYPES.IDTabTyp;
8323  l_bc_transfer_sts_code_tbl     PA_PLSQL_DATATYPES.Char30TabTyp;
8324 
8325  l_cdl_top_task_id              pa_tasks.top_task_id%TYPE;
8326  l_cdl_budget_version_id        pa_budget_versions.budget_version_id%TYPE;
8327  l_cdl_entry_level_code         pa_budget_entry_methods.entry_level_code%TYPE;
8328  l_budget_line_id               pa_bc_packets.budget_line_id%TYPE;
8329  l_budget_ccid                  pa_bc_packets.budget_ccid%TYPE;
8330  l_error_message_code           VARCHAR2(200) := NULL;
8331 
8332  -- Bug 5680236 : End of variable declaration
8333 
8334  /* Cursor to fetch CDL data associated with TBC and BTC lines
8335     Picks BTC expenditures and TBC lines generated on 'VI'/'ST'/'OT' expenditure items.
8336     This cursor picks even those expenditure items which are marked to 'A' status in this
8337     run because of implementation level allow interface flags set to 'N'  */
8338 
8339  CURSOR CDL_CUR IS
8340  SELECT CDL.Expenditure_Item_Id,
8341         CDL.Gl_Date,
8342 	ITEM.Burden_Sum_Dest_Run_Id,
8343 	ITEM.document_header_id,
8344 	ITEM.document_distribution_id ,
8345 	CDL.line_type,
8346 	ITEM.system_linkage_function,
8347         ITEM.expenditure_type,
8348         CDL.acct_event_id,
8349  -- Below columns added for bug 5263721
8350         CDL.billable_flag,
8351         CDL.project_id,
8352         CDL.task_id,
8353         CDL.pa_period_name,
8354         ITEM.denom_currency_code,
8355         ITEM.acct_currency_code,
8356         ITEM.project_currency_code,
8357         ITEM.projfunc_currency_code,
8358         CDL.system_reference1,
8359         EXP.person_type,
8360         ITEM.po_line_id,
8361 	ITEM.attribute1,
8362 	ITEM.attribute2,
8363 	ITEM.attribute3,
8364 	ITEM.attribute4,
8365 	ITEM.attribute5,
8366 	ITEM.attribute6,
8367 	ITEM.attribute7,
8368 	ITEM.attribute8,
8369 	ITEM.attribute9,
8370 	ITEM.attribute10,
8371 	ITEM.attribute_category,
8372 	ITEM.expenditure_item_date,
8373 	CDL.acct_rate_date,
8374 	CDL.acct_rate_type,
8375 	CDL.acct_exchange_rate,
8376 	CDL.project_rate_date,
8377 	CDL.project_rate_type,
8378 	CDL.project_exchange_rate,
8379 	CDL.projfunc_cost_rate_date,
8380 	CDL.projfunc_cost_rate_type,
8381 	CDL.projfunc_cost_exchange_rate,
8382 	ITEM.job_id,
8383 	ITEM.non_labor_resource,
8384 	ITEM.organization_id non_labor_resource_orgn_id,
8385 	ITEM.wip_resource_id,
8386 	EXP.incurred_by_person_id,
8387 	ITEM.inventory_item_id,
8388 	-- Columns added for Bug 5680236
8389 	cdl.budget_line_id,
8390 	cdl.budget_version_id,
8391 	ITEM.document_line_number,
8392 	ITEM.transaction_source,
8393 	cdl.rowid
8394  FROM   PA_Cost_Distribution_Lines CDL,
8395         pa_expenditure_items_all ITEM,
8396 	pa_expenditures_all EXP
8397  WHERE  CDL.transfer_status_code in ('A', 'G')
8398    AND  CDL.request_id = p_request_id
8399    AND  item.expenditure_item_id = cdl.expenditure_item_id
8400    AND  ITEM.expenditure_id = EXP.expenditure_id
8401    AND    ( (p_calling_module = 'BTC' AND item.system_linkage_function = 'BTC' AND CDL.line_type = 'R' ) OR
8402             (p_calling_module = 'TBC' AND CDL.line_type = 'D' AND item.system_linkage_function IN ('VI','ST','OT')) OR
8403             (p_calling_module = 'Cost' AND ((item.system_linkage_function = 'BTC'  AND CDL.line_type = 'R') OR
8404 						     (CDL.line_type = 'D' AND item.system_linkage_function IN ('VI','ST','OT'))))
8405           )
8406    AND  NVL(CDL.liquidate_encum_flag,'N') = 'Y'
8407 ORDER BY  CDL.Expenditure_Item_Id,CDL.line_num;
8408 
8409 
8410 -- Bug 5680236 : Cursor to fetch  bc_commitment records associated with CDL's
8411 CURSOR  c_bc_com_details       (p_doc_header_id         NUMBER,
8412                                 p_doc_line_number       NUMBER,
8413 			        p_doc_distribution_id   NUMBER,
8414 			        p_transaction_source    VARCHAR2,
8415 			        p_cdl_line_type         VARCHAR2,
8416 				p_expenditure_type      VARCHAR2,
8417 				p_expenditure_item_date DATE) IS
8418 SELECT pbc.bc_commitment_id,
8419        pbc.project_id,
8420        pbc.task_id,
8421        pbc.resource_list_member_id,
8422        DECODE(pm.entry_level_code,'P',0,pt.top_task_id) top_task_id,
8423        bv.budget_version_id,
8424        pbc.budget_line_id,
8425        pm.entry_level_code,
8426        (SELECT gl.start_date
8427           FROM  gl_period_statuses gl
8428          WHERE gl.application_id  = 101
8429            AND gl.set_of_books_id = pbc.set_of_books_id
8430            AND gl.period_name     = pbc.period_name) gl_start_date,
8431        pbc.exp_item_id,
8432        pbc.transfer_status_code
8433   FROM pa_bc_commitments_all pbc,
8434        pa_tasks pt,
8435        pa_budget_versions bv,
8436        pa_budget_entry_methods pm
8437  WHERE pbc.document_header_id = p_doc_header_id
8438    AND pbc.document_distribution_id = DECODE(substr(p_transaction_source,1,10),'PO RECEIPT',p_doc_line_number,p_doc_distribution_id)
8439    AND pbc.expenditure_type = p_expenditure_type
8440    AND pbc.expenditure_item_date = p_expenditure_item_date
8441    --AND  pbc.transfer_status_code in ('P','R','X')
8442    AND pbc.document_type in ('AP','PO')
8443    AND pbc.burden_cost_flag = 'R'
8444    AND ((pbc.parent_bc_packet_id is not null AND p_cdl_line_type ='R') OR  p_cdl_line_type ='D')
8445    AND pt.task_id = pbc.task_id
8446    AND bv.budget_version_id = pbc.budget_version_id
8447    AND bv.budget_entry_method_code = pm.budget_entry_method_code;
8448 
8449 -- Bug 5680236 : Cursor to fetch budget version details for a task on CDL's
8450 CURSOR c_budget_details (p_task_id    NUMBER) IS
8451 SELECT DECODE(pm.entry_level_code,'P',0,pt.top_task_id) top_task_id,
8452        bv.budget_version_id,
8453        pm.entry_level_code
8454   FROM pa_tasks pt,
8455        pa_budget_versions bv,
8456        Pa_Budget_Types bt,
8457        pa_budgetary_control_options pbct,
8458        pa_budget_entry_methods pm
8459  WHERE pt.task_id = p_task_id
8460    AND bv.project_id = pt.project_id
8461    AND bv.BUDGET_STATUS_CODE = 'B'
8462    AND bt.budget_type_Code = bv.budget_type_Code
8463    and bt.budget_amount_code = 'C'
8464    and bv.current_flag = 'Y'
8465    and pbct.project_id = bv.project_id
8466    and pbct.BDGT_CNTRL_FLAG = 'Y'
8467    and pbct.BUDGET_TYPE_CODE = bv.budget_type_code
8468    and (pbct.EXTERNAL_BUDGET_CODE = 'GL'
8469         OR
8470         pbct.EXTERNAL_BUDGET_CODE is NULL
8471        )
8472    AND  bv.budget_entry_method_code = pm.budget_entry_method_code;
8473 
8474 BEGIN
8475 
8476         --- Initialize the error statck
8477 	x_result_code := 'Success';
8478 
8479         PA_DEBUG.init_err_stack ('PA_FUNDS_CONTROL_PKG1.INTERFACE_TBC_BTC_COMT_UPDATE');
8480 
8481 
8482         fnd_profile.get('PA_DEBUG_MODE',PA_FUNDS_CONTROL_PKG.g_debug_mode);
8483         PA_FUNDS_CONTROL_PKG.g_debug_mode := NVL(PA_FUNDS_CONTROL_PKG.g_debug_mode, 'N');
8484 
8485         PA_DEBUG.SET_PROCESS( x_process        => 'PLSQL'
8486                              ,x_write_file     => 'LOG'
8487                              ,x_debug_mode     => PA_FUNDS_CONTROL_PKG.g_debug_mode);
8488 
8489 	IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
8490  	  pa_funds_control_pkg.log_message(p_msg_token1 => 'INTERFACE_TBC_BTC_COMT_UPDATE : In start ');
8491  	  pa_funds_control_pkg.log_message(p_msg_token1 => 'INTERFACE_TBC_BTC_COMT_UPDATE:  p_calling_module = '||p_calling_module);
8492  	  pa_funds_control_pkg.log_message(p_msg_token1 => 'INTERFACE_TBC_BTC_COMT_UPDATE : P_request_id = '||P_request_id);
8493 	End if;
8494 
8495         -----------------------------------------------------------------------------------+
8496         -- Invoke the Sweeper process to sweep all the encumbrance etries
8497         -- from PA_BC_PACKETS to PA_BC_BALANCES and PA_BC_COMMITMENTS
8498         -----------------------------------------------------------------------------------+
8499         PA_Sweeper.Update_Act_Enc_Balance (
8500                   X_Return_Status              => l_Return_Status,
8501                   X_Error_Message_Code         => l_Msg_Data
8502          );
8503 
8504         IF l_Return_Status <> FND_API.G_RET_STS_SUCCESS THEN
8505           IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
8506              pa_funds_control_pkg.log_message(p_msg_token1 => 'Error occured while running sweeper process PA_Sweeper.Update_Act_Enc_Balance');
8507              pa_funds_control_pkg.log_message(p_msg_token1 => 'X_Error_Message_Code:'||l_Msg_Data);
8508           END IF;
8509 	  x_result_code := 'Error';
8510           RETURN;
8511         END IF;
8512 
8513 	IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
8514  	  pa_funds_control_pkg.log_message(p_msg_token1 => 'INTERFACE_TBC_BTC_COMT_UPDATE : Opening cursor CDL_CUR ');
8515         End if;
8516 
8517         OPEN CDL_CUR ;
8518         FETCH CDL_CUR BULK COLLECT INTO
8519 		l_Exp_Item_Id_tbl,
8520 		l_cdl_gl_Date_tbl,
8521 		l_Burd_Sum_Dest_Run_Id_tbl,
8522 		l_doc_header_id_tbl,
8523 		l_doc_distribution_id_tbl,
8524 		l_cdl_line_type_tbl,
8525 		l_system_linkage_function_tbl,
8526 		l_expenditure_type_tbl,
8527 		l_cdl_acct_event_id_tbl,
8528  -- Below columns added for bug 5263721
8529 		l_billable_flag_tbl,
8530 		l_project_id_tbl,
8531 		l_task_id_tbl,
8532 		l_pa_period_name_tbl,
8533 		l_denom_currency_code_tbl,
8534 		l_acct_currency_code_tbl,
8535 		l_project_currency_code_tbl,
8536 		l_projfunc_currency_code_tbl,
8537 		l_system_reference1_tbl,
8538 		l_person_type_tbl,
8539 		l_po_line_id_tbl,
8540 		l_attribute1_tbl,
8541 		l_attribute2_tbl,
8542 		l_attribute3_tbl,
8543 		l_attribute4_tbl,
8544 		l_attribute5_tbl,
8545 		l_attribute6_tbl,
8546 		l_attribute7_tbl,
8547 		l_attribute8_tbl,
8548 		l_attribute9_tbl,
8549 		l_attribute10_tbl,
8550 		l_attribute_category_tbl,
8551 		l_expenditure_item_date_tbl,
8552 		l_acct_rate_date_tbl,
8553 		l_acct_rate_type_tbl,
8554 		l_acct_exchange_rate_tbl,
8555 		l_project_rate_date_tbl,
8556 		l_project_rate_type_tbl,
8557 		l_project_exchange_rate_tbl,
8558 		l_projfunc_cost_rate_date_tbl,
8559 		l_projfunc_cost_rate_type_tbl,
8560 		l_pfc_ex_rate_tbl,
8561 		l_job_id_tbl,
8562 		l_non_labor_resource_tbl,
8563 		l_nl_res_orgn_id_tbl,
8564 		l_wip_resource_id_tbl,
8565 		l_incurred_by_person_id_tbl,
8566 		l_inventory_item_id_tbl,
8567                 l_budget_line_id_tbl,
8568                 l_budget_version_id_tbl,
8569 		l_doc_line_number_tbl,
8570 		l_transaction_source_tbl,
8571 		l_cdl_rowid_tbl;
8572 
8573         CLOSE CDL_CUR;
8574 
8575         l_count_of_records := l_Exp_Item_Id_tbl.count;
8576 
8577 	IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
8578  	  pa_funds_control_pkg.log_message(p_msg_token1 => 'INTERFACE_TBC_BTC_COMT_UPDATE : NUmber of records fethced '||l_count_of_records);
8579         End if;
8580 
8581        IF l_count_of_records <> 0 THEN
8582 
8583          -- Bug 5680236 : Columns used by R12 fundscheck logic which were not populated prior R12
8584 	 -- Table name                   column name             Prior FP.M     In FP.M
8585          -- -----------------------------------------------------------------------------
8586 	 -- pa_cost_distribution_line    budget_version_id       Not Supported  Not Supported
8587 	 --                              budget_line_id          Not Supported  Not Supported
8588 	 -- pa_bc_commitments            budget_line_id          Not Supported  Not Supported
8589 	 --                              exp_item_id             Not Supported  Supported
8590 
8591          -- Below logic has been added to populate above columns for expenditures interfaced to
8592 	 -- projects prior R12
8593 
8594 	 FOR cdl_rec in 1..l_count_of_records LOOP
8595 
8596 	    IF l_budget_line_id_tbl(cdl_rec) IS NULL OR l_budget_version_id_tbl(cdl_rec) is NULL THEN
8597 
8598            	IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
8599  	           pa_funds_control_pkg.log_message(p_msg_token1 => 'INTERFACE_TBC_BTC_COMT_UPDATE : Data interfaced prior R12');
8600                 End if;
8601 
8602                 OPEN c_bc_com_details       (l_doc_header_id_tbl(cdl_rec),
8603                                              l_doc_line_number_tbl(cdl_rec),
8604 			                     l_doc_distribution_id_tbl(cdl_rec),
8605 			                     l_transaction_source_tbl(cdl_rec),
8606 			                     l_cdl_line_type_tbl(cdl_rec),
8607 				             l_expenditure_type_tbl(cdl_rec),
8608 					     l_expenditure_item_date_tbl(cdl_rec));
8609                 FETCH c_bc_com_details       BULK COLLECT INTO
8610                                              l_bc_commitment_id_tbl,
8611                                              l_bc_project_id_tbl,
8612                                              l_bc_task_id_tbl,
8613                                              l_bc_res_list_mem_id_tbl,
8614                                              l_bc_top_task_id_tbl,
8615                                              l_bc_budget_version_id_tbl,
8616                                              l_bc_budget_line_id_tbl,
8617                                              l_bc_entry_level_code_tbl,
8618                                              l_bc_gl_start_date_tbl,
8619                                              l_bc_exp_item_id_tbl,
8620                                              l_bc_transfer_sts_code_tbl;
8621                 CLOSE c_bc_com_details;
8622 
8623            	IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
8624  	           pa_funds_control_pkg.log_message(p_msg_token1 => 'INTERFACE_TBC_BTC_COMT_UPDATE : # of records fetched from CDL'||l_bc_commitment_id_tbl.count);
8625                 End if;
8626 
8627                 l_budget_line_id := NULL;
8628 
8629                 -- Check if budget_line_id was already derived and stamped on any of the bc commitment record
8630                 -- IF exists then we just need to stamp the same budget_line_id on CDL and other bc commitment records.
8631 		-- IF NULL then need to derive budget line id
8632 		-- Note : Based on the filters provided to fetch data from above cursor c_bc_com_details all records
8633 		-- will have same budget line id
8634 
8635                 FOR bccomm_rec in 1..l_bc_commitment_id_tbl.count LOOP
8636                     IF l_bc_budget_line_id_tbl(bccomm_rec) IS NOT NULL THEN
8637 		       l_budget_line_id := l_bc_budget_line_id_tbl(bccomm_rec);
8638 		       EXIT;
8639 		    END IF;
8640                 END LOOP;
8641 
8642           	IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
8643  	           pa_funds_control_pkg.log_message(p_msg_token1 => 'INTERFACE_TBC_BTC_COMT_UPDATE : budget line id on bc comt'||l_budget_line_id);
8644  	           pa_funds_control_pkg.log_message(p_msg_token1 => 'INTERFACE_TBC_BTC_COMT_UPDATE : Before calling PA_FUNDS_CONTROL_UTILS.Get_Budget_CCID' );
8645 		End if;
8646 
8647 		IF l_budget_line_id IS NULL THEN
8648 
8649                    -- Calling below procedure to derive budget_line_id for bc commitments data
8650                    PA_FUNDS_CONTROL_UTILS.Get_Budget_CCID (
8651                                        p_project_id 		=> l_bc_project_id_tbl(1),
8652                                        p_task_id    		=> l_bc_task_id_tbl(1),
8653                                        p_res_list_mem_id 	=> l_bc_res_list_mem_id_tbl(1),
8654 		                       p_start_date		=> l_bc_gl_start_date_tbl(1),
8655                                        p_budget_version_id 	=> l_bc_budget_version_id_tbl(1),
8656 		                       p_top_task_id		=> l_bc_top_task_id_tbl(1),
8657 		                       p_entry_level_code       => l_bc_entry_level_code_tbl(1),
8658                                        x_budget_ccid  	        => l_budget_ccid,
8659                                        x_budget_line_id         => l_budget_line_id,
8660                                        x_return_status 	        => l_return_status,
8661                                        x_error_message_code 	=> l_error_message_code);
8662 
8663               	  IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
8664  	           pa_funds_control_pkg.log_message(p_msg_token1 => 'INTERFACE_TBC_BTC_COMT_UPDATE : After calling PA_FUNDS_CONTROL_UTILS.Get_Budget_CCID' );
8665  	             pa_funds_control_pkg.log_message(p_msg_token1 => 'INTERFACE_TBC_BTC_COMT_UPDATE : derived budget line id on bc comt'||l_budget_line_id);
8666                   End if;
8667 
8668                 END IF;
8669 
8670                 FORALL i in 1..l_bc_commitment_id_tbl.count
8671                 UPDATE pa_bc_commitments
8672 		   SET budget_line_id = l_budget_line_id
8673 		 WHERE bc_commitment_id = l_bc_commitment_id_tbl(i)
8674 		   AND budget_line_id IS NULL;
8675 
8676          	IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
8677  	             pa_funds_control_pkg.log_message(p_msg_token1 => 'INTERFACE_TBC_BTC_COMT_UPDATE : # of bc comt records updated'||SQL%ROWCOUNT);
8678                 End if;
8679 
8680       	        -- Check if Project/task on CDL has ben modified i.e. different from the one stamped
8681 		-- on bc commitments.In this case rederive budget version details and then
8682 		-- derive budget_line_id for CDL
8683 
8684                 l_cdl_budget_version_id := NULL;
8685 		IF l_bc_project_id_tbl(1) <> l_project_id_tbl(cdl_rec) OR
8686 		   l_bc_task_id_tbl(1)    <> l_task_id_tbl(cdl_rec)       THEN
8687 
8688                          l_budget_line_id := NULL;
8689 
8690                          OPEN  c_budget_details(l_task_id_tbl(cdl_rec));
8691                          FETCH c_budget_details INTO
8692                                              l_cdl_top_task_id ,
8693                                              l_cdl_budget_version_id,
8694                                              l_cdl_entry_level_code;
8695                          CLOSE c_budget_details;
8696 
8697           	IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
8698  	           pa_funds_control_pkg.log_message(p_msg_token1 => 'INTERFACE_TBC_BTC_COMT_UPDATE : Before calling PA_FUNDS_CONTROL_UTILS.Get_Budget_CCID' );
8699 		End if;
8700 
8701                          PA_FUNDS_CONTROL_UTILS.Get_Budget_CCID (
8702                                        p_project_id 		=> l_project_id_tbl(cdl_rec),
8703                                        p_task_id    		=> l_task_id_tbl(cdl_rec),
8704                                        p_res_list_mem_id 	=> l_bc_res_list_mem_id_tbl(1),
8705 		                       p_start_date		=> l_bc_gl_start_date_tbl(1),
8706                                        p_budget_version_id 	=> l_cdl_budget_version_id,
8707 		                       p_top_task_id		=> l_cdl_top_task_id,
8708 		                       p_entry_level_code       => l_cdl_entry_level_code,
8709                                        x_budget_ccid  	        => l_budget_ccid,
8710                                        x_budget_line_id         => l_budget_line_id,
8711                                        x_return_status    	=> l_return_status,
8712                                        x_error_message_code 	=> l_error_message_code);
8713 
8714               	         IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
8715 	           pa_funds_control_pkg.log_message(p_msg_token1 => 'INTERFACE_TBC_BTC_COMT_UPDATE : After calling PA_FUNDS_CONTROL_UTILS.Get_Budget_CCID' );
8716  	                    pa_funds_control_pkg.log_message(p_msg_token1 => 'INTERFACE_TBC_BTC_COMT_UPDATE : derived budget line id on cdl'||l_budget_line_id);
8717                          End if;
8718                 END IF;
8719 
8720 		UPDATE pa_cost_distribution_lines_all cdl
8721 		   SET cdl.budget_version_id = NVL(l_cdl_budget_version_id,l_bc_budget_version_id_tbl(1)),
8722 		       cdl.budget_line_id    = l_budget_line_id
8723  		 WHERE cdl.rowid = l_cdl_rowid_tbl(cdl_rec);
8724 
8725          	IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
8726                    pa_funds_control_pkg.log_message(p_msg_token1 => 'INTERFACE_TBC_BTC_COMT_UPDATE : # of cdl records updated'||SQL%ROWCOUNT);
8727                 End if;
8728 
8729                 FORALL i in 1..l_bc_commitment_id_tbl.count
8730                 UPDATE pa_bc_commitments
8731 		   SET exp_item_id = l_Exp_Item_Id_tbl(cdl_rec)
8732 		 WHERE bc_commitment_id = l_bc_commitment_id_tbl(i)
8733 		   AND exp_item_id IS NULL
8734 		   AND transfer_status_code in ('P','R','X');
8735 
8736          	IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
8737  	             pa_funds_control_pkg.log_message(p_msg_token1 => 'INTERFACE_TBC_BTC_COMT_UPDATE : # of bc comt records updated'||SQL%ROWCOUNT);
8738                 End if;
8739 
8740 	    END IF; --IF l_budget_line_id_tbl(cdl_rec) IS NULL OR l_budget_version_id_tbl(cdl_rec) is NULL THEN
8741          END LOOP; --FOR cdl_rec in 1..l_count_of_records LOOP
8742          -- End of logic added for Bug 5680236
8743 
8744  	 IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
8745  	  pa_funds_control_pkg.log_message(p_msg_token1 => 'INTERFACE_TBC_BTC_COMT_UPDATE : updating eligible AP commitments for liq encumbrance ');
8746          End if;
8747 
8748          FORALL i in 1..l_count_of_records
8749          UPDATE  pa_bc_commitments bc_cm
8750             SET  bc_cm.bc_event_id = l_cdl_acct_event_id_tbl(i),
8751   	         bc_cm.transfer_status_code = 'A',
8752 	         bc_cm.request_id  = p_request_id,
8753 	  	 bc_cm.transferred_date = SYSDATE,
8754 		 bc_cm.liquidate_gl_date = l_cdl_gl_Date_tbl(i),
8755 		 bc_cm.exp_item_id = DECODE (l_cdl_line_type_tbl(i),'R',l_Exp_Item_Id_tbl(i),bc_cm.exp_item_id) -- Bug 5076612
8756           WHERE ( bc_cm.document_header_id,bc_cm.document_distribution_id,expenditure_type)
8757 		   IN ( SELECT exp.document_header_id,exp.document_distribution_id,l_expenditure_type_tbl(i)
8758 		          FROM PA_Cost_Distribution_lines  cdl_raw,
8759 			       pa_expenditure_items_all  exp ,
8760 			       pa_expenditures_all expend
8761 			      WHERE cdl_raw.burden_sum_source_run_id = l_Burd_Sum_Dest_Run_Id_tbl(i)
8762 				AND exp.expenditure_item_id = cdl_raw.expenditure_item_id
8763 				AND cdl_raw.line_num = 1
8764 				AND l_cdl_line_type_tbl(i) ='R'
8765  -- Below join conditions added for bug 5263721
8766 				AND exp.expenditure_id = expend.expenditure_id
8767 				AND nvl(l_billable_flag_tbl(i), -1) = nvl(cdl_raw.billable_flag, -1)
8768 				AND nvl(l_project_id_tbl(i), -1) = nvl(cdl_raw.project_id, -1)
8769 				AND nvl(l_task_id_tbl(i), -1) = nvl(cdl_raw.task_id, -1)
8770 				AND nvl(l_pa_period_name_tbl(i), -1) = nvl(cdl_raw.pa_period_name, -1)
8771 				AND nvl(l_denom_currency_code_tbl(i), -1) = nvl(exp.denom_currency_code, -1)
8772 				AND nvl(l_acct_currency_code_tbl(i), -1) = nvl(exp.acct_currency_code, -1)
8773 				AND nvl(l_project_currency_code_tbl(i), -1) = nvl(exp.project_currency_code, -1)
8774 				AND nvl(l_projfunc_currency_code_tbl(i), -1) = nvl(exp.projfunc_currency_code, -1)
8775 				/* AND nvl(l_system_reference1_tbl(i), -1) = nvl(cdl_raw.system_reference1, -1) bug 5453131*/
8776 				AND nvl(l_person_type_tbl(i), -1) = nvl(expend.person_type, -1)
8777 				AND nvl(l_po_line_id_tbl(i), -1) = nvl(exp.po_line_id, -1)
8778 				AND nvl(PA_CLIENT_EXTN_BURDEN_SUMMARY.CLIENT_GROUPING(
8779 					null,
8780 					null,
8781 					l_attribute1_tbl(i),
8782 					l_attribute2_tbl(i),
8783 					l_attribute3_tbl(i),
8784 					l_attribute4_tbl(i),
8785 					l_attribute5_tbl(i),
8786 					l_attribute6_tbl(i),
8787 					l_attribute7_tbl(i),
8788 					l_attribute8_tbl(i),
8789 					l_attribute9_tbl(i),
8790 					l_attribute10_tbl(i),
8791 					l_attribute_category_tbl(i),
8792 					l_expenditure_item_date_tbl(i),
8793 					l_acct_rate_date_tbl(i),
8794 					l_acct_rate_type_tbl(i),
8795 					l_acct_exchange_rate_tbl(i),
8796 					l_project_rate_date_tbl(i),
8797 					l_project_rate_type_tbl(i),
8798 					l_project_exchange_rate_tbl(i),
8799 					l_projfunc_cost_rate_date_tbl(i),
8800 					l_projfunc_cost_rate_type_tbl(i),
8801 					l_pfc_ex_rate_tbl(i)),-1) =
8802 					    nvl(PA_CLIENT_EXTN_BURDEN_SUMMARY.CLIENT_GROUPING(
8803 						null,
8804 						null,
8805 						exp.attribute1,
8806 						exp.attribute2,
8807 						exp.attribute3,
8808 						exp.attribute4,
8809 						exp.attribute5,
8810 						exp.attribute6,
8811 						exp.attribute7,
8812 						exp.attribute8,
8813 						exp.attribute9,
8814 						exp.attribute10,
8815 						exp.attribute_category,
8816 						exp.expenditure_item_date,
8817 						exp.acct_rate_date,
8818 						exp.acct_rate_type,
8819 						exp.acct_exchange_rate,
8820 						exp.project_rate_date,
8821 						exp.project_rate_type,
8822 						exp.project_exchange_rate,
8823 						exp.projfunc_cost_rate_date,
8824 						exp.projfunc_cost_rate_type,
8825 						exp.projfunc_cost_exchange_rate),-1)
8826 				AND nvl(PA_CLIENT_EXTN_BURDEN_RESOURCE.CLIENT_GROUPING(
8827 					l_job_id_tbl(i),
8828 					l_non_labor_resource_tbl(i),
8829 					l_nl_res_orgn_id_tbl(i),
8830 					l_wip_resource_id_tbl(i),
8831 					l_incurred_by_person_id_tbl(i),
8832 					l_inventory_item_id_tbl(i)), -1) =
8833 					   nvl(PA_CLIENT_EXTN_BURDEN_RESOURCE.CLIENT_GROUPING(
8834 						exp.job_id,
8835 						exp.non_labor_resource,
8836 						exp.organization_id,
8837 						exp.wip_resource_id,
8838 						expend.incurred_by_person_id,
8839 						exp.inventory_item_id),-1)
8840 		         UNION ALL
8841 		        SELECT l_doc_header_id_tbl(i),l_doc_distribution_id_tbl(i),l_expenditure_type_tbl(i)
8842 		          FROM dual
8843 		         WHERE l_cdl_line_type_tbl(i)  ='D' )
8844             AND bc_cm.transfer_status_code in ('P','R','X')
8845             AND bc_cm.document_type = 'AP'
8846             AND bc_cm.burden_cost_flag = 'R'
8847             AND ((bc_cm.parent_bc_packet_id IS NOT NULL AND l_cdl_line_type_tbl(i) ='R') OR l_cdl_line_type_tbl(i) ='D');
8848 
8849  	 IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
8850  	  pa_funds_control_pkg.log_message(p_msg_token1 => 'INTERFACE_TBC_BTC_COMT_UPDATE :Number of AP commitments updated '||SQL%ROWCOUNT);
8851  	  pa_funds_control_pkg.log_message(p_msg_token1 => 'INTERFACE_TBC_BTC_COMT_UPDATE : updating eligible AP commitments for liq encumbrance ');
8852 	 End if;
8853 
8854     	    FORALL i in 1..l_count_of_records
8855 	    UPDATE  pa_bc_commitments bc_cm
8856 	       SET  bc_cm.bc_event_id = l_cdl_acct_event_id_tbl(i),
8857 	            bc_cm.transfer_status_code = 'A',
8858 		    bc_cm.request_id  = p_request_id,
8859 		    bc_cm.transferred_date = SYSDATE,
8860 		    bc_cm.liquidate_gl_date = l_cdl_gl_Date_tbl(i),
8861 		    bc_cm.exp_item_id = DECODE (l_cdl_line_type_tbl(i),'R',l_Exp_Item_Id_tbl(i),bc_cm.exp_item_id)-- Bug 5076612
8862 	     WHERE (bc_cm.exp_item_id,bc_cm.expenditure_type)
8863 		IN ( SELECT peia.expenditure_item_id ,l_expenditure_type_tbl(i) -- Bug 5663343 : Handled transfer/split cases
8864                      FROM pa_expenditure_items_all peia
8865                      WHERE peia.TRANSFERRED_FROM_EXP_ITEM_ID IS NULL
8866                      START WITH peia.expenditure_item_id in (
8867 	                     SELECT  cdl_raw.expenditure_item_id
8868 		               FROM  PA_Cost_Distribution_lines  cdl_raw,
8869 			             Pa_Expenditure_Items ei_raw,
8870 			             pa_expenditures_all expend
8871 		              WHERE  cdl_raw.burden_sum_source_run_id = l_Burd_Sum_Dest_Run_Id_tbl(i)
8872 		                AND  cdl_raw.line_num = 1
8873 		                AND  cdl_raw.expenditure_item_id = ei_raw.expenditure_item_id
8874 		                AND  ei_raw.system_linkage_function in ('ST','OT','VI')
8875 		                AND  l_cdl_line_type_tbl(i) ='R'
8876                                 -- Below join conditions added for bug 5263721
8877 				AND ei_raw.expenditure_id = expend.expenditure_id
8878 				AND nvl(l_billable_flag_tbl(i), -1) = nvl(cdl_raw.billable_flag, -1)
8879 				AND nvl(l_project_id_tbl(i), -1) = nvl(cdl_raw.project_id, -1)
8880 				AND nvl(l_task_id_tbl(i), -1) = nvl(cdl_raw.task_id, -1)
8881 				AND nvl(l_pa_period_name_tbl(i), -1) = nvl(cdl_raw.pa_period_name, -1)
8882 				AND nvl(l_denom_currency_code_tbl(i), -1) = nvl(ei_raw.denom_currency_code, -1)
8883 				AND nvl(l_acct_currency_code_tbl(i), -1) = nvl(ei_raw.acct_currency_code, -1)
8884 				AND nvl(l_project_currency_code_tbl(i), -1) = nvl(ei_raw.project_currency_code, -1)
8885 				AND nvl(l_projfunc_currency_code_tbl(i), -1) = nvl(ei_raw.projfunc_currency_code, -1)
8886 				/* AND nvl(l_system_reference1_tbl(i), -1) = nvl(cdl_raw.system_reference1, -1) bug 5453131*/
8887 				AND nvl(l_person_type_tbl(i), -1) = nvl(expend.person_type, -1)
8888 				AND nvl(l_po_line_id_tbl(i), -1) = nvl(ei_raw.po_line_id, -1)
8889 				AND nvl(PA_CLIENT_EXTN_BURDEN_SUMMARY.CLIENT_GROUPING(
8890 					null,
8891 					null,
8892 					l_attribute1_tbl(i),
8893 					l_attribute2_tbl(i),
8894 					l_attribute3_tbl(i),
8895 					l_attribute4_tbl(i),
8896 					l_attribute5_tbl(i),
8897 					l_attribute6_tbl(i),
8898 					l_attribute7_tbl(i),
8899 					l_attribute8_tbl(i),
8900 					l_attribute9_tbl(i),
8901 					l_attribute10_tbl(i),
8902 					l_attribute_category_tbl(i),
8903 					l_expenditure_item_date_tbl(i),
8904 					l_acct_rate_date_tbl(i),
8905 					l_acct_rate_type_tbl(i),
8906 					l_acct_exchange_rate_tbl(i),
8907 					l_project_rate_date_tbl(i),
8908 					l_project_rate_type_tbl(i),
8909 					l_project_exchange_rate_tbl(i),
8910 					l_projfunc_cost_rate_date_tbl(i),
8911 					l_projfunc_cost_rate_type_tbl(i),
8912 					l_pfc_ex_rate_tbl(i)),-1) =
8913 					    nvl(PA_CLIENT_EXTN_BURDEN_SUMMARY.CLIENT_GROUPING(
8914 						null,
8915 						null,
8916 						ei_raw.attribute1,
8917 						ei_raw.attribute2,
8918 						ei_raw.attribute3,
8919 						ei_raw.attribute4,
8920 						ei_raw.attribute5,
8921 						ei_raw.attribute6,
8922 						ei_raw.attribute7,
8923 						ei_raw.attribute8,
8924 						ei_raw.attribute9,
8925 						ei_raw.attribute10,
8926 						ei_raw.attribute_category,
8927 						ei_raw.expenditure_item_date,
8928 						ei_raw.acct_rate_date,
8929 						ei_raw.acct_rate_type,
8930 						ei_raw.acct_exchange_rate,
8931 						ei_raw.project_rate_date,
8932 						ei_raw.project_rate_type,
8933 						ei_raw.project_exchange_rate,
8934 						ei_raw.projfunc_cost_rate_date,
8935 						ei_raw.projfunc_cost_rate_type,
8936 						ei_raw.projfunc_cost_exchange_rate),-1)
8937 				AND nvl(PA_CLIENT_EXTN_BURDEN_RESOURCE.CLIENT_GROUPING(
8938 					l_job_id_tbl(i),
8939 					l_non_labor_resource_tbl(i),
8940 					l_nl_res_orgn_id_tbl(i),
8941 					l_wip_resource_id_tbl(i),
8942 					l_incurred_by_person_id_tbl(i),
8943 					l_inventory_item_id_tbl(i)), -1) =
8944 					   nvl(PA_CLIENT_EXTN_BURDEN_RESOURCE.CLIENT_GROUPING(
8945 						ei_raw.job_id,
8946 						ei_raw.non_labor_resource,
8947 						ei_raw.organization_id,
8948 						ei_raw.wip_resource_id,
8949 						expend.incurred_by_person_id,
8950 						ei_raw.inventory_item_id),-1)
8951 		     )
8952                      CONNECT BY PRIOR peia.transferred_from_exp_item_id = peia.expenditure_item_id
8953 		     UNION ALL
8954 		     select l_Exp_Item_Id_tbl(i),l_expenditure_type_tbl(i)
8955 		       from  dual
8956 		      where l_system_linkage_function_tbl(i) IN ('ST','OT','VI')
8957 		        AND l_cdl_line_type_tbl(i) ='D')
8958 	       and bc_cm.transfer_status_code in ('P','R','X')
8959 	       and bc_cm.document_type = 'PO'
8960 	       and bc_cm.burden_cost_flag = 'R'
8961 	       and ((bc_cm.parent_bc_packet_id is not null AND l_cdl_line_type_tbl(i) ='R') OR  l_cdl_line_type_tbl(i) ='D');
8962 
8963  	 IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
8964  	  pa_funds_control_pkg.log_message(p_msg_token1 => 'INTERFACE_TBC_BTC_COMT_UPDATE :Number of PO commitments updated '||SQL%ROWCOUNT);
8965  	  pa_funds_control_pkg.log_message(p_msg_token1 => 'INTERFACE_TBC_BTC_COMT_UPDATE : End');
8966 	 End if;
8967 
8968        END IF; --IF l_count_of_records <> 0 THEN
8969 
8970 EXCEPTION
8971 WHEN OTHERS THEN
8972    IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
8973       pa_funds_control_pkg.log_message(p_msg_token1 => 'INTERFACE_TBC_BTC_COMT_UPDATE :Exception '||SQLERRM);
8974       x_result_code := 'Error';
8975   End if;
8976 
8977 END INTERFACE_TBC_BTC_COMT_UPDATE;
8978 
8979 -- ----------------------------------------------------------------------------+
8980 -- Function get_ratio will determine the burden to raw ratio.
8981 -- This is used in cursor po_amounts and pkt_po_amounts in procedure
8982 -- populate_burden_cost
8983 -- ----------------------------------------------------------------------------+
8984 Function get_ratio(p_document_header_id       in number,
8985                    p_document_distribution_id in number,
8986                    p_document_type            in varchar2,
8987                    p_mode                     in varchar2,
8988                    p_dr_cr                    in varchar2)
8989                    return number
8990 is
8991  l_document_header_id           pa_bc_packets.document_header_id%type;
8992  l_document_distribution_id     pa_bc_packets.document_distribution_id%type;
8993  l_document_type                pa_bc_packets.document_type%type;
8994  l_ratio                        pa_bc_packets.accounted_dr%type;
8995 Begin
8996  IF pa_funds_control_pkg.g_debug_mode = 'Y' THEN
8997   pa_funds_control_pkg.log_message(p_msg_token1 => 'Inside get_ratio');
8998   pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of p_mode is= '||p_mode);
8999  End if;
9000 
9001  If (p_document_header_id       <> nvl(l_document_header_id,-1) and
9002      p_document_distribution_id <> nvl(l_document_distribution_id,-1) and
9003      p_document_type            <> nvl(l_document_type,'XX')
9004      ) then
9005 
9006      l_document_header_id       := p_document_header_id;
9007      l_document_distribution_id := p_document_distribution_id;
9008      l_document_type            := p_document_type;
9009 
9010       If p_mode ='BCPKT' then
9011          -- Burden/Raw ...
9012          SELECT sum(decode(parent_bc_packet_id,null,0,
9013                            decode(p_dr_cr,'A',abs(nvl(accounted_dr,0)-nvl(accounted_cr,0)),
9014                                   'E',abs(nvl(entered_dr,0)-nvl(entered_cr,0)))))/
9015                 sum(decode(parent_bc_packet_id,null,
9016                            decode(p_dr_cr,'A',abs(nvl(accounted_dr,0)-nvl(accounted_cr,0)),
9017                                   'E',abs(nvl(entered_dr,0)-nvl(entered_cr,0)))
9018                            ,0))
9019          INTO   l_ratio
9020          FROM   pa_bc_packets pbc1
9021          WHERE  pbc1.packet_id = ( SELECT max(pbc.packet_id)
9022                                    FROM   pa_bc_packets pbc
9023                                    WHERE  pbc.document_distribution_id = l_document_distribution_id
9024                                    AND    pbc.document_header_id = l_document_header_id
9025                                    AND    pbc.document_type = l_document_type
9026                                    AND    pbc.parent_bc_packet_id is NULL
9027                                    AND    pbc.balance_posted_flag in ('N')
9028                                    AND    pbc.status_code in ('A','C')
9029                                    AND    substr(nvl(pbc.result_code,'P'),1,1) = 'P')
9030          AND  pbc1.document_distribution_id = l_document_distribution_id
9031          AND  pbc1.document_header_id = l_document_header_id
9032          AND  pbc1.document_type = l_document_type
9033          AND  pbc1.balance_posted_flag in ('N')
9034          AND  pbc1.status_code in ('A','C')
9035          AND  substr(nvl(pbc1.result_code,'P'),1,1) = 'P';
9036 
9037       ElsIf p_mode ='BCCMT' then
9038 
9039          SELECT sum(decode(parent_bc_packet_id,null,0,
9040                            decode(p_dr_cr,'A',abs(nvl(accounted_dr,0)-nvl(accounted_cr,0)),
9041                                   'E',abs(nvl(entered_dr,0)-nvl(entered_cr,0)))))/
9042                 sum(decode(parent_bc_packet_id,null,
9043                            decode(p_dr_cr,'A',abs(nvl(accounted_dr,0)-nvl(accounted_cr,0)),
9044                                   'E',abs(nvl(entered_dr,0)-nvl(entered_cr,0)))
9045                            ,0))
9046          INTO   l_ratio
9047          FROM pa_bc_commitments comm1
9048          WHERE comm1.packet_id = ( SELECT max(comm.packet_id)
9049                                    FROM pa_bc_commitments comm
9050                                    WHERE comm.document_distribution_id = l_document_distribution_id
9051                                    AND comm.document_header_id = l_document_header_id
9052                                    AND comm.document_type = l_document_type
9053                                    AND comm.parent_bc_packet_id is NULL)
9054          AND  comm1.document_distribution_id = l_document_distribution_id
9055          AND  comm1.document_header_id = l_document_header_id
9056          AND  comm1.document_type = l_document_type;
9057 
9058       End If;
9059    return l_ratio;
9060 
9061  Else
9062 
9063      return l_ratio;
9064 
9065  End If;
9066 
9067 End get_ratio;
9068 
9069 -------->6599207 ------As part of CC Enhancements
9070 PROCEDURE populate_plsql_tabs_CBC
9071 		(p_packet_id  IN number
9072 		,p_calling_module  IN varchar2
9073 		,p_reference1  IN  varchar2
9074 		,p_reference2  IN  varchar2
9075 		,p_mode        IN  varchar2) IS
9076 
9077         l_request_id      NUMBER := fnd_global.conc_request_id();
9078         l_program_id      NUMBER := fnd_global.conc_program_id();
9079         l_program_application_id NUMBER:= fnd_global.prog_appl_id();
9080         l_update_login    NUMBER := FND_GLOBAL.login_id;
9081         l_num_rows        NUMBER := 0;
9082         l_return_status    VARCHAR2(1);
9083 	l_debug_mode       VARCHAR2(10);
9084 
9085 	/*CURSOR cur_gl_pkts IS
9086                 SELECT  decode('Confirmed','CC_C_PAY',
9087                                 'Provisional','CC_P_PAY'
9088                                 ) document_type,
9089                         gl.last_update_date,
9090                         gl.last_updated_by,
9091                         gl.ledger_id set_of_books_id,
9092                         gl.je_source_name,
9093                         gl.je_category_name,
9094                         gl.reference1,
9095                         gl.reference2,
9096                         gl.reference3,
9097 			gl.reference4,
9098 			gl.reference5,
9099                         gl.actual_flag,
9100                         gl.period_name,
9101                         gl.period_year,
9102                         gl.period_num,
9103                         NVL(gl.entered_dr,0),
9104                         NVL(gl.entered_cr,0),
9105                         NVL(gl.accounted_dr,0),
9106                         NVL(gl.accounted_cr,0),
9107                         gl.ROWID,    --gl_row_bc_packet_row_id
9108                         gl.code_combination_id,
9109 			NULL , --reference1
9110 			NULL , --reference2
9111 			NULL  --reference3
9112                 FROM  gl_bc_packets gl
9113                 WHERE gl.packet_id = p_packet_id
9114                 AND   gl.je_source_name = 'Contract Commitment'
9115                		and gl.je_category_name in ('Confirmed','Provisional')
9116                		and EXISTS
9117               		( SELECT  'Project Related'
9118                 	FROM    pa_tasks  pkt,
9119                         	pa_projects_all pp,
9120                         	igc_cc_acct_lines igc,
9121                         	igc_cc_det_pf igcpf,
9122                         	igc_cc_headers_all igchead,
9123 				pa_implementations_all imp
9124                 	WHERE igc.cc_header_id = gl.reference1
9125                 	AND igchead.cc_header_id = igc.cc_header_id
9126                 	AND igcpf.cc_det_pf_line_id = gl.reference4
9127                 	AND igc.cc_acct_line_id = igcpf.cc_acct_line_id
9128                 	AND igc.project_id IS NOT NULL
9129                 	AND igc.project_id = pp.project_id
9130                 	AND igc.task_id = pkt.task_id
9131                 	AND pkt.project_id = pp.project_id
9132                 	AND nvl(pp.org_id, -99)  = nvl(imp.org_id, -99)
9133                 	AND imp.set_of_books_id = gl.ledger_Id
9134                         AND EXISTS
9135                               ( select 'Project Bdgt Ctrl enabled'
9136                                 from  pa_budget_types bdgttype
9137                                   ,pa_budgetary_control_options pbct
9138                                 where pbct.project_id = pp.project_id
9139                                 and pbct.BDGT_CNTRL_FLAG = 'Y'
9140                                 and (pbct.EXTERNAL_BUDGET_CODE = 'GL'
9141                                   OR
9142                                   pbct.EXTERNAL_BUDGET_CODE is NULL
9143                                  )
9144                                 and pbct.BUDGET_TYPE_CODE = bdgttype.budget_type_code
9145                                 and bdgttype.budget_amount_code = 'C'
9146                              )
9147                 	);*/
9148 
9149      CURSOR cur_gl_pkts IS
9150      SELECT  decode(igchead.CC_STATE,'PR','CC_P_PAY','CC_C_PAY') document_type,
9151              igc.last_update_date,
9152              igc.last_updated_by,
9153              igc.set_of_books_id,
9154              'Contract Commitment' JeSourceName,
9155              decode(igchead.CC_STATE,'PR','Provisional','Confirmed') Category,
9156              to_char(igc.cc_header_id),
9157              NULL,
9158              NULL,
9159              to_char(igc.cc_det_pf_line_id),
9160 	     NULL,
9161 	     igc.CC_TRANSACTION_DATE gl_date,
9162              igc.actual_flag,
9163              glp.period_name,
9164              glp.period_year,
9165              glp.period_num,
9166              NVL(igc.cc_func_dr_amt,0) entered_dr,
9167              NVL(igc.cc_func_cr_amt,0) entered_cr,
9168              NVL(igc.cc_func_dr_amt,0) accounted_dr,
9169              NVL(igc.cc_func_cr_amt,0) accounted_cr,
9170              igc.ROWID,
9171              igc.code_combination_id,
9172 	     igc.event_id,
9173              NULL ,
9174              NULL ,
9175              NULL
9176     FROM     igc_cc_interface igc,
9177              igc_cc_headers_all igchead,
9178              psa_bc_xla_events_gt pbgt,
9179              gl_period_statuses glp
9180     WHERE    pbgt.event_id = igc.event_id
9181     and      igc.cc_header_id = igchead.cc_header_id
9182     and      igc.budget_dest_flag = 'S'
9183     and      glp.application_id = 101
9184     and      glp.closing_status = 'O'
9185     and      glp.adjustment_period_flag = 'N'
9186     and      igc.cc_transaction_date between glp.start_date and glp.end_date
9187     and      glp.set_of_books_id = igc.set_of_books_id
9188     and      exists (select 1 from igc_cc_acct_lines igcc where igcc.cc_acct_line_id = igc.cc_acct_line_id
9189                      and project_id >0);
9190 
9191 
9192 BEGIN
9193 
9194         fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
9195         l_debug_mode := NVL(l_debug_mode, 'N');
9196 
9197         PA_DEBUG.SET_PROCESS( x_process => 'PLSQL'
9198                              ,x_write_file     => 'LOG'
9199                              ,x_debug_mode      => l_debug_mode
9200                              );
9201        pa_funds_control_pkg.log_message(p_msg_token1 => 'Inside populate_plsql_tabs_CBC');
9202        pa_funds_control_pkg.log_message(p_msg_token1 => 'Value of p_calling_module is='||p_calling_module);
9203 
9204 	-- Initialize the pl/sql tabs
9205 	init_plsql_tabs;
9206 
9207 	IF p_calling_module = 'GL' then
9208 
9209 		OPEN cur_gl_pkts;
9210 		FETCH cur_gl_pkts BULK COLLECT INTO
9211                         g_tab_doc_type,
9212                         g_tab_last_update_date,
9213                         g_tab_last_updated_by,
9214                         g_tab_set_of_books_id,
9215                         g_tab_je_source_name,
9216                         g_tab_je_category_name,
9217                         g_tab_reference1,
9218                         g_tab_reference2,
9219                         g_tab_reference3,
9220                         g_tab_reference4,
9221                         g_tab_reference5,
9222 			g_tab_gl_date,
9223                         g_tab_actual_flag,
9224                         g_tab_period_name,
9225                         g_tab_period_year,
9226                         g_tab_period_num,
9227                         g_tab_entered_dr,
9228                         g_tab_entered_cr,
9229                         g_tab_accounted_dr,
9230                         g_tab_accounted_cr,
9231                         g_tab_rowid,
9232                         g_tab_trxn_ccid,
9233 			g_tab_event_id,
9234 			g_tab_pkt_reference1,
9235                         g_tab_pkt_reference2,
9236                         g_tab_pkt_reference3;
9237 
9238 
9239 		CLOSE cur_gl_pkts;
9240 		pa_funds_control_pkg.log_message
9241 		 (p_msg_token1 => 'Number of rows copied to plsql tabs :'||g_tab_rowid.count);
9242 		If g_tab_rowid.EXISTS(1) then
9243 
9244 			pa_funds_control_pkg.log_message
9245 			 (p_msg_token1 => 'calling pa_multi_currency api to initialize the currency ');
9246 
9247         		-- initialize the accounting currency code,
9248         		pa_multi_currency.init;
9249 
9250         		--Get the accounting currency into a global variable.
9251         		g_acct_currency_code := pa_multi_currency.g_accounting_currency_code;
9252 
9253                         --insert these records into pa bc packets
9254                         pa_funds_control_pkg.log_message
9255                         (p_msg_token1 => 'calling create pkt lines api');
9256 
9257 			create_CBC_pkt_lines
9258 			   (p_calling_module   => p_calling_module
9259                            ,p_packet_id       => p_packet_id
9260                            ,p_reference1      => null
9261                            ,p_reference2      => null
9262 			   ,p_mode            => p_mode);
9263 		End if;
9264 
9265 	ELSIF p_calling_module = 'CBC' and p_reference2 is NOT NULL then
9266 
9267 
9268                         pa_funds_control_pkg.log_message
9269                          (p_msg_token1 => 'calling pa_multi_currency api to initialize the currency ');
9270 
9271                         -- initialize the accounting currency code,
9272                         pa_multi_currency.init;
9273 
9274                         --Get the accounting currency into a global variable.
9275                         g_acct_currency_code := pa_multi_currency.g_accounting_currency_code;
9276 
9277                         --insert these records into pa bc packets
9278                         pa_funds_control_pkg.log_message
9279                         (p_msg_token1 => 'calling create pkt lines api');
9280 
9281 			create_CBC_pkt_lines
9282 			   (p_calling_module   => p_calling_module
9283                            ,p_packet_id       => p_packet_id
9284                            ,p_reference1      => p_reference1
9285                            ,p_reference2      => p_reference2
9286 			   ,p_mode            => p_mode);
9287 
9288 	END IF;
9289 
9290 	RETURN;
9291 
9292 EXCEPTION
9293 	WHEN OTHERS THEN
9294 		if cur_gl_pkts%isopen then
9295 			close cur_gl_pkts;
9296 		end if;
9297 		RAiSE;
9298 
9299 END populate_plsql_tabs_CBC;
9300 -------->6599207 ------END
9301 
9302 -------->6599207 ------As part of CC Enhancements
9303 -- This procedure is created to initialize plsql tables
9304 -- so as to use them while creating bcpackets.
9305 PROCEDURE create_CBC_pkt_lines(p_calling_module   IN varchar2,
9306 			   p_packet_id        IN number
9307 			   ,p_reference1      IN VARCHAR2
9308 			   ,p_reference2      IN VARCHAR2
9309 			   ,p_mode            IN  varchar2) IS
9310 
9311         l_counter NUMBER :=0;
9312 
9313 	CURSOR cur_igc_details( p_reference1    IN varchar2
9314                                 ,p_reference2   IN varchar2
9315                                 ,p_reference3   IN varchar2
9316                                 ,p_reference4   IN varchar2
9317                                 ,p_reference5   IN varchar2
9318                                 ,p_doc_type     IN varchar2)  IS
9319                 SELECT  p_reference1 document_header_id,
9320                         igc.cc_acct_line_id document_distribution_id,
9321                         NULL budget_version_id,
9322                         igc.project_id,
9323                         igc.task_id,
9324                         igc.expenditure_type,
9325                         igc.expenditure_org_id,
9326                         trunc(igc.expenditure_item_date),
9327                         igchead.org_id,
9328                         'N' balance_posted_flag,
9329                         'T' funds_process_mode,
9330                         'N' burden_cost_flag,
9331                         NULL  result_code,
9332                         'I' status_code,
9333                         pa_funds_control_utils.get_fnd_reqd_flag(pp.project_id ,'STD') fck_reqd_flag,
9334 			null parent_bc_packet_id,
9335 	                NULL main_or_backing_code,
9336 	                pa_funds_control_pkg.check_bdn_on_sep_item(igc.project_id) burden_method_code,
9337 	                NULL budget_line_id,
9338                         NULL source_event_id,
9339 	                NULL distribution_type,
9340 	                NULL po_release_Id,
9341 	                PA_FUNDS_CONTROL_UTILS.get_encum_type_id(igc.project_id,'STD') enc_type_id,
9342 	                igchead.vendor_id,
9343 	                pa_bc_packets_s.nextval
9344                 FROM    pa_tasks  pkt,
9345                         pa_projects_all pp,
9346                         igc_cc_acct_lines igc,
9347                         igc_cc_det_pf igcpf,
9348                         igc_cc_headers_all igchead
9349                 WHERE igc.cc_header_id = p_reference1
9350                 AND igchead.cc_header_id = igc.cc_header_id
9351                 AND igcpf.cc_det_pf_line_id = p_reference4
9352                 AND igc.cc_acct_line_id = igcpf.cc_acct_line_id
9353                 AND igc.project_id IS NOT NULL
9354                 AND igc.project_id = pp.project_id
9355                 AND igc.task_id = pkt.task_id
9356                 AND pkt.project_id = pp.project_id
9357                 AND EXISTS ( select 'Project Bdgt Ctrl enabled'
9358                              from  pa_budget_types bdgttype
9359                                   ,pa_budgetary_control_options pbct
9360                              where pbct.project_id = pp.project_id
9361                              and pbct.BDGT_CNTRL_FLAG = 'Y'
9362                              and (pbct.EXTERNAL_BUDGET_CODE = 'GL'
9363                                   OR
9364                                   pbct.EXTERNAL_BUDGET_CODE is NULL
9365                                  )
9366                              and pbct.BUDGET_TYPE_CODE = bdgttype.budget_type_code
9367                              and bdgttype.budget_amount_code = 'C'
9368                            );
9369 
9370 
9371 	CURSOR cur_cbc_details(p_reference1  in  varchar2
9372 			        ,p_reference2 in varchar2) IS
9373                 SELECT
9374                         igci.last_update_date,
9375                         igci.last_updated_by,
9376                         igci.last_updated_by,
9377                         NULL budget_version_id,
9378                         igc.project_id,
9379                         igc.task_id,
9380                         igc.expenditure_type,
9381                         igc.expenditure_org_id,
9382                         trunc(igc.expenditure_item_date),
9383                         igci.set_of_books_id,
9384                         'Contract Commitment' JeSourceName, -- igci.je_source_name,
9385                         decode(igchead.CC_STATE,'PR','Provisional','Confirmed') Category,        -- igci.je_category_name,
9386                         decode(decode(igchead.CC_STATE,'PR','Provisional','Confirmed'),'Confirmed','CC_C_CO',
9387                                                      'Provisional','CC_P_CO')
9388 				document_type,
9389                         igci.cc_header_id,
9390                         igci.cc_acct_line_id,
9391                         igci.actual_flag,
9392  		        igci.cc_acct_line_id line_id,
9393 			NULL event_id,
9394 			igchead.vendor_id,
9395 			NULL main_or_backing_code,
9396 			pa_funds_control_pkg.check_bdn_on_sep_item(igc.project_id) burden_method_code,
9397 			NULL budget_line_id,
9398 			igci.event_id source_event_id,
9399 			igc.CC_ACCT_ENCMBRNC_DATE gl_date,
9400 			NULL distribution_type,
9401 			NULL po_release_Id,
9402 			PA_FUNDS_CONTROL_UTILS.get_encum_type_id(igc.project_id,'STD') enc_type_id,
9403                         gl.period_name, --igci.period_name,
9404                         NULL  period_year, --igci.period_year,
9405                         NULL  period_num, --igci.period_num,
9406                         NULL  result_code, -- result_code
9407                         'P'   status_code, -- status_code,
9408                         NVL(igci.cc_func_dr_amt,0),
9409                         NVL(igci.cc_func_cr_amt,0),
9410                         NVL(igci.cc_func_dr_amt,0),
9411                         NVL(igci.cc_func_cr_amt,0),
9412                         igci.ROWID,    --gl_row_bc_packet_row_id
9413                         'N' balance_posted_flag,
9414                         'T' funds_process_mode, -- T - transaction B- base line
9415                         igci.code_combination_id,
9416                         'N'  burden_cost_flag,  -- original transaction (raw)
9417                         igchead.org_id,
9418                         pa_funds_control_utils.get_fnd_reqd_flag
9419                         (pp.project_id ,'STD') fck_reqd_flag,
9420                         null parent_bc_packet_id,
9421                         decode(igci.je_category_name,'Confirmed','CC_C_CO',
9422                                                      'Provisional','CC_P_CO')
9423                                 pkt_reference1,
9424                         igci.cc_header_id pkt_reference2,
9425                         igci.cc_acct_line_id pkt_reference3,
9426 			pa_bc_packets_s.nextval
9427                 FROM
9428                         pa_tasks  pkt,
9429                         pa_projects_all pp,
9430                         igc_cc_interface  igci,
9431                         igc_cc_acct_lines igc,
9432                         igc_cc_headers_all igchead,
9433 			gl_period_statuses gl,
9434 			pa_implementations_all imp
9435                 WHERE igc.cc_header_id = p_reference2
9436                 AND p_reference1 = 'CC'
9437                 AND igchead.cc_header_id = igc.cc_header_id
9438                 AND igc.project_id IS NOT NULL
9439                 AND igc.project_id = pp.project_id
9440                 AND igc.cc_header_id = igci.cc_header_id
9441                 AND igc.cc_acct_line_id = igci.cc_acct_line_id
9442                 AND pkt.task_id = igc.task_id
9443                 AND pkt.project_id = pp.project_id
9444 		AND gl.application_id = 101
9445 		ANd gl.set_of_books_id = igci.set_of_books_id
9446 		AND gl.ADJUSTMENT_PERIOD_FLAG <> 'Y'
9447 		AND trunc(igci.cc_transaction_date)
9448 			between gl.start_date  and gl.end_date
9449 		AND nvl(pp.org_id, -99)  = nvl(imp.org_id, -99)
9450                 AND imp.set_of_books_id = (SELECT imp1.set_of_books_id
9451 					   FROM pa_implementations_all imp1 where org_id = pp.org_id)
9452                 AND EXISTS ( select 'Project Bdgt Ctrl enabled'
9453                              from  pa_budget_types bdgttype
9454                                   ,pa_budgetary_control_options pbct
9455                              where pbct.project_id = pp.project_id
9456                              and pbct.BDGT_CNTRL_FLAG = 'Y'
9457                              and pbct.EXTERNAL_BUDGET_CODE = 'CC'
9458                              and pbct.BUDGET_TYPE_CODE = bdgttype.budget_type_code
9459                              and bdgttype.budget_amount_code = 'C'
9460                            );
9461 BEGIN
9462 
9463 
9464 	pa_funds_control_pkg.log_message(p_msg_token1 => 'Inside create_bc_pkt api calling module ['
9465 					 ||p_calling_module||'] g_tab_rowid.count['||g_tab_rowid.count||']'  );
9466 
9467 	IF p_calling_module = 'GL' and g_tab_rowid.EXISTS(1) then
9468 
9469 		FOR i IN g_tab_rowid.FIRST ..g_tab_rowid.LAST LOOP
9470 		        l_counter := l_counter + 1;
9471 			pa_funds_control_pkg.log_message(p_msg_token1 => 'inside for loop l_counter'||l_counter);
9472 
9473                     IF g_tab_doc_type(i)  in ('CC_C_PAY','CC_P_PAY') then
9474                         pa_funds_control_pkg.log_message
9475 				(p_msg_token1 =>'doc type is '||g_tab_doc_type(i));
9476                                 g_tab_p_bc_packet_id(i) := null;
9477                                 OPEN cur_igc_details(g_tab_reference1(i),
9478                                                      g_tab_reference2(i),
9479                                                      g_tab_reference3(i),
9480                                                      g_tab_reference4(i),
9481                                                      g_tab_reference5(i),
9482                                                      g_tab_doc_type(i));
9483 
9484                                 FETCH cur_igc_details INTO
9485                                         g_tab_doc_header_id(i),
9486                                         g_tab_doc_distribution_id(i),
9487                                         g_tab_budget_version_id(i),
9488                                         g_tab_project_id(i),
9489                                         g_tab_task_id(i),
9490                                         g_tab_exp_type(i),
9491                                         g_tab_exp_org_id(i),
9492                                         g_tab_exp_item_date(i),
9493                                         g_tab_org_id(i),
9494                                         g_tab_balance_posted_flag(i),
9495                                         g_tab_funds_process_mode(i),
9496                                         g_tab_burden_cost_flag(i),
9497                                         g_tab_result_code(i),
9498                                         g_tab_status_code(i),
9499                                         g_tab_fck_reqd_flag(i),
9500 					g_tab_p_bc_packet_id(i),
9501                                         g_tab_main_or_backing_code(i),
9502 			                g_tab_burden_method_code(i),
9503 			                g_tab_budget_line_id(i),
9504 			                g_tab_source_event_id(i),
9505 			                g_tab_distribution_type(i),
9506 			                g_tab_po_release_id(i),
9507 			                g_tab_enc_type_id(i),
9508 			                g_tab_vendor_id(i),
9509 			                g_tab_bc_packet_id(i);
9510 
9511                                 pa_funds_control_pkg.log_message(p_msg_token1 => 'fetch cursor');
9512                                 g_tab_p_bc_packet_id(i) := null;
9513 				g_tab_doc_line_id(i) := NULL;
9514                                 /* added for bug fix: 3086398 */
9515                                 IF cur_igc_details%found then
9516 				    g_tab_pkt_reference1(i) := g_tab_doc_type(i);
9517                                     g_tab_pkt_reference2(i) := g_tab_doc_header_id(i);
9518                                     g_tab_pkt_reference3(i) := g_tab_doc_distribution_id(i);
9519 
9520                                     g_tab_budget_version_id(i) := pa_funds_control_utils2.GET_DRAFTORBASELINE_BDGTVER
9521                                                                      (g_tab_project_id(i),'GL','BASELINE');
9522                                     If (g_tab_budget_version_id(i) is NULL ) Then
9523                                             g_tab_result_code(i)   := 'F166';
9524                                             g_tab_status_code(i)   := 'R';
9525                                             g_tab_fck_reqd_flag(i) := 'Y';
9526                                             g_tab_budget_version_id(i) := NVL(pa_funds_control_utils2.GET_DRAFTORBASELINE_BDGTVER
9527                                                 (g_tab_project_id(i),'GL','DRAFT'),-9999);
9528 						 -- Added as part of bug 13638904
9529                                             fnd_message.set_name('PA','PA_BC_NO_BASELINE_BGT');
9530                                             PSA_BC_XLA_PVT.psa_xla_error('PA_BC_NO_BASELINE_BGT',g_tab_source_event_id(i));
9531 
9532                                     End If;
9533                                     pa_funds_control_pkg.log_message(p_msg_token1 => 'igc cursor Bdgt_Version['
9534 						||g_tab_budget_version_id(i)||']');
9535                                     /* end of bug fix:3086398 */
9536                                 Elsif cur_igc_details%NOTfound then
9537                                         pa_funds_control_pkg.log_message(p_msg_token1 => 'cur not found');
9538 					assign_plsql_tabs(p_counter => l_counter,
9539                                                           p_fck_reqd_flag =>null);
9540                                 End if;
9541 
9542                                 CLOSE cur_igc_details;
9543 				null;
9544 			END IF;
9545 
9546 		  END LOOP;
9547 
9548                /* pa_funds_control_pkg.log_message(p_msg_token1 => 'calling Load_pkt API');
9549                 If g_tab_rowid.EXISTS(1) then
9550                         Load_pkts(p_packet_id, p_mode);
9551                 End if;*/
9552 
9553                 pa_funds_control_pkg.log_message(p_msg_token1 => 'end of create_pkt_lines api');
9554 
9555 	ELSIF p_calling_module = 'CBC' and p_reference1 is not null and p_reference2 is NOT NULL then
9556 
9557 		pa_funds_control_pkg.log_message(p_msg_token1 => 'calling module ['||p_calling_module||
9558 		']p_reference1 ['||p_reference1|| ']p_reference2 ['||p_reference2|| ']opening cursor cur_cbc_details');
9559 
9560 		OPEN cur_cbc_details(p_reference1,p_reference2);
9561 		FETCH cur_cbc_details BULK COLLECT INTO
9562                         g_tab_last_update_date,
9563                         g_tab_last_updated_by,
9564                         g_tab_last_updated_by,
9565                         g_tab_budget_version_id,
9566                         g_tab_project_id,
9567                         g_tab_task_id,
9568                         g_tab_exp_type,
9569                         g_tab_exp_org_id,
9570                         g_tab_exp_item_date,
9571                         g_tab_set_of_books_id,
9572                         g_tab_je_source_name,
9573                         g_tab_je_category_name,
9574                         g_tab_doc_type,
9575                         g_tab_doc_header_id,
9576                         g_tab_doc_distribution_id,
9577                         g_tab_actual_flag,
9578 			g_tab_doc_line_id,
9579 			g_tab_event_id,
9580 			g_tab_vendor_id,
9581 			g_tab_main_or_backing_code,
9582 			g_tab_burden_method_code,
9583 			g_tab_budget_line_id,
9584 			g_tab_source_event_id,
9585 			g_tab_gl_date,
9586 			g_tab_distribution_type,
9587 			g_tab_po_release_id,
9588 			g_tab_enc_type_id,
9589                         g_tab_period_name,
9590                         g_tab_period_year, --igci.period_year,
9591                         g_tab_period_num, --igci.period_num,
9592                         g_tab_result_code, -- result_code
9593                         g_tab_status_code, -- status_code,
9594                         g_tab_entered_dr,
9595                         g_tab_entered_cr,
9596                         g_tab_accounted_dr,
9597                         g_tab_accounted_cr,
9598                         g_tab_rowid,
9599                         g_tab_balance_posted_flag,
9600                         g_tab_funds_process_mode, -- T - transaction B- base line
9601                         g_tab_trxn_ccid,
9602                         g_tab_burden_cost_flag,  -- original transaction (raw)
9603                         g_tab_org_id,
9604                         g_tab_fck_reqd_flag,
9605                         g_tab_p_bc_packet_id,
9606 			g_tab_pkt_reference1,
9607 			g_tab_pkt_reference2,
9608 			g_tab_pkt_reference3,
9609 			g_tab_bc_packet_id;
9610 
9611 		CLOSE cur_cbc_details;
9612 
9613 
9614 		If g_tab_rowid.EXISTS(1) then
9615                         /* added for bug fix: 3086398 */
9616                         FOR i IN g_tab_rowid.FIRST .. g_tab_rowid.LAST LOOP
9617 				  g_tab_doc_line_id(i) := NULL;
9618                                   g_tab_budget_version_id(i) := pa_funds_control_utils2.GET_DRAFTORBASELINE_BDGTVER
9619                                                                      (g_tab_project_id(i),'CC','BASELINE');
9620                                   If (g_tab_budget_version_id(i) is NULL ) Then
9621                                             g_tab_result_code(i)   := 'F166';
9622                                             g_tab_status_code(i)   := 'R';
9623                                             g_tab_fck_reqd_flag(i) := 'Y';
9624                                             g_tab_budget_version_id(i) := NVL(pa_funds_control_utils2.GET_DRAFTORBASELINE_BDGTVER
9625                                                 (g_tab_project_id(i),'CC','DRAFT'),-9999);
9626 						 -- Added as part of bug 13638904
9627                                             fnd_message.set_name('PA','PA_BC_NO_BASELINE_BGT');
9628                                             PSA_BC_XLA_PVT.psa_xla_error('PA_BC_NO_BASELINE_BGT',g_tab_source_event_id(i));
9629 
9630                                   End If;
9631                         END LOOP;
9632                         /* end of bug fix:3086398 */
9633 			pa_funds_control_pkg.log_message(p_msg_token1 =>'cur found calling Load_pkt API');
9634 			Load_pkts(p_packet_id, p_mode, 'CBC');
9635 
9636 
9637 		Else
9638 			pa_funds_control_pkg.log_message(p_msg_token1 => ' cur_cbc_details not found');
9639 			null;
9640 		End if;
9641 		pa_funds_control_pkg.log_message(p_msg_token1 => 'end of create_pkt_lines api');
9642 
9643 	END IF;
9644 
9645 END create_CBC_pkt_lines;
9646 -------->6599207 ------END
9647 
9648 -----------------------------------------------------------------
9649 --This api initializes the plsql tables at the specified index
9650 -------->6599207 ------As part of CC Enhancements
9651 -----------------------------------------------------------------
9652 PROCEDURE assign_plsql_tabs(p_counter IN NUMBER,
9653 			    p_fck_reqd_flag  varchar2 default null) IS
9654 
9655 BEGIN
9656 
9657 	g_tab_doc_header_id(p_counter) := null;
9658 	g_tab_doc_line_id(p_counter) := null;
9659 	g_tab_doc_distribution_id(p_counter) := null;
9660 	g_tab_budget_version_id(p_counter) := null;
9661 	g_tab_project_id(p_counter) := null;
9662 	g_tab_task_id(p_counter) := null;
9663 	g_tab_exp_type(p_counter) := null;
9664 	g_tab_exp_org_id(p_counter) := null;
9665 	g_tab_exp_item_date(p_counter) := null;
9666 	g_tab_org_id(p_counter) := null;
9667 	g_tab_balance_posted_flag(p_counter) := null;
9668 	g_tab_funds_process_mode(p_counter) := null;
9669 	g_tab_burden_cost_flag(p_counter) := null;
9670 	g_tab_result_code(p_counter) := null;
9671 	g_tab_status_code(p_counter) := 'P';
9672 	g_tab_p_bc_packet_id(p_counter) := null;
9673 	g_tab_fck_reqd_flag(p_counter) := nvl(p_fck_reqd_flag,'N');
9674 
9675 EXCEPTION
9676 
9677 	when others then
9678 		raise;
9679 
9680 END assign_plsql_tabs;
9681 
9682 END PA_FUNDS_CONTROL_PKG1;