DBA Data[Home] [Help]

PACKAGE BODY: APPS.AP_ETAX_UTILITY_PKG

Source


1 PACKAGE BODY AP_ETAX_UTILITY_PKG AS
2 /* $Header: apetxutb.pls 120.102.12010000.16 2008/12/12 01:03:27 hchaudha ship $*/
3   -- Create global variables to maintain the session info
4   l_user_id		ap_invoices_all.created_by%TYPE 	:= FND_GLOBAL.user_id;
5   l_login_id		ap_invoices_all.last_update_login%TYPE 	:= FND_GLOBAL.login_id;
6   l_sysdate		DATE := sysdate;
7 
8   Global_Exception      EXCEPTION; -- bug 7126676
9 
10   G_PKG_NAME          CONSTANT VARCHAR2(30) := 'AP_ETAX_UTILITY_PKG';
11   G_MSG_UERROR        CONSTANT NUMBER       := FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR;
12   G_MSG_ERROR         CONSTANT NUMBER       := FND_MSG_PUB.G_MSG_LVL_ERROR;
13   G_MSG_SUCCESS       CONSTANT NUMBER       := FND_MSG_PUB.G_MSG_LVL_SUCCESS;
14   G_MSG_HIGH          CONSTANT NUMBER       := FND_MSG_PUB.G_MSG_LVL_DEBUG_HIGH;
15   G_MSG_MEDIUM        CONSTANT NUMBER       := FND_MSG_PUB.G_MSG_LVL_DEBUG_MEDIUM;
16   G_MSG_LOW           CONSTANT NUMBER       := FND_MSG_PUB.G_MSG_LVL_DEBUG_LOW;
17   G_LINES_PER_FETCH   CONSTANT NUMBER       := 1000;
18 
19   G_CURRENT_RUNTIME_LEVEL CONSTANT NUMBER       := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
20   G_LEVEL_UNEXPECTED      CONSTANT NUMBER       := FND_LOG.LEVEL_UNEXPECTED;
21   G_LEVEL_ERROR           CONSTANT NUMBER       := FND_LOG.LEVEL_ERROR;
22   G_LEVEL_EXCEPTION       CONSTANT NUMBER       := FND_LOG.LEVEL_EXCEPTION;
23   G_LEVEL_EVENT           CONSTANT NUMBER       := FND_LOG.LEVEL_EVENT;
24   G_LEVEL_PROCEDURE       CONSTANT NUMBER       := FND_LOG.LEVEL_PROCEDURE;
25   G_LEVEL_STATEMENT       CONSTANT NUMBER       := FND_LOG.LEVEL_STATEMENT;
26   G_MODULE_NAME           CONSTANT VARCHAR2(30) := 'AP.PLSQL.AP_ETAX_UTILITY_PKG.';
27 
28   -- This record type is used for ap_invoice_distributions_all and ap_self_assessed_tax_dist_all
29 
30   TYPE r_ins_tax_dist_info IS RECORD (
31       accounting_date 			ap_invoice_distributions_all.accounting_date%TYPE,
32       dist_code_combination_id		ap_invoice_distributions_all.dist_code_combination_id%TYPE,
33       line_type_lookup_code		ap_invoice_distributions_all.line_type_lookup_code%TYPE,
34       period_name			ap_invoice_distributions_all.period_name%TYPE,
35       amount				ap_invoice_distributions_all.amount%TYPE,
36       base_amount			ap_invoice_distributions_all.base_amount%TYPE,
37       description			ap_invoice_distributions_all.description%TYPE,
38       income_tax_region			ap_invoice_distributions_all.income_tax_region%TYPE,
39       po_distribution_id		ap_invoice_distributions_all.po_distribution_id%TYPE,
40       type_1099 			ap_invoice_distributions_all.type_1099%TYPE,
41       attribute1 			ap_invoice_distributions_all.attribute1%TYPE,
42       attribute10 			ap_invoice_distributions_all.attribute10%TYPE,
43       attribute11 			ap_invoice_distributions_all.attribute11%TYPE,
44       attribute12 			ap_invoice_distributions_all.attribute12%TYPE,
45       attribute13 			ap_invoice_distributions_all.attribute13%TYPE,
46       attribute14 			ap_invoice_distributions_all.attribute14%TYPE,
47       attribute15 			ap_invoice_distributions_all.attribute15%TYPE,
48       attribute2 			ap_invoice_distributions_all.attribute2%TYPE,
49       attribute3 			ap_invoice_distributions_all.attribute3%TYPE,
50       attribute4 			ap_invoice_distributions_all.attribute4%TYPE,
51       attribute5 			ap_invoice_distributions_all.attribute5%TYPE,
52       attribute6 			ap_invoice_distributions_all.attribute6%TYPE,
53       attribute7 			ap_invoice_distributions_all.attribute7%TYPE,
54       attribute8 			ap_invoice_distributions_all.attribute8%TYPE,
55       attribute9 			ap_invoice_distributions_all.attribute9%TYPE,
56       attribute_category 		ap_invoice_distributions_all.attribute_category%TYPE,
57       expenditure_item_date		ap_invoice_distributions_all.expenditure_item_date%TYPE,
58       expenditure_organization_id	ap_invoice_distributions_all.expenditure_organization_id%TYPE,
59       expenditure_type 			ap_invoice_distributions_all.expenditure_type%TYPE,
60       parent_invoice_id 		ap_invoice_distributions_all.parent_invoice_id%TYPE,
61       pa_addition_flag 			ap_invoice_distributions_all.pa_addition_flag%TYPE,
62       pa_quantity 			ap_invoice_distributions_all.pa_quantity%TYPE,
63       project_accounting_context	ap_invoice_distributions_all.project_accounting_context%TYPE,
64       project_id			ap_invoice_distributions_all.project_id%TYPE,
65       task_id 				ap_invoice_distributions_all.task_id%TYPE,
66       awt_group_id 			ap_invoice_distributions_all.awt_group_id%TYPE,
67       global_attribute_category		ap_invoice_distributions_all.global_attribute_category%TYPE,
68       global_attribute1 		ap_invoice_distributions_all.global_attribute1%TYPE,
69       global_attribute2 		ap_invoice_distributions_all.global_attribute2%TYPE,
70       global_attribute3 		ap_invoice_distributions_all.global_attribute3%TYPE,
71       global_attribute4 		ap_invoice_distributions_all.global_attribute4%TYPE,
72       global_attribute5 		ap_invoice_distributions_all.global_attribute5%TYPE,
73       global_attribute6 		ap_invoice_distributions_all.global_attribute6%TYPE,
74       global_attribute7 		ap_invoice_distributions_all.global_attribute7%TYPE,
75       global_attribute8 		ap_invoice_distributions_all.global_attribute8%TYPE,
76       global_attribute9 		ap_invoice_distributions_all.global_attribute9%TYPE,
77       global_attribute10 		ap_invoice_distributions_all.global_attribute10%TYPE,
78       global_attribute11 		ap_invoice_distributions_all.global_attribute11%TYPE,
79       global_attribute12 		ap_invoice_distributions_all.global_attribute12%TYPE,
80       global_attribute13 		ap_invoice_distributions_all.global_attribute13%TYPE,
81       global_attribute14 		ap_invoice_distributions_all.global_attribute14%TYPE,
82       global_attribute15 		ap_invoice_distributions_all.global_attribute15%TYPE,
83       global_attribute16 		ap_invoice_distributions_all.global_attribute16%TYPE,
84       global_attribute17 		ap_invoice_distributions_all.global_attribute17%TYPE,
85       global_attribute18 		ap_invoice_distributions_all.global_attribute18%TYPE,
86       global_attribute19 		ap_invoice_distributions_all.global_attribute19%TYPE,
87       global_attribute20 		ap_invoice_distributions_all.global_attribute20%TYPE,
88       award_id 				ap_invoice_distributions_all.award_id%TYPE,
89       dist_match_type 			ap_invoice_distributions_all.dist_match_type%TYPE,
90       rcv_transaction_id 		ap_invoice_distributions_all.rcv_transaction_id%TYPE,
91       tax_recoverable_flag		ap_invoice_distributions_all.tax_recoverable_flag%TYPE,
92       cancellation_flag 		ap_invoice_distributions_all.cancellation_flag%TYPE,
93       invoice_line_number		ap_invoice_distributions_all.invoice_line_number%TYPE,
94       corrected_invoice_dist_id		ap_invoice_distributions_all.corrected_invoice_dist_id%TYPE,
95       rounding_amt			ap_invoice_distributions_all.rounding_amt%TYPE,
96       charge_applicable_to_dist_id	ap_invoice_distributions_all.charge_applicable_to_dist_id%TYPE,
97       distribution_class 		ap_invoice_distributions_all.distribution_class%TYPE,
98       tax_code_id			ap_invoice_distributions_all.tax_code_id%TYPE,
99       detail_tax_dist_id 		ap_invoice_distributions_all.detail_tax_dist_id%TYPE,
100       rec_nrec_rate 			ap_invoice_distributions_all.rec_nrec_rate%TYPE,
101       recovery_rate_id 			ap_invoice_distributions_all.recovery_rate_id%TYPE,
102       recovery_rate_name 		ap_invoice_distributions_all.recovery_rate_name%TYPE,
103       recovery_type_code 		ap_invoice_distributions_all.recovery_type_code%TYPE,
104       summary_tax_line_id		ap_invoice_distributions_all.summary_tax_line_id%TYPE,
105       extra_po_erv 			ap_invoice_distributions_all.extra_po_erv%TYPE,
106       taxable_amount 			ap_invoice_distributions_all.taxable_amount%TYPE,
107       taxable_base_amount 		ap_invoice_distributions_all.taxable_base_amount%TYPE,
108       accrue_on_receipt_flag 		po_distributions_all.accrue_on_receipt_flag%TYPE,
109       allow_flex_override_flag		ap_system_parameters_all.allow_flex_override_flag%TYPE,
110       purch_encumbrance_flag		financials_system_params_all.purch_encumbrance_flag%TYPE,
111       org_id 				ap_invoice_distributions_all.org_id%TYPE,
112       tax_regime_id 			zx_rec_nrec_dist.tax_regime_id%TYPE,
113       tax_id 				zx_rec_nrec_dist.tax_id%TYPE,
114       tax_status_id 			zx_rec_nrec_dist.tax_status_id%TYPE,
115       tax_jurisdiction_id 		zx_lines.tax_jurisdiction_id%TYPE,
116       parent_dist_cancellation_flag	ap_invoice_distributions_all.cancellation_flag%TYPE,
117       parent_dist_reversal_flag		ap_invoice_distributions_all.reversal_flag%TYPE,
118       parent_dist_parent_reversal_id	ap_invoice_distributions_all.parent_reversal_id%TYPE,
119       reversed_tax_dist_id 		zx_rec_nrec_dist.reversed_tax_dist_id%TYPE,
120       adjusted_doc_tax_dist_id 		zx_rec_nrec_dist.adjusted_doc_tax_dist_id%TYPE,
121       applied_from_tax_dist_id 		zx_rec_nrec_dist.applied_from_tax_dist_id%TYPE,
122       prepay_distribution_id		ap_invoice_distributions_all.prepay_distribution_id%TYPE,
123       prepay_tax_diff_amount	        ap_invoice_distributions_all.prepay_tax_diff_amount%TYPE,
124       invoice_id			ap_invoice_distributions_all.invoice_id%TYPE,
125       batch_id				ap_invoice_distributions_all.batch_id%TYPE,
126       set_of_books_id			ap_invoice_distributions_all.set_of_books_id%TYPE,
127       account_source_tax_rate_id	zx_rec_nrec_dist.account_source_tax_rate_id%TYPE
128       );
129 
130   PROCEDURE insert_tax_distributions
131   			(p_invoice_header_rec        IN ap_invoices_all%ROWTYPE,
132 			 p_inv_dist_rec              IN r_ins_tax_dist_info,
133 			 p_dist_code_combination_id  IN NUMBER,
134 			 p_user_id	             IN NUMBER,
135 			 p_sysdate	             IN DATE,
136 			 p_login_id	             IN	NUMBER,
137 			 p_calling_sequence          IN VARCHAR2);
138 
139 /*=============================================================================
140  |  FUNCTION - Get_Event_Class_Code()
141  |
142  |  DESCRIPTION
143  |      Public function that will get the event class code required to call
144  |      the eTax services based on the invoice type.  These event class code is
145  |      AP specific.  eTax will convert this event class code to the tax class
146  |      code used by eTax.
147  |
148  |  PARAMETERS
149  |      P_Invoice_Type_Lookup_Code - Invoice Type
150  |      P_Event_Class_Code - event class code
151  |      P_error_code - Error code to be returned
152  |      P_calling_sequence -  Calling sequence
153  |
154  |  MODIFICATION HISTORY
155  |    DATE          Author         Action
156  |    08-OCT-2003   SYIDNER        Created
157  |
158  *============================================================================*/
159   FUNCTION Get_Event_Class_Code(
160              P_Invoice_Type_Lookup_Code    IN VARCHAR2,
161              P_Event_Class_Code            OUT NOCOPY VARCHAR2,
162              P_Error_Code                  OUT NOCOPY VARCHAR2,
163              P_Calling_Sequence            IN VARCHAR2) RETURN BOOLEAN
164   IS
165 
166     l_debug_info                 VARCHAR2(240);
167     l_curr_calling_sequence      VARCHAR2(4000);
168     l_return_var                 BOOLEAN := TRUE;
169 
170   BEGIN
171 
172     l_curr_calling_sequence := 'AP_ETAX_UTILITY_PKG.Get_Event_Class_Code<-' ||
173                                P_calling_sequence;
174 
175     -- The following invoice types are not included to get an event class due to:
176     -- AWT and INTEREST: Withholding Tax and Interest invoices do not require
177     --                   tax calculation.
178     -- QUICKDEFAULT and QUICKMATCH: These defined types for invoices are only
179     --                              entry mechanisms.  When the invoice is commit
180     --                              the type is STANDARD.
181 
182     IF (P_Invoice_Type_Lookup_Code IN ('STANDARD',
183                                        'CREDIT',
184                                        'DEBIT',
185                                        'MIXED',
186                                        'ADJUSTMENT',
187                                        'PO PRICE ADJUST',
188 				       'INVOICE REQUEST',
189 				       'CREDIT MEMO REQUEST',
190 				       'RETAINAGE RELEASE',
191                                        'PAYMENT REQUEST')) THEN   -- for bug 5948586
192 
193       ------------------------------------------------------------------
194       l_debug_info := 'Step 1: Event Class Code is STANDARD INVOICES';
195       ------------------------------------------------------------------
196       P_Event_Class_Code := 'STANDARD INVOICES';
197 
198 
199     ELSIF (P_Invoice_Type_Lookup_Code IN ('PREPAYMENT')) THEN
200 
201       ------------------------------------------------------------------
202       l_debug_info := 'Step 2: Event Class Code is PREPAYMENT INVOICES';
203       ------------------------------------------------------------------
204       P_Event_Class_Code := 'PREPAYMENT INVOICES';
205 
206 
207     ELSIF (P_Invoice_Type_Lookup_Code IN ('EXPENSE REPORT')) THEN
208 
209       ------------------------------------------------------------------
210       l_debug_info := 'Step 3: Event Class Code is EXPENSE REPORTS';
211       ------------------------------------------------------------------
212       P_Event_Class_Code := 'EXPENSE REPORTS';
213 
214     ELSE
215 
216       ------------------------------------------------------------------
217       l_debug_info := 'Step 4: Event Class Code is NULL and function '||
218                       'returns FALSE because eTax is not defined to be '||
219                       'called for this Invoice type ';
220       ------------------------------------------------------------------
221       P_Event_Class_Code := NULL;
222       l_return_var := FALSE;
223 
224     END IF;
225 
226     RETURN l_return_var;
227 
228   EXCEPTION
229     WHEN OTHERS THEN
230       IF (SQLCODE <> -20001) THEN
231         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
232         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
233         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
234         FND_MESSAGE.SET_TOKEN('PARAMETERS',
235           ' P_Invoice_Type_Lookup_Code = '||P_Invoice_Type_Lookup_Code||
236           ' P_Error_Code = '||P_Error_Code||
237           ' P_Calling_Sequence = '||P_Calling_Sequence);
238         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
239       END IF;
240 
241       APP_EXCEPTION.RAISE_EXCEPTION;
242 
243   END Get_Event_Class_Code;
244 
245 
246 /*=============================================================================
247  |  FUNCTION - Get_Event_Type_Code()
248  |
249  |  DESCRIPTION
250  |      Public function that will get the event type code required to call
251  |      eTax services based on the event class code, calling_mode and if
252  |      eTax was already called or not.
253  |
254  |  PARAMETERS
255  |      P_Event_Class_Code - Event class code
256  |      P_Calling_Mode - Calling mode
257  |      P_eTax_Already_called_flag - Is eTax already called?
258  |      P_Event_Type_Code - event type code
259  |      P_error_code - Error code to be returned
260  |      P_calling_sequence -  Calling sequence
261  |
262  |  MODIFICATION HISTORY
263  |    DATE          Author         Action
264  |    09-OCT-2003   SYIDNER        Created
265  |
266  *============================================================================*/
267 
268   FUNCTION Get_Event_Type_Code(
269              P_Event_Class_Code            IN VARCHAR2,
270              P_Calling_Mode                IN VARCHAR2,
271              P_eTax_Already_called_flag    IN VARCHAR2,
272              P_Event_Type_Code             OUT NOCOPY VARCHAR2,
273              P_Error_Code                  OUT NOCOPY VARCHAR2,
274              P_Calling_Sequence            IN VARCHAR2) RETURN BOOLEAN
275 
276   IS
277 
278     l_debug_info                 VARCHAR2(240);
279     l_curr_calling_sequence      VARCHAR2(4000);
280     l_class_section              VARCHAR2(500);
281     l_action_section             VARCHAR2(500);
282 
283   BEGIN
284 
285     l_curr_calling_sequence := 'AP_ETAX_UTILITY_PKG.Get_Event_Type_Code<-' ||
286                                P_calling_sequence;
287 
288     --  There are 3 different event classes defined for AP to handle tax in 11ix.
289     --  They are: STANDARD INVOICES, PREPAYMENT INVOICES and EXPENSE REPORTS.
290     --  There are different event type codes per event type and event class due to
291     --  a requirement of SLA.  The event type code should be unique per product.
292     --  So, we can say the event type code is composed of 2 sections:
293     --  The class section (STANDARD, PREPAYMENT or EXPENSE REPORT) and the
294     --  action section that determines the action ocurred to the class
295     --  (CREATE, UPDATE, CANCELLED, FROZEN, etc).
296 
297     ------------------------------------------------------------------
298     l_debug_info := 'Step 1: Event Class Code is: '||p_event_class_code;
299     ------------------------------------------------------------------
300 
301     IF ( P_Event_Class_Code = 'STANDARD INVOICES' ) THEN
302       l_class_section := 'STANDARD ';
303 
304     ELSIF ( P_Event_Class_Code = 'PREPAYMENT INVOICES' ) THEN
305       l_class_section := 'PREPAYMENT ';
306 
307     ELSIF ( P_Event_Class_Code = 'EXPENSE REPORTS' ) THEN
308       l_class_section := 'EXPENSE REPORT ';
309 
310     END IF;
311 
312     ------------------------------------------------------------------
313     l_debug_info := 'Step 2: Calling mode is: '||p_calling_mode;
314     ------------------------------------------------------------------
315     IF ( p_calling_mode IN
316          ('CALCULATE', 'CALCULATE QUOTE', 'APPLY PREPAY',
317           'UNAPPLY PREPAY', 'RECOUPMENT')) THEN
318       IF ( P_eTax_Already_called_flag = 'N' ) THEN
319         l_action_section := 'CREATED';
320       ELSE
321         l_action_section := 'UPDATED';
322 
323       END IF;
324 
325     ELSIF ( p_calling_mode IN ('CALCULATE IMPORT', 'VALIDATE IMPORT',
326                                'IMPORT INTERFACE')) THEN
327       l_action_section := 'CREATED';
328 
329     ELSIF ( p_calling_mode IN ('DISTRIBUTE', 'DISTRIBUTE RECOUP') ) THEN
330       IF ( P_eTax_Already_called_flag = 'N' ) THEN
331          l_action_section := 'DISTRIBUTE';
332 
333       ELSE
334          l_action_section := 'REDISTRIBUTE';
335 
336       END IF;
337 
338     ELSIF ( p_calling_mode = 'DISTRIBUTE IMPORT' ) THEN
339       l_action_section := 'DISTRIBUTE';
340 
341     ELSIF ( p_calling_mode = 'REVERSE INVOICE' ) THEN
342       l_action_section := 'FULLY REVERSED';
343 
344     ELSIF ( p_calling_mode = 'OVERRIDE TAX' ) THEN
345       l_action_section := 'OVERRIDE TAX';
346 
347     ELSIF ( p_calling_mode = 'OVERRIDE RECOVERY' ) THEN
348       l_action_section := 'OVERRIDE DIST';
349 
350     ELSIF ( p_calling_mode = 'CANCEL INVOICE' ) THEN
351       l_action_section := 'CANCELLED TAX';
352 
353     ELSIF ( p_calling_mode = 'FREEZE INVOICE' ) THEN
354       l_action_section := 'FROZEN';
355 
356     ELSIF ( p_calling_mode = 'UNFREEZE INVOICE' ) THEN
357       l_action_section := 'UNFROZEN';
358 
359     ELSIF ( p_calling_mode = 'RELEASE TAX HOLDS' ) THEN
360       l_action_section := 'HOLDS RELEASED';
361 
362     ELSIF ( p_calling_mode
363               IN ('MARK TAX LINES DELETED', 'FREEZE DISTRIBUTIONS')) THEN
364       l_action_section := 'UPDATED';
365 
366     ELSIF ( p_calling_mode = 'VALIDATE' ) THEN
367       l_action_section := 'VALIDATED TAX';
368 
369     /*Introduced this condition for the bug 7388641 */
370     ELSIF ( p_calling_mode = 'DELETE INVOICE' ) THEN
371       l_action_section := 'PURGED';
372 
373     END IF;
374 
375     -- Construct the code from the class and action sections
376     p_event_type_code := l_class_section||l_action_section;
377 
378     RETURN(TRUE);
379 
380   EXCEPTION
381     WHEN OTHERS THEN
382       IF (SQLCODE <> -20001) THEN
383         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
384         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
385         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
386         FND_MESSAGE.SET_TOKEN('PARAMETERS',
387           ' P_Event_Class_Code = '||P_Event_Class_Code||
388           ' P_Calling_Mode ='||P_Calling_Mode||
389           ' P_eTax_Already_called_flag ='||P_eTax_Already_called_flag||
390           ' P_Error_Code = '||P_Error_Code||
391           ' P_Calling_Sequence = '||P_Calling_Sequence);
392         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
393       END IF;
394 
395       APP_EXCEPTION.RAISE_EXCEPTION;
396 
397   END Get_Event_Type_Code;
398 
399 /*=============================================================================
400  |  FUNCTION - Get_Corrected_Invoice_Info()
401  |
402  |  DESCRIPTION
403  |      This function return the additional information required to populate
404  |      the zx_transaction_lines_gt global temporary table for eTax.
405  |
406  |  PARAMETERS
407  |      P_Corrected_Invoice_Id - Invoice Id for the corrected line
408  |      P_corrected_Line_number - Line number for the corrected line
409  |      P_Application_Id - Application Id for the corrected invoice
410  |      P_Entity_code - entity code required for the event class
411  |      P_Event_Class_Code - Event class code for the corrected invoice
412  |      P_Invoice_Number - Corrected invoice number
413  |      P_Invoice_Date - corrected invoice date
414  |      P_error_code - Error code to be returned
415  |      P_calling_sequence -  Calling sequence
416  |
417  |  MODIFICATION HISTORY
418  |    DATE          Author         Action
419  |    13-OCT-2003   SYIDNER        Created
420  |
421  *============================================================================*/
422   FUNCTION Get_Corrected_Invoice_Info(
423              P_Corrected_Invoice_Id        IN NUMBER,
424              P_Corrected_Line_Number       IN NUMBER,
425              P_Application_Id              OUT NOCOPY NUMBER,
426              P_Entity_code                 OUT NOCOPY VARCHAR2,
427              P_Event_Class_Code            OUT NOCOPY VARCHAR2,
428              P_Invoice_Number              OUT NOCOPY VARCHAR2,
429              P_Invoice_Date                OUT NOCOPY DATE,
430              P_Error_Code                  OUT NOCOPY VARCHAR2,
431              P_Calling_Sequence            IN VARCHAR2) RETURN BOOLEAN
432 
433   IS
434 
435     l_debug_info                 VARCHAR2(240);
436     l_curr_calling_sequence      VARCHAR2(4000);
437     l_corrected_invoice_type     ap_invoices_all.invoice_type_lookup_code%TYPE;
438 
439 
440     CURSOR corrected_inv( c_corrected_inv_id IN NUMBER) IS
441     SELECT invoice_num, invoice_date, invoice_type_lookup_code
442       FROM ap_invoices_all
443      WHERE invoice_id = c_corrected_inv_id;
444 
445   BEGIN
446 
447     l_curr_calling_sequence := 'AP_ETAX_UTILITY_PKG.Get_Corrected_Invoice_Info<-'||
448                                P_calling_sequence;
449 
450     -------------------------------------------------------------------
451     l_debug_info := 'Step 1: Get corrected invoice info';
452     -------------------------------------------------------------------
453     IF ( P_Corrected_Invoice_Id IS NOT NULL) THEN
454 
455       P_Application_Id := 200; -- Oracle Payables
456       P_Entity_code := 'AP_INVOICES';
457 
458       BEGIN
459         OPEN corrected_inv (P_Corrected_Invoice_Id);
460         FETCH corrected_inv
461           INTO P_Invoice_Number, P_Invoice_Date,
462                l_corrected_invoice_type;
463         CLOSE corrected_inv;
464       END ;
465 
466       --------------------------------------------------------------------------
467       l_debug_info := 'Step 2: Get event class code corrected_invoice_id';
468       --------------------------------------------------------------------------
469       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
470         P_Invoice_Type_Lookup_Code => l_corrected_invoice_type,
471         P_Event_Class_Code         => P_Event_Class_Code,
472         P_error_code               => P_error_code,
473         P_calling_sequence         => l_curr_calling_sequence)) THEN
474 
475         RETURN FALSE;
476       END IF;
477 
478     END IF;
479     RETURN TRUE;
480 
481   EXCEPTION
482     WHEN OTHERS THEN
483       IF (SQLCODE <> -20001) THEN
484         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
485         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
486         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
487         FND_MESSAGE.SET_TOKEN('PARAMETERS',
488           ' P_Corrected_Invoice_Id = '||P_Corrected_Invoice_Id||
489           ' P_Corrected_Line_Number = '||P_Corrected_Line_Number||
490           ' P_Error_Code = '||P_Error_Code||
491           ' P_Calling_Sequence = '||P_Calling_Sequence);
492         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
493       END IF;
494       APP_EXCEPTION.RAISE_EXCEPTION;
495 
496   END Get_Corrected_Invoice_Info;
497 
498 /*=============================================================================
499  |  FUNCTION - Get_Prepay_Invoice_Info()
500  |
501  |  DESCRIPTION
502  |      This function return the additional information required to populate
503  |      the zx_transaction_lines_gt global temporary table for eTax.
504  |
505  |  PARAMETERS
506  |      P_Prepay_Invoice_Id - Invoice Id for the applied prepay line
507  |      P_Prepay_Line_number - Line number for the applied prepay line
508  |      P_Application_Id - Application Id for the applied prepay invoice
509  |      P_Entity_code - entity code required for the event class
510  |      P_Event_Class_Code - Event class code for the applied prepay invoice
511  |      P_Invoice_Number - Applied Prepayment number
512  |      P_Invoice_Date - Applied prepayment invoice date
513  |      P_error_code - Error code to be returned
514  |      P_calling_sequence -  Calling sequence
515  |
516  |  MODIFICATION HISTORY
517  |    DATE          Author         Action
518  |    15-OCT-2003   SYIDNER        Created
519  |
520  *============================================================================*/
521 
522   FUNCTION Get_Prepay_Invoice_Info(
523              P_Prepay_Invoice_Id        IN  NUMBER,
524              P_Prepay_Line_Number       IN  NUMBER,
525              P_Application_Id           OUT NOCOPY NUMBER,
526              P_Entity_code              OUT NOCOPY VARCHAR2,
527              P_Event_Class_Code         OUT NOCOPY VARCHAR2,
528              P_Invoice_Number           OUT NOCOPY VARCHAR2,
529              P_Invoice_Date             OUT NOCOPY DATE,
530              P_Error_Code               OUT NOCOPY VARCHAR2,
531              P_Calling_Sequence         IN  VARCHAR2) RETURN BOOLEAN
532   IS
533 
534     l_debug_info                 VARCHAR2(240);
535     l_curr_calling_sequence      VARCHAR2(4000);
536     l_prepay_invoice_type        ap_invoices_all.invoice_type_lookup_code%TYPE;
537 
538     CURSOR prepay_inv (c_prepay_inv_id IN NUMBER) IS
539     SELECT invoice_num, invoice_date, invoice_type_lookup_code
540       FROM ap_invoices_all
541      WHERE invoice_id = c_prepay_inv_id;
542 
543   BEGIN
544 
545     l_curr_calling_sequence := 'AP_ETAX_UTILITY_PKG.Get_Prepay_Invoice_Info<-' ||
546                                P_calling_sequence;
547 
548     -------------------------------------------------------------------
549     l_debug_info := 'Step 1: Get applied prepayment invoice info';
550     -------------------------------------------------------------------
551     IF ( P_Prepay_Invoice_Id IS NOT NULL) THEN
552 
553       P_Application_Id := 200;
554       P_Entity_code    := 'AP_INVOICES';
555 
556       BEGIN
557         OPEN  prepay_inv (P_Prepay_Invoice_Id);
558         FETCH prepay_inv
559          INTO P_Invoice_Number, P_Invoice_Date,
560               l_prepay_invoice_type;
561         CLOSE prepay_inv;
562       END ;
563 
564       --------------------------------------------------------------------------
565       l_debug_info := 'Step 2: Get event class code prepay_invoice_id';
566       --------------------------------------------------------------------------
567       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
568         P_Invoice_Type_Lookup_Code => l_prepay_invoice_type,
569         P_Event_Class_Code         => P_Event_Class_Code,
570         P_error_code               => P_error_code,
571         P_calling_sequence         => l_curr_calling_sequence)) THEN
572 
573         RETURN FALSE;
574       END IF;
575 
576     END IF;
577     RETURN TRUE;
578 
579   EXCEPTION
580     WHEN OTHERS THEN
581       IF (SQLCODE <> -20001) THEN
582         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
583         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
584         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
585         FND_MESSAGE.SET_TOKEN('PARAMETERS',
586           ' P_Prepay_Invoice_Id = '||P_Prepay_Invoice_Id||
587           ' P_Prepay_Line_Number = '||P_Prepay_Line_Number||
588           ' P_Error_Code = '||P_Error_Code||
589           ' P_Calling_Sequence = '||P_Calling_Sequence);
590         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
591       END IF;
592 
593       APP_EXCEPTION.RAISE_EXCEPTION;
594 
595   END Get_Prepay_Invoice_Info;
596 
597 /*=============================================================================
598  |  FUNCTION - Get_Receipt_Info()
599  |
600  |  DESCRIPTION
601  |      This function return the additional information required to populate
602  |      the zx_transaction_lines_gt global temporary table for eTax.
603  |
604  |  PARAMETERS
605  |      P_Rcv_Transaction_Id - Receipt id
606  |      P_Application_Id - Application Id for the Receipt (201)
607  |      P_Entity_code - entity code required for the event class
608  |      P_Event_Class_Code - Event class code for the receipt
609  |      P_Transaction_Date - Transaction date
610  |      P_error_code - Error code to be returned
611  |      P_calling_sequence -  Calling sequence
612  |
613  |  MODIFICATION HISTORY
614  |    DATE          Author         Action
615  |    15-OCT-2003   SYIDNER        Created
616  |
617  *============================================================================*/
618   FUNCTION Get_Receipt_Info(
619              P_Rcv_Transaction_Id          IN  NUMBER,
620              P_Application_Id              OUT NOCOPY NUMBER,
621              P_Entity_code                 OUT NOCOPY VARCHAR2,
622              P_Event_Class_Code            OUT NOCOPY VARCHAR2,
623              P_Transaction_Date            OUT NOCOPY DATE,
624              P_Error_Code                  OUT NOCOPY VARCHAR2,
625              P_Calling_Sequence            IN  VARCHAR2) RETURN BOOLEAN
626   IS
627 
628     l_debug_info                 VARCHAR2(240);
629     l_curr_calling_sequence      VARCHAR2(4000);
630     l_return_status              VARCHAR2(100);
631     l_msg_count                  NUMBER;
632     l_msg_data                   VARCHAR2(2000);
633 
634     l_transaction_type           rcv_transactions.transaction_type%TYPE;
635 
636     CURSOR receipt_info (c_rcv_transaction IN NUMBER) IS
637     SELECT transaction_date, transaction_type
638       FROM rcv_transactions
639      WHERE transaction_id = c_rcv_transaction;
640 
641   BEGIN
642 
643     l_curr_calling_sequence := 'AP_ETAX_UTILITY_PKG.Get_Receipt_Info<-' ||
644                                P_calling_sequence;
645 
646     -------------------------------------------------------------------
647     l_debug_info := 'Step 1: Get receipt info';
648     -------------------------------------------------------------------
649     IF (P_Rcv_Transaction_Id IS NOT NULL) THEN
650 
651       OPEN  receipt_info (P_Rcv_Transaction_Id);
652       FETCH receipt_info
653        INTO P_Transaction_Date, l_transaction_type;
654       CLOSE receipt_info;
655 
656       --------------------------------------------------------------------------
657       l_debug_info := 'Step 2: Call PO API to get additional receipt info';
658       --------------------------------------------------------------------------
659       CST_UTILITY_PUB.Get_Receipt_Event_Info (
660 			  p_api_version      => 1.0 ,
661 			  p_transaction_type => 'MATCH',
662 			  x_return_status    => l_return_status,
663 			  x_msg_count        => l_msg_count,
664 			  x_msg_data         => l_msg_data,
665 			  p_entity_code      => p_entity_code,
666 			  p_application_id   => p_application_id,
667 			  p_event_class_code => p_event_class_code);
668 
669     END IF;
670 
671     RETURN TRUE;
672 
673   EXCEPTION
674     WHEN OTHERS THEN
675       IF (SQLCODE <> -20001) THEN
676         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
677         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
678         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
679         FND_MESSAGE.SET_TOKEN('PARAMETERS',
680           ' P_Rcv_Transaction_Id = '||P_Rcv_Transaction_Id||
681           ' P_Error_Code = '||P_Error_Code||
682           ' P_Calling_Sequence = '||P_Calling_Sequence);
683         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
684       END IF;
685 
686       APP_EXCEPTION.RAISE_EXCEPTION;
687 
688   END Get_Receipt_Info;
689 
690 /*=============================================================================
691  |  FUNCTION - Get_PO_Info()
692  |
693  |  DESCRIPTION
694  |      This function return the additional information required to populate
695  |      the zx_transaction_lines_gt and the zx_distribution_lines_gt global
696  |      temporary tables for eTax.
697  |      The parameters po_line_location_id and po_distribution_id are mutual
698  |      exclusive.
699  |
700  |  PARAMETERS
701  |      P_Po_line_location_id - PO line location
702  |      P_Po_Distribution_id - Po distribution
703  |      P_Application_Id - Application Id for the PO document (201)
704  |      P_Entity_code - entity code required for the event class
705  |      P_Event_Class_Code - Event class code for the PO doc
706  |      P_PO_Quantity - PO quantity
707  |      P_Product_Org_Id - Product Org_id
708  |      P_Po_Header_Id - Po header Id
709  |      P_Po_Header_Curr_Conv_Rate - Po Header currency conversion rate
710  |      P_error_code - Error code to be returned
711  |      P_calling_sequence -  Calling sequence
712  |
713  |  MODIFICATION HISTORY
714  |    DATE          Author         Action
715  |    15-OCT-2003   SYIDNER        Created
716  |    20-OCT-2003   SYIDNER        Included po_distribution as a parameter
717  |                                 and modify the function to return Po data
718  |                                 for the call to the determine_recovery serv
719  *============================================================================*/
720   FUNCTION Get_PO_Info(
721              P_PO_Line_Location_Id         IN  OUT NOCOPY NUMBER,
722              P_Po_Distribution_Id          IN  NUMBER,
723              P_Application_Id              OUT NOCOPY NUMBER,
724              P_Entity_code                 OUT NOCOPY VARCHAR2,
725              P_Event_Class_Code            OUT NOCOPY VARCHAR2,
726              P_PO_Quantity                 OUT NOCOPY NUMBER,
727              P_Product_Org_Id              OUT NOCOPY NUMBER,
728              P_Po_Header_Id                OUT NOCOPY NUMBER,
729              P_Po_Header_Curr_Conv_Rate    OUT NOCOPY NUMBER,
730 	     P_Uom_Code			   OUT NOCOPY VARCHAR2,
731 	     P_Dist_Qty			   OUT NOCOPY NUMBER,
732 	     P_Ship_Price		   OUT NOCOPY NUMBER,
733              P_Error_Code                  OUT NOCOPY VARCHAR2,
734              P_Calling_Sequence            IN  VARCHAR2) RETURN BOOLEAN
735 
736   IS
737 
738     l_debug_info                 VARCHAR2(240);
739     l_curr_calling_sequence      VARCHAR2(4000);
740     l_return_status 		 VARCHAR2(100);
741     l_msg_count			 NUMBER;
742     l_msg_data 			 VARCHAR2(2000);
743 
744     l_org_id			 po_line_locations_all.org_id%type;
745     l_po_release_id		 po_line_locations_all.po_release_id%type;
746     l_doc_type			 VARCHAR2(20);
747 
748     CURSOR PO_Info_from_line_loc (c_po_line_location_Id IN NUMBER) IS
749     SELECT pll.org_id, pll.quantity, pll.po_header_id, nvl(ph.rate ,1), pll.po_release_id, mum.uom_code
750       FROM po_line_locations_all pll, po_headers_all ph, mtl_units_of_measure mum
751      WHERE pll.line_location_id = c_po_line_location_Id
752        AND pll.po_header_id = ph.po_header_id
753        AND pll.unit_meas_lookup_code = mum.unit_of_measure (+);
754 
755     CURSOR PO_Info_from_dist (c_po_dist_Id IN NUMBER) IS
756     SELECT pll.org_id, pll.quantity, pll.line_location_id,
757            pll.po_header_id, nvl(ph.rate,1), pll.po_release_id, mum.uom_code,
758 	   pd.quantity_ordered, pll.price_override
759       FROM po_line_locations_all pll, po_distributions_all pd,
760            po_headers_all ph, mtl_units_of_measure mum
761      WHERE pd.po_distribution_id = c_po_dist_Id
762        AND pd.line_location_id = pll.line_location_id
763        AND pll.po_header_id = ph.po_header_id
764        AND pll.unit_meas_lookup_code = mum.unit_of_measure (+);
765 
766   BEGIN
767 
768     l_curr_calling_sequence := 'AP_ETAX_UTILITY_PKG.Get_PO_Info<-' ||
769                                P_calling_sequence;
770 
771     -------------------------------------------------------------------
772     l_debug_info := 'Step 1: Get PO additional info';
773     -------------------------------------------------------------------
774 
775     IF ( P_PO_Line_Location_Id IS NOT NULL) THEN
776         OPEN  PO_info_from_line_loc (P_PO_Line_Location_Id);
777         FETCH PO_info_from_line_loc
778          INTO l_org_Id, P_PO_Quantity,
779               P_Po_Header_Id, P_Po_header_curr_conv_rate, l_po_release_id, p_uom_code;
780         CLOSE PO_info_from_line_loc;
781 
782     ELSIF ( P_PO_Distribution_Id IS NOT NULL) THEN
783         OPEN  PO_info_from_dist (P_PO_Distribution_Id);
784         FETCH PO_info_from_dist
785          INTO l_org_id, P_PO_Quantity, P_PO_Line_Location_Id,
786               P_Po_Header_Id, P_Po_header_curr_conv_rate, l_po_release_id, p_uom_code,
787 	      P_Dist_Qty, P_Ship_Price;
788         CLOSE PO_info_from_dist;
789 
790     END IF;
791 
792     IF NOT AP_ETAX_SERVICES_PKG.g_fsp_attributes.exists(l_org_id) THEN
793 
794        SELECT inventory_organization_id
795          INTO AP_ETAX_SERVICES_PKG.g_fsp_attributes(l_org_id).inventory_organization_id
796          FROM financials_system_params_all
797         WHERE org_id = l_org_id;
798 
799     END IF;
800 
801     p_product_org_id := AP_ETAX_SERVICES_PKG.g_fsp_attributes(l_org_id).inventory_organization_id;
802 
803 
804     -- Bug 5193570
805     -- Set ref_doc_trx_id to po_release_id when matched to a Release.
806 
807     IF l_po_release_id IS NOT NULL THEN
808 	p_po_header_id := l_po_release_id;
809 	l_doc_type     := 'RELEASE';
810     ELSE
811         l_doc_type     := 'PO';
812     END IF;
813 
814     -------------------------------------------------------------------
815     l_debug_info := 'Step 1: Calling PO API to get PO etax document '||
816                     ' setup info';
817     -------------------------------------------------------------------
818     PO_TAX_INTERFACE_GRP.get_document_tax_constants(
819     		p_api_version 		=> 1.0,
820 		p_init_msg_list		=> NULL,
821 		p_commit		=> 'N',
822 		p_validation_level	=> NULL,
823 		x_return_status		=> l_return_status,
824 		x_msg_count		=> l_msg_count,
825 		x_msg_data		=> l_msg_data,
826 		p_doc_type		=> l_doc_type,
827 		x_application_id	=> p_application_id,
828 		x_entity_code		=> p_entity_code,
829 		x_event_class_code	=> p_event_class_code );
830 
831     RETURN TRUE;
832 
833   EXCEPTION
834     WHEN OTHERS THEN
835       IF (SQLCODE <> -20001) THEN
836         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
837         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
838         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
839         FND_MESSAGE.SET_TOKEN('PARAMETERS',
840           ' P_PO_Line_Location_Id = '||P_PO_Line_Location_Id||
841           ' P_Error_Code = '||P_Error_Code||
842           ' P_Calling_Sequence = '||P_Calling_Sequence);
843         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
844       END IF;
845 
846       APP_EXCEPTION.RAISE_EXCEPTION;
847 
848   END Get_PO_Info;
849 
850 /*=============================================================================
851  |  FUNCTION - Get_Prepay_Awt_Group_Id()
852  |
853  |  DESCRIPTION
854  |      This function return the awt_group_id for a parent prepay item line
855  |      based on the prepayment distribution id.
856  |
857  |  PARAMETERS
858  |      P_Prepay_Distribution_id - Distribution Id of the prepayment
859  |
860  |  MODIFICATION HISTORY
861  |    DATE          Author         Action
862  |    15-OCT-2003   SYIDNER        Created
863  |
864  *============================================================================*/
865   FUNCTION Get_Prepay_Awt_Group_Id(
866              P_Prepay_Distribution_id    IN NUMBER,
867              P_Calling_Sequence          IN VARCHAR2) RETURN NUMBER
868   IS
869 
870     l_debug_info                 VARCHAR2(240);
871     l_curr_calling_sequence      VARCHAR2(4000);
872 
873     CURSOR prepay_awt_group_id (c_prepay_dist_id IN NUMBER) IS
874     SELECT ail.awt_group_id
875       FROM ap_invoice_distributions_all aid,
876            ap_invoice_lines_all ail
877      WHERE aid.invoice_distribution_id = c_prepay_dist_id
878        AND aid.invoice_id = ail.invoice_id
879        AND aid.invoice_line_number = ail.line_number;
880 
881     l_awt_group_id     ap_invoice_lines_all.awt_group_id%TYPE;
882 
883   BEGIN
884 
885     l_curr_calling_sequence := 'AP_ETAX_UTILITY_PKG.Get_Prepay_Awt_Group_Id<-'||
886                                P_calling_sequence;
887 
888     -------------------------------------------------------------------
889     l_debug_info := 'Step 1: Get awt_group_id from prepay item line';
890     -------------------------------------------------------------------
891     OPEN  prepay_awt_group_id( P_Prepay_Distribution_id);
892     FETCH prepay_awt_group_id
893     INTO  l_awt_group_id;
894     CLOSE prepay_awt_group_id;
895 
896     RETURN l_awt_group_id;
897 
898   EXCEPTION
899     WHEN OTHERS THEN
900       IF (SQLCODE <> -20001) THEN
901         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
902         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
903         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
904         FND_MESSAGE.SET_TOKEN('PARAMETERS',
905           ' P_Prepay_Distribution_id = '||P_Prepay_Distribution_id||
906           ' P_Calling_Sequence = '||P_Calling_Sequence);
907         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
908       END IF;
909 
910       IF ( prepay_awt_group_id%ISOPEN ) THEN
911         CLOSE prepay_awt_group_id;
912       END IF;
913 
914       APP_EXCEPTION.RAISE_EXCEPTION;
915   END Get_Prepay_Awt_Group_Id;
916 
917 /*=============================================================================
918  |  FUNCTION - Return_Tax_Lines()
919  |
920  |  DESCRIPTION
921  |      This function handles the return of tax lines.  It includes creation,
922  |      update, or delete of existing exclusive tax lines in AP if required.
923  |      It also handles the update of the total tax amounts. (Inclusive and
924  |      self-assessed.
925  |
926  |  PARAMETERS
927  |      P_Invoice_Header_Rec - Header info
928  |      P_error_code - Error code to be returned
929  |      P_calling_sequence -  Calling sequence
930  |
931  |  MODIFICATION HISTORY
932  |    DATE          Author         Action
933  |    15-OCT-2003   SYIDNER        Created
934  |
935  *============================================================================*/
936   FUNCTION Return_Tax_Lines(
937              P_Invoice_Header_Rec        IN ap_invoices_all%ROWTYPE,
938              P_Error_Code                OUT NOCOPY VARCHAR2,
939              P_Calling_Sequence          IN VARCHAR2) RETURN BOOLEAN
940   IS
941 
942     l_debug_info                 VARCHAR2(240);
943     l_curr_calling_sequence      VARCHAR2(4000);
944 
945     l_period_name                gl_period_statuses.period_name%TYPE;
946     l_gl_date                    ap_invoice_lines_all.accounting_date%TYPE;
947     l_wfapproval_flag		 ap_system_parameters_all.approval_workflow_flag%TYPE;
948     l_awt_include_tax_amt        ap_system_parameters_all.awt_include_tax_amt%TYPE;
949     l_base_currency_code         ap_system_parameters_all.base_currency_code%TYPE;
950     l_combined_filing_flag       ap_system_parameters_all.combined_filing_flag%TYPE;
951     l_income_tax_region_flag     ap_system_parameters_all.income_tax_region_flag%TYPE;
952     l_income_tax_region          ap_system_parameters_all.income_tax_region%TYPE;
953     l_disc_is_inv_less_tax_flag	 ap_system_parameters_all.disc_is_inv_less_tax_flag%TYPE;
954     l_wfapproval_status          ap_invoice_lines_all.wfapproval_status%TYPE;
955     l_new_amt_applicable_to_disc ap_invoices_all.amount_applicable_to_discount%TYPE;
956     l_payment_priority           ap_batches_all.payment_priority%TYPE;
957 
958     l_total_tax_amount          NUMBER;
959     l_self_assessed_tax_amt     NUMBER;
960 
961     -- Allocations
962     Cursor c_item_line (c_invoice_id IN NUMBER) IS
963 	Select ail.line_number
964 	From   ap_invoice_lines_all ail
965 	Where  ail.invoice_id = c_invoice_id
966         And    ail.line_type_lookup_code = 'TAX'
967         And    ail.prepay_invoice_id IS NULL;
968 
969         /*
970 	And NOT EXISTS
971 		(Select chrg_invoice_line_number
972 		 From ap_allocation_rule_lines arl
973           	 Where arl.invoice_id = ail.invoice_id
974             	 And arl.chrg_invoice_line_number = ail.line_number);
975         */
976 
977     l_item_line	     c_item_line%rowtype;
978 
979     l_ap_summary_tax_line_id    NUMBER;
980     l_invoice_line_number       NUMBER;
981 
982     l_api_name                  CONSTANT VARCHAR2(100) := 'RETURN_TAX_LINES';
983 
984   BEGIN
985 
986     l_curr_calling_sequence := 'AP_ETAX_UTILITY_PKG.Return_Tax_Lines<-'||
987                                P_calling_sequence;
988 
989      -------------------------------------------------------------------
990      l_debug_info := 'Incorrect Summary Tax Line Check';
991      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
992         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
993      END IF;
994      -------------------------------------------------------------------
995 
996      BEGIN
997 
998      l_ap_summary_tax_line_id := NULL;
999 
1000      SELECT ail.line_number, ail.summary_tax_line_id
1001        INTO l_invoice_line_number, l_ap_summary_tax_line_id
1002        FROM AP_INVOICE_LINES_ALL ail
1003       WHERE ail.invoice_id            = P_Invoice_Header_Rec.invoice_id
1004         AND ail.line_type_lookup_code = 'TAX'
1005         AND NOT EXISTS
1006             (SELECT ls.summary_tax_line_id
1007                FROM zx_lines_summary ls
1008               WHERE ls.summary_tax_line_id             = ail.summary_tax_line_id
1009                 AND ls.trx_id                          = ail.invoice_id
1010                 AND NVL(ls.tax_amt_included_flag, 'N') = 'N'
1011                 AND NVL(ls.self_assessed_flag, 'N')    = 'N'
1012                 AND NVL(ls.reporting_only_flag, 'N')   = 'N'
1013             )
1014         AND EXISTS
1015             (SELECT 'Invoice Distributions Exist'
1016                FROM ap_invoice_distributions_all aid
1017               WHERE aid.invoice_id          = ail.invoice_id
1018                 AND aid.invoice_line_number = ail.line_number
1019                 AND (aid.accounting_event_id IS NOT NULL
1020                      OR NVL(aid.match_status_flag,'N') IN ('A','T')
1021                      OR aid.bc_event_id IS NOT NULL
1022                      OR NVL(aid.encumbered_flag, 'N') IN ('Y','D','W','X')
1023                     ) --This is done for Mexicana Bug7623255 to avoid such Data Fix Condition
1024             )
1025         AND EXISTS
1026             (SELECT zl.summary_tax_line_id
1027                FROM zx_lines_summary zl
1028               WHERE zl.application_id                = 200
1029                 AND zl.entity_code                   = 'AP_INVOICES'
1030                 AND zl.event_class_code             IN ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
1031                 AND zl.trx_id                        = ail.invoice_id
1032                 AND zl.tax_regime_code               = ail.tax_regime_code
1033                 AND zl.tax                           = ail.tax
1034                 AND zl.tax_status_code               = ail.tax_status_code
1035                 AND (zl.tax_rate_code                = ail.tax_rate_code
1036                      OR zl.tax_rate_id               = ail.tax_rate_id)
1037                 AND (zl.tax_jurisdiction_code        = ail.tax_jurisdiction_code
1038                      OR zl.tax_jurisdiction_code    IS NULL)
1039                 AND zl.tax_rate                      = ail.tax_rate
1040                 AND zl.tax_amt			 = ail.amount
1041                 AND NVL(zl.reporting_only_flag, 'N') = 'N'
1042                 AND NVL(zl.self_assessed_flag, 'N')  = 'N'
1043                 AND NVL(zl.cancel_flag, 'N')         = NVL(ail.cancelled_flag,'N')
1044                 AND zl.summary_tax_line_id          <> ail.summary_tax_line_id
1045            )
1046        AND rownum = 1;
1047 
1048        --Bug 7485573 -- Move the exception block above the if condition.
1049 
1050        EXCEPTION
1051           WHEN NO_DATA_FOUND THEN
1052                NULL;
1053 
1054           WHEN OTHERS THEN
1055 	       NULL;
1056     END;
1057 
1058      IF l_ap_summary_tax_line_id IS NOT NULL THEN
1059 
1060                -------------------------------------------------------------------
1061                l_debug_info := 'Invoice Line Number that causes this failure: '||l_invoice_line_number;
1062                IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1063                   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1064                END IF;
1065 
1066                l_debug_info := 'Summary Tax Line ID for this line: '||l_ap_summary_tax_line_id;
1067                IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1068                   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1069                END IF;
1070                -------------------------------------------------------------------
1071 
1072                FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
1073 	       FND_MESSAGE.SET_TOKEN('ERROR', 'Summary Tax Line Deleted by EBTax. This would cause orphan distributions.');
1074 	       FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
1075 	       FND_MESSAGE.SET_TOKEN('PARAMETERS',
1076 	            ' P_Invoice_Id = '||P_Invoice_Header_Rec.invoice_id||
1077 	            ' P_Error_Code = '||P_Error_Code||
1078 	            ' P_Calling_Sequence = '||P_Calling_Sequence);
1079 	       FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
1080 
1081 	       APP_EXCEPTION.RAISE_EXCEPTION;
1082 
1083     END IF;
1084 
1085 
1086 
1087     -------------------------------------------------------------------
1088     l_debug_info := 'Get ap_system_parameters data';
1089     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1090       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1091     END IF;
1092     -------------------------------------------------------------------
1093     BEGIN
1094       SELECT
1095           approval_workflow_flag,
1096           awt_include_tax_amt,
1097           disc_is_inv_less_tax_flag,
1098           base_currency_code,
1099           combined_filing_flag,
1100           income_tax_region_flag,
1101           income_tax_region
1102         INTO
1103           l_wfapproval_flag,
1104           l_awt_include_tax_amt,
1105           l_disc_is_inv_less_tax_flag,
1106           l_base_currency_code,
1107           l_combined_filing_flag,
1108           l_income_tax_region_flag,
1109           l_income_tax_region
1110         FROM ap_system_parameters_all
1111        WHERE org_id = P_Invoice_Header_Rec.org_id;
1112     END;
1113 
1114     -------------------------------------------------------------------
1115     l_debug_info := 'Update existing exclusive tax lines';
1116     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1117       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1118     END IF;
1119     -------------------------------------------------------------------
1120     BEGIN
1121       UPDATE ap_invoice_lines_all ail
1122          SET
1123        (ail.description,
1124         ail.amount,
1125         ail.base_amount,
1126         ail.cancelled_flag,
1127         ail.last_updated_by,
1128         ail.last_update_login,
1129         ail.last_update_date,
1130         ail.tax_regime_code,
1131         ail.tax,
1132         ail.tax_jurisdiction_code,
1133         ail.tax_status_code,
1134         ail.tax_rate_id,
1135         ail.tax_rate_code,
1136         ail.tax_rate,
1137 	ail.generate_dists) =
1138 	(
1139       SELECT
1140         zls.tax_regime_code||' - '||zls.tax,			-- description
1141         zls.tax_amt, 						-- amount
1142         zls.tax_amt_funcl_curr,					-- base_amount
1143         zls.cancel_flag,					-- cancelled_flag
1144         l_user_id,						-- last_updated_by
1145         l_login_id,						-- last_update_login
1146         l_sysdate, 						-- last_update_date
1147         zls.tax_regime_code,	    				-- tax_regime_code
1148         zls.tax,		    				-- tax
1149         zls.tax_jurisdiction_code,  				-- tax_jurisdiction_code
1150         zls.tax_status_code,	    				-- tax_status_code
1151         zls.tax_rate_id,	    				-- tax_rate_id
1152         zls.tax_rate_code,	    				-- tax_rate_code
1153         zls.tax_rate, 		    				-- tax_rate
1154 	DECODE(ail.generate_dists,'D','D','Y')		        -- generate_dists  bug 5460342
1155         FROM zx_lines_summary zls
1156        WHERE zls.summary_tax_line_id		= ail.summary_tax_line_id
1157          AND nvl(zls.reporting_only_flag, 'N')	= 'N'
1158        )
1159        WHERE ail.invoice_id = P_Invoice_Header_Rec.invoice_id
1160 	 AND ail.line_type_lookup_code	= 'TAX'
1161          AND EXISTS
1162 	       	    (SELECT ls.summary_tax_line_id
1163                        FROM zx_lines_summary ls
1164                       WHERE ls.summary_tax_line_id		= ail.summary_tax_line_id
1165                         AND ls.trx_id				= ail.invoice_id
1166                         AND NVL(ls.tax_amt_included_flag, 'N')	= 'N'
1167                         AND NVL(ls.self_assessed_flag, 'N')	= 'N'
1168                         AND NVL(ls.reporting_only_flag, 'N')	= 'N');
1169 
1170     EXCEPTION
1171       WHEN NO_DATA_FOUND THEN
1172         NULL;
1173 
1174       WHEN OTHERS THEN
1175         IF (SQLCODE <> -20001) THEN
1176           FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
1177           FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
1178           FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
1179           FND_MESSAGE.SET_TOKEN('PARAMETERS',
1180             ' P_Invoice_Id = '||P_Invoice_Header_Rec.Invoice_Id||
1181             ' P_Error_Code = '||P_Error_Code||
1182             ' P_Calling_Sequence = '||P_Calling_Sequence);
1183           FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
1184         END IF;
1185 
1186       APP_EXCEPTION.RAISE_EXCEPTION;
1187     END;
1188 
1189     /* Start for bug 5943074-- updating summary_tax_line_id of ap_invoice_lines_all based on zx_lines
1190        for manual tax lines and tax only lines so that we do not delete and regenerate the tax lines . */
1191     -------------------------------------------------------------------
1192     l_debug_info := 'Update summary tax line id for manual tax lines';
1193     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1194       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1195     END IF;
1196     -------------------------------------------------------------------
1197     BEGIN
1198 
1199       UPDATE ap_invoice_lines_all ail
1200       SET ail.summary_tax_line_id = (SELECT  zl.summary_tax_line_id
1201                                             FROM zx_lines zl
1202                                             WHERE zl.application_id 	 =  200
1203                                             AND zl.entity_code 	 =  'AP_INVOICES'
1204                                             AND zl.event_class_code IN
1205 					        ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
1206                                             AND zl.trx_id 	= ail.invoice_id
1207                                             AND zl.tax_regime_code = ail.tax_regime_code
1208                                             AND zl.tax = ail.tax
1209                                             AND zl.tax_status_code = ail.tax_status_code
1210                                             AND (zl.tax_rate_code = ail.tax_rate_code OR zl.tax_rate_id = ail.tax_rate_id)
1211 					    AND (zl.tax_jurisdiction_code = ail.tax_jurisdiction_code OR zl.tax_jurisdiction_code IS NULL)
1212                                             AND zl.tax_rate = ail.tax_rate
1213                                             AND zl.manually_entered_flag = 'Y'
1214                                             AND nvl(zl.reporting_only_flag, 'N') = 'N'
1215 					    AND nvl(zl.self_assessed_flag, 'N') = 'N'
1216 					    AND nvl(zl.cancel_flag, 'N') = nvl(ail.cancelled_flag,'N')
1217                                             AND rownum = 1       )
1218       WHERE ail.invoice_id = P_Invoice_Header_Rec.invoice_id
1219       AND ail.line_type_lookup_code	= 'TAX'
1220       AND ail.line_source in ('MANUAL LINE ENTRY','IMPORTED')
1221       AND ail.summary_tax_line_id IS NULL
1222       AND NOT EXISTS
1223            (SELECT 1
1224               FROM ap_invoice_lines_all ail2
1225              WHERE ail2.invoice_id = ail.invoice_id
1226                AND ail2.line_number <> ail.line_number
1227                AND ail2.tax_regime_code = ail.tax_regime_code
1228                AND ail2.tax = ail.tax
1229                AND ail2.tax_status_code = ail.tax_status_code
1230                AND (ail2.tax_rate_code = ail.tax_rate_code OR ail2.tax_rate_id = ail.tax_rate_id)
1231 	       AND (ail2.tax_jurisdiction_code = ail.tax_jurisdiction_code OR ail2.tax_jurisdiction_code IS NULL)
1232                AND ail2.tax_rate = ail.tax_rate
1233                AND ail2.line_type_lookup_code = 'TAX'
1234                AND ail2.line_source in ('MANUAL LINE ENTRY','IMPORTED')
1235                AND ail2.line_group_number IS NOT NULL
1236                AND ail2.prorate_across_all_items='Y');    --Bug7331216
1237 
1238     EXCEPTION
1239        WHEN NO_DATA_FOUND THEN
1240             NULL;
1241 
1242        WHEN OTHERS THEN
1243             IF (SQLCODE <> -20001) THEN
1244                FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
1245                FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
1246                FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
1247                FND_MESSAGE.SET_TOKEN('PARAMETERS',' P_Invoice_Id = '||P_Invoice_Header_Rec.Invoice_Id
1248                                                   ||' P_Error_Code = '||P_Error_Code
1249                                                   ||' P_Calling_Sequence = '||P_Calling_Sequence);
1250                FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
1251             END IF;
1252             APP_EXCEPTION.RAISE_EXCEPTION;
1253     END;
1254     /* End for bug 5943074 */
1255 
1256     -------------------------------------------------------------------
1257     l_debug_info := 'Delete exclusive tax lines if required';
1258     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1259       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1260     END IF;
1261     -------------------------------------------------------------------
1262     BEGIN
1263       DELETE ap_invoice_lines_all ail
1264        WHERE ail.invoice_id = P_Invoice_Header_Rec.invoice_id
1265          AND ail.line_type_lookup_code = 'TAX'
1266          AND NOT EXISTS (SELECT ls.summary_tax_line_id
1267                            FROM zx_lines_summary ls
1268                           WHERE ls.summary_tax_line_id  = ail.summary_tax_line_id
1269                             AND ls.trx_id               = ail.invoice_id
1270                             AND NVL(ls.tax_amt_included_flag, 'N') = 'N'
1271                             AND NVL(ls.self_assessed_flag,    'N') = 'N'
1272                             AND NVL(ls.reporting_only_flag,   'N') = 'N');
1273 -- Bug 7260087 Starts
1274 -- When ever invoice line is deleted corresponding
1275 -- allocations should  be deleted.
1276       DELETE FROM ap_allocation_rules ar
1277        WHERE ar.invoice_id = p_invoice_header_rec.invoice_id
1278          AND NOT EXISTS
1279                  (SELECT 'y'
1280                   FROM ap_invoice_lines_all l
1281                   WHERE l.invoice_id = ar.invoice_id
1282                   AND l.line_number = ar.chrg_invoice_line_number);
1283 
1284        DELETE FROM ap_allocation_rule_lines arl
1285         WHERE arl.invoice_id = p_invoice_header_rec.invoice_id
1286           AND NOT EXISTS
1287                   (SELECT 'y'
1288                    FROM ap_invoice_lines_all l
1289                    WHERE l.invoice_id = arl.invoice_id
1290                    AND l.line_number = arl.chrg_invoice_line_number);
1291 -- Bug 7260087 ends
1292 
1293     EXCEPTION
1294       WHEN NO_DATA_FOUND THEN
1295         NULL;
1296 
1297       WHEN OTHERS THEN
1298         IF (SQLCODE <> -20001) THEN
1299           FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
1300           FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
1301           FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
1302           FND_MESSAGE.SET_TOKEN('PARAMETERS',
1303             ' P_Invoice_Id = '||P_Invoice_Header_Rec.invoice_id||
1304             ' P_Error_Code = '||P_Error_Code||
1305             ' P_Calling_Sequence = '||P_Calling_Sequence);
1306           FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
1307         END IF;
1308 
1309       APP_EXCEPTION.RAISE_EXCEPTION;
1310     END;
1311 
1312     -------------------------------------------------------------------
1313     l_debug_info := 'Get open gl_date';
1314     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1315       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1316     END IF;
1317     -------------------------------------------------------------------
1318 
1319     l_period_name := AP_UTILITIES_PKG.get_current_gl_date (P_Invoice_Header_Rec.gl_date, P_Invoice_header_Rec.org_id);
1320 
1321     IF (l_period_name IS NULL) THEN
1322 	AP_UTILITIES_PKG.get_open_gl_date(
1323 	       P_Date                  => P_Invoice_Header_Rec.gl_date,
1324 	       P_Period_Name           => l_period_name,
1325 	       P_GL_Date               => l_gl_date,
1326 	       P_Org_Id                => P_Invoice_Header_Rec.org_id);
1327     ELSE
1328 	l_gl_date := P_Invoice_Header_Rec.gl_date;
1329     END IF;
1330 
1331     IF NVL(l_wfapproval_flag,'N') = 'Y' THEN
1332       l_wfapproval_status := 'REQUIRED';
1333     ELSE
1334       l_wfapproval_status := 'NOT REQUIRED';
1335     END IF;
1336 
1337     -------------------------------------------------------------------
1338     l_debug_info := 'Insert exclusive tax lines';
1339     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1340       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1341     END IF;
1342     -------------------------------------------------------------------
1343     BEGIN
1344       INSERT INTO ap_invoice_lines_all (
1345         invoice_id,
1346         line_number,
1347         line_type_lookup_code,
1348         requester_id,
1349         description,
1350         line_source,
1351         org_id,
1352         line_group_number,
1353         inventory_item_id,
1354         item_description,
1355         serial_number,
1356         manufacturer,
1357         model_number,
1358         warranty_number,
1359         generate_dists,
1360         match_type,
1361         distribution_set_id,
1362         account_segment,
1363         balancing_segment,
1364         cost_center_segment,
1365         overlay_dist_code_concat,
1366         default_dist_ccid,
1367         prorate_across_all_items,
1368         accounting_date,
1369         period_name,
1370         deferred_acctg_flag,
1371         def_acctg_start_date,
1372         def_acctg_end_date,
1373         def_acctg_number_of_periods,
1374         def_acctg_period_type,
1375         set_of_books_id,
1376         amount,
1377         base_amount,
1378         rounding_amt,
1379         quantity_invoiced,
1380         unit_meas_lookup_code,
1381         unit_price,
1382         wfapproval_status,
1383         discarded_flag,
1384         original_amount,
1385         original_base_amount,
1386         original_rounding_amt,
1387         cancelled_flag,
1388         income_tax_region,
1389         type_1099,
1390         stat_amount,
1391         prepay_invoice_id,
1392         prepay_line_number,
1393         invoice_includes_prepay_flag,
1394         corrected_inv_id,
1395         corrected_line_number,
1396         po_header_id,
1397         po_line_id,
1398         po_release_id,
1399         po_line_location_id,
1400         po_distribution_id,
1401         rcv_transaction_id,
1402         final_match_flag,
1403         assets_tracking_flag,
1404         asset_book_type_code,
1405         asset_category_id,
1406         project_id,
1407         task_id,
1408         expenditure_type,
1409         expenditure_item_date,
1410         expenditure_organization_id,
1411         pa_quantity,
1412         pa_cc_ar_invoice_id,
1413         pa_cc_ar_invoice_line_num,
1414         pa_cc_processed_code,
1415         award_id,
1416         awt_group_id,
1417         reference_1,
1418         reference_2,
1419         receipt_verified_flag,
1420         receipt_required_flag,
1421         receipt_missing_flag,
1422         justification,
1423         expense_group,
1424         start_expense_date,
1425         end_expense_date,
1426         receipt_currency_code,
1427         receipt_conversion_rate,
1428         receipt_currency_amount,
1429         daily_amount,
1430         web_parameter_id,
1431         adjustment_reason,
1432         merchant_document_number,
1433         merchant_name,
1434         merchant_reference,
1435         merchant_tax_reg_number,
1436         merchant_taxpayer_id,
1437         country_of_supply,
1438         credit_card_trx_id,
1439         company_prepaid_invoice_id,
1440         cc_reversal_flag,
1441         creation_date,
1442         created_by,
1443         last_updated_by,
1444         last_update_date,
1445         last_update_login,
1446         program_application_id,
1447         program_id,
1448         program_update_date,
1449         request_id,
1450         attribute_category,
1451         attribute1,
1452         attribute2,
1453         attribute3,
1454         attribute4,
1455         attribute5,
1456         attribute6,
1457         attribute7,
1458         attribute8,
1459         attribute9,
1460         attribute10,
1461         attribute11,
1462         attribute12,
1463         attribute13,
1464         attribute14,
1465         attribute15,
1466         global_attribute_category,
1467         global_attribute1,
1468         global_attribute2,
1469         global_attribute3,
1470         global_attribute4,
1471         global_attribute5,
1472         global_attribute6,
1473         global_attribute7,
1474         global_attribute8,
1475         global_attribute9,
1476         global_attribute10,
1477         global_attribute11,
1478         global_attribute12,
1479         global_attribute13,
1480         global_attribute14,
1481         global_attribute15,
1482         global_attribute16,
1483         global_attribute17,
1484         global_attribute18,
1485         global_attribute19,
1486         global_attribute20,
1487         control_amount,
1488         assessable_value,
1489         total_rec_tax_amount,
1490         total_nrec_tax_amount,
1491         total_rec_tax_amt_funcl_curr,
1492         total_nrec_tax_amt_funcl_curr,
1493         included_tax_amount,
1494         primary_intended_use,
1495         ship_to_location_id,
1496         product_type,
1497         product_category,
1498         product_fisc_classification,
1499         user_defined_fisc_class,
1500         trx_business_category,
1501         summary_tax_line_id,
1502         tax_regime_code,
1503         tax,
1504         tax_jurisdiction_code,
1505         tax_status_code,
1506         tax_rate_id,
1507         tax_rate_code,
1508         tax_rate,
1509         tax_code_id)
1510       SELECT
1511         P_Invoice_Header_Rec.Invoice_Id,				-- invoice_id
1512         (SELECT NVL(MAX(ail2.line_number),0)
1513            FROM ap_invoice_lines_all ail2
1514           WHERE ail2.invoice_id =  zls.trx_id) + ROWNUM,  		-- line_number
1515         'TAX',								-- line_type_lookup_code
1516         null,								-- requester_id
1517         zls.tax_regime_code||' - '||zls.tax,				-- description
1518         'ETAX',								-- line_source
1519         P_Invoice_Header_Rec.org_id,   					-- org_id
1520         null,   							-- line_group_number
1521         null,   							-- inventory_item_id
1522         null,   							-- item_description
1523         null,   							-- serial_number
1524         null,   							-- manufacturer
1525         null,   							-- model_number
1526         null,   							-- warranty_number
1527         DECODE(NVL(zls.tax_only_line_flag, 'N'),
1528                'Y', 'D',
1529                'Y'),   							-- generate_dists
1530         DECODE(zls.applied_to_trx_id,
1531                null, 'NOT_MATCHED',
1532                'OTHER_TO_RECEIPT'),   					-- match_type
1533         null,   							-- distribution_set_id
1534         null,   							-- account_segment
1535         null,   							-- balancing_segment
1536         null,   							-- cost_center_segment
1537         null,   							-- overlay_dist_code_concat
1538         null,   							-- default_dist_ccid
1539         'N',   								-- prorate_across_all_items
1540         l_gl_date,   							-- accounting_date
1541         DECODE(NVL(zls.tax_only_line_flag, 'N'),
1542                'N', DECODE(zls.applied_to_trx_id,
1543                            null, null, l_period_name),
1544                 l_period_name),   					-- period_name
1545         'N',   								-- deferred_acctg_flag
1546         null,   							-- def_acctg_start_date
1547         null,   							-- def_acctg_end_date
1548         null,   							-- def_acctg_number_of_periods
1549         null,   							-- def_acctg_period_type
1550         P_Invoice_Header_Rec.set_of_books_id,   			-- set_of_books_id
1551         zls.tax_amt,   							-- amount
1552         DECODE(P_Invoice_Header_Rec.invoice_currency_code,
1553                l_base_currency_code, NULL,
1554                zls.tax_amt_funcl_curr),    				-- base_amount
1555         null,   							-- rounding_amt
1556         null,   							-- quantity_invoiced
1557         null,   							-- unit_meas_lookup_code
1558         null,   							-- unit_price
1559         l_wfapproval_status,   						-- wfapproval_status
1560         'N',   								-- discarded_flag
1561         null,   							-- original_amount
1562         null,   							-- original_base_amount
1563         null,   							-- original_rounding_amt
1564         'N',   								-- cancelled_flag
1565         DECODE(ap.type_1099,
1566                '','',
1567                DECODE(l_combined_filing_flag,
1568                       'N', '',
1569                       DECODE(l_income_tax_region_flag,
1570                              'Y', aps.state,
1571                              l_income_tax_region))),  			-- income_tax_region
1572         ap.type_1099,   						-- type_1099
1573         null,   							-- stat_amount
1574         zls.applied_from_trx_id,   					-- prepay_invoice_id
1575         zls.applied_from_line_id,   					-- prepay_line_number
1576         prepay.invoice_includes_prepay_flag,   				-- invoice_includes_prepay_flag
1577         zls.adjusted_doc_trx_id,   					-- corrected_inv_id
1578         -- zls.adjusted_doc_line_id,   					-- corrected_line_number
1579         null,								-- corrected_line_number
1580         null,   							-- po_header_id
1581         null,   							-- po_line_id
1582         null,   							-- po_release_id
1583         null,   							-- po_line_location_id
1584         null,   							-- po_distribution_id
1585         zls.applied_to_trx_id,						-- rcv_transaction_id
1586         'N',   								-- final_match_flag
1587         null,   							-- assets_tracking_flag
1588         null,   							-- asset_book_type_code
1589         null,   							-- asset_category_id
1590         null,   							-- project_id
1591         null,   							-- task_id
1592         null,   							-- expenditure_type
1593         null,   							-- expenditure_item_date
1594         null,   							-- expenditure_organization_id
1595         null,   							-- pa_quantity
1596         null,   							-- pa_cc_ar_invoice_id
1597         null,   							-- pa_cc_ar_invoice_line_num
1598         null,   							-- pa_cc_processed_code
1599         null,   							-- award_id
1600         DECODE(l_awt_include_tax_amt,
1601                'N', null,
1602                DECODE(zls.applied_from_trx_id,
1603                       null, P_Invoice_Header_Rec.awt_group_id,
1604                       prepay.awt_group_id)),   				-- awt_group_id
1605         null,   							-- reference_1
1606         null,   							-- reference_2
1607         null,   							-- receipt_verified_flag
1608         null,   							-- receipt_required_flag
1609         null,   							-- receipt_missing_flag
1610         null,   							-- justification
1611         null,   							-- expense_group
1612         null,   							-- start_expense_date
1613         null,   							-- end_expense_date
1614         null,   							-- receipt_currency_code
1615         null,   							-- receipt_conversion_rate
1616         null,   							-- receipt_currency_amount
1617         null,   							-- daily_amount
1618         null,   							-- web_parameter_id
1619         null,   							-- adjustment_reason
1620         null,   							-- merchant_document_number
1621         null,   							-- merchant_name
1622         null,   							-- merchant_reference
1623         null,   							-- merchant_tax_reg_number
1624         null,								-- merchant_taxpayer_id
1625         null,								-- country_of_supply
1626         null,								-- credit_card_trx_id
1627         null,								-- company_prepaid_invoice_id
1628         null,								-- cc_reversal_flag
1629         l_sysdate,							-- creation_date
1630         l_user_id,   							-- created_by
1631         l_user_id,   							-- last_updated_by
1632         l_sysdate,   							-- last_update_date
1633         l_login_id,   							-- last_update_login
1634         null,   							-- program_application_id
1635         null,   							-- program_id
1636         null,   							-- program_update_date
1637         null,   							-- request_id
1638         zls.attribute_category,   					-- attribute_category
1639         zls.attribute1,   						-- attribute1
1640         zls.attribute2,   						-- attribute2
1641         zls.attribute3,   						-- attribute3
1642         zls.attribute4,   						-- attribute4
1643         zls.attribute5,   						-- attribute5
1644         zls.attribute6,   						-- attribute6
1645         zls.attribute7,   						-- attribute7
1646         zls.attribute8,   						-- attribute8
1647         zls.attribute9,   						-- attribute9
1648         zls.attribute10,   						-- attribute10
1649         zls.attribute11,   						-- attribute11
1650         zls.attribute12,   						-- attribute12
1651         zls.attribute13,   						-- attribute13
1652         zls.attribute14,   						-- attribute14
1653         zls.attribute15,   						-- attribute15
1654         zls.global_attribute_category,   				-- global_attribute_category
1655         zls.global_attribute1,   					-- global_attribute1
1656         zls.global_attribute2,   					-- global_attribute2
1657         zls.global_attribute3,   					-- global_attribute3
1658         zls.global_attribute4,   					-- global_attribute4
1659         zls.global_attribute5,   					-- global_attribute5
1660         zls.global_attribute6,   					-- global_attribute6
1661         zls.global_attribute7,   					-- global_attribute7
1662         zls.global_attribute8,   					-- global_attribute8
1663         zls.global_attribute9,   					-- global_attribute9
1664         zls.global_attribute10,   					-- global_attribute10
1665         zls.global_attribute11,   					-- global_attribute11
1666         zls.global_attribute12,   					-- global_attribute12
1667         zls.global_attribute13,   					-- global_attribute13
1668         zls.global_attribute14,   					-- global_attribute14
1669         zls.global_attribute15,   					-- global_attribute15
1670         zls.global_attribute16,   					-- global_attribute16
1671         zls.global_attribute17,   					-- global_attribute17
1672         zls.global_attribute18,   					-- global_attribute18
1673         zls.global_attribute19,   					-- global_attribute19
1674         zls.global_attribute20,   					-- global_attribute20
1675         null,   							-- control_amount
1676         null,   							-- assessable_value
1677         null,   							-- total_rec_tax_amount
1678         null,   							-- total_nrec_tax_amount
1679         null,   							-- total_rec_tax_amt_funcl_curr
1680         null,   							-- total_nrec_tax_amt_funcl_curr
1681         null,   							-- included_tax_amount
1682         null,   							-- primary_intended_use
1683         null,   							-- ship_to_location_id
1684         null,   							-- product_type
1685         null,   							-- product_category
1686         null,   							-- product_fisc_classification
1687         null,   							-- user_defined_fisc_class
1688         null,   							-- trx_business_category
1689         zls.summary_tax_line_id,   					-- summary_tax_line_id
1690         zls.tax_regime_code,   						-- tax_regime_code
1691         zls.tax,   							-- tax
1692         zls.tax_jurisdiction_code,   					-- tax_jurisdiction_code
1693         zls.tax_status_code,   						-- tax_status_code
1694         zls.tax_rate_id,   						-- tax_rate_id
1695         zls.tax_rate_code,   						-- tax_rate_code
1696         zls.tax_rate,   						-- tax_rate
1697         null   								-- tax_code_id
1698      FROM ap_invoices_all       ai,
1699           ap_suppliers          ap,
1700           ap_supplier_sites_all aps,
1701           zx_lines_summary      zls,
1702           ap_invoice_lines_all  prepay
1703     WHERE ai.invoice_id				= p_invoice_header_rec.invoice_id
1704       AND ai.vendor_id                          = ap.vendor_id
1705       AND ai.vendor_site_id                     = aps.vendor_site_id
1706       AND zls.application_id 			= 200
1707       AND zls.entity_code 			= 'AP_INVOICES'
1708       AND zls.event_class_code			IN ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
1709       AND zls.trx_id 				= ai.invoice_id
1710       AND NVL(zls.tax_amt_included_flag, 'N') 	= 'N'
1711       AND NVL(zls.self_assessed_flag, 'N') 	= 'N'
1712       AND NVL(zls.reporting_only_flag, 'N') 	= 'N'
1713       AND zls.applied_from_trx_id  		= prepay.invoice_id(+)
1714       AND zls.applied_from_line_id 		= prepay.line_number(+)
1715       AND NOT EXISTS (SELECT il.summary_tax_line_id
1716                         FROM ap_invoice_lines_all il
1717                        WHERE il.invoice_id = ai.invoice_id
1718                          AND il.summary_tax_line_id = zls.summary_tax_line_id)
1719       AND EXISTS
1720           (SELECT 'Recoupment Exists'
1721            FROM    ZX_LINES ZL
1722            WHERE   ZL.application_id      = ZLS.application_id
1723            AND     ZL.entity_code         = ZLS.entity_code
1724            AND     ZL.event_class_code    = ZLS.event_class_code
1725            AND     ZL.trx_id              = ZLS.trx_id
1726            AND     ((nvl(ZL.tax_only_line_flag,'N') <> 'Y'
1727                      and sign(ZL.TRX_LINE_ID)       <> -1)
1728                     OR nvl(ZL.tax_only_line_flag,'N') = 'Y')
1729            AND     ZL.SUMMARY_TAX_LINE_ID = ZLS.SUMMARY_TAX_LINE_ID);
1730 
1731     EXCEPTION
1732       WHEN NO_DATA_FOUND THEN
1733         NULL;
1734 
1735       WHEN OTHERS THEN
1736         IF (SQLCODE <> -20001) THEN
1737           FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
1738           FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
1739           FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
1740           FND_MESSAGE.SET_TOKEN('PARAMETERS',
1741             ' P_Invoice_Id = '||P_Invoice_Header_Rec.invoice_id||
1742             ' P_Error_Code = '||P_Error_Code||
1743             ' P_Calling_Sequence = '||P_Calling_Sequence);
1744           FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
1745         END IF;
1746 
1747       APP_EXCEPTION.RAISE_EXCEPTION;
1748     END;
1749 
1750     -------------------------------------------------------------------
1751     l_debug_info := 'Update Inclusive tax amount';
1752     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1753       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1754     END IF;
1755     -------------------------------------------------------------------
1756     BEGIN
1757     --bug 6931461
1758       UPDATE ap_invoice_lines_all ail
1759          SET ail.included_tax_amount =
1760              (SELECT /*+ index(ZL ZX_LINES_U1) */SUM(NVL(zl.tax_amt, 0))
1761                 FROM zx_lines zl
1762                WHERE zl.application_id 	 =  200
1763                  AND zl.entity_code 	 =  'AP_INVOICES'
1764 		 AND zl.event_class_code IN ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
1765                  AND zl.trx_id 		= ail.invoice_id
1766                  AND zl.trx_line_id 	= ail.line_number
1767                  AND NVL(zl.self_assessed_flag,    'N')	= 'N'
1768                  AND NVL(zl.reporting_only_flag,   'N') = 'N'
1769                  AND NVL(zl.tax_amt_included_flag, 'N') = 'Y')
1770        WHERE ail.invoice_id = P_Invoice_Header_Rec.invoice_id
1771          AND ail.line_type_lookup_code NOT IN ('TAX', 'AWT');
1772 
1773     EXCEPTION
1774       WHEN NO_DATA_FOUND THEN
1775         NULL;
1776 
1777       WHEN OTHERS THEN
1778         IF (SQLCODE <> -20001) THEN
1779           FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
1780           FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
1781           FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
1782           FND_MESSAGE.SET_TOKEN('PARAMETERS',
1783             ' P_Invoice_Id = '||P_Invoice_Header_Rec.invoice_id||
1784             ' P_Error_Code = '||P_Error_Code||
1785             ' P_Calling_Sequence = '||P_Calling_Sequence);
1786           FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
1787         END IF;
1788 
1789       APP_EXCEPTION.RAISE_EXCEPTION;
1790     END;
1791 
1792     --------------------------------------------------
1793     l_debug_info := 'Create Tax Allocations';
1794     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1795       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1796     END IF;
1797     --------------------------------------------------
1798     BEGIN
1799 	OPEN c_item_line(P_Invoice_Header_Rec.invoice_id);
1800 	LOOP
1801 	   FETCH c_item_line
1802 	    INTO l_item_line;
1803 	   EXIT WHEN c_item_line%notfound;
1804 
1805 	   IF NOT AP_ALLOCATION_RULES_PKG.insert_tax_allocations (
1806 				          P_Invoice_Header_Rec.invoice_id,
1807 				          L_Item_Line.line_number,
1808 				          P_error_code ) THEN
1809 	      NULL;
1810 	   END IF;
1811 	END LOOP;
1812 	CLOSE c_item_line;
1813     EXCEPTION
1814       WHEN OTHERS THEN
1815         IF (SQLCODE <> -20001) THEN
1816           FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
1817           FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
1818           FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
1819           FND_MESSAGE.SET_TOKEN('PARAMETERS',
1820             				' P_Invoice_Id		= '||P_Invoice_Header_Rec.invoice_id||
1821 					' P_Error_Code		= '||P_Error_Code||
1822 					' P_Calling_Sequence 	= '||P_Calling_Sequence);
1823           FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
1824         END IF;
1825 
1826         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1827             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,sqlerrm);
1828         END IF;
1829 
1830         IF ( c_item_line%ISOPEN ) THEN
1831              CLOSE c_item_line;
1832         END IF;
1833 
1834         APP_EXCEPTION.RAISE_EXCEPTION;
1835     END;
1836 
1837     -------------------------------------------------------------------
1838     l_debug_info := 'Update total_tax_amount and self_assessed tax';
1839     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1840       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1841     END IF;
1842     -------------------------------------------------------------------
1843     BEGIN
1844       UPDATE ap_invoices_all ai
1845          SET (ai.total_tax_amount,
1846               ai.self_assessed_tax_amount) =
1847              (SELECT SUM(DECODE(NVL(zls.self_assessed_flag, 'N'),
1848                                 'N', NVL(zls.tax_amt, 0),
1849                                 0)),
1850                      SUM(DECODE(NVL(zls.self_assessed_flag, 'N'),
1851                                 'Y', NVL(zls.tax_amt, 0),
1852                                  0))
1853                 FROM zx_lines_summary zls
1854 	       WHERE zls.application_id   = 200
1855 		 AND zls.entity_code      = 'AP_INVOICES'
1856 		 AND zls.event_class_code IN ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
1857                  AND zls.trx_id 	= ai.invoice_id
1858                  AND NVL(zls.reporting_only_flag, 'N') = 'N')
1859        WHERE ai.invoice_id = P_Invoice_Header_Rec.invoice_id
1860        RETURNING ai.total_tax_amount, ai.self_assessed_tax_amount
1861             INTO l_total_tax_amount, l_self_assessed_tax_amt;
1862 
1863     EXCEPTION
1864       WHEN NO_DATA_FOUND THEN
1865         NULL;
1866 
1867       WHEN OTHERS THEN
1868         IF (SQLCODE <> -20001) THEN
1869           FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
1870           FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
1871           FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
1872           FND_MESSAGE.SET_TOKEN('PARAMETERS',
1873             ' P_Invoice_Id = '||P_Invoice_Header_Rec.invoice_id||
1874             ' P_Error_Code = '||P_Error_Code||
1875             ' P_Calling_Sequence = '||P_Calling_Sequence);
1876           FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
1877         END IF;
1878 
1879       APP_EXCEPTION.RAISE_EXCEPTION;
1880     END;
1881 
1882     -----------------------------------------------------------------
1883     l_debug_info := 'Update tax_already_calculated_flag';
1884     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1885       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1886     END IF;
1887     -----------------------------------------------------------------
1888     BEGIN
1889       UPDATE ap_invoice_lines_all ail
1890          SET ail.tax_already_calculated_flag = 'Y'
1891        WHERE ail.invoice_id = P_invoice_header_rec.invoice_id
1892          AND ail.line_type_lookup_code NOT IN ('TAX', 'AWT')
1893          AND NVL(ail.tax_already_calculated_flag, 'N') = 'N';
1894 
1895       /* Bug 5013526: We will set tax_already_calculated_flag to 'Y' even if tax lines are
1896 		      not generated. User could change any of the tax determining attributes
1897 		      on the invoice invoice line and resubmit tax calculation. In this case
1898 		      we would need to pass event_type as UPDATED.
1899          AND EXISTS
1900              ( SELECT zl.tax_line_id
1901                  FROM zx_lines zl
1902                 WHERE zl.trx_id = ail.invoice_id
1903                   AND zl.trx_line_id = ail.line_number
1904                   AND zl.application_id = 200
1905                   AND zl.entity_code = 'AP_INVOICES'
1906                   AND nvl(zl.reporting_only_flag, 'N') = 'N');
1907       */
1908 
1909     EXCEPTION
1910       WHEN no_data_found THEN
1911         null;
1912     END;
1913 
1914     -----------------------------------------------------------------
1915     l_debug_info := 'Update Invoice Includes Prepay Flag';
1916     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1917       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1918     END IF;
1919     -----------------------------------------------------------------
1920 
1921     UPDATE ap_invoice_lines_all tax
1922        SET tax.invoice_includes_prepay_flag = 'Y'
1923      WHERE tax.invoice_id		=  P_Invoice_Header_Rec.Invoice_Id
1924        AND tax.line_type_lookup_code	= 'TAX'
1925        AND EXISTS
1926            (SELECT 'Prepay App Exists'
1927             FROM   ap_invoice_lines_all prepay
1928             WHERE  prepay.invoice_id 		= tax.invoice_id
1929             AND    prepay.line_type_lookup_code = 'PREPAY'
1930             AND    prepay.prepay_invoice_id 	= tax.prepay_invoice_id
1931             AND    prepay.prepay_line_number 	= tax.prepay_line_number
1932             AND    prepay.invoice_includes_prepay_flag = 'Y');
1933 
1934     RETURN TRUE;
1935 
1936   EXCEPTION
1937     WHEN OTHERS THEN
1938       IF (SQLCODE <> -20001) THEN
1939         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
1940         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
1941         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
1942         FND_MESSAGE.SET_TOKEN('PARAMETERS',
1943           			' P_Invoice_Id = '      ||P_Invoice_Header_Rec.Invoice_Id||
1944           			' P_Error_Code = '      ||P_Error_Code||
1945           			' P_Calling_Sequence = '||P_Calling_Sequence);
1946         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
1947       END IF;
1948 
1949       APP_EXCEPTION.RAISE_EXCEPTION;
1950 
1951   END Return_Tax_Lines;
1952 
1953 
1954 /*=============================================================================
1955  |  FUNCTION - Return_Tax_Distributions()
1956  |
1957  |  DESCRIPTION
1958  |      This function handles the return of tax distributions.  It includes
1959  |      creation, update, or delete of existing distributions and TIPV and
1960  |      TERV distributions if required.
1961  |      It also handles the creation, update or delete of self-assessed
1962  |      distributions.
1963  |
1964  |  PARAMETERS
1965  |      P_Invoice_Header_Rec - Header info
1966  |      P_All_Error_Messages - Should API return 1 error message or allow
1967  |                             calling point to get them from message stack
1968  |      P_error_code - Error code to be returned
1969  |      P_calling_sequence -  Calling sequence
1970  |
1971  |  MODIFICATION HISTORY
1972  |    DATE          Author         Action
1973  |    23-OCT-2003   SYIDNER        Created
1974  |    05-MAR-2004   SYIDNER        Included changes for returning of tax
1975  |                                 distributions related to prepayment
1976  |                                 application distributions (PREPAY)
1977  |                                 In this case only the primary distribution
1978  |                                 should be created for the total value of
1979  |                                 the distribution (same as eTax.) No tax
1980  |                                 variances will be created for PREPAY type
1981  |                                 distributions
1982  *============================================================================*/
1983 
1984   FUNCTION Return_Tax_Distributions(
1985              P_Invoice_Header_Rec        IN  ap_invoices_all%ROWTYPE,
1986              P_All_Error_Messages        IN  VARCHAR2,
1987              P_Error_Code                OUT NOCOPY VARCHAR2,
1988              P_Calling_Sequence          IN  VARCHAR2) RETURN BOOLEAN
1989   IS
1990 
1991     l_debug_info		VARCHAR2(240);
1992     l_curr_calling_sequence	VARCHAR2(4000);
1993     l_api_name			CONSTANT VARCHAR2(100) := 'RETURN_TAX_DISTRIBUTIONS';
1994 
1995     l_dist_ccid_rec		zx_api_pub.distccid_det_facts_rec_type;
1996     l_dist_count		NUMBER;
1997 
1998     -- This record type is used for ap_invoice_distributions_all and ap_self_assessed_tax_dist_all
1999 
2000     TYPE r_upd_tax_dist_info IS RECORD (
2001       accounting_date 			ap_invoice_distributions_all.accounting_date%TYPE,
2002       dist_code_combination_id		ap_invoice_distributions_all.dist_code_combination_id%TYPE,
2003       line_type_lookup_code		ap_invoice_distributions_all.line_type_lookup_code%TYPE,
2004       period_name 			ap_invoice_distributions_all.period_name%TYPE,
2005       amount 				ap_invoice_distributions_all.amount%TYPE,
2006       base_amount 			ap_invoice_distributions_all.base_amount%TYPE,
2007       description 			ap_invoice_distributions_all.description%TYPE,
2008       income_tax_region 		ap_invoice_distributions_all.income_tax_region%TYPE,
2009       type_1099 			ap_invoice_distributions_all.type_1099%TYPE,
2010       attribute1 			ap_invoice_distributions_all.attribute1%TYPE,
2011       attribute10 			ap_invoice_distributions_all.attribute10%TYPE,
2012       attribute11 			ap_invoice_distributions_all.attribute11%TYPE,
2013       attribute12 			ap_invoice_distributions_all.attribute12%TYPE,
2014       attribute13 			ap_invoice_distributions_all.attribute13%TYPE,
2015       attribute14 			ap_invoice_distributions_all.attribute14%TYPE,
2016       attribute15 			ap_invoice_distributions_all.attribute15%TYPE,
2017       attribute2 			ap_invoice_distributions_all.attribute2%TYPE,
2018       attribute3 			ap_invoice_distributions_all.attribute3%TYPE,
2019       attribute4 			ap_invoice_distributions_all.attribute4%TYPE,
2020       attribute5 			ap_invoice_distributions_all.attribute5%TYPE,
2021       attribute6 			ap_invoice_distributions_all.attribute6%TYPE,
2022       attribute7 			ap_invoice_distributions_all.attribute7%TYPE,
2023       attribute8 			ap_invoice_distributions_all.attribute8%TYPE,
2024       attribute9 			ap_invoice_distributions_all.attribute9%TYPE,
2025       attribute_category 		ap_invoice_distributions_all.attribute_category%TYPE,
2026       expenditure_item_date		ap_invoice_distributions_all.expenditure_item_date%TYPE,
2027       expenditure_organization_id	ap_invoice_distributions_all.expenditure_organization_id%TYPE,
2028       expenditure_type 			ap_invoice_distributions_all.expenditure_type%TYPE,
2029       parent_invoice_id 		ap_invoice_distributions_all.parent_invoice_id%TYPE,
2030       pa_addition_flag 			ap_invoice_distributions_all.pa_addition_flag%TYPE,
2031       pa_quantity 			ap_invoice_distributions_all.pa_quantity%TYPE,
2032       project_accounting_context	ap_invoice_distributions_all.project_accounting_context%TYPE,
2033       project_id 			ap_invoice_distributions_all.project_id%TYPE,
2034       task_id 				ap_invoice_distributions_all.task_id%TYPE,
2035       awt_group_id 			ap_invoice_distributions_all.awt_group_id%TYPE,
2036       global_attribute_category		ap_invoice_distributions_all.global_attribute_category%TYPE,
2037       global_attribute1 		ap_invoice_distributions_all.global_attribute1%TYPE,
2038       global_attribute2 		ap_invoice_distributions_all.global_attribute2%TYPE,
2039       global_attribute3 		ap_invoice_distributions_all.global_attribute3%TYPE,
2040       global_attribute4 		ap_invoice_distributions_all.global_attribute4%TYPE,
2041       global_attribute5 		ap_invoice_distributions_all.global_attribute5%TYPE,
2042       global_attribute6 		ap_invoice_distributions_all.global_attribute6%TYPE,
2043       global_attribute7 		ap_invoice_distributions_all.global_attribute7%TYPE,
2044       global_attribute8 		ap_invoice_distributions_all.global_attribute8%TYPE,
2045       global_attribute9 		ap_invoice_distributions_all.global_attribute9%TYPE,
2046       global_attribute10 		ap_invoice_distributions_all.global_attribute10%TYPE,
2047       global_attribute11 		ap_invoice_distributions_all.global_attribute11%TYPE,
2048       global_attribute12 		ap_invoice_distributions_all.global_attribute12%TYPE,
2049       global_attribute13 		ap_invoice_distributions_all.global_attribute13%TYPE,
2050       global_attribute14 		ap_invoice_distributions_all.global_attribute14%TYPE,
2051       global_attribute15 		ap_invoice_distributions_all.global_attribute15%TYPE,
2052       global_attribute16 		ap_invoice_distributions_all.global_attribute16%TYPE,
2053       global_attribute17 		ap_invoice_distributions_all.global_attribute17%TYPE,
2054       global_attribute18 		ap_invoice_distributions_all.global_attribute18%TYPE,
2055       global_attribute19 		ap_invoice_distributions_all.global_attribute19%TYPE,
2056       global_attribute20 		ap_invoice_distributions_all.global_attribute20%TYPE,
2057       award_id 				ap_invoice_distributions_all.award_id%TYPE,
2058       dist_match_type 			ap_invoice_distributions_all.dist_match_type%TYPE,
2059       rcv_transaction_id 		ap_invoice_distributions_all.rcv_transaction_id%TYPE,
2060       tax_recoverable_flag		ap_invoice_distributions_all.tax_recoverable_flag%TYPE,
2061       cancellation_flag 		ap_invoice_distributions_all.cancellation_flag%TYPE,
2062       invoice_line_number		ap_invoice_distributions_all.invoice_line_number%TYPE,
2063       corrected_invoice_dist_id		ap_invoice_distributions_all.corrected_invoice_dist_id%TYPE,
2064       rounding_amt 			ap_invoice_distributions_all.rounding_amt%TYPE,
2065       charge_applicable_to_dist_id	ap_invoice_distributions_all.charge_applicable_to_dist_id%TYPE,
2066       distribution_class 		ap_invoice_distributions_all.distribution_class%TYPE,
2067       tax_code_id 			ap_invoice_distributions_all.tax_code_id%TYPE,
2068       detail_tax_dist_id 		ap_invoice_distributions_all.detail_tax_dist_id%TYPE,
2069       rec_nrec_rate 			ap_invoice_distributions_all.rec_nrec_rate%TYPE,
2070       recovery_rate_id 			ap_invoice_distributions_all.recovery_rate_id%TYPE,
2071       recovery_rate_name		ap_invoice_distributions_all.recovery_rate_name%TYPE,
2072       recovery_type_code 		ap_invoice_distributions_all.recovery_type_code%TYPE,
2073       taxable_amount 			ap_invoice_distributions_all.taxable_amount%TYPE,
2074       taxable_base_amount 		ap_invoice_distributions_all.taxable_base_amount%TYPE,
2075       summary_tax_line_id		ap_invoice_distributions_all.summary_tax_line_id%TYPE,
2076       extra_po_erv			ap_invoice_distributions_all.extra_po_erv%TYPE ,
2077       prepay_tax_diff_amount		ap_invoice_distributions_all.prepay_tax_diff_amount%TYPE,
2078       invoice_distribution_id		ap_invoice_distributions_all.invoice_distribution_id%TYPE,
2079       account_source_tax_rate_id	zx_rec_nrec_dist.account_source_tax_rate_id%TYPE);
2080 
2081 
2082     TYPE ins_tax_dist_type IS TABLE OF r_ins_tax_dist_info;
2083     TYPE upd_tax_dist_type IS TABLE OF r_upd_tax_dist_info;
2084     TYPE del_tax_dist_type IS TABLE OF ap_invoice_distributions_all.invoice_distribution_id%TYPE;
2085 
2086     l_inv_dist_ins		ins_tax_dist_type;
2087     l_inv_self_ins		ins_tax_dist_type;
2088 
2089     l_inv_dist_upd		upd_tax_dist_type;
2090     l_inv_self_upd		upd_tax_dist_type;
2091 
2092     l_inv_dist_del		del_tax_dist_type;
2093     l_inv_self_del		del_tax_dist_type;
2094 
2095 
2096     CURSOR insert_tax_dist IS
2097       SELECT /*+ leading(gt,zd) cardinality(gt,1) */
2098          zd.gl_date accounting_date , --Bug6809792
2099          /*For tax distributions accounting date will be stamped from zx_rec_nrec_dist
2100            Ebtax will now store accounting date of opne period in their table*/
2101          decode(NVL(zl.tax_only_line_flag,'N'),
2102                                'Y',parent_tax_line.default_dist_ccid,
2103                                 parent_taxable_dist.dist_code_combination_id) 			dist_code_combination_id, ---for 6010950
2104         -- this ccid is a temporary value that will be used if other
2105         -- conditions are met before inserting the tax distribution.
2106         DECODE(NVL(zd.recoverable_flag, 'N'),
2107                'Y', 'REC_TAX',
2108                'N', 'NONREC_TAX') 					line_type_lookup_code,
2109         ap_utilities_pkg.get_gl_period_name(zd.gl_date,ai.org_id)	period_name,
2110         -- included the decode as part of the prepayment changes.
2111         -- since for prepayment tax variances will not be created,
2112         -- the dist amount should be the total including variances
2113         DECODE(parent_item_line.line_type_lookup_code,
2114                'PREPAY', zd.rec_nrec_tax_amt,
2115                decode(parent_taxable_dist.line_type_lookup_code, 'PREPAY', zd.rec_nrec_tax_amt,
2116                decode(nvl(zd.recoverable_flag,'N'),
2117 			  'Y', zd.rec_nrec_tax_amt,
2118 				(NVL(zd.rec_nrec_tax_amt, 0) -
2119 					 get_tv(zd.rate_tax_factor, zd.trx_line_dist_qty, zd.per_unit_nrec_tax_amt,
2120 						nvl(zd.ref_doc_per_unit_nrec_tax_amt,0), zd.per_trx_curr_unit_nr_amt,
2121 						zd.ref_per_trx_curr_unit_nr_amt, zd.price_diff, parent_tax_line.corrected_inv_id,
2122 						parent_tax_line.line_type_lookup_code, parent_tax_line.line_source, ai.invoice_currency_code,
2123 						parent_item_line.match_type, zd.unit_price)))))
2124 										amount,
2125 	ap_utilities_pkg.ap_round_currency(
2126         DECODE(parent_item_line.line_type_lookup_code,
2127 		'PREPAY', zd.rec_nrec_tax_amt_funcl_curr,
2128                 decode(parent_taxable_dist.line_type_lookup_code, 'PREPAY', zd.rec_nrec_tax_amt_funcl_curr,
2129                 decode(nvl(zd.recoverable_flag,'N'),
2130 		       'Y', zd.rec_nrec_tax_amt_funcl_curr,
2131 			     (NVL(zd.rec_nrec_tax_amt_funcl_curr, 0) -
2132 				    (get_tv_base
2133 					(zd.rate_tax_factor, zd.trx_line_dist_qty, zd.per_unit_nrec_tax_amt, nvl(zd.ref_doc_per_unit_nrec_tax_amt,0),
2134 					 zd.per_trx_curr_unit_nr_amt, zd.ref_per_trx_curr_unit_nr_amt, nvl(zd.currency_conversion_rate, 1),
2135 					 zd.ref_doc_curr_conv_rate, zd.price_diff, parent_tax_line.corrected_inv_id,
2136 					 parent_tax_line.line_type_lookup_code, parent_tax_line.line_source, asp.base_currency_code,
2137 					 parent_item_line.match_type, zd.unit_price) +
2138 				     get_terv
2139 					(zd.trx_line_dist_qty, zd.currency_conversion_rate, zd.ref_doc_curr_conv_rate, zd.applied_to_doc_curr_conv_rate,
2140 					 NULL, zd.per_unit_nrec_tax_amt, nvl(zd.ref_doc_per_unit_nrec_tax_amt,zd.per_unit_nrec_tax_amt),
2141 					 parent_tax_line.corrected_inv_id, parent_tax_line.line_type_lookup_code, parent_tax_line.line_source,
2142 					 asp.base_currency_code)))))),
2143 					 asp.base_currency_code)		base_amount,
2144         -- included the decode as part of the prepayment changes.
2145         -- since for prepayments tax variances will not be created,
2146         -- the base_amount should be the total including variances
2147         DECODE(NVL(zd.inclusive_flag, 'N'),
2148 		'Y', parent_item_line.description,
2149 		'N', parent_tax_line.description) 				description,
2150         DECODE(NVL(zd.inclusive_flag, 'N'),
2151 		'Y', DECODE(parent_item_line.type_1099,
2152 			    NULL, NULL,
2153 			    parent_item_line.income_tax_region),
2154          	'N', DECODE(parent_tax_line.type_1099,
2155                 	    NULL, NULL,
2156 			    parent_tax_line.income_tax_region))			income_tax_region,
2157         DECODE(NVL(zd.recoverable_flag, 'N'),
2158 		'Y', NULL,
2159 		'N', parent_taxable_dist.po_distribution_id) 			po_distribution_id,
2160         DECODE(NVL(zd.inclusive_flag, 'N'),
2161 		'Y', parent_item_line.type_1099,
2162 		'N', parent_tax_line.type_1099) 				type_1099,
2163         zd.attribute1 								attribute1,
2164         zd.attribute10 								attribute10,
2165         zd.attribute11 								attribute11,
2166         zd.attribute12 								attribute12,
2167         zd.attribute13 								attribute13,
2168         zd.attribute14 								attribute14,
2169         zd.attribute15 								attribute15,
2170         zd.attribute2 								attribute2,
2171         zd.attribute3 								attribute3,
2172         zd.attribute4 								attribute4,
2173         zd.attribute5 								attribute5,
2174         zd.attribute6 								attribute6,
2175         zd.attribute7 								attribute7,
2176         zd.attribute8 								attribute8,
2177         zd.attribute9 								attribute9,
2178         zd.attribute_category 							attribute_category,
2179         DECODE(NVL(zd.recoverable_flag, 'N'),
2180 		'Y', NULL,
2181 		'N', parent_taxable_dist.expenditure_item_date)			expenditure_item_date,
2182         DECODE(NVL(zd.recoverable_flag, 'N'),
2183 		'Y', NULL,
2184 		'N', parent_taxable_dist.expenditure_organization_id)		expenditure_organization_id,
2185         DECODE(NVL(zd.recoverable_flag, 'N'),
2186 		'Y', NULL,
2187 		'N', parent_taxable_dist.expenditure_type) 			expenditure_type,
2188         parent_taxable_dist.parent_invoice_id 					parent_invoice_id,
2189         DECODE(NVL(zd.recoverable_flag, 'N'),
2190          	'Y', 'E',
2191          	'N', parent_taxable_dist.pa_addition_flag) 			pa_addition_flag,
2192         DECODE(NVL(zd.recoverable_flag, 'N'),
2193          	'Y', NULL,
2194          	'N', parent_taxable_dist.pa_quantity) pa_quantity,
2195         DECODE(NVL(zd.recoverable_flag, 'N'),
2196          	'Y', NULL,
2197          	'N', parent_taxable_dist.project_accounting_context)		project_accounting_context,
2198         DECODE(NVL(zd.recoverable_flag, 'N'),
2199 		'Y', NULL,
2200 		'N', parent_taxable_dist.project_id)				project_id,
2201         DECODE(NVL(zd.recoverable_flag, 'N'),
2202 		'Y', NULL,
2203 		'N', parent_taxable_dist.task_id) 				task_id,
2204         DECODE(NVL(asp.allow_awt_flag, 'N'),
2205                'Y', DECODE(NVL(pvs.allow_awt_flag, 'N'),
2206                       'Y', DECODE(NVL(asp.awt_include_tax_amt, 'N'),
2207                              'Y', DECODE(NVL(zd.inclusive_flag, 'N'),
2208                                     'Y', DECODE(zd.applied_from_tax_dist_id,
2209                                            null, parent_item_line.awt_group_id,
2210                                            ap_etax_utility_pkg.Get_Prepay_Awt_Group_Id(
2211                                              parent_taxable_dist.prepay_distribution_id,
2212                                              P_Calling_Sequence)),
2213                                     'N',parent_tax_line.awt_group_id), --Bug6648050
2214                              NULL),
2215                       NULL),
2216                NULL) 								awt_group_id,
2217               --Bug6505640 Populating DFF's from Invoice distributions instead of Tax dists
2218 		parent_taxable_dist.global_attribute_category 						global_attribute_category,
2219 		parent_taxable_dist.global_attribute1 							global_attribute1,
2220 		parent_taxable_dist.global_attribute2 							global_attribute2,
2221 		parent_taxable_dist.global_attribute3 							global_attribute3,
2222 		parent_taxable_dist.global_attribute4 							global_attribute4,
2223 		parent_taxable_dist.global_attribute5 							global_attribute5,
2224 		parent_taxable_dist.global_attribute6 							global_attribute6,
2225 		parent_taxable_dist.global_attribute7 							global_attribute7,
2226 		parent_taxable_dist.global_attribute8 							global_attribute8,
2227 		parent_taxable_dist.global_attribute9 							global_attribute9,
2228 		parent_taxable_dist.global_attribute10 							global_attribute10,
2229 		parent_taxable_dist.global_attribute11 							global_attribute11,
2230 		parent_taxable_dist.global_attribute12 							global_attribute12,
2231 		parent_taxable_dist.global_attribute13 							global_attribute13,
2232 		parent_taxable_dist.global_attribute14 							global_attribute14,
2233 		parent_taxable_dist.global_attribute15 							global_attribute15,
2234 		parent_taxable_dist.global_attribute16 							global_attribute16,
2235 		parent_taxable_dist.global_attribute17 							global_attribute17,
2236 		parent_taxable_dist.global_attribute18 							global_attribute18,
2237 		parent_taxable_dist.global_attribute19 							global_attribute19,
2238 		parent_taxable_dist.global_attribute20 							global_attribute20,
2239         DECODE(NVL(zd.recoverable_flag, 'N'),
2240 		'Y', NULL,
2241 		'N', parent_taxable_dist.award_id) 				award_id,
2242         DECODE(zd.ref_doc_dist_id,
2243 		NULL, DECODE(zl.applied_to_trx_id,
2244               			NULL, 'NOT_MATCHED',
2245               			'OTHER_TO_RECEIPT'),
2246 		'NOT_MATCHED') 							dist_match_type,
2247          DECODE(NVL(zd.recoverable_flag, 'N'),
2248 		'Y', NULL,
2249          	'N', parent_tax_line.rcv_transaction_id) 			rcv_transaction_id,
2250         zd.recoverable_flag 							tax_recoverable_flag,
2251         parent_taxable_dist.cancellation_flag 					cancellation_flag,
2252         DECODE(NVL(zd.inclusive_flag, 'N'),
2253 		'Y', zd.trx_line_id,
2254 		'N', nvl(parent_tax_line.line_number,
2255                          parent_taxable_dist.invoice_line_number)) 		invoice_line_number,
2256         parent_taxable_dist.corrected_invoice_dist_id 				corrected_invoice_dist_id,
2257         DECODE(NVL(zd.recoverable_flag, 'N'),
2258 		'Y', NULL,
2259 		zd.func_curr_rounding_adjustment) 				rounding_amt,
2260         -- the rounding amount in the non-recoverable case will be populated
2261         -- to the primary distribution later in the cycle.
2262         decode(NVL(zl.tax_only_line_flag,'N'),
2263                      'Y', NULL, zd.trx_line_dist_id)				charge_applicable_to_dist_id,
2264         DECODE(zl.ref_doc_trx_id,
2265           	NULL, 'CANDIDATE', 'PERMANENT') 				distribution_class,
2266         zd.tax_rate_id 								tax_code_id,
2267         zd.rec_nrec_tax_dist_id 						detail_tax_dist_id,
2268         zd.rec_nrec_rate 							rec_nrec_rate,
2269         zd.recovery_rate_id 							recovery_rate_id,
2270         zd.recovery_rate_code 							recovery_rate_name,
2271         zd.recovery_type_code 							recovery_type_code,
2272         zd.summary_tax_line_id 							summary_tax_line_id,
2273         null									extra_po_erv,
2274         zd.taxable_amt								taxable_amount,
2275         zd.taxable_amt_funcl_curr 						taxable_base_amount,
2276         pd.accrue_on_receipt_flag 						accrue_on_receipt_flag,
2277         asp.allow_flex_override_flag 						allow_flex_override_flag,
2278         fsp.purch_encumbrance_flag 						purch_encumbrance_flag,
2279         asp.org_id 								org_id,
2280         zd.tax_regime_id 							tax_regime_id,
2281         zd.tax_id 								tax_id,
2282         zd.tax_status_id 							tax_status_id,
2283         zl.tax_jurisdiction_id 							tax_jurisdiction_id,
2284         parent_taxable_dist.cancellation_flag 					parent_dist_cancellation_flag,
2285         parent_taxable_dist.reversal_flag 					parent_dist_reversal_flag,
2286         parent_taxable_dist.parent_reversal_id 					parent_dist_parent_reversal_id,
2287         zd.reversed_tax_dist_id 						reversed_tax_dist_id,
2288         zd.adjusted_doc_tax_dist_id 						adjusted_doc_tax_dist_id,
2289         zd.applied_from_tax_dist_id 						applied_from_tax_dist_id,
2290         -- the prepay_distribution_id will be populated with
2291         -- invoice_distribution_id for the associated rec or
2292         -- nonrec tax distributions
2293         DECODE(parent_item_line.line_type_lookup_code,
2294                'PREPAY', AP_ETAX_UTILITY_PKG.get_dist_id_for_tax_dist_id
2295                			(zd.applied_from_tax_dist_id),
2296                decode(parent_taxable_dist.line_type_lookup_code,
2297 		      'PREPAY', AP_ETAX_UTILITY_PKG.get_dist_id_for_tax_dist_id
2298                                        (zd.applied_from_tax_dist_id), NULL))	prepay_distribution_id,
2299         DECODE(parent_item_line.line_type_lookup_code,
2300                'PREPAY', DECODE(NVL(zd.recoverable_flag, 'N'),
2301 				'N', (zd.prd_tax_amt - zd.rec_nrec_tax_amt), NULL),
2302                NULL) 								prepay_tax_diff_amount,
2303        ai.invoice_id								invoice_id,
2304        ai.batch_id								batch_id,
2305        ai.set_of_books_id							set_of_books_id,
2306        zd.account_source_tax_rate_id						account_source_tax_rate_id
2307   FROM zx_trx_headers_gt                gt,
2308        zx_rec_nrec_dist                 zd,
2309        zx_lines                         zl,
2310        ap_invoices_all                  ai,
2311        ap_supplier_sites_all            pvs,
2312        ap_system_parameters_all         asp,
2313        financials_system_params_all     fsp,
2314        po_distributions_all             pd,
2315        ap_invoice_lines_all             parent_tax_line,
2316        ap_invoice_lines_all             parent_item_line,
2317        ap_invoice_distributions_all     parent_taxable_dist
2318  WHERE gt.APPLICATION_ID                        = zd.APPLICATION_ID
2319    AND gt.ENTITY_CODE                           = zd.ENTITY_CODE
2320    AND gt.EVENT_CLASS_CODE                      = zd.EVENT_CLASS_CODE
2321    AND gt.TRX_ID                                = zd.TRX_ID
2322    AND zd.tax_line_id                           = zl.tax_line_id
2323    AND gt.trx_id                                = ai.invoice_id
2324    AND ai.vendor_site_id                        = pvs.vendor_site_id
2325    AND ai.set_of_books_id                       = asp.set_of_books_id
2326    AND ai.org_id                                = asp.org_id
2327    AND asp.set_of_books_id                      = fsp.set_of_books_id
2328    AND asp.org_id                               = fsp.org_id
2329    AND NVL(zd.self_assessed_flag, 'N')          = 'N'
2330    AND NVL(zl.reporting_only_flag, 'N')         = 'N'
2331    AND parent_taxable_dist.po_distribution_id   = pd.po_distribution_id (+)
2332    AND zd.trx_id                                = parent_tax_line.invoice_id (+)
2333    AND zd.summary_tax_line_id                   = parent_tax_line.summary_tax_line_id (+)
2334    AND zd.trx_id                                = parent_item_line.invoice_id (+)
2335    AND zd.trx_line_id                           = parent_item_line.line_number (+)
2336    AND zd.trx_id                                = parent_taxable_dist.invoice_id (+)
2337    AND zd.trx_line_dist_id                      = parent_taxable_dist.invoice_distribution_id (+)
2338    AND (zd.ref_doc_application_id IS NULL
2339         or (zd.ref_doc_application_id IS NOT NULL
2340             and (nvl(zd.recoverable_flag, 'N') = 'Y'
2341                  or (parent_taxable_dist.prepay_distribution_id IS NOT NULL
2342                      and (parent_item_line.line_type_lookup_code IS NULL
2343 			  or parent_item_line.line_type_lookup_code <> 'PREPAY'))
2344 		 or ( zd.rec_nrec_tax_amt = 0
2345 		    and nvl(zd.recoverable_flag, 'N') = 'N'
2346 	/* Commented for Bug 6906867*/	/*    and zd.rec_nrec_rate = 100*/  )  -- added the condition for bug fix 6695517
2347                  or zd.rec_nrec_tax_amt <> ap_etax_utility_pkg.get_tv
2348                                             (zd.rate_tax_factor, zd.trx_line_dist_qty, zd.per_unit_nrec_tax_amt,
2349                                              nvl(zd.ref_doc_per_unit_nrec_tax_amt,0), zd.per_trx_curr_unit_nr_amt,
2350                                              zd.ref_per_trx_curr_unit_nr_amt, zd.price_diff, parent_tax_line.corrected_inv_id,
2351                                              parent_tax_line.line_type_lookup_code, parent_tax_line.line_source, ai.invoice_currency_code,
2352 					     parent_item_line.match_type, zd.unit_price))))
2353 --   AND ((zd.recoverable_flag = 'N'  AND zd.rec_nrec_rate<>0) OR zd.recoverable_flag = 'Y') --bug 6350100   -- commented out the condition for bug fix 6695517
2354    AND NOT EXISTS
2355       (SELECT aid.detail_tax_dist_id
2356          FROM ap_invoice_distributions_all aid
2357         WHERE aid.invoice_id            = zd.trx_id
2358           AND aid.detail_tax_dist_id    = zd.rec_nrec_tax_dist_id
2359           AND aid.line_type_lookup_code IN ('REC_TAX','NONREC_TAX'))
2360    AND (parent_taxable_dist.dist_match_type is null
2361         or nvl(zd.recoverable_flag, 'N') = 'Y'
2362         or parent_taxable_dist.dist_match_type <> 'PRICE_CORRECTION')
2363    AND (parent_taxable_dist.prepay_distribution_id IS NULL
2364         or (parent_taxable_dist.prepay_distribution_id IS NOT NULL
2365             and (parent_item_line.line_number IS NOT NULL
2366                  or zd.trx_line_id = (select -1 * (aid.invoice_id || aid.invoice_line_number || parent_taxable_dist.invoice_line_number)
2367                                         from ap_invoice_distributions_all aid
2368                                        where aid.invoice_distribution_id = parent_taxable_dist.prepay_distribution_id))))
2369    -- Bug 7462582
2370    -- Reverting the fixes done in bugs 6805527 and 7389822 as Etax bug 7515711 will take care of these fixes.
2371    /* Added by schitlap, epajaril to fix the issue in Bug 6805527 */
2372    /*AND (nvl(parent_taxable_dist.reversal_flag, 'N') <> 'Y'
2373         OR zd.reversed_tax_dist_id IS NULL) -- 7389822*/
2374   ORDER BY detail_tax_dist_id;
2375 
2376 
2377   CURSOR insert_tax_variances IS
2378 	SELECT /*+ leading(gt,zd) cardinality(gt,1) */
2379                 zd.gl_date accounting_date , --Bug6809792
2380                 /*For tax distributions accounting date will be stamped from zx_rec_nrec_dist
2381                   Ebtax will now store accounting date of opne period in their table*/
2382 		(CASE dist.line_type
2383 			WHEN 'TERV' THEN
2384 			    DECODE(pd.destination_type_code, 'EXPENSE', pd.code_combination_id,
2385 				   parent_taxable_dist.dist_code_combination_id)
2386 			ELSE
2387 			    DECODE(pd.destination_type_code, 'EXPENSE',
2388 				      DECODE(pd.accrue_on_receipt_flag, 'Y', pd.code_combination_id,
2389 				             parent_taxable_dist.dist_code_combination_id),
2390 				   pd.variance_account_id)
2391 		END)												dist_code_combination_id,
2392 	        (CASE dist.line_type
2393 			WHEN 'TIPV' THEN 'TIPV'
2394 			WHEN 'TERV' THEN 'TERV'
2395 			WHEN 'TRV'  THEN 'TRV'
2396 	        END)												line_type_lookup_code,
2397         	ap_utilities_pkg.get_gl_period_name(zd.gl_date,ai.org_id)					period_name,
2398 	        (CASE dist.line_type
2399                  WHEN 'TIPV' THEN
2400 		        decode(parent_taxable_dist.dist_match_type,
2401 				'PRICE_CORRECTION', zd.rec_nrec_tax_amt,
2402 				ap_etax_utility_pkg.get_tipv(
2403 					zd.rate_tax_factor, zd.trx_line_dist_qty,
2404 			                nvl2(parent_taxable_dist.rcv_transaction_id,
2405 						ap_etax_utility_pkg.get_converted_price
2406 						(parent_taxable_dist.invoice_distribution_id), zd.unit_price),
2407 					zd.ref_doc_unit_price, nvl(zd.ref_per_trx_curr_unit_nr_amt,zd.per_trx_curr_unit_nr_amt),
2408 					zd.price_diff, parent_tax_line.corrected_inv_id, parent_tax_line.line_type_lookup_code,
2409 					parent_tax_line.line_source, ai.invoice_currency_code,
2410 					parent_item_line.match_type))
2411 		 WHEN 'TRV' THEN
2412 		        (ap_etax_utility_pkg.get_tv(
2413 				zd.rate_tax_factor, zd.trx_line_dist_qty, zd.per_unit_nrec_tax_amt,
2414 				nvl(zd.ref_doc_per_unit_nrec_tax_amt,0), zd.per_trx_curr_unit_nr_amt,
2415 				zd.ref_per_trx_curr_unit_nr_amt, zd.price_diff, parent_tax_line.corrected_inv_id,
2416 				parent_tax_line.line_type_lookup_code, parent_tax_line.line_source, ai.invoice_currency_code,
2417 				parent_item_line.match_type, zd.unit_price) -
2418 		         ap_etax_utility_pkg.get_tipv(
2419 				zd.rate_tax_factor, zd.trx_line_dist_qty,
2420 		                nvl2(parent_taxable_dist.rcv_transaction_id,
2421 					ap_etax_utility_pkg.get_converted_price
2422 					(parent_taxable_dist.invoice_distribution_id), zd.unit_price),
2423 				zd.ref_doc_unit_price, nvl(zd.ref_per_trx_curr_unit_nr_amt,per_trx_curr_unit_nr_amt),
2424 				zd.price_diff, parent_tax_line.corrected_inv_id, parent_tax_line.line_type_lookup_code,
2425 				parent_tax_line.line_source, ai.invoice_currency_code, parent_item_line.match_type))
2426 		 WHEN 'TERV' THEN 0
2427 	         END)														amount,
2428 	        (CASE dist.line_type
2429 		 WHEN 'TIPV' THEN
2430 		        ap_utilities_pkg.ap_round_currency(
2431 	                        decode(parent_taxable_dist.dist_match_type,
2432 	                                'PRICE_CORRECTION', zd.rec_nrec_tax_amt,
2433 					ap_etax_utility_pkg.get_tipv_base(
2434 						zd.rate_tax_factor, zd.trx_line_dist_qty,
2435 				                nvl2(parent_taxable_dist.rcv_transaction_id,
2436 							ap_etax_utility_pkg.get_converted_price
2437 							(parent_taxable_dist.invoice_distribution_id), zd.unit_price),
2438 						zd.ref_doc_unit_price, nvl(zd.ref_per_trx_curr_unit_nr_amt,zd.per_trx_curr_unit_nr_amt),
2439 						zd.price_diff, zd.currency_conversion_rate, zd.ref_doc_curr_conv_rate, NULL,
2440 						parent_tax_line.corrected_inv_id, parent_tax_line.line_type_lookup_code,
2441 						parent_tax_line.line_source, ai.invoice_currency_code, asp.base_currency_code,
2442 						parent_item_line.match_type)),
2443 				asp.base_currency_code)
2444 		 WHEN 'TRV' THEN
2445 			ap_utilities_pkg.ap_round_currency(
2446 			(ap_etax_utility_pkg.get_tv_base(
2447 				zd.rate_tax_factor, zd.trx_line_dist_qty, zd.per_unit_nrec_tax_amt,
2448 				nvl(zd.ref_doc_per_unit_nrec_tax_amt,0), zd.per_trx_curr_unit_nr_amt,
2449 				zd.ref_per_trx_curr_unit_nr_amt, nvl(zd.currency_conversion_rate,1),
2450 				zd.ref_doc_curr_conv_rate, zd.price_diff, parent_tax_line.corrected_inv_id,
2451 				parent_tax_line.line_type_lookup_code, parent_tax_line.line_source, asp.base_currency_code,
2452 				parent_item_line.match_type, zd.unit_price) -
2453 			ap_etax_utility_pkg.get_tipv_base(
2454 				zd.rate_tax_factor, zd.trx_line_dist_qty,
2455 		                nvl2(parent_taxable_dist.rcv_transaction_id,
2456 					ap_etax_utility_pkg.get_converted_price
2457 					(parent_taxable_dist.invoice_distribution_id), zd.unit_price),
2458 				zd.ref_doc_unit_price, nvl(zd.ref_per_trx_curr_unit_nr_amt,zd.per_trx_curr_unit_nr_amt),
2459 				zd.price_diff, zd.currency_conversion_rate, zd.ref_doc_curr_conv_rate, NULL,
2460 				parent_tax_line.corrected_inv_id, parent_tax_line.line_type_lookup_code,
2461 				parent_tax_line.line_source, ai.invoice_currency_code, asp.base_currency_code,
2462 				parent_item_line.match_type) -
2463 			decode (parent_tax_line.match_type, 'PRICE_CORRECTION', 0,
2464 				ap_etax_utility_pkg.get_terv(
2465 					zd.trx_line_dist_qty, zd.currency_conversion_rate, zd.ref_doc_curr_conv_rate,
2466 					zd.applied_to_doc_curr_conv_rate, NULL, zd.per_unit_nrec_tax_amt,
2467 					nvl(zd.ref_doc_per_unit_nrec_tax_amt,zd.per_unit_nrec_tax_amt),
2468 					parent_tax_line.corrected_inv_id, parent_tax_line.line_type_lookup_code,
2469 					parent_tax_line.line_source, asp.base_currency_code))), asp.base_currency_code)
2470 		 WHEN 'TERV' THEN
2471 			ap_utilities_pkg.ap_round_currency
2472 			(ap_etax_utility_pkg.get_terv(
2473 					zd.trx_line_dist_qty, zd.currency_conversion_rate, zd.ref_doc_curr_conv_rate,
2474 					zd.applied_to_doc_curr_conv_rate, NULL, zd.per_unit_nrec_tax_amt,
2475 					nvl(zd.ref_doc_per_unit_nrec_tax_amt,zd.per_unit_nrec_tax_amt),
2476 					parent_tax_line.corrected_inv_id, parent_tax_line.line_type_lookup_code,
2477 					parent_tax_line.line_source, asp.base_currency_code), asp.base_currency_code)
2478 	        END)														base_amount,
2479         DECODE(NVL(zd.inclusive_flag, 'N'),
2480 		'Y', parent_item_line.description,
2481 		'N', parent_tax_line.description)							description,
2482         DECODE(NVL(zd.inclusive_flag, 'N'),
2483 		'Y', DECODE(parent_item_line.type_1099,
2484 			    NULL, NULL, parent_item_line.income_tax_region),
2485 		'N', DECODE(parent_tax_line.type_1099,
2486 			    NULL, NULL, parent_tax_line.income_tax_region))				income_tax_region,
2487         DECODE(NVL(zd.recoverable_flag, 'N'),
2488 		'Y', NULL,
2489 		'N', parent_taxable_dist.po_distribution_id)						po_distribution_id,
2490         DECODE(NVL(zd.inclusive_flag, 'N'),
2491 		'Y', parent_item_line.type_1099,
2492 		'N', parent_tax_line.type_1099) 							type_1099,
2493         zd.attribute1											attribute1,
2494         zd.attribute10 											attribute10,
2495         zd.attribute11 											attribute11,
2496         zd.attribute12 											attribute12,
2497         zd.attribute13 											attribute13,
2498         zd.attribute14 											attribute14,
2499         zd.attribute15 											attribute15,
2500         zd.attribute2 											attribute2,
2501         zd.attribute3 											attribute3,
2502         zd.attribute4 											attribute4,
2503         zd.attribute5 											attribute5,
2504         zd.attribute6 											attribute6,
2505         zd.attribute7 											attribute7,
2506         zd.attribute8 											attribute8,
2507         zd.attribute9 											attribute9,
2508         zd.attribute_category 										attribute_category,
2509         DECODE(NVL(zd.recoverable_flag, 'N'),
2510          	'Y', NULL,
2511          	'N', parent_taxable_dist.expenditure_item_date) 					expenditure_item_date,
2512         DECODE(NVL(zd.recoverable_flag, 'N'),
2513          	'Y', NULL,
2514          	'N', parent_taxable_dist.expenditure_organization_id)					expenditure_organization_id,
2515         DECODE(NVL(zd.recoverable_flag, 'N'),
2516 		'Y', NULL,
2517 		'N', parent_taxable_dist.expenditure_type)						expenditure_type,
2518         parent_taxable_dist.parent_invoice_id								parent_invoice_id,
2519         DECODE(NVL(zd.recoverable_flag, 'N'),
2520 		'Y', 'E',
2521 		'N', parent_taxable_dist.pa_addition_flag)						pa_addition_flag,
2522         DECODE(NVL(zd.recoverable_flag, 'N'),
2523          	'Y', NULL,
2524          	'N', parent_taxable_dist.pa_quantity) 							pa_quantity,
2525         DECODE(NVL(zd.recoverable_flag, 'N'),
2526          	'Y', NULL,
2527          	'N', parent_taxable_dist.project_accounting_context)					project_accounting_context,
2528         DECODE(NVL(zd.recoverable_flag, 'N'),
2529 		'Y', NULL,
2530 		'N', parent_taxable_dist.project_id)							project_id,
2531         DECODE(NVL(zd.recoverable_flag, 'N'),
2532          	'Y', NULL,
2533          	'N', parent_taxable_dist.task_id) 							task_id,
2534         DECODE(NVL(asp.allow_awt_flag, 'N'),
2535                'Y', DECODE(NVL(pvs.allow_awt_flag, 'N'),
2536                       'Y', DECODE(NVL(asp.awt_include_tax_amt, 'N'),
2537                              'Y', DECODE(NVL(zd.inclusive_flag, 'N'),
2538                                     'Y', DECODE(zd.applied_from_tax_dist_id,
2539                                            null, parent_item_line.awt_group_id,
2540                                            ap_etax_utility_pkg.Get_Prepay_Awt_Group_Id(
2541                                              parent_taxable_dist.prepay_distribution_id,
2542                                              P_Calling_Sequence)),
2543                              'N', parent_tax_line.awt_group_id),
2544                           NULL),
2545                       NULL),
2546                NULL) 											awt_group_id,
2547         zd.global_attribute_category									global_attribute_category,
2548         zd.global_attribute1										global_attribute1,
2549         zd.global_attribute2										global_attribute2,
2550         zd.global_attribute3										global_attribute3,
2551         zd.global_attribute4										global_attribute4,
2552         zd.global_attribute5										global_attribute5,
2553         zd.global_attribute6										global_attribute6,
2554         zd.global_attribute7										global_attribute7,
2555         zd.global_attribute8										global_attribute8,
2556         zd.global_attribute9										global_attribute9,
2557         zd.global_attribute10										global_attribute10,
2558         zd.global_attribute11										global_attribute11,
2559         zd.global_attribute12										global_attribute12,
2560         zd.global_attribute13										global_attribute13,
2561         zd.global_attribute14										global_attribute14,
2562         zd.global_attribute15										global_attribute15,
2563         zd.global_attribute16										global_attribute16,
2564         zd.global_attribute17										global_attribute17,
2565         zd.global_attribute18										global_attribute18,
2566         zd.global_attribute19										global_attribute19,
2567         zd.global_attribute20										global_attribute20,
2568         DECODE(NVL(zd.recoverable_flag, 'N'),
2569 		'Y', NULL,
2570 		'N', parent_taxable_dist.award_id) 							award_id,
2571         DECODE(zd.ref_doc_dist_id,
2572           	NULL, DECODE(zl.applied_to_trx_id,
2573 				NULL, 'NOT_MATCHED',
2574 				'OTHER_TO_RECEIPT'),
2575 		'NOT_MATCHED')										dist_match_type,
2576          DECODE(NVL(zd.recoverable_flag, 'N'),
2577          	'Y', NULL,
2578          	'N', parent_tax_line.rcv_transaction_id) 						rcv_transaction_id,
2579         zd.recoverable_flag 										tax_recoverable_flag,
2580         parent_taxable_dist.cancellation_flag 								cancellation_flag,
2581         DECODE(NVL(zd.inclusive_flag, 'N'),
2582          	'Y', zd.trx_line_id,
2583          	'N', parent_tax_line.line_number) 							invoice_line_number,
2584         parent_taxable_dist.corrected_invoice_dist_id 							corrected_invoice_dist_id,
2585         NULL rounding_amt,
2586         decode(NVL(zl.tax_only_line_flag,'N'),
2587                      'Y', NULL, zd.trx_line_dist_id)							charge_applicable_to_dist_id,
2588         DECODE(zl.ref_doc_trx_id,
2589           	NULL, 'CANDIDATE', 'PERMANENT') 							distribution_class,
2590         zd.tax_rate_id 											tax_code_id,
2591         zd.rec_nrec_tax_dist_id 									detail_tax_dist_id,
2592         zd.rec_nrec_rate 										rec_nrec_rate,
2593         zd.recovery_rate_id 										recovery_rate_id,
2594         zd.recovery_rate_code 										recovery_rate_name,
2595         zd.recovery_type_code 										recovery_type_code,
2596         zd.summary_tax_line_id 										summary_tax_line_id,
2597         NULL 												extra_po_erv,
2598         NULL 												taxable_amount,
2599         NULL 												taxable_base_amount,
2600         pd.accrue_on_receipt_flag 									accrue_on_receipt_flag,
2601         asp.allow_flex_override_flag 									allow_flex_override_flag,
2602         fsp.purch_encumbrance_flag 									purch_encumbrance_flag,
2603         asp.org_id 											org_id,
2604         zd.tax_regime_id 										tax_regime_id,
2605         zd.tax_id 											tax_id,
2606         zd.tax_status_id 										tax_status_id,
2607         zl.tax_jurisdiction_id 										tax_jurisdiction_id,
2608         parent_taxable_dist.cancellation_flag 								parent_dist_cancellation_flag,
2609         parent_taxable_dist.reversal_flag 								parent_dist_reversal_flag,
2610         parent_taxable_dist.parent_reversal_id 								parent_dist_parent_reversal_id,
2611         zd.reversed_tax_dist_id 									reversed_tax_dist_id,
2612         zd.adjusted_doc_tax_dist_id 									adjusted_doc_tax_dist_id,
2613         zd.applied_from_tax_dist_id  									applied_from_tax_dist_id,
2614         NULL 												prepay_distribution_id,
2615         NULL 												prepay_tax_diff_amount,
2616         ai.invoice_id                                                            			invoice_id,
2617         ai.batch_id                                                              			batch_id,
2618         ai.set_of_books_id                                                       			set_of_books_id,
2619         zd.account_source_tax_rate_id									account_source_tax_rate_id
2620   FROM zx_trx_headers_gt		gt,
2621        zx_rec_nrec_dist                 zd,
2622        zx_lines                         zl,
2623        ap_invoices_all                  ai,
2624        ap_supplier_sites_all            pvs,
2625        ap_system_parameters_all         asp,
2626        financials_system_params_all     fsp,
2627        po_distributions_all             pd,
2628        ap_invoice_lines_all             parent_tax_line,
2629        ap_invoice_lines_all             parent_item_line,
2630        ap_invoice_distributions_all     parent_taxable_dist,
2631        ap_line_temp_gt			dist
2632  WHERE gt.APPLICATION_ID                        = zd.APPLICATION_ID
2633    AND gt.ENTITY_CODE                           = zd.ENTITY_CODE
2634    AND gt.EVENT_CLASS_CODE                      = zd.EVENT_CLASS_CODE
2635    AND gt.TRX_ID                                = zd.TRX_ID
2636    AND zd.tax_line_id                           = zl.tax_line_id
2637    AND gt.trx_id                                = ai.invoice_id
2638    AND ai.vendor_site_id                        = pvs.vendor_site_id
2639    AND ai.set_of_books_id                       = asp.set_of_books_id
2640    AND ai.org_id                                = asp.org_id
2641    AND asp.set_of_books_id                      = fsp.set_of_books_id
2642    AND asp.org_id                               = fsp.org_id
2643    AND NVL(zd.recoverable_flag, 'N')    	= 'N'
2644    AND NVL(zd.self_assessed_flag, 'N')          = 'N'
2645    AND NVL(zl.reporting_only_flag, 'N')         = 'N'
2646    AND parent_taxable_dist.po_distribution_id   = pd.po_distribution_id(+)
2647    AND zd.trx_id                                = parent_tax_line.invoice_id (+)
2648    AND zd.summary_tax_line_id                   = parent_tax_line.summary_tax_line_id(+)
2649    AND zd.trx_id                                = parent_item_line.invoice_id(+)
2650    AND zd.trx_line_id                           = parent_item_line.line_number(+)
2651    AND zd.trx_id                                = parent_taxable_dist.invoice_id
2652    AND zd.trx_line_dist_id                      = parent_taxable_dist.invoice_distribution_id
2653    AND parent_item_line.line_type_lookup_code(+) <> 'PREPAY'
2654    AND parent_taxable_dist.line_type_lookup_code <> 'PREPAY'
2655    AND NOT EXISTS
2656       (SELECT aid.detail_tax_dist_id
2657          FROM ap_invoice_distributions_all aid
2658         WHERE aid.invoice_id            = zd.trx_id
2659           AND aid.detail_tax_dist_id    = zd.rec_nrec_tax_dist_id
2660           AND aid.line_type_lookup_code IN ('TIPV', 'TERV', 'TRV'))
2661    AND ((dist.line_type =  'TIPV'
2662 	 and (ap_etax_utility_pkg.get_tipv
2663 		(zd.rate_tax_factor, zd.trx_line_dist_qty,
2664 		 nvl2(parent_taxable_dist.rcv_transaction_id,
2665 			ap_etax_utility_pkg.get_converted_price
2666 			(parent_taxable_dist.invoice_distribution_id), zd.unit_price),
2667 		 zd.ref_doc_unit_price, nvl(zd.ref_per_trx_curr_unit_nr_amt,zd.per_trx_curr_unit_nr_amt),
2668                  zd.price_diff, parent_tax_line.corrected_inv_id, parent_tax_line.line_type_lookup_code,
2669 		 parent_tax_line.line_source, ai.invoice_currency_code, parent_item_line.match_type) <> 0
2670 	      or parent_taxable_dist.dist_match_type = 'PRICE_CORRECTION'
2671 	     ))
2672 	OR
2673 	(dist.line_type =  'TRV'
2674 	 and (ap_etax_utility_pkg.get_tv(
2675 			zd.rate_tax_factor, zd.trx_line_dist_qty, zd.per_unit_nrec_tax_amt,
2676 			nvl(zd.ref_doc_per_unit_nrec_tax_amt,0), zd.per_trx_curr_unit_nr_amt,
2677 			zd.ref_per_trx_curr_unit_nr_amt, zd.price_diff, parent_tax_line.corrected_inv_id,
2678 			parent_tax_line.line_type_lookup_code, parent_tax_line.line_source, ai.invoice_currency_code,
2679 			parent_item_line.match_type, zd.unit_price) -
2680 	        ap_etax_utility_pkg.get_tipv(
2681 			zd.rate_tax_factor, zd.trx_line_dist_qty,
2682                         nvl2(parent_taxable_dist.rcv_transaction_id,
2683                                 ap_etax_utility_pkg.get_converted_price
2684                                 (parent_taxable_dist.invoice_distribution_id), zd.unit_price),
2685 			zd.ref_doc_unit_price, nvl(zd.ref_per_trx_curr_unit_nr_amt,per_trx_curr_unit_nr_amt),
2686 			zd.price_diff, parent_tax_line.corrected_inv_id, parent_tax_line.line_type_lookup_code,
2687 			parent_tax_line.line_source, ai.invoice_currency_code, parent_item_line.match_type)) <> 0)
2688 	OR
2689 	(dist.line_type =  'TERV'
2690 	 and (ap_etax_utility_pkg.get_terv(
2691 			zd.trx_line_dist_qty, zd.currency_conversion_rate, zd.ref_doc_curr_conv_rate,
2692 			zd.applied_to_doc_curr_conv_rate, NULL, zd.per_unit_nrec_tax_amt,
2693 			nvl(zd.ref_doc_per_unit_nrec_tax_amt,zd.per_unit_nrec_tax_amt),
2694 			parent_tax_line.corrected_inv_id, parent_tax_line.line_type_lookup_code,
2695 			parent_tax_line.line_source, asp.base_currency_code)) <> 0))
2696     -- Bug 7462582
2697     -- Reverting the fixes done in bugs 6805527 and 7389822 as Etax bug 7515711 will take care of these fixes.
2698     /* Added by schitlap, epajaril to fix the issue in Bug 6805527 */
2699     /*AND (nvl(parent_taxable_dist.reversal_flag, 'N') <> 'Y'
2700         OR zd.reversed_tax_dist_id IS NULL) -- 7389822*/
2701   ORDER BY detail_tax_dist_id;
2702 
2703 
2704   CURSOR update_tax_dist IS
2705       SELECT /*+ leading(gt,zd) cardinality(gt,1) */
2706         zd.gl_date accounting_date , --Bug6809792
2707          /*For tax distributions accounting date will be stamped from zx_rec_nrec_dist
2708            Ebtax will now store accounting date of opne period in their table*/
2709         parent_taxable_dist.dist_code_combination_id dist_code_combination_id,
2710         -- this ccid is a temporary value that will be used if other conditions
2711         -- are met before inserting the tax distribution
2712         DECODE(NVL(zd.recoverable_flag, 'N'),
2713          'Y', 'REC_TAX',
2714          'N', 'NONREC_TAX') line_type_lookup_code,
2715         ap_utilities_pkg.get_gl_period_name(zd.gl_date,ai.org_id)	period_name,
2716         DECODE(parent_item_line.line_type_lookup_code,
2717                'PREPAY', zd.rec_nrec_tax_amt,
2718                decode(parent_taxable_dist.line_type_lookup_code, 'PREPAY', zd.rec_nrec_tax_amt,
2719                decode(nvl(zd.recoverable_flag,'N'),
2720                           'Y', zd.rec_nrec_tax_amt,
2721                                 (NVL(zd.rec_nrec_tax_amt, 0) -
2722                                          get_tv(zd.rate_tax_factor, zd.trx_line_dist_qty, zd.per_unit_nrec_tax_amt,
2723                                                 nvl(zd.ref_doc_per_unit_nrec_tax_amt,0), zd.per_trx_curr_unit_nr_amt,
2724                                                 zd.ref_per_trx_curr_unit_nr_amt, zd.price_diff, parent_tax_line.corrected_inv_id,
2725                                                 parent_tax_line.line_type_lookup_code, parent_tax_line.line_source, ai.invoice_currency_code,
2726 						parent_item_line.match_type, zd.unit_price)))))
2727 														amount,
2728         ap_utilities_pkg.ap_round_currency(
2729         DECODE(parent_item_line.line_type_lookup_code,
2730                 'PREPAY', zd.rec_nrec_tax_amt_funcl_curr,
2731                 decode(parent_taxable_dist.line_type_lookup_code, 'PREPAY', zd.rec_nrec_tax_amt_funcl_curr,
2732                 decode(nvl(zd.recoverable_flag,'N'),
2733                        'Y', zd.rec_nrec_tax_amt_funcl_curr,
2734                              (NVL(zd.rec_nrec_tax_amt_funcl_curr, 0) -
2735                                     (get_tv_base
2736                                         (zd.rate_tax_factor, zd.trx_line_dist_qty, zd.per_unit_nrec_tax_amt, nvl(zd.ref_doc_per_unit_nrec_tax_amt,0),
2737                                          zd.per_trx_curr_unit_nr_amt, zd.ref_per_trx_curr_unit_nr_amt, nvl(zd.currency_conversion_rate, 1),
2738                                          zd.ref_doc_curr_conv_rate, zd.price_diff, parent_tax_line.corrected_inv_id,
2739                                          parent_tax_line.line_type_lookup_code, parent_tax_line.line_source, asp.base_currency_code,
2740 					 parent_item_line.match_type, zd.unit_price) +
2741                                      get_terv
2742                                         (zd.trx_line_dist_qty, zd.currency_conversion_rate, zd.ref_doc_curr_conv_rate, zd.applied_to_doc_curr_conv_rate,
2743                                          NULL, zd.per_unit_nrec_tax_amt, nvl(zd.ref_doc_per_unit_nrec_tax_amt,zd.per_unit_nrec_tax_amt),
2744                                          parent_tax_line.corrected_inv_id, parent_tax_line.line_type_lookup_code, parent_tax_line.line_source,
2745 					 asp.base_currency_code)))))),
2746                                          asp.base_currency_code)                                                base_amount,
2747         -- included the decode as part of the prepayment changes.
2748         -- since for prepayment applic tax variances will not be created,
2749         -- the base_amount should be the total including variances
2750         DECODE(NVL(zd.inclusive_flag, 'N'),
2751          'Y', parent_item_line.description,
2752          'N', parent_tax_line.description) description,
2753         DECODE(NVL(zd.inclusive_flag, 'N'),
2754          'Y', DECODE(parent_item_line.type_1099,
2755                 NULL, NULL,
2756                 parent_item_line.income_tax_region),
2757          'N', DECODE(parent_tax_line.type_1099,
2758                 NULL, NULL,
2759                 parent_tax_line.income_tax_region)) income_tax_region,
2760         DECODE(NVL(zd.inclusive_flag, 'N'),
2761          'Y', parent_item_line.type_1099,
2762          'N', parent_tax_line.type_1099) type_1099,
2763 -- bug 6914575: modify start
2764 -- Populating DFF's from Invoice distributions instead of Tax dists
2765         aid.attribute1 attribute1,
2766         aid.attribute10 attribute10,
2767         aid.attribute11 attribute11,
2768         aid.attribute12 attribute12,
2769         aid.attribute13 attribute13,
2770         aid.attribute14 attribute14,
2771         aid.attribute15 attribute15,
2772         aid.attribute2 attribute2,
2773         aid.attribute3 attribute3,
2774         aid.attribute4 attribute4,
2775         aid.attribute5 attribute5,
2776         aid.attribute6 attribute6,
2777         aid.attribute7 attribute7,
2778         aid.attribute8 attribute8,
2779         aid.attribute9 attribute9,
2780         aid.attribute_category attribute_category,
2781 -- bug 6914575: modify end
2782         DECODE(NVL(zd.recoverable_flag, 'N'),
2783          'Y', NULL,
2784          'N', parent_taxable_dist.expenditure_item_date) expenditure_item_date,
2785         DECODE(NVL(zd.recoverable_flag, 'N'),
2786          'Y', NULL,
2787          'N', parent_taxable_dist.expenditure_organization_id)
2788            expenditure_organization_id,
2789         DECODE(NVL(zd.recoverable_flag, 'N'),
2790          'Y', NULL,
2791          'N', parent_taxable_dist.expenditure_type) expenditure_type,
2792         parent_taxable_dist.parent_invoice_id parent_invoice_id,
2793         DECODE(NVL(zd.recoverable_flag, 'N'),
2794          'Y', 'E',
2795          'N', parent_taxable_dist.pa_addition_flag) pa_addition_flag,
2796         DECODE(NVL(zd.recoverable_flag, 'N'),
2797          'Y', NULL,
2798          'N', parent_taxable_dist.pa_quantity) pa_quantity,
2799         DECODE(NVL(zd.recoverable_flag, 'N'),
2800          'Y', NULL,
2801          'N', parent_taxable_dist.project_accounting_context)
2802            project_accounting_context,
2803         DECODE(NVL(zd.recoverable_flag, 'N'),
2804          'Y', NULL,
2805          'N', parent_taxable_dist.project_id) project_id,
2806         DECODE(NVL(zd.recoverable_flag, 'N'),
2807          'Y', NULL,
2808          'N', parent_taxable_dist.task_id) task_id,
2809         aid.awt_group_id awt_group_id,
2810 	--Bug6505640 Populating DFF's from Invoice distributions instead of Tax dists
2811         aid.global_attribute_category global_attribute_category,
2812         aid.global_attribute1 global_attribute1,
2813         aid.global_attribute2 global_attribute2,
2814         aid.global_attribute3 global_attribute3,
2815         aid.global_attribute4 global_attribute4,
2816         aid.global_attribute5 global_attribute5,
2817         aid.global_attribute6 global_attribute6,
2818         aid.global_attribute7 global_attribute7,
2819         aid.global_attribute8 global_attribute8,
2820         aid.global_attribute9 global_attribute9,
2821         aid.global_attribute10 global_attribute10,
2822         aid.global_attribute11 global_attribute11,
2823         aid.global_attribute12 global_attribute12,
2824         aid.global_attribute13 global_attribute13,
2825         aid.global_attribute14 global_attribute14,
2826         aid.global_attribute15 global_attribute15,
2827         aid.global_attribute16 global_attribute16,
2828         aid.global_attribute17 global_attribute17,
2829         aid.global_attribute18 global_attribute18,
2830         aid.global_attribute19 global_attribute19,
2831         aid.global_attribute20 global_attribute20,
2832         DECODE(NVL(zd.recoverable_flag, 'N'),
2833          'Y', NULL,
2834          'N', parent_taxable_dist.award_id) award_id,
2835         DECODE(zd.ref_doc_dist_id,
2836           NULL, DECODE(zl.applied_to_trx_id,
2837               NULL, 'NOT_MATCHED',
2838               'OTHER_TO_RECEIPT'),
2839           'NOT_MATCHED') dist_match_type,
2840          DECODE(NVL(zd.recoverable_flag, 'N'),
2841          'Y', NULL,
2842          'N', parent_tax_line.rcv_transaction_id) rcv_transaction_id,
2843         zd.recoverable_flag tax_recoverable_flag,
2844         parent_taxable_dist.cancellation_flag cancellation_flag,
2845         DECODE(NVL(zd.inclusive_flag, 'N'),
2846          'Y', zd.trx_line_id,
2847          'N', nvl(parent_tax_line.line_number,
2848                   parent_taxable_dist.invoice_line_number)) invoice_line_number,
2849         parent_taxable_dist.corrected_invoice_dist_id corrected_invoice_dist_id,
2850         DECODE(NVL(zd.recoverable_flag, 'N'),
2851           'Y', NULL,
2852           zd.func_curr_rounding_adjustment) rounding_amt,
2853         -- This will update the rounding_amt in the recoverable dist
2854         -- for the non-recoverable the primary distribution will be
2855         -- updated later on
2856         decode(NVL(zl.tax_only_line_flag,'N'),
2857                      'Y', NULL, zd.trx_line_dist_id)	charge_applicable_to_dist_id,
2858         DECODE(zl.ref_doc_trx_id,
2859           NULL, 'CANDIDATE',
2860           'PERMANENT') distribution_class,
2861         zd.tax_rate_id tax_code_id,
2862         zd.rec_nrec_tax_dist_id detail_tax_dist_id,
2863         zd.rec_nrec_rate rec_nrec_rate,
2864         zd.recovery_rate_id recovery_rate_id,
2865         zd.recovery_rate_code recovery_rate_name,
2866         zd.recovery_type_code recovery_type_code,
2867         zd.taxable_amt taxable_amount,
2868         zd.taxable_amt_funcl_curr taxable_base_amount,
2869         zd.summary_tax_line_id summary_tax_line_id,
2870         null extra_po_erv,
2871         -- (zd.applied_to_doc_erv - zx.reference_doc_erv) null extra_po_erv,
2872         DECODE(parent_item_line.line_type_lookup_code,
2873           'PREPAY', DECODE(NVL(zd.recoverable_flag, 'N'),
2874                       'N', (zd.prd_tax_amt - zd.rec_nrec_tax_amt), NULL),
2875           NULL) prepay_tax_diff_amount,
2876         aid.invoice_distribution_id invoice_distribution_id,
2877         zd.account_source_tax_rate_id
2878   FROM  zx_trx_headers_gt		gt,
2879 	zx_rec_nrec_dist                zd,
2880         zx_lines                        zl,
2881 	ap_invoices_all                 ai,
2882         ap_invoice_distributions_all    aid,
2883         ap_supplier_sites_all           pvs,
2884         ap_system_parameters_all        asp,
2885         financials_system_params_all    fsp,
2886         po_distributions_all            pd,
2887         ap_invoice_lines_all            parent_tax_line,
2888         ap_invoice_lines_all            parent_item_line,
2889         ap_invoice_distributions_all    parent_taxable_dist
2890  WHERE  gt.APPLICATION_ID                        = zd.APPLICATION_ID
2891    AND  gt.ENTITY_CODE                           = zd.ENTITY_CODE
2892    AND  gt.EVENT_CLASS_CODE                      = zd.EVENT_CLASS_CODE
2893    AND  gt.TRX_ID                                = zd.TRX_ID
2894    AND  zd.tax_line_id                           = zl.tax_line_id
2895    AND  gt.trx_id                                = ai.invoice_id
2896    AND  ai.invoice_id                            = aid.invoice_id
2897    AND  aid.detail_tax_dist_id                   = zd.rec_nrec_tax_dist_id
2898    AND  aid.line_type_lookup_code                IN ('REC_TAX','NONREC_TAX')
2899    AND  nvl(aid.reversal_flag,'N')               <> 'Y'
2900    AND  ai.vendor_site_id                        = pvs.vendor_site_id
2901    AND  ai.set_of_books_id                       = asp.set_of_books_id
2902    AND  ai.org_id                                = asp.org_id
2903    AND  asp.set_of_books_id                      = fsp.set_of_books_id
2904    AND  asp.org_id                               = fsp.org_id
2905    AND  parent_taxable_dist.po_distribution_id   = pd.po_distribution_id(+)
2906    AND  zd.trx_id                                = parent_tax_line.invoice_id (+)
2907    AND  zd.summary_tax_line_id                   = parent_tax_line.summary_tax_line_id(+)
2908    AND  zd.trx_id                                = parent_item_line.invoice_id(+)
2909    AND  zd.trx_line_id                           = parent_item_line.line_number(+)
2910    AND  zd.trx_id                                = parent_taxable_dist.invoice_id(+)
2911    AND  zd.trx_line_dist_id                      = parent_taxable_dist.invoice_distribution_id(+);
2912 
2913 
2914   CURSOR update_tax_variances IS
2915 	SELECT /*+ leading(gt,zd) cardinality(gt,1) */
2916 	         zd.gl_date accounting_date , --Bug6809792
2917                  /*For tax distributions accounting date will be stamped from zx_rec_nrec_dist
2918                    Ebtax will now store accounting date of opne period in their table*/
2919                 (CASE aid.line_type_lookup_code
2920                         WHEN 'TERV' THEN
2921                             DECODE(pd.destination_type_code, 'EXPENSE', pd.code_combination_id,
2922                                    parent_taxable_dist.dist_code_combination_id)
2923                         ELSE
2924                             DECODE(pd.destination_type_code, 'EXPENSE',
2925                                       DECODE(pd.accrue_on_receipt_flag, 'Y', pd.code_combination_id,
2926                                              parent_taxable_dist.dist_code_combination_id),
2927                                    pd.variance_account_id)
2928                 END)                                                                                            dist_code_combination_id,
2929 	        (CASE aid.line_type_lookup_code
2930 			WHEN 'TIPV' THEN 'TIPV'
2931 			WHEN 'TERV' THEN 'TERV'
2932 			WHEN 'TRV'  THEN 'TRV'
2933 	        END)												line_type_lookup_code,
2934         	ap_utilities_pkg.get_gl_period_name(zd.gl_date,ai.org_id)					period_name,
2935                (CASE aid.line_type_lookup_code
2936                  WHEN 'TIPV' THEN
2937                        decode(parent_taxable_dist.dist_match_type,     -- Bug 5639076
2938                               'PRICE_CORRECTION', zd.rec_nrec_tax_amt,
2939 		                        ap_etax_utility_pkg.get_tipv(
2940    		                          zd.rate_tax_factor, zd.trx_line_dist_qty,
2941 		                          nvl2(parent_taxable_dist.rcv_transaction_id,
2942 		                                ap_etax_utility_pkg.get_converted_price
2943 		                                  (parent_taxable_dist.invoice_distribution_id), zd.unit_price),
2944                                                 zd.ref_doc_unit_price, nvl(zd.ref_per_trx_curr_unit_nr_amt,zd.per_trx_curr_unit_nr_amt),
2945 			                        zd.price_diff, parent_tax_line.corrected_inv_id, parent_tax_line.line_type_lookup_code,
2946 		                                parent_tax_line.line_source, ai.invoice_currency_code, parent_item_line.match_type))
2947 		 WHEN 'TRV' THEN
2948 		        (ap_etax_utility_pkg.get_tv(
2949 				zd.rate_tax_factor, zd.trx_line_dist_qty, zd.per_unit_nrec_tax_amt,
2950 				nvl(zd.ref_doc_per_unit_nrec_tax_amt,0), zd.per_trx_curr_unit_nr_amt,
2951 				zd.ref_per_trx_curr_unit_nr_amt, zd.price_diff, parent_tax_line.corrected_inv_id,
2952 				parent_tax_line.line_type_lookup_code, parent_tax_line.line_source, ai.invoice_currency_code,
2953 				parent_item_line.match_type, zd.unit_price) -
2954 		         ap_etax_utility_pkg.get_tipv(
2955 				zd.rate_tax_factor, zd.trx_line_dist_qty,
2956 	                        nvl2(parent_taxable_dist.rcv_transaction_id,
2957 	                                ap_etax_utility_pkg.get_converted_price
2958 	                                (parent_taxable_dist.invoice_distribution_id), zd.unit_price),
2959 				zd.ref_doc_unit_price, nvl(zd.ref_per_trx_curr_unit_nr_amt,per_trx_curr_unit_nr_amt),
2960 				zd.price_diff, parent_tax_line.corrected_inv_id, parent_tax_line.line_type_lookup_code,
2961 				parent_tax_line.line_source, ai.invoice_currency_code, parent_item_line.match_type))
2962 		 WHEN 'TERV' THEN 0
2963 	        END)												amount,
2964 	        (CASE aid.line_type_lookup_code
2965 		 WHEN 'TIPV' THEN
2966 		        ap_utilities_pkg.ap_round_currency(
2967 			ap_etax_utility_pkg.get_tipv_base(
2968 				zd.rate_tax_factor, zd.trx_line_dist_qty,
2969 	                        nvl2(parent_taxable_dist.rcv_transaction_id,
2970 	                                ap_etax_utility_pkg.get_converted_price
2971 	                                (parent_taxable_dist.invoice_distribution_id), zd.unit_price),
2972 				zd.ref_doc_unit_price, nvl(zd.ref_per_trx_curr_unit_nr_amt,zd.per_trx_curr_unit_nr_amt),
2973 				zd.price_diff, zd.currency_conversion_rate, zd.ref_doc_curr_conv_rate, NULL,
2974 				parent_tax_line.corrected_inv_id, parent_tax_line.line_type_lookup_code,
2975 				parent_tax_line.line_source, ai.invoice_currency_code, asp.base_currency_code,
2976 				parent_item_line.match_type),
2977 				asp.base_currency_code)
2978 		 WHEN 'TRV' THEN
2979 			ap_utilities_pkg.ap_round_currency(
2980 			(ap_etax_utility_pkg.get_tv_base(
2981 				zd.rate_tax_factor, zd.trx_line_dist_qty, zd.per_unit_nrec_tax_amt,
2982 				nvl(zd.ref_doc_per_unit_nrec_tax_amt,0), zd.per_trx_curr_unit_nr_amt,
2983 				zd.ref_per_trx_curr_unit_nr_amt, nvl(zd.currency_conversion_rate,1),
2984 				zd.ref_doc_curr_conv_rate, zd.price_diff, parent_tax_line.corrected_inv_id,
2985 				parent_tax_line.line_type_lookup_code, parent_tax_line.line_source, asp.base_currency_code,
2986 				parent_item_line.match_type, zd.unit_price) -
2987 			ap_etax_utility_pkg.get_tipv_base(
2988 				zd.rate_tax_factor, zd.trx_line_dist_qty,
2989 	                        nvl2(parent_taxable_dist.rcv_transaction_id,
2990 	                                ap_etax_utility_pkg.get_converted_price
2991 	                                (parent_taxable_dist.invoice_distribution_id), zd.unit_price),
2992 				zd.ref_doc_unit_price, nvl(zd.ref_per_trx_curr_unit_nr_amt,zd.per_trx_curr_unit_nr_amt),
2993 				zd.price_diff, zd.currency_conversion_rate, zd.ref_doc_curr_conv_rate, NULL,
2994 				parent_tax_line.corrected_inv_id, parent_tax_line.line_type_lookup_code,
2995 				parent_tax_line.line_source, ai.invoice_currency_code, asp.base_currency_code,
2996 				parent_item_line.match_type) -
2997 			decode (parent_tax_line.match_type, 'PRICE_CORRECTION', 0,
2998 				ap_etax_utility_pkg.get_terv(
2999 					zd.trx_line_dist_qty, zd.currency_conversion_rate, zd.ref_doc_curr_conv_rate,
3000 					zd.applied_to_doc_curr_conv_rate, NULL, zd.per_unit_nrec_tax_amt,
3001 					nvl(zd.ref_doc_per_unit_nrec_tax_amt,zd.per_unit_nrec_tax_amt),
3002 					parent_tax_line.corrected_inv_id, parent_tax_line.line_type_lookup_code,
3003 					parent_tax_line.line_source, asp.base_currency_code))), asp.base_currency_code)
3004 		 WHEN 'TERV' THEN
3005 			ap_utilities_pkg.ap_round_currency
3006 			(ap_etax_utility_pkg.get_terv(
3007 					zd.trx_line_dist_qty, zd.currency_conversion_rate, zd.ref_doc_curr_conv_rate,
3008 					zd.applied_to_doc_curr_conv_rate, NULL, zd.per_unit_nrec_tax_amt,
3009 					nvl(zd.ref_doc_per_unit_nrec_tax_amt,zd.per_unit_nrec_tax_amt),
3010 					parent_tax_line.corrected_inv_id, parent_tax_line.line_type_lookup_code,
3011 					parent_tax_line.line_source, asp.base_currency_code), asp.base_currency_code)
3012 	        END)												base_amount,
3013 	        DECODE(NVL(zd.inclusive_flag, 'N'),
3014 			'Y', parent_item_line.description,
3015 			'N', parent_tax_line.description) 							description,
3016 	        DECODE(NVL(zd.inclusive_flag, 'N'),
3017 			'Y', DECODE(parent_item_line.type_1099, NULL, NULL,
3018 					parent_item_line.income_tax_region),
3019 			'N', DECODE(parent_tax_line.type_1099, NULL, NULL,
3020 					parent_tax_line.income_tax_region))					income_tax_region,
3021 	        DECODE(NVL(zd.inclusive_flag, 'N'),
3022 			'Y', parent_item_line.type_1099,
3023 			'N', parent_tax_line.type_1099)								type_1099,
3024 	        zd.attribute1											attribute1,
3025 	        zd.attribute10 											attribute10,
3026 	        zd.attribute11 											attribute11,
3027 	        zd.attribute12 											attribute12,
3028 	        zd.attribute13 											attribute13,
3029 	        zd.attribute14 											attribute14,
3030 	        zd.attribute15 											attribute15,
3031 	        zd.attribute2 											attribute2,
3032 	        zd.attribute3 											attribute3,
3033 	        zd.attribute4 											attribute4,
3034 	        zd.attribute5 											attribute5,
3035 	        zd.attribute6 											attribute6,
3036 	        zd.attribute7 											attribute7,
3037 	        zd.attribute8 											attribute8,
3038 	        zd.attribute9 											attribute9,
3039 	        zd.attribute_category 										attribute_category,
3040 	        DECODE(NVL(zd.recoverable_flag, 'N'),
3041 			'Y', NULL, 'N', parent_taxable_dist.expenditure_item_date) 				expenditure_item_date,
3042 	        DECODE(NVL(zd.recoverable_flag, 'N'),
3043 			'Y', NULL, 'N', parent_taxable_dist.expenditure_organization_id) 			expenditure_organization_id,
3044 	        DECODE(NVL(zd.recoverable_flag, 'N'),
3045 			'Y', NULL, 'N', parent_taxable_dist.expenditure_type) 					expenditure_type,
3046 	        parent_taxable_dist.parent_invoice_id 								parent_invoice_id,
3047 	        DECODE(NVL(zd.recoverable_flag, 'N'),
3048 			'Y', 'E', 'N', parent_taxable_dist.pa_addition_flag) pa_addition_flag,
3049 	        DECODE(NVL(zd.recoverable_flag, 'N'),
3050 			'Y', NULL, 'N', parent_taxable_dist.pa_quantity) 					pa_quantity,
3051 	        DECODE(NVL(zd.recoverable_flag, 'N'),
3052 			'Y', NULL, 'N', parent_taxable_dist.project_accounting_context) 			project_accounting_context,
3053 	        DECODE(NVL(zd.recoverable_flag, 'N'),
3054 			'Y', NULL, 'N', parent_taxable_dist.project_id) 					project_id,
3055 	        DECODE(NVL(zd.recoverable_flag, 'N'),
3056 			'Y', NULL, 'N', parent_taxable_dist.task_id) 						task_id,
3057 	        aid.awt_group_id 										awt_group_id,
3058 	        zd.global_attribute_category									global_attribute_category,
3059 	        zd.global_attribute1										global_attribute1,
3060 	        zd.global_attribute2										global_attribute2,
3061 	        zd.global_attribute3										global_attribute3,
3062 	        zd.global_attribute4										global_attribute4,
3063 	        zd.global_attribute5										global_attribute5,
3064 	        zd.global_attribute6										global_attribute6,
3065 	        zd.global_attribute7										global_attribute7,
3066 	        zd.global_attribute8										global_attribute8,
3067 	        zd.global_attribute9										global_attribute9,
3068 	        zd.global_attribute10										global_attribute10,
3069 	        zd.global_attribute11										global_attribute11,
3070 	        zd.global_attribute12										global_attribute12,
3071 	        zd.global_attribute13										global_attribute13,
3072 	        zd.global_attribute14										global_attribute14,
3073 	        zd.global_attribute15										global_attribute15,
3074 	        zd.global_attribute16										global_attribute16,
3075 	        zd.global_attribute17										global_attribute17,
3076 	        zd.global_attribute18										global_attribute18,
3077 	        zd.global_attribute19										global_attribute19,
3078 	        zd.global_attribute20										global_attribute20,
3079 	        DECODE(NVL(zd.recoverable_flag, 'N'),
3080 			'Y', NULL, 'N', parent_taxable_dist.award_id) 						award_id,
3081 	        DECODE(zd.ref_doc_dist_id,
3082 			NULL, DECODE(zl.applied_to_trx_id,
3083 				NULL, 'NOT_MATCHED', 'OTHER_TO_RECEIPT'), 'NOT_MATCHED')			dist_match_type,
3084 	        DECODE(NVL(zd.recoverable_flag, 'N'),
3085 				'Y', NULL, 'N', parent_tax_line.rcv_transaction_id) 				rcv_transaction_id,
3086 	        zd.recoverable_flag										tax_recoverable_flag,
3087 	        parent_taxable_dist.cancellation_flag 								cancellation_flag,
3088 	        DECODE(NVL(zd.inclusive_flag, 'N'),
3089 				'Y', zd.trx_line_id, 'N', parent_tax_line.line_number)				invoice_line_number,
3090 	        parent_taxable_dist.corrected_invoice_dist_id							corrected_invoice_dist_id,
3091 	        NULL 												rounding_amt,
3092 	        decode(NVL(zl.tax_only_line_flag,'N'),
3093                          'Y', NULL, zd.trx_line_dist_id)							charge_applicable_to_dist_id,
3094 	        DECODE(zl.ref_doc_trx_id, NULL, 'CANDIDATE', 'PERMANENT') 					distribution_class,
3095 	        zd.tax_rate_id 											tax_code_id,
3096 	        zd.rec_nrec_tax_dist_id 									detail_tax_dist_id,
3097 	        zd.rec_nrec_rate 										rec_nrec_rate,
3098 	        zd.recovery_rate_id 										recovery_rate_id,
3099 	        zd.recovery_rate_code 										recovery_rate_name,
3100 	        zd.recovery_type_code 										recovery_type_code,
3101 	        NULL 												taxable_amount,
3102 	        NULL 												taxable_base_amount,
3103 	        zd.summary_tax_line_id 										summary_tax_line_id,
3104 	        NULL 												extra_po_erv,
3105 	        NULL 												prepay_tax_diff_amount,
3106 	        aid.invoice_distribution_id 									invoice_distribution_id,
3107 		zd.account_source_tax_rate_id									account_source_tax_rate_id
3108            FROM  zx_trx_headers_gt               gt,
3109 	        zx_rec_nrec_dist                zd,
3110 	        zx_lines                        zl,
3111 	        ap_invoices_all                 ai,
3112 	        ap_invoice_distributions_all    aid,
3113 	        ap_supplier_sites_all           pvs,
3114 	        ap_system_parameters_all        asp,
3115 	        financials_system_params_all    fsp,
3116 	        po_distributions_all            pd,
3117 	        ap_invoice_lines_all            parent_tax_line,
3118 	        ap_invoice_lines_all            parent_item_line,
3119 	        ap_invoice_distributions_all    parent_taxable_dist
3120 	 WHERE  gt.APPLICATION_ID                        = zd.APPLICATION_ID
3121 	   AND  gt.ENTITY_CODE                           = zd.ENTITY_CODE
3122 	   AND  gt.EVENT_CLASS_CODE                      = zd.EVENT_CLASS_CODE
3123 	   AND  gt.TRX_ID                                = zd.TRX_ID
3124 	   AND  zd.tax_line_id                           = zl.tax_line_id
3125 	   AND  gt.trx_id                                = ai.invoice_id
3126 	   AND  ai.invoice_id                            = aid.invoice_id
3127 	   AND  aid.detail_tax_dist_id                   = zd.rec_nrec_tax_dist_id
3128 	   AND  aid.line_type_lookup_code                IN ('TIPV', 'TRV', 'TERV')
3129            AND  nvl(aid.reversal_flag,'N')               <> 'Y'
3130 	   AND  ai.vendor_site_id                        = pvs.vendor_site_id
3131 	   AND  ai.set_of_books_id                       = asp.set_of_books_id
3132 	   AND  ai.org_id                                = asp.org_id
3133 	   AND  asp.set_of_books_id                      = fsp.set_of_books_id
3134 	   AND  asp.org_id                               = fsp.org_id
3135 	   AND  parent_taxable_dist.po_distribution_id   = pd.po_distribution_id(+)
3136 	   AND  zd.trx_id                                = parent_tax_line.invoice_id (+)
3137 	   AND  zd.summary_tax_line_id                   = parent_tax_line.summary_tax_line_id(+)
3138 	   AND  zd.trx_id                                = parent_item_line.invoice_id(+)
3139 	   AND  zd.trx_line_id                           = parent_item_line.line_number(+)
3140 	   AND  zd.trx_id                                = parent_taxable_dist.invoice_id(+)
3141 	   AND  zd.trx_line_dist_id                      = parent_taxable_dist.invoice_distribution_id(+)
3142 	   AND  parent_item_line.line_type_lookup_code(+) <> 'PREPAY';
3143 
3144 
3145   -- Cursors for self assessed distributions
3146   CURSOR insert_tax_self IS
3147       SELECT /*+ leading(gt,zd) cardinality(gt,1) INDEX (ZD ZX_REC_NREC_DIST_U3) */
3148         zd.gl_date accounting_date , --Bug6809792
3149         /*For tax distributions accounting date will be stamped from zx_rec_nrec_dist
3150           Ebtax will now store accounting date of opne period in their table*/
3151         parent_taxable_dist.dist_code_combination_id dist_code_combination_id,
3152         -- this ccid is a temporary value that will be used if other conditions
3153         -- are met before inserting the tax distribution
3154         DECODE(NVL(zd.recoverable_flag, 'N'),
3155          'Y', 'REC_TAX',
3156          'N', 'NONREC_TAX') line_type_lookup_code,
3157         ap_utilities_pkg.get_gl_period_name(zd.gl_date,ai.org_id)	period_name,
3158         zd.rec_nrec_tax_amt amount,
3159         ap_utilities_pkg.ap_round_currency
3160                 (zd.rec_nrec_tax_amt_funcl_curr,asp.base_currency_code) base_amount,
3161 	parent_item_line.description description,
3162 	DECODE(parent_item_line.type_1099,
3163                 NULL, NULL,
3164                 parent_item_line.income_tax_region) income_tax_region,
3165         DECODE(NVL(zd.recoverable_flag, 'N'),
3166          'Y', NULL,
3167          'N', parent_taxable_dist.po_distribution_id) po_distribution_id,
3168 	parent_item_line.type_1099 type_1099,
3169         zd.attribute1 attribute1,
3170         zd.attribute10 attribute10,
3171         zd.attribute11 attribute11,
3172         zd.attribute12 attribute12,
3173         zd.attribute13 attribute13,
3174         zd.attribute14 attribute14,
3175         zd.attribute15 attribute15,
3176         zd.attribute2 attribute2,
3177         zd.attribute3 attribute3,
3178         zd.attribute4 attribute4,
3179         zd.attribute5 attribute5,
3180         zd.attribute6 attribute6,
3181         zd.attribute7 attribute7,
3182         zd.attribute8 attribute8,
3183         zd.attribute9 attribute9,
3184         zd.attribute_category attribute_category,
3185         DECODE(NVL(zd.recoverable_flag, 'N'),
3186          'Y', NULL,
3187          'N', parent_taxable_dist.expenditure_item_date) expenditure_item_date,
3188         DECODE(NVL(zd.recoverable_flag, 'N'),
3189          'Y', NULL,
3190          'N', parent_taxable_dist.expenditure_organization_id)
3191            expenditure_organization_id,
3192         DECODE(NVL(zd.recoverable_flag, 'N'),
3193          'Y', NULL,
3194          'N', parent_taxable_dist.expenditure_type) expenditure_type,
3195         parent_taxable_dist.parent_invoice_id parent_invoice_id,
3196         DECODE(NVL(zd.recoverable_flag, 'N'),
3197          'Y', 'E',
3198          'N', parent_taxable_dist.pa_addition_flag) pa_addition_flag,
3199         DECODE(NVL(zd.recoverable_flag, 'N'),
3200          'Y', NULL,
3201          'N', parent_taxable_dist.pa_quantity) pa_quantity,
3202         DECODE(NVL(zd.recoverable_flag, 'N'),
3203          'Y', NULL,
3204          'N', parent_taxable_dist.project_accounting_context)
3205            project_accounting_context,
3206         DECODE(NVL(zd.recoverable_flag, 'N'),
3207          'Y', NULL,
3208          'N', parent_taxable_dist.project_id) project_id,
3209         DECODE(NVL(zd.recoverable_flag, 'N'),
3210          'Y', NULL,
3211          'N', parent_taxable_dist.task_id) task_id,
3212         DECODE(NVL(asp.allow_awt_flag, 'N'),
3213                'Y', DECODE(NVL(pvs.allow_awt_flag, 'N'),
3214                       'Y', DECODE(NVL(asp.awt_include_tax_amt, 'N'),
3215                              'Y', DECODE(NVL(zd.inclusive_flag, 'N'),
3216                                     'Y', DECODE(zd.applied_from_tax_dist_id,
3217                                            null, parent_item_line.awt_group_id,
3218                                            ap_etax_utility_pkg.Get_Prepay_Awt_Group_Id(
3219                                              parent_taxable_dist.prepay_distribution_id,
3220                                              P_Calling_Sequence)),
3221                                     'N', parent_taxable_dist.awt_group_id),
3222                              NULL),
3223                       NULL),
3224                NULL) awt_group_id,
3225         zd.global_attribute_category global_attribute_category,
3226         zd.global_attribute1 global_attribute1,
3227         zd.global_attribute2 global_attribute2,
3228         zd.global_attribute3 global_attribute3,
3229         zd.global_attribute4 global_attribute4,
3230         zd.global_attribute5 global_attribute5,
3231         zd.global_attribute6 global_attribute6,
3232         zd.global_attribute7 global_attribute7,
3233         zd.global_attribute8 global_attribute8,
3234         zd.global_attribute9 global_attribute9,
3235         zd.global_attribute10 global_attribute10,
3236         zd.global_attribute11 global_attribute11,
3237         zd.global_attribute12 global_attribute12,
3238         zd.global_attribute13 global_attribute13,
3239         zd.global_attribute14 global_attribute14,
3240         zd.global_attribute15 global_attribute15,
3241         zd.global_attribute16 global_attribute16,
3242         zd.global_attribute17 global_attribute17,
3243         zd.global_attribute18 global_attribute18,
3244         zd.global_attribute19 global_attribute19,
3245         zd.global_attribute20 global_attribute20,
3246         DECODE(NVL(zd.recoverable_flag, 'N'),
3247          'Y', NULL,
3248          'N', parent_taxable_dist.award_id) award_id,
3249         DECODE(zd.ref_doc_dist_id,
3250           NULL, DECODE(zl.applied_to_trx_id,
3251               NULL, 'NOT_MATCHED',
3252               'OTHER_TO_RECEIPT'),
3253           'NOT_MATCHED') dist_match_type,
3254          DECODE(NVL(zd.recoverable_flag, 'N'),
3255          'Y', NULL,
3256          'N', parent_item_line.rcv_transaction_id) rcv_transaction_id,
3257         zd.recoverable_flag tax_recoverable_flag,
3258         parent_taxable_dist.cancellation_flag cancellation_flag,
3259 	zd.trx_line_id invoice_line_number,
3260         parent_taxable_dist.corrected_invoice_dist_id corrected_invoice_dist_id,
3261         DECODE(NVL(zd.recoverable_flag, 'N'),
3262           'Y', NULL,
3263           zd.func_curr_rounding_adjustment) rounding_amt,
3264         -- the rounding amount in the non-recoverable case will be populated
3265         -- to the primary distribution later in the cycle.
3266         decode(NVL(zl.tax_only_line_flag,'N'),
3267                      'Y', NULL, zd.trx_line_dist_id) charge_applicable_to_dist_id,
3268         DECODE(zl.ref_doc_trx_id,
3269           NULL, 'CANDIDATE',
3270           'PERMANENT') distribution_class,
3271         zd.tax_rate_id tax_code_id,
3272         zd.rec_nrec_tax_dist_id detail_tax_dist_id,
3273         zd.rec_nrec_rate rec_nrec_rate,
3274         zd.recovery_rate_id recovery_rate_id,
3275         zd.recovery_rate_code recovery_rate_name,
3276         zd.recovery_type_code recovery_type_code,
3277         zd.summary_tax_line_id summary_tax_line_id,
3278         null extra_po_erv,
3279         zd.taxable_amt taxable_amount,
3280         zd.taxable_amt_funcl_curr taxable_base_amount,
3281         pd.accrue_on_receipt_flag accrue_on_receipt_flag,
3282         asp.allow_flex_override_flag allow_flex_override_flag,
3283         fsp.purch_encumbrance_flag purch_encumbrance_flag,
3284         asp.org_id org_id,
3285         zd.tax_regime_id tax_regime_id,
3286         zd.tax_id tax_id,
3287         zd.tax_status_id tax_status_id,
3288         zl.tax_jurisdiction_id tax_jurisdiction_id,
3289         parent_taxable_dist.cancellation_flag parent_dist_cancellation_flag,
3290         parent_taxable_dist.reversal_flag parent_dist_reversal_flag,
3291         parent_taxable_dist.parent_reversal_id parent_dist_parent_reversal_id,
3292         zd.reversed_tax_dist_id reversed_tax_dist_id,
3293         zd.adjusted_doc_tax_dist_id adjusted_doc_tax_dist_id,
3294         zd.applied_from_tax_dist_id applied_from_tax_dist_id,
3295         -- the prepay_distribution_id will be populated with
3296         -- invoice_distribution_id for the TAX rec or nonrec
3297         -- asociated
3298         DECODE(parent_item_line.line_type_lookup_code,
3299                'PREPAY',
3300                AP_ETAX_UTILITY_PKG.Get_Dist_Id_For_Tax_Dist_Id(
3301                  zd.applied_from_tax_dist_id),
3302                NULL) prepay_distribution_id,
3303         DECODE(parent_item_line.line_type_lookup_code,
3304                'PREPAY', DECODE(NVL(zd.recoverable_flag, 'N'),
3305                            'N', (zd.prd_tax_amt - zd.rec_nrec_tax_amt), NULL),
3306                NULL) prepay_tax_diff_amount,
3307        ai.invoice_id     	invoice_id,
3308        ai.batch_id              batch_id,
3309        ai.set_of_books_id       set_of_books_id,
3310        zd.account_source_tax_rate_id	account_source_tax_rate_id
3311   FROM zx_trx_headers_gt                gt,
3312        zx_rec_nrec_dist                 zd,
3313        zx_lines                         zl,
3314        ap_invoices_all                  ai,
3315        ap_supplier_sites_all            pvs,
3316        ap_system_parameters_all         asp,
3317        financials_system_params_all     fsp,
3318        po_distributions_all             pd,
3319        ap_invoice_lines_all             parent_item_line,
3320        ap_invoice_distributions_all     parent_taxable_dist
3321  WHERE gt.APPLICATION_ID                        = zd.APPLICATION_ID
3322    AND gt.ENTITY_CODE                           = zd.ENTITY_CODE
3323    AND gt.EVENT_CLASS_CODE                      = zd.EVENT_CLASS_CODE
3324    AND gt.TRX_ID                                = zd.TRX_ID
3325    AND zd.tax_line_id                           = zl.tax_line_id
3326    AND gt.trx_id                                = ai.invoice_id
3327    AND ai.vendor_site_id                        = pvs.vendor_site_id
3328    AND ai.set_of_books_id                       = asp.set_of_books_id
3329    AND ai.org_id                                = asp.org_id
3330    AND asp.set_of_books_id                      = fsp.set_of_books_id
3331    AND asp.org_id                               = fsp.org_id
3332    AND NVL(zd.self_assessed_flag,  'N')         = 'Y'
3333    AND NVL(zl.reporting_only_flag, 'N')         = 'N'
3334    AND parent_taxable_dist.po_distribution_id   = pd.po_distribution_id(+)
3335    AND zd.trx_id                                = parent_item_line.invoice_id(+)
3336    AND zd.trx_line_id                           = parent_item_line.line_number(+)
3337    AND zd.trx_id                                = parent_taxable_dist.invoice_id(+)
3338    AND zd.trx_line_dist_id                      = parent_taxable_dist.invoice_distribution_id(+)
3339    AND NOT EXISTS
3340       (SELECT aid.detail_tax_dist_id
3341          FROM ap_self_assessed_tax_dist_all aid
3342         WHERE aid.invoice_id            = ai.invoice_id
3343           AND aid.detail_tax_dist_id    = zd.rec_nrec_tax_dist_id
3344           AND aid.line_type_lookup_code IN ('REC_TAX','NONREC_TAX'))
3345    -- Bug 7462582
3346    -- Reverting the fixes done in bugs 6805527 and 7389822 as Etax bug 7515711 will take care of these fixes.
3347    /* Added by schitlap, epajaril to fix the issue in Bug 6805527 */
3348    /*AND (nvl(parent_taxable_dist.reversal_flag, 'N') <> 'Y'
3349         OR zd.reversed_tax_dist_id IS NULL) -- 7389822*/
3350   ORDER BY detail_tax_dist_id;
3351 
3352 
3353   CURSOR update_tax_self IS
3354       SELECT /*+ leading(gt,zd) cardinality(gt,1) */
3355         zd.gl_date accounting_date , --Bug6809792
3356         /*For tax distributions accounting date will be stamped from zx_rec_nrec_dist
3357           Ebtax will now store accounting date of opne period in their table*/
3358         parent_taxable_dist.dist_code_combination_id dist_code_combination_id,
3359         -- this ccid is a temporary value that will be used if other conditions
3360         -- are met before inserting the tax distribution
3361         DECODE(NVL(zd.recoverable_flag, 'N'),
3362          'Y', 'REC_TAX',
3363          'N', 'NONREC_TAX') line_type_lookup_code,
3364         ap_utilities_pkg.get_gl_period_name(zd.gl_date,ai.org_id)	period_name,
3365 	zd.rec_nrec_tax_amt amount,
3366         ap_utilities_pkg.ap_round_currency
3367 		(zd.rec_nrec_tax_amt_funcl_curr,asp.base_currency_code) base_amount,
3368         -- included the decode as part of the prepayment changes.
3369         -- since for prepayment applic tax variances will not be created,
3370         -- the base_amount should be the total including variances
3371         DECODE(NVL(zd.inclusive_flag, 'N'),
3372          'Y', parent_item_line.description,
3373          'N', parent_tax_line.description) description,
3374         DECODE(NVL(zd.inclusive_flag, 'N'),
3375          'Y', DECODE(parent_item_line.type_1099,
3376                 NULL, NULL,
3377                 parent_item_line.income_tax_region),
3378          'N', DECODE(parent_tax_line.type_1099,
3379                 NULL, NULL,
3380                 parent_tax_line.income_tax_region)) income_tax_region,
3381         DECODE(NVL(zd.inclusive_flag, 'N'),
3382          'Y', parent_item_line.type_1099,
3383          'N', parent_tax_line.type_1099) type_1099,
3384         zd.attribute1 attribute1,
3385         zd.attribute10 attribute10,
3386         zd.attribute11 attribute11,
3387         zd.attribute12 attribute12,
3388         zd.attribute13 attribute13,
3389         zd.attribute14 attribute14,
3390         zd.attribute15 attribute15,
3391         zd.attribute2 attribute2,
3392         zd.attribute3 attribute3,
3393         zd.attribute4 attribute4,
3394         zd.attribute5 attribute5,
3395         zd.attribute6 attribute6,
3396         zd.attribute7 attribute7,
3397         zd.attribute8 attribute8,
3398         zd.attribute9 attribute9,
3399         zd.attribute_category attribute_category,
3400         DECODE(NVL(zd.recoverable_flag, 'N'),
3401          'Y', NULL,
3402          'N', parent_taxable_dist.expenditure_item_date) expenditure_item_date,
3403         DECODE(NVL(zd.recoverable_flag, 'N'),
3404          'Y', NULL,
3405          'N', parent_taxable_dist.expenditure_organization_id)
3406            expenditure_organization_id,
3407         DECODE(NVL(zd.recoverable_flag, 'N'),
3408          'Y', NULL,
3409          'N', parent_taxable_dist.expenditure_type) expenditure_type,
3410         parent_taxable_dist.parent_invoice_id parent_invoice_id,
3411         DECODE(NVL(zd.recoverable_flag, 'N'),
3412          'Y', 'E',
3413          'N', parent_taxable_dist.pa_addition_flag) pa_addition_flag,
3414         DECODE(NVL(zd.recoverable_flag, 'N'),
3415          'Y', NULL,
3416          'N', parent_taxable_dist.pa_quantity) pa_quantity,
3417         DECODE(NVL(zd.recoverable_flag, 'N'),
3418          'Y', NULL,
3419          'N', parent_taxable_dist.project_accounting_context)
3420            project_accounting_context,
3421         DECODE(NVL(zd.recoverable_flag, 'N'),
3422          'Y', NULL,
3423          'N', parent_taxable_dist.project_id) project_id,
3424         DECODE(NVL(zd.recoverable_flag, 'N'),
3425          'Y', NULL,
3426          'N', parent_taxable_dist.task_id) task_id,
3427         DECODE(NVL(asp.allow_awt_flag, 'N'),
3428                'Y', DECODE(NVL(pvs.allow_awt_flag, 'N'),
3429                       'Y', DECODE(NVL(asp.awt_include_tax_amt, 'N'),
3430                              'Y', DECODE(NVL(zd.inclusive_flag, 'N'),
3431                                     'Y', DECODE(zd.applied_from_tax_dist_id,
3432                                            null, parent_item_line.awt_group_id,
3433                                            ap_etax_utility_pkg.Get_Prepay_Awt_Group_Id(
3434                                              parent_taxable_dist.prepay_distribution_id,
3435                                              P_Calling_Sequence)),
3436                                     'N', parent_taxable_dist.awt_group_id),
3437                              NULL),
3438                       NULL),
3439                NULL) awt_group_id,
3440         zd.global_attribute_category global_attribute_category,
3441         zd.global_attribute1 global_attribute1,
3442         zd.global_attribute2 global_attribute2,
3443         zd.global_attribute3 global_attribute3,
3444         zd.global_attribute4 global_attribute4,
3445         zd.global_attribute5 global_attribute5,
3446         zd.global_attribute6 global_attribute6,
3447         zd.global_attribute7 global_attribute7,
3448         zd.global_attribute8 global_attribute8,
3449         zd.global_attribute9 global_attribute9,
3450         zd.global_attribute10 global_attribute10,
3451         zd.global_attribute11 global_attribute11,
3452         zd.global_attribute12 global_attribute12,
3453         zd.global_attribute13 global_attribute13,
3454         zd.global_attribute14 global_attribute14,
3455         zd.global_attribute15 global_attribute15,
3456         zd.global_attribute16 global_attribute16,
3457         zd.global_attribute17 global_attribute17,
3458         zd.global_attribute18 global_attribute18,
3459         zd.global_attribute19 global_attribute19,
3460         zd.global_attribute20 global_attribute20,
3461         DECODE(NVL(zd.recoverable_flag, 'N'),
3462          'Y', NULL,
3463          'N', parent_taxable_dist.award_id) award_id,
3464         DECODE(zd.ref_doc_dist_id,
3465           NULL, DECODE(zl.applied_to_trx_id,
3466               NULL, 'NOT_MATCHED',
3467               'OTHER_TO_RECEIPT'),
3468           'NOT_MATCHED') dist_match_type,
3469          DECODE(NVL(zd.recoverable_flag, 'N'),
3470          'Y', NULL,
3471          'N', parent_tax_line.rcv_transaction_id) rcv_transaction_id,
3472         zd.recoverable_flag tax_recoverable_flag,
3473         parent_taxable_dist.cancellation_flag cancellation_flag,
3474         DECODE(NVL(zd.inclusive_flag, 'N'),
3475          'Y', zd.trx_line_id,
3476          'N', parent_tax_line.line_number) invoice_line_number,
3477         parent_taxable_dist.corrected_invoice_dist_id corrected_invoice_dist_id,
3478         DECODE(NVL(zd.recoverable_flag, 'N'),
3479           'Y', NULL,
3480           zd.func_curr_rounding_adjustment) rounding_amt,
3481         -- This will update the rounding_amt in the recoverable dist
3482         -- for the non-recoverable the primary distribution will be
3483         -- updated later on
3484         decode(NVL(zl.tax_only_line_flag,'N'),
3485                      'Y', NULL, zd.trx_line_dist_id) charge_applicable_to_dist_id,
3486         DECODE(zl.ref_doc_trx_id,
3487           NULL, 'CANDIDATE',
3488           'PERMANENT') distribution_class,
3489         zd.tax_rate_id tax_code_id,
3490         zd.rec_nrec_tax_dist_id detail_tax_dist_id,
3491         zd.rec_nrec_rate rec_nrec_rate,
3492         zd.recovery_rate_id recovery_rate_id,
3493         zd.recovery_rate_code recovery_rate_name,
3494         zd.recovery_type_code recovery_type_code,
3495         zd.taxable_amt taxable_amount,
3496         zd.taxable_amt_funcl_curr taxable_base_amount,
3497         zd.summary_tax_line_id summary_tax_line_id,
3498         null extra_po_erv,
3499         -- (zd.applied_to_doc_erv - zx.reference_doc_erv) null extra_po_erv,
3500         DECODE(parent_item_line.line_type_lookup_code,
3501           'PREPAY', DECODE(NVL(zd.recoverable_flag, 'N'),
3502                       'N', (zd.prd_tax_amt - zd.rec_nrec_tax_amt), NULL),
3503           NULL) prepay_tax_diff_amount,
3504         aid.invoice_distribution_id invoice_distribution_id,
3505         zd.account_source_tax_rate_id
3506   FROM  zx_trx_headers_gt               gt,
3507         zx_rec_nrec_dist                zd,
3508         zx_lines                        zl,
3509         ap_invoices_all                 ai,
3510         ap_self_assessed_tax_dist_all	aid,
3511         ap_supplier_sites_all           pvs,
3512         ap_system_parameters_all        asp,
3513         financials_system_params_all    fsp,
3514         po_distributions_all            pd,
3515         ap_invoice_lines_all            parent_tax_line,
3516         ap_invoice_lines_all            parent_item_line,
3517         ap_invoice_distributions_all    parent_taxable_dist
3518  WHERE  gt.APPLICATION_ID                        = zd.APPLICATION_ID
3519    AND  gt.ENTITY_CODE                           = zd.ENTITY_CODE
3520    AND  gt.EVENT_CLASS_CODE                      = zd.EVENT_CLASS_CODE
3521    AND  gt.TRX_ID                                = zd.TRX_ID
3522    AND  zd.tax_line_id                           = zl.tax_line_id
3523    AND  gt.trx_id                                = ai.invoice_id
3524    AND  ai.invoice_id                            = aid.invoice_id
3525    AND  aid.detail_tax_dist_id                   = zd.rec_nrec_tax_dist_id
3526    AND  aid.line_type_lookup_code                IN ('REC_TAX','NONREC_TAX')
3527    AND  nvl(aid.reversal_flag,'N')               <> 'Y'
3528    AND  ai.vendor_site_id                        = pvs.vendor_site_id
3529    AND  ai.set_of_books_id                       = asp.set_of_books_id
3530    AND  ai.org_id                                = asp.org_id
3531    AND  asp.set_of_books_id                      = fsp.set_of_books_id
3532    AND  asp.org_id                               = fsp.org_id
3533    AND  parent_taxable_dist.po_distribution_id   = pd.po_distribution_id(+)
3534    AND  zd.trx_id                                = parent_tax_line.invoice_id (+)
3535    AND  zd.summary_tax_line_id                   = parent_tax_line.summary_tax_line_id(+)
3536    AND  zd.trx_id                                = parent_item_line.invoice_id(+)
3537    AND  zd.trx_line_id                           = parent_item_line.line_number(+)
3538    AND  zd.trx_id                                = parent_taxable_dist.invoice_id(+)
3539    AND  zd.trx_line_dist_id                      = parent_taxable_dist.invoice_distribution_id(+);
3540 
3541 
3542   l_dist_code_combination_id	ap_invoice_distributions_all.dist_code_combination_id%TYPE;
3543   l_allow_pa_override		VARCHAR2(1);
3544 
3545   -- Variables for the eTax API to get the default ccids
3546 
3547   l_return_status_service       VARCHAR2(4000);
3548   l_msg_count                   NUMBER;
3549   l_msg_data                    VARCHAR2(4000);
3550   l_tax_liab_ccid		ap_invoice_distributions_all.dist_code_combination_id%TYPE;
3551 
3552   l_trx_id			NUMBER;
3553   l_trx_line_id			NUMBER;
3554   l_trx_line_dist_id		NUMBER;
3555   l_summary_tax_line_id		NUMBER;
3556   l_rec_nrec_tax_dist_id	NUMBER;
3557   l_tax_line_id			NUMBER;
3558   l_application_id		NUMBER;
3559 
3560   l_self_assessed_flag		VARCHAR2(20);
3561   l_recoverable_flag		VARCHAR2(20);
3562   l_reporting_only_flag		VARCHAR2(20);
3563 
3564   l_first 			NUMBER;
3565   l_last			NUMBER;
3566 
3567   -- Project LCM 7588322
3568   l_lcm_enabled                   VARCHAR2(1) := 'N';
3569   l_rcv_transaction_id            NUMBER;
3570   l_lcm_account_id                NUMBER;
3571   l_tax_variance_account_id       NUMBER;
3572   l_def_charges_account_id        NUMBER;
3573   l_exchange_variance_account_id  NUMBER;
3574   l_inv_variance_account_id       NUMBER;
3575 
3576   TYPE LineList IS TABLE OF VARCHAR2(25);
3577   linetype  LineList := LineList('TIPV', 'TRV', 'TERV');
3578 
3579   BEGIN
3580 
3581     l_curr_calling_sequence := 'AP_ETAX_UTILITY_PKG.Return_Tax_Distributions<-'||
3582                                P_calling_sequence;
3583 
3584     IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
3585         FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_ETAX_UTILITY_PKG.Return_Tax_Distributions(+)');
3586     END IF;
3587 
3588     DELETE FROM AP_Line_Temp_GT;
3589 
3590     FORALL i IN linetype.FIRST..linetype.LAST
3591 	INSERT INTO AP_Line_Temp_GT (Line_Type)
3592 	VALUES (linetype(i));
3593 
3594     -------------------------------------------------------------------
3595     l_debug_info := 'Get profile option info';
3596     -------------------------------------------------------------------
3597     l_allow_pa_override := FND_PROFILE.VALUE('PA_ALLOW_FLEXBUILDER_OVERRIDES');
3598 
3599     -------------------------------------------------------------------
3600     l_debug_info := 'Get tax distributions for update';
3601     -------------------------------------------------------------------
3602     OPEN UPDATE_TAX_DIST;
3603     LOOP
3604         FETCH UPDATE_TAX_DIST
3605          BULK COLLECT INTO L_INV_DIST_UPD
3606         LIMIT AP_ETAX_PKG.G_BATCH_LIMIT;
3607 
3608         EXIT WHEN UPDATE_TAX_DIST%NOTFOUND
3609                   AND L_INV_DIST_UPD.COUNT <= 0;
3610 
3611 	-------------------------------------------------------------------
3612 	l_debug_info := 'Tax distributions updated: '||l_inv_dist_upd.count;
3613 	-------------------------------------------------------------------
3614 	IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3615 	    FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3616 	END IF;
3617 
3618 	IF (l_inv_dist_upd.COUNT <> 0) THEN
3619 
3620 	    FOR j IN l_inv_dist_upd.FIRST..l_inv_dist_upd.LAST LOOP
3621 
3622 
3623 
3624 	         UPDATE ap_invoice_distributions_all
3625 	          SET 	accounting_date 		= l_inv_dist_upd(j).accounting_date,
3626 			last_updated_by 		= l_user_id,
3627 	            	last_update_date 		= l_sysdate,
3628 	            	line_type_lookup_code 		= l_inv_dist_upd(j).line_type_lookup_code,
3629 	            	period_name 			= l_inv_dist_upd(j).period_name,
3630 	            	amount 				= l_inv_dist_upd(j).amount,
3631 	            	base_amount 			= decode(l_inv_dist_upd(j).base_amount,
3632                                                                  0, decode(l_inv_dist_upd(j).amount, 0, l_inv_dist_upd(j).base_amount, NULL),
3633                                                                  l_inv_dist_upd(j).base_amount),
3634 	            	description 			= l_inv_dist_upd(j).description,
3635 	            	income_tax_region 		= l_inv_dist_upd(j).income_tax_region,
3636 	            	last_update_login 		= l_login_id,
3637 	            	type_1099 			= l_inv_dist_upd(j).type_1099,
3638 	            	attribute1 			= l_inv_dist_upd(j).attribute1,
3639 	            	attribute10 			= l_inv_dist_upd(j).attribute10,
3640 	            	attribute11 			= l_inv_dist_upd(j).attribute11,
3641 	            	attribute12 			= l_inv_dist_upd(j).attribute12,
3642 	            	attribute13 			= l_inv_dist_upd(j).attribute13,
3643 	            	attribute14 			= l_inv_dist_upd(j).attribute14,
3644 	            	attribute15 			= l_inv_dist_upd(j).attribute15,
3645 	            	attribute2 			= l_inv_dist_upd(j).attribute2,
3646 	            	attribute3 			= l_inv_dist_upd(j).attribute3,
3647 	            	attribute4 			= l_inv_dist_upd(j).attribute4,
3648 	            	attribute5 			= l_inv_dist_upd(j).attribute5,
3649 	            	attribute6 			= l_inv_dist_upd(j).attribute6,
3650 	            	attribute7 			= l_inv_dist_upd(j).attribute7,
3651 	            	attribute8 			= l_inv_dist_upd(j).attribute8,
3652 	            	attribute9 			= l_inv_dist_upd(j).attribute9,
3653 	            	attribute_category 		= l_inv_dist_upd(j).attribute_category,
3654 	            	expenditure_item_date 		= l_inv_dist_upd(j).expenditure_item_date,
3655 	            	expenditure_organization_id 	= l_inv_dist_upd(j).expenditure_organization_id,
3656 	            	expenditure_type 		= l_inv_dist_upd(j).expenditure_type,
3657 	            	parent_invoice_id 		= l_inv_dist_upd(j).parent_invoice_id,
3658 	            	pa_addition_flag 		= l_inv_dist_upd(j).pa_addition_flag,
3659 	            	pa_quantity 			= l_inv_dist_upd(j).pa_quantity,
3660 	            	project_accounting_context 	= l_inv_dist_upd(j).project_accounting_context,
3661 	            	project_id 			= l_inv_dist_upd(j).project_id,
3662 	            	task_id 			= l_inv_dist_upd(j).task_id,
3663 	            	awt_group_id 			= l_inv_dist_upd(j).awt_group_id,
3664 	            	global_attribute_category 	= l_inv_dist_upd(j).global_attribute_category,
3665 	            	global_attribute1 		= l_inv_dist_upd(j).global_attribute1,
3666 	            	global_attribute2 		= l_inv_dist_upd(j).global_attribute2,
3667 	            	global_attribute3 		= l_inv_dist_upd(j).global_attribute3,
3668 	            	global_attribute4 		= l_inv_dist_upd(j).global_attribute4,
3669 	            	global_attribute5 		= l_inv_dist_upd(j).global_attribute5,
3670 	            	global_attribute6 		= l_inv_dist_upd(j).global_attribute6,
3671 	            	global_attribute7 		= l_inv_dist_upd(j).global_attribute7,
3672 	            	global_attribute8 		= l_inv_dist_upd(j).global_attribute8,
3673 	            	global_attribute9 		= l_inv_dist_upd(j).global_attribute9,
3674 	            	global_attribute10 		= l_inv_dist_upd(j).global_attribute10,
3675 	            	global_attribute11 		= l_inv_dist_upd(j).global_attribute11,
3676 	            	global_attribute12 		= l_inv_dist_upd(j).global_attribute12,
3677 	            	global_attribute13 		= l_inv_dist_upd(j).global_attribute13,
3678 	            	global_attribute14 		= l_inv_dist_upd(j).global_attribute14,
3679 	            	global_attribute15 		= l_inv_dist_upd(j).global_attribute15,
3680 	            	global_attribute16 		= l_inv_dist_upd(j).global_attribute16,
3681 	            	global_attribute17 		= l_inv_dist_upd(j).global_attribute17,
3682 	            	global_attribute18 		= l_inv_dist_upd(j).global_attribute18,
3683 	            	global_attribute19 		= l_inv_dist_upd(j).global_attribute19,
3684 	            	global_attribute20 		= l_inv_dist_upd(j).global_attribute20,
3685 	            	award_id 			= l_inv_dist_upd(j).award_id,
3686 	            	dist_match_type 		= l_inv_dist_upd(j).dist_match_type,
3687 	            	rcv_transaction_id 		= l_inv_dist_upd(j).rcv_transaction_id,
3688 	            	tax_recoverable_flag 		= l_inv_dist_upd(j).tax_recoverable_flag,
3689 	            	cancellation_flag 		= l_inv_dist_upd(j).cancellation_flag,
3690 	            	--invoice_line_number 		= l_inv_dist_upd(j).invoice_line_number,
3691         	    	corrected_invoice_dist_id 	= l_inv_dist_upd(j).corrected_invoice_dist_id,
3692 	            	rounding_amt 			= l_inv_dist_upd(j).rounding_amt,
3693 	            	charge_applicable_to_dist_id 	= l_inv_dist_upd(j).charge_applicable_to_dist_id,
3694 	            	--distribution_class 		= l_inv_dist_upd(j).distribution_class,    --Bug6678578
3695 	            	tax_code_id 			= l_inv_dist_upd(j).tax_code_id,
3696 	            	detail_tax_dist_id 		= l_inv_dist_upd(j).detail_tax_dist_id,
3697 	            	rec_nrec_rate 			= l_inv_dist_upd(j).rec_nrec_rate,
3698 	            	recovery_rate_id 		= l_inv_dist_upd(j).recovery_rate_id,
3699 	            	recovery_rate_name 		= l_inv_dist_upd(j).recovery_rate_name,
3700 	            	recovery_type_code 		= l_inv_dist_upd(j).recovery_type_code,
3701 	            	taxable_amount 			= l_inv_dist_upd(j).taxable_amount,
3702 	            	taxable_base_amount 		= l_inv_dist_upd(j).taxable_base_amount,
3703 	            	summary_tax_line_id 		= l_inv_dist_upd(j).summary_tax_line_id,
3704 	            	extra_po_erv 			= l_inv_dist_upd(j).extra_po_erv,
3705 	            	prepay_tax_diff_amount 		= l_inv_dist_upd(j).prepay_tax_diff_amount,
3706 			match_status_flag		= decode (amount, l_inv_dist_upd(j).amount,
3707 									  match_status_flag, 'N')
3708 	          WHERE invoice_distribution_id = l_inv_dist_upd(j).invoice_distribution_id;
3709 
3710              END LOOP;
3711              L_INV_DIST_UPD.DELETE;
3712          END IF;
3713     END LOOP;
3714     CLOSE UPDATE_TAX_DIST;
3715 
3716 
3717     -------------------------------------------------------------------
3718     l_debug_info := 'Get tax variance distributions for update';
3719     -------------------------------------------------------------------
3720     OPEN UPDATE_TAX_VARIANCES;
3721     LOOP
3722         FETCH UPDATE_TAX_VARIANCES
3723          BULK COLLECT INTO L_INV_DIST_UPD
3724         LIMIT AP_ETAX_PKG.G_BATCH_LIMIT;
3725 
3726         EXIT WHEN UPDATE_TAX_VARIANCES%NOTFOUND
3727                   AND L_INV_DIST_UPD.COUNT <= 0;
3728 
3729 	-------------------------------------------------------------------
3730 	l_debug_info := 'Tax Variance distributions updated: '||l_inv_dist_upd.count;
3731 	-------------------------------------------------------------------
3732 	IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3733 	    FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3734 	END IF;
3735 
3736 	IF (l_inv_dist_upd.COUNT <> 0) THEN
3737 
3738 	    FOR j IN l_inv_dist_upd.FIRST..l_inv_dist_upd.LAST LOOP
3739 
3740 	         UPDATE ap_invoice_distributions_all
3741 	          SET 	accounting_date 		= l_inv_dist_upd(j).accounting_date,
3742 			last_updated_by 		= l_user_id,
3743 	            	last_update_date 		= l_sysdate,
3744 	            	line_type_lookup_code 		= l_inv_dist_upd(j).line_type_lookup_code,
3745 	            	period_name 			= l_inv_dist_upd(j).period_name,
3746 	            	amount 				= l_inv_dist_upd(j).amount,
3747 	            	base_amount 			= decode(l_inv_dist_upd(j).base_amount,
3748 								 0, decode(l_inv_dist_upd(j).amount, 0, l_inv_dist_upd(j).base_amount, NULL),
3749                                                                  l_inv_dist_upd(j).base_amount),
3750 	            	description 			= l_inv_dist_upd(j).description,
3751 	            	income_tax_region 		= l_inv_dist_upd(j).income_tax_region,
3752 	            	last_update_login 		= l_login_id,
3753 	            	type_1099 			= l_inv_dist_upd(j).type_1099,
3754 	            	attribute1 			= l_inv_dist_upd(j).attribute1,
3755 	            	attribute10 			= l_inv_dist_upd(j).attribute10,
3756 	            	attribute11 			= l_inv_dist_upd(j).attribute11,
3757 	            	attribute12 			= l_inv_dist_upd(j).attribute12,
3758 	            	attribute13 			= l_inv_dist_upd(j).attribute13,
3759 	            	attribute14 			= l_inv_dist_upd(j).attribute14,
3760 	            	attribute15 			= l_inv_dist_upd(j).attribute15,
3761 	            	attribute2 			= l_inv_dist_upd(j).attribute2,
3762 	            	attribute3 			= l_inv_dist_upd(j).attribute3,
3763 	            	attribute4 			= l_inv_dist_upd(j).attribute4,
3764 	            	attribute5 			= l_inv_dist_upd(j).attribute5,
3765 	            	attribute6 			= l_inv_dist_upd(j).attribute6,
3766 	            	attribute7 			= l_inv_dist_upd(j).attribute7,
3767 	            	attribute8 			= l_inv_dist_upd(j).attribute8,
3768 	            	attribute9 			= l_inv_dist_upd(j).attribute9,
3769 	            	attribute_category 		= l_inv_dist_upd(j).attribute_category,
3770 	            	expenditure_item_date 		= l_inv_dist_upd(j).expenditure_item_date,
3771 	            	expenditure_organization_id 	= l_inv_dist_upd(j).expenditure_organization_id,
3772 	            	expenditure_type 		= l_inv_dist_upd(j).expenditure_type,
3773 	            	parent_invoice_id 		= l_inv_dist_upd(j).parent_invoice_id,
3774 	            	pa_addition_flag 		= l_inv_dist_upd(j).pa_addition_flag,
3775 	            	pa_quantity 			= l_inv_dist_upd(j).pa_quantity,
3776 	            	project_accounting_context 	= l_inv_dist_upd(j).project_accounting_context,
3777 	            	project_id 			= l_inv_dist_upd(j).project_id,
3778 	            	task_id 			= l_inv_dist_upd(j).task_id,
3779 	            	awt_group_id 			= l_inv_dist_upd(j).awt_group_id,
3780 	            	global_attribute_category 	= l_inv_dist_upd(j).global_attribute_category,
3781 	            	global_attribute1 		= l_inv_dist_upd(j).global_attribute1,
3782 	            	global_attribute2 		= l_inv_dist_upd(j).global_attribute2,
3783 	            	global_attribute3 		= l_inv_dist_upd(j).global_attribute3,
3784 	            	global_attribute4 		= l_inv_dist_upd(j).global_attribute4,
3785 	            	global_attribute5 		= l_inv_dist_upd(j).global_attribute5,
3786 	            	global_attribute6 		= l_inv_dist_upd(j).global_attribute6,
3787 	            	global_attribute7 		= l_inv_dist_upd(j).global_attribute7,
3788 	            	global_attribute8 		= l_inv_dist_upd(j).global_attribute8,
3789 	            	global_attribute9 		= l_inv_dist_upd(j).global_attribute9,
3790 	            	global_attribute10 		= l_inv_dist_upd(j).global_attribute10,
3791 	            	global_attribute11 		= l_inv_dist_upd(j).global_attribute11,
3792 	            	global_attribute12 		= l_inv_dist_upd(j).global_attribute12,
3793 	            	global_attribute13 		= l_inv_dist_upd(j).global_attribute13,
3794 	            	global_attribute14 		= l_inv_dist_upd(j).global_attribute14,
3795 	            	global_attribute15 		= l_inv_dist_upd(j).global_attribute15,
3796 	            	global_attribute16 		= l_inv_dist_upd(j).global_attribute16,
3797 	            	global_attribute17 		= l_inv_dist_upd(j).global_attribute17,
3798 	            	global_attribute18 		= l_inv_dist_upd(j).global_attribute18,
3799 	            	global_attribute19 		= l_inv_dist_upd(j).global_attribute19,
3800 	            	global_attribute20 		= l_inv_dist_upd(j).global_attribute20,
3801 	            	award_id 			= l_inv_dist_upd(j).award_id,
3802 	            	dist_match_type 		= l_inv_dist_upd(j).dist_match_type,
3803 	            	rcv_transaction_id 		= l_inv_dist_upd(j).rcv_transaction_id,
3804 	            	tax_recoverable_flag 		= l_inv_dist_upd(j).tax_recoverable_flag,
3805 	            	cancellation_flag 		= l_inv_dist_upd(j).cancellation_flag,
3806 	            	--invoice_line_number 		= l_inv_dist_upd(j).invoice_line_number,
3807         	    	corrected_invoice_dist_id 	= l_inv_dist_upd(j).corrected_invoice_dist_id,
3808 	            	rounding_amt 			= l_inv_dist_upd(j).rounding_amt,
3809 	            	charge_applicable_to_dist_id 	= l_inv_dist_upd(j).charge_applicable_to_dist_id,
3810 	            	--distribution_class 		= l_inv_dist_upd(j).distribution_class, --Bug6678578
3811 	            	tax_code_id 			= l_inv_dist_upd(j).tax_code_id,
3812 	            	detail_tax_dist_id 		= l_inv_dist_upd(j).detail_tax_dist_id,
3813 	            	rec_nrec_rate 			= l_inv_dist_upd(j).rec_nrec_rate,
3814 	            	recovery_rate_id 		= l_inv_dist_upd(j).recovery_rate_id,
3815 	            	recovery_rate_name 		= l_inv_dist_upd(j).recovery_rate_name,
3816 	            	recovery_type_code 		= l_inv_dist_upd(j).recovery_type_code,
3817 	            	taxable_amount 			= l_inv_dist_upd(j).taxable_amount,
3818 	            	taxable_base_amount 		= l_inv_dist_upd(j).taxable_base_amount,
3819 	            	summary_tax_line_id 		= l_inv_dist_upd(j).summary_tax_line_id,
3820 	            	extra_po_erv 			= l_inv_dist_upd(j).extra_po_erv,
3821 	            	prepay_tax_diff_amount 		= l_inv_dist_upd(j).prepay_tax_diff_amount,
3822                         match_status_flag               = decode (amount, l_inv_dist_upd(j).amount,
3823                                                                           match_status_flag, 'N')
3824 	          WHERE invoice_distribution_id = l_inv_dist_upd(j).invoice_distribution_id;
3825 
3826              END LOOP;
3827              L_INV_DIST_UPD.DELETE;
3828          END IF;
3829     END LOOP;
3830     CLOSE UPDATE_TAX_VARIANCES;
3831 
3832 
3833     -------------------------------------------------------------------
3834     l_debug_info := 'Step 5: Get tax distributions for update';
3835     -------------------------------------------------------------------
3836     OPEN UPDATE_TAX_SELF;
3837     LOOP
3838         FETCH UPDATE_TAX_SELF
3839          BULK COLLECT INTO L_INV_SELF_UPD
3840         LIMIT AP_ETAX_PKG.G_BATCH_LIMIT;
3841 
3842         EXIT WHEN UPDATE_TAX_SELF%NOTFOUND
3843                   AND L_INV_DIST_UPD.COUNT <= 0;
3844 
3845 	-------------------------------------------------------------------
3846     	l_debug_info := 'Step 5: Self assessed dist updated: '||l_inv_self_upd.COUNT;
3847     	-------------------------------------------------------------------
3848     	IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3849       	    FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3850     	END IF;
3851 
3852     	IF (l_inv_self_upd.COUNT <> 0) THEN
3853 
3854       	    FOR j IN l_inv_self_upd.FIRST..l_inv_self_upd.LAST LOOP
3855 
3856 	        UPDATE ap_self_assessed_tax_dist_all
3857 	        SET accounting_date 		= l_inv_self_upd(j).accounting_date,
3858 		    last_updated_by 		= l_user_id,
3859 	            last_update_date 		= l_sysdate,
3860 	            line_type_lookup_code 	= l_inv_self_upd(j).line_type_lookup_code,
3861 	            period_name 		= l_inv_self_upd(j).period_name,
3862 	            amount 			= l_inv_self_upd(j).amount,
3863 	            base_amount 		= decode(l_inv_self_upd(j).base_amount,
3864                                                          0, decode(l_inv_self_upd(j).amount, 0, l_inv_self_upd(j).base_amount, NULL),
3865                                                          l_inv_self_upd(j).base_amount),
3866 	            description 		= l_inv_self_upd(j).description,
3867 	            income_tax_region 		= l_inv_self_upd(j).income_tax_region,
3868 	            last_update_login 		= l_login_id,
3869 	            type_1099 			= l_inv_self_upd(j).type_1099,
3870 	            attribute1 			= l_inv_self_upd(j).attribute1,
3871 	            attribute10 		= l_inv_self_upd(j).attribute10,
3872 	            attribute11 		= l_inv_self_upd(j).attribute11,
3873 	            attribute12 		= l_inv_self_upd(j).attribute12,
3874 	            attribute13 		= l_inv_self_upd(j).attribute13,
3875 	            attribute14 		= l_inv_self_upd(j).attribute14,
3876 	            attribute15 		= l_inv_self_upd(j).attribute15,
3877 	            attribute2 			= l_inv_self_upd(j).attribute2,
3878 	            attribute3 			= l_inv_self_upd(j).attribute3,
3879 	            attribute4 			= l_inv_self_upd(j).attribute4,
3880 	            attribute5 			= l_inv_self_upd(j).attribute5,
3881 	            attribute6 			= l_inv_self_upd(j).attribute6,
3882 	            attribute7 			= l_inv_self_upd(j).attribute7,
3883 	            attribute8 			= l_inv_self_upd(j).attribute8,
3884 	            attribute9 			= l_inv_self_upd(j).attribute9,
3885 	            attribute_category 		= l_inv_self_upd(j).attribute_category,
3886 	            expenditure_item_date 	= l_inv_self_upd(j).expenditure_item_date,
3887 	            expenditure_organization_id = l_inv_self_upd(j).expenditure_organization_id,
3888 	            expenditure_type 		= l_inv_self_upd(j).expenditure_type,
3889 	            parent_invoice_id 		= l_inv_self_upd(j).parent_invoice_id,
3890 	            pa_addition_flag 		= l_inv_self_upd(j).pa_addition_flag,
3891 	            pa_quantity 		= l_inv_self_upd(j).pa_quantity,
3892 	            project_accounting_context 	= l_inv_self_upd(j).project_accounting_context,
3893 	            project_id 			= l_inv_self_upd(j).project_id,
3894 	            task_id 			= l_inv_self_upd(j).task_id,
3895 	            awt_group_id 		= l_inv_self_upd(j).awt_group_id,
3896 	            global_attribute_category 	= l_inv_self_upd(j).global_attribute_category,
3897 	            global_attribute1 		= l_inv_self_upd(j).global_attribute1,
3898 	            global_attribute2 		= l_inv_self_upd(j).global_attribute2,
3899 	            global_attribute3 		= l_inv_self_upd(j).global_attribute3,
3900 	            global_attribute4 		= l_inv_self_upd(j).global_attribute4,
3901 	            global_attribute5 		= l_inv_self_upd(j).global_attribute5,
3902 	            global_attribute6 		= l_inv_self_upd(j).global_attribute6,
3903 	            global_attribute7 		= l_inv_self_upd(j).global_attribute7,
3904 	            global_attribute8 		= l_inv_self_upd(j).global_attribute8,
3905 	            global_attribute9 		= l_inv_self_upd(j).global_attribute9,
3906 	            global_attribute10 		= l_inv_self_upd(j).global_attribute10,
3907 	            global_attribute11 		= l_inv_self_upd(j).global_attribute11,
3908 	            global_attribute12 		= l_inv_self_upd(j).global_attribute12,
3909 	            global_attribute13 		= l_inv_self_upd(j).global_attribute13,
3910 	            global_attribute14 		= l_inv_self_upd(j).global_attribute14,
3911 	            global_attribute15 		= l_inv_self_upd(j).global_attribute15,
3912 	            global_attribute16 		= l_inv_self_upd(j).global_attribute16,
3913 	            global_attribute17 		= l_inv_self_upd(j).global_attribute17,
3914 	            global_attribute18 		= l_inv_self_upd(j).global_attribute18,
3915 	            global_attribute19 		= l_inv_self_upd(j).global_attribute19,
3916 	            global_attribute20 		= l_inv_self_upd(j).global_attribute20,
3917 	            award_id 			= l_inv_self_upd(j).award_id,
3918 	            dist_match_type 		= l_inv_self_upd(j).dist_match_type,
3919 	            rcv_transaction_id 		= l_inv_self_upd(j).rcv_transaction_id,
3920 	            tax_recoverable_flag 	= l_inv_self_upd(j).tax_recoverable_flag,
3921 	            cancellation_flag 		= l_inv_self_upd(j).cancellation_flag,
3922 	            invoice_line_number 	= l_inv_self_upd(j).invoice_line_number,
3923 	            corrected_invoice_dist_id 	= l_inv_self_upd(j).corrected_invoice_dist_id,
3924 	            rounding_amt 		= l_inv_self_upd(j).rounding_amt,
3925 	            charge_applicable_to_dist_id = l_inv_self_upd(j).charge_applicable_to_dist_id,
3926 	            --distribution_class 		= l_inv_self_upd(j).distribution_class, --Bug6678578
3927 	            tax_code_id 		= l_inv_self_upd(j).tax_code_id,
3928 	            detail_tax_dist_id 		= l_inv_self_upd(j).detail_tax_dist_id,
3929 	            rec_nrec_rate 		= l_inv_self_upd(j).rec_nrec_rate,
3930 	            recovery_rate_id 		= l_inv_self_upd(j).recovery_rate_id,
3931 	            recovery_rate_name 		= l_inv_self_upd(j).recovery_rate_name,
3932 	            recovery_type_code 		= l_inv_self_upd(j).recovery_type_code,
3933 	            taxable_amount 		= l_inv_self_upd(j).taxable_amount,
3934 	            taxable_base_amount 	= l_inv_self_upd(j).taxable_base_amount,
3935 	            summary_tax_line_id 	= l_inv_self_upd(j).summary_tax_line_id,
3936 	            extra_po_erv 		= l_inv_self_upd(j).extra_po_erv,
3937 	            prepay_tax_diff_amount 	=  l_inv_self_upd(j).prepay_tax_diff_amount
3938 	        WHERE invoice_distribution_id = l_inv_self_upd(j).invoice_distribution_id;
3939 
3940             END LOOP;
3941             L_INV_SELF_UPD.DELETE;
3942         END IF;
3943     END LOOP;
3944     CLOSE UPDATE_TAX_SELF;
3945 
3946     -------------------------------------------------------------------
3947     l_debug_info := 'Step 6: Get tax distributions for insert';
3948     -------------------------------------------------------------------
3949     OPEN INSERT_TAX_DIST;
3950     LOOP
3951         FETCH INSERT_TAX_DIST
3952          BULK COLLECT INTO L_INV_DIST_INS
3953         LIMIT AP_ETAX_PKG.G_BATCH_LIMIT;
3954 
3955         EXIT WHEN INSERT_TAX_DIST%NOTFOUND
3956                   AND L_INV_DIST_INS.COUNT <= 0;
3957 
3958         IF (l_inv_dist_ins.COUNT <> 0) THEN
3959 
3960             -------------------------------------------------------------------
3961             l_debug_info := 'Tax distributions to insert: '||l_inv_dist_ins.COUNT;
3962             -------------------------------------------------------------------
3963             IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3964                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3965             END IF;
3966 
3967             FOR i IN l_inv_dist_ins.FIRST..l_inv_dist_ins.LAST LOOP
3968 
3969                 IF (l_inv_dist_ins(i).line_type_lookup_code =  'NONREC_TAX') THEN
3970 
3971 	           IF ( l_inv_dist_ins(i).po_distribution_id IS NOT NULL OR
3972 	                l_inv_dist_ins(i).rcv_transaction_id IS NOT NULL) THEN
3973 
3974 	              IF ( l_inv_dist_ins(i).accrue_on_receipt_flag = 'Y' OR
3975 	                   --l_inv_dist_ins(i).allow_flex_override_flag = 'Y' -- Bug 6720793
3976 	                   l_inv_dist_ins(i).purch_encumbrance_flag = 'Y') THEN
3977 
3978                            -------------------------------------------------------------------
3979                            l_debug_info := 'PO: Setting Non-Rec tax account same as its parent';
3980                            -------------------------------------------------------------------
3981                            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3982                                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3983                            END IF;
3984 
3985                            l_dist_code_combination_id := l_inv_dist_ins(i).dist_code_combination_id;
3986 
3987 	              END IF;
3988 
3989 	           ELSIF ( l_inv_dist_ins(i).project_id IS NOT NULL AND
3990 	                   l_allow_pa_override = 'N') THEN
3991 
3992 	              l_dist_code_combination_id := l_inv_dist_ins(i).dist_code_combination_id;
3993 
3994 	           END IF;
3995 
3996 
3997                    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3998                        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'before geting ccid from corresponding tax distribution');
3999                    END IF;
4000                    -- This section to get ccid from corresponding tax distribution if
4001 	           -- line discard/invoice cancellation or correction
4002 	           -- quick credit or prepayment application/unapplication
4003 
4004 	           IF (l_inv_dist_ins(i).parent_dist_cancellation_flag = 'Y' OR
4005 	               (nvl(p_invoice_header_rec.quick_credit, 'N') = 'Y'
4006 	                and p_invoice_header_rec.credited_invoice_id IS NOT NULL) OR
4007 	               (l_inv_dist_ins(i).parent_dist_reversal_flag = 'Y'
4008 	                and l_inv_dist_ins(i).parent_dist_parent_reversal_id IS NOT NULL
4009 					AND l_inv_dist_ins(i).reversed_tax_dist_id IS NOT NULL)) THEN -- bug 7389822
4010 
4011                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4012                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'geting ccid for discard, cancellation');
4013                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'l_inv_dist_ins(i).parent_dist_cancellation_flag'||l_inv_dist_ins(i).parent_dist_cancellation_flag);
4014                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'p_invoice_header_rec.quick_credit'||p_invoice_header_rec.quick_credit);
4015                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'p_invoice_header_rec.credited_invoice_id'||to_char(p_invoice_header_rec.credited_invoice_id));
4016                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'l_inv_dist_ins(i).parent_dist_reversal_flag'||l_inv_dist_ins(i).parent_dist_reversal_flag);
4017                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'l_inv_dist_ins(i).parent_dist_parent_reversal_id'||to_char(l_inv_dist_ins(i).parent_dist_parent_reversal_id));
4018                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'l_inv_dist_ins(i).reversed_tax_dist_id'||to_char(l_inv_dist_ins(i).reversed_tax_dist_id));
4019                       END IF;
4020 
4021                       --Bug fix 6653070, bug fix 6687031
4022 	              SELECT dist_code_combination_id
4023 	                INTO l_dist_code_combination_id
4024 	                FROM ap_invoice_distributions_all
4025 	               WHERE detail_tax_dist_id = l_inv_dist_ins(i).reversed_tax_dist_id
4026 	                 AND line_type_lookup_code IN ('REC_TAX', 'NONREC_TAX','TIPV', 'TRV', 'TERV')
4027                          AND rownum =1;--Bug7241425
4028 
4029                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4030                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'after geting ccid for discard, cancellation');
4031                       END IF;
4032 
4033 	           ELSIF (l_inv_dist_ins(i).corrected_invoice_dist_id IS NOT NULL) THEN
4034 
4035                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4036                          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'geting ccid for correction case');
4037                       END IF;
4038 
4039 	              SELECT dist_code_combination_id
4040 	                INTO l_dist_code_combination_id
4041 	                FROM ap_invoice_distributions_all
4042 	               WHERE detail_tax_dist_id = l_inv_dist_ins(i).adjusted_doc_tax_dist_id
4043 	                 AND line_type_lookup_code IN ('REC_TAX','NONREC_TAX','TIPV','TRV','TERV')--Bug7241425
4044                          AND rownum =1;--Bug7241425
4045                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4046                          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'after geting ccid from corresponding tax distribution for correction case');
4047                       END IF;
4048 	           ELSIF (l_inv_dist_ins(i).applied_from_tax_dist_id IS NOT NULL) THEN
4049 
4050 	             l_dist_code_combination_id := l_inv_dist_ins(i).dist_code_combination_id;
4051 
4052 	           END IF;
4053 	        END IF;
4054 
4055 	        IF (l_inv_dist_ins(i).line_type_lookup_code = 'REC_TAX' OR
4056 	            (l_inv_dist_ins(i).line_type_lookup_code = 'NONREC_TAX'
4057 	             and l_dist_code_combination_id IS NULL)) THEN
4058 
4059 		  l_dist_ccid_rec.gl_date			:= l_inv_dist_ins(i).accounting_date;
4060 		  l_dist_ccid_rec.tax_rate_id			:= l_inv_dist_ins(i).tax_code_id;
4061 		  l_dist_ccid_rec.rec_rate_id			:= l_inv_dist_ins(i).recovery_rate_id;
4062 		  l_dist_ccid_rec.self_assessed_flag		:= 'N';
4063 		  l_dist_ccid_rec.recoverable_flag		:= l_inv_dist_ins(i).tax_recoverable_flag;
4064 		  l_dist_ccid_rec.tax_jurisdiction_id		:= l_inv_dist_ins(i).tax_jurisdiction_id;
4065 		  l_dist_ccid_rec.tax_regime_id			:= l_inv_dist_ins(i).tax_regime_id;
4066 		  l_dist_ccid_rec.tax_id			:= l_inv_dist_ins(i).tax_id;
4067 		  l_dist_ccid_rec.internal_organization_id	:= l_inv_dist_ins(i).org_id;
4068 		  l_dist_ccid_rec.tax_status_id			:= l_inv_dist_ins(i).tax_status_id;
4069 
4070 		  l_dist_ccid_rec.revenue_expense_ccid		:= NVL(l_inv_dist_ins(i).dist_code_combination_id,-99);  ---6010950
4071 		  l_dist_ccid_rec.account_source_tax_rate_id    := l_inv_dist_ins(i).account_source_tax_rate_id;
4072 
4073 	          -----------------------------------------------------------
4074 	          l_debug_info := 'Call zx_api_pub.get_tax_distribution_ccids';
4075 	          -----------------------------------------------------------
4076 		  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4077 		      FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4078 		  END IF;
4079 
4080 	          zx_api_pub.get_tax_distribution_ccids(
4081 		            p_api_version            => 1.0,
4082 		            p_init_msg_list          => FND_API.G_TRUE,
4083 		            p_commit                 => FND_API.G_FALSE,
4084 		            p_validation_level       => FND_API.G_VALID_LEVEL_FULL,
4085 		            x_return_status          => l_return_status_service,
4086 		            x_msg_count              => l_msg_count,
4087 		            x_msg_data               => l_msg_data,
4088 			    p_dist_ccid_rec	     => l_dist_ccid_rec);
4089 
4090 		  l_dist_code_combination_id	:= l_dist_ccid_rec.rec_nrec_ccid;
4091 		  l_tax_liab_ccid		:= l_dist_ccid_rec.tax_liab_ccid;
4092 
4093 	          IF (l_return_status_service <> 'S') THEN
4094 	              -----------------------------------------------------------
4095 	              l_debug_info := 'Handle errors returned by API';
4096 	              -----------------------------------------------------------
4097 		      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4098 		          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4099 	              END IF;
4100 
4101 	              IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
4102 				P_All_Error_Messages  => P_All_Error_Messages,
4103 	               		P_Msg_Count           => l_msg_count,
4104 	               		P_Msg_Data            => l_msg_data,
4105 	               		P_Error_Code          => P_Error_Code,
4106 	               		P_Calling_Sequence    => l_curr_calling_sequence)) THEN
4107 	                 NULL;
4108 	              END IF;
4109 
4110 	              RETURN FALSE;
4111 
4112 	          END IF;
4113 	        END IF;
4114 
4115 	        -------------------------------------------------------------------
4116 	        l_debug_info := 'Insert REC/NONREC distributions';
4117 	        -------------------------------------------------------------------
4118 		IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4119 	          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4120 	        END IF;
4121 
4122 	        ap_etax_utility_pkg.insert_tax_distributions
4123 	        			(p_invoice_header_rec        => p_invoice_header_rec,
4124 					 p_inv_dist_rec              => l_inv_dist_ins(i),
4125 					 p_dist_code_combination_id  => l_dist_code_combination_id,
4126 					 p_user_id		     => l_user_id,
4127 					 p_sysdate	     	     => l_sysdate,
4128 					 p_login_id		     => l_login_id,
4129 					 p_calling_sequence          => l_curr_calling_sequence);
4130 
4131 		l_dist_code_combination_id := Null;
4132 
4133 	    END LOOP;
4134             L_INV_DIST_INS.DELETE;
4135         END IF;
4136     END LOOP;
4137     CLOSE INSERT_TAX_DIST;
4138 
4139     -------------------------------------------------------------------
4140     l_debug_info := 'Get Tax Variances';
4141     ------------------------------------------------------------------
4142     OPEN INSERT_TAX_VARIANCES;
4143     LOOP
4144         FETCH INSERT_TAX_VARIANCES
4145          BULK COLLECT INTO L_INV_DIST_INS
4146         LIMIT AP_ETAX_PKG.G_BATCH_LIMIT;
4147 
4148         EXIT WHEN INSERT_TAX_VARIANCES%NOTFOUND
4149                   AND L_INV_DIST_INS.COUNT <= 0;
4150 
4151         IF (l_inv_dist_ins.COUNT <> 0) THEN
4152 
4153             FOR i IN l_inv_dist_ins.FIRST..l_inv_dist_ins.LAST LOOP
4154                 l_dist_code_combination_id := l_inv_dist_ins(i).dist_code_combination_id;
4155 
4156 				-- Project LCM 7588322
4157 	           BEGIN
4158               SELECT 'Y'
4159 	            INTO   l_lcm_enabled
4160 	            FROM   RCV_TRANSACTIONS
4161 	            WHERE  TRANSACTION_ID = L_INV_DIST_INS(i).rcv_transaction_id
4162 	            AND    LCM_SHIPMENT_LINE_ID IS NOT NULL;
4163 	           EXCEPTION
4164               WHEN NO_DATA_FOUND THEN NULL;
4165              END;
4166 
4167 	            IF(l_lcm_enabled = 'Y') THEN
4168 	               l_rcv_transaction_id := L_INV_DIST_INS(i).rcv_transaction_id;
4169 	               RCV_UTILITIES.Get_RtLcmInfo(
4170 	                            p_rcv_transaction_id           => l_rcv_transaction_id,
4171 	                            x_lcm_account_id               => l_lcm_account_id,
4172 								x_tax_variance_account_id      => l_tax_variance_account_id,
4173 								x_def_charges_account_id       => l_def_charges_account_id,
4174 								x_exchange_variance_account_id => l_exchange_variance_account_id,
4175 								x_inv_variance_account_id      => l_inv_variance_account_id
4176 								);
4177 	               l_dist_code_combination_id := l_tax_variance_account_id;
4178 
4179                 END IF;
4180 	            -- End Project LCM 7588322
4181 
4182 
4183                 -------------------------------------------------------------------
4184                 l_debug_info := 'Insert Tax Variance Distributions';
4185                 -------------------------------------------------------------------
4186                 IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4187                     FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4188                 END IF;
4189 
4190                 ap_etax_utility_pkg.insert_tax_distributions(
4191 				  p_invoice_header_rec       => p_invoice_header_rec,
4192 				  p_inv_dist_rec             => l_inv_dist_ins(i),
4193 				  p_dist_code_combination_id => l_dist_code_combination_id,
4194 				  p_user_id		     => l_user_id,
4195 				  p_sysdate	     	     => l_sysdate,
4196 				  p_login_id		     => l_login_id,
4197 				  p_calling_sequence         => l_curr_calling_sequence);
4198            END LOOP;
4199            L_INV_DIST_INS.DELETE;
4200         END IF;
4201     END LOOP;
4202     CLOSE INSERT_TAX_VARIANCES;
4203 
4204 
4205     -------------------------------------------------------------------
4206     l_debug_info := 'Get self assessed dist for insert';
4207     -------------------------------------------------------------------
4208     OPEN INSERT_TAX_SELF;
4209     LOOP
4210         FETCH INSERT_TAX_SELF
4211          BULK COLLECT INTO L_INV_SELF_INS
4212         LIMIT AP_ETAX_PKG.G_BATCH_LIMIT;
4213 
4214         EXIT WHEN INSERT_TAX_SELF%NOTFOUND
4215                   AND L_INV_SELF_INS.COUNT <= 0;
4216 
4217 	IF (l_inv_self_ins.COUNT <> 0) THEN
4218 
4219       	    -------------------------------------------------------------------
4220       	    l_debug_info := 'Insert self assessed tax distributions';
4221       	    -------------------------------------------------------------------
4222       	    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4223           	FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4224       	    END IF;
4225 
4226 	    FOR i IN l_inv_self_ins.FIRST..l_inv_self_ins.LAST LOOP
4227 
4228 		-------------------------------------------------------------------
4229 		l_debug_info := 'Get ccid for self assessed distributions';
4230              	-------------------------------------------------------------------
4231 		IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4232                     FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4233              	END IF;
4234 
4235 	        IF (l_inv_self_ins(i).line_type_lookup_code =  'NONREC_TAX') THEN
4236 
4237 	           IF ( l_inv_self_ins(i).po_distribution_id IS NOT NULL OR
4238 	                l_inv_self_ins(i).rcv_transaction_id IS NOT NULL) THEN
4239 
4240 	              IF ( l_inv_self_ins(i).accrue_on_receipt_flag = 'Y' OR
4241 	                   --l_inv_self_ins(i).allow_flex_override_flag = 'Y' -- Bug 6720793
4242 	                   l_inv_self_ins(i).purch_encumbrance_flag = 'Y') THEN
4243 
4244                            -------------------------------------------------------------------
4245                            l_debug_info := 'PO: Setting Self Assessed Non-Rec tax account same as its parent';
4246                            -------------------------------------------------------------------
4247                            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4248                                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4249                            END IF;
4250 
4251 	                   l_dist_code_combination_id := l_inv_self_ins(i).dist_code_combination_id;
4252 
4253 	              END IF;
4254 
4255 	           ELSIF ( l_inv_self_ins(i).project_id IS NOT NULL AND
4256 	                   l_allow_pa_override = 'N') THEN
4257 
4258 	             l_dist_code_combination_id := l_inv_self_ins(i).dist_code_combination_id;
4259 
4260 	           END IF;
4261 
4262 	           -- This section to get ccid from corresponding tax distribution if
4263 	           -- line discard/invoice cancellation or correction
4264 	           -- quick credit or prepayment application/unapplication
4265 	           IF (l_inv_self_ins(i).parent_dist_cancellation_flag = 'Y' OR
4266 	              (l_inv_self_ins(i).parent_dist_reversal_flag = 'Y'
4267 	               and l_inv_self_ins(i).parent_dist_parent_reversal_id IS NOT NULL)) THEN
4268 
4269 	              SELECT dist_code_combination_id
4270 	                INTO l_dist_code_combination_id
4271 	                FROM ap_self_assessed_tax_dist_all
4272 	               WHERE detail_tax_dist_id = l_inv_self_ins(i).reversed_tax_dist_id;
4273 
4274 	           ELSIF (l_inv_self_ins(i).corrected_invoice_dist_id IS NOT NULL) THEN
4275 
4276 	              SELECT dist_code_combination_id
4277 	                INTO l_dist_code_combination_id
4278 	                FROM ap_self_assessed_tax_dist_all
4279 	               WHERE detail_tax_dist_id = l_inv_self_ins(i).adjusted_doc_tax_dist_id;
4280 
4281 	           ELSIF (l_inv_self_ins(i).applied_from_tax_dist_id IS NOT NULL) THEN
4282 
4283 	             l_dist_code_combination_id := l_inv_self_ins(i).dist_code_combination_id;
4284 
4285 	           END IF;
4286 
4287 	        ELSIF (l_inv_self_ins(i).line_type_lookup_code IN
4288 	              ('TIPV', 'TERV', 'TRV')) THEN
4289 
4290 	          l_dist_code_combination_id := l_inv_self_ins(i).dist_code_combination_id;
4291 
4292 	        END IF;
4293 
4294 	        IF (l_inv_self_ins(i).line_type_lookup_code = 'REC_TAX' OR
4295 	            (l_inv_self_ins(i).line_type_lookup_code = 'NONREC_TAX' )) THEN --Bug6599804
4296 
4297 		  l_dist_ccid_rec.gl_date			:= l_inv_self_ins(i).accounting_date;
4298 		  l_dist_ccid_rec.tax_rate_id			:= l_inv_self_ins(i).tax_code_id;
4299 		  l_dist_ccid_rec.rec_rate_id			:= l_inv_self_ins(i).recovery_rate_id;
4300 		  l_dist_ccid_rec.self_assessed_flag		:= 'Y';
4301 		  l_dist_ccid_rec.recoverable_flag		:= l_inv_self_ins(i).tax_recoverable_flag;
4302 		  l_dist_ccid_rec.tax_jurisdiction_id		:= l_inv_self_ins(i).tax_jurisdiction_id;
4303 		  l_dist_ccid_rec.tax_regime_id			:= l_inv_self_ins(i).tax_regime_id;
4304 		  l_dist_ccid_rec.tax_id			:= l_inv_self_ins(i).tax_id;
4305 		  l_dist_ccid_rec.internal_organization_id	:= l_inv_self_ins(i).org_id;
4306 		  l_dist_ccid_rec.tax_status_id			:= l_inv_self_ins(i).tax_status_id;
4307 		  l_dist_ccid_rec.revenue_expense_ccid		:= l_inv_self_ins(i).dist_code_combination_id;
4308 		  l_dist_ccid_rec.account_source_tax_rate_id    := l_inv_self_ins(i).account_source_tax_rate_id;
4309 
4310 	          zx_api_pub.get_tax_distribution_ccids(
4311 		            p_api_version            => 1.0,
4312 		            p_init_msg_list          => FND_API.G_TRUE,
4313 		            p_commit                 => FND_API.G_FALSE,
4314 		            p_validation_level       => FND_API.G_VALID_LEVEL_FULL,
4315 		            x_return_status          => l_return_status_service,
4316 		            x_msg_count              => l_msg_count,
4317 		            x_msg_data               => l_msg_data,
4318 			    p_dist_ccid_rec	     => l_dist_ccid_rec);
4319 
4320                  IF (l_inv_self_ins(i).line_type_lookup_code = 'REC_TAX' OR
4321                     (l_inv_self_ins(i).line_type_lookup_code = 'NONREC_TAX'
4322                      and l_dist_code_combination_id IS NULL)) THEN  --Bug6599804
4323 
4324 		  l_dist_code_combination_id	:= l_dist_ccid_rec.rec_nrec_ccid;
4325 
4326                  END IF;
4327 
4328 		  l_tax_liab_ccid		:= l_dist_ccid_rec.tax_liab_ccid;
4329 
4330 	          IF (l_return_status_service <> 'S') THEN
4331 	              -----------------------------------------------------------
4332 	              l_debug_info := 'Handle errors returned by API';
4333 	              -----------------------------------------------------------
4334 		      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4335 	                  FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4336 	              END IF;
4337 
4338 	              IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
4339 		               P_All_Error_Messages  => P_All_Error_Messages,
4340 		               P_Msg_Count           => l_msg_count,
4341 		               P_Msg_Data            => l_msg_data,
4342 		               P_Error_Code          => P_Error_Code,
4343 		               P_Calling_Sequence    => l_curr_calling_sequence)) THEN
4344 	                 NULL;
4345 	              END IF;
4346 
4347 	              RETURN FALSE;
4348 	          END IF;
4349 	        END IF;
4350 
4351 	        -------------------------------------------------------------------
4352 	        l_debug_info := 'Insert self assessed distributions';
4353 	        -------------------------------------------------------------------
4354 		IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4355 	          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4356 	        END IF;
4357 
4358 	        BEGIN
4359 	          INSERT INTO ap_self_assessed_tax_dist_all (
4360 	            accounting_date,
4361 	            accrual_posted_flag,
4362 	            assets_addition_flag,
4363 	            assets_tracking_flag,
4364 	            cash_posted_flag,
4365 	            distribution_line_number,
4366 	            dist_code_combination_id,
4367 	            invoice_id,
4368 	            last_updated_by,
4369 	            last_update_date,
4370 	            line_type_lookup_code,
4371 	            period_name,
4372 	            set_of_books_id,
4373 	            amount,
4374 	            base_amount,
4375 	            batch_id,
4376 	            created_by,
4377 	            creation_date,
4378 	            description,
4379 	            final_match_flag,
4380 	            income_tax_region,
4381 	            last_update_login,
4382 	            match_status_flag,
4383 	            posted_flag,
4384 	            po_distribution_id,
4385 	            program_application_id,
4386 	            program_id,
4387 	            program_update_date,
4388 	            quantity_invoiced,
4389 	            request_id,
4390 	            reversal_flag,
4391 	            type_1099,
4392 	            unit_price,
4393 	            encumbered_flag,
4394 	            stat_amount,
4395 	            attribute1,
4396 	            attribute10,
4397 	            attribute11,
4398 	            attribute12,
4399 	            attribute13,
4400 	            attribute14,
4401 	            attribute15,
4402 	            attribute2,
4403 	            attribute3,
4404 	            attribute4,
4405 	            attribute5,
4406 	            attribute6,
4407 	            attribute7,
4408 	            attribute8,
4409 	            attribute9,
4410 	            attribute_category,
4411 	            expenditure_item_date,
4412 	            expenditure_organization_id,
4413 	            expenditure_type,
4414 	            parent_invoice_id,
4415 	            pa_addition_flag,
4416 	            pa_quantity,
4417 	            prepay_amount_remaining,
4418 	            project_accounting_context,
4419 	            project_id,
4420 	            task_id,
4421 	            packet_id,
4422 	            awt_flag,
4423 	            awt_group_id,
4424 	            awt_tax_rate_id,
4425 	            awt_gross_amount,
4426 	            awt_invoice_id,
4427 	            awt_origin_group_id,
4428 	            reference_1,
4429 	            reference_2,
4430 	            org_id,
4431 	            awt_invoice_payment_id,
4432 	            global_attribute_category,
4433 	            global_attribute1,
4434 	            global_attribute2,
4435 	            global_attribute3,
4436 	            global_attribute4,
4437 	            global_attribute5,
4438 	            global_attribute6,
4439 	            global_attribute7,
4440 	            global_attribute8,
4441 	            global_attribute9,
4442 	            global_attribute10,
4443 	            global_attribute11,
4444 	            global_attribute12,
4445 	            global_attribute13,
4446 	            global_attribute14,
4447 	            global_attribute15,
4448 	            global_attribute16,
4449 	            global_attribute17,
4450 	            global_attribute18,
4451 	            global_attribute19,
4452 	            global_attribute20,
4453 	            receipt_verified_flag,
4454 	            receipt_required_flag,
4455 	            receipt_missing_flag,
4456 	            justification,
4457 	            expense_group,
4458 	            start_expense_date,
4459 	            end_expense_date,
4460 	            receipt_currency_code,
4461 	            receipt_conversion_rate,
4462 	            receipt_currency_amount,
4463 	            daily_amount,
4464 	            web_parameter_id,
4465 	            adjustment_reason,
4466 	            award_id,
4467 	            credit_card_trx_id,
4468 	            dist_match_type,
4469 	            rcv_transaction_id,
4470 	            invoice_distribution_id,
4471 	            parent_reversal_id,
4472 	            tax_recoverable_flag,
4473 	            merchant_document_number,
4474 	            merchant_name,
4475 	            merchant_reference,
4476 	            merchant_tax_reg_number,
4477 	            merchant_taxpayer_id,
4478 	            country_of_supply,
4479 	            matched_uom_lookup_code,
4480 	            gms_burdenable_raw_cost,
4481 	            accounting_event_id,
4482 	            prepay_distribution_id,
4483 	            upgrade_posted_amt,
4484 	            upgrade_base_posted_amt,
4485 	            inventory_transfer_status,
4486 	            company_prepaid_invoice_id,
4487 	            cc_reversal_flag,
4488 	            awt_withheld_amt,
4489 	            pa_cmt_xface_flag,
4490 	            cancellation_flag,
4491 	            invoice_line_number,
4492 	            corrected_invoice_dist_id,
4493 	            rounding_amt,
4494 	            charge_applicable_to_dist_id,
4495 	            corrected_quantity,
4496 	            related_id,
4497 	            asset_book_type_code,
4498 	            asset_category_id,
4499 	            distribution_class,
4500 	            tax_code_id,
4501 	            intended_use,
4502 	            detail_tax_dist_id,
4503 	            rec_nrec_rate,
4504 	            recovery_rate_id,
4505 	            recovery_rate_name,
4506 	            recovery_type_code,
4507 	            withholding_tax_code_id,
4508 	            taxable_amount,
4509 	            taxable_base_amount,
4510 	            tax_already_distributed_flag,
4511         	    summary_tax_line_id,
4512 	            extra_po_erv,
4513 	            prepay_tax_diff_amount,
4514 	            self_assessed_tax_liab_ccid,
4515 		    rcv_charge_addition_flag,
4516 		    self_assessed_flag
4517 	          ) VALUES (
4518 	            l_inv_self_ins(i).accounting_date,    -- accounting_date
4519 	            'N',                                  -- accrual_posted_flag
4520 	            'U',                                  -- assets_addition_flag
4521 	            'N',                                  -- assets_tracking_flag
4522 	            'N',                                  -- cash_posted_flag
4523 	            AP_ETAX_UTILITY_PKG.get_max_dist_num_self(
4524                       --P_Invoice_Header_Rec.invoice_id,
4525                       l_inv_self_ins(i).invoice_id,
4526 	              l_inv_self_ins(i).invoice_line_number)+1, --Bug7611551
4527 	                                                  -- distribution_line_number
4528 	            l_dist_code_combination_id,           -- dist_code_combination_id
4529                     l_inv_self_ins(i).invoice_id,         -- invoice_id Bug7611551
4530 	            --P_Invoice_Header_Rec.invoice_id,      -- invoice_id
4531 	            l_user_id,                            -- last_updated_by
4532 	            l_sysdate,                            -- last_update_date
4533 	            l_inv_self_ins(i).line_type_lookup_code,
4534 	                                                  -- line_type_lookup_code
4535 	            l_inv_self_ins(i).period_name,        -- period_name
4536                     l_inv_self_ins(i).set_of_books_id,         -- set_of_books_id bug7611551
4537 	            --P_Invoice_Header_Rec.set_of_books_id, -- set_of_books_id
4538 	            l_inv_self_ins(i).amount,             -- amount
4539 	            decode(l_inv_self_ins(i).base_amount,
4540                            0, decode(l_inv_self_ins(i).amount, 0, l_inv_self_ins(i).base_amount, NULL),
4541                            l_inv_self_ins(i).base_amount), -- base_amount
4542 	            l_inv_self_ins(i).batch_id,           -- batch_id  --Bug7611551
4543                     --P_Invoice_Header_Rec.batch_id,      -- batch_id
4544 	            l_user_id,                            -- created_by
4545 	            l_sysdate,                            -- creation_date
4546 	            l_inv_self_ins(i).description,        -- description
4547 	            NULL,                                 -- final_match_flag
4548 	            l_inv_self_ins(i).income_tax_region,  -- income_tax_region
4549 	            l_login_id,                           -- last_update_login
4550 	            NULL,                                 -- match_status_flag
4551 	            'N',                                  -- posted_flag
4552 	            l_inv_self_ins(i).po_distribution_id, -- po_distribution_id
4553 	            NULL,                                 -- program_application_id
4554 	            NULL,                                 -- program_id
4555 	            NULL,                                 -- program_update_date
4556 	            NULL,                                 -- quantity_invoiced
4557 	            NULL,                                 -- request_id
4558 	            'N',                                  -- reversal_flag
4559 	            l_inv_self_ins(i).type_1099,          -- type_1099
4560 	            NULL,                                 -- unit_price
4561 	            'N',                                  -- encumbered_flag
4562 	            NULL,                                 -- stat_amount
4563 	            l_inv_self_ins(i).attribute1,         -- attribute1
4564 	            l_inv_self_ins(i).attribute10,        -- attribute10
4565 	            l_inv_self_ins(i).attribute11,        -- attribute11,
4566 	            l_inv_self_ins(i).attribute12,        -- attribute12
4567 	            l_inv_self_ins(i).attribute13,        -- attribute13
4568 	            l_inv_self_ins(i).attribute14,        -- attribute14
4569 	            l_inv_self_ins(i).attribute15,        -- attribute15
4570 	            l_inv_self_ins(i).attribute2,         -- attribute2
4571 	            l_inv_self_ins(i).attribute3,         -- attribute3
4572 	            l_inv_self_ins(i).attribute4,         -- attribute4
4573 	            l_inv_self_ins(i).attribute5,         -- attribute5
4574 	            l_inv_self_ins(i).attribute6,         -- attribute6
4575 	            l_inv_self_ins(i).attribute7,         -- attribute7
4576 	            l_inv_self_ins(i).attribute8,         -- attribute8
4577 	            l_inv_self_ins(i).attribute9,         -- attribute9
4578 	            l_inv_self_ins(i).attribute_category, -- attribute_category
4579 	            l_inv_self_ins(i).expenditure_item_date,
4580 	                                                  -- expenditure_item_date
4581 	            l_inv_self_ins(i).expenditure_organization_id,
4582 	                                                  -- expenditure_organization_id
4583 	            l_inv_self_ins(i).expenditure_type,   -- expenditure_type
4584         	    l_inv_self_ins(i).parent_invoice_id,  -- parent_invoice_id
4585 	            l_inv_self_ins(i).pa_addition_flag,   -- pa_addition_flag
4586 	            l_inv_self_ins(i).pa_quantity,        -- pa_quantity
4587 	            NULL,                                 -- prepay_amount_remaining
4588 	            -- the prepay_amount_remaining will be populated for all the
4589 	            -- prepayment distributions during the payment. And later will be
4590 	            -- updated during the prepayment applications
4591 	            l_inv_self_ins(i).project_accounting_context,
4592 	                                                  -- project_accounting_context
4593 	            l_inv_self_ins(i).project_id,         -- project_id
4594 	            l_inv_self_ins(i).task_id,            -- task_id
4595 	            NULL,                                 -- packet_id
4596 	            'N',                                  -- awt_flag
4597 	            l_inv_self_ins(i).awt_group_id,       -- awt_group_id
4598 	            NULL,                                 -- awt_tax_rate_id
4599 	            NULL,                                 -- awt_gross_amount
4600 	            NULL,                                 -- awt_invoice_id
4601 	            NULL,                                 -- awt_origin_group_id
4602 	            NULL,                                 -- reference_1
4603 	            NULL,                                 -- reference_2
4604                     l_inv_self_ins(i).org_id,             -- org_id --Bug7611551
4605   	            --P_Invoice_Header_Rec.org_id,        -- org_id
4606 	            NULL,                                 -- awt_invoice_payment_id
4607 	            l_inv_self_ins(i).global_attribute_category,
4608 	                                                  -- global_attribute_category
4609 	            l_inv_self_ins(i).global_attribute1,  -- global_attribute1
4610 	            l_inv_self_ins(i).global_attribute2,  -- global_attribute2
4611 	            l_inv_self_ins(i).global_attribute3,  -- global_attribute3
4612 	            l_inv_self_ins(i).global_attribute4,  -- global_attribute4
4613 	            l_inv_self_ins(i).global_attribute5,  -- global_attribute5
4614 	            l_inv_self_ins(i).global_attribute6,  -- global_attribute6
4615 	            l_inv_self_ins(i).global_attribute7,  -- global_attribute7
4616 	            l_inv_self_ins(i).global_attribute8,  -- global_attribute8
4617 	            l_inv_self_ins(i).global_attribute9,  -- global_attribute9
4618 	            l_inv_self_ins(i).global_attribute10, -- global_attribute10
4619 	            l_inv_self_ins(i).global_attribute11, -- global_attribute11
4620 	            l_inv_self_ins(i).global_attribute12, -- global_attribute12
4621 	            l_inv_self_ins(i).global_attribute13, -- global_attribute13
4622 	            l_inv_self_ins(i).global_attribute14, -- global_attribute14
4623 	            l_inv_self_ins(i).global_attribute15, -- global_attribute15
4624 	            l_inv_self_ins(i).global_attribute16, -- global_attribute16
4625 	            l_inv_self_ins(i).global_attribute17, -- global_attribute17
4626 	            l_inv_self_ins(i).global_attribute18, -- global_attribute18
4627         	    l_inv_self_ins(i).global_attribute19, -- global_attribute19
4628 	            l_inv_self_ins(i).global_attribute20, -- global_attribute20
4629 	            NULL,                                 -- receipt_verified_flag
4630 	            NULL,                                 -- receipt_required_flag
4631 	            NULL,                                 -- receipt_missing_flag
4632 	            NULL,                                 -- justification
4633 	            NULL,                                 -- expense_group
4634 	            NULL,                                 -- start_expense_date
4635 	            NULL,                                 -- end_expense_date
4636 	            NULL,                                 -- receipt_currency_code
4637 	            NULL,                                 -- receipt_conversion_rate
4638 	            NULL,                                 -- receipt_currency_amount
4639 	            NULL,                                 -- daily_amount
4640 	            NULL,                                 -- web_parameter_id
4641 	            NULL,                                 -- adjustment_reason
4642 	            l_inv_self_ins(i).award_id,           -- award_id
4643 	            NULL,                                 -- credit_card_trx_id
4644 	            l_inv_self_ins(i).dist_match_type,    -- dist_match_type
4645 	            l_inv_self_ins(i).rcv_transaction_id, -- rcv_transaction_id
4646         	    ap_invoice_distributions_s.NEXTVAL,   -- invoice_distribution_id
4647 	            NULL,                                 -- parent_reversal_id
4648 	            l_inv_self_ins(i).tax_recoverable_flag,
4649 	                                                  -- tax_recoverable_flag
4650 	            NULL,                                 -- merchant_document_number
4651 	            NULL,                                 -- merchant_name
4652 	            NULL,                                 -- merchant_reference
4653 	            NULL,                                 -- merchant_tax_reg_number
4654 	            NULL,                                 -- merchant_taxpayer_id
4655 	            NULL,                                 -- country_of_supply
4656 	            NULL,                                 -- matched_uom_lookup_code
4657 	            NULL,                                 -- gms_burdenable_raw_cost
4658 	            NULL,                                 -- accounting_event_id
4659 	            l_inv_self_ins(i).prepay_distribution_id,  -- prepay_distribution_id
4660 	            NULL,                                 -- upgrade_posted_amt
4661 	            NULL,                                 -- upgrade_base_posted_amt
4662 	            'N',                                  -- inventory_transfer_status
4663 	            NULL,                                 -- company_prepaid_invoice_id
4664 	            NULL,                                 -- cc_reversal_flag
4665 	            NULL,                                 -- awt_withheld_amt
4666 	            NULL,                                 -- pa_cmt_xface_flag
4667 	            l_inv_self_ins(i).cancellation_flag,  -- cancellation_flag
4668 	            l_inv_self_ins(i).invoice_line_number,-- invoice_line_number
4669 	            l_inv_self_ins(i).corrected_invoice_dist_id,
4670 	                                                  -- corrected_invoice_dist_id
4671 	            l_inv_self_ins(i).rounding_amt,       -- rounding_amt
4672 	            l_inv_self_ins(i).charge_applicable_to_dist_id,
4673 	                                                 -- charge_applicable_to_dist_id
4674 	            NULL,                                 -- corrected_quantity
4675 	            NULL,                                 -- related_id
4676 	            NULL,                                 -- asset_book_type_code
4677 	            NULL,                                 -- asset_category_id
4678 	            l_inv_self_ins(i).distribution_class, -- distribution_class
4679 	            l_inv_self_ins(i).tax_code_id,        -- tax_code_id
4680 	            NULL,                                 -- intended_use,
4681 	            l_inv_self_ins(i).detail_tax_dist_id, -- detail_tax_dist_id
4682 	            l_inv_self_ins(i).rec_nrec_rate,      -- rec_nrec_rate
4683 	            l_inv_self_ins(i).recovery_rate_id,   -- recovery_rate_id
4684 	            l_inv_self_ins(i).recovery_rate_name, -- recovery_rate_name
4685 	            l_inv_self_ins(i).recovery_type_code, -- recovery_type_code
4686 	            NULL,                                 -- withholding_tax_code_id,
4687 	            l_inv_self_ins(i).taxable_amount,     -- taxable_amount
4688 	            l_inv_self_ins(i).taxable_base_amount, -- taxable_base_amount
4689 	            NULL,                                -- tax_already_distributed_flag
4690 	            l_inv_self_ins(i).summary_tax_line_id, -- summary_tax_line_id
4691 	            l_inv_self_ins(i).extra_po_erv,        -- extra_po_erv
4692 	            l_inv_self_ins(i).prepay_tax_diff_amount, -- prepay_tax_diff_amount
4693 	            l_tax_liab_ccid,                        -- self_assessed_tax_liab_ccid
4694 		    'N',				    -- rcv_charge_addition_flag
4695 		    'Y'					    -- self_assessed_flag
4696 		     );
4697 
4698 	        EXCEPTION
4699 			WHEN OTHERS THEN
4700 			     IF (SQLCODE <> -20001) THEN
4701 				 FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
4702 				 FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
4703 				 FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
4704 				 FND_MESSAGE.SET_TOKEN('PARAMETERS',
4705 					            ' P_Invoice_Id = '||l_inv_self_ins(i).invoice_id||
4706 					            ' P_Error_Code = '||P_Error_Code||
4707 					            ' P_Calling_Sequence = '||P_Calling_Sequence);
4708 				 FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
4709 			     END IF;
4710 
4711 			IF (G_LEVEL_EXCEPTION >= G_CURRENT_RUNTIME_LEVEL) THEN
4712 	           	    FND_LOG.STRING(G_LEVEL_EXCEPTION,G_MODULE_NAME||l_api_name,SQLERRM);
4713 			END IF;
4714 
4715 			APP_EXCEPTION.RAISE_EXCEPTION;
4716 	        END;
4717                 l_dist_code_combination_id    := NULL;
4718                 l_tax_liab_ccid               := NULL;
4719             END LOOP;
4720             L_INV_SELF_INS.DELETE;
4721         END IF;
4722     END LOOP;
4723     CLOSE INSERT_TAX_SELF;
4724 
4725 
4726     -------------------------------------------------------------------
4727     l_debug_info := 'Delete tax distributions';
4728     -------------------------------------------------------------------
4729     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4730         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4731     END IF;
4732 
4733     DELETE FROM ap_invoice_distributions_all aid
4734      WHERE aid.invoice_distribution_id IN
4735 		(SELECT /*+ leading(gt) cardinality(gt,1) */
4736                         dist.invoice_distribution_id
4737 	           FROM zx_trx_headers_gt		gt,
4738 			ap_invoice_distributions_all	dist
4739 		  WHERE gt.application_id         = AP_ETAX_PKG.AP_APPLICATION_ID
4740 	            AND gt.entity_code            = AP_ETAX_PKG.AP_ENTITY_CODE
4741 	            AND gt.event_class_code       IN (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
4742 	                                              AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
4743 	                                              AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
4744 	            AND gt.trx_id                 = dist.invoice_id
4745 		    AND dist.line_type_lookup_code IN ('REC_TAX', 'NONREC_TAX', 'TRV', 'TIPV', 'TERV')
4746 		    AND NOT EXISTS
4747 				(SELECT 'Tax Distributions'
4748 				   FROM zx_rec_nrec_dist zd
4749 				  WHERE zd.rec_nrec_tax_dist_id = dist.detail_tax_dist_id
4750 				   AND NVL(SELF_ASSESSED_FLAG, 'N') = 'N')-- bug 7422547
4751 		);                                                        --Bug7634436
4752 
4753 
4754     -------------------------------------------------------------------
4755     l_debug_info := 'Get self assessed distributions to delete';
4756     -------------------------------------------------------------------
4757     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4758         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4759     END IF;
4760 
4761     DELETE FROM ap_self_assessed_tax_dist_all aid
4762      WHERE aid.invoice_distribution_id IN
4763                 (SELECT /*+ leading(gt) cardinality(gt,1) */
4764 			dist.invoice_distribution_id
4765                    FROM zx_trx_headers_gt               gt,
4766                         ap_self_assessed_tax_dist_all   dist
4767                   WHERE gt.application_id         = AP_ETAX_PKG.AP_APPLICATION_ID
4768                     AND gt.entity_code            = AP_ETAX_PKG.AP_ENTITY_CODE
4769                     AND gt.event_class_code       IN (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
4770                                                       AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
4771                                                       AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
4772                     AND gt.trx_id                 = dist.invoice_id
4773                     AND dist.line_type_lookup_code IN ('REC_TAX', 'NONREC_TAX')
4774                     AND NOT EXISTS
4775                                 (SELECT 'Tax Distributions'
4776                                    FROM zx_rec_nrec_dist zd
4777                                   WHERE zd.rec_nrec_tax_dist_id = dist.detail_tax_dist_id
4778 				                   AND NVL(SELF_ASSESSED_FLAG, 'N') = 'Y')-- bug 7422547
4779                 );                                                                        --bug7634436
4780 
4781     -------------------------------------------------------------------
4782     l_debug_info := 'Synchronize line numbers for orphan distributions';
4783     -------------------------------------------------------------------
4784     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4785         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4786     END IF;
4787 
4788     UPDATE ap_invoice_distributions_all aid
4789        SET invoice_line_number = (SELECT line_number
4790                                     FROM ap_invoice_lines_all ail
4791                                    WHERE ail.invoice_id          = aid.invoice_id
4792                                      AND ail.summary_tax_line_id = aid.summary_tax_line_id
4793                                      AND rownum                  = 1)
4794      WHERE aid.invoice_distribution_id IN
4795                 (SELECT /*+ leading(gt) cardinality(gt,1) */
4796                         dist.invoice_distribution_id
4797                    FROM zx_trx_headers_gt               gt,
4798                         ap_invoice_distributions_all    dist
4799                   WHERE gt.application_id         = 200
4800                     AND gt.entity_code            = 'AP_INVOICES'
4801                     AND gt.event_class_code       IN ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
4802                     AND gt.trx_id                 = dist.invoice_id
4803                     AND dist.line_type_lookup_code IN ('REC_TAX', 'NONREC_TAX', 'TRV', 'TIPV', 'TERV')
4804                     AND dist.invoice_line_number NOT IN
4805                                                         (SELECT line_number
4806                                                            FROM ap_invoice_lines_all al
4807                                                           WHERE al.invoice_id  = dist.invoice_id)
4808                 );
4809 
4810     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4811         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Orphan Distributions updated: '||sql%rowcount);
4812     END IF;
4813 
4814     RETURN TRUE;
4815 
4816   EXCEPTION
4817     WHEN OTHERS THEN
4818       IF (SQLCODE <> -20001) THEN
4819           FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
4820           FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
4821           FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
4822           FND_MESSAGE.SET_TOKEN('PARAMETERS',
4823           		' P_Invoice_Id = '||P_Invoice_Header_Rec.Invoice_Id||
4824           		' P_Error_Code = '||P_Error_Code||
4825           		' P_Calling_Sequence = '||P_Calling_Sequence);
4826           FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
4827       END IF;
4828 
4829       IF (insert_tax_dist%ISOPEN ) THEN
4830         CLOSE insert_tax_dist;
4831       END IF;
4832 
4833       IF ( G_LEVEL_EXCEPTION >= G_CURRENT_RUNTIME_LEVEL) THEN
4834          FND_LOG.STRING(G_LEVEL_EXCEPTION,G_MODULE_NAME||l_api_name,SQLERRM);
4835       END IF;
4836 
4837       APP_EXCEPTION.RAISE_EXCEPTION;
4838 
4839   END Return_Tax_Distributions;
4840 
4841 /*=============================================================================
4842  |  FUNCTION - Return_Default_Import()
4843  |
4844  |  DESCRIPTION
4845  |      This function handles the return of default values for tax and trx lines
4846  |      after running the eTax service that validates and defaults info during the
4847  |      import program.  This function will modify the pl/sql tables used in the
4848  |      import program.
4849  |
4850  |  PARAMETERS
4851  |      P_Invoice_Header_Rec - Header info
4852  |      P_All_Error_Messages - Should API return 1 error message or allow
4853  |                             calling point to get them from message stack
4854  |      P_error_code - Error code to be returned
4855  |      P_calling_sequence -  Calling sequence
4856  |	P_Invoice_Status -Status flag to check if further processing should be done.--Bug6625518
4857  |
4858  |  MODIFICATION HISTORY
4859  |    DATE          Author         Action
4860  |    20-JAN-2004   SYIDNER        Created
4861  |
4862  *============================================================================*/
4863   FUNCTION Return_Default_Import(
4864              P_Invoice_Header_Rec        IN OUT NOCOPY AP_IMPORT_INVOICES_PKG.r_invoice_info_rec,
4865              P_Invoice_Lines_Tab         IN OUT NOCOPY AP_IMPORT_INVOICES_PKG.t_lines_table,
4866              P_All_Error_Messages        IN VARCHAR2,
4867              P_Error_Code                OUT NOCOPY VARCHAR2,
4868              P_Calling_Sequence          IN VARCHAR2,
4869 	     P_Invoice_Status		 OUT NOCOPY VARCHAR2) --Bug6625518
4870 	     RETURN BOOLEAN
4871   IS
4872 
4873     l_debug_info                 VARCHAR2(240);
4874     l_curr_calling_sequence      VARCHAR2(4000);
4875 
4876     trans_lines_gt     zx_transaction_lines_gt%ROWTYPE;
4877     tax_lines_gt       zx_import_tax_lines_gt%ROWTYPE;
4878 
4879     TYPE rej_lines_rec IS RECORD (
4880       invoice_id zx_validation_errors_gt.trx_id%TYPE,
4881       line_number zx_validation_errors_gt.trx_line_id%TYPE,
4882       rejection_code zx_validation_errors_gt.message_name%TYPE,
4883       summary_tax_line_number zx_validation_errors_gt.summary_tax_line_number%TYPE,
4884       invoice_line_id ap_invoice_lines_interface.invoice_line_id%TYPE);
4885 
4886     TYPE rej_lines_tab IS TABLE OF rej_lines_rec;
4887     rej_lines rej_lines_tab;
4888 
4889     CURSOR Trx_Lines_c (c_line_number IN NUMBER) IS
4890     SELECT *
4891       FROM zx_transaction_lines_gt
4892      WHERE trx_id = P_Invoice_Header_Rec.invoice_id
4893        AND trx_line_id = c_line_number;
4894 
4895     CURSOR Tax_Lines_c (c_line_number IN NUMBER) IS
4896     SELECT *
4897       FROM zx_import_tax_lines_gt
4898      WHERE trx_id = P_Invoice_Header_Rec.invoice_id
4899        AND summary_tax_line_number = c_line_number;
4900 
4901     CURSOR rejections_gt IS
4902     SELECT ve.trx_id invoice_id,
4903            ve.trx_line_id line_number,
4904            ve.message_name rejection_code,
4905            ve.summary_tax_line_number summary_tax_line_number,
4906            ail.invoice_line_id invoice_line_id
4907       FROM zx_validation_errors_gt ve,
4908            ap_invoice_lines_interface ail
4909      WHERE ve.application_id = 200
4910        AND ve.entity_code = 'AP_INVOICES'
4911        AND ve.event_class_code IN ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
4912        AND ve.trx_id = P_Invoice_Header_Rec.invoice_id
4913        AND ail.invoice_id = ve.trx_id;
4914        --AND ail.line_number = ve.trx_line_id; --  bug6255826
4915 
4916     -- Bug 6665695
4917     l_api_name VARCHAR2(100):='AP_ETAX_SERVICES_PKG.Return_Default_Import';
4918 
4919   BEGIN
4920     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Return_Default_Import<-' ||
4921                                P_calling_sequence;
4922     -----------------------------------------------------------------
4923     l_debug_info := 'Step 1: Get rejections from zx_trans_lines_val_errs'||
4924                     ' table';
4925     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4926         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
4927                                             l_api_name,l_debug_info);
4928     END IF;
4929     -----------------------------------------------------------------
4930     BEGIN
4931       OPEN rejections_gt;
4932       FETCH rejections_gt BULK COLLECT INTO rej_lines;
4933       CLOSE rejections_gt;
4934     END;
4935 
4936     IF (rej_lines.COUNT <> 0 ) THEN
4937       FOR i IN rej_lines.FIRST..rej_lines.LAST LOOP
4938 
4939         -----------------------------------------------------------------
4940         l_debug_info := 'Step 2: Create rejections in the import rejections '||
4941                         'table';
4942         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4943                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
4944                                                   l_api_name,l_debug_info);
4945         END IF;
4946         -----------------------------------------------------------------
4947         IF (rej_lines(i).line_number IS NULL) THEN -- rejection is at invoice level
4948           IF (AP_IMPORT_UTILITIES_PKG.insert_rejections(
4949                 p_parent_table        => 'AP_INVOICES_INTERFACE',
4950                 p_parent_id           => rej_lines(i).invoice_id,
4951                 p_reject_code         => rej_lines(i).rejection_code,
4952                 p_last_updated_by     => l_user_id,
4953                 p_last_update_login   => l_login_id,
4954                 p_calling_sequence    => l_curr_calling_sequence) <> TRUE) THEN
4955 
4956                RETURN FALSE;
4957           END IF;
4958 
4959         ELSE  -- rejection is at line level
4960 
4961           IF (AP_IMPORT_UTILITIES_PKG.insert_rejections(
4962                 p_parent_table        => 'AP_INVOICE_LINES_INTERFACE',
4963                 p_parent_id           => rej_lines(i).invoice_line_id,
4964                 p_reject_code         => rej_lines(i).rejection_code,
4965                 p_last_updated_by     => l_user_id,
4966                 p_last_update_login   => l_login_id,
4967                 p_calling_sequence    => l_curr_calling_sequence) <> TRUE) THEN
4968 		RETURN FALSE;
4969           END IF;
4970 
4971         END IF;
4972 
4973       END LOOP;
4974 
4975       p_invoice_status := 'N';--Bug6625518 Set processing flag to 'N' if there are rejections
4976     ELSE  -- there are no rejections for this invoice.  Update pl/sql tables
4977           -- with defaulted info
4978 
4979       -- No need to update invoice header import pl/sql record since
4980       -- eTax will not default any column.
4981 
4982       -----------------------------------------------------------------
4983       l_debug_info := 'Step 3: Loop through lines import pl/sql table';
4984       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4985           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
4986                                                     l_api_name,l_debug_info);
4987       END IF;
4988       -----------------------------------------------------------------
4989       IF (P_Invoice_Lines_Tab.COUNT > 0) THEN
4990         FOR i IN P_Invoice_Lines_Tab.FIRST..P_Invoice_Lines_Tab.LAST LOOP
4991 
4992           -------------------------------------------------------------------
4993           l_debug_info := 'Step 4: Get trx and tax line info to update pl/sql '||
4994                           'table';
4995             IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4996                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
4997                                                     l_api_name,l_debug_info);
4998             END IF;
4999           -------------------------------------------------------------------
5000           IF (P_Invoice_Lines_Tab(i).line_type_lookup_code <> 'TAX') THEN
5001 
5002             BEGIN
5003               OPEN Trx_Lines_c (P_Invoice_Lines_Tab(i).line_number);
5004               FETCH Trx_Lines_c INTO trans_lines_gt;
5005               CLOSE Trx_Lines_c;
5006             END;
5007 
5008             -----------------------------------------------------------------
5009             l_debug_info := 'Step 5: Update non-tax lines in pl/sql table';
5010             IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5011                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
5012                                                     l_api_name,l_debug_info);
5013             END IF;
5014             -----------------------------------------------------------------
5015             P_Invoice_Lines_Tab(i).trx_business_category := trans_lines_gt.trx_business_category;
5016             P_Invoice_Lines_Tab(i).primary_intended_use := trans_lines_gt.line_intended_use;
5017             P_Invoice_Lines_Tab(i).product_fisc_classification
5018               := trans_lines_gt.product_fisc_classification;
5019             P_Invoice_Lines_Tab(i).product_type := trans_lines_gt.product_type;
5020             P_Invoice_Lines_Tab(i).product_category := trans_lines_gt.product_category;
5021 
5022             -----------------------------------------------------------------
5023             -- Bug 6665695 -- Added assignment for Tax Classification Code
5024             -----------------------------------------------------------------
5025             l_debug_info := 'Step 5.1 Getting INPUT_TAX_CLASSIFICATION_CODE:'
5026                             ||trans_lines_gt.INPUT_TAX_CLASSIFICATION_CODE;
5027             IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5028                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
5029                                                     l_api_name,l_debug_info);
5030             END IF;
5031             -----------------------------------------------------------------
5032             P_Invoice_Lines_Tab(i).tax_classification_code :=
5033                                 trans_lines_gt.INPUT_TAX_CLASSIFICATION_CODE;
5034           ELSE  -- It is a tax line
5035 
5036             BEGIN
5037               OPEN Tax_Lines_c (P_Invoice_Lines_Tab(i).line_number);
5038               FETCH Tax_Lines_c INTO tax_lines_gt;
5039               CLOSE Tax_Lines_c;
5040             END;
5041 
5042             -----------------------------------------------------------------
5043             l_debug_info := 'Step 6: Update tax lines in pl/sql table';
5044             IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5045                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
5046                                                     l_api_name,l_debug_info);
5047             END IF;
5048             -----------------------------------------------------------------
5049             --6255826 Added Tax_regime_code and Tax assignments
5050             P_Invoice_Lines_Tab(i).tax_regime_code :=
5051                                                tax_lines_gt.tax_regime_code;
5052             P_Invoice_Lines_Tab(i).tax := tax_lines_gt.tax;
5053             P_Invoice_Lines_Tab(i).tax_jurisdiction_code := tax_lines_gt.tax_jurisdiction_code;
5054             P_Invoice_Lines_Tab(i).tax_status_code := tax_lines_gt.tax_status_code;
5055             P_Invoice_Lines_Tab(i).tax_rate_id := tax_lines_gt.tax_rate_id;
5056             P_Invoice_Lines_Tab(i).tax_rate_code := tax_lines_gt.tax_rate_code;
5057             P_Invoice_Lines_Tab(i).tax_rate := tax_lines_gt.tax_rate;
5058 
5059             IF (P_Invoice_Header_Rec.tax_only_flag = 'Y') THEN
5060               -- If the invoice is tax only, copy any defaulted values from the
5061               -- trx global temp table to the tax line
5062 
5063               BEGIN
5064                 OPEN Trx_Lines_c (P_Invoice_Lines_Tab(i).line_number);
5065                 FETCH Trx_Lines_c INTO trans_lines_gt;
5066                 CLOSE Trx_Lines_c;
5067               END;
5068 
5069               -----------------------------------------------------------------
5070               l_debug_info := 'Step 7: Update tax line if tax only invoice in pl/sql table';
5071               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5072                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||
5073                                                     l_api_name,l_debug_info);
5074               END IF;
5075               -----------------------------------------------------------------
5076               P_Invoice_Lines_Tab(i).trx_business_category :=
5077                 trans_lines_gt.trx_business_category;
5078               P_Invoice_Lines_Tab(i).primary_intended_use :=
5079                 trans_lines_gt.line_intended_use;
5080               P_Invoice_Lines_Tab(i).product_fisc_classification
5081                 := trans_lines_gt.product_fisc_classification;
5082               P_Invoice_Lines_Tab(i).product_type := trans_lines_gt.product_type;
5083               P_Invoice_Lines_Tab(i).product_category :=
5084                 trans_lines_gt.product_category;
5085 
5086             END IF;
5087           END IF;
5088 
5089         END LOOP;
5090       END IF;  -- lines pl/sql table has records.
5091       p_invoice_status := 'Y';--Bug6625518 Set processing flag to Y if no rejections are there
5092      END IF;  -- there are no rejections for this invoice
5093 
5094    RETURN TRUE;
5095 
5096   EXCEPTION
5097     WHEN OTHERS THEN
5098       IF (SQLCODE <> -20001) THEN
5099         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
5100         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
5101         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
5102         FND_MESSAGE.SET_TOKEN('PARAMETERS',
5103           ' P_Invoice_Id = '||P_Invoice_Header_Rec.Invoice_Id||
5104           ' P_Error_Code = '||P_Error_Code||
5105           ' P_Calling_Sequence = '||P_Calling_Sequence);
5106         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
5107       END IF;
5108 
5109       APP_EXCEPTION.RAISE_EXCEPTION;
5110   END Return_Default_Import;
5111 
5112 /*=============================================================================
5113  |  FUNCTION - Return_Tax_Quote()
5114  |
5115  |  DESCRIPTION
5116  |      This function handles the return of tax lines when the calculate service is
5117  |      ran for quote.  This case is specific for recurring invoices and invoice
5118  |      lines created through distribution sets.
5119  |
5120  |  PARAMETERS
5121  |      P_Invoice_Header_Rec - Header info
5122  |      P_error_code - Error code to be returned
5123  |      P_calling_sequence -  Calling sequence
5124  |
5125  |  MODIFICATION HISTORY
5126  |    DATE          Author         Action
5127  |    03-NOV-2003   SYIDNER        Created
5128  |
5129  *============================================================================*/
5130   FUNCTION Return_Tax_Quote(
5131              P_Invoice_Header_Rec        IN ap_invoices_all%ROWTYPE,
5132              P_Error_Code                OUT NOCOPY VARCHAR2,
5133              P_Calling_Sequence          IN VARCHAR2) RETURN BOOLEAN
5134 
5135   IS
5136 
5137     l_debug_info                 VARCHAR2(240);
5138     l_curr_calling_sequence      VARCHAR2(4000);
5139 
5140   BEGIN
5141 
5142     l_curr_calling_sequence := 'AP_ETAX_UTILITY_PKG.Return_Tax_Quote<-'||
5143                                P_calling_sequence;
5144 
5145     -------------------------------------------------------------------
5146     l_debug_info := 'Step 1: Get data from zx_detail_tax_lines_gt and '||
5147                     'update amount and base_amount for line(s)';
5148     -------------------------------------------------------------------
5149     BEGIN
5150       UPDATE ap_invoice_lines_all ail
5151          SET (ail.amount, ail.base_amount) =
5152              (SELECT NVL(ail.amount, 0) - SUM(NVL(zdl.tax_amt,0)),
5153                      NVL(ail.base_amount,0) - SUM(NVL(zdl.tax_amt_funcl_curr,0))
5154                 FROM zx_detail_tax_lines_gt zdl
5155              WHERE zdl.application_id = 200
5156                  AND zdl.entity_code = 'AP_INVOICES'
5157 		 AND zdl.event_class_code IN ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
5158                  AND zdl.trx_id = ail.invoice_id
5159                  AND zdl.trx_line_id = ail.line_number
5160                  AND NVL(zdl.self_assessed_flag, 'N') = 'N'
5161                  AND NVL(zdl.tax_amt_included_flag, 'N') = 'N')
5162        WHERE ail.invoice_id = P_Invoice_Header_Rec.invoice_id
5163          AND ail.line_type_lookup_code NOT IN ('TAX', 'AWT');
5164 
5165     EXCEPTION
5166       WHEN no_data_found THEN
5167         null;
5168 
5169     END;
5170 
5171     RETURN TRUE;
5172 
5173   EXCEPTION
5174     WHEN OTHERS THEN
5175       IF (SQLCODE <> -20001) THEN
5176         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
5177         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
5178         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
5179         FND_MESSAGE.SET_TOKEN('PARAMETERS',
5180           ' P_Invoice_Id = '||P_Invoice_Header_Rec.Invoice_Id||
5181           ' P_Error_Code = '||P_Error_Code||
5182           ' P_Calling_Sequence = '||P_Calling_Sequence);
5183         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
5184       END IF;
5185 
5186       APP_EXCEPTION.RAISE_EXCEPTION;
5187 
5188   END Return_Tax_Quote;
5189 
5190 /*=============================================================================
5191  |  FUNCTION - Return_Error_Messages()
5192  |
5193  |  DESCRIPTION
5194  |      This function will handle the return of the error messages from the
5195  |      eTax services.  The services can return 1 or more error messages or
5196  |      warnings.  The calling point will indicate the API if it wants the return of
5197  |      only 1 error messages through the parameter P_All_Error_Messages = N
5198  |      eventhough the service returns more than one error message.
5199  |      If the calling point requires all the error messages, it will need to get them
5200  |      directly from the message stack.
5201  |
5202  |  PARAMETERS
5203  |
5204  |  MODIFICATION HISTORY
5205  |    DATE          Author         Action
5206  |    05-NOV-2003   SYIDNER        Created
5207  |
5208  *============================================================================*/
5209   FUNCTION Return_Error_Messages(
5210              P_All_Error_Messages        IN VARCHAR2,
5211              P_Msg_Count                 IN NUMBER,
5212              P_Msg_Data                  IN VARCHAR2,
5213              P_Error_Code                OUT NOCOPY VARCHAR2,
5214              P_Calling_Sequence          IN VARCHAR2) RETURN BOOLEAN
5215 
5216   IS
5217 
5218     l_debug_info                 VARCHAR2(240);
5219     l_curr_calling_sequence      VARCHAR2(4000);
5220     l_api_name                   CONSTANT VARCHAR2(100) := 'Return_Error_Messages';
5221 
5222   BEGIN
5223 
5224     l_curr_calling_sequence := 'AP_ETAX_UTILITY_PKG.Return_Error_Messages<-'||
5225                                P_calling_sequence;
5226 
5227     -------------------------------------------------------------------
5228     l_debug_info := 'Get error message from eTax API';
5229     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5230         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5231     END IF;
5232     -------------------------------------------------------------------
5233 
5234     BEGIN
5235     SELECT message_text
5236       INTO p_error_code
5237       FROM zx_errors_gt
5238      WHERE rownum = 1;
5239     EXCEPTION
5240 	WHEN OTHERS THEN
5241 	    -------------------------------------------------------------------
5242 	    l_debug_info := 'Get error message from stack';
5243 	    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5244 	        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5245 	    END IF;
5246 	    -------------------------------------------------------------------
5247 	    IF (NVL(P_Msg_Count, 0) <= 1) THEN
5248 	         P_Error_Code := P_Msg_Data;
5249 
5250 	    ELSE
5251 	      IF (P_All_Error_Messages = 'N') THEN
5252 	        LOOP
5253 	          P_Error_Code := FND_MSG_PUB.Get;
5254 	          EXIT;
5255 	        END LOOP;
5256 	      ELSE
5257 	        P_Error_Code := NULL;
5258 	      END IF;
5259 	    END IF;
5260     END;
5261 
5262     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5263       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,p_error_code);
5264     END IF;
5265 
5266     RETURN TRUE;
5267 
5268     EXCEPTION
5269       WHEN OTHERS THEN
5270        IF (SQLCODE <> -20001) THEN
5271          FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
5272          FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
5273          FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
5274          FND_MESSAGE.SET_TOKEN('PARAMETERS',
5275             ' P_Error_Code = '||P_Error_Code||
5276              ' P_Calling_Sequence = '||P_Calling_Sequence);
5277          FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
5278        END IF;
5279 
5280        APP_EXCEPTION.RAISE_EXCEPTION;
5281 
5282   END Return_Error_Messages;
5283 
5284 /*=============================================================================
5285  |  FUNCTION - Is_Tax_Already_Calc_Inv()
5286  |
5287  |  DESCRIPTION
5288  |    This function will return TRUE if any taxable line in the invoice has the
5289  |    tax_already_calculated_flag equals Y.  It will return FALSE otherwise.
5290  |
5291  |  PARAMETERS
5292  |    P_Invoice_Id - Invoice Id
5293  |    P_Calling_Sequence - calling sequence
5294  |
5295  |  MODIFICATION HISTORY
5296  |    DATE          Author         Action
5297  |    29-DEC-2003   SYIDNER        Created
5298  |
5299  *============================================================================*/
5300   FUNCTION Is_Tax_Already_Calc_Inv(
5301              P_Invoice_Id                IN NUMBER,
5302              P_Calling_Sequence          IN VARCHAR2) RETURN BOOLEAN
5303   IS
5304 
5305     l_debug_info                        VARCHAR2(240);
5306     l_curr_calling_sequence             VARCHAR2(4000);
5307     l_tax_already_calculated_flag       VARCHAR2(1) := 'N';
5308 
5309     -- Modified this select to include the TAX only case
5310     CURSOR tax_already_calculated IS
5311     SELECT 'Y'
5312 --- Start for bug 6485124
5313     FROM   zx_lines_det_factors
5314      WHERE  trx_id = p_invoice_id
5315      AND application_id        =  200
5316      AND entity_code   =  'AP_INVOICES'
5317      AND event_class_code IN
5318          ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
5319      AND ROWNUM=1;
5320 --- End for bug 6485124
5321 -- commented for bug 6485124
5322 /*      FROM ap_invoice_lines_all
5323      WHERE invoice_id = p_invoice_id
5324        AND line_type_lookup_code <> 'AWT'
5325        AND (tax_already_calculated_flag = 'Y'
5326              OR  summary_tax_line_id IS NOT NULL)
5327        AND ROWNUM = 1; */
5328 
5329   BEGIN
5330 
5331     l_curr_calling_sequence := 'AP_ETAX_UTILITY_PKG.Is_Tax_Already_Called_Inv<-'||
5332                                P_calling_sequence;
5333 
5334     -------------------------------------------------------------------
5335     l_debug_info := 'Step 1: Get tax_already_calculated_flag for any '||
5336                     'taxable line in the invoice';
5337     -------------------------------------------------------------------
5338     OPEN tax_already_calculated;
5339     FETCH tax_already_calculated
5340      INTO l_tax_already_calculated_flag;
5341       CLOSE tax_already_calculated;
5342 
5343     IF (l_tax_already_calculated_flag = 'Y') THEN
5344       RETURN TRUE;
5345 
5346     ELSE
5347       RETURN FALSE;
5348 
5349     END IF;
5350 
5351   EXCEPTION
5352     WHEN OTHERS THEN
5353       IF (SQLCODE <> -20001) THEN
5354         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
5355         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
5356         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
5357         FND_MESSAGE.SET_TOKEN('PARAMETERS',
5358           ' P_Invoice_Id = '||P_Invoice_Id||
5359           ' P_Calling_Sequence = '||P_Calling_Sequence);
5360         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
5361       END IF;
5362 
5363       IF (tax_already_calculated%ISOPEN ) THEN
5364         CLOSE tax_already_calculated;
5365       END IF;
5366 
5367       APP_EXCEPTION.RAISE_EXCEPTION;
5368 
5369   END Is_Tax_Already_Calc_Inv;
5370 
5371 
5372 
5373 
5374  /*=============================================================================
5375  |  FUNCTION - Is_Tax_Already_Calc_Inv_char()
5376  |
5377  |  DESCRIPTION
5378  |    This function will return Y if any taxable line in the invoice has the
5379  |    tax_already_calculated_flag equals Y.  It will return N otherwise.
5380  |
5381  |    This function is same of Is_Tax_Already_Calc_Inv except that
5382  |	it returns VARCHAR instead of BOOLEAN.
5383  |
5384  |  PARAMETERS
5385  |    P_Invoice_Id - Invoice Id
5386  |    P_Calling_Sequence - calling sequence
5387  |
5388  |  MODIFICATION HISTORY
5389  |    DATE          Author         Action
5390  |    29-DEC-2003   SYIDNER        Created
5391  |
5392  *============================================================================*/
5393   FUNCTION Is_Tax_Already_Calc_Inv_char(
5394              P_Invoice_Id                IN NUMBER,
5395              P_Calling_Sequence          IN VARCHAR2) RETURN VARCHAR2
5396   IS
5397 
5398     l_debug_info                        VARCHAR2(240);
5399     l_curr_calling_sequence             VARCHAR2(4000);
5400     l_tax_already_calculated_flag       VARCHAR2(1) := 'N';
5401 
5402 
5403   BEGIN
5404 
5405     l_curr_calling_sequence := 'AP_ETAX_UTILITY_PKG.Is_Tax_Already_Called_Inv<-'||
5406                                P_calling_sequence;
5407 
5408     -------------------------------------------------------------------
5409     l_debug_info := 'Step 1: Get tax_already_calculated_flag for any '||
5410                     'taxable line in the invoice';
5411     -------------------------------------------------------------------
5412      SELECT 'Y'
5413       INTO l_tax_already_calculated_flag
5414       FROM ap_invoice_lines_all
5415      WHERE invoice_id = p_invoice_id
5416        AND line_type_lookup_code <> 'AWT'
5417        AND (tax_already_calculated_flag = 'Y'
5418              OR  summary_tax_line_id IS NOT NULL)
5419        AND ROWNUM = 1;
5420 
5421    RETURN(l_tax_already_calculated_flag);
5422 
5423   EXCEPTION
5424     WHEN NO_DATA_FOUND THEN
5425       RETURN(l_tax_already_calculated_flag);
5426     WHEN OTHERS THEN
5427       IF (SQLCODE <> -20001) THEN
5428         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
5429         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
5430         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
5431         FND_MESSAGE.SET_TOKEN('PARAMETERS',
5432           ' P_Invoice_Id = '||P_Invoice_Id||
5433           ' P_Calling_Sequence = '||P_Calling_Sequence);
5434         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
5435       END IF;
5436 
5437       APP_EXCEPTION.RAISE_EXCEPTION;
5438 
5439   END Is_Tax_Already_Calc_Inv_char;
5440 
5441 /*=============================================================================
5442  |  FUNCTION - Is_Tax_Already_Dist_Inv()
5443  |
5444  |  DESCRIPTION
5445  |    This function will return TRUE if any taxable dist in the invoice has the
5446  |    tax_already_distributed_flag equals Y.  It will return FALSE otherwise.
5447  |
5448  |  PARAMETERS
5449  |    P_Invoice_Id - Invoice Id
5450  |    P_Calling_Sequence - calling sequence
5451  |
5452  |  MODIFICATION HISTORY
5453  |    DATE          Author         Action
5454  |    29-DEC-2003   SYIDNER        Created
5455  |
5456  *============================================================================*/
5457   FUNCTION Is_Tax_Already_Dist_Inv(
5458              P_Invoice_Id                IN NUMBER,
5459              P_Calling_Sequence          IN VARCHAR2) RETURN BOOLEAN
5460   IS
5461 
5462     l_debug_info                        VARCHAR2(240);
5463     l_curr_calling_sequence             VARCHAR2(4000);
5464     l_tax_already_distributed_flag       VARCHAR2(1) := 'N';
5465 
5466     -- Modified this select to include the TAX only case
5467     CURSOR etax_already_distributed IS
5468     SELECT 'Y'
5469       FROM ap_invoice_distributions_all
5470      WHERE invoice_id = p_invoice_id
5471        AND line_type_lookup_code <> 'AWT'
5472        AND (tax_already_distributed_flag = 'Y'
5473             OR detail_tax_dist_id IS NOT NULL)
5474        AND (related_id IS NULL
5475             OR related_id = invoice_distribution_id)
5476        AND ROWNUM = 1;
5477 
5478   BEGIN
5479 
5480     l_curr_calling_sequence := 'AP_ETAX_UTILITY_PKG.Is_Tax_Already_Dist_Inv<-'||
5481                                P_calling_sequence;
5482 
5483     -------------------------------------------------------------------
5484     l_debug_info := 'Step 1: Get tax_already_Distributed_flag for any '||
5485                     'taxable line in the invoice';
5486     -------------------------------------------------------------------
5487     OPEN etax_already_distributed;
5488     FETCH etax_already_distributed INTO l_tax_already_distributed_flag;
5489     IF (etax_already_distributed%NOTFOUND) THEN
5490       CLOSE etax_already_distributed;
5491       l_tax_already_distributed_flag := 'N';
5492 
5493     END IF;
5494 
5495     IF (etax_already_distributed%ISOPEN ) THEN
5496       CLOSE etax_already_distributed;
5497     END IF;
5498 
5499     IF (l_tax_already_distributed_flag = 'Y') THEN
5500       RETURN TRUE;
5501 
5502     ELSE
5503       RETURN FALSE;
5504 
5505     END IF;
5506 
5507   EXCEPTION
5508     WHEN OTHERS THEN
5509       IF (SQLCODE <> -20001) THEN
5510         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
5511         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
5512         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
5513         FND_MESSAGE.SET_TOKEN('PARAMETERS',
5514           ' P_Invoice_Id = '||P_Invoice_Id||
5515           ' P_Calling_Sequence = '||P_Calling_Sequence);
5516         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
5517       END IF;
5518 
5519       IF (etax_already_distributed%ISOPEN ) THEN
5520         CLOSE etax_already_distributed;
5521       END IF;
5522 
5523       APP_EXCEPTION.RAISE_EXCEPTION;
5524 
5525   END Is_Tax_Already_Dist_Inv;
5526 
5527 /*=============================================================================
5528  |  FUNCTION - Get_Dist_Id_For_Tax_Dist_Id()
5529  |
5530  |  DESCRIPTION
5531  |    This function will return the invoice_distribution_id for an AP TAX (
5532  |    recoverable, non recoverable or tax variance distribution)
5533  |    based on the detail_tax_dist_id
5534  |
5535  |  PARAMETERS
5536  |    P_Tax_Dist_Id - Is the id for a Tax distribution in eTax
5537  |
5538  |  MODIFICATION HISTORY
5539  |    DATE          Author         Action
5540  |    02-APR-2004   SYIDNER        Created
5541  |
5542  *============================================================================*/
5543   FUNCTION Get_Dist_Id_For_Tax_Dist_Id(
5544              P_Tax_Dist_Id               IN NUMBER ) RETURN NUMBER
5545   IS
5546 
5547     l_debug_info                        VARCHAR2(240);
5548     l_curr_calling_sequence             VARCHAR2(4000);
5549     l_invoice_distribution_id
5550       ap_invoice_distributions_all.invoice_distribution_id%TYPE;
5551 
5552     CURSOR invoice_dist_id IS
5553     SELECT invoice_distribution_id
5554       FROM ap_invoice_distributions_all
5555      WHERE detail_tax_dist_id = P_Tax_Dist_Id;
5556 
5557   BEGIN
5558 
5559     -------------------------------------------------------------------
5560     l_debug_info := 'Step 1: Get invoice_distribution_id from tax dist id';
5561     -------------------------------------------------------------------
5562     OPEN  invoice_dist_id;
5563     FETCH invoice_dist_id INTO l_invoice_distribution_id;
5564     IF (invoice_dist_id%NOTFOUND) THEN
5565       CLOSE invoice_dist_id;
5566       l_invoice_distribution_id := NULL;
5567 
5568     END IF;
5569 
5570     IF (invoice_dist_id%ISOPEN ) THEN
5571       CLOSE invoice_dist_id;
5572     END IF;
5573 
5574     RETURN l_invoice_distribution_id;
5575 
5576   EXCEPTION
5577     WHEN OTHERS THEN
5578       IF (SQLCODE <> -20001) THEN
5579         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
5580         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
5581         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',
5582           'AP_ETAX_UTILITY_PKG.Get_Dist_Id_For_Tax_Dist_Id');
5583         FND_MESSAGE.SET_TOKEN('PARAMETERS',
5584           ' P_Tax_Dist_Id = '||P_Tax_Dist_Id);
5585         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
5586       END IF;
5587 
5588       IF (invoice_dist_id%ISOPEN ) THEN
5589         CLOSE invoice_dist_id;
5590       END IF;
5591 
5592       APP_EXCEPTION.RAISE_EXCEPTION;
5593 
5594   END Get_Dist_Id_For_Tax_Dist_Id;
5595 
5596 
5597   --ETAX: Invwkb
5598   /*=============================================================================
5599   |  FUNCTION - Is_Tax_Dist_Frozen()
5600   |
5601   |  DESCRIPTION
5602   |    This function will return TRUE when the tax distribution is frozen
5603   |    as per the following rules, else will return FALSE.
5604   |
5605   |    When the function returns TRUE, then user should not modify the tax
5606   |    distribution, and vice versa.
5607   |
5608   |  PARAMETERS
5609   |    P_Invoice_Id  - Is the invoice_id of the tax distribution
5610   |    P_Tax_Dist_Id - Is the id for a Tax distribution in eTax
5611   |
5612   |  USAGE: This function is called from ETAX security functions in APXINWKB.fmb
5613   |         from the form procedure 'IS_TAX_DIST_FROZEN'.
5614   |
5615   |  MODIFICATION HISTORY
5616   |    DATE          Author         Action
5617   |    01-JUL-2004   SMYADAM        Created
5618   |
5619   *============================================================================*/
5620   FUNCTION Is_Tax_Dist_Frozen(P_Invoice_Id IN NUMBER,
5621                               P_Tax_Dist_Id IN NUMBER,
5622 			      P_Calling_Sequence IN VARCHAR2) RETURN BOOLEAN IS
5623 
5624    l_posted_flag              ap_invoice_distributions.posted_flag%TYPE;
5625    l_encumbered_flag  	      ap_invoice_distributions.encumbered_flag%TYPE;
5626    l_parent_distribution_id   ap_invoice_distributions.invoice_distribution_id%TYPE;
5627    l_prepay_distribution_id   ap_invoice_distributions.invoice_distribution_id%TYPE;
5628    l_pa_addition_flag	      ap_invoice_distributions.pa_addition_flag%TYPE;
5629    l_match_status_flag	      ap_invoice_distributions.match_status_flag%TYPE;
5630    l_invoice_type_lookup_code ap_invoices.invoice_type_lookup_code%TYPE;
5631    l_line_type_lookup_code    ap_invoice_lines.line_type_lookup_code%TYPE;
5632    l_parent_line_type_lookup_code  ap_invoice_lines.line_type_lookup_code%TYPE;
5633    l_parent_line_number	      ap_invoice_lines.line_number%TYPE;
5634    l_discarded_flag	      ap_invoice_lines.discarded_flag%TYPE;
5635    l_reversal_flag	      ap_invoice_distributions.reversal_flag%TYPE;
5636    l_po_distribution_id       po_distributions.po_distribution_id%TYPE;
5637    l_rcv_transaction_id	      rcv_transactions.transaction_id%TYPE;
5638    l_parent_dist_amount	      ap_invoice_distributions.amount%TYPE;
5639    l_prepay_amount_remaining  ap_invoice_distributions.prepay_amount_remaining%TYPE;
5640    l_debug_info 	      VARCHAR2(1000);
5641    l_tax_dist_frozen          VARCHAR2(1) := 'N';
5642    l_curr_calling_sequence    VARCHAR2(2000);
5643   BEGIN
5644 
5645    l_curr_calling_sequence := p_calling_sequence || ' -> AP_Etax_Utility_Pkg.Is_Tax_Dist_Frozen';
5646 
5647    l_debug_info := 'Select values from ap_invoice_distributions for tax distribution id';
5648 
5649    SELECT aid1.invoice_distribution_id parent_distribution_id,
5650    	  ai.invoice_type_lookup_code ,
5651 	  ail.line_type_lookup_code,
5652 	  ail1.line_type_lookup_code parent_line_type_lookup_code,
5653 	  ail1.line_number parent_line_number,
5654 	  ail1.discarded_flag,
5655 	  aid.posted_flag,
5656    	  aid.encumbered_flag,
5657 	  aid.reversal_flag,
5658 	  aid.prepay_distribution_id,
5659 	  aid.pa_addition_flag,
5660 	  nvl(aid.match_status_flag,'N'),
5661 	  aid1.po_distribution_id,
5662 	  aid1.rcv_transaction_id,
5663 	  aid1.amount,
5664 	  aid1.prepay_amount_remaining
5665    INTO l_parent_distribution_id,
5666     	l_invoice_type_lookup_code,
5667 	l_line_type_lookup_code,
5668 	l_parent_line_type_lookup_code,
5669 	l_parent_line_number,
5670 	l_discarded_flag,
5671         l_encumbered_flag,
5672 	l_posted_flag,
5673 	l_reversal_flag,
5674 	l_prepay_distribution_id,
5675 	l_pa_addition_flag,
5676 	l_match_status_flag,
5677 	l_po_distribution_id,
5678 	l_rcv_transaction_id,
5679 	l_parent_dist_amount,
5680 	l_prepay_amount_remaining
5681    FROM ap_invoice_distributions aid,
5682    	ap_invoice_distributions aid1,
5683 	ap_invoices ai,
5684 	ap_invoice_lines ail,
5685 	ap_invoice_lines ail1
5686    WHERE ai.invoice_id = p_invoice_id
5687    AND ail.invoice_id = ai.invoice_id
5688    AND ail.line_number = aid.invoice_line_number
5689    AND aid.invoice_id = ai.invoice_id
5690    AND aid1.invoice_id = ai.invoice_id
5691    AND aid.invoice_distribution_id = p_tax_dist_id
5692    /* Outer join is needed since charge_applicable_to_dist_id can be NULL
5693       for Tax-Only lines */
5694    AND aid.charge_applicable_to_dist_id = aid1.invoice_distribution_id(+)
5695    AND ail1.invoice_id = p_invoice_id
5696    AND aid1.invoice_line_number = ail1.line_number(+);
5697 
5698    --Rule 1: Tax distribution is frozen if the parent Tax line has been discarded
5699    l_debug_info := 'Checking if parent tax line is discarded';
5700    IF (l_line_type_lookup_code = 'TAX' ) THEN  /* for exclusive case */
5701 
5702       IF (l_tax_dist_frozen = 'N' and l_discarded_flag = 'Y') THEN
5703          l_tax_dist_frozen := 'Y';
5704       END IF;
5705 
5706    ELSE /* for inclusive case */
5707 
5708       --SMYADAM: Need to validate if this below query is right ?
5709       --Basically we need to figure out if the Tax line has been discarded in ETAX
5710       --for the inclusive case. can we use cancel_flag , since
5711       --discard_flag is not available on zx_lines?
5712 
5713       /*
5714       SELECT zl.discarded_flag
5715       INTO l_discarded_flag
5716       FROM zx_lines zl,
5717            zx_rec_nrec_dist zd,
5718            ap_invoice_distributions aid
5719       WHERE zl.tax_line_id = zd.tax_line_id
5720       AND nvl(zl.reporting_only_flag, 'N') = 'N'
5721       AND zd.rec_nrec_tax_dist_id = aid.detail_tax_dist_id
5722       AND aid.invoice_distribution_id = p_tax_dist_id; */
5723 
5724       IF (l_tax_dist_frozen = 'N' and l_discarded_flag = 'Y') THEN
5725          l_tax_dist_frozen := 'Y';
5726       END IF;
5727 
5728    END IF;
5729 
5730 
5731    --Rule 2: Tax distribution is frozen if the parent item line is adjusted
5732    --	     by PO Price adjustment or is itself an adjustment.
5733    l_debug_info := 'Checking if parent item line is a adjustment or referred by a adjustment';
5734 
5735    IF (l_parent_line_number IS NOT NULL) THEN
5736       IF (l_tax_dist_frozen = 'N' AND
5737           (ap_invoice_lines_utility_pkg.is_line_a_adjustment(p_invoice_id,
5738        						     l_parent_line_number,
5739 						     l_curr_calling_sequence) or
5740            ap_invoice_lines_utility_pkg.line_referred_by_adjustment(
5741 	   					     p_invoice_id,
5742 						     l_parent_line_number,
5743 						     l_curr_calling_sequence))) THEN
5744 
5745 	   l_tax_dist_frozen := 'Y';
5746       END IF;
5747    END IF;
5748 
5749 
5750    --Rule 3: Tax distribution is frozen if the parent item line is corrected
5751    --	     or is itself a correction.
5752    l_debug_info := 'Checking if parent item line is corrected or is a correction';
5753 
5754    IF (l_parent_line_number IS NOT NULL) THEN
5755       IF (l_tax_dist_frozen = 'N' AND
5756           (ap_invoice_lines_utility_pkg.is_line_a_correction(p_invoice_id,
5757 	                                             l_parent_line_number,
5758 						     l_curr_calling_sequence) or
5759            ap_invoice_lines_utility_pkg.line_referred_by_corr(p_invoice_id,
5760                                                      l_parent_line_number,
5761 						     l_curr_calling_sequence))) THEN
5762 
5763            l_tax_dist_frozen := 'Y';
5764       END IF;
5765    END IF;
5766 
5767 
5768    --Rule 4: Tax distribution is frozen if Parent Item line is a
5769    --	     Prepayment application/unapplication
5770    l_debug_info := 'Checking if parent item line is a Prepayment
5771    			application/unapplication';
5772 
5773    IF (l_parent_line_number IS NOT NULL) THEN
5774       IF (l_tax_dist_frozen = 'N' AND l_parent_line_type_lookup_code = 'PREPAY') THEN
5775          l_tax_dist_frozen := 'Y';
5776       END IF;
5777    END IF;
5778 
5779 
5780    --Rule 5: Tax distribution is frozen if the tax distribution is
5781    --	     partially or fully accounted
5782    l_debug_info := 'Checking if tax distribution is partially or fully accounted';
5783 
5784    IF (l_tax_dist_frozen = 'N' AND l_posted_flag <> 'N') THEN
5785       l_tax_dist_frozen := 'Y';
5786    END IF;
5787 
5788 
5789    --Rule 6: Tax distribution is frozen if the tax distribution is
5790    -- 	     encumbered.
5791    l_debug_info := 'Checking if tax distribution is encumbered';
5792 
5793    IF (l_tax_dist_frozen = 'N' AND l_encumbered_flag IN ('Y','D','W','X')) THEN
5794       l_tax_dist_frozen := 'Y';
5795    END IF;
5796 
5797 
5798    --Rule 7: Tax distribution is frozen if the tax distribution is
5799    -- 	     transferred to projects.
5800    l_debug_info := 'Checking if tax distribution is transferred to Projects';
5801 
5802    IF (l_tax_dist_frozen = 'N' AND l_pa_addition_flag NOT IN ('N', 'E')) THEN
5803       l_tax_dist_frozen := 'Y';
5804    END IF;
5805 
5806 
5807    --Rule 8: Tax distribution is frozen if the tax distribution is
5808    --        part of a reversal pair.
5809    l_debug_info := 'Checking if tax distribution is part of a reversal pair';
5810 
5811    IF (l_tax_dist_frozen = 'N' AND l_reversal_flag = 'Y') THEN
5812       l_tax_dist_frozen := 'Y';
5813    END IF;
5814 
5815 
5816    --Rule 9: Tax distribution is frozen if the tax distribution is
5817    --	     validated (accounting event has been created).
5818    l_debug_info := 'Checking if tax distribution is validated';
5819 
5820    IF (l_tax_dist_frozen = 'N' AND l_match_status_flag <> 'N') THEN
5821       l_tax_dist_frozen := 'Y';
5822    END IF;
5823 
5824 
5825    --Rule 10: Tax distribution is frozen if the parent item distribution is
5826    --	      PO/RCV matched.
5827    l_debug_info := 'Checking if parent item distribution is PO/RCV matched';
5828 
5829    IF (l_parent_line_number IS NOT NULL) THEN
5830       IF (l_tax_dist_frozen = 'N' AND
5831            ( l_po_distribution_id IS NOT NULL
5832 	     OR l_rcv_transaction_id IS NOT NULL)) THEN
5833          l_tax_dist_frozen := 'Y';
5834       END IF;
5835    END IF;
5836 
5837 
5838    --Rule 11: Tax distribution is frozen if the parent item distribution is
5839    --	      part of Prepayment , and has been partially or fully applied.
5840    l_debug_info := 'Checking if parent item distribution is prepay dist that has been partially
5841    		    or fully applied';
5842 
5843    IF (l_parent_line_number IS NOT NULL and l_parent_line_type_lookup_code = 'PREPAY') THEN
5844        IF (l_tax_dist_frozen = 'N' AND
5845            nvl(l_prepay_amount_remaining,l_parent_dist_amount) <> l_parent_dist_amount) THEN
5846 	   l_tax_dist_frozen := 'Y';
5847        END IF;
5848    END IF;
5849 
5850 
5851    IF (l_tax_dist_frozen = 'Y') THEN
5852       return(TRUE);
5853    ELSE
5854       return(FALSE);
5855    END IF;
5856 
5857   EXCEPTION
5858     WHEN OTHERS THEN
5859      IF (SQLCODE <> -20001) THEN
5860         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
5861         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
5862         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
5863         FND_MESSAGE.SET_TOKEN('PARAMETERS',
5864 	                ' P_Invoice_Id = '||P_Invoice_id||
5865 	                ' P_Tax_Dist_Id = '||P_Tax_Dist_Id);
5866         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
5867      END IF;
5868 
5869      APP_EXCEPTION.RAISE_EXCEPTION;
5870 
5871   END Is_Tax_Dist_Frozen;
5872 
5873 
5874 
5875   /*=============================================================================
5876   |  FUNCTION - Is_Tax_Line_Delete_Allowed()
5877   |
5878   |  DESCRIPTION
5879   |    This function will return TRUE when Detail Tax line can be deleted in ETAX,
5880   |    else will return FALSE.
5881   |
5882   |    When the function returns TRUE, then user can delete the TAX line else
5883   |      should not be allowed to delete the TAX line.
5884   |
5885   |  PARAMETERS
5886   |    P_Invoice_Id  - Is the invoice_id of the of the Invoice which owns this
5887   |                    detail tax line indirectly through the summary tax line.
5888   |    P_Detail_Tax_Line_Id - Is the id for a Detail Tax Line in eTax
5889   |
5890   |  USAGE: This function is called from ETAX security functions in APXINWKB.fmb
5891   |         from the form procedure 'IS_TAX_LINE_DELETE_ALLOWED'.
5892   |
5893   |  MODIFICATION HISTORY
5894   |    DATE          Author         Action
5895   |    04-JUL-2004   SMYADAM        Created
5896   |
5897   *============================================================================*/
5898   FUNCTION IS_TAX_LINE_DELETE_ALLOWED(P_Invoice_Id IN NUMBER,
5899                                       P_Detail_Tax_Line_Id IN NUMBER,
5900                                       P_Calling_Sequence IN VARCHAR2) RETURN BOOLEAN IS
5901 
5902      CURSOR Tax_Distributions IS
5903      SELECT aid.invoice_distribution_id
5904      FROM ap_invoice_distributions aid,
5905           zx_rec_nrec_dist zd
5906      WHERE aid.invoice_id = p_invoice_id
5907      AND aid.detail_tax_dist_id = zd.rec_nrec_tax_dist_id
5908      AND zd.tax_line_id = p_detail_tax_line_id;
5909 
5910      l_tax_invoice_distribution_id ap_invoice_distributions.invoice_distribution_id%TYPE;
5911      l_tax_line_delete_allowed     varchar2(1) := 'Y';
5912      l_debug_info		   varchar2(1000);
5913      l_curr_calling_sequence	   varchar2(2000);
5914 
5915   BEGIN
5916 
5917    l_curr_calling_sequence := p_calling_sequence ||'->AP_Etax_Utility_Pkg.Is_Tax_Line_Delete_Allowed';
5918    l_debug_info := 'Open Tax_Distributions Cursor';
5919 
5920    OPEN tax_distributions;
5921 
5922    LOOP
5923 
5924     FETCH tax_distributions INTO l_tax_invoice_distribution_id;
5925 
5926     EXIT WHEN tax_distributions%NOTFOUND OR l_tax_line_delete_allowed = 'N';
5927 
5928     IF (ap_etax_utility_pkg.is_tax_dist_frozen(p_invoice_id => p_invoice_id,
5929     					       p_tax_dist_id => l_tax_invoice_distribution_id,
5930 					       p_calling_sequence => l_curr_calling_sequence)) THEN
5931       l_tax_line_delete_allowed := 'N';
5932     END IF;
5933 
5934    END LOOP;
5935 
5936 
5937    IF (l_tax_line_delete_allowed = 'N') THEN
5938      RETURN(FALSE);
5939    ELSE
5940      RETURN(TRUE);
5941    END IF;
5942 
5943   EXCEPTION
5944     WHEN OTHERS THEN
5945       IF (SQLCODE <> -20001) THEN
5946         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
5947         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
5948         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
5949         FND_MESSAGE.SET_TOKEN('PARAMETERS',
5950                            ' P_Invoice_Id = '||P_Invoice_id||
5951                            ' P_Detail_Tax_Line_Id = '||P_Detail_Tax_Line_Id);
5952         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
5953       END IF;
5954 
5955       APP_EXCEPTION.RAISE_EXCEPTION;
5956 
5957   END IS_TAX_LINE_DELETE_ALLOWED;
5958 
5959 /*=============================================================================
5960  |  PROCEDURE - set_tax_security_context()
5961  |
5962  |  DESCRIPTION
5963  |    This procedure will return the tax effective date. The effective date
5964  |    is used in the list of values for tax drivers and tax related attributes.
5965  |
5966  |  PARAMETERS
5967  |	p_org_id		- Operating unit identifier
5968  |	p_legal_entity_id	- Legal entity identifier.
5969  |	p_transaction_date	- Transaction Date.
5970  |    P_Related_Doc_Date  - Date of the related document.  (Eg: Standard PO)
5971  |    P_Adjusted_Doc_Date - Date of the adjusted document. (Eg: DM/CM applied to Invoice)
5972  |
5973  |  MODIFICATION HISTORY
5974  |    DATE          Author  Action
5975  |    13-AUG-2004   Sanjay  Created
5976  *============================================================================*/
5977   PROCEDURE set_tax_security_context
5978 				(p_org_id		IN NUMBER,
5979 				 p_legal_entity_id	IN NUMBER,
5980 				 p_transaction_date	IN DATE,
5981 				 p_related_doc_date	IN DATE,
5982 				 p_adjusted_doc_date	IN DATE,
5983 				 p_effective_date	OUT NOCOPY DATE,
5984 				 p_return_status	OUT NOCOPY VARCHAR2,
5985 				 p_msg_count		OUT NOCOPY NUMBER,
5986 				 p_msg_data		OUT NOCOPY VARCHAR2) IS
5987 
5988        l_debug_info	VARCHAR2(240);
5989 
5990   BEGIN
5991         ---------------------------------------------------------------
5992 	l_debug_info := 'Calling zx_api_pub.set_tax_security_context';
5993         ---------------------------------------------------------------
5994 
5995 	IF p_org_id	      IS NOT NULL AND
5996 	   p_legal_entity_id  IS NOT NULL AND
5997 	   p_transaction_date IS NOT NULL THEN
5998 
5999 	zx_api_pub.set_tax_security_context
6000 				(p_api_version		=> 1.0,          -- Bug 6469397
6001 				 p_init_msg_list	=> FND_API.G_FALSE,
6002 				 p_commit		=> FND_API.G_FALSE,
6003 				 p_validation_level	=> FND_API.G_VALID_LEVEL_FULL,
6004 				 x_return_status	=> p_return_status,
6005 				 x_msg_count		=> p_msg_count,
6006 				 x_msg_data             => p_msg_data,
6007 				 p_internal_org_id	=> p_org_id,
6008 				 p_legal_entity_id      => p_legal_entity_id,
6009 				 p_transaction_date	=> p_transaction_date,
6010 				 p_related_doc_date	=> p_related_doc_date,
6011 				 p_adjusted_doc_date	=> p_adjusted_doc_date,
6012 				 x_effective_date	=> p_effective_date);
6013 
6014 	END IF;
6015 
6016   EXCEPTION
6017     WHEN OTHERS THEN
6018       IF (SQLCODE <> -20001) THEN
6019         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
6020         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
6021         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE', 'AP_ETAX_UTILITY_PKG.set_tax_security_context');
6022         FND_MESSAGE.SET_TOKEN('PARAMETERS', 'p_org_id: '	   || p_org_id 		 ||
6023 					    'p_legal_entity_id: '  || p_legal_entity_id  ||
6024 					    'p_transaction_date: ' || p_transaction_date ||
6025 					    'p_related_doc_date: ' || p_related_doc_date);
6026         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
6027       END IF;
6028 
6029       APP_EXCEPTION.RAISE_EXCEPTION;
6030 
6031   END set_tax_security_context;
6032 
6033 
6034 /*=============================================================================
6035  |  FUNCTION get_tipv()
6036  |
6037  |  DESCRIPTION
6038  |    This function will return the invoice price variance.
6039  |
6040  *============================================================================*/
6041   FUNCTION get_tipv ( p_rate_tax_factor		IN NUMBER   ,
6042 		      p_quantity_invoiced	IN NUMBER   ,
6043 		      p_inv_unit_price		IN NUMBER   ,
6044 		      p_ref_doc_unit_price	IN NUMBER   ,
6045 		      p_ref_per_unit_nr_amt	IN NUMBER   ,
6046 		      p_pc_price_diff		IN NUMBER   ,
6047 		      p_corrected_inv_id	IN NUMBER   ,
6048 		      p_line_type		IN VARCHAR2 ,
6049 		      p_line_source		IN VARCHAR2 ,
6050 		      p_inv_currency_code	IN VARCHAR2 ,
6051 		      p_line_match_type		IN VARCHAR2 ) RETURN NUMBER IS
6052 
6053 	p_tax_ipv NUMBER;
6054 
6055   BEGIN
6056 
6057 	IF p_line_type = 'RETROITEM' THEN
6058 
6059 	   -- When a PO is retro-priced, price variances are reversed on the original document
6060 	   -- and moved to the expense account. This is done in the open interface program and
6061 	   -- the tax lines will have tax_already_calculated_flag set to 'Y'. TIPV is computed
6062 	   -- as zero for the PPA document.
6063 
6064 	   p_tax_ipv := 0;
6065 
6066 	ELSIF p_corrected_inv_id IS NULL THEN
6067 
6068            IF p_line_match_type IN ('ITEM_TO_SERVICE_RECEIPT', 'ITEM_TO_SERVICE_PO') THEN
6069 
6070               p_tax_ipv := 0;
6071 
6072            ELSE
6073               -- PO/Receipt Matched Invoice
6074 
6075 	      p_tax_ipv := p_rate_tax_factor * p_quantity_invoiced *
6076 	                   (p_inv_unit_price - p_ref_doc_unit_price) * p_ref_per_unit_nr_amt;
6077 
6078 	   END IF;
6079 
6080 	ELSIF p_corrected_inv_id IS NOT NULL THEN
6081 
6082 	   -- Price/Quantity Correction
6083 
6084 	   p_tax_ipv := p_rate_tax_factor * p_quantity_invoiced * p_pc_price_diff * p_ref_per_unit_nr_amt;
6085 
6086 	END IF;
6087 
6088 	IF p_tax_ipv IS NOT NULL THEN
6089 	   p_tax_ipv := ap_utilities_pkg.ap_round_currency (p_tax_ipv, p_inv_currency_code);
6090 	END IF;
6091 
6092 	RETURN NVL(p_tax_ipv,0);
6093 
6094   EXCEPTION
6095 
6096 	WHEN NO_DATA_FOUND THEN
6097 
6098 	     RETURN NVL(p_tax_ipv,0);
6099 
6100 	WHEN OTHERS THEN
6101 
6102 	     RETURN NVL(p_tax_ipv,0);
6103 
6104   END get_tipv;
6105 
6106 /*=============================================================================
6107  |  FUNCTION get_tipv_base()
6108  |
6109  |  DESCRIPTION
6110  |    This function will return the base invoice price variance.
6111  |
6112  *============================================================================*/
6113   FUNCTION get_tipv_base
6114 		( p_rate_tax_factor		IN NUMBER ,
6115 		  p_quantity_invoiced		IN NUMBER ,
6116 		  p_inv_unit_price		IN NUMBER ,
6117 		  p_ref_doc_unit_price		IN NUMBER ,
6118 		  p_ref_per_trx_nrec_amt	IN NUMBER ,
6119 		  p_price_diff			IN NUMBER ,
6120                   p_inv_currency_rate		IN NUMBER ,
6121 		  p_ref_doc_curr_rate		IN NUMBER ,
6122 		  p_adj_doc_curr_rate		IN NUMBER ,
6123 		  p_corrected_inv_id		IN NUMBER ,
6124 		  p_line_type			IN VARCHAR2 ,
6125 		  p_line_source			IN VARCHAR2 ,
6126 		  p_inv_currency_code		IN VARCHAR2 ,
6127 		  p_base_currency_code		IN VARCHAR2 ,
6128 		  p_line_match_type		IN VARCHAR2 ) RETURN NUMBER IS
6129 
6130 	p_tax_ipv_base NUMBER;
6131 	l_debug_info varchar2(2000);
6132 
6133         --bug 5528375
6134         l_tax_ipv      NUMBER;
6135 
6136   BEGIN
6137 
6138 	IF p_line_type = 'RETROITEM' THEN
6139 
6140 	   -- PO Price Adjustment
6141 
6142 	   p_tax_ipv_base := 0;
6143 
6144 	ELSIF p_corrected_inv_id IS NULL THEN
6145 
6146            IF p_line_match_type IN ('ITEM_TO_SERVICE_RECEIPT', 'ITEM_TO_SERVICE_PO') THEN
6147 
6148 	      p_tax_ipv_base := 0;
6149 
6150 	   ELSE
6151               -- PO/Receipt Matched Invoice
6152 
6153 	      p_tax_ipv_base := p_rate_tax_factor * p_quantity_invoiced * p_inv_currency_rate *
6154 	                        (p_inv_unit_price - p_ref_doc_unit_price ) * p_ref_per_trx_nrec_amt;
6155 
6156               -- Bug 5528375
6157               l_tax_ipv := p_rate_tax_factor * p_quantity_invoiced *
6158                            (p_inv_unit_price - p_ref_doc_unit_price) * p_ref_per_trx_nrec_amt;
6159 
6160 	   END IF;
6161 
6162 	ELSIF p_corrected_inv_id IS NOT NULL THEN
6163 
6164 	   -- Corrections
6165 
6166 	   p_tax_ipv_base := p_rate_tax_factor * p_quantity_invoiced * p_adj_doc_curr_rate *
6167 			     p_price_diff * p_ref_per_trx_nrec_amt;
6168 
6169            -- Bug 5528375
6170 	   l_tax_ipv := p_rate_tax_factor * p_quantity_invoiced * p_price_diff * p_ref_per_trx_nrec_amt;
6171 
6172 	END IF;
6173 
6174 	IF p_tax_ipv_base IS NOT NULL THEN
6175 	   p_tax_ipv_base := ap_utilities_pkg.ap_round_currency (p_tax_ipv_base, p_base_currency_code);
6176 	END IF;
6177 
6178         -- Bug 5528375
6179 	IF l_tax_ipv IS NOT NULL THEN
6180            l_tax_ipv := ap_utilities_pkg.ap_round_currency (l_tax_ipv, p_inv_currency_code);
6181         ELSE
6182            l_tax_ipv := 0;
6183         END IF;
6184 
6185         RETURN NVL(p_tax_ipv_base,l_tax_ipv);
6186 
6187   EXCEPTION
6188 
6189 	WHEN NO_DATA_FOUND THEN
6190 
6191 	     RETURN NVL(p_tax_ipv_base,0);
6192 
6193 	WHEN OTHERS THEN
6194 
6195 	     RETURN NVL(p_tax_ipv_base,0);
6196 
6197   END get_tipv_base;
6198 
6199 
6200 /*=============================================================================
6201  |  FUNCTION get_terv()
6202  |
6203  |  DESCRIPTION
6204  |    This function will return the tax exchange rate variance.
6205  *============================================================================*/
6206   FUNCTION get_terv
6207 		( p_quantity_invoiced		IN NUMBER   ,
6208 		  p_inv_curr_conv_rate		IN NUMBER   ,
6209 		  p_ref_doc_curr_conv_rate	IN NUMBER   ,
6210 		  p_app_doc_curr_conv_rate	IN NUMBER   ,
6211 		  p_adj_doc_curr_conv_rate	IN NUMBER   ,
6212 		  p_per_unit_nrec_amt		IN NUMBER   ,
6213 		  p_ref_doc_per_unit_nrec_amt	IN NUMBER   ,
6214 		  p_corrected_inv_id		IN NUMBER   ,
6215 		  p_line_type			IN VARCHAR2 ,
6216 		  p_line_source			IN VARCHAR2 ,
6217 		  p_base_currency_code		IN VARCHAR2 ) RETURN NUMBER IS
6218 
6219 	p_tax_erv NUMBER;
6220         l_debug_info varchar2(2000);
6221 
6222   BEGIN
6223 	IF p_line_type = 'RETROITEM' THEN
6224 
6225 	   -- TERV = Qi * Ni * (Ei - Ep)
6226 
6227 	   p_tax_erv := p_quantity_invoiced * p_per_unit_nrec_amt *
6228 			(p_inv_curr_conv_rate - p_ref_doc_curr_conv_rate);
6229 
6230 	ELSIF p_corrected_inv_id IS NULL THEN
6231 
6232 	   -- TERV = Qi * Np * (Ei - Ep)
6233 
6234 	   p_tax_erv := p_quantity_invoiced * p_ref_doc_per_unit_nrec_amt *
6235 			(p_inv_curr_conv_rate - nvl(p_app_doc_curr_conv_rate,p_ref_doc_curr_conv_rate));
6236 
6237 	ELSIF p_corrected_inv_id IS NOT NULL THEN
6238 
6239 	   -- TERV = Qi * Ni * (Eij - Ei).
6240 
6241 	   p_tax_erv  := p_quantity_invoiced * p_per_unit_nrec_amt *
6242 			 (p_inv_curr_conv_rate - p_adj_doc_curr_conv_rate);
6243 
6244 	END IF;
6245 
6246 	IF p_tax_erv IS NOT NULL THEN
6247 	   p_tax_erv := ap_utilities_pkg.ap_round_currency (p_tax_erv, p_base_currency_code);
6248 	END IF;
6249 
6250 	RETURN NVL(p_tax_erv, 0);
6251 
6252   EXCEPTION
6253 	WHEN NO_DATA_FOUND THEN
6254 
6255 	     RETURN NVL(p_tax_erv,0);
6256 
6257 	WHEN OTHERS THEN
6258 
6259 	     RETURN NVL(p_tax_erv,0);
6260 
6261   END get_terv;
6262 
6263 /*=============================================================================
6264  |  FUNCTION get_tv()
6265  |
6266  |  DESCRIPTION
6267  |    This function will return the invoice price variance.
6268  |
6269  *============================================================================*/
6270   FUNCTION get_tv ( p_rate_tax_factor		IN NUMBER   ,
6271 		    p_quantity_invoiced		IN NUMBER   ,
6272 		    p_inv_per_unit_nrec         IN NUMBER   ,
6273 		    p_ref_per_unit_nrec         IN NUMBER   ,
6274 		    p_inv_per_trx_cur_unit_nrec IN NUMBER   ,
6275 		    p_ref_per_trx_cur_unit_nrec	IN NUMBER   ,
6276 		    p_pc_price_diff		IN NUMBER   ,
6277 		    p_corrected_inv_id		IN NUMBER   ,
6278 		    p_line_type			IN VARCHAR2 ,
6279 		    p_line_source		IN VARCHAR2 ,
6280 		    p_inv_currency_code		IN VARCHAR2 ,
6281 		    p_line_match_type		IN VARCHAR2 ,
6282 		    p_unit_price		IN NUMBER   ) RETURN NUMBER IS
6283 
6284 	p_tax_tv NUMBER;
6285 	l_debug_info varchar2(2000);
6286 
6287   BEGIN
6288 
6289 	/*
6290 	  ni = p_inv_per_unit_nrec
6291 	  np = p_ref_per_unit_nrec
6292 	  ti = p_inv_per_trx_cur_unit_nrec
6293 	  tp = p_ref_per_trx_cur_unit_nrec
6294 	*/
6295 
6296 	IF p_line_type = 'RETROITEM' THEN
6297 
6298 	   -- PPA Document. TV = Qi * Ni * (ti - tp)
6299 
6300 	   	   p_tax_tv := p_quantity_invoiced * p_inv_per_unit_nrec *
6301 		       (ap_utilities_pkg.ap_round_currency (p_inv_per_trx_cur_unit_nrec,p_inv_currency_code) - ap_utilities_pkg.ap_round_currency (p_ref_per_trx_cur_unit_nrec,p_inv_currency_code)); --Bug7213680
6302 
6303 	ELSIF p_corrected_inv_id IS NULL THEN
6304 
6305 	   IF p_line_match_type IN ('ITEM_TO_SERVICE_RECEIPT', 'ITEM_TO_SERVICE_PO') THEN
6306 
6307               p_tax_tv := p_quantity_invoiced * p_unit_price *
6308                           (ap_utilities_pkg.ap_round_currency (p_inv_per_trx_cur_unit_nrec,p_inv_currency_code) - ap_utilities_pkg.ap_round_currency (p_ref_per_trx_cur_unit_nrec,p_inv_currency_code)); --Bug7213680
6309 
6310 	   ELSE
6311               -- PO/Receipt Matched Invoice. TV = Qi * (ni - np)
6312 
6313               p_tax_tv := p_quantity_invoiced * (ap_utilities_pkg.ap_round_currency(p_inv_per_unit_nrec,p_inv_currency_code) - ap_utilities_pkg.ap_round_currency(p_ref_per_unit_nrec,p_inv_currency_code)); --Bug7213680
6314 
6315 	   END IF;
6316 
6317 	ELSIF p_corrected_inv_id IS NOT NULL THEN
6318 
6319 	   -- Price/Quantity Correction. TV = RTF * Qi * Price Diff * ti
6320 
6321 	   p_tax_tv := p_rate_tax_factor * p_quantity_invoiced * p_pc_price_diff * p_inv_per_trx_cur_unit_nrec;
6322 
6323 	END IF;
6324 
6325 	IF p_tax_tv IS NOT NULL THEN
6326 	   p_tax_tv := ap_utilities_pkg.ap_round_currency (p_tax_tv, p_inv_currency_code);
6327 	END IF;
6328 
6329 	RETURN NVL(p_tax_tv,0);
6330 
6331   EXCEPTION
6332 	WHEN NO_DATA_FOUND THEN
6333 
6334 	     RETURN NVL(p_tax_tv,0);
6335 
6336 	WHEN OTHERS THEN
6337 
6338 	     RETURN NVL(p_tax_tv,0);
6339 
6340   END get_tv;
6341 
6342 /*=============================================================================
6343  |  FUNCTION get_tv_base()
6344  |
6345  |  DESCRIPTION
6346  |    This function will return the invoice price variance.
6347  |
6348  *============================================================================*/
6349   FUNCTION get_tv_base ( p_rate_tax_factor		IN NUMBER   ,
6350 			 p_quantity_invoiced		IN NUMBER   ,
6351 			 p_inv_per_unit_nrec		IN NUMBER   ,
6352 			 p_ref_per_unit_nrec		IN NUMBER   ,
6353 			 p_inv_per_trx_cur_unit_nrec	IN NUMBER   ,
6354 			 p_ref_per_trx_cur_unit_nrec	IN NUMBER   ,
6355 			 p_inv_curr_rate		IN NUMBER   ,
6356 			 p_ref_doc_curr_rate		IN NUMBER   ,
6357 			 p_pc_price_diff		IN NUMBER   ,
6358 			 p_corrected_inv_id		IN NUMBER   ,
6359 			 p_line_type			IN VARCHAR2 ,
6360 			 p_line_source			IN VARCHAR2 ,
6361                          p_base_currency_code		IN VARCHAR2 ,
6362 			 p_line_match_type		IN VARCHAR2 ,
6363 			 p_unit_price			IN NUMBER   ) RETURN NUMBER IS
6364 
6365 	p_tax_tv_base NUMBER;
6366 	l_debug_info  varchar2(2000);
6367 
6368   BEGIN
6369 	/*
6370 	  ni = p_inv_per_unit_nrec
6371 	  np = p_ref_per_unit_nrec
6372 	  ti = p_inv_per_trx_cur_unit_nrec
6373 	  tp = p_ref_per_trx_cur_unit_nrec
6374 	*/
6375 
6376 	IF p_line_type = 'RETROITEM' THEN
6377 
6378 	   IF p_line_source = 'PO PRICE ADJUSTMENT' THEN
6379 
6380 	      -- PO Price Adjustment Line.
6381 	      -- tv_base = Qi * ni * (ti - tp) * Ep
6382 
6383 	      p_tax_tv_base := p_quantity_invoiced * p_inv_per_unit_nrec * p_ref_doc_curr_rate *
6384 			       (ap_utilities_pkg.ap_round_currency(p_inv_per_trx_cur_unit_nrec,p_base_currency_code) - ap_utilities_pkg.ap_round_currency(p_ref_per_trx_cur_unit_nrec,p_base_currency_code));--Bug7213680
6385 
6386 	   END IF;
6387 
6388 
6389 	ELSIF p_corrected_inv_id IS NULL THEN
6390 
6391            IF p_line_match_type IN ('ITEM_TO_SERVICE_RECEIPT', 'ITEM_TO_SERVICE_PO') THEN
6392 
6393               p_tax_tv_base := p_quantity_invoiced * p_unit_price * p_inv_curr_rate *
6394                                (ap_utilities_pkg.ap_round_currency(p_inv_per_trx_cur_unit_nrec,p_base_currency_code) - ap_utilities_pkg.ap_round_currency(p_ref_per_trx_cur_unit_nrec,p_base_currency_code));--Bug7213680
6395 
6396            ELSE
6397 
6398               -- PO/Receipt Matched Invoice.
6399 	      -- tv_base = Qi * (ni - np) * Ei
6400 
6401 	      p_tax_tv_base := p_quantity_invoiced * p_inv_curr_rate *
6402 	                       (ap_utilities_pkg.ap_round_currency(p_inv_per_unit_nrec,p_base_currency_code) - ap_utilities_pkg.ap_round_currency(p_ref_per_unit_nrec,p_base_currency_code));--Bug7213680
6403 	   END IF;
6404 
6405 	ELSIF p_corrected_inv_id IS NOT NULL THEN
6406 
6407 	   -- Price/Quantity Correction.
6408 	   -- tv_base = RTF * Qi * Pi * ti * Ei
6409 
6410 	   p_tax_tv_base := p_rate_tax_factor * p_quantity_invoiced * p_pc_price_diff *
6411 			    p_inv_curr_rate * p_inv_per_trx_cur_unit_nrec;
6412 
6413 	END IF;
6414 
6415 	IF p_tax_tv_base IS NOT NULL THEN
6416 	   p_tax_tv_base := ap_utilities_pkg.ap_round_currency (p_tax_tv_base, p_base_currency_code);
6417 	END IF;
6418 
6419 	RETURN NVL(p_tax_tv_base,0);
6420 
6421   EXCEPTION
6422 
6423 	WHEN NO_DATA_FOUND THEN
6424 
6425 	     RETURN NVL(p_tax_tv_base,0);
6426 
6427 	WHEN OTHERS THEN
6428 
6429 	     RETURN NVL(p_tax_tv_base,0);
6430 
6431   END get_tv_base;
6432 
6433 /*=============================================================================
6434  |  PROCEDURE - get_header_tax_attr_desc()
6435  |
6436  |  DESCRIPTION
6437  |    This procedure will return the description of the following tax drivers
6438  |    stored at the document header.
6439  |    1. Taxation Country
6440  |    2. Document Sub Type
6441  |    3. Related Invoice
6442  |
6443  |  PARAMETERS
6444  |    P_Taxation_Country        - Taxation Country
6445  |    P_Document_Sub_Type       - Document Sub Type
6446  |    P_Tax_Related_Invoice_Id  - Related Invoice
6447  |
6448  |  MODIFICATION HISTORY
6449  |    DATE          Author  Action
6450  |    13-AUG-2004   Sanjay  Created
6451  *============================================================================*/
6452   PROCEDURE get_header_tax_attr_desc
6453                 ( p_taxation_country            IN         VARCHAR2,
6454                   p_document_sub_type           IN         VARCHAR2,
6455                   p_tax_related_inv_id          IN         NUMBER,
6456                   p_taxation_country_desc       OUT NOCOPY VARCHAR2,
6457                   p_document_sub_type_desc      OUT NOCOPY VARCHAR2,
6458                   p_tax_related_inv_num         OUT NOCOPY VARCHAR2,
6459                   p_calling_sequence            IN         VARCHAR2) IS
6460 
6461         CURSOR c_txn_ctry (c_ctry_code VARCHAR2) IS
6462         SELECT territory_short_name
6463           FROM fnd_territories_tl
6464          WHERE territory_code = c_ctry_code
6465            AND language = userenv ('LANG');
6466 
6467         CURSOR c_tax_rel_inv_num (c_inv_id VARCHAR2) IS
6468         SELECT ai.invoice_num tax_related_invoice_num
6469           FROM ap_invoices ai
6470          WHERE ai.invoice_id = c_inv_id;
6471 
6472         CURSOR c_doc_sub_type (c_class_code VARCHAR2,
6473                                c_ctry_code  VARCHAR2) IS
6474         SELECT classification_name
6475           FROM zx_fc_intended_use_v
6476          WHERE classification_code = c_class_code
6477            AND country_code        = c_ctry_code;
6478 
6479         l_debug_info                 VARCHAR2(240);
6480         l_curr_calling_sequence      VARCHAR2(4000);
6481 
6482   BEGIN
6483         l_curr_calling_sequence := 'AP_ETAX_UTILITY_PKG.Get_Header_Tax_Attr_Desc<-' ||
6484                                     p_calling_sequence;
6485 
6486         --------------------------------------------------
6487         l_debug_info := 'Step 1: Get Taxation Country';
6488         --------------------------------------------------
6489         IF p_taxation_country IS NOT NULL THEN
6490 
6491            OPEN  c_txn_ctry (p_taxation_country);
6492            FETCH c_txn_ctry
6493            INTO  p_taxation_country_desc;
6494            CLOSE c_txn_ctry;
6495 
6496         END IF;
6497 
6498         --------------------------------------------------
6499         l_debug_info := 'Step 2: Get Related Inv Number';
6500         --------------------------------------------------
6501         IF p_tax_related_inv_id IS NOT NULL THEN
6502 
6503            OPEN  c_tax_rel_inv_num (p_tax_related_inv_id);
6504            FETCH c_tax_rel_inv_num
6505            INTO  p_tax_related_inv_num;
6506            CLOSE c_tax_rel_inv_num;
6507 
6508         END IF;
6509 
6510         --------------------------------------------------
6511         l_debug_info := 'Step 3: Get Document Sub Type';
6512         --------------------------------------------------
6513         IF p_document_sub_type IS NOT NULL AND
6514            p_taxation_country  IS NOT NULL THEN
6515 
6516            OPEN  c_doc_sub_type (p_document_sub_type,
6517                                  p_taxation_country);
6518            FETCH c_doc_sub_type
6519            INTO  p_document_sub_type_desc;
6520            CLOSE c_doc_sub_type;
6521 
6522         END IF;
6523 
6524   EXCEPTION
6525         WHEN OTHERS THEN
6526              IF (SQLCODE <> -20001) THEN
6527                 fnd_message.set_name ('SQLAP', 'AP_DEBUG');
6528                 fnd_message.set_token('ERROR', SQLERRM);
6529                 fnd_message.set_token('CALLING_SEQUENCE', l_curr_calling_sequence);
6530                 fnd_message.set_token('PARAMETERS',
6531                                       ' p_taxation_country   = '||p_taxation_country  ||
6532                                       ' p_document_sub_type  = '||p_document_sub_type ||
6533                                       ' p_tax_related_inv_id = '||p_tax_related_inv_id);
6534                 fnd_message.set_token('DEBUG_INFO',l_debug_info);
6535              END IF;
6536              app_exception.raise_exception;
6537 
6538 
6539   END get_header_tax_attr_desc;
6540 
6541 
6542 /*=============================================================================
6543  |  PROCEDURE - get_taxable_line_attr_desc()
6544  |
6545  |  DESCRIPTION
6546  |    This procedure will return the descriptions of tax drivers on the taxable
6547  |    line.
6548  |
6549  |  PARAMETERS
6550  |    P_Taxation_Country        - Taxation Country
6551  |    P_Trx_Bus_Category        - Transaction Business Category
6552  |    P_Prd_Fisc_Class          - Product Fiscal Classification
6553  |    P_User_Fisc_Class         - User Defined Fiscal Classification
6554  |    P_Prim_Int_Use            - Primary Intended Use
6555  |    P_Product_Type            - Product Type
6556  |    P_Product_Category        - Product Category
6557  |    P_Inv_Item_Id             - Inventory Item Identifier
6558  |    P_Org_Id                  - Organization Identifier
6559  |
6560  |  MODIFICATION HISTORY
6561  |    DATE          Author  Action
6562  |    13-AUG-2004   Sanjay  Created
6563  *============================================================================*/
6564   PROCEDURE get_taxable_line_attr_desc
6565                 ( p_taxation_country            IN         VARCHAR2,
6566                   p_trx_bus_category            IN         VARCHAR2,
6567                   p_prd_fisc_class              IN         VARCHAR2,
6568                   p_user_fisc_class             IN         VARCHAR2,
6569                   p_prim_int_use                IN         VARCHAR2,
6570                   p_product_type                IN         VARCHAR2,
6571                   p_product_category            IN         VARCHAR2,
6572                   p_inv_item_id                 IN         NUMBER,
6573                   p_org_id                      IN         NUMBER,
6574                   p_trx_bus_category_desc       OUT NOCOPY VARCHAR2,
6575                   p_prd_fisc_class_desc         OUT NOCOPY VARCHAR2,
6576                   p_user_fisc_class_desc        OUT NOCOPY VARCHAR2,
6577                   p_prim_int_use_desc           OUT NOCOPY VARCHAR2,
6578                   p_product_type_desc           OUT NOCOPY VARCHAR2,
6579                   p_product_category_desc       OUT NOCOPY VARCHAR2,
6580                   p_calling_sequence            IN         VARCHAR2) IS
6581 
6582         CURSOR c_bus_cat (c_class_code VARCHAR2,
6583                           c_txn_ctry   VARCHAR2) IS
6584         SELECT classification_name
6585         FROM   zx_fc_business_categories_v
6586         WHERE  classification_code = c_class_code
6587         AND    (country_code       = c_txn_ctry OR
6588                 country_code IS NULL);
6589 
6590         CURSOR c_fisc_class (c_class_code VARCHAR2,
6591                              c_txn_ctry   VARCHAR2) IS
6592         SELECT classification_name
6593         FROM   zx_fc_product_fiscal_v
6594         WHERE  classification_code = c_class_code
6595         AND    (country_code       = c_txn_ctry OR
6596                 country_code IS NULL);
6597 
6598         CURSOR c_user_fisc_class (c_class_code VARCHAR2,
6599                                   c_txn_ctry   VARCHAR2) IS
6600         SELECT classification_name
6601         FROM   zx_fc_user_defined_v
6602         WHERE  classification_code = c_class_code
6603         AND    (country_code       = c_txn_ctry OR
6604                 country_code IS NULL);
6605 
6606         CURSOR c_prim_int_use (c_class_code VARCHAR2,
6607                                c_txn_ctry   VARCHAR2) IS
6608         SELECT classification_name
6609         FROM   zx_fc_intended_use_v
6610         WHERE  classification_code = c_class_code
6611         AND    (country_code       = c_txn_ctry OR
6612                 country_code IS NULL);
6613 
6614 	CURSOR c_product_type (c_class_code  VARCHAR2) IS
6615         SELECT classification_name
6616         FROM   zx_product_types_v
6617 	WHERE  classification_code = c_class_code;
6618 
6619         CURSOR c_product_category (c_class_code VARCHAR2,
6620                                    c_txn_ctry   VARCHAR2) IS
6621         SELECT classification_name
6622         FROM   zx_fc_product_categories_v
6623         WHERE  classification_code = c_class_code
6624         AND    (country_code       = c_txn_ctry OR
6625                 country_code IS NULL);
6626 
6627         l_debug_info                 VARCHAR2(240);
6628         l_curr_calling_sequence      VARCHAR2(4000);
6629 
6630 BEGIN
6631 
6632         l_curr_calling_sequence := 'AP_ETAX_UTILITY_PKG.Get_Taxable_Line_Attr_Desc<-' ||
6633                                     p_calling_sequence;
6634 
6635         ------------------------------------------------------------------
6636         l_debug_info := 'Step 1: Get Business Category';
6637         ------------------------------------------------------------------
6638         IF p_trx_bus_category IS NOT NULL THEN
6639 
6640            OPEN  c_bus_cat (p_trx_bus_category,
6641                             p_taxation_country);
6642            FETCH c_bus_cat
6643            INTO  p_trx_bus_category_desc;
6644            CLOSE c_bus_cat;
6645 
6646         END IF;
6647 
6648         ------------------------------------------------------------------
6649         l_debug_info := 'Step 2: Get Fiscal Classification';
6650         ------------------------------------------------------------------
6651         IF p_prd_fisc_class IS NOT NULL THEN
6652 
6653            OPEN  c_fisc_class (p_prd_fisc_class,
6654                                p_taxation_country);
6655            FETCH c_fisc_class
6656            INTO  p_prd_fisc_class_desc;
6657            CLOSE c_fisc_class;
6658 
6659         END IF;
6660 
6661         ------------------------------------------------------------------
6662         l_debug_info := 'Step 3: Get User Defined Fiscal Classification';
6663         ------------------------------------------------------------------
6664         IF p_user_fisc_class IS NOT NULL THEN
6665 
6666            OPEN  c_user_fisc_class (p_user_fisc_class,
6667                                     p_taxation_country);
6668            FETCH c_user_fisc_class
6669            INTO  p_user_fisc_class_desc;
6670            CLOSE c_user_fisc_class;
6671 
6672         END IF;
6673 
6674         ------------------------------------------------------------------
6675         l_debug_info := 'Step 4: Get Primary Intended Use';
6676         ------------------------------------------------------------------
6677         IF p_prim_int_use IS NOT NULL THEN
6678 
6679            OPEN  c_prim_int_use (p_prim_int_use,
6680                                  p_taxation_country);
6681            FETCH c_prim_int_use
6682            INTO  p_prim_int_use_desc;
6683            CLOSE c_prim_int_use;
6684 
6685         END IF;
6686 
6687         ------------------------------------------------------------------
6688         l_debug_info := 'Step 5: Get Product Type';
6689         ------------------------------------------------------------------
6690         IF p_product_type IS NOT NULL THEN
6691 
6692 	   OPEN  c_product_type (p_product_type);
6693            FETCH c_product_type
6694            INTO  p_product_type_desc;
6695            CLOSE c_product_type;
6696 
6697         END IF;
6698 
6699         ------------------------------------------------------------------
6700         l_debug_info := 'Step 5: Get Product Category';
6701         ------------------------------------------------------------------
6702         IF p_product_category IS NOT NULL THEN
6703 
6704            OPEN  c_product_category (p_product_category,
6705                                      p_taxation_country);
6706            FETCH c_product_category
6707            INTO  p_product_category_desc;
6708            CLOSE c_product_category;
6709 
6710         END IF;
6711 
6712   EXCEPTION
6713         WHEN OTHERS THEN
6714              IF (SQLCODE <> -20001) THEN
6715                 fnd_message.set_name ('SQLAP', 'AP_DEBUG');
6716                 fnd_message.set_token('ERROR', SQLERRM);
6717                 fnd_message.set_token('CALLING_SEQUENCE', l_curr_calling_sequence);
6718                 fnd_message.set_token('PARAMETERS',
6719                                       ' p_taxation_country   = '||p_taxation_country  ||
6720                                       ' p_trx_bus_category   = '||p_trx_bus_category  ||
6721                                       ' p_prd_fisc_class     = '||p_prd_fisc_class    ||
6722                                       ' p_user_fisc_class    = '||p_user_fisc_class   ||
6723                                       ' p_prim_int_use       = '||p_prim_int_use      ||
6724                                       ' p_product_type       = '||p_product_type      ||
6725                                       ' p_product_category   = '||p_product_category  ||
6726                                       ' p_inv_item_id        = '||p_inv_item_id       ||
6727                                       ' p_org_id             = '||p_org_id            );
6728 
6729                 fnd_message.set_token('DEBUG_INFO',l_debug_info);
6730              END IF;
6731              app_exception.raise_exception;
6732 
6733   END get_taxable_line_attr_desc;
6734 
6735 /*=============================================================================
6736  |  PROCEDURE - get_tax_line_attr_desc()
6737  |
6738  |  DESCRIPTION
6739  |    This procedure will return the descriptions of tax related attributes
6740  |    on the tax line.
6741  |
6742  |  PARAMETERS
6743  |    P_Taxation_Country        - Taxation Country
6744  |    P_Tax_Regime_Code         - Tax Regime
6745  |    P_Tax                     - Tax
6746  |    P_Tax_Jurisdiction_Code   - Tax Jurisidiction
6747  |    P_Tax_Status_Code         - Tax Status
6748  |
6749  |  MODIFICATION HISTORY
6750  |    DATE          Author  Action
6751  |    13-AUG-2004   Sanjay  Created
6752  *============================================================================*/
6753   PROCEDURE get_tax_line_attr_desc
6754                 ( p_taxation_country            IN         VARCHAR2,
6755                   p_tax_regime_code             IN         VARCHAR2,
6756                   p_tax                         IN         VARCHAR2,
6757                   p_tax_jurisdiction_code       IN         VARCHAR2,
6758                   p_tax_status_code             IN         VARCHAR2,
6759                   p_tax_regime_code_desc        OUT NOCOPY VARCHAR2,
6760                   p_tax_desc                    OUT NOCOPY VARCHAR2,
6761                   p_tax_jurisdiction_desc       OUT NOCOPY VARCHAR2,
6762                   p_tax_status_code_desc        OUT NOCOPY VARCHAR2,
6763                   p_calling_sequence            IN         VARCHAR2) AS
6764 
6765         CURSOR c_tax_regime (c_tax_regime_code VARCHAR2,
6766                              c_txn_ctry        VARCHAR2) IS
6767         SELECT vl.tax_regime_name
6768         FROM   zx_regimes_vl vl
6769         WHERE  vl.country_code    = c_txn_ctry
6770         AND    vl.tax_regime_code = c_tax_regime_code;
6771 
6772         CURSOR c_tax (c_tax        VARCHAR2,
6773                       c_tax_regime VARCHAR2) IS
6774         SELECT tax_full_name
6775         FROM   zx_sco_taxes
6776         WHERE  tax_regime_code = c_tax_regime
6777         AND    tax             = c_tax;
6778 
6779         CURSOR c_tax_jurisdiction (c_tax              VARCHAR2,
6780                                    c_tax_regime       VARCHAR2,
6781                                    c_tax_jurisdiction VARCHAR2) IS
6782         SELECT tax_jurisdiction_name
6783           FROM zx_jurisdictions_vl
6784          WHERE tax_regime_code       = c_tax_regime
6785            AND tax                   = c_tax
6786            AND tax_jurisdiction_code = c_tax_jurisdiction;
6787 
6788         CURSOR c_tax_status (c_tax        VARCHAR2,
6789                              c_tax_regime VARCHAR2,
6790                              c_tax_status VARCHAR2) IS
6791         SELECT tax_status_name
6792         FROM   zx_sco_status
6793         WHERE  tax_regime_code  = c_tax_regime
6794         AND    tax              = c_tax
6795         AND    tax_status_code  = c_tax_status;
6796 
6797         l_debug_info                 VARCHAR2(240);
6798         l_curr_calling_sequence      VARCHAR2(4000);
6799 
6800   BEGIN
6801         l_curr_calling_sequence := 'AP_ETAX_UTILITY_PKG.Get_Taxable_Line_Attr_Desc<-' ||
6802                                     p_calling_sequence;
6803 
6804         ------------------------------------------------
6805         l_debug_info := 'Step 1: Get Tax Regime';
6806         ------------------------------------------------
6807         IF p_tax_regime_code IS NOT NULL THEN
6808 
6809            OPEN  c_tax_regime (p_tax_regime_code,
6810                                p_taxation_country);
6811            FETCH c_tax_regime
6812            INTO  p_tax_regime_code_desc;
6813            CLOSE c_tax_regime;
6814 
6815         END IF;
6816 
6817         ------------------------------------------------
6818         l_debug_info := 'Step 2: Get Tax';
6819         ------------------------------------------------
6820         IF p_tax IS NOT NULL THEN
6821 
6822            OPEN  c_tax (p_tax,
6823                         p_tax_regime_code);
6824            FETCH c_tax
6825            INTO  p_tax_desc;
6826            CLOSE c_tax;
6827 
6828         END IF;
6829 
6830         ------------------------------------------------
6831         l_debug_info := 'Step 3: Get Tax Jurisdiction';
6832         ------------------------------------------------
6833         IF p_tax_jurisdiction_code IS NOT NULL THEN
6834 
6835            OPEN  c_tax_jurisdiction (p_tax,
6836                                      p_tax_regime_code,
6837                                      p_tax_jurisdiction_code);
6838            FETCH c_tax_jurisdiction
6839            INTO  p_tax_jurisdiction_desc;
6840            CLOSE c_tax_jurisdiction;
6841 
6842         END IF;
6843 
6844         ------------------------------------------------
6845         l_debug_info := 'Step 4: Get Tax Status';
6846         ------------------------------------------------
6847         IF p_tax_status_code IS NOT NULL THEN
6848 
6849            OPEN  c_tax_status (p_tax,
6850                                p_tax_regime_code,
6851                                p_tax_status_code);
6852 
6853            FETCH c_tax_status
6854            INTO  p_tax_status_code_desc;
6855            CLOSE c_tax_status;
6856 
6857         END IF;
6858 
6859   EXCEPTION
6860         WHEN OTHERS THEN
6861              IF (SQLCODE <> -20001) THEN
6862                 fnd_message.set_name ('SQLAP', 'AP_DEBUG');
6863                 fnd_message.set_token('ERROR', SQLERRM);
6864                 fnd_message.set_token('CALLING_SEQUENCE', l_curr_calling_sequence);
6865                 fnd_message.set_token('PARAMETERS',
6866                                       ' p_taxation_country      = '||p_taxation_country      ||
6867                                       ' p_tax_regime_code       = '||p_tax_regime_code       ||
6868                                       ' p_tax                   = '||p_tax                   ||
6869                                       ' p_tax_jurisdiction_code = '||p_tax_jurisdiction_code ||
6870                                       ' p_tax_status_code       = '||p_tax_status_code );
6871 
6872                 fnd_message.set_token('DEBUG_INFO',l_debug_info);
6873              END IF;
6874              app_exception.raise_exception;
6875 
6876   END get_tax_line_attr_desc;
6877 
6878 /*=============================================================================
6879  |  PROCEDURE - get_default_tax_det_attribs()
6880  |
6881  |  DESCRIPTION
6882  |    This procedure will return the default tax drivers based on the
6883  |    organization identifier.
6884  |
6885  |  PARAMETERS
6886  |    P_Org_Id          - Organization Id
6887  |    P_Legal_Entity_Id - Legal Entity Id
6888  |    P_Item_Id         - Inventory Item Id
6889  |    P_Doc_Type        - Document Type
6890  |    P_Trx_Date        - Transaction Date
6891  |
6892  |  MODIFICATION HISTORY
6893  |    DATE          Author  Action
6894  |    13-AUG-2004   Sanjay  Created
6895  *============================================================================*/
6896   PROCEDURE get_default_tax_det_attribs
6897                         (p_org_id               IN NUMBER,
6898                          p_legal_entity_id      IN NUMBER,
6899                          p_item_id              IN NUMBER,
6900                          p_doc_type             IN VARCHAR2,
6901                          p_trx_date             IN DATE,
6902                          x_return_status        OUT NOCOPY VARCHAR2,
6903                          x_msg_count            OUT NOCOPY NUMBER,
6904                          x_msg_data             OUT NOCOPY VARCHAR2,
6905                          p_country_code         OUT NOCOPY VARCHAR2,
6906                          p_trx_biz_category     OUT NOCOPY VARCHAR2,
6907                          p_intended_use         OUT NOCOPY VARCHAR2,
6908                          p_prod_category        OUT NOCOPY VARCHAR2,
6909                          p_prod_fisc_class_code OUT NOCOPY VARCHAR2,
6910                          p_calling_sequence     IN  VARCHAR2) IS
6911 
6912         l_tax_effective_date    DATE;
6913         l_success               BOOLEAN;
6914         l_error_code            VARCHAR2(30);
6915         l_event_class_code      VARCHAR2(100);
6916 	l_product_type		ap_invoice_lines_all.product_type%type;
6917 
6918         l_debug_info            VARCHAR2(240);
6919         l_curr_calling_sequence VARCHAR2(4000);
6920 
6921   BEGIN
6922 
6923         l_curr_calling_sequence := 'AP_ETAX_UTILITY_PKG.Get_Default_Tax_Det_Attribs<-' ||
6924                                     p_calling_sequence;
6925 
6926         ------------------------------------------------
6927         l_debug_info := 'Step 1: Get Taxation Country';
6928         ------------------------------------------------
6929         xle_utilities_grp.get_fp_countrycode_ou (
6930                                 p_api_version       => 1.0,
6931                                 p_init_msg_list     => FND_API.G_FALSE,
6932                                 p_commit            => FND_API.G_FALSE,
6933                                 x_return_status     => x_return_status,
6934                                 x_msg_count         => x_msg_count,
6935                                 x_msg_data          => x_msg_data,
6936                                 p_operating_unit    => p_org_id,
6937                                 x_country_code      => p_country_code);
6938 
6939         IF p_country_code IS NOT NULL THEN
6940 
6941         -------------------------------------------
6942         l_debug_info := 'Step 2: Get Event Class';
6943         -------------------------------------------
6944            l_success := AP_ETAX_UTILITY_PKG.Get_Event_Class_Code (
6945                                 p_invoice_type_lookup_code  => p_doc_type,
6946                                 p_event_class_code          => l_event_class_code,
6947                                 p_error_code                => l_error_code,
6948                                 p_calling_sequence          => l_curr_calling_sequence);
6949 
6950            IF (l_success) THEN
6951 
6952                 ----------------------------------------------------
6953                 l_debug_info := 'Step 3: Set Tax Security Context';
6954                 ----------------------------------------------------
6955                 AP_ETAX_UTILITY_PKG.set_tax_security_context (
6956                                 p_org_id                => p_org_id,
6957                                 p_legal_entity_id       => p_legal_entity_id,
6958                                 p_transaction_date      => NVL(p_trx_date, sysdate),
6959                                 p_related_doc_date      => NULL,
6960                                 p_adjusted_doc_date     => NULL,
6961                                 p_effective_date        => l_tax_effective_date,
6962                                 p_return_status         => x_return_status,
6963                                 p_msg_count             => x_msg_count,
6964                                 p_msg_data              => x_msg_data);
6965 
6966                 ------------------------------------------------------------------
6967                 l_debug_info := 'Step 4: Get Default Tax Determining Attributes';
6968                 ------------------------------------------------------------------
6969                 zx_api_pub.get_default_tax_det_attribs (
6970                                 p_api_version           => 1.0,
6971                                 p_init_msg_list         => FND_API.G_FALSE,
6972                                 p_commit                => FND_API.G_FALSE,
6973                                 p_validation_level      => FND_API.G_VALID_LEVEL_FULL,
6974                                 x_return_status         => x_return_status,
6975                                 x_msg_count             => x_msg_count,
6976                                 x_msg_data              => x_msg_data,
6977                                 p_application_id        => 200,
6978                                 p_entity_code           => 'AP_INVOICES',
6979                                 p_event_class_code      => l_event_class_code,
6980                                 p_org_id                => p_org_id,
6981                                 p_item_id               => p_item_id,
6982                                 p_country_code          => p_country_code,
6983                                 p_effective_date        => l_tax_effective_date,
6984                                 x_trx_biz_category      => p_trx_biz_category,
6985                                 x_intended_use          => p_intended_use,
6986                                 x_prod_category         => p_prod_category,
6987                                 x_prod_fisc_class_code  => p_prod_fisc_class_code,
6988 				x_product_type		=> l_product_type);
6989 
6990            END IF;
6991 
6992         END IF;
6993 
6994   EXCEPTION
6995         WHEN OTHERS THEN
6996              IF (SQLCODE <> -20001) THEN
6997                 fnd_message.set_name ('SQLAP', 'AP_DEBUG');
6998                 fnd_message.set_token('ERROR', SQLERRM);
6999                 fnd_message.set_token('CALLING_SEQUENCE', l_curr_calling_sequence);
7000                 fnd_message.set_token('PARAMETERS',
7001                                       ' p_org_id                = '||p_org_id           ||
7002                                       ' p_legal_entity_id       = '||p_legal_entity_id  ||
7003                                       ' p_item_id               = '||p_item_id          ||
7004                                       ' p_doc_type              = '||p_doc_type         ||
7005                                       ' p_trx_date              = '||p_trx_date );
7006 
7007                 fnd_message.set_token('DEBUG_INFO',l_debug_info);
7008              END IF;
7009              app_exception.raise_exception;
7010 
7011   END get_default_tax_det_attribs;
7012 
7013 
7014 PROCEDURE insert_tax_distributions(
7015 				  p_invoice_header_rec       IN ap_invoices_all%ROWTYPE,
7016 				  p_inv_dist_rec             IN r_ins_tax_dist_info,
7017 				  p_dist_code_combination_id IN NUMBER,
7018 				  p_user_id	             IN NUMBER,
7019 				  p_sysdate	             IN DATE,
7020 				  p_login_id	             IN	NUMBER,
7021 				  p_calling_sequence         IN VARCHAR2) IS
7022 
7023  l_curr_calling_sequence VARCHAR2(2000);
7024  l_debug_info		 VARCHAR2(2000);
7025  l_api_name		 VARCHAR2(80);
7026 
7027   -- Bug 7126676
7028  l_awt_success   Varchar2(1000);
7029  l_invoice_distribution_id ap_invoice_distributions_all.invoice_distribution_id%TYPE;
7030 BEGIN
7031 
7032      l_api_name := 'Insert_Tax_Distributions';
7033 
7034      l_curr_calling_sequence := 'Insert_Tax_Distributions <- '||p_calling_sequence;
7035 
7036      IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
7037        FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_ETAX_UTILITY_PKG.Insert_Tax_Distributions(+)');
7038      END IF;
7039 
7040      l_debug_info := 'Step 11: Insert new distributions including variances';
7041 
7042      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7043         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7044      END IF;
7045 
7046      -- bug 7126676
7047      select ap_invoice_distributions_s.NEXTVAL into l_invoice_distribution_id from dual;
7048 
7049      INSERT INTO ap_invoice_distributions_all (
7050             accounting_date,
7051             accrual_posted_flag,
7052             assets_addition_flag,
7053             assets_tracking_flag,
7054             cash_posted_flag,
7055             distribution_line_number,
7056             dist_code_combination_id,
7057             invoice_id,
7058             last_updated_by,
7059             last_update_date,
7060             line_type_lookup_code,
7061             period_name,
7062             set_of_books_id,
7063             amount,
7064             base_amount,
7065             batch_id,
7066             created_by,
7067             creation_date,
7068             description,
7069             final_match_flag,
7070             income_tax_region,
7071             last_update_login,
7072             match_status_flag,
7073             posted_flag,
7074             po_distribution_id,
7075             program_application_id,
7076             program_id,
7077             program_update_date,
7078             quantity_invoiced,
7079             request_id,
7080             reversal_flag,
7081             type_1099,
7082             unit_price,
7083             encumbered_flag,
7084             stat_amount,
7085             attribute1,
7086             attribute10,
7087             attribute11,
7088             attribute12,
7089             attribute13,
7090             attribute14,
7091             attribute15,
7092             attribute2,
7093             attribute3,
7094             attribute4,
7095             attribute5,
7096             attribute6,
7097             attribute7,
7098             attribute8,
7099             attribute9,
7100             attribute_category,
7101             expenditure_item_date,
7102             expenditure_organization_id,
7103             expenditure_type,
7104             parent_invoice_id,
7105             pa_addition_flag,
7106             pa_quantity,
7107             prepay_amount_remaining,
7108             project_accounting_context,
7109             project_id,
7110             task_id,
7111             packet_id,
7112             awt_flag,
7113             awt_group_id,
7114             awt_tax_rate_id,
7115             awt_gross_amount,
7116             awt_invoice_id,
7117             awt_origin_group_id,
7118             reference_1,
7119             reference_2,
7120             org_id,
7121             awt_invoice_payment_id,
7122             global_attribute_category,
7123             global_attribute1,
7124             global_attribute2,
7125             global_attribute3,
7126             global_attribute4,
7127             global_attribute5,
7128             global_attribute6,
7129             global_attribute7,
7130             global_attribute8,
7131             global_attribute9,
7132             global_attribute10,
7133             global_attribute11,
7134             global_attribute12,
7135             global_attribute13,
7136             global_attribute14,
7137             global_attribute15,
7138             global_attribute16,
7139             global_attribute17,
7140             global_attribute18,
7141             global_attribute19,
7142             global_attribute20,
7143             receipt_verified_flag,
7144             receipt_required_flag,
7145             receipt_missing_flag,
7146             justification,
7147             expense_group,
7148             start_expense_date,
7149             end_expense_date,
7150             receipt_currency_code,
7151             receipt_conversion_rate,
7152             receipt_currency_amount,
7153             daily_amount,
7154             web_parameter_id,
7155             adjustment_reason,
7156             award_id,
7157             credit_card_trx_id,
7158             dist_match_type,
7159             rcv_transaction_id,
7160             invoice_distribution_id,
7161             parent_reversal_id,
7162             tax_recoverable_flag,
7163             merchant_document_number,
7164             merchant_name,
7165             merchant_reference,
7166             merchant_tax_reg_number,
7167             merchant_taxpayer_id,
7168             country_of_supply,
7169             matched_uom_lookup_code,
7170             gms_burdenable_raw_cost,
7171             accounting_event_id,
7172             prepay_distribution_id,
7173             upgrade_posted_amt,
7174             upgrade_base_posted_amt,
7175             inventory_transfer_status,
7176             company_prepaid_invoice_id,
7177             cc_reversal_flag,
7178             awt_withheld_amt,
7179             pa_cmt_xface_flag,
7180             cancellation_flag,
7181             invoice_line_number,
7182             corrected_invoice_dist_id,
7183             rounding_amt,
7184             charge_applicable_to_dist_id,
7185             corrected_quantity,
7186             related_id,
7187             asset_book_type_code,
7188             asset_category_id,
7189             distribution_class,
7190             tax_code_id,
7191             intended_use,
7192             detail_tax_dist_id,
7193             rec_nrec_rate,
7194             recovery_rate_id,
7195             recovery_rate_name,
7196             recovery_type_code,
7197             withholding_tax_code_id,
7198             taxable_amount,
7199             taxable_base_amount,
7200             tax_already_distributed_flag,
7201             summary_tax_line_id,
7202             extra_po_erv,
7203             prepay_tax_diff_amount,
7204 	    --Freight and Special Charges
7205 	    rcv_charge_addition_flag )
7206       VALUES (
7207             p_inv_dist_rec.accounting_date,    -- accounting_date
7208             'N',                                  -- accrual_posted_flag
7209             'U',                                  -- assets_addition_flag
7210             'N',                                  -- assets_tracking_flag
7211             'N',                                  -- cash_posted_flag
7212             AP_INVOICE_LINES_PKG.get_max_dist_line_num(
7213               p_inv_dist_rec.invoice_id,
7214               p_inv_dist_rec.invoice_line_number)+1,
7215                                                   -- distribution_line_number
7216             P_dist_code_combination_id,           -- dist_code_combination_id
7217             p_inv_dist_rec.invoice_id,      	  -- invoice_id
7218             P_user_id,                            -- last_updated_by
7219             P_sysdate,                            -- last_update_date
7220             p_inv_dist_rec.line_type_lookup_code,
7221                                                   -- line_type_lookup_code
7222             p_inv_dist_rec.period_name,           -- period_name
7223             p_inv_dist_rec.set_of_books_id,	  -- set_of_books_id
7224             p_inv_dist_rec.amount,                -- amount
7225             decode(p_inv_dist_rec.base_amount,
7226                    0, decode(p_inv_dist_rec.amount, 0, p_inv_dist_rec.base_amount, NULL),
7227                    p_inv_dist_rec.base_amount),   -- base_amount
7228             p_inv_dist_rec.batch_id,        	  -- batch_id
7229             P_user_id,                            -- created_by
7230             P_sysdate,                            -- creation_date
7231             p_inv_dist_rec.description,        	  -- description
7232             NULL,                                 -- final_match_flag
7233             p_inv_dist_rec.income_tax_region,  -- income_tax_region
7234             P_login_id,                           -- last_update_login
7235             NULL,                                 -- match_status_flag
7236             'N',                                  -- posted_flag
7237             p_inv_dist_rec.po_distribution_id, -- po_distribution_id
7238             NULL,                                 -- program_application_id
7239             NULL,                                 -- program_id
7240             NULL,                                 -- program_update_date
7241             NULL,                                 -- quantity_invoiced
7242             NULL,                                 -- request_id
7243             'N',                                  -- reversal_flag
7244             p_inv_dist_rec.type_1099,          -- type_1099
7245             NULL,                                 -- unit_price
7246             'N',                                  -- encumbered_flag
7247             NULL,                                 -- stat_amount
7248             p_inv_dist_rec.attribute1,         -- attribute1
7249             p_inv_dist_rec.attribute10,        -- attribute10
7250             p_inv_dist_rec.attribute11,        -- attribute11,
7251             p_inv_dist_rec.attribute12,        -- attribute12
7252             p_inv_dist_rec.attribute13,        -- attribute13
7253             p_inv_dist_rec.attribute14,        -- attribute14
7254             p_inv_dist_rec.attribute15,        -- attribute15
7255             p_inv_dist_rec.attribute2,         -- attribute2
7256             p_inv_dist_rec.attribute3,         -- attribute3
7257             p_inv_dist_rec.attribute4,         -- attribute4
7258             p_inv_dist_rec.attribute5,         -- attribute5
7259             p_inv_dist_rec.attribute6,         -- attribute6
7260             p_inv_dist_rec.attribute7,         -- attribute7
7261             p_inv_dist_rec.attribute8,         -- attribute8
7262             p_inv_dist_rec.attribute9,         -- attribute9
7263             p_inv_dist_rec.attribute_category, -- attribute_category
7264             p_inv_dist_rec.expenditure_item_date,
7265                                                   -- expenditure_item_date
7266             p_inv_dist_rec.expenditure_organization_id,
7267                                                   -- expenditure_organization_id
7268             p_inv_dist_rec.expenditure_type,   -- expenditure_type
7269             p_inv_dist_rec.parent_invoice_id,  -- parent_invoice_id
7270             p_inv_dist_rec.pa_addition_flag,   -- pa_addition_flag
7271             p_inv_dist_rec.pa_quantity,        -- pa_quantity
7272             NULL,                                 -- prepay_amount_remaining
7273             -- the prepay_amount_remaining will be populated for all the
7274             -- prepayment distributions during the payment. And later will be
7275             -- updated during the prepayment applications
7276             p_inv_dist_rec.project_accounting_context,
7277                                                   -- project_accounting_context
7278             p_inv_dist_rec.project_id,         -- project_id
7279             p_inv_dist_rec.task_id,            -- task_id
7280             NULL,                                 -- packet_id
7281             'N',                                  -- awt_flag
7282             p_inv_dist_rec.awt_group_id,       -- awt_group_id
7283             NULL,                                 -- awt_tax_rate_id
7284             NULL,                                 -- awt_gross_amount
7285             NULL,                                 -- awt_invoice_id
7286             NULL,                                 -- awt_origin_group_id
7287             NULL,                                 -- reference_1
7288             NULL,                                 -- reference_2
7289             p_inv_dist_rec.org_id,		  -- org_id
7290             NULL,                                 -- awt_invoice_payment_id
7291             p_inv_dist_rec.global_attribute_category,
7292                                                   -- global_attribute_category
7293             p_inv_dist_rec.global_attribute1,  -- global_attribute1
7294             p_inv_dist_rec.global_attribute2,  -- global_attribute2
7295             p_inv_dist_rec.global_attribute3,  -- global_attribute3
7296             p_inv_dist_rec.global_attribute4,  -- global_attribute4
7297             p_inv_dist_rec.global_attribute5,  -- global_attribute5
7298             p_inv_dist_rec.global_attribute6,  -- global_attribute6
7299             p_inv_dist_rec.global_attribute7,  -- global_attribute7
7300             p_inv_dist_rec.global_attribute8,  -- global_attribute8
7301             p_inv_dist_rec.global_attribute9,  -- global_attribute9
7302             p_inv_dist_rec.global_attribute10, -- global_attribute10
7303             p_inv_dist_rec.global_attribute11, -- global_attribute11
7304             p_inv_dist_rec.global_attribute12, -- global_attribute12
7305             p_inv_dist_rec.global_attribute13, -- global_attribute13
7306             p_inv_dist_rec.global_attribute14, -- global_attribute14
7307             p_inv_dist_rec.global_attribute15, -- global_attribute15
7308             p_inv_dist_rec.global_attribute16, -- global_attribute16
7309             p_inv_dist_rec.global_attribute17, -- global_attribute17
7310             p_inv_dist_rec.global_attribute18, -- global_attribute18
7311             p_inv_dist_rec.global_attribute19, -- global_attribute19
7312             p_inv_dist_rec.global_attribute20, -- global_attribute20
7313             NULL,                                 -- receipt_verified_flag
7314             NULL,                                 -- receipt_required_flag
7315             NULL,                                 -- receipt_missing_flag
7316             NULL,                                 -- justification
7317             NULL,                                 -- expense_group
7318             NULL,                                 -- start_expense_date
7319             NULL,                                 -- end_expense_date
7320             NULL,                                 -- receipt_currency_code
7321             NULL,                                 -- receipt_conversion_rate
7322             NULL,                                 -- receipt_currency_amount
7323             NULL,                                 -- daily_amount
7324             NULL,                                 -- web_parameter_id
7325             NULL,                                 -- adjustment_reason
7326             p_inv_dist_rec.award_id,           -- award_id
7327             NULL,                                 -- credit_card_trx_id
7328             p_inv_dist_rec.dist_match_type,    -- dist_match_type
7329             p_inv_dist_rec.rcv_transaction_id, -- rcv_transaction_id
7330             l_invoice_distribution_id,         -- invoice_distribution_id
7331             NULL,                                 -- parent_reversal_id
7332             p_inv_dist_rec.tax_recoverable_flag,
7333                                                   -- tax_recoverable_flag
7334             NULL,                                 -- merchant_document_number
7335             NULL,                                 -- merchant_name
7336             NULL,                                 -- merchant_reference
7337             NULL,                                 -- merchant_tax_reg_number
7338             NULL,                                 -- merchant_taxpayer_id
7339             NULL,                                 -- country_of_supply
7340             NULL,                                 -- matched_uom_lookup_code
7341             NULL,                                 -- gms_burdenable_raw_cost
7342             NULL,                                 -- accounting_event_id
7343             p_inv_dist_rec.prepay_distribution_id,  -- prepay_distribution_id
7344             NULL,                                 -- upgrade_posted_amt
7345             NULL,                                 -- upgrade_base_posted_amt
7346             'N',                                  -- inventory_transfer_status
7347             NULL,                                 -- company_prepaid_invoice_id
7348             NULL,                                 -- cc_reversal_flag
7349             NULL,                                 -- awt_withheld_amt
7350             NULL,                                 -- pa_cmt_xface_flag
7351             p_inv_dist_rec.cancellation_flag,  -- cancellation_flag
7352             p_inv_dist_rec.invoice_line_number,-- invoice_line_number
7353             p_inv_dist_rec.corrected_invoice_dist_id,
7354                                                   -- corrected_invoice_dist_id
7355             p_inv_dist_rec.rounding_amt,       -- rounding_amt
7356             p_inv_dist_rec.charge_applicable_to_dist_id,
7357                                                  -- charge_applicable_to_dist_id
7358             NULL,                                 -- corrected_quantity
7359             NULL,                                 -- related_id
7360             NULL,                                 -- asset_book_type_code
7361             NULL,                                 -- asset_category_id
7362             p_inv_dist_rec.distribution_class, -- distribution_class
7363             p_inv_dist_rec.tax_code_id,        -- tax_code_id
7364             NULL,                                 -- intended_use,
7365             p_inv_dist_rec.detail_tax_dist_id, -- detail_tax_dist_id
7366             p_inv_dist_rec.rec_nrec_rate,      -- rec_nrec_rate
7367             p_inv_dist_rec.recovery_rate_id,   -- recovery_rate_id
7368             p_inv_dist_rec.recovery_rate_name, -- recovery_rate_name
7369             p_inv_dist_rec.recovery_type_code, -- recovery_type_code
7370             NULL,                                 -- withholding_tax_code_id,
7371             p_inv_dist_rec.taxable_amount,     -- taxable_amount
7372             p_inv_dist_rec.taxable_base_amount, -- taxable_base_amount
7373             NULL,                                -- tax_already_distributed_flag
7374             p_inv_dist_rec.summary_tax_line_id, -- summary_tax_line_id
7375             p_inv_dist_rec.extra_po_erv,        -- extra_po_erv
7376             p_inv_dist_rec.prepay_tax_diff_amount, -- prepay_tax_diff_amount
7377 	    'N'					      -- rcv_charge_addition_flag
7378      );
7379 
7380   IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
7381       FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_ETAX_UTILITY_PKG.Insert_Tax_Distributions(-)');
7382   END IF;
7383 
7384   -- Following code was introduce because of bug 7126676
7385   -- This code will generate withholding applicability for
7386   -- tax lines for JL Exteneded withholdings
7387   IF (AP_EXTENDED_WITHHOLDING_PKG.AP_EXTENDED_WITHHOLDING_ACTIVE) THEN
7388 
7389        l_debug_info := 'Call the Ap_Ext_Withholding_Default from match';
7390        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7391            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7392        END IF;
7393 
7394        Ap_Extended_Withholding_Pkg.Ap_Ext_Withholding_Default
7395                            (p_invoice_id       => p_inv_dist_rec.invoice_id,
7396                             p_inv_line_num     => p_inv_dist_rec.invoice_line_number,
7397                             p_inv_dist_id      => l_invoice_distribution_id,
7398                             p_calling_module   => l_curr_calling_sequence,
7399                             p_parent_dist_id   => NULL,
7400                             p_awt_success      => l_awt_success);
7401 
7402        IF (l_awt_success <> 'SUCCESS') THEN
7403            RAISE Global_Exception;
7404        END IF;
7405 
7406   END IF;
7407 
7408   EXCEPTION
7409     WHEN OTHERS THEN
7410       IF (SQLCODE <> -20001) THEN
7411          FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
7412          FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
7413          FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
7414          FND_MESSAGE.SET_TOKEN('PARAMETERS',
7415            ' P_Invoice_Id = '||P_Invoice_Header_Rec.invoice_id||
7416            ' P_Calling_Sequence = '||P_Calling_Sequence);
7417          FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
7418        END IF;
7419 
7420         IF ( G_LEVEL_EXCEPTION >= G_CURRENT_RUNTIME_LEVEL) THEN
7421            FND_LOG.STRING(G_LEVEL_EXCEPTION,G_MODULE_NAME||l_api_name,SQLERRM);
7422          END IF;
7423 
7424       APP_EXCEPTION.RAISE_EXCEPTION;
7425 END;
7426 
7427 
7428  -- Bug 4887847: Added function Get_Line_Class to populate a new attribute that
7429  --              is required for tax calculation and reporting purposes.
7430 
7431 /*=============================================================================
7432  |  FUNCTION - Get_Line_Class()
7433  |
7434  |  DESCRIPTION
7435  |   This function will return the line class based on the invoice document type
7436  |    invoice line type and matching information.
7437  |
7438  *============================================================================*/
7439 
7440  FUNCTION Get_Line_Class(
7441          P_Invoice_Type_Lookup_Code    IN  VARCHAR2,
7442          P_Inv_Line_Type               IN  VARCHAR2,
7443          P_Line_Location_Id            IN  NUMBER,
7444          P_Line_Class                  OUT NOCOPY VARCHAR2,
7445          P_Error_Code                  OUT NOCOPY VARCHAR2,
7446          P_Calling_Sequence            IN  VARCHAR2) RETURN BOOLEAN
7447 IS
7448   l_matching_basis    po_line_locations_ap_v.matching_basis%type;
7449   l_payment_type      po_line_locations_ap_v.payment_type%type;
7450   l_shipment_type     po_line_locations_ap_v.shipment_type%type;
7451 
7452   l_debug_info                 VARCHAR2(240);
7453   l_curr_calling_sequence      VARCHAR2(4000);
7454 
7455 BEGIN
7456 
7457   l_curr_calling_sequence := 'AP_ETAX_UTILITY_PKG.Get_Line_Class_Code<-' ||
7458                               P_calling_sequence;
7459 
7460   IF (P_Invoice_Type_Lookup_Code IN ('STANDARD',
7461                                      'MIXED',
7462 			             'ADJUSTMENT',
7463 			             'PO PRICE ADJUST',
7464 			             'INVOICE REQUEST')) THEN
7465 
7466       P_Line_Class := 'STANDARD INVOICES';
7467 
7468    ELSIF (P_Invoice_Type_Lookup_Code IN ('CREDIT', 'CREDIT MEMO REQUEST')) THEN
7469 
7470       P_Line_Class := 'AP_CREDIT_MEMO';
7471 
7472    ELSIF (P_Invoice_Type_Lookup_Code = 'DEBIT') THEN
7473 
7474       P_Line_Class := 'AP_DEBIT_MEMO';
7475 
7476    ELSIF (P_Invoice_Type_Lookup_Code IN ('PREPAYMENT')) THEN
7477 
7478       P_Line_Class := 'PREPAYMENT INVOICES';
7479 
7480    ELSIF (P_Invoice_Type_Lookup_Code IN ('EXPENSE REPORT')) THEN
7481 
7482       P_Line_Class := 'EXPENSE REPORTS';
7483 
7484    END IF;
7485 
7486    IF P_Inv_Line_Type  = 'PREPAY' THEN
7487 
7488       P_Line_Class := 'PREPAY_APPLICATION';
7489 
7490    END IF;
7491 
7492    IF P_Line_Location_Id IS NOT NULL THEN
7493 
7494       SELECT matching_basis, payment_type, shipment_type
7495         INTO l_matching_basis, l_payment_type, l_shipment_type
7496         FROM po_line_locations_all
7497        WHERE line_location_id = P_Line_Location_Id;
7498 
7499       IF l_matching_basis = 'AMOUNT' THEN
7500 
7501          P_Line_Class := 'AMOUNT_MATCHED';
7502 
7503       END IF;
7504 
7505       IF l_shipment_type = 'PREPAYMENT' THEN
7506 
7507          IF l_payment_type = 'ADVANCE' THEN
7508 
7509              P_Line_Class := 'ADVANCE';
7510 
7511          ELSIF l_payment_type IN ('MILESTONE', 'RATE', 'LUMPSUM') THEN
7512 
7513              P_Line_Class := 'FINANCING';
7514 
7515          END IF;
7516       END IF;
7517 
7518    END IF;
7519 
7520    RETURN (TRUE);
7521 
7522  EXCEPTION
7523    WHEN OTHERS THEN
7524     IF (SQLCODE <> -20001) THEN
7525        FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
7526        FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
7527        FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
7528        FND_MESSAGE.SET_TOKEN('PARAMETERS',
7529             ' P_Invoice_Type_Lookup_Code = '||P_Invoice_Type_Lookup_Code||
7530             ' P_Error_Code = '||P_Error_Code||
7531             ' P_Calling_Sequence = '||P_Calling_Sequence);
7532       FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
7533    END IF;
7534 
7535    APP_EXCEPTION.RAISE_EXCEPTION;
7536 
7537 END Get_Line_Class;
7538 
7539   FUNCTION Is_Tax_Already_Dist_Inv_Char(
7540              P_Invoice_Id                IN NUMBER,
7541              P_Calling_Sequence          IN VARCHAR2) RETURN VARCHAR2
7542   IS
7543 
7544     l_debug_info                        VARCHAR2(240);
7545     l_curr_calling_sequence             VARCHAR2(4000);
7546     l_tax_already_distributed_flag       VARCHAR2(1) := 'N';
7547 
7548     -- Modified this select to include the TAX only case
7549     CURSOR etax_already_distributed IS
7550     SELECT 'Y'
7551       FROM ap_invoice_distributions_all
7552      WHERE invoice_id = p_invoice_id
7553        AND line_type_lookup_code <> 'AWT'
7554        AND (tax_already_distributed_flag = 'Y'
7555             OR detail_tax_dist_id IS NOT NULL)
7556        AND (related_id IS NULL
7557             OR related_id = invoice_distribution_id)
7558        AND ROWNUM = 1;
7559 
7560   BEGIN
7561 
7562     l_curr_calling_sequence := 'AP_ETAX_UTILITY_PKG.Is_Tax_Already_Dist_Inv<-'||
7563                                P_calling_sequence;
7564 
7565     -------------------------------------------------------------------
7566     l_debug_info := 'Step 1: Get tax_already_Distributed_flag for any '||
7567                     'taxable line in the invoice';
7568     -------------------------------------------------------------------
7569     OPEN etax_already_distributed;
7570     FETCH etax_already_distributed INTO l_tax_already_distributed_flag;
7571     IF (etax_already_distributed%NOTFOUND) THEN
7572       CLOSE etax_already_distributed;
7573       l_tax_already_distributed_flag := 'N';
7574 
7575     END IF;
7576 
7577     IF (etax_already_distributed%ISOPEN ) THEN
7578       CLOSE etax_already_distributed;
7579     END IF;
7580 
7581     RETURN l_tax_already_distributed_flag;
7582 
7583   EXCEPTION
7584     WHEN OTHERS THEN
7585       IF (SQLCODE <> -20001) THEN
7586         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
7587         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
7588         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
7589         FND_MESSAGE.SET_TOKEN('PARAMETERS',
7590           ' P_Invoice_Id = '||P_Invoice_Id||
7591           ' P_Calling_Sequence = '||P_Calling_Sequence);
7592         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
7593       END IF;
7594 
7595       IF (etax_already_distributed%ISOPEN ) THEN
7596         CLOSE etax_already_distributed;
7597       END IF;
7598 
7599       APP_EXCEPTION.RAISE_EXCEPTION;
7600 
7601   END Is_Tax_Already_Dist_Inv_Char;
7602 
7603 FUNCTION Get_Converted_Price (x_invoice_distribution_id IN NUMBER)
7604                 RETURN NUMBER
7605 IS
7606         CURSOR c_rct_info (c_inv_dist_id NUMBER) IS
7607          SELECT  D.unit_price                 unit_price,
7608                  pll.matching_basis           match_basis,
7609                  pll.match_option             match_option,
7610                  pl.unit_meas_lookup_code     po_uom,
7611                  D.matched_uom_lookup_code    rcv_uom,
7612                  rsl.item_id                  rcv_item_id
7613           FROM   ap_invoice_distributions_all D,
7614                  po_distributions_all         PD,
7615                  po_lines_all                 PL,
7616                  po_line_locations_all        PLL,
7617                  rcv_transactions             RTXN,
7618                  rcv_shipment_lines           RSL
7619           WHERE  D.invoice_distribution_id = c_inv_dist_id
7620             AND  D.po_distribution_id      = PD.po_distribution_id
7621             AND  PL.po_header_id           = PD.po_header_id
7622             AND  PL.po_line_id             = PD.po_line_id
7623             AND  PD.line_location_id       = PLL.line_location_id
7624             AND  D.rcv_transaction_id      = RTXN.transaction_id
7625             AND  RTXN.shipment_line_id     = RSL.shipment_line_id;
7626 
7627         l_match_basis   po_line_types.matching_basis%TYPE;
7628         l_match_option  po_line_locations.match_option%TYPE;
7629         l_po_uom        po_line_locations.unit_meas_lookup_code%TYPE;
7630         l_rct_uom       po_line_locations.unit_meas_lookup_code%TYPE;
7631         l_rct_item_id   rcv_shipment_lines.item_id%TYPE;
7632 
7633         l_uom_conv_rate NUMBER;
7634         l_inv_price     NUMBER;
7635 Begin
7636 
7637      OPEN  c_rct_info (x_invoice_distribution_id);
7638      FETCH c_rct_info
7639      INTO  l_inv_price, l_match_basis, l_match_option, l_po_uom, l_rct_uom, l_rct_item_id;
7640      CLOSE c_rct_info;
7641 
7642      IF l_match_basis  = 'QUANTITY'  and
7643         l_match_option = 'R'         and
7644         l_po_uom       <> l_rct_uom THEN
7645 
7646         l_uom_conv_rate := po_uom_s.po_uom_convert (
7647                              l_rct_uom,
7648                              l_po_uom,
7649                              l_rct_item_id);
7650 
7651         l_inv_price := l_inv_price / l_uom_conv_rate;
7652 
7653      END IF;
7654 
7655      RETURN l_inv_price;
7656 
7657 EXCEPTION
7658      WHEN OTHERS THEN
7659           NULL;
7660 END Get_Converted_Price;
7661 
7662   FUNCTION Get_Max_Dist_Num_Self(
7663               X_invoice_id          IN      NUMBER,
7664               X_line_number         IN      NUMBER) RETURN NUMBER
7665   IS
7666     l_max_dist_line_num NUMBER := 0;
7667   BEGIN
7668 
7669     SELECT nvl(max(distribution_line_number),0)
7670       INTO l_max_dist_line_num
7671       FROM ap_self_assessed_tax_dist_all
7672      WHERE invoice_id = X_invoice_id
7673        AND invoice_line_number = X_line_number;
7674 
7675     RETURN(l_max_dist_line_num);
7676 
7677   END Get_Max_Dist_Num_Self;
7678 
7679 END AP_ETAX_UTILITY_PKG;