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.223.12020000.16 2013/04/16 15:08:26 nbshaik 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
46 
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);
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             p_inventory_item_id     IN NUMBER DEFAULT NULL, /*Bug 13079028*/
114 	    p_org_id                IN NUMBER DEFAULT NULL, /*Bug 13079028*/
115 	    p_application_id        IN NUMBER DEFAULT NULL, /*Bug 13079028*/
116 	    p_reference_key         IN VARCHAR2 DEFAULT NULL) RETURN NUMBER IS /*Bug 13079028*/
117     --Bug9777752
118     l_debug_info                 VARCHAR2(240);
119     l_api_name			  CONSTANT VARCHAR2(100) := 'Get_po_ship_to_org_id';
120     --Bug9777752
121     l_po_ship_to_org_id NUMBER := NULL;
122     l_application_short_name    fnd_application.application_short_name%type := NULL; /*Bug 13079028*/
123 BEGIN
124 
125     l_debug_info := 'Step 1: Check p_po_line_location_id '||p_po_line_location_id;
126     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
127         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
128     END IF;
129 
130     /*Bug 13079028: Need to pass inventory org id for OKL interfaced invoices*/
131 
132       IF p_application_id is not null then
133 
134          BEGIN
135 
136 	     Select fa.application_short_name
137 		into l_application_short_name
138 	       from fnd_application fa,
139 		    fnd_product_installations fpi
140 	      Where fa.application_id = fpi.application_id
141 		and fa.application_id = p_application_id
142 		and nvl(fpi.status,'U') <> 'N'
143 		and rownum = 1;
144 
145 	   l_debug_info := 'Step 2: Check l_application_short_name '||l_application_short_name;
146            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
147             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
148            END IF;
149 
150 	 EXCEPTION
151 	  WHEN NO_DATA_FOUND THEN
152 	    l_application_short_name := '';
153 	  WHEN OTHERS THEN
154 	    l_application_short_name := '';
155 	 END;
156       END IF;
157     /*Bug 13079028 : End*/
158 
159     IF (p_po_line_location_id is NULL) THEN /*Bug 13079028: Changed "=" to "IS NULL" */
160 
161       /*Bug 13079028: Added below conditions to fetch inventory org id in case inventory item id is populated or interfacing product is OKL*/
162       IF (p_inventory_item_id is not null
163          AND l_application_short_name = 'OKL') then
164 
165          l_po_ship_to_org_id := p_reference_key;
166 
167       ELSIF (p_inventory_item_id is not null
168        AND p_org_id is not null) then
169 
170          l_po_ship_to_org_id := AP_ETAX_SERVICES_PKG.g_fsp_attributes(p_org_id).inventory_organization_id;
171 
172       ELSIF p_org_id is not null then
173 
174          l_po_ship_to_org_id := p_org_id;
175 
176       ELSE
177          l_po_ship_to_org_id := '';
178       END IF;
179 
183        END IF;
180        l_debug_info := 'Step 2.1: Check SHIP_TO_ORG_ID '||l_po_ship_to_org_id;
181        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
182         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
184 
185     ELSE /*Bug 13079028*/
186 
187      SELECT SHIP_TO_ORGANIZATION_ID
188       INTO l_po_ship_to_org_id
189      FROM po_line_locations_all pll
190      WHERE pll.line_location_id = p_po_line_location_Id;
191 
192 
193     l_debug_info := 'Step 2.2: Check SHIP_TO_ORG_ID '||l_po_ship_to_org_id;
194     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
195         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
196     END IF;
197 
198     END IF; /*Bug 13079028*/
199 
200     return l_po_ship_to_org_id;
201 
202 EXCEPTION
203     WHEN OTHERS THEN
204         RETURN NULL;
205 END get_po_ship_to_org_id;
206 -- 7262269
207 
208 /*=============================================================================
209  |  FUNCTION - Calculate()
210  |
211  |  DESCRIPTION
212  |      Public function that will call the calculate_tax service for
213  |      calculation and recalculation.
214  |      This API assumes the calling code controls the commit cycle.
215  |      This function returns TRUE if the call to the service is successful.
216  |      Otherwise, FALSE.
217  |
218  |  PARAMETERS
219  |      P_Invoice_Id - invoice id
220  |      P_Line_Number - This parameter will be used to allow this API to
221  |                      calculate tax only for the line specified in this
222  |                      parameter.  Additionally, this parameter will be used
223  |                      to determine the PREPAY line created for prepayment
224  |                      unapplications.
225  |      P_Calling_Mode - calling mode.  Identifies which service to call
226  |      P_All_Error_Messages - Should API return 1 error message or allow
227  |                             calling point to get them from message stack
228  |      P_error_code - Error code to be returned
229  |      P_calling_sequence -  Calling sequence
230  |
231  |  MODIFICATION HISTORY
232  |    DATE          Author         Action
233  |    07-OCT-2003   SYIDNER        Created
234  |    29-DEC-2003   SYIDNER        Use of new function to validate if tax was
235  |                                 already calculated for the invoice.  Function
236  |                                 created in the ap_etax_utility_pkg.
237  |    28-JAN-2004   SYIDNER        Included handling for tax-only invoices and
238  |                                 Manual import from Invoice workbench
239  |
240  *============================================================================*/
241 
242   FUNCTION Calculate(
243              P_Invoice_Id              IN NUMBER,
244              P_Line_Number             IN NUMBER,
245              P_Calling_Mode            IN VARCHAR2,
246              P_All_Error_Messages      IN VARCHAR2,
247              P_Error_Code              OUT NOCOPY VARCHAR2,
248              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
249   IS
250 
251     l_debug_info                 VARCHAR2(240);
252     l_curr_calling_sequence      VARCHAR2(4000);
253 
254     l_inv_header_rec             ap_invoices_all%ROWTYPE;
255     l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
256     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
257 
258     l_tax_already_calculated     VARCHAR2(1);
259 
260     l_return_status_service       VARCHAR2(4000);
261     l_msg_count                   NUMBER;
262     l_msg_data                    VARCHAR2(4000);
263     l_msg                         VARCHAR2(4000);
264 
265     l_return_status               BOOLEAN := TRUE;
266     l_call_etax                   BOOLEAN :=FALSE; --Bug7136832
267     /*l_no_tax_lines              VARCHAR2(1) := 'N';*/ --Bug6521120
268     l_no_tax_lines                VARCHAR2(1) := 'Y';  --Bug6521120
269     l_inv_rcv_matched             VARCHAR2(1) := 'N';
270 
271     l_api_name			  CONSTANT VARCHAR2(100) := 'Calculate';
272 
273 
274     CURSOR Invoice_Header (c_invoice_id NUMBER) IS
275     SELECT *
276       FROM ap_invoices_all
277      WHERE invoice_id = c_invoice_Id;
278 
279     CURSOR Invoice_Lines (c_invoice_id NUMBER) IS
280     SELECT *
281       FROM ap_invoice_lines_all
282      WHERE invoice_id = c_invoice_id
283        AND (line_type_lookup_code NOT IN ('TAX', 'AWT','RETROTAX','RETROITEM') OR
284 	   (line_type_lookup_code = 'RETROITEM' AND match_type = 'PO_PRICE_ADJUSTMENT'))  --Bug10416960
285        AND nvl(discarded_flag, 'N')  <> 'Y'  --Bug8811102
286        AND nvl(cancelled_flag, 'N') <> 'Y';  --Bug8811102;
287 
288     -- This cursor will be used in the case the API is call
289     -- to calculate tax for only 1 line
290     CURSOR Invoice_Line (c_invoice_id  NUMBER,
291 			 c_line_number NUMBER) IS
292     SELECT *
293       FROM ap_invoice_lines_all
294      WHERE invoice_id  = c_invoice_id
295        AND line_number = c_line_number
296        AND (line_type_lookup_code NOT IN ('TAX', 'AWT','RETROTAX','RETROITEM') OR
297 	       (line_type_lookup_code = 'RETROITEM' AND match_type = 'PO_PRICE_ADJUSTMENT'))  --Bug10416960
298        AND nvl(discarded_flag, 'N')  <> 'Y'  --Bug8811102
299        AND nvl(cancelled_flag, 'N') <> 'Y';  --Bug8811102
300 
301     CURSOR Tax_Lines_to_import (c_invoice_id IN NUMBER) IS
302     SELECT *
303       FROM ap_invoice_lines_all
304      WHERE invoice_id            = c_invoice_id
305        AND line_type_lookup_code = 'TAX'
306        AND summary_tax_line_id   IS NULL
307        AND nvl(discarded_flag, 'N')  <> 'Y'  --Bug8811102
308        AND nvl(cancelled_flag, 'N') <> 'Y';  --Bug8811102
312     CURSOR c_selected_invoices IS
309 
310     l_validation_request_id ap_invoices_all.validation_request_id%TYPE;
311 
313     SELECT trx_id, event_class_code
314       FROM zx_trx_headers_gt
315      WHERE application_id   =  ap_etax_pkg.ap_application_id
316        AND entity_code      =  ap_etax_pkg.ap_entity_code
317        AND event_class_code IN (ap_etax_pkg.ap_inv_event_class_code,
318                                 ap_etax_pkg.ap_pp_event_class_code,
319                                 ap_etax_pkg.ap_er_event_class_code);
320 
321     --6922266
322     l_count   NUMBER;
323 
324   BEGIN
325 
326     --Print(l_api_name,'AP_ETAX_SERVICES_PKG.Calculate (+)');
327     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
328         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'AP_ETAX_SERVICES_PKG.Calculate (+)');
329     END IF;
330 
331     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Calculate<-' || P_calling_sequence;
332 
333     l_validation_request_id := ap_approval_pkg.g_validation_request_id;
334 
335     g_invoices_to_process := 0;
336 
337 
338     --Bug9436217
339     IF P_Invoice_Id IS NULL THEN
340     --Bug9436217
341 
342        IF NOT AP_ETAX_SERVICES_PKG.Bulk_Populate_Headers_GT(
343 		p_validation_request_id => l_validation_request_id,
344 		p_calling_mode		=> P_Calling_Mode,
345 		p_error_code            => p_error_code) THEN
346 
347 	  l_return_status := FALSE;
348        END IF;
349 
350        -- Bug 6922266 Begin
351        -----------------------------------------------------------------
352        l_debug_info := 'Reset Tax Calculation Flag for Concurrent Mode';
353        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
354           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
355        END IF;
356        --Print(l_api_name, l_debug_info);
357        -----------------------------------------------------------------
358 
359        l_count := 0;
360        /* Added the following 3 hints for bug#8263883 */
361        UPDATE /*+ index(ail,AP_INVOICE_LINES_U1) */ ap_invoice_lines_all ail
362        SET    tax_already_calculated_flag = NULL
363        WHERE  ail.invoice_id IN (SELECT /*+ cardinality(gt 10) unnest */ DISTINCT(trx_id)
364                                  FROM   zx_trx_headers_gt gt
365                                  WHERE  application_id = 200
366                                  AND    entity_code = 'AP_INVOICES')
367        AND    NVL(ail.tax_already_calculated_flag, 'N') = 'Y'
368 	   AND    NVL(ail.historical_flag, 'N') = 'N' --bug14671024
369        AND    NOT EXISTS
370                 (SELECT /*+ no_unnest index(zf,ZX_LINES_DET_FACTORS_U1) */ 'Line Determining Factors Exist'
371                    FROM zx_lines_det_factors zf
372                   WHERE zf.application_id   = 200
373                     AND zf.entity_code        = 'AP_INVOICES'
374                     AND zf.event_class_code IN ('STANDARD INVOICES',
375                                                 'PREPAYMENT INVOICES',
376                                                 'EXPENSE REPORTS')
377                     -- bug 7233679
378                     AND ZF.TRX_LEVEL_TYPE = 'LINE'
379                     AND ZF.INTERNAL_ORGANIZATION_ID = AIL.ORG_ID
380                     AND ZF.lEDGER_ID=AIL.SET_OF_BOOKS_ID
381                     -- bug 7233679
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        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
389           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
390        END IF;
391        --Print(l_api_name, l_debug_info);
392        -----------------------------------------------------------------
393        --Bug 6922266 End
394 
395     ELSE
396 
397        DELETE FROM ZX_TRX_HEADERS_GT;
398 
399        -----------------------------------------------------------------
400        l_debug_info := 'Populating invoice header local record';
401        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
402           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
403        END IF;
404        --Print(l_api_name,l_debug_info);
405        -----------------------------------------------------------------
406        OPEN  Invoice_Header(p_invoice_id);
407        FETCH Invoice_Header INTO l_inv_header_rec;
408        CLOSE Invoice_Header;
409 
410        -- Bug 9034372.
411        -- In the case of quick credit invoice, Calculate tax should be invoked
412        -- only during cancel of quick creditted invoice and not during validation
413        -- or any other tax action.
414        -- Hence control would return if the invoice is quick credit invoice and
415        -- calling mode is not QUICK CANCEL. Added the calling mode check.
416 
417        IF ((l_inv_header_rec.quick_credit = 'Y' AND P_Calling_Mode NOT IN ('QUICK CANCEL')) OR    -- Bug 5638822
418            (l_inv_header_rec.invoice_type_lookup_code IN ('AWT', 'INTEREST'))) THEN
419           RETURN l_return_status;
420        END IF;
421 
422        -------------------------------------------------------------------
423        l_debug_info := 'Is tax already called invoice level';
424        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
425           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
426        END IF;
427        --Print(l_api_name,l_debug_info);
428        -------------------------------------------------------------------
432 
429        IF (AP_ETAX_UTILITY_PKG.Is_Tax_Already_Calc_Inv(
430 	          P_Invoice_Id           => p_invoice_id,
431 	          P_Calling_Sequence     => l_curr_calling_sequence)) THEN
433            l_tax_already_calculated := 'Y';
434        ELSE
435            l_tax_already_calculated := 'N';
436        END IF;
437 
438        -----------------------------------------------------------------
439        l_debug_info := 'Populate Header';
440        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
441           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
442        END IF;
443        --Print(l_api_name,l_debug_info);
444        -----------------------------------------------------------------
445        IF NOT(AP_ETAX_SERVICES_PKG.Populate_Headers_GT(
446 	      P_Invoice_Header_Rec         => l_inv_header_rec,
447 	      P_Calling_Mode               => P_Calling_Mode,
448 	      P_eTax_Already_called_flag   => l_tax_already_calculated,
449 	      P_Event_Class_Code           => l_event_class_code,
450 	      P_Event_Type_Code            => l_event_type_code,
451 	      P_Error_Code                 => P_error_code,
452 	      P_Calling_Sequence           => l_curr_calling_sequence )) THEN
453 
454 		--Bug15914464: Start
455         IF AP_APPROVAL_PKG.G_VALIDATION_REQUEST_ID IS NULL THEN
456           APP_EXCEPTION.RAISE_EXCEPTION;
457         ELSE
458          IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
459             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'Error in AP_ETAX_SERVICES_PKG.Populate_Headers_GT');
460          END IF;
461 		 Insert into ap_errors_gt(invoice_id , message_text) values(l_inv_header_rec.invoice_id , 'Error in AP_ETAX_SERVICES_PKG.Populate_Headers_GT');
462 		 --Bug15914464: End
463  	     l_return_status := FALSE;
464 	    END IF; --Bug15914464
465        END IF;
466 
467        ap_etax_pkg.g_inv_id_list(1) := l_inv_header_rec.invoice_id;
468 
469 
470        --Bug9436217
471 
472     -----------------------------------------------------------------
473     l_debug_info := 'Reset Tax Calculation Flag';
474     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
475         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
476     END IF;
477     --Print(l_api_name,l_debug_info);
478     -----------------------------------------------------------------
479     --
480     -- Reset invoice lines tax_already_calculated_flag to Null. This
481     -- will reset only if the following conditions are met.
482     --
483     --
484     -- 1. Invoices that have gone through tax calculation on recouped
485     --    prepay distributions during matching AND tax is calculated
486     --    for the first time on the invoice.
487     -- 2. This is required to pass document level event type is passed
488     --    to eTax as STANDARD UPDATED and line level action as CREATE.
489     -- 3. This update must happen ONLY after populate_headers_gt is
490     --    invoked.
491     --
492 
493 
494 
495 
496        l_count:=0;
497 
498        UPDATE ap_invoice_lines_all ail
499           SET tax_already_calculated_flag = NULL
500         WHERE ail.invoice_id = p_invoice_id
501           AND nvl(ail.tax_already_calculated_flag, 'N') = 'Y'
502 		  AND NVL(ail.historical_flag, 'N') = 'N' --bug14671024
503           AND NOT EXISTS
504 		      (SELECT 'Line Determining Factors Exist'
505 	             from zx_lines_det_factors zf
506  	            where zf.application_id   = 200
507   	              and zf.entity_code	    = 'AP_INVOICES'
508 	              and zf.event_class_code IN ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
509 	              and zf.trx_id           = ail.invoice_id
510   	              and zf.trx_line_id      = ail.line_number);
511 
512        l_count := SQL%ROWCOUNT;
513        -----------------------------------------------------------------
514        l_debug_info := l_count ||' rows updated in ap_invoice_lines_all.';
515        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
516           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
517        END IF;
518        --Print(l_api_name, l_debug_info);
519        -----------------------------------------------------------------
520        --Bug 6922266
521 
522        --Bug9436217
523 
524 
525     END IF;
526 
527     IF g_invoices_to_process = 0 THEN
528        RETURN TRUE;
529     END IF;
530 
531 
532     -----------------------------------------------------------------
533     l_debug_info := 'Purge staging tables';
534     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
535         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
536     END IF;
537     --Print(l_api_name,l_debug_info);
538     -----------------------------------------------------------------
539     DELETE FROM ZX_TRANSACTION_LINES_GT;
540     DELETE FROM ZX_IMPORT_TAX_LINES_GT;
541     DELETE FROM ZX_TRX_TAX_LINK_GT;
542 
543     AP_ETAX_SERVICES_PKG.G_SITE_ATTRIBUTES.DELETE;
544     AP_ETAX_SERVICES_PKG.G_ORG_ATTRIBUTES.DELETE;
545 
546 
547     OPEN C_SELECTED_INVOICES;
548     LOOP
549         FETCH C_SELECTED_INVOICES
550         BULK COLLECT INTO AP_ETAX_PKG.G_INV_ID_LIST,
551                           AP_ETAX_PKG.G_EVNT_CLS_LIST
552         LIMIT AP_ETAX_PKG.G_BATCH_LIMIT;
553 
554         --Bug9436217
555 
556         IF P_Invoice_Id IS NULL THEN
557 
558         --Bug9436217
559 
560            EXIT WHEN (C_SELECTED_INVOICES%NOTFOUND
561                       AND AP_ETAX_PKG.G_INV_ID_LIST.COUNT <= 0);
562         ELSE
563            EXIT WHEN AP_ETAX_PKG.G_INV_ID_LIST.COUNT <= 0;
564         END IF;
565 
569            --Bug9436217
566         FOR i IN AP_ETAX_PKG.G_INV_ID_LIST.FIRST.. AP_ETAX_PKG.G_INV_ID_LIST.LAST
567         LOOP
568 
570 
571            IF P_Invoice_Id IS NULL THEN
572 
573            --Bug9436217
574 
575               -----------------------------------------------------------------
576               l_debug_info := 'Collect Invoice Header Details';
577               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
578                  FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
579               END IF;
580               --Print(l_api_name,l_debug_info);
581               -----------------------------------------------------------------
582               OPEN  Invoice_Header(ap_etax_pkg.g_inv_id_list(i));
583               FETCH Invoice_Header INTO l_inv_header_rec;
584               CLOSE Invoice_Header;
585               l_inv_header_rec2:=l_inv_header_rec; ---for bug 6064593
586               -----------------------------------------------------------------
587               l_debug_info := 'Get event class code';
588               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
589                  FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
590               END IF;
591               --Print(l_api_name,l_debug_info);
592               -----------------------------------------------------------------
593               l_event_class_code := ap_etax_pkg.g_evnt_cls_list(i);
594 
595            ELSE
596 	          ap_etax_pkg.g_inv_id_list(1) := l_inv_header_rec.invoice_id;
597               l_inv_header_rec2:=l_inv_header_rec; ---for bug 6064593
598 
599            END IF;
600 
601            -- Bug 9526592 : Enhanced debug message
602            -----------------------------------------------------------------
603            l_debug_info := 'Cache Line Defaults :' ||
604 	                   ' Invoice Type Lookup Code = ' || l_inv_header_rec.invoice_type_lookup_code ||
605 			   ' ,Invoice Id = '              || l_inv_header_rec.invoice_id               ||
606 			   ' ,Org id = '                  || l_inv_header_rec.org_id                   ||
607 			   ' ,Vendor Site Id = '          || l_inv_header_rec.vendor_site_id           ||
608 			   ' ,Party Site Id = '           || l_inv_header_rec.party_site_id           ;
609            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
610               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
611            END IF;
612            --Print(l_api_name,l_debug_info);
613            --Bug9777752
614            -----------------------------------------------------------------
615                 IF l_inv_header_rec.invoice_type_lookup_code = 'PAYMENT REQUEST' THEN  /* if
616                   condition for bug 5967914 as we need tp pass party_site_id instead of
617                   vendor_site_id if invoice_type_lookup_code ='PAYMENT REQUEST' */
618 
619                   l_payment_request_flag :='Y';  --Bug13899981
620 
621                   IF (NOT AP_ETAX_SERVICES_PKG.g_site_attributes.exists(l_inv_header_rec.party_site_id) OR
622                       NOT AP_ETAX_SERVICES_PKG.g_org_attributes.exists(l_inv_header_rec.org_id))  THEN
623 
624 
625 		      -- Bug 9777752 : Added Begin - Exception - End block
626 		      BEGIN
627      	                 Cache_Line_Defaults
628 	                     (p_org_id           => l_inv_header_rec.org_id
629 	                      ,p_vendor_site_id   => l_inv_header_rec.party_site_id
630 	                      ,p_calling_sequence => l_curr_calling_sequence);
631 		      EXCEPTION
632                          WHEN NO_DATA_FOUND THEN
633                             FND_MESSAGE.SET_NAME('SQLAP','AP_INVALID_VENDOR_SITE_CODE');
634                             IF AP_APPROVAL_PKG.G_VALIDATION_REQUEST_ID IS NULL THEN
635                                 APP_EXCEPTION.RAISE_EXCEPTION;
636                             ELSE
637 
638 			        --Bug15914464: Start
639                                 IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
640                                   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'Error in AP_ETAX_SERVICES_PKG.Cache_Line_Defaults');
641                                 END IF;
642 		                       Insert into ap_errors_gt(invoice_id , message_text) values(l_inv_header_rec.invoice_id , 'Error in AP_ETAX_SERVICES_PKG.Cache_Line_Defaults');
643 				--Bug15914464: End
644 
645                                 RETURN FALSE;
646                             END IF;
647 		      END ;
648                   END IF;
649                 ELSE
650 
651                  l_payment_request_flag :='N';  --Bug13899981
652                  IF (NOT AP_ETAX_SERVICES_PKG.g_site_attributes.exists(l_inv_header_rec.vendor_site_id) OR
653                      NOT AP_ETAX_SERVICES_PKG.g_org_attributes.exists(l_inv_header_rec.org_id)) THEN
654 
655 
656 		     -- Bug 9777752 : Added Begin - Exception - End block
657 		     BEGIN
658                	        Cache_Line_Defaults
659 	                     ( p_org_id           => l_inv_header_rec.org_id
660 	                       ,p_vendor_site_id   => l_inv_header_rec.vendor_site_id
661     	                       ,p_calling_sequence => l_curr_calling_sequence);
662 		     EXCEPTION
663                         WHEN NO_DATA_FOUND THEN
664                            FND_MESSAGE.SET_NAME('SQLAP','AP_INVALID_VENDOR_SITE_CODE');
665                            IF AP_APPROVAL_PKG.G_VALIDATION_REQUEST_ID IS NULL THEN
666                                APP_EXCEPTION.RAISE_EXCEPTION;
667                            ELSE
668 				--Bug15914464: Start
669                                 IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
670                                   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'Error in AP_ETAX_SERVICES_PKG.Cache_Line_Defaults');
671                                 END IF;
675                            END IF;
672 		                       Insert into ap_errors_gt(invoice_id , message_text) values(l_inv_header_rec.invoice_id , 'Error in AP_ETAX_SERVICES_PKG.Cache_Line_Defaults');
673 			        --Bug15914464: End
674                                RETURN FALSE;
676 		     END ;
677                  END IF;
678                 END IF;
679 
680 	   -----------------------------------------------------------------
681        --Bug9777752
682            l_debug_info := 'Populate invoice tax lines collection';
683            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
684               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
685            END IF;
686            --Print(l_api_name,l_debug_info);
687            -----------------------------------------------------------------
688            BEGIN
689              OPEN  Tax_Lines_to_Import (ap_etax_pkg.g_inv_id_list(i));
690              FETCH Tax_Lines_to_Import
691              BULK  COLLECT INTO l_inv_tax_list;
692              CLOSE Tax_Lines_to_Import;
693            EXCEPTION WHEN NO_DATA_FOUND THEN NULL;
694            END;
695             /* Start added for 6014115 - If a invoice has some item lines
696                (receipt matched) and manual tax line not matched to receipt
697                 then we need to pass the applied to fields as null.
698                 So to do the same we are chking weahter we have manual
699                 tax lines or not by  g_manual_tax_lines = 'Y' and
700                 manual tax lines are receipt matched or not by
701                 l_manual_tax_line_rcv_mtch := 'N'.And then we will make
702                 the applied to columns values as NULL. also i will pass
703                 trans_lines(i).applied_to_trx_id   and
704                 trans_lines(i).applied_to_trx_id   as nullin this case.
705                 In this case we will proprate the manual tax line among
706                 all the ITEM lines.We are doing these chages as per
707                 AP_HLD_ETAX_VERSION_2_3.DOC. This is the documnet in
708                 in which we have all the guide lines for AP's Etax takeup. */
709             IF ( l_inv_tax_list.COUNT > 0) THEN
710               FOR i IN l_inv_tax_list.FIRST..l_inv_tax_list.LAST LOOP
711                 IF (l_inv_tax_list(i).rcv_transaction_id IS NOT NULL) THEN
712                    l_manual_tax_line_rcv_mtch := 'Y';
713                    EXIT;
714                 ELSE
715                    l_manual_tax_line_rcv_mtch := 'N';
716                    EXIT;
717                 END IF;
718                END LOOP;
719             ELSE
720                 l_manual_tax_line_rcv_mtch := NULL;
721 
722             END IF;
723             ---End for bug 6014115.
724            -----------------------------------------------------------------
725            l_debug_info := 'Populate invoice lines collection';
726            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
727               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
728            END IF;
729            --Print(l_api_name,l_debug_info);
730            -----------------------------------------------------------------
731            -- If there is any tax line to be imported the API will call
732            -- import document with tax for the whole invoice omiting the
733            -- p_line_number parameter
734 
735            IF (P_line_number IS NOT NULL AND
736                l_inv_tax_list.COUNT = 0 ) THEN
737                BEGIN
738                  OPEN  Invoice_Line (p_invoice_id, p_line_number);
739                  FETCH Invoice_Line
740                  BULK  COLLECT INTO l_inv_line_list;
741                  CLOSE Invoice_Line;
742                EXCEPTION WHEN NO_DATA_FOUND THEN NULL;
743                END;
744            ELSE
745                BEGIN
746                  OPEN  Invoice_Lines (ap_etax_pkg.g_inv_id_list(i));
747                  FETCH Invoice_Lines
748                  BULK  COLLECT INTO l_inv_line_list;
749                  CLOSE Invoice_Lines;
750                EXCEPTION WHEN NO_DATA_FOUND THEN NULL;
751                END;
755            --IF l_tax_already_calculated IS NULL THEN --Bug14779241
752            END IF;
753 
754 
756               -------------------------------------------------------------------
757               l_debug_info := 'Is tax already called invoice level';
758               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
759                  FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
760               END IF;
761               --Print(l_api_name,l_debug_info);
762               -------------------------------------------------------------------
763               IF (AP_ETAX_UTILITY_PKG.Is_Tax_Already_Calc_Inv(
764              	     P_Invoice_Id           => l_inv_header_rec.invoice_id,
765                      P_Calling_Sequence     => l_curr_calling_sequence)) THEN
766 	           l_tax_already_calculated := 'Y';
767               ELSE
768                    l_tax_already_calculated := 'N';
769               END IF;
770            --END IF;
771 
772 
773            IF (l_tax_already_calculated = 'Y') THEN
774                -----------------------------------------------------------------
775                l_debug_info := 'If tax already calculated call freeze distributions';
776                IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
777                  FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
778                END IF;
779                --Print(l_api_name,l_debug_info);
780                -----------------------------------------------------------------
781                --Bug7592845
782                IF NOT(AP_ETAX_SERVICES_PKG.Freeze_itm_Distributions(
783 	                P_Invoice_Header_Rec  => l_inv_header_rec,
784 	                P_Calling_Mode        => 'FREEZE DISTRIBUTIONS',
785 	                P_Event_Class_Code    => l_event_class_code,
786 	                P_All_Error_Messages  => P_All_Error_Messages,
787 	                P_Error_Code          => P_error_code,
788 	                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
789 
790 		--Bug15914464: Start
791         IF AP_APPROVAL_PKG.G_VALIDATION_REQUEST_ID IS NULL THEN
792           APP_EXCEPTION.RAISE_EXCEPTION;
793         ELSE
794          IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
795             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'Error in AP_ETAX_SERVICES_PKG.Freeze_itm_Distributions');
796          END IF;
797 		 Insert into ap_errors_gt(invoice_id , message_text) values(l_inv_header_rec.invoice_id , 'Error in AP_ETAX_SERVICES_PKG.Freeze_itm_Distributions');
798 		 --Bug15914464: End
799           l_return_status := FALSE;
800 		END IF; --Bug15914464
801 	       END IF;
802 	   END IF;
803 
804            -- This flow assumes that the UI will not call this API when the invoice
805            -- has tax lines to be imported and tax lines to matche to receipts. It
806            -- is restricted as this flow calls only 1 service at a time.
807 
808            IF (l_inv_line_list.COUNT > 0 AND l_inv_tax_list.COUNT = 0) THEN
809 
810                g_manual_tax_lines := 'N';
811                l_call_etax        := TRUE; --Bug7136832
812 
813                -----------------------------------------------------------------
814                l_debug_info := 'Populate TRX lines. No tax lines exist';
815                IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
816                   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
817                END IF;
818                --Print(l_api_name,l_debug_info);
819                -----------------------------------------------------------------
820                IF (l_return_status = TRUE)  THEN
821                    IF NOT(AP_ETAX_SERVICES_PKG.Populate_Lines_GT(
822 				P_Invoice_Header_Rec      => l_inv_header_rec,
823 				P_Calling_Mode            => P_Calling_Mode,
824 				P_Event_Class_Code        => l_event_class_code,
825 		          	P_Line_Number             => P_Line_Number,
826 		          	P_Error_Code              => P_error_code,
827 		          	P_Calling_Sequence        => l_curr_calling_sequence )) THEN
828 
829                        l_return_status := FALSE;
830 					   --Bug15914464: Start
831 					   IF P_Invoice_Id IS NULL THEN
832 					      RETURN l_return_status;
833 					   END IF;
834 					   --Bug15914464: End
835 	           END IF;
836 	       END IF;
837 
838                /*l_no_tax_lines := 'Y'; */ --Bug6521120
839 
840            ELSIF (l_inv_line_list.COUNT > 0 AND l_inv_tax_list.COUNT > 0) THEN
841 
842 		      l_no_tax_lines := 'N'; --Bug6521120
843                       g_manual_tax_lines := 'Y';
844                       l_call_etax        := TRUE; --Bug7136832
845 
846                   -----------------------------------------------------------------
847                   l_debug_info := 'Populate TRX Lines. IMPORT';
848                   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
849                       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
850                   END IF;
851                   --Print(l_api_name,l_debug_info);
852                   -----------------------------------------------------------------
853 	          IF (l_return_status = TRUE) THEN
854 	              IF NOT(AP_ETAX_SERVICES_PKG.Populate_Lines_GT(
855 			          P_Invoice_Header_Rec      => l_inv_header_rec,
856 			          P_Calling_Mode            => P_Calling_Mode,
857 			          P_Event_Class_Code        => l_event_class_code,
858 			          P_Line_Number             => P_Line_Number,
859 			          P_Error_Code              => P_error_code,
860 			          P_Calling_Sequence        => l_curr_calling_sequence )) THEN
861 
862                        l_return_status := FALSE;
863 					   --Bug15914464: Start
864 					   IF P_Invoice_Id IS NULL THEN
865 					      RETURN l_return_status;
866 					   END IF;
870 
867 					   --Bug15914464: End
868 	              END IF;
869 	          END IF;
871 	          -----------------------------------------------------------------
872 	          l_debug_info := 'Populate TAX lines to be imported';
873               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
874                   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
875               END IF;
876 	          --Print(l_api_name,l_debug_info);
877 	          -----------------------------------------------------------------
878 	          IF (l_return_status = TRUE) THEN
879 	              IF NOT(AP_ETAX_SERVICES_PKG.Populate_Tax_Lines_GT(
880 			          P_Invoice_Header_Rec      => l_inv_header_rec,
881 			          P_Calling_Mode            => P_Calling_Mode,
882 			          P_Event_Class_Code        => l_event_class_code,
883 			          P_Tax_only_Flag           => 'N',
884 			          P_Inv_Rcv_Matched         => l_Inv_Rcv_Matched,
885 			          P_Error_Code              => P_error_code,
886 			          P_Calling_Sequence        => l_curr_calling_sequence )) THEN
887 
888 
889 	                   l_return_status := FALSE;
890 					   --Bug15914464: Start
891 					   IF P_Invoice_Id IS NULL THEN
892 					      RETURN l_return_status;
893 					   END IF;
894 					   --Bug15914464: End
895 	              END IF;
896 	          END IF;
897 
898            ELSIF (l_inv_tax_list.COUNT > 0 AND l_inv_line_list.COUNT = 0) THEN
899 
900 	          -- Invoice is Tax only. We will need to determine if lines are receipt
901 		  -- matched to call calculate or if all tax information is populated to
902 		  -- call import. In both cases a pseudo line should be created to pass
903 	          -- additional information in the trx_lines GT table.
904               l_no_tax_lines := 'N';--Bug6521120
905               l_call_etax        := TRUE; --Bug7136832
906                   -----------------------------------------------------------------
907 	          l_debug_info := 'Populate pseudo TRX line, TAX lines to be imported';
908               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
909                  FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
910               END IF;
911               --Print(l_api_name,l_debug_info);
912 	          -----------------------------------------------------------------
913 		      IF (l_return_status = TRUE) THEN
914 	              IF NOT(AP_ETAX_SERVICES_PKG.Populate_Tax_Lines_GT(
915 			          P_Invoice_Header_Rec      => l_inv_header_rec,
916 			          P_Calling_Mode            => P_Calling_Mode,
917 			          P_Event_Class_Code        => l_event_class_code,
918 			          P_Tax_only_Flag           => 'Y',
919 			          P_Inv_Rcv_Matched         => l_Inv_Rcv_Matched,
920 			          P_Error_Code              => P_error_code,
921 			          P_Calling_Sequence        => l_curr_calling_sequence )) THEN
922 
923 
924 	                   l_return_status := FALSE;
925 					   --Bug15914464: Start
926 					   IF P_Invoice_Id IS NULL THEN
927 					      RETURN l_return_status;
928 					   END IF;
929 					   --Bug15914464: End
930 	              END IF;
931 	          END IF;
932            ELSE
933             --Bug7136832
934             -----------------------------------------------------------------
935             l_debug_info := 'No invoice lines to be processed for Invoice Id '||l_inv_header_rec.invoice_id;
936             IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
937                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
938             END IF;
939             --Print(l_api_name,l_debug_info);
940             -----------------------------------------------------------------
941             DELETE FROM zx_trx_headers_gt
942                 WHERE application_id   = AP_ETAX_PKG.AP_APPLICATION_ID
943                   AND entity_code      = AP_ETAX_PKG.AP_ENTITY_CODE
944                   AND event_class_code IN
945                                  (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
946                                   AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
947                                   AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
948                   AND trx_id = l_inv_header_rec.invoice_id;
949 
950                --Bug9436217
951 
952                If P_Invoice_Id IS  NOT NULL THEN -- Bug 10170317
953 
954                --Bug9436217
955 
956                   -----------------------------------------------------------------
957                   l_debug_info := 'Invoice Id Is Not Null - ' || P_Invoice_Id ; -- Bug 10170317
958                   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
959                      FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
960                   END IF;
961                   --Print(l_api_name,l_debug_info);
962                   -----------------------------------------------------------------
963                   RETURN TRUE;
964                END IF;
965            --Bug7136832
966            END IF;
967         END LOOP;
968 
969         --Bug7136832
970 
971         IF NOT l_call_etax  THEN
972             -----------------------------------------------------------------
973             l_debug_info := 'No lines to be processed Hence Return';
974             IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
975                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
976             END IF;
977             --Print(l_api_name,l_debug_info);
978             -----------------------------------------------------------------
979             RETURN TRUE;
980         END IF;
981 
982         --Bug7136832
983 
984         AP_ETAX_PKG.G_INV_ID_LIST.DELETE;
985         AP_ETAX_PKG.G_EVNT_CLS_LIST.DELETE;
986     END LOOP;
990     AP_ETAX_PKG.G_EVNT_CLS_LIST.DELETE;
987     CLOSE C_SELECTED_INVOICES;
988 
989     AP_ETAX_PKG.G_INV_ID_LIST.DELETE;
991 
992 
993     IF (l_Inv_Rcv_Matched = 'Y' OR l_no_tax_lines = 'Y') THEN
994 
995         IF (l_return_status = TRUE) THEN
996             -----------------------------------------------------------------
997             l_debug_info := 'Call calculate_tax service';
998             IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
999                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1000             END IF;
1001             --Print(l_api_name,l_debug_info);
1002             -----------------------------------------------------------------
1003             zx_api_pub.calculate_tax(
1004 	          p_api_version      => 1.0,
1005 	          p_init_msg_list    => FND_API.G_TRUE,
1006 	          p_commit           => FND_API.G_FALSE,
1007 	          p_validation_level => FND_API.G_VALID_LEVEL_FULL,
1008 	          x_return_status    => l_return_status_service,
1009 	          x_msg_count        => l_msg_count,
1010 	          x_msg_data         => l_msg_data);
1011        END IF;
1012     ELSE
1013        IF (l_return_status = TRUE) THEN
1014            -----------------------------------------------------------------
1015            l_debug_info := 'Call import_document_with_tax service';
1016            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1017               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1018            END IF;
1019            --Print(l_api_name,l_debug_info);
1020            -----------------------------------------------------------------
1021 	   zx_api_pub.import_document_with_tax(
1022 	          p_api_version      => 1.0,
1023 	          p_init_msg_list    => FND_API.G_TRUE,
1024 	          p_commit           => FND_API.G_FALSE,
1025 	          p_validation_level => FND_API.G_VALID_LEVEL_FULL,
1026 	          x_return_status    => l_return_status_service,
1027 	          x_msg_count        => l_msg_count,
1028 	          x_msg_data         => l_msg_data);
1029        END IF;
1030     END IF;
1031 
1032 
1033     IF (l_return_status_service = 'S') THEN
1034         -----------------------------------------------------------------
1035         l_debug_info := 'Handle return of tax lines';
1036         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1037            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1038         END IF;
1039         --Print(l_api_name,l_debug_info);
1040         -----------------------------------------------------------------
1041         OPEN C_SELECTED_INVOICES;
1042         LOOP
1043            FETCH C_SELECTED_INVOICES
1044             BULK COLLECT INTO AP_ETAX_PKG.G_INV_ID_LIST,
1045 	                      AP_ETAX_PKG.G_EVNT_CLS_LIST
1046 	   LIMIT AP_ETAX_PKG.G_BATCH_LIMIT;
1047 
1048            --Bug9436217
1049 
1050            IF P_Invoice_Id IS NULL THEN
1051 
1052            --Bug9436217
1053 
1054 
1055               EXIT WHEN (C_SELECTED_INVOICES%NOTFOUND
1056                          AND AP_ETAX_PKG.G_INV_ID_LIST.COUNT <= 0);
1057            ELSE
1058               EXIT WHEN AP_ETAX_PKG.G_INV_ID_LIST.COUNT <= 0;
1059            END IF;
1060 
1061            FOR i IN AP_ETAX_PKG.G_INV_ID_LIST.FIRST..AP_ETAX_PKG.G_INV_ID_LIST.LAST
1062            LOOP
1063                OPEN  Invoice_Header(ap_etax_pkg.g_inv_id_list(i));
1064                FETCH Invoice_Header INTO l_inv_header_rec;
1065                CLOSE Invoice_Header;
1066 
1067                IF NOT(AP_ETAX_SERVICES_PKG.Update_AP(
1068 		                P_Invoice_header_rec => l_inv_header_rec,
1069 		                P_Calling_Mode       => P_Calling_Mode,
1070 		                P_All_Error_Messages => P_All_Error_Messages,
1071 		                P_Error_Code         => P_error_code,
1072 		                P_Calling_Sequence   => l_curr_calling_sequence)) THEN
1073 
1074                    l_return_status := FALSE;
1075                END IF;
1076            END LOOP;
1077            AP_ETAX_PKG.G_INV_ID_LIST.DELETE;
1078            AP_ETAX_PKG.G_EVNT_CLS_LIST.DELETE;
1079         END LOOP;
1080         CLOSE C_SELECTED_INVOICES;
1081 
1082     ELSE
1083         -----------------------------------------------------------------
1084         l_debug_info := 'Handle errors returned by API';
1085         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1086            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1087         END IF;
1088         --Print(l_api_name,l_debug_info);
1089         -----------------------------------------------------------------
1090         l_return_status := FALSE;
1091 
1092         IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
1093 	               P_All_Error_Messages  => P_All_Error_Messages,
1094 	               P_Msg_Count           => l_msg_count,
1095 	               P_Msg_Data            => l_msg_data,
1096 	               P_Error_Code          => P_Error_Code,
1097 	               P_Calling_Sequence    => l_curr_calling_sequence)) THEN
1098             NULL;
1099         END IF;
1100     END IF;
1101 
1102     AP_ETAX_PKG.G_INV_ID_LIST.DELETE;
1103     AP_ETAX_PKG.G_EVNT_CLS_LIST.DELETE;
1104     g_manual_tax_lines := null; --bug 16656238
1105 
1106     RETURN l_return_status;
1107 
1108   EXCEPTION
1109     WHEN OTHERS THEN
1110       IF (SQLCODE <> -20001) THEN
1111         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
1112         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
1113         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
1114         FND_MESSAGE.SET_TOKEN('PARAMETERS',
1115           ' P_Invoice_Id = '||P_Invoice_Id||
1116           ' P_Calling_Mode ='||P_Calling_Mode||
1117           ' P_Error_Code = '||P_Error_Code||
1121 
1118           ' P_Calling_Sequence = '||P_Calling_Sequence);
1119         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
1120       END IF;
1122       APP_EXCEPTION.RAISE_EXCEPTION;
1123 
1124   END Calculate;
1125 
1126 /*=============================================================================
1127  |  FUNCTION - Calculate_Import()
1128  |
1129  |  DESCRIPTION
1130  |      Public function that will call the calculate_tax service for
1131  |      calculation and recalculation from the import program.
1132  |      This new calling mode is required to avoid the repopulation of the eTax
1133  |      global temp tables
1134  |      This API assumes the calling code controls the commit cycle.
1135  |      This function returns TRUE if the call to the service is successful.
1136  |      Otherwise, FALSE.
1137  |
1138  |  PARAMETERS
1139  |      P_Invoice_Id - invoice id
1140  |      P_Calling_Mode - calling mode.  Identifies which service to call
1141  |      P_Interface_Invoice_Id - Interface invoice id
1142  |      P_All_Error_Messages - Should API return 1 error message or allow
1143  |                             calling point to get them from message stack
1144  |      P_error_code - Error code to be returned
1145  |      P_calling_sequence -  Calling sequence
1146  |
1147  |  MODIFICATION HISTORY
1148  |    DATE          Author         Action
1149  |    14-JAN-2004   SYIDNER        Created
1150  |
1151  *============================================================================*/
1152 
1153   FUNCTION Calculate_Import(
1154              P_Invoice_Id              IN NUMBER,
1155              P_Calling_Mode            IN VARCHAR2,
1156              P_Interface_Invoice_Id    IN NUMBER,
1157              P_All_Error_Messages      IN VARCHAR2,
1158              P_Error_Code              OUT NOCOPY VARCHAR2,
1159              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
1160   IS
1161 
1162     l_debug_info                 VARCHAR2(240);
1163     l_curr_calling_sequence      VARCHAR2(4000);
1164 
1165     l_return_status_service       VARCHAR2(4000);
1166     l_msg_count                   NUMBER;
1167     l_msg_data                    VARCHAR2(4000);
1168     l_msg                         VARCHAR2(4000);
1169 
1170     l_return_status               BOOLEAN := TRUE;
1171     l_inv_header_rec             ap_invoices_all%ROWTYPE;
1172 
1173     CURSOR Invoice_Header IS
1174     SELECT *
1175       FROM ap_invoices_all
1176      WHERE invoice_id = P_Invoice_Id;
1177 
1178     CURSOR Invoice_Lines (c_invoice_id NUMBER) IS
1179     SELECT *
1180       FROM ap_invoice_lines_all
1181      WHERE invoice_id = c_invoice_id
1182        AND line_type_lookup_code NOT IN ('TAX', 'AWT');
1183 
1184     l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
1185     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
1186 
1187     l_api_name                    CONSTANT VARCHAR2(100) := 'Calculate_Import';
1188 
1189   BEGIN
1190 
1191 
1192     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Calculate_Import<-' ||
1193                                P_calling_sequence;
1194 
1195     -----------------------------------------------------------------
1196     l_debug_info := 'Step 1: Populating invoice header local record';
1197     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1198         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1199     END IF;
1200     -----------------------------------------------------------------
1204     BEGIN
1201     -- This call is included to get the data required to call the MC API
1202     -- and to call Update_AP if the call to the eTax service is succesfull
1203 
1205       OPEN Invoice_Header;
1206       FETCH Invoice_Header INTO l_inv_header_rec;
1207       CLOSE Invoice_Header;
1208     END;
1209 
1210     --BUG 16656238
1211     -----------------------------------------------------------------
1212     l_debug_info := 'Step 1.5: deleting the GT tbls data to populate
1213                      for actual invoice in Calculate_Import for invoice: '||P_Invoice_Id;
1214     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1215        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1216     END IF;
1217     -----------------------------------------------------------------
1218     DELETE FROM ZX_TRX_HEADERS_GT;
1219     DELETE FROM ZX_TRANSACTION_LINES_GT;
1220     DELETE FROM ZX_IMPORT_TAX_LINES_GT;
1221     DELETE FROM ZX_TRX_TAX_LINK_GT;
1222 
1223     /*
1224     -----------------------------------------------------------------
1225     l_debug_info := 'Step 2: Update trx_id in header';
1226     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1227         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1228     END IF;
1229     --Print(l_api_name, l_debug_info);
1230     -----------------------------------------------------------------
1231     UPDATE zx_trx_headers_gt
1232        SET trx_id = P_Invoice_Id
1233      WHERE application_id   = AP_ETAX_PKG.AP_APPLICATION_ID
1234        AND entity_code      = AP_ETAX_PKG.AP_ENTITY_CODE
1235        AND event_class_code IN (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
1236                                 AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
1237                                 AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
1238        AND trx_id = P_Interface_Invoice_Id;
1239    */ --commented for bug 16656238
1240 
1241   -- if condition commented bug 16656238
1242     -- IF SQL%ROWCOUNT = 0 THEN
1243 
1244            -----------------------------------------------------------------
1245            l_debug_info := 'Reset Tax Calculation Flag';
1246            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1247               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1248            END IF;
1249            --Print(l_api_name, l_debug_info);
1250            -----------------------------------------------------------------
1251            UPDATE ap_invoice_lines_all ail
1252               SET ail.tax_already_calculated_flag = NULL
1253             WHERE ail.invoice_id = p_invoice_id
1254               AND ail.line_type_lookup_code NOT IN ('TAX', 'AWT')
1255               AND NVL(ail.tax_already_calculated_flag, 'N') = 'Y';
1256 
1257 	   -- 1. validate_default_import is called during import. This populates
1258 	   --    zx_trx_headers_gt and zx_transaction_lines_gt for validation of
1259 	   --    the taxable lines.
1260 	   -- 2. During import of invoices matched to complex work purchase orders,
1261 	   --    matching will recoup prepayments and calculate tax on it. This
1262 	   --    would have purged zx_trx_headers_gt and zx_transaction_lines_gt.
1263            -- 3. In this case, the staging table will need to repopulated.
1264            --    Parameter P_eTax_Already_called_flag must be passed as 'Y'
1265 	   --    to ensure document level event type is passed to eTax
1266            --    as 'STANDARD UPDATED'.
1267 	   -- 4. Use Case is importing ERS invoices matched to complex work
1268            --    purchase orders with paid advances/prepayments.
1269 
1270 	   -----------------------------------------------------------------
1271 	   l_debug_info := 'Populate Headers';
1272        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1273           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1274        END IF;
1275 	   --Print(l_api_name, l_debug_info);
1276 	   -----------------------------------------------------------------
1277 
1278            IF NOT(AP_ETAX_SERVICES_PKG.Populate_Headers_GT(
1279                       P_Invoice_Header_Rec         => l_inv_header_rec,
1280                       P_Calling_Mode               => 'CALCULATE',
1281                       P_eTax_Already_called_flag   => 'Y',
1282                       P_Event_Class_Code           => l_event_class_code,
1283                       P_Event_Type_Code            => l_event_type_code,
1284                       P_Error_Code                 => P_error_code,
1285                       P_Calling_Sequence           => l_curr_calling_sequence )) THEN
1286 
1287                l_return_status := FALSE;
1288            END IF;
1289 
1290            IF (l_return_status = TRUE)  THEN
1291 
1292                OPEN  Invoice_Lines (p_invoice_id);
1293                FETCH Invoice_Lines
1297                -------------------------------------------------------------------
1294                BULK  COLLECT INTO l_inv_line_list;
1295                CLOSE Invoice_Lines;
1296 
1298 	           l_debug_info := 'Cache Line Defaults';
1299                IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1300                   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1301                END IF;
1302                --Print(l_api_name, l_debug_info);
1303 	           -------------------------------------------------------------------
1304                IF l_inv_header_rec.invoice_type_lookup_code = 'PAYMENT REQUEST' THEN  /* if
1305                   condition for bug 5967914 as we need tp pass party_site_id instead of
1306                   vendor_site_id if invoice_type_lookup_code ='PAYMENT REQUEST' */
1307                  l_payment_request_flag :='Y';  -- for bug 5967914
1308 	              Cache_Line_Defaults
1309 	               ( p_org_id           => l_inv_header_rec.org_id
1310 	                ,p_vendor_site_id   => l_inv_header_rec.party_site_id
1311 	                ,p_calling_sequence => l_curr_calling_sequence);
1312                ELSE
1313                  l_payment_request_flag :='N';  -- for bug 5967914
1314                	  Cache_Line_Defaults
1315 	               ( p_org_id           => l_inv_header_rec.org_id
1316 	                ,p_vendor_site_id   => l_inv_header_rec.vendor_site_id
1317 	                ,p_calling_sequence => l_curr_calling_sequence);
1318              END IF;
1319 
1320                -----------------------------------------------------------------
1321                l_debug_info := 'Populate Lines';
1322                IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1323                    FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1324                END IF;
1325                --Print(l_api_name, l_debug_info);
1326                -----------------------------------------------------------------
1327 
1328                IF NOT(AP_ETAX_SERVICES_PKG.Populate_Lines_GT(
1329                                 P_Invoice_Header_Rec      => l_inv_header_rec,
1330                                 P_Calling_Mode            => 'CALCULATE',
1331                                 P_Event_Class_Code        => l_event_class_code,
1332                                 P_Error_Code              => P_error_code,
1333                                 P_Calling_Sequence        => l_curr_calling_sequence )) THEN
1334 
1335                   l_return_status := FALSE;
1336                END IF;
1337            END IF;
1338 
1339    /* ELSE
1340 
1341       -----------------------------------------------------------------
1342       l_debug_info := 'Step 3: Update trx_id in Lines';
1343       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1344           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1345       END IF;
1346       -----------------------------------------------------------------
1347       UPDATE zx_transaction_lines_gt
1348          SET trx_id = P_Invoice_Id
1349        WHERE application_id   = AP_ETAX_PKG.AP_APPLICATION_ID
1350          AND entity_code      = AP_ETAX_PKG.AP_ENTITY_CODE
1351          AND event_class_code IN (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
1352                                   AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
1353                                   AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
1354          AND trx_id = P_Interface_Invoice_Id;
1355 
1356     END IF;
1357     */ --bug 16656238
1358 
1359 	/* bug 16656238
1360     -------------------------------------------------------------------
1361     l_debug_info := 'Step 4: Get event class code';
1362     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1363        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1364     END IF;
1365     -------------------------------------------------------------------
1366     IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
1367       P_Invoice_Type_Lookup_Code => l_inv_header_rec.invoice_type_lookup_code,
1368       P_Event_Class_Code         => l_event_class_code,
1369       P_error_code               => p_error_code,
1370       P_calling_sequence         => l_curr_calling_sequence)) THEN
1371 
1372       l_return_status := FALSE;
1373 
1374     END IF;
1375 
1376     -------------------------------------------------------------------
1377     l_debug_info := 'Step 5: Get event type code';
1381     -------------------------------------------------------------------
1378     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1379        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1380     END IF;
1382     IF (l_return_status = TRUE) THEN
1383       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
1384         P_Event_Class_Code          => l_event_class_code,
1385         P_Calling_Mode              => P_Calling_Mode,
1386         P_eTax_Already_called_flag  => NULL,
1387         P_Event_Type_Code           => l_event_type_Code,
1388         P_Error_Code                => P_error_code,
1389         P_Calling_Sequence          => l_curr_calling_sequence)) THEN
1390 
1391         l_return_status := FALSE;
1392       END IF;
1393     END IF;
1394     */ --bug 16656238
1395 
1396 
1397     IF (l_return_status = TRUE) THEN
1398 
1399       -----------------------------------------------------------------
1400       l_debug_info := 'Step 7: Call calculate_tax service';
1401       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1402          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1403       END IF;
1404       --Print(l_api_name,l_debug_info);
1405       -----------------------------------------------------------------
1406       zx_api_pub.calculate_tax(
1407         p_api_version      => 1.0,
1408         p_init_msg_list    => FND_API.G_TRUE,
1409         p_commit           => FND_API.G_FALSE,
1410         p_validation_level => FND_API.G_VALID_LEVEL_FULL,
1411         x_return_status    => l_return_status_service,
1412         x_msg_count        => l_msg_count,
1413         x_msg_data         => l_msg_data);
1414 
1415     END IF;
1416 
1417     IF (l_return_status_service = 'S') THEN
1418 
1419        -----------------------------------------------------------------
1420        l_debug_info := 'Step 8: Handle return of tax lines';
1421        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1422            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1423        END IF;
1424        -----------------------------------------------------------------
1425        IF NOT(AP_ETAX_SERVICES_PKG.Update_AP(
1426                 P_Invoice_header_rec => l_inv_header_rec,
1427                 P_Calling_Mode       => P_Calling_Mode,
1428                 P_All_Error_Messages => P_All_Error_Messages,
1429                 P_Error_Code         => P_error_code,
1430                 P_Calling_Sequence   => l_curr_calling_sequence)) THEN
1431 
1432           l_return_status := FALSE;
1433        END IF;
1434 
1435     ELSE  -- handle errors
1436 
1437       -----------------------------------------------------------------
1438       l_debug_info := 'Step 9: Handle errors returned by API';
1439       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1440          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1441       END IF;
1442       -----------------------------------------------------------------
1443       l_return_status := FALSE;
1444 
1445       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
1446                P_All_Error_Messages  => P_All_Error_Messages,
1447                P_Msg_Count           => l_msg_count,
1448                P_Msg_Data            => l_msg_data,
1449                P_Error_Code          => P_Error_Code,
1450                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
1451         NULL;
1452       END IF;
1453 
1454     END IF;
1455 
1456     RETURN l_return_status;
1457 
1458   EXCEPTION
1459     WHEN OTHERS THEN
1460       IF (SQLCODE <> -20001) THEN
1461         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
1462         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
1463         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
1464         FND_MESSAGE.SET_TOKEN('PARAMETERS',
1465           ' P_Invoice_Id = '||P_Invoice_Id||
1466           ' P_Calling_Mode ='||P_Calling_Mode||
1467           ' P_Error_Code = '||P_Error_Code||
1468           ' P_Calling_Sequence = '||P_Calling_Sequence);
1469         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
1470       END IF;
1471 
1472       APP_EXCEPTION.RAISE_EXCEPTION;
1473 
1474   END Calculate_Import;
1475 
1476 /*=============================================================================
1477  |  FUNCTION - Distribute()
1478  |
1479  |  DESCRIPTION
1480  |      Public function that will call the determine_recovery service for
1481  |      distribution and redistribution.
1482  |      This API assumes the calling code controls the commit cycle.
1483  |      This function returns TRUE if the call to the service is successful.
1484  |      Otherwise, FALSE.
1485  |
1486  |  PARAMETERS
1487  |      P_Invoice_Id - invoice id
1491  |      P_Calling_Mode - calling mode.  Identifies which service to call
1488  |      P_Line_Number - This parameter will be used to allow this API to
1489  |                      distribute tax only for the line specified in this
1490  |                      parameter.
1492  |      P_All_Error_Messages - Should API return 1 error message or allow
1493  |                             calling point to get them from message stack
1494  |      P_error_code - Error code to be returned
1495  |      P_calling_sequence -  Calling sequence
1496  |
1497  |  MODIFICATION HISTORY
1498  |    DATE          Author         Action
1499  |    07-OCT-2003   SYIDNER        Created
1500  |    29-DEC-2003   SYIDNER        Use of new function to validate if tax was
1501  |                                 already distributed for the invoice.  Function
1502  |                                 created in the ap_etax_utility_pkg.
1503  |
1504  *============================================================================*/
1505 
1506   FUNCTION Distribute(
1507              P_Invoice_id              IN NUMBER,
1508              P_Line_Number             IN NUMBER,
1509              P_Calling_Mode            IN VARCHAR2,
1510              P_All_Error_Messages      IN VARCHAR2,
1511              P_Error_Code              OUT NOCOPY VARCHAR2,
1512              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN IS
1513 
1514     l_debug_info                 VARCHAR2(240);
1515     l_curr_calling_sequence      VARCHAR2(4000);
1516     l_api_name                   CONSTANT VARCHAR2(100) := 'Distribute';
1517 
1518     l_inv_header_rec             ap_invoices_all%ROWTYPE;
1519     l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
1520     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
1521 
1522     l_return_status_service       VARCHAR2(4000);
1523     l_msg_count                   NUMBER;
1524     l_msg_data                    VARCHAR2(4000);
1525     l_msg                         VARCHAR2(4000);
1526 
1527     l_tax_already_distributed     VARCHAR2(1);
1528     l_return_status               BOOLEAN := TRUE;
1529 
1530     l_tax_only_invoice            NUMBER := 0;  --Bug7110987
1531 
1532     --Bug 7413378
1533     call_determine_recovery_flag  BOOLEAN := FALSE;
1534 
1535    CURSOR Invoice_Header (c_invoice_id NUMBER) IS
1536     SELECT ai.*
1537       FROM ap_invoices_all ai,
1538 	   ap_system_parameters_all asp --Bug14779241
1539      WHERE ai.invoice_id = c_invoice_id
1540 	   AND asp.org_id = ai.org_id /* Bug14779241: Start */
1541 	   AND ((asp.base_currency_code = ai.invoice_currency_code) OR
1542                (asp.base_currency_code <> ai.invoice_currency_code AND
1543                ai.exchange_rate IS NOT NULL)); /* Bug14779241: End */
1544 
1545     CURSOR Invoice_Dists (c_invoice_id NUMBER) IS
1546     SELECT aid.*
1547       FROM ap_invoice_lines_all ail,
1548            ap_invoice_distributions_all aid,
1549            ap_invoices_all ai , --Bug14779241
1550 	   ap_system_parameters_all asp  --Bug14779241
1551      WHERE ail.invoice_id  = aid.invoice_id
1552        AND ail.line_number = aid.invoice_line_number
1553        AND ail.invoice_id  = c_invoice_id
1554        AND ai.invoice_id = ail.invoice_id /* Bug14779241: Start */
1555        AND asp.org_id = ai.org_id
1556        AND ((asp.base_currency_code = ai.invoice_currency_code) OR
1557            (asp.base_currency_code <> ai.invoice_currency_code AND
1558            ai.exchange_rate IS NOT NULL)) /* Bug14779241: End */
1559        AND (aid.line_type_lookup_code NOT IN
1560            ('AWT', 'REC_TAX', 'NONREC_TAX', 'TRV', 'TERV', 'TIPV') OR
1561            (ail.line_type_lookup_code = 'TAX'
1562             AND aid.charge_applicable_to_dist_id IS NULL
1563             AND ail.summary_tax_line_id IS NOT NULL
1564             AND aid.detail_tax_dist_id IS NOT NULL)) --Bug9494315
1565        AND (aid.line_type_lookup_code <> 'RETAINAGE'
1566 	    or (aid.line_type_lookup_code = 'RETAINAGE'
1567 	        and ail.line_type_lookup_code = 'RETAINAGE RELEASE'))
1568        AND (aid.line_type_lookup_code <> 'RETROACCRUAL' OR
1569            (aid.line_type_lookup_code = 'RETROACCRUAL' AND dist_match_type ='PO_PRICE_ADJUSTMENT'))   --Bug10416960
1570        AND (related_id IS NULL
1571             or related_id = invoice_distribution_id)
1572        AND (aid.prepay_distribution_id IS NULL
1573             or (aid.prepay_distribution_id IS NOT NULL
1574                 and ail.line_type_lookup_code = 'PREPAY'))
1575        AND ((nvl(ail.discarded_flag, 'N') <> 'Y' AND nvl(ail.cancelled_flag, 'N') <> 'Y') --Bug8811102
1576             OR (ail.line_type_lookup_code = 'TAX'  AND aid.reversal_flag IS NULL)) --Bug9494315
1577        AND p_calling_mode <> 'DISTRIBUTE RECOUP'
1578        AND   (NVL(aid.reversal_flag,'N') <> 'Y') -- bug 12419997 --Bug14383132
1579     UNION
1580     SELECT aid.*
1581       FROM ap_invoice_lines_all ail,
1582            ap_invoice_distributions_all aid,
1583            ap_invoices_all ai,  --Bug14779241
1584 	   ap_system_parameters_all asp  --Bug14779241
1585      WHERE ail.invoice_id  = aid.invoice_id
1586        AND ail.line_number = aid.invoice_line_number
1587        AND ail.invoice_id  = c_invoice_id
1588        AND ai.invoice_id = ail.invoice_id /* Bug14779241: Start */
1589        AND asp.org_id = ai.org_id
1590        AND ((asp.base_currency_code = ai.invoice_currency_code) OR
1591            (asp.base_currency_code <> ai.invoice_currency_code AND
1592            ai.exchange_rate IS NOT NULL)) /* Bug14779241: End */
1593        AND ail.line_type_lookup_code  <> 'PREPAY'
1594        AND aid.line_type_lookup_code  =  'PREPAY'
1595        AND aid.prepay_distribution_id IS NOT NULL
1596        AND p_calling_mode = 'DISTRIBUTE RECOUP'
1600     -- The plsql table will include the primary distribution, but the amount
1597        AND   (NVL(aid.reversal_flag,'N') <> 'Y') -- bug 12419997  --Bug14383132
1598 	     ;
1599 
1601     -- will be modified in the populate_distributions_gt function to add the
1602     -- IPV and ERV amounts if they exist.
1603 
1604 
1605     -- If the API is called to distribute only 1 taxable line, the following
1606     -- cursor will be used
1607     CURSOR Invoice_Dist (c_invoice_id NUMBER, c_line_number NUMBER) IS
1608     SELECT aid.*
1609       FROM ap_invoice_lines_all ail,
1610            ap_invoice_distributions_all aid,
1611            ap_invoices_all ai,  --Bug14779241
1612 	   ap_system_parameters_all asp  --Bug14779241
1613      WHERE ail.invoice_id  = aid.invoice_id
1614        AND ail.line_number = aid.invoice_line_number
1615        AND ail.invoice_id  = c_invoice_id
1616        AND ail.line_number = c_line_number
1617        AND ai.invoice_id = ail.invoice_id /* Bug14779241: Start */
1618        AND asp.org_id = ai.org_id
1619        AND ((asp.base_currency_code = ai.invoice_currency_code) OR
1620            (asp.base_currency_code <> ai.invoice_currency_code AND
1621            ai.exchange_rate IS NOT NULL)) /* Bug14779241: End */
1622        AND (aid.line_type_lookup_code NOT IN
1623            ('AWT', 'REC_TAX', 'NONREC_TAX', 'TRV', 'TERV', 'TIPV') OR
1624            (ail.line_type_lookup_code = 'TAX'
1625             AND aid.charge_applicable_to_dist_id IS NULL
1626             AND ail.summary_tax_line_id IS NOT NULL
1627             AND aid.detail_tax_dist_id IS NOT NULL))  --Bug9494315
1628        AND (aid.line_type_lookup_code <> 'RETROACCRUAL' OR
1629            (aid.line_type_lookup_code = 'RETROACCRUAL' AND dist_match_type ='PO_PRICE_ADJUSTMENT'))  --Bug10416960
1630        AND (related_id IS NULL
1631             or related_id = invoice_distribution_id)
1632        AND (aid.prepay_distribution_id IS NULL
1633             or (aid.prepay_distribution_id IS NOT NULL
1634                 and ail.line_type_lookup_code = 'PREPAY'))
1635        AND ((nvl(ail.discarded_flag, 'N') <> 'Y' AND nvl(ail.cancelled_flag, 'N') <> 'Y') --Bug8811102
1636             OR (ail.line_type_lookup_code = 'TAX'  AND aid.reversal_flag IS NULL)) --Bug9494315
1637        AND p_calling_mode <> 'DISTRIBUTE RECOUP'
1638        AND   (NVL(aid.reversal_flag,'N') <> 'Y') -- bug 12419997  --Bug14383132
1639     UNION
1640     SELECT aid.*
1641       FROM ap_invoice_lines_all ail,
1642            ap_invoice_distributions_all aid,
1643 	   ap_invoices_all ai,  --Bug14779241
1644 	   ap_system_parameters_all asp  --Bug14779241
1645      WHERE ail.invoice_id  = aid.invoice_id
1646        AND ail.line_number = aid.invoice_line_number
1647        AND ail.invoice_id  = c_invoice_id
1648        AND ail.line_number = c_line_number
1649        AND ai.invoice_id = ail.invoice_id /* Bug14779241: Start */
1650        AND asp.org_id = ai.org_id
1651        AND ((asp.base_currency_code = ai.invoice_currency_code) OR
1652            (asp.base_currency_code <> ai.invoice_currency_code AND
1653            ai.exchange_rate IS NOT NULL)) /* Bug14779241: End */
1654        AND ail.line_type_lookup_code  <> 'PREPAY'
1655        AND aid.line_type_lookup_code  =  'PREPAY'
1656        AND aid.prepay_distribution_id IS NOT NULL
1657        AND p_calling_mode = 'DISTRIBUTE RECOUP'
1658        AND   (NVL(aid.reversal_flag,'N') <> 'Y') -- bug 12419997  --Bug14383132
1659 	     ;
1660 
1661 
1662     CURSOR c_selected_invoices IS
1663     SELECT trx_id, event_class_code
1664       FROM zx_trx_headers_gt
1665      WHERE application_id   =  ap_etax_pkg.ap_application_id
1666        AND entity_code      =  ap_etax_pkg.ap_entity_code
1667        AND event_class_code IN (ap_etax_pkg.ap_inv_event_class_code,
1668                                 ap_etax_pkg.ap_pp_event_class_code,
1669                                 ap_etax_pkg.ap_er_event_class_code);
1670     --ER CHANGES 6772098
1671     --Bug6678578 START
1672     /* bug 7569660 modified the below two cursors added a new attribute discarded_flag */
1673     /*
1674     CURSOR c_included_tax_amounts (c_invoice_id NUMBER) IS
1675     SELECT amount,
1676 	       NVL(included_tax_amount,0) included_tax_amount,
1677 		   line_number,
1678            (NVL(total_rec_tax_amt_funcl_curr,0) + NVL(total_nrec_tax_amt_funcl_curr,0)) base_included_tax_amount,
1679 	   discarded_flag
1680       FROM ap_invoice_lines_all
1681      WHERE invoice_id =  c_invoice_id
1682        AND line_type_lookup_code IN ('ITEM', 'ACCRUAL', 'FREIGHT', 'MISCELLANEOUS','PREPAY')  -- bug7338249
1683        AND included_tax_amount IS NOT NULL ;--Bug6874234
1684 
1685     CURSOR c_included_tax_amount (c_invoice_id NUMBER,c_line_number NUMBER) IS
1686     SELECT amount,
1687 	       NVL(included_tax_amount,0) included_tax_amount,
1688 		   line_number,
1689            (NVL(total_rec_tax_amt_funcl_curr,0) + NVL(total_nrec_tax_amt_funcl_curr,0)) base_included_tax_amount,
1690 	   discarded_flag
1691       FROM ap_invoice_lines_all
1692      WHERE invoice_id =  c_invoice_id
1693        AND line_number = c_line_number
1694        AND line_type_lookup_code IN ('ITEM', 'ACCRUAL', 'FREIGHT', 'MISCELLANEOUS','PREPAY') --bug7338249
1695        AND included_tax_amount IS NOT NULL ;--Bug6874234
1696     --Bug6678578 END
1697     */
1698     --ER CHANGES 6772098
1699 
1700     --Bug9436217
1701 
1702     l_validation_request_id ap_invoices_all.validation_request_id%TYPE;
1703 
1704     --Bug9436217
1705 
1706   BEGIN
1707 
1708     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Distribute<-' ||
1709                                P_calling_sequence;
1710 
1711     --Bug9436217
1712 
1713     l_validation_request_id := ap_approval_pkg.g_validation_request_id;
1714 
1715     IF P_Invoice_id IS NOT NULL THEN
1719        DELETE FROM ZX_TRX_HEADERS_GT;
1716 
1717     --Bug9436217
1718 
1720 
1721        -----------------------------------------------------------------
1722        l_debug_info := 'Populate invoice header local record';
1723        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1724            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1725        END IF;
1726        --Print(l_api_name,l_debug_info);
1727        -----------------------------------------------------------------
1728        OPEN  Invoice_Header (p_invoice_id);
1729        FETCH Invoice_Header INTO l_inv_header_rec;
1730        CLOSE Invoice_Header;
1731 
1732 	--Bug14779241 start
1733            IF l_inv_header_rec.invoice_id IS NULL THEN
1734 	    l_return_status :=  TRUE;
1735 	    RETURN l_return_status;
1736 	   END IF;
1737         --Bug14779241 end
1738 
1739        IF ((l_inv_header_rec.quick_credit = 'Y') OR    -- Bug 5638822
1740            (l_inv_header_rec.invoice_type_lookup_code IN ('AWT', 'INTEREST'))) THEN
1741           RETURN l_return_status;
1742        END IF;
1743 
1744 
1745        -------------------------------------------------------------------
1746        l_debug_info := 'Is tax already distributed for invoice';
1747        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1748            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1749        END IF;
1750        --Print(l_api_name,l_debug_info);
1751        -------------------------------------------------------------------
1752        IF (AP_ETAX_UTILITY_PKG.Is_Tax_Already_Dist_Inv(
1753 	          P_Invoice_Id           => p_invoice_id,
1754 	          P_Calling_Sequence     => l_curr_calling_sequence)) THEN
1755 
1756 	         l_tax_already_distributed := 'Y';
1757        ELSE
1758            l_tax_already_distributed := 'N';
1759        END IF;
1760 
1761        -----------------------------------------------------------------
1762        l_debug_info := 'Populate Header';
1763        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1764            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1765        END IF;
1766        --Print(l_api_name,l_debug_info);
1767        -----------------------------------------------------------------
1768        IF NOT(AP_ETAX_SERVICES_PKG.Populate_Headers_GT(
1769 		      P_Invoice_Header_Rec        => l_inv_header_rec,
1770 		      P_Calling_Mode              => P_Calling_Mode,
1771 		      P_eTax_Already_called_flag  => l_tax_already_distributed,
1772 		      P_Event_Class_Code          => l_event_class_code,
1773 		      P_Event_Type_Code           => l_event_type_code,
1774 		      P_Error_Code                => P_error_code,
1775 		      P_Calling_Sequence          => l_curr_calling_sequence )) THEN
1776 
1777            l_return_status :=  FALSE;
1778        END IF;
1779 
1780        ap_etax_pkg.g_inv_id_list(1) := l_inv_header_rec.invoice_id;
1781 
1782 
1783     ELSE
1784        -----------------------------------------------------------------
1785        l_debug_info := 'Batch: Bulk Populate Header';
1786        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1787            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1788        END IF;
1789        --Print(l_api_name,l_debug_info);
1790        -----------------------------------------------------------------
1791        IF NOT AP_ETAX_SERVICES_PKG.Bulk_Populate_Headers_GT(
1792    		        p_validation_request_id => ap_approval_pkg.g_validation_request_id,
1793 	                p_calling_mode          => p_calling_mode,
1794 	                p_error_code            => p_error_code) THEN
1795 
1796            l_return_status := FALSE;
1797        END IF;
1798      END IF;
1799 
1800      IF g_invoices_to_process = 0 THEN
1801         RETURN TRUE;
1802      END IF;
1803 
1804      -----------------------------------------------------------------
1805      l_debug_info := 'Purge Staging Table';
1806      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1807         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1808      END IF;
1809      --Print(l_api_name,l_debug_info);
1810      -----------------------------------------------------------------
1811      DELETE FROM zx_itm_distributions_gt;
1812 
1813     --Bug9436217
1814 
1815     --Bug#11674044 modified the query to improve the performance.
1816      IF p_line_number IS NULL THEN
1820             (SELECT aid1.amount + NVL(SUM(Nvl(amount, 0)),0)
1817         UPDATE ap_invoice_distributions_All aid1
1818         /*no_unnest hints and consolidation of query for bug 11674044 */
1819         SET (aid1.amount, aid1.base_amount) =
1821             , aid1.base_amount + NVL(SUM(Nvl(base_amount, 0)),0)
1822             FROM ap_invoice_distributions_all aid2
1823             WHERE aid2.invoice_id = aid1.invoice_id
1824             AND aid2.charge_applicable_to_dist_id = aid1.invoice_distribution_id
1825             AND aid2.line_type_lookup_code IN ( 'REC_TAX', 'NONREC_TAX', 'TRV', 'TIPV', 'TERV' )
1826             AND EXISTS (SELECT /*+ NO_UNNEST */ 1
1827                            FROM zx_rec_nrec_dist zd
1828                           WHERE zd.application_id = 200
1829                             AND zd.entity_code = 'AP_INVOICES'
1830                             AND zd.event_class_code IN ( 'STANDARD INVOICES', 'EXPENSE REPORTS', 'PREPAYMENT INVOICES' )
1831                             AND zd.trx_id = aid2.invoice_id
1832                             AND zd.rec_nrec_tax_dist_id = aid2.detail_tax_dist_id
1833                             AND Nvl(zd.inclusive_flag, 'N') = 'Y'))
1834          WHERE aid1.invoice_id IN (SELECT /*+ cardinality(gt 10) unnest */ DISTINCT(trx_id)
1835                                      FROM zx_trx_headers_gt gt
1836                                     WHERE application_id = 200
1837                                       AND entity_code = 'AP_INVOICES'
1838                                       AND event_class_code IN ('STANDARD INVOICES','EXPENSE REPORTS','PREPAYMENT INVOICES'))
1839            AND aid1.line_type_lookup_code IN ('ITEM', 'ACCRUAL', 'FREIGHT', 'MISCELLANEOUS')   --bug9314506
1840            AND NOT EXISTS (SELECT /*+  nl_aj */ 1
1841                              FROM ap_invoice_lines_all ail
1842                             WHERE ail.invoice_id = aid1.invoice_id
1843                               AND ail.line_number=aid1.invoice_line_number
1844                               AND NVL(ail.discarded_flag,'N') = 'Y');
1845      END IF;
1846 
1847      --Bug9436217
1848 
1849 
1850 
1851      OPEN C_SELECTED_INVOICES;
1852      LOOP
1853      FETCH C_SELECTED_INVOICES
1854         BULK COLLECT INTO AP_ETAX_PKG.G_INV_ID_LIST,
1855 			  AP_ETAX_PKG.G_EVNT_CLS_LIST
1856         LIMIT AP_ETAX_PKG.G_BATCH_LIMIT;
1857 
1858         --Bug9436217
1859 
1860         IF P_Invoice_id IS NULL THEN
1861 
1862         --Bug9436217
1863 
1864            EXIT WHEN (C_SELECTED_INVOICES%NOTFOUND
1865                       AND AP_ETAX_PKG.G_INV_ID_LIST.COUNT <= 0);
1866         ELSE
1867            EXIT WHEN AP_ETAX_PKG.G_INV_ID_LIST.COUNT <= 0;
1868         END IF;
1869 
1870         FOR i IN AP_ETAX_PKG.G_INV_ID_LIST.FIRST.. AP_ETAX_PKG.G_INV_ID_LIST.LAST
1871         LOOP
1872 
1873         --Bug9436217
1874 
1875 	    IF P_Invoice_id IS NULL THEN
1876 
1877         --Bug9436217
1878 
1879 	      OPEN  Invoice_Header(ap_etax_pkg.g_inv_id_list(i));
1880 	      FETCH Invoice_Header INTO l_inv_header_rec;
1881 	      CLOSE Invoice_Header;
1882 
1883 	      l_event_class_code := ap_etax_pkg.g_evnt_cls_list(i);
1884 
1885 	    ELSE
1886 
1887           ap_etax_pkg.g_inv_id_list(1) := l_inv_header_rec.invoice_id;
1888 
1889         END IF;
1890 
1891            -----------------------------------------------------------------
1892            l_debug_info := 'Populate invoice distributions collection';
1893            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1894                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1895            END IF;
1896            --Print(l_api_name,l_debug_info);
1897            -----------------------------------------------------------------
1898        --ER CHANGES 6772098
1899        IF (p_line_number IS NOT NULL) THEN
1900 
1901         UPDATE ap_invoice_distributions_All aid1
1902         /*no_unnest hints and consolidation of query for bug 11674044 */
1903         SET (aid1.amount, aid1.base_amount) =
1904             (SELECT aid1.amount + NVL(SUM(Nvl(amount, 0)),0)
1905             , aid1.base_amount + NVL(SUM(Nvl(base_amount, 0)),0)
1906             FROM ap_invoice_distributions_all aid2
1907             WHERE aid2.invoice_id =  p_invoice_id
1908 	    AND aid2.invoice_line_number = p_line_number
1909             AND aid2.charge_applicable_to_dist_id = aid1.invoice_distribution_id
1910             AND aid2.line_type_lookup_code IN ( 'REC_TAX', 'NONREC_TAX', 'TRV', 'TIPV', 'TERV' )
1911             AND EXISTS (SELECT /*+ NO_UNNEST */ 1
1912                            FROM zx_rec_nrec_dist zd
1913                           WHERE zd.application_id = 200
1914                             AND zd.entity_code = 'AP_INVOICES'
1915                             AND zd.event_class_code IN ( 'STANDARD INVOICES', 'EXPENSE REPORTS', 'PREPAYMENT INVOICES' )
1916                             AND zd.trx_id = aid2.invoice_id
1917                             AND zd.rec_nrec_tax_dist_id = aid2.detail_tax_dist_id
1918                             AND Nvl(zd.inclusive_flag, 'N') = 'Y'))
1919 		         WHERE aid1.invoice_id =  p_invoice_id
1920 		           AND aid1.invoice_line_number = p_line_number
1921 		           AND aid1.line_type_lookup_code IN ('ITEM', 'ACCRUAL', 'FREIGHT', 'MISCELLANEOUS')  --bug9314506
1922                AND NOT EXISTS (SELECT  /*+  nl_aj */ 1   --9325964
1923                                  FROM ap_invoice_lines_all ail
1924                                 WHERE ail.invoice_id = aid1.invoice_id
1925                                   AND ail.line_number=aid1.invoice_line_number
1926                                   AND NVL(ail.discarded_flag,'N') = 'Y');
1927 	       OPEN  Invoice_Dist (p_invoice_id, p_line_number);
1928 	       FETCH Invoice_Dist
1929 	       BULK COLLECT INTO l_inv_dist_list;
1930 	       CLOSE Invoice_Dist;
1931 
1932 	       --Bug 7436274 (7413378)
1936        ELSE
1933 	       IF (NOT (call_determine_recovery_flag)) AND ((l_inv_dist_list.count > 0) OR  TAX_ONLY_LINE_EXIST(p_invoice_id)) THEN  --Bug8811102
1934 		      call_determine_recovery_flag := TRUE;
1935 	       END IF;
1937 
1938           OPEN Invoice_Dists (ap_etax_pkg.g_inv_id_list(i));
1939 	      FETCH Invoice_Dists
1940 	      BULK COLLECT INTO l_inv_dist_list;
1941 	      CLOSE Invoice_Dists;
1942 
1943 	      --Bug 7413378
1944 	      IF (NOT (call_determine_recovery_flag)) AND ((l_inv_dist_list.count > 0) OR TAX_ONLY_LINE_EXIST(l_inv_header_rec.invoice_id)) THEN  --Bug8811102
1945 		      call_determine_recovery_flag := TRUE;
1946 	      END IF;
1947 
1948           --Bug13632623 : DELETE INVOICE HEADERS DATA FROM GT WHERE NO APPLICABLE DISTS FOR PROCESSING EXIST
1949 
1950           IF l_inv_dist_list.count = 0 AND NOT TAX_ONLY_LINE_EXIST(l_inv_header_rec.invoice_id) THEN
1951 
1952              DELETE FROM zx_trx_headers_gt
1953               WHERE application_id   = AP_ETAX_PKG.AP_APPLICATION_ID
1954                 AND entity_code      = AP_ETAX_PKG.AP_ENTITY_CODE
1955                 AND event_class_code IN (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
1956                                          AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
1957                                          AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
1958                 AND trx_id = l_inv_header_rec.invoice_id;
1959 
1960              -----------------------------------------------------------------
1961      	     l_debug_info := 'DELETE INV ID '||l_inv_header_rec.invoice_id||' FROM zx_trx_headers_gt';
1962              IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1963                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1964              END IF;
1965              -----------------------------------------------------------------
1966 
1967           END IF;
1968 
1969           --Bug13632623 : DELETE INVOICE HEADERS DATA FROM GT WHERE NO APPLICABLE DISTS FOR PROCESSING EXIST
1970 
1971       END IF;
1972       --ER CHANGES 6772098
1973 	   -----------------------------------------------------------------
1974     	   l_debug_info := 'Populate Distributions';
1975            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1976               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
1977            END IF;
1978            --Print(l_api_name,l_debug_info);
1979     	   -----------------------------------------------------------------
1980     	   IF (l_return_status = TRUE
1981 	       and l_inv_dist_list.count > 0) THEN
1982 
1983 	       IF NOT (AP_ETAX_SERVICES_PKG.Populate_Distributions_GT(
1984 			        P_Invoice_Header_Rec      => l_inv_header_rec,
1985 			        P_Calling_Mode            => P_Calling_Mode,
1986 			        P_Event_Class_Code        => l_event_class_code,
1987 			        P_Event_Type_Code         => l_event_type_code,
1988 			        P_Error_Code              => P_error_code,
1989 			        P_Calling_Sequence        => l_curr_calling_sequence )) THEN
1990 
1991 		        l_return_status := FALSE;
1992 	       END IF;
1993 	   END IF;
1994 
1995 
1996            IF l_inv_header_rec.historical_flag = 'Y' THEN
1997 	      -------------------------------------------------------------------
1998        	      l_debug_info := 'Upgrade historical invoice distributions';
1999               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2000                  FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2001               END IF;
2002               --Print(l_api_name,l_debug_info);
2003        	      -------------------------------------------------------------------
2004 	      UPDATE /*+ ROWID (AID) */ AP_Invoice_Distributions_All AID
2005 	      SET   (RECOVERY_RATE_CODE,
2006 	             RECOVERY_RATE_ID,
2007 	             RECOVERY_TYPE_CODE) =
2008 		                       (SELECT REC.Tax_Rate_Code,
2009 		                               REC.Tax_Rate_ID,
2010 		                               REC.Recovery_Type_Code
2011 		                        FROM   ZX_Rates_B RATE,
2012 		                               ZX_Rates_B REC
2013 		                        WHERE  RATE.Tax_Rate_ID		 = AID.Tax_Code_ID
2014 		                        AND    RATE.Tax_Regime_Code	 = REC.Tax_Regime_Code
2015 		                        AND    RATE.Tax			 = REC.Tax
2016 		                        AND    RATE.Tax_Status_Code	 = REC.Tax_Status_Code
2017 		                        AND    RATE.Content_Owner_ID	 = REC.Content_Owner_ID
2018 		                        AND    REC.Rate_type_code	 = 'RECOVERY'
2019 		                        AND    REC.Effective_From <= AID.Accounting_Date
2020 		                        AND    NVL(REC.Effective_To, AID.Accounting_Date) >= AID.Accounting_Date
2021 		                        AND    REC.Active_Flag		 = 'Y'
2022 		                        AND    REC.Percentage_Rate	 = AID.Rec_NRec_Rate
2023 		                        AND    REC.Tax_Rate_Code 	 = 'STANDARD-' || REC.Percentage_Rate
2024 		                        AND    AID.Line_Type_Lookup_Code = 'REC_TAX')
2025              WHERE AID.invoice_id            = l_inv_header_rec.invoice_id
2026 	       AND AID.historical_flag       = 'Y'
2027                AND AID.line_type_lookup_code = 'REC_TAX'
2028                AND AID.recovery_rate_code    Is Null
2032        END LOOP;
2029                AND AID.recovery_rate_id      Is Null
2030                AND AID.recovery_type_code    Is Null;
2031            END IF;
2033 
2034        AP_ETAX_PKG.G_INV_ID_LIST.DELETE;
2035        AP_ETAX_PKG.G_EVNT_CLS_LIST.DELETE;
2036 
2037     END LOOP;
2038     CLOSE C_SELECTED_INVOICES;
2039 
2040     -----------------------------------------------------------------
2041     l_debug_info := 'Call determine_recovery service';
2042     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2043        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2044     END IF;
2045     --Print(l_api_name,l_debug_info);
2046     -----------------------------------------------------------------
2047     --Bug 7413378
2048     --At the end of C_SELECTED_INVOICES cursor, l_inv_dist_list.count contains number of distributions
2049     --for the last invoice id of Invoice Batch.If last invoice has no distributions the
2050     --"return of tax distributions" code is not executed for any invoice of the Invoice Batch.
2051     --Hence removed the l_inv_dist_list.count from IF condition.
2052     /*IF (l_return_status = TRUE
2053         and l_inv_dist_list.count > 0) THEN*/
2054     --"determine_recovery" should be executed if atleast one invoice of the Invoice Batch has distributions.
2055     --Bug7110987
2056     IF (l_return_status = TRUE
2057         and (call_determine_recovery_flag = TRUE OR l_tax_only_invoice > 0)) THEN
2058     --Bug7110987
2059         zx_api_pub.determine_recovery(
2060  	        p_api_version      => 1.0,
2061 	        p_init_msg_list    => FND_API.G_TRUE,
2062 	        p_commit           => FND_API.G_FALSE,
2063 	        p_validation_level => FND_API.G_VALID_LEVEL_FULL,
2064 	        x_return_status    => l_return_status_service,
2065 	        x_msg_count        => l_msg_count,
2066 	        x_msg_data         => l_msg_data);
2067 
2068        IF (l_return_status_service = 'S') THEN
2069            -----------------------------------------------------------------
2070            l_debug_info := 'Handle return of tax distributions';
2071            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2072               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2073            END IF;
2074            --Print(l_api_name,l_debug_info);
2075            -----------------------------------------------------------------
2076            IF NOT(AP_ETAX_SERVICES_PKG.Update_AP(
2077  			P_Invoice_header_rec => l_inv_header_rec,
2078 	                P_Calling_Mode       => P_Calling_Mode,
2079 	                P_All_Error_Messages => P_All_Error_Messages,
2080 	                P_Error_Code         => P_error_code,
2081 	                P_Calling_Sequence   => l_curr_calling_sequence)) THEN
2082 
2083               l_return_status := FALSE;
2084            END IF;
2085 
2086        ELSE  -- handle errors
2087 
2088            l_return_status := FALSE;
2089 
2090            -----------------------------------------------------------------
2091            l_debug_info := 'Handle errors returned by API';
2092            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2093               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2094            END IF;
2095            --Print(l_api_name,l_debug_info);
2096            -----------------------------------------------------------------
2097            IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
2098 			P_All_Error_Messages  => P_All_Error_Messages,
2099 			P_Msg_Count           => l_msg_count,
2100 			P_Msg_Data            => l_msg_data,
2101 			P_Error_Code          => P_Error_Code,
2102 			P_Calling_Sequence    => l_curr_calling_sequence)) THEN
2103               NULL;
2104            END IF;
2105        END IF;
2106 
2107        IF l_return_status = TRUE THEN
2108 
2109        OPEN C_SELECTED_INVOICES;
2110        LOOP
2111           FETCH C_SELECTED_INVOICES
2112           BULK COLLECT INTO AP_ETAX_PKG.G_INV_ID_LIST,
2113                             AP_ETAX_PKG.G_EVNT_CLS_LIST
2114           LIMIT AP_ETAX_PKG.G_BATCH_LIMIT;
2115 
2116           --Bug9436217
2117           IF P_Invoice_id IS NULL THEN
2118           --Bug9436217
2119              EXIT WHEN (C_SELECTED_INVOICES%NOTFOUND
2120                         AND AP_ETAX_PKG.G_INV_ID_LIST.COUNT <= 0);
2121           ELSE
2122              EXIT WHEN AP_ETAX_PKG.G_INV_ID_LIST.COUNT <= 0;
2123           END IF;
2124 
2125           FOR i IN AP_ETAX_PKG.G_INV_ID_LIST.FIRST.. AP_ETAX_PKG.G_INV_ID_LIST.LAST
2126           LOOP
2127             --Bug9436217
2128             IF P_Invoice_id IS NULL THEN
2129             --Bug9436217
2130                 OPEN  Invoice_Header (ap_etax_pkg.g_inv_id_list(i));
2131                 FETCH Invoice_Header
2132                 INTO  l_inv_header_rec;
2133                 CLOSE Invoice_Header;
2134              END IF;
2135 
2136              -----------------------------------------------------------------
2137              l_debug_info := 'Update Invoice Distributions';
2138              IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2139                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2140              END IF;
2141              --Print(l_api_name,l_debug_info);
2142              -----------------------------------------------------------------
2143              IF NOT(AP_ETAX_SERVICES_PKG.Update_Distributions(
2144 	                        P_Invoice_header_rec => l_inv_header_rec,
2145 	                        P_Calling_Mode       => P_Calling_Mode,
2146 	                        P_All_Error_Messages => P_All_Error_Messages,
2147 	                        P_Error_Code         => P_error_code,
2148 	                        P_Calling_Sequence   => l_curr_calling_sequence)) THEN
2149 
2150 	         l_return_status := FALSE;
2151 	     END IF;
2152 	  END LOOP;
2156       CLOSE C_SELECTED_INVOICES;
2153           AP_ETAX_PKG.G_INV_ID_LIST.DELETE;
2154           AP_ETAX_PKG.G_EVNT_CLS_LIST.DELETE;
2155       END LOOP;
2157 
2158       END IF;
2159 
2160       AP_ETAX_PKG.G_INV_ID_LIST.DELETE;
2161       AP_ETAX_PKG.G_EVNT_CLS_LIST.DELETE;
2162 
2163     END IF;
2164 
2165     RETURN l_return_status;
2166 
2167   EXCEPTION
2168     WHEN OTHERS THEN
2169       IF (SQLCODE <> -20001) THEN
2170         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
2171         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
2172         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
2173         FND_MESSAGE.SET_TOKEN('PARAMETERS',
2174           ' P_Invoice_Id = '||P_Invoice_Id||
2175           ' P_Calling_Mode ='||P_Calling_Mode||
2176           ' P_Error_Code = '||P_Error_Code||
2177           ' P_Calling_Sequence = '||P_Calling_Sequence);
2178         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
2179       END IF;
2180 
2181       APP_EXCEPTION.RAISE_EXCEPTION;
2182 
2183   END Distribute;
2184 
2185 /*=============================================================================
2186  |  FUNCTION - Distribute_Import()
2187  |
2188  |  DESCRIPTION
2189  |      Public function that will call the determine_recovery service for
2190  |      distribution during the import.  This API will called only in the case
2191  |      TAX-ONLY lines exist in the invoice.
2192  |      This API assumes the calling code controls the commit cycle.
2193  |      This function returns TRUE if the call to the service is successful.
2194  |      Otherwise, FALSE.
2195  |
2196  |  PARAMETERS
2197  |      P_Invoice_Id - invoice id
2198  |      P_Calling_Mode - calling mode.  Identifies which service to call
2199  |      P_All_Error_Messages - Should API return 1 error message or allow
2200  |                             calling point to get them from message stack
2201  |      P_error_code - Error code to be returned
2202  |      P_calling_sequence -  Calling sequence
2203  |
2204  |  MODIFICATION HISTORY
2205  |    DATE          Author         Action
2206  |    20-JAN-2004   SYIDNER        Created
2207  |
2208  *============================================================================*/
2209   FUNCTION Distribute_Import(
2210              P_Invoice_id              IN NUMBER,
2211              P_Calling_Mode            IN VARCHAR2,
2212              P_All_Error_Messages      IN VARCHAR2,
2213              P_Error_Code              OUT NOCOPY VARCHAR2,
2214              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN IS
2215 
2216     l_debug_info                 VARCHAR2(240);
2217     l_curr_calling_sequence      VARCHAR2(4000);
2218     l_event_class_code
2219       zx_trx_headers_gt.event_class_code%TYPE;
2220     l_event_type_code
2221       zx_trx_headers_gt.event_type_code%TYPE;
2222 
2223     l_return_status_service       VARCHAR2(4000);
2224     l_msg_count                   NUMBER;
2225     l_msg_data                    VARCHAR2(4000);
2226     l_msg                         VARCHAR2(4000);
2227     l_return_status               BOOLEAN := TRUE;
2228 
2229     l_inv_header_rec             ap_invoices_all%ROWTYPE;
2230     l_invoice_id                 ap_invoices_all.invoice_id%TYPE; --Bug14779241
2231     l_api_name                   varchar2(30) := 'Distribute_import'; -- bug 6321366
2232     CURSOR Invoice_Header IS
2233     SELECT *
2234       FROM ap_invoices_all
2235      WHERE invoice_id = P_Invoice_Id;
2236 
2237     CURSOR Invoice_ex_rate_check IS --Bug14779241
2238     SELECT ai.invoice_id
2239       FROM ap_invoices_all ai,
2240 	       ap_system_parameters_all asp
2241      WHERE ai.invoice_id = P_Invoice_Id
2242 	   AND asp.org_id = ai.org_id
2243 	   AND ((asp.base_currency_code = ai.invoice_currency_code) OR
2244          (asp.base_currency_code <> ai.invoice_currency_code AND
2245           ai.exchange_rate IS NOT NULL));
2246 
2247   BEGIN
2248 
2249     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Distribute_Import<-' ||
2250                                P_calling_sequence;
2251 
2252     --Bug14779241: Start
2253 
2254     BEGIN
2255       OPEN Invoice_ex_rate_check;
2256       FETCH Invoice_ex_rate_check INTO l_invoice_id;
2257       CLOSE Invoice_ex_rate_check;
2258     END;
2259 
2260       IF l_invoice_id IS NULL THEN
2261 	    l_return_status :=  TRUE;
2262 	    RETURN l_return_status;
2263 	  END IF;
2264     --Bug14779241: End
2265 
2266     -----------------------------------------------------------------
2267     l_debug_info := 'Step 1: Populating invoice header local record';
2268     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2269        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2270     END IF;
2271     -----------------------------------------------------------------
2272     BEGIN
2273       OPEN Invoice_Header;
2274       FETCH Invoice_Header INTO l_inv_header_rec;
2278     -- There is no need to populate the header since this service is called
2275       CLOSE Invoice_Header;
2276     END;
2277 
2279     -- after the call to calculate tax only if any tax-only line is created
2280     -- as per the eTax cookbook, in this case there is no need to populate
2281     -- the distribution global temporary table.
2282     -----------------------------------------------------------------
2283     l_debug_info := 'Step 3: Call determine_recovery service';
2284     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2285        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2286     END IF;
2287     -----------------------------------------------------------------
2288     IF (l_return_status = TRUE) THEN
2289     --print(l_api_name,l_debug_info); --bug 6321366
2290 
2291       zx_api_pub.determine_recovery(
2292         p_api_version      => 1.0,
2293         p_init_msg_list    => FND_API.G_TRUE,
2294         p_commit           => FND_API.G_FALSE,
2295         p_validation_level => FND_API.G_VALID_LEVEL_FULL,
2296         x_return_status    => l_return_status_service,
2297         x_msg_count        => l_msg_count,
2298         x_msg_data         => l_msg_data);
2299 
2300     END IF;
2301 
2302     -----------------------------------------------------------------
2303     l_debug_info := 'Step 4: Verify return status for determine_recovery';
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_service = 'S') THEN
2309 
2310       -----------------------------------------------------------------
2311       l_debug_info := 'Step 5: Handle return of tax lines';
2312       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2313          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2314       END IF;
2315       -----------------------------------------------------------------
2316       --print(l_api_name,l_debug_info); -- bug 6321366
2317 
2318        IF NOT(AP_ETAX_SERVICES_PKG.Update_AP(
2319                 P_Invoice_header_rec => l_inv_header_rec,
2320                 P_Calling_Mode       => P_Calling_Mode,
2321                 P_All_Error_Messages => P_All_Error_Messages,
2322                 P_Error_Code         => P_error_code,
2323                 P_Calling_Sequence   => l_curr_calling_sequence)) THEN
2324 
2325           l_return_status := FALSE;
2326        END IF;
2327 
2328 
2329     ELSE  -- handle errors
2330       l_return_status := FALSE;
2331       -----------------------------------------------------------------
2332       l_debug_info := 'Step 6: Handle errors returned by API';
2333       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2334          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2335       END IF;
2336       -----------------------------------------------------------------
2337       --print(l_api_name,l_debug_info); --bug 6321366
2338 
2339       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
2340                P_All_Error_Messages  => P_All_Error_Messages,
2341                P_Msg_Count           => l_msg_count,
2342                P_Msg_Data            => l_msg_data,
2343                P_Error_Code          => P_Error_Code,
2344                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
2345         NULL;
2346       END IF;
2347 
2348     END IF;
2349 
2350     RETURN l_return_status;
2351 
2352   EXCEPTION
2353     WHEN OTHERS THEN
2354       IF (SQLCODE <> -20001) THEN
2355         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
2356         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
2357         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
2358         FND_MESSAGE.SET_TOKEN('PARAMETERS',
2359           ' P_Invoice_Id = '||P_Invoice_Id||
2360           ' P_Calling_Mode ='||P_Calling_Mode||
2361           ' P_Error_Code = '||P_Error_Code||
2362           ' P_Calling_Sequence = '||P_Calling_Sequence);
2363         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
2364       END IF;
2365 
2366       APP_EXCEPTION.RAISE_EXCEPTION;
2367 
2368   END Distribute_Import;
2369 
2373  |  DESCRIPTION
2370 /*=============================================================================
2371  |  FUNCTION - Import_Interface()
2372  |
2374  |      Public function that will call the import_document_with_tax service
2375  |      This API assumes the calling code controls the commit cycle.
2376  |      This function returns TRUE if the call to the service is successful.
2377  |      Otherwise, FALSE.
2378  |
2379  |  PARAMETERS
2380  |      P_Invoice_Id - invoice id
2381  |      P_Calling_Mode - calling mode.  Identifies which service to call
2382  |      P_Interface_Invoice_Id - Interface invoice id
2383  |      P_All_Error_Messages - Should API return 1 error message or allow
2384  |                             calling point to get them from message stack
2385  |      P_error_code - Error code to be returned
2386  |      P_calling_sequence -  Calling sequence
2387  |
2388  |  MODIFICATION HISTORY
2389  |    DATE          Author         Action
2390  |    07-OCT-2003   SYIDNER        Created
2391  |
2392  *============================================================================*/
2393 
2394    FUNCTION Import_Interface(
2395              P_Invoice_id              IN NUMBER,
2396              P_Calling_Mode            IN VARCHAR2,
2397              P_Interface_Invoice_Id    IN NUMBER,
2398              P_All_Error_Messages      IN VARCHAR2,
2399              P_Error_Code              OUT NOCOPY VARCHAR2,
2400              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
2401   IS
2402     l_debug_info                 VARCHAR2(240);
2403     l_curr_calling_sequence      VARCHAR2(4000);
2404 
2405     l_return_status_service       VARCHAR2(4000);
2406     l_msg_count                   NUMBER;
2407     l_msg_data                    VARCHAR2(4000);
2408     l_msg                         VARCHAR2(4000);
2409 
2410     l_event_class_code            zx_trx_headers_gt.event_class_code%TYPE;
2411     l_event_type_code             zx_trx_headers_gt.event_type_code%TYPE;
2412 
2413     l_return_status               BOOLEAN := TRUE;
2414     l_inv_header_rec             ap_invoices_all%ROWTYPE;
2415     l_api_name                   varchar2(30) := 'Import_Interface';
2416     l_inv_rcv_matched     VARCHAR2(1) := 'N'; --BUG 16656238
2417 
2418     CURSOR Invoice_Header IS
2419     SELECT *
2420       FROM ap_invoices_all
2421      WHERE invoice_id = P_Invoice_Id;
2422 
2423      --BUG 16656238
2424     CURSOR Invoice_Lines (c_invoice_id NUMBER) IS
2425     SELECT *
2426       FROM ap_invoice_lines_all
2427       WHERE invoice_id = c_invoice_id
2428       AND line_type_lookup_code = 'TAX'
2429       AND NVL(discarded_flag,'N')='N'
2430       AND NVL(cancelled_flag,'N')='N'
2431 	  AND summary_tax_line_id IS NULL;
2432 
2433   BEGIN
2434 
2435     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Import_Interface<-' ||
2436                                P_calling_sequence;
2437 
2438     -----------------------------------------------------------------
2439     l_debug_info := 'Step 1: Populating invoice header local record';
2440     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2441        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2442     END IF;
2443     -----------------------------------------------------------------
2444     BEGIN
2445       OPEN Invoice_Header;
2446       FETCH Invoice_Header INTO l_inv_header_rec;
2447       CLOSE Invoice_Header;
2448     END;
2449     --BUG 16656238 starts
2450     -----------------------------------------------------------------
2451     l_debug_info := 'Step 1.5: deleting the GT tbls data to populate
2452                      for actual invoice in Import Interface for invoice: '||P_Invoice_Id;
2453     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2454        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2455     END IF;
2456     -----------------------------------------------------------------
2457     DELETE FROM ZX_TRX_HEADERS_GT;
2458     DELETE FROM ZX_TRANSACTION_LINES_GT;
2459     DELETE FROM ZX_IMPORT_TAX_LINES_GT;
2460     DELETE FROM ZX_TRX_TAX_LINK_GT;
2461 
2462     -----------------------------------------------------------------
2463     --bug 16656238 ends
2464 
2465     /* --bug 16656238
2466     l_debug_info := 'Step 2: Update Header P_Invoice_Id '||P_Invoice_Id;
2467     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2468        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2469     END IF;
2470     -----------------------------------------------------------------
2471     BEGIN
2472       UPDATE zx_trx_headers_gt
2473          SET trx_id = P_Invoice_Id
2474        WHERE application_id   = AP_ETAX_PKG.AP_APPLICATION_ID
2475          AND entity_code      = AP_ETAX_PKG.AP_ENTITY_CODE
2476          AND event_class_code IN (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
2477                                   AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
2478                                   AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
2479          AND trx_id = P_Interface_Invoice_Id;
2480     EXCEPTION WHEN NO_DATA_FOUND THEN NULL;
2481     END;
2482 
2483 		l_debug_info := 'Header rows updated: '||SQL%ROWCOUNT;
2484        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2485           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2486        END IF;
2487     */
2488     --BUG 16656238 starts
2489     -- IF SQL%ROWCOUNT = 0 THEN
2490 
2491 	   -----------------------------------------------------------------
2492 	   l_debug_info := 'Populate Headers';
2493        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2494           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2495        END IF;
2496 	   --Print(l_api_name, l_debug_info);
2497 	   -----------------------------------------------------------------
2498 
2502                       P_eTax_Already_called_flag   => 'N',
2499            IF NOT(AP_ETAX_SERVICES_PKG.Populate_Headers_GT(
2500                       P_Invoice_Header_Rec         => l_inv_header_rec,
2501                       P_Calling_Mode               => 'CALCULATE',
2503                       P_Event_Class_Code           => l_event_class_code,
2504                       P_Event_Type_Code            => l_event_type_code,
2505                       P_Error_Code                 => P_error_code,
2506                       P_Calling_Sequence           => l_curr_calling_sequence )) THEN
2507 
2508                l_return_status := FALSE;
2509            END IF;
2510 
2511            IF (l_return_status = TRUE)  THEN
2512 
2513                OPEN  Invoice_Lines (p_invoice_id);
2514                FETCH Invoice_Lines
2515               -- BULK  COLLECT INTO l_inv_line_list; --commented for BUG 16656238
2516                BULK  COLLECT INTO l_inv_tax_list; --added for BUG 16656238
2517                CLOSE Invoice_Lines;
2518 
2519                -------------------------------------------------------------------
2520 	           l_debug_info := 'Cache Line Defaults';
2521                IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2522                   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2523                END IF;
2524                --Print(l_api_name, l_debug_info);
2525 	           -------------------------------------------------------------------
2526                IF l_inv_header_rec.invoice_type_lookup_code = 'PAYMENT REQUEST' THEN  /* if
2527                   condition for bug 5967914 as we need tp pass party_site_id instead of
2528                   vendor_site_id if invoice_type_lookup_code ='PAYMENT REQUEST' */
2529                  l_payment_request_flag :='Y';  -- for bug 5967914
2530 	                Cache_Line_Defaults
2531 	               ( p_org_id           => l_inv_header_rec.org_id
2532 	                ,p_vendor_site_id   => l_inv_header_rec.party_site_id
2533 	                ,p_calling_sequence => l_curr_calling_sequence);
2534                ELSE
2535                  l_payment_request_flag :='N';  -- for bug 5967914
2536                	        Cache_Line_Defaults
2537 	               ( p_org_id           => l_inv_header_rec.org_id
2538 	                ,p_vendor_site_id   => l_inv_header_rec.vendor_site_id
2539 	                ,p_calling_sequence => l_curr_calling_sequence);
2540                END IF;
2541 
2542                -----------------------------------------------------------------
2543                l_debug_info := 'Populate Lines';
2544                IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2545                    FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2546                END IF;
2547                --Print(l_api_name, l_debug_info);
2548                -----------------------------------------------------------------
2549 
2550              IF NOT(AP_ETAX_SERVICES_PKG.Populate_Tax_Lines_GT(
2551 			        P_Invoice_Header_Rec      => l_inv_header_rec,
2552 			        P_Calling_Mode            => P_Calling_Mode,
2553 			        P_Event_Class_Code        => l_event_class_code,
2554 			        P_Tax_only_Flag           => 'Y',
2555 			        P_Inv_Rcv_Matched         => l_inv_rcv_matched,
2556 			        P_Error_Code              => P_error_code,
2557 			        P_Calling_Sequence        => l_curr_calling_sequence )) THEN
2558 
2559 	                l_return_status := FALSE;
2560              END IF;
2561            END IF;
2562 
2563    /* ELSE
2564 
2565       -----------------------------------------------------------------
2566       l_debug_info := 'Step 3: Update trx_id in Lines';
2567       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2568           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2569       END IF;
2570       -----------------------------------------------------------------
2571       UPDATE zx_transaction_lines_gt
2572          SET trx_id = P_Invoice_Id
2573        WHERE application_id   = AP_ETAX_PKG.AP_APPLICATION_ID
2574          AND entity_code      = AP_ETAX_PKG.AP_ENTITY_CODE
2575          AND event_class_code IN (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
2576                                   AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
2577                                   AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
2578          AND trx_id = P_Interface_Invoice_Id;
2579 
2580     -----------------------------------------------------------------
2581     l_debug_info := 'Step 4: Update trx_id in tax Lines';
2582     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2583         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2584     END IF;
2585     -----------------------------------------------------------------
2586      BEGIN
2587       UPDATE zx_import_tax_lines_gt
2588          SET trx_id = P_Invoice_Id
2589        WHERE trx_id = P_Interface_Invoice_Id;
2590      EXCEPTION WHEN NO_DATA_FOUND THEN NULL;
2591      END;
2592 
2593 
2594     -----------------------------------------------------------------
2595     l_debug_info := 'Step 5: Update trx_id in the allocation structure '||
2596                     'etax table';
2597     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2598        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2599     END IF;
2600     -----------------------------------------------------------------
2601     BEGIN
2602       UPDATE zx_trx_tax_link_gt
2603          SET trx_id = P_Invoice_Id
2604        WHERE trx_id = P_Interface_Invoice_Id;
2605     EXCEPTION WHEN NO_DATA_FOUND THEN NULL;
2606     END;
2607 
2608     END IF;
2609 	*/
2610  --END BUG 16656238
2611     /*  --bug 16656238
2612     -------------------------------------------------------------------
2613     l_debug_info := 'Step 6: Get event class code';
2614     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2618 
2615        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2616     END IF;
2617     -------------------------------------------------------------------
2619     IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
2620       P_Invoice_Type_Lookup_Code => l_inv_header_rec.invoice_type_lookup_code,
2621       P_Event_Class_Code         => l_event_class_code,
2622       P_error_code               => p_error_code,
2623       P_calling_sequence         => l_curr_calling_sequence)) THEN
2624 
2625       l_return_status := FALSE;
2626 
2627     END IF;
2628 
2629     -------------------------------------------------------------------
2630     l_debug_info := 'Step 7: Get event type code';
2631     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2632         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2633     END IF;
2634     -------------------------------------------------------------------
2635 
2636     IF (l_return_status = TRUE) THEN
2637       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
2638         P_Event_Class_Code          => l_event_class_code,
2639         P_Calling_Mode              => P_Calling_Mode,
2640         P_eTax_Already_called_flag  => NULL,
2641         P_Event_Type_Code           => l_event_type_Code,
2642         P_Error_Code                => P_error_code,
2643         P_Calling_Sequence          => l_curr_calling_sequence)) THEN
2644 
2645         l_return_status := FALSE;
2646       END IF;
2647     END IF;
2648     */
2649     -----------------------------------------------------------------
2650     l_debug_info := 'Step 9: Call import_document_with_tax service';
2651     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2652        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2653     END IF;
2654     -----------------------------------------------------------------
2655     IF ( l_return_status = TRUE ) THEN --bug 16656238
2656 
2657       zx_api_pub.import_document_with_tax(
2658         p_api_version      => 1.0,
2659         p_init_msg_list    => FND_API.G_TRUE,
2660         p_commit           => FND_API.G_FALSE,
2661         p_validation_level => FND_API.G_VALID_LEVEL_FULL,
2662         x_return_status    => l_return_status_service,
2663         x_msg_count        => l_msg_count,
2664         x_msg_data         => l_msg_data);
2665 
2666     END IF;
2667 
2668     IF (l_return_status_service = 'S') THEN
2669 
2670       -----------------------------------------------------------------
2671       l_debug_info := 'Step 10: Handle return of tax lines';
2672       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2673           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2674       END IF;
2675       -----------------------------------------------------------------
2676        IF NOT(AP_ETAX_SERVICES_PKG.Update_AP(
2677                 P_Invoice_header_rec => l_inv_header_rec,
2678                 P_Calling_Mode       => P_Calling_Mode,
2679                 P_All_Error_Messages => P_All_Error_Messages,
2680                 P_Error_Code         => P_error_code,
2681                 P_Calling_Sequence   => l_curr_calling_sequence)) THEN
2682 
2683           l_return_status := FALSE;
2684        END IF;
2685 
2686     ELSE  -- handle errors
2687 
2688       -----------------------------------------------------------------
2689       l_debug_info := 'Step 11: Handle errors returned by API';
2690       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2691          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2692       END IF;
2693       -----------------------------------------------------------------
2694       l_return_status := FALSE;
2695 
2696       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
2697                P_All_Error_Messages  => P_All_Error_Messages,
2698                P_Msg_Count           => l_msg_count,
2699                P_Msg_Data            => l_msg_data,
2700                P_Error_Code          => P_Error_Code,
2704 
2701                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
2702         NULL;
2703       END IF;
2705     END IF;
2706 
2707    RETURN l_return_status;
2708 
2709   EXCEPTION
2710     WHEN OTHERS THEN
2711       IF (SQLCODE <> -20001) THEN
2712         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
2713         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
2714         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
2715         FND_MESSAGE.SET_TOKEN('PARAMETERS',
2716           ' P_Invoice_Id = '||P_Invoice_Id||
2717           ' P_Calling_Mode ='||P_Calling_Mode||
2718           ' P_Error_Code = '||P_Error_Code||
2719           ' P_Calling_Sequence = '||P_Calling_Sequence);
2720         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
2721       END IF;
2722 
2723       APP_EXCEPTION.RAISE_EXCEPTION;
2724   END Import_Interface;
2725 
2726 /*=============================================================================
2727  |  FUNCTION - Reverse_Invoice()
2728  |
2729  |  DESCRIPTION
2730  |      Public function that will call the reverse_document_distribution
2731  |      service for quick credit (full reversal.)
2732  |      This API assumes the calling code controls the commit cycle.
2733  |      This function returns TRUE if the call to the service is successful.
2734  |      Otherwise, FALSE.
2735  |
2736  |  PARAMETERS
2737  |      P_Invoice_Id - invoice id
2738  |      P_Calling_Mode - calling mode.  Identifies which service to call
2739  |      P_All_Error_Messages - Should API return 1 error message or allow
2740  |                             calling point to get them from message stack
2741  |      P_error_code - Error code to be returned
2742  |      P_calling_sequence -  Calling sequence
2743  |
2744  |  MODIFICATION HISTORY
2745  |    DATE          Author         Action
2746  |    07-OCT-2003   SYIDNER        Created
2747  |
2748  *============================================================================*/
2749 
2750   FUNCTION Reverse_Invoice(
2751              P_Invoice_id              IN NUMBER,
2752              P_Calling_Mode            IN VARCHAR2,
2753              P_All_Error_Messages      IN VARCHAR2,
2754              P_Error_Code              OUT NOCOPY VARCHAR2,
2755              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
2756   IS
2757 
2758     l_debug_info                 VARCHAR2(240);
2759     l_curr_calling_sequence      VARCHAR2(4000);
2760 
2761     l_inv_header_rec             ap_invoices_all%ROWTYPE;
2762     l_credited_inv_rec           ap_invoices_all%ROWTYPE;
2763 
2764     l_event_class_code_crediting zx_trx_headers_gt.event_class_code%TYPE;
2765     l_event_class_code_credited  zx_trx_headers_gt.event_class_code%TYPE;
2766 
2767     l_tax_already_distributed     VARCHAR2(1);
2768     l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
2769     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
2770 
2771     CURSOR Crediting_Inv_Header IS
2772     SELECT *
2773       FROM ap_invoices_all
2774      WHERE invoice_id = P_Invoice_Id
2775        AND quick_credit = 'Y'
2776        AND credited_invoice_id IS NOT NULL;
2777 
2778     CURSOR Credited_Inv_Header(c_credited_inv NUMBER) IS
2779     SELECT *
2780       FROM ap_invoices_all
2781      WHERE invoice_id = c_credited_inv;
2782 
2783     l_return_status_service       VARCHAR2(4000);
2784     l_msg_count                   NUMBER;
2785     l_msg_data                    VARCHAR2(4000);
2786     l_msg                         VARCHAR2(4000);
2787 
2788     l_return_status               BOOLEAN := TRUE;
2789 
2790     l_api_name                    CONSTANT VARCHAR2(100) := 'Reverse_Invoice';
2791 
2792   BEGIN
2793     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Reverse_Invoice<-' ||
2794                                P_calling_sequence;
2795 
2796     -----------------------------------------------------------------
2797     l_debug_info := 'Step 1: Populating crediting invoice header';
2798     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2799       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2800     END IF;
2801     -----------------------------------------------------------------
2802     BEGIN
2803       OPEN Crediting_Inv_Header;
2804       FETCH Crediting_Inv_Header INTO l_inv_header_rec;
2805       CLOSE Crediting_Inv_Header;
2806     END;
2807 
2808     -----------------------------------------------------------------
2809     l_debug_info := 'Step 2: Populating credited invoice header';
2810     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2811       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2812     END IF;
2813     -----------------------------------------------------------------
2814     BEGIN
2815       OPEN Credited_Inv_Header(l_inv_header_rec.credited_invoice_id);
2816       FETCH Credited_Inv_Header INTO l_credited_inv_rec;
2817       CLOSE Credited_Inv_Header;
2818     END;
2819 
2820     IF NOT tax_distributions_exist
2821 		(p_invoice_id  => l_credited_inv_rec.invoice_id) THEN
2822 
2823        RETURN l_return_status;
2824 
2825     END IF;
2826 
2827     -------------------------------------------------------------------
2828     l_debug_info := 'Step 3: Get crediting invoice event class code';
2829     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2830       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2831     END IF;
2832     -------------------------------------------------------------------
2833 
2834     IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
2835       P_Invoice_Type_Lookup_Code => l_inv_header_rec.invoice_type_lookup_code,
2836       P_Event_Class_Code         => l_event_class_code_crediting,
2837       P_error_code               => P_error_code,
2841 
2838       P_calling_sequence         => l_curr_calling_sequence)) THEN
2839 
2840       l_return_status := FALSE;
2842     END IF;
2843 
2844     -------------------------------------------------------------------
2845     l_debug_info := 'Step 4: Get credited invoice event class code';
2846     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2847       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2848     END IF;
2849     -------------------------------------------------------------------
2850     IF (l_return_status = TRUE) THEN
2851       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
2852         P_Invoice_Type_Lookup_Code => l_credited_inv_rec.invoice_type_lookup_code,
2853         P_Event_Class_Code         => l_event_class_code_credited,
2854         P_error_code               => P_error_code,
2855         P_calling_sequence         => l_curr_calling_sequence)) THEN
2856 
2857         l_return_status := FALSE;
2858       END IF;
2859     END IF;
2860 
2861     INSERT INTO zx_rev_trx_headers_gt(
2862         internal_organization_id,
2863         reversing_appln_id,
2864         reversing_entity_code,
2865         reversing_evnt_cls_code,
2866         reversing_trx_id,
2867         legal_entity_id,
2868         trx_number
2869     ) VALUES
2870 	(l_inv_header_rec.org_id,
2871          200,
2872          'AP_INVOICES',
2873          l_event_class_code_credited,
2874          l_inv_header_rec.invoice_id,
2875          l_inv_header_rec.legal_entity_id,
2876          l_inv_header_rec.invoice_num);
2877 
2878     -----------------------------------------------------------------
2879     l_debug_info := 'Step 5: Populate zx_reverse_trx_lines_gt';
2880     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2881       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2882     END IF;
2883     -----------------------------------------------------------------
2884     IF (l_return_status = TRUE) THEN
2885       INSERT INTO zx_reverse_trx_lines_gt(
2886         internal_organization_id,
2887         reversing_appln_id,
2888         reversing_entity_code,
2889         reversing_evnt_cls_code,
2890         reversing_trx_id,
2891         reversing_trx_level_type,
2892         reversing_trx_line_id,
2893         reversed_appln_id,
2894         reversed_entity_code,
2895         reversed_evnt_cls_code,
2896         reversed_trx_id,
2897         reversed_trx_level_type,
2898         reversed_trx_line_id,
2899         trx_line_gl_date -- bug 9880212
2900       )
2901       SELECT
2902         l_inv_header_rec.org_id,        -- internal_organization_id
2903         200,                            -- reversing_appln_id
2904         'AP_INVOICES',                  -- reversing_entity_code
2905         l_event_class_code_crediting,   -- reversing_evnt_cls_code
2906         ail.invoice_id,                 -- reversing_trx_id
2907         'LINE',                         -- reversing_trx_level_type
2908         ail.line_number,                -- reversing_trx_line_id
2909         200,                            -- reversed_appln_id
2910         'AP_INVOICES',                  -- reversed_entity_code
2911         l_event_class_code_credited,    -- reversed_evnt_cls_code
2912         aic.invoice_id,                 -- reversed_trx_id
2913         'LINE',                         -- reversed_trx_level_type
2914         aic.line_number,                -- reversed_trx_line_id
2915         ail.accounting_date             -- trx_line_gl_date -- bug 9880212
2916         FROM ap_invoice_lines_all ail,
2917              ap_invoice_lines_all aic
2918        WHERE ail.invoice_id = l_inv_header_rec.invoice_id
2919          AND ail.line_type_lookup_code NOT IN ('TAX', 'AWT')
2920          AND ail.corrected_inv_id = aic.invoice_id
2921          AND ail.corrected_line_number = aic.line_number;
2922 
2923 
2924     END IF;
2925 
2926     -----------------------------------------------------------------
2927     l_debug_info := 'Step 6: Populate zx_reverse_dist_gt';
2928     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2929       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2930     END IF;
2931     -----------------------------------------------------------------
2932     IF (l_return_status = TRUE) THEN
2933       INSERT INTO zx_reverse_dist_gt(
2934         internal_organization_id,
2935         reversing_appln_id,
2936         reversing_entity_code,
2937         reversing_evnt_cls_code,
2938         reversing_trx_id,
2939         reversing_trx_level_type,
2940         reversing_trx_line_id,
2941         reversing_trx_line_dist_id,
2942         reversing_tax_line_id,
2943         reversed_appln_id,
2944         reversed_entity_code,
2945         reversed_evnt_cls_code,
2946         reversed_trx_id,
2947         reversed_trx_level_type,
2948         reversed_trx_line_id,
2949         reversed_trx_line_dist_id,
2950         reversed_tax_line_id
2951       )
2952       SELECT
2953         l_inv_header_rec.org_id,        -- internal_organization_id
2954         200,                            -- reversing_appln_id
2955         'AP_INVOICES',                  -- reversing_entity_code
2956         l_event_class_code_crediting,   -- reversing_evnt_cls_code
2957         aid.invoice_id,                 -- reversing_trx_id
2958         'LINE',                         -- reversing_trx_level_type
2959         aid.invoice_line_number,        -- reversing_trx_line_id
2960         aid.invoice_distribution_id,    -- reversing_trx_line_dist_id
2961         NULL,                           -- reversing_tax_line_id
2962         200,                            -- reversed_appln_id
2963         'AP_INVOICES',                  -- reversed_entity_code
2964         l_event_class_code_credited,    -- reversed_evnt_cls_code
2965         idc.invoice_id,                 -- reversed_trx_id
2969         NULL                            -- reversed_tax_line_id
2966         'LINE',                         -- reversed_trx_level_type
2967         idc.invoice_line_number,        -- reversed_trx_line_id
2968         idc.invoice_distribution_id,    -- reversed_trx_line_dist_id
2970         FROM ap_invoice_distributions_all aid,
2971              ap_invoice_distributions_all idc
2972        WHERE aid.invoice_id = l_inv_header_rec.invoice_id
2973          AND aid.line_type_lookup_code NOT IN
2974              ('AWT', 'REC_TAX', 'NONREC_TAX', 'TRV', 'TERV', 'TIPV')
2975          AND (aid.related_id IS NULL
2976               OR aid.related_id = aid.invoice_distribution_id)
2977          AND aid.corrected_invoice_dist_id = idc.invoice_distribution_id;
2978 
2979        -- this select make sure that only the primary distribution is populated
2980        -- in the eTax temporary table.  There is no need to summary because the
2981        -- amount is not included.  eTax will take the amount from the reversed
2982        -- tax distributions.
2983 
2984     END IF;
2985 
2986     -----------------------------------------------------------------
2987     l_debug_info := 'Step 7: Call reverse_document_distribution service';
2988     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
2989       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
2990     END IF;
2991     -----------------------------------------------------------------
2992     IF ( l_return_status = TRUE ) THEN
2993 
2994       zx_api_pub.reverse_document_distribution(
2995         p_api_version      => 1.0,
2996         p_init_msg_list    => FND_API.G_TRUE,
2997         p_commit           => FND_API.G_FALSE,
2998         p_validation_level => FND_API.G_VALID_LEVEL_FULL,
2999         x_return_status    => l_return_status_service,
3000         x_msg_count        => l_msg_count,
3001         x_msg_data         => l_msg_data);
3002 
3003     END IF;
3004 
3005     -----------------------------------------------------------------
3006     l_debug_info := 'Step 8: Verify return status';
3007     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3008       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3009     END IF;
3010     -----------------------------------------------------------------
3011     IF (l_return_status_service = 'S') THEN
3012        -----------------------------------------------------------------
3013        l_debug_info := 'Step 9: Handle return of tax lines and dist';
3014        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3015            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3016        END IF;
3017        -----------------------------------------------------------------
3018 
3019        -- Tax distributions insert works off of zx_trx_headers_gt. Since
3020        -- reverse_document_distribution does not require this table to be
3021        -- populated, we are using this as a proxy to avoid maintaining two
3022        -- code lines.
3023        -------------------------------------------------------------------
3024        l_debug_info := 'Is tax already distributed for invoice';
3025        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3026           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3027        END IF;
3028        --Print(l_api_name,l_debug_info);
3029        -------------------------------------------------------------------
3030        IF (AP_ETAX_UTILITY_PKG.Is_Tax_Already_Dist_Inv(
3031                   P_Invoice_Id           => p_invoice_id,
3032                   P_Calling_Sequence     => l_curr_calling_sequence)) THEN
3033 
3034            l_tax_already_distributed := 'Y';
3035        ELSE
3036            l_tax_already_distributed := 'N';
3037        END IF;
3038 
3039        -----------------------------------------------------------------
3040        l_debug_info := 'Populate Header';
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        --Print(l_api_name,l_debug_info);
3045        -----------------------------------------------------------------
3046        IF NOT(AP_ETAX_SERVICES_PKG.Populate_Headers_GT(
3047                       P_Invoice_Header_Rec        => l_inv_header_rec,
3048                       P_Calling_Mode              => P_Calling_Mode,
3049                       P_eTax_Already_called_flag  => l_tax_already_distributed,
3050                       P_Event_Class_Code          => l_event_class_code,
3051                       P_Event_Type_Code           => l_event_type_code,
3052                       P_Error_Code                => P_error_code,
3053                       P_Calling_Sequence          => l_curr_calling_sequence )) THEN
3054 
3055            l_return_status :=  FALSE;
3056        END IF;
3057 
3058        IF NOT(AP_ETAX_SERVICES_PKG.Update_AP(
3059                 P_Invoice_header_rec => l_inv_header_rec,
3060                 P_Calling_Mode       => P_Calling_Mode,
3061                 P_All_Error_Messages => P_All_Error_Messages,
3062                 P_Error_Code         => P_error_code,
3063                 P_Calling_Sequence   => l_curr_calling_sequence)) THEN
3064 
3065           l_return_status := FALSE;
3066        END IF;
3067 
3068    ELSE  -- handle errors
3069 
3070       l_return_status := FALSE;
3071       -----------------------------------------------------------------
3072       l_debug_info := 'Step 10: Handle errors returned by API';
3073       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3074         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3075       END IF;
3076       -----------------------------------------------------------------
3077       l_return_status := FALSE;
3078 
3079       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
3080                P_All_Error_Messages  => P_All_Error_Messages,
3084                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
3081                P_Msg_Count           => l_msg_count,
3082                P_Msg_Data            => l_msg_data,
3083                P_Error_Code          => P_Error_Code,
3085         NULL;
3086       END IF;
3087 
3088     END IF;
3089 
3090 
3091    RETURN l_return_status;
3092 
3093   EXCEPTION
3094     WHEN OTHERS THEN
3095       IF (SQLCODE <> -20001) THEN
3096         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
3097         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
3098         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
3099         FND_MESSAGE.SET_TOKEN('PARAMETERS',
3100           ' P_Invoice_Id = '||P_Invoice_Id||
3101           ' P_Calling_Mode ='||P_Calling_Mode||
3102           ' P_Error_Code = '||P_Error_Code||
3103           ' P_Calling_Sequence = '||P_Calling_Sequence);
3104         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
3105       END IF;
3106 
3107       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3108         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,sqlerrm);
3109       END IF;
3110 
3111       APP_EXCEPTION.RAISE_EXCEPTION;
3112 
3113   END Reverse_Invoice;
3114 
3115 /*=============================================================================
3116  |  FUNCTION - Override_Tax()
3117  |
3118  |  DESCRIPTION
3119  |      Public function that will call the override_tax service.
3120  |      This API assumes the calling code controls the commit cycle.
3121  |      This function returns TRUE if the call to the service is successful.
3122  |      Otherwise, FALSE.
3123  |
3124  |  PARAMETERS
3125  |      P_Invoice_Id         - invoice id
3126  |      P_Calling_Mode       - calling mode.  Identifies which service to call
3127  |      P_Override_Status    - override_status parameter returned by the eTax
3128  |                             UI (Tax lines and summary lines window).
3129  |      P_Event_id	     - Indicates a specific instance of the override event.
3130  |                             Tax line windows will return an event_id when there
3131  |                             are any user overrides.
3132  |      P_All_Error_Messages - Should API return 1 error message or allow
3133  |                             calling point to get them from message stack
3134  |      P_error_code         - Error code to be returned
3135  |      P_calling_sequence   -  Calling sequence
3136  |
3137  |  MODIFICATION HISTORY
3138  |    DATE          Author         Action
3139  |    07-OCT-2003   SYIDNER        Created
3140  |
3141  *============================================================================*/
3142 
3143   FUNCTION Override_Tax(
3144              P_Invoice_id              IN NUMBER,
3145              P_Calling_Mode            IN VARCHAR2,
3146              P_Override_Status         IN VARCHAR2,
3147 	     P_Event_Id		       IN NUMBER,
3148              P_All_Error_Messages      IN VARCHAR2,
3149              P_Error_Code              OUT NOCOPY VARCHAR2,
3150              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
3151   IS
3152 
3153     l_debug_info                 VARCHAR2(240);
3154     l_curr_calling_sequence      VARCHAR2(4000);
3155 
3156     l_inv_header_rec             ap_invoices_all%ROWTYPE;
3157     l_invoice_id                 ap_invoices_all.invoice_id%TYPE; --Bug14779241
3158     l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
3159     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
3160     l_transaction_rec		 zx_api_pub.transaction_rec_type;
3161 
3162     CURSOR Invoice_Header IS
3163     SELECT *
3164       FROM ap_invoices_all
3165      WHERE invoice_id = P_Invoice_Id;
3166 
3167     CURSOR Invoice_Lines IS
3168     SELECT *
3169       FROM ap_invoice_lines_all
3170      WHERE invoice_id = P_Invoice_Id
3171        AND line_type_lookup_code NOT IN ('TAX', 'AWT');
3172 
3173     CURSOR Invoice_ex_rate_check IS --Bug14779241
3174     SELECT ai.invoice_id
3175       FROM ap_invoices_all ai,
3176 	       ap_system_parameters_all asp
3177      WHERE ai.invoice_id = P_Invoice_Id
3178 	   AND asp.org_id = ai.org_id
3179 	   AND ((asp.base_currency_code = ai.invoice_currency_code) OR
3180          (asp.base_currency_code <> ai.invoice_currency_code AND
3181           ai.exchange_rate IS NOT NULL));
3182 
3183     l_return_status_service       VARCHAR2(4000);
3184     l_msg_count                   NUMBER;
3185     l_msg_data                    VARCHAR2(4000);
3186     l_msg                         VARCHAR2(4000);
3187 
3188     l_return_status               BOOLEAN := TRUE;
3189     l_success			  BOOLEAN := TRUE;
3190 
3191     l_api_name			  CONSTANT VARCHAR2(100) := 'Override_Tax';
3192 
3193   BEGIN
3194     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Override_Tax<-' ||
3195                                P_calling_sequence;
3196 
3197     IF (P_Override_Status = 'SYNCHRONIZE') THEN
3198       -----------------------------------------------------------------
3199       l_debug_info := 'Step 1: Update ap_invoice_lines_all from eTax '||
3200                       'repository';
3201       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3202           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3203       END IF;
3204       -----------------------------------------------------------------
3205       BEGIN
3206         UPDATE ap_invoice_lines_all ail
3207            SET
3208          (-- Bug 10157751 ail.description,
3209           ail.cancelled_flag,
3210           ail.last_updated_by,
3211           ail.last_update_login,
3212           ail.last_update_date,
3213           ail.attribute_category,
3214           ail.attribute1,
3215           ail.attribute2,
3216           ail.attribute3,
3220           ail.attribute7,
3217           ail.attribute4,
3218           ail.attribute5,
3219           ail.attribute6,
3221           ail.attribute8,
3222           ail.attribute9,
3223           ail.attribute10,
3224           ail.attribute11,
3225           ail.attribute12,
3226           ail.attribute13,
3227           ail.attribute14,
3228           ail.attribute15,
3229           ail.global_attribute_category,
3230           ail.global_attribute1,
3231           ail.global_attribute2,
3232           ail.global_attribute3,
3233           ail.global_attribute4,
3234           ail.global_attribute5,
3235           ail.global_attribute6,
3236           ail.global_attribute7,
3237           ail.global_attribute8,
3238           ail.global_attribute9,
3239           ail.global_attribute10,
3240           ail.global_attribute11,
3241           ail.global_attribute12,
3242           ail.global_attribute13,
3243           ail.global_attribute14,
3244           ail.global_attribute15,
3245           ail.global_attribute16,
3246           ail.global_attribute17,
3247           ail.global_attribute18,
3248           ail.global_attribute19,
3249           ail.global_attribute20 ) = (
3250           SELECT
3251 	  /* Bug 10157751
3252           DECODE( ail.line_source,
3253 		  'MANUAL LINE ENTRY', ail.description,
3254 		  'IMPORTED'         , ail.description,
3255                   zls.tax_regime_code||' - '||zls.tax ), -- description : Bug 9383712 - Added DECODE
3256 	  */
3257           zls.cancel_flag,                     -- cancelled_flag
3258           l_user_id,                           -- last_updated_by
3259           l_login_id,                          -- last_update_login
3260           l_sysdate,                           -- last_update_date
3261           zls.attribute_category,
3262           zls.attribute1,
3263           zls.attribute2,
3264           zls.attribute3,
3265           zls.attribute4,
3266           zls.attribute5,
3267           zls.attribute6,
3268           zls.attribute7,
3269           zls.attribute8,
3270           zls.attribute9,
3271           zls.attribute10,
3272           zls.attribute11,
3273           zls.attribute12,
3274           zls.attribute13,
3275           zls.attribute14,
3276           zls.attribute15,
3277           zls.global_attribute_category,
3278           zls.global_attribute1,
3279           zls.global_attribute2,
3280           zls.global_attribute3,
3281           zls.global_attribute4,
3282           zls.global_attribute5,
3283           zls.global_attribute6,
3284           zls.global_attribute7,
3285           zls.global_attribute8,
3286           zls.global_attribute9,
3287           zls.global_attribute10,
3288           zls.global_attribute11,
3289           zls.global_attribute12,
3290           zls.global_attribute13,
3291           zls.global_attribute14,
3292           zls.global_attribute15,
3293           zls.global_attribute16,
3294           zls.global_attribute17,
3295           zls.global_attribute18,
3296           zls.global_attribute19,
3297           zls.global_attribute20
3298           FROM zx_lines_summary zls
3299          WHERE zls.summary_tax_line_id = ail.summary_tax_line_id
3300            AND nvl(zls.reporting_only_flag, 'N') = 'N'
3301          )
3302          WHERE ail.invoice_id = P_Invoice_Id
3303            AND ail.line_type_lookup_code = 'TAX'
3304            AND EXISTS (SELECT ls.summary_tax_line_id
3305                          FROM zx_lines_summary ls
3306                         WHERE ls.summary_tax_line_id = ail.summary_tax_line_id
3307                           AND ls.trx_id = ail.invoice_id
3308                           AND NVL(ls.tax_amt_included_flag, 'N') = 'N'
3309                           AND NVL(ls.self_assessed_flag, 'N') = 'N'
3310                           AND nvl(ls.reporting_only_flag, 'N') = 'N');
3311       EXCEPTION
3312         WHEN NO_DATA_FOUND THEN NULL;
3313 
3314         WHEN OTHERS THEN
3315           IF (SQLCODE <> -20001) THEN
3316             FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
3317             FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
3318             FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
3319             FND_MESSAGE.SET_TOKEN('PARAMETERS',
3320               ' P_Invoice_Id = '||P_Invoice_Id||
3321               ' P_Error_Code = '||P_Error_Code||
3322               ' P_Calling_Sequence = '||P_Calling_Sequence);
3323             FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
3324           END IF;
3325 
3326         APP_EXCEPTION.RAISE_EXCEPTION;
3327       END;
3328 
3329     ELSIF (P_Override_Status IN ('DETAIL_OVERRIDE', 'SUMMARY_OVERRIDE')) THEN
3330       -----------------------------------------------------------------
3331       l_debug_info := 'Step 2: Populating invoice header local record';
3332       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3333           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3334       END IF;
3335       -----------------------------------------------------------------
3336 
3337       BEGIN
3338         OPEN Invoice_Header;
3339         FETCH Invoice_Header INTO l_inv_header_rec;
3340         CLOSE Invoice_Header;
3341       END;
3342 
3343       -----------------------------------------------------------------
3344       l_debug_info := 'Step 3: Populating invoice lines collection';
3345       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3346           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3347       END IF;
3348       -----------------------------------------------------------------
3349 
3350       BEGIN
3354         CLOSE Invoice_Lines;
3351         OPEN Invoice_Lines;
3352         FETCH Invoice_Lines
3353         BULK COLLECT INTO l_inv_line_list;
3355       END;
3356 
3357       -----------------------------------------------------------------
3358       l_debug_info := 'Step 4: Populate Header';
3359       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3360           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3361       END IF;
3362       -----------------------------------------------------------------
3363 
3364       IF NOT(AP_ETAX_SERVICES_PKG.Populate_Headers_GT(
3365         P_Invoice_Header_Rec         => l_inv_header_rec,
3366         P_Calling_Mode               => P_Calling_Mode,
3367         P_eTax_Already_called_flag   => NULL,
3368         P_Event_Class_Code           => l_event_class_code,
3369         P_Event_Type_Code            => l_event_type_code,
3370         P_Error_Code                 => P_error_code,
3371         P_Calling_Sequence           => l_curr_calling_sequence )) THEN
3372 
3373         l_return_status := FALSE;
3374       END IF;
3375 
3376       -----------------------------------------------------------------
3377       l_debug_info := 'Purge staging table, Clear/Load Cache';
3378       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3379          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3380       END IF;
3381       --Print(l_api_name,l_debug_info);
3382       -----------------------------------------------------------------
3383       DELETE FROM ZX_TRANSACTION_LINES_GT;
3384 
3385       AP_ETAX_SERVICES_PKG.G_SITE_ATTRIBUTES.DELETE;
3386       AP_ETAX_SERVICES_PKG.G_ORG_ATTRIBUTES.DELETE;
3387 
3388              IF l_inv_header_rec.invoice_type_lookup_code = 'PAYMENT REQUEST' THEN  /* if
3389                   condition for bug 5967914 as we need tp pass party_site_id instead of
3390                   vendor_site_id if invoice_type_lookup_code ='PAYMENT REQUEST' */
3391                  l_payment_request_flag :='Y';  -- for bug 5967914
3392 	         Cache_Line_Defaults
3393 	               ( p_org_id           => l_inv_header_rec.org_id
3394 	                ,p_vendor_site_id   => l_inv_header_rec.party_site_id
3395 	                ,p_calling_sequence => l_curr_calling_sequence);
3396              ELSE
3397                  l_payment_request_flag :='N';  -- for bug 5967914
3398                	  Cache_Line_Defaults
3399 	               ( p_org_id           => l_inv_header_rec.org_id
3400 	                ,p_vendor_site_id   => l_inv_header_rec.vendor_site_id
3401 	                ,p_calling_sequence => l_curr_calling_sequence);
3402              END IF;
3403 
3404       -----------------------------------------------------------------
3405       l_debug_info := 'Step 5: Populate Lines';
3406       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3407           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3408       END IF;
3409       -----------------------------------------------------------------
3410       IF ( l_return_status = TRUE ) THEN
3411         IF NOT(AP_ETAX_SERVICES_PKG.Populate_Lines_GT(
3412           P_Invoice_Header_Rec      => l_inv_header_rec,
3413           P_Calling_Mode            => P_Calling_Mode,
3414           P_Event_Class_Code        => l_event_class_code,
3415           P_Error_Code              => P_error_code,
3416           P_Calling_Sequence        => l_curr_calling_sequence )) THEN
3417 
3418           l_return_status := FALSE;
3419         END IF;
3420       END IF;
3421 
3422       l_transaction_rec.internal_organization_id := l_inv_header_rec.org_id;
3423       l_transaction_rec.application_id		 := 200;
3424       l_transaction_rec.entity_code		 := 'AP_INVOICES';
3425       l_transaction_rec.event_class_code	 := l_event_class_code;
3426       l_transaction_rec.event_type_code		 := l_event_type_code;
3427       l_transaction_rec.trx_id			 := l_inv_header_rec.invoice_id;
3428 
3429       -----------------------------------------------------------------
3430       l_debug_info := 'Step 6: Call override_tax service';
3431       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3432           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3433       END IF;
3434       -----------------------------------------------------------------
3435       IF ( l_return_status = TRUE ) THEN
3436 
3437         zx_api_pub.override_tax(
3438           p_api_version      => 1.0,
3439           p_init_msg_list    => FND_API.G_TRUE,
3440           p_commit           => FND_API.G_FALSE,
3441           p_validation_level => FND_API.G_VALID_LEVEL_FULL,
3442           p_override_level   => P_Override_Status,
3443 	      p_transaction_rec  => l_transaction_rec,
3444 	      p_event_id	     => p_event_id,
3445           x_return_status    => l_return_status_service,
3446           x_msg_count        => l_msg_count,
3447           x_msg_data         => l_msg_data);
3448 
3449       END IF;
3450 
3451       -----------------------------------------------------------------
3452       l_debug_info := 'Step 7: Verify return status';
3453       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3454           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3455       END IF;
3456       -----------------------------------------------------------------
3457 
3458       IF (l_return_status_service = 'S') THEN
3459 
3460         -----------------------------------------------------------------
3461         l_debug_info := 'Step 8: Handle return of tax lines';
3462         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3463             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3464         END IF;
3465         -----------------------------------------------------------------
3466          IF NOT(AP_ETAX_SERVICES_PKG.Update_AP(
3467                   P_Invoice_header_rec => l_inv_header_rec,
3468                   P_Calling_Mode       => P_Calling_Mode,
3469                 P_All_Error_Messages   => P_All_Error_Messages,
3470                   P_Error_Code         => P_error_code,
3471                   P_Calling_Sequence   => l_curr_calling_sequence)) THEN
3472 
3473             l_return_status := FALSE;
3474          END IF;
3475 
3476       ELSE  -- handle errors
3477         l_return_status := FALSE;
3478         -----------------------------------------------------------------
3479         l_debug_info := 'Step 9: Handle errors returned by API';
3480         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3481             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3482         END IF;
3483         -----------------------------------------------------------------
3484 
3485         IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
3486                  P_All_Error_Messages  => P_All_Error_Messages,
3487                  P_Msg_Count           => l_msg_count,
3488                  P_Msg_Data            => l_msg_data,
3489                  P_Error_Code          => P_Error_Code,
3490                  P_Calling_Sequence    => l_curr_calling_sequence)) THEN
3491           NULL;
3492         END IF;
3493       END IF; -- end of return_status_service
3494 
3495       -----------------------------------------------------------------
3496       l_debug_info := 'Step 10: Call Freeze Distributions';
3497       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3498           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3499       END IF;
3500       -----------------------------------------------------------------
3501       IF ( l_return_status = TRUE ) THEN -- Bug 9383712
3502         --Bug7592845
3503         IF NOT(AP_ETAX_SERVICES_PKG.Freeze_itm_Distributions(
3504 	                  P_Invoice_Header_Rec  => l_inv_header_rec,
3505 	                  P_Calling_Mode        => 'FREEZE DISTRIBUTIONS',
3506 	                  P_Event_Class_Code    => l_event_class_code,
3507 	                  P_All_Error_Messages  => P_All_Error_Messages,
3508 	                  P_Error_Code          => P_error_code,
3509       	              P_Calling_Sequence    => l_curr_calling_sequence)) THEN
3510 
3511                l_return_status := FALSE;
3512 
3513         END IF;
3514       END IF ;
3515 
3516       -----------------------------------------------------------------
3517       l_debug_info := 'Step 11: Call Distribute';
3518       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3519           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3520       END IF;
3521       -----------------------------------------------------------------
3522 
3523 	--Bug14779241: Start
3524 	BEGIN
3525           OPEN Invoice_ex_rate_check;
3526           FETCH Invoice_ex_rate_check INTO l_invoice_id;
3527           CLOSE Invoice_ex_rate_check;
3528         END;
3529 	--Bug14779241: End
3530 
3531       IF l_invoice_id IS NOT NULL THEN --Bug14779241
3532       IF ( l_return_status = TRUE ) THEN -- Bug 9383712
3533         l_success := ap_etax_pkg.calling_etax(
3534 			P_Invoice_id         => l_inv_header_rec.invoice_id,
3535 			P_Calling_Mode       => 'DISTRIBUTE',
3536 			P_All_Error_Messages => P_All_Error_Messages,
3537 			P_error_code         => P_error_code,
3538 			P_Calling_Sequence   => l_curr_calling_sequence);
3539 
3540         IF (not l_success) THEN
3541             l_return_status := FALSE;
3542         END IF;
3543       END IF ;
3544       END IF;
3545 
3546       -----------------------------------------------------------------
3547       l_debug_info := 'Step 12: Update Total Tax Amount';
3548       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3549           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3550       END IF;
3551       -----------------------------------------------------------------
3552       IF ( l_return_status = TRUE ) THEN -- Bug 9383712
3553 
3554 	--Modified below query for bug#16022947 to consider
3555 	--Prepay lines
3556        	UPDATE ap_invoices_all ai
3557           SET (ai.total_tax_amount,
3558                ai.self_assessed_tax_amount) =
3559                    (SELECT SUM(DECODE(NVL(zls.self_assessed_flag, 'N'),
3560                             'N', case when exists (SELECT 'Prepay App Exists'
3561                                           FROM ap_invoice_lines_all prepay
3562                                          WHERE prepay.invoice_id = zls.trx_id
3563                                          AND prepay.line_type_lookup_code = 'PREPAY'
3564                                          AND prepay.prepay_invoice_id  = zls.applied_from_trx_id
3565                                          AND prepay.prepay_line_number = zls.applied_from_line_id
3566                                          AND prepay.invoice_includes_prepay_flag = 'Y'
3567                                          AND (prepay.discarded_flag is null
3568                                            or prepay.discarded_flag = 'N')) THEN
3569                                            0
3570                                         ELSE NVL(zls.tax_amt, 0) end,
3571                                        0)),
3572                         SUM(DECODE(NVL(zls.self_assessed_flag, 'N'),
3573                                    'Y', NVL(zls.tax_amt, 0),
3574                                     0))
3575                    FROM zx_lines_summary zls
3576             WHERE zls.application_id = 200
3577            AND zls.entity_code = 'AP_INVOICES'
3578            AND zls.event_class_code IN
3579               ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
3580                   AND zls.trx_id   = ai.invoice_id
3581                   AND NVL(zls.reporting_only_flag, 'N') = 'N')
3582           WHERE ai.invoice_id = l_inv_header_rec.invoice_id;
3583 
3584      --Bug#16022947. Introduced below UPDATE to consider
3585      --Retro Tax amounts which will not be in ZLS tables.
3586 
3587       UPDATE ap_invoices_all ai
3588       SET ai.total_tax_amount = (nvl(ai.total_tax_amount,0) +
3589                                  (SELECT NVL(sum(NVL(ail.amount,0)),0)
3590                                   FROM ap_invoice_lines_all ail
3591                                   WHERE ail.invoice_id = ai.invoice_id
3592                                    AND line_type_lookup_code ='RETROTAX'))
3593       WHERE ai.invoice_id = l_inv_header_rec.invoice_id;
3594 
3595 
3596          --Bug9494315
3597         UPDATE ap_invoice_distributions_all
3598            SET distribution_class = 'PERMANENT'
3599          WHERE invoice_id = l_inv_header_rec.invoice_id
3600            AND distribution_class = 'CANDIDATE'
3601            AND line_type_lookup_code IN ('REC_TAX','NONREC_TAX','TRV','TERV','TIPV');
3602 
3603         --Bug9494315
3604 
3605         --Bug9777752
3606 
3607         UPDATE ap_self_assessed_tax_dist_all
3608            SET distribution_class = 'PERMANENT'
3609          WHERE invoice_id = l_inv_header_rec.invoice_id
3610            AND distribution_class = 'CANDIDATE'
3611            AND line_type_lookup_code IN ('REC_TAX','NONREC_TAX','TRV','TERV','TIPV');
3612 
3613         --Bug9777752
3614 
3615       END IF ;
3616 
3617    END IF;  -- end of p_override_status
3618 
3619    RETURN l_return_status;
3620 
3621   EXCEPTION
3622     WHEN OTHERS THEN
3623       IF (SQLCODE <> -20001) THEN
3624         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
3625         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
3626         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
3627         FND_MESSAGE.SET_TOKEN('PARAMETERS',
3628           ' P_Invoice_Id = '||P_Invoice_Id||
3629           ' P_Calling_Mode ='||P_Calling_Mode||
3630           ' P_Error_Code = '||P_Error_Code||
3631           ' P_Calling_Sequence = '||P_Calling_Sequence);
3632         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
3633       END IF;
3634 
3635       APP_EXCEPTION.RAISE_EXCEPTION;
3636 
3637   END Override_Tax;
3638 
3639 
3640 /*=============================================================================
3641  |  FUNCTION - Override_Recovery()
3642  |
3643  |  DESCRIPTION
3644  |      Public function that will call the override_recovery service.
3645  |      This API assumes the calling code controls the commit cycle.
3646  |      This function returns TRUE if the call to the service is successful.
3647  |      Otherwise, FALSE.
3648  |
3649  |  PARAMETERS
3650  |      P_Invoice_Id - invoice id
3651  |      P_Calling_Mode - calling mode.  Identifies which service to call
3652  |      P_All_Error_Messages - Should API return 1 error message or allow
3653  |                             calling point to get them from message stack
3654  |      P_error_code - Error code to be returned
3655  |      P_calling_sequence -  Calling sequence
3656  |
3657  |  MODIFICATION HISTORY
3658  |    DATE          Author         Action
3659  |    07-OCT-2003   SYIDNER        Created
3660  |
3661  *============================================================================*/
3662 
3663   FUNCTION Override_Recovery(
3664              P_Invoice_id              IN NUMBER,
3665              P_Calling_Mode            IN VARCHAR2,
3666              P_All_Error_Messages      IN VARCHAR2,
3667              P_Error_Code              OUT NOCOPY VARCHAR2,
3668              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
3669   IS
3670 
3671     l_debug_info                 VARCHAR2(240);
3672     l_curr_calling_sequence      VARCHAR2(4000);
3673 
3674     l_inv_header_rec             ap_invoices_all%ROWTYPE;
3675     l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
3676     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
3677 
3678     CURSOR Invoice_Header IS
3679     SELECT *
3680       FROM ap_invoices_all
3681      WHERE invoice_id = P_Invoice_Id;
3682 
3683     l_transaction_rec             zx_api_pub.transaction_rec_type;
3684 
3685     l_return_status_service       VARCHAR2(4000);
3686     l_msg_count                   NUMBER;
3687     l_msg_data                    VARCHAR2(4000);
3688     l_msg                         VARCHAR2(4000);
3689 
3690     l_return_status               BOOLEAN := TRUE;
3691     l_api_name                  varchar2(30) := 'Override_recovery';
3692 
3693   BEGIN
3694     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Override_Recovery<-' ||
3695                                P_calling_sequence;
3696 
3697     -----------------------------------------------------------------
3698     l_debug_info := 'Step 1: Populating invoice header local record';
3702     -----------------------------------------------------------------
3699     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3700        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3701     END IF;
3703 
3704     BEGIN
3705       OPEN Invoice_Header;
3706       FETCH Invoice_Header INTO l_inv_header_rec;
3707       CLOSE Invoice_Header;
3708     END;
3709 
3710     -------------------------------------------------------------------
3711     l_debug_info := 'Step 2: Get event class code';
3712     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3713        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3714     END IF;
3715     -------------------------------------------------------------------
3716 
3717     IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
3718       P_Invoice_Type_Lookup_Code => l_inv_header_rec.invoice_type_lookup_code,
3719       P_Event_Class_Code         => l_event_class_code,
3720       P_error_code               => P_error_code,
3721       P_calling_sequence         => l_curr_calling_sequence)) THEN
3722 
3723       l_return_status := FALSE;
3724 
3725     END IF;
3726 
3727     -------------------------------------------------------------------
3728     l_debug_info := 'Step 3: Get event type code';
3729     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3730        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3731     END IF;
3732     -------------------------------------------------------------------
3733     IF (l_return_status = TRUE) THEN
3734       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
3735         P_Event_Class_Code          => l_event_class_code,
3736         P_Calling_Mode              => P_Calling_Mode,
3737         P_eTax_Already_called_flag  => NULL,
3738         P_Event_Type_Code           => l_Event_Type_Code,
3739         P_Error_Code                => P_error_code,
3740         P_Calling_Sequence          => l_curr_calling_sequence)) THEN
3741 
3742         l_return_status := FALSE;
3743       END IF;
3744     END IF;
3745 /* added for 6157052 we need to populate zx_headers_gt in this case.*/
3746     IF NOT(AP_ETAX_SERVICES_PKG.Populate_Headers_GT(
3747     	      P_Invoice_Header_Rec         => l_inv_header_rec,
3748     	      P_Calling_Mode               => P_Calling_Mode,
3749     	      P_eTax_Already_called_flag   => 'Y',
3750     	      P_Event_Class_Code           => l_event_class_code,
3751     	      P_Event_Type_Code            => l_event_type_code,
3752     	      P_Error_Code                 => P_error_code,
3753 	      P_Calling_Sequence           => l_curr_calling_sequence )) THEN
3754 
3755     	  l_return_status := FALSE;
3756     END IF;
3757 
3758 --    IF (l_tax_already_calculated = 'Y') THEN
3759 --For bug 6157052 - Commented the below call as we need to call this api when we
3760 -- are freezing the distributions and after that we are not supposed to change
3761 --the distributons.But here we need to change the tax distributions as we are
3762 --changing the ecovery rate .so we need not make this callhere.
3763 /*
3764         IF NOT(AP_ETAX_SERVICES_PKG.Freeze_Distributions(
3765                 P_Invoice_Header_Rec  => l_inv_header_rec,
3766                 P_Calling_Mode        => 'FREEZE DISTRIBUTIONS',
3767                 P_Event_Class_Code    => l_event_class_code,
3768                 P_All_Error_Messages  => P_All_Error_Messages,
3769                 P_Error_Code          => P_error_code,
3770                 P_Calling_Sequence    => l_curr_calling_sequence)) THEN
3771 
3772            l_return_status := FALSE;
3773         END IF; */
3774 --    END IF;
3775 
3776     -------------------------------------------------------------------
3777     l_debug_info := 'Step 4: Populate service specific parameter';
3778     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3779        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3780     END IF;
3781     -------------------------------------------------------------------
3782     l_transaction_rec.internal_organization_id := l_inv_header_rec.org_id;
3783     l_transaction_rec.application_id           := 200;
3784     l_transaction_rec.entity_code              := 'AP_INVOICES';
3785     l_transaction_rec.event_class_code         := l_event_class_code;
3786     l_transaction_rec.event_type_code          := l_event_type_code;
3787     l_transaction_rec.trx_id                   := l_inv_header_rec.invoice_id;
3788 
3789     -------------------------------------------------------------------
3790     l_debug_info := 'Step 6: Call to override_recovery service';
3791     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3792        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3793     END IF;
3794     -------------------------------------------------------------------
3795     IF ( l_return_status = TRUE ) THEN
3796 -- Debug messages added for 6321366
3797 IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3798               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
3799               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'application_id: '           || l_transaction_rec.application_id);
3800               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'entity_code: ' || l_transaction_rec.entity_code);
3801               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: ' || l_transaction_rec.event_class_code);
3802               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_type_code: ' || l_transaction_rec.event_type_code);
3803               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '           || l_transaction_rec.trx_id);
3804 END IF;
3805 
3806       zx_api_pub.override_recovery(
3807         p_api_version        => 1.0,
3808         p_init_msg_list      => FND_API.G_TRUE,
3809         p_commit             => FND_API.G_FALSE,
3810         p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
3811         p_transaction_rec    => l_transaction_rec,
3812         x_return_status      => l_return_status_service,
3813         x_msg_count          => l_msg_count,
3814         x_msg_data           => l_msg_data);
3815 
3816     END IF;
3817 
3818     -----------------------------------------------------------------
3819     l_debug_info := 'Step 7: Verify return status';
3820     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3821        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
3822     END IF;
3823     -----------------------------------------------------------------
3824     IF (l_return_status_service = 'S') THEN
3825       -----------------------------------------------------------------
3826       l_debug_info := 'Step 8: Handle return of tax lines';
3827       -----------------------------------------------------------------
3828 -- Debug messages added for 6321366
3829       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3830          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
3831       END IF;
3832        IF NOT(AP_ETAX_SERVICES_PKG.Update_AP(
3833                 P_Invoice_header_rec => l_inv_header_rec,
3834                 P_Calling_Mode       => P_Calling_Mode,
3835                 P_All_Error_Messages => P_All_Error_Messages,
3836                 P_Error_Code         => P_error_code,
3837                 P_Calling_Sequence   => l_curr_calling_sequence)) THEN
3838 
3839           l_return_status := FALSE;
3840        END IF;
3841 
3842     ELSE -- handle errors
3843 
3844       l_return_status := FALSE;
3845       -----------------------------------------------------------------
3846       l_debug_info := 'Step 9: Handle errors returned by API';
3847       -----------------------------------------------------------------
3848       -- Debug messages added for 6321366
3849       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3850               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
3851       END IF;
3852       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
3853                P_All_Error_Messages  => P_All_Error_Messages,
3854                P_Msg_Count           => l_msg_count,
3855                P_Msg_Data            => l_msg_data,
3856                P_Error_Code          => P_Error_Code,
3857                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
3858         NULL;
3859       END IF;
3860 
3861     END IF; -- end of return_status_service
3862 
3863     RETURN l_return_status;
3864 
3865   EXCEPTION
3866     WHEN OTHERS THEN
3867       IF (SQLCODE <> -20001) THEN
3868         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
3869         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
3870         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
3871         FND_MESSAGE.SET_TOKEN('PARAMETERS',
3872           ' P_Invoice_Id = '||P_Invoice_Id||
3873           ' P_Calling_Mode ='||P_Calling_Mode||
3874           ' P_Error_Code = '||P_Error_Code||
3875           ' P_Calling_Sequence = '||P_Calling_Sequence);
3876         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
3877       END IF;
3878 
3879       APP_EXCEPTION.RAISE_EXCEPTION;
3880 
3881   END Override_Recovery;
3882 
3883 
3884 
3885 --Bug7592845
3886 /*=============================================================================
3887  |  FUNCTION - Freeze_itm_Distributions()
3888  |
3889  |  DESCRIPTION
3890  |      Public function that will call the freeze_tax_distributions service.
3891  |      This API assumes the calling code controls the commit cycle.
3892  |      This function returns TRUE if the call to the service is successful.
3893  |      Otherwise, FALSE.
3894  |
3895  |  PARAMETERS
3896  |      P_Invoice_Header_Rec - Invoice record info
3897  |      P_Calling_Mode - calling mode.  Identifies which service to call
3898  |      P_Event_Class_Code - event class code for the invoice type
3899  |      P_All_Error_Messages - Should API return 1 error message or allow
3900  |                             calling point to get them from message stack
3901  |      P_error_code - Error code to be returned
3902  |      P_calling_sequence -  Calling sequence
3903  |
3904  |  MODIFICATION HISTORY
3905  |    DATE                  Author                             Action
3906  |    11-DEC-2008   SCHITLAP/HCHAUDHA        Created
3907  |
3908  *============================================================================*/
3909 
3910 FUNCTION Freeze_itm_Distributions(
3911              P_Invoice_Header_Rec      IN ap_invoices_all%ROWTYPE,
3912              P_Calling_Mode            IN VARCHAR2,
3913              P_Event_Class_Code        IN VARCHAR2,
3914              P_All_Error_Messages      IN VARCHAR2,
3915              P_Error_Code              OUT NOCOPY VARCHAR2,
3916              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
3917 IS
3918 
3919     l_debug_info                 VARCHAR2(240);
3920     l_curr_calling_sequence      VARCHAR2(4000);
3921     l_transaction_rec            zx_api_pub.transaction_rec_type;
3922 
3923     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
3924 
3928              prepay_distribution_id,
3925     CURSOR itm_Dist IS
3926       SELECT encumbered_flag,
3927              reversal_flag,
3929              accrual_posted_flag,
3930              cash_posted_flag,
3931              posted_flag,
3932              org_id,
3933              pa_addition_flag,
3934              match_status_flag,
3935              corrected_invoice_dist_id,
3936              invoice_distribution_id,
3937              po_distribution_id,
3938              rcv_transaction_id,
3939              accounting_event_id,
3940              dist_match_type,
3941              amount,
3942              prepay_amount_remaining
3943         FROM ap_invoice_distributions_all
3944        WHERE invoice_id = p_invoice_header_rec.invoice_id
3945          AND line_type_lookup_code NOT IN ('REC_TAX', 'NONREC_TAX', 'TRV', 'TERV', 'TIPV');
3946 
3947     TYPE l_itm_dist_tab_local   IS TABLE OF itm_Dist%ROWTYPE;
3948     l_itm_dist_list_local 	l_itm_dist_tab_local;
3949 
3950     -- If related_id is equals invoice_distribution_id we are
3951     -- sure is the primary distribution created (not including variances)
3952     -- Rules applied to primary taxable distributions apply to
3953     -- related variances.
3954 
3955     l_return_status_service       VARCHAR2(4000);
3956     l_msg_count                   NUMBER;
3957     l_msg_data                    VARCHAR2(4000);
3958     l_msg                         VARCHAR2(4000);
3959 
3960     l_return_status               BOOLEAN := TRUE;
3961     l_freeze_dist_flag            VARCHAR2(1);
3962 
3963     l_po_distribution_id      ap_invoice_distributions_all.po_distribution_id%TYPE;
3964     l_rcv_transaction_id	  ap_invoice_distributions_all.rcv_transaction_id%TYPE;
3965 
3966     freeze_dist_list          ZX_API_PUB.number_tbl_type;
3967     freeze_dist_count         NUMBER := 0; --bug8302194
3968 
3969 
3970     l_api_name                CONSTANT VARCHAR2(100) := 'Freeze_Itm_Distributions';
3971 
3972     l_err varchar2(2000); --10140354
3973 
3974   BEGIN
3975     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Freeze_Itm_Distributions<-' ||
3976                                P_calling_sequence;
3977     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3978        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_curr_calling_sequence);
3979     END IF;
3980 
3981     -----------------------------------------------------------------
3982     l_debug_info := 'Step 1: Populating Item distributions collection';
3983     -----------------------------------------------------------------
3984 
3985     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
3986        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
3987     END IF;
3988 
3989 
3990 
3991     BEGIN
3992       OPEN Itm_Dist;
3993       FETCH Itm_Dist
3994       BULK COLLECT INTO l_itm_dist_list_local;
3995       CLOSE Itm_Dist;
3996     END;
3997 
3998     -------------------------------------------------------------------
3999     l_debug_info := 'Step 2: Get event type code';
4000     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4001         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4002     END IF;
4003     -------------------------------------------------------------------
4004 
4005     IF (l_return_status = TRUE) THEN
4006       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
4007         P_Event_Class_Code          => P_event_class_code,
4008         P_Calling_Mode              => P_Calling_Mode,
4009         P_eTax_Already_called_flag  => 'Y',
4010         P_Event_Type_Code           => l_Event_Type_Code,
4011         P_Error_Code                => P_error_code,
4012         P_Calling_Sequence          => l_curr_calling_sequence)) THEN
4013 
4014         l_return_status := FALSE;
4015       END IF;
4016     END IF;
4017 
4018     IF (l_itm_dist_list_local.COUNT <> 0) THEN
4019 
4020       FOR i IN l_itm_dist_list_local.FIRST..l_itm_dist_list_local.LAST LOOP
4021         -- set l_freeze_dist_flag to N to initiate process
4022         l_freeze_dist_flag := 'N';
4023 
4024         -- Rules for distributions
4025         -----------------------------------------------------------------
4026         l_debug_info := 'Step 3: Item distribution is encumbered';
4027         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4028            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4029         END IF;
4030         -----------------------------------------------------------------
4031         IF (NVL(l_itm_dist_list_local(i).encumbered_flag, 'N') IN ('Y','D','W','X')) THEN
4032 
4033           -- possible values verified for encumbered_flag
4034           -- Y: Regular line, has already been successfully encumbered by AP.
4035           -- D: Same as Y for reversal distribution line.
4036           -- W: Regular line, has been encumbered in advisory mode even though
4037           --    insufficient funds existed.
4038           -- X: Same as W for reversal distribution line.
4039 
4040           l_freeze_dist_flag := 'Y';
4041 
4042         END IF;
4043         -----------------------------------------------------------------
4044         l_debug_info := 'Step 4: Item distribution is part of a reversal pair';
4045         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4046            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4047         END IF;
4048         -----------------------------------------------------------------
4049         IF (l_freeze_dist_flag = 'N' AND
4050             l_itm_dist_list_local(i).reversal_flag = 'Y') THEN
4051 
4052           l_freeze_dist_flag := 'Y';
4053 
4054         END IF;
4055 
4056         -----------------------------------------------------------------
4057         l_debug_info := 'Step 5: Item distribution is PO/RCV matched';
4058         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4059            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4060         END IF;
4061         -----------------------------------------------------------------
4062         IF (l_freeze_dist_flag = 'N' AND
4063            (l_itm_dist_list_local(i).po_distribution_id IS NOT NULL
4064             OR l_itm_dist_list_local(i).rcv_transaction_id IS NOT NULL)) THEN
4065 
4066             l_freeze_dist_flag := 'Y';
4067         END IF;
4068 
4069         -----------------------------------------------------------------
4070         l_debug_info := 'Step 6: Item distribution is prepayment '||
4071                         'application/unapplication';
4072         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4073            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4074         END IF;
4075         -----------------------------------------------------------------
4076 
4077         -- For tax distributions created based on a PREPAY distribution
4078         -- (parent dist) the prepay_distribution_id will be always populated
4079         -- with the prepay_distribution_id of the parent (PREPAY) dist.
4080 
4081         IF (l_freeze_dist_flag = 'N' AND
4082             l_itm_dist_list_local(i).prepay_distribution_id IS NOT NULL) THEN
4083 
4084           l_freeze_dist_flag := 'Y';
4085 
4086         END IF;
4087 
4088         -----------------------------------------------------------------
4089         l_debug_info := 'Step 7: Item distribution if partially of fully '||
4090                         'accounted';
4091         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4092            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4093         END IF;
4094         -----------------------------------------------------------------
4095         IF (l_freeze_dist_flag = 'N' ) THEN
4096            IF ( ap_invoice_distributions_pkg.Get_Posted_Status(
4097              X_Accrual_Posted_Flag => l_itm_dist_list_local(i).accrual_posted_flag,
4098              X_Cash_Posted_Flag    => l_itm_dist_list_local(i).cash_posted_flag,
4099              X_Posted_Flag         => l_itm_dist_list_local(i).posted_flag,
4100              X_Org_Id              => l_itm_dist_list_local(i).org_id) <> 'N') THEN
4101              l_freeze_dist_flag := 'Y';
4102            END IF;
4103         END IF;
4104 
4105         -----------------------------------------------------------------
4106         l_debug_info := 'Step 8: Item distribution is transferred to projects';
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 
4112         IF (l_freeze_dist_flag = 'N' ) THEN
4113            IF (NVL(l_itm_dist_list_local(i).pa_addition_flag,'N') NOT IN ('N', 'E')) THEN
4114              -- N means not yet transfer to projects
4115              -- E means not project related.
4116              -- If the flag is Y or any rejection code, it is assumed to be
4117              -- transfered
4118 
4119              l_freeze_dist_flag := 'Y';
4120            END IF;
4121         END IF;
4122 
4123         -----------------------------------------------------------------
4124         l_debug_info := 'Step 9: Item distribution has been validated';
4125         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4126            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4127         END IF;
4128         -----------------------------------------------------------------
4129 
4130         IF (l_freeze_dist_flag = 'N' AND
4134         END IF;
4131            NVL(l_itm_dist_list_local(i).match_status_flag,'N') IN ('T', 'A')) THEN
4132 
4133           l_freeze_dist_flag := 'Y';
4135 
4136         -----------------------------------------------------------------
4137         l_debug_info := 'Step 9.1: accounting_event_id is stamped';
4138         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4139            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4140         END IF;
4141         -----------------------------------------------------------------
4142 
4143         IF (l_freeze_dist_flag = 'N' AND
4144             l_itm_dist_list_local(i).accounting_event_id IS NOT NULL) THEN
4145 
4146           l_freeze_dist_flag := 'Y';
4147         END IF;
4148 
4149         -----------------------------------------------------------------
4150         l_debug_info := 'Step 10: Item distribution is a part of '||
4151                         'prepayment and has been partially or fully applied';
4152         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4153            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4154         END IF;
4155         -----------------------------------------------------------------
4156 
4157             -- This validation was analized and it is not included since the
4158             -- this API will not be called after the prepayment is
4159             -- partiall or fully applied.  The prepayment invoice cannot be
4160             -- modified after it is partially or fully applied.
4161 
4162          IF(l_freeze_dist_flag = 'N' AND
4163             P_Invoice_Header_Rec.invoice_type_lookup_code='PREPAYMENT' AND
4164             l_itm_dist_list_local(i).amount <>l_itm_dist_list_local(i).prepay_amount_remaining) THEN
4165 
4166             l_freeze_dist_flag := 'Y';
4167          END IF;
4168 
4169          -----------------------------------------------------------------
4170         l_debug_info := 'Step 12: Check if the parent item line has been '||
4171                         'adjusted by a PO price Adjustment or it itself an '||
4172                         'adjustment';
4173         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4174            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4175         END IF;
4176         -----------------------------------------------------------------
4177         IF (l_freeze_dist_flag = 'N' AND
4178             l_itm_dist_list_local(i).dist_match_type IN
4179             ('ADJUSTMENT_CORRECTION','PO_PRICE_ADJUSTMENT')) THEN
4180             l_freeze_dist_flag := 'Y';
4181         END IF;
4182 
4183         -----------------------------------------------------------------
4184         l_debug_info := 'Step 13: Item line is a corrected one ';
4185         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4186            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4187         END IF;
4188         -----------------------------------------------------------------
4189         -- This rule can be applied at distribution level since the
4190         -- corrected_invoice_dist_id is populated if it corrects any other
4191         -- distribution.
4192         IF (l_freeze_dist_flag = 'N' AND
4193             l_itm_dist_list_local(i).corrected_invoice_dist_id IS NOT NULL) THEN
4194           l_freeze_dist_flag := 'Y';
4195         END IF;
4196 
4197         l_freeze_dist_flag := 'Y'; --Bug9021265
4198         --Setting the Freeze Flag Y by defauly as per discussion
4199         --Himesh,Atul,Venkat,Kiran,Ranjith,Taniya
4200         -----------------------------------------------------------------
4201         l_debug_info := ' Inv Dist ID: '||l_itm_dist_list_local(i).invoice_distribution_id||
4202                         ' Freeze Flag: '||l_freeze_dist_flag;
4203         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4204             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4205         END IF;
4206         -----------------------------------------------------------------
4207 
4208         IF (l_freeze_dist_flag = 'Y') THEN
4209 	  freeze_dist_count := freeze_dist_count + 1; --bug 8302194
4210 
4211            freeze_dist_list(freeze_dist_count) := l_itm_dist_list_local(i).invoice_distribution_id;
4212 
4213            l_debug_info := ' Frozen Dist Id '||freeze_dist_list(freeze_dist_count);
4214 
4215            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4216                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4217            END IF;
4218 
4219         END IF;
4220 
4221       END LOOP;
4222     END IF;
4223 
4224     l_transaction_rec.internal_organization_id := P_Invoice_Header_Rec.org_id;
4225     l_transaction_rec.application_id           := 200;
4226     l_transaction_rec.entity_code              := 'AP_INVOICES';
4227     l_transaction_rec.event_class_code         := P_event_class_code;
4228     l_transaction_rec.event_type_code          := l_event_type_code;
4229     l_transaction_rec.trx_id                   := P_Invoice_Header_Rec.invoice_id;
4230 
4231 
4232     -----------------------------------------------------------------
4233     l_debug_info := 'Step 18: Call Freeze_tax_distributions service';
4234     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4235         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4236     END IF;
4237     -----------------------------------------------------------------
4238     IF ( l_return_status = TRUE AND freeze_dist_list.count>0) THEN
4239 
4240      zx_new_services_pkg.freeze_tax_dists_for_items(
4241         p_api_version          => 1.0,
4245         x_return_status        => l_return_status_service,
4242         p_init_msg_list        => FND_API.G_TRUE,
4243         p_commit               => FND_API.G_FALSE,
4244         p_validation_level     => FND_API.G_VALID_LEVEL_FULL,
4246         x_msg_count            => l_msg_count,
4247         x_msg_data             => l_msg_data,
4248         p_transaction_rec      => l_transaction_rec,
4249         p_trx_line_dist_id_tbl => freeze_dist_list);
4250 
4251     END IF;
4252 
4253 
4254       -----------------------------------------------------------------
4255       l_debug_info := 'Step 19: Verify return status';
4256       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4257          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4258       END IF;
4259       -----------------------------------------------------------------
4260     IF (l_return_status_service <> 'S') THEN  -- handle errors
4261 
4262       l_return_status := FALSE;
4263       -----------------------------------------------------------------
4264       l_debug_info := 'Step 20: Handle errors returned by API';
4265       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4266          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4267       END IF;
4268       -----------------------------------------------------------------
4269 
4270       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
4271                P_All_Error_Messages  => P_All_Error_Messages,
4272                P_Msg_Count           => l_msg_count,
4273                P_Msg_Data            => l_msg_data,
4274                P_Error_Code          => P_Error_Code,
4275                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
4276         NULL;
4277       END IF;
4278 
4279     END IF;
4280 
4281   RETURN l_return_status;
4282 
4283 EXCEPTION
4284     WHEN OTHERS THEN
4285       IF (SQLCODE <> -20001) THEN
4286         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
4287         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
4288         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
4289         FND_MESSAGE.SET_TOKEN('PARAMETERS',
4290           ' P_Invoice_Id = '||P_Invoice_Header_Rec.Invoice_id||
4291           ' P_Calling_Mode ='||P_Calling_Mode||
4292           ' P_Error_Code = '||P_Error_Code||
4293           ' P_Calling_Sequence = '||P_Calling_Sequence);
4294         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
4295       END IF;
4296 
4297       IF ( Itm_Dist%ISOPEN ) THEN
4298         CLOSE Itm_Dist;
4299       END IF;
4300 
4301       --bug10140354
4302       IF AP_APPROVAL_PKG.G_VALIDATION_REQUEST_ID IS NULL THEN
4303         APP_EXCEPTION.RAISE_EXCEPTION;
4304     ELSE
4305     l_err:= SQLERRM;
4306     insert into ap_errors_gt(invoice_id , message_text) values (P_Invoice_Header_Rec.Invoice_id , l_err);
4307     RETURN FALSE;
4308 END IF;
4309 
4310 END Freeze_itm_Distributions;
4311 --Bug7592845
4312 
4313 /*=============================================================================
4314  |  FUNCTION - Freeze_Distributions()
4315  |
4316  |  DESCRIPTION
4317  |      Public function that will call the freeze_tax_distributions service.
4318  |      This API assumes the calling code controls the commit cycle.
4319  |      This function returns TRUE if the call to the service is successful.
4320  |      Otherwise, FALSE.
4321  |
4322  |  PARAMETERS
4323  |      P_Invoice_Header_Rec - Invoice record info
4324  |      P_Calling_Mode - calling mode.  Identifies which service to call
4325  |      P_Event_Class_Code - event class code for the invoice type
4326  |      P_All_Error_Messages - Should API return 1 error message or allow
4327  |                             calling point to get them from message stack
4328  |      P_error_code - Error code to be returned
4329  |      P_calling_sequence -  Calling sequence
4330  |
4331  |  MODIFICATION HISTORY
4332  |    DATE          Author         Action
4333  |    07-OCT-2003   SYIDNER        Created
4334  |
4335  *============================================================================*/
4336 
4337   FUNCTION Freeze_Distributions(
4338              P_Invoice_Header_Rec      IN ap_invoices_all%ROWTYPE,
4339              P_Calling_Mode            IN VARCHAR2,
4340              P_Event_Class_Code        IN VARCHAR2,
4341              P_All_Error_Messages      IN VARCHAR2,
4342              P_Error_Code              OUT NOCOPY VARCHAR2,
4343              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
4344   IS
4345 
4346     l_debug_info                 VARCHAR2(240);
4347     l_curr_calling_sequence      VARCHAR2(4000);
4348     l_transaction_rec            zx_api_pub.transaction_rec_type;
4349 
4350     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
4351 
4352     CURSOR Tax_Dist IS
4353     (SELECT encumbered_flag,
4354             reversal_flag,
4355             charge_applicable_to_dist_id,
4356             prepay_distribution_id,
4357             accrual_posted_flag,
4358             cash_posted_flag,
4359             posted_flag,
4360             org_id,
4361             pa_addition_flag,
4362             match_status_flag,
4363             corrected_invoice_dist_id,
4364             invoice_distribution_id,
4365             detail_tax_dist_id,
4366             accounting_event_id
4367        FROM ap_invoice_distributions_all
4368       WHERE invoice_id = p_invoice_header_rec.invoice_id
4369         AND line_type_lookup_code IN ('REC_TAX', 'NONREC_TAX', 'TRV', 'TERV', 'TIPV')
4370         AND (related_id IS NULL
4371                   OR (related_id = invoice_distribution_id
4372                       OR (related_id IS NOT NULL
4373                           AND line_type_lookup_code IN ('TRV', 'TERV', 'TIPV'))))
4374       UNION ALL
4375       SELECT encumbered_flag,
4376              reversal_flag,
4377              charge_applicable_to_dist_id,
4378              prepay_distribution_id,
4379              accrual_posted_flag,
4380              cash_posted_flag,
4381              posted_flag,
4382              org_id,
4383              pa_addition_flag,
4384              match_status_flag,
4385              corrected_invoice_dist_id,
4386              invoice_distribution_id,
4387              detail_tax_dist_id,
4388              accounting_event_id
4389         FROM ap_self_assessed_tax_dist_all
4390        WHERE invoice_id = p_invoice_header_rec.invoice_id
4391          AND line_type_lookup_code IN ('REC_TAX', 'NONREC_TAX', 'TRV', 'TERV', 'TIPV')
4392          AND (related_id IS NULL
4393                   OR (related_id = invoice_distribution_id
4394                       OR (related_id IS NOT NULL
4395                           AND line_type_lookup_code IN ('TRV', 'TERV', 'TIPV')))));
4396 
4397 
4398     TYPE l_tax_dist_tab_local   IS TABLE OF Tax_Dist%ROWTYPE;
4399     l_tax_dist_list_local 	l_tax_dist_tab_local;
4400 
4401     -- If related_id is equals invoice_distribution_id we are
4402     -- sure is the primary distribution created (not including variances)
4403     -- Rules applied to primary taxable distributions apply to
4404     -- related variances.
4405 
4406     l_return_status_service       VARCHAR2(4000);
4407     l_msg_count                   NUMBER;
4408     l_msg_data                    VARCHAR2(4000);
4409     l_msg                         VARCHAR2(4000);
4410 
4411     l_return_status               BOOLEAN := TRUE;
4412     l_freeze_dist_flag            VARCHAR2(1);
4413 
4414     l_po_distribution_id          ap_invoice_distributions_all.po_distribution_id%TYPE;
4415     l_rcv_transaction_id          ap_invoice_distributions_all.rcv_transaction_id%TYPE;
4416 
4417     TYPE freeze_tax_dist_type IS TABLE OF zx_tax_dist_id_gt%ROWTYPE;
4418     freeze_dist_list          freeze_tax_dist_type := freeze_tax_dist_type();
4419 
4420     l_api_name                    CONSTANT VARCHAR2(100) := 'Freeze_Distributions';
4421 
4422   BEGIN
4423     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Freeze_Distributions<-' ||
4424                                P_calling_sequence;
4425 
4426     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4427        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_curr_calling_sequence);
4428     END IF;
4429 
4430     -----------------------------------------------------------------
4431     l_debug_info := 'Step 1: Populating tax distributions collection';
4432     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4433        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4434     END IF;
4435     -----------------------------------------------------------------
4436 
4437     BEGIN
4438       OPEN Tax_Dist;
4439       FETCH Tax_Dist
4440       BULK COLLECT INTO l_tax_dist_list_local;
4441       CLOSE Tax_Dist;
4442     END;
4443 
4444     -------------------------------------------------------------------
4445     l_debug_info := 'Step 2: Get event type code';
4446     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4447        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4448     END IF;
4449     -------------------------------------------------------------------
4450 
4451     IF (l_return_status = TRUE) THEN
4452       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
4453         P_Event_Class_Code          => P_event_class_code,
4454         P_Calling_Mode              => P_Calling_Mode,
4455         P_eTax_Already_called_flag  => 'Y',
4456         P_Event_Type_Code           => l_Event_Type_Code,
4457         P_Error_Code                => P_error_code,
4458         P_Calling_Sequence          => l_curr_calling_sequence)) THEN
4459 
4460         l_return_status := FALSE;
4461       END IF;
4462     END IF;
4463 
4464     IF (l_tax_dist_list_local.COUNT <> 0) THEN
4465       -- Initialize freeze_dist_list collection
4466       freeze_dist_list.EXTEND(l_tax_dist_list_local.COUNT);
4467 
4468       FOR i IN l_tax_dist_list_local.FIRST..l_tax_dist_list_local.LAST LOOP
4469         -- set l_freeze_dist_flag to N to initiate process
4470         l_freeze_dist_flag := 'N';
4471 
4472         -- Rules for distributions
4473         -----------------------------------------------------------------
4474         l_debug_info := 'Step 3: Tax distribution is encumbered';
4475         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4476            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4477         END IF;
4478         -----------------------------------------------------------------
4479         IF (NVL(l_tax_dist_list_local(i).encumbered_flag, 'N') IN ('Y','D','W','X')) THEN
4480           -- possible values verified for encumbered_flag
4481           -- Y: Regular line, has already been successfully encumbered by AP.
4482           -- D: Same as Y for reversal distribution line.
4483           -- W: Regular line, has been encumbered in advisory mode even though
4484           --    insufficient funds existed.
4485           -- X: Same as W for reversal distribution line.
4486 
4487           l_freeze_dist_flag := 'Y';
4488 
4489         END IF;
4490         -----------------------------------------------------------------
4491         l_debug_info := 'Step 4: Tax distribution is part of a reversal pair';
4492         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4493            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4494         END IF;
4495         -----------------------------------------------------------------
4496         IF (l_freeze_dist_flag = 'N' AND
4497             l_tax_dist_list_local(i).reversal_flag = 'Y') THEN
4498 
4499           l_freeze_dist_flag := 'Y';
4500 
4501         END IF;
4502 
4503         -----------------------------------------------------------------
4504         l_debug_info := 'Step 5: Parent Item distribution is PO/RCV matched';
4505         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4506            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4507         END IF;
4508         -----------------------------------------------------------------
4509         IF (l_freeze_dist_flag = 'N' AND
4510            (l_tax_dist_list_local(i).charge_applicable_to_dist_id IS NOT NULL
4511             and l_tax_dist_list_local(i).charge_applicable_to_dist_id <>-99)) THEN
4512            /* for bug 6010950 added 'charge_applicable_to_dist_id<>-99 condition'.
4513               As for Tax only invoices which are not receipt matched below
4514               select should not fire.I found out that value of charge_applicable_to_dist_id is
4515               -99 in this case.So aaded the AND condition to avoid the select.  */
4516             --this validation because it can be null for tax-only lines
4517 
4518            SELECT po_distribution_id, rcv_transaction_id
4519              INTO l_po_distribution_id, l_rcv_transaction_id
4520              FROM ap_invoice_distributions_all
4521             WHERE invoice_distribution_id =
4522                   l_tax_dist_list_local(i).charge_applicable_to_dist_id;
4523 
4524 
4525            IF ( l_po_distribution_id IS NOT NULL OR
4526               l_rcv_transaction_id IS NOT NULL ) THEN
4527               l_freeze_dist_flag := 'Y';
4528 
4529            END IF;
4530         END IF;
4531 
4532         -----------------------------------------------------------------
4533         l_debug_info := 'Step 6: Parent Item distribution is prepayment '||
4534                         'application/unapplication';
4535         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4536            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4537         END IF;
4538         -----------------------------------------------------------------
4539         -- For tax distributions created based on a PREPAY distribution
4540         -- (parent dist) the prepay_distribution_id will be always populated
4541         -- with the prepay_distribution_id of the parent (PREPAY) dist.
4542         IF (l_freeze_dist_flag = 'N' AND
4543             l_tax_dist_list_local(i).prepay_distribution_id IS NOT NULL) THEN
4544 
4545           l_freeze_dist_flag := 'Y';
4546 
4547         END IF;
4548 
4549         -----------------------------------------------------------------
4550         l_debug_info := 'Step 7: Tax distribution if partially of fully '||
4551                         'accounted';
4552         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4553            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4554         END IF;
4555         -----------------------------------------------------------------
4556         IF (l_freeze_dist_flag = 'N' ) THEN
4557            IF ( ap_invoice_distributions_pkg.Get_Posted_Status(
4558              X_Accrual_Posted_Flag => l_tax_dist_list_local(i).accrual_posted_flag,
4559              X_Cash_Posted_Flag    => l_tax_dist_list_local(i).cash_posted_flag,
4560              X_Posted_Flag         => l_tax_dist_list_local(i).posted_flag,
4561              X_Org_Id              => l_tax_dist_list_local(i).org_id) <> 'N') THEN
4562              l_freeze_dist_flag := 'Y';
4563            END IF;
4564         END IF;
4565 
4566         -----------------------------------------------------------------
4567         l_debug_info := 'Step 8: Tax distribution is transferred to projects';
4568         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4569            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4570         END IF;
4571         -----------------------------------------------------------------
4572         IF (l_freeze_dist_flag = 'N' ) THEN
4573            IF (NVL(l_tax_dist_list_local(i).pa_addition_flag,'N') NOT IN ('N', 'E')) THEN
4574              -- N means not yet transfer to projects
4575              -- E means not project related.
4576              -- If the flag is Y or any rejection code, it is assumed to be
4577              -- transfered
4578 
4579              l_freeze_dist_flag := 'Y';
4580            END IF;
4581         END IF;
4582 
4583         -----------------------------------------------------------------
4584         l_debug_info := 'Step 9: Tax distribution has been validated';
4585         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4586            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4587         END IF;
4588         -----------------------------------------------------------------
4589         IF (l_freeze_dist_flag = 'N' AND
4590            NVL(l_tax_dist_list_local(i).match_status_flag,'N') IN ('T', 'A')) THEN
4591 
4592           l_freeze_dist_flag := 'Y';
4593         END IF;
4594 
4595         -----------------------------------------------------------------
4596         l_debug_info := 'Step 9.1: accounting_event_id is stamped';
4597         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4598            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4599         END IF;
4600         -----------------------------------------------------------------
4601         IF (l_freeze_dist_flag = 'N' AND
4602             l_tax_dist_list_local(i).accounting_event_id IS NOT NULL) THEN
4603 
4604           l_freeze_dist_flag := 'Y';
4605         END IF;
4606 
4607         -----------------------------------------------------------------
4608         l_debug_info := 'Step 10: Parent Item distribution is a part of '||
4609                         'prepayment and has been partially or fully applied';
4610         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4611            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4612         END IF;
4613         -----------------------------------------------------------------
4614             -- This validation was analized and it is not included since the
4615             -- this API will not be called after the prepayment is
4616             -- partiall or fully applied.  The prepayment invoice cannot be
4617             -- modified after it is partially or fully applied.
4618 
4619 
4620         -- Rules for lines
4621         -----------------------------------------------------------------
4622         l_debug_info := 'Step 11: Check if tax line has been discarded';
4623         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4624            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4625         END IF;
4626         -----------------------------------------------------------------
4627         -- This validation is included in the reversal of the distributions.
4628         -- when the line is discarded the distributions are reversed.
4629 
4630         -----------------------------------------------------------------
4631         l_debug_info := 'Step 12: Check if the parent item line has been '||
4632                         'adjusted by a PO price Adjustment or it itself an '||
4633                         'adjustment';
4634         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4635            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4636         END IF;
4637         -----------------------------------------------------------------
4638         --YIDSAL.  This validation will be included when the retro pricing code
4639         -- is included in 11iX
4640 
4641         -----------------------------------------------------------------
4642         l_debug_info := 'Step 13: Parent Item line is a corrected one or is '||
4643                         'itself a correction';
4644         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4645            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4646         END IF;
4647         -----------------------------------------------------------------
4648         -- This rule can be applied at distribution level since the
4649         -- corrected_invoice_dist_id is populated if it corrects any other
4650         -- distribution.
4651         IF (l_freeze_dist_flag = 'N' AND
4652             l_tax_dist_list_local(i).corrected_invoice_dist_id IS NOT NULL) THEN
4653 
4654           l_freeze_dist_flag := 'Y';
4655         END IF;
4656 
4657         -- To know if the distribution is a corrected one
4658         -- Verify if the parent item (taxable) distribution is corrected.
4659         IF (l_freeze_dist_flag = 'N' ) THEN
4660           BEGIN
4661             SELECT 'Y'
4662               INTO l_freeze_dist_flag
4663               FROM ap_invoice_distributions_all
4664              WHERE corrected_invoice_dist_id =
4665                    l_tax_dist_list_local(i).charge_applicable_to_dist_id
4666                AND ROWNUM = 1;
4667 
4668           EXCEPTION
4669             WHEN NO_DATA_FOUND THEN NULL;
4670           END;
4671         END IF;
4672 
4673         -----------------------------------------------------------------
4674         l_debug_info := 'Step 14: Parent item line is a prepayment application'||
4675                         '/unapplication';
4676         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4677            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4678         END IF;
4679         -----------------------------------------------------------------
4680         -- this validation is included in the distribution validations for the
4681         -- parent item distribution
4682 
4683         -----------------------------------------------------------------
4684         l_debug_info := ' Inv Dist ID: '||l_tax_dist_list_local(i).invoice_distribution_id||
4685                         ' Freeze Flag: '||l_freeze_dist_flag;
4686         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4687             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4688         END IF;
4689         -----------------------------------------------------------------
4690 
4691         IF (l_freeze_dist_flag = 'Y') THEN
4692 
4693            freeze_dist_list(i).tax_dist_id := l_tax_dist_list_local(i).detail_tax_dist_id;
4694 
4695            l_debug_info := ' Frozen Dist Id '||freeze_dist_list(i).tax_dist_id;
4696 
4697            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4698                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4699            END IF;
4700 
4701         END IF;
4702 
4703       END LOOP;
4704     END IF;
4705 
4706     delete zx_tax_dist_id_gt;--Bug7582775
4707 
4708     l_debug_info := 'No Of Rows Deleted From zx_tax_dist_id_gt  '||sql%rowcount;
4709 
4710     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4711        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4712     END IF;
4713 
4714     FOR m IN NVL(freeze_dist_list.FIRST,0)..NVL(freeze_dist_list.LAST,0)
4715     LOOP
4716       IF (freeze_dist_list.exists(m)) THEN
4717 
4718         IF (freeze_dist_list(m).tax_dist_id IS NOT NULL) THEN
4719           INSERT INTO zx_tax_dist_id_gt(tax_dist_id)
4720           VALUES (freeze_dist_list(m).tax_dist_id);
4721 
4722         END IF;
4723       END IF;
4724     END LOOP;
4725 
4726     l_transaction_rec.internal_organization_id := P_Invoice_Header_Rec.org_id;
4727     l_transaction_rec.application_id           := 200;
4728     l_transaction_rec.entity_code              := 'AP_INVOICES';
4729     l_transaction_rec.event_class_code         := P_event_class_code;
4730     l_transaction_rec.event_type_code          := l_event_type_code;
4731     l_transaction_rec.trx_id                   := P_Invoice_Header_Rec.invoice_id;
4732 
4733     -----------------------------------------------------------------
4734     l_debug_info := 'Step 18: Call Freeze_tax_distributions service';
4735     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4736        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4737     END IF;
4738     -----------------------------------------------------------------
4739     IF ( l_return_status = TRUE AND freeze_dist_list.count>0) THEN --Bug7582775
4740 
4741      zx_api_pub.freeze_tax_distributions(
4742         p_api_version        => 1.0,
4743         p_init_msg_list      => FND_API.G_TRUE,
4744         p_commit             => FND_API.G_FALSE,
4745         p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
4746         x_return_status      => l_return_status_service,
4747         x_msg_count          => l_msg_count,
4748         x_msg_data           => l_msg_data,
4749         p_transaction_rec    => l_transaction_rec);
4750 
4751     END IF;
4752 
4753     -----------------------------------------------------------------
4754     l_debug_info := 'Step 19: Verify return status';
4755     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4756        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4757     END IF;
4758     -----------------------------------------------------------------
4759     IF (l_return_status_service <> 'S') THEN  -- handle errors
4760 
4761       l_return_status := FALSE;
4762       -----------------------------------------------------------------
4763       l_debug_info := 'Step 20: Handle errors returned by API';
4764       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4765          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
4766       END IF;
4767       -----------------------------------------------------------------
4768 
4769       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
4770                P_All_Error_Messages  => P_All_Error_Messages,
4771                P_Msg_Count           => l_msg_count,
4772                P_Msg_Data            => l_msg_data,
4773                P_Error_Code          => P_Error_Code,
4774                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
4775         NULL;
4776       END IF;
4777 
4778     END IF;
4779 
4780   RETURN l_return_status;
4781 
4782   EXCEPTION
4783     WHEN OTHERS THEN
4784       IF (SQLCODE <> -20001) THEN
4785         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
4786         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
4787         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
4788         FND_MESSAGE.SET_TOKEN('PARAMETERS',
4789           ' P_Invoice_Id = '||P_Invoice_Header_Rec.Invoice_id||
4790           ' P_Calling_Mode ='||P_Calling_Mode||
4791           ' P_Error_Code = '||P_Error_Code||
4792           ' P_Calling_Sequence = '||P_Calling_Sequence);
4793         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
4794       END IF;
4795 
4796       IF ( tax_dist%ISOPEN ) THEN
4797         CLOSE tax_dist;
4798       END IF;
4799 
4800       APP_EXCEPTION.RAISE_EXCEPTION;
4801 
4802   END Freeze_Distributions;
4803 
4804 /*=============================================================================
4805  |  FUNCTION - Global_Document_Update()
4806  |
4807  |  DESCRIPTION
4808  |      Public function that will call the global_document_update service to
4809  |      inform eTax of a cancellation of an invoice, the freeze after the
4810  |      invoice is validated (meaning is ready to reporting), the unfreeze
4811  |      of an invoice because it has to be modified after it was validated, and
4812  |      the release of tax holds by the user.
4813  |      This API assumes the calling code controls the commit cycle.
4814  |      This function returns TRUE if the call to the service is successful.
4815  |      Otherwise, FALSE.
4816  |
4817  |  PARAMETERS
4818  |      P_Invoice_Id - invoice id
4819  |      P_Calling_Mode - calling mode.  Identifies which service to call
4820  |      P_All_Error_Messages - Should API return 1 error message or allow
4821  |                             calling point to get them from message stack
4822  |      P_error_code - Error code to be returned
4823  |      P_calling_sequence -  Calling sequence
4824  |
4825  |  MODIFICATION HISTORY
4826  |    DATE          Author         Action
4827  |    07-OCT-2003   SYIDNER        Created
4828  |
4829  *============================================================================*/
4830 
4831   FUNCTION Global_Document_Update(
4832              P_Invoice_id              IN NUMBER,
4833 	     P_Line_Number	       IN NUMBER DEFAULT NULL,
4834              P_Calling_Mode            IN VARCHAR2,
4835              P_All_Error_Messages      IN VARCHAR2,
4836              P_Error_Code              OUT NOCOPY VARCHAR2,
4837              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
4838   IS
4839 
4840     l_debug_info                 VARCHAR2(240);
4841     l_curr_calling_sequence      VARCHAR2(4000);
4842 
4843     l_inv_header_rec             ap_invoices_all%ROWTYPE;
4844     l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
4845     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
4846 
4847     CURSOR Invoice_Header IS
4848     SELECT *
4849       FROM ap_invoices_all
4850      WHERE invoice_id = P_Invoice_Id;
4851 
4852     l_transaction_rec             zx_api_pub.transaction_rec_type;
4853 
4854     l_return_status_service       VARCHAR2(4000);
4855     l_msg_count                   NUMBER;
4856     l_msg_data                    VARCHAR2(4000);
4857     l_msg                         VARCHAR2(4000);
4858 
4859     l_success			  BOOLEAN;
4860     Tax_Exception		  EXCEPTION;
4861     l_return_status               BOOLEAN := TRUE;
4862     l_api_name                  VARCHAR2(30) := 'global_document_update'; -- bug 6321366
4863   BEGIN
4864 
4865     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Global_Document_Update<-' ||
4866                                P_calling_sequence;
4867 
4868     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4869        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_curr_calling_sequence);
4870     END IF;
4871 
4872     --Bug8811102
4873     IF P_Calling_Mode IN('CANCEL INVOICE','DISCARD LINE','UNAPPLY PREPAY') THEN
4874         --Bug6799496
4875         --IF control_amount (Control Tax amount is non zero then invoice cancellation
4876         --errors out. Hence making it 0 before we populate headers_gt (zx table)
4877         -------------------------------------------------------------------
4878         l_debug_info := 'If CANCEL INVOICE Then Make Control Amount 0';
4879         -------------------------------------------------------------------
4880         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4881            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
4882         END IF;
4883 
4884         UPDATE ap_invoices_all ail
4885            SET control_amount= NULL --Bug6887264
4886          WHERE ail.invoice_id = p_invoice_id
4887            AND P_Calling_Mode = 'CANCEL INVOICE'
4888            AND ail.control_amount IS NOT NULL ;
4889            --bug 6845888
4890            --Bug6799496
4891 
4892         UPDATE ap_invoice_lines_all ail
4893           SET control_amount= NULL --Bug6887264
4894         WHERE ail.invoice_id = p_invoice_id
4895           AND P_Calling_Mode = 'CANCEL INVOICE'
4896           AND ail.control_amount IS NOT NULL ;
4897 
4898 
4899 
4900 
4901         IF NOT cancel_invoice
4902             (p_invoice_id   => p_invoice_id,
4903 			 p_line_number  => p_line_number,
4904 			 p_calling_mode => P_Calling_Mode) THEN
4905 
4906            l_return_status := FALSE;
4907         END IF;
4908     END IF;
4909     --Bug8811102
4910 
4911     RETURN l_return_status;
4912 
4913   EXCEPTION
4914     WHEN OTHERS THEN
4915       IF (SQLCODE <> -20001) THEN
4916         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
4917         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
4918         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
4919         FND_MESSAGE.SET_TOKEN('PARAMETERS',
4920           ' P_Invoice_Id = '||P_Invoice_Id||
4921           ' P_Calling_Mode ='||P_Calling_Mode||
4922           ' P_Error_Code = '||P_Error_Code||
4923           ' P_Calling_Sequence = '||P_Calling_Sequence);
4924         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
4925       END IF;
4926 
4927       APP_EXCEPTION.RAISE_EXCEPTION;
4928 
4929   END Global_Document_Update;
4930 
4931 
4932 /*=============================================================================
4933  |  FUNCTION - Release_Tax_Holds()
4934  |
4935  |  DESCRIPTION
4936  |      Public function that will call the global_document_update service to
4937  |      inform eTax the release of tax holds by the user.
4938  |      This function returns TRUE if the call to the service is successful.
4939  |      Otherwise, FALSE.
4940  |
4941  |  PARAMETERS
4942  |      P_Invoice_Id - invoice id
4943  |      P_Calling_Mode - calling mode.  Identifies which service to call
4944  |      P_Tax_Hold_Code - List of tax hold codes released in AP
4945  |                        Posible values: TAX VARIANCE and TAX AMOUNT RANGE
4946  |      P_All_Error_Messages - Should API return 1 error message or allow
4947  |                             calling point to get them from message stack
4948  |      P_error_code - Error code to be returned
4949  |      P_calling_sequence -  Calling sequence
4950  |
4951  |  MODIFICATION HISTORY
4952  |    DATE          Author         Action
4953  |    05-NOV-2003   SYIDNER        Created
4954  |
4955  *============================================================================*/
4956 
4957   FUNCTION Release_Tax_Holds(
4958              P_Invoice_id              IN NUMBER,
4959              P_Calling_Mode            IN VARCHAR2,
4960              P_Tax_Hold_Code           IN Rel_Hold_Codes_Type,
4961              P_All_Error_Messages      IN VARCHAR2,
4962              P_Error_Code              OUT NOCOPY VARCHAR2,
4963              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
4964   IS
4965 
4966     l_debug_info                 VARCHAR2(240);
4967     l_curr_calling_sequence      VARCHAR2(4000);
4968 
4969     l_inv_header_rec             ap_invoices_all%ROWTYPE;
4970     l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
4971     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
4972 
4973     CURSOR Invoice_Header IS
4974     SELECT *
4975       FROM ap_invoices_all
4976      WHERE invoice_id = P_Invoice_Id;
4977 
4978     l_transaction_rec             zx_api_pub.transaction_rec_type;
4979     l_validation_status           zx_api_pub.validation_status_tbl_type;
4980 
4981     l_return_status_service       VARCHAR2(4000);
4982     l_msg_count                   NUMBER;
4983     l_msg_data                    VARCHAR2(4000);
4984     l_msg                         VARCHAR2(4000);
4985 
4986     l_return_status               BOOLEAN := TRUE;
4987     l_api_name                    VARCHAR2(30) := 'Release_Tax_Holds';
4988 
4989   BEGIN
4990 
4991     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Release_Tax_Holds<-' ||
4992                                P_calling_sequence;
4993 
4994     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
4995       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_curr_calling_sequence);
4996     END IF;
4997 
4998     IF (P_Tax_Hold_Code.COUNT = 0 ) THEN
4999 
5000       RETURN TRUE;
5001     END IF;
5002 
5003     -----------------------------------------------------------------
5004     l_debug_info := 'Step 1: Populating invoice header local record';
5005     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5006        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5007     END IF;
5008     -----------------------------------------------------------------
5009 
5010     BEGIN
5011       OPEN Invoice_Header;
5012       FETCH Invoice_Header INTO l_inv_header_rec;
5013       CLOSE Invoice_Header;
5014     END;
5015 
5016     -------------------------------------------------------------------
5017     l_debug_info := 'Step 2: Get event class code';
5018     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5019        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5020     END IF;
5021     -------------------------------------------------------------------
5022 
5023     IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
5024       P_Invoice_Type_Lookup_Code => l_inv_header_rec.invoice_type_lookup_code,
5025       P_Event_Class_Code         => l_event_class_code,
5026       P_error_code               => P_error_code,
5027       P_calling_sequence         => l_curr_calling_sequence)) THEN
5028 
5029       l_return_status := FALSE;
5030 
5031     END IF;
5032 
5033     -------------------------------------------------------------------
5034     l_debug_info := 'Step 3: Get event type code';
5035     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5036        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5037     END IF;
5038     -------------------------------------------------------------------
5039     IF (l_return_status = TRUE) THEN
5040       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
5041         P_Event_Class_Code          => l_event_class_code,
5042         P_Calling_Mode              => P_Calling_Mode,
5043         P_eTax_Already_called_flag  => NULL,
5044         P_Event_Type_Code           => l_Event_Type_Code,
5045         P_Error_Code                => P_error_code,
5046         P_Calling_Sequence          => l_curr_calling_sequence)) THEN
5047 
5048         l_return_status := FALSE;
5049       END IF;
5050     END IF;
5051 
5052 
5053     -------------------------------------------------------------------
5054     l_debug_info := 'Step 4: Populate service specific parameter';
5055     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5056        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5057     END IF;
5058     -------------------------------------------------------------------
5059     l_transaction_rec.internal_organization_id := l_inv_header_rec.org_id;
5060     l_transaction_rec.application_id           := 200;
5061     l_transaction_rec.entity_code              := 'AP_INVOICES';
5062     l_transaction_rec.event_class_code         := l_event_class_code;
5063     l_transaction_rec.event_type_code          := l_event_type_code;
5064     l_transaction_rec.trx_id                   := l_inv_header_rec.invoice_id;
5065 
5066     -------------------------------------------------------------------
5067     l_debug_info := 'Step 5: Populate tax_hold_release_code ';
5068     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5069        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5070     END IF;
5071     -------------------------------------------------------------------
5072     IF ( l_return_status = TRUE) THEN
5073 
5074       IF ( P_Tax_Hold_Code.COUNT <> 0 ) THEN
5075         FOR i IN P_Tax_Hold_Code.FIRST..P_Tax_Hold_Code.LAST LOOP
5076           --  P_Tax_Hold_Code is populated with the tax hold codes to
5077           --  release.  Posible values are: TAX VARIANCE and TAX AMOUNT RANGE
5078           --  We need to pass to eTax the release code, so a conversion is
5079           --  required
5080 
5081           IF (P_Tax_Hold_Code(i) = 'TAX VARIANCE') THEN
5082             l_validation_status(i) :=  'TAX VARIANCE CORRECTED';
5083 
5084           ELSIF (P_Tax_Hold_Code(i) = 'TAX AMOUNT RANGE') THEN
5085             l_validation_status(i) :=  'TAX AMOUNT RANGE CORRECTED';
5086 
5087           END IF;
5088         END LOOP;
5089       END IF;
5090     END IF;
5091 
5092 
5093     -------------------------------------------------------------------
5094     l_debug_info := 'Step 6: Call to global_document_update service';
5095     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5096        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5097     END IF;
5098     -------------------------------------------------------------------
5099     IF ( l_return_status = TRUE ) THEN
5100       zx_api_pub.global_document_update(
5101         p_api_version        => 1.0,
5102         p_init_msg_list      => FND_API.G_TRUE,
5103         p_commit             => FND_API.G_FALSE,
5104         p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
5105         p_transaction_rec    => l_transaction_rec,
5106         p_validation_status  => l_validation_status,
5107         x_return_status      => l_return_status_service,
5108         x_msg_count          => l_msg_count,
5109         x_msg_data           => l_msg_data);
5110 
5111     END IF;
5112 
5113     -----------------------------------------------------------------
5117     END IF;
5114     l_debug_info := 'Step 7: Verify return status';
5115     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5116        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5118     -----------------------------------------------------------------
5119     IF (l_return_status_service <> 'S') THEN  -- handle errors
5120 
5121       l_return_status := FALSE;
5122       -----------------------------------------------------------------
5123       l_debug_info := 'Step 8: Handle errors returned by API';
5124       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5125          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5126       END IF;
5127       -----------------------------------------------------------------
5128 
5129       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
5130                P_All_Error_Messages  => P_All_Error_Messages,
5131                P_Msg_Count           => l_msg_count,
5132                P_Msg_Data            => l_msg_data,
5133                P_Error_Code          => P_Error_Code,
5134                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
5135         NULL;
5136       END IF;
5137     END IF;
5138 
5139     RETURN l_return_status;
5140 
5141   EXCEPTION
5142     WHEN OTHERS THEN
5143       IF (SQLCODE <> -20001) THEN
5144         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
5145         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
5146         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
5147         FND_MESSAGE.SET_TOKEN('PARAMETERS',
5148           ' P_Invoice_Id = '||P_Invoice_Id||
5149           ' P_Calling_Mode ='||P_Calling_Mode||
5150           ' P_Error_Code = '||P_Error_Code||
5151           ' P_Calling_Sequence = '||P_Calling_Sequence);
5152         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
5153       END IF;
5154 
5155       APP_EXCEPTION.RAISE_EXCEPTION;
5156 
5157   END Release_Tax_Holds;
5158 
5159 /*=============================================================================
5160  |  FUNCTION - Mark_Tax_Lines_Deleted()
5161  |
5162  |  DESCRIPTION
5163  |      Public function that will call the mark_tax_lines_deleted service.
5164  |      This API assumes the calling code controls the commit cycle.
5165  |      This function returns TRUE if the call to the service is successful.
5166  |      Otherwise, FALSE.
5167  |      This service should be called per invoice line.
5168  |
5169  |  PARAMETERS
5170  |      P_Invoice_Id - invoice id
5171  |      P_Calling_Mode - calling mode.  Identifies which service to call
5172  |      P_Line_Number_To_Delete - line number deleted in AP
5173  |      P_All_Error_Messages - Should API return 1 error message or allow
5174  |                             calling point to get them from message stack
5175  |      P_error_code - Error code to be returned
5176  |      P_calling_sequence -  Calling sequence
5177  |
5178  |  MODIFICATION HISTORY
5179  |    DATE          Author         Action
5180  |    07-OCT-2003   SYIDNER        Created
5181  |
5182  *============================================================================*/
5183 
5184   FUNCTION Mark_Tax_Lines_Deleted(
5185              P_Invoice_id              IN NUMBER,
5186              P_Calling_Mode            IN VARCHAR2,
5187              P_Line_Number_To_Delete   IN NUMBER,
5188              P_All_Error_Messages      IN VARCHAR2,
5189              P_Error_Code              OUT NOCOPY VARCHAR2,
5190              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
5191   IS
5192 
5193     l_debug_info                 VARCHAR2(240);
5194     l_curr_calling_sequence      VARCHAR2(4000);
5195     --Bug8604959: Added debug logging
5196     l_api_name			 VARCHAR2(30) := 'Mark_Tax_Lines_Deleted';
5197     l_inv_header_rec             ap_invoices_all%ROWTYPE;
5198     l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
5199     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
5200 
5201     CURSOR Invoice_Header IS
5202     SELECT *
5203       FROM ap_invoices_all
5204      WHERE invoice_id = P_Invoice_Id;
5205 
5206     l_transaction_line_rec       zx_api_pub.transaction_line_rec_type;
5207 
5208     l_return_status_service      VARCHAR2(4000);
5209     l_msg_count                  NUMBER;
5210     l_msg_data                   VARCHAR2(4000);
5211     l_msg                        VARCHAR2(4000);
5212 
5213     l_return_status              BOOLEAN := TRUE;
5214 
5215   BEGIN
5216     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Mark_Tax_Lines_Deleted<-' ||
5217                                P_calling_sequence;
5218 
5219     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5220        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_curr_calling_sequence);
5221     END IF;
5222 
5223     -----------------------------------------------------------------
5224     l_debug_info := 'Step 1: Populating invoice header local record';
5225     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5226        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5227     END IF;
5228     --Print(l_api_name, l_debug_info);
5229     -----------------------------------------------------------------
5230 
5231     BEGIN
5232       OPEN Invoice_Header;
5233       FETCH Invoice_Header INTO l_inv_header_rec;
5234       CLOSE Invoice_Header;
5235     END;
5236 
5237 
5238     -------------------------------------------------------------------
5239     l_debug_info := 'Step 2: Get event class code';
5240     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5241        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5242     END IF;
5243     --Print(l_api_name, l_debug_info);
5244     -------------------------------------------------------------------
5245 
5246     IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
5247       P_Invoice_Type_Lookup_Code => l_inv_header_rec.invoice_type_lookup_code,
5248       P_Event_Class_Code         => l_event_class_code,
5249       P_error_code               => P_error_code,
5250       P_calling_sequence         => l_curr_calling_sequence)) THEN
5251 
5252       l_return_status := FALSE;
5253 
5254     END IF;
5255 
5256     -------------------------------------------------------------------
5257     l_debug_info := 'Step 3: Get event type code';
5258     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5262     -------------------------------------------------------------------
5259        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5260     END IF;
5261     --Print(l_api_name, l_debug_info);
5263     IF (l_return_status = TRUE) THEN
5264       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
5265         P_Event_Class_Code          => l_event_class_code,
5266         P_Calling_Mode              => P_Calling_Mode,
5267         P_eTax_Already_called_flag  => NULL,
5268         P_Event_Type_Code           => l_Event_Type_Code,
5269         P_Error_Code                => P_error_code,
5270         P_Calling_Sequence          => l_curr_calling_sequence)) THEN
5271 
5272         l_return_status := FALSE;
5273       END IF;
5274     END IF;
5275 
5276     -------------------------------------------------------------------
5277     l_debug_info := 'Step 4: Populate service specific parameter';
5278     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5279        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5280     END IF;
5281     --Print(l_api_name, l_debug_info);
5282     -------------------------------------------------------------------
5283     l_transaction_line_rec.internal_organization_id := l_inv_header_rec.org_id;
5284     l_transaction_line_rec.application_id           := 200;
5285     l_transaction_line_rec.entity_code              := 'AP_INVOICES';
5286     l_transaction_line_rec.event_class_code         := l_event_class_code;
5287     l_transaction_line_rec.event_type_code          := l_event_type_code;
5288     l_transaction_line_rec.trx_id                   := P_Invoice_Id;
5289     l_transaction_line_rec.trx_level_type           := 'LINE';
5290     l_transaction_line_rec.trx_line_id              := P_Line_Number_To_Delete;
5291 
5292     -------------------------------------------------------------------
5293     l_debug_info := 'Step 6: Call to del_tax_line_and_distributions';
5294     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5295        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5296     END IF;
5297     --Print(l_api_name, l_debug_info);
5298     -------------------------------------------------------------------
5299     IF ( l_return_status = TRUE ) THEN
5300 
5301       --Bug8604959: Calling ZX API del_tax_line_and_distributions instead of
5302       --old zx api Mark_Tax_Lines_Deleted
5303       zx_api_pub.del_tax_line_and_distributions(
5304         p_api_version             => 1.0,
5305         p_init_msg_list           => FND_API.G_TRUE,
5306         p_commit                  => FND_API.G_FALSE,
5307         p_validation_level        => FND_API.G_VALID_LEVEL_FULL,
5308         p_transaction_line_rec    => l_transaction_line_rec,
5309         x_return_status           => l_return_status_service,
5310         x_msg_count               => l_msg_count,
5311         x_msg_data                => l_msg_data);
5312 
5313     END IF;
5314 
5315     -----------------------------------------------------------------
5316     l_debug_info := 'Step 7: Verify return status';
5317     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5318        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5319     END IF;
5320     --Print(l_api_name, l_debug_info);
5321     -----------------------------------------------------------------
5322     --Bug8604959 : Changed the IF ELSE block to add call to Update_AP api
5323     IF (l_return_status_service = 'S') THEN  -- Sync ZX and AP
5324 
5325 	-----------------------------------------------------------------
5326         l_debug_info := 'Step 8: Sync up ZX and AP data after delete';
5327         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5328            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5329         END IF;
5330         --Print(l_api_name, l_debug_info);
5331         -----------------------------------------------------------------
5332         IF NOT(AP_ETAX_SERVICES_PKG.Update_AP(
5333 	        P_Invoice_header_rec => l_inv_header_rec,
5334 	        P_Calling_Mode       => 'DELETE TAX LINE',
5335 	        P_All_Error_Messages => P_All_Error_Messages,
5336 	        P_Error_Code         => P_error_code,
5337 	        P_Calling_Sequence   => l_curr_calling_sequence)) THEN
5338 
5339             l_return_status := FALSE;
5340         END IF;
5341 
5342     ELSE --Handle errors
5343 
5344       l_return_status := FALSE;
5345       -----------------------------------------------------------------
5346       l_debug_info := 'Step 8: Handle errors returned by API';
5347       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5348          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5349       END IF;
5350       --Print(l_api_name, l_debug_info);
5351       -----------------------------------------------------------------
5352 
5353       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
5354                P_All_Error_Messages  => P_All_Error_Messages,
5355                P_Msg_Count           => l_msg_count,
5356                P_Msg_Data            => l_msg_data,
5357                P_Error_Code          => P_Error_Code,
5358                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
5359         NULL;
5360       END IF;
5361 
5362     END IF; --l_return_status
5363     --End of bug8604959
5364     RETURN l_return_status;
5365 
5366   EXCEPTION
5367     WHEN OTHERS THEN
5368       IF (SQLCODE <> -20001) THEN
5369         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
5370         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
5371         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
5372         FND_MESSAGE.SET_TOKEN('PARAMETERS',
5373           ' P_Invoice_Id = '||P_Invoice_Id||
5374           ' P_Line_Number_To_Delete = '||P_Line_Number_To_Delete||
5375           ' P_Calling_Mode ='||P_Calling_Mode||
5376           ' P_Error_Code = '||P_Error_Code||
5377           ' P_Calling_Sequence = '||P_Calling_Sequence);
5378         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
5379       END IF;
5380 
5381       APP_EXCEPTION.RAISE_EXCEPTION;
5382 
5383   END Mark_Tax_Lines_Deleted;
5384 
5385 --bug 9343533
5386 /*=============================================================================
5387  |  FUNCTION - Mark_Tax_Lines_Deleted()
5388  |
5389  |  DESCRIPTION
5390  |      Public function that will call the mark_tax_lines_deleted service.
5391  |      This API assumes the calling code controls the commit cycle.
5392  |      This function returns TRUE/FALSE as varchar2 for bug 9343533
5393  |
5394  |  PARAMETERS
5395  |      P_Invoice_Id - invoice id
5396  |      P_Calling_Mode - calling mode.  Identifies which service to call
5397  |      P_Line_Number_To_Delete - Tax Line to delete
5398  |      P_All_Error_Messages - Should API return 1 error message or allow
5399  |                             calling point to get them from message stack
5400  |      P_error_code - Error code to be returned
5401  |      P_calling_sequence -  Calling sequence
5402  |      P_dummy - dummy variable to differentiate from existing
5403  |                Mark_Tax_Lines_Deleted API
5404  |
5405  |  MODIFICATION HISTORY
5406  |    DATE              Author                  Action
5407  |    25-MAR-2010   DCSHANMU        Created
5408  |
5409  *============================================================================*/
5410   FUNCTION Mark_Tax_Lines_Deleted(
5411              P_Invoice_id              IN NUMBER,
5412              P_Calling_Mode            IN VARCHAR2,
5413              P_Line_Number_To_Delete   IN NUMBER,
5414              P_All_Error_Messages      IN VARCHAR2,
5415              P_Error_Code              OUT NOCOPY VARCHAR2,
5416              P_Calling_Sequence        IN VARCHAR2,
5417              p_dummy                   IN VARCHAR2) RETURN VARCHAR2 IS
5418 
5419 result boolean := false;
5420 success varchar2(1000) := 'FALSE';
5421 
5422 BEGIN
5423 	result := Mark_Tax_Lines_Deleted(
5424              P_Invoice_id,
5425              P_Calling_Mode,
5426              P_Line_Number_To_Delete,
5427              P_All_Error_Messages,
5428              P_Error_Code,
5429              P_Calling_Sequence);
5430 
5431 	IF (result = true) THEN
5432 	   success := 'TRUE';
5433 	END IF;
5434 
5435 	return success;
5436 END;
5437 
5438 /*=============================================================================
5439  |  FUNCTION - Validate_Invoice()
5440  |
5441  |  DESCRIPTION
5442  |      Public function that will call the validate_document_for_tax service.
5443  |      This API assumes the calling code controls the commit cycle.
5444  |      This function returns TRUE if the call to the service is successful.
5445  |      Otherwise, FALSE.
5446  |
5447  |  PARAMETERS
5448  |      P_Invoice_Id - invoice id
5449  |      P_Calling_Mode - calling mode.  Identifies which service to call
5450  |      P_All_Error_Messages - Should API return 1 error message or allow
5451  |                             calling point to get them from message stack
5452  |      P_error_code - Error code to be returned
5453  |      P_calling_sequence -  Calling sequence
5454  |
5455  |  MODIFICATION HISTORY
5456  |    DATE          Author         Action
5457  |    07-OCT-2003   SYIDNER        Created
5458  |
5459  *============================================================================*/
5460 
5461   FUNCTION Validate_Invoice(
5462              P_Invoice_id              IN NUMBER,
5463              P_Calling_Mode            IN VARCHAR2,
5464              P_All_Error_Messages      IN VARCHAR2,
5465              P_Error_Code              OUT NOCOPY VARCHAR2,
5466              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN IS
5467 
5468 
5469     l_debug_info                 VARCHAR2(240);
5470     l_curr_calling_sequence      VARCHAR2(4000);
5471 
5472     l_inv_header_rec             ap_invoices_all%ROWTYPE;
5473     l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
5474     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
5475 
5476     CURSOR Invoice_Header IS
5477     SELECT *
5478       FROM ap_invoices_all
5479      WHERE invoice_id = P_Invoice_Id;
5480 
5481     l_transaction_rec             zx_api_pub.transaction_rec_type;
5482 
5483 --    l_validation_status_tab       zx_api_pub.validation_status_tbl_type;
5484     l_validation_status		  VARCHAR2(1);
5485     l_hold_codes_tab		  zx_api_pub.hold_codes_tbl_type;
5486 
5487     l_return_status_service       VARCHAR2(4000);
5488     l_msg_count                   NUMBER;
5489     l_msg_data                    VARCHAR2(4000);
5490     l_msg                         VARCHAR2(4000);
5491 
5492     l_return_status               BOOLEAN := TRUE;
5493 
5494     TYPE hold_lookup_code_tab IS TABLE OF ap_holds_all.hold_lookup_code%TYPE;
5495     l_hold_lookup_code            hold_lookup_code_tab;
5496     l_release_lookup_code         ap_holds_all.release_lookup_code%TYPE;
5497     l_api_name			 varchar2(30);
5498     --Bug 7410237 start
5499     l_system_user                 NUMBER := 5;
5500 
5501     l_holds                       AP_APPROVAL_PKG.HOLDSARRAY;
5502     l_hold_count                  AP_APPROVAL_PKG.COUNTARRAY;
5503     l_release_count               AP_APPROVAL_PKG.COUNTARRAY;
5504     --Bug 7410237 End
5505 
5506 
5507   BEGIN
5508 
5509     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Validate_Invoice<-' ||
5510                                P_calling_sequence;
5511 
5512     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5513        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_curr_calling_sequence);
5514     END IF;
5515 
5516     l_api_name := 'Validate_Invoice';
5517     -----------------------------------------------------------------
5518     l_debug_info := 'Step 1: Populating invoice header local record';
5519     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5520        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5521     END IF;
5522     -----------------------------------------------------------------
5523 
5524     BEGIN
5525       OPEN Invoice_Header;
5526       FETCH Invoice_Header INTO l_inv_header_rec;
5527       CLOSE Invoice_Header;
5528     END;
5529 
5530     IF ((l_inv_header_rec.quick_credit = 'Y') OR    -- Bug 5660314
5531         (l_inv_header_rec.invoice_type_lookup_code IN ('AWT', 'INTEREST'))) THEN
5532       RETURN l_return_status;
5533     END IF;
5534 
5535 
5536     -------------------------------------------------------------------
5537     l_debug_info := 'Step 2: Get event class code';
5538     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5539        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5540     END IF;
5541     -------------------------------------------------------------------
5542 
5543     IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
5544       P_Invoice_Type_Lookup_Code => l_inv_header_rec.invoice_type_lookup_code,
5545       P_Event_Class_Code         => l_event_class_code,
5546       P_error_code               => P_error_code,
5547       P_calling_sequence         => l_curr_calling_sequence)) THEN
5548 
5549       l_return_status := FALSE;
5550 
5551     END IF;
5552 
5553     -------------------------------------------------------------------
5554     l_debug_info := 'Step 3: Get event type code';
5555     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5556        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5557     END IF;
5558     -------------------------------------------------------------------
5559     IF (l_return_status = TRUE) THEN
5560       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
5561         P_Event_Class_Code          => l_event_class_code,
5562         P_Calling_Mode              => P_Calling_Mode,
5563         P_eTax_Already_called_flag  => NULL,
5564         P_Event_Type_Code           => l_Event_Type_Code,
5565         P_Error_Code                => P_error_code,
5566         P_Calling_Sequence          => l_curr_calling_sequence)) THEN
5567 
5568         l_return_status := FALSE;
5569       END IF;
5570     END IF;
5571 
5572     -------------------------------------------------------------------
5573     l_debug_info := 'Step 4: Populate service specific parameter';
5574     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5575        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5579     l_transaction_rec.application_id           := 200;
5576     END IF;
5577     -------------------------------------------------------------------
5578     l_transaction_rec.internal_organization_id := l_inv_header_rec.org_id;
5580     l_transaction_rec.entity_code              := 'AP_INVOICES';
5581     l_transaction_rec.event_class_code         := l_event_class_code;
5582     l_transaction_rec.event_type_code          := l_event_type_code;
5583     l_transaction_rec.trx_id                   := l_inv_header_rec.invoice_id;
5584 
5585     -------------------------------------------------------------------
5586     l_debug_info := 'Step 5: Call validate_document_for_tax service';
5587     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5588        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5589     END IF;
5590     -------------------------------------------------------------------
5591     IF ( l_return_status = TRUE ) THEN
5592 
5593       zx_api_pub.validate_document_for_tax(
5594         p_api_version        => 1.0,
5595         p_init_msg_list      => FND_API.G_TRUE,
5596         p_commit             => FND_API.G_FALSE,
5597         p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
5598         p_transaction_rec    => l_transaction_rec,
5599         x_validation_status  => l_validation_status,
5600 	    x_hold_codes_tbl     => l_hold_codes_tab,
5601         x_return_status      => l_return_status_service,
5602         x_msg_count          => l_msg_count,
5603         x_msg_data           => l_msg_data);
5604 
5605     END IF;
5606 
5607     -----------------------------------------------------------------
5608     l_debug_info := 'Step 6: Verify return status';
5609     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5610        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5611     END IF;
5612     -----------------------------------------------------------------
5613     IF (l_return_status_service = 'S') THEN
5614 
5615       l_debug_info := 'l_hold_codes_tab.count is '||l_hold_codes_tab.count;
5616       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5617           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5618       END IF;
5619 
5620       -----------------------------------------------------------------
5621       l_debug_info := 'Step 7: Check for tax holds on invoice';
5622       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5623        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5624       END IF;
5625       -----------------------------------------------------------------
5626       --bugfix:5523240 Replaced the IF condition as the etax service
5627       --returns 'Y' eventhough tax holds are place
5628       --IF ( l_validation_status = 'N' ) THEN
5629       IF(l_hold_codes_tab.count = 0) THEN
5630             -----------------------------------------------------------------
5631             l_debug_info := 'Step 8: Verify if invoice has no released tax '||
5632                             ' holds';
5633             IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5634                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5635             END IF;
5636             -----------------------------------------------------------------
5637             BEGIN
5638               SELECT hold_lookup_code
5639                 BULK COLLECT INTO l_hold_lookup_code
5640                 FROM ap_holds_all
5641                WHERE invoice_id = l_inv_header_rec.invoice_id
5642                  AND org_id = l_inv_header_rec.org_id
5643                  AND hold_lookup_code IN ('TAX VARIANCE', 'TAX AMOUNT RANGE')
5644                  AND release_lookup_code is NULL;
5645 
5646             EXCEPTION
5647               WHEN NO_DATA_FOUND THEN NULL;
5648             END;
5649 
5650             IF (l_hold_lookup_code.COUNT <> 0) THEN
5651                FOR i IN l_hold_lookup_code.FIRST..l_hold_lookup_code.LAST LOOP
5652 
5653 		 ----------------------------------------------------------------------------
5654 	         l_debug_info := 'Release tax holds';
5655                  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5656                    FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5657                  END IF;
5658 		 ----------------------------------------------------------------------------
5659                 -- Bug 7410237 Start
5660 
5661                 /*
5662                  IF (l_hold_lookup_code(i) = 'TAX VARIANCE') THEN
5663                   l_release_lookup_code := 'VARIANCE CORRECTED';
5664 
5665                  ELSE
5666                    l_release_lookup_code := 'HOLDS QUICK RELEASED';
5667 
5668                  END IF;
5669 
5670                  ap_holds_pkg.release_single_hold(
5671                    X_invoice_id              => l_inv_header_rec.invoice_id,
5672                    X_hold_lookup_code        => l_hold_lookup_code(i),
5673                    X_release_lookup_code     => l_release_lookup_code,
5674                    X_held_by                 => NULL,
5675                    X_calling_sequence        => l_curr_calling_sequence);
5676                  */
5677 
5678                   AP_APPROVAL_PKG.Process_Inv_Hold_Status(
5679                         p_invoice_id              =>   l_inv_header_rec.invoice_id,
5680                         p_line_location_id        =>   NULL,
5681                         p_rcv_transaction_id      =>   NULL,
5682                         p_hold_lookup_code        =>   l_hold_lookup_code(i),
5683                         p_should_have_hold        =>   'N',
5684                         p_hold_reason             =>   NULL,
5685                         p_system_user             =>   l_system_user,
5686                         p_holds                   =>   l_holds,
5687                         p_holds_count             =>   l_hold_count,
5688                         p_release_count           =>   l_release_count,
5689                         p_calling_sequence        =>   l_curr_calling_sequence);
5690 
5691                    -- Bug 7410237 End
5692                END LOOP;
5693             END IF;
5694 
5695         ELSIF (l_hold_codes_tab.count <> 0) THEN
5696            FOR i IN l_hold_codes_tab.FIRST..l_hold_codes_tab.LAST LOOP
5697 
5698               l_debug_info := 'l_hold_codes_tab(i) is '||l_hold_codes_tab(i);
5699               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5700                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5701               END IF;
5702 
5703               -----------------------------------------------------------------
5704               l_debug_info := 'Step 9: Create tax hold in AP if not exists';
5705               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5706                  FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5707               END IF;
5708               -----------------------------------------------------------------
5709               -- the posible values eTax will populate l_validation_status_tab
5710               -- with are:  TAX VARIANCE and TAX AMOUNT RANGE.  These are the same
5711               -- holds lookup codes used by API so there is no need to convert any
5712               -- value here
5713               -- Bug 7410237 Start
5714               /*
5715               ap_holds_pkg.insert_single_hold(
5716            	   X_invoice_id          => l_inv_header_rec.invoice_id,
5717               	   X_hold_lookup_code    => l_hold_codes_tab(i),
5718                    X_hold_type           => NULL,
5719                    X_hold_reason         => NULL,
5720                    X_held_by             => l_user_id,
5721                    X_calling_sequence    => l_curr_calling_sequence);
5722               */
5723                   AP_APPROVAL_PKG.Process_Inv_Hold_Status(
5724                         p_invoice_id              =>   l_inv_header_rec.invoice_id,
5725                         p_line_location_id        =>   NULL,
5726                         p_rcv_transaction_id      =>   NULL,
5727                         p_hold_lookup_code        =>   l_hold_codes_tab(i),
5728                         p_should_have_hold        =>   'Y',
5729                         p_hold_reason             =>   NULL,
5730                         p_system_user             =>   l_system_user,
5731                         p_holds                   =>   l_holds,
5732                         p_holds_count             =>   l_hold_count,
5733                         p_release_count           =>   l_release_count,
5734                         p_calling_sequence        =>   l_curr_calling_sequence);
5735 
5736                -- Bug 7410237 End
5737           END LOOP;
5738         END IF;
5739 
5740    ELSE  -- handle errors
5741 
5742       l_return_status := FALSE;
5743       -----------------------------------------------------------------
5744       l_debug_info := 'Step 8: Handle errors returned by API';
5745       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5746          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5747       END IF;
5748       -----------------------------------------------------------------
5749 
5750       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
5751                P_All_Error_Messages  => P_All_Error_Messages,
5752                P_Msg_Count           => l_msg_count,
5753                P_Msg_Data            => l_msg_data,
5754                P_Error_Code          => P_Error_Code,
5755                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
5756         NULL;
5757       END IF;
5758     END IF;
5759 
5760     RETURN l_return_status;
5761 
5762   END Validate_Invoice;
5763 
5764 /*=============================================================================
5765  |  FUNCTION - Validate_Default_Import()
5766  |
5767  |  DESCRIPTION
5768  |      Public function that will call the validate_and_default_tax_attr service.
5769  |      This API assumes the calling code controls the commit cycle.
5770  |      This function returns TRUE if the call to the service is successful.
5771  |      Otherwise, FALSE.
5772  |      This API will validate the taxable and tax lines to be imported regarding
5773  |      tax.  The lines will be passed to this API using the pl/sql structures
5774  |      defined in the import process.
5775  |      The service validate_and_default_tax_attr will default any possible tax
5776  |      value, and this API will modify the pl/sql structures with the defaulted
5777  |      tax info.
5778  |
5779  |  PARAMETERS
5780  |      p_invoice_rec - record defined in the import program for the invoice header
5781  |      p_invoice_lines_tab - array with the taxable and tax lines
5782  |      P_Calling_Mode - calling mode.  Identifies which service to call
5783  |      P_All_Error_Messages - Should API return 1 error message or allow
5784  |                             calling point to get them from message stack
5785  |      p_invoice_status - returns N if the invoice should be rejected.
5786  |      P_error_code - Error code to be returned
5787  |      P_calling_sequence -  Calling sequence
5788  |
5789  |  MODIFICATION HISTORY
5790  |    DATE          Author         Action
5791  |    20-JAN-2004   SYIDNER        Created
5792  |
5793  *============================================================================*/
5794    FUNCTION Validate_Default_Import(
5795              P_Invoice_Rec_table             IN OUT NOCOPY
5796                AP_IMPORT_INVOICES_PKG.t_invoice_table,
5797              p_invoice_lines_table       IN OUT NOCOPY
5798                AP_IMPORT_INVOICES_PKG.t_lines_table,
5799              P_Calling_Mode            IN VARCHAR2,
5800              P_All_Error_Messages      IN VARCHAR2,
5801              p_invoice_status          OUT NOCOPY VARCHAR2,
5802              P_Error_Code              OUT NOCOPY VARCHAR2,
5803              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
5804   IS
5805     l_debug_info                 VARCHAR2(240);
5806     l_curr_calling_sequence      VARCHAR2(4000);
5807 
5808     l_event_class_code
5809       zx_trx_headers_gt.event_class_code%TYPE;
5810     l_event_type_code
5811       zx_trx_headers_gt.event_type_code%TYPE;
5812 
5813     l_return_status_service       VARCHAR2(4000);
5814     l_msg_count                   NUMBER;
5815     l_msg_data                    VARCHAR2(4000);
5816     l_msg                         VARCHAR2(4000);
5817 
5818     l_return_status               BOOLEAN := TRUE;
5819     l_api_name                    varchar2(30) := 'Validate_Default_Import';
5820     --bug 15862708 starts
5821     P_Invoice_Rec       AP_IMPORT_INVOICES_PKG.r_invoice_info_rec;
5822     p_invoice_lines_tabl AP_IMPORT_INVOICES_PKG.t_lines_table;
5823     k   number;
5824     l_cnt number;
5825    --bug 15862708 ends
5826   BEGIN
5827 
5828     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Validate_Default_Import<-'
5829                                ||P_calling_sequence;
5830 
5831     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5832        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_curr_calling_sequence);
5833     END IF;
5834  --bug 15862708
5835      IF P_Invoice_Rec_table.COUNT>0  THEN
5836        p_invoice_lines_tabl.DELETE;
5837       FOR i IN P_Invoice_Rec_table.FIRST .. P_Invoice_Rec_table.LAST
5838       LOOP
5839 	    IF P_Invoice_Rec_table(i).invoice_type_lookup_code<>'PAYMENT REQUEST' THEN
5840          P_Invoice_Rec := P_Invoice_Rec_table(i);
5841           p_invoice_lines_tabl.delete;
5842           k :=0;
5843          FOR j IN p_invoice_lines_table.FIRST .. p_invoice_lines_table.LAST
5844          LOOP
5845             IF p_invoice_lines_table(j).invoice_id=P_Invoice_Rec.invoice_id THEN
5846                p_invoice_lines_tabl(k) := p_invoice_lines_table(j);
5847                k := k+1;
5848             END IF;
5849          END LOOP;
5850            -----------------------------------------------------------------
5851            l_debug_info := 'Step 1: Populate Header';
5852            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5853               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5854            END IF;
5855            -----------------------------------------------------------------
5856            IF NOT(AP_ETAX_SERVICES_PKG.Populate_Header_Import_GT(
5857              P_Invoice_Header_Rec         => P_Invoice_Rec,
5858              P_Calling_Mode               => P_Calling_Mode,
5859              P_Event_Class_Code           => l_event_class_code,
5860              P_Event_Type_Code            => l_event_type_code,
5861              P_Error_Code                 => P_error_code,
5862              P_Calling_Sequence           => l_curr_calling_sequence )) THEN
5863 
5864              l_return_status := FALSE;
5865            END IF;
5866 
5867 
5868 
5869            -----------------------------------------------------------------
5870            l_debug_info := 'Step 2: Populate Trx and Tax Lines and allocation '||
5871                            'structure ';
5872            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5873               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5874            END IF;
5875            -----------------------------------------------------------------
5876            IF ( l_return_status = TRUE ) THEN
5877              IF NOT(AP_ETAX_SERVICES_PKG.Populate_Lines_Import_GT(
5878                P_Invoice_Header_Rec      => P_Invoice_Rec,
5879                P_Inv_Line_List           => p_invoice_lines_tabl,
5880                P_Calling_Mode            => P_Calling_Mode,
5881                P_Event_Class_Code        => l_event_class_code,
5882                P_Error_Code              => P_error_code,
5883                P_Calling_Sequence        => l_curr_calling_sequence )) THEN
5884 
5885                l_return_status := FALSE;
5886              END IF;
5887            END IF;
5888          END IF;
5889        END LOOP;
5890      END IF;
5891     -----------------------------------------------------------------
5892     l_debug_info := 'Step 3: Call validate_and_default_tax_attr service';
5893     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5894        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5895     END IF;
5896     -----------------------------------------------------------------
5897     l_debug_info := 'Call validate_and_default_tax_attr service start ' ||to_char(sysdate,'YY-MON-DD HH24:MI:SS');
5898             IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
5899               AP_IMPORT_UTILITIES_PKG.Print(
5900                 AP_IMPORT_INVOICES_PKG.g_debug_switch, l_debug_info);
5901             END IF;
5902 
5903       zx_api_pub.validate_and_default_tax_attr(
5904         p_api_version      => 1.0,
5905         p_init_msg_list    => FND_API.G_TRUE,
5906         p_commit           => FND_API.G_FALSE,
5907         p_validation_level => FND_API.G_VALID_LEVEL_FULL,
5908         x_return_status    => l_return_status_service,
5909         x_msg_count        => l_msg_count,
5910         x_msg_data         => l_msg_data);
5911 
5912 
5913      l_debug_info := 'Call validate_and_default_tax_attr service ends ' ||to_char(sysdate,'YY-MON-DD HH24:MI:SS');
5914             IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
5915               AP_IMPORT_UTILITIES_PKG.Print(
5916                 AP_IMPORT_INVOICES_PKG.g_debug_switch, l_debug_info);
5917             END IF;
5918 
5919    IF (l_return_status_service = 'S') THEN
5920 
5921     IF  P_Invoice_Rec_table.COUNT>0 THEN
5922      FOR i IN P_Invoice_Rec_table.FIRST .. P_Invoice_Rec_table.LAST
5923      LOOP
5924 	   IF P_Invoice_Rec_table(i).invoice_type_lookup_code<>'PAYMENT REQUEST' THEN
5925        P_Invoice_Rec := P_Invoice_Rec_table(i);
5926 
5927        -----------------------------------------------------------------
5928        l_debug_info := 'Step 4: Handle return of tax lines';
5929 
5930        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5931            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5932        END IF;
5933        -----------------------------------------------------------------
5934 
5935       IF NOT (AP_ETAX_UTILITY_PKG.Return_Default_Import(
5936                 P_Invoice_Header_Rec      => P_Invoice_Rec,
5937                 P_Invoice_Lines_Tab       => p_invoice_lines_table,
5938                 P_All_Error_Messages      => 'N',
5939                 P_Error_Code              => P_Error_Code,
5940                 P_Calling_Sequence        => substr(l_curr_calling_sequence,1,100),
5941 		P_invoice_status	  => p_invoice_status --Bug6625518
5942 		)) THEN
5943 
5944         l_return_status := FALSE;
5945 
5946       ELSE
5947 
5948             l_debug_info := 'Return_Default_Import was fine' ;
5949             IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
5950               AP_IMPORT_UTILITIES_PKG.Print(
5951                 AP_IMPORT_INVOICES_PKG.g_debug_switch, l_debug_info);
5952             END IF;
5953 	 --returns the record data to original collection
5954 	 --so that it can be passed to calling proc
5955 
5956            P_Invoice_Rec_table(i) := P_Invoice_Rec;
5957 
5958       END IF;
5959 
5960      END IF;
5961      END LOOP;
5962     END IF;
5963 
5964 
5965       --Bug6625518 Commenting the assignment below.
5966       --This was making the flag for processing as 'Y' even in case of rejections.
5967       --p_invoice_status := 'Y';
5968 
5969    ELSE  -- handle errors
5970 
5971       -----------------------------------------------------------------
5972       l_debug_info := 'Step 5: Handle errors returned by API';
5973       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5974          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
5975       END IF;
5976       -----------------------------------------------------------------
5977       l_return_status := FALSE;
5978 
5979       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
5980                P_All_Error_Messages  => P_All_Error_Messages,
5981                P_Msg_Count           => l_msg_count,
5982                P_Msg_Data            => l_msg_data,
5983                P_Error_Code          => P_Error_Code,
5984                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
5985         NULL;
5986 
5987       END IF;
5988 
5989       p_invoice_status := 'N';
5990 
5991    END IF;
5992 
5993    RETURN l_return_status;
5994 
5995   EXCEPTION
5996     WHEN OTHERS THEN
5997      /* Bug8345322: We dont want to raise an exception since this rollbacks
5998         the whole the import program and all the imported invoices are rolled back
6002         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
5999         The invoice causing the exception will be rejected and we will continue
6000         with import program
6001         IF (SQLCODE <> -20001) THEN
6003         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
6004         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
6005         FND_MESSAGE.SET_TOKEN('PARAMETERS',
6006           ' Invoice_Id = '||P_Invoice_Rec.Invoice_Id||
6007           ' P_Calling_Mode ='||P_Calling_Mode||
6008           ' P_Error_Code = '||P_Error_Code||
6009           ' P_Calling_Sequence = '||P_Calling_Sequence);
6010         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
6011       END IF;
6012 
6013       APP_EXCEPTION.RAISE_EXCEPTION;*/
6014       --IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
6015 	  --We want to print the error message even if debug switch is off
6016 	  --so user need not run the report again.
6017       RETURN FALSE;
6018 
6019         AP_IMPORT_UTILITIES_PKG.Print(AP_IMPORT_INVOICES_PKG.g_debug_switch,
6020                                      l_debug_info);
6021       --END IF;
6022       IF (SQLCODE < 0) then
6023          IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
6024           AP_IMPORT_UTILITIES_PKG.Print(AP_IMPORT_INVOICES_PKG.g_debug_switch,
6025                                        SQLERRM);
6026          END IF;
6027       END IF;
6028 	  p_invoice_status := 'N';
6029       RETURN (FALSE);
6030       --End of Bug8345322
6031 
6032   END Validate_Default_Import;
6033 
6034 
6035 
6036 
6037 /*=============================================================================
6038  |  FUNCTION - Populate_Headers_GT()
6039  |
6040  |  DESCRIPTION
6041  |      This function will get additional information required to populate the
6042  |      zx_trx_headers_gt
6043  |      This function returns TRUE if the insert to the temp table goes
6044  |      through successfully.  Otherwise, FALSE.
6045  |
6046  |  PARAMETERS
6047  |      P_Invoice_Header_Rec - record with invoice header information
6048  |      P_Calling_Mode - calling mode. it is used to
6049  |      P_eTax_Already_called_flag - Flag to know if this is the first time tax
6050  |                                   has been called
6051  |      P_Event_Class_Code - Event class code
6052  |      P_Event_Type_Code - Event type code
6053  |      P_error_code - Error code to be returned
6054  |      P_calling_sequence -  Calling sequence
6055  |
6056  |  MODIFICATION HISTORY
6057  |    DATE          Author         Action
6058  |    07-OCT-2003   SYIDNER        Created
6059  |
6060  *============================================================================*/
6061 
6062   FUNCTION Populate_Headers_GT(
6063              P_Invoice_Header_Rec        IN ap_invoices_all%ROWTYPE,
6064              P_Calling_Mode              IN VARCHAR2,
6065              P_eTax_Already_called_flag  IN VARCHAR2,
6066              P_Event_Class_Code          OUT NOCOPY VARCHAR2,
6067              P_Event_Type_Code           OUT NOCOPY VARCHAR2,
6068              P_Error_Code                OUT NOCOPY VARCHAR2,
6069              P_Calling_Sequence          IN VARCHAR2) RETURN BOOLEAN
6070   IS
6071 
6072     l_debug_info                 VARCHAR2(240);
6073     l_curr_calling_sequence      VARCHAR2(4000);
6074 
6075     l_application_id             zx_trx_headers_gt.application_id%TYPE;
6076     l_entity_code                zx_trx_headers_gt.entity_code%TYPE;
6077 
6078     l_quote_flag                 zx_trx_headers_gt.quote_flag%TYPE := 'N';
6079     -- This flag is always N except when the calculate service is called for
6080     -- quote for the recurring invoices and distributions sets.
6081 
6082     CURSOR tax_related_invoice( c_tax_related_invoice_id IN NUMBER) IS
6083 -- B# 6907814 ...  SELECT invoice_num, invoice_date, invoice_type_lookup_code
6084     SELECT invoice_num, invoice_type_lookup_code, invoice_date
6085       FROM ap_invoices_all
6086      WHERE invoice_id = c_tax_related_invoice_id;
6087 
6088     l_related_inv_application_id
6089       zx_trx_headers_gt.related_doc_application_id%TYPE;
6090     l_related_inv_entity_code
6094     l_related_inv_number         ap_invoices_all.invoice_num%TYPE;
6091       zx_trx_headers_gt.related_doc_entity_code%TYPE;
6092     l_related_event_class_code
6093       zx_trx_headers_gt.related_doc_event_class_code%TYPE;
6095     l_related_inv_date           ap_invoices_all.invoice_date%TYPE;
6096     l_related_inv_type           ap_invoices_all.invoice_type_lookup_code%TYPE;
6097 
6098     l_precision                  fnd_currencies.precision%TYPE;
6099     l_minimum_accountable_unit   fnd_currencies.minimum_accountable_unit%TYPE;
6100     l_doc_seq_name		 fnd_document_sequences.db_sequence_name%TYPE;
6101 
6102     l_return_status              BOOLEAN := TRUE;
6103     l_api_name                  VARCHAR2(30) := 'Populate_headers_gt'; --bug 6321366
6104   BEGIN
6105 
6106     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Populate_Headers_GT<-' ||
6107                                P_calling_sequence;
6108 
6109     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6110        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_curr_calling_sequence);
6111     END IF;
6112 
6113     -------------------------------------------------------------------
6114     l_debug_info := 'Step 1: Get event class code';
6115 
6116     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6117        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6118     END IF;
6119     -------------------------------------------------------------------
6120 
6121     IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
6122       P_Invoice_Type_Lookup_Code => P_Invoice_Header_Rec.invoice_type_lookup_code,
6123       P_Event_Class_Code         => P_event_class_code,
6124       P_error_code               => P_error_code,
6125       P_calling_sequence         => l_curr_calling_sequence)) THEN
6126 
6127       l_return_status := FALSE;
6128 
6129     END IF;
6130 
6131     -------------------------------------------------------------------
6132     l_debug_info := 'Step 2: Get event type code';
6133     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6134        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6135     END IF;
6136     -------------------------------------------------------------------
6137 
6138     IF (l_return_status = TRUE) THEN
6139       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
6140         P_Event_Class_Code          => P_event_class_code,
6141         P_Calling_Mode              => P_Calling_Mode,
6142         P_eTax_Already_called_flag  => P_eTax_Already_called_flag,
6143         P_Event_Type_Code           => P_Event_Type_Code,
6144         P_Error_Code                => P_error_code,
6145         P_Calling_Sequence          => l_curr_calling_sequence)) THEN
6146 
6147         l_return_status := FALSE;
6148       END IF;
6149     END IF;
6150 
6151     -------------------------------------------------------------------
6152     l_debug_info := 'Step 3: Populate product specific attributes';
6153     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6154        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6155     END IF;
6156     -------------------------------------------------------------------
6157     l_application_id := 200;   -- Oracle Payables
6158     -- The same code is used for all invoice types.
6159     l_entity_code := 'AP_INVOICES';
6160 
6161 
6162     -------------------------------------------------------------------
6163     l_debug_info := 'Step 4: Populate tax related invoice information '||
6164                     'if tax_related_invoice_id is not null';
6165 
6166     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6167        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6168     END IF;
6169     -------------------------------------------------------------------
6170     IF (l_return_status = TRUE AND
6171         P_Invoice_Header_Rec.tax_related_invoice_id IS NOT NULL) THEN
6172       l_related_inv_application_id := 200;  --Oracle Payables
6173       l_related_inv_entity_code := 'AP_INVOICES';
6174 
6175 
6176       BEGIN
6177         OPEN tax_related_invoice(P_Invoice_Header_Rec.tax_related_invoice_id);
6178         FETCH tax_related_invoice
6179           INTO l_related_inv_number, l_related_inv_type,
6180                l_related_inv_date;
6181         CLOSE tax_related_invoice;
6182       END;
6183 
6184       --------------------------------------------------------------------------
6185       l_debug_info := 'Step 5: Get event class code for tax_related_invoice_id';
6186       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6187          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6188       END IF;
6189       --------------------------------------------------------------------------
6190       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
6191         P_Invoice_Type_Lookup_Code => l_related_inv_type,
6192         P_Event_Class_Code         => l_related_event_class_code,
6193         P_error_code               => P_error_code,
6194         P_calling_sequence         => l_curr_calling_sequence)) THEN
6195 
6196         l_return_status := FALSE;
6197       END IF;
6198     END IF;
6199 
6200     -------------------------------------------------------------------
6201     l_debug_info := 'Step 6: Set quote flag based on calling_mode';
6202     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6203        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6204     END IF;
6205     -------------------------------------------------------------------
6206     IF (p_calling_mode = 'CALCULATE QUOTE') THEN
6207       l_quote_flag := 'Y';
6208 
6209     END IF;
6210 
6211     -------------------------------------------------------------------
6212     l_debug_info := 'Step 7: Get transaction currency details';
6216     -------------------------------------------------------------------
6213     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6214        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6215     END IF;
6217     IF (l_return_status = TRUE) THEN
6218       BEGIN
6219         SELECT NVL(precision, 0), NVL(minimum_accountable_unit,(1/power(10,precision)))
6220           INTO l_precision, l_minimum_accountable_unit
6221           FROM fnd_currencies
6222          WHERE currency_code = P_Invoice_Header_Rec.invoice_currency_code;
6223 
6224       END;
6225     END IF;
6226 
6227     -------------------------------------------------------------------
6228     l_debug_info := 'Step 8: Get doc_sequence_name';
6229     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6230        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6231     END IF;
6232     -------------------------------------------------------------------
6233     IF (l_return_status = TRUE
6234         and p_invoice_header_rec.doc_sequence_id is not null) THEN
6235       BEGIN
6236         SELECT name
6237           INTO l_doc_seq_name
6238           FROM fnd_document_sequences
6239          WHERE doc_sequence_id = p_invoice_header_rec.doc_sequence_id;
6240 
6241       EXCEPTION
6242 	WHEN OTHERS THEN
6243 	     Null;
6244       END;
6245     END IF;
6246 
6247     -------------------------------------------------------------------
6248     l_debug_info := 'Step 9: Populate zx_trx_headers_gt';
6249     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6250        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6251     END IF;
6252     -------------------------------------------------------------------
6253     IF (l_return_status = TRUE) THEN
6254 
6255       DELETE FROM zx_trx_headers_gt
6256        WHERE application_id   = AP_ETAX_PKG.AP_APPLICATION_ID
6257          AND entity_code      = AP_ETAX_PKG.AP_ENTITY_CODE
6258          AND event_class_code IN (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
6259                                   AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
6260                                   AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
6261          AND trx_id = p_invoice_header_rec.invoice_id;
6262 
6263       INSERT INTO zx_trx_headers_gt(
6264         internal_organization_id,
6265         internal_org_location_id,
6266         application_id,
6267         entity_code,
6268         event_class_code,
6269         event_type_code,
6270         trx_id,
6271         hdr_trx_user_key1,
6272         hdr_trx_user_key2,
6273         hdr_trx_user_key3,
6274         hdr_trx_user_key4,
6275         hdr_trx_user_key5,
6276         hdr_trx_user_key6,
6277         trx_date,
6278         trx_doc_revision,
6279         ledger_id,
6280         trx_currency_code,
6281         currency_conversion_date,
6282         currency_conversion_rate,
6283         currency_conversion_type,
6284         minimum_accountable_unit,
6285         precision,
6286         legal_entity_id,
6287         rounding_ship_to_party_id,
6288         rounding_ship_from_party_id,
6289         rounding_bill_to_party_id,
6290         rounding_bill_from_party_id,
6291         rndg_ship_to_party_site_id,
6292         rndg_ship_from_party_site_id,
6293         rndg_bill_to_party_site_id,
6294         rndg_bill_from_party_site_id,
6295         establishment_id,
6296         receivables_trx_type_id,
6297         related_doc_application_id,
6298         related_doc_entity_code,
6299         related_doc_event_class_code,
6300         related_doc_trx_id,
6301         rel_doc_hdr_trx_user_key1,
6302         rel_doc_hdr_trx_user_key2,
6303         rel_doc_hdr_trx_user_key3,
6304         rel_doc_hdr_trx_user_key4,
6305         rel_doc_hdr_trx_user_key5,
6306         rel_doc_hdr_trx_user_key6,
6307         related_doc_number,
6308         related_doc_date,
6309         default_taxation_country,
6310         quote_flag,
6311         ctrl_total_hdr_tx_amt,
6312         trx_number,
6313         trx_description,
6314         trx_communicated_date,
6315         batch_source_id,
6316         batch_source_name,
6317         doc_seq_id,
6318         doc_seq_name,
6319         doc_seq_value,
6320         trx_due_date,
6321         trx_type_description,
6322         document_sub_type,
6323         supplier_tax_invoice_number,
6324         supplier_tax_invoice_date,
6325         supplier_exchange_rate,
6326         tax_invoice_date,
6327         tax_invoice_number,
6328         tax_event_class_code,
6329         tax_event_type_code,
6330         doc_event_status,
6331         rdng_ship_to_pty_tx_prof_id,
6332         rdng_ship_from_pty_tx_prof_id,
6333         rdng_bill_to_pty_tx_prof_id,
6334         rdng_bill_from_pty_tx_prof_id,
6335         rdng_ship_to_pty_tx_p_st_id,
6336         rdng_ship_from_pty_tx_p_st_id,
6337         rdng_bill_to_pty_tx_p_st_id,
6338         rdng_bill_from_pty_tx_p_st_id,
6339         bill_third_pty_acct_id,
6340         bill_third_pty_acct_site_id,
6341 	ship_third_pty_acct_id,
6342 	ship_third_pty_acct_site_id
6343         ) VALUES (
6344         p_invoice_header_rec.org_id,                       --internal_organization_id
6345         NULL,                                              --internal_org_location_id
6346         l_application_id,                                  --application_id
6347         l_entity_code,                                     --entity_code
6348         P_event_class_code,                                --event_class_code
6349         P_event_type_code,                                 --event_type_code
6353         NULL,                                              --hdr_trx_user_key3
6350         p_invoice_header_rec.invoice_id,                   --trx_id
6351         NULL,                                              --hdr_trx_user_key1
6352         NULL,                                              --hdr_trx_user_key2
6354         NULL,                                              --hdr_trx_user_key4
6355         NULL,                                              --hdr_trx_user_key5
6356         NULL,                                              --hdr_trx_user_key6
6357         p_invoice_header_rec.invoice_date,                 --trx_date
6358         NULL,                                              --trx_doc_revision
6359         p_invoice_header_rec.set_of_books_id,              --ledger_id
6360         p_invoice_header_rec.invoice_currency_code,        --trx_currency_code
6361         p_invoice_header_rec.exchange_date,                --currency_conversion_date
6362         p_invoice_header_rec.exchange_rate,                --currency_conversion_rate
6363         p_invoice_header_rec.exchange_rate_type,           --currency_conversion_type
6364         l_minimum_accountable_unit,                        --minimum_accountable_unit
6365         l_precision,                                       --precision
6366         p_invoice_header_rec.legal_entity_id,              --legal_entity_id
6367         NULL,                                              --rounding_ship_to_party_id
6368         p_invoice_header_rec.party_id,                     --rounding_ship_from_party_id
6369         NULL,                                              --rounding_bill_to_party_id
6370         p_invoice_header_rec.party_id,                     --rounding_bill_from_party_id
6371         NULL,                                              --rndg_ship_to_party_site_id
6372         p_invoice_header_rec.party_site_id,                --rndg_ship_from_party_site_id
6373         NULL,                                              --rndg_bill_to_party_site_id
6374         p_invoice_header_rec.party_site_id,                --rndg_bill_from_party_site_id
6375         NULL,                                              --establishment_id
6376         NULL,                                              --receivables_trx_type_id
6377         l_related_inv_application_id,                      --related_doc_application_id
6378         l_related_inv_entity_code,                         --related_doc_entity_code
6379         l_related_event_class_code,                        --related_doc_event_class_code
6380         p_invoice_header_rec.tax_related_invoice_id,       --related_doc_trx_id
6381         NULL,                                              --rel_doc_hdr_trx_user_key1
6382         NULL,                                              --rel_doc_hdr_trx_user_key2
6383         NULL,                                              --rel_doc_hdr_trx_user_key3
6384         NULL,                                              --rel_doc_hdr_trx_user_key4
6385         NULL,                                              --rel_doc_hdr_trx_user_key5
6386         NULL,                                              --rel_doc_hdr_trx_user_key6
6387         l_related_inv_number,                              --related_doc_number
6388         l_related_inv_date,                                --related_doc_date
6389         p_invoice_header_rec.taxation_country,             --default_taxation_country
6390         l_quote_flag,                                      --quote_flag
6391         p_invoice_header_rec.control_amount,               --ctrl_total_hdr_tx_amt
6392         p_invoice_header_rec.invoice_num,                  --trx_number
6393         p_invoice_header_rec.description,                  --trx_description
6394         NULL,                                              --trx_communicated_date
6395         NULL,                                              --batch_source_id
6396         NULL,                                              --batch_source_name
6397         p_invoice_header_rec.doc_sequence_id,              --doc_seq_id
6398         l_doc_seq_name,            			   --doc_seq_name
6399         nvl(to_char(p_invoice_header_rec.doc_sequence_value),--bug6656894
6400 	    p_invoice_header_rec.voucher_num),             --doc_seq_value
6401         NULL,                                              --trx_due_date
6402         NULL,                                              --trx_type_description
6403         p_invoice_header_rec.document_sub_type,            --document_sub_type
6404         p_invoice_header_rec.supplier_tax_invoice_number,  --supplier_tax_invoice_number
6405         p_invoice_header_rec.supplier_tax_invoice_date,    --supplier_tax_invoice_date
6406         p_invoice_header_rec.supplier_tax_exchange_rate,   --supplier_exchange_rate
6407         p_invoice_header_rec.tax_invoice_recording_date,   --tax_invoice_date
6408         p_invoice_header_rec.tax_invoice_internal_seq,     --tax_invoice_number
6409         NULL,                                              --tax_event_class_code
6410         NULL,                                              --tax_event_type_code
6411         NULL,                                              --doc_event_status
6412         NULL,                                              --rdng_ship_to_pty_tx_prof_id
6413         NULL,                                              --rdng_ship_from_pty_tx_prof_id
6414         NULL,                                              --rdng_bill_to_pty_tx_prof_id
6415         NULL,                                              --rdng_bill_from_pty_tx_prof_id
6416         NULL,                                              --rdng_ship_to_pty_tx_p_st_id
6417         NULL,                                              --rdng_ship_from_pty_tx_p_st_id
6418         NULL,                                              --rdng_bill_to_pty_tx_p_st_id
6419         NULL,                                              --rdng_bill_from_pty_tx_p_st_id
6420 	p_invoice_header_rec.vendor_id,			   --bill_third_pty_acct_id
6421 	p_invoice_header_rec.vendor_site_id,		   --bill_third_pty_acct_site_id
6422         p_invoice_header_rec.vendor_id,                    --ship_third_pty_acct_id
6423         p_invoice_header_rec.vendor_site_id                --ship_third_pty_acct_site_id
6424      );
6425 
6426      -- Global Variable g_invoices_to_process should be initialized right after
6430 
6427      -- the previous insert. No other sql statements must be placed after the
6428      -- insert because the sql rowcount will be reset. This variable is used in
6429      -- calculate_tax and determine_recovery.
6431      g_invoices_to_process := sql%rowcount;
6432 
6433      -- Added log messages for bug 6321366
6434      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6435       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_trx_headers_gt values ');
6436       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Application_id: '|| l_application_id);
6437       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Entity_code: ' || l_entity_code);
6438       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: ' || P_event_class_code);
6439       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_type_code: ' ||P_event_type_code);
6440       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Invoice_id: '|| p_invoice_header_rec.invoice_id);
6441      END IF;
6442 
6443    END IF;
6444 
6445    RETURN l_return_status;
6446 
6447   EXCEPTION
6448     WHEN OTHERS THEN
6449       IF (SQLCODE <> -20001) THEN
6450         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
6451         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
6452         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
6453         FND_MESSAGE.SET_TOKEN('PARAMETERS',
6454           ' P_Invoice_Header_Rec = '||P_Invoice_Header_Rec.Invoice_Id||
6455           ' P_Calling_Mode ='||P_Calling_Mode||
6456           ' P_Error_Code = '||P_Error_Code||
6457           ' P_Calling_Sequence = '||P_Calling_Sequence);
6458         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
6459       END IF;
6460 
6461       IF ( tax_related_invoice%ISOPEN ) THEN
6462         CLOSE tax_related_invoice;
6463       END IF;
6464 
6465       APP_EXCEPTION.RAISE_EXCEPTION;
6466 
6467   END Populate_Headers_GT;
6468 
6469 /*=============================================================================
6470  |  FUNCTION - Populate_Header_Import_GT()
6471  |
6472  |  DESCRIPTION
6473  |    This function will get additional information required to populate the
6474  |    zx_trx_headers_gt from the import array structure.
6475  |    This function returns TRUE if the insert to the temp table goes
6476  |    through successfully.  Otherwise, FALSE.
6477  |
6478  |  PARAMETERS
6479  |    P_Invoice_Header_Rec - record with invoice header information
6480  |    P_Calling_Mode - calling mode. it is used to
6481  |    P_Event_Class_Code - Event class code
6482  |    P_Event_Type_Code - Event type code
6483  |    P_error_code - Error code to be returned
6484  |    P_calling_sequence -  Calling sequence
6485  |
6486  |  MODIFICATION HISTORY
6487  |    DATE          Author         Action
6488  |    20-JAN-2004   SYIDNER        Created
6489  |
6490 *============================================================================*/
6491   FUNCTION Populate_Header_Import_GT(
6492              P_Invoice_Header_Rec        IN AP_IMPORT_INVOICES_PKG.r_invoice_info_rec,
6493              P_Calling_Mode              IN VARCHAR2,
6494              P_Event_Class_Code          OUT NOCOPY VARCHAR2,
6495              P_Event_Type_Code           OUT NOCOPY VARCHAR2,
6496              P_Error_Code                OUT NOCOPY VARCHAR2,
6497              P_Calling_Sequence          IN VARCHAR2) RETURN BOOLEAN
6498   IS
6499 
6500     l_debug_info                 VARCHAR2(240);
6501     l_curr_calling_sequence      VARCHAR2(4000);
6502 
6503     l_application_id             zx_trx_headers_gt.application_id%TYPE;
6504     l_entity_code                zx_trx_headers_gt.entity_code%TYPE;
6505 
6506     l_quote_flag                 zx_trx_headers_gt.quote_flag%TYPE
6507       := 'N';
6508     -- This flag is always N except when the calculate service is called for
6509     -- quote for the recurring invoices and distributions sets.
6510 
6511     CURSOR tax_related_invoice( c_tax_related_invoice_id IN NUMBER) IS
6512 -- B# 6907814 ...  SELECT invoice_num, invoice_date, invoice_type_lookup_code
6513     SELECT invoice_num, invoice_type_lookup_code, invoice_date
6514       FROM ap_invoices_all
6515      WHERE invoice_id = c_tax_related_invoice_id;
6516 
6517     l_related_inv_application_id
6518       zx_trx_headers_gt.related_doc_application_id%TYPE;
6519     l_related_inv_entity_code
6520       zx_trx_headers_gt.related_doc_entity_code%TYPE;
6521     l_related_event_class_code
6522       zx_trx_headers_gt.related_doc_event_class_code%TYPE;
6523     l_related_inv_number         ap_invoices_all.invoice_num%TYPE;
6524     l_related_inv_date           ap_invoices_all.invoice_date%TYPE;
6525     l_related_inv_type           ap_invoices_all.invoice_type_lookup_code%TYPE;
6526 
6527     l_precision                  fnd_currencies.precision%TYPE := 0;
6528     l_minimum_accountable_unit   fnd_currencies.minimum_accountable_unit%TYPE;
6529 
6530     l_return_status              BOOLEAN := TRUE;
6531     l_api_name                  VARCHAR2(30) := 'populate_header_import_gt';
6532 
6533   BEGIN
6534 
6535     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Populate_Header_Import_GT<-'||
6536                                P_calling_sequence;
6537 
6538 
6539     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6540        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_curr_calling_sequence);
6541     END IF;
6542 
6543     -------------------------------------------------------------------
6544     l_debug_info := 'Step 1: Get event class code';
6545     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6546        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6547     END IF;
6548     -------------------------------------------------------------------
6549 
6550     IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
6554       P_calling_sequence         => l_curr_calling_sequence)) THEN
6551       P_Invoice_Type_Lookup_Code => P_Invoice_Header_Rec.invoice_type_lookup_code,
6552       P_Event_Class_Code         => P_event_class_code,
6553       P_error_code               => P_error_code,
6555 
6556       l_return_status := FALSE;
6557 
6558     END IF;
6559 
6560     -------------------------------------------------------------------
6561     l_debug_info := 'Step 2: Get event type code';
6562     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6563        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6564     END IF;
6565     -------------------------------------------------------------------
6566     -- Since this procedure will be called only from the import program
6567     -- it is the first time eTax is call so the etax_already_called_flag is N
6568 
6569     IF (l_return_status = TRUE) THEN
6570       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
6571         P_Event_Class_Code          => P_event_class_code,
6572         P_Calling_Mode              => P_Calling_Mode,
6573         P_eTax_Already_called_flag  => 'N',
6574         P_Event_Type_Code           => P_Event_Type_Code,
6575         P_Error_Code                => P_error_code,
6576         P_Calling_Sequence          => l_curr_calling_sequence)) THEN
6577 
6578         l_return_status := FALSE;
6579       END IF;
6580     END IF;
6581 
6582     -------------------------------------------------------------------
6583     l_debug_info := 'Step 3: Populate product specific attributes';
6584     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6585        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6586     END IF;
6587     -------------------------------------------------------------------
6588     l_application_id := 200;   -- Oracle Payables
6589     -- The same code is used for all invoice types.
6590     l_entity_code := 'AP_INVOICES';
6591 
6592     -------------------------------------------------------------------
6593     l_debug_info := 'Step 4: Populate tax related invoice information '||
6594                     'if tax_related_invoice_id is not null';
6595 
6596     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6597        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6598     END IF;
6599     -------------------------------------------------------------------
6600     IF (l_return_status = TRUE AND
6601         P_Invoice_Header_Rec.tax_related_invoice_id IS NOT NULL) THEN
6602 
6603       -- At this moment we are sure the tax_related_invoice_id is valid.
6604       -- It was validated in the previous to this call in the import
6605       -- process.
6606 
6607       l_related_inv_application_id := 200;  --Oracle Payables
6608       l_related_inv_entity_code := 'AP_INVOICES';
6609 
6610       BEGIN
6611         OPEN tax_related_invoice(P_Invoice_Header_Rec.tax_related_invoice_id);
6612         FETCH tax_related_invoice
6613           INTO l_related_inv_number, l_related_inv_type,
6614                l_related_inv_date;
6615         CLOSE tax_related_invoice;
6616       END;
6617 
6618       --------------------------------------------------------------------------
6619       l_debug_info := 'Step 5: Get event class code for tax_related_invoice_id';
6620 
6621       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6622          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6623       END IF;
6624       --------------------------------------------------------------------------
6625       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
6626         P_Invoice_Type_Lookup_Code => l_related_inv_type,
6627         P_Event_Class_Code         => l_related_event_class_code,
6628         P_error_code               => P_error_code,
6629         P_calling_sequence         => l_curr_calling_sequence)) THEN
6630 
6631         l_return_status := FALSE;
6632       END IF;
6633     END IF;
6634 
6635     -------------------------------------------------------------------
6636     l_debug_info := 'Step 6: Get transaction currency details';
6637     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6638        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6639     END IF;
6640     -------------------------------------------------------------------
6641     IF (l_return_status = TRUE) THEN
6642       BEGIN
6643         SELECT NVL(precision, 0), NVL(minimum_accountable_unit,(1/power(10,precision)))
6644           INTO l_precision, l_minimum_accountable_unit
6648       END;
6645           FROM fnd_currencies
6646          WHERE currency_code = P_Invoice_Header_Rec.invoice_currency_code;
6647 
6649     END IF;
6650 
6651     -------------------------------------------------------------------
6652     l_debug_info := 'Step 8: Populate zx_trx_headers_gt';
6653     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6654        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6655     END IF;
6656     -------------------------------------------------------------------
6657     IF (l_return_status = TRUE) THEN
6658 
6659       DELETE FROM zx_trx_headers_gt
6660        WHERE application_id   = AP_ETAX_PKG.AP_APPLICATION_ID
6661          AND entity_code      = AP_ETAX_PKG.AP_ENTITY_CODE
6662          AND event_class_code IN (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
6663                                   AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
6664                                   AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
6665          AND trx_id = p_invoice_header_rec.invoice_id;
6666 
6667       INSERT INTO zx_trx_headers_gt(
6668         internal_organization_id,
6669         internal_org_location_id,
6670         application_id,
6671         entity_code,
6672         event_class_code,
6673         event_type_code,
6674         trx_id,
6675         hdr_trx_user_key1,
6676         hdr_trx_user_key2,
6677         hdr_trx_user_key3,
6678         hdr_trx_user_key4,
6679         hdr_trx_user_key5,
6680         hdr_trx_user_key6,
6681         trx_date,
6682         trx_doc_revision,
6683         ledger_id,
6684         trx_currency_code,
6685         currency_conversion_date,
6686         currency_conversion_rate,
6687         currency_conversion_type,
6688         minimum_accountable_unit,
6689         precision,
6690         legal_entity_id,
6691         rounding_ship_to_party_id,
6692         rounding_ship_from_party_id,
6693         rounding_bill_to_party_id,
6694         rounding_bill_from_party_id,
6695         rndg_ship_to_party_site_id,
6696         rndg_ship_from_party_site_id,
6697         rndg_bill_to_party_site_id,
6698         rndg_bill_from_party_site_id,
6699         establishment_id,
6700         receivables_trx_type_id,
6701         related_doc_application_id,
6702         related_doc_entity_code,
6703         related_doc_event_class_code,
6704         related_doc_trx_id,
6705         rel_doc_hdr_trx_user_key1,
6706         rel_doc_hdr_trx_user_key2,
6707         rel_doc_hdr_trx_user_key3,
6708         rel_doc_hdr_trx_user_key4,
6709         rel_doc_hdr_trx_user_key5,
6710         rel_doc_hdr_trx_user_key6,
6711         related_doc_number,
6712         related_doc_date,
6713         default_taxation_country,
6714         quote_flag,
6715         ctrl_total_hdr_tx_amt,
6716         trx_number,
6717         trx_description,
6718         trx_communicated_date,
6719         batch_source_id,
6720         batch_source_name,
6721         doc_seq_id,
6722         doc_seq_name,
6723         doc_seq_value,
6724         trx_due_date,
6725         trx_type_description,
6726         document_sub_type,
6727         supplier_tax_invoice_number,
6728         supplier_tax_invoice_date,
6729         supplier_exchange_rate,
6730         tax_invoice_date,
6731         tax_invoice_number,
6732         tax_event_class_code,
6733         tax_event_type_code,
6734         doc_event_status,
6735         rdng_ship_to_pty_tx_prof_id,
6736         rdng_ship_from_pty_tx_prof_id,
6737         rdng_bill_to_pty_tx_prof_id,
6738         rdng_bill_from_pty_tx_prof_id,
6739         rdng_ship_to_pty_tx_p_st_id,
6740         rdng_ship_from_pty_tx_p_st_id,
6741         rdng_bill_to_pty_tx_p_st_id,
6742         rdng_bill_from_pty_tx_p_st_id,
6743         bill_third_pty_acct_id,
6744         bill_third_pty_acct_site_id,
6745         ship_third_pty_acct_id,
6746 	ship_third_pty_acct_site_id
6747         ) VALUES (
6748         p_invoice_header_rec.org_id,                       --internal_organization_id
6749         NULL,                                              --internal_org_location_id
6750         l_application_id,                                  --application_id
6751         l_entity_code,                                     --entity_code
6752         P_event_class_code,                                --event_class_code
6753         P_event_type_code,                                 --event_type_code
6754         p_invoice_header_rec.invoice_id,                   --trx_id
6755         NULL,                                              --hdr_trx_user_key1
6756         NULL,                                              --hdr_trx_user_key2
6757         NULL,                                              --hdr_trx_user_key3
6758         NULL,                                              --hdr_trx_user_key4
6759         NULL,                                              --hdr_trx_user_key5
6760         NULL,                                              --hdr_trx_user_key6
6761         p_invoice_header_rec.invoice_date,                 --trx_date
6762         NULL,                                              --trx_doc_revision
6763         p_invoice_header_rec.set_of_books_id,              --ledger_id
6764         p_invoice_header_rec.invoice_currency_code,        --trx_currency_code
6765         p_invoice_header_rec.exchange_date,                --currency_conversion_date
6766         p_invoice_header_rec.exchange_rate,                --currency_conversion_rate
6767         p_invoice_header_rec.exchange_rate_type,           --currency_conversion_type
6768         l_minimum_accountable_unit,                        --minimum_accountable_unit
6769         l_precision,                                       --precision
6770         p_invoice_header_rec.legal_entity_id,              --legal_entity_id
6771         NULL,                                              --rounding_ship_to_party_id
6772         p_invoice_header_rec.party_id,                     --rounding_ship_from_party_id
6773         NULL,                                              --rounding_bill_to_party_id
6774         p_invoice_header_rec.party_id,                     --rounding_bill_from_party_id
6775         NULL,                                              --rndg_ship_to_party_site_id
6776         p_invoice_header_rec.party_site_id,                --rndg_ship_from_party_site_id
6777         NULL,                                              --rndg_bill_to_party_site_id
6778         p_invoice_header_rec.party_site_id,                --rndg_bill_from_party_site_id
6779         NULL,                                              --establishment_id
6780         NULL, --receivables_trx_type_id
6781         l_related_inv_application_id, --related_doc_application_id
6782         l_related_inv_entity_code, --related_doc_entity_code
6783         l_related_event_class_code, --related_doc_event_class_code
6784         p_invoice_header_rec.tax_related_invoice_id,       --related_doc_trx_id
6785         NULL, --rel_doc_hdr_trx_user_key1
6786         NULL, --rel_doc_hdr_trx_user_key2
6787         NULL, --rel_doc_hdr_trx_user_key3
6788         NULL, --rel_doc_hdr_trx_user_key4
6789         NULL, --rel_doc_hdr_trx_user_key5
6790         NULL, --rel_doc_hdr_trx_user_key6
6791         l_related_inv_number,                              --related_doc_number
6792         l_related_inv_date,                                --related_doc_date
6793         p_invoice_header_rec.taxation_country, --default_taxation_country
6794         l_quote_flag,                                      --quote_flag
6795         p_invoice_header_rec.control_amount, --ctrl_total_hdr_tx_amt
6796         p_invoice_header_rec.invoice_num,                  --trx_number
6797         p_invoice_header_rec.description,                  --trx_description
6798         NULL, --trx_communicated_date
6799         NULL,                                              --batch_source_id
6800         NULL,                                              --batch_source_name
6801         NULL,                                              --doc_seq_id
6802         NULL,                                              --doc_seq_name
6803         NULL,                                              --doc_seq_value
6804         NULL,                                              --trx_due_date
6805         NULL, --trx_type_description
6806         p_invoice_header_rec.document_sub_type,            --document_sub_type
6807         p_invoice_header_rec.supplier_tax_invoice_number, --supplier_tax_invoice_number
6808         p_invoice_header_rec.supplier_tax_invoice_date, --supplier_tax_invoice_date
6809         p_invoice_header_rec.supplier_tax_exchange_rate, --supplier_exchange_rate
6810         p_invoice_header_rec.tax_invoice_recording_date,   --tax_invoice_date
6811         p_invoice_header_rec.tax_invoice_internal_seq,     --tax_invoice_number
6812         NULL, --tax_event_class_code
6813         NULL,                                              --tax_event_type_code
6814         NULL,                                              --doc_event_status
6815         NULL, --rdng_ship_to_pty_tx_prof_id
6816         NULL, --rdng_ship_from_pty_tx_prof_id
6817         NULL, --rdng_bill_to_pty_tx_prof_id
6818         NULL, --rdng_bill_from_pty_tx_prof_id
6819         NULL, --rdng_ship_to_pty_tx_p_st_id
6820         NULL, --rdng_ship_from_pty_tx_p_st_id
6821         NULL, --rdng_bill_to_pty_tx_p_st_id
6822         NULL, --rdng_bill_from_pty_tx_p_st_id
6823         p_invoice_header_rec.vendor_id,			   --bill_third_pty_acct_id
6824         p_invoice_header_rec.vendor_site_id,               --bill_third_pty_acct_site_id
6825         p_invoice_header_rec.vendor_id,                    --ship_third_pty_acct_id
6826         p_invoice_header_rec.vendor_site_id                --ship_third_pty_acct_site_id
6827      );
6828 --Log messages added for bug 6321366
6829   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6830       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_trx_headers_gt values ');
6831       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Application_id: '|| l_application_id);
6832       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Entity_code: ' || l_entity_code);
6833       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: ' || P_event_class_code);
6834       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_type_code: ' ||P_event_type_code);
6835       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Invoice_id: '|| p_invoice_header_rec.invoice_id);
6836   END IF;
6837 
6838    END IF;
6839 
6840    RETURN l_return_status;
6841 
6842   EXCEPTION
6843     WHEN OTHERS THEN
6844       IF (SQLCODE <> -20001) THEN
6845         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
6846         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
6847         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
6848         FND_MESSAGE.SET_TOKEN('PARAMETERS',
6849           ' P_Invoice_Header_Rec = '||P_Invoice_Header_Rec.Invoice_Id||
6850           ' P_Calling_Mode ='||P_Calling_Mode||
6851           ' P_Error_Code = '||P_Error_Code||
6852           ' P_Calling_Sequence = '||P_Calling_Sequence);
6853         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
6854       END IF;
6855 
6856       IF ( tax_related_invoice%ISOPEN ) THEN
6857         CLOSE tax_related_invoice;
6858       END IF;
6859 
6860       APP_EXCEPTION.RAISE_EXCEPTION;
6861 
6862   END Populate_Header_Import_GT;
6863 
6864 
6865 /*=============================================================================
6866  |  FUNCTION - Populate_Lines_GT()
6867  |
6868  |  DESCRIPTION
6869  |      This function will get additional information required to populate the
6870  |      ZX_TRANSACTION_LINES_GT
6871  |      This function returns TRUE if the population of the temp table goes
6872  |      through successfully.  Otherwise, FALSE.
6873  |
6874  |  PARAMETERS
6875  |      P_Invoice_Header_Rec - record with invoice header information
6876  |      P_Calling_Mode - calling mode. it is used to
6877  |      P_Event_Class_Code - Event class code for document
6878  |      P_Line_Number - prepay line number to be unapplied.
6879  |      P_error_code - Error code to be returned
6880  |      P_calling_sequence -  Calling sequence
6881  |
6882  |  MODIFICATION HISTORY
6883  |    DATE          Author         Action
6884  |    09-OCT-2003   SYIDNER        Created
6885  |    03-MAR-2004   SYIDNER        Including prepayment
6886  |                                 application/unapplication functionality
6887  |
6888  *============================================================================*/
6889 
6890   FUNCTION Populate_Lines_GT(
6891              P_Invoice_Header_Rec      IN ap_invoices_all%ROWTYPE,
6892              P_Calling_Mode            IN VARCHAR2,
6893              P_Event_Class_Code        IN VARCHAR2,
6894              P_Line_Number             IN NUMBER DEFAULT NULL,
6895              P_Error_Code              OUT NOCOPY VARCHAR2,
6896              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
6897   IS
6898 
6899     l_debug_info                 VARCHAR2(240);
6900     l_curr_calling_sequence      VARCHAR2(4000);
6901 
6902     -- This structure to populate all the lines information previous to insert
6903     -- in eTax global temporary table.
6904     TYPE Trans_Lines_Tab_Type IS TABLE OF zx_transaction_lines_gt%ROWTYPE;
6905     trans_lines                    Trans_Lines_Tab_Type := Trans_Lines_Tab_Type();
6906 
6907     l_application_id               zx_trx_headers_gt.application_id%TYPE;
6908     l_ctrl_hdr_tx_appl_flag        zx_transaction_lines_gt.ctrl_hdr_tx_appl_flag%TYPE;
6909     l_line_level_action            zx_transaction_lines_gt.line_level_action%TYPE;
6910     l_line_class		   zx_transaction_lines_gt.line_class%TYPE;
6911     l_line_amt_includes_tax_flag   zx_transaction_lines_gt.line_amt_includes_tax_flag%TYPE;
6912     l_init_line_amt_incl_tax_fg    zx_transaction_lines_gt.line_amt_includes_tax_flag%TYPE;
6913     l_product_org_id               zx_transaction_lines_gt.product_org_id%TYPE;
6914     l_bill_to_location_id          zx_transaction_lines_gt.bill_to_location_id%TYPE;
6915 
6916 
6917     -- Purchase Order Info
6918     l_ref_doc_application_id      zx_transaction_lines_gt.ref_doc_application_id%TYPE;
6919     l_ref_doc_entity_code	  zx_transaction_lines_gt.ref_doc_entity_code%TYPE;
6920     l_ref_doc_event_class_code    zx_transaction_lines_gt.ref_doc_event_class_code%TYPE;
6921     l_ref_doc_line_quantity       zx_transaction_lines_gt.ref_doc_line_quantity%TYPE;
6922     l_po_header_curr_conv_rat     po_headers_all.rate%TYPE;
6923     l_ref_doc_trx_level_type      zx_transaction_lines_gt.ref_doc_trx_level_type%TYPE;
6924     l_po_header_curr_conv_rate    po_headers_all.rate%TYPE;
6925     l_uom_code			  mtl_units_of_measure.uom_code%TYPE;
6926     l_dummy			  number;
6927 
6928 
6929     -- Receipt Info
6930     l_applied_to_application_id    zx_transaction_lines_gt.applied_to_application_id%TYPE;
6931     l_applied_to_entity_code       zx_transaction_lines_gt.applied_to_entity_code%TYPE;
6932     l_applied_to_event_class_code  zx_transaction_lines_gt.applied_to_event_class_code%TYPE;
6933     l_trx_receipt_date             zx_transaction_lines_gt.trx_receipt_date%TYPE;
6934     l_ref_doc_trx_id               zx_transaction_lines_gt.ref_doc_trx_id%TYPE;
6935 
6936     -- Prepayment Info
6937     l_prepay_doc_application_id    zx_transaction_lines_gt.applied_from_application_id%TYPE;
6938     l_prepay_doc_entity_code       zx_transaction_lines_gt.applied_from_entity_code%TYPE;
6939     l_prepay_doc_event_class_code  zx_transaction_lines_gt.applied_from_event_class_code%TYPE;
6940     l_prepay_doc_number            ap_invoices_all.invoice_num%TYPE;
6941     l_prepay_doc_date              ap_invoices_all.invoice_date%TYPE;
6942     l_applied_from_trx_level_type  zx_transaction_lines_gt.applied_from_trx_level_type%TYPE;
6943     l_applied_from_trx_id	   zx_transaction_lines_gt.applied_from_trx_id%TYPE;
6944     l_applied_from_line_id         zx_transaction_lines_gt.applied_from_line_id%TYPE;
6945 
6946     -- Corrected Invoice Info
6947     l_adj_doc_application_id     zx_transaction_lines_gt.adjusted_doc_application_id%TYPE;
6948     l_adj_doc_entity_code	 zx_transaction_lines_gt.adjusted_doc_entity_code%TYPE;
6949     l_adj_doc_event_class_code   zx_transaction_lines_gt.adjusted_doc_event_class_code%TYPE;
6950     l_adj_doc_number             zx_transaction_lines_gt.adjusted_doc_number%TYPE;
6951     l_adj_doc_date               zx_transaction_lines_gt.adjusted_doc_date%TYPE;
6952     l_adj_doc_trx_level_type	 zx_transaction_lines_gt.adjusted_doc_trx_level_type%TYPE;
6953 
6954     l_fob_point                  po_vendor_sites_all.fob_lookup_code%TYPE;
6955     l_location_id		 zx_transaction_lines_gt.ship_from_location_id%type;
6956 
6957     l_dflt_tax_class_code       zx_transaction_lines_gt.input_tax_classification_code%type;
6958     l_allow_tax_code_override   varchar2(10);
6959 
6960     l_intended_use		zx_lines_det_factors.line_intended_use%type;
6961     l_product_type		zx_lines_det_factors.product_type%type;
6962     l_product_category		zx_lines_det_factors.product_category%type;
6963     l_product_fisc_class	zx_lines_det_factors.product_fisc_classification%type;
6964     l_user_defined_fisc_class	zx_lines_det_factors.user_defined_fisc_class%type;
6965     l_assessable_value		zx_lines_det_factors.assessable_value%type;
6966 
6967     l_default_ccid      	ap_invoice_lines_all.default_dist_ccid%TYPE; --Bug6908977
6968 
6969     l_return_status             BOOLEAN := TRUE;
6970 
6971     l_api_name                  VARCHAR2(30) := 'Populate_Lines_GT';
6972     l_tax_already_calculated_line  VARCHAR2(1);
6973 
6974     l_ship_to_party_id          po_line_locations_all.ship_to_organization_id%type; -- 7262269
6975 
6976      l_err varchar2(2000); --10140354
6977   BEGIN
6978 
6979     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Populate_Lines_GT<-' ||
6980                                P_calling_sequence;
6981 
6982     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6983        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_curr_calling_sequence);
6984     END IF;
6985 
6986     --Bug13899981    Added Debug Messages
6987     ----------------------------------------------------------------------
6988     l_debug_info := 'Populate Lines - Invoice Id '||p_invoice_header_rec.invoice_id;
6989     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6990        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6991     END IF;
6992     --Print(l_api_name,l_debug_info);
6993     ----------------------------------------------------------------------
6994 
6995     ----------------------------------------------------------------------
6996     l_debug_info := 'Set line defaults from cache';
6997     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6998        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
6999     END IF;
7000     --Print(l_api_name,l_debug_info);
7001     ----------------------------------------------------------------------
7002 
7003     ----------------------------------------------------------------------
7004     l_debug_info := 'Cache Index : Supplier Site '||p_invoice_header_rec.vendor_site_id;
7005     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7006        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7007     END IF;
7008     --Print(l_api_name,l_debug_info);
7009     ----------------------------------------------------------------------
7010 
7011     ----------------------------------------------------------------------
7012     l_debug_info := 'Cache Index : Party Site '||p_invoice_header_rec.party_site_id;
7013     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7014        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7015     END IF;
7016     --Print(l_api_name,l_debug_info);
7017     ----------------------------------------------------------------------
7018 
7019     ----------------------------------------------------------------------
7020     l_debug_info := 'Cache Index : Org Id '||p_invoice_header_rec.org_id;
7021     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7022        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7023     END IF;
7024     --Print(l_api_name,l_debug_info);
7025     ----------------------------------------------------------------------
7026 
7027     IF l_payment_request_flag ='N' THEN ---for bug 5967914
7028 
7029        ----------------------------------------------------------------------
7030        l_debug_info := 'Non Payment Request Invoice : Populate FOB Point';
7031        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7032            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7033        END IF;
7034        --Print(l_api_name,l_debug_info);
7035        ----------------------------------------------------------------------
7036 
7037         l_fob_point            := AP_ETAX_SERVICES_PKG.g_site_attributes
7038 					(p_invoice_header_rec.vendor_site_id).fob_lookup_code;
7039     END IF;
7040 
7041     IF l_payment_request_flag ='Y' THEN ---if condition added for bug 5967914
7042 
7043        ----------------------------------------------------------------------
7044        l_debug_info := 'Payment Request Invoice : Populate Location Id';
7045        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7046           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7047        END IF;
7048        --Print(l_api_name,l_debug_info);
7049        ----------------------------------------------------------------------
7050 
7051        l_location_id         := AP_ETAX_SERVICES_PKG.g_site_attributes
7052 					(p_invoice_header_rec.party_site_id).location_id;
7053     ELSE
7054 
7055        ----------------------------------------------------------------------
7056        l_debug_info := 'Non Payment Request Invoice : Populate Location Id';
7057        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7058           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7059        END IF;
7060        --Print(l_api_name,l_debug_info);
7061        ----------------------------------------------------------------------
7062 
7063        l_location_id         := AP_ETAX_SERVICES_PKG.g_site_attributes
7064 					(p_invoice_header_rec.vendor_site_id).location_id;
7065     END IF;
7066 
7067 
7068     ----------------------------------------------------------------------
7069     l_debug_info := 'For every invoice : Populate Bill To Location Id';
7070     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7071         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7072     END IF;
7073     --Print(l_api_name,l_debug_info);
7074     ----------------------------------------------------------------------
7075     --Bug13899981    Added Debug Messages
7076 
7077     l_bill_to_location_id := AP_ETAX_SERVICES_PKG.g_org_attributes
7078 					(p_invoice_header_rec.org_id).bill_to_location_id;
7079 
7080     ----------------------------------------------------------------------
7081     l_debug_info := 'Go through taxable lines';
7082     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7083        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7084     END IF;
7085     ----------------------------------------------------------------------
7086 
7087     IF ( l_inv_line_list.COUNT > 0) THEN
7088       -- For non-tax only lines
7089       trans_lines.EXTEND(l_inv_line_list.COUNT);
7090       FOR i IN l_inv_line_list.FIRST..l_inv_line_list.LAST LOOP
7091         -------------------------------------------------------------------
7092          l_debug_info := 'Get line_level_action for line number: '||l_inv_line_list(i).line_number;
7093          IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7094             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7095          END IF;
7096         -------------------------------------------------------------------
7097         IF (l_return_status = TRUE) THEN
7098          IF ( P_calling_mode = 'OVERRIDE TAX' ) THEN
7099            l_line_level_action := 'NO_CHANGE';
7100 
7101          ELSIF (l_inv_line_list(i).line_type_lookup_code = 'PREPAY'
7102                 or p_calling_mode = 'RECOUPMENT') THEN
7103 
7104            -- The treatment of PREPAY lines is different from a regular
7105            -- line.  We will differienciate the PREPAY line created
7109            -- Since the prepayment unapplication will discard the PREPAY
7106            -- for the prepayment application if no tax has been canculated
7107            -- for it since we call calculate tax during the prepayment
7108            -- application.
7110            -- line created during the application, there is no way to
7111            -- identify the PREPAY line to unapply if there is more than
7112            -- one unapplied PREPAY lines in the invoice.  For this reason
7113            -- the parameter used to calculate tax per line will be used.
7114 
7115            IF (P_calling_mode = 'APPLY PREPAY' or p_calling_mode = 'RECOUPMENT') THEN
7116              IF (NVL(l_inv_line_list(i).tax_already_calculated_flag, 'N') = 'N'
7117 		 or p_calling_mode = 'RECOUPMENT') THEN
7118                l_line_level_action := 'APPLY_FROM';
7119 
7120              ELSE
7121                l_line_level_action := 'NO_CHANGE';
7122 
7123              END IF;
7124 
7125            ELSIF (P_calling_mode = 'UNAPPLY PREPAY') THEN
7126              IF (l_inv_line_list(i).line_number = p_line_number) THEN
7127                 l_line_level_action := 'UNAPPLY_FROM';
7128 
7129              ELSE
7130                l_line_level_action := 'NO_CHANGE';
7131 
7132              END IF;
7133            ELSE
7134              l_line_level_action := 'NO_CHANGE';
7135 
7136            END IF;
7137 
7138          ELSIF ( NVL(l_inv_line_list(i).discarded_flag, 'N') = 'Y'
7139                  OR NVL(l_inv_line_list(i).cancelled_flag, 'N') = 'Y') THEN
7140 
7141                -- Bug 7444234
7142                -- line_level_action as DISCARD irrespective of migrated
7143                -- transaction.
7144 
7145                l_line_level_action := 'DISCARD';
7146 
7147 	       --IF NVL(l_inv_line_list(i).historical_flag, 'N') = 'Y' THEN
7148 	       --   l_line_level_action := 'UPDATE';
7149 	       --ELSE
7150                --   l_line_level_action := 'DISCARD';
7151 	       --END IF;
7152 
7153 	/*  --Bug12660669 Added source, invoice type and line type check */
7154          ELSIF (P_Invoice_Header_Rec.source = 'SelfService' AND
7155 		        P_Invoice_Header_Rec.invoice_type_lookup_code = 'EXPENSE REPORT' AND
7156                 l_inv_line_list(i).line_type_lookup_code = 'MISCELLANEOUS' ) THEN
7157 		--Bug16362966: Start
7158                 --l_line_level_action := 'NO_CHANGE' ;
7159                 IF (NVL(l_inv_line_list(i).tax_already_calculated_flag, 'N') = 'N') THEN
7160 	           l_line_level_action := 'RECORD_WITH_NO_TAX' ;
7161                 ELSE
7162                    l_line_level_action := 'NO_CHANGE';
7163                 END IF;
7164                 --Bug16362966: End
7165          ELSIF (NVL(l_inv_line_list(i).tax_already_calculated_flag, 'N') = 'Y') THEN
7166 	      -- Bug 9068689
7167 	      IF ( l_inv_line_list(i).included_tax_amount IS NOT NULL AND -- Bug 9526592 : Added this condition
7168 	           NOT AP_ETAX_UTILITY_PKG.Is_Incl_Tax_Driver_Updatable(
7169                                 p_invoice_id        => l_inv_line_list(i).invoice_id,
7170                                 p_line_number       => l_inv_line_list(i).line_number,
7171                                 p_calling_sequence  => l_curr_calling_sequence ) )
7172               THEN
7173 		  l_line_level_action := 'NO_CHANGE' ;
7174               ELSE
7175       -- Start for bug 6485124
7176                   l_line_level_action := 'UPDATE';
7177 	      END IF ;
7178          ELSE
7179             BEGIN
7180               SELECT 'Y'
7181               INTO   l_tax_already_calculated_line
7182               FROM   zx_lines_det_factors
7183               WHERE  application_id        =  200
7184               AND    entity_code           =  'AP_INVOICES'
7185               AND    event_class_code      IN ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
7186               AND    trx_id                = l_inv_line_list(i).invoice_id
7187               AND    trx_line_id           = l_inv_line_list(i).line_number
7188               AND    ROWNUM = 1;
7189 
7190                IF l_tax_already_calculated_line = 'Y' THEN
7191                   l_line_level_action := 'UPDATE';
7192                ELSE
7193                   l_line_level_action := 'CREATE';
7194                END IF;
7195              EXCEPTION
7196                 WHEN NO_DATA_FOUND  THEN
7197                      l_line_level_action := 'CREATE';
7198                 WHEN OTHERS THEN
7199                      RAISE;
7200              END;
7201       -- End for bug 6485124
7202           END IF;
7203 
7204          -------------------------------------------------------------------
7205           l_debug_info := 'Get Additional PO matched  info';
7206           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7207              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7208           END IF;
7209          -------------------------------------------------------------------
7210 
7211          IF ( l_inv_line_list(i).po_line_location_id IS NOT NULL AND
7212               l_inv_line_list(i).line_type_lookup_code NOT IN ('MISCELLANEOUS','FREIGHT')) THEN   --Bug14039130
7213 
7214            IF NOT (AP_ETAX_UTILITY_PKG.Get_PO_Info(
7215               P_Po_Line_Location_Id         => l_inv_line_list(i).po_line_location_id,
7216               P_PO_Distribution_Id          => null,
7217               P_Application_Id              => l_ref_doc_application_id,
7218               P_Entity_code                 => l_ref_doc_entity_code,
7219               P_Event_Class_Code            => l_ref_doc_event_class_code,
7220               P_PO_Quantity                 => l_ref_doc_line_quantity,
7221               P_Product_Org_Id              => l_product_org_id,
7222               P_Po_Header_Id                => l_ref_doc_trx_id,
7223               P_Po_Header_curr_conv_rate    => l_po_header_curr_conv_rate,
7224 	          P_Uom_Code		            => l_uom_code,
7225               P_Dist_Qty                    => l_dummy,
7226               P_Ship_Price                  => l_dummy,
7227               P_Error_Code                  => P_error_code,
7228               P_Calling_Sequence            => l_curr_calling_sequence)) THEN
7229 
7230 		--Bug15914464: Start
7231              IF AP_APPROVAL_PKG.G_VALIDATION_REQUEST_ID IS NULL THEN
7232                APP_EXCEPTION.RAISE_EXCEPTION;
7233              ELSE
7234 			  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7235                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'Error in AP_ETAX_UTILITY_PKG.Get_PO_Info');
7236               END IF;
7237 		--Bug15914464: End
7238               --bug10140354
7239 	          Insert into ap_errors_gt(invoice_id , message_text) values(P_Invoice_Header_Rec.invoice_id , 'Error in Get_po_info');
7240 	          l_return_status := FALSE;
7241 			  RETURN l_return_status; --Bug15914464
7242 			 END IF; --Bug15914464
7243            END IF;
7244 
7245 	   l_ref_doc_trx_level_type := 'SHIPMENT';
7246 
7247          ELSE
7248             l_ref_doc_application_id	 := Null;
7249             l_ref_doc_entity_code	 := Null;
7250             l_ref_doc_event_class_code   := Null;
7251             l_ref_doc_line_quantity      := Null;
7252             l_product_org_id		 := Null;
7253             l_ref_doc_trx_id		 := Null;
7254             l_ref_doc_trx_level_type	 := Null;
7255             l_uom_code			 := Null;
7256          END IF;
7257 
7258          -------------------------------------------------------------------
7259           l_debug_info := 'Get Additional receipt matched info ';
7260           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7261              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7262           END IF;
7263          -------------------------------------------------------------------
7264          IF ( l_return_status = TRUE AND
7265               l_inv_line_list(i).rcv_transaction_id IS NOT NULL AND
7266               l_inv_line_list(i).line_type_lookup_code NOT IN ('MISCELLANEOUS','FREIGHT')) THEN --Bug14039130
7267            IF NOT (AP_ETAX_UTILITY_PKG.Get_Receipt_Info(
7268               P_Rcv_Transaction_Id          => l_inv_line_list(i).rcv_transaction_id,
7269               P_Application_Id              => l_applied_to_application_id,
7270               P_Entity_code                 => l_applied_to_entity_code,
7271               P_Event_Class_Code            => l_applied_to_event_class_code,
7272               P_Transaction_Date            => l_trx_receipt_date,
7273               P_Error_Code                  => P_error_code,
7274               P_Calling_Sequence            => l_curr_calling_sequence)) THEN
7275 
7276 		--Bug15914464: Start
7277              IF AP_APPROVAL_PKG.G_VALIDATION_REQUEST_ID IS NULL THEN
7278                APP_EXCEPTION.RAISE_EXCEPTION;
7279              ELSE
7280 			  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7281                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'Error in AP_ETAX_UTILITY_PKG.Get_Receipt_Info');
7282               END IF;
7283 		--Bug15914464: End
7284               --bug10140354
7285 	          Insert into ap_errors_gt(invoice_id , message_text) values(P_Invoice_Header_Rec.invoice_id , 'Error in Get_Receipt_Info');
7286 	          l_return_status := FALSE;
7287 			  RETURN l_return_status; --Bug15914464
7288 			 END IF; --Bug15914464
7289            END IF;
7290          ELSE
7291 	    l_applied_to_application_id   := Null;
7292             l_applied_to_entity_code      := Null;
7293             l_applied_to_event_class_code := Null;
7294 	    l_trx_receipt_date		  := Null;
7295          END IF;
7296 
7297          -------------------------------------------------------------------
7298           l_debug_info := 'Get Additional Prepayment Application Info';
7299           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7300              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7301           END IF;
7302          -------------------------------------------------------------------
7303 
7304          IF (l_return_status = TRUE) THEN
7305 	     IF (l_inv_line_list(i).prepay_invoice_id IS NOT NULL AND
7306                  l_inv_line_list(i).prepay_line_number IS NOT NULL) THEN
7307 
7311 			              P_Application_Id              => l_prepay_doc_application_id,
7308 	         IF NOT (AP_ETAX_UTILITY_PKG.Get_Prepay_Invoice_Info(
7309 			              P_Prepay_Invoice_Id           => l_inv_line_list(i).prepay_invoice_id,
7310 			              P_Prepay_Line_Number          => l_inv_line_list(i).prepay_line_number,
7312 			              P_Entity_code                 => l_prepay_doc_entity_code,
7313 			              P_Event_Class_Code            => l_prepay_doc_event_class_code,
7314 			              P_Invoice_Number              => l_prepay_doc_number,
7315 			              P_Invoice_Date                => l_prepay_doc_date,
7316 			              P_Error_Code                  => P_error_code,
7317 			              P_Calling_Sequence            => l_curr_calling_sequence)) THEN
7318 
7319 		--Bug15914464: Start
7320              IF AP_APPROVAL_PKG.G_VALIDATION_REQUEST_ID IS NULL THEN
7321               APP_EXCEPTION.RAISE_EXCEPTION;
7322              ELSE
7323 			  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7324                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'Error in AP_ETAX_UTILITY_PKG.Get_Prepay_Invoice_Info');
7325               END IF;
7326 		--Bug15914464: End
7327               --bug10140354
7328 	          Insert into ap_errors_gt(invoice_id , message_text) values(P_Invoice_Header_Rec.invoice_id , 'Error in Get_Prepay_Invoice_Info');
7329 	          l_return_status := FALSE;
7330 			  RETURN l_return_status; --Bug15914464
7331 			 END IF; --Bug15914464
7332 		END IF;
7333 
7334 		l_applied_from_trx_id         := l_inv_line_list(i).prepay_invoice_id;
7335 		l_applied_from_line_id        := l_inv_line_list(i).prepay_line_number;
7336 		l_applied_from_trx_level_type := 'LINE';
7337 
7338 	    ELSIF p_calling_mode = 'RECOUPMENT' THEN
7339 
7340                   IF NOT (AP_ETAX_UTILITY_PKG.Get_Prepay_Invoice_Info(
7341                                       P_Prepay_Invoice_Id           => l_inv_line_list(i).invoice_id,
7342                                       P_Prepay_Line_Number          => l_inv_line_list(i).line_number,
7343                                       P_Application_Id              => l_prepay_doc_application_id,
7344                                       P_Entity_code                 => l_prepay_doc_entity_code,
7345                                       P_Event_Class_Code            => l_prepay_doc_event_class_code,
7346                                       P_Invoice_Number              => l_prepay_doc_number,
7347                                       P_Invoice_Date                => l_prepay_doc_date,
7348                                       P_Error_Code                  => P_error_code,
7349                                       P_Calling_Sequence            => l_curr_calling_sequence)) THEN
7350 
7351 		--Bug15914464: Start
7352              IF AP_APPROVAL_PKG.G_VALIDATION_REQUEST_ID IS NULL THEN
7353               APP_EXCEPTION.RAISE_EXCEPTION;
7354              ELSE
7355 			  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7356                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'Error in AP_ETAX_UTILITY_PKG.Get_Prepay_Invoice_Info');
7357               END IF;
7358 	      Insert into ap_errors_gt(invoice_id , message_text) values(P_Invoice_Header_Rec.invoice_id , 'Error in Get_Prepay_Invoice_Info');
7359 	      --Bug15914464: End
7360 	          l_return_status := FALSE;
7361 			  RETURN l_return_status; --Bug15914464
7362 			 END IF; --Bug15914464
7363                   END IF;
7364 
7365                   l_applied_from_trx_id         := l_inv_line_list(i).invoice_id;
7366                   l_applied_from_line_id        := l_inv_line_list(i).line_number;
7367                   l_applied_from_trx_level_type := 'LINE';
7368 
7369 	    ELSE
7370 	       l_prepay_doc_application_id   := Null;
7371 	       l_prepay_doc_entity_code      := Null;
7372 	       l_prepay_doc_event_class_code := Null;
7373 	       l_prepay_doc_number           := Null;
7374 	       l_prepay_doc_date             := Null;
7375 	       l_applied_from_trx_level_type := Null;
7376                l_applied_from_trx_id         := Null;
7377                l_applied_from_line_id        := Null;
7378 	    END IF;
7379         END IF;
7380 
7381          -------------------------------------------------------------------
7382           l_debug_info := 'Get Additional Correction Invoice Info ';
7383           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7384              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7385           END IF;
7386          -------------------------------------------------------------------
7387 
7388          IF ( l_return_status = TRUE AND
7389               l_inv_line_list(i).corrected_inv_id IS NOT NULL AND
7390               l_inv_line_list(i).corrected_line_number IS NOT NULL) THEN
7391 
7392            IF NOT (AP_ETAX_UTILITY_PKG.Get_Corrected_Invoice_Info(
7393               P_Corrected_Invoice_Id        => l_inv_line_list(i).corrected_inv_id,
7394               P_Corrected_Line_Number       => l_inv_line_list(i).corrected_line_number,
7395               P_Application_Id              => l_adj_doc_application_id,
7396               P_Entity_code                 => l_adj_doc_entity_code,
7397               P_Event_Class_Code            => l_adj_doc_event_class_code,
7398               P_Invoice_Number              => l_adj_doc_number,
7399               P_Invoice_Date                => l_adj_doc_date,
7400               P_Error_Code                  => P_error_code,
7401               P_Calling_Sequence            => l_curr_calling_sequence)) THEN
7402 
7403 		--Bug15914464: Start
7404              IF AP_APPROVAL_PKG.G_VALIDATION_REQUEST_ID IS NULL THEN
7405               APP_EXCEPTION.RAISE_EXCEPTION;
7406              ELSE
7407 			  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7408                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'Error in AP_ETAX_UTILITY_PKG.Get_Corrected_Invoice_Info');
7409               END IF;
7410 		--Bug15914464: End
7411               --bug10140354
7412 	          Insert into ap_errors_gt(invoice_id , message_text) values(P_Invoice_Header_Rec.invoice_id , 'Error in Get_Corrected_Invoice_Info');
7413 	          l_return_status := FALSE;
7414 			  RETURN l_return_status; --Bug15914464
7415 			 END IF; --Bug15914464
7416           END IF;
7417 
7418           l_adj_doc_trx_level_type := 'LINE';
7419 
7420          ELSE
7421             l_adj_doc_application_id   := Null;
7422             l_adj_doc_entity_code      := Null;
7423             l_adj_doc_event_class_code := Null;
7424             l_adj_doc_number	       := Null;
7425             l_adj_doc_date             := Null;
7426 	    l_adj_doc_trx_level_type   := Null;
7427          END IF;
7428 
7429          -------------------------------------------------------------------
7430           l_debug_info := 'Get line_amt_includes_tax_flag';
7431           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7432              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7433           END IF;
7434          -------------------------------------------------------------------
7435 
7436          IF (l_inv_line_list(i).po_line_location_id IS NOT NULL) THEN
7437            -- NONE
7438            l_line_amt_includes_tax_flag := 'N';
7439 
7440          ELSE
7441            IF (p_calling_mode = 'CALCULATE QUOTE')
7442               OR
7443               (p_invoice_header_rec.invoice_type_lookup_code = 'EXPENSE REPORT'
7444                and nvl(l_inv_line_list(i).line_type_lookup_code, 'N') <> 'PREPAY') THEN
7445              -- ALL
7446              l_line_amt_includes_tax_flag := 'A';
7447 
7448            ELSE
7449              -- STANDARD
7450              l_line_amt_includes_tax_flag := 'S';
7451 
7452            END IF;
7453          END IF;
7454 
7455 	 BEGIN
7456               IF (l_inv_line_list(i).tax_already_calculated_flag = 'Y') THEN
7457 
7458 		 SELECT /*+ index(ZX_LINES_DET_FACTORS ZX_LINES_DET_FACTORS_U1) */  -- 9373895
7459 		        line_amt_includes_tax_flag
7460 		   INTO l_init_line_amt_incl_tax_fg
7461 		   FROM zx_lines_det_factors
7462 		  WHERE application_id = 200
7463 		    AND entity_code    = 'AP_INVOICES'
7464 		    AND event_class_code IN ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
7465 		    AND trx_id         = l_inv_line_list(i).invoice_id
7466 		    AND trx_line_id    = l_inv_line_list(i).line_number
7467 		    AND rownum         = 1;
7468 
7469 		IF l_init_line_amt_incl_tax_fg IS NOT NULL THEN
7470 		   l_line_amt_includes_tax_flag := l_init_line_amt_incl_tax_fg;
7471 		END IF;
7472 
7473 	      END IF;
7474          EXCEPTION
7475 		WHEN OTHERS THEN
7476 			NULL;
7477 	 END;
7478 
7479          -------------------------------------------------------------------
7480           l_debug_info := 'Get ctrl_hdr_tx_appl_flag';
7481           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7482              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7483           END IF;
7484          -------------------------------------------------------------------
7485          IF P_Invoice_Header_Rec.control_amount IS NOT NULL THEN
7486            l_ctrl_hdr_tx_appl_flag := 'Y';
7487          ELSE
7488            l_ctrl_hdr_tx_appl_flag := 'N';
7489          END IF;
7490 
7491          -------------------------------------------------------------------
7492           l_debug_info := 'Get line_class';
7493           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7494              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7495           END IF;
7496          -------------------------------------------------------------------
7497 
7498          IF (l_return_status = TRUE) THEN
7499 
7500 	      IF NOT (AP_ETAX_UTILITY_PKG.Get_Line_Class(
7501 		             P_Invoice_Type_Lookup_Code    => P_Invoice_Header_Rec.invoice_type_lookup_code,
7502 		             P_Inv_Line_Type               => l_inv_line_list(i).line_type_lookup_code,
7503 		             P_Line_Location_Id            => l_inv_line_list(i).po_line_location_id,
7504 		             P_Line_Class                  => l_line_class,
7505 		             P_Error_Code                  => P_error_code,
7506 		             P_Calling_Sequence            => l_curr_calling_sequence)) THEN
7507 
7508 	     --Bug15914464: Start
7509              IF AP_APPROVAL_PKG.G_VALIDATION_REQUEST_ID IS NULL THEN
7510               APP_EXCEPTION.RAISE_EXCEPTION;
7511              ELSE
7512 			  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7513                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'Error in AP_ETAX_UTILITY_PKG.Get_Line_Class');
7514               END IF;
7515 	      Insert into ap_errors_gt(invoice_id , message_text) values(P_Invoice_Header_Rec.invoice_id , 'Error in Get_Line_Class');
7516 		--Bug15914464: End
7517 	          l_return_status := FALSE;
7518 			  RETURN l_return_status; --Bug15914464
7519 			 END IF; --Bug15914464
7520              END IF;
7521          END IF;
7522 
7523        --Bug6908977 STARTS
7524        IF l_inv_line_list(i).match_type IN --Bug6965650
7525          ('ITEM_TO_PO','ITEM_TO_RECEIPT','ITEM_TO_SERVICE_PO',
7526           'ITEM_TO_SERVICE_RECEIPT','PRICE_CORRECTION','QTY_CORRECTION',
7527           'AMOUNT_CORRECTION') THEN
7528 
7529           IF  l_inv_line_list(i).po_line_location_id IS NOT NULL THEN
7530 
7531               SELECT pd.code_combination_id
7532                 INTO l_default_ccid
7533                 FROM po_distributions_all pd
7534                WHERE pd.line_location_id = l_inv_line_list(i).po_line_location_id
7535                  AND rownum = 1;
7536 
7537 	      /* Bug 8230574
7538 	         If the account on the Invoice Line is already populated and
7539 		 it is different from that being defaulted from the PO then
7540 		 retain the line level account....else use the defaulting
7541 		 from PO. */
7542 
7543 		 IF l_inv_line_list(i).default_dist_ccid IS NOT NULL AND
7544                     l_inv_line_list(i).default_dist_ccid <> l_default_ccid THEN
7545 
7546 		      null; -- retain the Invoice line cc_id
7547 
7548                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7549                          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
7550                                  '1 Retaining the Line level cc_id '||l_inv_line_list(i).default_dist_ccid);
7551                       END IF;
7552 
7553 		 ELSE
7554                       l_inv_line_list(i).default_dist_ccid:=l_default_ccid;
7555 
7556                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7557                          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
7558                                  '1 Default CCID passed from PO '||l_default_ccid);
7559                       END IF;
7560 
7561 		 END IF;
7562 
7563           ELSIF l_inv_line_list(i).po_distribution_id IS NOT NULL THEN
7564 
7565               SELECT pd.code_combination_id
7566                 INTO l_default_ccid
7567                 FROM po_distributions_all pd
7568                WHERE pd.line_location_id =
7569                     (SELECT pod.line_location_id
7570                        FROM po_distributions_all pod
7571                       WHERE po_distribution_id = l_inv_line_list(i).po_distribution_id)
7572                  AND rownum = 1;
7573 
7574 	      /* Bug 8230574
7575 	         If the account on the Invoice Line is already populated and
7576 		 it is different from that being defaulted from the PO then
7577 		 retain the line level account....else use the defaulting
7578 		 from PO. */
7579 
7580 		 IF l_inv_line_list(i).default_dist_ccid IS NOT NULL AND
7581                     l_inv_line_list(i).default_dist_ccid <> l_default_ccid THEN
7582 
7583 		      null; -- retain the Invoice line cc_id
7584 
7585                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7586                          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
7587                                  '3 Retaining the Line level cc_id '||l_inv_line_list(i).default_dist_ccid);
7588                       END IF;
7589 
7590 		 ELSE
7591                       l_inv_line_list(i).default_dist_ccid:=l_default_ccid;
7592 
7593                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7594                          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
7595                                  '3 Default CCID passed from PO '||l_default_ccid);
7596                       END IF;
7597 
7598 		 END IF;
7599 
7600           ELSIF l_inv_line_list(i).rcv_shipment_line_id IS NOT NULL THEN
7601 
7602               SELECT pd.code_combination_id
7603                 INTO l_default_ccid
7604                 FROM po_distributions_all pd
7605                WHERE pd.line_location_id =
7606                      (SELECT rcv.po_line_location_id
7607                         FROM rcv_shipment_lines rcv
7608                        WHERE rcv.shipment_line_id = l_inv_line_list(i).rcv_shipment_line_id)
7609                  AND rownum = 1;
7610 
7611 	      /* Bug 8230574
7612 	         If the account on the Invoice Line is already populated and
7613 		 it is different from that being defaulted from the PO then
7614 		 retain the line level account....else use the defaulting
7615 		 from PO. */
7616 
7617 		 IF l_inv_line_list(i).default_dist_ccid IS NOT NULL AND
7618                     l_inv_line_list(i).default_dist_ccid <> l_default_ccid THEN
7619 
7620 		      null; -- retain the Invoice line cc_id
7621 
7622                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7623                          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
7624                                  '4 Retaining the Line level cc_id '||l_inv_line_list(i).default_dist_ccid);
7625                       END IF;
7626 
7627 		 ELSE
7628                       l_inv_line_list(i).default_dist_ccid:=l_default_ccid;
7629 
7630                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7631                          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
7632                                  '4 Default CCID passed from PO '||l_default_ccid);
7633                       END IF;
7634 
7635 		 END IF;
7636 
7637           ELSIF l_inv_line_list(i).rcv_transaction_id IS NOT NULL THEN
7638 
7639               SELECT pd.code_combination_id
7640                 INTO l_default_ccid
7641                 FROM po_distributions_all pd
7642                WHERE pd.line_location_id =
7643                      (SELECT rcv.po_line_location_id
7644                         FROM rcv_transactions rcv
7645                        WHERE rcv.transaction_id = l_inv_line_list(i).rcv_transaction_id)
7646                  AND rownum = 1;
7647 
7648 
7649 	      /* Bug 8230574
7650 	         If the account on the Invoice Line is already populated and
7651 		 it is different from that being defaulted from the PO then
7652 		 retain the line level account....else use the defaulting
7653 		 from PO. */
7654 
7655 		 IF l_inv_line_list(i).default_dist_ccid IS NOT NULL AND
7656                     l_inv_line_list(i).default_dist_ccid <> l_default_ccid THEN
7657 
7658 		      null; -- retain the Invoice line cc_id
7659 
7660                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7661                          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
7662                                  '5 Retaining the Line level cc_id '||l_inv_line_list(i).default_dist_ccid);
7663                       END IF;
7664 
7665 		 ELSE
7666                       l_inv_line_list(i).default_dist_ccid:=l_default_ccid;
7667 
7668                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7669                          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
7670                                  '5 Default CCID passed from PO '||l_default_ccid);
7671                       END IF;
7672 
7673 		 END IF;
7674 
7675           ELSE
7676               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7677                   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
7678                                  '6 Default CCID passed from line rather than of PO Distribution');
7679               END IF;
7680           END IF;
7681 
7682   /* Bug 12909089 starts */
7683 ELSE
7684     IF (l_inv_line_list(i).line_type_lookup_code <> 'PREPAY' AND
7685 	        P_Invoice_Header_Rec.invoice_type_lookup_code <> 'EXPENSE REPORT') THEN
7686     	  IF (l_inv_line_list(i).default_dist_ccid IS NOT NULL) THEN
7687     	          IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7688                            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
7689                                                '6 CCID is taken from Line: '||l_default_ccid);
7690                    END IF;
7691 
7692 		   NULL;
7693     	  ELSE
7694 
7695          		BEGIN
7696 
7700          		  WHERE aid.invoice_id = l_inv_line_list(i).invoice_id
7697          		 SELECT aid.dist_code_combination_id
7698          		   INTO l_inv_line_list(i).default_dist_ccid
7699                          FROM ap_invoice_distributions_all aid
7701          		    AND aid.invoice_line_number =  l_inv_line_list(i).line_number
7702 					AND ROWNUM = 1;
7703 
7704     	                  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7705                              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
7706                                                 '7 CCID is taken from Distribution: '||l_default_ccid);
7707                           END IF;
7708 
7709                        EXCEPTION
7710                         WHEN NO_DATA_FOUND THEN
7711     	                  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7712                             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
7713                                                '8 CCID is NULL: '||l_default_ccid);
7714                           END IF;
7715                        END;
7716 
7717 		 /*Bug 13399833: Begins*/
7718 		      IF l_inv_line_list(i).project_id IS NOT NULL
7719 		       AND l_inv_line_list(i).default_dist_ccid IS NULL then
7720 
7721 			 IF NOT(AP_ETAX_UTILITY_PKG.GET_CCID_FROM_PROJECTS(p_invoice_id => l_inv_line_list(i).invoice_id ,
7722                                                         p_line_number =>l_inv_line_list(i).line_number ,
7723      				                        p_ccid => l_inv_line_list(i).default_dist_ccid )) THEN
7724 
7725 			         --Bug15914464: Start
7726                                  IF AP_APPROVAL_PKG.G_VALIDATION_REQUEST_ID IS NULL THEN
7727                                    APP_EXCEPTION.RAISE_EXCEPTION;
7728                                  ELSE
7729                                  --Bug15914464: End
7730                                     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7731                                        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'Error while retriving the ccid from projects');
7732                                     END IF;
7733 	                            Insert into ap_errors_gt(invoice_id , message_text) values(P_Invoice_Header_Rec.invoice_id , 'Error in get_ccid_from_projects');
7734 	                            l_return_status := FALSE;
7735 		                    RETURN l_return_status; --Bug15914464
7736 			        END IF; --Bug15914464
7737                          END IF;
7738 		      END IF;
7739 		     /*Bug 13399833 end*/
7740 
7741           END IF;
7742     END IF;
7743   /* Bug 12909089 ends */
7744   END IF;
7745       --Bug6908977 ENDS
7746 
7747       -- bug7350421
7748       IF (l_inv_line_list(i).default_dist_ccid IS NULL AND
7749           l_inv_line_list(i).line_type_lookup_code <> 'PREPAY') THEN
7750 
7751          BEGIN
7752 
7753           SELECT aerd.code_combination_id
7754             INTO l_inv_line_list(i).default_dist_ccid
7755             FROM ap_exp_report_dists_all aerd,
7756                  ap_expense_report_lines_all aerl,
7757                  ap_invoices_all ai
7758            WHERE aerd.report_header_id = l_inv_line_list(i).reference_key1
7759              AND aerd.report_line_id = l_inv_line_list(i).reference_key2
7760              AND aerd.report_line_id = aerl.report_line_id
7761              AND aerd.report_header_id = aerl.report_header_id
7762              AND ai.invoice_id = l_inv_line_list(i).invoice_id
7763              AND ai.invoice_type_lookup_code = 'EXPENSE REPORT'
7764              AND rownum = 1;
7765 
7766          EXCEPTION
7767           WHEN OTHERS THEN
7768             NULL;
7769         END;
7770 
7771        END IF;
7772 
7773       --
7774       -- Bug 5565310: Commented out the below code as this is replaced by
7775       --              code in matching packages and in invoice workbench.
7776       --
7777       -- Bug 5605359: Enabled the code only for invoices created from ISP.
7778       --              Ideally the tax determining attributes should be
7779       --              added to the ISP UI instead of defaulting here.
7780       --
7781 
7782       IF  P_Invoice_Header_Rec.source = 'ISP' THEN
7783 
7784         IF  (l_inv_line_list(i).po_header_id         IS NOT NULL AND
7785              l_inv_line_list(i).po_line_location_id  IS NOT NULL AND
7786              l_inv_line_list(i).primary_intended_use        IS NULL AND
7787              l_inv_line_list(i).product_type	            IS NULL AND
7788              l_inv_line_list(i).product_category            IS NULL AND
7792 
7789              l_inv_line_list(i).product_fisc_classification IS NULL AND
7790              l_inv_line_list(i).user_defined_fisc_class     IS NULL AND
7791 	     l_inv_line_list(i).tax_classification_code     IS NULL ) THEN
7793              -------------------------------------------------------------------
7794              l_debug_info := 'ISP: get_po_tax_attributes';
7795              IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7796                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7797              END IF;
7798              -------------------------------------------------------------------
7799              get_po_tax_attributes
7800                         (
7801                          p_application_id              => l_ref_doc_application_id,
7802                          p_org_id                      => P_Invoice_Header_Rec.org_id,
7803                          p_entity_code                 => l_ref_doc_entity_code,
7804                          p_event_class_code            => l_ref_doc_event_class_code,
7805                          p_trx_level_type              => 'SHIPMENT',
7806                          p_trx_id                      => l_ref_doc_trx_id,
7807                          p_trx_line_id                 => l_inv_line_list(i).po_line_location_id,
7808                          x_line_intended_use           => l_intended_use,
7809                          x_product_type                => l_product_type,
7810                          x_product_category            => l_product_category,
7811                          x_product_fisc_classification => l_product_fisc_class,
7812                          x_user_defined_fisc_class     => l_user_defined_fisc_class,
7813                          x_assessable_value            => l_assessable_value,
7814 			 x_tax_classification_code     => l_dflt_tax_class_code
7815                         );
7816 
7817         ELSE
7818            l_intended_use            := Null;
7819            l_product_type            := Null;
7820            l_product_category        := Null;
7821            l_product_fisc_class      := Null;
7822            l_user_defined_fisc_class := Null;
7823            l_assessable_value        := Null;
7824            l_dflt_tax_class_code     := Null;
7825 
7826         END IF;
7827 
7828         IF (l_dflt_tax_class_code IS NULL
7829             AND l_inv_line_list(i).tax_classification_code IS NULL) THEN
7830 
7831              -------------------------------------------------------------------
7832              l_debug_info := 'ISP: ZX_PKG.get_default_tax_classification';
7833              IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7834                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7835              END IF;
7836              -------------------------------------------------------------------
7837 
7838              ZX_AP_TAX_CLASSIFICATN_DEF_PKG.get_default_tax_classification
7839 		            (p_ref_doc_application_id           => l_ref_doc_application_id,
7840 		             p_ref_doc_entity_code              => l_ref_doc_entity_code,
7841 		             p_ref_doc_event_class_code         => l_ref_doc_event_class_code,
7842 		             p_ref_doc_trx_id                   => l_ref_doc_trx_id,
7843 		             p_ref_doc_line_id                  => l_inv_line_list(i).po_line_location_id,
7844 		             p_ref_doc_trx_level_type           => 'SHIPMENT',
7845 		             p_vendor_id                        => P_Invoice_Header_Rec.vendor_id,
7846 		             p_vendor_site_id                   => P_Invoice_Header_Rec.vendor_site_id,
7847 		             p_code_combination_id              => l_inv_line_list(i).default_dist_ccid,
7848 		             p_concatenated_segments            => null,
7849 		             p_templ_tax_classification_cd      => null,
7850 		             p_ship_to_location_id              => l_inv_line_list(i).ship_to_location_id,
7851 		             p_ship_to_loc_org_id               => null,
7852 		             p_inventory_item_id                => l_inv_line_list(i).inventory_item_id,
7853 		             p_item_org_id                      => l_product_org_id,
7854 		             p_tax_classification_code          => l_dflt_tax_class_code,
7855 		             p_allow_tax_code_override_flag     => l_allow_tax_code_override,
7856 		             APPL_SHORT_NAME                    => 'SQLAP',
7857 		             FUNC_SHORT_NAME                    => 'NONE',
7858 		             p_calling_sequence                 => 'AP_ETAX_SERVICES_PKG',
7859 		             p_event_class_code                 => P_Event_Class_Code,
7860 		             p_entity_code                      => 'AP_INVOICES',
7861 		             p_application_id                   => 200,
7865       END IF; -- For ISP invoices only
7862 		             p_internal_organization_id         => P_Invoice_Header_Rec.org_id);
7863 
7864         END IF;
7866 
7867         IF g_manual_tax_lines = 'Y' and l_manual_tax_line_rcv_mtch = 'N' THEN  ---for 6014115
7868                 l_applied_to_application_id   := NULL;
7869                 l_applied_to_entity_code      := Null;
7870                 l_applied_to_event_class_code := Null;
7871                 l_trx_receipt_date            := Null;
7872         END IF;
7873         IF g_manual_tax_lines = 'Y'
7874            and l_prepay_doc_application_id is null
7875            and l_adj_doc_application_id    is null
7876            and l_applied_to_application_id is null THEN
7877 
7878 	   l_line_level_action := 'CREATE_WITH_TAX';
7879 
7880 	END IF;
7881 
7882         -------------------------------------------------------------------
7883         l_debug_info := 'Populate pl/sql table';
7884         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7885            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
7886         END IF;
7887         -------------------------------------------------------------------
7888         IF (l_return_status = TRUE ) THEN
7889 
7890           trans_lines(i).application_id		:= ap_etax_pkg.ap_application_id;
7891           trans_lines(i).entity_code		:= ap_etax_pkg.ap_entity_code;
7892           trans_lines(i).event_class_code	:= p_event_class_code;
7893 
7894 	  IF p_calling_mode = 'RECOUPMENT' THEN
7895 
7896              trans_lines(i).trx_id      := P_Invoice_Header_Rec.invoice_id;
7897              trans_lines(i).trx_line_id := -1 * (l_inv_line_list(i).invoice_id || l_inv_line_list(i).line_number || p_line_number);
7898 
7899              SELECT sum(amount)
7900                INTO trans_lines(i).line_amt
7901                FROM ap_invoice_distributions_all aids
7902               WHERE invoice_id            = p_invoice_header_rec.invoice_id
7903 		AND invoice_line_number   = p_line_number
7904                 AND line_type_lookup_code = 'PREPAY'
7905                 AND EXISTS
7906                       (select 'Prepayment Invoice'
7907                          from ap_invoice_distributions_all aidp
7908                          where aidp.invoice_distribution_id = aids.prepay_distribution_id
7909                            and aidp.invoice_id              = l_inv_line_list(i).invoice_id
7910                            and aidp.invoice_line_number     = l_inv_line_list(i).line_number);
7911 
7912 	  ELSE
7913 
7914 	     trans_lines(i).trx_id      := l_inv_line_list(i).invoice_id;
7915 	     trans_lines(i).trx_line_id := l_inv_line_list(i).line_number;
7916 	     trans_lines(i).line_amt    := l_inv_line_list(i).amount + nvl(l_inv_line_list(i).retained_amount,0);
7917 
7918 	  END IF;
7919 
7920           trans_lines(i).trx_level_type 		:= 'LINE';
7921           trans_lines(i).line_level_action 		:= l_line_level_action;
7922 	      trans_lines(i).line_class 			:= l_line_class;
7923 
7924           trans_lines(i).trx_receipt_date 		:= l_trx_receipt_date;
7925           trans_lines(i).trx_line_type 			:= l_inv_line_list(i).line_type_lookup_code;
7926           trans_lines(i).trx_line_date 			:= nvl(l_inv_line_list(i).start_expense_date, p_invoice_header_rec.invoice_date);
7927           trans_lines(i).trx_line_number 		:= l_inv_line_list(i).line_number;
7928           trans_lines(i).trx_line_description 		:= l_inv_line_list(i).description;
7929           trans_lines(i).trx_line_gl_date 		:= l_inv_line_list(i).accounting_date;
7930           trans_lines(i).account_ccid 			:= l_inv_line_list(i).default_dist_ccid;
7931 
7932           trans_lines(i).trx_line_quantity		:= nvl(l_inv_line_list(i).quantity_invoiced, 1);
7933 
7934           --Bug10621602 In Retainange Release Case we will pass unit price from standard invoice to compute the
7935           --proper variances
7936           IF P_Invoice_Header_Rec.invoice_type_lookup_code ='RETAINAGE RELEASE' AND
7937              l_inv_line_list(i).line_type_lookup_code = 'RETAINAGE RELEASE' AND
7938              l_inv_line_list(i).po_line_location_id IS NOT NULL THEN
7939 
7940              BEGIN
7941                 SELECT unit_price
7942                   INTO trans_lines(i).unit_price
7943                   FROM ap_invoice_lines_all ail
7944                  WHERE ail.invoice_id = l_inv_line_list(i).Retained_invoice_id
7945                    AND ail.line_number =  l_inv_line_list(i).Retained_line_number;
7946              EXCEPTION
7947              WHEN OTHERS THEN
7948                   NULL;
7949              END;
7950 
7951              trans_lines(i).unit_price 			:= nvl(trans_lines(i).unit_price,trans_lines(i).line_amt);
7952 
7953           ELSE
7954 
7955              trans_lines(i).unit_price 			:= nvl(l_inv_line_list(i).unit_price, trans_lines(i).line_amt);
7956           END IF;
7957            --Bug10621602
7958 
7959           trans_lines(i).uom_code			:= l_uom_code;
7960 
7961           trans_lines(i).trx_business_category 		:= l_inv_line_list(i).trx_business_category;
7962           trans_lines(i).line_intended_use 		:= nvl(l_inv_line_list(i).primary_intended_use,l_intended_use);
7963           trans_lines(i).user_defined_fisc_class 	:= nvl(l_inv_line_list(i).user_defined_fisc_class,l_user_defined_fisc_class);
7964           trans_lines(i).product_fisc_classification	:= nvl(l_inv_line_list(i).product_fisc_classification,l_product_fisc_class);
7965           trans_lines(i).assessable_value 		:= nvl(l_inv_line_list(i).assessable_value,l_assessable_value);
7966           trans_lines(i).input_tax_classification_code	:= nvl(l_inv_line_list(i).tax_classification_code,l_dflt_tax_class_code);
7967 
7968           trans_lines(i).product_id 			:= l_inv_line_list(i).inventory_item_id;
7969           trans_lines(i).product_org_id			:= l_product_org_id;
7973           trans_lines(i).fob_point			:= l_fob_point;
7970           trans_lines(i).product_category		:= nvl(l_inv_line_list(i).product_category,l_product_category);
7971           trans_lines(i).product_type			:= nvl(l_inv_line_list(i).product_type,l_product_type);
7972           trans_lines(i).product_description 		:= l_inv_line_list(i).item_description;
7974 
7975           -- AP is not going to pass this parameter.  eTax is aware of this and will derive the value
7976           -- trans_lines(i).product_code
7977 
7978           -- 7262269 /*Bug 13079028: Removed the IF-ELSE and called the function directly to handle all cases*/
7979           --IF l_inv_line_list(i).po_line_location_id IS NOT NULL THEN
7980              l_ship_to_party_id := get_po_ship_to_org_id (l_inv_line_list(i).po_line_location_id,
7981 	                                                  l_inv_line_list(i).inventory_item_id, /*Bug 13079028*/
7982 							  l_inv_line_list(i).org_id, /*Bug 13079028*/
7983 							  l_inv_line_list(i).application_id, /*Bug 13079028*/
7984 							  l_inv_line_list(i).reference_key5); /*Bug 13079028*/
7985           /*ELSE
7986              l_ship_to_party_id := l_inv_line_list(i).org_id;
7987           END IF;*/ /*Bug 13079028*/
7988 
7989           trans_lines(i).ship_to_party_id		:= l_ship_to_party_id;
7990           -- 7262269
7991           trans_lines(i).ship_from_party_id		:= P_Invoice_Header_Rec.party_id;
7992 
7993           trans_lines(i).bill_to_party_id		:= l_inv_line_list(i).org_id;
7994           trans_lines(i).bill_from_party_id		:= P_Invoice_Header_Rec.party_id;
7995 
7996           trans_lines(i).ship_from_party_site_id	:= P_Invoice_Header_Rec.party_site_id;
7997           trans_lines(i).bill_from_party_site_id	:= P_Invoice_Header_Rec.party_site_id;
7998 
7999           trans_lines(i).ship_to_location_id		:= l_inv_line_list(i).ship_to_location_id;
8000 	      trans_lines(i).ship_from_location_id		:= l_location_id;
8001           trans_lines(i).bill_to_location_id		:= l_bill_to_location_id;
8002           trans_lines(i).bill_from_location_id          := l_location_id;
8003 
8004           trans_lines(i).ref_doc_application_id 	:= l_ref_doc_application_id;
8005           trans_lines(i).ref_doc_entity_code 		:= l_ref_doc_entity_code;
8006           trans_lines(i).ref_doc_event_class_code 	:= l_ref_doc_event_class_code;
8007           trans_lines(i).ref_doc_trx_id 		:= l_ref_doc_trx_id;
8008 	      trans_lines(i).ref_doc_trx_level_type 	:= l_ref_doc_trx_level_type;
8009 
8010           --Bug14039130
8011           IF l_inv_line_list(i).line_type_lookup_code NOT IN ('MISCELLANEOUS','FREIGHT') THEN
8012               trans_lines(i).ref_doc_line_id 		:= l_inv_line_list(i).po_line_location_id;
8013           ELSE
8014               trans_lines(i).ref_doc_line_id 		:= NULL;
8015           END IF;
8016           --Bug14039130
8017 
8018           trans_lines(i).ref_doc_line_quantity 		:= l_ref_doc_line_quantity;
8019 
8020           trans_lines(i).applied_from_application_id 	:= l_prepay_doc_application_id;
8021           trans_lines(i).applied_from_entity_code 	:= l_prepay_doc_entity_code;
8022           trans_lines(i).applied_from_event_class_code 	:= l_prepay_doc_event_class_code;
8023           trans_lines(i).applied_from_trx_id 		:= l_applied_from_trx_id;
8024           trans_lines(i).applied_from_trx_level_type 	:= l_applied_from_trx_level_type;
8025           trans_lines(i).applied_from_line_id 		:= l_applied_from_line_id;
8026 
8027           trans_lines(i).adjusted_doc_application_id 	:= l_adj_doc_application_id;
8028           trans_lines(i).adjusted_doc_entity_code 	:= l_adj_doc_entity_code;
8029           trans_lines(i).adjusted_doc_event_class_code 	:= l_adj_doc_event_class_code;
8030           trans_lines(i).adjusted_doc_trx_id 		:= l_inv_line_list(i).corrected_inv_id;
8031           trans_lines(i).adjusted_doc_line_id 		:= l_inv_line_list(i).corrected_line_number;
8032 	      trans_lines(i).adjusted_doc_trx_level_type 	:= l_adj_doc_trx_level_type;
8033           trans_lines(i).adjusted_doc_number 		:= l_adj_doc_number;
8034           trans_lines(i).adjusted_doc_date 		:= l_adj_doc_date;
8035 
8036           trans_lines(i).applied_to_application_id 	:= l_applied_to_application_id;
8037           trans_lines(i).applied_to_entity_code 	:= l_applied_to_entity_code;
8038           trans_lines(i).applied_to_event_class_code 	:= l_applied_to_event_class_code;
8039 
8040            --Bug14039130
8041 
8042           IF (g_manual_tax_lines = 'Y' and l_manual_tax_line_rcv_mtch = 'N') OR
8043              l_inv_line_list(i).line_type_lookup_code IN ('MISCELLANEOUS','FREIGHT') THEN  ---for 6014115
8044              trans_lines(i).applied_to_trx_id      := NULL;
8045           ELSE
8046              trans_lines(i).applied_to_trx_id 		:= l_inv_line_list(i).rcv_transaction_id;
8047           END IF;
8048 
8049 
8050 
8051 
8052           IF g_manual_tax_lines = 'Y'THEN---for 6014115
8053              IF  l_manual_tax_line_rcv_mtch = 'N' THEN
8054                  trans_lines(i).applied_to_trx_line_id :=NULL;
8055               END IF;
8056           ELSIF l_inv_line_list(i).rcv_transaction_id IS NOT NULL AND
8057                 l_inv_line_list(i).line_type_lookup_code NOT IN ('MISCELLANEOUS','FREIGHT') THEN
8058                 trans_lines(i).applied_to_trx_line_id 	:= l_inv_line_list(i).po_line_location_id;
8059           ELSIF l_inv_line_list(i).rcv_transaction_id IS NOT NULL AND
8060                 l_inv_line_list(i).line_type_lookup_code IN ('MISCELLANEOUS','FREIGHT') THEN
8061                 trans_lines(i).applied_to_trx_line_id 	:= NULL;
8062           END IF;
8063 
8064           --Bug14039130
8065 
8066           trans_lines(i).source_application_id   	:= l_inv_line_list(i).source_application_id;
8067           trans_lines(i).source_entity_code	 	:= l_inv_line_list(i).source_entity_code;
8068           trans_lines(i).source_event_class_code 	:= l_inv_line_list(i).source_event_class_code;
8069           trans_lines(i).source_trx_id		 	:= l_inv_line_list(i).source_trx_id;
8070           trans_lines(i).source_line_id		 	:= l_inv_line_list(i).source_line_id;
8074           trans_lines(i).merchant_party_document_number := l_inv_line_list(i).merchant_document_number;
8071           trans_lines(i).source_trx_level_type	 	:= l_inv_line_list(i).source_trx_level_type;
8072 
8073           trans_lines(i).merchant_party_name 		:= l_inv_line_list(i).merchant_name;
8075           trans_lines(i).merchant_party_reference 	:= l_inv_line_list(i).merchant_reference;
8076           trans_lines(i).merchant_party_taxpayer_id 	:= l_inv_line_list(i).merchant_taxpayer_id;
8077           trans_lines(i).merchant_party_tax_reg_number 	:= l_inv_line_list(i).merchant_tax_reg_number;
8078           trans_lines(i).merchant_party_country 	:= l_inv_line_list(i).country_of_supply;
8079 
8080           trans_lines(i).line_amt_includes_tax_flag 	:= l_line_amt_includes_tax_flag;
8081           trans_lines(i).historical_flag 		:= NVL(l_inv_line_list(i).historical_flag, 'N'); -- Bug 7117591
8082 /*NVL(P_Invoice_Header_Rec.historical_flag, 'N');*/
8083           trans_lines(i).ctrl_hdr_tx_appl_flag 		:= l_ctrl_hdr_tx_appl_flag;
8084           trans_lines(i).ctrl_total_line_tx_amt 	:= l_inv_line_list(i).control_amount;
8085 
8086           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8087               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_transaction_lines_gt values ');
8088 	          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: ' || trans_lines(i).event_class_code);
8089 	          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '           || trans_lines(i).trx_id);
8090 	          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_id: '      || trans_lines(i).trx_line_id);
8091 	          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_level_type: '   || trans_lines(i).trx_level_type);
8092               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_type: '    || trans_lines(i).trx_line_type );
8093 	          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_level_action: '|| trans_lines(i).line_level_action);
8094 	          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_class: '       || trans_lines(i).line_class);
8095 	          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_amt: '         || trans_lines(i).line_amt);
8096               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'unit_price: '       || trans_lines(i).unit_price);
8097               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'ship_to_party_id: ' || trans_lines(i).ship_to_party_id);
8098           END IF;
8099          END IF;
8100        END IF;
8101      END LOOP;
8102 
8103     END IF;
8104 
8105     -------------------------------------------------------------------
8106     l_debug_info := 'Bulk Insert into global temp table';
8107     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8108        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8109     END IF;
8110     -------------------------------------------------------------------
8111     IF ( l_return_status = TRUE ) THEN
8112       FORALL m IN trans_lines.FIRST..trans_lines.LAST
8113         INSERT INTO zx_transaction_lines_gt
8114         VALUES trans_lines(m);
8115     END IF;
8116 
8117     RETURN l_return_status;
8118 
8119   EXCEPTION
8120     WHEN OTHERS THEN
8121       IF (SQLCODE <> -20001) THEN
8122         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
8123         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
8124         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
8125         FND_MESSAGE.SET_TOKEN('PARAMETERS',
8126           ' P_Invoice_Header_Rec = '||P_Invoice_Header_Rec.Invoice_Id||
8127           ' P_Calling_Mode ='||P_Calling_Mode||
8128           ' P_Error_Code = '||P_Error_Code||
8129           ' P_Calling_Sequence = '||P_Calling_Sequence);
8130         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
8131       END IF;
8132 
8133       --bug10140354
8134       IF AP_APPROVAL_PKG.G_VALIDATION_REQUEST_ID IS NULL THEN
8135         APP_EXCEPTION.RAISE_EXCEPTION;
8136     ELSE
8137     l_err:= SQLERRM;
8138     insert into ap_errors_gt(invoice_id , message_text) values(P_Invoice_Header_Rec.Invoice_id , l_err);
8139     RETURN FALSE;
8140     END IF;
8141 
8142   END Populate_Lines_GT;
8143 
8144 /*=============================================================================
8145  |  FUNCTION - Populate_Lines_Import_GT()
8146  |
8147  |  DESCRIPTION
8148  |      This function will get additional information required to populate the
8149  |      ZX_TRANSACTION_LINES_GT
8150  |      This function returns TRUE if the population of the temp table goes
8151  |      through successfully.  Otherwise, FALSE.
8152  |
8153  |  PARAMETERS
8154  |      P_Invoice_Header_Rec - record with invoice header information
8155  |      P_Invoice_Lines_Tab - List of trx and tax lines for the invoice
8156  |        existing in the ap_invoice_lines_interface table
8157  |      P_Calling_Mode - calling mode. it is used to
8158  |      P_Event_Class_Code - Event class code for document
8159  |      P_error_code - Error code to be returned
8160  |      P_calling_sequence -  Calling sequence
8161  |
8162  |  MODIFICATION HISTORY
8163  |    DATE          Author         Action
8164  |    20-JAN-2004   SYIDNER        Created
8165  |
8166  *============================================================================*/
8167 
8168   FUNCTION Populate_Lines_Import_GT(
8169              P_Invoice_Header_Rec      IN AP_IMPORT_INVOICES_PKG.r_invoice_info_rec,
8170              P_Inv_Line_List           IN AP_IMPORT_INVOICES_PKG.t_lines_table,
8171              P_Calling_Mode            IN VARCHAR2,
8172              P_Event_Class_Code        IN VARCHAR2,
8173              P_Error_Code              OUT NOCOPY VARCHAR2,
8174              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN IS
8175 
8176     l_debug_info                 VARCHAR2(240);
8180     TYPE Tax_Lines_Tab_Type  IS TABLE OF zx_import_tax_lines_gt%ROWTYPE;
8177     l_curr_calling_sequence      VARCHAR2(4000);
8178 
8179     TYPE Trx_Lines_Tab_Type  IS TABLE OF zx_transaction_lines_gt%ROWTYPE;
8181     TYPE Link_Lines_Tab_Type IS TABLE OF zx_trx_tax_link_gt%ROWTYPE;
8182 
8183     trans_lines                     Trx_Lines_Tab_Type  := Trx_Lines_Tab_Type();
8184     tax_lines                       Tax_Lines_Tab_Type  := Tax_Lines_Tab_Type();
8185     link_lines                      Link_Lines_Tab_Type := Link_Lines_Tab_Type();
8186 
8187     l_ctrl_hdr_tx_appl_flag         zx_transaction_lines_gt.ctrl_hdr_tx_appl_flag%TYPE;
8188     l_line_control_amount           zx_transaction_lines_gt.ctrl_total_line_tx_amt%TYPE;
8189     l_line_level_action             zx_transaction_lines_gt.line_level_action%TYPE;
8190     l_line_class                    zx_transaction_lines_gt.line_class%TYPE;
8191     l_line_amt_includes_tax_flag    zx_transaction_lines_gt.line_amt_includes_tax_flag%TYPE;
8192 
8193     l_product_org_id		    zx_transaction_lines_gt.product_org_id%TYPE;
8194     l_uom_code			    mtl_units_of_measure.uom_code%TYPE;
8195     l_fob_point                     po_vendor_sites_all.fob_lookup_code%TYPE;
8196 
8197     l_po_line_location_id           ap_invoice_lines_interface.po_line_location_id%TYPE;
8198     l_location_id         	    zx_transaction_lines_gt.ship_from_location_id%type;
8199     l_ship_to_location_id           ap_supplier_sites_all.ship_to_location_id%type;
8200     l_bill_to_location_id           zx_transaction_lines_gt.bill_to_location_id%TYPE;
8201 
8202     -- Purchase Order Info
8203     l_ref_doc_application_id	    zx_transaction_lines_gt.ref_doc_application_id%TYPE;
8204     l_ref_doc_entity_code	    zx_transaction_lines_gt.ref_doc_entity_code%TYPE;
8205     l_ref_doc_event_class_code	    zx_transaction_lines_gt.ref_doc_event_class_code%TYPE;
8206     l_ref_doc_line_quantity	    zx_transaction_lines_gt.ref_doc_line_quantity%TYPE;
8207     l_ref_doc_trx_level_type	    zx_transaction_lines_gt.ref_doc_trx_level_type%TYPE;
8208     l_ref_doc_trx_id                zx_transaction_lines_gt.ref_doc_trx_id%TYPE;
8209     l_po_header_curr_conv_rate	    po_headers_all.rate%TYPE;
8210     l_dummy			    number;
8211 
8212     -- Receipt Info
8213     l_applied_to_application_id	    zx_transaction_lines_gt.applied_to_application_id%TYPE;
8214     l_applied_to_entity_code	    zx_transaction_lines_gt.applied_to_entity_code%TYPE;
8215     l_applied_to_event_class_code   zx_transaction_lines_gt.applied_to_event_class_code%TYPE;
8216     l_trx_receipt_date		    zx_transaction_lines_gt.trx_receipt_date%TYPE;
8217 
8218     -- Correction Invoices
8219     l_adj_doc_application_id	    zx_transaction_lines_gt.adjusted_doc_application_id%TYPE;
8220     l_adj_doc_entity_code	    zx_transaction_lines_gt.adjusted_doc_entity_code%TYPE;
8221     l_adj_doc_event_class_code	    zx_transaction_lines_gt.adjusted_doc_event_class_code%TYPE;
8222     l_adj_doc_number		    zx_transaction_lines_gt.adjusted_doc_number%TYPE;
8223     l_adj_doc_date		    zx_transaction_lines_gt.adjusted_doc_date%TYPE;
8224     l_adj_doc_trx_level_type	    zx_transaction_lines_gt.adjusted_doc_trx_level_type%TYPE; --Bug8332737
8225 
8226 
8227     l_dflt_tax_class_code	    zx_transaction_lines_gt.input_tax_classification_code%type;
8228     l_allow_tax_code_override	    varchar2(10);
8229 
8230     l_return_status                 BOOLEAN := TRUE;
8231     j                               INT := 1;
8232     k                               INT := 1;
8233     l_pseudo                        INT := 1; -- bug 8839697: add
8234     l_pseudo2                       INT := 1; -- bug 8839697: add
8235 
8236     l_prorating_total               NUMBER;
8237     l_total_prorated                NUMBER;
8238 
8239     l_ship_to_party_id          po_line_locations_all.ship_to_organization_id%type; -- 7262269
8240 
8241     l_api_name                    CONSTANT VARCHAR2(100) := 'Populate_Lines_Import_GT';
8242 
8243     l_copy_line_dff_flag         VARCHAR2(1) := 'N'; -- Bug10082937
8244 
8245    l_default_ccid      	AP_INVOICE_LINES_INTERFACE.default_dist_ccid%TYPE; --Bug9841978
8246 
8247   BEGIN
8248 
8249     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Populate_Lines_Import_GT<-' ||
8250                                P_calling_sequence;
8251     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8252         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_curr_calling_sequence);
8253     END IF;
8254     ----------------------------------------------------------------------
8255     l_debug_info := 'Step 1: Get location_id for org_id';
8256     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8257         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8258     END IF;
8259     ----------------------------------------------------------------------
8260     BEGIN
8261       SELECT location_id
8262         INTO l_bill_to_location_id
8263         FROM hr_all_organization_units
8264        WHERE organization_id = P_Invoice_Header_Rec.org_id;
8265 
8266     EXCEPTION
8267       WHEN no_data_found THEN
8268          l_bill_to_location_id := null;
8269     END;
8270 
8271     ----------------------------------------------------------------------
8272     l_debug_info := 'Step 1.1: Get location_id for vendor site';
8273     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8274         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8275     END IF;
8276     ----------------------------------------------------------------------
8277     BEGIN
8278       SELECT location_id, ship_to_location_id, fob_lookup_code
8279         INTO l_location_id, l_ship_to_location_id, l_fob_point
8280         FROM ap_supplier_sites_all
8281        WHERE vendor_site_id = P_Invoice_Header_Rec.vendor_site_id;
8282 
8283     EXCEPTION
8284       WHEN no_data_found THEN
8285          l_location_id		:= null;
8286 	 l_ship_to_location_id	:= null;
8290     ----------------------------------------------------------------------
8287 	 l_fob_point		:= null;
8288     END;
8289 
8291     l_debug_info := 'Step 3: Determine if the invoice is tax-only.  If the '||
8292                     ' invoice is not tax-only user line level action CREATE'||
8293                     ' always';
8294     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8295         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8296     END IF;
8297     ----------------------------------------------------------------------
8298     IF (P_Inv_Line_List.COUNT <> 0) THEN
8299 
8300     FOR i IN P_Inv_Line_List.FIRST..P_Inv_Line_List.LAST LOOP
8301 
8302       -- Invoice is not tax-only.  TRX lines will be populated in the
8303       -- ZX_TRANSAXTION_LINES_GT and TAX lines in ZX_IMPORT_TAX_LINES_GT
8304       -- allocation structure will be store in ZX_TRX_TAX_LINK_GT
8305 
8306       IF ( NVL(P_Invoice_Header_Rec.tax_only_flag, 'N') = 'N' ) THEN
8307 
8308         IF (P_inv_line_list(i).line_type_lookup_code <> 'TAX' ) THEN
8309           -------------------------------------------------------------------
8310           l_debug_info := 'Step 4: Get line_level_action for line ITEM number'||
8311                           P_inv_line_list(i).line_number ;
8312           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8313              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8314           END IF;
8315           -------------------------------------------------------------------
8316             /*  --Bug12660669 Added source, invoice type and line type check */
8317           IF (P_Invoice_Header_Rec.source = 'SelfService' AND
8318 		        P_Invoice_Header_Rec.invoice_type_lookup_code = 'EXPENSE REPORT' AND
8319                 P_inv_line_list(i).line_type_lookup_code = 'MISCELLANEOUS' ) THEN
8320 
8321 		   l_line_level_action := 'RECORD_WITH_NO_TAX' ;
8322 
8323           ELSE
8324                    l_line_level_action := 'CREATE';
8325 	  END IF;
8326           -------------------------------------------------------------------
8327           l_debug_info := 'Step 5: Get Additional PO matched  info ';
8328           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8329              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8330           END IF;
8331           -------------------------------------------------------------------
8332 
8333 	  --bug14189635, changed IF condition to check variable p_inv_line_list instead of l_inv_line_list
8334           IF ( P_Inv_Line_List(i).po_line_location_id IS NOT NULL AND
8335                p_inv_line_list(i).line_type_lookup_code NOT IN ('MISCELLANEOUS','FREIGHT')) THEN   --Bug14039130
8336 
8337               -- this assignment is required since the p_po_line_location_id
8338               -- parameter is IN/OUT.  However, in this case it will not be
8339               -- modified because the po_distribution_id is not provided
8340 
8341             l_po_line_location_id := P_Inv_Line_List(i).po_line_location_id;
8342 
8343             IF NOT (AP_ETAX_UTILITY_PKG.Get_PO_Info(
8344                P_Po_Line_Location_Id         => l_po_line_location_id,
8345                P_PO_Distribution_Id          => null,
8346                P_Application_Id              => l_ref_doc_application_id,
8347                P_Entity_code                 => l_ref_doc_entity_code,
8348                P_Event_Class_Code            => l_ref_doc_event_class_code,
8349                P_PO_Quantity                 => l_ref_doc_line_quantity,
8350                P_Product_Org_Id              => l_product_org_id,
8351                P_Po_Header_Id                => l_ref_doc_trx_id,
8352                P_Po_Header_curr_conv_rate    => l_po_header_curr_conv_rate,
8353 	           P_Uom_Code		             => l_uom_code,
8354                P_Dist_Qty                    => l_dummy,
8355                P_Ship_Price                  => l_dummy,
8356                P_Error_Code                  => P_error_code,
8357                P_Calling_Sequence            => l_curr_calling_sequence)) THEN
8358 
8359               l_return_status := FALSE;
8360             END IF;
8361 
8362             l_ref_doc_trx_level_type := 'SHIPMENT';
8363 
8364           ELSE
8365              l_ref_doc_application_id     := Null;
8366              l_ref_doc_entity_code        := Null;
8367              l_ref_doc_event_class_code   := Null;
8368              l_ref_doc_line_quantity      := Null;
8369              l_product_org_id             := Null;
8370              l_ref_doc_trx_id             := Null;
8371              l_ref_doc_trx_level_type     := Null;
8372           END IF;
8373 
8374           -------------------------------------------------------------------
8375           l_debug_info := 'Step 6: Get Additional receipt matched info ';
8376           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8377              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8378           END IF;
8379           -------------------------------------------------------------------
8380 
8381 	  --bug14189635, changed IF condition to check variable p_inv_line_list instead of l_inv_line_list
8382           IF ( l_return_status = TRUE AND
8383                P_Inv_Line_List(i).rcv_transaction_id IS NOT NULL AND
8384                p_inv_line_list(i).line_type_lookup_code NOT IN ('MISCELLANEOUS','FREIGHT')) THEN   --Bug14039130
8385 
8386             IF NOT (AP_ETAX_UTILITY_PKG.Get_Receipt_Info(
8387                P_Rcv_Transaction_Id          => P_Inv_Line_List(i).rcv_transaction_id,
8388                P_Application_Id              => l_applied_to_application_id,
8389                P_Entity_code                 => l_applied_to_entity_code,
8390                P_Event_Class_Code            => l_applied_to_event_class_code,
8391                P_Transaction_Date            => l_trx_receipt_date,
8395                l_return_status := FALSE;
8392                P_Error_Code                  => P_error_code,
8393                P_Calling_Sequence            => l_curr_calling_sequence)) THEN
8394 
8396             END IF;
8397           ELSE
8398              l_applied_to_application_id   := Null;
8399              l_applied_to_entity_code      := Null;
8400              l_applied_to_event_class_code := Null;
8401              l_trx_receipt_date            := Null;
8402           END IF;
8403 
8404           -------------------------------------------------------------------
8405           l_debug_info := 'Step 8: Get Additional Correction Invoice Info ';
8406           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8407              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8408           END IF;
8409           -------------------------------------------------------------------
8410           IF ( l_return_status = TRUE AND
8411                P_Inv_Line_List(i).corrected_inv_id IS NOT NULL AND
8412                P_Inv_Line_list(i).price_correct_inv_line_num IS NOT NULL) THEN
8413 
8414             IF NOT (AP_ETAX_UTILITY_PKG.Get_Corrected_Invoice_Info(
8415                P_Corrected_Invoice_Id        => P_Inv_Line_List(i).corrected_inv_id,
8416                P_Corrected_Line_Number       => P_Inv_Line_List(i).price_correct_inv_line_num,
8417                P_Application_Id              => l_adj_doc_application_id,
8418                P_Entity_code                 => l_adj_doc_entity_code,
8419                P_Event_Class_Code            => l_adj_doc_event_class_code,
8420                P_Invoice_Number              => l_adj_doc_number,
8421                P_Invoice_Date                => l_adj_doc_date,
8422                P_Error_Code                  => P_error_code,
8423                P_Calling_Sequence            => l_curr_calling_sequence)) THEN
8424 
8425               l_return_status := FALSE;
8426             END IF;
8427 
8428 			l_adj_doc_trx_level_type := 'LINE'; /* Bug 12868432 */
8429 
8430           ELSE
8431             l_adj_doc_application_id   := Null;
8432             l_adj_doc_entity_code      := Null;
8433             l_adj_doc_event_class_code := Null;
8434             l_adj_doc_number           := Null;
8435             l_adj_doc_date             := Null;
8436           END IF;
8437 
8438           -------------------------------------------------------------------
8439           l_debug_info := 'Step 9: Get line_amt_includes_tax_flag';
8440           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8441              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8442           END IF;
8443           -------------------------------------------------------------------
8444 
8445 	  --bug14189635, changed IF condition to check variable p_inv_line_list instead of l_inv_line_list
8446           IF (P_Inv_Line_List(i).po_line_location_id IS NOT NULL AND
8447               p_inv_line_list(i).line_type_lookup_code NOT IN ('MISCELLANEOUS','FREIGHT')) THEN   --Bug14039130
8448             -- NONE
8449             l_line_amt_includes_tax_flag := 'N';
8450 
8451           ELSE
8452            IF (p_calling_mode = 'CALCULATE QUOTE')
8453               OR
8454               (p_invoice_header_rec.invoice_type_lookup_code = 'EXPENSE REPORT'
8455                and nvl(p_inv_line_list(i).line_type_lookup_code, 'N') <> 'PREPAY') THEN
8456              -- ALL
8457              l_line_amt_includes_tax_flag := 'A';
8458 
8459            ELSE
8460             -- STANDARD
8461             l_line_amt_includes_tax_flag := 'S';
8462            END IF;
8463           END IF;
8464 
8465 	  IF l_line_amt_includes_tax_flag = 'S' AND
8466              p_inv_line_list(i).amount_includes_tax_flag IS NOT NULL THEN
8467 	     IF (p_inv_line_list(i).amount_includes_tax_flag = 'Y' OR
8468 		 p_inv_line_list(i).amount_includes_tax_flag = 'A') THEN
8469                  -- ALL
8470                  l_line_amt_includes_tax_flag := 'A';
8471 	     ELSIF p_inv_line_list(i).amount_includes_tax_flag = 'N' THEN
8472                  -- NONE
8473                  l_line_amt_includes_tax_flag := 'N';
8474 	     ELSIF p_inv_line_list(i).amount_includes_tax_flag = 'S' THEN
8475                  -- STANDARD
8476                  l_line_amt_includes_tax_flag := 'S';
8477 	     ELSE
8478                  -- STANDARD
8479                  l_line_amt_includes_tax_flag := 'S';
8480 	     END IF;
8481 	  END IF;
8482 
8483 	  -----------------------------------------------------------------
8484           l_debug_info := 'l_line_amt_includes_tax_flag: '||l_line_amt_includes_tax_flag;
8485           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8486               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8487           END IF;
8488 
8489           l_debug_info := 'p_inv_line_list(i).amount_includes_tax_flag: '||p_inv_line_list(i).amount_includes_tax_flag;
8490           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8491               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8492           END IF;
8493           -----------------------------------------------------------------
8494 
8495           -------------------------------------------------------------------
8496           l_debug_info := 'Step 10: Get ctrl_hdr_tx_appl_flag';
8497           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8498               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8499           END IF;
8500           -------------------------------------------------------------------
8501           IF P_Invoice_Header_Rec.control_amount IS NOT NULL THEN
8502             l_ctrl_hdr_tx_appl_flag := 'Y';
8503           ELSE
8507           -------------------------------------------------------------------
8504             l_ctrl_hdr_tx_appl_flag := 'N';
8505           END IF;
8506 
8508           l_debug_info := 'Step 10.1: Get line_class';
8509           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8510               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8511           END IF;
8512           -------------------------------------------------------------------
8513           IF (l_return_status = TRUE) THEN
8514 
8515               IF NOT (AP_ETAX_UTILITY_PKG.Get_Line_Class(
8516                              P_Invoice_Type_Lookup_Code    => p_invoice_header_rec.invoice_type_lookup_code,
8517                              P_Inv_Line_Type               => p_inv_line_list(i).line_type_lookup_code,
8518                              P_Line_Location_Id            => p_inv_line_list(i).po_line_location_id,
8519                              P_Line_Class                  => l_line_class,
8520                              P_Error_Code                  => p_error_code,
8521                              P_Calling_Sequence            => l_curr_calling_sequence)) THEN
8522 
8523                   l_return_status := FALSE;
8524               END IF;
8525           END IF;
8526 
8527 	  --Bug9841978 STARTS
8528 
8529  IF P_Inv_Line_List(i).match_type IN
8530          ('ITEM_TO_PO','ITEM_TO_RECEIPT','ITEM_TO_SERVICE_PO',
8531           'ITEM_TO_SERVICE_RECEIPT','PRICE_CORRECTION','QTY_CORRECTION',
8532           'AMOUNT_CORRECTION') THEN
8533 
8534           IF  P_Inv_Line_List(i).po_line_location_id IS NOT NULL THEN
8535 
8536               SELECT pd.code_combination_id
8537                 INTO l_default_ccid
8538                 FROM po_distributions_all pd
8539                WHERE pd.line_location_id = P_Inv_Line_List(i).po_line_location_id
8540                  AND rownum = 1;
8541 
8542 	      /* Bug 8230574
8543 	         If the account on the Invoice Line is already populated and
8544 		 it is different from that being defaulted from the PO then
8545 		 retain the line level account....else use the defaulting
8546 		 from PO. */
8547 
8548 		 IF P_Inv_Line_List(i).dist_code_combination_id IS NOT NULL AND
8549                    P_Inv_Line_List(i).dist_code_combination_id <> l_default_ccid THEN
8550 
8551 		      -- retain the Invoice line cc_id
8552 
8553 		     l_default_ccid :=  P_Inv_Line_List(i).dist_code_combination_id  ;
8554 
8555                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8556                          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
8557                                  '1 Retaining the Line level cc_id '||P_Inv_Line_List(i).dist_code_combination_id);
8558                       END IF;
8559 
8560 		 ELSE
8561                        null;
8562 
8563                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8564                          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
8565                                  '1 Default CCID passed from PO '||l_default_ccid);
8566                       END IF;
8567 
8568 		 END IF;
8569 
8570           ELSIF P_Inv_Line_List(i).po_distribution_id IS NOT NULL THEN
8571 
8572               SELECT pd.code_combination_id
8573                 INTO l_default_ccid
8574                 FROM po_distributions_all pd
8575                WHERE pd.line_location_id =
8576                     (SELECT pod.line_location_id
8577                        FROM po_distributions_all pod
8578                       WHERE po_distribution_id = P_Inv_Line_List(i).po_distribution_id)
8579                  AND rownum = 1;
8580 
8581 	      /* Bug 8230574
8582 	         If the account on the Invoice Line is already populated and
8583 		 it is different from that being defaulted from the PO then
8584 		 retain the line level account....else use the defaulting
8585 		 from PO. */
8586 
8587 		 IF P_Inv_Line_List(i).dist_code_combination_id IS NOT NULL AND
8588                     P_Inv_Line_List(i).dist_code_combination_id <> l_default_ccid THEN
8589 
8590 		       l_default_ccid :=  P_Inv_Line_List(i).dist_code_combination_id  ; -- retain the Invoice line cc_id
8591 
8592                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8593                          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
8594                                  '3 Retaining the Line level cc_id '||P_Inv_Line_List(i).dist_code_combination_id);
8595                       END IF;
8596 
8597 		 ELSE
8598                      null ;
8599 
8600                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8601                          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
8602                                  '3 Default CCID passed from PO '||l_default_ccid);
8603                       END IF;
8604 
8605 		 END IF;
8606 
8607           ELSIF P_Inv_Line_List(i).rcv_shipment_line_id IS NOT NULL THEN
8608 
8609               SELECT pd.code_combination_id
8610                 INTO l_default_ccid
8611                 FROM po_distributions_all pd
8612                WHERE pd.line_location_id =
8613                      (SELECT rcv.po_line_location_id
8614                         FROM rcv_shipment_lines rcv
8615                        WHERE rcv.shipment_line_id = P_Inv_Line_List(i).rcv_shipment_line_id)
8616                  AND rownum = 1;
8617 
8618 	      /* Bug 8230574
8619 	         If the account on the Invoice Line is already populated and
8620 		 it is different from that being defaulted from the PO then
8621 		 retain the line level account....else use the defaulting
8622 		 from PO. */
8623 
8624 		 IF P_Inv_Line_List(i).dist_code_combination_id IS NOT NULL AND
8628 
8625                     P_Inv_Line_List(i).dist_code_combination_id <> l_default_ccid THEN
8626 
8627 		      l_default_ccid :=  P_Inv_Line_List(i).dist_code_combination_id  ; -- retain the Invoice line cc_id
8629                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8630                          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
8631                                  '4 Retaining the Line level cc_id '||P_Inv_Line_List(i).dist_code_combination_id);
8632                       END IF;
8633 
8634 		 ELSE
8635                       null;
8636 
8637                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8638                          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
8639                                  '4 Default CCID passed from PO '||l_default_ccid);
8640                       END IF;
8641 
8642 		 END IF;
8643 
8644           ELSIF P_Inv_Line_List(i).rcv_transaction_id IS NOT NULL THEN
8645 
8646               SELECT pd.code_combination_id
8647                 INTO l_default_ccid
8648                 FROM po_distributions_all pd
8649                WHERE pd.line_location_id =
8650                      (SELECT rcv.po_line_location_id
8651                         FROM rcv_transactions rcv
8652                        WHERE rcv.transaction_id = P_Inv_Line_List(i).rcv_transaction_id)
8653                  AND rownum = 1;
8654 
8655 
8656 	      /* Bug 8230574
8657 	         If the account on the Invoice Line is already populated and
8658 		 it is different from that being defaulted from the PO then
8659 		 retain the line level account....else use the defaulting
8660 		 from PO. */
8661 
8662 		 IF P_Inv_Line_List(i).dist_code_combination_id IS NOT NULL AND
8663                     P_Inv_Line_List(i).dist_code_combination_id <> l_default_ccid THEN
8664 
8665 		      l_default_ccid :=  P_Inv_Line_List(i).dist_code_combination_id  ; -- retain the Invoice line cc_id
8666 
8667                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8668                          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
8669                                  '5 Retaining the Line level cc_id '||P_Inv_Line_List(i).dist_code_combination_id);
8670                       END IF;
8671 
8672 		 ELSE
8673                       null;
8674 
8675                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8676                          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
8677                                  '5 Default CCID passed from PO '||l_default_ccid);
8678                       END IF;
8679 
8680 		 END IF;
8681 
8682           ELSE
8683               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8684                   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,
8685                                  '6 Default CCID passed from line rather than of PO Distribution');
8686               END IF;
8687           END IF;
8688 
8689 	  ELSE
8690           l_default_ccid :=  P_Inv_Line_List(i).dist_code_combination_id  ;
8691       END IF;
8692 
8693 
8694        IF (P_Inv_Line_List(i).dist_code_combination_id IS NULL AND
8695            P_Inv_Line_List(i).line_type_lookup_code <> 'PREPAY') THEN
8696 
8697          BEGIN
8698 
8699           SELECT aerd.code_combination_id
8700             INTO l_default_ccid
8701             FROM ap_exp_report_dists_all aerd,
8702                  ap_expense_report_lines_all aerl,
8703                  ap_invoices_all ai
8704            WHERE aerd.report_header_id = P_Inv_Line_List(i).reference_key1
8705              AND aerd.report_line_id = P_Inv_Line_List(i).reference_key2
8706              AND aerd.report_line_id = aerl.report_line_id
8707              AND aerd.report_header_id = aerl.report_header_id
8708              AND ai.invoice_id = P_Invoice_Header_Rec.invoice_id  --9841978
8709              AND ai.invoice_type_lookup_code = 'EXPENSE REPORT'
8710              AND rownum = 1;
8711 
8712          EXCEPTION
8713           WHEN OTHERS THEN
8714             NULL;
8715         END;
8716 
8717        END IF;
8718 
8719 --Bug9841978 ENDS
8720 
8721 
8722           IF (p_inv_line_list(i).tax_classification_code IS NULL) THEN
8723             --Bug12660669 Added source, invoice type and line type check
8724             IF (p_invoice_header_rec.source = 'SelfService' AND
8725 			    p_invoice_header_rec.invoice_type_lookup_code = 'EXPENSE REPORT' AND
8726                 p_inv_line_list(i).line_type_lookup_code = 'MISCELLANEOUS' ) THEN
8727 
8728                 -------------------------------------------------------------------
8729                 l_debug_info := 'tax_classification_code is NULL and Should remain NULL';
8730                 IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8731                    FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8732                 END IF;
8733                 ----------------------------------------------------------------
8734                 NULL;
8735             ELSE
8736 
8737 	      ZX_AP_TAX_CLASSIFICATN_DEF_PKG.get_default_tax_classification
8738 		            (p_ref_doc_application_id           => l_ref_doc_application_id,
8739 		             p_ref_doc_entity_code              => l_ref_doc_entity_code,
8740 		             p_ref_doc_event_class_code         => l_ref_doc_event_class_code,
8741 		             p_ref_doc_trx_id                   => l_ref_doc_trx_id,
8742 		             p_ref_doc_line_id                  => p_inv_line_list(i).po_line_location_id,
8743 		             p_ref_doc_trx_level_type           => 'SHIPMENT',
8744 		             p_vendor_id                        => p_invoice_header_rec.vendor_id,
8745 		             p_vendor_site_id                   => p_invoice_header_rec.vendor_site_id,
8749 		             p_ship_to_location_id              => nvl(p_inv_line_list(i).ship_to_location_id,l_ship_to_location_id),
8746 		             p_code_combination_id              => l_default_ccid,  --9841978
8747 		             p_concatenated_segments            => null,
8748 		             p_templ_tax_classification_cd      => null,
8750 		             p_ship_to_loc_org_id               => null,
8751 		             p_inventory_item_id                => p_inv_line_list(i).inventory_item_id,
8752 		             p_item_org_id                      => l_product_org_id,
8753 		             p_tax_classification_code          => l_dflt_tax_class_code,
8754 		             p_allow_tax_code_override_flag     => l_allow_tax_code_override,
8755 		             APPL_SHORT_NAME                    => 'SQLAP',
8756 		             FUNC_SHORT_NAME                    => 'NONE',
8757 		             p_calling_sequence                 => 'AP_ETAX_SERVICES_PKG',
8758 		             p_event_class_code                 => p_event_class_code,
8759 		             p_entity_code                      => 'AP_INVOICES',
8760 		             p_application_id                   => 200,
8761 		             p_internal_organization_id         => p_invoice_header_rec.org_id);
8762 	     END IF; --Bug12660669 Added source, invoice type and line type check
8763 
8764 	 END IF;
8765 
8766          -------------------------------------------------------------------
8767          l_debug_info := 'Step 11: Populate pl/sql table';
8768          IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8769              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8770          END IF;
8771          -------------------------------------------------------------------
8772          IF (l_return_status = TRUE ) THEN
8773 
8774             trans_lines.EXTEND(1);
8775 
8776 			j := trans_lines.COUNT; /* bug 12955467 */
8777 
8778             trans_lines(j).application_id		:= 200;
8779             trans_lines(j).entity_code			:= 'AP_INVOICES';
8780             trans_lines(j).event_class_code 		:= p_event_class_code;
8781 
8782             trans_lines(j).trx_id 			:= P_Invoice_Header_Rec.invoice_id;
8783             trans_lines(j).trx_level_type 		:= 'LINE';
8784             trans_lines(j).trx_line_id			:= p_inv_line_list(i).line_number;
8785             trans_lines(j).line_level_action		:= l_line_level_action;
8786 	        trans_lines(j).line_class			:= l_line_class;
8787 
8788             trans_lines(j).trx_receipt_date	 	:= l_trx_receipt_date;
8789             trans_lines(j).trx_line_type 	 	:= p_inv_line_list(i).line_type_lookup_code;
8790             trans_lines(j).trx_line_date	 	:= p_invoice_header_rec.invoice_date;
8791             trans_lines(j).trx_line_number              := p_inv_line_list(i).line_number;
8792             trans_lines(j).trx_line_description         := p_inv_line_list(i).description;
8793             trans_lines(j).trx_line_gl_date             := p_inv_line_list(i).accounting_date;
8794 
8795 
8796             trans_lines(j).account_ccid			:= l_default_ccid; --9841978
8797 
8798             trans_lines(j).line_amt                     := p_inv_line_list(i).amount + nvl(p_inv_line_list(i).retained_amount,0);
8799             trans_lines(j).trx_line_quantity            := p_inv_line_list(i).quantity_invoiced;
8800             trans_lines(j).unit_price                   := p_inv_line_list(i).unit_price;
8801             trans_lines(j).uom_code                     := l_uom_code;
8802 
8803             trans_lines(j).trx_business_category        := p_inv_line_list(i).trx_business_category;
8804             trans_lines(j).line_intended_use            := p_inv_line_list(i).primary_intended_use;
8805             trans_lines(j).user_defined_fisc_class      := p_inv_line_list(i).user_defined_fisc_class;
8806             trans_lines(j).product_fisc_classification	:= p_inv_line_list(i).product_fisc_classification;
8807 	        trans_lines(j).assessable_value		:= p_inv_line_list(i).assessable_value;
8808 
8809               --Bug12660669
8810               IF (p_invoice_header_rec.source = 'SelfService' AND
8811 			      p_invoice_header_rec.invoice_type_lookup_code = 'EXPENSE REPORT' AND
8812                   p_inv_line_list(i).line_type_lookup_code = 'MISCELLANEOUS') THEN
8813                   trans_lines(j).input_tax_classification_code := NULL; --Bug12660669
8814               ELSE
8815                   trans_lines(j).input_tax_classification_code := NVL(p_inv_line_list(i).tax_classification_code, l_dflt_tax_class_code);
8816               END IF;
8817 
8818             trans_lines(j).product_id                   := p_inv_line_list(i).inventory_item_id;
8819             trans_lines(j).product_org_id		:= l_product_org_id;
8820             trans_lines(j).product_type			:= p_inv_line_list(i).product_type;
8821             trans_lines(j).product_category		:= p_inv_line_list(i).product_category;
8822             trans_lines(j).product_description		:= p_inv_line_list(i).item_description;
8823             trans_lines(j).fob_point			:= l_fob_point;
8824 
8825             -- AP is not going to pass this parameter.  eTax is aware of this and they will derive this.
8826             -- trans_lines(j).product_code
8827 
8828             -- 7262269 /*Bug 13079028: Removed the IF-ELSE and called the function directly to handle all cases*/
8829             --IF p_inv_line_list(i).po_line_location_id IS NOT NULL THEN
8830 
8831             --Bug14039130
8832 	     --bug14189635, changed IF condition to check variable p_inv_line_list instead of l_inv_line_list
8833              IF p_inv_line_list(i).line_type_lookup_code NOT IN ('MISCELLANEOUS','FREIGHT') THEN
8834                 l_po_line_location_id := p_inv_line_list(i).po_line_location_id;
8835              ELSE
8836                 l_po_line_location_id := NULL;
8837              END IF;
8838 
8839              l_ship_to_party_id := get_po_ship_to_org_id (l_po_line_location_id,
8840 	                                                      p_inv_line_list(i).inventory_item_id, /*Bug 13079028*/
8841 							                              p_inv_line_list(i).org_id, /*Bug 13079028*/
8845             --Bug14039130
8842 							                              p_inv_line_list(i).application_id, /*Bug 13079028*/
8843 							                              p_inv_line_list(i).reference_key5); /*Bug 13079028*/
8844 
8846 
8847            /*ELSE
8848              l_ship_to_party_id := p_inv_line_list(i).org_id;
8849             END IF;*/ /*Bug 13079028*/
8850 
8851             trans_lines(j).ship_to_party_id		:= l_ship_to_party_id; /* Changed the subscript from i to j for bug#7319191 */
8852             -- 7262269
8853 
8854             trans_lines(j).ship_from_party_id		:= P_Invoice_Header_Rec.party_id;
8855 
8856             trans_lines(j).bill_to_party_id		:= p_inv_line_list(i).org_id;
8857             trans_lines(j).bill_from_party_id		:= P_Invoice_Header_Rec.party_id;
8858 
8859             trans_lines(j).ship_from_party_site_id	:= P_Invoice_Header_Rec.party_site_id;
8860             trans_lines(j).bill_from_party_site_id	:= P_Invoice_Header_Rec.party_site_id;
8861 
8862             trans_lines(j).ship_to_location_id		:= nvl(p_inv_line_list(i).ship_to_location_id,l_ship_to_location_id);
8863 	        trans_lines(j).ship_from_location_id	:= l_location_id;
8864             trans_lines(j).bill_to_location_id		:= l_bill_to_location_id;
8865             trans_lines(j).bill_from_location_id        := l_location_id;
8866 
8867             trans_lines(j).ref_doc_application_id	:= l_ref_doc_application_id;
8868             trans_lines(j).ref_doc_entity_code		:= l_ref_doc_entity_code;
8869             trans_lines(j).ref_doc_event_class_code	:= l_ref_doc_event_class_code;
8870             trans_lines(j).ref_doc_trx_id		:= l_ref_doc_trx_id;
8871 
8872             --Bug14039130
8873 	    --bug14189635, changed IF condition to check variable p_inv_line_list instead of l_inv_line_list
8874             IF p_inv_line_list(i).line_type_lookup_code NOT IN ('MISCELLANEOUS','FREIGHT') THEN
8875                trans_lines(j).ref_doc_line_id		:= p_inv_line_list(i).po_line_location_id;
8876             ELSE
8877                trans_lines(j).ref_doc_line_id		:= NULL;
8878             END IF;
8879             --Bug14039130
8880 
8881             trans_lines(j).ref_doc_line_quantity	:= l_ref_doc_line_quantity;
8882 	        trans_lines(j).ref_doc_trx_level_type       := l_ref_doc_trx_level_type; -- bug 8578833
8883 
8884             -- Not require to populate this values here since this function will
8885             -- not be run for prepayment application
8886             -- trans_lines(j).applied_from_application_id
8887             -- trans_lines(j).applied_from_entity_code
8888             -- trans_lines(j).applied_from_event_class_code
8889             -- trans_lines(j).applied_from_trx_id
8890             -- trans_lines(j).applied_from_line_id
8891 
8892             trans_lines(j).adjusted_doc_application_id	 := l_adj_doc_application_id;
8893             trans_lines(j).adjusted_doc_entity_code	 := l_adj_doc_entity_code;
8894             trans_lines(j).adjusted_doc_event_class_code := l_adj_doc_event_class_code;
8895             trans_lines(j).adjusted_doc_trx_id		 := p_inv_line_list(i).corrected_inv_id;
8896             trans_lines(j).adjusted_doc_line_id		 := p_inv_line_list(i).price_correct_inv_line_num;
8897             trans_lines(j).adjusted_doc_number		 := l_adj_doc_number;
8898             trans_lines(j).adjusted_doc_date		 := l_adj_doc_date;
8899 
8900             trans_lines(j).applied_to_application_id	 := l_applied_to_application_id;
8901             trans_lines(j).applied_to_entity_code	 := l_applied_to_entity_code;
8902             trans_lines(j).applied_to_event_class_code	 := l_applied_to_event_class_code;
8903 
8904 
8905             --Bug14039130
8906 	    --bug14189635, changed IF condition to check variable p_inv_line_list instead of l_inv_line_list
8907             IF p_inv_line_list(i).line_type_lookup_code NOT IN ('MISCELLANEOUS','FREIGHT') THEN
8908                trans_lines(j).applied_to_trx_id		 := p_inv_line_list(i).rcv_transaction_id;
8909             ELSE
8910                trans_lines(j).applied_to_trx_id		 := NULL;
8911             END IF;
8912 
8913 	    --bug14189635, changed IF condition to check variable p_inv_line_list instead of l_inv_line_list
8914             IF p_inv_line_list(i).rcv_transaction_id IS NOT NULL AND
8915                p_inv_line_list(i).line_type_lookup_code NOT IN ('MISCELLANEOUS','FREIGHT') THEN
8916 
8917                trans_lines(j).applied_to_trx_line_id	 := p_inv_line_list(i).po_line_location_id;
8918             ELSE
8919                trans_lines(j).applied_to_trx_line_id	 := NULL;
8920             END IF;
8921             --Bug14039130
8922 
8923             trans_lines(j).source_application_id   	 := p_inv_line_list(i).source_application_id;
8924             trans_lines(j).source_entity_code      	 := p_inv_line_list(i).source_entity_code;
8925             trans_lines(j).source_event_class_code 	 := p_inv_line_list(i).source_event_class_code;
8926             trans_lines(j).source_trx_id           	 := p_inv_line_list(i).source_trx_id;
8927             trans_lines(j).source_line_id          	 := p_inv_line_list(i).source_line_id;
8928             trans_lines(j).source_trx_level_type   	 := p_inv_line_list(i).source_trx_level_type;
8929 
8930             trans_lines(j).line_amt_includes_tax_flag	 := l_line_amt_includes_tax_flag;
8931             trans_lines(j).ctrl_hdr_tx_appl_flag	 := l_ctrl_hdr_tx_appl_flag;
8932             trans_lines(j).ctrl_total_line_tx_amt	 := p_inv_line_list(i).control_amount;
8933 
8934             -- This function will be called only from the import program.  This
8935             -- flag will be always N.
8936             trans_lines(j).historical_flag := 'N';
8937 -- Debug messages added for 6321366
8938  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8939               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_transaction_lines_gt values ');
8940               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: ' || trans_lines(j).event_class_code);
8941               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '           || trans_lines(j).trx_id);
8945               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_level_action: '|| trans_lines(j).line_level_action);
8942               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_id: '      || trans_lines(j).trx_line_id);
8943               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_level_type: '   || trans_lines(j).trx_level_type);
8944               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_type: '    || trans_lines(j).trx_line_type );
8946               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_class: '       || trans_lines(j).line_class);
8947               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_amt: '         || trans_lines(j).line_amt);
8948               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'unit_price: '       || trans_lines(j).unit_price);
8949               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);
8950                 --Bug12660669 Added below two debug stmts
8951 		--bug14189635, changed from trans_lines(i) to trans_lines(j) for below two lines
8952                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'default dist ccid: ' || trans_lines(j).account_ccid);
8953                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'input_tax_classification_code: ' || trans_lines(j).input_tax_classification_code);
8954  END IF;
8955 
8956             --j := j + 1; /*  bug 12955467 */
8957 
8958           END IF; -- l_return_status
8959 
8960        ELSE  -- The line is TAX
8961 
8962           -------------------------------------------------------------------
8963           l_debug_info := 'Step 12: Populate pl/sql table if TAX line';
8964           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
8965              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
8966           END IF;
8967           -------------------------------------------------------------------
8968           IF (l_return_status = TRUE ) THEN
8969             tax_lines.EXTEND(1);
8970 
8971             tax_lines(k).summary_tax_line_number := p_inv_line_list(i).line_number;
8972             tax_lines(k).internal_organization_id := P_Invoice_Header_Rec.org_id;
8973             tax_lines(k).application_id := 200;
8974 
8975             tax_lines(k).entity_code := 'AP_INVOICES';
8976             tax_lines(k).event_class_code := p_event_class_code;
8977             tax_lines(k).trx_id := P_Invoice_Header_Rec.invoice_id;
8978 
8979             -- Not used by AP
8980             -- tax_lines(k).hrd_trx_user_key1..6
8981 
8982             tax_lines(k).tax_regime_code := p_inv_line_list(i).tax_regime_code;
8983             tax_lines(k).tax := p_inv_line_list(i).tax;
8984             tax_lines(k).tax_status_code := p_inv_line_list(i).tax_status_code;
8985             tax_lines(k).tax_rate_code := nvl(p_inv_line_list(i).tax_rate_code,
8986                      p_inv_line_list(i).TAX_CLASSIFICATION_CODE); --bug6255826
8987             tax_lines(k).tax_rate := p_inv_line_list(i).tax_rate;
8988             tax_lines(k).tax_amt :=  p_inv_line_list(i).amount;
8989             tax_lines(k).tax_jurisdiction_code := p_inv_line_list(i).tax_jurisdiction_code;
8990             tax_lines(k).tax_amt_included_flag := p_inv_line_list(i).incl_in_taxable_line_flag;
8991             tax_lines(k).tax_rate_id := p_inv_line_list(i).tax_rate_id;
8992 
8993             --Bug10082937
8994             l_copy_line_dff_flag := NVL(fnd_profile.value('AP_COPY_INV_LINE_DFF'),'N');
8995             IF l_copy_line_dff_flag = 'Y' THEN
8996                tax_lines(k).ATTRIBUTE1         :=  p_inv_line_list(i).ATTRIBUTE1;
8997                tax_lines(k).ATTRIBUTE2         :=  p_inv_line_list(i).ATTRIBUTE2;
8998                tax_lines(k).ATTRIBUTE3         :=  p_inv_line_list(i).ATTRIBUTE3;
8999                tax_lines(k).ATTRIBUTE4         :=  p_inv_line_list(i).ATTRIBUTE4;
9000                tax_lines(k).ATTRIBUTE5         :=  p_inv_line_list(i).ATTRIBUTE5;
9001                tax_lines(k).ATTRIBUTE6         :=  p_inv_line_list(i).ATTRIBUTE6;
9002                tax_lines(k).ATTRIBUTE7         :=  p_inv_line_list(i).ATTRIBUTE7;
9003                tax_lines(k).ATTRIBUTE8         :=  p_inv_line_list(i).ATTRIBUTE8;
9004                tax_lines(k).ATTRIBUTE9         :=  p_inv_line_list(i).ATTRIBUTE9;
9005                tax_lines(k).ATTRIBUTE10        :=  p_inv_line_list(i).ATTRIBUTE10;
9006                tax_lines(k).ATTRIBUTE11        :=  p_inv_line_list(i).ATTRIBUTE11;
9007                tax_lines(k).ATTRIBUTE12        :=  p_inv_line_list(i).ATTRIBUTE12;
9008                tax_lines(k).ATTRIBUTE13        :=  p_inv_line_list(i).ATTRIBUTE13;
9009                tax_lines(k).ATTRIBUTE14        :=  p_inv_line_list(i).ATTRIBUTE14;
9010                tax_lines(k).ATTRIBUTE15        :=  p_inv_line_list(i).ATTRIBUTE15;  --BUG13719477
9011                tax_lines(k).ATTRIBUTE_CATEGORY := p_inv_line_list(i).ATTRIBUTE_CATEGORY;
9012             END IF;
9013             --Bug10082937
9014 
9015 
9016 
9017 
9018             /*6255826  Added following if condition to populate
9019                        tax_line_allocation_flag correctly*/
9020 
9021             --bug 6412397 - changed the index of p_inv_line_list
9022             --              from k to i
9023 
9024             IF  (p_inv_line_list(i).prorate_across_flag = 'Y' AND
9025                      p_inv_line_list(i).line_group_number IS NOT NULL) THEN
9026                 tax_lines(k).tax_line_allocation_flag       := 'Y';
9027             ELSE
9028                 tax_lines(k).tax_line_allocation_flag       := 'N';
9029             END IF;
9030 -- Debug messages added for 6321366
9031  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9032               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_import_tax_lines_gt values ');
9033               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'summary_tax_line_number: ' || tax_lines(k).summary_tax_line_number);
9037               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax: '   || tax_lines(k).tax );
9034               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'application_id: '           || tax_lines(k).application_id);
9035               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: '      || tax_lines(k).event_class_code);
9036 	          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '   || tax_lines(k).trx_id);
9038               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax_amt: '    || tax_lines(k).tax_amt );
9039               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax_amt_included_flag: '|| tax_lines(k).tax_amt_included_flag);
9040               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax_line_allocation_flag: '|| tax_lines(k).tax_line_allocation_flag);
9041  END IF;
9042 
9043             -- k := k + 1; bug 8839697
9044 
9045             -------------------------------------------------------------------
9046             l_debug_info := 'Step 13: Populate allocation structure if needed';
9047             IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9048                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9049             END IF;
9050             -------------------------------------------------------------------
9051 
9052             IF (AP_IMPORT_INVOICES_PKG.g_source IN('ISP', 'ASBN')) THEN
9053               -------------------------------------------------------------------
9054               l_debug_info := 'Step 14: Populate allocation using taxable_flag if '||
9055                               'source is ISP or ASBN';
9056               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9057                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9058               END IF;
9059               -------------------------------------------------------------------
9060 
9061               INSERT INTO zx_trx_tax_link_gt (
9062                 application_id,
9063                 entity_code,
9064                 event_class_code,
9065                 trx_id,
9066                 trx_level_type,
9067                 trx_line_id,
9068                 summary_tax_line_number,
9069                 line_amt
9070               ) SELECT
9071                   200,                                           -- application_id
9072                   'AP_INVOICES',                                 -- entity_code
9073                   p_event_class_code,                            -- event_class_code
9074                   P_Invoice_Header_Rec.invoice_id,               -- trx_id
9075                   'LINE',                                        -- trx_level_type
9076                   aili.line_number,                              -- trx_line_id
9077                   p_inv_line_list(i).line_number,                -- summary_tax_line_number
9078                   AP_UTILITIES_PKG.ap_round_currency(
9079                     p_inv_line_list(i).amount*aili.amount/l_prorating_total,
9080                     P_Invoice_Header_Rec.invoice_currency_code)  -- line_amt
9081                  FROM ap_invoice_lines_interface aili
9082                 WHERE aili.invoice_id = P_Invoice_Header_Rec.invoice_id
9083                   AND aili.line_number <> p_inv_line_list(i).line_number
9084                   AND aili.line_type_lookup_code <> 'TAX'
9085                   AND NVL(aili.taxable_flag, 'N') = 'Y';
9086 -- Debug messages added for 6321366
9087 
9088               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9089                  FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Rows instrted in zx_trx_tax_link_gt: '|| sql%rowcount);
9090               END IF;
9091               --------------------------------------------------------------
9092               l_debug_info := 'Step 15: Verify if there is any rounding and '||
9093                               'apply it to max of largest.';
9094               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9095                  FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9096               END IF;
9097               --------------------------------------------------------------
9098               BEGIN
9099                 SELECT SUM(NVL(line_amt,0))
9100                   INTO l_total_prorated
9101                   FROM zx_trx_tax_link_gt
9102                  WHERE trx_id = P_Invoice_Header_Rec.invoice_id
9103                    AND summary_tax_line_number = p_inv_line_list(i).line_number;
9104 
9105                 IF (NVL(p_inv_line_list(i).amount, 0) <> l_total_prorated) THEN
9106                   UPDATE zx_trx_tax_link_gt
9107                      SET line_amt = line_amt + (p_inv_line_list(i).amount - l_total_prorated)
9108                    WHERE trx_id = P_Invoice_Header_Rec.invoice_id
9109                      AND trx_line_id <> p_inv_line_list(i).line_number
9110                      AND trx_line_id =
9111                         (SELECT (MAX(aili.line_number))
9112                            FROM ap_invoice_lines_interface aili
9113                           WHERE aili.invoice_id = P_Invoice_Header_Rec.invoice_id
9114                             AND aili.line_number <> p_inv_line_list(i).line_number
9115                             AND aili.amount <> 0
9116                             AND aili.line_type_lookup_code <> 'TAX'
9117                             AND NVL(aili.taxable_flag, 'N') = 'Y'
9118                             AND ABS(aili.amount) >=
9119                               ( SELECT MAX(ABS(ail2.amount))
9120                                   FROM ap_invoice_lines_interface ail2
9121                                  WHERE ail2.invoice_id = aili.invoice_id
9122                                    AND ail2.line_number <> p_inv_line_list(i).line_number
9123                                    AND ail2.line_number <> aili.line_number
9124                                    AND ail2.line_type_lookup_code <> 'TAX'
9125                                    AND NVL(ail2.taxable_flag, 'N') = 'Y'));
9126 
9127                 END IF;
9131               END;
9128               EXCEPTION
9129                 WHEN OTHERS THEN
9130                   l_return_status := FALSE;
9132 
9133 
9134             ELSE -- source is not ISP or ASBN.  Allocations will be based on the
9135                  -- prorate_across_flag and line_group_number is available
9136               -------------------------------------------------------------------
9137               l_debug_info := 'Step 16: Populate allocation structure if needed';
9138               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9139                  FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9140               END IF;
9141               -------------------------------------------------------------------
9142               IF (p_inv_line_list(i).prorate_across_flag = 'Y' AND
9143                   p_inv_line_list(i).line_group_number IS NOT NULL) THEN
9144 
9145                    --------------------------------------------------------------
9146                    l_debug_info := 'Step 17: Get prorated total';
9147                    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9148                       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9149                    END IF;
9150                    --------------------------------------------------------------
9151                    SELECT SUM(NVL(amount, 0))
9152                      INTO l_prorating_total
9153                      FROM ap_invoice_lines_interface
9154                     WHERE invoice_id = P_Invoice_Header_Rec.invoice_id
9155                       AND line_number <> p_inv_line_list(i).line_number
9156 					  AND line_type_lookup_code <> 'TAX'     --Bug6608702**
9157                       AND line_group_number = p_inv_line_list(i).line_group_number;
9158 
9159                    --------------------------------------------------------------
9160                    l_debug_info := 'Step 18: Get Insert in global temp table';
9161                    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9162                       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9163                    END IF;
9164                    --------------------------------------------------------------
9165                    IF (l_prorating_total <> 0) THEN
9166                      INSERT INTO zx_trx_tax_link_gt (
9167                        application_id,
9168                        entity_code,
9169                        event_class_code,
9170                        trx_id,
9171                        trx_level_type,
9172                        trx_line_id,
9173                        summary_tax_line_number,
9174                        line_amt
9175                      ) SELECT
9176                          200,                                           -- application_id
9177                          'AP_INVOICES',                                 -- entity_code
9178                          p_event_class_code,                            -- event_class_code
9179                          P_Invoice_Header_Rec.invoice_id,               -- trx_id
9180                          'LINE',                                        -- trx_level_type
9181                          aili.line_number,                              -- trx_line_id
9182                          p_inv_line_list(i).line_number,                -- summary_tax_line_number
9183                          AP_UTILITIES_PKG.ap_round_currency(
9184                            p_inv_line_list(i).amount*aili.amount/l_prorating_total,
9185                            P_Invoice_Header_Rec.invoice_currency_code)  -- line_amt
9186                         FROM ap_invoice_lines_interface aili
9187                        WHERE aili.invoice_id = P_Invoice_Header_Rec.invoice_id
9188                          AND aili.line_number <> p_inv_line_list(i).line_number
9189 						 AND aili.line_type_lookup_code <> 'TAX' --Bug6608702**
9190                          AND aili.line_group_number = p_inv_line_list(i).line_group_number;
9191 -- Debug messages added for 6321366
9192                    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9193                       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Rows instrted in zx_trx_tax_link_gt: '|| sql%rowcount);
9194                    END IF;
9195                    --------------------------------------------------------------
9196                    l_debug_info := 'Step 19: Verify if there is any rounding and '||
9197                                    'apply it to max of largest.';
9198                    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9199                       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9200                    END IF;
9201                    --------------------------------------------------------------
9202                    BEGIN
9203                      SELECT SUM(NVL(line_amt,0))
9204                        INTO l_total_prorated
9205                        FROM zx_trx_tax_link_gt
9206                       WHERE trx_id = P_Invoice_Header_Rec.invoice_id
9207                         AND summary_tax_line_number = p_inv_line_list(i).line_number;
9208 
9209                      IF (NVL(p_inv_line_list(i).amount, 0) <> l_total_prorated) THEN
9210                        UPDATE zx_trx_tax_link_gt
9211                           SET line_amt = line_amt + (p_inv_line_list(i).amount - l_total_prorated)
9212                         WHERE trx_id = P_Invoice_Header_Rec.invoice_id
9213                           AND trx_line_id <> p_inv_line_list(i).line_number
9214                           AND trx_line_id =
9215                              (SELECT (MAX(aili.line_number))
9216                                 FROM ap_invoice_lines_interface aili
9217                                WHERE aili.invoice_id = P_Invoice_Header_Rec.invoice_id
9218                                  AND aili.line_number <> p_inv_line_list(i).line_number
9219 								 AND aili.line_type_lookup_code <> 'TAX' --Bug6608702**
9220                                  AND aili.amount <> 0
9224                                        FROM  ap_invoice_lines_interface ail2
9221                                  AND aili.line_group_number = p_inv_line_list(i).line_group_number
9222                                  AND ABS(aili.amount) >=
9223                                    ( SELECT  MAX(ABS(ail2.amount))
9225                                       WHERE  ail2.invoice_id = aili.invoice_id
9226                                         AND  ail2.line_number <> p_inv_line_list(i).line_number
9227                                         AND  ail2.line_number <> aili.line_number
9228 										AND  ail2.line_type_lookup_code <> 'TAX' --Bug6608702**
9229                                         AND  ail2.line_group_number =
9230                                                p_inv_line_list(i).line_group_number));
9231                      END IF;
9232                    EXCEPTION
9233                      WHEN OTHERS THEN
9234                        l_return_status := FALSE;
9235                    END;
9236 
9237                   END IF;  -- l_prorating_total <> 0
9238 -- bug 8839697: add start
9239                 -- Added functionality to import tax only lines
9240                 -- case only when prorate across flag is set to 'N'
9241                 ELSIF (p_inv_line_list(i).prorate_across_flag = 'N') THEN
9242 
9243                    --------------------------------------------------------------
9244                    l_debug_info := 'Populating tax only line info';
9245                    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9246                       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9247                    END IF;
9248                    --------------------------------------------------------------
9249                       -- allocation flag is marked as N (above) when prorate accross flag is not Y
9250                       tax_lines(k).tax_line_allocation_flag           := 'Y';
9251 
9252                       trans_lines.EXTEND(1);
9253                       link_lines.EXTEND(1);
9254 
9255                       l_pseudo := trans_lines.COUNT;
9256                       l_pseudo2:= link_lines.COUNT;
9257 
9258                       -- add pseudo line for manual non prorated tax line
9259                       trans_lines(l_pseudo).application_id := 200;
9260                       trans_lines(l_pseudo).entity_code := 'AP_INVOICES';
9261                       trans_lines(l_pseudo).event_class_code := p_event_class_code;
9262                       trans_lines(l_pseudo).trx_id := P_Invoice_Header_Rec.invoice_id;
9263                       trans_lines(l_pseudo).trx_level_type := 'LINE';
9264                       trans_lines(l_pseudo).trx_line_id := p_inv_line_list(i).line_number;
9265                       trans_lines(l_pseudo).line_level_action := 'LINE_INFO_TAX_ONLY';
9266 
9267                       trans_lines(l_pseudo).trx_line_type := 'ITEM';
9268                       trans_lines(l_pseudo).trx_line_date := P_Invoice_Header_Rec.invoice_date;
9269                       trans_lines(l_pseudo).trx_business_category := p_inv_line_list(i).trx_business_category;
9270                       trans_lines(l_pseudo).line_intended_use := p_inv_line_list(i).primary_intended_use;
9271                       trans_lines(l_pseudo).user_defined_fisc_class := p_inv_line_list(i).user_defined_fisc_class;
9272                       trans_lines(l_pseudo).line_amt := p_inv_line_list(i).amount;
9273                       trans_lines(l_pseudo).trx_line_quantity := p_inv_line_list(i).quantity_invoiced;
9274                       trans_lines(l_pseudo).unit_price := p_inv_line_list(i).unit_price;
9275 
9276                       trans_lines(l_pseudo).product_id := p_inv_line_list(i).inventory_item_id;
9277                       trans_lines(l_pseudo).product_fisc_classification := p_inv_line_list(i).product_fisc_classification;
9278                       trans_lines(l_pseudo).product_type := p_inv_line_list(i).product_type;
9279                       trans_lines(l_pseudo).product_category := p_inv_line_list(i).product_category;
9280                       trans_lines(l_pseudo).fob_point := l_fob_point;
9281                       trans_lines(l_pseudo).ship_to_party_id:= p_inv_line_list(i).org_id;
9282 
9283                       trans_lines(l_pseudo).ship_from_party_id := P_Invoice_Header_Rec.party_id;
9284 
9285                       trans_lines(l_pseudo).bill_to_party_id:= p_inv_line_list(i).org_id;
9286                       trans_lines(l_pseudo).bill_from_party_id:= P_Invoice_Header_Rec.party_id;
9287                       trans_lines(l_pseudo).ship_from_party_site_id:= P_Invoice_Header_Rec.party_site_id;
9288                       trans_lines(l_pseudo).bill_from_party_site_id:= P_Invoice_Header_Rec.party_site_id;
9289 
9290                       trans_lines(l_pseudo).ship_to_location_id:= p_inv_line_list(i).ship_to_location_id;
9291                       trans_lines(l_pseudo).ship_from_location_id:= l_location_id;
9292                       trans_lines(l_pseudo).bill_to_location_id:= l_bill_to_location_id;
9293                       trans_lines(l_pseudo).bill_from_location_id:= l_location_id;
9294 
9295                       trans_lines(l_pseudo).account_ccid:= p_inv_line_list(i).default_dist_ccid;
9296                       trans_lines(l_pseudo).merchant_party_country:= p_inv_line_list(i).country_of_supply;
9297 
9298                       trans_lines(l_pseudo).trx_line_number:= p_inv_line_list(i).line_number;
9299                       trans_lines(l_pseudo).trx_line_description:= p_inv_line_list(i).description;
9300                       trans_lines(l_pseudo).product_description:= p_inv_line_list(i).item_description;
9301                       trans_lines(l_pseudo).trx_line_gl_date:= p_inv_line_list(i).accounting_date;
9302 
9303                       trans_lines(l_pseudo).merchant_party_name:= p_inv_line_list(i).merchant_name;
9304                       trans_lines(l_pseudo).merchant_party_document_number:= p_inv_line_list(i).merchant_document_number;
9305                       trans_lines(l_pseudo).merchant_party_reference:= p_inv_line_list(i).merchant_reference;
9306                       trans_lines(l_pseudo).merchant_party_taxpayer_id:= p_inv_line_list(i).merchant_taxpayer_id;
9310 
9307                       trans_lines(l_pseudo).merchant_party_tax_reg_number:= p_inv_line_list(i).merchant_tax_reg_number;
9308 
9309                       trans_lines(l_pseudo).assessable_value:= p_inv_line_list(i).assessable_value;
9311                       IF (p_inv_line_list(i).po_line_location_id IS NOT NULL) THEN
9312                           -- NONE
9313                           l_line_amt_includes_tax_flag := 'N';
9314                       ELSE
9315                           IF (p_calling_mode = 'CALCULATE QUOTE')
9316                           OR
9317                          (p_invoice_header_rec.invoice_type_lookup_code = 'EXPENSE REPORT'
9318                           and nvl(p_inv_line_list(i).line_type_lookup_code, 'N') <> 'PREPAY') THEN
9319                              -- ALL
9320                              l_line_amt_includes_tax_flag := 'A';
9321                           ELSE
9322                              -- STANDARD
9323                              l_line_amt_includes_tax_flag := 'S';
9324                           END IF;
9325                       END IF;
9326                       trans_lines(l_pseudo).line_amt_includes_tax_flag:= l_line_amt_includes_tax_flag;
9327                       trans_lines(l_pseudo).historical_flag:= 'N';
9328 
9329                       IF P_Invoice_Header_Rec.control_amount IS NOT NULL THEN
9330                          l_ctrl_hdr_tx_appl_flag := 'Y';
9331                       ELSE
9332                          l_ctrl_hdr_tx_appl_flag := 'N';
9333                       END IF;
9334                       trans_lines(l_pseudo).ctrl_hdr_tx_appl_flag:= l_ctrl_hdr_tx_appl_flag;
9335                       trans_lines(l_pseudo).ctrl_total_line_tx_amt:= p_inv_line_list(i).control_amount;
9336 
9337                       trans_lines(l_pseudo).source_application_id:= p_inv_line_list(i).source_application_id;
9338                       trans_lines(l_pseudo).source_entity_code   := p_inv_line_list(i).source_entity_code;
9339                       trans_lines(l_pseudo).source_event_class_code := p_inv_line_list(i).source_event_class_code;
9340                       trans_lines(l_pseudo).source_trx_id   := p_inv_line_list(i).source_trx_id;
9341                       trans_lines(l_pseudo).source_line_id   := p_inv_line_list(i).source_line_id;
9342                       trans_lines(l_pseudo).source_trx_level_type:= p_inv_line_list(i).source_trx_level_type;
9343 
9344                       trans_lines(l_pseudo).input_tax_classification_code:= p_inv_line_list(i).tax_classification_code;
9345 
9346                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9347                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_transaction_lines_gt values ');
9348                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: ' || trans_lines(l_pseudo).event_class_code);
9349                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '           || trans_lines(l_pseudo).trx_id);
9350                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_id: '      || trans_lines(l_pseudo).trx_line_id);
9351                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_level_type: '   || trans_lines(l_pseudo).trx_level_type);
9352                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_type: '    || trans_lines(l_pseudo).trx_line_type );
9353                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_level_action: '|| trans_lines(l_pseudo).line_level_action);
9354                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_amt: '         || trans_lines(l_pseudo).line_amt);
9355                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'unit_price: '       || trans_lines(l_pseudo).unit_price);
9356                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_amt_includes_tax_flag: ' || trans_lines(l_pseudo).line_amt_includes_tax_flag );
9357                       END IF;
9358 
9359                       -- add to link gt
9360                       link_lines(l_pseudo2).application_id:= 200;
9361                       link_lines(l_pseudo2).entity_code:= 'AP_INVOICES';
9362                       link_lines(l_pseudo2).event_class_code:= p_event_class_code;
9363                       link_lines(l_pseudo2).trx_id:= P_Invoice_Header_Rec.invoice_id;
9364                       link_lines(l_pseudo2).trx_level_type:= 'LINE';
9365                       link_lines(l_pseudo2).trx_line_id:= p_inv_line_list(i).line_number;
9366                       link_lines(l_pseudo2).summary_tax_line_number:= p_inv_line_list(i).line_number;
9367                       link_lines(l_pseudo2).line_amt:= p_inv_line_list(i).amount;
9368 
9369                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9370                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_trx_tax_link_gt values ');
9371                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'summary_tax_line_number: ' || link_lines(l_pseudo2).summary_tax_line_number);
9372                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'application_id: '           || link_lines(l_pseudo2).application_id);
9373                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: '      || link_lines(l_pseudo2).event_class_code);
9374                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '   || link_lines(l_pseudo2).trx_id);
9375                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_level_type: '   || link_lines(l_pseudo2).trx_level_type );
9376                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_id: '   || link_lines(l_pseudo2).trx_line_id);
9377                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_amt: '    || link_lines(l_pseudo2).line_amt );
9378                       END IF;
9379 -- bug 8839697: add end
9383           END IF;  -- l_return_status validation for TAX lines
9380               END IF;  -- prorate_accross_flag = 'Y' and line_group_number is not null
9381             END IF;  -- End of if for the SOURCE of the invoice
9382             k := k + 1; -- bug 8839697
9384         END IF;  -- line type lookup code and it is not tax-only
9385 
9386 
9387       ELSE  -- It is a tax-only invoice
9388 
9389           -------------------------------------------------------------------
9390           l_debug_info := 'Step 4: Get line_level_action for TAX ONLY line number'||
9391                           P_inv_line_list(i).line_number ;
9392           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9393              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9394           END IF;
9395           -------------------------------------------------------------------
9396           IF (NVL(P_Invoice_Header_Rec.tax_only_rcv_matched_flag, 'N') = 'Y') THEN
9397             -- In this case  eTax will need to run tax
9398             -- applicability and calculation because it is the matched to other
9399             -- charges case. This flag is populated in the import program based on
9400             -- a select from the lines interface table.
9401             -- In this case the import program will call the calculate service
9402             -- This is transparent to the user since the
9403             -- calc_tax_during_import_flag should be set to N.  Otherwise, the
9404             -- invoice will be rejected.
9405 
9406             l_line_level_action := 'CREATE_TAX_ONLY';
9407           ELSE
9408             -- Invoice is tax-only, and there is no need to run applicability.
9409             -- In this case the user provides all the tax information for the line
9410             -- to be imported.  However, the additional taxable related info
9411             -- that eTax need to store will be passed to eTax using a pseudo line
9412             -- in the zx_transaction_lines_gt table.
9413 
9414             l_line_level_action := 'LINE_INFO_TAX_ONLY';
9415           END IF;
9416 
9417           -------------------------------------------------------------------
9418           l_debug_info := 'Step 5: Get Additional PO matched  info ';
9419           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9420              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9421           END IF;
9422           -------------------------------------------------------------------
9423 
9424           IF ( P_Inv_Line_List(i).po_line_location_id IS NOT NULL AND
9425                P_Inv_Line_List(i).line_type_lookup_code NOT IN ('MISCELLANEOUS','FREIGHT')) THEN  --Bug14039130
9426               -- this assigned is required since the p_po_line_location_id
9427               -- parameter is IN/OUT.  However, in this case it will not be
9428               -- modified because the po_distribution_id is not provided
9429               l_po_line_location_id := P_Inv_Line_List(i).po_line_location_id;
9430 
9431             IF NOT (AP_ETAX_UTILITY_PKG.Get_PO_Info(
9432                P_Po_Line_Location_Id         => l_po_line_location_id,
9433                P_PO_Distribution_Id          => null,
9434                P_Application_Id              => l_ref_doc_application_id,
9435                P_Entity_code                 => l_ref_doc_entity_code,
9436                P_Event_Class_Code            => l_ref_doc_event_class_code,
9437                P_PO_Quantity                 => l_ref_doc_line_quantity,
9438                P_Product_Org_Id              => l_product_org_id,
9439                P_Po_Header_Id                => l_ref_doc_trx_id,
9440                P_Po_Header_curr_conv_rate    => l_po_header_curr_conv_rate,
9441                P_Uom_Code                    => l_uom_code,
9442                P_Dist_Qty                    => l_dummy,
9443                P_Ship_Price                  => l_dummy,
9444                P_Error_Code                  => P_error_code,
9445                P_Calling_Sequence            => l_curr_calling_sequence)) THEN
9446 
9447               l_return_status := FALSE;
9448             END IF;
9449 
9450             l_ref_doc_trx_level_type := 'SHIPMENT';
9451 
9452           ELSE
9453             l_ref_doc_application_id     := Null;
9454             l_ref_doc_entity_code        := Null;
9455             l_ref_doc_event_class_code   := Null;
9456             l_ref_doc_line_quantity      := Null;
9457             l_product_org_id             := Null;
9458             l_ref_doc_trx_id             := Null;
9459             l_ref_doc_trx_level_type     := Null;
9460           END IF;
9461 
9462           -------------------------------------------------------------------
9463           l_debug_info := 'Step 6: Get Additional receipt matched info ';
9464           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9465              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9466           END IF;
9467           -------------------------------------------------------------------
9468           IF ( l_return_status = TRUE AND
9469                P_Inv_Line_List(i).rcv_transaction_id IS NOT NULL AND
9470                P_Inv_Line_List(i).line_type_lookup_code NOT IN ('MISCELLANEOUS','FREIGHT')) THEN     --Bug14039130
9471             IF NOT (AP_ETAX_UTILITY_PKG.Get_Receipt_Info(
9472                P_Rcv_Transaction_Id          => P_Inv_Line_List(i).rcv_transaction_id,
9473                P_Application_Id              => l_applied_to_application_id,
9474                P_Entity_code                 => l_applied_to_entity_code,
9475                P_Event_Class_Code            => l_applied_to_event_class_code,
9476                P_Transaction_Date            => l_trx_receipt_date,
9477                P_Error_Code                  => P_error_code,
9478                P_Calling_Sequence            => l_curr_calling_sequence)) THEN
9479 
9483             l_applied_to_application_id   := Null;
9480                l_return_status := FALSE;
9481             END IF;
9482           ELSE
9484             l_applied_to_entity_code      := Null;
9485             l_applied_to_event_class_code := Null;
9486             l_trx_receipt_date            := Null;
9487           END IF;
9488 
9489           -------------------------------------------------------------------
9490           l_debug_info := 'Step 8: Get Additional Correction Invoice Info ';
9491           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9492              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9493           END IF;
9494           -------------------------------------------------------------------
9495 
9496           IF ( l_return_status = TRUE AND
9497                P_Inv_Line_List(i).corrected_inv_id IS NOT NULL AND
9498                P_Inv_Line_list(i).price_correct_inv_line_num IS NOT NULL) THEN
9499             IF NOT (AP_ETAX_UTILITY_PKG.Get_Corrected_Invoice_Info(
9500                P_Corrected_Invoice_Id        => P_Inv_Line_List(i).corrected_inv_id,
9501                P_Corrected_Line_Number       => P_Inv_Line_List(i).price_correct_inv_line_num,
9502                P_Application_Id              => l_adj_doc_application_id,
9503                P_Entity_code                 => l_adj_doc_entity_code,
9504                P_Event_Class_Code            => l_adj_doc_event_class_code,
9505                P_Invoice_Number              => l_adj_doc_number,
9506                P_Invoice_Date                => l_adj_doc_date,
9507                P_Error_Code                  => P_error_code,
9508                P_Calling_Sequence            => l_curr_calling_sequence)) THEN
9509 
9510               l_return_status := FALSE;
9511             END IF;
9512               l_adj_doc_trx_level_type := 'LINE'; --Bug8332737
9513           ELSE
9514             l_adj_doc_application_id   := Null;
9515             l_adj_doc_entity_code      := Null;
9516             l_adj_doc_event_class_code := Null;
9517             l_adj_doc_number           := Null;
9518             l_adj_doc_date             := Null;
9519             l_adj_doc_trx_level_type   := NULL; --Bug8332737
9520 
9521           END IF;
9522 
9523           -------------------------------------------------------------------
9524           l_debug_info := 'Step 9: Get line_amt_includes_tax_flag';
9525           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9526              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9527           END IF;
9528           -------------------------------------------------------------------
9529 
9530           IF (P_Inv_Line_List(i).po_line_location_id IS NOT NULL) THEN
9531             -- NONE
9532             l_line_amt_includes_tax_flag := 'N';
9533 
9534           ELSE
9535            IF (p_calling_mode = 'CALCULATE QUOTE')
9536               OR
9537               (p_invoice_header_rec.invoice_type_lookup_code = 'EXPENSE REPORT'
9538 	      --bug14189635, changed IF condition to check variable p_inv_line_list instead of l_inv_line_list
9539                and nvl(p_inv_line_list(i).line_type_lookup_code, 'N') <> 'PREPAY') THEN
9540              -- ALL
9541              l_line_amt_includes_tax_flag := 'A';
9542 
9543            ELSE
9544              -- STANDARD
9545              IF p_calling_mode = 'VALIDATE IMPORT' THEN /* if condition added for 6010950 -
9546                  For tax only invoices we need to pass it as 'N' */
9547                 l_line_amt_includes_tax_flag := 'N';
9548             ELSE
9549                 l_line_amt_includes_tax_flag := 'S';
9550             END IF;
9551 
9552            END IF;
9553           END IF;
9554 
9555           -------------------------------------------------------------------
9556           l_debug_info := 'Step 10: Get ctrl_hdr_tx_appl_flag';
9557           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9558              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9559           END IF;
9560           -------------------------------------------------------------------
9561           IF P_Invoice_Header_Rec.control_amount IS NOT NULL THEN
9562             l_ctrl_hdr_tx_appl_flag := 'Y';
9563           ELSE
9564             l_ctrl_hdr_tx_appl_flag := 'N';
9565           END IF;
9566 
9567 
9568           -------------------------------------------------------------------
9569           l_debug_info := 'Step 10z: Get control_amount line level';
9570           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9571              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9572           END IF;
9573           -------------------------------------------------------------------
9574           IF (NVL(P_Invoice_Header_Rec.tax_only_rcv_matched_flag, 'N') = 'Y') THEN
9575             l_line_control_amount :=
9576               NVL(p_inv_line_list(i).control_amount, p_inv_line_list(i).amount);
9577 
9578           ELSE
9579             l_line_control_amount := p_inv_line_list(i).control_amount;
9580           END IF;
9581 
9582           -------------------------------------------------------------------
9583           l_debug_info := 'Step 11: Populate pl/sql table';
9584           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9585              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9586           END IF;
9587           -------------------------------------------------------------------
9588           IF (l_return_status = TRUE ) THEN
9589             trans_lines.EXTEND(1);
9590             trans_lines(j).line_amt_includes_tax_flag           := l_line_amt_includes_tax_flag ; ---for bug 6010950
9591             trans_lines(j).application_id 			:= 200;
9592             trans_lines(j).entity_code 				:= 'AP_INVOICES';
9593             trans_lines(j).event_class_code 			:= p_event_class_code;
9594             trans_lines(j).trx_id 				:= P_Invoice_Header_Rec.invoice_id;
9598 
9595             trans_lines(j).trx_level_type 			:= 'LINE';
9596             trans_lines(j).trx_line_id 				:= p_inv_line_list(i).line_number;
9597             trans_lines(j).line_level_action 			:= l_line_level_action;
9599             trans_lines(j).trx_receipt_date 			:= l_trx_receipt_date;
9600             trans_lines(j).trx_line_type 			:= 'ITEM';
9601 --bug6255826
9602             trans_lines(j).trx_line_date 			:= P_Invoice_Header_Rec.invoice_date;
9603             trans_lines(j).trx_business_category 		:= p_inv_line_list(i).trx_business_category;
9604             trans_lines(j).line_intended_use 			:= p_inv_line_list(i).primary_intended_use;
9605             trans_lines(j).user_defined_fisc_class 		:= p_inv_line_list(i).user_defined_fisc_class;
9606             trans_lines(j).line_amt 				:= p_inv_line_list(i).amount;
9607             trans_lines(j).trx_line_quantity 			:= p_inv_line_list(i).quantity_invoiced;
9608             trans_lines(j).unit_price 				:= p_inv_line_list(i).unit_price;
9609 
9610             trans_lines(j).product_id 				:= p_inv_line_list(i).inventory_item_id;
9611             trans_lines(j).product_fisc_classification 		:= p_inv_line_list(i).product_fisc_classification;
9612             trans_lines(j).product_org_id 			:= l_product_org_id;
9613             trans_lines(j).uom_code 				:= l_uom_code;
9614             trans_lines(j).product_type 			:= p_inv_line_list(i).product_type;
9615             trans_lines(j).product_category 			:= p_inv_line_list(i).product_category;
9616             trans_lines(j).fob_point 				:= l_fob_point;
9617 
9618             -- 7262269 /*Bug 13079028: Removed the IF-ELSE and called the function directly to handle all cases*/
9619             --IF p_inv_line_list(i).po_line_location_id IS NOT NULL THEN
9620 
9621             --Bug14039130
9622 
9623 	    --bug14189635, changed IF condition to check variable p_inv_line_list instead of l_inv_line_list
9624             IF p_inv_line_list(i).line_type_lookup_code NOT IN ('MISCELLANEOUS','FREIGHT') THEN
9625                l_po_line_location_id := p_inv_line_list(i).po_line_location_id;
9626             ELSE
9627                l_po_line_location_id := NULL;
9628             END IF;
9629 
9630 
9631             l_ship_to_party_id := get_po_ship_to_org_id (l_po_line_location_id,
9632 	                                                     p_inv_line_list(i).inventory_item_id, /*Bug 13079028*/
9633 							                             p_inv_line_list(i).org_id, /*Bug 13079028*/
9634 							                             p_inv_line_list(i).application_id, /*Bug 13079028*/
9635 							                             p_inv_line_list(i).reference_key5); /*Bug 13079028*/
9636            /*ELSE
9637              l_ship_to_party_id := p_inv_line_list(i).org_id;
9638             END IF;*/ /*Bug 13079028*/
9639 
9640             --Bug14039130
9641 
9642             trans_lines(j).ship_to_party_id		:= l_ship_to_party_id;  /* Changed the subscript from i to j for bug#7319191 */
9643             -- 7262269
9644             trans_lines(j).ship_from_party_id 			:= P_Invoice_Header_Rec.party_id;
9645             trans_lines(j).bill_to_party_id 			:= p_inv_line_list(i).org_id;
9646             trans_lines(j).bill_from_party_id 			:= P_Invoice_Header_Rec.party_id;
9647             trans_lines(j).ship_from_party_site_id 		:= P_Invoice_Header_Rec.party_site_id;
9648             trans_lines(j).bill_from_party_site_id 		:= P_Invoice_Header_Rec.party_site_id;
9649 
9650             trans_lines(j).ship_to_location_id 			:= p_inv_line_list(i).ship_to_location_id;
9651 	        trans_lines(j).ship_from_location_id 		:= l_location_id;
9652             trans_lines(j).bill_to_location_id 			:= l_bill_to_location_id;
9653             trans_lines(j).bill_from_location_id 		:= l_location_id;
9654 
9655             trans_lines(j).account_ccid 			:= p_inv_line_list(i).default_dist_ccid;
9656 
9657             trans_lines(j).ref_doc_application_id 		:= l_ref_doc_application_id;
9658             trans_lines(j).ref_doc_entity_code 			:= l_ref_doc_entity_code;
9659             trans_lines(j).ref_doc_event_class_code 		:= l_ref_doc_event_class_code;
9660             trans_lines(j).ref_doc_trx_id 			:= l_ref_doc_trx_id;
9661 
9662             --Bug14039130
9663 	    --bug14189635, changed IF condition to check variable p_inv_line_list instead of l_inv_line_list
9664             IF p_inv_line_list(i).line_type_lookup_code NOT IN ('MISCELLANEOUS','FREIGHT') THEN
9665                trans_lines(j).ref_doc_line_id 			:= p_inv_line_list(i).po_line_location_id;
9666             ELSE
9667                trans_lines(j).ref_doc_line_id 			:= NULL;
9668             END IF;
9669             --Bug14039130
9670 
9671             trans_lines(j).ref_doc_line_quantity 		:= l_ref_doc_line_quantity;
9672 	        trans_lines(j).ref_doc_trx_level_type               := l_ref_doc_trx_level_type; -- bug 8578833
9673 
9674             trans_lines(j).adjusted_doc_application_id 		:= l_adj_doc_application_id;
9675             trans_lines(j).adjusted_doc_entity_code 		:= l_adj_doc_entity_code;
9676             trans_lines(j).adjusted_doc_event_class_code 	:= l_adj_doc_event_class_code;
9677             trans_lines(j).adjusted_doc_trx_id 			:= p_inv_line_list(i).corrected_inv_id;
9678             trans_lines(j).adjusted_doc_line_id 		:= p_inv_line_list(i).price_correct_inv_line_num;
9679             trans_lines(j).adjusted_doc_number 			:= l_adj_doc_number;
9680             trans_lines(j).adjusted_doc_date 			:= l_adj_doc_date;
9681             trans_lines(j).adjusted_doc_trx_level_type 	        := l_adj_doc_trx_level_type; --Bug8332737
9682 
9683             trans_lines(j).applied_to_application_id 		:= l_applied_to_application_id;
9684             trans_lines(j).applied_to_entity_code 		:= l_applied_to_entity_code;
9685             trans_lines(j).applied_to_event_class_code 		:= l_applied_to_event_class_code;
9686 
9687 
9688             --Bug14039130
9689 	    --bug14189635, changed IF condition to check variable p_inv_line_list instead of l_inv_line_list
9690             IF p_inv_line_list(i).line_type_lookup_code NOT IN ('MISCELLANEOUS','FREIGHT') THEN
9694             END IF;
9691                trans_lines(j).applied_to_trx_id		 := p_inv_line_list(i).rcv_transaction_id;
9692             ELSE
9693                trans_lines(j).applied_to_trx_id		 := NULL;
9695 
9696 	    --bug14189635, changed IF condition to check variable p_inv_line_list instead of l_inv_line_list
9697             IF p_inv_line_list(i).rcv_transaction_id IS NOT NULL AND
9698                p_inv_line_list(i).line_type_lookup_code NOT IN ('MISCELLANEOUS','FREIGHT') THEN
9699 
9700                trans_lines(j).applied_to_trx_line_id	 := p_inv_line_list(i).po_line_location_id;
9701             ELSE
9702                trans_lines(j).applied_to_trx_line_id	 := NULL;
9703             END IF;
9704             --Bug14039130
9705 
9706             trans_lines(j).trx_line_number 			:= p_inv_line_list(i).line_number;
9707             trans_lines(j).trx_line_description 		:= p_inv_line_list(i).description;
9708             trans_lines(j).product_description 			:= p_inv_line_list(i).item_description;
9709             trans_lines(j).trx_line_gl_date 			:= p_inv_line_list(i).accounting_date;
9710 
9711             trans_lines(j).assessable_value 			:= p_inv_line_list(i).assessable_value;
9712 
9713             trans_lines(j).ctrl_hdr_tx_appl_flag 		:= l_ctrl_hdr_tx_appl_flag;
9714             -- The default for the control amount amount is included only in the
9715             -- case the tax line is tax only and it is RCV matched.
9716             trans_lines(j).ctrl_total_line_tx_amt		:= l_line_control_amount;
9717 
9718             trans_lines(j).source_application_id   		:= p_inv_line_list(i).source_application_id;
9719             trans_lines(j).source_entity_code	   		:= p_inv_line_list(i).source_entity_code;
9720             trans_lines(j).source_event_class_code 		:= p_inv_line_list(i).source_event_class_code;
9721             trans_lines(j).source_trx_id	   		:= p_inv_line_list(i).source_trx_id;
9722             trans_lines(j).source_line_id	   		:= p_inv_line_list(i).source_line_id;
9723             trans_lines(j).source_trx_level_type   		:= p_inv_line_list(i).source_trx_level_type;
9724 
9725             trans_lines(j).input_tax_classification_code 	:= p_inv_line_list(i).tax_classification_code;
9726 --debug log messages added for 6321366
9727             IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9728               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_transaction_lines_gt values ');
9729               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: ' || trans_lines(j).event_class_code);
9730               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '           || trans_lines(j).trx_id);
9731               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_id: '      || trans_lines(j).trx_line_id);
9732               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_level_type: '   || trans_lines(j).trx_level_type);
9733               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_type: '    || trans_lines(j).trx_line_type );
9734               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_level_action: '|| trans_lines(j).line_level_action);
9735               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 );
9736               END IF;
9737 
9738             -- Increase the index for the next line to be included in the pl/sql
9739             -- table
9740             j := j + 1;
9741 
9742           END IF; -- l_return_status
9743 
9744         IF (NVL(P_Invoice_Header_Rec.tax_only_rcv_matched_flag, 'N') = 'N') THEN
9745          -------------------------------------------------------------------
9746           l_debug_info := 'Step 12: Populate pl/sql table if TAX line';
9747           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9748              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9749           END IF;
9750           -------------------------------------------------------------------
9751           IF (l_return_status = TRUE ) THEN
9752             tax_lines.EXTEND(1);
9753             link_lines.EXTEND(1);
9754 
9755 	        tax_lines(k).tax_line_allocation_flag             := 'Y';  /* for bug 6010950 as for
9756             tax only line we need to pass it as 'Y'. */
9757             tax_lines(k).summary_tax_line_number := p_inv_line_list(i).line_number;
9758             tax_lines(k).internal_organization_id := P_Invoice_Header_Rec.org_id;
9759             tax_lines(k).application_id := 200;
9760             tax_lines(k).entity_code := 'AP_INVOICES';
9761             tax_lines(k).event_class_code := p_event_class_code;
9762             tax_lines(k).trx_id := P_Invoice_Header_Rec.invoice_id;
9763 
9764             -- Not used by AP
9765             -- tax_lines(k).hrd_trx_user_key1..6
9766 
9767             tax_lines(k).tax_regime_code := p_inv_line_list(i).tax_regime_code;
9768             tax_lines(k).tax := p_inv_line_list(i).tax;
9769             tax_lines(k).tax_status_code := p_inv_line_list(i).tax_status_code;
9770             tax_lines(k).tax_rate_code := nvl(p_inv_line_list(i).tax_rate_code,
9771                       p_inv_line_list(i).tax_classification_code); --bug6255826
9772 
9773             --Bug10082937
9774             l_copy_line_dff_flag := NVL(fnd_profile.value('AP_COPY_INV_LINE_DFF'),'N');
9775             IF l_copy_line_dff_flag = 'Y' THEN
9776                tax_lines(k).ATTRIBUTE1         :=  p_inv_line_list(i).ATTRIBUTE1;
9777                tax_lines(k).ATTRIBUTE2         :=  p_inv_line_list(i).ATTRIBUTE2;
9778                tax_lines(k).ATTRIBUTE3         :=  p_inv_line_list(i).ATTRIBUTE3;
9779                tax_lines(k).ATTRIBUTE4         :=  p_inv_line_list(i).ATTRIBUTE4;
9780                tax_lines(k).ATTRIBUTE5         :=  p_inv_line_list(i).ATTRIBUTE5;
9781                tax_lines(k).ATTRIBUTE6         :=  p_inv_line_list(i).ATTRIBUTE6;
9782                tax_lines(k).ATTRIBUTE7         :=  p_inv_line_list(i).ATTRIBUTE7;
9783                tax_lines(k).ATTRIBUTE8         :=  p_inv_line_list(i).ATTRIBUTE8;
9787                tax_lines(k).ATTRIBUTE12        :=  p_inv_line_list(i).ATTRIBUTE12;
9784                tax_lines(k).ATTRIBUTE9         :=  p_inv_line_list(i).ATTRIBUTE9;
9785                tax_lines(k).ATTRIBUTE10        :=  p_inv_line_list(i).ATTRIBUTE10;
9786                tax_lines(k).ATTRIBUTE11        :=  p_inv_line_list(i).ATTRIBUTE11;
9788                tax_lines(k).ATTRIBUTE13        :=  p_inv_line_list(i).ATTRIBUTE13;
9789                tax_lines(k).ATTRIBUTE14        :=  p_inv_line_list(i).ATTRIBUTE14;
9790                tax_lines(k).ATTRIBUTE15        :=  p_inv_line_list(i).ATTRIBUTE15;   --BUG13719477
9791                tax_lines(k).ATTRIBUTE_CATEGORY := p_inv_line_list(i).ATTRIBUTE_CATEGORY;
9792             END IF;
9793             --Bug10082937
9794 --            tax_lines(k).tax_rate_code := p_inv_line_list(i).tax_rate_code;
9795 --            commented for 6255826
9796             tax_lines(k).tax_rate := p_inv_line_list(i).tax_rate;
9797             --- bug 6429993 - we need to populate tax_jurisdiction_code also.
9798             tax_lines(k).tax_jurisdiction_code :=p_inv_line_list(i).tax_jurisdiction_code;
9799             tax_lines(k).tax_amt :=  p_inv_line_list(i).amount;
9800 
9801 
9802 
9803             --------------------------------------------------------------
9804             l_debug_info := 'Step 13: Populate link structure';
9805             --------------------------------------------------------------
9806             link_lines(k).application_id := 200;
9807             link_lines(k).entity_code := 'AP_INVOICES';
9808             link_lines(k).event_class_code := p_event_class_code;
9809             link_lines(k).trx_id := P_Invoice_Header_Rec.invoice_id;
9810             link_lines(k).trx_level_type := 'LINE';
9811             link_lines(k).trx_line_id := p_inv_line_list(i).line_number;
9812             link_lines(k).summary_tax_line_number := p_inv_line_list(i).line_number;
9813             link_lines(k).line_amt := p_inv_line_list(i).amount;
9814 
9815 -- Debug messages added for 6321366
9816  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9817               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_import_tax_lines_gt values ');
9818               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'summary_tax_line_number: ' || tax_lines(k).summary_tax_line_number);
9819               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'application_id: '           || tax_lines(k).application_id);
9820               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: '      || tax_lines(k).event_class_code);
9821               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '   || tax_lines(k).trx_id);
9822               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax: '   || tax_lines(k).tax );
9823               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax_amt: '    || tax_lines(k).tax_amt );
9824               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax_line_allocation_flag: '|| tax_lines(k).tax_line_allocation_flag);
9825  END IF;
9826 -- Debug messages added for 6321366
9827 IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9828 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_trx_tax_link_gt values ');
9829               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'application_id: '           || link_lines(k).application_id);
9830               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'entity_code: ' || link_lines(k).entity_code);
9831               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: ' || link_lines(k).event_class_code);
9832               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '           || link_lines(k).trx_id);
9833               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_level_type: '           || link_lines(k).trx_level_type);
9834               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_id: '           || link_lines(k).trx_line_id);
9835               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_amt: '    || link_lines(k).line_amt );
9836               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'summary_tax_line_number: ' || link_lines(k).summary_tax_line_number);
9837 END IF;
9838             k := k + 1;
9839 
9840           END IF;  -- l_return_status validation for TAX lines
9841         END IF;
9842       END IF;
9843     END LOOP;   -- end of loop in the p_invoice_lines_tab
9844 
9845     ELSE
9846       -- There are no TRX or TAX lines to validate.  Return without error
9847       RETURN TRUE;
9848 
9849     END IF;  -- There are TRX or TAX lines in the invoice to be imported
9850 
9851     -------------------------------------------------------------------
9852     l_debug_info := 'Step 12: Bulk Insert into global temp tables';
9853     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9854        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9855     END IF;
9856     -------------------------------------------------------------------
9857     IF ( l_return_status = TRUE ) THEN
9858 
9859       DELETE FROM zx_transaction_lines_gt
9860        WHERE application_id   = AP_ETAX_PKG.AP_APPLICATION_ID
9861          AND entity_code      = AP_ETAX_PKG.AP_ENTITY_CODE
9862          AND event_class_code IN (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
9863                                   AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
9864                                   AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
9865          AND trx_id = p_invoice_header_rec.invoice_id;
9866 
9867       IF (trans_lines.COUNT > 0) THEN
9868         FORALL m IN trans_lines.FIRST..trans_lines.LAST
9869           INSERT INTO zx_transaction_lines_gt
9870           VALUES trans_lines(m);
9871       END IF;
9872 
9873       IF (tax_lines.COUNT > 0) THEN
9874         FORALL m IN tax_lines.FIRST..tax_lines.LAST
9875           INSERT INTO zx_import_tax_lines_gt
9876           VALUES tax_lines(m);
9877 
9878 
9879         --Bug10082937
9883            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9880         -------------------------------------------------------------------
9881         l_debug_info := 'DFFs Of The Tax Lines';
9882         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9884         END IF;
9885        -------------------------------------------------------------------
9886         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
9887            IF l_copy_line_dff_flag = 'Y'  THEN
9888               FOR i IN (SELECT *
9889                           FROM zx_import_tax_lines_gt
9890                          WHERE trx_id = P_Invoice_Header_Rec.invoice_id
9891                            AND application_id = 200
9892                            AND entity_code ='AP_INVOICES'
9893                            AND event_class_code IN ('STANDARD INVOICES','PREPAYMENT INVOICES','EXPENSE REPORTS')) LOOP
9894 
9895                         l_debug_info := '1 '|| i.ATTRIBUTE1;
9896                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9897                         l_debug_info := '2 '||i.ATTRIBUTE2;
9898                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9899                         l_debug_info := '3 '||i.ATTRIBUTE3;
9900                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9901                         l_debug_info := '4 '||i.ATTRIBUTE4;
9902                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9903                         l_debug_info := '5 '||i.ATTRIBUTE5;
9904                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9905                         l_debug_info := '6 '||i.ATTRIBUTE6;
9906                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9907                         l_debug_info := '7 '||i.ATTRIBUTE7;
9908                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9909                         l_debug_info := '8 '||i.ATTRIBUTE8;
9910                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9911                         l_debug_info := '9 '||i.ATTRIBUTE8;
9912                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9913                         l_debug_info := '10 '||i.ATTRIBUTE10;
9914                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9915                         l_debug_info := '11 '||i.ATTRIBUTE11;
9916                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9917                         l_debug_info := '12 '||i.ATTRIBUTE12;
9918                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9919                         l_debug_info := '13 '||i.ATTRIBUTE13;
9920                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9921                         l_debug_info := '14 '||i.ATTRIBUTE14;
9922                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9923                         l_debug_info := '15 '||i.ATTRIBUTE15;
9924                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9925                         l_debug_info := 'Attrib Cat '||i.ATTRIBUTE_CATEGORY;
9926                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
9927               END LOOP;
9928            END IF;
9929         END IF;
9930       --Bug10082937
9931 
9932 
9933 
9934       END IF;
9935 
9936       -- This bulk insert will only be effective when the invoice is tax_only
9937       -- and there are tax lines with the tax info to be imported
9938       IF (link_lines.COUNT > 0) THEN
9939         FORALL m IN link_lines.FIRST..link_lines.LAST
9940           INSERT INTO zx_trx_tax_link_gt
9941           VALUES link_lines(m);
9942       END IF;
9943 
9944     END IF;
9945 
9946     RETURN l_return_status;
9947 
9948   EXCEPTION
9949     WHEN OTHERS THEN
9950       IF (SQLCODE <> -20001) THEN
9951         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
9952         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
9953         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
9954         FND_MESSAGE.SET_TOKEN('PARAMETERS',
9955           ' P_Invoice_Header_Rec = '||P_Invoice_Header_Rec.Invoice_Id||
9956           ' P_Calling_Mode ='||P_Calling_Mode||
9957           ' P_Error_Code = '||P_Error_Code||
9958           ' P_Calling_Sequence = '||P_Calling_Sequence);
9959         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
9960       END IF;
9961 
9962       APP_EXCEPTION.RAISE_EXCEPTION;
9963 
9964   END Populate_Lines_Import_GT;
9965 
9966 /*=============================================================================
9967  |  FUNCTION - Populate_Tax_Lines_GT()
9968  |
9969  |  DESCRIPTION
9970  |      This function will get additional information required to populate the
9971  |      ZX_TRANSACTION_LINES_GT, and  ZX_IMPORT_TAX_LINES_GT.
9972  |      There is no need to populate ZX_TRX_TAX_LINK_GT since any tax line
9973  |      manually created is assume to be allocated to all the ITEM lines in the
9974  |      invoice.
9975  |      This function returns TRUE if the population of the temp table goes
9976  |      through successfully.  Otherwise, FALSE.
9977  |
9978  |  PARAMETERS
9979  |      P_Invoice_Header_Rec - record with invoice header information
9980  |      P_Calling_Mode - calling mode. it is used to
9981  |      P_Event_Class_Code - Event class code for document
9982  |      P_Tax_only_Flag - determine if the invoice is tax only
9983  |      P_Inv_Rcv_Matched - determine if the invoice has any line matched to a
9984  |                          receipt
9985  |      P_error_code - Error code to be returned
9986  |      P_calling_sequence -  Calling sequence
9987  |
9988  |  MODIFICATION HISTORY
9992  *============================================================================*/
9989  |    DATE          Author         Action
9990  |    06-FEB-2004   SYIDNER        Created
9991  |
9993   FUNCTION Populate_Tax_Lines_GT(
9994              P_Invoice_Header_Rec      IN ap_invoices_all%ROWTYPE,
9995              P_Calling_Mode            IN VARCHAR2,
9996              P_Event_Class_Code        IN VARCHAR2,
9997              P_Tax_only_Flag           IN VARCHAR2,
9998              P_Inv_Rcv_Matched         IN OUT NOCOPY VARCHAR2,
9999              P_Error_Code              OUT NOCOPY VARCHAR2,
10000              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
10001   IS
10002 
10003     l_debug_info                 VARCHAR2(240);
10004     l_curr_calling_sequence      VARCHAR2(4000);
10005 
10006     TYPE Trx_Lines_Tab_Type IS TABLE OF zx_transaction_lines_gt%ROWTYPE;
10007     TYPE Tax_Lines_Tab_Type IS TABLE OF zx_import_tax_lines_gt%ROWTYPE;
10008     TYPE Link_Lines_Tab_Type IS TABLE OF zx_trx_tax_link_gt%ROWTYPE;
10009 
10010     trans_lines		Trx_Lines_Tab_Type := Trx_Lines_Tab_Type();
10011     tax_lines		Tax_Lines_Tab_Type := Tax_Lines_Tab_Type();
10012     link_lines		Link_Lines_Tab_Type := Link_Lines_Tab_Type();
10013 
10014     l_ctrl_hdr_tx_appl_flag		zx_transaction_lines_gt.ctrl_hdr_tx_appl_flag%TYPE;
10015     l_line_control_amount		zx_transaction_lines_gt.ctrl_total_line_tx_amt%TYPE;
10016     l_line_level_action			zx_transaction_lines_gt.line_level_action%TYPE;
10017     l_line_amt_includes_tax_flag	zx_transaction_lines_gt.line_amt_includes_tax_flag%TYPE;
10018     l_product_org_id			zx_transaction_lines_gt.product_org_id%TYPE;
10019 
10020     -- Purchase Order
10021     l_ref_doc_application_id		zx_transaction_lines_gt.ref_doc_application_id%TYPE;
10022     l_ref_doc_entity_code		zx_transaction_lines_gt.ref_doc_entity_code%TYPE;
10023     l_ref_doc_event_class_code		zx_transaction_lines_gt.ref_doc_event_class_code%TYPE;
10024     l_ref_doc_trx_level_type		zx_transaction_lines_gt.ref_doc_trx_level_type%TYPE;
10025     l_ref_doc_line_quantity		zx_transaction_lines_gt.ref_doc_line_quantity%TYPE;
10026     l_po_header_curr_conv_rate		po_headers_all.rate%TYPE;
10027 
10028     -- Receipt Matched
10029     l_applied_to_application_id		zx_transaction_lines_gt.applied_to_application_id%TYPE;
10030     l_applied_to_entity_code		zx_transaction_lines_gt.applied_to_entity_code%TYPE;
10031     l_applied_to_event_class_code	zx_transaction_lines_gt.applied_to_event_class_code%TYPE;
10032     l_trx_receipt_date			zx_transaction_lines_gt.trx_receipt_date%TYPE;
10033     l_ref_doc_trx_id			zx_transaction_lines_gt.ref_doc_trx_id%TYPE;
10034     l_uom_code				mtl_units_of_measure.uom_code%TYPE;
10035     l_dummy				number;
10036 
10037     -- Corrections
10038     l_adj_doc_application_id		zx_transaction_lines_gt.adjusted_doc_application_id%TYPE;
10039     l_adj_doc_entity_code		zx_transaction_lines_gt.adjusted_doc_entity_code%TYPE;
10040     l_adj_doc_event_class_code		zx_transaction_lines_gt.adjusted_doc_event_class_code%TYPE;
10041     l_adj_doc_number			zx_transaction_lines_gt.adjusted_doc_number%TYPE;
10042     l_adj_doc_date			zx_transaction_lines_gt.adjusted_doc_date%TYPE;
10043 
10044     l_fob_point                  	po_vendor_sites_all.fob_lookup_code%TYPE;
10045     l_location_id                       zx_transaction_lines_gt.ship_from_location_id%type;
10046     l_bill_to_location_id               zx_transaction_lines_gt.bill_to_location_id%TYPE;
10047     l_po_line_location_id		ap_invoice_lines_interface.po_line_location_id%TYPE;
10048     l_ship_to_party_id          po_line_locations_all.ship_to_organization_id%type; -- 7262269
10049 
10050     k                           INT := 1;
10051     l_pseudo                    INT := 1;
10052 
10053     l_return_status	BOOLEAN := TRUE;
10054     l_api_name		CONSTANT VARCHAR2(100) := 'Populate_Tax_Lines_GT';
10055     l_prorating_total   number;    ---for bug 6064593
10056     l_total_prorated    number;    ---for bug 6064593
10057     l_copy_line_dff_flag         VARCHAR2(1); -- Bug9819170
10058 
10059       l_err varchar2(2000); --bug10140354
10060   BEGIN
10061 
10062     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Populate_Tax_Lines_GT<-'||
10063                                P_calling_sequence;
10064 
10065     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10066        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_curr_calling_sequence);
10067     END IF;
10068 
10069 
10070     l_fob_point           := AP_ETAX_SERVICES_PKG.g_site_attributes
10071                                         (p_invoice_header_rec.vendor_site_id).fob_lookup_code;
10072     l_location_id         := AP_ETAX_SERVICES_PKG.g_site_attributes
10073                                         (p_invoice_header_rec.vendor_site_id).location_id;
10074     l_bill_to_location_id := AP_ETAX_SERVICES_PKG.g_org_attributes
10075                                         (p_invoice_header_rec.org_id).bill_to_location_id;
10076 
10077     IF (P_Tax_only_Flag = 'N') THEN
10078 
10079       -------------------------------------------------------------------
10080       l_debug_info := 'Step 1: Populate pl/sql table TAX line will be '||
10081                       'allocated to all the ITEM lines in the invoice';
10082       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10083           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10084       END IF;
10085       -------------------------------------------------------------------
10086       IF ( l_inv_tax_list.COUNT > 0) THEN
10087         tax_lines.EXTEND(l_inv_tax_list.COUNT);
10088 
10089         FOR i IN l_inv_tax_list.FIRST..l_inv_tax_list.LAST LOOP
10090 
10091             tax_lines(i).summary_tax_line_number	:= l_inv_tax_list(i).line_number;
10092             tax_lines(i).internal_organization_id	:= l_inv_tax_list(i).org_id;
10096             tax_lines(i).trx_id 			:= l_inv_tax_list(i).invoice_id;
10093             tax_lines(i).application_id 		:= 200;
10094             tax_lines(i).entity_code 			:= 'AP_INVOICES';
10095             tax_lines(i).event_class_code 		:= p_event_class_code;
10097 
10098             tax_lines(i).tax_regime_code		:= l_inv_tax_list(i).tax_regime_code;
10099             tax_lines(i).tax				:= l_inv_tax_list(i).tax;
10100             tax_lines(i).tax_status_code		:= l_inv_tax_list(i).tax_status_code;
10101             tax_lines(i).tax_rate_code			:= l_inv_tax_list(i).tax_rate_code;
10102             tax_lines(i).tax_rate			:= l_inv_tax_list(i).tax_rate;
10103             tax_lines(i).tax_amt			:= l_inv_tax_list(i).amount;
10104             tax_lines(i).tax_jurisdiction_code		:= l_inv_tax_list(i).tax_jurisdiction_code;
10105             tax_lines(i).tax_rate_id                    := l_inv_tax_list(i).tax_rate_id;
10106 
10107             tax_lines(i).tax_amt_included_flag		:= 'N';
10108 
10109             --Bug9819170
10110             l_copy_line_dff_flag := NVL(fnd_profile.value('AP_COPY_INV_LINE_DFF'),'N');
10111             IF l_copy_line_dff_flag = 'Y' THEN
10112                tax_lines(i).ATTRIBUTE1         :=  l_inv_tax_list(i).ATTRIBUTE1;
10113                tax_lines(i).ATTRIBUTE2         :=  l_inv_tax_list(i).ATTRIBUTE2;
10114                tax_lines(i).ATTRIBUTE3         :=  l_inv_tax_list(i).ATTRIBUTE3;
10115                tax_lines(i).ATTRIBUTE4         :=  l_inv_tax_list(i).ATTRIBUTE4;
10116                tax_lines(i).ATTRIBUTE5         :=  l_inv_tax_list(i).ATTRIBUTE5;
10117                tax_lines(i).ATTRIBUTE6         :=  l_inv_tax_list(i).ATTRIBUTE6;
10118                tax_lines(i).ATTRIBUTE7         :=  l_inv_tax_list(i).ATTRIBUTE7;
10119                tax_lines(i).ATTRIBUTE8         :=  l_inv_tax_list(i).ATTRIBUTE8;
10120                tax_lines(i).ATTRIBUTE9         :=  l_inv_tax_list(i).ATTRIBUTE9;
10121                tax_lines(i).ATTRIBUTE10        :=  l_inv_tax_list(i).ATTRIBUTE10;
10122                tax_lines(i).ATTRIBUTE11        :=  l_inv_tax_list(i).ATTRIBUTE11;
10123                tax_lines(i).ATTRIBUTE12        :=  l_inv_tax_list(i).ATTRIBUTE12;
10124                tax_lines(i).ATTRIBUTE13        :=  l_inv_tax_list(i).ATTRIBUTE13;
10125                tax_lines(i).ATTRIBUTE14        :=  l_inv_tax_list(i).ATTRIBUTE14;
10126                tax_lines(i).ATTRIBUTE15        :=  l_inv_tax_list(i).ATTRIBUTE15;       --BUG13719477
10127                tax_lines(i).ATTRIBUTE_CATEGORY := l_inv_tax_list(i).ATTRIBUTE_CATEGORY;
10128             END IF;
10129             --Bug9819170
10130 
10131             --Start For bug 6064593 - if there is allocation information provided
10132             --for the manual tax line then We need to populate zx_trx_tax_link_gt.
10133             --ie if line_group_number is provided on the tax line and
10134             --ITEM line then we need to populate the zx_trx_tax_link_gt
10135             --with the allocation info.And we need to pass tax_line_allocation_flag
10136             --as 'Y' in zx_import_tax_lines_gt for the tax line.
10137 
10138           IF  (l_inv_tax_list(i).prorate_across_all_items = 'Y'AND
10139                 l_inv_tax_list(i).line_group_number IS NOT NULL) THEN
10140                 tax_lines(i).tax_line_allocation_flag       := 'Y';
10141           ELSE
10142                 tax_lines(i).tax_line_allocation_flag       := 'N';
10143           END IF;
10144 -- Debug messages added for 6321366
10145           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10146               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_import_tax_lines_gt values ');
10147               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'summary_tax_line_number: ' || tax_lines(i).summary_tax_line_number);
10148               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'application_id: '           || tax_lines(i).application_id);
10149               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: '      || tax_lines(i).event_class_code);
10150               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '   || tax_lines(i).trx_id);
10151               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax: '   || tax_lines(i).tax );
10152               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax_amt: '    || tax_lines(i).tax_amt );
10153               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax_amt_included_flag: '|| tax_lines(i).tax_amt_included_flag);
10154               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax_line_allocation_flag: '|| tax_lines(i).tax_line_allocation_flag);
10155           END IF;
10156 
10157                -------------------------------------------------------------------
10158                l_debug_info := 'Step 13: Populate allocation structure if needed';
10159                IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10160                   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10161                END IF;
10162                -------------------------------------------------------------------
10163 
10164                   IF (l_inv_tax_list(i).prorate_across_all_items = 'Y' AND
10165                      l_inv_tax_list(i).line_group_number IS NOT NULL) THEN
10166 
10167                       --------------------------------------------------------------
10168                       l_debug_info := 'Step 17: Get prorated total';
10169                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10170                          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10171                       END IF;
10172                       --------------------------------------------------------------
10173 
10174 					  SELECT SUM(NVL(amount, 0))
10175                         INTO l_prorating_total
10176                         FROM ap_invoice_lines
10177                        WHERE invoice_id = l_inv_tax_list(i).invoice_id
10178                          AND line_number <> l_inv_tax_list(i).line_number
10179 						 AND line_type_lookup_code <> 'TAX'   --Bug6608702
10180                          AND line_group_number = l_inv_tax_list(i).line_group_number;
10181 
10182 					  /************************************************************
10183 					  Bug6608702- Ebtax expects the line amount in
10184 					  zx_trx_tax_link_gt for each TAX line to be the sum of line
10185 					  amounts of all ITEM lines under same line_group_number.
10186 					  Here All the things worked fine till there was only one tax
10187 					  line linked to ITEM line. When a ITEM line is linked to
10188 					  multiple TAX lines through line_group_number then existing
10189                       logic did sum of all the lines for the same line_group_no
10190                       except current TAX line. So other TAX lines within the
10191                       same line_group_number are also considered. To avoid this
10192                       we added another WHERE clause line_type_lookup_code <> 'TAX'
10193                       This would avoid TAX line amount to be considered in the
10194                       query.
10195    		              ************************************************************/
10196 
10197 					  --------------------------------------------------------------
10198                       l_debug_info := 'Step 18: Get Insert in global temp table';
10199                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10200                          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10201                       END IF;
10202                       --------------------------------------------------------------
10203 
10204                       IF (l_prorating_total <> 0) THEN
10205                         INSERT INTO zx_trx_tax_link_gt (
10206                           application_id,
10207                           entity_code,
10208                           event_class_code,
10209                           trx_id,
10210                           trx_level_type,
10211                           trx_line_id,
10212                           summary_tax_line_number,
10213                           line_amt
10214                         ) SELECT
10215                             200,                                           -- application_id
10216                             'AP_INVOICES',                                 -- entity_code
10217                             p_event_class_code,                            -- event_class_code
10218                             l_inv_tax_list(i).invoice_id,               -- trx_id
10219                             'LINE',                                        -- trx_level_type
10220                             ail.line_number,                              -- trx_line_id
10221                             l_inv_tax_list(i).line_number,                -- summary_tax_line_number
10222                             AP_UTILITIES_PKG.ap_round_currency(
10223                               l_inv_tax_list(i).amount*ail.amount/l_prorating_total,
10224                               l_inv_header_rec2.invoice_currency_code)  -- line_amt
10225                            FROM ap_invoice_lines ail
10226                           WHERE ail.invoice_id = l_inv_tax_list(i).invoice_id
10227                             AND ail.line_number <> l_inv_tax_list(i).line_number
10228 							AND ail.line_type_lookup_code <> 'TAX'   --Bug6608702
10229                             AND ail.line_group_number = l_inv_tax_list(i).line_group_number;
10230 -- Debug messages added for 6321366
10231                IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10232                   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Rows instrted in zx_trx_tax_link_gt: '|| sql%rowcount);
10233                END IF;
10234                       --------------------------------------------------------------
10235                       l_debug_info := 'Step 19: Verify if there is any rounding and '||
10236                                       'apply it to max of largest.';
10237                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10238                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10239                       END IF;
10240                       --------------------------------------------------------------
10241                       BEGIN
10242                         SELECT SUM(NVL(line_amt,0))
10243                           INTO l_total_prorated
10244                           FROM zx_trx_tax_link_gt
10245                          WHERE trx_id = l_inv_tax_list(i).invoice_id
10246                            AND summary_tax_line_number = l_inv_tax_list(i).line_number;
10247 
10248                         IF (NVL(l_inv_tax_list(i).amount, 0) <> l_total_prorated) THEN
10249                           UPDATE zx_trx_tax_link_gt
10250                              SET line_amt = line_amt + (l_inv_tax_list(i).amount - l_total_prorated)
10251                            WHERE trx_id = l_inv_tax_list(i).invoice_id
10252                              AND trx_line_id <> l_inv_tax_list(i).line_number
10253                              AND trx_line_id =
10254                                 (SELECT (MAX(ail.line_number))
10255                                    FROM ap_invoice_lines ail
10256                                   WHERE ail.invoice_id = l_inv_tax_list(i).invoice_id
10257                                     AND ail.line_number <> l_inv_tax_list(i).line_number
10258 									AND ail.line_type_lookup_code <> 'TAX'   --Bug6608702
10259                                     AND ail.amount <> 0
10260                                     AND ail.line_group_number = l_inv_tax_list(i).line_group_number
10261                                     AND ABS(ail.amount) >=
10262                                       ( SELECT  MAX(ABS(ail2.amount))
10263                                           FROM  ap_invoice_lines ail2
10264                                          WHERE  ail2.invoice_id = ail.invoice_id
10265                                            AND  ail2.line_number <> l_inv_tax_list(i).line_number
10266                                            AND  ail2.line_number <> ail.line_number
10267 										   AND  ail2.line_type_lookup_code <> 'TAX'   --Bug6608702
10268                                            AND  ail2.line_group_number =
10269                                                   l_inv_tax_list(i).line_group_number));
10270                       END IF;
10271                       EXCEPTION
10272                         WHEN OTHERS THEN
10273                           l_return_status := FALSE;
10274                       END;
10275 
10276                      END IF;  -- l_prorating_total <> 0
10277 -- bug 8680775: add start
10278                 -- Added functionality to import tax only lines
10279                 -- case only when prorate across flag is set to 'N'
10280                 ELSIF (l_inv_tax_list(i).prorate_across_all_items = 'N' AND l_inv_tax_list(i).line_source = 'IMPORTED') THEN
10281 
10282                 --Added line_source condition for Bug9074940
10283 
10284                       -- allocation flag is marked as N (above) when prorate accross flag is not Y
10285                       tax_lines(i).tax_line_allocation_flag           := 'Y';
10286 
10287                       trans_lines.EXTEND(1);
10288                       link_lines.EXTEND(1);
10289 
10290                       -- add pseudo line for manual non prorated tax line
10291                       trans_lines(l_pseudo).application_id := 200;
10292                       trans_lines(l_pseudo).entity_code := 'AP_INVOICES';
10293                       trans_lines(l_pseudo).event_class_code := p_event_class_code;
10294                       trans_lines(l_pseudo).trx_id := P_Invoice_Header_Rec.invoice_id;
10295                       trans_lines(l_pseudo).trx_level_type := 'LINE';
10296                       trans_lines(l_pseudo).trx_line_id := l_inv_tax_list(i).line_number;
10297                       trans_lines(l_pseudo).line_level_action := 'LINE_INFO_TAX_ONLY';
10298 
10299                       trans_lines(l_pseudo).trx_line_type := l_inv_tax_list(i).line_type_lookup_code;
10300                       trans_lines(l_pseudo).trx_line_date := P_Invoice_Header_Rec.invoice_date;
10301                       trans_lines(l_pseudo).trx_business_category := l_inv_tax_list(i).trx_business_category;
10302                       trans_lines(l_pseudo).line_intended_use := l_inv_tax_list(i).primary_intended_use;
10303                       trans_lines(l_pseudo).user_defined_fisc_class := l_inv_tax_list(i).user_defined_fisc_class;
10304                       trans_lines(l_pseudo).line_amt := l_inv_tax_list(i).amount;
10305                       trans_lines(l_pseudo).trx_line_quantity := l_inv_tax_list(i).quantity_invoiced;
10306                       trans_lines(l_pseudo).unit_price := l_inv_tax_list(i).unit_price;
10307 
10308                       trans_lines(l_pseudo).product_id := l_inv_tax_list(i).inventory_item_id;
10309                       trans_lines(l_pseudo).product_fisc_classification := l_inv_tax_list(i).product_fisc_classification;
10310                       trans_lines(l_pseudo).product_type := l_inv_tax_list(i).product_type;
10311                       trans_lines(l_pseudo).product_category := l_inv_tax_list(i).product_category;
10312                       trans_lines(l_pseudo).fob_point := l_fob_point;
10313                       trans_lines(l_pseudo).ship_to_party_id:= l_inv_tax_list(i).org_id;
10314 
10315                       trans_lines(l_pseudo).ship_from_party_id := P_Invoice_Header_Rec.party_id;
10316 
10317                       trans_lines(l_pseudo).bill_to_party_id:= l_inv_tax_list(i).org_id;
10318                       trans_lines(l_pseudo).bill_from_party_id:= P_Invoice_Header_Rec.party_id;
10319                       trans_lines(l_pseudo).ship_from_party_site_id:= P_Invoice_Header_Rec.party_site_id;
10320                       trans_lines(l_pseudo).bill_from_party_site_id:= P_Invoice_Header_Rec.party_site_id;
10321 
10322                       trans_lines(l_pseudo).ship_to_location_id:= l_inv_tax_list(i).ship_to_location_id;
10323                       trans_lines(l_pseudo).ship_from_location_id:= l_location_id;
10324                       trans_lines(l_pseudo).bill_to_location_id:= l_bill_to_location_id;
10325                       trans_lines(l_pseudo).bill_from_location_id:= l_location_id;
10326 
10327                       trans_lines(l_pseudo).account_ccid:= l_inv_tax_list(i).default_dist_ccid;
10328                       trans_lines(l_pseudo).merchant_party_country:= l_inv_tax_list(i).country_of_supply;
10329 
10330                       trans_lines(l_pseudo).trx_line_number:= l_inv_tax_list(i).line_number;
10331                       trans_lines(l_pseudo).trx_line_description:= l_inv_tax_list(i).description;
10332                       trans_lines(l_pseudo).product_description:= l_inv_tax_list(i).item_description;
10333                       trans_lines(l_pseudo).trx_line_gl_date:= l_inv_tax_list(i).accounting_date;
10334 
10335                       trans_lines(l_pseudo).merchant_party_name:= l_inv_tax_list(i).merchant_name;
10336                       trans_lines(l_pseudo).merchant_party_document_number:= l_inv_tax_list(i).merchant_document_number;
10337                       trans_lines(l_pseudo).merchant_party_reference:= l_inv_tax_list(i).merchant_reference;
10338                       trans_lines(l_pseudo).merchant_party_taxpayer_id:= l_inv_tax_list(i).merchant_taxpayer_id;
10339                       trans_lines(l_pseudo).merchant_party_tax_reg_number:= l_inv_tax_list(i).merchant_tax_reg_number;
10340 
10341                       trans_lines(l_pseudo).assessable_value:= l_inv_tax_list(i).assessable_value;
10342 
10343                       IF (l_Inv_tax_List(i).po_line_location_id IS NOT NULL) THEN
10344                           -- NONE
10345                           l_line_amt_includes_tax_flag := 'N';
10346                       ELSE
10347                           IF (p_calling_mode = 'CALCULATE QUOTE')
10348                           OR
10349                          (p_invoice_header_rec.invoice_type_lookup_code = 'EXPENSE REPORT'
10350                           and nvl(l_inv_tax_list(i).line_type_lookup_code, 'N') <> 'PREPAY') THEN
10351                              -- ALL
10352                              l_line_amt_includes_tax_flag := 'A';
10353                           ELSE
10354                              -- STANDARD
10355                              l_line_amt_includes_tax_flag := 'S';
10356                           END IF;
10357                       END IF;
10358                       trans_lines(l_pseudo).line_amt_includes_tax_flag:= l_line_amt_includes_tax_flag;
10359                       trans_lines(l_pseudo).historical_flag:= 'N';
10360 
10361                       IF P_Invoice_Header_Rec.control_amount IS NOT NULL THEN
10362                          l_ctrl_hdr_tx_appl_flag := 'Y';
10363                       ELSE
10364                          l_ctrl_hdr_tx_appl_flag := 'N';
10365                       END IF;
10366                       trans_lines(l_pseudo).ctrl_hdr_tx_appl_flag:= l_ctrl_hdr_tx_appl_flag;
10367                       trans_lines(l_pseudo).ctrl_total_line_tx_amt:= l_inv_tax_list(i).control_amount;
10368 
10369                       trans_lines(l_pseudo).source_application_id:= l_inv_tax_list(i).source_application_id;
10370                       trans_lines(l_pseudo).source_entity_code   := l_inv_tax_list(i).source_entity_code;
10371                       trans_lines(l_pseudo).source_event_class_code := l_inv_tax_list(i).source_event_class_code;
10372                       trans_lines(l_pseudo).source_trx_id   := l_inv_tax_list(i).source_trx_id;
10373                       trans_lines(l_pseudo).source_line_id   := l_inv_tax_list(i).source_line_id;
10374                       trans_lines(l_pseudo).source_trx_level_type:= l_inv_tax_list(i).source_trx_level_type;
10375 
10376                       trans_lines(l_pseudo).input_tax_classification_code:= l_inv_tax_list(i).tax_classification_code;
10377 
10378                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10379                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_transaction_lines_gt values ');
10380                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: ' || trans_lines(l_pseudo).event_class_code);
10381                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '           || trans_lines(l_pseudo).trx_id);
10382                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_id: '      || trans_lines(l_pseudo).trx_line_id);
10383                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_level_type: '   || trans_lines(l_pseudo).trx_level_type);
10384                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_type: '    || trans_lines(l_pseudo).trx_line_type );
10385                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_level_action: '|| trans_lines(l_pseudo).line_level_action);
10386                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_amt: '         || trans_lines(l_pseudo).line_amt);
10387                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'unit_price: '       || trans_lines(l_pseudo).unit_price);
10388                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_amt_includes_tax_flag: ' || trans_lines(l_pseudo).line_amt_includes_tax_flag );
10389                       END IF;
10390 
10391                       -- add to link gt
10392                       link_lines(l_pseudo).application_id:= 200;
10393                       link_lines(l_pseudo).entity_code:= 'AP_INVOICES';
10394                       link_lines(l_pseudo).event_class_code:= p_event_class_code;
10395                       link_lines(l_pseudo).trx_id:= P_Invoice_Header_Rec.invoice_id;
10396                       link_lines(l_pseudo).trx_level_type:= 'LINE';
10397                       link_lines(l_pseudo).trx_line_id:= l_inv_tax_list(i).line_number;
10398                       link_lines(l_pseudo).summary_tax_line_number:= l_inv_tax_list(i).line_number;
10399                       link_lines(l_pseudo).line_amt:= l_inv_tax_list(i).amount;
10400 
10401                       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10402                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_trx_tax_link_gt values ');
10403                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'summary_tax_line_number: ' || link_lines(l_pseudo).summary_tax_line_number);
10404                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'application_id: '           || link_lines(l_pseudo).application_id);
10405                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: '      || link_lines(l_pseudo).event_class_code);
10406                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '   || link_lines(l_pseudo).trx_id);
10407                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_level_type: '   || link_lines(l_pseudo).trx_level_type );
10408                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_id: '   || link_lines(l_pseudo).trx_line_id);
10409                           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_amt: '    || link_lines(l_pseudo).line_amt );
10410                       END IF;
10411 
10412                       l_pseudo := l_pseudo + 1;
10413 -- bug 8680775: add end
10414                   END IF;  -- prorate_accross_flag = 'Y' and line_group_number is not null
10415 
10416             ---End for bug 6064593
10417 
10418         END LOOP;
10419       END IF; -- is tax pl/sql table populated?
10420 
10421     ELSE
10422       -- Invoice is tax-only.  We have 2 scenarios.
10423       -- TAX lines created for the  match other charges case (no tax
10424       -- information populated and it is expected to call calculate_Tax), and
10425       -- TAX lines manually created not allocated to any ITEM line in
10426       -- the invoice.
10427       -- It is restricted to have both types of tax-only lines in the
10428       -- same invoice, so we can assume that if the TAX line has populated
10429       -- the RCV_TRANSACTION_ID field it is a match other charges case.
10430       -- In this case we expect the tax columns to be null and the we require
10431       -- the control amount to be populated so that eTax will know how much the
10432       -- tax should be. In this case we will populate the
10433       -- zx_transaction_lines_gt GT table.
10434       -- For the second case, where the tax lines are manually created we will
10435       -- populate both zx_transaction_lines_gt and zx_import_tax_lines_gt.  It
10436       -- is also required to populate the zx_trx_tax_link_gt with a one to one
10437       -- allocation.
10438 
10439 
10440       -------------------------------------------------------------------
10441       l_debug_info := 'Step 4: Populate pl/sql table.  TRX only if match'||
10442                       'to receipt, and the three GT tables in the other case';
10443       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10444           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10445       END IF;
10446       -------------------------------------------------------------------
10447       IF ( l_inv_tax_list.COUNT > 0) THEN
10448 
10449         trans_lines.EXTEND(l_inv_tax_list.COUNT);
10450 
10451         FOR i IN l_inv_tax_list.FIRST..l_inv_tax_list.LAST LOOP
10452           -- since for the 2 cases we need to populate the trans_lines pl/sql
10453           -- table, we populate it and if the rcv_transaction_id is not null
10454           -- we set the flag P_Inv_Rcv_Matched to Y.  If it is not RCV matched, we
10455           -- will not modified the flag since N is the initial value and will
10456           -- populate the zx_import_tax_lines_gt and zx_trx_tax_link_gt GT tables
10457 
10458           -------------------------------------------------------------------
10459           l_debug_info := 'Step 5: Get line_level_action for TAX ONLY line '||
10460                           'number:'||l_inv_tax_list(i).line_number;
10461     	  IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10462               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10463           END IF;
10464           -------------------------------------------------------------------
10465           IF (l_inv_tax_list(i).rcv_transaction_id IS NOT NULL) THEN
10466             -- In this case  eTax will need to run tax
10467             -- applicability and calculation because it is the matched to other
10468             -- charges case.
10469             -- since the control amount at line level is required for eTax in
10470             -- this case we will set it right away
10471 
10472             l_line_level_action := 'CREATE_TAX_ONLY';
10473             P_Inv_Rcv_Matched := 'Y';
10474             l_line_control_amount :=
10475               NVL(l_inv_tax_list(i).control_amount, l_inv_tax_list(i).amount);
10476 
10477 
10478           ELSE
10479             -- Invoice is tax-only, and there is no need to run applicability.
10480             -- In this case the user provides all the tax information for the line
10481             -- to be imported.  The additional taxable related info
10482             -- that eTax need to store will be passed to eTax using a pseudo line
10483             -- in the zx_transaction_lines_gt table.
10484 
10485             l_line_level_action := 'LINE_INFO_TAX_ONLY';
10486             l_line_control_amount := l_inv_tax_list(i).control_amount;
10487 
10488           END IF;
10489 
10490           -------------------------------------------------------------------
10491           l_debug_info := 'Step 6: Get Additional PO matched info if any ';
10492 	      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10493               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10494           END IF;
10495           -------------------------------------------------------------------
10496           IF ( l_Inv_tax_List(i).po_line_location_id IS NOT NULL) THEN
10497               -- this assigned is required since the p_po_line_location_id
10498               -- parameter is IN/OUT.  However, in this case it will not be
10499               -- modified because the po_distribution_id is not provided
10500               l_po_line_location_id := l_Inv_tax_List(i).po_line_location_id;
10501 
10502             IF NOT (AP_ETAX_UTILITY_PKG.Get_PO_Info(
10503                P_Po_Line_Location_Id         => l_po_line_location_id,
10504                P_PO_Distribution_Id          => null,
10505                P_Application_Id              => l_ref_doc_application_id,
10506                P_Entity_code                 => l_ref_doc_entity_code,
10507                P_Event_Class_Code            => l_ref_doc_event_class_code,
10508                P_PO_Quantity                 => l_ref_doc_line_quantity,
10509                P_Product_Org_Id              => l_product_org_id,
10510                P_Po_Header_Id                => l_ref_doc_trx_id,
10511                P_Po_Header_curr_conv_rate    => l_po_header_curr_conv_rate,
10512                P_Uom_Code                    => l_uom_code,
10513                P_Dist_Qty                    => l_dummy,
10514                P_Ship_Price                  => l_dummy,
10515                P_Error_Code                  => P_error_code,
10516                P_Calling_Sequence            => l_curr_calling_sequence)) THEN
10517 
10518              --bug10140354
10519 	     insert into ap_errors_gt(invoice_id , message_text) values (P_Invoice_Header_Rec.Invoice_id , 'Error in Get_PO_Info ');
10520 	      l_return_status := FALSE;
10521             END IF;
10522 
10523             l_ref_doc_trx_level_type := 'SHIPMENT';
10524 
10525           ELSE
10526             l_ref_doc_application_id     := Null;
10527             l_ref_doc_entity_code        := Null;
10528             l_ref_doc_event_class_code   := Null;
10529             l_ref_doc_line_quantity      := Null;
10530             l_product_org_id             := Null;
10531             l_ref_doc_trx_id             := Null;
10532             l_ref_doc_trx_level_type     := Null;
10533           END IF;
10534 
10535           -------------------------------------------------------------------
10536           l_debug_info := 'Step 7: Get Additional receipt matched info ';
10537           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10538               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10539           END IF;
10540           -------------------------------------------------------------------
10541           IF ( l_return_status = TRUE AND
10542                l_Inv_tax_List(i).rcv_transaction_id IS NOT NULL) THEN
10543             IF NOT (AP_ETAX_UTILITY_PKG.Get_Receipt_Info(
10544                P_Rcv_Transaction_Id          => l_Inv_tax_List(i).rcv_transaction_id,
10545                P_Application_Id              => l_applied_to_application_id,
10546                P_Entity_code                 => l_applied_to_entity_code,
10547                P_Event_Class_Code            => l_applied_to_event_class_code,
10548                P_Transaction_Date            => l_trx_receipt_date,
10549                P_Error_Code                  => P_error_code,
10550                P_Calling_Sequence            => l_curr_calling_sequence)) THEN
10551 
10552               --bug10140354
10553 	       insert into ap_errors_gt(invoice_id , message_text) values (P_Invoice_Header_Rec.Invoice_id , 'Error in Get_Receipt_Info');
10554 	       l_return_status := FALSE;
10555             END IF;
10556           ELSE
10557             l_applied_to_application_id   := Null;
10558             l_applied_to_entity_code      := Null;
10559             l_applied_to_event_class_code := Null;
10560             l_trx_receipt_date            := Null;
10561           END IF;
10562 
10563           -------------------------------------------------------------------
10564           l_debug_info := 'Step 8: Get Additional Correction Invoice Info ';
10565           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10566               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10567           END IF;
10568           -------------------------------------------------------------------
10569           IF ( l_return_status = TRUE AND
10570                l_Inv_tax_List(i).corrected_inv_id IS NOT NULL AND
10571                l_Inv_tax_list(i).corrected_line_number IS NOT NULL) THEN
10572             IF NOT (AP_ETAX_UTILITY_PKG.Get_Corrected_Invoice_Info(
10573                P_Corrected_Invoice_Id        => l_Inv_tax_List(i).corrected_inv_id,
10574                P_Corrected_Line_Number       => l_Inv_tax_List(i).corrected_line_number,
10575                P_Application_Id              => l_adj_doc_application_id,
10576                P_Entity_code                 => l_adj_doc_entity_code,
10577                P_Event_Class_Code            => l_adj_doc_event_class_code,
10578                P_Invoice_Number              => l_adj_doc_number,
10579                P_Invoice_Date                => l_adj_doc_date,
10580                P_Error_Code                  => P_error_code,
10581                P_Calling_Sequence            => l_curr_calling_sequence)) THEN
10582 
10583             --bug10140354
10584 	     insert into ap_errors_gt(invoice_id , message_text) values (P_Invoice_Header_Rec.Invoice_id , 'Error in Get_Corrected_Invoice_Info');
10585 	      l_return_status := FALSE;
10586             END IF;
10587 
10588           ELSE
10589             l_adj_doc_application_id   := Null;
10590             l_adj_doc_entity_code      := Null;
10591             l_adj_doc_event_class_code := Null;
10592             l_adj_doc_number           := Null;
10593             l_adj_doc_date             := Null;
10594           END IF;
10595 
10596           -------------------------------------------------------------------
10597           l_debug_info := 'Step 9: Get line_amt_includes_tax_flag';
10598           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10599               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10600           END IF;
10601           -------------------------------------------------------------------
10602 
10603           IF (l_Inv_tax_List(i).po_line_location_id IS NOT NULL) THEN
10604             -- NONE
10605             l_line_amt_includes_tax_flag := 'N';
10606 
10607           ELSE
10608            IF (p_calling_mode = 'CALCULATE QUOTE')
10609               OR
10610               (p_invoice_header_rec.invoice_type_lookup_code = 'EXPENSE REPORT'
10611                and nvl(l_inv_tax_list(i).line_type_lookup_code, 'N') <> 'PREPAY') THEN
10612              -- ALL
10613              l_line_amt_includes_tax_flag := 'A';
10614 
10615            ELSE
10616              -- STANDARD
10617              l_line_amt_includes_tax_flag := 'S';
10618 
10619            END IF;
10620           END IF;
10621 
10622           -------------------------------------------------------------------
10623           l_debug_info := 'Step 10: Get ctrl_hdr_tx_appl_flag';
10624           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10625               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10626           END IF;
10627           -------------------------------------------------------------------
10628           IF P_Invoice_Header_Rec.control_amount IS NOT NULL THEN
10629             l_ctrl_hdr_tx_appl_flag := 'Y';
10630           ELSE
10631             l_ctrl_hdr_tx_appl_flag := 'N';
10632           END IF;
10633 
10634           -------------------------------------------------------------------
10635           l_debug_info := 'Step 11: Populate pl/sql table';
10636           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10637               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10638           END IF;
10639           -------------------------------------------------------------------
10640           IF (l_return_status = TRUE ) THEN
10641 
10642             trans_lines(i).application_id 			:= 200;
10643             trans_lines(i).entity_code 				:= 'AP_INVOICES';
10644             trans_lines(i).event_class_code 			:= p_event_class_code;
10645             trans_lines(i).trx_id 				:= P_Invoice_Header_Rec.invoice_id;
10646             trans_lines(i).trx_level_type 			:= 'LINE';
10647             trans_lines(i).trx_line_id 				:= l_inv_tax_list(i).line_number;
10648             trans_lines(i).line_level_action 			:= l_line_level_action;
10649 
10650             trans_lines(i).trx_receipt_date 			:= l_trx_receipt_date;
10651             trans_lines(i).trx_line_type 			:= l_inv_tax_list(i).line_type_lookup_code;
10652             trans_lines(i).trx_line_date 			:= P_Invoice_Header_Rec.invoice_date;
10653             trans_lines(i).trx_business_category 		:= l_inv_tax_list(i).trx_business_category;
10654             trans_lines(i).line_intended_use 			:= l_inv_tax_list(i).primary_intended_use;
10655             trans_lines(i).user_defined_fisc_class 		:= l_inv_tax_list(i).user_defined_fisc_class;
10656             trans_lines(i).line_amt 				:= l_inv_tax_list(i).amount;
10657             trans_lines(i).trx_line_quantity 			:= l_inv_tax_list(i).quantity_invoiced;
10658             trans_lines(i).unit_price 				:= l_inv_tax_list(i).unit_price;
10659 
10660             trans_lines(i).product_id 				:= l_inv_tax_list(i).inventory_item_id;
10661             trans_lines(i).product_fisc_classification 		:= l_inv_tax_list(i).product_fisc_classification;
10662             trans_lines(i).product_org_id 			:= l_product_org_id;
10663             trans_lines(i).uom_code 				:= l_uom_code;
10664             trans_lines(i).product_type 			:= l_inv_tax_list(i).product_type;
10665             trans_lines(i).product_category 			:= l_inv_tax_list(i).product_category;
10666             trans_lines(i).fob_point 				:= l_fob_point;
10667 
10668             -- 7262269 /*Bug 13079028: Removed the IF-ELSE and called the function directly to handle all cases*/
10669             --IF l_inv_tax_list(i).po_line_location_id IS NOT NULL THEN
10670              l_ship_to_party_id := get_po_ship_to_org_id (l_inv_tax_list(i).po_line_location_id,
10671 	                                                  l_inv_tax_list(i).inventory_item_id, /*Bug 13079028*/
10672 							  l_inv_tax_list(i).org_id, /*Bug 13079028*/
10673 							  l_inv_tax_list(i).application_id, /*Bug 13079028*/
10674 							  l_inv_tax_list(i).reference_key5); /*Bug 13079028*/
10675            /*ELSE
10676              l_ship_to_party_id := l_inv_tax_list(i).org_id;
10677             END IF;*/ /*Bug 13079028*/
10678 
10679             trans_lines(i).ship_to_party_id		:= l_ship_to_party_id;
10680             -- 7262269
10681 
10682             trans_lines(i).ship_from_party_id 			:= P_Invoice_Header_Rec.party_id;
10683 
10684             trans_lines(i).bill_to_party_id			:= l_inv_tax_list(i).org_id;
10685             trans_lines(i).bill_from_party_id			:= P_Invoice_Header_Rec.party_id;
10686             trans_lines(i).ship_from_party_site_id		:= P_Invoice_Header_Rec.party_site_id;
10687             trans_lines(i).bill_from_party_site_id		:= P_Invoice_Header_Rec.party_site_id;
10688 
10689             trans_lines(i).ship_to_location_id			:= l_inv_tax_list(i).ship_to_location_id;
10690 	        trans_lines(i).ship_from_location_id		:= l_location_id;
10691             trans_lines(i).bill_to_location_id			:= l_bill_to_location_id;
10692             trans_lines(i).bill_from_location_id		:= l_location_id;
10693 
10694             trans_lines(i).account_ccid				:= l_inv_tax_list(i).default_dist_ccid;
10695             trans_lines(i).merchant_party_country		:= l_inv_tax_list(i).country_of_supply;
10696 
10697             trans_lines(i).ref_doc_application_id		:= l_ref_doc_application_id;
10698             trans_lines(i).ref_doc_entity_code			:= l_ref_doc_entity_code;
10699             trans_lines(i).ref_doc_event_class_code		:= l_ref_doc_event_class_code;
10700             trans_lines(i).ref_doc_trx_id			:= l_ref_doc_trx_id;
10701             trans_lines(i).ref_doc_line_id			:= l_inv_tax_list(i).po_line_location_id;
10702             trans_lines(i).ref_doc_line_quantity		:= l_ref_doc_line_quantity;
10703 	        trans_lines(i).ref_doc_trx_level_type               := l_ref_doc_trx_level_type; -- bug 8578833
10704 
10705             trans_lines(i).adjusted_doc_application_id		:= l_adj_doc_application_id;
10706             trans_lines(i).adjusted_doc_entity_code		:= l_adj_doc_entity_code;
10707             trans_lines(i).adjusted_doc_event_class_code	:= l_adj_doc_event_class_code;
10708             trans_lines(i).adjusted_doc_trx_id			:= l_inv_tax_list(i).corrected_inv_id;
10709             trans_lines(i).adjusted_doc_line_id			:= l_inv_tax_list(i).corrected_line_number;
10710             trans_lines(i).adjusted_doc_number			:= l_adj_doc_number;
10711             trans_lines(i).adjusted_doc_date			:= l_adj_doc_date;
10712 
10713             trans_lines(i).applied_to_application_id		:= l_applied_to_application_id;
10714             trans_lines(i).applied_to_entity_code		:= l_applied_to_entity_code;
10715             trans_lines(i).applied_to_event_class_code		:= l_applied_to_event_class_code;
10716             trans_lines(i).applied_to_trx_id			:= l_inv_tax_list(i).rcv_transaction_id;
10717 
10718 	        IF l_inv_tax_list(i).rcv_transaction_id IS NOT NULL THEN
10719 	           trans_lines(i).applied_to_trx_line_id		:= l_inv_tax_list(i).po_line_location_id;
10720             END IF;
10721 
10722             trans_lines(i).trx_line_number			:= l_inv_tax_list(i).line_number;
10723             trans_lines(i).trx_line_description			:= l_inv_tax_list(i).description;
10724             trans_lines(i).product_description			:= l_inv_tax_list(i).item_description;
10725             trans_lines(i).trx_line_gl_date			:= l_inv_tax_list(i).accounting_date;
10726 
10727             trans_lines(i).merchant_party_name			:= l_inv_tax_list(i).merchant_name;
10728             trans_lines(i).merchant_party_document_number	:= l_inv_tax_list(i).merchant_document_number;
10729             trans_lines(i).merchant_party_reference		:= l_inv_tax_list(i).merchant_reference;
10730             trans_lines(i).merchant_party_taxpayer_id		:= l_inv_tax_list(i).merchant_taxpayer_id;
10731             trans_lines(i).merchant_party_tax_reg_number	:= l_inv_tax_list(i).merchant_tax_reg_number;
10732 
10733             trans_lines(i).assessable_value			:= l_inv_tax_list(i).assessable_value;
10734             trans_lines(i).line_amt_includes_tax_flag		:= l_line_amt_includes_tax_flag;
10735             trans_lines(i).historical_flag			:= 'N';
10736 
10737             trans_lines(i).ctrl_hdr_tx_appl_flag		:= l_ctrl_hdr_tx_appl_flag;
10738             trans_lines(i).ctrl_total_line_tx_amt		:= l_line_control_amount;
10739 
10740             trans_lines(i).source_application_id		:= l_inv_tax_list(i).source_application_id;
10741             trans_lines(i).source_entity_code	   		:= l_inv_tax_list(i).source_entity_code;
10742             trans_lines(i).source_event_class_code 		:= l_inv_tax_list(i).source_event_class_code;
10743             trans_lines(i).source_trx_id	   		:= l_inv_tax_list(i).source_trx_id;
10744             trans_lines(i).source_line_id	   		:= l_inv_tax_list(i).source_line_id;
10745             trans_lines(i).source_trx_level_type		:= l_inv_tax_list(i).source_trx_level_type;
10746 
10747             trans_lines(i).input_tax_classification_code	:= l_inv_tax_list(i).tax_classification_code;
10748 -- Debug messages added for 6321366
10749            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10750               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_transaction_lines_gt values ');
10751               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: ' || trans_lines(i).event_class_code);
10752               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '           || trans_lines(i).trx_id);
10753               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_id: '      || trans_lines(i).trx_line_id);
10754               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_level_type: '   || trans_lines(i).trx_level_type);
10755               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_type: '    || trans_lines(i).trx_line_type );
10756               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_level_action: '|| trans_lines(i).line_level_action);
10757               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_amt: '         || trans_lines(i).line_amt);
10758               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'unit_price: '       || trans_lines(i).unit_price);
10759               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_amt_includes_tax_flag: '       || trans_lines(i)
10760                 .line_amt_includes_tax_flag );
10761            END IF;
10762 
10763           END IF; -- l_return_status
10764 
10765           IF (l_inv_tax_list(i).rcv_transaction_id IS NULL) THEN
10766             -------------------------------------------------------------------
10767             l_debug_info := 'Step 12: Populate pl/sql table for TAX line if it '||
10768                             'is not receipt matched';
10769             IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10770                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10771             END IF;
10772             -------------------------------------------------------------------
10773             IF (l_return_status = TRUE ) THEN
10774               tax_lines.EXTEND(1);
10775               link_lines.EXTEND(1);
10776 
10777               tax_lines(k).summary_tax_line_number		:= l_inv_tax_list(i).line_number;
10778               tax_lines(k).internal_organization_id		:= P_Invoice_Header_Rec.org_id;
10779               tax_lines(k).application_id			:= 200;
10780               tax_lines(k).entity_code				:= 'AP_INVOICES';
10781               tax_lines(k).event_class_code			:= p_event_class_code;
10782               tax_lines(k).trx_id				:= P_Invoice_Header_Rec.invoice_id;
10783 
10784               tax_lines(k).tax_regime_code			:= l_inv_tax_list(i).tax_regime_code;
10785               tax_lines(k).tax					:= l_inv_tax_list(i).tax;
10786               tax_lines(k).tax_status_code			:= l_inv_tax_list(i).tax_status_code;
10787               tax_lines(k).tax_rate_code			:= l_inv_tax_list(i).tax_rate_code;
10788               tax_lines(k).tax_rate				:= l_inv_tax_list(i).tax_rate;
10789               tax_lines(k).tax_amt				:= l_inv_tax_list(i).amount;
10790               tax_lines(k).tax_line_allocation_flag             := 'Y';
10791               -- Populating tax jurisdiction code for bug 6411838
10792               tax_lines(k).tax_jurisdiction_code                := l_inv_tax_list(i).tax_jurisdiction_code ;
10793 
10794               --Bug9819170
10795               l_copy_line_dff_flag := NVL(fnd_profile.value('AP_COPY_INV_LINE_DFF'),'N');
10796               IF l_copy_line_dff_flag = 'Y' THEN
10797                tax_lines(k).ATTRIBUTE1         :=  l_inv_tax_list(i).ATTRIBUTE1;
10798                tax_lines(k).ATTRIBUTE2         :=  l_inv_tax_list(i).ATTRIBUTE2;
10799                tax_lines(k).ATTRIBUTE3         :=  l_inv_tax_list(i).ATTRIBUTE3;
10800                tax_lines(k).ATTRIBUTE4         :=  l_inv_tax_list(i).ATTRIBUTE4;
10801                tax_lines(k).ATTRIBUTE5         :=  l_inv_tax_list(i).ATTRIBUTE5;
10802                tax_lines(k).ATTRIBUTE6         :=  l_inv_tax_list(i).ATTRIBUTE6;
10803                tax_lines(k).ATTRIBUTE7         :=  l_inv_tax_list(i).ATTRIBUTE7;
10804                tax_lines(k).ATTRIBUTE8         :=  l_inv_tax_list(i).ATTRIBUTE8;
10805                tax_lines(k).ATTRIBUTE9         :=  l_inv_tax_list(i).ATTRIBUTE9;
10806                tax_lines(k).ATTRIBUTE10        :=  l_inv_tax_list(i).ATTRIBUTE10;
10807                tax_lines(k).ATTRIBUTE11        :=  l_inv_tax_list(i).ATTRIBUTE11;
10808                tax_lines(k).ATTRIBUTE12        :=  l_inv_tax_list(i).ATTRIBUTE12;
10809                tax_lines(k).ATTRIBUTE13        :=  l_inv_tax_list(i).ATTRIBUTE13;
10810                tax_lines(k).ATTRIBUTE14        :=  l_inv_tax_list(i).ATTRIBUTE14;
10811                tax_lines(k).ATTRIBUTE15        :=  l_inv_tax_list(i).ATTRIBUTE15;         --BUG13719477
10812                tax_lines(k).ATTRIBUTE_CATEGORY := l_inv_tax_list(i).ATTRIBUTE_CATEGORY;
10813               END IF;
10814               --Bug9819170
10815 
10816 -- Debug messages added for 6321366
10817             IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10818               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_import_tax_lines_gt values ');
10819               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'summary_tax_line_number: ' || tax_lines(k).summary_tax_line_number);
10820               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'application_id: '           || tax_lines(k).application_id);
10821               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: '      || tax_lines(k).event_class_code);
10822               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '   || tax_lines(k).trx_id);
10823               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax: '   || tax_lines(k).tax );
10824               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax_amt: '    || tax_lines(k).tax_amt );
10825               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'tax_line_allocation_flag: '|| tax_lines(k).tax_line_allocation_flag);
10826             END IF;
10827 
10828               --------------------------------------------------------------
10829               l_debug_info := 'Step 13: Populate link structure';
10830               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10831                   FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10832               END IF;
10833               --------------------------------------------------------------
10834               link_lines(k).application_id			:= 200;
10835               link_lines(k).entity_code				:= 'AP_INVOICES';
10836               link_lines(k).event_class_code			:= p_event_class_code;
10837               link_lines(k).trx_id				:= P_Invoice_Header_Rec.invoice_id;
10838               link_lines(k).trx_level_type			:= 'LINE';
10839               link_lines(k).trx_line_id				:= l_inv_tax_list(i).line_number;
10840               link_lines(k).summary_tax_line_number		:= l_inv_tax_list(i).line_number;
10841               link_lines(k).line_amt				:= l_inv_tax_list(i).amount;
10842 
10843 -- Debug messages added for 6321366
10844             IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10845               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'zx_trx_tax_link_gt values ');
10846               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'summary_tax_line_number: ' || link_lines(k).summary_tax_line_number);
10847               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'application_id: '           || link_lines(k).application_id);
10848               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: '      || link_lines(k).event_class_code);
10849               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '   || link_lines(k).trx_id);
10850               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_level_type: '   || link_lines(k).trx_level_type );
10851               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_id: '   || link_lines(k).trx_line_id);
10852               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_amt: '    || link_lines(k).line_amt );
10853             END IF;
10854               k := k + 1;
10855 
10856             END IF;  -- l_return_status validation for TAX lines
10857           END IF;
10858 
10859         END LOOP;  -- end of loop TAX lines
10860       END IF; -- is l_inv_tax_list populated
10861     END IF; -- Is invoice tax only?
10862 
10863     -------------------------------------------------------------------
10864     l_debug_info := 'Step 14: Bulk Insert into global temp tables';
10865     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10866       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10867     END IF;
10868     -------------------------------------------------------------------
10869     IF ( l_return_status = TRUE ) THEN
10870 
10871       IF (trans_lines.COUNT > 0) THEN
10872         FORALL m IN trans_lines.FIRST..trans_lines.LAST
10873           INSERT INTO zx_transaction_lines_gt
10874           VALUES trans_lines(m);
10875       END IF;
10876 
10877       IF (tax_lines.COUNT > 0) THEN
10878 
10879     -------------------------------------------------------------------
10880     l_debug_info := 'Step 15: Populate Tax Lines: '||tax_lines.count;
10881     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10882       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10883     END IF;
10884     -------------------------------------------------------------------
10885 
10886         FORALL m IN tax_lines.FIRST..tax_lines.LAST
10887           INSERT INTO zx_import_tax_lines_gt
10888           VALUES tax_lines(m);
10889 
10890         --Bug9819170
10891         -------------------------------------------------------------------
10892         l_debug_info := 'DFFs Of The Tax Lines';
10893         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10894            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10895         END IF;
10896        -------------------------------------------------------------------
10897         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
10898            IF l_copy_line_dff_flag = 'Y'  THEN
10899               FOR i IN (SELECT *
10900                           FROM zx_import_tax_lines_gt
10901                          WHERE trx_id = P_Invoice_Header_Rec.invoice_id
10902                            AND application_id = 200
10903                            AND entity_code ='AP_INVOICES'
10904                            AND event_class_code IN ('STANDARD INVOICES','PREPAYMENT INVOICES','EXPENSE REPORTS')) LOOP
10905 
10906                         l_debug_info := '1 '|| i.ATTRIBUTE1;
10907                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10908                         l_debug_info := '2 '||i.ATTRIBUTE2;
10909                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10910                         l_debug_info := '3 '||i.ATTRIBUTE3;
10911                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10912                         l_debug_info := '4 '||i.ATTRIBUTE4;
10913                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10914                         l_debug_info := '5 '||i.ATTRIBUTE5;
10915                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10916                         l_debug_info := '6 '||i.ATTRIBUTE6;
10917                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10918                         l_debug_info := '7 '||i.ATTRIBUTE7;
10919                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10920                         l_debug_info := '8 '||i.ATTRIBUTE8;
10921                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10922                         l_debug_info := '9 '||i.ATTRIBUTE8;
10923                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10924                         l_debug_info := '10 '||i.ATTRIBUTE10;
10925                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10926                         l_debug_info := '11 '||i.ATTRIBUTE11;
10927                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10928                         l_debug_info := '12 '||i.ATTRIBUTE12;
10929                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10930                         l_debug_info := '13 '||i.ATTRIBUTE13;
10931                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10932                         l_debug_info := '14 '||i.ATTRIBUTE14;
10933                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10934                         l_debug_info := '15 '||i.ATTRIBUTE15;
10935                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10936                         l_debug_info := 'Attrib Cat '||i.ATTRIBUTE_CATEGORY;
10937                         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
10938               END LOOP;
10939            END IF;
10940         END IF;
10941       --Bug9819170
10942 
10943       END IF;
10944 
10945       -- This bulk insert will only be effective when the invoice is tax_only
10946       -- and there are tax lines with the tax info to be imported
10947       IF (link_lines.COUNT > 0) THEN
10948         FORALL m IN link_lines.FIRST..link_lines.LAST
10949           INSERT INTO zx_trx_tax_link_gt
10950           VALUES link_lines(m);
10951       END IF;
10952 
10953     END IF;
10954 
10955     RETURN l_return_status;
10956 
10957   EXCEPTION
10958     WHEN OTHERS THEN
10959       IF (SQLCODE <> -20001) THEN
10960         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
10961         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
10962         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
10963         FND_MESSAGE.SET_TOKEN('PARAMETERS',
10964           ' P_Invoice_Header_Rec = '||P_Invoice_Header_Rec.Invoice_Id||
10965           ' P_Calling_Mode ='||P_Calling_Mode||
10966           ' P_Error_Code = '||P_Error_Code||
10967           ' P_Calling_Sequence = '||P_Calling_Sequence);
10968         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
10969       END IF;
10970 
10971      --bug10140354
10972      IF AP_APPROVAL_PKG.G_VALIDATION_REQUEST_ID IS NULL THEN
10973         APP_EXCEPTION.RAISE_EXCEPTION;
10974     ELSE
10975     l_err:= SQLERRM;
10976     insert into ap_errors_gt(invoice_id , message_text) values(P_Invoice_Header_Rec.Invoice_id , l_err);
10977     RETURN FALSE;
10978     END IF;
10979 
10980   END Populate_Tax_Lines_GT;
10981 
10982 
10983 /*=============================================================================
10984  |  FUNCTION - Populate_Distributions_GT()
10985  |
10986  |  DESCRIPTION
10987  |      This function will get additional information required to populate the
10988  |      ZX_ITM_DISTRIBUTIONS_GT
10989  |      This function returns TRUE if the population of the temp table goes
10990  |      through successfully.  Otherwise, FALSE.
10991  |
10992  |  PARAMETERS
10993  |      P_Invoice_Header_Rec - record with invoice header information
10994  |      P_Calling_Mode - calling mode. it is used to
10995  |      P_Event_Class_Code - Event class code for document
10996  |      P_Event_Type_Code - Event Type code for invoice
10997  |      P_error_code - Error code to be returned
10998  |      P_calling_sequence -  Calling sequence
10999  |
11000  |  MODIFICATION HISTORY
11001  |    DATE          Author         Action
11002  |    20-OCT-2003   SYIDNER        Created
11003  |
11004  *============================================================================*/
11005 
11006   FUNCTION Populate_Distributions_GT(
11007              P_Invoice_Header_Rec      IN ap_invoices_all%ROWTYPE,
11008              P_Calling_Mode            IN VARCHAR2,
11009              P_Event_Class_Code        IN VARCHAR2,
11010              P_Event_Type_Code         IN VARCHAR2,
11011              P_Error_Code              OUT NOCOPY VARCHAR2,
11012              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN
11013   IS
11014     l_debug_info                 VARCHAR2(240);
11015     l_curr_calling_sequence      VARCHAR2(4000);
11016 
11017     TYPE Trans_Dist_Tab_Type IS TABLE OF zx_itm_distributions_gt%ROWTYPE;
11018     trans_dists                     Trans_Dist_Tab_Type := Trans_Dist_Tab_Type();
11019 
11020     -- Purchase Order Info
11021     l_ref_doc_application_id	    zx_itm_distributions_gt.ref_doc_application_id%TYPE;
11022     l_ref_doc_entity_code	    zx_itm_distributions_gt.ref_doc_entity_code%TYPE;
11023     l_ref_doc_event_class_code      zx_itm_distributions_gt.ref_doc_event_class_code%TYPE;
11024     l_ref_doc_trx_id                zx_itm_distributions_gt.ref_doc_trx_id%TYPE;
11025     l_ref_doc_trx_level_type	    zx_itm_distributions_gt.ref_doc_trx_level_type%TYPE;
11026     l_ref_doc_line_quantity	    zx_transaction_lines_gt.ref_doc_line_quantity%TYPE;
11027     l_ref_doc_trx_line_dist_qty     zx_itm_distributions_gt.ref_doc_trx_line_dist_qty%TYPE;
11028     l_po_unit_price		    po_line_locations_all.price_override%TYPE;
11029     l_po_line_location_id           po_line_locations_all.line_location_id%TYPE;
11030     l_po_release_id                 zx_itm_distributions_gt.ref_doc_trx_id%TYPE;
11031     l_product_org_id                zx_transaction_lines_gt.product_org_id%TYPE;
11032     l_uom_code                      mtl_units_of_measure.uom_code%TYPE;
11033 
11034     l_dist_level_action             zx_itm_distributions_gt.dist_level_action%TYPE;
11035     l_line_quantity_invoiced        zx_itm_distributions_gt.trx_line_quantity%TYPE;
11036     l_amount                        zx_itm_distributions_gt.trx_line_dist_amt%TYPE;
11037     l_price_diff		    zx_itm_distributions_gt.price_diff%TYPE;
11038     l_po_header_curr_conv_rate      zx_itm_distributions_gt.ref_doc_curr_conv_rate%TYPE;
11039     l_receipt_curr_conv_rate	    zx_itm_distributions_gt.applied_to_doc_curr_conv_rate%TYPE;
11040     l_converted_qty		    ap_invoice_distributions_all.quantity_invoiced%TYPE;
11041     l_converted_price		    ap_invoice_distributions_all.unit_price%TYPE;
11042 
11043     -- Correction Invoice Info
11044     l_adj_doc_application_id	    zx_transaction_lines_gt.adjusted_doc_application_id%TYPE;
11045     l_adj_doc_entity_code           zx_transaction_lines_gt.adjusted_doc_entity_code%TYPE;
11046     l_adj_doc_event_class_code      zx_transaction_lines_gt.adjusted_doc_event_class_code%TYPE;
11047     l_adj_doc_number                zx_transaction_lines_gt.adjusted_doc_number%TYPE;
11048     l_adj_doc_date                  zx_transaction_lines_gt.adjusted_doc_date%TYPE;
11049     l_adj_doc_trx_level_type        zx_transaction_lines_gt.adjusted_doc_trx_level_type%TYPE;
11050     l_adj_doc_trx_id		    zx_itm_distributions_gt.adjusted_doc_trx_id%TYPE;
11051     l_adj_doc_line_id		    zx_itm_distributions_gt.adjusted_doc_line_id%TYPE;
11052 
11053     -- Prepayment Info
11054     l_prepay_doc_application_id     zx_transaction_lines_gt.applied_from_application_id%TYPE;
11055     l_prepay_doc_entity_code        zx_transaction_lines_gt.applied_from_entity_code%TYPE;
11056     l_prepay_doc_event_class_code   zx_transaction_lines_gt.applied_from_event_class_code%TYPE;
11057     l_prepay_doc_number             ap_invoices_all.invoice_num%TYPE;
11058     l_prepay_doc_date               ap_invoices_all.invoice_date%TYPE;
11059     l_applied_from_trx_id	    ap_invoice_lines_all.invoice_id%TYPE;
11060     l_applied_from_line_id	    ap_invoice_lines_all.line_number%TYPE;
11061     l_applied_from_trx_level_type   zx_transaction_lines_gt.applied_from_trx_level_type%TYPE;
11062 
11063     l_prepay_inv_id	            ap_invoice_lines_all.invoice_id%TYPE;
11064     l_prepay_line_num               ap_invoice_lines_all.line_number%TYPE;
11065 
11066     l_return_status                 BOOLEAN := TRUE;
11067     l_api_name                      CONSTANT VARCHAR2(100) := 'Populate_Distributions_GT';
11068     l_intended_use                ap_invoice_lines_all.primary_intended_use%TYPE; --8796484
11069     j                               NUMBER:=0;                                    --Bug9494315
11070 
11071     --bug 10621602 Starts Added the below variables to be used
11072 
11073     l_retained_amount  NUMBER ;
11074     l_retainage_rate  NUMBER ;
11075     l_retainage_release_factor NUMBER  := 1;
11076     l_retainage_rrf_amount NUMBER;
11077     l_quantity_invoiced NUMBER := 0 ;
11078     l_unit_price NUMBER := 0 ;
11079 
11080     --bug10621602 ends
11081 
11082   BEGIN
11083 
11084     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Populate_Distributions_GT<-'||
11085                                P_calling_sequence;
11086 
11087     IF (l_inv_dist_list.COUNT > 0) THEN
11088 
11089       --trans_dists.EXTEND(l_inv_dist_list.COUNT);                                --Bug9494315
11090 
11091       -------------------------------------------------------------------
11092       l_debug_info := 'Step 1: Loop through all the distributions';
11093       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11094           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11095       END IF;
11096       -------------------------------------------------------------------
11097 
11098       FOR i IN l_inv_dist_list.FIRST..l_inv_dist_list.LAST LOOP
11099 
11100           IF (l_inv_dist_list(i).line_type_lookup_code NOT IN
11101              ('AWT', 'REC_TAX', 'NONREC_TAX', 'TRV', 'TERV', 'TIPV')) THEN  --Bug9494315
11102 
11103                 trans_dists.EXTEND(1);   --Bug9494315
11104                 j:=j+1;                  --Bug9494315
11105 
11106 
11107 
11108 
11109 
11110         -------------------------------------------------------------------
11111         l_debug_info := 'Step 2: Get line_level_action for distribution';
11112 	    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11113             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11114         END IF;
11115         -------------------------------------------------------------------
11116         /*  --Bug12660669 Added source and line type check */
11117 	--bug14189635, changed l_inv_line_list to l_inv_dist_list
11118         IF (P_Invoice_Header_Rec.source = 'SelfService' AND
11119 		        P_Invoice_Header_Rec.invoice_type_lookup_code = 'EXPENSE REPORT' AND
11120                 l_inv_dist_list(i).line_type_lookup_code = 'MISCELLANEOUS' ) THEN
11121 		--bug14155552, changed to dist level action to 'NO_ACTION' from 'NO_CHANGE'
11122                 l_dist_level_action := 'NO_ACTION';
11123 
11124         ELSIF ( l_inv_dist_list(i).tax_already_distributed_flag ='Y' ) THEN
11125 
11126 	   IF l_inv_dist_list(i).prepay_distribution_id is not null then /*Bug13622888*/
11127 	      l_dist_level_action := 'NO_ACTION';  /*Bug13622888*/
11128 	    ELSE
11129               l_dist_level_action := 'UPDATE';
11130             END IF;
11131 
11132         ELSE
11133            l_dist_level_action := 'CREATE';
11134 
11135         END IF;
11136 
11137         -----------------------------------------------------------------
11138         l_debug_info := 'Step 3: Update the amount including IPV/ERV';
11139 	    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11140             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11141         END IF;
11142         -----------------------------------------------------------------
11143         IF (l_inv_dist_list(i).related_retainage_dist_id IS NOT NULL) THEN
11144            SELECT SUM(amount)
11145              INTO l_amount
11146              FROM ap_invoice_distributions_all
11147             WHERE invoice_id = l_inv_dist_list(i).invoice_id
11148 	      AND (related_id = l_inv_dist_list(i).related_id
11149                    or related_retainage_dist_id = l_inv_dist_list(i).related_retainage_dist_id);
11150 
11151          /* 9526592 - added elsif condition for better performance of the query */
11152 	ELSIF (l_inv_dist_list(i).related_id IS NOT NULL) THEN
11153 
11154            SELECT SUM(amount)
11155              INTO l_amount
11156              FROM ap_invoice_distributions_all
11157             WHERE invoice_id = l_inv_dist_list(i).invoice_id
11158 	      AND related_id = l_inv_dist_list(i).related_id;
11159 
11160         ELSE
11161            l_amount := l_inv_dist_list(i).amount;
11162 
11163         END IF;
11164 
11165         -------------------------------------------------------------------
11166         l_debug_info := 'Step 4: Get correction invoice info';
11167         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11168             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11169         END IF;
11170         -------------------------------------------------------------------
11171         BEGIN
11172           SELECT quantity_invoiced, po_release_id, primary_intended_use --8796484
11173             INTO l_line_quantity_invoiced, l_po_release_id, l_intended_use
11174             FROM ap_invoice_lines_all
11175            WHERE invoice_id = l_inv_dist_list(i).invoice_id
11176              AND line_number = l_inv_dist_list(i).invoice_line_number;
11177         END;
11178 
11179         -------------------------------------------------------------------
11180         l_debug_info := 'Step 5: Get purchase order info';
11181 	    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11182             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11183         END IF;
11184         -------------------------------------------------------------------
11185 
11186 	--bug14189635, changed l_inv_line_list to l_inv_dist_list
11187         IF ( l_inv_dist_list(i).po_distribution_id IS NOT NULL AND
11188              l_inv_dist_list(i).line_type_lookup_code NOT IN ('MISCELLANEOUS','FREIGHT')) THEN   --Bug14039130
11189 
11190           IF NOT (AP_ETAX_UTILITY_PKG.Get_PO_Info(
11191                P_Po_Line_Location_Id      => l_po_line_location_id,
11192                P_Po_Distribution_Id       => l_inv_dist_list(i).po_distribution_id,
11193                P_Application_Id           => l_ref_doc_application_id,
11194                P_Entity_code              => l_ref_doc_entity_code,
11195                P_Event_Class_Code         => l_ref_doc_event_class_code,
11196                P_PO_Quantity              => l_ref_doc_line_quantity,
11197                P_Product_Org_Id           => l_product_org_id,
11198                P_Po_Header_Id             => l_ref_doc_trx_id,
11199                P_Po_Header_curr_conv_rate => l_po_header_curr_conv_rate,
11200                P_Uom_Code                 => l_uom_code,
11201                P_Dist_Qty                 => l_ref_doc_trx_line_dist_qty,
11202                P_Ship_Price               => l_po_unit_price,
11203                P_Error_Code               => P_error_code,
11204                P_Calling_Sequence         => l_curr_calling_sequence)) THEN
11205 
11206             l_return_status := FALSE;
11207           END IF;
11208 
11209 	  l_ref_doc_trx_level_type := 'SHIPMENT';
11210 
11211         ELSE
11212           l_ref_doc_application_id     := Null;
11216           l_product_org_id             := Null;
11213           l_ref_doc_entity_code        := Null;
11214           l_ref_doc_event_class_code   := Null;
11215           l_ref_doc_line_quantity      := Null;
11217           l_ref_doc_trx_id             := Null;
11218           l_ref_doc_trx_level_type     := Null;
11219 	  l_ref_doc_trx_line_dist_qty  := Null;
11220 	  l_po_unit_price              := Null;
11221 	  l_po_header_curr_conv_rate   := Null;
11222 	  l_uom_code		       := Null;
11223         END IF;
11224 
11225         -------------------------------------------------------------------
11226         l_debug_info := 'Step 6: Set ref_doc_trx_id if distribution is '||
11227                         'receipt matched';
11228 	    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11229             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11230         END IF;
11231         -------------------------------------------------------------------
11232 	--bug14189635, changed l_inv_line_list to l_inv_dist_list
11233         IF (l_return_status = TRUE AND
11234             l_inv_dist_list(i).rcv_transaction_id IS NOT NULL AND
11235             l_inv_dist_list(i).line_type_lookup_code NOT IN ('MISCELLANEOUS','FREIGHT')) THEN   --Bug14039130
11236 
11237           BEGIN
11238             SELECT currency_conversion_rate
11239               INTO l_receipt_curr_conv_rate
11240               FROM rcv_transactions
11241              WHERE transaction_id = l_inv_dist_list(i).rcv_transaction_id;
11242           END;
11243 
11244           get_converted_qty_price
11245                 (x_invoice_distribution_id => l_inv_dist_list(i).invoice_distribution_id,
11246                  x_inv_qty                 => l_converted_qty,
11247                  x_inv_price               => l_converted_price);
11248 
11249         ELSE
11250 	    l_receipt_curr_conv_rate := Null;
11251 	    l_converted_qty          := Null;
11252 	    l_converted_price	     := Null;
11253         END IF;
11254 
11255 /*
11256         -------------------------------------------------------------------
11257         l_debug_info := 'Step 8: Get Additional Correction Invoice Info ';
11258         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11259             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11260         END IF;
11261         -------------------------------------------------------------------
11262         IF ( l_return_status = TRUE AND
11263 	     l_inv_dist_list(i).corrected_invoice_dist_id IS NOT NULL) THEN
11264 
11265 	   Select invoice_id, invoice_line_number
11266 	     Into l_adj_doc_trx_id, l_adj_doc_line_id
11267 	     From ap_invoice_distributions_all
11268 	    Where invoice_distribution_id = l_inv_dist_list(i).corrected_invoice_dist_id;
11269 
11270            IF NOT (AP_ETAX_UTILITY_PKG.Get_Corrected_Invoice_Info(
11271               P_Corrected_Invoice_Id        => l_inv_line_list(i).corrected_inv_id,
11272               P_Corrected_Line_Number       => l_inv_line_list(i).corrected_line_number,
11273               P_Application_Id              => l_adj_doc_application_id,
11277               P_Invoice_Date                => l_adj_doc_date,
11274               P_Entity_code                 => l_adj_doc_entity_code,
11275               P_Event_Class_Code            => l_adj_doc_event_class_code,
11276               P_Invoice_Number              => l_adj_doc_number,
11278               P_Error_Code                  => P_error_code,
11279               P_Calling_Sequence            => l_curr_calling_sequence)) THEN
11280 
11281               l_return_status := FALSE;
11282           END IF;
11283 
11284           l_adj_doc_trx_level_type := 'LINE';
11285 
11286         ELSE
11287             l_adj_doc_application_id   := Null;
11288             l_adj_doc_entity_code      := Null;
11289             l_adj_doc_event_class_code := Null;
11290             l_adj_doc_number           := Null;
11291             l_adj_doc_date             := Null;
11292             l_adj_doc_trx_level_type   := Null;
11293         END IF;
11294 
11295         -------------------------------------------------------------------
11296         l_debug_info := 'Step 8: Get prepayment invoice info';
11297         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11298             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11299         END IF;
11300         -------------------------------------------------------------------
11301         IF ( l_return_status = TRUE AND
11302 	     l_inv_dist_list(i).prepay_distribution_id IS NOT NULL) THEN
11303 
11304 	  SELECT invoice_id, invoice_line_number
11305 	    INTO l_applied_from_trx_id, l_applied_from_line_id
11306 	    FROM ap_invoice_distributions_all
11307 	   WHERE invoice_distribution_id = l_inv_dist_list(i).prepay_distribution_id;
11308 
11309           IF NOT (AP_ETAX_UTILITY_PKG.Get_Prepay_Invoice_Info(
11310               P_Prepay_Invoice_Id           => l_applied_from_trx_id,
11311               P_Prepay_Line_Number          => l_applied_from_line_id,
11312               P_Application_Id              => l_prepay_doc_application_id,
11313               P_Entity_code                 => l_prepay_doc_entity_code,
11314               P_Event_Class_Code            => l_prepay_doc_event_class_code,
11315               P_Invoice_Number              => l_prepay_doc_number,
11316               P_Invoice_Date                => l_prepay_doc_date,
11317               P_Error_Code                  => P_error_code,
11318               P_Calling_Sequence            => l_curr_calling_sequence)) THEN
11319 
11320               l_return_status := FALSE;
11321           END IF;
11322 
11323           l_applied_from_trx_level_type := 'LINE';
11324 
11325         ELSE
11326           l_prepay_doc_application_id   := Null;
11327           l_prepay_doc_entity_code      := Null;
11328           l_prepay_doc_event_class_code := Null;
11329           l_prepay_doc_number           := Null;
11330           l_prepay_doc_date             := Null;
11331           l_applied_from_trx_level_type := Null;
11332 	  l_applied_from_trx_id		:= Null;
11333 	  l_applied_from_line_id	:= Null;
11334         END IF;
11335 */
11336         -------------------------------------------------------------------
11337         l_debug_info := 'Step 7: Populate pl/sql table';
11338 	    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11339             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11340         END IF;
11341         -------------------------------------------------------------------
11342         IF (l_return_status = TRUE) THEN
11343 
11344           trans_dists(j).application_id			:= AP_ETAX_PKG.AP_APPLICATION_ID;
11345           trans_dists(j).entity_code			:= AP_ETAX_PKG.AP_ENTITY_CODE;
11346           trans_dists(j).event_class_code		:= P_event_class_code;
11347           trans_dists(j).trx_id				:= l_inv_dist_list(i).invoice_id;
11348 
11349           IF p_calling_mode = 'DISTRIBUTE RECOUP' THEN
11350              IF  l_inv_dist_list(i).prepay_distribution_id IS NOT NULL THEN
11351 
11352 		 SELECT invoice_id, invoice_line_number
11353                    INTO l_prepay_inv_id, l_prepay_line_num
11354                    FROM ap_invoice_distributions_all
11355                   WHERE invoice_distribution_id = l_inv_dist_list(i).prepay_distribution_id;
11356 
11357                  trans_dists(j).trx_line_id := -1 * (l_prepay_inv_id || l_prepay_line_num || l_inv_dist_list(i).invoice_line_number);
11358 
11359 	     END IF;
11360 	  ELSE
11361 	     trans_dists(j).trx_line_id := l_inv_dist_list(i).invoice_line_number;
11362 
11363           END IF;
11364 
11365 
11366 	   --bug10621602 starts
11367 	   --The below changes are done only for the distributions involving retainage and retainage release .
11368 	   --we will populate the structure ret_dists by deriving the values
11369 
11370 	    IF (l_inv_dist_list(i).related_retainage_dist_id IS NOT NULL
11371 	        OR (l_inv_dist_list(i).retained_invoice_dist_id IS NOT NULL and l_inv_dist_list(i).po_distribution_id IS NOT NULL)) THEN
11372 
11373 	      BEGIN
11374 		    SELECT POL.retainage_rate INTO l_retainage_rate
11375               FROM po_lines_all POL , po_line_locations_all PLL
11376              WHERE PLL.po_header_id = POL.po_header_id
11377                AND PLL.po_line_id = POL.po_line_id
11378                AND PLL.line_location_id = l_po_line_location_id
11379                AND PLL.po_header_id =  l_ref_doc_trx_id;
11380 
11381 		    IF ( l_inv_dist_list(i).RELATED_RETAINAGE_DIST_ID IS NOT NULL) THEN
11382 
11383 		    -- For standard invoice having retainage we will populate the char4 column as 'S' and will get the retained amount
11384 
11385                 trans_dists(j).char4 := 'S';
11386 
11387 		        select amount into l_retained_amount from ap_invoice_distributions_all aid
11388 		         where  aid.invoice_id = l_inv_dist_list(i).invoice_id
11389 		           and aid.line_type_lookup_code = 'RETAINAGE'
11390 		           and aid.related_retainage_dist_id is NOT NULL
11391 		           and aid.related_retainage_dist_id = l_inv_dist_list(i).invoice_distribution_id;
11392 
11393 		    END IF;
11394 
11395             IF (l_inv_dist_list(i).RETAINED_INVOICE_DIST_ID IS NOT NULL) THEN
11396 
11397 		        -- For retainge release invoice we will populate the char4 value as 'R' and the retained amount is 0 .
11398 
11399 			    trans_dists(j).char4 := 'R';
11400                 l_retained_amount := 0;
11401 
11402 		        SELECT amount INTO l_retainage_rrf_amount FROM ap_invoice_distributions_all
11403 		         WHERE line_type_lookup_code = 'RETAINAGE'
11404 			       AND invoice_distribution_id = l_inv_dist_list(i).RETAINED_INVOICE_DIST_ID;
11405 
11406 		        SELECT aid1.quantity_invoiced  , aid1.unit_price INTO l_quantity_invoiced  , l_unit_price
11407                   FROM ap_invoice_distributions_all aid1 , ap_invoice_distributions_all aid2
11408 		         WHERE aid1.line_type_lookup_code IN ('ACCRUAL' ,'ITEM')
11409 		           AND aid1.invoice_distribution_id = aid2.related_retainage_dist_id
11410 			       AND aid2.invoice_distribution_id = l_inv_dist_list(i).RETAINED_INVOICE_DIST_ID
11411 			       AND aid2.line_type_lookup_code = 'RETAINAGE';
11412 
11413 			    -- Get the RRF Factor for multiple retainage release invoices . The value will be 1 in case of single release.
11414 
11415 			    l_retainage_release_factor := abs(l_amount/l_retainage_rrf_amount);
11416 			    l_inv_dist_list(i).unit_price := l_unit_price;
11417                 l_inv_dist_list(i).quantity_invoiced := l_quantity_invoiced;
11418 
11422                 IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11419                 l_debug_info := 'Retainage Release Factor is   ' || l_retainage_release_factor ||
11420 			                    '  Derived Quanity Invoice ' || l_quantity_invoiced ||
11421 				                '  Derived Unit Price ' || l_unit_price  ;
11423                     FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11424                 END IF;
11425             END IF;
11426 
11427 		  EXCEPTION
11428 	        WHEN OTHERS  THEN
11429                  NULL;
11430  	      END;
11431 
11432 	        --Populate the char5 column with the value retainage_rate that will be used by EBTAX
11433 
11434 	      trans_dists(j).char5 := l_retainage_rate * l_retainage_release_factor ;
11435 
11436 
11437 	      ap_etax_services_pkg.ret_dists(l_inv_dist_list(i).invoice_distribution_id).retainage_rate  :=
11438                                l_retainage_rate *  l_retainage_release_factor;
11439 	      ap_etax_services_pkg.ret_dists(l_inv_dist_list(i).invoice_distribution_id).amount := l_amount;
11440 	      ap_etax_services_pkg.ret_dists(l_inv_dist_list(i).invoice_distribution_id).retained_amount := l_retained_amount;
11441 	      ap_etax_services_pkg.ret_dists(l_inv_dist_list(i).invoice_distribution_id).related_retainage_dist_id :=
11442                                l_inv_dist_list(i).related_retainage_dist_id;
11443           ap_etax_services_pkg.ret_dists(l_inv_dist_list(i).invoice_distribution_id).retained_invoice_dist_id :=
11444                                l_inv_dist_list(i).retained_invoice_dist_id;
11445 	      ap_etax_services_pkg.ret_dists(l_inv_dist_list(i).invoice_distribution_id).quantity_invoiced  := l_quantity_invoiced;
11446 	      ap_etax_services_pkg.ret_dists(l_inv_dist_list(i).invoice_distribution_id).unit_price  := l_unit_price;
11447 
11448 
11449 	     l_debug_info := 'Populating the char4 and char5 value for retainage invoice as  '
11450                          || trans_dists(j).char4 || '  and ' || trans_dists(j).char5;
11451          IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11452              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11453          END IF;
11454 
11455 	    END IF;
11456         --bug10621602 ends
11457 
11458           -- Bug 8624271 Restructing fix 7476261
11459           trans_dists(j).trx_level_type			:= 'LINE';
11460           trans_dists(j).trx_line_dist_id		:= l_inv_dist_list(i).invoice_distribution_id;
11461           trans_dists(j).dist_level_action		:= l_dist_level_action;
11462           trans_dists(j).item_dist_number		:= l_inv_dist_list(i).distribution_line_number;
11463           trans_dists(j).dist_intended_use		:= nvl(l_inv_dist_list(i).intended_use,
11464 		                                               l_intended_use); --8796484
11465 
11466           trans_dists(j).trx_line_dist_amt 		:= l_amount;
11467           trans_dists(j).trx_line_quantity 		:= l_line_quantity_invoiced;
11468           trans_dists(j).trx_line_dist_qty 		:= coalesce(
11469                                                             l_converted_qty,
11470                                                             l_inv_dist_list(i).quantity_invoiced,
11471   							    l_inv_dist_list(i).corrected_quantity,
11472   							    1); --8624271
11473 
11474 	      trans_dists(j).unit_price			:= nvl(l_inv_dist_list(i).unit_price,l_amount);
11475           trans_dists(j).trx_line_dist_date		:= l_inv_dist_list(i).accounting_date;
11476 
11477           trans_dists(j).task_id			:= l_inv_dist_list(i).task_id;
11478           trans_dists(j).award_id			:= l_inv_dist_list(i).award_id;
11479           trans_dists(j).project_id			:= l_inv_dist_list(i).project_id;
11480           trans_dists(j).expenditure_type		:= l_inv_dist_list(i).expenditure_type;
11481           trans_dists(j).expenditure_organization_id	:= l_inv_dist_list(i).expenditure_organization_id;
11482           trans_dists(j).expenditure_item_date		:= l_inv_dist_list(i).expenditure_item_date;
11483           trans_dists(j).account_ccid 			:= l_inv_dist_list(i).dist_code_combination_id;
11484 
11485           trans_dists(j).ref_doc_application_id 	:= l_ref_doc_application_id;
11486           trans_dists(j).ref_doc_entity_code 		:= l_ref_doc_entity_code;
11487           trans_dists(j).ref_doc_event_class_code 	:= l_ref_doc_event_class_code;
11488           trans_dists(j).ref_doc_trx_id 		:= l_ref_doc_trx_id;
11489           trans_dists(j).ref_doc_line_id		:= l_po_line_location_id;
11490 
11491           --Bug14039130
11492 	  --bug14189635, changed l_inv_line_list to l_inv_dist_list
11493           IF l_inv_dist_list(i).line_type_lookup_code NOT IN ('MISCELLANEOUS','FREIGHT') THEN
11494             trans_dists(j).ref_doc_dist_id 		:= l_inv_dist_list(i).po_distribution_id;
11495           ELSE
11496             trans_dists(j).ref_doc_dist_id 		:= NULL;
11497 	      END IF;
11498           --Bug14039130
11499 
11500 
11501           trans_dists(j).ref_doc_trx_level_type 	:= l_ref_doc_trx_level_type;
11502 	      trans_dists(j).ref_doc_trx_line_dist_qty 	:= l_ref_doc_trx_line_dist_qty;
11503 
11504           trans_dists(j).adjusted_doc_dist_id           := l_inv_dist_list(i).corrected_invoice_dist_id;
11505           trans_dists(j).applied_from_dist_id           := l_inv_dist_list(i).prepay_distribution_id;
11506 
11507           /*
11508           trans_dists(j).adjusted_doc_application_id 	:= l_adj_doc_application_id;
11509           trans_dists(j).adjusted_doc_entity_code 	:= l_adj_doc_entity_code;
11510           trans_dists(j).adjusted_doc_event_class_code 	:= l_adj_doc_event_class_code;
11511           trans_dists(j).adjusted_doc_trx_id 		:= l_adj_doc_trx_id;
11512           trans_dists(j).adjusted_doc_line_id 		:= l_adj_doc_line_id;
11513           trans_dists(j).adjusted_doc_dist_id		:= l_inv_dist_list(i).corrected_invoice_dist_id;
11514           trans_dists(j).adjusted_doc_trx_level_type 	:= l_adj_doc_trx_level_type;
11515 
11516           trans_dists(j).applied_from_application_id 	:= l_prepay_doc_application_id;
11517           trans_dists(j).applied_from_entity_code 	:= l_prepay_doc_entity_code;
11518           trans_dists(j).applied_from_event_class_code 	:= l_prepay_doc_event_class_code;
11519           trans_dists(j).applied_from_trx_id 		:= l_applied_from_trx_id;
11520           trans_dists(j).applied_from_line_id 		:= l_applied_from_line_id;
11521        	  trans_dists(j).applied_from_dist_id		:= l_inv_dist_list(i).prepay_distribution_id;
11522           trans_dists(j).applied_from_trx_level_type 	:= l_applied_from_trx_level_type;
11523           */
11524 
11525           trans_dists(j).ref_doc_curr_conv_rate 	:= l_po_header_curr_conv_rate;
11526           trans_dists(j).applied_to_doc_curr_conv_rate 	:= l_receipt_curr_conv_rate;
11527 
11528 
11529            --Bug9363214
11530  	   --l_price_diff				:= l_inv_dist_list(i).unit_price - l_po_unit_price;
11531            --Bug9643462 started using corrected_invoice_dist_id
11532            if ((l_inv_dist_list(i).corrected_invoice_dist_id is not null) and (l_inv_dist_list(i).dist_match_type = 'PRICE_CORRECTION'))then
11533                   l_price_diff				:= l_inv_dist_list(i).unit_price;
11534            else
11535                   l_price_diff				:= l_inv_dist_list(i).unit_price - l_po_unit_price;
11536            end if;
11537            --Bug9363214
11538 	 --Bug 7476261 End
11539 
11540           trans_dists(j).price_diff 			:= l_price_diff;
11541           trans_dists(j).historical_flag		:= NVL(l_inv_dist_list(i).historical_flag, 'N'); -- Bug 7117591
11542 /*NVL(P_Invoice_Header_Rec.historical_flag, 'N');*/
11543 
11544           IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
11545               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11546 				'trans_dists(j).trx_id: '||trans_dists(j).trx_id);
11547               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11548                                 'trans_dists(j).trx_line_id: '||trans_dists(j).trx_line_id);
11549               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11550 				'trans_dists(j).trx_line_dist_amt: '||trans_dists(j).trx_line_dist_amt);
11551               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11552 				'l_ref_doc_application_id: '||l_ref_doc_application_id);
11553               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11554 				'l_ref_doc_entity_code: '   ||l_ref_doc_entity_code);
11555               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11556 				'l_ref_doc_event_class_code: ' ||l_ref_doc_event_class_code);
11557               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11558 				'l_ref_doc_trx_id: '||l_ref_doc_trx_id);
11559               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11560 				'l_po_line_location_id: '||l_po_line_location_id);
11561               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11562 				'l_inv_dist_list(i).po_distribution_id: '||l_inv_dist_list(i).po_distribution_id);
11563               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11564 				'l_adj_doc_application_id: '|| l_adj_doc_application_id);
11565               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11566 				'l_adj_doc_entity_code: '|| l_adj_doc_entity_code);
11567               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11568 				'l_adj_doc_event_class_code: '|| l_adj_doc_event_class_code);
11569               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11570 				'l_adj_doc_trx_id: '|| l_adj_doc_trx_id);
11571               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11572 				'l_adj_doc_line_id: '|| l_adj_doc_line_id);
11573               FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11574 				'l_adj_doc_trx_level_type: '|| l_adj_doc_trx_level_type);
11575 	      FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11576 				'l_prepay_doc_application_id: '||l_prepay_doc_application_id);
11577 	      FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11578 				'l_prepay_doc_entity_code: '||l_prepay_doc_entity_code);
11579 	      FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11580 				'l_prepay_doc_event_class_code: '||l_prepay_doc_event_class_code);
11581 	      FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11582 				'l_applied_from_trx_id: '||l_applied_from_trx_id);
11583 	      FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11584 				'l_applied_from_line_id: '||l_applied_from_line_id);
11585 	      FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11586 				'l_inv_dist_list(i).prepay_distribution_id: '||l_inv_dist_list(i).prepay_distribution_id);
11587 	      FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11588 				'l_applied_from_trx_level_type: '||l_applied_from_trx_level_type);
11589           --Bug12834600
11590           FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11591         		'trx_line_dist_qty : '||trans_dists(j).trx_line_dist_qty);
11592           FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
11593 				'Converted Qty : '||l_converted_qty);
11594           --Bug12834600
11595 
11596           END IF;
11597 
11598           -- Reset the derived values
11599           l_po_line_location_id := Null;
11600 	  l_prepay_inv_id	:= Null;
11601 	  l_prepay_line_num	:= Null;
11602         END IF;
11603       END IF; --Bug9494315
11604       END LOOP;
11605 
11606       -------------------------------------------------------------------
11607       l_debug_info := 'Step 7: Bulk Insert into global temp table';
11608       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11609           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11610       END IF;
11611       -------------------------------------------------------------------
11612       IF (l_return_status = TRUE) THEN
11613 
11614         FORALL m IN trans_dists.FIRST..trans_dists.LAST
11615           INSERT INTO zx_itm_distributions_gt
11616           VALUES trans_dists(m);
11617 
11618       END IF;
11619     END IF;
11620 
11621     RETURN l_return_status;
11622 
11623   EXCEPTION
11624     WHEN OTHERS THEN
11625       IF (SQLCODE <> -20001) THEN
11626         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
11627         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
11628         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
11629         FND_MESSAGE.SET_TOKEN('PARAMETERS',
11630           ' P_Invoice_Header_Rec = '||P_Invoice_Header_Rec.Invoice_Id||
11631           ' P_Calling_Mode ='||P_Calling_Mode||
11632           ' P_Error_Code = '||P_Error_Code||
11633           ' P_Calling_Sequence = '||P_Calling_Sequence);
11634         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
11635       END IF;
11636 
11637       APP_EXCEPTION.RAISE_EXCEPTION;
11638   END Populate_Distributions_GT;
11639 
11640 /*=============================================================================
11641  |  FUNCTION - Update_AP()
11642  |
11643  |  DESCRIPTION
11644  |      This function will handle the return of values from the eTax repository
11645  |      This will be called from all the functions that call the etax services
11646  |      in the case the call is successfull.
11647  |
11648  |  PARAMETERS
11649  |      P_Invoice_header_rec - Invoice header info
11650  |      P_Calling_Mode - calling mode.
11651  |      P_All_Error_Messages - Should API return 1 error message or allow
11652  |                             calling point to get them from message stack
11653  |      P_error_code - Error code to be returned
11654  |      P_calling_sequence -  Calling sequence
11655  |
11656  |  MODIFICATION HISTORY
11657  |    DATE          Author         Action
11658  |    20-OCT-2003   SYIDNER        Created
11659  |
11660  *============================================================================*/
11661 
11662   FUNCTION Update_AP(
11663              P_Invoice_header_rec    IN ap_invoices_all%ROWTYPE,
11664              P_Calling_Mode          IN VARCHAR2,
11665              P_All_Error_Messages    IN VARCHAR2,
11666              P_Error_Code            OUT NOCOPY VARCHAR2,
11667              P_Calling_Sequence      IN VARCHAR2) RETURN BOOLEAN
11668   IS
11669 
11670     l_debug_info                 VARCHAR2(240);
11671     l_curr_calling_sequence      VARCHAR2(4000);
11672     l_return_status              BOOLEAN := TRUE;
11673     l_api_name                   CONSTANT VARCHAR2(30) := 'Update_AP';
11674 
11675     l_err varchar2(2000); --bug10140354
11676   BEGIN
11677 
11678     l_curr_calling_sequence :=
11679       'AP_ETAX_SERVICES_PKG.Update_AP<-'||P_calling_sequence;
11680 
11681     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11682        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_curr_calling_sequence);
11683     END IF;
11684 
11685     ------------------------------------------------------------------
11686     l_debug_info := 'Step 1: Calling_Mode is:'||P_Calling_Mode;
11687     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11688        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11689     END IF;
11690     ------------------------------------------------------------------
11691 
11692     IF (P_Calling_Mode IN ('CALCULATE', 'CALCULATE IMPORT',
11693                            'OVERRIDE TAX', 'IMPORT INTERFACE',
11694                            'APPLY PREPAY', 'UNAPPLY PREPAY')) THEN
11695       -------------------------------------------------------------------
11696       l_debug_info := 'Step 2: Calling Return_Tax_Lines ';
11697       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11698          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11699       END IF;
11700       -------------------------------------------------------------------
11701       IF NOT (AP_ETAX_UTILITY_PKG.Return_Tax_Lines(
11702                 P_Invoice_Header_Rec => P_invoice_header_rec,
11703                 P_Error_Code         => P_Error_Code,
11704                 P_Calling_Sequence   => l_curr_calling_sequence)) THEN
11705 
11706         l_return_status :=  FALSE;
11707       END IF;
11708 
11709 
11710     ELSIF (P_Calling_Mode IN ('DISTRIBUTE', 'DISTRIBUTE RECOUP', 'OVERRIDE RECOVERY','DISTRIBUTE IMPORT')) THEN
11711         /*  for bug  6010950 added 'DISTRIBUTE IMPORT' to create the tax distributions while import itself for
11712               tax only invoices. */
11713       -------------------------------------------------------------------
11714       l_debug_info := 'Step 3: Calling Return_Tax_Distributions ';
11715       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11716          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11717       END IF;
11718       -------------------------------------------------------------------
11719       IF NOT (AP_ETAX_UTILITY_PKG.Return_Tax_Distributions(
11720                 P_Invoice_Header_Rec => P_invoice_header_rec,
11721                 P_All_Error_Messages => P_All_Error_Messages,
11725         l_return_status := FALSE;
11722                 P_Error_Code         => P_Error_Code,
11723                 P_Calling_Sequence   => l_curr_calling_sequence)) THEN
11724 
11726       END IF;
11727 
11728     ELSIF (P_Calling_Mode = 'CALCULATE QUOTE') THEN
11729       -------------------------------------------------------------------
11730       l_debug_info := 'Step 4: Calling Return_Tax_Quote ';
11731       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11732          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11733       END IF;
11734       -------------------------------------------------------------------
11735       IF NOT (AP_ETAX_UTILITY_PKG.Return_Tax_Quote(
11736                 P_Invoice_Header_Rec => P_invoice_header_rec,
11737                 P_Error_Code         => P_Error_Code,
11738                 P_Calling_Sequence   => l_curr_calling_sequence)) THEN
11739 
11740 
11741         l_return_status := FALSE;
11742       END IF;
11743     --Bug8604959: Added p_calling_mode 'DELETE TAX LINE'
11744     ELSIF (P_Calling_Mode in ('REVERSE INVOICE','DELETE TAX LINE')) THEN
11745       -------------------------------------------------------------------
11746       l_debug_info := 'Step 5: Calling Return_Tax_Lines for '||
11747                       'REVERSE INVOICE';
11748       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11749          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11750       END IF;
11751       -------------------------------------------------------------------
11752       IF NOT (AP_ETAX_UTILITY_PKG.Return_Tax_Lines(
11753                 P_Invoice_Header_Rec => P_invoice_header_rec,
11754                 P_Error_Code         => P_Error_Code,
11755                 P_Calling_Sequence   => l_curr_calling_sequence)) THEN
11756 
11757         l_return_status := FALSE;
11758       END IF;
11759 
11760       -------------------------------------------------------------------
11761       l_debug_info := 'Step 6: Calling Return_Tax_Distributions for '||
11762                       'REVERSE INVOICE';
11763       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11764          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11765       END IF;
11766       -------------------------------------------------------------------
11767       IF NOT (AP_ETAX_UTILITY_PKG.Return_Tax_Distributions(
11768                 P_Invoice_Header_Rec => P_invoice_header_rec,
11769                 P_All_Error_Messages => P_All_Error_Messages,
11770                 P_Error_Code         => P_Error_Code,
11771                 P_Calling_Sequence   => l_curr_calling_sequence)) THEN
11772 
11773         l_return_status := FALSE;
11774       END IF;
11775 
11776     END IF;
11777 
11778     RETURN l_return_status;
11779 
11780   EXCEPTION
11781     WHEN OTHERS THEN
11782       IF (SQLCODE <> -20001) THEN
11783         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
11784         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
11785         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
11786         FND_MESSAGE.SET_TOKEN('PARAMETERS',
11787           ' P_Invoice_Id = '||P_Invoice_header_rec.Invoice_Id||
11788           ' P_Calling_Mode ='||P_Calling_Mode||
11789           ' P_Error_Code = '||P_Error_Code||
11790           ' P_Calling_Sequence = '||P_Calling_Sequence);
11791         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
11792       END IF;
11793 
11794      --bug10140354
11795      IF AP_APPROVAL_PKG.G_VALIDATION_REQUEST_ID IS NULL THEN
11796                   APP_EXCEPTION.RAISE_EXCEPTION;
11797                ELSE
11798 	       l_err:= SQLERRM;
11799                 insert into ap_errors_gt(invoice_id , message_text) values(P_Invoice_Header_Rec.Invoice_id , l_err);
11800                   RETURN FALSE;
11801                END IF;
11802 
11803   END Update_AP;
11804 
11805 /*=============================================================================
11806  |  FUNCTION - Calculate_Quote ()
11807  |
11808  |  DESCRIPTION
11809  |      This function will return the tax amount and indicate if it is inclusive.
11810  |      This will be called from the recurring invoices form. This is a special
11811  |      case, as the invoices for which the tax is to be calculated are not yet
11812  |      saved to the database and eBTax global temporary tables are populated
11813  |      based on the parameter p_invoice_header_rec. A psuedo-line is inserted
11814  |      into the GTT and removed after the tax amount is calculated.
11815  |
11816  |  PARAMETERS
11817  |      P_Invoice_Header_Rec 	- Invoice header info
11818  |      P_Invoice_Lines_Rec	- Invoice lines info
11819  |      P_Calling_Mode 		- Calling mode. (CALCULATE_QUOTE)
11820  |      P_All_Error_Messages 	- Should API return 1 error message or allow
11821  |                                calling point to get them from message stack
11822  |      P_error_code 		- Error code to be returned
11823  |      P_calling_sequence 	- Calling sequence
11824  |
11825  |  MODIFICATION HISTORY
11826  |    DATE          Author         Action
11827  |    13-AUG-2004   Sanjay         Created
11828  *============================================================================*/
11829 
11830   FUNCTION CALCULATE_QUOTE(
11831              P_Invoice_Header_Rec      	IN  ap_invoices_all%ROWTYPE,
11832              P_Invoice_Lines_Rec	    IN  ap_invoice_lines_all%ROWTYPE,
11833              P_Calling_Mode            	IN  VARCHAR2,
11834 	         P_Tax_Amount		        OUT NOCOPY NUMBER,
11835 	         P_Tax_Amt_Included		    OUT NOCOPY VARCHAR2,
11836              P_Error_Code              	OUT NOCOPY VARCHAR2,
11837              P_Calling_Sequence         IN  VARCHAR2) RETURN BOOLEAN
11838   IS
11839 
11840     l_debug_info                 	VARCHAR2(240);
11841     l_curr_calling_sequence      	VARCHAR2(4000);
11842 
11843     l_tax_already_calculated     	VARCHAR2(1);
11844 
11845     l_event_class_code           	zx_trx_headers_gt.event_class_code%TYPE;
11846     l_event_type_code            	zx_trx_headers_gt.event_type_code%TYPE;
11847 
11848     l_location_id			zx_transaction_lines_gt.ship_from_location_id%type;
11849     l_bill_to_location_id          	zx_transaction_lines_gt.bill_to_location_id%TYPE;
11850     l_fob_point                  	po_vendor_sites_all.fob_lookup_code%TYPE;
11851     l_po_line_location_id		ap_invoice_lines_all.po_line_location_id%TYPE;
11852 
11853     l_ctrl_hdr_tx_appl_flag         	zx_transaction_lines_gt.ctrl_hdr_tx_appl_flag%TYPE;
11854     l_line_level_action            	zx_transaction_lines_gt.line_level_action%TYPE;
11855     l_line_amt_includes_tax_flag	zx_transaction_lines_gt.line_amt_includes_tax_flag%TYPE;
11856     l_product_org_id               	zx_transaction_lines_gt.product_org_id%TYPE;
11857     l_uom_code                          mtl_units_of_measure.uom_code%TYPE;
11858 
11859     -- Variables for PO doc info
11860     l_ref_doc_application_id		zx_transaction_lines_gt.ref_doc_application_id%TYPE;
11861     l_ref_doc_entity_code		zx_transaction_lines_gt.ref_doc_entity_code%TYPE;
11862     l_ref_doc_event_class_code		zx_transaction_lines_gt.ref_doc_event_class_code%TYPE;
11863     l_ref_doc_line_quantity		zx_transaction_lines_gt.ref_doc_line_quantity%TYPE;
11864     l_ref_doc_trx_level_type		zx_transaction_lines_gt.ref_doc_trx_level_type%TYPE;
11865     l_po_header_curr_conv_rate		po_headers_all.rate%TYPE;
11869 
11866     l_dummy				number;
11867 
11868     l_ref_doc_trx_id			zx_transaction_lines_gt.ref_doc_trx_id%TYPE;
11870     l_return_status              	BOOLEAN := TRUE;
11871     l_return_status_service       	VARCHAR2(4000);
11872     l_msg_count                   	NUMBER;
11873     l_msg_data                    	VARCHAR2(4000);
11874     l_api_name              	        CONSTANT VARCHAR2(200) := 'Calculate_Quote';
11875 
11876     CURSOR c_tax_amount IS
11877     SELECT SUM(NVL(zdl.tax_amt,0))
11878     FROM   zx_detail_tax_lines_gt zdl
11879     WHERE  zdl.application_id = 200
11880     AND    zdl.entity_code    = 'AP_INVOICES'
11881     AND    zdl.trx_id         = P_Invoice_Lines_Rec.invoice_id
11882     AND    NVL(zdl.self_assessed_flag,  'N') = 'N'
11883     AND    NVL(zdl.reporting_only_flag, 'N') = 'N';
11884 
11885   BEGIN
11886 
11887     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.CALCULATE_QUOTE<-' || P_calling_sequence;
11888 
11889     IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
11890       FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_ETAX_SERVICES_PKG.Calculate_Quote (+)');
11891     END IF;
11892 
11893     ----------------------------------------------------------------------
11894     l_debug_info := 'Get location_id for vendor site';
11895     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11896       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11897     END IF;
11898     ----------------------------------------------------------------------
11899     BEGIN
11900       SELECT location_id, fob_lookup_code
11901         INTO l_location_id, l_fob_point
11902         FROM ap_supplier_sites_all
11903        WHERE vendor_site_id = P_Invoice_Header_Rec.vendor_site_id;
11904 
11905     EXCEPTION
11906       WHEN no_data_found THEN
11907          l_location_id := null;
11908          l_fob_point   := null;
11909     END;
11910     ----------------------------------------------------------------------
11911     l_debug_info := 'Location_id for vendor site' || l_location_id ||'& '||l_fob_point;
11912     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11913       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11914     END IF;
11915     ----------------------------------------------------------------------
11916     -------------------------------------------------------------------
11917     l_debug_info := 'Is tax already called invoice level?';
11918     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11919       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11920     END IF;
11921     -------------------------------------------------------------------
11922     IF (AP_ETAX_UTILITY_PKG.Is_Tax_Already_Calc_Inv(
11923           P_Invoice_Id           => p_invoice_header_rec.invoice_id,
11924           P_Calling_Sequence     => l_curr_calling_sequence)) THEN
11925 
11926       l_tax_already_calculated := 'Y';
11927     ELSE
11928       l_tax_already_calculated := 'N';
11929 
11930     END IF;
11931 
11932     -------------------------------------------------------------------------
11933     l_debug_info := 'Step 1: Call AP_ETAX_SERVICES_PKG.Populate_Headers_GT';
11934     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11935         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11936     END IF;
11937     -------------------------------------------------------------------------
11938     IF NOT(AP_ETAX_SERVICES_PKG.Populate_Headers_GT(
11939 		      P_Invoice_Header_Rec         => P_Invoice_Header_Rec,
11940 		      P_Calling_Mode               => P_Calling_Mode,
11941 		      P_eTax_Already_called_flag   => l_tax_already_calculated,
11942 		      P_Event_Class_Code           => l_event_class_code,
11943 		      P_Event_Type_Code            => l_event_type_code,
11944 		      P_Error_Code                 => P_error_code,
11945 		      P_Calling_Sequence           => l_curr_calling_sequence )) THEN
11946 
11947       l_return_status := FALSE;
11948 
11949     END IF;
11950 
11951     ------------------------------------------------------------------------
11952     l_debug_info := 'Step 2: Get location_id for org_id';
11953     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11954         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11955     END IF;
11956     ------------------------------------------------------------------------
11957     BEGIN
11958       SELECT location_id
11959         INTO l_bill_to_location_id
11960         FROM hr_all_organization_units
11961        WHERE organization_id = P_Invoice_Header_Rec.org_id;
11962 
11963     EXCEPTION
11964       WHEN no_data_found THEN
11965          l_bill_to_location_id := null;
11966     END;
11967     ------------------------------------------------------------------------
11968     l_debug_info := 'Location_id for org_id '||P_Invoice_Header_Rec.org_id||'& '||l_bill_to_location_id;
11969     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11970         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11971     END IF;
11972     ------------------------------------------------------------------------
11973     ------------------------------------------------------------------------
11974     l_debug_info := 'Step 4: Go through taxable lines';
11975     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11976         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11977     END IF;
11978     ------------------------------------------------------------------------
11979     IF ( P_Invoice_Lines_Rec.invoice_id IS NOT NULL ) THEN
11980 
11981         --------------------------------------------------------------------
11982          l_debug_info := 'Step 5: Get line_level_action for line number: '||
11983 			  P_Invoice_Lines_Rec.line_number;
11984          IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11985            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11986          END IF;
11987         --------------------------------------------------------------------
11988         IF (l_return_status = TRUE) THEN
11989 
11990          l_line_level_action := 'CREATE';
11991 
11992          -------------------------------------------------------------------
11993           l_debug_info := 'Step 6: Get Additional PO matched  info ';
11994           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
11995              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
11996           END IF;
11997          -------------------------------------------------------------------
11998 
11999          IF ( P_Invoice_Lines_Rec.po_line_location_id IS NOT NULL) THEN
12000 
12001 	   l_po_line_location_id := P_Invoice_Lines_Rec.po_line_location_id;
12002 
12003            IF NOT (AP_ETAX_UTILITY_PKG.Get_PO_Info(
12004               P_Po_Line_Location_Id         => l_po_line_location_id,
12005               P_PO_Distribution_Id          => null,
12006               P_Application_Id              => l_ref_doc_application_id,
12007               P_Entity_code                 => l_ref_doc_entity_code,
12008               P_Event_Class_Code            => l_ref_doc_event_class_code,
12009               P_PO_Quantity                 => l_ref_doc_line_quantity,
12010               P_Product_Org_Id              => l_product_org_id,
12011               P_Po_Header_Id                => l_ref_doc_trx_id,
12012               P_Po_Header_curr_conv_rate    => l_po_header_curr_conv_rate,
12013               P_Uom_Code                    => l_uom_code,
12014               P_Dist_Qty                    => l_dummy,
12015               P_Ship_Price                  => l_dummy,
12016               P_Error_Code                  => P_error_code,
12017               P_Calling_Sequence            => l_curr_calling_sequence)) THEN
12018 
12019              l_return_status := FALSE;
12020            END IF;
12021 
12022 	   l_ref_doc_trx_level_type := 'SHIPMENT';
12023 
12024          END IF;
12025 
12026          -------------------------------------------------------------------
12027           l_debug_info := 'Step 7: Get line_amt_includes_tax_flag';
12028 	      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12029              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
12030           END IF;
12031          -------------------------------------------------------------------
12032 
12033          IF (P_Invoice_Lines_Rec.po_line_location_id IS NOT NULL) THEN
12034            -- NONE
12035            l_line_amt_includes_tax_flag := 'N';
12036 
12037          ELSE
12038            IF (p_calling_mode = 'CALCULATE QUOTE') THEN
12039 
12040 	     -- Refer eTax bug 3819487 for the value of line_amt_includes_tax_flag
12041 	     -- that AP should be passing.
12042 
12043              l_line_amt_includes_tax_flag := 'I';
12044 
12045            ELSE
12046              -- STANDARD
12047              l_line_amt_includes_tax_flag := 'S';
12048 
12049            END IF;
12050          END IF;
12051           -------------------------------------------------------------------
12052           l_debug_info := 'Line_Amt_Includes_Tax_Flag '||l_line_amt_includes_tax_flag;
12053 	      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12054             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
12055           END IF;
12056          -------------------------------------------------------------------
12057          -------------------------------------------------------------------
12058           l_debug_info := 'Step 8: Get ctrl_hdr_tx_appl_flag';
12059 	      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12060             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
12061           END IF;
12062          -------------------------------------------------------------------
12063          IF P_Invoice_Header_Rec.control_amount IS NOT NULL THEN
12064            l_ctrl_hdr_tx_appl_flag := 'Y';
12065          ELSE
12066            l_ctrl_hdr_tx_appl_flag := 'N';
12067          END IF;
12068 
12069        END IF;
12070     END IF;
12071 
12072     ------------------------------------------------------------------------
12073     l_debug_info := 'Step 9: Insert into zx_transaction_lines_gt';
12074     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12075         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
12076     END IF;
12077     ------------------------------------------------------------------------
12078     IF ( l_return_status = TRUE ) THEN
12079 
12080 	INSERT INTO zx_transaction_lines_gt
12081 	(
12082 	 application_id,
12083 	 entity_code,
12084 	 event_class_code,
12085 	 trx_id,
12086 	 trx_level_type,
12087 	 trx_line_id,
12088 	 line_level_action,
12089 	 trx_line_type,
12090 	 trx_line_date,
12091 	 trx_business_category,
12092 	 line_intended_use,
12093 	 user_defined_fisc_class,
12094 	 line_amt,
12095 	 trx_line_quantity,
12096 	 unit_price,
12097 	 product_id,
12098 	 product_fisc_classification,
12099 	 product_org_id,
12100 	 uom_code,
12101 	 product_type,
12102 	 product_category,
12103 	 fob_point,
12104 	 ship_to_party_id,
12105 	 ship_from_party_id,
12106 	 bill_to_party_id,
12107 	 bill_from_party_id,
12108 	 ship_from_party_site_id,
12109 	 bill_from_party_site_id,
12110 	 ship_to_location_id,
12111      ship_from_location_id,
12112 	 bill_to_location_id,
12113 	 bill_from_location_id,
12114 	 account_ccid,
12115 	 merchant_party_country,
12116 	 ref_doc_application_id,
12117 	 ref_doc_entity_code,
12118 	 ref_doc_event_class_code,
12119 	 ref_doc_trx_id,
12120 	 ref_doc_line_id,
12121 	 ref_doc_line_quantity,
12122      ref_doc_trx_level_type,
12123 	 --applied_to_trx_line_id,
12124 	 trx_line_number,
12125 	 trx_line_description,
12126 	 product_description,
12127 	 trx_line_gl_date,
12128 	 merchant_party_name,
12129 	 merchant_party_document_number,
12130 	 merchant_party_reference,
12131 	 merchant_party_taxpayer_id,
12132 	 merchant_party_tax_reg_number,
12133 	 assessable_value,
12134 	 line_amt_includes_tax_flag,
12135 	 historical_flag,
12136 	 ctrl_hdr_tx_appl_flag,
12137 	 ctrl_total_line_tx_amt,
12138 	 input_tax_classification_code
12139 	)
12140 	VALUES
12141 	(
12142 	 200,
12143 	 'AP_INVOICES',
12144 	 l_event_class_code,
12145 	 P_Invoice_Lines_Rec.invoice_id,
12146 	 'LINE',
12147 	 P_Invoice_Lines_Rec.line_number,
12148 	 l_line_level_action,
12149 	 P_Invoice_Lines_Rec.line_type_lookup_code,
12150 	 P_Invoice_Header_Rec.invoice_date,
12151 	 P_Invoice_Lines_Rec.trx_business_category,
12152 	 P_Invoice_Lines_Rec.primary_intended_use,
12153 	 P_Invoice_Lines_Rec.user_defined_fisc_class,
12154 	 P_Invoice_Lines_Rec.amount,
12155 	 P_Invoice_Lines_Rec.quantity_invoiced,
12156 	 P_Invoice_Lines_Rec.unit_price,
12157 	 P_Invoice_Lines_Rec.inventory_item_id,
12158 	 P_Invoice_Lines_Rec.product_fisc_classification,
12159 	 l_product_org_id,
12160 	 P_Invoice_Lines_Rec.unit_meas_lookup_code,
12161 	 P_Invoice_Lines_Rec.product_type,
12162 	 P_Invoice_Lines_Rec.product_category,
12163 	 l_fob_point,
12164 	 P_Invoice_Lines_Rec.org_id,
12165 	 P_Invoice_Header_Rec.party_id,
12166 	 P_Invoice_Lines_Rec.org_id,
12167 	 P_Invoice_Header_Rec.party_id,
12168 	 P_Invoice_Header_Rec.party_site_id,
12169 	 P_Invoice_Header_Rec.party_site_id,
12170 	 P_Invoice_Lines_Rec.ship_to_location_id,
12171      l_location_id,
12172 	 l_bill_to_location_id,
12173      l_location_id,
12174 	 P_Invoice_Lines_Rec.default_dist_ccid,
12175 	 P_Invoice_Lines_Rec.country_of_supply,
12176 	 l_ref_doc_application_id,
12177 	 l_ref_doc_entity_code,
12178 	 l_ref_doc_event_class_code,
12179 	 l_ref_doc_trx_id,
12180          --Bug5680407 corrected the wrong ordering of below
12181          --3 coulmns
12182 	 P_Invoice_Lines_Rec.po_line_location_id,
12183 	 l_ref_doc_line_quantity,
12184      l_ref_doc_trx_level_type,
12185 	 P_Invoice_Lines_Rec.line_number,
12186 	 P_Invoice_Lines_Rec.description,
12187 	 P_Invoice_Lines_Rec.item_description,
12188 	 P_Invoice_Lines_Rec.accounting_date,
12189 	 P_Invoice_Lines_Rec.merchant_name,
12190 	 P_Invoice_Lines_Rec.merchant_document_number,
12194 	 P_Invoice_Lines_Rec.assessable_value,
12191 	 P_Invoice_Lines_Rec.merchant_reference,
12192 	 P_Invoice_Lines_Rec.merchant_taxpayer_id,
12193 	 P_Invoice_Lines_Rec.merchant_tax_reg_number,
12195 	 l_line_amt_includes_tax_flag,
12196 	 NVL(P_Invoice_Header_Rec.historical_flag, 'N'),
12197 	 l_ctrl_hdr_tx_appl_flag,
12198 	 P_Invoice_Lines_Rec.control_amount,
12199 	 P_Invoice_Lines_Rec.tax_classification_code
12200 	);
12201 
12202     END IF;
12203 
12204     IF ( l_return_status = TRUE ) THEN
12205 
12206        -----------------------------------------------------------------
12207        l_debug_info := 'Step 10: Call Calculate_Tax service';
12208        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12209         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
12210        END IF;
12211        -----------------------------------------------------------------
12212 
12213        zx_api_pub.calculate_tax(
12214                  p_api_version      => 1.0,
12215                  p_init_msg_list    => FND_API.G_TRUE,
12216                  p_commit           => FND_API.G_FALSE,
12217                  p_validation_level => FND_API.G_VALID_LEVEL_FULL,
12218                  x_return_status    => l_return_status_service,
12219                  x_msg_count        => l_msg_count,
12220                  x_msg_data         => l_msg_data);
12221 
12222     END IF;
12223 
12224     IF (l_return_status_service = 'S') THEN
12225 
12226        OPEN  c_tax_amount;
12227        FETCH c_tax_amount
12228        INTO  p_tax_amount;
12229        CLOSE c_tax_amount;
12230 
12231        IF p_tax_amount IS NOT NULL THEN
12232 
12233            -----------------------------------------------------------------
12234            l_debug_info := 'Step 11: Get tax inclusive/exclusive flag';
12235            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12236               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
12237            END IF;
12238            -----------------------------------------------------------------
12239 
12240 	   SELECT distinct zdl.tax_amt_included_flag
12241            INTO   P_Tax_Amt_Included
12242            FROM   zx_detail_tax_lines_gt zdl
12243            WHERE  zdl.application_id	= 200
12244            AND    zdl.entity_code	= 'AP_INVOICES'
12245            AND    zdl.trx_id 		= P_Invoice_Lines_Rec.invoice_id
12246            AND    NVL(zdl.self_assessed_flag,  'N') = 'N'
12247            AND    NVL(zdl.reporting_only_flag, 'N') = 'N';
12248 
12249 	END IF;
12250 
12251     ELSE  -- handle errors
12252 
12253        -----------------------------------------------------------------
12254        l_debug_info := 'Step 12: Calculate_Tax service returns error';
12255        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12256          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
12257        END IF;
12258        -----------------------------------------------------------------
12259 
12260       l_return_status := FALSE;
12261 
12262       IF l_msg_count = 1 THEN
12263 
12264          P_Error_Code := FND_MSG_PUB.Get;
12265 
12266       ELSIF l_msg_count > 1 THEN
12267          LOOP
12268              P_Error_Code := FND_MSG_PUB.Get;
12269 
12270              IF P_Error_Code IS NULL THEN
12271 	        EXIT;
12272 	     END IF;
12273 
12274          END LOOP;
12275       END IF;
12276 
12277     END IF;
12278 
12279     -----------------------------------------------------------------
12280     l_debug_info := 'Step 13: Delete eTax Global Temporary Tables';
12281     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12282         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
12283     END IF;
12284     -----------------------------------------------------------------
12285     BEGIN DELETE zx_trx_headers_gt;
12286     EXCEPTION WHEN NO_DATA_FOUND THEN null;
12287     END;
12288 
12289     BEGIN DELETE zx_transaction_lines_gt;
12290     EXCEPTION WHEN NO_DATA_FOUND THEN null;
12291     END;
12292 
12293     RETURN l_return_status;
12294 
12295   EXCEPTION
12296     WHEN OTHERS THEN
12297       IF (SQLCODE <> -20001) THEN
12298 
12299         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
12300         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
12301         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
12302         FND_MESSAGE.SET_TOKEN('PARAMETERS',
12303           ' P_Invoice_Header_Rec.Invoice_Id = '||P_Invoice_Header_Rec.Invoice_Id||
12304           ' P_Calling_Mode ='||P_Calling_Mode||
12305           ' P_Error_Code = '||P_Error_Code);
12306         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
12307       END IF;
12308 
12309       APP_EXCEPTION.RAISE_EXCEPTION;
12310 
12311   END CALCULATE_QUOTE;
12312 
12313 PROCEDURE get_po_tax_attributes
12314 			(
12315 			 p_application_id		IN  NUMBER,
12316 			 p_org_id			IN  NUMBER,
12317 			 p_entity_code			IN  VARCHAR2,
12318 			 p_event_class_code		IN  VARCHAR2,
12319 			 p_trx_level_type		IN  VARCHAR2,
12320 			 p_trx_id			IN  NUMBER,
12321 			 p_trx_line_id			IN  NUMBER,
12322 			 x_line_intended_use		OUT NOCOPY VARCHAR2,
12323 			 x_product_type			OUT NOCOPY VARCHAR2,
12324 			 x_product_category		OUT NOCOPY VARCHAR2,
12325 			 x_product_fisc_classification	OUT NOCOPY VARCHAR2,
12326 			 x_user_defined_fisc_class	OUT NOCOPY VARCHAR2,
12327 			 x_assessable_value		OUT NOCOPY NUMBER,
12328 			 x_tax_classification_code	OUT NOCOPY VARCHAR2
12329 			) IS
12330 
12331 	CURSOR  c_po_default IS
12332         SELECT  line_intended_use,
12333                 product_type,
12334                 product_category,
12335                 product_fisc_classification,
12336                 user_defined_fisc_class,
12337                 assessable_value,
12338 		input_tax_classification_code
12339         FROM	zx_lines_det_factors
12340         WHERE	application_id           = p_application_id
12341         AND	internal_organization_id = p_org_id
12342         AND	entity_code              = p_entity_code
12343         AND 	event_class_code         = p_event_class_code
12344         AND	trx_level_type           = p_trx_level_type
12345         AND	trx_id                   = p_trx_id
12346         AND	trx_line_id              = p_trx_line_id;
12347 
12348 	l_po_default	c_po_default%rowtype;
12349     l_api_name              	        CONSTANT VARCHAR2(200) := 'get_po_tax_attributes';
12350 
12351 BEGIN
12352 
12353      Open  c_po_default;
12354      Fetch c_po_default
12355      Into  l_po_default;
12356      Close c_po_default;
12357 
12358      x_line_intended_use		:= l_po_default.line_intended_use;
12359      x_product_type			:= l_po_default.product_type;
12360      x_product_category			:= l_po_default.product_category;
12361      x_product_fisc_classification	:= l_po_default.product_fisc_classification;
12362      x_user_defined_fisc_class		:= l_po_default.user_defined_fisc_class;
12363      x_assessable_value			:= l_po_default.assessable_value;
12364      x_tax_classification_code		:= l_po_default.input_tax_classification_code;
12365 
12366      -- Need to call on-the-fly po upgrade api when no rows are returned.
12367 
12368      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12369          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'PO Default values ');
12370          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'x_line_intended_use: ' || x_line_intended_use);
12371          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'x_product_type: '           || x_product_type);
12372          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'x_product_category: '      || x_product_category);
12373          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'x_product_fisc_classification: '   || x_product_fisc_classification);
12374          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'x_user_defined_fisc_class: '   || x_user_defined_fisc_class);
12375          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'x_assessable_value: '   || x_assessable_value);
12376          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'x_tax_classification_code: '    || x_tax_classification_code);
12377      END IF;
12378 
12379 EXCEPTION
12380       WHEN OTHERS THEN
12381            Null;
12382 
12383 END get_po_tax_attributes;
12384 
12385 -- bug 8495005 fix starts
12386 PROCEDURE get_po_tax_attributes
12387 			(
12388 			 p_application_id		IN  NUMBER,
12389 			 p_org_id			IN  NUMBER,
12390 			 p_entity_code			IN  VARCHAR2,
12391 			 p_event_class_code		IN  VARCHAR2,
12392 			 p_trx_level_type		IN  VARCHAR2,
12393 			 p_trx_id			IN  NUMBER,
12394 			 p_trx_line_id			IN  NUMBER,
12395 			 x_line_intended_use		OUT NOCOPY VARCHAR2,
12396 			 x_product_type			OUT NOCOPY VARCHAR2,
12397 			 x_product_category		OUT NOCOPY VARCHAR2,
12398 			 x_product_fisc_classification	OUT NOCOPY VARCHAR2,
12399 			 x_user_defined_fisc_class	OUT NOCOPY VARCHAR2,
12400 			 x_assessable_value		OUT NOCOPY NUMBER,
12401 			 x_tax_classification_code	OUT NOCOPY VARCHAR2,
12402 			 x_taxation_country		OUT NOCOPY VARCHAR2,
12403 			 x_trx_biz_category		OUT NOCOPY VARCHAR2
12404 			) IS
12405 
12406 	CURSOR  c_po_default IS
12407         SELECT  line_intended_use,
12408                 product_type,
12409                 product_category,
12410                 product_fisc_classification,
12411                 user_defined_fisc_class,
12412                 assessable_value,
12413 		input_tax_classification_code,
12414 		default_taxation_country,
12415 		trx_business_category
12416         FROM	 zx_lines_det_factors
12417         WHERE	application_id           = p_application_id
12418         AND	internal_organization_id = p_org_id
12419         AND	entity_code              = p_entity_code
12420         AND 	event_class_code         = p_event_class_code
12421         AND	trx_level_type           = p_trx_level_type
12422         AND	trx_id                   = p_trx_id
12423         AND	trx_line_id              = p_trx_line_id;
12424 
12425 	l_po_default	c_po_default%rowtype;
12426     l_api_name              	        CONSTANT VARCHAR2(200) := 'get_po_tax_attributes';
12427 
12428 BEGIN
12429 
12430      Open  c_po_default;
12431      Fetch c_po_default
12432      Into  l_po_default;
12433      Close c_po_default;
12434 
12435      x_line_intended_use		:= l_po_default.line_intended_use;
12436      x_product_type			:= l_po_default.product_type;
12440      x_assessable_value			:= l_po_default.assessable_value;
12437      x_product_category			:= l_po_default.product_category;
12438      x_product_fisc_classification	:= l_po_default.product_fisc_classification;
12439      x_user_defined_fisc_class		:= l_po_default.user_defined_fisc_class;
12441      x_tax_classification_code		:= l_po_default.input_tax_classification_code;
12442      x_taxation_country			:= l_po_default.default_taxation_country;
12443      x_trx_biz_category			:= l_po_default.trx_business_category;
12444 
12445 
12446      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
12447          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'PO Default values ');
12448          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'x_line_intended_use: ' || x_line_intended_use);
12449          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'x_product_type: '           || x_product_type);
12450          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'x_product_category: '      || x_product_category);
12451          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'x_product_fisc_classification: '   || x_product_fisc_classification);
12452          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'x_user_defined_fisc_class: '   || x_user_defined_fisc_class);
12453          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'x_assessable_value: '   || x_assessable_value);
12454          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'x_tax_classification_code: '    || x_tax_classification_code);
12455          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'x_taxation_country: '    || x_taxation_country);
12456          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'x_trx_biz_category: '    || x_trx_biz_category);
12457      END IF;
12458 
12459      -- Need to call on-the-fly po upgrade api when no rows are returned.
12460 
12461 EXCEPTION
12462       WHEN OTHERS THEN
12463            Null;
12464 
12465 END get_po_tax_attributes;
12466 -- bug 8495005 ends
12467 
12468 FUNCTION CANCEL_INVOICE
12469 		(P_Invoice_Id   IN NUMBER,
12470 		 P_Line_Number  IN NUMBER DEFAULT NULL,
12471                  P_Calling_Mode IN VARCHAR2) RETURN BOOLEAN IS
12472 
12473     l_debug_info		VARCHAR2(240);
12474     l_curr_calling_sequence	VARCHAR2(4000);
12475     l_api_name			CONSTANT VARCHAR2(100) := 'Cancel_Invoice';
12476 
12477     l_return_status              	BOOLEAN := TRUE;
12478     l_self_assess_tax_dist_exist        BOOLEAN := FALSE ; -- Bug 6694536
12479     l_tax_distributions_exist           BOOLEAN := FALSE ; -- Bug 6694536
12480     l_return_status_service       	VARCHAR2(4000);
12481     l_msg_count                   	NUMBER;
12482     l_msg_data                    	VARCHAR2(4000);
12483     l_inv_cancel_date                   DATE ;  --Bug 8350132
12484     l_open_gl_date                      date;          --bug 13101979
12485     l_open_gl_period                    varchar2(100); --bug 13101979
12486 
12487     cursor c_reverse_tax_dist is
12488 	  select
12489             nvl(item_dist.accounting_date,
12490 	        zx_dist.gl_date)                        accounting_date, --Bug 8350132
12491             'N'						accrual_posted_flag,
12492             'U'						assets_addition_flag,
12493             tax_dist.assets_tracking_flag		assets_tracking_flag, --Bug14772756
12494             'N'						cash_posted_flag,
12495             AP_INVOICE_LINES_PKG.get_max_dist_line_num(
12496 		              p_invoice_id,
12497 		              tax_dist.invoice_line_number)+1
12498 							distribution_line_number,
12499             tax_dist.dist_code_combination_id		dist_code_combination_id,
12500             tax_dist.invoice_id				invoice_id,
12501             l_user_id					last_updated_by,
12502             l_sysdate					last_update_date,
12503             tax_dist.line_type_lookup_code		line_type_lookup_code,
12504             ap_utilities_pkg.get_gl_period_name(
12505                               zx_dist.gl_date,
12506                               tax_dist.org_id)          period_name,
12507             tax_dist.set_of_books_id			set_of_books_id,
12508             (-tax_dist.amount)				amount,
12509             (-tax_dist.base_amount)			base_amount,
12510             --P_Invoice_Header_Rec.batch_id		batch_id,
12511             l_user_id					created_by,
12512             l_sysdate					creation_date,
12513             tax_dist.description			description,
12514             NULL					final_match_flag,
12515             tax_dist.income_tax_region			income_tax_region,
12516             l_user_id					last_update_login,
12517             NULL					match_status_flag,
12518             'N'						posted_flag,
12519             tax_dist.po_distribution_id			po_distribution_id,
12520             NULL					program_application_id,
12521             NULL					program_id,
12522             NULL					program_update_date,
12523             NULL					quantity_invoiced,
12524             NULL					request_id,
12525             'Y'						reversal_flag,
12526             tax_dist.type_1099				type_1099,
12527             tax_dist.unit_price				unit_price,
12528             DECODE(tax_dist.encumbered_flag,
12529 		   'R', 'R', 'N')   			encumbered_flag,    --Bug 8733916
12530             NULL					stat_amount,
12531             tax_dist.attribute1				attribute1,
12532             tax_dist.attribute10			attribute10,
12533             tax_dist.attribute11			attribute11,
12534             tax_dist.attribute12			attribute12,
12535             tax_dist.attribute13			attribute13,
12536             tax_dist.attribute14			attribute14,
12537             tax_dist.attribute15			attribute15,
12538             tax_dist.attribute2				attribute2,
12539             tax_dist.attribute3				attribute3,
12540             tax_dist.attribute4				attribute4,
12541             tax_dist.attribute5				attribute5,
12542             tax_dist.attribute6				attribute6,
12543             tax_dist.attribute7				attribute7,
12544             tax_dist.attribute8				attribute8,
12545             tax_dist.attribute9				attribute9,
12546             tax_dist.attribute_category			attribute_category,
12547             decode(zx_dist.recoverable_flag,
12548                    'Y', NULL,
12549 		   tax_dist.expenditure_item_date)	expenditure_item_date, /* bug 11076722 */
12550             decode(zx_dist.recoverable_flag,
12551                    'Y', NULL,
12552                    tax_dist.expenditure_organization_id)  expenditure_organization_id, /* bug 11076722 */
12553             decode(zx_dist.recoverable_flag,
12554                    'Y', NULL,
12555             	   tax_dist.expenditure_type)		expenditure_type, /* bug 11076722 */
12556             tax_dist.parent_invoice_id			parent_invoice_id,
12557             decode(zx_dist.recoverable_flag,
12558 		   'Y', 'E',
12559 		   item_dist.pa_addition_flag)		pa_addition_flag,
12560             decode(zx_dist.recoverable_flag,
12561                    'Y', NULL,
12562                    tax_dist.pa_quantity)		pa_quantity, /* bug 11076722 */
12563             NULL					prepay_amount_remaining,
12564             decode(zx_dist.recoverable_flag,
12565                    'Y', NULL,
12566                    tax_dist.project_accounting_context) project_accounting_context, /* bug 11076722 */
12567             decode(zx_dist.recoverable_flag,
12568                    'Y', NULL,
12569                    tax_dist.project_id)		project_id, /* bug 11076722 */
12570             decode(zx_dist.recoverable_flag,
12571                    'Y', NULL,
12572                    tax_dist.task_id)			task_id, /* bug 11076722 */
12573             NULL					packet_id,
12574             'N'						awt_flag,
12575             tax_dist.awt_group_id			awt_group_id,
12576             NULL					awt_tax_rate_id,
12577             NULL					awt_gross_amount,
12578             NULL					awt_invoice_id,
12579             NULL					awt_origin_group_id,
12580             NULL					reference_1,
12581             NULL					reference_2,
12582             tax_dist.org_id				org_id,
12583             NULL					awt_invoice_payment_id,
12584             tax_dist.global_attribute_category		global_attribute_category,
12585             tax_dist.global_attribute1			global_attribute1,
12586             tax_dist.global_attribute2			global_attribute2,
12587             tax_dist.global_attribute3			global_attribute3,
12588             tax_dist.global_attribute4			global_attribute4,
12589             tax_dist.global_attribute5			global_attribute5,
12590             tax_dist.global_attribute6			global_attribute6,
12591             tax_dist.global_attribute7			global_attribute7,
12592             tax_dist.global_attribute8			global_attribute8,
12593             tax_dist.global_attribute9			global_attribute9,
12594             tax_dist.global_attribute10			global_attribute10,
12595             tax_dist.global_attribute11			global_attribute11,
12596             tax_dist.global_attribute12			global_attribute12,
12597             tax_dist.global_attribute13			global_attribute13,
12598             tax_dist.global_attribute14			global_attribute14,
12599             tax_dist.global_attribute15			global_attribute15,
12600             tax_dist.global_attribute16			global_attribute16,
12601             tax_dist.global_attribute17			global_attribute17,
12602             tax_dist.global_attribute18			global_attribute18,
12603             tax_dist.global_attribute19			global_attribute19,
12604             tax_dist.global_attribute20			global_attribute20,
12605             NULL                                 	receipt_verified_flag,
12606             NULL                                 	receipt_required_flag,
12607             NULL                                 	receipt_missing_flag,
12608             NULL                                 	justification,
12609             NULL                                 	expense_group,
12610             NULL                                 	start_expense_date,
12611             NULL                                 	end_expense_date,
12615             NULL                                 	daily_amount,
12612             NULL                                 	receipt_currency_code,
12613             NULL                                 	receipt_conversion_rate,
12614             NULL                                 	receipt_currency_amount,
12616             NULL                                 	web_parameter_id,
12617             NULL                                 	adjustment_reason,
12618             decode(zx_dist.recoverable_flag,
12619                    'Y', NULL,
12620                    tax_dist.award_id)          		award_id,
12621             NULL                        		credit_card_trx_id,
12622             tax_dist.dist_match_type    		dist_match_type,
12623             tax_dist.rcv_transaction_id 		rcv_transaction_id,
12624             ap_invoice_distributions_s.NEXTVAL   	invoice_distribution_id,
12625             tax_dist.invoice_distribution_id     	parent_reversal_id,
12626             tax_dist.tax_recoverable_flag        	tax_recoverable_flag,
12627             NULL                                 	merchant_document_number,
12628             NULL                                 	merchant_name,
12629             NULL                                 	merchant_reference,
12630             NULL                                 	merchant_tax_reg_number,
12631             NULL                                 	merchant_taxpayer_id,
12632             NULL                                 	country_of_supply,
12633             NULL                                 	matched_uom_lookup_code,
12634             NULL                                 	gms_burdenable_raw_cost,
12635             NULL                                 	accounting_event_id,
12636             tax_dist.prepay_distribution_id  	  	prepay_distribution_id,
12637             NULL                                 	upgrade_posted_amt,
12638             NULL                                 	upgrade_base_posted_amt,
12639             'N'                                  	inventory_transfer_status,
12640             NULL                                 	company_prepaid_invoice_id,
12641             NULL                                 	cc_reversal_flag,
12642             NULL                                  	awt_withheld_amt,
12643             NULL                                  	pa_cmt_xface_flag,
12644 	    -- bug9321979
12645             decode(p_calling_mode,'CANCEL INVOICE',
12646                    DECODE(tax_dist.prepay_distribution_id,NULL, 'Y',NULL),Null) cancellation_flag,   --Bug8811102
12647             tax_dist.invoice_line_number	  	invoice_line_number,
12648             tax_dist.corrected_invoice_dist_id		corrected_invoice_dist_id,
12649             tax_dist.rounding_amt       	  	rounding_amt,
12650             decode(NVL(zx_dist.tax_only_line_flag,'N'),'Y',
12651 			        NULL,zx_dist.trx_line_dist_id)		charge_applicable_to_dist_id, --Bug14772756
12652             NULL					corrected_quantity,
12653             -- bug 5572121
12654             -- NULL                                  	related_id,
12655             DECODE( tax_dist.related_id, NULL, NULL,
12656                     tax_dist.invoice_distribution_id,
12657                      ap_invoice_distributions_s.CURRVAL, NULL) related_id,
12658             NULL                                  	asset_book_type_code,
12659             NULL                                  	asset_category_id,
12660             tax_dist.distribution_class 	  	distribution_class,
12661             tax_dist.tax_code_id        	  	tax_code_id,
12662             tax_dist.intended_use                 	intended_use,
12663             zx_dist.rec_nrec_tax_dist_id 	  	detail_tax_dist_id,
12664             zx_dist.rec_nrec_rate     	  	  	rec_nrec_rate,
12665             zx_dist.recovery_rate_id  	  	  	recovery_rate_id,
12666             zx_dist.recovery_type_code	  	  	recovery_type_code,
12667             NULL                                  	withholding_tax_code_id,
12668             NULL			     	  	taxable_amount,
12669             NULL			 	  	taxable_base_amount,
12670             tax_dist.tax_already_distributed_flag	tax_already_distributed_flag,
12671             tax_dist.summary_tax_line_id 	  	summary_tax_line_id,
12672 	        'N'				      	  	rcv_charge_addition_flag,
12673             (-1)*tax_dist.prepay_tax_diff_amount  prepay_tax_diff_amount, -- BUG 7338249 bug 9040333 added (-1)* as this is reversal
12674             tax_dist.pay_awt_group_id     pay_awt_group_id /*BUG 10261626*/
12675 	from	ap_invoice_distributions_all 	tax_dist,
12676 		ap_invoice_distributions_all	item_dist,
12677 		zx_rec_nrec_dist		zx_dist
12678 	where	tax_dist.invoice_id		  = p_invoice_id
12679 	/* -- Bug8575619 start */
12680 	and     tax_dist.invoice_id               = zx_dist.trx_id
12681 	and     zx_dist.application_id   = 200
12682 	and     zx_dist.entity_code        = 'AP_INVOICES'
12683 	and     zx_dist.event_class_code IN ('STANDARD INVOICES',
12684 	                                     'PREPAYMENT INVOICES',
12685 					     'EXPENSE REPORTS')
12686 	/* -- Bug8575619 end */
12687 	and	tax_dist.line_type_lookup_code    IN ('NONREC_TAX', 'REC_TAX', 'TIPV', 'TERV', 'TRV')
12688 	and	tax_dist.detail_tax_dist_id	  = zx_dist.reversed_tax_dist_id
12689 	and	item_dist.invoice_distribution_id(+) = zx_dist.trx_line_dist_id  --bug7394712
12690 	and	zx_dist.reverse_flag 		  = 'Y'
12691         --and     (p_line_number IS NULL -- bug 6056777
12692          --       or zx_dist.trx_line_id = p_line_number) --bug605677
12693 	--bugfix:5582836
12694         and     not exists(select detail_tax_dist_id
12695 			   from ap_invoice_distributions aid
12696 			   where aid.invoice_id = p_invoice_id
12697 			   and aid.detail_tax_dist_id = zx_dist.rec_nrec_tax_dist_id);
12698 
12699 
12700 /*
12701 Adding a new cursor for bug6056777, IF the parameter p_line_number is null then c_reverse_tax_dist would be opened
12702 Else c_reverse_tax_dist_1 would be opened.
12703 */
12704 
12705     cursor c_reverse_tax_dist_1 is
12706 	  select /*+ INDEX (ZX_DIST, ZX_REC_NREC_DIST_N2) */ --8576175
12707               nvl(item_dist.accounting_date,
12708 	        zx_dist.gl_date)                        accounting_date,  --bug 13101979
12709             'N'						accrual_posted_flag,
12710             'U'						assets_addition_flag,
12711             tax_dist.assets_tracking_flag		assets_tracking_flag, --Bug14772756
12712             'N'						cash_posted_flag,
12713             AP_INVOICE_LINES_PKG.get_max_dist_line_num(
12714 		              p_invoice_id,
12715 		              tax_dist.invoice_line_number)+1
12716 							distribution_line_number,
12717             tax_dist.dist_code_combination_id		dist_code_combination_id,
12718             tax_dist.invoice_id				invoice_id,
12719             l_user_id					last_updated_by,
12720             l_sysdate					last_update_date,
12721             tax_dist.line_type_lookup_code		line_type_lookup_code,
12722             ap_utilities_pkg.get_gl_period_name(
12723                               zx_dist.gl_date,
12724                               tax_dist.org_id)          period_name,
12725             tax_dist.set_of_books_id			set_of_books_id,
12726             (-tax_dist.amount)				amount,
12727             (-tax_dist.base_amount)			base_amount,
12728             --P_Invoice_Header_Rec.batch_id		batch_id,
12729             l_user_id					created_by,
12730             l_sysdate					creation_date,
12731             tax_dist.description			description,
12732             NULL					final_match_flag,
12733             tax_dist.income_tax_region			income_tax_region,
12734             l_user_id					last_update_login,
12735             NULL					match_status_flag,
12736             'N'						posted_flag,
12737             tax_dist.po_distribution_id			po_distribution_id,
12738             NULL					program_application_id,
12739             NULL					program_id,
12740             NULL					program_update_date,
12741             NULL					quantity_invoiced,
12742             NULL					request_id,
12743             'Y'						reversal_flag,
12744             tax_dist.type_1099				type_1099,
12745             tax_dist.unit_price				unit_price,
12746             DECODE(tax_dist.encumbered_flag,
12747 		   'R', 'R', 'N')   			encumbered_flag,    --Bug 8733916
12748             NULL					stat_amount,
12749             tax_dist.attribute1				attribute1,
12750             tax_dist.attribute10			attribute10,
12751             tax_dist.attribute11			attribute11,
12752             tax_dist.attribute12			attribute12,
12753             tax_dist.attribute13			attribute13,
12754             tax_dist.attribute14			attribute14,
12755             tax_dist.attribute15			attribute15,
12756             tax_dist.attribute2				attribute2,
12757             tax_dist.attribute3				attribute3,
12758             tax_dist.attribute4				attribute4,
12759             tax_dist.attribute5				attribute5,
12760             tax_dist.attribute6				attribute6,
12761             tax_dist.attribute7				attribute7,
12762             tax_dist.attribute8				attribute8,
12763             tax_dist.attribute9				attribute9,
12764             tax_dist.attribute_category			attribute_category,
12765             decode(zx_dist.recoverable_flag,
12766                    'Y', NULL,
12767 		   tax_dist.expenditure_item_date)	expenditure_item_date, /* bug 11076722 */
12768             decode(zx_dist.recoverable_flag,
12769                    'Y', NULL,
12770                    tax_dist.expenditure_organization_id)  expenditure_organization_id, /* bug 11076722 */
12771             decode(zx_dist.recoverable_flag,
12772                    'Y', NULL,
12773             	   tax_dist.expenditure_type)		expenditure_type, /* bug 11076722 */
12774             tax_dist.parent_invoice_id			parent_invoice_id,
12775             decode(zx_dist.recoverable_flag,
12776 		   'Y', 'E',
12777 		   item_dist.pa_addition_flag)		pa_addition_flag,
12778             decode(zx_dist.recoverable_flag,
12779                    'Y', NULL,
12780                    tax_dist.pa_quantity)		pa_quantity, /* bug 11076722 */
12781             NULL					prepay_amount_remaining,
12782             decode(zx_dist.recoverable_flag,
12783                    'Y', NULL,
12784                    tax_dist.project_accounting_context) project_accounting_context, /* bug 11076722 */
12785             decode(zx_dist.recoverable_flag,
12786                    'Y', NULL,
12787                    tax_dist.project_id)		project_id, /* bug 11076722 */
12788             decode(zx_dist.recoverable_flag,
12789                    'Y', NULL,
12790                    tax_dist.task_id)			task_id, /* bug 11076722 */
12791             NULL					packet_id,
12792             'N'						awt_flag,
12793             tax_dist.awt_group_id			awt_group_id,
12794             NULL					awt_tax_rate_id,
12795             NULL					awt_gross_amount,
12796             NULL					awt_invoice_id,
12797             NULL					awt_origin_group_id,
12798             NULL					reference_1,
12799             NULL					reference_2,
12800             tax_dist.org_id				org_id,
12801             NULL					awt_invoice_payment_id,
12802             tax_dist.global_attribute_category		global_attribute_category,
12803             tax_dist.global_attribute1			global_attribute1,
12807             tax_dist.global_attribute5			global_attribute5,
12804             tax_dist.global_attribute2			global_attribute2,
12805             tax_dist.global_attribute3			global_attribute3,
12806             tax_dist.global_attribute4			global_attribute4,
12808             tax_dist.global_attribute6			global_attribute6,
12809             tax_dist.global_attribute7			global_attribute7,
12810             tax_dist.global_attribute8			global_attribute8,
12811             tax_dist.global_attribute9			global_attribute9,
12812             tax_dist.global_attribute10			global_attribute10,
12813             tax_dist.global_attribute11			global_attribute11,
12814             tax_dist.global_attribute12			global_attribute12,
12815             tax_dist.global_attribute13			global_attribute13,
12816             tax_dist.global_attribute14			global_attribute14,
12817             tax_dist.global_attribute15			global_attribute15,
12818             tax_dist.global_attribute16			global_attribute16,
12819             tax_dist.global_attribute17			global_attribute17,
12820             tax_dist.global_attribute18			global_attribute18,
12821             tax_dist.global_attribute19			global_attribute19,
12822             tax_dist.global_attribute20			global_attribute20,
12823             NULL                                 	receipt_verified_flag,
12824             NULL                                 	receipt_required_flag,
12825             NULL                                 	receipt_missing_flag,
12826             NULL                                 	justification,
12827             NULL                                 	expense_group,
12828             NULL                                 	start_expense_date,
12829             NULL                                 	end_expense_date,
12830             NULL                                 	receipt_currency_code,
12831             NULL                                 	receipt_conversion_rate,
12832             NULL                                 	receipt_currency_amount,
12833             NULL                                 	daily_amount,
12834             NULL                                 	web_parameter_id,
12835             NULL                                 	adjustment_reason,
12836             decode(zx_dist.recoverable_flag,
12837                    'Y', NULL,
12838                    tax_dist.award_id)          		award_id,
12839             NULL                        		credit_card_trx_id,
12840             tax_dist.dist_match_type    		dist_match_type,
12841             tax_dist.rcv_transaction_id 		rcv_transaction_id,
12842             ap_invoice_distributions_s.NEXTVAL   	invoice_distribution_id,
12843             tax_dist.invoice_distribution_id     	parent_reversal_id,
12844             tax_dist.tax_recoverable_flag        	tax_recoverable_flag,
12845             NULL                                 	merchant_document_number,
12846             NULL                                 	merchant_name,
12847             NULL                                 	merchant_reference,
12848             NULL                                 	merchant_tax_reg_number,
12849             NULL                                 	merchant_taxpayer_id,
12850             NULL                                 	country_of_supply,
12851             NULL                                 	matched_uom_lookup_code,
12852             NULL                                 	gms_burdenable_raw_cost,
12853             NULL                                 	accounting_event_id,
12854             tax_dist.prepay_distribution_id  	  	prepay_distribution_id,
12855             NULL                                 	upgrade_posted_amt,
12856             NULL                                 	upgrade_base_posted_amt,
12857             'N'                                  	inventory_transfer_status,
12858             NULL                                 	company_prepaid_invoice_id,
12859             NULL                                 	cc_reversal_flag,
12860             NULL                                  	awt_withheld_amt,
12861             NULL                                  	pa_cmt_xface_flag,
12862 	    -- bug9321979
12863             decode(p_calling_mode,'CANCEL INVOICE',
12864                    DECODE(tax_dist.prepay_distribution_id,NULL, 'Y',NULL),Null) cancellation_flag,   --Bug8811102
12865             tax_dist.invoice_line_number	  	invoice_line_number,
12866             tax_dist.corrected_invoice_dist_id		corrected_invoice_dist_id,
12867             tax_dist.rounding_amt       	  	rounding_amt,
12868             decode(NVL(zx_dist.tax_only_line_flag,'N'),'Y',
12869 			        NULL,zx_dist.trx_line_dist_id)		charge_applicable_to_dist_id, --Bug14772756
12870             NULL					corrected_quantity,
12871             -- bug 5572121
12872             -- NULL                                  	related_id,
12873             DECODE( tax_dist.related_id, NULL, NULL,
12874                     tax_dist.invoice_distribution_id,
12875                      ap_invoice_distributions_s.CURRVAL, NULL) related_id,
12876 
12877             NULL                                  	asset_book_type_code,
12878             NULL                                  	asset_category_id,
12879             tax_dist.distribution_class 	  	distribution_class,
12880             tax_dist.tax_code_id        	  	tax_code_id,
12881             tax_dist.intended_use                 	intended_use,
12882             zx_dist.rec_nrec_tax_dist_id 	  	detail_tax_dist_id,
12883             zx_dist.rec_nrec_rate     	  	  	rec_nrec_rate,
12884             zx_dist.recovery_rate_id  	  	  	recovery_rate_id,
12885             zx_dist.recovery_type_code	  	  	recovery_type_code,
12886             NULL                                  	withholding_tax_code_id,
12887             NULL			     	  	taxable_amount,
12888             NULL			 	  	taxable_base_amount,
12889             tax_dist.tax_already_distributed_flag	tax_already_distributed_flag,
12890             tax_dist.summary_tax_line_id 	  	summary_tax_line_id,
12891 	        'N'				      	  	rcv_charge_addition_flag,
12892             (-1)*tax_dist.prepay_tax_diff_amount prepay_tax_diff_amount, -- BUG 7338249 bug 9040333 added (-1)* as this is reversal
12893             tax_dist.pay_awt_group_id     pay_awt_group_id /*BUG 10261626*/
12894 	from	ap_invoice_distributions_all 	tax_dist,
12895 		ap_invoice_distributions_all	item_dist,
12896 		zx_rec_nrec_dist		zx_dist
12897 	where	tax_dist.invoice_id		  = p_invoice_id
12898 	/* -- Bug8575619 start */
12899 	and     tax_dist.invoice_id               = zx_dist.trx_id
12900 	and     zx_dist.application_id   = 200
12901 	and     zx_dist.entity_code        = 'AP_INVOICES'
12902 	and     zx_dist.event_class_code IN ('STANDARD INVOICES',
12903 	                                     'PREPAYMENT INVOICES',
12904 					     'EXPENSE REPORTS')
12905 	/* -- Bug8575619 end */
12906 	and	tax_dist.line_type_lookup_code    IN ('NONREC_TAX', 'REC_TAX', 'TIPV', 'TERV', 'TRV')
12907 	and	tax_dist.detail_tax_dist_id	  = zx_dist.reversed_tax_dist_id
12908 	and	item_dist.invoice_distribution_id(+) = zx_dist.trx_line_dist_id  --bug7394712
12909 	and	zx_dist.reverse_flag 		  = 'Y'
12910         and   zx_dist.trx_line_id = p_line_number --bug6056777
12911 	--bugfix:5582836
12912         and     not exists(select detail_tax_dist_id
12913 			   from ap_invoice_distributions aid
12914 			   where aid.invoice_id = p_invoice_id
12915 			   and aid.detail_tax_dist_id = zx_dist.rec_nrec_tax_dist_id);
12916 
12917 
12918 
12919 	l_reverse_tax_dist	c_reverse_tax_dist%rowtype ;
12920 
12921     /*
12922      --  Bug 6694536. Added cursor to fetch reversal lines from ap_self_assessed_tax_dist_all.
12923      --  Cursor is similar to c_reverse_tax_dist, except it is using ap_self_assessed_tax_dist_all
12924      --  to fetch lines.
12925      */
12926 
12927      cursor c_rev_self_assess_tax_dist is
12928 	  select
12929             nvl(item_dist.accounting_date,
12930 	        zx_dist.gl_date)                        accounting_date, --Bug 8350132
12931             'N'						accrual_posted_flag,
12932             'U'						assets_addition_flag,
12933             tax_dist.assets_tracking_flag		assets_tracking_flag, --Bug14772756
12934             'N'						cash_posted_flag,
12935             AP_ETAX_UTILITY_PKG.Get_Max_Dist_Num_Self(
12936 		              p_invoice_id,
12937 		              tax_dist.invoice_line_number)+1
12938 							distribution_line_number,
12939             tax_dist.dist_code_combination_id		dist_code_combination_id,
12940             tax_dist.invoice_id				invoice_id,
12941             l_user_id					last_updated_by,
12942             l_sysdate					last_update_date,
12943             tax_dist.line_type_lookup_code		line_type_lookup_code,
12944             tax_dist.period_name			period_name,
12945             tax_dist.set_of_books_id			set_of_books_id,
12946             (-tax_dist.amount)				amount,
12947             (-tax_dist.base_amount)			base_amount,
12948             --P_Invoice_Header_Rec.batch_id		batch_id,
12949             l_user_id					created_by,
12950             l_sysdate					creation_date,
12951             tax_dist.description			description,
12952             NULL					final_match_flag,
12953             tax_dist.income_tax_region			income_tax_region,
12954             l_user_id					last_update_login,
12955             NULL					match_status_flag,
12956             'N'						posted_flag,
12957             tax_dist.po_distribution_id			po_distribution_id,
12958             NULL					program_application_id,
12959             NULL					program_id,
12960             NULL					program_update_date,
12961             NULL					quantity_invoiced,
12962             NULL					request_id,
12963             'Y'						reversal_flag,
12964             tax_dist.type_1099				type_1099,
12965             tax_dist.unit_price				unit_price,
12966             DECODE(tax_dist.encumbered_flag,
12967  		   'R', 'R', 'N')   			encumbered_flag,    --Bug 8733916
12968             NULL					stat_amount,
12969             tax_dist.attribute1				attribute1,
12970             tax_dist.attribute10			attribute10,
12971             tax_dist.attribute11			attribute11,
12972             tax_dist.attribute12			attribute12,
12973             tax_dist.attribute13			attribute13,
12974             tax_dist.attribute14			attribute14,
12975             tax_dist.attribute15			attribute15,
12976             tax_dist.attribute2				attribute2,
12977             tax_dist.attribute3				attribute3,
12978             tax_dist.attribute4				attribute4,
12979             tax_dist.attribute5				attribute5,
12980             tax_dist.attribute6				attribute6,
12981             tax_dist.attribute7				attribute7,
12982             tax_dist.attribute8				attribute8,
12983             tax_dist.attribute9				attribute9,
12984             tax_dist.attribute_category			attribute_category,
12985             decode(zx_dist.recoverable_flag,
12986                    'Y', NULL,
12987 		   tax_dist.expenditure_item_date)	expenditure_item_date, /* bug 11076722 */
12988             decode(zx_dist.recoverable_flag,
12989                    'Y', NULL,
12990                    tax_dist.expenditure_organization_id)  expenditure_organization_id, /* bug 11076722 */
12991             decode(zx_dist.recoverable_flag,
12992                    'Y', NULL,
12993             	   tax_dist.expenditure_type)		expenditure_type, /* bug 11076722 */
12994             tax_dist.parent_invoice_id			parent_invoice_id,
12995             decode(zx_dist.recoverable_flag,
12996 		   'Y', 'E',
12997 		   item_dist.pa_addition_flag)		pa_addition_flag,
12998             decode(zx_dist.recoverable_flag,
12999                    'Y', NULL,
13000                    tax_dist.pa_quantity)		pa_quantity, /* bug 11076722 */
13001             NULL					prepay_amount_remaining,
13002             decode(zx_dist.recoverable_flag,
13003                    'Y', NULL,
13004                    tax_dist.project_accounting_context) project_accounting_context, /* bug 11076722 */
13005             decode(zx_dist.recoverable_flag,
13006                    'Y', NULL,
13007                    tax_dist.project_id)		project_id, /* bug 11076722 */
13008             decode(zx_dist.recoverable_flag,
13009                    'Y', NULL,
13010                    tax_dist.task_id)			task_id, /* bug 11076722 */
13011             NULL					packet_id,
13012             'N'						awt_flag,
13013             tax_dist.awt_group_id			awt_group_id,
13014             NULL					awt_tax_rate_id,
13015             NULL					awt_gross_amount,
13016             NULL					awt_invoice_id,
13017             NULL					awt_origin_group_id,
13018             NULL					reference_1,
13019             NULL					reference_2,
13020             tax_dist.org_id				org_id,
13021             NULL					awt_invoice_payment_id,
13022             tax_dist.global_attribute_category		global_attribute_category,
13023             tax_dist.global_attribute1			global_attribute1,
13024             tax_dist.global_attribute2			global_attribute2,
13025             tax_dist.global_attribute3			global_attribute3,
13026             tax_dist.global_attribute4			global_attribute4,
13027             tax_dist.global_attribute5			global_attribute5,
13028             tax_dist.global_attribute6			global_attribute6,
13029             tax_dist.global_attribute7			global_attribute7,
13030             tax_dist.global_attribute8			global_attribute8,
13031             tax_dist.global_attribute9			global_attribute9,
13032             tax_dist.global_attribute10			global_attribute10,
13033             tax_dist.global_attribute11			global_attribute11,
13034             tax_dist.global_attribute12			global_attribute12,
13035             tax_dist.global_attribute13			global_attribute13,
13036             tax_dist.global_attribute14			global_attribute14,
13037             tax_dist.global_attribute15			global_attribute15,
13038             tax_dist.global_attribute16			global_attribute16,
13039             tax_dist.global_attribute17			global_attribute17,
13040             tax_dist.global_attribute18			global_attribute18,
13041             tax_dist.global_attribute19			global_attribute19,
13042             tax_dist.global_attribute20			global_attribute20,
13043             NULL                                 	receipt_verified_flag,
13044             NULL                                 	receipt_required_flag,
13045             NULL                                 	receipt_missing_flag,
13046             NULL                                 	justification,
13047             NULL                                 	expense_group,
13048             NULL                                 	start_expense_date,
13049             NULL                                 	end_expense_date,
13050             NULL                                 	receipt_currency_code,
13051             NULL                                 	receipt_conversion_rate,
13052             NULL                                 	receipt_currency_amount,
13053             NULL                                 	daily_amount,
13054             NULL                                 	web_parameter_id,
13055             NULL                                 	adjustment_reason,
13056             decode(zx_dist.recoverable_flag,
13057                    'Y', NULL,
13058                    tax_dist.award_id)          		award_id,
13059             NULL                        		credit_card_trx_id,
13060             tax_dist.dist_match_type    		dist_match_type,
13061             tax_dist.rcv_transaction_id 		rcv_transaction_id,
13062             ap_invoice_distributions_s.NEXTVAL   	invoice_distribution_id,
13063             tax_dist.invoice_distribution_id     	parent_reversal_id,
13064             tax_dist.tax_recoverable_flag        	tax_recoverable_flag,
13065             NULL                                 	merchant_document_number,
13066             NULL                                 	merchant_name,
13067             NULL                                 	merchant_reference,
13068             NULL                                 	merchant_tax_reg_number,
13069             NULL                                 	merchant_taxpayer_id,
13070             NULL                                 	country_of_supply,
13071             NULL                                 	matched_uom_lookup_code,
13072             NULL                                 	gms_burdenable_raw_cost,
13073             NULL                                 	accounting_event_id,
13074             tax_dist.prepay_distribution_id  	  	prepay_distribution_id,
13075             NULL                                 	upgrade_posted_amt,
13076             NULL                                 	upgrade_base_posted_amt,
13077             'N'                                  	inventory_transfer_status,
13078             NULL                                 	company_prepaid_invoice_id,
13079             NULL                                 	cc_reversal_flag,
13080             NULL                                  	awt_withheld_amt,
13081             NULL                                  	pa_cmt_xface_flag,
13082 	    -- bug9321979
13083             decode(p_calling_mode,'CANCEL INVOICE',
13084                    DECODE(tax_dist.prepay_distribution_id,NULL,'Y',NULL),Null) cancellation_flag,   --Bug8811102
13085             tax_dist.invoice_line_number	  	invoice_line_number,
13086             tax_dist.corrected_invoice_dist_id		corrected_invoice_dist_id,
13087             tax_dist.rounding_amt       	  	rounding_amt,
13088             decode(NVL(zx_dist.tax_only_line_flag,'N'),'Y',
13089 			        NULL,zx_dist.trx_line_dist_id)		charge_applicable_to_dist_id, --Bug14772756
13090             NULL					corrected_quantity,
13091             -- bug 5572121
13092             -- NULL                                  	related_id,
13093             DECODE( tax_dist.related_id, NULL, NULL,
13094                     tax_dist.invoice_distribution_id,
13095                      ap_invoice_distributions_s.CURRVAL, NULL) related_id,
13096 
13097             NULL                                  	asset_book_type_code,
13098             NULL                                  	asset_category_id,
13099             tax_dist.distribution_class 	  	distribution_class,
13100             tax_dist.tax_code_id        	  	tax_code_id,
13101             tax_dist.intended_use                 	intended_use,
13102             zx_dist.rec_nrec_tax_dist_id 	  	detail_tax_dist_id,
13103             zx_dist.rec_nrec_rate     	  	  	rec_nrec_rate,
13104             zx_dist.recovery_rate_id  	  	  	recovery_rate_id,
13105             zx_dist.recovery_type_code	  	  	recovery_type_code,
13106             NULL                                  	withholding_tax_code_id,
13107             NULL			     	  	taxable_amount,
13108             NULL			 	  	taxable_base_amount,
13109             tax_dist.tax_already_distributed_flag	tax_already_distributed_flag,
13110             tax_dist.summary_tax_line_id 	  	summary_tax_line_id,
13111 	        'N'				      	  	rcv_charge_addition_flag,
13112 	        zx_dist.self_assessed_flag                  self_assessed_flag,
13113             -- bug 6805655
13114             tax_dist.self_assessed_tax_liab_ccid        self_assessed_tax_liab_ccid,
13115             (-1)*tax_dist.prepay_tax_diff_amount prepay_tax_diff_amount -- BUG 7338249  bug 9040333 added (-1)* as this is reversal
13116 	from	ap_self_assessed_tax_dist_all 	tax_dist,
13117 		ap_invoice_distributions_all	item_dist,
13118 		zx_rec_nrec_dist		zx_dist
13119 	where	tax_dist.invoice_id		  = p_invoice_id
13120 	/* -- Bug8575619 start */
13121 	and     tax_dist.invoice_id               = zx_dist.trx_id
13122 	and     zx_dist.application_id   = 200
13123 	and     zx_dist.entity_code        = 'AP_INVOICES'
13124 	and     zx_dist.event_class_code IN ('STANDARD INVOICES',
13125 	                                     'PREPAYMENT INVOICES',
13126 					     'EXPENSE REPORTS')
13127 	/* -- Bug8575619 end */
13128 	and	tax_dist.line_type_lookup_code    IN ('NONREC_TAX', 'REC_TAX')
13129 	and	tax_dist.detail_tax_dist_id	  = zx_dist.reversed_tax_dist_id
13130 	and	item_dist.invoice_distribution_id(+) = zx_dist.trx_line_dist_id  --bug7394712
13131 	and	zx_dist.reverse_flag 		  = 'Y'
13132         --and     (p_line_number IS NULL -- bug 6056777
13133           --       or zx_dist.trx_line_id = p_line_number) --bug605677
13134 	--bugfix:5582836
13135     /*  -- bug 6896627
13136         and     not exists(select detail_tax_dist_id
13137 			   from ap_invoice_distributions aid
13138 			   where aid.invoice_id = p_invoice_id
13139 			   and aid.detail_tax_dist_id = zx_dist.rec_nrec_tax_dist_id)
13140     */
13141     AND NOT EXISTS
13142       (SELECT aid.detail_tax_dist_id
13143          FROM ap_self_assessed_tax_dist_all aid
13144         WHERE aid.invoice_id            = p_invoice_id
13145           AND aid.detail_tax_dist_id    = zx_dist.rec_nrec_tax_dist_id
13146           AND aid.line_type_lookup_code IN ('REC_TAX','NONREC_TAX'))
13147     -- bug 6896627
13148     ;
13149 
13150      /*
13151        -- Bug 6694536. Added cursor to fetch reversal lines from ap_self_assessed_tax_dist_all.
13152        -- Cursor is similar to c_reverse_tax_dist_1, except it is using ap_self_assessed_tax_dist_all
13153        -- to fetch lines. Second cursor for performance reasons(bug 6056777)
13154      */
13155 
13156     cursor c_rev_self_assess_tax_dist_1 is
13157 	  select
13158               nvl(item_dist.accounting_date,
13159 	        zx_dist.gl_date)                        accounting_date,  --bug 13101979
13160             'N'						accrual_posted_flag,
13161             'U'						assets_addition_flag,
13162             tax_dist.assets_tracking_flag		assets_tracking_flag, --Bug14772756
13163             'N'						cash_posted_flag,
13164             AP_ETAX_UTILITY_PKG.Get_Max_Dist_Num_Self(
13165 		              p_invoice_id,
13166 		              tax_dist.invoice_line_number)+1
13167 							distribution_line_number,
13168             tax_dist.dist_code_combination_id		dist_code_combination_id,
13169             tax_dist.invoice_id				invoice_id,
13170             l_user_id					last_updated_by,
13171             l_sysdate					last_update_date,
13172             tax_dist.line_type_lookup_code		line_type_lookup_code,
13173             tax_dist.period_name			period_name,
13174             tax_dist.set_of_books_id			set_of_books_id,
13175             (-tax_dist.amount)				amount,
13176             (-tax_dist.base_amount)			base_amount,
13177             --P_Invoice_Header_Rec.batch_id		batch_id,
13178             l_user_id					created_by,
13179             l_sysdate					creation_date,
13180             tax_dist.description			description,
13181             NULL					final_match_flag,
13182             tax_dist.income_tax_region			income_tax_region,
13183             l_user_id					last_update_login,
13184             NULL					match_status_flag,
13185             'N'						posted_flag,
13186             tax_dist.po_distribution_id			po_distribution_id,
13187             NULL					program_application_id,
13188             NULL					program_id,
13189             NULL					program_update_date,
13190             NULL					quantity_invoiced,
13191             NULL					request_id,
13192             'Y'						reversal_flag,
13193             tax_dist.type_1099				type_1099,
13194             tax_dist.unit_price				unit_price,
13195             DECODE(tax_dist.encumbered_flag,
13196 		   'R', 'R', 'N')   			encumbered_flag,    --Bug 8733916
13197             NULL					stat_amount,
13198             tax_dist.attribute1				attribute1,
13199             tax_dist.attribute10			attribute10,
13200             tax_dist.attribute11			attribute11,
13201             tax_dist.attribute12			attribute12,
13202             tax_dist.attribute13			attribute13,
13203             tax_dist.attribute14			attribute14,
13204             tax_dist.attribute15			attribute15,
13205             tax_dist.attribute2				attribute2,
13206             tax_dist.attribute3				attribute3,
13207             tax_dist.attribute4				attribute4,
13208             tax_dist.attribute5				attribute5,
13209             tax_dist.attribute6				attribute6,
13210             tax_dist.attribute7				attribute7,
13211             tax_dist.attribute8				attribute8,
13212             tax_dist.attribute9				attribute9,
13213             tax_dist.attribute_category			attribute_category,
13214             decode(zx_dist.recoverable_flag,
13215                    'Y', NULL,
13216 		   tax_dist.expenditure_item_date)	expenditure_item_date, /* bug 11076722 */
13217             decode(zx_dist.recoverable_flag,
13218                    'Y', NULL,
13219                    tax_dist.expenditure_organization_id)  expenditure_organization_id, /* bug 11076722 */
13220             decode(zx_dist.recoverable_flag,
13221                    'Y', NULL,
13222             	   tax_dist.expenditure_type)		expenditure_type, /* bug 11076722 */
13223             tax_dist.parent_invoice_id			parent_invoice_id,
13224             decode(zx_dist.recoverable_flag,
13225 		   'Y', 'E',
13226 		   item_dist.pa_addition_flag)		pa_addition_flag,
13227             decode(zx_dist.recoverable_flag,
13228                    'Y', NULL,
13229                    tax_dist.pa_quantity)		pa_quantity, /* bug 11076722 */
13230             NULL					prepay_amount_remaining,
13231             decode(zx_dist.recoverable_flag,
13232                    'Y', NULL,
13233                    tax_dist.project_accounting_context) project_accounting_context, /* bug 11076722 */
13234             decode(zx_dist.recoverable_flag,
13235                    'Y', NULL,
13236                    tax_dist.project_id)		project_id, /* bug 11076722 */
13237             decode(zx_dist.recoverable_flag,
13238                    'Y', NULL,
13239                    tax_dist.task_id)			task_id, /* bug 11076722 */
13240             NULL					packet_id,
13241             'N'						awt_flag,
13242             tax_dist.awt_group_id			awt_group_id,
13243             NULL					awt_tax_rate_id,
13244             NULL					awt_gross_amount,
13245             NULL					awt_invoice_id,
13246             NULL					awt_origin_group_id,
13247             NULL					reference_1,
13248             NULL					reference_2,
13249             tax_dist.org_id				org_id,
13250             NULL					awt_invoice_payment_id,
13251             tax_dist.global_attribute_category		global_attribute_category,
13252             tax_dist.global_attribute1			global_attribute1,
13253             tax_dist.global_attribute2			global_attribute2,
13254             tax_dist.global_attribute3			global_attribute3,
13255             tax_dist.global_attribute4			global_attribute4,
13256             tax_dist.global_attribute5			global_attribute5,
13257             tax_dist.global_attribute6			global_attribute6,
13258             tax_dist.global_attribute7			global_attribute7,
13259             tax_dist.global_attribute8			global_attribute8,
13260             tax_dist.global_attribute9			global_attribute9,
13261             tax_dist.global_attribute10			global_attribute10,
13262             tax_dist.global_attribute11			global_attribute11,
13263             tax_dist.global_attribute12			global_attribute12,
13264             tax_dist.global_attribute13			global_attribute13,
13265             tax_dist.global_attribute14			global_attribute14,
13266             tax_dist.global_attribute15			global_attribute15,
13267             tax_dist.global_attribute16			global_attribute16,
13268             tax_dist.global_attribute17			global_attribute17,
13269             tax_dist.global_attribute18			global_attribute18,
13270             tax_dist.global_attribute19			global_attribute19,
13271             tax_dist.global_attribute20			global_attribute20,
13272             NULL                                 	receipt_verified_flag,
13273             NULL                                 	receipt_required_flag,
13274             NULL                                 	receipt_missing_flag,
13275             NULL                                 	justification,
13276             NULL                                 	expense_group,
13277             NULL                                 	start_expense_date,
13278             NULL                                 	end_expense_date,
13279             NULL                                 	receipt_currency_code,
13280             NULL                                 	receipt_conversion_rate,
13281             NULL                                 	receipt_currency_amount,
13282             NULL                                 	daily_amount,
13283             NULL                                 	web_parameter_id,
13284             NULL                                 	adjustment_reason,
13285             decode(zx_dist.recoverable_flag,
13286                    'Y', NULL,
13287                    tax_dist.award_id)          		award_id,
13288             NULL                        		credit_card_trx_id,
13289             tax_dist.dist_match_type    		dist_match_type,
13290             tax_dist.rcv_transaction_id 		rcv_transaction_id,
13291             ap_invoice_distributions_s.NEXTVAL   	invoice_distribution_id,
13292             tax_dist.invoice_distribution_id     	parent_reversal_id,
13293             tax_dist.tax_recoverable_flag        	tax_recoverable_flag,
13294             NULL                                 	merchant_document_number,
13295             NULL                                 	merchant_name,
13296             NULL                                 	merchant_reference,
13297             NULL                                 	merchant_tax_reg_number,
13298             NULL                                 	merchant_taxpayer_id,
13299             NULL                                 	country_of_supply,
13300             NULL                                 	matched_uom_lookup_code,
13301             NULL                                 	gms_burdenable_raw_cost,
13302             NULL                                 	accounting_event_id,
13303             tax_dist.prepay_distribution_id  	  	prepay_distribution_id,
13304             NULL                                 	upgrade_posted_amt,
13305             NULL                                 	upgrade_base_posted_amt,
13306             'N'                                  	inventory_transfer_status,
13307             NULL                                 	company_prepaid_invoice_id,
13308             NULL                                 	cc_reversal_flag,
13309             NULL                                  	awt_withheld_amt,
13310             NULL                                  	pa_cmt_xface_flag,
13311 	    -- bug9321979
13312             decode(p_calling_mode,'CANCEL INVOICE',
13313                    DECODE(tax_dist.prepay_distribution_id,NULL,'Y',NULL),Null) cancellation_flag,   --Bug8811102
13314             tax_dist.invoice_line_number	  	invoice_line_number,
13315             tax_dist.corrected_invoice_dist_id		corrected_invoice_dist_id,
13316             tax_dist.rounding_amt       	  	rounding_amt,
13317             decode(NVL(zx_dist.tax_only_line_flag,'N'),'Y',
13318 			        NULL,zx_dist.trx_line_dist_id)		charge_applicable_to_dist_id, --Bug14772756
13319             NULL					corrected_quantity,
13320             -- bug 5572121
13321             -- NULL                                  	related_id,
13322             DECODE( tax_dist.related_id, NULL, NULL,
13323                     tax_dist.invoice_distribution_id,
13324                      ap_invoice_distributions_s.CURRVAL, NULL) related_id,
13325 
13326             NULL                                  	asset_book_type_code,
13327             NULL                                  	asset_category_id,
13328             tax_dist.distribution_class 	  	distribution_class,
13329             tax_dist.tax_code_id        	  	tax_code_id,
13330             tax_dist.intended_use                 	intended_use,
13331             zx_dist.rec_nrec_tax_dist_id 	  	detail_tax_dist_id,
13332             zx_dist.rec_nrec_rate     	  	  	rec_nrec_rate,
13333             zx_dist.recovery_rate_id  	  	  	recovery_rate_id,
13334             zx_dist.recovery_type_code	  	  	recovery_type_code,
13335             NULL                                  	withholding_tax_code_id,
13336             NULL			     	  	taxable_amount,
13337             NULL			 	  	taxable_base_amount,
13338             tax_dist.tax_already_distributed_flag	tax_already_distributed_flag,
13339             tax_dist.summary_tax_line_id 	  	summary_tax_line_id,
13340 	        'N'				      	rcv_charge_addition_flag,
13341             zx_dist.self_assessed_flag                  self_assessed_flag,
13342             -- bug 6805655
13343             tax_dist.self_assessed_tax_liab_ccid        self_assessed_tax_liab_ccid,
13344             (-1)*tax_dist.prepay_tax_diff_amount  prepay_tax_diff_amount-- BUG 7338249  bug 9040333 added (-1)* as this is reversal
13345 	from	ap_self_assessed_tax_dist_all 	tax_dist,
13346 	        ap_invoice_distributions_all	item_dist,
13347                 zx_rec_nrec_dist		zx_dist
13348 	where	tax_dist.invoice_id		  = p_invoice_id
13349 	/* -- Bug8575619 start */
13350 	and     tax_dist.invoice_id               = zx_dist.trx_id
13351 	and     zx_dist.application_id   = 200
13352 	and     zx_dist.entity_code        = 'AP_INVOICES'
13353 	and     zx_dist.event_class_code IN ('STANDARD INVOICES',
13354 	                                     'PREPAYMENT INVOICES',
13355 					     'EXPENSE REPORTS')
13356 	/* -- Bug8575619 end */
13357 	and	tax_dist.line_type_lookup_code    IN ('NONREC_TAX', 'REC_TAX')
13358 	and	tax_dist.detail_tax_dist_id	  = zx_dist.reversed_tax_dist_id
13359 	and	item_dist.invoice_distribution_id(+) = zx_dist.trx_line_dist_id  --bug7394712
13360 	and	zx_dist.reverse_flag 		  = 'Y'
13361         and   zx_dist.trx_line_id = p_line_number --bug6056777
13362 	--bugfix:5582836
13363     /*     -- bug 6896627
13364         and     not exists(select detail_tax_dist_id
13365 			   from ap_invoice_distributions aid
13366 			   where aid.invoice_id = p_invoice_id
13367 			   and aid.detail_tax_dist_id = zx_dist.rec_nrec_tax_dist_id)
13368      */
13369 
13370     AND NOT EXISTS
13371       (SELECT aid.detail_tax_dist_id
13372          FROM ap_self_assessed_tax_dist_all aid
13373         WHERE aid.invoice_id            = p_invoice_id
13374           AND aid.detail_tax_dist_id    = zx_dist.rec_nrec_tax_dist_id
13375           AND aid.line_type_lookup_code IN ('REC_TAX','NONREC_TAX'))
13376     -- bug 6896627
13377     ;
13378 
13379 
13380 	l_self_assess_rev_tax_dist   c_rev_self_assess_tax_dist%rowtype ;  -- Bug 6694536
13381 	l_self_assess_rev_tax_dist_1 c_rev_self_assess_tax_dist_1%rowtype ; -- Bug 6694536
13382 
13383     l_reverse_dist_count  NUMBER;
13384     l_Error_Code          ZX_ERRORS_GT.message_text%TYPE;
13385 
13386 
13387     --Bug8811102
13388 
13389     l_All_Error_Messages  VARCHAR2(4000);
13390     l_tax_only_line_flag  VARCHAR2(1):='N';
13391     l_line_level_action   VARCHAR2(15):='';
13392     l_line_number         NUMBER;
13393     l_trx_level_type      VARCHAR2(15);
13394     l_inv_rcv_matched     VARCHAR2(1) := 'N';
13395 
13396 
13397     l_tax_already_calculated VARCHAR2(1) :='N';
13398 
13399     l_inv_header_rec      ap_invoices_all%ROWTYPE;
13400     l_event_class_code    zx_trx_headers_gt.event_class_code%TYPE;
13401     l_event_type_code     zx_trx_headers_gt.event_type_code%TYPE;
13402 
13403     l_transaction_rec     zx_api_pub.transaction_rec_type;
13404 
13405     l_msg                 VARCHAR2(4000);
13406     l_success			  BOOLEAN;
13407 
13408 
13409     CURSOR Invoice_Header IS
13410     SELECT *
13411       FROM ap_invoices_all
13412      WHERE invoice_id = P_Invoice_Id;
13413 
13414     CURSOR Invoice_Lines IS
13415     SELECT *
13416       FROM ap_invoice_lines_all
13417      WHERE invoice_id = P_Invoice_Id
13418        AND line_type_lookup_code NOT IN ('TAX', 'AWT')
13419        AND NVL(tax_already_calculated_flag,'N') = 'Y'
13420        AND NVL(discarded_flag,'N')='N';
13421 --       AND NVL(cancelled_flag,'N')='N'; --Bug 13714737
13422 
13423     CURSOR Invoice_Line IS
13424     SELECT *
13425       FROM ap_invoice_lines_all
13426      WHERE invoice_id  = P_Invoice_Id
13427        AND line_number = P_Line_Number
13428        AND line_type_lookup_code NOT IN ('TAX', 'AWT')
13429        AND NVL(tax_already_calculated_flag,'N') = 'Y';
13430 
13431     CURSOR Invoice_Tax_OnlyLines IS
13432     SELECT *
13433       FROM ap_invoice_lines_all
13434      WHERE invoice_id = P_Invoice_Id
13435        AND line_type_lookup_code IN ('TAX')
13436        AND NVL(discarded_flag,'N')='N'
13437        AND NVL(cancelled_flag,'N')='N';
13438 
13439     --Bug8811102
13440 
13441     -- Bug 10140793 - Begin
13442     l_unrev_dist_count         NUMBER := 0 ;
13443     l_unrev_sa_dist_count      NUMBER := 0 ;
13444     l_rev_sa_dist_count        NUMBER := 0 ;
13445     l_rev_dist_cursor_count    NUMBER := 0 ;
13446     l_rev_sa_dist_cursor_count NUMBER := 0 ;
13447     -- Bug 10140793 - End
13448 
13449 BEGIN
13450 
13451     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.CANCEL_INVOICE';
13452 
13453  /*Added the following delete statements for bug 12418361 -Begin*/
13454    l_debug_info := 'Purge staging tables of ZX';
13455    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13456         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13457    END IF;
13458 
13459 
13460     DELETE zx_transaction_lines_gt;
13461     DELETE zx_import_tax_lines_gt;
13462     DELETE zx_trx_tax_link_gt;
13463     DELETE zx_reverse_dist_gt;
13464 
13465     AP_ETAX_SERVICES_PKG.G_SITE_ATTRIBUTES.DELETE;
13466     AP_ETAX_SERVICES_PKG.G_ORG_ATTRIBUTES.DELETE;
13467 
13468    /*Added the following delete statements for bug 12418361 -End*/
13469 
13470 
13471 --Bug8811102
13472 --==============================================================================
13473     -----------------------------------------------------------------
13474 	l_debug_info := 'Step 1: Populating invoice header local record';
13475     -----------------------------------------------------------------
13476     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13477         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
13478     END IF;
13479 
13480 	BEGIN
13481        OPEN Invoice_Header;
13482        FETCH Invoice_Header INTO l_inv_header_rec;
13483        CLOSE Invoice_Header;
13484     END;
13485 
13489 
13486     IF (l_inv_header_rec.invoice_type_lookup_code IN ('AWT', 'INTEREST')) THEN
13487        RETURN l_return_status;
13488     END IF;
13490     -----------------------------------------------------------------
13491 	l_debug_info := 'Step 1.1: Populating invoice lines global record';
13492     -----------------------------------------------------------------
13493     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13494         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
13495     END IF;
13496 
13497     IF P_Line_Number IS NOT NULL THEN
13498         BEGIN
13499            OPEN Invoice_Line;
13500           FETCH Invoice_Line
13501            BULK  COLLECT INTO l_inv_line_list;
13502           CLOSE Invoice_Line;
13503         END;
13504     ELSE
13505         BEGIN
13506            OPEN Invoice_Lines;
13507           FETCH Invoice_Lines
13508            BULK COLLECT INTO l_inv_line_list;
13509           CLOSE Invoice_Lines;
13510         END;
13511     END IF;
13512 
13513     -------------------------------------------------------------------
13514 	l_debug_info := 'Step 2: Get event class code';
13515     -------------------------------------------------------------------
13516     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13517        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
13518     END IF;
13519 
13520     IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
13521        P_Invoice_Type_Lookup_Code => l_inv_header_rec.invoice_type_lookup_code,
13522        P_Event_Class_Code         => l_event_class_code,
13523        P_error_code               => l_error_code,
13524        P_calling_sequence         => l_curr_calling_sequence)) THEN
13525 
13526        l_return_status := FALSE;
13527     END IF;
13528 
13529     -------------------------------------------------------------------
13530     l_debug_info := 'Step 3: Get event type code';
13531     -------------------------------------------------------------------
13532     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13533        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
13534     END IF;
13535 
13536     IF (l_return_status = TRUE) THEN
13537       	IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
13538 	       P_Event_Class_Code          => l_event_class_code,
13539 	       P_Calling_Mode              => P_Calling_Mode,
13540 	       P_eTax_Already_called_flag  => NULL,
13541 		   P_Event_Type_Code           => l_Event_Type_Code,
13542 		   P_Error_Code                => l_error_code,
13543 		   P_Calling_Sequence          => l_curr_calling_sequence)) THEN
13544 
13545 	       l_return_status := FALSE;
13546 	    END IF;
13547 	END IF;
13548 
13549     -------------------------------------------------------------------
13550     l_debug_info := 'Step 4: Populate service specific parameter';
13551     -------------------------------------------------------------------
13552     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13553        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
13554     END IF;
13555 
13556     l_transaction_rec.internal_organization_id := l_inv_header_rec.org_id;
13557     l_transaction_rec.application_id           := 200;
13558     l_transaction_rec.entity_code              := 'AP_INVOICES';
13559     l_transaction_rec.event_class_code         := l_event_class_code;
13560     l_transaction_rec.event_type_code          := l_event_type_code;
13561     l_transaction_rec.trx_id                   := l_inv_header_rec.invoice_id;
13562 
13563     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13564        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'CANCEL_TAX_LINES values');
13565        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'application_id: '|| l_transaction_rec.application_id);
13566        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'entity_code: ' || l_transaction_rec.entity_code);
13567        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: ' || l_transaction_rec.event_class_code);
13568        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_type_code: ' || l_transaction_rec.event_type_code);
13569        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '           || l_transaction_rec.trx_id);
13570     END IF;
13571 
13572     -------------------------------------------------------------------
13573    	l_debug_info := 'Step 5: Handle Cancel Invoice Mode';
13574     -------------------------------------------------------------------
13575     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13576        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
13577     END IF;
13578 
13579     IF P_Calling_Mode = 'CANCEL INVOICE' THEN
13580        IF TAX_ONLY_LINE_EXIST(p_invoice_id) THEN
13581           l_tax_only_line_flag :='Y';
13582        ELSE
13583           l_tax_only_line_flag :='N';
13584        END IF;
13585           l_line_number           :=NULL;
13586           l_trx_level_type        :=NULL;
13587           l_line_level_action     := 'CANCEL';
13588     ELSIF P_Calling_Mode = 'DISCARD LINE' THEN
13589           l_tax_only_line_flag :='N';
13590           l_line_number        :=P_Line_Number;
13591           l_trx_level_type     :='LINE';
13592           l_line_level_action  :='DISCARD';
13593     ELSIF P_Calling_Mode = 'UNAPPLY PREPAY' THEN
13594           l_tax_only_line_flag :='N';
13595           l_line_number        :=P_Line_Number;
13596           l_trx_level_type     :='LINE';
13597           l_line_level_action  :='UNAPPLY_FROM';
13598     END IF;
13599 
13600     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13601        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Calling Mode '||P_Calling_Mode);
13602        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Tax Only Flag: '|| l_tax_only_line_flag);
13603        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Line Number: ' || l_line_number);
13604        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Trx Line Level Type: ' || l_trx_level_type);
13605        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Line Level Action: ' || l_line_level_action);
13606     END IF;
13607 
13608     l_tax_distributions_exist := tax_distributions_exist (p_invoice_id=>p_invoice_id);
13609 
13610     l_self_assess_tax_dist_exist := self_assess_tax_dist_exist (p_invoice_id=>p_invoice_id);
13611 
13612 --==============================================================================
13613 --Bug8811102
13614 
13615 
13616 --bug8733916
13617 
13618    IF(p_calling_mode='DISCARD LINE') THEN  --Bug8811102
13619 
13620    	    UPDATE ap_invoice_distributions_all aid
13621       	       SET aid.encumbered_flag='R'
13622     	     WHERE aid.invoice_id=p_invoice_id
13623      	       AND nvl(aid.encumbered_flag,'N') in ('N','H','P')
13624 	       AND aid.charge_applicable_to_dist_id in (select invoice_distribution_id from ap_invoice_distributions_all aid1
13625 	                                             where aid1.invoice_id=p_invoice_id
13626 				  		       and aid1.invoice_line_number=p_line_number)
13627                AND aid.line_type_lookup_code in ('REC_TAX','NONREC_TAX','TRV','TERV','TIPV')
13628 	       AND nvl(aid.reversal_flag,'N')<>'Y'
13629                AND EXISTS (SELECT 1
13630                		     FROM financials_system_params_all fsp
13631 	                    WHERE fsp.org_id = aid.org_id
13632 		              AND nvl(fsp.purch_encumbrance_flag, 'N') = 'Y');
13633 
13634 
13635     END IF;
13636 
13637    IF(p_calling_mode='CANCEL INVOICE') THEN  --Bug8811102
13638 
13639    	    UPDATE ap_invoice_distributions_all aid
13640                SET aid.encumbered_flag='R'
13641     	     WHERE aid.invoice_id=p_invoice_id
13642      	       AND nvl(aid.encumbered_flag,'N') in ('N','H','P')
13643 	       AND aid.line_type_lookup_code in ('REC_TAX','NONREC_TAX','TRV','TERV','TIPV')
13644 	       AND nvl(aid.reversal_flag,'N')<>'Y'
13645 	       AND EXISTS (SELECT 1
13646                              FROM financials_system_params_all fsp
13647                             WHERE fsp.org_id = aid.org_id
13648                               AND nvl(fsp.purch_encumbrance_flag, 'N') = 'Y');
13649 
13650     END IF;
13651 
13652 --End of bug 8733916
13653    --Removed the if statement for bug9749258
13654 
13655 
13656     IF l_tax_distributions_exist THEN -- Marker 1
13657 
13658       -----------------------------------------------------------------
13659       l_debug_info := 'Step 1: Insert into zx_reverse_dist_gt';
13660       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13661           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13662       END IF;
13663       -----------------------------------------------------------------
13664 
13665       INSERT INTO zx_reverse_dist_gt(
13666         internal_organization_id,
13667         reversing_appln_id,
13668         reversing_entity_code,
13669         reversing_evnt_cls_code,
13670         reversing_trx_id,
13671         reversing_trx_level_type,
13672         reversing_trx_line_id,
13673         reversing_trx_line_dist_id,
13674         reversing_tax_line_id,
13675         reversed_appln_id,
13676         reversed_entity_code,
13677         reversed_evnt_cls_code,
13678         reversed_trx_id,
13679         reversed_trx_level_type,
13680         reversed_trx_line_id,
13681         reversed_trx_line_dist_id,
13682         reversed_tax_line_id
13683       )
13684       select	distinct
13685 		item_dist.org_id                	internal_organization_id,
13686                 zx_dist.application_id          	reversing_appln_id,
13687                 zx_dist.entity_code             	reversing_entity_code,
13688                 zx_dist.event_class_code        	reversing_evnt_cls_code,
13689                 zx_dist.trx_id                  	reversing_trx_id,
13690                 zx_dist.trx_level_type          	reversing_trx_level_type,
13691                 zx_dist.trx_line_id             	reversing_trx_line_id,
13692                 reverse_dist.invoice_distribution_id	reversing_trx_line_dist_id,
13693                 zx_dist.tax_line_id             	reversing_tax_line_id,
13694                 zx_dist.application_id          	reversed_appln_id,
13695                 zx_dist.entity_code             	reversed_entity_code,
13696                 zx_dist.event_class_code        	reversed_evnt_cls_code,
13697                 zx_dist.trx_id                  	reversed_trx_id,
13698                 zx_dist.trx_level_type          	reversed_trx_level_type,
13699                 zx_dist.trx_line_id             	reversed_trx_line_id,
13700 		        zx_dist.trx_line_dist_id        	reversed_trx_line_dist_id,
13701                 zx_dist.tax_line_id             	reversed_tax_line_id
13702         from    ap_invoice_distributions_all    item_dist,
13703                 ap_invoice_distributions_all    tax_dist,
13704                 ap_invoice_distributions_all    reverse_dist,
13705                 zx_rec_nrec_dist                zx_dist
13706         where   tax_dist.invoice_id                     = p_invoice_id
13707         and     tax_dist.invoice_id                     = item_dist.invoice_id
13708         and     tax_dist.charge_applicable_to_dist_id   = item_dist.invoice_distribution_id
13709         and  	item_dist.invoice_distribution_id	= reverse_dist.parent_reversal_id
13710         and     tax_dist.line_type_lookup_code          IN ('NONREC_TAX', 'REC_TAX', 'TIPV', 'TRV', 'TERV')
13711         and     tax_dist.detail_tax_dist_id             = zx_dist.rec_nrec_tax_dist_id
13712 	and     item_dist.invoice_id                    = reverse_dist.invoice_id  --bug 12920913
13713         and     nvl(zx_dist.reverse_flag, 'N')          = 'N'
13714         and     (p_line_number IS NULL
13715                  or item_dist.invoice_line_number = p_line_number);
13716 
13717       l_reverse_dist_count := SQL%ROWCOUNT;
13718 
13719       -----------------------------------------------------------------
13720       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13721         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Row Count inserted into zx_reverse_dist_gt: ' || l_reverse_dist_count);
13722       END IF;
13723       -----------------------------------------------------------------
13724 
13725       -- Bug 10140793 Begin
13726       IF l_reverse_dist_count = 0 THEN
13727          IF p_line_number IS NULL THEN
13728             SELECT COUNT(1)
13729               INTO l_unrev_dist_count
13730 	      FROM ap_invoice_distributions_all tax_dist
13731 	     WHERE tax_dist.invoice_id                    = p_invoice_id
13732                AND tax_dist.line_type_lookup_code        IN ('NONREC_TAX', 'REC_TAX', 'TIPV', 'TRV', 'TERV')
13733 	       AND tax_dist.charge_applicable_to_dist_id IS NOT NULL
13734 	       AND tax_dist.detail_tax_dist_id IS NOT NULL
13735 	       AND tax_dist.parent_reversal_id IS NULL
13736                AND NOT EXISTS ( SELECT 1
13737 	                          FROM ap_invoice_distributions_all rev_tax_dist
13738 			         WHERE rev_tax_dist.invoice_id         = p_invoice_id
13739 			           AND rev_tax_dist.parent_reversal_id = tax_dist.invoice_distribution_id
13740 			      )
13741 	       AND ROWNUM = 1 ;
13742          ELSE
13743             SELECT COUNT(1)
13744               INTO l_unrev_dist_count
13745 	      FROM ap_invoice_distributions_all tax_dist
13746 	     WHERE tax_dist.invoice_id                    = p_invoice_id
13747                AND tax_dist.line_type_lookup_code        IN ('NONREC_TAX', 'REC_TAX', 'TIPV', 'TRV', 'TERV')
13748 	       AND tax_dist.charge_applicable_to_dist_id IS NOT NULL
13749 	       AND tax_dist.detail_tax_dist_id IS NOT NULL
13750 	       AND tax_dist.parent_reversal_id IS NULL
13751                AND NOT EXISTS ( SELECT 1
13752 	                          FROM ap_invoice_distributions_all rev_tax_dist
13753 			         WHERE rev_tax_dist.invoice_id         = p_invoice_id
13754 			           AND rev_tax_dist.parent_reversal_id = tax_dist.invoice_distribution_id
13755 			      )
13756                AND tax_dist.charge_applicable_to_dist_id IN
13757 	           ( SELECT item_dist.invoice_distribution_id
13758 		       FROM ap_invoice_distributions_all item_dist
13759 		      WHERE item_dist.invoice_id          = p_invoice_id
13760 			AND item_dist.invoice_line_number = p_line_number
13761 		   )
13762 	       AND ROWNUM = 1 ;
13763          END IF ;
13764 
13765          -----------------------------------------------------------------
13766          IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13767            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Unreversed tax distributions found');
13768          END IF;
13769          -----------------------------------------------------------------
13770 
13771 	 IF l_unrev_dist_count > 0 THEN
13772             FND_MESSAGE.SET_NAME('SQLAP','AP_ERR_TAX_DIST_SYNC');
13773             APP_EXCEPTION.RAISE_EXCEPTION;
13774 	 END IF ;
13775       END IF;
13776       -- Bug 10140793 End
13777 
13778     END IF; -- Marker 1
13779 
13780 
13781     IF l_self_assess_tax_dist_exist THEN -- Marker 2
13782 
13783        l_debug_info := 'Inserting self assessed tax entries into zx_reverse_dist_gt';
13784        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13785           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13786        END IF;
13787 
13788        -- Populating zx_reverse_dist_gt with self assessed entries that has to be
13789        -- reversed. Fetched tax distributions from ap_self_assessed_tax_dist_all.
13790        -- ITEM line is already reversed in Discard method. reverse_dist refers to
13791        -- the reversed item line.
13792 
13793        INSERT INTO zx_reverse_dist_gt(
13794         internal_organization_id,
13795         reversing_appln_id,
13796         reversing_entity_code,
13797         reversing_evnt_cls_code,
13798         reversing_trx_id,
13799         reversing_trx_level_type,
13800         reversing_trx_line_id,
13801         reversing_trx_line_dist_id,
13802         reversing_tax_line_id,
13803         reversed_appln_id,
13804         reversed_entity_code,
13805         reversed_evnt_cls_code,
13806         reversed_trx_id,
13807         reversed_trx_level_type,
13808         reversed_trx_line_id,
13809         reversed_trx_line_dist_id,
13810         reversed_tax_line_id
13811       )
13812       select	distinct
13813 		item_dist.org_id                	internal_organization_id,
13814                 zx_dist.application_id          	reversing_appln_id,
13815                 zx_dist.entity_code             	reversing_entity_code,
13816                 zx_dist.event_class_code        	reversing_evnt_cls_code,
13817                 zx_dist.trx_id                  	reversing_trx_id,
13818                 zx_dist.trx_level_type          	reversing_trx_level_type,
13819                 zx_dist.trx_line_id             	reversing_trx_line_id,
13820                 reverse_dist.invoice_distribution_id	reversing_trx_line_dist_id,
13821                 zx_dist.tax_line_id             	reversing_tax_line_id,
13822                 zx_dist.application_id          	reversed_appln_id,
13823                 zx_dist.entity_code             	reversed_entity_code,
13824                 zx_dist.event_class_code        	reversed_evnt_cls_code,
13825                 zx_dist.trx_id                  	reversed_trx_id,
13826                 zx_dist.trx_level_type          	reversed_trx_level_type,
13827                 zx_dist.trx_line_id             	reversed_trx_line_id,
13828 		zx_dist.trx_line_dist_id        	reversed_trx_line_dist_id,
13829                 zx_dist.tax_line_id             	reversed_tax_line_id
13830         from    ap_invoice_distributions_all    item_dist,
13831                 ap_self_assessed_tax_dist_all    tax_dist,
13832                 ap_invoice_distributions_all    reverse_dist,
13833                 zx_rec_nrec_dist                zx_dist
13834         where   tax_dist.invoice_id                     = p_invoice_id
13835         and     tax_dist.invoice_id                     = item_dist.invoice_id
13836         and     tax_dist.charge_applicable_to_dist_id   = item_dist.invoice_distribution_id
13837         and	item_dist.invoice_distribution_id	= reverse_dist.parent_reversal_id
13838         and     tax_dist.line_type_lookup_code          IN ('NONREC_TAX', 'REC_TAX')
13839         and     tax_dist.detail_tax_dist_id             = zx_dist.rec_nrec_tax_dist_id
13840         and     nvl(zx_dist.reverse_flag, 'N')          = 'N'
13841         and     (p_line_number IS NULL
13842                  or item_dist.invoice_line_number = p_line_number);
13843 
13844       -- Bug 10140793 l_reverse_dist_count := NVL(l_reverse_dist_count, 0) + SQL%ROWCOUNT;
13845       l_rev_sa_dist_count :=  SQL%ROWCOUNT ; -- Bug 10140793
13846 
13847       -----------------------------------------------------------------
13848       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13849         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Row Count inserted into zx_reverse_dist_gt: ' || SQL%ROWCOUNT);
13850       END IF;
13851       -----------------------------------------------------------------
13852 
13853       -- Bug 10140793 Begin
13854       IF l_rev_sa_dist_count = 0 THEN
13855          IF p_line_number IS NULL THEN
13856             SELECT COUNT(1)
13857               INTO l_unrev_sa_dist_count
13858 	      FROM ap_self_assessed_tax_dist_all tax_dist
13859 	     WHERE tax_dist.invoice_id                    = p_invoice_id
13860                AND tax_dist.line_type_lookup_code        IN ('NONREC_TAX', 'REC_TAX' )
13861 	       AND tax_dist.charge_applicable_to_dist_id IS NOT NULL
13862 	       AND tax_dist.detail_tax_dist_id IS NOT NULL
13863 	       AND tax_dist.parent_reversal_id IS NULL
13864                AND NOT EXISTS ( SELECT 1
13865 	                          FROM ap_self_assessed_tax_dist_all rev_tax_dist
13866 			         WHERE rev_tax_dist.invoice_id         = p_invoice_id
13867 			           AND rev_tax_dist.parent_reversal_id = tax_dist.invoice_distribution_id
13868 			      )
13869 	       AND ROWNUM = 1 ;
13870          ELSE
13871             SELECT COUNT(1)
13872               INTO l_unrev_sa_dist_count
13873 	      FROM ap_self_assessed_tax_dist_all tax_dist
13874 	     WHERE tax_dist.invoice_id                    = p_invoice_id
13875                AND tax_dist.line_type_lookup_code        IN ('NONREC_TAX', 'REC_TAX' )
13876 	       AND tax_dist.charge_applicable_to_dist_id IS NOT NULL
13877 	       AND tax_dist.detail_tax_dist_id IS NOT NULL
13878 	       AND tax_dist.parent_reversal_id IS NULL
13879                AND NOT EXISTS ( SELECT 1
13880 	                          FROM ap_self_assessed_tax_dist_all rev_tax_dist
13881 			         WHERE rev_tax_dist.invoice_id         = p_invoice_id
13882 			           AND rev_tax_dist.parent_reversal_id = tax_dist.invoice_distribution_id
13883 			      )
13884                AND tax_dist.charge_applicable_to_dist_id IN
13885 	           ( SELECT item_dist.invoice_distribution_id
13886 		       FROM ap_invoice_distributions_all item_dist
13887 		      WHERE item_dist.invoice_id          = p_invoice_id
13888 			AND item_dist.invoice_line_number = p_line_number
13889 		   )
13890 	      AND ROWNUM = 1 ;
13891          END IF ;
13892 
13893          -----------------------------------------------------------------
13894          IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13895            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Unreversed sa tax distributions exist');
13896          END IF;
13897          -----------------------------------------------------------------
13898 
13899 	 IF l_unrev_sa_dist_count > 0 THEN
13900             FND_MESSAGE.SET_NAME('SQLAP','AP_ERR_TAX_DIST_SYNC');
13901             APP_EXCEPTION.RAISE_EXCEPTION;
13902 	 END IF ;
13903       END IF;
13904       -- Bug 10140793 End
13905 
13906     END IF; -- Marker 2
13907 
13908 --Bug8811102
13909 --==============================================================================
13910     IF (AP_ETAX_UTILITY_PKG.Is_Tax_Already_Calc_Inv(
13911 			P_Invoice_Id       => p_invoice_id,
13912 			P_Calling_Sequence => l_curr_calling_sequence)) THEN
13913        l_tax_already_calculated := 'Y';
13914     ELSE
13915        l_tax_already_calculated := 'N';
13916     END IF;
13917 
13918 
13919 
13920     -----------------------------------------------------------------
13921     l_debug_info := 'Populate Header';
13922     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13923        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13924     END IF;
13925     ----------------------------------------------------------------
13926     IF NOT(AP_ETAX_SERVICES_PKG.Populate_Headers_GT(
13927 	   P_Invoice_Header_Rec         => l_inv_header_rec,
13928 	   P_Calling_Mode               => P_Calling_Mode,
13929 	   P_eTax_Already_called_flag   => l_tax_already_calculated,
13930 	   P_Event_Class_Code           => l_event_class_code,
13931 	   P_Event_Type_Code            => l_event_type_code,
13932 	   P_Error_Code                 => l_error_code,
13933 	   P_Calling_Sequence           => l_curr_calling_sequence )) THEN
13934        l_return_status := FALSE;
13935     END IF;
13936 
13937     -----------------------------------------------------------------
13938     l_debug_info := 'Cache Line Defaults';
13939     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13940        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13941     END IF;
13942     -----------------------------------------------------------------
13943 
13944     IF l_inv_header_rec.invoice_type_lookup_code = 'PAYMENT REQUEST' THEN
13945         l_payment_request_flag :='Y';
13946 	    Cache_Line_Defaults
13947 	       ( p_org_id           => l_inv_header_rec.org_id
13948 	        ,p_vendor_site_id   => l_inv_header_rec.party_site_id
13949 	        ,p_calling_sequence => l_curr_calling_sequence);
13950     ELSE
13951         l_payment_request_flag :='N';
13952         Cache_Line_Defaults
13953 	       ( p_org_id           => l_inv_header_rec.org_id
13954 	        ,p_vendor_site_id   => l_inv_header_rec.vendor_site_id
13955             ,p_calling_sequence => l_curr_calling_sequence);
13956     END IF;
13957 
13958 
13959     -----------------------------------------------------------------
13960     l_debug_info := 'Populate TRX Lines';
13961     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
13962        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
13963     END IF;
13964     -----------------------------------------------------------------
13965 	IF (l_return_status = TRUE) THEN
13966 
13967      IF NOT(AP_ETAX_SERVICES_PKG.Populate_Lines_GT(
13968 	    P_Invoice_Header_Rec      => l_inv_header_rec,
13969 	    P_Calling_Mode            => P_Calling_Mode,
13970   	    P_Event_Class_Code        => l_event_class_code,
13971 	    P_Line_Number             => p_line_number,
13972         P_Error_Code              => l_error_code,
13973 	    P_Calling_Sequence        => l_curr_calling_sequence )) THEN
13974         l_return_status := FALSE;
13975      END IF;
13976     END IF;
13977 
13978     IF (l_return_status = TRUE)
13979         AND TAX_ONLY_LINE_EXIST(p_invoice_id)
13980         AND P_Line_Number IS NULL THEN
13981 
13982           BEGIN
13983              OPEN Invoice_Tax_OnlyLines;
13984             FETCH Invoice_Tax_OnlyLines
13985              BULK COLLECT INTO l_inv_tax_list;
13986             CLOSE Invoice_Tax_OnlyLines;
13987           END;
13988           IF NOT(AP_ETAX_SERVICES_PKG.Populate_Tax_Lines_GT(
13989 			        P_Invoice_Header_Rec      => l_inv_header_rec,
13990 			        P_Calling_Mode            => P_Calling_Mode,
13991 			        P_Event_Class_Code        => l_event_class_code,
13992 			        P_Tax_only_Flag           => 'Y',
13993 			        P_Inv_Rcv_Matched         => l_Inv_Rcv_Matched,
13994 			        P_Error_Code              => l_error_code,
13995 			        P_Calling_Sequence        => l_curr_calling_sequence )) THEN
13996 
13997 	                l_return_status := FALSE;
13998           END IF;
13999     END IF;
14000 
14001 
14002     ZX_NEW_SERVICES_PKG.CANCEL_TAX_LINES(
14003         p_api_version           => 1.0,
14004         p_init_msg_list         => FND_API.G_TRUE,
14005         p_commit                => FND_API.G_FALSE,
14006         p_validation_level      => FND_API.G_VALID_LEVEL_FULL,
14007         x_return_status         => l_return_status_service,
14008         x_msg_count             => l_msg_count,
14009         x_msg_data              => l_msg_data,
14010         p_transaction_rec       => l_transaction_rec,
14011         p_tax_only_line_flag    => l_tax_only_line_flag,
14012         p_trx_line_id           => l_line_number,
14013         p_trx_level_type        => l_trx_level_type,
14014         p_line_level_action     => l_line_level_action);
14015 
14016 
14017     IF (l_return_status_service <> 'S') THEN  -- handle errors --Marker 0
14018        l_return_status := FALSE;
14019        -----------------------------------------------------------------
14020        l_debug_info := 'Step 5.5: Handle errors returned by API';
14021 	   -----------------------------------------------------------------
14022        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
14023           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
14024        END IF;
14025 
14026   	   IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
14027 	          P_All_Error_Messages  => l_msg_data,
14028 	          P_Msg_Count           => l_msg_count,
14029 	          P_Msg_Data            => l_msg_data,
14030 	          P_Error_Code          => l_Error_Code,
14031 	          P_Calling_Sequence    => l_curr_calling_sequence)) THEN
14032 	          NULL;
14033    	   END IF;
14034        DELETE zx_transaction_lines_gt;
14035        DELETE zx_import_tax_lines_gt;
14036        DELETE zx_trx_tax_link_gt;
14037        DELETE zx_reverse_dist_gt;
14038        RETURN l_return_status;
14039     ELSE -- update the tax only line amount to 0
14040        -----------------------------------------------------------------
14041        l_debug_info := 'Update the tax line amount to 0';
14045        END IF;
14042 	   -----------------------------------------------------------------
14043        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
14044           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
14046        IF P_Line_Number IS NULL THEN
14047 
14048            UPDATE ap_invoice_lines_all a
14049               SET (amount,
14050                    base_amount,
14051                    cancelled_flag) =
14052                  (select  NVL(b.tax_amt,0),
14053                           NVL(b.tax_amt_funcl_curr,0),
14054                           b.cancel_flag
14055                     from zx_lines_summary b
14056                    where b.application_id 	 =  200
14057                      and b.entity_code 	     =  'AP_INVOICES'
14058                      and b.event_class_code IN ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
14059                      and b.summary_tax_line_id = a.summary_tax_line_id
14060                      and b.trx_id            = p_invoice_id)
14061             WHERE a.invoice_id            =  p_invoice_id
14062               AND a.line_type_lookup_code = 'TAX'
14063               AND exists
14064                   (select 'Detail Line'
14065 		             from zx_lines zx
14066 		            where zx.application_id 	 =  200
14067                       and zx.entity_code 	     =  'AP_INVOICES'
14068 		              and zx.event_class_code IN ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
14069                       and zx.summary_tax_line_id = a.summary_tax_line_id
14070 		              and zx.trx_id              = p_invoice_id);
14071 
14072            -------------------------------------------------------------------
14073            l_debug_info := 'Update Inclusive tax amount';
14074            -------------------------------------------------------------------
14075            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
14076               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
14077            END IF;
14078 
14079            BEGIN
14080              UPDATE ap_invoice_lines_all ail
14081                 SET ail.included_tax_amount =
14082                    (SELECT /*+ index(ZL ZX_LINES_U1) */SUM(NVL(zl.tax_amt, 0))
14083                       FROM zx_lines zl
14084                      WHERE zl.application_id 	 =  200
14085                        AND zl.entity_code 	     =  'AP_INVOICES'
14086 		               AND zl.event_class_code IN ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
14087                        AND zl.trx_id 		= ail.invoice_id
14088                        AND zl.trx_line_id 	= ail.line_number
14089                        AND NVL(zl.self_assessed_flag,    'N') = 'N'
14090                        AND NVL(zl.reporting_only_flag,   'N') = 'N'
14091                        AND NVL(zl.tax_amt_included_flag, 'N') = 'Y')
14092               WHERE ail.invoice_id = P_Invoice_Id
14093                 AND ail.line_type_lookup_code NOT IN ('TAX', 'AWT');
14094            EXCEPTION
14095            WHEN NO_DATA_FOUND THEN
14096              NULL;
14097            WHEN OTHERS THEN
14098              IF (SQLCODE <> -20001) THEN
14099                 FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
14100                 FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
14101                 FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
14102                 FND_MESSAGE.SET_TOKEN('PARAMETERS',
14103                 ' P_Invoice_Id = '||P_Invoice_Id||
14104                 ' P_Calling_Sequence = '||l_curr_calling_sequence);
14105                 FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
14106              END IF;
14107              APP_EXCEPTION.RAISE_EXCEPTION;
14108            END;
14109 
14110 
14111            UPDATE ap_invoice_lines_all a
14112               SET amount = 0,
14113                   base_amount=0,
14114                   cancelled_flag='Y'
14115             WHERE a.invoice_id            = p_invoice_id
14116               AND a.line_type_lookup_code = 'TAX'
14117               AND a.summary_tax_line_id IS NULL;
14118 
14119            -----------------------------------------------------------------
14120            l_debug_info := 'Invoice Cancelled All Tax Lines Synched';
14121 	       -----------------------------------------------------------------
14122            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
14123               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
14124            END IF;
14125 
14126        ELSIF p_line_number IS NOT NULL THEN
14127 
14128              UPDATE ap_invoice_lines_all a
14129                 SET (amount,base_amount) =
14130                     (select nvl(b.tax_amt,0),nvl(b.tax_amt_funcl_curr,0)
14131                        from zx_lines_summary b
14132                       where b.application_id 	    =  200
14133                         and b.entity_code 	    =  'AP_INVOICES'
14134 		                and b.event_class_code IN ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
14135                         and b.trx_id              = a.invoice_id
14136                         and b.summary_tax_line_id = a.summary_tax_line_id)
14137               WHERE a.invoice_id            = p_invoice_id
14138                 AND a.line_type_lookup_code = 'TAX'
14139                 AND exists
14140 		            (select 'Detail Line'
14141 		               from zx_lines zx
14142 		              where zx.application_id 	   =  200
14143                         and zx.entity_code 	       =  'AP_INVOICES'
14144 		                and zx.event_class_code IN ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
14145                         and zx.summary_tax_line_id = a.summary_tax_line_id
14146 		                and zx.trx_id              = p_invoice_id
14147 		                and zx.trx_line_id         = p_line_number);
14148 
14149              -----------------------------------------------------------------
14150              l_debug_info := 'Invoice Line Discareded '||p_line_number;
14151 	         -----------------------------------------------------------------
14152              IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
14153                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
14154              END IF;
14155        END IF;
14156 
14157        UPDATE ap_invoices_all ai
14158           SET (ai.total_tax_amount,
14159                ai.self_assessed_tax_amount) =
14160               (SELECT SUM(DECODE(NVL(zls.self_assessed_flag, 'N'),
14161                           'N', NVL(zls.tax_amt, 0),0)),
14162                       SUM(DECODE(NVL(zls.self_assessed_flag, 'N'),
14163                           'Y', NVL(zls.tax_amt, 0),0))
14164                  FROM zx_lines_summary zls
14165                 WHERE application_id   = AP_ETAX_PKG.AP_APPLICATION_ID
14166                   AND entity_code      = AP_ETAX_PKG.AP_ENTITY_CODE
14167                   AND event_class_code IN (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
14168                                            AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
14169                                            AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
14170                   AND zls.trx_id       = ai.invoice_id
14171                   AND NVL(zls.reporting_only_flag, 'N') = 'N')
14172         WHERE ai.invoice_id = p_invoice_id;
14173 
14174       --Removed the if statement for the bug9749258
14175 
14176 --==============================================================================
14177 --Bug8811102
14178 
14179     IF l_return_status THEN
14180 
14181      IF p_line_number IS NULL THEN -- Marker 4
14182 
14183        delete zx_tax_dist_id_gt;  --Bug 8350132
14184 
14185        IF l_tax_distributions_exist THEN -- Marker 5
14186 
14187        -----------------------------------------------------------------
14188        l_debug_info := 'Step 3: Insert reverse tax distributions into ap_invoice_distributions';
14192        ----------------------------------------------------------------
14189        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
14190            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
14191        END IF;
14193 
14194        OPEN c_reverse_tax_dist;
14195        LOOP
14196 	   FETCH c_reverse_tax_dist
14197 	   INTO  l_reverse_tax_dist;
14198 	   EXIT WHEN c_reverse_tax_dist%NOTFOUND;
14199 
14200 --bug 13101979 starts
14201 	   IF (l_open_gl_date is null and l_open_gl_period is null) THEN
14202 
14203 	      BEGIN
14204 	        l_open_gl_period :=  AP_UTILITIES_PKG.get_current_gl_date_no_cache
14205 		                     (l_reverse_tax_dist.accounting_date,l_reverse_tax_dist.org_id);
14206 
14207                    IF l_open_gl_period IS NULL THEN
14208 		     BEGIN
14209 			 select accounting_date,
14210 			        period_name
14211 			  into l_open_gl_date,
14212 			       l_open_gl_period
14213 			from ap_invoice_distributions_All aid
14214 			    where rowid =
14215 					 (select max(rowid)
14216 					  from ap_invoice_distributions_All aid1
14217 					  where aid1.invoice_id=l_reverse_tax_dist.invoice_id
14218 					  and aid1.cancellation_flag = 'Y'
14219 					  and aid1.detail_tax_dist_id is null
14220 					  and aid1.prepay_distribution_id is null);
14221 	             EXCEPTION
14222 		        WHEN OTHERS THEN
14223 			  l_open_gl_date := '' ;
14224 			  l_open_gl_period := '' ;
14225 		     END;
14226 
14227                      IF (l_open_gl_date IS NULL OR l_open_gl_period IS NULL) THEN
14228                         ap_utilities_pkg.get_open_gl_date_no_cache
14229 		          (l_reverse_tax_dist.accounting_date,l_open_gl_period,l_open_gl_date);
14230 
14231 		     END IF;
14232                    ELSE
14233 		      l_open_gl_date := l_reverse_tax_dist.accounting_date;
14234                    END IF;
14235 	     EXCEPTION
14236 	       WHEN OTHERS THEN
14237                   l_open_gl_date := '' ;
14238 	          l_open_gl_period := '' ;
14239 	     END;
14240 	  END IF;
14241 --bug 13101979 ends
14242 
14243 	   INSERT INTO ap_invoice_distributions_all (
14244             accounting_date,
14245             accrual_posted_flag,
14246             assets_addition_flag,
14247             assets_tracking_flag,
14248             cash_posted_flag,
14249             distribution_line_number,
14250             dist_code_combination_id,
14251             invoice_id,
14252             last_updated_by,
14253             last_update_date,
14254             line_type_lookup_code,
14255             period_name,
14256             set_of_books_id,
14257             amount,
14258             base_amount,
14259             --batch_id,
14260             created_by,
14261             creation_date,
14262             description,
14263             final_match_flag,
14264             income_tax_region,
14265             last_update_login,
14266             match_status_flag,
14267             posted_flag,
14268             po_distribution_id,
14269             program_application_id,
14270             program_id,
14271             program_update_date,
14272             quantity_invoiced,
14273             request_id,
14274             reversal_flag,
14275             type_1099,
14276             unit_price,
14277             encumbered_flag,
14278             stat_amount,
14279             attribute1,
14280             attribute10,
14281             attribute11,
14282             attribute12,
14283             attribute13,
14284             attribute14,
14285             attribute15,
14286             attribute2,
14287             attribute3,
14288             attribute4,
14289             attribute5,
14290             attribute6,
14291             attribute7,
14292             attribute8,
14293             attribute9,
14294             attribute_category,
14295             expenditure_item_date,
14296             expenditure_organization_id,
14297             expenditure_type,
14298             parent_invoice_id,
14299             pa_addition_flag,
14300             pa_quantity,
14301             prepay_amount_remaining,
14302             project_accounting_context,
14303             project_id,
14304             task_id,
14305             packet_id,
14306             awt_flag,
14307             awt_group_id,
14308             awt_tax_rate_id,
14309             awt_gross_amount,
14310             awt_invoice_id,
14311             awt_origin_group_id,
14312             reference_1,
14313             reference_2,
14314             org_id,
14315             awt_invoice_payment_id,
14316             global_attribute_category,
14317             global_attribute1,
14318             global_attribute2,
14319             global_attribute3,
14320             global_attribute4,
14321             global_attribute5,
14322             global_attribute6,
14323             global_attribute7,
14324             global_attribute8,
14325             global_attribute9,
14326             global_attribute10,
14327             global_attribute11,
14328             global_attribute12,
14329             global_attribute13,
14330             global_attribute14,
14331             global_attribute15,
14332             global_attribute16,
14333             global_attribute17,
14334             global_attribute18,
14335             global_attribute19,
14336             global_attribute20,
14337             receipt_verified_flag,
14338             receipt_required_flag,
14339             receipt_missing_flag,
14340             justification,
14341             expense_group,
14342             start_expense_date,
14343             end_expense_date,
14344             receipt_currency_code,
14345             receipt_conversion_rate,
14346             receipt_currency_amount,
14347             daily_amount,
14348             web_parameter_id,
14349             adjustment_reason,
14350             award_id,
14351             credit_card_trx_id,
14352             dist_match_type,
14353             rcv_transaction_id,
14354             invoice_distribution_id,
14355             parent_reversal_id,
14356             tax_recoverable_flag,
14357             merchant_document_number,
14358             merchant_name,
14359             merchant_reference,
14360             merchant_tax_reg_number,
14361             merchant_taxpayer_id,
14362             country_of_supply,
14363             matched_uom_lookup_code,
14364             gms_burdenable_raw_cost,
14365             accounting_event_id,
14366             prepay_distribution_id,
14367             upgrade_posted_amt,
14368             upgrade_base_posted_amt,
14369             inventory_transfer_status,
14370             company_prepaid_invoice_id,
14371             cc_reversal_flag,
14372             awt_withheld_amt,
14373             pa_cmt_xface_flag,
14374             cancellation_flag,
14375             invoice_line_number,
14376             corrected_invoice_dist_id,
14377             rounding_amt,
14381             asset_book_type_code,
14378             charge_applicable_to_dist_id,
14379             corrected_quantity,
14380             related_id,
14382             asset_category_id,
14383             distribution_class,
14384             tax_code_id,
14385             intended_use,
14386             detail_tax_dist_id,
14387             rec_nrec_rate,
14388             recovery_rate_id,
14389             recovery_type_code,
14390             withholding_tax_code_id,
14391             taxable_amount,
14392             taxable_base_amount,
14393             tax_already_distributed_flag,
14394             summary_tax_line_id,
14395 	        rcv_charge_addition_flag,
14396             prepay_tax_diff_amount, -- BUG 7338249
14397             pay_awt_group_id) /*BUG 10261626*/
14398 	VALUES
14399 	(
14400 	nvl(l_open_gl_date, l_reverse_tax_dist.accounting_date),  --bug 13101979
14401 	l_reverse_tax_dist.accrual_posted_flag,
14402 	l_reverse_tax_dist.assets_addition_flag,
14403 	l_reverse_tax_dist.assets_tracking_flag,
14404 	l_reverse_tax_dist.cash_posted_flag,
14405 	l_reverse_tax_dist.distribution_line_number,
14406 	l_reverse_tax_dist.dist_code_combination_id,
14407 	l_reverse_tax_dist.invoice_id,
14408 	l_reverse_tax_dist.last_updated_by,
14409 	l_reverse_tax_dist.last_update_date,
14410 	l_reverse_tax_dist.line_type_lookup_code,
14411 	nvl(l_open_gl_period , l_reverse_tax_dist.period_name),  --bug 13101979
14412 	l_reverse_tax_dist.set_of_books_id,
14413 	l_reverse_tax_dist.amount,
14414 	l_reverse_tax_dist.base_amount,
14415 	--l_reverse_tax_dist.batch_id,
14416 	l_reverse_tax_dist.created_by,
14417 	l_reverse_tax_dist.creation_date,
14418 	l_reverse_tax_dist.description,
14419 	l_reverse_tax_dist.final_match_flag,
14420 	l_reverse_tax_dist.income_tax_region,
14421 	l_reverse_tax_dist.last_update_login,
14422 	l_reverse_tax_dist.match_status_flag,
14423 	l_reverse_tax_dist.posted_flag,
14424 	l_reverse_tax_dist.po_distribution_id,
14425 	l_reverse_tax_dist.program_application_id,
14426 	l_reverse_tax_dist.program_id,
14427 	l_reverse_tax_dist.program_update_date,
14428 	l_reverse_tax_dist.quantity_invoiced,
14429 	l_reverse_tax_dist.request_id,
14430 	l_reverse_tax_dist.reversal_flag,
14431 	l_reverse_tax_dist.type_1099,
14432 	l_reverse_tax_dist.unit_price,
14433 	l_reverse_tax_dist.encumbered_flag,
14434 	l_reverse_tax_dist.stat_amount,
14435 	l_reverse_tax_dist.attribute1,
14436 	l_reverse_tax_dist.attribute10,
14437 	l_reverse_tax_dist.attribute11,
14438 	l_reverse_tax_dist.attribute12,
14439 	l_reverse_tax_dist.attribute13,
14440 	l_reverse_tax_dist.attribute14,
14441 	l_reverse_tax_dist.attribute15,
14442 	l_reverse_tax_dist.attribute2,
14443 	l_reverse_tax_dist.attribute3,
14444 	l_reverse_tax_dist.attribute4,
14445 	l_reverse_tax_dist.attribute5,
14446 	l_reverse_tax_dist.attribute6,
14447 	l_reverse_tax_dist.attribute7,
14448 	l_reverse_tax_dist.attribute8,
14449 	l_reverse_tax_dist.attribute9,
14450 	l_reverse_tax_dist.attribute_category,
14451 	l_reverse_tax_dist.expenditure_item_date,
14452 	l_reverse_tax_dist.expenditure_organization_id,
14453 	l_reverse_tax_dist.expenditure_type,
14454 	l_reverse_tax_dist.parent_invoice_id,
14455 	l_reverse_tax_dist.pa_addition_flag,
14456 	l_reverse_tax_dist.pa_quantity,
14457 	l_reverse_tax_dist.prepay_amount_remaining,
14458 	l_reverse_tax_dist.project_accounting_context,
14459 	l_reverse_tax_dist.project_id,
14460 	l_reverse_tax_dist.task_id,
14461 	l_reverse_tax_dist.packet_id,
14462 	l_reverse_tax_dist.awt_flag,
14463 	l_reverse_tax_dist.awt_group_id,
14464 	l_reverse_tax_dist.awt_tax_rate_id,
14465 	l_reverse_tax_dist.awt_gross_amount,
14466 	l_reverse_tax_dist.awt_invoice_id,
14467 	l_reverse_tax_dist.awt_origin_group_id,
14468 	l_reverse_tax_dist.reference_1,
14469 	l_reverse_tax_dist.reference_2,
14470 	l_reverse_tax_dist.org_id,
14471 	l_reverse_tax_dist.awt_invoice_payment_id,
14472 	l_reverse_tax_dist.global_attribute_category,
14473 	l_reverse_tax_dist.global_attribute1,
14474 	l_reverse_tax_dist.global_attribute2,
14475 	l_reverse_tax_dist.global_attribute3,
14476 	l_reverse_tax_dist.global_attribute4,
14477 	l_reverse_tax_dist.global_attribute5,
14478 	l_reverse_tax_dist.global_attribute6,
14479 	l_reverse_tax_dist.global_attribute7,
14480 	l_reverse_tax_dist.global_attribute8,
14481 	l_reverse_tax_dist.global_attribute9,
14482 	l_reverse_tax_dist.global_attribute10,
14483 	l_reverse_tax_dist.global_attribute11,
14484 	l_reverse_tax_dist.global_attribute12,
14485 	l_reverse_tax_dist.global_attribute13,
14486 	l_reverse_tax_dist.global_attribute14,
14487 	l_reverse_tax_dist.global_attribute15,
14488 	l_reverse_tax_dist.global_attribute16,
14489 	l_reverse_tax_dist.global_attribute17,
14490 	l_reverse_tax_dist.global_attribute18,
14491 	l_reverse_tax_dist.global_attribute19,
14492 	l_reverse_tax_dist.global_attribute20,
14493 	l_reverse_tax_dist.receipt_verified_flag,
14494 	l_reverse_tax_dist.receipt_required_flag,
14495 	l_reverse_tax_dist.receipt_missing_flag,
14496 	l_reverse_tax_dist.justification,
14497 	l_reverse_tax_dist.expense_group,
14498 	l_reverse_tax_dist.start_expense_date,
14499 	l_reverse_tax_dist.end_expense_date,
14500 	l_reverse_tax_dist.receipt_currency_code,
14501 	l_reverse_tax_dist.receipt_conversion_rate,
14502 	l_reverse_tax_dist.receipt_currency_amount,
14503 	l_reverse_tax_dist.daily_amount,
14504 	l_reverse_tax_dist.web_parameter_id,
14505 	l_reverse_tax_dist.adjustment_reason,
14506 	l_reverse_tax_dist.award_id,
14507 	l_reverse_tax_dist.credit_card_trx_id,
14508 	l_reverse_tax_dist.dist_match_type,
14509 	l_reverse_tax_dist.rcv_transaction_id,
14510 	l_reverse_tax_dist.invoice_distribution_id,
14511 	l_reverse_tax_dist.parent_reversal_id,
14512 	l_reverse_tax_dist.tax_recoverable_flag,
14516 	l_reverse_tax_dist.merchant_tax_reg_number,
14513 	l_reverse_tax_dist.merchant_document_number,
14514 	l_reverse_tax_dist.merchant_name,
14515 	l_reverse_tax_dist.merchant_reference,
14517 	l_reverse_tax_dist.merchant_taxpayer_id,
14518 	l_reverse_tax_dist.country_of_supply,
14519 	l_reverse_tax_dist.matched_uom_lookup_code,
14520 	l_reverse_tax_dist.gms_burdenable_raw_cost,
14521 	l_reverse_tax_dist.accounting_event_id,
14522 	l_reverse_tax_dist.prepay_distribution_id,
14523 	l_reverse_tax_dist.upgrade_posted_amt,
14524 	l_reverse_tax_dist.upgrade_base_posted_amt,
14525 	l_reverse_tax_dist.inventory_transfer_status,
14526 	l_reverse_tax_dist.company_prepaid_invoice_id,
14527 	l_reverse_tax_dist.cc_reversal_flag,
14528 	l_reverse_tax_dist.awt_withheld_amt,
14529 	l_reverse_tax_dist.pa_cmt_xface_flag,
14530 	l_reverse_tax_dist.cancellation_flag,
14531 	l_reverse_tax_dist.invoice_line_number,
14532 	l_reverse_tax_dist.corrected_invoice_dist_id,
14533 	l_reverse_tax_dist.rounding_amt,
14534 	l_reverse_tax_dist.charge_applicable_to_dist_id,
14535 	l_reverse_tax_dist.corrected_quantity,
14536 	l_reverse_tax_dist.related_id,
14537 	l_reverse_tax_dist.asset_book_type_code,
14538 	l_reverse_tax_dist.asset_category_id,
14539 	l_reverse_tax_dist.distribution_class,
14540 	l_reverse_tax_dist.tax_code_id,
14541 	l_reverse_tax_dist.intended_use,
14542 	l_reverse_tax_dist.detail_tax_dist_id,
14543 	l_reverse_tax_dist.rec_nrec_rate,
14544 	l_reverse_tax_dist.recovery_rate_id,
14545 	l_reverse_tax_dist.recovery_type_code,
14546 	l_reverse_tax_dist.withholding_tax_code_id,
14547 	l_reverse_tax_dist.taxable_amount,
14548 	l_reverse_tax_dist.taxable_base_amount,
14549 	l_reverse_tax_dist.tax_already_distributed_flag,
14550 	l_reverse_tax_dist.summary_tax_line_id,
14551 	l_reverse_tax_dist.rcv_charge_addition_flag,
14552 	l_reverse_tax_dist.prepay_tax_diff_amount, -- BUG 7338249
14553     l_reverse_tax_dist.pay_awt_group_id); /*BUG 10261626*/
14554 
14555         --Bug 8350132
14556 
14557 	INSERT into ZX_TAX_DIST_ID_GT (TAX_DIST_ID) values (l_reverse_tax_dist.detail_tax_dist_id) ;
14558 	l_inv_cancel_date := l_reverse_tax_dist.accounting_date ;
14559 
14560         --End Bug 8350132
14561 
14562      END LOOP;
14563 
14564      l_rev_dist_cursor_count := c_reverse_tax_dist%ROWCOUNT  ;  -- Bug 10140793
14565 
14566      CLOSE c_reverse_tax_dist;
14567 
14568      -- Bug 10140793 Begin
14569      -----------------------------------------------------------------
14570      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
14571        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, '(1) l_rev_dist_cursor_count: ' || l_rev_dist_cursor_count);
14572      END IF;
14573      -----------------------------------------------------------------
14574 
14575      IF ( l_reverse_dist_count <> 0 AND l_rev_dist_cursor_count = 0 ) THEN
14576         FND_MESSAGE.SET_NAME('SQLAP','AP_ERR_TAX_DIST_SYNC');
14577         APP_EXCEPTION.RAISE_EXCEPTION;
14578      END IF ;
14579      -- Bug 10140793  End
14580 
14581      END IF; -- Marker 5
14582 
14583 
14584      IF l_self_assess_tax_dist_exist THEN -- Marker 6
14585 
14586         -----------------------------------------------------------------
14587         l_debug_info := 'Step 3: Insert reverse self assessed tax distributions into ap_invoice_distributions';
14588         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
14589             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
14590         END IF;
14591         ----------------------------------------------------------------
14592 
14593         OPEN c_rev_self_assess_tax_dist;
14594         LOOP
14595 	    FETCH c_rev_self_assess_tax_dist
14596 	     INTO  l_self_assess_rev_tax_dist;
14597 	     EXIT WHEN c_rev_self_assess_tax_dist%NOTFOUND;
14598 
14599 --bug 13101979 starts
14600 
14601           IF (l_open_gl_date is null and l_open_gl_period is null) THEN
14602 
14603 	     BEGIN
14604 	        l_open_gl_period :=  AP_UTILITIES_PKG.get_current_gl_date_no_cache
14605 		                     (l_self_assess_rev_tax_dist.accounting_date,l_self_assess_rev_tax_dist.org_id);
14606 
14607                    IF l_open_gl_period IS NULL THEN
14608 		     BEGIN
14609 			 select accounting_date,
14610 			        period_name
14611 			  into l_open_gl_date,
14612 			       l_open_gl_period
14613 			from ap_invoice_distributions_All aid
14614 			    where rowid =
14615 					 (select max(rowid)
14616 					  from ap_invoice_distributions_All aid1
14617 					  where aid1.invoice_id=l_self_assess_rev_tax_dist.invoice_id
14618 					  and aid1.cancellation_flag = 'Y'
14619 					  and aid1.detail_tax_dist_id is null
14620 					  and aid1.prepay_distribution_id is null);
14621 	             EXCEPTION
14622 		        WHEN OTHERS THEN
14623 			  l_open_gl_date := '';
14624 			  l_open_gl_period := '';
14625 		     END;
14626 
14627                        IF (l_open_gl_date IS NULL OR l_open_gl_period IS NULL) THEN
14628                            ap_utilities_pkg.get_open_gl_date_no_cache
14629 		             (l_self_assess_rev_tax_dist.accounting_date,l_open_gl_period,l_open_gl_date);
14630 
14631 		       END IF;
14632 	           ELSE
14633 		      l_open_gl_date := l_self_assess_rev_tax_dist.accounting_date;
14634                    END IF;
14635 	     EXCEPTION
14636 	       WHEN OTHERS THEN
14637                   l_open_gl_date := '';
14638 	          l_open_gl_period := '';
14639 	     END;
14640 	  END IF;
14641 --bug 13101979 ends
14642 
14643 	   INSERT INTO ap_self_assessed_tax_dist_all (
14644                 accounting_date,
14645                 accrual_posted_flag,
14646                 assets_addition_flag,
14650                 dist_code_combination_id,
14647                 assets_tracking_flag,
14648                 cash_posted_flag,
14649                 distribution_line_number,
14651                 invoice_id,
14652                 last_updated_by,
14653                 last_update_date,
14654                 line_type_lookup_code,
14655                 period_name,
14656                 set_of_books_id,
14657                 amount,
14658                 base_amount,
14659                 --batch_id,
14660                 created_by,
14661                 creation_date,
14662                 description,
14663                 final_match_flag,
14664                 income_tax_region,
14665                 last_update_login,
14666                 match_status_flag,
14667                 posted_flag,
14668                 po_distribution_id,
14669                 program_application_id,
14670                 program_id,
14671                 program_update_date,
14672                 quantity_invoiced,
14673                 request_id,
14674                 reversal_flag,
14675                 type_1099,
14676                 unit_price,
14677                 encumbered_flag,
14678                 stat_amount,
14679                 attribute1,
14680                 attribute10,
14681                 attribute11,
14682                 attribute12,
14683                 attribute13,
14684                 attribute14,
14685                 attribute15,
14686                 attribute2,
14687                 attribute3,
14688                 attribute4,
14689                 attribute5,
14690                 attribute6,
14691                 attribute7,
14692                 attribute8,
14693                 attribute9,
14694                 attribute_category,
14695                 expenditure_item_date,
14696                 expenditure_organization_id,
14697                 expenditure_type,
14698                 parent_invoice_id,
14699                 pa_addition_flag,
14700                 pa_quantity,
14701                 prepay_amount_remaining,
14702                 project_accounting_context,
14703                 project_id,
14704                 task_id,
14705                 packet_id,
14706                 awt_flag,
14707                 awt_group_id,
14708                 awt_tax_rate_id,
14709                 awt_gross_amount,
14710                 awt_invoice_id,
14711                 awt_origin_group_id,
14712                 reference_1,
14713                 reference_2,
14714                 org_id,
14715                 awt_invoice_payment_id,
14716                 global_attribute_category,
14717                 global_attribute1,
14718                 global_attribute2,
14719                 global_attribute3,
14720                 global_attribute4,
14721                 global_attribute5,
14722                 global_attribute6,
14723                 global_attribute7,
14724                 global_attribute8,
14725                 global_attribute9,
14726                 global_attribute10,
14727                 global_attribute11,
14728                 global_attribute12,
14729                 global_attribute13,
14730                 global_attribute14,
14731                 global_attribute15,
14732                 global_attribute16,
14733                 global_attribute17,
14734                 global_attribute18,
14735                 global_attribute19,
14736                 global_attribute20,
14737                 receipt_verified_flag,
14738                 receipt_required_flag,
14739                 receipt_missing_flag,
14740                 justification,
14741                 expense_group,
14742                 start_expense_date,
14743                 end_expense_date,
14744                 receipt_currency_code,
14745                 receipt_conversion_rate,
14746                 receipt_currency_amount,
14747                 daily_amount,
14748                 web_parameter_id,
14749                 adjustment_reason,
14750                 award_id,
14751                 credit_card_trx_id,
14752                 dist_match_type,
14753                 rcv_transaction_id,
14754                 invoice_distribution_id,
14755                 parent_reversal_id,
14756                 tax_recoverable_flag,
14757                 merchant_document_number,
14758                 merchant_name,
14759                 merchant_reference,
14760                 merchant_tax_reg_number,
14761                 merchant_taxpayer_id,
14762                 country_of_supply,
14763                 matched_uom_lookup_code,
14764                 gms_burdenable_raw_cost,
14765                 accounting_event_id,
14766                 prepay_distribution_id,
14767                 upgrade_posted_amt,
14768                 upgrade_base_posted_amt,
14769                 inventory_transfer_status,
14770                 company_prepaid_invoice_id,
14771                 cc_reversal_flag,
14772                 awt_withheld_amt,
14773                 pa_cmt_xface_flag,
14774                 cancellation_flag,
14775                 invoice_line_number,
14776                 corrected_invoice_dist_id,
14777                 rounding_amt,
14778                 charge_applicable_to_dist_id,
14779                 corrected_quantity,
14780                 related_id,
14781                 asset_book_type_code,
14782                 asset_category_id,
14783                 distribution_class,
14784                 tax_code_id,
14785                 intended_use,
14786                 detail_tax_dist_id,
14787                 rec_nrec_rate,
14788                 recovery_rate_id,
14789                 recovery_type_code,
14790                 withholding_tax_code_id,
14791                 taxable_amount,
14792                 taxable_base_amount,
14793                 tax_already_distributed_flag,
14794                 summary_tax_line_id,
14795 		        rcv_charge_addition_flag,
14796                 self_assessed_flag,
14797                 self_assessed_tax_liab_ccid, --bug6805655
14798                 prepay_tax_diff_amount -- BUG 7338249
14799                 )
14800 		VALUES
14801 		(
14802 		nvl(l_open_gl_date,l_self_assess_rev_tax_dist.accounting_date), --bug 13101979
14803 		l_self_assess_rev_tax_dist.accrual_posted_flag,
14804 		l_self_assess_rev_tax_dist.assets_addition_flag,
14805 		l_self_assess_rev_tax_dist.assets_tracking_flag,
14806 		l_self_assess_rev_tax_dist.cash_posted_flag,
14807 		l_self_assess_rev_tax_dist.distribution_line_number,
14808 		l_self_assess_rev_tax_dist.dist_code_combination_id,
14809 		l_self_assess_rev_tax_dist.invoice_id,
14810 		l_self_assess_rev_tax_dist.last_updated_by,
14811 		l_self_assess_rev_tax_dist.last_update_date,
14812 		l_self_assess_rev_tax_dist.line_type_lookup_code,
14813 		nvl(l_open_gl_period,l_self_assess_rev_tax_dist.period_name), --bug 13101979
14814 		l_self_assess_rev_tax_dist.set_of_books_id,
14815 		l_self_assess_rev_tax_dist.amount,
14816 		l_self_assess_rev_tax_dist.base_amount,
14817 		--l_self_assess_rev_tax_dist.batch_id,
14818 		l_self_assess_rev_tax_dist.created_by,
14819 		l_self_assess_rev_tax_dist.creation_date,
14820 		l_self_assess_rev_tax_dist.description,
14821 		l_self_assess_rev_tax_dist.final_match_flag,
14822 		l_self_assess_rev_tax_dist.income_tax_region,
14823 		l_self_assess_rev_tax_dist.last_update_login,
14824 		l_self_assess_rev_tax_dist.match_status_flag,
14825 		l_self_assess_rev_tax_dist.posted_flag,
14826 		l_self_assess_rev_tax_dist.po_distribution_id,
14827 		l_self_assess_rev_tax_dist.program_application_id,
14828 		l_self_assess_rev_tax_dist.program_id,
14829 		l_self_assess_rev_tax_dist.program_update_date,
14830 		l_self_assess_rev_tax_dist.quantity_invoiced,
14831 		l_self_assess_rev_tax_dist.request_id,
14832 		l_self_assess_rev_tax_dist.reversal_flag,
14833 		l_self_assess_rev_tax_dist.type_1099,
14834 		l_self_assess_rev_tax_dist.unit_price,
14835 		l_self_assess_rev_tax_dist.encumbered_flag,
14836 		l_self_assess_rev_tax_dist.stat_amount,
14837 		l_self_assess_rev_tax_dist.attribute1,
14838 		l_self_assess_rev_tax_dist.attribute10,
14839 		l_self_assess_rev_tax_dist.attribute11,
14840 		l_self_assess_rev_tax_dist.attribute12,
14841 		l_self_assess_rev_tax_dist.attribute13,
14842 		l_self_assess_rev_tax_dist.attribute14,
14843 		l_self_assess_rev_tax_dist.attribute15,
14847 		l_self_assess_rev_tax_dist.attribute5,
14844 		l_self_assess_rev_tax_dist.attribute2,
14845 		l_self_assess_rev_tax_dist.attribute3,
14846 		l_self_assess_rev_tax_dist.attribute4,
14848 		l_self_assess_rev_tax_dist.attribute6,
14849 		l_self_assess_rev_tax_dist.attribute7,
14850 		l_self_assess_rev_tax_dist.attribute8,
14851 		l_self_assess_rev_tax_dist.attribute9,
14852 		l_self_assess_rev_tax_dist.attribute_category,
14853 		l_self_assess_rev_tax_dist.expenditure_item_date,
14854 		l_self_assess_rev_tax_dist.expenditure_organization_id,
14855 		l_self_assess_rev_tax_dist.expenditure_type,
14856 		l_self_assess_rev_tax_dist.parent_invoice_id,
14857 		l_self_assess_rev_tax_dist.pa_addition_flag,
14858 		l_self_assess_rev_tax_dist.pa_quantity,
14859 		l_self_assess_rev_tax_dist.prepay_amount_remaining,
14860 		l_self_assess_rev_tax_dist.project_accounting_context,
14861 		l_self_assess_rev_tax_dist.project_id,
14862 		l_self_assess_rev_tax_dist.task_id,
14863 		l_self_assess_rev_tax_dist.packet_id,
14864 		l_self_assess_rev_tax_dist.awt_flag,
14865 		l_self_assess_rev_tax_dist.awt_group_id,
14866 		l_self_assess_rev_tax_dist.awt_tax_rate_id,
14867 		l_self_assess_rev_tax_dist.awt_gross_amount,
14868 		l_self_assess_rev_tax_dist.awt_invoice_id,
14869 		l_self_assess_rev_tax_dist.awt_origin_group_id,
14870 		l_self_assess_rev_tax_dist.reference_1,
14871 		l_self_assess_rev_tax_dist.reference_2,
14872 		l_self_assess_rev_tax_dist.org_id,
14873 		l_self_assess_rev_tax_dist.awt_invoice_payment_id,
14874 		l_self_assess_rev_tax_dist.global_attribute_category,
14875 		l_self_assess_rev_tax_dist.global_attribute1,
14876 		l_self_assess_rev_tax_dist.global_attribute2,
14877 		l_self_assess_rev_tax_dist.global_attribute3,
14878 		l_self_assess_rev_tax_dist.global_attribute4,
14879 		l_self_assess_rev_tax_dist.global_attribute5,
14880 		l_self_assess_rev_tax_dist.global_attribute6,
14881 		l_self_assess_rev_tax_dist.global_attribute7,
14882 		l_self_assess_rev_tax_dist.global_attribute8,
14883 		l_self_assess_rev_tax_dist.global_attribute9,
14884 		l_self_assess_rev_tax_dist.global_attribute10,
14885 		l_self_assess_rev_tax_dist.global_attribute11,
14886 		l_self_assess_rev_tax_dist.global_attribute12,
14887 		l_self_assess_rev_tax_dist.global_attribute13,
14888 		l_self_assess_rev_tax_dist.global_attribute14,
14889 		l_self_assess_rev_tax_dist.global_attribute15,
14890 		l_self_assess_rev_tax_dist.global_attribute16,
14891 		l_self_assess_rev_tax_dist.global_attribute17,
14892 		l_self_assess_rev_tax_dist.global_attribute18,
14893 		l_self_assess_rev_tax_dist.global_attribute19,
14894 		l_self_assess_rev_tax_dist.global_attribute20,
14895 		l_self_assess_rev_tax_dist.receipt_verified_flag,
14896 		l_self_assess_rev_tax_dist.receipt_required_flag,
14897 		l_self_assess_rev_tax_dist.receipt_missing_flag,
14898 		l_self_assess_rev_tax_dist.justification,
14899 		l_self_assess_rev_tax_dist.expense_group,
14900 		l_self_assess_rev_tax_dist.start_expense_date,
14901 		l_self_assess_rev_tax_dist.end_expense_date,
14902 		l_self_assess_rev_tax_dist.receipt_currency_code,
14903 		l_self_assess_rev_tax_dist.receipt_conversion_rate,
14904 		l_self_assess_rev_tax_dist.receipt_currency_amount,
14905 		l_self_assess_rev_tax_dist.daily_amount,
14906 		l_self_assess_rev_tax_dist.web_parameter_id,
14907 		l_self_assess_rev_tax_dist.adjustment_reason,
14908 		l_self_assess_rev_tax_dist.award_id,
14909 		l_self_assess_rev_tax_dist.credit_card_trx_id,
14910 		l_self_assess_rev_tax_dist.dist_match_type,
14911 		l_self_assess_rev_tax_dist.rcv_transaction_id,
14912 		l_self_assess_rev_tax_dist.invoice_distribution_id,
14913 		l_self_assess_rev_tax_dist.parent_reversal_id,
14914 		l_self_assess_rev_tax_dist.tax_recoverable_flag,
14915 		l_self_assess_rev_tax_dist.merchant_document_number,
14916 		l_self_assess_rev_tax_dist.merchant_name,
14917 		l_self_assess_rev_tax_dist.merchant_reference,
14918 		l_self_assess_rev_tax_dist.merchant_tax_reg_number,
14919 		l_self_assess_rev_tax_dist.merchant_taxpayer_id,
14920 		l_self_assess_rev_tax_dist.country_of_supply,
14921 		l_self_assess_rev_tax_dist.matched_uom_lookup_code,
14922 		l_self_assess_rev_tax_dist.gms_burdenable_raw_cost,
14923 		l_self_assess_rev_tax_dist.accounting_event_id,
14924 		l_self_assess_rev_tax_dist.prepay_distribution_id,
14925 		l_self_assess_rev_tax_dist.upgrade_posted_amt,
14926 		l_self_assess_rev_tax_dist.upgrade_base_posted_amt,
14927 		l_self_assess_rev_tax_dist.inventory_transfer_status,
14928 		l_self_assess_rev_tax_dist.company_prepaid_invoice_id,
14929 		l_self_assess_rev_tax_dist.cc_reversal_flag,
14930 		l_self_assess_rev_tax_dist.awt_withheld_amt,
14931 		l_self_assess_rev_tax_dist.pa_cmt_xface_flag,
14932 		l_self_assess_rev_tax_dist.cancellation_flag,
14933 		l_self_assess_rev_tax_dist.invoice_line_number,
14934 		l_self_assess_rev_tax_dist.corrected_invoice_dist_id,
14935 		l_self_assess_rev_tax_dist.rounding_amt,
14936 		l_self_assess_rev_tax_dist.charge_applicable_to_dist_id,
14937 		l_self_assess_rev_tax_dist.corrected_quantity,
14938 		l_self_assess_rev_tax_dist.related_id,
14939 		l_self_assess_rev_tax_dist.asset_book_type_code,
14940 		l_self_assess_rev_tax_dist.asset_category_id,
14941 		l_self_assess_rev_tax_dist.distribution_class,
14942 		l_self_assess_rev_tax_dist.tax_code_id,
14943 		l_self_assess_rev_tax_dist.intended_use,
14944 		l_self_assess_rev_tax_dist.detail_tax_dist_id,
14945 		l_self_assess_rev_tax_dist.rec_nrec_rate,
14946 		l_self_assess_rev_tax_dist.recovery_rate_id,
14947 		l_self_assess_rev_tax_dist.recovery_type_code,
14948 		l_self_assess_rev_tax_dist.withholding_tax_code_id,
14949 		l_self_assess_rev_tax_dist.taxable_amount,
14950 		l_self_assess_rev_tax_dist.taxable_base_amount,
14951 		l_self_assess_rev_tax_dist.tax_already_distributed_flag,
14952 		l_self_assess_rev_tax_dist.summary_tax_line_id,
14953 		l_self_assess_rev_tax_dist.rcv_charge_addition_flag,
14954 		l_self_assess_rev_tax_dist.self_assessed_flag,
14955 		l_self_assess_rev_tax_dist.self_assessed_tax_liab_ccid,  --bug6805655
14956         l_self_assess_rev_tax_dist.prepay_tax_diff_amount -- BUG 7338249
14957                );
14958 
14959         --Bug 8350132
14960 
14961 	INSERT into ZX_TAX_DIST_ID_GT (TAX_DIST_ID) values (l_self_assess_rev_tax_dist.detail_tax_dist_id) ;
14962         l_inv_cancel_date := l_self_assess_rev_tax_dist.accounting_date ;
14963 
14964         --End Bug 8350132
14965 
14966          END LOOP;
14967 
14968          l_rev_sa_dist_cursor_count := c_rev_self_assess_tax_dist%ROWCOUNT  ;  -- Bug 10140793
14969 
14970          CLOSE c_rev_self_assess_tax_dist;
14971 
14972          -- Bug 10140793 Begin
14973          -----------------------------------------------------------------
14974          IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
14975            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, '(1) l_rev_sa_dist_cursor_count: ' || l_rev_sa_dist_cursor_count);
14976          END IF;
14977          -----------------------------------------------------------------
14978 
14979          IF ( l_rev_sa_dist_count <> 0 AND l_rev_sa_dist_cursor_count = 0 ) THEN
14980             FND_MESSAGE.SET_NAME('SQLAP','AP_ERR_TAX_DIST_SYNC');
14981             APP_EXCEPTION.RAISE_EXCEPTION;
14982          END IF ;
14983          -- Bug 10140793  End
14984 
14985 	 END IF; -- Marker 6
14986 
14987         --Bug 8350132
14988         --bug 13101979 moved the API to the end of if block so that for all it wil update once hence removing redundancy
14989        /* ZX_API_PUB.Update_Tax_dist_gl_date (
14990 				1.0,
14991 				FND_API.G_TRUE,
14992 				FND_API.G_FALSE,
14993 				FND_API.G_VALID_LEVEL_FULL,
14994 				l_return_status_service,
14995 				l_msg_count,
14996 				l_msg_data,
14997 				l_inv_cancel_date );
14998 
14999 
15000         IF (l_return_status_service <> FND_API.G_RET_STS_SUCCESS) THEN  -- handle errors
15001 
15002              l_return_status := FALSE;
15003 
15004              -----------------------------------------------------------------
15005              l_debug_info := 'Step 2.5: Handle errors returned by API';
15006              -----------------------------------------------------------------
15007              IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
15008                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
15009              END IF;
15010 
15011              IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
15012                    P_All_Error_Messages  => 'N',
15013                    P_Msg_Count           => l_msg_count,
15014                    P_Msg_Data            => l_msg_data,
15015                    P_Error_Code          => l_Error_Code,
15016                    P_Calling_Sequence    => l_curr_calling_sequence)) THEN
15017                NULL;
15018              END IF;
15019 
15020              RETURN l_return_status;
15021          END IF; */
15022 
15023        --End Bug 8350132
15024 
15025     else -- Marker 4
15026 
15027          IF l_tax_distributions_exist THEN -- Marker 7
15028 
15029          l_debug_info := 'Inserting reverse entries into ap_invoice_distributions_all after line discard';
15030          IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
15031           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
15032          END IF;
15033 
15034          OPEN c_reverse_tax_dist_1;
15035          LOOP
15036 	     FETCH c_reverse_tax_dist_1
15037 	     INTO  l_reverse_tax_dist;
15038 	     EXIT WHEN c_reverse_tax_dist_1%NOTFOUND;
15039 
15040 	     INSERT INTO ap_invoice_distributions_all (
15041             accounting_date,
15042             accrual_posted_flag,
15043             assets_addition_flag,
15044             assets_tracking_flag,
15045             cash_posted_flag,
15046             distribution_line_number,
15047             dist_code_combination_id,
15048             invoice_id,
15049             last_updated_by,
15050             last_update_date,
15051             line_type_lookup_code,
15052             period_name,
15053             set_of_books_id,
15054             amount,
15055             base_amount,
15056             --batch_id,
15057             created_by,
15058             creation_date,
15059             description,
15060             final_match_flag,
15061             income_tax_region,
15062             last_update_login,
15063             match_status_flag,
15064             posted_flag,
15065             po_distribution_id,
15066             program_application_id,
15067             program_id,
15068             program_update_date,
15069             quantity_invoiced,
15070             request_id,
15071             reversal_flag,
15072             type_1099,
15073             unit_price,
15074             encumbered_flag,
15075             stat_amount,
15076             attribute1,
15077             attribute10,
15078             attribute11,
15079             attribute12,
15080             attribute13,
15081             attribute14,
15082             attribute15,
15083             attribute2,
15084             attribute3,
15085             attribute4,
15086             attribute5,
15087             attribute6,
15088             attribute7,
15089             attribute8,
15090             attribute9,
15091             attribute_category,
15092             expenditure_item_date,
15093             expenditure_organization_id,
15094             expenditure_type,
15095             parent_invoice_id,
15096             pa_addition_flag,
15097             pa_quantity,
15098             prepay_amount_remaining,
15099             project_accounting_context,
15100             project_id,
15101             task_id,
15102             packet_id,
15103             awt_flag,
15104             awt_group_id,
15105             awt_tax_rate_id,
15106             awt_gross_amount,
15107             awt_invoice_id,
15108             awt_origin_group_id,
15109             reference_1,
15110             reference_2,
15111             org_id,
15112             awt_invoice_payment_id,
15113             global_attribute_category,
15114             global_attribute1,
15115             global_attribute2,
15116             global_attribute3,
15117             global_attribute4,
15118             global_attribute5,
15119             global_attribute6,
15120             global_attribute7,
15121             global_attribute8,
15122             global_attribute9,
15123             global_attribute10,
15124             global_attribute11,
15125             global_attribute12,
15126             global_attribute13,
15127             global_attribute14,
15128             global_attribute15,
15129             global_attribute16,
15130             global_attribute17,
15131             global_attribute18,
15132             global_attribute19,
15133             global_attribute20,
15134             receipt_verified_flag,
15135             receipt_required_flag,
15136             receipt_missing_flag,
15137             justification,
15138             expense_group,
15139             start_expense_date,
15140             end_expense_date,
15141             receipt_currency_code,
15142             receipt_conversion_rate,
15143             receipt_currency_amount,
15144             daily_amount,
15145             web_parameter_id,
15146             adjustment_reason,
15147             award_id,
15148             credit_card_trx_id,
15149             dist_match_type,
15150             rcv_transaction_id,
15151             invoice_distribution_id,
15152             parent_reversal_id,
15153             tax_recoverable_flag,
15154             merchant_document_number,
15155             merchant_name,
15156             merchant_reference,
15157             merchant_tax_reg_number,
15158             merchant_taxpayer_id,
15159             country_of_supply,
15160             matched_uom_lookup_code,
15161             gms_burdenable_raw_cost,
15162             accounting_event_id,
15163             prepay_distribution_id,
15164             upgrade_posted_amt,
15165             upgrade_base_posted_amt,
15166             inventory_transfer_status,
15167             company_prepaid_invoice_id,
15168             cc_reversal_flag,
15169             awt_withheld_amt,
15170             pa_cmt_xface_flag,
15171             cancellation_flag,
15172             invoice_line_number,
15173             corrected_invoice_dist_id,
15174             rounding_amt,
15175             charge_applicable_to_dist_id,
15176             corrected_quantity,
15177             related_id,
15178             asset_book_type_code,
15179             asset_category_id,
15180             distribution_class,
15181             tax_code_id,
15182             intended_use,
15183             detail_tax_dist_id,
15184             rec_nrec_rate,
15185             recovery_rate_id,
15186             recovery_type_code,
15187             withholding_tax_code_id,
15188             taxable_amount,
15189             taxable_base_amount,
15190             tax_already_distributed_flag,
15191             summary_tax_line_id,
15192 	        rcv_charge_addition_flag,
15193             prepay_tax_diff_amount, -- BUG 7338249
15194             pay_awt_group_id) /*BUG 10261626*/
15195 	VALUES
15196 	(
15197 	l_reverse_tax_dist.accounting_date,
15198 	l_reverse_tax_dist.accrual_posted_flag,
15199 	l_reverse_tax_dist.assets_addition_flag,
15200 	l_reverse_tax_dist.assets_tracking_flag,
15201 	l_reverse_tax_dist.cash_posted_flag,
15202 	l_reverse_tax_dist.distribution_line_number,
15203 	l_reverse_tax_dist.dist_code_combination_id,
15204 	l_reverse_tax_dist.invoice_id,
15205 	l_reverse_tax_dist.last_updated_by,
15206 	l_reverse_tax_dist.last_update_date,
15207 	l_reverse_tax_dist.line_type_lookup_code,
15208 	l_reverse_tax_dist.period_name,
15209 	l_reverse_tax_dist.set_of_books_id,
15210 	l_reverse_tax_dist.amount,
15211 	l_reverse_tax_dist.base_amount,
15212 	--l_reverse_tax_dist.batch_id,
15213 	l_reverse_tax_dist.created_by,
15214 	l_reverse_tax_dist.creation_date,
15215 	l_reverse_tax_dist.description,
15216 	l_reverse_tax_dist.final_match_flag,
15217 	l_reverse_tax_dist.income_tax_region,
15218 	l_reverse_tax_dist.last_update_login,
15219 	l_reverse_tax_dist.match_status_flag,
15220 	l_reverse_tax_dist.posted_flag,
15221 	l_reverse_tax_dist.po_distribution_id,
15222 	l_reverse_tax_dist.program_application_id,
15223 	l_reverse_tax_dist.program_id,
15224 	l_reverse_tax_dist.program_update_date,
15225 	l_reverse_tax_dist.quantity_invoiced,
15226 	l_reverse_tax_dist.request_id,
15227 	l_reverse_tax_dist.reversal_flag,
15228 	l_reverse_tax_dist.type_1099,
15229 	l_reverse_tax_dist.unit_price,
15230 	l_reverse_tax_dist.encumbered_flag,
15231 	l_reverse_tax_dist.stat_amount,
15232 	l_reverse_tax_dist.attribute1,
15233 	l_reverse_tax_dist.attribute10,
15234 	l_reverse_tax_dist.attribute11,
15235 	l_reverse_tax_dist.attribute12,
15236 	l_reverse_tax_dist.attribute13,
15237 	l_reverse_tax_dist.attribute14,
15238 	l_reverse_tax_dist.attribute15,
15239 	l_reverse_tax_dist.attribute2,
15240 	l_reverse_tax_dist.attribute3,
15241 	l_reverse_tax_dist.attribute4,
15242 	l_reverse_tax_dist.attribute5,
15243 	l_reverse_tax_dist.attribute6,
15244 	l_reverse_tax_dist.attribute7,
15245 	l_reverse_tax_dist.attribute8,
15246 	l_reverse_tax_dist.attribute9,
15247 	l_reverse_tax_dist.attribute_category,
15248 	l_reverse_tax_dist.expenditure_item_date,
15249 	l_reverse_tax_dist.expenditure_organization_id,
15250 	l_reverse_tax_dist.expenditure_type,
15251 	l_reverse_tax_dist.parent_invoice_id,
15252 	l_reverse_tax_dist.pa_addition_flag,
15253 	l_reverse_tax_dist.pa_quantity,
15254 	l_reverse_tax_dist.prepay_amount_remaining,
15255 	l_reverse_tax_dist.project_accounting_context,
15256 	l_reverse_tax_dist.project_id,
15257 	l_reverse_tax_dist.task_id,
15258 	l_reverse_tax_dist.packet_id,
15259 	l_reverse_tax_dist.awt_flag,
15260 	l_reverse_tax_dist.awt_group_id,
15261 	l_reverse_tax_dist.awt_tax_rate_id,
15262 	l_reverse_tax_dist.awt_gross_amount,
15263 	l_reverse_tax_dist.awt_invoice_id,
15264 	l_reverse_tax_dist.awt_origin_group_id,
15265 	l_reverse_tax_dist.reference_1,
15266 	l_reverse_tax_dist.reference_2,
15267 	l_reverse_tax_dist.org_id,
15268 	l_reverse_tax_dist.awt_invoice_payment_id,
15269 	l_reverse_tax_dist.global_attribute_category,
15270 	l_reverse_tax_dist.global_attribute1,
15271 	l_reverse_tax_dist.global_attribute2,
15272 	l_reverse_tax_dist.global_attribute3,
15273 	l_reverse_tax_dist.global_attribute4,
15274 	l_reverse_tax_dist.global_attribute5,
15275 	l_reverse_tax_dist.global_attribute6,
15276 	l_reverse_tax_dist.global_attribute7,
15277 	l_reverse_tax_dist.global_attribute8,
15278 	l_reverse_tax_dist.global_attribute9,
15279 	l_reverse_tax_dist.global_attribute10,
15280 	l_reverse_tax_dist.global_attribute11,
15281 	l_reverse_tax_dist.global_attribute12,
15282 	l_reverse_tax_dist.global_attribute13,
15283 	l_reverse_tax_dist.global_attribute14,
15284 	l_reverse_tax_dist.global_attribute15,
15285 	l_reverse_tax_dist.global_attribute16,
15286 	l_reverse_tax_dist.global_attribute17,
15287 	l_reverse_tax_dist.global_attribute18,
15288 	l_reverse_tax_dist.global_attribute19,
15289 	l_reverse_tax_dist.global_attribute20,
15290 	l_reverse_tax_dist.receipt_verified_flag,
15291 	l_reverse_tax_dist.receipt_required_flag,
15292 	l_reverse_tax_dist.receipt_missing_flag,
15293 	l_reverse_tax_dist.justification,
15294 	l_reverse_tax_dist.expense_group,
15295 	l_reverse_tax_dist.start_expense_date,
15296 	l_reverse_tax_dist.end_expense_date,
15297 	l_reverse_tax_dist.receipt_currency_code,
15298 	l_reverse_tax_dist.receipt_conversion_rate,
15302 	l_reverse_tax_dist.adjustment_reason,
15299 	l_reverse_tax_dist.receipt_currency_amount,
15300 	l_reverse_tax_dist.daily_amount,
15301 	l_reverse_tax_dist.web_parameter_id,
15303 	l_reverse_tax_dist.award_id,
15304 	l_reverse_tax_dist.credit_card_trx_id,
15305 	l_reverse_tax_dist.dist_match_type,
15306 	l_reverse_tax_dist.rcv_transaction_id,
15307 	l_reverse_tax_dist.invoice_distribution_id,
15308 	l_reverse_tax_dist.parent_reversal_id,
15309 	l_reverse_tax_dist.tax_recoverable_flag,
15310 	l_reverse_tax_dist.merchant_document_number,
15311 	l_reverse_tax_dist.merchant_name,
15312 	l_reverse_tax_dist.merchant_reference,
15313 	l_reverse_tax_dist.merchant_tax_reg_number,
15314 	l_reverse_tax_dist.merchant_taxpayer_id,
15315 	l_reverse_tax_dist.country_of_supply,
15316 	l_reverse_tax_dist.matched_uom_lookup_code,
15317 	l_reverse_tax_dist.gms_burdenable_raw_cost,
15318 	l_reverse_tax_dist.accounting_event_id,
15319 	l_reverse_tax_dist.prepay_distribution_id,
15320 	l_reverse_tax_dist.upgrade_posted_amt,
15321 	l_reverse_tax_dist.upgrade_base_posted_amt,
15322 	l_reverse_tax_dist.inventory_transfer_status,
15323 	l_reverse_tax_dist.company_prepaid_invoice_id,
15324 	l_reverse_tax_dist.cc_reversal_flag,
15325 	l_reverse_tax_dist.awt_withheld_amt,
15326 	l_reverse_tax_dist.pa_cmt_xface_flag,
15327 	l_reverse_tax_dist.cancellation_flag,
15328 	l_reverse_tax_dist.invoice_line_number,
15329 	l_reverse_tax_dist.corrected_invoice_dist_id,
15330 	l_reverse_tax_dist.rounding_amt,
15331 	l_reverse_tax_dist.charge_applicable_to_dist_id,
15332 	l_reverse_tax_dist.corrected_quantity,
15333 	l_reverse_tax_dist.related_id,
15334 	l_reverse_tax_dist.asset_book_type_code,
15335 	l_reverse_tax_dist.asset_category_id,
15336 	l_reverse_tax_dist.distribution_class,
15337 	l_reverse_tax_dist.tax_code_id,
15338 	l_reverse_tax_dist.intended_use,
15339 	l_reverse_tax_dist.detail_tax_dist_id,
15340 	l_reverse_tax_dist.rec_nrec_rate,
15341 	l_reverse_tax_dist.recovery_rate_id,
15342 	l_reverse_tax_dist.recovery_type_code,
15343 	l_reverse_tax_dist.withholding_tax_code_id,
15344 	l_reverse_tax_dist.taxable_amount,
15345 	l_reverse_tax_dist.taxable_base_amount,
15346 	l_reverse_tax_dist.tax_already_distributed_flag,
15347 	l_reverse_tax_dist.summary_tax_line_id,
15348 	l_reverse_tax_dist.rcv_charge_addition_flag,
15349 	l_reverse_tax_dist.prepay_tax_diff_amount, -- BUG 7338249
15350     l_reverse_tax_dist.pay_awt_group_id); /*BUG 10261626*/
15351 
15352     INSERT into ZX_TAX_DIST_ID_GT (TAX_DIST_ID) values (l_reverse_tax_dist.detail_tax_dist_id) ; --bug 13101979
15353     l_inv_cancel_date := l_reverse_tax_dist.accounting_date; --bug 13101979
15354 
15355      END LOOP;
15356 
15357      l_rev_dist_cursor_count := c_reverse_tax_dist_1%ROWCOUNT  ;  -- Bug 10140793
15358      CLOSE c_reverse_tax_dist_1;
15359 
15360      -- Bug 10140793 Begin
15361      -----------------------------------------------------------------
15362      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
15363        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, '(2) l_rev_dist_cursor_count: ' || l_rev_dist_cursor_count);
15364      END IF;
15365      -----------------------------------------------------------------
15366 
15367      IF ( l_reverse_dist_count <> 0 AND l_rev_dist_cursor_count = 0 ) THEN
15368         FND_MESSAGE.SET_NAME('SQLAP','AP_ERR_TAX_DIST_SYNC');
15369         APP_EXCEPTION.RAISE_EXCEPTION;
15370      END IF ;
15371      -- Bug 10140793  End
15372 
15373     END IF; -- Marker 7
15374 
15375 
15376     IF l_self_assess_tax_dist_exist THEN -- Marker 8
15377 
15378        l_debug_info := 'Inserting reverse entries into ap_self_assessed_tax_dist_all';
15379        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
15380           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
15381        END IF;
15382 
15383        OPEN c_rev_self_assess_tax_dist_1;
15384        LOOP
15385            FETCH c_rev_self_assess_tax_dist_1
15386 	    INTO  l_self_assess_rev_tax_dist_1;
15387 	    EXIT WHEN c_rev_self_assess_tax_dist_1%NOTFOUND;
15388 
15389 	   INSERT INTO ap_self_assessed_tax_dist_all (
15390                 accounting_date,
15391                 accrual_posted_flag,
15392                 assets_addition_flag,
15393                 assets_tracking_flag,
15394                 cash_posted_flag,
15395                 distribution_line_number,
15396                 dist_code_combination_id,
15397                 invoice_id,
15398                 last_updated_by,
15399                 last_update_date,
15400                 line_type_lookup_code,
15401                 period_name,
15402                 set_of_books_id,
15403                 amount,
15404                 base_amount,
15405                 --batch_id,
15406                 created_by,
15407                 creation_date,
15408                 description,
15409                 final_match_flag,
15410                 income_tax_region,
15411                 last_update_login,
15412                 match_status_flag,
15413                 posted_flag,
15414                 po_distribution_id,
15415                 program_application_id,
15416                 program_id,
15417                 program_update_date,
15418                 quantity_invoiced,
15419                 request_id,
15420                 reversal_flag,
15421                 type_1099,
15422                 unit_price,
15423                 encumbered_flag,
15424                 stat_amount,
15425                 attribute1,
15426                 attribute10,
15427                 attribute11,
15428                 attribute12,
15429                 attribute13,
15430                 attribute14,
15431                 attribute15,
15432                 attribute2,
15433                 attribute3,
15434                 attribute4,
15435                 attribute5,
15436                 attribute6,
15437                 attribute7,
15438                 attribute8,
15439                 attribute9,
15440                 attribute_category,
15441                 expenditure_item_date,
15442                 expenditure_organization_id,
15443                 expenditure_type,
15444                 parent_invoice_id,
15445                 pa_addition_flag,
15446                 pa_quantity,
15447                 prepay_amount_remaining,
15448                 project_accounting_context,
15449                 project_id,
15450                 task_id,
15451                 packet_id,
15452                 awt_flag,
15453                 awt_group_id,
15454                 awt_tax_rate_id,
15455                 awt_gross_amount,
15456                 awt_invoice_id,
15457                 awt_origin_group_id,
15458                 reference_1,
15459                 reference_2,
15460                 org_id,
15461                 awt_invoice_payment_id,
15462                 global_attribute_category,
15463                 global_attribute1,
15464                 global_attribute2,
15465                 global_attribute3,
15466                 global_attribute4,
15467                 global_attribute5,
15468                 global_attribute6,
15469                 global_attribute7,
15470                 global_attribute8,
15471                 global_attribute9,
15472                 global_attribute10,
15473                 global_attribute11,
15474                 global_attribute12,
15475                 global_attribute13,
15476                 global_attribute14,
15477                 global_attribute15,
15478                 global_attribute16,
15479                 global_attribute17,
15480                 global_attribute18,
15481                 global_attribute19,
15482                 global_attribute20,
15483                 receipt_verified_flag,
15484                 receipt_required_flag,
15485                 receipt_missing_flag,
15486                 justification,
15487                 expense_group,
15488                 start_expense_date,
15489                 end_expense_date,
15490                 receipt_currency_code,
15491                 receipt_conversion_rate,
15492                 receipt_currency_amount,
15493                 daily_amount,
15494                 web_parameter_id,
15495                 adjustment_reason,
15496                 award_id,
15497                 credit_card_trx_id,
15498                 dist_match_type,
15499                 rcv_transaction_id,
15500                 invoice_distribution_id,
15501                 parent_reversal_id,
15502                 tax_recoverable_flag,
15503                 merchant_document_number,
15504                 merchant_name,
15505                 merchant_reference,
15506                 merchant_tax_reg_number,
15507                 merchant_taxpayer_id,
15508                 country_of_supply,
15509                 matched_uom_lookup_code,
15510                 gms_burdenable_raw_cost,
15511                 accounting_event_id,
15512                 prepay_distribution_id,
15513                 upgrade_posted_amt,
15514                 upgrade_base_posted_amt,
15515                 inventory_transfer_status,
15516                 company_prepaid_invoice_id,
15517                 cc_reversal_flag,
15518                 awt_withheld_amt,
15519                 pa_cmt_xface_flag,
15520                 cancellation_flag,
15521                 invoice_line_number,
15522                 corrected_invoice_dist_id,
15523                 rounding_amt,
15524                 charge_applicable_to_dist_id,
15525                 corrected_quantity,
15526                 related_id,
15527                 asset_book_type_code,
15528                 asset_category_id,
15529                 distribution_class,
15530                 tax_code_id,
15531                 intended_use,
15532                 detail_tax_dist_id,
15533                 rec_nrec_rate,
15534                 recovery_rate_id,
15535                 recovery_type_code,
15536                 withholding_tax_code_id,
15537                 taxable_amount,
15538                 taxable_base_amount,
15539                 tax_already_distributed_flag,
15540                 summary_tax_line_id,
15541 		        rcv_charge_addition_flag,
15542                 self_assessed_flag,
15543                 self_assessed_tax_liab_ccid,  --bug6805655
15544                 prepay_tax_diff_amount -- BUG 7338249
15545                 )
15546 		VALUES
15547 		(
15548 		l_self_assess_rev_tax_dist_1.accounting_date,
15549 		l_self_assess_rev_tax_dist_1.accrual_posted_flag,
15550 		l_self_assess_rev_tax_dist_1.assets_addition_flag,
15551 		l_self_assess_rev_tax_dist_1.assets_tracking_flag,
15552 		l_self_assess_rev_tax_dist_1.cash_posted_flag,
15553 		l_self_assess_rev_tax_dist_1.distribution_line_number,
15554 		l_self_assess_rev_tax_dist_1.dist_code_combination_id,
15555 		l_self_assess_rev_tax_dist_1.invoice_id,
15556 		l_self_assess_rev_tax_dist_1.last_updated_by,
15557 		l_self_assess_rev_tax_dist_1.last_update_date,
15558 		l_self_assess_rev_tax_dist_1.line_type_lookup_code,
15559 		l_self_assess_rev_tax_dist_1.period_name,
15560 		l_self_assess_rev_tax_dist_1.set_of_books_id,
15561 		l_self_assess_rev_tax_dist_1.amount,
15562 		l_self_assess_rev_tax_dist_1.base_amount,
15563 		--l_self_assess_rev_tax_dist_1.batch_id,
15564 		l_self_assess_rev_tax_dist_1.created_by,
15565 		l_self_assess_rev_tax_dist_1.creation_date,
15566 		l_self_assess_rev_tax_dist_1.description,
15567 		l_self_assess_rev_tax_dist_1.final_match_flag,
15568 		l_self_assess_rev_tax_dist_1.income_tax_region,
15569 		l_self_assess_rev_tax_dist_1.last_update_login,
15570 		l_self_assess_rev_tax_dist_1.match_status_flag,
15571 		l_self_assess_rev_tax_dist_1.posted_flag,
15572 		l_self_assess_rev_tax_dist_1.po_distribution_id,
15573 		l_self_assess_rev_tax_dist_1.program_application_id,
15574 		l_self_assess_rev_tax_dist_1.program_id,
15575 		l_self_assess_rev_tax_dist_1.program_update_date,
15576 		l_self_assess_rev_tax_dist_1.quantity_invoiced,
15577 		l_self_assess_rev_tax_dist_1.request_id,
15578 		l_self_assess_rev_tax_dist_1.reversal_flag,
15579 		l_self_assess_rev_tax_dist_1.type_1099,
15580 		l_self_assess_rev_tax_dist_1.unit_price,
15581 		l_self_assess_rev_tax_dist_1.encumbered_flag,
15582 		l_self_assess_rev_tax_dist_1.stat_amount,
15583 		l_self_assess_rev_tax_dist_1.attribute1,
15584 		l_self_assess_rev_tax_dist_1.attribute10,
15585 		l_self_assess_rev_tax_dist_1.attribute11,
15586 		l_self_assess_rev_tax_dist_1.attribute12,
15587 		l_self_assess_rev_tax_dist_1.attribute13,
15588 		l_self_assess_rev_tax_dist_1.attribute14,
15589 		l_self_assess_rev_tax_dist_1.attribute15,
15590 		l_self_assess_rev_tax_dist_1.attribute2,
15591 		l_self_assess_rev_tax_dist_1.attribute3,
15592 		l_self_assess_rev_tax_dist_1.attribute4,
15593 		l_self_assess_rev_tax_dist_1.attribute5,
15594 		l_self_assess_rev_tax_dist_1.attribute6,
15595 		l_self_assess_rev_tax_dist_1.attribute7,
15596 		l_self_assess_rev_tax_dist_1.attribute8,
15597 		l_self_assess_rev_tax_dist_1.attribute9,
15598 		l_self_assess_rev_tax_dist_1.attribute_category,
15599 		l_self_assess_rev_tax_dist_1.expenditure_item_date,
15600 		l_self_assess_rev_tax_dist_1.expenditure_organization_id,
15601 		l_self_assess_rev_tax_dist_1.expenditure_type,
15602 		l_self_assess_rev_tax_dist_1.parent_invoice_id,
15603 		l_self_assess_rev_tax_dist_1.pa_addition_flag,
15604 		l_self_assess_rev_tax_dist_1.pa_quantity,
15605 		l_self_assess_rev_tax_dist_1.prepay_amount_remaining,
15606 		l_self_assess_rev_tax_dist_1.project_accounting_context,
15607 		l_self_assess_rev_tax_dist_1.project_id,
15608 		l_self_assess_rev_tax_dist_1.task_id,
15609 		l_self_assess_rev_tax_dist_1.packet_id,
15610 		l_self_assess_rev_tax_dist_1.awt_flag,
15611 		l_self_assess_rev_tax_dist_1.awt_group_id,
15612 		l_self_assess_rev_tax_dist_1.awt_tax_rate_id,
15613 		l_self_assess_rev_tax_dist_1.awt_gross_amount,
15614 		l_self_assess_rev_tax_dist_1.awt_invoice_id,
15615 		l_self_assess_rev_tax_dist_1.awt_origin_group_id,
15616 		l_self_assess_rev_tax_dist_1.reference_1,
15617 		l_self_assess_rev_tax_dist_1.reference_2,
15618 		l_self_assess_rev_tax_dist_1.org_id,
15619 		l_self_assess_rev_tax_dist_1.awt_invoice_payment_id,
15620 		l_self_assess_rev_tax_dist_1.global_attribute_category,
15621 		l_self_assess_rev_tax_dist_1.global_attribute1,
15622 		l_self_assess_rev_tax_dist_1.global_attribute2,
15623 		l_self_assess_rev_tax_dist_1.global_attribute3,
15624 		l_self_assess_rev_tax_dist_1.global_attribute4,
15625 		l_self_assess_rev_tax_dist_1.global_attribute5,
15626 		l_self_assess_rev_tax_dist_1.global_attribute6,
15627 		l_self_assess_rev_tax_dist_1.global_attribute7,
15628 		l_self_assess_rev_tax_dist_1.global_attribute8,
15629 		l_self_assess_rev_tax_dist_1.global_attribute9,
15630 		l_self_assess_rev_tax_dist_1.global_attribute10,
15631 		l_self_assess_rev_tax_dist_1.global_attribute11,
15632 		l_self_assess_rev_tax_dist_1.global_attribute12,
15633 		l_self_assess_rev_tax_dist_1.global_attribute13,
15634 		l_self_assess_rev_tax_dist_1.global_attribute14,
15635 		l_self_assess_rev_tax_dist_1.global_attribute15,
15636 		l_self_assess_rev_tax_dist_1.global_attribute16,
15637 		l_self_assess_rev_tax_dist_1.global_attribute17,
15638 		l_self_assess_rev_tax_dist_1.global_attribute18,
15639 		l_self_assess_rev_tax_dist_1.global_attribute19,
15640 		l_self_assess_rev_tax_dist_1.global_attribute20,
15641 		l_self_assess_rev_tax_dist_1.receipt_verified_flag,
15642 		l_self_assess_rev_tax_dist_1.receipt_required_flag,
15643 		l_self_assess_rev_tax_dist_1.receipt_missing_flag,
15644 		l_self_assess_rev_tax_dist_1.justification,
15645 		l_self_assess_rev_tax_dist_1.expense_group,
15646 		l_self_assess_rev_tax_dist_1.start_expense_date,
15647 		l_self_assess_rev_tax_dist_1.end_expense_date,
15648 		l_self_assess_rev_tax_dist_1.receipt_currency_code,
15649 		l_self_assess_rev_tax_dist_1.receipt_conversion_rate,
15650 		l_self_assess_rev_tax_dist_1.receipt_currency_amount,
15651 		l_self_assess_rev_tax_dist_1.daily_amount,
15652 		l_self_assess_rev_tax_dist_1.web_parameter_id,
15653 		l_self_assess_rev_tax_dist_1.adjustment_reason,
15654 		l_self_assess_rev_tax_dist_1.award_id,
15655 		l_self_assess_rev_tax_dist_1.credit_card_trx_id,
15656 		l_self_assess_rev_tax_dist_1.dist_match_type,
15657 		l_self_assess_rev_tax_dist_1.rcv_transaction_id,
15658 		l_self_assess_rev_tax_dist_1.invoice_distribution_id,
15659 		l_self_assess_rev_tax_dist_1.parent_reversal_id,
15660 		l_self_assess_rev_tax_dist_1.tax_recoverable_flag,
15661 		l_self_assess_rev_tax_dist_1.merchant_document_number,
15662 		l_self_assess_rev_tax_dist_1.merchant_name,
15663 		l_self_assess_rev_tax_dist_1.merchant_reference,
15664 		l_self_assess_rev_tax_dist_1.merchant_tax_reg_number,
15665 		l_self_assess_rev_tax_dist_1.merchant_taxpayer_id,
15666 		l_self_assess_rev_tax_dist_1.country_of_supply,
15667 		l_self_assess_rev_tax_dist_1.matched_uom_lookup_code,
15668 		l_self_assess_rev_tax_dist_1.gms_burdenable_raw_cost,
15669 		l_self_assess_rev_tax_dist_1.accounting_event_id,
15670 		l_self_assess_rev_tax_dist_1.prepay_distribution_id,
15671 		l_self_assess_rev_tax_dist_1.upgrade_posted_amt,
15672 		l_self_assess_rev_tax_dist_1.upgrade_base_posted_amt,
15673 		l_self_assess_rev_tax_dist_1.inventory_transfer_status,
15674 		l_self_assess_rev_tax_dist_1.company_prepaid_invoice_id,
15675 		l_self_assess_rev_tax_dist_1.cc_reversal_flag,
15676 		l_self_assess_rev_tax_dist_1.awt_withheld_amt,
15677 		l_self_assess_rev_tax_dist_1.pa_cmt_xface_flag,
15678 		l_self_assess_rev_tax_dist_1.cancellation_flag,
15679 		l_self_assess_rev_tax_dist_1.invoice_line_number,
15680 		l_self_assess_rev_tax_dist_1.corrected_invoice_dist_id,
15681 		l_self_assess_rev_tax_dist_1.rounding_amt,
15682 		l_self_assess_rev_tax_dist_1.charge_applicable_to_dist_id,
15683 		l_self_assess_rev_tax_dist_1.corrected_quantity,
15684 		l_self_assess_rev_tax_dist_1.related_id,
15685 		l_self_assess_rev_tax_dist_1.asset_book_type_code,
15686 		l_self_assess_rev_tax_dist_1.asset_category_id,
15687 		l_self_assess_rev_tax_dist_1.distribution_class,
15688 		l_self_assess_rev_tax_dist_1.tax_code_id,
15689 		l_self_assess_rev_tax_dist_1.intended_use,
15690 		l_self_assess_rev_tax_dist_1.detail_tax_dist_id,
15691 		l_self_assess_rev_tax_dist_1.rec_nrec_rate,
15692 		l_self_assess_rev_tax_dist_1.recovery_rate_id,
15693 		l_self_assess_rev_tax_dist_1.recovery_type_code,
15694 		l_self_assess_rev_tax_dist_1.withholding_tax_code_id,
15695 		l_self_assess_rev_tax_dist_1.taxable_amount,
15696 		l_self_assess_rev_tax_dist_1.taxable_base_amount,
15697 		l_self_assess_rev_tax_dist_1.tax_already_distributed_flag,
15698 		l_self_assess_rev_tax_dist_1.summary_tax_line_id,
15699 		l_self_assess_rev_tax_dist_1.rcv_charge_addition_flag,
15700 		l_self_assess_rev_tax_dist_1.self_assessed_flag,
15701  		l_self_assess_rev_tax_dist_1.self_assessed_tax_liab_ccid,   --bug6805655
15702         l_self_assess_rev_tax_dist_1.prepay_tax_diff_amount -- BUG 7338249
15703         );
15704 
15705 	INSERT into ZX_TAX_DIST_ID_GT (TAX_DIST_ID) values (l_self_assess_rev_tax_dist_1.detail_tax_dist_id) ; --bug 13101979
15706 	l_inv_cancel_date := l_self_assess_rev_tax_dist_1.accounting_date ; --bug 13101979
15707 
15708          END LOOP;
15709 
15710          l_rev_sa_dist_cursor_count := c_rev_self_assess_tax_dist_1%ROWCOUNT  ;  -- Bug 10140793
15711 
15712          CLOSE c_rev_self_assess_tax_dist_1;
15713 
15714          -- Bug 10140793 Begin
15715          -----------------------------------------------------------------
15716          IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
15717            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, '(2) l_rev_sa_dist_cursor_count: ' || l_rev_sa_dist_cursor_count);
15718          END IF;
15719          -----------------------------------------------------------------
15720 
15721          IF ( l_rev_sa_dist_count <> 0 AND l_rev_sa_dist_cursor_count = 0 ) THEN
15722             FND_MESSAGE.SET_NAME('SQLAP','AP_ERR_TAX_DIST_SYNC');
15723             APP_EXCEPTION.RAISE_EXCEPTION;
15724          END IF ;
15725          -- Bug 10140793  End
15726 
15727 	 END IF; -- Marker 8
15728 
15729     END IF; -- Marker 4
15730 
15731        --bug 13101979 moved the API here, the end of if block so that for all it will update once hence removing redundancy
15732         ZX_API_PUB.Update_Tax_dist_gl_date (
15733 				1.0,
15734 				FND_API.G_TRUE,
15735 				FND_API.G_FALSE,
15736 				FND_API.G_VALID_LEVEL_FULL,
15737 				l_return_status_service,
15738 				l_msg_count,
15739 				l_msg_data,
15740 				l_inv_cancel_date );
15741 
15742         IF (l_return_status_service <> FND_API.G_RET_STS_SUCCESS) THEN  -- handle errors
15743 
15744              l_return_status := FALSE;
15745 
15746              -----------------------------------------------------------------
15747              l_debug_info := 'Step 2.5: Handle errors returned by API';
15748              -----------------------------------------------------------------
15749              IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
15750                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, l_debug_info);
15751              END IF;
15752 
15753              IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
15754                    P_All_Error_Messages  => 'N',
15755                    P_Msg_Count           => l_msg_count,
15756                    P_Msg_Data            => l_msg_data,
15757                    P_Error_Code          => l_Error_Code,
15758                    P_Calling_Sequence    => l_curr_calling_sequence)) THEN
15759                NULL;
15760              END IF;
15761 
15762              RETURN l_return_status;
15763          END IF;
15764         --bug 13101979
15765 
15766 
15767     IF l_tax_distributions_exist THEN
15768 
15769         -----------------------------------------------------------------
15770         l_debug_info := 'Step 4: Update reversal_flag';
15771         -----------------------------------------------------------------
15772         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
15773             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
15774         END IF;
15775 
15776         UPDATE ap_invoice_distributions_all aid
15777            SET reversal_flag = (select reverse_flag
15778                                   from zx_rec_nrec_dist zx
15779                                  where zx.rec_nrec_tax_dist_id = aid.detail_tax_dist_id)
15780          WHERE aid.invoice_id = p_invoice_id
15781            AND aid.detail_tax_dist_id IS NOT NULL;
15782 
15783         -----------------------------------------------------------------
15784         l_debug_info := 'Step 5: Update related_flag';
15785         -----------------------------------------------------------------
15786         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
15787             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
15788         END IF;
15789 
15790         UPDATE ap_invoice_distributions aid
15791            SET aid.related_id =
15792       			(SELECT invoice_distribution_id
15793                            FROM ap_invoice_distributions_all aid1
15794 			  WHERE aid1.invoice_id = aid.invoice_id
15795 			    AND aid1.invoice_line_number = aid.invoice_line_number
15796 			    AND aid1.parent_reversal_id =
15797 					(SELECT related_id
15798 			                   FROM ap_invoice_distributions_all aid2
15799 					  WHERE aid2.invoice_id = aid.invoice_id
15800 					    AND aid2.invoice_line_number = aid.invoice_line_number
15801 					    AND aid2.invoice_distribution_id = aid.parent_reversal_id)
15802                        )
15803         WHERE aid.related_id IS NULL
15804           AND aid.parent_reversal_id IS NOT NULL
15805           AND aid.invoice_id = p_invoice_id
15806           AND aid.reversal_flag = 'Y'
15807           AND aid.detail_tax_dist_id IS NOT NULL;
15808 
15809     END IF;
15810 
15811 
15812 
15813     IF l_self_assess_tax_dist_exist THEN
15814 
15815            -----------------------------------------------------------------
15816            l_debug_info := 'Step 4: Update reversal_flag';
15817            -----------------------------------------------------------------
15818            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
15819                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
15820            END IF;
15821 
15822            UPDATE ap_self_assessed_tax_dist_all aid
15823               SET reversal_flag = (select reverse_flag
15824                                      from zx_rec_nrec_dist zx
15825                                     where zx.rec_nrec_tax_dist_id = aid.detail_tax_dist_id)
15826             WHERE aid.invoice_id = p_invoice_id
15827               AND aid.detail_tax_dist_id IS NOT NULL;
15828 
15829            -----------------------------------------------------------------
15830            l_debug_info := 'Step 5: Update related_flag';
15831            -----------------------------------------------------------------
15832            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
15833                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
15834            END IF;
15835 
15836            UPDATE ap_self_assessed_tax_dist_all aid
15837               SET aid.related_id =
15838                         (SELECT invoice_distribution_id
15839                            FROM ap_self_assessed_tax_dist_all aid1
15840                           WHERE aid1.invoice_id = aid.invoice_id
15841                             AND aid1.invoice_line_number = aid.invoice_line_number
15842                             AND aid1.parent_reversal_id =
15843                                         (SELECT related_id
15844                                            FROM ap_self_assessed_tax_dist_all aid2
15845                                           WHERE aid2.invoice_id = aid.invoice_id
15846                                             AND aid2.invoice_line_number = aid.invoice_line_number
15847                                             AND aid2.invoice_distribution_id = aid.parent_reversal_id)
15848                        )
15849             WHERE aid.related_id IS NULL
15850               AND aid.parent_reversal_id IS NOT NULL
15851               AND aid.invoice_id = p_invoice_id
15852               AND aid.reversal_flag = 'Y'
15853               AND aid.detail_tax_dist_id IS NOT NULL;
15854 
15855    END IF;
15856 
15857    END IF; -- l_return_status
15858    END IF; --MARKER 0 --Bug8811102
15859 
15860    RETURN l_return_status;
15861 
15862   EXCEPTION
15863     WHEN OTHERS THEN
15864 
15865       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
15866           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'Exception: '||sqlerrm);
15867       END IF;
15868 
15869       IF (SQLCODE <> -20001) THEN
15870 
15871         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
15872         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
15873         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
15874         FND_MESSAGE.SET_TOKEN('PARAMETERS',
15875           ' P_Invoice_Id = '||P_Invoice_Id);
15876         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
15877       END IF;
15878 
15879       APP_EXCEPTION.RAISE_EXCEPTION;
15880 
15881 END CANCEL_INVOICE;
15882 
15883 FUNCTION Generate_Recouped_Tax(
15884 	             P_Invoice_id              IN NUMBER,
15885 		     P_Invoice_Line_Number     IN NUMBER,
15886 	             P_Calling_Mode            IN VARCHAR2,
15887 	             P_All_Error_Messages      IN VARCHAR2,
15888 	             P_Error_Code              OUT NOCOPY VARCHAR2,
15889 	             P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN IS
15890 
15891     CURSOR invoice_header IS
15892     SELECT *
15893       FROM ap_invoices_all
15894      WHERE invoice_id = P_Invoice_Id;
15895 
15896     CURSOR prepay_lines IS
15897     SELECT distinct prepLine.*
15898       FROM ap_invoice_distributions_all invDist,
15899            ap_invoice_distributions_all prepDist,
15900            ap_invoice_lines_all		prepLine
15901      WHERE invDist.prepay_distribution_id = prepDist.invoice_distribution_id
15902        AND prepLine.invoice_id		  = prepDist.invoice_id
15903        AND prepLine.line_number		  = prepDist.invoice_line_number
15904        AND invDist.line_type_lookup_code  = 'PREPAY'
15905        AND invDist.invoice_id             = p_invoice_id
15906        AND invDist.invoice_line_number    = p_invoice_line_number;
15907 
15908     l_inv_header_rec			ap_invoices_all%ROWTYPE;
15909     l_event_class_code			zx_trx_headers_gt.event_class_code%TYPE;
15910     l_event_type_code			zx_trx_headers_gt.event_type_code%TYPE;
15911 
15912     l_tax_already_calculated     	VARCHAR2(1);
15913 
15914     l_debug_info                	VARCHAR2(240);
15915     l_curr_calling_sequence     	VARCHAR2(4000);
15916     l_api_name                  	CONSTANT VARCHAR2(100) := 'Generate_Recouped_Tax';
15917 
15918     l_return_status                     BOOLEAN := TRUE;
15919     l_return_status_service             VARCHAR2(4000);
15920     l_msg_count                         NUMBER;
15921     l_msg_data                          VARCHAR2(4000);
15922     l_error_code			VARCHAR2(4000);
15923 
15924 BEGIN
15925 
15926     -----------------------------------------------------------------
15927     l_debug_info := 'Step 1: Populating invoice header local record';
15928     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
15929       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
15930     END IF;
15931     -----------------------------------------------------------------
15932 
15933     BEGIN
15934       OPEN  Invoice_Header;
15935       FETCH Invoice_Header INTO l_inv_header_rec;
15936       CLOSE Invoice_Header;
15937     END;
15938 
15939     -------------------------------------------------------------------
15940     l_debug_info := 'Step 2: Is tax already called invoice level?';
15941     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
15942       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
15943     END IF;
15944     -------------------------------------------------------------------
15945     IF (AP_ETAX_UTILITY_PKG.Is_Tax_Already_Calc_Inv(
15946           P_Invoice_Id           => p_invoice_id,
15947           P_Calling_Sequence     => l_curr_calling_sequence)) THEN
15948 
15949       l_tax_already_calculated := 'Y';
15950     ELSE
15951       l_tax_already_calculated := 'N';
15952 
15953     END IF;
15954 
15955     -----------------------------------------------------------------
15956     l_debug_info := 'Step 3: Populate Header';
15957     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
15958       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
15959     END IF;
15960     -----------------------------------------------------------------
15961     IF NOT(AP_ETAX_SERVICES_PKG.Populate_Headers_GT(
15962 		       P_Invoice_Header_Rec         => l_inv_header_rec
15963 		      ,P_Calling_Mode               => p_calling_mode
15964 		      ,P_eTax_Already_called_flag   => l_tax_already_calculated
15965 		      ,P_Event_Class_Code           => l_event_class_code
15966 		      ,P_Event_Type_Code            => l_event_type_code
15967 		      ,P_Error_Code                 => p_error_code
15968 		      ,P_Calling_Sequence           => l_curr_calling_sequence)) THEN
15969 
15970       l_return_status := FALSE;
15971     END IF;
15972 
15973     -----------------------------------------------------------------
15974     l_debug_info := 'Step 4: Populate psuedo prepay lines for the recouped distributions';
15975     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
15976       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
15977     END IF;
15978     -----------------------------------------------------------------
15979     IF ( l_return_status = TRUE ) THEN
15980 
15981       OPEN  prepay_lines;
15982       FETCH prepay_lines
15983        BULK COLLECT INTO l_inv_line_list;
15984       CLOSE prepay_lines;
15985 
15986       IF l_inv_line_list.count > 0 THEN
15987 
15988 
15989          -----------------------------------------------------------------
15990          l_debug_info := 'Purge Staging Tables. Clear/Load Cache';
15991          IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
15992             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
15993          END IF;
15994          --Print(l_api_name,l_debug_info);
15995          -----------------------------------------------------------------
15996          DELETE FROM ZX_TRANSACTION_LINES_GT;
15997 
15998          AP_ETAX_SERVICES_PKG.G_SITE_ATTRIBUTES.DELETE;
15999          AP_ETAX_SERVICES_PKG.G_ORG_ATTRIBUTES.DELETE;
16000 
16001           -----------------------------------------------------------------
16002           l_debug_info := 'Cache Line Defaults :' ||
16003 	           ' Invoice Type Lookup Code = ' || l_inv_header_rec.invoice_type_lookup_code ||
16004 			   ' ,Invoice Id = '              || l_inv_header_rec.invoice_id               ||
16005 			   ' ,Org id = '                  || l_inv_header_rec.org_id                   ||
16006 			   ' ,Vendor Site Id = '          || l_inv_header_rec.vendor_site_id           ||
16007 			   ' ,Party Site Id = '           || l_inv_header_rec.party_site_id           ;
16008           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16009             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16010           END IF;
16011           -----------------------------------------------------------------
16012 
16013               IF l_inv_header_rec.invoice_type_lookup_code = 'PAYMENT REQUEST' THEN  /* if
16014                   condition for bug 5967914 as we need tp pass party_site_id instead of
16015                   vendor_site_id if invoice_type_lookup_code ='PAYMENT REQUEST' */
16016                  l_payment_request_flag :='Y';  -- for bug 5967914
16017 	         Cache_Line_Defaults
16018 	               ( p_org_id           => l_inv_header_rec.org_id
16019 	                ,p_vendor_site_id   => l_inv_header_rec.party_site_id
16020 	                ,p_calling_sequence => l_curr_calling_sequence);
16021               ELSE
16022                  l_payment_request_flag :='N';  -- for bug 5967914
16023                	  Cache_Line_Defaults
16024 	               ( p_org_id           => l_inv_header_rec.org_id
16025 	                ,p_vendor_site_id   => l_inv_header_rec.vendor_site_id
16026 	                ,p_calling_sequence => l_curr_calling_sequence);
16027               END IF;
16028 
16029 
16030 	 IF NOT(AP_ETAX_SERVICES_PKG.Populate_Lines_GT(
16031 		           P_Invoice_Header_Rec      => l_inv_header_rec
16032 		          ,P_Calling_Mode            => p_calling_mode
16033 		          ,P_Event_Class_Code        => l_event_class_code
16034 		          ,P_Line_Number             => p_invoice_line_number
16035 		          ,P_Error_Code              => p_error_code
16036 		          ,P_Calling_Sequence        => l_curr_calling_sequence )) THEN
16037 
16038 	     l_return_status := FALSE;
16039 
16040          END IF;
16041 
16042          IF ( l_return_status = TRUE ) THEN
16043 
16044              -----------------------------------------------------------------
16045              l_debug_info := 'Step 5: Call Calculate_Tax service';
16046              IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16047                  FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16048              END IF;
16049              -----------------------------------------------------------------
16050 
16051              zx_api_pub.calculate_tax(
16052 		          p_api_version      => 1.0,
16053 		          p_init_msg_list    => FND_API.G_TRUE,
16054 		          p_commit           => FND_API.G_FALSE,
16055 		          p_validation_level => FND_API.G_VALID_LEVEL_FULL,
16056 		          x_return_status    => l_return_status_service,
16057 		          x_msg_count        => l_msg_count,
16058 		          x_msg_data         => l_msg_data);
16059 
16060          END IF;
16061 
16062          IF (l_return_status_service = 'S') THEN
16063 
16064              -----------------------------------------------------------------
16065              l_debug_info := 'Step 5.1: Update Tax Already Calculated Flag';
16066              IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16067                  FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16068              END IF;
16069              -----------------------------------------------------------------
16070 
16071 	     UPDATE ap_invoice_lines_all ail
16072 	        SET ail.tax_already_calculated_flag = 'Y'
16073 	      WHERE ail.invoice_id  = p_invoice_id
16074                 AND ail.line_number = p_invoice_line_number;
16075 
16076              -----------------------------------------------------------------
16077              l_debug_info := 'Step 6: Generate Tax Distributions';
16078              IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16079                  FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16080              END IF;
16081              -----------------------------------------------------------------
16082 
16083              l_return_status := ap_etax_pkg.calling_etax
16084 		                        (p_invoice_id         => p_invoice_id,
16085 					             p_line_number	      => p_invoice_line_number,
16086 		                         p_calling_mode       => 'DISTRIBUTE RECOUP',
16087 		                         p_all_error_messages => 'N',
16088 		                         p_error_code         =>  l_error_code,
16089 		                         p_calling_sequence   => l_curr_calling_sequence);
16090 
16091          ELSE  -- handle errors
16092 
16093             -----------------------------------------------------------------
16094             l_debug_info := 'Step 7: Handle errors returned by API';
16095             IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16096                 FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16097             END IF;
16098             -----------------------------------------------------------------
16099 
16100             l_return_status := FALSE;
16101 
16102             IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
16103 	               P_All_Error_Messages  => P_All_Error_Messages,
16104 	               P_Msg_Count           => l_msg_count,
16105 	               P_Msg_Data            => l_msg_data,
16106 	               P_Error_Code          => P_Error_Code,
16107 	               P_Calling_Sequence    => l_curr_calling_sequence)) THEN
16108 	       NULL;
16109 	    END IF;
16110 
16111          END IF;
16112       END IF;
16113     END IF;
16114 
16115     DELETE FROM ZX_TRX_HEADERS_GT;
16116     DELETE FROM ZX_TRANSACTION_LINES_GT;
16117 
16118     RETURN l_return_status;
16119 
16120 EXCEPTION
16121     WHEN OTHERS THEN
16122 
16123       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16124           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'Exception: '||sqlerrm);
16125       END IF;
16126 
16127       IF (SQLCODE <> -20001) THEN
16128 
16129         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
16130         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
16131         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
16132         FND_MESSAGE.SET_TOKEN('PARAMETERS',
16133           ' P_Invoice_Id = '||P_Invoice_Id);
16134         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
16135       END IF;
16136 
16137       DELETE FROM ZX_TRX_HEADERS_GT;
16138       DELETE FROM ZX_TRANSACTION_LINES_GT;
16139 
16140       APP_EXCEPTION.RAISE_EXCEPTION;
16141 
16142 END Generate_Recouped_Tax;
16143 
16144 Function Delete_Tax_Distributions
16145 			(p_invoice_id         IN  ap_invoice_distributions_all.invoice_id%Type,
16146 			 p_calling_mode	      IN  VARCHAR2,
16147 			 p_all_error_messages IN  VARCHAR2,
16148 			 p_error_code         OUT NOCOPY VARCHAR2,
16149 		         p_calling_sequence   IN  VARCHAR2) RETURN BOOLEAN IS
16150 
16151    -- Removed the cursor for the bug 9749258
16152 
16153 
16154    l_invoice_header_rec		ap_invoices_all%rowtype;
16155    l_application_id             zx_trx_headers_gt.application_id%TYPE;
16156    l_entity_code                zx_trx_headers_gt.entity_code%TYPE;
16157    l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
16158    l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
16159    l_tax_already_calculated     VARCHAR2(1);
16160 
16161    l_debug_info                 VARCHAR2(240);
16162    l_curr_calling_sequence      VARCHAR2(4000);
16163 
16164    l_return_status_service      VARCHAR2(4000);
16165    l_return_status              BOOLEAN := TRUE;
16166    l_error_code                 VARCHAR2(4000);
16167    l_msg_data			VARCHAR2(2000);
16168    l_msg_count			NUMBER;
16169 
16170   -- l_preview_dists		c_preview_dists%rowtype;
16171    l_transaction_line_rec_type  ZX_API_PUB.transaction_line_rec_type;
16172 
16173    l_api_name                    CONSTANT VARCHAR2(100) := 'Delete_Tax_distributions';
16174 
16175 Begin
16176 
16177    l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Delete_Tax_Distributions<-' ||
16178                                p_calling_sequence;
16179 
16180    IF NOT tax_distributions_exist
16181                         (p_invoice_id  => p_invoice_id) THEN
16182 
16183        l_debug_info := 'Exit delete_tax_distributions';
16184        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16185            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16186        END IF;
16187 
16188        RETURN l_return_status;
16189 
16190    END IF;
16191 
16192    -------------------------------------------------------------------
16193    l_debug_info := 'Step 1: Get invoice header details';
16194    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16195        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16196    END IF;
16197    -------------------------------------------------------------------
16198    Select * Into l_invoice_header_rec
16199      From ap_invoices_all
16200     Where invoice_id = p_invoice_id;
16201 
16202    IF ((l_invoice_header_rec.quick_credit = 'Y') OR    -- Bug 5660314
16203        (l_invoice_header_rec.invoice_type_lookup_code IN ('AWT', 'INTEREST'))) THEN
16204      RETURN l_return_status;
16205    END IF;
16206 
16207    -------------------------------------------------------------------
16208    l_debug_info := 'Step 2: Populate product specific attributes';
16209    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16210        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16211    END IF;
16212    -------------------------------------------------------------------
16213    l_application_id := 200;
16214    l_entity_code    := 'AP_INVOICES';
16215 
16216    -------------------------------------------------------------------
16217    l_debug_info := 'Step 3: Get event class code';
16218    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16219        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16220    END IF;
16221    -------------------------------------------------------------------
16222    IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Class_Code(
16223 		      P_Invoice_Type_Lookup_Code => l_invoice_header_rec.invoice_type_lookup_code,
16224 		      P_Event_Class_Code         => l_event_class_code,
16225 		      P_error_code               => l_error_code,
16226 		      P_calling_sequence         => l_curr_calling_sequence)) THEN
16227       l_return_status := FALSE;
16228    END IF;
16229 
16230    -------------------------------------------------------------------
16231    l_debug_info := 'Step 4: Is tax already called invoice level?';
16232    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16233       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16234    END IF;
16235    -------------------------------------------------------------------
16236    IF (AP_ETAX_UTILITY_PKG.Is_Tax_Already_Calc_Inv(
16237 			P_Invoice_Id       => p_invoice_id,
16238 			P_Calling_Sequence => l_curr_calling_sequence)) THEN
16239        l_tax_already_calculated := 'Y';
16240    ELSE
16241        l_tax_already_calculated := 'N';
16242    END IF;
16243 
16244    -------------------------------------------------------------------
16245    l_debug_info := 'Step 5: Get event type code';
16246    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16247        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16248    END IF;
16249    -------------------------------------------------------------------
16250    IF (l_return_status = TRUE) THEN
16251       IF NOT(AP_ETAX_UTILITY_PKG.Get_Event_Type_Code(
16252 			P_Event_Class_Code          => l_event_class_code,
16253 			P_Calling_Mode              => 'DISTRIBUTE',
16254 			P_eTax_Already_called_flag  => l_tax_already_calculated,
16255 			P_Event_Type_Code           => l_event_type_code,
16256 			P_Error_Code                => l_error_code,
16257 			P_Calling_Sequence          => l_curr_calling_sequence)) THEN
16258         l_return_status := FALSE;
16259       END IF;
16260    END IF;
16261    --Bug6678578START
16262    --ER CHANGES 6772098
16263    UPDATE ap_invoice_distributions_All aid1
16264 			    SET  aid1.amount = aid1.amount + nvl((SELECT SUM(nvl(amount,0))
16265 			                            FROM ap_invoice_distributions_All aid2
16266 			                           WHERE aid2.invoice_id =  p_invoice_id
16267                                          AND aid2.invoice_line_number = aid1.invoice_line_number
16268 			                             AND aid2.charge_applicable_to_dist_id = aid1.invoice_distribution_id
16269 			                             AND aid2.line_type_lookup_code IN ('REC_TAX','NONREC_TAX','TRV','TIPV')
16270                                          AND aid2.distribution_class = 'CANDIDATE'
16271                                          AND EXISTS (SELECT 1
16272                                                          FROM zx_rec_nrec_dist zd
16273                                                         WHERE zd.application_id =200
16274                                                           AND zd.entity_code = 'AP_INVOICES'
16275                                                           AND zd.event_class_code IN ('STANDARD INVOICES','EXPENSE REPORTS','PREPAYMENT INVOICES')
16276                                                           AND zd.trx_id = aid2.invoice_id
16277                                                           AND zd.rec_nrec_tax_dist_id = aid2.detail_tax_dist_id
16278                                                           AND NVL(zd.inclusive_flag,'N') = 'Y')),0),
16279 			           aid1.base_amount =aid1.base_amount + nvl((SELECT SUM(nvl(base_amount,0))
16280 			                            FROM ap_invoice_distributions_All aid3
16281 			                           WHERE aid3.invoice_id =  p_invoice_id
16282                                          AND aid3.invoice_line_number = aid1.invoice_line_number
16283 	                                     AND aid3.charge_applicable_to_dist_id = aid1.invoice_distribution_id
16284           		                         AND aid3.line_type_lookup_code IN ('REC_TAX','NONREC_TAX','TRV','TIPV')
16285                                          AND aid3.distribution_class = 'CANDIDATE'
16286                                          AND EXISTS (SELECT 1
16287                                                        FROM zx_rec_nrec_dist zd1
16288                                                       WHERE zd1.application_id =200
16289                                                         AND zd1.entity_code = 'AP_INVOICES'
16290                                                         AND zd1.event_class_code IN ('STANDARD INVOICES','EXPENSE REPORTS','PREPAYMENT INVOICES')
16291                                                         AND zd1.trx_id = aid3.invoice_id
16292                                                         AND zd1.rec_nrec_tax_dist_id = aid3.detail_tax_dist_id
16293                                                         AND NVL(zd1.inclusive_flag,'N') = 'Y')),0)   --ER CHANGES
16294 			    WHERE aid1.invoice_id =  p_invoice_id
16295 			      AND  aid1.line_type_lookup_code IN ('ITEM', 'ACCRUAL', 'FREIGHT', 'MISCELLANEOUS');   --bug9314506
16296    --ER CHANGES 6772098
16297    --Bug6678578END
16298    IF (l_return_status = TRUE) THEN
16299 
16300 
16301          INSERT INTO zx_tax_dist_id_gt
16302           (Select distinct  detail_tax_dist_id
16303              From ap_invoice_distributions_all
16304             Where invoice_id = p_invoice_id
16305               AND distribution_class = 'CANDIDATE'
16306               And line_type_lookup_code  In('NONREC_TAX', 'REC_TAX', 'TRV', 'TERV', 'TIPV')
16307            UNION
16308            Select distinct detail_tax_dist_id
16309              From ap_self_assessed_tax_dist_all
16310             Where invoice_id = p_invoice_id
16311               AND distribution_class = 'CANDIDATE'
16312               And line_type_lookup_code  In('NONREC_TAX', 'REC_TAX', 'TRV', 'TERV', 'TIPV'));
16313            --bug9749528
16314 
16315 
16316        --Removed the open cursor for bug 9749258
16317 
16318            l_transaction_line_rec_type.internal_organization_id := l_invoice_header_rec.org_id; --bug9749258
16319            l_transaction_line_rec_type.application_id           := l_application_id;
16320            l_transaction_line_rec_type.entity_code              := l_entity_code;
16321            l_transaction_line_rec_type.event_class_code         := l_event_class_code;
16322            l_transaction_line_rec_type.event_type_code          := l_event_type_code;
16323            l_transaction_line_rec_type.trx_id                   := p_invoice_id; --bug9749258
16324            l_transaction_line_rec_type.trx_line_id              := NULL; --bug9749258
16325            l_transaction_line_rec_type.trx_level_type           := 'LINE';
16326 
16327 	     -------------------------------------------------------------------
16328            l_debug_info := 'Step 5: Call eTax API to delete tax distributions';
16329            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16330               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16331            END IF;
16332            -------------------------------------------------------------------
16333           --used the new API for bug9749258
16334 
16335 	   ZX_NEW_SERVICES_PKG.delete_tax_dists(
16336 	          p_api_version             =>  1.0,
16337 	          p_init_msg_list           =>  FND_API.G_TRUE,
16338 	          p_commit                  =>  FND_API.G_FALSE,
16339 	          p_validation_level        =>  FND_API.G_VALID_LEVEL_FULL,
16340 	          x_return_status           =>  l_return_status_service,
16341 	          x_msg_count               =>  l_msg_count,
16342 	          x_msg_data                =>  l_msg_data,
16343 	          p_transaction_line_rec    =>  l_transaction_line_rec_type
16344 	        );
16345 
16346            IF l_return_status_service <> FND_API.G_RET_STS_SUCCESS THEN
16347               RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
16348            END IF;
16349 
16350    END IF;
16351 
16352    --Bug8811102
16353    --Bug 9749258 deleted the extra delete statement.
16354    ---------------------------
16355    --Bug8811102
16356    RETURN l_return_status;
16357 
16358 EXCEPTION
16359    WHEN OTHERS THEN
16360 
16361       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16362           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,'Exception: '||sqlerrm);
16363       END IF;
16364 
16365       IF (SQLCODE <> -20001) THEN
16366         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
16367         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
16368         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
16369         FND_MESSAGE.SET_TOKEN('PARAMETERS', ' P_Invoice_Id = '||P_Invoice_Id);
16370         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
16371       END IF;
16372 
16373       APP_EXCEPTION.RAISE_EXCEPTION;
16374 
16375 END Delete_Tax_distributions;
16376 
16377 FUNCTION TAX_DISTRIBUTIONS_EXIST
16378                         (p_invoice_id  IN NUMBER) RETURN BOOLEAN IS
16379 
16380       l_dummy VARCHAR2(40);
16381 
16382 BEGIN
16383     SELECT 'Tax Distributions Exist'
16384       INTO l_dummy
16385       FROM ap_invoice_distributions_all
16386      WHERE invoice_id = p_invoice_id
16387        AND line_type_lookup_code IN ('REC_TAX', 'NONREC_TAX', 'TIPV', 'TRV', 'TERV')
16388        AND rownum = 1;
16389 
16390     RETURN (l_dummy IS NOT NULL);
16391 
16392 EXCEPTION
16393     WHEN OTHERS THEN
16394          RETURN FALSE;
16395 END TAX_DISTRIBUTIONS_EXIST;
16396 
16397 FUNCTION TAX_ONLY_LINE_EXIST
16398                         (p_invoice_id  IN NUMBER) RETURN BOOLEAN IS
16399 
16400       l_dummy VARCHAR2(40);
16401 
16402 BEGIN
16403        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16404            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||'TAX_ONLY_LINE_EXIST','Checking if there is tax only line exists');
16405        END IF;
16406 
16407    /* SELECT 'Tax Only Line Exist'
16408       INTO l_dummy
16409       FROM ap_invoice_distributions_all
16410      WHERE invoice_id = p_invoice_id
16411        AND line_type_lookup_code IN ('REC_TAX', 'NONREC_TAX', 'TIPV', 'TRV', 'TERV')
16412        AND charge_applicable_to_dist_id IS NULL
16413        AND rownum = 1; */
16414 
16415     SELECT 'Tax Only Line Exist'
16416       INTO l_dummy
16417       FROM zx_lines_summary zls
16418      WHERE application_id   = AP_ETAX_PKG.AP_APPLICATION_ID
16419        AND entity_code      = AP_ETAX_PKG.AP_ENTITY_CODE
16420        AND event_class_code IN (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
16421                                 AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
16422                                 AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
16423        AND zls.trx_id = p_invoice_id
16424        AND NVL(zls.reporting_only_flag, 'N') = 'N'
16425        AND NVL(zls.tax_only_line_flag, 'N') = 'Y'
16426        AND rownum = 1;
16427 
16428        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16429            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||'TAX_ONLY_LINE_EXIST','l_dummy: '|| NVL(l_dummy, 'No Tax Only Line'));
16430        END IF;
16431 
16432        RETURN (l_dummy IS NOT NULL);
16433 
16434 EXCEPTION
16435     WHEN OTHERS THEN
16436          IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16437            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||'TAX_ONLY_LINE_EXIST', 'in others: '|| NVL(l_dummy, 'No Tax Only Line'));
16438          END IF;
16439          RETURN FALSE;
16440 END TAX_ONLY_LINE_EXIST;
16441 
16442 /* Bug 6694536. Added function to verify whether there are self assessed tax lines
16443 --   associated with an invoice. Join with zx_rec_nrec_dist is required becase
16444 --   self_assessed_flag of zx_rec_nrec_dist actually indicates whether the lines
16445 --   are self assessed or not.
16446 */
16447 
16448 FUNCTION SELF_ASSESS_TAX_DIST_EXIST
16449                         (p_invoice_id  IN NUMBER) RETURN BOOLEAN IS
16450 
16451       l_dummy VARCHAR2(40);
16452 
16453 BEGIN
16454 
16455     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16456         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||'SELF_ASSESS_TAX_DIST_EXIST','Checking if there is self assessed tax');
16457     END IF;
16458 
16459     SELECT 'Tax Distributions Exist'
16460       INTO l_dummy
16461       FROM ap_self_assessed_tax_dist_all asat,
16462            zx_rec_nrec_dist zx_dist
16463      WHERE invoice_id = p_invoice_id
16464        AND asat.detail_tax_dist_id = zx_dist.rec_nrec_tax_dist_id
16465        AND zx_dist.self_assessed_flag = 'Y'
16466        AND nvl(zx_dist.reverse_flag, 'N') <> 'Y'
16467        AND line_type_lookup_code IN ('REC_TAX', 'NONREC_TAX')
16468        AND rownum = 1;
16469 
16470     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16471        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||'SELF_ASSESS_TAX_DIST_EXIST','l_dummy: '|| NVL(l_dummy, 'No Self Assessed Tax'));
16472     END IF;
16473 
16474     RETURN (l_dummy IS NOT NULL);
16475 
16476 EXCEPTION
16477     WHEN OTHERS THEN
16478          RETURN FALSE;
16479 END SELF_ASSESS_TAX_DIST_EXIST;
16480 
16481 PROCEDURE get_converted_qty_price (x_invoice_distribution_id IN  NUMBER,
16482 	                               x_inv_price		     OUT NOCOPY NUMBER,
16483 		                           x_inv_qty		     OUT NOCOPY NUMBER) IS
16484 
16485 	 CURSOR c_rct_info (c_inv_dist_id NUMBER) IS
16486 	 SELECT  D.unit_price		      unit_price,
16487                  D.quantity_invoiced      quantity_invoiced,  --Bug12834600
16488 		     pll.matching_basis	      match_basis,
16489 	         pll.match_option             match_option,
16490 	         pl.unit_meas_lookup_code     po_uom,
16491 	         D.matched_uom_lookup_code    rcv_uom,
16492 	         rsl.item_id                  rcv_item_id
16493 	  FROM   ap_invoice_distributions_all D,
16494 	         po_distributions_all 	      PD,
16495 	         po_lines_all		      PL,
16496 	         po_line_locations_all	      PLL,
16497 	         rcv_transactions	      RTXN,
16498 	         rcv_shipment_lines 	      RSL
16499 	  WHERE  D.invoice_distribution_id = c_inv_dist_id
16500 	    AND  D.po_distribution_id      = PD.po_distribution_id
16501 	    AND  PL.po_header_id           = PD.po_header_id
16502 	    AND  PL.po_line_id             = PD.po_line_id
16503 	    AND  PD.line_location_id       = PLL.line_location_id
16504 	    AND  D.rcv_transaction_id      = RTXN.transaction_id
16505 	    AND  RTXN.shipment_line_id     = RSL.shipment_line_id;
16506 
16507 	l_match_basis	po_line_types.matching_basis%TYPE;
16508 	l_match_option	po_line_locations.match_option%TYPE;
16509 	l_po_uom	po_line_locations.unit_meas_lookup_code%TYPE;
16510 	l_rct_uom	po_line_locations.unit_meas_lookup_code%TYPE;
16511 	l_rct_item_id	rcv_shipment_lines.item_id%TYPE;
16512 
16513 	l_uom_conv_rate NUMBER;
16514         l_qty_invoiced  NUMBER;
16515         l_inv_price     NUMBER;
16516 BEGIN
16517      OPEN  c_rct_info (x_invoice_distribution_id);
16518      FETCH c_rct_info
16519      INTO  x_inv_price, x_inv_qty, l_match_basis, l_match_option,
16520            l_po_uom, l_rct_uom, l_rct_item_id;
16521      CLOSE c_rct_info;
16522 
16523      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16524        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||'get_converted_qty_price','get_converted_qty_price');
16525      END IF;
16526 
16527      IF l_match_basis  = 'QUANTITY'  and
16528         l_match_option = 'R'	     and
16529         l_po_uom       <> l_rct_uom THEN
16530 
16531         l_uom_conv_rate := po_uom_s.po_uom_convert (
16532                              l_rct_uom,
16533                              l_po_uom,
16534                              l_rct_item_id);
16535 
16536         x_inv_qty   := x_inv_qty * l_uom_conv_rate;
16537         x_inv_price := x_inv_price / l_uom_conv_rate;
16538 
16539      IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16540        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||'get_converted_qty_price','l_uom_conv_rate '||l_uom_conv_rate);
16541        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||'get_converted_qty_price','x_inv_qty '||x_inv_qty);
16542        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||'get_converted_qty_price','x_inv_price '||x_inv_price);
16543      END IF;
16544 
16545      END IF;
16546 
16547 EXCEPTION
16548      WHEN OTHERS THEN
16549           NULL;
16550 END get_converted_qty_price;
16551 
16552 FUNCTION Calculate_Tax_Receipt_Match(
16553 			P_Invoice_Id              IN  NUMBER,
16554 			P_Calling_Mode            IN  VARCHAR2,
16555 			P_All_Error_Messages      IN  VARCHAR2,
16556 			P_Error_Code              OUT NOCOPY VARCHAR2,
16557 			P_Calling_Sequence        IN  VARCHAR2) RETURN BOOLEAN
16558 IS
16559 
16560     l_debug_info                 VARCHAR2(240);
16561     l_curr_calling_sequence      VARCHAR2(4000);
16562 
16563     l_inv_header_rec             ap_invoices_all%ROWTYPE;
16564     l_event_class_code           zx_trx_headers_gt.event_class_code%TYPE;
16565     l_event_type_code            zx_trx_headers_gt.event_type_code%TYPE;
16566 
16567     l_tax_already_calculated     VARCHAR2(1);
16568 
16572     l_msg                         VARCHAR2(4000);
16569     l_return_status_service       VARCHAR2(4000);
16570     l_msg_count                   NUMBER;
16571     l_msg_data                    VARCHAR2(4000);
16573 
16574     l_return_status               BOOLEAN := TRUE;
16575     l_no_tax_lines                VARCHAR2(1) := 'N';
16576     l_inv_rcv_matched             VARCHAR2(1) := 'N';
16577 
16578     CURSOR Invoice_Header IS
16579     SELECT *
16580       FROM ap_invoices_all
16581      WHERE invoice_id = P_Invoice_Id;
16582 
16583     CURSOR Tax_Lines_to_import IS
16584     SELECT *
16585       FROM ap_invoice_lines_all
16586      WHERE invoice_id = P_Invoice_Id
16587        AND line_type_lookup_code = 'TAX'
16588        AND summary_tax_line_id IS NULL;
16589 
16590     l_api_name CONSTANT VARCHAR2(100) := 'Calculate_Tax_Receipt_Match';
16591 
16592   BEGIN
16593     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.Calculate_Tax_Receipt_Match<-' ||
16594                                P_calling_sequence;
16595 
16596     -----------------------------------------------------------------
16597     l_debug_info := 'Step 1: Populating invoice header local record';
16598     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16599       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16600     END IF;
16601     -----------------------------------------------------------------
16602     BEGIN
16603       OPEN Invoice_Header;
16604       FETCH Invoice_Header INTO l_inv_header_rec;
16605       CLOSE Invoice_Header;
16606     END;
16607 
16608     BEGIN
16609       OPEN  Tax_Lines_to_Import;
16610       FETCH Tax_Lines_to_Import
16611       BULK  COLLECT INTO p_rct_match_tax_list;
16612       CLOSE Tax_Lines_to_Import;
16613     END;
16614 
16615     -----------------------------------------------------------------
16616     l_debug_info := 'Step 2: Is tax already called invoice level?';
16617     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16618       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16619     END IF;
16620     -----------------------------------------------------------------
16621     IF (AP_ETAX_UTILITY_PKG.Is_Tax_Already_Calc_Inv(
16622           P_Invoice_Id           => p_invoice_id,
16623           P_Calling_Sequence     => l_curr_calling_sequence)) THEN
16624 
16625       l_tax_already_calculated := 'Y';
16626     ELSE
16627       l_tax_already_calculated := 'N';
16628 
16629     END IF;
16630 
16631     -----------------------------------------------------------------
16632     l_debug_info := 'Step 3: Populate ZX header tables';
16633     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16634       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16635     END IF;
16636     -----------------------------------------------------------------
16637     IF NOT(AP_ETAX_SERVICES_PKG.Populate_Headers_GT(
16638 		P_Invoice_Header_Rec         => l_inv_header_rec,
16639 		P_Calling_Mode               => P_Calling_Mode,
16640 		P_eTax_Already_called_flag   => l_tax_already_calculated,
16641 		P_Event_Class_Code           => l_event_class_code,
16642 		P_Event_Type_Code            => l_event_type_code,
16643 		P_Error_Code                 => P_error_code,
16644 		P_Calling_Sequence           => l_curr_calling_sequence )) THEN
16645 
16646       l_return_status := FALSE;
16647     END IF;
16648 
16649     -------------------------------------------------------------------
16650     l_debug_info := 'Step 4: If tax already calculated call freeze '||
16651                     'distributions';
16652     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16653       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16654     END IF;
16655     -------------------------------------------------------------------
16656     IF (l_tax_already_calculated = 'Y') THEN
16657          IF NOT(AP_ETAX_SERVICES_PKG.Freeze_itm_Distributions(
16658                 P_Invoice_Header_Rec  => l_inv_header_rec,
16659                 P_Calling_Mode        => 'FREEZE DISTRIBUTIONS',
16660                 P_Event_Class_Code    => l_event_class_code,
16661                 P_All_Error_Messages  => P_All_Error_Messages,
16662                 P_Error_Code          => P_error_code,
16663                 P_Calling_Sequence    => l_curr_calling_sequence)) THEN --Bug7592845
16664 
16665            l_return_status := FALSE;
16666         END IF;
16667     END IF;
16668 
16669     ------------------------------------------------------------
16670     l_debug_info := 'Step 5: Populate zx_transaction_lines_gt';
16671     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16672       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16673     END IF;
16674     ------------------------------------------------------------
16675     IF ( l_return_status = TRUE ) THEN
16676         IF NOT(Populate_Rct_Match_Lines_GT(
16677 		P_Invoice_Header_Rec   => l_inv_header_rec,
16678 		P_Event_Class_Code     => l_event_class_code,
16679 		P_Error_Code           => P_error_code,
16680 		P_Calling_Sequence     => l_curr_calling_sequence )) THEN
16681 
16682 	   l_return_status := FALSE;
16683         END IF;
16684     END IF;
16685 
16686     IF (l_return_status = TRUE) THEN
16687 
16688         -----------------------------------------------------------------
16689         l_debug_info := 'Step 8: Call Calculate_Tax service';
16690         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16691             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16692         END IF;
16693         -----------------------------------------------------------------
16694         zx_api_pub.calculate_tax(
16695           p_api_version      => 1.0,
16696           p_init_msg_list    => FND_API.G_TRUE,
16697           p_commit           => FND_API.G_FALSE,
16698           p_validation_level => FND_API.G_VALID_LEVEL_FULL,
16699           x_return_status    => l_return_status_service,
16700           x_msg_count        => l_msg_count,
16701           x_msg_data         => l_msg_data);
16702     END IF;
16703 
16704     IF (l_return_status_service = 'S') THEN
16705       -----------------------------------------------------------------
16706       l_debug_info := 'Step 9: Handle return of tax lines';
16707       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16708           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16709       END IF;
16710       -----------------------------------------------------------------
16711       IF NOT(AP_ETAX_SERVICES_PKG.Return_Other_Charge_Lines(
16712                 P_Invoice_header_rec => l_inv_header_rec,
16713                 P_Error_Code         => P_error_code,
16714                 P_Calling_Sequence   => l_curr_calling_sequence)) THEN
16715 
16716 
16717           l_return_status := FALSE;
16718       END IF;
16719 
16720     ELSE  -- handle errors
16721 
16722       -----------------------------------------------------------------
16723       l_debug_info := 'Step 10: Handle errors returned by API';
16724       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16725           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16726       END IF;
16727       -----------------------------------------------------------------
16728       l_return_status := FALSE;
16729 
16730       IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
16731                P_All_Error_Messages  => P_All_Error_Messages,
16732                P_Msg_Count           => l_msg_count,
16733                P_Msg_Data            => l_msg_data,
16734                P_Error_Code          => P_Error_Code,
16735                P_Calling_Sequence    => l_curr_calling_sequence)) THEN
16736         NULL;
16737       END IF;
16738 
16739     END IF;
16740 
16741    RETURN l_return_status;
16742 
16743   EXCEPTION
16744     WHEN OTHERS THEN
16745       IF (SQLCODE <> -20001) THEN
16746         FND_MESSAGE.SET_NAME ('SQLAP', 'AP_DEBUG');
16747         FND_MESSAGE.SET_TOKEN('ERROR', SQLERRM);
16748         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
16749         FND_MESSAGE.SET_TOKEN('PARAMETERS', ' P_Invoice_Id = '      ||P_Invoice_Id  ||
16750 					    ' P_Calling_Mode ='     ||P_Calling_Mode||
16751 					    ' P_Error_Code = '      ||P_Error_Code  ||
16752 					    ' P_Calling_Sequence = '||P_Calling_Sequence);
16753         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
16754 
16755       END IF;
16756 
16757       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16758           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,sqlerrm);
16759       END IF;
16760 
16761       APP_EXCEPTION.RAISE_EXCEPTION;
16762 
16763   END Calculate_Tax_Receipt_Match;
16764 
16765   FUNCTION Populate_Rct_Match_Lines_GT(
16766              P_Invoice_Header_Rec      IN ap_invoices_all%ROWTYPE,
16767              P_Event_Class_Code        IN VARCHAR2,
16768              P_Error_Code              OUT NOCOPY VARCHAR2,
16769              P_Calling_Sequence        IN VARCHAR2) RETURN BOOLEAN IS
16770 
16771     TYPE Trx_Lines_Tab_Type IS TABLE OF zx_transaction_lines_gt%ROWTYPE;
16772 
16773     trans_lines				Trx_Lines_Tab_Type := Trx_Lines_Tab_Type();
16774 
16775     l_line_class                   	zx_transaction_lines_gt.line_class%TYPE;
16776     l_line_level_action			zx_transaction_lines_gt.line_level_action%TYPE;
16777     l_line_amt_includes_tax_flag	zx_transaction_lines_gt.line_amt_includes_tax_flag%TYPE;
16778     l_product_org_id			zx_transaction_lines_gt.product_org_id%TYPE;
16779     l_bill_to_location_id		zx_transaction_lines_gt.bill_to_location_id%TYPE;
16780     l_location_id		 	zx_transaction_lines_gt.ship_from_location_id%type;
16781     l_fob_point				zx_transaction_lines_gt.fob_point%TYPE;
16782 
16783     l_po_line_location_id		ap_invoice_lines_interface.po_line_location_id%TYPE;
16784 
16785     -- Purchase Order
16786     l_ref_doc_application_id		zx_transaction_lines_gt.ref_doc_application_id%TYPE;
16787     l_ref_doc_entity_code		zx_transaction_lines_gt.ref_doc_entity_code%TYPE;
16788     l_ref_doc_event_class_code		zx_transaction_lines_gt.ref_doc_event_class_code%TYPE;
16789     l_ref_doc_line_quantity		zx_transaction_lines_gt.ref_doc_line_quantity%TYPE;
16790     l_po_header_curr_conv_rate		po_headers_all.rate%TYPE;
16791     l_uom_code                    	mtl_units_of_measure.uom_code%TYPE;
16792     l_ref_doc_trx_level_type		zx_transaction_lines_gt.ref_doc_trx_level_type%TYPE;
16793     l_dummy				number;
16794 
16795     -- Receipt
16796     l_applied_to_application_id		zx_transaction_lines_gt.applied_to_application_id%TYPE;
16797     l_applied_to_entity_code		zx_transaction_lines_gt.applied_to_entity_code%TYPE;
16798     l_applied_to_event_class_code	zx_transaction_lines_gt.applied_to_event_class_code%TYPE;
16799     l_trx_receipt_date			zx_transaction_lines_gt.trx_receipt_date%TYPE;
16800     l_ref_doc_trx_id			zx_transaction_lines_gt.ref_doc_trx_id%TYPE;
16801 
16802     -- PO Tax Determining Attributes
16803     l_trx_bus_category			zx_transaction_lines_gt.trx_business_category%TYPE;
16804     l_intended_use              	zx_lines_det_factors.line_intended_use%type;
16805     l_product_type              	zx_lines_det_factors.product_type%type;
16806     l_product_category          	zx_lines_det_factors.product_category%type;
16807     l_product_fisc_class        	zx_lines_det_factors.product_fisc_classification%type;
16808     l_user_def_fisc_class   		zx_lines_det_factors.user_defined_fisc_class%type;
16809     l_assessable_value          	zx_lines_det_factors.assessable_value%type;
16810     l_dflt_tax_class_code       	zx_transaction_lines_gt.input_tax_classification_code%type;
16811     l_allow_tax_code_override           VARCHAR2(10);
16812     l_ship_to_party_id          po_line_locations_all.ship_to_organization_id%type; -- 7262269
16813 
16814     l_debug_info			Varchar2(240);
16815     l_curr_calling_sequence		Varchar2(4000);
16816     l_return_status			BOOLEAN := TRUE;
16817 
16818     l_api_name CONSTANT VARCHAR2(100) := 'Populate_Rct_Match_Lines_GT';
16819 
16820   BEGIN
16821 
16822       ----------------------------------------------------------------------
16823       l_debug_info := 'Step 1: Get location_id for org_id';
16824       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16825           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16826       END IF;
16827       ----------------------------------------------------------------------
16828       BEGIN
16829         SELECT location_id
16830           INTO l_bill_to_location_id
16831           FROM hr_all_organization_units
16832          WHERE organization_id = P_Invoice_Header_Rec.org_id;
16833       END;
16834       ----------------------------------------------------------------------
16835       l_debug_info := 'Location_id for org_id '|| l_bill_to_location_id||'& '||P_Invoice_Header_Rec.org_id;
16836       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16837           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16838       END IF;
16839       ----------------------------------------------------------------------
16840       ----------------------------------------------------------------------
16841       l_debug_info := 'Step 2: Get fob_lookup_code from po_vendor_sites_all';
16842       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16843           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16844       END IF;
16845       ----------------------------------------------------------------------
16846       BEGIN
16847         SELECT location_id, fob_lookup_code
16848           INTO l_location_id, l_fob_point
16849           FROM ap_supplier_sites_all
16850          WHERE vendor_site_id = P_Invoice_Header_Rec.vendor_site_id;
16851       END;
16852       ----------------------------------------------------------------------
16853       l_debug_info := 'fob_lookup_code from po_vendor_sites_all '||l_fob_point ||'& '||l_location_id||'& '||P_Invoice_Header_Rec.vendor_site_id;
16854       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16855           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16856       END IF;
16857       ----------------------------------------------------------------------
16858       ----------------------------------------------------------------------
16859       l_debug_info := 'Step 4: Populate zx_transaction_lines_gt';
16860       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16861           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16862       END IF;
16863       ----------------------------------------------------------------------
16864       IF (p_rct_match_tax_list.COUNT > 0) THEN
16865 
16866         trans_lines.EXTEND(p_rct_match_tax_list.COUNT);
16867 
16868         FOR i IN p_rct_match_tax_list.FIRST..p_rct_match_tax_list.LAST LOOP
16869 
16870           --------------------------------------------------------------------
16871           l_debug_info := 'Step 5: Get line_level_action for TAX ONLY line ';
16872           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16873               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16874           END IF;
16875           --------------------------------------------------------------------
16876           IF (p_rct_match_tax_list(i).rcv_transaction_id IS NOT NULL) THEN
16877 
16878               l_line_level_action 	:= 'CREATE_TAX_ONLY';
16879 
16880           END IF;
16881 
16882           -------------------------------------------------------------------
16883           l_debug_info := 'Step 6: Get Additional PO matched info if any ';
16884           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16885               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16886           END IF;
16887           -------------------------------------------------------------------
16888           IF (p_rct_match_tax_list(i).po_line_location_id IS NOT NULL) THEN
16889 
16890               l_po_line_location_id := p_rct_match_tax_list(i).po_line_location_id;
16891 
16892               IF NOT (AP_ETAX_UTILITY_PKG.Get_PO_Info(
16893 	              P_Po_Line_Location_Id         => l_po_line_location_id,
16894 	              P_PO_Distribution_Id          => null,
16895 	              P_Application_Id              => l_ref_doc_application_id,
16896 	              P_Entity_code                 => l_ref_doc_entity_code,
16897 	              P_Event_Class_Code            => l_ref_doc_event_class_code,
16898 	              P_PO_Quantity                 => l_ref_doc_line_quantity,
16899 	              P_Product_Org_Id              => l_product_org_id,
16900 	              P_Po_Header_Id                => l_ref_doc_trx_id,
16901 	              P_Po_Header_curr_conv_rate    => l_po_header_curr_conv_rate,
16902 	              P_Uom_Code                    => l_uom_code,
16903 	              P_Dist_Qty                    => l_dummy,
16904 	              P_Ship_Price                  => l_dummy,
16905 	              P_Error_Code                  => P_error_code,
16906 	              P_Calling_Sequence            => l_curr_calling_sequence)) THEN
16907 
16908 	             l_return_status := FALSE;
16909               END IF;
16910 
16911 	          l_ref_doc_trx_level_type := 'SHIPMENT';
16912 
16913          ELSE
16914             l_ref_doc_application_id	 := Null;
16915             l_ref_doc_entity_code	 := Null;
16916             l_ref_doc_event_class_code   := Null;
16917             l_ref_doc_line_quantity      := Null;
16918             l_product_org_id		 := Null;
16919             l_ref_doc_trx_id		 := Null;
16920             l_ref_doc_trx_level_type	 := Null;
16921             l_uom_code			 := Null;
16922          END IF;
16923 
16924           -------------------------------------------------------------------
16925           l_debug_info := 'Step 7: Get Additional receipt matched info ';
16926           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16927               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
16928           END IF;
16929           -------------------------------------------------------------------
16930           IF ( l_return_status = TRUE AND
16931                p_rct_match_tax_list(i).rcv_transaction_id IS NOT NULL) THEN
16932             IF NOT (AP_ETAX_UTILITY_PKG.Get_Receipt_Info(
16933                P_Rcv_Transaction_Id          => p_rct_match_tax_list(i).rcv_transaction_id,
16934                P_Application_Id              => l_applied_to_application_id,
16935                P_Entity_code                 => l_applied_to_entity_code,
16936                P_Event_Class_Code            => l_applied_to_event_class_code,
16937                P_Transaction_Date            => l_trx_receipt_date,
16938                P_Error_Code                  => P_error_code,
16939                P_Calling_Sequence            => l_curr_calling_sequence)) THEN
16940 
16941                l_return_status := FALSE;
16942             END IF;
16943          ELSE
16944 	    l_applied_to_application_id   := Null;
16945             l_applied_to_entity_code      := Null;
16946             l_applied_to_event_class_code := Null;
16947 	    l_trx_receipt_date		  := Null;
16948          END IF;
16949 
16950 	/*
16951 	IF (l_dflt_tax_class_code IS NULL
16952             AND p_rct_match_tax_list(i).tax_classification_code IS NULL) THEN
16953 
16954 	    ZX_AP_TAX_CLASSIFICATN_DEF_PKG.get_default_tax_classification
16955 			            (p_ref_doc_application_id           => l_ref_doc_application_id,
16956 			             p_ref_doc_entity_code              => l_ref_doc_entity_code,
16957 			             p_ref_doc_event_class_code         => l_ref_doc_event_class_code,
16958 			             p_ref_doc_trx_id                   => l_ref_doc_trx_id,
16959 			             p_ref_doc_line_id                  => p_rct_match_tax_list(i).po_line_location_id,
16960 			             p_ref_doc_trx_level_type           => 'SHIPMENT',
16961 			             p_vendor_id                        => P_Invoice_Header_Rec.vendor_id,
16962 			             p_vendor_site_id                   => P_Invoice_Header_Rec.vendor_site_id,
16963 			             p_code_combination_id              => p_rct_match_tax_list(i).default_dist_ccid,
16964 			             p_concatenated_segments            => null,
16965 			             p_templ_tax_classification_cd      => null,
16966 			             p_ship_to_location_id              => p_rct_match_tax_list(i).ship_to_location_id,
16967 			             p_ship_to_loc_org_id               => null,
16968 			             p_inventory_item_id                => p_rct_match_tax_list(i).inventory_item_id,
16969 			             p_item_org_id                      => l_product_org_id,
16970 			             p_tax_classification_code          => l_dflt_tax_class_code,
16971 			             p_allow_tax_code_override_flag     => l_allow_tax_code_override,
16972 			             APPL_SHORT_NAME                    => 'SQLAP',
16973 			             FUNC_SHORT_NAME                    => 'NONE',
16974 			             p_calling_sequence                 => 'AP_ETAX_SERVICES_PKG',
16975 			             p_event_class_code                 => P_Event_Class_Code,
16976 			             p_entity_code                      => 'AP_INVOICES',
16977 			             p_application_id                   => 200,
16978 			             p_internal_organization_id         => P_Invoice_Header_Rec.org_id);
16979 
16980 	END IF; */
16981 
16982          IF (l_return_status = TRUE) THEN
16983 
16984 	      IF NOT (AP_ETAX_UTILITY_PKG.Get_Line_Class(
16985 		             P_Invoice_Type_Lookup_Code    => P_Invoice_Header_Rec.invoice_type_lookup_code,
16986 		             P_Inv_Line_Type               => p_rct_match_tax_list(i).line_type_lookup_code,
16987 		             P_Line_Location_Id            => p_rct_match_tax_list(i).po_line_location_id,
16988 		             P_Line_Class                  => l_line_class,
16989 		             P_Error_Code                  => P_error_code,
16990 		             P_Calling_Sequence            => l_curr_calling_sequence)) THEN
16991 
16992                  l_return_status := FALSE;
16993              END IF;
16994          END IF;
16995 
16996           -------------------------------------------------------------------
16997           l_debug_info := 'Step 11: Populate pl/sql table';
16998           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
16999               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
17000           END IF;
17001           -------------------------------------------------------------------
17002           IF (l_return_status = TRUE ) THEN
17003 
17004             trans_lines(i).application_id			:= 200;
17005             trans_lines(i).entity_code				:= 'AP_INVOICES';
17006             trans_lines(i).event_class_code			:= p_event_class_code;
17007             trans_lines(i).trx_id				:= P_Invoice_Header_Rec.invoice_id;
17008             trans_lines(i).trx_level_type			:= 'LINE';
17009             trans_lines(i).line_level_action			:= l_line_level_action;
17010             trans_lines(i).line_class 				:= l_line_class;
17011             trans_lines(i).account_ccid				:= p_rct_match_tax_list(i).default_dist_ccid;
17012 
17013 	    -- Tax for PO/Receipt Match is always treated as exclusive.
17014             trans_lines(i).line_amt_includes_tax_flag		:= 'N';
17015             trans_lines(i).historical_flag			:= 'N';
17016 
17017 	    -- Pass negative line numbers to not conflict with existing invoice lines.
17018             trans_lines(i).trx_line_id				:= -i;
17019             trans_lines(i).trx_line_number			:= -i;
17020 
17021             trans_lines(i).trx_line_type			:= p_rct_match_tax_list(i).line_type_lookup_code;
17022             trans_lines(i).trx_line_description			:= p_rct_match_tax_list(i).description;
17023             trans_lines(i).trx_line_date                        := P_Invoice_Header_Rec.invoice_date;
17024             trans_lines(i).trx_line_gl_date                     := p_rct_match_tax_list(i).accounting_date;
17025             trans_lines(i).trx_receipt_date                     := l_trx_receipt_date;
17026 
17027             trans_lines(i).uom_code				:= p_rct_match_tax_list(i).unit_meas_lookup_code;
17028             trans_lines(i).trx_line_quantity 			:= p_rct_match_tax_list(i).quantity_invoiced;
17029             trans_lines(i).unit_price				:= p_rct_match_tax_list(i).unit_price;
17030             trans_lines(i).line_amt				:= p_rct_match_tax_list(i).amount;
17031 
17032             -- 7262269 /*Bug 13079028: Removed the IF-ELSE and called the function directly to handle all cases*/
17033             --IF p_rct_match_tax_list(i).po_line_location_id IS NOT NULL THEN
17034              l_ship_to_party_id := get_po_ship_to_org_id (p_rct_match_tax_list(i).po_line_location_id,
17035 	                                                  p_rct_match_tax_list(i).inventory_item_id, /*Bug 13079028*/
17036 							  p_rct_match_tax_list(i).org_id, /*Bug 13079028*/
17037 							  p_rct_match_tax_list(i).application_id, /*Bug 13079028*/
17038 							  p_rct_match_tax_list(i).reference_key5); /*Bug 13079028*/
17039             /*ELSE
17040              l_ship_to_party_id := p_rct_match_tax_list(i).org_id;
17041             END IF;*/ /*Bug 13079028*/
17042 
17043             trans_lines(i).ship_to_party_id		:= l_ship_to_party_id;
17044             -- 7262269
17045 
17046             trans_lines(i).bill_to_party_id			:= p_rct_match_tax_list(i).org_id;
17047 
17048             trans_lines(i).ship_from_party_id			:= P_Invoice_Header_Rec.party_id;
17049             trans_lines(i).bill_from_party_id			:= P_Invoice_Header_Rec.party_id;
17050 
17051             trans_lines(i).ship_from_party_site_id		:= P_Invoice_Header_Rec.party_site_id;
17052             trans_lines(i).bill_from_party_site_id		:= P_Invoice_Header_Rec.party_site_id;
17053 
17054             trans_lines(i).ship_to_location_id			:= p_rct_match_tax_list(i).ship_to_location_id;
17055             trans_lines(i).bill_to_location_id			:= l_bill_to_location_id;
17056 
17057             trans_lines(i).ship_from_location_id		:= l_location_id;
17058             trans_lines(i).bill_from_location_id          	:= l_location_id;
17059 
17060             trans_lines(i).trx_business_category		:= l_trx_bus_category;
17061             trans_lines(i).line_intended_use			:= l_intended_use;
17062             trans_lines(i).user_defined_fisc_class 		:= l_user_def_fisc_class;
17063             trans_lines(i).product_fisc_classification		:= l_product_fisc_class;
17064             trans_lines(i).product_type				:= l_product_type;
17065             trans_lines(i).product_category			:= l_product_category;
17066             trans_lines(i).assessable_value 		        := nvl(p_rct_match_tax_list(i).assessable_value,l_assessable_value);
17067             trans_lines(i).input_tax_classification_code	:= nvl(p_rct_match_tax_list(i).tax_classification_code,l_dflt_tax_class_code);
17068 
17069             trans_lines(i).product_id                           := p_rct_match_tax_list(i).inventory_item_id;
17070             trans_lines(i).product_description                  := p_rct_match_tax_list(i).item_description;
17071             trans_lines(i).product_org_id                       := l_product_org_id;
17072             trans_lines(i).fob_point                            := l_fob_point;
17073 
17074             trans_lines(i).ref_doc_application_id		:= l_ref_doc_application_id;
17075             trans_lines(i).ref_doc_entity_code			:= l_ref_doc_entity_code;
17076             trans_lines(i).ref_doc_event_class_code		:= l_ref_doc_event_class_code;
17077             trans_lines(i).ref_doc_trx_id			:= l_ref_doc_trx_id;
17078             trans_lines(i).ref_doc_line_id			:= p_rct_match_tax_list(i).po_line_location_id;
17079             trans_lines(i).ref_doc_trx_level_type		:= l_ref_doc_trx_level_type;
17080             trans_lines(i).ref_doc_line_quantity		:= l_ref_doc_line_quantity;
17081 
17082             trans_lines(i).applied_to_application_id		:= l_applied_to_application_id;
17083             trans_lines(i).applied_to_entity_code		:= l_applied_to_entity_code;
17084             trans_lines(i).applied_to_event_class_code		:= l_applied_to_event_class_code;
17085             trans_lines(i).applied_to_trx_id			:= p_rct_match_tax_list(i).rcv_transaction_id;
17086 
17087 	    IF p_rct_match_tax_list(i).rcv_transaction_id IS NOT NULL THEN
17088                trans_lines(i).applied_to_trx_line_id		:= p_rct_match_tax_list(i).po_line_location_id;
17089             END IF;
17090 
17091             trans_lines(i).product_id                           := p_rct_match_tax_list(i).inventory_item_id;
17092             trans_lines(i).product_description                  := p_rct_match_tax_list(i).item_description;
17093             trans_lines(i).product_org_id			:= l_product_org_id;
17094             trans_lines(i).fob_point				:= l_fob_point;
17095 
17096           END IF; -- l_return_status
17097 
17098           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17099 	      FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'event_class_code: ' || trans_lines(i).event_class_code);
17100 	      FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_id: '           || trans_lines(i).trx_id);
17101 	      FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_line_id: '      || trans_lines(i).trx_line_id);
17102 	      FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'trx_level_type: '   || trans_lines(i).trx_level_type);
17103 	      FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_level_action: '|| trans_lines(i).line_level_action);
17104 	      FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_class: '       || trans_lines(i).line_class);
17105 	      FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'line_amt: '         || trans_lines(i).line_amt);
17106           END IF;
17107 
17108         END LOOP;  -- end of loop TAX lines
17109       END IF; -- is p_rct_match_tax_list populated
17110 
17111     -------------------------------------------------------------------
17112     l_debug_info := 'Step 14: Bulk Insert into global temp tables';
17113     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17114       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
17115     END IF;
17116     -------------------------------------------------------------------
17117     IF (l_return_status = TRUE) THEN
17118 
17119       IF (trans_lines.COUNT > 0) THEN
17120 
17121 	DELETE FROM zx_transaction_lines_gt;
17122 
17123         FORALL m IN trans_lines.FIRST..trans_lines.LAST
17124           INSERT INTO zx_transaction_lines_gt
17125           VALUES trans_lines(m);
17126 
17127       END IF;
17128     END IF;
17129 
17130     RETURN TRUE;
17131 
17132   EXCEPTION
17133     WHEN OTHERS THEN
17134       IF (SQLCODE <> -20001) THEN
17135         FND_MESSAGE.SET_NAME ('SQLAP', 'AP_DEBUG');
17136         FND_MESSAGE.SET_TOKEN('ERROR', SQLERRM);
17137         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
17138         FND_MESSAGE.SET_TOKEN('PARAMETERS', ' P_Invoice_Id = '      ||P_Invoice_Header_Rec.Invoice_Id  ||
17139                                             ' P_Error_Code = '      ||P_Error_Code  ||
17140                                             ' P_Calling_Sequence = '||P_Calling_Sequence);
17141         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
17142 
17143       END IF;
17144 
17145       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17146           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,sqlerrm);
17147       END IF;
17148 
17149       APP_EXCEPTION.RAISE_EXCEPTION;
17150 
17151   END Populate_Rct_Match_Lines_GT;
17152 
17153 FUNCTION Bulk_Populate_Headers_GT(
17154              p_validation_request_id    IN  NUMBER,
17155              p_calling_mode             IN  VARCHAR2,
17156              p_error_code               OUT NOCOPY VARCHAR2) RETURN BOOLEAN IS
17157 
17158     l_api_name       	CONSTANT VARCHAR2(100) := 'Bulk_Populate_Headers_GT';
17159 
17160 BEGIN
17161         --Print(l_api_name, 'Bulk_Populate_Headers_GT (+)');
17162         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17163             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Bulk_Populate_Headers_GT (+)');
17164         END IF;
17165 
17166         DELETE FROM zx_trx_headers_gt;
17167 
17168 	INSERT INTO zx_trx_headers_gt(
17169 	        internal_organization_id,
17170 	        application_id,
17171 	        entity_code,
17172 	        event_class_code,
17173 	        event_type_code,
17174 	        trx_id,
17175 	        trx_date,
17176 		    ledger_id,
17177 	        trx_currency_code,
17178 	        currency_conversion_date,
17179 	        currency_conversion_rate,
17180 	        currency_conversion_type,
17181 	        minimum_accountable_unit,
17182 	        precision,
17183 	        legal_entity_id,
17184 	        rounding_ship_from_party_id,
17185 	        rounding_bill_from_party_id,
17186 	        rndg_ship_from_party_site_id,
17187 	        rndg_bill_from_party_site_id,
17188 	        related_doc_application_id,
17189 	        related_doc_entity_code,
17190 	        related_doc_event_class_code,
17191 	        related_doc_trx_id,
17192 	        related_doc_number,
17193 	        related_doc_date,
17194 	        default_taxation_country,
17195 	        quote_flag,
17196 	        ctrl_total_hdr_tx_amt,
17197 	        trx_number,
17198 		    trx_description,
17199 	        doc_seq_id,
17200 	        doc_seq_name,
17201 	        doc_seq_value,
17202 	        document_sub_type,
17203 	        supplier_tax_invoice_number,
17204 	        supplier_tax_invoice_date,
17205 	        supplier_exchange_rate,
17206 	        tax_invoice_date,
17207 	        tax_invoice_number,
17208 	        bill_third_pty_acct_id,
17209 	        bill_third_pty_acct_site_id,
17210 		    ship_third_pty_acct_id,
17211 		    ship_third_pty_acct_site_id
17212 	        )
17213 	  SELECT
17214 	        ai.org_id,								--internal_organization_id
17215 	        200,									--application_id
17216 	        'AP_INVOICES',                        					--entity_code
17217 	        (CASE
17218 	           WHEN ai.invoice_type_lookup_code IN
17219 			  ('STANDARD', 'CREDIT', 'DEBIT',   'MIXED',
17220 	                   'ADJUSTMENT', 'PO PRICE ADJUST', 'INVOICE REQUEST',
17221 	                   'CREDIT MEMO REQUEST', 'RETAINAGE RELEASE', 'PAYMENT REQUEST') -- bug 9281264
17222 	                THEN 'STANDARD INVOICES'
17223 	           WHEN (ai.invoice_type_lookup_code = 'PREPAYMENT')
17224 	                 or (p_calling_mode IN ('RECOUPMENT', 'DISTRIBUTE RECOUP'))
17225 	                THEN 'PREPAYMENT INVOICES'
17226 	           WHEN ai.invoice_type_lookup_code = 'EXPENSE REPORT'
17227 	                THEN 'EXPENSE REPORTS'
17228 	        END), 				     					--event_class_code
17229 	        (CASE
17230 	           WHEN ai.invoice_type_lookup_code IN
17231 			  ('STANDARD', 'CREDIT', 'DEBIT',   'MIXED',
17232 	                   'ADJUSTMENT', 'PO PRICE ADJUST', 'INVOICE REQUEST',
17233 	                   'CREDIT MEMO REQUEST', 'RETAINAGE RELEASE', 'PAYMENT REQUEST') -- bug 9281264
17234 	                THEN 'STANDARD '
17235 	           WHEN (ai.invoice_type_lookup_code = 'PREPAYMENT')
17236 	                 or (p_calling_mode IN ('RECOUPMENT', 'DISTRIBUTE RECOUP'))
17237 	                THEN 'PREPAYMENT '
17238 	           WHEN ai.invoice_type_lookup_code = 'EXPENSE REPORT'
17239 	                THEN 'EXPENSE REPORT '
17240 	        END)||
17241 	        DECODE(p_calling_mode,
17242 			'CALCULATE',
17243                         (CASE ((SELECT 'Y'
17244 				 FROM ap_invoice_lines_all
17245 				WHERE invoice_id = ai.invoice_id
17246 				  AND line_type_lookup_code <> 'AWT'
17247 				  AND (tax_already_calculated_flag = 'Y'
17248 			               OR  summary_tax_line_id IS NOT NULL)
17249 				  AND ROWNUM = 1)
17250                              --- Start for bug 6485124
17251                                 UNION
17252                                           (SELECT 'Y'
17253                                              FROM zx_lines_det_factors
17254                                             WHERE application_id = 200
17255                                              AND  entity_code = 'AP_INVOICES'
17256                                              AND  trx_id = ai.invoice_id
17257                                              AND  event_class_code in ('STANDARD INVOICES','PREPAYMENT INVOICES','EXPENSE REPORTS')
17258                                              AND  ROWNUM=1))
17259                              --- End for bug 6485124
17260 				WHEN 'Y' THEN 'UPDATED'
17261 		            	ELSE 'CREATED'
17262                 	END),
17263 			'DISTRIBUTE',
17264 			(CASE (SELECT 'Y'
17265 				 FROM ap_invoice_distributions_all
17266 				WHERE invoice_id = ai.invoice_id
17267 			          AND line_type_lookup_code <> 'AWT'
17268 				  AND (tax_already_distributed_flag = 'Y'
17269 			               OR detail_tax_dist_id IS NOT NULL)
17270 			          AND (related_id IS NULL
17271 				       OR related_id = invoice_distribution_id)
17272 				  AND ROWNUM = 1)
17273 				WHEN 'Y' THEN 'REDISTRIBUTE'
17274 				ELSE 'DISTRIBUTE'
17275 			END)),								--event_type_code
17276 	        ai.invoice_id,                   					--trx_id
17277 	        ai.invoice_date,                 					--trx_date
17278 	        ai.set_of_books_id,              					--ledger_id
17279 	        ai.invoice_currency_code,        					--trx_currency_code
17280 	        ai.exchange_date,                					--currency_conversion_date
17281 	        ai.exchange_rate,                					--currency_conversion_rate
17282 	        ai.exchange_rate_type,           					--currency_conversion_type
17283 	        NVL(cur.minimum_accountable_unit,
17284 			 (1/power(10,cur.precision))),   				--minimum_accountable_unit
17285 	        nvl(cur.precision,0),            					--precision
17286 	        ai.legal_entity_id,              					--legal_entity_id
17287 	        ai.party_id,                     					--rounding_ship_from_party_id
17288 	        ai.party_id,                     					--rounding_bill_from_party_id
17289 	        ai.party_site_id,                					--rndg_ship_from_party_site_id
17290 	        ai.party_site_id,                					--rndg_bill_from_party_site_id
17291                 (CASE
17292                    WHEN related_ai.invoice_type_lookup_code IS NOT NULL
17293                         THEN 200
17294                         ELSE NULL
17295                 END),									--related_doc_application_id
17296 		(CASE
17297                    WHEN related_ai.invoice_type_lookup_code IS NOT NULL
17298                         THEN 'AP_INVOICES'
17299 		        ELSE NULL
17300                 END),									--related_doc_entity_code
17301 	        (CASE
17302 	           WHEN related_ai.invoice_type_lookup_code IN
17303 		          ('STANDARD', 'CREDIT', 'DEBIT',   'MIXED',
17304 	                   'ADJUSTMENT', 'PO PRICE ADJUST', 'INVOICE REQUEST',
17305 	                   'CREDIT MEMO REQUEST', 'RETAINAGE RELEASE')
17306 	                THEN 'STANDARD INVOICES'
17307 	           WHEN ai.invoice_type_lookup_code = 'PREPAYMENT'
17308 	                THEN 'PREPAYMENT INVOICES'
17309 	           WHEN ai.invoice_type_lookup_code = 'EXPENSE REPORT'
17310 	                THEN 'EXPENSE REPORTS'
17311 	        END),									--related_doc_event_class_code
17312 	        ai.tax_related_invoice_id,       					--related_doc_trx_id
17313 	        related_ai.invoice_num,                            			--related_doc_number
17314 	        related_ai.invoice_date,                           			--related_doc_date
17315 	        ai.taxation_country,             					--default_taxation_country
17316 	        decode(p_calling_mode,
17317 				'CALCULATE QUOTE', 'Y', 'N'),				--quote_flag
17318 	        ai.control_amount,               					--ctrl_total_hdr_tx_amt
17319 	        ai.invoice_num,                  					--trx_number
17320 	        ai.description,                  					--trx_description
17321 	        ai.doc_sequence_id,              					--doc_seq_id
17322 	        doc.name,            		     					--doc_seq_name
17323 	        nvl(to_char(ai.doc_sequence_value), ai.voucher_num),				--doc_seq_value bug6656894
17324 	        ai.document_sub_type,            					--document_sub_type
17325 	        ai.supplier_tax_invoice_number,  					--supplier_tax_invoice_number
17326 	        ai.supplier_tax_invoice_date,    					--supplier_tax_invoice_date
17327 	        ai.supplier_tax_exchange_rate,   					--supplier_exchange_rate
17328 	        ai.tax_invoice_recording_date,   					--tax_invoice_date
17329 	        ai.tax_invoice_internal_seq,     					--tax_invoice_number
17330 	        ai.vendor_id,			     					--bill_third_pty_acct_id
17331 		ai.vendor_site_id,		     					--bill_third_pty_acct_site_id
17332 	        ai.vendor_id,                    					--ship_third_pty_acct_id
17333 	        ai.vendor_site_id                					--ship_third_pty_acct_site_id
17334 	  FROM  ap_invoices_all        ai,
17335 	        fnd_currencies         cur,
17336 	        fnd_document_sequences doc,
17337 	        ap_invoices_all        related_ai,
17338                 ap_system_parameters_all asp --Bug14779241
17339 	 WHERE  ai.invoice_currency_code  = cur.currency_code
17340 	   AND  ai.doc_sequence_id        = doc.doc_sequence_id (+)
17341 	   AND  ai.tax_related_invoice_id = related_ai.invoice_id (+)
17342            AND  nvl(ai.quick_credit,'N') = 'N'
17343            AND  ai.invoice_type_lookup_code NOT IN ('AWT', 'INTEREST') -- bug 9281264
17344 	   AND  ai.validation_request_id  = p_validation_request_id
17345        	   AND  asp.org_id = ai.org_id /* Bug14779241: Start */
17346            AND  ((asp.base_currency_code = ai.invoice_currency_code) OR
17347 	        (p_calling_mode IN ('CALCULATE')) OR
17348                 (asp.base_currency_code <> ai.invoice_currency_code AND
17349                 ai.exchange_rate IS NOT NULL AND p_calling_mode IN ('DISTRIBUTE','DISTRIBUTE IMPORT')
17350                 )); /* Bug14779241: End */
17351 
17352     -- Global Variable g_invoices_to_process should be initialized right after
17353     -- the previous insert. No other sql statements must be placed after the
17354     -- insert because the sql rowcount will be reset. This variable is used in
17355     -- calculate_tax and determine_recovery.
17356 
17357     g_invoices_to_process := sql%rowcount;
17358     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17359         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Rows inserted in zx_trx_headers_gt '||g_invoices_to_process);
17360         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name, 'Bulk_Populate_Headers_GT (-)');
17361     END IF;
17362     --Print(l_api_name, 'Rows inserted in zx_trx_headers_gt '||g_invoices_to_process);
17363     --Print(l_api_name, 'Bulk_Populate_Headers_GT (-)');
17364 
17365     RETURN TRUE;
17366 
17367 EXCEPTION
17368      WHEN OTHERS THEN
17369           RETURN FALSE;
17370 
17371 END Bulk_Populate_Headers_GT;
17372 
17373 FUNCTION Update_Distributions(
17374              P_Invoice_header_rec    IN ap_invoices_all%ROWTYPE,
17375              P_Calling_Mode          IN VARCHAR2,
17376              P_All_Error_Messages    IN VARCHAR2,
17377              P_Error_Code            OUT NOCOPY VARCHAR2,
17378              P_Calling_Sequence      IN VARCHAR2) RETURN BOOLEAN
17379 IS
17380 
17381     CURSOR c_reverse_dist (c_invoice_id NUMBER) IS
17382         SELECT aid_reverse.invoice_distribution_id invoice_distribution_id,
17383                aid_parent.invoice_distribution_id  parent_reversal_id,
17384                aid_parent.dist_code_combination_id parent_ccid
17385           FROM ap_invoice_distributions_all aid_reverse,
17386                ap_invoice_distributions_all aid_parent,
17387                zx_rec_nrec_dist zx
17388          WHERE aid_reverse.invoice_id         = c_invoice_id
17389            AND aid_reverse.detail_tax_dist_id = zx.rec_nrec_tax_dist_id
17390            AND aid_parent.detail_tax_dist_id  = zx.reversed_tax_dist_id
17391            AND aid_parent.line_type_lookup_code = aid_reverse.line_type_lookup_code;
17392 
17393     -- Bug 10140793 : Added below cursor
17394     CURSOR c_reverse_dist_sa (c_invoice_id NUMBER) IS
17395         SELECT aid_reverse.invoice_distribution_id invoice_distribution_id,
17396                aid_parent.invoice_distribution_id  parent_reversal_id,
17397                aid_parent.dist_code_combination_id parent_ccid
17398           FROM ap_self_assessed_tax_dist_all aid_reverse,
17399                ap_self_assessed_tax_dist_all aid_parent,
17400                zx_rec_nrec_dist zx
17401          WHERE aid_reverse.invoice_id         = c_invoice_id
17402            AND aid_reverse.detail_tax_dist_id = zx.rec_nrec_tax_dist_id
17403            AND aid_parent.detail_tax_dist_id  = zx.reversed_tax_dist_id
17404            AND aid_parent.line_type_lookup_code = aid_reverse.line_type_lookup_code;
17405 
17406    --Bug7253420 For RTAX/NRTAX and TRV having same detail_tax_dist_id
17407    --so the TRV distribution id is populating in the parent_reversal_id of
17408    --RTAX/NRTAX reversal distribution. So to get the Proper Reversal entries
17409    --line_type_lookup_code condition is added
17410 
17411    --       Cursor added for 6155675 - To get the tax amount which is included in
17412    --       the ITEM line (for tax inclusive lines and then we will subtract
17413    --       the included tax amount from the ITEM distributions .
17414     CURSOR  c_included_tax_amount is
17415         SELECT amount,included_tax_amount,line_number,
17416                (total_rec_tax_amt_funcl_curr +total_nrec_tax_amt_funcl_curr) base_included_tax_amount,discarded_flag --Bug8717396
17417         FROM   ap_invoice_lines_all
17418         WHERE  invoice_id =  p_invoice_header_rec.invoice_id
17419         AND    line_type_lookup_code IN ('ITEM', 'ACCRUAL', 'FREIGHT', 'MISCELLANEOUS','PREPAY') --/*Bug7338249*/
17420         --Bug9436217
17421         AND    included_tax_amount <> 0 ; --ER CHANGES
17422         --Bug9436217
17423 
17424     TYPE reversal_dist_info IS RECORD
17425                    (invoice_distribution_id ap_invoice_distributions_all.invoice_distribution_id%TYPE,
17426                     parent_reversal_id      ap_invoice_distributions_all.parent_reversal_id%TYPE,
17427                     parent_ccid             ap_invoice_distributions_all.dist_code_combination_id%TYPE);
17428 
17429     TYPE reversal_dist_type IS TABLE OF reversal_dist_info;
17430 
17431     l_reveral_dist_tab reversal_dist_type;
17432 
17433     l_debug_info		VARCHAR2(240);
17434     l_curr_calling_sequence	VARCHAR2(4000);
17435     l_api_name			CONSTANT VARCHAR2(100) := 'Update_Distributions';
17436     l_dist_amt                  ap_invoice_distributions_all.amount%type; -- for bug 6326552
17437     l_tot_tax_amt               ap_invoice_distributions_all.amount%type; -- for bug 6326552
17438 BEGIN
17439 
17440     -------------------------------------------------------------------
17441     l_debug_info := 'Step 1: Update the related_id column';
17442     -------------------------------------------------------------------
17443     IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17444          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
17445     END IF;
17446 
17447     /*  UPDATE ap_invoice_distributions_all aid
17448          SET related_id = (SELECT DECODE(MIN(nrtax.invoice_distribution_id),
17449 						NULL, MIN(other.invoice_distribution_id),
17450                                     		MIN(nrtax.invoice_distribution_id))
17451                              FROM ap_invoice_distributions_all nrtax,
17452                                   ap_invoice_distributions_all other
17453                             WHERE nrtax.invoice_id 	   = aid.invoice_id
17454                               AND other.invoice_id	   = aid.invoice_id
17455                               AND nrtax.detail_tax_dist_id = aid.detail_tax_dist_id
17456                               AND other.detail_tax_dist_id = aid.detail_tax_dist_id
17457                               AND nrtax.line_type_lookup_code <> 'TERV' --Bug9415464
17458                               AND (nrtax.line_type_lookup_code = 'NONREC_TAX'
17459                                    OR other.line_type_lookup_code IN ('TIPV', 'TRV'))
17460                              GROUP BY 1)
17461       WHERE aid.invoice_id = P_Invoice_Header_Rec.invoice_id
17462         AND aid.line_type_lookup_code in ('NONREC_TAX', 'TIPV', 'TRV', 'TERV')
17463         AND EXISTS (SELECT aid1.detail_tax_dist_id
17464                       FROM ap_invoice_distributions_all aid1
17468                      GROUP BY aid1.detail_tax_dist_id);*/
17465                      WHERE aid1.invoice_id = aid.invoice_id
17466                        AND aid1.detail_tax_dist_id = aid.detail_tax_dist_id
17467                     HAVING count(*) > 1
17469 
17470 
17471      --Bug11730310 changed the above sql as follows
17472 
17473       UPDATE ap_invoice_distributions_all aid
17474       SET related_id =DECODE((SELECT MIN(nrtax.invoice_distribution_id)
17475                                  FROM ap_invoice_distributions_all nrtax
17476                                  WHERE nrtax.invoice_id         = aid.invoice_id
17477                                  AND nrtax.detail_tax_dist_id = aid.detail_tax_dist_id
17478                                  AND nrtax.line_type_lookup_code = 'NONREC_TAX'
17479                                  GROUP BY 1) ,NULL ,
17480                                 (SELECT MIN(other.invoice_distribution_id)
17481                                  FROM ap_invoice_distributions_all other
17482                                  WHERE other.invoice_id         = aid.invoice_id
17483                                  AND other.detail_tax_dist_id = aid.detail_tax_dist_id
17484                                  AND other.line_type_lookup_code IN ('TIPV', 'TRV')
17485                                  GROUP BY 1) ,
17486                                 (SELECT MIN(nrtax.invoice_distribution_id)
17487                                  FROM ap_invoice_distributions_all nrtax
17488                                  WHERE nrtax.invoice_id         = aid.invoice_id
17489                                  AND nrtax.detail_tax_dist_id = aid.detail_tax_dist_id
17490                                  AND nrtax.line_type_lookup_code = 'NONREC_TAX'
17491                                  GROUP BY 1))
17492         WHERE aid.invoice_id = P_Invoice_Header_Rec.invoice_id
17493         AND aid.line_type_lookup_code in ('NONREC_TAX', 'TIPV', 'TRV', 'TERV')
17494         AND EXISTS (SELECT aid1.detail_tax_dist_id
17495                     FROM ap_invoice_distributions_all aid1
17496                     WHERE aid1.invoice_id = aid.invoice_id
17497                     AND aid1.detail_tax_dist_id = aid.detail_tax_dist_id
17498                     HAVING count(*) > 1
17499                     GROUP BY aid1.detail_tax_dist_id);
17500 
17501     -------------------------------------------------------------------
17502     l_debug_info := 'Step 2: Update the related_id column for self assessed distributions';
17503     -------------------------------------------------------------------
17504     IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17505          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
17506     END IF;
17507 
17508       UPDATE ap_self_assessed_tax_dist_all aid
17509          SET related_id = (SELECT DECODE(MIN(nrtax.invoice_distribution_id),
17510 					 NULL, MIN(other.invoice_distribution_id),
17511 					 MIN(nrtax.invoice_distribution_id))
17512                              FROM ap_self_assessed_tax_dist_all nrtax,
17513                                   ap_self_assessed_tax_dist_all other
17514                             WHERE nrtax.invoice_id 	   = aid.invoice_id
17515                               AND other.invoice_id 	   = aid.invoice_id
17516                               AND nrtax.detail_tax_dist_id = aid.detail_tax_dist_id
17517                               AND other.detail_tax_dist_id = aid.detail_tax_dist_id
17518                               AND nrtax.line_type_lookup_code <> 'TERV' --Bug9415464
17519                               AND (nrtax.line_type_lookup_code = 'NONREC_TAX'
17520                                    OR other.line_type_lookup_code IN ('TIPV', 'TRV'))
17521                              GROUP BY 1)
17522       WHERE aid.invoice_id = P_Invoice_Header_Rec.invoice_id
17523         AND aid.line_type_lookup_code in ('NONREC_TAX', 'TIPV', 'TRV', 'TERV')
17524         AND EXISTS (SELECT aid1.detail_tax_dist_id
17525                       FROM ap_self_assessed_tax_dist_all aid1
17526                      WHERE aid1.invoice_id = aid.invoice_id
17527                        AND aid1.detail_tax_dist_id = aid.detail_tax_dist_id
17528                     HAVING count(*) > 1
17529                      GROUP BY aid1.detail_tax_dist_id);
17530 
17531 
17532     -------------------------------------------------------------------
17533     l_debug_info := 'Step 3: Update rounding_amt for the primary NONREC tax dist';
17534     -------------------------------------------------------------------
17535     IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17536        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
17537     END IF;
17538 
17539       UPDATE ap_invoice_distributions_all aid
17540          SET rounding_amt =
17541              (SELECT zd.func_curr_rounding_adjustment
17542                 FROM zx_rec_nrec_dist zd
17543                WHERE zd.rec_nrec_tax_dist_id = aid.detail_tax_dist_id)
17544       WHERE aid.invoice_id = P_Invoice_Header_Rec.invoice_id
17545         AND aid.line_type_lookup_code in ('NONREC_TAX', 'TIPV', 'TRV', 'TERV')
17546         AND (aid.related_id IS NULL
17547              OR aid.related_id = aid.invoice_distribution_id);
17548 
17549 
17550     -------------------------------------------------------------------
17551     l_debug_info := 'Step 4: Update rounding_amt for the primary NONREC self assessed dist';
17552     -------------------------------------------------------------------
17553     IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17554        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
17555     END IF;
17556 
17557       UPDATE ap_self_assessed_tax_dist_all aid
17558          SET rounding_amt =
17559              (SELECT zd.func_curr_rounding_adjustment
17560                 FROM zx_rec_nrec_dist zd
17561                WHERE zd.rec_nrec_tax_dist_id = aid.detail_tax_dist_id)
17562       WHERE aid.invoice_id = P_Invoice_Header_Rec.invoice_id
17563         AND aid.line_type_lookup_code in ('NONREC_TAX', 'TIPV', 'TRV', 'TERV')
17564         AND (aid.related_id IS NULL
17565              OR aid.related_id = aid.invoice_distribution_id);
17566 
17567 
17568    -----------------------------------------------------------------
17569    l_debug_info := 'Step 5: Update REC and NONREC totals at line level';
17570    -----------------------------------------------------------------
17571     IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17572        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
17573     END IF;
17574 
17575       UPDATE ap_invoice_lines_all ail
17576          SET (ail.total_rec_tax_amount,
17577               ail.total_nrec_tax_amount,
17578               ail.total_rec_tax_amt_funcl_curr,
17579               ail.total_nrec_tax_amt_funcl_curr) =
17580              (SELECT SUM(DECODE(NVL(zd.recoverable_flag, 'N'),
17581                                 'Y', NVL(zd.rec_nrec_tax_amt, 0),
17582                                 0)),
17583                      SUM(DECODE(NVL(zd.recoverable_flag, 'N'),
17584                                 'N', NVL(zd.rec_nrec_tax_amt, 0),
17585                                  0)),
17586                      SUM(DECODE(NVL(zd.recoverable_flag, 'N'),
17587                                 'Y', NVL(zd.rec_nrec_tax_amt_funcl_curr, 0),
17588                                  0)),
17589                      SUM(DECODE(NVL(zd.recoverable_flag, 'N'),
17590                                 'N', NVL(zd.rec_nrec_tax_amt_funcl_curr, 0),
17591                                  0))
17592                 FROM zx_rec_nrec_dist zd
17593                WHERE application_id   = AP_ETAX_PKG.AP_APPLICATION_ID
17594                  AND entity_code      = AP_ETAX_PKG.AP_ENTITY_CODE
17595                  AND event_class_code IN (AP_ETAX_PKG.AP_INV_EVENT_CLASS_CODE,
17596 					  AP_ETAX_PKG.AP_PP_EVENT_CLASS_CODE,
17597 					  AP_ETAX_PKG.AP_ER_EVENT_CLASS_CODE)
17598                  AND zd.trx_id		= ail.invoice_id
17599                  AND ((zd.trx_line_id	= ail.line_number AND zd.inclusive_flag = 'Y')
17600                     OR (ail.summary_tax_line_id = zd.summary_tax_line_id AND zd.inclusive_flag = 'N'))  --Bug11720331
17601                  AND NVL(zd.self_assessed_flag, 'N') = 'N')
17602        WHERE ail.invoice_id		= P_Invoice_Header_Rec.invoice_id
17603          AND (ail.summary_tax_line_id IS NOT NULL
17604               OR (ail.line_type_lookup_code <> 'TAX'
17605                   AND ail.included_tax_amount IS NOT NULL));   --Bug11720331
17606 
17607        -- the total will be updated in the TAX lines for any exclusive tax
17608        -- line created and in the taxable line (ITEM, PREPAY) in the  case
17609        -- the calculation is inclusive
17610 
17611    -----------------------------------------------------------------
17612    l_debug_info := 'Step 5: Update tax_already_distributed_flag';
17613    -----------------------------------------------------------------
17614    IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17615        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
17616    END IF;
17617 
17618       UPDATE ap_invoice_distributions_all aid
17619          SET aid.tax_already_distributed_flag = 'Y'
17620        WHERE aid.invoice_id = p_invoice_header_rec.invoice_id
17621          AND NVL(aid.tax_already_distributed_flag, 'N') = 'N'
17622          AND aid.invoice_distribution_id IN
17623              ( SELECT aid1.charge_applicable_to_dist_id
17624                  FROM ap_invoice_distributions_all aid1,
17625                       zx_rec_nrec_dist zd
17626                 WHERE zd.REC_NREC_TAX_DIST_ID = aid1.DETAIL_TAX_DIST_ID
17627                   AND aid1.invoice_id = p_invoice_header_rec.invoice_id );
17628 
17629    -----------------------------------------------------------------
17630    l_debug_info := 'Step 6: Update generate_dists on the invoice line';
17631    -----------------------------------------------------------------
17632    IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17633        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
17634    END IF;
17635 
17636    UPDATE ap_invoice_lines_all ail
17637    SET    generate_dists = 'D'
17638    WHERE  ail.invoice_id = p_invoice_header_rec.invoice_id
17639    AND    ail.generate_dists <> 'D'
17640    AND    line_type_lookup_code = 'TAX'
17641    AND    EXISTS
17642 		(SELECT aid.invoice_distribution_id
17643 	           FROM ap_invoice_distributions_all aid
17644 	          WHERE aid.invoice_id          = ail.invoice_id
17645 	            AND aid.invoice_line_number = ail.line_number);
17646 
17647     -----------------------------------------------------------------
17648     l_debug_info := 'Step 7: Update Invoice Includes Prepay Flag';
17649     IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17650        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
17651     END IF;
17652     -----------------------------------------------------------------
17653     UPDATE ap_invoice_distributions_all tax
17654     SET    tax.invoice_includes_prepay_flag = 'Y'
17655     WHERE  tax.invoice_id = p_invoice_header_rec.invoice_id
17656     AND    nvl(tax.invoice_includes_prepay_flag,'N') <> 'Y'
17657     AND    line_type_lookup_code in ('NONREC_TAX','REC_TAX','TIPV','TERV','TRV')
17658     AND    exists
17659     		(SELECT 1
17660 	         FROM   ap_invoice_lines_all prepay
17661 	         WHERE  prepay.invoice_id	     = tax.invoice_id
17662        	         AND    prepay.line_number	     = tax.invoice_line_number
17663 	         AND    prepay.line_type_lookup_code = 'TAX'
17664 	         AND    prepay.prepay_line_number    IS NOT NULL
17665 	         AND    nvl(prepay.invoice_includes_prepay_flag,'N') = 'Y');
17666 
17667    -- For Loop added for bug 6155675 to subtract the inclusive tax amount
17668    -- from ITEM line.
17669    FOR i  in c_included_tax_amount LOOP
17670 
17671    --Bug9436217
17672 
17673    IF (NVL(i.discarded_flag,'N')='N') THEN
17674 
17675    --Bug9436217
17676 
17677    --Bug16315002: Commented below code
17678 
17679  /*  ------------------------------------------------------------------------
17680    l_debug_info := 'Step 7.1: Select sum of dist amount for each ITEM line';
17681    ------------------------------------------------------------------------
17682 
17683    IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17684        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
17685    END IF;
17686 
17687 
17688      SELECT sum(amount) -- Select added for bug 6326552 to make sure
17689      INTO   l_dist_amt  -- sure that we update the distributions only once.
17690      FROM   ap_invoice_distributions_All
17691      WHERE  invoice_id =  p_invoice_header_rec.invoice_id
17692      AND    invoice_line_number = i.line_number
17693      AND    line_type_lookup_code IN ('ITEM',   --Bug6653070 Added SUM()
17694                                                  --instead of only amount
17695 -- bug 7145041: add start
17696 -- These lookup codes also have inclusive amount included in their amount.
17697 			'ACCRUAL', 'FREIGHT', 'MISCELLANEOUS','PREPAY');
17698 -- bug 7145041: add end
17699 
17700    ------------------------------------------------------------------------
17701       l_debug_info := 'Step 7.2: sum of dist amount for each ITEM line' ;
17702    ------------------------------------------------------------------------
17703 
17704    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17705       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info||' '||i.line_number||' '||l_dist_amt);
17706    END IF; */
17707 
17708    ------------------------------------------------------------------------
17709    l_debug_info := 'Step 7.3: Select sum of dist amount for each TAX line';
17710    ------------------------------------------------------------------------
17711 
17712    IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17713        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
17714    END IF;
17715 
17716 
17717 
17718      SELECT SUM(amount)         -- Select added for bug 6326552 to make sure
17719      INTO   l_tot_tax_amt       -- sure that we update the distributions if
17720                                 --  if included tax amount is changed at
17721        			       --  line level.       .
17722      FROM   ap_invoice_distributions_All
17723      WHERE  invoice_id =  p_invoice_header_rec.invoice_id
17724      AND    invoice_line_number = i.line_number
17725      AND    line_type_lookup_code IN ('REC_TAX','NONREC_TAX','TRV','TERV','TIPV');
17726 
17727 
17728      ------------------------------------------------------------------------
17729       l_debug_info := 'Step 7.4: Sum of dist amount for TAX line' ;
17730      ------------------------------------------------------------------------
17731 
17732       IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17733          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info||' '||i.line_number||' '||l_tot_tax_amt);
17734       END IF;
17735 
17736 
17737       ------------------------------------------------------------------------
17738       l_debug_info := 'Step 7.4.1: Discarded Flag' ;
17739       ------------------------------------------------------------------------
17740 
17741 
17742       IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17743          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info||' '||i.line_number||' '||i.discarded_flag);
17744       END IF;
17745 
17746       --ER CHANGES 6772098
17747 
17748       --Bug9436217
17749 
17750       -- IF (i.amount = l_dist_amt) THEN --Bug8717396  --Bug16315002: Commented IF
17751 
17752       --Bug9436217
17753 
17754      ------------------------------------------------------------------------
17755       l_debug_info := 'Step 7.5: Update dist amount for each included TAX' ;
17756      ------------------------------------------------------------------------
17757 
17758       IF ( G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17759          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
17760       END IF;
17761 
17762       UPDATE ap_invoice_distributions_All aid1    --Bug6653070
17763         SET    aid1.amount = aid1.amount - nvl((SELECT SUM(nvl(amount,0))
17764                                   FROM ap_invoice_distributions_All aid2
17765                                  WHERE aid2.invoice_id =  p_invoice_header_rec.invoice_id
17766                                    AND aid2.invoice_line_number = i.line_number
17767                                    AND aid2.charge_applicable_to_dist_id = aid1.invoice_distribution_id
17768                                    AND aid2.line_type_lookup_code IN ('REC_TAX','NONREC_TAX', 'TIPV', 'TRV')
17769                                    AND EXISTS (SELECT 1
17770                                                  FROM zx_rec_nrec_dist zd1
17771                                                 WHERE zd1.application_id =200
17772                                                   AND zd1.entity_code = 'AP_INVOICES'
17773                                                 AND zd1.event_class_code IN ('STANDARD INVOICES','EXPENSE REPORTS','PREPAYMENT INVOICES')
17774                                                   AND zd1.trx_id = aid2.invoice_id
17775                                                   AND zd1.rec_nrec_tax_dist_id = aid2.detail_tax_dist_id
17776                                                   AND NVL(zd1.inclusive_flag,'N') = 'Y')),0),
17777                aid1.base_amount =aid1.base_amount- nvl((SELECT SUM(nvl(base_amount,0))
17778                                  FROM ap_invoice_distributions_All aid3
17779                                 WHERE aid3.invoice_id =  p_invoice_header_rec.invoice_id
17780                                   AND aid3.invoice_line_number = i.line_number
17781 					                        AND aid3.charge_applicable_to_dist_id = aid1.invoice_distribution_id
17782                                   AND aid3.line_type_lookup_code IN ('REC_TAX','NONREC_TAX', 'TIPV', 'TRV','TERV')
17783                                   AND EXISTS (SELECT 1
17784                                                 FROM zx_rec_nrec_dist zd2
17785                                                WHERE zd2.application_id =200
17786                                                  AND zd2.entity_code = 'AP_INVOICES'
17787                                                 AND zd2.event_class_code IN ('STANDARD INVOICES','EXPENSE REPORTS','PREPAYMENT INVOICES')
17788                                                  AND zd2.trx_id = aid3.invoice_id
17789                                                  AND zd2.rec_nrec_tax_dist_id = aid3.detail_tax_dist_id
17790                                                  AND NVL(zd2.inclusive_flag,'N') = 'Y')),0)
17791         WHERE  aid1.invoice_id =  p_invoice_header_rec.invoice_id
17792         AND    aid1.invoice_line_number = i.line_number
17793         AND    aid1.line_type_lookup_code IN ('ITEM', 'ACCRUAL', 'FREIGHT', 'MISCELLANEOUS');/*Bug7338249, bug9314506*/
17794 
17795       -- END IF;  --Bug8717396  --Bug16315002: Commented IF
17796       --ER CHANGES 6772098
17797 
17798    --Bug9436217
17799 
17800 
17801    END IF;
17802 
17803    --Bug9436217
17804 
17805    END LOOP;
17806 
17807 
17808    -----------------------------------------------------------------
17809    l_debug_info := 'Step 8: Update parent_reversal_id and parent_ccid';
17810    -----------------------------------------------------------------
17811     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17812         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
17813     END IF;
17814 
17815     OPEN  c_reverse_dist (p_invoice_header_rec.invoice_id);
17816     FETCH c_reverse_dist
17817      BULK COLLECT INTO l_reveral_dist_tab;
17818     CLOSE c_reverse_dist;
17819 
17820     IF l_reveral_dist_tab.count > 0 THEN
17821        FOR h in l_reveral_dist_tab.first .. l_reveral_dist_tab.last LOOP
17822 
17823            UPDATE ap_invoice_distributions_all
17824 	   SET    parent_reversal_id       = l_reveral_dist_tab(h).parent_reversal_id
17825 	         ,dist_code_combination_id = l_reveral_dist_tab(h).parent_ccid
17826            WHERE invoice_distribution_id   = l_reveral_dist_tab(h).invoice_distribution_id
17827              AND nvl(reversal_flag, 'N') <> 'Y';
17828 
17829        END LOOP;
17830    END IF;
17831 
17832    -- Bug 10140793 : Begin
17833    -------------------------------------------------
17834    l_debug_info := 'Now updating self assessed tax';
17835    -------------------------------------------------
17836     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17837         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
17838     END IF;
17839 
17840     l_reveral_dist_tab.delete ;
17841 
17842     OPEN  c_reverse_dist_sa (p_invoice_header_rec.invoice_id);
17843     FETCH c_reverse_dist_sa
17844      BULK COLLECT INTO l_reveral_dist_tab;
17845     CLOSE c_reverse_dist_sa;
17846 
17847     IF l_reveral_dist_tab.count > 0 THEN
17848        FOR h in l_reveral_dist_tab.first .. l_reveral_dist_tab.last LOOP
17849 
17850            UPDATE ap_self_assessed_tax_dist_all
17851 	   SET    parent_reversal_id       = l_reveral_dist_tab(h).parent_reversal_id
17852 	         ,dist_code_combination_id = l_reveral_dist_tab(h).parent_ccid
17853            WHERE invoice_distribution_id   = l_reveral_dist_tab(h).invoice_distribution_id
17854              AND nvl(reversal_flag, 'N') <> 'Y';
17855 
17856        END LOOP;
17857    END IF;
17858 
17859    -- Bug 10140793 : End
17860 
17861    -----------------------------------------------------------------
17862    l_debug_info := 'Step 9: Update reversal_flag';
17863    -----------------------------------------------------------------
17864     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17865         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
17866     END IF;
17867 
17868     UPDATE ap_invoice_distributions_all aid
17869        SET reversal_flag = (select reverse_flag
17870                               from zx_rec_nrec_dist zx
17871                              where zx.rec_nrec_tax_dist_id = aid.detail_tax_dist_id)
17872      WHERE aid.invoice_id = p_invoice_header_rec.invoice_id
17873        AND aid.detail_tax_dist_id IS NOT NULL;
17874 
17875    -- Bug 10140793 : Begin
17876    -------------------------------------------------
17877    l_debug_info := 'Now updating self assessed tax';
17878    -------------------------------------------------
17879     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17880         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
17881     END IF;
17882 
17883     UPDATE ap_self_assessed_tax_dist_all aid
17884        SET reversal_flag = (select reverse_flag
17885                               from zx_rec_nrec_dist zx
17886                              where zx.rec_nrec_tax_dist_id = aid.detail_tax_dist_id)
17887      WHERE aid.invoice_id = p_invoice_header_rec.invoice_id
17888        AND aid.detail_tax_dist_id IS NOT NULL;
17889    -- Bug 10140793 : End
17890 
17891 
17892    RETURN TRUE;
17893 
17894 EXCEPTION
17895        WHEN NO_DATA_FOUND THEN
17896             IF (G_LEVEL_EXCEPTION >= G_CURRENT_RUNTIME_LEVEL) THEN
17897                 FND_LOG.STRING(G_LEVEL_EXCEPTION,G_MODULE_NAME||l_api_name,SQLERRM);
17898             END IF;
17899 
17900        WHEN OTHERS THEN
17901          IF (SQLCODE <> -20001) THEN
17902              FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
17903              FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
17904              FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
17905              FND_MESSAGE.SET_TOKEN('PARAMETERS',
17906                                 ' P_Invoice_Id = '||P_Invoice_Header_Rec.invoice_id||
17907                                 ' P_Error_Code = '||P_Error_Code||
17908                                 ' P_Calling_Sequence = '||P_Calling_Sequence);
17909              FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
17910          END IF;
17911 
17912          IF ( G_LEVEL_EXCEPTION >= G_CURRENT_RUNTIME_LEVEL) THEN
17913             FND_LOG.STRING(G_LEVEL_EXCEPTION,G_MODULE_NAME||l_api_name,SQLERRM);
17914          END IF;
17915 
17916          APP_EXCEPTION.RAISE_EXCEPTION;
17917 
17918 END Update_Distributions;
17919 
17920 PROCEDURE Cache_Line_Defaults
17921                         (p_org_id               IN ap_invoices_all.org_id%type,
17922                          p_vendor_site_id       IN ap_supplier_sites_all.vendor_site_id%type,
17923                          p_calling_sequence     IN VARCHAR2) IS
17924 
17925     l_api_name                  VARCHAR2(30)    := 'Cache_Line_Defaults';
17926     l_curr_calling_sequence     VARCHAR2(2000);
17927     l_debug_info                VARCHAR2(1000);
17928 
17929 BEGIN
17930 
17931     l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.'||l_api_name||'<-'||p_calling_sequence;
17932 
17933     IF NOT AP_ETAX_SERVICES_PKG.g_org_attributes.exists(p_org_id) THEN
17934 
17935        ------------------------------------------------------------
17936        l_debug_info := 'Cache Org Attributes';
17937        IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17938           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
17939        END IF;
17940        --Print(l_api_name,l_debug_info);
17941        ------------------------------------------------------------
17942 
17943         SELECT location_id
17944           INTO AP_ETAX_SERVICES_PKG.g_org_attributes(p_org_id).bill_to_location_id
17945           FROM hr_all_organization_units
17946          WHERE organization_id = p_org_id;
17947 
17948     END IF;
17949 
17950     IF NOT AP_ETAX_SERVICES_PKG.g_site_attributes.exists(p_vendor_site_id) THEN
17951 
17952       ------------------------------------------------------------
17953       l_debug_info := 'Cache Supplier Site Attributes';
17954       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17955         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
17956       END IF;
17957       --Print(l_api_name,l_debug_info);
17958       ------------------------------------------------------------
17959 
17960       -- Bug 9526592 : Added debug messages
17961       ------------------------------------------------------------
17962       l_debug_info := 'Payment Req Flag '|| l_payment_request_flag;
17963       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17964          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
17965       END IF;
17966       --Print(l_api_name,l_debug_info);
17967       ------------------------------------------------------------
17968 
17969       ------------------------------------------------------------
17970       l_debug_info := 'Vendor Site Id  '|| p_vendor_site_id;
17971       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
17972          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
17973       END IF;
17974       --Print(l_api_name,l_debug_info);
17975       ------------------------------------------------------------
17976 
17977       IF l_payment_request_flag = 'Y' THEN  --- if condition for bug 5967914
17978 
17979         SELECT  hps.location_id
17980          INTO   AP_ETAX_SERVICES_PKG.g_site_attributes(p_vendor_site_id).location_id
17981                 FROM hz_party_sites hps
17982         WHERE party_site_id = p_vendor_site_id;
17983 
17984       ELSE
17985         SELECT  location_id
17986              ,fob_lookup_code
17987         INTO  AP_ETAX_SERVICES_PKG.g_site_attributes(p_vendor_site_id).location_id
17988              ,AP_ETAX_SERVICES_PKG.g_site_attributes(p_vendor_site_id).fob_lookup_code
17989         FROM ap_supplier_sites_all
17990         WHERE vendor_site_id = p_vendor_site_id;
17991       END IF;
17992 
17993     END IF;
17994 
17995 EXCEPTION
17996     WHEN OTHERS THEN
17997       IF (SQLCODE <> -20001) THEN
17998         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
17999         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
18000         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE', l_curr_calling_sequence);
18001         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
18002       END IF;
18003       APP_EXCEPTION.RAISE_EXCEPTION;
18004 END Cache_Line_Defaults;
18005 
18006 FUNCTION Return_Other_Charge_Lines(
18007              P_Invoice_Header_Rec        IN ap_invoices_all%ROWTYPE,
18011 
18008              P_Error_Code                OUT NOCOPY VARCHAR2,
18009              P_Calling_Sequence          IN VARCHAR2) RETURN BOOLEAN
18010 IS
18012     l_debug_info                 VARCHAR2(240);
18013     l_curr_calling_sequence      VARCHAR2(4000);
18014 
18015     l_period_name                gl_period_statuses.period_name%TYPE;
18016     l_gl_date                    ap_invoice_lines_all.accounting_date%TYPE;
18017     l_wfapproval_flag		 ap_system_parameters_all.approval_workflow_flag%TYPE;
18018     l_awt_include_tax_amt        ap_system_parameters_all.awt_include_tax_amt%TYPE;
18019     l_base_currency_code         ap_system_parameters_all.base_currency_code%TYPE;
18020     l_combined_filing_flag       ap_system_parameters_all.combined_filing_flag%TYPE;
18021     l_income_tax_region_flag     ap_system_parameters_all.income_tax_region_flag%TYPE;
18022     l_income_tax_region          ap_system_parameters_all.income_tax_region%TYPE;
18023     l_disc_is_inv_less_tax_flag	 ap_system_parameters_all.disc_is_inv_less_tax_flag%TYPE;
18024     l_wfapproval_status          ap_invoice_lines_all.wfapproval_status%TYPE;
18025     l_new_amt_applicable_to_disc ap_invoices_all.amount_applicable_to_discount%TYPE;
18026     l_payment_priority           ap_batches_all.payment_priority%TYPE;
18027 
18028     l_total_tax_amount          NUMBER;
18029     l_self_assessed_tax_amt     NUMBER;
18030 
18031     -- Allocations
18032     Cursor c_item_line (c_invoice_id IN NUMBER) IS
18033 	Select ail.line_number
18034 	From   ap_invoice_lines_all ail
18035 	Where  ail.invoice_id = c_invoice_id
18036         And    ail.line_type_lookup_code = 'TAX'
18037         And    ail.prepay_invoice_id IS NULL;
18038 
18039         /*
18040 	And NOT EXISTS
18041 		(Select chrg_invoice_line_number
18042 		 From ap_allocation_rule_lines arl
18043           	 Where arl.invoice_id = ail.invoice_id
18044             	 And arl.chrg_invoice_line_number = ail.line_number);
18045         */
18046 
18047     l_item_line	     c_item_line%rowtype;
18048 
18049     l_api_name                  CONSTANT VARCHAR2(100) := 'RETURN_TAX_LINES';
18050 
18051   BEGIN
18052 
18053     l_curr_calling_sequence := 'AP_ETAX_UTILITY_PKG.Return_Tax_Lines<-'||
18054                                P_calling_sequence;
18055 
18056     -------------------------------------------------------------------
18057     l_debug_info := 'Step 1: Get ap_system_parameters data';
18058     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
18059       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
18060     END IF;
18061     -------------------------------------------------------------------
18062     BEGIN
18063       SELECT
18064           approval_workflow_flag,
18065           awt_include_tax_amt,
18066           disc_is_inv_less_tax_flag,
18067           base_currency_code,
18068           combined_filing_flag,
18069           income_tax_region_flag,
18070           income_tax_region
18071         INTO
18072           l_wfapproval_flag,
18073           l_awt_include_tax_amt,
18074           l_disc_is_inv_less_tax_flag,
18075           l_base_currency_code,
18076           l_combined_filing_flag,
18077           l_income_tax_region_flag,
18078           l_income_tax_region
18079         FROM ap_system_parameters_all
18080        WHERE org_id = P_Invoice_Header_Rec.org_id;
18081     END;
18082 
18083     -------------------------------------------------------------------
18084     l_debug_info := 'Step 2: Update existing exclusive tax lines';
18085     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
18086       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
18087     END IF;
18088     -------------------------------------------------------------------
18089     BEGIN
18090       UPDATE ap_invoice_lines_all ail
18091          SET
18092        (-- Bug 10157751 ail.description,
18093         ail.amount,
18094         ail.base_amount,
18095         ail.cancelled_flag,
18096         ail.last_updated_by,
18097         ail.last_update_login,
18098         ail.last_update_date,
18099         ail.tax_regime_code,
18100         ail.tax,
18101         ail.tax_jurisdiction_code,
18102         ail.tax_status_code,
18103         ail.tax_rate_id,
18104         ail.tax_rate_code,
18105         ail.tax_rate,
18106 	ail.generate_dists) =
18107 	(
18108       SELECT
18109         /* Bug 10157751
18110         DECODE( ail.line_source,
18111 		'MANUAL LINE ENTRY', ail.description,
18112 		'IMPORTED'         , ail.description,
18113                 zls.tax_regime_code||' - '||zls.tax ),		-- description : Bug 9383712 - Added DECODE
18114 	*/
18115         zls.tax_amt, 						-- amount
18116         zls.tax_amt_funcl_curr,					-- base_amount
18117         zls.cancel_flag,					-- cancelled_flag
18118         l_user_id,						-- last_updated_by
18119         l_login_id,						-- last_update_login
18120         l_sysdate, 						-- last_update_date
18121         zls.tax_regime_code,	    				-- tax_regime_code
18122         zls.tax,		    				-- tax
18123         zls.tax_jurisdiction_code,  				-- tax_jurisdiction_code
18124         zls.tax_status_code,	    				-- tax_status_code
18125         zls.tax_rate_id,	    				-- tax_rate_id
18126         zls.tax_rate_code,	    				-- tax_rate_code
18127         zls.tax_rate, 		    				-- tax_rate
18128 	DECODE(ail.generate_dists,'D','D','Y')		        -- generate_dists  bug 5460342
18129         FROM zx_lines_summary zls
18130        WHERE zls.summary_tax_line_id		= ail.summary_tax_line_id
18131          AND nvl(zls.reporting_only_flag, 'N')	= 'N'
18132        )
18133        WHERE ail.invoice_id = P_Invoice_Header_Rec.invoice_id
18134 	 AND ail.line_type_lookup_code	= 'TAX'
18135          AND EXISTS
18136 	       	    (SELECT ls.summary_tax_line_id
18137                        FROM zx_lines_summary ls
18138                       WHERE ls.summary_tax_line_id		= ail.summary_tax_line_id
18139                         AND ls.trx_id				= ail.invoice_id
18140                         AND NVL(ls.tax_amt_included_flag, 'N')	= 'N'
18141                         AND NVL(ls.self_assessed_flag, 'N')	= 'N'
18142                         AND NVL(ls.reporting_only_flag, 'N')	= 'N');
18143 
18144     EXCEPTION
18145       WHEN NO_DATA_FOUND THEN
18146         NULL;
18147 
18148       WHEN OTHERS THEN
18149         IF (SQLCODE <> -20001) THEN
18150           FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
18151           FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
18152           FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
18153           FND_MESSAGE.SET_TOKEN('PARAMETERS',
18154             ' P_Invoice_Id = '||P_Invoice_Header_Rec.Invoice_Id||
18155             ' P_Error_Code = '||P_Error_Code||
18156             ' P_Calling_Sequence = '||P_Calling_Sequence);
18157           FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
18158         END IF;
18159 
18160       APP_EXCEPTION.RAISE_EXCEPTION;
18161     END;
18162 
18163     -------------------------------------------------------------------
18164     l_debug_info := 'Step 3: Get open gl_date';
18165     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
18166       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
18167     END IF;
18168     -------------------------------------------------------------------
18169 
18170     l_period_name := AP_UTILITIES_PKG.get_current_gl_date
18171 				(P_Invoice_Header_Rec.gl_date, P_Invoice_header_Rec.org_id);
18172 
18173     IF (l_period_name IS NULL) THEN
18174 	AP_UTILITIES_PKG.get_open_gl_date(
18175 	       P_Date                  => P_Invoice_Header_Rec.gl_date,
18176 	       P_Period_Name           => l_period_name,
18177 	       P_GL_Date               => l_gl_date,
18178 	       P_Org_Id                => P_Invoice_Header_Rec.org_id);
18179     ELSE
18180 	l_gl_date := P_Invoice_Header_Rec.gl_date;
18181     END IF;
18182 
18183     -------------------------------------------------------------------
18184     l_debug_info := 'Step 4: Determine if tax amount will be included in awt';
18185     -------------------------------------------------------------------
18186     -- Bug 9160534. Added P_Invoice_Header_Rec.SOURCE check.
18187     IF (NVL(P_Invoice_Header_Rec.SOURCE, 'DUMMY') = 'RTS' OR
18188         NVL(l_wfapproval_flag,'N') <> 'Y') THEN
18189       l_wfapproval_status := 'NOT REQUIRED';
18190     ELSE
18191       l_wfapproval_status := 'REQUIRED';
18192     END IF;
18193 
18194     -------------------------------------------------------------------
18195     l_debug_info := 'Step 5: Insert exclusive tax lines';
18196     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
18197       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
18198     END IF;
18199     -------------------------------------------------------------------
18200     BEGIN
18201       INSERT INTO ap_invoice_lines_all (
18202         invoice_id,
18203         line_number,
18204         line_type_lookup_code,
18205         requester_id,
18206         description,
18207         line_source,
18208         org_id,
18209         line_group_number,
18210         inventory_item_id,
18211         item_description,
18212         serial_number,
18213         manufacturer,
18214         model_number,
18215         warranty_number,
18216         generate_dists,
18217         match_type,
18218         distribution_set_id,
18219         account_segment,
18220         balancing_segment,
18221         cost_center_segment,
18222         overlay_dist_code_concat,
18223         default_dist_ccid,
18224         prorate_across_all_items,
18225         accounting_date,
18226         period_name,
18227         deferred_acctg_flag,
18228         def_acctg_start_date,
18229         def_acctg_end_date,
18230         def_acctg_number_of_periods,
18231         def_acctg_period_type,
18232         set_of_books_id,
18233         amount,
18234         base_amount,
18235         rounding_amt,
18236         quantity_invoiced,
18237         unit_meas_lookup_code,
18238         unit_price,
18239         wfapproval_status,
18240         discarded_flag,
18241         original_amount,
18242         original_base_amount,
18243         original_rounding_amt,
18244         cancelled_flag,
18245         income_tax_region,
18246         type_1099,
18247         stat_amount,
18248         prepay_invoice_id,
18249         prepay_line_number,
18250         invoice_includes_prepay_flag,
18251         corrected_inv_id,
18252         corrected_line_number,
18253         po_header_id,
18254         po_line_id,
18255         po_release_id,
18256         po_line_location_id,
18257         po_distribution_id,
18258         rcv_transaction_id,
18259         final_match_flag,
18260         assets_tracking_flag,
18261         asset_book_type_code,
18262         asset_category_id,
18263         project_id,
18264         task_id,
18265         expenditure_type,
18266         expenditure_item_date,
18267         expenditure_organization_id,
18268         pa_quantity,
18269         pa_cc_ar_invoice_id,
18270         pa_cc_ar_invoice_line_num,
18271         pa_cc_processed_code,
18272         award_id,
18273         awt_group_id,
18274         reference_1,
18275         reference_2,
18276         receipt_verified_flag,
18277         receipt_required_flag,
18278         receipt_missing_flag,
18279         justification,
18280         expense_group,
18281         start_expense_date,
18282         end_expense_date,
18283         receipt_currency_code,
18284         receipt_conversion_rate,
18285         receipt_currency_amount,
18286         daily_amount,
18287         web_parameter_id,
18288         adjustment_reason,
18289         merchant_document_number,
18290         merchant_name,
18291         merchant_reference,
18292         merchant_tax_reg_number,
18293         merchant_taxpayer_id,
18294         country_of_supply,
18295         credit_card_trx_id,
18296         company_prepaid_invoice_id,
18297         cc_reversal_flag,
18298         creation_date,
18299         created_by,
18300         last_updated_by,
18301         last_update_date,
18302         last_update_login,
18303         program_application_id,
18304         program_id,
18305         program_update_date,
18306         request_id,
18307         attribute_category,
18308         attribute1,
18309         attribute2,
18310         attribute3,
18311         attribute4,
18312         attribute5,
18313         attribute6,
18314         attribute7,
18315         attribute8,
18316         attribute9,
18317         attribute10,
18318         attribute11,
18319         attribute12,
18320         attribute13,
18321         attribute14,
18322         attribute15,
18323         global_attribute_category,
18324         global_attribute1,
18325         global_attribute2,
18326         global_attribute3,
18327         global_attribute4,
18328         global_attribute5,
18329         global_attribute6,
18330         global_attribute7,
18331         global_attribute8,
18332         global_attribute9,
18333         global_attribute10,
18334         global_attribute11,
18335         global_attribute12,
18336         global_attribute13,
18337         global_attribute14,
18338         global_attribute15,
18339         global_attribute16,
18340         global_attribute17,
18341         global_attribute18,
18342         global_attribute19,
18343         global_attribute20,
18344         control_amount,
18345         assessable_value,
18346         total_rec_tax_amount,
18347         total_nrec_tax_amount,
18348         total_rec_tax_amt_funcl_curr,
18349         total_nrec_tax_amt_funcl_curr,
18350         included_tax_amount,
18351         primary_intended_use,
18352         ship_to_location_id,
18353         product_type,
18354         product_category,
18355         product_fisc_classification,
18356         user_defined_fisc_class,
18357         trx_business_category,
18358         summary_tax_line_id,
18359         tax_regime_code,
18360         tax,
18361         tax_jurisdiction_code,
18362         tax_status_code,
18363         tax_rate_id,
18364         tax_rate_code,
18365         tax_rate,
18366         tax_code_id)
18367       SELECT
18368         P_Invoice_Header_Rec.Invoice_Id,				-- invoice_id
18369         (SELECT NVL(MAX(ail2.line_number),0)
18370            FROM ap_invoice_lines_all ail2
18371           WHERE ail2.invoice_id =  zls.trx_id) + ROWNUM,  		-- line_number
18372         'TAX',								-- line_type_lookup_code
18373         null,								-- requester_id
18374         NVL( AI.description, zls.tax_regime_code||' - '||zls.tax ),	-- description    Bug 10157751
18375         'ETAX',								-- line_source
18376         P_Invoice_Header_Rec.org_id,   					-- org_id
18377         null,   							-- line_group_number
18378         null,   							-- inventory_item_id
18379         null,   							-- item_description
18380         null,   							-- serial_number
18381         null,   							-- manufacturer
18382         null,   							-- model_number
18383         null,   							-- warranty_number
18384         DECODE(NVL(zls.tax_only_line_flag, 'N'),
18385                'Y', 'D',
18386                'Y'),   							-- generate_dists
18387         DECODE(zls.applied_to_trx_id,
18388                null, 'NOT_MATCHED',
18389                'OTHER_TO_RECEIPT'),   					-- match_type
18390         null,   							-- distribution_set_id
18391         null,   							-- account_segment
18392         null,   							-- balancing_segment
18393         null,   							-- cost_center_segment
18394         null,   							-- overlay_dist_code_concat
18395         null,   							-- default_dist_ccid
18396         'N',   								-- prorate_across_all_items
18397         l_gl_date,   							-- accounting_date
18398         DECODE(NVL(zls.tax_only_line_flag, 'N'),
18399                'N', DECODE(zls.applied_to_trx_id,
18400                            null, null, l_period_name),
18401                 l_period_name),   					-- period_name
18402         'N',   								-- deferred_acctg_flag
18403         null,   							-- def_acctg_start_date
18404         null,   							-- def_acctg_end_date
18405         null,   							-- def_acctg_number_of_periods
18406         null,   							-- def_acctg_period_type
18407         P_Invoice_Header_Rec.set_of_books_id,   			-- set_of_books_id
18408         zls.tax_amt,   							-- amount
18409         DECODE(P_Invoice_Header_Rec.invoice_currency_code,
18410                l_base_currency_code, NULL,
18411                zls.tax_amt_funcl_curr),    				-- base_amount
18412         null,   							-- rounding_amt
18413         null,   							-- quantity_invoiced
18414         null,   							-- unit_meas_lookup_code
18415         null,   							-- unit_price
18416         l_wfapproval_status,   						-- wfapproval_status
18417         'N',   								-- discarded_flag
18418         null,   							-- original_amount
18419         null,   							-- original_base_amount
18420         null,   							-- original_rounding_amt
18421         'N',   								-- cancelled_flag
18422         DECODE(ap.type_1099,
18423                '','',
18424                DECODE(l_combined_filing_flag,
18425                       'N', '',
18426                       DECODE(l_income_tax_region_flag,
18427                              'Y', aps.state,
18428                              l_income_tax_region))),  			-- income_tax_region
18429         ap.type_1099,   						-- type_1099
18430         null,   							-- stat_amount
18431         zls.applied_from_trx_id,   					-- prepay_invoice_id
18432         zls.applied_from_line_id,   					-- prepay_line_number
18433         prepay.invoice_includes_prepay_flag,   				-- invoice_includes_prepay_flag
18434         zls.adjusted_doc_trx_id,   					-- corrected_inv_id
18435         -- zls.adjusted_doc_line_id,   					-- corrected_line_number
18436         null,								-- corrected_line_number
18437         null,   							-- po_header_id
18438         null,   							-- po_line_id
18439         null,   							-- po_release_id
18440         null,   							-- po_line_location_id
18441         null,   							-- po_distribution_id
18442         zls.applied_to_trx_id,						-- rcv_transaction_id
18443         'N',   								-- final_match_flag
18444         null,   							-- assets_tracking_flag
18445         null,   							-- asset_book_type_code
18446         null,   							-- asset_category_id
18447         null,   							-- project_id
18448         null,   							-- task_id
18449         null,   							-- expenditure_type
18450         null,   							-- expenditure_item_date
18451         null,   							-- expenditure_organization_id
18452         null,   							-- pa_quantity
18453         null,   							-- pa_cc_ar_invoice_id
18454         null,   							-- pa_cc_ar_invoice_line_num
18455         null,   							-- pa_cc_processed_code
18456         null,   							-- award_id
18457         DECODE(l_awt_include_tax_amt,
18458                'N', null,
18459                DECODE(zls.applied_from_trx_id,
18460                       null, P_Invoice_Header_Rec.awt_group_id,
18461                       prepay.awt_group_id)),   				-- awt_group_id
18462         null,   							-- reference_1
18463         null,   							-- reference_2
18464         null,   							-- receipt_verified_flag
18465         null,   							-- receipt_required_flag
18466         null,   							-- receipt_missing_flag
18467         null,   							-- justification
18468         null,   							-- expense_group
18469         null,   							-- start_expense_date
18470         null,   							-- end_expense_date
18471         null,   							-- receipt_currency_code
18472         null,   							-- receipt_conversion_rate
18473         null,   							-- receipt_currency_amount
18474         null,   							-- daily_amount
18475         null,   							-- web_parameter_id
18476         null,   							-- adjustment_reason
18477         null,   							-- merchant_document_number
18478         null,   							-- merchant_name
18479         null,   							-- merchant_reference
18480         null,   							-- merchant_tax_reg_number
18481         null,								-- merchant_taxpayer_id
18482         null,								-- country_of_supply
18483         null,								-- credit_card_trx_id
18484         null,								-- company_prepaid_invoice_id
18485         null,								-- cc_reversal_flag
18486         l_sysdate,							-- creation_date
18487         l_user_id,   							-- created_by
18488         l_user_id,   							-- last_updated_by
18489         l_sysdate,   							-- last_update_date
18490         l_login_id,   							-- last_update_login
18491         null,   							-- program_application_id
18492         null,   							-- program_id
18493         null,   							-- program_update_date
18494         null,   							-- request_id
18495         zls.attribute_category,   					-- attribute_category
18496         zls.attribute1,   						-- attribute1
18497         zls.attribute2,   						-- attribute2
18498         zls.attribute3,   						-- attribute3
18499         zls.attribute4,   						-- attribute4
18500         zls.attribute5,   						-- attribute5
18501         zls.attribute6,   						-- attribute6
18502         zls.attribute7,   						-- attribute7
18503         zls.attribute8,   						-- attribute8
18504         zls.attribute9,   						-- attribute9
18505         zls.attribute10,   						-- attribute10
18506         zls.attribute11,   						-- attribute11
18507         zls.attribute12,   						-- attribute12
18508         zls.attribute13,   						-- attribute13
18509         zls.attribute14,   						-- attribute14
18510         zls.attribute15,   						-- attribute15
18511         zls.global_attribute_category,   				-- global_attribute_category
18512         zls.global_attribute1,   					-- global_attribute1
18513         zls.global_attribute2,   					-- global_attribute2
18514         zls.global_attribute3,   					-- global_attribute3
18515         zls.global_attribute4,   					-- global_attribute4
18516         zls.global_attribute5,   					-- global_attribute5
18517         zls.global_attribute6,   					-- global_attribute6
18518         zls.global_attribute7,   					-- global_attribute7
18519         zls.global_attribute8,   					-- global_attribute8
18520         zls.global_attribute9,   					-- global_attribute9
18521         zls.global_attribute10,   					-- global_attribute10
18522         zls.global_attribute11,   					-- global_attribute11
18523         zls.global_attribute12,   					-- global_attribute12
18524         zls.global_attribute13,   					-- global_attribute13
18525         zls.global_attribute14,   					-- global_attribute14
18526         zls.global_attribute15,   					-- global_attribute15
18527         zls.global_attribute16,   					-- global_attribute16
18528         zls.global_attribute17,   					-- global_attribute17
18529         zls.global_attribute18,   					-- global_attribute18
18530         zls.global_attribute19,   					-- global_attribute19
18531         zls.global_attribute20,   					-- global_attribute20
18532         null,   							-- control_amount
18533         null,   							-- assessable_value
18534         null,   							-- total_rec_tax_amount
18535         null,   							-- total_nrec_tax_amount
18536         null,   							-- total_rec_tax_amt_funcl_curr
18537         null,   							-- total_nrec_tax_amt_funcl_curr
18538         null,   							-- included_tax_amount
18539         null,   							-- primary_intended_use
18540         null,   							-- ship_to_location_id
18541         null,   							-- product_type
18542         null,   							-- product_category
18543         null,   							-- product_fisc_classification
18544         null,   							-- user_defined_fisc_class
18545         null,   							-- trx_business_category
18546         zls.summary_tax_line_id,   					-- summary_tax_line_id
18547         zls.tax_regime_code,   						-- tax_regime_code
18548         zls.tax,   							-- tax
18549         zls.tax_jurisdiction_code,   					-- tax_jurisdiction_code
18550         zls.tax_status_code,   						-- tax_status_code
18551         zls.tax_rate_id,   						-- tax_rate_id
18552         zls.tax_rate_code,   						-- tax_rate_code
18553         zls.tax_rate,   						-- tax_rate
18554         null   								-- tax_code_id
18555      FROM ap_invoices_all       ai,
18556           ap_suppliers          ap,
18557           ap_supplier_sites_all aps,
18558           zx_lines_summary      zls,
18559           ap_invoice_lines_all  prepay
18560     WHERE ai.invoice_id				= p_invoice_header_rec.invoice_id
18561       AND ai.vendor_id                          = ap.vendor_id
18562       AND ai.vendor_site_id                     = aps.vendor_site_id
18563       AND zls.application_id 			= 200
18564       AND zls.entity_code 			= 'AP_INVOICES'
18565       AND zls.event_class_code			IN ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
18566       AND zls.trx_id 				= ai.invoice_id
18567       AND NVL(zls.tax_amt_included_flag, 'N') 	= 'N'
18568       AND NVL(zls.self_assessed_flag, 'N') 	= 'N'
18569       AND NVL(zls.reporting_only_flag, 'N') 	= 'N'
18570       AND zls.applied_from_trx_id  		= prepay.invoice_id(+)
18571       AND zls.applied_from_line_id 		= prepay.line_number(+)
18572       AND NOT EXISTS (SELECT il.summary_tax_line_id
18573                         FROM ap_invoice_lines_all il
18574                        WHERE il.invoice_id = ai.invoice_id
18575                          AND il.summary_tax_line_id = zls.summary_tax_line_id);
18576 
18577     EXCEPTION
18578       WHEN NO_DATA_FOUND THEN
18579         NULL;
18580 
18581       WHEN OTHERS THEN
18582         IF (SQLCODE <> -20001) THEN
18583           FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
18584           FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
18585           FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
18586           FND_MESSAGE.SET_TOKEN('PARAMETERS',
18587             ' P_Invoice_Id = '||P_Invoice_Header_Rec.invoice_id||
18588             ' P_Error_Code = '||P_Error_Code||
18589             ' P_Calling_Sequence = '||P_Calling_Sequence);
18590           FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
18591         END IF;
18592 
18593       APP_EXCEPTION.RAISE_EXCEPTION;
18594     END;
18595 
18596     -------------------------------------------------------------------
18597     l_debug_info := 'Step 8: Delete exclusive tax lines if required';
18598     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
18599       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
18600     END IF;
18601     -------------------------------------------------------------------
18602     BEGIN
18603       DELETE ap_invoice_lines_all ail
18604        WHERE ail.invoice_id = P_Invoice_Header_Rec.invoice_id
18605          AND ail.line_type_lookup_code = 'TAX'
18606          AND NOT EXISTS (SELECT ls.summary_tax_line_id
18607                            FROM zx_lines_summary ls
18608                           WHERE ls.summary_tax_line_id	= ail.summary_tax_line_id
18609                             AND ls.trx_id 		= ail.invoice_id
18610                             AND NVL(ls.tax_amt_included_flag, 'N') = 'N'
18611                             AND NVL(ls.self_assessed_flag,    'N') = 'N'
18612                             AND NVL(ls.reporting_only_flag,   'N') = 'N');
18613 
18614     EXCEPTION
18615       WHEN NO_DATA_FOUND THEN
18616         NULL;
18617 
18618       WHEN OTHERS THEN
18619         IF (SQLCODE <> -20001) THEN
18620           FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
18621           FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
18622           FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
18623           FND_MESSAGE.SET_TOKEN('PARAMETERS',
18624             ' P_Invoice_Id = '||P_Invoice_Header_Rec.invoice_id||
18625             ' P_Error_Code = '||P_Error_Code||
18626             ' P_Calling_Sequence = '||P_Calling_Sequence);
18627           FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
18628         END IF;
18629 
18630       APP_EXCEPTION.RAISE_EXCEPTION;
18631     END;
18632 
18633     -------------------------------------------------------------------
18634     l_debug_info := 'Step 10: Update total_tax_amount and self_assessed tax';
18635     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
18636       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
18637     END IF;
18638     -------------------------------------------------------------------
18639     /*Bug 8638881 added CASE in the below SQL to consider the case of invoice includes prepay*/
18640     BEGIN
18641       UPDATE ap_invoices_all ai
18642           SET (ai.total_tax_amount,
18643                ai.self_assessed_tax_amount) =
18644                    (SELECT SUM(DECODE(NVL(zls.self_assessed_flag, 'N'),
18645                             'N', case when exists (SELECT 'Prepay App Exists'
18646                                           FROM ap_invoice_lines_all prepay
18647                                          WHERE prepay.invoice_id = zls.trx_id
18648                                          AND prepay.line_type_lookup_code = 'PREPAY'
18649                                          AND prepay.prepay_invoice_id  = zls.applied_from_trx_id
18650                                          AND prepay.prepay_line_number = zls.applied_from_line_id
18651                                          AND prepay.invoice_includes_prepay_flag = 'Y'
18652                                          AND (prepay.discarded_flag is null
18653                                            or prepay.discarded_flag = 'N')) THEN
18654                                            0
18655                                         ELSE NVL(zls.tax_amt, 0) end,
18656                                        0)),
18657                         SUM(DECODE(NVL(zls.self_assessed_flag, 'N'),
18658                                    'Y', NVL(zls.tax_amt, 0),
18659                                     0))
18660                    FROM zx_lines_summary zls
18661             WHERE zls.application_id = 200
18662            AND zls.entity_code = 'AP_INVOICES'
18663            AND zls.event_class_code IN
18664               ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
18665                   AND zls.trx_id   = ai.invoice_id
18666                   AND NVL(zls.reporting_only_flag, 'N') = 'N')
18667         WHERE ai.invoice_id = P_Invoice_Header_Rec.invoice_id
18668         RETURNING ai.total_tax_amount, ai.self_assessed_tax_amount
18669              INTO l_total_tax_amount, l_self_assessed_tax_amt;
18670 
18671     EXCEPTION
18672       WHEN NO_DATA_FOUND THEN
18673         NULL;
18674 
18675       WHEN OTHERS THEN
18676         IF (SQLCODE <> -20001) THEN
18677           FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
18678           FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
18679           FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
18680           FND_MESSAGE.SET_TOKEN('PARAMETERS',
18681             ' P_Invoice_Id = '||P_Invoice_Header_Rec.invoice_id||
18682             ' P_Error_Code = '||P_Error_Code||
18683             ' P_Calling_Sequence = '||P_Calling_Sequence);
18684           FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
18685         END IF;
18686 
18687       APP_EXCEPTION.RAISE_EXCEPTION;
18688     END;
18689 
18690     -----------------------------------------------------------------
18691     l_debug_info := 'Step 12: Update Invoice Includes Prepay Flag';
18692     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
18693       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
18694     END IF;
18695     -----------------------------------------------------------------
18696 
18697     /*added the case for bug 8638881*/
18698    UPDATE ap_invoice_lines_all tax
18699      SET tax.invoice_includes_prepay_flag = CASE WHEN EXISTS (SELECT 'Prepay App Exists'
18700                             FROM ap_invoice_lines_all prepay
18701                            WHERE prepay.invoice_id = tax.invoice_id
18702                              AND prepay.line_type_lookup_code = 'PREPAY'
18703                              AND prepay.prepay_invoice_id = tax.prepay_invoice_id
18704                              AND prepay.prepay_line_number = tax.prepay_line_number
18705                              AND prepay.invoice_includes_prepay_flag = 'Y'
18706                              AND (prepay.discarded_flag is null or
18707                                  prepay.discarded_flag = 'N')) THEN
18708                                'Y'
18709                             ELSE
18710                               'N'
18711                             END /*added the case for bug 8638881*/
18712    WHERE tax.invoice_id = P_Invoice_Header_Rec.Invoice_Id
18713     AND tax.line_type_lookup_code = 'TAX'
18714     AND tax.prepay_invoice_id is not null;
18715 
18716     RETURN TRUE;
18717 
18718   EXCEPTION
18719     WHEN OTHERS THEN
18720       IF (SQLCODE <> -20001) THEN
18721         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
18722         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
18723         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
18724         FND_MESSAGE.SET_TOKEN('PARAMETERS',
18725           			' P_Invoice_Id = '      ||P_Invoice_Header_Rec.Invoice_Id||
18726           			' P_Error_Code = '      ||P_Error_Code||
18727           			' P_Calling_Sequence = '||P_Calling_Sequence);
18728         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
18729       END IF;
18730 
18731       APP_EXCEPTION.RAISE_EXCEPTION;
18732 
18733   END Return_Other_Charge_Lines;
18734 
18735 --Bug 7570234 Start
18736 /*=============================================================================
18737  |  FUNCTION - synchronize_for_doc_seq()
18738  |
18739  |  DESCRIPTION
18740  |      Public function that will call ZX_API_PUB.synchronize_tax_repository
18741  |      to make sync between AP and ZX
18742   *============================================================================*/
18743 PROCEDURE synchronize_for_doc_seq
18744       (
18745             p_invoice_id       IN NUMBER ,
18746             p_calling_sequence IN VARCHAR2 ,
18747             x_return_status    OUT NOCOPY VARCHAR2)
18748 IS
18749 
18750       l_debug_info                 VARCHAR2(240);
18751       l_api_name                   CONSTANT VARCHAR2(100) := 'Update';
18752       l_curr_calling_sequence      VARCHAR2(4000);
18753 
18754       l_message_count              NUMBER;
18755       l_message_data               VARCHAR2(2000) ;
18756       l_Error_Code                 VARCHAR2(2000) ;
18757       l_return_status              VARCHAR2(2000) ;
18758       l_sync_trx_rec               ZX_API_PUB.sync_trx_rec_type;
18759       l_sync_trx_lines_t           ZX_API_PUB.sync_trx_lines_tbl_type%type;
18760 
18761       --Bug8975892
18762 
18763       l_invoice_type_lookup_code   AP_INVOICES_ALL.invoice_type_lookup_code%type;
18764       l_quick_credit               AP_INVOICES_ALL.quick_credit%type;
18765       l_credited_invoice_id        AP_INVOICES_ALL.credited_invoice_id%type;
18766       l_credited_inv_rec           AP_INVOICES_ALL%ROWTYPE;
18767 
18768       --Bug8975892
18769 
18770 
18771 
18772       CURSOR c_trx
18773       IS
18774              SELECT
18775                   (CASE
18776                         WHEN ai.invoice_type_lookup_code IN('STANDARD' , 'CREDIT' , 'DEBIT' ,
18777                               'MIXED' , 'ADJUSTMENT' , 'PO PRICE ADJUST' , 'INVOICE REQUEST' ,
18778                               'CREDIT MEMO REQUEST' , 'RETAINAGE RELEASE','PAYMENT REQUEST')--Bug9122724
18779                         THEN 'STANDARD INVOICES'
18780                         WHEN(ai.invoice_type_lookup_code='PREPAYMENT')
18781                         THEN 'PREPAYMENT INVOICES'
18782                         WHEN ai.invoice_type_lookup_code='EXPENSE REPORT'
18783                         THEN 'EXPENSE REPORTS'
18784                   END) event_class_code
18785                 ,
18786                   (CASE
18787                         WHEN ai.invoice_type_lookup_code IN('STANDARD' , 'CREDIT' , 'DEBIT' ,
18788                               'MIXED' , 'ADJUSTMENT' , 'PO PRICE ADJUST' , 'INVOICE REQUEST' ,
18789                               'CREDIT MEMO REQUEST' , 'RETAINAGE RELEASE','PAYMENT REQUEST')--Bug9122724
18790                         THEN 'STANDARD '
18791                         WHEN(ai.invoice_type_lookup_code='PREPAYMENT')
18792                         THEN 'PREPAYMENT '
18793                         WHEN ai.invoice_type_lookup_code='EXPENSE REPORT'
18794                         THEN 'EXPENSE REPORT '
18795                   END) || 'UPDATED' event_type_code
18796                 , ai.invoice_id trx_id
18797                 , ai.invoice_num trx_number
18798                 , SUBSTRB(ai.description , 1 , 240) trx_description
18799                 , ai.doc_sequence_id doc_seq_id
18800                 , doc.name doc_seq_name
18801                 , ai.doc_sequence_value doc_seq_value
18802                 , ai.batch_id batch_source_id
18803                 , NULL batch_source_name
18804                 , NULL trx_type_description
18805                 , ai.invoice_date trx_communicated_date
18806                 , ai.terms_date trx_due_date
18807                 , ai.supplier_tax_invoice_number supplier_tax_invoice_number
18808                 , ai.supplier_tax_invoice_date supplier_tax_invoice_date
18809                 , ai.supplier_tax_exchange_rate supplier_exchange_rate
18810                 , ai.tax_invoice_internal_seq tax_invoice_number
18811                 , ai.tax_invoice_recording_date tax_invoice_date
18812                 , ai.tax_invoice_internal_seq tax_invoice_number
18813                 , ai.invoice_type_lookup_code
18814                 , ai.quick_credit
18815                 , ai.credited_invoice_id
18816                FROM ap_invoices_all ai
18817                    , fnd_document_sequences doc
18818               WHERE ai.invoice_id=p_invoice_id
18819                 AND ai.doc_sequence_id=doc.doc_sequence_id (+);
18820 
18821 BEGIN
18822 
18823       --Print(l_api_name,'AP_ETAX_SERVICES_PKG.synchronize_for_doc_seq');
18824       l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.synchronize_for_doc_seq  <- '||p_calling_sequence;
18825       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
18826          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_curr_calling_sequence);
18827       END IF;
18828 
18829       l_debug_info := 'Step. 1 Open c_trx cursor';
18830 
18831       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
18832          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
18833       END IF;
18834 
18835       l_sync_trx_rec.application_id:=200;
18836       l_sync_trx_rec.entity_code   :='AP_INVOICES';
18837 
18838       IF p_invoice_id  IS NOT NULL THEN
18839 
18840         l_debug_info := 'Step 2. The invoice is  '||p_invoice_id;
18841         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
18842            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
18843         END IF;
18844         --Print(l_api_name,l_debug_info);
18845 
18846             OPEN c_trx;
18847 
18848             FETCH c_trx INTO
18849                   l_sync_trx_rec.event_class_code
18850                 , l_sync_trx_rec.event_type_code
18851                 , l_sync_trx_rec.trx_id
18852                 , l_sync_trx_rec.trx_number
18853                 , l_sync_trx_rec.trx_description
18854                 , l_sync_trx_rec.doc_seq_id
18855                 , l_sync_trx_rec.doc_seq_name
18856                 , l_sync_trx_rec.doc_seq_value
18857                 , l_sync_trx_rec.batch_source_id
18858                 , l_sync_trx_rec.batch_source_name
18859                 , l_sync_trx_rec.trx_type_description
18860                 , l_sync_trx_rec.trx_communicated_date
18861                 , l_sync_trx_rec.trx_due_date
18862                 , l_sync_trx_rec.supplier_tax_invoice_number
18863                 , l_sync_trx_rec.supplier_tax_invoice_date
18864                 , l_sync_trx_rec.supplier_exchange_rate
18865                 , l_sync_trx_rec.tax_invoice_number
18866                 , l_sync_trx_rec.tax_invoice_date
18867                 , l_sync_trx_rec.tax_invoice_number
18868                 , l_invoice_type_lookup_code
18869                 , l_quick_credit
18870                 , l_credited_invoice_id;
18871 
18872 
18873         --Bug8975892
18874 
18875         IF (l_invoice_type_lookup_code IN ('AWT','INTEREST')) THEN
18876            l_debug_info := 'Step 2.1. Exiting with Success since AWT/Interest Invoice ';
18877            IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
18878               FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
18879            END IF;
18880            --Print(l_api_name,l_debug_info);
18881            x_return_status:= FND_API.G_RET_STS_SUCCESS;
18882            RETURN ;
18883         END IF;
18884 
18885         IF (l_quick_credit = 'Y' AND l_credited_invoice_id IS NOT NULL)THEN
18886            IF NOT tax_distributions_exist(p_invoice_id  => l_credited_invoice_id) THEN
18887               l_debug_info := 'Step 2.2. Exiting with Success since Quick Invoice With no tax';
18888               IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
18889                  FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
18890               END IF;
18891               --Print(l_api_name,l_debug_info);
18892               x_return_status:= FND_API.G_RET_STS_SUCCESS;
18893               RETURN;
18894            END IF;
18895         END IF;
18896 
18897         --Bug8975892
18898 
18899         --Bug9122724
18900         IF NVL(l_quick_credit,'N') = 'N' THEN
18901            IF NOT (AP_ETAX_UTILITY_PKG.Is_Tax_Already_Calc_Inv(
18902 	                 P_Invoice_Id           => p_invoice_id,
18903 	                 P_Calling_Sequence     => l_curr_calling_sequence)) THEN
18904                         l_debug_info := 'Step 2.3. Tax Already Calculated Flag is N';
18905                         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
18906                            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
18907                         END IF;
18908                         x_return_status:= FND_API.G_RET_STS_SUCCESS;
18909               RETURN;
18910            END IF;
18911         END IF;
18912         --Bug9122724
18913 
18914         l_debug_info := 'Step 3. Before calling ZX_API_PUB.synchronize_tax_repository() API ';
18915         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
18916             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
18917         END IF;
18918         --Print(l_api_name,l_debug_info);
18919 
18920 
18921             ZX_API_PUB.synchronize_tax_repository(
18922                                p_api_version        =>  1.0
18923                              , p_init_msg_list      =>  FND_API.G_FALSE
18924                              , p_commit             =>  FND_API.G_FALSE
18925                              , p_validation_level   =>  FND_API.G_VALID_LEVEL_FULL
18926                              , x_return_status      =>  l_return_status
18927                              , x_msg_count          =>  l_message_count
18928                              , x_msg_data           =>  l_message_data
18929                              , p_sync_trx_rec       =>  l_sync_trx_rec
18930                              , p_sync_trx_lines_tbl =>  l_sync_trx_lines_t) ;
18931 
18932             l_debug_info := 'Step 4. Retun status is '||l_return_status;
18933             IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
18934                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
18935             END IF;
18936             --Print(l_api_name,l_debug_info);
18937 
18938             IF (l_return_status = FND_API.G_RET_STS_SUCCESS) then
18939                 x_return_status:= FND_API.G_RET_STS_SUCCESS;
18940             ELSE
18941                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
18942                 IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
18943                        P_All_Error_Messages  => 'Y',
18944                        P_Msg_Count           => l_message_count,
18945                        P_Msg_Data            => l_message_data,
18946                        P_Error_Code          => l_Error_Code,
18947                        P_Calling_Sequence    => l_curr_calling_sequence)) THEN
18948 
18949                      NULL;
18950 
18951                 END IF;
18952             END IF;
18953 
18954             CLOSE c_trx;
18955       ELSE -- IF p_invoice_id  IS NOT NULL THEN
18956 
18957         l_debug_info := 'Step 5. Invoice ID is null';
18958         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
18959             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
18960         END IF;
18961         --Print(l_api_name,l_debug_info);
18962 
18963         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
18964         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
18965         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
18966         FND_MESSAGE.SET_TOKEN('PARAMETERS',' P_Invoice_Id = '||p_invoice_id||
18967                               ' l_curr_calling_sequence = '||l_curr_calling_sequence);
18968         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
18969 
18970         APP_EXCEPTION.RAISE_EXCEPTION;
18971 
18972       END IF;  -- IF p_invoice_id  IS NOT NULL THEN
18973 
18974         l_debug_info := 'Step 6. End of the API';
18975         Print(l_api_name,l_debug_info);
18976   EXCEPTION
18977     WHEN OTHERS THEN
18978       IF (SQLCODE <> -20001) THEN
18979         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
18980         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
18981         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
18982         FND_MESSAGE.SET_TOKEN('PARAMETERS',
18983           ' P_Invoice_Id = '||p_invoice_id||
18984           ' l_Error_Code ='||l_Error_Code||
18985           ' l_curr_calling_sequence = '||l_curr_calling_sequence);
18986         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
18987       END IF;
18988       APP_EXCEPTION.RAISE_EXCEPTION;
18989 END synchronize_for_doc_seq;
18990 --Bug 7570234 End
18991 
18992 --Bug9819170
18993 /*=============================================================================
18994  |  FUNCTION - synchronize_tax_dff()
18995  |
18996  |  DESCRIPTION
18997  |      Public function that will call ZX_NEW_SERVICES_PKG.SYNC_TAX_DIST_DFF()
18998  |      to sync DFF on Tax distribution from AP to Ebtax
18999   *============================================================================*/
19000  PROCEDURE synchronize_tax_dff
19001            	(p_invoice_id                 IN NUMBER ,
19002              p_invoice_dist_id            IN NUMBER   DEFAULT NULL,
19003              p_related_id                 IN NUMBER   DEFAULT NULL,
19004              p_detail_tax_dist_id         IN NUMBER   DEFAULT NULL,
19005              p_line_type_lookup_code      IN VARCHAR2 DEFAULT NULL,
19006              p_invoice_line_number        IN NUMBER,
19007              p_distribution_line_number   IN NUMBER,
19008              P_ATTRIBUTE1                 IN VARCHAR2,
19012              P_ATTRIBUTE5                 IN VARCHAR2,
19009              P_ATTRIBUTE2                 IN VARCHAR2,
19010              P_ATTRIBUTE3                 IN VARCHAR2,
19011              P_ATTRIBUTE4                 IN VARCHAR2,
19013              P_ATTRIBUTE6                 IN VARCHAR2,
19014              P_ATTRIBUTE7                 IN VARCHAR2,
19015              P_ATTRIBUTE8                 IN VARCHAR2,
19016              P_ATTRIBUTE9                 IN VARCHAR2,
19017              P_ATTRIBUTE10                IN VARCHAR2,
19018              P_ATTRIBUTE11                IN VARCHAR2,
19019              P_ATTRIBUTE12                IN VARCHAR2,
19020              P_ATTRIBUTE13                IN VARCHAR2,
19021              P_ATTRIBUTE14                IN VARCHAR2,
19022              P_ATTRIBUTE15                IN VARCHAR2,
19023              P_ATTRIBUTE_CATEGORY         IN VARCHAR2,
19024        	     p_calling_sequence           IN VARCHAR2 ,
19025              x_return_status              OUT NOCOPY VARCHAR2)
19026 IS
19027 
19028       l_debug_info                 VARCHAR2(2000);
19029       l_api_name                   CONSTANT VARCHAR2(100) := 'synchronize_tax_dff';
19030       l_curr_calling_sequence      VARCHAR2(4000);
19031 
19032       l_message_count              NUMBER;
19033       l_message_data               VARCHAR2(2000) ;
19034       l_Error_Code                 VARCHAR2(2000) ;
19035       l_return_status              VARCHAR2(2000) ;
19036 
19037       l_detail_tax_dist_id     AP_INVOICE_DISTRIBUTIONS_ALL.DETAIL_TAX_DIST_ID%TYPE;
19038       l_line_type_lookup_code  AP_INVOICE_DISTRIBUTIONS_ALL.LINE_TYPE_LOOKUP_CODE%TYPE;
19039       l_inv_dist_id            AP_INVOICE_DISTRIBUTIONS_ALL.INVOICE_DISTRIBUTION_ID%TYPE;
19040       l_related_id             AP_INVOICE_DISTRIBUTIONS_ALL.RELATED_ID%TYPE;
19041 
19042 
19043       l_sync_trx_dist_dff_t        ZX_NEW_SERVICES_PKG.tax_dist_dff_type%TYPE;
19044 
19045 BEGIN
19046 
19047       l_curr_calling_sequence := 'AP_ETAX_SERVICES_PKG.synchronize_tax_dff  <- '||p_calling_sequence;
19048 
19049       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
19050          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_curr_calling_sequence);
19051       END IF;
19052 
19053       l_debug_info := 'Synching DFFS For Invoice Id '||p_invoice_id;
19054 
19055       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
19056            FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19057       END IF;
19058 
19059       IF p_invoice_dist_id IS NULL THEN
19060 
19061          l_debug_info := 'Get the details based on Invoice Line Number, Distribution Line Number and Invoice Id ';
19062 
19063          IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
19064              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19065          END IF;
19066 
19067           l_debug_info := 'Invoice Line Number '||p_invoice_line_number||
19068                           ' and Distribution Line Number '||p_distribution_line_number;
19069 
19070           IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
19071              FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19072           END IF;
19073 
19074 
19075          BEGIN
19076 
19077            SELECT line_type_lookup_code,detail_tax_dist_id,
19078                   invoice_distribution_id,related_id
19079              INTO l_line_type_lookup_code,l_detail_tax_dist_id,
19080                   l_inv_dist_id,l_related_id
19081              FROM ap_invoice_distributions_all
19082             WHERE invoice_id               = p_invoice_id
19083               AND distribution_line_number = p_distribution_line_number
19084               AND invoice_line_number      = p_invoice_line_number;
19085 
19086          EXCEPTION
19087               WHEN OTHERS THEN
19088                    APP_EXCEPTION.RAISE_EXCEPTION;
19089          END;
19090 
19091 
19092 
19093       END IF;
19094 
19095 
19096       l_debug_info := 'Step. 1 Local Related Dist Id '||l_related_id||
19097                       ', Parameter Related Dist Id '||p_related_id||
19098                       ', Local Invoice Dist Id '||l_inv_dist_id||
19099                       ', Parameter Invoice Dist Id '||p_invoice_dist_id||
19100                       ', Local Detail Tax Dist Id '||l_detail_tax_dist_id||
19101                       ', Parameter Detail Tax Dist Id '||p_detail_tax_dist_id||
19102                       ', Local Line Type Lookup Code '||l_line_type_lookup_code||
19103                       ', Parameter Line Type Lookup Code '||p_line_type_lookup_code;
19104 
19105       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
19106          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19107       END IF;
19108 
19109 
19110       l_related_id            := NVL(l_related_id,p_related_id);
19111       l_line_type_lookup_code := NVL(l_line_type_lookup_code,p_line_type_lookup_code);
19112       l_detail_tax_dist_id    := NVL(l_detail_tax_dist_id,p_detail_tax_dist_id);
19113       l_inv_dist_id           := NVL(l_inv_dist_id,p_invoice_dist_id);
19114 
19115 
19116       l_debug_info := 'Step. 2 All Local : Related Dist Id '||l_related_id||
19117                       ', Invoice Dist Id '||l_inv_dist_id||
19118                       ', Detail Tax Dist Id '||l_detail_tax_dist_id||
19119                       ', Line Type Lookup Code '||l_line_type_lookup_code;
19120 
19121 
19122       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
19123          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19124       END IF;
19125 
19126 
19127       IF (l_related_id IS NULL OR l_related_id = l_inv_dist_id)
19128          AND (l_line_type_lookup_code IN ('REC_TAX','NONREC_TAX','TRV','TERV','TIPV'))
19129          AND (l_detail_tax_dist_id IS NOT NULL) THEN
19130 
19131         l_debug_info := 'Step 2. Before calling ZX_API_PUB.SYNC_TAX_DIST_DFF() API ';
19132         IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
19133             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19134         END IF;
19135 
19136             l_sync_trx_dist_dff_t.attribute1(1) :=  P_ATTRIBUTE1;
19137             l_sync_trx_dist_dff_t.attribute2(1) :=  P_ATTRIBUTE2;
19138             l_sync_trx_dist_dff_t.attribute3(1) :=  P_ATTRIBUTE3;
19139             l_sync_trx_dist_dff_t.attribute4(1) :=  P_ATTRIBUTE4;
19140             l_sync_trx_dist_dff_t.attribute5(1) :=  P_ATTRIBUTE5;
19141             l_sync_trx_dist_dff_t.attribute6(1) :=  P_ATTRIBUTE6;
19142             l_sync_trx_dist_dff_t.attribute7(1) :=  P_ATTRIBUTE7;
19143             l_sync_trx_dist_dff_t.attribute8(1) :=  P_ATTRIBUTE8;
19144             l_sync_trx_dist_dff_t.attribute9(1) :=  P_ATTRIBUTE9;
19145             l_sync_trx_dist_dff_t.attribute10(1) := P_ATTRIBUTE10;
19146             l_sync_trx_dist_dff_t.attribute11(1) := P_ATTRIBUTE11;
19147             l_sync_trx_dist_dff_t.attribute12(1) := P_ATTRIBUTE12;
19148             l_sync_trx_dist_dff_t.attribute13(1) := P_ATTRIBUTE13;
19149             l_sync_trx_dist_dff_t.attribute14(1) := P_ATTRIBUTE14;
19150             l_sync_trx_dist_dff_t.attribute15(1) := P_ATTRIBUTE15;
19151             l_sync_trx_dist_dff_t.attribute_category(1) := P_attribute_category;
19152             l_sync_trx_dist_dff_t.rec_nrec_tax_dist_id(1) := l_detail_tax_dist_id ;
19153 
19154 
19155             IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
19156                l_debug_info := '1 '||  l_sync_trx_dist_dff_t.attribute1(1);
19157                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19158                l_debug_info := '2 '||l_sync_trx_dist_dff_t.attribute2(1) ;
19162                l_debug_info := '4 '||l_sync_trx_dist_dff_t.attribute4(1);
19159                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19160                l_debug_info := '3 '||l_sync_trx_dist_dff_t.attribute3(1);
19161                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19163                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19164                l_debug_info := '5 '||l_sync_trx_dist_dff_t.attribute5(1);
19165                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19166                l_debug_info := '6 '||l_sync_trx_dist_dff_t.attribute6(1);
19167                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19168                l_debug_info := '7 '||l_sync_trx_dist_dff_t.attribute7(1);
19169                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19170                l_debug_info := '8 '||l_sync_trx_dist_dff_t.attribute8(1);
19171                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19172                l_debug_info := '9 '||l_sync_trx_dist_dff_t.attribute9(1);
19173                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19174                l_debug_info := '10 '||l_sync_trx_dist_dff_t.attribute10(1);
19175                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19176                l_debug_info := '11 '||l_sync_trx_dist_dff_t.attribute11(1);
19177                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19178                l_debug_info := '12 '||l_sync_trx_dist_dff_t.attribute12(1);
19179                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19180                l_debug_info := '13 '||l_sync_trx_dist_dff_t.attribute14(1);
19181                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19182                l_debug_info := '14 '||l_sync_trx_dist_dff_t.attribute14(1);
19183                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19184                l_debug_info := '15 '||l_sync_trx_dist_dff_t.attribute15(1);
19185                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19186                l_debug_info := 'Attrib Cat '||l_sync_trx_dist_dff_t.attribute_category(1);
19187                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19188                l_debug_info := 'Detail Tax Dist Id '||l_sync_trx_dist_dff_t.rec_nrec_tax_dist_id(1);
19189                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19190             END IF;
19191 
19192             ZX_NEW_SERVICES_PKG.SYNC_TAX_DIST_DFF(
19193                                p_api_version        =>  1.0
19194                              , p_init_msg_list      =>  FND_API.G_FALSE
19195                              , p_commit             =>  FND_API.G_FALSE
19196                              , p_validation_level   =>  FND_API.G_VALID_LEVEL_FULL
19197                              , x_return_status      =>  l_return_status
19198                              , x_msg_count          =>  l_message_count
19199                              , x_msg_data           =>  l_message_data
19200                              , p_tax_dist_dff_tbl   =>  l_sync_trx_dist_dff_t);
19201 
19202             l_debug_info := 'Step 4. Retun status is '||l_return_status;
19203             IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
19204                FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,l_debug_info);
19205             END IF;
19206 
19207             IF (l_return_status = FND_API.G_RET_STS_SUCCESS) THEN
19208                 x_return_status:= FND_API.G_RET_STS_SUCCESS;
19209             ELSE
19210                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
19211                 IF NOT(AP_ETAX_UTILITY_PKG.Return_Error_Messages(
19212                        P_All_Error_Messages  => 'Y',
19213                        P_Msg_Count           => l_message_count,
19214                        P_Msg_Data            => l_message_data,
19215                        P_Error_Code          => l_Error_Code,
19216                        P_Calling_Sequence    => l_curr_calling_sequence)) THEN
19217 
19218                      NULL;
19219                 END IF;
19220             END IF;
19221      END IF;
19222 
19223 EXCEPTION
19224     WHEN OTHERS THEN
19225       IF (SQLCODE <> -20001) THEN
19226         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
19227         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
19228         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',l_curr_calling_sequence);
19229         FND_MESSAGE.SET_TOKEN('PARAMETERS',
19230           ' P_Invoice_Id = '||p_invoice_id||
19231           ' l_Error_Code ='||l_Error_Code||
19232           ' l_curr_calling_sequence = '||l_curr_calling_sequence);
19233         FND_MESSAGE.SET_TOKEN('DEBUG_INFO',l_debug_info);
19234       END IF;
19235       APP_EXCEPTION.RAISE_EXCEPTION;
19236 END synchronize_tax_dff;
19237 --Bug9819170
19238 
19239 PROCEDURE Print
19240              (P_API_NAME   IN VARCHAR2,
19241               p_debug_info IN VARCHAR2) IS
19242 BEGIN
19243   IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
19244       FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||p_api_name,p_debug_info);
19245   END IF;
19246 END Print;
19247 
19248 END AP_ETAX_SERVICES_PKG;