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