DBA Data[Home] [Help]

PACKAGE BODY: APPS.AP_ETAX_SERVICES_PKG

Source


1 PACKAGE BODY AP_ETAX_SERVICES_PKG AS
2 /* $Header: apetxsrb.pls 120.115.12010000.18 2008/12/16 07:15:34 hchaudha ship $ */
3 
4   G_PKG_NAME          CONSTANT VARCHAR2(30) := 'AP_ETAX_SERVICES_PKG';
5   G_MSG_UERROR        CONSTANT NUMBER       := FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR;
6   G_MSG_ERROR         CONSTANT NUMBER       := FND_MSG_PUB.G_MSG_LVL_ERROR;
7   G_MSG_SUCCESS       CONSTANT NUMBER       := FND_MSG_PUB.G_MSG_LVL_SUCCESS;
8   G_MSG_HIGH          CONSTANT NUMBER       := FND_MSG_PUB.G_MSG_LVL_DEBUG_HIGH;
9   G_MSG_MEDIUM        CONSTANT NUMBER       := FND_MSG_PUB.G_MSG_LVL_DEBUG_MEDIUM;
10   G_MSG_LOW           CONSTANT NUMBER       := FND_MSG_PUB.G_MSG_LVL_DEBUG_LOW;
11   G_LINES_PER_FETCH   CONSTANT NUMBER       := 1000;
12 
13   G_CURRENT_RUNTIME_LEVEL CONSTANT NUMBER   := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
14   G_LEVEL_UNEXPECTED      CONSTANT NUMBER   := FND_LOG.LEVEL_UNEXPECTED;
15   G_LEVEL_ERROR           CONSTANT NUMBER   := FND_LOG.LEVEL_ERROR;
16   G_LEVEL_EXCEPTION       CONSTANT NUMBER   := FND_LOG.LEVEL_EXCEPTION;
17   G_LEVEL_EVENT           CONSTANT NUMBER   := FND_LOG.LEVEL_EVENT;
18   G_LEVEL_PROCEDURE       CONSTANT NUMBER   := FND_LOG.LEVEL_PROCEDURE;
19   G_LEVEL_STATEMENT       CONSTANT NUMBER   := FND_LOG.LEVEL_STATEMENT;
20   G_MODULE_NAME           CONSTANT VARCHAR2(100) := 'AP.PLSQL.AP_ETAX_SERVICES_PKG.';
21 
22   -- Define the structures for lines and distributions
23   TYPE Inv_Lines_Tab_Type IS TABLE OF ap_invoice_lines_all%ROWTYPE;
24   TYPE Inv_Dists_Tab_Type IS TABLE OF ap_invoice_distributions_all%ROWTYPE;
25 
26   l_inv_line_list       Inv_Lines_Tab_Type;
27   l_inv_tax_list        Inv_Lines_Tab_Type;
28   l_inv_dist_list       Inv_Dists_Tab_Type;
29   l_tax_dist_list       Inv_Dists_Tab_Type;
30   p_rct_match_tax_list  Inv_Lines_Tab_Type;
31 
32   l_user_id		ap_invoices_all.created_by%TYPE := FND_GLOBAL.user_id;
33   l_login_id		ap_invoices_all.last_update_login%TYPE := FND_GLOBAL.login_id;
34   l_sysdate             DATE := sysdate;
35   g_manual_tax_lines	VARCHAR2(1) := 'N';
36   g_invoices_to_process NUMBER;
37 
38   l_payment_request_flag       varchar2(1); ---for bug 5967914
39   l_manual_tax_line_rcv_mtch   varchar2(1);
40   l_inv_header_rec2            ap_invoices_all%rowtype; -- For bug 6064593
41 
42   PROCEDURE Cache_Line_Defaults
43                         (p_org_id               IN ap_invoices_all.org_id%type,
44                          p_vendor_site_id       IN ap_supplier_sites_all.vendor_site_id%type,
45                          p_calling_sequence     IN VARCHAR2);
46 
47   FUNCTION CANCEL_INVOICE
48 			(p_invoice_id   IN NUMBER,
49 			 p_line_number  IN NUMBER DEFAULT NULL,
50 			 p_calling_mode IN VARCHAR2) RETURN BOOLEAN;
51 
52   FUNCTION TAX_DISTRIBUTIONS_EXIST
53 			(p_invoice_id  IN NUMBER) RETURN BOOLEAN;
54 
55   FUNCTION TAX_ONLY_LINE_EXIST
56                         (p_invoice_id  IN NUMBER) RETURN BOOLEAN;
57 
58   PROCEDURE get_converted_qty_price
59 			(x_invoice_distribution_id IN  NUMBER,
60                          x_inv_price	       	   OUT NOCOPY NUMBER,
61                          x_inv_qty		   OUT NOCOPY NUMBER);
62 
63   FUNCTION Populate_Rct_Match_Lines_GT
64 			(P_Invoice_Header_Rec      IN ap_invoices_all%ROWTYPE,
65 			 P_Event_Class_Code        IN VARCHAR2,
66 			 P_Error_Code              OUT NOCOPY VARCHAR2,
67 			 P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN;
68 
69   FUNCTION Update_Distributions
70 			(P_Invoice_header_rec    IN ap_invoices_all%ROWTYPE,
71              		 P_Calling_Mode          IN VARCHAR2,
72              		 P_All_Error_Messages    IN VARCHAR2,
73              		 P_Error_Code            OUT NOCOPY VARCHAR2,
74              		 P_Calling_Sequence      IN VARCHAR2) RETURN BOOLEAN;
75 
76   PROCEDURE Print(p_api_name   IN VARCHAR2,
77 	          p_debug_info IN VARCHAR2);
78 
79   FUNCTION Return_Other_Charge_Lines(
80              P_Invoice_Header_Rec        IN ap_invoices_all%ROWTYPE,
81              P_Error_Code                OUT NOCOPY VARCHAR2,
82              P_Calling_Sequence          IN VARCHAR2) RETURN BOOLEAN;
83 
84   -- Bug 6694536
85   FUNCTION SELF_ASSESS_TAX_DIST_EXIST
86                         (p_invoice_id  IN NUMBER) RETURN BOOLEAN;
87   FUNCTION Freeze_Distributions(
88              P_Invoice_Header_Rec      IN ap_invoices_all%ROWTYPE,
89              P_Calling_Mode            IN VARCHAR2,
90              P_Event_Class_Code        IN VARCHAR2,
91              P_All_Error_Messages      IN VARCHAR2,
92              P_Error_Code              OUT NOCOPY VARCHAR2,
93              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN;--Bug7592845
94 
95 
96 -- 7262269
97 /*=============================================================================
98  |  FUNCTION - get_po_ship_to_org_id()
99  |
100  |  DESCRIPTION
101  |      Procedure to return ship to organization id from po_line_locations
102  |       table.
103  |
104  |  PARAMETERS
105  |      p_po_line_location_id
106  |
107  |  RETURNS
108  |      p_po_ship_to_org_id
109  |
110  *============================================================================*/
111 FUNCTION get_po_ship_to_org_id (
112             p_po_line_location_id   IN NUMBER
113         ) RETURN NUMBER IS
114     l_po_ship_to_org_id NUMBER := NULL;
115 BEGIN
116 
117     IF (p_po_line_location_id = NULL) THEN
118         RETURN NULL;
119     END IF;
120 
121     SELECT SHIP_TO_ORGANIZATION_ID
122       INTO l_po_ship_to_org_id
123     FROM po_line_locations_all pll
124     WHERE pll.line_location_id = p_po_line_location_Id;
125 
126     return l_po_ship_to_org_id;
127 
128 EXCEPTION
129     WHEN OTHERS THEN
130         RETURN NULL;
131 END get_po_ship_to_org_id;
132 -- 7262269
133 
134 /*=============================================================================
135  |  FUNCTION - Calculate()
136  |
137  |  DESCRIPTION
138  |      Public function that will call the calculate_tax service for
139  |      calculation and recalculation.
140  |      This API assumes the calling code controls the commit cycle.
141  |      This function returns TRUE if the call to the service is successful.
142  |      Otherwise, FALSE.
143  |
144  |  PARAMETERS
145  |      P_Invoice_Id - invoice id
146  |      P_Line_Number - This parameter will be used to allow this API to
147  |                      calculate tax only for the line specified in this
148  |                      parameter.  Additionally, this parameter will be used
149  |                      to determine the PREPAY line created for prepayment
150  |                      unapplications.
151  |      P_Calling_Mode - calling mode.  Identifies which service to call
152  |      P_All_Error_Messages - Should API return 1 error message or allow
153  |                             calling point to get them from message stack
154  |      P_error_code - Error code to be returned
155  |      P_calling_sequence -  Calling sequence
156  |
157  |  MODIFICATION HISTORY
158  |    DATE          Author         Action
159  |    07-OCT-2003   SYIDNER        Created
160  |    29-DEC-2003   SYIDNER        Use of new function to validate if tax was
161  |                                 already calculated for the invoice.  Function
162  |                                 created in the ap_etax_utility_pkg.
163  |    28-JAN-2004   SYIDNER        Included handling for tax-only invoices and
164  |                                 Manual import from Invoice workbench
165  |
166  *============================================================================*/
167 
168   FUNCTION Calculate(
169              P_Invoice_Id              IN NUMBER,
170              P_Line_Number             IN NUMBER,
171              P_Calling_Mode            IN VARCHAR2,
172              P_All_Error_Messages      IN VARCHAR2,
173              P_Error_Code              OUT NOCOPY VARCHAR2,
174              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
175   IS
176 
177     l_debug_info                 VARCHAR2(240);
178     l_curr_calling_sequence      VARCHAR2(4000);
179 
180     l_inv_header_rec             ap_invoices_all%ROWTYPE;
181     l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
182     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
183 
184     l_tax_already_calculated     VARCHAR2(1);
185 
186     l_return_status_service       VARCHAR2(4000);
187     l_msg_count                   NUMBER;
188     l_msg_data                    VARCHAR2(4000);
189     l_msg                         VARCHAR2(4000);
190 
191     l_return_status               BOOLEAN := TRUE;
192     l_call_etax                   BOOLEAN :=FALSE; --Bug7136832
193     /*l_no_tax_lines              VARCHAR2(1) := 'N';*/ --Bug6521120
194     l_no_tax_lines                VARCHAR2(1) := 'Y';  --Bug6521120
195     l_inv_rcv_matched             VARCHAR2(1) := 'N';
196 
197     l_api_name			  CONSTANT VARCHAR2(100) := 'Calculate';
198 
199     CURSOR Invoice_Header (c_invoice_id NUMBER) IS
200     SELECT *
201       FROM ap_invoices_all
202      WHERE invoice_id = c_invoice_Id;
203 
204     CURSOR Invoice_Lines (c_invoice_id NUMBER) IS
205     SELECT *
206       FROM ap_invoice_lines_all
207      WHERE invoice_id = c_invoice_id
208        AND line_type_lookup_code NOT IN ('TAX', 'AWT');
209 
210     -- This cursor will be used in the case the API is call
211     -- to calculate tax for only 1 line
212     CURSOR Invoice_Line (c_invoice_id  NUMBER,
213 			 c_line_number NUMBER) IS
214     SELECT *
215       FROM ap_invoice_lines_all
216      WHERE invoice_id  = c_invoice_id
217        AND line_number = c_line_number
218        AND line_type_lookup_code NOT IN ('TAX', 'AWT');
219 
220     CURSOR Tax_Lines_to_import (c_invoice_id IN NUMBER) IS
221     SELECT *
222       FROM ap_invoice_lines_all
223      WHERE invoice_id            = c_invoice_id
224        AND line_type_lookup_code = 'TAX'
225        AND summary_tax_line_id   IS NULL;
226 
227     l_validation_request_id ap_invoices_all.validation_request_id%TYPE;
228 
229     CURSOR c_selected_invoices IS
230     SELECT trx_id, event_class_code
231       FROM zx_trx_headers_gt
232      WHERE application_id   =  ap_etax_pkg.ap_application_id
233        AND entity_code      =  ap_etax_pkg.ap_entity_code
234        AND event_class_code IN (ap_etax_pkg.ap_inv_event_class_code,
235                                 ap_etax_pkg.ap_pp_event_class_code,
236                                 ap_etax_pkg.ap_er_event_class_code);
237 
238     --6922266
239     l_count   NUMBER;
240 
241   BEGIN
242 
243     Print(l_api_name,'AP_ETAX_SERVICES_PKG.Calculate (+)');
244 
245     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Calculate<-' || P_calling_sequence;
246 
247     l_validation_request_id := ap_approval_pkg.g_validation_request_id;
248 
249     g_invoices_to_process := 0;
250 
251     IF l_validation_request_id IS NOT NULL THEN
252 
253        IF NOT AP_ETAX_SERVICES_PKG.Bulk_Populate_Headers_GT(
254 		p_validation_request_id => l_validation_request_id,
255 		p_calling_mode		=> p_calling_mode,
256 		p_error_code            => p_error_code) THEN
257 
258 	  l_return_status := FALSE;
259        END IF;
260 
261        -- Bug 6922266 Begin
262        -----------------------------------------------------------------
263        l_debug_info := 'Reset Tax Calculation Flag for Concurrent Mode';
264        Print(l_api_name, l_debug_info);
265        -----------------------------------------------------------------
266 
267        l_count := 0;
268        UPDATE ap_invoice_lines_all ail
269        SET    tax_already_calculated_flag = NULL
270        WHERE  ail.invoice_id IN (SELECT DISTINCT(trx_id)
271                                  FROM   zx_trx_headers_gt
272                                  WHERE  application_id = 200
273                                  AND    entity_code = 'AP_INVOICES')
274        AND    NVL(ail.tax_already_calculated_flag, 'N') = 'Y'
275        AND    NOT EXISTS
276                 (SELECT 'Line Determining Factors Exist'
277                    FROM zx_lines_det_factors zf
278                   WHERE zf.application_id   = 200
279                     AND zf.entity_code        = 'AP_INVOICES'
280                     AND zf.event_class_code IN ('STANDARD INVOICES',
281                                                 'PREPAYMENT INVOICES',
282                                                 'EXPENSE REPORTS')
283                     -- bug 7233679
284                     AND ZF.TRX_LEVEL_TYPE = 'LINE'
285                     AND ZF.INTERNAL_ORGANIZATION_ID = AIL.ORG_ID
286                     AND ZF.lEDGER_ID=AIL.SET_OF_BOOKS_ID
287                     -- bug 7233679
288                     AND zf.trx_id           = ail.invoice_id
289                     AND zf.trx_line_id      = ail.line_number);
290 
291        l_count := SQL%ROWCOUNT;
292        -----------------------------------------------------------------
293        l_debug_info := l_count ||' rows updated in ap_invoice_lines_all.';
294        Print(l_api_name, l_debug_info);
295        -----------------------------------------------------------------
296        --Bug 6922266 End
297 
298     ELSE
299 
300        DELETE FROM ZX_TRX_HEADERS_GT;
301 
302        -----------------------------------------------------------------
303        l_debug_info := 'Populating invoice header local record';
304        Print(l_api_name,l_debug_info);
305        -----------------------------------------------------------------
306        OPEN  Invoice_Header(p_invoice_id);
307        FETCH Invoice_Header INTO l_inv_header_rec;
308        CLOSE Invoice_Header;
309 
310        IF ((l_inv_header_rec.quick_credit = 'Y') OR    -- Bug 5638822
311            (l_inv_header_rec.invoice_type_lookup_code IN ('AWT', 'INTEREST'))) THEN
312           RETURN l_return_status;
313        END IF;
314 
315        -------------------------------------------------------------------
316        l_debug_info := 'Is tax already called invoice level';
317        Print(l_api_name,l_debug_info);
318        -------------------------------------------------------------------
319        IF (AP_ETAX_UTILITY_PKG.Is_Tax_Already_Calc_Inv(
320 	          P_Invoice_Id           => p_invoice_id,
321 	          P_Calling_Sequence     => l_curr_calling_sequence)) THEN
322 
323            l_tax_already_calculated := 'Y';
324        ELSE
325            l_tax_already_calculated := 'N';
326        END IF;
327 
328        -----------------------------------------------------------------
329        l_debug_info := 'Populate Header';
330        Print(l_api_name,l_debug_info);
331        -----------------------------------------------------------------
332        IF NOT(AP_ETAX_SERVICES_PKG.Populate_Headers_GT(
333 	      P_Invoice_Header_Rec         => l_inv_header_rec,
334 	      P_Calling_Mode               => P_Calling_Mode,
335 	      P_eTax_Already_called_flag   => l_tax_already_calculated,
336 	      P_Event_Class_Code           => l_event_class_code,
337 	      P_Event_Type_Code            => l_event_type_code,
338 	      P_Error_Code                 => P_error_code,
339 	      P_Calling_Sequence           => l_curr_calling_sequence )) THEN
340 
341 	  l_return_status := FALSE;
342        END IF;
343 
344        ap_etax_pkg.g_inv_id_list(1) := l_inv_header_rec.invoice_id;
345 
346     END IF;
347 
348     IF g_invoices_to_process = 0 THEN
349        RETURN TRUE;
350     END IF;
351 
352     -----------------------------------------------------------------
353     l_debug_info := 'Reset Tax Calculation Flag';
354     Print(l_api_name,l_debug_info);
355     -----------------------------------------------------------------
356     --
357     -- Reset invoice lines tax_already_calculated_flag to Null. This
358     -- will reset only if the following conditions are met.
359     --
360     --
361     -- 1. Invoices that have gone through tax calculation on recouped
362     --    prepay distributions during matching AND tax is calculated
363     --    for the first time on the invoice.
364     -- 2. This is required to pass document level event type is passed
365     --    to eTax as STANDARD UPDATED and line level action as CREATE.
366     -- 3. This update must happen ONLY after populate_headers_gt is
367     --    invoked.
368     --
369 
370     l_count:=0; --Bug 6922266
371 
372     UPDATE ap_invoice_lines_all ail
373        SET tax_already_calculated_flag = NULL
374      WHERE ail.invoice_id = p_invoice_id
375        AND nvl(ail.tax_already_calculated_flag, 'N') = 'Y'
376        AND NOT EXISTS
377 		(SELECT 'Line Determining Factors Exist'
378 	           from zx_lines_det_factors zf
379 	          where zf.application_id   = 200
380 	            and zf.entity_code	    = 'AP_INVOICES'
381 	            and zf.event_class_code IN ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
382 	            and zf.trx_id           = ail.invoice_id
383 	            and zf.trx_line_id      = ail.line_number);
384 
385     l_count := SQL%ROWCOUNT;
386     -----------------------------------------------------------------
387     l_debug_info := l_count ||' rows updated in ap_invoice_lines_all.';
388     Print(l_api_name, l_debug_info);
389     -----------------------------------------------------------------
390     --Bug 6922266
391 
392     -----------------------------------------------------------------
393     l_debug_info := 'Purge staging tables';
394     Print(l_api_name,l_debug_info);
395     -----------------------------------------------------------------
396     DELETE FROM ZX_TRANSACTION_LINES_GT;
397     DELETE FROM ZX_IMPORT_TAX_LINES_GT;
398     DELETE FROM ZX_TRX_TAX_LINK_GT;
399 
400     AP_ETAX_SERVICES_PKG.G_SITE_ATTRIBUTES.DELETE;
401     AP_ETAX_SERVICES_PKG.G_ORG_ATTRIBUTES.DELETE;
402 
403 
404     OPEN C_SELECTED_INVOICES;
405     LOOP
406         FETCH C_SELECTED_INVOICES
407         BULK COLLECT INTO AP_ETAX_PKG.G_INV_ID_LIST,
408                           AP_ETAX_PKG.G_EVNT_CLS_LIST
409         LIMIT AP_ETAX_PKG.G_BATCH_LIMIT;
410 
411         IF AP_APPROVAL_PKG.G_VALIDATION_REQUEST_ID IS NOT NULL THEN
412            EXIT WHEN (C_SELECTED_INVOICES%NOTFOUND
413                       AND AP_ETAX_PKG.G_INV_ID_LIST.COUNT <= 0);
414         ELSE
415            EXIT WHEN AP_ETAX_PKG.G_INV_ID_LIST.COUNT <= 0;
416         END IF;
417 
418         FOR i IN AP_ETAX_PKG.G_INV_ID_LIST.FIRST.. AP_ETAX_PKG.G_INV_ID_LIST.LAST
419         LOOP
420 
421            IF l_validation_request_id IS NOT NULL THEN
422               -----------------------------------------------------------------
423               l_debug_info := 'Collect Invoice Header Details';
424               Print(l_api_name,l_debug_info);
425               -----------------------------------------------------------------
426               OPEN  Invoice_Header(ap_etax_pkg.g_inv_id_list(i));
427               FETCH Invoice_Header INTO l_inv_header_rec;
428               CLOSE Invoice_Header;
429               l_inv_header_rec2:=l_inv_header_rec; ---for bug 6064593
430               -----------------------------------------------------------------
431               l_debug_info := 'Get event class code';
432               Print(l_api_name,l_debug_info);
433               -----------------------------------------------------------------
434               l_event_class_code := ap_etax_pkg.g_evnt_cls_list(i);
435 
436            ELSE
437 	      ap_etax_pkg.g_inv_id_list(1) := l_inv_header_rec.invoice_id;
438               l_inv_header_rec2:=l_inv_header_rec; ---for bug 6064593
439 
440            END IF;
441 
442            -----------------------------------------------------------------
443            l_debug_info := 'Cache Line Defaults';
444            Print(l_api_name,l_debug_info);
445            -----------------------------------------------------------------
446 
447                IF l_inv_header_rec.invoice_type_lookup_code = 'PAYMENT REQUEST' THEN  /* if
448                   condition for bug 5967914 as we need tp pass party_site_id instead of
449                   vendor_site_id if invoice_type_lookup_code ='PAYMENT REQUEST' */
450                  l_payment_request_flag :='Y';  -- for bug 5967914
451 	         Cache_Line_Defaults
452 	               ( p_org_id           => l_inv_header_rec.org_id
453 	                ,p_vendor_site_id   => l_inv_header_rec.party_site_id
454 	                ,p_calling_sequence => l_curr_calling_sequence);
455                ELSE
456                  l_payment_request_flag :='N';  -- for bug 5967914
457                	  Cache_Line_Defaults
458 	               ( p_org_id           => l_inv_header_rec.org_id
459 	                ,p_vendor_site_id   => l_inv_header_rec.vendor_site_id
460 	                ,p_calling_sequence => l_curr_calling_sequence);
461                END IF;
462 
463 	   -----------------------------------------------------------------
464            l_debug_info := 'Populate invoice tax lines collection';
465            Print(l_api_name,l_debug_info);
466            -----------------------------------------------------------------
467            BEGIN
468              OPEN  Tax_Lines_to_Import (ap_etax_pkg.g_inv_id_list(i));
469              FETCH Tax_Lines_to_Import
470              BULK  COLLECT INTO l_inv_tax_list;
471              CLOSE Tax_Lines_to_Import;
472            EXCEPTION WHEN NO_DATA_FOUND THEN NULL;
473            END;
474             /* Start added for 6014115 - If a invoice has some item lines
475                (receipt matched) and manual tax line not matched to receipt
476                 then we need to pass the applied to fields as null.
477                 So to do the same we are chking weahter we have manual
478                 tax lines or not by  g_manual_tax_lines = 'Y' and
479                 manual tax lines are receipt matched or not by
480                 l_manual_tax_line_rcv_mtch := 'N'.And then we will make
481                 the applied to columns values as NULL. also i will pass
482                 trans_lines(i).applied_to_trx_id   and
483                 trans_lines(i).applied_to_trx_id   as nullin this case.
484                 In this case we will proprate the manual tax line among
485                 all the ITEM lines.We are doing these chages as per
486                 AP_HLD_ETAX_VERSION_2_3.DOC. This is the documnet in
487                 in which we have all the guide lines for AP's Etax takeup. */
488             IF ( l_inv_tax_list.COUNT > 0) THEN
489               FOR i IN l_inv_tax_list.FIRST..l_inv_tax_list.LAST LOOP
490                 IF (l_inv_tax_list(i).rcv_transaction_id IS NOT NULL) THEN
491                    l_manual_tax_line_rcv_mtch := 'Y';
492                    EXIT;
493                 ELSE
494                    l_manual_tax_line_rcv_mtch := 'N';
495                    EXIT;
496                 END IF;
497                END LOOP;
498             ELSE
499                 l_manual_tax_line_rcv_mtch := NULL;
500 
501             END IF;
502             ---End for bug 6014115.
503            -----------------------------------------------------------------
504            l_debug_info := 'Populate invoice lines collection';
505            Print(l_api_name,l_debug_info);
506            -----------------------------------------------------------------
507            -- If there is any tax line to be imported the API will call
508            -- import document with tax for the whole invoice omiting the
509            -- p_line_number parameter
510 
511            IF (P_line_number IS NOT NULL AND
512                l_inv_tax_list.COUNT = 0 ) THEN
513                BEGIN
514                  OPEN  Invoice_Line (p_invoice_id, p_line_number);
515                  FETCH Invoice_Line
516                  BULK  COLLECT INTO l_inv_line_list;
517                  CLOSE Invoice_Line;
518                EXCEPTION WHEN NO_DATA_FOUND THEN NULL;
519                END;
520            ELSE
521                BEGIN
522                  OPEN  Invoice_Lines (ap_etax_pkg.g_inv_id_list(i));
523                  FETCH Invoice_Lines
524                  BULK  COLLECT INTO l_inv_line_list;
525                  CLOSE Invoice_Lines;
526                EXCEPTION WHEN NO_DATA_FOUND THEN NULL;
527                END;
528            END IF;
529 
530 
531            IF l_tax_already_calculated IS NULL THEN
532               -------------------------------------------------------------------
533               l_debug_info := 'Is tax already called invoice level';
534               Print(l_api_name,l_debug_info);
535               -------------------------------------------------------------------
536               IF (AP_ETAX_UTILITY_PKG.Is_Tax_Already_Calc_Inv(
537              	     P_Invoice_Id           => l_inv_header_rec.invoice_id,
538                      P_Calling_Sequence     => l_curr_calling_sequence)) THEN
539 	           l_tax_already_calculated := 'Y';
540               ELSE
541                    l_tax_already_calculated := 'N';
542               END IF;
543            END IF;
544 
545 
546            IF (l_tax_already_calculated = 'Y') THEN
547                -----------------------------------------------------------------
548                l_debug_info := 'If tax already calculated call freeze distributions';
549                Print(l_api_name,l_debug_info);
550                -----------------------------------------------------------------
551                --Bug7592845
552                IF NOT(AP_ETAX_SERVICES_PKG.Freeze_itm_Distributions(
553 	                P_Invoice_Header_Rec  => l_inv_header_rec,
554 	                P_Calling_Mode        => 'FREEZE DISTRIBUTIONS',
555 	                P_Event_Class_Code    => l_event_class_code,
556 	                P_All_Error_Messages  => P_All_Error_Messages,
557 	                P_Error_Code          => P_error_code,
558 	                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
559 
560 	          l_return_status := FALSE;
561 	       END IF;
562 	   END IF;
563 
564            -- This flow assumes that the UI will not call this API when the invoice
565            -- has tax lines to be imported and tax lines to matche to receipts. It
566            -- is restricted as this flow calls only 1 service at a time.
567 
568            IF (l_inv_line_list.COUNT > 0 AND l_inv_tax_list.COUNT = 0) THEN
569 
570                g_manual_tax_lines := 'N';
571                l_call_etax        := TRUE; --Bug7136832
572 
573                -----------------------------------------------------------------
574                l_debug_info := 'Populate TRX lines. No tax lines exist';
575                Print(l_api_name,l_debug_info);
576                -----------------------------------------------------------------
577                IF (l_return_status = TRUE)  THEN
578                    IF NOT(AP_ETAX_SERVICES_PKG.Populate_Lines_GT(
579 				P_Invoice_Header_Rec      => l_inv_header_rec,
580 				P_Calling_Mode            => P_Calling_Mode,
581 				P_Event_Class_Code        => l_event_class_code,
582 		          	P_Line_Number             => P_Line_Number,
583 		          	P_Error_Code              => P_error_code,
584 		          	P_Calling_Sequence        => l_curr_calling_sequence )) THEN
585 
586                        l_return_status := FALSE;
587 	           END IF;
588 	       END IF;
589 
590                /*l_no_tax_lines := 'Y'; */ --Bug6521120
591 
592            ELSIF (l_inv_line_list.COUNT > 0 AND l_inv_tax_list.COUNT > 0) THEN
593 
594 		      l_no_tax_lines := 'N'; --Bug6521120
595                       g_manual_tax_lines := 'Y';
596                       l_call_etax        := TRUE; --Bug7136832
597 
598                   -----------------------------------------------------------------
599                   l_debug_info := 'Populate TRX Lines. IMPORT';
600                   Print(l_api_name,l_debug_info);
601                   -----------------------------------------------------------------
602 	          IF (l_return_status = TRUE) THEN
603 	              IF NOT(AP_ETAX_SERVICES_PKG.Populate_Lines_GT(
604 			          P_Invoice_Header_Rec      => l_inv_header_rec,
605 			          P_Calling_Mode            => P_Calling_Mode,
606 			          P_Event_Class_Code        => l_event_class_code,
607 			          P_Line_Number             => P_Line_Number,
608 			          P_Error_Code              => P_error_code,
609 			          P_Calling_Sequence        => l_curr_calling_sequence )) THEN
610 
611                           l_return_status := FALSE;
612 	              END IF;
613 	          END IF;
614 
615 	          -----------------------------------------------------------------
616 	          l_debug_info := 'Populate TAX lines to be imported';
617 	          Print(l_api_name,l_debug_info);
618 	          -----------------------------------------------------------------
619 	          IF (l_return_status = TRUE) THEN
620 	              IF NOT(AP_ETAX_SERVICES_PKG.Populate_Tax_Lines_GT(
621 			          P_Invoice_Header_Rec      => l_inv_header_rec,
622 			          P_Calling_Mode            => P_Calling_Mode,
623 			          P_Event_Class_Code        => l_event_class_code,
624 			          P_Tax_only_Flag           => 'N',
625 			          P_Inv_Rcv_Matched         => l_Inv_Rcv_Matched,
626 			          P_Error_Code              => P_error_code,
627 			          P_Calling_Sequence        => l_curr_calling_sequence )) THEN
628 
629 	                  l_return_status := FALSE;
630 	              END IF;
631 	          END IF;
632 
633            ELSIF (l_inv_tax_list.COUNT > 0 AND l_inv_line_list.COUNT = 0) THEN
634 
635 	          -- Invoice is Tax only. We will need to determine if lines are receipt
636 		  -- matched to call calculate or if all tax information is populated to
637 		  -- call import. In both cases a pseudo line should be created to pass
638 	          -- additional information in the trx_lines GT table.
639               l_no_tax_lines := 'N';--Bug6521120
640               l_call_etax        := TRUE; --Bug7136832
641                   -----------------------------------------------------------------
642 	          l_debug_info := 'Populate pseudo TRX line, TAX lines to be imported';
643                   Print(l_api_name,l_debug_info);
644 	          -----------------------------------------------------------------
645 		      IF (l_return_status = TRUE) THEN
646 	              IF NOT(AP_ETAX_SERVICES_PKG.Populate_Tax_Lines_GT(
647 			          P_Invoice_Header_Rec      => l_inv_header_rec,
648 			          P_Calling_Mode            => P_Calling_Mode,
649 			          P_Event_Class_Code        => l_event_class_code,
650 			          P_Tax_only_Flag           => 'Y',
651 			          P_Inv_Rcv_Matched         => l_Inv_Rcv_Matched,
652 			          P_Error_Code              => P_error_code,
653 			          P_Calling_Sequence        => l_curr_calling_sequence )) THEN
654 
655 	                  l_return_status := FALSE;
656 	              END IF;
657 	          END IF;
658            ELSE
659             --Bug7136832
660             -----------------------------------------------------------------
661             l_debug_info := 'No invoice lines to be processed for Invoice Id '||l_inv_header_rec.invoice_id;
662             Print(l_api_name,l_debug_info);
663             -----------------------------------------------------------------
664             DELETE FROM zx_trx_headers_gt
665                 WHERE application_id   = AP_ETAX_PKG.AP_APPLICATION_ID
666                   AND entity_code      = AP_ETAX_PKG.AP_ENTITY_CODE
667                   AND event_class_code IN
668                                  (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
669                                   AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
670                                   AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
671                   AND trx_id = l_inv_header_rec.invoice_id;
672 
673                If l_validation_request_id IS  NULL THEN
674                   -----------------------------------------------------------------
675                   l_debug_info := 'Validation Request Id Is Null';
676                   Print(l_api_name,l_debug_info);
677                   -----------------------------------------------------------------
678                   RETURN TRUE;
679                END IF;
680            --Bug7136832
681            END IF;
682         END LOOP;
683 
684         --Bug7136832
685 
686         IF NOT l_call_etax  THEN
687             -----------------------------------------------------------------
688             l_debug_info := 'No lines to be processed Hence Return';
689             Print(l_api_name,l_debug_info);
690             -----------------------------------------------------------------
691             RETURN TRUE;
692         END IF;
693 
694         --Bug7136832
695 
696         AP_ETAX_PKG.G_INV_ID_LIST.DELETE;
697         AP_ETAX_PKG.G_EVNT_CLS_LIST.DELETE;
698     END LOOP;
699     CLOSE C_SELECTED_INVOICES;
700 
701     AP_ETAX_PKG.G_INV_ID_LIST.DELETE;
702     AP_ETAX_PKG.G_EVNT_CLS_LIST.DELETE;
703 
704 
705     IF (l_Inv_Rcv_Matched = 'Y' OR l_no_tax_lines = 'Y') THEN
706 
707         IF (l_return_status = TRUE) THEN
708             -----------------------------------------------------------------
709             l_debug_info := 'Call calculate_tax service';
710             Print(l_api_name,l_debug_info);
711             -----------------------------------------------------------------
712             zx_api_pub.calculate_tax(
713 	          p_api_version      => 1.0,
714 	          p_init_msg_list    => FND_API.G_TRUE,
715 	          p_commit           => FND_API.G_FALSE,
716 	          p_validation_level => FND_API.G_VALID_LEVEL_FULL,
717 	          x_return_status    => l_return_status_service,
718 	          x_msg_count        => l_msg_count,
719 	          x_msg_data         => l_msg_data);
720        END IF;
721     ELSE
722        IF (l_return_status = TRUE) THEN
723            -----------------------------------------------------------------
724            l_debug_info := 'Call import_document_with_tax service';
725            Print(l_api_name,l_debug_info);
726            -----------------------------------------------------------------
727 	   zx_api_pub.import_document_with_tax(
728 	          p_api_version      => 1.0,
729 	          p_init_msg_list    => FND_API.G_TRUE,
730 	          p_commit           => FND_API.G_FALSE,
731 	          p_validation_level => FND_API.G_VALID_LEVEL_FULL,
732 	          x_return_status    => l_return_status_service,
733 	          x_msg_count        => l_msg_count,
734 	          x_msg_data         => l_msg_data);
735        END IF;
736     END IF;
737 
738 
739     IF (l_return_status_service = 'S') THEN
740         -----------------------------------------------------------------
741         l_debug_info := 'Handle return of tax lines';
742         Print(l_api_name,l_debug_info);
743         -----------------------------------------------------------------
744         OPEN C_SELECTED_INVOICES;
745         LOOP
746            FETCH C_SELECTED_INVOICES
747             BULK COLLECT INTO AP_ETAX_PKG.G_INV_ID_LIST,
748 	                      AP_ETAX_PKG.G_EVNT_CLS_LIST
749 	   LIMIT AP_ETAX_PKG.G_BATCH_LIMIT;
750 
751            IF AP_APPROVAL_PKG.G_VALIDATION_REQUEST_ID IS NOT NULL THEN
752               EXIT WHEN (C_SELECTED_INVOICES%NOTFOUND
753                          AND AP_ETAX_PKG.G_INV_ID_LIST.COUNT <= 0);
754            ELSE
755               EXIT WHEN AP_ETAX_PKG.G_INV_ID_LIST.COUNT <= 0;
756            END IF;
757 
758            FOR i IN AP_ETAX_PKG.G_INV_ID_LIST.FIRST..AP_ETAX_PKG.G_INV_ID_LIST.LAST
759            LOOP
760                OPEN  Invoice_Header(ap_etax_pkg.g_inv_id_list(i));
761                FETCH Invoice_Header INTO l_inv_header_rec;
762                CLOSE Invoice_Header;
763 
764                IF NOT(AP_ETAX_SERVICES_PKG.Update_AP(
765 		                P_Invoice_header_rec => l_inv_header_rec,
766 		                P_Calling_Mode       => P_Calling_Mode,
767 		                P_All_Error_Messages => P_All_Error_Messages,
768 		                P_Error_Code         => P_error_code,
769 		                P_Calling_Sequence   => l_curr_calling_sequence)) THEN
770 
771 
772                    l_return_status := FALSE;
773                END IF;
774            END LOOP;
775            AP_ETAX_PKG.G_INV_ID_LIST.DELETE;
776            AP_ETAX_PKG.G_EVNT_CLS_LIST.DELETE;
777         END LOOP;
778         CLOSE C_SELECTED_INVOICES;
779 
780     ELSE
781         -----------------------------------------------------------------
782         l_debug_info := 'Handle errors returned by API';
783         Print(l_api_name,l_debug_info);
784         -----------------------------------------------------------------
785         l_return_status := FALSE;
786 
787         IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
788 	               P_All_Error_Messages  => P_All_Error_Messages,
789 	               P_Msg_Count           => l_msg_count,
790 	               P_Msg_Data            => l_msg_data,
791 	               P_Error_Code          => P_Error_Code,
792 	               P_Calling_Sequence    => l_curr_calling_sequence)) THEN
793             NULL;
794         END IF;
795     END IF;
796 
797     AP_ETAX_PKG.G_INV_ID_LIST.DELETE;
798     AP_ETAX_PKG.G_EVNT_CLS_LIST.DELETE;
799 
800     RETURN l_return_status;
801 
802   EXCEPTION
803     WHEN OTHERS THEN
804       IF (SQLCODE <> -20001) THEN
805         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
806         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
807         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
808         FND_MESSAGE.SET_TOKEN('PARAMETERS',
809           ' P_Invoice_Id = '||P_Invoice_Id||
810           ' P_Calling_Mode ='||P_Calling_Mode||
811           ' P_Error_Code = '||P_Error_Code||
812           ' P_Calling_Sequence = '||P_Calling_Sequence);
813         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
814       END IF;
815 
816       APP_EXCEPTION.RAISE_EXCEPTION;
817 
818   END Calculate;
819 
820 /*=============================================================================
821  |  FUNCTION - Calculate_Import()
822  |
823  |  DESCRIPTION
824  |      Public function that will call the calculate_tax service for
825  |      calculation and recalculation from the import program.
826  |      This new calling mode is required to avoid the repopulation of the eTax
827  |      global temp tables
828  |      This API assumes the calling code controls the commit cycle.
829  |      This function returns TRUE if the call to the service is successful.
830  |      Otherwise, FALSE.
831  |
832  |  PARAMETERS
833  |      P_Invoice_Id - invoice id
834  |      P_Calling_Mode - calling mode.  Identifies which service to call
835  |      P_Interface_Invoice_Id - Interface invoice id
836  |      P_All_Error_Messages - Should API return 1 error message or allow
837  |                             calling point to get them from message stack
838  |      P_error_code - Error code to be returned
839  |      P_calling_sequence -  Calling sequence
840  |
841  |  MODIFICATION HISTORY
842  |    DATE          Author         Action
843  |    14-JAN-2004   SYIDNER        Created
844  |
845  *============================================================================*/
846 
847   FUNCTION Calculate_Import(
848              P_Invoice_Id              IN NUMBER,
849              P_Calling_Mode            IN VARCHAR2,
850              P_Interface_Invoice_Id    IN NUMBER,
851              P_All_Error_Messages      IN VARCHAR2,
852              P_Error_Code              OUT NOCOPY VARCHAR2,
853              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
854   IS
855 
856     l_debug_info                 VARCHAR2(240);
857     l_curr_calling_sequence      VARCHAR2(4000);
858 
859     l_return_status_service       VARCHAR2(4000);
860     l_msg_count                   NUMBER;
861     l_msg_data                    VARCHAR2(4000);
862     l_msg                         VARCHAR2(4000);
863 
864     l_return_status               BOOLEAN := TRUE;
865     l_inv_header_rec             ap_invoices_all%ROWTYPE;
866 
867     CURSOR Invoice_Header IS
868     SELECT *
869       FROM ap_invoices_all
870      WHERE invoice_id = P_Invoice_Id;
871 
872     CURSOR Invoice_Lines (c_invoice_id NUMBER) IS
873     SELECT *
874       FROM ap_invoice_lines_all
875      WHERE invoice_id = c_invoice_id
876        AND line_type_lookup_code NOT IN ('TAX', 'AWT');
877 
878     l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
879     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
880 
881     l_api_name                    CONSTANT VARCHAR2(100) := 'Calculate_Import';
882 
883   BEGIN
884     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Calculate_Import<-' ||
885                                P_calling_sequence;
886 
887     -----------------------------------------------------------------
888     l_debug_info := 'Step 1: Populating invoice header local record';
889     -----------------------------------------------------------------
890     -- This call is included to get the data required to call the MC API
891     -- and to call Update_AP if the call to the eTax service is succesfull
892 
893     BEGIN
894       OPEN Invoice_Header;
895       FETCH Invoice_Header INTO l_inv_header_rec;
896       CLOSE Invoice_Header;
897     END;
898 
899     -----------------------------------------------------------------
900     l_debug_info := 'Step 2: Update trx_id in header';
901     Print(l_api_name, l_debug_info);
902     -----------------------------------------------------------------
903     UPDATE zx_trx_headers_gt
904        SET trx_id = P_Invoice_Id
905      WHERE application_id   = AP_ETAX_PKG.AP_APPLICATION_ID
906        AND entity_code      = AP_ETAX_PKG.AP_ENTITY_CODE
907        AND event_class_code IN (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
908                                 AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
909                                 AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
910        AND trx_id = P_Interface_Invoice_Id;
911 
912 
913     IF SQL%ROWCOUNT = 0 THEN
914 
915            -----------------------------------------------------------------
916            l_debug_info := 'Reset Tax Calculation Flag';
917            Print(l_api_name, l_debug_info);
918            -----------------------------------------------------------------
919            UPDATE ap_invoice_lines_all ail
920               SET ail.tax_already_calculated_flag = NULL
921             WHERE ail.invoice_id = p_invoice_id
922               AND ail.line_type_lookup_code NOT IN ('TAX', 'AWT')
923               AND NVL(ail.tax_already_calculated_flag, 'N') = 'Y';
924 
925 	   -- 1. validate_default_import is called during import. This populates
926 	   --    zx_trx_headers_gt and zx_transaction_lines_gt for validation of
927 	   --    the taxable lines.
928 	   -- 2. During import of invoices matched to complex work purchase orders,
929 	   --    matching will recoup prepayments and calculate tax on it. This
930 	   --    would have purged zx_trx_headers_gt and zx_transaction_lines_gt.
931            -- 3. In this case, the staging table will need to repopulated.
932            --    Parameter P_eTax_Already_called_flag must be passed as 'Y'
933 	   --    to ensure document level event type is passed to eTax
934            --    as 'STANDARD UPDATED'.
935 	   -- 4. Use Case is importing ERS invoices matched to complex work
936            --    purchase orders with paid advances/prepayments.
937 
938 	   -----------------------------------------------------------------
939 	   l_debug_info := 'Populate Headers';
940 	   Print(l_api_name, l_debug_info);
941 	   -----------------------------------------------------------------
942 
943            IF NOT(AP_ETAX_SERVICES_PKG.Populate_Headers_GT(
944                       P_Invoice_Header_Rec         => l_inv_header_rec,
945                       P_Calling_Mode               => 'CALCULATE',
946                       P_eTax_Already_called_flag   => 'Y',
947                       P_Event_Class_Code           => l_event_class_code,
948                       P_Event_Type_Code            => l_event_type_code,
949                       P_Error_Code                 => P_error_code,
950                       P_Calling_Sequence           => l_curr_calling_sequence )) THEN
951 
952                l_return_status := FALSE;
953            END IF;
954 
955            IF (l_return_status = TRUE)  THEN
956 
957                OPEN  Invoice_Lines (p_invoice_id);
958                FETCH Invoice_Lines
959                BULK  COLLECT INTO l_inv_line_list;
960                CLOSE Invoice_Lines;
961 
962                -------------------------------------------------------------------
963 	       l_debug_info := 'Cache Line Defaults';
964                Print(l_api_name, l_debug_info);
965 	       -------------------------------------------------------------------
966                IF l_inv_header_rec.invoice_type_lookup_code = 'PAYMENT REQUEST' THEN  /* if
967                   condition for bug 5967914 as we need tp pass party_site_id instead of
968                   vendor_site_id if invoice_type_lookup_code ='PAYMENT REQUEST' */
969                  l_payment_request_flag :='Y';  -- for bug 5967914
970 	         Cache_Line_Defaults
971 	               ( p_org_id           => l_inv_header_rec.org_id
972 	                ,p_vendor_site_id   => l_inv_header_rec.party_site_id
973 	                ,p_calling_sequence => l_curr_calling_sequence);
974                ELSE
975                  l_payment_request_flag :='N';  -- for bug 5967914
976                	  Cache_Line_Defaults
977 	               ( p_org_id           => l_inv_header_rec.org_id
978 	                ,p_vendor_site_id   => l_inv_header_rec.vendor_site_id
979 	                ,p_calling_sequence => l_curr_calling_sequence);
980              END IF;
981 
982                -----------------------------------------------------------------
983                l_debug_info := 'Populate Lines';
984                Print(l_api_name, l_debug_info);
985                -----------------------------------------------------------------
986 
987                IF NOT(AP_ETAX_SERVICES_PKG.Populate_Lines_GT(
988                                 P_Invoice_Header_Rec      => l_inv_header_rec,
989                                 P_Calling_Mode            => 'CALCULATE',
990                                 P_Event_Class_Code        => l_event_class_code,
991                                 P_Error_Code              => P_error_code,
992                                 P_Calling_Sequence        => l_curr_calling_sequence )) THEN
993 
994                   l_return_status := FALSE;
995                END IF;
996            END IF;
997 
998     ELSE
999 
1000       -----------------------------------------------------------------
1001       l_debug_info := 'Step 3: Update trx_id in Lines';
1002       -----------------------------------------------------------------
1003       UPDATE zx_transaction_lines_gt
1004          SET trx_id = P_Invoice_Id
1005        WHERE application_id   = AP_ETAX_PKG.AP_APPLICATION_ID
1006          AND entity_code      = AP_ETAX_PKG.AP_ENTITY_CODE
1007          AND event_class_code IN (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
1008                                   AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
1009                                   AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
1010          AND trx_id = P_Interface_Invoice_Id;
1011 
1012     END IF;
1013 
1014     -------------------------------------------------------------------
1015     l_debug_info := 'Step 4: Get event class code';
1016     -------------------------------------------------------------------
1017     IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
1018       P_Invoice_Type_Lookup_Code => l_inv_header_rec.invoice_type_lookup_code,
1019       P_Event_Class_Code         => l_event_class_code,
1020       P_error_code               => p_error_code,
1021       P_calling_sequence         => l_curr_calling_sequence)) THEN
1022 
1023       l_return_status := FALSE;
1024 
1025     END IF;
1026 
1027     -------------------------------------------------------------------
1028     l_debug_info := 'Step 5: Get event type code';
1029     -------------------------------------------------------------------
1030     IF (l_return_status = TRUE) THEN
1031       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
1032         P_Event_Class_Code          => l_event_class_code,
1033         P_Calling_Mode              => P_Calling_Mode,
1034         P_eTax_Already_called_flag  => NULL,
1035         P_Event_Type_Code           => l_event_type_Code,
1036         P_Error_Code                => P_error_code,
1037         P_Calling_Sequence          => l_curr_calling_sequence)) THEN
1038 
1039         l_return_status := FALSE;
1040       END IF;
1041     END IF;
1042 
1043 
1044     IF (l_return_status = TRUE) THEN
1045 
1046       -----------------------------------------------------------------
1047       l_debug_info := 'Step 7: Call calculate_tax service';
1048       Print(l_api_name,l_debug_info);
1049       -----------------------------------------------------------------
1050       zx_api_pub.calculate_tax(
1051         p_api_version      => 1.0,
1052         p_init_msg_list    => FND_API.G_TRUE,
1053         p_commit           => FND_API.G_FALSE,
1054         p_validation_level => FND_API.G_VALID_LEVEL_FULL,
1055         x_return_status    => l_return_status_service,
1056         x_msg_count        => l_msg_count,
1057         x_msg_data         => l_msg_data);
1058 
1059     END IF;
1060 
1061     IF (l_return_status_service = 'S') THEN
1062 
1063        -----------------------------------------------------------------
1064        l_debug_info := 'Step 8: Handle return of tax lines';
1065        -----------------------------------------------------------------
1066        IF NOT(AP_ETAX_SERVICES_PKG.Update_AP(
1067                 P_Invoice_header_rec => l_inv_header_rec,
1068                 P_Calling_Mode       => P_Calling_Mode,
1069                 P_All_Error_Messages => P_All_Error_Messages,
1070                 P_Error_Code         => P_error_code,
1071                 P_Calling_Sequence   => l_curr_calling_sequence)) THEN
1072 
1073           l_return_status := FALSE;
1074        END IF;
1075 
1076     ELSE  -- handle errors
1077 
1078       -----------------------------------------------------------------
1079       l_debug_info := 'Step 9: Handle errors returned by API';
1080       -----------------------------------------------------------------
1081       l_return_status := FALSE;
1082 
1083       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
1084                P_All_Error_Messages  => P_All_Error_Messages,
1085                P_Msg_Count           => l_msg_count,
1086                P_Msg_Data            => l_msg_data,
1087                P_Error_Code          => P_Error_Code,
1088                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
1089         NULL;
1090       END IF;
1091 
1092     END IF;
1093 
1094     RETURN l_return_status;
1095 
1096   EXCEPTION
1097     WHEN OTHERS THEN
1098       IF (SQLCODE <> -20001) THEN
1099         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
1100         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
1101         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
1102         FND_MESSAGE.SET_TOKEN('PARAMETERS',
1103           ' P_Invoice_Id = '||P_Invoice_Id||
1104           ' P_Calling_Mode ='||P_Calling_Mode||
1105           ' P_Error_Code = '||P_Error_Code||
1106           ' P_Calling_Sequence = '||P_Calling_Sequence);
1107         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
1108       END IF;
1109 
1110       APP_EXCEPTION.RAISE_EXCEPTION;
1111 
1112   END Calculate_Import;
1113 
1114 /*=============================================================================
1115  |  FUNCTION - Distribute()
1116  |
1117  |  DESCRIPTION
1118  |      Public function that will call the determine_recovery service for
1119  |      distribution and redistribution.
1120  |      This API assumes the calling code controls the commit cycle.
1121  |      This function returns TRUE if the call to the service is successful.
1122  |      Otherwise, FALSE.
1123  |
1124  |  PARAMETERS
1125  |      P_Invoice_Id - invoice id
1126  |      P_Line_Number - This parameter will be used to allow this API to
1127  |                      distribute tax only for the line specified in this
1128  |                      parameter.
1129  |      P_Calling_Mode - calling mode.  Identifies which service to call
1130  |      P_All_Error_Messages - Should API return 1 error message or allow
1131  |                             calling point to get them from message stack
1132  |      P_error_code - Error code to be returned
1133  |      P_calling_sequence -  Calling sequence
1134  |
1135  |  MODIFICATION HISTORY
1136  |    DATE          Author         Action
1137  |    07-OCT-2003   SYIDNER        Created
1138  |    29-DEC-2003   SYIDNER        Use of new function to validate if tax was
1139  |                                 already distributed for the invoice.  Function
1140  |                                 created in the ap_etax_utility_pkg.
1141  |
1142  *============================================================================*/
1143 
1144   FUNCTION Distribute(
1145              P_Invoice_id              IN NUMBER,
1146              P_Line_Number             IN NUMBER,
1147              P_Calling_Mode            IN VARCHAR2,
1148              P_All_Error_Messages      IN VARCHAR2,
1149              P_Error_Code              OUT NOCOPY VARCHAR2,
1150              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN IS
1151 
1152     l_debug_info                 VARCHAR2(240);
1153     l_curr_calling_sequence      VARCHAR2(4000);
1154     l_api_name                   CONSTANT VARCHAR2(100) := 'Distribute';
1155 
1156     l_inv_header_rec             ap_invoices_all%ROWTYPE;
1157     l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
1158     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
1159 
1160     l_return_status_service       VARCHAR2(4000);
1161     l_msg_count                   NUMBER;
1162     l_msg_data                    VARCHAR2(4000);
1163     l_msg                         VARCHAR2(4000);
1164 
1165     l_tax_already_distributed     VARCHAR2(1);
1166     l_return_status               BOOLEAN := TRUE;
1167 
1168     l_tax_only_invoice            NUMBER := 0;  --Bug7110987
1169 
1170     --Bug 7413378
1171     call_determine_recovery_flag  BOOLEAN := FALSE;
1172 
1173     CURSOR Invoice_Header (c_invoice_id NUMBER) IS
1174     SELECT *
1175       FROM ap_invoices_all
1176      WHERE invoice_id = c_invoice_id;
1177 
1178     CURSOR Invoice_Dists (c_invoice_id NUMBER) IS
1179     SELECT aid.*
1180       FROM ap_invoice_lines_all ail,
1181            ap_invoice_distributions_all aid
1182      WHERE ail.invoice_id  = aid.invoice_id
1183        AND ail.line_number = aid.invoice_line_number
1184        AND ail.invoice_id  = c_invoice_id
1185        AND aid.line_type_lookup_code NOT IN
1186            	('AWT', 'REC_TAX', 'NONREC_TAX', 'TRV', 'TERV', 'TIPV')
1187        AND (aid.line_type_lookup_code <> 'RETAINAGE'
1188 	    or (aid.line_type_lookup_code = 'RETAINAGE'
1189 	        and ail.line_type_lookup_code = 'RETAINAGE RELEASE'))
1190        AND (related_id IS NULL
1191             or related_id = invoice_distribution_id)
1192        AND (aid.prepay_distribution_id IS NULL
1193             or (aid.prepay_distribution_id IS NOT NULL
1194                 and ail.line_type_lookup_code = 'PREPAY'))
1195        AND nvl(ail.discarded_flag, 'N') <> 'Y'
1196        AND p_calling_mode <> 'DISTRIBUTE RECOUP'
1197     UNION
1198     SELECT aid.*
1199       FROM ap_invoice_lines_all ail,
1200            ap_invoice_distributions_all aid
1201      WHERE ail.invoice_id  = aid.invoice_id
1202        AND ail.line_number = aid.invoice_line_number
1203        AND ail.invoice_id  = c_invoice_id
1204        AND ail.line_type_lookup_code  <> 'PREPAY'
1205        AND aid.line_type_lookup_code  =  'PREPAY'
1206        AND aid.prepay_distribution_id IS NOT NULL
1207        AND p_calling_mode = 'DISTRIBUTE RECOUP';
1208 
1209     -- The plsql table will include the primary distribution, but the amount
1210     -- will be modified in the populate_distributions_gt function to add the
1211     -- IPV and ERV amounts if they exist.
1212 
1213 
1214     -- If the API is called to distribute only 1 taxable line, the following
1215     -- cursor will be used
1216     CURSOR Invoice_Dist (c_invoice_id NUMBER, c_line_number NUMBER) IS
1217     SELECT aid.*
1218       FROM ap_invoice_lines_all ail,
1219            ap_invoice_distributions_all aid
1220      WHERE ail.invoice_id  = aid.invoice_id
1221        AND ail.line_number = aid.invoice_line_number
1222        AND ail.invoice_id  = c_invoice_id
1223        AND ail.line_number = c_line_number
1224        AND aid.line_type_lookup_code NOT IN
1225                 ('AWT', 'REC_TAX', 'NONREC_TAX', 'TRV', 'TERV', 'TIPV')
1226        AND (related_id IS NULL
1227             or related_id = invoice_distribution_id)
1228        AND (aid.prepay_distribution_id IS NULL
1229             or (aid.prepay_distribution_id IS NOT NULL
1230                 and ail.line_type_lookup_code = 'PREPAY'))
1231        AND nvl(ail.discarded_flag, 'N') <> 'Y'
1232        AND p_calling_mode <> 'DISTRIBUTE RECOUP'
1233     UNION
1234     SELECT aid.*
1235       FROM ap_invoice_lines_all ail,
1236            ap_invoice_distributions_all aid
1237      WHERE ail.invoice_id  = aid.invoice_id
1238        AND ail.line_number = aid.invoice_line_number
1239        AND ail.invoice_id  = c_invoice_id
1240        AND ail.line_number = c_line_number
1241        AND ail.line_type_lookup_code  <> 'PREPAY'
1242        AND aid.line_type_lookup_code  =  'PREPAY'
1243        AND aid.prepay_distribution_id IS NOT NULL
1244        AND p_calling_mode = 'DISTRIBUTE RECOUP';
1245 
1246 
1247     CURSOR c_selected_invoices IS
1248     SELECT trx_id, event_class_code
1249       FROM zx_trx_headers_gt
1250      WHERE application_id   =  ap_etax_pkg.ap_application_id
1251        AND entity_code      =  ap_etax_pkg.ap_entity_code
1252        AND event_class_code IN (ap_etax_pkg.ap_inv_event_class_code,
1253                                 ap_etax_pkg.ap_pp_event_class_code,
1254                                 ap_etax_pkg.ap_er_event_class_code);
1255 
1256     --Bug6678578 START
1257     CURSOR c_included_tax_amounts (c_invoice_id NUMBER) IS
1258     SELECT amount,
1259 	       NVL(included_tax_amount,0) included_tax_amount,
1260 		   line_number,
1261            (NVL(total_rec_tax_amt_funcl_curr,0) + NVL(total_nrec_tax_amt_funcl_curr,0)) base_included_tax_amount
1262       FROM ap_invoice_lines_all
1263      WHERE invoice_id =  c_invoice_id
1264        AND line_type_lookup_code IN ('ITEM', 'ACCRUAL', 'FREIGHT', 'MISCELLANEOUS','PREPAY')  -- bug7338249
1265        AND included_tax_amount IS NOT NULL ;--Bug6874234
1266 
1267     CURSOR c_included_tax_amount (c_invoice_id NUMBER,c_line_number NUMBER) IS
1268     SELECT amount,
1269 	       NVL(included_tax_amount,0) included_tax_amount,
1270 		   line_number,
1271            (NVL(total_rec_tax_amt_funcl_curr,0) + NVL(total_nrec_tax_amt_funcl_curr,0)) base_included_tax_amount
1272       FROM ap_invoice_lines_all
1273      WHERE invoice_id =  c_invoice_id
1274        AND line_number = c_line_number
1275        AND line_type_lookup_code IN ('ITEM', 'ACCRUAL', 'FREIGHT', 'MISCELLANEOUS','PREPAY') --bug7338249
1276        AND included_tax_amount IS NOT NULL ;--Bug6874234
1277     --Bug6678578 END
1278 
1279 
1280   BEGIN
1281 
1282     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Distribute<-' ||
1283                                P_calling_sequence;
1284 
1285     IF ap_approval_pkg.g_validation_request_id IS NULL THEN
1286 
1287        DELETE FROM ZX_TRX_HEADERS_GT;
1288 
1289        -----------------------------------------------------------------
1290        l_debug_info := 'Populate invoice header local record';
1291        Print(l_api_name,l_debug_info);
1292        -----------------------------------------------------------------
1293        OPEN  Invoice_Header (p_invoice_id);
1294        FETCH Invoice_Header INTO l_inv_header_rec;
1295        CLOSE Invoice_Header;
1296 
1297        IF ((l_inv_header_rec.quick_credit = 'Y') OR    -- Bug 5638822
1298            (l_inv_header_rec.invoice_type_lookup_code IN ('AWT', 'INTEREST'))) THEN
1299           RETURN l_return_status;
1300        END IF;
1301 
1302 
1303        -------------------------------------------------------------------
1304        l_debug_info := 'Is tax already distributed for invoice';
1305        Print(l_api_name,l_debug_info);
1306        -------------------------------------------------------------------
1307        IF (AP_ETAX_UTILITY_PKG.Is_Tax_Already_Dist_Inv(
1308 	          P_Invoice_Id           => p_invoice_id,
1309 	          P_Calling_Sequence     => l_curr_calling_sequence)) THEN
1310 
1311 	         l_tax_already_distributed := 'Y';
1312        ELSE
1313            l_tax_already_distributed := 'N';
1314        END IF;
1315 
1316        -----------------------------------------------------------------
1317        l_debug_info := 'Populate Header';
1318        Print(l_api_name,l_debug_info);
1319        -----------------------------------------------------------------
1320        IF NOT(AP_ETAX_SERVICES_PKG.Populate_Headers_GT(
1321 		      P_Invoice_Header_Rec        => l_inv_header_rec,
1322 		      P_Calling_Mode              => P_Calling_Mode,
1323 		      P_eTax_Already_called_flag  => l_tax_already_distributed,
1324 		      P_Event_Class_Code          => l_event_class_code,
1325 		      P_Event_Type_Code           => l_event_type_code,
1326 		      P_Error_Code                => P_error_code,
1327 		      P_Calling_Sequence          => l_curr_calling_sequence )) THEN
1328 
1329            l_return_status :=  FALSE;
1330        END IF;
1331 
1332        ap_etax_pkg.g_inv_id_list(1) := l_inv_header_rec.invoice_id;
1333 
1334 
1335     ELSE
1336        -----------------------------------------------------------------
1337        l_debug_info := 'Batch: Bulk Populate Header';
1338        Print(l_api_name,l_debug_info);
1339        -----------------------------------------------------------------
1340        IF NOT AP_ETAX_SERVICES_PKG.Bulk_Populate_Headers_GT(
1341 			     p_validation_request_id => ap_approval_pkg.g_validation_request_id,
1342 	                p_calling_mode          => p_calling_mode,
1343 	                p_error_code            => p_error_code) THEN
1344 
1345            l_return_status := FALSE;
1346        END IF;
1347      END IF;
1348 
1349      IF g_invoices_to_process = 0 THEN
1350         RETURN TRUE;
1351      END IF;
1352 
1353      -----------------------------------------------------------------
1354      l_debug_info := 'Purge Staging Table';
1355      Print(l_api_name,l_debug_info);
1356      -----------------------------------------------------------------
1357      DELETE FROM zx_itm_distributions_gt;
1358 
1359 
1360      OPEN C_SELECTED_INVOICES;
1361      LOOP
1362      FETCH C_SELECTED_INVOICES
1363         BULK COLLECT INTO AP_ETAX_PKG.G_INV_ID_LIST,
1364 			  AP_ETAX_PKG.G_EVNT_CLS_LIST
1365         LIMIT AP_ETAX_PKG.G_BATCH_LIMIT;
1366 
1367         IF AP_APPROVAL_PKG.G_VALIDATION_REQUEST_ID IS NOT NULL THEN
1368            EXIT WHEN (C_SELECTED_INVOICES%NOTFOUND
1369                       AND AP_ETAX_PKG.G_INV_ID_LIST.COUNT <= 0);
1370         ELSE
1371            EXIT WHEN AP_ETAX_PKG.G_INV_ID_LIST.COUNT <= 0;
1372         END IF;
1373 
1374         FOR i IN AP_ETAX_PKG.G_INV_ID_LIST.FIRST.. AP_ETAX_PKG.G_INV_ID_LIST.LAST
1375         LOOP
1376 
1377 	    IF ap_approval_pkg.g_validation_request_id IS NOT NULL THEN
1378 
1379 	      OPEN  Invoice_Header(ap_etax_pkg.g_inv_id_list(i));
1380 	      FETCH Invoice_Header INTO l_inv_header_rec;
1381 	      CLOSE Invoice_Header;
1382 
1383 	      l_event_class_code := ap_etax_pkg.g_evnt_cls_list(i);
1384           --Bug7110987
1385           IF (l_tax_only_invoice = 0) THEN
1386 
1387              SELECT count(1)
1388                INTO l_tax_only_invoice
1389                FROM ap_invoice_lines_all ail
1390               WHERE ail.invoice_id = ap_etax_pkg.g_inv_id_list(i)
1391 			          AND ail.line_type_lookup_code = 'TAX'
1392                 AND NOT EXISTS --To Make Sure We do not call Determine Recovery If we do not have any lines to process
1393                     (SELECT 'No Non Tax Line'
1394                        FROM ap_invoice_lines_all ail2
1395                       WHERE ail2.invoice_id = ail.Invoice_id
1396 			                  AND ail2.line_type_lookup_code <> 'TAX');
1397           END IF;
1398           --Bug7110987
1399 	    ELSE
1400 
1401           ap_etax_pkg.g_inv_id_list(1) := l_inv_header_rec.invoice_id;
1402           --Bug7110987
1403           IF (l_tax_only_invoice = 0) THEN
1404 
1405             SELECT count(1)
1406                INTO l_tax_only_invoice
1407                FROM ap_invoice_lines_all ail
1408               WHERE ail.invoice_id = l_inv_header_rec.Invoice_id
1409 			          AND ail.line_type_lookup_code = 'TAX'
1410                 AND NOT EXISTS --To Make Sure We do not call Determine Recovery If we do not have any lines to process
1411                     (SELECT 'No Non Tax Line'
1412                        FROM ap_invoice_lines_all ail2
1413                       WHERE ail2.invoice_id = ail.Invoice_id
1414 			                  AND ail2.line_type_lookup_code <> 'TAX');
1415           END IF;
1416           --Bug7110987
1417         END IF;
1418 
1419            -----------------------------------------------------------------
1420            l_debug_info := 'Populate invoice distributions collection';
1421            Print(l_api_name,l_debug_info);
1422            -----------------------------------------------------------------
1423        IF (p_line_number IS NOT NULL) THEN
1424 
1425           --Bug6678578START
1426 	  -----------------------------------------------------------------
1427 	  FOR i  in c_included_tax_amount(p_invoice_id,p_line_number) LOOP
1428 	     -- IF (i.included_tax_amount <> 0) THEN      --Bug6874234
1429 	  ------------------------------------------------------------------------
1430             l_debug_info := 'Update dist amount for included TAX (ITEM line no and tax amount) '||p_line_number||' '||i.included_tax_amount;
1431 			Print(l_api_name,l_debug_info);
1432           ------------------------------------------------------------------------
1433 		        UPDATE ap_invoice_distributions_All aid1
1434 		        SET    aid1.amount = aid1.amount + nvl((SELECT SUM(nvl(amount,0))
1435 		                                  FROM ap_invoice_distributions_All aid2
1436 		                                 WHERE aid2.invoice_id =  p_invoice_id
1437 		                                  AND  aid2.invoice_line_number = i.line_number
1438 		                                  AND  aid2.charge_applicable_to_dist_id = aid1.invoice_distribution_id
1439 		                                  AND  aid2.line_type_lookup_code IN ('REC_TAX','NONREC_TAX','TRV','TIPV')),0),
1440 		               aid1.base_amount = aid1.base_amount + nvl((SELECT SUM(nvl(base_amount,0))
1441 		                                  FROM ap_invoice_distributions_All aid3
1442 		                                 WHERE aid3.invoice_id =  p_invoice_id
1443 		                                   AND aid3.invoice_line_number = i.line_number
1444 	                                           AND aid3.charge_applicable_to_dist_id = aid1.invoice_distribution_id
1445 		                                   AND aid3.line_type_lookup_code IN ('REC_TAX','NONREC_TAX','TRV','TIPV')),0)
1446 		        WHERE  aid1.invoice_id =  p_invoice_id
1447 		          AND  aid1.invoice_line_number = i.line_number
1448 		          AND  aid1.line_type_lookup_code IN ('ITEM', 'ACCRUAL', 'FREIGHT', 'MISCELLANEOUS','PREPAY');--/*Bug7338249*/
1449               -- END IF;	  --Bug6874234
1450 	  END LOOP;
1451 	--Bug6678578END
1452 	       OPEN  Invoice_Dist (p_invoice_id, p_line_number);
1453 	       FETCH Invoice_Dist
1454 	       BULK COLLECT INTO l_inv_dist_list;
1455 	       CLOSE Invoice_Dist;
1456 
1457 	       --Bug 7436274 (7413378)
1458 	       IF (l_inv_dist_list.count > 0) THEN
1459 		 call_determine_recovery_flag := TRUE;
1460 	       END IF;
1461        ELSE
1462           --Bug6678578START
1463           FOR i  in c_included_tax_amounts(p_invoice_id) LOOP
1464 	     -- IF (i.included_tax_amount <> 0) THEN   --Bug6874234
1465             ------------------------------------------------------------------------
1466             l_debug_info := 'Test: Update dist amount for included TAX (tax amount) '|| i.included_tax_amount;
1467 			Print(l_api_name,l_debug_info);
1468             ------------------------------------------------------------------------
1469   	            UPDATE ap_invoice_distributions_All aid1
1470 			    SET  aid1.amount = aid1.amount + nvl((SELECT SUM(nvl(amount,0))
1471 			                            FROM ap_invoice_distributions_All aid2
1472 			                           WHERE aid2.invoice_id =  p_invoice_id
1473 			                             AND aid2.invoice_line_number = i.line_number
1474 			                             AND aid2.charge_applicable_to_dist_id = aid1.invoice_distribution_id
1475 			                             AND aid2.line_type_lookup_code IN ('REC_TAX','NONREC_TAX','TRV','TIPV')),0),
1476 			           aid1.base_amount =aid1.base_amount + nvl((SELECT SUM(nvl(base_amount,0))
1477 			                            FROM ap_invoice_distributions_All aid3
1478 			                           WHERE aid3.invoice_id =  p_invoice_id
1479 			                             AND aid3.invoice_line_number = i.line_number
1480 	                                             AND aid3.charge_applicable_to_dist_id = aid1.invoice_distribution_id
1481           		                             AND aid3.line_type_lookup_code IN ('REC_TAX','NONREC_TAX','TRV','TIPV')),0)
1482 			    WHERE aid1.invoice_id =  p_invoice_id
1483 			      AND aid1.invoice_line_number = i.line_number
1484 			      AND  aid1.line_type_lookup_code IN ('ITEM', 'ACCRUAL', 'FREIGHT', 'MISCELLANEOUS','PREPAY');--/*Bug7338249*/
1485               -- END IF;	--Bug6874234
1486          END LOOP;
1487          --Bug6678578END
1488 	      OPEN Invoice_Dists (ap_etax_pkg.g_inv_id_list(i));
1489 	      FETCH Invoice_Dists
1490 	      BULK COLLECT INTO l_inv_dist_list;
1491 	      CLOSE Invoice_Dists;
1492 
1493 	      --Bug 7413378
1494 	      IF (l_inv_dist_list.count > 0) THEN
1495 		call_determine_recovery_flag := TRUE;
1496 	      END IF;
1497       END IF;
1498 
1499 	   -----------------------------------------------------------------
1500     	   l_debug_info := 'Populate Distributions';
1501            Print(l_api_name,l_debug_info);
1502     	   -----------------------------------------------------------------
1503     	   IF (l_return_status = TRUE
1504 	       and l_inv_dist_list.count > 0) THEN
1505 
1506 	       IF NOT (AP_ETAX_SERVICES_PKG.Populate_Distributions_GT(
1507 			        P_Invoice_Header_Rec      => l_inv_header_rec,
1508 			        P_Calling_Mode            => P_Calling_Mode,
1509 			        P_Event_Class_Code        => l_event_class_code,
1510 			        P_Event_Type_Code         => l_event_type_code,
1511 			        P_Error_Code              => P_error_code,
1512 			        P_Calling_Sequence        => l_curr_calling_sequence )) THEN
1513 
1514 		        l_return_status := FALSE;
1515 	       END IF;
1516 	   END IF;
1517 
1518            IF l_inv_header_rec.historical_flag = 'Y' THEN
1519 	      -------------------------------------------------------------------
1520        	      l_debug_info := 'Upgrade historical invoice distributions';
1521               Print(l_api_name,l_debug_info);
1522        	      -------------------------------------------------------------------
1523 	      UPDATE /*+ ROWID (AID) */ AP_Invoice_Distributions_All AID
1524 	      SET   (RECOVERY_RATE_CODE,
1525 	             RECOVERY_RATE_ID,
1526 	             RECOVERY_TYPE_CODE) =
1527 		                       (SELECT REC.Tax_Rate_Code,
1528 		                               REC.Tax_Rate_ID,
1529 		                               REC.Recovery_Type_Code
1530 		                        FROM   ZX_Rates_B RATE,
1531 		                               ZX_Rates_B REC
1532 		                        WHERE  RATE.Tax_Rate_ID		 = AID.Tax_Code_ID
1533 		                        AND    RATE.Tax_Regime_Code	 = REC.Tax_Regime_Code
1534 		                        AND    RATE.Tax			 = REC.Tax
1535 		                        AND    RATE.Tax_Status_Code	 = REC.Tax_Status_Code
1536 		                        AND    RATE.Content_Owner_ID	 = REC.Content_Owner_ID
1537 		                        AND    REC.Rate_type_code	 = 'RECOVERY'
1538 		                        AND    REC.Effective_From <= AID.Accounting_Date
1539 		                        AND    NVL(REC.Effective_To, AID.Accounting_Date) >= AID.Accounting_Date
1540 		                        AND    REC.Active_Flag		 = 'Y'
1541 		                        AND    REC.Percentage_Rate	 = AID.Rec_NRec_Rate
1542 		                        AND    REC.Tax_Rate_Code 	 = 'STANDARD-' || REC.Percentage_Rate
1543 		                        AND    AID.Line_Type_Lookup_Code = 'REC_TAX')
1544              WHERE AID.invoice_id            = l_inv_header_rec.invoice_id
1545 	       AND AID.historical_flag       = 'Y'
1546                AND AID.line_type_lookup_code = 'REC_TAX'
1547                AND AID.recovery_rate_code    Is Null
1548                AND AID.recovery_rate_id      Is Null
1549                AND AID.recovery_type_code    Is Null;
1550            END IF;
1551        END LOOP;
1552 
1553        AP_ETAX_PKG.G_INV_ID_LIST.DELETE;
1554        AP_ETAX_PKG.G_EVNT_CLS_LIST.DELETE;
1555 
1556     END LOOP;
1557     CLOSE C_SELECTED_INVOICES;
1558 
1559     -----------------------------------------------------------------
1560     l_debug_info := 'Call determine_recovery service';
1561     Print(l_api_name,l_debug_info);
1562     -----------------------------------------------------------------
1563     --Bug 7413378
1564     --At the end of C_SELECTED_INVOICES cursor, l_inv_dist_list.count contains number of distributions
1565     --for the last invoice id of Invoice Batch.If last invoice has no distributions the
1566     --"return of tax distributions" code is not executed for any invoice of the Invoice Batch.
1567     --Hence removed the l_inv_dist_list.count from IF condition.
1568     /*IF (l_return_status = TRUE
1569         and l_inv_dist_list.count > 0) THEN*/
1570     --"determine_recovery" should be executed if atleast one invoice of the Invoice Batch has distributions.
1571     --Bug7110987
1572     IF (l_return_status = TRUE
1573         and (call_determine_recovery_flag = TRUE OR l_tax_only_invoice > 0)) THEN
1574     --Bug7110987
1575         zx_api_pub.determine_recovery(
1576  	        p_api_version      => 1.0,
1577 	        p_init_msg_list    => FND_API.G_TRUE,
1578 	        p_commit           => FND_API.G_FALSE,
1579 	        p_validation_level => FND_API.G_VALID_LEVEL_FULL,
1580 	        x_return_status    => l_return_status_service,
1581 	        x_msg_count        => l_msg_count,
1582 	        x_msg_data         => l_msg_data);
1583 
1584        IF (l_return_status_service = 'S') THEN
1585            -----------------------------------------------------------------
1586            l_debug_info := 'Handle return of tax distributions';
1587            Print(l_api_name,l_debug_info);
1588            -----------------------------------------------------------------
1589            IF NOT(AP_ETAX_SERVICES_PKG.Update_AP(
1590  			P_Invoice_header_rec => l_inv_header_rec,
1591 	                P_Calling_Mode       => P_Calling_Mode,
1592 	                P_All_Error_Messages => P_All_Error_Messages,
1593 	                P_Error_Code         => P_error_code,
1594 	                P_Calling_Sequence   => l_curr_calling_sequence)) THEN
1595 
1596               l_return_status := FALSE;
1597            END IF;
1598 
1599        ELSE  -- handle errors
1600 
1601            l_return_status := FALSE;
1602 
1603            -----------------------------------------------------------------
1604            l_debug_info := 'Handle errors returned by API';
1605            Print(l_api_name,l_debug_info);
1606            -----------------------------------------------------------------
1607            IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
1608 			P_All_Error_Messages  => P_All_Error_Messages,
1609 			P_Msg_Count           => l_msg_count,
1610 			P_Msg_Data            => l_msg_data,
1611 			P_Error_Code          => P_Error_Code,
1612 			P_Calling_Sequence    => l_curr_calling_sequence)) THEN
1613               NULL;
1614            END IF;
1615        END IF;
1616 
1617        IF l_return_status = TRUE THEN
1618 
1619        OPEN C_SELECTED_INVOICES;
1620        LOOP
1621           FETCH C_SELECTED_INVOICES
1622           BULK COLLECT INTO AP_ETAX_PKG.G_INV_ID_LIST,
1623                             AP_ETAX_PKG.G_EVNT_CLS_LIST
1624           LIMIT AP_ETAX_PKG.G_BATCH_LIMIT;
1625 
1626           IF AP_APPROVAL_PKG.G_VALIDATION_REQUEST_ID IS NOT NULL THEN
1627              EXIT WHEN (C_SELECTED_INVOICES%NOTFOUND
1628                         AND AP_ETAX_PKG.G_INV_ID_LIST.COUNT <= 0);
1629           ELSE
1630              EXIT WHEN AP_ETAX_PKG.G_INV_ID_LIST.COUNT <= 0;
1631           END IF;
1632 
1633           FOR i IN AP_ETAX_PKG.G_INV_ID_LIST.FIRST.. AP_ETAX_PKG.G_INV_ID_LIST.LAST
1634           LOOP
1635              IF ap_approval_pkg.g_validation_request_id IS NOT NULL THEN
1636                 OPEN  Invoice_Header (ap_etax_pkg.g_inv_id_list(i));
1637                 FETCH Invoice_Header
1638                 INTO  l_inv_header_rec;
1639                 CLOSE Invoice_Header;
1640              END IF;
1641 
1642              -----------------------------------------------------------------
1643              l_debug_info := 'Update Invoice Distributions';
1644              Print(l_api_name,l_debug_info);
1645              -----------------------------------------------------------------
1646              IF NOT(AP_ETAX_SERVICES_PKG.Update_Distributions(
1647 	                        P_Invoice_header_rec => l_inv_header_rec,
1648 	                        P_Calling_Mode       => P_Calling_Mode,
1649 	                        P_All_Error_Messages => P_All_Error_Messages,
1650 	                        P_Error_Code         => P_error_code,
1651 	                        P_Calling_Sequence   => l_curr_calling_sequence)) THEN
1652 
1653 	         l_return_status := FALSE;
1654 	     END IF;
1655 	  END LOOP;
1656           AP_ETAX_PKG.G_INV_ID_LIST.DELETE;
1657           AP_ETAX_PKG.G_EVNT_CLS_LIST.DELETE;
1658       END LOOP;
1659       CLOSE C_SELECTED_INVOICES;
1660 
1661       END IF;
1662 
1663       AP_ETAX_PKG.G_INV_ID_LIST.DELETE;
1664       AP_ETAX_PKG.G_EVNT_CLS_LIST.DELETE;
1665 
1666     END IF;
1667 
1668     RETURN l_return_status;
1669 
1670   EXCEPTION
1671     WHEN OTHERS THEN
1672       IF (SQLCODE <> -20001) THEN
1673         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
1674         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
1675         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
1676         FND_MESSAGE.SET_TOKEN('PARAMETERS',
1677           ' P_Invoice_Id = '||P_Invoice_Id||
1678           ' P_Calling_Mode ='||P_Calling_Mode||
1679           ' P_Error_Code = '||P_Error_Code||
1680           ' P_Calling_Sequence = '||P_Calling_Sequence);
1681         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
1682       END IF;
1683 
1684       APP_EXCEPTION.RAISE_EXCEPTION;
1685 
1686   END Distribute;
1687 
1688 /*=============================================================================
1689  |  FUNCTION - Distribute_Import()
1690  |
1691  |  DESCRIPTION
1692  |      Public function that will call the determine_recovery service for
1693  |      distribution during the import.  This API will called only in the case
1694  |      TAX-ONLY lines exist in the invoice.
1695  |      This API assumes the calling code controls the commit cycle.
1696  |      This function returns TRUE if the call to the service is successful.
1697  |      Otherwise, FALSE.
1698  |
1699  |  PARAMETERS
1700  |      P_Invoice_Id - invoice id
1701  |      P_Calling_Mode - calling mode.  Identifies which service to call
1702  |      P_All_Error_Messages - Should API return 1 error message or allow
1703  |                             calling point to get them from message stack
1704  |      P_error_code - Error code to be returned
1705  |      P_calling_sequence -  Calling sequence
1706  |
1707  |  MODIFICATION HISTORY
1708  |    DATE          Author         Action
1709  |    20-JAN-2004   SYIDNER        Created
1710  |
1711  *============================================================================*/
1712   FUNCTION Distribute_Import(
1713              P_Invoice_id              IN NUMBER,
1714              P_Calling_Mode            IN VARCHAR2,
1715              P_All_Error_Messages      IN VARCHAR2,
1716              P_Error_Code              OUT NOCOPY VARCHAR2,
1717              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN IS
1718 
1719     l_debug_info                 VARCHAR2(240);
1720     l_curr_calling_sequence      VARCHAR2(4000);
1721     l_event_class_code
1722       zx_trx_headers_gt.event_class_code%TYPE;
1723     l_event_type_code
1724       zx_trx_headers_gt.event_type_code%TYPE;
1725 
1726     l_return_status_service       VARCHAR2(4000);
1727     l_msg_count                   NUMBER;
1728     l_msg_data                    VARCHAR2(4000);
1729     l_msg                         VARCHAR2(4000);
1730     l_return_status               BOOLEAN := TRUE;
1731 
1732     l_inv_header_rec             ap_invoices_all%ROWTYPE;
1733     l_api_name                   varchar2(30) := 'Distribute_import'; -- bug 6321366
1734     CURSOR Invoice_Header IS
1735     SELECT *
1736       FROM ap_invoices_all
1737      WHERE invoice_id = P_Invoice_Id;
1738 
1739   BEGIN
1740 
1741     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Distribute_Import<-' ||
1742                                P_calling_sequence;
1743 
1744     -----------------------------------------------------------------
1745     l_debug_info := 'Step 1: Populating invoice header local record';
1746     -----------------------------------------------------------------
1747     BEGIN
1748       OPEN Invoice_Header;
1749       FETCH Invoice_Header INTO l_inv_header_rec;
1750       CLOSE Invoice_Header;
1751     END;
1752 
1753     -- There is no need to populate the header since this service is called
1754     -- after the call to calculate tax only if any tax-only line is created
1755     -- as per the eTax cookbook, in this case there is no need to populate
1756     -- the distribution global temporary table.
1757     -----------------------------------------------------------------
1758     l_debug_info := 'Step 3: Call determine_recovery service';
1759     -----------------------------------------------------------------
1760     IF (l_return_status = TRUE) THEN
1761 print(l_api_name,l_debug_info); --bug 6321366
1762 
1763       zx_api_pub.determine_recovery(
1764         p_api_version      => 1.0,
1765         p_init_msg_list    => FND_API.G_TRUE,
1766         p_commit           => FND_API.G_FALSE,
1767         p_validation_level => FND_API.G_VALID_LEVEL_FULL,
1768         x_return_status    => l_return_status_service,
1769         x_msg_count        => l_msg_count,
1770         x_msg_data         => l_msg_data);
1771 
1772     END IF;
1773 
1774     -----------------------------------------------------------------
1775     l_debug_info := 'Step 4: Verify return status for determine_recovery';
1776     -----------------------------------------------------------------
1777     IF (l_return_status_service = 'S') THEN
1778 
1779       -----------------------------------------------------------------
1780       l_debug_info := 'Step 5: Handle return of tax lines';
1781       -----------------------------------------------------------------
1782 print(l_api_name,l_debug_info); -- bug 6321366
1783 
1784        IF NOT(AP_ETAX_SERVICES_PKG.Update_AP(
1785                 P_Invoice_header_rec => l_inv_header_rec,
1786                 P_Calling_Mode       => P_Calling_Mode,
1787                 P_All_Error_Messages => P_All_Error_Messages,
1788                 P_Error_Code         => P_error_code,
1789                 P_Calling_Sequence   => l_curr_calling_sequence)) THEN
1790 
1791           l_return_status := FALSE;
1792        END IF;
1793 
1794 
1795     ELSE  -- handle errors
1796       l_return_status := FALSE;
1797       -----------------------------------------------------------------
1798       l_debug_info := 'Step 6: Handle errors returned by API';
1799       -----------------------------------------------------------------
1800 print(l_api_name,l_debug_info); --bug 6321366
1801 
1802       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
1803                P_All_Error_Messages  => P_All_Error_Messages,
1804                P_Msg_Count           => l_msg_count,
1805                P_Msg_Data            => l_msg_data,
1806                P_Error_Code          => P_Error_Code,
1807                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
1808         NULL;
1809       END IF;
1810 
1811     END IF;
1812 
1813     RETURN l_return_status;
1814 
1815   EXCEPTION
1816     WHEN OTHERS THEN
1817       IF (SQLCODE <> -20001) THEN
1818         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
1819         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
1820         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
1821         FND_MESSAGE.SET_TOKEN('PARAMETERS',
1822           ' P_Invoice_Id = '||P_Invoice_Id||
1823           ' P_Calling_Mode ='||P_Calling_Mode||
1824           ' P_Error_Code = '||P_Error_Code||
1825           ' P_Calling_Sequence = '||P_Calling_Sequence);
1826         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
1827       END IF;
1828 
1829       APP_EXCEPTION.RAISE_EXCEPTION;
1830 
1831   END Distribute_Import;
1832 
1833 /*=============================================================================
1834  |  FUNCTION - Import_Interface()
1835  |
1836  |  DESCRIPTION
1837  |      Public function that will call the import_document_with_tax service
1838  |      This API assumes the calling code controls the commit cycle.
1839  |      This function returns TRUE if the call to the service is successful.
1840  |      Otherwise, FALSE.
1841  |
1842  |  PARAMETERS
1843  |      P_Invoice_Id - invoice id
1844  |      P_Calling_Mode - calling mode.  Identifies which service to call
1845  |      P_Interface_Invoice_Id - Interface invoice id
1846  |      P_All_Error_Messages - Should API return 1 error message or allow
1847  |                             calling point to get them from message stack
1848  |      P_error_code - Error code to be returned
1849  |      P_calling_sequence -  Calling sequence
1850  |
1851  |  MODIFICATION HISTORY
1852  |    DATE          Author         Action
1853  |    07-OCT-2003   SYIDNER        Created
1854  |
1855  *============================================================================*/
1856 
1857   FUNCTION Import_Interface(
1858              P_Invoice_id              IN NUMBER,
1859              P_Calling_Mode            IN VARCHAR2,
1860              P_Interface_Invoice_Id    IN NUMBER,
1861              P_All_Error_Messages      IN VARCHAR2,
1862              P_Error_Code              OUT NOCOPY VARCHAR2,
1863              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
1864   IS
1865     l_debug_info                 VARCHAR2(240);
1866     l_curr_calling_sequence      VARCHAR2(4000);
1867 
1868     l_return_status_service       VARCHAR2(4000);
1869     l_msg_count                   NUMBER;
1870     l_msg_data                    VARCHAR2(4000);
1871     l_msg                         VARCHAR2(4000);
1872 
1873     l_event_class_code            zx_trx_headers_gt.event_class_code%TYPE;
1874     l_event_type_code             zx_trx_headers_gt.event_type_code%TYPE;
1875 
1876     l_return_status               BOOLEAN := TRUE;
1877     l_inv_header_rec             ap_invoices_all%ROWTYPE;
1878 
1879     CURSOR Invoice_Header IS
1880     SELECT *
1881       FROM ap_invoices_all
1882      WHERE invoice_id = P_Invoice_Id;
1883 
1884   BEGIN
1885     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Import_Interface<-' ||
1886                                P_calling_sequence;
1887 
1888     -----------------------------------------------------------------
1889     l_debug_info := 'Step 1: Populating invoice header local record';
1890     -----------------------------------------------------------------
1891     BEGIN
1892       OPEN Invoice_Header;
1893       FETCH Invoice_Header INTO l_inv_header_rec;
1894       CLOSE Invoice_Header;
1895     END;
1896 
1897     -----------------------------------------------------------------
1898     l_debug_info := 'Step 2: Update Header';
1899     -----------------------------------------------------------------
1900     BEGIN
1901       UPDATE zx_trx_headers_gt
1902          SET trx_id = P_Invoice_Id
1903        WHERE application_id   = AP_ETAX_PKG.AP_APPLICATION_ID
1904          AND entity_code      = AP_ETAX_PKG.AP_ENTITY_CODE
1905          AND event_class_code IN (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
1906                                   AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
1907                                   AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
1908          AND trx_id = P_Interface_Invoice_Id;
1909     EXCEPTION WHEN NO_DATA_FOUND THEN NULL;
1910     END;
1911 
1912     -----------------------------------------------------------------
1913     l_debug_info := 'Step 3: Update trx_id in Lines';
1914     -----------------------------------------------------------------
1915     BEGIN
1916       UPDATE zx_transaction_lines_gt
1917          SET trx_id = P_Invoice_Id
1918        WHERE trx_id = P_Interface_Invoice_Id;
1919     EXCEPTION WHEN NO_DATA_FOUND THEN NULL;
1920     END;
1921 
1922 
1923     -----------------------------------------------------------------
1924     l_debug_info := 'Step 4: Update trx_id in tax Lines';
1925     -----------------------------------------------------------------
1926     BEGIN
1927       UPDATE zx_import_tax_lines_gt
1928          SET trx_id = P_Invoice_Id
1929        WHERE trx_id = P_Interface_Invoice_Id;
1930     EXCEPTION WHEN NO_DATA_FOUND THEN NULL;
1931     END;
1932 
1933 
1934     -----------------------------------------------------------------
1935     l_debug_info := 'Step 5: Update trx_id in the allocation structure '||
1936                     'etax table';
1937     -----------------------------------------------------------------
1938     BEGIN
1939       UPDATE zx_trx_tax_link_gt
1940          SET trx_id = P_Invoice_Id
1941        WHERE trx_id = P_Interface_Invoice_Id;
1942     EXCEPTION WHEN NO_DATA_FOUND THEN NULL;
1943     END;
1944 
1945     -------------------------------------------------------------------
1946     l_debug_info := 'Step 6: Get event class code';
1947     -------------------------------------------------------------------
1948 
1949     IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
1950       P_Invoice_Type_Lookup_Code => l_inv_header_rec.invoice_type_lookup_code,
1951       P_Event_Class_Code         => l_event_class_code,
1952       P_error_code               => p_error_code,
1953       P_calling_sequence         => l_curr_calling_sequence)) THEN
1954 
1955       l_return_status := FALSE;
1956 
1957     END IF;
1958 
1959     -------------------------------------------------------------------
1960     l_debug_info := 'Step 7: Get event type code';
1961     -------------------------------------------------------------------
1962 
1963     IF (l_return_status = TRUE) THEN
1964       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
1965         P_Event_Class_Code          => l_event_class_code,
1966         P_Calling_Mode              => P_Calling_Mode,
1967         P_eTax_Already_called_flag  => NULL,
1968         P_Event_Type_Code           => l_event_type_Code,
1969         P_Error_Code                => P_error_code,
1970         P_Calling_Sequence          => l_curr_calling_sequence)) THEN
1971 
1972         l_return_status := FALSE;
1973       END IF;
1974     END IF;
1975 
1976     -----------------------------------------------------------------
1977     l_debug_info := 'Step 9: Call import_document_with_tax service';
1978     -----------------------------------------------------------------
1979     IF ( l_return_status = TRUE ) THEN
1980 
1981       zx_api_pub.import_document_with_tax(
1982         p_api_version      => 1.0,
1983         p_init_msg_list    => FND_API.G_TRUE,
1984         p_commit           => FND_API.G_FALSE,
1985         p_validation_level => FND_API.G_VALID_LEVEL_FULL,
1986         x_return_status    => l_return_status_service,
1987         x_msg_count        => l_msg_count,
1988         x_msg_data         => l_msg_data);
1989 
1990     END IF;
1991 
1992     IF (l_return_status_service = 'S') THEN
1993 
1994       -----------------------------------------------------------------
1995       l_debug_info := 'Step 10: Handle return of tax lines';
1996       -----------------------------------------------------------------
1997        IF NOT(AP_ETAX_SERVICES_PKG.Update_AP(
1998                 P_Invoice_header_rec => l_inv_header_rec,
1999                 P_Calling_Mode       => P_Calling_Mode,
2000                 P_All_Error_Messages => P_All_Error_Messages,
2001                 P_Error_Code         => P_error_code,
2002                 P_Calling_Sequence   => l_curr_calling_sequence)) THEN
2003 
2004           l_return_status := FALSE;
2005        END IF;
2006 
2007     ELSE  -- handle errors
2008 
2009       -----------------------------------------------------------------
2010       l_debug_info := 'Step 11: Handle errors returned by API';
2011       -----------------------------------------------------------------
2012       l_return_status := FALSE;
2013 
2014       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
2015                P_All_Error_Messages  => P_All_Error_Messages,
2016                P_Msg_Count           => l_msg_count,
2017                P_Msg_Data            => l_msg_data,
2018                P_Error_Code          => P_Error_Code,
2019                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
2020         NULL;
2021       END IF;
2022 
2023     END IF;
2024 
2025    RETURN l_return_status;
2026 
2027   EXCEPTION
2028     WHEN OTHERS THEN
2029       IF (SQLCODE <> -20001) THEN
2030         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
2031         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
2032         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
2033         FND_MESSAGE.SET_TOKEN('PARAMETERS',
2034           ' P_Invoice_Id = '||P_Invoice_Id||
2035           ' P_Calling_Mode ='||P_Calling_Mode||
2036           ' P_Error_Code = '||P_Error_Code||
2037           ' P_Calling_Sequence = '||P_Calling_Sequence);
2038         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
2039       END IF;
2040 
2041       APP_EXCEPTION.RAISE_EXCEPTION;
2042   END Import_Interface;
2043 
2044 /*=============================================================================
2045  |  FUNCTION - Reverse_Invoice()
2046  |
2047  |  DESCRIPTION
2048  |      Public function that will call the reverse_document_distribution
2049  |      service for quick credit (full reversal.)
2050  |      This API assumes the calling code controls the commit cycle.
2051  |      This function returns TRUE if the call to the service is successful.
2052  |      Otherwise, FALSE.
2053  |
2054  |  PARAMETERS
2055  |      P_Invoice_Id - invoice id
2056  |      P_Calling_Mode - calling mode.  Identifies which service to call
2057  |      P_All_Error_Messages - Should API return 1 error message or allow
2058  |                             calling point to get them from message stack
2059  |      P_error_code - Error code to be returned
2060  |      P_calling_sequence -  Calling sequence
2061  |
2062  |  MODIFICATION HISTORY
2063  |    DATE          Author         Action
2064  |    07-OCT-2003   SYIDNER        Created
2065  |
2066  *============================================================================*/
2067 
2068   FUNCTION Reverse_Invoice(
2069              P_Invoice_id              IN NUMBER,
2070              P_Calling_Mode            IN VARCHAR2,
2071              P_All_Error_Messages      IN VARCHAR2,
2072              P_Error_Code              OUT NOCOPY VARCHAR2,
2073              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
2074   IS
2075 
2076     l_debug_info                 VARCHAR2(240);
2077     l_curr_calling_sequence      VARCHAR2(4000);
2078 
2079     l_inv_header_rec             ap_invoices_all%ROWTYPE;
2080     l_credited_inv_rec           ap_invoices_all%ROWTYPE;
2081 
2082     l_event_class_code_crediting zx_trx_headers_gt.event_class_code%TYPE;
2083     l_event_class_code_credited  zx_trx_headers_gt.event_class_code%TYPE;
2084 
2085     l_tax_already_distributed     VARCHAR2(1);
2086     l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
2087     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
2088 
2089     CURSOR Crediting_Inv_Header IS
2090     SELECT *
2091       FROM ap_invoices_all
2092      WHERE invoice_id = P_Invoice_Id
2093        AND quick_credit = 'Y'
2094        AND credited_invoice_id IS NOT NULL;
2095 
2096     CURSOR Credited_Inv_Header(c_credited_inv NUMBER) IS
2097     SELECT *
2098       FROM ap_invoices_all
2099      WHERE invoice_id = c_credited_inv;
2100 
2101     l_return_status_service       VARCHAR2(4000);
2102     l_msg_count                   NUMBER;
2103     l_msg_data                    VARCHAR2(4000);
2104     l_msg                         VARCHAR2(4000);
2105 
2106     l_return_status               BOOLEAN := TRUE;
2107 
2108     l_api_name                    CONSTANT VARCHAR2(100) := 'Reverse_Invoice';
2109 
2110   BEGIN
2111     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Reverse_Invoice<-' ||
2112                                P_calling_sequence;
2113 
2114     -----------------------------------------------------------------
2115     l_debug_info := 'Step 1: Populating crediting invoice header';
2116     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2117       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2118     END IF;
2119     -----------------------------------------------------------------
2120     BEGIN
2121       OPEN Crediting_Inv_Header;
2122       FETCH Crediting_Inv_Header INTO l_inv_header_rec;
2123       CLOSE Crediting_Inv_Header;
2124     END;
2125 
2126     -----------------------------------------------------------------
2127     l_debug_info := 'Step 2: Populating credited invoice header';
2128     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2129       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2130     END IF;
2131     -----------------------------------------------------------------
2132     BEGIN
2133       OPEN Credited_Inv_Header(l_inv_header_rec.credited_invoice_id);
2134       FETCH Credited_Inv_Header INTO l_credited_inv_rec;
2135       CLOSE Credited_Inv_Header;
2136     END;
2137 
2138     IF NOT tax_distributions_exist
2139 		(p_invoice_id  => l_credited_inv_rec.invoice_id) THEN
2140 
2141        RETURN l_return_status;
2142 
2143     END IF;
2144 
2145     -------------------------------------------------------------------
2146     l_debug_info := 'Step 3: Get crediting invoice event class code';
2147     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2148       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2149     END IF;
2150     -------------------------------------------------------------------
2151 
2152     IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
2153       P_Invoice_Type_Lookup_Code => l_inv_header_rec.invoice_type_lookup_code,
2154       P_Event_Class_Code         => l_event_class_code_crediting,
2155       P_error_code               => P_error_code,
2156       P_calling_sequence         => l_curr_calling_sequence)) THEN
2157 
2158       l_return_status := FALSE;
2159 
2160     END IF;
2161 
2162     -------------------------------------------------------------------
2163     l_debug_info := 'Step 4: Get credited invoice event class code';
2164     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2165       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2166     END IF;
2167     -------------------------------------------------------------------
2168     IF (l_return_status = TRUE) THEN
2169       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
2170         P_Invoice_Type_Lookup_Code => l_credited_inv_rec.invoice_type_lookup_code,
2171         P_Event_Class_Code         => l_event_class_code_credited,
2172         P_error_code               => P_error_code,
2173         P_calling_sequence         => l_curr_calling_sequence)) THEN
2174 
2175         l_return_status := FALSE;
2176       END IF;
2177     END IF;
2178 
2179     INSERT INTO zx_rev_trx_headers_gt(
2180         internal_organization_id,
2181         reversing_appln_id,
2182         reversing_entity_code,
2183         reversing_evnt_cls_code,
2184         reversing_trx_id,
2185         legal_entity_id,
2186         trx_number
2187     ) VALUES
2188 	(l_inv_header_rec.org_id,
2189          200,
2190          'AP_INVOICES',
2191          l_event_class_code_credited,
2192          l_inv_header_rec.invoice_id,
2193          l_inv_header_rec.legal_entity_id,
2194          l_inv_header_rec.invoice_num);
2195 
2196     -----------------------------------------------------------------
2197     l_debug_info := 'Step 5: Populate zx_reverse_trx_lines_gt';
2198     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2199       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2200     END IF;
2201     -----------------------------------------------------------------
2202     IF (l_return_status = TRUE) THEN
2203       INSERT INTO zx_reverse_trx_lines_gt(
2204         internal_organization_id,
2205         reversing_appln_id,
2206         reversing_entity_code,
2207         reversing_evnt_cls_code,
2208         reversing_trx_id,
2209         reversing_trx_level_type,
2210         reversing_trx_line_id,
2211         reversed_appln_id,
2212         reversed_entity_code,
2213         reversed_evnt_cls_code,
2214         reversed_trx_id,
2215         reversed_trx_level_type,
2216         reversed_trx_line_id
2217       )
2218       SELECT
2219         l_inv_header_rec.org_id,        -- internal_organization_id
2220         200,                            -- reversing_appln_id
2221         'AP_INVOICES',                  -- reversing_entity_code
2222         l_event_class_code_crediting,   -- reversing_evnt_cls_code
2223         ail.invoice_id,                 -- reversing_trx_id
2224         'LINE',                         -- reversing_trx_level_type
2225         ail.line_number,                -- reversing_trx_line_id
2226         200,                            -- reversed_appln_id
2227         'AP_INVOICES',                  -- reversed_entity_code
2228         l_event_class_code_credited,    -- reversed_evnt_cls_code
2229         aic.invoice_id,                 -- reversed_trx_id
2230         'LINE',                         -- reversed_trx_level_type
2231         aic.line_number                 -- reversed_trx_line_id
2232         FROM ap_invoice_lines_all ail,
2233              ap_invoice_lines_all aic
2234        WHERE ail.invoice_id = l_inv_header_rec.invoice_id
2235          AND ail.line_type_lookup_code NOT IN ('TAX', 'AWT')
2236          AND ail.corrected_inv_id = aic.invoice_id
2237          AND ail.corrected_line_number = aic.line_number;
2238 
2239 
2240     END IF;
2241 
2242     -----------------------------------------------------------------
2243     l_debug_info := 'Step 6: Populate zx_reverse_dist_gt';
2244     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2245       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2246     END IF;
2247     -----------------------------------------------------------------
2248     IF (l_return_status = TRUE) THEN
2249       INSERT INTO zx_reverse_dist_gt(
2250         internal_organization_id,
2251         reversing_appln_id,
2252         reversing_entity_code,
2253         reversing_evnt_cls_code,
2254         reversing_trx_id,
2255         reversing_trx_level_type,
2256         reversing_trx_line_id,
2257         reversing_trx_line_dist_id,
2258         reversing_tax_line_id,
2259         reversed_appln_id,
2260         reversed_entity_code,
2261         reversed_evnt_cls_code,
2262         reversed_trx_id,
2263         reversed_trx_level_type,
2264         reversed_trx_line_id,
2265         reversed_trx_line_dist_id,
2266         reversed_tax_line_id
2267       )
2268       SELECT
2269         l_inv_header_rec.org_id,        -- internal_organization_id
2270         200,                            -- reversing_appln_id
2271         'AP_INVOICES',                  -- reversing_entity_code
2272         l_event_class_code_crediting,   -- reversing_evnt_cls_code
2273         aid.invoice_id,                 -- reversing_trx_id
2274         'LINE',                         -- reversing_trx_level_type
2275         aid.invoice_line_number,        -- reversing_trx_line_id
2276         aid.invoice_distribution_id,    -- reversing_trx_line_dist_id
2277         NULL,                           -- reversing_tax_line_id
2278         200,                            -- reversed_appln_id
2279         'AP_INVOICES',                  -- reversed_entity_code
2280         l_event_class_code_credited,    -- reversed_evnt_cls_code
2281         idc.invoice_id,                 -- reversed_trx_id
2282         'LINE',                         -- reversed_trx_level_type
2283         idc.invoice_line_number,        -- reversed_trx_line_id
2284         idc.invoice_distribution_id,    -- reversed_trx_line_dist_id
2285         NULL                            -- reversed_tax_line_id
2286         FROM ap_invoice_distributions_all aid,
2287              ap_invoice_distributions_all idc
2288        WHERE aid.invoice_id = l_inv_header_rec.invoice_id
2289          AND aid.line_type_lookup_code NOT IN
2290              ('AWT', 'REC_TAX', 'NONREC_TAX', 'TRV', 'TERV', 'TIPV')
2291          AND (aid.related_id IS NULL
2292               OR aid.related_id = aid.invoice_distribution_id)
2293          AND aid.corrected_invoice_dist_id = idc.invoice_distribution_id;
2294 
2295        -- this select make sure that only the primary distribution is populated
2296        -- in the eTax temporary table.  There is no need to summary because the
2297        -- amount is not included.  eTax will take the amount from the reversed
2298        -- tax distributions.
2299 
2300     END IF;
2301 
2302     -----------------------------------------------------------------
2303     l_debug_info := 'Step 7: Call reverse_document_distribution service';
2304     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2305       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2306     END IF;
2307     -----------------------------------------------------------------
2308     IF ( l_return_status = TRUE ) THEN
2309 
2310       zx_api_pub.reverse_document_distribution(
2311         p_api_version      => 1.0,
2312         p_init_msg_list    => FND_API.G_TRUE,
2313         p_commit           => FND_API.G_FALSE,
2314         p_validation_level => FND_API.G_VALID_LEVEL_FULL,
2315         x_return_status    => l_return_status_service,
2316         x_msg_count        => l_msg_count,
2317         x_msg_data         => l_msg_data);
2318 
2319     END IF;
2320 
2321     -----------------------------------------------------------------
2322     l_debug_info := 'Step 8: Verify return status';
2323     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2324       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2325     END IF;
2326     -----------------------------------------------------------------
2327     IF (l_return_status_service = 'S') THEN
2328        -----------------------------------------------------------------
2329        l_debug_info := 'Step 9: Handle return of tax lines and dist';
2330        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2331            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2332        END IF;
2333        -----------------------------------------------------------------
2334 
2335        -- Tax distributions insert works off of zx_trx_headers_gt. Since
2336        -- reverse_document_distribution does not require this table to be
2337        -- populated, we are using this as a proxy to avoid maintaining two
2338        -- code lines.
2339        -------------------------------------------------------------------
2340        l_debug_info := 'Is tax already distributed for invoice';
2341        Print(l_api_name,l_debug_info);
2342        -------------------------------------------------------------------
2343        IF (AP_ETAX_UTILITY_PKG.Is_Tax_Already_Dist_Inv(
2344                   P_Invoice_Id           => p_invoice_id,
2345                   P_Calling_Sequence     => l_curr_calling_sequence)) THEN
2346 
2347            l_tax_already_distributed := 'Y';
2348        ELSE
2349            l_tax_already_distributed := 'N';
2350        END IF;
2351 
2352        -----------------------------------------------------------------
2353        l_debug_info := 'Populate Header';
2354        Print(l_api_name,l_debug_info);
2355        -----------------------------------------------------------------
2356        IF NOT(AP_ETAX_SERVICES_PKG.Populate_Headers_GT(
2357                       P_Invoice_Header_Rec        => l_inv_header_rec,
2358                       P_Calling_Mode              => P_Calling_Mode,
2359                       P_eTax_Already_called_flag  => l_tax_already_distributed,
2360                       P_Event_Class_Code          => l_event_class_code,
2361                       P_Event_Type_Code           => l_event_type_code,
2362                       P_Error_Code                => P_error_code,
2363                       P_Calling_Sequence          => l_curr_calling_sequence )) THEN
2364 
2365            l_return_status :=  FALSE;
2366        END IF;
2367 
2368        IF NOT(AP_ETAX_SERVICES_PKG.Update_AP(
2369                 P_Invoice_header_rec => l_inv_header_rec,
2370                 P_Calling_Mode       => P_Calling_Mode,
2371                 P_All_Error_Messages => P_All_Error_Messages,
2372                 P_Error_Code         => P_error_code,
2373                 P_Calling_Sequence   => l_curr_calling_sequence)) THEN
2374 
2375           l_return_status := FALSE;
2376        END IF;
2377 
2378    ELSE  -- handle errors
2379 
2380       l_return_status := FALSE;
2381       -----------------------------------------------------------------
2382       l_debug_info := 'Step 10: Handle errors returned by API';
2383       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2384         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2385       END IF;
2386       -----------------------------------------------------------------
2387       l_return_status := FALSE;
2388 
2389       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
2390                P_All_Error_Messages  => P_All_Error_Messages,
2391                P_Msg_Count           => l_msg_count,
2392                P_Msg_Data            => l_msg_data,
2393                P_Error_Code          => P_Error_Code,
2394                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
2395         NULL;
2396       END IF;
2397 
2398     END IF;
2399 
2400 
2401    RETURN l_return_status;
2402 
2403   EXCEPTION
2404     WHEN OTHERS THEN
2405       IF (SQLCODE <> -20001) THEN
2406         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
2407         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
2408         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
2409         FND_MESSAGE.SET_TOKEN('PARAMETERS',
2410           ' P_Invoice_Id = '||P_Invoice_Id||
2411           ' P_Calling_Mode ='||P_Calling_Mode||
2412           ' P_Error_Code = '||P_Error_Code||
2413           ' P_Calling_Sequence = '||P_Calling_Sequence);
2414         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
2415       END IF;
2416 
2417       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2418         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,sqlerrm);
2419       END IF;
2420 
2421       APP_EXCEPTION.RAISE_EXCEPTION;
2422 
2423   END Reverse_Invoice;
2424 
2425 /*=============================================================================
2426  |  FUNCTION - Override_Tax()
2427  |
2428  |  DESCRIPTION
2429  |      Public function that will call the override_tax service.
2430  |      This API assumes the calling code controls the commit cycle.
2431  |      This function returns TRUE if the call to the service is successful.
2432  |      Otherwise, FALSE.
2433  |
2434  |  PARAMETERS
2435  |      P_Invoice_Id         - invoice id
2436  |      P_Calling_Mode       - calling mode.  Identifies which service to call
2437  |      P_Override_Status    - override_status parameter returned by the eTax
2438  |                             UI (Tax lines and summary lines window).
2439  |      P_Event_id	     - Indicates a specific instance of the override event.
2440  |                             Tax line windows will return an event_id when there
2441  |                             are any user overrides.
2442  |      P_All_Error_Messages - Should API return 1 error message or allow
2443  |                             calling point to get them from message stack
2444  |      P_error_code         - Error code to be returned
2445  |      P_calling_sequence   -  Calling sequence
2446  |
2447  |  MODIFICATION HISTORY
2448  |    DATE          Author         Action
2449  |    07-OCT-2003   SYIDNER        Created
2450  |
2451  *============================================================================*/
2452 
2453   FUNCTION Override_Tax(
2454              P_Invoice_id              IN NUMBER,
2455              P_Calling_Mode            IN VARCHAR2,
2456              P_Override_Status         IN VARCHAR2,
2457 	     P_Event_Id		       IN NUMBER,
2458              P_All_Error_Messages      IN VARCHAR2,
2459              P_Error_Code              OUT NOCOPY VARCHAR2,
2460              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
2461   IS
2462 
2463     l_debug_info                 VARCHAR2(240);
2464     l_curr_calling_sequence      VARCHAR2(4000);
2465 
2466     l_inv_header_rec             ap_invoices_all%ROWTYPE;
2467     l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
2468     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
2469     l_transaction_rec		 zx_api_pub.transaction_rec_type;
2470 
2471     CURSOR Invoice_Header IS
2472     SELECT *
2473       FROM ap_invoices_all
2474      WHERE invoice_id = P_Invoice_Id;
2475 
2476     CURSOR Invoice_Lines IS
2477     SELECT *
2478       FROM ap_invoice_lines_all
2479      WHERE invoice_id = P_Invoice_Id
2480        AND line_type_lookup_code NOT IN ('TAX', 'AWT');
2481 
2482     l_return_status_service       VARCHAR2(4000);
2483     l_msg_count                   NUMBER;
2484     l_msg_data                    VARCHAR2(4000);
2485     l_msg                         VARCHAR2(4000);
2486 
2487     l_return_status               BOOLEAN := TRUE;
2488     l_success			  BOOLEAN := TRUE;
2489 
2490     l_api_name			  CONSTANT VARCHAR2(100) := 'Override_Tax';
2491 
2492   BEGIN
2493     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Override_Tax<-' ||
2494                                P_calling_sequence;
2495 
2496     IF (P_Override_Status = 'SYNCHRONIZE') THEN
2497       -----------------------------------------------------------------
2498       l_debug_info := 'Step 1: Update ap_invoice_lines_all from eTax '||
2499                       'repository';
2500       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2501           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2502       END IF;
2503       -----------------------------------------------------------------
2504       BEGIN
2505         UPDATE ap_invoice_lines_all ail
2506            SET
2507          (ail.description,
2508           ail.cancelled_flag,
2509           ail.last_updated_by,
2510           ail.last_update_login,
2511           ail.last_update_date,
2512           ail.attribute_category,
2513           ail.attribute1,
2514           ail.attribute2,
2515           ail.attribute3,
2516           ail.attribute4,
2517           ail.attribute5,
2518           ail.attribute6,
2519           ail.attribute7,
2520           ail.attribute8,
2521           ail.attribute9,
2522           ail.attribute10,
2523           ail.attribute11,
2524           ail.attribute12,
2525           ail.attribute13,
2526           ail.attribute14,
2527           ail.attribute15,
2528           ail.global_attribute_category,
2529           ail.global_attribute1,
2530           ail.global_attribute2,
2531           ail.global_attribute3,
2532           ail.global_attribute4,
2533           ail.global_attribute5,
2534           ail.global_attribute6,
2535           ail.global_attribute7,
2536           ail.global_attribute8,
2537           ail.global_attribute9,
2538           ail.global_attribute10,
2539           ail.global_attribute11,
2540           ail.global_attribute12,
2541           ail.global_attribute13,
2542           ail.global_attribute14,
2543           ail.global_attribute15,
2544           ail.global_attribute16,
2545           ail.global_attribute17,
2546           ail.global_attribute18,
2547           ail.global_attribute19,
2548           ail.global_attribute20 ) = (
2549           SELECT
2550           zls.tax_regime_code||' - '||zls.tax, -- description
2551           zls.cancel_flag,                     -- cancelled_flag
2552           l_user_id,                           -- last_updated_by
2553           l_login_id,                          -- last_update_login
2554           l_sysdate,                           -- last_update_date
2555           zls.attribute_category,
2556           zls.attribute1,
2557           zls.attribute2,
2558           zls.attribute3,
2559           zls.attribute4,
2560           zls.attribute5,
2561           zls.attribute6,
2562           zls.attribute7,
2563           zls.attribute8,
2564           zls.attribute9,
2565           zls.attribute10,
2566           zls.attribute11,
2567           zls.attribute12,
2568           zls.attribute13,
2569           zls.attribute14,
2570           zls.attribute15,
2571           zls.global_attribute_category,
2572           zls.global_attribute1,
2573           zls.global_attribute2,
2574           zls.global_attribute3,
2575           zls.global_attribute4,
2576           zls.global_attribute5,
2577           zls.global_attribute6,
2578           zls.global_attribute7,
2579           zls.global_attribute8,
2580           zls.global_attribute9,
2581           zls.global_attribute10,
2582           zls.global_attribute11,
2583           zls.global_attribute12,
2584           zls.global_attribute13,
2585           zls.global_attribute14,
2586           zls.global_attribute15,
2587           zls.global_attribute16,
2588           zls.global_attribute17,
2589           zls.global_attribute18,
2590           zls.global_attribute19,
2591           zls.global_attribute20
2592           FROM zx_lines_summary zls
2593          WHERE zls.summary_tax_line_id = ail.summary_tax_line_id
2594            AND nvl(zls.reporting_only_flag, 'N') = 'N'
2595          )
2596          WHERE ail.invoice_id = P_Invoice_Id
2597            AND ail.line_type_lookup_code = 'TAX'
2598            AND EXISTS (SELECT ls.summary_tax_line_id
2599                          FROM zx_lines_summary ls
2600                         WHERE ls.summary_tax_line_id = ail.summary_tax_line_id
2601                           AND ls.trx_id = ail.invoice_id
2602                           AND NVL(ls.tax_amt_included_flag, 'N') = 'N'
2603                           AND NVL(ls.self_assessed_flag, 'N') = 'N'
2604                           AND nvl(ls.reporting_only_flag, 'N') = 'N');
2605       EXCEPTION
2606         WHEN NO_DATA_FOUND THEN NULL;
2607 
2608         WHEN OTHERS THEN
2609           IF (SQLCODE <> -20001) THEN
2610             FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
2611             FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
2612             FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
2613             FND_MESSAGE.SET_TOKEN('PARAMETERS',
2614               ' P_Invoice_Id = '||P_Invoice_Id||
2615               ' P_Error_Code = '||P_Error_Code||
2616               ' P_Calling_Sequence = '||P_Calling_Sequence);
2617             FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
2618           END IF;
2619 
2620         APP_EXCEPTION.RAISE_EXCEPTION;
2621       END;
2622 
2623     ELSIF (P_Override_Status IN ('DETAIL_OVERRIDE', 'SUMMARY_OVERRIDE')) THEN
2624       -----------------------------------------------------------------
2625       l_debug_info := 'Step 2: Populating invoice header local record';
2626       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2627           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2628       END IF;
2629       -----------------------------------------------------------------
2630 
2631       BEGIN
2632         OPEN Invoice_Header;
2633         FETCH Invoice_Header INTO l_inv_header_rec;
2634         CLOSE Invoice_Header;
2635       END;
2636 
2637       -----------------------------------------------------------------
2638       l_debug_info := 'Step 3: Populating invoice lines collection';
2639       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2640           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2641       END IF;
2642       -----------------------------------------------------------------
2643 
2644       BEGIN
2645         OPEN Invoice_Lines;
2646         FETCH Invoice_Lines
2647         BULK COLLECT INTO l_inv_line_list;
2648         CLOSE Invoice_Lines;
2649       END;
2650 
2651       -----------------------------------------------------------------
2652       l_debug_info := 'Step 4: Populate Header';
2653       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2654           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2655       END IF;
2656       -----------------------------------------------------------------
2657 
2658       IF NOT(AP_ETAX_SERVICES_PKG.Populate_Headers_GT(
2659         P_Invoice_Header_Rec         => l_inv_header_rec,
2660         P_Calling_Mode               => P_Calling_Mode,
2661         P_eTax_Already_called_flag   => NULL,
2662         P_Event_Class_Code           => l_event_class_code,
2663         P_Event_Type_Code            => l_event_type_code,
2664         P_Error_Code                 => P_error_code,
2665         P_Calling_Sequence           => l_curr_calling_sequence )) THEN
2666 
2667         l_return_status := FALSE;
2668       END IF;
2669 
2670       -----------------------------------------------------------------
2671       l_debug_info := 'Purge staging table, Clear/Load Cache';
2672       Print(l_api_name,l_debug_info);
2673       -----------------------------------------------------------------
2674       DELETE FROM ZX_TRANSACTION_LINES_GT;
2675 
2676       AP_ETAX_SERVICES_PKG.G_SITE_ATTRIBUTES.DELETE;
2677       AP_ETAX_SERVICES_PKG.G_ORG_ATTRIBUTES.DELETE;
2678 
2679              IF l_inv_header_rec.invoice_type_lookup_code = 'PAYMENT REQUEST' THEN  /* if
2680                   condition for bug 5967914 as we need tp pass party_site_id instead of
2681                   vendor_site_id if invoice_type_lookup_code ='PAYMENT REQUEST' */
2682                  l_payment_request_flag :='Y';  -- for bug 5967914
2683 	         Cache_Line_Defaults
2684 	               ( p_org_id           => l_inv_header_rec.org_id
2685 	                ,p_vendor_site_id   => l_inv_header_rec.party_site_id
2686 	                ,p_calling_sequence => l_curr_calling_sequence);
2687              ELSE
2688                  l_payment_request_flag :='N';  -- for bug 5967914
2689                	  Cache_Line_Defaults
2690 	               ( p_org_id           => l_inv_header_rec.org_id
2691 	                ,p_vendor_site_id   => l_inv_header_rec.vendor_site_id
2692 	                ,p_calling_sequence => l_curr_calling_sequence);
2693              END IF;
2694 
2695       -----------------------------------------------------------------
2696       l_debug_info := 'Step 5: Populate Lines';
2697       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2698           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2699       END IF;
2700       -----------------------------------------------------------------
2701       IF ( l_return_status = TRUE ) THEN
2702         IF NOT(AP_ETAX_SERVICES_PKG.Populate_Lines_GT(
2703           P_Invoice_Header_Rec      => l_inv_header_rec,
2704           P_Calling_Mode            => P_Calling_Mode,
2705           P_Event_Class_Code        => l_event_class_code,
2706           P_Error_Code              => P_error_code,
2707           P_Calling_Sequence        => l_curr_calling_sequence )) THEN
2708 
2709           l_return_status := FALSE;
2710         END IF;
2711       END IF;
2712 
2713       l_transaction_rec.internal_organization_id := l_inv_header_rec.org_id;
2714       l_transaction_rec.application_id		 := 200;
2715       l_transaction_rec.entity_code		 := 'AP_INVOICES';
2716       l_transaction_rec.event_class_code	 := l_event_class_code;
2717       l_transaction_rec.event_type_code		 := l_event_type_code;
2718       l_transaction_rec.trx_id			 := l_inv_header_rec.invoice_id;
2719 
2720       -----------------------------------------------------------------
2721       l_debug_info := 'Step 6: Call override_tax service';
2722       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2723           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2724       END IF;
2725       -----------------------------------------------------------------
2726       IF ( l_return_status = TRUE ) THEN
2727 
2728         zx_api_pub.override_tax(
2729           p_api_version      => 1.0,
2730           p_init_msg_list    => FND_API.G_TRUE,
2731           p_commit           => FND_API.G_FALSE,
2732           p_validation_level => FND_API.G_VALID_LEVEL_FULL,
2733           p_override_level   => P_Override_Status,
2734 	  p_transaction_rec  => l_transaction_rec,
2735 	  p_event_id	     => p_event_id,
2736           x_return_status    => l_return_status_service,
2737           x_msg_count        => l_msg_count,
2738           x_msg_data         => l_msg_data);
2739 
2740       END IF;
2741 
2742       -----------------------------------------------------------------
2743       l_debug_info := 'Step 7: Verify return status';
2744       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2745           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2746       END IF;
2747       -----------------------------------------------------------------
2748 
2749       IF (l_return_status_service = 'S') THEN
2750 
2751         -----------------------------------------------------------------
2752         l_debug_info := 'Step 8: Handle return of tax lines';
2753         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2754             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2755         END IF;
2756         -----------------------------------------------------------------
2757          IF NOT(AP_ETAX_SERVICES_PKG.Update_AP(
2758                   P_Invoice_header_rec => l_inv_header_rec,
2759                   P_Calling_Mode       => P_Calling_Mode,
2760                 P_All_Error_Messages   => P_All_Error_Messages,
2761                   P_Error_Code         => P_error_code,
2762                   P_Calling_Sequence   => l_curr_calling_sequence)) THEN
2763 
2764             l_return_status := FALSE;
2765          END IF;
2766 
2767       ELSE  -- handle errors
2768         l_return_status := FALSE;
2769         -----------------------------------------------------------------
2770         l_debug_info := 'Step 9: Handle errors returned by API';
2771         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2772             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2773         END IF;
2774         -----------------------------------------------------------------
2775 
2776         IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
2777                  P_All_Error_Messages  => P_All_Error_Messages,
2778                  P_Msg_Count           => l_msg_count,
2779                  P_Msg_Data            => l_msg_data,
2780                  P_Error_Code          => P_Error_Code,
2781                  P_Calling_Sequence    => l_curr_calling_sequence)) THEN
2782           NULL;
2783         END IF;
2784       END IF; -- end of return_status_service
2785 
2786       -----------------------------------------------------------------
2787       l_debug_info := 'Step 10: Call Freeze Distributions';
2788       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2789           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2790       END IF;
2791       -----------------------------------------------------------------
2792       --Bug7592845
2793       IF NOT(AP_ETAX_SERVICES_PKG.Freeze_itm_Distributions(
2794 	                P_Invoice_Header_Rec  => l_inv_header_rec,
2795 	                P_Calling_Mode        => 'FREEZE DISTRIBUTIONS',
2796 	                P_Event_Class_Code    => l_event_class_code,
2797 	                P_All_Error_Messages  => P_All_Error_Messages,
2798 	                P_Error_Code          => P_error_code,
2799       	            P_Calling_Sequence    => l_curr_calling_sequence)) THEN
2800 
2801              l_return_status := FALSE;
2802 
2803       END IF;
2804 
2805       -----------------------------------------------------------------
2806       l_debug_info := 'Step 11: Call Distribute';
2807       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2808           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2809       END IF;
2810       -----------------------------------------------------------------
2811       l_success := ap_etax_pkg.calling_etax(
2812 			P_Invoice_id         => l_inv_header_rec.invoice_id,
2813 			P_Calling_Mode       => 'DISTRIBUTE',
2814 			P_All_Error_Messages => P_All_Error_Messages,
2815 			P_error_code         => P_error_code,
2816 			P_Calling_Sequence   => l_curr_calling_sequence);
2817 
2818       IF (not l_success) THEN
2819           l_return_status := FALSE;
2820       END IF;
2821 
2822       -----------------------------------------------------------------
2823       l_debug_info := 'Step 12: Update Total Tax Amount';
2824       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2825           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2826       END IF;
2827       -----------------------------------------------------------------
2828       UPDATE ap_invoices_all ai
2829          SET (ai.total_tax_amount,
2830               ai.self_assessed_tax_amount) =
2831              (SELECT SUM(DECODE(NVL(zls.self_assessed_flag, 'N'),
2832                                 'N', NVL(zls.tax_amt, 0),
2833                                 0)),
2834                      SUM(DECODE(NVL(zls.self_assessed_flag, 'N'),
2835                                 'Y', NVL(zls.tax_amt, 0),
2836                                  0))
2837                 FROM zx_lines_summary zls
2838                WHERE application_id   = AP_ETAX_PKG.AP_APPLICATION_ID
2839                  AND entity_code      = AP_ETAX_PKG.AP_ENTITY_CODE
2840                  AND event_class_code IN (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
2841                                           AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
2842                                           AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
2843                  AND zls.trx_id       = ai.invoice_id
2844                  AND NVL(zls.reporting_only_flag, 'N') = 'N')
2845        WHERE ai.invoice_id = l_inv_header_rec.invoice_id;
2846 
2847    END IF;  -- end of p_override_status
2848 
2849    RETURN l_return_status;
2850 
2851   EXCEPTION
2852     WHEN OTHERS THEN
2853       IF (SQLCODE <> -20001) THEN
2854         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
2855         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
2856         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
2857         FND_MESSAGE.SET_TOKEN('PARAMETERS',
2858           ' P_Invoice_Id = '||P_Invoice_Id||
2859           ' P_Calling_Mode ='||P_Calling_Mode||
2860           ' P_Error_Code = '||P_Error_Code||
2861           ' P_Calling_Sequence = '||P_Calling_Sequence);
2862         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
2863       END IF;
2864 
2865       APP_EXCEPTION.RAISE_EXCEPTION;
2866 
2867   END Override_Tax;
2868 
2869 
2870 /*=============================================================================
2871  |  FUNCTION - Override_Recovery()
2872  |
2873  |  DESCRIPTION
2874  |      Public function that will call the override_recovery service.
2875  |      This API assumes the calling code controls the commit cycle.
2876  |      This function returns TRUE if the call to the service is successful.
2877  |      Otherwise, FALSE.
2878  |
2879  |  PARAMETERS
2880  |      P_Invoice_Id - invoice id
2881  |      P_Calling_Mode - calling mode.  Identifies which service to call
2882  |      P_All_Error_Messages - Should API return 1 error message or allow
2883  |                             calling point to get them from message stack
2884  |      P_error_code - Error code to be returned
2885  |      P_calling_sequence -  Calling sequence
2886  |
2887  |  MODIFICATION HISTORY
2888  |    DATE          Author         Action
2889  |    07-OCT-2003   SYIDNER        Created
2890  |
2891  *============================================================================*/
2892 
2893   FUNCTION Override_Recovery(
2894              P_Invoice_id              IN NUMBER,
2895              P_Calling_Mode            IN VARCHAR2,
2896              P_All_Error_Messages      IN VARCHAR2,
2897              P_Error_Code              OUT NOCOPY VARCHAR2,
2898              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
2899   IS
2900 
2901     l_debug_info                 VARCHAR2(240);
2902     l_curr_calling_sequence      VARCHAR2(4000);
2903 
2904     l_inv_header_rec             ap_invoices_all%ROWTYPE;
2905     l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
2906     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
2907 
2908     CURSOR Invoice_Header IS
2909     SELECT *
2910       FROM ap_invoices_all
2911      WHERE invoice_id = P_Invoice_Id;
2912 
2913     l_transaction_rec             zx_api_pub.transaction_rec_type;
2914 
2915     l_return_status_service       VARCHAR2(4000);
2916     l_msg_count                   NUMBER;
2917     l_msg_data                    VARCHAR2(4000);
2918     l_msg                         VARCHAR2(4000);
2919 
2920     l_return_status               BOOLEAN := TRUE;
2921     l_api_name                  varchar2(30) := 'Override_recovery';
2922 
2923   BEGIN
2924     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Override_Recovery<-' ||
2925                                P_calling_sequence;
2926 
2927     -----------------------------------------------------------------
2928     l_debug_info := 'Step 1: Populating invoice header local record';
2929     -----------------------------------------------------------------
2930 
2931     BEGIN
2932       OPEN Invoice_Header;
2933       FETCH Invoice_Header INTO l_inv_header_rec;
2934       CLOSE Invoice_Header;
2935     END;
2936 
2937     -------------------------------------------------------------------
2938     l_debug_info := 'Step 2: Get event class code';
2939     -------------------------------------------------------------------
2940 
2941     IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
2942       P_Invoice_Type_Lookup_Code => l_inv_header_rec.invoice_type_lookup_code,
2943       P_Event_Class_Code         => l_event_class_code,
2944       P_error_code               => P_error_code,
2945       P_calling_sequence         => l_curr_calling_sequence)) THEN
2946 
2947       l_return_status := FALSE;
2948 
2949     END IF;
2950 
2951     -------------------------------------------------------------------
2952     l_debug_info := 'Step 3: Get event type code';
2953     -------------------------------------------------------------------
2954     IF (l_return_status = TRUE) THEN
2955       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
2956         P_Event_Class_Code          => l_event_class_code,
2957         P_Calling_Mode              => P_Calling_Mode,
2958         P_eTax_Already_called_flag  => NULL,
2959         P_Event_Type_Code           => l_Event_Type_Code,
2960         P_Error_Code                => P_error_code,
2961         P_Calling_Sequence          => l_curr_calling_sequence)) THEN
2962 
2963         l_return_status := FALSE;
2964       END IF;
2965     END IF;
2966 /* added for 6157052 we need to populate zx_headers_gt in this case.*/
2967     IF NOT(AP_ETAX_SERVICES_PKG.Populate_Headers_GT(
2968     	      P_Invoice_Header_Rec         => l_inv_header_rec,
2969     	      P_Calling_Mode               => P_Calling_Mode,
2970     	      P_eTax_Already_called_flag   => 'Y',
2971     	      P_Event_Class_Code           => l_event_class_code,
2972     	      P_Event_Type_Code            => l_event_type_code,
2973     	      P_Error_Code                 => P_error_code,
2974 	      P_Calling_Sequence           => l_curr_calling_sequence )) THEN
2975 
2976     	  l_return_status := FALSE;
2977     END IF;
2978 
2979 --    IF (l_tax_already_calculated = 'Y') THEN
2980 --For bug 6157052 - Commented the below call as we need to call this api when we
2981 -- are freezing the distributions and after that we are not supposed to change
2982 --the distributons.But here we need to change the tax distributions as we are
2983 --changing the ecovery rate .so we need not make this callhere.
2984 /*
2985         IF NOT(AP_ETAX_SERVICES_PKG.Freeze_Distributions(
2986                 P_Invoice_Header_Rec  => l_inv_header_rec,
2987                 P_Calling_Mode        => 'FREEZE DISTRIBUTIONS',
2988                 P_Event_Class_Code    => l_event_class_code,
2989                 P_All_Error_Messages  => P_All_Error_Messages,
2990                 P_Error_Code          => P_error_code,
2991                 P_Calling_Sequence    => l_curr_calling_sequence)) THEN
2992 
2993            l_return_status := FALSE;
2994         END IF; */
2995 --    END IF;
2996 
2997     -------------------------------------------------------------------
2998     l_debug_info := 'Step 4: Populate service specific parameter';
2999     -------------------------------------------------------------------
3000     l_transaction_rec.internal_organization_id := l_inv_header_rec.org_id;
3001     l_transaction_rec.application_id           := 200;
3002     l_transaction_rec.entity_code              := 'AP_INVOICES';
3003     l_transaction_rec.event_class_code         := l_event_class_code;
3004     l_transaction_rec.event_type_code          := l_event_type_code;
3005     l_transaction_rec.trx_id                   := l_inv_header_rec.invoice_id;
3006 
3007     -------------------------------------------------------------------
3008     l_debug_info := 'Step 6: Call to override_recovery service';
3009     -------------------------------------------------------------------
3010     IF ( l_return_status = TRUE ) THEN
3011 -- Debug messages added for 6321366
3012 IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3013               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
3014               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'application_id: '           || l_transaction_rec.application_id);
3015               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'entity_code: ' || l_transaction_rec.entity_code);
3016               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: ' || l_transaction_rec.event_class_code);
3017               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_type_code: ' || l_transaction_rec.event_type_code);
3018               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '           || l_transaction_rec.trx_id);
3019 END IF;
3020 
3021       zx_api_pub.override_recovery(
3022         p_api_version        => 1.0,
3023         p_init_msg_list      => FND_API.G_TRUE,
3024         p_commit             => FND_API.G_FALSE,
3025         p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
3026         p_transaction_rec    => l_transaction_rec,
3027         x_return_status      => l_return_status_service,
3028         x_msg_count          => l_msg_count,
3029         x_msg_data           => l_msg_data);
3030 
3031     END IF;
3032 
3033     -----------------------------------------------------------------
3034     l_debug_info := 'Step 7: Verify return status';
3035     -----------------------------------------------------------------
3036     IF (l_return_status_service = 'S') THEN
3037       -----------------------------------------------------------------
3038       l_debug_info := 'Step 8: Handle return of tax lines';
3039       -----------------------------------------------------------------
3040 -- Debug messages added for 6321366
3041  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3042               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
3043  END IF;
3044        IF NOT(AP_ETAX_SERVICES_PKG.Update_AP(
3045                 P_Invoice_header_rec => l_inv_header_rec,
3046                 P_Calling_Mode       => P_Calling_Mode,
3047                 P_All_Error_Messages => P_All_Error_Messages,
3048                 P_Error_Code         => P_error_code,
3049                 P_Calling_Sequence   => l_curr_calling_sequence)) THEN
3050 
3051           l_return_status := FALSE;
3052        END IF;
3053 
3054     ELSE -- handle errors
3055 
3056       l_return_status := FALSE;
3057       -----------------------------------------------------------------
3058       l_debug_info := 'Step 9: Handle errors returned by API';
3059       -----------------------------------------------------------------
3060 -- Debug messages added for 6321366
3061  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3062               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
3063  END IF;
3064       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
3065                P_All_Error_Messages  => P_All_Error_Messages,
3066                P_Msg_Count           => l_msg_count,
3067                P_Msg_Data            => l_msg_data,
3068                P_Error_Code          => P_Error_Code,
3069                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
3070         NULL;
3071       END IF;
3072 
3073     END IF; -- end of return_status_service
3074 
3075     RETURN l_return_status;
3076 
3077   EXCEPTION
3078     WHEN OTHERS THEN
3079       IF (SQLCODE <> -20001) THEN
3080         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
3081         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
3082         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
3083         FND_MESSAGE.SET_TOKEN('PARAMETERS',
3084           ' P_Invoice_Id = '||P_Invoice_Id||
3085           ' P_Calling_Mode ='||P_Calling_Mode||
3086           ' P_Error_Code = '||P_Error_Code||
3087           ' P_Calling_Sequence = '||P_Calling_Sequence);
3088         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
3089       END IF;
3090 
3091       APP_EXCEPTION.RAISE_EXCEPTION;
3092 
3093   END Override_Recovery;
3094 
3095 
3096 
3097 --Bug7592845
3098 /*=============================================================================
3099  |  FUNCTION - Freeze_itm_Distributions()
3100  |
3101  |  DESCRIPTION
3102  |      Public function that will call the freeze_tax_distributions service.
3103  |      This API assumes the calling code controls the commit cycle.
3104  |      This function returns TRUE if the call to the service is successful.
3105  |      Otherwise, FALSE.
3106  |
3107  |  PARAMETERS
3108  |      P_Invoice_Header_Rec - Invoice record info
3109  |      P_Calling_Mode - calling mode.  Identifies which service to call
3110  |      P_Event_Class_Code - event class code for the invoice type
3111  |      P_All_Error_Messages - Should API return 1 error message or allow
3112  |                             calling point to get them from message stack
3113  |      P_error_code - Error code to be returned
3114  |      P_calling_sequence -  Calling sequence
3115  |
3116  |  MODIFICATION HISTORY
3117  |    DATE                  Author                             Action
3118  |    11-DEC-2008   SCHITLAP/HCHAUDHA        Created
3119  |
3120  *============================================================================*/
3121 
3122 FUNCTION Freeze_itm_Distributions(
3123              P_Invoice_Header_Rec      IN ap_invoices_all%ROWTYPE,
3124              P_Calling_Mode            IN VARCHAR2,
3125              P_Event_Class_Code        IN VARCHAR2,
3126              P_All_Error_Messages      IN VARCHAR2,
3127              P_Error_Code              OUT NOCOPY VARCHAR2,
3128              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
3129 IS
3130 
3131     l_debug_info                 VARCHAR2(240);
3132     l_curr_calling_sequence      VARCHAR2(4000);
3133     l_transaction_rec            zx_api_pub.transaction_rec_type;
3134 
3135     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
3136 
3137     CURSOR itm_Dist IS
3138       SELECT encumbered_flag,
3139              reversal_flag,
3140              prepay_distribution_id,
3141              accrual_posted_flag,
3142              cash_posted_flag,
3143              posted_flag,
3144              org_id,
3145              pa_addition_flag,
3146              match_status_flag,
3147              corrected_invoice_dist_id,
3148              invoice_distribution_id,
3149              po_distribution_id,
3150              rcv_transaction_id,
3151              accounting_event_id,
3152              dist_match_type,
3153              amount,
3154              prepay_amount_remaining
3155         FROM ap_invoice_distributions_all
3156        WHERE invoice_id = p_invoice_header_rec.invoice_id
3157          AND line_type_lookup_code NOT IN ('REC_TAX', 'NONREC_TAX', 'TRV', 'TERV', 'TIPV');
3158 
3159     TYPE l_itm_dist_tab_local   IS TABLE OF itm_Dist%ROWTYPE;
3160     l_itm_dist_list_local 	l_itm_dist_tab_local;
3161 
3162     -- If related_id is equals invoice_distribution_id we are
3163     -- sure is the primary distribution created (not including variances)
3164     -- Rules applied to primary taxable distributions apply to
3165     -- related variances.
3166 
3167     l_return_status_service       VARCHAR2(4000);
3168     l_msg_count                   NUMBER;
3169     l_msg_data                    VARCHAR2(4000);
3170     l_msg                         VARCHAR2(4000);
3171 
3172     l_return_status               BOOLEAN := TRUE;
3173     l_freeze_dist_flag            VARCHAR2(1);
3174 
3175     l_po_distribution_id      ap_invoice_distributions_all.po_distribution_id%TYPE;
3176     l_rcv_transaction_id	  ap_invoice_distributions_all.rcv_transaction_id%TYPE;
3177 
3178     freeze_dist_list          ZX_API_PUB.number_tbl_type;
3179 
3180 
3181     l_api_name                CONSTANT VARCHAR2(100) := 'Freeze_Itm_Distributions';
3182 
3183   BEGIN
3184     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Freeze_Itm_Distributions<-' ||
3185                                P_calling_sequence;
3186     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3187        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_curr_calling_sequence);
3188     END IF;
3189 
3190     -----------------------------------------------------------------
3191     l_debug_info := 'Step 1: Populating Item distributions collection';
3192     -----------------------------------------------------------------
3193 
3194     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3195        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
3196     END IF;
3197 
3198 
3199 
3200     BEGIN
3201       OPEN Itm_Dist;
3202       FETCH Itm_Dist
3203       BULK COLLECT INTO l_itm_dist_list_local;
3204       CLOSE Itm_Dist;
3205     END;
3206 
3207     -------------------------------------------------------------------
3208     l_debug_info := 'Step 2: Get event type code';
3209     -------------------------------------------------------------------
3210 
3211     IF (l_return_status = TRUE) THEN
3212       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
3213         P_Event_Class_Code          => P_event_class_code,
3214         P_Calling_Mode              => P_Calling_Mode,
3215         P_eTax_Already_called_flag  => 'Y',
3216         P_Event_Type_Code           => l_Event_Type_Code,
3217         P_Error_Code                => P_error_code,
3218         P_Calling_Sequence          => l_curr_calling_sequence)) THEN
3219 
3220         l_return_status := FALSE;
3221       END IF;
3222     END IF;
3223 
3224     IF (l_itm_dist_list_local.COUNT <> 0) THEN
3225 
3226       FOR i IN l_itm_dist_list_local.FIRST..l_itm_dist_list_local.LAST LOOP
3227         -- set l_freeze_dist_flag to N to initiate process
3228         l_freeze_dist_flag := 'N';
3229 
3230         -- Rules for distributions
3231         -----------------------------------------------------------------
3232         l_debug_info := 'Step 3: Item distribution is encumbered';
3233         -----------------------------------------------------------------
3234         IF (NVL(l_itm_dist_list_local(i).encumbered_flag, 'N') IN ('Y','D','W','X')) THEN
3235 
3236           -- possible values verified for encumbered_flag
3237           -- Y: Regular line, has already been successfully encumbered by AP.
3238           -- D: Same as Y for reversal distribution line.
3239           -- W: Regular line, has been encumbered in advisory mode even though
3240           --    insufficient funds existed.
3241           -- X: Same as W for reversal distribution line.
3242 
3243           l_freeze_dist_flag := 'Y';
3244 
3245         END IF;
3246         -----------------------------------------------------------------
3247         l_debug_info := 'Step 4: Item distribution is part of a reversal pair';
3248         -----------------------------------------------------------------
3249         IF (l_freeze_dist_flag = 'N' AND
3250             l_itm_dist_list_local(i).reversal_flag = 'Y') THEN
3251 
3252           l_freeze_dist_flag := 'Y';
3253 
3254         END IF;
3255 
3256         -----------------------------------------------------------------
3257         l_debug_info := 'Step 5: Item distribution is PO/RCV matched';
3258         -----------------------------------------------------------------
3259         IF (l_freeze_dist_flag = 'N' AND
3260            (l_itm_dist_list_local(i).po_distribution_id IS NOT NULL
3261             OR l_itm_dist_list_local(i).rcv_transaction_id IS NOT NULL)) THEN
3262 
3263             l_freeze_dist_flag := 'Y';
3264         END IF;
3265 
3266         -----------------------------------------------------------------
3267         l_debug_info := 'Step 6: Item distribution is prepayment '||
3268                         'application/unapplication';
3269         -----------------------------------------------------------------
3270 
3271         -- For tax distributions created based on a PREPAY distribution
3272         -- (parent dist) the prepay_distribution_id will be always populated
3273         -- with the prepay_distribution_id of the parent (PREPAY) dist.
3274 
3275         IF (l_freeze_dist_flag = 'N' AND
3276             l_itm_dist_list_local(i).prepay_distribution_id IS NOT NULL) THEN
3277 
3278           l_freeze_dist_flag := 'Y';
3279 
3280         END IF;
3281 
3282         -----------------------------------------------------------------
3283         l_debug_info := 'Step 7: Item distribution if partially of fully '||
3284                         'accounted';
3285         -----------------------------------------------------------------
3286         IF (l_freeze_dist_flag = 'N' ) THEN
3287            IF ( ap_invoice_distributions_pkg.Get_Posted_Status(
3288              X_Accrual_Posted_Flag => l_itm_dist_list_local(i).accrual_posted_flag,
3289              X_Cash_Posted_Flag    => l_itm_dist_list_local(i).cash_posted_flag,
3290              X_Posted_Flag         => l_itm_dist_list_local(i).posted_flag,
3291              X_Org_Id              => l_itm_dist_list_local(i).org_id) <> 'N') THEN
3292              l_freeze_dist_flag := 'Y';
3293            END IF;
3294         END IF;
3295 
3296         -----------------------------------------------------------------
3297         l_debug_info := 'Step 8: Item distribution is transferred to projects';
3298         -----------------------------------------------------------------
3299 
3300         IF (l_freeze_dist_flag = 'N' ) THEN
3301            IF (NVL(l_itm_dist_list_local(i).pa_addition_flag,'N') NOT IN ('N', 'E')) THEN
3302              -- N means not yet transfer to projects
3303              -- E means not project related.
3304              -- If the flag is Y or any rejection code, it is assumed to be
3305              -- transfered
3306 
3307              l_freeze_dist_flag := 'Y';
3308            END IF;
3309         END IF;
3310 
3311         -----------------------------------------------------------------
3312         l_debug_info := 'Step 9: Item distribution has been validated';
3313         -----------------------------------------------------------------
3314 
3315         IF (l_freeze_dist_flag = 'N' AND
3316            NVL(l_itm_dist_list_local(i).match_status_flag,'N') IN ('T', 'A')) THEN
3317 
3318           l_freeze_dist_flag := 'Y';
3319         END IF;
3320 
3321         -----------------------------------------------------------------
3322         l_debug_info := 'Step 9.1: accounting_event_id is stamped';
3323         -----------------------------------------------------------------
3324 
3325         IF (l_freeze_dist_flag = 'N' AND
3326             l_itm_dist_list_local(i).accounting_event_id IS NOT NULL) THEN
3327 
3328           l_freeze_dist_flag := 'Y';
3329         END IF;
3330 
3331         -----------------------------------------------------------------
3332         l_debug_info := 'Step 10: Item distribution is a part of '||
3333                         'prepayment and has been partially or fully applied';
3334         -----------------------------------------------------------------
3335 
3336             -- This validation was analized and it is not included since the
3337             -- this API will not be called after the prepayment is
3338             -- partiall or fully applied.  The prepayment invoice cannot be
3339             -- modified after it is partially or fully applied.
3340 
3341          IF(l_freeze_dist_flag = 'N' AND
3342             P_Invoice_Header_Rec.invoice_type_lookup_code='PREPAYMENT' AND
3343             l_itm_dist_list_local(i).amount <>l_itm_dist_list_local(i).prepay_amount_remaining) THEN
3344 
3345             l_freeze_dist_flag := 'Y';
3346          END IF;
3347 
3348          -----------------------------------------------------------------
3349         l_debug_info := 'Step 12: Check if the parent item line has been '||
3350                         'adjusted by a PO price Adjustment or it itself an '||
3351                         'adjustment';
3352         -----------------------------------------------------------------
3353         IF (l_freeze_dist_flag = 'N' AND
3354             l_itm_dist_list_local(i).dist_match_type IN
3355             ('ADJUSTMENT_CORRECTION','PO_PRICE_ADJUSTMENT')) THEN
3356             l_freeze_dist_flag := 'Y';
3357         END IF;
3358 
3359         -----------------------------------------------------------------
3360         l_debug_info := 'Step 13: Item line is a corrected one ';
3361         -----------------------------------------------------------------
3362         -- This rule can be applied at distribution level since the
3363         -- corrected_invoice_dist_id is populated if it corrects any other
3364         -- distribution.
3365         IF (l_freeze_dist_flag = 'N' AND
3366             l_itm_dist_list_local(i).corrected_invoice_dist_id IS NOT NULL) THEN
3367           l_freeze_dist_flag := 'Y';
3368         END IF;
3369 
3370         -----------------------------------------------------------------
3371         l_debug_info := ' Inv Dist ID: '||l_itm_dist_list_local(i).invoice_distribution_id||
3372                         ' Freeze Flag: '||l_freeze_dist_flag;
3373         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3374             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3375         END IF;
3376         -----------------------------------------------------------------
3377 
3378         IF (l_freeze_dist_flag = 'Y') THEN
3379 
3380            freeze_dist_list(i) := l_itm_dist_list_local(i).invoice_distribution_id;
3381 
3382            l_debug_info := ' Frozen Dist Id '||freeze_dist_list(i);
3383 
3384            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3385                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3386            END IF;
3387 
3388         END IF;
3389 
3390       END LOOP;
3391     END IF;
3392 
3393     l_transaction_rec.internal_organization_id := P_Invoice_Header_Rec.org_id;
3394     l_transaction_rec.application_id           := 200;
3395     l_transaction_rec.entity_code              := 'AP_INVOICES';
3396     l_transaction_rec.event_class_code         := P_event_class_code;
3397     l_transaction_rec.event_type_code          := l_event_type_code;
3398     l_transaction_rec.trx_id                   := P_Invoice_Header_Rec.invoice_id;
3399 
3400 
3401     -----------------------------------------------------------------
3402     l_debug_info := 'Step 18: Call Freeze_tax_distributions service';
3403     -----------------------------------------------------------------
3404     IF ( l_return_status = TRUE AND freeze_dist_list.count>0) THEN
3405 
3406      zx_new_services_pkg.freeze_tax_dists_for_items(
3407         p_api_version          => 1.0,
3408         p_init_msg_list        => FND_API.G_TRUE,
3409         p_commit               => FND_API.G_FALSE,
3410         p_validation_level     => FND_API.G_VALID_LEVEL_FULL,
3411         x_return_status        => l_return_status_service,
3412         x_msg_count            => l_msg_count,
3413         x_msg_data             => l_msg_data,
3414         p_transaction_rec      => l_transaction_rec,
3415         p_trx_line_dist_id_tbl => freeze_dist_list);
3416 
3417     END IF;
3418 
3419 
3420       -----------------------------------------------------------------
3421       l_debug_info := 'Step 19: Verify return status';
3422       -----------------------------------------------------------------
3423     IF (l_return_status_service <> 'S') THEN  -- handle errors
3424 
3425       l_return_status := FALSE;
3426       -----------------------------------------------------------------
3427       l_debug_info := 'Step 20: Handle errors returned by API';
3428       -----------------------------------------------------------------
3429 
3430       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
3431                P_All_Error_Messages  => P_All_Error_Messages,
3432                P_Msg_Count           => l_msg_count,
3433                P_Msg_Data            => l_msg_data,
3434                P_Error_Code          => P_Error_Code,
3435                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
3436         NULL;
3437       END IF;
3438 
3439     END IF;
3440 
3441   RETURN l_return_status;
3442 
3443 EXCEPTION
3444     WHEN OTHERS THEN
3445       IF (SQLCODE <> -20001) THEN
3446         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
3447         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
3448         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
3449         FND_MESSAGE.SET_TOKEN('PARAMETERS',
3450           ' P_Invoice_Id = '||P_Invoice_Header_Rec.Invoice_id||
3451           ' P_Calling_Mode ='||P_Calling_Mode||
3452           ' P_Error_Code = '||P_Error_Code||
3453           ' P_Calling_Sequence = '||P_Calling_Sequence);
3454         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
3455       END IF;
3456 
3457       IF ( Itm_Dist%ISOPEN ) THEN
3458         CLOSE Itm_Dist;
3459       END IF;
3460 
3461       APP_EXCEPTION.RAISE_EXCEPTION;
3462 
3463 END Freeze_itm_Distributions;
3464 --Bug7592845
3465 
3466 /*=============================================================================
3467  |  FUNCTION - Freeze_Distributions()
3468  |
3469  |  DESCRIPTION
3470  |      Public function that will call the freeze_tax_distributions service.
3471  |      This API assumes the calling code controls the commit cycle.
3472  |      This function returns TRUE if the call to the service is successful.
3473  |      Otherwise, FALSE.
3474  |
3475  |  PARAMETERS
3476  |      P_Invoice_Header_Rec - Invoice record info
3477  |      P_Calling_Mode - calling mode.  Identifies which service to call
3478  |      P_Event_Class_Code - event class code for the invoice type
3479  |      P_All_Error_Messages - Should API return 1 error message or allow
3480  |                             calling point to get them from message stack
3481  |      P_error_code - Error code to be returned
3482  |      P_calling_sequence -  Calling sequence
3483  |
3484  |  MODIFICATION HISTORY
3485  |    DATE          Author         Action
3486  |    07-OCT-2003   SYIDNER        Created
3487  |
3488  *============================================================================*/
3489 
3490   FUNCTION Freeze_Distributions(
3491              P_Invoice_Header_Rec      IN ap_invoices_all%ROWTYPE,
3492              P_Calling_Mode            IN VARCHAR2,
3493              P_Event_Class_Code        IN VARCHAR2,
3494              P_All_Error_Messages      IN VARCHAR2,
3495              P_Error_Code              OUT NOCOPY VARCHAR2,
3496              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
3497   IS
3498 
3499     l_debug_info                 VARCHAR2(240);
3500     l_curr_calling_sequence      VARCHAR2(4000);
3501     l_transaction_rec            zx_api_pub.transaction_rec_type;
3502 
3503     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
3504 
3505     CURSOR Tax_Dist IS
3506     (SELECT encumbered_flag,
3507             reversal_flag,
3508             charge_applicable_to_dist_id,
3509             prepay_distribution_id,
3510             accrual_posted_flag,
3511             cash_posted_flag,
3512             posted_flag,
3513             org_id,
3514             pa_addition_flag,
3515             match_status_flag,
3516             corrected_invoice_dist_id,
3517             invoice_distribution_id,
3518             detail_tax_dist_id,
3519             accounting_event_id
3520        FROM ap_invoice_distributions_all
3521       WHERE invoice_id = p_invoice_header_rec.invoice_id
3522         AND line_type_lookup_code IN ('REC_TAX', 'NONREC_TAX', 'TRV', 'TERV', 'TIPV')
3523         AND (related_id IS NULL
3524                   OR (related_id = invoice_distribution_id
3525                       OR (related_id IS NOT NULL
3526                           AND line_type_lookup_code IN ('TRV', 'TERV', 'TIPV'))))
3527       UNION ALL
3528       SELECT encumbered_flag,
3529              reversal_flag,
3530              charge_applicable_to_dist_id,
3531              prepay_distribution_id,
3532              accrual_posted_flag,
3533              cash_posted_flag,
3534              posted_flag,
3535              org_id,
3536              pa_addition_flag,
3537              match_status_flag,
3538              corrected_invoice_dist_id,
3539              invoice_distribution_id,
3540              detail_tax_dist_id,
3541              accounting_event_id
3542         FROM ap_self_assessed_tax_dist_all
3543        WHERE invoice_id = p_invoice_header_rec.invoice_id
3544          AND line_type_lookup_code IN ('REC_TAX', 'NONREC_TAX', 'TRV', 'TERV', 'TIPV')
3545          AND (related_id IS NULL
3546                   OR (related_id = invoice_distribution_id
3547                       OR (related_id IS NOT NULL
3548                           AND line_type_lookup_code IN ('TRV', 'TERV', 'TIPV')))));
3549 
3550 
3551     TYPE l_tax_dist_tab_local   IS TABLE OF Tax_Dist%ROWTYPE;
3552     l_tax_dist_list_local 	l_tax_dist_tab_local;
3553 
3554     -- If related_id is equals invoice_distribution_id we are
3555     -- sure is the primary distribution created (not including variances)
3556     -- Rules applied to primary taxable distributions apply to
3557     -- related variances.
3558 
3559     l_return_status_service       VARCHAR2(4000);
3560     l_msg_count                   NUMBER;
3561     l_msg_data                    VARCHAR2(4000);
3562     l_msg                         VARCHAR2(4000);
3563 
3564     l_return_status               BOOLEAN := TRUE;
3565     l_freeze_dist_flag            VARCHAR2(1);
3566 
3567     l_po_distribution_id          ap_invoice_distributions_all.po_distribution_id%TYPE;
3568     l_rcv_transaction_id          ap_invoice_distributions_all.rcv_transaction_id%TYPE;
3569 
3570     TYPE freeze_tax_dist_type IS TABLE OF zx_tax_dist_id_gt%ROWTYPE;
3571     freeze_dist_list          freeze_tax_dist_type := freeze_tax_dist_type();
3572 
3573     l_api_name                    CONSTANT VARCHAR2(100) := 'Freeze_Distributions';
3574 
3575   BEGIN
3576     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Freeze_Distributions<-' ||
3577                                P_calling_sequence;
3578 
3579     -----------------------------------------------------------------
3580     l_debug_info := 'Step 1: Populating tax distributions collection';
3581     -----------------------------------------------------------------
3582     BEGIN
3583       OPEN Tax_Dist;
3584       FETCH Tax_Dist
3585       BULK COLLECT INTO l_tax_dist_list_local;
3586       CLOSE Tax_Dist;
3587     END;
3588 
3589     -------------------------------------------------------------------
3590     l_debug_info := 'Step 2: Get event type code';
3591     -------------------------------------------------------------------
3592 
3593     IF (l_return_status = TRUE) THEN
3594       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
3595         P_Event_Class_Code          => P_event_class_code,
3596         P_Calling_Mode              => P_Calling_Mode,
3597         P_eTax_Already_called_flag  => 'Y',
3598         P_Event_Type_Code           => l_Event_Type_Code,
3599         P_Error_Code                => P_error_code,
3600         P_Calling_Sequence          => l_curr_calling_sequence)) THEN
3601 
3602         l_return_status := FALSE;
3603       END IF;
3604     END IF;
3605 
3606     IF (l_tax_dist_list_local.COUNT <> 0) THEN
3607       -- Initialize freeze_dist_list collection
3608       freeze_dist_list.EXTEND(l_tax_dist_list_local.COUNT);
3609 
3610       FOR i IN l_tax_dist_list_local.FIRST..l_tax_dist_list_local.LAST LOOP
3611         -- set l_freeze_dist_flag to N to initiate process
3612         l_freeze_dist_flag := 'N';
3613 
3614         -- Rules for distributions
3615         -----------------------------------------------------------------
3616         l_debug_info := 'Step 3: Tax distribution is encumbered';
3617         -----------------------------------------------------------------
3618         IF (NVL(l_tax_dist_list_local(i).encumbered_flag, 'N') IN ('Y','D','W','X')) THEN
3619           -- possible values verified for encumbered_flag
3620           -- Y: Regular line, has already been successfully encumbered by AP.
3621           -- D: Same as Y for reversal distribution line.
3622           -- W: Regular line, has been encumbered in advisory mode even though
3623           --    insufficient funds existed.
3624           -- X: Same as W for reversal distribution line.
3625 
3626           l_freeze_dist_flag := 'Y';
3627 
3628         END IF;
3629         -----------------------------------------------------------------
3630         l_debug_info := 'Step 4: Tax distribution is part of a reversal pair';
3631         -----------------------------------------------------------------
3632         IF (l_freeze_dist_flag = 'N' AND
3633             l_tax_dist_list_local(i).reversal_flag = 'Y') THEN
3634 
3635           l_freeze_dist_flag := 'Y';
3636 
3637         END IF;
3638 
3639         -----------------------------------------------------------------
3640         l_debug_info := 'Step 5: Parent Item distribution is PO/RCV matched';
3641         -----------------------------------------------------------------
3642         IF (l_freeze_dist_flag = 'N' AND
3643            (l_tax_dist_list_local(i).charge_applicable_to_dist_id IS NOT NULL
3644             and l_tax_dist_list_local(i).charge_applicable_to_dist_id <>-99)) THEN
3645            /* for bug 6010950 added 'charge_applicable_to_dist_id<>-99 condition'.
3646               As for Tax only invoices which are not receipt matched below
3647               select should not fire.I found out that value of charge_applicable_to_dist_id is
3648               -99 in this case.So aaded the AND condition to avoid the select.  */
3649             --this validation because it can be null for tax-only lines
3650 
3651            SELECT po_distribution_id, rcv_transaction_id
3652              INTO l_po_distribution_id, l_rcv_transaction_id
3653              FROM ap_invoice_distributions_all
3654             WHERE invoice_distribution_id =
3655                   l_tax_dist_list_local(i).charge_applicable_to_dist_id;
3656 
3657 
3658            IF ( l_po_distribution_id IS NOT NULL OR
3659               l_rcv_transaction_id IS NOT NULL ) THEN
3660               l_freeze_dist_flag := 'Y';
3661 
3662            END IF;
3663         END IF;
3664 
3665         -----------------------------------------------------------------
3666         l_debug_info := 'Step 6: Parent Item distribution is prepayment '||
3667                         'application/unapplication';
3668         -----------------------------------------------------------------
3669         -- For tax distributions created based on a PREPAY distribution
3670         -- (parent dist) the prepay_distribution_id will be always populated
3671         -- with the prepay_distribution_id of the parent (PREPAY) dist.
3672         IF (l_freeze_dist_flag = 'N' AND
3673             l_tax_dist_list_local(i).prepay_distribution_id IS NOT NULL) THEN
3674 
3675           l_freeze_dist_flag := 'Y';
3676 
3677         END IF;
3678 
3679         -----------------------------------------------------------------
3680         l_debug_info := 'Step 7: Tax distribution if partially of fully '||
3681                         'accounted';
3682         -----------------------------------------------------------------
3683         IF (l_freeze_dist_flag = 'N' ) THEN
3684            IF ( ap_invoice_distributions_pkg.Get_Posted_Status(
3685              X_Accrual_Posted_Flag => l_tax_dist_list_local(i).accrual_posted_flag,
3686              X_Cash_Posted_Flag    => l_tax_dist_list_local(i).cash_posted_flag,
3687              X_Posted_Flag         => l_tax_dist_list_local(i).posted_flag,
3688              X_Org_Id              => l_tax_dist_list_local(i).org_id) <> 'N') THEN
3689              l_freeze_dist_flag := 'Y';
3690            END IF;
3691         END IF;
3692 
3693         -----------------------------------------------------------------
3694         l_debug_info := 'Step 8: Tax distribution is transferred to projects';
3695         -----------------------------------------------------------------
3696         IF (l_freeze_dist_flag = 'N' ) THEN
3697            IF (NVL(l_tax_dist_list_local(i).pa_addition_flag,'N') NOT IN ('N', 'E')) THEN
3698              -- N means not yet transfer to projects
3699              -- E means not project related.
3700              -- If the flag is Y or any rejection code, it is assumed to be
3701              -- transfered
3702 
3703              l_freeze_dist_flag := 'Y';
3704            END IF;
3705         END IF;
3706 
3707         -----------------------------------------------------------------
3708         l_debug_info := 'Step 9: Tax distribution has been validated';
3709         -----------------------------------------------------------------
3710         IF (l_freeze_dist_flag = 'N' AND
3711            NVL(l_tax_dist_list_local(i).match_status_flag,'N') IN ('T', 'A')) THEN
3712 
3713           l_freeze_dist_flag := 'Y';
3714         END IF;
3715 
3716         -----------------------------------------------------------------
3717         l_debug_info := 'Step 9.1: accounting_event_id is stamped';
3718         -----------------------------------------------------------------
3719         IF (l_freeze_dist_flag = 'N' AND
3720             l_tax_dist_list_local(i).accounting_event_id IS NOT NULL) THEN
3721 
3722           l_freeze_dist_flag := 'Y';
3723         END IF;
3724 
3725         -----------------------------------------------------------------
3726         l_debug_info := 'Step 10: Parent Item distribution is a part of '||
3727                         'prepayment and has been partially or fully applied';
3728         -----------------------------------------------------------------
3729             -- This validation was analized and it is not included since the
3730             -- this API will not be called after the prepayment is
3731             -- partiall or fully applied.  The prepayment invoice cannot be
3732             -- modified after it is partially or fully applied.
3733 
3734 
3735         -- Rules for lines
3736         -----------------------------------------------------------------
3737         l_debug_info := 'Step 11: Check if tax line has been discarded';
3738         -----------------------------------------------------------------
3739         -- This validation is included in the reversal of the distributions.
3740         -- when the line is discarded the distributions are reversed.
3741 
3742         -----------------------------------------------------------------
3743         l_debug_info := 'Step 12: Check if the parent item line has been '||
3744                         'adjusted by a PO price Adjustment or it itself an '||
3745                         'adjustment';
3746         -----------------------------------------------------------------
3747         --YIDSAL.  This validation will be included when the retro pricing code
3748         -- is included in 11iX
3749 
3750         -----------------------------------------------------------------
3751         l_debug_info := 'Step 13: Parent Item line is a corrected one or is '||
3752                         'itself a correction';
3753         -----------------------------------------------------------------
3754         -- This rule can be applied at distribution level since the
3755         -- corrected_invoice_dist_id is populated if it corrects any other
3756         -- distribution.
3757         IF (l_freeze_dist_flag = 'N' AND
3758             l_tax_dist_list_local(i).corrected_invoice_dist_id IS NOT NULL) THEN
3759 
3760           l_freeze_dist_flag := 'Y';
3761         END IF;
3762 
3763         -- To know if the distribution is a corrected one
3764         -- Verify if the parent item (taxable) distribution is corrected.
3765         IF (l_freeze_dist_flag = 'N' ) THEN
3766           BEGIN
3767             SELECT 'Y'
3768               INTO l_freeze_dist_flag
3769               FROM ap_invoice_distributions_all
3770              WHERE corrected_invoice_dist_id =
3771                    l_tax_dist_list_local(i).charge_applicable_to_dist_id
3772                AND ROWNUM = 1;
3773 
3774           EXCEPTION
3775             WHEN NO_DATA_FOUND THEN NULL;
3776           END;
3777         END IF;
3778 
3779         -----------------------------------------------------------------
3780         l_debug_info := 'Step 14: Parent item line is a prepayment application'||
3781                         '/unapplication';
3782         -----------------------------------------------------------------
3783         -- this validation is included in the distribution validations for the
3784         -- parent item distribution
3785 
3786         -----------------------------------------------------------------
3787         l_debug_info := ' Inv Dist ID: '||l_tax_dist_list_local(i).invoice_distribution_id||
3788                         ' Freeze Flag: '||l_freeze_dist_flag;
3789         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3790             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3791         END IF;
3792         -----------------------------------------------------------------
3793 
3794         IF (l_freeze_dist_flag = 'Y') THEN
3795 
3796            freeze_dist_list(i).tax_dist_id := l_tax_dist_list_local(i).detail_tax_dist_id;
3797 
3798            l_debug_info := ' Frozen Dist Id '||freeze_dist_list(i).tax_dist_id;
3799 
3800            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3801                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3802            END IF;
3803 
3804         END IF;
3805 
3806       END LOOP;
3807     END IF;
3808 
3809     delete zx_tax_dist_id_gt;--Bug7582775
3810 
3811     l_debug_info := 'No Of Rows Deleted From zx_tax_dist_id_gt  '||sql%rowcount;
3812 
3813     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3814        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3815     END IF;
3816 
3817     FOR m IN NVL(freeze_dist_list.FIRST,0)..NVL(freeze_dist_list.LAST,0)
3818     LOOP
3819       IF (freeze_dist_list.exists(m)) THEN
3820 
3821         IF (freeze_dist_list(m).tax_dist_id IS NOT NULL) THEN
3822           INSERT INTO zx_tax_dist_id_gt(tax_dist_id)
3823           VALUES (freeze_dist_list(m).tax_dist_id);
3824 
3825         END IF;
3826       END IF;
3827     END LOOP;
3828 
3829     l_transaction_rec.internal_organization_id := P_Invoice_Header_Rec.org_id;
3830     l_transaction_rec.application_id           := 200;
3831     l_transaction_rec.entity_code              := 'AP_INVOICES';
3832     l_transaction_rec.event_class_code         := P_event_class_code;
3833     l_transaction_rec.event_type_code          := l_event_type_code;
3834     l_transaction_rec.trx_id                   := P_Invoice_Header_Rec.invoice_id;
3835 
3836     -----------------------------------------------------------------
3837     l_debug_info := 'Step 18: Call Freeze_tax_distributions service';
3838     -----------------------------------------------------------------
3839     IF ( l_return_status = TRUE AND freeze_dist_list.count>0) THEN --Bug7582775
3840 
3841      zx_api_pub.freeze_tax_distributions(
3842         p_api_version        => 1.0,
3843         p_init_msg_list      => FND_API.G_TRUE,
3844         p_commit             => FND_API.G_FALSE,
3845         p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
3846         x_return_status      => l_return_status_service,
3847         x_msg_count          => l_msg_count,
3848         x_msg_data           => l_msg_data,
3849         p_transaction_rec    => l_transaction_rec);
3850 
3851     END IF;
3852 
3853     -----------------------------------------------------------------
3854     l_debug_info := 'Step 19: Verify return status';
3855     -----------------------------------------------------------------
3856     IF (l_return_status_service <> 'S') THEN  -- handle errors
3857 
3858       l_return_status := FALSE;
3859       -----------------------------------------------------------------
3860       l_debug_info := 'Step 20: Handle errors returned by API';
3861       -----------------------------------------------------------------
3862 
3863       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
3864                P_All_Error_Messages  => P_All_Error_Messages,
3865                P_Msg_Count           => l_msg_count,
3866                P_Msg_Data            => l_msg_data,
3867                P_Error_Code          => P_Error_Code,
3868                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
3869         NULL;
3870       END IF;
3871 
3872     END IF;
3873 
3874   RETURN l_return_status;
3875 
3876   EXCEPTION
3877     WHEN OTHERS THEN
3878       IF (SQLCODE <> -20001) THEN
3879         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
3880         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
3881         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
3882         FND_MESSAGE.SET_TOKEN('PARAMETERS',
3883           ' P_Invoice_Id = '||P_Invoice_Header_Rec.Invoice_id||
3884           ' P_Calling_Mode ='||P_Calling_Mode||
3885           ' P_Error_Code = '||P_Error_Code||
3886           ' P_Calling_Sequence = '||P_Calling_Sequence);
3887         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
3888       END IF;
3889 
3890       IF ( tax_dist%ISOPEN ) THEN
3891         CLOSE tax_dist;
3892       END IF;
3893 
3894       APP_EXCEPTION.RAISE_EXCEPTION;
3895 
3896   END Freeze_Distributions;
3897 
3898 /*=============================================================================
3899  |  FUNCTION - Global_Document_Update()
3900  |
3901  |  DESCRIPTION
3902  |      Public function that will call the global_document_update service to
3903  |      inform eTax of a cancellation of an invoice, the freeze after the
3904  |      invoice is validated (meaning is ready to reporting), the unfreeze
3905  |      of an invoice because it has to be modified after it was validated, and
3906  |      the release of tax holds by the user.
3907  |      This API assumes the calling code controls the commit cycle.
3908  |      This function returns TRUE if the call to the service is successful.
3909  |      Otherwise, FALSE.
3910  |
3911  |  PARAMETERS
3912  |      P_Invoice_Id - invoice id
3913  |      P_Calling_Mode - calling mode.  Identifies which service to call
3914  |      P_All_Error_Messages - Should API return 1 error message or allow
3915  |                             calling point to get them from message stack
3916  |      P_error_code - Error code to be returned
3917  |      P_calling_sequence -  Calling sequence
3918  |
3919  |  MODIFICATION HISTORY
3920  |    DATE          Author         Action
3921  |    07-OCT-2003   SYIDNER        Created
3922  |
3923  *============================================================================*/
3924 
3925   FUNCTION Global_Document_Update(
3926              P_Invoice_id              IN NUMBER,
3927 	     P_Line_Number	       IN NUMBER DEFAULT NULL,
3928              P_Calling_Mode            IN VARCHAR2,
3929              P_All_Error_Messages      IN VARCHAR2,
3930              P_Error_Code              OUT NOCOPY VARCHAR2,
3931              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
3932   IS
3933 
3934     l_debug_info                 VARCHAR2(240);
3935     l_curr_calling_sequence      VARCHAR2(4000);
3936 
3937     l_inv_header_rec             ap_invoices_all%ROWTYPE;
3938     l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
3939     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
3940 
3941     CURSOR Invoice_Header IS
3942     SELECT *
3943       FROM ap_invoices_all
3944      WHERE invoice_id = P_Invoice_Id;
3945 
3946     l_transaction_rec             zx_api_pub.transaction_rec_type;
3947 
3948     l_return_status_service       VARCHAR2(4000);
3949     l_msg_count                   NUMBER;
3950     l_msg_data                    VARCHAR2(4000);
3951     l_msg                         VARCHAR2(4000);
3952 
3953     l_success			  BOOLEAN;
3954     Tax_Exception		  EXCEPTION;
3955     l_return_status               BOOLEAN := TRUE;
3956     l_api_name                  VARCHAR2(30) := 'global_document_update'; -- bug 6321366
3957   BEGIN
3958 
3959     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Global_Document_Update<-' ||
3960                                P_calling_sequence;
3961 
3962     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3963        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_curr_calling_sequence);
3964     END IF;
3965 
3966     IF P_Calling_Mode IN ('CANCEL INVOICE', 'DISCARD LINE') THEN
3967 
3968        --Bug6799496
3969        --IF control_amount (Control Tax amount is non zero then invoice cancellation
3970        --errors out. Hence making it 0 before we populate headers_gt (zx table)
3971        -------------------------------------------------------------------
3972        l_debug_info := 'If CANCEL INVOICE Then Make Control Amount 0';
3973        -------------------------------------------------------------------
3974        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3975           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
3976        END IF;
3977 
3978        UPDATE ap_invoices_all ail
3979           SET control_amount= NULL --Bug6887264
3980         WHERE ail.invoice_id = p_invoice_id
3981           AND P_Calling_Mode = 'CANCEL INVOICE'
3982           AND ail.control_amount IS NOT NULL ;
3983               --bug 6845888
3984        --Bug6799496
3985 
3986         UPDATE ap_invoice_lines_all ail
3987           SET control_amount= NULL --Bug6887264
3988         WHERE ail.invoice_id = p_invoice_id
3989           AND P_Calling_Mode = 'CANCEL INVOICE'
3990           AND ail.control_amount IS NOT NULL ;
3991 
3992 
3993        l_success := ap_etax_pkg.calling_etax(
3994                            p_invoice_id         => p_invoice_id,
3995                            p_calling_mode       => 'CALCULATE',
3996                            p_all_error_messages => 'N',
3997                            p_error_code         => p_error_code,
3998                            p_calling_sequence   => l_curr_calling_sequence);
3999 
4000        IF (NOT l_success) THEN
4001            RAISE Tax_Exception;
4002        END IF;
4003 
4004     --bugfix:5697764
4005     ELSIF P_Calling_Mode = 'UNAPPLY PREPAY' THEN
4006 
4007        l_success := AP_ETAX_SERVICES_PKG.Calculate(
4008                                P_Invoice_id             => P_Invoice_Id,
4009                                P_Line_Number            => P_Line_Number,
4010                                P_Calling_Mode           => P_Calling_Mode,
4011                                P_All_Error_Messages     => P_All_Error_Messages,
4012                                P_Error_Code             => P_Error_Code,
4013                                P_Calling_Sequence       => l_curr_calling_sequence);
4014 
4015        IF (NOT l_success) THEN
4016            RAISE Tax_Exception;
4017        END IF;
4018 
4019     END IF;
4020 
4021     --bugfix:5697764
4022     IF P_Calling_Mode IN ('DISCARD LINE' ,'UNAPPLY PREPAY') THEN
4023 
4024         IF NOT cancel_invoice
4025                         (p_invoice_id   => p_invoice_id,
4026 			 p_line_number  => p_line_number,
4027 			 p_calling_mode => 'DISCARD') THEN
4028 
4029            l_return_status := FALSE;
4030 
4031         END IF;
4032 
4033     ELSE --   P_Calling_Mode NOT IN ('DISCARD LINE', 'UNAPPLY PREPAY')
4034 
4035 	-----------------------------------------------------------------
4036 	l_debug_info := 'Step 1: Populating invoice header local record';
4037     	-----------------------------------------------------------------
4038         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4039           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
4040         END IF;
4041 
4042 	BEGIN
4043       	    OPEN Invoice_Header;
4044       	   FETCH Invoice_Header INTO l_inv_header_rec;
4045       	   CLOSE Invoice_Header;
4046     	END;
4047 
4048         IF ((l_inv_header_rec.quick_credit = 'Y') OR    -- Bug 5660314
4049            (l_inv_header_rec.invoice_type_lookup_code IN ('AWT', 'INTEREST'))) THEN
4050           RETURN l_return_status;
4051         END IF;
4052 
4053 	-------------------------------------------------------------------
4054 	l_debug_info := 'Step 2: Get event class code';
4055     	-------------------------------------------------------------------
4056         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4057           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
4058         END IF;
4059 
4060     	IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
4061       	   	P_Invoice_Type_Lookup_Code => l_inv_header_rec.invoice_type_lookup_code,
4062 	   	P_Event_Class_Code         => l_event_class_code,
4063       	   	P_error_code               => P_error_code,
4064       		P_calling_sequence         => l_curr_calling_sequence)) THEN
4065 
4066       	   l_return_status := FALSE;
4067 
4068     	END IF;
4069 
4070     	-------------------------------------------------------------------
4071     	l_debug_info := 'Step 3: Get event type code';
4072     	-------------------------------------------------------------------
4073         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4074           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
4075         END IF;
4076 
4077     	IF (l_return_status = TRUE) THEN
4078       		IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
4079 		        P_Event_Class_Code          => l_event_class_code,
4080 		        P_Calling_Mode              => P_Calling_Mode,
4081 		        P_eTax_Already_called_flag  => NULL,
4082 		        P_Event_Type_Code           => l_Event_Type_Code,
4083 		        P_Error_Code                => P_error_code,
4084 		        P_Calling_Sequence          => l_curr_calling_sequence)) THEN
4085 
4086 	          l_return_status := FALSE;
4087 	       END IF;
4088 	END IF;
4089 
4090     	-------------------------------------------------------------------
4091 	l_debug_info := 'Step 4: Populate service specific parameter';
4092     	-------------------------------------------------------------------
4093         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4094           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
4095         END IF;
4096 
4097     	l_transaction_rec.internal_organization_id := l_inv_header_rec.org_id;
4098     	l_transaction_rec.application_id           := 200;
4099     	l_transaction_rec.entity_code              := 'AP_INVOICES';
4100     	l_transaction_rec.event_class_code         := l_event_class_code;
4101     	l_transaction_rec.event_type_code          := l_event_type_code;
4102     	l_transaction_rec.trx_id                   := l_inv_header_rec.invoice_id;
4103 
4104     	-------------------------------------------------------------------
4105 	l_debug_info := 'Step 5: Handle Cancel Invoice Mode';
4106     	-------------------------------------------------------------------
4107         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4108           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
4109         END IF;
4110 
4111         -- bug fix 6718488
4112         IF P_Calling_Mode = 'CANCEL INVOICE' THEN
4113 
4114           IF TAX_ONLY_LINE_EXIST(p_invoice_id) THEN
4115 
4116               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4117                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Tax Only Line Exists, before calling zx_api_pub.discard_tax_only_lines');
4118               END IF;
4119 
4120 	      zx_api_pub.discard_tax_only_lines(
4121 		        p_api_version        => 1.0,
4122 		        p_init_msg_list      => FND_API.G_TRUE,
4123 		        p_commit             => FND_API.G_FALSE,
4124 		        p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
4125 		        x_return_status      => l_return_status_service,
4126 		        x_msg_count          => l_msg_count,
4127 		        x_msg_data           => l_msg_data,
4128 		        p_transaction_rec    => l_transaction_rec );
4129 
4130             IF (l_return_status_service <> 'S') THEN  -- handle errors
4131 
4132 	      l_return_status := FALSE;
4133 	      -----------------------------------------------------------------
4134 	      l_debug_info := 'Step 5.5: Handle errors returned by API';
4135 	      -----------------------------------------------------------------
4136               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4137                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
4138               END IF;
4139 
4140 	      IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
4141 		               P_All_Error_Messages  => P_All_Error_Messages,
4142 		               P_Msg_Count           => l_msg_count,
4143 		               P_Msg_Data            => l_msg_data,
4144 		               P_Error_Code          => P_Error_Code,
4145 		               P_Calling_Sequence    => l_curr_calling_sequence)) THEN
4146 		        NULL;
4147 	      END IF;
4148 
4149               RETURN l_return_status;
4150             ELSE -- update the tax only line amount to 0
4151               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4152                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Update the tax only line amount to 0');
4153               END IF;
4154 
4155               UPDATE ap_invoice_lines_all a
4156               SET    amount = 0
4157               WHERE  a.invoice_id            = p_invoice_id
4158               AND    a.line_type_lookup_code = 'TAX'
4159               AND    exists
4160                       (select 'Tax Only Line'
4161                          from zx_lines_summary zx
4162                         where zx.summary_tax_line_id = a.summary_tax_line_id
4163                           and zx.tax_only_line_flag = 'Y');
4164 
4165             END IF;
4166 
4167           END IF;
4168 
4169 	  IF NOT cancel_invoice
4170 			(p_invoice_id   => p_invoice_id,
4171 			 p_line_number  => Null,
4172                          p_calling_mode => 'CANCEL') THEN
4173 
4174 	     l_return_status := FALSE;
4175 
4176 	  END IF;
4177 
4178 	END IF;
4179 
4180     	-------------------------------------------------------------------
4181     	l_debug_info := 'Step 6: Call to global_document_update service';
4182     	-------------------------------------------------------------------
4183     	IF ( l_return_status = TRUE ) THEN
4184 
4185 -- Debug messages added for 6321366
4186 IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4187               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'global_document_update values ');
4188               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'application_id: '           || l_transaction_rec.application_id);
4189               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'entity_code: ' || l_transaction_rec.entity_code);
4190               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: ' || l_transaction_rec.event_class_code);
4191               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_type_code: ' || l_transaction_rec.event_type_code);
4192               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '           || l_transaction_rec.trx_id);
4193 END IF;
4194 	      zx_api_pub.global_document_update(
4195 		        p_api_version        => 1.0,
4196 		        p_init_msg_list      => FND_API.G_TRUE,
4197 		        p_commit             => FND_API.G_FALSE,
4198 		        p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
4199 		        p_transaction_rec    => l_transaction_rec,
4200 		        x_return_status      => l_return_status_service,
4201 		        x_msg_count          => l_msg_count,
4202 		        x_msg_data           => l_msg_data);
4203 
4204 	END IF;
4205 
4206     	-----------------------------------------------------------------
4207     	l_debug_info := 'Step 7: Verify return status';
4208     	-----------------------------------------------------------------
4209     	IF (l_return_status_service <> 'S') THEN  -- handle errors
4210 
4211 	      l_return_status := FALSE;
4212 	      -----------------------------------------------------------------
4213 	      l_debug_info := 'Step 8: Handle errors returned by API';
4214 	      -----------------------------------------------------------------
4215 -- Debug messages added for 6321366
4216  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4217               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
4218  END IF;
4219 	      IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
4220 		               P_All_Error_Messages  => P_All_Error_Messages,
4221 		               P_Msg_Count           => l_msg_count,
4222 		               P_Msg_Data            => l_msg_data,
4223 		               P_Error_Code          => P_Error_Code,
4224 		               P_Calling_Sequence    => l_curr_calling_sequence)) THEN
4225 		        NULL;
4226 	      END IF;
4227 
4228 	END IF;
4229 
4230     END IF;
4231 
4232     RETURN l_return_status;
4233 
4234   EXCEPTION
4235     WHEN OTHERS THEN
4236       IF (SQLCODE <> -20001) THEN
4237         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
4238         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
4239         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
4240         FND_MESSAGE.SET_TOKEN('PARAMETERS',
4241           ' P_Invoice_Id = '||P_Invoice_Id||
4242           ' P_Calling_Mode ='||P_Calling_Mode||
4243           ' P_Error_Code = '||P_Error_Code||
4244           ' P_Calling_Sequence = '||P_Calling_Sequence);
4245         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
4246       END IF;
4247 
4248       APP_EXCEPTION.RAISE_EXCEPTION;
4249 
4250   END Global_Document_Update;
4251 
4252 
4253 /*=============================================================================
4254  |  FUNCTION - Release_Tax_Holds()
4255  |
4256  |  DESCRIPTION
4257  |      Public function that will call the global_document_update service to
4258  |      inform eTax the release of tax holds by the user.
4259  |      This function returns TRUE if the call to the service is successful.
4260  |      Otherwise, FALSE.
4261  |
4262  |  PARAMETERS
4263  |      P_Invoice_Id - invoice id
4264  |      P_Calling_Mode - calling mode.  Identifies which service to call
4265  |      P_Tax_Hold_Code - List of tax hold codes released in AP
4266  |                        Posible values: TAX VARIANCE and TAX AMOUNT RANGE
4267  |      P_All_Error_Messages - Should API return 1 error message or allow
4268  |                             calling point to get them from message stack
4269  |      P_error_code - Error code to be returned
4270  |      P_calling_sequence -  Calling sequence
4271  |
4272  |  MODIFICATION HISTORY
4273  |    DATE          Author         Action
4274  |    05-NOV-2003   SYIDNER        Created
4275  |
4276  *============================================================================*/
4277 
4278   FUNCTION Release_Tax_Holds(
4279              P_Invoice_id              IN NUMBER,
4280              P_Calling_Mode            IN VARCHAR2,
4281              P_Tax_Hold_Code           IN Rel_Hold_Codes_Type,
4282              P_All_Error_Messages      IN VARCHAR2,
4283              P_Error_Code              OUT NOCOPY VARCHAR2,
4284              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
4285   IS
4286 
4287     l_debug_info                 VARCHAR2(240);
4288     l_curr_calling_sequence      VARCHAR2(4000);
4289 
4290     l_inv_header_rec             ap_invoices_all%ROWTYPE;
4291     l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
4292     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
4293 
4294     CURSOR Invoice_Header IS
4295     SELECT *
4296       FROM ap_invoices_all
4297      WHERE invoice_id = P_Invoice_Id;
4298 
4299     l_transaction_rec             zx_api_pub.transaction_rec_type;
4300     l_validation_status           zx_api_pub.validation_status_tbl_type;
4301 
4302     l_return_status_service       VARCHAR2(4000);
4303     l_msg_count                   NUMBER;
4304     l_msg_data                    VARCHAR2(4000);
4305     l_msg                         VARCHAR2(4000);
4306 
4307     l_return_status               BOOLEAN := TRUE;
4308 
4309   BEGIN
4310 
4311     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Release_Tax_Holds<-' ||
4312                                P_calling_sequence;
4313 
4314     IF (P_Tax_Hold_Code.COUNT = 0 ) THEN
4315 
4316       RETURN TRUE;
4317     END IF;
4318 
4319     -----------------------------------------------------------------
4320     l_debug_info := 'Step 1: Populating invoice header local record';
4321     -----------------------------------------------------------------
4322 
4323     BEGIN
4324       OPEN Invoice_Header;
4325       FETCH Invoice_Header INTO l_inv_header_rec;
4326       CLOSE Invoice_Header;
4327     END;
4328 
4329     -------------------------------------------------------------------
4330     l_debug_info := 'Step 2: Get event class code';
4331     -------------------------------------------------------------------
4332 
4333     IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
4334       P_Invoice_Type_Lookup_Code => l_inv_header_rec.invoice_type_lookup_code,
4335       P_Event_Class_Code         => l_event_class_code,
4336       P_error_code               => P_error_code,
4337       P_calling_sequence         => l_curr_calling_sequence)) THEN
4338 
4339       l_return_status := FALSE;
4340 
4341     END IF;
4342 
4343     -------------------------------------------------------------------
4344     l_debug_info := 'Step 3: Get event type code';
4345     -------------------------------------------------------------------
4346     IF (l_return_status = TRUE) THEN
4347       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
4348         P_Event_Class_Code          => l_event_class_code,
4349         P_Calling_Mode              => P_Calling_Mode,
4350         P_eTax_Already_called_flag  => NULL,
4351         P_Event_Type_Code           => l_Event_Type_Code,
4352         P_Error_Code                => P_error_code,
4353         P_Calling_Sequence          => l_curr_calling_sequence)) THEN
4354 
4355         l_return_status := FALSE;
4356       END IF;
4357     END IF;
4358 
4359 
4360     -------------------------------------------------------------------
4361     l_debug_info := 'Step 4: Populate service specific parameter';
4362     -------------------------------------------------------------------
4363     l_transaction_rec.internal_organization_id := l_inv_header_rec.org_id;
4364     l_transaction_rec.application_id           := 200;
4365     l_transaction_rec.entity_code              := 'AP_INVOICES';
4366     l_transaction_rec.event_class_code         := l_event_class_code;
4367     l_transaction_rec.event_type_code          := l_event_type_code;
4368     l_transaction_rec.trx_id                   := l_inv_header_rec.invoice_id;
4369 
4370     -------------------------------------------------------------------
4371     l_debug_info := 'Step 5: Populate tax_hold_release_code ';
4372     -------------------------------------------------------------------
4373     IF ( l_return_status = TRUE) THEN
4374 
4375       IF ( P_Tax_Hold_Code.COUNT <> 0 ) THEN
4376         FOR i IN P_Tax_Hold_Code.FIRST..P_Tax_Hold_Code.LAST LOOP
4377           --  P_Tax_Hold_Code is populated with the tax hold codes to
4378           --  release.  Posible values are: TAX VARIANCE and TAX AMOUNT RANGE
4379           --  We need to pass to eTax the release code, so a conversion is
4380           --  required
4381 
4382           IF (P_Tax_Hold_Code(i) = 'TAX VARIANCE') THEN
4383             l_validation_status(i) :=  'TAX VARIANCE CORRECTED';
4384 
4385           ELSIF (P_Tax_Hold_Code(i) = 'TAX AMOUNT RANGE') THEN
4386             l_validation_status(i) :=  'TAX AMOUNT RANGE CORRECTED';
4387 
4388           END IF;
4389         END LOOP;
4390       END IF;
4391     END IF;
4392 
4393 
4394     -------------------------------------------------------------------
4395     l_debug_info := 'Step 6: Call to global_document_update service';
4396     -------------------------------------------------------------------
4397     IF ( l_return_status = TRUE ) THEN
4398       zx_api_pub.global_document_update(
4399         p_api_version        => 1.0,
4400         p_init_msg_list      => FND_API.G_TRUE,
4401         p_commit             => FND_API.G_FALSE,
4402         p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
4403         p_transaction_rec    => l_transaction_rec,
4404         p_validation_status  => l_validation_status,
4405         x_return_status      => l_return_status_service,
4406         x_msg_count          => l_msg_count,
4407         x_msg_data           => l_msg_data);
4408 
4409     END IF;
4410 
4411     -----------------------------------------------------------------
4412     l_debug_info := 'Step 7: Verify return status';
4413     -----------------------------------------------------------------
4414     IF (l_return_status_service <> 'S') THEN  -- handle errors
4415 
4416       l_return_status := FALSE;
4417       -----------------------------------------------------------------
4418       l_debug_info := 'Step 8: Handle errors returned by API';
4419       -----------------------------------------------------------------
4420 
4421       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
4422                P_All_Error_Messages  => P_All_Error_Messages,
4423                P_Msg_Count           => l_msg_count,
4424                P_Msg_Data            => l_msg_data,
4425                P_Error_Code          => P_Error_Code,
4426                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
4427         NULL;
4428       END IF;
4429     END IF;
4430 
4431     RETURN l_return_status;
4432 
4433   EXCEPTION
4434     WHEN OTHERS THEN
4435       IF (SQLCODE <> -20001) THEN
4436         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
4437         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
4438         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
4439         FND_MESSAGE.SET_TOKEN('PARAMETERS',
4440           ' P_Invoice_Id = '||P_Invoice_Id||
4441           ' P_Calling_Mode ='||P_Calling_Mode||
4442           ' P_Error_Code = '||P_Error_Code||
4443           ' P_Calling_Sequence = '||P_Calling_Sequence);
4444         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
4445       END IF;
4446 
4447       APP_EXCEPTION.RAISE_EXCEPTION;
4448 
4449   END Release_Tax_Holds;
4450 
4451 /*=============================================================================
4452  |  FUNCTION - Mark_Tax_Lines_Deleted()
4453  |
4454  |  DESCRIPTION
4455  |      Public function that will call the mark_tax_lines_deleted service.
4456  |      This API assumes the calling code controls the commit cycle.
4457  |      This function returns TRUE if the call to the service is successful.
4458  |      Otherwise, FALSE.
4459  |      This service should be called per invoice line.
4460  |
4461  |  PARAMETERS
4462  |      P_Invoice_Id - invoice id
4463  |      P_Calling_Mode - calling mode.  Identifies which service to call
4464  |      P_Line_Number_To_Delete - line number deleted in AP
4465  |      P_All_Error_Messages - Should API return 1 error message or allow
4466  |                             calling point to get them from message stack
4467  |      P_error_code - Error code to be returned
4468  |      P_calling_sequence -  Calling sequence
4469  |
4470  |  MODIFICATION HISTORY
4471  |    DATE          Author         Action
4472  |    07-OCT-2003   SYIDNER        Created
4473  |
4474  *============================================================================*/
4475 
4476   FUNCTION Mark_Tax_Lines_Deleted(
4477              P_Invoice_id              IN NUMBER,
4478              P_Calling_Mode            IN VARCHAR2,
4479              P_Line_Number_To_Delete   IN NUMBER,
4480              P_All_Error_Messages      IN VARCHAR2,
4481              P_Error_Code              OUT NOCOPY VARCHAR2,
4482              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
4483   IS
4484 
4485     l_debug_info                 VARCHAR2(240);
4486     l_curr_calling_sequence      VARCHAR2(4000);
4487 
4488     l_inv_header_rec             ap_invoices_all%ROWTYPE;
4489     l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
4490     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
4491 
4492     CURSOR Invoice_Header IS
4493     SELECT *
4494       FROM ap_invoices_all
4495      WHERE invoice_id = P_Invoice_Id;
4496 
4497     l_transaction_line_rec       zx_api_pub.transaction_line_rec_type;
4498 
4499     l_return_status_service      VARCHAR2(4000);
4500     l_msg_count                  NUMBER;
4501     l_msg_data                   VARCHAR2(4000);
4502     l_msg                        VARCHAR2(4000);
4503 
4504     l_return_status              BOOLEAN := TRUE;
4505 
4506   BEGIN
4507     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Mark_Tax_Lines_Deleted<-' ||
4508                                P_calling_sequence;
4509 
4510     -----------------------------------------------------------------
4511     l_debug_info := 'Step 1: Populating invoice header local record';
4512     -----------------------------------------------------------------
4513 
4514     BEGIN
4515       OPEN Invoice_Header;
4516       FETCH Invoice_Header INTO l_inv_header_rec;
4517       CLOSE Invoice_Header;
4518     END;
4519 
4520 
4521     -------------------------------------------------------------------
4522     l_debug_info := 'Step 2: Get event class code';
4523     -------------------------------------------------------------------
4524 
4525     IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
4526       P_Invoice_Type_Lookup_Code => l_inv_header_rec.invoice_type_lookup_code,
4527       P_Event_Class_Code         => l_event_class_code,
4528       P_error_code               => P_error_code,
4529       P_calling_sequence         => l_curr_calling_sequence)) THEN
4530 
4531       l_return_status := FALSE;
4532 
4533     END IF;
4534 
4535     -------------------------------------------------------------------
4536     l_debug_info := 'Step 3: Get event type code';
4537     -------------------------------------------------------------------
4538     IF (l_return_status = TRUE) THEN
4539       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
4540         P_Event_Class_Code          => l_event_class_code,
4541         P_Calling_Mode              => P_Calling_Mode,
4542         P_eTax_Already_called_flag  => NULL,
4543         P_Event_Type_Code           => l_Event_Type_Code,
4544         P_Error_Code                => P_error_code,
4545         P_Calling_Sequence          => l_curr_calling_sequence)) THEN
4546 
4547         l_return_status := FALSE;
4548       END IF;
4549     END IF;
4550 
4551     -------------------------------------------------------------------
4552     l_debug_info := 'Step 4: Populate service specific parameter';
4553     -------------------------------------------------------------------
4554     l_transaction_line_rec.internal_organization_id := l_inv_header_rec.org_id;
4555     l_transaction_line_rec.application_id           := 200;
4556     l_transaction_line_rec.entity_code              := 'AP_INVOICES';
4557     l_transaction_line_rec.event_class_code         := l_event_class_code;
4558     l_transaction_line_rec.event_type_code          := l_event_type_code;
4559     l_transaction_line_rec.trx_id                   := P_Invoice_Id;
4560     l_transaction_line_rec.trx_level_type           := 'LINE';
4561     l_transaction_line_rec.trx_line_id              := P_Line_Number_To_Delete;
4562 
4563     -------------------------------------------------------------------
4564     l_debug_info := 'Step 6: Call to mark_tax_lines_deleted';
4565     -------------------------------------------------------------------
4566     IF ( l_return_status = TRUE ) THEN
4567 
4568       zx_api_pub.mark_tax_lines_deleted(
4569         p_api_version             => 1.0,
4570         p_init_msg_list           => FND_API.G_TRUE,
4571         p_commit                  => FND_API.G_FALSE,
4572         p_validation_level        => FND_API.G_VALID_LEVEL_FULL,
4573         p_transaction_line_rec    => l_transaction_line_rec,
4574         x_return_status           => l_return_status_service,
4575         x_msg_count               => l_msg_count,
4576         x_msg_data                => l_msg_data);
4577 
4578     END IF;
4579 
4580     -----------------------------------------------------------------
4581     l_debug_info := 'Step 7: Verify return status';
4582     -----------------------------------------------------------------
4583     IF (l_return_status_service <> 'S') THEN  -- handle errors
4584 
4585       l_return_status := FALSE;
4586       -----------------------------------------------------------------
4587       l_debug_info := 'Step 8: Handle errors returned by API';
4588       -----------------------------------------------------------------
4589 
4590       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
4591                P_All_Error_Messages  => P_All_Error_Messages,
4592                P_Msg_Count           => l_msg_count,
4593                P_Msg_Data            => l_msg_data,
4594                P_Error_Code          => P_Error_Code,
4595                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
4596         NULL;
4597       END IF;
4598 
4599     END IF;
4600     RETURN l_return_status;
4601 
4602   EXCEPTION
4603     WHEN OTHERS THEN
4604       IF (SQLCODE <> -20001) THEN
4605         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
4606         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
4607         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
4608         FND_MESSAGE.SET_TOKEN('PARAMETERS',
4609           ' P_Invoice_Id = '||P_Invoice_Id||
4610           ' P_Line_Number_To_Delete = '||P_Line_Number_To_Delete||
4611           ' P_Calling_Mode ='||P_Calling_Mode||
4612           ' P_Error_Code = '||P_Error_Code||
4613           ' P_Calling_Sequence = '||P_Calling_Sequence);
4614         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
4615       END IF;
4616 
4617       APP_EXCEPTION.RAISE_EXCEPTION;
4618 
4619   END Mark_Tax_Lines_Deleted;
4620 
4621 /*=============================================================================
4622  |  FUNCTION - Validate_Invoice()
4623  |
4624  |  DESCRIPTION
4625  |      Public function that will call the validate_document_for_tax service.
4626  |      This API assumes the calling code controls the commit cycle.
4627  |      This function returns TRUE if the call to the service is successful.
4628  |      Otherwise, FALSE.
4629  |
4630  |  PARAMETERS
4631  |      P_Invoice_Id - invoice id
4632  |      P_Calling_Mode - calling mode.  Identifies which service to call
4633  |      P_All_Error_Messages - Should API return 1 error message or allow
4634  |                             calling point to get them from message stack
4635  |      P_error_code - Error code to be returned
4636  |      P_calling_sequence -  Calling sequence
4637  |
4638  |  MODIFICATION HISTORY
4639  |    DATE          Author         Action
4640  |    07-OCT-2003   SYIDNER        Created
4641  |
4642  *============================================================================*/
4643 
4644   FUNCTION Validate_Invoice(
4645              P_Invoice_id              IN NUMBER,
4646              P_Calling_Mode            IN VARCHAR2,
4647              P_All_Error_Messages      IN VARCHAR2,
4648              P_Error_Code              OUT NOCOPY VARCHAR2,
4649              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN IS
4650 
4651 
4652     l_debug_info                 VARCHAR2(240);
4653     l_curr_calling_sequence      VARCHAR2(4000);
4654 
4655     l_inv_header_rec             ap_invoices_all%ROWTYPE;
4656     l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
4657     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
4658 
4659     CURSOR Invoice_Header IS
4660     SELECT *
4661       FROM ap_invoices_all
4662      WHERE invoice_id = P_Invoice_Id;
4663 
4664     l_transaction_rec             zx_api_pub.transaction_rec_type;
4665 
4666 --    l_validation_status_tab       zx_api_pub.validation_status_tbl_type;
4667     l_validation_status		  VARCHAR2(1);
4668     l_hold_codes_tab		  zx_api_pub.hold_codes_tbl_type;
4669 
4670     l_return_status_service       VARCHAR2(4000);
4671     l_msg_count                   NUMBER;
4672     l_msg_data                    VARCHAR2(4000);
4673     l_msg                         VARCHAR2(4000);
4674 
4675     l_return_status               BOOLEAN := TRUE;
4676 
4677     TYPE hold_lookup_code_tab IS TABLE OF ap_holds_all.hold_lookup_code%TYPE;
4678     l_hold_lookup_code            hold_lookup_code_tab;
4679     l_release_lookup_code         ap_holds_all.release_lookup_code%TYPE;
4680     l_api_name			 varchar2(30);
4681     --Bug 7410237 start
4682     l_system_user                 NUMBER := 5;
4683 
4684     l_holds                       AP_APPROVAL_PKG.HOLDSARRAY;
4685     l_hold_count                  AP_APPROVAL_PKG.COUNTARRAY;
4686     l_release_count               AP_APPROVAL_PKG.COUNTARRAY;
4687     --Bug 7410237 End
4688 
4689 
4690   BEGIN
4691 
4692     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Validate_Invoice<-' ||
4693                                P_calling_sequence;
4694 
4695     l_api_name := 'Validate_Invoice';
4696     -----------------------------------------------------------------
4697     l_debug_info := 'Step 1: Populating invoice header local record';
4698     -----------------------------------------------------------------
4699 
4700     BEGIN
4701       OPEN Invoice_Header;
4702       FETCH Invoice_Header INTO l_inv_header_rec;
4703       CLOSE Invoice_Header;
4704     END;
4705 
4706     IF ((l_inv_header_rec.quick_credit = 'Y') OR    -- Bug 5660314
4707         (l_inv_header_rec.invoice_type_lookup_code IN ('AWT', 'INTEREST'))) THEN
4708       RETURN l_return_status;
4709     END IF;
4710 
4711 
4712     -------------------------------------------------------------------
4713     l_debug_info := 'Step 2: Get event class code';
4714     -------------------------------------------------------------------
4715 
4716     IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
4717       P_Invoice_Type_Lookup_Code => l_inv_header_rec.invoice_type_lookup_code,
4718       P_Event_Class_Code         => l_event_class_code,
4719       P_error_code               => P_error_code,
4720       P_calling_sequence         => l_curr_calling_sequence)) THEN
4721 
4722       l_return_status := FALSE;
4723 
4724     END IF;
4725 
4726     -------------------------------------------------------------------
4727     l_debug_info := 'Step 3: Get event type code';
4728     -------------------------------------------------------------------
4729     IF (l_return_status = TRUE) THEN
4730       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
4731         P_Event_Class_Code          => l_event_class_code,
4732         P_Calling_Mode              => P_Calling_Mode,
4733         P_eTax_Already_called_flag  => NULL,
4734         P_Event_Type_Code           => l_Event_Type_Code,
4735         P_Error_Code                => P_error_code,
4736         P_Calling_Sequence          => l_curr_calling_sequence)) THEN
4737 
4738         l_return_status := FALSE;
4739       END IF;
4740     END IF;
4741 
4742     -------------------------------------------------------------------
4743     l_debug_info := 'Step 4: Populate service specific parameter';
4744     -------------------------------------------------------------------
4745     l_transaction_rec.internal_organization_id := l_inv_header_rec.org_id;
4746     l_transaction_rec.application_id           := 200;
4747     l_transaction_rec.entity_code              := 'AP_INVOICES';
4748     l_transaction_rec.event_class_code         := l_event_class_code;
4749     l_transaction_rec.event_type_code          := l_event_type_code;
4750     l_transaction_rec.trx_id                   := l_inv_header_rec.invoice_id;
4751 
4752     -------------------------------------------------------------------
4753     l_debug_info := 'Step 5: Call validate_document_for_tax service';
4754     -------------------------------------------------------------------
4755     IF ( l_return_status = TRUE ) THEN
4756 
4757       zx_api_pub.validate_document_for_tax(
4758         p_api_version        => 1.0,
4759         p_init_msg_list      => FND_API.G_TRUE,
4760         p_commit             => FND_API.G_FALSE,
4761         p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
4762         p_transaction_rec    => l_transaction_rec,
4763         x_validation_status  => l_validation_status,
4764 	x_hold_codes_tbl     => l_hold_codes_tab,
4765         x_return_status      => l_return_status_service,
4766         x_msg_count          => l_msg_count,
4767         x_msg_data           => l_msg_data);
4768 
4769     END IF;
4770 
4771     -----------------------------------------------------------------
4772     l_debug_info := 'Step 6: Verify return status';
4773     -----------------------------------------------------------------
4774     IF (l_return_status_service = 'S') THEN
4775 
4776       l_debug_info := 'l_hold_codes_tab.count is '||l_hold_codes_tab.count;
4777       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4778           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4779       END IF;
4780 
4781       -----------------------------------------------------------------
4782       l_debug_info := 'Step 7: Check for tax holds on invoice';
4783       -----------------------------------------------------------------
4784       --bugfix:5523240 Replaced the IF condition as the etax service
4785       --returns 'Y' eventhough tax holds are place
4786       --IF ( l_validation_status = 'N' ) THEN
4787       IF(l_hold_codes_tab.count = 0) THEN
4788             -----------------------------------------------------------------
4789             l_debug_info := 'Step 8: Verify if invoice has no released tax '||
4790                             ' holds';
4791             IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4792                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4793             END IF;
4794             -----------------------------------------------------------------
4795             BEGIN
4796               SELECT hold_lookup_code
4797                 BULK COLLECT INTO l_hold_lookup_code
4798                 FROM ap_holds_all
4799                WHERE invoice_id = l_inv_header_rec.invoice_id
4800                  AND org_id = l_inv_header_rec.org_id
4801                  AND hold_lookup_code IN ('TAX VARIANCE', 'TAX AMOUNT RANGE')
4802                  AND release_lookup_code is NULL;
4803 
4804             EXCEPTION
4805               WHEN NO_DATA_FOUND THEN NULL;
4806             END;
4807 
4808             IF (l_hold_lookup_code.COUNT <> 0) THEN
4809                FOR i IN l_hold_lookup_code.FIRST..l_hold_lookup_code.LAST LOOP
4810 
4811 		 ----------------------------------------------------------------------------
4812 	         l_debug_info := 'Release tax holds';
4813                  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4814                    FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4815                  END IF;
4816 		 ----------------------------------------------------------------------------
4817                 -- Bug 7410237 Start
4818 
4819                 /*
4820                  IF (l_hold_lookup_code(i) = 'TAX VARIANCE') THEN
4821                   l_release_lookup_code := 'VARIANCE CORRECTED';
4822 
4823                  ELSE
4824                    l_release_lookup_code := 'HOLDS QUICK RELEASED';
4825 
4826                  END IF;
4827 
4828                  ap_holds_pkg.release_single_hold(
4829                    X_invoice_id              => l_inv_header_rec.invoice_id,
4830                    X_hold_lookup_code        => l_hold_lookup_code(i),
4831                    X_release_lookup_code     => l_release_lookup_code,
4832                    X_held_by                 => NULL,
4833                    X_calling_sequence        => l_curr_calling_sequence);
4834                  */
4835 
4836                   AP_APPROVAL_PKG.Process_Inv_Hold_Status(
4837                         p_invoice_id              =>   l_inv_header_rec.invoice_id,
4838                         p_line_location_id        =>   NULL,
4839                         p_rcv_transaction_id      =>   NULL,
4840                         p_hold_lookup_code        =>   l_hold_lookup_code(i),
4841                         p_should_have_hold        =>   'N',
4842                         p_hold_reason             =>   NULL,
4843                         p_system_user             =>   l_system_user,
4844                         p_holds                   =>   l_holds,
4845                         p_holds_count             =>   l_hold_count,
4846                         p_release_count           =>   l_release_count,
4847                         p_calling_sequence        =>   l_curr_calling_sequence);
4848 
4849                    -- Bug 7410237 End
4850                END LOOP;
4851             END IF;
4852 
4853         ELSIF (l_hold_codes_tab.count <> 0) THEN
4854            FOR i IN l_hold_codes_tab.FIRST..l_hold_codes_tab.LAST LOOP
4855 
4856               l_debug_info := 'l_hold_codes_tab(i) is '||l_hold_codes_tab(i);
4857               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4858                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4859               END IF;
4860 
4861               -----------------------------------------------------------------
4862               l_debug_info := 'Step 9: Create tax hold in AP if not exists';
4863               -----------------------------------------------------------------
4864               -- the posible values eTax will populate l_validation_status_tab
4865               -- with are:  TAX VARIANCE and TAX AMOUNT RANGE.  These are the same
4866               -- holds lookup codes used by API so there is no need to convert any
4867               -- value here
4868               -- Bug 7410237 Start
4869               /*
4870               ap_holds_pkg.insert_single_hold(
4871            	   X_invoice_id          => l_inv_header_rec.invoice_id,
4872               	   X_hold_lookup_code    => l_hold_codes_tab(i),
4873                    X_hold_type           => NULL,
4874                    X_hold_reason         => NULL,
4875                    X_held_by             => l_user_id,
4876                    X_calling_sequence    => l_curr_calling_sequence);
4877               */
4878                   AP_APPROVAL_PKG.Process_Inv_Hold_Status(
4879                         p_invoice_id              =>   l_inv_header_rec.invoice_id,
4880                         p_line_location_id        =>   NULL,
4881                         p_rcv_transaction_id      =>   NULL,
4882                         p_hold_lookup_code        =>   l_hold_codes_tab(i),
4883                         p_should_have_hold        =>   'Y',
4884                         p_hold_reason             =>   NULL,
4885                         p_system_user             =>   l_system_user,
4886                         p_holds                   =>   l_holds,
4887                         p_holds_count             =>   l_hold_count,
4888                         p_release_count           =>   l_release_count,
4889                         p_calling_sequence        =>   l_curr_calling_sequence);
4890 
4891                -- Bug 7410237 End
4892           END LOOP;
4893         END IF;
4894 
4895    ELSE  -- handle errors
4896 
4897       l_return_status := FALSE;
4898       -----------------------------------------------------------------
4899       l_debug_info := 'Step 8: Handle errors returned by API';
4900       -----------------------------------------------------------------
4901 
4902       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
4903                P_All_Error_Messages  => P_All_Error_Messages,
4904                P_Msg_Count           => l_msg_count,
4905                P_Msg_Data            => l_msg_data,
4906                P_Error_Code          => P_Error_Code,
4907                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
4908         NULL;
4909       END IF;
4910     END IF;
4911 
4912     RETURN l_return_status;
4913 
4914   END Validate_Invoice;
4915 
4916 /*=============================================================================
4917  |  FUNCTION - Validate_Default_Import()
4918  |
4919  |  DESCRIPTION
4920  |      Public function that will call the validate_and_default_tax_attr service.
4921  |      This API assumes the calling code controls the commit cycle.
4922  |      This function returns TRUE if the call to the service is successful.
4923  |      Otherwise, FALSE.
4924  |      This API will validate the taxable and tax lines to be imported regarding
4925  |      tax.  The lines will be passed to this API using the pl/sql structures
4926  |      defined in the import process.
4927  |      The service validate_and_default_tax_attr will default any possible tax
4928  |      value, and this API will modify the pl/sql structures with the defaulted
4929  |      tax info.
4930  |
4931  |  PARAMETERS
4932  |      p_invoice_rec - record defined in the import program for the invoice header
4933  |      p_invoice_lines_tab - array with the taxable and tax lines
4934  |      P_Calling_Mode - calling mode.  Identifies which service to call
4935  |      P_All_Error_Messages - Should API return 1 error message or allow
4936  |                             calling point to get them from message stack
4937  |      p_invoice_status - returns N if the invoice should be rejected.
4938  |      P_error_code - Error code to be returned
4939  |      P_calling_sequence -  Calling sequence
4940  |
4941  |  MODIFICATION HISTORY
4942  |    DATE          Author         Action
4943  |    20-JAN-2004   SYIDNER        Created
4944  |
4945  *============================================================================*/
4946   FUNCTION Validate_Default_Import(
4947              P_Invoice_Rec             IN OUT NOCOPY
4948                AP_IMPORT_INVOICES_PKG.r_invoice_info_rec,
4949              p_invoice_lines_tab       IN OUT NOCOPY
4950                AP_IMPORT_INVOICES_PKG.t_lines_table,
4951              P_Calling_Mode            IN VARCHAR2,
4952              P_All_Error_Messages      IN VARCHAR2,
4953              p_invoice_status          OUT NOCOPY VARCHAR2,
4954              P_Error_Code              OUT NOCOPY VARCHAR2,
4955              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
4956   IS
4957     l_debug_info                 VARCHAR2(240);
4958     l_curr_calling_sequence      VARCHAR2(4000);
4959 
4960     l_event_class_code
4961       zx_trx_headers_gt.event_class_code%TYPE;
4962     l_event_type_code
4963       zx_trx_headers_gt.event_type_code%TYPE;
4964 
4965     l_return_status_service       VARCHAR2(4000);
4966     l_msg_count                   NUMBER;
4967     l_msg_data                    VARCHAR2(4000);
4968     l_msg                         VARCHAR2(4000);
4969 
4970     l_return_status               BOOLEAN := TRUE;
4971 
4972   BEGIN
4973     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Validate_Default_Import<-'
4974                                ||P_calling_sequence;
4975 
4976     -----------------------------------------------------------------
4977     l_debug_info := 'Step 1: Populate Header';
4978     -----------------------------------------------------------------
4979     IF NOT(AP_ETAX_SERVICES_PKG.Populate_Header_Import_GT(
4980       P_Invoice_Header_Rec         => P_Invoice_Rec,
4981       P_Calling_Mode               => P_Calling_Mode,
4982       P_Event_Class_Code           => l_event_class_code,
4983       P_Event_Type_Code            => l_event_type_code,
4984       P_Error_Code                 => P_error_code,
4985       P_Calling_Sequence           => l_curr_calling_sequence )) THEN
4986 
4987       l_return_status := FALSE;
4988     END IF;
4989 
4990     -----------------------------------------------------------------
4991     l_debug_info := 'Step 2: Populate Trx and Tax Lines and allocation '||
4992                     'structure ';
4993     -----------------------------------------------------------------
4994     IF ( l_return_status = TRUE ) THEN
4995       IF NOT(AP_ETAX_SERVICES_PKG.Populate_Lines_Import_GT(
4996         P_Invoice_Header_Rec      => P_Invoice_Rec,
4997         P_Inv_Line_List           => p_invoice_lines_tab,
4998         P_Calling_Mode            => P_Calling_Mode,
4999         P_Event_Class_Code        => l_event_class_code,
5000         P_Error_Code              => P_error_code,
5001         P_Calling_Sequence        => l_curr_calling_sequence )) THEN
5002 
5003         l_return_status := FALSE;
5004       END IF;
5005     END IF;
5006 
5007     -----------------------------------------------------------------
5008     l_debug_info := 'Step 3: Call validate_and_default_tax_attr service';
5009     -----------------------------------------------------------------
5010     IF ( l_return_status = TRUE ) THEN
5011 
5012       zx_api_pub.validate_and_default_tax_attr(
5013         p_api_version      => 1.0,
5014         p_init_msg_list    => FND_API.G_TRUE,
5015         p_commit           => FND_API.G_FALSE,
5016         p_validation_level => FND_API.G_VALID_LEVEL_FULL,
5017         x_return_status    => l_return_status_service,
5018         x_msg_count        => l_msg_count,
5019         x_msg_data         => l_msg_data);
5020 
5021     END IF;
5022 
5023     IF (l_return_status_service = 'S') THEN
5024       -----------------------------------------------------------------
5025       l_debug_info := 'Step 4: Handle return of tax lines';
5026       -----------------------------------------------------------------
5027       IF NOT (AP_ETAX_UTILITY_PKG.Return_Default_Import(
5028                 P_Invoice_Header_Rec      => P_Invoice_Rec,
5029                 P_Invoice_Lines_Tab       => p_invoice_lines_tab,
5030                 P_All_Error_Messages      => 'N',
5031                 P_Error_Code              => P_Error_Code,
5032                 P_Calling_Sequence        => l_curr_calling_sequence,
5033 		P_invoice_status	  => p_invoice_status --Bug6625518
5034 		)) THEN
5035 
5036 
5037         l_return_status := FALSE;
5038       END IF;
5039 
5040       --Bug6625518 Commenting the assignment below.
5041       --This was making the flag for processing as 'Y' even in case of rejections.
5042       --p_invoice_status := 'Y';
5043 
5044     ELSE  -- handle errors
5045 
5046       -----------------------------------------------------------------
5047       l_debug_info := 'Step 5: Handle errors returned by API';
5048       -----------------------------------------------------------------
5049       l_return_status := FALSE;
5050 
5051       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
5052                P_All_Error_Messages  => P_All_Error_Messages,
5053                P_Msg_Count           => l_msg_count,
5054                P_Msg_Data            => l_msg_data,
5055                P_Error_Code          => P_Error_Code,
5056                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
5057         NULL;
5058 
5059       END IF;
5060 
5061       p_invoice_status := 'N';
5062 
5063     END IF;
5064 
5065    RETURN l_return_status;
5066 
5067   EXCEPTION
5068     WHEN OTHERS THEN
5069       IF (SQLCODE <> -20001) THEN
5070         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
5071         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
5072         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
5073         FND_MESSAGE.SET_TOKEN('PARAMETERS',
5074           ' Invoice_Id = '||P_Invoice_Rec.Invoice_Id||
5075           ' P_Calling_Mode ='||P_Calling_Mode||
5076           ' P_Error_Code = '||P_Error_Code||
5077           ' P_Calling_Sequence = '||P_Calling_Sequence);
5078         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
5079       END IF;
5080 
5081       APP_EXCEPTION.RAISE_EXCEPTION;
5082 
5083   END Validate_Default_Import;
5084 
5085 
5086 /*=============================================================================
5087  |  FUNCTION - Populate_Headers_GT()
5088  |
5089  |  DESCRIPTION
5090  |      This function will get additional information required to populate the
5091  |      zx_trx_headers_gt
5092  |      This function returns TRUE if the insert to the temp table goes
5093  |      through successfully.  Otherwise, FALSE.
5094  |
5095  |  PARAMETERS
5096  |      P_Invoice_Header_Rec - record with invoice header information
5097  |      P_Calling_Mode - calling mode. it is used to
5098  |      P_eTax_Already_called_flag - Flag to know if this is the first time tax
5099  |                                   has been called
5100  |      P_Event_Class_Code - Event class code
5101  |      P_Event_Type_Code - Event type code
5102  |      P_error_code - Error code to be returned
5103  |      P_calling_sequence -  Calling sequence
5104  |
5105  |  MODIFICATION HISTORY
5106  |    DATE          Author         Action
5107  |    07-OCT-2003   SYIDNER        Created
5108  |
5109  *============================================================================*/
5110 
5111   FUNCTION Populate_Headers_GT(
5112              P_Invoice_Header_Rec        IN ap_invoices_all%ROWTYPE,
5113              P_Calling_Mode              IN VARCHAR2,
5114              P_eTax_Already_called_flag  IN VARCHAR2,
5115              P_Event_Class_Code          OUT NOCOPY VARCHAR2,
5116              P_Event_Type_Code           OUT NOCOPY VARCHAR2,
5117              P_Error_Code                OUT NOCOPY VARCHAR2,
5118              P_Calling_Sequence          IN VARCHAR2) RETURN BOOLEAN
5119   IS
5120 
5121     l_debug_info                 VARCHAR2(240);
5122     l_curr_calling_sequence      VARCHAR2(4000);
5123 
5124     l_application_id             zx_trx_headers_gt.application_id%TYPE;
5125     l_entity_code                zx_trx_headers_gt.entity_code%TYPE;
5126 
5127     l_quote_flag                 zx_trx_headers_gt.quote_flag%TYPE := 'N';
5128     -- This flag is always N except when the calculate service is called for
5129     -- quote for the recurring invoices and distributions sets.
5130 
5131     CURSOR tax_related_invoice( c_tax_related_invoice_id IN NUMBER) IS
5132 -- B# 6907814 ...  SELECT invoice_num, invoice_date, invoice_type_lookup_code
5133     SELECT invoice_num, invoice_type_lookup_code, invoice_date
5134       FROM ap_invoices_all
5135      WHERE invoice_id = c_tax_related_invoice_id;
5136 
5137     l_related_inv_application_id
5138       zx_trx_headers_gt.related_doc_application_id%TYPE;
5139     l_related_inv_entity_code
5140       zx_trx_headers_gt.related_doc_entity_code%TYPE;
5141     l_related_event_class_code
5142       zx_trx_headers_gt.related_doc_event_class_code%TYPE;
5143     l_related_inv_number         ap_invoices_all.invoice_num%TYPE;
5144     l_related_inv_date           ap_invoices_all.invoice_date%TYPE;
5145     l_related_inv_type           ap_invoices_all.invoice_type_lookup_code%TYPE;
5146 
5147     l_precision                  fnd_currencies.precision%TYPE;
5148     l_minimum_accountable_unit   fnd_currencies.minimum_accountable_unit%TYPE;
5149     l_doc_seq_name		 fnd_document_sequences.db_sequence_name%TYPE;
5150 
5151     l_return_status              BOOLEAN := TRUE;
5152     l_api_name                  VARCHAR2(30) := 'Populate_headers_gt'; --bug 6321366
5153   BEGIN
5154 
5155     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Populate_Headers_GT<-' ||
5156                                P_calling_sequence;
5157 
5158     -------------------------------------------------------------------
5159     l_debug_info := 'Step 1: Get event class code';
5160     -------------------------------------------------------------------
5161 
5162     IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
5163       P_Invoice_Type_Lookup_Code => P_Invoice_Header_Rec.invoice_type_lookup_code,
5164       P_Event_Class_Code         => P_event_class_code,
5165       P_error_code               => P_error_code,
5166       P_calling_sequence         => l_curr_calling_sequence)) THEN
5167 
5168       l_return_status := FALSE;
5169 
5170     END IF;
5171 
5172     -------------------------------------------------------------------
5173     l_debug_info := 'Step 2: Get event type code';
5174     -------------------------------------------------------------------
5175 
5176     IF (l_return_status = TRUE) THEN
5177       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
5178         P_Event_Class_Code          => P_event_class_code,
5179         P_Calling_Mode              => P_Calling_Mode,
5180         P_eTax_Already_called_flag  => P_eTax_Already_called_flag,
5181         P_Event_Type_Code           => P_Event_Type_Code,
5182         P_Error_Code                => P_error_code,
5183         P_Calling_Sequence          => l_curr_calling_sequence)) THEN
5184 
5185         l_return_status := FALSE;
5186       END IF;
5187     END IF;
5188 
5189     -------------------------------------------------------------------
5190     l_debug_info := 'Step 3: Populate product specific attributes';
5191     -------------------------------------------------------------------
5192     l_application_id := 200;   -- Oracle Payables
5193     -- The same code is used for all invoice types.
5194     l_entity_code := 'AP_INVOICES';
5195 
5196 
5197     -------------------------------------------------------------------
5198     l_debug_info := 'Step 4: Populate tax related invoice information '||
5199                     'if tax_related_invoice_id is not null';
5200     -------------------------------------------------------------------
5201     IF (l_return_status = TRUE AND
5202         P_Invoice_Header_Rec.tax_related_invoice_id IS NOT NULL) THEN
5203       l_related_inv_application_id := 200;  --Oracle Payables
5204       l_related_inv_entity_code := 'AP_INVOICES';
5205 
5206 
5207       BEGIN
5208         OPEN tax_related_invoice(P_Invoice_Header_Rec.tax_related_invoice_id);
5209         FETCH tax_related_invoice
5210           INTO l_related_inv_number, l_related_inv_type,
5211                l_related_inv_date;
5212         CLOSE tax_related_invoice;
5213       END;
5214 
5215       --------------------------------------------------------------------------
5216       l_debug_info := 'Step 5: Get event class code for tax_related_invoice_id';
5217       --------------------------------------------------------------------------
5218       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
5219         P_Invoice_Type_Lookup_Code => l_related_inv_type,
5220         P_Event_Class_Code         => l_related_event_class_code,
5221         P_error_code               => P_error_code,
5222         P_calling_sequence         => l_curr_calling_sequence)) THEN
5223 
5224         l_return_status := FALSE;
5225       END IF;
5226     END IF;
5227 
5228     -------------------------------------------------------------------
5229     l_debug_info := 'Step 6: Set quote flag based on calling_mode';
5230     -------------------------------------------------------------------
5231     IF (p_calling_mode = 'CALCULATE QUOTE') THEN
5232       l_quote_flag := 'Y';
5233 
5234     END IF;
5235 
5236     -------------------------------------------------------------------
5237     l_debug_info := 'Step 7: Get transaction currency details';
5238     -------------------------------------------------------------------
5239     IF (l_return_status = TRUE) THEN
5240       BEGIN
5241         SELECT NVL(precision, 0), NVL(minimum_accountable_unit,(1/power(10,precision)))
5242           INTO l_precision, l_minimum_accountable_unit
5243           FROM fnd_currencies
5244          WHERE currency_code = P_Invoice_Header_Rec.invoice_currency_code;
5245 
5246       END;
5247     END IF;
5248 
5249     -------------------------------------------------------------------
5250     l_debug_info := 'Step 8: Get doc_sequence_name';
5251     -------------------------------------------------------------------
5252     IF (l_return_status = TRUE
5253         and p_invoice_header_rec.doc_sequence_id is not null) THEN
5254       BEGIN
5255         SELECT name
5256           INTO l_doc_seq_name
5257           FROM fnd_document_sequences
5258          WHERE doc_sequence_id = p_invoice_header_rec.doc_sequence_id;
5259 
5260       EXCEPTION
5261 	WHEN OTHERS THEN
5262 	     Null;
5263       END;
5264     END IF;
5265 
5266     -------------------------------------------------------------------
5267     l_debug_info := 'Step 9: Populate zx_trx_headers_gt';
5268     -------------------------------------------------------------------
5269     IF (l_return_status = TRUE) THEN
5270 
5271       DELETE FROM zx_trx_headers_gt
5272        WHERE application_id   = AP_ETAX_PKG.AP_APPLICATION_ID
5273          AND entity_code      = AP_ETAX_PKG.AP_ENTITY_CODE
5274          AND event_class_code IN (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
5275                                   AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
5276                                   AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
5277          AND trx_id = p_invoice_header_rec.invoice_id;
5278 
5279       INSERT INTO zx_trx_headers_gt(
5280         internal_organization_id,
5281         internal_org_location_id,
5282         application_id,
5283         entity_code,
5284         event_class_code,
5285         event_type_code,
5286         trx_id,
5287         hdr_trx_user_key1,
5288         hdr_trx_user_key2,
5289         hdr_trx_user_key3,
5290         hdr_trx_user_key4,
5291         hdr_trx_user_key5,
5292         hdr_trx_user_key6,
5293         trx_date,
5294         trx_doc_revision,
5295         ledger_id,
5296         trx_currency_code,
5297         currency_conversion_date,
5298         currency_conversion_rate,
5299         currency_conversion_type,
5300         minimum_accountable_unit,
5301         precision,
5302         legal_entity_id,
5303         rounding_ship_to_party_id,
5304         rounding_ship_from_party_id,
5305         rounding_bill_to_party_id,
5306         rounding_bill_from_party_id,
5307         rndg_ship_to_party_site_id,
5308         rndg_ship_from_party_site_id,
5309         rndg_bill_to_party_site_id,
5310         rndg_bill_from_party_site_id,
5311         establishment_id,
5312         receivables_trx_type_id,
5313         related_doc_application_id,
5314         related_doc_entity_code,
5315         related_doc_event_class_code,
5316         related_doc_trx_id,
5317         rel_doc_hdr_trx_user_key1,
5318         rel_doc_hdr_trx_user_key2,
5319         rel_doc_hdr_trx_user_key3,
5320         rel_doc_hdr_trx_user_key4,
5321         rel_doc_hdr_trx_user_key5,
5322         rel_doc_hdr_trx_user_key6,
5323         related_doc_number,
5324         related_doc_date,
5325         default_taxation_country,
5326         quote_flag,
5327         ctrl_total_hdr_tx_amt,
5328         trx_number,
5329         trx_description,
5330         trx_communicated_date,
5331         batch_source_id,
5332         batch_source_name,
5333         doc_seq_id,
5334         doc_seq_name,
5335         doc_seq_value,
5336         trx_due_date,
5337         trx_type_description,
5338         document_sub_type,
5339         supplier_tax_invoice_number,
5340         supplier_tax_invoice_date,
5341         supplier_exchange_rate,
5342         tax_invoice_date,
5343         tax_invoice_number,
5344         tax_event_class_code,
5345         tax_event_type_code,
5346         doc_event_status,
5347         rdng_ship_to_pty_tx_prof_id,
5348         rdng_ship_from_pty_tx_prof_id,
5349         rdng_bill_to_pty_tx_prof_id,
5350         rdng_bill_from_pty_tx_prof_id,
5351         rdng_ship_to_pty_tx_p_st_id,
5352         rdng_ship_from_pty_tx_p_st_id,
5353         rdng_bill_to_pty_tx_p_st_id,
5354         rdng_bill_from_pty_tx_p_st_id,
5355         bill_third_pty_acct_id,
5356         bill_third_pty_acct_site_id,
5357 	ship_third_pty_acct_id,
5358 	ship_third_pty_acct_site_id
5359         ) VALUES (
5360         p_invoice_header_rec.org_id,                       --internal_organization_id
5361         NULL,                                              --internal_org_location_id
5362         l_application_id,                                  --application_id
5363         l_entity_code,                                     --entity_code
5364         P_event_class_code,                                --event_class_code
5365         P_event_type_code,                                 --event_type_code
5366         p_invoice_header_rec.invoice_id,                   --trx_id
5367         NULL,                                              --hdr_trx_user_key1
5368         NULL,                                              --hdr_trx_user_key2
5369         NULL,                                              --hdr_trx_user_key3
5370         NULL,                                              --hdr_trx_user_key4
5371         NULL,                                              --hdr_trx_user_key5
5372         NULL,                                              --hdr_trx_user_key6
5373         p_invoice_header_rec.invoice_date,                 --trx_date
5374         NULL,                                              --trx_doc_revision
5375         p_invoice_header_rec.set_of_books_id,              --ledger_id
5376         p_invoice_header_rec.invoice_currency_code,        --trx_currency_code
5377         p_invoice_header_rec.exchange_date,                --currency_conversion_date
5378         p_invoice_header_rec.exchange_rate,                --currency_conversion_rate
5379         p_invoice_header_rec.exchange_rate_type,           --currency_conversion_type
5380         l_minimum_accountable_unit,                        --minimum_accountable_unit
5381         l_precision,                                       --precision
5382         p_invoice_header_rec.legal_entity_id,              --legal_entity_id
5383         NULL,                                              --rounding_ship_to_party_id
5384         p_invoice_header_rec.party_id,                     --rounding_ship_from_party_id
5385         NULL,                                              --rounding_bill_to_party_id
5386         p_invoice_header_rec.party_id,                     --rounding_bill_from_party_id
5387         NULL,                                              --rndg_ship_to_party_site_id
5388         p_invoice_header_rec.party_site_id,                --rndg_ship_from_party_site_id
5389         NULL,                                              --rndg_bill_to_party_site_id
5390         p_invoice_header_rec.party_site_id,                --rndg_bill_from_party_site_id
5391         NULL,                                              --establishment_id
5392         NULL,                                              --receivables_trx_type_id
5393         l_related_inv_application_id,                      --related_doc_application_id
5394         l_related_inv_entity_code,                         --related_doc_entity_code
5395         l_related_event_class_code,                        --related_doc_event_class_code
5396         p_invoice_header_rec.tax_related_invoice_id,       --related_doc_trx_id
5397         NULL,                                              --rel_doc_hdr_trx_user_key1
5398         NULL,                                              --rel_doc_hdr_trx_user_key2
5399         NULL,                                              --rel_doc_hdr_trx_user_key3
5400         NULL,                                              --rel_doc_hdr_trx_user_key4
5401         NULL,                                              --rel_doc_hdr_trx_user_key5
5402         NULL,                                              --rel_doc_hdr_trx_user_key6
5403         l_related_inv_number,                              --related_doc_number
5404         l_related_inv_date,                                --related_doc_date
5405         p_invoice_header_rec.taxation_country,             --default_taxation_country
5406         l_quote_flag,                                      --quote_flag
5407         p_invoice_header_rec.control_amount,               --ctrl_total_hdr_tx_amt
5408         p_invoice_header_rec.invoice_num,                  --trx_number
5409         p_invoice_header_rec.description,                  --trx_description
5410         NULL,                                              --trx_communicated_date
5411         NULL,                                              --batch_source_id
5412         NULL,                                              --batch_source_name
5413         p_invoice_header_rec.doc_sequence_id,              --doc_seq_id
5414         l_doc_seq_name,            			   --doc_seq_name
5415         nvl(to_char(p_invoice_header_rec.doc_sequence_value),--bug6656894
5416 	    p_invoice_header_rec.voucher_num),             --doc_seq_value
5417         NULL,                                              --trx_due_date
5418         NULL,                                              --trx_type_description
5419         p_invoice_header_rec.document_sub_type,            --document_sub_type
5420         p_invoice_header_rec.supplier_tax_invoice_number,  --supplier_tax_invoice_number
5421         p_invoice_header_rec.supplier_tax_invoice_date,    --supplier_tax_invoice_date
5422         p_invoice_header_rec.supplier_tax_exchange_rate,   --supplier_exchange_rate
5423         p_invoice_header_rec.tax_invoice_recording_date,   --tax_invoice_date
5424         p_invoice_header_rec.tax_invoice_internal_seq,     --tax_invoice_number
5425         NULL,                                              --tax_event_class_code
5426         NULL,                                              --tax_event_type_code
5427         NULL,                                              --doc_event_status
5428         NULL,                                              --rdng_ship_to_pty_tx_prof_id
5429         NULL,                                              --rdng_ship_from_pty_tx_prof_id
5430         NULL,                                              --rdng_bill_to_pty_tx_prof_id
5431         NULL,                                              --rdng_bill_from_pty_tx_prof_id
5432         NULL,                                              --rdng_ship_to_pty_tx_p_st_id
5433         NULL,                                              --rdng_ship_from_pty_tx_p_st_id
5434         NULL,                                              --rdng_bill_to_pty_tx_p_st_id
5435         NULL,                                              --rdng_bill_from_pty_tx_p_st_id
5436 	p_invoice_header_rec.vendor_id,			   --bill_third_pty_acct_id
5437 	p_invoice_header_rec.vendor_site_id,		   --bill_third_pty_acct_site_id
5438         p_invoice_header_rec.vendor_id,                    --ship_third_pty_acct_id
5439         p_invoice_header_rec.vendor_site_id                --ship_third_pty_acct_site_id
5440      );
5441 
5442      -- Global Variable g_invoices_to_process should be initialized right after
5443      -- the previous insert. No other sql statements must be placed after the
5444      -- insert because the sql rowcount will be reset. This variable is used in
5445      -- calculate_tax and determine_recovery.
5446 
5447      g_invoices_to_process := sql%rowcount;
5448 
5449      -- Added log messages for bug 6321366
5450      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5451       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_trx_headers_gt values ');
5452       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Application_id: '|| l_application_id);
5453       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Entity_code: ' || l_entity_code);
5454       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: ' || P_event_class_code);
5455       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_type_code: ' ||P_event_type_code);
5456       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Invoice_id: '|| p_invoice_header_rec.invoice_id);
5457      END IF;
5458 
5459    END IF;
5460 
5461    RETURN l_return_status;
5462 
5463   EXCEPTION
5464     WHEN OTHERS THEN
5465       IF (SQLCODE <> -20001) THEN
5466         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
5467         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
5468         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
5469         FND_MESSAGE.SET_TOKEN('PARAMETERS',
5470           ' P_Invoice_Header_Rec = '||P_Invoice_Header_Rec.Invoice_Id||
5471           ' P_Calling_Mode ='||P_Calling_Mode||
5472           ' P_Error_Code = '||P_Error_Code||
5473           ' P_Calling_Sequence = '||P_Calling_Sequence);
5474         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
5475       END IF;
5476 
5477       IF ( tax_related_invoice%ISOPEN ) THEN
5478         CLOSE tax_related_invoice;
5479       END IF;
5480 
5481       APP_EXCEPTION.RAISE_EXCEPTION;
5482 
5483   END Populate_Headers_GT;
5484 
5485 /*=============================================================================
5486  |  FUNCTION - Populate_Header_Import_GT()
5487  |
5488  |  DESCRIPTION
5489  |    This function will get additional information required to populate the
5490  |    zx_trx_headers_gt from the import array structure.
5491  |    This function returns TRUE if the insert to the temp table goes
5492  |    through successfully.  Otherwise, FALSE.
5493  |
5494  |  PARAMETERS
5495  |    P_Invoice_Header_Rec - record with invoice header information
5496  |    P_Calling_Mode - calling mode. it is used to
5497  |    P_Event_Class_Code - Event class code
5498  |    P_Event_Type_Code - Event type code
5499  |    P_error_code - Error code to be returned
5500  |    P_calling_sequence -  Calling sequence
5501  |
5502  |  MODIFICATION HISTORY
5503  |    DATE          Author         Action
5504  |    20-JAN-2004   SYIDNER        Created
5505  |
5506 *============================================================================*/
5507   FUNCTION Populate_Header_Import_GT(
5508              P_Invoice_Header_Rec        IN AP_IMPORT_INVOICES_PKG.r_invoice_info_rec,
5509              P_Calling_Mode              IN VARCHAR2,
5510              P_Event_Class_Code          OUT NOCOPY VARCHAR2,
5511              P_Event_Type_Code           OUT NOCOPY VARCHAR2,
5512              P_Error_Code                OUT NOCOPY VARCHAR2,
5513              P_Calling_Sequence          IN VARCHAR2) RETURN BOOLEAN
5514   IS
5515 
5516     l_debug_info                 VARCHAR2(240);
5517     l_curr_calling_sequence      VARCHAR2(4000);
5518 
5519     l_application_id             zx_trx_headers_gt.application_id%TYPE;
5520     l_entity_code                zx_trx_headers_gt.entity_code%TYPE;
5521 
5522     l_quote_flag                 zx_trx_headers_gt.quote_flag%TYPE
5523       := 'N';
5524     -- This flag is always N except when the calculate service is called for
5525     -- quote for the recurring invoices and distributions sets.
5526 
5527     CURSOR tax_related_invoice( c_tax_related_invoice_id IN NUMBER) IS
5528 -- B# 6907814 ...  SELECT invoice_num, invoice_date, invoice_type_lookup_code
5529     SELECT invoice_num, invoice_type_lookup_code, invoice_date
5530       FROM ap_invoices_all
5531      WHERE invoice_id = c_tax_related_invoice_id;
5532 
5533     l_related_inv_application_id
5534       zx_trx_headers_gt.related_doc_application_id%TYPE;
5535     l_related_inv_entity_code
5536       zx_trx_headers_gt.related_doc_entity_code%TYPE;
5537     l_related_event_class_code
5538       zx_trx_headers_gt.related_doc_event_class_code%TYPE;
5539     l_related_inv_number         ap_invoices_all.invoice_num%TYPE;
5540     l_related_inv_date           ap_invoices_all.invoice_date%TYPE;
5541     l_related_inv_type           ap_invoices_all.invoice_type_lookup_code%TYPE;
5542 
5543     l_precision                  fnd_currencies.precision%TYPE := 0;
5544     l_minimum_accountable_unit   fnd_currencies.minimum_accountable_unit%TYPE;
5545 
5546     l_return_status              BOOLEAN := TRUE;
5547     l_api_name                  VARCHAR2(30) := 'populate_header_import_gt';
5548 
5549   BEGIN
5550 
5551     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Populate_Header_Import_GT<-'||
5552                                P_calling_sequence;
5553 
5554     -------------------------------------------------------------------
5555     l_debug_info := 'Step 1: Get event class code';
5556     -------------------------------------------------------------------
5557 
5558     IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
5559       P_Invoice_Type_Lookup_Code => P_Invoice_Header_Rec.invoice_type_lookup_code,
5560       P_Event_Class_Code         => P_event_class_code,
5561       P_error_code               => P_error_code,
5562       P_calling_sequence         => l_curr_calling_sequence)) THEN
5563 
5564       l_return_status := FALSE;
5565 
5566     END IF;
5567 
5568     -------------------------------------------------------------------
5569     l_debug_info := 'Step 2: Get event type code';
5570     -------------------------------------------------------------------
5571     -- Since this procedure will be called only from the import program
5572     -- it is the first time eTax is call so the etax_already_called_flag is N
5573 
5574     IF (l_return_status = TRUE) THEN
5575       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
5576         P_Event_Class_Code          => P_event_class_code,
5577         P_Calling_Mode              => P_Calling_Mode,
5578         P_eTax_Already_called_flag  => 'N',
5579         P_Event_Type_Code           => P_Event_Type_Code,
5580         P_Error_Code                => P_error_code,
5581         P_Calling_Sequence          => l_curr_calling_sequence)) THEN
5582 
5583         l_return_status := FALSE;
5584       END IF;
5585     END IF;
5586 
5587     -------------------------------------------------------------------
5588     l_debug_info := 'Step 3: Populate product specific attributes';
5589     -------------------------------------------------------------------
5590     l_application_id := 200;   -- Oracle Payables
5591     -- The same code is used for all invoice types.
5592     l_entity_code := 'AP_INVOICES';
5593 
5594     -------------------------------------------------------------------
5595     l_debug_info := 'Step 4: Populate tax related invoice information '||
5596                     'if tax_related_invoice_id is not null';
5597     -------------------------------------------------------------------
5598     IF (l_return_status = TRUE AND
5599         P_Invoice_Header_Rec.tax_related_invoice_id IS NOT NULL) THEN
5600 
5601       -- At this moment we are sure the tax_related_invoice_id is valid.
5602       -- It was validated in the previous to this call in the import
5603       -- process.
5604 
5605       l_related_inv_application_id := 200;  --Oracle Payables
5606       l_related_inv_entity_code := 'AP_INVOICES';
5607 
5608       BEGIN
5609         OPEN tax_related_invoice(P_Invoice_Header_Rec.tax_related_invoice_id);
5610         FETCH tax_related_invoice
5611           INTO l_related_inv_number, l_related_inv_type,
5612                l_related_inv_date;
5613         CLOSE tax_related_invoice;
5614       END;
5615 
5616       --------------------------------------------------------------------------
5617       l_debug_info := 'Step 5: Get event class code for tax_related_invoice_id';
5618       --------------------------------------------------------------------------
5619       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
5620         P_Invoice_Type_Lookup_Code => l_related_inv_type,
5621         P_Event_Class_Code         => l_related_event_class_code,
5622         P_error_code               => P_error_code,
5623         P_calling_sequence         => l_curr_calling_sequence)) THEN
5624 
5625         l_return_status := FALSE;
5626       END IF;
5627     END IF;
5628 
5629     -------------------------------------------------------------------
5630     l_debug_info := 'Step 6: Get transaction currency details';
5631     -------------------------------------------------------------------
5632     IF (l_return_status = TRUE) THEN
5633       BEGIN
5634         SELECT NVL(precision, 0), NVL(minimum_accountable_unit,(1/power(10,precision)))
5635           INTO l_precision, l_minimum_accountable_unit
5636           FROM fnd_currencies
5637          WHERE currency_code = P_Invoice_Header_Rec.invoice_currency_code;
5638 
5639       END;
5640     END IF;
5641 
5642     -------------------------------------------------------------------
5643     l_debug_info := 'Step 8: Populate zx_trx_headers_gt';
5644     -------------------------------------------------------------------
5645     IF (l_return_status = TRUE) THEN
5646 
5647       DELETE FROM zx_trx_headers_gt
5648        WHERE application_id   = AP_ETAX_PKG.AP_APPLICATION_ID
5649          AND entity_code      = AP_ETAX_PKG.AP_ENTITY_CODE
5650          AND event_class_code IN (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
5651                                   AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
5652                                   AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
5653          AND trx_id = p_invoice_header_rec.invoice_id;
5654 
5655       INSERT INTO zx_trx_headers_gt(
5656         internal_organization_id,
5657         internal_org_location_id,
5658         application_id,
5659         entity_code,
5660         event_class_code,
5661         event_type_code,
5662         trx_id,
5663         hdr_trx_user_key1,
5664         hdr_trx_user_key2,
5665         hdr_trx_user_key3,
5666         hdr_trx_user_key4,
5667         hdr_trx_user_key5,
5668         hdr_trx_user_key6,
5669         trx_date,
5670         trx_doc_revision,
5671         ledger_id,
5672         trx_currency_code,
5673         currency_conversion_date,
5674         currency_conversion_rate,
5675         currency_conversion_type,
5676         minimum_accountable_unit,
5677         precision,
5678         legal_entity_id,
5679         rounding_ship_to_party_id,
5680         rounding_ship_from_party_id,
5681         rounding_bill_to_party_id,
5682         rounding_bill_from_party_id,
5683         rndg_ship_to_party_site_id,
5684         rndg_ship_from_party_site_id,
5685         rndg_bill_to_party_site_id,
5686         rndg_bill_from_party_site_id,
5687         establishment_id,
5688         receivables_trx_type_id,
5689         related_doc_application_id,
5690         related_doc_entity_code,
5691         related_doc_event_class_code,
5692         related_doc_trx_id,
5693         rel_doc_hdr_trx_user_key1,
5694         rel_doc_hdr_trx_user_key2,
5695         rel_doc_hdr_trx_user_key3,
5696         rel_doc_hdr_trx_user_key4,
5697         rel_doc_hdr_trx_user_key5,
5698         rel_doc_hdr_trx_user_key6,
5699         related_doc_number,
5700         related_doc_date,
5701         default_taxation_country,
5702         quote_flag,
5703         ctrl_total_hdr_tx_amt,
5704         trx_number,
5705         trx_description,
5706         trx_communicated_date,
5707         batch_source_id,
5708         batch_source_name,
5709         doc_seq_id,
5710         doc_seq_name,
5711         doc_seq_value,
5712         trx_due_date,
5713         trx_type_description,
5714         document_sub_type,
5715         supplier_tax_invoice_number,
5716         supplier_tax_invoice_date,
5717         supplier_exchange_rate,
5718         tax_invoice_date,
5719         tax_invoice_number,
5720         tax_event_class_code,
5721         tax_event_type_code,
5722         doc_event_status,
5723         rdng_ship_to_pty_tx_prof_id,
5724         rdng_ship_from_pty_tx_prof_id,
5725         rdng_bill_to_pty_tx_prof_id,
5726         rdng_bill_from_pty_tx_prof_id,
5727         rdng_ship_to_pty_tx_p_st_id,
5728         rdng_ship_from_pty_tx_p_st_id,
5729         rdng_bill_to_pty_tx_p_st_id,
5730         rdng_bill_from_pty_tx_p_st_id,
5731         bill_third_pty_acct_id,
5732         bill_third_pty_acct_site_id,
5733         ship_third_pty_acct_id,
5734 	ship_third_pty_acct_site_id
5735         ) VALUES (
5736         p_invoice_header_rec.org_id,                       --internal_organization_id
5737         NULL,                                              --internal_org_location_id
5738         l_application_id,                                  --application_id
5739         l_entity_code,                                     --entity_code
5740         P_event_class_code,                                --event_class_code
5741         P_event_type_code,                                 --event_type_code
5742         p_invoice_header_rec.invoice_id,                   --trx_id
5743         NULL,                                              --hdr_trx_user_key1
5744         NULL,                                              --hdr_trx_user_key2
5745         NULL,                                              --hdr_trx_user_key3
5746         NULL,                                              --hdr_trx_user_key4
5747         NULL,                                              --hdr_trx_user_key5
5748         NULL,                                              --hdr_trx_user_key6
5749         p_invoice_header_rec.invoice_date,                 --trx_date
5750         NULL,                                              --trx_doc_revision
5751         p_invoice_header_rec.set_of_books_id,              --ledger_id
5752         p_invoice_header_rec.invoice_currency_code,        --trx_currency_code
5753         p_invoice_header_rec.exchange_date,                --currency_conversion_date
5754         p_invoice_header_rec.exchange_rate,                --currency_conversion_rate
5755         p_invoice_header_rec.exchange_rate_type,           --currency_conversion_type
5756         l_minimum_accountable_unit,                        --minimum_accountable_unit
5757         l_precision,                                       --precision
5758         p_invoice_header_rec.legal_entity_id,              --legal_entity_id
5759         NULL,                                              --rounding_ship_to_party_id
5760         p_invoice_header_rec.party_id,                     --rounding_ship_from_party_id
5761         NULL,                                              --rounding_bill_to_party_id
5762         p_invoice_header_rec.party_id,                     --rounding_bill_from_party_id
5763         NULL,                                              --rndg_ship_to_party_site_id
5764         p_invoice_header_rec.party_site_id,                --rndg_ship_from_party_site_id
5765         NULL,                                              --rndg_bill_to_party_site_id
5766         p_invoice_header_rec.party_site_id,                --rndg_bill_from_party_site_id
5767         NULL,                                              --establishment_id
5768         NULL, --receivables_trx_type_id
5769         l_related_inv_application_id, --related_doc_application_id
5770         l_related_inv_entity_code, --related_doc_entity_code
5771         l_related_event_class_code, --related_doc_event_class_code
5772         p_invoice_header_rec.tax_related_invoice_id,       --related_doc_trx_id
5773         NULL, --rel_doc_hdr_trx_user_key1
5774         NULL, --rel_doc_hdr_trx_user_key2
5775         NULL, --rel_doc_hdr_trx_user_key3
5776         NULL, --rel_doc_hdr_trx_user_key4
5777         NULL, --rel_doc_hdr_trx_user_key5
5778         NULL, --rel_doc_hdr_trx_user_key6
5779         l_related_inv_number,                              --related_doc_number
5780         l_related_inv_date,                                --related_doc_date
5781         p_invoice_header_rec.taxation_country, --default_taxation_country
5782         l_quote_flag,                                      --quote_flag
5783         p_invoice_header_rec.control_amount, --ctrl_total_hdr_tx_amt
5784         p_invoice_header_rec.invoice_num,                  --trx_number
5785         p_invoice_header_rec.description,                  --trx_description
5786         NULL, --trx_communicated_date
5787         NULL,                                              --batch_source_id
5788         NULL,                                              --batch_source_name
5789         NULL,                                              --doc_seq_id
5790         NULL,                                              --doc_seq_name
5791         NULL,                                              --doc_seq_value
5792         NULL,                                              --trx_due_date
5793         NULL, --trx_type_description
5794         p_invoice_header_rec.document_sub_type,            --document_sub_type
5795         p_invoice_header_rec.supplier_tax_invoice_number, --supplier_tax_invoice_number
5796         p_invoice_header_rec.supplier_tax_invoice_date, --supplier_tax_invoice_date
5797         p_invoice_header_rec.supplier_tax_exchange_rate, --supplier_exchange_rate
5798         p_invoice_header_rec.tax_invoice_recording_date,   --tax_invoice_date
5799         p_invoice_header_rec.tax_invoice_internal_seq,     --tax_invoice_number
5800         NULL, --tax_event_class_code
5801         NULL,                                              --tax_event_type_code
5802         NULL,                                              --doc_event_status
5803         NULL, --rdng_ship_to_pty_tx_prof_id
5804         NULL, --rdng_ship_from_pty_tx_prof_id
5805         NULL, --rdng_bill_to_pty_tx_prof_id
5806         NULL, --rdng_bill_from_pty_tx_prof_id
5807         NULL, --rdng_ship_to_pty_tx_p_st_id
5808         NULL, --rdng_ship_from_pty_tx_p_st_id
5809         NULL, --rdng_bill_to_pty_tx_p_st_id
5810         NULL, --rdng_bill_from_pty_tx_p_st_id
5811         p_invoice_header_rec.vendor_id,			   --bill_third_pty_acct_id
5812         p_invoice_header_rec.vendor_site_id,               --bill_third_pty_acct_site_id
5813         p_invoice_header_rec.vendor_id,                    --ship_third_pty_acct_id
5814         p_invoice_header_rec.vendor_site_id                --ship_third_pty_acct_site_id
5815      );
5816 --Log messages added for bug 6321366
5817   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5818       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_trx_headers_gt values ');
5819       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Application_id: '|| l_application_id);
5820       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Entity_code: ' || l_entity_code);
5821       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: ' || P_event_class_code);
5822       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_type_code: ' ||P_event_type_code);
5823       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Invoice_id: '|| p_invoice_header_rec.invoice_id);
5824   END IF;
5825 
5826    END IF;
5827 
5828    RETURN l_return_status;
5829 
5830   EXCEPTION
5831     WHEN OTHERS THEN
5832       IF (SQLCODE <> -20001) THEN
5833         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
5834         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
5835         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
5836         FND_MESSAGE.SET_TOKEN('PARAMETERS',
5837           ' P_Invoice_Header_Rec = '||P_Invoice_Header_Rec.Invoice_Id||
5838           ' P_Calling_Mode ='||P_Calling_Mode||
5839           ' P_Error_Code = '||P_Error_Code||
5840           ' P_Calling_Sequence = '||P_Calling_Sequence);
5841         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
5842       END IF;
5843 
5844       IF ( tax_related_invoice%ISOPEN ) THEN
5845         CLOSE tax_related_invoice;
5846       END IF;
5847 
5848       APP_EXCEPTION.RAISE_EXCEPTION;
5849 
5850   END Populate_Header_Import_GT;
5851 
5852 
5853 /*=============================================================================
5854  |  FUNCTION - Populate_Lines_GT()
5855  |
5856  |  DESCRIPTION
5857  |      This function will get additional information required to populate the
5858  |      ZX_TRANSACTION_LINES_GT
5859  |      This function returns TRUE if the population of the temp table goes
5860  |      through successfully.  Otherwise, FALSE.
5861  |
5862  |  PARAMETERS
5863  |      P_Invoice_Header_Rec - record with invoice header information
5864  |      P_Calling_Mode - calling mode. it is used to
5865  |      P_Event_Class_Code - Event class code for document
5866  |      P_Line_Number - prepay line number to be unapplied.
5867  |      P_error_code - Error code to be returned
5868  |      P_calling_sequence -  Calling sequence
5869  |
5870  |  MODIFICATION HISTORY
5871  |    DATE          Author         Action
5872  |    09-OCT-2003   SYIDNER        Created
5873  |    03-MAR-2004   SYIDNER        Including prepayment
5874  |                                 application/unapplication functionality
5875  |
5876  *============================================================================*/
5877 
5878   FUNCTION Populate_Lines_GT(
5879              P_Invoice_Header_Rec      IN ap_invoices_all%ROWTYPE,
5880              P_Calling_Mode            IN VARCHAR2,
5881              P_Event_Class_Code        IN VARCHAR2,
5882              P_Line_Number             IN NUMBER DEFAULT NULL,
5883              P_Error_Code              OUT NOCOPY VARCHAR2,
5884              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
5885   IS
5886 
5887     l_debug_info                 VARCHAR2(240);
5888     l_curr_calling_sequence      VARCHAR2(4000);
5889 
5890     -- This structure to populate all the lines information previous to insert
5891     -- in eTax global temporary table.
5892     TYPE Trans_Lines_Tab_Type IS TABLE OF zx_transaction_lines_gt%ROWTYPE;
5893     trans_lines                    Trans_Lines_Tab_Type := Trans_Lines_Tab_Type();
5894 
5895     l_application_id               zx_trx_headers_gt.application_id%TYPE;
5896     l_ctrl_hdr_tx_appl_flag        zx_transaction_lines_gt.ctrl_hdr_tx_appl_flag%TYPE;
5897     l_line_level_action            zx_transaction_lines_gt.line_level_action%TYPE;
5898     l_line_class		   zx_transaction_lines_gt.line_class%TYPE;
5899     l_line_amt_includes_tax_flag   zx_transaction_lines_gt.line_amt_includes_tax_flag%TYPE;
5900     l_init_line_amt_incl_tax_fg    zx_transaction_lines_gt.line_amt_includes_tax_flag%TYPE;
5901     l_product_org_id               zx_transaction_lines_gt.product_org_id%TYPE;
5902     l_bill_to_location_id          zx_transaction_lines_gt.bill_to_location_id%TYPE;
5903 
5904 
5905     -- Purchase Order Info
5906     l_ref_doc_application_id      zx_transaction_lines_gt.ref_doc_application_id%TYPE;
5907     l_ref_doc_entity_code	  zx_transaction_lines_gt.ref_doc_entity_code%TYPE;
5908     l_ref_doc_event_class_code    zx_transaction_lines_gt.ref_doc_event_class_code%TYPE;
5909     l_ref_doc_line_quantity       zx_transaction_lines_gt.ref_doc_line_quantity%TYPE;
5910     l_po_header_curr_conv_rat     po_headers_all.rate%TYPE;
5911     l_ref_doc_trx_level_type      zx_transaction_lines_gt.ref_doc_trx_level_type%TYPE;
5912     l_po_header_curr_conv_rate    po_headers_all.rate%TYPE;
5913     l_uom_code			  mtl_units_of_measure.uom_code%TYPE;
5914     l_dummy			  number;
5915 
5916 
5917     -- Receipt Info
5918     l_applied_to_application_id    zx_transaction_lines_gt.applied_to_application_id%TYPE;
5919     l_applied_to_entity_code       zx_transaction_lines_gt.applied_to_entity_code%TYPE;
5920     l_applied_to_event_class_code  zx_transaction_lines_gt.applied_to_event_class_code%TYPE;
5921     l_trx_receipt_date             zx_transaction_lines_gt.trx_receipt_date%TYPE;
5922     l_ref_doc_trx_id               zx_transaction_lines_gt.ref_doc_trx_id%TYPE;
5923 
5924     -- Prepayment Info
5925     l_prepay_doc_application_id    zx_transaction_lines_gt.applied_from_application_id%TYPE;
5926     l_prepay_doc_entity_code       zx_transaction_lines_gt.applied_from_entity_code%TYPE;
5927     l_prepay_doc_event_class_code  zx_transaction_lines_gt.applied_from_event_class_code%TYPE;
5928     l_prepay_doc_number            ap_invoices_all.invoice_num%TYPE;
5929     l_prepay_doc_date              ap_invoices_all.invoice_date%TYPE;
5930     l_applied_from_trx_level_type  zx_transaction_lines_gt.applied_from_trx_level_type%TYPE;
5931     l_applied_from_trx_id	   zx_transaction_lines_gt.applied_from_trx_id%TYPE;
5932     l_applied_from_line_id         zx_transaction_lines_gt.applied_from_line_id%TYPE;
5933 
5934     -- Corrected Invoice Info
5935     l_adj_doc_application_id     zx_transaction_lines_gt.adjusted_doc_application_id%TYPE;
5936     l_adj_doc_entity_code	 zx_transaction_lines_gt.adjusted_doc_entity_code%TYPE;
5937     l_adj_doc_event_class_code   zx_transaction_lines_gt.adjusted_doc_event_class_code%TYPE;
5938     l_adj_doc_number             zx_transaction_lines_gt.adjusted_doc_number%TYPE;
5939     l_adj_doc_date               zx_transaction_lines_gt.adjusted_doc_date%TYPE;
5940     l_adj_doc_trx_level_type	 zx_transaction_lines_gt.adjusted_doc_trx_level_type%TYPE;
5941 
5942     l_fob_point                  po_vendor_sites_all.fob_lookup_code%TYPE;
5943     l_location_id		 zx_transaction_lines_gt.ship_from_location_id%type;
5944 
5945     l_dflt_tax_class_code       zx_transaction_lines_gt.input_tax_classification_code%type;
5946     l_allow_tax_code_override   varchar2(10);
5947 
5948     l_intended_use		zx_lines_det_factors.line_intended_use%type;
5949     l_product_type		zx_lines_det_factors.product_type%type;
5950     l_product_category		zx_lines_det_factors.product_category%type;
5951     l_product_fisc_class	zx_lines_det_factors.product_fisc_classification%type;
5952     l_user_defined_fisc_class	zx_lines_det_factors.user_defined_fisc_class%type;
5953     l_assessable_value		zx_lines_det_factors.assessable_value%type;
5954 
5955     l_default_ccid      	ap_invoice_lines_all.default_dist_ccid%TYPE; --Bug6908977
5956 
5957     l_return_status             BOOLEAN := TRUE;
5958 
5959     l_api_name                  VARCHAR2(30) := 'Populate_Lines_GT';
5960     l_tax_already_calculated_line  VARCHAR2(1);
5961 
5962     l_ship_to_party_id          po_line_locations_all.ship_to_organization_id%type; -- 7262269
5963 
5964   BEGIN
5965 
5966     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Populate_Lines_GT<-' ||
5967                                P_calling_sequence;
5968 
5969     ----------------------------------------------------------------------
5970     l_debug_info := 'Set line defaults from cache';
5971     Print(l_api_name,l_debug_info);
5972     ----------------------------------------------------------------------
5973     IF l_payment_request_flag ='N' THEN ---for bug 5967914
5974        l_fob_point            := AP_ETAX_SERVICES_PKG.g_site_attributes
5975 					(p_invoice_header_rec.vendor_site_id).fob_lookup_code;
5976     END IF;
5977 
5978     IF l_payment_request_flag ='Y' THEN ---if condition added for bug 5967914
5979        l_location_id         := AP_ETAX_SERVICES_PKG.g_site_attributes
5980 					(p_invoice_header_rec.party_site_id).location_id;
5981     ELSE
5982        l_location_id         := AP_ETAX_SERVICES_PKG.g_site_attributes
5983 					(p_invoice_header_rec.vendor_site_id).location_id;
5984     END IF;
5985 
5986     l_bill_to_location_id := AP_ETAX_SERVICES_PKG.g_org_attributes
5987 					(p_invoice_header_rec.org_id).bill_to_location_id;
5988 
5989     ----------------------------------------------------------------------
5990     l_debug_info := 'Go through taxable lines';
5991     ----------------------------------------------------------------------
5992 
5993     IF ( l_inv_line_list.COUNT > 0) THEN
5994       -- For non-tax only lines
5995       trans_lines.EXTEND(l_inv_line_list.COUNT);
5996       FOR i IN l_inv_line_list.FIRST..l_inv_line_list.LAST LOOP
5997         -------------------------------------------------------------------
5998          l_debug_info := 'Get line_level_action for line number: '||l_inv_line_list(i).line_number;
5999         -------------------------------------------------------------------
6000         IF (l_return_status = TRUE) THEN
6001          IF ( P_calling_mode = 'OVERRIDE TAX' ) THEN
6002            l_line_level_action := 'NO_CHANGE';
6003 
6004          ELSIF (l_inv_line_list(i).line_type_lookup_code = 'PREPAY'
6005                 or p_calling_mode = 'RECOUPMENT') THEN
6006 
6007            -- The treatment of PREPAY lines is different from a regular
6008            -- line.  We will differienciate the PREPAY line created
6009            -- for the prepayment application if no tax has been canculated
6010            -- for it since we call calculate tax during the prepayment
6011            -- application.
6012            -- Since the prepayment unapplication will discard the PREPAY
6013            -- line created during the application, there is no way to
6014            -- identify the PREPAY line to unapply if there is more than
6015            -- one unapplied PREPAY lines in the invoice.  For this reason
6016            -- the parameter used to calculate tax per line will be used.
6017 
6018            IF (P_calling_mode = 'APPLY PREPAY' or p_calling_mode = 'RECOUPMENT') THEN
6019              IF (NVL(l_inv_line_list(i).tax_already_calculated_flag, 'N') = 'N'
6020 		 or p_calling_mode = 'RECOUPMENT') THEN
6021                l_line_level_action := 'APPLY_FROM';
6022 
6023              ELSE
6024                l_line_level_action := 'NO_CHANGE';
6025 
6026              END IF;
6027 
6028            ELSIF (P_calling_mode = 'UNAPPLY PREPAY') THEN
6029              IF (l_inv_line_list(i).line_number = p_line_number) THEN
6030                 l_line_level_action := 'UNAPPLY_FROM';
6031 
6032              ELSE
6033                l_line_level_action := 'NO_CHANGE';
6034 
6035              END IF;
6036            ELSE
6037              l_line_level_action := 'NO_CHANGE';
6038 
6039            END IF;
6040 
6041          ELSIF ( NVL(l_inv_line_list(i).discarded_flag, 'N') = 'Y'
6042                  OR NVL(l_inv_line_list(i).cancelled_flag, 'N') = 'Y') THEN
6043 
6044                -- Bug 7444234
6045                -- line_level_action as DISCARD irrespective of migrated
6046                -- transaction.
6047 
6048                l_line_level_action := 'DISCARD';
6049 
6050 	       --IF NVL(l_inv_line_list(i).historical_flag, 'N') = 'Y' THEN
6051 	       --   l_line_level_action := 'UPDATE';
6052 	       --ELSE
6053                --   l_line_level_action := 'DISCARD';
6054 	       --END IF;
6055 
6056          ELSIF (NVL(l_inv_line_list(i).tax_already_calculated_flag, 'N') = 'Y') THEN
6057       -- Start for bug 6485124
6058               l_line_level_action := 'UPDATE';
6059          ELSE
6060             BEGIN
6061               SELECT 'Y'
6062               INTO   l_tax_already_calculated_line
6063               FROM   zx_lines_det_factors
6064               WHERE  application_id        =  200
6065               AND    entity_code           =  'AP_INVOICES'
6066               AND    event_class_code      IN ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
6067               AND    trx_id                = l_inv_line_list(i).invoice_id
6068               AND    trx_line_id           = l_inv_line_list(i).line_number
6069               AND    ROWNUM = 1;
6070 
6071                IF l_tax_already_calculated_line = 'Y' THEN
6072                   l_line_level_action := 'UPDATE';
6073                ELSE
6074                   l_line_level_action := 'CREATE';
6075                END IF;
6076              EXCEPTION
6077                 WHEN NO_DATA_FOUND  THEN
6078                      l_line_level_action := 'CREATE';
6079                 WHEN OTHERS THEN
6080                      RAISE;
6081              END;
6082       -- End for bug 6485124
6083           END IF;
6084 
6085          -------------------------------------------------------------------
6086           l_debug_info := 'Get Additional PO matched  info';
6087          -------------------------------------------------------------------
6088 
6089          IF ( l_inv_line_list(i).po_line_location_id IS NOT NULL) THEN
6090 
6091            IF NOT (AP_ETAX_UTILITY_PKG.Get_PO_Info(
6092               P_Po_Line_Location_Id         => l_inv_line_list(i).po_line_location_id,
6093               P_PO_Distribution_Id          => null,
6094               P_Application_Id              => l_ref_doc_application_id,
6095               P_Entity_code                 => l_ref_doc_entity_code,
6096               P_Event_Class_Code            => l_ref_doc_event_class_code,
6097               P_PO_Quantity                 => l_ref_doc_line_quantity,
6098               P_Product_Org_Id              => l_product_org_id,
6099               P_Po_Header_Id                => l_ref_doc_trx_id,
6100               P_Po_Header_curr_conv_rate    => l_po_header_curr_conv_rate,
6101 	          P_Uom_Code		            => l_uom_code,
6102               P_Dist_Qty                    => l_dummy,
6103               P_Ship_Price                  => l_dummy,
6104               P_Error_Code                  => P_error_code,
6105               P_Calling_Sequence            => l_curr_calling_sequence)) THEN
6106 
6107              l_return_status := FALSE;
6108            END IF;
6109 
6110 	   l_ref_doc_trx_level_type := 'SHIPMENT';
6111 
6112          ELSE
6113             l_ref_doc_application_id	 := Null;
6114             l_ref_doc_entity_code	 := Null;
6115             l_ref_doc_event_class_code   := Null;
6116             l_ref_doc_line_quantity      := Null;
6117             l_product_org_id		 := Null;
6118             l_ref_doc_trx_id		 := Null;
6119             l_ref_doc_trx_level_type	 := Null;
6120             l_uom_code			 := Null;
6121          END IF;
6122 
6123          -------------------------------------------------------------------
6124           l_debug_info := 'Get Additional receipt matched info ';
6125          -------------------------------------------------------------------
6126          IF ( l_return_status = TRUE AND
6127               l_inv_line_list(i).rcv_transaction_id IS NOT NULL) THEN
6128            IF NOT (AP_ETAX_UTILITY_PKG.Get_Receipt_Info(
6129               P_Rcv_Transaction_Id          => l_inv_line_list(i).rcv_transaction_id,
6130               P_Application_Id              => l_applied_to_application_id,
6131               P_Entity_code                 => l_applied_to_entity_code,
6132               P_Event_Class_Code            => l_applied_to_event_class_code,
6133               P_Transaction_Date            => l_trx_receipt_date,
6134               P_Error_Code                  => P_error_code,
6135               P_Calling_Sequence            => l_curr_calling_sequence)) THEN
6136 
6137              l_return_status := FALSE;
6138            END IF;
6139          ELSE
6140 	    l_applied_to_application_id   := Null;
6141             l_applied_to_entity_code      := Null;
6142             l_applied_to_event_class_code := Null;
6143 	    l_trx_receipt_date		  := Null;
6144          END IF;
6145 
6146          -------------------------------------------------------------------
6147           l_debug_info := 'Get Additional Prepayment Application Info';
6148          -------------------------------------------------------------------
6149 
6150          IF (l_return_status = TRUE) THEN
6151 	     IF (l_inv_line_list(i).prepay_invoice_id IS NOT NULL AND
6152                  l_inv_line_list(i).prepay_line_number IS NOT NULL) THEN
6153 
6154 	         IF NOT (AP_ETAX_UTILITY_PKG.Get_Prepay_Invoice_Info(
6155 			              P_Prepay_Invoice_Id           => l_inv_line_list(i).prepay_invoice_id,
6156 			              P_Prepay_Line_Number          => l_inv_line_list(i).prepay_line_number,
6157 			              P_Application_Id              => l_prepay_doc_application_id,
6158 			              P_Entity_code                 => l_prepay_doc_entity_code,
6159 			              P_Event_Class_Code            => l_prepay_doc_event_class_code,
6160 			              P_Invoice_Number              => l_prepay_doc_number,
6161 			              P_Invoice_Date                => l_prepay_doc_date,
6162 			              P_Error_Code                  => P_error_code,
6163 			              P_Calling_Sequence            => l_curr_calling_sequence)) THEN
6164 
6165 		        l_return_status := FALSE;
6166 		END IF;
6167 
6168 		l_applied_from_trx_id         := l_inv_line_list(i).prepay_invoice_id;
6169 		l_applied_from_line_id        := l_inv_line_list(i).prepay_line_number;
6170 		l_applied_from_trx_level_type := 'LINE';
6171 
6172 	    ELSIF p_calling_mode = 'RECOUPMENT' THEN
6173 
6174                   IF NOT (AP_ETAX_UTILITY_PKG.Get_Prepay_Invoice_Info(
6175                                       P_Prepay_Invoice_Id           => l_inv_line_list(i).invoice_id,
6176                                       P_Prepay_Line_Number          => l_inv_line_list(i).line_number,
6177                                       P_Application_Id              => l_prepay_doc_application_id,
6178                                       P_Entity_code                 => l_prepay_doc_entity_code,
6179                                       P_Event_Class_Code            => l_prepay_doc_event_class_code,
6180                                       P_Invoice_Number              => l_prepay_doc_number,
6181                                       P_Invoice_Date                => l_prepay_doc_date,
6182                                       P_Error_Code                  => P_error_code,
6183                                       P_Calling_Sequence            => l_curr_calling_sequence)) THEN
6184 
6185                               l_return_status := FALSE;
6186                   END IF;
6187 
6188                   l_applied_from_trx_id         := l_inv_line_list(i).invoice_id;
6189                   l_applied_from_line_id        := l_inv_line_list(i).line_number;
6190                   l_applied_from_trx_level_type := 'LINE';
6191 
6192 	    ELSE
6193 	       l_prepay_doc_application_id   := Null;
6194 	       l_prepay_doc_entity_code      := Null;
6195 	       l_prepay_doc_event_class_code := Null;
6196 	       l_prepay_doc_number           := Null;
6197 	       l_prepay_doc_date             := Null;
6198 	       l_applied_from_trx_level_type := Null;
6199                l_applied_from_trx_id         := Null;
6200                l_applied_from_line_id        := Null;
6201 	    END IF;
6202         END IF;
6203 
6204          -------------------------------------------------------------------
6205           l_debug_info := 'Get Additional Correction Invoice Info ';
6206          -------------------------------------------------------------------
6207 
6208          IF ( l_return_status = TRUE AND
6209               l_inv_line_list(i).corrected_inv_id IS NOT NULL AND
6210               l_inv_line_list(i).corrected_line_number IS NOT NULL) THEN
6211 
6212            IF NOT (AP_ETAX_UTILITY_PKG.Get_Corrected_Invoice_Info(
6213               P_Corrected_Invoice_Id        => l_inv_line_list(i).corrected_inv_id,
6214               P_Corrected_Line_Number       => l_inv_line_list(i).corrected_line_number,
6215               P_Application_Id              => l_adj_doc_application_id,
6216               P_Entity_code                 => l_adj_doc_entity_code,
6217               P_Event_Class_Code            => l_adj_doc_event_class_code,
6218               P_Invoice_Number              => l_adj_doc_number,
6219               P_Invoice_Date                => l_adj_doc_date,
6220               P_Error_Code                  => P_error_code,
6221               P_Calling_Sequence            => l_curr_calling_sequence)) THEN
6222 
6223               l_return_status := FALSE;
6224           END IF;
6225 
6226           l_adj_doc_trx_level_type := 'LINE';
6227 
6228          ELSE
6229             l_adj_doc_application_id   := Null;
6230             l_adj_doc_entity_code      := Null;
6231             l_adj_doc_event_class_code := Null;
6232             l_adj_doc_number	       := Null;
6233             l_adj_doc_date             := Null;
6234 	    l_adj_doc_trx_level_type   := Null;
6235          END IF;
6236 
6237          -------------------------------------------------------------------
6238           l_debug_info := 'Get line_amt_includes_tax_flag';
6239          -------------------------------------------------------------------
6240 
6241          IF (l_inv_line_list(i).po_line_location_id IS NOT NULL) THEN
6242            -- NONE
6243            l_line_amt_includes_tax_flag := 'N';
6244 
6245          ELSE
6246            IF (p_calling_mode = 'CALCULATE QUOTE')
6247               OR
6248               (p_invoice_header_rec.invoice_type_lookup_code = 'EXPENSE REPORT'
6249                and nvl(l_inv_line_list(i).line_type_lookup_code, 'N') <> 'PREPAY') THEN
6250              -- ALL
6251              l_line_amt_includes_tax_flag := 'A';
6252 
6253            ELSE
6254              -- STANDARD
6255              l_line_amt_includes_tax_flag := 'S';
6256 
6257            END IF;
6258          END IF;
6259 
6260 	 BEGIN
6261               IF (l_inv_line_list(i).tax_already_calculated_flag = 'Y') THEN
6262 
6263 		 SELECT line_amt_includes_tax_flag
6264 		   INTO l_init_line_amt_incl_tax_fg
6265 		   FROM zx_lines_det_factors
6266 		  WHERE application_id = 200
6267 		    AND entity_code    = 'AP_INVOICES'
6268 		    AND event_class_code IN ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
6269 		    AND trx_id         = l_inv_line_list(i).invoice_id
6270 		    AND trx_line_id    = l_inv_line_list(i).line_number
6271 		    AND rownum         = 1;
6272 
6273 		IF l_init_line_amt_incl_tax_fg IS NOT NULL THEN
6274 		   l_line_amt_includes_tax_flag := l_init_line_amt_incl_tax_fg;
6275 		END IF;
6276 
6277 	      END IF;
6278          EXCEPTION
6279 		WHEN OTHERS THEN
6280 			NULL;
6281 	 END;
6282 
6283          -------------------------------------------------------------------
6284           l_debug_info := 'Get ctrl_hdr_tx_appl_flag';
6285          -------------------------------------------------------------------
6286          IF P_Invoice_Header_Rec.control_amount IS NOT NULL THEN
6287            l_ctrl_hdr_tx_appl_flag := 'Y';
6288          ELSE
6289            l_ctrl_hdr_tx_appl_flag := 'N';
6290          END IF;
6291 
6292          -------------------------------------------------------------------
6293           l_debug_info := 'Get line_class';
6294          -------------------------------------------------------------------
6295 
6296          IF (l_return_status = TRUE) THEN
6297 
6298 	      IF NOT (AP_ETAX_UTILITY_PKG.Get_Line_Class(
6299 		             P_Invoice_Type_Lookup_Code    => P_Invoice_Header_Rec.invoice_type_lookup_code,
6300 		             P_Inv_Line_Type               => l_inv_line_list(i).line_type_lookup_code,
6301 		             P_Line_Location_Id            => l_inv_line_list(i).po_line_location_id,
6302 		             P_Line_Class                  => l_line_class,
6303 		             P_Error_Code                  => P_error_code,
6304 		             P_Calling_Sequence            => l_curr_calling_sequence)) THEN
6305 
6306                  l_return_status := FALSE;
6307              END IF;
6308          END IF;
6309 
6310        --Bug6908977 STARTS
6311        IF l_inv_line_list(i).match_type IN --Bug6965650
6312          ('ITEM_TO_PO','ITEM_TO_RECEIPT','ITEM_TO_SERVICE_PO',
6313           'ITEM_TO_SERVICE_RECEIPT','PRICE_CORRECTION','QTY_CORRECTION',
6314           'AMOUNT_CORRECTION') THEN
6315 
6316           IF  l_inv_line_list(i).po_line_location_id IS NOT NULL THEN
6317 
6318               SELECT pd.code_combination_id
6319                 INTO l_default_ccid
6320                 FROM po_distributions_all pd
6321                WHERE pd.line_location_id = l_inv_line_list(i).po_line_location_id
6322                  AND rownum = 1;
6323 
6324               l_inv_line_list(i).default_dist_ccid:=l_default_ccid;
6325 
6326               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6327                   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
6328                                  '1 Default CCID passed from PO '||l_default_ccid);
6329               END IF;
6330 
6331           ELSIF l_inv_line_list(i).po_distribution_id IS NOT NULL THEN
6332 
6333               SELECT pd.code_combination_id
6334                 INTO l_default_ccid
6335                 FROM po_distributions_all pd
6336                WHERE pd.line_location_id =
6337                     (SELECT pod.line_location_id
6338                        FROM po_distributions_all pod
6339                       WHERE po_distribution_id = l_inv_line_list(i).po_distribution_id)
6340                  AND rownum = 1;
6341 
6342               l_inv_line_list(i).default_dist_ccid:=l_default_ccid;
6343 
6344               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6345                   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
6346                                  '3 Default CCID passed from PO '||l_default_ccid);
6347               END IF;
6348 
6349           ELSIF l_inv_line_list(i).rcv_shipment_line_id IS NOT NULL THEN
6350 
6351               SELECT pd.code_combination_id
6352                 INTO l_default_ccid
6353                 FROM po_distributions_all pd
6354                WHERE pd.line_location_id =
6355                      (SELECT rcv.po_line_location_id
6356                         FROM rcv_shipment_lines rcv
6357                        WHERE rcv.shipment_line_id = l_inv_line_list(i).rcv_shipment_line_id)
6358                  AND rownum = 1;
6359 
6360               l_inv_line_list(i).default_dist_ccid:=l_default_ccid;
6361 
6362               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6363                   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
6364                                  '4 Default CCID passed from PO '||l_default_ccid);
6365               END IF;
6366 
6367           ELSIF l_inv_line_list(i).rcv_transaction_id IS NOT NULL THEN
6368 
6369               SELECT pd.code_combination_id
6370                 INTO l_default_ccid
6371                 FROM po_distributions_all pd
6372                WHERE pd.line_location_id =
6373                      (SELECT rcv.po_line_location_id
6374                         FROM rcv_transactions rcv
6375                        WHERE rcv.transaction_id = l_inv_line_list(i).rcv_transaction_id)
6376                  AND rownum = 1;
6377 
6378               l_inv_line_list(i).default_dist_ccid:=l_default_ccid;
6379 
6380               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6381                   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
6382                                  '5 Default CCID passed from PO '||l_default_ccid);
6383               END IF;
6384           ELSE
6385               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6386                   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
6387                                  '6 Default CCID passed from line rather than of PO Distribution');
6388               END IF;
6389           END IF;
6390       END IF;
6391       --Bug6908977 ENDS
6392 
6393       -- bug7350421
6394       IF (l_inv_line_list(i).default_dist_ccid IS NULL AND
6395           l_inv_line_list(i).line_type_lookup_code <> 'PREPAY') THEN
6396 
6397          BEGIN
6398 
6399           SELECT aerd.code_combination_id
6400             INTO l_inv_line_list(i).default_dist_ccid
6401             FROM ap_exp_report_dists_all aerd,
6402                  ap_expense_report_lines_all aerl,
6403                  ap_invoices_all ai
6404            WHERE aerd.report_header_id = l_inv_line_list(i).reference_key1
6405              AND aerd.report_line_id = l_inv_line_list(i).reference_key2
6406              AND aerd.report_line_id = aerl.report_line_id
6407              AND aerd.report_header_id = aerl.report_header_id
6408              AND ai.invoice_id = l_inv_line_list(i).invoice_id
6409              AND ai.invoice_type_lookup_code = 'EXPENSE REPORT'
6410              AND rownum = 1;
6411 
6412          EXCEPTION
6413           WHEN OTHERS THEN
6414             NULL;
6415         END;
6416 
6417        END IF;
6418 
6419       --
6420       -- Bug 5565310: Commented out the below code as this is replaced by
6421       --              code in matching packages and in invoice workbench.
6422       --
6423       -- Bug 5605359: Enabled the code only for invoices created from ISP.
6424       --              Ideally the tax determining attributes should be
6425       --              added to the ISP UI instead of defaulting here.
6426       --
6427 
6428       IF  P_Invoice_Header_Rec.source = 'ISP' THEN
6429 
6430         IF  (l_inv_line_list(i).po_header_id         IS NOT NULL AND
6431              l_inv_line_list(i).po_line_location_id  IS NOT NULL AND
6432              l_inv_line_list(i).primary_intended_use        IS NULL AND
6433              l_inv_line_list(i).product_type	            IS NULL AND
6434              l_inv_line_list(i).product_category            IS NULL AND
6435              l_inv_line_list(i).product_fisc_classification IS NULL AND
6436              l_inv_line_list(i).user_defined_fisc_class     IS NULL AND
6437 	     l_inv_line_list(i).tax_classification_code     IS NULL ) THEN
6438 
6439              -------------------------------------------------------------------
6440              l_debug_info := 'ISP: get_po_tax_attributes';
6441              -------------------------------------------------------------------
6442              get_po_tax_attributes
6443                         (
6444                          p_application_id              => l_ref_doc_application_id,
6445                          p_org_id                      => P_Invoice_Header_Rec.org_id,
6446                          p_entity_code                 => l_ref_doc_entity_code,
6447                          p_event_class_code            => l_ref_doc_event_class_code,
6448                          p_trx_level_type              => 'SHIPMENT',
6449                          p_trx_id                      => l_ref_doc_trx_id,
6450                          p_trx_line_id                 => l_inv_line_list(i).po_line_location_id,
6451                          x_line_intended_use           => l_intended_use,
6452                          x_product_type                => l_product_type,
6453                          x_product_category            => l_product_category,
6454                          x_product_fisc_classification => l_product_fisc_class,
6455                          x_user_defined_fisc_class     => l_user_defined_fisc_class,
6456                          x_assessable_value            => l_assessable_value,
6457 			 x_tax_classification_code     => l_dflt_tax_class_code
6458                         );
6459 
6460         ELSE
6461            l_intended_use            := Null;
6462            l_product_type            := Null;
6463            l_product_category        := Null;
6464            l_product_fisc_class      := Null;
6465            l_user_defined_fisc_class := Null;
6466            l_assessable_value        := Null;
6467            l_dflt_tax_class_code     := Null;
6468 
6469         END IF;
6470 
6471         IF (l_dflt_tax_class_code IS NULL
6472             AND l_inv_line_list(i).tax_classification_code IS NULL) THEN
6473 
6474              -------------------------------------------------------------------
6475              l_debug_info := 'ISP: ZX_PKG.get_default_tax_classification';
6476              -------------------------------------------------------------------
6477 
6478              ZX_AP_TAX_CLASSIFICATN_DEF_PKG.get_default_tax_classification
6479 		            (p_ref_doc_application_id           => l_ref_doc_application_id,
6480 		             p_ref_doc_entity_code              => l_ref_doc_entity_code,
6481 		             p_ref_doc_event_class_code         => l_ref_doc_event_class_code,
6482 		             p_ref_doc_trx_id                   => l_ref_doc_trx_id,
6483 		             p_ref_doc_line_id                  => l_inv_line_list(i).po_line_location_id,
6484 		             p_ref_doc_trx_level_type           => 'SHIPMENT',
6485 		             p_vendor_id                        => P_Invoice_Header_Rec.vendor_id,
6486 		             p_vendor_site_id                   => P_Invoice_Header_Rec.vendor_site_id,
6487 		             p_code_combination_id              => l_inv_line_list(i).default_dist_ccid,
6488 		             p_concatenated_segments            => null,
6489 		             p_templ_tax_classification_cd      => null,
6490 		             p_ship_to_location_id              => l_inv_line_list(i).ship_to_location_id,
6491 		             p_ship_to_loc_org_id               => null,
6492 		             p_inventory_item_id                => l_inv_line_list(i).inventory_item_id,
6493 		             p_item_org_id                      => l_product_org_id,
6494 		             p_tax_classification_code          => l_dflt_tax_class_code,
6495 		             p_allow_tax_code_override_flag     => l_allow_tax_code_override,
6496 		             APPL_SHORT_NAME                    => 'SQLAP',
6497 		             FUNC_SHORT_NAME                    => 'NONE',
6498 		             p_calling_sequence                 => 'AP_ETAX_SERVICES_PKG',
6499 		             p_event_class_code                 => P_Event_Class_Code,
6500 		             p_entity_code                      => 'AP_INVOICES',
6501 		             p_application_id                   => 200,
6502 		             p_internal_organization_id         => P_Invoice_Header_Rec.org_id);
6503 
6504         END IF;
6505       END IF; -- For ISP invoices only
6506 
6507         IF g_manual_tax_lines = 'Y' and l_manual_tax_line_rcv_mtch = 'N' THEN  ---for 6014115
6508                 l_applied_to_application_id   := NULL;
6509                 l_applied_to_entity_code      := Null;
6510                 l_applied_to_event_class_code := Null;
6511                 l_trx_receipt_date            := Null;
6512         END IF;
6513         IF g_manual_tax_lines = 'Y'
6514            and l_prepay_doc_application_id is null
6515            and l_adj_doc_application_id    is null
6516            and l_applied_to_application_id is null THEN
6517 
6518 	   l_line_level_action := 'CREATE_WITH_TAX';
6519 
6520 	END IF;
6521 
6522         -------------------------------------------------------------------
6523         l_debug_info := 'Populate pl/sql table';
6524         -------------------------------------------------------------------
6525         IF (l_return_status = TRUE ) THEN
6526 
6527           trans_lines(i).application_id		:= ap_etax_pkg.ap_application_id;
6528           trans_lines(i).entity_code		:= ap_etax_pkg.ap_entity_code;
6529           trans_lines(i).event_class_code	:= p_event_class_code;
6530 
6531 	  IF p_calling_mode = 'RECOUPMENT' THEN
6532 
6533              trans_lines(i).trx_id      := P_Invoice_Header_Rec.invoice_id;
6534              trans_lines(i).trx_line_id := -1 * (l_inv_line_list(i).invoice_id || l_inv_line_list(i).line_number || p_line_number);
6535 
6536              SELECT sum(amount)
6537                INTO trans_lines(i).line_amt
6538                FROM ap_invoice_distributions_all aids
6539               WHERE invoice_id            = p_invoice_header_rec.invoice_id
6540 		AND invoice_line_number   = p_line_number
6541                 AND line_type_lookup_code = 'PREPAY'
6542                 AND EXISTS
6543                       (select 'Prepayment Invoice'
6544                          from ap_invoice_distributions_all aidp
6545                          where aidp.invoice_distribution_id = aids.prepay_distribution_id
6546                            and aidp.invoice_id              = l_inv_line_list(i).invoice_id
6547                            and aidp.invoice_line_number     = l_inv_line_list(i).line_number);
6548 
6549 	  ELSE
6550 
6551 	     trans_lines(i).trx_id      := l_inv_line_list(i).invoice_id;
6552 	     trans_lines(i).trx_line_id := l_inv_line_list(i).line_number;
6553 	     trans_lines(i).line_amt    := l_inv_line_list(i).amount + nvl(l_inv_line_list(i).retained_amount,0);
6554 
6555 	  END IF;
6556 
6557           trans_lines(i).trx_level_type 		:= 'LINE';
6558           trans_lines(i).line_level_action 		:= l_line_level_action;
6559 	  trans_lines(i).line_class 			:= l_line_class;
6560 
6561           trans_lines(i).trx_receipt_date 		:= l_trx_receipt_date;
6562           trans_lines(i).trx_line_type 			:= l_inv_line_list(i).line_type_lookup_code;
6563           trans_lines(i).trx_line_date 			:= nvl(l_inv_line_list(i).start_expense_date, p_invoice_header_rec.invoice_date);
6564           trans_lines(i).trx_line_number 		:= l_inv_line_list(i).line_number;
6565           trans_lines(i).trx_line_description 		:= l_inv_line_list(i).description;
6566           trans_lines(i).trx_line_gl_date 		:= l_inv_line_list(i).accounting_date;
6567           trans_lines(i).account_ccid 			:= l_inv_line_list(i).default_dist_ccid;
6568 
6569           trans_lines(i).trx_line_quantity		:= nvl(l_inv_line_list(i).quantity_invoiced, 1);
6570           trans_lines(i).unit_price 			:= nvl(l_inv_line_list(i).unit_price, trans_lines(i).line_amt);
6571           trans_lines(i).uom_code			:= l_uom_code;
6572 
6573           trans_lines(i).trx_business_category 		:= l_inv_line_list(i).trx_business_category;
6574           trans_lines(i).line_intended_use 		:= nvl(l_inv_line_list(i).primary_intended_use,l_intended_use);
6575           trans_lines(i).user_defined_fisc_class 	:= nvl(l_inv_line_list(i).user_defined_fisc_class,l_user_defined_fisc_class);
6576           trans_lines(i).product_fisc_classification	:= nvl(l_inv_line_list(i).product_fisc_classification,l_product_fisc_class);
6577           trans_lines(i).assessable_value 		:= nvl(l_inv_line_list(i).assessable_value,l_assessable_value);
6578           trans_lines(i).input_tax_classification_code	:= nvl(l_inv_line_list(i).tax_classification_code,l_dflt_tax_class_code);
6579 
6580           trans_lines(i).product_id 			:= l_inv_line_list(i).inventory_item_id;
6581           trans_lines(i).product_org_id			:= l_product_org_id;
6582           trans_lines(i).product_category		:= nvl(l_inv_line_list(i).product_category,l_product_category);
6583           trans_lines(i).product_type			:= nvl(l_inv_line_list(i).product_type,l_product_type);
6584           trans_lines(i).product_description 		:= l_inv_line_list(i).item_description;
6585           trans_lines(i).fob_point			:= l_fob_point;
6586 
6587           -- AP is not going to pass this parameter.  eTax is aware of this and will derive the value
6588           -- trans_lines(i).product_code
6589 
6590           -- 7262269
6591           IF l_inv_line_list(i).po_line_location_id IS NOT NULL THEN
6592              l_ship_to_party_id := get_po_ship_to_org_id (l_inv_line_list(i).po_line_location_id);
6593           ELSE
6594              l_ship_to_party_id := l_inv_line_list(i).org_id;
6595           END IF;
6596 
6597           trans_lines(i).ship_to_party_id		:= l_ship_to_party_id;
6598           -- 7262269
6599           trans_lines(i).ship_from_party_id		:= P_Invoice_Header_Rec.party_id;
6600 
6601           trans_lines(i).bill_to_party_id		:= l_inv_line_list(i).org_id;
6602           trans_lines(i).bill_from_party_id		:= P_Invoice_Header_Rec.party_id;
6603 
6604           trans_lines(i).ship_from_party_site_id	:= P_Invoice_Header_Rec.party_site_id;
6605           trans_lines(i).bill_from_party_site_id	:= P_Invoice_Header_Rec.party_site_id;
6606 
6607           trans_lines(i).ship_to_location_id		:= l_inv_line_list(i).ship_to_location_id;
6608 	  trans_lines(i).ship_from_location_id		:= l_location_id;
6609           trans_lines(i).bill_to_location_id		:= l_bill_to_location_id;
6610           trans_lines(i).bill_from_location_id          := l_location_id;
6611 
6612           trans_lines(i).ref_doc_application_id 	:= l_ref_doc_application_id;
6613           trans_lines(i).ref_doc_entity_code 		:= l_ref_doc_entity_code;
6614           trans_lines(i).ref_doc_event_class_code 	:= l_ref_doc_event_class_code;
6615           trans_lines(i).ref_doc_trx_id 		:= l_ref_doc_trx_id;
6616 	  trans_lines(i).ref_doc_trx_level_type 	:= l_ref_doc_trx_level_type;
6617           trans_lines(i).ref_doc_line_id 		:= l_inv_line_list(i).po_line_location_id;
6618           trans_lines(i).ref_doc_line_quantity 		:= l_ref_doc_line_quantity;
6619 
6620           trans_lines(i).applied_from_application_id 	:= l_prepay_doc_application_id;
6621           trans_lines(i).applied_from_entity_code 	:= l_prepay_doc_entity_code;
6622           trans_lines(i).applied_from_event_class_code 	:= l_prepay_doc_event_class_code;
6623           trans_lines(i).applied_from_trx_id 		:= l_applied_from_trx_id;
6624           trans_lines(i).applied_from_trx_level_type 	:= l_applied_from_trx_level_type;
6625           trans_lines(i).applied_from_line_id 		:= l_applied_from_line_id;
6626 
6627           trans_lines(i).adjusted_doc_application_id 	:= l_adj_doc_application_id;
6628           trans_lines(i).adjusted_doc_entity_code 	:= l_adj_doc_entity_code;
6629           trans_lines(i).adjusted_doc_event_class_code 	:= l_adj_doc_event_class_code;
6630           trans_lines(i).adjusted_doc_trx_id 		:= l_inv_line_list(i).corrected_inv_id;
6631           trans_lines(i).adjusted_doc_line_id 		:= l_inv_line_list(i).corrected_line_number;
6632 	  trans_lines(i).adjusted_doc_trx_level_type 	:= l_adj_doc_trx_level_type;
6633           trans_lines(i).adjusted_doc_number 		:= l_adj_doc_number;
6634           trans_lines(i).adjusted_doc_date 		:= l_adj_doc_date;
6635 
6636           trans_lines(i).applied_to_application_id 	:= l_applied_to_application_id;
6637           trans_lines(i).applied_to_entity_code 	:= l_applied_to_entity_code;
6638           trans_lines(i).applied_to_event_class_code 	:= l_applied_to_event_class_code;
6639           IF g_manual_tax_lines = 'Y' and l_manual_tax_line_rcv_mtch = 'N' THEN  ---for 6014115
6640              trans_lines(i).applied_to_trx_id      := NULL;
6641           ELSE
6642              trans_lines(i).applied_to_trx_id 		:= l_inv_line_list(i).rcv_transaction_id;
6643           END IF;
6644 
6645           IF g_manual_tax_lines = 'Y'THEN---for 6014115
6646              IF  l_manual_tax_line_rcv_mtch = 'N' THEN
6647                  trans_lines(i).applied_to_trx_line_id :=NULL;
6648               END IF;
6649           ELSIF l_inv_line_list(i).rcv_transaction_id IS NOT NULL THEN
6650                 trans_lines(i).applied_to_trx_line_id 	:= l_inv_line_list(i).po_line_location_id;
6651           END IF;
6652 
6653           trans_lines(i).source_application_id   	:= l_inv_line_list(i).source_application_id;
6654           trans_lines(i).source_entity_code	 	:= l_inv_line_list(i).source_entity_code;
6655           trans_lines(i).source_event_class_code 	:= l_inv_line_list(i).source_event_class_code;
6656           trans_lines(i).source_trx_id		 	:= l_inv_line_list(i).source_trx_id;
6657           trans_lines(i).source_line_id		 	:= l_inv_line_list(i).source_line_id;
6658           trans_lines(i).source_trx_level_type	 	:= l_inv_line_list(i).source_trx_level_type;
6659 
6660           trans_lines(i).merchant_party_name 		:= l_inv_line_list(i).merchant_name;
6661           trans_lines(i).merchant_party_document_number := l_inv_line_list(i).merchant_document_number;
6662           trans_lines(i).merchant_party_reference 	:= l_inv_line_list(i).merchant_reference;
6663           trans_lines(i).merchant_party_taxpayer_id 	:= l_inv_line_list(i).merchant_taxpayer_id;
6664           trans_lines(i).merchant_party_tax_reg_number 	:= l_inv_line_list(i).merchant_tax_reg_number;
6665           trans_lines(i).merchant_party_country 	:= l_inv_line_list(i).country_of_supply;
6666 
6667           trans_lines(i).line_amt_includes_tax_flag 	:= l_line_amt_includes_tax_flag;
6668           trans_lines(i).historical_flag 		:= NVL(l_inv_line_list(i).historical_flag, 'N'); -- Bug 7117591
6669 /*NVL(P_Invoice_Header_Rec.historical_flag, 'N');*/
6670           trans_lines(i).ctrl_hdr_tx_appl_flag 		:= l_ctrl_hdr_tx_appl_flag;
6671           trans_lines(i).ctrl_total_line_tx_amt 	:= l_inv_line_list(i).control_amount;
6672 
6673           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6674               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_transaction_lines_gt values ');
6675 	          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: ' || trans_lines(i).event_class_code);
6676 	          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '           || trans_lines(i).trx_id);
6677 	          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_id: '      || trans_lines(i).trx_line_id);
6678 	          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_level_type: '   || trans_lines(i).trx_level_type);
6679               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_type: '    || trans_lines(i).trx_line_type );
6680 	          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_level_action: '|| trans_lines(i).line_level_action);
6681 	          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_class: '       || trans_lines(i).line_class);
6682 	          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_amt: '         || trans_lines(i).line_amt);
6683               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'unit_price: '       || trans_lines(i).unit_price);
6684               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'ship_to_party_id: ' || trans_lines(i).ship_to_party_id);
6685           END IF;
6686          END IF;
6687        END IF;
6688      END LOOP;
6689 
6690     END IF;
6691 
6692     -------------------------------------------------------------------
6693     l_debug_info := 'Bulk Insert into global temp table';
6694     -------------------------------------------------------------------
6695     IF ( l_return_status = TRUE ) THEN
6696       FORALL m IN trans_lines.FIRST..trans_lines.LAST
6697         INSERT INTO zx_transaction_lines_gt
6698         VALUES trans_lines(m);
6699     END IF;
6700 
6701     RETURN l_return_status;
6702 
6703   EXCEPTION
6704     WHEN OTHERS THEN
6705       IF (SQLCODE <> -20001) THEN
6706         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
6707         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
6708         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
6709         FND_MESSAGE.SET_TOKEN('PARAMETERS',
6710           ' P_Invoice_Header_Rec = '||P_Invoice_Header_Rec.Invoice_Id||
6711           ' P_Calling_Mode ='||P_Calling_Mode||
6712           ' P_Error_Code = '||P_Error_Code||
6713           ' P_Calling_Sequence = '||P_Calling_Sequence);
6714         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
6715       END IF;
6716 
6717       APP_EXCEPTION.RAISE_EXCEPTION;
6718 
6719   END Populate_Lines_GT;
6720 
6721 /*=============================================================================
6722  |  FUNCTION - Populate_Lines_Import_GT()
6723  |
6724  |  DESCRIPTION
6725  |      This function will get additional information required to populate the
6726  |      ZX_TRANSACTION_LINES_GT
6727  |      This function returns TRUE if the population of the temp table goes
6728  |      through successfully.  Otherwise, FALSE.
6729  |
6730  |  PARAMETERS
6731  |      P_Invoice_Header_Rec - record with invoice header information
6732  |      P_Invoice_Lines_Tab - List of trx and tax lines for the invoice
6733  |        existing in the ap_invoice_lines_interface table
6734  |      P_Calling_Mode - calling mode. it is used to
6735  |      P_Event_Class_Code - Event class code for document
6736  |      P_error_code - Error code to be returned
6737  |      P_calling_sequence -  Calling sequence
6738  |
6739  |  MODIFICATION HISTORY
6740  |    DATE          Author         Action
6741  |    20-JAN-2004   SYIDNER        Created
6742  |
6743  *============================================================================*/
6744 
6745   FUNCTION Populate_Lines_Import_GT(
6746              P_Invoice_Header_Rec      IN AP_IMPORT_INVOICES_PKG.r_invoice_info_rec,
6747              P_Inv_Line_List           IN AP_IMPORT_INVOICES_PKG.t_lines_table,
6748              P_Calling_Mode            IN VARCHAR2,
6749              P_Event_Class_Code        IN VARCHAR2,
6750              P_Error_Code              OUT NOCOPY VARCHAR2,
6751              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN IS
6752 
6753     l_debug_info                 VARCHAR2(240);
6754     l_curr_calling_sequence      VARCHAR2(4000);
6755 
6756     TYPE Trx_Lines_Tab_Type  IS TABLE OF zx_transaction_lines_gt%ROWTYPE;
6757     TYPE Tax_Lines_Tab_Type  IS TABLE OF zx_import_tax_lines_gt%ROWTYPE;
6758     TYPE Link_Lines_Tab_Type IS TABLE OF zx_trx_tax_link_gt%ROWTYPE;
6759 
6760     trans_lines                     Trx_Lines_Tab_Type  := Trx_Lines_Tab_Type();
6761     tax_lines                       Tax_Lines_Tab_Type  := Tax_Lines_Tab_Type();
6762     link_lines                      Link_Lines_Tab_Type := Link_Lines_Tab_Type();
6763 
6764     l_ctrl_hdr_tx_appl_flag         zx_transaction_lines_gt.ctrl_hdr_tx_appl_flag%TYPE;
6765     l_line_control_amount           zx_transaction_lines_gt.ctrl_total_line_tx_amt%TYPE;
6766     l_line_level_action             zx_transaction_lines_gt.line_level_action%TYPE;
6767     l_line_class                    zx_transaction_lines_gt.line_class%TYPE;
6768     l_line_amt_includes_tax_flag    zx_transaction_lines_gt.line_amt_includes_tax_flag%TYPE;
6769 
6770     l_product_org_id		    zx_transaction_lines_gt.product_org_id%TYPE;
6771     l_uom_code			    mtl_units_of_measure.uom_code%TYPE;
6772     l_fob_point                     po_vendor_sites_all.fob_lookup_code%TYPE;
6773 
6774     l_po_line_location_id           ap_invoice_lines_interface.po_line_location_id%TYPE;
6775     l_location_id         	    zx_transaction_lines_gt.ship_from_location_id%type;
6776     l_ship_to_location_id           ap_supplier_sites_all.ship_to_location_id%type;
6777     l_bill_to_location_id           zx_transaction_lines_gt.bill_to_location_id%TYPE;
6778 
6779     -- Purchase Order Info
6780     l_ref_doc_application_id	    zx_transaction_lines_gt.ref_doc_application_id%TYPE;
6781     l_ref_doc_entity_code	    zx_transaction_lines_gt.ref_doc_entity_code%TYPE;
6782     l_ref_doc_event_class_code	    zx_transaction_lines_gt.ref_doc_event_class_code%TYPE;
6783     l_ref_doc_line_quantity	    zx_transaction_lines_gt.ref_doc_line_quantity%TYPE;
6784     l_ref_doc_trx_level_type	    zx_transaction_lines_gt.ref_doc_trx_level_type%TYPE;
6785     l_ref_doc_trx_id                zx_transaction_lines_gt.ref_doc_trx_id%TYPE;
6786     l_po_header_curr_conv_rate	    po_headers_all.rate%TYPE;
6787     l_dummy			    number;
6788 
6789     -- Receipt Info
6790     l_applied_to_application_id	    zx_transaction_lines_gt.applied_to_application_id%TYPE;
6791     l_applied_to_entity_code	    zx_transaction_lines_gt.applied_to_entity_code%TYPE;
6792     l_applied_to_event_class_code   zx_transaction_lines_gt.applied_to_event_class_code%TYPE;
6793     l_trx_receipt_date		    zx_transaction_lines_gt.trx_receipt_date%TYPE;
6794 
6795     -- Correction Invoices
6796     l_adj_doc_application_id	    zx_transaction_lines_gt.adjusted_doc_application_id%TYPE;
6797     l_adj_doc_entity_code	    zx_transaction_lines_gt.adjusted_doc_entity_code%TYPE;
6798     l_adj_doc_event_class_code	    zx_transaction_lines_gt.adjusted_doc_event_class_code%TYPE;
6799     l_adj_doc_number		    zx_transaction_lines_gt.adjusted_doc_number%TYPE;
6800     l_adj_doc_date		    zx_transaction_lines_gt.adjusted_doc_date%TYPE;
6801 
6802     l_dflt_tax_class_code	    zx_transaction_lines_gt.input_tax_classification_code%type;
6803     l_allow_tax_code_override	    varchar2(10);
6804 
6805     l_return_status                 BOOLEAN := TRUE;
6806     j                               INT := 1;
6807     k                               INT := 1;
6808 
6809     l_prorating_total               NUMBER;
6810     l_total_prorated                NUMBER;
6811 
6812     l_ship_to_party_id          po_line_locations_all.ship_to_organization_id%type; -- 7262269
6813 
6814     l_api_name                    CONSTANT VARCHAR2(100) := 'Populate_Lines_Import_GT';
6815 
6816   BEGIN
6817 
6818     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Populate_Lines_Import_GT<-' ||
6819                                P_calling_sequence;
6820 
6821     ----------------------------------------------------------------------
6822     l_debug_info := 'Step 1: Get location_id for org_id';
6823     ----------------------------------------------------------------------
6824     BEGIN
6825       SELECT location_id
6826         INTO l_bill_to_location_id
6827         FROM hr_all_organization_units
6828        WHERE organization_id = P_Invoice_Header_Rec.org_id;
6829 
6830     EXCEPTION
6831       WHEN no_data_found THEN
6832          l_bill_to_location_id := null;
6833     END;
6834 
6835     ----------------------------------------------------------------------
6836     l_debug_info := 'Step 1.1: Get location_id for vendor site';
6837     ----------------------------------------------------------------------
6838     BEGIN
6839       SELECT location_id, ship_to_location_id, fob_lookup_code
6840         INTO l_location_id, l_ship_to_location_id, l_fob_point
6841         FROM ap_supplier_sites_all
6842        WHERE vendor_site_id = P_Invoice_Header_Rec.vendor_site_id;
6843 
6844     EXCEPTION
6845       WHEN no_data_found THEN
6846          l_location_id		:= null;
6847 	 l_ship_to_location_id	:= null;
6848 	 l_fob_point		:= null;
6849     END;
6850 
6851     ----------------------------------------------------------------------
6852     l_debug_info := 'Step 3: Determine if the invoice is tax-only.  If the '||
6853                     ' invoice is not tax-only user line level action CREATE'||
6854                     ' always';
6855     ----------------------------------------------------------------------
6856     IF (P_Inv_Line_List.COUNT <> 0) THEN
6857 
6858     FOR i IN P_Inv_Line_List.FIRST..P_Inv_Line_List.LAST LOOP
6859 
6860       -- Invoice is not tax-only.  TRX lines will be populated in the
6861       -- ZX_TRANSAXTION_LINES_GT and TAX lines in ZX_IMPORT_TAX_LINES_GT
6862       -- allocation structure will be store in ZX_TRX_TAX_LINK_GT
6863 
6864       IF ( NVL(P_Invoice_Header_Rec.tax_only_flag, 'N') = 'N' ) THEN
6865 
6866         IF (P_inv_line_list(i).line_type_lookup_code <> 'TAX' ) THEN
6867           -------------------------------------------------------------------
6868           l_debug_info := 'Step 4: Get line_level_action for line ITEM number'||
6869                           P_inv_line_list(i).line_number ;
6870           -------------------------------------------------------------------
6871           l_line_level_action := 'CREATE';
6872 
6873           -------------------------------------------------------------------
6874           l_debug_info := 'Step 5: Get Additional PO matched  info ';
6875           -------------------------------------------------------------------
6876           IF ( P_Inv_Line_List(i).po_line_location_id IS NOT NULL) THEN
6877 
6878               -- this assignment is required since the p_po_line_location_id
6879               -- parameter is IN/OUT.  However, in this case it will not be
6880               -- modified because the po_distribution_id is not provided
6881 
6882             l_po_line_location_id := P_Inv_Line_List(i).po_line_location_id;
6883 
6884             IF NOT (AP_ETAX_UTILITY_PKG.Get_PO_Info(
6885                P_Po_Line_Location_Id         => l_po_line_location_id,
6886                P_PO_Distribution_Id          => null,
6887                P_Application_Id              => l_ref_doc_application_id,
6888                P_Entity_code                 => l_ref_doc_entity_code,
6889                P_Event_Class_Code            => l_ref_doc_event_class_code,
6890                P_PO_Quantity                 => l_ref_doc_line_quantity,
6891                P_Product_Org_Id              => l_product_org_id,
6892                P_Po_Header_Id                => l_ref_doc_trx_id,
6893                P_Po_Header_curr_conv_rate    => l_po_header_curr_conv_rate,
6894 	           P_Uom_Code		             => l_uom_code,
6895                P_Dist_Qty                    => l_dummy,
6896                P_Ship_Price                  => l_dummy,
6897                P_Error_Code                  => P_error_code,
6898                P_Calling_Sequence            => l_curr_calling_sequence)) THEN
6899 
6900               l_return_status := FALSE;
6901             END IF;
6902 
6903             l_ref_doc_trx_level_type := 'SHIPMENT';
6904 
6905           ELSE
6906              l_ref_doc_application_id     := Null;
6907              l_ref_doc_entity_code        := Null;
6908              l_ref_doc_event_class_code   := Null;
6909              l_ref_doc_line_quantity      := Null;
6910              l_product_org_id             := Null;
6911              l_ref_doc_trx_id             := Null;
6912              l_ref_doc_trx_level_type     := Null;
6913           END IF;
6914 
6915           -------------------------------------------------------------------
6916           l_debug_info := 'Step 6: Get Additional receipt matched info ';
6917           -------------------------------------------------------------------
6918           IF ( l_return_status = TRUE AND
6919                P_Inv_Line_List(i).rcv_transaction_id IS NOT NULL) THEN
6920 
6921             IF NOT (AP_ETAX_UTILITY_PKG.Get_Receipt_Info(
6922                P_Rcv_Transaction_Id          => P_Inv_Line_List(i).rcv_transaction_id,
6923                P_Application_Id              => l_applied_to_application_id,
6924                P_Entity_code                 => l_applied_to_entity_code,
6925                P_Event_Class_Code            => l_applied_to_event_class_code,
6926                P_Transaction_Date            => l_trx_receipt_date,
6927                P_Error_Code                  => P_error_code,
6928                P_Calling_Sequence            => l_curr_calling_sequence)) THEN
6929 
6930                l_return_status := FALSE;
6931             END IF;
6932           ELSE
6933              l_applied_to_application_id   := Null;
6934              l_applied_to_entity_code      := Null;
6935              l_applied_to_event_class_code := Null;
6936              l_trx_receipt_date            := Null;
6937           END IF;
6938 
6939           -------------------------------------------------------------------
6940           l_debug_info := 'Step 8: Get Additional Correction Invoice Info ';
6941           -------------------------------------------------------------------
6942           IF ( l_return_status = TRUE AND
6943                P_Inv_Line_List(i).corrected_inv_id IS NOT NULL AND
6944                P_Inv_Line_list(i).price_correct_inv_line_num IS NOT NULL) THEN
6945 
6946             IF NOT (AP_ETAX_UTILITY_PKG.Get_Corrected_Invoice_Info(
6947                P_Corrected_Invoice_Id        => P_Inv_Line_List(i).corrected_inv_id,
6948                P_Corrected_Line_Number       => P_Inv_Line_List(i).price_correct_inv_line_num,
6949                P_Application_Id              => l_adj_doc_application_id,
6950                P_Entity_code                 => l_adj_doc_entity_code,
6951                P_Event_Class_Code            => l_adj_doc_event_class_code,
6952                P_Invoice_Number              => l_adj_doc_number,
6953                P_Invoice_Date                => l_adj_doc_date,
6954                P_Error_Code                  => P_error_code,
6955                P_Calling_Sequence            => l_curr_calling_sequence)) THEN
6956 
6957               l_return_status := FALSE;
6958             END IF;
6959           ELSE
6960             l_adj_doc_application_id   := Null;
6961             l_adj_doc_entity_code      := Null;
6962             l_adj_doc_event_class_code := Null;
6963             l_adj_doc_number           := Null;
6964             l_adj_doc_date             := Null;
6965           END IF;
6966 
6967           -------------------------------------------------------------------
6968           l_debug_info := 'Step 9: Get line_amt_includes_tax_flag';
6969           -------------------------------------------------------------------
6970 
6971           IF (P_Inv_Line_List(i).po_line_location_id IS NOT NULL) THEN
6972             -- NONE
6973             l_line_amt_includes_tax_flag := 'N';
6974 
6975           ELSE
6976            IF (p_calling_mode = 'CALCULATE QUOTE')
6977               OR
6978               (p_invoice_header_rec.invoice_type_lookup_code = 'EXPENSE REPORT'
6979                and nvl(p_inv_line_list(i).line_type_lookup_code, 'N') <> 'PREPAY') THEN
6980              -- ALL
6981              l_line_amt_includes_tax_flag := 'A';
6982 
6983            ELSE
6984             -- STANDARD
6985             l_line_amt_includes_tax_flag := 'S';
6986            END IF;
6987           END IF;
6988 
6989 	  IF l_line_amt_includes_tax_flag = 'S' AND
6990              p_inv_line_list(i).amount_includes_tax_flag IS NOT NULL THEN
6991 	     IF (p_inv_line_list(i).amount_includes_tax_flag = 'Y' OR
6992 		 p_inv_line_list(i).amount_includes_tax_flag = 'A') THEN
6993                  -- ALL
6994                  l_line_amt_includes_tax_flag := 'A';
6995 	     ELSIF p_inv_line_list(i).amount_includes_tax_flag = 'N' THEN
6996                  -- NONE
6997                  l_line_amt_includes_tax_flag := 'N';
6998 	     ELSIF p_inv_line_list(i).amount_includes_tax_flag = 'S' THEN
6999                  -- STANDARD
7000                  l_line_amt_includes_tax_flag := 'S';
7001 	     ELSE
7002                  -- STANDARD
7003                  l_line_amt_includes_tax_flag := 'S';
7004 	     END IF;
7005 	  END IF;
7006 
7007 	  -----------------------------------------------------------------
7008           l_debug_info := 'l_line_amt_includes_tax_flag: '||l_line_amt_includes_tax_flag;
7009           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7010               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7011           END IF;
7012 
7013           l_debug_info := 'p_inv_line_list(i).amount_includes_tax_flag: '||p_inv_line_list(i).amount_includes_tax_flag;
7014           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7015               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7016           END IF;
7017           -----------------------------------------------------------------
7018 
7019           -------------------------------------------------------------------
7020           l_debug_info := 'Step 10: Get ctrl_hdr_tx_appl_flag';
7021           -------------------------------------------------------------------
7022           IF P_Invoice_Header_Rec.control_amount IS NOT NULL THEN
7023             l_ctrl_hdr_tx_appl_flag := 'Y';
7024           ELSE
7025             l_ctrl_hdr_tx_appl_flag := 'N';
7026           END IF;
7027 
7028           -------------------------------------------------------------------
7029           l_debug_info := 'Step 10.1: Get line_class';
7030           -------------------------------------------------------------------
7031           IF (l_return_status = TRUE) THEN
7032 
7033               IF NOT (AP_ETAX_UTILITY_PKG.Get_Line_Class(
7034                              P_Invoice_Type_Lookup_Code    => p_invoice_header_rec.invoice_type_lookup_code,
7035                              P_Inv_Line_Type               => p_inv_line_list(i).line_type_lookup_code,
7036                              P_Line_Location_Id            => p_inv_line_list(i).po_line_location_id,
7037                              P_Line_Class                  => l_line_class,
7038                              P_Error_Code                  => p_error_code,
7039                              P_Calling_Sequence            => l_curr_calling_sequence)) THEN
7040 
7041                   l_return_status := FALSE;
7042               END IF;
7043           END IF;
7044 
7045           IF (p_inv_line_list(i).tax_classification_code IS NULL) THEN
7046 
7047 	      ZX_AP_TAX_CLASSIFICATN_DEF_PKG.get_default_tax_classification
7048 		            (p_ref_doc_application_id           => l_ref_doc_application_id,
7049 		             p_ref_doc_entity_code              => l_ref_doc_entity_code,
7050 		             p_ref_doc_event_class_code         => l_ref_doc_event_class_code,
7051 		             p_ref_doc_trx_id                   => l_ref_doc_trx_id,
7052 		             p_ref_doc_line_id                  => p_inv_line_list(i).po_line_location_id,
7053 		             p_ref_doc_trx_level_type           => 'SHIPMENT',
7054 		             p_vendor_id                        => p_invoice_header_rec.vendor_id,
7055 		             p_vendor_site_id                   => p_invoice_header_rec.vendor_site_id,
7056 		             p_code_combination_id              => p_inv_line_list(i).default_dist_ccid,
7057 		             p_concatenated_segments            => null,
7058 		             p_templ_tax_classification_cd      => null,
7059 		             p_ship_to_location_id              => nvl(p_inv_line_list(i).ship_to_location_id,l_ship_to_location_id),
7060 		             p_ship_to_loc_org_id               => null,
7061 		             p_inventory_item_id                => p_inv_line_list(i).inventory_item_id,
7062 		             p_item_org_id                      => l_product_org_id,
7063 		             p_tax_classification_code          => l_dflt_tax_class_code,
7064 		             p_allow_tax_code_override_flag     => l_allow_tax_code_override,
7065 		             APPL_SHORT_NAME                    => 'SQLAP',
7066 		             FUNC_SHORT_NAME                    => 'NONE',
7067 		             p_calling_sequence                 => 'AP_ETAX_SERVICES_PKG',
7068 		             p_event_class_code                 => p_event_class_code,
7069 		             p_entity_code                      => 'AP_INVOICES',
7070 		             p_application_id                   => 200,
7071 		             p_internal_organization_id         => p_invoice_header_rec.org_id);
7072 
7073 	 END IF;
7074 
7075          -------------------------------------------------------------------
7076          l_debug_info := 'Step 11: Populate pl/sql table';
7077          -------------------------------------------------------------------
7078          IF (l_return_status = TRUE ) THEN
7079 
7080             trans_lines.EXTEND(1);
7081 
7082             trans_lines(j).application_id		:= 200;
7083             trans_lines(j).entity_code			:= 'AP_INVOICES';
7084             trans_lines(j).event_class_code 		:= p_event_class_code;
7085 
7086             trans_lines(j).trx_id 			:= P_Invoice_Header_Rec.invoice_id;
7087             trans_lines(j).trx_level_type 		:= 'LINE';
7088             trans_lines(j).trx_line_id			:= p_inv_line_list(i).line_number;
7089             trans_lines(j).line_level_action		:= l_line_level_action;
7090 	    trans_lines(j).line_class			:= l_line_class;
7091 
7092             trans_lines(j).trx_receipt_date	 	:= l_trx_receipt_date;
7093             trans_lines(j).trx_line_type 	 	:= p_inv_line_list(i).line_type_lookup_code;
7094             trans_lines(j).trx_line_date	 	:= p_invoice_header_rec.invoice_date;
7095             trans_lines(j).trx_line_number              := p_inv_line_list(i).line_number;
7096             trans_lines(j).trx_line_description         := p_inv_line_list(i).description;
7097             trans_lines(j).trx_line_gl_date             := p_inv_line_list(i).accounting_date;
7098             trans_lines(j).account_ccid			:= p_inv_line_list(i).default_dist_ccid;
7099 
7100             trans_lines(j).line_amt                     := p_inv_line_list(i).amount + nvl(p_inv_line_list(i).retained_amount,0);
7101             trans_lines(j).trx_line_quantity            := p_inv_line_list(i).quantity_invoiced;
7102             trans_lines(j).unit_price                   := p_inv_line_list(i).unit_price;
7103             trans_lines(j).uom_code                     := l_uom_code;
7104 
7105             trans_lines(j).trx_business_category        := p_inv_line_list(i).trx_business_category;
7106             trans_lines(j).line_intended_use            := p_inv_line_list(i).primary_intended_use;
7107             trans_lines(j).user_defined_fisc_class      := p_inv_line_list(i).user_defined_fisc_class;
7108             trans_lines(j).product_fisc_classification	:= p_inv_line_list(i).product_fisc_classification;
7109 	    trans_lines(j).assessable_value		:= p_inv_line_list(i).assessable_value;
7110             trans_lines(j).input_tax_classification_code := p_inv_line_list(i).tax_classification_code;
7111 
7112             trans_lines(j).product_id                   := p_inv_line_list(i).inventory_item_id;
7113             trans_lines(j).product_org_id		:= l_product_org_id;
7114             trans_lines(j).product_type			:= p_inv_line_list(i).product_type;
7115             trans_lines(j).product_category		:= p_inv_line_list(i).product_category;
7116             trans_lines(j).product_description		:= p_inv_line_list(i).item_description;
7117             trans_lines(j).fob_point			:= l_fob_point;
7118 
7119             -- AP is not going to pass this parameter.  eTax is aware of this and they will derive this.
7120             -- trans_lines(j).product_code
7121 
7122             -- 7262269
7123             IF p_inv_line_list(i).po_line_location_id IS NOT NULL THEN
7124                l_ship_to_party_id := get_po_ship_to_org_id (p_inv_line_list(i).po_line_location_id);
7125             ELSE
7126                l_ship_to_party_id := p_inv_line_list(i).org_id;
7127             END IF;
7128 
7129             trans_lines(j).ship_to_party_id		:= l_ship_to_party_id; /* Changed the subscript from i to j for bug#7319191 */
7130             -- 7262269
7131 
7132             trans_lines(j).ship_from_party_id		:= P_Invoice_Header_Rec.party_id;
7133 
7134             trans_lines(j).bill_to_party_id		:= p_inv_line_list(i).org_id;
7135             trans_lines(j).bill_from_party_id		:= P_Invoice_Header_Rec.party_id;
7136 
7137             trans_lines(j).ship_from_party_site_id	:= P_Invoice_Header_Rec.party_site_id;
7138             trans_lines(j).bill_from_party_site_id	:= P_Invoice_Header_Rec.party_site_id;
7139 
7140             trans_lines(j).ship_to_location_id		:= nvl(p_inv_line_list(i).ship_to_location_id,l_ship_to_location_id);
7141 	    trans_lines(j).ship_from_location_id	:= l_location_id;
7142             trans_lines(j).bill_to_location_id		:= l_bill_to_location_id;
7143             trans_lines(j).bill_from_location_id        := l_location_id;
7144 
7145             trans_lines(j).ref_doc_application_id	:= l_ref_doc_application_id;
7146             trans_lines(j).ref_doc_entity_code		:= l_ref_doc_entity_code;
7147             trans_lines(j).ref_doc_event_class_code	:= l_ref_doc_event_class_code;
7148             trans_lines(j).ref_doc_trx_id		:= l_ref_doc_trx_id;
7149             trans_lines(j).ref_doc_line_id		:= p_inv_line_list(i).po_line_location_id;
7150             trans_lines(j).ref_doc_line_quantity	:= l_ref_doc_line_quantity;
7151 
7152             -- Not require to populate this values here since this function will
7153             -- not be run for prepayment application
7154             -- trans_lines(j).applied_from_application_id
7155             -- trans_lines(j).applied_from_entity_code
7156             -- trans_lines(j).applied_from_event_class_code
7157             -- trans_lines(j).applied_from_trx_id
7158             -- trans_lines(j).applied_from_line_id
7159 
7160             trans_lines(j).adjusted_doc_application_id	 := l_adj_doc_application_id;
7161             trans_lines(j).adjusted_doc_entity_code	 := l_adj_doc_entity_code;
7162             trans_lines(j).adjusted_doc_event_class_code := l_adj_doc_event_class_code;
7163             trans_lines(j).adjusted_doc_trx_id		 := p_inv_line_list(i).corrected_inv_id;
7164             trans_lines(j).adjusted_doc_line_id		 := p_inv_line_list(i).price_correct_inv_line_num;
7165             trans_lines(j).adjusted_doc_number		 := l_adj_doc_number;
7166             trans_lines(j).adjusted_doc_date		 := l_adj_doc_date;
7167 
7168             trans_lines(j).applied_to_application_id	 := l_applied_to_application_id;
7169             trans_lines(j).applied_to_entity_code	 := l_applied_to_entity_code;
7170             trans_lines(j).applied_to_event_class_code	 := l_applied_to_event_class_code;
7171             trans_lines(j).applied_to_trx_id		 := p_inv_line_list(i).rcv_transaction_id;
7172 
7173             IF p_inv_line_list(i).rcv_transaction_id IS NOT NULL THEN
7174                trans_lines(j).applied_to_trx_line_id	 := p_inv_line_list(i).po_line_location_id;
7175             END IF;
7176 
7177             trans_lines(j).source_application_id   	 := p_inv_line_list(i).source_application_id;
7178             trans_lines(j).source_entity_code      	 := p_inv_line_list(i).source_entity_code;
7179             trans_lines(j).source_event_class_code 	 := p_inv_line_list(i).source_event_class_code;
7180             trans_lines(j).source_trx_id           	 := p_inv_line_list(i).source_trx_id;
7181             trans_lines(j).source_line_id          	 := p_inv_line_list(i).source_line_id;
7182             trans_lines(j).source_trx_level_type   	 := p_inv_line_list(i).source_trx_level_type;
7183 
7184             trans_lines(j).line_amt_includes_tax_flag	 := l_line_amt_includes_tax_flag;
7185             trans_lines(j).ctrl_hdr_tx_appl_flag	 := l_ctrl_hdr_tx_appl_flag;
7186             trans_lines(j).ctrl_total_line_tx_amt	 := p_inv_line_list(i).control_amount;
7187 
7188             -- This function will be called only from the import program.  This
7189             -- flag will be always N.
7190             trans_lines(j).historical_flag := 'N';
7191 -- Debug messages added for 6321366
7192  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7193               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_transaction_lines_gt values ');
7194               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: ' || trans_lines(j).event_class_code);
7195               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '           || trans_lines(j).trx_id);
7196               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_id: '      || trans_lines(j).trx_line_id);
7197               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_level_type: '   || trans_lines(j).trx_level_type);
7198               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_type: '    || trans_lines(j).trx_line_type );
7199               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_level_action: '|| trans_lines(j).line_level_action);
7200               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_class: '       || trans_lines(j).line_class);
7201               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_amt: '         || trans_lines(j).line_amt);
7202               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'unit_price: '       || trans_lines(j).unit_price);
7203               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_amt_includes_tax_flag: '       || trans_lines(j).line_amt_includes_tax_flag);
7204  END IF;
7205 
7206             j := j + 1;
7207 
7208           END IF; -- l_return_status
7209 
7210        ELSE  -- The line is TAX
7211 
7212           -------------------------------------------------------------------
7213           l_debug_info := 'Step 12: Populate pl/sql table if TAX line';
7214           -------------------------------------------------------------------
7215           IF (l_return_status = TRUE ) THEN
7216             tax_lines.EXTEND(1);
7217 
7218             tax_lines(k).summary_tax_line_number := p_inv_line_list(i).line_number;
7219             tax_lines(k).internal_organization_id := P_Invoice_Header_Rec.org_id;
7220             tax_lines(k).application_id := 200;
7221 
7222             tax_lines(k).entity_code := 'AP_INVOICES';
7223             tax_lines(k).event_class_code := p_event_class_code;
7224             tax_lines(k).trx_id := P_Invoice_Header_Rec.invoice_id;
7225 
7226             -- Not used by AP
7227             -- tax_lines(k).hrd_trx_user_key1..6
7228 
7229             tax_lines(k).tax_regime_code := p_inv_line_list(i).tax_regime_code;
7230             tax_lines(k).tax := p_inv_line_list(i).tax;
7231             tax_lines(k).tax_status_code := p_inv_line_list(i).tax_status_code;
7232             tax_lines(k).tax_rate_code := nvl(p_inv_line_list(i).tax_rate_code,
7233                      p_inv_line_list(i).TAX_CLASSIFICATION_CODE); --bug6255826
7234             tax_lines(k).tax_rate := p_inv_line_list(i).tax_rate;
7235             tax_lines(k).tax_amt :=  p_inv_line_list(i).amount;
7236             tax_lines(k).tax_jurisdiction_code := p_inv_line_list(i).tax_jurisdiction_code;
7237             tax_lines(k).tax_amt_included_flag := p_inv_line_list(i).incl_in_taxable_line_flag;
7238             tax_lines(k).tax_rate_id := p_inv_line_list(i).tax_rate_id;
7239             /*6255826  Added following if condition to populate
7240                        tax_line_allocation_flag correctly*/
7241 
7242             --bug 6412397 - changed the index of p_inv_line_list
7243             --              from k to i
7244 
7245             IF  (p_inv_line_list(i).prorate_across_flag = 'Y' AND
7246                      p_inv_line_list(i).line_group_number IS NOT NULL) THEN
7247                 tax_lines(k).tax_line_allocation_flag       := 'Y';
7248             ELSE
7249                 tax_lines(k).tax_line_allocation_flag       := 'N';
7250             END IF;
7251 -- Debug messages added for 6321366
7252  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7253               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_import_tax_lines_gt values ');
7254               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'summary_tax_line_number: ' || tax_lines(k).summary_tax_line_number);
7255               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'application_id: '           || tax_lines(k).application_id);
7256               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: '      || tax_lines(k).event_class_code);
7257 	      FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '   || tax_lines(k).trx_id);
7258               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax: '   || tax_lines(k).tax );
7259               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax_amt: '    || tax_lines(k).tax_amt );
7260               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax_amt_included_flag: '|| tax_lines(k).tax_amt_included_flag);
7261               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax_line_allocation_flag: '|| tax_lines(k).tax_line_allocation_flag);
7262  END IF;
7263 
7264             k := k + 1;
7265 
7266             -------------------------------------------------------------------
7267             l_debug_info := 'Step 13: Populate allocation structure if needed';
7268             -------------------------------------------------------------------
7269 
7270             IF (AP_IMPORT_INVOICES_PKG.g_source IN('ISP', 'ASBN')) THEN
7271               -------------------------------------------------------------------
7272               l_debug_info := 'Step 14: Populate allocation using taxable_flag if '||
7273                               'source is ISP or ASBN';
7274               -------------------------------------------------------------------
7275 
7276               INSERT INTO zx_trx_tax_link_gt (
7277                 application_id,
7278                 entity_code,
7279                 event_class_code,
7280                 trx_id,
7281                 trx_level_type,
7282                 trx_line_id,
7283                 summary_tax_line_number,
7284                 line_amt
7285               ) SELECT
7286                   200,                                           -- application_id
7287                   'AP_INVOICES',                                 -- entity_code
7288                   p_event_class_code,                            -- event_class_code
7289                   P_Invoice_Header_Rec.invoice_id,               -- trx_id
7290                   'LINE',                                        -- trx_level_type
7291                   aili.line_number,                              -- trx_line_id
7292                   p_inv_line_list(i).line_number,                -- summary_tax_line_number
7293                   AP_UTILITIES_PKG.ap_round_currency(
7294                     p_inv_line_list(i).amount*aili.amount/l_prorating_total,
7295                     P_Invoice_Header_Rec.invoice_currency_code)  -- line_amt
7296                  FROM ap_invoice_lines_interface aili
7297                 WHERE aili.invoice_id = P_Invoice_Header_Rec.invoice_id
7298                   AND aili.line_number <> p_inv_line_list(i).line_number
7299                   AND aili.line_type_lookup_code <> 'TAX'
7300                   AND NVL(aili.taxable_flag, 'N') = 'Y';
7301 -- Debug messages added for 6321366
7302  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7303               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Rows instrted in zx_trx_tax_link_gt: '|| sql%rowcount);
7304  END IF;
7305               --------------------------------------------------------------
7306               l_debug_info := 'Step 15: Verify if there is any rounding and '||
7307                               'apply it to max of largest.';
7308               --------------------------------------------------------------
7309               BEGIN
7310                 SELECT SUM(NVL(line_amt,0))
7311                   INTO l_total_prorated
7312                   FROM zx_trx_tax_link_gt
7313                  WHERE trx_id = P_Invoice_Header_Rec.invoice_id
7314                    AND summary_tax_line_number = p_inv_line_list(i).line_number;
7315 
7316                 IF (NVL(p_inv_line_list(i).amount, 0) <> l_total_prorated) THEN
7317                   UPDATE zx_trx_tax_link_gt
7318                      SET line_amt = line_amt + (p_inv_line_list(i).amount - l_total_prorated)
7319                    WHERE trx_id = P_Invoice_Header_Rec.invoice_id
7320                      AND trx_line_id <> p_inv_line_list(i).line_number
7321                      AND trx_line_id =
7322                         (SELECT (MAX(aili.line_number))
7323                            FROM ap_invoice_lines_interface aili
7324                           WHERE aili.invoice_id = P_Invoice_Header_Rec.invoice_id
7325                             AND aili.line_number <> p_inv_line_list(i).line_number
7326                             AND aili.amount <> 0
7327                             AND aili.line_type_lookup_code <> 'TAX'
7328                             AND NVL(aili.taxable_flag, 'N') = 'Y'
7329                             AND ABS(aili.amount) >=
7330                               ( SELECT MAX(ABS(ail2.amount))
7331                                   FROM ap_invoice_lines_interface ail2
7332                                  WHERE ail2.invoice_id = aili.invoice_id
7333                                    AND ail2.line_number <> p_inv_line_list(i).line_number
7334                                    AND ail2.line_number <> aili.line_number
7335                                    AND ail2.line_type_lookup_code <> 'TAX'
7336                                    AND NVL(ail2.taxable_flag, 'N') = 'Y'));
7337 
7338                 END IF;
7339               EXCEPTION
7340                 WHEN OTHERS THEN
7341                   l_return_status := FALSE;
7342               END;
7343 
7344 
7345             ELSE -- source is not ISP or ASBN.  Allocations will be based on the
7346                  -- prorate_across_flag and line_group_number is available
7347               -------------------------------------------------------------------
7348               l_debug_info := 'Step 16: Populate allocation structure if needed';
7349               -------------------------------------------------------------------
7350               IF (p_inv_line_list(i).prorate_across_flag = 'Y' AND
7351                   p_inv_line_list(i).line_group_number IS NOT NULL) THEN
7352 
7353                    --------------------------------------------------------------
7354                    l_debug_info := 'Step 17: Get prorated total';
7355                    --------------------------------------------------------------
7356                    SELECT SUM(NVL(amount, 0))
7357                      INTO l_prorating_total
7358                      FROM ap_invoice_lines_interface
7359                     WHERE invoice_id = P_Invoice_Header_Rec.invoice_id
7360                       AND line_number <> p_inv_line_list(i).line_number
7361 					  AND line_type_lookup_code <> 'TAX'     --Bug6608702**
7362                       AND line_group_number = p_inv_line_list(i).line_group_number;
7363 
7364                    --------------------------------------------------------------
7365                    l_debug_info := 'Step 18: Get Insert in global temp table';
7366                    --------------------------------------------------------------
7367                    IF (l_prorating_total <> 0) THEN
7368                      INSERT INTO zx_trx_tax_link_gt (
7369                        application_id,
7370                        entity_code,
7371                        event_class_code,
7372                        trx_id,
7373                        trx_level_type,
7374                        trx_line_id,
7375                        summary_tax_line_number,
7376                        line_amt
7377                      ) SELECT
7378                          200,                                           -- application_id
7379                          'AP_INVOICES',                                 -- entity_code
7380                          p_event_class_code,                            -- event_class_code
7381                          P_Invoice_Header_Rec.invoice_id,               -- trx_id
7382                          'LINE',                                        -- trx_level_type
7383                          aili.line_number,                              -- trx_line_id
7384                          p_inv_line_list(i).line_number,                -- summary_tax_line_number
7385                          AP_UTILITIES_PKG.ap_round_currency(
7386                            p_inv_line_list(i).amount*aili.amount/l_prorating_total,
7387                            P_Invoice_Header_Rec.invoice_currency_code)  -- line_amt
7388                         FROM ap_invoice_lines_interface aili
7389                        WHERE aili.invoice_id = P_Invoice_Header_Rec.invoice_id
7390                          AND aili.line_number <> p_inv_line_list(i).line_number
7391 						 AND aili.line_type_lookup_code <> 'TAX' --Bug6608702**
7392                          AND aili.line_group_number = p_inv_line_list(i).line_group_number;
7393 -- Debug messages added for 6321366
7394  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7395               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Rows instrted in zx_trx_tax_link_gt: '|| sql%rowcount);
7396  END IF;
7397                    --------------------------------------------------------------
7398                    l_debug_info := 'Step 19: Verify if there is any rounding and '||
7399                                    'apply it to max of largest.';
7400                    --------------------------------------------------------------
7401                    BEGIN
7402                      SELECT SUM(NVL(line_amt,0))
7403                        INTO l_total_prorated
7404                        FROM zx_trx_tax_link_gt
7405                       WHERE trx_id = P_Invoice_Header_Rec.invoice_id
7406                         AND summary_tax_line_number = p_inv_line_list(i).line_number;
7407 
7408                      IF (NVL(p_inv_line_list(i).amount, 0) <> l_total_prorated) THEN
7409                        UPDATE zx_trx_tax_link_gt
7410                           SET line_amt = line_amt + (p_inv_line_list(i).amount - l_total_prorated)
7411                         WHERE trx_id = P_Invoice_Header_Rec.invoice_id
7412                           AND trx_line_id <> p_inv_line_list(i).line_number
7413                           AND trx_line_id =
7414                              (SELECT (MAX(aili.line_number))
7415                                 FROM ap_invoice_lines_interface aili
7416                                WHERE aili.invoice_id = P_Invoice_Header_Rec.invoice_id
7417                                  AND aili.line_number <> p_inv_line_list(i).line_number
7418 								 AND aili.line_type_lookup_code <> 'TAX' --Bug6608702**
7419                                  AND aili.amount <> 0
7420                                  AND aili.line_group_number = p_inv_line_list(i).line_group_number
7421                                  AND ABS(aili.amount) >=
7422                                    ( SELECT  MAX(ABS(ail2.amount))
7423                                        FROM  ap_invoice_lines_interface ail2
7424                                       WHERE  ail2.invoice_id = aili.invoice_id
7425                                         AND  ail2.line_number <> p_inv_line_list(i).line_number
7426                                         AND  ail2.line_number <> aili.line_number
7427 										AND  ail2.line_type_lookup_code <> 'TAX' --Bug6608702**
7428                                         AND  ail2.line_group_number =
7429                                                p_inv_line_list(i).line_group_number));
7430                      END IF;
7431                    EXCEPTION
7432                      WHEN OTHERS THEN
7433                        l_return_status := FALSE;
7434                    END;
7435 
7436                   END IF;  -- l_prorating_total <> 0
7437               END IF;  -- prorate_accross_flag = 'Y' and line_group_number is not null
7438             END IF;  -- End of if for the SOURCE of the invoice
7439           END IF;  -- l_return_status validation for TAX lines
7440         END IF;  -- line type lookup code and it is not tax-only
7441 
7442 
7443       ELSE  -- It is a tax-only invoice
7444 
7445           -------------------------------------------------------------------
7446           l_debug_info := 'Step 4: Get line_level_action for TAX ONLY line number'||
7447                           P_inv_line_list(i).line_number ;
7448           -------------------------------------------------------------------
7449           IF (NVL(P_Invoice_Header_Rec.tax_only_rcv_matched_flag, 'N') = 'Y') THEN
7450             -- In this case  eTax will need to run tax
7451             -- applicability and calculation because it is the matched to other
7452             -- charges case. This flag is populated in the import program based on
7453             -- a select from the lines interface table.
7454             -- In this case the import program will call the calculate service
7455             -- This is transparent to the user since the
7456             -- calc_tax_during_import_flag should be set to N.  Otherwise, the
7457             -- invoice will be rejected.
7458 
7459             l_line_level_action := 'CREATE_TAX_ONLY';
7460           ELSE
7461             -- Invoice is tax-only, and there is no need to run applicability.
7462             -- In this case the user provides all the tax information for the line
7463             -- to be imported.  However, the additional taxable related info
7464             -- that eTax need to store will be passed to eTax using a pseudo line
7465             -- in the zx_transaction_lines_gt table.
7466 
7467             l_line_level_action := 'LINE_INFO_TAX_ONLY';
7468           END IF;
7469 
7470           -------------------------------------------------------------------
7471           l_debug_info := 'Step 5: Get Additional PO matched  info ';
7472           -------------------------------------------------------------------
7473 
7474           IF ( P_Inv_Line_List(i).po_line_location_id IS NOT NULL) THEN
7475               -- this assigned is required since the p_po_line_location_id
7476               -- parameter is IN/OUT.  However, in this case it will not be
7477               -- modified because the po_distribution_id is not provided
7478               l_po_line_location_id := P_Inv_Line_List(i).po_line_location_id;
7479 
7480             IF NOT (AP_ETAX_UTILITY_PKG.Get_PO_Info(
7481                P_Po_Line_Location_Id         => l_po_line_location_id,
7482                P_PO_Distribution_Id          => null,
7483                P_Application_Id              => l_ref_doc_application_id,
7484                P_Entity_code                 => l_ref_doc_entity_code,
7485                P_Event_Class_Code            => l_ref_doc_event_class_code,
7486                P_PO_Quantity                 => l_ref_doc_line_quantity,
7487                P_Product_Org_Id              => l_product_org_id,
7488                P_Po_Header_Id                => l_ref_doc_trx_id,
7489                P_Po_Header_curr_conv_rate    => l_po_header_curr_conv_rate,
7490                P_Uom_Code                    => l_uom_code,
7491                P_Dist_Qty                    => l_dummy,
7492                P_Ship_Price                  => l_dummy,
7493                P_Error_Code                  => P_error_code,
7494                P_Calling_Sequence            => l_curr_calling_sequence)) THEN
7495 
7496               l_return_status := FALSE;
7497             END IF;
7498 
7499             l_ref_doc_trx_level_type := 'SHIPMENT';
7500 
7501           ELSE
7502             l_ref_doc_application_id     := Null;
7503             l_ref_doc_entity_code        := Null;
7504             l_ref_doc_event_class_code   := Null;
7505             l_ref_doc_line_quantity      := Null;
7506             l_product_org_id             := Null;
7507             l_ref_doc_trx_id             := Null;
7508             l_ref_doc_trx_level_type     := Null;
7509           END IF;
7510 
7511           -------------------------------------------------------------------
7512           l_debug_info := 'Step 6: Get Additional receipt matched info ';
7513           -------------------------------------------------------------------
7514           IF ( l_return_status = TRUE AND
7515                P_Inv_Line_List(i).rcv_transaction_id IS NOT NULL) THEN
7516             IF NOT (AP_ETAX_UTILITY_PKG.Get_Receipt_Info(
7517                P_Rcv_Transaction_Id          => P_Inv_Line_List(i).rcv_transaction_id,
7518                P_Application_Id              => l_applied_to_application_id,
7519                P_Entity_code                 => l_applied_to_entity_code,
7520                P_Event_Class_Code            => l_applied_to_event_class_code,
7521                P_Transaction_Date            => l_trx_receipt_date,
7522                P_Error_Code                  => P_error_code,
7523                P_Calling_Sequence            => l_curr_calling_sequence)) THEN
7524 
7525                l_return_status := FALSE;
7526             END IF;
7527           ELSE
7528             l_applied_to_application_id   := Null;
7529             l_applied_to_entity_code      := Null;
7530             l_applied_to_event_class_code := Null;
7531             l_trx_receipt_date            := Null;
7532           END IF;
7533 
7534           -------------------------------------------------------------------
7535           l_debug_info := 'Step 8: Get Additional Correction Invoice Info ';
7536           -------------------------------------------------------------------
7537 
7538           IF ( l_return_status = TRUE AND
7539                P_Inv_Line_List(i).corrected_inv_id IS NOT NULL AND
7540                P_Inv_Line_list(i).price_correct_inv_line_num IS NOT NULL) THEN
7541             IF NOT (AP_ETAX_UTILITY_PKG.Get_Corrected_Invoice_Info(
7542                P_Corrected_Invoice_Id        => P_Inv_Line_List(i).corrected_inv_id,
7543                P_Corrected_Line_Number       => P_Inv_Line_List(i).price_correct_inv_line_num,
7544                P_Application_Id              => l_adj_doc_application_id,
7545                P_Entity_code                 => l_adj_doc_entity_code,
7546                P_Event_Class_Code            => l_adj_doc_event_class_code,
7547                P_Invoice_Number              => l_adj_doc_number,
7548                P_Invoice_Date                => l_adj_doc_date,
7549                P_Error_Code                  => P_error_code,
7550                P_Calling_Sequence            => l_curr_calling_sequence)) THEN
7551 
7552               l_return_status := FALSE;
7553             END IF;
7554           ELSE
7555             l_adj_doc_application_id   := Null;
7556             l_adj_doc_entity_code      := Null;
7557             l_adj_doc_event_class_code := Null;
7558             l_adj_doc_number           := Null;
7559             l_adj_doc_date             := Null;
7560           END IF;
7561 
7562           -------------------------------------------------------------------
7563           l_debug_info := 'Step 9: Get line_amt_includes_tax_flag';
7564           -------------------------------------------------------------------
7565 
7566           IF (P_Inv_Line_List(i).po_line_location_id IS NOT NULL) THEN
7567             -- NONE
7568             l_line_amt_includes_tax_flag := 'N';
7569 
7570           ELSE
7571            IF (p_calling_mode = 'CALCULATE QUOTE')
7572               OR
7573               (p_invoice_header_rec.invoice_type_lookup_code = 'EXPENSE REPORT'
7574                and nvl(l_inv_line_list(i).line_type_lookup_code, 'N') <> 'PREPAY') THEN
7575              -- ALL
7576              l_line_amt_includes_tax_flag := 'A';
7577 
7578            ELSE
7579              -- STANDARD
7580              IF p_calling_mode = 'VALIDATE IMPORT' THEN /* if condition added for 6010950 -
7581                  For tax only invoices we need to pass it as 'N' */
7582                 l_line_amt_includes_tax_flag := 'N';
7583             ELSE
7584                 l_line_amt_includes_tax_flag := 'S';
7585             END IF;
7586 
7587            END IF;
7588           END IF;
7589 
7590           -------------------------------------------------------------------
7591           l_debug_info := 'Step 10: Get ctrl_hdr_tx_appl_flag';
7592           -------------------------------------------------------------------
7593           IF P_Invoice_Header_Rec.control_amount IS NOT NULL THEN
7594             l_ctrl_hdr_tx_appl_flag := 'Y';
7595           ELSE
7596             l_ctrl_hdr_tx_appl_flag := 'N';
7597           END IF;
7598 
7599 
7600           -------------------------------------------------------------------
7601           l_debug_info := 'Step 10z: Get control_amount line level';
7602           -------------------------------------------------------------------
7603           IF (NVL(P_Invoice_Header_Rec.tax_only_rcv_matched_flag, 'N') = 'Y') THEN
7604             l_line_control_amount :=
7605               NVL(p_inv_line_list(i).control_amount, p_inv_line_list(i).amount);
7606 
7607           ELSE
7608             l_line_control_amount := p_inv_line_list(i).control_amount;
7609           END IF;
7610 
7611           -------------------------------------------------------------------
7612           l_debug_info := 'Step 11: Populate pl/sql table';
7613           -------------------------------------------------------------------
7614           IF (l_return_status = TRUE ) THEN
7615             trans_lines.EXTEND(1);
7616             trans_lines(j).line_amt_includes_tax_flag           := l_line_amt_includes_tax_flag ; ---for bug 6010950
7617             trans_lines(j).application_id 			:= 200;
7618             trans_lines(j).entity_code 				:= 'AP_INVOICES';
7619             trans_lines(j).event_class_code 			:= p_event_class_code;
7620             trans_lines(j).trx_id 				:= P_Invoice_Header_Rec.invoice_id;
7621             trans_lines(j).trx_level_type 			:= 'LINE';
7622             trans_lines(j).trx_line_id 				:= p_inv_line_list(i).line_number;
7623             trans_lines(j).line_level_action 			:= l_line_level_action;
7624 
7625             trans_lines(j).trx_receipt_date 			:= l_trx_receipt_date;
7626             trans_lines(j).trx_line_type 			:= 'ITEM';
7627 --bug6255826
7628             trans_lines(j).trx_line_date 			:= P_Invoice_Header_Rec.invoice_date;
7629             trans_lines(j).trx_business_category 		:= p_inv_line_list(i).trx_business_category;
7630             trans_lines(j).line_intended_use 			:= p_inv_line_list(i).primary_intended_use;
7631             trans_lines(j).user_defined_fisc_class 		:= p_inv_line_list(i).user_defined_fisc_class;
7632             trans_lines(j).line_amt 				:= p_inv_line_list(i).amount;
7633             trans_lines(j).trx_line_quantity 			:= p_inv_line_list(i).quantity_invoiced;
7634             trans_lines(j).unit_price 				:= p_inv_line_list(i).unit_price;
7635 
7636             trans_lines(j).product_id 				:= p_inv_line_list(i).inventory_item_id;
7637             trans_lines(j).product_fisc_classification 		:= p_inv_line_list(i).product_fisc_classification;
7638             trans_lines(j).product_org_id 			:= l_product_org_id;
7639             trans_lines(j).uom_code 				:= l_uom_code;
7640             trans_lines(j).product_type 			:= p_inv_line_list(i).product_type;
7641             trans_lines(j).product_category 			:= p_inv_line_list(i).product_category;
7642             trans_lines(j).fob_point 				:= l_fob_point;
7643 
7644             -- 7262269
7645             IF p_inv_line_list(i).po_line_location_id IS NOT NULL THEN
7646                l_ship_to_party_id := get_po_ship_to_org_id (p_inv_line_list(i).po_line_location_id);
7647             ELSE
7648                l_ship_to_party_id := p_inv_line_list(i).org_id;
7649             END IF;
7650 
7651             trans_lines(j).ship_to_party_id		:= l_ship_to_party_id;  /* Changed the subscript from i to j for bug#7319191 */
7652             -- 7262269
7653             trans_lines(j).ship_from_party_id 			:= P_Invoice_Header_Rec.party_id;
7654             trans_lines(j).bill_to_party_id 			:= p_inv_line_list(i).org_id;
7655             trans_lines(j).bill_from_party_id 			:= P_Invoice_Header_Rec.party_id;
7656             trans_lines(j).ship_from_party_site_id 		:= P_Invoice_Header_Rec.party_site_id;
7657             trans_lines(j).bill_from_party_site_id 		:= P_Invoice_Header_Rec.party_site_id;
7658 
7659             trans_lines(j).ship_to_location_id 			:= p_inv_line_list(i).ship_to_location_id;
7660 	    trans_lines(j).ship_from_location_id 		:= l_location_id;
7661             trans_lines(j).bill_to_location_id 			:= l_bill_to_location_id;
7662             trans_lines(j).bill_from_location_id 		:= l_location_id;
7663 
7664             trans_lines(j).account_ccid 			:= p_inv_line_list(i).default_dist_ccid;
7665 
7666             trans_lines(j).ref_doc_application_id 		:= l_ref_doc_application_id;
7667             trans_lines(j).ref_doc_entity_code 			:= l_ref_doc_entity_code;
7668             trans_lines(j).ref_doc_event_class_code 		:= l_ref_doc_event_class_code;
7669             trans_lines(j).ref_doc_trx_id 			:= l_ref_doc_trx_id;
7670             trans_lines(j).ref_doc_line_id 			:= p_inv_line_list(i).po_line_location_id;
7671             trans_lines(j).ref_doc_line_quantity 		:= l_ref_doc_line_quantity;
7672 
7673             trans_lines(j).adjusted_doc_application_id 		:= l_adj_doc_application_id;
7674             trans_lines(j).adjusted_doc_entity_code 		:= l_adj_doc_entity_code;
7675             trans_lines(j).adjusted_doc_event_class_code 	:= l_adj_doc_event_class_code;
7676             trans_lines(j).adjusted_doc_trx_id 			:= p_inv_line_list(i).corrected_inv_id;
7677             trans_lines(j).adjusted_doc_line_id 		:= p_inv_line_list(i).price_correct_inv_line_num;
7678             trans_lines(j).adjusted_doc_number 			:= l_adj_doc_number;
7679             trans_lines(j).adjusted_doc_date 			:= l_adj_doc_date;
7680 
7681             trans_lines(j).applied_to_application_id 		:= l_applied_to_application_id;
7682             trans_lines(j).applied_to_entity_code 		:= l_applied_to_entity_code;
7683             trans_lines(j).applied_to_event_class_code 		:= l_applied_to_event_class_code;
7684             trans_lines(j).applied_to_trx_id 			:= p_inv_line_list(i).rcv_transaction_id;
7685 
7686             IF p_inv_line_list(i).rcv_transaction_id IS NOT NULL THEN
7687                trans_lines(j).applied_to_trx_line_id 		:= p_inv_line_list(i).po_line_location_id;
7688             END IF;
7689 
7690             trans_lines(j).trx_line_number 			:= p_inv_line_list(i).line_number;
7691             trans_lines(j).trx_line_description 		:= p_inv_line_list(i).description;
7692             trans_lines(j).product_description 			:= p_inv_line_list(i).item_description;
7693             trans_lines(j).trx_line_gl_date 			:= p_inv_line_list(i).accounting_date;
7694 
7695             trans_lines(j).assessable_value 			:= p_inv_line_list(i).assessable_value;
7696 
7697             trans_lines(j).ctrl_hdr_tx_appl_flag 		:= l_ctrl_hdr_tx_appl_flag;
7698             -- The default for the control amount amount is included only in the
7699             -- case the tax line is tax only and it is RCV matched.
7700             trans_lines(j).ctrl_total_line_tx_amt		:= l_line_control_amount;
7701 
7702             trans_lines(j).source_application_id   		:= p_inv_line_list(i).source_application_id;
7703             trans_lines(j).source_entity_code	   		:= p_inv_line_list(i).source_entity_code;
7704             trans_lines(j).source_event_class_code 		:= p_inv_line_list(i).source_event_class_code;
7705             trans_lines(j).source_trx_id	   		:= p_inv_line_list(i).source_trx_id;
7706             trans_lines(j).source_line_id	   		:= p_inv_line_list(i).source_line_id;
7707             trans_lines(j).source_trx_level_type   		:= p_inv_line_list(i).source_trx_level_type;
7708 
7709             trans_lines(j).input_tax_classification_code 	:= p_inv_line_list(i).tax_classification_code;
7710 --debug log messages added for 6321366
7711 IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7712               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_transaction_lines_gt values ');
7713               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: ' || trans_lines(j).event_class_code);
7714               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '           || trans_lines(j).trx_id);
7715               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_id: '      || trans_lines(j).trx_line_id);
7716               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_level_type: '   || trans_lines(j).trx_level_type);
7717               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_type: '    || trans_lines(j).trx_line_type );
7718               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_level_action: '|| trans_lines(j).line_level_action);
7719               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_amt_includes_tax_flag: '       || trans_lines(j).line_amt_includes_tax_flag );
7720 END IF;
7721 
7722             -- Increase the index for the next line to be included in the pl/sql
7723             -- table
7724             j := j + 1;
7725 
7726           END IF; -- l_return_status
7727 
7728         IF (NVL(P_Invoice_Header_Rec.tax_only_rcv_matched_flag, 'N') = 'N') THEN
7729          -------------------------------------------------------------------
7730           l_debug_info := 'Step 12: Populate pl/sql table if TAX line';
7731           -------------------------------------------------------------------
7732           IF (l_return_status = TRUE ) THEN
7733             tax_lines.EXTEND(1);
7734             link_lines.EXTEND(1);
7735 
7736 	    tax_lines(k).tax_line_allocation_flag             := 'Y';  /* for bug 6010950 as for
7737             tax only line we need to pass it as 'Y'. */
7738             tax_lines(k).summary_tax_line_number := p_inv_line_list(i).line_number;
7739             tax_lines(k).internal_organization_id := P_Invoice_Header_Rec.org_id;
7740             tax_lines(k).application_id := 200;
7741             tax_lines(k).entity_code := 'AP_INVOICES';
7742             tax_lines(k).event_class_code := p_event_class_code;
7743             tax_lines(k).trx_id := P_Invoice_Header_Rec.invoice_id;
7744 
7745             -- Not used by AP
7746             -- tax_lines(k).hrd_trx_user_key1..6
7747 
7748             tax_lines(k).tax_regime_code := p_inv_line_list(i).tax_regime_code;
7749             tax_lines(k).tax := p_inv_line_list(i).tax;
7750             tax_lines(k).tax_status_code := p_inv_line_list(i).tax_status_code;
7751             tax_lines(k).tax_rate_code := nvl(p_inv_line_list(i).tax_rate_code,
7752                       p_inv_line_list(i).tax_classification_code); --bug6255826
7753 --            tax_lines(k).tax_rate_code := p_inv_line_list(i).tax_rate_code;
7754 --            commented for 6255826
7755             tax_lines(k).tax_rate := p_inv_line_list(i).tax_rate;
7756             --- bug 6429993 - we need to populate tax_jurisdiction_code also.
7757             tax_lines(k).tax_jurisdiction_code :=p_inv_line_list(i).tax_jurisdiction_code;
7758             tax_lines(k).tax_amt :=  p_inv_line_list(i).amount;
7759 
7760             --------------------------------------------------------------
7761             l_debug_info := 'Step 13: Populate link structure';
7762             --------------------------------------------------------------
7763             link_lines(k).application_id := 200;
7764             link_lines(k).entity_code := 'AP_INVOICES';
7765             link_lines(k).event_class_code := p_event_class_code;
7766             link_lines(k).trx_id := P_Invoice_Header_Rec.invoice_id;
7767             link_lines(k).trx_level_type := 'LINE';
7768             link_lines(k).trx_line_id := p_inv_line_list(i).line_number;
7769             link_lines(k).summary_tax_line_number := p_inv_line_list(i).line_number;
7770             link_lines(k).line_amt := p_inv_line_list(i).amount;
7771 
7772 -- Debug messages added for 6321366
7773  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7774               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_import_tax_lines_gt values ');
7775               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'summary_tax_line_number: ' || tax_lines(k).summary_tax_line_number);
7776               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'application_id: '           || tax_lines(k).application_id);
7777               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: '      || tax_lines(k).event_class_code);
7778               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '   || tax_lines(k).trx_id);
7779               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax: '   || tax_lines(k).tax );
7780               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax_amt: '    || tax_lines(k).tax_amt );
7781               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax_line_allocation_flag: '|| tax_lines(k).tax_line_allocation_flag);
7782  END IF;
7783 -- Debug messages added for 6321366
7784 IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7785 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_trx_tax_link_gt values ');
7786               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'application_id: '           || link_lines(k).application_id);
7787               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'entity_code: ' || link_lines(k).entity_code);
7788               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: ' || link_lines(k).event_class_code);
7789               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '           || link_lines(k).trx_id);
7790               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_level_type: '           || link_lines(k).trx_level_type);
7791               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_id: '           || link_lines(k).trx_line_id);
7792               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_amt: '    || link_lines(k).line_amt );
7793               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'summary_tax_line_number: ' || link_lines(k).summary_tax_line_number);
7794 END IF;
7795             k := k + 1;
7796 
7797           END IF;  -- l_return_status validation for TAX lines
7798         END IF;
7799       END IF;
7800     END LOOP;   -- end of loop in the p_invoice_lines_tab
7801 
7802     ELSE
7803       -- There are no TRX or TAX lines to validate.  Return without error
7804       RETURN TRUE;
7805 
7806     END IF;  -- There are TRX or TAX lines in the invoice to be imported
7807 
7808     -------------------------------------------------------------------
7809     l_debug_info := 'Step 12: Bulk Insert into global temp tables';
7810     -------------------------------------------------------------------
7811     IF ( l_return_status = TRUE ) THEN
7812 
7813       DELETE FROM zx_transaction_lines_gt
7814        WHERE application_id   = AP_ETAX_PKG.AP_APPLICATION_ID
7815          AND entity_code      = AP_ETAX_PKG.AP_ENTITY_CODE
7816          AND event_class_code IN (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
7817                                   AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
7818                                   AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
7819          AND trx_id = p_invoice_header_rec.invoice_id;
7820 
7821       IF (trans_lines.COUNT > 0) THEN
7822         FORALL m IN trans_lines.FIRST..trans_lines.LAST
7823           INSERT INTO zx_transaction_lines_gt
7824           VALUES trans_lines(m);
7825       END IF;
7826 
7827       IF (tax_lines.COUNT > 0) THEN
7828         FORALL m IN tax_lines.FIRST..tax_lines.LAST
7829           INSERT INTO zx_import_tax_lines_gt
7830           VALUES tax_lines(m);
7831       END IF;
7832 
7833       -- This bulk insert will only be effective when the invoice is tax_only
7834       -- and there are tax lines with the tax info to be imported
7835       IF (link_lines.COUNT > 0) THEN
7836         FORALL m IN link_lines.FIRST..link_lines.LAST
7837           INSERT INTO zx_trx_tax_link_gt
7838           VALUES link_lines(m);
7839       END IF;
7840 
7841     END IF;
7842 
7843     RETURN l_return_status;
7844 
7845   EXCEPTION
7846     WHEN OTHERS THEN
7847       IF (SQLCODE <> -20001) THEN
7848         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
7849         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
7850         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
7851         FND_MESSAGE.SET_TOKEN('PARAMETERS',
7852           ' P_Invoice_Header_Rec = '||P_Invoice_Header_Rec.Invoice_Id||
7853           ' P_Calling_Mode ='||P_Calling_Mode||
7854           ' P_Error_Code = '||P_Error_Code||
7855           ' P_Calling_Sequence = '||P_Calling_Sequence);
7856         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
7857       END IF;
7858 
7859       APP_EXCEPTION.RAISE_EXCEPTION;
7860 
7861   END Populate_Lines_Import_GT;
7862 
7863 /*=============================================================================
7864  |  FUNCTION - Populate_Tax_Lines_GT()
7865  |
7866  |  DESCRIPTION
7867  |      This function will get additional information required to populate the
7868  |      ZX_TRANSACTION_LINES_GT, and  ZX_IMPORT_TAX_LINES_GT.
7869  |      There is no need to populate ZX_TRX_TAX_LINK_GT since any tax line
7870  |      manually created is assume to be allocated to all the ITEM lines in the
7871  |      invoice.
7872  |      This function returns TRUE if the population of the temp table goes
7873  |      through successfully.  Otherwise, FALSE.
7874  |
7875  |  PARAMETERS
7876  |      P_Invoice_Header_Rec - record with invoice header information
7877  |      P_Calling_Mode - calling mode. it is used to
7878  |      P_Event_Class_Code - Event class code for document
7879  |      P_Tax_only_Flag - determine if the invoice is tax only
7880  |      P_Inv_Rcv_Matched - determine if the invoice has any line matched to a
7881  |                          receipt
7882  |      P_error_code - Error code to be returned
7883  |      P_calling_sequence -  Calling sequence
7884  |
7885  |  MODIFICATION HISTORY
7886  |    DATE          Author         Action
7887  |    06-FEB-2004   SYIDNER        Created
7888  |
7889  *============================================================================*/
7890   FUNCTION Populate_Tax_Lines_GT(
7891              P_Invoice_Header_Rec      IN ap_invoices_all%ROWTYPE,
7892              P_Calling_Mode            IN VARCHAR2,
7893              P_Event_Class_Code        IN VARCHAR2,
7894              P_Tax_only_Flag           IN VARCHAR2,
7895              P_Inv_Rcv_Matched         IN OUT NOCOPY VARCHAR2,
7896              P_Error_Code              OUT NOCOPY VARCHAR2,
7897              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
7898   IS
7899 
7900     l_debug_info                 VARCHAR2(240);
7901     l_curr_calling_sequence      VARCHAR2(4000);
7902 
7903     TYPE Trx_Lines_Tab_Type IS TABLE OF zx_transaction_lines_gt%ROWTYPE;
7904     TYPE Tax_Lines_Tab_Type IS TABLE OF zx_import_tax_lines_gt%ROWTYPE;
7905     TYPE Link_Lines_Tab_Type IS TABLE OF zx_trx_tax_link_gt%ROWTYPE;
7906 
7907     trans_lines		Trx_Lines_Tab_Type := Trx_Lines_Tab_Type();
7908     tax_lines		Tax_Lines_Tab_Type := Tax_Lines_Tab_Type();
7909     link_lines		Link_Lines_Tab_Type := Link_Lines_Tab_Type();
7910 
7911     l_ctrl_hdr_tx_appl_flag		zx_transaction_lines_gt.ctrl_hdr_tx_appl_flag%TYPE;
7912     l_line_control_amount		zx_transaction_lines_gt.ctrl_total_line_tx_amt%TYPE;
7913     l_line_level_action			zx_transaction_lines_gt.line_level_action%TYPE;
7914     l_line_amt_includes_tax_flag	zx_transaction_lines_gt.line_amt_includes_tax_flag%TYPE;
7915     l_product_org_id			zx_transaction_lines_gt.product_org_id%TYPE;
7916 
7917     -- Purchase Order
7918     l_ref_doc_application_id		zx_transaction_lines_gt.ref_doc_application_id%TYPE;
7919     l_ref_doc_entity_code		zx_transaction_lines_gt.ref_doc_entity_code%TYPE;
7920     l_ref_doc_event_class_code		zx_transaction_lines_gt.ref_doc_event_class_code%TYPE;
7921     l_ref_doc_trx_level_type		zx_transaction_lines_gt.ref_doc_trx_level_type%TYPE;
7922     l_ref_doc_line_quantity		zx_transaction_lines_gt.ref_doc_line_quantity%TYPE;
7923     l_po_header_curr_conv_rate		po_headers_all.rate%TYPE;
7924 
7925     -- Receipt Matched
7926     l_applied_to_application_id		zx_transaction_lines_gt.applied_to_application_id%TYPE;
7927     l_applied_to_entity_code		zx_transaction_lines_gt.applied_to_entity_code%TYPE;
7928     l_applied_to_event_class_code	zx_transaction_lines_gt.applied_to_event_class_code%TYPE;
7929     l_trx_receipt_date			zx_transaction_lines_gt.trx_receipt_date%TYPE;
7930     l_ref_doc_trx_id			zx_transaction_lines_gt.ref_doc_trx_id%TYPE;
7931     l_uom_code				mtl_units_of_measure.uom_code%TYPE;
7932     l_dummy				number;
7933 
7934     -- Corrections
7935     l_adj_doc_application_id		zx_transaction_lines_gt.adjusted_doc_application_id%TYPE;
7936     l_adj_doc_entity_code		zx_transaction_lines_gt.adjusted_doc_entity_code%TYPE;
7937     l_adj_doc_event_class_code		zx_transaction_lines_gt.adjusted_doc_event_class_code%TYPE;
7938     l_adj_doc_number			zx_transaction_lines_gt.adjusted_doc_number%TYPE;
7939     l_adj_doc_date			zx_transaction_lines_gt.adjusted_doc_date%TYPE;
7940 
7941     l_fob_point                  	po_vendor_sites_all.fob_lookup_code%TYPE;
7942     l_location_id                       zx_transaction_lines_gt.ship_from_location_id%type;
7943     l_bill_to_location_id               zx_transaction_lines_gt.bill_to_location_id%TYPE;
7944     l_po_line_location_id		ap_invoice_lines_interface.po_line_location_id%TYPE;
7945     l_ship_to_party_id          po_line_locations_all.ship_to_organization_id%type; -- 7262269
7946 
7947     k                           INT := 1;
7948 
7949     l_return_status	BOOLEAN := TRUE;
7950     l_api_name		CONSTANT VARCHAR2(100) := 'Populate_Tax_Lines_GT';
7951     l_prorating_total   number;    ---for bug 6064593
7952     l_total_prorated    number;    ---for bug 6064593
7953   BEGIN
7954 
7955     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Populate_Tax_Lines_GT<-'||
7956                                P_calling_sequence;
7957 
7958 
7959     l_fob_point           := AP_ETAX_SERVICES_PKG.g_site_attributes
7960                                         (p_invoice_header_rec.vendor_site_id).fob_lookup_code;
7961     l_location_id         := AP_ETAX_SERVICES_PKG.g_site_attributes
7962                                         (p_invoice_header_rec.vendor_site_id).location_id;
7963     l_bill_to_location_id := AP_ETAX_SERVICES_PKG.g_org_attributes
7964                                         (p_invoice_header_rec.org_id).bill_to_location_id;
7965 
7966     IF (P_Tax_only_Flag = 'N') THEN
7967 
7968       -------------------------------------------------------------------
7969       l_debug_info := 'Step 1: Populate pl/sql table TAX line will be '||
7970                       'allocated to all the ITEM lines in the invoice';
7971       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7972           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7973       END IF;
7974       -------------------------------------------------------------------
7975       IF ( l_inv_tax_list.COUNT > 0) THEN
7976         tax_lines.EXTEND(l_inv_tax_list.COUNT);
7977 
7978         FOR i IN l_inv_tax_list.FIRST..l_inv_tax_list.LAST LOOP
7979 
7980             tax_lines(i).summary_tax_line_number	:= l_inv_tax_list(i).line_number;
7981             tax_lines(i).internal_organization_id	:= l_inv_tax_list(i).org_id;
7982             tax_lines(i).application_id 		:= 200;
7983             tax_lines(i).entity_code 			:= 'AP_INVOICES';
7984             tax_lines(i).event_class_code 		:= p_event_class_code;
7985             tax_lines(i).trx_id 			:= l_inv_tax_list(i).invoice_id;
7986 
7987             tax_lines(i).tax_regime_code		:= l_inv_tax_list(i).tax_regime_code;
7988             tax_lines(i).tax				:= l_inv_tax_list(i).tax;
7989             tax_lines(i).tax_status_code		:= l_inv_tax_list(i).tax_status_code;
7990             tax_lines(i).tax_rate_code			:= l_inv_tax_list(i).tax_rate_code;
7991             tax_lines(i).tax_rate			:= l_inv_tax_list(i).tax_rate;
7992             tax_lines(i).tax_amt			:= l_inv_tax_list(i).amount;
7993             tax_lines(i).tax_jurisdiction_code		:= l_inv_tax_list(i).tax_jurisdiction_code;
7994             tax_lines(i).tax_rate_id                    := l_inv_tax_list(i).tax_rate_id;
7995 
7996             tax_lines(i).tax_amt_included_flag		:= 'N';
7997             --Start For bug 6064593 - if there is allocation information provided
7998             --for the manual tax line then We need to populate zx_trx_tax_link_gt.
7999             --ie if line_group_number is provided on the tax line and
8000             --ITEM line then we need to populate the zx_trx_tax_link_gt
8001             --with the allocation info.And we need to pass tax_line_allocation_flag
8002             --as 'Y' in zx_import_tax_lines_gt for the tax line.
8003 
8004           IF  (l_inv_tax_list(i).prorate_across_all_items = 'Y'AND
8005                 l_inv_tax_list(i).line_group_number IS NOT NULL) THEN
8006                 tax_lines(i).tax_line_allocation_flag       := 'Y';
8007           ELSE
8008                 tax_lines(i).tax_line_allocation_flag       := 'N';
8009           END IF;
8010 -- Debug messages added for 6321366
8011  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8012 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_import_tax_lines_gt values ');
8013               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'summary_tax_line_number: ' || tax_lines(i).summary_tax_line_number);
8014               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'application_id: '           || tax_lines(i).application_id);
8015               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: '      || tax_lines(i).event_class_code);
8016               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '   || tax_lines(i).trx_id);
8017               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax: '   || tax_lines(i).tax );
8018               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax_amt: '    || tax_lines(i).tax_amt );
8019               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax_amt_included_flag: '|| tax_lines(i).tax_amt_included_flag);
8020               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax_line_allocation_flag: '|| tax_lines(i).tax_line_allocation_flag);
8021  END IF;
8022 
8023                -------------------------------------------------------------------
8024                l_debug_info := 'Step 13: Populate allocation structure if needed';
8025                -------------------------------------------------------------------
8026 
8027                   IF (l_inv_tax_list(i).prorate_across_all_items = 'Y' AND
8028                      l_inv_tax_list(i).line_group_number IS NOT NULL) THEN
8029 
8030                       --------------------------------------------------------------
8031                       l_debug_info := 'Step 17: Get prorated total';
8032                       --------------------------------------------------------------
8033 
8034 					  SELECT SUM(NVL(amount, 0))
8035                         INTO l_prorating_total
8036                         FROM ap_invoice_lines
8037                        WHERE invoice_id = l_inv_tax_list(i).invoice_id
8038                          AND line_number <> l_inv_tax_list(i).line_number
8039 						 AND line_type_lookup_code <> 'TAX'   --Bug6608702
8040                          AND line_group_number = l_inv_tax_list(i).line_group_number;
8041 
8042 					  /************************************************************
8043 					  Bug6608702- Ebtax expects the line amount in
8044 					  zx_trx_tax_link_gt for each TAX line to be the sum of line
8045 					  amounts of all ITEM lines under same line_group_number.
8046 					  Here All the things worked fine till there was only one tax
8047 					  line linked to ITEM line. When a ITEM line is linked to
8048 					  multiple TAX lines through line_group_number then existing
8049                       logic did sum of all the lines for the same line_group_no
8050                       except current TAX line. So other TAX lines within the
8051                       same line_group_number are also considered. To avoid this
8052                       we added another WHERE clause line_type_lookup_code <> 'TAX'
8053                       This would avoid TAX line amount to be considered in the
8054                       query.
8055    		              ************************************************************/
8056 
8057 					  --------------------------------------------------------------
8058                       l_debug_info := 'Step 18: Get Insert in global temp table';
8059                       --------------------------------------------------------------
8060 
8061                       IF (l_prorating_total <> 0) THEN
8062                         INSERT INTO zx_trx_tax_link_gt (
8063                           application_id,
8064                           entity_code,
8065                           event_class_code,
8066                           trx_id,
8067                           trx_level_type,
8068                           trx_line_id,
8069                           summary_tax_line_number,
8070                           line_amt
8071                         ) SELECT
8072                             200,                                           -- application_id
8073                             'AP_INVOICES',                                 -- entity_code
8074                             p_event_class_code,                            -- event_class_code
8075                             l_inv_tax_list(i).invoice_id,               -- trx_id
8076                             'LINE',                                        -- trx_level_type
8077                             ail.line_number,                              -- trx_line_id
8078                             l_inv_tax_list(i).line_number,                -- summary_tax_line_number
8079                             AP_UTILITIES_PKG.ap_round_currency(
8080                               l_inv_tax_list(i).amount*ail.amount/l_prorating_total,
8081                               l_inv_header_rec2.invoice_currency_code)  -- line_amt
8082                            FROM ap_invoice_lines ail
8083                           WHERE ail.invoice_id = l_inv_tax_list(i).invoice_id
8084                             AND ail.line_number <> l_inv_tax_list(i).line_number
8085 							AND ail.line_type_lookup_code <> 'TAX'   --Bug6608702
8086                             AND ail.line_group_number = l_inv_tax_list(i).line_group_number;
8087 -- Debug messages added for 6321366
8088  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8089               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Rows instrted in zx_trx_tax_link_gt: '|| sql%rowcount);
8090  END IF;
8091                       --------------------------------------------------------------
8092                       l_debug_info := 'Step 19: Verify if there is any rounding and '||
8093                                       'apply it to max of largest.';
8094                       --------------------------------------------------------------
8095                       BEGIN
8096                         SELECT SUM(NVL(line_amt,0))
8097                           INTO l_total_prorated
8098                           FROM zx_trx_tax_link_gt
8099                          WHERE trx_id = l_inv_tax_list(i).invoice_id
8100                            AND summary_tax_line_number = l_inv_tax_list(i).line_number;
8101 
8102                         IF (NVL(l_inv_tax_list(i).amount, 0) <> l_total_prorated) THEN
8103                           UPDATE zx_trx_tax_link_gt
8104                              SET line_amt = line_amt + (l_inv_tax_list(i).amount - l_total_prorated)
8105                            WHERE trx_id = l_inv_tax_list(i).invoice_id
8106                              AND trx_line_id <> l_inv_tax_list(i).line_number
8107                              AND trx_line_id =
8108                                 (SELECT (MAX(ail.line_number))
8109                                    FROM ap_invoice_lines ail
8110                                   WHERE ail.invoice_id = l_inv_tax_list(i).invoice_id
8111                                     AND ail.line_number <> l_inv_tax_list(i).line_number
8112 									AND ail.line_type_lookup_code <> 'TAX'   --Bug6608702
8113                                     AND ail.amount <> 0
8114                                     AND ail.line_group_number = l_inv_tax_list(i).line_group_number
8115                                     AND ABS(ail.amount) >=
8116                                       ( SELECT  MAX(ABS(ail2.amount))
8117                                           FROM  ap_invoice_lines ail2
8118                                          WHERE  ail2.invoice_id = ail.invoice_id
8119                                            AND  ail2.line_number <> l_inv_tax_list(i).line_number
8120                                            AND  ail2.line_number <> ail.line_number
8121 										   AND  ail2.line_type_lookup_code <> 'TAX'   --Bug6608702
8122                                            AND  ail2.line_group_number =
8123                                                   l_inv_tax_list(i).line_group_number));
8124                       END IF;
8125                       EXCEPTION
8126                         WHEN OTHERS THEN
8127                           l_return_status := FALSE;
8128                       END;
8129 
8130                      END IF;  -- l_prorating_total <> 0
8131                   END IF;  -- prorate_accross_flag = 'Y' and line_group_number is not null
8132 
8133             ---End for bug 6064593
8134 
8135         END LOOP;
8136       END IF; -- is tax pl/sql table populated?
8137 
8138     ELSE
8139       -- Invoice is tax-only.  We have 2 scenarios.
8140       -- TAX lines created for the  match other charges case (no tax
8141       -- information populated and it is expected to call calculate_Tax), and
8142       -- TAX lines manually created not allocated to any ITEM line in
8143       -- the invoice.
8144       -- It is restricted to have both types of tax-only lines in the
8145       -- same invoice, so we can assume that if the TAX line has populated
8146       -- the RCV_TRANSACTION_ID field it is a match other charges case.
8147       -- In this case we expect the tax columns to be null and the we require
8148       -- the control amount to be populated so that eTax will know how much the
8149       -- tax should be. In this case we will populate the
8150       -- zx_transaction_lines_gt GT table.
8151       -- For the second case, where the tax lines are manually created we will
8152       -- populate both zx_transaction_lines_gt and zx_import_tax_lines_gt.  It
8153       -- is also required to populate the zx_trx_tax_link_gt with a one to one
8154       -- allocation.
8155 
8156 
8157       -------------------------------------------------------------------
8158       l_debug_info := 'Step 4: Populate pl/sql table.  TRX only if match'||
8159                       'to receipt, and the three GT tables in the other case';
8160       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8161           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8162       END IF;
8163       -------------------------------------------------------------------
8164       IF ( l_inv_tax_list.COUNT > 0) THEN
8165 
8166         trans_lines.EXTEND(l_inv_tax_list.COUNT);
8167 
8168         FOR i IN l_inv_tax_list.FIRST..l_inv_tax_list.LAST LOOP
8169           -- since for the 2 cases we need to populate the trans_lines pl/sql
8170           -- table, we populate it and if the rcv_transaction_id is not null
8171           -- we set the flag P_Inv_Rcv_Matched to Y.  If it is not RCV matched, we
8172           -- will not modified the flag since N is the initial value and will
8173           -- populate the zx_import_tax_lines_gt and zx_trx_tax_link_gt GT tables
8174 
8175           -------------------------------------------------------------------
8176           l_debug_info := 'Step 5: Get line_level_action for TAX ONLY line '||
8177                           'number:'||l_inv_tax_list(i).line_number;
8178     	  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8179               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8180           END IF;
8181           -------------------------------------------------------------------
8182           IF (l_inv_tax_list(i).rcv_transaction_id IS NOT NULL) THEN
8183             -- In this case  eTax will need to run tax
8184             -- applicability and calculation because it is the matched to other
8185             -- charges case.
8186             -- since the control amount at line level is required for eTax in
8187             -- this case we will set it right away
8188 
8189             l_line_level_action := 'CREATE_TAX_ONLY';
8190             P_Inv_Rcv_Matched := 'Y';
8191             l_line_control_amount :=
8192               NVL(l_inv_tax_list(i).control_amount, l_inv_tax_list(i).amount);
8193 
8194 
8195           ELSE
8196             -- Invoice is tax-only, and there is no need to run applicability.
8197             -- In this case the user provides all the tax information for the line
8198             -- to be imported.  The additional taxable related info
8199             -- that eTax need to store will be passed to eTax using a pseudo line
8200             -- in the zx_transaction_lines_gt table.
8201 
8202             l_line_level_action := 'LINE_INFO_TAX_ONLY';
8203             l_line_control_amount := l_inv_tax_list(i).control_amount;
8204 
8205           END IF;
8206 
8207           -------------------------------------------------------------------
8208           l_debug_info := 'Step 6: Get Additional PO matched info if any ';
8209 	  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8210               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8211           END IF;
8212           -------------------------------------------------------------------
8213           IF ( l_Inv_tax_List(i).po_line_location_id IS NOT NULL) THEN
8214               -- this assigned is required since the p_po_line_location_id
8215               -- parameter is IN/OUT.  However, in this case it will not be
8216               -- modified because the po_distribution_id is not provided
8217               l_po_line_location_id := l_Inv_tax_List(i).po_line_location_id;
8218 
8219             IF NOT (AP_ETAX_UTILITY_PKG.Get_PO_Info(
8220                P_Po_Line_Location_Id         => l_po_line_location_id,
8221                P_PO_Distribution_Id          => null,
8222                P_Application_Id              => l_ref_doc_application_id,
8223                P_Entity_code                 => l_ref_doc_entity_code,
8224                P_Event_Class_Code            => l_ref_doc_event_class_code,
8225                P_PO_Quantity                 => l_ref_doc_line_quantity,
8226                P_Product_Org_Id              => l_product_org_id,
8227                P_Po_Header_Id                => l_ref_doc_trx_id,
8228                P_Po_Header_curr_conv_rate    => l_po_header_curr_conv_rate,
8229                P_Uom_Code                    => l_uom_code,
8230                P_Dist_Qty                    => l_dummy,
8231                P_Ship_Price                  => l_dummy,
8232                P_Error_Code                  => P_error_code,
8233                P_Calling_Sequence            => l_curr_calling_sequence)) THEN
8234 
8235               l_return_status := FALSE;
8236             END IF;
8237 
8238             l_ref_doc_trx_level_type := 'SHIPMENT';
8239 
8240           ELSE
8241             l_ref_doc_application_id     := Null;
8242             l_ref_doc_entity_code        := Null;
8243             l_ref_doc_event_class_code   := Null;
8244             l_ref_doc_line_quantity      := Null;
8245             l_product_org_id             := Null;
8246             l_ref_doc_trx_id             := Null;
8247             l_ref_doc_trx_level_type     := Null;
8248           END IF;
8249 
8250           -------------------------------------------------------------------
8251           l_debug_info := 'Step 7: Get Additional receipt matched info ';
8252           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8253               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8254           END IF;
8255           -------------------------------------------------------------------
8256           IF ( l_return_status = TRUE AND
8257                l_Inv_tax_List(i).rcv_transaction_id IS NOT NULL) THEN
8258             IF NOT (AP_ETAX_UTILITY_PKG.Get_Receipt_Info(
8259                P_Rcv_Transaction_Id          => l_Inv_tax_List(i).rcv_transaction_id,
8260                P_Application_Id              => l_applied_to_application_id,
8261                P_Entity_code                 => l_applied_to_entity_code,
8262                P_Event_Class_Code            => l_applied_to_event_class_code,
8263                P_Transaction_Date            => l_trx_receipt_date,
8264                P_Error_Code                  => P_error_code,
8265                P_Calling_Sequence            => l_curr_calling_sequence)) THEN
8266 
8267                l_return_status := FALSE;
8268             END IF;
8269           ELSE
8270             l_applied_to_application_id   := Null;
8271             l_applied_to_entity_code      := Null;
8272             l_applied_to_event_class_code := Null;
8273             l_trx_receipt_date            := Null;
8274           END IF;
8275 
8276           -------------------------------------------------------------------
8277           l_debug_info := 'Step 8: Get Additional Correction Invoice Info ';
8278           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8279               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8280           END IF;
8281           -------------------------------------------------------------------
8282           IF ( l_return_status = TRUE AND
8283                l_Inv_tax_List(i).corrected_inv_id IS NOT NULL AND
8284                l_Inv_tax_list(i).corrected_line_number IS NOT NULL) THEN
8285             IF NOT (AP_ETAX_UTILITY_PKG.Get_Corrected_Invoice_Info(
8286                P_Corrected_Invoice_Id        => l_Inv_tax_List(i).corrected_inv_id,
8287                P_Corrected_Line_Number       => l_Inv_tax_List(i).corrected_line_number,
8288                P_Application_Id              => l_adj_doc_application_id,
8289                P_Entity_code                 => l_adj_doc_entity_code,
8290                P_Event_Class_Code            => l_adj_doc_event_class_code,
8291                P_Invoice_Number              => l_adj_doc_number,
8292                P_Invoice_Date                => l_adj_doc_date,
8293                P_Error_Code                  => P_error_code,
8294                P_Calling_Sequence            => l_curr_calling_sequence)) THEN
8295 
8296               l_return_status := FALSE;
8297             END IF;
8298           ELSE
8299             l_adj_doc_application_id   := Null;
8300             l_adj_doc_entity_code      := Null;
8301             l_adj_doc_event_class_code := Null;
8302             l_adj_doc_number           := Null;
8303             l_adj_doc_date             := Null;
8304           END IF;
8305 
8306           -------------------------------------------------------------------
8307           l_debug_info := 'Step 9: Get line_amt_includes_tax_flag';
8308           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8309               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8310           END IF;
8311           -------------------------------------------------------------------
8312 
8313           IF (l_Inv_tax_List(i).po_line_location_id IS NOT NULL) THEN
8314             -- NONE
8315             l_line_amt_includes_tax_flag := 'N';
8316 
8317           ELSE
8318            IF (p_calling_mode = 'CALCULATE QUOTE')
8319               OR
8320               (p_invoice_header_rec.invoice_type_lookup_code = 'EXPENSE REPORT'
8321                and nvl(l_inv_tax_list(i).line_type_lookup_code, 'N') <> 'PREPAY') THEN
8322              -- ALL
8323              l_line_amt_includes_tax_flag := 'A';
8324 
8325            ELSE
8326              -- STANDARD
8327              l_line_amt_includes_tax_flag := 'S';
8328 
8329            END IF;
8330           END IF;
8331 
8332           -------------------------------------------------------------------
8333           l_debug_info := 'Step 10: Get ctrl_hdr_tx_appl_flag';
8334           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8335               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8336           END IF;
8337           -------------------------------------------------------------------
8338           IF P_Invoice_Header_Rec.control_amount IS NOT NULL THEN
8339             l_ctrl_hdr_tx_appl_flag := 'Y';
8340           ELSE
8341             l_ctrl_hdr_tx_appl_flag := 'N';
8342           END IF;
8343 
8344           -------------------------------------------------------------------
8345           l_debug_info := 'Step 11: Populate pl/sql table';
8346           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8347               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8348           END IF;
8349           -------------------------------------------------------------------
8350           IF (l_return_status = TRUE ) THEN
8351 
8352             trans_lines(i).application_id 			:= 200;
8353             trans_lines(i).entity_code 				:= 'AP_INVOICES';
8354             trans_lines(i).event_class_code 			:= p_event_class_code;
8355             trans_lines(i).trx_id 				:= P_Invoice_Header_Rec.invoice_id;
8356             trans_lines(i).trx_level_type 			:= 'LINE';
8357             trans_lines(i).trx_line_id 				:= l_inv_tax_list(i).line_number;
8358             trans_lines(i).line_level_action 			:= l_line_level_action;
8359 
8360             trans_lines(i).trx_receipt_date 			:= l_trx_receipt_date;
8361             trans_lines(i).trx_line_type 			:= l_inv_tax_list(i).line_type_lookup_code;
8362             trans_lines(i).trx_line_date 			:= P_Invoice_Header_Rec.invoice_date;
8363             trans_lines(i).trx_business_category 		:= l_inv_tax_list(i).trx_business_category;
8364             trans_lines(i).line_intended_use 			:= l_inv_tax_list(i).primary_intended_use;
8365             trans_lines(i).user_defined_fisc_class 		:= l_inv_tax_list(i).user_defined_fisc_class;
8366             trans_lines(i).line_amt 				:= l_inv_tax_list(i).amount;
8367             trans_lines(i).trx_line_quantity 			:= l_inv_tax_list(i).quantity_invoiced;
8368             trans_lines(i).unit_price 				:= l_inv_tax_list(i).unit_price;
8369 
8370             trans_lines(i).product_id 				:= l_inv_tax_list(i).inventory_item_id;
8371             trans_lines(i).product_fisc_classification 		:= l_inv_tax_list(i).product_fisc_classification;
8372             trans_lines(i).product_org_id 			:= l_product_org_id;
8373             trans_lines(i).uom_code 				:= l_uom_code;
8374             trans_lines(i).product_type 			:= l_inv_tax_list(i).product_type;
8375             trans_lines(i).product_category 			:= l_inv_tax_list(i).product_category;
8376             trans_lines(i).fob_point 				:= l_fob_point;
8377 
8378             -- 7262269
8379             IF l_inv_tax_list(i).po_line_location_id IS NOT NULL THEN
8380                l_ship_to_party_id := get_po_ship_to_org_id (l_inv_tax_list(i).po_line_location_id);
8381             ELSE
8382                l_ship_to_party_id := l_inv_tax_list(i).org_id;
8383             END IF;
8384 
8385             trans_lines(i).ship_to_party_id		:= l_ship_to_party_id;
8386             -- 7262269
8387 
8388             trans_lines(i).ship_from_party_id 			:= P_Invoice_Header_Rec.party_id;
8389 
8390             trans_lines(i).bill_to_party_id			:= l_inv_tax_list(i).org_id;
8391             trans_lines(i).bill_from_party_id			:= P_Invoice_Header_Rec.party_id;
8392             trans_lines(i).ship_from_party_site_id		:= P_Invoice_Header_Rec.party_site_id;
8393             trans_lines(i).bill_from_party_site_id		:= P_Invoice_Header_Rec.party_site_id;
8394 
8395             trans_lines(i).ship_to_location_id			:= l_inv_tax_list(i).ship_to_location_id;
8396 	    trans_lines(i).ship_from_location_id		:= l_location_id;
8397             trans_lines(i).bill_to_location_id			:= l_bill_to_location_id;
8398             trans_lines(i).bill_from_location_id		:= l_location_id;
8399 
8400             trans_lines(i).account_ccid				:= l_inv_tax_list(i).default_dist_ccid;
8401             trans_lines(i).merchant_party_country		:= l_inv_tax_list(i).country_of_supply;
8402 
8403             trans_lines(i).ref_doc_application_id		:= l_ref_doc_application_id;
8404             trans_lines(i).ref_doc_entity_code			:= l_ref_doc_entity_code;
8405             trans_lines(i).ref_doc_event_class_code		:= l_ref_doc_event_class_code;
8406             trans_lines(i).ref_doc_trx_id			:= l_ref_doc_trx_id;
8407             trans_lines(i).ref_doc_line_id			:= l_inv_tax_list(i).po_line_location_id;
8408             trans_lines(i).ref_doc_line_quantity		:= l_ref_doc_line_quantity;
8409 
8410             trans_lines(i).adjusted_doc_application_id		:= l_adj_doc_application_id;
8411             trans_lines(i).adjusted_doc_entity_code		:= l_adj_doc_entity_code;
8412             trans_lines(i).adjusted_doc_event_class_code	:= l_adj_doc_event_class_code;
8413             trans_lines(i).adjusted_doc_trx_id			:= l_inv_tax_list(i).corrected_inv_id;
8414             trans_lines(i).adjusted_doc_line_id			:= l_inv_tax_list(i).corrected_line_number;
8415             trans_lines(i).adjusted_doc_number			:= l_adj_doc_number;
8416             trans_lines(i).adjusted_doc_date			:= l_adj_doc_date;
8417 
8418             trans_lines(i).applied_to_application_id		:= l_applied_to_application_id;
8419             trans_lines(i).applied_to_entity_code		:= l_applied_to_entity_code;
8420             trans_lines(i).applied_to_event_class_code		:= l_applied_to_event_class_code;
8421             trans_lines(i).applied_to_trx_id			:= l_inv_tax_list(i).rcv_transaction_id;
8422 
8423 	    IF l_inv_tax_list(i).rcv_transaction_id IS NOT NULL THEN
8424 	       trans_lines(i).applied_to_trx_line_id		:= l_inv_tax_list(i).po_line_location_id;
8425             END IF;
8426 
8427             trans_lines(i).trx_line_number			:= l_inv_tax_list(i).line_number;
8428             trans_lines(i).trx_line_description			:= l_inv_tax_list(i).description;
8429             trans_lines(i).product_description			:= l_inv_tax_list(i).item_description;
8430             trans_lines(i).trx_line_gl_date			:= l_inv_tax_list(i).accounting_date;
8431 
8432             trans_lines(i).merchant_party_name			:= l_inv_tax_list(i).merchant_name;
8433             trans_lines(i).merchant_party_document_number	:= l_inv_tax_list(i).merchant_document_number;
8434             trans_lines(i).merchant_party_reference		:= l_inv_tax_list(i).merchant_reference;
8435             trans_lines(i).merchant_party_taxpayer_id		:= l_inv_tax_list(i).merchant_taxpayer_id;
8436             trans_lines(i).merchant_party_tax_reg_number	:= l_inv_tax_list(i).merchant_tax_reg_number;
8437 
8438             trans_lines(i).assessable_value			:= l_inv_tax_list(i).assessable_value;
8439             trans_lines(i).line_amt_includes_tax_flag		:= l_line_amt_includes_tax_flag;
8440             trans_lines(i).historical_flag			:= 'N';
8441 
8442             trans_lines(i).ctrl_hdr_tx_appl_flag		:= l_ctrl_hdr_tx_appl_flag;
8443             trans_lines(i).ctrl_total_line_tx_amt		:= l_line_control_amount;
8444 
8445             trans_lines(i).source_application_id		:= l_inv_tax_list(i).source_application_id;
8446             trans_lines(i).source_entity_code	   		:= l_inv_tax_list(i).source_entity_code;
8447             trans_lines(i).source_event_class_code 		:= l_inv_tax_list(i).source_event_class_code;
8448             trans_lines(i).source_trx_id	   		:= l_inv_tax_list(i).source_trx_id;
8449             trans_lines(i).source_line_id	   		:= l_inv_tax_list(i).source_line_id;
8450             trans_lines(i).source_trx_level_type		:= l_inv_tax_list(i).source_trx_level_type;
8451 
8452             trans_lines(i).input_tax_classification_code	:= l_inv_tax_list(i).tax_classification_code;
8453 -- Debug messages added for 6321366
8454 IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8455 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_transaction_lines_gt values ');
8456               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: ' || trans_lines(i).event_class_code);
8457               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '           || trans_lines(i).trx_id);
8458               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_id: '      || trans_lines(i).trx_line_id);
8459               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_level_type: '   || trans_lines(i).trx_level_type);
8460               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_type: '    || trans_lines(i).trx_line_type );
8461               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_level_action: '|| trans_lines(i).line_level_action);
8462               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_amt: '         || trans_lines(i).line_amt);
8463               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'unit_price: '       || trans_lines(i).unit_price);
8464               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_amt_includes_tax_flag: '       || trans_lines(i).line_amt_includes_tax_flag );
8465 END IF;
8466 
8467           END IF; -- l_return_status
8468 
8469           IF (l_inv_tax_list(i).rcv_transaction_id IS NULL) THEN
8470             -------------------------------------------------------------------
8471             l_debug_info := 'Step 12: Populate pl/sql table for TAX line if it '||
8472                             'is not receipt matched';
8473             IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8474                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8475             END IF;
8476             -------------------------------------------------------------------
8477             IF (l_return_status = TRUE ) THEN
8478               tax_lines.EXTEND(1);
8479               link_lines.EXTEND(1);
8480 
8481               tax_lines(k).summary_tax_line_number		:= l_inv_tax_list(i).line_number;
8482               tax_lines(k).internal_organization_id		:= P_Invoice_Header_Rec.org_id;
8483               tax_lines(k).application_id			:= 200;
8484               tax_lines(k).entity_code				:= 'AP_INVOICES';
8485               tax_lines(k).event_class_code			:= p_event_class_code;
8486               tax_lines(k).trx_id				:= P_Invoice_Header_Rec.invoice_id;
8487 
8488               tax_lines(k).tax_regime_code			:= l_inv_tax_list(i).tax_regime_code;
8489               tax_lines(k).tax					:= l_inv_tax_list(i).tax;
8490               tax_lines(k).tax_status_code			:= l_inv_tax_list(i).tax_status_code;
8491               tax_lines(k).tax_rate_code			:= l_inv_tax_list(i).tax_rate_code;
8492               tax_lines(k).tax_rate				:= l_inv_tax_list(i).tax_rate;
8493               tax_lines(k).tax_amt				:= l_inv_tax_list(i).amount;
8494               tax_lines(k).tax_line_allocation_flag             := 'Y';
8495               -- Populating tax jurisdiction code for bug 6411838
8496               tax_lines(k).tax_jurisdiction_code                := l_inv_tax_list(i).tax_jurisdiction_code ;
8497 -- Debug messages added for 6321366
8498  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8499 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_import_tax_lines_gt values ');
8500               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'summary_tax_line_number: ' || tax_lines(k).summary_tax_line_number);
8501               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'application_id: '           || tax_lines(k).application_id);
8502               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: '      || tax_lines(k).event_class_code);
8503               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '   || tax_lines(k).trx_id);
8504               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax: '   || tax_lines(k).tax );
8505               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax_amt: '    || tax_lines(k).tax_amt );
8506               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax_line_allocation_flag: '|| tax_lines(k).tax_line_allocation_flag);
8507  END IF;
8508 
8509               --------------------------------------------------------------
8510               l_debug_info := 'Step 13: Populate link structure';
8511               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8512                   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8513               END IF;
8514               --------------------------------------------------------------
8515               link_lines(k).application_id			:= 200;
8516               link_lines(k).entity_code				:= 'AP_INVOICES';
8517               link_lines(k).event_class_code			:= p_event_class_code;
8518               link_lines(k).trx_id				:= P_Invoice_Header_Rec.invoice_id;
8519               link_lines(k).trx_level_type			:= 'LINE';
8520               link_lines(k).trx_line_id				:= l_inv_tax_list(i).line_number;
8521               link_lines(k).summary_tax_line_number		:= l_inv_tax_list(i).line_number;
8522               link_lines(k).line_amt				:= l_inv_tax_list(i).amount;
8523 
8524 -- Debug messages added for 6321366
8525  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8526 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_trx_tax_link_gt values ');
8527               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'summary_tax_line_number: ' || link_lines(k).summary_tax_line_number);
8528               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'application_id: '           || link_lines(k).application_id);
8529               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: '      || link_lines(k).event_class_code);
8530               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '   || link_lines(k).trx_id);
8531               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_level_type: '   || link_lines(k).trx_level_type );
8532               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_id: '   || link_lines(k).trx_line_id);
8533               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_amt: '    || link_lines(k).line_amt );
8534  END IF;
8535               k := k + 1;
8536 
8537             END IF;  -- l_return_status validation for TAX lines
8538           END IF;
8539 
8540         END LOOP;  -- end of loop TAX lines
8541       END IF; -- is l_inv_tax_list populated
8542     END IF; -- Is invoice tax only?
8543 
8544     -------------------------------------------------------------------
8545     l_debug_info := 'Step 14: Bulk Insert into global temp tables';
8546     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8547       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8548     END IF;
8549     -------------------------------------------------------------------
8550     IF ( l_return_status = TRUE ) THEN
8551 
8552       IF (trans_lines.COUNT > 0) THEN
8553         FORALL m IN trans_lines.FIRST..trans_lines.LAST
8554           INSERT INTO zx_transaction_lines_gt
8555           VALUES trans_lines(m);
8556       END IF;
8557 
8558       IF (tax_lines.COUNT > 0) THEN
8559 
8560     -------------------------------------------------------------------
8561     l_debug_info := 'Step 15: Populate Tax Lines: '||tax_lines.count;
8562     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8563       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8564     END IF;
8565     -------------------------------------------------------------------
8566 
8567         FORALL m IN tax_lines.FIRST..tax_lines.LAST
8568           INSERT INTO zx_import_tax_lines_gt
8569           VALUES tax_lines(m);
8570       END IF;
8571 
8572       -- This bulk insert will only be effective when the invoice is tax_only
8573       -- and there are tax lines with the tax info to be imported
8574       IF (link_lines.COUNT > 0) THEN
8575         FORALL m IN link_lines.FIRST..link_lines.LAST
8576           INSERT INTO zx_trx_tax_link_gt
8577           VALUES link_lines(m);
8578       END IF;
8579 
8580     END IF;
8581 
8582     RETURN l_return_status;
8583 
8584   EXCEPTION
8585     WHEN OTHERS THEN
8586       IF (SQLCODE <> -20001) THEN
8587         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
8588         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
8589         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
8590         FND_MESSAGE.SET_TOKEN('PARAMETERS',
8591           ' P_Invoice_Header_Rec = '||P_Invoice_Header_Rec.Invoice_Id||
8592           ' P_Calling_Mode ='||P_Calling_Mode||
8593           ' P_Error_Code = '||P_Error_Code||
8594           ' P_Calling_Sequence = '||P_Calling_Sequence);
8595         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
8596       END IF;
8597 
8598       APP_EXCEPTION.RAISE_EXCEPTION;
8599 
8600   END Populate_Tax_Lines_GT;
8601 
8602 
8603 /*=============================================================================
8604  |  FUNCTION - Populate_Distributions_GT()
8605  |
8606  |  DESCRIPTION
8607  |      This function will get additional information required to populate the
8608  |      ZX_ITM_DISTRIBUTIONS_GT
8609  |      This function returns TRUE if the population of the temp table goes
8610  |      through successfully.  Otherwise, FALSE.
8611  |
8612  |  PARAMETERS
8613  |      P_Invoice_Header_Rec - record with invoice header information
8614  |      P_Calling_Mode - calling mode. it is used to
8615  |      P_Event_Class_Code - Event class code for document
8616  |      P_Event_Type_Code - Event Type code for invoice
8617  |      P_error_code - Error code to be returned
8618  |      P_calling_sequence -  Calling sequence
8619  |
8620  |  MODIFICATION HISTORY
8621  |    DATE          Author         Action
8622  |    20-OCT-2003   SYIDNER        Created
8623  |
8624  *============================================================================*/
8625 
8626   FUNCTION Populate_Distributions_GT(
8627              P_Invoice_Header_Rec      IN ap_invoices_all%ROWTYPE,
8628              P_Calling_Mode            IN VARCHAR2,
8629              P_Event_Class_Code        IN VARCHAR2,
8630              P_Event_Type_Code         IN VARCHAR2,
8631              P_Error_Code              OUT NOCOPY VARCHAR2,
8632              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
8633   IS
8634     l_debug_info                 VARCHAR2(240);
8635     l_curr_calling_sequence      VARCHAR2(4000);
8636 
8637     TYPE Trans_Dist_Tab_Type IS TABLE OF zx_itm_distributions_gt%ROWTYPE;
8638     trans_dists                     Trans_Dist_Tab_Type := Trans_Dist_Tab_Type();
8639 
8640     -- Purchase Order Info
8641     l_ref_doc_application_id	    zx_itm_distributions_gt.ref_doc_application_id%TYPE;
8642     l_ref_doc_entity_code	    zx_itm_distributions_gt.ref_doc_entity_code%TYPE;
8643     l_ref_doc_event_class_code      zx_itm_distributions_gt.ref_doc_event_class_code%TYPE;
8644     l_ref_doc_trx_id                zx_itm_distributions_gt.ref_doc_trx_id%TYPE;
8645     l_ref_doc_trx_level_type	    zx_itm_distributions_gt.ref_doc_trx_level_type%TYPE;
8646     l_ref_doc_line_quantity	    zx_transaction_lines_gt.ref_doc_line_quantity%TYPE;
8647     l_ref_doc_trx_line_dist_qty     zx_itm_distributions_gt.ref_doc_trx_line_dist_qty%TYPE;
8648     l_po_unit_price		    po_line_locations_all.price_override%TYPE;
8649     l_po_line_location_id           po_line_locations_all.line_location_id%TYPE;
8650     l_po_release_id                 zx_itm_distributions_gt.ref_doc_trx_id%TYPE;
8651     l_product_org_id                zx_transaction_lines_gt.product_org_id%TYPE;
8652     l_uom_code                      mtl_units_of_measure.uom_code%TYPE;
8653 
8654     l_dist_level_action             zx_itm_distributions_gt.dist_level_action%TYPE;
8655     l_line_quantity_invoiced        zx_itm_distributions_gt.trx_line_quantity%TYPE;
8656     l_amount                        zx_itm_distributions_gt.trx_line_dist_amt%TYPE;
8657     l_price_diff		    zx_itm_distributions_gt.price_diff%TYPE;
8658     l_po_header_curr_conv_rate      zx_itm_distributions_gt.ref_doc_curr_conv_rate%TYPE;
8659     l_receipt_curr_conv_rate	    zx_itm_distributions_gt.applied_to_doc_curr_conv_rate%TYPE;
8660     l_converted_qty		    ap_invoice_distributions_all.quantity_invoiced%TYPE;
8661     l_converted_price		    ap_invoice_distributions_all.unit_price%TYPE;
8662 
8663     -- Correction Invoice Info
8664     l_adj_doc_application_id	    zx_transaction_lines_gt.adjusted_doc_application_id%TYPE;
8665     l_adj_doc_entity_code           zx_transaction_lines_gt.adjusted_doc_entity_code%TYPE;
8666     l_adj_doc_event_class_code      zx_transaction_lines_gt.adjusted_doc_event_class_code%TYPE;
8667     l_adj_doc_number                zx_transaction_lines_gt.adjusted_doc_number%TYPE;
8668     l_adj_doc_date                  zx_transaction_lines_gt.adjusted_doc_date%TYPE;
8669     l_adj_doc_trx_level_type        zx_transaction_lines_gt.adjusted_doc_trx_level_type%TYPE;
8670     l_adj_doc_trx_id		    zx_itm_distributions_gt.adjusted_doc_trx_id%TYPE;
8671     l_adj_doc_line_id		    zx_itm_distributions_gt.adjusted_doc_line_id%TYPE;
8672 
8673     -- Prepayment Info
8674     l_prepay_doc_application_id     zx_transaction_lines_gt.applied_from_application_id%TYPE;
8675     l_prepay_doc_entity_code        zx_transaction_lines_gt.applied_from_entity_code%TYPE;
8676     l_prepay_doc_event_class_code   zx_transaction_lines_gt.applied_from_event_class_code%TYPE;
8677     l_prepay_doc_number             ap_invoices_all.invoice_num%TYPE;
8678     l_prepay_doc_date               ap_invoices_all.invoice_date%TYPE;
8679     l_applied_from_trx_id	    ap_invoice_lines_all.invoice_id%TYPE;
8680     l_applied_from_line_id	    ap_invoice_lines_all.line_number%TYPE;
8681     l_applied_from_trx_level_type   zx_transaction_lines_gt.applied_from_trx_level_type%TYPE;
8682 
8683     l_prepay_inv_id	            ap_invoice_lines_all.invoice_id%TYPE;
8684     l_prepay_line_num               ap_invoice_lines_all.line_number%TYPE;
8685 
8686     l_return_status                 BOOLEAN := TRUE;
8687     l_api_name                      CONSTANT VARCHAR2(100) := 'Populate_Distributions_GT';
8688 
8689   BEGIN
8690 
8691     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Populate_Distributions_GT<-'||
8692                                P_calling_sequence;
8693 
8694     IF (l_inv_dist_list.COUNT > 0) THEN
8695 
8696       trans_dists.EXTEND(l_inv_dist_list.COUNT);
8697 
8698       -------------------------------------------------------------------
8699       l_debug_info := 'Step 1: Loop through all the distributions';
8700       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8701           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8702       END IF;
8703       -------------------------------------------------------------------
8704 
8705       FOR i IN l_inv_dist_list.FIRST..l_inv_dist_list.LAST LOOP
8706 
8707         -------------------------------------------------------------------
8708         l_debug_info := 'Step 2: Get line_level_action for distribution';
8709 	IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8710             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8711         END IF;
8712         -------------------------------------------------------------------
8713         IF ( l_inv_dist_list(i).tax_already_distributed_flag ='Y' ) THEN
8714             l_dist_level_action := 'UPDATE';
8715 
8716         ELSE
8717            l_dist_level_action := 'CREATE';
8718 
8719         END IF;
8720 
8721         -----------------------------------------------------------------
8722         l_debug_info := 'Step 3: Update the amount including IPV/ERV';
8723 	IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8724             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8725         END IF;
8726         -----------------------------------------------------------------
8727         IF (l_inv_dist_list(i).related_id IS NOT NULL
8728             OR l_inv_dist_list(i).related_retainage_dist_id IS NOT NULL) THEN
8729            SELECT SUM(amount)
8730              INTO l_amount
8731              FROM ap_invoice_distributions_all
8732             WHERE invoice_id = l_inv_dist_list(i).invoice_id
8733 	      AND (related_id = l_inv_dist_list(i).related_id
8734                    or related_retainage_dist_id = l_inv_dist_list(i).related_retainage_dist_id);
8735 
8736         ELSE
8737            l_amount := l_inv_dist_list(i).amount;
8738 
8739         END IF;
8740 
8741         -------------------------------------------------------------------
8742         l_debug_info := 'Step 4: Get correction invoice info';
8743         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8744             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8745         END IF;
8746         -------------------------------------------------------------------
8747         BEGIN
8748           SELECT quantity_invoiced, po_release_id
8749             INTO l_line_quantity_invoiced, l_po_release_id
8750             FROM ap_invoice_lines_all
8751            WHERE invoice_id = l_inv_dist_list(i).invoice_id
8752              AND line_number = l_inv_dist_list(i).invoice_line_number;
8753         END;
8754 
8755         -------------------------------------------------------------------
8756         l_debug_info := 'Step 5: Get purchase order info';
8757 	IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8758             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8759         END IF;
8760         -------------------------------------------------------------------
8761         IF ( l_inv_dist_list(i).po_distribution_id IS NOT NULL) THEN
8762 
8763           IF NOT (AP_ETAX_UTILITY_PKG.Get_PO_Info(
8764                P_Po_Line_Location_Id      => l_po_line_location_id,
8765                P_Po_Distribution_Id       => l_inv_dist_list(i).po_distribution_id,
8766                P_Application_Id           => l_ref_doc_application_id,
8767                P_Entity_code              => l_ref_doc_entity_code,
8768                P_Event_Class_Code         => l_ref_doc_event_class_code,
8769                P_PO_Quantity              => l_ref_doc_line_quantity,
8770                P_Product_Org_Id           => l_product_org_id,
8771                P_Po_Header_Id             => l_ref_doc_trx_id,
8772                P_Po_Header_curr_conv_rate => l_po_header_curr_conv_rate,
8773                P_Uom_Code                 => l_uom_code,
8774                P_Dist_Qty                 => l_ref_doc_trx_line_dist_qty,
8775                P_Ship_Price               => l_po_unit_price,
8776                P_Error_Code               => P_error_code,
8777                P_Calling_Sequence         => l_curr_calling_sequence)) THEN
8778 
8779             l_return_status := FALSE;
8780           END IF;
8781 
8782 	  l_ref_doc_trx_level_type := 'SHIPMENT';
8783 
8784         ELSE
8785           l_ref_doc_application_id     := Null;
8786           l_ref_doc_entity_code        := Null;
8787           l_ref_doc_event_class_code   := Null;
8788           l_ref_doc_line_quantity      := Null;
8789           l_product_org_id             := Null;
8790           l_ref_doc_trx_id             := Null;
8791           l_ref_doc_trx_level_type     := Null;
8792 	  l_ref_doc_trx_line_dist_qty  := Null;
8793 	  l_po_unit_price              := Null;
8794 	  l_po_header_curr_conv_rate   := Null;
8795 	  l_uom_code		       := Null;
8796         END IF;
8797 
8798         -------------------------------------------------------------------
8799         l_debug_info := 'Step 6: Set ref_doc_trx_id if distribution is '||
8800                         'receipt matched';
8801 	IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8802             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8803         END IF;
8804         -------------------------------------------------------------------
8805         IF (l_return_status = TRUE AND
8806             l_inv_dist_list(i).rcv_transaction_id IS NOT NULL) THEN
8807 
8808           BEGIN
8809             SELECT currency_conversion_rate
8810               INTO l_receipt_curr_conv_rate
8811               FROM rcv_transactions
8812              WHERE transaction_id = l_inv_dist_list(i).rcv_transaction_id;
8813           END;
8814 
8815           get_converted_qty_price
8816                 (x_invoice_distribution_id => l_inv_dist_list(i).invoice_distribution_id,
8817                  x_inv_qty                 => l_converted_qty,
8818                  x_inv_price               => l_converted_price);
8819 
8820         ELSE
8821 	    l_receipt_curr_conv_rate := Null;
8822 	    l_converted_qty          := Null;
8823 	    l_converted_price	     := Null;
8824         END IF;
8825 
8826 /*
8827         -------------------------------------------------------------------
8828         l_debug_info := 'Step 8: Get Additional Correction Invoice Info ';
8829         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8830             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8831         END IF;
8832         -------------------------------------------------------------------
8833         IF ( l_return_status = TRUE AND
8834 	     l_inv_dist_list(i).corrected_invoice_dist_id IS NOT NULL) THEN
8835 
8836 	   Select invoice_id, invoice_line_number
8837 	     Into l_adj_doc_trx_id, l_adj_doc_line_id
8838 	     From ap_invoice_distributions_all
8839 	    Where invoice_distribution_id = l_inv_dist_list(i).corrected_invoice_dist_id;
8840 
8841            IF NOT (AP_ETAX_UTILITY_PKG.Get_Corrected_Invoice_Info(
8842               P_Corrected_Invoice_Id        => l_inv_line_list(i).corrected_inv_id,
8843               P_Corrected_Line_Number       => l_inv_line_list(i).corrected_line_number,
8844               P_Application_Id              => l_adj_doc_application_id,
8845               P_Entity_code                 => l_adj_doc_entity_code,
8846               P_Event_Class_Code            => l_adj_doc_event_class_code,
8847               P_Invoice_Number              => l_adj_doc_number,
8848               P_Invoice_Date                => l_adj_doc_date,
8849               P_Error_Code                  => P_error_code,
8850               P_Calling_Sequence            => l_curr_calling_sequence)) THEN
8851 
8852               l_return_status := FALSE;
8853           END IF;
8854 
8855           l_adj_doc_trx_level_type := 'LINE';
8856 
8857         ELSE
8858             l_adj_doc_application_id   := Null;
8859             l_adj_doc_entity_code      := Null;
8860             l_adj_doc_event_class_code := Null;
8861             l_adj_doc_number           := Null;
8862             l_adj_doc_date             := Null;
8863             l_adj_doc_trx_level_type   := Null;
8864         END IF;
8865 
8866         -------------------------------------------------------------------
8867         l_debug_info := 'Step 8: Get prepayment invoice info';
8868         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8869             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8870         END IF;
8871         -------------------------------------------------------------------
8872         IF ( l_return_status = TRUE AND
8873 	     l_inv_dist_list(i).prepay_distribution_id IS NOT NULL) THEN
8874 
8875 	  SELECT invoice_id, invoice_line_number
8876 	    INTO l_applied_from_trx_id, l_applied_from_line_id
8877 	    FROM ap_invoice_distributions_all
8878 	   WHERE invoice_distribution_id = l_inv_dist_list(i).prepay_distribution_id;
8879 
8880           IF NOT (AP_ETAX_UTILITY_PKG.Get_Prepay_Invoice_Info(
8881               P_Prepay_Invoice_Id           => l_applied_from_trx_id,
8882               P_Prepay_Line_Number          => l_applied_from_line_id,
8883               P_Application_Id              => l_prepay_doc_application_id,
8884               P_Entity_code                 => l_prepay_doc_entity_code,
8885               P_Event_Class_Code            => l_prepay_doc_event_class_code,
8886               P_Invoice_Number              => l_prepay_doc_number,
8887               P_Invoice_Date                => l_prepay_doc_date,
8888               P_Error_Code                  => P_error_code,
8889               P_Calling_Sequence            => l_curr_calling_sequence)) THEN
8890 
8891               l_return_status := FALSE;
8892           END IF;
8893 
8894           l_applied_from_trx_level_type := 'LINE';
8895 
8896         ELSE
8897           l_prepay_doc_application_id   := Null;
8898           l_prepay_doc_entity_code      := Null;
8899           l_prepay_doc_event_class_code := Null;
8900           l_prepay_doc_number           := Null;
8901           l_prepay_doc_date             := Null;
8902           l_applied_from_trx_level_type := Null;
8903 	  l_applied_from_trx_id		:= Null;
8904 	  l_applied_from_line_id	:= Null;
8905         END IF;
8906 */
8907         -------------------------------------------------------------------
8908         l_debug_info := 'Step 7: Populate pl/sql table';
8909 	IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8910             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8911         END IF;
8912         -------------------------------------------------------------------
8913         IF (l_return_status = TRUE) THEN
8914 
8915           trans_dists(i).application_id			:= AP_ETAX_PKG.AP_APPLICATION_ID;
8916           trans_dists(i).entity_code			:= AP_ETAX_PKG.AP_ENTITY_CODE;
8917           trans_dists(i).event_class_code		:= P_event_class_code;
8918           trans_dists(i).trx_id				:= l_inv_dist_list(i).invoice_id;
8919 
8920           IF p_calling_mode = 'DISTRIBUTE RECOUP' THEN
8921              IF  l_inv_dist_list(i).prepay_distribution_id IS NOT NULL THEN
8922 
8923 		 SELECT invoice_id, invoice_line_number
8924                    INTO l_prepay_inv_id, l_prepay_line_num
8925                    FROM ap_invoice_distributions_all
8926                   WHERE invoice_distribution_id = l_inv_dist_list(i).prepay_distribution_id;
8927 
8928                  trans_dists(i).trx_line_id := -1 * (l_prepay_inv_id || l_prepay_line_num || l_inv_dist_list(i).invoice_line_number);
8929 
8930 	     END IF;
8931 	  ELSE
8932 	     trans_dists(i).trx_line_id := l_inv_dist_list(i).invoice_line_number;
8933 
8934           END IF;
8935 
8936           trans_dists(i).trx_level_type			:= 'LINE';
8937           trans_dists(i).trx_line_dist_id		:= l_inv_dist_list(i).invoice_distribution_id;
8938           trans_dists(i).dist_level_action		:= l_dist_level_action;
8939           trans_dists(i).item_dist_number		:= l_inv_dist_list(i).distribution_line_number;
8940           trans_dists(i).dist_intended_use		:= l_inv_dist_list(i).intended_use;
8941 
8942           trans_dists(i).trx_line_dist_amt 		:= l_amount;
8943           trans_dists(i).trx_line_quantity 		:= l_line_quantity_invoiced;
8944           trans_dists(i).trx_line_dist_qty 		:= nvl(nvl(l_converted_qty, l_inv_dist_list(i).quantity_invoiced),1);
8945 	  trans_dists(i).unit_price			:= nvl(l_inv_dist_list(i).unit_price,l_amount);
8946           trans_dists(i).trx_line_dist_date		:= l_inv_dist_list(i).accounting_date;
8947 
8948           trans_dists(i).task_id			:= l_inv_dist_list(i).task_id;
8949           trans_dists(i).award_id			:= l_inv_dist_list(i).award_id;
8950           trans_dists(i).project_id			:= l_inv_dist_list(i).project_id;
8951           trans_dists(i).expenditure_type		:= l_inv_dist_list(i).expenditure_type;
8952           trans_dists(i).expenditure_organization_id	:= l_inv_dist_list(i).expenditure_organization_id;
8953           trans_dists(i).expenditure_item_date		:= l_inv_dist_list(i).expenditure_item_date;
8954           trans_dists(i).account_ccid 			:= l_inv_dist_list(i).dist_code_combination_id;
8955 
8956           trans_dists(i).ref_doc_application_id 	:= l_ref_doc_application_id;
8957           trans_dists(i).ref_doc_entity_code 		:= l_ref_doc_entity_code;
8958           trans_dists(i).ref_doc_event_class_code 	:= l_ref_doc_event_class_code;
8959           trans_dists(i).ref_doc_trx_id 		:= l_ref_doc_trx_id;
8960           trans_dists(i).ref_doc_line_id		:= l_po_line_location_id;
8961           trans_dists(i).ref_doc_dist_id 		:= l_inv_dist_list(i).po_distribution_id;
8962 	  trans_dists(i).ref_doc_trx_level_type 	:= l_ref_doc_trx_level_type;
8963 	  trans_dists(i).ref_doc_trx_line_dist_qty 	:= l_ref_doc_trx_line_dist_qty;
8964 
8965           trans_dists(i).adjusted_doc_dist_id           := l_inv_dist_list(i).corrected_invoice_dist_id;
8966           trans_dists(i).applied_from_dist_id           := l_inv_dist_list(i).prepay_distribution_id;
8967 
8968           /*
8969           trans_dists(i).adjusted_doc_application_id 	:= l_adj_doc_application_id;
8970           trans_dists(i).adjusted_doc_entity_code 	:= l_adj_doc_entity_code;
8971           trans_dists(i).adjusted_doc_event_class_code 	:= l_adj_doc_event_class_code;
8972           trans_dists(i).adjusted_doc_trx_id 		:= l_adj_doc_trx_id;
8973           trans_dists(i).adjusted_doc_line_id 		:= l_adj_doc_line_id;
8974           trans_dists(i).adjusted_doc_dist_id		:= l_inv_dist_list(i).corrected_invoice_dist_id;
8975           trans_dists(i).adjusted_doc_trx_level_type 	:= l_adj_doc_trx_level_type;
8976 
8977           trans_dists(i).applied_from_application_id 	:= l_prepay_doc_application_id;
8978           trans_dists(i).applied_from_entity_code 	:= l_prepay_doc_entity_code;
8979           trans_dists(i).applied_from_event_class_code 	:= l_prepay_doc_event_class_code;
8980           trans_dists(i).applied_from_trx_id 		:= l_applied_from_trx_id;
8981           trans_dists(i).applied_from_line_id 		:= l_applied_from_line_id;
8982        	  trans_dists(i).applied_from_dist_id		:= l_inv_dist_list(i).prepay_distribution_id;
8983           trans_dists(i).applied_from_trx_level_type 	:= l_applied_from_trx_level_type;
8984           */
8985 
8986           trans_dists(i).ref_doc_curr_conv_rate 	:= l_po_header_curr_conv_rate;
8987           trans_dists(i).applied_to_doc_curr_conv_rate 	:= l_receipt_curr_conv_rate;
8988 
8989 	  l_price_diff					:= l_inv_dist_list(i).unit_price - l_po_unit_price;
8990 	  trans_dists(i).price_diff 			:= l_price_diff;
8991           trans_dists(i).historical_flag		:= NVL(l_inv_dist_list(i).historical_flag, 'N'); -- Bug 7117591
8992 /*NVL(P_Invoice_Header_Rec.historical_flag, 'N');*/
8993 
8994           IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
8995               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
8996 				'trans_dists(i).trx_id: '||trans_dists(i).trx_id);
8997               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
8998                                 'trans_dists(i).trx_line_id: '||trans_dists(i).trx_line_id);
8999               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
9000 				'trans_dists(i).trx_line_dist_amt: '||trans_dists(i).trx_line_dist_amt);
9001               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
9002 				'l_ref_doc_application_id: '||l_ref_doc_application_id);
9003               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
9004 				'l_ref_doc_entity_code: '   ||l_ref_doc_entity_code);
9005               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
9006 				'l_ref_doc_event_class_code: ' ||l_ref_doc_event_class_code);
9007               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
9008 				'l_ref_doc_trx_id: '||l_ref_doc_trx_id);
9009               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
9010 				'l_po_line_location_id: '||l_po_line_location_id);
9011               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
9012 				'l_inv_dist_list(i).po_distribution_id: '||l_inv_dist_list(i).po_distribution_id);
9013               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
9014 				'l_adj_doc_application_id: '|| l_adj_doc_application_id);
9015               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
9016 				'l_adj_doc_entity_code: '|| l_adj_doc_entity_code);
9017               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
9018 				'l_adj_doc_event_class_code: '|| l_adj_doc_event_class_code);
9019               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
9020 				'l_adj_doc_trx_id: '|| l_adj_doc_trx_id);
9021               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
9022 				'l_adj_doc_line_id: '|| l_adj_doc_line_id);
9023               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
9024 				'l_adj_doc_trx_level_type: '|| l_adj_doc_trx_level_type);
9025 	      FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
9026 				'l_prepay_doc_application_id: '||l_prepay_doc_application_id);
9027 	      FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
9028 				'l_prepay_doc_entity_code: '||l_prepay_doc_entity_code);
9029 	      FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
9030 				'l_prepay_doc_event_class_code: '||l_prepay_doc_event_class_code);
9031 	      FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
9032 				'l_applied_from_trx_id: '||l_applied_from_trx_id);
9033 	      FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
9034 				'l_applied_from_line_id: '||l_applied_from_line_id);
9035 	      FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
9036 				'l_inv_dist_list(i).prepay_distribution_id: '||l_inv_dist_list(i).prepay_distribution_id);
9037 	      FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
9038 				'l_applied_from_trx_level_type: '||l_applied_from_trx_level_type);
9039           END IF;
9040 
9041           -- Reset the derived values
9042           l_po_line_location_id := Null;
9043 	  l_prepay_inv_id	:= Null;
9044 	  l_prepay_line_num	:= Null;
9045         END IF;
9046 
9047       END LOOP;
9048 
9049       -------------------------------------------------------------------
9050       l_debug_info := 'Step 7: Bulk Insert into global temp table';
9051       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9052           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9053       END IF;
9054       -------------------------------------------------------------------
9055       IF (l_return_status = TRUE) THEN
9056 
9057         FORALL m IN trans_dists.FIRST..trans_dists.LAST
9058           INSERT INTO zx_itm_distributions_gt
9059           VALUES trans_dists(m);
9060 
9061       END IF;
9062     END IF;
9063 
9064     RETURN l_return_status;
9065 
9066   EXCEPTION
9067     WHEN OTHERS THEN
9068       IF (SQLCODE <> -20001) THEN
9069         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
9070         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
9071         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
9072         FND_MESSAGE.SET_TOKEN('PARAMETERS',
9073           ' P_Invoice_Header_Rec = '||P_Invoice_Header_Rec.Invoice_Id||
9074           ' P_Calling_Mode ='||P_Calling_Mode||
9075           ' P_Error_Code = '||P_Error_Code||
9076           ' P_Calling_Sequence = '||P_Calling_Sequence);
9077         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
9078       END IF;
9079 
9080       APP_EXCEPTION.RAISE_EXCEPTION;
9081   END Populate_Distributions_GT;
9082 
9083 /*=============================================================================
9084  |  FUNCTION - Update_AP()
9085  |
9086  |  DESCRIPTION
9087  |      This function will handle the return of values from the eTax repository
9088  |      This will be called from all the functions that call the etax services
9089  |      in the case the call is successfull.
9090  |
9091  |  PARAMETERS
9092  |      P_Invoice_header_rec - Invoice header info
9093  |      P_Calling_Mode - calling mode.
9094  |      P_All_Error_Messages - Should API return 1 error message or allow
9095  |                             calling point to get them from message stack
9096  |      P_error_code - Error code to be returned
9097  |      P_calling_sequence -  Calling sequence
9098  |
9099  |  MODIFICATION HISTORY
9100  |    DATE          Author         Action
9101  |    20-OCT-2003   SYIDNER        Created
9102  |
9103  *============================================================================*/
9104 
9105   FUNCTION Update_AP(
9106              P_Invoice_header_rec    IN ap_invoices_all%ROWTYPE,
9107              P_Calling_Mode          IN VARCHAR2,
9108              P_All_Error_Messages    IN VARCHAR2,
9109              P_Error_Code            OUT NOCOPY VARCHAR2,
9110              P_Calling_Sequence      IN VARCHAR2) RETURN BOOLEAN
9111   IS
9112 
9113     l_debug_info                 VARCHAR2(240);
9114     l_curr_calling_sequence      VARCHAR2(4000);
9115     l_return_status              BOOLEAN := TRUE;
9116 
9117   BEGIN
9118 
9119     l_curr_calling_sequence :=
9120       'AP_ETAX_SERVICES_PKG.Update_AP<-'||P_calling_sequence;
9121 
9122     ------------------------------------------------------------------
9123     l_debug_info := 'Step 1: Calling_Mode is:'||P_Calling_Mode;
9124     ------------------------------------------------------------------
9125 
9126     IF (P_Calling_Mode IN ('CALCULATE', 'CALCULATE IMPORT',
9127                            'OVERRIDE TAX', 'IMPORT INTERFACE',
9128                            'APPLY PREPAY', 'UNAPPLY PREPAY')) THEN
9129       -------------------------------------------------------------------
9130       l_debug_info := 'Step 2: Calling Return_Tax_Lines ';
9131       -------------------------------------------------------------------
9132       IF NOT (AP_ETAX_UTILITY_PKG.Return_Tax_Lines(
9133                 P_Invoice_Header_Rec => P_invoice_header_rec,
9134                 P_Error_Code         => P_Error_Code,
9135                 P_Calling_Sequence   => l_curr_calling_sequence)) THEN
9136 
9137         l_return_status :=  FALSE;
9138       END IF;
9139 
9140 
9141     ELSIF (P_Calling_Mode IN ('DISTRIBUTE', 'DISTRIBUTE RECOUP', 'OVERRIDE RECOVERY','DISTRIBUTE IMPORT')) THEN
9142         /*  for bug  6010950 added 'DISTRIBUTE IMPORT' to create the tax distributions while import itself for
9143               tax only invoices. */
9144       -------------------------------------------------------------------
9145       l_debug_info := 'Step 3: Calling Return_Tax_Distributions ';
9146       -------------------------------------------------------------------
9147       IF NOT (AP_ETAX_UTILITY_PKG.Return_Tax_Distributions(
9148                 P_Invoice_Header_Rec => P_invoice_header_rec,
9149                 P_All_Error_Messages => P_All_Error_Messages,
9150                 P_Error_Code         => P_Error_Code,
9151                 P_Calling_Sequence   => l_curr_calling_sequence)) THEN
9152 
9153         l_return_status := FALSE;
9154       END IF;
9155 
9156     ELSIF (P_Calling_Mode = 'CALCULATE QUOTE') THEN
9157       -------------------------------------------------------------------
9158       l_debug_info := 'Step 4: Calling Return_Tax_Quote ';
9159       -------------------------------------------------------------------
9160       IF NOT (AP_ETAX_UTILITY_PKG.Return_Tax_Quote(
9161                 P_Invoice_Header_Rec => P_invoice_header_rec,
9162                 P_Error_Code         => P_Error_Code,
9163                 P_Calling_Sequence   => l_curr_calling_sequence)) THEN
9164 
9165 
9166         l_return_status := FALSE;
9167       END IF;
9168 
9169     ELSIF (P_Calling_Mode = 'REVERSE INVOICE') THEN
9170       -------------------------------------------------------------------
9171       l_debug_info := 'Step 5: Calling Return_Tax_Lines for '||
9172                       'REVERSE INVOICE';
9173       -------------------------------------------------------------------
9174       IF NOT (AP_ETAX_UTILITY_PKG.Return_Tax_Lines(
9175                 P_Invoice_Header_Rec => P_invoice_header_rec,
9176                 P_Error_Code         => P_Error_Code,
9177                 P_Calling_Sequence   => l_curr_calling_sequence)) THEN
9178 
9179         l_return_status := FALSE;
9180       END IF;
9181 
9182       -------------------------------------------------------------------
9183       l_debug_info := 'Step 6: Calling Return_Tax_Distributions for '||
9184                       'REVERSE INVOICE';
9185       -------------------------------------------------------------------
9186       IF NOT (AP_ETAX_UTILITY_PKG.Return_Tax_Distributions(
9187                 P_Invoice_Header_Rec => P_invoice_header_rec,
9188                 P_All_Error_Messages => P_All_Error_Messages,
9189                 P_Error_Code         => P_Error_Code,
9190                 P_Calling_Sequence   => l_curr_calling_sequence)) THEN
9191 
9192         l_return_status := FALSE;
9193       END IF;
9194 
9195     END IF;
9196 
9197     RETURN l_return_status;
9198 
9199   EXCEPTION
9200     WHEN OTHERS THEN
9201       IF (SQLCODE <> -20001) THEN
9202         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
9203         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
9204         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
9205         FND_MESSAGE.SET_TOKEN('PARAMETERS',
9206           ' P_Invoice_Id = '||P_Invoice_header_rec.Invoice_Id||
9207           ' P_Calling_Mode ='||P_Calling_Mode||
9208           ' P_Error_Code = '||P_Error_Code||
9209           ' P_Calling_Sequence = '||P_Calling_Sequence);
9210         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
9211       END IF;
9212 
9213       APP_EXCEPTION.RAISE_EXCEPTION;
9214 
9215   END Update_AP;
9216 
9217 /*=============================================================================
9218  |  FUNCTION - Calculate_Quote ()
9219  |
9220  |  DESCRIPTION
9221  |      This function will return the tax amount and indicate if it is inclusive.
9222  |      This will be called from the recurring invoices form. This is a special
9223  |      case, as the invoices for which the tax is to be calculated are not yet
9224  |      saved to the database and eBTax global temporary tables are populated
9225  |      based on the parameter p_invoice_header_rec. A psuedo-line is inserted
9226  |      into the GTT and removed after the tax amount is calculated.
9227  |
9228  |  PARAMETERS
9229  |      P_Invoice_Header_Rec 	- Invoice header info
9230  |      P_Invoice_Lines_Rec	- Invoice lines info
9231  |      P_Calling_Mode 		- Calling mode. (CALCULATE_QUOTE)
9232  |      P_All_Error_Messages 	- Should API return 1 error message or allow
9233  |                                calling point to get them from message stack
9234  |      P_error_code 		- Error code to be returned
9235  |      P_calling_sequence 	- Calling sequence
9236  |
9237  |  MODIFICATION HISTORY
9238  |    DATE          Author         Action
9239  |    13-AUG-2004   Sanjay         Created
9240  *============================================================================*/
9241 
9242   FUNCTION CALCULATE_QUOTE(
9243              P_Invoice_Header_Rec      	IN  ap_invoices_all%ROWTYPE,
9244              P_Invoice_Lines_Rec	IN  ap_invoice_lines_all%ROWTYPE,
9245              P_Calling_Mode            	IN  VARCHAR2,
9246 	     P_Tax_Amount		OUT NOCOPY NUMBER,
9247 	     P_Tax_Amt_Included		OUT NOCOPY VARCHAR2,
9248              P_Error_Code              	OUT NOCOPY VARCHAR2,
9249              P_Calling_Sequence         IN  VARCHAR2) RETURN BOOLEAN
9250   IS
9251 
9252     l_debug_info                 	VARCHAR2(240);
9253     l_curr_calling_sequence      	VARCHAR2(4000);
9254 
9255     l_tax_already_calculated     	VARCHAR2(1);
9256 
9257     l_event_class_code           	zx_trx_headers_gt.event_class_code%TYPE;
9258     l_event_type_code            	zx_trx_headers_gt.event_type_code%TYPE;
9259 
9260     l_location_id			zx_transaction_lines_gt.ship_from_location_id%type;
9261     l_bill_to_location_id          	zx_transaction_lines_gt.bill_to_location_id%TYPE;
9262     l_fob_point                  	po_vendor_sites_all.fob_lookup_code%TYPE;
9263     l_po_line_location_id		ap_invoice_lines_all.po_line_location_id%TYPE;
9264 
9265     l_ctrl_hdr_tx_appl_flag         	zx_transaction_lines_gt.ctrl_hdr_tx_appl_flag%TYPE;
9266     l_line_level_action            	zx_transaction_lines_gt.line_level_action%TYPE;
9267     l_line_amt_includes_tax_flag	zx_transaction_lines_gt.line_amt_includes_tax_flag%TYPE;
9268     l_product_org_id               	zx_transaction_lines_gt.product_org_id%TYPE;
9269     l_uom_code                          mtl_units_of_measure.uom_code%TYPE;
9270 
9271     -- Variables for PO doc info
9272     l_ref_doc_application_id		zx_transaction_lines_gt.ref_doc_application_id%TYPE;
9273     l_ref_doc_entity_code		zx_transaction_lines_gt.ref_doc_entity_code%TYPE;
9274     l_ref_doc_event_class_code		zx_transaction_lines_gt.ref_doc_event_class_code%TYPE;
9275     l_ref_doc_line_quantity		zx_transaction_lines_gt.ref_doc_line_quantity%TYPE;
9276     l_ref_doc_trx_level_type		zx_transaction_lines_gt.ref_doc_trx_level_type%TYPE;
9277     l_po_header_curr_conv_rate		po_headers_all.rate%TYPE;
9278     l_dummy				number;
9279 
9280     l_ref_doc_trx_id			zx_transaction_lines_gt.ref_doc_trx_id%TYPE;
9281 
9282     l_return_status              	BOOLEAN := TRUE;
9283     l_return_status_service       	VARCHAR2(4000);
9284     l_msg_count                   	NUMBER;
9285     l_msg_data                    	VARCHAR2(4000);
9286     l_api_name              	        CONSTANT VARCHAR2(200) := 'Calculate_Quote';
9287 
9288     CURSOR c_tax_amount IS
9289     SELECT SUM(NVL(zdl.tax_amt,0))
9290     FROM   zx_detail_tax_lines_gt zdl
9291     WHERE  zdl.application_id = 200
9292     AND    zdl.entity_code    = 'AP_INVOICES'
9293     AND    zdl.trx_id         = P_Invoice_Lines_Rec.invoice_id
9294     AND    NVL(zdl.self_assessed_flag,  'N') = 'N'
9295     AND    NVL(zdl.reporting_only_flag, 'N') = 'N';
9296 
9297   BEGIN
9298 
9299     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.CALCULATE_QUOTE<-' || P_calling_sequence;
9300 
9301     IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
9302       FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_ETAX_SERVICES_PKG.Calculate_Quote (+)');
9303     END IF;
9304 
9305     ----------------------------------------------------------------------
9306     l_debug_info := 'Get location_id for vendor site';
9307     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9308       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9309     END IF;
9310     ----------------------------------------------------------------------
9311     BEGIN
9312       SELECT location_id, fob_lookup_code
9313         INTO l_location_id, l_fob_point
9314         FROM ap_supplier_sites_all
9315        WHERE vendor_site_id = P_Invoice_Header_Rec.vendor_site_id;
9316 
9317     EXCEPTION
9318       WHEN no_data_found THEN
9319          l_location_id := null;
9320          l_fob_point   := null;
9321     END;
9322 
9323     -------------------------------------------------------------------
9324     l_debug_info := 'Is tax already called invoice level?';
9325     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9326       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9327     END IF;
9328     -------------------------------------------------------------------
9329     IF (AP_ETAX_UTILITY_PKG.Is_Tax_Already_Calc_Inv(
9330           P_Invoice_Id           => p_invoice_header_rec.invoice_id,
9331           P_Calling_Sequence     => l_curr_calling_sequence)) THEN
9332 
9333       l_tax_already_calculated := 'Y';
9334     ELSE
9335       l_tax_already_calculated := 'N';
9336 
9337     END IF;
9338 
9339     -------------------------------------------------------------------------
9340     l_debug_info := 'Step 1: Call AP_ETAX_SERVICES_PKG.Populate_Headers_GT';
9341     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9342         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9343     END IF;
9344     -------------------------------------------------------------------------
9345     IF NOT(AP_ETAX_SERVICES_PKG.Populate_Headers_GT(
9346 		      P_Invoice_Header_Rec         => P_Invoice_Header_Rec,
9347 		      P_Calling_Mode               => P_Calling_Mode,
9348 		      P_eTax_Already_called_flag   => l_tax_already_calculated,
9349 		      P_Event_Class_Code           => l_event_class_code,
9350 		      P_Event_Type_Code            => l_event_type_code,
9351 		      P_Error_Code                 => P_error_code,
9352 		      P_Calling_Sequence           => l_curr_calling_sequence )) THEN
9353 
9354       l_return_status := FALSE;
9355 
9356     END IF;
9357 
9358     ------------------------------------------------------------------------
9359     l_debug_info := 'Step 2: Get location_id for org_id';
9360     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9361         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9362     END IF;
9363     ------------------------------------------------------------------------
9364     BEGIN
9365       SELECT location_id
9366         INTO l_bill_to_location_id
9367         FROM hr_all_organization_units
9368        WHERE organization_id = P_Invoice_Header_Rec.org_id;
9369 
9370     EXCEPTION
9371       WHEN no_data_found THEN
9372          l_bill_to_location_id := null;
9373     END;
9374 
9375     ------------------------------------------------------------------------
9376     l_debug_info := 'Step 4: Go through taxable lines';
9377     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9378         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9379     END IF;
9380     ------------------------------------------------------------------------
9381     IF ( P_Invoice_Lines_Rec.invoice_id IS NOT NULL ) THEN
9382 
9383         --------------------------------------------------------------------
9384          l_debug_info := 'Step 5: Get line_level_action for line number: '||
9385 			  P_Invoice_Lines_Rec.line_number;
9386          IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9387            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9388          END IF;
9389         --------------------------------------------------------------------
9390         IF (l_return_status = TRUE) THEN
9391 
9392          l_line_level_action := 'CREATE';
9393 
9394          -------------------------------------------------------------------
9395           l_debug_info := 'Step 6: Get Additional PO matched  info ';
9396           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9397              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9398           END IF;
9399          -------------------------------------------------------------------
9400 
9401          IF ( P_Invoice_Lines_Rec.po_line_location_id IS NOT NULL) THEN
9402 
9403 	   l_po_line_location_id := P_Invoice_Lines_Rec.po_line_location_id;
9404 
9405            IF NOT (AP_ETAX_UTILITY_PKG.Get_PO_Info(
9406               P_Po_Line_Location_Id         => l_po_line_location_id,
9407               P_PO_Distribution_Id          => null,
9408               P_Application_Id              => l_ref_doc_application_id,
9409               P_Entity_code                 => l_ref_doc_entity_code,
9410               P_Event_Class_Code            => l_ref_doc_event_class_code,
9411               P_PO_Quantity                 => l_ref_doc_line_quantity,
9412               P_Product_Org_Id              => l_product_org_id,
9413               P_Po_Header_Id                => l_ref_doc_trx_id,
9414               P_Po_Header_curr_conv_rate    => l_po_header_curr_conv_rate,
9415               P_Uom_Code                    => l_uom_code,
9416               P_Dist_Qty                    => l_dummy,
9417               P_Ship_Price                  => l_dummy,
9418               P_Error_Code                  => P_error_code,
9419               P_Calling_Sequence            => l_curr_calling_sequence)) THEN
9420 
9421              l_return_status := FALSE;
9422            END IF;
9423 
9424 	   l_ref_doc_trx_level_type := 'SHIPMENT';
9425 
9426          END IF;
9427 
9428          -------------------------------------------------------------------
9429           l_debug_info := 'Step 7: Get line_amt_includes_tax_flag';
9430 	  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9431              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9432           END IF;
9433          -------------------------------------------------------------------
9434 
9435          IF (P_Invoice_Lines_Rec.po_line_location_id IS NOT NULL) THEN
9436            -- NONE
9437            l_line_amt_includes_tax_flag := 'N';
9438 
9439          ELSE
9440            IF (p_calling_mode = 'CALCULATE QUOTE') THEN
9441 
9442 	     -- Refer eTax bug 3819487 for the value of line_amt_includes_tax_flag
9443 	     -- that AP should be passing.
9444 
9445              l_line_amt_includes_tax_flag := 'I';
9446 
9447            ELSE
9448              -- STANDARD
9449              l_line_amt_includes_tax_flag := 'S';
9450 
9451            END IF;
9452          END IF;
9453 
9454          -------------------------------------------------------------------
9455           l_debug_info := 'Step 8: Get ctrl_hdr_tx_appl_flag';
9456 	  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9457             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9458           END IF;
9459          -------------------------------------------------------------------
9460          IF P_Invoice_Header_Rec.control_amount IS NOT NULL THEN
9461            l_ctrl_hdr_tx_appl_flag := 'Y';
9462          ELSE
9463            l_ctrl_hdr_tx_appl_flag := 'N';
9464          END IF;
9465 
9466        END IF;
9467     END IF;
9468 
9469     ------------------------------------------------------------------------
9470     l_debug_info := 'Step 9: Insert into zx_transaction_lines_gt';
9471     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9472         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9473     END IF;
9474     ------------------------------------------------------------------------
9475     IF ( l_return_status = TRUE ) THEN
9476 
9477 	INSERT INTO zx_transaction_lines_gt
9478 	(
9479 	 application_id,
9480 	 entity_code,
9481 	 event_class_code,
9482 	 trx_id,
9483 	 trx_level_type,
9484 	 trx_line_id,
9485 	 line_level_action,
9486 	 trx_line_type,
9487 	 trx_line_date,
9488 	 trx_business_category,
9489 	 line_intended_use,
9490 	 user_defined_fisc_class,
9491 	 line_amt,
9492 	 trx_line_quantity,
9493 	 unit_price,
9494 	 product_id,
9495 	 product_fisc_classification,
9496 	 product_org_id,
9497 	 uom_code,
9498 	 product_type,
9499 	 product_category,
9500 	 fob_point,
9501 	 ship_to_party_id,
9502 	 ship_from_party_id,
9503 	 bill_to_party_id,
9504 	 bill_from_party_id,
9505 	 ship_from_party_site_id,
9506 	 bill_from_party_site_id,
9507 	 ship_to_location_id,
9508          ship_from_location_id,
9509 	 bill_to_location_id,
9510 	 bill_from_location_id,
9511 	 account_ccid,
9512 	 merchant_party_country,
9513 	 ref_doc_application_id,
9514 	 ref_doc_entity_code,
9515 	 ref_doc_event_class_code,
9516 	 ref_doc_trx_id,
9517 	 ref_doc_line_id,
9518 	 ref_doc_line_quantity,
9519          ref_doc_trx_level_type,
9520 	 --applied_to_trx_line_id,
9521 	 trx_line_number,
9522 	 trx_line_description,
9523 	 product_description,
9524 	 trx_line_gl_date,
9525 	 merchant_party_name,
9526 	 merchant_party_document_number,
9527 	 merchant_party_reference,
9528 	 merchant_party_taxpayer_id,
9529 	 merchant_party_tax_reg_number,
9530 	 assessable_value,
9531 	 line_amt_includes_tax_flag,
9532 	 historical_flag,
9533 	 ctrl_hdr_tx_appl_flag,
9534 	 ctrl_total_line_tx_amt,
9535 	 input_tax_classification_code
9536 	)
9537 	VALUES
9538 	(
9539 	 200,
9540 	 'AP_INVOICES',
9541 	 l_event_class_code,
9542 	 P_Invoice_Lines_Rec.invoice_id,
9543 	 'LINE',
9544 	 P_Invoice_Lines_Rec.line_number,
9545 	 l_line_level_action,
9546 	 P_Invoice_Lines_Rec.line_type_lookup_code,
9547 	 P_Invoice_Header_Rec.invoice_date,
9548 	 P_Invoice_Lines_Rec.trx_business_category,
9549 	 P_Invoice_Lines_Rec.primary_intended_use,
9550 	 P_Invoice_Lines_Rec.user_defined_fisc_class,
9551 	 P_Invoice_Lines_Rec.amount,
9552 	 P_Invoice_Lines_Rec.quantity_invoiced,
9553 	 P_Invoice_Lines_Rec.unit_price,
9554 	 P_Invoice_Lines_Rec.inventory_item_id,
9555 	 P_Invoice_Lines_Rec.product_fisc_classification,
9556 	 l_product_org_id,
9557 	 P_Invoice_Lines_Rec.unit_meas_lookup_code,
9558 	 P_Invoice_Lines_Rec.product_type,
9559 	 P_Invoice_Lines_Rec.product_category,
9560 	 l_fob_point,
9561 	 P_Invoice_Lines_Rec.org_id,
9562 	 P_Invoice_Header_Rec.party_id,
9563 	 P_Invoice_Lines_Rec.org_id,
9564 	 P_Invoice_Header_Rec.party_id,
9565 	 P_Invoice_Header_Rec.party_site_id,
9566 	 P_Invoice_Header_Rec.party_site_id,
9567 	 P_Invoice_Lines_Rec.ship_to_location_id,
9568          l_location_id,
9569 	 l_bill_to_location_id,
9570          l_location_id,
9571 	 P_Invoice_Lines_Rec.default_dist_ccid,
9572 	 P_Invoice_Lines_Rec.country_of_supply,
9573 	 l_ref_doc_application_id,
9574 	 l_ref_doc_entity_code,
9575 	 l_ref_doc_event_class_code,
9576 	 l_ref_doc_trx_id,
9577          --Bug5680407 corrected the wrong ordering of below
9578          --3 coulmns
9579 	 P_Invoice_Lines_Rec.po_line_location_id,
9580 	 l_ref_doc_line_quantity,
9581          l_ref_doc_trx_level_type,
9582 	 P_Invoice_Lines_Rec.line_number,
9583 	 P_Invoice_Lines_Rec.description,
9584 	 P_Invoice_Lines_Rec.item_description,
9585 	 P_Invoice_Lines_Rec.accounting_date,
9586 	 P_Invoice_Lines_Rec.merchant_name,
9587 	 P_Invoice_Lines_Rec.merchant_document_number,
9588 	 P_Invoice_Lines_Rec.merchant_reference,
9589 	 P_Invoice_Lines_Rec.merchant_taxpayer_id,
9590 	 P_Invoice_Lines_Rec.merchant_tax_reg_number,
9591 	 P_Invoice_Lines_Rec.assessable_value,
9592 	 l_line_amt_includes_tax_flag,
9593 	 NVL(P_Invoice_Header_Rec.historical_flag, 'N'),
9594 	 l_ctrl_hdr_tx_appl_flag,
9595 	 P_Invoice_Lines_Rec.control_amount,
9596 	 P_Invoice_Lines_Rec.tax_classification_code
9597 	);
9598 
9599     END IF;
9600 
9601     IF ( l_return_status = TRUE ) THEN
9602 
9603        -----------------------------------------------------------------
9604        l_debug_info := 'Step 10: Call Calculate_Tax service';
9605        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9606         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9607        END IF;
9608        -----------------------------------------------------------------
9609 
9610        zx_api_pub.calculate_tax(
9611                  p_api_version      => 1.0,
9612                  p_init_msg_list    => FND_API.G_TRUE,
9613                  p_commit           => FND_API.G_FALSE,
9614                  p_validation_level => FND_API.G_VALID_LEVEL_FULL,
9615                  x_return_status    => l_return_status_service,
9616                  x_msg_count        => l_msg_count,
9617                  x_msg_data         => l_msg_data);
9618 
9619     END IF;
9620 
9621     IF (l_return_status_service = 'S') THEN
9622 
9623        OPEN  c_tax_amount;
9624        FETCH c_tax_amount
9625        INTO  p_tax_amount;
9626        CLOSE c_tax_amount;
9627 
9628        IF p_tax_amount IS NOT NULL THEN
9629 
9630            -----------------------------------------------------------------
9631            l_debug_info := 'Step 11: Get tax inclusive/exclusive flag';
9632            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9633               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9634            END IF;
9635            -----------------------------------------------------------------
9636 
9637 	   SELECT distinct zdl.tax_amt_included_flag
9638            INTO   P_Tax_Amt_Included
9639            FROM   zx_detail_tax_lines_gt zdl
9640            WHERE  zdl.application_id	= 200
9641            AND    zdl.entity_code	= 'AP_INVOICES'
9642            AND    zdl.trx_id 		= P_Invoice_Lines_Rec.invoice_id
9643            AND    NVL(zdl.self_assessed_flag,  'N') = 'N'
9644            AND    NVL(zdl.reporting_only_flag, 'N') = 'N';
9645 
9646 	END IF;
9647 
9648     ELSE  -- handle errors
9649 
9650        -----------------------------------------------------------------
9651        l_debug_info := 'Step 12: Calculate_Tax service returns error';
9652        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9653          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9654        END IF;
9655        -----------------------------------------------------------------
9656 
9657       l_return_status := FALSE;
9658 
9659       IF l_msg_count = 1 THEN
9660 
9661          P_Error_Code := FND_MSG_PUB.Get;
9662 
9663       ELSIF l_msg_count > 1 THEN
9664          LOOP
9665              P_Error_Code := FND_MSG_PUB.Get;
9666 
9667              IF P_Error_Code IS NULL THEN
9668 	        EXIT;
9669 	     END IF;
9670 
9671          END LOOP;
9672       END IF;
9673 
9674     END IF;
9675 
9676     -----------------------------------------------------------------
9677     l_debug_info := 'Step 13: Delete eTax Global Temporary Tables';
9678     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9679         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9680     END IF;
9681     -----------------------------------------------------------------
9682     BEGIN DELETE zx_trx_headers_gt;
9683     EXCEPTION WHEN NO_DATA_FOUND THEN null;
9684     END;
9685 
9686     BEGIN DELETE zx_transaction_lines_gt;
9687     EXCEPTION WHEN NO_DATA_FOUND THEN null;
9688     END;
9689 
9690     RETURN l_return_status;
9691 
9692   EXCEPTION
9693     WHEN OTHERS THEN
9694       IF (SQLCODE <> -20001) THEN
9695 
9696         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
9697         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
9698         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
9699         FND_MESSAGE.SET_TOKEN('PARAMETERS',
9700           ' P_Invoice_Header_Rec.Invoice_Id = '||P_Invoice_Header_Rec.Invoice_Id||
9701           ' P_Calling_Mode ='||P_Calling_Mode||
9702           ' P_Error_Code = '||P_Error_Code);
9703         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
9704       END IF;
9705 
9706       APP_EXCEPTION.RAISE_EXCEPTION;
9707 
9708   END CALCULATE_QUOTE;
9709 
9710 PROCEDURE get_po_tax_attributes
9711 			(
9712 			 p_application_id		IN  NUMBER,
9713 			 p_org_id			IN  NUMBER,
9714 			 p_entity_code			IN  VARCHAR2,
9715 			 p_event_class_code		IN  VARCHAR2,
9716 			 p_trx_level_type		IN  VARCHAR2,
9717 			 p_trx_id			IN  NUMBER,
9718 			 p_trx_line_id			IN  NUMBER,
9719 			 x_line_intended_use		OUT NOCOPY VARCHAR2,
9720 			 x_product_type			OUT NOCOPY VARCHAR2,
9721 			 x_product_category		OUT NOCOPY VARCHAR2,
9722 			 x_product_fisc_classification	OUT NOCOPY VARCHAR2,
9723 			 x_user_defined_fisc_class	OUT NOCOPY VARCHAR2,
9724 			 x_assessable_value		OUT NOCOPY NUMBER,
9725 			 x_tax_classification_code	OUT NOCOPY VARCHAR2
9726 			) IS
9727 
9728 	CURSOR  c_po_default IS
9729         SELECT  line_intended_use,
9730                 product_type,
9731                 product_category,
9732                 product_fisc_classification,
9733                 user_defined_fisc_class,
9734                 assessable_value,
9735 		input_tax_classification_code
9736         FROM	zx_lines_det_factors
9737         WHERE	application_id           = p_application_id
9738         AND	internal_organization_id = p_org_id
9739         AND	entity_code              = p_entity_code
9740         AND 	event_class_code         = p_event_class_code
9741         AND	trx_level_type           = p_trx_level_type
9742         AND	trx_id                   = p_trx_id
9743         AND	trx_line_id              = p_trx_line_id;
9744 
9745 	l_po_default	c_po_default%rowtype;
9746 
9747 BEGIN
9748 
9749      Open  c_po_default;
9750      Fetch c_po_default
9751      Into  l_po_default;
9752      Close c_po_default;
9753 
9754      x_line_intended_use		:= l_po_default.line_intended_use;
9755      x_product_type			:= l_po_default.product_type;
9756      x_product_category			:= l_po_default.product_category;
9757      x_product_fisc_classification	:= l_po_default.product_fisc_classification;
9758      x_user_defined_fisc_class		:= l_po_default.user_defined_fisc_class;
9759      x_assessable_value			:= l_po_default.assessable_value;
9760      x_tax_classification_code		:= l_po_default.input_tax_classification_code;
9761 
9762      -- Need to call on-the-fly po upgrade api when no rows are returned.
9763 
9764 EXCEPTION
9765       WHEN OTHERS THEN
9766            Null;
9767 
9768 END get_po_tax_attributes;
9769 
9770 FUNCTION CANCEL_INVOICE
9771 		(P_Invoice_Id   IN NUMBER,
9772 		 P_Line_Number  IN NUMBER DEFAULT NULL,
9773                  P_Calling_Mode IN VARCHAR2) RETURN BOOLEAN IS
9774 
9775     l_debug_info		VARCHAR2(240);
9776     l_curr_calling_sequence	VARCHAR2(4000);
9777     l_api_name			CONSTANT VARCHAR2(100) := 'Cancel_Invoice';
9778 
9779     l_return_status              	BOOLEAN := TRUE;
9780     l_self_assess_tax_dist_exist        BOOLEAN := FALSE ; -- Bug 6694536
9781     l_tax_distributions_exist           BOOLEAN := FALSE ; -- Bug 6694536
9782     l_return_status_service       	VARCHAR2(4000);
9783     l_msg_count                   	NUMBER;
9784     l_msg_data                    	VARCHAR2(4000);
9785 
9786     cursor c_reverse_tax_dist is
9787 	  select
9788             zx_dist.gl_date		                accounting_date,
9789             'N'						accrual_posted_flag,
9790             'U'						assets_addition_flag,
9791             'N'						assets_tracking_flag,
9792             'N'						cash_posted_flag,
9793             AP_INVOICE_LINES_PKG.get_max_dist_line_num(
9794 		              p_invoice_id,
9795 		              tax_dist.invoice_line_number)+1
9796 							distribution_line_number,
9797             tax_dist.dist_code_combination_id		dist_code_combination_id,
9798             tax_dist.invoice_id				invoice_id,
9799             l_user_id					last_updated_by,
9800             l_sysdate					last_update_date,
9801             tax_dist.line_type_lookup_code		line_type_lookup_code,
9802             ap_utilities_pkg.get_gl_period_name(
9803                               zx_dist.gl_date,
9804                               tax_dist.org_id)          period_name,
9805             tax_dist.set_of_books_id			set_of_books_id,
9806             (-tax_dist.amount)				amount,
9807             (-tax_dist.base_amount)			base_amount,
9808             --P_Invoice_Header_Rec.batch_id		batch_id,
9809             l_user_id					created_by,
9810             l_sysdate					creation_date,
9811             tax_dist.description			description,
9812             NULL					final_match_flag,
9813             tax_dist.income_tax_region			income_tax_region,
9814             l_user_id					last_update_login,
9815             NULL					match_status_flag,
9816             'N'						posted_flag,
9817             tax_dist.po_distribution_id			po_distribution_id,
9818             NULL					program_application_id,
9819             NULL					program_id,
9820             NULL					program_update_date,
9821             NULL					quantity_invoiced,
9822             NULL					request_id,
9823             'Y'						reversal_flag,
9824             tax_dist.type_1099				type_1099,
9825             tax_dist.unit_price				unit_price,
9826             'N'						encumbered_flag,
9827             NULL					stat_amount,
9828             tax_dist.attribute1				attribute1,
9829             tax_dist.attribute10			attribute10,
9830             tax_dist.attribute11			attribute11,
9831             tax_dist.attribute12			attribute12,
9832             tax_dist.attribute13			attribute13,
9833             tax_dist.attribute14			attribute14,
9834             tax_dist.attribute15			attribute15,
9835             tax_dist.attribute2				attribute2,
9836             tax_dist.attribute3				attribute3,
9837             tax_dist.attribute4				attribute4,
9838             tax_dist.attribute5				attribute5,
9839             tax_dist.attribute6				attribute6,
9840             tax_dist.attribute7				attribute7,
9841             tax_dist.attribute8				attribute8,
9842             tax_dist.attribute9				attribute9,
9843             tax_dist.attribute_category			attribute_category,
9844             decode(zx_dist.recoverable_flag,
9845                    'Y', NULL,
9846 		   item_dist.expenditure_item_date)	expenditure_item_date,
9847             decode(zx_dist.recoverable_flag,
9848                    'Y', NULL,
9849                    item_dist.expenditure_organization_id)  expenditure_organization_id,
9850             decode(zx_dist.recoverable_flag,
9851                    'Y', NULL,
9852             	   item_dist.expenditure_type)		expenditure_type,
9853             tax_dist.parent_invoice_id			parent_invoice_id,
9854             decode(zx_dist.recoverable_flag,
9855 		   'Y', 'E',
9856 		   item_dist.pa_addition_flag)		pa_addition_flag,
9857             decode(zx_dist.recoverable_flag,
9858                    'Y', NULL,
9859                    item_dist.pa_quantity)		pa_quantity,
9860             NULL					prepay_amount_remaining,
9861             decode(zx_dist.recoverable_flag,
9862                    'Y', NULL,
9863                    item_dist.project_accounting_context) project_accounting_context,
9864             decode(zx_dist.recoverable_flag,
9865                    'Y', NULL,
9866                    item_dist.project_id)		project_id,
9867             decode(zx_dist.recoverable_flag,
9868                    'Y', NULL,
9869                    item_dist.task_id)			task_id,
9870             NULL					packet_id,
9871             'N'						awt_flag,
9872             tax_dist.awt_group_id			awt_group_id,
9873             NULL					awt_tax_rate_id,
9874             NULL					awt_gross_amount,
9875             NULL					awt_invoice_id,
9876             NULL					awt_origin_group_id,
9877             NULL					reference_1,
9878             NULL					reference_2,
9879             tax_dist.org_id				org_id,
9880             NULL					awt_invoice_payment_id,
9881             tax_dist.global_attribute_category		global_attribute_category,
9882             tax_dist.global_attribute1			global_attribute1,
9883             tax_dist.global_attribute2			global_attribute2,
9884             tax_dist.global_attribute3			global_attribute3,
9885             tax_dist.global_attribute4			global_attribute4,
9886             tax_dist.global_attribute5			global_attribute5,
9887             tax_dist.global_attribute6			global_attribute6,
9888             tax_dist.global_attribute7			global_attribute7,
9889             tax_dist.global_attribute8			global_attribute8,
9890             tax_dist.global_attribute9			global_attribute9,
9891             tax_dist.global_attribute10			global_attribute10,
9892             tax_dist.global_attribute11			global_attribute11,
9893             tax_dist.global_attribute12			global_attribute12,
9894             tax_dist.global_attribute13			global_attribute13,
9895             tax_dist.global_attribute14			global_attribute14,
9896             tax_dist.global_attribute15			global_attribute15,
9897             tax_dist.global_attribute16			global_attribute16,
9898             tax_dist.global_attribute17			global_attribute17,
9899             tax_dist.global_attribute18			global_attribute18,
9900             tax_dist.global_attribute19			global_attribute19,
9901             tax_dist.global_attribute20			global_attribute20,
9902             NULL                                 	receipt_verified_flag,
9903             NULL                                 	receipt_required_flag,
9904             NULL                                 	receipt_missing_flag,
9905             NULL                                 	justification,
9906             NULL                                 	expense_group,
9907             NULL                                 	start_expense_date,
9908             NULL                                 	end_expense_date,
9909             NULL                                 	receipt_currency_code,
9910             NULL                                 	receipt_conversion_rate,
9911             NULL                                 	receipt_currency_amount,
9912             NULL                                 	daily_amount,
9913             NULL                                 	web_parameter_id,
9914             NULL                                 	adjustment_reason,
9915             decode(zx_dist.recoverable_flag,
9916                    'Y', NULL,
9917                    tax_dist.award_id)          		award_id,
9918             NULL                        		credit_card_trx_id,
9919             tax_dist.dist_match_type    		dist_match_type,
9920             tax_dist.rcv_transaction_id 		rcv_transaction_id,
9921             ap_invoice_distributions_s.NEXTVAL   	invoice_distribution_id,
9922             tax_dist.invoice_distribution_id     	parent_reversal_id,
9923             tax_dist.tax_recoverable_flag        	tax_recoverable_flag,
9924             NULL                                 	merchant_document_number,
9925             NULL                                 	merchant_name,
9926             NULL                                 	merchant_reference,
9927             NULL                                 	merchant_tax_reg_number,
9928             NULL                                 	merchant_taxpayer_id,
9929             NULL                                 	country_of_supply,
9930             NULL                                 	matched_uom_lookup_code,
9931             NULL                                 	gms_burdenable_raw_cost,
9932             NULL                                 	accounting_event_id,
9933             tax_dist.prepay_distribution_id  	  	prepay_distribution_id,
9934             NULL                                 	upgrade_posted_amt,
9935             NULL                                 	upgrade_base_posted_amt,
9936             'N'                                  	inventory_transfer_status,
9937             NULL                                 	company_prepaid_invoice_id,
9938             NULL                                 	cc_reversal_flag,
9939             NULL                                  	awt_withheld_amt,
9940             NULL                                  	pa_cmt_xface_flag,
9941             decode(p_calling_mode,
9942 			'CANCEL', 'Y', Null)  	  	cancellation_flag,
9943             tax_dist.invoice_line_number	  	invoice_line_number,
9944             tax_dist.corrected_invoice_dist_id		corrected_invoice_dist_id,
9945             tax_dist.rounding_amt       	  	rounding_amt,
9946             zx_dist.trx_line_dist_id			charge_applicable_to_dist_id,
9947             NULL					corrected_quantity,
9948             -- bug 5572121
9949             -- NULL                                  	related_id,
9950             DECODE( tax_dist.related_id, NULL, NULL,
9951                     tax_dist.invoice_distribution_id,
9952                      ap_invoice_distributions_s.CURRVAL, NULL) related_id,
9953 
9954             NULL                                  	asset_book_type_code,
9955             NULL                                  	asset_category_id,
9956             tax_dist.distribution_class 	  	distribution_class,
9957             tax_dist.tax_code_id        	  	tax_code_id,
9958             tax_dist.intended_use                 	intended_use,
9959             zx_dist.rec_nrec_tax_dist_id 	  	detail_tax_dist_id,
9960             zx_dist.rec_nrec_rate     	  	  	rec_nrec_rate,
9961             zx_dist.recovery_rate_id  	  	  	recovery_rate_id,
9962             zx_dist.recovery_type_code	  	  	recovery_type_code,
9963             NULL                                  	withholding_tax_code_id,
9964             NULL			     	  	taxable_amount,
9965             NULL			 	  	taxable_base_amount,
9966             tax_dist.tax_already_distributed_flag	tax_already_distributed_flag,
9967             tax_dist.summary_tax_line_id 	  	summary_tax_line_id,
9968 	        'N'				      	  	rcv_charge_addition_flag,
9969             tax_dist.prepay_tax_diff_amount -- BUG 7338249
9970 	from	ap_invoice_distributions_all 	tax_dist,
9971 		ap_invoice_distributions_all	item_dist,
9972 		zx_rec_nrec_dist		zx_dist
9973 	where	tax_dist.invoice_id		  = p_invoice_id
9974 	and	tax_dist.line_type_lookup_code    IN ('NONREC_TAX', 'REC_TAX', 'TIPV', 'TERV', 'TRV')
9975 	and	tax_dist.detail_tax_dist_id	  = zx_dist.reversed_tax_dist_id
9976 	and	item_dist.invoice_distribution_id(+) = zx_dist.trx_line_dist_id  --bug7394712
9977 	and	zx_dist.reverse_flag 		  = 'Y'
9978         --and     (p_line_number IS NULL -- bug 6056777
9979          --       or zx_dist.trx_line_id = p_line_number) --bug605677
9980 	--bugfix:5582836
9981         and     not exists(select detail_tax_dist_id
9982 			   from ap_invoice_distributions aid
9983 			   where aid.invoice_id = p_invoice_id
9984 			   and aid.detail_tax_dist_id = zx_dist.rec_nrec_tax_dist_id);
9985 
9986 
9987 /*
9988 Adding a new cursor for bug6056777, IF the parameter p_line_number is null then c_reverse_tax_dist would be opened
9989 Else c_reverse_tax_dist_1 would be opened.
9990 */
9991 
9992     cursor c_reverse_tax_dist_1 is
9993 	  select
9994             zx_dist.gl_date                             accounting_date,
9995             'N'						accrual_posted_flag,
9996             'U'						assets_addition_flag,
9997             'N'						assets_tracking_flag,
9998             'N'						cash_posted_flag,
9999             AP_INVOICE_LINES_PKG.get_max_dist_line_num(
10000 		              p_invoice_id,
10001 		              tax_dist.invoice_line_number)+1
10002 							distribution_line_number,
10003             tax_dist.dist_code_combination_id		dist_code_combination_id,
10004             tax_dist.invoice_id				invoice_id,
10005             l_user_id					last_updated_by,
10006             l_sysdate					last_update_date,
10007             tax_dist.line_type_lookup_code		line_type_lookup_code,
10008             ap_utilities_pkg.get_gl_period_name(
10009                               zx_dist.gl_date,
10010                               tax_dist.org_id)          period_name,
10011             tax_dist.set_of_books_id			set_of_books_id,
10012             (-tax_dist.amount)				amount,
10013             (-tax_dist.base_amount)			base_amount,
10014             --P_Invoice_Header_Rec.batch_id		batch_id,
10015             l_user_id					created_by,
10016             l_sysdate					creation_date,
10017             tax_dist.description			description,
10018             NULL					final_match_flag,
10019             tax_dist.income_tax_region			income_tax_region,
10020             l_user_id					last_update_login,
10021             NULL					match_status_flag,
10022             'N'						posted_flag,
10023             tax_dist.po_distribution_id			po_distribution_id,
10024             NULL					program_application_id,
10025             NULL					program_id,
10026             NULL					program_update_date,
10027             NULL					quantity_invoiced,
10028             NULL					request_id,
10029             'Y'						reversal_flag,
10030             tax_dist.type_1099				type_1099,
10031             tax_dist.unit_price				unit_price,
10032             'N'						encumbered_flag,
10033             NULL					stat_amount,
10034             tax_dist.attribute1				attribute1,
10035             tax_dist.attribute10			attribute10,
10036             tax_dist.attribute11			attribute11,
10037             tax_dist.attribute12			attribute12,
10038             tax_dist.attribute13			attribute13,
10039             tax_dist.attribute14			attribute14,
10040             tax_dist.attribute15			attribute15,
10041             tax_dist.attribute2				attribute2,
10042             tax_dist.attribute3				attribute3,
10043             tax_dist.attribute4				attribute4,
10044             tax_dist.attribute5				attribute5,
10045             tax_dist.attribute6				attribute6,
10046             tax_dist.attribute7				attribute7,
10047             tax_dist.attribute8				attribute8,
10048             tax_dist.attribute9				attribute9,
10049             tax_dist.attribute_category			attribute_category,
10050             decode(zx_dist.recoverable_flag,
10051                    'Y', NULL,
10052 		   item_dist.expenditure_item_date)	expenditure_item_date,
10053             decode(zx_dist.recoverable_flag,
10054                    'Y', NULL,
10055                    item_dist.expenditure_organization_id)  expenditure_organization_id,
10056             decode(zx_dist.recoverable_flag,
10057                    'Y', NULL,
10058             	   item_dist.expenditure_type)		expenditure_type,
10059             tax_dist.parent_invoice_id			parent_invoice_id,
10060             decode(zx_dist.recoverable_flag,
10061 		   'Y', 'E',
10062 		   item_dist.pa_addition_flag)		pa_addition_flag,
10063             decode(zx_dist.recoverable_flag,
10064                    'Y', NULL,
10065                    item_dist.pa_quantity)		pa_quantity,
10066             NULL					prepay_amount_remaining,
10067             decode(zx_dist.recoverable_flag,
10068                    'Y', NULL,
10069                    item_dist.project_accounting_context) project_accounting_context,
10070             decode(zx_dist.recoverable_flag,
10071                    'Y', NULL,
10072                    item_dist.project_id)		project_id,
10073             decode(zx_dist.recoverable_flag,
10074                    'Y', NULL,
10075                    item_dist.task_id)			task_id,
10076             NULL					packet_id,
10077             'N'						awt_flag,
10078             tax_dist.awt_group_id			awt_group_id,
10079             NULL					awt_tax_rate_id,
10080             NULL					awt_gross_amount,
10081             NULL					awt_invoice_id,
10082             NULL					awt_origin_group_id,
10083             NULL					reference_1,
10084             NULL					reference_2,
10085             tax_dist.org_id				org_id,
10086             NULL					awt_invoice_payment_id,
10087             tax_dist.global_attribute_category		global_attribute_category,
10088             tax_dist.global_attribute1			global_attribute1,
10089             tax_dist.global_attribute2			global_attribute2,
10090             tax_dist.global_attribute3			global_attribute3,
10091             tax_dist.global_attribute4			global_attribute4,
10092             tax_dist.global_attribute5			global_attribute5,
10093             tax_dist.global_attribute6			global_attribute6,
10094             tax_dist.global_attribute7			global_attribute7,
10095             tax_dist.global_attribute8			global_attribute8,
10096             tax_dist.global_attribute9			global_attribute9,
10097             tax_dist.global_attribute10			global_attribute10,
10098             tax_dist.global_attribute11			global_attribute11,
10099             tax_dist.global_attribute12			global_attribute12,
10100             tax_dist.global_attribute13			global_attribute13,
10101             tax_dist.global_attribute14			global_attribute14,
10102             tax_dist.global_attribute15			global_attribute15,
10103             tax_dist.global_attribute16			global_attribute16,
10104             tax_dist.global_attribute17			global_attribute17,
10105             tax_dist.global_attribute18			global_attribute18,
10106             tax_dist.global_attribute19			global_attribute19,
10107             tax_dist.global_attribute20			global_attribute20,
10108             NULL                                 	receipt_verified_flag,
10109             NULL                                 	receipt_required_flag,
10110             NULL                                 	receipt_missing_flag,
10111             NULL                                 	justification,
10112             NULL                                 	expense_group,
10113             NULL                                 	start_expense_date,
10114             NULL                                 	end_expense_date,
10115             NULL                                 	receipt_currency_code,
10116             NULL                                 	receipt_conversion_rate,
10117             NULL                                 	receipt_currency_amount,
10118             NULL                                 	daily_amount,
10119             NULL                                 	web_parameter_id,
10120             NULL                                 	adjustment_reason,
10121             decode(zx_dist.recoverable_flag,
10122                    'Y', NULL,
10123                    tax_dist.award_id)          		award_id,
10124             NULL                        		credit_card_trx_id,
10125             tax_dist.dist_match_type    		dist_match_type,
10126             tax_dist.rcv_transaction_id 		rcv_transaction_id,
10127             ap_invoice_distributions_s.NEXTVAL   	invoice_distribution_id,
10128             tax_dist.invoice_distribution_id     	parent_reversal_id,
10129             tax_dist.tax_recoverable_flag        	tax_recoverable_flag,
10130             NULL                                 	merchant_document_number,
10131             NULL                                 	merchant_name,
10132             NULL                                 	merchant_reference,
10133             NULL                                 	merchant_tax_reg_number,
10134             NULL                                 	merchant_taxpayer_id,
10135             NULL                                 	country_of_supply,
10136             NULL                                 	matched_uom_lookup_code,
10137             NULL                                 	gms_burdenable_raw_cost,
10138             NULL                                 	accounting_event_id,
10139             tax_dist.prepay_distribution_id  	  	prepay_distribution_id,
10140             NULL                                 	upgrade_posted_amt,
10141             NULL                                 	upgrade_base_posted_amt,
10142             'N'                                  	inventory_transfer_status,
10143             NULL                                 	company_prepaid_invoice_id,
10144             NULL                                 	cc_reversal_flag,
10145             NULL                                  	awt_withheld_amt,
10146             NULL                                  	pa_cmt_xface_flag,
10147             decode(p_calling_mode,
10148 			'CANCEL', 'Y', Null)  	  	cancellation_flag,
10149             tax_dist.invoice_line_number	  	invoice_line_number,
10150             tax_dist.corrected_invoice_dist_id		corrected_invoice_dist_id,
10151             tax_dist.rounding_amt       	  	rounding_amt,
10152             zx_dist.trx_line_dist_id			charge_applicable_to_dist_id,
10153             NULL					corrected_quantity,
10154             -- bug 5572121
10155             -- NULL                                  	related_id,
10156             DECODE( tax_dist.related_id, NULL, NULL,
10157                     tax_dist.invoice_distribution_id,
10158                      ap_invoice_distributions_s.CURRVAL, NULL) related_id,
10159 
10160             NULL                                  	asset_book_type_code,
10161             NULL                                  	asset_category_id,
10162             tax_dist.distribution_class 	  	distribution_class,
10163             tax_dist.tax_code_id        	  	tax_code_id,
10164             tax_dist.intended_use                 	intended_use,
10165             zx_dist.rec_nrec_tax_dist_id 	  	detail_tax_dist_id,
10166             zx_dist.rec_nrec_rate     	  	  	rec_nrec_rate,
10167             zx_dist.recovery_rate_id  	  	  	recovery_rate_id,
10168             zx_dist.recovery_type_code	  	  	recovery_type_code,
10169             NULL                                  	withholding_tax_code_id,
10170             NULL			     	  	taxable_amount,
10171             NULL			 	  	taxable_base_amount,
10172             tax_dist.tax_already_distributed_flag	tax_already_distributed_flag,
10173             tax_dist.summary_tax_line_id 	  	summary_tax_line_id,
10174 	        'N'				      	  	rcv_charge_addition_flag,
10175             tax_dist.prepay_tax_diff_amount -- BUG 7338249
10176 	from	ap_invoice_distributions_all 	tax_dist,
10177 		ap_invoice_distributions_all	item_dist,
10178 		zx_rec_nrec_dist		zx_dist
10179 	where	tax_dist.invoice_id		  = p_invoice_id
10180 	and	tax_dist.line_type_lookup_code    IN ('NONREC_TAX', 'REC_TAX', 'TIPV', 'TERV', 'TRV')
10181 	and	tax_dist.detail_tax_dist_id	  = zx_dist.reversed_tax_dist_id
10182 	and	item_dist.invoice_distribution_id(+) = zx_dist.trx_line_dist_id  --bug7394712
10183 	and	zx_dist.reverse_flag 		  = 'Y'
10184       and   zx_dist.trx_line_id = p_line_number --bug6056777
10185 	--bugfix:5582836
10186         and     not exists(select detail_tax_dist_id
10187 			   from ap_invoice_distributions aid
10188 			   where aid.invoice_id = p_invoice_id
10189 			   and aid.detail_tax_dist_id = zx_dist.rec_nrec_tax_dist_id);
10190 
10191 
10192 
10193 	l_reverse_tax_dist	c_reverse_tax_dist%rowtype ;
10194 
10195     /*
10196      --  Bug 6694536. Added cursor to fetch reversal lines from ap_self_assessed_tax_dist_all.
10197      --  Cursor is similar to c_reverse_tax_dist, except it is using ap_self_assessed_tax_dist_all
10198      --  to fetch lines.
10199      */
10200 
10201      cursor c_rev_self_assess_tax_dist is
10202 	  select
10203             zx_dist.gl_date                             accounting_date,
10204             'N'						accrual_posted_flag,
10205             'U'						assets_addition_flag,
10206             'N'						assets_tracking_flag,
10207             'N'						cash_posted_flag,
10208             AP_ETAX_UTILITY_PKG.Get_Max_Dist_Num_Self(
10209 		              p_invoice_id,
10210 		              tax_dist.invoice_line_number)+1
10211 							distribution_line_number,
10212             tax_dist.dist_code_combination_id		dist_code_combination_id,
10213             tax_dist.invoice_id				invoice_id,
10214             l_user_id					last_updated_by,
10215             l_sysdate					last_update_date,
10216             tax_dist.line_type_lookup_code		line_type_lookup_code,
10217             tax_dist.period_name			period_name,
10218             tax_dist.set_of_books_id			set_of_books_id,
10219             (-tax_dist.amount)				amount,
10220             (-tax_dist.base_amount)			base_amount,
10221             --P_Invoice_Header_Rec.batch_id		batch_id,
10222             l_user_id					created_by,
10223             l_sysdate					creation_date,
10224             tax_dist.description			description,
10225             NULL					final_match_flag,
10226             tax_dist.income_tax_region			income_tax_region,
10227             l_user_id					last_update_login,
10228             NULL					match_status_flag,
10229             'N'						posted_flag,
10230             tax_dist.po_distribution_id			po_distribution_id,
10231             NULL					program_application_id,
10232             NULL					program_id,
10233             NULL					program_update_date,
10234             NULL					quantity_invoiced,
10235             NULL					request_id,
10236             'Y'						reversal_flag,
10237             tax_dist.type_1099				type_1099,
10238             tax_dist.unit_price				unit_price,
10239             'N'						encumbered_flag,
10240             NULL					stat_amount,
10241             tax_dist.attribute1				attribute1,
10242             tax_dist.attribute10			attribute10,
10243             tax_dist.attribute11			attribute11,
10244             tax_dist.attribute12			attribute12,
10245             tax_dist.attribute13			attribute13,
10246             tax_dist.attribute14			attribute14,
10247             tax_dist.attribute15			attribute15,
10248             tax_dist.attribute2				attribute2,
10249             tax_dist.attribute3				attribute3,
10250             tax_dist.attribute4				attribute4,
10251             tax_dist.attribute5				attribute5,
10252             tax_dist.attribute6				attribute6,
10253             tax_dist.attribute7				attribute7,
10254             tax_dist.attribute8				attribute8,
10255             tax_dist.attribute9				attribute9,
10256             tax_dist.attribute_category			attribute_category,
10257             decode(zx_dist.recoverable_flag,
10258                    'Y', NULL,
10259 		   item_dist.expenditure_item_date)	expenditure_item_date,
10260             decode(zx_dist.recoverable_flag,
10261                    'Y', NULL,
10262                    item_dist.expenditure_organization_id)  expenditure_organization_id,
10263             decode(zx_dist.recoverable_flag,
10264                    'Y', NULL,
10265             	   item_dist.expenditure_type)		expenditure_type,
10266             tax_dist.parent_invoice_id			parent_invoice_id,
10267             decode(zx_dist.recoverable_flag,
10268 		   'Y', 'E',
10269 		   item_dist.pa_addition_flag)		pa_addition_flag,
10270             decode(zx_dist.recoverable_flag,
10271                    'Y', NULL,
10272                    item_dist.pa_quantity)		pa_quantity,
10273             NULL					prepay_amount_remaining,
10274             decode(zx_dist.recoverable_flag,
10275                    'Y', NULL,
10276                    item_dist.project_accounting_context) project_accounting_context,
10277             decode(zx_dist.recoverable_flag,
10278                    'Y', NULL,
10279                    item_dist.project_id)		project_id,
10280             decode(zx_dist.recoverable_flag,
10281                    'Y', NULL,
10282                    item_dist.task_id)			task_id,
10283             NULL					packet_id,
10284             'N'						awt_flag,
10285             tax_dist.awt_group_id			awt_group_id,
10286             NULL					awt_tax_rate_id,
10287             NULL					awt_gross_amount,
10288             NULL					awt_invoice_id,
10289             NULL					awt_origin_group_id,
10290             NULL					reference_1,
10291             NULL					reference_2,
10292             tax_dist.org_id				org_id,
10293             NULL					awt_invoice_payment_id,
10294             tax_dist.global_attribute_category		global_attribute_category,
10295             tax_dist.global_attribute1			global_attribute1,
10296             tax_dist.global_attribute2			global_attribute2,
10297             tax_dist.global_attribute3			global_attribute3,
10298             tax_dist.global_attribute4			global_attribute4,
10299             tax_dist.global_attribute5			global_attribute5,
10300             tax_dist.global_attribute6			global_attribute6,
10301             tax_dist.global_attribute7			global_attribute7,
10302             tax_dist.global_attribute8			global_attribute8,
10303             tax_dist.global_attribute9			global_attribute9,
10304             tax_dist.global_attribute10			global_attribute10,
10305             tax_dist.global_attribute11			global_attribute11,
10306             tax_dist.global_attribute12			global_attribute12,
10307             tax_dist.global_attribute13			global_attribute13,
10308             tax_dist.global_attribute14			global_attribute14,
10309             tax_dist.global_attribute15			global_attribute15,
10310             tax_dist.global_attribute16			global_attribute16,
10311             tax_dist.global_attribute17			global_attribute17,
10312             tax_dist.global_attribute18			global_attribute18,
10313             tax_dist.global_attribute19			global_attribute19,
10314             tax_dist.global_attribute20			global_attribute20,
10315             NULL                                 	receipt_verified_flag,
10316             NULL                                 	receipt_required_flag,
10317             NULL                                 	receipt_missing_flag,
10318             NULL                                 	justification,
10319             NULL                                 	expense_group,
10320             NULL                                 	start_expense_date,
10321             NULL                                 	end_expense_date,
10322             NULL                                 	receipt_currency_code,
10323             NULL                                 	receipt_conversion_rate,
10324             NULL                                 	receipt_currency_amount,
10325             NULL                                 	daily_amount,
10326             NULL                                 	web_parameter_id,
10327             NULL                                 	adjustment_reason,
10328             decode(zx_dist.recoverable_flag,
10329                    'Y', NULL,
10330                    tax_dist.award_id)          		award_id,
10331             NULL                        		credit_card_trx_id,
10332             tax_dist.dist_match_type    		dist_match_type,
10333             tax_dist.rcv_transaction_id 		rcv_transaction_id,
10334             ap_invoice_distributions_s.NEXTVAL   	invoice_distribution_id,
10335             tax_dist.invoice_distribution_id     	parent_reversal_id,
10336             tax_dist.tax_recoverable_flag        	tax_recoverable_flag,
10337             NULL                                 	merchant_document_number,
10338             NULL                                 	merchant_name,
10339             NULL                                 	merchant_reference,
10340             NULL                                 	merchant_tax_reg_number,
10341             NULL                                 	merchant_taxpayer_id,
10342             NULL                                 	country_of_supply,
10343             NULL                                 	matched_uom_lookup_code,
10344             NULL                                 	gms_burdenable_raw_cost,
10345             NULL                                 	accounting_event_id,
10346             tax_dist.prepay_distribution_id  	  	prepay_distribution_id,
10347             NULL                                 	upgrade_posted_amt,
10348             NULL                                 	upgrade_base_posted_amt,
10349             'N'                                  	inventory_transfer_status,
10350             NULL                                 	company_prepaid_invoice_id,
10351             NULL                                 	cc_reversal_flag,
10352             NULL                                  	awt_withheld_amt,
10353             NULL                                  	pa_cmt_xface_flag,
10354             decode(p_calling_mode,
10355 			'CANCEL', 'Y', Null)  	  	cancellation_flag,
10356             tax_dist.invoice_line_number	  	invoice_line_number,
10357             tax_dist.corrected_invoice_dist_id		corrected_invoice_dist_id,
10358             tax_dist.rounding_amt       	  	rounding_amt,
10359             zx_dist.trx_line_dist_id			charge_applicable_to_dist_id,
10360             NULL					corrected_quantity,
10361             -- bug 5572121
10362             -- NULL                                  	related_id,
10363             DECODE( tax_dist.related_id, NULL, NULL,
10364                     tax_dist.invoice_distribution_id,
10365                      ap_invoice_distributions_s.CURRVAL, NULL) related_id,
10366 
10367             NULL                                  	asset_book_type_code,
10368             NULL                                  	asset_category_id,
10369             tax_dist.distribution_class 	  	distribution_class,
10370             tax_dist.tax_code_id        	  	tax_code_id,
10371             tax_dist.intended_use                 	intended_use,
10372             zx_dist.rec_nrec_tax_dist_id 	  	detail_tax_dist_id,
10373             zx_dist.rec_nrec_rate     	  	  	rec_nrec_rate,
10374             zx_dist.recovery_rate_id  	  	  	recovery_rate_id,
10375             zx_dist.recovery_type_code	  	  	recovery_type_code,
10376             NULL                                  	withholding_tax_code_id,
10377             NULL			     	  	taxable_amount,
10378             NULL			 	  	taxable_base_amount,
10379             tax_dist.tax_already_distributed_flag	tax_already_distributed_flag,
10380             tax_dist.summary_tax_line_id 	  	summary_tax_line_id,
10381 	        'N'				      	  	rcv_charge_addition_flag,
10382 	        zx_dist.self_assessed_flag                  self_assessed_flag,
10383             -- bug 6805655
10384             tax_dist.self_assessed_tax_liab_ccid        self_assessed_tax_liab_ccid,
10385             tax_dist.prepay_tax_diff_amount -- BUG 7338249
10386 	from	ap_self_assessed_tax_dist_all 	tax_dist,
10387 		    ap_invoice_distributions_all	item_dist,
10388 		    zx_rec_nrec_dist		zx_dist
10389 	where	tax_dist.invoice_id		  = p_invoice_id
10390 	and	tax_dist.line_type_lookup_code    IN ('NONREC_TAX', 'REC_TAX')
10391 	and	tax_dist.detail_tax_dist_id	  = zx_dist.reversed_tax_dist_id
10392 	and	item_dist.invoice_distribution_id(+) = zx_dist.trx_line_dist_id  --bug7394712
10393 	and	zx_dist.reverse_flag 		  = 'Y'
10394         --and     (p_line_number IS NULL -- bug 6056777
10395           --       or zx_dist.trx_line_id = p_line_number) --bug605677
10396 	--bugfix:5582836
10397     /*  -- bug 6896627
10398         and     not exists(select detail_tax_dist_id
10399 			   from ap_invoice_distributions aid
10400 			   where aid.invoice_id = p_invoice_id
10401 			   and aid.detail_tax_dist_id = zx_dist.rec_nrec_tax_dist_id)
10402     */
10403     AND NOT EXISTS
10404       (SELECT aid.detail_tax_dist_id
10405          FROM ap_self_assessed_tax_dist_all aid
10406         WHERE aid.invoice_id            = p_invoice_id
10407           AND aid.detail_tax_dist_id    = zx_dist.rec_nrec_tax_dist_id
10408           AND aid.line_type_lookup_code IN ('REC_TAX','NONREC_TAX'))
10409     -- bug 6896627
10410     ;
10411 
10412      /*
10413        -- Bug 6694536. Added cursor to fetch reversal lines from ap_self_assessed_tax_dist_all.
10414        -- Cursor is similar to c_reverse_tax_dist_1, except it is using ap_self_assessed_tax_dist_all
10415        -- to fetch lines. Second cursor for performance reasons(bug 6056777)
10416      */
10417 
10418     cursor c_rev_self_assess_tax_dist_1 is
10419 	  select
10420             zx_dist.gl_date                             accounting_date,
10421             'N'						accrual_posted_flag,
10422             'U'						assets_addition_flag,
10423             'N'						assets_tracking_flag,
10424             'N'						cash_posted_flag,
10425             AP_ETAX_UTILITY_PKG.Get_Max_Dist_Num_Self(
10426 		              p_invoice_id,
10427 		              tax_dist.invoice_line_number)+1
10428 							distribution_line_number,
10429             tax_dist.dist_code_combination_id		dist_code_combination_id,
10430             tax_dist.invoice_id				invoice_id,
10431             l_user_id					last_updated_by,
10432             l_sysdate					last_update_date,
10433             tax_dist.line_type_lookup_code		line_type_lookup_code,
10434             tax_dist.period_name			period_name,
10435             tax_dist.set_of_books_id			set_of_books_id,
10436             (-tax_dist.amount)				amount,
10437             (-tax_dist.base_amount)			base_amount,
10438             --P_Invoice_Header_Rec.batch_id		batch_id,
10439             l_user_id					created_by,
10440             l_sysdate					creation_date,
10441             tax_dist.description			description,
10442             NULL					final_match_flag,
10443             tax_dist.income_tax_region			income_tax_region,
10444             l_user_id					last_update_login,
10445             NULL					match_status_flag,
10446             'N'						posted_flag,
10447             tax_dist.po_distribution_id			po_distribution_id,
10448             NULL					program_application_id,
10449             NULL					program_id,
10450             NULL					program_update_date,
10451             NULL					quantity_invoiced,
10452             NULL					request_id,
10453             'Y'						reversal_flag,
10454             tax_dist.type_1099				type_1099,
10455             tax_dist.unit_price				unit_price,
10456             'N'						encumbered_flag,
10457             NULL					stat_amount,
10458             tax_dist.attribute1				attribute1,
10459             tax_dist.attribute10			attribute10,
10460             tax_dist.attribute11			attribute11,
10461             tax_dist.attribute12			attribute12,
10462             tax_dist.attribute13			attribute13,
10463             tax_dist.attribute14			attribute14,
10464             tax_dist.attribute15			attribute15,
10465             tax_dist.attribute2				attribute2,
10466             tax_dist.attribute3				attribute3,
10467             tax_dist.attribute4				attribute4,
10468             tax_dist.attribute5				attribute5,
10469             tax_dist.attribute6				attribute6,
10470             tax_dist.attribute7				attribute7,
10471             tax_dist.attribute8				attribute8,
10472             tax_dist.attribute9				attribute9,
10473             tax_dist.attribute_category			attribute_category,
10474             decode(zx_dist.recoverable_flag,
10475                    'Y', NULL,
10476 		   item_dist.expenditure_item_date)	expenditure_item_date,
10477             decode(zx_dist.recoverable_flag,
10478                    'Y', NULL,
10479                    item_dist.expenditure_organization_id)  expenditure_organization_id,
10480             decode(zx_dist.recoverable_flag,
10481                    'Y', NULL,
10482             	   item_dist.expenditure_type)		expenditure_type,
10483             tax_dist.parent_invoice_id			parent_invoice_id,
10484             decode(zx_dist.recoverable_flag,
10485 		   'Y', 'E',
10486 		   item_dist.pa_addition_flag)		pa_addition_flag,
10487             decode(zx_dist.recoverable_flag,
10488                    'Y', NULL,
10489                    item_dist.pa_quantity)		pa_quantity,
10490             NULL					prepay_amount_remaining,
10491             decode(zx_dist.recoverable_flag,
10492                    'Y', NULL,
10493                    item_dist.project_accounting_context) project_accounting_context,
10494             decode(zx_dist.recoverable_flag,
10495                    'Y', NULL,
10496                    item_dist.project_id)		project_id,
10497             decode(zx_dist.recoverable_flag,
10498                    'Y', NULL,
10499                    item_dist.task_id)			task_id,
10500             NULL					packet_id,
10501             'N'						awt_flag,
10502             tax_dist.awt_group_id			awt_group_id,
10503             NULL					awt_tax_rate_id,
10504             NULL					awt_gross_amount,
10505             NULL					awt_invoice_id,
10506             NULL					awt_origin_group_id,
10507             NULL					reference_1,
10508             NULL					reference_2,
10509             tax_dist.org_id				org_id,
10510             NULL					awt_invoice_payment_id,
10511             tax_dist.global_attribute_category		global_attribute_category,
10512             tax_dist.global_attribute1			global_attribute1,
10513             tax_dist.global_attribute2			global_attribute2,
10514             tax_dist.global_attribute3			global_attribute3,
10515             tax_dist.global_attribute4			global_attribute4,
10516             tax_dist.global_attribute5			global_attribute5,
10517             tax_dist.global_attribute6			global_attribute6,
10518             tax_dist.global_attribute7			global_attribute7,
10519             tax_dist.global_attribute8			global_attribute8,
10520             tax_dist.global_attribute9			global_attribute9,
10521             tax_dist.global_attribute10			global_attribute10,
10522             tax_dist.global_attribute11			global_attribute11,
10523             tax_dist.global_attribute12			global_attribute12,
10524             tax_dist.global_attribute13			global_attribute13,
10525             tax_dist.global_attribute14			global_attribute14,
10526             tax_dist.global_attribute15			global_attribute15,
10527             tax_dist.global_attribute16			global_attribute16,
10528             tax_dist.global_attribute17			global_attribute17,
10529             tax_dist.global_attribute18			global_attribute18,
10530             tax_dist.global_attribute19			global_attribute19,
10531             tax_dist.global_attribute20			global_attribute20,
10532             NULL                                 	receipt_verified_flag,
10533             NULL                                 	receipt_required_flag,
10534             NULL                                 	receipt_missing_flag,
10535             NULL                                 	justification,
10536             NULL                                 	expense_group,
10537             NULL                                 	start_expense_date,
10538             NULL                                 	end_expense_date,
10539             NULL                                 	receipt_currency_code,
10540             NULL                                 	receipt_conversion_rate,
10541             NULL                                 	receipt_currency_amount,
10542             NULL                                 	daily_amount,
10543             NULL                                 	web_parameter_id,
10544             NULL                                 	adjustment_reason,
10545             decode(zx_dist.recoverable_flag,
10546                    'Y', NULL,
10547                    tax_dist.award_id)          		award_id,
10548             NULL                        		credit_card_trx_id,
10549             tax_dist.dist_match_type    		dist_match_type,
10550             tax_dist.rcv_transaction_id 		rcv_transaction_id,
10551             ap_invoice_distributions_s.NEXTVAL   	invoice_distribution_id,
10552             tax_dist.invoice_distribution_id     	parent_reversal_id,
10553             tax_dist.tax_recoverable_flag        	tax_recoverable_flag,
10554             NULL                                 	merchant_document_number,
10555             NULL                                 	merchant_name,
10556             NULL                                 	merchant_reference,
10557             NULL                                 	merchant_tax_reg_number,
10558             NULL                                 	merchant_taxpayer_id,
10559             NULL                                 	country_of_supply,
10560             NULL                                 	matched_uom_lookup_code,
10561             NULL                                 	gms_burdenable_raw_cost,
10562             NULL                                 	accounting_event_id,
10563             tax_dist.prepay_distribution_id  	  	prepay_distribution_id,
10564             NULL                                 	upgrade_posted_amt,
10565             NULL                                 	upgrade_base_posted_amt,
10566             'N'                                  	inventory_transfer_status,
10567             NULL                                 	company_prepaid_invoice_id,
10568             NULL                                 	cc_reversal_flag,
10569             NULL                                  	awt_withheld_amt,
10570             NULL                                  	pa_cmt_xface_flag,
10571             decode(p_calling_mode,
10572 			'CANCEL', 'Y', Null)  	  	cancellation_flag,
10573             tax_dist.invoice_line_number	  	invoice_line_number,
10574             tax_dist.corrected_invoice_dist_id		corrected_invoice_dist_id,
10575             tax_dist.rounding_amt       	  	rounding_amt,
10576             zx_dist.trx_line_dist_id			charge_applicable_to_dist_id,
10577             NULL					corrected_quantity,
10578             -- bug 5572121
10579             -- NULL                                  	related_id,
10580             DECODE( tax_dist.related_id, NULL, NULL,
10581                     tax_dist.invoice_distribution_id,
10582                      ap_invoice_distributions_s.CURRVAL, NULL) related_id,
10583 
10584             NULL                                  	asset_book_type_code,
10585             NULL                                  	asset_category_id,
10586             tax_dist.distribution_class 	  	distribution_class,
10587             tax_dist.tax_code_id        	  	tax_code_id,
10588             tax_dist.intended_use                 	intended_use,
10589             zx_dist.rec_nrec_tax_dist_id 	  	detail_tax_dist_id,
10590             zx_dist.rec_nrec_rate     	  	  	rec_nrec_rate,
10591             zx_dist.recovery_rate_id  	  	  	recovery_rate_id,
10592             zx_dist.recovery_type_code	  	  	recovery_type_code,
10593             NULL                                  	withholding_tax_code_id,
10594             NULL			     	  	taxable_amount,
10595             NULL			 	  	taxable_base_amount,
10596             tax_dist.tax_already_distributed_flag	tax_already_distributed_flag,
10597             tax_dist.summary_tax_line_id 	  	summary_tax_line_id,
10598 	        'N'				      	rcv_charge_addition_flag,
10599             zx_dist.self_assessed_flag                  self_assessed_flag,
10600             -- bug 6805655
10601             tax_dist.self_assessed_tax_liab_ccid        self_assessed_tax_liab_ccid,
10602             tax_dist.prepay_tax_diff_amount -- BUG 7338249
10603 	from	ap_self_assessed_tax_dist_all 	tax_dist,
10604 		    ap_invoice_distributions_all	item_dist,
10605 		    zx_rec_nrec_dist		zx_dist
10606 	where	tax_dist.invoice_id		  = p_invoice_id
10607 	and	tax_dist.line_type_lookup_code    IN ('NONREC_TAX', 'REC_TAX')
10608 	and	tax_dist.detail_tax_dist_id	  = zx_dist.reversed_tax_dist_id
10609 	and	item_dist.invoice_distribution_id(+) = zx_dist.trx_line_dist_id  --bug7394712
10610 	and	zx_dist.reverse_flag 		  = 'Y'
10611       and   zx_dist.trx_line_id = p_line_number --bug6056777
10612 	--bugfix:5582836
10613     /*     -- bug 6896627
10614         and     not exists(select detail_tax_dist_id
10615 			   from ap_invoice_distributions aid
10616 			   where aid.invoice_id = p_invoice_id
10617 			   and aid.detail_tax_dist_id = zx_dist.rec_nrec_tax_dist_id)
10618      */
10619 
10620     AND NOT EXISTS
10621       (SELECT aid.detail_tax_dist_id
10622          FROM ap_self_assessed_tax_dist_all aid
10623         WHERE aid.invoice_id            = p_invoice_id
10624           AND aid.detail_tax_dist_id    = zx_dist.rec_nrec_tax_dist_id
10625           AND aid.line_type_lookup_code IN ('REC_TAX','NONREC_TAX'))
10626     -- bug 6896627
10627     ;
10628 
10629 
10630 	l_self_assess_rev_tax_dist   c_rev_self_assess_tax_dist%rowtype ;  -- Bug 6694536
10631 	l_self_assess_rev_tax_dist_1 c_rev_self_assess_tax_dist_1%rowtype ; -- Bug 6694536
10632 
10633     l_reverse_dist_count  NUMBER;
10634     l_Error_Code          ZX_ERRORS_GT.message_text%TYPE;
10635 BEGIN
10636 
10637     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.CANCEL_INVOICE';
10638 
10639 
10640     l_tax_distributions_exist := tax_distributions_exist (p_invoice_id=>p_invoice_id);
10641 
10642     l_self_assess_tax_dist_exist := self_assess_tax_dist_exist (p_invoice_id=>p_invoice_id);
10643 
10644 
10645     IF NOT (l_tax_distributions_exist OR l_self_assess_tax_dist_exist) THEN
10646 
10647        l_debug_info := 'Exit reverse_distributions';
10648        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10649            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10650        END IF;
10651 
10652        RETURN l_return_status;
10653 
10654     END IF;
10655 
10656 
10657     IF l_tax_distributions_exist THEN -- Marker 1
10658 
10659       -----------------------------------------------------------------
10660       l_debug_info := 'Step 1: Insert into zx_reverse_dist_gt';
10661       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10662           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10663       END IF;
10664       -----------------------------------------------------------------
10665 
10666       INSERT INTO zx_reverse_dist_gt(
10667         internal_organization_id,
10668         reversing_appln_id,
10669         reversing_entity_code,
10670         reversing_evnt_cls_code,
10671         reversing_trx_id,
10672         reversing_trx_level_type,
10673         reversing_trx_line_id,
10674         reversing_trx_line_dist_id,
10675         reversing_tax_line_id,
10676         reversed_appln_id,
10677         reversed_entity_code,
10678         reversed_evnt_cls_code,
10679         reversed_trx_id,
10680         reversed_trx_level_type,
10681         reversed_trx_line_id,
10682         reversed_trx_line_dist_id,
10683         reversed_tax_line_id
10684       )
10685       select	distinct
10686 		item_dist.org_id                	internal_organization_id,
10687                 zx_dist.application_id          	reversing_appln_id,
10688                 zx_dist.entity_code             	reversing_entity_code,
10689                 zx_dist.event_class_code        	reversing_evnt_cls_code,
10690                 zx_dist.trx_id                  	reversing_trx_id,
10691                 zx_dist.trx_level_type          	reversing_trx_level_type,
10692                 zx_dist.trx_line_id             	reversing_trx_line_id,
10693                 reverse_dist.invoice_distribution_id	reversing_trx_line_dist_id,
10694                 zx_dist.tax_line_id             	reversing_tax_line_id,
10695                 zx_dist.application_id          	reversed_appln_id,
10696                 zx_dist.entity_code             	reversed_entity_code,
10697                 zx_dist.event_class_code        	reversed_evnt_cls_code,
10698                 zx_dist.trx_id                  	reversed_trx_id,
10699                 zx_dist.trx_level_type          	reversed_trx_level_type,
10700                 zx_dist.trx_line_id             	reversed_trx_line_id,
10701 		zx_dist.trx_line_dist_id        	reversed_trx_line_dist_id,
10702                 zx_dist.tax_line_id             	reversed_tax_line_id
10703         from    ap_invoice_distributions_all    item_dist,
10704                 ap_invoice_distributions_all    tax_dist,
10705                 ap_invoice_distributions_all    reverse_dist,
10706                 zx_rec_nrec_dist                zx_dist
10707         where   tax_dist.invoice_id                     = p_invoice_id
10708         and     tax_dist.invoice_id                     = item_dist.invoice_id
10709         and     tax_dist.charge_applicable_to_dist_id   = item_dist.invoice_distribution_id
10710         and	item_dist.invoice_distribution_id	= reverse_dist.parent_reversal_id
10711         and     tax_dist.line_type_lookup_code          IN ('NONREC_TAX', 'REC_TAX', 'TIPV', 'TRV', 'TERV')
10712         and     tax_dist.detail_tax_dist_id             = zx_dist.rec_nrec_tax_dist_id
10713         and     nvl(zx_dist.reverse_flag, 'N')          = 'N'
10714         and     (p_line_number IS NULL
10715                  or item_dist.invoice_line_number = p_line_number);
10716 
10717       l_reverse_dist_count := SQL%ROWCOUNT;
10718 
10719       -----------------------------------------------------------------
10720       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10721         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Row Count inserted into zx_reverse_dist_gt: ' || l_reverse_dist_count);
10722       END IF;
10723       -----------------------------------------------------------------
10724 
10725     END IF; -- Marker 1
10726 
10727 
10728     IF l_self_assess_tax_dist_exist THEN -- Marker 2
10729 
10730        l_debug_info := 'Inserting self assessed tax entries into zx_reverse_dist_gt';
10731        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10732           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10733        END IF;
10734 
10735        -- Populating zx_reverse_dist_gt with self assessed entries that has to be
10736        -- reversed. Fetched tax distributions from ap_self_assessed_tax_dist_all.
10737        -- ITEM line is already reversed in Discard method. reverse_dist refers to
10738        -- the reversed item line.
10739 
10740        INSERT INTO zx_reverse_dist_gt(
10741         internal_organization_id,
10742         reversing_appln_id,
10743         reversing_entity_code,
10744         reversing_evnt_cls_code,
10745         reversing_trx_id,
10746         reversing_trx_level_type,
10747         reversing_trx_line_id,
10748         reversing_trx_line_dist_id,
10749         reversing_tax_line_id,
10750         reversed_appln_id,
10751         reversed_entity_code,
10752         reversed_evnt_cls_code,
10753         reversed_trx_id,
10754         reversed_trx_level_type,
10755         reversed_trx_line_id,
10756         reversed_trx_line_dist_id,
10757         reversed_tax_line_id
10758       )
10759       select	distinct
10760 		item_dist.org_id                	internal_organization_id,
10761                 zx_dist.application_id          	reversing_appln_id,
10762                 zx_dist.entity_code             	reversing_entity_code,
10763                 zx_dist.event_class_code        	reversing_evnt_cls_code,
10764                 zx_dist.trx_id                  	reversing_trx_id,
10765                 zx_dist.trx_level_type          	reversing_trx_level_type,
10766                 zx_dist.trx_line_id             	reversing_trx_line_id,
10767                 reverse_dist.invoice_distribution_id	reversing_trx_line_dist_id,
10768                 zx_dist.tax_line_id             	reversing_tax_line_id,
10769                 zx_dist.application_id          	reversed_appln_id,
10770                 zx_dist.entity_code             	reversed_entity_code,
10771                 zx_dist.event_class_code        	reversed_evnt_cls_code,
10772                 zx_dist.trx_id                  	reversed_trx_id,
10773                 zx_dist.trx_level_type          	reversed_trx_level_type,
10774                 zx_dist.trx_line_id             	reversed_trx_line_id,
10775 		zx_dist.trx_line_dist_id        	reversed_trx_line_dist_id,
10776                 zx_dist.tax_line_id             	reversed_tax_line_id
10777         from    ap_invoice_distributions_all    item_dist,
10778                 ap_self_assessed_tax_dist_all    tax_dist,
10779                 ap_invoice_distributions_all    reverse_dist,
10780                 zx_rec_nrec_dist                zx_dist
10781         where   tax_dist.invoice_id                     = p_invoice_id
10782         and     tax_dist.invoice_id                     = item_dist.invoice_id
10783         and     tax_dist.charge_applicable_to_dist_id   = item_dist.invoice_distribution_id
10784         and	item_dist.invoice_distribution_id	= reverse_dist.parent_reversal_id
10785         and     tax_dist.line_type_lookup_code          IN ('NONREC_TAX', 'REC_TAX')
10786         and     tax_dist.detail_tax_dist_id             = zx_dist.rec_nrec_tax_dist_id
10787         and     nvl(zx_dist.reverse_flag, 'N')          = 'N'
10788         and     (p_line_number IS NULL
10789                  or item_dist.invoice_line_number = p_line_number);
10790 
10791       l_reverse_dist_count := NVL(l_reverse_dist_count, 0) + SQL%ROWCOUNT;
10792 
10793     END IF; -- Marker 2
10794 
10795 
10796     IF NVL(l_reverse_dist_count,0) > 0 THEN -- Marker 3
10797 
10798       -----------------------------------------------------------------
10799       l_debug_info := 'Step 2: Call zx_api_pub.reverse_distributions';
10800       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10801           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10802       END IF;
10803       ----------------------------------------------------------------
10804 
10805       zx_api_pub.reverse_distributions(
10806                 p_api_version      => 1.0,
10807                 p_init_msg_list    => FND_API.G_TRUE,
10808                 p_commit           => FND_API.G_FALSE,
10809                 p_validation_level => FND_API.G_VALID_LEVEL_FULL,
10810                 x_return_status    => l_return_status_service,
10811                 x_msg_count        => l_msg_count,
10812                 x_msg_data         => l_msg_data);
10813 
10814        IF (l_return_status_service <> 'S') THEN  -- handle errors
10815 
10816          l_return_status := FALSE;
10817          -----------------------------------------------------------------
10818          l_debug_info := 'Step 2.5: Handle errors returned by API';
10819          -----------------------------------------------------------------
10820          IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10821            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
10822          END IF;
10823 
10824          IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
10825                    P_All_Error_Messages  => 'N',
10826                    P_Msg_Count           => l_msg_count,
10827                    P_Msg_Data            => l_msg_data,
10828                    P_Error_Code          => l_Error_Code,
10829                    P_Calling_Sequence    => l_curr_calling_sequence)) THEN
10830             NULL;
10831          END IF;
10832 
10833          RETURN l_return_status;
10834        END IF;
10835 
10836     END IF; -- Marker 3
10837 
10838 
10839     IF l_return_status THEN
10840 
10841      IF p_line_number IS NULL THEN -- Marker 4
10842 
10843        IF l_tax_distributions_exist THEN -- Marker 5
10844 
10845        -----------------------------------------------------------------
10846        l_debug_info := 'Step 3: Insert reverse tax distributions into ap_invoice_distributions';
10847        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10848            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10849        END IF;
10850        ----------------------------------------------------------------
10851 
10852        OPEN c_reverse_tax_dist;
10853        LOOP
10854 	   FETCH c_reverse_tax_dist
10855 	   INTO  l_reverse_tax_dist;
10856 	   EXIT WHEN c_reverse_tax_dist%NOTFOUND;
10857 
10858 	   INSERT INTO ap_invoice_distributions_all (
10859             accounting_date,
10860             accrual_posted_flag,
10861             assets_addition_flag,
10862             assets_tracking_flag,
10863             cash_posted_flag,
10864             distribution_line_number,
10865             dist_code_combination_id,
10866             invoice_id,
10867             last_updated_by,
10868             last_update_date,
10869             line_type_lookup_code,
10870             period_name,
10871             set_of_books_id,
10872             amount,
10873             base_amount,
10874             --batch_id,
10875             created_by,
10876             creation_date,
10877             description,
10878             final_match_flag,
10879             income_tax_region,
10880             last_update_login,
10881             match_status_flag,
10882             posted_flag,
10883             po_distribution_id,
10884             program_application_id,
10885             program_id,
10886             program_update_date,
10887             quantity_invoiced,
10888             request_id,
10889             reversal_flag,
10890             type_1099,
10891             unit_price,
10892             encumbered_flag,
10893             stat_amount,
10894             attribute1,
10895             attribute10,
10896             attribute11,
10897             attribute12,
10898             attribute13,
10899             attribute14,
10900             attribute15,
10901             attribute2,
10902             attribute3,
10903             attribute4,
10904             attribute5,
10905             attribute6,
10906             attribute7,
10907             attribute8,
10908             attribute9,
10909             attribute_category,
10910             expenditure_item_date,
10911             expenditure_organization_id,
10912             expenditure_type,
10913             parent_invoice_id,
10914             pa_addition_flag,
10915             pa_quantity,
10916             prepay_amount_remaining,
10917             project_accounting_context,
10918             project_id,
10919             task_id,
10920             packet_id,
10921             awt_flag,
10922             awt_group_id,
10923             awt_tax_rate_id,
10924             awt_gross_amount,
10925             awt_invoice_id,
10926             awt_origin_group_id,
10927             reference_1,
10928             reference_2,
10929             org_id,
10930             awt_invoice_payment_id,
10931             global_attribute_category,
10932             global_attribute1,
10933             global_attribute2,
10934             global_attribute3,
10935             global_attribute4,
10936             global_attribute5,
10937             global_attribute6,
10938             global_attribute7,
10939             global_attribute8,
10940             global_attribute9,
10941             global_attribute10,
10942             global_attribute11,
10943             global_attribute12,
10944             global_attribute13,
10945             global_attribute14,
10946             global_attribute15,
10947             global_attribute16,
10948             global_attribute17,
10949             global_attribute18,
10950             global_attribute19,
10951             global_attribute20,
10952             receipt_verified_flag,
10953             receipt_required_flag,
10954             receipt_missing_flag,
10955             justification,
10956             expense_group,
10957             start_expense_date,
10958             end_expense_date,
10959             receipt_currency_code,
10960             receipt_conversion_rate,
10961             receipt_currency_amount,
10962             daily_amount,
10963             web_parameter_id,
10964             adjustment_reason,
10965             award_id,
10966             credit_card_trx_id,
10967             dist_match_type,
10968             rcv_transaction_id,
10969             invoice_distribution_id,
10970             parent_reversal_id,
10971             tax_recoverable_flag,
10972             merchant_document_number,
10973             merchant_name,
10974             merchant_reference,
10975             merchant_tax_reg_number,
10976             merchant_taxpayer_id,
10977             country_of_supply,
10978             matched_uom_lookup_code,
10979             gms_burdenable_raw_cost,
10980             accounting_event_id,
10981             prepay_distribution_id,
10982             upgrade_posted_amt,
10983             upgrade_base_posted_amt,
10984             inventory_transfer_status,
10985             company_prepaid_invoice_id,
10986             cc_reversal_flag,
10987             awt_withheld_amt,
10988             pa_cmt_xface_flag,
10989             cancellation_flag,
10990             invoice_line_number,
10991             corrected_invoice_dist_id,
10992             rounding_amt,
10993             charge_applicable_to_dist_id,
10994             corrected_quantity,
10995             related_id,
10996             asset_book_type_code,
10997             asset_category_id,
10998             distribution_class,
10999             tax_code_id,
11000             intended_use,
11001             detail_tax_dist_id,
11002             rec_nrec_rate,
11003             recovery_rate_id,
11004             recovery_type_code,
11005             withholding_tax_code_id,
11006             taxable_amount,
11007             taxable_base_amount,
11008             tax_already_distributed_flag,
11009             summary_tax_line_id,
11010 	        rcv_charge_addition_flag,
11011             prepay_tax_diff_amount ) -- BUG 7338249
11012 	VALUES
11013 	(
11014 	l_reverse_tax_dist.accounting_date,
11015 	l_reverse_tax_dist.accrual_posted_flag,
11016 	l_reverse_tax_dist.assets_addition_flag,
11017 	l_reverse_tax_dist.assets_tracking_flag,
11018 	l_reverse_tax_dist.cash_posted_flag,
11019 	l_reverse_tax_dist.distribution_line_number,
11020 	l_reverse_tax_dist.dist_code_combination_id,
11021 	l_reverse_tax_dist.invoice_id,
11022 	l_reverse_tax_dist.last_updated_by,
11023 	l_reverse_tax_dist.last_update_date,
11024 	l_reverse_tax_dist.line_type_lookup_code,
11025 	l_reverse_tax_dist.period_name,
11026 	l_reverse_tax_dist.set_of_books_id,
11027 	l_reverse_tax_dist.amount,
11028 	l_reverse_tax_dist.base_amount,
11029 	--l_reverse_tax_dist.batch_id,
11030 	l_reverse_tax_dist.created_by,
11031 	l_reverse_tax_dist.creation_date,
11032 	l_reverse_tax_dist.description,
11033 	l_reverse_tax_dist.final_match_flag,
11034 	l_reverse_tax_dist.income_tax_region,
11035 	l_reverse_tax_dist.last_update_login,
11036 	l_reverse_tax_dist.match_status_flag,
11037 	l_reverse_tax_dist.posted_flag,
11038 	l_reverse_tax_dist.po_distribution_id,
11039 	l_reverse_tax_dist.program_application_id,
11040 	l_reverse_tax_dist.program_id,
11041 	l_reverse_tax_dist.program_update_date,
11042 	l_reverse_tax_dist.quantity_invoiced,
11043 	l_reverse_tax_dist.request_id,
11044 	l_reverse_tax_dist.reversal_flag,
11045 	l_reverse_tax_dist.type_1099,
11046 	l_reverse_tax_dist.unit_price,
11047 	l_reverse_tax_dist.encumbered_flag,
11048 	l_reverse_tax_dist.stat_amount,
11049 	l_reverse_tax_dist.attribute1,
11050 	l_reverse_tax_dist.attribute10,
11051 	l_reverse_tax_dist.attribute11,
11052 	l_reverse_tax_dist.attribute12,
11053 	l_reverse_tax_dist.attribute13,
11054 	l_reverse_tax_dist.attribute14,
11055 	l_reverse_tax_dist.attribute15,
11056 	l_reverse_tax_dist.attribute2,
11057 	l_reverse_tax_dist.attribute3,
11058 	l_reverse_tax_dist.attribute4,
11059 	l_reverse_tax_dist.attribute5,
11060 	l_reverse_tax_dist.attribute6,
11061 	l_reverse_tax_dist.attribute7,
11062 	l_reverse_tax_dist.attribute8,
11063 	l_reverse_tax_dist.attribute9,
11064 	l_reverse_tax_dist.attribute_category,
11065 	l_reverse_tax_dist.expenditure_item_date,
11066 	l_reverse_tax_dist.expenditure_organization_id,
11067 	l_reverse_tax_dist.expenditure_type,
11068 	l_reverse_tax_dist.parent_invoice_id,
11069 	l_reverse_tax_dist.pa_addition_flag,
11070 	l_reverse_tax_dist.pa_quantity,
11071 	l_reverse_tax_dist.prepay_amount_remaining,
11072 	l_reverse_tax_dist.project_accounting_context,
11073 	l_reverse_tax_dist.project_id,
11074 	l_reverse_tax_dist.task_id,
11075 	l_reverse_tax_dist.packet_id,
11076 	l_reverse_tax_dist.awt_flag,
11077 	l_reverse_tax_dist.awt_group_id,
11078 	l_reverse_tax_dist.awt_tax_rate_id,
11079 	l_reverse_tax_dist.awt_gross_amount,
11080 	l_reverse_tax_dist.awt_invoice_id,
11081 	l_reverse_tax_dist.awt_origin_group_id,
11082 	l_reverse_tax_dist.reference_1,
11083 	l_reverse_tax_dist.reference_2,
11084 	l_reverse_tax_dist.org_id,
11085 	l_reverse_tax_dist.awt_invoice_payment_id,
11086 	l_reverse_tax_dist.global_attribute_category,
11087 	l_reverse_tax_dist.global_attribute1,
11088 	l_reverse_tax_dist.global_attribute2,
11089 	l_reverse_tax_dist.global_attribute3,
11090 	l_reverse_tax_dist.global_attribute4,
11091 	l_reverse_tax_dist.global_attribute5,
11092 	l_reverse_tax_dist.global_attribute6,
11093 	l_reverse_tax_dist.global_attribute7,
11094 	l_reverse_tax_dist.global_attribute8,
11095 	l_reverse_tax_dist.global_attribute9,
11096 	l_reverse_tax_dist.global_attribute10,
11097 	l_reverse_tax_dist.global_attribute11,
11098 	l_reverse_tax_dist.global_attribute12,
11099 	l_reverse_tax_dist.global_attribute13,
11100 	l_reverse_tax_dist.global_attribute14,
11101 	l_reverse_tax_dist.global_attribute15,
11102 	l_reverse_tax_dist.global_attribute16,
11103 	l_reverse_tax_dist.global_attribute17,
11104 	l_reverse_tax_dist.global_attribute18,
11105 	l_reverse_tax_dist.global_attribute19,
11106 	l_reverse_tax_dist.global_attribute20,
11107 	l_reverse_tax_dist.receipt_verified_flag,
11108 	l_reverse_tax_dist.receipt_required_flag,
11109 	l_reverse_tax_dist.receipt_missing_flag,
11110 	l_reverse_tax_dist.justification,
11111 	l_reverse_tax_dist.expense_group,
11112 	l_reverse_tax_dist.start_expense_date,
11113 	l_reverse_tax_dist.end_expense_date,
11114 	l_reverse_tax_dist.receipt_currency_code,
11115 	l_reverse_tax_dist.receipt_conversion_rate,
11116 	l_reverse_tax_dist.receipt_currency_amount,
11117 	l_reverse_tax_dist.daily_amount,
11118 	l_reverse_tax_dist.web_parameter_id,
11119 	l_reverse_tax_dist.adjustment_reason,
11120 	l_reverse_tax_dist.award_id,
11121 	l_reverse_tax_dist.credit_card_trx_id,
11122 	l_reverse_tax_dist.dist_match_type,
11123 	l_reverse_tax_dist.rcv_transaction_id,
11124 	l_reverse_tax_dist.invoice_distribution_id,
11125 	l_reverse_tax_dist.parent_reversal_id,
11126 	l_reverse_tax_dist.tax_recoverable_flag,
11127 	l_reverse_tax_dist.merchant_document_number,
11128 	l_reverse_tax_dist.merchant_name,
11129 	l_reverse_tax_dist.merchant_reference,
11130 	l_reverse_tax_dist.merchant_tax_reg_number,
11131 	l_reverse_tax_dist.merchant_taxpayer_id,
11132 	l_reverse_tax_dist.country_of_supply,
11133 	l_reverse_tax_dist.matched_uom_lookup_code,
11134 	l_reverse_tax_dist.gms_burdenable_raw_cost,
11135 	l_reverse_tax_dist.accounting_event_id,
11136 	l_reverse_tax_dist.prepay_distribution_id,
11137 	l_reverse_tax_dist.upgrade_posted_amt,
11138 	l_reverse_tax_dist.upgrade_base_posted_amt,
11139 	l_reverse_tax_dist.inventory_transfer_status,
11140 	l_reverse_tax_dist.company_prepaid_invoice_id,
11141 	l_reverse_tax_dist.cc_reversal_flag,
11142 	l_reverse_tax_dist.awt_withheld_amt,
11143 	l_reverse_tax_dist.pa_cmt_xface_flag,
11144 	l_reverse_tax_dist.cancellation_flag,
11145 	l_reverse_tax_dist.invoice_line_number,
11146 	l_reverse_tax_dist.corrected_invoice_dist_id,
11147 	l_reverse_tax_dist.rounding_amt,
11148 	l_reverse_tax_dist.charge_applicable_to_dist_id,
11149 	l_reverse_tax_dist.corrected_quantity,
11150 	l_reverse_tax_dist.related_id,
11151 	l_reverse_tax_dist.asset_book_type_code,
11152 	l_reverse_tax_dist.asset_category_id,
11153 	l_reverse_tax_dist.distribution_class,
11154 	l_reverse_tax_dist.tax_code_id,
11155 	l_reverse_tax_dist.intended_use,
11156 	l_reverse_tax_dist.detail_tax_dist_id,
11157 	l_reverse_tax_dist.rec_nrec_rate,
11158 	l_reverse_tax_dist.recovery_rate_id,
11159 	l_reverse_tax_dist.recovery_type_code,
11160 	l_reverse_tax_dist.withholding_tax_code_id,
11161 	l_reverse_tax_dist.taxable_amount,
11162 	l_reverse_tax_dist.taxable_base_amount,
11163 	l_reverse_tax_dist.tax_already_distributed_flag,
11164 	l_reverse_tax_dist.summary_tax_line_id,
11165 	l_reverse_tax_dist.rcv_charge_addition_flag,
11166 	l_reverse_tax_dist.prepay_tax_diff_amount) ; -- BUG 7338249
11167 
11168      END LOOP;
11169      CLOSE c_reverse_tax_dist;
11170 
11171      END IF; -- Marker 5
11172 
11173 
11174      IF l_self_assess_tax_dist_exist THEN -- Marker 6
11175 
11176         -----------------------------------------------------------------
11177         l_debug_info := 'Step 3: Insert reverse self assessed tax distributions into ap_invoice_distributions';
11178         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11179             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11180         END IF;
11181         ----------------------------------------------------------------
11182 
11183         OPEN c_rev_self_assess_tax_dist;
11184         LOOP
11185 	    FETCH c_rev_self_assess_tax_dist
11186 	     INTO  l_self_assess_rev_tax_dist;
11187 	     EXIT WHEN c_rev_self_assess_tax_dist%NOTFOUND;
11188 
11189 	   INSERT INTO ap_self_assessed_tax_dist_all (
11190                 accounting_date,
11191                 accrual_posted_flag,
11192                 assets_addition_flag,
11193                 assets_tracking_flag,
11194                 cash_posted_flag,
11195                 distribution_line_number,
11196                 dist_code_combination_id,
11197                 invoice_id,
11198                 last_updated_by,
11199                 last_update_date,
11200                 line_type_lookup_code,
11201                 period_name,
11202                 set_of_books_id,
11203                 amount,
11204                 base_amount,
11205                 --batch_id,
11206                 created_by,
11207                 creation_date,
11208                 description,
11209                 final_match_flag,
11210                 income_tax_region,
11211                 last_update_login,
11212                 match_status_flag,
11213                 posted_flag,
11214                 po_distribution_id,
11215                 program_application_id,
11216                 program_id,
11217                 program_update_date,
11218                 quantity_invoiced,
11219                 request_id,
11220                 reversal_flag,
11221                 type_1099,
11222                 unit_price,
11223                 encumbered_flag,
11224                 stat_amount,
11225                 attribute1,
11226                 attribute10,
11227                 attribute11,
11228                 attribute12,
11229                 attribute13,
11230                 attribute14,
11231                 attribute15,
11232                 attribute2,
11233                 attribute3,
11234                 attribute4,
11235                 attribute5,
11236                 attribute6,
11237                 attribute7,
11238                 attribute8,
11239                 attribute9,
11240                 attribute_category,
11241                 expenditure_item_date,
11242                 expenditure_organization_id,
11243                 expenditure_type,
11244                 parent_invoice_id,
11245                 pa_addition_flag,
11246                 pa_quantity,
11247                 prepay_amount_remaining,
11248                 project_accounting_context,
11249                 project_id,
11250                 task_id,
11251                 packet_id,
11252                 awt_flag,
11253                 awt_group_id,
11254                 awt_tax_rate_id,
11255                 awt_gross_amount,
11256                 awt_invoice_id,
11257                 awt_origin_group_id,
11258                 reference_1,
11259                 reference_2,
11260                 org_id,
11261                 awt_invoice_payment_id,
11262                 global_attribute_category,
11263                 global_attribute1,
11264                 global_attribute2,
11265                 global_attribute3,
11266                 global_attribute4,
11267                 global_attribute5,
11268                 global_attribute6,
11269                 global_attribute7,
11270                 global_attribute8,
11271                 global_attribute9,
11272                 global_attribute10,
11273                 global_attribute11,
11274                 global_attribute12,
11275                 global_attribute13,
11276                 global_attribute14,
11277                 global_attribute15,
11278                 global_attribute16,
11279                 global_attribute17,
11280                 global_attribute18,
11281                 global_attribute19,
11282                 global_attribute20,
11283                 receipt_verified_flag,
11284                 receipt_required_flag,
11285                 receipt_missing_flag,
11286                 justification,
11287                 expense_group,
11288                 start_expense_date,
11289                 end_expense_date,
11290                 receipt_currency_code,
11291                 receipt_conversion_rate,
11292                 receipt_currency_amount,
11293                 daily_amount,
11294                 web_parameter_id,
11295                 adjustment_reason,
11296                 award_id,
11297                 credit_card_trx_id,
11298                 dist_match_type,
11299                 rcv_transaction_id,
11300                 invoice_distribution_id,
11301                 parent_reversal_id,
11302                 tax_recoverable_flag,
11303                 merchant_document_number,
11304                 merchant_name,
11305                 merchant_reference,
11306                 merchant_tax_reg_number,
11307                 merchant_taxpayer_id,
11308                 country_of_supply,
11309                 matched_uom_lookup_code,
11310                 gms_burdenable_raw_cost,
11311                 accounting_event_id,
11312                 prepay_distribution_id,
11313                 upgrade_posted_amt,
11314                 upgrade_base_posted_amt,
11315                 inventory_transfer_status,
11316                 company_prepaid_invoice_id,
11317                 cc_reversal_flag,
11318                 awt_withheld_amt,
11319                 pa_cmt_xface_flag,
11320                 cancellation_flag,
11321                 invoice_line_number,
11322                 corrected_invoice_dist_id,
11323                 rounding_amt,
11324                 charge_applicable_to_dist_id,
11325                 corrected_quantity,
11326                 related_id,
11327                 asset_book_type_code,
11328                 asset_category_id,
11329                 distribution_class,
11330                 tax_code_id,
11331                 intended_use,
11332                 detail_tax_dist_id,
11333                 rec_nrec_rate,
11334                 recovery_rate_id,
11335                 recovery_type_code,
11336                 withholding_tax_code_id,
11337                 taxable_amount,
11338                 taxable_base_amount,
11339                 tax_already_distributed_flag,
11340                 summary_tax_line_id,
11341 		rcv_charge_addition_flag,
11342                 self_assessed_flag,
11343                 self_assessed_tax_liab_ccid, --bug6805655
11344                 prepay_tax_diff_amount -- BUG 7338249
11345                 )
11346 		VALUES
11347 		(
11348 		l_self_assess_rev_tax_dist.accounting_date,
11349 		l_self_assess_rev_tax_dist.accrual_posted_flag,
11350 		l_self_assess_rev_tax_dist.assets_addition_flag,
11351 		l_self_assess_rev_tax_dist.assets_tracking_flag,
11352 		l_self_assess_rev_tax_dist.cash_posted_flag,
11353 		l_self_assess_rev_tax_dist.distribution_line_number,
11354 		l_self_assess_rev_tax_dist.dist_code_combination_id,
11355 		l_self_assess_rev_tax_dist.invoice_id,
11356 		l_self_assess_rev_tax_dist.last_updated_by,
11357 		l_self_assess_rev_tax_dist.last_update_date,
11358 		l_self_assess_rev_tax_dist.line_type_lookup_code,
11359 		l_self_assess_rev_tax_dist.period_name,
11360 		l_self_assess_rev_tax_dist.set_of_books_id,
11361 		l_self_assess_rev_tax_dist.amount,
11362 		l_self_assess_rev_tax_dist.base_amount,
11363 		--l_self_assess_rev_tax_dist.batch_id,
11364 		l_self_assess_rev_tax_dist.created_by,
11365 		l_self_assess_rev_tax_dist.creation_date,
11366 		l_self_assess_rev_tax_dist.description,
11367 		l_self_assess_rev_tax_dist.final_match_flag,
11368 		l_self_assess_rev_tax_dist.income_tax_region,
11369 		l_self_assess_rev_tax_dist.last_update_login,
11370 		l_self_assess_rev_tax_dist.match_status_flag,
11371 		l_self_assess_rev_tax_dist.posted_flag,
11372 		l_self_assess_rev_tax_dist.po_distribution_id,
11373 		l_self_assess_rev_tax_dist.program_application_id,
11374 		l_self_assess_rev_tax_dist.program_id,
11375 		l_self_assess_rev_tax_dist.program_update_date,
11376 		l_self_assess_rev_tax_dist.quantity_invoiced,
11377 		l_self_assess_rev_tax_dist.request_id,
11378 		l_self_assess_rev_tax_dist.reversal_flag,
11379 		l_self_assess_rev_tax_dist.type_1099,
11380 		l_self_assess_rev_tax_dist.unit_price,
11381 		l_self_assess_rev_tax_dist.encumbered_flag,
11382 		l_self_assess_rev_tax_dist.stat_amount,
11383 		l_self_assess_rev_tax_dist.attribute1,
11384 		l_self_assess_rev_tax_dist.attribute10,
11385 		l_self_assess_rev_tax_dist.attribute11,
11386 		l_self_assess_rev_tax_dist.attribute12,
11387 		l_self_assess_rev_tax_dist.attribute13,
11388 		l_self_assess_rev_tax_dist.attribute14,
11389 		l_self_assess_rev_tax_dist.attribute15,
11390 		l_self_assess_rev_tax_dist.attribute2,
11391 		l_self_assess_rev_tax_dist.attribute3,
11392 		l_self_assess_rev_tax_dist.attribute4,
11393 		l_self_assess_rev_tax_dist.attribute5,
11394 		l_self_assess_rev_tax_dist.attribute6,
11395 		l_self_assess_rev_tax_dist.attribute7,
11396 		l_self_assess_rev_tax_dist.attribute8,
11397 		l_self_assess_rev_tax_dist.attribute9,
11398 		l_self_assess_rev_tax_dist.attribute_category,
11399 		l_self_assess_rev_tax_dist.expenditure_item_date,
11400 		l_self_assess_rev_tax_dist.expenditure_organization_id,
11401 		l_self_assess_rev_tax_dist.expenditure_type,
11402 		l_self_assess_rev_tax_dist.parent_invoice_id,
11403 		l_self_assess_rev_tax_dist.pa_addition_flag,
11404 		l_self_assess_rev_tax_dist.pa_quantity,
11405 		l_self_assess_rev_tax_dist.prepay_amount_remaining,
11406 		l_self_assess_rev_tax_dist.project_accounting_context,
11407 		l_self_assess_rev_tax_dist.project_id,
11408 		l_self_assess_rev_tax_dist.task_id,
11409 		l_self_assess_rev_tax_dist.packet_id,
11410 		l_self_assess_rev_tax_dist.awt_flag,
11411 		l_self_assess_rev_tax_dist.awt_group_id,
11412 		l_self_assess_rev_tax_dist.awt_tax_rate_id,
11413 		l_self_assess_rev_tax_dist.awt_gross_amount,
11414 		l_self_assess_rev_tax_dist.awt_invoice_id,
11415 		l_self_assess_rev_tax_dist.awt_origin_group_id,
11416 		l_self_assess_rev_tax_dist.reference_1,
11417 		l_self_assess_rev_tax_dist.reference_2,
11418 		l_self_assess_rev_tax_dist.org_id,
11419 		l_self_assess_rev_tax_dist.awt_invoice_payment_id,
11420 		l_self_assess_rev_tax_dist.global_attribute_category,
11421 		l_self_assess_rev_tax_dist.global_attribute1,
11422 		l_self_assess_rev_tax_dist.global_attribute2,
11423 		l_self_assess_rev_tax_dist.global_attribute3,
11424 		l_self_assess_rev_tax_dist.global_attribute4,
11425 		l_self_assess_rev_tax_dist.global_attribute5,
11426 		l_self_assess_rev_tax_dist.global_attribute6,
11427 		l_self_assess_rev_tax_dist.global_attribute7,
11428 		l_self_assess_rev_tax_dist.global_attribute8,
11429 		l_self_assess_rev_tax_dist.global_attribute9,
11430 		l_self_assess_rev_tax_dist.global_attribute10,
11431 		l_self_assess_rev_tax_dist.global_attribute11,
11432 		l_self_assess_rev_tax_dist.global_attribute12,
11433 		l_self_assess_rev_tax_dist.global_attribute13,
11434 		l_self_assess_rev_tax_dist.global_attribute14,
11435 		l_self_assess_rev_tax_dist.global_attribute15,
11436 		l_self_assess_rev_tax_dist.global_attribute16,
11437 		l_self_assess_rev_tax_dist.global_attribute17,
11438 		l_self_assess_rev_tax_dist.global_attribute18,
11439 		l_self_assess_rev_tax_dist.global_attribute19,
11440 		l_self_assess_rev_tax_dist.global_attribute20,
11441 		l_self_assess_rev_tax_dist.receipt_verified_flag,
11442 		l_self_assess_rev_tax_dist.receipt_required_flag,
11443 		l_self_assess_rev_tax_dist.receipt_missing_flag,
11444 		l_self_assess_rev_tax_dist.justification,
11445 		l_self_assess_rev_tax_dist.expense_group,
11446 		l_self_assess_rev_tax_dist.start_expense_date,
11447 		l_self_assess_rev_tax_dist.end_expense_date,
11448 		l_self_assess_rev_tax_dist.receipt_currency_code,
11449 		l_self_assess_rev_tax_dist.receipt_conversion_rate,
11450 		l_self_assess_rev_tax_dist.receipt_currency_amount,
11451 		l_self_assess_rev_tax_dist.daily_amount,
11452 		l_self_assess_rev_tax_dist.web_parameter_id,
11453 		l_self_assess_rev_tax_dist.adjustment_reason,
11454 		l_self_assess_rev_tax_dist.award_id,
11455 		l_self_assess_rev_tax_dist.credit_card_trx_id,
11456 		l_self_assess_rev_tax_dist.dist_match_type,
11457 		l_self_assess_rev_tax_dist.rcv_transaction_id,
11458 		l_self_assess_rev_tax_dist.invoice_distribution_id,
11459 		l_self_assess_rev_tax_dist.parent_reversal_id,
11460 		l_self_assess_rev_tax_dist.tax_recoverable_flag,
11461 		l_self_assess_rev_tax_dist.merchant_document_number,
11462 		l_self_assess_rev_tax_dist.merchant_name,
11463 		l_self_assess_rev_tax_dist.merchant_reference,
11464 		l_self_assess_rev_tax_dist.merchant_tax_reg_number,
11465 		l_self_assess_rev_tax_dist.merchant_taxpayer_id,
11466 		l_self_assess_rev_tax_dist.country_of_supply,
11467 		l_self_assess_rev_tax_dist.matched_uom_lookup_code,
11468 		l_self_assess_rev_tax_dist.gms_burdenable_raw_cost,
11469 		l_self_assess_rev_tax_dist.accounting_event_id,
11470 		l_self_assess_rev_tax_dist.prepay_distribution_id,
11471 		l_self_assess_rev_tax_dist.upgrade_posted_amt,
11472 		l_self_assess_rev_tax_dist.upgrade_base_posted_amt,
11473 		l_self_assess_rev_tax_dist.inventory_transfer_status,
11474 		l_self_assess_rev_tax_dist.company_prepaid_invoice_id,
11475 		l_self_assess_rev_tax_dist.cc_reversal_flag,
11476 		l_self_assess_rev_tax_dist.awt_withheld_amt,
11477 		l_self_assess_rev_tax_dist.pa_cmt_xface_flag,
11478 		l_self_assess_rev_tax_dist.cancellation_flag,
11479 		l_self_assess_rev_tax_dist.invoice_line_number,
11480 		l_self_assess_rev_tax_dist.corrected_invoice_dist_id,
11481 		l_self_assess_rev_tax_dist.rounding_amt,
11482 		l_self_assess_rev_tax_dist.charge_applicable_to_dist_id,
11483 		l_self_assess_rev_tax_dist.corrected_quantity,
11484 		l_self_assess_rev_tax_dist.related_id,
11485 		l_self_assess_rev_tax_dist.asset_book_type_code,
11486 		l_self_assess_rev_tax_dist.asset_category_id,
11487 		l_self_assess_rev_tax_dist.distribution_class,
11488 		l_self_assess_rev_tax_dist.tax_code_id,
11489 		l_self_assess_rev_tax_dist.intended_use,
11490 		l_self_assess_rev_tax_dist.detail_tax_dist_id,
11491 		l_self_assess_rev_tax_dist.rec_nrec_rate,
11492 		l_self_assess_rev_tax_dist.recovery_rate_id,
11493 		l_self_assess_rev_tax_dist.recovery_type_code,
11494 		l_self_assess_rev_tax_dist.withholding_tax_code_id,
11495 		l_self_assess_rev_tax_dist.taxable_amount,
11496 		l_self_assess_rev_tax_dist.taxable_base_amount,
11497 		l_self_assess_rev_tax_dist.tax_already_distributed_flag,
11498 		l_self_assess_rev_tax_dist.summary_tax_line_id,
11499 		l_self_assess_rev_tax_dist.rcv_charge_addition_flag,
11500 		l_self_assess_rev_tax_dist.self_assessed_flag,
11501 		l_self_assess_rev_tax_dist.self_assessed_tax_liab_ccid,  --bug6805655
11502         l_self_assess_rev_tax_dist.prepay_tax_diff_amount -- BUG 7338249
11503                );
11504 
11505          END LOOP;
11506          CLOSE c_rev_self_assess_tax_dist;
11507 
11508 	 END IF; -- Marker 6
11509 
11510     else -- Marker 4
11511 
11512          IF l_tax_distributions_exist THEN -- Marker 7
11513 
11514          OPEN c_reverse_tax_dist_1;
11515          LOOP
11516 	     FETCH c_reverse_tax_dist_1
11517 	     INTO  l_reverse_tax_dist;
11518 	     EXIT WHEN c_reverse_tax_dist_1%NOTFOUND;
11519 
11520 	     INSERT INTO ap_invoice_distributions_all (
11521             accounting_date,
11522             accrual_posted_flag,
11523             assets_addition_flag,
11524             assets_tracking_flag,
11525             cash_posted_flag,
11526             distribution_line_number,
11527             dist_code_combination_id,
11528             invoice_id,
11529             last_updated_by,
11530             last_update_date,
11531             line_type_lookup_code,
11532             period_name,
11533             set_of_books_id,
11534             amount,
11535             base_amount,
11536             --batch_id,
11537             created_by,
11538             creation_date,
11539             description,
11540             final_match_flag,
11541             income_tax_region,
11542             last_update_login,
11543             match_status_flag,
11544             posted_flag,
11545             po_distribution_id,
11546             program_application_id,
11547             program_id,
11548             program_update_date,
11549             quantity_invoiced,
11550             request_id,
11551             reversal_flag,
11552             type_1099,
11553             unit_price,
11554             encumbered_flag,
11555             stat_amount,
11556             attribute1,
11557             attribute10,
11558             attribute11,
11559             attribute12,
11560             attribute13,
11561             attribute14,
11562             attribute15,
11563             attribute2,
11564             attribute3,
11565             attribute4,
11566             attribute5,
11567             attribute6,
11568             attribute7,
11569             attribute8,
11570             attribute9,
11571             attribute_category,
11572             expenditure_item_date,
11573             expenditure_organization_id,
11574             expenditure_type,
11575             parent_invoice_id,
11576             pa_addition_flag,
11577             pa_quantity,
11578             prepay_amount_remaining,
11579             project_accounting_context,
11580             project_id,
11581             task_id,
11582             packet_id,
11583             awt_flag,
11584             awt_group_id,
11585             awt_tax_rate_id,
11586             awt_gross_amount,
11587             awt_invoice_id,
11588             awt_origin_group_id,
11589             reference_1,
11590             reference_2,
11591             org_id,
11592             awt_invoice_payment_id,
11593             global_attribute_category,
11594             global_attribute1,
11595             global_attribute2,
11596             global_attribute3,
11597             global_attribute4,
11598             global_attribute5,
11599             global_attribute6,
11600             global_attribute7,
11601             global_attribute8,
11602             global_attribute9,
11603             global_attribute10,
11604             global_attribute11,
11605             global_attribute12,
11606             global_attribute13,
11607             global_attribute14,
11608             global_attribute15,
11609             global_attribute16,
11610             global_attribute17,
11611             global_attribute18,
11612             global_attribute19,
11613             global_attribute20,
11614             receipt_verified_flag,
11615             receipt_required_flag,
11616             receipt_missing_flag,
11617             justification,
11618             expense_group,
11619             start_expense_date,
11620             end_expense_date,
11621             receipt_currency_code,
11622             receipt_conversion_rate,
11623             receipt_currency_amount,
11624             daily_amount,
11625             web_parameter_id,
11626             adjustment_reason,
11627             award_id,
11628             credit_card_trx_id,
11629             dist_match_type,
11630             rcv_transaction_id,
11631             invoice_distribution_id,
11632             parent_reversal_id,
11633             tax_recoverable_flag,
11634             merchant_document_number,
11635             merchant_name,
11636             merchant_reference,
11637             merchant_tax_reg_number,
11638             merchant_taxpayer_id,
11639             country_of_supply,
11640             matched_uom_lookup_code,
11641             gms_burdenable_raw_cost,
11642             accounting_event_id,
11643             prepay_distribution_id,
11644             upgrade_posted_amt,
11645             upgrade_base_posted_amt,
11646             inventory_transfer_status,
11647             company_prepaid_invoice_id,
11648             cc_reversal_flag,
11649             awt_withheld_amt,
11650             pa_cmt_xface_flag,
11651             cancellation_flag,
11652             invoice_line_number,
11653             corrected_invoice_dist_id,
11654             rounding_amt,
11655             charge_applicable_to_dist_id,
11656             corrected_quantity,
11657             related_id,
11658             asset_book_type_code,
11659             asset_category_id,
11660             distribution_class,
11661             tax_code_id,
11662             intended_use,
11663             detail_tax_dist_id,
11664             rec_nrec_rate,
11665             recovery_rate_id,
11666             recovery_type_code,
11667             withholding_tax_code_id,
11668             taxable_amount,
11669             taxable_base_amount,
11670             tax_already_distributed_flag,
11671             summary_tax_line_id,
11672 	        rcv_charge_addition_flag,
11673             prepay_tax_diff_amount) -- BUG 7338249
11674 	VALUES
11675 	(
11676 	l_reverse_tax_dist.accounting_date,
11677 	l_reverse_tax_dist.accrual_posted_flag,
11678 	l_reverse_tax_dist.assets_addition_flag,
11679 	l_reverse_tax_dist.assets_tracking_flag,
11680 	l_reverse_tax_dist.cash_posted_flag,
11681 	l_reverse_tax_dist.distribution_line_number,
11682 	l_reverse_tax_dist.dist_code_combination_id,
11683 	l_reverse_tax_dist.invoice_id,
11684 	l_reverse_tax_dist.last_updated_by,
11685 	l_reverse_tax_dist.last_update_date,
11686 	l_reverse_tax_dist.line_type_lookup_code,
11687 	l_reverse_tax_dist.period_name,
11688 	l_reverse_tax_dist.set_of_books_id,
11689 	l_reverse_tax_dist.amount,
11690 	l_reverse_tax_dist.base_amount,
11691 	--l_reverse_tax_dist.batch_id,
11692 	l_reverse_tax_dist.created_by,
11693 	l_reverse_tax_dist.creation_date,
11694 	l_reverse_tax_dist.description,
11695 	l_reverse_tax_dist.final_match_flag,
11696 	l_reverse_tax_dist.income_tax_region,
11697 	l_reverse_tax_dist.last_update_login,
11698 	l_reverse_tax_dist.match_status_flag,
11699 	l_reverse_tax_dist.posted_flag,
11700 	l_reverse_tax_dist.po_distribution_id,
11701 	l_reverse_tax_dist.program_application_id,
11702 	l_reverse_tax_dist.program_id,
11703 	l_reverse_tax_dist.program_update_date,
11704 	l_reverse_tax_dist.quantity_invoiced,
11705 	l_reverse_tax_dist.request_id,
11706 	l_reverse_tax_dist.reversal_flag,
11707 	l_reverse_tax_dist.type_1099,
11708 	l_reverse_tax_dist.unit_price,
11709 	l_reverse_tax_dist.encumbered_flag,
11710 	l_reverse_tax_dist.stat_amount,
11711 	l_reverse_tax_dist.attribute1,
11712 	l_reverse_tax_dist.attribute10,
11713 	l_reverse_tax_dist.attribute11,
11714 	l_reverse_tax_dist.attribute12,
11715 	l_reverse_tax_dist.attribute13,
11716 	l_reverse_tax_dist.attribute14,
11717 	l_reverse_tax_dist.attribute15,
11718 	l_reverse_tax_dist.attribute2,
11719 	l_reverse_tax_dist.attribute3,
11720 	l_reverse_tax_dist.attribute4,
11721 	l_reverse_tax_dist.attribute5,
11722 	l_reverse_tax_dist.attribute6,
11723 	l_reverse_tax_dist.attribute7,
11724 	l_reverse_tax_dist.attribute8,
11725 	l_reverse_tax_dist.attribute9,
11726 	l_reverse_tax_dist.attribute_category,
11727 	l_reverse_tax_dist.expenditure_item_date,
11728 	l_reverse_tax_dist.expenditure_organization_id,
11729 	l_reverse_tax_dist.expenditure_type,
11730 	l_reverse_tax_dist.parent_invoice_id,
11731 	l_reverse_tax_dist.pa_addition_flag,
11732 	l_reverse_tax_dist.pa_quantity,
11733 	l_reverse_tax_dist.prepay_amount_remaining,
11734 	l_reverse_tax_dist.project_accounting_context,
11735 	l_reverse_tax_dist.project_id,
11736 	l_reverse_tax_dist.task_id,
11737 	l_reverse_tax_dist.packet_id,
11738 	l_reverse_tax_dist.awt_flag,
11739 	l_reverse_tax_dist.awt_group_id,
11740 	l_reverse_tax_dist.awt_tax_rate_id,
11741 	l_reverse_tax_dist.awt_gross_amount,
11742 	l_reverse_tax_dist.awt_invoice_id,
11743 	l_reverse_tax_dist.awt_origin_group_id,
11744 	l_reverse_tax_dist.reference_1,
11745 	l_reverse_tax_dist.reference_2,
11746 	l_reverse_tax_dist.org_id,
11747 	l_reverse_tax_dist.awt_invoice_payment_id,
11748 	l_reverse_tax_dist.global_attribute_category,
11749 	l_reverse_tax_dist.global_attribute1,
11750 	l_reverse_tax_dist.global_attribute2,
11751 	l_reverse_tax_dist.global_attribute3,
11752 	l_reverse_tax_dist.global_attribute4,
11753 	l_reverse_tax_dist.global_attribute5,
11754 	l_reverse_tax_dist.global_attribute6,
11755 	l_reverse_tax_dist.global_attribute7,
11756 	l_reverse_tax_dist.global_attribute8,
11757 	l_reverse_tax_dist.global_attribute9,
11758 	l_reverse_tax_dist.global_attribute10,
11759 	l_reverse_tax_dist.global_attribute11,
11760 	l_reverse_tax_dist.global_attribute12,
11761 	l_reverse_tax_dist.global_attribute13,
11762 	l_reverse_tax_dist.global_attribute14,
11763 	l_reverse_tax_dist.global_attribute15,
11764 	l_reverse_tax_dist.global_attribute16,
11765 	l_reverse_tax_dist.global_attribute17,
11766 	l_reverse_tax_dist.global_attribute18,
11767 	l_reverse_tax_dist.global_attribute19,
11768 	l_reverse_tax_dist.global_attribute20,
11769 	l_reverse_tax_dist.receipt_verified_flag,
11770 	l_reverse_tax_dist.receipt_required_flag,
11771 	l_reverse_tax_dist.receipt_missing_flag,
11772 	l_reverse_tax_dist.justification,
11773 	l_reverse_tax_dist.expense_group,
11774 	l_reverse_tax_dist.start_expense_date,
11775 	l_reverse_tax_dist.end_expense_date,
11776 	l_reverse_tax_dist.receipt_currency_code,
11777 	l_reverse_tax_dist.receipt_conversion_rate,
11778 	l_reverse_tax_dist.receipt_currency_amount,
11779 	l_reverse_tax_dist.daily_amount,
11780 	l_reverse_tax_dist.web_parameter_id,
11781 	l_reverse_tax_dist.adjustment_reason,
11782 	l_reverse_tax_dist.award_id,
11783 	l_reverse_tax_dist.credit_card_trx_id,
11784 	l_reverse_tax_dist.dist_match_type,
11785 	l_reverse_tax_dist.rcv_transaction_id,
11786 	l_reverse_tax_dist.invoice_distribution_id,
11787 	l_reverse_tax_dist.parent_reversal_id,
11788 	l_reverse_tax_dist.tax_recoverable_flag,
11789 	l_reverse_tax_dist.merchant_document_number,
11790 	l_reverse_tax_dist.merchant_name,
11791 	l_reverse_tax_dist.merchant_reference,
11792 	l_reverse_tax_dist.merchant_tax_reg_number,
11793 	l_reverse_tax_dist.merchant_taxpayer_id,
11794 	l_reverse_tax_dist.country_of_supply,
11795 	l_reverse_tax_dist.matched_uom_lookup_code,
11796 	l_reverse_tax_dist.gms_burdenable_raw_cost,
11797 	l_reverse_tax_dist.accounting_event_id,
11798 	l_reverse_tax_dist.prepay_distribution_id,
11799 	l_reverse_tax_dist.upgrade_posted_amt,
11800 	l_reverse_tax_dist.upgrade_base_posted_amt,
11801 	l_reverse_tax_dist.inventory_transfer_status,
11802 	l_reverse_tax_dist.company_prepaid_invoice_id,
11803 	l_reverse_tax_dist.cc_reversal_flag,
11804 	l_reverse_tax_dist.awt_withheld_amt,
11805 	l_reverse_tax_dist.pa_cmt_xface_flag,
11806 	l_reverse_tax_dist.cancellation_flag,
11807 	l_reverse_tax_dist.invoice_line_number,
11808 	l_reverse_tax_dist.corrected_invoice_dist_id,
11809 	l_reverse_tax_dist.rounding_amt,
11810 	l_reverse_tax_dist.charge_applicable_to_dist_id,
11811 	l_reverse_tax_dist.corrected_quantity,
11812 	l_reverse_tax_dist.related_id,
11813 	l_reverse_tax_dist.asset_book_type_code,
11814 	l_reverse_tax_dist.asset_category_id,
11815 	l_reverse_tax_dist.distribution_class,
11816 	l_reverse_tax_dist.tax_code_id,
11817 	l_reverse_tax_dist.intended_use,
11818 	l_reverse_tax_dist.detail_tax_dist_id,
11819 	l_reverse_tax_dist.rec_nrec_rate,
11820 	l_reverse_tax_dist.recovery_rate_id,
11821 	l_reverse_tax_dist.recovery_type_code,
11822 	l_reverse_tax_dist.withholding_tax_code_id,
11823 	l_reverse_tax_dist.taxable_amount,
11824 	l_reverse_tax_dist.taxable_base_amount,
11825 	l_reverse_tax_dist.tax_already_distributed_flag,
11826 	l_reverse_tax_dist.summary_tax_line_id,
11827 	l_reverse_tax_dist.rcv_charge_addition_flag,
11828 	l_reverse_tax_dist.prepay_tax_diff_amount -- BUG 7338249
11829     );
11830 
11831      END LOOP;
11832      CLOSE c_reverse_tax_dist_1;
11833 
11834     END IF; -- Marker 7
11835 
11836 
11837     IF l_self_assess_tax_dist_exist THEN -- Marker 8
11838 
11839        l_debug_info := 'Inserting reverse entries into ap_self_assessed_tax_dist_all';
11840        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11841           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11842        END IF;
11843 
11844        OPEN c_rev_self_assess_tax_dist_1;
11845        LOOP
11846            FETCH c_rev_self_assess_tax_dist_1
11847 	    INTO  l_self_assess_rev_tax_dist_1;
11848 	    EXIT WHEN c_rev_self_assess_tax_dist_1%NOTFOUND;
11849 
11850 	   INSERT INTO ap_self_assessed_tax_dist_all (
11851                 accounting_date,
11852                 accrual_posted_flag,
11853                 assets_addition_flag,
11854                 assets_tracking_flag,
11855                 cash_posted_flag,
11856                 distribution_line_number,
11857                 dist_code_combination_id,
11858                 invoice_id,
11859                 last_updated_by,
11860                 last_update_date,
11861                 line_type_lookup_code,
11862                 period_name,
11863                 set_of_books_id,
11864                 amount,
11865                 base_amount,
11866                 --batch_id,
11867                 created_by,
11868                 creation_date,
11869                 description,
11870                 final_match_flag,
11871                 income_tax_region,
11872                 last_update_login,
11873                 match_status_flag,
11874                 posted_flag,
11875                 po_distribution_id,
11876                 program_application_id,
11877                 program_id,
11878                 program_update_date,
11879                 quantity_invoiced,
11880                 request_id,
11881                 reversal_flag,
11882                 type_1099,
11883                 unit_price,
11884                 encumbered_flag,
11885                 stat_amount,
11886                 attribute1,
11887                 attribute10,
11888                 attribute11,
11889                 attribute12,
11890                 attribute13,
11891                 attribute14,
11892                 attribute15,
11893                 attribute2,
11894                 attribute3,
11895                 attribute4,
11896                 attribute5,
11897                 attribute6,
11898                 attribute7,
11899                 attribute8,
11900                 attribute9,
11901                 attribute_category,
11902                 expenditure_item_date,
11903                 expenditure_organization_id,
11904                 expenditure_type,
11905                 parent_invoice_id,
11906                 pa_addition_flag,
11907                 pa_quantity,
11908                 prepay_amount_remaining,
11909                 project_accounting_context,
11910                 project_id,
11911                 task_id,
11912                 packet_id,
11913                 awt_flag,
11914                 awt_group_id,
11915                 awt_tax_rate_id,
11916                 awt_gross_amount,
11917                 awt_invoice_id,
11918                 awt_origin_group_id,
11919                 reference_1,
11920                 reference_2,
11921                 org_id,
11922                 awt_invoice_payment_id,
11923                 global_attribute_category,
11924                 global_attribute1,
11925                 global_attribute2,
11926                 global_attribute3,
11927                 global_attribute4,
11928                 global_attribute5,
11929                 global_attribute6,
11930                 global_attribute7,
11931                 global_attribute8,
11932                 global_attribute9,
11933                 global_attribute10,
11934                 global_attribute11,
11935                 global_attribute12,
11936                 global_attribute13,
11937                 global_attribute14,
11938                 global_attribute15,
11939                 global_attribute16,
11940                 global_attribute17,
11941                 global_attribute18,
11942                 global_attribute19,
11943                 global_attribute20,
11944                 receipt_verified_flag,
11945                 receipt_required_flag,
11946                 receipt_missing_flag,
11947                 justification,
11948                 expense_group,
11949                 start_expense_date,
11950                 end_expense_date,
11951                 receipt_currency_code,
11952                 receipt_conversion_rate,
11953                 receipt_currency_amount,
11954                 daily_amount,
11955                 web_parameter_id,
11956                 adjustment_reason,
11957                 award_id,
11958                 credit_card_trx_id,
11959                 dist_match_type,
11960                 rcv_transaction_id,
11961                 invoice_distribution_id,
11962                 parent_reversal_id,
11963                 tax_recoverable_flag,
11964                 merchant_document_number,
11965                 merchant_name,
11966                 merchant_reference,
11967                 merchant_tax_reg_number,
11968                 merchant_taxpayer_id,
11969                 country_of_supply,
11970                 matched_uom_lookup_code,
11971                 gms_burdenable_raw_cost,
11972                 accounting_event_id,
11973                 prepay_distribution_id,
11974                 upgrade_posted_amt,
11975                 upgrade_base_posted_amt,
11976                 inventory_transfer_status,
11977                 company_prepaid_invoice_id,
11978                 cc_reversal_flag,
11979                 awt_withheld_amt,
11980                 pa_cmt_xface_flag,
11981                 cancellation_flag,
11982                 invoice_line_number,
11983                 corrected_invoice_dist_id,
11984                 rounding_amt,
11985                 charge_applicable_to_dist_id,
11986                 corrected_quantity,
11987                 related_id,
11988                 asset_book_type_code,
11989                 asset_category_id,
11990                 distribution_class,
11991                 tax_code_id,
11992                 intended_use,
11993                 detail_tax_dist_id,
11994                 rec_nrec_rate,
11995                 recovery_rate_id,
11996                 recovery_type_code,
11997                 withholding_tax_code_id,
11998                 taxable_amount,
11999                 taxable_base_amount,
12000                 tax_already_distributed_flag,
12001                 summary_tax_line_id,
12002 		        rcv_charge_addition_flag,
12003                 self_assessed_flag,
12004                 self_assessed_tax_liab_ccid,  --bug6805655
12005                 prepay_tax_diff_amount -- BUG 7338249
12006                 )
12007 		VALUES
12008 		(
12009 		l_self_assess_rev_tax_dist_1.accounting_date,
12010 		l_self_assess_rev_tax_dist_1.accrual_posted_flag,
12011 		l_self_assess_rev_tax_dist_1.assets_addition_flag,
12012 		l_self_assess_rev_tax_dist_1.assets_tracking_flag,
12013 		l_self_assess_rev_tax_dist_1.cash_posted_flag,
12014 		l_self_assess_rev_tax_dist_1.distribution_line_number,
12015 		l_self_assess_rev_tax_dist_1.dist_code_combination_id,
12016 		l_self_assess_rev_tax_dist_1.invoice_id,
12017 		l_self_assess_rev_tax_dist_1.last_updated_by,
12018 		l_self_assess_rev_tax_dist_1.last_update_date,
12019 		l_self_assess_rev_tax_dist_1.line_type_lookup_code,
12020 		l_self_assess_rev_tax_dist_1.period_name,
12021 		l_self_assess_rev_tax_dist_1.set_of_books_id,
12022 		l_self_assess_rev_tax_dist_1.amount,
12023 		l_self_assess_rev_tax_dist_1.base_amount,
12024 		--l_self_assess_rev_tax_dist_1.batch_id,
12025 		l_self_assess_rev_tax_dist_1.created_by,
12026 		l_self_assess_rev_tax_dist_1.creation_date,
12027 		l_self_assess_rev_tax_dist_1.description,
12028 		l_self_assess_rev_tax_dist_1.final_match_flag,
12029 		l_self_assess_rev_tax_dist_1.income_tax_region,
12030 		l_self_assess_rev_tax_dist_1.last_update_login,
12031 		l_self_assess_rev_tax_dist_1.match_status_flag,
12032 		l_self_assess_rev_tax_dist_1.posted_flag,
12033 		l_self_assess_rev_tax_dist_1.po_distribution_id,
12034 		l_self_assess_rev_tax_dist_1.program_application_id,
12035 		l_self_assess_rev_tax_dist_1.program_id,
12036 		l_self_assess_rev_tax_dist_1.program_update_date,
12037 		l_self_assess_rev_tax_dist_1.quantity_invoiced,
12038 		l_self_assess_rev_tax_dist_1.request_id,
12039 		l_self_assess_rev_tax_dist_1.reversal_flag,
12040 		l_self_assess_rev_tax_dist_1.type_1099,
12041 		l_self_assess_rev_tax_dist_1.unit_price,
12042 		l_self_assess_rev_tax_dist_1.encumbered_flag,
12043 		l_self_assess_rev_tax_dist_1.stat_amount,
12044 		l_self_assess_rev_tax_dist_1.attribute1,
12045 		l_self_assess_rev_tax_dist_1.attribute10,
12046 		l_self_assess_rev_tax_dist_1.attribute11,
12047 		l_self_assess_rev_tax_dist_1.attribute12,
12048 		l_self_assess_rev_tax_dist_1.attribute13,
12049 		l_self_assess_rev_tax_dist_1.attribute14,
12050 		l_self_assess_rev_tax_dist_1.attribute15,
12051 		l_self_assess_rev_tax_dist_1.attribute2,
12052 		l_self_assess_rev_tax_dist_1.attribute3,
12053 		l_self_assess_rev_tax_dist_1.attribute4,
12054 		l_self_assess_rev_tax_dist_1.attribute5,
12055 		l_self_assess_rev_tax_dist_1.attribute6,
12056 		l_self_assess_rev_tax_dist_1.attribute7,
12057 		l_self_assess_rev_tax_dist_1.attribute8,
12058 		l_self_assess_rev_tax_dist_1.attribute9,
12059 		l_self_assess_rev_tax_dist_1.attribute_category,
12060 		l_self_assess_rev_tax_dist_1.expenditure_item_date,
12061 		l_self_assess_rev_tax_dist_1.expenditure_organization_id,
12062 		l_self_assess_rev_tax_dist_1.expenditure_type,
12063 		l_self_assess_rev_tax_dist_1.parent_invoice_id,
12064 		l_self_assess_rev_tax_dist_1.pa_addition_flag,
12065 		l_self_assess_rev_tax_dist_1.pa_quantity,
12066 		l_self_assess_rev_tax_dist_1.prepay_amount_remaining,
12067 		l_self_assess_rev_tax_dist_1.project_accounting_context,
12068 		l_self_assess_rev_tax_dist_1.project_id,
12069 		l_self_assess_rev_tax_dist_1.task_id,
12070 		l_self_assess_rev_tax_dist_1.packet_id,
12071 		l_self_assess_rev_tax_dist_1.awt_flag,
12072 		l_self_assess_rev_tax_dist_1.awt_group_id,
12073 		l_self_assess_rev_tax_dist_1.awt_tax_rate_id,
12074 		l_self_assess_rev_tax_dist_1.awt_gross_amount,
12075 		l_self_assess_rev_tax_dist_1.awt_invoice_id,
12076 		l_self_assess_rev_tax_dist_1.awt_origin_group_id,
12077 		l_self_assess_rev_tax_dist_1.reference_1,
12078 		l_self_assess_rev_tax_dist_1.reference_2,
12079 		l_self_assess_rev_tax_dist_1.org_id,
12080 		l_self_assess_rev_tax_dist_1.awt_invoice_payment_id,
12081 		l_self_assess_rev_tax_dist_1.global_attribute_category,
12082 		l_self_assess_rev_tax_dist_1.global_attribute1,
12083 		l_self_assess_rev_tax_dist_1.global_attribute2,
12084 		l_self_assess_rev_tax_dist_1.global_attribute3,
12085 		l_self_assess_rev_tax_dist_1.global_attribute4,
12086 		l_self_assess_rev_tax_dist_1.global_attribute5,
12087 		l_self_assess_rev_tax_dist_1.global_attribute6,
12088 		l_self_assess_rev_tax_dist_1.global_attribute7,
12089 		l_self_assess_rev_tax_dist_1.global_attribute8,
12090 		l_self_assess_rev_tax_dist_1.global_attribute9,
12091 		l_self_assess_rev_tax_dist_1.global_attribute10,
12092 		l_self_assess_rev_tax_dist_1.global_attribute11,
12093 		l_self_assess_rev_tax_dist_1.global_attribute12,
12094 		l_self_assess_rev_tax_dist_1.global_attribute13,
12095 		l_self_assess_rev_tax_dist_1.global_attribute14,
12096 		l_self_assess_rev_tax_dist_1.global_attribute15,
12097 		l_self_assess_rev_tax_dist_1.global_attribute16,
12098 		l_self_assess_rev_tax_dist_1.global_attribute17,
12099 		l_self_assess_rev_tax_dist_1.global_attribute18,
12100 		l_self_assess_rev_tax_dist_1.global_attribute19,
12101 		l_self_assess_rev_tax_dist_1.global_attribute20,
12102 		l_self_assess_rev_tax_dist_1.receipt_verified_flag,
12103 		l_self_assess_rev_tax_dist_1.receipt_required_flag,
12104 		l_self_assess_rev_tax_dist_1.receipt_missing_flag,
12105 		l_self_assess_rev_tax_dist_1.justification,
12106 		l_self_assess_rev_tax_dist_1.expense_group,
12107 		l_self_assess_rev_tax_dist_1.start_expense_date,
12108 		l_self_assess_rev_tax_dist_1.end_expense_date,
12109 		l_self_assess_rev_tax_dist_1.receipt_currency_code,
12110 		l_self_assess_rev_tax_dist_1.receipt_conversion_rate,
12111 		l_self_assess_rev_tax_dist_1.receipt_currency_amount,
12112 		l_self_assess_rev_tax_dist_1.daily_amount,
12113 		l_self_assess_rev_tax_dist_1.web_parameter_id,
12114 		l_self_assess_rev_tax_dist_1.adjustment_reason,
12115 		l_self_assess_rev_tax_dist_1.award_id,
12116 		l_self_assess_rev_tax_dist_1.credit_card_trx_id,
12117 		l_self_assess_rev_tax_dist_1.dist_match_type,
12118 		l_self_assess_rev_tax_dist_1.rcv_transaction_id,
12119 		l_self_assess_rev_tax_dist_1.invoice_distribution_id,
12120 		l_self_assess_rev_tax_dist_1.parent_reversal_id,
12121 		l_self_assess_rev_tax_dist_1.tax_recoverable_flag,
12122 		l_self_assess_rev_tax_dist_1.merchant_document_number,
12123 		l_self_assess_rev_tax_dist_1.merchant_name,
12124 		l_self_assess_rev_tax_dist_1.merchant_reference,
12125 		l_self_assess_rev_tax_dist_1.merchant_tax_reg_number,
12126 		l_self_assess_rev_tax_dist_1.merchant_taxpayer_id,
12127 		l_self_assess_rev_tax_dist_1.country_of_supply,
12128 		l_self_assess_rev_tax_dist_1.matched_uom_lookup_code,
12129 		l_self_assess_rev_tax_dist_1.gms_burdenable_raw_cost,
12130 		l_self_assess_rev_tax_dist_1.accounting_event_id,
12131 		l_self_assess_rev_tax_dist_1.prepay_distribution_id,
12132 		l_self_assess_rev_tax_dist_1.upgrade_posted_amt,
12133 		l_self_assess_rev_tax_dist_1.upgrade_base_posted_amt,
12134 		l_self_assess_rev_tax_dist_1.inventory_transfer_status,
12135 		l_self_assess_rev_tax_dist_1.company_prepaid_invoice_id,
12136 		l_self_assess_rev_tax_dist_1.cc_reversal_flag,
12137 		l_self_assess_rev_tax_dist_1.awt_withheld_amt,
12138 		l_self_assess_rev_tax_dist_1.pa_cmt_xface_flag,
12139 		l_self_assess_rev_tax_dist_1.cancellation_flag,
12140 		l_self_assess_rev_tax_dist_1.invoice_line_number,
12141 		l_self_assess_rev_tax_dist_1.corrected_invoice_dist_id,
12142 		l_self_assess_rev_tax_dist_1.rounding_amt,
12143 		l_self_assess_rev_tax_dist_1.charge_applicable_to_dist_id,
12144 		l_self_assess_rev_tax_dist_1.corrected_quantity,
12145 		l_self_assess_rev_tax_dist_1.related_id,
12146 		l_self_assess_rev_tax_dist_1.asset_book_type_code,
12147 		l_self_assess_rev_tax_dist_1.asset_category_id,
12148 		l_self_assess_rev_tax_dist_1.distribution_class,
12149 		l_self_assess_rev_tax_dist_1.tax_code_id,
12150 		l_self_assess_rev_tax_dist_1.intended_use,
12151 		l_self_assess_rev_tax_dist_1.detail_tax_dist_id,
12152 		l_self_assess_rev_tax_dist_1.rec_nrec_rate,
12153 		l_self_assess_rev_tax_dist_1.recovery_rate_id,
12154 		l_self_assess_rev_tax_dist_1.recovery_type_code,
12155 		l_self_assess_rev_tax_dist_1.withholding_tax_code_id,
12156 		l_self_assess_rev_tax_dist_1.taxable_amount,
12157 		l_self_assess_rev_tax_dist_1.taxable_base_amount,
12158 		l_self_assess_rev_tax_dist_1.tax_already_distributed_flag,
12159 		l_self_assess_rev_tax_dist_1.summary_tax_line_id,
12160 		l_self_assess_rev_tax_dist_1.rcv_charge_addition_flag,
12161 		l_self_assess_rev_tax_dist_1.self_assessed_flag,
12162  		l_self_assess_rev_tax_dist_1.self_assessed_tax_liab_ccid,   --bug6805655
12163         l_self_assess_rev_tax_dist_1.prepay_tax_diff_amount -- BUG 7338249
12164         );
12165 
12166          END LOOP;
12167          CLOSE c_rev_self_assess_tax_dist_1;
12168 
12169 	 END IF; -- Marker 8
12170 
12171     END IF; -- Marker 4
12172 
12173 
12174     IF p_line_number IS NOT NULL THEN
12175 
12176        UPDATE ap_invoice_lines_all a
12177           SET amount = (select nvl(tax_amt, 0)
12178                           from zx_lines_summary b
12179                          where b.summary_tax_line_id = a.summary_tax_line_id)
12180         WHERE a.invoice_id            = p_invoice_id
12181           AND a.line_type_lookup_code = 'TAX'
12182           AND exists
12183 		(select 'Detail Line'
12184 		   from zx_lines zx
12185 		  where zx.summary_tax_line_id = a.summary_tax_line_id
12186 		    and zx.trx_id              = p_invoice_id
12187 		    and zx.trx_line_id         = p_line_number);
12188 
12189     END IF;
12190 
12191     UPDATE ap_invoices_all ai
12192        SET (ai.total_tax_amount,
12193             ai.self_assessed_tax_amount) =
12194            (SELECT SUM(DECODE(NVL(zls.self_assessed_flag, 'N'),
12195                               'N', NVL(zls.tax_amt, 0),
12196                               0)),
12197                    SUM(DECODE(NVL(zls.self_assessed_flag, 'N'),
12198                               'Y', NVL(zls.tax_amt, 0),
12199                               0))
12200               FROM zx_lines_summary zls
12201              WHERE application_id   = AP_ETAX_PKG.AP_APPLICATION_ID
12202                AND entity_code      = AP_ETAX_PKG.AP_ENTITY_CODE
12203                AND event_class_code IN (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
12204                                         AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
12205                                         AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
12206                AND zls.trx_id       = ai.invoice_id
12207                AND NVL(zls.reporting_only_flag, 'N') = 'N')
12208     WHERE ai.invoice_id = p_invoice_id;
12209 
12210 
12211 
12212     IF l_tax_distributions_exist THEN
12213 
12214         -----------------------------------------------------------------
12215         l_debug_info := 'Step 4: Update reversal_flag';
12216         -----------------------------------------------------------------
12217         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12218             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
12219         END IF;
12220 
12221         UPDATE ap_invoice_distributions_all aid
12222            SET reversal_flag = (select reverse_flag
12223                                   from zx_rec_nrec_dist zx
12224                                  where zx.rec_nrec_tax_dist_id = aid.detail_tax_dist_id)
12225          WHERE aid.invoice_id = p_invoice_id
12226            AND aid.detail_tax_dist_id IS NOT NULL;
12227 
12228         -----------------------------------------------------------------
12229         l_debug_info := 'Step 5: Update related_flag';
12230         -----------------------------------------------------------------
12231         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12232             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
12233         END IF;
12234 
12235         UPDATE ap_invoice_distributions aid
12236            SET aid.related_id =
12237       			(SELECT invoice_distribution_id
12238                            FROM ap_invoice_distributions_all aid1
12239 			  WHERE aid1.invoice_id = aid.invoice_id
12240 			    AND aid1.invoice_line_number = aid.invoice_line_number
12241 			    AND aid1.parent_reversal_id =
12242 					(SELECT related_id
12243 			                   FROM ap_invoice_distributions_all aid2
12244 					  WHERE aid2.invoice_id = aid.invoice_id
12245 					    AND aid2.invoice_line_number = aid.invoice_line_number
12246 					    AND aid2.invoice_distribution_id = aid.parent_reversal_id)
12247                        )
12248         WHERE aid.related_id IS NULL
12249           AND aid.parent_reversal_id IS NOT NULL
12250           AND aid.invoice_id = p_invoice_id
12251           AND aid.reversal_flag = 'Y'
12252           AND aid.detail_tax_dist_id IS NOT NULL;
12253 
12254     END IF;
12255 
12256 
12257 
12258     IF l_self_assess_tax_dist_exist THEN
12259 
12260            -----------------------------------------------------------------
12261            l_debug_info := 'Step 4: Update reversal_flag';
12262            -----------------------------------------------------------------
12263            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12264                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
12265            END IF;
12266 
12267            UPDATE ap_self_assessed_tax_dist_all aid
12268               SET reversal_flag = (select reverse_flag
12269                                      from zx_rec_nrec_dist zx
12270                                     where zx.rec_nrec_tax_dist_id = aid.detail_tax_dist_id)
12271             WHERE aid.invoice_id = p_invoice_id
12272               AND aid.detail_tax_dist_id IS NOT NULL;
12273 
12274            -----------------------------------------------------------------
12275            l_debug_info := 'Step 5: Update related_flag';
12276            -----------------------------------------------------------------
12277            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12278                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
12279            END IF;
12280 
12281            UPDATE ap_self_assessed_tax_dist_all aid
12282               SET aid.related_id =
12283                         (SELECT invoice_distribution_id
12284                            FROM ap_self_assessed_tax_dist_all aid1
12285                           WHERE aid1.invoice_id = aid.invoice_id
12286                             AND aid1.invoice_line_number = aid.invoice_line_number
12287                             AND aid1.parent_reversal_id =
12288                                         (SELECT related_id
12289                                            FROM ap_self_assessed_tax_dist_all aid2
12290                                           WHERE aid2.invoice_id = aid.invoice_id
12291                                             AND aid2.invoice_line_number = aid.invoice_line_number
12292                                             AND aid2.invoice_distribution_id = aid.parent_reversal_id)
12293                        )
12294             WHERE aid.related_id IS NULL
12295               AND aid.parent_reversal_id IS NOT NULL
12296               AND aid.invoice_id = p_invoice_id
12297               AND aid.reversal_flag = 'Y'
12298               AND aid.detail_tax_dist_id IS NOT NULL;
12299 
12300    END IF;
12301 
12302    END IF; -- l_return_status
12303 
12304    RETURN l_return_status;
12305 
12306   EXCEPTION
12307     WHEN OTHERS THEN
12308 
12309       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12310           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'Exception: '||sqlerrm);
12311       END IF;
12312 
12313       IF (SQLCODE <> -20001) THEN
12314 
12315         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
12316         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
12317         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
12318         FND_MESSAGE.SET_TOKEN('PARAMETERS',
12319           ' P_Invoice_Id = '||P_Invoice_Id);
12320         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
12321       END IF;
12322 
12323       APP_EXCEPTION.RAISE_EXCEPTION;
12324 
12325 END CANCEL_INVOICE;
12326 
12327 FUNCTION Generate_Recouped_Tax(
12328 	             P_Invoice_id              IN NUMBER,
12329 		     P_Invoice_Line_Number     IN NUMBER,
12330 	             P_Calling_Mode            IN VARCHAR2,
12331 	             P_All_Error_Messages      IN VARCHAR2,
12332 	             P_Error_Code              OUT NOCOPY VARCHAR2,
12333 	             P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN IS
12334 
12335     CURSOR invoice_header IS
12336     SELECT *
12337       FROM ap_invoices_all
12338      WHERE invoice_id = P_Invoice_Id;
12339 
12340     CURSOR prepay_lines IS
12341     SELECT distinct prepLine.*
12342       FROM ap_invoice_distributions_all invDist,
12343            ap_invoice_distributions_all prepDist,
12344            ap_invoice_lines_all		prepLine
12345      WHERE invDist.prepay_distribution_id = prepDist.invoice_distribution_id
12346        AND prepLine.invoice_id		  = prepDist.invoice_id
12347        AND prepLine.line_number		  = prepDist.invoice_line_number
12348        AND invDist.line_type_lookup_code  = 'PREPAY'
12349        AND invDist.invoice_id             = p_invoice_id
12350        AND invDist.invoice_line_number    = p_invoice_line_number;
12351 
12352     l_inv_header_rec			ap_invoices_all%ROWTYPE;
12353     l_event_class_code			zx_trx_headers_gt.event_class_code%TYPE;
12354     l_event_type_code			zx_trx_headers_gt.event_type_code%TYPE;
12355 
12356     l_tax_already_calculated     	VARCHAR2(1);
12357 
12358     l_debug_info                	VARCHAR2(240);
12359     l_curr_calling_sequence     	VARCHAR2(4000);
12360     l_api_name                  	CONSTANT VARCHAR2(100) := 'Generate_Recouped_Tax';
12361 
12362     l_return_status                     BOOLEAN := TRUE;
12363     l_return_status_service             VARCHAR2(4000);
12364     l_msg_count                         NUMBER;
12365     l_msg_data                          VARCHAR2(4000);
12366     l_error_code			VARCHAR2(4000);
12367 
12368 BEGIN
12369 
12370     -----------------------------------------------------------------
12371     l_debug_info := 'Step 1: Populating invoice header local record';
12372     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12373       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
12374     END IF;
12375     -----------------------------------------------------------------
12376 
12377     BEGIN
12378       OPEN  Invoice_Header;
12379       FETCH Invoice_Header INTO l_inv_header_rec;
12380       CLOSE Invoice_Header;
12381     END;
12382 
12383     -------------------------------------------------------------------
12384     l_debug_info := 'Step 2: Is tax already called invoice level?';
12385     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12386       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
12387     END IF;
12388     -------------------------------------------------------------------
12389     IF (AP_ETAX_UTILITY_PKG.Is_Tax_Already_Calc_Inv(
12390           P_Invoice_Id           => p_invoice_id,
12391           P_Calling_Sequence     => l_curr_calling_sequence)) THEN
12392 
12393       l_tax_already_calculated := 'Y';
12394     ELSE
12395       l_tax_already_calculated := 'N';
12396 
12397     END IF;
12398 
12399     -----------------------------------------------------------------
12400     l_debug_info := 'Step 3: Populate Header';
12401     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12402       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
12403     END IF;
12404     -----------------------------------------------------------------
12405     IF NOT(AP_ETAX_SERVICES_PKG.Populate_Headers_GT(
12406 		       P_Invoice_Header_Rec         => l_inv_header_rec
12407 		      ,P_Calling_Mode               => p_calling_mode
12408 		      ,P_eTax_Already_called_flag   => l_tax_already_calculated
12409 		      ,P_Event_Class_Code           => l_event_class_code
12410 		      ,P_Event_Type_Code            => l_event_type_code
12411 		      ,P_Error_Code                 => p_error_code
12412 		      ,P_Calling_Sequence           => l_curr_calling_sequence)) THEN
12413 
12414       l_return_status := FALSE;
12415     END IF;
12416 
12417     -----------------------------------------------------------------
12418     l_debug_info := 'Step 4: Populate psuedo prepay lines for the recouped distributions';
12419     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12420       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
12421     END IF;
12422     -----------------------------------------------------------------
12423     IF ( l_return_status = TRUE ) THEN
12424 
12425       OPEN  prepay_lines;
12426       FETCH prepay_lines
12427        BULK COLLECT INTO l_inv_line_list;
12428       CLOSE prepay_lines;
12429 
12430       IF l_inv_line_list.count > 0 THEN
12431 
12432 
12433          -----------------------------------------------------------------
12434          l_debug_info := 'Purge Staging Tables. Clear/Load Cache';
12435          Print(l_api_name,l_debug_info);
12436          -----------------------------------------------------------------
12437          DELETE FROM ZX_TRANSACTION_LINES_GT;
12438 
12439          AP_ETAX_SERVICES_PKG.G_SITE_ATTRIBUTES.DELETE;
12440          AP_ETAX_SERVICES_PKG.G_ORG_ATTRIBUTES.DELETE;
12441 
12442               IF l_inv_header_rec.invoice_type_lookup_code = 'PAYMENT REQUEST' THEN  /* if
12443                   condition for bug 5967914 as we need tp pass party_site_id instead of
12444                   vendor_site_id if invoice_type_lookup_code ='PAYMENT REQUEST' */
12445                  l_payment_request_flag :='Y';  -- for bug 5967914
12446 	         Cache_Line_Defaults
12447 	               ( p_org_id           => l_inv_header_rec.org_id
12448 	                ,p_vendor_site_id   => l_inv_header_rec.party_site_id
12449 	                ,p_calling_sequence => l_curr_calling_sequence);
12450               ELSE
12451                  l_payment_request_flag :='N';  -- for bug 5967914
12452                	  Cache_Line_Defaults
12453 	               ( p_org_id           => l_inv_header_rec.org_id
12454 	                ,p_vendor_site_id   => l_inv_header_rec.vendor_site_id
12455 	                ,p_calling_sequence => l_curr_calling_sequence);
12456               END IF;
12457 
12458 
12459 	 IF NOT(AP_ETAX_SERVICES_PKG.Populate_Lines_GT(
12460 		           P_Invoice_Header_Rec      => l_inv_header_rec
12461 		          ,P_Calling_Mode            => p_calling_mode
12462 		          ,P_Event_Class_Code        => l_event_class_code
12463 		          ,P_Line_Number             => p_invoice_line_number
12464 		          ,P_Error_Code              => p_error_code
12465 		          ,P_Calling_Sequence        => l_curr_calling_sequence )) THEN
12466 
12467 	     l_return_status := FALSE;
12468 
12469          END IF;
12470 
12471          IF ( l_return_status = TRUE ) THEN
12472 
12473              -----------------------------------------------------------------
12474              l_debug_info := 'Step 5: Call Calculate_Tax service';
12475              IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12476                  FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
12477              END IF;
12478              -----------------------------------------------------------------
12479 
12480              zx_api_pub.calculate_tax(
12481 		          p_api_version      => 1.0,
12482 		          p_init_msg_list    => FND_API.G_TRUE,
12483 		          p_commit           => FND_API.G_FALSE,
12484 		          p_validation_level => FND_API.G_VALID_LEVEL_FULL,
12485 		          x_return_status    => l_return_status_service,
12486 		          x_msg_count        => l_msg_count,
12487 		          x_msg_data         => l_msg_data);
12488 
12489          END IF;
12490 
12491          IF (l_return_status_service = 'S') THEN
12492 
12493              -----------------------------------------------------------------
12494              l_debug_info := 'Step 5.1: Update Tax Already Calculated Flag';
12495              IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12496                  FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
12497              END IF;
12498              -----------------------------------------------------------------
12499 
12500 	     UPDATE ap_invoice_lines_all ail
12501 	        SET ail.tax_already_calculated_flag = 'Y'
12502 	      WHERE ail.invoice_id  = p_invoice_id
12503                 AND ail.line_number = p_invoice_line_number;
12504 
12505              -----------------------------------------------------------------
12506              l_debug_info := 'Step 6: Generate Tax Distributions';
12507              IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12508                  FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
12509              END IF;
12510              -----------------------------------------------------------------
12511 
12512              l_return_status := ap_etax_pkg.calling_etax
12513 		                        (p_invoice_id         => p_invoice_id,
12514 					 p_line_number	      => p_invoice_line_number,
12515 		                         p_calling_mode       => 'DISTRIBUTE RECOUP',
12516 		                         p_all_error_messages => 'N',
12517 		                         p_error_code         =>  l_error_code,
12518 		                         p_calling_sequence   => l_curr_calling_sequence);
12519 
12520          ELSE  -- handle errors
12521 
12522             -----------------------------------------------------------------
12523             l_debug_info := 'Step 7: Handle errors returned by API';
12524             IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12525                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
12526             END IF;
12527             -----------------------------------------------------------------
12528 
12529             l_return_status := FALSE;
12530 
12531             IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
12532 	               P_All_Error_Messages  => P_All_Error_Messages,
12533 	               P_Msg_Count           => l_msg_count,
12534 	               P_Msg_Data            => l_msg_data,
12535 	               P_Error_Code          => P_Error_Code,
12536 	               P_Calling_Sequence    => l_curr_calling_sequence)) THEN
12537 	       NULL;
12538 	    END IF;
12539 
12540          END IF;
12541       END IF;
12542     END IF;
12543 
12544     DELETE FROM ZX_TRX_HEADERS_GT;
12545     DELETE FROM ZX_TRANSACTION_LINES_GT;
12546 
12547     RETURN l_return_status;
12548 
12549 EXCEPTION
12550     WHEN OTHERS THEN
12551 
12552       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12553           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'Exception: '||sqlerrm);
12554       END IF;
12555 
12556       IF (SQLCODE <> -20001) THEN
12557 
12558         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
12559         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
12560         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
12561         FND_MESSAGE.SET_TOKEN('PARAMETERS',
12562           ' P_Invoice_Id = '||P_Invoice_Id);
12563         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
12564       END IF;
12565 
12566       DELETE FROM ZX_TRX_HEADERS_GT;
12567       DELETE FROM ZX_TRANSACTION_LINES_GT;
12568 
12569       APP_EXCEPTION.RAISE_EXCEPTION;
12570 
12571 END Generate_Recouped_Tax;
12572 
12573 Function Delete_Tax_Distributions
12574 			(p_invoice_id         IN  ap_invoice_distributions_all.invoice_id%Type,
12575 			 p_calling_mode	      IN  VARCHAR2,
12576 			 p_all_error_messages IN  VARCHAR2,
12577 			 p_error_code         OUT NOCOPY VARCHAR2,
12578 		         p_calling_sequence   IN  VARCHAR2) RETURN BOOLEAN IS
12579 
12580     Cursor c_preview_dists (c_invoice_id Number) Is
12581 	Select org_id, invoice_id, invoice_line_number
12582 	  From ap_invoice_distributions_all
12583 	  --bugfix:5565648 added the 'invoice_id = c_invoice_id' as without that
12584 	  --it was doing a full table scan
12585 	 Where invoice_id = c_invoice_id
12586 	   AND distribution_class = 'CANDIDATE'
12587 	   And line_type_lookup_code Not In
12588 			('NONREC_TAX', 'REC_TAX', 'TRV', 'TERV', 'TIPV', 'AWT')
12589 	   And (related_id IS NULL
12590               or related_id = invoice_distribution_id);
12591 
12592    l_invoice_header_rec		ap_invoices_all%rowtype;
12593    l_application_id             zx_trx_headers_gt.application_id%TYPE;
12594    l_entity_code                zx_trx_headers_gt.entity_code%TYPE;
12595    l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
12596    l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
12597    l_tax_already_calculated     VARCHAR2(1);
12598 
12599    l_debug_info                 VARCHAR2(240);
12600    l_curr_calling_sequence      VARCHAR2(4000);
12601 
12602    l_return_status_service      VARCHAR2(4000);
12603    l_return_status              BOOLEAN := TRUE;
12604    l_error_code                 VARCHAR2(4000);
12605    l_msg_data			VARCHAR2(2000);
12606    l_msg_count			NUMBER;
12607 
12608    l_preview_dists		c_preview_dists%rowtype;
12609    l_transaction_line_rec_type  ZX_API_PUB.transaction_line_rec_type;
12610 
12611    l_api_name                    CONSTANT VARCHAR2(100) := 'Delete_Tax_distributions';
12612 
12613 Begin
12614 
12615    l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Delete_Tax_Distributions<-' ||
12616                                p_calling_sequence;
12617 
12618    IF NOT tax_distributions_exist
12619                         (p_invoice_id  => p_invoice_id) THEN
12620 
12621        l_debug_info := 'Exit delete_tax_distributions';
12622        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12623            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
12624        END IF;
12625 
12626        RETURN l_return_status;
12627 
12628    END IF;
12629 
12630    -------------------------------------------------------------------
12631    l_debug_info := 'Step 1: Get invoice header details';
12632    -------------------------------------------------------------------
12633    Select * Into l_invoice_header_rec
12634      From ap_invoices_all
12635     Where invoice_id = p_invoice_id;
12636 
12637    IF ((l_invoice_header_rec.quick_credit = 'Y') OR    -- Bug 5660314
12638        (l_invoice_header_rec.invoice_type_lookup_code IN ('AWT', 'INTEREST'))) THEN
12639      RETURN l_return_status;
12640    END IF;
12641 
12642    -------------------------------------------------------------------
12643    l_debug_info := 'Step 2: Populate product specific attributes';
12644    -------------------------------------------------------------------
12645    l_application_id := 200;
12646    l_entity_code    := 'AP_INVOICES';
12647 
12648    -------------------------------------------------------------------
12649    l_debug_info := 'Step 3: Get event class code';
12650    -------------------------------------------------------------------
12651    IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
12652 		      P_Invoice_Type_Lookup_Code => l_invoice_header_rec.invoice_type_lookup_code,
12653 		      P_Event_Class_Code         => l_event_class_code,
12654 		      P_error_code               => l_error_code,
12655 		      P_calling_sequence         => l_curr_calling_sequence)) THEN
12656       l_return_status := FALSE;
12657    END IF;
12658 
12659    -------------------------------------------------------------------
12660    l_debug_info := 'Step 4: Is tax already called invoice level?';
12661    -------------------------------------------------------------------
12662    IF (AP_ETAX_UTILITY_PKG.Is_Tax_Already_Calc_Inv(
12663 			P_Invoice_Id       => p_invoice_id,
12664 			P_Calling_Sequence => l_curr_calling_sequence)) THEN
12665        l_tax_already_calculated := 'Y';
12666    ELSE
12667        l_tax_already_calculated := 'N';
12668    END IF;
12669 
12670    -------------------------------------------------------------------
12671    l_debug_info := 'Step 5: Get event type code';
12672    -------------------------------------------------------------------
12673    IF (l_return_status = TRUE) THEN
12674       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
12675 			P_Event_Class_Code          => l_event_class_code,
12676 			P_Calling_Mode              => 'DISTRIBUTE',
12677 			P_eTax_Already_called_flag  => l_tax_already_calculated,
12678 			P_Event_Type_Code           => l_event_type_code,
12679 			P_Error_Code                => l_error_code,
12680 			P_Calling_Sequence          => l_curr_calling_sequence)) THEN
12681         l_return_status := FALSE;
12682       END IF;
12683    END IF;
12684    --Bug6678578START
12685    UPDATE ap_invoice_distributions_All aid1
12686       SET aid1.amount = aid1.amount + nvl((SELECT SUM(nvl(aid2.amount,0))
12687 		                    FROM ap_invoice_distributions_All aid2
12688 		                   WHERE aid2.invoice_id =  p_invoice_id
12689 		                     AND  aid2.invoice_line_number = aid1.invoice_line_number
12690 		                     AND  aid2.charge_applicable_to_dist_id = aid1.invoice_distribution_id
12691 		                     AND  aid2.line_type_lookup_code IN ('REC_TAX','NONREC_TAX','TRV','TIPV')
12692 							 AND  aid2.distribution_class = 'CANDIDATE'),0),
12693 		                aid1.base_amount = aid1.base_amount + nvl((SELECT SUM(nvl(aid3.base_amount,0))
12694 		                    FROM ap_invoice_distributions_All aid3
12695 		                   WHERE aid3.invoice_id = p_invoice_id
12696 		                     AND aid3.invoice_line_number = aid1.invoice_line_number
12697 		                     AND aid3.charge_applicable_to_dist_id = aid1.invoice_distribution_id
12698 		                     AND aid3.line_type_lookup_code IN ('REC_TAX','NONREC_TAX','TRV','TIPV')
12699 						     AND  aid3.distribution_class = 'CANDIDATE'),0)
12700 	WHERE  aid1.invoice_id = p_invoice_id
12701       AND  aid1.line_type_lookup_code IN ('ITEM', 'ACCRUAL', 'FREIGHT', 'MISCELLANEOUS');
12702    --Bug6678578END
12703    IF (l_return_status = TRUE) THEN
12704 
12705        Open c_preview_dists (p_invoice_id);
12706        Loop
12707            Fetch c_preview_dists
12708            Into  l_preview_dists;
12709            Exit When c_preview_dists%notfound;
12710 
12711            l_transaction_line_rec_type.internal_organization_id := l_preview_dists.org_id;
12712            l_transaction_line_rec_type.application_id           := l_application_id;
12713            l_transaction_line_rec_type.entity_code              := l_entity_code;
12714            l_transaction_line_rec_type.event_class_code         := l_event_class_code;
12715            l_transaction_line_rec_type.event_type_code          := l_event_type_code;
12716            l_transaction_line_rec_type.trx_id                   := l_preview_dists.invoice_id;
12717            l_transaction_line_rec_type.trx_line_id              := l_preview_dists.invoice_line_number;
12718            l_transaction_line_rec_type.trx_level_type           := 'LINE';
12719 
12720 	     -------------------------------------------------------------------
12721            l_debug_info := 'Step 5: Call eTax API to delete tax distributions';
12722            -------------------------------------------------------------------
12723            ZX_API_PUB.delete_tax_distributions(
12724 	          p_api_version             =>  1.0,
12725 	          p_init_msg_list           =>  FND_API.G_TRUE,
12726 	          p_commit                  =>  FND_API.G_FALSE,
12727 	          p_validation_level        =>  FND_API.G_VALID_LEVEL_FULL,
12728 	          x_return_status           =>  l_return_status_service,
12729 	          x_msg_count               =>  l_msg_count,
12730 	          x_msg_data                =>  l_msg_data,
12731 	          p_transaction_line_rec    =>  l_transaction_line_rec_type
12732 	        );
12733 
12734            IF l_return_status_service <> FND_API.G_RET_STS_SUCCESS THEN
12735               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12736            END IF;
12737 
12738            DELETE FROM ap_self_assessed_tax_dist_all
12739 	   WHERE  invoice_id = l_preview_dists.invoice_id
12740            AND    invoice_line_number = l_preview_dists.invoice_line_number;
12741 
12742        End Loop;
12743        Close c_preview_dists;
12744    END IF;
12745 
12746    RETURN l_return_status;
12747 
12748 EXCEPTION
12749    WHEN OTHERS THEN
12750 
12751       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12752           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'Exception: '||sqlerrm);
12753       END IF;
12754 
12755       IF (SQLCODE <> -20001) THEN
12756         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
12757         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
12758         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
12759         FND_MESSAGE.SET_TOKEN('PARAMETERS', ' P_Invoice_Id = '||P_Invoice_Id);
12760         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
12761       END IF;
12762 
12763       APP_EXCEPTION.RAISE_EXCEPTION;
12764 
12765 END Delete_Tax_distributions;
12766 
12767 FUNCTION TAX_DISTRIBUTIONS_EXIST
12768                         (p_invoice_id  IN NUMBER) RETURN BOOLEAN IS
12769 
12770       l_dummy VARCHAR2(40);
12771 
12772 BEGIN
12773     SELECT 'Tax Distributions Exist'
12774       INTO l_dummy
12775       FROM ap_invoice_distributions_all
12776      WHERE invoice_id = p_invoice_id
12777        AND line_type_lookup_code IN ('REC_TAX', 'NONREC_TAX', 'TIPV', 'TRV', 'TERV')
12778        AND rownum = 1;
12779 
12780     RETURN (l_dummy IS NOT NULL);
12781 
12782 EXCEPTION
12783     WHEN OTHERS THEN
12784          RETURN FALSE;
12785 END TAX_DISTRIBUTIONS_EXIST;
12786 
12787 FUNCTION TAX_ONLY_LINE_EXIST
12788                         (p_invoice_id  IN NUMBER) RETURN BOOLEAN IS
12789 
12790       l_dummy VARCHAR2(40);
12791 
12792 BEGIN
12793        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12794            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||'TAX_ONLY_LINE_EXIST','Checking if there is tax only line exists');
12795        END IF;
12796 
12797    /* SELECT 'Tax Only Line Exist'
12798       INTO l_dummy
12799       FROM ap_invoice_distributions_all
12800      WHERE invoice_id = p_invoice_id
12801        AND line_type_lookup_code IN ('REC_TAX', 'NONREC_TAX', 'TIPV', 'TRV', 'TERV')
12802        AND charge_applicable_to_dist_id IS NULL
12803        AND rownum = 1; */
12804 
12805     SELECT 'Tax Only Line Exist'
12806       INTO l_dummy
12807       FROM zx_lines_summary zls
12808      WHERE application_id   = AP_ETAX_PKG.AP_APPLICATION_ID
12809        AND entity_code      = AP_ETAX_PKG.AP_ENTITY_CODE
12810        AND event_class_code IN (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
12811                                 AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
12812                                 AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
12813        AND zls.trx_id = p_invoice_id
12814        AND NVL(zls.reporting_only_flag, 'N') = 'N'
12815        AND NVL(zls.tax_only_line_flag, 'N') = 'Y'
12816        AND rownum = 1;
12817 
12818        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12819            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||'TAX_ONLY_LINE_EXIST','l_dummy: '|| NVL(l_dummy, 'No Tax Only Line'));
12820        END IF;
12821 
12822        RETURN (l_dummy IS NOT NULL);
12823 
12824 EXCEPTION
12825     WHEN OTHERS THEN
12826          IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12827            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||'TAX_ONLY_LINE_EXIST', 'in others: '|| NVL(l_dummy, 'No Tax Only Line'));
12828          END IF;
12829          RETURN FALSE;
12830 END TAX_ONLY_LINE_EXIST;
12831 
12832 /* Bug 6694536. Added function to verify whether there are self assessed tax lines
12833 --   associated with an invoice. Join with zx_rec_nrec_dist is required becase
12834 --   self_assessed_flag of zx_rec_nrec_dist actually indicates whether the lines
12835 --   are self assessed or not.
12836 */
12837 
12838 FUNCTION SELF_ASSESS_TAX_DIST_EXIST
12839                         (p_invoice_id  IN NUMBER) RETURN BOOLEAN IS
12840 
12841       l_dummy VARCHAR2(40);
12842 
12843 BEGIN
12844     SELECT 'Tax Distributions Exist'
12845       INTO l_dummy
12846       FROM ap_self_assessed_tax_dist_all asat,
12847            zx_rec_nrec_dist zx_dist
12848      WHERE invoice_id = p_invoice_id
12849        AND asat.detail_tax_dist_id = zx_dist.rec_nrec_tax_dist_id
12850        AND zx_dist.self_assessed_flag = 'Y'
12851        AND nvl(zx_dist.reverse_flag, 'N') <> 'Y'
12852        AND line_type_lookup_code IN ('REC_TAX', 'NONREC_TAX')
12853        AND rownum = 1;
12854 
12855     RETURN (l_dummy IS NOT NULL);
12856 
12857 EXCEPTION
12858     WHEN OTHERS THEN
12859          RETURN FALSE;
12860 END SELF_ASSESS_TAX_DIST_EXIST;
12861 
12862 PROCEDURE get_converted_qty_price (x_invoice_distribution_id IN  NUMBER,
12863 	                           x_inv_price		     OUT NOCOPY NUMBER,
12864 		                   x_inv_qty		     OUT NOCOPY NUMBER) IS
12865 
12866 	CURSOR c_rct_info (c_inv_dist_id NUMBER) IS
12867 	 SELECT  D.unit_price		      unit_price,
12868                  nvl(D.quantity_invoiced, 0)  quantity_invoiced,
12869 		 pll.matching_basis	      match_basis,
12870 	         pll.match_option             match_option,
12871 	         pl.unit_meas_lookup_code     po_uom,
12872 	         D.matched_uom_lookup_code    rcv_uom,
12873 	         rsl.item_id                  rcv_item_id
12874 	  FROM   ap_invoice_distributions_all D,
12875 	         po_distributions_all 	      PD,
12876 	         po_lines_all		      PL,
12877 	         po_line_locations_all	      PLL,
12878 	         rcv_transactions	      RTXN,
12879 	         rcv_shipment_lines 	      RSL
12880 	  WHERE  D.invoice_distribution_id = c_inv_dist_id
12881 	    AND  D.po_distribution_id      = PD.po_distribution_id
12882 	    AND  PL.po_header_id           = PD.po_header_id
12883 	    AND  PL.po_line_id             = PD.po_line_id
12884 	    AND  PD.line_location_id       = PLL.line_location_id
12885 	    AND  D.rcv_transaction_id      = RTXN.transaction_id
12886 	    AND  RTXN.shipment_line_id     = RSL.shipment_line_id;
12887 
12888 	l_match_basis	po_line_types.matching_basis%TYPE;
12889 	l_match_option	po_line_locations.match_option%TYPE;
12890 	l_po_uom	po_line_locations.unit_meas_lookup_code%TYPE;
12891 	l_rct_uom	po_line_locations.unit_meas_lookup_code%TYPE;
12892 	l_rct_item_id	rcv_shipment_lines.item_id%TYPE;
12893 
12894 	l_uom_conv_rate NUMBER;
12895         l_qty_invoiced  NUMBER;
12896         l_inv_price     NUMBER;
12897 BEGIN
12898      OPEN  c_rct_info (x_invoice_distribution_id);
12899      FETCH c_rct_info
12900      INTO  x_inv_price, x_inv_qty, l_match_basis, l_match_option,
12901            l_po_uom, l_rct_uom, l_rct_item_id;
12902      CLOSE c_rct_info;
12903 
12904      IF l_match_basis  = 'QUANTITY'  and
12905         l_match_option = 'R'	     and
12906         l_po_uom       <> l_rct_uom THEN
12907 
12908         l_uom_conv_rate := po_uom_s.po_uom_convert (
12909                              l_rct_uom,
12910                              l_po_uom,
12911                              l_rct_item_id);
12912 
12913         x_inv_qty   := x_inv_qty * l_uom_conv_rate;
12914         x_inv_price := x_inv_price / l_uom_conv_rate;
12915 
12916      END IF;
12917 
12918 EXCEPTION
12919      WHEN OTHERS THEN
12920           NULL;
12921 END get_converted_qty_price;
12922 
12923 FUNCTION Calculate_Tax_Receipt_Match(
12924 			P_Invoice_Id              IN  NUMBER,
12925 			P_Calling_Mode            IN  VARCHAR2,
12926 			P_All_Error_Messages      IN  VARCHAR2,
12927 			P_Error_Code              OUT NOCOPY VARCHAR2,
12928 			P_Calling_Sequence        IN  VARCHAR2) RETURN BOOLEAN
12929 IS
12930 
12931     l_debug_info                 VARCHAR2(240);
12932     l_curr_calling_sequence      VARCHAR2(4000);
12933 
12934     l_inv_header_rec             ap_invoices_all%ROWTYPE;
12935     l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
12936     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
12937 
12938     l_tax_already_calculated     VARCHAR2(1);
12939 
12940     l_return_status_service       VARCHAR2(4000);
12941     l_msg_count                   NUMBER;
12942     l_msg_data                    VARCHAR2(4000);
12943     l_msg                         VARCHAR2(4000);
12944 
12945     l_return_status               BOOLEAN := TRUE;
12946     l_no_tax_lines                VARCHAR2(1) := 'N';
12947     l_inv_rcv_matched             VARCHAR2(1) := 'N';
12948 
12949     CURSOR Invoice_Header IS
12950     SELECT *
12951       FROM ap_invoices_all
12952      WHERE invoice_id = P_Invoice_Id;
12953 
12954     CURSOR Tax_Lines_to_import IS
12955     SELECT *
12956       FROM ap_invoice_lines_all
12957      WHERE invoice_id = P_Invoice_Id
12958        AND line_type_lookup_code = 'TAX'
12959        AND summary_tax_line_id IS NULL;
12960 
12961     l_api_name CONSTANT VARCHAR2(100) := 'Calculate_Tax_Receipt_Match';
12962 
12963   BEGIN
12964     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Calculate_Tax_Receipt_Match<-' ||
12965                                P_calling_sequence;
12966 
12967     -----------------------------------------------------------------
12968     l_debug_info := 'Step 1: Populating invoice header local record';
12969     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12970       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
12971     END IF;
12972     -----------------------------------------------------------------
12973     BEGIN
12974       OPEN Invoice_Header;
12975       FETCH Invoice_Header INTO l_inv_header_rec;
12976       CLOSE Invoice_Header;
12977     END;
12978 
12979     BEGIN
12980       OPEN  Tax_Lines_to_Import;
12981       FETCH Tax_Lines_to_Import
12982       BULK  COLLECT INTO p_rct_match_tax_list;
12983       CLOSE Tax_Lines_to_Import;
12984     END;
12985 
12986     -----------------------------------------------------------------
12987     l_debug_info := 'Step 2: Is tax already called invoice level?';
12988     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12989       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
12990     END IF;
12991     -----------------------------------------------------------------
12992     IF (AP_ETAX_UTILITY_PKG.Is_Tax_Already_Calc_Inv(
12993           P_Invoice_Id           => p_invoice_id,
12994           P_Calling_Sequence     => l_curr_calling_sequence)) THEN
12995 
12996       l_tax_already_calculated := 'Y';
12997     ELSE
12998       l_tax_already_calculated := 'N';
12999 
13000     END IF;
13001 
13002     -----------------------------------------------------------------
13003     l_debug_info := 'Step 3: Populate ZX header tables';
13004     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13005       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13006     END IF;
13007     -----------------------------------------------------------------
13008     IF NOT(AP_ETAX_SERVICES_PKG.Populate_Headers_GT(
13009 		P_Invoice_Header_Rec         => l_inv_header_rec,
13010 		P_Calling_Mode               => P_Calling_Mode,
13011 		P_eTax_Already_called_flag   => l_tax_already_calculated,
13012 		P_Event_Class_Code           => l_event_class_code,
13013 		P_Event_Type_Code            => l_event_type_code,
13014 		P_Error_Code                 => P_error_code,
13015 		P_Calling_Sequence           => l_curr_calling_sequence )) THEN
13016 
13017       l_return_status := FALSE;
13018     END IF;
13019 
13020     -------------------------------------------------------------------
13021     l_debug_info := 'Step 4: If tax already calculated call freeze '||
13022                     'distributions';
13023     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13024       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13025     END IF;
13026     -------------------------------------------------------------------
13027     IF (l_tax_already_calculated = 'Y') THEN
13028          IF NOT(AP_ETAX_SERVICES_PKG.Freeze_itm_Distributions(
13029                 P_Invoice_Header_Rec  => l_inv_header_rec,
13030                 P_Calling_Mode        => 'FREEZE DISTRIBUTIONS',
13031                 P_Event_Class_Code    => l_event_class_code,
13032                 P_All_Error_Messages  => P_All_Error_Messages,
13033                 P_Error_Code          => P_error_code,
13034                 P_Calling_Sequence    => l_curr_calling_sequence)) THEN --Bug7592845
13035 
13036            l_return_status := FALSE;
13037         END IF;
13038     END IF;
13039 
13040     ------------------------------------------------------------
13041     l_debug_info := 'Step 5: Populate zx_transaction_lines_gt';
13042     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13043       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13044     END IF;
13045     ------------------------------------------------------------
13046     IF ( l_return_status = TRUE ) THEN
13047         IF NOT(Populate_Rct_Match_Lines_GT(
13048 		P_Invoice_Header_Rec   => l_inv_header_rec,
13049 		P_Event_Class_Code     => l_event_class_code,
13050 		P_Error_Code           => P_error_code,
13051 		P_Calling_Sequence     => l_curr_calling_sequence )) THEN
13052 
13053 	   l_return_status := FALSE;
13054         END IF;
13055     END IF;
13056 
13057     IF (l_return_status = TRUE) THEN
13058 
13059         -----------------------------------------------------------------
13060         l_debug_info := 'Step 8: Call Calculate_Tax service';
13061         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13062             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13063         END IF;
13064         -----------------------------------------------------------------
13065         zx_api_pub.calculate_tax(
13066           p_api_version      => 1.0,
13067           p_init_msg_list    => FND_API.G_TRUE,
13068           p_commit           => FND_API.G_FALSE,
13069           p_validation_level => FND_API.G_VALID_LEVEL_FULL,
13070           x_return_status    => l_return_status_service,
13071           x_msg_count        => l_msg_count,
13072           x_msg_data         => l_msg_data);
13073     END IF;
13074 
13075     IF (l_return_status_service = 'S') THEN
13076       -----------------------------------------------------------------
13077       l_debug_info := 'Step 9: Handle return of tax lines';
13078       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13079           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13080       END IF;
13081       -----------------------------------------------------------------
13082       IF NOT(AP_ETAX_SERVICES_PKG.Return_Other_Charge_Lines(
13083                 P_Invoice_header_rec => l_inv_header_rec,
13084                 P_Error_Code         => P_error_code,
13085                 P_Calling_Sequence   => l_curr_calling_sequence)) THEN
13086 
13087 
13088           l_return_status := FALSE;
13089       END IF;
13090 
13091     ELSE  -- handle errors
13092 
13093       -----------------------------------------------------------------
13094       l_debug_info := 'Step 10: Handle errors returned by API';
13095       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13096           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13097       END IF;
13098       -----------------------------------------------------------------
13099       l_return_status := FALSE;
13100 
13101       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
13102                P_All_Error_Messages  => P_All_Error_Messages,
13103                P_Msg_Count           => l_msg_count,
13104                P_Msg_Data            => l_msg_data,
13105                P_Error_Code          => P_Error_Code,
13106                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
13107         NULL;
13108       END IF;
13109 
13110     END IF;
13111 
13112    RETURN l_return_status;
13113 
13114   EXCEPTION
13115     WHEN OTHERS THEN
13116       IF (SQLCODE <> -20001) THEN
13117         FND_MESSAGE.SET_NAME ('SQLAP', 'AP_DEBUG');
13118         FND_MESSAGE.SET_TOKEN('ERROR', SQLERRM);
13119         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
13120         FND_MESSAGE.SET_TOKEN('PARAMETERS', ' P_Invoice_Id = '      ||P_Invoice_Id  ||
13121 					    ' P_Calling_Mode ='     ||P_Calling_Mode||
13122 					    ' P_Error_Code = '      ||P_Error_Code  ||
13123 					    ' P_Calling_Sequence = '||P_Calling_Sequence);
13124         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
13125 
13126       END IF;
13127 
13128       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13129           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,sqlerrm);
13130       END IF;
13131 
13132       APP_EXCEPTION.RAISE_EXCEPTION;
13133 
13134   END Calculate_Tax_Receipt_Match;
13135 
13136   FUNCTION Populate_Rct_Match_Lines_GT(
13137              P_Invoice_Header_Rec      IN ap_invoices_all%ROWTYPE,
13138              P_Event_Class_Code        IN VARCHAR2,
13139              P_Error_Code              OUT NOCOPY VARCHAR2,
13140              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN IS
13141 
13142     TYPE Trx_Lines_Tab_Type IS TABLE OF zx_transaction_lines_gt%ROWTYPE;
13143 
13144     trans_lines				Trx_Lines_Tab_Type := Trx_Lines_Tab_Type();
13145 
13146     l_line_class                   	zx_transaction_lines_gt.line_class%TYPE;
13147     l_line_level_action			zx_transaction_lines_gt.line_level_action%TYPE;
13148     l_line_amt_includes_tax_flag	zx_transaction_lines_gt.line_amt_includes_tax_flag%TYPE;
13149     l_product_org_id			zx_transaction_lines_gt.product_org_id%TYPE;
13150     l_bill_to_location_id		zx_transaction_lines_gt.bill_to_location_id%TYPE;
13151     l_location_id		 	zx_transaction_lines_gt.ship_from_location_id%type;
13152     l_fob_point				zx_transaction_lines_gt.fob_point%TYPE;
13153 
13154     l_po_line_location_id		ap_invoice_lines_interface.po_line_location_id%TYPE;
13155 
13156     -- Purchase Order
13157     l_ref_doc_application_id		zx_transaction_lines_gt.ref_doc_application_id%TYPE;
13158     l_ref_doc_entity_code		zx_transaction_lines_gt.ref_doc_entity_code%TYPE;
13159     l_ref_doc_event_class_code		zx_transaction_lines_gt.ref_doc_event_class_code%TYPE;
13160     l_ref_doc_line_quantity		zx_transaction_lines_gt.ref_doc_line_quantity%TYPE;
13161     l_po_header_curr_conv_rate		po_headers_all.rate%TYPE;
13162     l_uom_code                    	mtl_units_of_measure.uom_code%TYPE;
13163     l_ref_doc_trx_level_type		zx_transaction_lines_gt.ref_doc_trx_level_type%TYPE;
13164     l_dummy				number;
13165 
13166     -- Receipt
13167     l_applied_to_application_id		zx_transaction_lines_gt.applied_to_application_id%TYPE;
13168     l_applied_to_entity_code		zx_transaction_lines_gt.applied_to_entity_code%TYPE;
13169     l_applied_to_event_class_code	zx_transaction_lines_gt.applied_to_event_class_code%TYPE;
13170     l_trx_receipt_date			zx_transaction_lines_gt.trx_receipt_date%TYPE;
13171     l_ref_doc_trx_id			zx_transaction_lines_gt.ref_doc_trx_id%TYPE;
13172 
13173     -- PO Tax Determining Attributes
13174     l_trx_bus_category			zx_transaction_lines_gt.trx_business_category%TYPE;
13175     l_intended_use              	zx_lines_det_factors.line_intended_use%type;
13176     l_product_type              	zx_lines_det_factors.product_type%type;
13177     l_product_category          	zx_lines_det_factors.product_category%type;
13178     l_product_fisc_class        	zx_lines_det_factors.product_fisc_classification%type;
13179     l_user_def_fisc_class   		zx_lines_det_factors.user_defined_fisc_class%type;
13180     l_assessable_value          	zx_lines_det_factors.assessable_value%type;
13181     l_dflt_tax_class_code       	zx_transaction_lines_gt.input_tax_classification_code%type;
13182     l_allow_tax_code_override           VARCHAR2(10);
13183     l_ship_to_party_id          po_line_locations_all.ship_to_organization_id%type; -- 7262269
13184 
13185     l_debug_info			Varchar2(240);
13186     l_curr_calling_sequence		Varchar2(4000);
13187     l_return_status			BOOLEAN := TRUE;
13188 
13189     l_api_name CONSTANT VARCHAR2(100) := 'Populate_Rct_Match_Lines_GT';
13190 
13191   BEGIN
13192 
13193       ----------------------------------------------------------------------
13194       l_debug_info := 'Step 1: Get location_id for org_id';
13195       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13196           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13197       END IF;
13198       ----------------------------------------------------------------------
13199       BEGIN
13200         SELECT location_id
13201           INTO l_bill_to_location_id
13202           FROM hr_all_organization_units
13203          WHERE organization_id = P_Invoice_Header_Rec.org_id;
13204       END;
13205 
13206       ----------------------------------------------------------------------
13207       l_debug_info := 'Step 2: Get fob_lookup_code from po_vendor_sites_all';
13208       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13209           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13210       END IF;
13211       ----------------------------------------------------------------------
13212       BEGIN
13213         SELECT location_id, fob_lookup_code
13214           INTO l_location_id, l_fob_point
13215           FROM ap_supplier_sites_all
13216          WHERE vendor_site_id = P_Invoice_Header_Rec.vendor_site_id;
13217       END;
13218 
13219       ----------------------------------------------------------------------
13220       l_debug_info := 'Step 4: Populate zx_transaction_lines_gt';
13221       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13222           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13223       END IF;
13224       ----------------------------------------------------------------------
13225       IF (p_rct_match_tax_list.COUNT > 0) THEN
13226 
13227         trans_lines.EXTEND(p_rct_match_tax_list.COUNT);
13228 
13229         FOR i IN p_rct_match_tax_list.FIRST..p_rct_match_tax_list.LAST LOOP
13230 
13231           --------------------------------------------------------------------
13232           l_debug_info := 'Step 5: Get line_level_action for TAX ONLY line ';
13233           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13234               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13235           END IF;
13236           --------------------------------------------------------------------
13237           IF (p_rct_match_tax_list(i).rcv_transaction_id IS NOT NULL) THEN
13238 
13239               l_line_level_action 	:= 'CREATE_TAX_ONLY';
13240 
13241           END IF;
13242 
13243           -------------------------------------------------------------------
13244           l_debug_info := 'Step 6: Get Additional PO matched info if any ';
13245           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13246               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13247           END IF;
13248           -------------------------------------------------------------------
13249           IF (p_rct_match_tax_list(i).po_line_location_id IS NOT NULL) THEN
13250 
13251               l_po_line_location_id := p_rct_match_tax_list(i).po_line_location_id;
13252 
13253               IF NOT (AP_ETAX_UTILITY_PKG.Get_PO_Info(
13254 	              P_Po_Line_Location_Id         => l_po_line_location_id,
13255 	              P_PO_Distribution_Id          => null,
13256 	              P_Application_Id              => l_ref_doc_application_id,
13257 	              P_Entity_code                 => l_ref_doc_entity_code,
13258 	              P_Event_Class_Code            => l_ref_doc_event_class_code,
13259 	              P_PO_Quantity                 => l_ref_doc_line_quantity,
13260 	              P_Product_Org_Id              => l_product_org_id,
13261 	              P_Po_Header_Id                => l_ref_doc_trx_id,
13262 	              P_Po_Header_curr_conv_rate    => l_po_header_curr_conv_rate,
13263 	              P_Uom_Code                    => l_uom_code,
13264 	              P_Dist_Qty                    => l_dummy,
13265 	              P_Ship_Price                  => l_dummy,
13266 	              P_Error_Code                  => P_error_code,
13267 	              P_Calling_Sequence            => l_curr_calling_sequence)) THEN
13268 
13269 	             l_return_status := FALSE;
13270               END IF;
13271 
13272 	          l_ref_doc_trx_level_type := 'SHIPMENT';
13273 
13274          ELSE
13275             l_ref_doc_application_id	 := Null;
13276             l_ref_doc_entity_code	 := Null;
13277             l_ref_doc_event_class_code   := Null;
13278             l_ref_doc_line_quantity      := Null;
13279             l_product_org_id		 := Null;
13280             l_ref_doc_trx_id		 := Null;
13281             l_ref_doc_trx_level_type	 := Null;
13282             l_uom_code			 := Null;
13283          END IF;
13284 
13285           -------------------------------------------------------------------
13286           l_debug_info := 'Step 7: Get Additional receipt matched info ';
13287           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13288               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13289           END IF;
13290           -------------------------------------------------------------------
13291           IF ( l_return_status = TRUE AND
13292                p_rct_match_tax_list(i).rcv_transaction_id IS NOT NULL) THEN
13293             IF NOT (AP_ETAX_UTILITY_PKG.Get_Receipt_Info(
13294                P_Rcv_Transaction_Id          => p_rct_match_tax_list(i).rcv_transaction_id,
13295                P_Application_Id              => l_applied_to_application_id,
13296                P_Entity_code                 => l_applied_to_entity_code,
13297                P_Event_Class_Code            => l_applied_to_event_class_code,
13298                P_Transaction_Date            => l_trx_receipt_date,
13299                P_Error_Code                  => P_error_code,
13300                P_Calling_Sequence            => l_curr_calling_sequence)) THEN
13301 
13302                l_return_status := FALSE;
13303             END IF;
13304          ELSE
13305 	    l_applied_to_application_id   := Null;
13306             l_applied_to_entity_code      := Null;
13307             l_applied_to_event_class_code := Null;
13308 	    l_trx_receipt_date		  := Null;
13309          END IF;
13310 
13311 	/*
13312 	IF (l_dflt_tax_class_code IS NULL
13313             AND p_rct_match_tax_list(i).tax_classification_code IS NULL) THEN
13314 
13315 	    ZX_AP_TAX_CLASSIFICATN_DEF_PKG.get_default_tax_classification
13316 			            (p_ref_doc_application_id           => l_ref_doc_application_id,
13317 			             p_ref_doc_entity_code              => l_ref_doc_entity_code,
13318 			             p_ref_doc_event_class_code         => l_ref_doc_event_class_code,
13319 			             p_ref_doc_trx_id                   => l_ref_doc_trx_id,
13320 			             p_ref_doc_line_id                  => p_rct_match_tax_list(i).po_line_location_id,
13321 			             p_ref_doc_trx_level_type           => 'SHIPMENT',
13322 			             p_vendor_id                        => P_Invoice_Header_Rec.vendor_id,
13323 			             p_vendor_site_id                   => P_Invoice_Header_Rec.vendor_site_id,
13324 			             p_code_combination_id              => p_rct_match_tax_list(i).default_dist_ccid,
13325 			             p_concatenated_segments            => null,
13326 			             p_templ_tax_classification_cd      => null,
13327 			             p_ship_to_location_id              => p_rct_match_tax_list(i).ship_to_location_id,
13328 			             p_ship_to_loc_org_id               => null,
13329 			             p_inventory_item_id                => p_rct_match_tax_list(i).inventory_item_id,
13330 			             p_item_org_id                      => l_product_org_id,
13331 			             p_tax_classification_code          => l_dflt_tax_class_code,
13332 			             p_allow_tax_code_override_flag     => l_allow_tax_code_override,
13333 			             APPL_SHORT_NAME                    => 'SQLAP',
13334 			             FUNC_SHORT_NAME                    => 'NONE',
13335 			             p_calling_sequence                 => 'AP_ETAX_SERVICES_PKG',
13336 			             p_event_class_code                 => P_Event_Class_Code,
13337 			             p_entity_code                      => 'AP_INVOICES',
13338 			             p_application_id                   => 200,
13339 			             p_internal_organization_id         => P_Invoice_Header_Rec.org_id);
13340 
13341 	END IF; */
13342 
13343          IF (l_return_status = TRUE) THEN
13344 
13345 	      IF NOT (AP_ETAX_UTILITY_PKG.Get_Line_Class(
13346 		             P_Invoice_Type_Lookup_Code    => P_Invoice_Header_Rec.invoice_type_lookup_code,
13347 		             P_Inv_Line_Type               => p_rct_match_tax_list(i).line_type_lookup_code,
13348 		             P_Line_Location_Id            => p_rct_match_tax_list(i).po_line_location_id,
13349 		             P_Line_Class                  => l_line_class,
13350 		             P_Error_Code                  => P_error_code,
13351 		             P_Calling_Sequence            => l_curr_calling_sequence)) THEN
13352 
13353                  l_return_status := FALSE;
13354              END IF;
13355          END IF;
13356 
13357           -------------------------------------------------------------------
13358           l_debug_info := 'Step 11: Populate pl/sql table';
13359           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13360               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13361           END IF;
13362           -------------------------------------------------------------------
13363           IF (l_return_status = TRUE ) THEN
13364 
13365             trans_lines(i).application_id			:= 200;
13366             trans_lines(i).entity_code				:= 'AP_INVOICES';
13367             trans_lines(i).event_class_code			:= p_event_class_code;
13368             trans_lines(i).trx_id				:= P_Invoice_Header_Rec.invoice_id;
13369             trans_lines(i).trx_level_type			:= 'LINE';
13370             trans_lines(i).line_level_action			:= l_line_level_action;
13371             trans_lines(i).line_class 				:= l_line_class;
13372             trans_lines(i).account_ccid				:= p_rct_match_tax_list(i).default_dist_ccid;
13373 
13374 	    -- Tax for PO/Receipt Match is always treated as exclusive.
13375             trans_lines(i).line_amt_includes_tax_flag		:= 'N';
13376             trans_lines(i).historical_flag			:= 'N';
13377 
13378 	    -- Pass negative line numbers to not conflict with existing invoice lines.
13379             trans_lines(i).trx_line_id				:= -i;
13380             trans_lines(i).trx_line_number			:= -i;
13381 
13382             trans_lines(i).trx_line_type			:= p_rct_match_tax_list(i).line_type_lookup_code;
13383             trans_lines(i).trx_line_description			:= p_rct_match_tax_list(i).description;
13384             trans_lines(i).trx_line_date                        := P_Invoice_Header_Rec.invoice_date;
13385             trans_lines(i).trx_line_gl_date                     := p_rct_match_tax_list(i).accounting_date;
13386             trans_lines(i).trx_receipt_date                     := l_trx_receipt_date;
13387 
13388             trans_lines(i).uom_code				:= p_rct_match_tax_list(i).unit_meas_lookup_code;
13389             trans_lines(i).trx_line_quantity 			:= p_rct_match_tax_list(i).quantity_invoiced;
13390             trans_lines(i).unit_price				:= p_rct_match_tax_list(i).unit_price;
13391             trans_lines(i).line_amt				:= p_rct_match_tax_list(i).amount;
13392 
13393             -- 7262269
13394             IF p_rct_match_tax_list(i).po_line_location_id IS NOT NULL THEN
13395                l_ship_to_party_id := get_po_ship_to_org_id (p_rct_match_tax_list(i).po_line_location_id);
13396             ELSE
13397                l_ship_to_party_id := p_rct_match_tax_list(i).org_id;
13398             END IF;
13399 
13400             trans_lines(i).ship_to_party_id		:= l_ship_to_party_id;
13401             -- 7262269
13402 
13403             trans_lines(i).bill_to_party_id			:= p_rct_match_tax_list(i).org_id;
13404 
13405             trans_lines(i).ship_from_party_id			:= P_Invoice_Header_Rec.party_id;
13406             trans_lines(i).bill_from_party_id			:= P_Invoice_Header_Rec.party_id;
13407 
13408             trans_lines(i).ship_from_party_site_id		:= P_Invoice_Header_Rec.party_site_id;
13409             trans_lines(i).bill_from_party_site_id		:= P_Invoice_Header_Rec.party_site_id;
13410 
13411             trans_lines(i).ship_to_location_id			:= p_rct_match_tax_list(i).ship_to_location_id;
13412             trans_lines(i).bill_to_location_id			:= l_bill_to_location_id;
13413 
13414             trans_lines(i).ship_from_location_id		:= l_location_id;
13415             trans_lines(i).bill_from_location_id          	:= l_location_id;
13416 
13417             trans_lines(i).trx_business_category		:= l_trx_bus_category;
13418             trans_lines(i).line_intended_use			:= l_intended_use;
13419             trans_lines(i).user_defined_fisc_class 		:= l_user_def_fisc_class;
13420             trans_lines(i).product_fisc_classification		:= l_product_fisc_class;
13421             trans_lines(i).product_type				:= l_product_type;
13422             trans_lines(i).product_category			:= l_product_category;
13423             trans_lines(i).assessable_value 		        := nvl(p_rct_match_tax_list(i).assessable_value,l_assessable_value);
13424             trans_lines(i).input_tax_classification_code	:= nvl(p_rct_match_tax_list(i).tax_classification_code,l_dflt_tax_class_code);
13425 
13426             trans_lines(i).product_id                           := p_rct_match_tax_list(i).inventory_item_id;
13427             trans_lines(i).product_description                  := p_rct_match_tax_list(i).item_description;
13428             trans_lines(i).product_org_id                       := l_product_org_id;
13429             trans_lines(i).fob_point                            := l_fob_point;
13430 
13431             trans_lines(i).ref_doc_application_id		:= l_ref_doc_application_id;
13432             trans_lines(i).ref_doc_entity_code			:= l_ref_doc_entity_code;
13433             trans_lines(i).ref_doc_event_class_code		:= l_ref_doc_event_class_code;
13434             trans_lines(i).ref_doc_trx_id			:= l_ref_doc_trx_id;
13435             trans_lines(i).ref_doc_line_id			:= p_rct_match_tax_list(i).po_line_location_id;
13436             trans_lines(i).ref_doc_trx_level_type		:= l_ref_doc_trx_level_type;
13437             trans_lines(i).ref_doc_line_quantity		:= l_ref_doc_line_quantity;
13438 
13439             trans_lines(i).applied_to_application_id		:= l_applied_to_application_id;
13440             trans_lines(i).applied_to_entity_code		:= l_applied_to_entity_code;
13441             trans_lines(i).applied_to_event_class_code		:= l_applied_to_event_class_code;
13442             trans_lines(i).applied_to_trx_id			:= p_rct_match_tax_list(i).rcv_transaction_id;
13443 
13444 	    IF p_rct_match_tax_list(i).rcv_transaction_id IS NOT NULL THEN
13445                trans_lines(i).applied_to_trx_line_id		:= p_rct_match_tax_list(i).po_line_location_id;
13446             END IF;
13447 
13448             trans_lines(i).product_id                           := p_rct_match_tax_list(i).inventory_item_id;
13449             trans_lines(i).product_description                  := p_rct_match_tax_list(i).item_description;
13450             trans_lines(i).product_org_id			:= l_product_org_id;
13451             trans_lines(i).fob_point				:= l_fob_point;
13452 
13453           END IF; -- l_return_status
13454 
13455           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13456 	      FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: ' || trans_lines(i).event_class_code);
13457 	      FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '           || trans_lines(i).trx_id);
13458 	      FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_id: '      || trans_lines(i).trx_line_id);
13459 	      FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_level_type: '   || trans_lines(i).trx_level_type);
13460 	      FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_level_action: '|| trans_lines(i).line_level_action);
13461 	      FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_class: '       || trans_lines(i).line_class);
13462 	      FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_amt: '         || trans_lines(i).line_amt);
13463           END IF;
13464 
13465         END LOOP;  -- end of loop TAX lines
13466       END IF; -- is p_rct_match_tax_list populated
13467 
13468     -------------------------------------------------------------------
13469     l_debug_info := 'Step 14: Bulk Insert into global temp tables';
13470     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13471       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13472     END IF;
13473     -------------------------------------------------------------------
13474     IF (l_return_status = TRUE) THEN
13475 
13476       IF (trans_lines.COUNT > 0) THEN
13477 
13478 	DELETE FROM zx_transaction_lines_gt;
13479 
13480         FORALL m IN trans_lines.FIRST..trans_lines.LAST
13481           INSERT INTO zx_transaction_lines_gt
13482           VALUES trans_lines(m);
13483 
13484       END IF;
13485     END IF;
13486 
13487     RETURN TRUE;
13488 
13489   EXCEPTION
13490     WHEN OTHERS THEN
13491       IF (SQLCODE <> -20001) THEN
13492         FND_MESSAGE.SET_NAME ('SQLAP', 'AP_DEBUG');
13493         FND_MESSAGE.SET_TOKEN('ERROR', SQLERRM);
13494         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
13495         FND_MESSAGE.SET_TOKEN('PARAMETERS', ' P_Invoice_Id = '      ||P_Invoice_Header_Rec.Invoice_Id  ||
13496                                             ' P_Error_Code = '      ||P_Error_Code  ||
13497                                             ' P_Calling_Sequence = '||P_Calling_Sequence);
13498         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
13499 
13500       END IF;
13501 
13502       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13503           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,sqlerrm);
13504       END IF;
13505 
13506       APP_EXCEPTION.RAISE_EXCEPTION;
13507 
13508   END Populate_Rct_Match_Lines_GT;
13509 
13510 FUNCTION Bulk_Populate_Headers_GT(
13511              p_validation_request_id    IN  NUMBER,
13512              p_calling_mode             IN  VARCHAR2,
13513              p_error_code               OUT NOCOPY VARCHAR2) RETURN BOOLEAN IS
13514 
13515     l_api_name       	CONSTANT VARCHAR2(100) := 'Bulk_Populate_Headers_GT';
13516 
13517 BEGIN
13518         Print(l_api_name, 'Bulk_Populate_Headers_GT (+)');
13519 
13520         DELETE FROM zx_trx_headers_gt;
13521 
13522 	INSERT INTO zx_trx_headers_gt(
13523 	        internal_organization_id,
13524 	        application_id,
13525 	        entity_code,
13526 	        event_class_code,
13527 	        event_type_code,
13528 	        trx_id,
13529 	        trx_date,
13530 		ledger_id,
13531 	        trx_currency_code,
13532 	        currency_conversion_date,
13533 	        currency_conversion_rate,
13534 	        currency_conversion_type,
13535 	        minimum_accountable_unit,
13536 	        precision,
13537 	        legal_entity_id,
13538 	        rounding_ship_from_party_id,
13539 	        rounding_bill_from_party_id,
13540 	        rndg_ship_from_party_site_id,
13541 	        rndg_bill_from_party_site_id,
13542 	        related_doc_application_id,
13543 	        related_doc_entity_code,
13544 	        related_doc_event_class_code,
13545 	        related_doc_trx_id,
13546 	        related_doc_number,
13547 	        related_doc_date,
13548 	        default_taxation_country,
13549 	        quote_flag,
13550 	        ctrl_total_hdr_tx_amt,
13551 	        trx_number,
13552 		trx_description,
13553 	        doc_seq_id,
13554 	        doc_seq_name,
13555 	        doc_seq_value,
13556 	        document_sub_type,
13557 	        supplier_tax_invoice_number,
13558 	        supplier_tax_invoice_date,
13559 	        supplier_exchange_rate,
13560 	        tax_invoice_date,
13561 	        tax_invoice_number,
13562 	        bill_third_pty_acct_id,
13563 	        bill_third_pty_acct_site_id,
13564 		ship_third_pty_acct_id,
13565 		ship_third_pty_acct_site_id
13566 	        )
13567 	  SELECT
13568 	        ai.org_id,								--internal_organization_id
13569 	        200,									--application_id
13570 	        'AP_INVOICES',                        					--entity_code
13571 	        (CASE
13572 	           WHEN ai.invoice_type_lookup_code IN
13573 			  ('STANDARD', 'CREDIT', 'DEBIT',   'MIXED',
13574 	                   'ADJUSTMENT', 'PO PRICE ADJUST', 'INVOICE REQUEST',
13575 	                   'CREDIT MEMO REQUEST', 'RETAINAGE RELEASE')
13576 	                THEN 'STANDARD INVOICES'
13577 	           WHEN (ai.invoice_type_lookup_code = 'PREPAYMENT')
13578 	                 or (p_calling_mode IN ('RECOUPMENT', 'DISTRIBUTE RECOUP'))
13579 	                THEN 'PREPAYMENT INVOICES'
13580 	           WHEN ai.invoice_type_lookup_code = 'EXPENSE REPORT'
13581 	                THEN 'EXPENSE REPORTS'
13582 	        END), 				     					--event_class_code
13583 	        (CASE
13584 	           WHEN ai.invoice_type_lookup_code IN
13585 			  ('STANDARD', 'CREDIT', 'DEBIT',   'MIXED',
13586 	                   'ADJUSTMENT', 'PO PRICE ADJUST', 'INVOICE REQUEST',
13587 	                   'CREDIT MEMO REQUEST', 'RETAINAGE RELEASE')
13588 	                THEN 'STANDARD '
13589 	           WHEN (ai.invoice_type_lookup_code = 'PREPAYMENT')
13590 	                 or (p_calling_mode IN ('RECOUPMENT', 'DISTRIBUTE RECOUP'))
13591 	                THEN 'PREPAYMENT '
13592 	           WHEN ai.invoice_type_lookup_code = 'EXPENSE REPORT'
13593 	                THEN 'EXPENSE REPORT '
13594 	        END)||
13595 	        DECODE(p_calling_mode,
13596 			'CALCULATE',
13597                         (CASE ((SELECT 'Y'
13598 				 FROM ap_invoice_lines_all
13599 				WHERE invoice_id = ai.invoice_id
13600 				  AND line_type_lookup_code <> 'AWT'
13601 				  AND (tax_already_calculated_flag = 'Y'
13602 			               OR  summary_tax_line_id IS NOT NULL)
13603 				  AND ROWNUM = 1)
13604                              --- Start for bug 6485124
13605                                 UNION
13606                                           (SELECT 'Y'
13607                                              FROM zx_lines_det_factors
13608                                             WHERE application_id = 200
13609                                              AND  entity_code = 'AP_INVOICES'
13610                                              AND  trx_id = ai.invoice_id
13611                                              AND  event_class_code in ('STANDARD INVOICES','PREPAYMENT INVOICES','EXPENSE REPORTS')
13612                                              AND  ROWNUM=1))
13613                              --- End for bug 6485124
13614 				WHEN 'Y' THEN 'UPDATED'
13615 		            	ELSE 'CREATED'
13616                 	END),
13617 			'DISTRIBUTE',
13618 			(CASE (SELECT 'Y'
13619 				 FROM ap_invoice_distributions_all
13620 				WHERE invoice_id = ai.invoice_id
13621 			          AND line_type_lookup_code <> 'AWT'
13622 				  AND (tax_already_distributed_flag = 'Y'
13623 			               OR detail_tax_dist_id IS NOT NULL)
13624 			          AND (related_id IS NULL
13625 				       OR related_id = invoice_distribution_id)
13626 				  AND ROWNUM = 1)
13627 				WHEN 'Y' THEN 'REDISTRIBUTE'
13628 				ELSE 'DISTRIBUTE'
13629 			END)),								--event_type_code
13630 	        ai.invoice_id,                   					--trx_id
13631 	        ai.invoice_date,                 					--trx_date
13632 	        ai.set_of_books_id,              					--ledger_id
13633 	        ai.invoice_currency_code,        					--trx_currency_code
13634 	        ai.exchange_date,                					--currency_conversion_date
13635 	        ai.exchange_rate,                					--currency_conversion_rate
13636 	        ai.exchange_rate_type,           					--currency_conversion_type
13637 	        NVL(cur.minimum_accountable_unit,
13638 			 (1/power(10,cur.precision))),   				--minimum_accountable_unit
13639 	        nvl(cur.precision,0),            					--precision
13640 	        ai.legal_entity_id,              					--legal_entity_id
13641 	        ai.party_id,                     					--rounding_ship_from_party_id
13642 	        ai.party_id,                     					--rounding_bill_from_party_id
13643 	        ai.party_site_id,                					--rndg_ship_from_party_site_id
13644 	        ai.party_site_id,                					--rndg_bill_from_party_site_id
13645                 (CASE
13646                    WHEN related_ai.invoice_type_lookup_code IS NOT NULL
13647                         THEN 200
13648                         ELSE NULL
13649                 END),									--related_doc_application_id
13650 		(CASE
13651                    WHEN related_ai.invoice_type_lookup_code IS NOT NULL
13652                         THEN 'AP_INVOICES'
13653 		        ELSE NULL
13654                 END),									--related_doc_entity_code
13655 	        (CASE
13656 	           WHEN related_ai.invoice_type_lookup_code IN
13657 		          ('STANDARD', 'CREDIT', 'DEBIT',   'MIXED',
13658 	                   'ADJUSTMENT', 'PO PRICE ADJUST', 'INVOICE REQUEST',
13659 	                   'CREDIT MEMO REQUEST', 'RETAINAGE RELEASE')
13660 	                THEN 'STANDARD INVOICES'
13661 	           WHEN ai.invoice_type_lookup_code = 'PREPAYMENT'
13662 	                THEN 'PREPAYMENT INVOICES'
13663 	           WHEN ai.invoice_type_lookup_code = 'EXPENSE REPORT'
13664 	                THEN 'EXPENSE REPORTS'
13665 	        END),									--related_doc_event_class_code
13666 	        ai.tax_related_invoice_id,       					--related_doc_trx_id
13667 	        related_ai.invoice_num,                            			--related_doc_number
13668 	        related_ai.invoice_date,                           			--related_doc_date
13669 	        ai.taxation_country,             					--default_taxation_country
13670 	        decode(p_calling_mode,
13671 				'CALCULATE QUOTE', 'Y', 'N'),				--quote_flag
13672 	        ai.control_amount,               					--ctrl_total_hdr_tx_amt
13673 	        ai.invoice_num,                  					--trx_number
13674 	        ai.description,                  					--trx_description
13675 	        ai.doc_sequence_id,              					--doc_seq_id
13676 	        doc.name,            		     					--doc_seq_name
13677 	        nvl(to_char(ai.doc_sequence_value), ai.voucher_num),				--doc_seq_value bug6656894
13678 	        ai.document_sub_type,            					--document_sub_type
13679 	        ai.supplier_tax_invoice_number,  					--supplier_tax_invoice_number
13680 	        ai.supplier_tax_invoice_date,    					--supplier_tax_invoice_date
13681 	        ai.supplier_tax_exchange_rate,   					--supplier_exchange_rate
13682 	        ai.tax_invoice_recording_date,   					--tax_invoice_date
13683 	        ai.tax_invoice_internal_seq,     					--tax_invoice_number
13684 	        ai.vendor_id,			     					--bill_third_pty_acct_id
13685 		ai.vendor_site_id,		     					--bill_third_pty_acct_site_id
13686 	        ai.vendor_id,                    					--ship_third_pty_acct_id
13687 	        ai.vendor_site_id                					--ship_third_pty_acct_site_id
13688 	  FROM  ap_invoices_all        ai,
13689 	        fnd_currencies         cur,
13690 	        fnd_document_sequences doc,
13691 	        ap_invoices_all        related_ai
13692 	 WHERE  ai.invoice_currency_code  = cur.currency_code
13693 	   AND  ai.doc_sequence_id        = doc.doc_sequence_id (+)
13694 	   AND  ai.tax_related_invoice_id = related_ai.invoice_id (+)
13695            AND  (nvl(ai.quick_credit,'N') = 'N'
13696                  OR ai.invoice_type_lookup_code NOT IN ('AWT', 'INTEREST'))
13697 	   AND  ai.validation_request_id  = p_validation_request_id;
13698 
13699     -- Global Variable g_invoices_to_process should be initialized right after
13700     -- the previous insert. No other sql statements must be placed after the
13701     -- insert because the sql rowcount will be reset. This variable is used in
13702     -- calculate_tax and determine_recovery.
13703 
13704     g_invoices_to_process := sql%rowcount;
13705 
13706     Print(l_api_name, 'Rows inserted in zx_trx_headers_gt '||g_invoices_to_process);
13707     Print(l_api_name, 'Bulk_Populate_Headers_GT (-)');
13708 
13709     RETURN TRUE;
13710 
13711 EXCEPTION
13712      WHEN OTHERS THEN
13713           RETURN FALSE;
13714 
13715 END Bulk_Populate_Headers_GT;
13716 
13717 FUNCTION Update_Distributions(
13718              P_Invoice_header_rec    IN ap_invoices_all%ROWTYPE,
13719              P_Calling_Mode          IN VARCHAR2,
13720              P_All_Error_Messages    IN VARCHAR2,
13721              P_Error_Code            OUT NOCOPY VARCHAR2,
13722              P_Calling_Sequence      IN VARCHAR2) RETURN BOOLEAN
13723 IS
13724 
13725     CURSOR c_reverse_dist (c_invoice_id NUMBER) IS
13726         SELECT aid_reverse.invoice_distribution_id invoice_distribution_id,
13727                aid_parent.invoice_distribution_id  parent_reversal_id,
13728                aid_parent.dist_code_combination_id parent_ccid
13729           FROM ap_invoice_distributions_all aid_reverse,
13730                ap_invoice_distributions_all aid_parent,
13731                zx_rec_nrec_dist zx
13732          WHERE aid_reverse.invoice_id         = c_invoice_id
13733            AND aid_reverse.detail_tax_dist_id = zx.rec_nrec_tax_dist_id
13734            AND aid_parent.detail_tax_dist_id  = zx.reversed_tax_dist_id
13735            AND aid_parent.line_type_lookup_code = aid_reverse.line_type_lookup_code;
13736 
13737    --Bug7253420 For RTAX/NRTAX and TRV having same detail_tax_dist_id
13738    --so the TRV distribution id is populating in the parent_reversal_id of
13739    --RTAX/NRTAX reversal distribution. So to get the Proper Reversal entries
13740    --line_type_lookup_code condition is added
13741 
13742    --       Cursor added for 6155675 - To get the tax amount which is included in
13743    --       the ITEM line (for tax inclusive lines and then we will subtract
13744    --       the included tax amount from the ITEM distributions .
13745     CURSOR  c_included_tax_amount is
13746         SELECT amount,included_tax_amount,line_number,
13747                (total_rec_tax_amt_funcl_curr +      total_nrec_tax_amt_funcl_curr) base_included_tax_amount
13748         FROM   ap_invoice_lines_all
13749         WHERE  invoice_id =  p_invoice_header_rec.invoice_id
13750         AND    line_type_lookup_code IN ('ITEM', 'ACCRUAL', 'FREIGHT', 'MISCELLANEOUS','PREPAY') --/*Bug7338249*/
13751         AND    included_tax_amount <> 0 ;
13752 
13753     TYPE reversal_dist_info IS RECORD
13754                    (invoice_distribution_id ap_invoice_distributions_all.invoice_distribution_id%TYPE,
13755                     parent_reversal_id      ap_invoice_distributions_all.parent_reversal_id%TYPE,
13756                     parent_ccid             ap_invoice_distributions_all.dist_code_combination_id%TYPE);
13757 
13758     TYPE reversal_dist_type IS TABLE OF reversal_dist_info;
13759 
13760     l_reveral_dist_tab reversal_dist_type;
13761 
13762     l_debug_info		VARCHAR2(240);
13763     l_curr_calling_sequence	VARCHAR2(4000);
13764     l_api_name			CONSTANT VARCHAR2(100) := 'Update_Distributions';
13765     l_dist_amt                  ap_invoice_distributions_all.amount%type; -- for bug 6326552
13766     l_tot_tax_amt               ap_invoice_distributions_all.amount%type; -- for bug 6326552
13767 BEGIN
13768 
13769     -------------------------------------------------------------------
13770     l_debug_info := 'Step 1: Update the related_id column';
13771     -------------------------------------------------------------------
13772     IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13773          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13774     END IF;
13775 
13776       UPDATE ap_invoice_distributions_all aid
13777          SET related_id = (SELECT DECODE(MIN(nrtax.invoice_distribution_id),
13778 						NULL, MIN(other.invoice_distribution_id),
13779                                     		MIN(nrtax.invoice_distribution_id))
13780                              FROM ap_invoice_distributions_all nrtax,
13781                                   ap_invoice_distributions_all other
13782                             WHERE nrtax.invoice_id 	   = aid.invoice_id
13783                               AND other.invoice_id	   = aid.invoice_id
13784                               AND nrtax.detail_tax_dist_id = aid.detail_tax_dist_id
13785                               AND other.detail_tax_dist_id = aid.detail_tax_dist_id
13786                               AND (nrtax.line_type_lookup_code = 'NONREC_TAX'
13787                                    OR other.line_type_lookup_code IN ('TIPV', 'TRV'))
13788                              GROUP BY 1)
13789       WHERE aid.invoice_id = P_Invoice_Header_Rec.invoice_id
13790         AND aid.line_type_lookup_code in ('NONREC_TAX', 'TIPV', 'TRV', 'TERV')
13791         AND EXISTS (SELECT aid1.detail_tax_dist_id
13792                       FROM ap_invoice_distributions_all aid1
13793                      WHERE aid1.invoice_id = aid.invoice_id
13794                        AND aid1.detail_tax_dist_id = aid.detail_tax_dist_id
13795                     HAVING count(*) > 1
13796                      GROUP BY aid1.detail_tax_dist_id);
13797 
13798     -------------------------------------------------------------------
13799     l_debug_info := 'Step 2: Update the related_id column for self assessed distributions';
13800     -------------------------------------------------------------------
13801     IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13802          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13803     END IF;
13804 
13805       UPDATE ap_self_assessed_tax_dist_all aid
13806          SET related_id = (SELECT DECODE(MIN(nrtax.invoice_distribution_id),
13807 					 NULL, MIN(other.invoice_distribution_id),
13808 					 MIN(nrtax.invoice_distribution_id))
13809                              FROM ap_self_assessed_tax_dist_all nrtax,
13810                                   ap_self_assessed_tax_dist_all other
13811                             WHERE nrtax.invoice_id 	   = aid.invoice_id
13812                               AND other.invoice_id 	   = aid.invoice_id
13813                               AND nrtax.detail_tax_dist_id = aid.detail_tax_dist_id
13814                               AND other.detail_tax_dist_id = aid.detail_tax_dist_id
13815                               AND (nrtax.line_type_lookup_code = 'NONREC_TAX'
13816                                    OR other.line_type_lookup_code IN ('TIPV', 'TRV', 'TERV'))
13817                              GROUP BY 1)
13818       WHERE aid.invoice_id = P_Invoice_Header_Rec.invoice_id
13819         AND aid.line_type_lookup_code in ('NONREC_TAX', 'TIPV', 'TRV', 'TERV')
13820         AND EXISTS (SELECT aid1.detail_tax_dist_id
13821                       FROM ap_self_assessed_tax_dist_all aid1
13822                      WHERE aid1.invoice_id = aid.invoice_id
13823                        AND aid1.detail_tax_dist_id = aid.detail_tax_dist_id
13824                     HAVING count(*) > 1
13825                      GROUP BY aid1.detail_tax_dist_id);
13826 
13827 
13828     -------------------------------------------------------------------
13829     l_debug_info := 'Step 3: Update rounding_amt for the primary NONREC tax dist';
13830     -------------------------------------------------------------------
13831     IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13832        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13833     END IF;
13834 
13835       UPDATE ap_invoice_distributions_all aid
13836          SET rounding_amt =
13837              (SELECT zd.func_curr_rounding_adjustment
13838                 FROM zx_rec_nrec_dist zd
13839                WHERE zd.rec_nrec_tax_dist_id = aid.detail_tax_dist_id)
13840       WHERE aid.invoice_id = P_Invoice_Header_Rec.invoice_id
13841         AND aid.line_type_lookup_code in ('NONREC_TAX', 'TIPV', 'TRV', 'TERV')
13842         AND (aid.related_id IS NULL
13843              OR aid.related_id = aid.invoice_distribution_id);
13844 
13845 
13846     -------------------------------------------------------------------
13847     l_debug_info := 'Step 4: Update rounding_amt for the primary NONREC self assessed dist';
13848     -------------------------------------------------------------------
13849     IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13850        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13851     END IF;
13852 
13853       UPDATE ap_self_assessed_tax_dist_all aid
13854          SET rounding_amt =
13855              (SELECT zd.func_curr_rounding_adjustment
13856                 FROM zx_rec_nrec_dist zd
13857                WHERE zd.rec_nrec_tax_dist_id = aid.detail_tax_dist_id)
13858       WHERE aid.invoice_id = P_Invoice_Header_Rec.invoice_id
13859         AND aid.line_type_lookup_code in ('NONREC_TAX', 'TIPV', 'TRV', 'TERV')
13860         AND (aid.related_id IS NULL
13861              OR aid.related_id = aid.invoice_distribution_id);
13862 
13863 
13864    -----------------------------------------------------------------
13865    l_debug_info := 'Step 5: Update REC and NONREC totals at line level';
13866    -----------------------------------------------------------------
13867     IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13868        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13869     END IF;
13870 
13871       UPDATE ap_invoice_lines_all ail
13872          SET (ail.total_rec_tax_amount,
13873               ail.total_nrec_tax_amount,
13874               ail.total_rec_tax_amt_funcl_curr,
13875               ail.total_nrec_tax_amt_funcl_curr) =
13876              (SELECT SUM(DECODE(NVL(zd.recoverable_flag, 'N'),
13877                                 'Y', NVL(zd.rec_nrec_tax_amt, 0),
13878                                 0)),
13879                      SUM(DECODE(NVL(zd.recoverable_flag, 'N'),
13880                                 'N', NVL(zd.rec_nrec_tax_amt, 0),
13881                                  0)),
13882                      SUM(DECODE(NVL(zd.recoverable_flag, 'N'),
13883                                 'Y', NVL(zd.rec_nrec_tax_amt_funcl_curr, 0),
13884                                  0)),
13885                      SUM(DECODE(NVL(zd.recoverable_flag, 'N'),
13886                                 'N', NVL(zd.rec_nrec_tax_amt_funcl_curr, 0),
13887                                  0))
13888                 FROM zx_rec_nrec_dist zd
13889                WHERE application_id   = AP_ETAX_PKG.AP_APPLICATION_ID
13890                  AND entity_code      = AP_ETAX_PKG.AP_ENTITY_CODE
13891                  AND event_class_code IN (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
13892 					  AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
13893 					  AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
13894                  AND zd.trx_id		= ail.invoice_id
13895                  AND zd.trx_line_id	= ail.line_number
13896                  AND NVL(zd.self_assessed_flag, 'N') = 'N')
13897        WHERE ail.invoice_id		= P_Invoice_Header_Rec.invoice_id
13898          AND (ail.summary_tax_line_id IS NOT NULL
13899               OR (ail.line_type_lookup_code <> 'TAX'
13900                   AND NVL(ail.included_tax_amount, 0) <> 0));
13901 
13902        -- the total will be updated in the TAX lines for any exclusive tax
13903        -- line created and in the taxable line (ITEM, PREPAY) in the  case
13904        -- the calculation is inclusive
13905 
13906    -----------------------------------------------------------------
13907    l_debug_info := 'Step 5: Update tax_already_distributed_flag';
13908    -----------------------------------------------------------------
13909    IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13910        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13911    END IF;
13912 
13913       UPDATE ap_invoice_distributions_all aid
13914          SET aid.tax_already_distributed_flag = 'Y'
13915        WHERE aid.invoice_id = p_invoice_header_rec.invoice_id
13916          AND NVL(aid.tax_already_distributed_flag, 'N') = 'N'
13917          AND aid.invoice_distribution_id IN
13918              ( SELECT aid1.charge_applicable_to_dist_id
13919                  FROM ap_invoice_distributions_all aid1,
13920                       zx_rec_nrec_dist zd
13921                 WHERE zd.REC_NREC_TAX_DIST_ID = aid1.DETAIL_TAX_DIST_ID
13922                   AND aid1.invoice_id = p_invoice_header_rec.invoice_id );
13923 
13924    -----------------------------------------------------------------
13925    l_debug_info := 'Step 6: Update generate_dists on the invoice line';
13926    -----------------------------------------------------------------
13927    IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13928        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13929    END IF;
13930 
13931    UPDATE ap_invoice_lines_all ail
13932    SET    generate_dists = 'D'
13933    WHERE  ail.invoice_id = p_invoice_header_rec.invoice_id
13934    AND    ail.generate_dists <> 'D'
13935    AND    line_type_lookup_code = 'TAX'
13936    AND    EXISTS
13937 		(SELECT aid.invoice_distribution_id
13938 	           FROM ap_invoice_distributions_all aid
13939 	          WHERE aid.invoice_id          = ail.invoice_id
13940 	            AND aid.invoice_line_number = ail.line_number);
13941 
13942     -----------------------------------------------------------------
13943     l_debug_info := 'Step 7: Update Invoice Includes Prepay Flag';
13944     -----------------------------------------------------------------
13945     UPDATE ap_invoice_distributions_all tax
13946     SET    tax.invoice_includes_prepay_flag = 'Y'
13947     WHERE  tax.invoice_id = p_invoice_header_rec.invoice_id
13948     AND    nvl(tax.invoice_includes_prepay_flag,'N') <> 'Y'
13949     AND    line_type_lookup_code in ('NONREC_TAX','REC_TAX','TIPV','TERV','TRV')
13950     AND    exists
13951     		(SELECT 1
13952 	         FROM   ap_invoice_lines_all prepay
13953 	         WHERE  prepay.invoice_id	     = tax.invoice_id
13954        	         AND    prepay.line_number	     = tax.invoice_line_number
13955 	         AND    prepay.line_type_lookup_code = 'TAX'
13956 	         AND    prepay.prepay_line_number    IS NOT NULL
13957 	         AND    nvl(prepay.invoice_includes_prepay_flag,'N') = 'Y');
13958 
13959    -- For Loop added for bug 6155675 to subtract the inclusive tax amount
13960    -- from ITEM line.
13961    FOR i  in c_included_tax_amount LOOP
13962 
13963    ------------------------------------------------------------------------
13964    l_debug_info := 'Step 7.1: Select sum of dist amount for each ITEM line';
13965    ------------------------------------------------------------------------
13966 
13967    IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13968        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13969    END IF;
13970 
13971 
13972      SELECT sum(amount) -- Select added for bug 6326552 to make sure
13973      INTO   l_dist_amt  -- sure that we update the distributions only once.
13974      FROM   ap_invoice_distributions_All
13975      WHERE  invoice_id =  p_invoice_header_rec.invoice_id
13976      AND    invoice_line_number = i.line_number
13977      AND    line_type_lookup_code IN ('ITEM',   --Bug6653070 Added SUM()
13978                                                  --instead of only amount
13979 -- bug 7145041: add start
13980 -- These lookup codes also have inclusive amount included in their amount.
13981 			'ACCRUAL', 'FREIGHT', 'MISCELLANEOUS','PREPAY');
13982 -- bug 7145041: add end
13983 
13984    ------------------------------------------------------------------------
13985       l_debug_info := 'Step 7.2: sum of dist amount for each ITEM line' ;
13986    ------------------------------------------------------------------------
13987 
13988    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13989       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info||' '||i.line_number||' '||l_dist_amt);
13990    END IF;
13991 
13992    ------------------------------------------------------------------------
13993    l_debug_info := 'Step 7.3: Select sum of dist amount for each TAX line';
13994    ------------------------------------------------------------------------
13995 
13996    IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13997        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13998    END IF;
13999 
14000 
14001 
14002      SELECT SUM(amount)         -- Select added for bug 6326552 to make sure
14003      INTO   l_tot_tax_amt       -- sure that we update the distributions if
14004                                 --  if included tax amount is changed at
14005        			       --  line level.       .
14006      FROM   ap_invoice_distributions_All
14007      WHERE  invoice_id =  p_invoice_header_rec.invoice_id
14008      AND    invoice_line_number = i.line_number
14009      AND    line_type_lookup_code IN ('REC_TAX','NONREC_TAX','TRV','TERV','TIPV');
14010 
14011 
14012      ------------------------------------------------------------------------
14013       l_debug_info := 'Step 7.4: Sum of dist amount for TAX line' ;
14014      ------------------------------------------------------------------------
14015 
14016       IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
14017          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info||' '||i.line_number||' '||l_tot_tax_amt);
14018       END IF;
14019 
14020       IF (i.amount = l_dist_amt) OR (i.included_tax_amount <> l_tot_tax_amt) THEN
14021 
14022 
14023      ------------------------------------------------------------------------
14024       l_debug_info := 'Step 7.5: Update dist amount for each included TAX' ;
14025      ------------------------------------------------------------------------
14026 
14027       IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
14028          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
14029       END IF;
14030 
14031       UPDATE ap_invoice_distributions_All aid1    --Bug6653070
14032         SET    aid1.amount = aid1.amount - nvl((SELECT SUM(nvl(amount,0))
14033                                   FROM ap_invoice_distributions_All aid2
14034                                  WHERE aid2.invoice_id =  p_invoice_header_rec.invoice_id
14035                                   AND  aid2.invoice_line_number = i.line_number
14036                                   AND  aid2.charge_applicable_to_dist_id = aid1.invoice_distribution_id
14037                                   AND  aid2.line_type_lookup_code IN ('REC_TAX','NONREC_TAX', 'TIPV', 'TRV')),0),
14038                aid1.base_amount =aid1.base_amount- nvl((SELECT SUM(nvl(base_amount,0))
14039                                             FROM ap_invoice_distributions_All aid3
14040                                            WHERE aid3.invoice_id =  p_invoice_header_rec.invoice_id
14041                                              AND aid3.invoice_line_number = i.line_number
14042 					                         AND aid3.charge_applicable_to_dist_id = aid1.invoice_distribution_id
14043                                              AND aid3.line_type_lookup_code IN ('REC_TAX','NONREC_TAX', 'TIPV', 'TRV','TERV')),0)
14044         WHERE  aid1.invoice_id =  p_invoice_header_rec.invoice_id
14045         AND    aid1.invoice_line_number = i.line_number
14046         AND    aid1.line_type_lookup_code IN ('ITEM', 'ACCRUAL', 'FREIGHT', 'MISCELLANEOUS','PREPAY');/*Bug7338249*/
14047 
14048       END IF;
14049 
14050    END LOOP;
14051 
14052 
14053    -----------------------------------------------------------------
14054    l_debug_info := 'Step 8: Update parent_reversal_id and parent_ccid';
14055    -----------------------------------------------------------------
14056     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
14057         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
14058     END IF;
14059 
14060     OPEN  c_reverse_dist (p_invoice_header_rec.invoice_id);
14061     FETCH c_reverse_dist
14062      BULK COLLECT INTO l_reveral_dist_tab;
14063     CLOSE c_reverse_dist;
14064 
14065     IF l_reveral_dist_tab.count > 0 THEN
14066        FOR h in l_reveral_dist_tab.first .. l_reveral_dist_tab.last LOOP
14067 
14068            UPDATE ap_invoice_distributions_all
14069 	   SET    parent_reversal_id       = l_reveral_dist_tab(h).parent_reversal_id
14070 	         ,dist_code_combination_id = l_reveral_dist_tab(h).parent_ccid
14071            WHERE invoice_distribution_id   = l_reveral_dist_tab(h).invoice_distribution_id
14072              AND nvl(reversal_flag, 'N') <> 'Y';
14073 
14074        END LOOP;
14075    END IF;
14076 
14077    -----------------------------------------------------------------
14078    l_debug_info := 'Step 9: Update reversal_flag';
14079    -----------------------------------------------------------------
14080     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
14081         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
14082     END IF;
14083 
14084     UPDATE ap_invoice_distributions_all aid
14085        SET reversal_flag = (select reverse_flag
14086                               from zx_rec_nrec_dist zx
14087                              where zx.rec_nrec_tax_dist_id = aid.detail_tax_dist_id)
14088      WHERE aid.invoice_id = p_invoice_header_rec.invoice_id
14089        AND aid.detail_tax_dist_id IS NOT NULL;
14090 
14091    RETURN TRUE;
14092 
14093 EXCEPTION
14094        WHEN NO_DATA_FOUND THEN
14095             IF (G_LEVEL_EXCEPTION >= G_CURRENT_RUNTIME_LEVEL) THEN
14096                 FND_LOG.STRING(G_LEVEL_EXCEPTION,G_MODULE_NAME||l_api_name,SQLERRM);
14097             END IF;
14098 
14099        WHEN OTHERS THEN
14100          IF (SQLCODE <> -20001) THEN
14101              FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
14102              FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
14103              FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
14104              FND_MESSAGE.SET_TOKEN('PARAMETERS',
14105                                 ' P_Invoice_Id = '||P_Invoice_Header_Rec.invoice_id||
14106                                 ' P_Error_Code = '||P_Error_Code||
14107                                 ' P_Calling_Sequence = '||P_Calling_Sequence);
14108              FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
14109          END IF;
14110 
14111          IF ( G_LEVEL_EXCEPTION >= G_CURRENT_RUNTIME_LEVEL) THEN
14112             FND_LOG.STRING(G_LEVEL_EXCEPTION,G_MODULE_NAME||l_api_name,SQLERRM);
14113          END IF;
14114 
14115          APP_EXCEPTION.RAISE_EXCEPTION;
14116 
14117 END Update_Distributions;
14118 
14119 PROCEDURE Cache_Line_Defaults
14120                         (p_org_id               IN ap_invoices_all.org_id%type,
14121                          p_vendor_site_id       IN ap_supplier_sites_all.vendor_site_id%type,
14122                          p_calling_sequence     IN VARCHAR2) IS
14123 
14124     l_api_name                  VARCHAR2(30)    := 'Cache_Line_Defaults';
14125     l_curr_calling_sequence     VARCHAR2(2000);
14126     l_debug_info                VARCHAR2(1000);
14127 
14128 BEGIN
14129 
14130     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.'||l_api_name||'<-'||p_calling_sequence;
14131 
14132     IF NOT AP_ETAX_SERVICES_PKG.g_org_attributes.exists(p_org_id) THEN
14133 
14134        ------------------------------------------------------------
14135        l_debug_info := 'Cache Org Attributes';
14136        Print(l_api_name,l_debug_info);
14137        ------------------------------------------------------------
14138 
14139         SELECT location_id
14140           INTO AP_ETAX_SERVICES_PKG.g_org_attributes(p_org_id).bill_to_location_id
14141           FROM hr_all_organization_units
14142          WHERE organization_id = p_org_id;
14143 
14144     END IF;
14145 
14146     IF NOT AP_ETAX_SERVICES_PKG.g_site_attributes.exists(p_vendor_site_id) THEN
14147 
14148       ------------------------------------------------------------
14149       l_debug_info := 'Cache Supplier Site Attributes';
14150       Print(l_api_name,l_debug_info);
14151       ------------------------------------------------------------
14152 
14153       IF l_payment_request_flag = 'Y' THEN  --- if condition for bug 5967914
14154 
14155         SELECT  hps.location_id
14156          INTO   AP_ETAX_SERVICES_PKG.g_site_attributes(p_vendor_site_id).location_id
14157                 FROM hz_party_sites hps
14158         WHERE party_site_id = p_vendor_site_id;
14159 
14160       ELSE
14161         SELECT  location_id
14162              ,fob_lookup_code
14163         INTO  AP_ETAX_SERVICES_PKG.g_site_attributes(p_vendor_site_id).location_id
14164              ,AP_ETAX_SERVICES_PKG.g_site_attributes(p_vendor_site_id).fob_lookup_code
14165         FROM ap_supplier_sites_all
14166         WHERE vendor_site_id = p_vendor_site_id;
14167       END IF;
14168 
14169     END IF;
14170 
14171 EXCEPTION
14172     WHEN OTHERS THEN
14173       IF (SQLCODE <> -20001) THEN
14174         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
14175         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
14176         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE', l_curr_calling_sequence);
14177         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
14178       END IF;
14179       APP_EXCEPTION.RAISE_EXCEPTION;
14180 END Cache_Line_Defaults;
14181 
14182 FUNCTION Return_Other_Charge_Lines(
14183              P_Invoice_Header_Rec        IN ap_invoices_all%ROWTYPE,
14184              P_Error_Code                OUT NOCOPY VARCHAR2,
14185              P_Calling_Sequence          IN VARCHAR2) RETURN BOOLEAN
14186 IS
14187 
14188     l_debug_info                 VARCHAR2(240);
14189     l_curr_calling_sequence      VARCHAR2(4000);
14190 
14191     l_period_name                gl_period_statuses.period_name%TYPE;
14192     l_gl_date                    ap_invoice_lines_all.accounting_date%TYPE;
14193     l_wfapproval_flag		 ap_system_parameters_all.approval_workflow_flag%TYPE;
14194     l_awt_include_tax_amt        ap_system_parameters_all.awt_include_tax_amt%TYPE;
14195     l_base_currency_code         ap_system_parameters_all.base_currency_code%TYPE;
14196     l_combined_filing_flag       ap_system_parameters_all.combined_filing_flag%TYPE;
14197     l_income_tax_region_flag     ap_system_parameters_all.income_tax_region_flag%TYPE;
14198     l_income_tax_region          ap_system_parameters_all.income_tax_region%TYPE;
14199     l_disc_is_inv_less_tax_flag	 ap_system_parameters_all.disc_is_inv_less_tax_flag%TYPE;
14200     l_wfapproval_status          ap_invoice_lines_all.wfapproval_status%TYPE;
14201     l_new_amt_applicable_to_disc ap_invoices_all.amount_applicable_to_discount%TYPE;
14202     l_payment_priority           ap_batches_all.payment_priority%TYPE;
14203 
14204     l_total_tax_amount          NUMBER;
14205     l_self_assessed_tax_amt     NUMBER;
14206 
14207     -- Allocations
14208     Cursor c_item_line (c_invoice_id IN NUMBER) IS
14209 	Select ail.line_number
14210 	From   ap_invoice_lines_all ail
14211 	Where  ail.invoice_id = c_invoice_id
14212         And    ail.line_type_lookup_code = 'TAX'
14213         And    ail.prepay_invoice_id IS NULL;
14214 
14215         /*
14216 	And NOT EXISTS
14217 		(Select chrg_invoice_line_number
14218 		 From ap_allocation_rule_lines arl
14219           	 Where arl.invoice_id = ail.invoice_id
14220             	 And arl.chrg_invoice_line_number = ail.line_number);
14221         */
14222 
14223     l_item_line	     c_item_line%rowtype;
14224 
14225     l_api_name                  CONSTANT VARCHAR2(100) := 'RETURN_TAX_LINES';
14226 
14227   BEGIN
14228 
14229     l_curr_calling_sequence := 'AP_ETAX_UTILITY_PKG.Return_Tax_Lines<-'||
14230                                P_calling_sequence;
14231 
14232     -------------------------------------------------------------------
14233     l_debug_info := 'Step 1: Get ap_system_parameters data';
14234     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
14235       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
14236     END IF;
14237     -------------------------------------------------------------------
14238     BEGIN
14239       SELECT
14240           approval_workflow_flag,
14241           awt_include_tax_amt,
14242           disc_is_inv_less_tax_flag,
14243           base_currency_code,
14244           combined_filing_flag,
14245           income_tax_region_flag,
14246           income_tax_region
14247         INTO
14248           l_wfapproval_flag,
14249           l_awt_include_tax_amt,
14250           l_disc_is_inv_less_tax_flag,
14251           l_base_currency_code,
14252           l_combined_filing_flag,
14253           l_income_tax_region_flag,
14254           l_income_tax_region
14255         FROM ap_system_parameters_all
14256        WHERE org_id = P_Invoice_Header_Rec.org_id;
14257     END;
14258 
14259     -------------------------------------------------------------------
14260     l_debug_info := 'Step 2: Update existing exclusive tax lines';
14261     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
14262       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
14263     END IF;
14264     -------------------------------------------------------------------
14265     BEGIN
14266       UPDATE ap_invoice_lines_all ail
14267          SET
14268        (ail.description,
14269         ail.amount,
14270         ail.base_amount,
14271         ail.cancelled_flag,
14272         ail.last_updated_by,
14273         ail.last_update_login,
14274         ail.last_update_date,
14275         ail.tax_regime_code,
14276         ail.tax,
14277         ail.tax_jurisdiction_code,
14278         ail.tax_status_code,
14279         ail.tax_rate_id,
14280         ail.tax_rate_code,
14281         ail.tax_rate,
14282 	ail.generate_dists) =
14283 	(
14284       SELECT
14285         zls.tax_regime_code||' - '||zls.tax,			-- description
14286         zls.tax_amt, 						-- amount
14287         zls.tax_amt_funcl_curr,					-- base_amount
14288         zls.cancel_flag,					-- cancelled_flag
14289         l_user_id,						-- last_updated_by
14290         l_login_id,						-- last_update_login
14291         l_sysdate, 						-- last_update_date
14292         zls.tax_regime_code,	    				-- tax_regime_code
14293         zls.tax,		    				-- tax
14294         zls.tax_jurisdiction_code,  				-- tax_jurisdiction_code
14295         zls.tax_status_code,	    				-- tax_status_code
14296         zls.tax_rate_id,	    				-- tax_rate_id
14297         zls.tax_rate_code,	    				-- tax_rate_code
14298         zls.tax_rate, 		    				-- tax_rate
14299 	DECODE(ail.generate_dists,'D','D','Y')		        -- generate_dists  bug 5460342
14300         FROM zx_lines_summary zls
14301        WHERE zls.summary_tax_line_id		= ail.summary_tax_line_id
14302          AND nvl(zls.reporting_only_flag, 'N')	= 'N'
14303        )
14304        WHERE ail.invoice_id = P_Invoice_Header_Rec.invoice_id
14305 	 AND ail.line_type_lookup_code	= 'TAX'
14306          AND EXISTS
14307 	       	    (SELECT ls.summary_tax_line_id
14308                        FROM zx_lines_summary ls
14309                       WHERE ls.summary_tax_line_id		= ail.summary_tax_line_id
14310                         AND ls.trx_id				= ail.invoice_id
14311                         AND NVL(ls.tax_amt_included_flag, 'N')	= 'N'
14312                         AND NVL(ls.self_assessed_flag, 'N')	= 'N'
14313                         AND NVL(ls.reporting_only_flag, 'N')	= 'N');
14314 
14315     EXCEPTION
14316       WHEN NO_DATA_FOUND THEN
14317         NULL;
14318 
14319       WHEN OTHERS THEN
14320         IF (SQLCODE <> -20001) THEN
14321           FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
14322           FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
14323           FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
14324           FND_MESSAGE.SET_TOKEN('PARAMETERS',
14325             ' P_Invoice_Id = '||P_Invoice_Header_Rec.Invoice_Id||
14326             ' P_Error_Code = '||P_Error_Code||
14327             ' P_Calling_Sequence = '||P_Calling_Sequence);
14328           FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
14329         END IF;
14330 
14331       APP_EXCEPTION.RAISE_EXCEPTION;
14332     END;
14333 
14334     -------------------------------------------------------------------
14335     l_debug_info := 'Step 3: Get open gl_date';
14336     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
14337       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
14338     END IF;
14339     -------------------------------------------------------------------
14340 
14341     l_period_name := AP_UTILITIES_PKG.get_current_gl_date
14342 				(P_Invoice_Header_Rec.gl_date, P_Invoice_header_Rec.org_id);
14343 
14344     IF (l_period_name IS NULL) THEN
14345 	AP_UTILITIES_PKG.get_open_gl_date(
14346 	       P_Date                  => P_Invoice_Header_Rec.gl_date,
14347 	       P_Period_Name           => l_period_name,
14348 	       P_GL_Date               => l_gl_date,
14349 	       P_Org_Id                => P_Invoice_Header_Rec.org_id);
14350     ELSE
14351 	l_gl_date := P_Invoice_Header_Rec.gl_date;
14352     END IF;
14353 
14354     -------------------------------------------------------------------
14355     l_debug_info := 'Step 4: Determine if tax amount will be included in awt';
14356     -------------------------------------------------------------------
14357     IF NVL(l_wfapproval_flag,'N') = 'Y' THEN
14358       l_wfapproval_status := 'REQUIRED';
14359     ELSE
14360       l_wfapproval_status := 'NOT REQUIRED';
14361     END IF;
14362 
14363     -------------------------------------------------------------------
14364     l_debug_info := 'Step 5: Insert exclusive tax lines';
14365     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
14366       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
14367     END IF;
14368     -------------------------------------------------------------------
14369     BEGIN
14370       INSERT INTO ap_invoice_lines_all (
14371         invoice_id,
14372         line_number,
14373         line_type_lookup_code,
14374         requester_id,
14375         description,
14376         line_source,
14377         org_id,
14378         line_group_number,
14379         inventory_item_id,
14380         item_description,
14381         serial_number,
14382         manufacturer,
14383         model_number,
14384         warranty_number,
14385         generate_dists,
14386         match_type,
14387         distribution_set_id,
14388         account_segment,
14389         balancing_segment,
14390         cost_center_segment,
14391         overlay_dist_code_concat,
14392         default_dist_ccid,
14393         prorate_across_all_items,
14394         accounting_date,
14395         period_name,
14396         deferred_acctg_flag,
14397         def_acctg_start_date,
14398         def_acctg_end_date,
14399         def_acctg_number_of_periods,
14400         def_acctg_period_type,
14401         set_of_books_id,
14402         amount,
14403         base_amount,
14404         rounding_amt,
14405         quantity_invoiced,
14406         unit_meas_lookup_code,
14407         unit_price,
14408         wfapproval_status,
14409         discarded_flag,
14410         original_amount,
14411         original_base_amount,
14412         original_rounding_amt,
14413         cancelled_flag,
14414         income_tax_region,
14415         type_1099,
14416         stat_amount,
14417         prepay_invoice_id,
14418         prepay_line_number,
14419         invoice_includes_prepay_flag,
14420         corrected_inv_id,
14421         corrected_line_number,
14422         po_header_id,
14423         po_line_id,
14424         po_release_id,
14425         po_line_location_id,
14426         po_distribution_id,
14427         rcv_transaction_id,
14428         final_match_flag,
14429         assets_tracking_flag,
14430         asset_book_type_code,
14431         asset_category_id,
14432         project_id,
14433         task_id,
14434         expenditure_type,
14435         expenditure_item_date,
14436         expenditure_organization_id,
14437         pa_quantity,
14438         pa_cc_ar_invoice_id,
14439         pa_cc_ar_invoice_line_num,
14440         pa_cc_processed_code,
14441         award_id,
14442         awt_group_id,
14443         reference_1,
14444         reference_2,
14445         receipt_verified_flag,
14446         receipt_required_flag,
14447         receipt_missing_flag,
14448         justification,
14449         expense_group,
14450         start_expense_date,
14451         end_expense_date,
14452         receipt_currency_code,
14453         receipt_conversion_rate,
14454         receipt_currency_amount,
14455         daily_amount,
14456         web_parameter_id,
14457         adjustment_reason,
14458         merchant_document_number,
14459         merchant_name,
14460         merchant_reference,
14461         merchant_tax_reg_number,
14462         merchant_taxpayer_id,
14463         country_of_supply,
14464         credit_card_trx_id,
14465         company_prepaid_invoice_id,
14466         cc_reversal_flag,
14467         creation_date,
14468         created_by,
14469         last_updated_by,
14470         last_update_date,
14471         last_update_login,
14472         program_application_id,
14473         program_id,
14474         program_update_date,
14475         request_id,
14476         attribute_category,
14477         attribute1,
14478         attribute2,
14479         attribute3,
14480         attribute4,
14481         attribute5,
14482         attribute6,
14483         attribute7,
14484         attribute8,
14485         attribute9,
14486         attribute10,
14487         attribute11,
14488         attribute12,
14489         attribute13,
14490         attribute14,
14491         attribute15,
14492         global_attribute_category,
14493         global_attribute1,
14494         global_attribute2,
14495         global_attribute3,
14496         global_attribute4,
14497         global_attribute5,
14498         global_attribute6,
14499         global_attribute7,
14500         global_attribute8,
14501         global_attribute9,
14502         global_attribute10,
14503         global_attribute11,
14504         global_attribute12,
14505         global_attribute13,
14506         global_attribute14,
14507         global_attribute15,
14508         global_attribute16,
14509         global_attribute17,
14510         global_attribute18,
14511         global_attribute19,
14512         global_attribute20,
14513         control_amount,
14514         assessable_value,
14515         total_rec_tax_amount,
14516         total_nrec_tax_amount,
14517         total_rec_tax_amt_funcl_curr,
14518         total_nrec_tax_amt_funcl_curr,
14519         included_tax_amount,
14520         primary_intended_use,
14521         ship_to_location_id,
14522         product_type,
14523         product_category,
14524         product_fisc_classification,
14525         user_defined_fisc_class,
14526         trx_business_category,
14527         summary_tax_line_id,
14528         tax_regime_code,
14529         tax,
14530         tax_jurisdiction_code,
14531         tax_status_code,
14532         tax_rate_id,
14533         tax_rate_code,
14534         tax_rate,
14535         tax_code_id)
14536       SELECT
14537         P_Invoice_Header_Rec.Invoice_Id,				-- invoice_id
14538         (SELECT NVL(MAX(ail2.line_number),0)
14539            FROM ap_invoice_lines_all ail2
14540           WHERE ail2.invoice_id =  zls.trx_id) + ROWNUM,  		-- line_number
14541         'TAX',								-- line_type_lookup_code
14542         null,								-- requester_id
14543         zls.tax_regime_code||' - '||zls.tax,				-- description
14544         'ETAX',								-- line_source
14545         P_Invoice_Header_Rec.org_id,   					-- org_id
14546         null,   							-- line_group_number
14547         null,   							-- inventory_item_id
14548         null,   							-- item_description
14549         null,   							-- serial_number
14550         null,   							-- manufacturer
14551         null,   							-- model_number
14552         null,   							-- warranty_number
14553         DECODE(NVL(zls.tax_only_line_flag, 'N'),
14554                'Y', 'D',
14555                'Y'),   							-- generate_dists
14556         DECODE(zls.applied_to_trx_id,
14557                null, 'NOT_MATCHED',
14558                'OTHER_TO_RECEIPT'),   					-- match_type
14559         null,   							-- distribution_set_id
14560         null,   							-- account_segment
14561         null,   							-- balancing_segment
14562         null,   							-- cost_center_segment
14563         null,   							-- overlay_dist_code_concat
14564         null,   							-- default_dist_ccid
14565         'N',   								-- prorate_across_all_items
14566         l_gl_date,   							-- accounting_date
14567         DECODE(NVL(zls.tax_only_line_flag, 'N'),
14568                'N', DECODE(zls.applied_to_trx_id,
14569                            null, null, l_period_name),
14570                 l_period_name),   					-- period_name
14571         'N',   								-- deferred_acctg_flag
14572         null,   							-- def_acctg_start_date
14573         null,   							-- def_acctg_end_date
14574         null,   							-- def_acctg_number_of_periods
14575         null,   							-- def_acctg_period_type
14576         P_Invoice_Header_Rec.set_of_books_id,   			-- set_of_books_id
14577         zls.tax_amt,   							-- amount
14578         DECODE(P_Invoice_Header_Rec.invoice_currency_code,
14579                l_base_currency_code, NULL,
14580                zls.tax_amt_funcl_curr),    				-- base_amount
14581         null,   							-- rounding_amt
14582         null,   							-- quantity_invoiced
14583         null,   							-- unit_meas_lookup_code
14584         null,   							-- unit_price
14585         l_wfapproval_status,   						-- wfapproval_status
14586         'N',   								-- discarded_flag
14587         null,   							-- original_amount
14588         null,   							-- original_base_amount
14589         null,   							-- original_rounding_amt
14590         'N',   								-- cancelled_flag
14591         DECODE(ap.type_1099,
14592                '','',
14593                DECODE(l_combined_filing_flag,
14594                       'N', '',
14595                       DECODE(l_income_tax_region_flag,
14596                              'Y', aps.state,
14597                              l_income_tax_region))),  			-- income_tax_region
14598         ap.type_1099,   						-- type_1099
14599         null,   							-- stat_amount
14600         zls.applied_from_trx_id,   					-- prepay_invoice_id
14601         zls.applied_from_line_id,   					-- prepay_line_number
14602         prepay.invoice_includes_prepay_flag,   				-- invoice_includes_prepay_flag
14603         zls.adjusted_doc_trx_id,   					-- corrected_inv_id
14604         -- zls.adjusted_doc_line_id,   					-- corrected_line_number
14605         null,								-- corrected_line_number
14606         null,   							-- po_header_id
14607         null,   							-- po_line_id
14608         null,   							-- po_release_id
14609         null,   							-- po_line_location_id
14610         null,   							-- po_distribution_id
14611         zls.applied_to_trx_id,						-- rcv_transaction_id
14612         'N',   								-- final_match_flag
14613         null,   							-- assets_tracking_flag
14614         null,   							-- asset_book_type_code
14615         null,   							-- asset_category_id
14616         null,   							-- project_id
14617         null,   							-- task_id
14618         null,   							-- expenditure_type
14619         null,   							-- expenditure_item_date
14620         null,   							-- expenditure_organization_id
14621         null,   							-- pa_quantity
14622         null,   							-- pa_cc_ar_invoice_id
14623         null,   							-- pa_cc_ar_invoice_line_num
14624         null,   							-- pa_cc_processed_code
14625         null,   							-- award_id
14626         DECODE(l_awt_include_tax_amt,
14627                'N', null,
14628                DECODE(zls.applied_from_trx_id,
14629                       null, P_Invoice_Header_Rec.awt_group_id,
14630                       prepay.awt_group_id)),   				-- awt_group_id
14631         null,   							-- reference_1
14632         null,   							-- reference_2
14633         null,   							-- receipt_verified_flag
14634         null,   							-- receipt_required_flag
14635         null,   							-- receipt_missing_flag
14636         null,   							-- justification
14637         null,   							-- expense_group
14638         null,   							-- start_expense_date
14639         null,   							-- end_expense_date
14640         null,   							-- receipt_currency_code
14641         null,   							-- receipt_conversion_rate
14642         null,   							-- receipt_currency_amount
14643         null,   							-- daily_amount
14644         null,   							-- web_parameter_id
14645         null,   							-- adjustment_reason
14646         null,   							-- merchant_document_number
14647         null,   							-- merchant_name
14648         null,   							-- merchant_reference
14649         null,   							-- merchant_tax_reg_number
14650         null,								-- merchant_taxpayer_id
14651         null,								-- country_of_supply
14652         null,								-- credit_card_trx_id
14653         null,								-- company_prepaid_invoice_id
14654         null,								-- cc_reversal_flag
14655         l_sysdate,							-- creation_date
14656         l_user_id,   							-- created_by
14657         l_user_id,   							-- last_updated_by
14658         l_sysdate,   							-- last_update_date
14659         l_login_id,   							-- last_update_login
14660         null,   							-- program_application_id
14661         null,   							-- program_id
14662         null,   							-- program_update_date
14663         null,   							-- request_id
14664         zls.attribute_category,   					-- attribute_category
14665         zls.attribute1,   						-- attribute1
14666         zls.attribute2,   						-- attribute2
14667         zls.attribute3,   						-- attribute3
14668         zls.attribute4,   						-- attribute4
14669         zls.attribute5,   						-- attribute5
14670         zls.attribute6,   						-- attribute6
14671         zls.attribute7,   						-- attribute7
14672         zls.attribute8,   						-- attribute8
14673         zls.attribute9,   						-- attribute9
14674         zls.attribute10,   						-- attribute10
14675         zls.attribute11,   						-- attribute11
14676         zls.attribute12,   						-- attribute12
14677         zls.attribute13,   						-- attribute13
14678         zls.attribute14,   						-- attribute14
14679         zls.attribute15,   						-- attribute15
14680         zls.global_attribute_category,   				-- global_attribute_category
14681         zls.global_attribute1,   					-- global_attribute1
14682         zls.global_attribute2,   					-- global_attribute2
14683         zls.global_attribute3,   					-- global_attribute3
14684         zls.global_attribute4,   					-- global_attribute4
14685         zls.global_attribute5,   					-- global_attribute5
14686         zls.global_attribute6,   					-- global_attribute6
14687         zls.global_attribute7,   					-- global_attribute7
14688         zls.global_attribute8,   					-- global_attribute8
14689         zls.global_attribute9,   					-- global_attribute9
14690         zls.global_attribute10,   					-- global_attribute10
14691         zls.global_attribute11,   					-- global_attribute11
14692         zls.global_attribute12,   					-- global_attribute12
14693         zls.global_attribute13,   					-- global_attribute13
14694         zls.global_attribute14,   					-- global_attribute14
14695         zls.global_attribute15,   					-- global_attribute15
14696         zls.global_attribute16,   					-- global_attribute16
14697         zls.global_attribute17,   					-- global_attribute17
14698         zls.global_attribute18,   					-- global_attribute18
14699         zls.global_attribute19,   					-- global_attribute19
14700         zls.global_attribute20,   					-- global_attribute20
14701         null,   							-- control_amount
14702         null,   							-- assessable_value
14703         null,   							-- total_rec_tax_amount
14704         null,   							-- total_nrec_tax_amount
14705         null,   							-- total_rec_tax_amt_funcl_curr
14706         null,   							-- total_nrec_tax_amt_funcl_curr
14707         null,   							-- included_tax_amount
14708         null,   							-- primary_intended_use
14709         null,   							-- ship_to_location_id
14710         null,   							-- product_type
14711         null,   							-- product_category
14712         null,   							-- product_fisc_classification
14713         null,   							-- user_defined_fisc_class
14714         null,   							-- trx_business_category
14715         zls.summary_tax_line_id,   					-- summary_tax_line_id
14716         zls.tax_regime_code,   						-- tax_regime_code
14717         zls.tax,   							-- tax
14718         zls.tax_jurisdiction_code,   					-- tax_jurisdiction_code
14719         zls.tax_status_code,   						-- tax_status_code
14720         zls.tax_rate_id,   						-- tax_rate_id
14721         zls.tax_rate_code,   						-- tax_rate_code
14722         zls.tax_rate,   						-- tax_rate
14723         null   								-- tax_code_id
14724      FROM ap_invoices_all       ai,
14725           ap_suppliers          ap,
14726           ap_supplier_sites_all aps,
14727           zx_lines_summary      zls,
14728           ap_invoice_lines_all  prepay
14729     WHERE ai.invoice_id				= p_invoice_header_rec.invoice_id
14730       AND ai.vendor_id                          = ap.vendor_id
14731       AND ai.vendor_site_id                     = aps.vendor_site_id
14732       AND zls.application_id 			= 200
14733       AND zls.entity_code 			= 'AP_INVOICES'
14734       AND zls.event_class_code			IN ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
14735       AND zls.trx_id 				= ai.invoice_id
14736       AND NVL(zls.tax_amt_included_flag, 'N') 	= 'N'
14737       AND NVL(zls.self_assessed_flag, 'N') 	= 'N'
14738       AND NVL(zls.reporting_only_flag, 'N') 	= 'N'
14739       AND zls.applied_from_trx_id  		= prepay.invoice_id(+)
14740       AND zls.applied_from_line_id 		= prepay.line_number(+)
14741       AND NOT EXISTS (SELECT il.summary_tax_line_id
14742                         FROM ap_invoice_lines_all il
14743                        WHERE il.invoice_id = ai.invoice_id
14744                          AND il.summary_tax_line_id = zls.summary_tax_line_id);
14745 
14746     EXCEPTION
14747       WHEN NO_DATA_FOUND THEN
14748         NULL;
14749 
14750       WHEN OTHERS THEN
14751         IF (SQLCODE <> -20001) THEN
14752           FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
14753           FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
14754           FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
14755           FND_MESSAGE.SET_TOKEN('PARAMETERS',
14756             ' P_Invoice_Id = '||P_Invoice_Header_Rec.invoice_id||
14757             ' P_Error_Code = '||P_Error_Code||
14758             ' P_Calling_Sequence = '||P_Calling_Sequence);
14759           FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
14760         END IF;
14761 
14762       APP_EXCEPTION.RAISE_EXCEPTION;
14763     END;
14764 
14765     -------------------------------------------------------------------
14766     l_debug_info := 'Step 8: Delete exclusive tax lines if required';
14767     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
14768       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
14769     END IF;
14770     -------------------------------------------------------------------
14771     BEGIN
14772       DELETE ap_invoice_lines_all ail
14773        WHERE ail.invoice_id = P_Invoice_Header_Rec.invoice_id
14774          AND ail.line_type_lookup_code = 'TAX'
14775          AND NOT EXISTS (SELECT ls.summary_tax_line_id
14776                            FROM zx_lines_summary ls
14777                           WHERE ls.summary_tax_line_id	= ail.summary_tax_line_id
14778                             AND ls.trx_id 		= ail.invoice_id
14779                             AND NVL(ls.tax_amt_included_flag, 'N') = 'N'
14780                             AND NVL(ls.self_assessed_flag,    'N') = 'N'
14781                             AND NVL(ls.reporting_only_flag,   'N') = 'N');
14782 
14783     EXCEPTION
14784       WHEN NO_DATA_FOUND THEN
14785         NULL;
14786 
14787       WHEN OTHERS THEN
14788         IF (SQLCODE <> -20001) THEN
14789           FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
14790           FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
14791           FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
14792           FND_MESSAGE.SET_TOKEN('PARAMETERS',
14793             ' P_Invoice_Id = '||P_Invoice_Header_Rec.invoice_id||
14794             ' P_Error_Code = '||P_Error_Code||
14795             ' P_Calling_Sequence = '||P_Calling_Sequence);
14796           FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
14797         END IF;
14798 
14799       APP_EXCEPTION.RAISE_EXCEPTION;
14800     END;
14801 
14802     -------------------------------------------------------------------
14803     l_debug_info := 'Step 10: Update total_tax_amount and self_assessed tax';
14804     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
14805       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
14806     END IF;
14807     -------------------------------------------------------------------
14808     BEGIN
14809       UPDATE ap_invoices_all ai
14810          SET (ai.total_tax_amount,
14811               ai.self_assessed_tax_amount) =
14812              (SELECT SUM(DECODE(NVL(zls.self_assessed_flag, 'N'),
14813                                 'N', NVL(zls.tax_amt, 0),
14814                                 0)),
14815                      SUM(DECODE(NVL(zls.self_assessed_flag, 'N'),
14816                                 'Y', NVL(zls.tax_amt, 0),
14817                                  0))
14818                 FROM zx_lines_summary zls
14819 	       WHERE zls.application_id   = 200
14820 		 AND zls.entity_code      = 'AP_INVOICES'
14821 		 AND zls.event_class_code IN ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
14822                  AND zls.trx_id 	= ai.invoice_id
14823                  AND NVL(zls.reporting_only_flag, 'N') = 'N')
14824        WHERE ai.invoice_id = P_Invoice_Header_Rec.invoice_id
14825        RETURNING ai.total_tax_amount, ai.self_assessed_tax_amount
14826             INTO l_total_tax_amount, l_self_assessed_tax_amt;
14827 
14828     EXCEPTION
14829       WHEN NO_DATA_FOUND THEN
14830         NULL;
14831 
14832       WHEN OTHERS THEN
14833         IF (SQLCODE <> -20001) THEN
14834           FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
14835           FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
14836           FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
14837           FND_MESSAGE.SET_TOKEN('PARAMETERS',
14838             ' P_Invoice_Id = '||P_Invoice_Header_Rec.invoice_id||
14839             ' P_Error_Code = '||P_Error_Code||
14840             ' P_Calling_Sequence = '||P_Calling_Sequence);
14841           FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
14842         END IF;
14843 
14844       APP_EXCEPTION.RAISE_EXCEPTION;
14845     END;
14846 
14847     -----------------------------------------------------------------
14848     l_debug_info := 'Step 12: Update Invoice Includes Prepay Flag';
14849     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
14850       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
14851     END IF;
14852     -----------------------------------------------------------------
14853 
14854     UPDATE ap_invoice_lines_all tax
14855        SET tax.invoice_includes_prepay_flag = 'Y'
14856      WHERE tax.invoice_id		=  P_Invoice_Header_Rec.Invoice_Id
14857        AND tax.line_type_lookup_code	= 'TAX'
14858        AND EXISTS
14859            (SELECT 'Prepay App Exists'
14860             FROM   ap_invoice_lines_all prepay
14861             WHERE  prepay.invoice_id 		= tax.invoice_id
14862             AND    prepay.line_type_lookup_code = 'PREPAY'
14863             AND    prepay.prepay_invoice_id 	= tax.prepay_invoice_id
14864             AND    prepay.prepay_line_number 	= tax.prepay_line_number
14865             AND    prepay.invoice_includes_prepay_flag = 'Y');
14866 
14867     RETURN TRUE;
14868 
14869   EXCEPTION
14870     WHEN OTHERS THEN
14871       IF (SQLCODE <> -20001) THEN
14872         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
14873         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
14874         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
14875         FND_MESSAGE.SET_TOKEN('PARAMETERS',
14876           			' P_Invoice_Id = '      ||P_Invoice_Header_Rec.Invoice_Id||
14877           			' P_Error_Code = '      ||P_Error_Code||
14878           			' P_Calling_Sequence = '||P_Calling_Sequence);
14879         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
14880       END IF;
14881 
14882       APP_EXCEPTION.RAISE_EXCEPTION;
14883 
14884   END Return_Other_Charge_Lines;
14885 
14886 
14887 PROCEDURE Print
14888              (p_api_name   IN VARCHAR2,
14889               p_debug_info IN VARCHAR2) IS
14890 BEGIN
14891   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
14892       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||p_api_name,p_debug_info);
14893   END IF;
14894 END Print;
14895 
14896 END AP_ETAX_SERVICES_PKG;