DBA Data[Home] [Help]

PACKAGE BODY: APPS.PA_TRX_IMPORT

Source


1 PACKAGE BODY PA_TRX_IMPORT AS
2 /* $Header: PAXTTRXB.pls 120.78.12010000.9 2008/12/01 02:15:24 prabsing ship $ */
3 
4   last_empno       VARCHAR2(30)  DEFAULT NULL;
5   last_proj        VARCHAR2(25)  DEFAULT NULL;
6   last_task        VARCHAR2(25)  DEFAULT NULL;
7   last_etype       VARCHAR2(30)  DEFAULT NULL;
8   current_expend   VARCHAR2(2000) DEFAULT NULL;
9   current_expend2  VARCHAR2(2000) DEFAULT NULL;
10   current_period   DATE		 DEFAULT NULL;
11 
12 
13   current_system_linkage   VARCHAR2(30) DEFAULT NULL;
14 
15   G_trx_source     VARCHAR2(30)  DEFAULT NULL;
16   G_eclass         VARCHAR2(30)  DEFAULT NULL;
17   G_trx_link       VARCHAR2(30)  DEFAULT NULL;
18   G_trx_costed     VARCHAR2(1)   DEFAULT NULL;
19   G_trx_start      DATE          DEFAULT NULL;
20   G_trx_end        DATE          DEFAULT NULL;
21   G_emp_org_oride  VARCHAR2(1)   DEFAULT NULL;
22 
23   G_trx_predef_flag     VARCHAR2(1)   DEFAULT NULL;
24   G_allow_adj_flag      VARCHAR2(1)   DEFAULT NULL;
25   G_gl_accted_flag      VARCHAR2(1)   DEFAULT NULL;
26   G_allow_dup_flag      VARCHAR2(1)   DEFAULT NULL;
27   /* G_skip_tc_flag        VARCHAR2(1)   DEFAULT NULL;
28      Removed for bug 1299909 as this has been moved to sepcs of this package. */
29   G_burdened_flag       VARCHAR2(1)   DEFAULT NULL;
30 
31   G_etype_link     VARCHAR2(30)  DEFAULT NULL;
32   G_etype_start    DATE          DEFAULT NULL;
33   G_etype_end      DATE          DEFAULT NULL;
34   G_etype_cr	    NUMBER(22,5)  DEFAULT 1;
35   G_etype_cost_rate_flag VARCHAR2(1) DEFAULT NULL;
36 
37   G_etype_active   BOOLEAN       DEFAULT FALSE ;
38   G_etec_start     DATE          DEFAULT NULL  ;
39   G_etec_end       DATE          DEFAULT NULL  ;
40   G_etype_labor_flag  VARCHAR2(1)   DEFAULT NULL;
41 
42   G_person_id      NUMBER(15)    DEFAULT NULL;
43   G_org_id         NUMBER(15)    DEFAULT NULL;
44   G_job_id         NUMBER(15)    DEFAULT NULL;
45   G_task_id        NUMBER(15)    DEFAULT NULL;
46   G_project_id     NUMBER(15)    DEFAULT NULL;
47   G_adj_item_id    NUMBER(15)    DEFAULT NULL;
48   G_user           NUMBER(15)    DEFAULT NULL;
49 
50   G_Proj_bcost_flag  VARCHAR2(1) DEFAULT NULL ;
51 
52   G_nlro_id        NUMBER(15)    DEFAULT NULL;
53   G_nlr_etype      VARCHAR2(30)  DEFAULT NULL;
54   G_nlr_start      DATE          DEFAULT NULL;
55   G_nlr_end        DATE          DEFAULT NULL;
56   G_nlro_start     DATE          DEFAULT NULL;
57   G_nlro_end       DATE          DEFAULT NULL;
58 
59   G_lcm            VARCHAR2(20)  DEFAULT NULL;
60   G_burden_compile_set_id  	    NUMBER DEFAULT NULL;
61   G_compiled_multiplier             NUMBER := 0;
62 
63   -- 5235363   R12.PJ:XB4:DEV:APL:EXP ITEM DATE VALIDATIONS FOR SUPPLIER COST.
64   --
65   g_paapimp_validate_dt    varchar2(1) := 'Y' ;
66 
67   -- MC Changes
68   G_accounting_currency_code VARCHAR2(15) DEFAULT NULL;
69   G_allow_acct_user_rate  VARCHAR2(1) DEFAULT NULL;
70   G_allow_foreign_curr_txn VARCHAR2(1) DEFAULT NULL;
71   G_allow_proj_user_rate  VARCHAR2(1) DEFAULT NULL;
72   G_allow_copy_acct_cost_flag    VARCHAR2(1) DEFAULT NULL;
73   G_project_currency_code	 VARCHAR2(15) DEFAULT NULL;
74   G_default_rate_type VARCHAR2(30)  DEFAULT NULL;
75   G_proj_rate_date DATE		 DEFAULT NULL;
76   G_project_rate_type gl_daily_conversion_types.conversion_type%TYPE
77                           DEFAULT NULL;
78   G_acct_rate_type gl_daily_conversion_types.conversion_type%TYPE DEFAULT NULL;
79   G_raw_cost       NUMBER        DEFAULT NULL;
80 
81   /* Trx_Import Enhancement */
82   G_override_to_org_id  NUMBER   DEFAULT NULL;
83   /*G_orig_exp_txn_reference1 VARCHAR2(30) DEFAULT NULL;
84   G_orig_user_exp_txn_reference VARCHAR2(30) DEFAULT NULL;  */
85   /* Size of the two variables commented above have been modified to varchar2(60) as fix for bug 1504289*/
86   G_orig_exp_txn_reference1 VARCHAR2(60) DEFAULT NULL;
87   G_orig_user_exp_txn_reference VARCHAR2(60) DEFAULT NULL;
88   G_vendor_id		NUMBER DEFAULT NULL;
89   G_Vendor_Site_id  NUMBER DEFAULT NULL;
90   G_previous_vendor_number VARCHAR2(30) DEFAULT NULL;
91   G_orig_exp_txn_reference2 VARCHAR2(60) DEFAULT NULL;
92   G_orig_exp_txn_reference3 VARCHAR2(60) DEFAULT NULL;
93   /* End of Enhancment      */
94 
95   X_ei_id          NUMBER(15)    DEFAULT NULL;
96 
97   G_CDL_SYSTEM_REFERENCE2      NUMBER;
98   G_CDL_SYSTEM_REFERENCE3      NUMBER;
99   G_CDL_SYSTEM_REFERENCE4      pa_transaction_interface_all.cdl_system_reference4%TYPE; --2339216-apdisc; Commented for bug 4281765
100   /* REL12-AP Lines uptake
101   ** Support for cdl_system_reference5
102   ** cdl_system_reference5 to store invoice distribution ID
103   */
104   G_CDL_SYSTEM_REFERENCE5      NUMBER;
105   G_MOAC_ORG_ID                NUMBER ;
106 
107 
108   -- SST changes: New global variables
109   G_pre_processing_extn    pa_transaction_sources.pre_processing_extension%TYPE
110                             DEFAULT NULL;
111   G_post_processing_extn   pa_transaction_sources.post_processing_extension%TYPE
112                             DEFAULT NULL;
113 
114   G_reversed_orig_txn_reference PA_TRANSACTION_INTERFACE_ALL.reversed_orig_txn_reference%TYPE;
115 
116   /* Bug 4107560 */
117   G_prev_ORIG_TRAN_REF	PA_TRANSACTION_INTERFACE_ALL.ORIG_TRANSACTION_REFERENCE%TYPE;
118   G_WIP_RESOURCE_ID					PA_TRANSACTION_INTERFACE_ALL.WIP_RESOURCE_ID%TYPE;
119 
120 
121   G_project_type_class     PA_PROJECT_TYPES_ALL.PROJECT_TYPE_CLASS_CODE%TYPE;
122   /* added for the bug# 1428216, starts here */
123   G_burden_amt_display_method     PA_PROJECT_TYPES_ALL.BURDEN_AMT_DISPLAY_METHOD%TYPE;
124   /* added for the bug# 1428216, ends here */
125   -- End SST Changes
126 
127   -- IC Changes
128   G_CrossChargeType      pa_expenditure_items_all.cc_cross_charge_type%TYPE;
129   G_CrossChargeCode      pa_expenditure_items_all.cc_cross_charge_code%TYPE;
130   G_PrvdrOrganizationId  hr_organization_units.organization_id%TYPE;
131   G_RecvrOrganizationId  hr_organization_units.organization_id%TYPE;
132   G_RecvrOrgId           hr_organization_units.organization_id%TYPE;
133   G_BrowLentDistCode     pa_expenditure_items_all.cc_bl_distributed_code%TYPE;
134   G_IcProcessed_Code     pa_expenditure_items_all.cc_ic_processed_code%TYPE;
135   -- END IC Changes
136   current_etype_classcode    VARCHAR2(100) DEFAULT NULL;
137 
138   -- Oct 2001 Enhanced Period Processing
139   -- Start EPP Changes
140   G_PaDate              pa_cost_distribution_lines_all.pa_date%TYPE;
141   G_PaPeriodName        pa_cost_distribution_lines_all.pa_period_name%TYPE;
142   G_RecvrPaDate         pa_cost_distribution_lines_all.recvr_pa_date%TYPE;
143   G_RecvrPaPeriodName   pa_cost_distribution_lines_all.recvr_pa_period_name%TYPE;
144   G_GlPeriodName        pa_cost_distribution_lines_all.gl_period_name%TYPE;
145   G_RecvrGlDate         pa_cost_distribution_lines_all.recvr_gl_date%TYPE;
146   G_RecvrGlPeriodName   pa_cost_distribution_lines_all.recvr_gl_period_name%TYPE;
147   G_SobId               pa_implementations_all.set_of_books_id%TYPE;
148   G_RecvrSobId          pa_implementations_all.set_of_books_id%TYPE;
149  -- G_FSIO_ENABLED        varchar2(1);  --FSIO Changes
150 
151   Prev_SobId      NUMBER;
152   Prev_OrgID      NUMBER;
153   Prev_RecvrSobId NUMBER;
154   Prev_RecvrOrgID NUMBER;
155   -- End EPP Changes
156 
157   --Start PA-I Changes
158   G_projfunc_cost_rate_type gl_daily_conversion_types.conversion_type%TYPE  DEFAULT NULL;
159   G_Assignment_Id NUMBER;
160   G_Work_Type_Id  NUMBER;
161   G_Tp_Amt_Type_Code VARCHAR2(30);
162   --End PA-I Changes
163 
164   --Start PA-J Period-End Accrual Changes
165   G_GlDate           DATE;
166   G_AccDate          DATE;
167   G_RecvrAccDate     DATE;
168   G_RevAccDate       DATE;
169   G_RevRecvrAccDate  DATE;
170   G_RevPaDate        pa_cost_distribution_lines_all.pa_date%TYPE;
171   G_RevPaPeriodName  pa_cost_distribution_lines_all.pa_period_name%TYPE;
172   G_RevGlDate        pa_cost_distribution_lines_all.gl_date%TYPE;
173   G_RevGlPeriodName  pa_cost_distribution_lines_all.gl_period_name%TYPE;
174   G_RevRecvrPaDate   pa_cost_distribution_lines_all.recvr_pa_date%TYPE;
175   G_RevRecvrPaPdName pa_cost_distribution_lines_all.recvr_pa_period_name%TYPE;
176   G_RevRecvrGlDate   pa_cost_distribution_lines_all.recvr_gl_date%TYPE;
177   G_RevRecvrGlPdName pa_cost_distribution_lines_all.recvr_gl_period_name%TYPE;
178   --End PA-J Period-End Accrual Changes
179 
180   --Start PA-K Changes
181   G_Process_Funds_Flag  VARCHAR2(1);
182   l_Expend1             VARCHAR2(2000) DEFAULT NULL;
183   l_Expend2             VARCHAR2(2000) DEFAULT NULL;
184   G_Group_Name          VARCHAR2(240); /* Bug#2373198 Increased size from 80 to 240 */
185 
186   G_MOrg                VARCHAR2(1);
187   G_PrjInfoPrjId        NUMBER;
188   G_PrjInfoBCostFlag    VARCHAR2(1);
189   G_PrjInfoTypeClass    PA_PROJECT_TYPES_ALL.PROJECT_TYPE_CLASS_CODE%TYPE;
190   G_PrjInfoBdDisplay    PA_PROJECT_TYPES_ALL.BURDEN_AMT_DISPLAY_METHOD%TYPE;
191   G_PrjInfoTotBdFlag    PA_PROJECT_TYPES_ALL.TOTAL_BURDEN_FLAG%TYPE;
192   G_NewTxnPrjId         NUMBER;
193   --G_NewTxnsAllowed      VARCHAR2(1);
194   G_EClassInfoEtype     VARCHAR2(30);
195   /* bug #3134359 changed the size of G_EClassInfoSysLink to 30 from 3
196      as this is the size allowed in the base table pa_system_linkages.function,
197      size 30 is allowed in the front end also,ideally this should work with 3
198      if the values are from lookup */
199   --G_EClassInfoSysLink   VARCHAR2(3);
200   G_EClassInfoSysLink   VARCHAR2(30);
201   G_ETypeInfoEtype      VARCHAR2(30);
202   G_ETypeInfoDate       DATE;
203   G_NlrInfoNlr          VARCHAR2(20);
204   G_NlrInfoNlroId       NUMBER;
205   G_CurrInfoPrjCurrCode  VARCHAR2(15);
206   G_CurrInfoPrjRtType    VARCHAR2(30);
207   G_CurrInfoPrjFCurrCode VARCHAR2(15);
208   G_CurrInfoPrjFRtType   VARCHAR2(30);
209   G_CurrInfoTaskId       NUMBER;
210   G_OrgNameEmpNum        VARCHAR2(30);
211   G_OrgNameDate          DATE;
212   G_OrgNameOrgName       hr_all_organization_units.name%TYPE;
213   G_OrgNameBGName        hr_all_organization_units.name%TYPE;
214   G_OrgNameBGId          NUMBER;
215   G_PrevCCID             NUMBER;
216   G_PrevRetVal           NUMBER;
217   G_Total_Burden_Flag    VARCHAR2(1);
218 
219   G_Debug_Mode           VARCHAR2(1) ;
220 
221   --added variable for cost blue-print project
222   G_gl_posted_flag VARCHAR2(1)   DEFAULT NULL;
223 
224   PG_DEBUG varchar2(1) := NVL(FND_PROFILE.value('PA_DEBUG_MODE'), 'N');
225 
226   P_BTC_SRC_RESRC varchar2(1) := NVL(FND_PROFILE.value('PA_RPT_BTC_SRC_RESRC'), 'N'); -- 4057874
227 
228 -- R12 funds management uptake : Below global variables stores adjusting expenditures data
229 -- which will be accessed by fundscheck autonomous API's
230 
231 g_xface_project_id_tbl          PA_PLSQL_DATATYPES.IdTabTyp;
232 g_xface_task_id_tbl             PA_PLSQL_DATATYPES.IdTabTyp;
233 g_xface_exp_type_tbl            PA_PLSQL_DATATYPES.Char30TabTyp;
234 g_xface_ei_date_tbl             PA_PLSQL_DATATYPES.DateTabTyp;
235 g_xface_exp_org_id_tbl          PA_PLSQL_DATATYPES.IdTabTyp;
236 g_xface_bud_ver_id_tbl          PA_PLSQL_DATATYPES.IdTabTyp;
237 g_xface_Entered_Cr_tbl          PA_PLSQL_DATATYPES.NumTabTyp;
238 g_xface_Entered_Dr_tbl          PA_PLSQL_DATATYPES.NumTabTyp;
239 g_xface_acct_Cr_tbl             PA_PLSQL_DATATYPES.NumTabTyp;
240 g_xface_acct_Dr_tbl             PA_PLSQL_DATATYPES.NumTabTyp;
241 g_xface_Txn_Ccid_tbl            PA_PLSQL_DATATYPES.IdTabTyp;
242 g_xface_org_id_tbl              PA_PLSQL_DATATYPES.IdTabTyp;
243 g_xface_Txn_interface_tbl       PA_PLSQL_DATATYPES.IdTabTyp;
244 
245 
246 g_sob_Id_tbl              PA_PLSQL_DATATYPES.IdTabTyp;
247 g_Period_Year_tbl         PA_PLSQL_DATATYPES.NumTabTyp;
248 g_project_id_tbl          PA_PLSQL_DATATYPES.IdTabTyp;
249 g_task_id_tbl             PA_PLSQL_DATATYPES.IdTabTyp;
250 g_exp_type_tbl            PA_PLSQL_DATATYPES.Char30TabTyp;
251 g_ei_date_tbl             PA_PLSQL_DATATYPES.DateTabTyp;
252 g_exp_org_id_tbl          PA_PLSQL_DATATYPES.IdTabTyp;
253 g_bud_ver_id_tbl          PA_PLSQL_DATATYPES.IdTabTyp;
254 g_budget_line_id_tbl      PA_PLSQL_DATATYPES.IdTabTyp;  -- R12 Funds Management uptake
255 g_Document_Type_tbl       PA_PLSQL_DATATYPES.Char10TabTyp;
256 g_Doc_Header_Id_tbl       PA_PLSQL_DATATYPES.IdTabTyp;
257 g_Doc_dist_Id_tbl         PA_PLSQL_DATATYPES.IdTabTyp;
258 g_Entered_Cr_tbl          PA_PLSQL_DATATYPES.NumTabTyp;
259 g_Entered_Dr_tbl          PA_PLSQL_DATATYPES.NumTabTyp;
260 g_acct_Cr_tbl             PA_PLSQL_DATATYPES.NumTabTyp;
261 g_acct_Dr_tbl             PA_PLSQL_DATATYPES.NumTabTyp;
262 g_Actual_Flag_tbl         PA_PLSQL_DATATYPES.Char1TabTyp;
263 g_Txn_Ccid_tbl            PA_PLSQL_DATATYPES.IdTabTyp;
264 g_Je_Catg_Name_tbl        PA_PLSQL_DATATYPES.Char30TabTyp;
265 g_Je_sorce_Name_tbl       PA_PLSQL_DATATYPES.Char30TabTyp;
266 g_org_id_tbl              PA_PLSQL_DATATYPES.IdTabTyp;
267 g_Pa_Date_tbl             PA_PLSQL_DATATYPES.DateTabTyp;
268 g_packet_id_tbl           PA_PLSQL_DATATYPES.IdTabTyp;
269 g_bc_packet_id_tbl        PA_PLSQL_DATATYPES.IdTabTyp;
270 g_bc_parent_pkt_id_tbl    PA_PLSQL_DATATYPES.IdTabTyp;
271 g_enc_type_id_tbl         PA_PLSQL_DATATYPES.IdTabTyp;
272 g_doc_hdr_id_2_tbl        PA_PLSQL_DATATYPES.IdTabTyp;
273 g_doc_dist_type_tbl       PA_PLSQL_DATATYPES.Char30TabTyp;
274 g_bc_comt_id_tbl          PA_PLSQL_DATATYPES.IdTabTyp;
275 
276 
277 -- R12 funds management uptake : End
278 
279 PROCEDURE log_message(p_message in VARCHAR2, p_mode in NUMBER DEFAULT 0);
280 
281   --End PA-K Changes
282 
283   --Bug 2905892
284   G_FC_Gl_Date      DATE;
285   G_FC_Period_Name  pa_bc_packets.period_name%Type;
286   G_FC_Period_Year  pa_bc_packets.period_year%type; --REL12
287   G_PrevFCGLDate    DATE;
288   G_PrevFCPdName    pa_bc_packets.period_name%Type;
289 
290   PROCEDURE tr_import_funds_check (p_pa_date               IN  DATE,
291                                    p_txn_source            IN VARCHAR2,
292                                    p_acct_raw_cost         IN NUMBER,
293 				   p_adj_exp_item_id       IN NUMBER,
294 				   p_txn_interface_id      IN NUMBER,
295 				   p_fc_document_type      IN VARCHAR2,
296                                    x_packet_id            OUT NOCOPY NUMBER ,
297                                    x_error_message_code   OUT NOCOPY VARCHAR2 ,
298                                    x_error_stage          OUT NOCOPY VARCHAR2 ,
299                                    x_return_status        OUT NOCOPY VARCHAR2);
300 
301   PROCEDURE tieback_fc_records ( x_return_status   OUT NOCOPY VARCHAR2,
302                                  p_calling_mode     IN VARCHAR2) ;
303 
304   --PA-J Receipt Accrual Changes
305   --Added the below new procedures for funds check
306   PROCEDURE ap_funds_check (
307           p_txn_source            IN VARCHAR2,
308           p_acct_raw_cost         IN NUMBER,
309           p_packet_id             IN NUMBER,
310           p_sys_ref2              IN NUMBER,
311           p_sys_ref3              IN NUMBER,
312 	  p_sys_ref5              IN NUMBER,        --REL12
313 	  p_adj_exp_item_id       IN NUMBER,        --REL12
314 	  p_txn_interface_id      IN NUMBER,        --REL12
315 	  p_fc_document_type      IN VARCHAR2,      --REL12
316           x_error_message_code    OUT NOCOPY VARCHAR2,
317           x_error_stage           OUT NOCOPY VARCHAR2,
318           x_return_status         OUT NOCOPY VARCHAR2) ;
319 
320   PROCEDURE ap_po_funds_check (
321              p_txn_source            IN VARCHAR2,
322              p_acct_raw_cost         IN NUMBER,
323              p_packet_id             IN NUMBER,
324              p_po_hdr_id             IN NUMBER,
325              p_po_dist_id            IN NUMBER,
326              p_inv_id                IN NUMBER,
327 	     p_inv_dist_id           IN NUMBER, --REL12
328              p_dist_line             IN NUMBER,
329 	     p_adj_exp_item_id       IN NUMBER,        --REL12
330 	     p_txn_interface_id      IN NUMBER,        --REL12
331 	     p_fc_document_type      IN VARCHAR2,      --REL12
332              p_base_qty_var_amt      IN NUMBER,
333              p_base_amt_var_amt      IN NUMBER,
334              x_error_message_code    OUT NOCOPY VARCHAR2,
335              x_error_stage           OUT NOCOPY VARCHAR2,
336              x_return_status         OUT NOCOPY VARCHAR2);
337 
338   PROCEDURE po_funds_check (
339              p_txn_source            IN VARCHAR2,
340              p_acct_raw_cost         IN NUMBER,
341              p_packet_id             IN NUMBER,
342              p_sys_ref2              IN NUMBER,
343              p_sys_ref3              IN NUMBER,
344 	     p_sys_ref4              IN NUMBER,        -- Bug 5530897 : Added the parameter p_sys_ref4.
345 	     p_adj_exp_item_id       IN NUMBER,        --REL12
346 	     p_txn_interface_id      IN NUMBER,        --REL12
347 	     p_fc_document_type      IN VARCHAR2,      --REL12
348              x_error_message_code    OUT NOCOPY VARCHAR2,
349              x_error_stage           OUT NOCOPY VARCHAR2,
350              x_return_status         OUT NOCOPY VARCHAR2);
351 
352   --2339216-apdisc
353   PROCEDURE ap_disc_funds_check (
354           p_txn_source            IN VARCHAR2,
355           p_acct_raw_cost         IN NUMBER,
356           p_packet_id             IN NUMBER,
357           p_sys_ref2              IN NUMBER,
358           p_sys_ref3              IN NUMBER,
359 	  p_sys_ref4              IN VARCHAR2, --REL12
360  	  p_sys_ref5              IN NUMBER, --REL12
361 	  p_adj_exp_item_id       IN NUMBER,        --REL12
362 	  p_txn_interface_id      IN NUMBER,        --REL12
363 	  p_fc_document_type      IN VARCHAR2,      --REL12
364           x_error_message_code    OUT NOCOPY VARCHAR2,
365           x_error_stage           OUT NOCOPY VARCHAR2,
366           x_return_status         OUT NOCOPY VARCHAR2) ;
367   PROCEDURE ap_cash_based_funds_check (
368           p_txn_source            IN VARCHAR2,
369           p_acct_raw_cost         IN NUMBER,
370           p_packet_id             IN NUMBER,
371           p_sys_ref2              IN NUMBER,        --REL12
372           p_sys_ref4              IN VARCHAR2,        --REL12
373 	  p_sys_ref5              IN NUMBER,        --REL12
374 	  p_adj_exp_item_id       IN NUMBER,        --REL12
375 	  p_txn_interface_id      IN NUMBER,        --REL12
376 	  p_fc_document_type      IN VARCHAR2,      --REL12
377           x_error_message_code    OUT NOCOPY VARCHAR2,
378           x_error_stage           OUT NOCOPY VARCHAR2,
379           x_return_status         OUT NOCOPY VARCHAR2);
380 
381 PROCEDURE insert_ap_bc_packets(p_packet_id             IN NUMBER,
382                             p_sys_ref2              IN NUMBER,
383 			    p_sys_ref4              IN VARCHAR2,
384                             p_sys_ref5              IN NUMBER,
385                             p_acct_raw_cost         IN NUMBER,
386                             p_acct_bur_cost         IN NUMBER,
387                             p_fc_document_type      IN VARCHAR2,
388                             p_txn_source            IN VARCHAR2,
389                             p_adj_exp_item_id       IN NUMBER,
390 			    p_txn_interface_id      IN NUMBER);
391 
392 PROCEDURE insert_ap_bc_pkt_autonomous
393                            (p_packet_id             IN NUMBER,
394                             p_sys_ref2              IN NUMBER,
395 			    p_sys_ref4              IN VARCHAR2,
396                             p_sys_ref5              IN NUMBER,
397                             p_acct_raw_cost         IN NUMBER,
398                             p_acct_bur_cost         IN NUMBER,
399                             p_fc_document_type      IN VARCHAR2,
400                             p_txn_source            IN VARCHAR2,
401                             p_adj_exp_item_id       IN NUMBER,
402 			    p_txn_interface_id      IN NUMBER,
403 			    p_comm_fc_req           IN VARCHAR2,
404                             p_act_fc_req            IN VARCHAR2,
405                             p_adj_act_fc_req        IN VARCHAR2);
406 
407 PROCEDURE insert_cash_ap_bc_packets(p_packet_id             IN NUMBER,
408                                     p_sys_ref2              IN NUMBER,
409                                     p_sys_ref5              IN NUMBER,
410                                     p_acct_raw_cost         IN NUMBER,
411                                     p_fc_document_type      IN VARCHAR2,
412                                     p_txn_source            IN VARCHAR2,
413                                     p_adj_exp_item_id       IN NUMBER,
414 			            p_txn_interface_id      IN NUMBER,
415             			    p_cash_pay_to_relieve   IN NUMBER DEFAULT 0);
416 
417 PROCEDURE insert_cash_ap_bc_pkt_auto
418                                    (p_packet_id             IN NUMBER,
419                                     p_sys_ref2              IN NUMBER,
420                                     p_sys_ref5              IN NUMBER,
421                                     p_acct_raw_cost         IN NUMBER,
422                                     p_fc_document_type      IN VARCHAR2,
423                                     p_txn_source            IN VARCHAR2,
424                                     p_adj_exp_item_id       IN NUMBER,
425 			            p_txn_interface_id      IN NUMBER,
426 			            p_cash_pay_to_relieve   IN NUMBER DEFAULT 0,
427             			    p_comm_fc_req           IN VARCHAR2,
428                                     p_act_fc_req            IN VARCHAR2,
429                                     p_adj_act_fc_req        IN VARCHAR2);
430 
431 PROCEDURE insert_po_bc_packets(p_packet_id             IN NUMBER,
432                             p_sys_ref2              IN NUMBER,
433 			    p_sys_ref4              IN NUMBER,
434                             p_sys_ref3              IN NUMBER,
435                             p_acct_raw_cost         IN NUMBER,
436 			    p_cmt_raw_cost          IN NUMBER,
437                             p_fc_document_type      IN VARCHAR2,
438                             p_txn_source            IN VARCHAR2,
439                             p_adj_exp_item_id       IN NUMBER,
440 			    p_txn_interface_id      IN NUMBER);
441 
442  /* Bug 5396719 : Modified the parameters p_comm_fc_req,p_act_fc_req and p_adj_act_fc_req
443     of the PROCEDURE insert_po_bc_packets_auto to be of VARCHAR2 datatype. */
444 
445 PROCEDURE insert_po_bc_packets_auto
446                            (p_packet_id             IN NUMBER,
447                             p_sys_ref2              IN NUMBER,
448                             p_sys_ref4              IN NUMBER,
449 			    p_sys_ref3              IN NUMBER,
450                             p_acct_raw_cost         IN NUMBER,
451 			    p_cmt_raw_cost          IN NUMBER,
452                             p_fc_document_type      IN VARCHAR2,
453                             p_txn_source            IN VARCHAR2,
454                             p_adj_exp_item_id       IN NUMBER,
455 			    p_txn_interface_id      IN NUMBER,
456 			    p_comm_fc_req           IN VARCHAR2,
457                             p_act_fc_req            IN VARCHAR2,
458                             p_adj_act_fc_req        IN VARCHAR2);
459 
460 
461   -- Log messages changes
462   --l_debug_mode           VARCHAR2(20) := 'N';
463 
464   RESOURCE_BUSY     EXCEPTION;
465   PRAGMA EXCEPTION_INIT( RESOURCE_BUSY, -0054 );
466 
467   i       BINARY_INTEGER DEFAULT 0;
468 
469   PROCEDURE init_xface_plsql_tables IS
470   BEGIN
471     g_xface_project_id_tbl.delete;
472     g_xface_task_id_tbl.delete;
473     g_xface_exp_type_tbl.delete;
474     g_xface_ei_date_tbl.delete;
475     g_xface_exp_org_id_tbl.delete;
476     g_xface_bud_ver_id_tbl.delete;
477     g_xface_Entered_Cr_tbl.delete;
478     g_xface_Entered_Dr_tbl.delete;
479     g_xface_acct_Cr_tbl.delete;
480     g_xface_acct_Dr_tbl.delete;
481     g_xface_Txn_Ccid_tbl.delete;
482     g_xface_org_id_tbl.delete;
483     g_xface_Txn_interface_tbl.delete;
484   END init_xface_plsql_tables;
485 
486   /* Deleting plsql tables  */
487 
488 PROCEDURE clear_plsql_tables IS
489    BEGIN
490 
491      g_sob_Id_tbl.delete;
492      g_Period_Year_tbl.delete;
493      g_project_id_tbl.delete;
494      g_task_id_tbl.delete;
495      g_exp_type_tbl.delete;
496      g_ei_date_tbl.delete;
497      g_exp_org_id_tbl.delete;
498      g_bud_ver_id_tbl.delete;
499      g_budget_line_id_tbl.delete; -- R12 Funds Management uptake
500      g_Document_Type_tbl.delete;
501      g_Doc_Header_Id_tbl.delete;
502      g_Doc_dist_Id_tbl.delete;
503      g_Entered_Cr_tbl.delete;
504      g_Entered_Dr_tbl.delete;
505      g_acct_Cr_tbl.delete;
506      g_acct_Dr_tbl.delete;
507      g_Actual_Flag_tbl.delete;
508      g_Txn_Ccid_tbl.delete;
509      g_Je_Catg_Name_tbl.delete;
510      g_Je_sorce_Name_tbl.delete;
511      g_org_id_tbl.delete;
512      g_Pa_Date_tbl.delete;
513      g_bc_packet_id_tbl.delete;
514      g_packet_id_tbl.delete;
515      g_bc_parent_pkt_id_tbl.delete;
516      g_enc_type_id_tbl.delete;
517      g_doc_hdr_id_2_tbl.delete;
518      g_doc_dist_type_tbl.delete;
519      g_bc_comt_id_tbl.delete;
520 
521 END clear_plsql_tables;
522 
523   PROCEDURE  GetTrxSrcInfo ( X_trx_src  IN VARCHAR2 )
524   IS
525   BEGIN
526 
527     pa_cc_utils.set_curr_function('GetTrxSrcInfo');
528 
529     -- Modifying this query to check for transaction_source rather than
530     -- user transaction source
531     -- 697638 Bug fix
532     --
533     -- Removed cost_burdened_flag, this flag is obsolete for 11.5, the
534     -- functionality is replaced by allow_burden_flag
535 
536     SELECT  ts.transaction_source
537     ,       ts.system_linkage_function
538     ,       DECODE( ts.system_linkage_function,
539                 'ST',   'PT',
540                 'ER', 'PE',
541                 'VI','VI','PU' )/* Added for bug 2041741*/
542     ,       ts.costed_flag
543     ,       ts.start_date_active
544     ,       ts.end_date_active
545     ,       predefined_flag
546     ,       allow_adjustments_flag
547     ,       gl_accounted_flag
548     ,       nvl(posted_flag, 'N') -- get posted_flag
549     ,       allow_duplicate_reference_flag
550     ,       skip_tc_validation_flag
551     ,       allow_emp_org_override_flag
552     ,       allow_burden_flag
553     ,       pre_processing_extension  -- SST change
554     ,       post_processing_extension -- SST change
555     ,       nvl(batch_size,0)
556     ,       nvl(process_funds_check,'N')
557       INTO
558             G_trx_source
559     ,       G_trx_link
560     ,       G_eclass
561     ,       G_trx_costed
562     ,       G_trx_start
563     ,       G_trx_end
564     ,       G_trx_predef_flag
565     ,       G_allow_adj_flag
566     ,       G_gl_accted_flag
567     ,       G_gl_posted_flag
568     ,       G_allow_dup_flag
569     ,       G_skip_tc_flag
570     ,       G_emp_org_oride
571     ,       G_burdened_flag
572     ,       G_pre_processing_extn
573     ,       G_post_processing_extn
574     ,       G_Batch_Size
575     ,       G_Process_Funds_Flag
576       FROM
577             pa_transaction_sources ts
578      WHERE
579             ts.transaction_source = X_trx_src;
580 
581     pa_cc_utils.reset_curr_function;
582   EXCEPTION
583     WHEN  NO_DATA_FOUND  THEN
584       G_trx_link := NULL;
585       pa_cc_utils.reset_curr_function;
586   END  GetTrxSrcInfo;
587 
588   -- Get implementation currency information
589   --
590   PROCEDURE GetImpCurrInfo
591   IS
592   BEGIN
593      pa_cc_utils.set_curr_function('GetImpCurrInfo');
594      IF PG_DEBUG = 'Y' THEN
595      pa_debug.G_err_Stage := 'call to pa_multi_currency.init';
596         log_message('log_message: ' || pa_debug.G_err_Stage);
597      END IF;
598 
599      pa_multi_currency.init;
600 
601      IF PG_DEBUG = 'Y' THEN
602      pa_debug.G_err_stage := 'Assigning currency code and rate type to local variables';
603         log_message('log_message: ' || pa_debug.G_err_Stage);
604      END IF;
605      G_accounting_currency_code := pa_multi_currency.G_accounting_currency_code;
606      G_default_rate_type := pa_multi_currency.G_rate_type;
607 
608      pa_cc_utils.reset_curr_function;
609   EXCEPTION
610    WHEN NO_DATA_FOUND THEN
611      G_default_rate_type := NULL;
612      G_accounting_currency_code := NULL;
613      pa_cc_utils.reset_curr_function;
614   END GetImpCurrInfo;
615 
616   --
617   -- Get project/task currency information
618   --
619   PROCEDURE GetProjCurrInfo( X_task_id    IN NUMBER,
620 			X_project_currency_code IN OUT NOCOPY VARCHAR2,
621 			X_project_rate_type IN OUT NOCOPY VARCHAR2,
622                         -- PA-I Changes :  Added proj func attr
623                         X_projfunc_currency_code IN OUT NOCOPY VARCHAR2,
624                         X_projfunc_cost_rate_type IN OUT NOCOPY VARCHAR2)
625   IS
626   BEGIN
627 
628     pa_cc_utils.set_curr_function('GetProjCurrInfo');
629     IF PG_DEBUG = 'Y' THEN
630     pa_debug.G_err_stage := 'Calling get_project_rate_type';
631        log_message('log_message: ' || pa_debug.G_err_Stage);
632     END IF;
633 
634     If (G_CurrInfoTaskId = X_task_id) Then
635 
636         IF PG_DEBUG = 'Y' THEN
637         pa_debug.G_err_stage := 'Using Cached Values';
638            log_message('log_message: ' || pa_debug.G_err_Stage);
639         END IF;
640 
641         X_project_currency_code   := G_CurrInfoPrjCurrCode;
642         X_project_rate_type       := G_CurrInfoPrjRtType;
643         X_projfunc_currency_code  := G_CurrInfoPrjFCurrCode;
644         X_projfunc_cost_rate_type := G_CurrInfoPrjFRtType;
645 
646         G_CurrInfoTaskId := X_task_id;
647 
648     Else
649 
650         IF PG_DEBUG = 'Y' THEN
651         pa_debug.G_err_stage := 'Selecting Values';
652            log_message('log_message: ' || pa_debug.G_err_Stage);
653         END IF;
654 
655         -- PA-I Changes : The API get_project_rate_type has been renamed to get_proj_rate_type
656         pa_multi_currency_txn.get_proj_rate_type(P_task_id => X_task_id,
657 						P_project_currency_code => X_project_currency_code,
658 						P_project_rate_type => X_project_rate_type);
659 
660         IF PG_DEBUG = 'Y' THEN
661         pa_debug.G_err_stage := 'Calling get_projfunc_rate_type';
662            log_message('log_message: ' || pa_debug.G_err_Stage);
663         END IF;
664 
665         -- PA-I Changes :  Calling API to get Proj Functional Currency Code only
666         pa_multi_currency_txn.get_projfunc_cost_rate_type(P_task_id => X_task_id,
667 						P_projfunc_currency_code => X_projfunc_currency_code,
668 						P_projfunc_cost_rate_type => X_projfunc_cost_rate_type);
669 
670         G_CurrInfoPrjCurrCode  := X_project_currency_code;
671         G_CurrInfoPrjRtType    := X_project_rate_type;
672         G_CurrInfoPrjFCurrCode := X_projfunc_currency_code;
673         G_CurrInfoPrjFRtType   := X_projfunc_cost_rate_type;
674 
675         G_CurrInfoTaskId := X_task_id;
676 
677 
678     End If;
679 
680     pa_cc_utils.reset_curr_function;
681 
682   END GetProjCurrInfo;
683 
684   --
685   -- default functional and project currency conversion attributes
686   --
687   PROCEDURE DefaultCurrAttributes(X_acct_currency_code IN VARCHAR2,
688                                  X_project_currency_code IN VARCHAR2,
689                                  X_acct_rate_date        IN OUT NOCOPY DATE,
690                                  X_acct_rate_type  IN OUT NOCOPY VARCHAR2,
691                                  X_acct_exchange_rate IN OUT NOCOPY NUMBER,
692                                  X_project_rate_date IN OUT NOCOPY DATE,
693                                  X_project_rate_type IN OUT NOCOPY VARCHAR2,
694                                  X_project_exchange_rate IN OUT NOCOPY NUMBER )
695   IS
696 
697   BEGIN
698 
699       pa_cc_utils.set_curr_function('DefaultCurrAttributes');
700 
701       IF ( X_acct_currency_code = X_project_currency_code) THEN
702 
703            X_acct_rate_type := nvl(X_project_rate_type,X_acct_rate_type);
704            X_acct_rate_date := nvl(X_project_rate_date,X_acct_rate_date);
705            X_acct_exchange_rate := nvl(X_project_exchange_rate, X_acct_exchange_rate);
706 
707            X_project_rate_type := nvl(X_project_rate_type,X_acct_rate_type);
708            X_project_rate_date := nvl(X_project_rate_date,X_acct_rate_date);
709            X_project_exchange_rate := nvl(X_project_exchange_rate, X_acct_exchange_rate) ;
710 
711 
712       END IF;
713       pa_cc_utils.reset_curr_function;
714 
715   END DefaultCurrAttributes;
716 
717   /*  PA-I Changes
718     For un-accounted transactions we need not call DefaultCurrAttributes.
719     This is because the checks that are being performed here will be anyway
720     performed during costing in pa_multi_currency_txn. To avoid redundant code
721     we are not performing these checks in PA_TRX_IMPORT.
722     Please see prior versions for the changed DefaultCurrAttributes if needed.
723   */
724 
725   PROCEDURE GetVendorId(P_vendor_number IN VARCHAR2)
726   IS
727   BEGIN
728 
729       pa_cc_utils.set_curr_function('GetVendorId');
730 
731       SELECT vendor_ID INTO G_vendor_id
732       FROM po_vendors
733       WHERE segment1 = p_vendor_number;
734 
735       pa_cc_utils.reset_curr_function;
736 
737   EXCEPTION WHEN others THEN
738     G_vendor_id := NULL;
739     pa_cc_utils.reset_curr_function;
740   END GetVendorId;
741 
742   --
743   --BUG : 4696351 PJ.R12:DI4:APLINES: VENDOR INFORMATION NOT IMPORTED DURING TRANSACTION IMPORT
744   --
745   PROCEDURE GetVendorID ( p_person_id IN NUMBER )
746   IS
747   BEGIN
748 
749      pa_cc_utils.set_curr_function('GetVendorId for an employee (person_id):'||p_person_id);
750 
751 	 select vendor_id
752 	   into g_vendor_id
753 	   from po_vendors
754 	  where employee_id = p_person_id
755 	    and vendor_type_lookup_code = 'EMPLOYEE' ;
756 
757       pa_cc_utils.reset_curr_function;
758 
759   EXCEPTION WHEN others THEN
760     G_vendor_id := NULL;
761     pa_cc_utils.reset_curr_function;
762   END GetVendorId ;
763 
764   PROCEDURE  GetEtypeInfo( X_etype  IN VARCHAR2
765                          , X_date   IN DATE )
766   IS
767   BEGIN
768 
769     pa_cc_utils.set_curr_function('GetEtypeInfo');
770 
771     If (G_ETypeInfoEtype = X_etype AND trunc(G_ETypeInfoDate) = trunc(X_date)) Then
772 
773         IF PG_DEBUG = 'Y' THEN
774         pa_debug.G_err_Stage := 'Cached Values';
775            log_message('log_message: ' || pa_debug.G_err_Stage);
776         END IF;
777 
778        G_ETypeInfoEtype := X_etype;
779        G_ETypeInfoDate  := X_date;
780 
781     Else
782         IF PG_DEBUG = 'Y' THEN
783         pa_debug.G_err_Stage := 'Selecting Values';
784            log_message('log_message: ' || pa_debug.G_err_Stage);
785         END IF;
786 
787         SELECT
788          /* et.system_linkage_function  */ /* Commented for Bug#2726242 */
789             et.start_date_active
790     ,       et.end_date_active
791     ,       NVL(ecr.cost_rate, 1)
792     ,       et.cost_rate_flag
793          INTO
794          /* G_etype_link */ /* Commented for Bug#2726242 */
795             G_etype_start
796     ,       G_etype_end
797     ,       G_etype_cr
798     ,       G_etype_cost_rate_flag
799          FROM
800             pa_expenditure_cost_rates ecr
801     ,       pa_expenditure_types et
802         WHERE
803                et.expenditure_type = ecr.expenditure_type (+)
804           AND  X_date BETWEEN ecr.start_date_active (+)
805                        AND nvl(ecr.end_date_active (+), X_date)
806           AND et.expenditure_type = X_etype;
807 
808        G_etype_active := TRUE ;
809 
810        G_ETypeInfoEtype := X_etype;
811        G_ETypeInfoDate  := X_date;
812 
813     End If;
814 
815     pa_cc_utils.reset_curr_function;
816 
817   EXCEPTION
818     WHEN  NO_DATA_FOUND  THEN
819       /* G_etype_link  := NULL; */ /* Commented for Bug#2726242 */
820       G_etype_start := NULL;
821       G_etype_end   := NULL;
822       G_etype_cr    := NULL;
823       G_etype_active := FALSE ;
824 
825       G_ETypeInfoEtype := X_etype;
826       G_ETypeInfoDate  := X_date;
827 
828       pa_cc_utils.reset_curr_function;
829 
830   END GetEtypeInfo;
831 
832   PROCEDURE  GetEtypeEclassInfo( X_etype  IN VARCHAR2
833                                , X_system_linkage IN VARCHAR2 )
834   IS
835   BEGIN
836 
837     pa_cc_utils.set_curr_function('GetEtypeEclassInfo');
838 
839     If (G_EClassInfoEtype = X_etype AND G_EClassInfoSysLink = X_system_linkage) then
840 
841         IF PG_DEBUG = 'Y' THEN
842         pa_debug.G_err_Stage := 'Cached Values';
843            log_message('log_message: ' || pa_debug.G_err_Stage);
844         END IF;
845 
846         G_EClassInfoEtype := X_etype;
847         G_EClassInfoSysLink := X_system_linkage;
848 
849     Else
850 
851         IF PG_DEBUG = 'Y' THEN
852         pa_debug.G_err_Stage := 'Selecting Values';
853            log_message('log_message: ' || pa_debug.G_err_Stage);
854         END IF;
855 
856        SELECT
857                ets.system_linkage_function
858 	      ,st.labor_non_labor_flag
859               ,ets.start_date_active
860               ,ets.end_date_active
861          INTO
862                G_etype_link
863        ,       G_etype_labor_flag
864        ,       G_etec_start
865        ,       G_etec_end
866          FROM  pa_system_linkages st,
867 	       pa_expend_typ_sys_links ets
868         WHERE  st.function = ets.system_linkage_function
869           AND  ets.system_linkage_function = X_system_linkage
870           AND  ets.expenditure_type        = X_etype ;
871 
872         G_etype_active := TRUE ;
873 
874         G_EClassInfoEtype := X_etype;
875         G_EClassInfoSysLink := X_system_linkage;
876 
877     End If;
878 
879     pa_cc_utils.reset_curr_function;
880 
881   EXCEPTION
882     WHEN  NO_DATA_FOUND  THEN
883       G_etype_link  := NULL;
884       G_etec_start := NULL;
885       G_etec_end   := NULL;
886       G_etype_active := FALSE ;
887       G_EClassInfoEtype := X_etype;
888       G_EClassInfoSysLink := X_system_linkage;
889 
890       pa_cc_utils.reset_curr_function;
891 
892   END GetEtypeEclassInfo;
893 
894   PROCEDURE  GetNlrInfo( X_nlr      IN VARCHAR2
895                        , X_nlro_id  IN NUMBER )
896   IS
897   BEGIN
898 
899     pa_cc_utils.set_curr_function('GetNlrInfo');
900     IF PG_DEBUG = 'Y' THEN
901     pa_debug.G_err_Stage := 'select from pa_non_labor_resources';
902        log_message('log_message: ' || pa_debug.G_err_Stage);
903     END IF;
904 
905     If (G_NlrInfoNlr = X_nlr) Then
906 
907         IF PG_DEBUG = 'Y' THEN
908         pa_debug.G_err_Stage := 'Cached Values';
909            log_message('log_message: ' || pa_debug.G_err_Stage);
910         END IF;
911 
912         G_NlrInfoNlr := X_nlr;
913 
914     Else
915 
916         IF PG_DEBUG = 'Y' THEN
917         pa_debug.G_err_Stage := 'Selecting Values';
918            log_message('log_message: ' || pa_debug.G_err_Stage);
919         END IF;
920 
921         SELECT
922             nlr.expenditure_type
923     ,       nlr.start_date_active
924     ,       nlr.end_date_active
925           INTO
926             G_nlr_etype
927     ,       G_nlr_start
928     ,       G_nlr_end
929           FROM
930             pa_non_labor_resources nlr
931          WHERE
932             nlr.non_labor_resource = X_nlr;
933 
934       G_NlrInfoNlr := X_nlr;
935 
936     End If;
937 
938     BEGIN
939       IF PG_DEBUG = 'Y' THEN
940       pa_debug.G_err_stage := 'select from pa_non_labor_resource_orgs';
941          log_message('log_message: ' || pa_debug.G_err_Stage);
942       END IF;
943 
944       If (G_NlrInfoNlr = X_nlr and G_NlrInfoNlroId = X_nlro_id) Then
945 
946           G_NlrInfoNlroId := X_nlro_id;
947 
948       Else
949 
950           SELECT
951               nlro.start_date_active
952       ,       nlro.end_date_active
953             INTO
954               G_nlro_start
955       ,       G_nlro_end
956             FROM
957               pa_non_labor_resource_orgs nlro
958            WHERE
959               nlro.organization_id = X_nlro_id
960              AND  nlro.non_labor_resource = X_nlr;
961 
962           G_NlrInfoNlroId := X_nlro_id;
963 
964       End If;
965 
966     EXCEPTION
967       WHEN  NO_DATA_FOUND  THEN
968         G_nlro_start := NULL;
969         G_nlro_end   := NULL;
970         G_NlrInfoNlroId := X_nlro_id;
971         G_NlrInfoNlr := X_nlr;
972     END;
973 
974     pa_cc_utils.reset_curr_function;
975   EXCEPTION
976     WHEN  NO_DATA_FOUND  THEN
977       G_nlr_start  := NULL;
978       G_nlr_end    := NULL;
979       G_nlr_etype  := NULL;
980       G_nlro_start := NULL;
981       G_nlro_end   := NULL;
982       pa_cc_utils.reset_curr_function;
983 
984   END GetNlrInfo;
985 
986   PROCEDURE  CheckDupItem( X_trx_source  IN VARCHAR2
987                          , X_trx_ref     IN VARCHAR2
988                          , X_status      OUT NOCOPY VARCHAR2 )
989   IS
990     dummy     NUMBER;
991   BEGIN
992 
993     pa_cc_utils.set_curr_function('CheckDupItem');
994     BEGIN
995       SELECT
996               1
997         INTO
998               dummy
999         FROM
1000               sys.dual
1001        WHERE EXISTS
1002             ( SELECT  1
1003                 FROM  pa_expenditure_items ei
1004                WHERE  ei.orig_transaction_reference = X_trx_ref
1005                  AND  ei.transaction_source = X_trx_source );
1006 
1007     EXCEPTION
1008       WHEN  NO_DATA_FOUND  THEN
1009         NULL;
1010     END;
1011 
1012     IF ( dummy = 1 ) THEN
1013       X_status := 'DUPLICATE_ITEM';
1014       pa_cc_utils.reset_curr_function;
1015       RETURN;
1016 
1017     ELSIF ( i > 0 ) THEN
1018 
1019       FOR  j  IN 1..i  LOOP
1020         IF ( pa_transactions.TrxRefTab(j) = X_trx_ref ) THEN
1021           X_status := 'DUPLICATE_ITEM';
1022           pa_cc_utils.reset_curr_function;
1023           RETURN;
1024         END IF;
1025       END LOOP;
1026 
1027     ELSE
1028       X_status := NULL;
1029 
1030     END IF;
1031     pa_cc_utils.reset_curr_function;
1032 
1033   END  CheckDupItem;
1034 
1035   PROCEDURE  CheckDupAdjItem( X_adj_item_id IN NUMBER
1036                          , X_status      OUT NOCOPY VARCHAR2 )
1037   IS
1038   BEGIN
1039 
1040     pa_cc_utils.set_curr_function('CheckDupAdjItem');
1041     IF ( i > 0 ) THEN
1042 
1043       FOR  j  IN 1..i  LOOP
1044         IF ( pa_transactions.AdjEiTab(j) = X_adj_item_id) THEN
1045           X_status := 'DUPLICATE_ADJUST_ITEM';
1046           pa_cc_utils.reset_curr_function;
1047           RETURN;
1048         END IF;
1049       END LOOP;
1050 
1051     ELSE
1052       X_status := NULL;
1053 
1054     END IF;
1055     pa_cc_utils.reset_curr_function;
1056   END  CheckDupAdjItem;
1057 
1058   -- SST Change: changed procedure from getprojbcostflag
1059   -- to getprojtypeinfo because we're not only retrieving
1060   -- burden cost flag, we're also retrieving project type
1061   -- class code
1062   -- PA-K Changes: Selecting burden_amt_display_method
1063   -- Bug 2634812 : Selecting total_burden_flag
1064   PROCEDURE GetProjTypeInfo( X_Project_id IN NUMBER,
1065                              X_Proj_bcost_flag OUT NOCOPY VARCHAR2,
1066                              X_proj_type_class OUT NOCOPY VARCHAR2,
1067                              X_burden_amt_display_method OUT NOCOPY VARCHAR2,
1068                              X_Total_Burden_Flag OUT NOCOPY VARCHAR2)
1069 
1070   IS
1071   BEGIN
1072      pa_cc_utils.set_curr_function('GetProjTypeInfo');
1073 
1074      If (X_Project_id = G_PrjInfoPrjId) Then
1075 
1076         IF PG_DEBUG = 'Y' THEN
1077         pa_debug.G_err_stage := 'Inside GetProjTypeInfo, using cached values';
1078            log_message('log_message: ' || pa_debug.G_err_stage);
1079         END IF;
1080 
1081         X_Proj_bcost_flag := G_PrjInfoBCostFlag;
1082         X_proj_type_class := G_PrjInfoTypeClass;
1083         X_burden_amt_display_method := G_PrjInfoBdDisplay;
1084         X_Total_Burden_Flag := G_PrjInfoTotBdFlag;
1085 
1086      Else
1087 
1088         IF PG_DEBUG = 'Y' THEN
1089         pa_debug.G_err_stage := 'Inside GetProjTypeInfo, selecting values';
1090            log_message('log_message: ' || pa_debug.G_err_stage);
1091         END IF;
1092 
1093         -- SST changes, added project_type_class to select statement
1094         SELECT burden_cost_flag, project_type_class_code, burden_amt_display_method,
1095                total_burden_flag
1096         INTO   X_proj_bcost_flag, X_proj_type_class, X_burden_amt_display_method,
1097                X_Total_Burden_Flag
1098         FROM   pa_projects_all proj,
1099                pa_project_types_all ptype
1100         WHERE  proj.project_type = ptype.project_type
1101 	  -- MOAC Changes
1102           AND  proj.org_id  = ptype.org_id
1103           AND  project_id = X_Project_id ;
1104 
1105          G_PrjInfoPrjId := X_Project_id;
1106          G_PrjInfoBCostFlag := X_proj_bcost_flag;
1107          G_PrjInfoTypeClass := X_proj_type_class;
1108          G_PrjInfoBdDisplay := X_burden_amt_display_method;
1109          G_PrjInfoTotBdFlag := X_Total_Burden_Flag;
1110 
1111      End If;
1112 
1113      pa_cc_utils.reset_curr_function;
1114 
1115   EXCEPTION
1116       WHEN NO_DATA_FOUND THEN
1117            X_Proj_bcost_flag := NULL ;
1118            X_proj_type_class := NULL;
1119            X_burden_amt_display_method := NULL;
1120            X_Total_Burden_Flag := NULL;
1121            G_PrjInfoPrjId := X_Project_id;
1122            G_PrjInfoBCostFlag := X_proj_bcost_flag;
1123            G_PrjInfoTypeClass := X_proj_type_class;
1124            G_PrjInfoBdDisplay := X_burden_amt_display_method;
1125            G_PrjInfoTotBdFlag := X_Total_Burden_Flag;
1126 
1127            pa_cc_utils.reset_curr_function;
1128   END GetProjTypeInfo ;
1129 
1130   FUNCTION CheckCCID ( ccid number ) RETURN NUMBER
1131   IS
1132    X_ccid  number(15) ;
1133   BEGIN
1134     -- pa_cc_utils.set_curr_function('CheckCCID');
1135     -- not setting error stack because this procedure
1136     -- will be violating its associated pragma.
1137 
1138     If ccid is null Then
1139        G_PrevCCID := ccid;
1140        G_PrevRetVal := NULL;
1141        return(NULL);
1142     End If;
1143 
1144     If G_PrevCCID = ccid Then
1145 
1146        X_ccid := G_PrevRetVal;
1147 
1148     Else
1149 
1150        select code_combination_id
1151          into X_ccid
1152          from gl_code_combinations
1153         where code_combination_id = ccid ;
1154 
1155        G_PrevCCID    := ccid;
1156        G_PrevRetVal  := 1;
1157 
1158     End If;
1159 
1160      --pa_cc_utils.reset_curr_function;
1161      return(X_ccid) ;
1162   EXCEPTION
1163     WHEN NO_DATA_FOUND THEN
1164        G_PrevRetVal := NULL;
1165        G_PrevCCID   := ccid;
1166        --pa_cc_utils.reset_curr_function;
1167        RETURN (NULL) ;
1168   END CheckCCID ;
1169 
1170   -- =============================================================
1171   -- Bugs: 1063562 and 1069585
1172   -- New Procedure Validate_VI to do the following validations for Vendor Invoices:
1173   --         1. Transaction Source needs to be GL accounted.
1174   --         2. Vendor Number needs to be provided
1175 
1176   PROCEDURE Validate_VI (
1177             X_vendor_number IN VARCHAR2,
1178             X_employee_number IN VARCHAR2,
1179             X_result OUT NOCOPY NUMBER,
1180             X_status OUT NOCOPY VARCHAR2) AS
1181 
1182   BEGIN
1183 	IF G_gl_accted_flag <> 'Y' THEN
1184 		X_status := 'PA_VI_TRX_SRC_NOT_GL_ACCTED';
1185 		X_result := 1;
1186 		RETURN;
1187 	ELSIF X_vendor_number IS NULL THEN
1188 		X_status := 'PA_VI_VEND_NUM_IS_NULL';
1189 		X_result := 1;
1190 		RETURN;
1191 	END IF;
1192   END;
1193 --
1194 -- 5235363 R12.PJ:XB4:DEV:APL:EXP ITEM DATE VALIDATIONS FOR SUPPLIER COST.
1195 --
1196 PROCEDURE validate_exp_date ( p_project_id    IN NUMBER,
1197                              p_task_id       In NUMBER,
1198                              p_award_id      in NUMBER,
1199                              p_incurred_by_org_id    in number,
1200                              p_vendor_id     in NUMBER,
1201                              p_person_id     in number,
1202                              p_exp_item_date in date,
1203                              p_exp_type      in varchar2,
1204                              p_system_linkage   in varchar2,
1205                              p_txn_source    in varchar2,
1206                              p_calling_modle in varchar2,
1207                              x_err_msg_cd    in out nocopy varchar2,
1208                              x_status        out nocopy varchar2   )is
1209 Cursor c_dates is
1210        select nvl(p.start_date, p_exp_item_date)           proj_start_date,
1211               nvl(p.completion_date, p_exp_item_date)       proj_end_date,
1212               nvl(t.start_date, p_exp_item_date)            task_start_date,
1213               nvl(t.completion_date, p_exp_item_date)       task_completion_date,
1214               nvl(et.start_date_active, p_exp_item_date)    et_start_date,
1215               nvl(et.end_date_active, p_exp_item_date)      et_end_date,
1216               nvl(sl.start_date_active, p_exp_item_date)    sl_start_date,
1217               nvl(sl.end_date_active, p_exp_item_date)      sl_end_date
1218          from pa_projects_all p,
1219               pa_tasks        t,
1220               pa_expenditure_types et,
1221               pa_expend_typ_sys_links sl
1222         where p.project_id         = p_project_id
1223           and t.project_id         = p_project_id
1224           and t.task_id            = p_task_id
1225           and et.expenditure_type  = p_exp_type
1226           and sl.system_linkage_function = p_system_linkage
1227           and sl.expenditure_type  = p_exp_type ;
1228 
1229 CURSOR GET_VALID_AWARDS IS
1230        Select 	Allowable_Schedule_Id,
1231 		nvl(Preaward_Date,START_DATE_ACTIVE) preaward_date,
1232 		End_Date_Active                      end_date,
1233 		Close_Date                           close_date,
1234 		Status
1235 	from 	GMS_AWARDS
1236 	where 	award_id =  P_award_id;
1237 
1238   c_dates_rec  c_dates%rowtype ;
1239   c_award_rec  GET_VALID_AWARDS%Rowtype ;
1240 
1241 FUNCTION check_active_employee (p_vendor_id   Number ,p_person_id   Number ,p_Ei_Date     Date ) Return varchar2 IS
1242 
1243         l_return_string  varchar2(10) := 'Y';
1244         l_return_number  Number := NULL;
1245         l_emp_number     Number := Null;
1246 
1247           CURSOR cur_emp  IS
1248                   SELECT vend.employee_id
1249                   FROM  po_vendors vend
1250                   WHERE  vend.vendor_id = p_vendor_id
1251                   AND   p_ei_date BETWEEN nvl(vend.start_date_active,p_ei_date) AND
1252                            nvl( vend.end_date_active, trunc(sysdate) ) ;
1253 
1254  BEGIN
1255            If nvl(p_person_id,0) = 0  then
1256                 OPEN cur_emp;
1257                 FETCH cur_emp INTO l_emp_number;
1258                 CLOSE cur_emp;
1259            Else
1260                 l_emp_number := p_person_id;
1261            End If;
1262 
1263 	   If l_emp_number is NOT NULL then
1264 
1265            	l_return_number := pa_utils.GetEmpOrgId( l_emp_number, p_ei_date );
1266            	If l_return_number is NULL then
1267                 	l_return_string :=  'N';
1268            	End If;
1269 	   End If;
1270            Return l_return_string;
1271 
1272  END check_active_employee;
1273 
1274 BEGIN
1275 
1276   pa_cc_utils.set_curr_function('validate_exp_date');
1277 
1278   IF x_err_msg_cd is not NULL THEN
1279      x_status      := FND_API.G_RET_STS_ERROR ;
1280      RETURN ;
1281   END IF ;
1282 
1283   x_status  := FND_API.G_RET_STS_SUCCESS;
1284 
1285   IF PG_DEBUG = 'Y' THEN
1286      log_message('log_message: fetching project dates  ' );
1287   END IF;
1288 
1289   open c_dates ;
1290   fetch c_dates into c_dates_rec ;
1291   close c_dates ;
1292 
1293   IF PG_DEBUG = 'Y' THEN
1294      log_message('log_message: Validating exp dates now' );
1295   END IF;
1296 
1297 
1298   IF p_award_id is not null then
1299      IF PG_DEBUG = 'Y' THEN
1300         log_message('log_message: award passed is :'||p_award_id ) ;
1301      END IF;
1302 
1303      open get_valid_awards ;
1304      fetch get_valid_awards into c_award_rec ;
1305      close get_valid_awards ;
1306 
1307      IF p_exp_item_date <  TRUNC(c_award_rec.preaward_date) THEN
1308         x_err_msg_cd := 'GMS_EXP_ITEM_DT_BEFORE_AWD_ST' ;
1309         x_status      := FND_API.G_RET_STS_ERROR ;
1310         RETURN ;
1311      END IF ;
1312 
1313      IF p_exp_item_date >  TRUNC(c_award_rec.end_date) THEN
1314         x_err_msg_cd := 'GMS_EXP_ITEM_DT_AFTER_AWD_END' ;
1315         x_status      := FND_API.G_RET_STS_ERROR ;
1316         RETURN ;
1317      END IF ;
1318 
1319      IF c_award_rec.close_date < TRUNC(SYSDATE) THEN
1320         x_err_msg_cd := 'GMS_AWARD_IS_CLOSED' ;
1321         x_status      := FND_API.G_RET_STS_ERROR ;
1322         RETURN ;
1323      END IF ;
1324   END IF ;
1325 
1326   IF PG_DEBUG = 'Y' THEN
1327      log_message('log_message: Validating exp dates -> exp types' );
1328   END IF;
1329   IF p_exp_item_date not between  c_dates_rec.et_start_date  and c_dates_rec.et_end_date then
1330      x_err_msg_cd := 'EXP_TYPE_INACTIVE' ;
1331      x_status      := FND_API.G_RET_STS_ERROR ;
1332      RETURN ;
1333   END IF ;
1334 
1335   IF PG_DEBUG = 'Y' THEN
1336      log_message('log_message: Validating exp dates -> System Linkage Function' );
1337   END IF;
1338   IF p_exp_item_date not between c_dates_rec.sl_start_date and c_dates_rec.sl_end_date THEN
1339      x_err_msg_cd := 'ETYPE_SLINK_INACTIVE' ;
1340      x_status      := FND_API.G_RET_STS_ERROR ;
1341      RETURN ;
1342   END IF ;
1343 
1344   IF PG_DEBUG = 'Y' THEN
1345      log_message('log_message: Validating exp dates -> Project Level' );
1346   END IF;
1347   IF p_exp_item_date NOT between c_dates_rec.proj_start_date and c_dates_rec.proj_end_date THEN
1348      x_err_msg_cd := 'PA_EX_PROJECT_DATE' ;
1349      x_status      := FND_API.G_RET_STS_ERROR ;
1350      RETURN ;
1351   END IF ;
1352 
1353   IF PG_DEBUG = 'Y' THEN
1354      log_message('log_message: Validating exp dates -> Task Level' );
1355   END IF;
1356   IF p_exp_item_date NOT between c_dates_rec.task_start_date and c_dates_rec.task_completion_date THEN
1357      x_err_msg_cd := 'PA_EXP_TASK_EFF' ;
1358      x_status      := FND_API.G_RET_STS_ERROR ;
1359      RETURN ;
1360   END IF ;
1361 
1362   IF PG_DEBUG = 'Y' THEN
1363      log_message('log_message: Validating exp dates -> Exp Org Level' );
1364   END IF;
1365   IF pa_utils2.CheckExporg(p_incurred_by_org_id,p_exp_item_date) = 'N' then
1366       x_err_msg_cd := 'PA_EXP_ORG_NOT_ACTIVE' ;
1367       x_status      := FND_API.G_RET_STS_ERROR ;
1368       RETURN ;
1369   END IF;
1370 
1371   IF PG_DEBUG = 'Y' THEN
1372      log_message('log_message: Validating exp dates -> Employee Level' );
1373   END IF;
1374   IF nvl(check_active_employee (p_vendor_id => p_vendor_id ,p_person_id => p_person_id ,p_ei_date   => p_exp_item_date),'N') = 'N' then
1375      x_err_msg_cd := 'NO_ASSIGNMENT' ;
1376      x_status  := FND_API.G_RET_STS_ERROR ;
1377      RETURN ;
1378   End if;
1379 
1380   pa_cc_utils.reset_curr_function;
1381 
1382 
1383 END VALIDATE_EXP_DATE ;
1384 
1385 
1386 
1387   PROCEDURE ValidateItem(
1388                X_trx_src      IN VARCHAR2
1389             ,  X_enum         IN VARCHAR2
1390             ,  X_oname        IN VARCHAR2
1391             ,  X_end_date     IN DATE
1392             ,  X_ei_date      IN DATE
1393             ,  X_etype        IN VARCHAR2
1394             ,  X_pnum         IN VARCHAR2
1395             ,  X_tnum         IN VARCHAR2
1396             ,  X_nlr          IN VARCHAR2
1397             ,  X_nlro_name    IN VARCHAR2
1398             ,  X_qty          IN NUMBER
1399             ,  X_denom_raw_cost     IN NUMBER
1400             ,  X_module       IN VARCHAR2
1401             ,  X_trx_ref      IN VARCHAR2
1402             ,  X_match_flag   IN VARCHAR2
1403             ,  X_entered_by   IN NUMBER
1404             ,  X_att_cat      IN VARCHAR2
1405             ,  X_att1         IN OUT NOCOPY VARCHAR2 --DFF Upgrade:
1406             ,  X_att2         IN OUT NOCOPY VARCHAR2 --Change from IN to
1407             ,  X_att3         IN OUT NOCOPY VARCHAR2 --IN OUT
1408             ,  X_att4         IN OUT NOCOPY VARCHAR2
1409             ,  X_att5         IN OUT NOCOPY VARCHAR2
1410             ,  X_att6         IN OUT NOCOPY VARCHAR2
1411             ,  X_att7         IN OUT NOCOPY VARCHAR2
1412             ,  X_att8         IN OUT NOCOPY VARCHAR2
1413             ,  X_att9         IN OUT NOCOPY VARCHAR2
1414             ,  X_att10        IN OUT NOCOPY VARCHAR2
1415             ,  X_drccid       IN NUMBER
1416             ,  X_crccid       IN NUMBER
1417             ,  X_gl_date      IN OUT NOCOPY DATE -- Change from IN to IN OUT, bug 3357936
1418             ,  X_denom_burdened_cost IN OUT  NOCOPY NUMBER
1419             ,  X_system_linkage IN VARCHAR2
1420             ,  X_status       OUT NOCOPY VARCHAR2
1421             ,  X_bill_flag    OUT NOCOPY VARCHAR2
1422 	   , X_receipt_currency_amount IN NUMBER
1423 	   , X_receipt_currency_code   IN VARCHAR2
1424 	   , X_receipt_exchange_rate   IN OUT NOCOPY NUMBER
1425 	   , X_denom_currency_code     IN OUT NOCOPY VARCHAR2
1426 	   , X_acct_rate_date  	       IN OUT NOCOPY DATE
1427 	   , X_acct_rate_type          IN OUT NOCOPY VARCHAR2
1428 	   , X_acct_exchange_rate      IN OUT NOCOPY NUMBER
1429 	   , X_acct_raw_cost           IN NUMBER
1430 	   , X_acct_burdened_cost      IN OUT NOCOPY NUMBER
1431 	   , X_acct_exchange_rounding_limit IN NUMBER
1432 	   , X_project_currency_code   IN OUT NOCOPY VARCHAR2
1433 	   , X_project_rate_date       IN OUT NOCOPY DATE
1434 	   , X_project_rate_type       IN OUT NOCOPY VARCHAR2
1435 	   , X_project_exchange_rate   IN OUT NOCOPY NUMBER
1436 	   , X_project_raw_cost        IN OUT NOCOPY NUMBER
1437 	   , X_project_burdened_cost   IN OUT NOCOPY NUMBER
1438            -- Trx_import enhancement: New parameter
1439            , X_override_to_oname       IN VARCHAR2
1440            , X_vendor_number           IN VARCHAR2
1441            , X_org_id                  IN NUMBER
1442            , X_Business_Group_Name     IN VARCHAR2
1443            -- PA-I Changes : Added Proj Func Currency Attr
1444            , X_Projfunc_currency_code  IN OUT NOCOPY VARCHAR2
1445            , X_Projfunc_cost_rate_date      IN OUT NOCOPY DATE
1446            , X_Projfunc_cost_rate_type      IN OUT NOCOPY VARCHAR2
1447            , X_Projfunc_cost_exchange_rate  IN OUT NOCOPY NUMBER
1448            , X_actual_project_raw_cost  IN OUT NOCOPY NUMBER
1449            , X_actual_project_burdened_cost  IN OUT NOCOPY NUMBER
1450            -- PA-I Changes : Added Assignment and Work Type Name
1451            , X_Assignment_Name           IN OUT NOCOPY VARCHAR2
1452            , X_Work_Type_Name            IN OUT NOCOPY VARCHAR2
1453            -- PA-J Period-End Accrual Changes : Added accrual_flag
1454            , X_accrual_flag              IN VARCHAR2
1455            --PA-K Changes
1456            ,  P_project_id                IN NUMBER
1457            ,  P_task_id                   IN NUMBER
1458            ,  P_person_id                 IN NUMBER
1459            ,  P_organization_id           IN NUMBER
1460            ,  P_NLR_Org_Id                IN NUMBER
1461            ,  P_Vendor_Id                 IN NUMBER
1462            ,  P_Override_Organization_Id  IN NUMBER
1463            ,  P_Person_business_Group_Id  IN NUMBER
1464            ,  P_assignment_id             IN NUMBER
1465            ,  P_work_type_id              IN NUMBER
1466            ,  P_Emp_Org_Id                IN NUMBER
1467            ,  P_Emp_Job_Id                IN NUMBER
1468 /* Added parameter X_txn_interface_id for bug 2563364 */
1469            ,  X_txn_interface_id        IN NUMBER
1470            ,  P_po_number     IN VARCHAR2             /* cwk */
1471 	   ,  P_po_header_id       IN OUT NOCOPY NUMBER
1472 	   ,  P_po_line_num        IN NUMBER
1473 	   ,  P_po_line_id       IN OUT NOCOPY NUMBER
1474 	   ,  P_person_type        IN VARCHAR2
1475 	   ,  P_po_price_type        IN VARCHAR2
1476 	   /* REL12-AP Lines uptake */
1477            ,  p_adj_exp_item_id        IN NUMBER default NULL
1478 	   ,  p_fc_document_type       IN varchar2 default NULL)
1479           -- ,  p_agreement_id           IN OUT NOCOPY NUMBER   --FSIO Changes
1480          --  ,  p_agreement_number       IN OUT NOCOPY varchar2)
1481   IS
1482     temp_status      VARCHAR2(30) DEFAULT NULL;
1483     temp_bill_flag   VARCHAR2(1)  DEFAULT NULL;
1484     temp_msg_application VARCHAR2(50) := 'PA';
1485     temp_msg_type 	VARCHAR2(1) := 'E';
1486     temp_msg_token1  VARCHAR2(2000) := '';
1487     temp_msg_token2	VARCHAR2(2000) :='';
1488     temp_msg_token3	VARCHAR2(2000) :='';
1489     temp_msg_count	NUMBER :=1;
1490     temp_dff_msg     VARCHAR(2000) :='';
1491 
1492     dummy            NUMBER       DEFAULT NULL;
1493     l_dummy          Varchar2(1)  DEFAULT NULL;
1494 
1495     l_converted_amount NUMBER     DEFAULT NULL;
1496     l_denominator      NUMBER     DEFAULT NULL;
1497     l_numerator        NUMBER     DEFAULT NULL;
1498     l_rate             NUMBER     DEFAULT NULL;
1499     l_status           VARCHAR2(80) DEFAULT NULL;
1500     l_api_status       varchar2(1) ;
1501     l_status_num       NUMBER     DEFAULT NULL;
1502     l_stage            NUMBER     DEFAULT NULL;
1503     l_validate_user_rate_type VARCHAR2(1) DEFAULT 'Y';
1504     l_handel_exception_flag VARCHAR2(1) DEFAULT 'Y';
1505     l_denom_cost_ratio            NUMBER := 1;
1506     l_project_rate_type  VARCHAR2(30) := NULL;
1507     l_acct_rate_date       DATE;
1508     l_acct_rate_type     VARCHAR2(30);
1509     l_acct_exchange_rate NUMBER;
1510     l_acct_raw_cost  	 NUMBER;
1511 
1512     l_Raw_Bc_Packet_Id    NUMBER;
1513     l_Packet_Id           NUMBER;
1514     l_fc_return_status    VARCHAR2(30);
1515     l_fc_error_stage      VARCHAR2(30);
1516     l_fc_error_msg        VARCHAR2(30);
1517 
1518     -- Start PA-I Changes
1519     l_projfunc_cost_rate_type VARCHAR2(30) := NULL;
1520     l_asgn_work_ret_sts       VARCHAR2(1);
1521     l_asgn_work_err_msg       VARCHAR2(1000);
1522     -- End PA-I Changes
1523 
1524     -- PA-J Period-End Accrual Changes
1525     l_period_end_txn      VARCHAR2(1) := 'N';
1526     l_ret_sts             VARCHAR2(1);
1527     x_err_stage           VARCHAR2(100);
1528 
1529     --Bug 3010848
1530     L_Org_RetSts  Varchar2(50);
1531 
1532     l_SobId     pa_implementations_all.set_of_books_id%TYPE; -- bug 3357936
1533     l_appl_id  NUMBER(15) := PA_PERIOD_PROCESS_PKG.Application_Id; -- bug 3357936
1534 
1535     -- Start of Changes for  Bug 5743708
1536     x_cp_structure        VARCHAR2(30);
1537     x_cost_base           VARCHAR2(30);
1538     -- End of Changes for  Bug 5743708
1539 
1540  /* cwk */
1541     l_po_rate NUMBER := 0;
1542     l_Calc_Amt NUMBER := 0;
1543     l_processed_cost NUMBER := 0;
1544     l_costed NUMBER := 0;
1545     l_uncosted NUMBER := 0;
1546     l_retrn_val   NUMBER := -1;
1547     l_cwk_amt_updated VARCHAR2(1) := 'N';
1548 
1549     Cursor c_check_fail(c_packet_id  NUMBER) is
1550                    Select 'X'
1551                      from Dual
1552                    Where exists (select 'Y' from pa_bc_packets
1553                                   Where packet_id = c_packet_id
1554                                     and substr(nvl(result_code, 'P'),1,1) = 'F');
1555 
1556 
1557     pa_date          DATE         DEFAULT NULL ;
1558     recvr_pa_date    DATE         DEFAULT NULL ; /* Bug # 1653831 */
1559     v_result         NUMBER := 0;
1560 
1561     x_return_status NUMBER;
1562     x_error_code    VARCHAR2(100);
1563     x_error_stage   NUMBER;
1564 
1565    --l_NewTxnsAllowed  VARCHAR2(1);
1566 
1567 /***** CWK CHANGES - This function will check existance of record of a
1568 combination of po_line_id and task_id in PLSQL table *****/
1569 
1570 Function Is_Po_Line_Task_Processed (
1571 									P_Po_Line_Id  IN NUMBER,
1572 									P_Task_Id     IN NUMBER
1573 								   ) Return Boolean
1574 Is
1575 	l_Line_Task_str   VARCHAR2(150)  := NULL;
1576 Begin
1577 
1578 	l_Line_Task_str   := P_Po_Line_Id ||'.'||P_Task_Id;
1579 
1580 	IF PoLineTaskTab.COUNT > 0 THEN
1581 		FOR i IN PoLineTaskTab.FIRST..PoLineTaskTab.LAST
1582 		LOOP
1583 			  IF (l_Line_Task_str = PoLineTaskTab(i)) THEN
1584 					Return True;
1585 			  End If;
1586 		END LOOP;
1587 	End If;
1588 
1589 	Return False;
1590 
1591 End Is_Po_Line_Task_Processed;
1592 
1593 /***** CWK CHANGES - This Procedure will update PL/SQL Tables to reflect *****/
1594 /***** Processed Amount for the given Po_Line_ID. That will be used to   *****/
1595 /***** do the Po Funds check and validate the transaction record.        *****/
1596 
1597 PROCEDURE po_processed_amt_chk(P_Po_Line_Id  IN NUMBER
1598                               ,P_Task_Id   IN   NUMBER
1599                               ,P_Calc_Amt IN NUMBER
1600                               ,X_Processed_Amt OUT NOCOPY NUMBER
1601                               ,X_status OUT NOCOPY VARCHAR2
1602 			     ) IS
1603 PRAGMA AUTONOMOUS_TRANSACTION;
1604 l_counter         BINARY_INTEGER := 0 ;
1605 l_Line_Task_str   VARCHAR2(150)  := NULL;
1606 l_lock_status     NUMBER         := -1;
1607 l_costed_amt	  NUMBER := 0;
1608 l_uncosted_amt	  NUMBER := 0;
1609 
1610 
1611 BEGIN
1612 
1613 l_Line_Task_str   := P_Po_Line_Id ||'.'||P_Task_Id;
1614 
1615 IF PoLineTaskTab.COUNT > 0 THEN
1616 
1617 BEGIN
1618 
1619 FOR i IN PoLineTaskTab.FIRST..PoLineTaskTab.LAST
1620 LOOP
1621 
1622   IF (l_Line_Task_str = PoLineTaskTab(i)) THEN
1623 
1624       PoAmtTab(i) := PoAmtTab(i) + P_Calc_Amt;
1625       X_Processed_Amt := PoAmtTab(i);
1626 
1627       x_status := null;
1628       RETURN;
1629 
1630   END IF;
1631 
1632 END LOOP;
1633 
1634 EXCEPTION
1635 WHEN OTHERS THEN
1636 
1637   IF PG_DEBUG = 'Y' THEN
1638      pa_debug.G_err_stage := 'PO_PROCESSED_AMT_CHK : Search of Records : ' ||SQLERRM;
1639      log_message('log_message: ' || pa_debug.G_err_stage,1);
1640   END IF;
1641 
1642 END ;
1643 
1644 END IF ;
1645 
1646 -- Now Acquire the lock for Po Line Id and Task Id combination String.
1647 
1648 l_lock_status :=  Pa_Debug.Acquire_User_Lock(l_Line_Task_str);
1649 
1650 IF (l_lock_status = 0) THEN
1651 
1652    l_counter := PoLineTaskTab.COUNT;
1653    l_counter := l_counter + 1;
1654 
1655    PoLineTaskTab(l_counter) := l_Line_Task_str;
1656    PoAmtTab (l_counter)     := P_Calc_Amt ;
1657    X_Processed_Amt := P_Calc_Amt ;
1658    x_status := null;
1659    return;
1660 
1661 ELSE
1662 
1663   IF PG_DEBUG = 'Y' THEN
1664      pa_debug.G_err_stage := 'PO_PROCESSED_AMT_CHK : Failed To Aquire Lock for : '||l_Line_Task_str;
1665      log_message('log_message: ' || pa_debug.G_err_stage,1);
1666   END IF;
1667 
1668   x_status := 'PO_LINE_TASK_LOCKED';
1669   RETURN;
1670 
1671 END IF;
1672 
1673 EXCEPTION
1674 WHEN OTHERS THEN
1675 
1676   IF PG_DEBUG = 'Y' THEN
1677      pa_debug.G_err_stage := 'PO_PROCESSED_AMT_CHK : Insertion of Records : ' ||SQLERRM;
1678      log_message('log_message: ' || pa_debug.G_err_stage,1);
1679   END IF;
1680 
1681 END po_processed_amt_chk;
1682 
1683 PROCEDURE undo_processed_amt_chk(P_Po_Line_Id NUMBER
1684                                ,P_Task_Id    NUMBER
1685                                ,P_Calc_Amt NUMBER
1686 			        )  IS
1687 
1688 l_Line_Task_str   VARCHAR2(150)  := NULL;
1689 
1690 BEGIN
1691 
1692 l_Line_Task_str   := P_Po_Line_Id ||'.'||P_Task_Id;
1693 
1694   FOR i IN PoLineTaskTab.FIRST..PoLineTaskTab.LAST
1695   LOOP
1696 
1697     IF (l_Line_Task_str = PoLineTaskTab(i)) THEN
1698 
1699       PoAmtTab(i) := PoAmtTab(i) - P_Calc_Amt;
1700 
1701     END IF;
1702 
1703   END LOOP;
1704 
1705 EXCEPTION
1706 WHEN OTHERS THEN
1707 
1708   IF PG_DEBUG = 'Y' THEN
1709      pa_debug.G_err_stage := 'UNDO_PROCESSED_AMT_CHK : While Undoing Processed Amt Check : ' ||SQLERRM;
1710      log_message('log_message: ' || pa_debug.G_err_stage,1);
1711   END IF;
1712 
1713 END undo_processed_amt_chk;
1714 
1715   /** *
1716   *** Validate Items Main processing begins here
1717   **/
1718   BEGIN
1719     pa_cc_utils.set_curr_function('ValidateItem');
1720 
1721     IF PG_DEBUG = 'Y' THEN
1722     pa_debug.G_err_stage := 'Inside ValidateItem';
1723        log_message('log_message: ' || pa_debug.G_err_stage);
1724     END IF;
1725 
1726     G_adj_item_id := NULL;
1727     G_job_id := NULL;
1728 
1729     X_status := NULL;
1730 
1731     IF (G_accounting_currency_code IS NULL) THEN
1732        GetImpCurrInfo;
1733     END IF;
1734 
1735     --PA-K Changes: For PAAPIMP we can skip the following validations and
1736     --use the ID attributes provided.
1737     IF (nvl(X_module, 'EXTERNAL') <> 'PAAPIMP') THEN ---{
1738 
1739        IF PG_DEBUG = 'Y' THEN
1740        pa_debug.G_err_stage := 'Calling module not PAAPIMP';
1741           log_message('log_message: ' || pa_debug.G_err_stage);
1742        END IF;
1743 
1744        --
1745        -- Changes for CBGA
1746        --
1747        -- The following change was to get the business group id if a business group
1748        -- name is provided.
1749        --
1750        --PA-K Changes: If predefined source and ID provided then use it else derive.
1751        /** Bug#3026218 If Person Business Group Id is not null then use it and
1752            do not derive **/
1753        IF P_Person_Business_Group_Id IS NOT NULL THEN
1754           G_Business_Group_Id := P_Person_Business_Group_Id;
1755        ELSE
1756 
1757           IF X_Business_Group_Name IS NULL THEN
1758 
1759            G_Business_Group_Id := NULL ;
1760 
1761           ELSIF G_Prev_Business_Group_Name = X_Business_Group_Name THEN
1762 
1763            G_Business_Group_Id := G_Prev_Business_Group_Id ;
1764           ELSE
1765            G_Business_Group_Id := pa_utils2.GetBusinessGroupId(X_Business_Group_Name);
1766 
1767            IF G_Business_Group_Id is NULL THEN
1768 
1769             X_status := 'PA_INVALID_BUSINESS_GROUP';
1770 
1771             G_Prev_Business_Group_Name := NULL ;
1772             G_Prev_Business_Group_Id   := NULL ;
1773 
1774            ELSE
1775 
1776             G_Prev_Business_Group_Name := X_Business_Group_Name ;
1777             G_Prev_Business_Group_Id :=  G_Business_Group_Id ;
1778            END IF; /* G_Business_Group_Id is NULL */
1779           END IF; /* X_Business_Group_Name IS NULL */
1780        END IF; /* P_Person_Business_Group_Id IS NOT NULL */
1781 
1782        -- ===========================================================================
1783        --   Perform TRANSACTION SOURCE validation:
1784        --     * TRANSACTION SOURCE exists
1785        --     * If TRANSACTION SOURCE is defined as costed, then RAW_COST must be
1786        --            provided
1787        --     * TRANSACTION SORUCE must be active as of the transaction date
1788 
1789        last_empno := NULL;
1790        last_proj  := NULL;
1791        last_task  := NULL;
1792        last_etype := NULL;
1793 
1794        IF ( nvl( X_module, 'EXTERNAL' ) <> 'PAXTRTRX' ) THEN
1795 
1796           IF PG_DEBUG = 'Y' THEN
1797           pa_debug.G_err_Stage := 'G_trx_link = ' || G_trx_link;
1798              log_message('log_message: ' || pa_debug.G_err_Stage);
1799           END IF;
1800 
1801           IF (G_trx_link is null OR X_trx_src <> G_trx_source) Then --3567234
1802              IF PG_DEBUG = 'Y' THEN
1803              pa_debug.G_err_Stage := 'Calling GetTrxSrcInfo';
1804                 log_message('log_message: ' || pa_debug.G_err_Stage);
1805              END IF;
1806              GetTrxSrcInfo( X_trx_src );
1807           End If;
1808 
1809          IF ( G_trx_link IS NULL ) THEN
1810            IF PG_DEBUG = 'Y' THEN
1811            pa_debug.G_err_Stage := 'Invalid Trx Source';
1812               log_message('log_message: ' || pa_debug.G_err_Stage);
1813            END IF;
1814            X_status := 'INVALID_TRX_SOURCE';
1815            pa_cc_utils.reset_curr_function;
1816            RETURN;
1817          END IF;
1818 
1819        END IF;
1820 
1821        -- ==========================================================================
1822        -- Bugs: 1063562 and 1069585
1823        -- Calls procedure Validate_VI to do the following validations for Vendor Invoices:
1824        --         1. Transaction Source needs to be GL accounted.
1825        --         2. Vendor Number needs to be provided
1826        IF (X_system_linkage = 'VI') THEN
1827        	Validate_VI (
1828        	    X_vendor_number => X_vendor_number,
1829             X_employee_number => X_enum,
1830             X_result => v_result,
1831             X_status => X_status);
1832         IF (v_result <> 0) THEN
1833                 pa_cc_utils.reset_curr_function;
1834 		RETURN;
1835         END IF;
1836        END IF;
1837 
1838        -- Validation for non zero raw cost is moved from here to the else clause of the
1839        -- Burden Transaction validation.... Selva 03/07/97
1840        --
1841        IF ( trunc(X_ei_date) NOT BETWEEN trunc(G_trx_start)
1842                               AND nvl(trunc(G_trx_end), trunc(X_ei_date))) THEN
1843          X_status := 'TRX_SOURCE_INACTIVE';
1844          pa_cc_utils.reset_curr_function;
1845          RETURN;
1846 
1847        END IF;
1848 
1849        -- ==========================================================================
1850        -- Added 'VI' in the following IF condition.
1851        -- A supplier invoice does not need to have
1852        -- employee number and organization name
1853        IF ( X_system_linkage NOT IN  ('ST', 'OT','ER', 'VI') ) THEN
1854          if ( X_enum IS NULL  AND X_ONAME is NULL ) THEN
1855             X_status := 'EMP_OR_ORG_MAND' ;
1856             pa_cc_utils.reset_curr_function;
1857             RETURN ;
1858          end if ;
1859        END IF ;
1860 
1861        -- REL12 AP Lines uptake.
1862        -- Unaccounted Expense report transactions are not allowed.
1863        IF ( X_system_linkage = 'ER' and NVL(g_gl_accted_flag,'N')  = 'N' ) then
1864           X_status := 'PA_ER_NOT_ACCOUNTED' ;
1865           pa_cc_utils.reset_curr_function;
1866 	  RETURN ;
1867        END IF ;
1868 
1869        -- ===========================================================================
1870        --   Verify that the expenditure ending date is a valid expenditure week
1871        --   ending date
1872        --
1873        IF PG_DEBUG = 'Y' THEN
1874        pa_debug.G_err_stage := 'Calling pa_utils.GetWeekEnding';
1875           log_message('log_message: ' || pa_debug.G_err_Stage);
1876        END IF;
1877        IF (trunc( X_end_date) <> trunc(pa_utils.NewGetWeekEnding( X_end_date )) ) THEN
1878         X_status := 'INVALID_END_DATE';
1879         pa_cc_utils.reset_curr_function;
1880         RETURN;
1881 
1882         -- ===========================================================================
1883         --   Verify that the transaction item date is on or before the week ending
1884         --   date
1885         --
1886        ELSIF ( trunc(X_ei_date) > trunc(X_end_date) ) THEN   /* Bug 4284192 */
1887         X_status := 'EI_DATE_AFTER_END_DATE';
1888         pa_cc_utils.reset_curr_function;
1889         RETURN;
1890        END IF;  --PA-K Changes: Added 'EndIf' separating the date checks
1891                 --from the person and org checks
1892 
1893        --PA-K Changes: Use the ID if provided for predefined transaction sources.
1894        /** Bug#3026218. If Person Id and Organization Id are provided, then use them **/
1895        IF P_Person_Id is not null and P_Organization_Id is not null Then
1896           G_Person_Id := P_Person_Id;
1897           G_Org_Id := P_Organization_Id;
1898 
1899 	  /* Bug 6519602: Base Bug 6519570 - Changes start */
1900 	  IF PG_DEBUG = 'Y' THEN
1901              pa_debug.G_err_stage := 'Calling pa_utils.GetEmpJobId';
1902              log_message('log_message: ' || pa_debug.G_err_Stage);
1903           END IF;
1904 
1905           G_job_id := pa_utils.GetEmpJobId(
1906                              X_person_id => G_person_id,
1907                              X_date      => X_ei_date ,
1908                              X_po_number => p_po_number,
1909                              X_po_line_num => p_po_line_num);
1910 
1911 	  IF ( G_job_id IS NULL ) THEN
1912 	     X_status := 'NO_ASSIGNMENT';
1913 	     pa_cc_utils.reset_curr_function;
1914 	     RETURN;
1915 	  END IF;
1916 
1917 	  IF PG_DEBUG = 'Y' THEN
1918              pa_debug.G_err_stage := ' G_Job_Id = ' ||  G_Job_Id;
1919              log_message('log_message: ' || pa_debug.G_err_stage);
1920           END IF;
1921          /* Bug 6519602: Base Bug 6519570 - Changes end */
1922 
1923        ELSE
1924           -- Bug: 1063552
1925           -- For third party transaction sources transferring supplier invoices,
1926           -- we will not transfer the employee number
1927           -- We will transfer the organization info if the following requirements are met:
1928           --         1. Transaction source is defined with employee organization override
1929           --         2. User provided a valid organization name for the override.
1930           IF ((X_enum is NOT NULL) AND
1931              (G_trx_predef_flag = 'N') AND
1932              (X_system_linkage = 'VI')) THEN
1933 
1934             G_person_id := NULL;
1935 
1936             IF PG_DEBUG = 'Y' THEN
1937             pa_debug.G_err_Stage := 'Calling pa_utils.GetOrgnId';
1938                log_message('log_message: ' || pa_debug.G_err_Stage);
1939             END IF;
1940 
1941             IF ( X_oname IS NOT NULL ) THEN
1942 
1943 		    --Start of changes for bug 3010848
1944 		    --G_org_id := pa_utils.GetOrgId(X_oname);
1945 		    pa_utils.GetOrgnId(X_org_name => X_oname,
1946 				       X_bg_id    => G_Business_Group_Id,
1947 				       X_Orgn_Id  => G_org_id,
1948 				       X_Return_Status => L_Org_RetSts);
1949 
1950 		    If L_Org_RetSts is Not Null Then
1951 		       X_status := L_Org_RetSts;
1952 		       RETURN;
1953 		    End If;
1954 		    --End of changes for bug 3010848
1955 
1956 
1957                 IF (G_org_id IS NULL) THEN
1958                   X_status := 'PA_EXP_ORG_INVALID';
1959                   pa_cc_utils.reset_curr_function;
1960                   RETURN;
1961                 END IF;
1962 
1963                 /* PA-K Changes: Commenting the CheckExporg, moved the check to one place below*/
1964 
1965             ELSE
1966                G_org_id := NULL;
1967             END IF;  /* IF X_oname IS NOT NULL*/
1968 
1969           -- ===========================================================================
1970           --   Get the person ID for the employee number given
1971           ELSIF ( X_enum IS NOT NULL ) THEN
1972 
1973             IF PG_DEBUG = 'Y' THEN
1974             pa_debug.G_err_stage := 'Calling pa_utils.GetEmpId, G_business_Group_id = ' || G_business_Group_id;
1975                log_message('log_message: ' || pa_debug.G_err_Stage);
1976             END IF;
1977             -- Fixed Bug 1534973, 1581184
1978             -- Passing X_Ei_Date parameter to GetEmpId
1979 
1980             pa_utils2.GetEmpId( G_business_Group_id,
1981                             X_enum,
1982                             G_person_id,
1983 			    P_person_type, /* cwk */
1984                             X_Ei_Date );
1985 
1986             IF ( pa_utils2.G_return_status IS NOT NULL ) THEN
1987               X_status := pa_utils2.G_return_status ;
1988               pa_cc_utils.reset_curr_function;
1989               RETURN;
1990             ELSE
1991               last_empno := X_enum;
1992 
1993               -- ==============================================
1994               -- Enhancement for Oracle Labor Distribution
1995               -- Don't fetch the employee's org ID if the flag
1996               -- allow_emp_org_override_flag is set to 'Y'
1997 
1998               IF nvl(G_emp_org_oride,'N') = 'Y' THEN
1999 
2000 /*Bug 2655157, Commented this code see below for actual code
2001                 IF ( X_oname IS NULL ) THEN
2002                    X_status := 'PA_EXP_ORG_MANDATORY';
2003                    pa_cc_utils.reset_curr_function;
2004                    RETURN;
2005                 END IF;
2006 
2007                 IF PG_DEBUG = 'Y' THEN
2008                 pa_debug.G_err_Stage := 'Calling pa_utils.GetOrgId';
2009                    log_message('log_message: ' || pa_debug.G_err_Stage);
2010                 END IF;
2011 
2012                 G_org_id := pa_utils.GetOrgId(X_oname);
2013 
2014                 IF (G_org_id IS NULL) THEN
2015                   X_status := 'PA_EXP_ORG_INVALID';
2016                   pa_cc_utils.reset_curr_function;
2017                   RETURN;
2018                 END IF;
2019 */
2020                 -- Bug 2655157 : Below code added for the error being raised in
2021                 -- Review Transactions form
2022                 IF ( X_oname IS NULL ) THEN
2023                    IF PG_DEBUG = 'Y' THEN
2024                       pa_debug.G_err_Stage := 'Calling pa_utils.GetEmpOrgId';
2025                       log_message('log_message: ' || pa_debug.G_err_Stage);
2026                    END IF;
2027 
2028                    G_org_id  := pa_utils.GetEmpOrgId(G_person_id, X_Ei_Date);
2029 
2030                    IF ( G_org_id IS NULL ) THEN
2031                       X_status := 'PA_EXP_ORG_MANDATORY';
2032                       pa_cc_utils.reset_curr_function;
2033                       RETURN;
2034                    END IF;
2035 
2036                 ELSE
2037 
2038                    IF PG_DEBUG = 'Y' THEN
2039                       pa_debug.G_err_Stage := 'Calling pa_utils.GetOrgnId';
2040                       log_message('log_message: ' || pa_debug.G_err_Stage);
2041                    END IF;
2042 
2043 		    --Start of changes for Bug 3010848
2044 		    --G_org_id := pa_utils.GetOrgId(X_oname);
2045 		    pa_utils.GetOrgnId(X_org_name => X_oname,
2046 				       X_bg_id    => G_Business_Group_Id,
2047 				       X_Orgn_Id  => G_org_id,
2048 				       X_Return_Status => L_Org_RetSts);
2049 
2050 
2051 		    If L_Org_RetSts is Not Null Then
2052 		       X_status := L_Org_RetSts;
2053 		       RETURN;
2054 		    End If;
2055 		    --End of changes for Bug 3010848
2056 
2057 
2058                    IF (G_org_id IS NULL) THEN
2059                       X_status := 'PA_EXP_ORG_INVALID';
2060                       pa_cc_utils.reset_curr_function;
2061                       RETURN;
2062                    END IF;
2063 
2064                 END IF;
2065                 --End of fix for Bug 2655157
2066 
2067                 -- ============================================
2068                 --   Get the job ID of the employee's job
2069                 --   assignment as of the item date
2070 
2071                 IF PG_DEBUG = 'Y' THEN
2072                 pa_debug.G_err_stage := 'Calling pa_utils.GetEmpJobId';
2073                    log_message('log_message: ' || pa_debug.G_err_Stage);
2074                 END IF;
2075 
2076                 G_job_id := pa_utils.GetEmpJobId(
2077                              X_person_id => G_person_id,
2078                              X_date      => X_ei_date ,
2079                              X_po_number => p_po_number,
2080                              X_po_line_num => p_po_line_num);
2081 
2082                   -- Added PO params for bug 4044057
2083                   -- Need to validate the assigment for the entered PO
2084 
2085                 IF ( G_job_id IS NULL ) THEN
2086 
2087                     -- Commented for bug 4531168
2088                     -- IF p_po_number is null THEN
2089                     --   X_status := 'NO_ASSIGNMENT';
2090                     -- ELSE
2091                     --   X_status := 'NO_PO_ASSIGNMENT';
2092                     -- END IF;
2093                     -- pa_cc_utils.reset_curr_function;
2094                     -- RETURN;
2095 
2096                     /*Begin for bug 4531168*/
2097                     IF X_trx_src in ('OLD', 'GOLD', 'GOLDE') and p_po_number is null THEN
2098                          begin
2099                                select paa.job_id
2100                                into G_job_id
2101                                from PER_ALL_ASSIGNMENTS_F paa,
2102                                     psp_summary_lines psl
2103                                where paa.assignment_id        =  psl.assignment_id
2104                                and psl.effective_date       between paa.effective_start_date and paa.effective_end_date
2105                                and psl.SUMMARY_LINE_ID      =  X_trx_ref;
2106                          exception
2107                                when OTHERS then
2108                                     X_status := 'NO_ASSIGNMENT';
2109                                     pa_cc_utils.reset_curr_function;
2110                                     RETURN;
2111                          end;
2112                      ELSIF p_po_number is null then
2113                          X_status := 'NO_ASSIGNMENT';
2114                          pa_cc_utils.reset_curr_function;
2115                          RETURN;
2116                      ELSE
2117                          X_status := 'NO_PO_ASSIGNMENT';
2118                          pa_cc_utils.reset_curr_function;
2119                          RETURN;
2120                      END IF;
2121                      /* End for bug 4531168 */
2122 
2123                 END IF;
2124 
2125               ELSE
2126 
2127                 -- =============================================
2128                 --   Get the organization ID of the employee's
2129                 --   organization assignment as of the item date
2130 
2131                 /* Pa.K Changes: For Performance, combined org and job id derivation */
2132 
2133                 G_org_id := P_Emp_Org_Id;
2134                 G_Job_Id := P_Emp_Job_Id;
2135 
2136                 If G_org_id is NULL or G_Job_Id is NULL Then
2137                    IF PG_DEBUG = 'Y' THEN
2138                    pa_debug.G_err_stage := 'Calling pa_utils.GetEmpOrgJobId';
2139                       log_message('log_message: ' || pa_debug.G_err_Stage);
2140                    END IF;
2141                    pa_utils.GetEmpOrgJobId( G_person_id, X_ei_date, G_Org_Id, G_Job_Id ,p_po_number, p_po_line_num);
2142                   -- Added PO params for bug 4044057
2143                   -- Need to validate the assigment for the entered PO
2144 
2145                 End If;
2146 
2147 
2148                 IF ( G_org_id IS NULL or G_Job_Id IS NULL ) THEN
2149 
2150                      -- Commented for bug 4531168
2151                      -- IF p_po_number is null THEN
2152                      --     X_status := 'NO_ASSIGNMENT';
2153                      -- ELSE
2154                      --     X_status := 'NO_PO_ASSIGNMENT';
2155                      -- END IF;
2156                      -- pa_cc_utils.reset_curr_function;
2157                      -- RETURN;
2158 
2159                      /*Begin for bug 4531168*/
2160                      IF X_trx_src in ('OLD', 'GOLD', 'GOLDE') and p_po_number is null THEN
2161                           begin
2162                                select paa.job_id
2163                                into G_job_id
2164                                from PER_ALL_ASSIGNMENTS_F paa,
2165                                     psp_summary_lines psl
2166                                where paa.assignment_id        =  psl.assignment_id
2167                                and psl.effective_date       between paa.effective_start_date and paa.effective_end_date
2168                                and psl.SUMMARY_LINE_ID      =  X_trx_ref;
2169                           exception
2170                                when OTHERS then
2171                                     X_status := 'NO_ASSIGNMENT';
2172                                     pa_cc_utils.reset_curr_function;
2173                                     RETURN;
2174                           end;
2175                      ELSIF p_po_number is null then
2176                           X_status := 'NO_ASSIGNMENT';
2177                           pa_cc_utils.reset_curr_function;
2178                           RETURN;
2179                      ELSE
2180                           X_status := 'NO_PO_ASSIGNMENT';
2181                           pa_cc_utils.reset_curr_function;
2182                           RETURN;
2183                      END IF;
2184                      /* End for bug 4531168 */
2185 
2186                 END IF;
2187 
2188               END IF;  /* IF nvl(G_emp_org_oride,'N') = 'Y' */
2189 
2190               /* PA-K Changes: Commenting the CheckExporg, moved the check to one place below */
2191 
2192               /* PA-K Changes: For Performance, combined org and job id derivation */
2193 
2194             END IF; /* IF ( G_person_id IS NULL ) */
2195 
2196           -- ===========================================================================
2197           --   Get the organization ID for the incurred by organization name given
2198           ELSIF (  X_enum IS NULL   AND   X_oname IS NOT NULL ) THEN
2199 
2200             IF PG_DEBUG = 'Y' THEN
2201             pa_debug.G_err_Stage := 'Calling pa_utils.GetOrgnId';
2202                log_message('log_message: ' || pa_debug.G_err_Stage);
2203             END IF;
2204 
2205             G_person_id := NULL;
2206 	      --Start of changes for 3010848
2207 	      --G_org_id    := pa_utils.GetOrgId( X_oname );
2208 	      pa_utils.GetOrgnId(X_org_name => X_oname,
2209 				 X_bg_id    => G_Business_Group_Id,
2210 				 X_Orgn_Id  => G_org_id,
2211 				 X_Return_Status => L_Org_RetSts);
2212 
2213 	      If L_Org_RetSts is Not Null Then
2214 		 X_status := L_Org_RetSts;
2215 		 RETURN;
2216 	      End If;
2217 	      --End of changes for 3010848
2218 
2219 
2220             IF ( G_org_id IS NULL ) THEN
2221               X_status := 'INVALID_ORGANIZATION';
2222               pa_cc_utils.reset_curr_function;
2223               RETURN;
2224             END IF;
2225 
2226             /* PA-K Changes: Commenting the CheckExporg, moved the check to one place below */
2227 
2228           --  =============================================================================
2229           --  Trx_import enhancement:
2230           --  X_enum and X_oname are both NULL.  This can happen when transferring invoices
2231           --  with supplier not being an employee
2232           ELSE
2233 	        G_person_id := NULL;
2234 	        G_org_id    := NULL;
2235           END IF;  /*IF ( X_end_date <> trunc(pa_utils.GetWeekEnding( X_end_date )) ) */
2236 
2237       END IF; --Predefined source check for person and organization id
2238 
2239       -- PA-K Changes: Moved CheckExporg to below one place
2240       IF G_org_id is not NULL THEN
2241        IF pa_trx_import.g_skip_tc_flag <> 'Y' and PA_TRX_IMPORT.Get_GVal_ProjTskEi_Date = 'Y' then /* Added for Bug # 2170237 */
2242 	          -- Modified the above condition for BUG6931833
2243           IF PG_DEBUG = 'Y' THEN
2244           pa_debug.G_err_stage := 'Calling pa_utils2.CheckExporg';
2245              log_message('log_message: ' || pa_debug.G_err_Stage);
2246              log_message('log_message: ' || 'G_org_id = ' || G_org_id);
2247           END IF;
2248           IF pa_utils2.CheckExporg(G_org_id,X_ei_date) = 'N' then
2249              X_status := 'PA_EXP_ORG_NOT_ACTIVE';
2250              pa_cc_utils.reset_curr_function;
2251              RETURN;
2252           END IF;
2253        END IF;
2254       END IF;  /* Added for Bug # 2170237 */
2255 
2256       -- =============================================================================
2257       -- Trx_import enhancement:
2258       -- Get override-to organization ID if override_to_organization_name is provided
2259       --
2260       --PA-K Changes: Use the ID if provided for predefined transaction sources.
2261       /** Bug#3026218. Use ID if provided for any transaction source **/
2262       IF P_override_organization_id IS NOT NULL THEN
2263          G_override_to_org_id := P_override_organization_id;
2264       ELSE
2265          IF (X_override_to_oname IS NOT NULL) THEN
2266 
2267            IF PG_DEBUG = 'Y' THEN
2268            pa_debug.G_err_stage := 'Calling pa_utils.GetOrgnId for override org';
2269               log_message('log_message: ' || pa_debug.G_err_Stage);
2270            END IF;
2271 
2272         --Start of changes for bug 3010848
2273         --G_override_to_org_id := pa_utils.GetOrgId(X_override_to_oname);
2274 
2275         pa_cc_utils.log_message('X_override_to_oname = ' || X_override_to_oname
2276                                 || ' G_Business_Group_Id = ' || G_Business_Group_Id);
2277 
2278 
2279         pa_utils.GetOrgnId(X_org_name => X_override_to_oname,
2280                            X_bg_id    => G_Business_Group_Id,
2281                            X_Orgn_Id  => G_override_to_org_id,
2282                            X_Return_Status => L_Org_RetSts);
2283 
2284         If L_Org_RetSts is Not Null Then
2285            X_status := L_Org_RetSts;
2286            RETURN;
2287         End If;
2288         --End of changes for bug 3010848
2289 
2290 	   IF (G_override_to_org_id IS NULL) THEN
2291 		X_status := 'PA_OVERRIDE_ORG_INVALID';
2292                 pa_cc_utils.reset_curr_function;
2293 		RETURN;
2294            ELSIF pa_trx_import.g_skip_tc_flag <> 'Y' and PA_TRX_IMPORT.Get_GVal_ProjTskEi_Date = 'Y' THEN /* Added for Bug # 2170237 */
2295 		   --Modified above condition for bug6931833
2296              IF pa_utils2.CheckExporg(G_override_to_org_id, X_ei_date) = 'N' THEN
2297                 X_status := 'PA_OVERRIDE_ORG_NOT_ACTIVE';
2298                 pa_cc_utils.reset_curr_function;
2299                 RETURN;
2300   	     END IF;
2301            END IF; /* Added for Bug # 2170237 */
2302          ELSE
2303            -- Bug: 927655
2304            -- Reset G_override_to_org_id to NULL, so that the next transaction within
2305            -- the same expenditure does not get the same override to org.
2306            G_override_to_org_id := NULL;
2307 	   -- X_override_to_oname IS NULL, check if G_org_id IS NULL.
2308            -- If G_org_id is also NULL, then return error
2309 	   IF (G_org_id IS NULL) THEN
2310 		X_status := 'PA_EXP_ORG_NOT_SPECIFIED';
2311                 pa_cc_utils.reset_curr_function;
2312 		RETURN;
2313 	   END IF;
2314          END IF;
2315       END IF;
2316 
2317       -- =============================================================================
2318       -- Trx_import enhancement:
2319       -- Check the vendor number only if the current vendor number is different from
2320       -- the previous vendor number
2321       --PA-K Changes: Use the ID if provided for predefined transaction sources.
2322       /** Bug#3026218 Use Vendor Id if provided for any tyep of transaction source **/
2323       IF P_Vendor_Id is not NULL THEN
2324          G_vendor_id := P_Vendor_Id;
2325       ELSE
2326          IF ( X_vendor_number IS NOT NULL AND
2327            (G_previous_vendor_number <> X_vendor_number OR
2328             G_previous_vendor_number IS NULL ) ) THEN
2329 
2330           GetVendorId(P_vendor_number => X_vendor_number);
2331 
2332           IF (G_vendor_id IS NULL) THEN
2333 		X_status := 'PA_SUPPLIER_NUM_INVALID';
2334                 pa_cc_utils.reset_curr_function;
2335 		RETURN;
2336           END IF;
2337 
2338           G_previous_vendor_number := X_vendor_number;
2339          END IF;
2340       END IF;
2341 
2342       -- ===========================================================================
2343       --   Get the project and task IDs associated with the project and task
2344       --   numbers given
2345       --
2346       --  IF ( last_proj IS NULL   OR    X_pnum <> last_proj ) THEN
2347 
2348       --PA-K Changes: Use the ID if provided for predefined transaction sources.
2349       /** Bug#3026218. Use Project Id, if provided for any type of transaction source **/
2350       IF P_Project_Id is not null THEN
2351          G_project_id := P_Project_Id;
2352       ELSE
2353          IF PG_DEBUG = 'Y' THEN
2354          pa_debug.G_err_stage := 'Calling pa_utils.GetProjId';
2355             log_message('log_message: ' || pa_debug.G_err_Stage);
2356          END IF;
2357 
2358          G_project_id := pa_utils.GetProjId( X_pnum );
2359       END IF;
2360 
2361       --PA-K Changes:
2362       --IF (G_Task_Id IS NULL) THEN
2363       --PA-K Changes: Use the ID if provided for predefined transaction sources.
2364       /** Bug#3026218 Use Task Id, if provided for any type of transaction source **/
2365       IF P_Task_Id is not NULL THEN
2366          G_task_id := P_Task_Id;
2367       ELSE
2368          IF PG_DEBUG = 'Y' THEN
2369          pa_debug.G_err_stage := 'Calling  pa_utils.GetTaskId';
2370             log_message('log_message: ' || pa_debug.G_err_Stage);
2371          END IF;
2372 
2373          G_task_id := pa_utils.GetTaskId( G_project_id, X_tnum );
2374       END IF;
2375 
2376       -- ===========================================================================
2377       --   EXPENDITURE TYPE validation
2378       --      * Expenditure type given exists
2379       --      * Expenditure type has same system linkage as transaction source
2380       --      07/15/97- selva:   This above condition is not valid after project Manuf. changes.
2381       --      * Expenditure type is active as of the transaction item date
2382       --    IF ( last_etype IS NULL   OR   x_etype <> last_etype ) THEN
2383       --   Check pa_expend_typ_sys_links table for for existence and activeness of
2384       --   the given  exp_type/sys_link combination
2385 
2386       IF PG_DEBUG = 'Y' THEN
2387       pa_debug.G_err_stage := 'Calling GetEtypeEclassInfo';
2388          log_message('log_message: ' || pa_debug.G_err_Stage);
2389       END IF;
2390       GetEtypeEclassInfo(X_etype, X_system_linkage) ;
2391 
2392       IF  ( G_etype_link is NULL ) then
2393        X_status := 'INVALID_ETYPE_SYSLINK' ;
2394        pa_cc_utils.reset_curr_function;
2395        RETURN ;
2396       END IF ;
2397 
2398       IF  ( X_ei_date NOT BETWEEN G_etec_start
2399            AND nvl( G_etec_end, X_ei_date ) ) THEN
2400       X_status := 'ETYPE_SLINK_INACTIVE';
2401       pa_cc_utils.reset_curr_function;
2402       RETURN;
2403       END IF;
2404 
2405       --   Check pa_expenditure_types table for for existence and activeness of
2406       --   the given  exp_type combination
2407       IF PG_DEBUG = 'Y' THEN
2408       pa_debug.G_err_stage := 'Calling GetEtypeInfo';
2409          log_message('log_message: ' || pa_debug.G_err_Stage);
2410       END IF;
2411       GetEtypeInfo( X_etype, X_ei_date );
2412 
2413       IF ( NOT G_etype_active ) THEN
2414         X_status := 'INVALID_EXP_TYPE';
2415         pa_cc_utils.reset_curr_function;
2416         RETURN;
2417       ELSE
2418         last_etype := x_etype;
2419       END IF;
2420 
2421       --  Based on project journal design a transaction source can have more than one system_linkage
2422       --  So the validation of system_linkage of source with expenditure type is no more valid.
2423       --  The validation should be between the system_linkage of the record and the system_linkage of
2424       --  expenditure type of the same record. At present the system linkage is in pa_expenditure_types
2425       --  table. But once the new table for expenditure type and system_linkage is created, the given
2426       --  record's system linkage should be checked for existence in the new table.
2427       --  Selva 03/07/97
2428 
2429       IF  ( X_ei_date NOT BETWEEN G_etype_start
2430            AND nvl( G_etype_end, X_ei_date ) ) THEN
2431         X_status := 'EXP_TYPE_INACTIVE';
2432         pa_cc_utils.reset_curr_function;
2433         RETURN;
2434       END IF;
2435 
2436     ELSE  --Calling module = PAAPIMP
2437 
2438       IF PG_DEBUG = 'Y' THEN
2439       pa_debug.G_err_stage := 'Calling module is PAAPIMP';
2440          log_message('log_message: ' || pa_debug.G_err_stage);
2441       END IF;
2442 
2443       G_project_id := P_Project_Id;
2444       G_task_id    := P_Task_Id;
2445       G_Vendor_Id  := P_Vendor_Id;
2446       G_Person_Id  := P_Person_Id;
2447       G_override_to_org_id := P_Override_Organization_Id ;
2448 
2449        --
2450        -- 5235363 R12.PJ:XB4:DEV:APL:EXP ITEM DATE VALIDATIONS FOR SUPPLIER COST.
2451        -- Expenditure item related date validations are not relevant for Expense Reports.
2452        --
2453        IF PG_DEBUG = 'Y' THEN
2454 	  log_message('log_message: validate_exp_date x_module ='||x_module) ;
2455 	  log_message('log_message: validate_exp_date g_paapimp_validate_dt ='||g_paapimp_validate_dt) ;
2456 	  log_message('log_message: validate_exp_date x_system_linkage ='||x_system_linkage) ;
2457 	  log_message('log_message: validate_exp_date x_status ='||x_status) ;
2458        END IF ;
2459 
2460        IF (nvl(X_module, 'EXTERNAL') = 'PAAPIMP' and
2461           g_paapimp_validate_dt      = 'Y'       and
2462 	  x_system_linkage           = 'VI'      and
2463 	  x_status                  is NULL )   THEN
2464 
2465           IF PG_DEBUG = 'Y' THEN
2466 	     log_message('log_message: validate_exp_date p_project_id ='||p_project_id) ;
2467 	     log_message('log_message: validate_exp_date p_task_id ='||p_task_id) ;
2468 	     log_message('log_message: validate_exp_date p_organization_id ='||p_organization_id) ;
2469 	     log_message('log_message: validate_exp_date p_override_organization_id ='||p_override_organization_id) ;
2470 	     log_message('log_message: validate_exp_date p_vendor_id ='||p_vendor_id) ;
2471 	     log_message('log_message: validate_exp_date p_person_id ='||p_person_id) ;
2472 	     log_message('log_message: validate_exp_date x_system_linkage ='||x_system_linkage) ;
2473 	     log_message('log_message: validate_exp_date x_trx_source ='||x_trx_src) ;
2474 	  END IF ;
2475 
2476 	  -- ===================================================
2477           -- 5235363   R12.PJ:XB4:DEV:APL:EXP ITEM DATE VALIDATIONS FOR SUPPLIER COST.
2478 	  -- Following are not validated....
2479 	  -- PA adjustments for supplier invoice transactions
2480 	  -- Reversals
2481 	  -- Funds check adjustments
2482 	  -- Expenditure item adjustments
2483 	  -- Net ZERO Transactions for reversals
2484 	  -- ====================================================
2485 
2486           validate_exp_date( p_project_id,
2487 	                     p_task_id,
2488 			     NULL,
2489 			     p_override_organization_id, /*Bug 6146558 */
2490 			     p_vendor_id,
2491 			     p_person_id,
2492 			     x_ei_date,
2493 			     x_etype,
2494 			     x_system_linkage,
2495 			     x_trx_src,
2496 			     x_module,
2497 			     x_status,
2498 			     l_api_status) ;
2499 
2500           IF X_status is not null and l_api_status = fnd_api.g_ret_sts_error then
2501 
2502              --
2503 	     -- Bug:5502147 R12.PJ:XB9:QA:APL: ISSUES IN EI DATE VALIDATION IN PROJECTS
2504 	     -- Following is done to support Query by project number or task number in the transaction review form.
2505 	     --
2506              update pa_transaction_interface_all
2507                 Set project_number = ( select segment1
2508                                          from pa_projects_all
2509                                          where project_id = P_project_id ),
2510                      task_number   = ( select task_number from pa_tasks
2511                                        where  task_id = P_task_id)
2512                where txn_interface_id = X_txn_interface_id ;
2513 
2514 	     return ;
2515 	  END IF ;
2516        END IF ;
2517 
2518 
2519       --Bug 2719674: For AP EXPENSE, G_Org_Id equivalent to
2520       --pa_expenditures_all.incurred_by_organization_id and G_Job_Id should be populated
2521       --Since these values are derived while forming the grouping attributes,
2522       --just reuse the values.
2523       /* Bug 6498029: Base Bug 6339005: added OR ( X_trx_src= 'AP NRTAX' and X_system_linkage = 'ER' ) condition for populating G_Job_id if transaction source is 'Non-Recoverable Tax From Payables' */
2524       If ( X_trx_src= 'AP EXPENSE' OR ( X_trx_src= 'AP NRTAX' and X_system_linkage = 'ER' ))  Then
2525          -- Modified the following line to assign P_Organization_Id to  G_Org_id for
2526 	     -- bug#4689402 (forward port for 4614046)
2527          /* Begin bug 5400719:  See reasoning in 11i bug 5381025*/
2528          -- G_Org_Id := P_Organization_Id; --P_Emp_Org_Id;
2529          G_Org_Id := NVL(P_Override_Organization_Id, P_Organization_Id); /* added for bug 5381025 */ /*Bug 7343687*/
2530          /* End bug 5400719 */
2531          G_Job_Id := P_Emp_Job_Id;
2532 
2533          IF PG_DEBUG = 'Y' THEN
2534             pa_debug.G_err_stage := 'AP EXPENSE, G_Org_Id, G_Job_Id = ' || G_Org_Id || ',' || G_Job_Id;
2535             log_message('log_message: ' || pa_debug.G_err_stage);
2536          END IF;
2537 
2538          If (G_Org_Id is NULL or G_Job_Id is NULL) Then
2539              X_status := 'NO_ASSIGNMENT';
2540              pa_cc_utils.reset_curr_function;
2541              RETURN;
2542          End If;
2543 
2544       End If;
2545 
2546     END IF;  ---}
2547 
2548     IF ( G_project_id IS NULL ) THEN
2549         IF PG_DEBUG = 'Y' THEN
2550         pa_debug.G_err_stage := 'Project Id is null';
2551            log_message('log_message: ' || pa_debug.G_err_stage);
2552         END IF;
2553         X_status := 'INVALID_PROJECT';
2554         pa_cc_utils.reset_curr_function;
2555         RETURN;
2556     ELSE
2557         IF PG_DEBUG = 'Y' THEN
2558         pa_debug.G_err_stage := 'Calling GetProjTypeInfo';
2559            log_message('log_message: ' || pa_debug.G_err_Stage);
2560         END IF;
2561 
2562         -- Bug 2634812 : Deriving total_burden_flag
2563         GetProjTypeInfo( G_project_id,
2564                          G_Proj_bcost_flag,
2565                          G_project_type_class,
2566 			 G_burden_amt_display_method,
2567                          G_Total_Burden_Flag ) ;
2568 
2569         IF G_proj_bcost_flag IS NULL THEN
2570            IF PG_DEBUG = 'Y' THEN
2571            pa_debug.G_err_stage := 'Project Type is invalid';
2572               log_message('log_message: ' || pa_debug.G_err_stage);
2573            END IF;
2574 
2575            X_status := 'INVALID_PROJ_TYPE' ;
2576            pa_cc_utils.reset_curr_function;
2577            RETURN ;
2578         END IF ;
2579 
2580         /* PA-K Changes: Moved G_burden_amt_display_method to GetProjTypeInfo */
2581 
2582         last_proj := X_pnum;
2583 
2584         /* Bug 2726763: Remove reduntant call to pa_project_utils.check_project_action_allowed
2585                         This check is already present in PATC.
2586 
2587         -- ===========================================================================
2588         -- Check if new transactions are allowed against this project
2589 
2590         IF PG_DEBUG = 'Y' THEN
2591         pa_debug.G_err_stage := 'Calling pa_project_utils.check_project_action_allowed';
2592            log_message('log_message: ' || pa_debug.G_err_Stage);
2593         END IF;
2594 
2595         -- PA-K Changes: Using Caching for performance
2596 
2597         If (G_NewTxnPrjId = G_project_id) Then
2598 
2599             l_NewTxnsAllowed := G_NewTxnsAllowed;
2600 
2601         Else
2602 
2603             l_NewTxnsAllowed := pa_project_utils.check_project_action_allowed(
2604                                              X_project_id  => G_project_id,
2605                                              X_action_code => 'NEW_TXNS');
2606             G_NewTxnsAllowed := l_NewTxnsAllowed;
2607             G_NewTxnPrjId    := G_project_id;
2608 
2609         End If;
2610 
2611         IF l_NewTxnsAllowed = 'N'
2612         THEN
2613             IF PG_DEBUG = 'Y' THEN
2614             pa_debug.G_err_stage := 'Project does not allow new txns';
2615                log_message('log_message: ' || pa_debug.G_err_stage);
2616             END IF;
2617 
2618             X_status := 'PA_NEW_TXNS_NOT_ALLOWED';
2619             pa_cc_utils.reset_curr_function;
2620             RETURN;
2621         END IF;
2622         */
2623 
2624         -- ===========================================================================
2625         --   Check if the cross charging is allowed for this project
2626 
2627         --Bug 2726763: Add skip_tc_flag check for checking if project is chargeable.
2628         If pa_trx_import.g_skip_tc_flag <> 'Y' and PA_TRX_IMPORT.Get_GVal_ProjTskEi_Date = 'Y' then
2629 		        --Modified above condition for BUG6931833
2630 
2631            IF PG_DEBUG = 'Y' THEN
2632               pa_debug.G_err_stage := 'Calling pa_utils.IsCrossChargeable';
2633               log_message('log_message: ' || pa_debug.G_err_Stage);
2634            END IF;
2635 
2636            If not pa_utils.IsCrossChargeable(G_Project_Id) then
2637               X_Status := 'PA_PROJECT_NOT_VALID' ;
2638               pa_cc_utils.reset_curr_function;
2639               return ;
2640            End If ;
2641 
2642         End If ;
2643 
2644         --  ELSIF ( last_task IS NULL   OR   X_tnum <> last_task ) THEN
2645 
2646         IF ( G_task_id IS NULL ) THEN
2647           IF PG_DEBUG = 'Y' THEN
2648           pa_debug.G_err_stage := 'Task Id is null';
2649              log_message('log_message: ' || pa_debug.G_err_stage);
2650           END IF;
2651 
2652           X_status := 'INVALID_TASK';
2653           pa_cc_utils.reset_curr_function;
2654           RETURN;
2655         ELSE
2656           IF PG_DEBUG = 'Y' THEN
2657           pa_debug.G_err_stage := 'Calling pa_utils2.GetLaborCostMultiplier';
2658              log_message('log_message: ' || pa_debug.G_err_Stage);
2659           END IF;
2660           G_lcm  := pa_utils2.GetLaborCostMultiplier(G_Task_id);
2661           last_task := X_tnum;
2662         END IF;
2663 
2664     END IF;
2665 
2666     --FSIO Changes Starts
2667     --Populate Agreement_Number and ID if FSIO is enabled.
2668   /*  IF G_project_type_class = 'CONTRACT' AND G_FSIO_ENABLED = 'Y' THEN
2669       declare
2670         l_agreement_number pa_agreements.agreement_num%type;
2671         l_agreement_id number(15);
2672       begin
2673         PA_agreements_clnt_extn.get_agreement (
2674                                  p_project_id,
2675                                  p_task_id,
2676                                  x_ei_date,
2677                                  x_etype,
2678                                  NVL(p_organization_id, p_override_organization_id),
2679                                  p_vendor_id,
2680                                  p_person_id,
2681                                  x_system_linkage,
2682                                  l_agreement_number,
2683                                  l_agreement_id,
2684                                  x_trx_src,
2685                                  x_status,
2686                                  l_api_status);
2687         IF l_agreement_number IS NOT NULL AND l_agreement_id IS NOT NULL then
2688           declare
2689             l_dummy1 varchar2(1);
2690           begin
2691             SELECT 'Y'
2692             INTO l_dummy1
2693             FROM pa_agreements
2694             WHERE agreement_id  = l_agreement_id
2695               AND agreement_num = l_agreement_number;
2696           exception
2697             WHEN NO_DATA_FOUND then
2698             X_status := 'INVALID_AGREEMENT_FR_CLNT_EXTN';
2699             pa_cc_utils.reset_curr_function;
2700             RETURN;
2701           end;
2702           p_agreement_number := l_agreement_number;
2703           p_agreement_id     := l_agreement_id;
2704         END if;
2705       end;
2706       IF p_agreement_id IS NOT NULL or p_agreement_number IS NOT NULL then
2707         begin
2708           IF p_agreement_id IS NOT NULL then
2709             SELECT agreement_num
2710             INTO p_agreement_number
2711             FROM pa_agreements
2712             WHERE agreement_id = p_agreement_id;
2713           ELSIF p_agreement_number IS NOT NULL then
2714             SELECT agreement_id
2715             INTO p_agreement_id
2716             FROM pa_agreements
2717             WHERE agreement_num = p_agreement_number;
2718           END if;
2719         exception
2720           WHEN NO_DATA_FOUND then
2721             X_status := 'INVALID_AGREEMENT';
2722             pa_cc_utils.reset_curr_function;
2723             RETURN;
2724         end;
2725         IF p_agreement_id IS NULL or p_agreement_number IS NULL then
2726           X_status := 'INVALID_AGREEMENT';
2727           pa_cc_utils.reset_curr_function;
2728           RETURN;
2729         END if;
2730       else
2731         --Call default agreement derivation logic
2732         pa_billing.get_agreement_id (p_project_id,
2733                                      p_task_id,
2734                                      x_ei_date,
2735                                      p_agreement_id,
2736                                      p_agreement_number);
2737       END if;
2738     ELSE --IF G_project_type_class = 'CONTRACT' AND G_FSIO_ENABLED = 'Y' THEN
2739       p_agreement_id := NULL;
2740       p_agreement_number := NULL;
2741     END IF;
2742     --FSIO Changes Ends */
2743 
2744     -- ===================================================================
2745     -- Validation specific to Multi Currency Transactions
2746     --    * Check if user rate type is allowed for converting to
2747     --      functional currency
2748     --    * for acct_rate_type='User' check if exchange rate is populated
2749     --    * for acct_rate_type<> 'User' check if the rate type is valid
2750     --    * Check if user rate type is allowed for converting to
2751     --      project currency
2752     --    * for project_rate_type='User' check if exchange rate is populated
2753     --    * for project_rate_type<> 'User' check if the rate type is valid
2754     --    * check if denom currency code is populated.
2755     --    * check if transaction currency code is valid.
2756 
2757     -- Multi Currency validation common to uncosted,costed and accounted txns
2758     --
2759     /*-----------------------------------------------------------------------------
2760     -- Transaction currency should be always be available.
2761     -- if it is not availabe, then default it to functional currency code
2762     ------------------------------------------------------------------------------*/
2763     IF ( X_denom_currency_code IS NULL ) THEN
2764 
2765       X_denom_currency_code := G_accounting_currency_code;
2766 
2767     ELSE -- X_denom_currency_code IS NOT NULL
2768 
2769       IF PG_DEBUG = 'Y' THEN
2770       pa_debug.G_err_stage := 'Calling pa_multi_currency.validate_currency_code';
2771          log_message('log_message: ' || pa_debug.G_err_Stage);
2772       END IF;
2773 	IF (pa_multi_currency.validate_currency_code(
2774                      P_currency_code =>X_denom_currency_code,
2775                      P_ei_date       =>X_ei_date)= 'N')
2776 	THEN
2777 
2778 		X_status :='PA_INVALID_DENOM_CURRENCY';
2779                 pa_cc_utils.reset_curr_function;
2780 		RETURN;
2781 
2782 	END IF; -- end invalid denom currency validation
2783 
2784     END IF; -- End X_denom_currency_code IS NULL
2785 
2786     /*------------------------------------------------------------------------
2787     -- Get the project currency code, which is required for validation of project
2788     -- currency attributes. Get project currency information
2789     -------------------------------------------------------------------------*/
2790 
2791     IF PG_DEBUG = 'Y' THEN
2792     pa_debug.G_err_stage := 'Calling GetProjCurrInfo';
2793        log_message('log_message: ' || pa_debug.G_err_Stage);
2794     END IF;
2795 
2796     -- PA-I Changes: Get Proj Functional currency code
2797     GetProjCurrInfo(G_task_id,
2798 		X_project_currency_code,
2799 		l_project_rate_type,
2800                 X_projfunc_currency_code,
2801                 l_projfunc_cost_rate_type);
2802 
2803     --
2804     -- if project currency is null return null
2805     --
2806     IF ( X_project_currency_code IS NULL ) THEN
2807 
2808     	X_status := 'PA_MISSING_PROJ_CURR';
2809         pa_cc_utils.reset_curr_function;
2810     	RETURN;
2811 
2812     END IF;
2813 
2814     -- Start PA-I Changes
2815     -- if project functional currency is null return error
2816     --
2817     IF ( X_projfunc_currency_code IS NULL ) THEN
2818 
2819         X_status := 'PA_MISSING_PRJFUNC_CURR';
2820         pa_cc_utils.reset_curr_function;
2821         RETURN;
2822 
2823     END IF;
2824     -- End PA-I Changes
2825 
2826     /*-----------------------------------------------------------------------
2827     --  check if the functional rate type provided is valid.
2828     ------------------------------------------------------------------------*/
2829     IF  ( X_acct_rate_type IS NOT NULL ) THEN
2830 
2831       --Corrected Fix for Bug 2489534
2832       If (nvl(X_module, 'EXTERNAL') = 'PAAPIMP' OR
2833          -- S.N. Bug 3570261
2834            ((P_po_number IS NOT NULL OR P_Po_Line_Num IS NOT NULL OR
2835             P_Po_Header_Id IS NOT NULL OR P_Po_Line_Id IS NOT NULL) AND
2836             x_system_linkage in ('ST','OT'))
2837          -- E.N. Bug 3570261
2838          ) Then
2839 
2840 	 G_acct_rate_type := X_acct_rate_type;
2841 
2842       Else
2843 
2844          IF PG_DEBUG = 'Y' THEN
2845          pa_debug.G_err_stage := 'Calling pa_multi_currency.get_conversion_type';
2846             log_message('log_message: ' || pa_debug.G_err_Stage);
2847          END IF;
2848          G_acct_rate_type := pa_multi_currency.get_conversion_type(
2849                                P_user_rate_type => X_acct_rate_type);
2850 
2851       End If;
2852 
2853       IF ( G_acct_rate_type IS NULL ) THEN
2854 
2855          --Conversion rate type is invalid. Reject the txn
2856 
2857          X_status :=  'PA_INVALID_ACCT_RATE_TYPE';
2858          pa_cc_utils.reset_curr_function;
2859          RETURN;
2860       END IF; --G_acct_rate_type is null
2861 
2862     ELSE -- X_acct_rate_type is null
2863 
2864       G_acct_rate_type := NULL;
2865 
2866     END IF; -- X_acct_rate_type is not null
2867 
2868     /*-----------------------------------------------------------------------
2869     --  check if the project rate type provided is valid.
2870     ------------------------------------------------------------------------*/
2871     IF  ( X_project_rate_type IS NOT NULL ) THEN
2872 
2873       --Corrected Fix for Bug 2489534
2874       If (nvl(X_module, 'EXTERNAL') <> 'PAAPIMP') Then
2875          IF PG_DEBUG = 'Y' THEN
2876          pa_debug.G_err_stage := 'Calling pa_multi_currency.get_conversion_type';
2877             log_message('log_message: ' || pa_debug.G_err_Stage);
2878          END IF;
2879          G_project_rate_type := pa_multi_currency.get_conversion_type(
2880                                P_user_rate_type => X_project_rate_type);
2881       Else
2882          G_project_rate_type := X_project_rate_type;
2883       End If;
2884 
2885       IF ( G_project_rate_type IS NULL ) THEN
2886 
2887          --Conversion rate type is invalid. Reject the txn
2888          X_status :=  'PA_INVALID_PROJ_RATE_TYPE';
2889          pa_cc_utils.reset_curr_function;
2890          RETURN;
2891 
2892       END IF; --G_project_rate_type is null
2893 
2894     ELSE -- i.e. X_project_rate_type is null
2895 
2896      G_project_rate_type := NULL;
2897     END IF; -- X_project_rate_type is not null
2898 
2899     -- Start PA-I Changes
2900     /*-----------------------------------------------------------------------
2901     --  check if the project functional rate type provided is valid.
2902     ------------------------------------------------------------------------*/
2903     IF  ( X_projfunc_cost_rate_type IS NOT NULL ) THEN
2904 
2905       --Corrected Fix for Bug 2489534
2906       If (nvl(X_module, 'EXTERNAL') <> 'PAAPIMP') Then
2907          IF PG_DEBUG = 'Y' THEN
2908          pa_debug.G_err_stage := 'Calling pa_multi_currency.get_conversion_type';
2909             log_message('log_message: ' || pa_debug.G_err_Stage);
2910          END IF;
2911          G_projfunc_cost_rate_type := pa_multi_currency.get_conversion_type(
2912                                P_user_rate_type => X_projfunc_cost_rate_type);
2913       Else
2914          G_projfunc_cost_rate_type := X_projfunc_cost_rate_type;
2915       End If;
2916 
2917       IF ( G_projfunc_cost_rate_type IS NULL ) THEN
2918 
2919          --Conversion rate type is invalid. Reject the txn
2920 
2921          X_status :=  'PA_INVALID_PRJFUNC_CST_RT_TYP';
2922          pa_cc_utils.reset_curr_function;
2923          RETURN;
2924       END IF; --G_projfunc_cost_rate_type is null
2925 
2926     ELSE -- X_projfunc_cost_rate_type is null
2927 
2928       G_projfunc_cost_rate_type := NULL;
2929 
2930     END IF; -- X_projfunc_cost_rate_type is not null
2931     -- End PA-I Changes
2932 
2933     IF ( G_acct_rate_type = 'User' ) THEN
2934 
2935       -- check if rate type 'User' is allowed
2936       IF PG_DEBUG = 'Y' THEN
2937       pa_debug.G_err_stage := 'Calling pa_multi_currency.is_user_rate_type_allowed';
2938          log_message('log_message: ' || pa_debug.G_err_Stage);
2939       END IF;
2940       IF ( pa_multi_currency.is_user_rate_type_allowed(
2941          P_from_currency => X_denom_currency_code,
2942          P_to_currency => G_accounting_currency_code,
2943          P_conversion_date => nvl(X_acct_rate_date,sysdate))='N')
2944       THEN
2945 
2946       	-- If rate type 'User' is not allowed, reject the txn.
2947 			X_status := 'PA_NO_ACCT_USER_RATE_TYPE';
2948                         pa_cc_utils.reset_curr_function;
2949 	 		RETURN;
2950 
2951       ELSE -- Conversion Rate type 'User' is allowed
2952 
2953       -- If rate type 'User' is allowed then the exchange rate should
2954       -- be provided.
2955 
2956          IF ( X_acct_exchange_rate IS NULL ) THEN
2957             X_status := 'PA_ACCT_USER_RATE_NOT_DEFINED';
2958             pa_cc_utils.reset_curr_function;
2959             RETURN;
2960          END IF;-- end X_acct_exchange_rate IS NULL
2961 
2962       END IF;-- End is_user_rate_type_allowed ='N'
2963 
2964     END IF; -- End X_acct_rate_tye ='User'
2965 
2966     -- Perform the same validation for project conversion rate type
2967 
2968     IF ( G_project_rate_type = 'User' ) THEN
2969 
2970         IF PG_DEBUG = 'Y' THEN
2971         pa_debug.G_err_stage := 'Calling pa_multi_currency.is_user_rate_type_allowed';
2972            log_message('log_message: ' || pa_debug.G_err_Stage);
2973         END IF;
2974         IF ( pa_multi_currency.is_user_rate_type_allowed(
2975 			P_from_currency => X_denom_currency_code,
2976 			P_to_currency => X_project_currency_code,
2977 			P_conversion_date => nvl(X_project_rate_date,sysdate))='N')
2978 	THEN
2979 		X_status := 'PA_NO_PROJ_USER_RATE_TYPE';
2980                 pa_cc_utils.reset_curr_function;
2981 		RETURN;
2982 
2983 	ELSE  --conversion rate type 'User' is allowed
2984 
2985 		IF ( X_project_exchange_rate IS NULL ) THEN
2986 			X_status := 'PA_PROJ_USER_RATE_NOT_DEFINED';
2987                         pa_cc_utils.reset_curr_function;
2988 			RETURN;
2989 		END IF;-- End X_project_exchange_rate IS NULL
2990 
2991 	END IF;--End is_user_rate_type_allowed ='N'
2992 
2993     END IF;-- End G_project_rate_type = 'User'
2994 
2995     -- Start PA-I Changes
2996     -- Perform the same validation for project functional conversion rate type
2997 
2998     IF ( G_projfunc_cost_rate_type = 'User' ) THEN
2999 
3000       IF PG_DEBUG = 'Y' THEN
3001       pa_debug.G_err_stage := 'Calling pa_multi_currency.is_user_rate_type_allowed';
3002          log_message('log_message: ' || pa_debug.G_err_Stage);
3003       END IF;
3004        IF ( pa_multi_currency.is_user_rate_type_allowed(
3005                         P_from_currency => X_denom_currency_code,
3006                         P_to_currency => X_projfunc_currency_code,
3007                         P_conversion_date => nvl(X_projfunc_cost_rate_date,sysdate))='N')
3008        THEN
3009             X_status := 'PA_NO_PRJFUNC_CST_USER_RT_TYP';
3010             pa_cc_utils.reset_curr_function;
3011             RETURN;
3012 
3013        ELSE  --conversion rate type 'User' is allowed
3014 
3015             IF ( X_projfunc_cost_exchange_rate IS NULL ) THEN
3016                  X_status := 'PA_PRJFUNC_CST_USER_RATE_NULL';
3017                  pa_cc_utils.reset_curr_function;
3018                  RETURN;
3019             END IF;-- End X_project_exchange_rate IS NULL
3020 
3021        END IF;--End is_user_rate_type_allowed ='N'
3022 
3023     END IF;-- End G_projfunc_cost_rate_type = 'User'
3024     -- End PA-I Changes
3025 
3026     /*---------------------------------------------------------------------------
3027     --  END MULTI CURRENCY VALIDATION COMMON TO UNCOSTED,COSTED, ACCOUNTED TXNS
3028     ----------------------------------------------------------------------------*/
3029     --
3030     -- Multi currency validation for uncosted txns
3031     --
3032     -- if the txn is uncosted then
3033     -- * if expenditure types with cost rate flag = 'Y'
3034     --   and labor related system linkages, the denom_currency_code should be same
3035     --   as acct_currency_code because Oracle Projects maintains cost rates
3036     --   and employee compensations in functional currency only
3037     -- * for ER with receipt currency different
3038     --   from denom currency, the receipt amount should be populated.
3039     -- * for ER check if receipt currency code is valid
3040 
3041     IF ( nvl(G_trx_costed,'N') = 'N' ) THEN
3042 
3043        /* ----------------------------------------------------------------------------
3044        -- Prior to PA-K:
3045        -- Labor cost rates are entered in functional currency only hence the
3046        -- Transaction and Functional currencies should be same for labor transactions.
3047        -- Similarly transactions that require cost rate should have same transaction
3048        -- and functional currencies.
3049        -- With and Beyond PA-K:
3050        -- Labor Costing Enhancements, If Etype Labor Flag = Y, the denom and acct
3051           curreny could differ.
3052        ------------------------------------------------------------------------------*/
3053 
3054 	IF  nvl(G_etype_cost_rate_flag,'N') = 'Y'
3055            --OR nvl(G_etype_labor_flag,'N') = 'Y' )
3056 	THEN
3057 
3058 		IF ( X_denom_currency_code <> G_accounting_currency_code ) THEN
3059 
3060 			X_status := 'PA_DENOM_ACCT_CURR_DIFF';
3061                         pa_cc_utils.reset_curr_function;
3062 			RETURN;
3063 
3064 		END IF;
3065 
3066 	END IF;
3067 
3068         /*-----------------------------------------------------------------------------
3069         -- For Expense reports Users can enter the receipt currency information.  The
3070         -- following section validates if the user entered receipt currency is a valid
3071         -- currency in Oracle Applications.
3072         ------------------------------------------------------------------------------*/
3073 
3074         IF ( X_system_linkage = 'ER' ) THEN
3075 
3076            IF ( X_receipt_currency_code IS NOT NULL ) THEN
3077 
3078                     IF PG_DEBUG = 'Y' THEN
3079                     pa_debug.G_err_stage := 'Calling pa_multi_currency.validate_currency_code';
3080                        log_message('log_message: ' || pa_debug.G_err_Stage);
3081                     END IF;
3082                     IF (pa_multi_currency.validate_currency_code(
3083 					P_currency_code =>X_receipt_currency_code,
3084                                         P_ei_date => X_ei_date)= 'N')
3085 	    	    THEN
3086 
3087 			X_status :='PA_INVALID_RECEIPT_CURRENCY';
3088                         pa_cc_utils.reset_curr_function;
3089 			RETURN;
3090 
3091 		    END IF; -- end invalid receipt currency validation
3092 
3093                     /*----------------------------------------------------------------------------
3094                     -- If all the receipt currency is different from transaction currency, the user
3095                     -- should provide the receipt amount which will be used later to calculate the
3096                     -- receipt exchange rate.  If the amount is not provided, the transaction will
3097                     -- be rejected
3098                     -----------------------------------------------------------------------------*/
3099 
3100 		    IF ( nvl(X_receipt_currency_code,X_denom_currency_code) <> X_denom_currency_code ) THEN
3101 
3102 			IF ( nvl(X_receipt_currency_amount,0) = 0 ) THEN
3103 
3104 				X_status := 'PA_MISSING_RECEIPT_AMOUNT';
3105                                 pa_cc_utils.reset_curr_function;
3106 				RETURN;
3107 
3108 			ELSE -- receipt amount is not zero
3109 
3110 				-- derive the receipt exchange rate from the ratio of quantity
3111 				-- to receipt amount
3112 
3113                                 IF PG_DEBUG = 'Y' THEN
3114                                 pa_debug.G_err_stage := 'Calling pa_currency.round_trans_currency_amt';
3115                                    log_message('log_message: ' || pa_debug.G_err_Stage);
3116                                 END IF;
3117 				X_receipt_exchange_rate := (X_qty/
3118                                        pa_currency.round_trans_currency_amt1(
3119                                        X_receipt_currency_amount,
3120                                        nvl(X_receipt_currency_code,X_denom_currency_code)));
3121 
3122 
3123 			END IF; -- end receipt currency info validation
3124 
3125 		    END IF; -- end receipt curr code <> denom curr code
3126 
3127 	   END IF; -- end receipt curr code not null
3128 
3129         END IF; --end system linkage = ER
3130 
3131     END IF; -- end G_trx_costed ='N'
3132 
3133     /*-----------------------------------------------------------------------------
3134     --  END MULTI CURRENCY VALIDATION FOR UNCOSTED TXNS
3135     -----------------------------------------------------------------------------*/
3136 
3137     /*-----------------------------------------------------------------------------
3138     --  START MULTI CURRENCY VALIDATION FOR COSTED/UNCOSTED UNACCOUNTED TXNS
3139     -----------------------------------------------------------------------------*/
3140     /*  PA-I Changes
3141     For un-accounted transactions we need not call DefaultCurrAttributes.
3142     This is because the checks that are being performed here will be anyway
3143     performed during costing in pa_multi_currency_txn. To avoid redundant code
3144     we are not performing these checks in PA_TRX_IMPORT.
3145     */
3146 
3147     /*-----------------------------------------------------------------------------
3148     --  END MULTI CURRENCY VALIDATION FOR COSTED/UNCOSTED UNACCOUNTED TXNS
3149     -----------------------------------------------------------------------------*/
3150 
3151     --
3152     -- Multi currency validation for accounted transactions
3153     --
3154     -- If the transaction is accounted then
3155     -- * Currency information to convert from transaction to functional currency
3156     --   should be provided if transaction and functional currencies are different
3157     -- * The acct_raw_cost should be populated
3158     -- * validate if the user provided functional raw cost is with in the
3159     --   tolerance
3160     -- * if functional and transaction currencies are same the corresponding
3161     --   raw costs should be equal.
3162     -- * calculate project raw cost
3163     --
3164 
3165     IF ( nvl(G_gl_accted_flag,'N') = 'Y' ) THEN
3166 
3167 	-- Functional raw cost should be provided for accounted transactions
3168         /*Bug# 2168903:Replaced the check of nvl(X_acct_raw_cost,0) = 0 with
3169            x_acct_raw_cost is NULL ,in the IF condition below */
3170 
3171 	IF ( X_acct_raw_cost IS NULL AND  X_system_linkage <>'BTC' ) THEN
3172 
3173 		X_status := 'PA_NO_ACCT_COST';
3174                 pa_cc_utils.reset_curr_function;
3175 		RETURN;
3176 
3177 	END IF; -- acct_raw_cost is null
3178 
3179 	IF ( nvl(X_denom_currency_code,G_accounting_currency_code) <> G_accounting_currency_code) THEN
3180 
3181                 /* --------------------------------------------------------------------
3182 		-- If Transaction and Functional currencies are not same the user should
3183 		-- provide conversion attributes(i.e X_acct_rate_date,X_acct_rate_type,
3184 		-- X_acct_exchange_rate).
3185                 ---------------------------------------------------------------------*/
3186 		IF ( X_acct_rate_date IS NULL ) THEN
3187 
3188 			X_status := 'PA_NO_ACCT_CURR_RATE_DATE';
3189                         pa_cc_utils.reset_curr_function;
3190 			RETURN;
3191 
3192 		END IF; -- End X_acct_rate_date is NULL
3193 
3194 		IF (G_acct_rate_type IS NULL) THEN
3195 
3196 			X_status := 'PA_NO_ACCT_CURR_RATE_TYPE';
3197                         pa_cc_utils.reset_curr_function;
3198 			RETURN;
3199 
3200 		END IF; -- End G_acct_rate_type IS NULL
3201 
3202             /* Starts - Commented for bug# 5890661
3203 		IF ( X_acct_exchange_rate IS NULL) THEN
3204 
3205 			X_status := 'PA_NO_ACCT_CURR_RATE';
3206                         pa_cc_utils.reset_curr_function;
3207 			RETURN;
3208 
3209 		END IF; -- End X_acct_exchange_rate IS NULL
3210 	Ends - commented for bug# 5890661 */
3211 
3212                 /*-------------------------------------------------------------------
3213 		-- Validate if the user provided Functional raw cost is with in the
3214 		-- tolerance( tolerance is identified by acct_round_limit column).
3215                 -- This is done by calculating the functional raw cost from the
3216                 -- conversion attributes provided by the user, the calculated
3217                 -- functional raw cost is then compared with the fucntional cost
3218                 -- provided by the user. If acct_round_limit is NULL then it will be
3219                 -- assumed that the acct_round_limit is Zero(0).
3220 		--------------------------------------------------------------------*/
3221 
3222                l_acct_rate_date := X_acct_rate_date;
3223                l_acct_rate_type := G_acct_rate_type;
3224    	       l_acct_exchange_rate := X_acct_exchange_rate;
3225 	       l_acct_raw_cost := X_acct_raw_cost;
3226 
3227                IF PG_DEBUG = 'Y' THEN
3228                pa_debug.G_err_stage := 'Calling pa_multi_currency.convert_amount';
3229                   log_message('log_message: ' || pa_debug.G_err_Stage);
3230                END IF;
3231 
3232 	       pa_multi_currency.convert_amount(
3233                                 P_from_currency         => X_denom_currency_code,
3234 				P_to_currency           => G_accounting_currency_code,
3235 				P_conversion_date       => l_acct_rate_date,
3236 				P_conversion_type       => l_acct_rate_type,
3237 				P_amount                => X_denom_raw_cost,
3238                    	        P_user_validate_flag    => l_validate_user_rate_type,
3239 	  			P_handle_exception_flag => l_handel_exception_flag,
3240 				P_converted_amount      => l_converted_amount,
3241 				P_denominator           => l_denominator,
3242 				P_numerator             => l_numerator,
3243 				P_rate                  => X_acct_exchange_rate,
3244 			        X_status                => l_status);
3245 
3246 		IF ( l_status IS NOT NULL ) THEN
3247 
3248 			X_status := l_status;
3249                         pa_cc_utils.reset_curr_function;
3250 			RETURN;
3251 
3252 		END IF; -- End l_status IS NOT NULL
3253 
3254 
3255 		-- Check if the calculated functional raw cost is with in the
3256 		-- tolerance
3257 
3258                 -- Bug 1603841
3259                 -- If transaction_source is any of the following then
3260                 -- do not check for the rounding limit.
3261 
3262                 IF X_trx_src not in ('AP INVOICE' ,'AP EXPENSE','AP NRTAX', 'AP DISCOUNTS', 'AP ERV',
3263                                      --PA-J Receipt Accrual changes
3264                                  'AP VARIANCE', 'PO RECEIPT', 'PO RECEIPT NRTAX',
3265 				 'Inventory','Inventory Misc','Work In Process', /*Bug4202839*/
3266 				  'CSE_INV_ISSUE','CSE_INV_ISSUE_DEPR', /*Bug4202839*/
3267 				  'PJM_CSTBP_INV_NO_ACCOUNTS','PJM_CSTBP_WIP_NO_ACCOUNTS', /*Bug4202839*/
3268 				  'PJM_CSTBP_WIP_ACCOUNTS','PJM_CSTBP_INV_ACCOUNTS',
3269 				  'PJM_CSTBP_ST_ACCOUNTS','PJM_NON_CSTBP_ST_ACCOUNTS','PJM_CSTBP_ST_NO_ACCOUNTS',/*Bug4202839*/
3270 				 'PO RECEIPT NRTAX PRICE ADJ', 'PO RECEIPT PRICE ADJ',
3271                          'INTERPROJECT_AP_INVOICES','INTERCOMPANY_AP_INVOICES') THEN /* Added the 2 transaction sources for bug 3461993 */
3272 				-- pricing changes
3273 
3274 			IF abs(l_converted_amount - X_acct_raw_cost) >
3275 				abs(nvl(X_acct_exchange_rounding_limit,0)) THEN
3276 
3277 				X_status := 'PA_EXCEED_ROUND_LIMIT';
3278                                 pa_cc_utils.reset_curr_function;
3279 				RETURN;
3280 
3281 			END IF; -- end functional amount tolerance check
3282                 ELSE
3283                 -- Bug 4038568
3284                 -- If source is AP/PO, copy the exchange rate from the source system instead of rederiving from GL
3285 
3286                         X_acct_exchange_rate := nvl(l_acct_exchange_rate,X_acct_exchange_rate);
3287 
3288                 END IF;
3289 
3290 	ELSE -- denom currency_ = accounting currency
3291 
3292 		--
3293 		-- If functional and transaction currencies are same the the
3294 		-- corresponding amounts should also be equal
3295 		--
3296                 IF PG_DEBUG = 'Y' THEN
3297                 pa_debug.G_err_stage := 'Calling pa_multi_currency.round_trans_currency_amt';
3298                    log_message('log_message: ' || pa_debug.G_err_Stage);
3299                 END IF;
3300 
3301 	/* Added the call to round off X_acct_raw_cost for bug 2871273 */
3302 	      	IF ( pa_currency.round_trans_currency_amt1(X_acct_raw_cost, G_accounting_currency_code) <>
3303                      pa_currency.round_trans_currency_amt1(X_denom_raw_cost,
3304                      X_denom_currency_code )) THEN
3305 
3306 			X_status := 'PA_INVALID_ACCT_DENOM_COST';
3307                         pa_cc_utils.reset_curr_function;
3308 			RETURN;
3309 
3310 		END IF; -- end acct raw cost <> denom raw cost
3311 
3312 	END IF; -- denom <> acct curr
3313 
3314      	--
3315      	-- call get_currency_amounts to derive project currency amount
3316      	-- for accounted transactions.  For accounted transactions
3317      	-- the EI's are created with cost_distributed_flag='Y', so
3318      	-- the the costing program cannot pick up these records
3319      	-- to calculate the project currency costs. Here we are passing
3320      	-- the G_gl_accted_flag to pa_multi_currency_txn package, the
3321      	-- package will calculate the project cost if the gl_accted_flag
3322      	-- is 'Y'.
3323      	--
3324         -- initilize the local variables before passing them to
3325         -- get_currency_amounts
3326 
3327         l_acct_rate_date := X_acct_rate_date;
3328     	l_acct_rate_type := G_acct_rate_type;
3329 	l_acct_exchange_rate := X_acct_exchange_rate;
3330 	l_acct_raw_cost := X_acct_raw_cost;
3331 
3332      	BEGIN
3333 
3334                 IF PG_DEBUG = 'Y' THEN
3335                 pa_debug.G_err_stage := 'Calling pa_multi_currency_txn.get_currency_amounts';
3336                    log_message('log_message: ' || pa_debug.G_err_Stage);
3337                 END IF;
3338      		pa_multi_currency_txn.get_currency_amounts(
3339 				P_task_id          => G_task_id,
3340 				P_ei_date          => X_ei_date,
3341 				P_denom_raw_cost   => X_denom_raw_cost,
3342 				P_denom_curr_code  => X_denom_currency_code,
3343 				P_acct_curr_code   => G_accounting_currency_code,
3344 				P_accounted_flag   => G_gl_accted_flag,
3345 				P_acct_rate_date   => l_acct_rate_date,
3346 				P_acct_rate_type   => l_acct_rate_type,
3347 				P_acct_exch_rate   => l_acct_exchange_rate,
3348                                 P_acct_raw_cost    => l_acct_raw_cost,
3349 				P_project_curr_code => X_project_currency_code,
3350 				P_project_rate_type => G_project_rate_type,
3351 				P_project_rate_date => X_project_rate_date,
3352 				P_project_exch_rate => X_project_exchange_rate,
3353 			       --P_project_raw_cost => X_project_raw_cost,
3354                                --PA-I Changes : Proj Func raw cost is calculated in P_raw_cost
3355 			       --P_raw_cost          => X_project_raw_cost,
3356 			        P_projfunc_raw_cost          => X_project_raw_cost,
3357 				P_status            => l_status,
3358 				P_stage             => l_stage,
3359                                --PA-I Changes : Added Proj Func Attributes and Project raw cost
3360                                 P_projfunc_curr_code => X_projfunc_currency_code,
3361                                 P_projfunc_cost_rate_type => G_projfunc_cost_rate_type,
3362                                 P_projfunc_cost_rate_date => X_projfunc_cost_rate_date,
3363                                 P_projfunc_cost_exch_rate => X_projfunc_cost_exchange_rate,
3364                                 P_project_raw_cost   => X_actual_project_raw_cost);
3365 
3366     	/***************PA-I changes************************
3367          Commenting the exception section as the exceptions are already handled in
3368          the pa_multi_currency_txn pkg.
3369         ****************************************************/
3370 
3371     	END; -- end calculate project raw cost
3372 
3373 
3374 	IF ( l_status IS NOT NULL ) THEN
3375 		X_status := l_status;
3376                 pa_cc_utils.reset_curr_function;
3377         	RETURN;
3378 
3379 	END IF; -- end l_status IS NOT NULL
3380 
3381     END IF; -- end gl_accounted_flag =Y
3382 
3383     -- ===========================================================================
3384     --   Validation specific to STRAIGHT_TIME transactions
3385     --     * Transaction item date must be within the expenditure week ending
3386     --       on the ending date given
3387     --     * Employee must be specified
3388     --
3389     -- Bug 1000221, added OT to the in clause
3390 
3391     IF ( X_system_linkage in ( 'ST', 'OT' )) THEN
3392 
3393       IF PG_DEBUG = 'Y' THEN
3394       pa_debug.G_err_stage := 'Calling pa_utils.DateInExpWeek';
3395          log_message('log_message: ' || pa_debug.G_err_Stage);
3396       END IF;
3397 
3398       IF ( NOT pa_utils.DateInExpWeek( X_ei_date , X_end_date  ) ) THEN
3399           X_status := 'ITEM_NOT_IN_WEEK';
3400           pa_cc_utils.reset_curr_function;
3401           RETURN;
3402       ELSIF ( X_enum IS NULL ) THEN
3403           X_status := 'EMP_MAND_FOR_TIME';
3404           pa_cc_utils.reset_curr_function;
3405           RETURN;
3406       END IF;
3407 
3408     -- ===========================================================================
3409     --   Validation specific to EXPENSE_REPORTS transactions
3410     --     * Employee must be specified
3411     --
3412 
3413     ELSIF ( X_system_linkage = 'ER' ) THEN
3414       IF ( X_enum IS NULL and X_trx_src NOT IN  ('AP EXPENSE', 'AP NRTAX') ) THEN
3415                            /* Bug2780387. Added 'AP NRTAX' to the NOT IN list */
3416           X_status := 'EMP_MAND_FOR_ER';
3417           pa_cc_utils.reset_curr_function;
3418           RETURN;
3419       END IF;
3420 
3421     -- ===========================================================================
3422     --   Validation specific to USAGES items
3423     --     * Employee OR organization must be given
3424     --     * Non-labor resource and non-labor resource owning organization must
3425     --       be given
3426     --     * Non-labor resource and non-labor resource owning organization are
3427     --       both valid
3428     --     * Non-labor resource is active as of the transaction item date
3429     --     * Non-labor resource is owned by the given non-labor resource org as of
3430     --       the transaction item date
3431     --     * Expenditure type of the non-labor resource matches that of the
3432     --       transaction
3433     --
3434 
3435     ELSIF ( X_system_linkage = 'USG' ) THEN
3436       IF ( X_enum IS NULL    AND    X_oname IS NULL ) THEN
3437           X_status := 'EMP_OR_ORG_MAND_FOR_USAGES';
3438           pa_cc_utils.reset_curr_function;
3439           RETURN;
3440       ELSIF ( X_nlr IS NULL ) THEN
3441           X_status := 'NL_RSRC_MAND_FOR_USAGES';
3442           pa_cc_utils.reset_curr_function;
3443           RETURN;
3444       ELSIF ( X_nlro_name IS NULL ) THEN
3445           X_status := 'NL_RSRC_ORG_MAND_FOR_USAGES';
3446           pa_cc_utils.reset_curr_function;
3447           RETURN;
3448       END IF;
3449 
3450       --PA-K Changes: Use the ID if provided for predefined transaction sources.
3451       /** Bug#3026218 Use ID, if provided for any type of transaction source **/
3452       IF P_NLR_Org_Id is not null Then
3453          G_nlro_id := P_NLR_Org_Id;
3454       ELSE
3455          IF PG_DEBUG = 'Y' THEN
3456          pa_debug.G_err_stage := 'Calling pa_utils.GetOrgnId';
3457             log_message('log_message: ' || pa_debug.G_err_Stage);
3458          END IF;
3459       --Start of changes for Bug 3010848
3460 
3461       --G_nlro_id := pa_utils.GetOrgId( X_nlro_name );
3462       pa_utils.GetOrgnId(X_org_name => X_nlro_name,
3463                          X_bg_id    => G_Business_Group_Id,
3464                          X_Orgn_Id  => G_nlro_id,
3465                          X_Return_Status => L_Org_RetSts);
3466 
3467       If L_Org_RetSts is Not Null Then
3468          X_status := L_Org_RetSts;
3469          RETURN;
3470       End If;
3471       --End of changes for Bug 3010848
3472 
3473       END IF;
3474 
3475       IF ( G_nlro_id IS NULL ) THEN
3476           X_status := 'INVALID_NL_RSRC_ORG';
3477           pa_cc_utils.reset_curr_function;
3478           RETURN;
3479       END IF;
3480 
3481       IF PG_DEBUG = 'Y' THEN
3482       pa_debug.G_err_stage := 'Calling GetNlrInfo(X_nlr,G_nlro_id) ';
3483          log_message('log_message: ' || pa_debug.G_err_Stage);
3484       END IF;
3485       GetNlrInfo( X_nlr, G_nlro_id );
3486 
3487       IF ( G_nlr_etype IS NULL ) THEN
3488           X_status := 'INVALID_NL_RSRC';
3489           pa_cc_utils.reset_curr_function;
3490           RETURN;
3491       ELSIF ( X_ei_date NOT BETWEEN  G_nlr_start
3492                          AND  nvl( G_nlr_end, X_ei_date ) ) THEN
3493           X_status := 'NL_RSRC_INACTIVE';
3494           pa_cc_utils.reset_curr_function;
3495           RETURN;
3496       ELSIF ( G_nlro_start IS NULL ) THEN
3497           X_status := 'ORG_NOT_OWNER_OF_NL_RSRC';
3498           pa_cc_utils.reset_curr_function;
3499           RETURN;
3500       ELSIF ( X_ei_date NOT BETWEEN  G_nlro_start
3501                AND  nvl( G_nlro_end, X_ei_date ) ) THEN
3502           X_status := 'ORG_NOT_OWNER_OF_NL_RSRC';
3503           pa_cc_utils.reset_curr_function;
3504           RETURN;
3505       ELSIF ( G_nlr_etype <> X_etype ) THEN
3506           X_status := 'NL_EXP_TYPE_DIFF';
3507           pa_cc_utils.reset_curr_function;
3508           RETURN;
3509       END IF;
3510     END IF;
3511 
3512     -- ===========================================================================
3513     --  Validation specific to BURDEN TRANSACTIONS
3514     --  If transactionsource.cost_burden_flag = 'N' then don't allow burden trans.
3515     --  If ProjectType.burden_cost_flag = 'N' then don't allow burden trans.
3516     --  If both qty and raw cost are non zero  then don't allow burden trans.
3517     --  If burdened cost is zero  then don't allow burden trans.
3518 
3519     IF   X_system_linkage = 'BTC' THEN
3520 
3521         /* Bug# 2063667 - If the transaction source is not Burdened but
3522            system linkage is BTC , then reject the transaction */
3523        /* Bug 2844973 Added g_trx_source <> 'ALLOCATIONS' condition */
3524 
3525       IF nvl(G_burdened_flag,'N') = 'N'  and g_trx_source <> 'ALLOCATIONS' THEN
3526          X_status := 'TRXSRC_NOTALLOW_BURDEN' ;
3527          pa_cc_utils.reset_curr_function;
3528          RETURN ;
3529       END IF ;
3530 
3531       --      IF G_proj_bcost_flag = 'N' THEN
3532       --         X_status := 'PROJ_NOTALLOW_BURDEN' ;
3533       --         RETURN ;
3534       --      END IF ;
3535       -- Multi-Currency changes. Changed raw_cost to denom_raw_cost
3536       --
3537       IF nvl(X_qty,0) <> 0  OR nvl(X_denom_raw_cost,0) <> 0 THEN
3538         X_status := 'INVALID_BURDEN_TRANS' ;
3539         pa_cc_utils.reset_curr_function;
3540         RETURN ;
3541       END IF ;
3542 
3543       --
3544       -- Multi-Currency Changes. Changes burdened_cost to denom_burdened-cost
3545       --
3546       IF X_denom_burdened_cost IS NULL THEN
3547          X_status := 'INVALID_BURDEN_AMOUNT' ;
3548          pa_cc_utils.reset_curr_function;
3549          RETURN ;
3550       END IF ;
3551 
3552     ELSE --  X_system_linkage <> 'BTC'
3553 
3554       IF ( G_trx_costed = 'Y' AND X_denom_raw_cost is NULL  ) THEN
3555           X_status := 'NO_RAW_COST';
3556           pa_cc_utils.reset_curr_function;
3557           RETURN;
3558       END IF ;
3559 
3560       -- Get compiled multiplier and compile set id for transaction sources
3561       -- that have allow_burden_flag set to N and gl_accounted_flag set to Y
3562       -- Bug: 979112: Get compiled multiplier only if the project type is
3563       --      defined with a burden schedule
3564 
3565       -- All the below logic is commented for 2798971 has been
3566       -- handled while calculating the Burdened cost.
3567       -- After this change Burden Cost is calculated by calling
3568       -- pa_cost_plus1.get_indirect_cost_import procedure.
3569       -- instead of compiled multiplier since this was leading to
3570       -- a penny difference in Burden cost and Accrued revenue.
3571 /* --Commented code for Bug 2798971
3572       IF ( nvl(G_burdened_flag,'N') = 'N' ) AND
3573          ( nvl( G_gl_accted_flag,'N' ) = 'Y') AND
3574          ( nvl(G_proj_bcost_flag, 'N') = 'Y') THEN
3575 
3576          IF PG_DEBUG = 'Y' THEN
3577          pa_debug.G_err_stage := 'Calling pa_cost_plus1.get_compile_set_info';
3578             log_message('log_message: ' || pa_debug.G_err_Stage);
3579          END IF;
3580 	 pa_cost_plus1.get_compile_set_info(
3581                         task_id             => G_task_id,
3582                         p_txn_interface_id  => X_txn_interface_id, -- added for bug 2563364
3583 	                effective_date      => X_ei_date,
3584 		 	expenditure_type    => X_etype,
3585 			organization_id     => nvl(G_override_to_org_id, G_org_id),
3586 			schedule_type       => 'C',
3587 			Compiled_multiplier => G_compiled_multiplier,
3588 			compiled_set_id     => G_burden_compile_set_id,
3589 			status              => l_status_num,
3590 			stage               => l_stage,
3591             x_cp_structure      => x_cp_structure -- Added for Bug 5743708,
3592             x_cost_base         => x_cost_base-- Added for Bug 5743708 );
3593 
3594 
3595          --   Bug: 925488 and 956683: get_compile_set_info calls get_cost_base. Get_cost_base
3596          --       returns status = 100 when a expenditure type is not defined with a
3597          --       cost base.  We should not return error when this happens.  Instead,
3598          --       we'll set compiled multiplier to 0 so when we calculate burdened cost,
3599          --       burdened_cost = raw_cost * (1 + compiled_multipler), burdened_cost
3600          --       will equal to raw_cost
3601          IF ( l_status_num <> 0 ) THEN
3602 	   IF (l_status_num = 100) THEN
3603                G_compiled_multiplier := 0;
3604                G_burden_compile_set_id := to_number(NULL);
3605            ELSE
3606 	       X_status := 'PA_ERR_IN_COST_PLUS';
3607                pa_cc_utils.reset_curr_function;
3608      	       RETURN;
3609            END IF;
3610          END IF;
3611 
3612         -- code added for the Bug #1428216 starts here
3613              IF (NVL(G_burden_amt_display_method,'S') = 'D' ) THEN
3614                      G_compiled_multiplier := 0;
3615              END IF;
3616         -- code added for the Bug #1428216 endss here
3617 
3618         --    Bug: 979112: If the project type is not defined with burden schedule,
3619         --                 then set compiled_multiplier equal to 0 because the
3620         --                 formula is burden_cost = raw_cost * (1 + multipler)
3621       ELSE
3622 	 G_compiled_multiplier := 0;
3623          G_burden_compile_set_id := to_number(NULL);
3624 
3625       END IF;	-- End G_burdened_flag = N and G_gl_accted_flag =Y
3626 
3627  --Commented code for Bug 2798971 ends */
3628 
3629       -- If the transaction is burdened and does not have a burdened amount
3630       --        then reject the transaction
3631 
3632       IF nvl(G_burdened_flag,'N') = 'Y' AND
3633 /*		nvl( X_denom_burdened_cost ,0 ) = 0  THEN       Commented for bug3144614       */
3634                 X_denom_burdened_cost is NULL        THEN      /* Added for bug3144614 */
3635           X_status := 'INVALID_BURDEN_AMOUNT' ;
3636           pa_cc_utils.reset_curr_function;
3637           RETURN ;
3638       END IF; -- End
3639 
3640       /* Bug# 2063667 - If the transaction source is not Burdened and
3641       not externally gl accounted but burden amount is not null,
3642       then reject the transaction */
3643 
3644       IF (nvl(G_burdened_flag,'N') = 'N' AND
3645             nvl(G_gl_accted_flag,'N') = 'N' AND
3646                         X_denom_burdened_cost is NOT NULL) THEN
3647           X_status := 'TRXSRC_NOTALLOW_BURDEN';
3648           pa_cc_utils.reset_curr_function;
3649           RETURN ;
3650       END IF ;
3651 
3652       /* Bug# 2063667 - If the project type is not Burdened but burden
3653       amount is not null, then reject the transaction */
3654 
3655       /*Bug# 2448543 -If the project type is not burdened (which implies
3656         the defaulted burden amount should be equal to raw amount )but the
3657         burden amount is not equal to raw amount then reject the txn.*/
3658 
3659       /* Bug#2529120 - Added Check for Source Accounted Over fix done for 2448543
3660          For a Non Burdend Project, reject the transaction for the following conditions
3661          A. If Source is GL Unaccounted and denom burdened cost is not null
3662          B. If Source is GL accounted and denom burdened cost <> denom raw cost
3663       */
3664       /* Bug#2529120  -- Comment Code - Start
3665       IF (nvl(G_proj_bcost_flag,'N') = 'N' AND
3666             X_denom_burdened_cost <> X_denom_raw_cost) THEN           * 2448543*
3667          * X_denom_burdened_cost is NOT NULL) THEN -Commented for Bug# 2448543*
3668        Comment Code -- End -- Bug#2529120 */
3669 
3670       /* commented for Bug 3593432
3671       IF nvl(G_proj_bcost_flag,'N') = 'N' THEN
3672         IF ((nvl(G_gl_accted_flag,'N') = 'Y' AND X_denom_burdened_cost <> X_denom_raw_cost)
3673             OR (nvl(G_gl_accted_flag,'N') = 'N' AND X_denom_burdened_cost is NOT NULL)) THEN
3674                 X_status := 'PROJ_NOTALLOW_BURDEN' ;
3675                 pa_cc_utils.reset_curr_function;
3676                 RETURN ;
3677         END IF; * Accted Flag IF *
3678       END IF ;
3679        commented for Bug 3593432 End */
3680 
3681       /* Added for Bug 3593432   */
3682       IF nvl(G_proj_bcost_flag,'N') = 'N' AND X_denom_burdened_cost <> X_denom_raw_cost THEN
3683                 X_status := 'PROJ_NOTALLOW_BURDEN' ;
3684                 pa_cc_utils.reset_curr_function;
3685                 RETURN ;
3686       END IF ;
3687       /* Added for Bug 3593432  End  */
3688 
3689 
3690     END IF; -- End system_linkage <> BTC
3691 
3692     -- ============================================================================
3693     -- --------------------------------------------------------------------------
3694     -- Multi-Currency changes. calculating burden cost for accounted
3695     -- transactions.
3696     --
3697     -- Example: denom raw cost      acct_raw_cost   		proj_raw_cost
3698     --          ----------------     ------------------   -------------
3699     -- 	    	100 USD					120 BPL					120 BPL(derived)
3700     -- if acct_burdened_cost is null and denom_burdened_cost is 120 USD
3701     -- the acct_burdened_cost is derived by multiplying acct_raw_cost with the
3702     -- ratio of denom_burdened_cost and denom_raw_cost( for non BTC txns )
3703     -- for BTC txns since the denom_raw_cost will be zero(0), the func and proj
3704     -- Bcost are derrived by directly converting the denom_burden_cost using the
3705     -- project and functional currency conversion attributes.
3706     --
3707     -- For non BTC txns
3708     -- i.e acct_burdened_cost = (denom_burdened_cost/denom_raw_cost) * acct_raw_cost
3709     --     proj_burdened_cost = (denom_burdened_cost/denom_raw_cost) * proj_raw_cost
3710     --
3711     -- For BTC txns ( transaction raw cost = 0 )
3712     -- acct_burdened_cost = (transaction to functional curr conversion rate) *
3713     --                       transaction burdened cost
3714     -- project_burdened-cost = (transaction to project curr conversion rate)*
3715     --                       transaction burdened cost
3716     --
3717     -- if acct_burdened_cost is not null then
3718     -- derive proj_burdened_cost by multiplying project_raw_cost with the ratio
3719     -- of denom_burdened_cost and denom_raw_cost.
3720     --
3721     -- If allow_burden_flag is 'N' then the burden amounts
3722     -- are calculated by orcale projects(only for non BTC txns)
3723 
3724     -----------------------------------------------------------------------------
3725 
3726     IF ( nvl(G_gl_accted_flag,'N') = 'Y') THEN
3727        IF ( nvl(G_burdened_flag,'N') = 'N' ) THEN
3728 
3729           -- calculate the burden amount using the compiled multiplier derived
3730           -- from PA burden schedule for transactions accounted externally, txns
3731           -- that are not accounted externally, Oracle Projects costing programs
3732           -- will calculate the burdened costs.
3733           --
3734 
3735       /* --Commented code for Bug 2798971
3736           IF PG_DEBUG = 'Y' THEN
3737           pa_debug.G_err_stage := 'Calling pa_currency.round_trans_currency_amt';
3738              log_message('log_message: ' || pa_debug.G_err_Stage);
3739           END IF;
3740           X_denom_burdened_cost := pa_currency.round_trans_currency_amt1(
3741           X_denom_raw_cost * (1 + G_compiled_multiplier ),X_denom_currency_code);
3742 
3743           X_acct_burdened_cost := pa_currency.round_currency_amt1(
3744                               X_acct_raw_cost * (1 + G_compiled_multiplier));
3745 
3746           X_project_burdened_cost := pa_currency.round_trans_currency_amt1(
3747                               X_project_raw_cost * (1 + g_compiled_multiplier),
3748                               X_projfunc_currency_code);
3749 
3750           X_actual_project_burdened_cost := pa_currency.round_trans_currency_amt1(
3751                               X_actual_project_raw_cost * (1 + g_compiled_multiplier),
3752                               X_project_currency_code);
3753 	 --Commented code for Bug 2798971 ends*/
3754 
3755 /* Added code for 2798971 */
3756 	IF (nvl(G_proj_bcost_flag, 'N') = 'Y') THEN
3757           IF PG_DEBUG = 'Y' THEN
3758           pa_debug.G_err_stage := 'Calling pa_cost_plus1.get_indirect_cost_import';
3759              log_message('log_message: ' || pa_debug.G_err_Stage);
3760           END IF;
3761 	      pa_cost_plus1.get_indirect_cost_import (
3762 				task_id		           => G_task_id,
3763                                 p_txn_interface_id         => X_txn_interface_id, -- added for bug 3246794
3764 				effective_date	           => X_ei_date,
3765 				expenditure_type           => X_etype,
3766 				organization_id            => nvl(G_override_to_org_id, G_org_id),
3767 				schedule_type              => 'C',
3768 				direct_cost                => X_actual_project_raw_cost,
3769 				direct_cost_denom          => X_denom_raw_cost,
3770 				direct_cost_acct           => X_acct_raw_cost,
3771 				direct_cost_project        => X_project_raw_cost,
3772 				indirect_cost_sum          => X_actual_project_burdened_cost,
3773 				indirect_cost_denom_sum    => X_denom_burdened_cost,
3774 				indirect_cost_acct_sum	   => X_acct_burdened_cost,
3775 				indirect_cost_project_sum  => X_project_burdened_cost,
3776 				l_projfunc_currency_code   => X_projfunc_currency_code,
3777 				l_project_currency_code    => X_project_currency_code,
3778 				l_acct_currency_code       => null,
3779 				l_denom_currency_code      => X_denom_currency_code,
3780 				Compiled_set_id		   => G_burden_compile_set_id,
3781 				status                     => l_status_num,
3782 				stage                      => l_stage);
3783 --   Bug: 925488 and 956683: get_indirect_cost_import calls get_cost_base. Get_cost_base
3784 --       returns status = 100 when a expenditure type is not defined with a
3785 --       cost base.  We should not return error when this happens.  Instead,
3786 --       burdened_cost = raw_cost , burdened_cost will equal to raw_cost
3787 
3788 /* Error checking modified and added below. The checks are based on the
3789 different values of status and stage returned by get_indirect_cost_import
3790 		bug2837165 starts */
3791 
3792 		IF ( l_status_num <> 0 ) THEN
3793 		   IF (l_status_num = 100 AND l_stage = 300) THEN
3794 		      G_compiled_multiplier := 0;
3795 		      G_burden_compile_set_id := to_number(NULL);
3796 		   elsif (l_status_num = 100) then
3797 			if( l_stage = 100)then
3798 			  X_status := 'NO_IND_RATE_SCH_REVISION';
3799 		          pa_cc_utils.reset_curr_function;
3800 		          RETURN;
3801 			elsif (l_stage = 200) then
3802 			 X_status := 'NO_COST_PLUS_STRUCTURE';
3803 		         pa_cc_utils.reset_curr_function;
3804 		         RETURN;
3805 			elsif (l_stage = 400) then
3806 			 X_status := 'NO_ACTIVE_COMPILED_SET';
3807 		         pa_cc_utils.reset_curr_function;
3808 		         RETURN;
3809 			end if;
3810 		   ELSE
3811 		      X_status := 'PA_ERR_IN_COST_PLUS';
3812 		      pa_cc_utils.reset_curr_function;
3813 		      RETURN;
3814 		   END IF;
3815 		END IF;
3816 /* bug2837165 ends */
3817 	/* code added for the Bug #1428216 starts here*/
3818 	/* If project is burdened on a different EI we are stamping the compile_set_id
3819 	   but burden cost will be equal to raw costs. */
3820 		IF (NVL(G_burden_amt_display_method,'S') = 'D' ) THEN
3821 			G_compiled_multiplier := 0;
3822 			X_denom_burdened_cost := X_denom_raw_cost;
3823 			X_acct_burdened_cost := X_acct_raw_cost;
3824 			X_project_burdened_cost := X_project_raw_cost;
3825 			X_actual_project_burdened_cost := X_actual_project_raw_cost;
3826 		ELSE
3827 			/* code added for the Bug #1428216 ends here*/
3828 			/* Code modified for 2798971. Added an ELSE condition */
3829 			X_denom_burdened_cost := X_denom_burdened_cost + X_denom_raw_cost;
3830 			X_acct_burdened_cost := X_acct_burdened_cost + X_acct_raw_cost;
3831 			X_project_burdened_cost := X_project_burdened_cost + X_project_raw_cost;
3832 			X_actual_project_burdened_cost := X_actual_project_burdened_cost + X_actual_project_raw_cost;
3833 		END IF; -- IF (NVL(G_burden_amt_display_method,'S') = 'D' )
3834 	ELSE
3835 		G_compiled_multiplier := 0;
3836 		G_burden_compile_set_id := to_number(NULL);
3837 		X_denom_burdened_cost := X_denom_raw_cost;
3838 		X_acct_burdened_cost := X_acct_raw_cost;
3839 		X_project_burdened_cost := X_project_raw_cost;
3840 		X_actual_project_burdened_cost := X_actual_project_raw_cost;
3841 	END IF; -- IF (nvl(G_proj_bcost_flag, 'N') = 'Y') THEN
3842 /* Added code for 2798971 ends */
3843 
3844        ELSIF ( nvl(G_burdened_flag,'N') = 'Y' ) THEN
3845           IF ( nvl(X_denom_raw_cost,0) = 0) THEN
3846 
3847              -- if the transaction raw cost is zero then calculate the
3848              -- functional and project BCost by using the conversion attributes.
3849 
3850              IF ( nvl(X_acct_burdened_cost,0) = 0 ) THEN
3851 
3852                 -- initilize l_status
3853                 l_status := NULL;
3854 
3855                 IF PG_DEBUG = 'Y' THEN
3856                 pa_debug.G_err_stage := 'Calling pa_multi_currency.convert_amount for functional currency';
3857                    log_message('log_message: ' || pa_debug.G_err_Stage);
3858                 END IF;
3859 	        pa_multi_currency.convert_amount(
3860                                 P_from_currency         => X_denom_currency_code,
3861 				P_to_currency           => G_accounting_currency_code,
3862 				P_conversion_date       => X_acct_rate_date,
3863 				P_conversion_type       => G_acct_rate_type,
3864 				P_amount                => X_denom_burdened_cost,
3865 		                P_user_validate_flag    => l_validate_user_rate_type,
3866 	  			P_handle_exception_flag => l_handel_exception_flag,
3867 				P_converted_amount      => X_acct_burdened_cost,
3868 				P_denominator           => l_denominator,
3869 				P_numerator             => l_numerator,
3870 				P_rate                  => X_acct_exchange_rate,
3871 	      		        X_status                => l_status);
3872 
3873 	        IF ( l_status IS NOT NULL ) THEN
3874                      X_status := l_status;
3875                      pa_cc_utils.reset_curr_function;
3876                      RETURN;
3877 	        END IF; -- End l_status IS NOT NULL
3878 
3879              END IF;-- End X_acct_burdened_cost = 0
3880 
3881              -- calculate the project Bcost using the project conversion attributes
3882              -- before calling convert_amount initilize l_status
3883 
3884              l_status := NULL;
3885 
3886              IF PG_DEBUG = 'Y' THEN
3887              pa_debug.G_err_stage := 'Calling pa_multi_currency.convert_amount for project currency';
3888                 log_message('log_message: ' || pa_debug.G_err_Stage);
3889              END IF;
3890 	     pa_multi_currency.convert_amount(
3891                                 P_from_currency         => X_denom_currency_code,
3892 				P_to_currency           => X_project_currency_code,
3893 				P_conversion_date       => X_project_rate_date,
3894 				P_conversion_type       => G_project_rate_type,
3895 				P_amount                => X_denom_burdened_cost,
3896 		                P_user_validate_flag    => l_validate_user_rate_type,
3897 	  			P_handle_exception_flag => l_handel_exception_flag,
3898 			      --P_converted_amount      => X_project_burdened_cost,
3899                               --PA-I Changes : Project costs are calculated in x_actual_project.._cost
3900 				P_converted_amount      => X_actual_project_burdened_cost,
3901 				P_denominator           => l_denominator,
3902 				P_numerator             => l_numerator,
3903 				P_rate                  => X_project_exchange_rate,
3904 			        X_status                => l_status);
3905 
3906 	     IF ( l_status IS NOT NULL ) THEN
3907 			X_status := l_status;
3908                         pa_cc_utils.reset_curr_function;
3909 			RETURN;
3910 	     END IF; -- End l_status IS NOT NULL
3911 
3912              -- PA-I Changes : Added below for Project Functional Burdened cost calculation
3913              -- calculate the project functional Bcost using the project functional conversion attributes
3914              -- before calling convert_amount initilize l_status
3915 
3916              l_status := NULL;
3917 
3918              IF PG_DEBUG = 'Y' THEN
3919              pa_debug.G_err_stage := 'Calling pa_multi_currency.convert_amount for project functional currency';
3920                 log_message('log_message: ' || pa_debug.G_err_Stage);
3921              END IF;
3922              pa_multi_currency.convert_amount(
3923                                 P_from_currency         => X_denom_currency_code,
3924                                 P_to_currency           => X_projfunc_currency_code,
3925                                 P_conversion_date       => X_projfunc_cost_rate_date,
3926                                 P_conversion_type       => G_projfunc_cost_rate_type,
3927                                 P_amount                => X_denom_burdened_cost,
3928                                 P_user_validate_flag    => l_validate_user_rate_type,
3929                                 P_handle_exception_flag => l_handel_exception_flag,
3930                                 P_converted_amount      => X_project_burdened_cost,
3931                                 P_denominator           => l_denominator,
3932                                 P_numerator             => l_numerator,
3933                                 P_rate                  => X_projfunc_cost_exchange_rate,
3934                                 X_status                => l_status);
3935 
3936              IF ( l_status IS NOT NULL ) THEN
3937                    X_status := l_status;
3938                    pa_cc_utils.reset_curr_function;
3939                    RETURN;
3940              END IF; -- End l_status IS NOT NULL
3941 
3942           ELSE -- i.e denom_raw_cost <> 0
3943 
3944              -- Multi-Currency changes.
3945              -- If the Trx Source indicates that the transaction is burdened then
3946              -- for accounted transactions, if the acct_burdened_cost is null or 0 then
3947              -- acct_burdened_cost is derived by multiplying the acct_raw_cost with
3948              -- denom_cost_ratio.
3949 
3950              IF PG_DEBUG = 'Y' THEN
3951              pa_debug.G_err_stage := 'Calling pa_currency.round_trans_currency_amt';
3952                 log_message('log_message: ' || pa_debug.G_err_Stage);
3953              END IF;
3954 
3955              X_project_burdened_cost := pa_currency.round_trans_currency_amt1(
3956                               X_project_raw_cost *
3957                               (X_denom_burdened_cost/X_denom_raw_cost),
3958                               X_projfunc_currency_code);
3959 
3960              X_actual_project_burdened_cost := pa_currency.round_trans_currency_amt1(
3961                               X_actual_project_raw_cost *
3962                               (X_denom_burdened_cost/X_denom_raw_cost),
3963                               X_project_currency_code);
3964 
3965              IF ( nvl( X_acct_burdened_cost ,0 ) = 0  ) THEN
3966                  X_acct_burdened_cost := pa_currency.round_currency_amt1(
3967                                          X_acct_raw_cost * (X_denom_burdened_cost/X_denom_raw_cost));
3968              END IF; -- end acct_burdened_cost = 0
3969           END IF; -- End denom_raw_cost = 0
3970        END IF; -- end G_burdened_flag = N
3971     END IF; -- end G_gl_accted_flag = Y
3972 
3973     -- ===========================================================================
3974     --  If gl_accounted_flag = 'Y' then for each transaction we should have
3975     --   Valid dr_ccid, cr_ccid , gl_date.
3976     IF G_gl_accted_flag = 'Y' THEN
3977 
3978        IF (nvl(X_module, 'EXTERNAL') <> 'PAAPIMP') THEN
3979             --PA-J Receipt Accrual Changes : Added AP VARIANCE and PO RECEIPT transaction sources
3980             --PA-I Changes : Added Discounts for AP Discounts project
3981 
3982            IF Checkccid(X_drccid) IS NULL THEN
3983            pa_debug.G_err_stage := 'Calling Checkccid'; log_message(pa_debug.G_err_Stage);
3984               X_status := 'INVALID_DR_CCID' ;
3985               pa_cc_utils.reset_curr_function;
3986               RETURN ;
3987            END IF ;
3988            IF Checkccid(X_crccid) IS NULL THEN
3989               X_status := 'INVALID_CR_CCID' ;
3990               pa_cc_utils.reset_curr_function;
3991               RETURN ;
3992            END IF ;
3993 
3994            --
3995            --BUG : 4696351 PJ.R12:DI4:APLINES: VENDOR INFORMATION NOT IMPORTED DURING TRANSACTION IMPORT
3996            --
3997 	       IF x_system_linkage = 'ER' and
3998 		      g_vendor_id is NULL THEN
3999 
4000 			  getvendorId(g_person_id) ;
4001 
4002 			  IF g_vendor_id is NULL then
4003 			     x_status := 'PA_INVALID_SUPPLIER_INFO';
4004                  pa_cc_utils.reset_curr_function;
4005 		         RETURN;
4006 			  END IF ;
4007 
4008 		   END IF ;
4009        END IF;                                            -- Added for bug 1428539.
4010 
4011 /* -- Commented for bug 3357936
4012           IF  G_gl_posted_flag = 'N' AND X_gl_date IS NULL THEN
4013 
4014                 select set_of_books_id
4015                 into l_SobId
4016                 from pa_implementations_all
4017                 where nvl(org_id,-99) = nvl(X_org_id,-99);
4018 
4019 		X_gl_date := pa_utils2.get_prvdr_gl_date( X_ei_date, l_appl_id , l_SobId) ;
4020 
4021          END IF;
4022 */
4023 
4024        IF X_gl_date IS NULL THEN
4025          X_status := 'INVALID_GL_DATE' ;
4026            pa_cc_utils.reset_curr_function;
4027          RETURN ;
4028        END IF ;
4029 
4030        /*********EPP Changes. This code is commented, is moved after IC check*********
4031        --   Added the pa_date validation  fix  bug 572196. The for accounted items
4032        --   there should be a valid open/future pa_period
4033        --   Added the following function call calculate pa_date to Resolve Bug 1103257 base bug 967390
4034        *******************************************************************************/
4035 
4036 
4037 
4038     END IF ;
4039 
4040     -- ===========================================================================
4041     --   Verify that the transaction has not already been loaded into PA (no
4042     --   other expenditure items exist having the same TRANSACTION SOURCE and
4043     --   ORIG TRANSACTION REFERENCE)
4044     --
4045 
4046     IF G_allow_dup_flag <>  'Y' THEN
4047 
4048        IF PG_DEBUG = 'Y' THEN
4049        pa_debug.G_err_stage := 'Calling CheckDupItem';
4050           log_message('log_message: ' || pa_debug.G_err_Stage);
4051        END IF;
4052        CheckDupItem ( X_trx_src, X_trx_ref, temp_status );
4053        IF ( temp_status IS NOT NULL ) THEN
4054          X_status := temp_status;
4055          pa_cc_utils.reset_curr_function;
4056          RETURN;
4057        END IF;
4058 
4059     END IF ;
4060 
4061     -- ===========================================================================
4062     --   If the transaction is an adjustment (negative quantity) of a specific
4063     --   expenditure item (UNMATCHED_NEGATIVE_TXN_FLAG is 'N'), then verify that
4064     --   a matching expenditure item exists
4065     --
4066     --  Oracle Time and Labor(OTL)requires the ability to reverse an ei with a quantity = 0
4067     --  so only for OTL that option has been added.
4068     --  (X_match_flag = 'N' and nvl(X_module, 'EXTERNAL') =  'PAAPIMP' )
4069     --
4070     IF  ( ( X_qty < 0           and X_match_flag = 'N' ) OR
4071           ( X_match_flag = 'N'  and X_trx_src = 'ORACLE TIME AND LABOR' ) OR
4072           ( nvl(p_adj_exp_item_id,0) > 0  and nvl(X_module, 'EXTERNAL') =  'PAAPIMP') ) THEN
4073 
4074       IF PG_DEBUG = 'Y' THEN
4075       pa_debug.G_err_stage := 'Calling pa_adjustments.VerifyOrigItem';
4076          log_message('log_message: ' || pa_debug.G_err_Stage);
4077       END IF;
4078 
4079 	  /* REL12 AP Lines Uptake */
4080 	  IF ( p_adj_exp_item_id is not NULL and
4081 	       nvl(X_module, 'EXTERNAL') =  'PAAPIMP' ) THEN
4082 	       G_adj_item_id := p_adj_exp_item_id ;
4083 	  ELSE
4084 
4085       G_adj_item_id := pa_adjustments.VerifyOrigItem(
4086                 X_person_id => G_person_id
4087               , X_org_id => G_org_id
4088               , X_item_date => X_ei_date
4089               , X_task_id => G_task_id
4090               , X_exp_type => X_etype
4091               , X_system_linkage_function => X_system_linkage
4092               , X_nl_org_id => G_nlro_id
4093               , X_nl_resource => X_nlr
4094               , X_quantity => X_qty
4095               , X_denom_raw_cost => X_denom_raw_cost
4096               , X_trx_source => X_trx_src
4097               , X_denom_currency_code => X_denom_currency_code
4098               , X_acct_raw_cost => X_acct_raw_cost
4099               , X_reversed_orig_txn_reference => G_reversed_orig_txn_reference);
4100 	  END IF ;
4101 
4102       IF ( G_adj_item_id IS NULL ) THEN
4103 
4104         X_status := 'NO_MATCHING_ITEM';
4105         pa_cc_utils.reset_curr_function;
4106         RETURN;
4107 
4108       ELSIF ( G_adj_item_id IS NOT NULL)  AND
4109 	    ( X_module = 'PAXTRTRX'  OR X_module =  'PAAPIMP') THEN
4110 
4111 
4112         IF PG_DEBUG = 'Y' THEN
4113         pa_debug.G_err_stage := 'Calling pa_adjustments.ExpAdjItemTab';
4114            log_message('log_message: ' || pa_debug.G_err_Stage);
4115         END IF;
4116         pa_adjustments.ExpAdjItemTab(G_adj_item_id) := G_adj_item_id;
4117 
4118         -- Commenting out the following procedure because verifyorigitem
4119         -- already checks for adjusted ei's that are not yet inserted into
4120         -- database table( EI Table). Bug 752915
4121 
4122         -- CheckDupAdjItem( G_adj_item_id, temp_status );
4123 
4124         IF PG_DEBUG = 'Y' THEN
4125         pa_debug.G_err_stage := 'Locking adjusting item:'||to_char(G_adj_item_id);
4126            log_message('log_message: ' || pa_debug.G_err_Stage);
4127         END IF;
4128 
4129         BEGIN
4130 
4131           SELECT
4132                   expenditure_item_id
4133             INTO
4134                   dummy
4135             FROM
4136                   pa_expenditure_items
4137            WHERE
4138                   expenditure_item_id = G_adj_item_id
4139           FOR UPDATE NOWAIT;
4140 
4141         EXCEPTION
4142           WHEN  RESOURCE_BUSY  THEN
4143             temp_status := 'CANNOT_LOCK_ORIG_ITEM';
4144         END;
4145       END IF;
4146 
4147       IF ( temp_status IS NOT NULL ) THEN
4148         X_status := temp_status;
4149         pa_cc_utils.reset_curr_function;
4150         RETURN;
4151       END IF;
4152 
4153     END IF;
4154 
4155     -- ===========================================================================
4156     -- IC Changes
4157     --
4158     -- CAll Cross Charge identification API.  This API determines
4159     -- Provider Organization
4160     -- Receiver Organization,
4161     -- Cross charge code: Valid values are (B, I, N, X )
4162     --      B = Borrow and Lent , I = Inter Company, N= Not Cross charged
4163     --      X = Never Cross Charged
4164     --      The value of Cross charge code will be used to derive
4165     --      CC_BL_DISTRIBUTED_CODE and CC_IC_PROCESSED_CODE.
4166     --
4167     -- Receiver operating uint,
4168     -- Cross charge type: VAlid values are ( IU, IC, IO )
4169     --      IU = Cross Charge across operating units within one legal entity
4170     --      IC = Cross Charge across operating units across legal entities
4171     --      IO = Cross charge with in one Operating unit( Borrow and lent )
4172     --
4173     -- The cross charge identification is usually done by costing progarm
4174     -- However for accounted txns are not picked up by costing programs, hence
4175     -- transaction import should identify cross charge txns.
4176 
4177     --       Initilize the package body global variables before calling the
4178     --       CC Identification API.
4179 
4180     G_CrossChargeType := 'NO';
4181     G_CrossChargeCode := 'P';
4182     G_PrvdrOrganizationId := NULL;
4183     G_RecvrOrganizationId := NULL;
4184     G_RecvrOrgId := NULL;
4185     G_BrowLentDistCode := 'X';
4186     G_IcProcessed_Code := 'X';
4187 
4188     /* Added g_trx_costed in if condition for bug 1897348*/
4189     IF (( nvl(G_gl_accted_flag,'N') = 'Y') OR
4190 	      (nvl(G_trx_costed,'N') = 'Y')) THEN
4191 
4192           <<Cc_Identification_Api>>
4193           BEGIN
4194 
4195           IF PG_DEBUG = 'Y' THEN
4196           pa_debug.G_err_stage := 'Calling PA_CC_IDENT.PA_CC_IDENTIFY_TXN_ADJ';
4197              log_message('log_message: ' || pa_debug.G_err_Stage);
4198           END IF;
4199 
4200           PA_CC_IDENT.PA_CC_IDENTIFY_TXN_ADJ(
4201           -- Changed the follwing NVL as the G_override_to_org_id should always
4202 		  -- take precedence over G_org_id bug #4689402 (forward port for 4614046)
4203           P_ExpOrganizationId     => NVL(G_override_to_org_id, G_org_id), --For 1612483
4204           P_ExpOrgid              => X_org_id , -- bug 1612483
4205           P_ProjectId             => G_project_id,
4206           P_TaskId                => G_task_id,
4207           P_ExpItemDate           => X_ei_date,
4208 	  /* Passing TXN_INTERFACE_ID instead of null for 3234973
4209 	  and calling_module as 'TRANSACTION_IMPORT' */
4210           /* P_ExpItemId             => NULL, */
4211 	  P_ExpItemId             => X_txn_interface_id,
4212           P_ExpType               => X_etype,
4213           P_PersonId              => G_person_id ,
4214           P_SysLink               => X_system_linkage,
4215           P_PrjOrganizationId     => NULL,
4216           P_PrjOrgId              => NULL,
4217           P_TransSource           => X_trx_src,
4218           P_NLROrganizationId     => G_nlro_id,
4219           P_PrvdrLEId             => NULL,
4220           P_RecvrLEId             => NULL,
4221           X_Status                => X_Status,
4222           X_CrossChargeType       => G_CrossChargeType,
4223           X_CrossChargeCode       => G_CrossChargeCode,
4224           X_PrvdrOrganizationId   => G_PrvdrOrganizationId,
4225           X_RecvrOrganizationId   => G_RecvrOrganizationId,
4226           X_RecvrOrgId            => G_RecvrOrgId,
4227           X_Error_Stage           => temp_dff_msg,
4228           X_Error_Code            => temp_status,
4229  	  /* Added calling module for 3234973, so that client extention can function correctly. */
4230 	  X_Calling_Module        => 'TRANSACTION_IMPORT');
4231 
4232           EXCEPTION WHEN others THEN
4233              IF PG_DEBUG = 'Y' THEN
4234                 log_message('log_message: ' || pa_debug.G_err_stack,1);
4235                 log_message('log_message: ' || SQLERRM,1);
4236                 log_message('log_message: ' || pa_debug.G_err_stage,1);
4237              END IF;
4238              X_Status := 'PA_ERR_IN_CC_IDENT_API';
4239              pa_cc_utils.reset_curr_function;
4240              RETURN;
4241           END Cc_Identification_Api;
4242 
4243           --   if an error has occured in CC identification API, the
4244           --   X_Status variable will be populated with the error code.
4245          IF ( X_Status IS NOT NULL ) THEN
4246             pa_cc_utils.reset_curr_function;
4247             RETURN;
4248          ELSE -- i.e. successful completion
4249 
4250             IF ( G_CrossChargeCode = 'B' ) THEN
4251 
4252                -- Brrowed and lent processing
4253                -- The EI will be marked for
4254                -- borrow and lent processing
4255 
4256                G_BrowLentDistCode     := 'N';
4257                G_IcProcessed_Code     := 'X';
4258 
4259             ELSIF ( G_CrossChargeCode = 'I' ) THEN
4260 
4261                -- Inter company processing
4262                -- the Ei will be marked for IC processing
4263 
4264                G_BrowLentDistCode     := 'X';
4265                G_IcProcessed_Code     := 'N';
4266 
4267             ELSE
4268                -- No Cross Charge processing
4269 
4270                G_BrowLentDistCode     := 'X';
4271                G_IcProcessed_Code     := 'X';
4272 
4273             END IF; -- end G_CrossChargeCode = 'B'
4274          END IF; -- End X_status is not null
4275 
4276          --   Added the following function call calculate recvr_pa_date to Resolve Bug # 1653831
4277 
4278          /*  EPP Changes: Moved recvr_pa_date derivation after this section */
4279 
4280          --   End of code for Bug # 1653831
4281          /* IC Change: we need to get receiver org ID regarldess
4282             of accounted or unaccounted transaction.  For accounted
4283             case, we call PA_CC_IDENT.pa_cc_identify_txn_adj to
4284             get receiver org ID along with other info.  For
4285             unaccounted case, we call PA_UTILS2.GetPrjOrgId to get
4286             only the reciever org id info.  */
4287     ELSE
4288   	/* IC Changes: Get receiver organization ID */
4289         IF PG_DEBUG = 'Y' THEN
4290         pa_debug.G_err_stage := 'Calling PA_UTILS2.GetPrjOrgId';
4291            log_message('log_message: ' || pa_debug.G_err_Stage);
4292         END IF;
4293         G_RecvrOrgId := PA_UTILS2.GetPrjOrgId(p_project_id => G_project_id,
4294                                               p_task_id    => NULL);
4295     END IF; -- End gl_accted_flag = Y
4296     -- ==========================================================================
4297 
4298     /***********Start EPP CHANGES. Added call to get period information*************/
4299 
4300     G_PaDate            := NULL;
4301     G_PaPeriodName      := NULL;
4302     G_RecvrPaDate       := NULL;
4303     G_RecvrPaPeriodName := NULL;
4304     G_GlPeriodName      := NULL;
4305     G_RecvrGlDate       := NULL;
4306     G_RecvrGlPeriodName := NULL;
4307     G_SobId             := NULL;
4308     G_RecvrSobId        := NULL;
4309 
4310     /*  PA-J Period-End Accrual Changes :
4311                    Need to retrieve set_of_books information for provider
4312                    and receiver orgs when gl_accted_flag is Y and also
4313                    for unaccounted period-end accrual transactions.
4314                    A miscellaneous transaction (system_linkage = PJ) with the
4315                    accrual_flag set to Y is a period-end accrual transaction.
4316                    For period-end accrual transaction the period information
4317                    is retrieved from a new API get_accrual_period_information.
4318     */
4319 
4320     G_GlDate          := NULL;
4321     G_AccDate         := NULL;
4322     G_RecvrAccDate    := NULL;
4323     G_RevAccDate      := NULL;
4324     G_RevRecvrAccDate := NULL;
4325     G_RevPaDate       := NULL;
4326     G_RevPaPeriodName := NULL;
4327     G_RevGlDate       := NULL;
4328     G_RevGlPeriodName := NULL;
4329     G_RevRecvrPaDate  := NULL;
4330     G_RevRecvrPaPdName:= NULL;
4331     G_RevRecvrGlDate  := NULL;
4332     G_RevRecvrGlPdName:= NULL;
4333     l_period_end_txn  := 'N';
4334 
4335     --Identify if the transaction is a period-end accrual transaction
4336     IF (x_system_linkage = 'PJ' and x_accrual_flag = 'Y') THEN
4337 
4338        IF PG_DEBUG = 'Y' THEN
4339        pa_debug.G_err_stage := 'This is a Period-End Accrual transaction';
4340           log_message('log_message: ' || pa_debug.G_err_Stage);
4341        END IF;
4342 
4343        l_period_end_txn := 'Y';
4344 
4345     END IF;
4346 
4347     --If transaction is accounted then derive the provider and receiver set of books information.
4348     IF ( nvl(G_gl_accted_flag,'N') = 'Y' or l_period_end_txn = 'Y') THEN
4349 
4350        IF PG_DEBUG = 'Y' THEN
4351        pa_debug.G_err_stage := 'Retrieve provider and receiver set_of_books';
4352           log_message('log_message: ' || pa_debug.G_err_Stage);
4353        END IF;
4354 
4355        log_message('log_message: x_org_id= ' || X_org_id || ' Prev = '|| Prev_OrgId); -- For bug 3590027
4356        if (nvl(X_org_id,-101) <> nvl(Prev_OrgId,-99)) then   -- Added nvl to X_org_id for bug 3590027
4357 
4358           IF PG_DEBUG = 'Y' THEN
4359           pa_debug.G_err_stage := 'Diff org_id';
4360              log_message('log_message: ' || pa_debug.G_err_Stage);
4361           END IF;
4362 
4363           select set_of_books_id
4364           into G_SobId
4365           from pa_implementations_all
4366           where org_id = X_org_id;
4367 
4368           Prev_SobId := G_SobId;
4369           Prev_OrgID := X_Org_Id;
4370        else
4371 
4372           IF PG_DEBUG = 'Y' THEN
4373           pa_debug.G_err_stage := 'Same org_id';
4374              log_message('log_message: ' || pa_debug.G_err_Stage);
4375           END IF;
4376 
4377           G_SobId := Prev_SobId;
4378        end if;
4379 
4380        --IF (nvl(Prev_SobId,-101) <> nvl(G_SobId,-99)) THEN
4381        --  SELECT NVL(sla_ledger_cash_basis_flag,'N')
4382        --    INTO G_cash_based_accounting
4383        --    FROM gl_sets_of_books
4384        --   WHERE set_of_books_id = G_SobId;
4385        --
4386        --END IF;
4387 
4388    log_message('log_message: G_RecvrOrgId= ' || G_RecvrOrgId || ' Prev = '|| Prev_RecvrOrgId);--Bug 3590027
4389        if (nvl(G_RecvrOrgId,-101) <> nvl(Prev_RecvrOrgId,-99)) then -- Added nvl to G_RecvrOrgId for 3590027
4390 
4391           IF PG_DEBUG = 'Y' THEN
4392           pa_debug.G_err_stage := 'Diff recvr_org_id';
4393              log_message('log_message: ' || pa_debug.G_err_Stage);
4394           END IF;
4395 
4396           select set_of_books_id
4397           into G_RecvrSobId
4398           from pa_implementations_all
4399           where org_id = nvl(G_RecvrOrgId,-99);
4400 
4401           Prev_RecvrSobId := G_RecvrSobId;
4402           Prev_RecvrOrgID := G_RecvrOrgId;
4403        else
4404 
4405           IF PG_DEBUG = 'Y' THEN
4406           pa_debug.G_err_stage := 'Same recvr_org_id';
4407              log_message('log_message: ' || pa_debug.G_err_Stage);
4408           END IF;
4409 
4410           G_RecvrSobId := Prev_RecvrSobId;
4411        end if;
4412 
4413        IF PG_DEBUG = 'Y' THEN
4414           log_message('log_message: ' || 'Prvdr Sob ID = ' || G_SobId);
4415           log_message('log_message: ' || 'Recvr Sob ID = ' || G_RecvrSobId);
4416        END IF;
4417     END IF;
4418 
4419     --If transaction is an accounted, non-period end accrual transaction then call
4420     --pa_integration.get_period_information to derive the period information.
4421     IF ( (nvl(G_gl_accted_flag,'N') = 'Y') and (l_period_end_txn = 'N')) THEN
4422 
4423        IF PG_DEBUG = 'Y' THEN
4424        pa_debug.G_err_stage := 'Start PA_Date and Recvr_PA_Date Checks for non-period-end accrual txns';
4425           log_message('log_message: ' || pa_debug.G_err_Stage);
4426        END IF;
4427 
4428        IF PG_DEBUG = 'Y' THEN
4429        pa_debug.G_err_stage := 'Calling pa_integration.get_period_information';
4430           log_message('log_message: ' || pa_debug.G_err_Stage);
4431        END IF;
4432 
4433        pa_integration.get_period_information(
4434                  p_expenditure_item_date  => X_ei_date
4435                 ,p_prvdr_gl_date          => X_gl_date
4436                 ,x_recvr_gl_date          => G_RecvrGlDate
4437                 ,p_line_type              => 'R'
4438                 ,p_prvdr_org_id           => X_org_id
4439                 ,p_recvr_org_id           => G_RecvrOrgId
4440                 ,p_prvdr_sob_id           => G_SobId
4441                 ,p_recvr_sob_id           => G_RecvrSobId
4442                 ,x_prvdr_pa_date          => G_PaDate
4443                 ,x_prvdr_pa_period_name   => G_PaPeriodName
4444                 ,x_prvdr_gl_period_name   => G_GlPeriodName
4445                 ,x_recvr_pa_date          => G_RecvrPaDate
4446                 ,x_recvr_pa_period_name   => G_RecvrPaPeriodName
4447                 ,x_recvr_gl_period_name   => G_RecvrGlPeriodName
4448                 ,x_return_status          => x_return_status
4449                 ,x_error_code             => x_error_code
4450                 ,x_error_stage            => x_error_stage);
4451 
4452       IF PG_DEBUG = 'Y' THEN
4453          log_message('log_message: ' || 'G_OrgID = '|| X_org_id || ' G_RecvrOrgId = '|| G_RecvrOrgId || ' X_ei_date = '|| X_ei_date);
4454          log_message('log_message: ' || 'G_PaDate = '|| G_PaDate || ' G_PaPeriodName = '|| G_PaPeriodName);
4455          log_message('log_message: ' || 'G_GlDate = '|| X_gl_date || ' G_GlPeriodName = '|| G_GlPeriodName);
4456          log_message('log_message: ' || 'G_RecvrPaDate = '|| G_RecvrPaDate || ' G_RecvrPeriodName = '|| G_RecvrPaPeriodName);
4457          log_message('log_message: ' || 'G_RecvrGlDate = '|| G_RecvrGlDate || ' G_RecvrGlPeriodName = '|| G_RecvrGlPeriodName);
4458       END IF;
4459 
4460       IF PG_DEBUG = 'Y' THEN
4461       pa_debug.G_err_stage := 'After pa_integration.get_period_information';
4462          log_message('log_message: ' || pa_debug.G_err_Stage);
4463       END IF;
4464 
4465       --Pa_date check being done here
4466       IF G_PaDate is NULL then
4467 
4468          IF PG_DEBUG = 'Y' THEN
4469          pa_debug.G_err_stage := 'PA_Date is null';
4470             log_message('log_message: ' || pa_debug.G_err_Stage);
4471          END IF;
4472 
4473          X_status := 'INVALID_PA_DATE' ;
4474          pa_cc_utils.reset_curr_function;
4475          RETURN ;
4476 
4477       END IF ;
4478 
4479       IF   nvl(X_org_id,-999) <> nvl(G_RecvrOrgId,-999) THEN ----------------------{
4480 
4481        IF PG_DEBUG = 'Y' THEN
4482        pa_debug.G_err_stage := 'Before Check for the Receiver Date';
4483           log_message('log_message: ' || pa_debug.G_err_Stage);
4484        END IF;
4485 
4486        IF G_RecvrPaDate is NULL then
4487 
4488           IF PG_DEBUG = 'Y' THEN
4489           pa_debug.G_err_stage := 'Recvr_PA_Date is null';
4490              log_message('log_message: ' || pa_debug.G_err_Stage);
4491           END IF;
4492 
4493           X_status := 'INVALID_RECVR_PA_DATE' ;
4494           pa_cc_utils.reset_curr_function;
4495           RETURN ;
4496 
4497        END IF ;
4498 
4499        IF G_RecvrGlDate is NULL then
4500 
4501           IF PG_DEBUG = 'Y' THEN
4502           pa_debug.G_err_stage := 'Recvr_GL_Date is null';
4503              log_message('log_message: ' || pa_debug.G_err_Stage);
4504           END IF;
4505 
4506           X_status := 'INVALID_RECVR_GL_DATE' ;
4507           pa_cc_utils.reset_curr_function;
4508           RETURN ;
4509 
4510        END IF ;
4511 
4512        IF PG_DEBUG = 'Y' THEN
4513        pa_debug.G_err_stage := 'After Check for the Receiver Date';
4514           log_message('log_message: ' || pa_debug.G_err_Stage);
4515        END IF;
4516 
4517       END IF ;                                  ----------------------------------}
4518 
4519     END IF;  --Accted = Y and Period_End = N
4520 
4521     --If transaction is accounted or unaccounted, period end accrual transaction then call
4522     --pa_utils2.get_period_information to derive the accrual period information for the original
4523     --and reversing line.
4524     IF ( l_period_end_txn = 'Y' ) THEN
4525 
4526        IF PG_DEBUG = 'Y' THEN
4527        pa_debug.G_err_stage := 'Start PA_Date and Recvr_PA_Date Checks for period-end accrual txns';
4528           log_message('log_message: ' || pa_debug.G_err_Stage);
4529        END IF;
4530 
4531        IF PG_DEBUG = 'Y' THEN
4532        pa_debug.G_err_stage := 'Calling pa_utils2.get_accrual_period_information';
4533           log_message('log_message: ' || pa_debug.G_err_Stage);
4534        END IF;
4535 
4536        G_GlDate  := X_gl_date;
4537 
4538        --Call get_accrual_period_information API for the original line
4539        pa_utils2.get_accrual_period_information (
4540                 p_expenditure_item_date   => X_ei_date                 --in
4541                ,x_prvdr_accrual_date      => G_AccDate                 --in/out. For original item this is passed OUT
4542                ,x_recvr_accrual_date      => G_RecvrAccDate            --in/out. For original item this is passed OUT
4543                ,p_prvdr_org_id            => X_org_id                  --in
4544                ,p_recvr_org_id            => G_RecvrOrgId              --in
4545                ,p_prvdr_sob_id            => G_SobId                   --in
4546                ,p_recvr_sob_id            => G_RecvrSobId              --in
4547                ,p_calling_module          => 'TRXIMPORT'               --in
4548                ,x_prvdr_pa_date           => G_PaDate                  --out
4549                ,x_prvdr_pa_period_name    => G_PaPeriodName            --out
4550                ,x_prvdr_gl_date           => G_GlDate                  --in/out. This is passed IN if accounted. Unaccounted, it will be null
4551                ,x_prvdr_gl_period_name    => G_GlPeriodName            --out
4552                ,x_recvr_pa_date           => G_RecvrPaDate             --out
4553                ,x_recvr_pa_period_name    => G_RecvrPaPeriodName       --out
4554                ,x_recvr_gl_date           => G_RecvrGlDate             --out
4555                ,x_recvr_gl_period_name    => G_RecvrGlPeriodName       --out
4556                ,p_adj_ei_id               => null                      --in
4557                ,p_acct_flag               => nvl(G_gl_accted_flag,'N') --in
4558                ,x_return_status           => l_ret_sts                 --out
4559                ,x_error_code              => x_error_code              --out
4560                ,x_error_stage             => x_err_stage               --out
4561                );
4562 
4563        IF PG_DEBUG = 'Y' THEN
4564           log_message('log_message: ' || 'Prvdr Acc Date = ' || G_AccDate || ' Recvr Acc Date = ' || G_RecvrAccDate);
4565        END IF;
4566 
4567        if (nvl(G_gl_accted_flag,'N') = 'Y') then
4568           IF PG_DEBUG = 'Y' THEN
4569              log_message('log_message: ' || 'Prvdr Gl Date = ' || G_GlDate || ' Prvdr Gl Pd Name = ' || G_GlPeriodName);
4570              log_message('log_message: ' || 'Recvr Gl Date = ' || G_RecvrGlDate || ' Recvr Gl Pd Name = ' || G_RecvrGlPeriodName);
4571              log_message('log_message: ' || 'Prvdr PA Date = ' || G_PaDate || ' Prvdr PA Pd Name = ' || G_PaPeriodName);
4572              log_message('log_message: ' || 'Recvr PA Date = ' || G_RecvrPaDate || ' Recvr PA Pd Name = ' || G_RecvrPaPeriodName);
4573           END IF;
4574        end if;
4575 
4576        if (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) then
4577           x_status := x_error_code;
4578           IF PG_DEBUG = 'Y' THEN
4579           pa_debug.G_err_stage := 'Error returned for accrual date derivation = '||x_status;
4580              log_message('log_message: ' || pa_debug.G_err_Stage);
4581           END IF;
4582           pa_cc_utils.reset_curr_function;
4583           RETURN;
4584        end if;
4585 
4586        --We copy the original item's accrual date for provider and
4587        --receiver to the reversing item variables.
4588        --For reversing item the original item's accrual date is passed
4589        --in and the reversing item's accrual date is passed out.
4590        G_RevAccDate      := G_AccDate;
4591        G_RevRecvrAccDate := G_RecvrAccDate;
4592 
4593        IF PG_DEBUG = 'Y' THEN
4594        pa_debug.G_err_stage := 'Calling pa_utils2.get_accrual_period_information for the reversing line';
4595           log_message('log_message: ' || pa_debug.G_err_Stage);
4596        END IF;
4597 
4598 
4599        --Call get_accrual_period_information API for the reversing line
4600        pa_utils2.get_accrual_period_information (
4601                 p_expenditure_item_date   => X_ei_date
4602                ,x_prvdr_accrual_date      => G_RevAccDate              --in/out (both)
4603                ,x_recvr_accrual_date      => G_RevRecvrAccDate         --in/out (both)
4604                ,p_prvdr_org_id            => X_org_id
4605                ,p_recvr_org_id            => G_RecvrOrgId
4606                ,p_prvdr_sob_id            => G_SobId
4607                ,p_recvr_sob_id            => G_RecvrSobId
4608                ,p_calling_module          => 'TRXIMPORT'
4609                ,x_prvdr_pa_date           => G_RevPaDate
4610                ,x_prvdr_pa_period_name    => G_RevPaPeriodName
4611                ,x_prvdr_gl_date           => G_RevGlDate               --in/out (passed OUT for reversing item)
4612                ,x_prvdr_gl_period_name    => G_RevGlPeriodName
4613                ,x_recvr_pa_date           => G_RevRecvrPaDate
4614                ,x_recvr_pa_period_name    => G_RevRecvrPaPdName
4615                ,x_recvr_gl_date           => G_RevRecvrGlDate
4616                ,x_recvr_gl_period_name    => G_RevRecvrGLPdName
4617                ,p_adj_ei_id               => 99                        --dummy value to be passed
4618                ,p_acct_flag               => nvl(G_gl_accted_flag,'N')
4619                ,x_return_status           => l_ret_sts
4620                ,x_error_code              => x_error_code
4621                ,x_error_stage             => x_err_stage
4622                );
4623 
4624        IF PG_DEBUG = 'Y' THEN
4625           log_message('log_message: ' || 'Prvdr Acc Date = ' || G_RevAccDate || ' Recvr Acc Date = ' || G_RevRecvrAccDate);
4626        END IF;
4627 
4628        if (nvl(G_gl_accted_flag,'N') = 'Y') then
4629           IF PG_DEBUG = 'Y' THEN
4630              log_message('log_message: ' || 'Prvdr Gl Date = ' || G_RevGlDate || ' Prvdr Gl Pd Name = ' || G_RevGlPeriodName);
4631              log_message('log_message: ' || 'Recvr Gl Date = ' || G_RevRecvrGlDate || ' Recvr Gl Pd Name = ' || G_RevRecvrGlPdName);
4632              log_message('log_message: ' || 'Prvdr PA Date = ' || G_RevPaDate || ' Prvdr PA Pd Name = ' || G_RevPaPeriodName);
4633              log_message('log_message: ' || 'Recvr PA Date = ' || G_RevRecvrPaDate || ' Recvr PA Pd Name = ' || G_RevRecvrPaPdName);
4634           END IF;
4635        end if;
4636 
4637        IF (l_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
4638           x_status := x_error_code;
4639           IF PG_DEBUG = 'Y' THEN
4640           pa_debug.G_err_stage := 'Error returned for reversal line accrual date derivation = '||x_status;
4641              log_message('log_message: ' || pa_debug.G_err_Stage);
4642           END IF;
4643           pa_cc_utils.reset_curr_function;
4644           RETURN;
4645        END IF;
4646 
4647     END IF;      -- Period_End = Y accrual txn period derivation
4648 
4649     /***********End EPP CHANGES. Added call to get period information*************/
4650 
4651     /*=========Start Assignment_Id, Work_Type_Id Checks========================*/
4652 
4653     -- PA-I Changes
4654     -- Assignment_Id and Work_Type_Id derivation/validation checks
4655 
4656     -- PA-J Txn Ctrl Changes
4657     -- Assignment Id will now be derived for Expense Reports (system_linkage of ER)
4658     -- The derivation of assignment id for ER is done in pa_utils4.
4659 
4660     -- PA-K Changes: For pre-defined sources the values in assignment_name and
4661     -- work_type_name will be ignored. If ids are given they will used else it will be derived.
4662     -- For user defined sources ids will be ignored and the names if given will be validated
4663 
4664     If (G_trx_predef_flag = 'Y') Then
4665 
4666        IF PG_DEBUG = 'Y' THEN
4667        pa_debug.G_err_stage := 'Get Assignment and work type for predefined';
4668           log_message('log_message: ' || pa_debug.G_err_Stage);
4669        END IF;
4670 
4671        If (x_system_linkage in ('ST', 'OT', 'ER')) Then
4672 
4673           IF PG_DEBUG = 'Y' THEN
4674           pa_debug.G_err_stage := 'Sys Link is ST OT ER';
4675              log_message('log_message: ' || pa_debug.G_err_Stage);
4676           END IF;
4677 
4678           If (P_Assignment_Id is not null) Then
4679 
4680              IF PG_DEBUG = 'Y' THEN
4681              pa_debug.G_err_stage := 'P_Assignment_id is not null';
4682                 log_message('log_message: ' || pa_debug.G_err_Stage);
4683              END IF;
4684 
4685              G_Assignment_Id := P_Assignment_Id;
4686 
4687              IF PG_DEBUG = 'Y' THEN
4688              pa_debug.G_err_stage := 'G_Assignment_Id = ' || G_Assignment_Id;
4689                 log_message('log_message: ' || pa_debug.G_err_Stage);
4690              END IF;
4691 
4692           Else
4693 
4694              IF PG_DEBUG = 'Y' THEN
4695              pa_debug.G_err_stage := 'P_Assignment_id is null';
4696                 log_message('log_message: ' || pa_debug.G_err_Stage);
4697              END IF;
4698 
4699              G_Assignment_Id := pa_utils4.get_assignment_id(
4700                                   p_person_id   => G_Person_Id
4701                                  ,p_project_id  => G_Project_Id
4702                                  ,p_task_id     => G_Task_Id
4703                                  ,p_ei_date     => X_Ei_Date);
4704 
4705              IF PG_DEBUG = 'Y' THEN
4706              pa_debug.G_err_stage := 'G_Assignment_Id = ' || G_Assignment_Id;
4707                 log_message('log_message: ' || pa_debug.G_err_Stage);
4708              END IF;
4709 
4710           End If;
4711 
4712        Else
4713 
4714           IF PG_DEBUG = 'Y' THEN
4715           pa_debug.G_err_stage := 'Sys Link is not ST OT ER';
4716              log_message('log_message: ' || pa_debug.G_err_Stage);
4717           END IF;
4718 
4719           G_Assignment_Id := NULL;
4720 
4721        End If;
4722 
4723        /*Bug# 2737538:Added check of profile option value for deriving work type */
4724 /*       If (nvl(pa_utils4.is_exp_work_type_enabled,'N') = 'Y') Then ** moved condition below bug 3104004 */
4725 
4726           If (P_Work_Type_Id is not null) Then
4727 
4728              IF PG_DEBUG = 'Y' THEN
4729              pa_debug.G_err_stage := 'P_Work_Type_Id is not null';
4730                 log_message('log_message: ' || pa_debug.G_err_Stage);
4731              END IF;
4732 
4733              G_Work_Type_Id := P_Work_Type_Id;
4734 
4735              IF PG_DEBUG = 'Y' THEN
4736              pa_debug.G_err_stage := 'G_Work_Type_Id = ' || G_Work_Type_Id;
4737                 log_message('log_message: ' || pa_debug.G_err_Stage);
4738              END IF;
4739 
4740           Else
4741 
4742              IF PG_DEBUG = 'Y' THEN
4743              pa_debug.G_err_stage := 'P_Work_Type_Id is null';
4744                 log_message('log_message: ' || pa_debug.G_err_Stage);
4745              END IF;
4746 
4747              G_Work_Type_Id := pa_utils4.get_work_type_id(
4748                                  p_project_id     => G_Project_Id
4749                                  ,p_task_id        => G_Task_Id
4750                                  ,p_assignment_id  => nvl(G_Assignment_Id,0));
4751 
4752              IF PG_DEBUG = 'Y' THEN
4753              pa_debug.G_err_stage := 'G_Work_Type_Id = ' || G_Work_Type_Id;
4754                 log_message('log_message: ' || pa_debug.G_err_Stage);
4755              END IF;
4756 
4757              If (G_Work_Type_Id is NULL and nvl(pa_utils4.is_exp_work_type_enabled,'N') = 'Y') Then
4758                                                         /*** added and condition bug 3104004 */
4759                 X_Status := 'INVALID_WORK_TYPE';
4760                 pa_cc_utils.reset_curr_function;
4761                 Return;
4762              End If;
4763 
4764           End If;
4765 
4766           IF PG_DEBUG = 'Y' THEN
4767           pa_debug.G_err_stage := 'Get Tp Amt Type';
4768              log_message('log_message: ' || pa_debug.G_err_Stage);
4769           END IF;
4770 
4771           G_Tp_Amt_Type_Code := pa_utils4.get_tp_amt_type_code(
4772                                   p_work_type_id => G_Work_Type_Id);
4773 
4774           IF PG_DEBUG = 'Y' THEN
4775           pa_debug.G_err_stage := 'G_Tp_Amt_Type_Code = ' || G_Tp_Amt_Type_Code;
4776              log_message('log_message: ' || pa_debug.G_err_Stage);
4777           END IF;
4778 
4779        /**End If;** commented bug 3104004 */ /*Bug# 2737538 wrapped tp_amt_type_code and work_type related calls if work type profile is enabled*/
4780 
4781     Else
4782 
4783        IF PG_DEBUG = 'Y' THEN
4784        pa_debug.G_err_stage := 'Calling API to derive/validate assignment and work type info';
4785           log_message('log_message: ' || pa_debug.G_err_Stage);
4786           log_message('log_message: ' || 'Assignment Name = ' || X_Assignment_Name || ' Work Type Name = ' || X_Work_Type_Name);
4787        END IF;
4788 
4789        pa_utils4.get_work_assignment(
4790 	 p_person_id          => G_Person_Id
4791        , p_project_id         => G_Project_Id
4792        , p_task_id            => G_Task_Id
4793        , p_ei_date            => X_Ei_Date
4794        , p_system_linkage     => x_system_linkage
4795        , x_assignment_id      => G_Assignment_Id
4796        , x_assignment_name    => X_Assignment_Name
4797        , x_work_type_id       => G_Work_Type_Id
4798        , x_work_type_name     => X_Work_Type_Name
4799        , x_tp_amt_type_code   => G_Tp_Amt_Type_Code
4800        , x_return_status      => l_asgn_work_ret_sts
4801        , x_error_message_code => l_asgn_work_err_msg);
4802 
4803        --PA-J Txn Ctrl changes: Added ER to the below check.
4804        if (x_system_linkage not in ('ST', 'OT', 'ER')) then
4805           G_Assignment_Id := null;
4806        end if;
4807 
4808        IF PG_DEBUG = 'Y' THEN
4809           log_message('log_message: ' || 'Assignment Id = ' || G_Assignment_Id || ' Work Type Id = ' || G_Work_Type_Id ||
4810                             ' Tp Amt Type = ' || G_Tp_Amt_Type_Code);
4811        END IF;
4812 
4813        IF (l_asgn_work_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
4814 
4815           IF PG_DEBUG = 'Y' THEN
4816           pa_debug.G_err_stage := 'Get Work Type and Assignment API failed';
4817              log_message('log_message: ' || pa_debug.G_err_Stage);
4818              log_message('log_message: ' || 'Ret Sts = ' || l_asgn_work_ret_sts || ' Error = ' || l_asgn_work_err_msg);
4819           END IF;
4820 
4821           X_Status := l_asgn_work_err_msg;
4822           pa_cc_utils.reset_curr_function;
4823           RETURN;
4824 
4825        END IF;
4826 
4827     End If;
4828 
4829     /*=========End Assignment_Id, Work_Type_Id Checks==========================*/
4830 
4831     --PA-K Changes + bug 2634812
4832     --Bug 2688926: Reverted 2634812, removed  the check on total_burden_flag project type option
4833     --For transactions that are externally burdened or externally accounted
4834     --If project is burdened then set the compiled_set_id to -1 if it is null
4835     --Also see PAXTRANB.pls 115.41, where cost_burden_distributed_flag is
4836     --based on the value of compiled_set_id
4837     If (nvl(G_burdened_flag,'N') = 'Y') OR (nvl(G_gl_accted_flag,'N') = 'Y') Then
4838 
4839        If nvl(G_Proj_bcost_flag,'N') = 'Y' Then
4840 
4841           If G_burden_compile_set_id is NULL Then
4842 
4843              G_burden_compile_set_id := -1;
4844 
4845           End If;
4846 
4847        End If;
4848 
4849     End If;
4850 
4851  -- ==========================================================================
4852     -- The following section of the code performs valdiations specific to contingent worker timecards
4853     --  with purchase order integration for cwk. This is being placed before validate_transaction call since
4854     -- we need to pass po_header_id and po_line_id to validate_transaction and if not provided, these
4855     -- will be derived by the PO api validate_temp_labor_po
4856 
4857   IF nvl(P_Person_Type,'EMP') IN ('EMP','CWK') THEN
4858           null;
4859   ELSE
4860    	      X_status := 'INVALID_PERSON_TYPE';
4861           pa_cc_utils.reset_curr_function;
4862           RETURN;
4863   END IF;
4864 
4865   IF ((P_po_number is not null OR P_Po_Line_Num is not null OR
4866          P_Po_Header_Id is not null OR P_Po_Line_Id is not null) AND
4867           x_system_linkage in ('ST','OT'))
4868   THEN
4869 
4870 
4871 	   IF PG_DEBUG = 'Y' THEN
4872 		  pa_debug.G_err_stage := 'If any of the PO attributes are not null';
4873           log_message('log_message: ' || pa_debug.G_err_Stage);
4874        END IF;
4875 
4876                IF Pa_Pjc_CWk_Utils.Is_CWK_TC_Xface_Allowed(G_Project_Id) <> 'Y' THEN
4877                     X_Status := 'PA_CWK_TC_NOT_ALLOWED';
4878                     pa_cc_utils.reset_curr_function;
4879                     RETURN;
4880     		    ELSIF (( nvl(G_gl_accted_flag,'N') = 'Y') OR (nvl(G_trx_costed,'N') = 'Y')) THEN
4881                     X_status := 'PA_CWK_PO_COSTED_NOTSUP';
4882                     pa_cc_utils.reset_curr_function;
4883                     RETURN;
4884               ELSIF nvl(p_person_type,'EMP') = 'EMP' THEN
4885                     X_status := 'PA_EMP_PO_NOTSUP';
4886                     pa_cc_utils.reset_curr_function;
4887                     RETURN;
4888               END IF;
4889 
4890        IF PG_DEBUG = 'Y' THEN
4891 		  pa_debug.G_err_stage := 'Calling PO Validation api';
4892           log_message('log_message: ' || pa_debug.G_err_Stage);
4893        END IF;
4894 
4895 	   PO_PA_INTEGRATION_GRP.validate_temp_labor_po (
4896 				P_Api_Version => 1.0,
4897 				P_Project_Id => G_project_id,
4898 				P_Task_Id => G_task_id,
4899 				P_Po_Number => P_po_number,
4900 				P_Po_Line_Num => P_Po_Line_Num,
4901 				P_Price_Type => P_po_price_type,
4902 				P_Org_Id => X_org_id,
4903 				P_Person_Id => G_person_id,
4904                                 P_effective_date => X_EI_date, -- added for bug 4155220
4905 				P_Po_Header_Id => P_Po_Header_Id,
4906 				P_Po_Line_Id => P_Po_Line_Id,
4907 				X_Po_Line_Amt	=> G_Po_Line_Amt,
4908 				X_Po_Rate => l_po_rate,
4909 				X_Currency_Code => X_denom_currency_code,
4910 				X_Curr_Rate_Type => X_acct_rate_type,
4911 				X_Curr_Rate_Date => X_acct_rate_date,
4912 				X_Currency_Rate => X_acct_exchange_rate,
4913 				X_Vendor_Id =>   G_vendor_id,
4914 				X_Return_StatuS => temp_msg_type,
4915 				X_Message_Code => temp_status);
4916 
4917 	   G_acct_rate_type := X_acct_rate_type; -- Bug: 3570261
4918 
4919 
4920 	   IF PG_DEBUG = 'Y' THEN
4921 		pa_debug.G_err_stage := 'After PO_PA_INTEGRATION_GRP.validate_temp_labor_po ';
4922         log_message('log_message: ' || pa_debug.G_err_Stage);
4923 	   END IF;
4924 
4925 	   IF temp_status IS NOT NULL  THEN
4926          IF PG_DEBUG = 'Y' THEN
4927 			  pa_debug.G_err_stage := 'validate_temp_labor_po has failed';
4928 			  log_message('log_message: ' || pa_debug.G_err_Stage);
4929          END IF;
4930 
4931          X_status := temp_status;
4932          pa_cc_utils.reset_curr_function;
4933          RETURN;
4934 	   END IF;
4935 
4936   /* po amount check */
4937 
4938        IF nvl(l_po_rate,0) = 0 THEN
4939          X_status := 'INVALID_PO_RATE';
4940          pa_cc_utils.reset_curr_function;
4941          RETURN;
4942        END IF;
4943 
4944        l_Calc_Amt := X_qty * l_po_rate;
4945 
4946 
4947 
4948 
4949 		/* Bug 4098920
4950 		select sum(denom_raw_cost)
4951 		into l_costed
4952 		from pa_expenditure_items ei, pa_expenditures exp
4953 		where ei.cost_distributed_flag = 'Y'
4954 		and ei.project_id = G_project_id
4955 		and ei.task_id = G_task_id
4956 		and ei.po_line_id = P_Po_Line_Id
4957 		and ei.system_linkage_function in ('ST','OT')
4958 		and ei.expenditure_id = exp.expenditure_id
4959 		and exp.person_type = 'CWK';
4960 		*/
4961 
4962 
4963 		/*
4964 		select sum(quantity)*l_po_rate
4965 		into l_uncosted
4966 		from pa_expenditure_items ei, pa_expenditures exp
4967 		where ei.cost_distributed_flag = 'N'
4968 		and ei.project_id = G_project_id
4969 		and ei.task_id = G_task_id
4970 		and ei.po_line_id = P_Po_Line_Id
4971 		and ei.system_linkage_function in ('ST','OT')
4972 		and ei.expenditure_id = exp.expenditure_id
4973 		and exp.person_type = 'CWK';
4974 		*/
4975 
4976 		/* Bug 4098920
4977 		Select Sum ( Quantity * PA_TRX_IMPORT.GET_PO_PRICE_TYPE_RATE (Project_id , Task_Id , P_Po_Line_Id , po_price_type ) )
4978 		into l_uncosted
4979 		From
4980 		(
4981 		select ei.project_id , ei.task_id , ei.po_price_type , sum(quantity)  Quantity
4982 		from pa_expenditure_items ei, pa_expenditures exp
4983 		where ei.cost_distributed_flag = 'N'
4984 		and ei.project_id = G_project_id
4985 		and ei.task_id = G_task_id
4986 		and ei.po_line_id = P_Po_Line_Id
4987 		and ei.system_linkage_function in ('ST','OT')
4988 		and ei.expenditure_id = exp.expenditure_id
4989 		and exp.person_type = 'CWK'
4990 		Group By ei.project_id , ei.task_id , ei.po_price_type
4991 		);
4992 		*/
4993 
4994 
4995 		/* Bug 4098920 : Combined the SQLs for Costed and Uncosted EI */
4996 		If  Is_Po_Line_Task_Processed ( P_Po_Line_Id, G_task_id ) = False Then
4997 				Select Sum (
4998 						Decode ( cost_distributed_flag,
4999 									'N', Quantity * PA_TRX_IMPORT.GET_PO_PRICE_TYPE_RATE (G_project_id , G_task_id  , P_Po_Line_Id , po_price_type ), 0
5000 
5001 								)
5002 						) ,
5003 						Sum (
5004 						Decode ( cost_distributed_flag,
5005 									'Y', denom_raw_cost, 0
5006 								)
5007 						)
5008 				Into l_uncosted, l_costed
5009 				From
5010 				(
5011 				select ei.po_price_type ,  ei.cost_distributed_flag,
5012 										   Sum(Decode(ei.cost_distributed_flag, 'N', ei.quantity,0))  Quantity,
5013 										   Sum(Decode(ei.cost_distributed_flag, 'Y', ei.denom_raw_cost,0))  denom_raw_cost
5014 				from pa_expenditure_items ei, pa_expenditures exp
5015 				where  ei.project_id = G_project_id
5016 				and ei.task_id = G_task_id
5017 				and ei.po_line_id = P_Po_Line_Id
5018 				and ei.system_linkage_function in ('ST','OT')
5019 				and ei.expenditure_id = exp.expenditure_id
5020 				and exp.person_type = 'CWK'
5021 				Group By ei.po_price_type , ei.cost_distributed_flag
5022 				);
5023 
5024 		Else
5025 			l_uncosted := 0;
5026 			l_costed := 0;
5027 		End If;
5028 
5029 		IF PG_DEBUG = 'Y' THEN
5030           pa_debug.G_err_stage := 'Get the Processed Amount for Line ID : '||P_Po_Line_Id || ' and Task ID '||G_task_id;
5031           log_message('log_message: ' || pa_debug.G_err_stage,1);
5032         END IF;
5033 
5034 	    po_processed_amt_chk(P_Po_Line_Id   => P_Po_Line_Id
5035                            ,P_Task_Id      => G_task_id
5036                            ,P_Calc_Amt     => l_Calc_Amt + nvl(l_uncosted,0) + nvl(l_costed,0)
5037                            ,X_processed_amt => l_processed_cost
5038                            ,X_status => temp_status);
5039 
5040         IF temp_status IS NOT NULL  THEN
5041           IF PG_DEBUG = 'Y' THEN
5042 			  pa_debug.G_err_stage := 'po_processed_amt_chk unable to get a lock';
5043 			  log_message('log_message: ' || pa_debug.G_err_Stage);
5044 		  END IF;
5045           X_status := temp_status;
5046           pa_cc_utils.reset_curr_function;
5047           RETURN;
5048 	    END IF;
5049 
5050 		IF 0 <= (nvl(G_Po_Line_Amt,0) - nvl(l_processed_cost,0)) then /* Bug 4098920 */
5051 			l_cwk_amt_updated := 'Y' ;
5052 	    else
5053 			x_status := 'INSUFFICIENT_PO_AMOUNT';
5054 
5055          /* Undo changes that are done by po_processed_amt_chk ***/
5056 	       undo_processed_amt_chk(P_Po_Line_Id
5057                               ,G_Task_Id    /* Bug # 3609926 : Changed to G_TASK_ID from P_TASK_ID */
5058                               ,l_Calc_Amt ) ;
5059 		    pa_cc_utils.reset_curr_function;
5060 			RETURN;
5061 	    END IF;
5062 
5063 		/* po amount check */
5064 
5065     END IF; /* po attributes are not null */
5066 
5067     /* Bug # 3639470 : Vendor ID is not populated in case of UnAccounted/Uncosted ERs */
5068 
5069 	If x_system_linkage =  'ER' And NVL(P_Person_Type,'EMP') = 'CWK'  AND
5070 	( NVL(G_trx_costed,'N') <> 'Y' OR NVL(G_gl_accted_flag,'N') <> 'Y' ) Then
5071 
5072 		hr_po_info.get_vendor_for_primary_asg (
5073 											p_person_id      =>  G_person_id ,
5074 											p_effective_date =>  Trunc(X_ei_date) , --Bug 3891559
5075 										    p_vendor_id      =>  G_Vendor_id,
5076 										    p_vendor_site_id =>  G_Vendor_Site_id
5077 											  );
5078 
5079 		If (G_Vendor_id Is Null or G_Vendor_Site_id Is Null ) Then
5080 			x_status := 'PA_INVALID_SUPPLIER_INFO';
5081 			pa_cc_utils.reset_curr_function;
5082 			Return;
5083 		End If;
5084 
5085 	End If;
5086 
5087 
5088 
5089 
5090 
5091 
5092 
5093     -- ==========================================================================
5094     --   Verify that transaction does not violate any transaction controls,
5095     --   including transaction control extensions
5096     --
5097     --   DFF Upgrade:
5098     --   Calls pa_transactions_pub.validate_dff
5099     IF PG_DEBUG = 'Y' THEN
5100     pa_debug.G_err_stage := 'Calling pa_transactions_pub.validate_dff';
5101        log_message('log_message: ' || pa_debug.G_err_Stage);
5102     END IF;
5103 
5104     pa_transactions_pub.validate_dff(
5105 	       p_dff_name    => 'PA_EXPENDITURE_ITEMS_DESC_FLEX',
5106                p_attribute_category => X_att_cat,
5107                p_attribute1 => X_att1,
5108                p_attribute2 => X_att2,
5109                p_attribute3 => X_att3,
5110                p_attribute4 => X_att4,
5111                p_attribute5 => X_att5,
5112                p_attribute6 => X_att6,
5113                p_attribute7 => X_att7,
5114                p_attribute8 => X_att8,
5115                p_attribute9 => X_att9,
5116                p_attribute10 => x_att10,
5117                x_status_code => temp_status,
5118 	       x_error_message => temp_dff_msg);
5119 
5120     IF (temp_status IS NOT NULL) THEN
5121          IF PG_DEBUG = 'Y' THEN
5122             log_message('log_message: ' || temp_dff_msg,1);
5123             log_message('log_message: ' || temp_status,1);
5124          END IF;
5125 			X_status := 'PA_DFF_VALIDATION_FAILED';
5126 
5127 
5128          /*** CWK Changes : Now Undo changes that are done by po_processed_amt_chk ***/
5129      IF l_cwk_amt_updated = 'Y' THEN
5130 	     undo_processed_amt_chk(P_Po_Line_Id
5131                                               ,P_Task_Id
5132                                               ,l_Calc_Amt
5133 		                              );
5134      END IF;
5135          pa_cc_utils.reset_curr_function;
5136          RETURN;
5137     END IF;
5138 
5139     IF PG_DEBUG = 'Y' THEN
5140 	    pa_debug.G_err_stage := 'Calling pa_transactions_pub.validate_transaction';
5141        log_message('log_message: ' || pa_debug.G_err_Stage);
5142     END IF;
5143 
5144 	If PA_TRX_IMPORT.Get_GVal_ProjTskEi_Date = 'Y' then -- Added for BUG6931833
5145 
5146     pa_transactions_pub.validate_transaction(
5147                X_project_id => G_project_id
5148             ,  X_task_id => G_task_id
5149             ,  X_ei_date => X_ei_date
5150             ,  X_expenditure_type  => X_etype
5151             ,  X_non_labor_resource => X_nlr
5152             ,  X_person_id  => G_person_id
5153             ,  X_quantity => X_qty
5154             ,  X_denom_currency_code => X_denom_currency_code
5155             ,  X_acct_currency_code => G_accounting_currency_code
5156             ,  X_denom_raw_cost  => X_denom_raw_cost
5157             ,  X_acct_raw_cost => X_acct_raw_cost
5158             ,  X_acct_rate_type => G_acct_rate_type
5159             ,  X_acct_rate_date => X_acct_rate_date
5160             ,  X_acct_exchange_rate => X_acct_exchange_rate
5161             ,  X_transfer_ei => NULL
5162             -- Trx_import enhancement: If G_verride_to_org_id is not NULL,
5163             -- then that means we will check override to organization
5164             ,  X_incurred_by_org_id => nvl(G_override_to_org_id, G_org_id)
5165             ,  X_nl_resource_org_id => G_nlro_id
5166             ,  X_transaction_source => X_trx_src
5167             -- Bug 987539: Used to be hard code to 'PAXTRTRX'
5168             -- changing it to use the parameter validate_item was called
5169             ,  X_calling_module => X_module
5170        	    ,  X_vendor_id => NULL
5171             ,  X_entered_by_user_id => G_user
5172             ,  X_attribute_category => X_att_cat
5173  	    ,  X_attribute1 => X_att1
5174             ,  X_attribute2 => X_att2
5175             ,  X_attribute3 => X_att3
5176             ,  X_attribute4 => X_att4
5177             ,  X_attribute5 => X_att5
5178             ,  X_attribute6 => X_att6
5179             ,  X_attribute7 => X_att7
5180             ,  X_attribute8 => X_att8
5181             ,  X_attribute9 => X_att9
5182             ,  X_attribute10 => X_att10
5183        	    ,  X_attribute11 => NULL
5184             ,  X_attribute12 => NULL
5185             ,  X_attribute13 => NULL
5186             ,  X_attribute14 => NULL
5187             ,  X_attribute15 => NULL
5188             ,  X_msg_application => temp_msg_application
5189             ,  X_msg_type => temp_msg_type
5190             ,  X_msg_token1 => temp_msg_token1
5191             ,  X_msg_token2 => temp_msg_token2
5192             ,  X_msg_token3 => temp_msg_token3
5193             ,  X_msg_count => temp_msg_count
5194             ,  X_msg_data => temp_status
5195             ,  X_billable_flag=> temp_bill_flag
5196             -- PA-I Changes
5197             -- Added Assignment_Id and Work_Type_Id
5198             ,  p_projfunc_currency_code   => X_Projfunc_Currency_Code
5199             ,  p_projfunc_cost_rate_type  => G_projfunc_cost_rate_type
5200             ,  p_projfunc_cost_rate_date  => X_Projfunc_Cost_Rate_Date
5201             ,  p_projfunc_cost_exchg_rate => X_projfunc_cost_exchange_rate
5202             ,  P_Assignment_Id => G_Assignment_Id
5203             ,  P_Work_Type_Id  => G_Work_Type_Id
5204             -- PA-J Txn Ctrl Changes
5205             ,  P_SYS_LINK_FUNCTION => x_system_linkage
5206 	    ,  P_Po_Header_Id  =>  P_Po_Header_Id /* cwk */
5207 	    , P_Po_Line_Id => P_Po_Line_Id
5208 	    , P_Person_Type => P_Person_Type
5209 	    , P_Po_Price_Type => P_Po_Price_Type );
5210 
5211     END IF;
5212 
5213     IF PG_DEBUG = 'Y' THEN
5214     pa_debug.G_err_stage := 'After pa_transactions_pub.validate_transaction';
5215        log_message('log_message: ' || pa_debug.G_err_Stage);
5216     END IF;
5217 
5218     -- IF G_skip_tc_flag <> 'Y' THEN  /* commented for Bug # 2108456 */
5219 
5220     IF ( temp_msg_type = 'E' AND temp_status IS NOT NULL ) THEN
5221 
5222 		 IF PG_DEBUG = 'Y' THEN
5223 			pa_debug.G_err_stage := 'Validate_transaction has failed' ;
5224             log_message('log_message: ' || pa_debug.G_err_Stage);
5225          END IF;
5226 
5227          X_status := temp_status;
5228 
5229 	 /*** CWK Changes : Now Undo changes that are done by po_processed_amt_chk ***/
5230      IF l_cwk_amt_updated = 'Y' THEN
5231       undo_processed_amt_chk(P_Po_Line_Id
5232                                               ,P_Task_Id
5233                                               ,l_Calc_Amt );
5234 	 END IF;
5235 
5236 	 pa_cc_utils.reset_curr_function;
5237      RETURN;
5238 
5239     END IF;
5240 
5241     -- END IF ; /* commented for Bug # 2108456 */
5242     X_bill_flag := temp_bill_flag; /*added for bug 6509828*/
5243       --
5244     -- The following section of the code has been written
5245     -- for funds check related changes
5246     --
5247     --PA-J Receipt Accrual Changes: Added AP VARIANCE and PO RECEIPT
5248     IF G_Process_Funds_Flag = 'Y' AND   ---{
5249        --Pa-K Changes: Using the process_funds_check attributes of the source to perform funds checking
5250        --X_trx_src in ('AP INVOICE', 'AP VARIANCE', 'PO RECEIPT', 'AP NRTAX', 'PO RECEIPT NRTAX') AND
5251        X_acct_raw_cost <> 0 AND
5252        nvl(G_gl_accted_flag,'N') = 'Y' AND
5253        G_SobId = G_RecvrSobId AND
5254        --Pa_Funds_Control_Utils.Get_Fnd_Reqd_Flag(G_project_id, 'STD') = 'Y'
5255        -- REL12 AP Lines Uptake
5256 	   NVL(p_fc_document_type, 'NOT' )  <> 'NOT' THEN
5257 
5258          IF PG_DEBUG = 'Y' THEN
5259          pa_debug.G_err_stage := 'Before calling tr_import_funds_check';
5260             log_message('log_message: ' || pa_debug.G_err_Stage);
5261          END IF;
5262 
5263          --Bug 2905892
5264          --Assign txn gl_Date for insert of the reversing and EXP lines into pa_bc_packets
5265          G_FC_Gl_Date := X_Gl_Date;
5266 
5267          tr_import_funds_check (p_pa_date              => pa_date,
5268                                 p_txn_source           => X_trx_src,
5269                                 p_acct_raw_cost        => X_acct_raw_cost,
5270 				p_adj_exp_item_id      => p_adj_exp_item_id,
5271 				p_txn_interface_id     => x_txn_interface_id,
5272 				p_fc_document_type     => p_fc_document_type,
5273                                 x_packet_id            => l_packet_id,
5274                                 x_error_message_code   => l_fc_error_msg,
5275                                 x_error_stage          => l_fc_error_stage,
5276                                 x_return_status        => l_fc_return_status);
5277 
5278          IF l_fc_return_status <>  FND_API.G_RET_STS_SUCCESS then
5279 
5280               IF PG_DEBUG = 'Y' THEN
5281               pa_debug.G_err_stage := 'Call to tr_import_funds_check did not succeed';
5282                  log_message('log_message: ' || pa_debug.G_err_Stage);
5283               END IF;
5284 
5285               x_status := l_fc_error_msg ;
5286               pa_cc_utils.reset_curr_function;
5287               return;
5288          END IF;
5289 
5290          IF PG_DEBUG = 'Y' THEN
5291          pa_debug.G_err_stage := 'Call to internal funds check packet insert success';
5292             log_message('log_message: ' || pa_debug.G_err_Stage);
5293          END IF;
5294 
5295          IF Pa_Funds_Control_Pkg.pa_funds_check(p_calling_module  => 'TRXIMPORT',
5296                                                 p_conc_flag       => 'Y',
5297                                                 p_set_of_book_id  => 1,
5298                                                 p_packet_id       => l_packet_id,
5299                                                 p_mode            => 'R',
5300                                                 p_partial_flag    => 'N',
5301                                                 x_return_status   => l_fc_return_status,
5302                                                 x_error_stage     => l_fc_error_stage,
5303                                                 x_error_msg       => l_fc_error_msg) THEN
5304 
5305               IF l_fc_return_status <>  FND_API.G_RET_STS_SUCCESS then
5306 
5307                  IF PG_DEBUG = 'Y' THEN
5308                  pa_debug.G_err_stage := 'Call to funds check not success';
5309                     log_message('log_message: ' || pa_debug.G_err_Stage);
5310                  END IF;
5311 
5312                  --x_status := l_fc_return_status ;
5313                  x_status := 'PA_FC_ERROR' ;
5314                  pa_cc_utils.reset_curr_function;
5315                  return;
5316               END IF;
5317 
5318               IF PG_DEBUG = 'Y' THEN
5319               pa_debug.G_err_stage := 'Check if there are any failed records';
5320               END IF;
5321                  log_message('log_message: ' || pa_debug.G_err_Stage);
5322 
5323               Open c_check_fail(l_packet_id);
5324               Fetch c_check_fail
5325                into l_dummy ;
5326 
5327               close c_check_fail ;
5328 
5329               IF PG_DEBUG = 'Y' THEN
5330               pa_debug.G_err_stage := 'After select to check for funds check failed records';
5331                  log_message('log_message: ' || pa_debug.G_err_Stage);
5332               END IF;
5333               IF l_dummy = 'X' Then
5334 
5335                  IF PG_DEBUG = 'Y' THEN
5336                  pa_debug.G_err_stage := 'This transaction failed funds check';
5337                     log_message('log_message: ' || pa_debug.G_err_Stage);
5338                  END IF;
5339 
5340                  x_status := 'PA_FC_ERROR' ;
5341 
5342                  pa_cc_utils.reset_curr_function;
5343                  return;
5344               ELSE
5345                  IF PG_DEBUG = 'Y' THEN
5346                  pa_debug.G_err_stage := 'This transaction passed funds check';
5347                     log_message('log_message: ' || pa_debug.G_err_Stage);
5348                  END IF;
5349 
5350                  NULL;
5351 
5352               END IF;
5353          ELSE
5354 
5355               IF PG_DEBUG = 'Y' THEN
5356               pa_debug.G_err_stage := 'Error during funds check';
5357                  log_message('log_message: ' || pa_debug.G_err_Stage);
5358               END IF;
5359 
5360               x_status := 'PA_FC_UNEXP_ERROR' ;
5361               pa_cc_utils.reset_curr_function;
5362               return;
5363          END IF;
5364          IF PG_DEBUG = 'Y' THEN
5365          pa_debug.G_err_stage := 'Done with funds check';
5366             log_message('log_message: ' || pa_debug.G_err_Stage);
5367          END IF;
5368     END IF;   --- }
5369 
5370    /* X_bill_flag := temp_bill_flag; commented for bug 6509828*/
5371 
5372     --PA-J Txn Ctrl Changes
5373     --Assignment_Id is overwritten by the User in PATC/PATCX.
5374     --Since p_assignment_id in PATC cannot be changed to IN/OUT, we are using the
5375     --the global variable set in PATC.
5376     --Thus assign PATC.global variable to G_Assignment_Id here for sys links ER,ST,OT.
5377     if (x_system_linkage in ('ST', 'OT', 'ER')) then
5378         pa_debug.G_err_stage := 'Override Assignment Id';
5379         G_Assignment_Id := PATC.G_OVERIDE_ASSIGNMENT_ID;
5380 
5381         /* Start of bug 2648550 */
5382         G_Work_Type_Id := PATC.G_OVERIDE_WORK_TYPE_ID;
5383         G_Tp_Amt_Type_Code := PATC.G_OVERIDE_TP_AMT_TYPE_CODE;
5384         X_Assignment_Name :=  PATC.G_OVERIDE_ASSIGNMENT_NAME ;
5385         X_Work_Type_Name := PATC.G_OVERIDE_WORK_TYPE_NAME;
5386         /* End of bug 2648550 */
5387 
5388         IF PG_DEBUG = 'Y' THEN
5389            log_message('log_message: ' || 'Overriding Assignment Id = ' || G_Assignment_Id);
5390         END IF;
5391     end if;
5392 
5393     IF PG_DEBUG = 'Y' THEN
5394     pa_debug.G_err_stage := 'Done with Validate Item';
5395        log_message('log_message: ' || pa_debug.G_err_Stage);
5396     END IF;
5397     pa_cc_utils.reset_curr_function; /* bug 2181553 */
5398 
5399 -- S.N. CWK changes -> hkulkarn ---> If called from form, release the locks.
5400 
5401     IF (X_module = 'EXTERNAL') THEN
5402        release_po_line_task_lock;
5403 	   init_po_amt_chk; /* Bug # 3609926 : To free the PL/SQL tables if called from the form */
5404     END IF;
5405 
5406 -- E.N. CWK changes
5407 
5408   EXCEPTION
5409     WHEN OTHERS THEN
5410        IF PG_DEBUG = 'Y' THEN
5411           log_message('log_message: ' || 'In OTHERS of ValidateItem');
5412           log_message('log_message: ' || pa_debug.G_err_Stack,1);
5413           log_message('log_message: ' || SQLERRM,1);
5414           log_message('log_message: ' || pa_debug.G_err_stage,1);
5415        END IF;
5416 
5417        release_po_line_task_lock; -- bug 3512984
5418 
5419        pa_cc_utils.reset_curr_function; /* bug 2181553 */
5420 
5421        RAISE ;
5422   END  ValidateItem;
5423 
5424   PROCEDURE  ValidateOrgId (
5425                 X_org_id       IN NUMBER
5426              ,  X_status       OUT NOCOPY VARCHAR2 )
5427   IS
5428   BEGIN
5429     pa_cc_utils.set_curr_function('ValidateOrgId');
5430 
5431     --PA.K Changes: For Performance moved the Multi-Org check to init procedure.
5432     --IF pa_utils.pa_morg_implemented = 'Y' THEN
5433 
5434      /* Added for bug 3590027 */
5435      IF PG_DEBUG = 'Y' THEN
5436            log_message('Before call, G_Morg: ' || G_Morg);
5437      END IF;
5438 
5439      If G_Morg is Null Then
5440         G_Morg := pa_utils.pa_morg_implemented;
5441      End If;
5442 
5443      IF PG_DEBUG = 'Y' THEN
5444            log_message('After call, G_Morg: ' || G_Morg);
5445      END IF;
5446      /* Added for bug 3590027 End */
5447 
5448     IF G_Morg = 'Y' THEN
5449        IF X_org_id IS NULL THEN
5450           X_status := 'MISSING_ORG_ID';
5451            pa_cc_utils.reset_curr_function;
5452           RETURN;
5453        END IF;
5454     END IF;
5455 
5456     pa_cc_utils.reset_curr_function;
5457   EXCEPTION
5458     WHEN OTHERS THEN
5459        IF PG_DEBUG = 'Y' THEN
5460           log_message('log_message: ' || pa_debug.G_err_Stack,1);
5461           log_message('log_message: ' || pa_debug.G_err_Stage,1);
5462           log_message('log_message: ' || SQLERRM,1);
5463        END IF;
5464        RAISE ;
5465 
5466   END ValidateOrgId;
5467 
5468   -- Added the function below for bug number 1275169.
5469   /* For bug 1426802 change employee_number from NUMBER to VARCHAR2 */
5470 
5471  FUNCTION GetOrgName(employee_number IN VARCHAR2,
5472                       expenditure_item_date IN DATE,
5473                       business_group_name IN VARCHAR2,
5474 		      person_type IN VARCHAR2) RETURN VARCHAR2 IS
5475   X_emp_id  NUMBER;
5476   /* Bug No.2487147, UTF8 change, used %TYPE for X_org_name */
5477   /* X_org_name VARCHAR2(60); */
5478      X_org_name  hr_organization_units.name%TYPE;
5479   BEGIN
5480 
5481     --PA-K Changes: Caching the values.
5482 
5483     if employee_number is null then
5484       RETURN NULL;
5485     else
5486 
5487       If (G_OrgNameEmpNum = employee_number) and
5488          (trunc(G_OrgNameDate) = trunc(expenditure_item_date)) and
5489          (G_OrgNameBGName = business_group_name) and
5490 	 (G_person_type = nvl(person_type,'EMP'))Then
5491 
5492          RETURN G_OrgNameOrgName;
5493 
5494       Else
5495 
5496          G_OrgNameBGId := pa_utils2.GetBusinessGroupId(business_group_name);
5497 
5498          BEGIN
5499 
5500             pa_utils2.GetEmpId ( P_Business_Group_Id  => G_OrgNameBGId
5501                       , P_Employee_Number          => employee_number
5502                       , X_Employee_Id              => X_emp_id
5503 		      , P_Person_Type  => person_type
5504                       , P_EiDate                   => expenditure_item_date);
5505 
5506          EXCEPTION
5507             WHEN  NO_DATA_FOUND  THEN
5508                   NULL;
5509             WHEN  TOO_MANY_ROWS  THEN
5510                   NULL;
5511             WHEN  OTHERS  THEN
5512                   NULL;
5513          END;
5514 
5515          --X_emp_id := pa_utils.GetEmpId(employee_number) ;
5516          if X_emp_id is null then
5517            RETURN NULL;
5518          else
5519 
5520             X_org_name := pa_expenditures_utils.getorgtlname(pa_utils.GetEmpOrgId(X_emp_id, expenditure_item_date));
5521 
5522            G_OrgNameEmpNum := employee_number;
5523            G_OrgNameDate   := expenditure_item_date;
5524            G_OrgNameBGName := business_group_name;
5525            G_OrgNameOrgName  := X_org_name;
5526 	   G_person_type := nvl(person_type,'EMP');
5527 
5528            RETURN X_org_name;
5529          end if;
5530 
5531       End If;
5532 
5533     end if;
5534   EXCEPTION
5535   WHEN NO_DATA_FOUND THEN
5536         RETURN null;
5537   WHEN OTHERS THEN
5538         RAISE;
5539   END GetOrgName;
5540   --
5541   -- 5235363 R12.PJ:XB4:DEV:APL:EXP ITEM DATE VALIDATIONS FOR SUPPLIER COST.
5542   --
5543   PROCEDURE set_supplier_cost_eidate( X_transaction_source  IN VARCHAR2
5544                                , X_batch               IN VARCHAR2
5545                                , X_xface_id            IN NUMBER )
5546   IS
5547    V_src_txnifIDTab        PA_PLSQL_DATATYPES.IdTabTyp;
5548    V_dst_txnifIDTab        PA_PLSQL_DATATYPES.IdTabTyp;
5549    V_dst_ifIDTab           PA_PLSQL_DATATYPES.IdTabTyp;
5550    v_src_EiDateTab         PA_PLSQL_DATATYPES.DateTabTyp;
5551    v_dst_EiDateTab         PA_PLSQL_DATATYPES.DateTabTyp;
5552    v_dst_txnstatcdTab      PA_PLSQL_DATATYPES.Char1TabTyp;
5553    v_doc_headerIDTab       PA_PLSQL_DATATYPES.IdTabTyp;
5554    v_week_ending_dtTab     PA_PLSQL_DATATYPES.DateTabTyp;
5555 
5556 
5557   BEGIN
5558       pa_cc_utils.set_curr_function('Set_supplier_cost_eidate');
5559 
5560       IF PG_DEBUG = 'Y' THEN
5561          log_message('log_message: Set_supplier_cost_eidate begins ' );
5562          log_message('log_message: Populate bulk variables ' );
5563       END IF;
5564       v_src_txnifIDTab.DELETE ;
5565       v_src_eidateTab.DELETE ;
5566       v_dst_ifIDTab.DELETE ;
5567       v_dst_TxnifIDTab.DELETE ;
5568       v_dst_eidateTab.DELETE ;
5569       v_dst_txnstatcdTab.DELETE ;
5570       v_doc_headerIDTab.DELETE ;
5571       v_week_ending_dtTab.DELETE ;
5572 
5573       select a.txn_interface_id,
5574              a.expenditure_item_date,
5575 	     b.interface_id,
5576 	     b.txn_interface_id,
5577 	     b.expenditure_item_date,
5578 	     b.transaction_status_code
5579         BULK Collect into
5580              v_src_txnifIDTab,
5581 	     v_src_eidateTab,
5582 	     v_dst_ifIDTab,
5583 	     v_dst_TxnifIDTab,
5584 	     v_dst_eidateTab,
5585 	     v_dst_txnstatcdTab
5586         from pa_transaction_interface_all a,
5587 	     pa_transaction_interface_all b
5588        where a.interface_id          = X_xface_id
5589          and a.transaction_source    = X_transaction_source
5590 	 and a.batch_name            = X_batch
5591 	 and a.transaction_source    = b.transaction_source
5592 	 and b.interface_id          <> X_xface_id
5593 	 and a.cdl_system_reference1 = b.cdl_system_reference1
5594 	 and a.cdl_system_reference2 = b.cdl_system_reference2
5595 	 and a.cdl_system_reference3 = b.cdl_system_reference3
5596 	 and NVL(a.cdl_system_reference4,'0')  = NVL(b.cdl_system_reference4, '0')
5597 	 and NVL(a.cdl_system_reference5,0)    = NVL(b.cdl_system_reference5,0)
5598 	 and b.transaction_status_code        <> 'A'
5599 	 and NVL(a.expenditure_item_id, -1)   <> 0
5600 	 and a.adjusted_expenditure_item_id is NULL ;
5601 
5602       IF PG_DEBUG = 'Y' THEN
5603          log_message('log_message:  v_src_txnifIDTab.count '|| v_src_txnifIDTab.count );
5604       END IF ;
5605 
5606       IF v_src_txnifIDTab.count > 0 then
5607          IF PG_DEBUG = 'Y' THEN
5608             log_message('log_message: Updating the ei date   ' );
5609          END IF ;
5610 
5611          FORALL indx in 1..v_src_TxnIfIDTab.count
5612 	        Update pa_transaction_interface_all
5613 		   set expenditure_item_date =  v_dst_eidateTab(indx)
5614 		 where txn_interface_id  = v_src_TxnIfIDTab(indx) ;
5615 
5616 
5617          IF PG_DEBUG = 'Y' THEN
5618             log_message('log_message: Updating the ei date on adjusted transactions(reversals)  ' );
5619          END IF ;
5620 
5621          FORALL indx in 1..v_src_TxnIfIDTab.count
5622 	        Update pa_transaction_interface_all
5623 		   set expenditure_item_date =  v_dst_eidateTab(indx)
5624 		 where adjusted_txn_interface_id  = v_src_TxnIfIDTab(indx)
5625 		   and net_zero_adjustment_flag   = 'Y' ;
5626 
5627          IF PG_DEBUG = 'Y' THEN
5628             log_message('log_message: Determine the week ending date for the affected transactions. ' );
5629          END IF ;
5630 
5631           select pa_utils.getweekending(max(a.expenditure_item_date)),
5632 	         a.cdl_system_reference2
5633             BULK Collect into
5634 	         v_week_ending_dtTab,
5635 		 v_doc_headerIDTab
5636             from pa_transaction_interface_all a,
5637 	         pa_transaction_interface_all b
5638            where a.interface_id          = X_xface_id
5639              and a.transaction_source    = X_transaction_source
5640 	     and a.batch_name            = X_batch
5641 	     and a.transaction_source    = b.transaction_source
5642 	     and b.interface_id          <> X_xface_id
5643 	     and a.cdl_system_reference1 = b.cdl_system_reference1
5644 	     and a.cdl_system_reference2 = b.cdl_system_reference2
5645 	     and a.cdl_system_reference3 = b.cdl_system_reference3
5646 	     and NVL(a.cdl_system_reference4,'0') = NVL(b.cdl_system_reference4, '0')
5647 	     and nvl(a.cdl_system_reference5,0)   = nvl(b.cdl_system_reference5 ,0)
5648 	     and b.transaction_status_code        <> 'A'
5649 	   group by a.cdl_system_reference2 ;
5650 
5651          IF v_doc_headerIDTab.count > 0 THEN
5652 
5653             IF PG_DEBUG = 'Y' THEN
5654                log_message('log_message: Update the weekending date... '|| v_doc_headerIDTab.count );
5655             END IF ;
5656 
5657             FORALL indx in 1..v_doc_headerIDTab.count
5658 	        UPDATE pa_transaction_interface_all
5659 		   set expenditure_ending_date = v_week_ending_dtTab(indx)
5660 		 where interface_id          = X_xface_id
5661 		   and transaction_source    = X_transaction_source
5662 		   and batch_name            = X_batch
5663 		   and cdl_system_reference2 =v_doc_headerIDTab(indx) ;
5664 
5665 	 END IF ;
5666          IF PG_DEBUG = 'Y' THEN
5667             log_message('log_message: Deleting the previous batch rejected transactions.  ' );
5668          END IF ;
5669 	 FORALL indx in 1..v_dst_TxnIfIdTab.count
5670 	        delete from pa_transaction_interface_all
5671 		 where txn_interface_id = v_dst_TxnIfIdTab(indx) ;
5672 
5673       END IF ;
5674       IF PG_DEBUG = 'Y' THEN
5675          log_message('log_message: End of set_supplier_cost_eidate.  ' );
5676       END IF ;
5677 
5678       pa_cc_utils.reset_curr_function;
5679   END set_supplier_cost_eidate ;
5680 
5681   PROCEDURE import1( X_transaction_source  IN VARCHAR2
5682                    , X_batch               IN VARCHAR2
5683                    , X_xface_id            IN NUMBER
5684                    , X_userid              IN NUMBER
5685                    , X_online_exp_comment  IN VARCHAR2 )
5686   IS
5687 
5688     dummy              NUMBER;
5689     G_expenditure_id   NUMBER(15);
5690 
5691     temp_status        NUMBER DEFAULT NULL;
5692     X_billable_flag    VARCHAR2(1);
5693     X_org_status       VARCHAR2(30);
5694     X_status           VARCHAR2(30);
5695 
5696     -- REL12 AP Lines Uptake
5697     -- Cash based accounting support
5698     --
5699     l_status           VARCHAR2(30);
5700 
5701     l_return_status    Varchar2(10);
5702 
5703     error_msg          VARCHAR2(512);
5704 
5705     FIRST_RECORD       BOOLEAN DEFAULT TRUE;
5706     GROUP_CREATED      BOOLEAN DEFAULT TRUE;
5707     REJECT_EXP	       BOOLEAN DEFAULT FALSE;
5708 
5709     /*Added three fields  local for bug 2048868*/
5710     prev_acct_rate_type      varchar2(30);
5711     prev_acct_rate_date      date;
5712     prev_acct_exchange_rate  Number;
5713     prev_denom_currency_code varchar2(30);
5714     prev_person_type         varchar2(30);
5715 
5716     -- REL12 AP Lines Uptake
5717     -- Cash based accounting support
5718     --
5719     l_rejectedSysRef5        NUMBER ;
5720     l_invoice_id             NUMBER ;
5721     l_payment_status_flag    varchar2(1) ;
5722 
5723     CURSOR c_final_payment is
5724     SELECT payment_status_flag
5725      FROM ap_payment_schedules_all pmt
5726     WHERE pmt.invoice_id          = l_invoice_id
5727       AND pmt.payment_status_flag = 'Y' ;
5728 
5729 
5730     --Bug 987539: New variable to identify calling module
5731     v_calling_module   VARCHAR2(30);
5732 
5733     CURSOR TrxBatches
5734     IS
5735     SELECT
5736 	    xc.transaction_source
5737     ,       xc.batch_name
5738     ,       xc.system_linkage_function
5739     ,       xc.batch_name ||xc.system_linkage_function|| to_char(X_xface_id) exp_group_name
5740     ,       xc.transaction_count
5741     ,       xc.processed_count
5742       FROM
5743             pa_transaction_xface_control xc
5744      WHERE
5745             xc.transaction_source = X_transaction_source
5746        AND  xc.batch_name         = nvl(X_batch, xc.batch_name)
5747        AND  xc.status             = 'PENDING'
5748   --PA-K Changes: Added intermediate_flag
5749   ORDER BY  intermediate_flag desc;
5750 
5751     TrxBatch		TrxBatches%ROWTYPE;
5752 
5753     /* Trx_import enhancment: Modify the expend field in order to change the
5754        expenditure grouping logic
5755        PA-K Changes: Added the new ID columns.
5756                      Implementing 4 cursors depending on the system linkage and pre-defined flag
5757                      Expend and Expend2 will be constructed from the PLSQL tables in which the
5758                      values have been selected.
5759     */
5760 
5761    /** Modified the following cursors to support ID columns for Non-Oracle Txns
5762    *** The current code is based on four cursors - two cursors for pre-defined sources
5763    *** (one for ST, OT and the other for other than ST and OT) - two other cursors
5764    *** for non-oracle sources of which one for ST, OT and the other for rest Sys Links
5765    *** Now these four are collapsed into two cursors - irrespective of a transaction
5766    *** source whether pre-defined on user-defined - once cursor for ST and OT and the
5767    *** other for system linkages other than ST and OT
5768    *** Cursors are removed from the code and not commented for the sake of clarity.
5769    *** Hence there will be only two cursors, defined, opened, fetched from and closed
5770    *** with these changes
5771    **/
5772 
5773     /* TrxRecs1 is for ST, OT */
5774     CURSOR TrxRecs1 ( X_transaction_source  VARCHAR2
5775                    , current_batch         VARCHAR2
5776                    , curr_etype_class_code VARCHAR2  )
5777     IS
5778     SELECT
5779             system_linkage
5780     ,       trunc(expenditure_ending_date) expenditure_ending_date
5781     ,       employee_number
5782 
5783 /* Moving the logic for getting organization name based on the G_emp_oride_flag to TrxRec cursor
5784    for fix 2686544 */
5785     ,	    organization_name
5786 /* Bug fix 2655157 starts */
5787 /*    ,       decode (G_emp_org_oride,
5788                         'Y',
5789                         decode (organization_name,
5790                                         null,
5791                                         GetOrgName(employee_number, expenditure_item_date, person_business_group_name),
5792                                         organization_name),
5793                         decode (employee_number,
5794                                         null,
5795                                         organization_name,
5796                                         GetOrgName(employee_number, expenditure_item_date, person_business_group_name))
5797                    ) organization_name */
5798            /* decode( employee_number, NULL, organization_name,
5799             decode(G_emp_org_oride,'Y',organization_name,NULL))
5800                  organization_name */
5801 /* Bug fix 2655157 ends */
5802     ,       trunc(expenditure_item_date) expenditure_item_date
5803     ,       project_number
5804     ,       task_number
5805     ,       expenditure_type
5806     ,       non_labor_resource
5807     ,       non_labor_resource_org_name
5808     ,       quantity
5809     ,       raw_cost
5810     ,       raw_cost_rate
5811     ,       orig_transaction_reference
5812     ,       attribute_category
5813     ,       attribute1
5814     ,       attribute2
5815     ,       attribute3
5816     ,       attribute4
5817     ,       attribute5
5818     ,       attribute6
5819     ,       attribute7
5820     ,       attribute8
5821     ,       attribute9
5822     ,       attribute10
5823     ,       expenditure_comment
5824     ,       interface_id
5825     ,       expenditure_id
5826     ,       nvl(unmatched_negative_txn_flag, 'N') unmatched_negative_txn_flag
5827     ,       to_number( NULL )  expenditure_item_id
5828     ,       org_id             org_id
5829     ,       dr_code_combination_id
5830     ,       cr_code_combination_id
5831     ,       cdl_system_reference1
5832     ,       cdl_system_reference2
5833     ,       cdl_system_reference3
5834     ,       cdl_system_reference4
5835     ,       cdl_system_reference5
5836     ,       gl_date
5837     ,       burdened_cost
5838     ,       burdened_cost_rate
5839     ,       receipt_currency_amount
5840     ,       receipt_currency_code
5841     ,	    receipt_exchange_rate
5842     ,       denom_currency_code
5843     ,	    denom_raw_cost
5844     ,	    denom_burdened_cost
5845     ,	    acct_rate_date
5846     ,	    acct_rate_type
5847     ,       acct_exchange_rate
5848 --    ,       pa_currency.round_currency_amt1(acct_raw_cost) acct_raw_cost  -- Bug 7522080
5849     ,       acct_raw_cost
5850     ,       acct_burdened_cost
5851     ,       acct_exchange_rounding_limit
5852     ,       project_currency_code
5853     ,       project_rate_date
5854     ,       project_rate_type
5855     ,       project_exchange_rate
5856     ,       orig_exp_txn_reference1
5857     ,       orig_user_exp_txn_reference
5858     ,       vendor_number
5859     ,       orig_exp_txn_reference2
5860     ,       orig_exp_txn_reference3
5861     ,       override_to_organization_name
5862     ,       reversed_orig_txn_reference
5863     ,       billable_flag
5864     ,       txn_interface_id
5865     ,       person_business_group_name
5866     ,       projfunc_currency_code
5867     ,       projfunc_cost_rate_date
5868     ,       projfunc_cost_rate_type
5869     ,       projfunc_cost_exchange_rate
5870     ,       project_raw_cost
5871     ,       project_burdened_cost
5872     ,       assignment_name
5873     ,       work_type_name
5874     ,       nvl(accrual_flag,'N') accrual_flag
5875     ,       project_id
5876     ,       task_id
5877     ,       person_id
5878     ,       organization_id
5879     ,       non_labor_resource_org_id
5880     ,       vendor_id
5881     ,       override_to_organization_id
5882     ,       assignment_id
5883     ,       work_type_id
5884     ,       person_business_group_id
5885     ,       po_number  /* cwk */
5886     ,       po_header_id
5887     ,       po_line_num
5888     ,       po_line_id
5889     ,       person_type
5890     ,       po_price_type
5891     ,       wip_resource_id
5892     ,       inventory_item_id
5893     ,       unit_of_measure
5894     ,       adjusted_expenditure_item_id
5895     ,       NVL(fc_document_type, 'NOT')
5896     ,       NULL document_type
5897     ,       document_distribution_type -- R12 AP lines uptake : Prepayment changes
5898     ,       si_assets_addition_flag
5899     ,       NULL -- adjusted_txn_interface_id
5900     ,       NULL -- net_zero_adjustment_flag
5901     ,       NULL -- sc_xfer_code
5902     ,       0    -- final_payment_id
5903   --  ,       agreement_id  --FSIO Changes
5904    -- ,       agreement_number
5905       FROM pa_transaction_interface
5906      WHERE transaction_source = X_transaction_source
5907        AND batch_name = current_batch
5908        AND transaction_status_code = 'P'
5909        AND system_linkage in ('ST', 'OT')
5910     ORDER BY
5911             expenditure_ending_date DESC
5912     ,       decode(nvl(person_id,0), 0, employee_number, person_id)
5913     ,       decode(nvl(organization_id,0), 0, organization_name, organization_id)
5914     ,       orig_exp_txn_reference1
5915     ,       orig_user_exp_txn_reference
5916     ,       decode(nvl(vendor_id,0), 0, vendor_number, vendor_id)
5917     ,       orig_exp_txn_reference2
5918     ,       orig_exp_txn_reference3
5919     ,       denom_currency_code
5920     ,	    acct_rate_date
5921     ,	    acct_rate_type
5922     ,	    acct_exchange_rate
5923     ,       expenditure_item_date
5924     ,       decode(nvl(project_id,0), 0, project_number, project_id)
5925     ,       decode(nvl(task_id,0), 0, task_number, task_id)
5926     ,       accrual_flag    ;
5927 
5928 
5929     /* TrxRecs2 is for other than ST, OT */
5930 
5931     CURSOR TrxRecs2 ( X_transaction_source  VARCHAR2
5932                    , current_batch         VARCHAR2
5933                    , curr_etype_class_code VARCHAR2  )
5934     IS
5935     SELECT
5936             system_linkage
5937     ,       trunc(expenditure_ending_date) expenditure_ending_date
5938     ,       employee_number
5939 
5940 /* Moving the logic for getting organization name based on the G_emp_oride_flag to TrxRec cursor
5941    for fix 2686544 */
5942     ,	    organization_name
5943 /* Bug fix 2655157 starts */
5944 /*    ,       decode (G_emp_org_oride,
5945                         'Y',
5946                         decode (organization_name,
5947                                         null,
5948                                         GetOrgName(employee_number, expenditure_item_date, person_business_group_name),
5949                                         organization_name),
5950                         decode (employee_number,
5951                                         null,
5952                                         organization_name,
5953                                         GetOrgName(employee_number, expenditure_item_date, person_business_group_name))
5954                    ) organization_name */
5955            /* decode( employee_number, NULL, organization_name,
5956             decode(G_emp_org_oride,'Y',organization_name,NULL))
5957                  organization_name */
5958 /* Bug fix 2655157 ends */
5959     ,       trunc(expenditure_item_date) expenditure_item_date
5960     ,       project_number
5961     ,       task_number
5962     ,       expenditure_type
5963     ,       non_labor_resource
5964     ,       non_labor_resource_org_name
5965     ,       quantity
5966     ,       raw_cost
5967     ,       raw_cost_rate
5968     ,       orig_transaction_reference
5969     ,       attribute_category
5970     ,       attribute1
5971     ,       attribute2
5972     ,       attribute3
5973     ,       attribute4
5974     ,       attribute5
5975     ,       attribute6
5976     ,       attribute7
5977     ,       attribute8
5978     ,       attribute9
5979     ,       attribute10
5980     ,       expenditure_comment
5981     ,       interface_id
5982     ,       expenditure_id
5983     ,       nvl(unmatched_negative_txn_flag, 'N') unmatched_negative_txn_flag
5984     -- REL12 AP Lines uptake.
5985     -- to_number(NULL) was removed.
5986     ,       expenditure_item_id
5987     ,       org_id             org_id
5988     ,       dr_code_combination_id
5989     ,       cr_code_combination_id
5990     ,       cdl_system_reference1
5991     ,       cdl_system_reference2
5992     ,       cdl_system_reference3
5993     ,       cdl_system_reference4
5994     ,       cdl_system_reference5
5995     ,       gl_date
5996     ,       burdened_cost
5997     ,       burdened_cost_rate
5998     ,       receipt_currency_amount
5999     ,       receipt_currency_code
6000     ,	    receipt_exchange_rate
6001     ,       denom_currency_code
6002     ,	    denom_raw_cost
6003     ,	    denom_burdened_cost
6004     ,	    acct_rate_date
6005     ,	    acct_rate_type
6006     ,       acct_exchange_rate
6007 --    ,       pa_currency.round_currency_amt1(acct_raw_cost) acct_raw_cost  -- Bug 7522080
6008     ,       acct_raw_cost
6009     ,       acct_burdened_cost
6010     ,       acct_exchange_rounding_limit
6011     ,       project_currency_code
6012     ,       project_rate_date
6013     ,       project_rate_type
6014     ,       project_exchange_rate
6015     ,       orig_exp_txn_reference1
6016     ,       orig_user_exp_txn_reference
6017     ,       vendor_number
6018     ,       orig_exp_txn_reference2
6019     ,       orig_exp_txn_reference3
6020     ,       override_to_organization_name
6021     ,       reversed_orig_txn_reference
6022     ,       billable_flag
6023     ,       txn_interface_id
6024     ,       person_business_group_name
6025     ,       projfunc_currency_code
6026     ,       projfunc_cost_rate_date
6027     ,       projfunc_cost_rate_type
6028     ,       projfunc_cost_exchange_rate
6029     ,       project_raw_cost
6030     ,       project_burdened_cost
6031     ,       assignment_name
6032     ,       work_type_name
6033     ,       nvl(accrual_flag,'N') accrual_flag
6034     ,       project_id
6035     ,       task_id
6036     ,       person_id
6037     ,       organization_id
6038     ,       non_labor_resource_org_id
6039     ,       vendor_id
6040     ,       override_to_organization_id
6041     ,       assignment_id
6042     ,       work_type_id
6043     ,       person_business_group_id
6044     ,       po_number  /* cwk */
6045     ,       po_header_id
6046     ,       po_line_num
6047     ,       po_line_id
6048     ,       person_type
6049     ,       po_price_type
6050     ,       wip_resource_id
6051     ,       inventory_item_id
6052     ,       unit_of_measure
6053     ,       adjusted_expenditure_item_id
6054     ,       NVL(fc_document_type, 'NOT' )
6055     ,       document_type
6056     ,       document_distribution_type
6057     ,       si_assets_addition_flag
6058     ,       adjusted_txn_interface_id
6059     ,       net_zero_adjustment_flag
6060     ,       sc_xfer_code   -- supplier cost transfer status code.
6061     ,       0              -- final_payment_id
6062  --   ,       agreement_id  --FSIO Changes
6063   --  ,       agreement_number
6064       FROM pa_transaction_interface
6065      WHERE transaction_source = X_transaction_source
6066        AND batch_name = current_batch
6067        AND transaction_status_code = 'P'
6068        AND system_linkage = curr_etype_class_code
6069     ORDER BY
6070            expenditure_ending_date DESC
6071     ,       decode(nvl(person_id,0), 0, employee_number, person_id)
6072             -- 5389130 added order by criteria.
6073     ,       decode(nvl(override_to_organization_id     ,organization_id), 0, organization_name,
6074                       organization_id, organization_id, override_to_organization_id)
6075     ,       orig_exp_txn_reference1
6076     ,       orig_user_exp_txn_reference
6077     ,       decode(nvl(vendor_id,0), 0, vendor_number, vendor_id)
6078     ,       orig_exp_txn_reference2
6079     ,       orig_exp_txn_reference3
6080     ,       denom_currency_code
6081     ,	    acct_rate_date
6082     ,	    acct_rate_type
6083     ,	    acct_exchange_rate
6084     ,       expenditure_item_date
6085             -- 5389130 added order by criteria.
6086     ,       decode(adjusted_expenditure_item_id, null, 1, 0, 1, 0)
6087     ,	    decode(curr_etype_class_code, 'VI', cdl_system_reference2, '0' )
6088     ,       decode(curr_etype_class_code, 'VI', cdl_system_reference3, '0' )
6089     ,       decode(curr_etype_class_code, 'VI', cdl_system_reference5, '0' )
6090     ,       decode(curr_etype_class_code, 'VI', cdl_system_reference4, '0' )
6091     ,       decode(fc_document_type ,'ALL', 1, 'CMT', 2, 'ACT', 3 , 4 )
6092     ,       decode(nvl(project_id,0), 0, project_number, project_id)
6093     ,       decode(nvl(task_id,0), 0, task_number, task_id)
6094     ,       accrual_flag
6095     ,       NVL(adjusted_txn_interface_id,0) ;
6096 
6097     --PA-K Changes: Removing the for update clause
6098     --FOR UPDATE OF transaction_status_code;
6099 
6100     --TrxRec		TrxRecs%ROWTYPE;
6101     X_Owner_name        Dba_Tables.Owner%TYPE; /* 1869684 */
6102 
6103   --PA-K Changes: Declaration of the tables used for TrxRecs Bulk Fetch
6104   --l_ExpendTab
6105   --l_Expend2Tab
6106   l_SysLinkTab            PA_PLSQL_DATATYPES.Char30TabTyp;
6107   l_EmpNumTab             PA_PLSQL_DATATYPES.Char30TabTyp;
6108   l_EiEndDateTab          PA_PLSQL_DATATYPES.DateTabTyp;
6109   l_OrganizationNameTab   PA_PLSQL_DATATYPES.Char240TabTyp;
6110   l_EiDateTab             PA_PLSQL_DATATYPES.DateTabTyp;
6111   l_PrjNumTab             PA_PLSQL_DATATYPES.Char25TabTyp;
6112   l_TaskNumTab            PA_PLSQL_DATATYPES.Char25TabTyp;
6113   l_ETypeTab              PA_PLSQL_DATATYPES.Char30TabTyp;
6114   l_NlrTab                PA_PLSQL_DATATYPES.Char20TabTyp;
6115   l_NlrOrgNameTab         PA_PLSQL_DATATYPES.Char240TabTyp;
6116   l_QtyTab                PA_PLSQL_DATATYPES.QtyTabtyp;
6117   l_rawCostTab            PA_PLSQL_DATATYPES.NumTabTyp; -- Bug 5959023: Changed to NumTabTyp from QtyTabtyp
6118   l_rawCostRateTab        PA_PLSQL_DATATYPES.NumTabTyp; -- Bug 5959023: Changed to NumTabTyp from AmtTabTyp
6119   l_OrigTxnRefTab         PA_PLSQL_DATATYPES.Char30TabTyp;
6120   l_AttCatTab             PA_PLSQL_DATATYPES.Char30TabTyp;
6121   l_Att1Tab               PA_PLSQL_DATATYPES.Char150TabTyp;
6122   l_Att2Tab               PA_PLSQL_DATATYPES.Char150TabTyp;
6123   l_Att3Tab               PA_PLSQL_DATATYPES.Char150TabTyp;
6124   l_Att4Tab               PA_PLSQL_DATATYPES.Char150TabTyp;
6125   l_Att5Tab               PA_PLSQL_DATATYPES.Char150TabTyp;
6126   l_Att6Tab               PA_PLSQL_DATATYPES.Char150TabTyp;
6127   l_Att7Tab               PA_PLSQL_DATATYPES.Char150TabTyp;
6128   l_Att8Tab               PA_PLSQL_DATATYPES.Char150TabTyp;
6129   l_Att9Tab               PA_PLSQL_DATATYPES.Char150TabTyp;
6130   l_Att10Tab              PA_PLSQL_DATATYPES.Char150TabTyp;
6131   l_ExpCommentTab         PA_PLSQL_DATATYPES.Char240TabTyp;
6132   l_InterfaceIdTab        PA_PLSQL_DATATYPES.Num15TabTyp;
6133   l_ExpIdTab              PA_PLSQL_DATATYPES.Num15TabTyp;
6134   l_UnMatchNegFlagTab     PA_PLSQL_DATATYPES.Char1TabTyp;
6135   l_ExpItemIdTab          PA_PLSQL_DATATYPES.Num15TabTyp;
6136   --l_JobIdTab              PA_PLSQL_DATATYPES.
6137   l_OrgIdTab              PA_PLSQL_DATATYPES.Num15TabTyp;
6138   l_DRCCIDTab             PA_PLSQL_DATATYPES.Num15TabTyp;
6139   l_CRCCIDTab             PA_PLSQL_DATATYPES.Num15TabTyp;
6140   l_SysRef1Tab            PA_PLSQL_DATATYPES.Char30TabTyp;
6141   l_SysRef2Tab            PA_PLSQL_DATATYPES.Char30TabTyp;
6142   l_SysRef3Tab            PA_PLSQL_DATATYPES.Char30TabTyp;
6143   l_Sysref4Tab            PA_PLSQL_DATATYPES.Char30TabTyp;
6144   TYPE  Char30TabTyp  IS TABLE OF VARCHAR2(30) index by varchar2(30) ;
6145   l_txn_rejected_sr4Tab   Char30TabTyp;
6146 
6147   /* REL12-AP Lines uptake  START
6148   ** Support for cdl_system_reference5
6149   ** cdl_system_reference5 to store invoice distribution ID
6150   */
6151   l_Sysref5Tab            PA_PLSQL_DATATYPES.IdTabTyp;
6152   l_txn_rejected_sr5tab   PA_PLSQL_DATATYPES.IdTabTyp;
6153   l_AdjExpItemIdTab       PA_PLSQL_DATATYPES.IdTabTyp;
6154   l_FcDocTypeTab          PA_PLSQL_DATATYPES.Char3TabTyp;
6155   l_AdjTxnInterfaceIDTab  PA_PLSQL_DATATYPES.IdTabTyp;
6156   l_NZAdjFlagTab          PA_PLSQL_DATATYPES.Char1TabTyp;
6157   l_AdjTxnEiIDTab         PA_PLSQL_DATATYPES.IdTabTyp;
6158   l_temp_adjItemID        NUMBER ;
6159   l_scXferCdTab           PA_PLSQL_DATATYPES.Char1TabTyp ;
6160 
6161     -- REL12 AP Lines Uptake
6162     -- Cash based accounting support
6163     --
6164   -- bug 4902112 declaration was changed from Char1TabTyp to IdTabTyp.
6165   --
6166   l_finalPaymentIdTab     PA_PLSQL_DATATYPES.IdTabTyp ;
6167 
6168   --REL12 Changes
6169   l_siaAddFlagTab         PA_PLSQL_DATATYPES.Char1TabTyp ;
6170   l_old_SysRef1		  varchar2(30) ;
6171   l_old_SysRef2		  varchar2(30) ;
6172   l_old_SysRef3		  varchar2(30) ;
6173   l_old_SysRef4		  varchar2(30) ;
6174   l_old_SysRef5	          NUMBER ;
6175   l_doc_header_id         NUMBER ;
6176   l_vendor_id             NUMBER ;
6177   l_doc_distribution_id   NUMBER ;
6178   l_doc_payment_id        NUMBER ;
6179   l_doc_line_number       NUMBER ;
6180   l_gms_fundscheck        varchar2(1) ;
6181   l_DocumentTypeTab       PA_PLSQL_DATATYPES.Char30TabTyp;
6182   l_DocumentDistTypeTab   PA_PLSQL_DATATYPES.Char30TabTyp;
6183 
6184   /* REL12-AP Lines uptake  END */
6185 
6186 
6187   l_GlDateTab             PA_PLSQL_DATATYPES.DateTabTyp;
6188   l_burdenedCostTab       PA_PLSQL_DATATYPES.NumTabTyp; -- Bug 5959023: Changed to NumTabTyp from AmtTabTyp
6189   l_BdCostRateTab         PA_PLSQL_DATATYPES.NumTabTyp; -- Bug 5959023: Changed to NumTabTyp from AmtTabTyp
6190   l_RcptCurrAmtTab        PA_PLSQL_DATATYPES.NumTabTyp;
6191   l_RcptCurrCodeTab       PA_PLSQL_DATATYPES.Char15TabTyp;
6192   l_RcptExchRateTab       PA_PLSQL_DATATYPES.NumTabTyp;
6193   l_DenomCurrCodeTab      PA_PLSQL_DATATYPES.Char15TabTyp;
6194   l_DenomRawCostTab       PA_PLSQL_DATATYPES.NumTabTyp;
6195   l_DenomBdCostTab        PA_PLSQL_DATATYPES.NumTabTyp;
6196   l_AcctRateDateTab       PA_PLSQL_DATATYPES.DateTabTyp;
6197   l_AcctRateTypeTab       PA_PLSQL_DATATYPES.Char30TabTyp;
6198   l_AcctExchRateTab       PA_PLSQL_DATATYPES.NumTabTyp;
6199   l_AcctRawCostTab        PA_PLSQL_DATATYPES.NumTabTyp;
6200   l_AcctBdCostTab         PA_PLSQL_DATATYPES.NumTabTyp;
6201   l_AcctExchRndLimitTab   PA_PLSQL_DATATYPES.NumTabTyp;
6202   l_ProjCurrCodeTab       PA_PLSQL_DATATYPES.Char15TabTyp;
6203   l_prjRateDateTab        PA_PLSQL_DATATYPES.DateTabTyp;
6204   l_PrjRateTypeTab        PA_PLSQL_DATATYPES.Char30TabTyp;
6205   l_PrjExchRateTab        PA_PLSQL_DATATYPES.NumTabTyp;
6206   l_OrigExpTxnRef1Tab     PA_PLSQL_DATATYPES.Char60TabTyp;
6207   l_OrigUsrExpTxnRefTab   PA_PLSQL_DATATYPES.Char60TabTyp;
6208   l_VendorNumTab          PA_PLSQL_DATATYPES.Char30TabTyp;
6209   l_OrigExpTxnRef2Tab     PA_PLSQL_DATATYPES.Char60TabTyp;
6210   l_OrigExpTxnRef3Tab     PA_PLSQL_DATATYPES.Char60TabTyp;
6211   l_OverrideOrgNameTab    PA_PLSQL_DATATYPES.Char240TabTyp;
6212   l_RevOrigTxnRefTab      PA_PLSQL_DATATYPES.Char30TabTyp;
6213   l_billableFlagTab       PA_PLSQL_DATATYPES.Char1TabTyp;
6214   l_TxnIfIdTab            PA_PLSQL_DATATYPES.Num15TabTyp;
6215   l_PerBusGrpNameTab      PA_PLSQL_DATATYPES.Char60TabTyp;
6216   l_ProjFuncCurrCodeTab   PA_PLSQL_DATATYPES.Char15TabTyp;
6217   l_PrjFuncRateDateTab    PA_PLSQL_DATATYPES.DateTabTyp;
6218   l_PrjFuncRateTypeTab    PA_PLSQL_DATATYPES.Char30TabTyp;
6219   l_PrjFuncExchRateTab    PA_PLSQL_DATATYPES.NumTabTyp;
6220   l_ProjRawCostTab        PA_PLSQL_DATATYPES.NumTabTyp;
6221   l_ProjBdCostTab         PA_PLSQL_DATATYPES.NumTabTyp;
6222   l_AsgnNameTab           PA_PLSQL_DATATYPES.Char80TabTyp;
6223   l_WorkTypeNameTab       PA_PLSQL_DATATYPES.Char80TabTyp;
6224   l_AccrualFlagTab        PA_PLSQL_DATATYPES.Char1TabTyp;
6225   l_PrjIdTab              PA_PLSQL_DATATYPES.IdTabTyp;
6226   l_TaskIdTab             PA_PLSQL_DATATYPES.IdTabTyp;
6227   l_PersonIdTab           PA_PLSQL_DATATYPES.IdTabTyp;
6228   l_OrganizationIdTab     PA_PLSQL_DATATYPES.IdTabTyp;
6229   l_NLROrgIDTab           PA_PLSQL_DATATYPES.IdTabTyp;
6230   l_VendorIdTab           PA_PLSQL_DATATYPES.IdTabTyp;
6231   l_OverrideOrgIdTab      PA_PLSQL_DATATYPES.IdTabTyp;
6232   l_AsgnIdTab             PA_PLSQL_DATATYPES.IdTabTyp;
6233   l_WorkTypeIdTab         PA_PLSQL_DATATYPES.IdTabTyp;
6234   l_PersonBusGrpIdTab     PA_PLSQL_DATATYPES.IdTabTyp;
6235   l_EmpOrgIdTab           PA_PLSQL_DATATYPES.IdTabTyp;
6236   l_EmpJobIdTab           PA_PLSQL_DATATYPES.IdTabTyp;
6237   l_po_numberTab          PA_PLSQL_DATATYPES.Char20TabTyp; /* cwk */
6238   l_po_header_idTab       PA_PLSQL_DATATYPES.IdTabTyp;
6239   l_po_line_numTab        PA_PLSQL_DATATYPES.NumTabTyp;
6240   l_po_line_idTab         PA_PLSQL_DATATYPES.IdTabTyp;
6241   l_person_typeTab        PA_PLSQL_DATATYPES.Char30TabTyp;
6242   l_po_price_typeTab      PA_PLSQL_DATATYPES.Char20TabTyp;
6243   l_wip_resource_idTab    PA_PLSQL_DATATYPES.IdTabTyp;
6244   l_inventory_item_idTab  PA_PLSQL_DATATYPES.IdTabTyp;
6245   l_unit_of_measureTab    PA_PLSQL_DATATYPES.Char30TabTyp;
6246 
6247   l_Batch_Size           NUMBER;
6248   l_CommitSizeRecCount   NUMBER;
6249   l_BatchRecCount        NUMBER;
6250   l_ActualBatchRecCnt    NUMBER;
6251 
6252   l_sys_link             VARCHAR2(30);
6253   l_expenditure_id       NUMBER;
6254   l_RevOrigTxnRef        VARCHAR2(30);
6255   l_Accrual_Flag         VARCHAR2(1);
6256 
6257   l_gms_enabled		 VARCHAR2(1); --> variable for holding gms_enabled flag.
6258 
6259   l_src_system_linkage_function   VARCHAR2(30); -- 4057874
6260 
6261  -- l_agreement_idTab       PA_PLSQL_DATATYPES.IdTabTyp;     --FSIO Changes
6262  -- l_agreement_numberTab   PA_PLSQL_DATATYPES.Char50TabTyp;
6263 
6264     FUNCTION lockCntrlRec ( trx_source   VARCHAR2
6265                           , batch        VARCHAR2
6266                           , etypeclasscode VARCHAR2 ) RETURN NUMBER
6267     IS
6268     BEGIN
6269 
6270       pa_cc_utils.set_curr_function('lockCntrlRec');
6271 
6272       IF PG_DEBUG = 'Y' THEN
6273          log_message('log_message: ' || 'Trying to get lock for record in xface ctrl:'||
6274                                 ' transaction source ='||trx_source||
6275                                 ' batch = '||batch||
6276                                 ' sys link = '||etypeclasscode);
6277       END IF;
6278 
6279       BEGIN
6280         IF PG_DEBUG = 'Y' THEN
6281            log_message('log_message: ' || 'Before locking record');
6282         END IF;
6283 
6284         SELECT
6285               1
6286         INTO
6287               dummy
6288         FROM
6289               pa_transaction_xface_control
6290         WHERE
6291               transaction_source = trx_source
6292          AND  batch_name = batch
6293          AND  system_linkage_function = etypeclasscode
6294          AND  status = 'PENDING'
6295         FOR UPDATE OF status NOWAIT;
6296 
6297         IF PG_DEBUG = 'Y' THEN
6298            log_message('log_message: ' || 'After locking record');
6299         END IF;
6300 
6301       EXCEPTION
6302         when no_data_found then
6303            IF PG_DEBUG = 'Y' THEN
6304               log_message('log_message: ' || 'Not able to Lock Record, in no data found');
6305            END IF;
6306            raise;
6307       END;
6308 
6309       IF PG_DEBUG = 'Y' THEN
6310          log_message('log_message: ' || 'Got lock for record');
6311       END IF;
6312 
6313       UPDATE  pa_transaction_xface_control
6314          SET
6315               interface_id = X_xface_id
6316       ,       status = 'IN_PROGRESS'
6317        WHERE
6318               transaction_source = trx_source
6319          AND  batch_name = batch
6320          AND  system_linkage_function = etypeclasscode
6321          AND  status = 'PENDING';
6322 
6323       IF PG_DEBUG = 'Y' THEN
6324          log_message('log_message: ' || 'Updated interface id/status on pa_transaction_xface_control');
6325       END IF;
6326 
6327       pa_cc_utils.reset_curr_function;
6328       RETURN 0;
6329 
6330     EXCEPTION
6331       WHEN  RESOURCE_BUSY  THEN
6332       IF PG_DEBUG = 'Y' THEN
6333          log_message('log_message: ' || 'Cannot get lock',1);
6334       END IF;
6335       pa_cc_utils.reset_curr_function;
6336           RETURN -1;
6337     END lockCntrlRec;
6338 
6339     PROCEDURE UpdControlProcessed (P_TrxSource in VARCHAR2,
6340                                    P_BatchName in VARCHAR2,
6341                                    P_XfaceId  in NUMBER,
6342                                    P_TxnCount  in NUMBER,
6343                                    P_ProcCount in NUMBER,
6344                                    P_BatchProcCount in NUMBER,
6345                                    p_system_linkage_function  IN  VARCHAR2)
6346 /* Added the parameter system_linkage_function for bug # 3291066 */
6347     IS
6348     BEGIN
6349 
6350       pa_cc_utils.set_curr_function('UpdControlProcessed');
6351       IF PG_DEBUG = 'Y' THEN
6352          log_message('log_message: ' || 'Inside UpdControlProcessed,
6353                                 P_TrxSource = ' || P_TrxSource ||
6354                               ' P_BatchName = ' || P_BatchName ||
6355                               ' P_Xface_Id = ' || P_XfaceId ||
6356                               ' P_TxnCount = ' || P_TxnCount ||
6357                               ' P_ProcCount = ' || P_ProcCount ||
6358                               ' P_BatchProcCount = '|| P_BatchProcCount);
6359       END IF;
6360 
6361       /*PA-K Changes: Replaced the existing update stmt */
6362 
6363       /* Bug#3451108. Added an 'AND' condition in the if clause to check if the
6364          P_BatchProcCount is not equal to Zero */
6365       If (((P_BatchProcCount + P_ProcCount) < P_TxnCount) And (P_BatchProcCount <> 0)) Then
6366 
6367          IF PG_DEBUG = 'Y' THEN
6368             log_message('log_message: ' || 'Set ctrl record status to PENDING');
6369          END IF;
6370 
6371          UPDATE pa_transaction_xface_control
6372             SET status = 'PENDING',
6373                 intermediate_flag = 'Y',
6374                 processed_count = processed_count + P_BatchProcCount
6375           WHERE transaction_source = P_TrxSource
6376             AND batch_name = P_BatchName
6377             AND interface_id = P_XfaceId
6378             AND system_linkage_function = p_system_linkage_function;
6379 /* Added the join with system_linkage_function for bug # 3291066 */
6380 
6381          IF PG_DEBUG = 'Y' THEN
6382             log_message('log_message: ' || 'Updated ' || SQL%ROWCOUNT || ' records');
6383          END IF;
6384 
6385       Else
6386 
6387          IF PG_DEBUG = 'Y' THEN
6388             log_message('log_message: ' || 'Set ctrl record status to PROCESSED');
6389          END IF;
6390 
6391          UPDATE pa_transaction_xface_control
6392             SET status = 'PROCESSED',
6393                 processed_count = processed_count + P_BatchProcCount
6394           WHERE transaction_source = P_TrxSource
6395             AND batch_name = P_BatchName
6396             AND interface_id = P_XfaceId
6397 	    AND system_linkage_function = p_system_linkage_function;
6398 /* Added the join with system_linkage_function for bug # 3291066 */
6399 
6400          IF PG_DEBUG = 'Y' THEN
6401             log_message('log_message: ' || 'Updated ' || SQL%ROWCOUNT || ' records');
6402          END IF;
6403 
6404       End If;
6405 
6406       pa_cc_utils.reset_curr_function;
6407 
6408     END UpdControlProcessed;
6409 
6410     PROCEDURE loadExpCompareVars ( expend    IN VARCHAR2
6411                                , expend2   IN VARCHAR2
6412                                , end_date  IN DATE
6413                                , orig_exp_txn_reference1 IN VARCHAR2
6414                                , orig_user_exp_txn_reference IN VARCHAR2
6415                                , orig_exp_txn_reference2 IN VARCHAR2
6416                                , orig_exp_txn_reference3 IN VARCHAR2)
6417     IS
6418     BEGIN
6419 
6420       pa_cc_utils.set_curr_function('loadExpCompareVars');
6421       current_expend  := expend;
6422       current_expend2 := expend2;
6423       current_period  := end_date;
6424       i               := 0;
6425 
6426       --  Trx_import enhancement
6427       G_orig_exp_txn_reference1 := orig_exp_txn_reference1;
6428       G_orig_user_exp_txn_reference := orig_user_exp_txn_reference;
6429       G_orig_exp_txn_reference2 := orig_exp_txn_reference2;
6430       G_orig_exp_txn_reference3 := orig_exp_txn_reference3;
6431 
6432       --  Current_system_linkage is added in init for creating expenditure groups
6433       --  Selva 03/10/97
6434 
6435       -- Bug 1000221, OT and ST txns will be created in the same exp group
6436       -- with the system linkage = ST.
6437 
6438       IF ( l_sys_link = 'OT' ) THEN
6439          current_system_linkage := 'ST';
6440       ELSE
6441          current_system_linkage := l_sys_link ;
6442       END IF;
6443 
6444       -- SST change: If transaction source is 'Oracle Self Service Time',
6445       -- then we do not need to get a new expenditure ID because for
6446       -- self service time, expenditure is created at entry time, thus
6447       -- expenditure ID already exists.
6448       IF X_transaction_source = 'Oracle Self Service Time' AND
6449           l_Expenditure_Id IS NOT NULL THEN
6450 
6451        G_expenditure_id := l_Expenditure_Id;
6452 
6453       ELSE
6454        SELECT pa_expenditures_s.nextval
6455        INTO G_expenditure_id
6456        FROM sys.dual;
6457       END IF;
6458 
6459       pa_cc_utils.reset_curr_function;
6460   END loadExpCompareVars;
6461 
6462   -- Bug 2048868 : Added 5 parameters to pass currency attributes to
6463   --               pa_transactions.insertexp
6464 
6465   PROCEDURE newExpend ( group_name   IN VARCHAR2
6466                       , ending_date  IN DATE
6467                       , record_count IN NUMBER
6468                       , p_denom_currency_code IN VARCHAR2   DEFAULT NULL
6469                       , p_acct_currency_code IN VARCHAR2   DEFAULT NULL
6470                       , p_acct_rate_type     IN VARCHAR2   DEFAULT NULL
6471                       , p_acct_rate_date     IN DATE   DEFAULT NULL
6472                       , p_acct_exchange_rate IN NUMBER DEFAULT NULL
6473 		      , p_person_type IN VARCHAR2 DEFAULT NULL /*cwk */
6474 		      , p_batch_name  IN VARCHAR2 )  -- Bug 3613784 : Performance fix
6475   IS
6476 
6477     X_status                VARCHAR2(30);
6478     X_outcome               NUMBER;
6479     X_route_to_person_id    NUMBER;
6480     X_comment               VARCHAR2(240);
6481     X_approval_status       VARCHAR2(80);
6482 
6483     l_acct_rate_type        VARCHAR2(30);
6484     l_acct_rate_date        DATE;
6485     l_acct_exchange_rate    NUMBER;
6486     l_gl_accted_flag          VARCHAR2(2);
6487 
6488   BEGIN
6489     pa_cc_utils.set_curr_function('newExpend');
6490 
6491     IF PG_DEBUG = 'Y' THEN
6492        log_message('log_message: ' || 'record count = ' || record_count);
6493     END IF;
6494 
6495     -- BUG:4748305  DFHC.D4:PRC INTERFACE SUPPLIER COST NOT INTERFACING
6496     -- VALID INVOICE DISTRIBUTIONS
6497     -- We allow good ap invoice distributions to interface in a expenditure
6498     -- and do not reject all the expenditure items in expenditure
6499     -- This is only done for VI items.
6500 
6501     IF ( NOT REJECT_EXP ) OR
6502         ( v_calling_module = 'PAAPIMP' and
6503           current_system_linkage = 'VI' and
6504           record_count > 0 )
6505     THEN
6506 
6507       IF ( NOT GROUP_CREATED ) THEN
6508 
6509         IF X_transaction_source NOT IN ('PTE TIME', 'PTE EXPENSE') THEN
6510 
6511           IF PG_DEBUG = 'Y' THEN
6512           pa_debug.G_err_Stage := 'Calling pa_transactions.InsertExpGroupNew';
6513              log_message('log_message: ' || pa_debug.G_err_Stage);
6514              log_message('log_message: ' || 'Group Name = ' || group_name || ' Sys Link = ' || current_system_linkage);
6515           END IF;
6516 
6517           IF (group_name <> nvl(G_group_name,'X')) THEN
6518 
6519                 pa_transactions.InsertExpGroupNew(
6520                    group_name
6521                 ,  'RELEASED'
6522                 ,  ending_date
6523                 ,  current_system_linkage
6524                 ,  X_userid
6525                 ,  X_transaction_source
6526                 ,  l_Accrual_Flag
6527                 -- MOAC Changes
6528                 ,  g_moac_org_id );
6529 
6530              G_group_name := group_name;
6531 
6532              GROUP_CREATED := TRUE;
6533 
6534           END IF;
6535 
6536         END IF;
6537 
6538       END IF;
6539 
6540       -- SST Change: expenditure was created when user entered
6541       -- the SST.  Thus, we just need to update the expenditure_group
6542       -- for the expenditure.
6543       IF ( X_transaction_source = 'Oracle Self Service Time' ) THEN
6544 
6545         IF PG_DEBUG = 'Y' THEN
6546         pa_debug.G_err_Stage := 'Updating exp for SST';
6547            log_message('log_message: ' || pa_debug.G_err_Stage);
6548         END IF;
6549 
6550         UPDATE pa_expenditures
6551         SET expenditure_group = group_name
6552         WHERE expenditure_id = G_expenditure_id;
6553 
6554       ELSE
6555         IF PG_DEBUG = 'Y' THEN
6556         pa_debug.G_err_Stage := 'Calling pa_transactions.InsertExp';
6557            log_message('log_message: ' || pa_debug.G_err_Stage);
6558         END IF;
6559         -- Trx_import enhancement
6560         -- Bug 2048868 : Added 5 parameters to pass currency attributes to
6561         --               pa_transactions.insertexp
6562         -- If system_linkage = ER
6563 
6564 
6565 
6566         IF ( (current_system_linkage = 'ER') and
6567             (p_denom_currency_code <> p_acct_currency_code) ) THEN
6568 
6569          if (p_acct_rate_type is null) then
6570            l_acct_rate_type := pa_multi_currency.get_rate_type;
6571          else
6572            l_acct_rate_type := p_acct_rate_type;
6573          end if;
6574 
6575          if (p_acct_rate_date is null) then
6576            pa_multi_currency_txn.get_acct_rate_date(
6577               P_EI_date        => ending_date,
6578               P_acct_rate_date => l_acct_rate_date);
6579          else
6580             l_acct_rate_date := p_acct_rate_date;
6581          end if;
6582 
6583          pa_transactions.InsertExp(
6584            X_expenditure_id   => G_expenditure_id,
6585            X_expend_status    => 'APPROVED',
6586            X_expend_ending    => ending_date,
6587            X_expend_class     => G_eclass,
6588            X_inc_by_person    => G_person_id,
6589            X_inc_by_org       => G_org_id,
6590            X_expend_group     => group_name,
6591   --         X_entered_by_id    => X_userid,
6592            X_entered_by_id    => FND_GLOBAL.employee_id, -- Bug 2396121
6593            X_created_by_id    => X_userid,
6594            X_denom_currency_code  =>  p_denom_currency_code,
6595            X_acct_currency_code   =>  p_acct_currency_code,
6596            X_acct_rate_type       =>  l_acct_rate_type,
6597            X_acct_rate_date       =>  l_acct_rate_date,
6598            X_acct_exchange_rate   =>  p_acct_exchange_rate,
6599            X_orig_exp_txn_reference1 => G_orig_exp_txn_reference1,
6600            X_orig_user_exp_txn_reference => G_orig_user_exp_txn_reference,
6601            X_vendor_id        => G_vendor_id,
6602            X_orig_exp_txn_reference2 => G_orig_exp_txn_reference2,
6603            X_orig_exp_txn_reference3 => G_orig_exp_txn_reference3,
6604   	   X_person_type => p_person_type,
6605           -- MOAC Changes
6606            p_org_id                => g_moac_org_id); /*cwk */
6607 
6608         ELSE
6609 
6610           pa_transactions.InsertExp(
6611            X_expenditure_id   => G_expenditure_id,
6612            X_expend_status    => 'APPROVED',
6613            X_expend_ending    => ending_date,
6614            X_expend_class     => G_eclass,
6615            X_inc_by_person    => G_person_id,
6616            X_inc_by_org       => G_org_id,
6617            X_expend_group     => group_name,
6618   --         X_entered_by_id    => X_userid,
6619            X_entered_by_id    => FND_GLOBAL.employee_id, -- Bug 2396121
6620            X_created_by_id    => X_userid,
6621            X_orig_exp_txn_reference1 => G_orig_exp_txn_reference1,
6622            X_orig_user_exp_txn_reference => G_orig_user_exp_txn_reference,
6623            X_vendor_id        => G_vendor_id,
6624            X_acct_currency_code   =>  p_acct_currency_code,        -- Bug 6412762: Base Bug 6354066
6625            X_orig_exp_txn_reference2 => G_orig_exp_txn_reference2,
6626            X_orig_exp_txn_reference3 => G_orig_exp_txn_reference3,
6627            X_person_type => p_person_type,
6628            -- MOAC Changes
6629            p_org_id      => g_moac_org_id); /* cwk */
6630 
6631 		END IF;
6632 
6633       END IF;
6634 
6635       IF PG_DEBUG = 'Y' THEN
6636       pa_debug.G_err_Stage := 'Calling pa_transactions.InsItems';
6637          log_message('log_message: ' || pa_debug.G_err_Stage);
6638       END IF;
6639 
6640       select decode(G_gl_accted_flag,'N','N',decode(G_gl_posted_flag,'Y','Y','P')) into l_gl_accted_flag from dual;
6641 
6642       pa_transactions.InsItems(
6643          X_userid
6644       ,  0
6645       ,  'PAXTRTRX'
6646       ,  'TRX_IMPORT'
6647       ,  record_count
6648       ,  temp_status
6649       ,  l_gl_accted_flag );
6650 
6651       -- -----------------------------------------------------------------------
6652       -- OGM_0.0 - Interface for creating new ADLS for each expenditure Item
6653       -- created. This will create award distribution lines only when OGM is
6654       -- installed for the ORG in process.
6655       -- The folowing procedure returns doing nothing if status is in ERROR for
6656       -- pa_transactions.InsItems.
6657       -- ------------------------------------------------------------------------
6658       IF PG_DEBUG = 'Y' THEN
6659          log_message('log_message: ' || '1760:Call Vertical APPS interface for i>=500');
6660       END IF;
6661 
6662       IF l_gms_enabled = 'Y' THEN
6663 
6664          PA_GMS_API.vert_trx_interface(   X_userid,
6665 					   0,
6666 					   'PAXTTRXB',
6667 					   'TRX_IMPORT',
6668 					   record_count,
6669 					   temp_status,
6670 					   G_gl_accted_flag) ;
6671 
6672          IF PG_DEBUG = 'Y' THEN
6673             log_message('log_message: ' || '1760:Call Vertical APPS interface for i>=500 END.');
6674          END IF;
6675 
6676          IF ( temp_status <  0 ) THEN
6677            error_msg := SQLERRM( temp_status);
6678            IF PG_DEBUG = 'Y' THEN
6679               log_message('log_message: ' || pa_debug.G_err_Stack,1);
6680               log_message('log_message: ' || error_msg,1);
6681               log_message('log_message: ' || pa_debug.G_err_Stage,1);
6682            END IF;
6683            raise_application_error( -20000, error_msg ) ;
6684          END IF;
6685       END IF; --> l_gms_enabled = 'Y'
6686 
6687       IF PG_DEBUG = 'Y' THEN
6688          pa_debug.G_err_stage := 'Updating txn interface status to I';
6689          log_message('log_message: ' || pa_debug.G_err_Stage);
6690       END IF;
6691 
6692     -- BUG:4748305  DFHC.D4:PRC INTERFACE SUPPLIER COST NOT INTERFACING
6693     -- VALID INVOICE DISTRIBUTIONS
6694     -- We allow good ap invoice distributions to interface in a expenditure
6695     -- and do not reject all the expenditure items in expenditure
6696     -- This is only done for VI items.
6697 
6698       UPDATE  pa_transaction_interface
6699               -- SST Change: If there's post import extension, then
6700               -- set status code to 'I' so the post import extension
6701               -- will pick it up, otherwise set to 'A' meaning it has
6702               -- completed the transfer process.  Post-extension will
6703               -- eventually set the status code to 'A' as well if the
6704               -- the transaction went through post extension
6705               -- successfully.
6706          SET
6707               transaction_status_code = decode(G_post_processing_extn,NULL,'A',
6708                                         'I')
6709        WHERE
6710               expenditure_id = G_expenditure_id
6711          -- Bug 3613784 : Performance fix
6712 	 AND  batch_name = P_batch_name
6713          AND transaction_rejection_code is NULL; -- Bug 3613784 : Performance fix
6714 
6715       IF PG_DEBUG = 'Y' THEN
6716       pa_debug.G_err_stage := 'Updated ' || SQL%ROWCOUNT || ' txn interface for accepted expenditures';
6717          log_message('log_message: ' || pa_debug.G_err_Stage);
6718       END IF;
6719 
6720     ELSE  --IF ( NOT REJECT_EXP ) THEN
6721 
6722       IF PG_DEBUG = 'Y' THEN
6723       pa_debug.G_err_stage := 'Updating txn interface for rejected expenditures';
6724          log_message('log_message: ' || pa_debug.G_err_Stage);
6725       END IF;
6726       UPDATE  pa_transaction_interface
6727          SET
6728               transaction_status_code = 'R' ,
6729               expenditure_item_id = NULL,
6730               -- SST Change: For rejected expenditure, we need
6731               -- to reset interface table's reversed_orig_txn_reference
6732               -- field.  This field is updated after calling validateitem
6733               -- for each transaction.  While processing an entire
6734               -- expenditure, some transaction may pass validateitem API
6735               -- and get their reversed_orig_txn_reference field modified;
6736               -- Thus, at the end of each expenditure, we need to go back
6737               -- to reset the reversed_orig_txn_reference field to the
6738               -- original value when the expenditure is rejected.
6739               reversed_orig_txn_reference = l_RevOrigTxnRef
6740        WHERE
6741               expenditure_id = G_expenditure_id
6742 	 AND  batch_name = P_batch_name ; -- Bug 3613784 : Performance fix
6743 
6744       IF PG_DEBUG = 'Y' THEN
6745       pa_debug.G_err_stage := 'Updated ' || SQL%ROWCOUNT || ' txn interface for rejected expenditures';
6746          log_message('log_message: ' || pa_debug.G_err_Stage);
6747       END IF;
6748     END IF;
6749 
6750     IF PG_DEBUG = 'Y' THEN
6751     pa_debug.G_err_stage := 'Calling pa_transactions.FlushEiTabs';
6752        log_message('log_message: ' || pa_debug.G_err_Stage);
6753     END IF;
6754     pa_transactions.FlushEiTabs;
6755 
6756     -- Bug 752915, added the following to flush the adjustments ei pl/sql table
6757     --
6758     pa_adjustments.ExpAdjItemTab := pa_utils.EmptyIdTab;
6759 
6760 	/***** Bug 4106188 CWK Changes *****/
6761 --	release_po_line_task_lock;
6762 --	init_po_amt_chk;
6763 	/***** Bug 4106188 CWK Changes *****/
6764 
6765 
6766     REJECT_EXP := FALSE;
6767     pa_cc_utils.reset_curr_function;
6768   END newExpend;
6769 
6770   PROCEDURE InitPLSQLTab IS
6771   BEGIN
6772     --l_ExpendTab
6773     --l_Expend2Tab
6774     l_SysLinkTab.Delete;
6775     l_EmpNumTab.Delete;
6776     l_EiEndDateTab.Delete;
6777     l_OrganizationNameTab.Delete;
6778     l_EiDateTab.Delete;
6779     l_PrjNumTab.Delete;
6780     l_TaskNumTab.Delete;
6781     l_ETypeTab.Delete;
6782     l_NlrTab.Delete;
6783     l_NlrOrgNameTab.Delete;
6784     l_QtyTab.Delete;
6785     l_rawCostTab.Delete;
6786     l_rawCostRateTab.Delete;
6787     l_OrigTxnRefTab.Delete;
6788     l_AttCatTab.Delete;
6789     l_Att1Tab.Delete;
6790     l_Att2Tab.Delete;
6791     l_Att3Tab.Delete;
6792     l_Att4Tab.Delete;
6793     l_Att5Tab.Delete;
6794     l_Att6Tab.Delete;
6795     l_Att7Tab.Delete;
6796     l_Att8Tab.Delete;
6797     l_Att9Tab.Delete;
6798     l_Att10Tab.Delete;
6799     l_ExpCommentTab.Delete;
6800     l_InterfaceIdTab.Delete;
6801     l_ExpIdTab.Delete;
6802     l_UnMatchNegFlagTab.Delete;
6803     l_ExpItemIdTab.Delete;
6804     --l_JobIdTab.Delete;
6805     l_OrgIdTab.Delete;
6806     l_DRCCIDTab.Delete;
6807     l_CRCCIDTab.Delete;
6808     l_SysRef1Tab.Delete;
6809     l_SysRef2Tab.Delete;
6810     l_SysRef3Tab.Delete;
6811     l_Sysref4Tab.Delete;
6812     l_GlDateTab.Delete;
6813     l_burdenedCostTab.Delete;
6814     l_BdCostRateTab.Delete;
6815     l_RcptCurrAmtTab.Delete;
6816     l_RcptCurrCodeTab.Delete;
6817     l_RcptExchRateTab.Delete;
6818     l_DenomCurrCodeTab.Delete;
6819     l_DenomRawCostTab.Delete;
6820     l_DenomBdCostTab.Delete;
6821     l_AcctRateDateTab.Delete;
6822     l_AcctRateTypeTab.Delete;
6823     l_AcctExchRateTab.Delete;
6824     l_AcctRawCostTab.Delete;
6825     l_AcctBdCostTab.Delete;
6826     l_AcctExchRndLimitTab.Delete;
6827     l_ProjCurrCodeTab.Delete;
6828     l_prjRateDateTab.Delete;
6829     l_PrjRateTypeTab.Delete;
6830     l_PrjExchRateTab.Delete;
6831     l_OrigExpTxnRef1Tab.Delete;
6832     l_OrigUsrExpTxnRefTab.Delete;
6833     l_VendorNumTab.Delete;
6834     l_OrigExpTxnRef2Tab.Delete;
6835     l_OrigExpTxnRef3Tab.Delete;
6836     l_OverrideOrgNameTab.Delete;
6837     l_RevOrigTxnRefTab.Delete;
6838     l_billableFlagTab.Delete;
6839     l_TxnIfIdTab.Delete;
6840     l_PerBusGrpNameTab.Delete;
6841     l_ProjFuncCurrCodeTab.Delete;
6842     l_PrjFuncRateDateTab.Delete;
6843     l_PrjFuncRateTypeTab.Delete;
6844     l_PrjFuncExchRateTab.Delete;
6845     l_ProjRawCostTab.Delete;
6846     l_ProjBdCostTab.Delete;
6847     l_AsgnNameTab.Delete;
6848     l_WorkTypeNameTab.Delete;
6849     l_AccrualFlagTab.Delete;
6850     l_PrjIdTab.Delete;
6851     l_TaskIdTab.Delete;
6852     l_PersonIdTab.Delete;
6853     l_OrganizationIdTab.Delete;
6854     l_NLROrgIDTab.Delete;
6855     l_VendorIdTab.Delete;
6856     l_OverrideOrgIdTab.Delete;
6857     l_AsgnIdTab.Delete;
6858     l_WorkTypeIdTab.Delete;
6859     l_PersonBusGrpIdTab.Delete;
6860     l_EmpOrgIdTab.Delete;
6861     l_EmpJobIdTab.Delete;
6862     l_po_numberTab.Delete; /* cwk */
6863     l_po_header_idTab.Delete;
6864     l_po_line_numTab.Delete;
6865     l_po_line_idTab.Delete;
6866     l_person_typeTab.Delete;
6867     l_po_price_typeTab.Delete;
6868     l_wip_resource_idTab.Delete;
6869     l_inventory_item_idTab.Delete;
6870     l_unit_of_measureTab.Delete;
6871 
6872     /* REL12-AP Lines uptake  START
6873     ** Support for Related items and cdl_system_reference5
6874     ** cdl_system_reference3 to store invoice distribution ID
6875     */
6876     l_SysRef5Tab.Delete ;
6877     l_AdjExpItemIdTab.Delete ;
6878     l_FcDocTypeTab.Delete ;
6879     l_DocumentTypeTab.DELETE ;
6880     l_DocumentDistTypeTab.DELETE ;
6881     l_siaAddFlagTab.DELETE ;
6882     l_adjTxnInterfaceIDTab.delete ;
6883     l_NZAdjFlagTab.delete ;
6884     l_AdjTxnEiIDTab.delete ;
6885     l_scXferCdTab.delete ;
6886     -- REL12 AP Lines Uptake
6887     -- Cash based accounting support
6888     --
6889     l_finalPaymentIdTab.delete ;
6890     l_txn_rejected_sr5tab.DELETE ;
6891     l_txn_rejected_sr4tab.DELETE ;
6892 
6893     /* REL12-AP Lines uptake  END   */
6894 
6895    -- l_agreement_idTab.DELETE ;    --FSIO Changes
6896    -- l_agreement_numberTab.DELETE ;
6897 
6898         init_po_amt_chk;
6899 
6900   END InitPLSQLTab;
6901 
6902   --
6903   -- Bug:5064930
6904   -- Reject the net zero reversals if pa adjustments have rejected
6905   -- logic description:
6906   --    supplier cost reversal transaction interface reverse the project
6907   --    adjustments corresponding to the source transactions.
6908   --    Supplier cost reversed transactions do not get interface if
6909   --    project adjustment reversal fails.
6910   --    Following function stamp the supplier cost reversal with the same rejection code as
6911   --    of the corresponding project adjustments that has rejected before in the same batch
6912   --
6913   --
6914   FUNCTION get_parent_txn_status( p_status varchar2, p_indx number ) return varchar2 is
6915      l_status         varchar2(50) ;
6916      l_parent_dist_id  number ;
6917      l_rejected_txn_id number ;
6918 
6919      cursor c_prepay is
6920 	select reversed_prepay_app_dist_id
6921 	  from ap_prepay_app_dists
6922          where prepay_app_dist_id =  NVL(l_SysRef4Tab(p_indx),0)  ;
6923 
6924      cursor c_payment is
6925 	select reversal_inv_pmt_id
6926 	  from AP_Invoice_Payments
6927          where Invoice_Payment_Id =  NVL(l_SysRef4Tab(p_indx),0)
6928 	   and nvl(reversal_flag,'N')  = 'Y' ;
6929 
6930      cursor c_invdist is
6931 	select parent_reversal_id
6932 	  into l_parent_dist_id
6933 	  from ap_invoice_distributions_all apd
6934          where invoice_distribution_id =  NVL(l_SysRef5Tab(p_indx),0)
6935 	   and nvl(reversal_flag,'N')  = 'Y' ;
6936 
6937      cursor c_rcvtxn is
6938 	select parent_transaction_id
6939 	  from rcv_transactions
6940 	 where transaction_id = NVL(l_SysRef4Tab(p_indx),0) ;
6941 
6942      cursor c_status_cd is
6943         select  transaction_rejection_code
6944           from  pa_transaction_interface_all
6945          where  txn_interface_id = l_rejected_txn_id
6946            and  transaction_status_code  = 'R'  ;
6947   BEGIN
6948       l_status := p_status ;
6949 
6950       IF l_status is not null                  OR
6951          NVL(l_nzAdjFlagTab(p_indx), 'N')  <> 'Y'   OR
6952          v_calling_module <> 'PAAPIMP'  THEN
6953 
6954           RETURN l_status ;
6955       end if ;
6956       pa_cc_utils.set_curr_function('get_parent_txn_status');
6957 
6958       IF PG_DEBUG = 'Y' THEN
6959          log_message('log_message: X_transaction_source :' || X_transaction_source);
6960          log_message('log_message: G_cash_based_accounting :' || G_cash_based_accounting);
6961          log_message('log_message: l_DocumentDistTypeTab :' || l_DocumentDistTypeTab(p_indx));
6962          log_message('log_message: l_SysRef5Tab(p_indx) :' ||  l_SysRef5Tab(p_indx));
6963          log_message('log_message: l_SysRef4Tab(p_indx) :' ||  l_SysRef4Tab(p_indx));
6964       END IF;
6965 
6966       l_parent_dist_id := NULL ;
6967 
6968 
6969       IF  X_transaction_source in ( 'AP EXPENSE', 'AP INVOICE', 'AP NRTAX' , 'AP ERV',
6970                                   'INTERCOMPANY_AP_INVOICES', 'INTERPROJECT_AP_INVOICES', 'AP VARIANCE' ) THEN
6971       -- {
6972 	 IF  G_cash_based_accounting = 'Y' THEN
6973 	 -- {{{
6974 	    IF l_DocumentDistTypeTab(p_indx) = 'PREPAY' THEN
6975 	       open c_prepay ;
6976 	       fetch c_prepay into l_parent_dist_id ;
6977 	       close c_prepay ;
6978 	    ELSE
6979 	       open c_payment ;
6980 	       fetch c_payment into l_parent_dist_id ;
6981 	       close c_payment ;
6982 	    END IF ;
6983 
6984             IF  l_txn_rejected_sr4tab.EXISTS(l_parent_dist_id) THEN
6985                 l_rejected_txn_id  := l_txn_rejected_sr4tab(l_parent_dist_id)  ;
6986 	    END IF ;
6987          -- }}}
6988 	 ELSE
6989 	 -- {{{
6990 	    open c_invdist ;
6991 	    fetch c_invdist into l_parent_dist_id ;
6992 	    close c_invdist ;
6993 
6994             IF  l_txn_rejected_sr5tab.EXISTS(l_parent_dist_id) THEN
6995 	        l_rejected_txn_id  := l_txn_rejected_sr5tab(l_parent_dist_id)  ;
6996 	    END IF ;
6997 
6998 	 END IF ;
6999 	 --}}}
7000       END IF ; -- }
7001 
7002       IF X_transaction_source in ( 'PO RECEIPT', 'PO RECEIPT NRTAX',
7003                                    'PO RECEIPT PRICE ADJ', 'PO RECEIPT NRTAX PRICE ADJ' )  THEN
7004       --{
7005 	  open c_rcvtxn ;
7006 	  fetch c_rcvtxn into l_parent_dist_id ;
7007 	  close c_rcvtxn ;
7008 
7009           IF  l_txn_rejected_sr4tab.EXISTS(l_parent_dist_id) THEN
7010               l_rejected_txn_id  := l_txn_rejected_sr4tab(l_parent_dist_id)  ;
7011 	  END IF ;
7012       END IF ; --}
7013 
7014       IF X_transaction_source in (  'AP DISCOUNTS') THEN
7015       -- {
7016             open c_payment ;
7017 	    fetch c_payment into l_parent_dist_id ;
7018 	    close c_payment ;
7019 
7020             IF  l_txn_rejected_sr4tab.EXISTS(l_parent_dist_id) THEN
7021                 l_rejected_txn_id  := l_txn_rejected_sr4tab(l_parent_dist_id)  ;
7022 	    END IF ;
7023 
7024       END IF ; --}
7025 
7026       IF PG_DEBUG = 'Y' THEN
7027          log_message('log_message: l_parent_dist_id :' || l_parent_dist_id);
7028          log_message('log_message: l_rejected_txn_id :' || l_rejected_txn_id);
7029       END IF;
7030 
7031       IF l_rejected_txn_id is not null then
7032          open c_status_cd ;
7033 	 fetch c_status_cd into l_status ;
7034 	 close c_status_cd ;
7035       END IF ;
7036 
7037       IF PG_DEBUG = 'Y' THEN
7038          log_message('log_message: l_status :' || l_status);
7039       END IF;
7040 
7041       -- 5235363   R12.PJ:XB4:DEV:APL:EXP ITEM DATE VALIDATIONS FOR SUPPLIER COST.
7042       --
7043       IF l_status in ( 'GMS_EXP_ITEM_DT_BEFORE_AWD_ST' ,'GMS_EXP_ITEM_DT_AFTER_AWD_END' ,
7044                        'GMS_AWARD_IS_CLOSED' , 'EXP_TYPE_INACTIVE' , 'ETYPE_SLINK_INACTIVE' ,
7045 		       'PA_EX_PROJECT_DATE' , 'PA_EXP_TASK_EFF' , 'PA_EXP_ORG_NOT_ACTIVE' , 'NO_ASSIGNMENT' ) THEN
7046 	 --
7047 	 -- l_status      := 'Unable to process this record because at least one record for the same
7048 	 -- distributions has been rejected.'
7049 	 --
7050 	 l_status         := 'PA_REJECTED_PARENT_RECORD' ;
7051          IF PG_DEBUG = 'Y' THEN
7052             log_message('log_message: Status Override for child (Date Failure) l_status :' || l_status);
7053          END IF;
7054        END IF ;
7055 
7056       pa_cc_utils.reset_curr_function;
7057 
7058       return l_status ;
7059   END get_parent_txn_status ;
7060   -- =========================================
7061   BEGIN
7062 
7063       -- Interface supplier invoices from AP and Web expenses
7064       -- Interface Web expenses from AP calls import1 program
7065       -- instead of Import, since the debug log messages are
7066       -- enabled only in import prog, log messages are not
7067       -- written out for the Interface programs from AP.
7068       -- The following two lines should take care of this problem.
7069 
7070       If G_debug_mode is NULL Then
7071          fnd_profile.get('PA_DEBUG_MODE',G_debug_mode);
7072          G_debug_mode := NVL(G_debug_mode, 'N');
7073 
7074          pa_debug.set_process(x_process => 'PLSQL',
7075                               x_debug_mode => G_debug_mode);
7076       End If;
7077 
7078       pa_cc_utils.log_message('Debug Mode = '||G_debug_mode,1);
7079 
7080       g_request_id := fnd_global.conc_request_id;
7081       pa_cc_utils.set_curr_function('Import1');
7082       IF PG_DEBUG = 'Y' THEN
7083          log_message('log_message: ' || 'Start Transaction Import');
7084       END IF;
7085 
7086       /* Bug 2451653 commented FND_STATS.Gather_Table_Stats
7087          Please see earlier versions for the code
7088       */
7089 
7090       G_user := X_userid;
7091 
7092       IF PG_DEBUG = 'Y' THEN
7093          log_message('log_message: ' || 'G_trx_link = '|| G_trx_link);
7094       END IF;
7095 
7096       IF (G_trx_link is null) Then
7097          IF PG_DEBUG = 'Y' THEN
7098          pa_debug.G_err_Stage := 'Retriving Transaction_source';
7099             log_message('log_message: ' || pa_debug.G_err_Stage);
7100          END IF;
7101          GetTrxSrcInfo( X_transaction_source );
7102       End If;
7103 
7104       l_Batch_Size := G_Batch_Size;
7105       IF PG_DEBUG = 'Y' THEN
7106          log_message('log_message: ' || 'Initial l_Batch_Size = ' || l_Batch_Size);
7107       END IF;
7108 
7109       IF PG_DEBUG = 'Y' THEN
7110       pa_debug.G_err_stage := 'Retriving Currency Info from Imp options';
7111          log_message('log_message: ' || pa_debug.G_err_stage);
7112       END IF;
7113 
7114       IF (G_accounting_currency_code IS NULL) THEN
7115          GetImpCurrInfo;
7116       END IF;
7117 
7118       IF g_moac_org_id is NULL then
7119          g_moac_org_id := pa_moac_utils.get_current_org_id ;
7120       END IF ;
7121 
7122       -- Bug 987539: Initialize v_calling_module according
7123       -- to transaction source.  Basically this variable is
7124       -- used when calling validateitem API to let this API
7125       -- know who is calling it.  By initializing v_calling_module
7126       -- accordingly, we can let validateitem know that the item
7127       -- we're validating comes from AP, thus validation is
7128       -- is not necessary because AP does validation at entry time.
7129       IF (X_transaction_source in ( 'AP EXPENSE', 'AP INVOICE', 'AP NRTAX', 'AP DISCOUNTS', 'INTERCOMPANY_AP_INVOICES',
7130                                     'INTERPROJECT_AP_INVOICES', 'AP VARIANCE', 'AP ERV', 'PO RECEIPT', 'PO RECEIPT NRTAX',
7131 				    'PO RECEIPT PRICE ADJ', 'PO RECEIPT NRTAX PRICE ADJ')) THEN
7132 				    -- pricing changes
7133            --added for 1503237
7134     	   v_calling_module := 'PAAPIMP';
7135       ELSE
7136            v_calling_module := 'PAXTRTRX';
7137       END IF;
7138 
7139       IF PG_DEBUG = 'Y' THEN
7140          log_message('log_message: ' || 'Module:'||v_calling_module);
7141       END IF;
7142       IF PG_DEBUG = 'Y' THEN
7143       pa_debug.G_err_stage := 'Open and Fetch transaction batches';
7144          log_message('log_message: ' || pa_debug.G_err_stage);
7145       END IF;
7146 
7147       /* Check if gms is enabled and keep the status in a variable */
7148 
7149       l_gms_enabled := gms_pa_costing_pkg.grants_implemented;
7150 
7151      /* if NVL(fnd_profile.value('PA_DEBUG_MODE'), 'N') = 'Y' then --FSIO Change
7152         G_FSIO_ENABLED := 'Y';
7153       else
7154         G_FSIO_ENABLED := 'N';
7155       end if; */
7156 
7157       Open TrxBatches;
7158 
7159       l_CommitSizeRecCount := 0;
7160 
7161       <<batches>>
7162       Loop    ---{
7163 
7164         IF PG_DEBUG = 'Y' THEN
7165         pa_debug.G_err_Stage := 'Start TrxBatches loop';
7166            log_message('log_message: ' || pa_debug.G_err_stage);
7167         END IF;
7168 
7169         Fetch TrxBatches INTO TrxBatch;
7170 
7171         If TrxBatches%ROWCOUNT = 0 and G_Exit_Main is null Then
7172            IF PG_DEBUG = 'Y' THEN
7173               log_message('log_message: ' || pa_debug.G_err_stage,1);
7174            END IF;
7175            G_Exit_Main := TRUE;
7176            Exit batches;
7177         Elsif TrxBatches%NOTFOUND Then
7178            IF PG_DEBUG = 'Y' THEN
7179            pa_debug.G_err_Stage := 'TrxBatches notfound, exit trxbatches';
7180               log_message('log_message: ' || pa_debug.G_err_stage,1);
7181            END IF;
7182            G_Exit_Main := TRUE; --Bug 7307479
7183            Exit batches;
7184         End If;
7185 
7186         GROUP_CREATED := FALSE;
7187         --G_group_name := TrxBatch.exp_group_name;
7188 
7189         If l_Batch_Size = 0 Then
7190 
7191              SELECT sum(xc.transaction_count)
7192                INTO l_Batch_Size
7193                FROM pa_transaction_xface_control xc
7194               WHERE xc.transaction_source = TrxBatch.transaction_source
7195                 AND xc.status             = 'PENDING';
7196 
7197              IF PG_DEBUG = 'Y' THEN
7198                 log_message('log_message: ' || 'l_Batch_Size is zero,set it to sum of transaction_count = '||l_Batch_Size);
7199              END IF;
7200 
7201         End If;
7202 
7203         IF PG_DEBUG = 'Y' THEN
7204 		   pa_debug.G_err_Stage := 'Locking xface ctrl record';
7205            log_message('log_message: ' || pa_debug.G_err_stage||
7206                          'Transaction source = '||TrxBatch.transaction_source
7207                          ||' batch= '||TrxBatch.batch_name||' sys link= '||
7208                          TrxBatch.system_linkage_function);
7209         END IF;
7210 
7211         dummy := lockCntrlRec( TrxBatch.transaction_source
7212                            , TrxBatch.batch_name
7213                            , TrxBatch.system_linkage_function );
7214 
7215         IF ( dummy = 0 ) THEN   ---{
7216 
7217           IF PG_DEBUG = 'Y' THEN
7218              log_message('log_message: ' || 'Final l_Batch_Size = '|| l_Batch_Size);
7219           END IF;
7220 
7221            IF (l_CommitSizeRecCount <> l_Batch_Size) THEN    ---{
7222 
7223              IF PG_DEBUG = 'Y' THEN
7224 		pa_debug.G_err_Stage := 'Open cursor trxrecs';
7225                 log_message('log_message: ' ||  pa_debug.G_err_Stage);
7226                 log_message('log_message: ' || 'source = '||TrxBatch.transaction_source ||
7227                                      ' , batch = ' || TrxBatch.batch_name ||
7228                                      ' sys link = ' || TrxBatch.system_linkage_function);
7229 	      END IF;
7230 
7231              /** Bug#3026218 There are only two cursors, as per the changes done
7232 		     *** and hence open them based on Sys Link Fn
7233 		     **/
7234 
7235 			If (TrxBatch.system_linkage_function = 'ST') Then
7236                    OPEN TrxRecs1( TrxBatch.transaction_source
7237                                , TrxBatch.batch_name
7238                                , TrxBatch.system_linkage_function  );
7239              Else
7240                    OPEN TrxRecs2( TrxBatch.transaction_source
7241                                , TrxBatch.batch_name
7242                                , TrxBatch.system_linkage_function  );
7243              End If;
7244 
7245              FIRST_RECORD := TRUE;
7246 
7247              IF PG_DEBUG = 'Y' THEN
7248                 log_message('log_message: ' ||  'Start Trxrec loop',1);
7249              END IF;
7250 
7251              --Initialize PL/SQL tables
7252              InitPlSqlTab;
7253 
7254              If l_Batch_Size = 0 Then
7255                 l_BatchRecCount := TrxBatch.transaction_count;
7256              Else
7257                 l_BatchRecCount := l_Batch_Size - l_CommitSizeRecCount;
7258              End If;
7259 
7260              IF PG_DEBUG = 'Y' THEN
7261                 log_message('log_message: ' || 'Batch = '||l_BatchRecCount);
7262                 log_message('log_message: ' ||  'Fetch bulk from Trxrec');
7263              END IF;
7264 
7265 	     -- REL12 AP Lines Uptake
7266 	     -- Determine cash Based accounting flag
7267 
7268 	     SELECT NVL(glb.sla_ledger_cash_basis_flag,'N')
7269 	       INTO G_cash_based_accounting
7270 	       FROM gl_sets_of_books glb, pa_implementations_all pai
7271 	      WHERE glb.set_of_books_id = pai.set_of_books_id
7272 	        AND pai.org_id          = g_moac_org_id ;
7273 
7274              /** Bug#3026218 There will be only two fetches - from TrxRecs1 and TRxRecs2
7275              *** Removed the code that is based on Pre-defined flag
7276 	     **/
7277 
7278              If (TrxBatch.system_linkage_function = 'ST') Then --{
7279 
7280                    FETCH TrxRecs1 bulk collect into
7281                          l_SysLinkTab
7282                        , l_EiEndDateTab
7283                        , l_EmpNumTab
7284                        , l_OrganizationNameTab
7285                        , l_EiDateTab
7286                        , l_PrjNumTab
7287                        , l_TaskNumTab
7288                        , l_ETypeTab
7289                        , l_NlrTab
7290                        , l_NlrOrgNameTab
7291                        , l_QtyTab
7292                        , l_rawCostTab
7293                        , l_rawCostRateTab
7294                        , l_OrigTxnRefTab
7295                        , l_AttCatTab
7296                        , l_Att1Tab
7297                        , l_Att2Tab
7298                        , l_Att3Tab
7299                        , l_Att4Tab
7300                        , l_Att5Tab
7301                        , l_Att6Tab
7302                        , l_Att7Tab
7303                        , l_Att8Tab
7304                        , l_Att9Tab
7305                        , l_Att10Tab
7306                        , l_ExpCommentTab
7307                        , l_InterfaceIdTab
7308                        , l_ExpIdTab
7309                        , l_UnMatchNegFlagTab
7310                        , l_ExpItemIdTab
7311                      --, l_JobIdTab
7312                        , l_OrgIdTab
7313                        , l_DRCCIDTab
7314                        , l_CRCCIDTab
7315                        , l_SysRef1Tab
7316                        , l_SysRef2Tab
7317                        , l_SysRef3Tab
7318                        , l_Sysref4Tab
7319                        , l_Sysref5Tab
7320                        , l_GlDateTab
7321                        , l_burdenedCostTab
7322                        , l_BdCostRateTab
7323                        , l_RcptCurrAmtTab
7324                        , l_RcptCurrCodeTab
7325                        , l_RcptExchRateTab
7326                        , l_DenomCurrCodeTab
7327                        , l_DenomRawCostTab
7328                        , l_DenomBdCostTab
7329                        , l_AcctRateDateTab
7330                        , l_AcctRateTypeTab
7331                        , l_AcctExchRateTab
7332                        , l_AcctRawCostTab
7333                        , l_AcctBdCostTab
7334                        , l_AcctExchRndLimitTab
7335                        , l_ProjCurrCodeTab
7336                        , l_prjRateDateTab
7337                        , l_PrjRateTypeTab
7338                        , l_PrjExchRateTab
7339                        , l_OrigExpTxnRef1Tab
7340                        , l_OrigUsrExpTxnRefTab
7341                        , l_VendorNumTab
7342                        , l_OrigExpTxnRef2Tab
7343                        , l_OrigExpTxnRef3Tab
7344                        , l_OverrideOrgNameTab
7345                        , l_RevOrigTxnRefTab
7346                        , l_billableFlagTab
7347                        , l_TxnIfIdTab
7348                        , l_PerBusGrpNameTab
7349                        , l_ProjFuncCurrCodeTab
7350                        , l_PrjFuncRateDateTab
7351                        , l_PrjFuncRateTypeTab
7352                        , l_PrjFuncExchRateTab
7353                        , l_ProjRawCostTab
7354                        , l_ProjBdCostTab
7355                        , l_AsgnNameTab
7356                        , l_WorkTypeNameTab
7357                        , l_AccrualFlagTab
7358                        , l_PrjIdTab
7359                        , l_TaskIdTab
7360                        , l_PersonIdTab
7361                        , l_OrganizationIdTab
7362                        , l_NLROrgIDTab
7363                        , l_VendorIdTab
7364                        , l_OverrideOrgIdTab
7365                        , l_AsgnIdTab
7366                        , l_WorkTypeIdTab
7367                        , l_PersonBusGrpIdTab
7368 		       ,  l_po_numberTab /* cwk */
7369 		       ,  l_po_header_idTab
7370 		       ,  l_po_line_numTab
7371 		       ,  l_po_line_idTab
7372 		       ,  l_person_typeTab
7373 		       ,  l_po_price_typeTab
7374 		       ,  l_wip_resource_idTab
7375 		       ,  l_inventory_item_idTab
7376 		       ,  l_unit_of_measureTab
7377 		       ,  l_AdjExpItemIdTab
7378 		       ,  l_FcDocTypeTab
7379 		       ,  l_DocumentTypeTab
7380 		       ,  l_DocumentDistTypeTab
7381                        ,  l_siaAddFlagTab
7382 		       ,  l_AdjTxnInterfaceIdTab
7383 		       ,  l_nzAdjFlagTab
7384 		       ,  l_scXferCdTab
7385 		       ,  l_finalPaymentIdTab
7386                       -- ,  l_agreement_idTab     --FSIO Changes
7387                       -- ,  l_agreement_numberTab
7388                     LIMIT l_BatchRecCount;
7389 
7390              Else
7391 
7392                    FETCH TrxRecs2 bulk collect into
7393                          l_SysLinkTab
7394                        , l_EiEndDateTab
7395                        , l_EmpNumTab
7396                        , l_OrganizationNameTab
7397                        , l_EiDateTab
7398                        , l_PrjNumTab
7399                        , l_TaskNumTab
7400                        , l_ETypeTab
7401                        , l_NlrTab
7402                        , l_NlrOrgNameTab
7403                        , l_QtyTab
7404                        , l_rawCostTab
7405                        , l_rawCostRateTab
7406                        , l_OrigTxnRefTab
7407                        , l_AttCatTab
7408                        , l_Att1Tab
7409                        , l_Att2Tab
7410                        , l_Att3Tab
7411                        , l_Att4Tab
7412                        , l_Att5Tab
7413                        , l_Att6Tab
7414                        , l_Att7Tab
7415                        , l_Att8Tab
7416                        , l_Att9Tab
7417                        , l_Att10Tab
7418                        , l_ExpCommentTab
7419                        , l_InterfaceIdTab
7420                        , l_ExpIdTab
7421                        , l_UnMatchNegFlagTab
7422                        , l_ExpItemIdTab
7423                      --, l_JobIdTab
7424                        , l_OrgIdTab
7425                        , l_DRCCIDTab
7426                        , l_CRCCIDTab
7427                        , l_SysRef1Tab
7428                        , l_SysRef2Tab
7429                        , l_SysRef3Tab
7430                        , l_Sysref4Tab
7431                        , l_Sysref5Tab
7432                        , l_GlDateTab
7433                        , l_burdenedCostTab
7434                        , l_BdCostRateTab
7435                        , l_RcptCurrAmtTab
7436                        , l_RcptCurrCodeTab
7437                        , l_RcptExchRateTab
7438                        , l_DenomCurrCodeTab
7439                        , l_DenomRawCostTab
7440                        , l_DenomBdCostTab
7441                        , l_AcctRateDateTab
7442                        , l_AcctRateTypeTab
7443                        , l_AcctExchRateTab
7444                        , l_AcctRawCostTab
7445                        , l_AcctBdCostTab
7446                        , l_AcctExchRndLimitTab
7447                        , l_ProjCurrCodeTab
7448                        , l_prjRateDateTab
7449                        , l_PrjRateTypeTab
7450                        , l_PrjExchRateTab
7451                        , l_OrigExpTxnRef1Tab
7452                        , l_OrigUsrExpTxnRefTab
7453                        , l_VendorNumTab
7454                        , l_OrigExpTxnRef2Tab
7455                        , l_OrigExpTxnRef3Tab
7456                        , l_OverrideOrgNameTab
7457                        , l_RevOrigTxnRefTab
7458                        , l_billableFlagTab
7459                        , l_TxnIfIdTab
7460                        , l_PerBusGrpNameTab
7461                        , l_ProjFuncCurrCodeTab
7462                        , l_PrjFuncRateDateTab
7463                        , l_PrjFuncRateTypeTab
7464                        , l_PrjFuncExchRateTab
7465                        , l_ProjRawCostTab
7466                        , l_ProjBdCostTab
7467                        , l_AsgnNameTab
7468                        , l_WorkTypeNameTab
7469                        , l_AccrualFlagTab
7470                        , l_PrjIdTab
7471                        , l_TaskIdTab
7472                        , l_PersonIdTab
7473                        , l_OrganizationIdTab
7474                        , l_NLROrgIDTab
7475                        , l_VendorIdTab
7476                        , l_OverrideOrgIdTab
7477                        , l_AsgnIdTab
7478                        , l_WorkTypeIdTab
7479                        , l_PersonBusGrpIdTab
7480 		       ,  l_po_numberTab /* cwk */
7481 		       ,  l_po_header_idTab
7482 		       ,  l_po_line_numTab
7483 		       ,  l_po_line_idTab
7484 		       ,  l_person_typeTab
7485 		       ,  l_po_price_typeTab
7486 		       ,  l_wip_resource_idTab
7487 		       ,  l_inventory_item_idTab
7488 		       ,  l_unit_of_measureTab
7489 		       ,  l_AdjExpItemIdTab
7490 		       ,  l_FcDocTypeTab
7491 		       ,  l_DocumentTypeTab
7492 		       ,  l_DocumentDistTypeTab
7493                        ,  l_siaAddFlagTab
7494 		       ,  l_AdjTxnInterfaceIdTab
7495 		       ,  l_nzAdjFlagTab
7496 		       ,  l_scXferCdTab
7497 		       ,  l_finalPaymentIdTab
7498                      --  ,  l_agreement_idTab     --FSIO Changes
7499                       -- ,  l_agreement_numberTab
7500                     LIMIT l_BatchRecCount;
7501 
7502              End If;  --} /* TrxBatch.system_linkage_function = 'ST' */
7503 
7504 
7505              IF PG_DEBUG = 'Y' THEN
7506              pa_debug.g_err_stage := 'Log: No. of records fetched = '||l_TxnIfIDTab.count;
7507                 log_message('log_message: ' || pa_debug.G_err_stage);
7508              END IF;
7509              l_ActualBatchRecCnt := l_TxnIfIDTab.count;
7510 
7511 
7512 	     /* Bug#3451108 - Added code - Start */
7513 			 IF l_TxnIfIDTab.count = 0 THEN
7514 
7515                 IF PG_DEBUG = 'Y' THEN
7516                   log_message('log_message: ' || 'Fetch did not get any records, exit batches');
7517                 END IF;
7518 
7519 		        UpdControlProcessed( P_TrxSource      => TrxBatch.transaction_source,
7520                              P_BatchName      => TrxBatch.batch_name,
7521                              P_XfaceId        => X_xface_id,
7522                              P_TxnCount       => TrxBatch.transaction_count,
7523                              P_ProcCount      => TrxBatch.processed_count,
7524                              P_BatchProcCount => l_ActualBatchRecCnt,
7525                              P_System_Linkage_Function  => TrxBatch.system_linkage_function);
7526 
7527                 Exit batches;
7528               END IF; /* l_TxnIfIDTab.count = 0  */
7529 	     /* Bug#3451108 - Added code - End */
7530 
7531              FOR Z in 1..l_TxnIfIDTab.count LOOP  ---{        /* cwk */
7532 
7533              IF TrxBatch.system_linkage_function in ('ST','OT','VI','ER') THEN
7534                     null;
7535              ELSE
7536 		         l_po_numberTab(z) := null;
7537 		         l_po_header_idTab(z) := null;
7538 		         l_po_line_numTab(z) := null;
7539 		         l_po_line_idTab(z) := null;
7540                    l_po_price_typeTab(z) := null;
7541               END IF;
7542 
7543               END LOOP;   ---}
7544 
7545             --
7546             -- Bug : 4962731
7547             --     : R12.PJ:XB1:QA:BC:INCORRECT AMOUNTS INTERFACED TO PROJECTS IN CASH BASED ACC
7548             --
7549             IF G_cash_based_accounting = 'Y' and
7550 	       X_transaction_source in ( 'AP EXPENSE', 'AP INVOICE', 'AP NRTAX' , 'AP ERV',
7551 	                                 'INTERCOMPANY_AP_INVOICES', 'INTERPROJECT_AP_INVOICES', 'AP VARIANCE' ) THEN
7552                G_Profile_Discount_Start_date := fnd_date.canonical_to_date(PAAPIMP_PKG.return_profile_discount_date);
7553 	       G_Discount_Method             := PAAPIMP_PKG.return_discount_method;
7554 	    END IF ;
7555 
7556              FOR J in 1..l_TxnIfIDTab.count LOOP  ---{
7557 
7558                  -- REL12 : AP Lines uptake
7559 		 -- We only support net zero transactions for supplier invoice interface
7560 		 --
7561 	         IF v_calling_module <> 'PAAPIMP' THEN
7562 		    l_adjTxnInterfaceIDTab(j) := NULL ;
7563 		    l_NZAdjFlagTab(j)         := NULL ;
7564 		    -- supplier cost transfer status code is used only for supplier cost
7565 		    -- interface process.
7566 		    -- The value is ignored for all other transaction source
7567 		    l_scXfercdTab(j)          := NULL ;
7568                  ELSE
7569                     -- REL12 AP Lines Uptake
7570                     -- Cash based accounting support
7571                     --
7572                     IF G_cash_based_accounting = 'Y' and
7573 	               X_transaction_source in ( 'AP EXPENSE', 'AP INVOICE', 'AP NRTAX' , 'AP ERV',
7574 	                                         'INTERCOMPANY_AP_INVOICES', 'INTERPROJECT_AP_INVOICES', 'AP VARIANCE' ) THEN
7575 
7576                        l_invoice_id          := NVL(l_SysRef2Tab(j) ,0) ;
7577 		       l_payment_status_flag := 'N' ;
7578 
7579 		       -- R12 AP lines uptake : Prepayment changes
7580 		       -- For R12 prepayments applications , final payment logic is not required.Hence introduced below If condition
7581 
7582 		       l_finalPaymentIdTab(j) := 0 ;
7583 
7584                        IF l_DocumentDistTypeTab(j) <> 'PREPAY' THEN -- R12 AP lines uptake : Prepayment changes
7585 
7586 			       open c_final_payment ;
7587 			       fetch c_final_payment into l_payment_status_flag ;
7588 			       close c_final_payment ;
7589 
7590 			       IF NVL(l_payment_status_flag,'N')  = 'Y' THEN
7591 				  SELECT max(Invoice_Payment_Id)
7592 				    into l_finalPaymentIdTab(j)
7593 				    from ap_payment_hist_dists Paydist
7594 				    where NVL(paydist.pay_dist_lookup_code, 'CASH') = 'CASH'
7595 				      and Paydist.invoice_distribution_id           = NVL(l_SysRef5Tab(j) ,0)  ;
7596 			       END IF ; --  l_payment_status_flag = 'Y'
7597 
7598                        END IF;  --IF l_DocumentDistTypeTab(j) <> 'PREPAY' THEN
7599 	             END IF ; -- G_cash_based_accounting = 'Y'
7600 
7601                      IF PG_DEBUG = 'Y' THEN
7602                         log_message('log_message: l_invoice_id :' || l_invoice_id);
7603                         log_message('log_message: l_payment_status_flag :' || l_payment_status_flag);
7604                         log_message('log_message: NVL(l_SysRef5Tab(j) ,0) :' || NVL(l_SysRef5Tab(j) ,0));
7605                         log_message('log_message: l_finalPaymentIdTab(j) :' || l_finalPaymentIdTab(j));
7606                      END IF;
7607 		 END IF ;
7608 
7609                  IF PG_DEBUG = 'Y' THEN
7610                     log_message('log_message: ' || 'Inside TrxRec Loop ' || l_CommitSizeRecCount || 'Txn If = ' || l_TxnIfIDTab(j));
7611                  END IF;
7612 
7613                  l_CommitSizeRecCount := l_CommitSizeRecCount + 1;
7614 
7615                  -- Funds check related change
7616                  g_cdl_system_reference2 := to_number(TRIM(l_SysRef2Tab(j))); -- Bug 3704356
7617                  g_cdl_system_reference3 := to_number(TRIM(l_SysRef3Tab(j))); -- Bug 3704356
7618 		 g_cdl_system_reference4 := l_SysRef4Tab(j); -- 2339216; Commented for bug 4281765
7619                  -- REL12 AP Lines Uptake
7620                  -- Cash based accounting support
7621                  --
7622 		 g_finalPaymentId        := l_finalPaymentIdTab(j) ;
7623 
7624 		 /* REL12-AP Lines uptake */
7625 		 g_cdl_system_reference5 := to_number(TRIM(l_SysRef5Tab(j))); -- 2339216
7626                  -- End Funds check related change
7627 
7628                  IF PG_DEBUG = 'Y' THEN
7629                     log_message('log_message: g_cdl_system_reference2 :' || g_cdl_system_reference2);
7630                     log_message('log_message: g_cdl_system_reference3 :' || g_cdl_system_reference3);
7631                     log_message('log_message: g_cdl_system_reference4 :' || g_cdl_system_reference4);
7632                     log_message('log_message: g_cdl_system_reference5 :' || g_cdl_system_reference5);
7633                     log_message('log_message: g_finalPaymentId :' || g_finalPaymentId);
7634                  END IF;
7635 
7636                  -- SST Changes
7637                  -- Initilize reversed orig txn reference
7638                  G_reversed_orig_txn_reference := l_RevOrigTxnRefTab(j);
7639 
7640                  l_sys_link             := l_SysLinkTab(j);
7641                  l_expenditure_id       := l_ExpIdTab(j);
7642                  l_RevOrigTxnRef        := l_RevOrigTxnRefTab(j);
7643                  l_Accrual_Flag         := l_AccrualFlagTab(j);
7644 
7645 		 /* Fix for bug 2686544: Getting organization_name based on G_emp_org_oride flag
7646 		    here instead of inside the TrxRec cursors */
7647                  /* Bug#3026218 For user-defined sources and predefined source with no Ids use
7648 		    the Organization_Name */
7649 /**                 If ( (G_trx_predef_flag = 'N') OR
7650 ***                      (G_trx_predef_flag = 'Y' and l_PrjIdTab(j) is null) ) Then
7651 **/
7652                 IF  l_PrjIdTab(j) IS NULL THEN
7653                     If (nvl(G_emp_org_oride,'N') = 'Y') Then  --Bug 2719674 added nvl
7654                         If (l_OrganizationNameTab(j) is null) Then
7655 			        l_OrganizationNameTab(j) := GetOrgName(l_EmpNumTab(j),
7656 				                                       l_EiDateTab(j),
7657 								       l_PerBusGrpNameTab(j),
7658 								       l_person_typeTab(j)); /*cwk */
7659                         End If;
7660                     Else
7661 			If (l_EmpNumTab(j) is not null) Then
7662 			        l_OrganizationNameTab(j) := GetOrgName(l_EmpNumTab(j),
7663 				                                       l_EiDateTab(j),
7664 								       l_PerBusGrpNameTab(j),
7665 								       l_person_typeTab(j)); /*cwk */
7666                         End If;
7667                     End If;
7668                  End If;
7669 		 /* bug 2686544 */
7670 
7671                    IF PG_DEBUG = 'Y' THEN
7672                       log_message('log_message: ' || 'Before selecting expend grouping attributes for transaction sources');
7673                    END IF;
7674 
7675                    If (nvl(G_emp_org_oride,'N') = 'N' and l_PersonIdTab(j) is not NULL) Then /* bug#2719674 added nvl */
7676 
7677                        IF PG_DEBUG = 'Y' THEN
7678                        pa_debug.G_err_stage := 'Calling pa_utils.GetEmpOrgJobId';
7679                           log_message('log_message: ' || pa_debug.G_err_Stage);
7680                        END IF;
7681                        pa_utils.GetEmpOrgJobId( l_PersonIdTab(j),
7682                                                 l_EiDateTab(j),
7683                                                 l_EmpOrgIdTab(j),
7684                                                 l_EmpJobIdTab(j) );
7685 
7686                          /* Bug 6857130: For Reversal trxns from OTL, stamping back the organization_id that was passed and
7687                              discarding the rederived value in the above call*/
7688                           if (X_transaction_source = 'ORACLE TIME AND LABOR' and
7689                               l_UnMatchNegFlagTab(j) = 'N' and l_OrganizationIdTab(j) is not null and l_RevOrigTxnRefTab(j) is not null) then
7690 
7691                               IF PG_DEBUG = 'Y' THEN
7692                                    log_message('log_message: ' || 'Restamping the organization_id back to the value passed from OTL');
7693                               END IF;
7694 
7695                               l_EmpOrgIdTab(j) := l_OrganizationIdTab(j);
7696                           end if;
7697                           /* Bug 6857130 End */
7698 
7699 
7700                        IF PG_DEBUG = 'Y' THEN
7701                        pa_debug.G_err_stage := 'l_EmpOrgIdTab = ' || l_EmpOrgIdTab(j) ||
7702                                                ' l_EmpJobIdTab = ' || l_EmpJobIdTab(j) ;
7703                           log_message('log_message: ' || pa_debug.G_err_Stage);
7704                        END IF;
7705 
7706                    Else
7707                        l_EmpOrgIdTab(j) := NULL;
7708                        l_EmpJobIdTab(j) := NULL;
7709 
7710                    End If;
7711 
7712                    SELECT
7713                      to_char(trunc(l_EiEndDateTab(j)), 'J')||':'||
7714                      nvl(decode(nvl(l_PersonIdTab(j),0), 0, l_EmpNumTab(j), l_PersonIdTab(j)),
7715                          '-DUMMY EMP-')||':'||
7716   		     --removing the logic for getting organization name for getting l_expend1
7717 		     --as it has already been done above for 2686544. So we can just use l_OrganizationNameTab(j)
7718                      --nvl(decode(G_emp_org_oride,'Y',
7719                      --           decode(nvl(l_OrganizationIdTab(j),0), 0, l_OrganizationNameTab(j),l_OrganizationIdTab(j)),
7720                      --           decode(nvl(l_PersonIdTab(j),0),0,
7721                      --                  decode(l_EmpNumTab(j), null,l_OrganizationNameTab(j),
7722                      --                         GetOrgName(l_EmpNumTab(j),l_EiDateTab(j), l_PerBusGrpNameTab(j)))
7723                      --                ,l_EmpOrgIdTab(j))),
7724 		     nvl(decode(nvl(l_OrganizationIdTab(j),0), 0, l_OrganizationNameTab(j), nvl(l_EmpOrgIdTab(j), l_OrganizationIdTab(j)))
7725                          ,'-DUMMY ORG-')||':'||
7726                      nvl(l_OrigExpTxnRef1Tab(j), '-DUMMY EXP_TXN_REF1-') || ':' ||
7727                      nvl(l_OrigUsrExpTxnRefTab(j), '-DUMMY USER_EXP_TXN_REF-') || ':' ||
7728                      nvl(decode(nvl(l_VendorIdTab(j),0),0,l_VendorNumTab(j),l_VendorIdTab(j)),
7729                          '-DUMMY VENDOR_NUMBER-') || ':' ||
7730                      nvl(l_OrigExpTxnRef2Tab(j), '-DUMMY EXP_TXN_REF2-') || ':' ||
7731                      nvl(l_OrigExpTxnRef3Tab(j), '-DUMMY EXP_TXN_REF3-') || ':' ||
7732                      nvl(l_AccrualFlagTab(j),'-DUMMY ACC_FLAG-')|| ':' ||
7733 		     nvl(l_person_typeTab(j),'EMP') /* cwk */
7734                    , decode(l_SysLinkTab(j),'OT','ST',l_SysLinkTab(j)) || ':' ||
7735                      decode(l_SysLinkTab(j),'ER', nvl(l_DenomCurrCodeTab(j),'-DUMMY CODE-'),
7736                                            'VI', nvl(l_DenomCurrCodeTab(j),'-DUMMY CODE-'),
7737                                            '-DUMMY CODE-')||':'||
7738                      decode(l_SysLinkTab(j),'ER', nvl(to_char(l_AcctRateDateTab(j),'MMDDYYYY'),'-DUMMY DATE-'),
7739                                            'VI', nvl(to_char(l_AcctRateDateTab(j),'MMDDYYYY'),'-DUMMY DATE-'),
7740                                            '-DUMMY DATE-')||':'||
7741                      decode(l_SysLinkTab(j),'ER', nvl(l_AcctRateTypeTab(j),'-DUMMY TYPE-'),
7742                                            'VI', nvl(l_AcctRateTypeTab(j),'-DUMMY TYPE-'),
7743                                            '-DUMMY TYPE-')||':'||
7744                      decode(l_SysLinkTab(j),'ER', nvl(to_char(l_AcctExchRateTab(j)),'-DUMMY RATE-'),
7745                                            'VI', nvl(to_char(l_AcctExchRateTab(j)),'-DUMMY RATE-'),
7746                                            '-DUMMY RATE-')
7747                    Into l_Expend1, l_Expend2
7748                    From Dual;
7749 
7750                  IF ( FIRST_RECORD ) THEN
7751                    IF PG_DEBUG = 'Y' THEN
7752                    pa_debug.G_err_stage := 'Calling loadExpCompareVars';
7753                       log_message('log_message: ' || 'First Record Fetched');
7754                       log_message('log_message: ' || pa_debug.G_err_stage);
7755                    END IF;
7756 
7757                    loadExpCompareVars( l_Expend1
7758                               , l_Expend2
7759                               , l_EiEndDateTab(j)
7760                               , l_OrigExpTxnRef1Tab(j)
7761                               , l_OrigUsrExpTxnRefTab(j)
7762                               , l_OrigExpTxnRef2Tab(j)
7763                               , l_OrigExpTxnRef3Tab(j));
7764                    FIRST_RECORD := FALSE;
7765 
7766                  /* Bug#2374725 Commented the following ELSIF statement and added a new condition */
7767                  /* ELSIF ( current_expend <> l_Expend1 OR current_expend2 <> l_Expend2) THEN */
7768                  ELSIF (x_transaction_source <>  'Oracle Self Service Time' and
7769                         (current_expend <> l_Expend1 OR current_expend2 <> l_Expend2)) OR
7770                        (x_transaction_source = 'Oracle Self Service Time' and
7771                         l_ExpIdTab(j) <> G_expenditure_id) OR
7772 		       (x_transaction_source = 'ORACLE TIME AND LABOR' and  --Bug#4049401
7773                         l_EmpOrgIdTab(j) <> G_org_id) THEN
7774 
7775                    IF PG_DEBUG = 'Y' THEN
7776                    pa_debug.G_err_stage := 'Calling newExpend when expenditure changes';
7777                       log_message('log_message: ' || pa_debug.G_err_stage);
7778                       log_message('log_message: ' || 'Record Count = ' || i);
7779                    END IF;
7780 
7781                     newExpend( TrxBatch.exp_group_name
7782                      , current_period
7783                      , i
7784                      , prev_denom_currency_code
7785                      , G_accounting_currency_code
7786                      , prev_acct_rate_type
7787                      , prev_acct_rate_date
7788                      , prev_acct_exchange_rate
7789                      , prev_person_type /* cwk */
7790 					 , TrxBatch.batch_name ); -- Bug 3613784 : Performance fix
7791 
7792                    IF PG_DEBUG = 'Y' THEN
7793                    pa_debug.G_err_stage := 'CAlling loadExpCompareVars';
7794                       log_message('log_message: ' || pa_debug.G_err_stage);
7795                    END IF;
7796 
7797                    loadExpCompareVars( l_Expend1
7798                               , l_Expend2
7799                               , l_EiEndDateTab(j)
7800                               , l_OrigExpTxnRef1Tab(j)
7801                               , l_OrigUsrExpTxnRefTab(j)
7802                               , l_OrigExpTxnRef2Tab(j)
7803                               , l_OrigExpTxnRef3Tab(j));
7804                  END IF;
7805 
7806                  IF PG_DEBUG = 'Y' THEN
7807                  pa_debug.G_err_stage := 'CAlling ValidateOrgId';
7808                     log_message('log_message: ' || pa_debug.G_err_stage);
7809                  END IF;
7810 
7811                  ValidateOrgId(l_OrgIdTab(j),X_org_status );
7812 
7813 				 IF ( X_org_status IS NOT NULL) THEN
7814      	   	     -- Org id is null. Update status.
7815 
7816                      X_status := X_org_status;
7817 
7818                  ELSE -- org id is not null. continue with other validations
7819 
7820                     -- REL12 AP Lines uptake.
7821 		    -- Variance record created for CWK are only for funds check purpose
7822 		    -- and expenditure item will not be created.
7823 		    -- Variance in case of accrue on receipt and amount based PO
7824 		    -- will not create exp items.
7825                     IF l_ExpItemIdTab(j) = 0 THEN
7826                        X_ei_id := NULL ;
7827 		       -- null value of expenditure item id would indicate that expenditure item
7828 		       -- will not be created.
7829                     ELSE
7830                        X_ei_id := pa_utils.GetNextEiId;
7831 
7832 		       -- REL12 : AP Lines uptake
7833 		       -- Support net zero adjusted transactions for the supplier invoice interface.
7834 		       --
7835 		       IF NVL(l_nzAdjFlagTab(j), 'N')  = 'Y' and
7836 		          l_adjTxnInterfaceIdTab(j) is NULL THEN
7837 
7838 			  l_adjTxnEiIDTab(l_txnIfIdTab(j)) := X_ei_id ;
7839 
7840                           IF PG_DEBUG = 'Y' THEN
7841                              pa_debug.G_err_stage := 'AP Net Zero adjusted expenditure item id:'||X_ei_id;
7842                              log_message('log_message: ' || pa_debug.G_err_stage);
7843                              pa_debug.G_err_stage := 'TXN Interface ID:'||l_txnIfIdTab(j);
7844                              log_message('log_message: ' || pa_debug.G_err_stage);
7845                              pa_debug.G_err_stage := 'l_adjTxnEiIDTab(l_txnIfIdTab(j)):'||l_adjTxnEiIDTab(l_txnIfIdTab(j));
7846                              log_message('log_message: ' || pa_debug.G_err_stage);
7847                           END IF;
7848 
7849                        END IF ;
7850 
7851 		    END IF ;
7852 
7853                     IF PG_DEBUG = 'Y' THEN
7854                        log_message('log_message: l_ExpItemIdTab(j) :' || l_ExpItemIdTab(j));
7855                        log_message('log_message: NVL(l_nzAdjFlagTab(j), N) :' || NVL(l_nzAdjFlagTab(j), 'N'));
7856                        log_message('log_message: l_adjTxnInterfaceIdTab(j) :' || l_adjTxnInterfaceIdTab(j));
7857 		    END IF ;
7858 
7859                     IF PG_DEBUG = 'Y' THEN
7860 		       pa_debug.G_err_stage := 'CAlling ValidateItem';
7861                        log_message('log_message: ' || pa_debug.G_err_stage);
7862                     END IF;
7863 		    x_status := NULL ;
7864 
7865                     If (X_transaction_source = 'ORACLE TIME AND LABOR') Then
7866 
7867                        ValidateItemOTL(
7868                          X_trx_src                => X_transaction_source
7869                       ,  X_ei_date                => l_EiDateTab(j)
7870                       ,  X_etype                  => l_ETypeTab(j)
7871                       ,  X_nlr                    => l_NlrTab(j)
7872                       ,  X_qty                    => l_QtyTab(j)
7873                       ,  X_denom_raw_cost         => l_DenomRawCostTab(j)
7874                       ,  X_module                 => v_calling_module
7875                       ,  X_trx_ref                => l_OrigTxnRefTab(j)
7876                       ,  X_match_flag             => l_UnMatchNegFlagTab(j)
7877                       ,  X_att_cat                => l_AttCatTab(j)
7878                       ,  X_att1                   => l_Att1Tab(j)
7879                       ,  X_att2                   => l_Att2Tab(j)
7880                       ,  X_att3                   => l_Att3Tab(j)
7881                       ,  X_att4                   => l_Att4Tab(j)
7882                       ,  X_att5                   => l_Att5Tab(j)
7883                       ,  X_att6                   => l_Att6Tab(j)
7884                       ,  X_att7                   => l_Att7Tab(j)
7885                       ,  X_att8                   => l_Att8Tab(j)
7886                       ,  X_att9                   => l_Att9Tab(j)
7887                       ,  X_att10                  => l_Att10Tab(j)
7888                       ,  X_system_linkage         => l_SysLinkTab(j)
7889                       ,  X_status                 => X_status
7890                       --,  X_bill_flag              => X_billable_flag
7891                       ,  X_denom_currency_code    => l_DenomCurrCodeTab(j)
7892                       ,  X_acct_rate_date         => l_AcctRateDateTab(j)
7893                       ,  X_acct_rate_type         => l_AcctRateTypeTab(j)
7894                       ,  X_acct_exchange_rate     => l_AcctRateTypeTab(j)
7895                       ,  X_project_currency_code  => l_ProjCurrCodeTab(j)
7896                       ,  X_Projfunc_currency_code => l_ProjFuncCurrCodeTab(j)
7897                       ,  X_Projfunc_cost_rate_date =>l_PrjFuncRateDateTab(j)
7898                       ,  X_Projfunc_cost_rate_type => l_PrjFuncRateTypeTab(j)
7899                       ,  X_Projfunc_cost_exchange_rate => l_PrjFuncExchRateTab(j)
7900                       ,  X_Assignment_Name        => l_AsgnNameTab(j)
7901                       ,  X_Work_Type_Name         => l_WorkTypeNameTab(j)
7902                       ,  P_project_id             => l_PrjIdTab(j)
7903                       ,  P_task_id                => l_TaskIdTab(j)
7904                       ,  P_person_id              => l_PersonIdTab(j)
7905                       ,  P_organization_id        => l_OrganizationIdTab(j)
7906                       ,  P_assignment_id          => l_AsgnIdTab(j)
7907                       ,  P_work_type_id           => l_WorkTypeIdTab(j)
7908                       ,  P_Emp_Org_Id             => l_EmpOrgIdTab(j)
7909                       ,  P_Emp_Job_Id             => l_EmpJobIdTab(j)
7910                       ,  P_po_header_id          => l_po_header_idTab(j)
7911                       ,  P_po_line_id               => l_po_line_idTab(j)
7912                       ,  P_person_type            => l_person_typeTab(j)
7913                       ,  P_po_price_type          => l_po_price_typeTab(j)
7914 					  ,  p_vendor_id              => l_VendorIdTab(j) /* Bug # 3601024 */
7915 					  );
7916 
7917                       X_Billable_Flag := l_BillableFlagTab(j);
7918 
7919                     Else
7920 		       l_gms_fundscheck := 'Y' ;
7921 
7922 		       IF ( v_calling_module = 'PAAPIMP'  )  THEN
7923                           -- REL12 AP Lines Uptake
7924                           -- Cash based accounting support
7925                           --
7926                           IF G_cash_based_accounting = 'Y' and
7927 	                     X_transaction_source in ( 'AP EXPENSE', 'AP INVOICE', 'AP NRTAX' , 'AP ERV',
7928 	                                               'INTERCOMPANY_AP_INVOICES', 'INTERPROJECT_AP_INVOICES', 'AP VARIANCE' ) THEN
7929                              --
7930                              -- If one payment rejected then all the payments following it for the given
7931 			     -- invoice distribution id should be rejected.
7932 			     --
7933                              IF l_rejectedSysRef5 = NVL(l_SysRef5Tab(j),0) THEN
7934 
7935                                 -- 5235363   R12.PJ:XB4:DEV:APL:EXP ITEM DATE VALIDATIONS FOR SUPPLIER COST.
7936 				--
7937 				IF l_status in ( 'GMS_EXP_ITEM_DT_BEFORE_AWD_ST' ,'GMS_EXP_ITEM_DT_AFTER_AWD_END' ,
7938 				                 'GMS_AWARD_IS_CLOSED' , 'EXP_TYPE_INACTIVE' , 'ETYPE_SLINK_INACTIVE' ,
7939 				                 'PA_EX_PROJECT_DATE' , 'PA_EXP_TASK_EFF' , 'PA_EXP_ORG_NOT_ACTIVE' , 'NO_ASSIGNMENT'
7940 					) THEN
7941 				   --
7942 				   -- x_status      := 'Unable to process this record because at least one record for the same
7943 				   -- distributions has been rejected.'
7944 				   --
7945 				   x_status         := 'PA_REJECTED_PARENT_RECORD' ;
7946 				ELSE
7947 			           x_status         := l_status ;
7948 				END IF ;
7949 
7950 			     END IF ;
7951 
7952                              IF PG_DEBUG = 'Y' THEN
7953                                 log_message('log_message: If one payment rejected then all the payments following it for the given' );
7954                                 log_message('log_message: invoice distribution id should be rejected.') ;
7955                                 log_message('log_message: x_status :'||x_status ) ;
7956 		             END IF ;
7957 
7958 
7959 			  ELSE
7960 		             l_rejectedSysRef5   := 0 ;
7961 			  END IF ;
7962 
7963 		          IF ( NVL(l_old_sysRef1, '0')    = NVL(l_SysRef1Tab(j), '0') AND
7964 			       NVL(l_old_sysRef2, '0')    = NVL(l_SysRef2Tab(j), '0') AND
7965 			       NVL(l_old_sysRef3, '0')    = NVL(l_SysRef3Tab(j), '0') AND
7966 			       NVL(l_old_sysRef4, '0')    = NVL(l_SysRef4Tab(j), '0') AND
7967 			       NVL(l_old_sysRef5, 0)      = NVL(l_SysRef5Tab(j), 0) ) THEN
7968 			       --
7969                                -- Project and GMS fundschecking should be done once for a given ap
7970 			       -- distribution lines.
7971 			       -- Here because of adjustments in projects/Grants there may be multiple
7972 			       -- records created to back out the expenditure item.
7973 			       --
7974 			       l_gms_fundscheck := 'N' ;
7975 		               l_FcDocTypeTab(j):= 'NOT' ;
7976 			  ELSE
7977 			       l_gms_fundscheck := 'Y' ;
7978 			       l_old_sysRef1    := l_SysRef1Tab(j) ;
7979 			       l_old_sysRef2    := l_SysRef2Tab(j) ;
7980 			       l_old_sysRef3    := l_SysRef3Tab(j) ;
7981 			       l_old_sysRef4    := l_SysRef4Tab(j) ;
7982 			       l_old_sysRef5    := l_SysRef5Tab(j) ;
7983 			  END IF ;
7984 
7985 			  G_adj_item_id := l_AdjExpItemIDTab(j)  ;
7986 
7987                           IF PG_DEBUG = 'Y' THEN
7988                              log_message('log_message: l_gms_fundscheck :'||l_gms_fundscheck) ;
7989                              log_message('log_message: l_FcDocTypeTab(j) :'||l_FcDocTypeTab(j)) ;
7990                              log_message('log_message: G_adj_item_id :'|| G_adj_item_id ) ;
7991 		           END IF ;
7992 
7993 			  --
7994 			  -- Bug:5064930
7995 			  -- Reject the net zero reversals if pa adjustments have rejected
7996 			  --
7997 			  x_status := get_parent_txn_status(x_status, j) ;
7998 
7999 		          -- R12 Check if we need date validations..
8000                           -- 5235363   R12.PJ:XB4:DEV:APL:EXP ITEM DATE VALIDATIONS FOR SUPPLIER COST.
8001 		          -- We do not validate expenditure item adjustments, Net Zero for reversals or
8002 			  -- funds check adjustments.
8003 			  --
8004 			  IF ( G_adj_item_id is NOT NULL ) OR
8005 			     ( l_ExpItemIdTab(j) = 0     ) OR
8006 			     ( l_adjTxnInterfaceIDTab(j) is not NULL )
8007                           THEN
8008 			     g_paapimp_validate_dt := 'N' ;
8009 
8010 			     IF G_adj_item_id = 0 THEN
8011 			        -- PAAPIMP create a record with G_adj_item_id value 0 to indicate date validation
8012 				-- not required for the adjustment created to take care of the accounting mismatch
8013 				-- for the supplier cost adjustments.
8014 				--
8015 			        G_adj_item_id        := NULL ;
8016 				l_AdjExpItemIDTab(j) := NULL ;
8017 			     END IF ;
8018 
8019                           ELSE
8020 			     g_paapimp_validate_dt := 'Y' ;
8021 			  END IF ;
8022 		       END IF ;
8023 
8024                        -- REL12 AP Lines Uptake
8025                        -- Cash based accounting support
8026                        -- IF x_status is NULL THEN was added.
8027 		       --
8028                        IF x_status is NULL THEN
8029 			       ValidateItem(  X_transaction_source
8030 			      ,  l_EmpNumTab(j)
8031 			      ,  l_OrganizationNameTab(j)
8032 			      ,  l_EiEndDateTab(j)
8033 			      ,  l_EiDateTab(j)
8034 			      ,  l_ETypeTab(j)
8035 			      ,  l_PrjNumTab(j)
8036 			      ,  l_TaskNumTab(j)
8037 			      ,  l_NlrTab(j)
8038 			      ,  l_NlrOrgNameTab(j)
8039 			      ,  l_QtyTab(j)
8040 			      ,  l_DenomRawCostTab(j)
8041 			      -- Bug 987539
8042 			      ,  v_calling_module   --'PAXTRTRX'
8043 			      ,  l_OrigTxnRefTab(j)
8044 			      ,  l_UnMatchNegFlagTab(j)
8045 			      ,  X_userid
8046 			      ,  l_AttCatTab(j)
8047 			      ,  l_Att1Tab(j)
8048 			      ,  l_Att2Tab(j)
8049 			      ,  l_Att3Tab(j)
8050 			      ,  l_Att4Tab(j)
8051 			      ,  l_Att5Tab(j)
8052 			      ,  l_Att6Tab(j)
8053 			      ,  l_Att7Tab(j)
8054 			      ,  l_Att8Tab(j)
8055 			      ,  l_Att9Tab(j)
8056 			      ,  l_Att10Tab(j)
8057 			      ,  l_DRCCIDTab(j)
8058 			      ,  l_CRCCIDTab(j)
8059 			      ,  l_GlDateTab(j)
8060 			      ,  l_DenomBdCostTab(j)
8061 			      ,  l_SysLinkTab(j)
8062 			      ,  X_status
8063 			      ,  X_billable_flag
8064 			      ,  l_RcptCurrAmtTab(j)
8065 			      ,  l_RcptCurrCodeTab(j)
8066 			      ,  l_RcptExchRateTab(j)
8067 			      ,  l_DenomCurrCodeTab(j)
8068 			      ,  l_AcctRateDateTab(j)
8069 			      ,  l_AcctRateTypeTab(j)
8070 			      ,  l_AcctExchRateTab(j)
8071 			      ,  l_AcctRawCostTab(j)
8072 			      ,  l_AcctBdCostTab(j)
8073 			      ,  l_AcctExchRndLimitTab(j)
8074 			      ,  l_ProjCurrCodeTab(j)
8075 			      ,  l_prjRateDateTab(j)
8076 			      ,  l_PrjRateTypeTab(j)
8077 			      ,  l_PrjExchRateTab(j)
8078 			      ,  l_rawCostTab(j)
8079 			      ,  l_burdenedCostTab(j)
8080 			      ,  l_OverrideOrgNameTab(j)
8081 			      ,  l_VendorNumTab(j)
8082 			      ,  l_OrgIdTab(j)
8083 			      ,  l_PerBusGrpNameTab(j)
8084 			      ,  l_ProjFuncCurrCodeTab(j)
8085 			      ,  l_PrjFuncRateDateTab(j)
8086 			      ,  l_PrjFuncRateTypeTab(j)
8087 			      ,  l_PrjFuncExchRateTab(j)
8088 			      ,  l_ProjRawCostTab(j)
8089 			      ,  l_ProjBdCostTab(j)
8090 			      ,  l_AsgnNameTab(j)
8091 			      ,  l_WorkTypeNameTab(j)
8092 			      ,  l_AccrualFlagTab(j)
8093 			      ,  l_PrjIdTab(j)
8094 			      ,  l_TaskIdTab(j)
8095 			      ,  l_PersonIdTab(j)
8096 			      ,  l_OrganizationIdTab(j)
8097 			      ,  l_NLROrgIDTab(j)
8098 			      ,  l_VendorIdTab(j)
8099 			      ,  l_OverrideOrgIdTab(j)
8100 			      ,  l_PersonBusGrpIdTab(j)
8101 			      ,  l_AsgnIdTab(j)
8102 			      ,  l_WorkTypeIdTab(j)
8103 			      ,  l_EmpOrgIdTab(j)
8104 			      ,  l_EmpJobIdTab(j)
8105 			      /* Passed the value TrxRec.txn_interface_id for bug 2563364 */
8106 			      ,  l_TxnIfIDTab(j)
8107 			       ,  l_po_numberTab(j) /* cwk */
8108 			       ,  l_po_header_idTab(j)
8109 			       ,  l_po_line_numTab(j)
8110 			       ,  l_po_line_idTab(j)
8111 			       ,  l_person_typeTab(j)
8112 			      ,  l_po_price_typeTab(j)
8113 			      ,  l_AdjExpItemIdTab(j)
8114 			      ,  l_FcDocTypeTab(j) );
8115                            --   ,  l_agreement_idTab(j) --FSIO Change
8116                           --    ,  l_agreement_numberTab(j) );
8117 		       END IF ;
8118 
8119                     End If;
8120                   --  l_BdCostRateTab(j) := l_burdenedCostTab(j)/nvl(l_QtyTab(j),1); --For Bug 4057531
8121 
8122 		   /* Changes for bug 6719252 start here */
8123 
8124 	IF (l_SysLinkTab(j) = 'VI' OR l_SysLinkTab(j) = 'ER') THEN
8125 		IF ( NVL(G_PrjInfoBdDisplay, 'S') = 'S') THEN
8126 			IF (l_burdenedCostTab(j) = 0 or l_QtyTab(j)= 0) THEN --added condition to check for l_QtyTab(j)= 0 for the bug 7459889
8127 				l_BdCostRateTab(j) := l_burdenedCostTab(j); -- which would be 0, in this case.
8128 			ELSE
8129 			 /*IF l_QtyTab(j) = 0 then --added For bug:7436883
8130 	                 l_QtyTab(j) :=1;
8131                          END IF; Commented as part for bug 7591839*/
8132 				l_BdCostRateTab(j) := l_burdenedCostTab(j)/nvl(l_QtyTab(j),1);
8133 			END IF;
8134 		END IF;
8135 	 END IF;
8136 
8137 	  /* Changes for bug 6719252 end here */
8138 
8139 	            -- -----------------------------------------------------------------------
8140 	            -- OGM_0.0 - Vertical application implementations may need to validate
8141 	            -- transactions based on there business needs. So the following code hook
8142 	            -- will call Vertical applications validations routines. It will look st
8143 	            -- x_status and continue validations if x_status is NULL and vertical
8144 	            -- application is implemented only.
8145 	            -- ------------------------------------------------------------------------
8146                     IF PG_DEBUG = 'Y' THEN
8147 	               pa_debug.G_err_stage := 'Calling PA_GMS_API api';
8148                        log_message('log_message: ' || pa_debug.G_err_stage);
8149                     END IF;
8150 
8151                     IF l_gms_enabled = 'Y' and l_gms_fundscheck = 'Y'  THEN
8152                        PA_GMS_API.vert_app_validate(X_transaction_source,
8153                					    TrxBatch.batch_name,
8154 						    l_TxnIfIDTab(j),
8155 						    l_OrgIdTab(j),
8156 						    X_status ) ;
8157 
8158                        if X_status is null then
8159 
8160                           GMS_PA_Costing_Pkg.FundsCheck_Supplier_Cost(p_transaction_source => x_transaction_source,
8161                                                                       p_txn_interface_id   => l_TxnIfIDTab(j),
8162 				                                      p_request_id         => g_request_id,
8163                                                                       p_status             => X_status);
8164 
8165                        end if;
8166 
8167                     END IF; --> gms_enabled.
8168 
8169                     IF PG_DEBUG = 'Y' THEN
8170 	               pa_debug.G_err_stage := 'After PA_GMS_API api';
8171                        log_message('log_message: ' || pa_debug.G_err_stage);
8172                     END IF;
8173 
8174                     -- validateitem has lot of return statements, so instead
8175                     -- resetting the error stack for each return statement
8176                     -- we are resetting the stack after executing
8177                     -- validateitem.
8178 
8179                     --Bug 2749049
8180                     pa_cc_utils.reset_curr_function; /* Uncommented for Bug 4309932 */
8181 
8182 	         END IF; -- end X_org_status is not null
8183 
8184                  IF ( X_status IS NOT NULL ) THEN
8185 
8186                     IF PG_DEBUG = 'Y' THEN
8187                     pa_debug.G_err_stage := 'Updating txn interface table for txn'||
8188                                      ' rejected by validateitem';
8189                        log_message('log_message: ' || pa_debug.G_err_stage);
8190                     END IF;
8191 
8192                     /* PA-K
8193                        Performance Team Suggestion: To combine the below update (by txn_interface_id) with the
8194                        update in newexpend (by expenditure_id).
8195                        Results: For 20,000 records - gain of 30 sec.
8196                        Implementing this combined update has been phased out until there is a dire need to do it.
8197                        Placing this comment here for reference.
8198                     */
8199                     UPDATE pa_transaction_interface
8200                        SET
8201                             transaction_rejection_code = X_status
8202                     ,       interface_id = X_xface_id
8203                     ,       expenditure_id = G_expenditure_id
8204                     ,       transaction_status_code = 'R'
8205                      WHERE txn_interface_id = l_TxnIfIDTab(j);
8206                     --PA-K Changes: Removed 'current of' clause as the 'for update'
8207                     --              clause in Trxrecs has been removed.
8208                     --WHERE CURRENT OF TrxRecs;
8209                     IF PG_DEBUG = 'Y' THEN
8210                        log_message('log_message: ' || 'Updated to reject count ' || SQL%ROWCOUNT);
8211                     END IF;
8212 
8213                     REJECT_EXP := TRUE;
8214 
8215                     -- REL12 AP Lines Uptake
8216                     -- Cash based accounting support
8217                     -- l_rejectedSysRef5 and l_status was populated.
8218 		    --
8219 		    l_rejectedSysRef5   := NVL(l_SysRef5Tab(j), 0) ;
8220 		    l_status            := X_status ;
8221 		    l_txn_rejected_sr4tab(NVL(l_SysRef4Tab(j),0) )  := l_TxnIfIDTab(j) ;
8222 		    l_txn_rejected_sr5tab(NVL(l_SysRef5Tab(j),0) )  := l_TxnIfIDTab(j) ;
8223 
8224                     IF PG_DEBUG = 'Y' THEN
8225                        log_message('log_message: l_txn_rejected_sr4tab(NVL(l_SysRef4Tab(j),0) ) :'||
8226 		                    l_txn_rejected_sr4tab(NVL(l_SysRef4Tab(j),0) )) ;
8227                        log_message('log_message: l_txn_rejected_sr5tab(NVL(l_SysRef5Tab(j),0) ) :'||
8228 		                    l_txn_rejected_sr5tab(NVL(l_SysRef5Tab(j),0) )) ;
8229 
8230                        log_message('log_message: NVL(l_SysRef5Tab(j), 0) :'|| NVL(l_SysRef5Tab(j), 0) ) ;
8231                        log_message('log_message: NVL(l_SysRef4Tab(j), 0) :'|| NVL(l_SysRef4Tab(j), 0) ) ;
8232 		     END IF ;
8233 
8234 
8235                  ELSE
8236 
8237 		    -- BUG:4748305  DFHC.D4:PRC INTERFACE SUPPLIER COST NOT INTERFACING
8238 		    -- VALID INVOICE DISTRIBUTIONS
8239 		    -- We allow good ap invoice distributions to interface in a expenditure
8240 		    -- and do not reject all the expenditure items in expenditure
8241 		    -- This is only done for VI items.
8242                      IF v_calling_module = 'PAAPIMP' and
8243             	        TrxBatch.system_linkage_function = 'VI' THEN
8244 
8245                         IF PG_DEBUG = 'Y' THEN
8246                            pa_debug.G_err_stage := 'Processing the valid distributions '||
8247                                      ' accepted by validateitem';
8248                            log_message('log_message: ' || pa_debug.G_err_stage);
8249                         END IF;
8250 
8251 
8252                         REJECT_EXP := FALSE;
8253                     END IF ;
8254 
8255                     IF PG_DEBUG = 'Y' THEN
8256                     pa_debug.G_err_stage := 'Updating txn interface table for txn'||
8257                                      ' accepted by validateitem';
8258                        log_message('log_message: ' || pa_debug.G_err_stage);
8259                     END IF;
8260 
8261                     UPDATE pa_transaction_interface
8262                        SET
8263                             transaction_rejection_code = NULL
8264                     ,       interface_id = X_xface_id
8265                     ,       expenditure_id = G_expenditure_id
8266                     ,       expenditure_item_id = X_ei_id
8267                             -- SST Changes: If TrxRec.reversed_orig_txn_reference
8268                             -- is NULL, this means we've called verifyorigitem API
8269                             -- to get the reversed item's orig_txn_reference and
8270                             -- store it in G_reversed_orig_txn_reference
8271                     ,       reversed_orig_txn_reference =
8272                             nvl(l_RevOrigTxnRefTab(j),G_reversed_orig_txn_reference)
8273                      WHERE txn_interface_id = l_TxnIfIDTab(j);
8274 
8275 		     l_rejectedSysRef5   := 0 ;
8276 		     l_status            := NULL ;
8277 
8278                     IF PG_DEBUG = 'Y' THEN
8279                        log_message('log_message: ' || 'Updated to accepted count ' || SQL%ROWCOUNT);
8280                        log_message('log_message: ' || 'l_AdjTxnInterfaceIdTab(j) ' ||
8281                        l_AdjTxnInterfaceIdTab(j));
8282 
8283                     END IF;
8284 
8285 		    IF l_nzAdjFlagTab(j) = 'Y' and
8286 		       l_AdjTxnInterfaceIdTab(j) is not NULL THEN
8287 		       G_adj_item_id := l_AdjTxnEiIDTab(l_AdjTxnInterfaceIdTab(j) ) ;
8288 		    END IF ;
8289 
8290                  END IF;
8291 
8292                  IF ( NOT REJECT_EXP ) THEN   ---{
8293 
8294                     i := i + 1;
8295 
8296                     IF (G_trx_costed = 'N' ) THEN
8297 
8298                       l_DenomRawCostTab(j) := NULL;
8299                       l_rawCostRateTab(j) := NULL;
8300                       l_AcctRawCostTab(j) := NULL;
8301                       l_rawCostTab(j) := NULL;
8302 
8303                     ELSIF ( G_trx_costed = 'Y' ) THEN
8304 
8305                       IF PG_DEBUG = 'Y' THEN
8306                       pa_debug.G_err_stage := 'rounding transaction raw cost';
8307                          log_message('log_message: ' || pa_debug.G_err_stage);
8308                       END IF;
8309 
8310                       l_DenomRawCostTab(j) := pa_currency.round_trans_currency_amt1(l_DenomRawCostTab(j),l_DenomCurrCodeTab(j));
8311 
8312 	/* Added the call to round off denom_burdened_cost for bug 2871273 */
8313 
8314                       IF PG_DEBUG = 'Y' THEN
8315                       pa_debug.G_err_stage := 'rounding transaction burden cost';
8316                          log_message('log_message: ' || pa_debug.G_err_stage);
8317                       END IF;
8318 
8319                       l_DenomBdCostTab(j) := pa_currency.round_trans_currency_amt1(l_DenomBdCostTab(j),l_DenomCurrCodeTab(j));
8320 
8321                     END IF;
8322 
8323 		    /* REL12-AP Lines uptake  START */
8324 			    --
8325 			    --BUG : 4696351 PJ.R12:DI4:APLINES: VENDOR INFORMATION NOT IMPORTED DURING TRANSACTION IMPORT
8326 				--
8327 			    IF v_calling_module <> 'PAAPIMP' THEN
8328 			      IF l_SysLinkTab(j) in ( 'ER', 'VI' ) THEN
8329 				     l_vendor_id      := g_vendor_id ;
8330 					  l_SysRef1Tab(j) := g_vendor_id ;
8331 				  END IF ;
8332 				END IF ;
8333 
8334                     -- R12 Bug Fixes
8335 		    -- 4919999
8336 		    -- R12.PJ:XB1:DEV:APL:EIB RELATED CHANGES FOR R12 IN TRANSACTION IMPORT
8337 		    --
8338                     IF X_transaction_source in ('CSE_PO_RECEIPT', 'CSE_PO_RECEIPT_DEPR'  ) THEN
8339 		       l_vendor_id           := l_SysRef1Tab(j) ;
8340 		       l_doc_header_id       := l_SysRef2Tab(j) ;
8341 		       l_doc_distribution_id := l_SysRef4Tab(j) ; -- RCV Transaction ID
8342 		       -- ===============
8343 		       -- Populate po distribution id in l_Doc_line_number for receipt.
8344 		       -- ===============
8345 		       l_Doc_line_number     := l_SysRef3Tab(j) ;
8346 
8347 		    ELSIF  X_transaction_source in ( 'CSE_IPV_ADJUSTMENT', 'CSE_IPV_ADJUSTMENT_DEPR' ) THEN
8348 		       l_vendor_id           := l_SysRef1Tab(j) ;
8349 		       l_doc_header_id       := l_SysRef2Tab(j) ;
8350 		       l_doc_distribution_id := l_SysRef5Tab(j) ;
8351 		       l_Doc_line_number     := l_SysRef3Tab(j) ;
8352 		       l_SysRef5Tab(j)       := NULL ;
8353 		    END IF ;
8354 		    -- 4919999  R12.PJ:XB1:DEV:APL:EIB RELATED CHANGES FOR R12 IN TRANSACTION IMPORT
8355                     -- End of R12 Bug Fixes
8356 		    --
8357 
8358                     -- 4927696  PAYABLES INTERFACE TO POPULATE SR_5 WITH PAYMENT_HIST_DIST_ID
8359 		    -- Cost distribution lines , System_reference5 is populated with the payment_hist_dist_id
8360 		    -- for discounts and payments.
8361 		    -- For prepay application in cash based accounting, SR5 is populated with the pre-pay
8362 		    -- appdist_id.
8363                     IF PG_DEBUG = 'Y' THEN
8364 		       log_message('log_message: Cost distribution lines , System_reference5 is populated with the ') ;
8365 		       log_message('log_message: payment_hist_dist_id for discounts and payments. ') ;
8366 		       log_message('log_message: For prepay application in cash based accounting, SR5') ;
8367 		       log_message('log_message: is populated with the pre-pay appdist_id.') ;
8368 
8369                        pa_debug.G_err_stage := 'l_VendorIdTab(j) : '|| l_VendorIdTab(j) ;
8370                        log_message('log_message: ' || pa_debug.G_err_stage);
8371 
8372                        pa_debug.G_err_stage := 'l_SysRef1Tab(j) : '|| l_SysRef1Tab(j)  ;
8373                        log_message('log_message: ' || pa_debug.G_err_stage);
8374                        pa_debug.G_err_stage := 'l_SysRef2Tab(j) : '|| l_SysRef2Tab(j)  ;
8375                        log_message('log_message: ' || pa_debug.G_err_stage);
8376                        pa_debug.G_err_stage := 'l_SysRef3Tab(j) : '|| l_SysRef3Tab(j)  ;
8377                        log_message('log_message: ' || pa_debug.G_err_stage);
8378                        pa_debug.G_err_stage := 'l_SysRef4Tab(j) : '|| l_SysRef4Tab(j)  ;
8379                        log_message('log_message: ' || pa_debug.G_err_stage);
8380                        pa_debug.G_err_stage := 'l_SysRef5Tab(j) : '|| l_SysRef5Tab(j)  ;
8381                        log_message('log_message: ' || pa_debug.G_err_stage);
8382                     END IF;
8383 
8384 	            IF v_calling_module = 'PAAPIMP' THEN
8385 
8386                            -- 4927696  PAYABLES INTERFACE TO POPULATE SR_5 WITH PAYMENT_HIST_DIST_ID
8387 			   l_vendor_id := NVL(l_VendorIdTab(j), l_SysRef1Tab(j))  ;
8388 
8389 	                   IF X_transaction_source in ( 'AP EXPENSE', 'AP INVOICE', 'AP NRTAX' ,  'AP ERV',
8390 		                                         'INTERCOMPANY_AP_INVOICES', 'INTERPROJECT_AP_INVOICES',
8391 				              	         'AP VARIANCE' ) THEN
8392 		              l_doc_header_id       := l_SysRef2Tab(j) ;
8393 			      l_doc_distribution_id := l_SysRef5Tab(j) ;
8394 			      l_Doc_line_number     := l_SysRef3Tab(j) ;
8395 
8396 			      -- CDL place holder for subledger id for transaction source of RECEIPTS and
8397 			      -- Manufacturing related records.
8398 			      l_SysRef5Tab(j)       := NULL ;
8399 			      IF g_cash_based_accounting = 'Y' Then
8400 
8401 				-- For historical transactions  l_SysRef4Tab(j) is populated with the
8402 				-- 'NONREC_TAX'. Invoice distributions gets interfaced instead of
8403 				-- Payments.
8404 				-- BUG : 5370864
8405 				-- R12.PJ:XB6:QA:APL:UPG:INTERFACE SUPP COSTS ERRORS TAX NOT INTERFACED AFTER UPG
8406 				--
8407 			        IF l_DocumentDistTypeTab(j) <> NVL(l_SysRef4Tab(j), l_DocumentDistTypeTab(j))  THEN
8408  			           l_doc_payment_id    := l_SysRef4Tab(j) ; -- Populated when Cash based accounting is setup.
8409 				ELSE
8410 				   -- This is a historical transactions and payment is not interfaced. Invoice dist
8411 				   -- is getting interfaced instead.
8412 				   -- Bug : 5443263
8413 				   --       R12.PJ:XB7:QA:APL:UPG:ADJUSTMENT REVERSAL NOT GETTING ACCOUNTED IN CASH
8414 				   -- Resolution :
8415 				   --       We need to mark such expenditure items corresponding to invoice distributions
8416 				   --       as historical. The '-1' value in the l_doc_payment_id would indicate in
8417 				   --       PA_transactions package that this is a cash based accounting when invoice distributions
8418 				   --       is getting interfaced instead of payment. The value would be flipped back to null
8419 				   --       in the pa transactions InsItems api before creating the exp items.
8420 				   l_doc_payment_id := -1 ;
8421 				END IF ;
8422 
8423 				-- l_SysRef1Tab records the payment hist dist ID when needed to go to system_reference5
8424 				-- of CDLs.
8425                                 -- 4927696  PAYABLES INTERFACE TO POPULATE SR_5 WITH PAYMENT_HIST_DIST_ID
8426                                 l_SysRef5Tab(j)     := l_SysRef1Tab(j) ;
8427 				l_SysRef1Tab(j)     := l_VendorIdTab(j) ;
8428 
8429 				IF l_DocumentDistTypeTab(j) = 'PREPAY' and
8430 				   l_doc_payment_id is not NULL and
8431 				   l_doc_payment_id <> -1 THEN
8432 				   --
8433 				   -- Document_payment_id is the prepay app dist id
8434 				   --
8435                                    -- 4927696  PAYABLES INTERFACE TO POPULATE SR_5 WITH PAYMENT_HIST_DIST_ID
8436 				   l_SysRef5Tab(j)     := l_doc_payment_id ;
8437                                 END IF ;
8438 			      END IF;
8439 
8440 
8441 		           ELSIF X_transaction_source in (  'AP DISCOUNTS' ) THEN
8442 		              l_doc_header_id       := l_SysRef2Tab(j) ;
8443 			      l_doc_distribution_id := l_SysRef5Tab(j) ;
8444 			      l_Doc_line_number     := l_SysRef3Tab(j) ;
8445 			      l_doc_payment_id      := l_SysRef4Tab(j) ;
8446 
8447 			      -- CDL place holder for subledger id for transaction source of RECEIPTS and
8448 			      -- Manufacturing related records.
8449 
8450 			      -- l_SysRef1Tab records the payment hist dist ID when needed to go to system_reference5
8451 			      -- of CDLs.
8452                               -- 4927696  PAYABLES INTERFACE TO POPULATE SR_5 WITH PAYMENT_HIST_DIST_ID
8453                               l_SysRef5Tab(j)     := l_SysRef1Tab(j) ;
8454 			      l_SysRef1Tab(j)     := l_VendorIdTab(j) ;
8455 
8456 		           ELSIF X_transaction_source in ( 'PO RECEIPT', 'PO RECEIPT NRTAX',
8457 		                                           'PO RECEIPT PRICE ADJ', 'PO RECEIPT NRTAX PRICE ADJ' )  THEN
8458 		              l_doc_header_id       := l_SysRef2Tab(j) ;
8459 			      l_doc_distribution_id := l_SysRef4Tab(j) ; -- RCV Transaction ID
8460 			      -- ===============
8461 			      -- Populate po distribution id in l_Doc_line_number for receipt.
8462 			      -- ===============
8463 			      l_Doc_line_number     := l_SysRef3Tab(j) ;
8464 			      l_doc_payment_id      := NULL ;
8465                            ELSE
8466 		              l_doc_header_id       := NULL ;
8467 			      l_doc_distribution_id := NULL ;
8468 			      l_Doc_line_number     := NULL ;
8469 			      l_doc_payment_id      := NULL ;
8470 		           END IF ;
8471 	            END IF ;
8472 
8473                     IF PG_DEBUG = 'Y' THEN
8474 
8475                        pa_debug.G_err_stage := ' After l_VendorIdTab(j) : '|| l_VendorIdTab(j) ;
8476                        log_message('log_message: ' || pa_debug.G_err_stage);
8477 
8478                        pa_debug.G_err_stage := ' After l_SysRef1Tab(j) : '|| l_SysRef1Tab(j)  ;
8479                        log_message('log_message: ' || pa_debug.G_err_stage);
8480                        pa_debug.G_err_stage := ' After l_SysRef2Tab(j) : '|| l_SysRef2Tab(j)  ;
8481                        log_message('log_message: ' || pa_debug.G_err_stage);
8482                        pa_debug.G_err_stage := ' After l_SysRef3Tab(j) : '|| l_SysRef3Tab(j)  ;
8483                        log_message('log_message: ' || pa_debug.G_err_stage);
8484                        pa_debug.G_err_stage := 'After l_SysRef4Tab(j) : '|| l_SysRef4Tab(j)  ;
8485                        log_message('log_message: ' || pa_debug.G_err_stage);
8486                        pa_debug.G_err_stage := 'After l_SysRef5Tab(j) : '|| l_SysRef5Tab(j)  ;
8487                        log_message('log_message: ' || pa_debug.G_err_stage);
8488                     END IF;
8489 
8490 		    /* REL12-AP Lines uptake  END  */
8491 
8492 	            IF ( nvl(G_gl_accted_flag,'N') = 'N' ) THEN
8493           	       -- If the transaction is not accounted, the costing
8494      	               -- program will calculate the functional and project
8495 	               -- costs, so Null out the functional and project costs
8496           	       -- for un accounted txns. Null out the exchange rates if
8497           	       -- the exchange rate type is not user.
8498 
8499 	               l_AcctRawCostTab(j) := NULL;
8500 		       l_AcctBdCostTab(j) := NULL;
8501           	       l_rawCostTab(j) := NULL;
8502 	               l_burdenedCostTab(j) := NULL;
8503 
8504                /*S.N. Bug 5170366 */
8505                IF (nvl(G_proj_bcost_flag, 'N') = 'N' AND l_DenomBdCostTab(j) = l_DenomRawCostTab(j) ) THEN
8506                    l_DenomBdCostTab(j) := Null;
8507                     l_BdCostRateTab(j)  := Null; /*Bug#5874347*/
8508                End IF;
8509                /*E.N. Bug 5170366 */
8510 
8511 		       IF ( nvl(G_acct_rate_type,'DUMMY') <> 'User' ) THEN
8512 			  l_AcctExchRateTab(j) := NULL;
8513 		       END IF; -- end G_acct_rate_type <> User
8514 
8515 		       IF ( nvl(G_project_rate_type,'DUMMY') <> 'User') THEN
8516 		          l_PrjExchRateTab(j) := NULL;
8517 		       END IF; -- end G_project_rate_type <> User
8518 
8519 	/* Added the ELSE section to round off the acct raw and burden costs for gl costed transactions for bug 2871273 */
8520 
8521                     ELSE
8522 
8523                        l_AcctRawCostTab(j) :=  pa_currency.round_trans_currency_amt1(l_AcctRawCostTab(j),G_accounting_currency_code);
8524                        l_AcctBdCostTab(j)  :=  pa_currency.round_trans_currency_amt1(l_AcctBdCostTab(j),G_accounting_currency_code);
8525 
8526 	            END IF; -- end G_gl_accted_flag = N
8527 
8528                     IF (l_DenomCurrCodeTab(j) = G_accounting_currency_code) THEN
8529 
8530 	               l_AcctRateDateTab(j) := NULL;
8531 		       l_AcctRateTypeTab(j) := NULL;
8532 		       l_AcctExchRateTab(j) := NULL;
8533 
8534 	            END IF; -- end denom currency = acct currency
8535 
8536 	            IF ( l_DenomCurrCodeTab(j) = l_ProjCurrCodeTab(j)) THEN
8537 
8538 	  	       l_prjRateDateTab(j) := NULL;
8539 		       l_PrjRateTypeTab(j) := NULL;
8540 		       l_PrjExchRateTab(j) := NULL;
8541 
8542 	            END IF; -- End denom currency=project currency
8543 
8544                     -- SSt changes
8545                     -- For contract and capital projects, if the billable flag
8546                     -- is populated in the interface table, then override the
8547                     -- billable flag derived from pa_transactions_pub.validate_transaction
8548                     -- with the value in the billable flag column
8549 
8550                     IF PG_DEBUG = 'Y' THEN
8551                        log_message('log_message: ' || 'Intial Billable Flag = ' || X_billable_flag);
8552                     END IF;
8553 
8554                     IF ( G_project_type_class IN ('CONTRACT','CAPITAL') AND
8555                	     l_billableFlagTab(j) IN ( 'Y','N')) THEN
8556 
8557                        IF PG_DEBUG = 'Y' THEN
8558                        pa_debug.G_err_stage := 'Project_type_class in Contract, Capital';
8559                           log_message('log_message: ' || pa_debug.G_err_stage);
8560                        END IF;
8561 
8562                        X_billable_flag := l_billableFlagTab(j);
8563 
8564                        --PA-I Changes
8565                        --If  profile - PA: Require Work Type Entry for Expenditures
8566                        --and profile - PA: Transaction Billability Derived from Work Type
8567                        --is set to Y then we have to derive billability from work_type
8568                        --else use what user has entered in TrxRec.billable_flag
8569                        --Here we are calling the API get_trxn_work_billabilty to override
8570                        --the billable flag.
8571                        --For now we can call the API but latter if performance is a
8572                        --bottleneck then we can replace this API call by just checking the
8573                        --two profiles and if it is Y then use the billable_flag from
8574                        --validate_transaction and if N then override with what user entered.
8575 
8576                        IF PG_DEBUG = 'Y' THEN
8577                        pa_debug.G_err_stage := 'Calling pa_utils4.get_transaction_billability';
8578                           log_message('log_message: ' || pa_debug.G_err_stage);
8579                           log_message('log_message: ' || 'Billable Flag before get_trxn_work_billabilty = ' || X_billable_flag);
8580                           log_message('log_message: ' || 'Work Type Id = ' || G_work_type_id);
8581                        END IF;
8582 
8583                        X_billable_flag :=  pa_utils4.get_trxn_work_billabilty(
8584                                            p_work_type_id      => G_work_type_id,
8585                                            p_tc_extn_bill_flag => X_billable_flag);
8586 
8587                        IF PG_DEBUG = 'Y' THEN
8588                           log_message('log_message: ' || 'Billable Flag after get_trxn_work_billabilty = ' || X_billable_flag);
8589                        END IF;
8590 
8591                     END IF;
8592 
8593                     IF PG_DEBUG = 'Y' THEN
8594                        log_message('log_message: ' || 'Final Billable Flag = ' || X_billable_flag);
8595                     END IF;
8596 
8597                     --PA-K Changes:
8598                     --TrxRec.expenditure_item_id := X_ei_id;
8599 
8600                     --Added for bug 2048868
8601                     prev_acct_rate_type     := G_acct_rate_type;
8602                     prev_acct_rate_date     := l_AcctRateDateTab(j);
8603                     prev_acct_exchange_rate := l_AcctExchRateTab(j);
8604                     prev_denom_currency_code := l_DenomCurrCodeTab(j);
8605                     prev_person_type := l_person_typeTab(j);
8606 
8607                     /* Added for 4057874 */
8608                     IF (l_SysLinkTab(j) = 'BTC'
8609                     AND G_trx_predef_flag = 'Y'
8610                     AND X_transaction_source <> 'ALLOCATIONS'
8611                     AND P_BTC_SRC_RESRC = 'Y' ) THEN
8612 
8613                          l_src_system_linkage_function := G_trx_link;
8614 
8615                          IF PG_DEBUG = 'Y' THEN
8616                           log_message('log_message: ' || 'Src system linkage function = '||l_src_system_linkage_function);
8617                          END IF;
8618 
8619                     ELSE
8620 
8621                          l_src_system_linkage_function := null;
8622 
8623                     END IF;
8624 
8625 					/* Bug 4107560 : Overriding the wip_resource_id for the BTC transactions to be interfaced from transaction source
8626 					   with wip_resource_id of parent source transaction */
8627 
8628 					IF l_SysLinkTab(j) = 'BTC' AND l_wip_resource_idTab(j) IS NOT NULL Then
8629 
8630 						If G_prev_ORIG_TRAN_REF Is Null or G_prev_ORIG_TRAN_REF <> l_OrigTxnRefTab(j) Then
8631 
8632 							G_prev_ORIG_TRAN_REF := l_OrigTxnRefTab(j);
8633 
8634 							Begin
8635 								Select WIP_RESOURCE_ID
8636 								  INTO G_WIP_RESOURCE_ID
8637 								  FROM PA_TRANSACTION_INTERFACE_ALL
8638 								 WHERE ORIG_TRANSACTION_REFERENCE =  l_OrigTxnRefTab(j)
8639 								   and SYSTEM_LINKAGE  <> 'BTC'
8640 								   and rownum = 1;
8641 							Exception
8642 								When No_data_found Then
8643 									Select resource_id
8644 									  into G_WIP_RESOURCE_ID
8645 									  from wip_transactions
8646 									 where transaction_id = l_OrigTxnRefTab(j);
8647 							End;
8648 
8649 							l_wip_resource_idTab(j) := G_WIP_RESOURCE_ID;
8650 						Else
8651 							l_wip_resource_idTab(j) := G_WIP_RESOURCE_ID;
8652 						End if;
8653 
8654 						IF PG_DEBUG = 'Y' THEN
8655                           log_message('log_message: ' || 'BTC WIP Resource ID = ' || G_WIP_RESOURCE_ID );
8656                         END IF;
8657 
8658 					END IF;
8659 
8660 					/* End of bug 4107560 */
8661 
8662 
8663                     IF PG_DEBUG = 'Y' THEN
8664                     pa_debug.G_err_stage := 'Calling Loadei';
8665                        log_message('log_message: ' || pa_debug.G_err_stage);
8666                     END IF;
8667 
8668                     -- REL12 AP Lines uptake..
8669 		    l_temp_adjItemID := G_adj_item_id ;
8670 
8671 		    --
8672 		    -- The following code is added to calculate billable flag for the
8673 		    -- ap reversal distributions which is interfaced as net zero
8674 		    -- expenditure items
8675 		    --
8676 		    IF l_nzAdjFlagTab(j) = 'Y' Then
8677 		       l_temp_adjItemID := NULL ;
8678 		    END IF ;
8679 
8680 		    IF X_ei_id is NOT NULL THEN
8681 		            -- Bug 4604493, 4503768  begins
8682 				    IF (l_SysLinkTab(j) <> 'USG')  THEN
8683                         G_nlro_id := Null;
8684                     END IF;/*Bug 4503768*/
8685 		            -- Bug 4604493, 4503768  ends
8686 
8687                     pa_transactions.LoadEi( X_expenditure_item_id     =>	X_ei_id
8688                         --PA-K Changes: TrxRec.expenditure_item_id
8689                        ,X_expenditure_id          =>	G_expenditure_id
8690                        ,X_expenditure_item_date   =>	l_EiDateTab(j)
8691                        ,X_project_id              =>	G_project_id
8692                        ,X_task_id                 =>	G_task_id
8693                        ,X_expenditure_type        =>	l_ETypeTab(j)
8694                        ,X_non_labor_resource      =>	l_NlrTab(j)
8695                        ,X_nl_resource_org_id      =>	G_nlro_id
8696                        ,X_quantity                =>	l_QtyTab(j)
8697                        ,X_raw_cost                =>	l_rawCostTab(j)
8698                        ,X_raw_cost_rate           =>	l_rawCostRateTab(j)
8699                        -- Trx_import enhancement
8700                        ,X_override_to_org_id      =>	G_override_to_org_id -- Changed from NULL
8701                        /* Added for bug 3220230 for getting billability of items reversed in OIT    */
8702 		               ,X_billable_flag           =>	pa_utils4.GetOrig_EiBillability_SST(l_temp_adjItemID,X_billable_flag,X_transaction_source)
8703                        /* Added for bug 4057474 for getting bill_hold_flag of items reversed in external txn sources like OTL. */
8704                        ,X_bill_hold_flag          =>	pa_utils4.GetOrig_EiBill_hold(l_temp_adjItemID,'N')
8705                        ,X_orig_transaction_ref    =>	l_OrigTxnRefTab(j)
8706                        ,X_transferred_from_ei     =>	NULL
8707                        ,X_adj_expend_item_id      =>	G_adj_item_id
8708                        ,X_attribute_category      =>	l_AttCatTab(j)
8709                        ,X_attribute1              =>	l_Att1Tab(j)
8710                        ,X_attribute2              =>	l_Att2Tab(j)
8711                        ,X_attribute3              =>	l_Att3Tab(j)
8712                        ,X_attribute4              =>	l_Att4Tab(j)
8713                        ,X_attribute5              =>	l_Att5Tab(j)
8714                        ,X_attribute6              =>	l_Att6Tab(j)
8715                        ,X_attribute7              =>	l_Att7Tab(j)
8716                        ,X_attribute8              =>	l_Att8Tab(j)
8717                        ,X_attribute9              =>	l_Att9Tab(j)
8718                        ,X_attribute10             =>	l_Att10Tab(j)
8719                        ,X_ei_comment              =>	l_ExpCommentTab(j)
8720                        ,X_transaction_source      =>	X_transaction_source
8721                        ,X_source_exp_item_id      =>	NULL
8722                        ,i                         =>	i
8723                        ,X_job_id                  =>	G_job_id
8724                        ,X_org_id                  =>	l_OrgIdTab(j)
8725                        ,X_labor_cost_multiplier_name =>  G_lcm
8726                        ,X_drccid                  =>	l_DRCCIDTab(j)
8727                        ,X_crccid                  =>	l_CRCCIDTab(j)
8728                        ,X_cdlsr1                  =>	l_SysRef1Tab(j)
8729                        ,X_cdlsr2                  =>	l_SysRef2Tab(j)
8730                        ,X_cdlsr3                  =>	l_SysRef3Tab(j)
8731                        ,X_gldate                  =>	l_GlDateTab(j)
8732                        ,X_bcost                   =>	l_burdenedCostTab(j)
8733                        ,X_bcostrate               =>	l_BdCostRateTab(j)
8734                        ,X_etypeclass              =>	l_SysLinkTab(j)
8735                        ,X_burden_sum_dest_run_id  =>	''
8736                        ,X_burden_compile_set_id   =>	G_burden_compile_set_id
8737                        ,X_receipt_currency_amount =>	l_RcptCurrAmtTab(j)
8738                        ,X_receipt_currency_code   =>	l_RcptCurrCodeTab(j)
8739                        ,X_receipt_exchange_rate   =>	l_RcptExchRateTab(j)
8740                        ,X_denom_currency_code     =>	l_DenomCurrCodeTab(j)
8741                        ,X_denom_raw_cost          =>	l_DenomRawCostTab(j)
8742                        ,X_denom_burdened_cost     =>	l_DenomBdCostTab(j)
8743                        ,X_acct_currency_code      =>	G_accounting_currency_code
8744                        ,X_acct_rate_date          =>	l_AcctRateDateTab(j)
8745                        ,X_acct_rate_type          =>     G_acct_rate_type
8746                        ,X_acct_exchange_rate      =>	l_AcctExchRateTab(j)
8747                        ,X_acct_raw_cost           =>	l_AcctRawCostTab(j)
8748                        ,X_acct_burdened_cost      =>	l_AcctBdCostTab(j)
8749                        ,X_acct_exchange_rounding_limit =>	l_AcctExchRndLimitTab(j)
8750                        ,X_project_currency_code   =>	l_ProjCurrCodeTab(j)
8751                        ,X_project_rate_date       =>	l_prjRateDateTab(j)
8752                        ,X_project_rate_type       =>	G_project_rate_type
8753                        ,X_project_exchange_rate   =>	l_PrjExchRateTab(j)
8754                        ,X_Cross_Charge_Type       => G_CrossChargeType
8755                        ,X_Cross_Charge_Code       => G_CrossChargeCode
8756                        ,X_Prvdr_organization_id   => G_PrvdrOrganizationId
8757                        ,X_Recv_organization_id    => G_RecvrOrganizationId
8758                        ,X_Recv_Operating_Unit     => G_RecvrOrgId
8759                        ,X_Borrow_Lent_Dist_Code   => G_BrowLentDistCode
8760                        ,X_Ic_Processed_Code       => G_IcProcessed_Code
8761                        ,P_PaDate                  => G_PaDate
8762                        ,P_PaPeriodName            => G_PaPeriodName
8763                        ,P_RecvrPaDate             => G_RecvrPaDate
8764                        ,P_RecvrPaPeriodName       => G_RecvrPaPeriodName
8765                        ,P_GlPeriodName            => G_GlPeriodName
8766                        ,P_RecvrGlDate             => G_RecvrGlDate
8767                        ,P_RecvrGlPeriodName       => G_RecvrGlPeriodName
8768                        -- PA-I Changes
8769                        ,p_assignment_id               => G_Assignment_Id
8770                        ,p_work_type_id                => G_Work_Type_Id
8771                        ,p_projfunc_currency_code      => l_ProjFuncCurrCodeTab(j)
8772                        ,p_projfunc_cost_rate_date     => l_PrjFuncRateDateTab(j)
8773                        ,p_projfunc_cost_rate_type     => G_projfunc_cost_rate_type
8774                        ,p_projfunc_cost_exchange_rate => l_PrjFuncExchRateTab(j)
8775                        ,p_project_raw_cost            => l_ProjRawCostTab(j)
8776                        ,p_project_burdened_cost       => l_ProjBdCostTab(j)
8777                        ,p_tp_amt_type_code            => G_Tp_Amt_Type_Code
8778                        -- AP Discounts
8779                        ,p_cdlsr4                      => l_Sysref4Tab(j)
8780                        ,p_accrual_Date                => G_AccDate
8781                        ,p_recvr_accrual_date          => G_RecvrAccDate
8782 		               ,p_po_line_id                  =>          l_po_line_idTab(j) /* cwk */
8783 		               ,p_po_price_type            =>          l_po_price_typeTab(j)
8784 		               ,p_wip_resource_id          =>          l_wip_resource_idTab(j)
8785 		               ,p_inventory_item_id        =>          l_inventory_item_idTab(j)
8786 		               ,p_unit_of_measure          =>         l_unit_of_measureTab(j)
8787  	                   ,p_src_system_linkage_function  =>l_src_system_linkage_function /* 4057874 */
8788 		               ,p_document_header_id       =>    l_doc_header_id
8789 		               ,p_document_distribution_id =>    l_doc_distribution_ID
8790 		               ,p_document_line_number     =>    l_doc_line_number
8791 		               ,p_document_payment_ID      =>    l_doc_payment_ID
8792 		               ,p_vendor_id                =>    l_vendor_id
8793 		               ,p_document_type            =>    l_DocumentTypeTab(j)
8794 		               ,p_document_distribution_type=>   l_DocumentDistTypeTab(j)
8795                        ,p_si_assets_addition_flag   =>   l_siaAddFlagTab(j)
8796 		               ,p_sc_xfer_code              =>   l_scxfercdTab(j)
8797                        ,p_cdlsr5                    =>   l_Sysref5Tab(j)
8798                        --,p_agreement_id              =>   l_agreement_idTab(j)    --FSIO Changes
8799                         );
8800 		    END IF ;
8801 
8802                     -- PA-J Period-End Accrual Changes
8803                     -- Call LoadEI for creating the reversing item.
8804                     if (l_AccrualFlagTab(j) = 'Y' and l_SysLinkTab(j) = 'PJ' and X_ei_id is not NULL ) then
8805 
8806                        IF PG_DEBUG = 'Y' THEN
8807                        pa_debug.G_err_stage := 'Calling Loadei for the reversing line of a Period-End Accrual transaction';
8808                           log_message('log_message: ' || pa_debug.G_err_stage);
8809                        END IF;
8810 
8811                        -- increment the counter
8812                        i := i + 1;
8813 
8814                        pa_transactions.LoadEi(
8815                           X_expenditure_item_id     =>	pa_utils.GetNextEiId
8816                          ,X_expenditure_id          =>	G_expenditure_id
8817                          ,X_expenditure_item_date   =>	l_EiDateTab(j)
8818                          ,X_project_id              =>	G_project_id
8819                          ,X_task_id                 =>	G_task_id
8820                          ,X_expenditure_type        =>	l_ETypeTab(j)
8821                          ,X_non_labor_resource      =>	l_NlrTab(j)
8822                          ,X_nl_resource_org_id      =>	G_nlro_id
8823                          ,X_quantity                =>	(0 - l_QtyTab(j))
8824                          ,X_raw_cost                =>	(0 - l_rawCostTab(j))
8825                          ,X_raw_cost_rate           =>	l_rawCostRateTab(j)
8826                          ,X_override_to_org_id      =>	G_override_to_org_id
8827                          ,X_billable_flag           =>	X_billable_flag
8828                          ,X_bill_hold_flag          =>	'N'
8829                          ,X_orig_transaction_ref    =>	l_OrigTxnRefTab(j)
8830                          ,X_transferred_from_ei     =>	NULL
8831                          ,X_adj_expend_item_id      =>	X_ei_id --PA-K Changes: TrxRec.expenditure_item_id
8832                          ,X_attribute_category      =>	l_AttCatTab(j)
8833                          ,X_attribute1              =>	l_Att1Tab(j)
8834                          ,X_attribute2              =>	l_Att2Tab(j)
8835                          ,X_attribute3              =>	l_Att3Tab(j)
8836                          ,X_attribute4              =>	l_Att4Tab(j)
8837                          ,X_attribute5              =>	l_Att5Tab(j)
8838                          ,X_attribute6              =>	l_Att6Tab(j)
8839                          ,X_attribute7              =>	l_Att7Tab(j)
8840                          ,X_attribute8              =>	l_Att8Tab(j)
8841                          ,X_attribute9              =>	l_Att9Tab(j)
8842                          ,X_attribute10             =>	l_Att10Tab(j)
8843                          ,X_ei_comment              =>	l_ExpCommentTab(j)
8844                          ,X_transaction_source      =>	X_transaction_source
8845                          ,X_source_exp_item_id      =>	NULL
8846                          ,i                         =>	i
8847                          ,X_job_id                  =>	G_job_id
8848                          ,X_org_id                  =>	l_OrgIdTab(j)
8849                          ,X_labor_cost_multiplier_name =>  G_lcm
8850                          ,X_drccid                  =>	l_DRCCIDTab(j)
8851                          ,X_crccid                  =>	l_CRCCIDTab(j)
8852                          ,X_cdlsr1                  =>	l_SysRef1Tab(j)
8853                          ,X_cdlsr2                  =>	l_SysRef2Tab(j)
8854                          ,X_cdlsr3                  =>	l_SysRef3Tab(j)
8855                          ,X_gldate                  =>	G_RevGlDate
8856                          ,X_bcost                   =>	(0 - l_burdenedCostTab(j))
8857                          ,X_bcostrate               =>	l_BdCostRateTab(j)
8858                          ,X_etypeclass              =>	l_SysLinkTab(j)
8859                          ,X_burden_sum_dest_run_id  =>	''
8860                          ,X_burden_compile_set_id   =>	G_burden_compile_set_id
8861                          ,X_receipt_currency_amount =>	(0 - l_RcptCurrAmtTab(j))
8862                          ,X_receipt_currency_code   =>	l_RcptCurrCodeTab(j)
8863                          ,X_receipt_exchange_rate   =>	l_RcptExchRateTab(j)
8864                          ,X_denom_currency_code     =>	l_DenomCurrCodeTab(j)
8865                          ,X_denom_raw_cost          =>	(0 - l_DenomRawCostTab(j))      --2436444
8866                          ,X_denom_burdened_cost     =>	(0 - l_DenomBdCostTab(j))
8867                          ,X_acct_currency_code      =>	G_accounting_currency_code
8868                          ,X_acct_rate_date          =>	l_AcctRateDateTab(j)
8869                          ,X_acct_rate_type          => G_acct_rate_type
8870                          ,X_acct_exchange_rate      =>	l_AcctExchRateTab(j)
8871                          ,X_acct_raw_cost           =>	(0 - l_AcctRawCostTab(j))
8872                          ,X_acct_burdened_cost      =>	(0 - l_AcctBdCostTab(j))
8873                          ,X_acct_exchange_rounding_limit =>	l_AcctExchRndLimitTab(j)
8874                          ,X_project_currency_code   =>	l_ProjCurrCodeTab(j)
8875                          ,X_project_rate_date       =>	l_prjRateDateTab(j)
8876                          ,X_project_rate_type       =>	G_project_rate_type
8877                          ,X_project_exchange_rate   =>	l_PrjExchRateTab(j)
8878                          ,X_Cross_Charge_Type       => G_CrossChargeType
8879                          ,X_Cross_Charge_Code       => G_CrossChargeCode
8880                          ,X_Prvdr_organization_id   => G_PrvdrOrganizationId
8881                          ,X_Recv_organization_id    => G_RecvrOrganizationId
8882                          ,X_Recv_Operating_Unit     => G_RecvrOrgId
8883                          ,X_Borrow_Lent_Dist_Code   => G_BrowLentDistCode
8884                          ,X_Ic_Processed_Code       => G_IcProcessed_Code
8885                          ,P_PaDate                  => G_RevPaDate
8886                          ,P_PaPeriodName            => G_RevPaPeriodName
8887                          ,P_RecvrPaDate             => G_RevRecvrPaDate
8888                          ,P_RecvrPaPeriodName       => G_RevRecvrPaPdName
8889                          ,P_GlPeriodName            => G_RevGlPeriodName
8890                          ,P_RecvrGlDate             => G_RevRecvrGlDate
8891                          ,P_RecvrGlPeriodName       => G_RevRecvrGlPdName
8892                          ,p_assignment_id               => G_Assignment_Id
8893                          ,p_work_type_id                => G_Work_Type_Id
8894                          ,p_projfunc_currency_code      => l_ProjFuncCurrCodeTab(j)
8895                          ,p_projfunc_cost_rate_date     => l_PrjFuncRateDateTab(j)
8896                          ,p_projfunc_cost_rate_type     => G_projfunc_cost_rate_type
8897                          ,p_projfunc_cost_exchange_rate => l_PrjFuncExchRateTab(j)
8898                          ,p_project_raw_cost            => (0 - l_ProjRawCostTab(j))
8899                          ,p_project_burdened_cost       => (0 - l_ProjBdCostTab(j))
8900                          ,p_tp_amt_type_code            => G_Tp_Amt_Type_Code
8901                          ,p_cdlsr4                      => l_Sysref4Tab(j)
8902                          ,p_accrual_Date                => G_RevAccDate
8903                          ,p_recvr_accrual_date          => G_RevRecvrAccDate
8904                        ,p_po_line_id                  =>          l_po_line_idTab(j) /* cwk */
8905 		       ,p_po_price_type            =>          l_po_price_typeTab(j)
8906 		       ,p_wip_resource_id          =>          l_wip_resource_idTab(j)
8907 		       ,p_inventory_item_id        =>          l_inventory_item_idTab(j)
8908 		       ,p_unit_of_measure          =>         l_unit_of_measureTab(j)
8909                ,p_si_assets_addition_flag  =>         l_siaAddFlagTab(j)
8910                       -- ,p_agreement_id             => l_agreement_idTab(j)    --FSIO Changes
8911                           );
8912                     end if;
8913 
8914                  END IF;  ---}
8915 
8916                  --EXIT WHEN TrxRecs%NOTFOUND;
8917                  --END LOOP expenditures ;
8918              END LOOP;   ---}
8919 
8920 
8921 			/***** Bug 4091706 CWK Changes *****/
8922 			--- release_po_line_task_lock; Commented out for bug 4106188
8923 			--- init_po_amt_chk; Commented out for bug 4106188
8924 			/***** Bug 4091706 CWK Changes *****/
8925 
8926 
8927              IF PG_DEBUG = 'Y' THEN
8928              pa_debug.G_err_stage := 'Calling newExpend after TrxRecs loop';
8929                 log_message('log_message: ' || pa_debug.G_err_stage);
8930              END IF;
8931              newExpend( TrxBatch.exp_group_name
8932                      , current_period
8933                      , i
8934                      , prev_denom_currency_code
8935                      , G_accounting_currency_code
8936                      , prev_acct_rate_type
8937                      , prev_acct_rate_date
8938                      , prev_acct_exchange_rate
8939                      , prev_person_type /*cwk */
8940     		     , TrxBatch.batch_name ); -- Bug 3613784 : Performance fix
8941 
8942              /** Bug#3026218 Close the tow cursors that are opened. Removed conditions on
8943 	     *** pre-defined flag
8944 	     **/
8945              If (TrxBatch.system_linkage_function = 'ST') Then
8946                     CLOSE TrxRecs1;
8947              Else
8948                     CLOSE TrxRecs2;
8949              End If;
8950 
8951 
8952           ELSE
8953 
8954              IF PG_DEBUG = 'Y' THEN
8955              pa_debug.G_err_stage := 'Processed the commit size records, exit batches';
8956                 log_message('log_message: ' || pa_debug.G_err_stage);
8957              END IF;
8958 
8959              Exit batches;
8960 
8961           END IF;  ---}
8962 
8963         ELSE
8964 
8965           IF PG_DEBUG = 'Y' THEN
8966           pa_debug.G_err_stage := 'Not able to lock TrxBatch record';
8967              log_message('log_message: ' || pa_debug.G_err_stage);
8968           END IF;
8969 
8970           --Bug 3239369
8971           G_Exit_Main := TRUE;
8972 
8973           Exit batches;
8974 
8975         /***** CWK Changes *****/
8976 
8977 	--- Bug 4091706 release_po_line_task_lock;
8978 	--- BUg 4091706 init_po_amt_chk;
8979 
8980 	/***** CWK Changes *****/
8981 
8982         END IF;  ---}
8983 
8984 
8985         --PA-K Changes: UpdControlProcessed will be done for each loop of TrxBatches
8986         IF PG_DEBUG = 'Y' THEN
8987         pa_debug.G_err_stage := 'Calling UpdControlProcessed';
8988            log_message('log_message: ' || pa_debug.G_err_stage);
8989         END IF;
8990 
8991         UpdControlProcessed( P_TrxSource      => TrxBatch.transaction_source,
8992                              P_BatchName      => TrxBatch.batch_name,
8993                              P_XfaceId        => X_xface_id,
8994                              P_TxnCount       => TrxBatch.transaction_count,
8995                              P_ProcCount      => TrxBatch.processed_count,
8996                              P_BatchProcCount => l_ActualBatchRecCnt,
8997                              p_system_linkage_function     => trxbatch.system_linkage_function);
8998 /* added the parameter system linkage function in the function call above for BUG # 3291066 */
8999 
9000         IF PG_DEBUG = 'Y' THEN
9001            log_message('log_message: ' || 'l_CommitSizeRecCount = ' || l_CommitSizeRecCount || ' l_Batch_Size = ' || l_Batch_Size);
9002         END IF;
9003 
9004         If l_CommitSizeRecCount = l_Batch_Size Then
9005            IF PG_DEBUG = 'Y' THEN
9006               log_message('log_message: ' || 'l_CommitSizeRecCount equal to l_Batch_Size, exit batches');
9007            END IF;
9008            Exit batches;
9009         End If;
9010 
9011 
9012 --Vijay
9013       END LOOP;   ---}
9014 
9015       Close TrxBatches;
9016 
9017       --PA-J Receipt Accrual Changes: Added AP VARIANCE and PO RECEIPT
9018       --PA-K Changes: Using the process_funds_check attributes of the source to perform funds checking
9019       --X_transaction_source in ('AP INVOICE', 'AP VARIANCE', 'PO RECEIPT', 'AP NRTAX', 'PO RECEIPT NRTAX') THEN
9020       IF G_gl_accted_flag = 'Y' AND
9021          G_Process_Funds_Flag = 'Y'  THEN
9022 
9023          IF PG_DEBUG = 'Y' THEN
9024          pa_debug.G_err_stage := 'Calling tieback funds check records';
9025             log_message('log_message: ' || pa_debug.G_err_stage);
9026          END IF;
9027 
9028          IF PG_DEBUG = 'Y' THEN
9029          pa_debug.G_err_stage := 'Tieback Mode = '||X_transaction_source;
9030             log_message('log_message: ' || pa_debug.G_err_stage);
9031          END IF;
9032 
9033          tieback_fc_records (x_return_status => l_return_status,
9034                          p_calling_mode  => X_transaction_source) ;
9035 
9036       END IF;
9037 
9038       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
9039 
9040            raise_application_error(-20001,'Error during Balance Update');
9041            return ;
9042 
9043       end if;
9044 
9045       if l_gms_enabled = 'Y' then
9046          gms_pa_costing_pkg.Tieback_Interface(p_request_id => g_request_id,
9047                                               p_status     => l_return_status);
9048 
9049          if l_return_status <> FND_API.G_RET_STS_SUCCESS then
9050            raise_application_error(-20002,'Error during Grants Tieback process');
9051            return ;
9052          end if;
9053          --
9054          -- BUG : 5389130
9055 	 --       R12.PJ:XB7:DEV:BC: TO TRACK GRANTS INTERFACE ISSUES
9056 	 --
9057          gms_pa_costing_pkg.Net_zero_adls( X_transaction_source,
9058                                            X_batch ,
9059                                            X_xface_id,
9060                                            l_return_status) ;
9061 
9062          if l_return_status <> FND_API.G_RET_STS_SUCCESS then
9063            raise_application_error(-20002,'Error during Grants Tieback process net_zero_adjustment_flag Zero adls');
9064            return ;
9065          end if;
9066 
9067       end if;
9068 
9069       /*PA-K Changes: UpdControlProcessed will be done for each loop of TrxBatches*/
9070 
9071       pa_cc_utils.reset_curr_function;
9072 
9073   EXCEPTION
9074 
9075     WHEN OTHERS THEN
9076        IF PG_DEBUG = 'Y' THEN
9077        pa_debug.G_err_stage := 'In OTHERS exception of import1';
9078           log_message('log_message: ' || pa_debug.G_err_stage,1);
9079        END IF;
9080 
9081        release_po_line_task_lock;  -- bug 3512984
9082 
9083        --Call FC packets update API to update packets to T.
9084        Upd_PktSts_Fatal(p_request_id => G_REQUEST_ID);
9085 
9086        raise;
9087 
9088   END import1;
9089 
9090   PROCEDURE import( X_transaction_source  IN VARCHAR2
9091                   , X_batch               IN VARCHAR2
9092                   , X_xface_id            IN NUMBER
9093                   , X_userid              IN NUMBER
9094                   , X_online_exp_comment  IN VARCHAR2 )
9095   IS
9096 
9097    l_cursor         INTEGER;
9098    l_rows           INTEGER;
9099    l_stmt           VARCHAR2(2000);
9100    l_Exception_Desc VARCHAR2(2000);
9101 
9102    l_run binary_integer;
9103    l_uom_status VARCHAR2(2000) := 'S';
9104 
9105   BEGIN
9106 
9107    --SST changes
9108    <<init_prog>>
9109    BEGIN
9110 
9111      -- SST: Init calls GetTrxSrcInfo
9112      init(P_transaction_source => X_transaction_source);
9113 
9114    EXCEPTION WHEN others THEN
9115      IF PG_DEBUG = 'Y' THEN
9116         log_message('log_message: ' || 'Error in Init procedure',1);
9117         log_message('log_message: ' || 'Stage='||pa_debug.G_err_stage,1);
9118         log_message('log_message: ' || SQLERRM);
9119      END IF;
9120 
9121      raise_application_error(-20001,'Init:'||SQLERRM);
9122    END init_prog;
9123 
9124    IF PG_DEBUG = 'Y' THEN
9125       log_message('log_message: ' || 'Run = '|| l_run);
9126    END IF;
9127 
9128    -- 5235363 R12.PJ:XB4:DEV:APL:EXP ITEM DATE VALIDATIONS FOR SUPPLIER COST.
9129    --
9130    IF (X_transaction_source in (  'AP INVOICE', 'AP NRTAX', 'AP DISCOUNTS', 'INTERCOMPANY_AP_INVOICES',
9131                                   'INTERPROJECT_AP_INVOICES', 'AP VARIANCE', 'AP ERV', 'PO RECEIPT', 'PO RECEIPT NRTAX',
9132 				  'PO RECEIPT PRICE ADJ', 'PO RECEIPT NRTAX PRICE ADJ')) THEN
9133       set_supplier_cost_eidate(X_transaction_source,X_batch,X_xface_id) ;
9134    END IF ;
9135    --
9136    -- 5235363 end of code changes.
9137    /* PA.M PJM Additional Attribute changes: Calling unit of measure insertion api for all INV/WIP/ST sources from manufacturing */
9138 
9139    IF X_transaction_source in ('Inventory', 'Inventory Misc', 'PJM_CSTBP_INV_NO_ACCOUNTS',
9140                                            'PJM_CSTBP_INV_ACCOUNTS', 'Work In Process', 'PJM_CSTBP_ST_NO_ACCOUNTS',
9141                                            'PJM_CSTBP_WIP_NO_ACCOUNTS', 'PJM_CSTBP_ST_ACCOUNTS',
9142                                            'PJM_CSTBP_WIP_ACCOUNTS', 'PJM_NON_CSTBP_ST_ACCOUNTS') THEN
9143 
9144           IF PG_DEBUG = 'Y' THEN
9145                log_message('log_message: ' || 'Calling pa_uom.get_uom for transaction source '|| X_transaction_source);
9146           END IF;
9147 
9148           l_uom_status := Pa_Uom.Get_Uom (X_userid);
9149 
9150           IF l_uom_status <> 'S'  THEN
9151 
9152                IF PG_DEBUG = 'Y' THEN
9153                log_message('log_message: ' || 'Error in Get_Uom procedure',1);
9154                log_message('log_message: ' || l_uom_status);
9155               END IF;
9156 
9157                raise_application_error(-20001,'Get_Uom:'||SQLERRM);
9158 
9159           END IF;
9160 
9161      END IF; /* X_transaction_source */
9162 
9163    <<main_loop>>
9164    LOOP
9165 
9166     G_IterationNum := G_IterationNum + 1;
9167     IF PG_DEBUG = 'Y' THEN
9168        log_message('log_message: ' || 'G_IterationNum = '|| G_IterationNum);
9169     END IF;
9170 
9171     <<pre_import>>
9172     BEGIN
9173       IF PG_DEBUG = 'Y' THEN
9174       pa_debug.G_err_Stage := 'Calling pre_import extension';
9175          log_message('log_message: ' || pa_debug.G_err_Stage);
9176       END IF;
9177 
9178       pa_trx_import.pre_import(P_transaction_source => X_transaction_source,
9179                              P_batch => X_batch,
9180                              P_xface_id => X_xface_id,
9181                              P_user_id => X_userid);
9182 
9183       /* PA-K Changes: Remove the inter phase commits */
9184 
9185       /* PA-K Changes: Commenting the call to count_status
9186          Report will directly select the counts from the interface table */
9187 
9188     EXCEPTION WHEN others THEN
9189       IF PG_DEBUG = 'Y' THEN
9190          log_message('log_message: ' || 'Error in pre_import procedure',1);
9191          log_message('log_message: ' || 'Stage='||pa_debug.G_err_stage,1);
9192          log_message('log_message: ' || SQLERRM);
9193       END IF;
9194       raise_application_error(-20002,'pre_import:'||SQLERRM);
9195     END pre_import;
9196 
9197     <<Import1_prog>>
9198     BEGIN
9199    	 pa_trx_import.import1(X_transaction_source
9200                   , X_batch
9201                   , X_xface_id
9202                   , X_userid
9203                   , X_online_exp_comment);
9204 
9205       /* PA-K Changes: Remove the inter phase commits */
9206 
9207       /* PA-K Changes: Commenting the call to count_status
9208          Report will directly select the counts from the interface table */
9209 
9210       IF PG_DEBUG = 'Y' THEN
9211       pa_debug.G_err_stage := 'After import1';
9212          log_message('log_message: ' || pa_debug.G_err_stage);
9213       END IF;
9214 
9215 
9216     EXCEPTION WHEN others THEN
9217 
9218        IF PG_DEBUG = 'Y' THEN
9219           log_message('log_message: ' || 'Error in import1 procedure',1);
9220           log_message('log_message: ' || 'Stage='||pa_debug.G_err_stage,1);
9221           log_message('log_message: ' || SQLERRM);
9222        END IF;
9223 
9224        If X_transaction_source = 'ORACLE TIME AND LABOR' Then
9225 
9226                BEGIN
9227 
9228                       IF PG_DEBUG = 'Y' THEN
9229                       pa_debug.G_err_stage := 'Calling eception handler for OTL';
9230                          log_message('log_message: ' || pa_debug.G_err_stage,1);
9231                       END IF;
9232 
9233 
9234                       -- Get cursor handle
9235                       l_cursor  := dbms_sql.open_cursor;
9236 
9237                       l_Exception_Desc := 'Error in Projects Import routine: ' || pa_debug.G_err_stage || ' : ' || SQLERRM;
9238 
9239                       -- Associate a sql statement with the cursor.
9240 
9241                       --FP M OTL API changes (bug 3600642)
9242                       --replaced Hxc_Generic_Retrieval_Pkg with HXC_INTEGRATION_LAYER_V1_GRP
9243                       --l_stmt    := 'BEGIN Hxc_Generic_Retrieval_Pkg.Update_Transaction_Status ( ' ||
9244                       l_stmt    := 'BEGIN HXC_INTEGRATION_LAYER_V1_GRP.Update_Transaction_Status ( ' ||
9245                                    ' P_Process               => :process, '||
9246                                    ' P_Status                => :status, '||
9247                                    ' P_Exception_Description => :Exception_Desc );'||
9248                                    'END;';
9249 
9250                       IF PG_DEBUG = 'Y' THEN
9251                       pa_debug.G_err_stage := 'After constructing the  dynamic sql OTL';
9252                          log_message('log_message: ' || pa_debug.G_err_stage,1);
9253                       END IF;
9254 
9255                       -- parse the sql statement to check for any syntax or symantic errors
9256 
9257                       dbms_sql.parse(l_cursor,l_stmt,dbms_sql.native);
9258 
9259                       -- before executing the sql statement bind the variables
9260                       dbms_sql.bind_variable(l_cursor,':process',
9261                                              'Projects Retrieval Process');
9262 
9263                       dbms_sql.bind_variable(l_cursor,'status',
9264                                              'ERRORS');
9265 
9266                       dbms_sql.bind_variable(l_cursor,'Exception_Desc',
9267                                              l_Exception_Desc);
9268 
9269 
9270                       l_rows := dbms_sql.execute(l_cursor);
9271                       IF PG_DEBUG = 'Y' THEN
9272                       pa_debug.G_err_stage := 'After executing the  dynamic sql OTL';
9273                          log_message('log_message: ' || pa_debug.G_err_stage,1);
9274                       END IF;
9275 
9276                 EXCEPTION
9277                         When Others Then
9278                                 IF PG_DEBUG = 'Y' THEN
9279                                 pa_debug.G_err_stage := 'In excp of the dynamic sql executing OTL';
9280                                    log_message('log_message: ' || pa_debug.G_err_stage,1);
9281                                 END IF;
9282 
9283                                 dbms_sql.close_cursor(l_cursor);
9284                                 RAISE;
9285 
9286                 END;
9287 
9288        End If;
9289 
9290 
9291 
9292 
9293       raise_application_error(-20003,'import1_prog:'||SQLERRM||pa_debug.G_err_stage);
9294 
9295     END import1_prog;
9296 
9297 
9298 	/***** CWK Changes *****/
9299 	 release_po_line_task_lock;
9300 	 init_po_amt_chk;
9301 	/***** CWK Changes *****/
9302 
9303 
9304 
9305     <<post_import>>
9306     BEGIN
9307       IF PG_DEBUG = 'Y' THEN
9308       pa_debug.G_err_Stage := 'Calling post_import extension';
9309          log_message('log_message: ' || pa_debug.G_err_Stage);
9310       END IF;
9311 
9312       pa_trx_import.post_import(P_transaction_source => X_transaction_source,
9313                              P_batch => X_batch,
9314                              P_xface_id => X_xface_id,
9315                              P_user_id => X_userid);
9316 
9317       /* PA-K Changes: Remove the inter phase commits */
9318 
9319       /* PA-K Changes: Commenting the call to count_status
9320          Report will directly select the counts from the interface table */
9321 
9322     EXCEPTION WHEN others THEN
9323        IF PG_DEBUG = 'Y' THEN
9324           log_message('log_message: ' || 'Error in post import procedure',1);
9325           log_message('log_message: ' || SQLERRM);
9326           log_message('log_message: ' || 'Stage='||pa_debug.G_err_stage,1);
9327        END IF;
9328       raise_application_error(-20004,'post_import:'||SQLERRM);
9329     END post_import;
9330 
9331   /* Bug 6998382 Moved the condition to exit the import1 procedure here so that the post processing client extension
9332 					is called for the last batch as well */
9333     If (G_Exit_Main) Then
9334        IF PG_DEBUG = 'Y' THEN
9335        pa_debug.G_err_stage := 'G_Exit_Main is true, exit main loop';
9336           log_message('log_message: ' || pa_debug.G_err_stage,1);
9337        END IF;
9338        Exit main_loop;
9339     End If;
9340   /* Bug 6998382 End */
9341     COMMIT;
9342 
9343    END LOOP;
9344 
9345    IF PG_DEBUG = 'Y' THEN
9346       log_message('log_message: ' || 'End of Procedure Import');
9347    END IF;
9348 
9349    /* PA-K Changes: Commenting the call to update_status_counter
9350          Report will directly select the counts from the interface table */
9351 
9352   END import;
9353 
9354   -- SST changes: New APIs
9355 
9356   PROCEDURE init(P_transaction_source IN VARCHAR2) IS
9357 
9358    --l_debug_mode  VARCHAR2(1);
9359   BEGIN
9360 
9361    -- Get the debug mode, if the debug is turned on then write
9362    -- log messages to the log file.
9363 
9364    fnd_profile.get('PA_DEBUG_MODE',G_debug_mode);
9365    G_debug_mode := NVL(G_debug_mode, 'N');
9366 
9367    pa_debug.set_process(x_process => 'PLSQL',
9368                         x_debug_mode => G_debug_mode);
9369 
9370    pa_cc_utils.log_message('Debug Mode = '||G_debug_mode,1);
9371    pa_cc_utils.set_curr_function('pa_trx_import.init');
9372 
9373    IF PG_DEBUG = 'Y' THEN
9374    pa_debug.G_err_Stage := 'retriving transaction source';
9375       log_message('log_message: ' || pa_debug.G_err_Stage);
9376    END IF;
9377 
9378    -- Transaction import program is always run for 1 transaction source
9379    -- get the transaction source info and store in pkg body global variables
9380 
9381    GetTrxSrcInfo ( X_trx_src  => P_transaction_source );
9382 
9383    --Pa.K Changes: For performance moved the Multi-Org check to the init procedure.
9384    G_Morg := pa_utils.pa_morg_implemented;
9385 
9386    -- MOAC changes. populate the operating unit org id variable.
9387    g_moac_org_id := pa_moac_utils.get_current_org_id ;
9388 
9389    IF (G_accounting_currency_code IS NULL) THEN
9390        GetImpCurrInfo;
9391    END IF;
9392 
9393    pa_cc_utils.reset_curr_function;
9394 
9395   END init;
9396 
9397   PROCEDURE execute_import_extensions(P_program_name IN VARCHAR2,
9398                                  P_transaction_source  IN VARCHAR2,
9399                                  P_batch               IN VARCHAR2,
9400                                  P_user_id             IN NUMBER,
9401                                  P_xface_id            IN NUMBER) IS
9402 
9403    l_cursor   INTEGER;
9404    l_rows     INTEGER;
9405    l_stmt     VARCHAR2(2000);
9406 
9407   BEGIN
9408 
9409    -- If the transaction source has a extension associated with it then
9410    -- execute the extn, else just exit with out doing anything.
9411    -- P_program_name is the name of the extension API.
9412    -- For instance 'PA_SELF_SERVICE_PVT.Upload_SS_Timecard'
9413    IF ( P_program_name IS NOT NULL ) THEN
9414 
9415       -- Get cursor handle
9416       l_cursor  := dbms_sql.open_cursor;
9417 
9418       -- Associate a sql statement with the cursor.
9419 
9420       l_stmt    := 'BEGIN '||P_program_name||
9421                        '(P_transaction_source =>:transaction_source,'||
9422                          ' P_batch => :batch,'||
9423                          ' P_user_id => :user_id,'||
9424                          ' P_xface_id => :xface_id);'||
9425                    ' END;';
9426 
9427       -- parse the sql statemnt to check for any syntax or symantic errors
9428 
9429       dbms_sql.parse(l_cursor,l_stmt,dbms_sql.native);
9430 
9431       -- before executing the sql statement bind the variables
9432 
9433       dbms_sql.bind_variable(l_cursor,'transaction_source',
9434                                              P_transaction_source);
9435       dbms_sql.bind_variable(l_cursor,'batch',
9436                                              P_batch);
9437       dbms_sql.bind_variable(l_cursor,'user_id',
9438                                              P_user_id);
9439       dbms_sql.bind_variable(l_cursor,'xface_id',
9440                                              P_xface_id);
9441 
9442       l_rows := dbms_sql.execute(l_cursor);
9443 
9444    END IF;
9445 
9446   EXCEPTION WHEN others THEN
9447    dbms_sql.close_cursor(l_cursor);
9448    raise;
9449   END execute_import_extensions;
9450 
9451   PROCEDURE pre_import(P_transaction_source IN VARCHAR2,
9452                         P_batch              IN VARCHAR2,
9453                         P_xface_id           IN NUMBER,
9454                         P_user_id            IN NUMBER) IS
9455   BEGIN
9456 
9457      pa_trx_import.execute_import_extensions( P_program_name => G_pre_processing_extn,
9458                                 P_transaction_source => P_transaction_source,
9459                                 P_batch => P_batch,
9460                                 P_xface_id  => P_xface_id,
9461                                 P_user_id  => P_user_id);
9462 
9463   EXCEPTION WHEN others THEN
9464    raise;
9465   END pre_import;
9466 
9467   PROCEDURE post_import(P_transaction_source IN VARCHAR2,
9468                         P_batch              IN VARCHAR2,
9469                         P_xface_id           IN NUMBER,
9470                         P_user_id            IN NUMBER) IS
9471   BEGIN
9472 
9473      pa_trx_import.execute_import_extensions( P_program_name => G_post_processing_extn,
9474                                 P_transaction_source => P_transaction_source,
9475                                 P_batch => P_batch,
9476                                 P_xface_id  => P_xface_id,
9477                                 P_user_id  => P_user_id);
9478 
9479 
9480   EXCEPTION WHEN others THEN
9481    raise;
9482   END post_import;
9483 
9484   -- Changes for New TXN Codes
9485   --- New APIs
9486   ----------------------------------------------------------------------
9487   -- This new API counts the number of transactions which passed or failed
9488   -- certain phase, depending on the parameter
9489 
9490   PROCEDURE count_status(P_phase IN VARCHAR2,
9491                        P_xface_id IN NUMBER,
9492                        P_sucess_counter OUT NOCOPY NUMBER,
9493                        P_failed_counter OUT NOCOPY NUMBER) IS
9494 	v_sucess VARCHAR2(2);
9495 	v_failed VARCHAR2(2);
9496 
9497   BEGIN
9498 	IF (P_phase = 'PREIMPORT') THEN
9499       IF (G_pre_processing_extn IS NULL) THEN
9500          P_sucess_counter := 0;
9501          P_failed_counter := 0;
9502          return;
9503       ELSE
9504 		   v_sucess := 'P';
9505 		   v_failed := 'PR';
9506       END IF;
9507 	ELSIF (P_phase = 'IMPORT') THEN
9508 		IF (G_post_processing_extn IS NULL) THEN
9509 			v_sucess := 'A';
9510 		ELSE
9511 			v_sucess := 'I';
9512 		END IF;
9513 		v_failed := 'R';
9514 	ELSIF (P_phase = 'POSTIMPORT') THEN
9515       IF (G_post_processing_extn IS NULL) THEN
9516          P_sucess_counter := 0;
9517          P_failed_counter := 0;
9518          return;
9519       ELSE
9520 		   v_sucess := 'A';
9521 		   v_failed := 'PO';
9522       END IF;
9523 	END IF;
9524 
9525 	SELECT COUNT(DECODE(transaction_status_code, v_sucess, 1, NULL)),
9526 	       COUNT(DECODE(transaction_status_code, v_failed, 1, NULL))
9527 	INTO P_sucess_counter, P_failed_counter
9528    FROM pa_transaction_interface
9529    WHERE interface_id = P_xface_id
9530    AND transaction_status_code IN (v_sucess, v_failed);
9531 
9532 	EXCEPTION
9533    	WHEN OTHERS THEN
9534 			RAISE;
9535   END;  -- procedure count_status
9536 
9537   -- This API updates the counters in pa_transaction_xface_ctrl_all table.
9538   -- It keeps one set of counters for each concurrent request. Within a
9539   -- concurrent request, it does not keep track of counts at the system
9540   -- linkage function modularity.
9541 
9542   PROCEDURE update_status_counter(P_xface_id IN NUMBER) IS
9543   BEGIN
9544 
9545 	UPDATE pa_transaction_xface_ctrl_all
9546    SET pre_import_reject_count = G_PRE_IMPORT_REJECT_COUNT,
9547        pre_import_success_count = G_PRE_IMPORT_SUCCESS_COUNT,
9548        import_reject_count     = G_IMPORT_REJECT_COUNT,
9549        import_success_count     = G_IMPORT_SUCCESS_COUNT,
9550        post_import_reject_count = G_POST_IMPORT_REJECT_COUNT,
9551        post_import_success_count = G_POST_IMPORT_SUCCESS_COUNT
9552 	WHERE interface_id = P_xface_id;
9553 
9554   EXCEPTION
9555 		WHEN OTHERS THEN
9556 			RAISE;
9557   END;
9558 
9559   -- Bug 5550268 : Added comments to this procedure for clarity
9560   PROCEDURE tieback_fc_records ( x_return_status   OUT NOCOPY VARCHAR2,
9561                                p_calling_mode    IN  VARCHAR2) IS
9562 
9563    -- Bug 5560524 : Modified the following cursor to also fetch AP ERV records
9564    -- Cursor to fetch AP VARIANCE,PO RECEIPT and PO RECEIPT NRTAX records
9565    cursor  select_bc_packets(p_calling_mode in varchar2) is
9566          select a.packet_id,
9567                 a.document_header_id,
9568                 a.document_distribution_id,
9569                 a.budget_ccid,
9570                 b.cdl_system_reference4,
9571                 a.project_id,
9572 		a.budget_line_id ,
9573 		a.budget_version_id
9574            from pa_bc_packets a,
9575                 pa_transaction_interface b
9576           where a.txn_interface_id = b.txn_interface_id
9577 	    and a.document_header_id = b.cdl_system_reference2
9578             and a.document_distribution_id = decode(p_calling_mode, 'AP VARIANCE',b.cdl_system_reference5
9579 	                                                          , 'AP ERV',b.cdl_system_reference5
9580 	                                                          , b.cdl_system_reference3)
9581             and a.document_type = decode(p_calling_mode, 'AP VARIANCE', 'AP', 'AP ERV', 'AP' , 'PO RECEIPT', 'PO', 'PO RECEIPT NRTAX', 'PO')
9582             and a.parent_bc_packet_id is null
9583             and b.transaction_status_code = 'A'
9584             and a.request_id = g_request_id
9585             and to_char(a.request_id) = b.orig_transaction_reference
9586             and a.status_code = 'P'
9587             and b.transaction_source = p_calling_mode;
9588 
9589      -- Cursor to fetch expenditure records associated with AP DISCOUNTS records
9590      cursor select_disc_packets(p_calling_mode in varchar2) is
9591          select a.packet_id,
9592                 b.cdl_system_reference2,
9593 		b.cdl_system_reference5,
9594                 a.budget_ccid,
9595                 b.cdl_system_reference4,
9596                 a.project_id,
9597 		a.budget_line_id ,
9598 		a.budget_version_id
9599            from pa_bc_packets a,
9600                 pa_transaction_interface b,-- moac changes
9601                 pa_expenditure_items c
9602           where a.txn_interface_id = b.txn_interface_id
9603 	    and a.document_header_id = b.expenditure_item_id
9604             and a.document_distribution_id = 1
9605             and a.document_type = decode(p_calling_mode, 'AP DISCOUNTS', 'EXP')
9606             and a.parent_bc_packet_id is null
9607             and b.transaction_status_code = 'A'
9608             and a.request_id = g_request_id
9609             and a.request_id = b.orig_transaction_reference
9610             and a.status_code = 'P'
9611             and b.expenditure_item_id = c.expenditure_item_id
9612             and b.transaction_source = p_calling_mode;
9613 
9614    -- Cursor to fetch AP INVOICE/AP NRTAX records
9615    -- Second Union all is used to fetch PO records relieved while interfacing AP invoice distributions
9616    -- Eg : AP matched to CWK PO , AP matched to accrue on receipt
9617    cursor  rcpt_acc_ap_pkts(p_calling_mode in varchar2) is
9618          select a.packet_id ,
9619                 a.document_header_id ,
9620                 a.document_distribution_id ,
9621                 a.budget_ccid,
9622                 b.cdl_system_reference4,
9623                 a.project_id,
9624 		a.budget_line_id ,
9625 		a.budget_version_id
9626            from pa_bc_packets a,
9627                 pa_transaction_interface b -- moac changes
9628           where a.txn_interface_id = b.txn_interface_id
9629 	    and a.document_header_id = b.cdl_system_reference2
9630             and a.document_distribution_id = b.cdl_system_reference5
9631             and a.document_type = 'AP'
9632             and a.parent_bc_packet_id is null
9633             and b.transaction_status_code = 'A'
9634             and a.request_id = g_request_id
9635             and b.transaction_source = p_calling_mode
9636             and a.status_code = 'P'
9637          union
9638          select a.packet_id ,
9639                 to_number(b.cdl_system_reference2) ,
9640                 to_number(b.cdl_system_reference5) ,
9641                 a.budget_ccid,
9642                 b.cdl_system_reference4,
9643                 a.project_id,
9644 		a.budget_line_id ,
9645 		a.budget_version_id
9646            from pa_bc_packets a,
9647                 pa_transaction_interface b, -- moac changes
9648                 ap_invoice_distributions c,
9649                 po_distributions d
9650           where a.txn_interface_id = b.txn_interface_id
9651 	    and a.document_header_id = d.po_header_id
9652             and a.document_distribution_id = d.po_distribution_id
9653             and c.invoice_id = b.cdl_system_reference2
9654 	    and c.invoice_distribution_id = b.cdl_system_reference5
9655             and c.po_distribution_id = d.po_distribution_id
9656             and a.document_type = 'PO'
9657             and a.parent_bc_packet_id is null
9658             and b.transaction_status_code = 'A'
9659             and a.request_id = g_request_id
9660             and b.transaction_source IN  ('AP INVOICE','AP NRTAX') -- Bug 5550268
9661             and a.status_code = 'P';
9662 
9663    CURSOR c_exp_item_id IS
9664    SELECT txn.expenditure_item_id ,
9665           pkt.bc_packet_id
9666      FROM pa_transaction_interface txn,-- moac changes
9667           pa_bc_packets pkt
9668     WHERE pkt.txn_interface_id = txn.txn_interface_id
9669       and pkt.request_id = g_request_id
9670       and txn.transaction_status_code = 'A'
9671       and pkt.status_code = 'P'
9672       and pkt.document_type ='EXP'
9673       and txn.adjusted_expenditure_item_id IS NOT NULL
9674       and pkt.document_header_id <> txn.expenditure_item_id;
9675 
9676    l_packet_id          NUMBER ;
9677    l_sys_ref2           NUMBER ;
9678    l_sys_ref3           NUMBER ;
9679    l_sys_ref4           VARCHAR2(30) ;
9680    l_doc_dist_id        pa_bc_packets.document_distribution_id%TYPE ;
9681    l_ExpItemId_temp_Tab PA_PLSQL_DATATYPES.Num15TabTyp;
9682    l_budget_ccid        NUMBER ;
9683    l_old_pkt            NUMBER := 0;
9684    l_project_id         NUMBER ;
9685    l_bc_pkt_id          pa_bc_packets.bc_packet_id%TYPE;
9686    l_exp_item_id        pa_expenditure_items_all.expenditure_item_id%TYPE;
9687    l_budget_line_id     pa_bc_packets.budget_line_id%TYPE;
9688    l_budget_version_id  pa_bc_packets.budget_version_id%TYPE;
9689 
9690 
9691    PROCEDURE Upd_Sts_Enc_Bal(p_packet_id in number,
9692                     x_return_status out NOCOPY varchar2 ) is
9693 
9694    l_fc_return_status   VARCHAR2(10);
9695 
9696    -- Bug 5372480 : Removed the cursor c_bcpkt_projects as we already have p_packet_id.
9697 
9698    l_records_updated number;
9699 
9700    BEGIN
9701 
9702     IF PG_DEBUG = 'Y' THEN
9703        log_message('log_message: ' || 'Inside Upd_Sts_Enc_Bal');
9704     END IF;
9705 
9706     -- R12 funds management : 'C' status code has been obsoleted
9707     l_records_updated := 0;
9708 
9709     -- Bug 5372480 : Records of pa_bc_packets are marked to 'A' status before calling pa_funds_control_pkg.UPD_BDGT_ENCUM_BAL
9710     -- because the CURSOR bdgt_encum_details in pa_funds_control_pkg.UPD_BDGT_ENCUM_BAL queries for 'A' status records from pa_bc_packets.
9711     -- Also there is no need to loop for distinct projects earlier being fetched by cursor c_bcpkt_projects.
9712 
9713       update pa_bc_packets a
9714          set a.status_code = 'A'
9715        where a.packet_id = p_packet_id
9716          and a.request_id = g_request_id
9717          and a.status_code = 'P';
9718 
9719       l_records_updated := l_records_updated + SQL%ROWCOUNT;
9720 
9721 
9722     IF PG_DEBUG = 'Y' THEN
9723     pa_debug.G_err_stage := 'Updated PacketId [' || p_packet_id || ']-' || l_records_updated || ' records to A/C';
9724        log_message('log_message: ' || pa_debug.G_err_Stage);
9725     END IF;
9726 
9727 
9728     IF PG_DEBUG = 'Y' THEN
9729        log_message('log_message: ' || 'Calling pa_funds_control_pkg.UPD_BDGT_ENCUM_BAL');
9730     END IF;
9731 
9732     -- set the return status to success
9733     x_return_status := FND_API.G_RET_STS_SUCCESS;
9734 
9735     pa_funds_control_pkg.UPD_BDGT_ENCUM_BAL(
9736                        p_packet_id       =>  p_packet_id,
9737                        p_calling_module  =>  'TRXIMPORT',
9738                        p_mode          =>  'R',
9739                        p_packet_status =>  'S',
9740                        x_return_status =>  l_fc_return_status);
9741 
9742     if l_fc_return_status <> FND_API.G_RET_STS_SUCCESS then
9743        x_return_status := l_fc_return_status ;
9744        return;
9745     end if;
9746 
9747 
9748    EXCEPTION
9749         WHEN OTHERS THEN
9750         x_return_status  := fnd_api.g_ret_sts_unexp_error;
9751         RAISE;
9752 
9753    END Upd_Sts_Enc_Bal;
9754 
9755   BEGIN
9756 
9757     pa_cc_utils.set_curr_function('tieback_fc_records');
9758 
9759     IF PG_DEBUG = 'Y' THEN
9760        pa_debug.G_err_stage := 'Start of tieback_fc_records';
9761        log_message('log_message: ' || pa_debug.G_err_Stage);
9762     END IF;
9763 
9764     -- set the return status to success
9765     x_return_status := FND_API.G_RET_STS_SUCCESS;
9766 
9767     IF PG_DEBUG = 'Y' THEN
9768        log_message('log_message: ' || 'p_calling_mode = ' || p_calling_mode);
9769     END IF;
9770 
9771     open c_exp_item_id;
9772       loop
9773        fetch c_exp_item_id
9774         into l_exp_item_id,
9775 	     l_bc_pkt_id;
9776 
9777        if c_exp_item_id%notfound then
9778            IF PG_DEBUG = 'Y' THEN
9779               pa_debug.G_err_stage := 'No packets found for updating of exp item id , exit';
9780               log_message('log_message: ' || pa_debug.G_err_Stage);
9781            END IF;
9782            exit;
9783        end if;
9784 
9785       UPDATE pa_bc_packets
9786          SET document_header_id = l_exp_item_id,
9787 	     reference1         = l_exp_item_id
9788        WHERE bc_packet_id = l_bc_pkt_id;
9789      END LOOP;
9790     CLOSE c_exp_item_id;
9791 
9792     -- Bug 5560524 : Added 'AP ERV' to the following condition.
9793     if (p_calling_mode in ('AP VARIANCE', 'AP ERV' , 'PO RECEIPT', 'PO RECEIPT NRTAX')) Then
9794 
9795       IF PG_DEBUG = 'Y' THEN
9796          pa_debug.G_err_stage := 'Before opening the cursor select_bc_packets for ' || p_calling_mode;
9797          log_message('log_message: ' || pa_debug.G_err_Stage);
9798       END IF;
9799 
9800       open select_bc_packets(p_calling_mode) ;
9801 
9802       loop
9803 
9804        fetch select_bc_packets
9805         into l_packet_id,
9806              l_sys_ref2,
9807              l_doc_dist_id,
9808              l_budget_ccid,
9809              l_sys_ref4,
9810              l_project_id,
9811     	     l_budget_line_id ,
9812 	     l_budget_version_id;
9813 
9814        if select_bc_packets%notfound then
9815            IF PG_DEBUG = 'Y' THEN
9816                pa_debug.G_err_stage := 'No packets found, exit';
9817               log_message('log_message: ' || pa_debug.G_err_Stage);
9818            END IF;
9819            exit ;
9820            end if;
9821 
9822        IF PG_DEBUG = 'Y' THEN
9823        pa_debug.G_err_stage := 'Old Pkt = ' || l_old_pkt
9824                               || ' Packet Id = ' || l_packet_id
9825                               || ' Ref2 = ' || l_sys_ref2
9826                               || ' document dist id = ' || l_doc_dist_id
9827                               || ' Ref4 = ' || l_sys_ref4;
9828           log_message('log_message: ' || pa_debug.G_err_Stage);
9829        END IF;
9830 
9831        IF (l_old_pkt <> l_packet_id) THEN
9832           IF PG_DEBUG = 'Y' THEN
9833              pa_debug.G_err_stage := 'Calling Upd_Sts_Enc_Bal';
9834              log_message('log_message: ' || pa_debug.G_err_Stage);
9835           END IF;
9836 
9837           Upd_Sts_Enc_Bal(p_packet_id   => l_packet_id,
9838                        x_return_status =>  x_return_status);
9839 
9840           IF PG_DEBUG = 'Y' THEN
9841              pa_debug.G_err_stage := 'After Calling Upd_Sts_Enc_Bal';
9842              log_message('log_message: ' || pa_debug.G_err_Stage);
9843           END IF;
9844        END IF;
9845 
9846        IF (pa_funds_control_utils.get_bdgt_link(l_Project_Id, 'STD') = 'Y') THEN
9847 
9848           -- BUG : 4885459 : APPSPERF:PA:PJC: PA_TRX_IMPORT : PERF. REPOSITORY EXCEPTIONS
9849 
9850 	  l_ExpItemId_temp_Tab.DELETE ;
9851 
9852 	  select ei.expenditure_item_id
9853             bulk collect into l_ExpItemId_temp_Tab
9854 	    from pa_expenditure_items_all ei
9855 	   where ei.document_header_id = l_sys_ref2
9856              and ei.document_distribution_id = DECODE(p_calling_mode,'AP VARIANCE',l_doc_dist_id,'AP ERV',l_doc_dist_id,l_sys_ref4);  -- Bug 5560524
9857 
9858 	  IF l_ExpItemId_temp_Tab.COUNT > 0 THEN
9859 
9860 	     FORALL j in l_ExpItemId_temp_Tab.first..l_ExpItemId_temp_Tab.last
9861                    update pa_cost_distribution_lines_all cdl
9862                       set cdl.Budget_CCID         = l_budget_ccid    ,
9863                           cdl.encumbrance_type_id = pa_funds_control_utils.get_encum_type_id(cdl.project_id,'STD'),
9864                           cdl.budget_line_id      = DECODE(cdl.budget_line_id,NULL,l_budget_line_id,cdl.budget_line_id),
9865                           cdl.budget_version_id   = DECODE(cdl.budget_version_id,NULL,l_budget_version_id,cdl.budget_version_id)
9866                     Where cdl.budget_ccid is null
9867 	              and cdl.expenditure_item_id  = l_ExpItemId_temp_Tab(j) ;
9868 	  END IF ;
9869 
9870          IF PG_DEBUG = 'Y' THEN
9871             pa_debug.G_err_stage := 'Updated Ref2-Ref3 [' || l_sys_ref2 || ',' || l_doc_dist_id || ',' || l_sys_ref4 || ']-'
9872                                                      || SQL%ROWCOUNT || ' Budget CCID';
9873             log_message('log_message: ' || pa_debug.G_err_Stage);
9874          END IF;
9875 
9876        END IF;
9877 
9878        l_old_pkt := l_packet_id;
9879 
9880       end loop;
9881 
9882       /* start of bug 3905744 first part */
9883       IF select_bc_packets%isopen THEN
9884       close select_bc_packets;
9885       END IF;
9886      /* end of bug 3905744 first part */
9887      --bug  3905744 closed after the second part
9888 
9889     elsif (p_calling_mode in ('AP INVOICE', 'AP NRTAX') ) then
9890 
9891       IF PG_DEBUG = 'Y' THEN
9892       pa_debug.G_err_stage := 'Before opening the cursor rcpt_acc_ap_pkts for ' || p_calling_mode;
9893          log_message('log_message: ' || pa_debug.G_err_Stage);
9894       END IF;
9895 
9896       open rcpt_acc_ap_pkts(p_calling_mode);
9897 
9898       loop
9899 
9900        fetch rcpt_acc_ap_pkts
9901         into l_packet_id,
9902              l_sys_ref2,
9903              l_doc_dist_id,
9904              l_budget_ccid,
9905              l_sys_ref4,
9906              l_project_id,
9907     	     l_budget_line_id ,
9908 	     l_budget_version_id;
9909 
9910        if rcpt_acc_ap_pkts%notfound then
9911            IF PG_DEBUG = 'Y' THEN
9912            pa_debug.G_err_stage := 'No packets found, exit';
9913               log_message('log_message: ' || pa_debug.G_err_Stage);
9914            END IF;
9915            exit;
9916        end if;
9917 
9918        IF PG_DEBUG = 'Y' THEN
9919        pa_debug.G_err_stage := 'Old Pkt = ' || l_old_pkt
9920                               || ' Packet Id = ' || l_packet_id
9921                               || ' Ref2 = ' || l_sys_ref2
9922                               || ' Document dist Id = ' || l_doc_dist_id
9923                               || ' Ref4 = ' || l_sys_ref4;
9924           log_message('log_message: ' || pa_debug.G_err_Stage);
9925        END IF;
9926 
9927        IF (l_old_pkt <> l_packet_id) THEN
9928           IF PG_DEBUG = 'Y' THEN
9929              pa_debug.G_err_stage := 'Calling Upd_Sts_Enc_Bal';
9930              log_message('log_message: ' || pa_debug.G_err_Stage);
9931           END IF;
9932 
9933           Upd_Sts_Enc_Bal(p_packet_id   => l_packet_id,
9934                        x_return_status =>  x_return_status);
9935 
9936           IF PG_DEBUG = 'Y' THEN
9937              pa_debug.G_err_stage := 'After Calling Upd_Sts_Enc_Bal';
9938              log_message('log_message: ' || pa_debug.G_err_Stage);
9939           END IF;
9940        END IF;
9941 
9942        IF (pa_funds_control_utils.get_bdgt_link(l_Project_Id, 'STD') = 'Y') THEN
9943 
9944           -- BUG : 4885459  APPSPERF:PA:PJC: PA_TRX_IMPORT : PERF. REPOSITORY EXCEPTIONS
9945           l_ExpItemId_temp_Tab.DELETE ;
9946 
9947 	  select ei.expenditure_item_id
9948             bulk collect into l_ExpItemId_temp_Tab
9949 	    from pa_expenditure_items_all ei
9950 	   where ei.document_header_id = l_sys_ref2
9951 	     and ei.document_distribution_id = l_doc_dist_id;  /*REL12 : AP Lines Uptake changes*/
9952 
9953 	  IF l_ExpItemId_temp_Tab.COUNT > 0 THEN
9954 
9955 	     FORALL j in l_ExpItemId_temp_Tab.first..l_ExpItemId_temp_Tab.last
9956                     update pa_cost_distribution_lines_all cdl
9957                        set cdl.Budget_CCID = l_budget_ccid    ,
9958       	                  -- R12 Funds Management Uptake : Modified and added below columns logic
9959 	                  cdl.encumbrance_type_id = pa_funds_control_utils.get_encum_type_id(cdl.project_id,'STD'),
9960                           cdl.budget_line_id      = DECODE(cdl.budget_line_id,NULL,l_budget_line_id,cdl.budget_line_id),
9961                           cdl.budget_version_id = DECODE(cdl.budget_version_id,NULL,l_budget_version_id,cdl.budget_version_id)
9962                     Where cdl.budget_ccid         is null
9963    	              and cdl.expenditure_item_id = l_ExpItemId_temp_Tab(j) ; /*REL12 : AP Lines Uptake changes*/
9964 	  END IF ;
9965 
9966          IF PG_DEBUG = 'Y' THEN
9967             pa_debug.G_err_stage := 'Updated Ref2-Ref3 [' || l_sys_ref2 || ',' || l_doc_dist_id || ',' || l_sys_ref4 || ']-'
9968                                                      || SQL%ROWCOUNT || ' Budget CCID';
9969             log_message('log_message: ' || pa_debug.G_err_Stage);
9970          END IF;
9971 
9972        END IF;
9973 
9974        l_old_pkt := l_packet_id;
9975 
9976       end loop;
9977 
9978       /* Start of bug 3905744 second part */
9979       IF rcpt_acc_ap_pkts%isopen THEN
9980       close rcpt_acc_ap_pkts;
9981       END IF;
9982      /* End of bug 3905744 second part */
9983      --bug 3905744 is ended here
9984      --closed cursor select_bc_packets after use in the first part
9985      --closed cursor rcpt_acc_ap_pkts after use in the second part
9986 
9987     elsif (p_calling_mode in ('AP DISCOUNTS')) Then --Bug 2339216
9988 
9989       IF PG_DEBUG = 'Y' THEN
9990          pa_debug.G_err_stage := 'Before opening the cursor select_disc_packets for ' || p_calling_mode;
9991          log_message('log_message: ' || pa_debug.G_err_Stage);
9992       END IF;
9993 
9994       open select_disc_packets(p_calling_mode) ;
9995 
9996       loop
9997 
9998        fetch select_disc_packets
9999         into l_packet_id,
10000              l_sys_ref2,
10001              l_doc_dist_id,
10002              l_budget_ccid,
10003              l_sys_ref4,
10004              l_project_id,
10005     	     l_budget_line_id ,
10006 	     l_budget_version_id;
10007 
10008        if select_disc_packets%notfound then
10009            IF PG_DEBUG = 'Y' THEN
10010               pa_debug.G_err_stage := 'No discount packets found, exit';
10011               log_message('log_message: ' || pa_debug.G_err_Stage);
10012            END IF;
10013            exit ;
10014        end if;
10015 
10016        IF PG_DEBUG = 'Y' THEN
10017        pa_debug.G_err_stage := 'Old Pkt = ' || l_old_pkt
10018                               || ' Packet Id = ' || l_packet_id
10019                               || ' Ref2 = ' || l_sys_ref2
10020                               || ' Document dist Id = ' || l_doc_dist_id
10021                               || ' Ref4 = ' || l_sys_ref4;
10022           log_message('log_message: ' || pa_debug.G_err_Stage);
10023        END IF;
10024 
10025        IF (l_old_pkt <> l_packet_id) THEN
10026           IF PG_DEBUG = 'Y' THEN
10027              pa_debug.G_err_stage := 'Calling Upd_Sts_Enc_Bal for discount';
10028              log_message('log_message: ' || pa_debug.G_err_Stage);
10029           END IF;
10030 
10031           Upd_Sts_Enc_Bal(p_packet_id   => l_packet_id,
10032                        x_return_status =>  x_return_status);
10033 
10034           IF PG_DEBUG = 'Y' THEN
10035              pa_debug.G_err_stage := 'After Calling Upd_Sts_Enc_Bal';
10036              log_message('log_message: ' || pa_debug.G_err_Stage);
10037           END IF;
10038        END IF;
10039 
10040        IF (pa_funds_control_utils.get_bdgt_link(l_Project_Id, 'STD') = 'Y') THEN
10041 
10042           -- BUG : 4885459  APPSPERF:PA:PJC: PA_TRX_IMPORT : PERF. REPOSITORY EXCEPTIONS
10043 
10044           l_ExpItemId_temp_Tab.DELETE ;
10045 
10046 	  select ei.expenditure_item_id
10047             bulk collect into l_ExpItemId_temp_Tab
10048 	    from pa_expenditure_items_all ei
10049 	   where ei.document_header_id       = l_sys_ref2
10050 	     and ei.document_distribution_id = l_doc_dist_id
10051 	     and ei.document_payment_id      = to_number(l_sys_ref4);
10052 
10053 	  IF l_ExpItemId_temp_Tab.COUNT > 0 THEN
10054 	     FORALL j in l_ExpItemId_temp_Tab.first..l_ExpItemId_temp_Tab.last
10055 	            update pa_cost_distribution_lines_all cdl
10056                        set cdl.Budget_CCID = l_budget_ccid    ,
10057                            cdl.encumbrance_type_id = pa_funds_control_utils.get_encum_type_id(cdl.project_id,'STD'),
10058 			   -- R12 Funds Management Uptake
10059                            cdl.budget_line_id = DECODE(cdl.budget_line_id,NULL,l_budget_line_id,cdl.budget_line_id),
10060                            cdl.budget_version_id = DECODE(cdl.budget_version_id,NULL,l_budget_version_id,cdl.budget_version_id)
10061                      Where cdl.budget_ccid is null
10062 	               and cdl.expenditure_item_id = l_ExpItemId_temp_Tab(j) ;
10063 	  END IF ;
10064 
10065          IF PG_DEBUG = 'Y' THEN
10066             pa_debug.G_err_stage := 'Updated Ref2-Ref3 [' || l_sys_ref2 || ',' || l_sys_ref3 || ',' || l_sys_ref4 || ']-'
10067                                                      || SQL%ROWCOUNT || ' Budget CCID';
10068             log_message('log_message: ' || pa_debug.G_err_Stage);
10069          END IF;
10070 
10071        END IF;
10072 
10073        l_old_pkt := l_packet_id;
10074 
10075       end loop;
10076       close select_disc_packets;
10077     end if;
10078 
10079 /* Start of bug 3239837 */
10080     pa_debug.G_err_stage := 'Update rejected interface records in pa_bc_packets to T';
10081     pa_cc_utils.log_message(pa_debug.G_err_Stage);
10082 
10083     update pa_bc_packets
10084     set status_code = 'T',
10085         result_code = 'F140'
10086     where request_id = g_request_id
10087     and   status_code = 'P'
10088     and   txn_interface_id in
10089           (select txn_interface_id   /*REL12 : AP Lines Uptake changes*/
10090              from pa_transaction_interface_all
10091             where transaction_source = p_calling_mode
10092              and  transaction_status_code = 'R'
10093              and  orig_transaction_reference = to_char(g_request_id)
10094           );
10095 
10096     pa_debug.G_err_stage := 'In stage 1 Updated count to T = '|| SQL%ROWCOUNT;
10097     pa_cc_utils.log_message(pa_debug.G_err_Stage);
10098 
10099  update pa_bc_packets
10100     set status_code = 'T',
10101         result_code = 'F140'
10102     where request_id = g_request_id
10103     and   status_code = 'P'
10104     and   (document_header_id, document_distribution_id) in
10105           (select po_header_id, po_distribution_id
10106 	   from po_distributions_all
10107 	   where po_distribution_id in
10108                   (select po_distribution_id
10109                    from ap_invoice_distributions_all inv, pa_transaction_interface_all pti
10110                   where inv.invoice_id = pti.cdl_system_reference2
10111 		    and inv.invoice_distribution_id = pti.cdl_system_reference5 /*REL12 : AP Lines Uptake changes*/
10112                     and pti.transaction_source = p_calling_mode
10113                     and pti.transaction_status_code = 'R'
10114                     and pti.orig_transaction_reference = to_char(g_request_id))
10115           );
10116 
10117     pa_debug.G_err_stage := 'In stage 2 Updated count to T = '|| SQL%ROWCOUNT;
10118     pa_cc_utils.log_message(pa_debug.G_err_Stage);
10119 
10120 /* End of bug 3239837 */
10121 
10122     --Bug 3592289
10123     --If pkts are still left in 'P' sts, it means the corresponding
10124     --EIs in the interface table are rejected. The above cursors select
10125     --only transaction status code = 'A'. Hence, here we update all
10126     --the remaining 'P' status for the request id to 'T'
10127     IF PG_DEBUG = 'Y' THEN
10128        log_message('log_message: Before update to T');
10129     END IF;
10130 
10131     update pa_bc_packets
10132     set status_code = 'T',
10133       result_code = 'F140'
10134     where status_code = 'P'
10135     and request_id = G_REQUEST_ID;
10136 
10137     IF PG_DEBUG = 'Y' THEN
10138        log_message('log_message: ' || 'Updated ' || SQL%ROWCOUNT || ' packet statuses to T');
10139     END IF;
10140 
10141     --Bug 3592289 End
10142 
10143 
10144     IF PG_DEBUG = 'Y' THEN
10145     pa_debug.G_err_stage := 'Done with Tieback';
10146        log_message('log_message: ' || pa_debug.G_err_Stage);
10147     END IF;
10148 
10149     pa_cc_utils.reset_curr_function;
10150 
10151   EXCEPTION
10152 	WHEN OTHERS THEN
10153       	x_return_status  := fnd_api.g_ret_sts_unexp_error;
10154 	RAISE;
10155   END tieback_fc_records;
10156 
10157   --PA-J Receipt Accrual Changes:
10158   --     1. Renamed all existing cursors so that it can be distinguished by the source.
10159   --        Renamed c_commitment, c_bc_packets, commitment_exist cursors to
10160   --                c_ap_commitment, c_ap_bc_packets, ap_commitment_exist.
10161   --     2. Added 2 more IN parameters to tr_import_funds_check, namely
10162   --        p_txn_source, p_acct_raw_cost
10163   --     3. Added 3 more cursors for the PO records - c_po_commitment, c_po_bc_packets, po_commitment_exist
10164   --     4. Converted the existing section to insert AP funds check records and added a new section for
10165   --        PO based on the p_txn_source.
10166   --     5. For the PO reversing raw line, use -p_acct_raw_cost as the
10167   --        (accounted_dr-accounted_cr) and (entered_dr-entered_cr) columns resp.
10168   --     6. For the PO reversing burden line, prorate the amount from the burden line of the original PO line.
10169   --     7. For the positive EXP line, use the p_acct_raw_cost for the amount columns.
10170   PROCEDURE tr_import_funds_check ( p_pa_date               IN  DATE,
10171                                   p_txn_source            IN VARCHAR2,
10172                                   p_acct_raw_cost         IN NUMBER,
10173 				  p_adj_exp_item_id       IN NUMBER,
10174 				  p_txn_interface_id      IN NUMBER,
10175 				  p_fc_document_type      IN VARCHAR2,
10176                                   x_packet_id             OUT NOCOPY NUMBER,
10177                                   x_error_message_code    OUT NOCOPY VARCHAR2,
10178                                   x_error_stage           OUT NOCOPY VARCHAR2,
10179                                   x_return_status         OUT NOCOPY VARCHAR2) IS
10180 
10181    --R12 funds management uptake : modified corresponding insert api's to autonomous
10182    --   PRAGMA AUTONOMOUS_TRANSACTION;
10183 
10184    -- R12 AP lines uptake : Prepayment changes :
10185    -- Deleted obsolete variables
10186 
10187    l_packet_id          NUMBER ;
10188    l_po_dist_id         NUMBER;
10189    l_acc_flag           VARCHAR2(1);
10190    l_po_hdr_id          NUMBER;
10191    l_denom_po_amt       NUMBER;
10192    l_acct_po_amt        NUMBER;
10193    l_normal_inv         VARCHAR2(1) := 'Y';
10194    l_txn_source         VARCHAR2(30) := p_txn_source;
10195    --R12 Funds Management Uptake : Deleted variables defined for storing encumbrance types
10196    l_project_id              NUMBER(15);
10197    l_base_qv           NUMBER;
10198    l_base_av           NUMBER;
10199    l_period_year       pa_bc_packets.period_year%type; --REL12
10200    l_inv_type          ap_invoices_all.invoice_type_lookup_code%TYPE;
10201    l_line_type_lookup_code  ap_invoice_distributions_all.line_type_lookup_code%TYPE;
10202 
10203   -- R12 AP lines uptake : Prepayment changes :Cursor to fetch AP related details.
10204    CURSOR C_ap_details (p_invoice_id               NUMBER,
10205                         p_invoice_distribution_id  NUMBER) IS
10206    SELECT dist.po_distribution_id,
10207           dist.project_id,
10208           nvl(dist.base_quantity_variance,0), --PA.M QV and AV
10209           NVL(dist.base_amount_variance,0),
10210           inv.invoice_type_lookup_code,
10211 	  dist.line_type_lookup_code
10212     FROM  ap_invoice_distributions dist,
10213           ap_invoices inv
10214    WHERE dist.invoice_id = p_invoice_id
10215      AND dist.invoice_distribution_id = p_invoice_distribution_id
10216      AND inv.invoice_id  = dist.invoice_id;
10217 
10218    --Bug 2905892
10219    --Derive Period_Name for the given GL_Date from GL_Period_Statuses
10220    --for Application_Id 101.
10221    --Funds checking is done using GL periods (application id = 101)
10222    FUNCTION Get_FC_Period_Name(P_GL_Date IN DATE) RETURN VARCHAR2 IS
10223       l_Period_Name pa_bc_packets.period_name%type;
10224    BEGIN
10225 
10226      If P_GL_Date = G_PrevFCGlDate Then
10227 
10228         l_Period_Name := G_PrevFCPdName;
10229 
10230      Else
10231 
10232        Select Period_Name,PERIOD_YEAR
10233          Into l_Period_Name,l_period_year
10234          From Gl_Period_Statuses
10235         Where Application_Id = 101
10236           And Trunc(P_GL_Date) Between Trunc(Start_Date) And Trunc(End_Date)
10237           And Set_Of_Books_Id = G_SobId
10238 	  And adjustment_period_flag = 'N'; -- added for bug 3083625
10239 
10240         G_PrevFCGlDate := P_GL_Date;
10241         G_PrevFCPdName := l_Period_Name;
10242 	G_Fc_Period_Year  := l_period_year;
10243 
10244      End If;
10245 
10246      Return l_Period_Name;
10247 
10248    EXCEPTION
10249        WHEN OTHERS THEN
10250           Raise;
10251 
10252    END Get_FC_Period_Name;
10253 
10254   BEGIN
10255 
10256     pa_cc_utils.set_curr_function('tr_import_funds_check');
10257     IF PG_DEBUG = 'Y' THEN
10258     pa_debug.G_err_stage := 'Inside Tr_Import_Funds_Check';
10259        log_message('log_message: ' || pa_debug.G_err_Stage);
10260     END IF;
10261 
10262     --
10263     -- 1. Generate a packet identifier for all the records that
10264     --    needs to be inserted into Pa BC Packets
10265     --
10266     IF PG_DEBUG = 'Y' THEN
10267     pa_debug.G_err_stage := 'Generateing new packet Id';
10268        log_message('log_message: ' || pa_debug.G_err_Stage);
10269     END IF;
10270      Select Gl_Bc_Packets_s.NextVal
10271        Into l_Packet_Id
10272        From dual;
10273 
10274     /*  Since we have divided the funds check packet insert code into 3 sections
10275     the bc packet id will be generated in each of the individual sections
10276     rather than in the beginning
10277     */
10278 
10279     --Bug 2905892
10280     --Get the GL Period Name from txn's gl_date. This will be inserted into pa_bc_packets
10281     --for both the reversing raw and burden lines and also the EXP lines.
10282     --Funds Check API will derive the GL_Date based on the Period_Name, so what
10283     --we insert for gl_date is irrelevant here.
10284     IF PG_DEBUG = 'Y' THEN
10285        pa_debug.G_err_stage := 'Calling G_FC_Period_Name';
10286        log_message('log_message: ' || pa_debug.G_err_Stage);
10287     END IF;
10288 
10289     G_FC_Period_Name := Get_FC_Period_Name(P_Gl_Date => G_FC_Gl_Date);
10290 
10291     IF PG_DEBUG = 'Y' THEN
10292        pa_debug.G_err_stage := 'G_FC_Period_Name = ' || G_FC_Period_Name;
10293        log_message('log_message: ' || pa_debug.G_err_Stage);
10294     END IF;
10295     --End Bug 2905892
10296 
10297      -- R12 : AP Lines Uptake changes + R12 AP lines uptake : Prepayment changes
10298      -- Cash based Accounting code : When Cash based accounting is set to Yes then
10299      -- following type of transactions are allowed to be interafced to projects :
10300      --  a.  Historical Data(i.e.11i) : System interfaces invoice distributions and not the payments.
10301      --      For these records g_cdl_system_reference4 will be NULL.
10302      --      Following invoices in Cash based accounting are considered as historical data :
10303      --        1. All Invoices (including prepayments) with historical_flag ='Y'
10304      --        2. All reversal/cancelled Invoices associated with Invoices which are interfaced
10305      --        3. Prepayment application associated with interfaced prepayment distribution.
10306      --
10307      --      These historical invoices will be firing the same fundscheck logic as that of
10308      --      accrual based accounting invoices.
10309      --  b.  R12 Data : Only payments will be interfaced.
10310      --      For these records g_cdl_system_reference4 will be NOT NULL
10311 
10312      IF (l_txn_source in ('AP INVOICE', 'AP NRTAX','AP EXPENSE','INTERCOMPANY_AP_INVOICES', 'AP ERV',
10313                           'INTERPROJECT_AP_INVOICES','AP VARIANCE') )
10314 	             AND G_cash_based_accounting = 'Y'
10315 	             AND NVL(g_cdl_system_reference4,0) <> 0  THEN
10316 
10317 	            ap_cash_based_funds_check (
10318 	             p_txn_source                => l_txn_source,
10319 	             p_acct_raw_cost             => p_acct_raw_cost,
10320 	             p_packet_id                 => l_packet_id,
10321                      p_sys_ref2                  => to_number(g_cdl_system_reference2),
10322                      p_sys_ref4                  => g_cdl_system_reference4,
10323 	             p_sys_ref5                  => to_number(g_cdl_system_reference5),
10324 	             p_adj_exp_item_id           => p_adj_exp_item_id,
10325 	             p_txn_interface_id          => p_txn_interface_id,
10326 	             p_fc_document_type          => p_fc_document_type,
10327 	             x_error_message_code        => x_error_message_code,
10328 	             x_error_stage               => x_error_stage,
10329 	             x_return_status             => x_return_status);
10330 
10331     ELSIF (l_txn_source in ('AP INVOICE', 'AP NRTAX') ) THEN
10332 
10333        IF PG_DEBUG = 'Y' THEN
10334        pa_debug.G_err_stage := 'Selecting PO Dist Id for AP Invoice';
10335           log_message('log_message: ' || pa_debug.G_err_Stage);
10336        END IF;
10337 
10338        -- REL12 : AP Lines Uptake changes
10339        -- Commented logic associated with IPV and ERV columns as these will be now interfaced
10340        -- as seperate distribution lines
10341 
10342        -- R12 AP lines uptake : Prepayment changes: Shifted logic of SELECT statment to cursor .
10343        -- Also modified cursor to fetch value for invoice type
10344 
10345        OPEN c_ap_details (to_number(g_cdl_system_reference2),
10346                           to_number(g_cdl_system_reference5));
10347        FETCH c_ap_details INTO l_po_dist_id,
10348                                l_project_id,
10349                                l_base_qv,
10350 			       l_base_av,
10351 			       l_inv_type,
10352 			       l_line_type_lookup_code;
10353        CLOSE c_ap_details;
10354 
10355        -- R12 AP lines uptake : Prepayment changes
10356        -- Deleted code added for bug 3746522 as the logic is not required
10357 
10358        --PA.M QV and AV
10359        /*REL12 : AP Lines uptake changes */
10360        /*Call to AP_PA_API_PKG.Get_Inv_Amount_Var is deleted and introduced logic to fetch
10361          amt variance from ap_invoice_distributions*/
10362 
10363        IF PG_DEBUG = 'Y' THEN
10364           log_message('log_message: ' || 'Po Dist = ' || l_po_dist_id);
10365           log_message('log_message: ' || ' Base QV = ' || l_base_qv );
10366           log_message('log_message: ' || ' Base AV = ' || l_base_av );
10367           log_message('log_message: ' || ' line_type_lookup_code = ' || l_line_type_lookup_code );
10368        END IF;
10369 
10370        -- R12 AP lines uptake : Prepayment changes: For prepyament always fire ap_funds_check procedure
10371        -- as PREPAYMENT commitment matched to PO will never be fundschecked
10372 
10373        If (l_po_dist_id is not null AND l_inv_type <> 'PREPAYMENT' ) Then
10374 
10375           IF PG_DEBUG = 'Y' THEN
10376           pa_debug.G_err_stage := 'PO Dist Found, Selecting Receipt Accrual Flag';
10377              log_message('log_message: ' || pa_debug.G_err_Stage);
10378           END IF;
10379 
10380           --R12 Funds management uptake changes: Obsolete logic which was based on financial system parameters.Going forward
10381           --commitments fundscheck will be performed irrespective of value stored in encumbrance types.
10382           --Hence for matched invoices amount should always be relieved against AP.
10383 
10384           Select nvl(accrue_on_receipt_flag,'N'),
10385                  po_header_id
10386            Into l_acc_flag,
10387                 l_po_hdr_id
10388            from Po_distributions
10389            where po_distribution_id = l_po_dist_id;
10390 
10391            IF PG_DEBUG = 'Y' THEN
10392               log_message('log_message: ' || 'Receipt Acc Flag = ' || l_acc_flag || ' Po Hdr = ' || l_po_hdr_id);
10393            END IF;
10394 
10395            --Bug 2672772: If non-accrue on receipt and enc type same, PO commitment should be relieved
10396 	   -- Bug 5561690 : TIPV and TRV are interfaced with  'AP NRTAX' transaction source but
10397 	   -- these lines( matched to PO with accrue on receipt) are reserved as AP commitments.
10398 	   -- Hence should be treated as normal invoice lines.
10399            If (l_acc_flag = 'Y' AND l_line_type_lookup_code NOT IN ('TRV','TIPV'))
10400 	      OR --(l_acc_flag = 'N' AND l_po_enc_type_id = l_ap_enc_type_id)  OR --R12 Funds Management Uptake
10401            /* when a variance exists on a tax distribution for an invoice matched to a rate based PO
10402               (for which the CWK Imp option is set to Y, meaning timecards are interfaced and only
10403               variance and tax are to be interfaced from AP/PO), the amount minus the variance should be
10404               deducted from PO commitment and the variance should be deducted from AP
10405               commitment */
10406               (l_txn_source = 'AP NRTAX'  AND
10407                nvl(Pa_Pjc_Cwk_Utils.Is_rate_based_line( null, l_po_dist_id),'N') = 'Y' AND
10408                Pa_Pjc_CWk_Utils.Is_CWK_TC_Xface_Allowed(l_project_id) = 'Y')Then
10409 
10410                IF PG_DEBUG = 'Y' THEN
10411                pa_debug.G_err_stage := 'PO accrues on receipt or Enc Type Same for non-accrue on receipt';
10412                   log_message('log_message: ' || pa_debug.G_err_Stage);
10413                END IF;
10414 
10415                --This is not a normal invoice meaning we have to reverse the
10416                --PO commitment and then check if there is any variances and
10417                --reverse them too.
10418                l_normal_inv := 'N';
10419 
10420                --l_denom_po_amt := p_denom_raw_cost - (l_ipv + l_erv);
10421                --PA.M QV and AV
10422                l_acct_po_amt  := p_acct_raw_cost - (/* l_base_ipv + l_erv + */ --REL12
10423 	                                            l_base_qv + nvl(l_base_av,0));
10424 
10425                IF PG_DEBUG = 'Y' THEN
10426                   log_message('log_message: ' || ' Acct PO Amt = ' || l_acct_po_amt);
10427                END IF;
10428 
10429                IF PG_DEBUG = 'Y' THEN
10430                pa_debug.G_err_stage := 'Calling ap_po_funds_check';
10431                   log_message('log_message: ' || pa_debug.G_err_Stage);
10432                END IF;
10433 
10434                ap_po_funds_check (
10435                                   p_txn_source           => l_txn_source,
10436                                   p_acct_raw_cost        => l_acct_po_amt,
10437                                   p_packet_id            => l_packet_id,
10438                                   p_po_hdr_id            => l_po_hdr_id,
10439                                   p_po_dist_id           => l_po_dist_id,
10440                                   p_inv_id               => to_number(g_cdl_system_reference2),
10441 				  p_inv_dist_id          => to_number(g_cdl_system_reference5),   --REL12
10442                                   p_dist_line            => to_number(g_cdl_system_reference3),   --REL12
10443 				  p_adj_exp_item_id      => p_adj_exp_item_id,
10444 				  p_txn_interface_id     => p_txn_interface_id,
10445 				  p_fc_document_type     => p_fc_document_type,
10446 				  p_base_qty_var_amt     => l_base_qv,
10447                                   p_base_amt_var_amt     => l_base_av,
10448                                   x_error_message_code   => x_error_message_code,
10449                                   x_error_stage          => x_error_stage,
10450                                   x_return_status        => x_return_status);
10451 
10452                IF PG_DEBUG = 'Y' THEN
10453                pa_debug.G_err_stage := 'After ap_po_funds_check';
10454                   log_message('log_message: ' || pa_debug.G_err_Stage);
10455                END IF;
10456 
10457            End If;
10458 
10459        End If;
10460 
10461        If (l_normal_inv = 'Y') Then
10462 
10463           IF PG_DEBUG = 'Y' THEN
10464           pa_debug.G_err_stage := 'Start funds check packet insert for AP - non accrue on receipt diff enc type';
10465              log_message('log_message: ' || pa_debug.G_err_Stage);
10466           END IF;
10467 
10468           ap_funds_check (
10469                      p_txn_source         => l_txn_source,
10470                      p_acct_raw_cost      => p_acct_raw_cost,
10471                      p_packet_id          => l_packet_id,
10472                      p_sys_ref2           => g_cdl_system_reference2,
10473                      p_sys_ref3           => g_cdl_system_reference3,
10474 		     p_sys_ref5           => g_cdl_system_reference5, --REL12
10475 		     p_adj_exp_item_id    => p_adj_exp_item_id,       --REL12
10476 		     p_txn_interface_id   => p_txn_interface_id,      --REL12
10477 		     p_fc_document_type   => p_fc_document_type,      --REL12
10478                      x_error_message_code => x_error_message_code,
10479                      x_error_stage        => x_error_stage,
10480                      x_return_status      => x_return_status);
10481 
10482            IF PG_DEBUG = 'Y' THEN
10483            pa_debug.G_err_stage := 'After ap_funds_check';
10484               log_message('log_message: ' || pa_debug.G_err_Stage);
10485            END IF;
10486 
10487            /*REL12 : AP Lines Uptake changes
10488 	    Commented below logic as IPV / ERV/QV/AV lines will be separately interfaced
10489 	    and picked by the AP VARIANCE section */
10490 
10491        End If;
10492 
10493     --PA-J Receipt Accrual Changes:
10494     --     Added the below section for Receipt records to be funds checked.
10495     ELSIF (l_txn_source in ('PO RECEIPT', 'PO RECEIPT NRTAX')) THEN
10496 
10497        IF PG_DEBUG = 'Y' THEN
10498        pa_debug.G_err_stage := 'Start funds check packet insert for PO Receipts';
10499           log_message('log_message: ' || pa_debug.G_err_Stage);
10500        END IF;
10501 
10502        po_funds_check (
10503                      p_txn_source         => l_txn_source,
10504                      p_acct_raw_cost      => p_acct_raw_cost,
10505                      p_packet_id          => l_packet_id,
10506                      p_sys_ref2           => g_cdl_system_reference2,
10507                      p_sys_ref3           => g_cdl_system_reference3,
10508 		     p_sys_ref4           => g_cdl_system_reference4, -- Bug 5530897
10509 		     p_adj_exp_item_id    => p_adj_exp_item_id,       --REL12
10510 		     p_txn_interface_id   => p_txn_interface_id,      --REL12
10511 		     p_fc_document_type   => p_fc_document_type,      --REL12
10512                      x_error_message_code => x_error_message_code,
10513                      x_error_stage        => x_error_stage,
10514                      x_return_status      => x_return_status);
10515 
10516        IF PG_DEBUG = 'Y' THEN
10517        pa_debug.G_err_stage := 'After po_funds_check';
10518           log_message('log_message: ' || pa_debug.G_err_Stage);
10519        END IF;
10520 
10521     /* Bug 5560524 : Modified the following If condition so that ap_funds_check is also called for
10522                      'AP ERV' transaction source. */
10523     ELSIF (l_txn_source in ('AP VARIANCE','AP ERV')) THEN
10524 
10525        IF PG_DEBUG = 'Y' THEN
10526        pa_debug.G_err_stage := 'Start funds check packet insert for AP Variance records';
10527           log_message('log_message: ' || pa_debug.G_err_Stage);
10528        END IF;
10529 
10530        ap_funds_check (
10531                      p_txn_source         => l_txn_source,
10532                      p_acct_raw_cost      => p_acct_raw_cost,
10533                      p_packet_id          => l_packet_id,
10534                      p_sys_ref2           => g_cdl_system_reference2,
10535                      p_sys_ref3           => g_cdl_system_reference3,
10536 		     p_sys_ref5           => g_cdl_system_reference5,
10537 		     p_adj_exp_item_id    => p_adj_exp_item_id,       --REL12
10538 		     p_txn_interface_id   => p_txn_interface_id,      --REL12
10539 		     p_fc_document_type   => p_fc_document_type,      --REL12
10540                      x_error_message_code => x_error_message_code,
10541                      x_error_stage        => x_error_stage,
10542                      x_return_status      => x_return_status);
10543 
10544        IF PG_DEBUG = 'Y' THEN
10545        pa_debug.G_err_stage := 'After ap_funds_check';
10546           log_message('log_message: ' || pa_debug.G_err_Stage);
10547        END IF;
10548     -- R12 AP lines uptake : Prepayment changes: This is fired for both Cash and Accrual based AP discounts.
10549     ELSIF (l_txn_source in ('AP DISCOUNTS')) THEN --2339216
10550        IF PG_DEBUG = 'Y' THEN
10551        pa_debug.G_err_stage := 'Selecting disc amount for AP Invoice';
10552           log_message('log_message: ' || pa_debug.G_err_Stage);
10553        END IF;
10554 
10555        IF PG_DEBUG = 'Y' THEN
10556        pa_debug.G_err_stage := 'Discount amount='||p_acct_raw_cost;
10557           log_message('log_message: ' || pa_debug.G_err_Stage);
10558        END IF;
10559 
10560        if ( p_acct_raw_cost <> 0 ) then
10561        ap_disc_funds_check (
10562                  p_txn_source         => l_txn_source,
10563                  p_acct_raw_cost      => p_acct_raw_cost,
10564                  p_packet_id          => l_packet_id,
10565                  p_sys_ref2           => g_cdl_system_reference2,
10566                  p_sys_ref3           => g_cdl_system_reference3,
10567 		 p_sys_ref4           => g_cdl_system_reference4, --REL12
10568 		 p_sys_ref5           => g_cdl_system_reference5, --REL12
10569 		 p_adj_exp_item_id    => p_adj_exp_item_id,       --REL12
10570 		 p_txn_interface_id   => p_txn_interface_id,      --REL12
10571 		 p_fc_document_type   => p_fc_document_type,      --REL12
10572                  x_error_message_code => x_error_message_code,
10573                  x_error_stage        => x_error_stage,
10574                  x_return_status      => x_return_status);
10575 
10576               IF PG_DEBUG = 'Y' THEN
10577               pa_debug.G_err_stage := 'After Call ap_disc_funds_check';
10578                  log_message('log_message: ' || pa_debug.G_err_Stage);
10579               END IF;
10580        end if; --2339216
10581     END IF;  --p_txn_source check
10582 
10583     x_packet_id  := l_packet_id ;
10584     IF PG_DEBUG = 'Y' THEN
10585        log_message('log_message: ' || 'Returning from funds check');
10586     END IF;
10587     pa_cc_utils.reset_curr_function;
10588 
10589     -- set the return status to success
10590     x_return_status := FND_API.G_RET_STS_SUCCESS;
10591 
10592   EXCEPTION
10593     WHEN OTHERS THEN
10594        log_message('log_message: In others of tr import, packet_id = '|| l_packet_id); -- Bug 3592289
10595        Upd_PktSts(p_packet_id => l_packet_id);
10596 
10597        IF PG_DEBUG = 'Y' THEN
10598           log_message('log_message: ' || sqlerrm||' Returning from funds check');
10599           log_message('log_message: x_return_status = ' || x_return_status);  -- Bug 3592289
10600           log_message('log_message: x_error = ' || x_error_message_code);  -- Bug 3592289
10601        END IF;
10602        x_return_status  := fnd_api.g_ret_sts_unexp_error;
10603        pa_cc_utils.reset_curr_function;
10604   END tr_import_funds_check ;
10605 
10606   --2339216: Added procedure
10607   PROCEDURE ap_disc_funds_check (
10608           p_txn_source            IN VARCHAR2,
10609           p_acct_raw_cost         IN NUMBER,
10610           p_packet_id             IN NUMBER,
10611           p_sys_ref2              IN NUMBER,
10612           p_sys_ref3              IN NUMBER,
10613 	  p_sys_ref4              IN VARCHAR2, --REL12
10614  	  p_sys_ref5              IN NUMBER, --REL12
10615 	  p_adj_exp_item_id       IN NUMBER,        --REL12
10616 	  p_txn_interface_id      IN NUMBER,        --REL12
10617 	  p_fc_document_type      IN VARCHAR2,      --REL12
10618           x_error_message_code    OUT NOCOPY VARCHAR2,
10619           x_error_stage           OUT NOCOPY VARCHAR2,
10620           x_return_status         OUT NOCOPY VARCHAR2) IS
10621 
10622   BEGIN
10623 
10624     pa_cc_utils.set_curr_function('ap_disc_funds_check');
10625 
10626     IF PG_DEBUG = 'Y' THEN
10627     pa_debug.G_err_stage := 'Inside AP_Disc_Funds_Check';
10628        log_message('log_message: ' || pa_debug.G_err_Stage);
10629     END IF;
10630 
10631     insert_ap_bc_packets(   p_packet_id        => p_packet_id,
10632                             p_sys_ref2         => p_sys_ref2,
10633 			    p_sys_ref4         => p_sys_ref4,
10634                             p_sys_ref5         => p_sys_ref5,
10635                             p_acct_raw_cost    => p_acct_raw_cost,
10636                             p_acct_bur_cost    => 0,
10637                             p_fc_document_type => p_fc_document_type ,
10638                             p_txn_source       => p_txn_source   ,
10639                             p_adj_exp_item_id  => p_adj_exp_item_id ,
10640 			    p_txn_interface_id => p_txn_interface_id);
10641 
10642     IF PG_DEBUG = 'Y' THEN
10643        log_message('log_message: ' || 'Returning from ap funds check');
10644     END IF;
10645 
10646     -- set the return status to success
10647     x_return_status := FND_API.G_RET_STS_SUCCESS;
10648 
10649     pa_cc_utils.reset_curr_function;
10650 
10651   EXCEPTION
10652     --Bug 2672772: Handle the NDF exception,
10653     --raise the error here but not in tr_import_funds_check
10654     WHEN NO_DATA_FOUND THEN
10655        IF PG_DEBUG = 'Y' THEN
10656           log_message('log_message: ' || sqlerrm||' In NDF: Returning from ap disc funds check');
10657        END IF;
10658        --Call FC packets update API to update packets to T.
10659        --moved to tr_import_funds_check
10660        --Upd_PktSts(p_packet_id => p_packet_id);
10661 
10662        x_return_status  := fnd_api.g_ret_sts_error;
10663        x_error_message_code := 'PA_FC_NDF';
10664 
10665        pa_cc_utils.reset_curr_function;
10666        RAISE;
10667 
10668     WHEN OTHERS THEN
10669        IF PG_DEBUG = 'Y' THEN
10670           log_message('log_message: ' || sqlerrm||' Returning from ap disc funds check');
10671        END IF;
10672        x_return_status  := fnd_api.g_ret_sts_unexp_error;
10673        pa_cc_utils.reset_curr_function;
10674        RAISE;
10675   END ap_disc_funds_check;
10676 
10677   PROCEDURE ap_funds_check (
10678           p_txn_source            IN VARCHAR2,
10679           p_acct_raw_cost         IN NUMBER,
10680           p_packet_id             IN NUMBER,
10681           p_sys_ref2              IN NUMBER,
10682           p_sys_ref3              IN NUMBER,
10683 	  p_sys_ref5              IN NUMBER,        --REL12
10684 	  p_adj_exp_item_id       IN NUMBER,        --REL12
10685 	  p_txn_interface_id      IN NUMBER,        --REL12
10686 	  p_fc_document_type      IN VARCHAR2,      --REL12
10687           x_error_message_code    OUT NOCOPY VARCHAR2,
10688           x_error_stage           OUT NOCOPY VARCHAR2,
10689           x_return_status         OUT NOCOPY VARCHAR2) IS
10690 
10691   BEGIN
10692 
10693     pa_cc_utils.set_curr_function('ap_funds_check');
10694 
10695     IF PG_DEBUG = 'Y' THEN
10696     pa_debug.G_err_stage := 'Inside AP_Funds_Check';
10697        log_message('log_message: ' || pa_debug.G_err_Stage);
10698     END IF;
10699 
10700    insert_ap_bc_packets(   p_packet_id        => p_packet_id,
10701                             p_sys_ref2         => p_sys_ref2,
10702 			    p_sys_ref4         => NULL,
10703                             p_sys_ref5         => p_sys_ref5,
10704                             p_acct_raw_cost    => p_acct_raw_cost,
10705                             p_acct_bur_cost    => 0,
10706                             p_fc_document_type => p_fc_document_type ,
10707                             p_txn_source       => p_txn_source   ,
10708                             p_adj_exp_item_id  => p_adj_exp_item_id ,
10709 			    p_txn_interface_id => p_txn_interface_id);
10710 
10711     IF PG_DEBUG = 'Y' THEN
10712        log_message('log_message: ' || 'Returning from ap funds check');
10713     END IF;
10714 
10715     -- set the return status to success
10716     x_return_status := FND_API.G_RET_STS_SUCCESS;
10717 
10718     pa_cc_utils.reset_curr_function;
10719 
10720   EXCEPTION
10721     --Bug 2672772: Handle the NDF exception,
10722     --raise the error here but not in tr_import_funds_check
10723     WHEN NO_DATA_FOUND THEN
10724        IF PG_DEBUG = 'Y' THEN
10725           log_message('log_message: ' || sqlerrm||' In NDF: Returning from ap funds check');
10726        END IF;
10727        --Call FC packets update API to update packets to T.
10728        --moved to tr_import_funds_check
10729        --Upd_PktSts(p_packet_id => p_packet_id);
10730 
10731         x_return_status  := fnd_api.g_ret_sts_error;
10732         x_error_message_code := 'PA_FC_NDF';
10733 
10734         pa_cc_utils.reset_curr_function;
10735         RAISE;
10736 
10737 
10738     WHEN OTHERS THEN
10739        IF PG_DEBUG = 'Y' THEN
10740           log_message('log_message: ' || sqlerrm||' Returning from ap funds check');
10741        END IF;
10742        x_return_status  := fnd_api.g_ret_sts_unexp_error;
10743        pa_cc_utils.reset_curr_function;
10744        RAISE;
10745   END ap_funds_check;
10746 
10747   PROCEDURE po_funds_check (
10748              p_txn_source            IN VARCHAR2,
10749              p_acct_raw_cost         IN NUMBER,
10750              p_packet_id             IN NUMBER,
10751              p_sys_ref2              IN NUMBER,
10752              p_sys_ref3              IN NUMBER,
10753              p_sys_ref4              IN NUMBER,        -- Bug 5530897
10754 	     p_adj_exp_item_id       IN NUMBER,        --REL12
10755 	     p_txn_interface_id      IN NUMBER,        --REL12
10756 	     p_fc_document_type      IN VARCHAR2,      --REL12
10757              x_error_message_code    OUT NOCOPY VARCHAR2,
10758              x_error_stage           OUT NOCOPY VARCHAR2,
10759              x_return_status         OUT NOCOPY VARCHAR2) IS
10760 
10761      --PA.M QV and AV
10762      l_rev_acct_raw_cost     Number;
10763      l_rev_acct_cost_temp    Number; -- Bug 5731450
10764      l_sum_amt               Number;
10765      l_cmt_rate              Number; -- Bug 5731450
10766      l_act_rate              Number; -- Bug 5731450
10767 
10768      /* Bug 5731450 : Added the cursor to get the exchange rate for the actuals. */
10769      cursor c_acct_exchange_rate is
10770      select acct_exchange_rate
10771      from pa_transaction_interface_all
10772      where txn_interface_id = p_txn_interface_id;
10773 
10774      Procedure GetCommSummAmt(p_sys_ref2 In Number,
10775                               p_sys_ref3 In Number,
10776                               x_sum_amt  Out NOCOPY Number,
10777 			      x_rate     Out NOCOPY Number -- Bug 5731450 : Added a new parameter x_rate.
10778 			      ) Is
10779          l_pkt_summ number;
10780          l_com_summ number;
10781          l_pkt_rate number; -- Bug 5731450
10782 	 l_com_rate number; -- Bug 5731450
10783 
10784      Begin
10785 
10786 /* Bug 5731450 : The following two select statements are modified to also fetch the PO exchange rate. */
10787 
10788         select sum(nvl(accounted_dr,0)-nvl(accounted_cr,0)),SUM(POD.rate)/SUM(1)
10789           into l_pkt_summ,l_pkt_rate
10790           from pa_bc_packets bcc,
10791                po_distributions pod     --Added for Bug#3693974
10792           where pod.po_header_id = p_sys_ref2
10793             and pod.po_distribution_id = p_sys_ref3
10794             and ((
10795                   bcc.document_type = 'PO'
10796                   and pod.po_distribution_id = bcc.document_distribution_id
10797                   and pod.po_header_id = bcc.document_header_id
10798                  )
10799                 or
10800                  (
10801                  bcc.document_type = 'CC_C_PAY'
10802                  -- 4456442, 4221848
10803                  -- Bug : PQE:R12: PAAPIMP_SI: ORA-01722 IN  PO_FUNDS_CHECK AND ORA-01400
10804                  and pod.req_header_reference_num = to_char(bcc.document_header_id)
10805                  and pod.req_line_reference_num = to_char(bcc.document_distribution_id)
10806                  )
10807                 )
10808            and  (
10809                  (bcc.Status_Code in('A','C'))
10810                  or
10811                  (bcc.Status_Code = 'P' and bcc.request_id = G_Request_Id)
10812                 )
10813            and  bcc.Parent_Bc_Packet_Id is NULL;
10814 
10815         select sum(nvl(accounted_dr,0)-nvl(accounted_cr,0)),SUM(POD.rate)/SUM(1)
10816           into l_com_summ,l_com_rate
10817           from pa_bc_commitments bcc,
10818                po_distributions pod          --Added for Bug#3693974
10819           where pod.po_header_id = p_sys_ref2
10820             and pod.po_distribution_id = p_sys_ref3
10821             and ((
10822                   bcc.document_type = 'PO'
10823                   and pod.po_distribution_id = bcc.document_distribution_id
10824                   and pod.po_header_id = bcc.document_header_id
10825                  )
10826                 or
10827                  (
10828                  bcc.document_type = 'CC_C_PAY'
10829                  -- 4456442, 4221848
10830                  -- Bug : PQE:R12: PAAPIMP_SI: ORA-01722 IN  PO_FUNDS_CHECK AND ORA-01400
10831                  and pod.req_header_reference_num = to_char(bcc.document_header_id)
10832                  and pod.req_line_reference_num = to_char(bcc.document_distribution_id)
10833                  )
10834                 )
10835             and  bcc.Parent_Bc_Packet_Id is NULL;
10836 
10837         x_sum_amt := nvl(l_pkt_summ,0) + nvl(l_com_summ,0) ;
10838 	x_rate := COALESCE(l_pkt_rate,l_com_rate); -- Bug 5731450
10839 
10840       Exception
10841         When Others Then
10842              Raise;
10843 
10844       End GetCommSummAmt;
10845 
10846   BEGIN
10847 
10848     pa_cc_utils.set_curr_function('po_funds_check');
10849 
10850     IF PG_DEBUG = 'Y' THEN
10851     pa_debug.G_err_stage := 'Inside PO_Funds_Check';
10852        log_message('log_message: ' || pa_debug.G_err_Stage);
10853     END IF;
10854 
10855     -- Bug 4346519 : Receipt Returns ( i.e. -ve amt getting interfaced to projects) should reserve the cmt costs that are getting
10856     -- interfaced without any manipulations.Below logic should get fired only if commitment is being relieved
10857     -- (+ve amt getting interfaced to projects) then we should not be relieving more than the reserved amount.
10858 
10859        --PA.M QV and AV
10860        GetCommSummAmt (p_sys_ref2 => p_sys_ref2,
10861                        p_sys_ref3 => p_sys_ref3,
10862                        x_sum_amt => l_sum_amt,
10863 		       x_rate => l_cmt_rate);
10864 
10865        IF PG_DEBUG = 'Y' THEN
10866            log_message('log_message: l_sum_amt = ' || l_sum_amt || ' p_acct_raw_cost = '|| p_acct_raw_cost);
10867        END IF;
10868 
10869        --If commitment is liquidated completely then for the current record,
10870        --relieve 0 amount (l_rev_acct_raw_cost=0)
10871        /* Bug 5731450 : Derive the actual exchange rate only if the commitment exchange rate is not null. */
10872        If l_cmt_rate IS NOT NULL then
10873           open c_acct_exchange_rate;
10874 	  fetch c_acct_exchange_rate into l_act_rate;
10875 	  close c_acct_exchange_rate;
10876        end if;
10877 
10878        IF PG_DEBUG = 'Y' THEN
10879            log_message('log_message: l_cmt_rate = ' || l_cmt_rate || ' l_act_rate = '|| l_act_rate);
10880        END IF;
10881 
10882      /* Bug 5731450 : If the exchange rate for the PO commitment and the actuals are different then
10883         the commitment relieving is done using PO exchange rate. */
10884      If nvl(l_cmt_rate,1) <> nvl(l_act_rate,1) then
10885         l_rev_acct_cost_temp := nvl(p_acct_raw_cost,0)/nvl(l_act_rate,1)*nvl(l_cmt_rate,1);
10886      else
10887         l_rev_acct_cost_temp := nvl(p_acct_raw_cost,0);
10888      end If;
10889 
10890     IF NVL(p_acct_raw_cost,0) > 0 THEN
10891 
10892         if nvl(l_sum_amt,0) = 0 then
10893           if nvl(p_acct_raw_cost,0) < nvl(l_sum_amt,0) then  /* added for bug:7326188 */
10894             l_rev_acct_raw_cost := nvl(p_acct_raw_cost,0);
10895           else
10896              l_rev_acct_raw_cost := 0;
10897            end if;
10898        else
10899           if l_rev_acct_cost_temp > nvl(l_sum_amt,0) then -- Bug 5731450
10900              l_rev_acct_raw_cost := nvl(l_sum_amt,0);
10901           else
10902              l_rev_acct_raw_cost := l_rev_acct_cost_temp; -- Bug 5731450
10903           end if;
10904        end if;
10905 
10906     ELSE  -- Bug 4346519
10907 
10908       l_rev_acct_raw_cost := l_rev_acct_cost_temp; -- Bug 5731450
10909 
10910     END IF;
10911 
10912     IF PG_DEBUG = 'Y' THEN
10913         pa_debug.G_err_stage := 'Before Calling insert_po_bc_packets';
10914        log_message('log_message: ' || pa_debug.G_err_Stage);
10915     END IF;
10916 
10917     insert_po_bc_packets   (p_packet_id          => p_packet_id ,
10918                             p_sys_ref2           => p_sys_ref2  ,
10919 			    p_sys_ref4           => p_sys_ref4, -- Bug 5530897
10920                             p_sys_ref3           => p_sys_ref3  ,
10921                             p_acct_raw_cost      => p_acct_raw_cost,
10922 			    p_cmt_raw_cost       => l_rev_acct_raw_cost,
10923                             p_fc_document_type   => p_fc_document_type,
10924                             p_txn_source         => p_txn_source ,
10925                             p_adj_exp_item_id    => p_adj_exp_item_id,
10926 			    p_txn_interface_id   => p_txn_interface_id);
10927 
10928     IF PG_DEBUG = 'Y' THEN
10929         pa_debug.G_err_stage := 'After Calling insert_po_bc_packets';
10930        log_message('log_message: ' || pa_debug.G_err_Stage);
10931     END IF;
10932 
10933     IF PG_DEBUG = 'Y' THEN
10934        log_message('log_message: ' || 'Returning from po funds check');
10935     END IF;
10936 
10937     -- set the return status to success
10938     x_return_status := FND_API.G_RET_STS_SUCCESS;
10939 
10940     pa_cc_utils.reset_curr_function;
10941 
10942   EXCEPTION
10943     --Bug 2672772: Handle the NDF exception,
10944     --raise the error here but not in tr_import_funds_check
10945     WHEN NO_DATA_FOUND THEN
10946        IF PG_DEBUG = 'Y' THEN
10947           log_message('log_message: ' || sqlerrm||' In NDF: Returning from po funds check');
10948        END IF;
10949        --Call FC packets update API to update packets to T.
10950        --moved to tr_import_funds_check
10951        --Upd_PktSts(p_packet_id => p_packet_id);
10952 
10953        x_return_status  := fnd_api.g_ret_sts_error;
10954        x_error_message_code := 'PA_FC_NDF';
10955 
10956        pa_cc_utils.reset_curr_function;
10957        RAISE;
10958 
10959     WHEN OTHERS THEN
10960        IF PG_DEBUG = 'Y' THEN
10961           log_message('log_message: ' || sqlerrm||' Returning from po funds check');
10962        END IF;
10963        x_return_status  := fnd_api.g_ret_sts_unexp_error;
10964        pa_cc_utils.reset_curr_function;
10965        RAISE;
10966   END po_funds_check;
10967 
10968   PROCEDURE ap_po_funds_check (
10969              p_txn_source            IN VARCHAR2,
10970              p_acct_raw_cost         IN NUMBER,
10971              p_packet_id             IN NUMBER,
10972              p_po_hdr_id             IN NUMBER,
10973              p_po_dist_id            IN NUMBER,
10974              p_inv_id                IN NUMBER,
10975 	     p_inv_dist_id           IN NUMBER, --REL12
10976              p_dist_line             IN NUMBER, --REL12
10977 	     p_adj_exp_item_id       IN NUMBER,        --REL12
10978 	     p_txn_interface_id      IN NUMBER,        --REL12
10979 	     p_fc_document_type      IN VARCHAR2,      --REL12
10980              p_base_qty_var_amt      IN NUMBER,
10981              p_base_amt_var_amt      IN NUMBER,
10982              x_error_message_code    OUT NOCOPY VARCHAR2,
10983              x_error_stage           OUT NOCOPY VARCHAR2,
10984              x_return_status         OUT NOCOPY VARCHAR2) IS
10985 
10986   BEGIN
10987 
10988    pa_cc_utils.set_curr_function('ap_po_funds_check');
10989 
10990    IF PG_DEBUG = 'Y' THEN
10991    pa_debug.G_err_stage := 'Inside ap_po_Funds_Check';
10992       log_message('log_message: ' || pa_debug.G_err_Stage);
10993    END IF;
10994 
10995    IF PG_DEBUG = 'Y' THEN
10996    pa_debug.G_err_stage := 'Start funds check packet insert for AP records that accrue on receipt';
10997       log_message('log_message: ' || pa_debug.G_err_Stage);
10998    END IF;
10999 
11000    IF PG_DEBUG = 'Y' THEN
11001    pa_debug.G_err_stage := 'Call po_funds_check to reverse out the PO commitment';
11002       log_message('log_message: ' || pa_debug.G_err_Stage);
11003    END IF;
11004 
11005    po_funds_check (
11006                  p_txn_source         => p_txn_source,
11007                  p_acct_raw_cost      => p_acct_raw_cost,
11008                  p_packet_id          => p_packet_id,
11009                  p_sys_ref2           => p_po_hdr_id,
11010                  p_sys_ref3           => p_po_dist_id,
11011 		 p_sys_ref4           => TO_NUMBER(NULL), --Bug 5550268
11012 		 p_adj_exp_item_id    => p_adj_exp_item_id,       --REL12
11013 		 p_txn_interface_id   => p_txn_interface_id,      --REL12
11014 		 p_fc_document_type   => p_fc_document_type,      --REL12
11015                  x_error_message_code => x_error_message_code,
11016                  x_error_stage        => x_error_stage,
11017                  x_return_status      => x_return_status);
11018 
11019    /* Deleted code for IPV and ERV as these will be interfaced as seperate EXP's
11020       in REL12 */
11021 
11022    /* Replaced ap_funds_check calls for QV and AV with one call to ap_funds_check
11023       .In insert_ap_bc_packets procedure logic is introduced to relieve all the varaince/invoice
11024       amounts associated with invoice distribution */
11025 
11026    IF NVL(p_base_qty_var_amt,0) <> 0 THEN
11027 
11028       IF PG_DEBUG = 'Y' THEN
11029          pa_debug.G_err_stage := 'Call ap_funds_check to reverse out the ap p_base_qty_var_amt = '||p_base_qty_var_amt;
11030          log_message('log_message: ' || pa_debug.G_err_Stage);
11031       END IF;
11032 
11033       ap_funds_check (
11034                      p_txn_source         => 'AP VARIANCE',
11035                      p_acct_raw_cost      => p_base_qty_var_amt,
11036                      p_packet_id          => P_packet_id,
11037                      p_sys_ref2           => g_cdl_system_reference2,
11038                      p_sys_ref3           => g_cdl_system_reference3,
11039 		     p_sys_ref5           => g_cdl_system_reference5, --REL12
11040 		     p_adj_exp_item_id    => p_adj_exp_item_id,       --REL12
11041 		     p_txn_interface_id   => p_txn_interface_id,      --REL12
11042 		     p_fc_document_type   => p_fc_document_type,      --REL12
11043                      x_error_message_code => x_error_message_code,
11044                      x_error_stage        => x_error_stage,
11045                      x_return_status      => x_return_status);
11046 
11047    END IF; --IF NVL(p_base_qty_var_amt,0) <> 0 THEN
11048 
11049 
11050    IF NVL(p_base_amt_var_amt,0) <> 0 THEN
11051 
11052       IF PG_DEBUG = 'Y' THEN
11053          pa_debug.G_err_stage := 'Call ap_funds_check to reverse out the ap p_base_amt_var_amt = '||p_base_amt_var_amt;
11054          log_message('log_message: ' || pa_debug.G_err_Stage);
11055       END IF;
11056 
11057       ap_funds_check (
11058                      p_txn_source         => 'AP VARIANCE',
11059                      p_acct_raw_cost      => p_base_amt_var_amt,
11060                      p_packet_id          => P_packet_id,
11061                      p_sys_ref2           => g_cdl_system_reference2,
11062                      p_sys_ref3           => g_cdl_system_reference3,
11063 		     p_sys_ref5           => g_cdl_system_reference5, --REL12
11064 		     p_adj_exp_item_id    => p_adj_exp_item_id,       --REL12
11065 		     p_txn_interface_id   => p_txn_interface_id,      --REL12
11066 		     p_fc_document_type   => p_fc_document_type,      --REL12
11067                      x_error_message_code => x_error_message_code,
11068                      x_error_stage        => x_error_stage,
11069                      x_return_status      => x_return_status);
11070 
11071    END IF; --IF NVL(p_base_amt_var_amt,0) <> 0 THEN
11072 
11073    IF PG_DEBUG = 'Y' THEN
11074       log_message('log_message: ' || 'Returning from ap_po funds check');
11075    END IF;
11076 
11077    -- set the return status to success
11078    x_return_status := FND_API.G_RET_STS_SUCCESS;
11079 
11080    pa_cc_utils.reset_curr_function;
11081 
11082   EXCEPTION
11083    WHEN OTHERS THEN
11084        IF PG_DEBUG = 'Y' THEN
11085           log_message('log_message: ' || sqlerrm||' Returning from ap po funds check');
11086        END IF;
11087        x_return_status  := fnd_api.g_ret_sts_unexp_error;
11088        pa_cc_utils.reset_curr_function;
11089        RAISE;
11090   END ap_po_funds_check;
11091 
11092   PROCEDURE Upd_PktSts_Fatal(p_request_id in number) IS
11093 
11094     PRAGMA AUTONOMOUS_TRANSACTION;
11095 
11096   BEGIN
11097 
11098     IF PG_DEBUG = 'Y' THEN
11099        log_message('log_message: ' || 'Inside Upd_PktSts_Fatal');
11100     END IF;
11101 
11102     update pa_bc_packets
11103     set status_code = 'T',
11104       result_code = 'F140'
11105     where status_code = 'P'
11106     and request_id = P_REQUEST_ID;
11107 
11108     IF PG_DEBUG = 'Y' THEN
11109        log_message('log_message: ' || 'Updated ' || SQL%ROWCOUNT || ' packet statuses to T');
11110     END IF;
11111 
11112     commit;
11113 
11114   EXCEPTION
11115 
11116     WHEN OTHERS THEN
11117       --Bug 2672772
11118        rollback;
11119       IF PG_DEBUG = 'Y' THEN -- Printed for 3592289
11120          log_message('log_message: In others of UpdPktsts_Fatal');
11121       END IF;
11122       raise;
11123 
11124   END Upd_PktSts_Fatal;
11125 
11126   --Bug 2672772 : Added the below procedure to update the packet status to T
11127   --if NDF error occurs in ap_funds_check and po_funds_check
11128   PROCEDURE Upd_PktSts(p_packet_id in number) IS
11129 
11130     --PRAGMA AUTONOMOUS_TRANSACTION;
11131 
11132   BEGIN
11133 
11134     IF PG_DEBUG = 'Y' THEN
11135        log_message('log_message: ' || 'Inside Upd_PktSts,packet_id = '|| p_packet_id);
11136     END IF;
11137 
11138     update pa_bc_packets
11139     set status_code = 'T',
11140       result_code = 'F140'
11141     where status_code = 'P'
11142     --Bug 3592289 changed p_packet_id to packet_id
11143     --and p_packet_id = p_packet_id;
11144     and packet_id = p_packet_id;
11145 
11146     IF PG_DEBUG = 'Y' THEN
11147        log_message('log_message: ' || 'Updated ' || SQL%ROWCOUNT || ' packet statuses to T');
11148     END IF;
11149 
11150     --commit;
11151 
11152   EXCEPTION
11153 
11154     WHEN OTHERS THEN
11155       --Bug 2672772
11156       --rollback;
11157       IF PG_DEBUG = 'Y' THEN  -- Added 3 lines for 3592289
11158          log_message('log_message: In others of UpdPktsts');
11159       END IF;
11160       raise;
11161 
11162   END Upd_PktSts;
11163 
11164   PROCEDURE ValidateItemOTL(
11165                X_trx_src      IN VARCHAR2
11166             ,  X_ei_date      IN DATE
11167             ,  X_etype        IN VARCHAR2
11168             ,  X_nlr          IN VARCHAR2
11169             ,  X_qty          IN NUMBER
11170             ,  X_denom_raw_cost     IN NUMBER
11171             ,  X_module       IN VARCHAR2
11172             ,  X_trx_ref      IN VARCHAR2
11173             ,  X_match_flag   IN VARCHAR2
11174             ,  X_att_cat      IN VARCHAR2
11175             ,  X_att1         IN OUT NOCOPY VARCHAR2
11176             ,  X_att2         IN OUT NOCOPY VARCHAR2
11177             ,  X_att3         IN OUT NOCOPY VARCHAR2
11178             ,  X_att4         IN OUT NOCOPY VARCHAR2
11179             ,  X_att5         IN OUT NOCOPY VARCHAR2
11180             ,  X_att6         IN OUT NOCOPY VARCHAR2
11181             ,  X_att7         IN OUT NOCOPY VARCHAR2
11182             ,  X_att8         IN OUT NOCOPY VARCHAR2
11183             ,  X_att9         IN OUT NOCOPY VARCHAR2
11184             ,  X_att10        IN OUT NOCOPY VARCHAR2
11185             ,  X_system_linkage IN VARCHAR2
11186             ,  X_status       OUT NOCOPY VARCHAR2
11187             --,  X_bill_flag    OUT NOCOPY VARCHAR2
11188 	        , X_denom_currency_code     IN OUT NOCOPY VARCHAR2
11189 	        , X_acct_rate_date  	       IN OUT NOCOPY DATE
11190 	        , X_acct_rate_type          IN OUT NOCOPY VARCHAR2
11191 	        , X_acct_exchange_rate      IN OUT NOCOPY NUMBER
11192 	        , X_acct_raw_cost           IN NUMBER
11193 	        , X_project_currency_code   IN OUT NOCOPY VARCHAR2
11194             , X_Projfunc_currency_code  IN OUT NOCOPY VARCHAR2
11195             , X_Projfunc_cost_rate_date      IN OUT NOCOPY DATE
11196             , X_Projfunc_cost_rate_type      IN OUT NOCOPY VARCHAR2
11197             , X_Projfunc_cost_exchange_rate  IN OUT NOCOPY VARCHAR2
11198             , X_Assignment_Name           IN OUT NOCOPY VARCHAR2
11199             , X_Work_Type_Name            IN OUT NOCOPY VARCHAR2
11200             , P_project_id               IN NUMBER
11201             , P_task_id                  IN NUMBER
11202             , P_person_id                IN NUMBER
11203             , P_organization_id          IN NUMBER
11204             , P_assignment_id            IN NUMBER
11205             , P_work_type_id             IN NUMBER
11206             , P_Emp_Org_Id               IN NUMBER
11207             , P_Emp_Job_Id               IN NUMBER
11208             , P_po_header_id       IN NUMBER
11209             , P_po_line_id       IN NUMBER
11210             , P_person_type        IN VARCHAR2
11211             , P_po_price_type        IN VARCHAR2
11212 		    , p_vendor_id			In Number	/* Bug# 3601024 : Vendor ID is not passed to the PA_EXPENDITURE_ITEMS_ALL.VENDOR_ID in OTL timecards  */
11213 		   )
11214   IS
11215 
11216     temp_status      VARCHAR2(30) DEFAULT NULL;
11217     temp_bill_flag   VARCHAR2(1)  DEFAULT NULL;
11218     temp_msg_application VARCHAR2(50) := 'PA';
11219     temp_msg_type 	VARCHAR2(1) := 'E';
11220     temp_msg_token1  VARCHAR2(2000) := '';
11221     temp_msg_token2	VARCHAR2(2000) :='';
11222     temp_msg_token3	VARCHAR2(2000) :='';
11223     temp_msg_count	NUMBER :=1;
11224 
11225     dummy            NUMBER       DEFAULT NULL;
11226 
11227     l_project_rate_type  VARCHAR2(30) := NULL;
11228 
11229     l_projfunc_cost_rate_type VARCHAR2(30) := NULL;
11230     l_asgn_work_ret_sts       VARCHAR2(1);
11231     l_asgn_work_err_msg       VARCHAR2(1000);
11232     l_temp_g_assignment_id    number := null; -- bug 5297060
11233 
11234   BEGIN
11235     pa_cc_utils.set_curr_function('ValidateItemOTL');
11236 
11237 
11238     G_adj_item_id := NULL;
11239     G_job_id := NULL;
11240 
11241 
11242     X_status := NULL;
11243 
11244     G_project_id := P_Project_Id;
11245     G_task_id := P_Task_Id;
11246     G_Person_Id := P_Person_Id;
11247 
11248     G_Org_Id := P_Emp_Org_Id;
11249 
11250 	G_Vendor_Id := P_Vendor_ID; /* Bug# 3601024 : Vendor ID is not passed to the PA_EXPENDITURE_ITEMS_ALL.VENDOR_ID in OTL timecards  */
11251 
11252     /*S.N. 5297060*/
11253     IF X_match_flag = 'N' THEN
11254 
11255          G_Assignment_Id := P_Assignment_Id;
11256          G_Work_Type_Id  := P_Work_Type_Id;
11257 
11258          G_Tp_Amt_Type_Code := pa_utils4.get_tp_amt_type_code(p_work_type_id => p_work_type_id);
11259 
11260     END IF;
11261 
11262     IF PG_DEBUG = 'Y' THEN
11263          log_message('log_message: G_Assignment_Id' ||G_Assignment_Id);
11264          log_message('log_message: G_Work_Type_Id' ||G_Work_Type_Id);
11265          log_message('log_message: G_Tp_Amt_Type_Code' ||G_Tp_Amt_Type_Code);
11266     END IF;
11267 
11268     /*E.N. 5297060*/
11269 
11270     IF ( G_Org_Id IS NULL ) THEN
11271        IF PG_DEBUG = 'Y' THEN
11272        pa_debug.G_err_Stage := 'P_Emp_Org_Id is null';
11273           log_message('log_message: ' || pa_debug.G_err_Stage);
11274        END IF;
11275        X_status := 'NO_ASSIGNMENT';
11276        pa_cc_utils.reset_curr_function;
11277        RETURN;
11278     END IF;
11279 
11280     IF (G_accounting_currency_code IS NULL) THEN
11281      GetImpCurrInfo;
11282     END IF;
11283 
11284     G_job_id := P_Emp_Job_Id;
11285 
11286     IF ( G_job_id IS NULL ) THEN
11287        X_status := 'NO_ASSIGNMENT';
11288        pa_cc_utils.reset_curr_function;
11289        RETURN;
11290     END IF;
11291 
11292     IF ( X_denom_currency_code IS NULL ) THEN
11293 
11294       X_denom_currency_code := G_accounting_currency_code;
11295 
11296     END IF;
11297 
11298     IF PG_DEBUG = 'Y' THEN
11299     pa_debug.G_err_stage := 'Calling GetProjCurrInfo';
11300        log_message('log_message: ' || pa_debug.G_err_Stage);
11301     END IF;
11302 
11303     GetProjCurrInfo(G_task_id,
11304 		X_project_currency_code,
11305 		l_project_rate_type,
11306                 X_projfunc_currency_code,
11307                 l_projfunc_cost_rate_type);
11308 
11309     IF ( X_project_currency_code IS NULL ) THEN
11310 
11311     	X_status := 'PA_MISSING_PROJ_CURR';
11312         pa_cc_utils.reset_curr_function;
11313     	RETURN;
11314 
11315     END IF;
11316 
11317     IF ( X_projfunc_currency_code IS NULL ) THEN
11318 
11319         X_status := 'PA_MISSING_PRJFUNC_CURR';
11320         pa_cc_utils.reset_curr_function;
11321         RETURN;
11322 
11323     END IF;
11324 
11325 /* Added the call to GetProjTypeInfo for getting the project type class for
11326  * bug#4903329 */
11327     IF PG_DEBUG = 'Y' THEN
11328        pa_debug.G_err_stage := 'Calling GetProjTypeInfo';
11329        log_message('log_message: ' || pa_debug.G_err_Stage);
11330     END IF;
11331 
11332 
11333     GetProjTypeInfo( G_project_id,
11334                      G_Proj_bcost_flag,
11335                      G_project_type_class,
11336                      G_burden_amt_display_method,
11337                      G_Total_Burden_Flag ) ;
11338 /* Ends changes for bug#4903329 */
11339 
11340     -- ===========================================================================
11341     --   Verify that the transaction has not already been loaded into PA (no
11342     --   other expenditure items exist having the same TRANSACTION SOURCE and
11343     --   ORIG TRANSACTION REFERENCE)
11344 
11345     IF G_allow_dup_flag <>  'Y' THEN
11346 
11347        IF PG_DEBUG = 'Y' THEN
11348        pa_debug.G_err_stage := 'Calling CheckDupItem';
11349           log_message('log_message: ' || pa_debug.G_err_Stage);
11350        END IF;
11351        CheckDupItem ( X_trx_src, X_trx_ref, temp_status );
11352        IF ( temp_status IS NOT NULL ) THEN
11353          X_status := temp_status;
11354          pa_cc_utils.reset_curr_function;
11355          RETURN;
11356        END IF;
11357 
11358     END IF ;
11359 
11360     -- ===========================================================================
11361     --  Oracle Time and Labor(OTL)requires the ability to reverse an ei with a quantity = 0
11362     --  so only for OTL that option has been added.
11363     --
11364     IF  ( ( X_qty < 0    and    X_match_flag = 'N' ) OR
11365           ( X_match_flag = 'N' and X_trx_src = 'ORACLE TIME AND LABOR' ) ) THEN
11366       IF PG_DEBUG = 'Y' THEN
11367       pa_debug.G_err_stage := 'Calling pa_adjustments.VerifyOrigItem';
11368          log_message('log_message: ' || pa_debug.G_err_Stage);
11369       END IF;
11370       G_adj_item_id := pa_adjustments.VerifyOrigItem(
11371                 X_person_id => G_person_id
11372               , X_org_id => G_org_id
11373               , X_item_date => X_ei_date
11374               , X_task_id => G_task_id
11375               , X_exp_type => X_etype
11376               , X_system_linkage_function => X_system_linkage
11377               , X_nl_org_id => NULL
11378               , X_nl_resource => X_nlr
11379               , X_quantity => X_qty
11380               , X_denom_raw_cost => X_denom_raw_cost
11381               , X_trx_source => X_trx_src
11382               , X_denom_currency_code => X_denom_currency_code
11383               , X_acct_raw_cost => X_acct_raw_cost
11384               , X_reversed_orig_txn_reference => G_reversed_orig_txn_reference);
11385 
11386       IF ( G_adj_item_id IS NULL ) THEN
11387 
11388         X_status := 'NO_MATCHING_ITEM';
11389         pa_cc_utils.reset_curr_function;
11390         RETURN;
11391 
11392       ELSIF ( G_adj_item_id IS NOT NULL  AND   X_module = 'PAXTRTRX' ) THEN
11393 
11394         IF PG_DEBUG = 'Y' THEN
11395         pa_debug.G_err_stage := 'Calling pa_adjustments.ExpAdjItemTab';
11396            log_message('log_message: ' || pa_debug.G_err_Stage);
11397         END IF;
11398         pa_adjustments.ExpAdjItemTab(G_adj_item_id) := G_adj_item_id;
11399 
11400         IF PG_DEBUG = 'Y' THEN
11401         pa_debug.G_err_stage := 'Locking adjusting item:'||to_char(G_adj_item_id);
11402            log_message('log_message: ' || pa_debug.G_err_Stage);
11403         END IF;
11404 
11405         BEGIN
11406 
11407           SELECT
11408                   expenditure_item_id
11409             INTO
11410                   dummy
11411             FROM
11412                   pa_expenditure_items
11413            WHERE
11414                   expenditure_item_id = G_adj_item_id
11415           FOR UPDATE NOWAIT;
11416 
11417         EXCEPTION
11418           WHEN  RESOURCE_BUSY  THEN
11419             temp_status := 'CANNOT_LOCK_ORIG_ITEM';
11420         END;
11421       END IF;
11422 
11423       IF ( temp_status IS NOT NULL ) THEN
11424         X_status := temp_status;
11425         pa_cc_utils.reset_curr_function;
11426         RETURN;
11427       END IF;
11428 
11429     END IF;
11430 
11431     IF PG_DEBUG = 'Y' THEN
11432     pa_debug.G_err_stage := 'Calling PA_UTILS2.GetPrjOrgId';
11433        log_message('log_message: ' || pa_debug.G_err_Stage);
11434     END IF;
11435     G_RecvrOrgId := PA_UTILS2.GetPrjOrgId(p_project_id => G_project_id,
11436                                           p_task_id    => NULL);
11437 
11438     IF PG_DEBUG = 'Y' THEN
11439     pa_debug.G_err_stage := 'Calling API to derive/validate assignment and work type info';
11440        log_message('log_message: ' || pa_debug.G_err_Stage);
11441        log_message('log_message: ' || 'Assignment Name = ' || X_Assignment_Name || ' Work Type Name = ' || X_Work_Type_Name);
11442     END IF;
11443 
11444     /*S.N. 5297060*/
11445     If ( X_match_flag <> 'N' )  THEN
11446 
11447          pa_utils4.get_work_assignment(
11448 	          p_person_id          => G_Person_Id
11449             , p_project_id         => G_Project_Id
11450             , p_task_id            => G_Task_Id
11451             , p_ei_date            => X_Ei_Date
11452             , p_system_linkage     => x_system_linkage
11453             , x_assignment_id      => G_Assignment_Id
11454             , x_assignment_name    => X_Assignment_Name
11455             , x_work_type_id       => G_Work_Type_Id
11456             , x_work_type_name     => X_Work_Type_Name
11457             , x_tp_amt_type_code   => G_Tp_Amt_Type_Code
11458             , x_return_status      => l_asgn_work_ret_sts
11459             , x_error_message_code => l_asgn_work_err_msg);
11460 
11461          IF PG_DEBUG = 'Y' THEN
11462               log_message('log_message: ' || 'Assignment Id = ' || G_Assignment_Id ||
11463                                              ' Work Type Id = ' || G_Work_Type_Id ||
11464                                              ' Tp Amt Type = '  || G_Tp_Amt_Type_Code);
11465          END IF;
11466 
11467          IF (l_asgn_work_ret_sts <> FND_API.G_RET_STS_SUCCESS) THEN
11468 
11469               IF PG_DEBUG = 'Y' THEN
11470                    pa_debug.G_err_stage := 'Get Work Type and Assignment API failed';
11471                    log_message('log_message: ' || pa_debug.G_err_Stage);
11472                    log_message('log_message: ' || 'Ret Sts = ' || l_asgn_work_ret_sts || ' Error = ' || l_asgn_work_err_msg);
11473               END IF;
11474 
11475               X_Status := l_asgn_work_err_msg;
11476               pa_cc_utils.reset_curr_function;
11477               RETURN;
11478 
11479          END IF;
11480 
11481     END IF; -- ( X_match_flag <> 'N' )
11482     /*E.N. 5297060*/
11483 
11484     G_CrossChargeCode := 'P';
11485     G_BrowLentDistCode := 'X';
11486     G_IcProcessed_Code := 'X';
11487     l_temp_g_assignment_id := G_Assignment_Id; -- bug 5297060
11488 
11489     IF PG_DEBUG = 'Y' THEN
11490     pa_debug.G_err_stage := 'Calling patc client extension for override to assignment id';
11491        log_message('log_message: ' || pa_debug.G_err_Stage);
11492     END IF;
11493 
11494     patcx.tc_extension( X_project_id => g_project_id
11495                       , X_task_id => g_task_id
11496                       , X_expenditure_item_date => X_ei_date
11497                       , X_expenditure_type => X_etype
11498                       , X_non_labor_resource => X_nlr
11499                       , X_incurred_by_person_id => g_person_id
11500                       , X_quantity => X_qty
11501                       , X_denom_currency_code => X_denom_currency_code
11502                       , X_acct_currency_code => G_accounting_currency_code
11503                       , X_denom_raw_cost => X_denom_raw_cost
11504                       , X_acct_raw_cost => X_acct_raw_cost
11505                       , X_acct_rate_type => X_acct_rate_type
11506                       , X_acct_rate_date => X_acct_rate_date
11507                       , X_acct_exchange_rate => X_acct_exchange_rate
11508                       , X_transferred_from_id => NULL
11509                       , X_incurred_by_org_id => G_org_id
11510                       , X_nl_resource_org_id => NULL
11511                       , X_transaction_source => X_trx_src
11512                       , X_calling_module => X_module
11513                       , X_vendor_id => NULL
11514                       , X_entered_by_user_id => G_user
11515                       , X_attribute_category => X_att_cat
11516                       , X_attribute1 => X_att1
11517                       , X_attribute2 => X_att2
11518                       , X_attribute3 => X_att3
11519                       , X_attribute4 => X_att4
11520                       , X_attribute5 => X_att5
11521                       , X_attribute6 => X_att6
11522                       , X_attribute7 => X_att7
11523                       , X_attribute8 => X_att8
11524                       , X_attribute9 => X_att9
11525                       , X_attribute10 => X_att10
11526                       , X_attribute11 => NULL
11527                       , X_attribute12 => NULL
11528                       , X_attribute13 => NULL
11529                       , X_attribute14 => NULL
11530                       , X_attribute15 => NULL
11531                       , X_msg_application => temp_msg_application
11532                       , X_billable_flag => temp_bill_flag
11533                       , X_msg_type => temp_msg_type
11534                       , X_msg_token1 => temp_msg_token1
11535                       , X_msg_token2 => temp_msg_token2
11536                       , X_msg_token3 => temp_msg_token3
11537                       , X_msg_count => temp_msg_count
11538                       , X_outcome => temp_status
11539                       , p_projfunc_currency_code   => x_projfunc_currency_code
11540                       , p_projfunc_cost_rate_type  => x_projfunc_cost_rate_type
11541                       , p_projfunc_cost_rate_date  => x_projfunc_cost_rate_date
11542                       , p_projfunc_cost_exchg_rate => X_Projfunc_cost_exchange_rate
11543                       , x_assignment_id            => G_ASSIGNMENT_ID
11544                       , p_work_type_id             => g_work_type_id
11545                       , p_sys_link_function        => x_system_linkage
11546                       , p_po_header_id  =>  p_po_header_id /* cwk */
11547                       , p_po_line_id => p_po_line_id
11548                       , p_person_type => p_person_type
11549                       , p_po_price_type => p_po_price_type );
11550 
11551     -- No change to Billable Flag here, use what is inserted into the interface table
11552           X_status := temp_status; -- Added for bug#6278593
11553     /*S.N. 5297060  The patcx extension parameter x_assignment_id is IN OUT so the value can change
11554                     for assignment_id.  To handle this we will use a dummy assigment id to hold the value
11555                     and reset the value back if the transaction is a reversal. */
11556     If X_match_flag = 'N' Then
11557          G_assignment_id := l_temp_g_assignment_id;
11558     End If;
11559     /*E.N. 5297060*/
11560 
11561     pa_cc_utils.reset_curr_function;
11562 
11563   EXCEPTION
11564     WHEN OTHERS THEN
11565        IF PG_DEBUG = 'Y' THEN
11566           log_message('log_message: ' || pa_debug.G_err_Stack,1);
11567           log_message('log_message: ' || pa_debug.G_err_stage,1);
11568           log_message('log_message: ' || SQLERRM,1);
11569        END IF;
11570        pa_cc_utils.reset_curr_function;
11571        RAISE ;
11572   END  ValidateItemOTL;
11573 
11574   PROCEDURE Log_Message(p_message in VARCHAR2,
11575                         p_mode    in NUMBER DEFAULT 0) IS
11576   BEGIN
11577     If (G_Debug_Mode = 'Y') Then
11578 
11579        pa_cc_utils.log_message(p_message,p_mode);
11580 
11581     End If;
11582   END Log_Message;
11583 
11584 PROCEDURE init_po_amt_chk IS
11585 
11586 BEGIN
11587 
11588   PoLineTaskTab.DELETE;
11589   PoAmtTab.DELETE;
11590 
11591 END init_po_amt_chk;
11592 
11593 PROCEDURE  release_po_line_task_lock IS
11594 PRAGMA AUTONOMOUS_TRANSACTION;
11595 l_lock_status     NUMBER         := -1;
11596 BEGIN
11597 
11598   IF PoLineTaskTab.COUNT > 0 THEN
11599      FOR i IN PoLineTaskTab.FIRST..PoLineTaskTab.LAST
11600      LOOP
11601      BEGIN
11602 
11603        l_lock_status := -1;
11604        l_lock_status :=  Pa_Debug.Release_User_Lock(PoLineTaskTab(i));
11605 
11606        IF (l_lock_status = 0) THEN
11607            IF PG_DEBUG = 'Y' THEN
11608               pa_debug.G_err_stage := 'RELEASE_PO_LINE_TASK_LOCK : Released Lock for : '||PoLineTaskTab(i);
11609               log_message('log_message: ' || pa_debug.G_err_stage,1);
11610            END IF;
11611        ELSE
11612            IF PG_DEBUG = 'Y' THEN
11613               pa_debug.G_err_stage := 'RELEASE_PO_LINE_TASK_LOCK : Failed To Release Lock for : '||PoLineTaskTab(i);
11614               log_message('log_message: ' || pa_debug.G_err_stage,1);
11615            END IF;
11616        END IF;
11617 
11618      EXCEPTION
11619      WHEN OTHERS THEN
11620 
11621      IF PG_DEBUG = 'Y' THEN
11622         pa_debug.G_err_stage := 'RELEASE_PO_LINE_TASK_LOCK : In Process of Releasing Locks : ' ||SQLERRM;
11623         log_message('log_message: ' || pa_debug.G_err_stage,1);
11624      END IF;
11625 
11626      END ;
11627 
11628      END LOOP;
11629 
11630    END IF ;
11631 
11632 END release_po_line_task_lock;
11633 
11634 /*
11635  API Decs : Retrieving rate from PO for the PO_LINE_ID And PO_PRICE_TYPE for CWK related Time Card EIs.
11636 */
11637 
11638 Function GET_PO_PRICE_TYPE_RATE (p_project_id In Number, p_task_id In Number, P_Po_Line_Id In Number, P_Price_Type In Varchar2) Return Number
11639 Is
11640 
11641 	x_po_rate			NUMBER	;
11642 	x_currency_code     VARCHAR (15) ;
11643 	x_curr_rate_type    VARCHAR (30) ;
11644 	x_curr_rate_date    DATE	;
11645 	x_currency_rate     NUMBER	;
11646 	x_vendor_id         NUMBER	;
11647 	x_return_status     VARCHAR2(100) ;
11648 	x_message_code      VARCHAR2(100) ;
11649 
11650 Begin
11651 
11652 
11653 	PO_PA_INTEGRATION_GRP.get_line_rate_info(   p_api_version    => 1.0 ,
11654 											    p_price_type     => p_price_type ,
11655 						                        p_po_line_id     => p_po_line_id ,
11656 										        p_project_id     => p_project_id ,
11657 				                                p_task_id        => p_task_id ,
11658 								                x_po_rate        => x_po_rate ,
11659 				                                x_currency_code  => x_currency_code  ,
11660 				                                x_curr_rate_type => x_curr_rate_type ,
11661 									            x_curr_rate_date => x_curr_rate_date ,
11662 					                            x_currency_rate  => x_currency_rate ,
11663 									            x_vendor_id      => x_vendor_id ,
11664 					                            x_return_status  => x_return_status ,
11665 									            x_message_code   => x_message_code
11666 				                            );
11667 
11668 	Return ( x_po_rate );
11669 
11670 End GET_PO_PRICE_TYPE_RATE ;
11671 
11672   PROCEDURE ap_cash_based_funds_check (
11673           p_txn_source            IN VARCHAR2,
11674           p_acct_raw_cost         IN NUMBER,
11675           p_packet_id             IN NUMBER,
11676           p_sys_ref2              IN NUMBER,        --REL12
11677           p_sys_ref4              IN VARCHAR2,        --REL12
11678 	  p_sys_ref5              IN NUMBER,        --REL12
11679 	  p_adj_exp_item_id       IN NUMBER,        --REL12
11680 	  p_txn_interface_id      IN NUMBER,        --REL12
11681 	  p_fc_document_type      IN VARCHAR2,      --REL12
11682           x_error_message_code    OUT NOCOPY VARCHAR2,
11683           x_error_stage           OUT NOCOPY VARCHAR2,
11684           x_return_status         OUT NOCOPY VARCHAR2) IS
11685 
11686     l_raw_amount              NUMBER;
11687 
11688     Procedure GetCommSummAmt( p_ap_pkt_raw_amt  OUT NOCOPY NUMBER) Is
11689 
11690          l_pkt_summ                NUMBER;
11691          l_com_summ                NUMBER;
11692 	 l_ap_outstanding_raw      NUMBER;
11693 	 l_pay_inv_dist_amt        NUMBER;
11694 	 -- R12 AP lines uptake : Prepayment changes
11695 	 l_cmt_pkt_found           VARCHAR2(1) := 'N';
11696 	 l_disc_amount             NUMBER ;
11697 
11698      -- Below cursors calcultes Raw and Burden outstanding amount associated with AP Invoice Distribution.
11699 
11700      -- Fetch BC Pkt Raw outstanding amount
11701      CURSOR C_get_raw_pkt_amt IS
11702      select sum(nvl(accounted_dr,0)-nvl(accounted_cr,0))
11703        from pa_bc_packets bcc
11704       where bcc.document_type = 'AP'
11705         and bcc.document_header_id = p_sys_ref2
11706         and bcc.document_distribution_id = p_sys_ref5
11707         and ( (bcc.Status_Code in('A','C'))
11708 		 or
11709 	      (bcc.Status_Code = 'P' and bcc.request_id = G_Request_Id))
11710         and bcc.Parent_Bc_Packet_Id is NULL;
11711 
11712      CURSOR C_get_raw_bccmt_amt IS
11713      select sum(nvl(accounted_dr,0)-nvl(accounted_cr,0))
11714        from pa_bc_commitments bcc
11715       where bcc.document_type = 'AP'
11716         and bcc.document_header_id = p_sys_ref2
11717         and bcc.document_distribution_id = p_sys_ref5
11718         and  bcc.Parent_Bc_Packet_Id is NULL;
11719 
11720      -- ====================================================================================
11721      --
11722      -- Bug : 4962731
11723      --     : R12.PJ:XB1:QA:BC:INCORRECT AMOUNTS INTERFACED TO PROJECTS IN CASH BASED ACC
11724      --       For payments, payment amount includes discount amount. So we are interfacing
11725      --       only payments. But we need to relieve corresponding invoice amount for that
11726      --       payment.
11727      --       Invoice dist amount :100
11728      --                  Payment  : 80
11729      --                  Disc     : 20
11730      --          Actual interface : 80
11731      --          AP Relieve       : 80 + 20 = 100
11732      -- Functionality :
11733      --       Discount is applicable when discount method is EXPENSE
11734      --       Discount is applicable for tax distributions  when discount method is TAX
11735      --       Discount is not applicable when discount method is 'SYSTEM'
11736      --       Discount is also based on the discount profile start date
11737      --       ap payment record includes the discount amount and we do not need to interface
11738      --       discount record because we are interfacing the payments.
11739      --       But we need to relieve corresponding inv dist amount paid to relieve the ap commitment amount.
11740      --       ap amount to relieve := payment amunt + discount amount (when applicable).
11741      -- ====================================================================================
11742      CURSOR c_get_disc_amount is
11743           SELECT  NVL(b.invoice_dist_base_amount , b.invoice_dist_amount) amount
11744 	    from ap_payment_hist_dists b,
11745 	         ap_invoice_distributions_all apd
11746 	   where b.invoice_payment_id      = p_sys_ref4
11747 	     and b.invoice_distribution_id = p_sys_ref5
11748 	     and b.pay_dist_lookup_code    = 'DISCOUNT'
11749 	     and apd.invoice_distribution_id = b.invoice_distribution_id
11750 	     and NVL(apd.historical_flag,'N')       <> 'Y'
11751 	     and apd.expenditure_item_date  >=G_Profile_Discount_Start_date
11752 	     and apd.line_type_lookup_code  = decode ( G_discount_Method,
11753 	                                                            'TAX', decode (apd.line_type_lookup_code,
11754                                                                                                       'TIPV', 'TIPV',
11755 												      'TERV','TERV',
11756 												      'TRV', 'TRV',
11757 												      'NONREC_TAX') ,
11758 	                                                            'SYSTEM', 'NOT APPLICABLE',
11759 								     apd.line_type_lookup_code ) ;
11760      -- AP Invoice dist amount to be relieved for payment with ERV
11761      CURSOR   C_payment_erv_amount IS
11762      SELECT   NVL(paydist.invoice_dist_base_amount,paydist.invoice_dist_amount)
11763        FROM   ap_payment_hist_dists Paydist
11764       WHERE   paydist.pay_dist_lookup_code = 'CASH'
11765         AND   Paydist.invoice_distribution_id = p_sys_ref5
11766         AND   PayDIST.invoice_payment_id = p_sys_ref4
11767         AND   EXISTS
11768 	        (SELECT 1
11769                    FROM ap_invoice_payments Pay,
11770 		        ap_invoices_all inv
11771                   WHERE pay.invoice_payment_id   = p_sys_ref4
11772 		    AND pay.invoice_id           =  inv.invoice_id
11773 		    AND NVL(pay.exchange_rate,0) <> NVL(inv.exchange_rate,0));
11774 
11775      Begin
11776         -- Intializing Variables
11777         l_pkt_summ            := 0;
11778         l_com_summ            := 0;
11779 	p_ap_pkt_raw_amt      := 0;
11780 	l_pay_inv_dist_amt    := 0;
11781 	l_disc_amount         := 0 ;
11782 
11783 	-- Fetching applicable discount amount absed on the discount method and profile discount start date.
11784         --
11785         -- Bug : 4962731
11786         --     : R12.PJ:XB1:QA:BC:INCORRECT AMOUNTS INTERFACED TO PROJECTS IN CASH BASED ACC
11787         --
11788 	OPEN  c_get_disc_amount ;
11789 	fetch c_get_disc_amount into l_disc_amount ;
11790 	close c_get_disc_amount ;
11791 	l_disc_amount := NVL(l_disc_amount,0) ;
11792 
11793         -- Fetching outstanding AP Distribution Raw amount
11794 	OPEN   C_get_raw_pkt_amt ;
11795         FETCH  C_get_raw_pkt_amt INTO l_pkt_summ;
11796 	-- R12 AP lines uptake : Prepayment changes
11797 	IF C_get_raw_pkt_amt%FOUND THEN
11798 	  l_cmt_pkt_found := 'Y';
11799         END IF;
11800 	CLOSE  C_get_raw_pkt_amt;
11801 
11802 	OPEN   C_get_raw_bccmt_amt ;
11803         FETCH  C_get_raw_bccmt_amt INTO l_com_summ;
11804 	-- R12 AP lines uptake : Prepayment changes
11805 	IF C_get_raw_bccmt_amt%FOUND THEN
11806 	  l_cmt_pkt_found := 'Y';
11807         END IF;
11808 	CLOSE  C_get_raw_bccmt_amt;
11809 
11810         -- Calculating total Outstanding raw amount on AP dist
11811 	l_ap_outstanding_raw := nvl(l_pkt_summ,0) + nvl(l_com_summ,0) ;
11812 
11813        -- R12 AP lines uptake : Prepayment changes
11814        -- Donot relieve commitment amount for records which have no entry in bc_commitments/bc_packets .
11815        -- These records were not Fundschecked (eg : pre-payment application , pre-payment matched to PO ..)
11816 
11817        IF l_cmt_pkt_found = 'N' THEN
11818 
11819 	   p_ap_pkt_raw_amt := 0;
11820 
11821        ELSIF l_cmt_pkt_found = 'Y' THEN
11822 
11823         -- Below code populates l_pay_inv_dist_amt  with AP raw amount to be relieved against payments having ERV .
11824 	-- If no ERV for payment then l_pay_inv_dist_amt = p_acct_raw_cost
11825 
11826         OPEN  C_payment_erv_amount;
11827         FETCH C_payment_erv_amount INTO l_pay_inv_dist_amt;
11828 	IF C_payment_erv_amount%NOTFOUND THEN
11829 	   l_pay_inv_dist_amt := P_acct_raw_cost;
11830         END IF;
11831         CLOSE C_payment_erv_amount;
11832 
11833 
11834         --
11835         -- Bug : 4962731
11836         --     : R12.PJ:XB1:QA:BC:INCORRECT AMOUNTS INTERFACED TO PROJECTS IN CASH BASED ACC
11837         --
11838 	l_pay_inv_dist_amt := l_pay_inv_dist_amt + l_disc_amount ;
11839 
11840         -- If final payment relieve outstanding amount .
11841 	-- Note : For refund final payment zero outstanding amount will be reserved
11842         IF NVL(G_finalPaymentID,-99) = NVL(p_sys_ref4,-99) THEN
11843 	   p_ap_pkt_raw_amt := -1 * l_ap_outstanding_raw;
11844 
11845         -- If postive payment and not a final payment then relieve MIN(outstanding amount,payment amount)
11846         ELSIF l_pay_inv_dist_amt >0 THEN
11847 	   IF l_pay_inv_dist_amt >= l_ap_outstanding_raw THEN
11848 	      p_ap_pkt_raw_amt := -1 * l_ap_outstanding_raw;
11849            ELSE
11850 	      p_ap_pkt_raw_amt := -1 * l_pay_inv_dist_amt;
11851            END IF;
11852 
11853         -- If negative payment and not a final payment then reserve payment amount
11854 	ELSIF l_pay_inv_dist_amt < 0 THEN
11855 	      p_ap_pkt_raw_amt := -1 * l_pay_inv_dist_amt;
11856         END IF;
11857 
11858       END IF; --IF l_cmt_pkt_found = 'N' THEN
11859 
11860      Exception
11861         When Others Then
11862              Raise;
11863 
11864       End GetCommSummAmt;
11865 
11866   BEGIN
11867 
11868     pa_cc_utils.set_curr_function('ap_funds_check');
11869 
11870     IF PG_DEBUG = 'Y' THEN
11871     pa_debug.G_err_stage := 'Inside AP_Funds_Check';
11872        log_message('log_message: ' || pa_debug.G_err_Stage);
11873     END IF;
11874 
11875     -- Calling GetCommSummAmt to calculate AP distribution's raw and burden amount to be relieved/reserved.
11876     GetCommSummAmt(p_ap_pkt_raw_amt  => l_raw_amount);
11877 
11878     insert_cash_ap_bc_packets(   p_packet_id         => p_packet_id,
11879                             p_sys_ref2         => p_sys_ref2,
11880                             p_sys_ref5         => p_sys_ref5,
11881                             p_acct_raw_cost    => p_acct_raw_cost,
11882                             p_fc_document_type => p_fc_document_type ,
11883                             p_txn_source       => p_txn_source   ,
11884                             p_adj_exp_item_id  => p_adj_exp_item_id,
11885 			    p_txn_interface_id => p_txn_interface_id,
11886 			    p_cash_pay_to_relieve => l_raw_amount   );
11887 
11888 
11889     IF PG_DEBUG = 'Y' THEN
11890        log_message('log_message: ' || 'Returning from ap funds check');
11891     END IF;
11892 
11893     -- set the return status to success
11894     x_return_status := FND_API.G_RET_STS_SUCCESS;
11895 
11896     pa_cc_utils.reset_curr_function;
11897 
11898   EXCEPTION
11899     --Bug 2672772: Handle the NDF exception,
11900     --raise the error here but not in tr_import_funds_check
11901     WHEN NO_DATA_FOUND THEN
11902        IF PG_DEBUG = 'Y' THEN
11903           log_message('log_message: ' || sqlerrm||' In NDF: Returning from ap funds check');
11904        END IF;
11905 
11906     WHEN OTHERS THEN
11907        IF PG_DEBUG = 'Y' THEN
11908           log_message('log_message: ' || sqlerrm||' Returning from ap funds check');
11909        END IF;
11910        x_return_status  := fnd_api.g_ret_sts_unexp_error;
11911        pa_cc_utils.reset_curr_function;
11912        RAISE;
11913   END ap_cash_based_funds_check;
11914 
11915 -- R12 funds management Uptake : New procedure fired in non-autonomous mode to read
11916 -- pa_transaction_interface_all table and to derive variables which drives the
11917 -- relieving/reserving of commitments and actuals flow  during interface.
11918 
11919 PROCEDURE insert_ap_bc_packets(p_packet_id             IN NUMBER,
11920                                p_sys_ref2              IN NUMBER,
11921   			       p_sys_ref4              IN VARCHAR2,
11922                                p_sys_ref5              IN NUMBER,
11923                                p_acct_raw_cost         IN NUMBER,
11924                                p_acct_bur_cost         IN NUMBER,
11925                                p_fc_document_type      IN VARCHAR2,
11926                                p_txn_source            IN VARCHAR2,
11927                                p_adj_exp_item_id       IN NUMBER,
11928 		    	       p_txn_interface_id      IN NUMBER) IS
11929 
11930 -- Cursor to fetch data from pa_transaction_interface_table.This data is later used
11931 -- in autonomous fundscheck insert_ap_bc_pkts_autonomous procedure.
11932 
11933 
11934  CURSOR c_pa_txn_interface_data (p_adj_act_fc_req VARCHAR2,
11935                                  p_act_fc_req  VARCHAR2) IS
11936  Select txn.Project_Id
11937         ,txn.Task_Id
11938         ,txn.Expenditure_Type
11939         ,txn.Expenditure_Item_Date
11940         ,nvl(txn.OVERRIDE_TO_ORGANIZATION_ID,txn.Org_Id)
11941         ,bv.Budget_Version_Id
11942 	-- R12 AP lines uptake : Prepayment changes
11943 	-- For Prepayment application/pre-payment matched to PO records consider p_acct_raw_cost as amount
11944         ,DECODE(txn_interface_id,p_txn_interface_id, p_acct_raw_cost,txn.acct_raw_cost)
11945         ,0
11946         ,DECODE(txn_interface_id,p_txn_interface_id, p_acct_raw_cost,txn.acct_raw_cost)
11947         ,0
11948         ,txn.cr_code_combination_id
11949         ,txn.Org_id
11950         ,txn.txn_interface_id  --REL12
11951   From  Pa_Budget_Versions bv,
11952         Pa_Budget_Types bt,
11953         pa_budgetary_control_options pbct,
11954 	pa_transaction_interface_all txn
11955  Where  bv.budget_type_code = bt.budget_type_code
11956    and  bt.budget_amount_code = 'C'
11957    and  bv.current_flag = 'Y'
11958    AND  pbct.project_id = bv.project_id
11959    AND  pbct.BDGT_CNTRL_FLAG = 'Y'
11960    AND  pbct.BUDGET_TYPE_CODE = bv.budget_type_code
11961    AND (pbct.EXTERNAL_BUDGET_CODE = 'GL'
11962         OR
11963         pbct.EXTERNAL_BUDGET_CODE is NULL
11964         )
11965    AND  bv.project_id = txn.project_id
11966    AND  txn.txn_interface_id in ( SELECT txn1.txn_interface_id
11967                                    FROM pa_transaction_interface_all txn1
11968  				   WHERE txn1.TRANSACTION_SOURCE = p_txn_source -- Condition for using index
11969 				   -- new index usage needs to be verified for below columns
11970                                      AND txn1.cdl_system_reference2 = p_sys_ref2
11971                                      AND txn1.cdl_system_reference5 = p_sys_ref5
11972                                      AND txn1.fc_document_type in ('ACT','ALL')
11973 	                             AND txn1.adjusted_expenditure_item_id IS NOT NULL
11974 				     AND p_adj_act_fc_req = 'Y'
11975                                 UNION ALL -- R12 AP lines uptake : Prepayment changes : Added to pick current transaction for prepayment scenarios
11976 				  SELECT p_txn_interface_id
11977 				    FROM DUAL
11978                                    WHERE p_act_fc_req  = 'Y');
11979 
11980 l_comm_fc_req             VARCHAR2(1);  -- Variable to identify whether commitment fundscheck is required
11981 l_act_fc_req              VARCHAR2(1);  -- Variable to identify whether actual fundscheck is required
11982 l_adj_act_fc_req          VARCHAR2(1);  -- Variable to identify whether adjusted actual fundscheck is required
11983 
11984 
11985 BEGIN
11986 
11987     IF PG_DEBUG = 'Y' THEN
11988        log_message('log_message: ' || 'In insert_bc_packets - Start');
11989        log_message('log_message: ' || 'In insert_bc_packets - Value of p_packet_id '||p_packet_id);
11990        log_message('log_message: ' || 'In insert_bc_packets - Value of p_sys_ref2  '||p_sys_ref2 );
11991        log_message('log_message: ' || 'In insert_bc_packets - Value of p_sys_ref4  '||p_sys_ref4 );
11992        log_message('log_message: ' || 'In insert_bc_packets - Value of p_sys_ref5  '||p_sys_ref5 );
11993        log_message('log_message: ' || 'In insert_bc_packets - Value of p_acct_raw_cost '||p_acct_raw_cost);
11994        log_message('log_message: ' || 'In insert_bc_packets - Value of p_acct_bur_cost '||p_acct_bur_cost);
11995        log_message('log_message: ' || 'In insert_bc_packets - Value of p_fc_document_type  '||p_fc_document_type );
11996        log_message('log_message: ' || 'In insert_bc_packets - Value of p_txn_source '||p_txn_source);
11997        log_message('log_message: ' || 'In insert_bc_packets - Value of p_adj_exp_item_id '||p_adj_exp_item_id);
11998        log_message('log_message: ' || 'In insert_bc_packets - Value of p_txn_interface_id '||p_txn_interface_id);
11999 
12000     END IF;
12001 
12002     -- Code to derive values for variables which will decide the flow of this procedure
12003 
12004     l_comm_fc_req :='N';        -- Variable to identify whether commitment fundscheck is required
12005     l_act_fc_req :='N';         -- Variable to identify whether actual fundscheck is required
12006     l_adj_act_fc_req:='N';      -- Variable to identify whether adjusted actual fundscheck is required
12007 
12008     IF  p_adj_exp_item_id IS NULL THEN
12009 
12010         IF p_fc_document_type ='ALL' THEN  --ap normal lines import
12011            l_comm_fc_req := 'Y' ;
12012            l_act_fc_req  := 'Y' ;
12013         ELSIF p_fc_document_type ='ACT' OR p_txn_source = 'AP DISCOUNTS' THEN --ap discounts lines import
12014             l_act_fc_req  := 'Y' ;
12015         END IF;
12016 
12017     ELSIF p_adj_exp_item_id IS NOT NULL THEN
12018 
12019         IF p_fc_document_type ='ALL' THEN    --ap invoice having fc enabled and adjusted ei's having fc enabled
12020            l_comm_fc_req := 'Y' ;
12021            l_adj_act_fc_req := 'Y';
12022         ELSIF p_fc_document_type ='CMT' THEN --ap invoice having fc enabled and adjusted ei's having fc disabled
12023            l_comm_fc_req := 'Y' ;
12024         ELSIF p_fc_document_type ='ACT' THEN --ap invoice having fc disabled and adjusted ei's having fc ENabled
12025            l_adj_act_fc_req := 'Y';
12026         END IF;
12027 
12028     END IF;
12029 
12030     IF PG_DEBUG = 'Y' THEN
12031        log_message('log_message: ' || 'In insert_bc_packets - Value of l_comm_fc_req '||l_comm_fc_req);
12032        log_message('log_message: ' || 'In insert_bc_packets - Value of l_adj_act_fc_req   '||l_adj_act_fc_req  );
12033        log_message('log_message: ' || 'In insert_bc_packets - Value of l_act_fc_req  '||l_act_fc_req );
12034     END IF;
12035 
12036     -- Cursor to fetch data from pa_transaction_interface_table.This data is later used
12037     -- in autonomous fundscheck insert_ap_bc_pkts_autonomous procedure.
12038 
12039     IF ( l_adj_act_fc_req = 'Y'  OR l_act_fc_req  = 'Y' ) THEN
12040 
12041        -- call to clean up global plsql arrays used for storing interface data.
12042        init_xface_plsql_tables;
12043 
12044        OPEN c_pa_txn_interface_data(l_adj_act_fc_req ,l_act_fc_req);
12045        FETCH c_pa_txn_interface_data BULK COLLECT INTO
12046                                           g_xface_project_id_tbl,
12047 					  g_xface_task_id_tbl,
12048 					  g_xface_exp_type_tbl,
12049 					  g_xface_ei_date_tbl,
12050 					  g_xface_exp_org_id_tbl,
12051 					  g_xface_bud_ver_id_tbl,
12052 					  g_xface_Entered_Dr_tbl,
12053 					  g_xface_Entered_Cr_tbl,
12054 					  g_xface_acct_Dr_tbl,
12055 				          g_xface_acct_Cr_tbl,
12056 					  g_xface_Txn_Ccid_tbl,
12057 					  g_xface_org_id_tbl,
12058 					  g_xface_Txn_interface_tbl;
12059        CLOSE c_pa_txn_interface_data;
12060 
12061        IF PG_DEBUG = 'Y' THEN
12062           log_message('log_message: ' || 'In insert_bc_packets - # of recs fetched from interface table c_pa_txn_interface_data = '||g_xface_Txn_interface_tbl.count);
12063        END IF;
12064 
12065     END IF; --IF ( l_adj_act_fc_req = 'Y'  OR l_act_fc_req  = 'Y' ) THEN
12066 
12067     IF PG_DEBUG = 'Y' THEN
12068        log_message('log_message: ' || 'In insert_bc_packets - calling insert_ap_bc_pkts_autonomous');
12069     END IF;
12070 
12071     insert_ap_bc_pkt_autonomous
12072                            (p_packet_id             => p_packet_id,
12073                             p_sys_ref2              => p_sys_ref2,
12074 			    p_sys_ref4              => p_sys_ref4,
12075                             p_sys_ref5              => p_sys_ref5,
12076                             p_acct_raw_cost         => p_acct_raw_cost,
12077                             p_acct_bur_cost         => p_acct_bur_cost,
12078                             p_fc_document_type      => p_fc_document_type,
12079                             p_txn_source            => p_txn_source,
12080                             p_adj_exp_item_id       => p_adj_exp_item_id ,
12081 			    p_txn_interface_id      => p_txn_interface_id,
12082 			    p_comm_fc_req           => l_comm_fc_req,
12083                             p_act_fc_req            => l_act_fc_req,
12084                             p_adj_act_fc_req        => l_adj_act_fc_req );
12085 
12086     IF PG_DEBUG = 'Y' THEN
12087        log_message('log_message: ' || 'In insert_bc_packets - End');
12088     END IF;
12089 
12090 EXCEPTION
12091 WHEN OTHERS THEN
12092    IF PG_DEBUG = 'Y' THEN
12093       log_message('log_message:I n insert_bc_packets exception' || sqlerrm||' Returning from insert_bc_packets');
12094    END IF;
12095    RAISE;
12096 END insert_ap_bc_packets;
12097 
12098 -- This procedure inserts records into pa_bc_packets for relieving commitment raw and burden
12099 -- and also for reserving raw and burden against actual.
12100 -- 1. IF p_fc_document_type = 'CMT'/'ALL' then we need to relieve commitment amount.
12101 -- 2. If commitment exists in pa_bc_commitments (i.e. sweeper process if run) then
12102 --      insert records into pa_bc_packets to relieve the raw and burden commitment
12103 --      amounts lying in pa_bc_commitments.
12104 --    else
12105 --      insert records into pa_bc_packets to relieve the raw and burden commitment
12106 --      amounts lying in pa_bc_packets.
12107 --    end if;
12108 -- 3.p_document type = 'ALL' and p_adj_exp_item_id IS NULL implies its a regular
12109 --   Payable Invoice getting interfaced to Projects. In this case
12110 --      insert records into pa_bc_packets to reserve actual raw cost amount which is equal to
12111 --      commitment amount in pa_bc_commitments relieved in step 2.
12112 --      Note : parent_bc_packet_id is populated to -1 such that pa_funds_check process will
12113 --             generate burden against this actual based on latest compiled set id.
12114 -- 4.If p_document type = 'ACT'/'ALL' and p_adj_exp_item_id IS NOT NULL then it implies thats its a
12115 --   reversing/cancelled Payable Invoice getting interfaced to Projects and original
12116 --   interfaced exp in project has been adjusted .
12117 --   In this case insert records into pa_bc_packets to fundscheck actual raw cost which is equal to
12118 --      amount in pa_transaction_interface for each non net zero expenditures associated with this
12119 --      invoice.
12120 --      Note : In this scenraio if non net zero expenditures associated with this invoice are not costed
12121 --             then import process (PAAPIMPB.pls) will reject the transactions with PA_EI_NOT_COST_DISTRIBUTED.
12122 --             Hence the ind_compiled_set_id on original exp's will always be the latest ,so parent_bc_packet_id
12123 --             is populated to -1 such that pa_funds_check process will generate burden against this
12124 --             actual based on latest compiled set id.
12125 
12126 PROCEDURE INSERT_AP_BC_PKT_AUTONOMOUS
12127                            (p_packet_id             IN NUMBER,
12128                             p_sys_ref2              IN NUMBER,
12129 			    p_sys_ref4              IN VARCHAR2,
12130                             p_sys_ref5              IN NUMBER,
12131                             p_acct_raw_cost         IN NUMBER,
12132                             p_acct_bur_cost         IN NUMBER,
12133                             p_fc_document_type      IN VARCHAR2,
12134                             p_txn_source            IN VARCHAR2,
12135                             p_adj_exp_item_id       IN NUMBER,
12136 			    p_txn_interface_id      IN NUMBER,
12137 			    p_comm_fc_req           IN VARCHAR2,
12138                             p_act_fc_req            IN VARCHAR2,
12139                             p_adj_act_fc_req        IN VARCHAR2) IS
12140 
12141 PRAGMA AUTONOMOUS_TRANSACTION;
12142 
12143 CURSOR c_gen_raw_bc_pkt IS
12144 SELECT Pa_Bc_Packets_s.NextVal
12145   FROM dual;
12146 
12147 CURSOR c_get_po_dist_id IS
12148 SELECT po_distribution_id
12149   FROM ap_invoice_distributions
12150  WHERE invoice_id = p_sys_ref2
12151    AND invoice_distribution_id = p_sys_ref5;
12152 
12153 CURSOR c_get_po_LINE_id (p_po_dist_id NUMBER) IS
12154 SELECT po_line_id
12155   FROM po_distributions_all
12156  WHERE po_distribution_id = p_po_dist_id;
12157 
12158 -- R12 Funds Management : This cursor can fetches both Raw and Burden Lines for commitment
12159 -- to be relieved.
12160 -- Note : This cursor can fetch more than one raw record for Quantity/amount variance scenarios.
12161 -- In this case QTY/Amount variance are stroed on same ITEM distributions , but there will be 2
12162 -- bc records one for item amout and other for quantity/amount variance
12163 
12164 Cursor C_Ap_Commitment Is
12165 Select bcc.Set_Of_Books_Id
12166        ,bcc.Period_Year
12167        ,bcc.Project_Id
12168        ,bcc.Task_Id
12169        ,bcc.Expenditure_Type
12170        ,bcc.Expenditure_Item_Date
12171        ,bcc.Expenditure_Organization_Id
12172        ,bcc.Budget_Version_Id
12173        ,bcc.budget_line_id  -- R12 Funds Management uptake
12174        ,bcc.Document_Type
12175        ,bcc.Document_Header_Id
12176        ,bcc.Document_Distribution_Id
12177        ,bcc.Entered_Cr
12178        ,bcc.Entered_Dr
12179        ,bcc.Accounted_Cr
12180        ,bcc.Accounted_Dr
12181        ,bcc.Actual_Flag
12182        ,bcc.Txn_Ccid
12183        ,bcc.Je_Category_Name
12184        ,bcc.Je_Source_Name
12185        ,bcc.Org_Id
12186        ,bcc.Pa_Date
12187        ,bcc.bc_packet_id
12188        ,bcc.packet_id
12189        ,bcc.parent_bc_packet_id
12190        ,bcc.encumbrance_type_id
12191        ,bcc.document_header_id_2
12192        ,bcc.document_distribution_type
12193  From  Pa_Bc_Commitments_all bcc
12194 Where  bcc.Document_Header_Id = p_sys_ref2
12195   and  bcc.Document_Distribution_Id =p_sys_ref5
12196   --PA-J Receipt accrual changes
12197   and  bcc.document_type = 'AP'
12198   and bcc.exp_item_id IS NULL -- Bug 5014138 : to pick just the encumbrance record
12199   and bcc.actual_flag = 'E' -- Bug 5014138 : to pick just the encumbrance record
12200   -- Bug 5550268 : For variance we should be picking just the variance records
12201   /* Bug 5560524 : Modified the following condition such that the burden commitment relieving record is also
12202                    fetched for 'AP VARIANCE' transaction source. */
12203   and ((nvl(bcc.accounted_dr,0)-nvl(bcc.accounted_cr,0) = p_acct_raw_cost
12204         and p_txn_source = 'AP VARIANCE' and bcc.parent_bc_packet_id is null)
12205         OR
12206         p_txn_source <> 'AP VARIANCE'
12207 	OR
12208         (bcc.parent_bc_packet_id is NOT null AND  p_txn_source = 'AP VARIANCE')
12209 	);
12210 
12211 
12212 
12213 -- R12 Funds Management : This cursor can fetches both Raw and Burden Lines for commitment
12214 -- to be relieved.
12215 -- Note : This cursor can fetch more than one raw record for Quantity/amount variance scenarios.
12216 -- In this case QTY/Amount variance are stroed on same ITEM distributions , but there will be 2
12217 -- bc records one for item amout and other for quantity/amount variance
12218 
12219 Cursor C_Ap_Bc_Packets Is
12220 Select  bcc.Set_Of_Books_Id
12221        ,bcc.Period_Year
12222        ,bcc.Project_Id
12223        ,bcc.Task_Id
12224        ,bcc.Expenditure_Type
12225        ,bcc.Expenditure_Item_Date
12226        ,bcc.Expenditure_Organization_Id
12227        ,bcc.Budget_Version_Id
12228        ,bcc.budget_line_id  -- R12 Funds Management uptake
12229        ,bcc.Document_Type
12230        ,bcc.Document_Header_Id
12231        ,bcc.Document_Distribution_Id
12232        ,bcc.Entered_Cr
12233        ,bcc.Entered_Dr
12234        ,bcc.Accounted_Cr
12235        ,bcc.Accounted_Dr
12236        ,bcc.Actual_Flag
12237        ,bcc.Txn_Ccid
12238        ,bcc.Je_Category_Name
12239        ,bcc.Je_Source_Name
12240        ,bcc.Org_Id
12241        ,bcc.Pa_Date
12242        ,bcc.bc_packet_id
12243        ,bcc.packet_id
12244        ,bcc.parent_bc_packet_id
12245        ,bcc.encumbrance_type_id
12246        ,bcc.document_header_id_2
12247        ,bcc.document_distribution_type
12248  From  Pa_Bc_Packets bcc
12249 where  bcc.Document_Header_Id = p_sys_ref2
12250   and  bcc.Document_Distribution_Id =p_sys_ref5
12251   and  bcc.Status_Code in ('A','C')
12252   and  bcc.document_type = 'AP'
12253   and bcc.exp_item_id IS NULL -- Bug 5014138 : to pick just the encumbrance record
12254   and bcc.actual_flag = 'E'   -- Bug 5014138 : to pick just the encumbrance record
12255     -- Bug 5550268 : For variance we should be picking just the variance records
12256   /* Bug 5560524 : Modified the following condition such that the burden commitment relieving record is also
12257                    fetched for 'AP VARIANCE' transaction source. */
12258   and ((nvl(bcc.accounted_dr,0)-nvl(bcc.accounted_cr,0) = p_acct_raw_cost
12259         and p_txn_source = 'AP VARIANCE' and bcc.parent_bc_packet_id is null)
12260         OR
12261         p_txn_source <> 'AP VARIANCE'
12262 	OR
12263         (bcc.parent_bc_packet_id is NOT null AND  p_txn_source = 'AP VARIANCE')
12264 	);
12265 
12266 l_rec_bc_comm_exists      VARCHAR2(1);  -- Variable to identify whether commitment record exists in pa_bc_commitments
12267 l_rec_pkt_comm_exists     VARCHAR2(1);  -- Variable to identify whether commitment record exists in pa_bc_packets
12268 L_RAW_BC_PACKET_ID        NUMBER;
12269 l_po_dist_id              ap_invoice_distributions.po_distribution_id%TYPE;
12270 l_po_line_id              po_distributions_all.po_line_id%TYPE;
12271 
12272 
12273 CURSOR c_curr_raw_pkt IS
12274 SELECT bc_packet_id
12275   FROM pa_bc_packets
12276  WHERE packet_id = p_packet_id
12277    AND Document_Header_Id = p_sys_ref2
12278    AND Document_Distribution_Id =p_sys_ref5
12279    AND parent_bc_packet_id IS NULL
12280    AND status_code ='P';
12281 
12282 
12283 BEGIN
12284 
12285     IF PG_DEBUG = 'Y' THEN
12286        log_message('log_message: ' || 'In insert_ap_bc_pkt_autonomous - Start');
12287     END IF;
12288 
12289     /* cwk */
12290 
12291     IF p_txn_source in ('AP NRTAX','AP VARIANCE') THEN
12292 
12293           OPEN c_get_po_dist_id;
12294           FETCH c_get_po_dist_id into l_po_dist_id;
12295           CLOSE c_get_po_dist_id;
12296 
12297           IF nvl(Pa_Pjc_Cwk_Utils.Is_rate_based_line( null, l_po_dist_id),'N') = 'Y' THEN
12298             OPEN c_get_po_LINE_id(l_po_dist_id);
12299             FETCH c_get_po_LINE_id into l_po_line_id;
12300             CLOSE c_get_po_LINE_id;
12301           END IF;
12302 
12303          IF PG_DEBUG = 'Y' THEN
12304             log_message('log_message: ' || 'In insert_ap_bc_pkt_autonomous - After fetching CWK related data l_po_dist_id'||l_po_dist_id);
12305             log_message('log_message: ' || 'In insert_ap_bc_pkt_autonomous - After fetching CWK related data l_po_line_id'||l_po_line_id);
12306          END IF;
12307 
12308     END IF;
12309 
12310     -- Code to derive values for variables which will decide the flow of this procedure
12311     l_rec_bc_comm_exists:='N';  -- Variable to identify whether commitment record exists in pa_bc_commitments
12312     l_rec_pkt_comm_exists:='N'; -- Variable to identify whether commitment record exists in pa_bc_packets
12313 
12314     IF p_comm_fc_req ='Y' OR p_act_fc_req  = 'Y' THEN
12315 
12316       IF PG_DEBUG = 'Y' THEN
12317          log_message('log_message: ' || 'In insert_ap_bc_pkt_autonomous - Fetchign data from bc commitments');
12318       END IF;
12319 
12320       clear_plsql_tables;
12321 
12322       Open C_Ap_Commitment ;
12323       FETCH C_Ap_Commitment BULK COLLECT INTO
12324                 g_sob_Id_tbl,
12325                 g_Period_Year_tbl,
12326                 g_project_id_tbl,
12327                 g_task_id_tbl,
12328                 g_exp_type_tbl,
12329                 g_ei_date_tbl,
12330                 g_exp_org_id_tbl,
12331                 g_bud_ver_id_tbl,
12332 		g_budget_line_id_tbl, -- R12 Funds management uptake
12333                 g_Document_Type_tbl,
12334                 g_Doc_Header_Id_tbl,
12335                 g_Doc_dist_Id_tbl,
12336                 g_Entered_Cr_tbl,
12337                 g_Entered_Dr_tbl,
12338                 g_acct_Cr_tbl,
12339                 g_acct_Dr_tbl,
12340                 g_Actual_Flag_tbl,
12341                 g_Txn_Ccid_tbl,
12342                 g_Je_Catg_Name_tbl,
12343                 g_Je_sorce_Name_tbl,
12344                 g_org_id_tbl,
12345                 g_Pa_Date_tbl,
12346                 g_bc_packet_id_tbl,
12347                 g_packet_id_tbl,
12348 		g_bc_parent_pkt_id_tbl,
12349                 g_enc_type_id_tbl,
12350                 g_doc_hdr_id_2_tbl,
12351                 g_doc_dist_type_tbl;
12352 
12353 
12354      IF g_bc_packet_id_tbl.COUNT <> 0 THEN
12355         l_rec_bc_comm_exists  := 'Y';
12356      END IF;
12357      CLOSE C_Ap_Commitment;
12358 
12359      IF l_rec_bc_comm_exists = 'N' THEN
12360 
12361         IF PG_DEBUG = 'Y' THEN
12362            log_message('log_message: ' || 'In insert_ap_bc_pkt_autonomous - Fetchign data from bc packets');
12363         END IF;
12364 
12365         OPEN C_Ap_Bc_Packets;
12366         FETCH C_Ap_Bc_Packets BULK COLLECT INTO
12367                 g_sob_Id_tbl,
12368                 g_Period_Year_tbl,
12369                 g_project_id_tbl,
12370                 g_task_id_tbl,
12371                 g_exp_type_tbl,
12372                 g_ei_date_tbl,
12373                 g_exp_org_id_tbl,
12374                 g_bud_ver_id_tbl,
12375 		g_budget_line_id_tbl, -- R12 Funds management uptake
12376                 g_Document_Type_tbl,
12377                 g_Doc_Header_Id_tbl,
12378                 g_Doc_dist_Id_tbl,
12379                 g_Entered_Cr_tbl,
12380                 g_Entered_Dr_tbl,
12381                 g_acct_Cr_tbl,
12382                 g_acct_Dr_tbl,
12383                 g_Actual_Flag_tbl,
12384                 g_Txn_Ccid_tbl,
12385                 g_Je_Catg_Name_tbl,
12386                 g_Je_sorce_Name_tbl,
12387                 g_org_id_tbl,
12388                 g_Pa_Date_tbl,
12389                 g_bc_packet_id_tbl,
12390                 g_packet_id_tbl,
12391 		g_bc_parent_pkt_id_tbl,
12392                 g_enc_type_id_tbl,
12393                 g_doc_hdr_id_2_tbl,
12394                 g_doc_dist_type_tbl;
12395 
12396         IF g_bc_packet_id_tbl.COUNT <> 0 THEN
12397            l_rec_pkt_comm_exists := 'Y' ;
12398         END IF;
12399         CLOSE C_Ap_Bc_Packets;
12400 
12401       END IF;
12402 
12403       IF PG_DEBUG = 'Y' THEN
12404             log_message('log_message: ' || 'In insert_ap_bc_pkt_autonomous - Number of commitment records fetched = '||g_bc_packet_id_tbl.count);
12405       END IF;
12406 
12407      IF (l_rec_bc_comm_exists ='Y' OR l_rec_pkt_comm_exists  = 'Y') THEN
12408 
12409      -- Bug 5510447 : Insert commitment relieving raw and burden records only if p_comm_fc_req = 'Y'
12410      IF p_comm_fc_req = 'Y' THEN
12411 
12412       -- Logic to insert commitment relieving raw and burden records
12413       FORALL i IN 1 .. g_bc_packet_id_tbl.count
12414           Insert Into Pa_Bc_Packets
12415                 (Packet_Id
12416                 ,Bc_Packet_Id
12417                 ,Parent_Bc_Packet_Id
12418                 ,Set_Of_Books_Id
12419                 ,Gl_Date
12420                 ,Period_Name
12421                 ,Period_Year
12422                 ,Pa_Date
12423                 ,Project_Id
12424                 ,Task_Id
12425                 ,Expenditure_Type
12426                 ,Expenditure_Item_Date
12427                 ,Expenditure_Organization_Id
12428                 ,Budget_Version_Id
12429 		,budget_line_id -- R12 Funds management uptake
12430                 ,Document_Type
12431                 ,Document_Header_Id
12432                 ,Document_Distribution_Id
12433                 ,Entered_Dr
12434                 ,Entered_Cr
12435                 ,Accounted_Dr
12436                 ,Accounted_Cr
12437                 ,Request_Id
12438                 ,Program_Id
12439                 ,Program_Application_Id
12440                 ,Program_Update_Date
12441                 ,Actual_Flag
12442                 ,Last_Update_Date
12443                 ,Last_Updated_By
12444                 ,Created_By
12445                 ,Creation_Date
12446                 ,Last_Update_Login
12447                 ,Txn_Ccid
12448                 ,Burden_Cost_Flag
12449                 ,Status_Code
12450                 ,Je_Category_Name
12451                 ,Je_Source_Name
12452                 ,Org_id
12453                 ,Exp_Item_Id
12454                 ,Document_Line_Id
12455 		,reference1
12456 		,reference2
12457 		,reference3
12458 		,txn_interface_id  --REL12
12459 		,encumbrance_type_id
12460 		,document_header_id_2
12461 		,document_distribution_type
12462                 )
12463          Select  p_packet_id
12464                 ,g_bc_packet_id_tbl(i) -- Later updated with newly generated Id's
12465                 ,g_bc_parent_pkt_id_tbl(i) -- Later updated with newly generated Id's
12466                 ,g_sob_Id_tbl(i)
12467                 ,G_FC_Gl_Date
12468                 ,G_FC_Period_Name
12469                 ,g_Period_Year_tbl(i)
12470                 ,g_Pa_Date_tbl(i)
12471                 ,g_project_id_tbl(i)
12472                 ,g_task_id_tbl(i)
12473                 ,g_exp_type_tbl(i)
12474                 ,g_ei_date_tbl(i)
12475                 ,g_exp_org_id_tbl(i)
12476                 ,g_bud_ver_id_tbl(i)
12477 		,g_budget_line_id_tbl(i)
12478                 ,g_Document_Type_tbl(i)
12479                 ,g_Doc_Header_Id_tbl(i)
12480                 ,g_Doc_dist_Id_tbl(i)
12481                 ,g_Entered_Cr_tbl(i) -- reversing dr
12482                 ,g_Entered_Dr_tbl(i) -- reversing cr
12483                 ,g_acct_Cr_tbl(i)
12484                 ,g_acct_Dr_tbl(i)
12485                 ,g_Request_Id
12486                 ,G_Program_Id
12487                 ,G_PROG_APPL_ID
12488                 ,sysdate
12489                 ,g_Actual_Flag_tbl(i)
12490                 ,sysdate
12491                 ,G_CONC_LOGIN_ID
12492                 ,G_CONC_LOGIN_ID
12493                 ,sysdate
12494                 ,G_CONC_LOGIN_ID
12495                 ,g_Txn_Ccid_tbl(i)
12496                 ,'R'
12497                 ,'P'
12498                 ,g_Je_Catg_Name_tbl(i)
12499                 ,g_Je_sorce_Name_tbl(i)
12500                 ,g_org_id_tbl(i)
12501                 ,X_ei_id
12502                 ,l_po_line_id
12503 		,'EXP'
12504 		,X_ei_id
12505 		,1
12506 		,p_txn_interface_id --REL12
12507 		,g_enc_type_id_tbl(i)
12508 		,g_doc_hdr_id_2_tbl(i)
12509 		,g_doc_dist_type_tbl(i)
12510           From  dual;
12511 
12512         IF PG_DEBUG = 'Y' THEN
12513          log_message('log_message: ' || 'In insert_ap_bc_pkt_autonomous - Inserted '||SQL%ROWCOUNT||' AP reversing raw and burden line(s) into bc packets');
12514         END IF;
12515 
12516         -- Logic to update bc_packet_id and parent_bc_packet_id with newly generated sequences
12517         FOR c_cur IN c_curr_raw_pkt LOOP
12518 
12519            IF PG_DEBUG = 'Y' THEN
12520               log_message('log_message: ' || 'In insert_ap_bc_pkt_autonomous -Logic to update correct bc pkt Id/parent BC pkt Id');
12521            END IF;
12522 
12523            OPEN  c_gen_raw_bc_pkt;
12524            FETCH c_gen_raw_bc_pkt INTO l_Raw_Bc_Packet_Id;
12525            CLOSE c_gen_raw_bc_pkt;
12526 
12527            IF PG_DEBUG = 'Y' THEN
12528               log_message('log_message: ' || 'In insert_ap_bc_pkt_autonomous -Old Bc Packet Id = ' || c_cur.bc_packet_id||'New Bc Packet Id = ' || l_Raw_Bc_Packet_Id ||'Packet Id = ' || p_packet_id);
12529            END IF;
12530 
12531 	   -- Update raw records with new bc_packet_id
12532             UPDATE Pa_Bc_Packets
12533 	       SET bc_packet_id = l_Raw_Bc_Packet_Id
12534              WHERE packet_id = p_packet_id
12535                AND bc_packet_id = c_cur.bc_packet_id;
12536 
12537            IF PG_DEBUG = 'Y' THEN
12538               log_message('log_message: ' || 'In insert_ap_bc_pkt_autonomous - NUmber of raw packets updated with new bc_packet_id '||SQL%ROWCOUNT);
12539            END IF;
12540 
12541             -- Update burden records with new bc_packet_id abd parent_bc_packet_id
12542             UPDATE Pa_Bc_Packets
12543 	       SET bc_packet_id = Pa_Bc_Packets_s.NextVal
12544 	           ,parent_bc_packet_id = l_Raw_Bc_Packet_Id
12545              WHERE packet_id = p_packet_id
12546                AND parent_bc_packet_id = c_cur.bc_packet_id;
12547 
12548            IF PG_DEBUG = 'Y' THEN
12549               log_message('log_message: ' || 'In insert_ap_bc_pkt_autonomous - NUmber of burden packets updated with new parent_bc_packet_id '||SQL%ROWCOUNT);
12550            END IF;
12551 
12552        END LOOP;
12553 
12554      END IF; -- Bug 5510447 : IF p_comm_fc_req = 'Y' THEN
12555 
12556        --Logic to Insert the positive records for the raw line with document type 'EXP'.
12557        IF p_act_fc_req = 'Y' THEN
12558 
12559          IF PG_DEBUG = 'Y' THEN
12560             log_message('log_message: ' || 'In insert_ap_bc_pkt_autonomous - Before inserting ACTUAL raw line from bc packets');
12561          END IF;
12562 
12563          FOR i IN 1 .. g_bc_packet_id_tbl.count LOOP
12564 
12565 
12566           Insert Into Pa_Bc_Packets
12567                 (Packet_Id
12568                 ,Bc_Packet_Id
12569                 ,Parent_Bc_Packet_Id
12570                 ,Set_Of_Books_Id
12571                 ,gl_date
12572                 ,Period_Name
12573                 ,Period_Year
12574                 ,Pa_Date
12575                 ,Project_Id
12576                 ,Task_Id
12577                 ,Expenditure_Type
12578                 ,Expenditure_Item_Date
12579                 ,Expenditure_Organization_Id
12580                 ,Budget_Version_Id
12581 		,budget_line_id -- R12 Funds management uptake
12582                 ,Document_Type
12583                 ,Document_Header_Id
12584                 ,Document_Distribution_Id
12585                 ,Entered_Dr
12586                 ,Entered_Cr
12587                 ,Accounted_Dr
12588                 ,Accounted_Cr
12589                 ,Request_Id
12590                 ,Program_Id
12591                 ,Program_Application_Id
12592                 ,Program_Update_Date
12593                 ,Actual_Flag
12594                 ,Last_Update_Date
12595                 ,Last_Updated_By
12596                 ,Created_By
12597                 ,Creation_Date
12598                 ,Last_Update_Login
12599                 ,Txn_Ccid
12600                 ,Burden_Cost_Flag
12601                 ,Status_Code
12602                 ,Je_Category_Name
12603                 ,Je_Source_Name
12604                 ,Org_id
12605                 ,Document_Line_Id
12606 		,reference1
12607 		,reference2
12608 		,reference3
12609 		,txn_interface_id  --REL12
12610                 )
12611           Select p_Packet_Id
12612                 ,Pa_Bc_Packets_s.NextVal
12613                 ,-1
12614                 ,g_sob_Id_tbl(i)
12615                 ,G_FC_Gl_Date
12616                 ,G_FC_Period_Name
12617                 ,g_Period_Year_tbl(i)
12618                 ,g_Pa_Date_tbl(i)
12619                 ,g_project_id_tbl(i)
12620                 ,g_task_id_tbl(i)
12621                 ,g_exp_type_tbl(i)
12622                 ,g_ei_date_tbl(i)
12623                 ,g_exp_org_id_tbl(i)
12624                 ,g_bud_ver_id_tbl(i)
12625 		,g_budget_line_id_tbl(i) -- R12 Funds management uptake
12626                 ,'EXP'
12627                 ,X_ei_id
12628                 ,1
12629                 ,DECODE(p_txn_source,'AP DISCOUNTS',p_acct_raw_cost,g_Entered_Dr_tbl(i))
12630                 ,DECODE(p_txn_source,'AP DISCOUNTS',0,g_Entered_Cr_tbl(i))
12631                 ,DECODE(p_txn_source,'AP DISCOUNTS',p_acct_raw_cost,g_acct_Dr_tbl(i))
12632                 ,DECODE(p_txn_source,'AP DISCOUNTS',0,g_acct_Cr_tbl(i))
12633                 ,g_Request_Id
12634                 ,g_program_id
12635                 ,g_prog_appl_id
12636                 ,sysdate
12637                 ,'A'
12638                 ,sysdate
12639                 ,g_conc_login_id
12640                 ,g_conc_login_id
12641                 ,sysdate
12642                 ,g_conc_login_id
12643                 ,g_Txn_Ccid_tbl(i)
12644                 ,'N'
12645                 ,'P'
12646                 ,'Expenditures'
12647                 ,'Project Accounting'
12648                 ,g_org_id_tbl(i)
12649                 ,DECODE(p_txn_source,'AP DISCOUNTS',NULL,l_po_line_id)
12650 		,DECODE(p_txn_source,'AP DISCOUNTS',NULL,'EXP')
12651 		,DECODE(p_txn_source,'AP DISCOUNTS',NULL,x_Ei_Id)
12652 		,DECODE(p_txn_source,'AP DISCOUNTS',NULL,1)
12653 		,p_txn_interface_id  --REL12
12654           From  Pa_Budget_Versions bv,
12655                 Pa_Budget_Types bt,
12656                 pa_budgetary_control_options pbct
12657          Where  bt.budget_type_Code = bv.budget_type_Code
12658            and  bt.budget_amount_code = 'C'
12659            and  bv.project_id = g_project_id_tbl(i)
12660            and  bv.current_flag = 'Y'
12661            and  pbct.project_id = bv.project_id
12662            and  pbct.BDGT_CNTRL_FLAG = 'Y'
12663            and  pbct.BUDGET_TYPE_CODE = bv.budget_type_code
12664            and (pbct.EXTERNAL_BUDGET_CODE = 'GL'
12665                 OR
12666                 pbct.EXTERNAL_BUDGET_CODE is NULL
12667                 )
12668 	   and  g_bc_parent_pkt_id_tbl(i) IS NULL;  -- Need to fire only for raw records
12669 
12670 
12671           -- Bug 5562671 :  Scenario where QTY/AMT varaince exists there will be multiple records in
12672 	  -- bc_commitments/bc_packets for same inv distribution, hence the global varaibles will have
12673 	  -- multiple distributions.But there will be only one ap discount line for both item amount +
12674 	  -- qty/amt varaince. Hence actuals need to reserved only once.
12675 	  /* Bug 5984525 : The following code is modified so that ACTUAL lines are inserted correctly
12676 	     into pa bc packets while interfacing discounts to Projects. */
12677 	  IF p_txn_source = 'AP DISCOUNTS' AND SQL%ROWCOUNT = 1 THEN
12678             IF PG_DEBUG = 'Y' THEN
12679                  log_message('log_message: ' || 'In insert_ap_bc_pkt_autonomous - Inserted 1 ACTUAL raw line(s) into bc packets');
12680             END IF;
12681 	    EXIT;
12682           END IF;
12683 
12684 	 END LOOP;
12685 
12686        END IF;--IF p_act_fc_req = 'Y' THEN
12687 
12688      END IF; --IF l_rec_bc_comm_exists ='Y' OR l_rec_pkt_comm_exists  = 'Y' THEN
12689 
12690      END IF; -- p_comm_fc_req ='Y' OR p_act_fc_req  = 'Y' THEN
12691 
12692 
12693      -- R12 AP lines uptake : Prepayment changes : Below insert will be fired for inserting 'EXP'
12694      -- records by fetching data from pa_transaction_interface_all table for below TXN records :
12695      -- a. Adjusting expenditures in txn_interface table
12696      -- b. For those commitments which were never FC'ed (eg : Prepayment application, pre-payment matched to PO ..)
12697 
12698      IF ((p_adj_act_fc_req = 'Y')
12699          OR (l_rec_bc_comm_exists ='N' AND l_rec_pkt_comm_exists  = 'N' AND p_act_fc_req  = 'Y' )) THEN
12700 
12701 	 IF PG_DEBUG = 'Y' THEN
12702             log_message('log_message: ' || 'In insert_ap_bc_pkt_autonomous - Before inserting adjusted ACTUAL raw line from transaction import table');
12703          END IF;
12704 
12705          FORALL i IN 1 .. g_xface_Txn_interface_tbl.count
12706          Insert Into Pa_Bc_Packets
12707                 (Packet_Id
12708                 ,Bc_Packet_Id
12709                 ,Parent_Bc_Packet_Id
12710                 ,Set_Of_Books_Id
12711                 ,Gl_Date
12712                 ,Period_Name
12713                 ,Period_Year
12714                 ,Pa_Date
12715                 ,Project_Id
12716                 ,Task_Id
12717                 ,Expenditure_Type
12718                 ,Expenditure_Item_Date
12719                 ,Expenditure_Organization_Id
12720                 ,Budget_Version_Id
12721                 ,Document_Type
12722                 ,Document_Header_Id
12723                 ,Document_Distribution_Id
12724                 ,Entered_Dr
12725                 ,Entered_Cr
12726                 ,Accounted_Dr
12727                 ,Accounted_Cr
12728                 ,Request_Id
12729                 ,Program_Id
12730                 ,Program_Application_Id
12731                 ,Program_Update_Date
12732                 ,Actual_Flag
12733                 ,Last_Update_Date
12734                 ,Last_Updated_By
12735                 ,Created_By
12736                 ,Creation_Date
12737                 ,Last_Update_Login
12738                 ,Txn_Ccid
12739                 ,Burden_Cost_Flag
12740                 ,Status_Code
12741                 ,Je_Category_Name
12742                 ,Je_Source_Name
12743                 ,Org_id
12744                 ,Document_Line_Id
12745 		,reference1
12746 		,reference2
12747 		,reference3
12748 		,txn_interface_id  --REL12
12749                 )
12750           Select p_Packet_Id
12751                 ,Pa_Bc_Packets_s.NextVal
12752                 ,-1
12753                 ,g_sobid
12754                 ,G_FC_Gl_Date
12755                 ,G_FC_Period_Name
12756                 ,G_FC_Period_Year
12757                 ,G_PaDate
12758                 ,g_xface_project_id_tbl(i)
12759                 ,g_xface_task_id_tbl(i)
12760                 ,g_xface_exp_type_tbl(i)
12761                 ,g_xface_ei_date_tbl(i)
12762                 ,g_xface_exp_org_id_tbl(i)
12763                 ,g_xface_bud_ver_id_tbl(i)
12764                 ,'EXP'
12765                 ,X_ei_id
12766                 ,1
12767 		-- R12 AP lines uptake : Prepayment changes
12768 		-- For Prepayment application/pre-payment matched to PO records consider p_acct_raw_cost as amount
12769                 ,g_xface_Entered_Dr_tbl(i)
12770                 ,g_xface_acct_Cr_tbl(i)
12771                 ,g_xface_acct_Dr_tbl(i)
12772                 ,g_xface_acct_Cr_tbl(i)
12773                 ,g_Request_Id
12774                 ,g_program_id
12775                 ,g_prog_appl_id
12776                 ,sysdate
12777                 ,'A' --Actual_Flag
12778                 ,sysdate
12779                 ,g_conc_login_id
12780                 ,g_conc_login_id
12781                 ,sysdate
12782                 ,g_conc_login_id
12783                 ,g_xface_Txn_Ccid_tbl(i)
12784                 ,'N'
12785                 ,'P'
12786                 ,'Expenditures'
12787                 ,'Project Accounting'
12788                 ,g_xface_org_id_tbl(i)
12789                 ,l_po_line_id
12790 		,'EXP'
12791 		,X_Ei_Id
12792 		,1
12793 		,g_xface_Txn_interface_tbl(i)  --REL12
12794           From  dual;
12795 
12796          IF PG_DEBUG = 'Y' THEN
12797             log_message('log_message: ' || 'In insert_ap_bc_pkt_autonomous - Inserted '||SQL%ROWCOUNT||' ACTUAL raw line(s) into bc packets');
12798          END IF;
12799 
12800        END IF;
12801 
12802  COMMIT;
12803  IF PG_DEBUG = 'Y' THEN
12804      log_message('log_message: ' || 'In insert_ap_bc_pkt_autonomous - End');
12805  END IF;
12806 
12807 EXCEPTION
12808 WHEN OTHERS THEN
12809    IF PG_DEBUG = 'Y' THEN
12810       log_message('log_message: In insert_ap_bc_pkt_autonomous - ' || sqlerrm||' Returning from insert_ap_bc_pkt_autonomous');
12811    END IF;
12812    RAISE;
12813 END insert_ap_bc_pkt_autonomous;
12814 
12815 
12816 -- R12 funds management Uptake : New procedure fired in non-autonomous mode to read
12817 -- pa_transaction_interface_all table and to derive variables which drives the
12818 -- relieving/reserving of commitments and actuals flow during interface.
12819 
12820 PROCEDURE insert_cash_ap_bc_packets(p_packet_id             IN NUMBER,
12821                                     p_sys_ref2              IN NUMBER,
12822                                     p_sys_ref5              IN NUMBER,
12823                                     p_acct_raw_cost         IN NUMBER,
12824                                     p_fc_document_type      IN VARCHAR2,
12825                                     p_txn_source            IN VARCHAR2,
12826                                     p_adj_exp_item_id       IN NUMBER,
12827 			            p_txn_interface_id      IN NUMBER,
12828 			            p_cash_pay_to_relieve   IN NUMBER DEFAULT 0) IS
12829 
12830 -- Cursor to fetch data from pa_transaction_interface_table.This data is later used
12831 -- in autonomous fundscheck insert_ap_bc_pkts_autonomous procedure.
12832 
12833  CURSOR c_pa_txn_interface_data (p_adj_act_fc_req VARCHAR2,
12834                                  p_act_fc_req  VARCHAR2) IS
12835  Select txn.Project_Id
12836         ,txn.Task_Id
12837         ,txn.Expenditure_Type
12838         ,txn.Expenditure_Item_Date
12839         ,nvl(txn.OVERRIDE_TO_ORGANIZATION_ID,txn.Org_Id)
12840         ,bv.Budget_Version_Id
12841 	-- R12 AP lines uptake : Prepayment changes
12842 	-- For Prepayment application/pre-payment matched to PO records consider p_acct_raw_cost as amount
12843         ,DECODE(txn_interface_id,p_txn_interface_id, p_acct_raw_cost,txn.acct_raw_cost)
12844         ,0
12845         ,DECODE(txn_interface_id,p_txn_interface_id, p_acct_raw_cost,txn.acct_raw_cost)
12846         ,0
12847         ,txn.cr_code_combination_id
12848         ,txn.Org_id
12849         ,txn.txn_interface_id  --REL12
12850   From  Pa_Budget_Versions bv,
12851         Pa_Budget_Types bt,
12852         pa_budgetary_control_options pbct,
12853 	pa_transaction_interface_all txn
12854  Where  bv.budget_type_code = bt.budget_type_code
12855    and  bt.budget_amount_code = 'C'
12856    and  bv.current_flag = 'Y'
12857    AND  pbct.project_id = bv.project_id
12858    AND  pbct.BDGT_CNTRL_FLAG = 'Y'
12859    AND  pbct.BUDGET_TYPE_CODE = bv.budget_type_code
12860    AND (pbct.EXTERNAL_BUDGET_CODE = 'GL'
12861         OR
12862         pbct.EXTERNAL_BUDGET_CODE is NULL
12863         )
12864    AND  bv.project_id = txn.project_id
12865    AND  txn.txn_interface_id in ( SELECT txn1.txn_interface_id
12866                                    FROM pa_transaction_interface_all txn1
12867  				   WHERE txn1.TRANSACTION_SOURCE = p_txn_source -- Condition for using index
12868 				   -- new index usage needs to be verified for below columns
12869                                      AND txn1.cdl_system_reference2 = p_sys_ref2
12870                                      AND txn1.cdl_system_reference5 = p_sys_ref5
12871                                      AND txn1.fc_document_type in ('ACT','ALL')
12872 	                             AND txn1.adjusted_expenditure_item_id IS NOT NULL
12873 				     AND p_adj_act_fc_req = 'Y'
12874                                 UNION ALL -- R12 AP lines uptake : Prepayment changes : Added to pick current transaction for prepayment scenarios
12875 				  SELECT p_txn_interface_id
12876 				    FROM DUAL
12877                                    WHERE p_act_fc_req  = 'Y');
12878 
12879 l_comm_fc_req             VARCHAR2(1);  -- Variable to identify whether commitment fundscheck is required
12880 l_act_fc_req              VARCHAR2(1);  -- Variable to identify whether actual fundscheck is required
12881 l_adj_act_fc_req          VARCHAR2(1);  -- Variable to identify whether adjusted actual fundscheck is required
12882 
12883 BEGIN
12884 
12885     IF PG_DEBUG = 'Y' THEN
12886        log_message('log_message: ' || 'In insert_cash_ap_bc_packets - Start');
12887        log_message('log_message: ' || 'In insert_cash_ap_bc_packets - Value of p_packet_id '||p_packet_id);
12888        log_message('log_message: ' || 'In insert_cash_ap_bc_packets - Value of p_sys_ref2  '||p_sys_ref2 );
12889        log_message('log_message: ' || 'In insert_cash_ap_bc_packets - Value of p_sys_ref5  '||p_sys_ref5 );
12890        log_message('log_message: ' || 'In insert_cash_ap_bc_packets - Value of p_acct_raw_cost '||p_acct_raw_cost);
12891        log_message('log_message: ' || 'In insert_cash_ap_bc_packets - Value of p_fc_document_type  '||p_fc_document_type );
12892        log_message('log_message: ' || 'In insert_cash_ap_bc_packets - Value of p_txn_source '||p_txn_source);
12893        log_message('log_message: ' || 'In insert_cash_ap_bc_packets - Value of p_adj_exp_item_id '||p_adj_exp_item_id);
12894        log_message('log_message: ' || 'In insert_cash_ap_bc_packets - Value of p_txn_interface_id '||p_txn_interface_id);
12895 
12896     END IF;
12897 
12898     -- Code to derive values for variables which will decide the flow of this procedure
12899 
12900     l_comm_fc_req :='N';        -- Variable to identify whether commitment fundscheck is required
12901     l_act_fc_req :='N';         -- Variable to identify whether actual fundscheck is required
12902     l_adj_act_fc_req:='N';      -- Variable to identify whether adjusted actual fundscheck is required
12903 
12904     -- Deriving value for variable which identifies if FC required for AP commitment
12905 
12906     IF ( (p_adj_exp_item_id IS NULL AND p_fc_document_type ='ALL') OR     --AP normal Invoice lines
12907          (p_adj_exp_item_id IS NOT NULL AND (p_fc_document_type ='ALL'    --FC enabled for AP INV and FC enabled for adjusted ei's
12908 	                                     OR p_fc_document_type ='CMT')--FC enabled for AP INV and FC Disabled for adjusted ei's
12909           )
12910 	) AND p_cash_pay_to_relieve <> 0 THEN
12911        l_comm_fc_req := 'Y' ;
12912     END IF;
12913 
12914     -- Deriving value for variable which identifies if FC required for Actuals
12915     IF  p_adj_exp_item_id IS NULL THEN
12916 
12917         IF ( p_fc_document_type ='ALL' OR p_fc_document_type ='ACT' OR p_txn_source = 'AP DISCOUNTS') THEN
12918             l_act_fc_req  := 'Y' ;
12919         END IF;
12920 
12921     ELSIF p_adj_exp_item_id IS NOT NULL THEN
12922 
12923         IF (p_fc_document_type ='ALL' OR  p_fc_document_type ='ACT') THEN
12924             l_adj_act_fc_req := 'Y';
12925         END IF;
12926 
12927     END IF;
12928 
12929     IF PG_DEBUG = 'Y' THEN
12930        log_message('log_message: ' || 'In insert_cash_ap_bc_packets - Value of l_comm_fc_req '||l_comm_fc_req);
12931        log_message('log_message: ' || 'In insert_cash_ap_bc_packets - Value of l_adj_act_fc_req   '||l_adj_act_fc_req  );
12932        log_message('log_message: ' || 'In insert_cash_ap_bc_packets - Value of l_act_fc_req  '||l_act_fc_req );
12933     END IF;
12934 
12935     -- Cursor to fetch data from pa_transaction_interface_table.This data is later used
12936     -- in autonomous fundscheck insert_ap_bc_pkts_autonomous procedure.
12937 
12938     IF ( l_adj_act_fc_req = 'Y'  OR l_act_fc_req  = 'Y' ) THEN
12939 
12940        -- call to clean up global plsql arrays used for storing interface data.
12941        init_xface_plsql_tables;
12942 
12943        OPEN c_pa_txn_interface_data(l_adj_act_fc_req ,l_act_fc_req);
12944        FETCH c_pa_txn_interface_data BULK COLLECT INTO
12945                                           g_xface_project_id_tbl,
12946 					  g_xface_task_id_tbl,
12947 					  g_xface_exp_type_tbl,
12948 					  g_xface_ei_date_tbl,
12949 					  g_xface_exp_org_id_tbl,
12950 					  g_xface_bud_ver_id_tbl,
12951 					  g_xface_Entered_Dr_tbl,
12952 					  g_xface_Entered_Cr_tbl,
12953 					  g_xface_acct_Dr_tbl,
12954 				          g_xface_acct_Cr_tbl,
12955 					  g_xface_Txn_Ccid_tbl,
12956 					  g_xface_org_id_tbl,
12957 					  g_xface_Txn_interface_tbl;
12958        CLOSE c_pa_txn_interface_data;
12959 
12960        IF PG_DEBUG = 'Y' THEN
12961           log_message('log_message: ' || 'In insert_cash_ap_bc_packets - fetched data from interface table c_pa_txn_interface_data ');
12962        END IF;
12963 
12964     END IF; --IF ( l_adj_act_fc_req = 'Y'  OR l_act_fc_req  = 'Y' ) THEN
12965 
12966     IF PG_DEBUG = 'Y' THEN
12967        log_message('log_message: ' || 'In insert_cash_ap_bc_packets - calling insert_cash_ap_bc_pkt_auto');
12968     END IF;
12969 
12970     insert_cash_ap_bc_pkt_auto
12971                            (p_packet_id             => p_packet_id,
12972                             p_sys_ref2              => p_sys_ref2,
12973                             p_sys_ref5              => p_sys_ref5,
12974                             p_acct_raw_cost         => p_acct_raw_cost,
12975                             p_fc_document_type      => p_fc_document_type,
12976                             p_txn_source            => p_txn_source,
12977                             p_adj_exp_item_id       => p_adj_exp_item_id ,
12978 			    p_txn_interface_id      => p_txn_interface_id,
12979 		            p_cash_pay_to_relieve   => p_cash_pay_to_relieve,
12980 			    p_comm_fc_req           => l_comm_fc_req,
12981                             p_act_fc_req            => l_act_fc_req,
12982                             p_adj_act_fc_req        => l_adj_act_fc_req );
12983 
12984     IF PG_DEBUG = 'Y' THEN
12985        log_message('log_message: ' || 'In insert_cash_ap_bc_packets - End');
12986     END IF;
12987 
12988 EXCEPTION
12989 WHEN OTHERS THEN
12990    IF PG_DEBUG = 'Y' THEN
12991       log_message('log_message:I n insert_cash_ap_bc_packets exception' || sqlerrm||' Returning from insert_cash_ap_bc_packets');
12992    END IF;
12993    RAISE;
12994 END insert_cash_ap_bc_packets;
12995 
12996 -- This procedure inserts records into pa_bc_packets for relieving commitment raw and burden
12997 -- and also for reserving raw and burden against actual.
12998 
12999 -- Parameter values : p_packet_id     -  Packet Id to be inserted
13000 --                    p_sys_ref2      -  Invoice Id
13001 --                    p_sys_ref5      -  Invoice Distribution Id
13002 --                    p_acct_raw_cost -  Amount to be relieved against AP and to be reserved against EXP
13003 --                    p_fc_document_type - 'CMT'/'ALL'/'ACT'/'NOT'
13004 --                    p_txn_source       - Transaction source associated with the txn.
13005 --                    p_adj_exp_item_id  - Exp item id to be adjusted
13006 --                    p_txn_interface_id - txn interface Id
13007 --                    p_cash_pay_to_relieve - AP distribution amount to be relieved for cash based accounting.
13008 --                                            For cash based accounting p_acct_raw_cost amount will be reserved against EXP and
13009 --                                            p_cash_amt_to_relieve amount will be relived against AP.
13010 
13011 PROCEDURE insert_cash_ap_bc_pkt_auto
13012                                    (p_packet_id             IN NUMBER,
13013                                     p_sys_ref2              IN NUMBER,
13014                                     p_sys_ref5              IN NUMBER,
13015                                     p_acct_raw_cost         IN NUMBER,
13016                                     p_fc_document_type      IN VARCHAR2,
13017                                     p_txn_source            IN VARCHAR2,
13018                                     p_adj_exp_item_id       IN NUMBER,
13019 			            p_txn_interface_id      IN NUMBER,
13020 			            p_cash_pay_to_relieve   IN NUMBER DEFAULT 0,
13021             			    p_comm_fc_req           IN VARCHAR2,
13022                                     p_act_fc_req            IN VARCHAR2,
13023                                     p_adj_act_fc_req        IN VARCHAR2) IS
13024 
13025 PRAGMA AUTONOMOUS_TRANSACTION;
13026 
13027 CURSOR c_gen_raw_bc_pkt IS
13028 SELECT Pa_Bc_Packets_s.NextVal
13029   FROM dual;
13030 
13031 CURSOR c_get_po_dist_id IS
13032 SELECT po_distribution_id
13033   FROM ap_invoice_distributions
13034  WHERE invoice_id = p_sys_ref2
13035    AND invoice_distribution_id = p_sys_ref5;
13036 
13037 CURSOR c_get_po_LINE_id (p_po_dist_id NUMBER) IS
13038 SELECT po_line_id
13039   FROM po_distributions_all
13040  WHERE po_distribution_id = p_po_dist_id;
13041 
13042 Cursor C_Ap_Commitment Is
13043 Select  bcc.Set_Of_Books_Id
13044        ,bcc.Period_Year
13045        ,bcc.Project_Id
13046        ,bcc.Task_Id
13047        ,bcc.Expenditure_Type
13048        ,bcc.Expenditure_Item_Date
13049        ,bcc.Expenditure_Organization_Id
13050        ,bcc.Budget_Version_Id
13051        ,bcc.budget_line_id -- R12 Funds management uptake
13052        ,bcc.Document_Type
13053        ,bcc.Document_Header_Id
13054        ,bcc.Document_Distribution_Id
13055        ,bcc.Entered_Cr
13056        ,bcc.Entered_Dr
13057        ,bcc.Accounted_Cr
13058        ,bcc.Accounted_Dr
13059        ,bcc.Actual_Flag
13060        ,bcc.Txn_Ccid
13061        ,bcc.Je_Category_Name
13062        ,bcc.Je_Source_Name
13063        ,bcc.Org_Id
13064        ,bcc.Pa_Date
13065        ,bcc.bc_packet_id
13066        ,bcc.packet_id
13067  From  Pa_Bc_Commitments_all bcc
13068 Where  bcc.Document_Header_Id = p_sys_ref2
13069   and  bcc.Document_Distribution_Id = p_sys_ref5
13070   and  bcc.Parent_Bc_Packet_Id is NULL
13071   --PA-J Receipt accrual changes
13072   and  bcc.document_type = 'AP'
13073   and bcc.exp_item_id IS NULL -- Bug 5014138 : to pick just the encumbrance record
13074   and bcc.actual_flag = 'E'   -- Bug 5014138 : to pick just the encumbrance record
13075   -- R12 Ap lines Uptake : For cash based accounting which can have multiple packets associated with a distribution.
13076   and  rownum = 1 ;
13077 
13078 
13079 Cursor C_Ap_Bc_Packets Is
13080 Select  bcc.Set_Of_Books_Id
13081        ,bcc.Period_Year
13082        ,bcc.Project_Id
13083        ,bcc.Task_Id
13084        ,bcc.Expenditure_Type
13085        ,bcc.Expenditure_Item_Date
13086        ,bcc.Expenditure_Organization_Id
13087        ,bcc.Budget_Version_Id
13088        ,bcc.budget_line_id -- R12 Funds management uptake
13089        ,bcc.Document_Type
13090        ,bcc.Document_Header_Id
13091        ,bcc.Document_Distribution_Id
13092        ,bcc.Entered_Cr
13093        ,bcc.Entered_Dr
13094        ,bcc.Accounted_Cr
13095        ,bcc.Accounted_Dr
13096        ,bcc.Actual_Flag
13097        ,bcc.Txn_Ccid
13098        ,bcc.Je_Category_Name
13099        ,bcc.Je_Source_Name
13100        ,bcc.Org_Id
13101        ,bcc.Pa_Date
13102        ,bcc.bc_packet_id
13103        ,bcc.packet_id
13104  From  Pa_Bc_Packets bcc
13105 where  bcc.Document_Header_Id = p_sys_ref2
13106   and  bcc.Document_Distribution_Id = p_sys_ref5
13107   and  bcc.Status_Code in ('A','C')
13108   and  bcc.Parent_Bc_Packet_Id is NULL
13109   and  bcc.document_type = 'AP'
13110   and bcc.exp_item_id IS NULL -- Bug 5014138 : to pick just the encumbrance record
13111   and bcc.actual_flag = 'E'   -- Bug 5014138 : to pick just the encumbrance record
13112   -- R12 Ap lines Uptake : Added for cash based accounting which can have multiple packets associated witha  distribution.
13113   and  rownum = 1 ;
13114 
13115 l_rec_bc_comm_exists      VARCHAR2(1);  -- Variable to identify whether commitment record exists in pa_bc_commitments
13116 l_rec_pkt_comm_exists     VARCHAR2(1);  -- Variable to identify whether commitment record exists in pa_bc_packets
13117 l_Raw_Bc_Packet_Id        pa_bc_packets.bc_packet_id%TYPE;
13118 l_po_dist_id              ap_invoice_distributions.po_distribution_id%TYPE;
13119 l_po_line_id              po_distributions_all.po_line_id%TYPE;
13120 
13121 BEGIN
13122 
13123     IF PG_DEBUG = 'Y' THEN
13124        log_message('log_message: ' || 'In insert_cash_ap_bc_pkt_auto - Start');
13125     END IF;
13126 
13127     /* cwk */
13128 
13129     IF p_txn_source in ('AP NRTAX','AP VARIANCE') THEN
13130 
13131           OPEN c_get_po_dist_id;
13132           FETCH c_get_po_dist_id into l_po_dist_id;
13133           CLOSE c_get_po_dist_id;
13134 
13135           IF nvl(Pa_Pjc_Cwk_Utils.Is_rate_based_line( null, l_po_dist_id),'N') = 'Y' THEN
13136             OPEN c_get_po_LINE_id(l_po_dist_id);
13137             FETCH c_get_po_LINE_id into l_po_line_id;
13138             CLOSE c_get_po_LINE_id;
13139           END IF;
13140 
13141          IF PG_DEBUG = 'Y' THEN
13142             log_message('log_message: ' || 'In insert_cash_ap_bc_pkt_auto - After fetching CWK related data l_po_dist_id'||l_po_dist_id);
13143             log_message('log_message: ' || 'In insert_cash_ap_bc_pkt_auto - After fetching CWK related data l_po_line_id'||l_po_line_id);
13144          END IF;
13145     END IF;
13146 
13147     -- Code to derive values for variables which will decide the flow of this procedure
13148 
13149     l_rec_bc_comm_exists:='N';  -- Variable to identify whether commitment record exists in pa_bc_commitments
13150     l_rec_pkt_comm_exists:='N'; -- Variable to identify whether commitment record exists in pa_bc_packets
13151 
13152     IF p_comm_fc_req ='Y' OR p_act_fc_req  = 'Y' THEN
13153 
13154       IF PG_DEBUG = 'Y' THEN
13155          log_message('log_message: ' || 'In insert_cash_ap_bc_pkt_auto - Fetchign data from bc commitments');
13156       END IF;
13157 
13158       Open C_Ap_Commitment ;
13159       clear_plsql_tables;
13160       FETCH C_Ap_Commitment BULK COLLECT INTO
13161                 g_sob_Id_tbl,
13162                 g_Period_Year_tbl,
13163                 g_project_id_tbl,
13164                 g_task_id_tbl,
13165                 g_exp_type_tbl,
13166                 g_ei_date_tbl,
13167                 g_exp_org_id_tbl,
13168                 g_bud_ver_id_tbl,
13169  	        g_budget_line_id_tbl, -- R12 funds management uptake
13170                 g_Document_Type_tbl,
13171                 g_Doc_Header_Id_tbl,
13172                 g_Doc_dist_Id_tbl,
13173                 g_Entered_Cr_tbl,
13174                 g_Entered_Dr_tbl,
13175                 g_acct_Cr_tbl,
13176                 g_acct_Dr_tbl,
13177                 g_Actual_Flag_tbl,
13178                 g_Txn_Ccid_tbl,
13179                 g_Je_Catg_Name_tbl,
13180                 g_Je_sorce_Name_tbl,
13181                 g_org_id_tbl,
13182                 g_Pa_Date_tbl,
13183                 g_bc_packet_id_tbl,
13184                 g_packet_id_tbl;
13185 
13186      IF g_bc_packet_id_tbl.COUNT <> 0 THEN
13187         l_rec_bc_comm_exists  := 'Y';
13188      END IF;
13189      CLOSE C_Ap_Commitment;
13190 
13191      IF l_rec_bc_comm_exists = 'N' THEN
13192 
13193         IF PG_DEBUG = 'Y' THEN
13194            log_message('log_message: ' || 'In insert_cash_ap_bc_pkt_auto - Fetchign data from bc packets');
13195         END IF;
13196 
13197         OPEN C_Ap_Bc_Packets;
13198         FETCH C_Ap_Bc_Packets BULK COLLECT INTO
13199                 g_sob_Id_tbl,
13200                 g_Period_Year_tbl,
13201                 g_project_id_tbl,
13202                 g_task_id_tbl,
13203                 g_exp_type_tbl,
13204                 g_ei_date_tbl,
13205                 g_exp_org_id_tbl,
13206                 g_bud_ver_id_tbl,
13207  	        g_budget_line_id_tbl, -- R12 funds management uptake
13208                 g_Document_Type_tbl,
13209                 g_Doc_Header_Id_tbl,
13210                 g_Doc_dist_Id_tbl,
13211                 g_Entered_Cr_tbl,
13212                 g_Entered_Dr_tbl,
13213                 g_acct_Cr_tbl,
13214                 g_acct_Dr_tbl,
13215                 g_Actual_Flag_tbl,
13216                 g_Txn_Ccid_tbl,
13217                 g_Je_Catg_Name_tbl,
13218                 g_Je_sorce_Name_tbl,
13219                 g_org_id_tbl,
13220                 g_Pa_Date_tbl,
13221                 g_bc_packet_id_tbl,
13222                 g_packet_id_tbl;
13223 
13224         IF g_bc_packet_id_tbl.COUNT <> 0 THEN
13225            l_rec_pkt_comm_exists := 'Y' ;
13226         END IF;
13227         CLOSE C_Ap_Bc_Packets;
13228 
13229       END IF;
13230 
13231      IF l_rec_bc_comm_exists ='Y' OR l_rec_pkt_comm_exists  = 'Y' THEN
13232 
13233       FOR i IN g_bc_packet_id_tbl.FIRST..g_bc_packet_id_tbl.LAST LOOP
13234 
13235         -- Generate a packet identifier for RAW record that
13236         -- needs to be inserted into Pa BC Packets
13237 
13238         IF PG_DEBUG = 'Y' THEN
13239            log_message('log_message: ' || 'In insert_cash_ap_bc_pkt_auto - Generating new Bc packet Id for reversing RAW act and commitment line');
13240         END IF;
13241 
13242         OPEN  c_gen_raw_bc_pkt;
13243         FETCH c_gen_raw_bc_pkt INTO l_Raw_Bc_Packet_Id;
13244         CLOSE c_gen_raw_bc_pkt;
13245 
13246         IF PG_DEBUG = 'Y' THEN
13247           log_message('log_message: ' || 'In insert_cash_ap_bc_pkt_auto - Bc Packet Id = ' || l_Raw_Bc_Packet_Id ||'Packet Id = ' || p_packet_id);
13248         END IF;
13249 
13250         IF l_rec_bc_comm_exists = 'Y' AND p_comm_fc_req ='Y' THEN
13251 
13252           IF PG_DEBUG = 'Y' THEN
13253            log_message('log_message: ' || 'In insert_cash_ap_bc_pkt_auto - Before inserting AP reversing raw and burden line from bc commitments');
13254           END IF;
13255 
13256           Insert Into Pa_Bc_Packets
13257                 (Packet_Id
13258                 ,Bc_Packet_Id
13259                 ,Parent_Bc_Packet_Id
13260                 ,Set_Of_Books_Id
13261                 ,Gl_Date
13262                 ,Period_Name
13263                 ,Period_Year
13264                 ,Pa_Date
13265                 ,Project_Id
13266                 ,Task_Id
13267                 ,Expenditure_Type
13268                 ,Expenditure_Item_Date
13269                 ,Expenditure_Organization_Id
13270                 ,Budget_Version_Id
13271  	        ,budget_line_id -- R12 funds management uptake
13272                 ,Document_Type
13273                 ,Document_Header_Id
13274                 ,Document_Distribution_Id
13275                 ,Entered_Dr
13276                 ,Entered_Cr
13277                 ,Accounted_Dr
13278                 ,Accounted_Cr
13279                 ,Request_Id
13280                 ,Program_Id
13281                 ,Program_Application_Id
13282                 ,Program_Update_Date
13283                 ,Actual_Flag
13284                 ,Last_Update_Date
13285                 ,Last_Updated_By
13286                 ,Created_By
13287                 ,Creation_Date
13288                 ,Last_Update_Login
13289                 ,Txn_Ccid
13290                 ,Burden_Cost_Flag
13291                 ,Status_Code
13292                 ,Je_Category_Name
13293                 ,Je_Source_Name
13294                 ,Org_id
13295                 ,Exp_Item_Id
13296                 ,Document_Line_Id
13297 		,reference1
13298 		,reference2
13299 		,reference3
13300 		,txn_interface_id  --REL12
13301 		,encumbrance_type_id
13302 		,document_header_id_2
13303 		,document_distribution_type
13304                 )
13305          Select  p_packet_id
13306                 ,decode(bcc.Parent_Bc_Packet_Id,NULL,l_Raw_Bc_Packet_id,Pa_Bc_Packets_s.NextVal)
13307                 ,decode(bcc.Parent_Bc_Packet_Id,NULL,NULL,l_Raw_Bc_Packet_id)
13308                 ,bcc.Set_Of_Books_Id
13309                 ,G_FC_Gl_Date
13310                 ,G_FC_Period_Name
13311                 ,bcc.Period_Year
13312                 ,bcc.Pa_Date -- pa_date on raw is used for burden line
13313                 ,bcc.Project_Id
13314                 ,bcc.Task_Id
13315                 ,bcc.Expenditure_Type
13316                 ,bcc.Expenditure_Item_Date
13317                 ,bcc.Expenditure_Organization_Id
13318                 ,bcc.Budget_Version_Id
13319  	        ,bcc.budget_line_id -- R12 funds management uptake
13320                 ,bcc.Document_Type
13321                 ,bcc.Document_Header_Id
13322                 ,bcc.Document_Distribution_Id
13323                 ,DECODE(bcc.Parent_Bc_Packet_Id,NULL,p_cash_pay_to_relieve
13324  	  				            ,pa_currency.round_trans_currency_amt1((p_cash_pay_to_relieve * bcc.compiled_multiplier),G_accounting_currency_code))
13325                 ,0
13326                 ,DECODE(bcc.Parent_Bc_Packet_Id,NULL,p_cash_pay_to_relieve
13327  	  				            ,pa_currency.round_trans_currency_amt1((p_cash_pay_to_relieve * bcc.compiled_multiplier),G_accounting_currency_code))
13328                 ,0
13329                 ,g_Request_Id
13330                 ,G_Program_Id
13331                 ,G_PROG_APPL_ID
13332                 ,sysdate
13333                 ,bcc.Actual_Flag
13334                 ,sysdate
13335                 ,G_CONC_LOGIN_ID
13336                 ,G_CONC_LOGIN_ID
13337                 ,sysdate
13338                 ,G_CONC_LOGIN_ID
13339                 ,bcc.Txn_Ccid
13340                 ,'R'
13341                 ,'P'
13342                 ,bcc.Je_Category_Name
13343                 ,bcc.Je_Source_Name
13344                 ,bcc.Org_Id
13345                 ,X_ei_id
13346                 ,l_po_line_id
13347 		,'EXP'
13348 		,X_ei_id
13349 		,1
13350 		,p_txn_interface_id --REL12
13351 		,bcc.encumbrance_type_id
13352 		,bcc.document_header_id_2
13353 		,bcc.document_distribution_type
13354           From  Pa_Bc_Commitments_all bcc
13355          Where  bcc.Document_Header_Id = p_sys_ref2 -- Added for using index
13356            and  bcc.Document_Distribution_Id = p_sys_ref5
13357            and  bcc.packet_id = g_packet_id_tbl(i)
13358 	   and  bcc.document_type = 'AP' ;
13359 
13360         IF PG_DEBUG = 'Y' THEN
13361          log_message('log_message: ' || 'In insert_cash_ap_bc_pkt_auto - Inserted '||SQL%ROWCOUNT||' AP reversing raw and burden line(s) into bc packets');
13362         END IF;
13363 
13364        END IF; --IF l_rec_bc_comm_exists = 'Y' AND p_comm_fc_req ='Y' THEN
13365 
13366 
13367        IF l_rec_pkt_comm_exists='Y' AND p_comm_fc_req ='Y' THEN
13368 
13369           IF PG_DEBUG = 'Y' THEN
13370             log_message('log_message: ' || 'In insert_cash_ap_bc_pkt_auto - Before inserting the AP raw and burden lines from Bc Packets');
13371           END IF;
13372 
13373           Insert Into Pa_Bc_Packets
13374                 (Packet_Id
13375                 ,Bc_Packet_Id
13376                 ,Parent_Bc_Packet_Id
13377                 ,Set_Of_Books_Id
13378                 ,Gl_Date
13379                 ,Period_Name
13380                 ,Period_Year
13381                 ,Pa_Date
13382                 ,Project_Id
13383                 ,Task_Id
13384                 ,Expenditure_Type
13385                 ,Expenditure_Item_Date
13386                 ,Expenditure_Organization_Id
13387                 ,Budget_Version_Id
13388  	        ,budget_line_id -- R12 funds management uptake
13389                 ,Document_Type
13390                 ,Document_Header_Id
13391                 ,Document_Distribution_Id
13392                 ,Entered_Dr
13393                 ,Entered_Cr
13394                 ,Accounted_Dr
13395                 ,Accounted_Cr
13396                 ,Request_Id
13397                 ,Program_Id
13398                 ,Program_Application_Id
13399                 ,Program_Update_Date
13400                 ,Actual_Flag
13401                 ,Last_Update_Date
13402                 ,Last_Updated_By
13403                 ,Created_By
13404                 ,Creation_Date
13405                 ,Last_Update_Login
13406                 ,Txn_Ccid
13407                 ,Burden_Cost_Flag
13408                 ,Status_Code
13409                 ,Je_Category_Name
13410                 ,Je_Source_Name
13411                 ,Org_id
13412                 ,Exp_Item_Id
13413                 ,Document_Line_Id
13414 		,reference1
13415 		,reference2
13416 		,reference3
13417 		,txn_interface_id  --REL12
13418 		,encumbrance_type_id
13419 		,document_header_id_2
13420 		,document_distribution_type
13421                 )
13422           Select p_packet_id
13423                 ,decode(bcc.Parent_Bc_Packet_Id,NULL,l_Raw_Bc_Packet_id,Pa_Bc_Packets_s.NextVal)
13424                 ,decode(bcc.Parent_Bc_Packet_Id,NULL,NULL,l_Raw_Bc_Packet_id)
13425                 ,bcc.Set_Of_Books_Id
13426                 ,G_FC_Gl_Date
13427                 ,G_FC_Period_Name
13428                 ,bcc.Period_Year
13429                 ,bcc.pa_date
13430                 ,bcc.Project_Id
13431                 ,bcc.Task_Id
13432                 ,bcc.Expenditure_Type
13433                 ,bcc.Expenditure_Item_Date
13434                 ,bcc.Expenditure_Organization_Id
13435                 ,bcc.Budget_Version_Id
13436  	        ,bcc.budget_line_id -- R12 funds management uptake
13437                 ,bcc.Document_Type
13438                 ,bcc.Document_Header_Id
13439                 ,bcc.Document_Distribution_Id
13440                 ,DECODE(bcc.Parent_Bc_Packet_Id,NULL,p_cash_pay_to_relieve
13441  	  				            ,pa_currency.round_trans_currency_amt1((p_cash_pay_to_relieve * bcc.compiled_multiplier),G_accounting_currency_code))
13442                 ,0
13443                 ,DECODE(bcc.Parent_Bc_Packet_Id,NULL,p_cash_pay_to_relieve
13444  	  				            ,pa_currency.round_trans_currency_amt1((p_cash_pay_to_relieve * bcc.compiled_multiplier),G_accounting_currency_code))
13445                 ,0
13446 		,g_Request_Id
13447                 ,G_Program_Id
13448                 ,G_PROG_APPL_ID
13449                 ,sysdate
13450                 ,bcc.Actual_Flag
13451                 ,sysdate
13452                 ,G_CONC_LOGIN_ID
13453                 ,G_CONC_LOGIN_ID
13454                 ,sysdate
13455                 ,G_CONC_LOGIN_ID
13456                 ,bcc.Txn_Ccid
13457                 ,'R'
13458                 ,'P'
13459                 ,bcc.Je_Category_Name
13460                 ,bcc.Je_Source_Name
13461                 ,bcc.Org_id
13462                 ,X_ei_id
13463                 ,l_po_line_id
13464 		,'EXP'
13465 		,X_ei_id
13466 		,1
13467 		,p_txn_interface_id  --REL12
13468 		,bcc.encumbrance_type_id
13469 		,bcc.document_header_id_2
13470 		,bcc.document_distribution_type
13471           From  Pa_Bc_Packets bcc
13472          Where  bcc.packet_id                = g_packet_id_tbl(i)
13473 	   and  bcc.document_type            = 'AP'
13474            -- Bug : 4962731
13475            --     : R12.PJ:XB1:QA:BC:INCORRECT AMOUNTS INTERFACED TO PROJECTS IN CASH BASED ACC
13476            and  bcc.Document_Header_Id       = p_sys_ref2 -- Added for using index
13477            and  bcc.Document_Distribution_Id = p_sys_ref5
13478 	   and  bcc.Status_Code in ('A','C');
13479 
13480           IF PG_DEBUG = 'Y' THEN
13481             log_message('log_message: ' || 'In insert_cash_ap_bc_pkt_auto - Before inserting the AP raw and burden lines from Bc Packets');
13482           END IF;
13483 
13484        END IF; --IF l_rec_pkt_comm_exists='Y' AND p_comm_fc_req ='Y' THEN
13485 
13486        IF p_act_fc_req = 'Y' THEN
13487 
13488          -- Insert the positive records for the raw line with
13489          -- document type 'EXP'.
13490 
13491          IF PG_DEBUG = 'Y' THEN
13492             log_message('log_message: ' || 'In insert_cash_ap_bc_pkt_auto - Before inserting ACTUAL raw line from bc packets');
13493          END IF;
13494 
13495           Insert Into Pa_Bc_Packets
13496                 (Packet_Id
13497                 ,Bc_Packet_Id
13498                 ,Parent_Bc_Packet_Id
13499                 ,Set_Of_Books_Id
13500                 ,gl_date
13501                 ,Period_Name
13502                 ,Period_Year
13503                 ,Pa_Date
13504                 ,Project_Id
13505                 ,Task_Id
13506                 ,Expenditure_Type
13507                 ,Expenditure_Item_Date
13508                 ,Expenditure_Organization_Id
13509                 ,Budget_Version_Id
13510  	        ,budget_line_id -- R12 funds management uptake
13511                 ,Document_Type
13512                 ,Document_Header_Id
13513                 ,Document_Distribution_Id
13514                 ,Entered_Dr
13515                 ,Entered_Cr
13516                 ,Accounted_Dr
13517                 ,Accounted_Cr
13518                 ,Request_Id
13519                 ,Program_Id
13520                 ,Program_Application_Id
13521                 ,Program_Update_Date
13522                 ,Actual_Flag
13523                 ,Last_Update_Date
13524                 ,Last_Updated_By
13525                 ,Created_By
13526                 ,Creation_Date
13527                 ,Last_Update_Login
13528                 ,Txn_Ccid
13529                 ,Burden_Cost_Flag
13530                 ,Status_Code
13531                 ,Je_Category_Name
13532                 ,Je_Source_Name
13533                 ,Org_id
13534                 ,Document_Line_Id
13535 		,reference1
13536 		,reference2
13537 		,reference3
13538 		,txn_interface_id  --REL12
13539                 )
13540           Select p_Packet_Id
13541                 ,Pa_Bc_Packets_s.NextVal
13542                 ,-1
13543                 ,g_sob_Id_tbl(i)
13544                 ,G_FC_Gl_Date
13545                 ,G_FC_Period_Name
13546                 ,g_Period_Year_tbl(i)
13547                 ,g_Pa_Date_tbl(i)
13548                 ,g_project_id_tbl(i)
13549                 ,g_task_id_tbl(i)
13550                 ,g_exp_type_tbl(i)
13551                 ,g_ei_date_tbl(i)
13552                 ,g_exp_org_id_tbl(i)
13553                 ,g_bud_ver_id_tbl(i)
13554  	        ,g_budget_line_id_tbl(i) -- R12 funds management uptake
13555                 ,'EXP'
13556                 ,X_ei_id
13557                 ,1
13558                 ,p_acct_raw_cost
13559                 ,0
13560                 ,p_acct_raw_cost
13561                 ,0
13562                 ,g_Request_Id
13563                 ,g_program_id
13564                 ,g_prog_appl_id
13565                 ,sysdate
13566                 ,'A'
13567                 ,sysdate
13568                 ,g_conc_login_id
13569                 ,g_conc_login_id
13570                 ,sysdate
13571                 ,g_conc_login_id
13572                 ,g_Txn_Ccid_tbl(i)
13573                 ,'N'
13574                 ,'P'
13575                 ,'Expenditures'
13576                 ,'Project Accounting'
13577                 ,g_org_id_tbl(i)
13578                 ,l_po_line_id
13579 		,'EXP'
13580 		,x_Ei_Id
13581 		,1
13582 		,p_txn_interface_id  --REL12
13583           From  Pa_Budget_Versions bv,
13584                 Pa_Budget_Types bt,
13585                 pa_budgetary_control_options pbct
13586          Where  bt.budget_type_Code = bv.budget_type_Code
13587            and  bt.budget_amount_code = 'C'
13588            and  bv.project_id = g_project_id_tbl(i)
13589            and  bv.current_flag = 'Y'
13590            and  pbct.project_id = bv.project_id
13591            and  pbct.BDGT_CNTRL_FLAG = 'Y'
13592            and  pbct.BUDGET_TYPE_CODE = bv.budget_type_code
13593            and (pbct.EXTERNAL_BUDGET_CODE = 'GL'
13594                 OR
13595                 pbct.EXTERNAL_BUDGET_CODE is NULL
13596                 );
13597 
13598          IF PG_DEBUG = 'Y' THEN
13599             log_message('log_message: ' || 'In insert_cash_ap_bc_pkt_auto - Inserted '||SQL%ROWCOUNT||' ACTUAL raw line(s) into bc packets');
13600          END IF;
13601 
13602        END IF;--IF p_act_fc_req = 'Y' THEN
13603       END LOOP;
13604      END IF; -- l_rec_bc_comm_exists ='Y' OR l_rec_pkt_comm_exists  = 'Y' THEN
13605 
13606     END IF; -- p_comm_fc_req ='Y' OR p_act_fc_req  = 'Y' THEN
13607 
13608 
13609      -- R12 AP lines uptake : Prepayment changes : Below insert will be fired for inserting 'EXP'
13610      -- records by fetching data from pa_transaction_interface_all table for below TXN records :
13611      -- a. Adjusting expenditures in txn_interface table
13612      -- b. For those commitments which were never FC'ed (eg : Prepayment application, pre-payment matched to PO ..)
13613 
13614      IF ((p_adj_act_fc_req = 'Y')
13615          OR (l_rec_bc_comm_exists ='N' AND l_rec_pkt_comm_exists  = 'N' AND p_act_fc_req  = 'Y' )) THEN
13616 
13617 	 IF PG_DEBUG = 'Y' THEN
13618             log_message('log_message: ' || 'In insert_cash_ap_bc_pkt_auto - Before inserting adjusted ACTUAL raw line from transaction import table');
13619          END IF;
13620 
13621          FORALL i IN 1 .. g_xface_Txn_interface_tbl.count
13622          Insert Into Pa_Bc_Packets
13623                 (Packet_Id
13624                 ,Bc_Packet_Id
13625                 ,Parent_Bc_Packet_Id
13626                 ,Set_Of_Books_Id
13627                 ,Gl_Date
13628                 ,Period_Name
13629                 ,Period_Year
13630                 ,Pa_Date
13631                 ,Project_Id
13632                 ,Task_Id
13633                 ,Expenditure_Type
13634                 ,Expenditure_Item_Date
13635                 ,Expenditure_Organization_Id
13636                 ,Budget_Version_Id
13637                 ,Document_Type
13638                 ,Document_Header_Id
13639                 ,Document_Distribution_Id
13640                 ,Entered_Dr
13641                 ,Entered_Cr
13642                 ,Accounted_Dr
13643                 ,Accounted_Cr
13644                 ,Request_Id
13645                 ,Program_Id
13646                 ,Program_Application_Id
13647                 ,Program_Update_Date
13648                 ,Actual_Flag
13649                 ,Last_Update_Date
13650                 ,Last_Updated_By
13651                 ,Created_By
13652                 ,Creation_Date
13653                 ,Last_Update_Login
13654                 ,Txn_Ccid
13655                 ,Burden_Cost_Flag
13656                 ,Status_Code
13657                 ,Je_Category_Name
13658                 ,Je_Source_Name
13659                 ,Org_id
13660                 ,Document_Line_Id
13661 		,reference1
13662 		,reference2
13663 		,reference3
13664 		,txn_interface_id  --REL12
13665                 )
13666           Select p_Packet_Id
13667                 ,Pa_Bc_Packets_s.NextVal
13668                 ,-1
13669                 ,g_sobid
13670                 ,G_FC_Gl_Date
13671                 ,G_FC_Period_Name
13672                 ,G_FC_Period_Year
13673                 ,G_PaDate
13674                 ,g_xface_project_id_tbl(i)
13675                 ,g_xface_task_id_tbl(i)
13676                 ,g_xface_exp_type_tbl(i)
13677                 ,g_xface_ei_date_tbl(i)
13678                 ,g_xface_exp_org_id_tbl(i)
13679                 ,g_xface_bud_ver_id_tbl(i)
13680                 ,'EXP'
13681                 ,X_ei_id
13682                 ,1
13683 		-- R12 AP lines uptake : Prepayment changes
13684 		-- For Prepayment application/pre-payment matched to PO records consider p_acct_raw_cost as amount
13685                 ,g_xface_Entered_Dr_tbl(i)
13686                 ,g_xface_acct_Cr_tbl(i)
13687                 ,g_xface_acct_Dr_tbl(i)
13688                 ,g_xface_acct_Cr_tbl(i)
13689                 ,g_Request_Id
13690                 ,g_program_id
13691                 ,g_prog_appl_id
13692                 ,sysdate
13693                 ,'A' --Actual_Flag
13694                 ,sysdate
13695                 ,g_conc_login_id
13696                 ,g_conc_login_id
13697                 ,sysdate
13698                 ,g_conc_login_id
13699                 ,g_xface_Txn_Ccid_tbl(i)
13700                 ,'N'
13701                 ,'P'
13702                 ,'Expenditures'
13703                 ,'Project Accounting'
13704                 ,g_xface_org_id_tbl(i)
13705                 ,l_po_line_id
13706 		,'EXP'
13707 		,X_Ei_Id
13708 		,1
13709 		,g_xface_Txn_interface_tbl(i)  --REL12
13710           From  dual;
13711 
13712 	 IF PG_DEBUG = 'Y' THEN
13713             log_message('log_message: ' || 'In insert_cash_ap_bc_pkt_auto - Before inserting adjusted ACTUAL raw line from transaction import table');
13714          END IF;
13715 
13716        END IF;
13717 
13718  COMMIT;
13719  IF PG_DEBUG = 'Y' THEN
13720      log_message('log_message: ' || 'In insert_cash_ap_bc_pkt_auto - End');
13721  END IF;
13722 
13723 EXCEPTION
13724 WHEN OTHERS THEN
13725    IF PG_DEBUG = 'Y' THEN
13726       log_message('log_message: In insert_cash_ap_bc_pkt_auto exception' || sqlerrm||' Returning from insert_cash_ap_bc_pkt_auto');
13727    END IF;
13728    RAISE;
13729 END insert_cash_ap_bc_pkt_auto;
13730 
13731 -- R12 funds management Uptake : New procedure fired in non-autonomous mode to read
13732 -- pa_transaction_interface_all table and to derive variables which drives the
13733 -- relieving/reserving of commitments and actuals flow during interface.
13734 
13735 PROCEDURE insert_po_bc_packets
13736                            (p_packet_id             IN NUMBER,
13737                             p_sys_ref2              IN NUMBER,
13738                             p_sys_ref4              IN NUMBER,
13739 			    p_sys_ref3              IN NUMBER,
13740                             p_acct_raw_cost         IN NUMBER,
13741 			    p_cmt_raw_cost          IN NUMBER,
13742                             p_fc_document_type      IN VARCHAR2,
13743                             p_txn_source            IN VARCHAR2,
13744                             p_adj_exp_item_id       IN NUMBER,
13745 			    p_txn_interface_id      IN NUMBER) IS
13746 
13747 -- Cursor to fetch data from pa_transaction_interface_table.This data is later used
13748 -- in autonomous fundscheck insert_ap_bc_pkts_autonomous procedure.
13749 
13750  CURSOR c_pa_txn_interface_data IS
13751  Select txn.Project_Id
13752         ,txn.Task_Id
13753         ,txn.Expenditure_Type
13754         ,txn.Expenditure_Item_Date
13755         ,nvl(txn.OVERRIDE_TO_ORGANIZATION_ID,txn.Org_Id)
13756         ,bv.Budget_Version_Id
13757 	-- R12 AP lines uptake : Prepayment changes
13758 	-- For Prepayment application/pre-payment matched to PO records consider p_acct_raw_cost as amount
13759         ,txn.acct_raw_cost
13760         ,0
13761         ,txn.acct_raw_cost
13762         ,0
13763         ,txn.cr_code_combination_id
13764         ,txn.Org_id
13765         ,txn.txn_interface_id  --REL12
13766   From  Pa_Budget_Versions bv,
13767         Pa_Budget_Types bt,
13768         pa_budgetary_control_options pbct,
13769         pa_transaction_interface_all txn
13770  Where  bv.budget_type_code = bt.budget_type_code
13771    and  bt.budget_amount_code = 'C'
13772    and  bv.current_flag = 'Y'
13773    AND  pbct.project_id = bv.project_id
13774    AND  pbct.BDGT_CNTRL_FLAG = 'Y'
13775    AND  pbct.BUDGET_TYPE_CODE = bv.budget_type_code
13776    AND (pbct.EXTERNAL_BUDGET_CODE = 'GL'
13777         OR
13778         pbct.EXTERNAL_BUDGET_CODE is NULL
13779         )
13780    AND bv.project_id = txn.project_id
13781    and txn.TRANSACTION_SOURCE = p_txn_source -- Condition for using index
13782     -- new index usage needs to be verified for below columns
13783    AND txn.cdl_system_reference2 = p_sys_ref2
13784    AND txn.cdl_system_reference3 = p_sys_ref3 --Bug 5550268
13785    --Bug 5550268  : Will be NULL when fired from ap_po_fundscheck_check
13786    AND (txn.cdl_system_reference4 = p_sys_ref4 OR p_sys_ref4 IS NULL)
13787    AND txn.fc_document_type in ('ACT','ALL')
13788    AND txn.adjusted_expenditure_item_id IS NOT NULL;
13789 
13790 l_comm_fc_req             VARCHAR2(1);  -- Variable to identify whether commitment fundscheck is required
13791 l_act_fc_req              VARCHAR2(1);  -- Variable to identify whether actual fundscheck is required
13792 l_adj_act_fc_req          VARCHAR2(1);  -- Variable to identify whether adjusted actual fundscheck is required
13793 
13794 BEGIN
13795 
13796     IF PG_DEBUG = 'Y' THEN
13797        log_message('log_message: ' || 'In insert_po_bc_packets - Start');
13798        log_message('log_message: ' || 'In insert_po_bc_packets - Value of p_packet_id '||p_packet_id);
13799        log_message('log_message: ' || 'In insert_po_bc_packets - Value of p_sys_ref2  '||p_sys_ref2 );
13800        log_message('log_message: ' || 'In insert_po_bc_packets - Value of p_sys_ref4  '||p_sys_ref4 );
13801        log_message('log_message: ' || 'In insert_po_bc_packets - Value of p_sys_ref3  '||p_sys_ref3 );
13802        log_message('log_message: ' || 'In insert_po_bc_packets - Value of p_acct_raw_cost '||p_acct_raw_cost);
13803        log_message('log_message: ' || 'In insert_po_bc_packets - Value of p_cmt_raw_cost '||p_cmt_raw_cost); -- Bug 5731450
13804        log_message('log_message: ' || 'In insert_po_bc_packets - Value of p_fc_document_type  '||p_fc_document_type );
13805        log_message('log_message: ' || 'In insert_po_bc_packets - Value of p_txn_source '||p_txn_source);
13806        log_message('log_message: ' || 'In insert_po_bc_packets - Value of p_adj_exp_item_id '||p_adj_exp_item_id);
13807        log_message('log_message: ' || 'In insert_po_bc_packets - Value of p_txn_interface_id '||p_txn_interface_id);
13808 
13809     END IF;
13810 
13811     -- Code to derive values for variables which will decide the flow of this procedure
13812 
13813     l_comm_fc_req :='N';        -- Variable to identify whether commitment fundscheck is required
13814     l_act_fc_req :='N';         -- Variable to identify whether actual fundscheck is required
13815     l_adj_act_fc_req:='N';      -- Variable to identify whether adjusted actual fundscheck is required
13816 
13817     -- Deriving value for variable which identifies if FC required for AP commitment
13818     IF  p_adj_exp_item_id IS NULL THEN
13819 
13820         IF p_fc_document_type ='ALL' THEN  --PO normal lines import
13821            l_comm_fc_req := 'Y' ;
13822            l_act_fc_req  := 'Y' ;
13823         END IF;
13824 
13825     ELSIF p_adj_exp_item_id IS NOT NULL THEN
13826 
13827         IF p_fc_document_type ='ALL' THEN    --PO having fc enabled and adjusted ei's having fc enabled
13828            l_comm_fc_req := 'Y' ;
13829            l_adj_act_fc_req := 'Y';
13830         ELSIF p_fc_document_type ='CMT' THEN --PO having fc enabled and adjusted ei's having fc disabled
13831            l_comm_fc_req := 'Y' ;
13832         ELSIF p_fc_document_type ='ACT' THEN --PO having fc disabled and adjusted ei's having fc ENabled
13833            l_adj_act_fc_req := 'Y';
13834         END IF;
13835 
13836     END IF;
13837 
13838     IF PG_DEBUG = 'Y' THEN
13839        log_message('log_message: ' || 'In insert_po_bc_packets - Value of l_comm_fc_req '||l_comm_fc_req);
13840        log_message('log_message: ' || 'In insert_po_bc_packets - Value of l_adj_act_fc_req   '||l_adj_act_fc_req  );
13841        log_message('log_message: ' || 'In insert_po_bc_packets - Value of l_act_fc_req  '||l_act_fc_req );
13842     END IF;
13843 
13844     -- Cursor to fetch data from pa_transaction_interface_table.This data is later used
13845     -- in autonomous fundscheck insert_ap_bc_pkts_autonomous procedure.
13846 
13847     IF ( l_adj_act_fc_req = 'Y'  OR l_act_fc_req  = 'Y' ) THEN
13848 
13849        -- call to clean up global plsql arrays used for storing interface data.
13850        init_xface_plsql_tables;
13851 
13852        OPEN c_pa_txn_interface_data;
13853        FETCH c_pa_txn_interface_data BULK COLLECT INTO
13854                                           g_xface_project_id_tbl,
13855 					  g_xface_task_id_tbl,
13856 					  g_xface_exp_type_tbl,
13857 					  g_xface_ei_date_tbl,
13858 					  g_xface_exp_org_id_tbl,
13859 					  g_xface_bud_ver_id_tbl,
13860 					  g_xface_Entered_Dr_tbl,
13861 					  g_xface_Entered_Cr_tbl,
13862 					  g_xface_acct_Dr_tbl,
13863 				          g_xface_acct_Cr_tbl,
13864 					  g_xface_Txn_Ccid_tbl,
13865 					  g_xface_org_id_tbl,
13866 					  g_xface_Txn_interface_tbl;
13867        CLOSE c_pa_txn_interface_data;
13868 
13869        IF PG_DEBUG = 'Y' THEN
13870           log_message('log_message: ' || 'In insert_po_bc_packets - fetched data from interface table c_pa_txn_interface_data ');
13871        END IF;
13872 
13873     END IF; --IF ( l_adj_act_fc_req = 'Y'  OR l_act_fc_req  = 'Y' ) THEN
13874 
13875     IF PG_DEBUG = 'Y' THEN
13876        log_message('log_message: ' || 'In insert_po_bc_packets - calling insert_po_bc_packets_auto');
13877     END IF;
13878 
13879     insert_po_bc_packets_auto
13880                            (p_packet_id             => p_packet_id,
13881                             p_sys_ref2              => p_sys_ref2,
13882                             p_sys_ref4              => p_sys_ref4,
13883 			    p_sys_ref3              => p_sys_ref3,
13884                             p_acct_raw_cost         => p_acct_raw_cost,
13885 			    p_cmt_raw_cost          => p_cmt_raw_cost,
13886                             p_fc_document_type      => p_fc_document_type,
13887                             p_txn_source            => p_txn_source,
13888                             p_adj_exp_item_id       => p_adj_exp_item_id ,
13889 			    p_txn_interface_id      => p_txn_interface_id,
13890 			    p_comm_fc_req           => l_comm_fc_req,
13891                             p_act_fc_req            => l_act_fc_req,
13892                             p_adj_act_fc_req        => l_adj_act_fc_req );
13893 
13894     IF PG_DEBUG = 'Y' THEN
13895        log_message('log_message: ' || 'In insert_po_bc_packets - End');
13896     END IF;
13897 
13898 EXCEPTION
13899 WHEN OTHERS THEN
13900    IF PG_DEBUG = 'Y' THEN
13901       log_message('log_message:I n insert_po_bc_packets exception' || sqlerrm||' Returning from insert_po_bc_packets');
13902    END IF;
13903    RAISE;
13904 END insert_po_bc_packets;
13905 
13906 PROCEDURE insert_po_bc_packets_auto
13907                            (p_packet_id             IN NUMBER,
13908                             p_sys_ref2              IN NUMBER,
13909                             p_sys_ref4              IN NUMBER,
13910 			    p_sys_ref3              IN NUMBER,
13911                             p_acct_raw_cost         IN NUMBER,
13912                             p_cmt_raw_cost          IN NUMBER,
13913                             p_fc_document_type      IN VARCHAR2,
13914                             p_txn_source            IN VARCHAR2,
13915                             p_adj_exp_item_id       IN NUMBER,
13916 			    p_txn_interface_id      IN NUMBER,
13917 			    p_comm_fc_req           IN VARCHAR2,
13918                             p_act_fc_req            IN VARCHAR2,
13919                             p_adj_act_fc_req        IN VARCHAR2) IS
13920 CURSOR c_gen_raw_bc_pkt IS
13921 SELECT Pa_Bc_Packets_s.NextVal
13922   FROM dual;
13923 
13924 CURSOR c_get_po_LINE_id (p_po_dist_id NUMBER) IS
13925 SELECT po_line_id
13926   FROM po_distributions_all
13927  WHERE po_distribution_id = p_po_dist_id;
13928 
13929 Cursor C_po_Commitment Is
13930 Select  bcc.Set_Of_Books_Id
13931        ,bcc.Period_Year
13932        ,bcc.Project_Id
13933        ,bcc.Task_Id
13934        ,bcc.Expenditure_Type
13935        ,bcc.Expenditure_Item_Date
13936        ,bcc.Expenditure_Organization_Id
13937        ,bcc.Budget_Version_Id
13938        ,bcc.budget_line_id -- R12 funds management uptake
13939        ,bcc.Document_Type
13940        ,bcc.Document_Header_Id
13941        ,bcc.Document_Distribution_Id
13942        ,bcc.Entered_Cr
13943        ,bcc.Entered_Dr
13944        ,bcc.Accounted_Cr
13945        ,bcc.Accounted_Dr
13946        ,bcc.Actual_Flag
13947        ,bcc.Txn_Ccid
13948        ,bcc.Je_Category_Name
13949        ,bcc.Je_Source_Name
13950        ,bcc.Org_Id
13951        ,bcc.Pa_Date
13952        ,bcc.bc_packet_id
13953        ,bcc.packet_id
13954        ,bcc.bc_commitment_id
13955  From  Pa_Bc_Commitments_all bcc,
13956        po_distributions pod
13957   -- R12 Funds management Uptake : For fixing performance issues removing the code for CC which is obsolete for R12
13958   -- Modifying code to use Index on document_header_id and document_distribution_id of bc commitments
13959 where bcc.document_header_id = pod.po_header_id
13960   and bcc.document_distribution_id = pod.po_distribution_id
13961   and bcc.project_id = pod.project_id -- Bug# 4479105
13962   and bcc.task_id = pod.task_id --Bug# 4479105
13963   and bcc.document_type = 'PO'
13964   and pod.po_header_id = p_sys_ref2
13965   and pod.po_distribution_id = p_sys_ref3
13966   and bcc.exp_item_id IS NULL -- Bug 5014138 : to pick just the encumbrance record
13967   and bcc.actual_flag = 'E'   -- Bug 5014138 : to pick just the encumbrance record
13968   and  bcc.Parent_Bc_Packet_Id is NULL
13969 Order By  bcc.packet_id;
13970 
13971 Cursor C_po_Bc_Packets Is
13972 Select  bcc.Set_Of_Books_Id
13973        ,bcc.Period_Year
13974        ,bcc.Project_Id
13975        ,bcc.Task_Id
13976        ,bcc.Expenditure_Type
13977        ,bcc.Expenditure_Item_Date
13978        ,bcc.Expenditure_Organization_Id
13979        ,bcc.Budget_Version_Id
13980        ,bcc.budget_line_id -- R12 funds management uptake
13981        ,bcc.Document_Type
13982        ,bcc.Document_Header_Id
13983        ,bcc.Document_Distribution_Id
13984        ,bcc.Entered_Cr
13985        ,bcc.Entered_Dr
13986        ,bcc.Accounted_Cr
13987        ,bcc.Accounted_Dr
13988        ,bcc.Actual_Flag
13989        ,bcc.Txn_Ccid
13990        ,bcc.Je_Category_Name
13991        ,bcc.Je_Source_Name
13992        ,bcc.Org_Id
13993        ,bcc.Pa_Date
13994        ,bcc.bc_packet_id
13995        ,bcc.packet_id
13996   -- R12 Funds management Uptake : For fixing performance issues removing the code for CC which is obsolete for R12
13997   -- Modifying code to use Index on document_header_id and document_distribution_id of bc commitments
13998  From  Pa_Bc_packets bcc,
13999        po_distributions pod
14000 where bcc.document_header_id = pod.po_header_id
14001   and bcc.document_distribution_id = pod.po_distribution_id
14002   and bcc.project_id = pod.project_id -- Bug# 4479105
14003   and bcc.task_id = pod.task_id --Bug# 4479105
14004   and bcc.document_type = 'PO'
14005   and pod.po_header_id = p_sys_ref2
14006   and pod.po_distribution_id = p_sys_ref3
14007   and bcc.exp_item_id IS NULL -- Bug 5014138 : to pick just the encumbrance record
14008   and bcc.actual_flag = 'E'   -- Bug 5014138 : to pick just the encumbrance record
14009   and  bcc.Status_Code in('A','C')
14010   and  bcc.Parent_Bc_Packet_Id is NULL
14011 Order By  bcc.packet_id;
14012 
14013 l_rec_bc_comm_exists      VARCHAR2(1);  -- Variable to identify whether commitment record exists in pa_bc_commitments
14014 l_rec_pkt_comm_exists     VARCHAR2(1);  -- Variable to identify whether commitment record exists in pa_bc_packets
14015 l_Raw_Bc_Packet_Id        pa_bc_packets.bc_packet_id%TYPE;
14016 l_po_line_id              po_distributions_all.po_line_id%TYPE;
14017 
14018 BEGIN
14019 
14020     IF PG_DEBUG = 'Y' THEN
14021        log_message('log_message: ' || 'In insert_po_bc_packets_auto - Start');
14022     END IF;
14023 
14024        /* cwk */
14025 
14026     IF p_txn_source in ('PO RECEIPT NRTAX', 'AP NRTAX') THEN
14027       IF nvl(Pa_Pjc_Cwk_Utils.Is_rate_based_line( null, p_sys_ref3),'N') = 'Y' THEN
14028         OPEN c_get_po_LINE_id(p_sys_ref3);
14029         FETCH c_get_po_LINE_id into l_po_line_id;
14030         CLOSE c_get_po_LINE_id;
14031       END IF;
14032 
14033          IF PG_DEBUG = 'Y' THEN
14034             log_message('log_message: ' || 'In insert_po_bc_packets_auto - After fetching CWK related data l_po_line_id'||l_po_line_id);
14035          END IF;
14036     END IF;
14037 
14038     -- Code to derive values for variables which will decide the flow of this procedure
14039 
14040     l_rec_bc_comm_exists:='N';  -- Variable to identify whether commitment record exists in pa_bc_commitments
14041     l_rec_pkt_comm_exists:='N'; -- Variable to identify whether commitment record exists in pa_bc_packets
14042 
14043     IF p_comm_fc_req ='Y' OR p_act_fc_req  = 'Y' THEN
14044 
14045       IF PG_DEBUG = 'Y' THEN
14046          log_message('log_message: ' || 'In insert_po_bc_packets_auto - Fetchign data from bc commitments');
14047       END IF;
14048 
14049       Open C_po_Commitment ;
14050 
14051       clear_plsql_tables;
14052 
14053       FETCH C_po_Commitment BULK COLLECT INTO
14054                 g_sob_Id_tbl,
14055                 g_Period_Year_tbl,
14056                 g_project_id_tbl,
14057                 g_task_id_tbl,
14058                 g_exp_type_tbl,
14059                 g_ei_date_tbl,
14060                 g_exp_org_id_tbl,
14061                 g_bud_ver_id_tbl,
14062                 g_budget_line_id_tbl, -- R12 funds management uptake
14063                 g_Document_Type_tbl,
14064                 g_Doc_Header_Id_tbl,
14065                 g_Doc_dist_Id_tbl,
14066                 g_Entered_Cr_tbl,
14067                 g_Entered_Dr_tbl,
14068                 g_acct_Cr_tbl,
14069                 g_acct_Dr_tbl,
14070                 g_Actual_Flag_tbl,
14071                 g_Txn_Ccid_tbl,
14072                 g_Je_Catg_Name_tbl,
14073                 g_Je_sorce_Name_tbl,
14074                 g_org_id_tbl,
14075                 g_Pa_Date_tbl,
14076                 g_bc_packet_id_tbl,
14077                 g_packet_id_tbl,
14078 		g_bc_comt_id_tbl;
14079 
14080      IF g_bc_packet_id_tbl.COUNT <> 0 THEN
14081         l_rec_bc_comm_exists  := 'Y';
14082      END IF;
14083      CLOSE C_po_Commitment;
14084 
14085      IF l_rec_bc_comm_exists = 'N' THEN
14086 
14087         IF PG_DEBUG = 'Y' THEN
14088            log_message('log_message: ' || 'In insert_po_bc_packets_auto - Fetchign data from bc packets');
14089         END IF;
14090 
14091         OPEN C_po_Bc_Packets;
14092         FETCH C_po_Bc_Packets BULK COLLECT INTO
14093                 g_sob_Id_tbl,
14094                 g_Period_Year_tbl,
14095                 g_project_id_tbl,
14096                 g_task_id_tbl,
14097                 g_exp_type_tbl,
14098                 g_ei_date_tbl,
14099                 g_exp_org_id_tbl,
14100                 g_bud_ver_id_tbl,
14101                 g_budget_line_id_tbl, -- R12 funds management uptake
14102                 g_Document_Type_tbl,
14103                 g_Doc_Header_Id_tbl,
14104                 g_Doc_dist_Id_tbl,
14105                 g_Entered_Cr_tbl,
14106                 g_Entered_Dr_tbl,
14107                 g_acct_Cr_tbl,
14108                 g_acct_Dr_tbl,
14109                 g_Actual_Flag_tbl,
14110                 g_Txn_Ccid_tbl,
14111                 g_Je_Catg_Name_tbl,
14112                 g_Je_sorce_Name_tbl,
14113                 g_org_id_tbl,
14114                 g_Pa_Date_tbl,
14115                 g_bc_packet_id_tbl,
14116                 g_packet_id_tbl;
14117 
14118         IF g_bc_packet_id_tbl.COUNT <> 0 THEN
14119            l_rec_pkt_comm_exists := 'Y' ;
14120         END IF;
14121         CLOSE C_po_Bc_Packets;
14122 
14123       END IF;
14124 
14125      IF l_rec_bc_comm_exists ='Y' OR l_rec_pkt_comm_exists  = 'Y' THEN
14126 
14127       FOR i IN g_bc_packet_id_tbl.FIRST..g_bc_packet_id_tbl.LAST LOOP
14128 
14129 
14130         -- Generate a packet identifier for RAW record that
14131         -- needs to be inserted into Pa BC Packets
14132 
14133         IF PG_DEBUG = 'Y' THEN
14134            log_message('log_message: ' || 'In insert_po_bc_packets_auto - Generating new Bc packet Id for reversing RAW act and commitment line');
14135         END IF;
14136 
14137         OPEN  c_gen_raw_bc_pkt;
14138         FETCH c_gen_raw_bc_pkt INTO l_Raw_Bc_Packet_Id;
14139         CLOSE c_gen_raw_bc_pkt;
14140 
14141         IF PG_DEBUG = 'Y' THEN
14142           log_message('log_message: ' || 'In insert_po_bc_packets_auto - Bc Packet Id = ' || l_Raw_Bc_Packet_Id ||'Packet Id = ' || p_packet_id);
14143         END IF;
14144 
14145         IF l_rec_bc_comm_exists = 'Y' AND p_comm_fc_req ='Y' THEN
14146 
14147           IF PG_DEBUG = 'Y' THEN
14148            log_message('log_message: ' || 'In insert_po_bc_packets_auto - Before inserting PO reversing raw and burden line from bc commitments');
14149           END IF;
14150 
14151           Insert Into Pa_Bc_Packets
14152                 (Packet_Id
14153                 ,Bc_Packet_Id
14154                 ,Parent_Bc_Packet_Id
14155                 ,Set_Of_Books_Id
14156                 ,Gl_Date
14157                 ,Period_Name
14158                 ,Period_Year
14159                 ,Pa_Date
14160                 ,Project_Id
14161                 ,Task_Id
14162                 ,Expenditure_Type
14163                 ,Expenditure_Item_Date
14164                 ,Expenditure_Organization_Id
14165                 ,Budget_Version_Id
14166                 ,Budget_line_id -- R12 funds management uptake
14167                 ,Document_Type
14168                 ,Document_Header_Id
14169                 ,Document_Distribution_Id
14170                 ,Entered_Dr
14171                 ,Entered_Cr
14172                 ,Accounted_Dr
14173                 ,Accounted_Cr
14174                 ,Request_Id
14175                 ,Program_Id
14176                 ,Program_Application_Id
14177                 ,Program_Update_Date
14178                 ,Actual_Flag
14179                 ,Last_Update_Date
14180                 ,Last_Updated_By
14181                 ,Created_By
14182                 ,Creation_Date
14183                 ,Last_Update_Login
14184                 ,Txn_Ccid
14185                 ,Burden_Cost_Flag
14186                 ,Status_Code
14187                 ,Je_Category_Name
14188                 ,Je_Source_Name
14189                 ,Org_id
14190                 ,Exp_Item_Id
14191                 ,Document_Line_Id
14192 		,reference1
14193 		,reference2
14194 		,reference3
14195 		,txn_interface_id  --REL12
14196 		,encumbrance_type_id
14197 		,document_header_id_2
14198 		,document_distribution_type
14199                 )
14200          Select  p_packet_id
14201                 ,decode(bcc.Parent_Bc_Packet_Id,NULL,l_Raw_Bc_Packet_id,Pa_Bc_Packets_s.NextVal)
14202                 ,decode(bcc.Parent_Bc_Packet_Id,NULL,NULL,l_Raw_Bc_Packet_id)
14203                 ,bcc.Set_Of_Books_Id
14204                 ,G_FC_Gl_Date
14205                 ,G_FC_Period_Name
14206                 ,bcc.Period_Year
14207                 ,bcc.Pa_Date -- pa_date on raw is used for burden line
14208                 ,bcc.Project_Id
14209                 ,bcc.Task_Id
14210                 ,bcc.Expenditure_Type
14211                 ,bcc.Expenditure_Item_Date
14212                 ,bcc.Expenditure_Organization_Id
14213                 ,bcc.Budget_Version_Id
14214                 ,bcc.Budget_line_id -- R12 funds management uptake
14215                 ,bcc.Document_Type
14216                 ,bcc.Document_Header_Id
14217                 ,bcc.Document_Distribution_Id
14218                 ,DECODE(bcc.parent_bc_packet_id,NULL,
14219                                                 (-1 * p_cmt_raw_cost), --rshaik
14220                                                 (0 - (pa_currency.round_trans_currency_amt1((((nvl(bcc.Entered_dr,0)-nvl(bcc.Entered_Cr,0))/
14221                                                   decode((nvl(g_Entered_Dr_tbl(i),0)-nvl(g_Entered_Cr_tbl(i),0)),
14222                                                   0,1,(nvl(g_Entered_Dr_tbl(i),0)-nvl(g_Entered_Cr_tbl(i),0))))
14223                                                   *p_cmt_raw_cost), G_accounting_currency_code))))
14224                 ,0
14225                 ,DECODE(bcc.parent_bc_packet_id,NULL,
14226                                                 (-1 * p_cmt_raw_cost)
14227                                                 ,(0 - (pa_currency.round_trans_currency_amt1((((nvl(bcc.Accounted_dr,0)-nvl(bcc.Accounted_cr,0))/
14228                                                   decode((nvl(g_acct_Dr_tbl(i),0)-nvl(g_acct_Cr_tbl(i),0))
14229                                                   ,0,1,(nvl(g_acct_Dr_tbl(i),0)-nvl(g_acct_Cr_tbl(i),0))))
14230                                                   *p_cmt_raw_cost), G_accounting_currency_code))))
14231                 ,0
14232                 ,g_Request_Id
14233                 ,G_Program_Id
14234                 ,G_PROG_APPL_ID
14235                 ,sysdate
14236                 ,bcc.Actual_Flag
14237                 ,sysdate
14238                 ,G_CONC_LOGIN_ID
14239                 ,G_CONC_LOGIN_ID
14240                 ,sysdate
14241                 ,G_CONC_LOGIN_ID
14242                 ,bcc.Txn_Ccid
14243                 ,'R'
14244                 ,'P'
14245                 ,decode(p_txn_source, 'AP NRTAX' , g_Je_Catg_Name_tbl(i)||' Tax', g_Je_Catg_Name_tbl(i))
14246                 ,bcc.Je_Source_Name
14247                 ,bcc.Org_Id
14248                 ,X_ei_id
14249                 ,l_po_line_id
14250 		,'EXP'
14251 		,X_ei_id
14252 		,1
14253 		,p_txn_interface_id --REL12
14254 		,bcc.encumbrance_type_id
14255 		,bcc.document_header_id_2
14256 		,bcc.document_distribution_type
14257           From  Pa_Bc_Commitments_all bcc
14258          Where  bcc.bc_commitment_id in (SELECT bcc1.bc_commitment_id
14259                                            FROM PA_BC_COMMITMENTS_ALL bcc1
14260                                           WHERE bcc1.document_header_id = p_sys_ref2
14261                                             AND bcc1.document_distribution_id = p_sys_ref3
14262 					    AND bcc1.parent_bc_packet_id = g_bc_packet_id_tbl(i)
14263                                           UNIOn ALL
14264                                          SELECT g_bc_comt_id_tbl(i)
14265                                            FROM DUAL);
14266 
14267         IF PG_DEBUG = 'Y' THEN
14268          log_message('log_message: ' || 'In insert_po_bc_packets_auto - Inserted '||SQL%ROWCOUNT||' PO reversing raw and burden line(s) into bc packets');
14269         END IF;
14270 
14271        END IF; --IF l_rec_bc_comm_exists = 'Y' AND p_comm_fc_req ='Y' THEN
14272 
14273 
14274        IF l_rec_pkt_comm_exists='Y' AND p_comm_fc_req ='Y' THEN
14275 
14276           IF PG_DEBUG = 'Y' THEN
14277             log_message('log_message: ' || 'In insert_po_bc_packets_auto - Before inserting the PO raw and burden lines from Bc Packets');
14278           END IF;
14279 
14280           Insert Into Pa_Bc_Packets
14281                 (Packet_Id
14282                 ,Bc_Packet_Id
14283                 ,Parent_Bc_Packet_Id
14284                 ,Set_Of_Books_Id
14285                 ,Gl_Date
14286                 ,Period_Name
14287                 ,Period_Year
14288                 ,Pa_Date
14289                 ,Project_Id
14290                 ,Task_Id
14291                 ,Expenditure_Type
14292                 ,Expenditure_Item_Date
14293                 ,Expenditure_Organization_Id
14294                 ,Budget_Version_Id
14295                 ,Budget_line_id -- R12 funds management uptake
14296                 ,Document_Type
14297                 ,Document_Header_Id
14298                 ,Document_Distribution_Id
14299                 ,Entered_Dr
14300                 ,Entered_Cr
14301                 ,Accounted_Dr
14302                 ,Accounted_Cr
14303                 ,Request_Id
14304                 ,Program_Id
14305                 ,Program_Application_Id
14306                 ,Program_Update_Date
14307                 ,Actual_Flag
14308                 ,Last_Update_Date
14309                 ,Last_Updated_By
14310                 ,Created_By
14311                 ,Creation_Date
14312                 ,Last_Update_Login
14313                 ,Txn_Ccid
14314                 ,Burden_Cost_Flag
14315                 ,Status_Code
14316                 ,Je_Category_Name
14317                 ,Je_Source_Name
14318                 ,Org_id
14319                 ,Exp_Item_Id
14320                 ,Document_Line_Id
14321 		,reference1
14322 		,reference2
14323 		,reference3
14324 		,txn_interface_id  --REL12
14325 		,encumbrance_type_id
14326 		,document_header_id_2
14327 		,document_distribution_type
14328                 )
14329           Select p_packet_id
14330                 ,decode(bcc.Parent_Bc_Packet_Id,NULL,l_Raw_Bc_Packet_id,Pa_Bc_Packets_s.NextVal)
14331                 ,decode(bcc.Parent_Bc_Packet_Id,NULL,NULL,l_Raw_Bc_Packet_id)
14332                 ,bcc.Set_Of_Books_Id
14333                 ,G_FC_Gl_Date
14334                 ,G_FC_Period_Name
14335                 ,bcc.Period_Year
14336                 ,bcc.pa_date
14337                 ,bcc.Project_Id
14338                 ,bcc.Task_Id
14339                 ,bcc.Expenditure_Type
14340                 ,bcc.Expenditure_Item_Date
14341                 ,bcc.Expenditure_Organization_Id
14342                 ,bcc.Budget_Version_Id
14343                 ,bcc.Budget_line_id -- R12 funds management uptake
14344                 ,bcc.Document_Type
14345                 ,bcc.Document_Header_Id
14346                 ,bcc.Document_Distribution_Id
14347                 ,DECODE(bcc.parent_bc_packet_id,NULL,
14348                                                 -1 * p_cmt_raw_cost,
14349                                                 (0 - (pa_currency.round_trans_currency_amt1((((nvl(bcc.Entered_dr,0)-nvl(bcc.Entered_Cr,0))/
14350                                                   decode((nvl(g_Entered_Dr_tbl(i),0)-nvl(g_Entered_Cr_tbl(i),0)),
14351                                                   0,1,(nvl(g_Entered_Dr_tbl(i),0)-nvl(g_Entered_Cr_tbl(i) ,0))))
14352                                                   *p_cmt_raw_cost), G_accounting_currency_code))))
14353                 ,0
14354                 ,DECODE(bcc.parent_bc_packet_id,NULL,
14355                                                 -1 * p_cmt_raw_cost
14356                                                 ,(0 - (pa_currency.round_trans_currency_amt1((((nvl(bcc.Accounted_dr,0)-nvl(bcc.Accounted_cr,0))/
14357                                                   decode((nvl(g_acct_Dr_tbl(i),0)-nvl(g_acct_Cr_tbl(i),0))
14358                                                   ,0,1,(nvl(g_acct_Dr_tbl(i),0)-nvl(g_acct_Cr_tbl(i),0))))
14359                                                   *p_cmt_raw_cost), G_accounting_currency_code))))
14360                 ,0
14361                 ,g_Request_Id
14362                 ,G_Program_Id
14363                 ,G_PROG_APPL_ID
14364                 ,sysdate
14365                 ,bcc.Actual_Flag
14366                 ,sysdate
14367                 ,G_CONC_LOGIN_ID
14368                 ,G_CONC_LOGIN_ID
14369                 ,sysdate
14370                 ,G_CONC_LOGIN_ID
14371                 ,bcc.Txn_Ccid
14372                 ,'R'
14373                 ,'P'
14374                 ,bcc.Je_Category_Name
14375                 ,bcc.Je_Source_Name
14376                 ,bcc.Org_id
14377                 ,X_ei_id
14378                 ,l_po_line_id
14379 		,'EXP'
14380 		,X_ei_id
14381 		,1
14382 		,p_txn_interface_id  --REL12
14383 		,bcc.encumbrance_type_id
14384 		,bcc.document_header_id_2
14385 		,bcc.document_distribution_type
14386           From  Pa_Bc_Packets bcc
14387          Where  bcc.packet_id = g_packet_id_tbl(i)
14388            and  bcc.bc_packet_id in (SELECT bcc1.bc_packet_id
14389                                        FROM PA_BC_PACKETS bcc1
14390                                       WHERE bcc1.packet_id = g_packet_id_tbl(i)
14391 				       and  bcc1.parent_bc_packet_id = g_bc_packet_id_tbl(i)
14392 				       and  bcc1.Status_Code in ('A','C')
14393                                      UNIOn ALL
14394                                      SELECT g_bc_packet_id_tbl(i)
14395                                        FROM DUAL);
14396 
14397          IF PG_DEBUG = 'Y' THEN
14398          log_message('log_message: ' || 'In insert_po_bc_packets_auto - Inserted '||SQL%ROWCOUNT||' PO reversing raw and burden line(s) into bc packets');
14399         END IF;
14400 
14401       END IF; --IF l_rec_pkt_comm_exists='Y' AND p_comm_fc_req ='Y' THEN
14402 
14403       IF p_act_fc_req = 'Y' THEN
14404 
14405          -- Insert the positive records for the raw line with
14406          -- document type 'EXP'.
14407 
14408          IF PG_DEBUG = 'Y' THEN
14409             log_message('log_message: ' || 'In insert_po_bc_packets_auto - Before inserting ACTUAL raw line from bc packets');
14410          END IF;
14411 
14412           Insert Into Pa_Bc_Packets
14413                 (Packet_Id
14414                 ,Bc_Packet_Id
14415                 ,Parent_Bc_Packet_Id
14416                 ,Set_Of_Books_Id
14417                 ,gl_date
14418                 ,Period_Name
14419                 ,Period_Year
14420                 ,Pa_Date
14421                 ,Project_Id
14422                 ,Task_Id
14423                 ,Expenditure_Type
14424                 ,Expenditure_Item_Date
14425                 ,Expenditure_Organization_Id
14426                 ,Budget_Version_Id
14427                 ,Budget_line_id -- R12 funds management uptake
14428                 ,Document_Type
14429                 ,Document_Header_Id
14430                 ,Document_Distribution_Id
14431                 ,Entered_Dr
14432                 ,Entered_Cr
14433                 ,Accounted_Dr
14434                 ,Accounted_Cr
14435                 ,Request_Id
14436                 ,Program_Id
14437                 ,Program_Application_Id
14438                 ,Program_Update_Date
14439                 ,Actual_Flag
14440                 ,Last_Update_Date
14441                 ,Last_Updated_By
14442                 ,Created_By
14443                 ,Creation_Date
14444                 ,Last_Update_Login
14445                 ,Txn_Ccid
14446                 ,Burden_Cost_Flag
14447                 ,Status_Code
14448                 ,Je_Category_Name
14449                 ,Je_Source_Name
14450                 ,Org_id
14451                 ,Document_Line_Id
14452 		,reference1
14453 		,reference2
14454 		,reference3
14455 		,txn_interface_id  --REL12
14456                 )
14457           Select p_Packet_Id
14458                 ,Pa_Bc_Packets_s.NextVal
14459                 ,-1
14460                 ,g_sob_Id_tbl(i)
14461                 ,G_FC_Gl_Date
14462                 ,G_FC_Period_Name
14463                 ,g_Period_Year_tbl(i)
14464                 ,g_Pa_Date_tbl(i)
14465                 ,g_project_id_tbl(i)
14466                 ,g_task_id_tbl(i)
14467                 ,g_exp_type_tbl(i)
14468                 ,g_ei_date_tbl(i)
14469                 ,g_exp_org_id_tbl(i)
14470                 ,g_bud_ver_id_tbl(i)
14471                 ,g_budget_line_id_tbl(i) -- R12 funds management uptake
14472                 ,'EXP'
14473                 ,X_ei_id
14474                 ,1
14475                 ,nvl(p_acct_raw_cost,0)
14476                 ,0
14477                 ,nvl(p_acct_raw_cost,0)
14478                 ,0
14479                 ,g_Request_Id
14480                 ,g_program_id
14481                 ,g_prog_appl_id
14482                 ,sysdate
14483                 ,'A'
14484                 ,sysdate
14485                 ,g_conc_login_id
14486                 ,g_conc_login_id
14487                 ,sysdate
14488                 ,g_conc_login_id
14489                 ,g_Txn_Ccid_tbl(i)
14490                 ,'N'
14491                 ,'P'
14492                 ,'Expenditures'
14493                 ,'Project Accounting'
14494                 ,g_org_id_tbl(i)
14495                 ,l_po_line_id
14496 		,'EXP'
14497 		,x_Ei_Id
14498 		,1
14499 		,p_txn_interface_id  --REL12
14500           From  Pa_Budget_Versions bv,
14501                 Pa_Budget_Types bt,
14502                 pa_budgetary_control_options pbct
14503          Where  bt.budget_type_Code = bv.budget_type_Code
14504            and  bt.budget_amount_code = 'C'
14505            and  bv.project_id = g_project_id_tbl(i)
14506            and  bv.current_flag = 'Y'
14507            and  pbct.project_id = bv.project_id
14508            and  pbct.BDGT_CNTRL_FLAG = 'Y'
14509            and  pbct.BUDGET_TYPE_CODE = bv.budget_type_code
14510            and (pbct.EXTERNAL_BUDGET_CODE = 'GL'
14511                 OR
14512                 pbct.EXTERNAL_BUDGET_CODE is NULL
14513                 );
14514 
14515          IF PG_DEBUG = 'Y' THEN
14516             log_message('log_message: ' || 'In insert_po_bc_packets_auto - Inserted '||SQL%ROWCOUNT||' ACTUAL raw line(s) into bc packets');
14517          END IF;
14518 
14519        END IF;--IF p_act_fc_req = 'Y' THEN
14520       END LOOP;
14521      END IF; -- l_rec_bc_comm_exists ='Y' OR l_rec_pkt_comm_exists  = 'Y' THEN
14522     END IF; -- p_comm_fc_req ='Y' OR p_act_fc_req  = 'Y' THEN
14523 
14524      IF p_adj_act_fc_req = 'Y' THEN
14525 
14526 	 IF PG_DEBUG = 'Y' THEN
14527             log_message('log_message: ' || 'In insert_po_bc_packets_auto - Before inserting adjusted ACTUAL raw line from transaction import table');
14528          END IF;
14529 
14530          FORALL i IN 1 .. g_xface_Txn_interface_tbl.count
14531          Insert Into Pa_Bc_Packets
14532                 (Packet_Id
14533                 ,Bc_Packet_Id
14534                 ,Parent_Bc_Packet_Id
14535                 ,Set_Of_Books_Id
14536                 ,Gl_Date
14537                 ,Period_Name
14538                 ,Period_Year
14539                 ,Pa_Date
14540                 ,Project_Id
14541                 ,Task_Id
14542                 ,Expenditure_Type
14543                 ,Expenditure_Item_Date
14544                 ,Expenditure_Organization_Id
14545                 ,Budget_Version_Id
14546                 ,Document_Type
14547                 ,Document_Header_Id
14548                 ,Document_Distribution_Id
14549                 ,Entered_Dr
14550                 ,Entered_Cr
14551                 ,Accounted_Dr
14552                 ,Accounted_Cr
14553                 ,Request_Id
14554                 ,Program_Id
14555                 ,Program_Application_Id
14556                 ,Program_Update_Date
14557                 ,Actual_Flag
14558                 ,Last_Update_Date
14559                 ,Last_Updated_By
14560                 ,Created_By
14561                 ,Creation_Date
14562                 ,Last_Update_Login
14563                 ,Txn_Ccid
14564                 ,Burden_Cost_Flag
14565                 ,Status_Code
14566                 ,Je_Category_Name
14567                 ,Je_Source_Name
14568                 ,Org_id
14569                 ,Document_Line_Id
14570 		,reference1
14571 		,reference2
14572 		,reference3
14573 		,txn_interface_id  --REL12
14574                 )
14575           Select p_Packet_Id
14576                 ,Pa_Bc_Packets_s.NextVal
14577                 ,-1
14578                 ,g_sobid
14579                 ,G_FC_Gl_Date
14580                 ,G_FC_Period_Name
14581                 ,G_FC_Period_Year
14582                 ,G_PaDate
14583                 ,g_xface_project_id_tbl(i)
14584                 ,g_xface_task_id_tbl(i)
14585                 ,g_xface_exp_type_tbl(i)
14586                 ,g_xface_ei_date_tbl(i)
14587                 ,g_xface_exp_org_id_tbl(i)
14588                 ,g_xface_bud_ver_id_tbl(i)
14589                 ,'EXP'
14590                 ,X_ei_id
14591                 ,1
14592                 ,g_xface_Entered_Dr_tbl(i)
14593                 ,g_xface_acct_Cr_tbl(i)
14594                 ,g_xface_acct_Dr_tbl(i)
14595                 ,g_xface_acct_Cr_tbl(i)
14596                 ,g_Request_Id
14597                 ,g_program_id
14598                 ,g_prog_appl_id
14599                 ,sysdate
14600                 ,'A' --Actual_Flag
14601                 ,sysdate
14602                 ,g_conc_login_id
14603                 ,g_conc_login_id
14604                 ,sysdate
14605                 ,g_conc_login_id
14606                 ,g_xface_Txn_Ccid_tbl(i)
14607                 ,'N'
14608                 ,'P'
14609                 ,'Expenditures'
14610                 ,'Project Accounting'
14611                 ,g_xface_org_id_tbl(i)
14612                 ,l_po_line_id
14613 		,'EXP'
14614 		,X_Ei_Id
14615 		,1
14616 		,g_xface_Txn_interface_tbl(i)
14617           From  dual;
14618 
14619          IF PG_DEBUG = 'Y' THEN
14620             log_message('log_message: ' || 'In insert_po_bc_packets_auto - Inserted '||SQL%ROWCOUNT||' ACTUAL raw line(s) into bc packets');
14621          END IF;
14622 
14623        END IF;
14624 
14625  COMMIT;
14626  IF PG_DEBUG = 'Y' THEN
14627      log_message('log_message: ' || 'In insert_po_bc_packets_auto - End');
14628  END IF;
14629 
14630 EXCEPTION
14631 WHEN OTHERS THEN
14632    IF PG_DEBUG = 'Y' THEN
14633       log_message('log_message: insert_po_bc_packets_auto ' || sqlerrm||' Returning from insert_po_bc_packets_auto');
14634    END IF;
14635    RAISE;
14636 END insert_po_bc_packets_auto;
14637 
14638 -- BUG: 4600792 PQE:R12 CHANGE AWARD END WHEN ENCUMBRANCE EXISTS, IMPORT ENC REVERSALS FOR CLOSE
14639 --
14640 /* Added the following  procedure for the bug 4138033 */
14641 PROCEDURE  Set_GVal_ProjTskEi_Date(L_Validate_Proj_Tsk_Ei_Date   IN VARCHAR2) IS
14642 BEGIN
14643      G_Validate_Proj_Tsk_Ei_Date := L_Validate_Proj_Tsk_Ei_Date;
14644 END Set_GVal_ProjTskEi_Date;
14645 
14646 /* Added the following function for the bug 4138033 */
14647 FUNCTION   Get_GVal_ProjTskEi_Date RETURN VARCHAR2 IS
14648 BEGIN
14649      Return G_Validate_Proj_Tsk_Ei_Date;
14650 END;
14651 
14652 END PA_TRX_IMPORT;