DBA Data[Home] [Help]

PACKAGE BODY: APPS.AP_RETRO_PRICING_PKG

Source


1 PACKAGE BODY AP_RETRO_PRICING_PKG  AS
2 /* $Header: apretrob.pls 120.44.12020000.10 2013/03/11 14:14:19 harsanan ship $ */
3 
4 G_PKG_NAME          CONSTANT VARCHAR2(30) := 'AP_RETRO_PRICING_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_RETRO_PRICING_PKG.';
21 
22 
23 /*=============================================================================
24  |  PROCEDURE - insert_ap_inv_interface()
25  |
26  |  DESCRIPTION
27  |      Private procedure called from Create_Instructions. Program identifies PO
28  |      suppliers that are listed in the PO view within the report parameters.
29  |      It then populates the Payables Open Interface Table with one instruction
30  |      record per supplier.  Each header record will include: a source of PPA,
31  |      the supplier ID, userid of the PO user and a unique group_id for the CADIP.
32  |
33  |  PARAMETERS
34  |      p_group_id       - Unique group_id generated in Create_Instructions
35  |      p_org_id         - Org Id of the PO User
36  |      p_po_user_id     - PO's User Id
37  |      p_vendor_id      - Vendor Id
38  |      p_vendor_site_id - Vendor Site Id
39  |      p_po_header_id   - Valid PO's Header Id
40  |      p_po_release_id  - Valid PO Release Id
41  |      P_calling_sequence -  Calling sequence
42  |
43  |  MODIFICATION HISTORY
44  |  Date         Author             Description of Change
45  |  29-JUL-2003  dgulraja           Creation
46  |
47  *============================================================================*/
48 PROCEDURE insert_ap_inv_interface (
49            p_group_id         IN     VARCHAR2,
50            p_org_id           IN     NUMBER,
51            p_po_user_id       IN     NUMBER,
52            p_vendor_id        IN     NUMBER,
53            p_vendor_site_id   IN     NUMBER,
54            p_po_header_id     IN     NUMBER,
55            p_po_release_id    IN     NUMBER,
56            p_calling_sequence IN     VARCHAR2) IS
57 
58 l_vendor_id_list            id_list_type;
59 l_vendor_num_list           vendor_num_list_type;
60 l_vendor_name_list          vendor_name_list_type;
61 
62 
63 current_calling_sequence    VARCHAR2(2000);
64 debug_info                  VARCHAR2(2000);
65 l_api_name              CONSTANT VARCHAR2(200) := 'INSERT_AP_INV_INTERFACE';
66 
67 --Bug 5048503 added new ref cursor variable
68 Type vendorcur          is REF CURSOR;
69 vendor_cur              vendorcur;
70 sql_stmt                varchar2(4000);
71 
72 /* Bug 5048503. will replace the cursor def with dynamic query
73 CURSOR vendor  IS
74 SELECT DISTINCT pd.vendor_id,
75        pv.segment1,  -- supplier number
76        pv.vendor_name
77   FROM po_ap_retroactive_dist_v pd,
78        po_vendors pv
79  WHERE mo_global.check_access(pd.org_id) = 'Y'
80    AND pd.vendor_id = pv.vendor_id
81    AND pd.invoice_adjustment_flag = 'R'
82    AND pd.org_id         = p_org_id
83    AND pd.vendor_id      = DECODE(p_vendor_id, NULL,
84                                   pd.vendor_id, p_vendor_id)
85    AND pd.po_header_id   = DECODE(p_po_header_id, NULL,
86                                   pd.po_header_id, p_po_header_id)
87    -- Commented out until bug 4484058 is resolved.
88    AND pd.vendor_site_id = DECODE(p_vendor_site_id, NULL,
89                                   pd.vendor_site_id, p_vendor_site_id)
90    AND NVL(pd.po_release_id, 1)  = DECODE(p_po_release_id, NULL,
91                                           NVL(pd.po_release_id,1),
92                                               p_po_release_id);*/
93    --
94 BEGIN
95   --
96   current_calling_sequence := 'init<-'||P_calling_sequence;
97 
98   IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
99     FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_RETRO_PRICING_PKG.INSERT_AP_INV_INTERFACE(+)');
100  END IF;
101 
102 -- Bug 5048503 : starts
103   -------------------------------------------------
104   debug_info := ' dynamic query for Vendor_cursor';
105   -------------------------------------------------
106 
107   sql_stmt :=  'SELECT DISTINCT pd.vendor_id,
108                 pv.segment1,  -- supplier number
109                 pv.vendor_name
110                 FROM po_ap_retroactive_dist_v pd,
111                      po_vendors pv
112                 WHERE mo_global.check_access(pd.org_id) = ''Y''
113                 AND pd.vendor_id = pv.vendor_id
114                 AND pd.invoice_adjustment_flag = ''R'' ' ;
115 
116   IF ( p_org_id is NOT NULL) then
117       sql_stmt := sql_stmt ||' AND pd.org_id = ' || p_org_id  ;
118   END IF;
119   IF ( p_vendor_id is NOT NULL) then
120       sql_stmt := sql_stmt ||' AND pd.vendor_id = ' || p_vendor_id ;
121   END IF;
122   IF ( p_po_header_id is NOT NULL) then
123       sql_stmt := sql_stmt ||' AND pd.po_header_id = '|| p_po_header_id  ;
124   END IF;
125   IF ( p_vendor_site_id is NOT NULL) then
126       sql_stmt := sql_stmt ||' AND pd.vendor_site_id  = ' || p_vendor_site_id ;
127   END IF;
128   IF ( p_po_release_id is NOT NULL) then
129       sql_stmt := sql_stmt ||' AND NVL(pd.po_release_id, 1) = '
130                            || p_po_release_id ;
131   END IF;
132 
133   -----------------------------------------------
134   debug_info := 'Step 4a. build l_vendor_list';
135   -----------------------------------------------
136 
137  -- open the cursor for the dynamic select stmt
138   OPEN vendor_cur for sql_stmt;
139 
140   FETCH vendor_cur
141   BULK COLLECT INTO l_vendor_id_list,
142                     l_vendor_num_list,
143                     l_vendor_name_list;
144 
145   CLOSE vendor_cur;
146 
147 -- Bug 5048503 : ends
148 
149 /* commented for Bug 5048503
150   OPEN vendor;
151   FETCH vendor
152   BULK COLLECT INTO l_vendor_id_list,
153                     l_vendor_num_list,
154                     l_vendor_name_list;
155   CLOSE vendor;
156 */
157   ---------------------------------------------------------
158   debug_info := 'Step 4b.Insert into ap_invoices_interface';
159   ----------------------------------------------------------
160   FORALL I IN 1 .. l_vendor_id_list.count
161          INSERT INTO ap_invoices_interface
162                    (org_id,
163                     invoice_id,
164                     source,
165                     vendor_id,
166                     vendor_num,
167                     vendor_name,
168                     group_id,
169                     created_by,
170                     creation_date,
171                     last_updated_by,
172                     last_update_date,
173                     last_update_login,
174                     request_id)
175           VALUES   (p_org_id,
176                     AP_INVOICES_INTERFACE_S.nextval,
177                     'PPA',
178                     l_vendor_id_list(i),
179                     l_vendor_num_list(i),
180                     l_vendor_name_list(i),
181                     p_group_id,
182                     p_po_user_id,
183                     SYSDATE,                   --creation_date
184                     FND_GLOBAL.user_id,        --last_updated_by
185                     SYSDATE,                   --last_update_date
186                     FND_GLOBAL.conc_login_id,  --last_update_login
187                     FND_GLOBAL.conc_request_id --request_id
188                    );
189 
190 
191   --------------------------------------------------
192   debug_info := 'Step 4c. Clear PL/SQL tables';
193   --------------------------------------------------
194   l_vendor_id_list.DELETE;
195   l_vendor_num_list.DELETE;
196   l_vendor_name_list.DELETE;
197 
198   IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
199    FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_RETRO_PRICING_PKG.INSERT_AP_INV_INTERFACE(-)');
200  END IF;
201 
202 
203 
204 EXCEPTION
205    --
206    WHEN OTHERS THEN
207       IF (SQLCODE <> -20001 ) THEN
208         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
209         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
210         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',current_calling_sequence);
211         FND_MESSAGE.SET_TOKEN('DEBUG_INFO', debug_info );
212         FND_MESSAGE.SET_TOKEN('PARAMETERS','P_vendor_id: '||TO_CHAR(P_vendor_id)
213                                         ||',P_vendor_site_id: '||TO_CHAR(P_vendor_site_id)
214                                         ||',p_po_header_id: '||TO_CHAR(p_po_header_id)
215                                         ||',P_vendor_site_id: '||TO_CHAR(P_vendor_site_id)
216                                         ||',p_po_release_id: '||TO_CHAR(p_po_release_id)
217                                         ||',p_po_user_id: '||TO_CHAR(p_po_user_id)
218                                         ||',p_org_id: '||TO_CHAR(p_org_id));
219    END IF;
220 
221    debug_info := 'In Others Exception';
222    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
223      FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,debug_info);
224    END IF;
225    --
226 -- Bug 5048503. changed vendor to vendor_cur
227    IF ( vendor_cur%ISOPEN ) THEN
228         CLOSE vendor_cur;
229    END IF;
230     --
231    APP_EXCEPTION.RAISE_EXCEPTION;
232    --
233 END insert_ap_inv_interface;
234 
235 
236 
237 /*=============================================================================
238  |  PROCEDURE - insert_ap_inv_lines_interface()
239  |
240  |  DESCRIPTION
241  |      Private procedure called from Create_Instructions. Program identifies
242  |      for header record, a unique line record for each retropriced PO shipment.
243  |
244  |  PARAMETERS
245  |      p_group_id       - Unique group_id generated in Create_Instructions
246  |      p_org_id         - Org Id of the PO User
247  |      p_po_user_id     - PO's User Id
248  |      p_vendor_id      - Vendor Id
249  |      p_vendor_site_id - Vendor Site Id
250  |      p_po_header_id   - Valid PO's Header Id
251  |      p_po_release_id  - Valid PO Release Id
252  |      P_calling_sequence -  Calling sequence
253  |
254  |  MODIFICATION HISTORY
255  |  Date         Author             Description of Change
256  |  29-JUL-2003  dgulraja           Creation
257  |
258  *============================================================================*/
259 
260 
261 PROCEDURE insert_ap_inv_lines_interface (
262            p_group_id         IN  VARCHAR2,
263            p_org_id           IN  NUMBER,
264            p_po_user_id       IN  NUMBER,
265            p_vendor_id        IN  NUMBER,
266            p_vendor_site_id   IN  NUMBER,
267            p_po_header_id     IN  NUMBER,
268            p_po_release_id    IN  NUMBER,
269            p_calling_sequence IN  VARCHAR2) IS
270 
271 l_po_line_loc_id_list       id_list_type;
272 l_po_header_id_list         id_list_type;
273 l_po_line_id_list           id_list_type;
274 l_po_release_id_list        id_list_type;
275 l_invoice_id_list           id_list_type;
276 l_unit_price_list           id_list_type;
277 l_po_number_list            po_number_list_type;
278 l_po_line_number_list       id_list_type;
279 l_release_num_list          id_list_type;
280 l_po_shipment_num_list      id_list_type;
281 
282 
283 current_calling_sequence    VARCHAR2(2000);
284 debug_info                  VARCHAR2(2000);
285 
286 CURSOR shipment IS
287 SELECT DISTINCT pd.line_location_id,
288        pll.shipment_num,
289        pd.po_header_id,
290        ph.segment1,
291        pd.po_line_id,
292        pl.line_num,
293        pd.po_release_id,
294        pr.release_num,
295        pd.price_override,
296        aii.invoice_id
297   FROM po_ap_retroactive_dist_v pd,
298        po_headers_all ph,
299        po_releases_all pr,
300        po_lines_all pl,
301        po_line_locations_all pll,
302        ap_invoices_interface aii
303  WHERE mo_global.check_access(pd.org_id) = 'Y'
304    AND pd.po_header_id = ph.po_header_id
305    AND pd.po_release_id =  pr.po_release_id(+)
306    AND pd.po_line_id    =  pl.po_line_id
307    AND pd.line_location_id = pll.line_location_id
308    AND pd.invoice_adjustment_flag = 'R'
309    AND pd.org_id         = aii.org_id
310    AND aii.vendor_id     = pd.vendor_id
311    AND aii.source        = 'PPA'
312    AND aii.group_id      = p_group_id
313    AND aii.org_id        = p_org_id
314    AND pd.vendor_id      = DECODE(p_vendor_id, NULL,
315                                   pd.vendor_id, p_vendor_id)
316    AND pd.po_header_id   = DECODE(p_po_header_id, NULL,
317                                   pd.po_header_id, p_po_header_id)
318   AND pd.vendor_site_id = DECODE(p_vendor_site_id, NULL,
319                                  pd.vendor_site_id, p_vendor_site_id)
320   AND NVL(pd.po_release_id, 1)  = DECODE(p_po_release_id, NULL,
321                                          NVL(pd.po_release_id,1),
322                                              p_po_release_id);
323 
324    num BINARY_INTEGER := 1;
325    l_api_name CONSTANT VARCHAR2(200) := 'insert_ap_inv_lines_interface';
326   --
327 BEGIN
328 
329 
330   current_calling_sequence := 'insert_ap_inv_lines_interface<-'||P_calling_sequence;
331 
332    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
333       FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,
334       	'AP_RETRO_PRICING_PKG.INSERT_AP_INV_LINES_INTERFACE(+)');
335    END IF;
336   -----------------------------------------------
337   debug_info := 'Step 5a. build shipment list';
338   -----------------------------------------------
339 
340   --bugfix:4681253
341   FOR num IN 1..1000 LOOP
342 
343    l_po_line_loc_id_list(num) := NULL;
344    l_po_shipment_num_list(num) := NULL;
345    l_po_header_id_list(num) := NULL;
346    l_po_number_list(num) := NULL;
347    l_po_line_id_list(num) := NULL;
348    l_po_line_number_list(num) := NULL;
349    l_release_num_list(num) := NULL;
350    l_unit_price_list(num) := NULL;
351    l_invoice_id_list(num) := NULL;
352 
353   END LOOP;
354 
355   OPEN shipment;
356   FETCH shipment
357   BULK COLLECT INTO l_po_line_loc_id_list,
358                     l_po_shipment_num_list,
359                     l_po_header_id_list,
360                     l_po_number_list,
361                     l_po_line_id_list,
362                     l_po_line_number_list,
363 		    --Commented out until bug 4484058 is resolved.
364                     l_po_release_id_list,
365                     l_release_num_list,
366                     l_unit_price_list,
367                     l_invoice_id_list;
368   CLOSE shipment;
369 
370   ------------------------------------------------------
371   debug_info := 'Step 5b.Insert into ap_invoice_lines_interface';
372   -------------------------------------------------------
373   --Bugfix:4681253, added the IF condition and modified the FORALL from
374   --'1..count' to 'first..last'.
375   IF (l_po_line_loc_id_list.COUNT > 0) THEN
376 
377      FORALL i IN nvl(l_po_line_loc_id_list.first,0) .. l_po_line_loc_id_list.last
378        INSERT INTO  ap_invoice_lines_interface
379                     (invoice_id,
380                     invoice_line_id,
381                     po_header_id,
382                     po_number,
383                     po_line_id,
384                     po_line_number,
385                  --   po_release_id,
386                    -- release_num,
387                     po_line_location_id,
388                     po_shipment_num,
389                     unit_price,
390                     created_by,
391                     creation_date,
392                     last_updated_by,
393                     last_update_date,
394                     last_update_login)
395            VALUES  (l_invoice_id_list(i),
396                     AP_INVOICE_LINES_INTERFACE_S.nextval,
397                     l_po_header_id_list(i),
398                     l_po_number_list(i),
399                     l_po_line_id_list(i),
400                     l_po_line_number_list(i),
401                     --l_po_release_id_list(i),
402                     --l_release_num_list(i),
403                     l_po_line_loc_id_list(i),
404                     l_po_shipment_num_list(i),
405                     l_unit_price_list(i),
406                     p_po_user_id,
407                     SYSDATE,                   --creation_date
408                     FND_GLOBAL.user_id,        --last_updated_by
409                     SYSDATE,                   --last_update_date
410                     FND_GLOBAL.conc_login_id   --last_update_login
411                     );
412 
413 		    --Introduced below UPDATE for bug#9573078
414 		    -- and commented in CREATE_INSTRUCTIONS procedure
415 		    -- at step3
416 
417 		    FORALL i in 1..l_po_line_loc_id_list.COUNT
418                     UPDATE PO_DISTRIBUTIONS_ALL
419                     SET    invoice_adjustment_flag = 'S'
420                     WHERE  line_location_id = l_po_line_loc_id_list(i);
421 
422    END IF; /* l_po_line_loc_id_list.count > 0 */
423 
424 
425   --------------------------------------------------
426   debug_info := 'Step 5c. Clear PL/SQL tables';
427   --------------------------------------------------
428 
429   l_po_line_loc_id_list.DELETE;
430   l_po_header_id_list.DELETE;
431   l_po_line_id_list.DELETE;
432   l_po_release_id_list.DELETE;
433   l_invoice_id_list.DELETE;
434   l_unit_price_list.DELETE;
435   l_po_number_list.DELETE;
436   l_po_line_number_list.DELETE;
437   l_release_num_list.DELETE;
438   l_po_shipment_num_list.DELETE;
439 
440   IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
441      FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_RETRO_PRICING_PKG.INSERT_AP_INV_LINES_INTERFACE(-)');
442   END IF;
443 
444 
445 EXCEPTION
446 
447   WHEN OTHERS THEN
448 
449    IF (SQLCODE <> -20001 ) THEN
450         FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
451         FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
452         FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',current_calling_sequence);
453         FND_MESSAGE.SET_TOKEN('DEBUG_INFO', debug_info );
454         FND_MESSAGE.SET_TOKEN('PARAMETERS','P_vendor_id: '||TO_CHAR(P_vendor_id)
455                                         ||',P_vendor_site_id: '||TO_CHAR(P_vendor_site_id)
456                                         ||',p_po_header_id: '||TO_CHAR(p_po_header_id)
457                                         ||',P_vendor_site_id: '||TO_CHAR(P_vendor_site_id)
458                                         ||',p_po_release_id: '||TO_CHAR(p_po_release_id)
459                                         ||',p_po_user_id: '||TO_CHAR(p_po_user_id)
460                                         ||',p_org_id: '||TO_CHAR(p_org_id));
461     END IF;
462 
463     debug_info := 'In Others Exception';
464     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
465      FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,debug_info);
466     END IF;
467     --
468     IF ( shipment%ISOPEN ) THEN
469         CLOSE shipment;
470     END IF;
471     --
472     APP_EXCEPTION.RAISE_EXCEPTION;
473 
474 END insert_ap_inv_lines_interface;
475 
476 
477 /*=============================================================================
478  |  FUNCTION - Create_Instructions()
479  |
480  |  DESCRIPTION
481  |      Main Public procedure for the CADIP called from before report trigger
482  |      of APXCADIP. The parameters to the program can limit the process to a
483  |      single supplier, site, PO, or release.This program overloads the Invoice
484  |      Interface with Instructions. CADIP then initiates the Payables Open
485  |      Interface Import program for the instruction records in the interface
486  |      using the GROUP_ID(Gateway Batch) as a program parameter. If the
487  |      instructions are rejected then CADIP can be resubmitted. Open Interface
488  |      Import on resubmission runs for all Instruction rejections(GROUP_ID
489  |      is NULL).
490  |
491  |  PARAMETERS
492  |      p_org_id           - Org Id of the PO User
493  |      p_po_user_id       - PO's User Id
494  |      p_vendor_id        - Vendor Id: Concurrent program parameter
495  |      p_vendor_site_id   - Vendor Site Id: Concurrent program parameter
496  |      p_po_header_id     - Valid PO's Header Id: Concurrent program parameter
497  |      p_po_release_id    - Valid PO Release Id: Concurrent program parameter
498  |      P_calling_sequence - Calling sequence
499  |
500  |  MODIFICATION HISTORY
501  |  Date         Author             Description of Change
502  |  29-JUL-2003  dgulraja           Creation
503  |
504  *============================================================================*/
505 FUNCTION Create_Instructions (
506            p_vendor_id              IN            NUMBER,
507            p_vendor_site_id         IN            NUMBER,
508            p_po_header_id           IN            NUMBER,
509            p_po_release_id          IN            NUMBER,
510            p_po_user_id             IN            NUMBER,
511            p_resubmit_flag          IN            VARCHAR2,
512            errbuf                      OUT NOCOPY VARCHAR2,
513            retcode                     OUT NOCOPY NUMBER,
514            p_import_conc_request_id    OUT NOCOPY NUMBER,
515            p_calling_sequence       IN            VARCHAR2)
516 RETURN BOOLEAN IS
517 
518 l_org_id                     NUMBER;
519 l_ou_count                   NUMBER;
520 l_ou_name                    VARCHAR2(240);
521 
522 Request_Submission_Failure   EXCEPTION;
523 Allow_paid_Invoice_Adjust    EXCEPTION;
524 current_calling_sequence     VARCHAR2(2000);
525 debug_info                   VARCHAR2(1000);
526 
527 l_allow_paid_invoice_adjust  VARCHAR2(1);
528 l_group_id                   AP_INVOICES_INTERFACE.group_id%TYPE;
529 l_po_line_loc_id_list        id_list_type;
530 l_batch_id                   NUMBER;
531 l_batch_num                  NUMBER;
532 l_batch_control_flag         VARCHAR2(1) := 'N';
533 l_request_id                 NUMBER;
534 l_msg                        VARCHAR2(2000);
535 l_batch_name		     VARCHAR2(200);
536 
537 l_api_name CONSTANT VARCHAR2(200) := 'CREATE_INSTRUCTIONS';
538 
539 l_org_id_list            id_list_type;
540 
541 CURSOR orgs  IS
542 SELECT DISTINCT pd.org_id
543   FROM po_ap_retroactive_dist_v pd,
544        po_vendors pv
545  WHERE mo_global.check_access(pd.org_id) = 'Y'
546    AND pd.vendor_id = pv.vendor_id
547    AND pd.invoice_adjustment_flag = 'R'
548    AND pd.vendor_id      = DECODE(p_vendor_id, NULL,
549                                   pd.vendor_id, p_vendor_id)
550    AND pd.po_header_id   = DECODE(p_po_header_id, NULL,
551                                   pd.po_header_id, p_po_header_id)
552    AND pd.vendor_site_id = DECODE(p_vendor_site_id, NULL,
553                                   pd.vendor_site_id, p_vendor_site_id)
554    AND NVL(pd.po_release_id, 1)  = DECODE(p_po_release_id, NULL,
555                                           NVL(pd.po_release_id,1),
556                                               p_po_release_id);
557  l_use_batch_controls VARCHAR2(30) := NULL; --added for bug#6926296
558 BEGIN
559   -- Update the calling sequence
560   --
561   current_calling_sequence := 'CADIP<-'||P_calling_sequence ;
562 
563    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
564      FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_RETRO_PRICING_PKG.CREATE_INSTRUCTIONS(+)');
565    END IF;
566 
567    -----------------------------------------------------------------
568    debug_info := 'Step 1. Generate a Group Id(Invoice Gateway Batch';
569    -----------------------------------------------------------------
570 	-- Although we are using the  ap_batches_s to generate
571 	-- the Invoice_gateway_batch name, it has no relationship
572 	-- with the Invoice Batch_name(used if the batch control options
573 	IF p_resubmit_flag <> 'Y' THEN
574 	    SELECT  ap_batches_s.nextval
575 	      INTO  l_batch_id
576 	      FROM  sys.dual;
577 	END IF;
578 	--Gateway Batch
579 	l_group_id := 'PPA' || ':' || to_char(l_batch_id);
580 
581 	 --Commenting the below code for bug#6926296
582 	--Bugfix:4681253
583 	/*select decode(nvl(fpov1.profile_option_value,'N'),
584 	'N', lc.displayed_field)
585 	INTO l_batch_name
586 	FROM fnd_profile_option_values fpov1,
587 	    fnd_profile_options fpo1,
588 	    ap_lookup_codes lc
589 	WHERE fpov1.profile_option_id = fpo1.profile_option_id
590 	AND fpo1.profile_option_name ='AP_USE_INV_BATCH_CONTROLS'
591 	AND fpov1.level_id = 10004
592 	AND lc.lookup_type ='NLS REPORT PARAMETER'
593 	and lc.lookup_code = 'NA'
594 	AND rownum = 1;*/
595   --End of commenting code for bug#6926296
596 
597   --Added the below code for bug#6926296
598    l_use_batch_controls := fnd_profile.value('AP_USE_INV_BATCH_CONTROLS');
599 	if nvl(l_use_batch_controls,'N') = 'N' then
600 	  select lc.displayed_field
601 	    into l_batch_name
602 	   from ap_lookup_codes lc
603 	  where lc.lookup_type = 'NLS REPORT PARAMETER'
604 	    and lc.lookup_code = 'NA';
605 	end if;
606    --End of code addition for bug#6926296
607 
608   -----------------------------------------------------------
609   debug_info := 'Step 2. Get the distinct Org Ids.';
610   -----------------------------------------------------------
611    OPEN orgs;
612    FETCH orgs
613    BULK COLLECT INTO l_org_id_list;
614    CLOSE orgs;
615 
616     FOR I IN 1 .. l_org_id_list.count
617     LOOP
618 
619         l_org_id := l_org_id_list(i);
620         -----------------------------------------------------------------
621         debug_info := 'Step 2a. Check Allow Adjustments to paid Invoices'||
622                       to_char(l_org_id) ;
623         -----------------------------------------------------------------
624 		SELECT NVL(ALLOW_PAID_INVOICE_ADJUST, 'N')
625 		INTO   l_allow_paid_invoice_adjust
626 		FROM   ap_system_parameters_all
627 		WHERE  org_id = l_org_id_list(i);
628 
629 		IF  l_allow_paid_invoice_adjust = 'N' THEN
630 		  FND_MESSAGE.SET_NAME('SQLAP', 'ALLOW_PAID_INVOICE_ADJUST');
631 		  RAISE Allow_paid_Invoice_Adjust;
632 		END IF;
633 
634 
635 
636 		-----------------------------------------------------------------
637 		debug_info := 'Step 2b.  Populate AP_INVOICES_INTERFACE';
638 		-----------------------------------------------------------------
639 		   IF p_resubmit_flag <> 'Y' THEN
640 
641 		     AP_RETRO_PRICING_PKG.insert_ap_inv_interface (
642 		          l_group_id,
643 		          l_org_id,
644 		          p_po_user_id,
645 		          p_vendor_id,
646 		          p_vendor_site_id,
647 		          p_po_header_id,
648 		          p_po_release_id,
649 		          current_calling_sequence);
650 		   END IF;
651 
652 		---------------------------------------------------------------
653 		debug_info := 'Step 2c.  Populate AP_INVOICE_LINES_INTERFACE';
654 	        ----------------------------------------------------------------
655 		   IF p_resubmit_flag <> 'Y' THEN
656 
657 		     AP_RETRO_PRICING_PKG.insert_ap_inv_lines_interface(
658 		        l_group_id,
659 		        l_org_id,
660 		        p_po_user_id,
661 		        p_vendor_id,       -- IN
662 		        p_vendor_site_id,  -- IN
663 		        p_po_header_id,    -- IN
664 		        p_po_release_id,   -- IN
665 		        current_calling_sequence);
666 
667 		   END IF;
668 
669                  --Commented below code for bug#9573078
670 		   --and introduced in insert_ap_inv_lines_interface
671 		   --procedure.
672 
673               /*  ---------------------------------------------------------------
674                 debug_info := 'Step 3.  Update the PO View';
675                 ---------------------------------------------------------------
676                 IF p_resubmit_flag <> 'Y' THEN
677 
678                   FORALL i in 1..l_po_line_loc_id_list.COUNT
679 
680                     UPDATE PO_AP_RETROACTIVE_DIST_V
681                     SET    invoice_adjustment_flag = 'S'
682                     WHERE  line_location_id = l_po_line_loc_id_list(i);
683 
684                 END IF; */
685 
686   END LOOP;
687 
688   -----------------------------------------------------------
689   debug_info := 'Step 4.  Submit Invoice Import';
690   -----------------------------------------------------------
691    IF p_resubmit_flag = 'Y' THEN
692        l_group_id := NULL;
693    END IF;
694 
695 
696  l_request_id := FND_REQUEST.SUBMIT_REQUEST(
697                   'SQLAP',
698                   'APXIIMPT',
699                   '', '', FALSE,
700                   '',
701                   'PPA',
702                   l_group_id,
703                   l_batch_name,
704                   '','','','','','','',
705                   chr(0),'', '', '', '', '', '', '', '', '',
706                   '', '', '', '', '', '', '', '', '', '',
707                   '', '', '', '', '', '', '', '', '', '',
708                   '', '', '', '', '', '', '', '', '', '',
709                   '', '', '', '', '', '', '', '', '', '',
710                   '', '', '', '', '', '', '', '', '', '',
711                   '', '', '', '', '', '', '', '', '', '',
712                   '', '', '', '', '', '', '', '', '', '',
713                   '', '', '', '', '', '', '', '', '');
714 
715     IF l_request_id <> 0 THEN
716        p_import_conc_request_id := l_request_id;
717        commit;
718     ELSE
719         RAISE Request_Submission_Failure;
720     END IF;
721    --
722 
723     IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
724       FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_RETRO_PRICING_PKG.CREATE_INSTRUCTIONS(-)');
725     END IF;
726 
727     RETURN(TRUE);
728     --
729 EXCEPTION
730     WHEN  Allow_paid_Invoice_Adjust  THEN
731 
732       FND_FILE.PUT_LINE(FND_FILE.LOG, fnd_message.GET);
733       debug_info := 'In Allow_paid_Invoice_Adjust Exception';
734       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
735          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,debug_info);
736       END IF;
737 
738 
739       RETURN TRUE;
740 
741     WHEN request_submission_failure THEN
742        l_msg := FND_MESSAGE.GET;
743        FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
744        FND_MESSAGE.SET_TOKEN('ERROR',l_msg);
745        FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',current_calling_sequence);
746        FND_MESSAGE.SET_TOKEN('DEBUG_INFO', debug_info );
747        FND_MESSAGE.SET_TOKEN('PARAMETERS','P_vendor_id: '||TO_CHAR(P_vendor_id)
748                                         ||',P_vendor_site_id: '||TO_CHAR(P_vendor_site_id)
749                                         ||',p_po_header_id: '||TO_CHAR(p_po_header_id)
750                                         ||',P_vendor_site_id: '||TO_CHAR(P_vendor_site_id)
751                                         ||',p_po_release_id: '||TO_CHAR(p_po_release_id)
752                                         ||',p_po_user_id: '||TO_CHAR(p_po_user_id)
753                                         ||',p_org_id: '||TO_CHAR(l_org_id)
754                                         ||',p_resubmit_flag: '||p_resubmit_flag);
755 
756       debug_info := 'In request_submission_failure Exception';
757       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
758         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,debug_info);
759       END IF;
760 
761       RETURN FALSE;
762 
763   WHEN OTHERS THEN
764       IF (SQLCODE <> -20001 ) THEN
765 	        FND_MESSAGE.SET_NAME('SQLAP','AP_DEBUG');
766 	        FND_MESSAGE.SET_TOKEN('ERROR',SQLERRM);
767 	        FND_MESSAGE.SET_TOKEN('CALLING_SEQUENCE',current_calling_sequence);
768 	        FND_MESSAGE.SET_TOKEN('DEBUG_INFO', debug_info );
769 	        FND_MESSAGE.SET_TOKEN('PARAMETERS','P_vendor_id: '||TO_CHAR(P_vendor_id)
770 	                                        ||',P_vendor_site_id: '||TO_CHAR(P_vendor_site_id)
771 	                                        ||',p_po_header_id: '||TO_CHAR(p_po_header_id)
772 	                                        ||',P_vendor_site_id: '||TO_CHAR(P_vendor_site_id)
773 	                                        ||',p_po_release_id: '||TO_CHAR(p_po_release_id)
774 	                                        ||',p_po_user_id: '||TO_CHAR(p_po_user_id)
775 	                                        ||',p_org_id: '||TO_CHAR(l_org_id)
776 	                                        ||',p_resubmit_flag: '||p_resubmit_flag);
777     END IF;
778 
779     debug_info := 'In Others Exception';
780     IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
781        FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,debug_info);
782     END IF;
783 
784     APP_EXCEPTION.RAISE_EXCEPTION;
785     RETURN FALSE;
786 	    --
787 END Create_Instructions;
788 
789 
790 /*===============================================================================
791  |  FUNCTION - Reverse_Existing_Ppa_Dists()
792  |
793  |  DESCRIPTION
794  |        This function is called from Reverse_Existing_Ppa for every Po Price
795  |  Adjustment line on the active PPA. It effectively reverses all the
796  |  distributions on a Po Price Adjustment Line for a PPA.
797  |
798  |  PARAMETERS
799  |      p_instruction_id
800  |      p_ppa_lines_rec
801  |      p_existing_ppa_lines_rec IN
802  |      P_calling_sequence - Calling sequence
803  |
804  |  MODIFICATION HISTORY
805  |  Date         Author             Description of Change
806  |  29-JUL-2003  dgulraja           Creation
807  |
808  *==============================================================================*/
809 FUNCTION Reverse_Existing_Ppa_Dists(
810               p_instruction_id          IN NUMBER,
811               p_ppa_lines_rec           IN AP_RETRO_PRICING_PKG.invoice_lines_rec_type,
812               p_existing_ppa_lines_rec  IN AP_RETRO_PRICING_PKG.invoice_lines_rec_type,
813               P_calling_sequence        IN VARCHAR2)
814 RETURN BOOLEAN IS
815 
816 CURSOR Existing_ppa_invoice_dists   IS
817 SELECT  accounting_date,
818         accrual_posted_flag,
819         amount,
820         asset_book_type_code,
821         asset_category_id,
822         assets_addition_flag,
823         assets_tracking_flag,
824         attribute_category,
825         attribute1,
826         attribute10,
827         attribute11,
828         attribute12,
829         attribute13,
830         attribute14,
831         attribute15,
832         attribute2,
833         attribute3,
834         attribute4,
835         attribute5,
836         attribute6,
837         attribute7,
838         attribute8,
839         attribute9,
840         award_id,
841         awt_flag,
842         awt_group_id,
843         awt_tax_rate_id,
844         base_amount,
845         batch_id,
846         cancellation_flag,
847         cash_posted_flag,
848         corrected_invoice_dist_id,
849         corrected_quantity,
850         country_of_supply,
851         created_by,
852         description,
853         dist_code_combination_id,
854         dist_match_type,
855         distribution_class,
856         distribution_line_number,
857         encumbered_flag,
858         expenditure_item_date,
859         expenditure_organization_id,
860         expenditure_type,
861         final_match_flag,
862         global_attribute_category,
863         global_attribute1,
864         global_attribute10,
865         global_attribute11,
866         global_attribute12,
867         global_attribute13,
868         global_attribute14,
869         global_attribute15,
870         global_attribute16,
871         global_attribute17,
872         global_attribute18,
873         global_attribute19,
874         global_attribute2,
875         global_attribute20,
876         global_attribute3,
877         global_attribute4,
878         global_attribute5,
879         global_attribute6,
880         global_attribute7,
881         global_attribute8,
882         global_attribute9,
883         income_tax_region,
884         inventory_transfer_status,
885         invoice_distribution_id,
886         invoice_id,
887         invoice_line_number,
888         line_type_lookup_code,
889         match_status_flag,
890         matched_uom_lookup_code,
891         merchant_document_number,
892         merchant_name,
893         merchant_reference,
894         merchant_tax_reg_number,
895         merchant_taxpayer_id,
896         org_id,
897         pa_addition_flag,
898         pa_quantity,
899         period_name,
900         po_distribution_id,
901         posted_flag,
902         project_id,
903         quantity_invoiced,
904         rcv_transaction_id,
905         related_id,
906         reversal_flag,
907         rounding_amt,
908         set_of_books_id,
909         task_id,
910         type_1099,
911         unit_price,
912         p_instruction_id,      --instruction_id,
913         NULL,                    --charge_applicable_to_dist_id
914         INTENDED_USE,
915         WITHHOLDING_TAX_CODE_ID,
916         PROJECT_ACCOUNTING_CONTEXT,
917         REQ_DISTRIBUTION_ID,
918         REFERENCE_1,
919         REFERENCE_2,
920         NULL,                   -- line_group_number
921         PA_CC_AR_INVOICE_ID,
922         PA_CC_AR_INVOICE_LINE_NUM,
923         PA_CC_PROCESSED_CODE,
924 	pay_awt_group_id,  --bug6817107
925 	--Bug#10416960
926         summary_tax_line_id,
927         detail_tax_dist_id
928    FROM ap_invoice_distributions_all
929   WHERE invoice_id          = p_existing_ppa_lines_rec.invoice_id
930     AND invoice_line_number = p_existing_ppa_lines_rec.line_number
931     AND  NVL(cancellation_flag, 'N' ) <> 'Y'
932     AND NVL(reversal_flag, 'N' ) <> 'Y';
933 
934 l_existing_ppa_dist_list   AP_RETRO_PRICING_PKG.invoice_dists_list_type;
935 l_ppa_invoice_dists_list   AP_RETRO_PRICING_PKG.invoice_dists_list_type;
936 
937 current_calling_sequence   VARCHAR2(1000);
938 debug_info                 VARCHAR2(1000);
939 l_api_name                 CONSTANT VARCHAR2(200) := 'Reverse_Existing_Ppa_Dists';
940 
941 
942 BEGIN
943 
944   current_calling_sequence := 'AP_RETRO_PRICING_PKG.Reverse_Existing_Ppa_Dists'
945                     ||P_Calling_Sequence;
946 
947    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
948      FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_RETRO_PRICING_PKG.Reverse_Existing_Ppa_Dists(+)');
949   END IF;
950 
951   ---------------------------------------------------------------------------
952   debug_info := 'Reverse_Existing_Ppa_Dists Step 1. Open cursor '
953                  ||'existing_ppa_invoice_dists';
954   ---------------------------------------------------------------------------
955   OPEN existing_ppa_invoice_dists;
956   FETCH existing_ppa_invoice_dists
957   BULK COLLECT INTO l_existing_ppa_dist_list;
958   CLOSE existing_ppa_invoice_dists;
959   --
960   ---------------------------------------------------------------------------
961   debug_info := 'Reverse_Existing_Ppa_Dists  Step 2.  Compute Reversal PPA Dist';
962   ---------------------------------------------------------------------------
963   FOR i IN 1..l_existing_ppa_dist_list.COUNT
964   LOOP
965       l_ppa_invoice_dists_list(i)   := l_existing_ppa_dist_list(i);
966       l_ppa_invoice_dists_list(i).invoice_id          :=  p_ppa_lines_rec.invoice_id;
967       l_ppa_invoice_dists_list(i).invoice_line_number :=  p_ppa_lines_rec.line_number;
968       --Bug#10416960
969       l_ppa_invoice_dists_list(i).invoice_distribution_id :=  AP_RETRO_PRICING_UTIL_PKG.get_invoice_distribution_id;
970      -- l_ppa_invoice_dists_list(i).invoice_distribution_id := Null;
971       l_ppa_invoice_dists_list(i).distribution_line_number := i;  -- Bug 5525506
972       --Bug#10416960
973       --l_ppa_invoice_dists_list(i).dist_match_type     := 'ADJUSTMENT_CORRECTION';
974       l_ppa_invoice_dists_list(i).distribution_class  := 'PERMANENT';
975       l_ppa_invoice_dists_list(i).accounting_date     := SYSDATE;
976       l_ppa_invoice_dists_list(i).period_name         := AP_INVOICES_PKG.get_period_name(SYSDATE);
977       l_ppa_invoice_dists_list(i).accrual_posted_flag := 'N';
978       l_ppa_invoice_dists_list(i).cash_posted_flag    := 'N';
979       l_ppa_invoice_dists_list(i).posted_flag         := 'N';
980       l_ppa_invoice_dists_list(i).amount              := (-1)*l_existing_ppa_dist_list(i).amount;
981       l_ppa_invoice_dists_list(i).unit_price          := (-1)*l_existing_ppa_dist_list(i).unit_price;
982       --For base currency invoices NULL*(-1) = NULL
983       l_ppa_invoice_dists_list(i).base_amount         := (-1)*l_existing_ppa_dist_list(i).base_amount;
984 
985       l_ppa_invoice_dists_list(i).match_status_flag   := NULL;
986       l_ppa_invoice_dists_list(i).encumbered_flag     := 'N';
987       l_ppa_invoice_dists_list(i).corrected_invoice_dist_id := l_existing_ppa_dist_list(i).invoice_distribution_id;
988       l_ppa_invoice_dists_list(i).corrected_quantity  :=  l_existing_ppa_dist_list(i).corrected_quantity;
989       l_ppa_invoice_dists_list(i).quantity_invoiced    := NULL;
990       l_ppa_invoice_dists_list(i).final_match_flag     := 'N';
991       l_ppa_invoice_dists_list(i).assets_addition_flag := 'U';
992 
993       IF l_ppa_invoice_dists_list(i).assets_tracking_flag = 'Y' THEN
994          l_ppa_invoice_dists_list(i).asset_book_type_code := p_ppa_lines_rec.asset_book_type_code;
995          l_ppa_invoice_dists_list(i).asset_category_id    := p_ppa_lines_rec.asset_category_id;
996       END IF;
997 
998       IF l_ppa_invoice_dists_list(i).project_id IS NOT NULL THEN
999         l_ppa_invoice_dists_list(i).pa_Addition_flag := 'N';
1000       ELSE
1001         l_ppa_invoice_dists_list(i).pa_Addition_flag := 'E';
1002       END IF;
1003       l_ppa_invoice_dists_list(i).inventory_transfer_status := 'N';
1004       l_ppa_invoice_dists_list(i).created_by := p_ppa_lines_rec.created_by;
1005       l_ppa_invoice_dists_list(i).INTENDED_USE := l_existing_ppa_dist_list(i).INTENDED_USE;
1006       l_ppa_invoice_dists_list(i).WITHHOLDING_TAX_CODE_ID := l_existing_ppa_dist_list(i).WITHHOLDING_TAX_CODE_ID;
1007       l_ppa_invoice_dists_list(i).PROJECT_ACCOUNTING_CONTEXT := l_existing_ppa_dist_list(i).PROJECT_ACCOUNTING_CONTEXT;
1008       l_ppa_invoice_dists_list(i).REQ_DISTRIBUTION_ID := l_existing_ppa_dist_list(i).REQ_DISTRIBUTION_ID;
1009       l_ppa_invoice_dists_list(i).REFERENCE_1 := l_existing_ppa_dist_list(i).REFERENCE_1;
1010       l_ppa_invoice_dists_list(i).REFERENCE_2 := l_existing_ppa_dist_list(i).REFERENCE_2;
1011       l_ppa_invoice_dists_list(i).LINE_GROUP_NUMBER := l_existing_ppa_dist_list(i).LINE_GROUP_NUMBER;
1012       l_ppa_invoice_dists_list(i).PA_CC_AR_INVOICE_ID := l_existing_ppa_dist_list(i).PA_CC_AR_INVOICE_ID;
1013       l_ppa_invoice_dists_list(i).PA_CC_AR_INVOICE_LINE_NUM := l_existing_ppa_dist_list(i).PA_CC_AR_INVOICE_LINE_NUM;
1014       l_ppa_invoice_dists_list(i).PA_CC_PROCESSED_CODE := l_existing_ppa_dist_list(i).PA_CC_PROCESSED_CODE;
1015 
1016 
1017       END LOOP;
1018 
1019       ------------------------------------------------------------------------
1020       debug_info := 'Reverse_Existing_Ppa_Dists Step 3. Insert PPA Reversal '
1021                      ||' Dists in the Global Temp Table';
1022       ------------------------------------------------------------------------
1023       FORALL i IN 1..l_ppa_invoice_dists_list.COUNT
1024                INSERT INTO ap_ppa_invoice_dists_gt values  l_ppa_invoice_dists_list(i);
1025 
1026        --Bug#10416960
1027       --Update related_id to avoid recalculation of base amounts
1028       BEGIN
1029       UPDATE ap_ppa_invoice_dists_gt  d1
1030         SET related_id = (Select invoice_distribution_id
1031 	                  FROM ap_ppa_invoice_dists_gt d2
1032 			  WHERE d2.invoice_id=d1.invoice_id
1033 			    and d2.invoice_line_number=d1.invoice_line_number
1034 			    and d2.line_type_lookup_code in ('RETROEXPENSE', 'RETROACCRUAL'))
1035       WHERE invoice_id = p_ppa_lines_rec.invoice_id
1036         AND invoice_line_number = p_ppa_lines_rec.line_number;
1037       EXCEPTION
1038       WHEN OTHERS THEN
1039          NULL;
1040       END;
1041 
1042       ----------------------------------------------------------------------
1043       debug_info := 'Reverse_Existing_Ppa_Dists Step 4. Clear PL/SQL tables';
1044       ----------------------------------------------------------------------
1045       l_existing_ppa_dist_list.DELETE;
1046       l_ppa_invoice_dists_list.DELETE;
1047 
1048     IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
1049         FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_RETRO_PRICING_PKG.Reverse_Existing_Ppa_Dists(-)');
1050   END IF;
1051   --
1052   RETURN(TRUE);
1053   --
1054 EXCEPTION
1055  WHEN OTHERS THEN
1056     IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
1057       AP_IMPORT_UTILITIES_PKG.Print(AP_IMPORT_INVOICES_PKG.g_debug_switch,
1058                                     debug_info);
1059       debug_info := 'In Others Exception';
1060       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
1061         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,debug_info);
1062       END IF;
1063     END IF;
1064     --
1065     IF (SQLCODE < 0) then
1066       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
1067         AP_IMPORT_UTILITIES_PKG.Print(AP_IMPORT_INVOICES_PKG.g_debug_switch,
1068                                       SQLERRM);
1069       END IF;
1070     END IF;
1071     --
1072     IF ( existing_ppa_invoice_dists%ISOPEN ) THEN
1073         CLOSE existing_ppa_invoice_dists;
1074     END IF;
1075     --
1076     RETURN(FALSE);
1077 
1078 END Reverse_Existing_Ppa_Dists;
1079 
1080 
1081 /*=============================================================================
1082  |  FUNCTION - Reverse_Existing_Ppa()
1083  |
1084  |  DESCRIPTION
1085  |        If the PPA already exists for a base match line then all the PO PRICE
1086  |  ADJUSTMENT lines of the active PPA will be reversed via call to this
1087  |  function. This function is called once for every new PPA Document
1088  |
1089  |  Note: There will be only one active PPA document(which itself has not been
1090  |        reversed by  another PPA document) per base matched INVOICE for a
1091  |        particular shipment.
1092  |
1093  |  PARAMETERS
1094  |      p_instruction_id
1095  |      p_ppa_invoice_rec
1096  |      p_instruction_lines_rec
1097  |      p_existing_ppa_inv_id
1098  |      P_calling_sequence - Calling sequence
1099  |
1100  |  MODIFICATION HISTORY
1101  |  Date         Author             Description of Change
1102  |  29-JUL-2003  dgulraja           Creation
1103  |
1104  *============================================================================*/
1105 FUNCTION Reverse_Existing_Ppa(
1106            p_instruction_id     IN NUMBER,
1107            p_ppa_invoice_rec      IN AP_RETRO_PRICING_PKG.invoice_rec_type,
1108            p_instruction_lines_rec      IN AP_RETRO_PRICING_PKG.instruction_lines_rec_type,
1109            p_existing_ppa_inv_id IN NUMBER,
1110            P_calling_sequence    IN VARCHAR2)
1111 RETURN BOOLEAN    IS
1112 CURSOR existing_ppa_lines IS
1113 SELECT invoice_id,
1114        line_number,
1115        line_type_lookup_code,
1116        requester_id,
1117        description,
1118        line_source,
1119        org_id,
1120        inventory_item_id,
1121        item_description,
1122        serial_number,
1123        manufacturer,
1124        model_number,
1125        generate_dists,
1126        match_type,
1127        default_dist_ccid,
1128        prorate_across_all_items,
1129        accounting_date,
1130        period_name,
1131        deferred_acctg_flag,
1132        set_of_books_id,
1133        amount,
1134        base_amount,
1135        rounding_amt,
1136        quantity_invoiced,
1137        unit_meas_lookup_code,
1138        unit_price,
1139        discarded_flag,
1140        cancelled_flag,
1141        income_tax_region,
1142        type_1099,
1143        corrected_inv_id,
1144        corrected_line_number,
1145        po_header_id,
1146        po_line_id,
1147        po_release_id,
1148        po_line_location_id,
1149        po_distribution_id,
1150        rcv_transaction_id,
1151        final_match_flag,
1152        assets_tracking_flag,
1153        asset_book_type_code,
1154        asset_category_id,
1155        project_id,
1156        task_id,
1157        expenditure_type,
1158        expenditure_item_date,
1159        expenditure_organization_id,
1160        award_id,
1161        awt_group_id,
1162        pay_awt_group_id,--bug6817107
1163        receipt_verified_flag,
1164        receipt_required_flag,
1165        receipt_missing_flag,
1166        justification,
1167        expense_group,
1168        start_expense_date,
1169        end_expense_date,
1170        receipt_currency_code,
1171        receipt_conversion_rate,
1172        receipt_currency_amount,
1173        daily_amount,
1174        web_parameter_id,
1175        adjustment_reason,
1176        merchant_document_number,
1177        merchant_name,
1178        merchant_reference,
1179        merchant_tax_reg_number,
1180        merchant_taxpayer_id,
1181        country_of_supply,
1182        credit_card_trx_id,
1183        company_prepaid_invoice_id,
1184        cc_reversal_flag,
1185        creation_date,
1186        created_by,
1187        attribute_category,
1188        attribute1,
1189        attribute2,
1190        attribute3,
1191        attribute4,
1192        attribute5,
1193        attribute6,
1194        attribute7,
1195        attribute8,
1196        attribute9,
1197        attribute10,
1198        attribute11,
1199        attribute12,
1200        attribute13,
1201        attribute14,
1202        attribute15,
1203        global_attribute_category,
1204        global_attribute1,
1205        global_attribute2,
1206        global_attribute3,
1207        global_attribute4,
1208        global_attribute5,
1209        global_attribute6,
1210        global_attribute7,
1211        global_attribute8,
1212        global_attribute9,
1213        global_attribute10,
1214        global_attribute11,
1215        global_attribute12,
1216        global_attribute13,
1217        global_attribute14,
1218        global_attribute15,
1219        global_attribute16,
1220        global_attribute17,
1221        global_attribute18,
1222        global_attribute19,
1223        global_attribute20,
1224        primary_intended_use,
1225        ship_to_location_id,
1226        product_type,
1227        product_category,
1228        product_fisc_classification,
1229        user_defined_fisc_class,
1230        trx_business_category,
1231        summary_tax_line_id,
1232        tax_regime_code,
1233        tax,
1234        tax_jurisdiction_code,
1235        tax_status_code,
1236        tax_rate_id,
1237        tax_rate_code,
1238        tax_rate,
1239        wfapproval_status,
1240        pa_quantity,
1241        p_instruction_id,   --instruction_id
1242        'PPA',              --adj_type
1243        cost_factor_id,      --cost_factor_id
1244        TAX_CLASSIFICATION_CODE,
1245        SOURCE_APPLICATION_ID         ,
1246        SOURCE_EVENT_CLASS_CODE         ,
1247        SOURCE_ENTITY_CODE         ,
1248        SOURCE_TRX_ID         ,
1249        SOURCE_LINE_ID         ,
1250        SOURCE_TRX_LEVEL_TYPE         ,
1251        PA_CC_AR_INVOICE_ID         ,
1252        PA_CC_AR_INVOICE_LINE_NUM         ,
1253        PA_CC_PROCESSED_CODE         ,
1254        REFERENCE_1         ,
1255        REFERENCE_2         ,
1256        DEF_ACCTG_START_DATE         ,
1257        DEF_ACCTG_END_DATE         ,
1258        DEF_ACCTG_NUMBER_OF_PERIODS         ,
1259        DEF_ACCTG_PERIOD_TYPE         ,
1260        REFERENCE_KEY5         ,
1261        PURCHASING_CATEGORY_ID         ,
1262        LINE_GROUP_NUMBER         ,
1263        WARRANTY_NUMBER         ,
1264        REFERENCE_KEY3         ,
1265        REFERENCE_KEY4         ,
1266        APPLICATION_ID         ,
1267        PRODUCT_TABLE         ,
1268        REFERENCE_KEY1         ,
1269        REFERENCE_KEY2         ,
1270        RCV_SHIPMENT_LINE_ID
1271   FROM ap_invoice_lines_all
1272  WHERE invoice_id = p_existing_ppa_inv_id
1273    AND line_source = 'PO PRICE ADJUSTMENT'
1274    AND match_type = 'PO_PRICE_ADJUSTMENT'
1275    AND line_type_lookup_code = 'RETROITEM'
1276    AND discarded_flag <> 'Y'
1277    AND cancelled_flag <> 'Y';
1278 
1279 l_ppa_lines_rec            AP_RETRO_PRICING_PKG.invoice_lines_rec_type;
1280 l_existing_ppa_lines_rec   AP_RETRO_PRICING_PKG.invoice_lines_rec_type;
1281 l_existing_ppa_lines_list  AP_RETRO_PRICING_PKG.invoice_lines_list_type;
1282 Ppa_Line_Reversal_failure  EXCEPTION;
1283 current_calling_sequence   VARCHAR2(1000);
1284 debug_info                 VARCHAR2(1000);
1285 
1286 BEGIN
1287    --
1288    current_calling_sequence := 'AP_RETRO_PRICING_PKG.Reverse_Existing_Ppa'
1289                   ||P_Calling_Sequence;
1290    ---------------------------------------------------------------------------
1291    debug_info := 'Reverse_Existing_Ppa Step 1. Open Existing PPA Lines';
1292    ---------------------------------------------------------------------------
1293    OPEN existing_ppa_lines;
1294    FETCH existing_ppa_lines
1295    BULK COLLECT INTO l_existing_ppa_lines_list;
1296    CLOSE existing_ppa_lines;
1297    --
1298    -- Create PPA Lines that are reversal of all existing
1299    -- PO Price Adjustment Lines of the PPA
1300    ---------------------------------------------------------------------------
1301    debug_info := 'Reverse_Existing_Ppa Step 2. Compute Reversal PPA Lines';
1302    ---------------------------------------------------------------------------
1303    FOR i IN 1..l_existing_ppa_lines_list.COUNT
1304    LOOP
1305       l_existing_ppa_lines_rec := l_existing_ppa_lines_list(i);
1306       --
1307       l_ppa_lines_rec      := l_existing_ppa_lines_rec;
1308       --
1309       l_ppa_lines_rec.invoice_id   := p_ppa_invoice_rec.invoice_id;
1310       l_ppa_lines_rec.line_number  := AP_RETRO_PRICING_UTIL_PKG.get_max_ppa_line_num(
1311                                            p_ppa_invoice_Rec.invoice_id) + 1;
1312       l_ppa_lines_rec.line_source  := 'ADJUSTMENT CORRECTION';
1313       l_ppa_lines_rec.line_type_lookup_code := 'RETROITEM';
1314       l_ppa_lines_rec.requester_id := NVL(p_instruction_lines_rec.requester_id,
1315                                        l_existing_ppa_lines_rec.requester_id);
1316       l_ppa_lines_rec.description  := NVL(p_instruction_lines_rec.description,
1317                                         l_existing_ppa_lines_rec.description);
1318       l_ppa_lines_rec.default_dist_ccid := NULL;
1319       l_ppa_lines_rec.generate_dists           := 'D';
1320       l_ppa_lines_rec.prorate_across_all_items := 'N';
1321 
1322       l_ppa_lines_rec.accounting_date := NVL(p_instruction_lines_rec.accounting_date,
1323                     AP_INVOICES_PKG.get_GL_date(p_ppa_invoice_rec.invoice_date));
1324       l_ppa_lines_rec.period_name     := AP_INVOICES_PKG.get_period_name(l_ppa_lines_rec.accounting_date);
1325       --
1326       l_ppa_lines_rec.deferred_Acctg_flag := 'N';
1327 
1328       l_ppa_lines_rec.amount := (-1)*l_existing_ppa_lines_rec.amount;
1329       l_ppa_lines_rec.quantity_invoiced  := l_existing_ppa_lines_rec.quantity_invoiced;
1330       l_ppa_lines_rec.unit_price  := (-1)*l_existing_ppa_lines_rec.unit_price;
1331       l_ppa_lines_rec.discarded_flag := 'N';
1332       l_ppa_lines_rec.cancelled_flag := 'N';
1333       l_ppa_lines_rec.corrected_inv_id := l_existing_ppa_lines_rec.invoice_id;
1334       l_ppa_lines_rec.corrected_line_number := l_existing_ppa_lines_rec.line_number;
1335       l_ppa_lines_rec.final_match_flag := 'N';
1336       l_ppa_lines_rec.award_id := NVL(p_instruction_lines_rec.award_id,
1337                                       l_existing_ppa_lines_rec.award_id);
1338       l_ppa_lines_rec.created_by := p_ppa_invoice_rec.created_by;
1339       l_ppa_lines_rec.instruction_id       := p_instruction_id;
1340       l_ppa_lines_rec.adj_type := 'PPA';   -- Bug 5525506
1341 
1342        l_ppa_lines_rec.TAX_CLASSIFICATION_CODE := l_existing_ppa_lines_rec.TAX_CLASSIFICATION_CODE;
1343        l_ppa_lines_rec.SOURCE_APPLICATION_ID := l_existing_ppa_lines_rec.SOURCE_APPLICATION_ID;
1344        l_ppa_lines_rec.SOURCE_EVENT_CLASS_CODE := l_existing_ppa_lines_rec.SOURCE_EVENT_CLASS_CODE;
1345        l_ppa_lines_rec.SOURCE_ENTITY_CODE := l_existing_ppa_lines_rec.SOURCE_ENTITY_CODE;
1346        l_ppa_lines_rec.SOURCE_TRX_ID := l_existing_ppa_lines_rec.SOURCE_TRX_ID;
1347        l_ppa_lines_rec.SOURCE_LINE_ID := l_existing_ppa_lines_rec.SOURCE_LINE_ID;
1348        l_ppa_lines_rec.SOURCE_TRX_LEVEL_TYPE := l_existing_ppa_lines_rec.SOURCE_TRX_LEVEL_TYPE;
1349        l_ppa_lines_rec.PA_CC_AR_INVOICE_ID := l_existing_ppa_lines_rec.PA_CC_AR_INVOICE_ID;
1350        l_ppa_lines_rec.PA_CC_AR_INVOICE_LINE_NUM := l_existing_ppa_lines_rec.PA_CC_AR_INVOICE_LINE_NUM;
1351        l_ppa_lines_rec.PA_CC_PROCESSED_CODE := l_existing_ppa_lines_rec.PA_CC_PROCESSED_CODE;
1352        l_ppa_lines_rec.REFERENCE_1 := l_existing_ppa_lines_rec.REFERENCE_1;
1353        l_ppa_lines_rec.REFERENCE_2 := l_existing_ppa_lines_rec.REFERENCE_2;
1354        l_ppa_lines_rec.DEF_ACCTG_START_DATE := l_existing_ppa_lines_rec.DEF_ACCTG_START_DATE;
1355        l_ppa_lines_rec.DEF_ACCTG_END_DATE := l_existing_ppa_lines_rec.DEF_ACCTG_END_DATE;
1356        l_ppa_lines_rec.DEF_ACCTG_NUMBER_OF_PERIODS := l_existing_ppa_lines_rec.DEF_ACCTG_NUMBER_OF_PERIODS;
1357        l_ppa_lines_rec.DEF_ACCTG_PERIOD_TYPE := l_existing_ppa_lines_rec.DEF_ACCTG_PERIOD_TYPE;
1358        l_ppa_lines_rec.REFERENCE_KEY5 := l_existing_ppa_lines_rec.REFERENCE_KEY5;
1359        l_ppa_lines_rec.PURCHASING_CATEGORY_ID := l_existing_ppa_lines_rec.PURCHASING_CATEGORY_ID;
1360        l_ppa_lines_rec.LINE_GROUP_NUMBER  := NULL;
1361        l_ppa_lines_rec.WARRANTY_NUMBER := l_existing_ppa_lines_rec.WARRANTY_NUMBER;
1362        l_ppa_lines_rec.REFERENCE_KEY3 := l_existing_ppa_lines_rec.REFERENCE_KEY3;
1363        l_ppa_lines_rec.REFERENCE_KEY4 := l_existing_ppa_lines_rec.REFERENCE_KEY4;
1364        l_ppa_lines_rec.APPLICATION_ID := l_existing_ppa_lines_rec.APPLICATION_ID;
1365        l_ppa_lines_rec.PRODUCT_TABLE := l_existing_ppa_lines_rec.PRODUCT_TABLE;
1366        l_ppa_lines_rec.REFERENCE_KEY1 := l_existing_ppa_lines_rec.REFERENCE_KEY1;
1367        l_ppa_lines_rec.REFERENCE_KEY2 := l_existing_ppa_lines_rec.REFERENCE_KEY2;
1368        l_ppa_lines_rec.RCV_SHIPMENT_LINE_ID := l_existing_ppa_lines_rec.RCV_SHIPMENT_LINE_ID;
1369       --
1370       ------------------------------------------------------------------------
1371       debug_info := 'Reverse_Existing_Ppa Step 3. Insert temp PPA Reversal '
1372                      ||'Line';
1373       ------------------------------------------------------------------------
1374       IF (AP_RETRO_PRICING_UTIL_PKG.Create_Line(
1375               l_ppa_lines_rec,
1376               current_calling_sequence) <> TRUE) THEN
1377          --
1378          IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
1379              AP_IMPORT_UTILITIES_PKG.Print(
1380              AP_IMPORT_INVOICES_PKG.g_debug_switch,
1381              'insert_rejections<- '||current_calling_sequence);
1382          END IF;
1383          --
1384          Raise Ppa_Line_Reversal_failure;
1385          --
1386       END IF;
1387 
1388       ----------------------------------------------------------------------
1389       debug_info := 'Reverse_Existing_Ppa Step 4. Compute PPA Dist Reversal';
1390       ----------------------------------------------------------------------
1391       IF (Reverse_Existing_Ppa_Dists(
1392                   p_instruction_id,
1393                   l_ppa_lines_rec,
1394                   l_existing_ppa_lines_rec,
1395                   current_calling_sequence) <> TRUE) THEN
1396          --
1397          IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
1398              AP_IMPORT_UTILITIES_PKG.Print(
1399              AP_IMPORT_INVOICES_PKG.g_debug_switch,
1400              'Reverse_Existing_Ppa_Dists<- '||current_calling_sequence);
1401          END IF;
1402          --
1403          Raise Ppa_Line_Reversal_failure;
1404          --
1405       END IF;
1406       --
1407    END LOOP;
1408    --
1409    ----------------------------------------------------------------------
1410    debug_info := 'Reverse_Existing_Ppa Step 5. Clear PL/SQL tables';
1411    ----------------------------------------------------------------------
1412    l_existing_ppa_lines_list.DELETE;
1413    --
1414    RETURN(TRUE);
1415    --
1416 EXCEPTION
1417  WHEN OTHERS THEN
1418     IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
1419       AP_IMPORT_UTILITIES_PKG.Print(AP_IMPORT_INVOICES_PKG.g_debug_switch,
1420                                     debug_info);
1421     END IF;
1422 
1423     IF (SQLCODE < 0) then
1424       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
1425         AP_IMPORT_UTILITIES_PKG.Print(AP_IMPORT_INVOICES_PKG.g_debug_switch,
1426                                       SQLERRM);
1427       END IF;
1428     END IF;
1429     --
1430     IF ( existing_ppa_lines%ISOPEN ) THEN
1431         CLOSE existing_ppa_lines;
1432     END IF;
1433     --
1434     RETURN(FALSE);
1435     --
1436 END Reverse_Existing_Ppa;
1437 
1438 
1439 /*=============================================================================
1440  |  FUNCTION - Create_Zero_Amt_Adj_Line()
1441  |
1442  |  DESCRIPTION
1443  |      This function is used to create zero-amount RetroItem or RetroTax lines
1444  |  on the original lines to reverse and redistribute all outstanding IPV and
1445  |  TIPV
1446  |
1447  |  PARAMETERS
1448  |      p_instruction_id
1449  |      p_created_by
1450  |      p_correcting
1451  |      p_lines_rec
1452  |      p_adj_lines_rec
1453  |      P_calling_sequence - Calling sequence
1454  |
1455  |  MODIFICATION HISTORY
1456  |  Date         Author             Description of Change
1457  |  29-JUL-2003  dgulraja           Creation
1458  |
1459  *============================================================================*/
1460 FUNCTION Create_Zero_Amt_Adj_Line(
1461            p_instruction_id  IN     NUMBER,
1462            p_created_by       IN     NUMBER,
1463            p_correcting       IN     VARCHAR2,
1464            p_lines_rec        IN     AP_RETRO_PRICING_PKG.invoice_lines_rec_type,
1465            p_adj_lines_rec       OUT NOCOPY AP_RETRO_PRICING_PKG.invoice_lines_rec_type,
1466            P_calling_sequence IN     VARCHAR2)
1467 RETURN BOOLEAN IS
1468 
1469 l_adj_lines_rec              AP_RETRO_PRICING_PKG.invoice_lines_rec_type;
1470 current_calling_sequence     VARCHAR2(1000);
1471 debug_info                   VARCHAR2(1000);
1472 Zero_Amt_Adj_Line_Failure    EXCEPTION;
1473 
1474 BEGIN
1475    --
1476    current_calling_sequence := 'AP_RETRO_PRICING_PKG.Create_Zero_Amt_Adj_Line'
1477                 ||P_Calling_Sequence;
1478    ---------------------------------------------------------------------------
1479    debug_info := 'Create_Zero_Amt_Adj_Line Step 1. Zero Amt Adj Line';
1480    ---------------------------------------------------------------------------
1481    l_adj_lines_rec             := p_lines_rec;
1482    l_adj_lines_rec.line_number := AP_INVOICES_UTILITY_PKG.get_max_inv_line_num(
1483                                      p_lines_rec.invoice_id) +
1484                                   AP_RETRO_PRICING_UTIL_PKG.get_max_ppa_line_num(
1485                                           p_lines_rec.invoice_id) + 1;
1486    IF p_correcting = 'IPV' THEN
1487        l_adj_lines_rec.line_type_lookup_code := 'RETROITEM' ;
1488 	   --'Redistribution of IPV due to Retroactive Pricing of Purchase Order'
1489 	   FND_MESSAGE.SET_NAME('SQLAP', 'AP_RETRO_IPV_REDIST');
1490 	   l_adj_lines_rec.description := FND_MESSAGE.GET;
1491    ELSE
1492       l_adj_lines_rec.line_type_lookup_code := 'RETROTAX' ;
1493 	   --'Redistribution of TIPV due to Retroactive Pricing of Purchase Order'
1494 	   FND_MESSAGE.SET_NAME('SQLAP', 'AP_RETRO_TIPV_REDIST');
1495 	   l_adj_lines_rec.description := FND_MESSAGE.GET;
1496    END IF;
1497    --
1498    l_adj_lines_rec.line_source           := 'ADJUSTMENT CORRECTION';
1499    l_adj_lines_rec.generate_dists        := 'D';
1500    l_adj_lines_rec.match_type            := 'ADJUSTMENT_CORRECTION';
1501    --
1502    l_adj_lines_rec.accounting_date := AP_INVOICES_PKG.get_GL_date(SYSDATE);
1503    l_adj_lines_rec.period_name     := AP_INVOICES_PKG.get_period_name(
1504                                           l_adj_lines_rec.accounting_date);
1505    --
1506    l_adj_lines_rec.amount                := 0;
1507    l_adj_lines_rec.base_amount           := 0;
1508    l_adj_lines_rec.rounding_amt          := 0;
1509    l_adj_lines_rec.quantity_invoiced     := NULL;
1510    l_adj_lines_rec.unit_meas_lookup_code := NULL;
1511    l_adj_lines_rec.unit_price            := NULL;
1512    l_adj_lines_rec.wfapproval_status     := 'NOT_REQUIRED';
1513    l_adj_lines_rec.corrected_inv_id      := p_lines_rec.invoice_id;
1514    l_adj_lines_rec.corrected_line_number := p_lines_rec.line_number;
1515    l_adj_lines_rec.pa_quantity           := NULL;
1516    l_adj_lines_rec.creation_date         := SYSDATE;
1517    l_adj_lines_rec.created_by            := p_created_by;
1518    l_adj_lines_rec.instruction_id       := p_instruction_id;
1519    l_adj_lines_rec.adj_type              := 'ADJ';
1520    --
1521    --Create reversal PPA Line for the existing PPA
1522    ---------------------------------------------------------------------------
1523    debug_info := 'Create_Zero_Amt_Adj_Line Step 2. Insert the Adj Line in '
1524                  ||'the Global Temp Table';
1525    ----------------------------------------------------------------------------
1526    IF (AP_RETRO_PRICING_UTIL_PKG.Create_Line(
1527             l_adj_lines_rec,
1528             current_calling_sequence) <> TRUE) THEN
1529      --
1530      IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
1531           AP_IMPORT_UTILITIES_PKG.Print(
1532             AP_IMPORT_INVOICES_PKG.g_debug_switch,
1533              'Create_Line<- '||current_calling_sequence);
1534      END IF;
1535      --
1536      Raise Zero_Amt_Adj_Line_Failure;
1537      --
1538    END IF;
1539    --
1540    p_adj_lines_rec := l_adj_lines_rec;
1541    --
1542    RETURN (TRUE);
1543    --
1544 EXCEPTION
1545  WHEN OTHERS THEN
1546     IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
1547       AP_IMPORT_UTILITIES_PKG.Print(AP_IMPORT_INVOICES_PKG.g_debug_switch,
1548                                     debug_info);
1549     END IF;
1550 
1551     IF (SQLCODE < 0) then
1552       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
1553         AP_IMPORT_UTILITIES_PKG.Print(AP_IMPORT_INVOICES_PKG.g_debug_switch,
1554                                       SQLERRM);
1555       END IF;
1556     END IF;
1557 
1558     RETURN(FALSE);
1559 
1560 END Create_Zero_Amt_Adj_Line;
1561 
1562 
1563 /*============================================================================
1564  |  FUNCTION - Create_Adjustment_Corrections()
1565  |
1566  |  DESCRIPTION
1567  |     This function is called in context of PC Line for the following type
1568  |     of lines --
1569  |     1. Zero Amount RetroItem line that adjusts the  the price correction
1570  |        line
1571  |     2. Zero Amount RetroTax Lines that adjust the Tax lines allocated to
1572  |        the PC Line
1573  |
1574  |     The function reverses the RetroExpense(NonRecoverable Tax) associated
1575  |     with the Zero Amount RetoItem(RetroTax) line on the Original Invoice.
1576  |     The function also reverses the associated ERV(If Any?) with the
1577  |     RetroItem and RetrTax Lines.
1578  |
1579  |  NOTE: ERV and Base Amount Calculations for the Adjustment Correction
1580  |        Lines are not during validation process. However for the Po Price
1581  |        Adjustment Lines on the PPA Doc the ERV and Base Amount
1582  |        Calculations are done during the Validation Process.
1583  |
1584  |  PARAMETERS
1585  |      p_base_currency_code
1586  |      p_instruction_id
1587  |      p_ppa_invoice_rec
1588  |      p_instruction_lines_rec
1589  |      p_lines_rec
1590  |      P_calling_sequence - Calling sequence
1591  |
1592  |  MODIFICATION HISTORY
1593  |  Date         Author             Description of Change
1594  |  29-JUL-2003  dgulraja           Creation
1595  |
1596  *==========================================================================*/
1597 FUNCTION Create_Adjustment_Corrections(
1598             p_ppa_invoice_rec       IN AP_RETRO_PRICING_PKG.invoice_rec_type,
1599             p_base_currency_code    IN VARCHAR2,
1600             p_adj_lines_rec         IN AP_RETRO_PRICING_PKG.invoice_lines_rec_type,
1601             P_calling_sequence      IN VARCHAR2)
1602 RETURN BOOLEAN IS
1603 
1604 
1605 CURSOR adj_corr_dists IS
1606 SELECT  accounting_date,
1607         accrual_posted_flag,
1608         amount,
1609         asset_book_type_code,
1610         asset_category_id,
1611         assets_addition_flag,
1612         assets_tracking_flag,
1613         attribute_category,
1614         attribute1,
1615         attribute10,
1616         attribute11,
1617         attribute12,
1618         attribute13,
1619         attribute14,
1620         attribute15,
1621         attribute2,
1622         attribute3,
1623         attribute4,
1624         attribute5,
1625         attribute6,
1626         attribute7,
1627         attribute8,
1628         attribute9,
1629         award_id,
1630         awt_flag,
1631         awt_group_id,
1632         awt_tax_rate_id,
1633         base_amount,
1634         batch_id,
1635         cancellation_flag,
1636         cash_posted_flag,
1637         corrected_invoice_dist_id,
1638         corrected_quantity,
1639         country_of_supply,
1640         created_by,
1641         description,
1642         dist_code_combination_id,
1643         dist_match_type,
1644         distribution_class,
1645         distribution_line_number,
1646         encumbered_flag,
1647         expenditure_item_date,
1648         expenditure_organization_id,
1649         expenditure_type,
1650         final_match_flag,
1651         global_attribute_category,
1652         global_attribute1,
1653         global_attribute10,
1654         global_attribute11,
1655         global_attribute12,
1656         global_attribute13,
1657         global_attribute14,
1658         global_attribute15,
1659         global_attribute16,
1660         global_attribute17,
1661         global_attribute18,
1662         global_attribute19,
1663         global_attribute2,
1664         global_attribute20,
1665         global_attribute3,
1666         global_attribute4,
1667         global_attribute5,
1668         global_attribute6,
1669         global_attribute7,
1670         global_attribute8,
1671         global_attribute9,
1672         income_tax_region,
1673         inventory_transfer_status,
1674         invoice_distribution_id,
1675         invoice_id,
1676         invoice_line_number,
1677         line_type_lookup_code,
1678         match_status_flag,
1679         matched_uom_lookup_code,
1680         merchant_document_number,
1681         merchant_name,
1682         merchant_reference,
1683         merchant_tax_reg_number,
1684         merchant_taxpayer_id,
1685         org_id,
1686         pa_addition_flag,
1687         pa_quantity,
1688         period_name,
1689         po_distribution_id,
1690         posted_flag,
1691         project_id,
1692         quantity_invoiced,
1693         rcv_transaction_id,
1694         related_id,
1695         reversal_flag,
1696         rounding_amt,
1697         set_of_books_id,
1698         task_id,
1699         type_1099,
1700         unit_price,
1701         instruction_id,          --instruction_id
1702 	 --Bug#10416960
1703         charge_applicable_to_dist_id, --Null
1704         INTENDED_USE,
1705         WITHHOLDING_TAX_CODE_ID,
1706         PROJECT_ACCOUNTING_CONTEXT,
1707         REQ_DISTRIBUTION_ID,
1708         REFERENCE_1,
1709         REFERENCE_2,
1710         NULL,                   -- line_group_number
1711         PA_CC_AR_INVOICE_ID,
1712         PA_CC_AR_INVOICE_LINE_NUM,
1713         PA_CC_PROCESSED_CODE,
1714 	pay_awt_group_id,  --bug6817107
1715 	/* Bug#10416960 */
1716         summary_tax_line_id,
1717         detail_tax_dist_id
1718 FROM   ap_ppa_invoice_dists_gt
1719 WHERE  invoice_id =  p_adj_lines_rec.invoice_id
1720 AND    invoice_line_number = p_adj_lines_rec.line_number
1721 --bug#10416960
1722 AND    line_type_lookup_code IN ('RETROEXPENSE', 'RETROACCRUAL', 'ERV','NONREC_TAX','REC_TAX','TRV','TERV')
1723 AND    invoice_distribution_id not in
1724       (select corrected_invoice_dist_id
1725        from ap_ppa_invoice_dists_gt gt2
1726        where gt2.dist_match_type='ADJUSTMENT_CORRECTION')
1727 ORDER BY invoice_distribution_id;
1728 
1729 
1730 l_ppa_lines_rec            AP_RETRO_PRICING_PKG.invoice_lines_rec_type;
1731 l_ppa_invoice_dists_list   AP_RETRO_PRICING_PKG.invoice_dists_list_type;
1732 l_adj_dists_list           AP_RETRO_PRICING_PKG.invoice_dists_list_type;
1733 
1734 i                              INTEGER;
1735 l_dist_total                   NUMBER;
1736 l_line_amount                  NUMBER  := 0;  --bug#9573078
1737 l_line_base_amount             NUMBER  := 0;  --bug#9573078
1738 
1739 current_calling_sequence       VARCHAR2(1000);
1740 debug_info                     VARCHAR2(1000);
1741 Adj_Correction_Lines_Failure EXCEPTION;
1742 
1743 BEGIN
1744    --
1745    current_calling_sequence :=
1746       'AP_RETRO_PRICING_PKG.Create_Adjustment_Corrections'||P_Calling_Sequence;
1747    --
1748    ---------------------------------------------------------------------------
1749     debug_info := 'Create_Adjustment_Corrections Step 1. Compute Adj Corr Line ';
1750    ---------------------------------------------------------------------------
1751    -- Compute PPA Line
1752    l_ppa_lines_rec := p_adj_lines_rec;
1753    l_ppa_lines_rec.invoice_id := p_ppa_invoice_rec.invoice_id;
1754    l_ppa_lines_rec.line_number  := AP_RETRO_PRICING_UTIL_PKG.get_max_ppa_line_num(
1755                                                     p_ppa_invoice_rec.invoice_id) + 1;
1756    l_ppa_lines_rec.corrected_inv_id := p_adj_lines_rec.invoice_id;
1757    l_ppa_lines_rec.corrected_line_number := p_adj_lines_rec.line_number;
1758    --
1759    ----------------------------------------------------------------------------
1760    debug_info := 'Create_Adjustment_Corrections Step 2. Insert the Adj Line in the'
1761                   ||' Global Temp Table';
1762    ----------------------------------------------------------------------------
1763 
1764    IF (AP_RETRO_PRICING_UTIL_PKG.Create_Line(
1765            l_ppa_lines_rec,
1766            current_calling_sequence) <> TRUE) THEN
1767       --
1768       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
1769           AP_IMPORT_UTILITIES_PKG.Print(
1770           AP_IMPORT_INVOICES_PKG.g_debug_switch,
1771           'insert_rejections<- '||current_calling_sequence);
1772       END IF;
1773       --
1774       Raise Adj_Correction_Lines_Failure;
1775       --
1776    END IF;
1777    ----------------------------------------------------------------------------
1778    debug_info := 'Create_Adjustment_Corrections Step 3. Open cursor adj_corr_dists';
1779    ----------------------------------------------------------------------------
1780    OPEN adj_corr_dists;
1781    FETCH adj_corr_dists
1782    BULK COLLECT INTO l_adj_dists_list;
1783    CLOSE adj_corr_dists;
1784    --
1785 
1786    ----------------------------------------------------------------------------
1787    debug_info := 'Create_Adjustment_Corrections Step 4. Compute PPA Adjustment Dists';
1788    ----------------------------------------------------------------------------
1789    FOR i IN 1..l_adj_dists_list.COUNT
1790    LOOP
1791        l_ppa_invoice_dists_list(i) := l_adj_dists_list(i);
1792        --
1793        l_ppa_invoice_dists_list(i).invoice_id := l_ppa_lines_rec.invoice_id;
1794        l_ppa_invoice_dists_list(i).invoice_line_number := l_ppa_lines_rec.line_number;
1795              --Bug#10416960
1796        l_ppa_invoice_dists_list(i).invoice_distribution_id := AP_RETRO_PRICING_UTIL_PKG.get_invoice_distribution_id;
1797        --l_ppa_invoice_dists_list(i).invoice_distribution_id := Null;
1798        l_ppa_invoice_dists_list(i).corrected_invoice_dist_id := l_adj_dists_list(i).invoice_distribution_id;
1799        --End Bug#10416960
1800        l_ppa_invoice_dists_list(i).distribution_line_number := i;
1801        l_ppa_invoice_dists_list(i).amount       := (-1)*l_adj_dists_list(i).amount;
1802        l_ppa_invoice_dists_list(i).base_amount  := (-1)*l_adj_dists_list(i).base_amount;
1803        l_ppa_invoice_dists_list(i).rounding_amt := (-1)*l_adj_dists_list(i).rounding_amt;
1804        l_ppa_invoice_dists_list(i).match_status_flag := NULL;  --bug#9573078
1805        l_ppa_invoice_dists_list(i).encumbered_flag   := 'N';   --bug#9573078
1806        --
1807        --Bug#10416960
1808        IF(l_ppa_invoice_dists_list(i).line_type_lookup_code not in ('RETROEXPENSE', 'RETROACCRUAL', 'ERV')) THEN
1809           l_ppa_invoice_dists_list(i).charge_applicable_to_dist_id :=
1810             AP_RETRO_PRICING_UTIL_PKG.Get_corresponding_retro_DistId(
1811                          l_ppa_lines_rec.match_type,
1812                          l_adj_dists_list(i).charge_applicable_to_dist_id);
1813        END IF;
1814 
1815        l_line_amount      := l_line_amount + l_ppa_invoice_dists_list(i).amount;
1816        l_line_base_amount := l_line_base_amount + l_ppa_invoice_dists_list(i).base_amount;
1817        --
1818    END LOOP;
1819    --
1820    l_ppa_lines_rec.amount := l_line_amount;
1821    l_ppa_lines_rec.base_amount := l_line_base_amount;
1822 
1823    ----------------------------------------------------------------------------
1824    debug_info := 'Create_Adjustment_Corrections Step 5. Insert the Adj Dists in the'
1825                   ||' Global Temp Table';
1826    ----------------------------------------------------------------------------
1827 
1828     FORALL i IN 1..l_ppa_invoice_dists_list.COUNT
1829          INSERT INTO ap_ppa_invoice_dists_gt values  l_ppa_invoice_dists_list(i);
1830 
1831    ----------------------------------------------------------------------------
1832    debug_info := 'Create_Adjustment_Corrections Step 6. Update Related Id';
1833    ----------------------------------------------------------------------------
1834      --Bug#10416960
1835      BEGIN
1836      UPDATE ap_ppa_invoice_dists_gt  d1
1837         SET related_id = (Select invoice_distribution_id
1838 	                  FROM ap_ppa_invoice_dists_gt d2
1839 			  WHERE d2.invoice_id=d1.invoice_id
1840 			    and d2.invoice_line_number=d1.invoice_line_number
1841 			    and d2.line_type_lookup_code in ('RETROEXPENSE', 'RETROACCRUAL','NONREC_TAX'))
1842       WHERE invoice_id = l_ppa_lines_rec.invoice_id
1843         AND invoice_line_number = l_ppa_lines_rec.line_number;
1844      EXCEPTION
1845       WHEN OTHERS THEN
1846         NULL;
1847      END;
1848 
1849       --Introduced below UPDATE for bug#9573078
1850 
1851       UPDATE ap_ppa_invoice_lines_gt l1
1852         SET amount = l_ppa_lines_rec.amount,
1853             base_amount = l_ppa_lines_rec.base_amount
1854       WHERE invoice_id = l_ppa_lines_rec.invoice_id
1855         AND line_number = l_ppa_lines_rec.line_number;
1856 
1857    -------------------------------------------------------------------------
1858    debug_info := 'Create_Adjustment_Corrections Step 7. Clear PL/SQL tables';
1859    -------------------------------------------------------------------------
1860    l_ppa_invoice_dists_list.DELETE;
1861    l_adj_dists_list.DELETE;
1862    --
1863    RETURN(TRUE);
1864 
1865 
1866 RETURN(TRUE);
1867 
1868 EXCEPTION
1869  WHEN OTHERS THEN
1870     IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
1871       AP_IMPORT_UTILITIES_PKG.Print(AP_IMPORT_INVOICES_PKG.g_debug_switch,
1872                                     debug_info);
1873     END IF;
1874     --
1875     IF (SQLCODE < 0) then
1876       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
1877         AP_IMPORT_UTILITIES_PKG.Print(AP_IMPORT_INVOICES_PKG.g_debug_switch,
1878                                       SQLERRM);
1879       END IF;
1880     END IF;
1881     --
1882     IF ( adj_corr_dists%ISOPEN ) THEN
1883         CLOSE adj_corr_dists;
1884     END IF;
1885     --
1886     RETURN(FALSE);
1887 
1888 END Create_Adjustment_Corrections;
1889 
1890 
1891 /*=============================================================================
1892  |  FUNCTION - Reverse_Redistribute_IPV()
1893  |
1894  |  DESCRIPTION
1895  |        This program is called from Process_Retroprice_Adjustments for every
1896  |  the Base Matched( or a price correction or quantity correction line) that
1897  |  has not been retro adjusted. This procedure creates a zero amount PPA
1898  |  correction adjustment line . Distributions are created in the Temp tables
1899  |  such that there are no net charges to the IPV A/c. If the original line had
1900  |  ERV then the IPV amount is redistributed to the charge a/c and the ERV a/c.
1901  |
1902  |
1903  |  PARAMETERS
1904  |      p_base_currency_code
1905  |      p_instruction_id
1906  |      p_created_by
1907  |      p_lines_rec
1908  |      p_adj_lines_rec     --OUT
1909  |      p_erv_dists_exist   --OUT
1910  |      P_calling_sequence  --Calling sequence
1911  |
1912  |  MODIFICATION HISTORY
1913  |  Date         Author             Description of Change
1914  |  29-JUL-2003  dgulraja           Creation
1915  |
1916  *============================================================================*/
1917 FUNCTION Reverse_Redistribute_IPV(
1918             p_ppa_invoice_rec    IN AP_RETRO_PRICING_PKG.invoice_rec_type,
1919             p_base_currency_code IN VARCHAR2,
1920             p_instruction_id     IN NUMBER,
1921             p_created_by         IN NUMBER,
1922             p_lines_rec          IN AP_RETRO_PRICING_PKG.invoice_lines_rec_type,
1923             p_erv_dists_exist       OUT NOCOPY VARCHAR2,
1924             P_calling_sequence   IN VARCHAR2)
1925 RETURN BOOLEAN IS
1926 
1927 CURSOR ipv_dists(
1928          c_rows        NUMBER) IS
1929 SELECT  aid1.accounting_date,
1930         aid1.accrual_posted_flag,
1931         aid1.amount,
1932         aid1.asset_book_type_code,
1933         aid1.asset_category_id,
1934         aid1.assets_addition_flag,
1935         aid1.assets_tracking_flag,
1936         aid1.attribute_category,
1937         aid1.attribute1,
1938         aid1.attribute10,
1939         aid1.attribute11,
1940         aid1.attribute12,
1941         aid1.attribute13,
1942         aid1.attribute14,
1943         aid1.attribute15,
1944         aid1.attribute2,
1945         aid1.attribute3,
1946         aid1.attribute4,
1947         aid1.attribute5,
1948         aid1.attribute6,
1949         aid1.attribute7,
1950         aid1.attribute8,
1951         aid1.attribute9,
1952         aid1.award_id,
1953         aid1.awt_flag,
1954         aid1.awt_group_id,
1955         aid1.awt_tax_rate_id,
1956         aid1.base_amount,
1957         aid1.batch_id,
1958         aid1.cancellation_flag,
1959         aid1.cash_posted_flag,
1960         aid1.corrected_invoice_dist_id,
1961         aid1.corrected_quantity,
1962         aid1.country_of_supply,
1963         aid1.created_by,
1964         aid1.description,
1965         aid1.dist_code_combination_id,
1966         aid1.dist_match_type,
1967         aid1.distribution_class,
1968         aid1.distribution_line_number,
1969         aid1.encumbered_flag,
1970         aid1.expenditure_item_date,
1971         aid1.expenditure_organization_id,
1972         aid1.expenditure_type,
1973         aid1.final_match_flag,
1974         aid1.global_attribute_category,
1975         aid1.global_attribute1,
1976         aid1.global_attribute10,
1977         aid1.global_attribute11,
1978         aid1.global_attribute12,
1979         aid1.global_attribute13,
1980         aid1.global_attribute14,
1981         aid1.global_attribute15,
1982         aid1.global_attribute16,
1983         aid1.global_attribute17,
1984         aid1.global_attribute18,
1985         aid1.global_attribute19,
1986         aid1.global_attribute2,
1987         aid1.global_attribute20,
1988         aid1.global_attribute3,
1989         aid1.global_attribute4,
1990         aid1.global_attribute5,
1991         aid1.global_attribute6,
1992         aid1.global_attribute7,
1993         aid1.global_attribute8,
1994         aid1.global_attribute9,
1995         aid1.income_tax_region,
1996         aid1.inventory_transfer_status,
1997         aid1.invoice_distribution_id,
1998         aid1.invoice_id,
1999         aid1.invoice_line_number,
2000         aid1.line_type_lookup_code,
2001         aid1.match_status_flag,
2002         aid1.matched_uom_lookup_code,
2003         aid1.merchant_document_number,
2004         aid1.merchant_name,
2005         aid1.merchant_reference,
2006         aid1.merchant_tax_reg_number,
2007         aid1.merchant_taxpayer_id,
2008         aid1.org_id,
2009         aid1.pa_addition_flag,
2010         aid1.pa_quantity,
2011         aid1.period_name,
2012         aid1.po_distribution_id,
2013         aid1.posted_flag,
2014         aid1.project_id,
2015         aid1.quantity_invoiced,
2016         aid1.rcv_transaction_id,
2017         aid1.related_id,
2018         aid1.reversal_flag,
2019         aid1.rounding_amt,
2020         aid1.set_of_books_id,
2021         aid1.task_id,
2022         aid1.type_1099,
2023         aid1.unit_price,
2024         p_instruction_id,        --instruction_id
2025         NULL,                      --charge_applicable_to_dist_id
2026         aid1.INTENDED_USE,
2027         aid1.WITHHOLDING_TAX_CODE_ID,
2028         aid1.PROJECT_ACCOUNTING_CONTEXT,
2029         aid1.REQ_DISTRIBUTION_ID,
2030         aid1.REFERENCE_1,
2031         aid1.REFERENCE_2,
2032         NULL,                   -- line_group_number
2033         aid1.PA_CC_AR_INVOICE_ID,
2034         aid1.PA_CC_AR_INVOICE_LINE_NUM,
2035         aid1.PA_CC_PROCESSED_CODE,
2036 	aid1.pay_awt_group_id,   --bug6817107
2037 	/* Bug#10416960 */
2038         summary_tax_line_id,
2039         detail_tax_dist_id
2040  FROM   ap_invoice_distributions_all aid1,
2041         (SELECT rownum r FROM ap_invoice_distributions_all  WHERE ROWNUM <= c_rows) aid2
2042  WHERE  aid1.invoice_id =  p_lines_rec.invoice_id
2043  AND    aid1.invoice_line_number = p_lines_rec.line_number
2044  AND    aid2.r <= c_rows
2045   --Bug#10416960
2046  AND    aid1.line_type_lookup_code in ('IPV','ERV')
2047  AND    NVL(aid1.cancellation_flag, 'N' ) <> 'Y'
2048  AND    NVL( aid1.reversal_flag, 'N' ) <> 'Y'
2049  AND    NOT EXISTS (SELECT 1
2050                     FROM ap_invoice_distributions_all  aid3
2051                     WHERE aid3.corrected_invoice_dist_id = aid1.invoice_distribution_id
2052                     AND aid3.line_type_lookup_code IN ('RETROACCRUAL', 'RETROEXPENSE')
2053                     );
2054  -- Distribution should not have been Adjusted by prior PPA's. Adjustment
2055  -- Corr is done once. However the PO price adjustment will be done
2056  -- w.r.t modified PO unit price.
2057 
2058 l_adj_lines_rec            AP_RETRO_PRICING_PKG.invoice_lines_rec_type;
2059 l_adj_dists_list           AP_RETRO_PRICING_PKG.invoice_dists_list_type;
2060 l_ipv_dists_list           AP_RETRO_PRICING_PKG.invoice_dists_list_type;
2061 l_ipv_dists_exist          VARCHAR2(1);
2062 
2063 l_rows                     NUMBER;
2064 l_po_exchange_rate         NUMBER;
2065 l_rcv_exchange_rate        NUMBER;
2066 l_original_exchange_rate   NUMBER;
2067 i                          INTEGER;
2068 l_correcting               VARCHAR2(5) := 'IPV';
2069 
2070 current_calling_sequence   VARCHAR2(1000);
2071 debug_info                 VARCHAR2(1000);
2072 Reverse_Redist_IPV_FAILURE    EXCEPTION;
2073 --Bug#10416960
2074 l_adj_dists_list1          AP_RETRO_PRICING_PKG.invoice_dists_list_type;
2075 j                          INTEGER;
2076 
2077 BEGIN
2078 
2079    current_calling_sequence := 'AP_RETRO_PRICING_PKG.Reverse_Redistribute_IPV'
2080                 ||P_Calling_Sequence;
2081    ---------------------------------------------------------------------------
2082    debug_info := 'Reverse_Redistribute_IPV Step 1. Compute Zero Amt Adj Line';
2083    ---------------------------------------------------------------------------
2084    --
2085 
2086    IF (Create_Zero_Amt_Adj_Line(
2087               p_instruction_id,
2088               p_created_by,
2089               l_correcting,
2090               p_lines_rec,
2091               l_adj_lines_rec,    --OUT
2092               current_calling_sequence) <> TRUE) THEN
2093       --
2094       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
2095                 AP_IMPORT_UTILITIES_PKG.Print(
2096                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
2097                    'Create_Zero_Amt_Adj_Line<- '||current_calling_sequence);
2098       END IF;
2099       --
2100       Raise Reverse_Redist_IPV_FAILURE;
2101       --
2102    END IF;
2103    --
2104 
2105    ---------------------------------------------------------------------------
2106    debug_info := 'Reverse_Redistribute_IPV Step 2. Get Exchange rate for the '
2107                   ||'Original Invoice';
2108    ---------------------------------------------------------------------------
2109    SELECT NVL(exchange_rate, 1)
2110      INTO l_original_exchange_rate
2111      FROM ap_invoices_all
2112     WHERE invoice_id = p_lines_rec.invoice_id;
2113 
2114    --------------------------------------------------------------------------
2115    debug_info := 'Reverse_Redistribute_IPV Step 3. Check IF Erv Dists Exist';
2116    --------------------------------------------------------------------------
2117    IF (AP_RETRO_PRICING_UTIL_PKG.Erv_Dists_exists(
2118           p_lines_rec.invoice_id,
2119           p_lines_rec.line_number,
2120           p_erv_dists_exist) <> TRUE) THEN
2121       --
2122       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
2123           AP_IMPORT_UTILITIES_PKG.Print(
2124           AP_IMPORT_INVOICES_PKG.g_debug_switch,
2125           'Erv_Dists_exists<- '||current_calling_sequence);
2126       END IF;
2127       --
2128       Raise Reverse_Redist_IPV_FAILURE;
2129       --
2130    END IF;
2131    --
2132 
2133    IF p_erv_dists_exist = 'Y' THEN
2134       l_rows := 3;
2135    ELSE
2136       l_rows := 2;
2137    END IF;
2138    --
2139 
2140    ---------------------------------------------------------------------------
2141    debug_info := 'Reverse_Redistribute_IPV Step 4. Open cursor ipv_dists_list';
2142    ---------------------------------------------------------------------------
2143    OPEN ipv_dists(
2144           l_rows);
2145     FETCH ipv_dists
2146     BULK COLLECT INTO l_ipv_dists_list;
2147     CLOSE ipv_dists;
2148 
2149    ---------------------------------------------------------------------------
2150    debug_info := 'Reverse_Redistribute_IPV Step 5. Redistribute IPV due to '
2151                   ||'Retropricing';
2152    ----------------------------------------------------------------------------
2153     i :=1;
2154     j:=1;  --Bug#10416960
2155 
2156     WHILE i <= (l_ipv_dists_list.COUNT  - l_rows + 1)
2157     LOOP
2158         --Bug#10416960
2159       IF(p_lines_rec.match_type = 'PRICE_CORRECTION' OR
2160          l_ipv_dists_list(i).line_type_lookup_code = 'IPV')THEN
2161 
2162       --------------------
2163       --IPV Dist
2164       --------------------
2165       --IPV Adj Dist copies Existing IPV Dist
2166       l_adj_dists_list(i) := l_ipv_dists_list(i);
2167       l_adj_dists_list(i).invoice_id :=  l_adj_lines_rec.invoice_id;
2168       l_adj_dists_list(i).invoice_line_number := l_adj_lines_rec.line_number;
2169       --Bug#10416960
2170       l_adj_dists_list(i).invoice_distribution_id := AP_RETRO_PRICING_UTIL_PKG.get_invoice_distribution_id;
2171       --l_adj_dists_list(i).invoice_distribution_id := Null;
2172       l_adj_dists_list(i).distribution_line_number := i;
2173       --Bug#10416960
2174       -- l_adj_dists_list(i).line_type_lookup_code    := 'IPV';
2175       l_adj_dists_list(i).dist_match_type           := 'ADJUSTMENT_CORRECTION';
2176 
2177      --'Reversal of IPV due to Retroactive Pricing of Purchase Order'
2178       FND_MESSAGE.SET_NAME('SQLAP', 'AP_RETRO_IPV_REVERSAL');
2179       l_adj_dists_list(i).description := FND_MESSAGE.GET;
2180 
2181       l_adj_dists_list(i).dist_code_combination_id := l_ipv_dists_list(i).dist_code_combination_id;
2182 
2183       l_adj_dists_list(i).accounting_date := SYSDATE;
2184       l_adj_dists_list(i).period_name := l_adj_lines_rec.period_name;
2185 
2186       l_adj_dists_list(i).amount       := (-1)*l_ipv_dists_list(i).amount;
2187       l_adj_dists_list(i).base_amount  := (-1)*l_ipv_dists_list(i).base_amount;
2188       l_adj_dists_list(i).rounding_amt := (-1)*l_ipv_dists_list(i).rounding_amt;
2189 
2190       l_adj_dists_list(i).posted_flag         := 'N';
2191       l_adj_dists_list(i).cash_posted_flag    := 'N';
2192       l_adj_dists_list(i).accrual_posted_flag := 'N';
2193 
2194       --Following fields will be NULL as we do not select it.
2195       --accounting_event_id
2196       --upgrade_posted_amount
2197 
2198       l_adj_dists_list(i).created_by := l_adj_lines_rec.created_by;
2199       l_adj_dists_list(i).related_id  := l_ipv_dists_list(i).related_id;
2200       l_adj_dists_list(i).corrected_invoice_dist_id := l_ipv_dists_list(i).invoice_distribution_id;
2201       l_adj_dists_list(i).unit_price := (-1)*l_ipv_dists_list(i).unit_price;
2202       l_adj_dists_list(i).match_status_flag  := l_ipv_dists_list(i).match_status_flag; -- Bug 549166
2203       l_adj_dists_list(i).encumbered_flag    := 'N';
2204 
2205       -- Bug 5509712. Comment out following line
2206       l_adj_dists_list(i).po_distribution_id := l_ipv_dists_list(i).po_distribution_id;
2207       l_adj_dists_list(i).rcv_transaction_id := l_ipv_dists_list(i).rcv_transaction_id  ;
2208 
2209       --l_adj_dists_list(i).po_distribution_id := NULL;
2210       --l_adj_dists_list(i).rcv_transaction_id := NULL;
2211 
2212        -- Start of bug# 9504423
2213        IF l_adj_dists_list(i).project_id IS NOT NULL THEN
2214          l_adj_dists_list(i).pa_Addition_flag := 'N';
2215        ELSE
2216          l_adj_dists_list(i).pa_Addition_flag := 'E';
2217        END IF;
2218       --End bug#9504423
2219 
2220 
2221       ---------------
2222       -- Expense Dist
2223       ---------------
2224       l_adj_dists_list(i+1)   := l_adj_dists_list(i);
2225          --Bug#10416960
2226       l_adj_dists_list(i+1).invoice_distribution_id := AP_RETRO_PRICING_UTIL_PKG.get_invoice_distribution_id;
2227       --l_adj_dists_list(i+1).invoice_distribution_id := Null;
2228       l_adj_dists_list(i+1).distribution_line_number := i+1;
2229       --
2230 
2231        --Bug#10416960
2232 
2233   IF(l_adj_dists_list(i).line_type_lookup_code='IPV'
2234       AND p_lines_rec.match_type = 'PRICE_CORRECTION') THEN
2235        l_adj_dists_list(i+1).dist_code_combination_id :=
2236         AP_RETRO_PRICING_UTIL_PKG.get_ccid(l_ipv_dists_list(i).corrected_invoice_dist_id);
2237   ELSE
2238    --ERV case. For ERV always related_id only get stamped
2239    --IPV for base match case
2240      l_adj_dists_list(i+1).dist_code_combination_id :=
2241                   AP_RETRO_PRICING_UTIL_PKG.get_ccid(l_ipv_dists_list(i).related_id);
2242   END IF;
2243 
2244 
2245  IF(l_adj_dists_list(i).line_type_lookup_code='IPV'
2246     AND p_lines_rec.match_type = 'PRICE_CORRECTION') THEN
2247    l_adj_dists_list(i+1).line_type_lookup_code :=
2248        AP_RETRO_PRICING_UTIL_PKG.get_dist_type_lookup_code(
2249              l_ipv_dists_list(i).corrected_invoice_dist_id);
2250  ELSIF (l_adj_dists_list(i).line_type_lookup_code='IPV'
2251         AND p_lines_rec.match_type <> 'PRICE_CORRECTION') THEN
2252     l_adj_dists_list(i+1).line_type_lookup_code :=
2253        AP_RETRO_PRICING_UTIL_PKG.get_dist_type_lookup_code(
2254              l_ipv_dists_list(i).related_id);
2255  ELSE
2256    --Non IPV(ERV) case. Same as previous line type
2257     l_adj_dists_list(i+1).line_type_lookup_code := l_adj_dists_list(i).line_type_lookup_code;
2258  END IF;
2259 
2260   --End Bug#10416960
2261 
2262      l_adj_dists_list(i+1).amount      := l_ipv_dists_list(i).amount;
2263 
2264      /* Bug#10416960
2265       l_adj_dists_list(i+1).base_amount :=
2266             AP_UTILITIES_PKG.ap_round_currency(
2267                    l_ipv_dists_list(i+1).amount*l_original_exchange_rate,
2268                    p_base_currency_code); */
2269 
2270       l_adj_dists_list(i+1).base_amount := l_ipv_dists_list(i).base_amount;
2271 
2272       l_adj_dists_list(i+1).rounding_amt := l_ipv_dists_list(i).rounding_amt;
2273 
2274        --Bug#10416960
2275           l_adj_dists_list(i+1).related_id := l_adj_dists_list(i).invoice_distribution_id;
2276 
2277       --Bug#10416960
2278       --  l_adj_dists_list(i+1).corrected_invoice_dist_id := l_ipv_dists_list(i).invoice_distribution_id;
2279        l_adj_dists_list(i+1).corrected_invoice_dist_id := l_adj_dists_list(i).invoice_distribution_id;
2280 
2281       l_adj_dists_list(i+1).unit_price := l_ipv_dists_list(i).unit_price;
2282 
2283       -- Bug 5509712
2284       l_adj_dists_list(i+1).po_distribution_id := l_ipv_dists_list(i).po_distribution_id;
2285       l_adj_dists_list(i+1).rcv_transaction_id := l_ipv_dists_list(i).rcv_transaction_id  ;
2286 
2287       -------------
2288       --ERV Dist
2289       -------------
2290       --  Only if the base invoice has ERV create the Erv Dist
2291       --Bug#10416960
2292       IF (l_rows=3 and l_ipv_dists_list(i).line_type_lookup_code = 'IPV'
2293 	     and p_lines_rec.match_type <> 'PRICE_CORRECTION') THEN
2294 
2295          l_adj_dists_list(i+2)   :=  l_adj_dists_list(i+1);
2296                --Bug#10416960
2297 	 l_adj_dists_list(i+2).invoice_distribution_id := AP_RETRO_PRICING_UTIL_PKG.get_invoice_distribution_id;
2298          --l_adj_dists_list(i+2).invoice_distribution_id := Null;
2299          l_adj_dists_list(i+2).distribution_line_number := i+2;
2300 
2301          l_adj_dists_list(i+2).line_type_lookup_code   := 'ERV';
2302 
2303           -- automatically set by assignment of line 2.
2304           --Bug#10416960
2305            l_adj_dists_list(i+2).corrected_invoice_dist_id := l_ipv_dists_list(i).invoice_distribution_id;
2306            l_adj_dists_list(i+2).related_id := l_adj_dists_list(i+1).invoice_distribution_id; --or related_dist_id
2307           --End bug#10416960
2308 
2309 
2310          l_adj_dists_list(i+2).amount  :=  0;
2311          --
2312          IF (p_lines_rec.rcv_transaction_id IS NOT NULL) THEN
2313             l_rcv_exchange_rate := AP_RETRO_PRICING_UTIL_PKG.get_exchange_rate(
2314                                       'RECEIPT',
2315                                       p_lines_rec.rcv_transaction_id);
2316                  --Bug#10416960
2317             l_adj_dists_list(i+2).base_amount :=
2318                 AP_UTILITIES_PKG.ap_round_currency(
2319                     l_ipv_dists_list(i).amount*(l_original_exchange_rate - l_rcv_exchange_rate),
2320                     p_base_currency_code);
2321          ELSE
2322             l_po_exchange_rate  := AP_RETRO_PRICING_UTIL_PKG.get_exchange_rate(
2323                                       'PO',
2324                                        l_adj_dists_list(i).po_distribution_id );
2325 				       /*Bug#10416960 p_lines_rec.po_header_id */
2326 
2327                   --Bug#10416960
2328             l_adj_dists_list(i+2).base_amount :=
2329                 AP_UTILITIES_PKG.ap_round_currency(
2330                     l_ipv_dists_list(i).base_amount*(l_original_exchange_rate - l_po_exchange_rate),
2331                     p_base_currency_code);
2332          END IF;
2333          -- Adjust Expense Dist Amount
2334 
2335 	 --Bug#10416960
2336 	 IF(l_adj_dists_list(i+2).base_amount <>0)THEN
2337 	   l_adj_dists_list(i+1).base_amount :=
2338 	         l_adj_dists_list(i+1).base_amount - l_adj_dists_list(i+2).base_amount;
2339 	 END IF;
2340 
2341          l_adj_dists_list(i+2).rounding_amt := 0;
2342 
2343          l_adj_dists_list(i+2).dist_code_combination_id := AP_RETRO_PRICING_UTIL_PKG.get_erv_ccid(
2344                                                                    l_adj_dists_list(i+2).corrected_invoice_dist_id);
2345         -- l_adj_dists_list(i+2).corrected_invoice_dist_id := l_ipv_dists_list(i).invoice_distribution_id;
2346          l_adj_dists_list(i+2).unit_price := NULL;
2347 
2348           -- Bug 5509712
2349          l_adj_dists_list(i+2).po_distribution_id := l_ipv_dists_list(i).po_distribution_id;
2350          l_adj_dists_list(i+2).rcv_transaction_id := l_ipv_dists_list(i).rcv_transaction_id  ;
2351 
2352          --
2353       END IF;
2354       --
2355        --Bug#10416960
2356             IF(l_adj_dists_list.exists(i))THEN
2357                l_adj_dists_list1(j) := l_adj_dists_list(i);
2358                j := j +1;
2359             END IF;
2360 
2361             IF(l_adj_dists_list.exists(i+1))THEN
2362                  l_adj_dists_list1(j) := l_adj_dists_list(i+1);
2363                   j := j +1;
2364 
2365             END IF;
2366 
2367             IF(l_adj_dists_list.exists(i+2))THEN
2368                l_adj_dists_list1(j) := l_adj_dists_list(i+2);
2369                j := j +1;
2370             END IF;
2371             --End Bug#10416960
2372 
2373           END IF;
2374 
2375       i:=i+ l_rows;  --loop counter
2376       --
2377      END LOOP;
2378      -------------------------------------------------------------------------
2379      debug_info := 'Reverse_Redistribute_IPV Step 6. Insert the Adj Dists in '
2380                     ||'the Global Temp Table ';
2381      -------------------------------------------------------------------------
2382       --Bug#10416960
2383         FORALL i IN 1..l_adj_dists_list1.COUNT
2384           INSERT INTO ap_ppa_invoice_dists_gt values  l_adj_dists_list1(i);
2385 
2386 
2387      -------------------------------------------------------------------------
2388      debug_info := 'Reverse_Redistribute_IPV Step 7. Clear PL/SQL tables';
2389      -------------------------------------------------------------------------
2390      l_ipv_dists_list.DELETE;
2391      l_adj_dists_list.DELETE;
2392      l_adj_dists_list1.DELETE; --Bug#10416960
2393 
2394      -------------------------------------------------------------------------
2395      debug_info := 'Reverse_Redistribute_IPV Step 8. Reverse outstanding price'
2396                    ||' correction';
2397      -------------------------------------------------------------------------
2398 
2399      IF p_lines_rec.match_type = 'PRICE_CORRECTION' THEN
2400         --
2401         IF (Create_Adjustment_Corrections(
2402                     p_ppa_invoice_rec,
2403                     p_base_currency_code,
2404                     l_adj_lines_rec,
2405                     current_calling_sequence) <> TRUE) THEN
2406           --
2407           IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
2408               AP_IMPORT_UTILITIES_PKG.Print(
2409               AP_IMPORT_INVOICES_PKG.g_debug_switch,
2410               'Create_Adjustment_Corrections<- '||current_calling_sequence);
2411           END IF;
2412           --
2413           Raise Reverse_Redist_IPV_FAILURE;
2414         END IF;
2415         --
2416      END IF;
2417 
2418 RETURN (TRUE);
2419 
2420 EXCEPTION
2421  WHEN OTHERS THEN
2422     IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
2423       AP_IMPORT_UTILITIES_PKG.Print(AP_IMPORT_INVOICES_PKG.g_debug_switch,
2424                                     debug_info);
2425     END IF;
2426 
2427     IF (SQLCODE < 0) then
2428       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
2429         AP_IMPORT_UTILITIES_PKG.Print(AP_IMPORT_INVOICES_PKG.g_debug_switch,
2430                                       SQLERRM);
2431       END IF;
2432     END IF;
2433     --
2434     IF ( ipv_dists%ISOPEN ) THEN
2435         CLOSE ipv_dists;
2436     END IF;
2437     --
2438     RETURN(FALSE);
2439     --
2440 END Reverse_Redistribute_IPV;
2441 
2442 /*=============================================================================
2443  |  FUNCTION - Reverse_Redistribute_TIPV()
2444  |
2445  |  DESCRIPTION
2446  |           This program is called from Process_TIPV_Reversal for every
2447  |  Tax line that is matched to original line that has not been retro-adjusted.
2448  |  This procedure creates a zero amount Tax adjustment line . Distributions
2449  |  are created in the Temp tables such that there are no net charges to the
2450  |  TIPV A/c. If the tax line has a TERV then the TIPV amount is redistributed
2451  |  to the nonrecoverable charge a/c and the TERV a/c.
2452  |
2453  |  Note: Payables only support Exclusive Tax for PO Matched Invoices and as a
2454  |        consequence TIPV distributions exist only for Tax Lines that are
2455  |        allocated to the original line
2456  |
2457  |  PARAMETERS
2458  |      p_base_currency_code
2459  |      p_instruction_id
2460  |      p_created_by
2461  |      p_original_exchange_rate
2462  |      p_lines_rec
2463  |      p_tax_lines_rec
2464  |      p_erv_dists_exist
2465  |      P_calling_sequence - Calling sequence
2466  |
2467  |  MODIFICATION HISTORY
2468  |  Date         Author             Description of Change
2469  |  29-JUL-2003  dgulraja           Creation
2470  |
2471  *============================================================================*/
2472 FUNCTION Reverse_Redistribute_TIPV(
2473             p_ppa_invoice_rec        IN AP_RETRO_PRICING_PKG.invoice_rec_type,
2474             p_base_currency_code     IN VARCHAR2,
2475             p_instruction_id         IN NUMBER,
2476             p_created_by             IN NUMBER,
2477             p_original_exchange_rate IN NUMBER,
2478             p_lines_rec              IN AP_RETRO_PRICING_PKG.invoice_lines_rec_type,
2479             p_tax_lines_rec          IN AP_RETRO_PRICING_PKG.invoice_lines_rec_type,
2480             P_calling_sequence       IN VARCHAR2)
2481 RETURN BOOLEAN IS
2482 
2483 CURSOR tipv_dists(
2484          c_rows                IN   NUMBER) IS
2485 SELECT  aid1.accounting_date,
2486         aid1.accrual_posted_flag,
2487         aid1.amount,
2488         aid1.asset_book_type_code,
2489         aid1.asset_category_id,
2490         aid1.assets_addition_flag,
2491         aid1.assets_tracking_flag,
2492         aid1.attribute_category,
2493         aid1.attribute1,
2494         aid1.attribute10,
2495         aid1.attribute11,
2496         aid1.attribute12,
2497         aid1.attribute13,
2498         aid1.attribute14,
2499         aid1.attribute15,
2500         aid1.attribute2,
2501         aid1.attribute3,
2502         aid1.attribute4,
2503         aid1.attribute5,
2504         aid1.attribute6,
2505         aid1.attribute7,
2506         aid1.attribute8,
2507         aid1.attribute9,
2508         aid1.award_id,
2509         aid1.awt_flag,
2510         aid1.awt_group_id,
2511         aid1.awt_tax_rate_id,
2512         aid1.base_amount,
2513         aid1.batch_id,
2514         aid1.cancellation_flag,
2515         aid1.cash_posted_flag,
2516         aid1.corrected_invoice_dist_id,
2517         aid1.corrected_quantity,
2518         aid1.country_of_supply,
2519         aid1.created_by,
2520         aid1.description,
2521         aid1.dist_code_combination_id,
2522         aid1.dist_match_type,
2523         aid1.distribution_class,
2524         aid1.distribution_line_number,
2525         aid1.encumbered_flag,
2526         aid1.expenditure_item_date,
2527         aid1.expenditure_organization_id,
2528         aid1.expenditure_type,
2529         aid1.final_match_flag,
2530         aid1.global_attribute_category,
2531         aid1.global_attribute1,
2532         aid1.global_attribute10,
2533         aid1.global_attribute11,
2534         aid1.global_attribute12,
2535         aid1.global_attribute13,
2536         aid1.global_attribute14,
2537         aid1.global_attribute15,
2538         aid1.global_attribute16,
2539         aid1.global_attribute17,
2540         aid1.global_attribute18,
2541         aid1.global_attribute19,
2542         aid1.global_attribute2,
2543         aid1.global_attribute20,
2544         aid1.global_attribute3,
2545         aid1.global_attribute4,
2546         aid1.global_attribute5,
2547         aid1.global_attribute6,
2548         aid1.global_attribute7,
2549         aid1.global_attribute8,
2550         aid1.global_attribute9,
2551         aid1.income_tax_region,
2552         aid1.inventory_transfer_status,
2553         aid1.invoice_distribution_id,
2554         aid1.invoice_id,
2555         aid1.invoice_line_number,
2556         aid1.line_type_lookup_code,
2557         aid1.match_status_flag,
2558         aid1.matched_uom_lookup_code,
2559         aid1.merchant_document_number,
2560         aid1.merchant_name,
2561         aid1.merchant_reference,
2562         aid1.merchant_tax_reg_number,
2563         aid1.merchant_taxpayer_id,
2564         aid1.org_id,
2565         aid1.pa_addition_flag,
2566         aid1.pa_quantity,
2567         aid1.period_name,
2568         aid1.po_distribution_id,
2569         aid1.posted_flag,
2570         aid1.project_id,
2571         aid1.quantity_invoiced,
2572         aid1.rcv_transaction_id,
2573         aid1.related_id,
2574         aid1.reversal_flag,
2575         aid1.rounding_amt,
2576         aid1.set_of_books_id,
2577         aid1.task_id,
2578         aid1.type_1099,
2579         aid1.unit_price,
2580         p_instruction_id,        --instruction_id
2581         aid1.charge_applicable_to_dist_id,
2582         aid1.INTENDED_USE,
2583         aid1.WITHHOLDING_TAX_CODE_ID,
2584         aid1.PROJECT_ACCOUNTING_CONTEXT,
2585         aid1.REQ_DISTRIBUTION_ID,
2586         aid1.REFERENCE_1,
2587         aid1.REFERENCE_2,
2588         NULL,                   -- line_group_number
2589         aid1.PA_CC_AR_INVOICE_ID,
2590         aid1.PA_CC_AR_INVOICE_LINE_NUM,
2591         aid1.PA_CC_PROCESSED_CODE,
2592 	aid1.pay_awt_group_id, --bugu6817107
2593 	 /* Bug#10416960 */
2594         summary_tax_line_id,
2595        detail_tax_dist_id
2596  FROM   ap_invoice_distributions_all aid1,
2597         (SELECT rownum r FROM ap_invoice_distributions_all  WHERE ROWNUM <= c_rows) aid2
2598  WHERE  aid1.invoice_id =  p_tax_lines_rec.invoice_id
2599  AND    aid1.invoice_line_number = p_tax_lines_rec.line_number
2600  AND    aid2.r <= c_rows
2601   --Bug#10416960
2602  AND    aid1.line_type_lookup_code in ('TIPV','TRV','REC_TAX','TERV')
2603  AND    NVL(aid1.cancellation_flag, 'N' ) <> 'Y'
2604  AND    NVL( aid1.reversal_flag, 'N' ) <> 'Y'
2605  AND    NOT EXISTS (SELECT 1
2606                       FROM ap_invoice_distributions_all  aid3
2607                      WHERE aid3.corrected_invoice_dist_id = aid1.invoice_distribution_id
2608                         --Bug#10416960
2609                       --AND aid3.line_type_lookup_code IN ('RETROTAX')
2610                       AND aid3.dist_match_type='ADJUSTMENT_CORRECTION')
2611  AND   aid1.charge_applicable_to_dist_id IN
2612 	                (SELECT invoice_distribution_id
2613 	                   FROM ap_invoice_distributions_all
2614 	                  WHERE invoice_id  = p_lines_rec.invoice_id
2615                        --Bug5485084 replaced p_tax_lines_rec with p_lines_rec
2616 	                    AND invoice_line_number = p_lines_rec.line_number);
2617  -- Distribution should not have been Adjusted by prior PPA's. Adjustment
2618  -- Corr is done once.
2619 l_tipv_adj_lines_rec       AP_RETRO_PRICING_PKG.invoice_lines_rec_type;
2620 l_tipv_adj_dists_list      AP_RETRO_PRICING_PKG.invoice_dists_list_type;
2621 l_tipv_dists_list          AP_RETRO_PRICING_PKG.invoice_dists_list_type;
2622 l_terv_ccid       AP_INVOICE_DISTRIBUTIONS_ALL.dist_code_combination_id%TYPE;
2623 l_terv_dists_exist                VARCHAR2(1);
2624 l_rows                            NUMBER;
2625 l_po_exchange_rate                NUMBER;
2626 l_rcv_exchange_rate               NUMBER;
2627 l_original_exchange_rate          NUMBER;
2628 i                                 INTEGER;
2629 l_correcting                      VARCHAR2(5) := 'TIPV';
2630 current_calling_sequence          VARCHAR2(1000);
2631 debug_info                        VARCHAR2(1000);
2632 Tipv_Adjustment_Corr_Failure      EXCEPTION;
2633 --Bug#10416960
2634 l_tipv_adj_dists_list1            AP_RETRO_PRICING_PKG.invoice_dists_list_type;
2635 j                                 INTEGER;
2636 
2637 BEGIN
2638    --
2639    current_calling_sequence := 'AP_RETRO_PRICING_PKG.Reverse_Redistribute_TIPV'
2640                 ||P_Calling_Sequence;
2641    ---------------------------------------------------------------------------
2642    debug_info := 'Reverse_Redistribute_TIPV Step 1. Compute Zero Amt TAX '
2643                   ||'Adj Line ';
2644    ----------------------------------------------------------------------------
2645 
2646 
2647    IF (Create_Zero_Amt_Adj_Line(
2648          p_instruction_id,
2649          p_created_by,
2650          l_correcting,
2651          p_tax_lines_rec,
2652          l_tipv_adj_lines_rec, --OUT
2653          current_calling_sequence) <> TRUE) THEN
2654        --
2655        IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
2656            AP_IMPORT_UTILITIES_PKG.Print(
2657              AP_IMPORT_INVOICES_PKG.g_debug_switch,
2658               'Create_Zero_Amt_Adj_Line<- '||current_calling_sequence);
2659         END IF;
2660        --
2661        Raise Tipv_Adjustment_Corr_Failure;
2662        --
2663     END IF;
2664     --
2665     ---------------------------------------------------------------------------
2666     debug_info := 'Reverse_Redistribute_TIPV Step 2. Check IF Terv Dists '
2667                    ||'exists ';
2668     --------------------------------------------------------------------------
2669     IF (AP_RETRO_PRICING_UTIL_PKG.Terv_Dists_exists(
2670           p_tax_lines_rec.invoice_id,
2671           p_tax_lines_rec.line_number,
2672           l_terv_dists_exist) <> TRUE) THEN
2673       --
2674       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
2675           AP_IMPORT_UTILITIES_PKG.Print(
2676           AP_IMPORT_INVOICES_PKG.g_debug_switch,
2677           'Terv_Dists_exists<- '||current_calling_sequence);
2678       END IF;
2679       --
2680       Raise Tipv_Adjustment_Corr_Failure;
2681       --
2682    END IF;
2683    --
2684    --Set the number of rows to be retrieved
2685    IF l_terv_dists_exist = 'Y' THEN
2686       l_rows := 3;
2687    ELSE
2688       l_rows := 2;
2689    END IF;
2690    --
2691    ---------------------------------------------------------------------------
2692    debug_info := 'Reverse_Redistribute_TIPV Step 3. Open cursor tipv_dists';
2693    --------------------------------------------------------------------------
2694    OPEN tipv_dists(
2695           l_rows);
2696    FETCH tipv_dists
2697    BULK COLLECT INTO l_tipv_dists_list;
2698    CLOSE tipv_dists;
2699    --
2700    ---------------------------------------------------------------------------
2701    debug_info := 'Reverse_Redistribute_TIPV Step 4. Redistribute TIPV due '
2702                  ||'to Retropricing ';
2703    ---------------------------------------------------------------------------
2704    i:=1;  --Bug5485084
2705    j:=1;  --Bug#10416960
2706 
2707    WHILE i <= (l_tipv_dists_list.COUNT  - l_rows + 1)
2708    LOOP
2709         --Bug#10416960
2710       IF(p_lines_rec.match_type = 'PRICE_CORRECTION' OR
2711          l_tipv_dists_list(i).line_type_lookup_code = 'TIPV')THEN
2712 
2713         --------------------
2714         --TIPV Dist
2715         --------------------
2716         --TIPV Adj Dist copies Existing TIPV Dist
2717         l_tipv_adj_dists_list(i) := l_tipv_dists_list(i);
2718         l_tipv_adj_dists_list(i).invoice_id := l_tipv_adj_lines_rec.invoice_id;
2719         l_tipv_adj_dists_list(i).invoice_line_number := l_tipv_adj_lines_rec.line_number;
2720 	      --Bug#10416960
2721         l_tipv_adj_dists_list(i).invoice_distribution_id := AP_RETRO_PRICING_UTIL_PKG.get_invoice_distribution_id;
2722         --l_tipv_adj_dists_list(i).invoice_distribution_id := Null;
2723         l_tipv_adj_dists_list(i).distribution_line_number := i;
2724 	--Bug#10416960
2725         --l_tipv_adj_dists_list(i).line_type_lookup_code    := 'TIPV';
2726         l_tipv_adj_dists_list(i).dist_match_type   := 'ADJUSTMENT_CORRECTION';
2727 
2728         --'Reversal of TIPV due to Retroactive Pricing of Purchase Order'
2729         FND_MESSAGE.SET_NAME('SQLAP', 'AP_RETRO_TIPV_REVERSAL');
2730         l_tipv_adj_dists_list(i).description := FND_MESSAGE.GET;
2731 
2732         l_tipv_adj_dists_list(i).dist_code_combination_id := l_tipv_dists_list(i).dist_code_combination_id;
2733 
2734         l_tipv_adj_dists_list(i).accounting_date := SYSDATE;
2735         l_tipv_adj_dists_list(i).period_name := l_tipv_adj_lines_rec.period_name;
2736 
2737         l_tipv_adj_dists_list(i).amount       := (-1)*l_tipv_dists_list(i).amount;
2738         l_tipv_adj_dists_list(i).base_amount  := (-1)*l_tipv_dists_list(i).base_amount;
2739         l_tipv_adj_dists_list(i).rounding_amt := (-1)*l_tipv_dists_list(i).rounding_amt;
2740 
2741         l_tipv_adj_dists_list(i).posted_flag         := 'N';
2742         --Following fields will be NULL as we do not select it.
2743         --accounting_event_id
2744         --upgrade_posted_amount
2745 
2746         l_tipv_adj_dists_list(i).created_by := l_tipv_adj_lines_rec.created_by;
2747 
2748         l_tipv_adj_dists_list(i).related_id  := l_tipv_dists_list(i).related_id;
2749         l_tipv_adj_dists_list(i).corrected_invoice_dist_id := l_tipv_dists_list(i).invoice_distribution_id;
2750 	-- Bug#10416960
2751          --l_tipv_adj_dists_list(i).charge_applicable_to_dist_id := l_tipv_dists_list(i).charge_applicable_to_dist_id;
2752 
2753 	 l_tipv_adj_dists_list(i).charge_applicable_to_dist_id :=
2754             AP_RETRO_PRICING_UTIL_PKG.Get_corresponding_retro_DistId(
2755                          p_lines_rec.match_type,
2756                          l_tipv_dists_list(i).charge_applicable_to_dist_id);
2757 
2758         l_tipv_adj_dists_list(i).cash_posted_flag    := 'N';
2759         l_tipv_adj_dists_list(i).accrual_posted_flag := 'N';
2760         l_tipv_adj_dists_list(i).match_status_flag   := l_tipv_dists_list(i).match_status_flag;
2761         l_tipv_adj_dists_list(i).encumbered_flag     := 'N';
2762         --l_tipv_adj_dists_list(i).po_distribution_id  := NULL;
2763         --l_tipv_adj_dists_list(i).rcv_transaction_id  := NULL;
2764 
2765         -- Bug 5509712
2766         l_tipv_adj_dists_list(i).po_distribution_id := l_tipv_dists_list(i).po_distribution_id;
2767         l_tipv_adj_dists_list(i).rcv_transaction_id := l_tipv_dists_list(i).rcv_transaction_id;
2768 
2769 	  --Start bug#9504423
2770 	IF l_tipv_adj_dists_list(i).project_id IS NOT NULL THEN
2771                l_tipv_adj_dists_list(i).pa_Addition_flag := 'N';
2772         ELSE
2773                l_tipv_adj_dists_list(i).pa_Addition_flag := 'E';
2774         END IF;
2775          --End bug#9504423
2776 
2777         --------------------------
2778         -- NonRecoverable Tax Dist
2779         ----------------------------
2780         l_tipv_adj_dists_list(i+1)   := l_tipv_adj_dists_list(i);
2781 	      --Bug#10416960
2782         l_tipv_adj_dists_list(i+1).invoice_distribution_id := AP_RETRO_PRICING_UTIL_PKG.get_invoice_distribution_id;
2783         --l_tipv_adj_dists_list(i+1).invoice_distribution_id := Null;
2784         l_tipv_adj_dists_list(i+1).distribution_line_number := i+1;
2785 
2786 	  --Bug#10416960
2787         IF(l_tipv_adj_dists_list(i).line_type_lookup_code='TIPV')THEN
2788             l_tipv_adj_dists_list(i+1).line_type_lookup_code := 'NONREC_TAX';
2789         ELSE
2790             l_tipv_adj_dists_list(i+1).line_type_lookup_code := l_tipv_adj_dists_list(i).line_type_lookup_code;
2791 	END IF;
2792 
2793 
2794         l_tipv_adj_dists_list(i+1).amount      := l_tipv_dists_list(i).amount; --Bug5485084 removed -1
2795          /*Bug#10416960
2796 	  l_tipv_adj_dists_list(i+1).base_amount :=
2797             AP_UTILITIES_PKG.ap_round_currency(
2798               l_tipv_dists_list(i+1).amount*p_original_exchange_rate,
2799               p_base_currency_code); */
2800 
2801         l_tipv_adj_dists_list(i+1).base_amount := l_tipv_dists_list(i).base_amount;
2802 
2803         l_tipv_adj_dists_list(i+1).rounding_amt := l_tipv_dists_list(i).rounding_amt;
2804 
2805         IF (l_rows = 3) THEN
2806           l_tipv_adj_dists_list(i+1).related_id := l_tipv_adj_dists_list(i+1).invoice_distribution_id;
2807         ELSE
2808           l_tipv_adj_dists_list(i+1).related_id := NULL;
2809         END IF;
2810 
2811 
2812       --Bug#10416960
2813         l_tipv_adj_dists_list(i+1).charge_applicable_to_dist_id :=
2814             AP_RETRO_PRICING_UTIL_PKG.Get_corresponding_retro_DistId(
2815                          p_lines_rec.match_type,
2816                          l_tipv_adj_dists_list(i).charge_applicable_to_dist_id);
2817 
2818          l_tipv_adj_dists_list(i+1).dist_code_combination_id :=
2819                        AP_RETRO_PRICING_UTIL_PKG.Get_ccid(
2820                                l_tipv_dists_list(i+1).charge_applicable_to_dist_id);  --Bug5485084 replaced l_tipv_adj_dists_list with l_tipv_dists_list
2821 
2822          -- Bug 5509712
2823          l_tipv_adj_dists_list(i+1).po_distribution_id := l_tipv_dists_list(i).po_distribution_id;
2824          l_tipv_adj_dists_list(i+1).rcv_transaction_id := l_tipv_dists_list(i).rcv_transaction_id;
2825 
2826         --Bug#10416960
2827          l_tipv_adj_dists_list(i+1).corrected_invoice_dist_id := l_tipv_adj_dists_list(i).invoice_distribution_id;
2828 
2829 
2830          -------------
2831          --TERV Dist
2832          -------------
2833          --  Only if the base invoice has ERV create the Terv Dist
2834 	  --Bug#10416960
2835           IF (l_rows=3 and l_tipv_dists_list(i).line_type_lookup_code = 'TIPV'
2836 	     and p_lines_rec.match_type <> 'PRICE_CORRECTION') THEN
2837 
2838 	     l_tipv_adj_dists_list(i+2)   :=  l_tipv_adj_dists_list(i+1);
2839 	          --Bug#10416960
2840             l_tipv_adj_dists_list(i+2).invoice_distribution_id :=
2841                                   AP_RETRO_PRICING_UTIL_PKG.get_invoice_distribution_id;
2842            -- l_tipv_adj_dists_list(i+2).invoice_distribution_id := Null;
2843             l_tipv_adj_dists_list(i+2).distribution_line_number := i+2;
2844 
2845             l_tipv_adj_dists_list(i+2).line_type_lookup_code   := 'TERV';
2846             l_tipv_adj_dists_list(i+2).amount  :=  0;
2847             --
2848             --NOTE: Exchange Rate is always calculated w.r.t base match line
2849             IF (p_lines_rec.rcv_transaction_id IS NOT NULL) THEN
2850                 l_rcv_exchange_rate := AP_RETRO_PRICING_UTIL_PKG.get_exchange_rate(
2851                                           'RECEIPT',
2852                                           p_lines_rec.rcv_transaction_id);
2853                --Bug#10416960
2854               l_tipv_adj_dists_list(i+2).base_amount :=
2855                AP_UTILITIES_PKG.ap_round_currency(
2856                    l_tipv_dists_list(i).amount*(p_original_exchange_rate - l_rcv_exchange_rate),
2857                    p_base_currency_code);
2858             ELSE
2859               l_po_exchange_rate  := AP_RETRO_PRICING_UTIL_PKG.get_exchange_rate(
2860                                         'PO',
2861                                         l_tipv_adj_dists_list(i).po_distribution_id );
2862 					/*Bug#10416960 p_lines_rec.po_header_id */
2863                 --Bug#10416960
2864               l_tipv_adj_dists_list(i+2).base_amount :=
2865                  AP_UTILITIES_PKG.ap_round_currency(
2866                    l_tipv_dists_list(i).amount*(p_original_exchange_rate - l_po_exchange_rate),
2867                    p_base_currency_code);
2868             END IF;
2869 
2870 	    --Bug#10416960
2871 	    IF(l_tipv_adj_dists_list(i+2).base_amount <>0)THEN
2872 	     l_tipv_adj_dists_list(i+1).base_amount :=
2873 	         l_tipv_adj_dists_list(i+1).base_amount-l_tipv_adj_dists_list(i+2).base_amount;
2874 	    END IF;
2875 
2876             l_tipv_adj_dists_list(i+2).rounding_amt := NULL;
2877 
2878              --Bug#10416960
2879             l_tipv_adj_dists_list(i+2).corrected_invoice_dist_id := l_tipv_dists_list(i).invoice_distribution_id;
2880             l_tipv_adj_dists_list(i+2).related_id := l_tipv_adj_dists_list(i+1).invoice_distribution_id; --or related_dist_id
2881 	    --end
2882 
2883             l_tipv_adj_dists_list(i+2).charge_applicable_to_dist_id :=
2884                   l_tipv_adj_dists_list(i+1).charge_applicable_to_dist_id;
2885 
2886             l_tipv_adj_dists_list(i+2).dist_code_combination_id :=  AP_RETRO_PRICING_UTIL_PKG.get_terv_ccid(
2887                                                                    l_tipv_adj_dists_list(i+2).corrected_invoice_dist_id);
2888 
2889             -- Bug 5509712
2890             l_tipv_adj_dists_list(i+2).po_distribution_id := l_tipv_dists_list(i).po_distribution_id;
2891             l_tipv_adj_dists_list(i+2).rcv_transaction_id := l_tipv_dists_list(i).rcv_transaction_id;
2892 
2893          END IF;
2894 
2895 	    --Bug#10416960
2896             IF(l_tipv_adj_dists_list.exists(i))THEN
2897                l_tipv_adj_dists_list1(j) := l_tipv_adj_dists_list(i);
2898                j := j +1;
2899             END IF;
2900 
2901             IF(l_tipv_adj_dists_list.exists(i+1))THEN
2902                  l_tipv_adj_dists_list1(j) := l_tipv_adj_dists_list(i+1);
2903                   j := j +1;
2904 
2905             END IF;
2906 
2907             IF(l_tipv_adj_dists_list.exists(i+2))THEN
2908                l_tipv_adj_dists_list1(j) := l_tipv_adj_dists_list(i+2);
2909                j := j +1;
2910             END IF;
2911             --End Bug#10416960
2912 
2913            END IF;
2914 
2915          --
2916          i:=i+ l_rows;  --loop counter
2917          --
2918     END LOOP;
2919 
2920     --------------------------------------------------------------------------
2921     debug_info := 'Reverse_Redistribute_TIPV Step 4. Insert the '
2922                   ||'Adjustments in the Global Temp Table';
2923     --------------------------------------------------------------------------
2924    --Bug#10416960
2925         FORALL i IN 1..l_tipv_adj_dists_list1.COUNT
2926           INSERT INTO ap_ppa_invoice_dists_gt values  l_tipv_adj_dists_list1(i);
2927 
2928     -------------------------------------------------------------------------
2929     debug_info := 'Reverse_Redistribute_TIPV Step 5. Clear PL/SQL tables';
2930     -------------------------------------------------------------------------
2931      l_tipv_adj_dists_list.DELETE;
2932      l_tipv_dists_list.DELETE;
2933      l_tipv_adj_dists_list1.DELETE; --Bug#10416960
2934 
2935     ---------------------------------------------------------------------------
2936     debug_info := 'Reverse_Redistribute_TIPV Step 6. Reverse outstanding PC';
2937     ---------------------------------------------------------------------------
2938     IF p_lines_rec.match_type = 'PRICE_CORRECTION' THEN
2939         IF (Create_Adjustment_Corrections(
2940                    p_ppa_invoice_rec,
2941                    p_base_currency_code,
2942                    l_tipv_adj_lines_rec,
2943                    current_calling_sequence) <> TRUE) THEN
2944           --
2945           IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
2946               AP_IMPORT_UTILITIES_PKG.Print(
2947               AP_IMPORT_INVOICES_PKG.g_debug_switch,
2948               'Create_Adjustment_Corrections<- '||current_calling_sequence);
2949           END IF;
2950           --
2951           Raise Tipv_Adjustment_Corr_Failure;
2952         END IF;
2953         --
2954     END IF;
2955     --
2956 RETURN (TRUE);
2957 
2958 
2959 EXCEPTION
2960  WHEN OTHERS THEN
2961     IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
2962       AP_IMPORT_UTILITIES_PKG.Print(AP_IMPORT_INVOICES_PKG.g_debug_switch,
2963                                     debug_info);
2964     END IF;
2965 
2966     IF (SQLCODE < 0) then
2967       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
2968         AP_IMPORT_UTILITIES_PKG.Print(AP_IMPORT_INVOICES_PKG.g_debug_switch,
2969                                       SQLERRM);
2970       END IF;
2971     END IF;
2972     --
2973     IF ( tipv_dists%ISOPEN ) THEN
2974         CLOSE tipv_dists;
2975     END IF;
2976     --
2977   RETURN(FALSE);
2978   --
2979 END Reverse_Redistribute_TIPV;
2980 
2981 
2982 
2983 /*=============================================================================
2984  |  FUNCTION - Process_TIPV_Reversal()
2985  |
2986  |  DESCRIPTION
2987  |          This program is called from Process_Retroprice_Adjustments for every
2988  |  the Base Matched( or a price correction or quantity correction line) that
2989  |  has not been retro adjusted had has Taxes associated with it. This procedure
2990  |  calls Reverse_Redistribute_TIPV for every Tax line associted with the
2991  |  original line.
2992  |
2993  |  Note: Payables only support Exclusive Tax for PO Matched Invoices and as a
2994  |        consequence TIPV distributions exist only for Tax Lines that are
2995  |        allocated to the original line that have IPV's on the original line.
2996  |        TIPV is the component of Tax that is due to the IPV on the line that the
2997  |        tax is allocated to.
2998  |
2999  |  PARAMETERS
3000  |      p_base_currency_code
3001  |      p_instruction_id
3002  |      p_created_by
3003  |      p_lines_rec
3004  |      p_tax_lines_list
3005  |      P_calling_sequence - Calling sequence
3006  |
3007  |  MODIFICATION HISTORY
3008  |  Date         Author             Description of Change
3009  |  29-JUL-2003  dgulraja           Creation
3010  |
3011  *============================================================================*/
3012 FUNCTION Process_TIPV_Reversal(
3013             p_ppa_invoice_rec    IN AP_RETRO_PRICING_PKG.invoice_rec_type,
3014             p_base_currency_code IN VARCHAR2,
3015             p_instruction_id    IN NUMBER,
3016             p_created_by         IN NUMBER,
3017             p_lines_rec          IN AP_RETRO_PRICING_PKG.invoice_lines_rec_type,
3018             p_tax_lines_list     IN AP_RETRO_PRICING_PKG.invoice_lines_list_type,
3019             P_calling_sequence   IN VARCHAR2)
3020 RETURN BOOLEAN IS
3021 
3022 
3023 l_tax_lines_rec        AP_RETRO_PRICING_PKG.invoice_lines_rec_type;
3024 --l_tax_lines_list       AP_RETRO_PRICING_PKG.invoice_lines_list_type;
3025 l_original_exchange_rate   NUMBER;
3026 current_calling_sequence   VARCHAR2(1000);
3027 debug_info                 VARCHAR2(1000);
3028 
3029 Process_TIPV_Adj_failure EXCEPTION;
3030 
3031 BEGIN
3032     --
3033     current_calling_sequence :=
3034     'AP_RETRO_PRICING_PKG.Process_TIPV_Reversal<-'
3035     ||P_calling_sequence;
3036 
3037     ---------------------------------------------------------------------------
3038     debug_info := 'Process_TIPV_Reversal Step 1. Get Exchange rate for the '
3039                   ||'Original Invoice';
3040     ---------------------------------------------------------------------------
3041     SELECT exchange_rate
3042       INTO l_original_exchange_rate
3043       FROM ap_invoices_all
3044      WHERE invoice_id = p_lines_rec.invoice_id;
3045 
3046 
3047     --------------------------------------------------------------------------
3048     debug_info := 'Process_TIPV_Reversal Step 2. Reverse_Redistribute_TIPV'
3049                   ||' for Exclusive Tax';
3050     --------------------------------------------------------------------------
3051     FOR i in 1..p_tax_lines_list.COUNT
3052     LOOP
3053     --
3054     l_tax_lines_rec := p_tax_lines_list(i);
3055     --
3056     IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
3057     AP_IMPORT_UTILITIES_PKG.Print(
3058     AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
3059     END IF;
3060     IF (Reverse_Redistribute_TIPV(
3061            p_ppa_invoice_rec,
3062            p_base_currency_code,
3063            p_instruction_id,
3064            p_created_by,
3065            l_original_exchange_rate,
3066            p_lines_rec,
3067            l_tax_lines_rec,
3068            p_calling_sequence) <> TRUE) THEN
3069       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
3070         AP_IMPORT_UTILITIES_PKG.Print(
3071           AP_IMPORT_INVOICES_PKG.g_debug_switch,
3072            'Reverse_Redistribute_TIPV<- '||current_calling_sequence);
3073       END IF;
3074       Raise Process_TIPV_Adj_failure;
3075     END IF;
3076     --
3077     END LOOP;  --Tax_line loop
3078     --
3079     RETURN(TRUE);
3080     --
3081 EXCEPTION
3082 WHEN OTHERS THEN
3083     IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
3084       AP_IMPORT_UTILITIES_PKG.Print(
3085         AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
3086     END IF;
3087 
3088     IF (SQLCODE < 0) then
3089       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
3090         AP_IMPORT_UTILITIES_PKG.Print(AP_IMPORT_INVOICES_PKG.g_debug_switch,SQLERRM);
3091       END IF;
3092     END IF;
3093     RETURN(FALSE);
3094 
3095 END Process_TIPV_Reversal;
3096 
3097 /*============================================================================
3098  |  FUNCTION - Create_Po_Price_Adjustments()
3099  |
3100  |  DESCRIPTION
3101  |     This program is called in context of base match line (or qty
3102  |     correction line) and creates a PPA Line in the  global temp tables.
3103  |     For base match(or qty corr) line the program creates a RETROITEM line
3104  |     of matchtype PO PRICE ADJUSTMENT which records the delta in price.
3105  |
3106  |     NOTE: This program will be called for subsequent Retoprices on the PO
3107  |           which would be driven by the Instruction in the Interface Lines.
3108  |
3109  |  PARAMETERS
3110  |      p_base_currency_code
3111  |      p_instruction_id
3112  |      p_ppa_invoice_rec
3113  |      p_instruction_lines_rec
3114  |      p_lines_rec
3115  |      P_calling_sequence - Calling sequence
3116  |
3117  |  MODIFICATION HISTORY
3118  |  Date         Author             Description of Change
3119  |  29-JUL-2003  dgulraja           Creation
3120  |
3121  *==========================================================================*/
3122 FUNCTION Create_Po_Price_Adjustments(
3123             p_base_currency_code   IN VARCHAR2,
3124             p_instruction_id       IN NUMBER,
3125             p_ppa_invoice_rec      IN AP_RETRO_PRICING_PKG.invoice_rec_type,
3126             p_instruction_lines_rec      IN AP_RETRO_PRICING_PKG.instruction_lines_rec_type,
3127             p_lines_rec            IN AP_RETRO_PRICING_PKG.invoice_lines_rec_type,
3128             P_calling_sequence     IN VARCHAR2)
3129 RETURN BOOLEAN IS
3130 
3131 
3132 CURSOR item_dists IS
3133 SELECT  accounting_date,
3134         accrual_posted_flag,
3135         amount,
3136         asset_book_type_code,
3137         asset_category_id,
3138         assets_addition_flag,
3139         assets_tracking_flag,
3140         attribute_category,
3141         attribute1,
3142         attribute10,
3143         attribute11,
3144         attribute12,
3145         attribute13,
3146         attribute14,
3147         attribute15,
3148         attribute2,
3149         attribute3,
3150         attribute4,
3151         attribute5,
3152         attribute6,
3153         attribute7,
3154         attribute8,
3155         attribute9,
3156         award_id,
3157         awt_flag,
3158         awt_group_id,
3159         awt_tax_rate_id,
3160         base_amount,
3161         batch_id,
3162         cancellation_flag,
3163         cash_posted_flag,
3164         corrected_invoice_dist_id,
3165         corrected_quantity,
3166         country_of_supply,
3167         created_by,
3168         description,
3169         dist_code_combination_id,
3170         dist_match_type,
3171         distribution_class,
3172         distribution_line_number,
3173         encumbered_flag,
3174         expenditure_item_date,
3175         expenditure_organization_id,
3176         expenditure_type,
3177         final_match_flag,
3178         global_attribute_category,
3179         global_attribute1,
3180         global_attribute10,
3181         global_attribute11,
3182         global_attribute12,
3183         global_attribute13,
3184         global_attribute14,
3185         global_attribute15,
3186         global_attribute16,
3187         global_attribute17,
3188         global_attribute18,
3189         global_attribute19,
3190         global_attribute2,
3191         global_attribute20,
3192         global_attribute3,
3193         global_attribute4,
3194         global_attribute5,
3195         global_attribute6,
3196         global_attribute7,
3197         global_attribute8,
3198         global_attribute9,
3199         income_tax_region,
3200         inventory_transfer_status,
3201         invoice_distribution_id,
3202         invoice_id,
3203         invoice_line_number,
3204         line_type_lookup_code,
3205         match_status_flag,
3206         matched_uom_lookup_code,
3207         merchant_document_number,
3208         merchant_name,
3209         merchant_reference,
3210         merchant_tax_reg_number,
3211         merchant_taxpayer_id,
3212         org_id,
3213         pa_addition_flag,
3214         pa_quantity,
3215         period_name,
3216         po_distribution_id,
3217         posted_flag,
3218         project_id,
3219         quantity_invoiced,
3220         rcv_transaction_id,
3221         NULL,                   --related_id,
3222         reversal_flag,
3223         rounding_amt,
3224         set_of_books_id,
3225         task_id,
3226         type_1099,
3227         unit_price,
3228         p_instruction_id,          --instruction_id
3229         NULL,                       --charge_applicable_dist_id
3230         INTENDED_USE,
3231         WITHHOLDING_TAX_CODE_ID,
3232         PROJECT_ACCOUNTING_CONTEXT,
3233         REQ_DISTRIBUTION_ID,
3234         REFERENCE_1,
3235         REFERENCE_2,
3236         NULL,                   -- line_group_number
3237         PA_CC_AR_INVOICE_ID,
3238         PA_CC_AR_INVOICE_LINE_NUM,
3239         PA_CC_PROCESSED_CODE,
3240 	pay_awt_group_id,    --bug6817107
3241 	/* Bug#10416960 */
3242        summary_tax_line_id,
3243        detail_tax_dist_id
3244 FROM   ap_invoice_distributions_all
3245 WHERE  invoice_id =  p_lines_rec.invoice_id
3246 AND    invoice_line_number = p_lines_rec.line_number
3247 AND    line_type_lookup_code IN ('ITEM', 'ACCRUAL');
3248 
3249 l_ppa_lines_rec          AP_RETRO_PRICING_PKG.invoice_lines_rec_type;
3250 l_ppa_invoice_dists_list AP_RETRO_PRICING_PKG.invoice_dists_list_type;
3251 l_item_dists_list        AP_RETRO_PRICING_PKG.invoice_dists_list_type;
3252 
3253 l_rows                     NUMBER;
3254 l_po_exchange_rate         NUMBER;
3255 l_rcv_exchange_rate        NUMBER;
3256 i                          INTEGER;
3257 l_dist_total               NUMBER;
3258 l_rounding_amount          NUMBER;
3259 l_rounding_dist            INTEGER;
3260 l_max_dist_amount          NUMBER;
3261 
3262 current_calling_sequence   VARCHAR2(1000);
3263 debug_info                 VARCHAR2(1000);
3264 Po_Price_Adj_Failure       EXCEPTION;
3265 l_api_name constant varchar2(200) := 'Create_Po_Price_Adjustments';
3266 
3267 BEGIN
3268    --
3269    current_calling_sequence :=
3270       'AP_RETRO_PRICING_PKG.Create_Po_Price_Adjustments'||P_Calling_Sequence;
3271 
3272     IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
3273        FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_RETRO_PRICING_PKG.Create_Po_Price_Adjustments(+)');
3274     END IF;
3275    --
3276    ---------------------------------------------------------------------------
3277     debug_info := 'Create_Po_Price_Adjustments Step 1. Compute Po Price Adj '
3278                   ||'Line';
3279    ---------------------------------------------------------------------------
3280    -- Compute PPA Line
3281    l_ppa_lines_rec              := p_lines_rec;
3282    l_ppa_lines_rec.invoice_id   := p_ppa_invoice_rec.invoice_id;
3283    l_ppa_lines_rec.line_number  := AP_RETRO_PRICING_UTIL_PKG.get_max_ppa_line_num(
3284                                                       p_ppa_invoice_rec.invoice_id) + 1;
3285    l_ppa_lines_rec.line_type_lookup_code := 'RETROITEM';
3286    l_ppa_lines_rec.requester_id := NVL(p_instruction_lines_rec.requester_id, p_lines_rec.requester_id);
3287    l_ppa_lines_rec.description  := NVL(p_instruction_lines_rec.description, p_lines_rec.description);
3288 
3289    l_ppa_lines_rec.default_dist_ccid    := NULL;
3290    l_ppa_lines_rec.generate_dists       := 'D';
3291    l_ppa_lines_rec.prorate_across_all_items := 'N';
3292 
3293    IF (p_instruction_lines_rec.accounting_date is NOT NULL) THEN
3294        l_ppa_lines_rec.accounting_date := AP_INVOICES_PKG.get_GL_date(
3295                                              p_instruction_lines_rec.accounting_date);
3296    ELSE
3297        l_ppa_lines_rec.accounting_date := AP_INVOICES_PKG.get_GL_date(SYSDATE);
3298    END IF;
3299 
3300    l_ppa_lines_rec.period_name := AP_INVOICES_PKG.get_period_name(
3301                                          l_ppa_lines_rec.accounting_date);
3302    l_ppa_lines_rec.deferred_acctg_flag := 'N';
3303 
3304    l_ppa_lines_rec.line_source  := 'PO PRICE ADJUSTMENT';
3305    l_ppa_lines_rec.match_type   := 'PO_PRICE_ADJUSTMENT';
3306 
3307    l_ppa_lines_rec.amount :=
3308             AP_UTILITIES_PKG.ap_round_currency(
3309                  p_lines_rec.quantity_invoiced*(p_instruction_lines_rec.unit_price - p_lines_rec.unit_price),
3310                  p_ppa_invoice_rec.invoice_currency_code);
3311 
3312    l_ppa_lines_rec.unit_price  := p_instruction_lines_rec.unit_price - p_lines_rec.unit_price;
3313 
3314    --
3315    l_ppa_lines_rec.discarded_flag  := 'N';
3316    l_ppa_lines_rec.cancelled_flag  := 'N';
3317    --
3318    l_ppa_lines_rec.corrected_inv_id := p_lines_rec.invoice_id;
3319    l_ppa_lines_rec.corrected_line_number := p_lines_rec.line_number;
3320    l_ppa_lines_rec.final_match_flag := 'N';
3321    --
3322    l_ppa_lines_rec.award_id   := NVL(p_instruction_lines_rec.award_id, p_lines_rec.award_id);
3323    l_ppa_lines_rec.created_by      := p_ppa_invoice_rec.created_by;
3324    l_ppa_lines_rec.instruction_id := p_instruction_id;
3325    l_ppa_lines_rec.adj_type        := 'PPA';
3326 
3327    debug_info := 'Insert the PPA Line in the Global Temp Table';
3328    IF (AP_RETRO_PRICING_UTIL_PKG.Create_Line(
3329            l_ppa_lines_rec,
3330            current_calling_sequence) <> TRUE) THEN
3331       --
3332       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
3333           AP_IMPORT_UTILITIES_PKG.Print(
3334           AP_IMPORT_INVOICES_PKG.g_debug_switch,
3335           'insert_rejections<- '||current_calling_sequence);
3336       END IF;
3337       --
3338       Raise Po_Price_Adj_Failure;
3339       --
3340    END IF;
3341 
3342    ----------------------------------------------------------------------------
3343    debug_info := 'Create_Po_Price_Adjustments Step 2. Open cursor item_dists';
3344    ----------------------------------------------------------------------------
3345    OPEN item_dists;
3346    FETCH item_dists
3347    BULK COLLECT INTO l_item_dists_list;
3348    CLOSE item_dists;
3349 
3350    -----------------------------------------------------------------------
3351    debug_info := 'Create_Po_Price_Adjustments Step 3. Compute PPA Dists';
3352    ------------------------------------------------------------------------
3353    FOR i IN 1..l_item_dists_list.COUNT
3354    LOOP
3355      --
3356      l_ppa_invoice_dists_list(i)   := l_item_dists_list(i);
3357 
3358      l_ppa_invoice_dists_list(i).invoice_id := l_ppa_lines_rec.invoice_id;
3359      l_ppa_invoice_dists_list(i).invoice_line_number := l_ppa_lines_rec.line_number;
3360            --Bug#10416960
3361      l_ppa_invoice_dists_list(i).invoice_distribution_id := AP_RETRO_PRICING_UTIL_PKG.get_invoice_distribution_id;
3362      --l_ppa_invoice_dists_list(i).invoice_distribution_id := Null;
3363      l_ppa_invoice_dists_list(i).distribution_line_number := i;
3364      /*select max(distribution_line_number) + 1
3365      into l_ppa_invoice_dists_list(i).distribution_line_number
3366      from ap_ppa_invoice_dists_gt
3367      where invoice_id = l_ppa_lines_rec.invoice_id
3368      and   invoice_line_number = l_ppa_lines_rec.line_number; */
3369      -- line_type_lookup_code = 'RETROITEM' for Price Corrections
3370      l_ppa_invoice_dists_list(i).line_type_lookup_code := AP_RETRO_PRICING_UTIL_PKG.get_dist_type_lookup_code(
3371                                                        l_item_dists_list(i).invoice_distribution_id);
3372      l_ppa_invoice_dists_list(i).dist_match_type     := 'PO_PRICE_ADJUSTMENT';
3373      l_ppa_invoice_dists_list(i).distribution_class := 'PERMANENT';
3374      l_ppa_invoice_dists_list(i).accounting_date := SYSDATE;
3375      l_ppa_invoice_dists_list(i).period_name := l_ppa_lines_rec.period_name;
3376      l_ppa_invoice_dists_list(i).accrual_posted_flag := 'N';
3377      l_ppa_invoice_dists_list(i).cash_posted_flag := 'N';
3378      l_ppa_invoice_dists_list(i).posted_flag := 'N';
3379      --
3380      --quantity_invoiced and corrected_quantity are same for Qty Corrections
3381      IF p_lines_rec.match_type = 'QTY_CORRECTION'  THEN
3382         l_ppa_invoice_dists_list(i).amount  :=
3383              AP_UTILITIES_PKG.ap_round_currency(
3384                 l_item_dists_list(i).corrected_quantity*(p_instruction_lines_rec.unit_price - p_lines_rec.unit_price),
3385                 p_ppa_invoice_rec.invoice_currency_code);
3386      ELSE
3387          l_ppa_invoice_dists_list(i).amount  :=
3388              AP_UTILITIES_PKG.ap_round_currency(
3389                 l_item_dists_list(i).quantity_invoiced*(p_instruction_lines_rec.unit_price - p_lines_rec.unit_price),
3390                 p_ppa_invoice_rec.invoice_currency_code);
3391 
3392      END IF;
3393      --
3394      l_ppa_invoice_dists_list(i).base_amount :=
3395                 AP_UTILITIES_PKG.ap_round_currency(
3396                      l_ppa_invoice_dists_list(i).amount*p_ppa_invoice_rec.exchange_rate,
3397                      p_base_currency_code);
3398 
3399      --l_ppa_invoice_dists_list(i).rounding_amount := NULL;  -not selected
3400      --
3401      l_ppa_invoice_dists_list(i).match_status_flag         := NULL;
3402      l_ppa_invoice_dists_list(i).encumbered_flag           := 'N';
3403      l_ppa_invoice_dists_list(i).reversal_flag             := 'N';
3404      l_ppa_invoice_dists_list(i).cancellation_flag         := 'N';
3405      l_ppa_invoice_dists_list(i).corrected_invoice_dist_id := l_item_dists_list(i).invoice_distribution_id;
3406      l_ppa_invoice_dists_list(i).corrected_quantity        := NVL(l_item_dists_list(i).corrected_quantity,
3407                                                                   l_item_dists_list(i).quantity_invoiced);
3408      l_ppa_invoice_dists_list(i).quantity_invoiced         := NULL;
3409 
3410      l_ppa_invoice_dists_list(i).unit_price := l_ppa_lines_rec.unit_price;
3411      --
3412      l_ppa_invoice_dists_list(i).final_match_flag      := 'N';
3413      l_ppa_invoice_dists_list(i).assets_addition_flag  := 'U';
3414 
3415      IF l_ppa_invoice_dists_list(i).assets_tracking_flag = 'Y' THEN
3416         l_ppa_invoice_dists_list(i).asset_book_type_code := p_lines_rec.asset_book_type_code;
3417         l_ppa_invoice_dists_list(i).asset_category_id    := p_lines_rec.asset_category_id;
3418      END IF;
3419      --
3420      IF l_ppa_invoice_dists_list(i).project_id IS NOT NULL THEN
3421         l_ppa_invoice_dists_list(i).pa_Addition_flag := 'N';
3422      ELSE
3423         l_ppa_invoice_dists_list(i).pa_Addition_flag := 'E';
3424      END IF;
3425      --
3426      l_ppa_invoice_dists_list(i).inventory_transfer_status := 'N';
3427      l_ppa_invoice_dists_list(i).created_by            := p_ppa_invoice_rec.created_by;
3428      l_ppa_invoice_dists_list(i).inventory_transfer_status := 'N';
3429      l_ppa_invoice_dists_list(i).created_by := l_ppa_lines_rec.created_by;
3430      --
3431      l_dist_total := l_dist_total +  l_ppa_invoice_dists_list(i).amount;
3432      --
3433    END LOOP;
3434 
3435    --If line_amount <> total_dist_line_amount
3436    --update MAX of the largest dist
3437    debug_info := 'Round max of the largest PPA Dist';
3438    IF (l_dist_total <> l_ppa_lines_rec.amount) THEN
3439      l_rounding_amount := l_ppa_lines_rec.amount -l_dist_total;
3440       FOR i IN 1..l_item_dists_list.COUNT
3441       LOOP
3442         IF i = 1 THEN
3443           l_max_dist_amount := l_ppa_invoice_dists_list(i).amount;
3444         END IF;
3445         IF l_item_dists_list(i).amount > l_max_dist_amount  THEN
3446            l_max_dist_amount := l_ppa_invoice_dists_list(i).amount;
3447                l_rounding_dist := i;
3448         END IF;
3449       END LOOP;
3450       --
3451       l_ppa_invoice_dists_list(l_rounding_dist).amount :=
3452                  l_rounding_amount + l_ppa_invoice_dists_list(i).amount;
3453       --
3454    END IF;
3455 
3456    ---------------------------------------------------------------------------
3457    debug_info := 'Create_Po_Price_Adjustments Step 4. Insert the PPA Dists in'
3458                  ||' the Global Temp Table';
3459    ---------------------------------------------------------------------------
3460    FORALL i IN 1..l_ppa_invoice_dists_list.COUNT
3461          INSERT INTO ap_ppa_invoice_dists_gt values  l_ppa_invoice_dists_list(i);
3462 
3463    -------------------------------------------------------------------------
3464    debug_info := 'Create_Po_Price_Adjustments Step 5. Clear PL/SQL tables';
3465    -------------------------------------------------------------------------
3466    l_ppa_invoice_dists_list.DELETE;
3467    l_item_dists_list.DELETE;
3468 
3469    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
3470       FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_RETRO_PRICING_PKG.Create_Po_Price_Adjustments(-)');
3471    END IF;
3472    --
3473    RETURN(TRUE);
3474    --
3475 EXCEPTION
3476  WHEN OTHERS THEN
3477     IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
3478       AP_IMPORT_UTILITIES_PKG.Print(AP_IMPORT_INVOICES_PKG.g_debug_switch,
3479                                     debug_info);
3480     END IF;
3481     --
3482     IF (SQLCODE < 0) then
3483       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
3484         AP_IMPORT_UTILITIES_PKG.Print(AP_IMPORT_INVOICES_PKG.g_debug_switch,
3485                                       SQLERRM);
3486       END IF;
3487     END IF;
3488     --
3489     IF ( item_dists%ISOPEN ) THEN
3490         CLOSE item_dists;
3491     END IF;
3492     --
3493     RETURN(FALSE);
3494 
3495 END Create_Po_Price_Adjustments;
3496 
3497 
3498 /*==============================================================================
3499  |  FUNCTION - Process_Retroprice_Adjustments()
3500  |
3501  |  DESCRIPTION
3502  |      This program is called from Import_Retroprice_Adjustments for every
3503  |      base matched invoice line that is a candidate for retropricing. The
3504  |      program has the following logic to populate the Global Temp tables:
3505  |      1.Reverse IPV and TIPV on the Base Matched Lines(as well as all Price
3506  |        Corrections and Quantity Corrections done on the base matched line)
3507  |        and popuate the Temp tables with Zero Amt Adjustment Correction Lines.
3508  |      2.Create PPA correction adjustment lines in the Temp Tables to reverse
3509  |         any outstanding price correction.
3510  |      3.Create PO price adjustment lines to record the delta in price in the
3511  |        Global Temporary Tables.
3512  |      4.If PPA already exists for the original base match line then Reverse
3513  |        all po price adjustment lines for the existing PPA.
3514  |
3515  |
3516  |  PARAMETERS
3517  |      p_base_currency_code
3518  |      p_base_match_lines_list
3519  |      p_instruction_rec
3520  |      p_instruction_lines_rec
3521  |      p_batch_id
3522  |      P_calling_sequence - Calling sequence
3523  |
3524  |  MODIFICATION HISTORY
3525  |  Date         Author             Description of Change
3526  |  29-JUL-2003  dgulraja           Creation
3527  |
3528  *============================================================================*/
3529 FUNCTION Process_Retroprice_Adjustments(
3530            p_base_currency_code    IN VARCHAR2,
3531            p_base_match_lines_list IN AP_RETRO_PRICING_PKG.invoice_lines_list_type,
3532            p_instruction_rec       IN AP_IMPORT_INVOICES_PKG.r_invoice_info_rec,
3533            p_instruction_lines_rec IN AP_RETRO_PRICING_PKG.instruction_lines_rec_type,
3534            p_batch_id              IN NUMBER,
3535            p_calling_sequence      IN VARCHAR2)
3536 RETURN BOOLEAN IS
3537 
3538 l_base_match_lines_rec    AP_RETRO_PRICING_PKG.invoice_lines_rec_type;
3539 l_pc_lines_list           AP_RETRO_PRICING_PKG.invoice_lines_list_type;
3540 l_pc_lines_rec            AP_RETRO_PRICING_PKG.invoice_lines_rec_type;
3541 l_qc_lines_list           AP_RETRO_PRICING_PKG.invoice_lines_list_type;
3542 l_qc_lines_rec            AP_RETRO_PRICING_PKG.invoice_lines_rec_type;
3543 l_adj_lines_rec           AP_RETRO_PRICING_PKG.invoice_lines_rec_type;
3544 
3545 l_tax_lines_list          AP_RETRO_PRICING_PKG.invoice_lines_list_type;
3546 l_ppa_invoice_rec         AP_RETRO_PRICING_PKG.invoice_rec_type;
3547 
3548 l_prev_invoice_id         NUMBER(15);
3549 l_existing_ppa_inv_id     NUMBER(15);
3550 l_ppa_exists              VARCHAR2(1);
3551 l_adj_corr_exists         VARCHAR2(1);
3552 l_pc_exists               VARCHAR2(1);
3553 l_qc_exists               VARCHAR2(1);
3554 l_ipv_dists_exist         VARCHAR2(1);
3555 l_erv_dists_exist         VARCHAR2(1);
3556 l_TIPV_exist              VARCHAR2(1);
3557 debug_info                VARCHAR2(1000);
3558 current_calling_sequence  VARCHAR2(1000);
3559 
3560 Process_Retro_Adj_failure EXCEPTION;
3561 l_api_name constant varchar2(200) := 'Process_Retroprice_Adjustments';
3562 
3563 BEGIN
3564   --
3565   current_calling_sequence :=
3566   'AP_RETRO_PRICING_PKG.Process_Retroprice_Adjustments<-'
3567   ||P_calling_sequence;
3568 
3569    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
3570       FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_RETRO_PRICING_PKG.Process_Retroprice_Adjustments(+)');
3571    END IF;
3572 
3573    debug_info := 'Inside the procedure Process_Retroprice Adjustments';
3574 
3575    IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
3576        AP_IMPORT_UTILITIES_PKG.Print(
3577          AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
3578    END IF;
3579 
3580   FOR i IN 1..p_base_match_lines_list.COUNT
3581   LOOP
3582     --
3583     l_base_match_lines_rec := p_base_match_lines_list(i);
3584 
3585     ------------------------------------------------------------------------------
3586     debug_info := 'Process Retroprice Adjustments Step 1. Insert Temp PPA Invoice l_base_match_lines_rec.invoice_id,l_prev_invoice_id  '||l_base_match_lines_rec.invoice_id||','||l_prev_invoice_id;
3587     IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
3588        AP_IMPORT_UTILITIES_PKG.Print(
3589            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
3590     END IF;
3591     ------------------------------------------------------------------------------
3592     --Bugfix:4281253
3593     IF  (l_base_match_lines_rec.invoice_id <> nvl(l_prev_invoice_id,0))
3594     THEN
3595       --
3596       IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
3597          AP_IMPORT_UTILITIES_PKG.Print(
3598          AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
3599       END IF;
3600       --
3601       debug_info := '8889999 l_base_match_lines_rec.amount is '||l_base_match_lines_rec.amount;
3602       IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
3603         AP_IMPORT_UTILITIES_PKG.Print(
3604           AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
3605       END IF;
3606 
3607 
3608       IF (AP_RETRO_PRICING_UTIL_PKG.Create_ppa_Invoice(
3609               p_instruction_rec.invoice_id,
3610               l_base_match_lines_rec.invoice_id,
3611               l_base_match_lines_rec.line_number,
3612               p_batch_id,
3613               l_ppa_invoice_rec,     --OUT
3614               current_calling_sequence) <> TRUE) THEN
3615 
3616          IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
3617              AP_IMPORT_UTILITIES_PKG.Print(
3618                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
3619                    'Create_ppa_Invoice<- '||current_calling_sequence);
3620          END IF;
3621          Raise Process_Retro_Adj_failure;
3622 
3623       END IF;
3624       --   Bug 5525506. Remove the following END IF below
3625       --
3626       --      l_prev_invoice_id :=  l_base_match_lines_rec.invoice_id;
3627       --
3628       --   END IF;  -- l_prev_invoice_id
3629 
3630       --------------------------------------------------------------------------
3631       debug_info := 'Process Retroprice Adjustments Step 2. Check if '
3632                   ||'ppa_already_exists';
3633       IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
3634          AP_IMPORT_UTILITIES_PKG.Print(
3635            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
3636       END IF;
3637       --------------------------------------------------------------------------
3638       IF (AP_RETRO_PRICING_UTIL_PKG.ppa_already_exists(
3639           l_base_match_lines_rec.invoice_id,
3640           l_base_match_lines_rec.line_number,
3641           l_ppa_exists,         --OUT
3642           l_existing_ppa_inv_id --OUT
3643           ) <> TRUE) THEN
3644 
3645         IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
3646            AP_IMPORT_UTILITIES_PKG.Print(
3647                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
3648                    'ppa_already_exists<- '||current_calling_sequence);
3649         END IF;
3650         Raise Process_Retro_Adj_failure;
3651       END IF;
3652 
3653       debug_info := 'Existing PPA Invoice Id: '||l_existing_ppa_inv_id;
3654       IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
3655          AP_IMPORT_UTILITIES_PKG.Print(
3656            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
3657       END IF;
3658 
3659 
3660       -- PPA Docs have two types of lines --
3661       -- 1. Adjustment Correction: To reverse any outstanding price correction.
3662       -- 2. PO Price Adjustment: To record the delta in price in the Global
3663       --    Temporary Tables.
3664       -- The Reversal Process should not reverse the lines associated with Price
3665       -- Corrections on the PPA.
3666 
3667       ----------------------------------------------------------------------------
3668       debug_info := 'Process Retroprice Adjustments Step 3. Reverse_Existing_Ppa';
3669       IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
3670          AP_IMPORT_UTILITIES_PKG.Print(
3671            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
3672       END IF;
3673       ----------------------------------------------------------------------------
3674       IF (l_ppa_exists = 'Y')  THEN
3675        --  l_base_match_lines_rec.invoice_id <> l_prev_invoice_id) THEN
3676         debug_info := 'PPA exists for this Invoice ';
3677         IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
3678            AP_IMPORT_UTILITIES_PKG.Print(
3679            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
3680         END IF;
3681         IF  (Reverse_Existing_Ppa(
3682                 p_instruction_rec.invoice_id,
3683                 l_ppa_invoice_rec,
3684                 p_instruction_lines_rec,
3685                 l_existing_ppa_inv_id,
3686                 current_calling_sequence) <> TRUE) THEN
3687           --
3688           IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
3689                 AP_IMPORT_UTILITIES_PKG.Print(
3690                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
3691                    'Reverse_Existing_Ppa<- '||current_calling_sequence);
3692           END IF;
3693           --
3694           Raise Process_Retro_Adj_failure;
3695           --
3696         END IF; --Compute Ppa_reversal
3697       END IF; --l_ppa_exists
3698 
3699       --
3700       l_prev_invoice_id :=  l_base_match_lines_rec.invoice_id;
3701       --
3702     END IF;  -- l_prev_invoice_id. Bug 5525506
3703 
3704    ----------------------------------------------------------------------------
3705     debug_info := 'Process Retroprice Adjustments Step 4. Check if IPV Dists'
3706                   ||' Exists';
3707     IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
3708        AP_IMPORT_UTILITIES_PKG.Print(
3709            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
3710     END IF;
3711    ----------------------------------------------------------------------------
3712     -- If this base matched line has already been Adjusted by a line
3713     -- then no adjustments are required on this line. However adjustments
3714     -- may be required on the PC or QC for this base matched line
3715     --
3716     IF (AP_RETRO_PRICING_UTIL_PKG.Ipv_Dists_exists(
3717           l_base_match_lines_rec.invoice_id,
3718           l_base_match_lines_rec.line_number,
3719           l_ipv_dists_exist         --OUT
3720           ) <> TRUE) THEN
3721       --
3722       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
3723                 AP_IMPORT_UTILITIES_PKG.Print(
3724                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
3725                    'Ipv_Dists_exists<- '||current_calling_sequence);
3726       END IF;
3727       --
3728       Raise Process_Retro_Adj_failure;
3729     END IF;
3730     --
3731    ------------------------------------------------------------------------
3732     debug_info := 'Process Retroprice Adjustments Step 5. Check if Adj Corr'
3733                   ||' Exists';
3734     IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
3735        AP_IMPORT_UTILITIES_PKG.Print(
3736            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
3737     END IF;
3738    -------------------------------------------------------------------------
3739     IF (AP_RETRO_PRICING_UTIL_PKG.Adj_Corr_Exists(
3740                l_base_match_lines_rec.invoice_id,
3741                l_base_match_lines_rec.line_number,
3742                l_adj_corr_exists) <> TRUE) THEN
3743         --
3744          IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
3745                 AP_IMPORT_UTILITIES_PKG.Print(
3746                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
3747                    'Adj_Corr_Exists<- '||current_calling_sequence);
3748          END IF;
3749          Raise Process_Retro_Adj_failure;
3750     END IF;
3751     --
3752     IF  (l_ipv_dists_exist = 'Y') AND ( l_adj_corr_exists = 'N') THEN
3753     --
3754         -----------------------------------------------------------------------
3755         debug_info := 'Process Retroprice Adjustments Step 6. '
3756                        ||'Reverse_Redistribute_IPV for the base matched line';
3757         IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
3758           AP_IMPORT_UTILITIES_PKG.Print(
3759            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
3760         END IF;
3761         -----------------------------------------------------------------------
3762         --
3763         IF (Reverse_Redistribute_IPV(
3764                          l_ppa_invoice_rec,
3765                          p_base_currency_code,
3766                          p_instruction_rec.invoice_id,
3767                          p_instruction_rec.created_by,
3768                          l_base_match_lines_rec,
3769                          l_erv_dists_exist,
3770                          current_calling_sequence) <> TRUE) THEN
3771           --
3772           IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
3773                 AP_IMPORT_UTILITIES_PKG.Print(
3774                 AP_IMPORT_INVOICES_PKG.g_debug_switch,
3775                 'Reverse_Redistribute_IPV<- '||current_calling_sequence);
3776           END IF;
3777           --
3778           Raise Process_Retro_Adj_failure;
3779           --
3780         END IF;
3781         --
3782 
3783         ----------------------------------------------------------------------
3784         debug_info := 'Process Retroprice Adjustments Step 7. '||
3785                       'Check if TIPV Dists Exists for the base matched Line';
3786         IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
3787           AP_IMPORT_UTILITIES_PKG.Print(
3788            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
3789         END IF;
3790         ----------------------------------------------------------------------
3791         IF (AP_RETRO_PRICING_UTIL_PKG.Tipv_Exists(
3792               l_base_match_lines_rec.invoice_id,
3793               l_base_match_lines_rec.line_number,
3794               l_tax_lines_list,
3795               l_tipv_exist) <> TRUE) THEN
3796            --
3797            IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
3798                AP_IMPORT_UTILITIES_PKG.Print(
3799                AP_IMPORT_INVOICES_PKG.g_debug_switch,
3800               'Tipv_Exists<- '||current_calling_sequence);
3801            END IF;
3802            --
3803            Raise Process_Retro_Adj_failure;
3804            --
3805         END IF;
3806         --
3807         IF  l_tipv_exist = 'Y' THEN
3808             --
3809             -----------------------------------------------------------------
3810             debug_info := 'Process Retroprice Adjustments Step 8. '
3811               ||'Process_TIPV_Reversal for the base matched line';
3812             IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
3813                AP_IMPORT_UTILITIES_PKG.Print(
3814                  AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
3815             END IF;
3816             -----------------------------------------------------------------
3817             IF (Process_TIPV_Reversal(
3818                              l_ppa_invoice_rec,
3819                              p_base_currency_code,
3820                              p_instruction_rec.invoice_id,
3821                              p_instruction_rec.created_by,
3822                              l_base_match_lines_rec,
3823                              l_tax_lines_list,
3824                              current_calling_sequence) <> TRUE) THEN
3825               --
3826               IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
3827                     AP_IMPORT_UTILITIES_PKG.Print(
3828                     AP_IMPORT_INVOICES_PKG.g_debug_switch,
3829                     'Process_TIPV_Reversal<- '||current_calling_sequence);
3830               END IF;
3831               --
3832               Raise Process_Retro_Adj_failure;
3833               --
3834             END IF;
3835             --
3836         END IF;
3837 
3838     END IF;  --ipv dists and l_adj_corr
3839 
3840     -- Create PPA Line even if the IPV's don't exist for the Base Match Line
3841     --------------------------------------------------------------------------
3842     debug_info := 'Process Retroprice Adjustments Step 9. '
3843                    ||'Create_Po_Price_Adjustments for the base matched line';
3844     IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
3845        AP_IMPORT_UTILITIES_PKG.Print(
3846            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
3847     END IF;
3848     --------------------------------------------------------------------------
3849     --
3850     -- Bug 5469166. Not Calling Create_Po_Price_Adjustment id the wash scenario
3851     IF (p_instruction_lines_rec.unit_price <> l_base_match_lines_rec.unit_price) THEN
3852 
3853       IF (Create_Po_Price_Adjustments(
3854              p_base_currency_code,
3855              p_instruction_rec.invoice_id,
3856              l_ppa_invoice_rec,
3857              p_instruction_lines_rec,
3858              l_base_match_lines_rec,
3859              current_calling_sequence) <> TRUE) THEN
3860         --
3861          IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
3862             AP_IMPORT_UTILITIES_PKG.Print(
3863               AP_IMPORT_INVOICES_PKG.g_debug_switch,
3864              'Create_Po_Price_Adjustments<- '||current_calling_sequence);
3865          END IF;
3866        --
3867          Raise Process_Retro_Adj_failure;
3868        --
3869       END IF;
3870 
3871     END IF;
3872 
3873     -- Price Corrections
3874     -------------------------------------------------------------------------
3875     debug_info := 'Process Retroprice Adjustments Step 10. IF PC Exists';
3876     -------------------------------------------------------------------------
3877     IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
3878          AP_IMPORT_UTILITIES_PKG.Print(
3879          AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
3880     END IF;
3881     --
3882     IF (AP_RETRO_PRICING_UTIL_PKG.Corrections_exists(
3883               l_base_match_lines_rec.invoice_id,
3884               l_base_match_lines_rec.line_number,
3885                'PRICE_CORRECTION',  --Modified spelling for bug#9573078
3886               l_pc_lines_list,
3887               l_pc_exists) <> TRUE) THEN
3888        --
3889        IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
3890            AP_IMPORT_UTILITIES_PKG.Print(
3891              AP_IMPORT_INVOICES_PKG.g_debug_switch,
3892              'Corrections_exists<- '||current_calling_sequence);
3893        END IF;
3894        --
3895        Raise Process_Retro_Adj_failure;
3896        --
3897     END IF;
3898 
3899     IF  (l_pc_exists = 'Y') THEN
3900       --
3901       FOR i in 1..l_pc_lines_list.COUNT
3902       LOOP
3903          --
3904          l_pc_lines_rec :=  l_pc_lines_list(i);
3905          --
3906          -- IPV Dists always exist for Price Corrections
3907          ---------------------------------------------------------------------
3908          debug_info := 'Process Retroprice Adjustments Step 11. Check if IPV '
3909                         ||'Dists Exist for PC';
3910          IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
3911            AP_IMPORT_UTILITIES_PKG.Print(
3912              AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
3913          END IF;
3914          ---------------------------------------------------------------------
3915          IF (AP_RETRO_PRICING_UTIL_PKG.Ipv_Dists_exists(
3916                 l_pc_lines_rec.invoice_id,
3917                 l_pc_lines_rec.line_number,
3918                 l_ipv_dists_exist) <> TRUE) THEN
3919             --
3920             IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
3921                 AP_IMPORT_UTILITIES_PKG.Print(
3922                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
3923                    'Ipv_Dists_exists<- '||current_calling_sequence);
3924             END IF;
3925             Raise Process_Retro_Adj_failure;
3926          END IF;
3927          --
3928          ---------------------------------------------------------------------
3929          debug_info := 'Process Retroprice Adjustments Step 12. Check if Adj '
3930                        ||'Corr Exists for PC';
3931          IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
3932            AP_IMPORT_UTILITIES_PKG.Print(
3933             AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
3934          END IF;
3935          ---------------------------------------------------------------------
3936          IF (AP_RETRO_PRICING_UTIL_PKG.Adj_Corr_Exists(
3937                l_pc_lines_rec.invoice_id,
3938                l_pc_lines_rec.line_number,
3939                l_adj_corr_exists) <> TRUE) THEN
3940             --
3941             IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
3942                 AP_IMPORT_UTILITIES_PKG.Print(
3943                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
3944                    'Adj_Corr_Exists<- '||current_calling_sequence);
3945             END IF;
3946            Raise Process_Retro_Adj_failure;
3947          END IF;
3948 
3949          IF  (l_ipv_dists_exist = 'Y') AND ( l_adj_corr_exists = 'N') THEN
3950            --
3951            ------------------------------------------------------------------
3952            debug_info := 'Process Retroprice Adjustments Step 13. '
3953                        ||'Reverse_Redistribute_IPV for the PC line';
3954            IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
3955              AP_IMPORT_UTILITIES_PKG.Print(
3956                AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
3957            END IF;
3958            ------------------------------------------------------------------
3959            IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
3960              AP_IMPORT_UTILITIES_PKG.Print(
3961              AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
3962            END IF;
3963            --
3964            IF (Reverse_Redistribute_IPV(
3965                          l_ppa_invoice_rec,
3966                          p_base_currency_code,
3967                          p_instruction_rec.invoice_id,
3968                          p_instruction_rec.created_by,
3969                          l_pc_lines_rec,
3970                          l_erv_dists_exist,
3971                          current_calling_sequence) <> TRUE) THEN
3972               --
3973               IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
3974                   AP_IMPORT_UTILITIES_PKG.Print(
3975                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
3976                    'Reverse_Redistribute_IPV<- '||current_calling_sequence);
3977               END IF;
3978               --
3979               Raise Process_Retro_Adj_failure;
3980               --
3981            END IF;
3982            --
3983            -------------------------------------------------------------------
3984            debug_info := 'Process Retroprice Adjustments Step 14. '||
3985                          'Check if TIPV Dists Exists for the PC Line';
3986            IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
3987              AP_IMPORT_UTILITIES_PKG.Print(
3988                AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
3989            END IF;
3990            -------------------------------------------------------------------
3991            IF (AP_RETRO_PRICING_UTIL_PKG.Tipv_Exists(
3992                   l_pc_lines_rec.invoice_id,
3993                   l_pc_lines_rec.line_number,
3994                   l_tax_lines_list,
3995                   l_tipv_exist) <> TRUE) THEN
3996                --
3997                IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
3998                    AP_IMPORT_UTILITIES_PKG.Print(
3999                    AP_IMPORT_INVOICES_PKG.g_debug_switch,
4000                   'Tipv_Exists<- '||current_calling_sequence);
4001                END IF;
4002                --
4003                Raise Process_Retro_Adj_failure;
4004                --
4005            END IF;
4006            --
4007            IF  l_tipv_exist = 'Y' THEN
4008                 --
4009                 --------------------------------------------------------------
4010                 debug_info := 'Process Retroprice Adjustments Step 15. '
4011                              ||'Process_TIPV_Reversal for the PC line';
4012                 IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
4013                    AP_IMPORT_UTILITIES_PKG.Print(
4014                       AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
4015                 END IF;
4016                 --------------------------------------------------------------
4017                 IF (Process_TIPV_Reversal(
4018                                  l_ppa_invoice_rec,
4019                                  p_base_currency_code,
4020                                  p_instruction_rec.invoice_id,
4021                                  p_instruction_rec.created_by,
4022                                  l_pc_lines_rec,
4023                                  l_tax_lines_list,
4024                                  current_calling_sequence) <> TRUE) THEN
4025                   --
4026                   IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
4027                         AP_IMPORT_UTILITIES_PKG.Print(
4028                         AP_IMPORT_INVOICES_PKG.g_debug_switch,
4029                         'Process_TIPV_Reversal<- '||current_calling_sequence);
4030                   END IF;
4031                   --
4032                   Raise Process_Retro_Adj_failure;
4033                   --
4034                 END IF;
4035                 --
4036            END IF;
4037 
4038 
4039             -- PPA Line should only be created for a PC Line if the
4040             -- if the PC  Lines have not been adjustment corrected.
4041             -- NOTE : PC's always have IPV Dists
4042             -----------------------------------------------------------------
4043             debug_info := 'Process Retroprice Adjustments Step 16. '
4044                            ||'Create_Po_Price_Adjustments for the PC line';
4045             ------------------------------------------------------------------
4046             IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
4047                AP_IMPORT_UTILITIES_PKG.Print(
4048                AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
4049             END IF;
4050             --
4051 
4052          END IF;  --ipv dists and l_adj_corr
4053          --
4054        END LOOP;  --PC Loop
4055        --
4056     END IF; -- If PC Exists
4057 
4058     -- Quantity Corrections
4059     ----------------------------------------------------------------------
4060     debug_info := 'Process Retroprice Adjustments Step 17. IF QC Exists';
4061     ----------------------------------------------------------------------
4062     IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
4063          AP_IMPORT_UTILITIES_PKG.Print(
4064          AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
4065     END IF;
4066     --
4067     IF (AP_RETRO_PRICING_UTIL_PKG.Corrections_exists(
4068               l_base_match_lines_rec.invoice_id,
4069               l_base_match_lines_rec.line_number,
4070               'QTY_CORRECTION',  --Modified spelling for bug#9573078
4071               l_qc_lines_list,
4072               l_qc_exists) <> TRUE) THEN
4073        --
4074        IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
4075            AP_IMPORT_UTILITIES_PKG.Print(
4076              AP_IMPORT_INVOICES_PKG.g_debug_switch,
4077              'Corrections_exists<- '||current_calling_sequence);
4078        END IF;
4079        --
4080        Raise Process_Retro_Adj_failure;
4081        --
4082     END IF;
4083     --
4084     IF  (l_qc_exists = 'Y') THEN
4085       --
4086       FOR i in 1..l_qc_lines_list.COUNT
4087       LOOP
4088          --
4089          l_qc_lines_rec :=  l_qc_lines_list(i);
4090          --
4091          ---------------------------------------------------------------------
4092          debug_info := 'Process Retroprice Adjustments Step 18. Check if IPV '
4093                         ||'Dists Exist for QC';
4094          ---------------------------------------------------------------------
4095          IF (AP_RETRO_PRICING_UTIL_PKG.Ipv_Dists_exists(
4096                 l_qc_lines_rec.invoice_id,
4097                 l_qc_lines_rec.line_number,
4098                 l_ipv_dists_exist) <> TRUE) THEN
4099             --
4100             IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
4101                 AP_IMPORT_UTILITIES_PKG.Print(
4102                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
4103                    'Ipv_Dists_exists<- '||current_calling_sequence);
4104             END IF;
4105             Raise Process_Retro_Adj_failure;
4106          END IF;
4107          --
4108          -------------------------------------------------------------------------
4109          debug_info := 'Process Retroprice Adjustments Step 19. Check if Adj '
4110                         ||'Corr Exists for QC';
4111          -------------------------------------------------------------------------
4112          IF (AP_RETRO_PRICING_UTIL_PKG.Adj_Corr_Exists(
4113                l_qc_lines_rec.invoice_id,
4114                l_qc_lines_rec.line_number,
4115                l_adj_corr_exists) <> TRUE) THEN
4116             --
4117             IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
4118                 AP_IMPORT_UTILITIES_PKG.Print(
4119                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
4120                    'Adj_Corr_Exists<- '||current_calling_sequence);
4121             END IF;
4122            Raise Process_Retro_Adj_failure;
4123          END IF;
4124          --
4125          IF  (l_ipv_dists_exist = 'Y') AND ( l_adj_corr_exists = 'N') THEN
4126            --
4127            --------------------------------------------------------------
4128            debug_info := 'Process Retroprice Adjustments Step 20. '
4129                           ||'Reverse_Redistribute_IPV for the QC line';
4130            ----------------------------------------------------------------
4131            IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
4132              AP_IMPORT_UTILITIES_PKG.Print(
4133              AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
4134            END IF;
4135            --
4136            IF (Reverse_Redistribute_IPV(
4137                      l_ppa_invoice_rec,
4138                      p_base_currency_code,
4139                      p_instruction_rec.invoice_id,
4140                      p_instruction_rec.created_by,
4141                      l_qc_lines_rec,
4142                      l_erv_dists_exist,
4143                      current_calling_sequence) <> TRUE) THEN
4144               --
4145               IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
4146                   AP_IMPORT_UTILITIES_PKG.Print(
4147                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
4148                    'Reverse_Redistribute_IPV<- '||current_calling_sequence);
4149               END IF;
4150               --
4151               Raise Process_Retro_Adj_failure;
4152               --
4153            END IF;
4154            --
4155 
4156           --------------------------------------------------------------------
4157            debug_info := 'Process Retroprice Adjustments Step 21. '||
4158                           'Check if TIPV Dists Exists for the QC Line';
4159            -------------------------------------------------------------------
4160            IF (AP_RETRO_PRICING_UTIL_PKG.Tipv_Exists(
4161                   l_qc_lines_rec.invoice_id,
4162                   l_qc_lines_rec.line_number,
4163                   l_tax_lines_list,
4164                   l_tipv_exist) <> TRUE) THEN
4165                --
4166                IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
4167                    AP_IMPORT_UTILITIES_PKG.Print(
4168                    AP_IMPORT_INVOICES_PKG.g_debug_switch,
4169                   'Tipv_Exists<- '||current_calling_sequence);
4170                END IF;
4171                --
4172                Raise Process_Retro_Adj_failure;
4173                --
4174            END IF;
4175            --
4176            IF  l_tipv_exist = 'Y' THEN
4177                 --
4178                 --------------------------------------------------------------
4179                 debug_info := 'Process Retroprice Adjustments Step 22. '||
4180                                'Process_TIPV_Reversal for the PC line';
4181                 --------------------------------------------------------------
4182                 IF (Process_TIPV_Reversal(
4183                                  l_ppa_invoice_rec,
4184                                  p_base_currency_code,
4185                                  p_instruction_rec.invoice_id,
4186                                  p_instruction_rec.created_by,
4187                                  l_qc_lines_rec,
4188                                  l_tax_lines_list,
4189                                  current_calling_sequence) <> TRUE) THEN
4190                   --
4191                   IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
4192                         AP_IMPORT_UTILITIES_PKG.Print(
4193                         AP_IMPORT_INVOICES_PKG.g_debug_switch,
4194                         'Process_TIPV_Reversal<- '||current_calling_sequence);
4195                   END IF;
4196                   --
4197                   Raise Process_Retro_Adj_failure;
4198                   --
4199                 END IF;
4200                 --
4201            END IF;
4202 
4203 
4204          END IF; --l_ipv_dists_Exist and l_adj_corr_Exists
4205 
4206          -----------------------------------------------------------------
4207             debug_info := 'Process Retroprice Adjustments Step 23. '
4208                    ||'Create_Po_Price_Adjustments for the QC line';
4209          ------------------------------------------------------------------
4210          IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
4211                AP_IMPORT_UTILITIES_PKG.Print(
4212                AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
4213          END IF;
4214          --
4215 
4216 	 --Introduced below IF clause for bug#9573078
4217 	 IF (p_instruction_lines_rec.unit_price <> l_qc_lines_rec.unit_price) THEN
4218          IF (Create_Po_Price_Adjustments(
4219                    p_base_currency_code,
4220                    p_instruction_rec.invoice_id,
4221                    l_ppa_invoice_rec,
4222                    p_instruction_lines_rec,
4223                    l_qc_lines_rec,
4224                    current_calling_sequence) <> TRUE) THEN
4225             --
4226             IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
4227                    AP_IMPORT_UTILITIES_PKG.Print(
4228                    AP_IMPORT_INVOICES_PKG.g_debug_switch,
4229                    'Create_Po_Price_Adjustments<- '||current_calling_sequence);
4230             END IF;
4231             --
4232             Raise Process_Retro_Adj_failure;
4233          END IF;
4234 	  END IF; --unit price check
4235          --
4236       END LOOP;  --QC loop
4237       --
4238     END IF; -- If QC Exists
4239 
4240  END LOOP; --Base Match Line List
4241 
4242   -------------------------------------------------------------------------
4243    debug_info := 'Process Retroprice Adjustments Step 24 Clear PL/SQL tables';
4244    -------------------------------------------------------------------------
4245    l_pc_lines_list.DELETE;
4246    l_qc_lines_list.DELETE;
4247    l_tax_lines_list.DELETE;
4248 
4249    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
4250      FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_RETRO_PRICING_PKG.Process_Retroprice_Adjustments(-)');
4251    END IF;
4252    --
4253    RETURN(TRUE);
4254    --
4255 EXCEPTION
4256  WHEN OTHERS THEN
4257     IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
4258       AP_IMPORT_UTILITIES_PKG.Print(
4259         AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
4260     END IF;
4261 
4262     IF (SQLCODE < 0) then
4263       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
4264         AP_IMPORT_UTILITIES_PKG.Print(AP_IMPORT_INVOICES_PKG.g_debug_switch,SQLERRM);
4265       END IF;
4266     END IF;
4267     RETURN(FALSE);
4268 
4269 END Process_Retroprice_Adjustments;
4270 
4271 
4272 /*=============================================================================
4273  |  FUNCTION - Insert_Zero_Amt_Adjustments()
4274  |
4275  |  DESCRIPTION
4276  |       This function creates Zero Amount RetroItem and RetoTax lines on all
4277  |  the original invoices that need retro adjustment for a vendor. Furthermore
4278  |  this function reverses and redistributes all outstanding IPV and TIPV
4279  |  distributions
4280  |
4281  |
4282  |  PARAMETERS
4283  |      p_base_currency_code
4284  |      p_base_match_lines_list
4285  |      p_instruction_rec
4286  |      p_instruction_lines_rec
4287  |      p_batch_id
4288  |      P_calling_sequence - Calling sequence
4289  |
4290  |  MODIFICATION HISTORY
4291  |  Date         Author             Description of Change
4292  |  29-JUL-2003  dgulraja           Creation
4293  |  Bug 5353893 -- Added NVL to the WHO Columns so that in cases
4294  |                 when it is null we will use the "Standalone Batch
4295  |                 Process" as the possible user.
4296  |
4297  *============================================================================*/
4298 FUNCTION Insert_Zero_Amt_Adjustments(
4299             -- p_instruction_id         IN NUMBER, --Commented for bug#9573078
4300 	     p_invoice_id            IN NUMBER,  --bug#9573078
4301              p_line_number           IN NUMBER,  --bug#9573078
4302 	      --Bug#15996840
4303 	     p_base_match_invoice_id   IN NUMBER,
4304              p_base_match_line_number  IN NUMBER,
4305              p_calling_sequence        IN VARCHAR2)
4306 RETURN BOOLEAN IS
4307 
4308 current_calling_sequence    VARCHAR2(1000);
4309 debug_info                  VARCHAR2(1000);
4310 
4311 BEGIN
4312    --
4313    current_calling_sequence :=
4314     'AP_RETRO_PRICING_PKG.Insert_Zero_Amt_Adjustments<-'
4315     ||P_calling_sequence;
4316 
4317    ---------------------------------------------------------------------------
4318    debug_info := 'Insert_Zero_Amt_Adjustments Step 1. Insert into '
4319                  ||'AP_INVOICE_LINES_ALL';
4320    ---------------------------------------------------------------------------
4321 
4322    INSERT INTO AP_INVOICE_LINES_ALL(
4323                     invoice_id,
4324                     line_number,
4325                     line_type_lookup_code,
4326                     requester_id,
4327                     description,
4328                     line_source,
4329                     org_id,
4330                     inventory_item_id,
4331                     item_description,
4332                     serial_number,
4333                     manufacturer,
4334                     model_number,
4335                     generate_dists,
4336                     match_type,
4337                     default_dist_ccid,
4338                     prorate_across_all_items,
4339                     accounting_date,
4340                     period_name,
4341                     deferred_acctg_flag,
4342                     set_of_books_id,
4343                     amount,
4344                     base_amount,
4345                     rounding_amt,
4346                     quantity_invoiced,
4347                     unit_meas_lookup_code,
4348                     unit_price,
4349                     discarded_flag,
4350                     cancelled_flag,
4351                     income_tax_region,
4352                     type_1099,
4353                     corrected_inv_id,
4354                     corrected_line_number,
4355                     po_header_id,
4356                     po_line_id,
4357                     po_release_id,
4358                     po_line_location_id,
4359                     po_distribution_id,
4360                     rcv_transaction_id,
4361                     final_match_flag,
4362                     assets_tracking_flag,
4363                     asset_book_type_code,
4364                     asset_category_id,
4365                     project_id,
4366                     task_id,
4367                     expenditure_type,
4368                     expenditure_item_date,
4369                     expenditure_organization_id,
4370                     award_id,
4371                     awt_group_id,
4372 		    pay_awt_group_id,--bug6817107
4373                     receipt_verified_flag,
4374                     receipt_required_flag,
4375                     receipt_missing_flag,
4376                     justification,
4377                     expense_group,
4378                     start_expense_date,
4379                     end_expense_date,
4380                     receipt_currency_code,
4381                     receipt_conversion_rate,
4382                     receipt_currency_amount,
4383                     daily_amount,
4384                     web_parameter_id,
4385                     adjustment_reason,
4386                     merchant_document_number,
4387                     merchant_name,
4388                     merchant_reference,
4389                     merchant_tax_reg_number,
4390                     merchant_taxpayer_id,
4391                     country_of_supply,
4392                     credit_card_trx_id,
4393                     company_prepaid_invoice_id,
4394                     cc_reversal_flag,
4395                     creation_date,
4396                     created_by,
4397                     attribute_category,
4398                     attribute1,
4399                     attribute2,
4400                     attribute3,
4401                     attribute4,
4402                     attribute5,
4403                     attribute6,
4404                     attribute7,
4405                     attribute8,
4406                     attribute9,
4407                     attribute10,
4408                     attribute11,
4409                     attribute12,
4410                     attribute13,
4411                     attribute14,
4412                     attribute15,
4413                     global_attribute_category,
4414                     global_attribute1,
4415                     global_attribute2,
4416                     global_attribute3,
4417                     global_attribute4,
4418                     global_attribute5,
4419                     global_attribute6,
4420                     global_attribute7,
4421                     global_attribute8,
4422                     global_attribute9,
4423                     global_attribute10,
4424                     global_attribute11,
4425                     global_attribute12,
4426                     global_attribute13,
4427                     global_attribute14,
4428                     global_attribute15,
4429                     global_attribute16,
4430                     global_attribute17,
4431                     global_attribute18,
4432                     global_attribute19,
4433                     global_attribute20,
4434                     primary_intended_use,
4435                     ship_to_location_id,
4436                     product_type,
4437                     product_category,
4438                     product_fisc_classification,
4439                     user_defined_fisc_class,
4440                     trx_business_category,
4441                     summary_tax_line_id,
4442                     tax_regime_code,
4443                     tax,
4444                     tax_jurisdiction_code,
4445                     tax_status_code,
4446                     tax_rate_id,
4447                     tax_rate_code,
4448                     tax_rate,
4449                     wfapproval_status,
4450                     pa_quantity,
4451                     last_updated_by,
4452                     last_update_date)
4453             SELECT  invoice_id,
4454                     line_number,
4455                     line_type_lookup_code,
4456                     requester_id,
4457                     description,
4458                     line_source,
4459                     org_id,
4460                     inventory_item_id,
4461                     item_description,
4462                     serial_number,
4463                     manufacturer,
4464                     model_number,
4465                     generate_dists,
4466                     match_type,
4467                     default_dist_ccid,
4468                     prorate_across_all_items,
4469                     accounting_date,
4470                     period_name,
4471                     deferred_acctg_flag,
4472                     set_of_books_id,
4473                     amount,
4474                     base_amount,
4475                     rounding_amt,
4476                     quantity_invoiced,
4477                     unit_meas_lookup_code,
4478                     unit_price,
4479                     discarded_flag,
4480                     cancelled_flag,
4481                     income_tax_region,
4482                     type_1099,
4483                     corrected_inv_id,
4484                     corrected_line_number,
4485                     po_header_id,
4486                     po_line_id,
4487                     po_release_id,
4488                     po_line_location_id,
4489                     po_distribution_id,
4490                     rcv_transaction_id,
4491                     final_match_flag,
4492                     assets_tracking_flag,
4493                     asset_book_type_code,
4494                     asset_category_id,
4495                     project_id,
4496                     task_id,
4497                     expenditure_type,
4498                     expenditure_item_date,
4499                     expenditure_organization_id,
4500                     award_id,
4501                     awt_group_id,
4502 		    pay_awt_group_id,--bug6817107
4503                     receipt_verified_flag,
4504                     receipt_required_flag,
4505                     receipt_missing_flag,
4506                     justification,
4507                     expense_group,
4508                     start_expense_date,
4509                     end_expense_date,
4510                     receipt_currency_code,
4511                     receipt_conversion_rate,
4512                     receipt_currency_amount,
4513                     daily_amount,
4514                     web_parameter_id,
4515                     adjustment_reason,
4516                     merchant_document_number,
4517                     merchant_name,
4518                     merchant_reference,
4519                     merchant_tax_reg_number,
4520                     merchant_taxpayer_id,
4521                     country_of_supply,
4522                     credit_card_trx_id,
4523                     company_prepaid_invoice_id,
4524                     cc_reversal_flag,
4525                     nvl(creation_date,sysdate),
4526                     nvl(created_by,5),
4527                     attribute_category,
4528                     attribute1,
4529                     attribute2,
4530                     attribute3,
4531                     attribute4,
4532                     attribute5,
4533                     attribute6,
4534                     attribute7,
4535                     attribute8,
4536                     attribute9,
4537                     attribute10,
4538                     attribute11,
4539                     attribute12,
4540                     attribute13,
4541                     attribute14,
4542                     attribute15,
4543                     global_attribute_category,
4544                     global_attribute1,
4545                     global_attribute2,
4546                     global_attribute3,
4547                     global_attribute4,
4548                     global_attribute5,
4549                     global_attribute6,
4550                     global_attribute7,
4551                     global_attribute8,
4552                     global_attribute9,
4553                     global_attribute10,
4554                     global_attribute11,
4555                     global_attribute12,
4556                     global_attribute13,
4557                     global_attribute14,
4558                     global_attribute15,
4559                     global_attribute16,
4560                     global_attribute17,
4561                     global_attribute18,
4562                     global_attribute19,
4563                     global_attribute20,
4564                     primary_intended_use,
4565                     ship_to_location_id,
4566                     product_type,
4567                     product_category,
4568                     product_fisc_classification,
4569                     user_defined_fisc_class,
4570                     trx_business_category,
4571                     summary_tax_line_id,
4572                     tax_regime_code,
4573                     tax,
4574                     tax_jurisdiction_code,
4575                     tax_status_code,
4576                     tax_rate_id,
4577                     tax_rate_code,
4578                     tax_rate,
4579                     wfapproval_status,
4580                     pa_quantity,
4581                     nvl(created_by,5),
4582                     nvl(creation_date,sysdate)
4583            FROM    ap_ppa_invoice_lines_gt l
4584           --Introduced new and modified existing conditions for bug#9573078
4585 	        --instruction_id = p_instruction_id
4586 	    WHERE  invoice_id = p_invoice_id
4587              AND corrected_inv_id = p_invoice_id
4588              AND corrected_line_number = p_line_number
4589              AND     adj_type = 'ADJ'
4590 	     --Bug#15996840
4591 	     AND (l.invoice_id,l.line_number) IN
4592                   (SELECT invoice_id,invoice_line_number
4593                    FROM ap_ppa_invoice_dists_gt d
4594                    WHERE l.invoice_id = d.invoice_id
4595                      AND l.line_number =d.invoice_line_number
4596                      AND nvl(d.charge_applicable_to_dist_id,d.invoice_distribution_id) IN
4597 	                    (SELECT d1.invoice_distribution_id
4598 	                     FROM ap_ppa_invoice_dists_gt d1,
4599                                   ap_ppa_invoice_lines_gt l1
4600 	                     WHERE l1.corrected_inv_id  = p_base_match_invoice_id
4601 	                       AND l1.corrected_line_number = p_base_match_line_number
4602                                AND d1.invoice_id = l1.invoice_id
4603                                AND d1.invoice_line_number = l1.line_number));
4604       --
4605       ------------------------------------------------------------------------------
4606       debug_info := 'Insert_Zero_Amt_Adjustments Step 2. Insert into AP_INVOICE_DISTRIBUTIONS_ALL';
4607       ------------------------------------------------------------------------------
4608       INSERT INTO ap_invoice_distributions_all(
4609                 accounting_date,
4610                 accrual_posted_flag,
4611                 amount,
4612                 asset_book_type_code,
4613                 asset_category_id,
4614                 assets_addition_flag,
4615                 assets_tracking_flag,
4616                 attribute_category,
4617                 attribute1,
4618                 attribute10,
4619                 attribute11,
4620                 attribute12,
4621                 attribute13,
4622                 attribute14,
4623                 attribute15,
4624                 attribute2,
4625                 attribute3,
4626                 attribute4,
4627                 attribute5,
4628                 attribute6,
4629                 attribute7,
4630                 attribute8,
4631                 attribute9,
4632                 award_id,
4633                 awt_flag,
4634                 awt_group_id,
4635 		pay_awt_group_id,--bug6817107
4636                 awt_tax_rate_id,
4637                 base_amount,
4638                 batch_id,
4639                 cancellation_flag,
4640                 cash_posted_flag,
4641                 corrected_invoice_dist_id,
4642                 corrected_quantity,
4643                 country_of_supply,
4644                 created_by,
4645                 creation_date,
4646                 description,
4647                 dist_code_combination_id,
4648                 dist_match_type,
4649                 distribution_class,
4650                 distribution_line_number,
4651                 encumbered_flag,
4652                 expenditure_item_date,
4653                 expenditure_organization_id,
4654                 expenditure_type,
4655                 final_match_flag,
4656                 global_attribute_category,
4657                 global_attribute1,
4658                 global_attribute10,
4659                 global_attribute11,
4660                 global_attribute12,
4661                 global_attribute13,
4662                 global_attribute14,
4663                 global_attribute15,
4664                 global_attribute16,
4665                 global_attribute17,
4666                 global_attribute18,
4667                 global_attribute19,
4668                 global_attribute2,
4669                 global_attribute20,
4670                 global_attribute3,
4671                 global_attribute4,
4672                 global_attribute5,
4673                 global_attribute6,
4674                 global_attribute7,
4675                 global_attribute8,
4676                 global_attribute9,
4677                 income_tax_region,
4678                 inventory_transfer_status,
4679                 invoice_distribution_id,
4680                 invoice_id,
4681                 invoice_line_number,
4682                 line_type_lookup_code,
4683                 match_status_flag,
4684                 matched_uom_lookup_code,
4685                 merchant_document_number,
4686                 merchant_name,
4687                 merchant_reference,
4688                 merchant_tax_reg_number,
4689                 merchant_taxpayer_id,
4690                 org_id,
4691                 pa_addition_flag,
4692                 pa_quantity,
4693                 period_name,
4694                 po_distribution_id,
4695                 posted_flag,
4696                 project_id,
4697                 quantity_invoiced,
4698                 rcv_transaction_id,
4699                 reversal_flag,
4700                 rounding_amt,
4701                 set_of_books_id,
4702                 task_id,
4703                 type_1099,
4704                 unit_price,
4705 		--Freight and Special Charges
4706 		rcv_charge_addition_flag,
4707                 last_updated_by,
4708                 last_update_date,
4709 		 /*Bug#10416960 */
4710                 charge_applicable_to_dist_id,
4711                 summary_tax_line_id,
4712                 detail_tax_dist_id,
4713 		related_id)
4714        SELECT d.accounting_date,
4715               d.accrual_posted_flag,
4716               d.amount,
4717               d.asset_book_type_code,
4718               d.asset_category_id,
4719               d.assets_addition_flag,
4720               d.assets_tracking_flag,
4721               d.attribute_category,
4722               d.attribute1,
4723               d.attribute10,
4724               d.attribute11,
4725               d.attribute12,
4726               d.attribute13,
4727               d.attribute14,
4728               d.attribute15,
4729               d.attribute2,
4730               d.attribute3,
4731               d.attribute4,
4732               d.attribute5,
4733               d.attribute6,
4734               d.attribute7,
4735               d.attribute8,
4736               d.attribute9,
4737               d.award_id,
4738               d.awt_flag,
4739               d.awt_group_id,
4740 	      d.pay_awt_group_id,--bug6817107
4741               d.awt_tax_rate_id,
4742               d.base_amount,
4743               d.batch_id,
4744               d.cancellation_flag,
4745               d.cash_posted_flag,
4746               d.corrected_invoice_dist_id,
4747               d.corrected_quantity,
4748               d.country_of_supply,
4749               nvl(d.created_by,5),
4750               SYSDATE,
4751               d.description,
4752               d.dist_code_combination_id,
4753               d.dist_match_type,
4754               d.distribution_class,
4755               d.distribution_line_number,
4756               d.encumbered_flag,
4757               d.expenditure_item_date,
4758               d.expenditure_organization_id,
4759               d.expenditure_type,
4760               d.final_match_flag,
4761               d.global_attribute_category,
4762               d.global_attribute1,
4763               d.global_attribute10,
4764               d.global_attribute11,
4765               d.global_attribute12,
4766               d.global_attribute13,
4767               d.global_attribute14,
4768               d.global_attribute15,
4769               d.global_attribute16,
4770               d.global_attribute17,
4771               d.global_attribute18,
4772               d.global_attribute19,
4773               d.global_attribute2,
4774               d.global_attribute20,
4775               d.global_attribute3,
4776               d.global_attribute4,
4777               d.global_attribute5,
4778               d.global_attribute6,
4779               d.global_attribute7,
4780               d.global_attribute8,
4781               d.global_attribute9,
4782               d.income_tax_region,
4783               d.inventory_transfer_status,
4784 	       --Bug#10416960
4785                --ap_invoice_distributions_s.NEXTVAL,
4786               d.invoice_distribution_id,
4787               d.invoice_id,
4788               d.invoice_line_number,
4789               d.line_type_lookup_code,
4790               d.match_status_flag,
4791               d.matched_uom_lookup_code,
4792               d.merchant_document_number,
4793               d.merchant_name,
4794               d.merchant_reference,
4795               d.merchant_tax_reg_number,
4796               d.merchant_taxpayer_id,
4797               d.org_id,
4798               d.pa_addition_flag,
4799               d.pa_quantity,
4800               d.period_name,
4801               d.po_distribution_id,
4802               d.posted_flag,
4803               d.project_id,
4804               d.quantity_invoiced,
4805               d.rcv_transaction_id,
4806               d.reversal_flag,
4807               d.rounding_amt,
4808               d.set_of_books_id,
4809               d.task_id,
4810               d.type_1099,
4811               d.unit_price,
4812 	      'N',
4813               nvl(d.created_by,5),
4814               SYSDATE,
4815 	      /*Bug#10416960 */
4816                d.charge_applicable_to_dist_id,
4817                d.summary_tax_line_id,
4818                d.detail_tax_dist_id,
4819 	       d.related_id
4820           FROM ap_ppa_invoice_dists_gt d,
4821                ap_ppa_invoice_lines_gt l
4822          --Introduced new and modified existing conditions for bug#9573078
4823           --d.instruction_id = p_instruction_id
4824 	  WHERE  l.invoice_id = p_invoice_id
4825              AND l.corrected_inv_id = p_invoice_id
4826              AND l.corrected_line_number = p_line_number
4827              AND l.adj_type = 'ADJ'
4828              AND d.invoice_id = l.invoice_id
4829              AND d.invoice_line_number = l.line_number
4830 	     --Bug#15996840
4831 	     AND nvl(d.charge_applicable_to_dist_id,d.invoice_distribution_id) IN
4832 	                (SELECT d1.invoice_distribution_id
4833 	                 FROM ap_ppa_invoice_dists_gt d1,
4834                               ap_ppa_invoice_lines_gt l1
4835 	                  WHERE l1.corrected_inv_id  = p_base_match_invoice_id
4836 	                   AND l1.corrected_line_number = p_base_match_line_number
4837                            AND d1.invoice_id = l1.invoice_id
4838                            AND d1.invoice_line_number = l1.line_number);
4839 
4840     --
4841     RETURN(TRUE);
4842     --
4843 
4844 EXCEPTION
4845 WHEN OTHERS THEN
4846     IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
4847       AP_IMPORT_UTILITIES_PKG.Print(
4848         AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
4849     END IF;
4850 
4851     IF (SQLCODE < 0) then
4852       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
4853         AP_IMPORT_UTILITIES_PKG.Print(AP_IMPORT_INVOICES_PKG.g_debug_switch,SQLERRM);
4854       END IF;
4855     END IF;
4856     RETURN(FALSE);
4857 
4858 END Insert_Zero_Amt_Adjustments;
4859 
4860 /*=============================================================================
4861  |  FUNCTION - Validate_Temp_Ppa_Invoices()
4862  |
4863  |  DESCRIPTION
4864  |         This program leverages the Import Validation routines to validate
4865  |  all the PPA Invoices and Invoices Lines in the Temp table for a vendor.
4866  |  If any Invoice is returned with a status of 'N' then this program returns
4867  |  with a instr_status of N , meaning that instruction would be rejected in
4868  |  the interface table and neither Retroprice Adjustments would be made to
4869  |  the Original Invoices for the vendor nor any PPA documents would be created
4870  |  for the vendor.
4871  |
4872  |  PARAMETERS
4873  |      p_instruction_id
4874  |      p_base_currency_code
4875  |      p_multi_currency_flag
4876  |      p_set_of_books_id
4877  |      p_default_exchange_rate_type
4878  |      p_make_rate_mandatory_flag
4879  |      p_gl_date_from_get_info
4880  |      p_gl_date_from_receipt_flag
4881  |      p_positive_price_tolerance
4882  |      p_pa_installed
4883  |      p_qty_tolerance
4884  |      p_max_qty_ord_tolerance
4885  |      p_base_min_acct_unit
4886  |      p_base_precision
4887  |      p_chart_of_accounts_id
4888  |      p_freight_code_combination_id
4889  |      p_purch_encumbrance_flag
4890  |      p_calc_user_xrate
4891  |      p_default_last_updated_by
4892  |      p_default_last_update_login
4893  |      p_instr_status_flag
4894  |      P_calling_sequence - Calling sequence
4895  |
4896  |  MODIFICATION HISTORY
4897  |  Date         Author             Description of Change
4898  |  29-JUL-2003  dgulraja           Creation
4899  |
4900  *============================================================================*/
4901 FUNCTION Validate_Temp_Ppa_Invoices(
4902             p_instruction_id               IN     NUMBER,
4903             p_base_currency_code            IN     VARCHAR2,
4904             p_multi_currency_flag           IN     VARCHAR2,
4905             p_set_of_books_id               IN     NUMBER,
4906             p_default_exchange_rate_type    IN     VARCHAR2,
4907             p_make_rate_mandatory_flag      IN     VARCHAR2,
4908             p_gl_date_from_get_info         IN     DATE,
4909             p_gl_date_from_receipt_flag     IN     VARCHAR2,
4910             p_positive_price_tolerance      IN     NUMBER,
4911             p_pa_installed                  IN     VARCHAR2,
4912             p_qty_tolerance                 IN     NUMBER,
4913             p_max_qty_ord_tolerance         IN     NUMBER,
4914             p_base_min_acct_unit            IN     NUMBER,
4915             p_base_precision                IN     NUMBER,
4916             p_chart_of_accounts_id          IN     NUMBER,
4917             p_freight_code_combination_id   IN     NUMBER,
4918             p_purch_encumbrance_flag        IN     VARCHAR2,
4919             p_calc_user_xrate               IN     VARCHAR2,
4920             p_default_last_updated_by       IN     NUMBER,
4921             p_default_last_update_login     IN     NUMBER,
4922             p_instr_status_flag                OUT NOCOPY VARCHAR2,
4923             p_calling_sequence              IN     VARCHAR2)
4924 RETURN BOOLEAN IS
4925 
4926 CURSOR invoice_header  IS
4927 SELECT  invoice_id,
4928         invoice_num,
4929         invoice_type_lookup_code,
4930         invoice_date,
4931         NULL, --po_number should be NULL at the Invoice Header level
4932         vendor_id,
4933         NULL, --vendor_num,
4934         NULL, --vendor_name,
4935         vendor_site_id,
4936         NULL, --vendor_site_code,
4937         invoice_amount,
4938         invoice_currency_code,
4939         exchange_rate,
4940         exchange_rate_type,
4941         exchange_date,
4942         terms_id,
4943         NULL, --terms_name,
4944         terms_date,
4945         description,
4946         awt_group_id,
4947         NULL, --awt_group_name,
4948 	pay_awt_group_id,  --bug6817107
4949 	NULL,--pay_awt_group_name --bug6817107
4950         amount_applicable_to_discount,
4951         NULL, --last_update_date,
4952         NULL, --last_updated_by,
4953         NULL, --last_update_login,
4954         creation_date,
4955         created_by,
4956         NULL, --status,
4957         trim(attribute_category) attribute_category,
4958         trim(attribute1) attribute1,
4959         trim(attribute2) attribute2,
4960         trim(attribute3) attribute3,
4961         trim(attribute4) attribute4,
4962         trim(attribute5) attribute5,
4963         trim(attribute6) attribute6,
4964         trim(attribute7) attribute7,
4965         trim(attribute8) attribute8,
4966         trim(attribute9) attribute9,
4967         trim(attribute10) attribute10,
4968         trim(attribute11) attribute11,
4969         trim(attribute12) attribute12,
4970         trim(attribute13) attribute13,
4971         trim(attribute14) attribute14,
4972         trim(attribute15) attribute15,
4973         trim(global_attribute_category) global_attribute_category,
4974         trim(global_attribute1) global_attribute1,
4975         trim(global_attribute2) global_attribute2,
4976         trim(global_attribute3) global_attribute3,
4977         trim(global_attribute4) global_attribute4,
4978         trim(global_attribute5) global_attribute5,
4979         trim(global_attribute6) global_attribute6,
4980         trim(global_attribute7) global_attribute7,
4981         trim(global_attribute8) global_attribute8,
4982         trim(global_attribute9) global_attribute9,
4983         trim(global_attribute10) global_attribute10,
4984         trim(global_attribute11) global_attribute11,
4985         trim(global_attribute12) global_attribute12,
4986         trim(global_attribute13) global_attribute13,
4987         trim(global_attribute14) global_attribute14,
4988         trim(global_attribute15) global_attribute15,
4989         trim(global_attribute16) global_attribute16,
4990         trim(global_attribute17) global_attribute17,
4991         trim(global_attribute18) global_attribute18,
4992         trim(global_attribute19) global_attribute19,
4993         trim(global_attribute20) global_attribute20,
4994         payment_currency_code,
4995         payment_cross_rate,
4996         NULL, --payment_cross_rate_type,
4997         NULL, --payment_cross_rate_date,
4998         NULL, --doc_category_code,
4999         NULL, --voucher_num,
5000         payment_method_code, --4552701
5001         pay_group_lookup_code,
5002         goods_received_date,
5003         invoice_received_date,
5004         NULL, --gl_date,
5005         accts_pay_code_combination_id,
5006         NULL, --accts_pay_code_concatenated, -- bug 6603310
5007         exclusive_payment_flag,
5008         NULL, --prepay_num,
5009         NULL, --prepay_line_num,
5010         NULL, --prepay_apply_amount,
5011         NULL, --prepay_gl_date,
5012         NULL, --invoice_includes_prepay_flag,
5013         NULL, --no_xrate_base_amount,
5014         requester_id,
5015         org_id,
5016         NULL, --operating_unit,
5017         source,
5018         NULL, --group_id,
5019         NULL, --request_id,
5020         NULL, --workflow_flag,
5021         NULL, --vendor_email_address,
5022         NULL, --calc_tax_during_import_flag,
5023         NULL, --control_amount,
5024         NULL, --add_tax_to_inv_amt_flag,
5025         NULL, --tax_related_invoice_id,
5026         NULL, --taxation_country,
5027         NULL, --document_sub_type,
5028         NULL, --supplier_tax_invoice_number,
5029         NULL, --supplier_tax_invoice_date,
5030         NULL, --supplier_tax_exchange_rate,
5031         NULL, --tax_invoice_recording_date,
5032         NULL, --tax_invoice_internal_seq,
5033         NULL, --legal_entity_id,
5034         NULL, --set_of_books_id,
5035         NULL, --tax_only_rcv_matched_flag,
5036         NULL, --tax_only_flag,
5037         NULL, --apply_advances_flag
5038 	NULL, --application_id
5039 	NULL, --product_table
5040 	NULL, --reference_key1
5041 	NULL, --reference_key2
5042 	NULL, --reference_key3
5043 	NULL, --reference_key4
5044 	NULL, --reference_key5
5045 	NULL, --reference_1
5046 	NULL, --reference_2
5047         NULL,  --net_of_retainage_flag
5048         null,  --4552701, added nulls below so this code would compile
5049         null,
5050         null,
5051         null,
5052         null,
5053         null,
5054         null,
5055         null,
5056         null,
5057         null,
5058         null,
5059         null,
5060         null,
5061         null,
5062         null,
5063         null,
5064         null,
5065         null,
5066         null,
5067 	null,  -- original_invoice_amount bug7357218
5068 	null,   -- dispute_reason bug7357218
5069 	null,	-- 7535348 adding nulls to compile code, after third party payments
5070 	null,
5071 	null,
5072 	null,
5073 	null,
5074 	null
5075 	/* Added for bug 10226070 */
5076 	,NULL /* Requester_last_name */
5077 	,NULL /* Requester_first_name */
5078 	/* Added for bug 13074325 */
5079 	,NULL /* REQUESTER_EMPLOYEE_NUM */
5080   --bug 15862708
5081   ,null
5082   ,null
5083   ,null
5084   ,null
5085   ,null
5086   -- bug 16092065 starts
5087   ,null  --PREPAY_PERIOD_NAME
5088   ,null  --PREPAY_INV_ID
5089   ,null  --PREPAY_CASE_NAME
5090   --bug 16092065 ends
5091   FROM  ap_ppa_invoices_gt
5092   WHERE instruction_id = p_instruction_id;
5093 
5094 ---
5095 CURSOR invoice_lines(
5096                c_invoice_id  NUMBER) IS
5097 SELECT NULL,   --rowid
5098        invoice_line_id, --invoice_line_id,
5099        line_type_lookup_code,
5100        line_number,
5101        NULL, --line_group_number,
5102        amount,
5103        NULL, -- base amount
5104        accounting_date,
5105        NULL, --period name
5106        deferred_acctg_flag,
5107        NULL, --def_acctg_start_date,
5108        NULL, --def_acctg_end_date,
5109        NULL, --def_acctg_number_of_periods,
5110        NULL, --def_acctg_period_type,
5111        description,
5112        prorate_across_all_items,
5113        NULL, -- match_type
5114        po_header_id,
5115        NULL, --po_number,
5116        po_line_id,
5117        NULL, --po_line_number,
5118        po_release_id,
5119        NULL, --release_num,
5120        po_line_location_id,
5121        NULL, --po_shipment_num,
5122        po_distribution_id,
5123        NULL, --po_distribution_num,
5124        unit_meas_lookup_code,
5125        inventory_item_id,
5126        item_description,
5127        quantity_invoiced,
5128        NULL, --ship_to_location_code,
5129        unit_price,
5130        final_match_flag,
5131        NULL, --distribution_set_id,
5132        NULL, --distribution_set_name,
5133        NULL, -- partial segments
5134        NULL, --dist_code_concatenated,
5135        NULL, --dist_code_combination_id,
5136        awt_group_id,
5137        NULL, --awt_group_name,
5138        pay_awt_group_id,  --bug6817107
5139        NULL,--pay_awt_group_name --bug6817107
5140        NULL, --balancing_segment,
5141        NULL, --cost_center_segment,
5142        NULL, --account_segment,
5143        attribute_category,
5144        attribute1,
5145        attribute2,
5146        attribute3,
5147        attribute4,
5148        attribute5,
5149        attribute6,
5150        attribute7,
5151        attribute8,
5152        attribute9,
5153        attribute10,
5154        attribute11,
5155        attribute12,
5156        attribute13,
5157        attribute14,
5158        attribute15,
5159        global_attribute_category,
5160        global_attribute1,
5161        global_attribute2,
5162        global_attribute3,
5163        global_attribute4,
5164        global_attribute5,
5165        global_attribute6,
5166        global_attribute7,
5167        global_attribute8,
5168        global_attribute9,
5169        global_attribute10,
5170        global_attribute11,
5171        global_attribute12,
5172        global_attribute13,
5173        global_attribute14,
5174        global_attribute15,
5175        global_attribute16,
5176        global_attribute17,
5177        global_attribute18,
5178        global_attribute19,
5179        global_attribute20,
5180        project_id,
5181        task_id,
5182        award_id,
5183        expenditure_type,
5184        expenditure_item_date,
5185        expenditure_organization_id,
5186        NULL, --pa_addition_flag,
5187        pa_quantity,
5188        NULL, --stat_amount,
5189        type_1099,
5190        income_tax_region,
5191        assets_tracking_flag,
5192        asset_book_type_code,
5193        asset_category_id,
5194        serial_number,
5195        manufacturer,
5196        model_number,
5197        NULL, --warranty_number,
5198        NULL, --price_correction_flag,
5199        NULL, --price_correct_inv_num,
5200        NULL, -- corrected_inv_id           -- for price corrections via import
5201        NULL, --price_correct_inv_line_num,
5202        NULL, --receipt_number,
5203        NULL, --receipt_line_number,
5204        rcv_transaction_id,
5205        NULL, --rcv_shipment_line_id  --Bug7344899
5206        NULL, --match_option,
5207        NULL, --packing_slip,
5208        NULL, --vendor_item_num,
5209        NULL, --taxable_flag,
5210        NULL, --pa_cc_ar_invoice_id,
5211        NULL, --pa_cc_ar_invoice_line_num,
5212        NULL, --pa_cc_processed_code,
5213        NULL, --reference_1,
5214        NULL, --reference_2,
5215        credit_card_trx_id,
5216        requester_id,
5217        org_id,
5218        NULL, -- program_application_id
5219        NULL, -- program_id
5220        NULL, -- request_id
5221        NULL,  -- program_update_date
5222        NULL, --control_amount,
5223        NULL, --assessable_value,
5224        default_dist_ccid,
5225        primary_intended_use,
5226        ship_to_location_id,
5227        product_type,
5228        product_category,
5229        product_fisc_classification,
5230        user_defined_fisc_class,
5231        trx_business_category,
5232        tax_regime_code,
5233        tax,
5234        NULL,  --   tax_jurisdiction_code,
5235        tax_status_code,
5236        tax_rate_id,
5237        tax_rate_code,
5238        tax_rate,
5239        NULL,         --incl_in_taxable_line_flag
5240        NULL,	     --application_id
5241        NULL,	     --product_table
5242        NULL,	     --reference_key1
5243        NULL,	     --reference_key2
5244        NULL,	     --reference_key3
5245        NULL,         --reference_key4
5246        NULL,         --reference_key5
5247        NULL,	     --purchasing_category
5248        NULL,	     --purchasing_category_id
5249        cost_factor_id,  --cost_factor_id
5250        NULL,          --cost_factor_name
5251        NULL,	      		--source_application_id
5252        NULL,          		--source_entity_code
5253        NULL,          		--source_event_class_code
5254        NULL,	      		--source_trx_id
5255        NULL,	        	--source_line_id
5256        NULL,			--source_trx_level_type
5257        NULL,			--tax_classification_code
5258        NULL,                    --retained_amount
5259        NULL,                     --amount_includes_tax_flag  -- Bug 5436859
5260        --Bug6277609 starts Added the following columns to record
5261        NULL,                    --cc_reversal_flag
5262        NULL,                    --company_prepaid_invoice_id,
5263        NULL,                    --expense_group
5264        NULL,                    --justification
5265        NULL,                    --merchant_document_number,
5266        NULL,                    --merchant_name
5267        NULL,                    --merchant_reference
5268        NULL,                    --merchant_taxpayer_id
5269        NULL,                    --merchant_tax_reg_number
5270        NULL,                    --receipt_conversion_rate
5271        NULL,                    --receipt_conversion_amount
5272        NULL,                    --receipt_currency_code
5273        NULL                    --country_of_supply
5274        ,NULL			  --expense_start_date. Bug 8658097
5275        ,NULL			  --expense_end_date. Bug 8658097
5276        --Bug6277609 ends
5277 	   /* Added for bug 10226070 */
5278    	,NULL /* Requester_last_name */
5279    	,NULL /* Requester_first_name */
5280    	,NULL /* Bug10175718 CASCADE_RECEIPTS_FLAG Added to r_line_info_rec record type in AP_IMPORT_INVOICES_PKG*/
5281 	/* Added for bug 13074325 */
5282 	,NULL /* REQUESTER_EMPLOYEE_NUM */
5283   ,invoice_id --bug 15862708
5284   ,NULL
5285   ,NULL
5286 /*Bug 14271140 Start*/
5287   ,NULL  --Last_Updated_by
5288   ,NULL  --Last_update_login
5289   ,CREATED_BY
5290   ,CREATION_DATE
5291   ,NULL   --Last_Update_date
5292  /*Bug 14271140 End*/
5293  FROM ap_ppa_invoice_lines_gt
5294 WHERE invoice_id = c_invoice_id
5295  ORDER BY line_number;
5296 
5297 l_invoice_header_rec  AP_IMPORT_INVOICES_PKG.r_invoice_info_rec;
5298 l_invoice_header_list AP_IMPORT_INVOICES_PKG.t_invoice_table;
5299 l_invoice_lines_rec   AP_IMPORT_INVOICES_PKG.r_line_info_Rec;
5300 l_invoice_lines_list  AP_IMPORT_INVOICES_PKG.t_lines_table;
5301 
5302 
5303 l_fatal_error_flag       VARCHAR2(1);             -- OUT NOCOPY
5304 l_invoice_status         VARCHAR2(1) :='Y';
5305 l_instruction_status     VARCHAR2(1);
5306 l_match_mode             VARCHAR2(25);
5307 l_min_acct_unit_inv_curr NUMBER;
5308 l_precision_inv_curr     NUMBER;
5309 
5310 l_conc_request_id        NUMBER;
5311 l_prepay_appl_info       ap_prepay_pkg.Prepay_Appl_Tab;
5312 l_prepay_period_name     VARCHAR2(25);
5313 l_allow_interest_invoices VARCHAR2(1);
5314 
5315 --Contract Payments: Tolerance Redesign Project
5316 l_positive_price_tolerance NUMBER;
5317 l_negative_price_tolerance NUMBER;
5318 l_qty_tolerance 	   NUMBER;
5319 l_qty_rec_tolerance        NUMBER;
5320 l_max_qty_ord_tolerance    NUMBER;
5321 l_max_qty_rec_tolerance    NUMBER;
5322 l_amt_tolerance		   NUMBER;
5323 l_amt_rec_tolerance        NUMBER;
5324 l_max_amt_ord_tolerance    NUMBER;
5325 l_max_amt_rec_tolerance	   NUMBER;
5326 l_goods_ship_amt_tolerance NUMBER;
5327 l_goods_rate_amt_tolerance NUMBER;
5328 l_goods_total_amt_tolerance NUMBER;
5329 l_services_ship_amt_tolerance  NUMBER;
5330 l_services_rate_amt_tolerance  NUMBER;
5331 l_services_total_amt_tolerance NUMBER;
5332 l_prepay_invoice_id	 NUMBER;
5333 l_prepay_case_name	 VARCHAR2(50);
5334 
5335 debug_info               VARCHAR2(1000);
5336 current_calling_sequence VARCHAR2(1000);
5337 
5338 ppa_validation_failure   EXCEPTION;
5339 l_api_name constant VARCHAR2(200) := 'Validate_Temp_Ppa_Invoices';
5340 
5341 
5342 BEGIN
5343    --
5344    current_calling_sequence :=
5345     'AP_RETRO_PRICING_PKG.Validate_Temp_Ppa_Invoices<-'
5346     ||P_calling_sequence;
5347 
5348     IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
5349        FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_RETRO_PRICING_PKG.Validate_Temp_Ppa_Invoices(+)');
5350     END IF;
5351    --
5352    ---------------------------------------------------------------------------
5353    debug_info := 'Validate_Temp_Ppa_Invoices Step 1. Open Invoice_Header';
5354    ---------------------------------------------------------------------------
5355    OPEN invoice_header;
5356    FETCH invoice_header
5357    BULK COLLECT INTO  l_invoice_header_list;
5358    CLOSE invoice_header;
5359 
5360    --------------------------------------------------------------------------
5361      debug_info := 'Validate_Temp_Ppa_Invoices Step 2. Invoice Num: '||
5362      		    l_invoice_header_rec.invoice_num;
5363    --------------------------------------------------------------------------
5364    FOR i IN 1..l_invoice_header_list.COUNT  LOOP
5365 
5366       l_invoice_header_rec := l_invoice_header_list(i);
5367 
5368       SELECT auto_calculate_interest_flag
5369       INTO l_allow_interest_invoices
5370       FROM ap_system_parameters
5371       WHERE org_id = l_invoice_header_rec.org_id;
5372 
5373       --
5374       IF (AP_IMPORT_VALIDATION_PKG.v_check_invoice_validation
5375                (l_invoice_header_rec,           -- IN OUT NOCOPY
5376                 l_match_mode,                   -- OUT    ---no longer used.
5377                 l_min_acct_unit_inv_curr,       -- OUT NOCOPY  --used in lines val. for dist set
5378                 l_precision_inv_curr,           -- OUT NOCOPY --in val. of lineamt and inv amt.
5379 		l_positive_price_tolerance,	-- OUT
5380 		l_negative_price_tolerance,     -- OUT
5381 		l_qty_tolerance	,		-- OUT
5382 		l_qty_rec_tolerance,		-- OUT
5383 		l_max_qty_ord_tolerance,	-- OUT
5384 		l_max_qty_rec_tolerance,	-- OUT
5385 		l_amt_tolerance,		-- OUT
5386 		l_amt_rec_tolerance,		-- OUT
5387 		l_max_amt_ord_tolerance,	-- OUT
5388 		l_max_amt_rec_tolerance ,       -- OUT
5389 		l_goods_ship_amt_tolerance,     -- OUT
5390 		l_goods_rate_amt_tolerance ,    -- OUT
5391 		l_goods_total_amt_tolerance,    -- OUT
5392 		l_services_ship_amt_tolerance,  -- OUT
5393 		l_services_rate_amt_tolerance,  -- OUT
5394 		l_services_total_amt_tolerance, -- OUT
5395                 p_base_currency_code,           -- IN
5396                 p_multi_currency_flag,          -- IN
5397                 p_set_of_books_id,              -- IN
5398                 p_default_exchange_rate_type,   -- IN
5399                 p_make_rate_mandatory_flag,     -- IN
5400                 p_default_last_updated_by,      -- IN
5401                 p_default_last_update_login,    -- IN
5402                 l_fatal_error_flag,             -- OUT NOCOPY
5403                 l_invoice_status,               -- OUT NOCOPY
5404                 p_calc_user_xrate,              -- IN
5405                 l_prepay_period_name,           -- IN OUT
5406 		l_prepay_invoice_id,		-- OUT
5407 		l_prepay_case_name,		-- OUT
5408                 l_conc_request_id,               --IN
5409 		l_allow_interest_invoices,	--
5410                 current_calling_sequence) <> TRUE) THEN
5411       --
5412         IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
5413           AP_IMPORT_UTILITIES_PKG.Print(
5414             AP_IMPORT_INVOICES_PKG.g_debug_switch,
5415             'v_check_invoice_validation<-'||current_calling_sequence);
5416         END IF;
5417         Raise ppa_validation_failure;
5418         --
5419       END IF; --v_check_invoice_validation
5420 
5421       debug_info := 'Temp Invoice Validation Status,fatal_error_flag are : '||l_invoice_status||','||l_fatal_error_flag;
5422       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
5423            AP_IMPORT_UTILITIES_PKG.Print(
5424 	       AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
5425       END IF;
5426 
5427       IF (( l_invoice_status = 'Y') AND
5428           (NVL(l_fatal_error_flag,'N') = 'N')) THEN
5429 
5430 	 --  Start 9740137
5431          ---------------------------------------------------------------------
5432          debug_info := 'Validate_Temp_Ppa_Invoices Step 2.1 '
5433                        ||'Updating exchange rate in gt header table';
5434          ---------------------------------------------------------------------
5435 
5436            IF( l_invoice_header_rec.EXCHANGE_RATE IS NOT NULL) THEN
5437 
5438 	     UPDATE  ap_ppa_invoices_gt
5439              set exchange_rate = l_invoice_header_rec.EXCHANGE_RATE
5440              where invoice_id = l_invoice_header_rec.invoice_id
5441                and exchange_rate is null
5442 	       and exchange_rate_type <> 'User';
5443 
5444 	  END IF;
5445 
5446         --End 9740137
5447          ---------------------------------------------------------------------
5448          debug_info := 'Validate_Temp_Ppa_Invoices Step 3. '
5449                        ||'Open invoice_lines';
5450          ---------------------------------------------------------------------
5451          OPEN invoice_lines(l_invoice_header_Rec.invoice_id);
5452          FETCH invoice_lines
5453          BULK COLLECT INTO  l_invoice_lines_list;
5454          CLOSE invoice_lines;
5455          --
5456          ---------------------------------------------------------------------
5457          debug_info := 'Validate_Temp_Ppa_Invoices Step 4. Call '
5458                        ||'v_check_lines_validation';
5459          ---------------------------------------------------------------------
5460          IF (AP_IMPORT_VALIDATION_PKG.v_check_lines_validation (
5461                     l_invoice_header_Rec,             -- IN
5462                     l_invoice_lines_list,             -- IN --change it to IN parameter
5463                     p_gl_date_from_get_info,          -- IN
5464                     p_gl_date_from_receipt_flag,      -- IN
5465                     p_positive_price_tolerance,       -- IN
5466                     p_pa_installed,                   -- IN
5467                     l_qty_tolerance,                  -- IN
5468 		    l_amt_tolerance,		      -- IN
5469                     l_max_qty_ord_tolerance,          -- IN
5470 		    l_max_amt_ord_tolerance,	      -- IN
5471                     l_min_acct_unit_inv_curr,         -- IN from v_check_invoice_validation
5472                     l_precision_inv_curr,             -- IN from v_check_invoice_validation
5473                     p_base_currency_code,             -- IN
5474                     p_base_min_acct_unit,             -- IN
5475                     p_base_precision,                 -- IN
5476                     p_set_of_books_id,                -- IN
5477                     NULL,                             -- IN --5448579. Asset Book
5478                     p_chart_of_accounts_id,           -- IN
5479                     p_freight_code_combination_id,    -- IN
5480                     p_purch_encumbrance_flag,         -- IN
5481 		    NULL, --p_retainage_ccid	      -- IN
5482                     p_default_last_updated_by,        -- IN
5483                     p_default_last_update_login,      -- IN
5484                     l_invoice_status,                 -- OUT NOCOPY
5485                     current_calling_sequence) <> TRUE) THEN
5486 
5487             IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
5488                 AP_IMPORT_UTILITIES_PKG.Print(
5489                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
5490                   'v_check_lines_validation<-'||current_calling_sequence);
5491             END IF;
5492             --
5493             Raise ppa_validation_failure;
5494             --
5495          END IF;  --v_check_lines_validation
5496          --
5497       END IF; -- l_invoice_status
5498       --
5499       debug_info := 'Temp Invoice Line Validation Status,fatal_error_flag are : '||l_invoice_status||','||l_fatal_error_flag;
5500       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
5501            AP_IMPORT_UTILITIES_PKG.Print(
5502                AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
5503       END IF;
5504 
5505       IF  ((l_invoice_status = 'N')  AND
5506            NVL(l_fatal_error_flag,'N') = 'N' ) THEN
5507           --
5508           p_instr_status_flag := 'N';
5509       ELSE
5510           p_instr_status_flag := 'Y';
5511           --
5512       END IF;
5513       --
5514    END LOOP;  --invoice_header
5515    --
5516 
5517    -------------------------------------------------------------------------
5518    debug_info := 'Validate_Temp_Ppa_Invoices Step 5. Clear Header PL/SQL table';
5519    -------------------------------------------------------------------------
5520    l_invoice_header_list.DELETE;
5521    l_invoice_lines_list.DELETE;
5522 
5523   IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
5524      FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_RETRO_PRICING_PKG.Validate_Temp_Ppa_Invoices(-)');
5525   END IF;
5526 
5527    RETURN(TRUE);
5528    --
5529 EXCEPTION
5530 WHEN OTHERS THEN
5531     IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
5532       AP_IMPORT_UTILITIES_PKG.Print(
5533         AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
5534     END IF;
5535 
5536     IF (SQLCODE < 0) then
5537       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
5538         AP_IMPORT_UTILITIES_PKG.Print(AP_IMPORT_INVOICES_PKG.g_debug_switch,SQLERRM);
5539       END IF;
5540     END IF;
5541     --
5542     IF ( invoice_header%ISOPEN ) THEN
5543         CLOSE invoice_header;
5544     END IF;
5545     --
5546     IF ( invoice_lines%ISOPEN ) THEN
5547         CLOSE invoice_lines;
5548     END IF;
5549     --
5550     RETURN(FALSE);
5551 
5552 END Validate_Temp_Ppa_Invoices;
5553 
5554 
5555 /*=============================================================================
5556  |  FUNCTION - Insert_Ppa_Invoices()
5557  |
5558  |  DESCRIPTION
5559  |      After validating a all proposed PPA's for a vendor(instruction), this
5560  |  function insert PPA documents in the Transaction Tables. It also creates payment schedules for the
5561  |  for all the PPA invoices for a valid insruction(Vendor).
5562  |
5563  |  PARAMETERS
5564  |      p_instruction_id
5565  |      p_ppa_invoices_count
5566  |      p_ppa_invoices_total
5567  |      P_calling_sequence - Calling sequence
5568  |
5569  |  MODIFICATION HISTORY
5570  |  Date         Author             Description of Change
5571  |  29-JUL-2003  dgulraja           Creation
5572  |
5573  *============================================================================*/
5574 FUNCTION Insert_Ppa_Invoices(
5575              p_instruction_id         IN            NUMBER,
5576              p_ppa_invoices_count         OUT NOCOPY NUMBER,
5577              p_ppa_invoices_total         OUT NOCOPY NUMBER,
5578              p_calling_sequence        IN            VARCHAR2)
5579 RETURN BOOLEAN IS
5580 
5581 CURSOR ppa_invoices IS
5582 SELECT invoice_id,
5583        vendor_id,
5584        vendor_site_id,
5585        terms_id,
5586        terms_date,
5587        payment_cross_rate,
5588        invoice_currency_code,
5589        payment_currency_code,
5590        invoice_amount,
5591        base_amount,
5592        amount_applicable_to_discount,
5593        payment_method_code, --4552701
5594        exclusive_payment_flag,
5595        FND_GLOBAL.user_id, --bugfix:4681253
5596        FND_GLOBAL.user_id, --bugfix:4681253
5597        batch_id,
5598        org_id
5599 FROM   ap_ppa_invoices_gt
5600 WHERE  instruction_id = p_instruction_id
5601 AND    instr_status_flag = 'Y';
5602 
5603 l_ppa_invoice_id                NUMBER;
5604 
5605 l_vendor_id                     AP_INVOICES_ALL.vendor_id%TYPE;
5606 l_vendor_site_id                AP_INVOICES_ALL.vendor_site_id%TYPE;
5607 l_payment_cross_rate            AP_INVOICES_ALL.payment_cross_rate%TYPE;
5608 l_invoice_currency_code         AP_INVOICES_ALL.invoice_currency_code%TYPE;
5609 l_payment_currency_code         AP_INVOICES_ALL.payment_currency_code%TYPE;
5610 l_payment_cross_rate_date       AP_INVOICES_ALL.payment_cross_rate_date%TYPE;
5611 l_invoice_type_lookup_code      AP_INVOICES_ALL.invoice_type_lookup_code%TYPE;
5612 l_payment_method_code           iby_payment_methods_vl.payment_method_code%TYPE;
5613 l_amt_applicable_to_discount    AP_INVOICES_ALL.amount_applicable_to_discount%TYPE;
5614 l_invoice_amount                AP_INVOICES_ALL.invoice_amount%TYPE;
5615 l_base_amount                   AP_INVOICES_ALL.base_amount%TYPE;
5616 l_exclusive_payment_flag        AP_INVOICES_ALL.exclusive_payment_flag%TYPE;
5617 l_terms_id                      AP_INVOICES_ALL.terms_id%TYPE;
5618 l_terms_date                    AP_INVOICES_ALL.terms_date%TYPE;
5619 l_batch_id                      AP_INVOICES_ALL.batch_id%TYPE;
5620 l_set_of_books_id               AP_INVOICES_ALL.set_of_books_id%TYPE;
5621 l_org_id                        AP_INVOICES_ALL.org_id%TYPE;
5622 l_doc_category_code             AP_INVOICES_ALL.doc_category_code%TYPE;
5623 l_doc_sequence_id               AP_INVOICES_ALL.doc_sequence_id%TYPE;
5624 l_doc_sequence_value            AP_INVOICES_ALL.doc_sequence_value%TYPE;
5625 
5626 l_pay_curr_invoice_amount       NUMBER;
5627 l_payment_priority              NUMBER;
5628 l_invoice_amount_limit          NUMBER;
5629 l_hold_future_payments_flag     VARCHAR2(1);
5630 l_supplier_hold_reason          VARCHAR2(240);
5631 l_seq_num_profile               VARCHAR2(80);
5632 l_return_code                   NUMBER;
5633 l_dbseqnm                       VARCHAR2(30);
5634 l_seqassid                      NUMBER;
5635 
5636 l_created_by                    NUMBER;
5637 l_last_updated_by               NUMBER;
5638 l_last_update_login             NUMBER;
5639 
5640 l_ppa_invoices_count            NUMBER;
5641 l_ppa_invoices_total            NUMBER;
5642 i                               INTEGER;
5643 
5644 debug_info                      VARCHAR2(1000);
5645 current_calling_sequence        VARCHAR2(1000);
5646 
5647 ppa_creation_failure            EXCEPTION;
5648 l_api_name constant varchar2(200) := 'Insert_PPa_Invoices';
5649 
5650 BEGIN
5651    --
5652    current_calling_sequence :=
5653     'AP_RETRO_PRICING_PKG.Insert_Ppa_Invoices<-'
5654     ||P_calling_sequence;
5655 
5656    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
5657             FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_RETRO_PRICING_PKG.Insert_Ppa_Invoices(+)');
5658    END IF;
5659    --
5660    i := 1;
5661    l_ppa_invoices_count := 0;
5662    l_ppa_invoices_total  := 0;
5663    --
5664    -----------------------------------------------------------------------
5665    debug_info := 'Insert_Ppa_Invoices Step 1. Open cursor Ppa_invoices';
5666    IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
5667       AP_IMPORT_UTILITIES_PKG.Print(
5668         AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
5669    END IF;
5670 
5671    IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5672             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,debug_info);
5673    END IF;
5674 
5675    -----------------------------------------------------------------------
5676    OPEN  ppa_invoices;
5677    LOOP
5678       FETCH ppa_invoices
5679       INTO l_ppa_invoice_id,
5680            l_vendor_id,
5681            l_vendor_site_id,
5682            l_terms_id,
5683            l_terms_date,
5684            l_payment_cross_rate,
5685            l_invoice_currency_code,
5686            l_payment_currency_code,
5687            l_invoice_amount,
5688            l_base_amount,
5689            l_amt_applicable_to_discount,
5690            l_payment_method_code,
5691            l_exclusive_payment_flag,
5692            l_created_by,
5693 	   l_last_updated_by,  --Bugfix:4681253
5694            l_batch_id,
5695            l_org_id;
5696       EXIT WHEN ppa_invoices%NOTFOUND;
5697       --
5698       l_ppa_invoices_count  := l_ppa_invoices_count + i;
5699       l_ppa_invoices_total  := l_ppa_invoices_total + l_invoice_amount;
5700       --
5701       ------------------------------------------------------------------
5702       debug_info := 'Insert_Ppa_Invoices Step 2. Get Info';
5703       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
5704          AP_IMPORT_UTILITIES_PKG.Print(
5705             AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
5706       END IF;
5707 
5708       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5709             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,debug_info);
5710       END IF;
5711       -------------------------------------------------------------------
5712       IF (l_invoice_currency_code <> l_payment_currency_code) AND
5713          (l_payment_cross_rate is NOT NULL) THEN
5714          --
5715          l_pay_curr_invoice_amount := ap_utilities_pkg.ap_round_currency(
5716                                         l_invoice_amount *l_payment_cross_rate,
5717                                         l_payment_currency_code);
5718       --Bugfix:4681253
5719       ELSE
5720          l_pay_curr_invoice_amount := l_invoice_amount;
5721 
5722       END IF;
5723        --
5724       SELECT DECODE(l_invoice_type_lookup_code,
5725                      'CREDIT','N',
5726                       l_exclusive_payment_flag), --4552701 don't get this flag from vendors table
5727             payment_priority,
5728             invoice_amount_limit,
5729             hold_future_payments_flag,
5730             hold_reason
5731        INTO l_exclusive_payment_flag,
5732             l_payment_priority,
5733             l_invoice_amount_limit,
5734             l_hold_future_payments_flag,
5735             l_supplier_hold_reason
5736        FROM po_vendor_sites_all
5737       WHERE vendor_id = l_vendor_id
5738         AND vendor_site_id = l_vendor_site_id;
5739       --
5740   -- bug8514744, moved the fetch of the next doc sequence, and the
5741   -- doc sequence value from apretrub.pls at the time of PPA GT
5742   -- insertion, to the time of actual invoice creation
5743   -------------------------------------------
5744   debug_info := 'Create_Ppa_Invoice Step :4 Doc_sequence_Num';
5745   IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
5746       AP_IMPORT_UTILITIES_PKG.Print(
5747          AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
5748    END IF;
5749   -------------------------------------------
5750   FND_PROFILE.GET('UNIQUE:SEQ_NUMBERS',l_seq_num_profile);
5751   IF (l_seq_num_profile  IN ('A','P')) THEN
5752 
5753      l_doc_category_code := 'PO ADJ INV';
5754 
5755      --Bug5769161 (adding exception handling)
5756      BEGIN
5757 
5758      SELECT SEQ.db_sequence_name,
5759             SEQ.doc_sequence_id,
5760             SA.doc_sequence_assignment_id,
5761             asp.set_of_books_id
5762        INTO l_dbseqnm,
5763             l_doc_sequence_id,
5764             l_seqassid,
5765             l_set_of_books_id
5766        FROM fnd_document_sequences SEQ,
5767             fnd_doc_sequence_assignments SA,
5768             ap_system_parameters_all asp
5769       WHERE SEQ.doc_sequence_id        = SA.doc_sequence_id
5770         AND SA.application_id          = 200
5771         AND SA.category_code           = l_doc_category_code
5772         AND (NVL(SA.method_code,'A') = 'A')
5773         AND (SA.set_of_books_id = asp.set_of_books_id)
5774         AND asp.org_id = l_org_id
5775         AND SYSDATE -- never null
5776              BETWEEN SA.start_date
5777              AND NVL(SA.end_date, TO_DATE('31/12/4712','DD/MM/YYYY'));
5778 
5779      ---------------------------------------
5780      debug_info :=  'Create_Ppa_Invoice Step :5 Get doc sequence val';
5781      IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
5782        AP_IMPORT_UTILITIES_PKG.Print(
5783          AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
5784      END IF;
5785      ----------------------------------------
5786 
5787      l_return_code := FND_SEQNUM.GET_SEQ_VAL(
5788                          200,
5789                          l_doc_category_code,
5790                          l_set_of_books_id,
5791                          'A',
5792                          SYSDATE,
5793                          l_doc_sequence_value,
5794                          l_doc_sequence_id,
5795                          'N','N');
5796 
5797       debug_info := 'After fetching the doc sequence'||
5798                      l_doc_category_code||'--'||
5799                      l_set_of_books_id||'--'||
5800                      l_doc_sequence_value||'--'||
5801                      l_doc_sequence_id;
5802 
5803       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
5804          AP_IMPORT_UTILITIES_PKG.Print(
5805            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
5806       END IF;
5807 
5808       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5809          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,debug_info);
5810       END IF;
5811 
5812 
5813      EXCEPTION
5814        WHEN no_data_found THEN
5815             NULL;
5816 
5817      END;
5818 
5819      debug_info := 'after FND_SEQNUM.GET_SEQ_VAL';
5820      IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
5821        AP_IMPORT_UTILITIES_PKG.Print(
5822          AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
5823      END IF;
5824 
5825   END IF;
5826 
5827       ------------------------------------------------------------------------
5828       debug_info := 'Insert_Ppa_Invoices Step 3. Insert into AP_INVOICES_ALL, l_ppa_invoice_id is'||l_ppa_invoice_id;
5829       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
5830          AP_IMPORT_UTILITIES_PKG.Print(
5831            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
5832       END IF;
5833 
5834       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
5835          FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,debug_info);
5836       END IF;
5837       ------------------------------------------------------------------------
5838       INSERT INTO ap_invoices_All(
5839                 accts_pay_code_combination_id,
5840                 amount_applicable_to_discount,
5841                 approval_ready_flag,
5842                 attribute_category,
5843                 attribute1,
5844                 attribute10,
5845                 attribute11,
5846                 attribute12,
5847                 attribute13,
5848                 attribute14,
5849                 attribute15,
5850                 attribute2,
5851                 attribute3,
5852                 attribute4,
5853                 attribute5,
5854                 attribute6,
5855                 attribute7,
5856                 attribute8,
5857                 attribute9,
5858                 award_id,
5859                 awt_flag,
5860                 awt_group_id,
5861 		pay_awt_group_id,--bug6817107
5862                 base_amount,
5863                 description,
5864                 exchange_date,
5865                 exchange_rate,
5866                 exchange_rate_type,
5867                 exclusive_payment_flag,
5868                 expenditure_item_date,
5869                 expenditure_organization_id,
5870                 expenditure_type,
5871                 global_attribute_category,
5872                 global_attribute1,
5873                 global_attribute10,
5874                 global_attribute11,
5875                 global_attribute12,
5876                 global_attribute13,
5877                 global_attribute14,
5878                 global_attribute15,
5879                 global_attribute16,
5880                 global_attribute17,
5881                 global_attribute18,
5882                 global_attribute19,
5883                 global_attribute2,
5884                 global_attribute20,
5885                 global_attribute3,
5886                 global_attribute4,
5887                 global_attribute5,
5888                 global_attribute6,
5889                 global_attribute7,
5890                 global_attribute8,
5891                 global_attribute9,
5892                 goods_received_date,
5893                 invoice_amount,
5894                 invoice_currency_code,
5895                 invoice_date,
5896                 invoice_id,
5897                 invoice_num,
5898                 invoice_received_date,
5899                 invoice_type_lookup_code,
5900                 org_id,
5901                 pa_default_dist_ccid,
5902                 pay_group_lookup_code,
5903                 payment_cross_rate,
5904                 payment_currency_code,
5905                 payment_method_code,
5906                 payment_status_flag,
5907                 project_id,
5908                 requester_id,
5909                 set_of_books_id,
5910                 source,
5911                 task_id,
5912                 terms_date,
5913                 terms_id,
5914                 vendor_id,
5915                 vendor_site_id,
5916                 wfapproval_status,
5917                 creation_date,
5918                 created_by,
5919                 last_updated_by,
5920                 last_update_date,
5921                 last_update_login,
5922 		gl_date,
5923                 APPLICATION_ID ,
5924                 BANK_CHARGE_BEARER ,
5925                 DELIVERY_CHANNEL_CODE ,
5926                 DISC_IS_INV_LESS_TAX_FLAG ,
5927                 DOCUMENT_SUB_TYPE	,
5928                 EXCLUDE_FREIGHT_FROM_DISCOUNT	,
5929                 EXTERNAL_BANK_ACCOUNT_ID	,
5930                 LEGAL_ENTITY_ID	,
5931                 NET_OF_RETAINAGE_FLAG	,
5932                 PARTY_ID	,
5933                 PARTY_SITE_ID	,
5934                 PAYMENT_CROSS_RATE_DATE	,
5935                 PAYMENT_CROSS_RATE_TYPE	,
5936                 PAYMENT_FUNCTION	,
5937                 PAYMENT_REASON_CODE	,
5938                 PAYMENT_REASON_COMMENTS	,
5939                 PAY_CURR_INVOICE_AMOUNT	,
5940                 PAY_PROC_TRXN_TYPE_CODE	,
5941                 PORT_OF_ENTRY_CODE	,
5942                 POSTING_STATUS	,
5943                 PO_HEADER_ID	,
5944                 PRODUCT_TABLE	,
5945                 PROJECT_ACCOUNTING_CONTEXT	,
5946                 QUICK_PO_HEADER_ID	,
5947                 REFERENCE_1	,
5948                 REFERENCE_2	,
5949                 REFERENCE_KEY1	,
5950                 REFERENCE_KEY2	,
5951                 REFERENCE_KEY3	,
5952                 REFERENCE_KEY4	,
5953                 REFERENCE_KEY5	,
5954                 REMITTANCE_MESSAGE1	,
5955                 REMITTANCE_MESSAGE2	,
5956                 REMITTANCE_MESSAGE3	,
5957                 SETTLEMENT_PRIORITY	,
5958                 SUPPLIER_TAX_EXCHANGE_RATE ,
5959                 SUPPLIER_TAX_INVOICE_DATE	,
5960                 SUPPLIER_TAX_INVOICE_NUMBER	,
5961                 TAXATION_COUNTRY	,
5962                 TAX_INVOICE_INTERNAL_SEQ ,
5963                 TAX_INVOICE_RECORDING_DATE	,
5964                 TAX_RELATED_INVOICE_ID	,
5965                 TRX_BUSINESS_CATEGORY	,
5966                 UNIQUE_REMITTANCE_IDENTIFIER	,
5967                 URI_CHECK_DIGIT	,
5968                 USER_DEFINED_FISC_CLASS,
5969                 DOC_CATEGORY_CODE,
5970                 DOC_SEQUENCE_ID,
5971                 DOC_SEQUENCE_VALUE)
5972       SELECT    accts_pay_code_combination_id,
5973                 amount_applicable_to_discount,
5974                 approval_ready_flag,
5975                 attribute_category,
5976                 attribute1,
5977                 attribute10,
5978                 attribute11,
5979                 attribute12,
5980                 attribute13,
5981                 attribute14,
5982                 attribute15,
5983                 attribute2,
5984                 attribute3,
5985                 attribute4,
5986                 attribute5,
5987                 attribute6,
5988                 attribute7,
5989                 attribute8,
5990                 attribute9,
5991                 award_id,
5992                 awt_flag,
5993                 awt_group_id,
5994 		pay_awt_group_id,--bug6817107
5995                 base_amount,
5996                 description,
5997                 exchange_date,
5998                 exchange_rate,
5999                 exchange_rate_type,
6000                 exclusive_payment_flag,
6001                 expenditure_item_date,
6002                 expenditure_organization_id,
6003                 expenditure_type,
6004                 global_attribute_category,
6005                 global_attribute1,
6006                 global_attribute10,
6007                 global_attribute11,
6008                 global_attribute12,
6009                 global_attribute13,
6010                 global_attribute14,
6011                 global_attribute15,
6012                 global_attribute16,
6013                 global_attribute17,
6014                 global_attribute18,
6015                 global_attribute19,
6016                 global_attribute2,
6017                 global_attribute20,
6018                 global_attribute3,
6019                 global_attribute4,
6020                 global_attribute5,
6021                 global_attribute6,
6022                 global_attribute7,
6023                 global_attribute8,
6024                 global_attribute9,
6025                 goods_received_date,
6026                 invoice_amount,
6027                 invoice_currency_code,
6028                 invoice_date,
6029                 invoice_id,
6030                 invoice_num,
6031                 invoice_received_date,
6032                 invoice_type_lookup_code,
6033                 org_id,
6034                 pa_default_dist_ccid,
6035                 pay_group_lookup_code,
6036                 payment_cross_rate,
6037                 payment_currency_code,
6038                 payment_method_code,
6039                 payment_status_flag,
6040                 project_id,
6041                 requester_id,
6042                 set_of_books_id,
6043                 source,
6044                 task_id,
6045                 terms_date,
6046                 terms_id,
6047                 vendor_id,
6048                 vendor_site_id,
6049                 wfapproval_status,
6050                 SYSDATE,                   --creation_date
6051                 FND_GLOBAL.user_id,        --created_by
6052                 FND_GLOBAL.user_id,        --last_updated_by
6053                 SYSDATE,                   --last_update_date
6054                 FND_GLOBAL.conc_login_id,   --last_update_login
6055 		SYSDATE,			   --4681253
6056                 APPLICATION_ID ,
6057                 BANK_CHARGE_BEARER ,
6058                 DELIVERY_CHANNEL_CODE ,
6059                 DISC_IS_INV_LESS_TAX_FLAG ,
6060                 DOCUMENT_SUB_TYPE       ,
6061                 EXCLUDE_FREIGHT_FROM_DISCOUNT   ,
6062                 EXTERNAL_BANK_ACCOUNT_ID        ,
6063                 LEGAL_ENTITY_ID ,
6064                 NET_OF_RETAINAGE_FLAG   ,
6065                 PARTY_ID        ,
6066                 PARTY_SITE_ID   ,
6067                 PAYMENT_CROSS_RATE_DATE     ,
6068                 PAYMENT_CROSS_RATE_TYPE ,
6069                 PAYMENT_FUNCTION        ,
6070                 PAYMENT_REASON_CODE     ,
6071                 PAYMENT_REASON_COMMENTS ,
6072                 PAY_CURR_INVOICE_AMOUNT ,
6073                 PAY_PROC_TRXN_TYPE_CODE ,
6074                 PORT_OF_ENTRY_CODE      ,
6075                 POSTING_STATUS  ,
6076                 PO_HEADER_ID    ,
6077                 PRODUCT_TABLE   ,
6078                 PROJECT_ACCOUNTING_CONTEXT      ,
6079                 QUICK_PO_HEADER_ID      ,
6080                 REFERENCE_1     ,
6081                 REFERENCE_2     ,
6082                 REFERENCE_KEY1  ,
6083                 REFERENCE_KEY2  ,
6084                 REFERENCE_KEY3  ,
6085                 REFERENCE_KEY4  ,
6086                 REFERENCE_KEY5  ,
6087                 REMITTANCE_MESSAGE1     ,
6088                 REMITTANCE_MESSAGE2     ,
6089                 REMITTANCE_MESSAGE3     ,
6090                 SETTLEMENT_PRIORITY     ,
6091                 SUPPLIER_TAX_EXCHANGE_RATE ,
6092                 SUPPLIER_TAX_INVOICE_DATE       ,
6093                 SUPPLIER_TAX_INVOICE_NUMBER     ,
6094                 TAXATION_COUNTRY        ,
6095                 TAX_INVOICE_INTERNAL_SEQ ,
6096                 TAX_INVOICE_RECORDING_DATE      ,
6097                 TAX_RELATED_INVOICE_ID  ,
6098                 TRX_BUSINESS_CATEGORY   ,
6099                 UNIQUE_REMITTANCE_IDENTIFIER    ,
6100                 URI_CHECK_DIGIT ,
6101                 USER_DEFINED_FISC_CLASS,
6102                 l_doc_category_code,
6103                 l_doc_sequence_id,
6104                 l_doc_sequence_value
6105       FROM      ap_ppa_invoices_gt
6106       WHERE     instruction_id = p_instruction_id
6107       AND       invoice_id = l_ppa_invoice_id
6108       AND       instr_status_flag = 'Y';
6109       --
6110       -------------------------------------------------------------------
6111       debug_info := 'Insert_Ppa_Invoices Step 4. AP_Create_From_Terms';
6112       debug_info := 'p_invoice_id : '||l_ppa_invoice_id
6113       		   || ',p_terms_id : '||l_terms_id
6114 		   || ',p_last_updated_by : '||l_last_updated_by
6115 		   || ',p_created_by : '||l_created_by
6116 		   || ',p_payment_priority : '||l_payment_priority
6117 		    ||',p_batch_id : '||l_batch_id
6118 		    ||',p_terms_date : '||l_terms_date
6119 		    ||',p_invoice_amount : '||l_invoice_amount
6120 		    ||',p_pay_curr_invoice_amount : '||l_pay_curr_invoice_amount
6121 		    ||', p_payment_cross_rate : '||l_payment_cross_rate
6122 		    ||',p_amount_for_discount : '||NVL(l_amt_applicable_to_discount,
6123 		                                                l_invoice_amount)
6124 	            ||',p_payment_method : '||l_payment_method_code
6125 		    ||',p_invoice_currency : '||l_invoice_currency_code
6126 		    ||',p_payment_currency : '||l_payment_currency_code;
6127 
6128       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
6129         AP_IMPORT_UTILITIES_PKG.Print(
6130            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
6131       END IF;
6132 
6133       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6134             FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,debug_info);
6135       END IF;
6136 
6137       -------------------------------------------------------------------
6138       AP_CREATE_PAY_SCHEDS_PKG.AP_Create_From_Terms(
6139            p_invoice_id               =>l_ppa_invoice_id,
6140            p_terms_id                 =>l_terms_id,
6141            p_last_updated_by          =>l_last_updated_by,
6142            p_created_by               =>l_created_by,
6143            p_payment_priority         =>l_payment_priority,
6144            p_batch_id                 =>l_batch_id,
6145            p_terms_date               =>l_terms_date,
6146            p_invoice_amount           =>l_invoice_amount,
6147            p_pay_curr_invoice_amount  =>l_pay_curr_invoice_amount,
6148            p_payment_cross_rate       =>l_payment_cross_rate,
6149            p_amount_for_discount      =>NVL(l_amt_applicable_to_discount,
6150                                             l_invoice_amount),
6151            p_payment_method           =>l_payment_method_code,
6152            p_invoice_currency         =>l_invoice_currency_code,
6153            p_payment_currency         =>l_payment_currency_code,
6154            p_calling_sequence         =>current_calling_sequence);
6155       --
6156       ------------------------------------------------------------------------
6157       debug_info := 'Insert_Ppa_Invoices Step 5. Insert into '
6158                     ||'AP_INVOICE_LINES_ALL ';
6159       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
6160          AP_IMPORT_UTILITIES_PKG.Print(
6161            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
6162       END IF;
6163 
6164       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6165           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,debug_info);
6166       END IF;
6167 
6168       ------------------------------------------------------------------------
6169       INSERT INTO AP_INVOICE_LINES_ALL(
6170                     invoice_id,
6171                     line_number,
6172                     line_type_lookup_code,
6173                     requester_id,
6174                     description,
6175                     line_source,
6176                     org_id,
6177                     inventory_item_id,
6178                     item_description,
6179                     serial_number,
6180                     manufacturer,
6181                     model_number,
6182                     generate_dists,
6183                     match_type,
6184                     default_dist_ccid,
6185                     prorate_across_all_items,
6186                     accounting_date,
6187                     period_name,
6188                     deferred_acctg_flag,
6189                     set_of_books_id,
6190                     amount,
6191                     base_amount,
6192                     rounding_amt,
6193                     quantity_invoiced,
6194                     unit_meas_lookup_code,
6195                     unit_price,
6196                     discarded_flag,
6197                     cancelled_flag,
6198                     income_tax_region,
6199                     type_1099,
6200                     corrected_inv_id,
6201                     corrected_line_number,
6202                     po_header_id,
6203                     po_line_id,
6204                     po_release_id,
6205                     po_line_location_id,
6206                     po_distribution_id,
6207                     rcv_transaction_id,
6208                     final_match_flag,
6209                     assets_tracking_flag,
6210                     asset_book_type_code,
6211                     asset_category_id,
6212                     project_id,
6213                     task_id,
6214                     expenditure_type,
6215                     expenditure_item_date,
6216                     expenditure_organization_id,
6217                     award_id,
6218                     awt_group_id,
6219 		    pay_awt_group_id,--bug6817107
6220                     receipt_verified_flag,
6221                     receipt_required_flag,
6222                     receipt_missing_flag,
6223                     justification,
6224                     expense_group,
6225                     start_expense_date,
6226                     end_expense_date,
6227                     receipt_currency_code,
6228                     receipt_conversion_rate,
6229                     receipt_currency_amount,
6230                     daily_amount,
6231                     web_parameter_id,
6232                     adjustment_reason,
6233                     merchant_document_number,
6234                     merchant_name,
6235                     merchant_reference,
6236                     merchant_tax_reg_number,
6237                     merchant_taxpayer_id,
6238                     country_of_supply,
6239                     credit_card_trx_id,
6240                     company_prepaid_invoice_id,
6241                     cc_reversal_flag,
6242                     attribute_category,
6243                     attribute1,
6244                     attribute2,
6245                     attribute3,
6246                     attribute4,
6247                     attribute5,
6248                     attribute6,
6249                     attribute7,
6250                     attribute8,
6251                     attribute9,
6252                     attribute10,
6253                     attribute11,
6254                     attribute12,
6255                     attribute13,
6256                     attribute14,
6257                     attribute15,
6258                     global_attribute_category,
6259                     global_attribute1,
6260                     global_attribute2,
6261                     global_attribute3,
6262                     global_attribute4,
6263                     global_attribute5,
6264                     global_attribute6,
6265                     global_attribute7,
6266                     global_attribute8,
6267                     global_attribute9,
6268                     global_attribute10,
6269                     global_attribute11,
6270                     global_attribute12,
6271                     global_attribute13,
6272                     global_attribute14,
6273                     global_attribute15,
6274                     global_attribute16,
6275                     global_attribute17,
6276                     global_attribute18,
6277                     global_attribute19,
6278                     global_attribute20,
6279                     primary_intended_use,
6280                     ship_to_location_id,
6281                     product_type,
6282                     product_category,
6283                     product_fisc_classification,
6284                     user_defined_fisc_class,
6285                     trx_business_category,
6286                     summary_tax_line_id,
6287                     tax_regime_code,
6288                     tax,
6289                     tax_jurisdiction_code,
6290                     tax_status_code,
6291                     tax_rate_id,
6292                     tax_rate_code,
6293                     tax_rate,
6294                     wfapproval_status,
6295                     pa_quantity,
6296                     creation_date,
6297                     created_by,
6298                     last_updated_by,
6299                     last_update_date,
6300                     last_update_login,
6301                     program_application_id,
6302                     program_id,
6303                     program_update_date,
6304                     request_id)
6305             SELECT  invoice_id,
6306                     line_number,
6307                     line_type_lookup_code,
6308                     requester_id,
6309                     description,
6310                     line_source,
6311                     org_id,
6312                     inventory_item_id,
6313                     item_description,
6314                     serial_number,
6315                     manufacturer,
6316                     model_number,
6317                     generate_dists,
6318                     match_type,
6319                     default_dist_ccid,
6320                     prorate_across_all_items,
6321                     accounting_date,
6322                     period_name,
6323                     deferred_acctg_flag,
6324                     set_of_books_id,
6325                     amount,
6326                     base_amount,
6327                     rounding_amt,
6328                     quantity_invoiced,
6329                     unit_meas_lookup_code,
6330                     unit_price,
6331                     discarded_flag,
6332                     cancelled_flag,
6333                     income_tax_region,
6334                     type_1099,
6335                     corrected_inv_id,
6336                     corrected_line_number,
6337                     po_header_id,
6338                     po_line_id,
6339                     po_release_id,
6340                     po_line_location_id,
6341                     po_distribution_id,
6342                     rcv_transaction_id,
6343                     final_match_flag,
6344                     assets_tracking_flag,
6345                     asset_book_type_code,
6346                     asset_category_id,
6347                     project_id,
6348                     task_id,
6349                     expenditure_type,
6350                     expenditure_item_date,
6351                     expenditure_organization_id,
6352                     award_id,
6353                     awt_group_id,
6354 		    pay_awt_group_id,--bug6817107
6355                     receipt_verified_flag,
6356                     receipt_required_flag,
6357                     receipt_missing_flag,
6358                     justification,
6359                     expense_group,
6360                     start_expense_date,
6361                     end_expense_date,
6362                     receipt_currency_code,
6363                     receipt_conversion_rate,
6364                     receipt_currency_amount,
6365                     daily_amount,
6366                     web_parameter_id,
6367                     adjustment_reason,
6368                     merchant_document_number,
6369                     merchant_name,
6370                     merchant_reference,
6371                     merchant_tax_reg_number,
6372                     merchant_taxpayer_id,
6373                     country_of_supply,
6374                     credit_card_trx_id,
6375                     company_prepaid_invoice_id,
6376                     cc_reversal_flag,
6377                     attribute_category,
6378                     attribute1,
6379                     attribute2,
6380                     attribute3,
6381                     attribute4,
6382                     attribute5,
6383                     attribute6,
6384                     attribute7,
6385                     attribute8,
6386                     attribute9,
6387                     attribute10,
6388                     attribute11,
6389                     attribute12,
6390                     attribute13,
6391                     attribute14,
6392                     attribute15,
6393                     global_attribute_category,
6394                     global_attribute1,
6395                     global_attribute2,
6396                     global_attribute3,
6397                     global_attribute4,
6398                     global_attribute5,
6399                     global_attribute6,
6400                     global_attribute7,
6401                     global_attribute8,
6402                     global_attribute9,
6403                     global_attribute10,
6404                     global_attribute11,
6405                     global_attribute12,
6406                     global_attribute13,
6407                     global_attribute14,
6408                     global_attribute15,
6409                     global_attribute16,
6410                     global_attribute17,
6411                     global_attribute18,
6412                     global_attribute19,
6413                     global_attribute20,
6414                     primary_intended_use,
6415                     ship_to_location_id,
6416                     product_type,
6417                     product_category,
6418                     product_fisc_classification,
6419                     user_defined_fisc_class,
6420                     trx_business_category,
6421                     summary_tax_line_id,
6422                     tax_regime_code,
6423                     tax,
6424                     tax_jurisdiction_code,
6425                     tax_status_code,
6426                     tax_rate_id,
6427                     tax_rate_code,
6428                     tax_rate,
6429                     wfapproval_status,
6430                     pa_quantity,
6431                     SYSDATE,                   --creation_date
6432                     FND_GLOBAL.user_id,        --created_by
6433                     FND_GLOBAL.user_id,        --last_updated_by
6434                     SYSDATE,                   --last_update_date
6435                     FND_GLOBAL.conc_login_id,  --last_update_login
6436                     FND_GLOBAL.prog_appl_id,   --program_application_id
6437                     FND_GLOBAL.conc_program_id,--program_id
6438                     SYSDATE,                   -- program_update_date
6439                     FND_GLOBAL.conc_request_id --request_id
6440             FROM    ap_ppa_invoice_lines_gt
6441             WHERE   instruction_id = p_instruction_id
6442             AND     invoice_id = l_ppa_invoice_id;
6443 	     --Commented for bug#9573078.
6444            -- AND     adj_type = 'PPA';
6445       --
6446       ------------------------------------------------------------------------
6447       debug_info := 'Insert_Ppa_Invoices Step 5. Insert into '
6448                      ||'AP_INVOICE_DISTRIBUTIONS_ALL ';
6449       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
6450          AP_IMPORT_UTILITIES_PKG.Print(
6451            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
6452       END IF;
6453 
6454       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
6455           FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,debug_info);
6456       END IF;
6457       ------------------------------------------------------------------------
6458       INSERT INTO ap_invoice_distributions_all(
6459                 accounting_date,
6460                 accrual_posted_flag,
6461                 amount,
6462                 asset_book_type_code,
6463                 asset_category_id,
6464                 assets_addition_flag,
6465                 assets_tracking_flag,
6466                 attribute_category,
6467                 attribute1,
6468                 attribute10,
6469                 attribute11,
6470                 attribute12,
6471                 attribute13,
6472                 attribute14,
6473                 attribute15,
6474                 attribute2,
6475                 attribute3,
6476                 attribute4,
6477                 attribute5,
6478                 attribute6,
6479                 attribute7,
6480                 attribute8,
6481                 attribute9,
6482                 award_id,
6483                 awt_flag,
6484                 awt_group_id,
6485 		pay_awt_group_id,--bug6817107
6486                 awt_tax_rate_id,
6487                 base_amount,
6488                 batch_id,
6489                 cancellation_flag,
6490                 cash_posted_flag,
6491                 corrected_invoice_dist_id,
6492                 corrected_quantity,
6493                 country_of_supply,
6494                 description,
6495                 dist_code_combination_id,
6496                 dist_match_type,
6497                 distribution_class,
6498                 distribution_line_number,
6499                 encumbered_flag,
6500                 expenditure_item_date,
6501                 expenditure_organization_id,
6502                 expenditure_type,
6503                 final_match_flag,
6504                 global_attribute_category,
6505                 global_attribute1,
6506                 global_attribute10,
6507                 global_attribute11,
6508                 global_attribute12,
6509                 global_attribute13,
6510                 global_attribute14,
6511                 global_attribute15,
6512                 global_attribute16,
6513                 global_attribute17,
6514                 global_attribute18,
6515                 global_attribute19,
6516                 global_attribute2,
6517                 global_attribute20,
6518                 global_attribute3,
6519                 global_attribute4,
6520                 global_attribute5,
6521                 global_attribute6,
6522                 global_attribute7,
6523                 global_attribute8,
6524                 global_attribute9,
6525                 income_tax_region,
6526                 inventory_transfer_status,
6527                 invoice_distribution_id,
6528                 invoice_id,
6529                 invoice_line_number,
6530                 line_type_lookup_code,
6531                 match_status_flag,
6532                 matched_uom_lookup_code,
6533                 merchant_document_number,
6534                 merchant_name,
6535                 merchant_reference,
6536                 merchant_tax_reg_number,
6537                 merchant_taxpayer_id,
6538                 org_id,
6539                 pa_addition_flag,
6540                 pa_quantity,
6541                 period_name,
6542                 po_distribution_id,
6543                 posted_flag,
6544                 project_id,
6545                 quantity_invoiced,
6546                 rcv_transaction_id,
6547                 reversal_flag,
6548                 rounding_amt,
6549                 set_of_books_id,
6550                 task_id,
6551                 type_1099,
6552                 unit_price,
6553                 creation_date,
6554                 created_by,
6555                 last_updated_by,
6556                 last_update_date,
6557                 last_update_login,
6558                 program_application_id,
6559                 program_id,
6560                 program_update_date,
6561                 request_id,
6562 	        --Freight and Special Charges
6563 	        rcv_charge_addition_flag,
6564 		/*Bug#10416960 */
6565                charge_applicable_to_dist_id,
6566                summary_tax_line_id,
6567                detail_tax_dist_id,
6568 	       related_id)
6569          SELECT accounting_date,
6570                 accrual_posted_flag,
6571                 amount,
6572                 asset_book_type_code,
6573                 asset_category_id,
6574                 assets_addition_flag,
6575                 assets_tracking_flag,
6576                 attribute_category,
6577                 attribute1,
6578                 attribute10,
6579                 attribute11,
6580                 attribute12,
6581                 attribute13,
6582                 attribute14,
6583                 attribute15,
6584                 attribute2,
6585                 attribute3,
6586                 attribute4,
6587                 attribute5,
6588                 attribute6,
6589                 attribute7,
6590                 attribute8,
6591                 attribute9,
6592                 award_id,
6593                 awt_flag,
6594                 awt_group_id,
6595 		pay_awt_group_id,--bug6817107
6596                 awt_tax_rate_id,
6597                 base_amount,
6598                 batch_id,
6599                 cancellation_flag,
6600                 cash_posted_flag,
6601                 corrected_invoice_dist_id,
6602                 corrected_quantity,
6603                 country_of_supply,
6604                 description,
6605                 dist_code_combination_id,
6606                 dist_match_type,
6607                 distribution_class,
6608                 distribution_line_number,
6609                 encumbered_flag,
6610                 expenditure_item_date,
6611                 expenditure_organization_id,
6612                 expenditure_type,
6613                 final_match_flag,
6614                 global_attribute_category,
6615                 global_attribute1,
6616                 global_attribute10,
6617                 global_attribute11,
6618                 global_attribute12,
6619                 global_attribute13,
6620                 global_attribute14,
6621                 global_attribute15,
6622                 global_attribute16,
6623                 global_attribute17,
6624                 global_attribute18,
6625                 global_attribute19,
6626                 global_attribute2,
6627                 global_attribute20,
6628                 global_attribute3,
6629                 global_attribute4,
6630                 global_attribute5,
6631                 global_attribute6,
6632                 global_attribute7,
6633                 global_attribute8,
6634                 global_attribute9,
6635                 income_tax_region,
6636                 inventory_transfer_status,
6637 		--Bug#10416960
6638                 --ap_invoice_distributions_s.NEXTVAL, --invoice_distribution_id,
6639 		invoice_distribution_id,
6640                 invoice_id,
6641                 invoice_line_number,
6642                 line_type_lookup_code,
6643                 match_status_flag,
6644                 matched_uom_lookup_code,
6645                 merchant_document_number,
6646                 merchant_name,
6647                 merchant_reference,
6648                 merchant_tax_reg_number,
6649                 merchant_taxpayer_id,
6650                 org_id,
6651                 pa_addition_flag,
6652                 pa_quantity,
6653                 period_name,
6654                 po_distribution_id,
6655                 posted_flag,
6656                 project_id,
6657                 quantity_invoiced,
6658                 rcv_transaction_id,
6659                 reversal_flag,
6660                 rounding_amt,
6661                 set_of_books_id,
6662                 task_id,
6663                 type_1099,
6664                 unit_price,
6665                 SYSDATE,                     --creation_date,
6666                 FND_GLOBAL.user_id,          --created_by,
6667                 FND_GLOBAL.user_id,          --last_updated_by,
6668                 SYSDATE,                     --last_update_date,
6669                 FND_GLOBAL.conc_login_id,    --last_update_login,
6670                 FND_GLOBAL.prog_appl_id,     --program_application_id,
6671                 FND_GLOBAL.conc_program_id,  --program_id,
6672                 SYSDATE,                     --program_update_date,
6673                 FND_GLOBAL.conc_request_id,  --request_id,
6674 		'N',			     --rcv_charge_addition_flag
6675 		 /*Bug#10416960 */
6676                charge_applicable_to_dist_id,
6677                summary_tax_line_id,
6678                detail_tax_dist_id,
6679 	       related_id
6680            FROM ap_ppa_invoice_dists_gt
6681           WHERE instruction_id = p_instruction_id
6682             AND invoice_id = l_ppa_invoice_id;
6683 
6684           -- Bug 5525506
6685           UPDATE ap_invoices_all
6686           set invoice_amount = (select sum(amount)
6687                                 from ap_invoice_lines_all
6688                                 where invoice_id = l_ppa_invoice_id)
6689           where invoice_id = l_ppa_invoice_id;
6690       --
6691    END LOOP;
6692    CLOSE  ppa_invoices;
6693    --
6694    p_ppa_invoices_count          := l_ppa_invoices_count;
6695    p_ppa_invoices_total          := l_ppa_invoices_total;
6696 
6697    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
6698       FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_RETRO_PRICING_PKG.Insert_Ppa_Invoices(-)');
6699    END IF;
6700    --
6701    RETURN(TRUE);
6702    --
6703 EXCEPTION
6704 WHEN OTHERS THEN
6705     IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
6706       AP_IMPORT_UTILITIES_PKG.Print(
6707         AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
6708     END IF;
6709 
6710     IF (SQLCODE < 0) then
6711       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
6712         AP_IMPORT_UTILITIES_PKG.Print(AP_IMPORT_INVOICES_PKG.g_debug_switch,SQLERRM);
6713       END IF;
6714     END IF;
6715     IF ( ppa_invoices%ISOPEN ) THEN
6716         CLOSE ppa_invoices;
6717     END IF;
6718     RETURN(FALSE);
6719 
6720 END Insert_Ppa_Invoices;
6721 
6722 
6723 
6724 /*=============================================================================
6725  |  FUNCTION - Import_Retroprice_Adjustments()
6726  |
6727  |  DESCRIPTION
6728  |     Main Public procedure called from the Payables Open Interface Import
6729  |     Program ("import") which treats the records in the interface tables
6730  |     as "invoice instructions" rather than each record as an individual
6731  |     invoice. For recods with source='PPA' the program makes all necessary
6732  |     adjustments to original invoices and will create the new adjustment
6733  |     documents in Global Temp Tables.This program will then leverage the
6734  |     the import validations for the new adjustment docs in the temp tables.
6735  |     For every instruction the control will return to the Import Program
6736  |     resulting in Instruction with the status of PROCESSED or REJECTED.
6737  |     PROCESSED Instructions results in adjustment correction being made
6738  |     to the original Invoices alongwith the creation of PPA Documents.
6739  |
6740  |
6741  |  IN-PARAMETERS
6742  |   p_instruction_rec -- Record in the AP_INVOICE_INTERFACE with source=PPA
6743  |   p_base_currency_code
6744  |   p_multi_currency_flag
6745  |   p_set_of_books_id
6746  |   p_default_exchange_rate_type
6747  |   p_make_rate_mandatory_flag
6748  |   p_gl_date_from_get_info
6749  |   p_gl_date_from_receipt_flag
6750  |   p_positive_price_tolerance
6751  |   p_pa_installed
6752  |   p_qty_tolerance
6753  |   p_max_qty_ord_tolerance
6754  |   p_base_min_acct_unit
6755  |   p_base_precision
6756  |   p_chart_of_accounts_id
6757  |   p_freight_code_combination_id
6758  |   p_purch_encumbrance_flag
6759  |   p_calc_user_xrate
6760  |   p_default_last_updated_by
6761  |   p_default_last_update_login
6762  |   p_instr_status_flag -- status of the Instruction
6763  |   p_invoices_count --OUT Count of PPA Invoices Created
6764  |   p_invoices_total --OUT PPA Invoice Total --to be updated in the Inv Batch
6765  |   p_invoices_base_amt_total  --OUT PPA Invoice Total
6766  |   P_calling_sequence - Calling Sequence
6767  |
6768  |  MODIFICATION HISTORY
6769  |  Date         Author             Description of Change
6770  |  29-JUL-2003  dgulraja           Creation
6771  |
6772  *============================================================================*/
6773 FUNCTION Import_Retroprice_Adjustments(
6774            p_instruction_rec   IN     AP_IMPORT_INVOICES_PKG.r_invoice_info_rec,
6775            p_base_currency_code            IN     VARCHAR2,
6776            p_multi_currency_flag           IN     VARCHAR2,
6777            p_set_of_books_id               IN     NUMBER,
6778            p_default_exchange_rate_type    IN     VARCHAR2,
6779            p_make_rate_mandatory_flag      IN     VARCHAR2,
6780            p_gl_date_from_get_info         IN     DATE,
6781            p_gl_date_from_receipt_flag     IN     VARCHAR2,
6782            p_positive_price_tolerance      IN     NUMBER,
6783            p_pa_installed                  IN     VARCHAR2,
6784            p_qty_tolerance                 IN     NUMBER,
6785            p_max_qty_ord_tolerance         IN     NUMBER,
6786            p_base_min_acct_unit            IN     NUMBER,
6787            p_base_precision                IN     NUMBER,
6788            p_chart_of_accounts_id          IN     NUMBER,
6789            p_freight_code_combination_id   IN     NUMBER,
6790            p_purch_encumbrance_flag        IN     VARCHAR2,
6791            p_calc_user_xrate               IN     VARCHAR2,
6792            p_default_last_updated_by       IN     NUMBER,
6793            p_default_last_update_login     IN     NUMBER,
6794            p_instr_status_flag                OUT NOCOPY VARCHAR2,
6795            p_invoices_count                   OUT NOCOPY NUMBER,
6796            p_invoices_total                   OUT NOCOPY NUMBER,
6797            P_calling_sequence              IN     VARCHAR2)
6798 RETURN BOOLEAN IS
6799 
6800 CURSOR instruction_lines IS
6801 SELECT invoice_id,
6802        invoice_line_id,
6803        po_line_location_id,
6804        accounting_date,
6805        unit_price,
6806        requester_id,
6807        description,
6808        award_id,
6809        created_by
6810 FROM   ap_invoice_lines_interface
6811 WHERE  invoice_id = p_instruction_rec.invoice_id;
6812 
6813 l_invoice_id_list         AP_RETRO_PRICING_PKG.id_list_type;
6814 l_base_match_lines_list   AP_RETRO_PRICING_PKG.invoice_lines_list_type;
6815 l_instruction_lines_list  AP_RETRO_PRICING_PKG.instruction_lines_list_type;
6816 l_batch_id                NUMBER;
6817 l_invoices_count          NUMBER;
6818 l_invoices_total          NUMBER;
6819 l_invoices_base_amt_total NUMBER;
6820 
6821 debug_info                VARCHAR2(1000);
6822 current_calling_sequence  VARCHAR2(1000);
6823 
6824 Import_Retro_Adj_failure  EXCEPTION;
6825 l_instr_status_flag       VARCHAR2(1) := 'Y';
6826 l_instr_status_flag1      VARCHAR2(1) := 'Y';    --Bug5769161
6827 l_orig_invoices_valid     VARCHAR2(1);
6828 
6829 --Contract Payments: Added this cursor so as to
6830 --replace the direct updates to po_distributions/po_line_locations
6831 --with single api.
6832 
6833 l_po_ap_dist_rec               PO_AP_DIST_REC_TYPE;
6834 l_po_ap_line_loc_rec           PO_AP_LINE_LOC_REC_TYPE;
6835 l_return_status                VARCHAR2(100);
6836 l_msg_data                     VARCHAR2(4000);
6837 l_po_distribution_id	       NUMBER;
6838 l_po_line_location_id	       NUMBER;
6839 l_amount_billed		       NUMBER;
6840 l_shipment_amount_billed       NUMBER;
6841 l_uom_code		       ap_invoice_distributions_all.matched_uom_lookup_code%TYPE;
6842 l_last_update_login	       NUMBER;
6843 l_request_id		       NUMBER;
6844 l_api_name CONSTANT VARCHAR2(200) := 'Import_Retroprice_Adjustments';
6845 l_base_match_lines_rec    AP_RETRO_PRICING_PKG.invoice_lines_rec_type;  -- Bug 5525506
6846 l_ppa_exists                   VARCHAR2(1);
6847 l_seq_num_profile              VARCHAR2(1);
6848 l_existing_ppa_inv_id          NUMBER;
6849 
6850 --Introduced below variables for bug#9573078
6851 l_pc_lines_list           AP_RETRO_PRICING_PKG.invoice_lines_list_type;
6852 l_pc_lines_rec            AP_RETRO_PRICING_PKG.invoice_lines_rec_type;
6853 l_qc_lines_list           AP_RETRO_PRICING_PKG.invoice_lines_list_type;
6854 l_qc_lines_rec            AP_RETRO_PRICING_PKG.invoice_lines_rec_type;
6855 l_pc_exists               VARCHAR2(1);
6856 l_qc_exists               VARCHAR2(1);
6857 --Bug#10416960
6858 l_tax_lines_list           AP_RETRO_PRICING_PKG.invoice_lines_list_type;
6859 l_tax_lines_rec            AP_RETRO_PRICING_PKG.invoice_lines_rec_type;
6860 l_tipv_exists               VARCHAR2(1);
6861 
6862 
6863 
6864 
6865 CURSOR po_shipments IS
6866 SELECT pd.line_location_id,
6867        SUM(d.amount)
6868 FROM ap_ppa_invoice_dists_gt d,
6869      ap_ppa_invoice_lines_gt l,
6870      po_distributions_all pd
6871 --bug#9573078 Introduced new conditions to put join lines_gt
6872 --and dists_gt with line number
6873 WHERE d.instruction_id = p_instruction_rec.invoice_id
6874 AND d.po_distribution_id = pd.po_distribution_id
6875 AND pd.invoice_adjustment_flag = 'S'
6876 AND d.invoice_id = l.invoice_id
6877 AND d.invoice_line_number = l.line_number
6878 GROUP BY pd.line_location_id;
6879 
6880 
6881 CURSOR po_dists(c_po_line_location_id IN NUMBER) IS
6882 SELECT d.po_distribution_id,
6883        d.matched_uom_lookup_code,
6884        d.amount,
6885        FND_GLOBAL.conc_login_id,
6886        FND_GLOBAL.conc_request_id
6887 FROM ap_ppa_invoice_dists_gt d,
6888      ap_ppa_invoice_lines_gt l,
6889      po_distributions_all pd
6890 --bug#9573078 Introduced new conditions to put join lines_gt
6891 --and dists_gt with line number
6892 WHERE pd.line_location_id = c_po_line_location_id
6893 AND pd.invoice_adjustment_flag = 'S'
6894 AND d.po_distribution_id = pd.po_distribution_id
6895 AND d.instruction_id = p_instruction_rec.invoice_id
6896 AND d.invoice_id = l.invoice_id
6897 AND d.invoice_line_number = l.line_number;
6898 
6899 l_invoice_id NUMBER;
6900 l_invoice_amount number;
6901 l_invoice_currency_code VARCHAR2(100);
6902 l_accounting_event_id number;
6903 l_inv_amount   ap_invoices_all.invoice_amount%TYPE;  /*Bug 16450538*/
6904 
6905 BEGIN
6906    --
6907    current_calling_sequence :=
6908    'AP_RETRO_PRICING_PKG.Import_Retroprice_Adjustments<-'||P_calling_sequence;
6909 
6910    IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
6911       FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_RETRO_PRICING_PKG.Import_Retroprice_Adjustments(+)');
6912    END IF;
6913    ---------------------------------------------------------------------------
6914    -- Step 1.Check if the base matched Invoices affected by retropricing are
6915    -- Valid. Also all the Price Corrections and Quantity corrections on the
6916    --affected base match Invoices should be valid for the vendor.
6917    ---------------------------------------------------------------------------
6918    debug_info := 'Import_Retroprice_Adjustments Step 1. '
6919                   ||'Are_Original_Invoices_Valid';
6920    IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
6921       AP_IMPORT_UTILITIES_PKG.Print(
6922         AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
6923    END IF;
6924    --
6925    IF (AP_RETRO_PRICING_UTIL_PKG.Are_Original_Invoices_Valid(
6926          p_instruction_rec.invoice_id,
6927          p_instruction_rec.org_id,
6928          l_orig_invoices_valid) <> TRUE)  THEN
6929        --
6930        IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
6931            AP_IMPORT_UTILITIES_PKG.Print(
6932            AP_IMPORT_INVOICES_PKG.g_debug_switch,
6933            'Are_Original_Invoices_Valid<-' ||current_calling_sequence);
6934        END IF;
6935        RAISE Import_Retro_Adj_failure;
6936    END IF;
6937    --
6938    IF  l_orig_invoices_valid = 'N' THEN
6939    --
6940        IF (AP_IMPORT_UTILITIES_PKG.insert_rejections(
6941              'AP_INVOICES_INTERFACE',
6942               p_instruction_rec.invoice_id,
6943              'ORIGINAL INVOICE NOT VALIDATED',
6944               FND_GLOBAL.user_id,
6945               FND_GLOBAL.login_id,
6946               current_calling_sequence) <>  TRUE) THEN
6947               --
6948               IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
6949                 AP_IMPORT_UTILITIES_PKG.Print(
6950                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
6951                    'insert_rejections<- '||current_calling_sequence);
6952               END IF;
6953               Raise Import_Retro_Adj_failure;
6954        END IF; -- Insert rejections
6955        --
6956        l_instr_status_flag := 'N';
6957        --
6958    END IF;
6959    --
6960    --------------------------------------------------------------------------
6961    -- Step 2. Are there any Holds other than the Price Holds on the base
6962    -- matched Invoices(along with the PC and QC) for the vendor
6963    ------------------------------------------------------------------------
6964    debug_info := 'Import_Retroprice_Adjustments Step 2. Are_Holds_Ok';
6965    IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
6966        AP_IMPORT_UTILITIES_PKG.Print(
6967          AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
6968    END IF;
6969    --
6970    IF (AP_RETRO_PRICING_UTIL_PKG.Are_Holds_Ok(
6971           p_instruction_rec.invoice_id,
6972           p_instruction_rec.org_id,
6973           l_orig_invoices_valid) <> TRUE)  THEN
6974           --
6975          IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
6976             AP_IMPORT_UTILITIES_PKG.Print(
6977               AP_IMPORT_INVOICES_PKG.g_debug_switch,
6978               'Are_Holds_Ok<-' ||current_calling_sequence);
6979          END IF;
6980          RAISE Import_Retro_Adj_failure;
6981    END IF;
6982    --
6983    IF  l_orig_invoices_valid = 'N' THEN
6984        --Insert rejections
6985        IF (AP_IMPORT_UTILITIES_PKG.insert_rejections
6986              ('AP_INVOICES_INTERFACE',
6987               p_instruction_rec.invoice_id,
6988              'ORIGINAL INVOICE HAS A HOLD',
6989               FND_GLOBAL.user_id,
6990               FND_GLOBAL.login_id,
6991               current_calling_sequence) <>  TRUE) THEN
6992            IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
6993                 AP_IMPORT_UTILITIES_PKG.Print(
6994                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
6995                    'insert_rejections<- '||current_calling_sequence);
6996            END IF;
6997            Raise Import_Retro_Adj_failure;
6998        END IF; -- Insert rejections
6999        --
7000        l_instr_status_flag := 'N';
7001        --
7002     END IF;
7003 
7004 
7005    --------------------------------------------------------------------------
7006    -- Step 2.1. If sequence_numbering is always used, is there a sequence
7007    --           assigned to the document class of 'PO ADJ INV'
7008    --           Added for the bug5769161
7009    ------------------------------------------------------------------------
7010    debug_info := 'Import_Retroprice_Adjustments Step 2.1 Is sequence Assigned';
7011    IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
7012        AP_IMPORT_UTILITIES_PKG.Print(
7013          AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
7014    END IF;
7015    --
7016    IF (AP_RETRO_PRICING_UTIL_PKG.Is_Sequence_Assigned(
7017           'PO ADJ INV',
7018           p_instruction_rec.org_id,
7019           l_orig_invoices_valid) <> TRUE)  THEN
7020           --
7021          IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
7022             AP_IMPORT_UTILITIES_PKG.Print(
7023               AP_IMPORT_INVOICES_PKG.g_debug_switch,
7024               'Is_Sequence_Assigned<-' ||current_calling_sequence);
7025          END IF;
7026          RAISE Import_Retro_Adj_failure;
7027    END IF;
7028    --
7029    FND_PROFILE.GET('UNIQUE:SEQ_NUMBERS',l_seq_num_profile);
7030    IF  ((l_orig_invoices_valid = 'N') and (l_seq_num_profile = 'A')) THEN
7031        --Insert rejections
7032        IF (AP_IMPORT_UTILITIES_PKG.insert_rejections
7033              ('AP_INVOICES_INTERFACE',
7034               p_instruction_rec.invoice_id,
7035              'NO SEQUENCE DEFINED FOR PPA',
7036               FND_GLOBAL.user_id,
7037               FND_GLOBAL.login_id,
7038               current_calling_sequence) <>  TRUE) THEN
7039            IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
7040                 AP_IMPORT_UTILITIES_PKG.Print(
7041                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
7042                    'insert_rejections<- '||current_calling_sequence);
7043            END IF;
7044            Raise Import_Retro_Adj_failure;
7045        END IF; -- Insert rejections
7046        --
7047        l_instr_status_flag := 'N';
7048        --
7049     END IF;
7050 
7051 
7052     -----------------------------------------------------------------------
7053     -- STEP 3. Derive Batch_id from the Batch Name(Group_id in APII ).
7054     -----------------------------------------------------------------------
7055     debug_info := 'Import_Retroprice_Adjustments Step 3. Derive Batch_Id';
7056     IF l_instr_status_flag = 'Y'  THEN
7057       debug_info := 'Import_Retroprice_Adjustments 3. Derive Batch Id, p_instruction_rec.group_id is'||p_instruction_rec.group_id;
7058       IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
7059          AP_IMPORT_UTILITIES_PKG.Print(
7060            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
7061       END IF;
7062       --
7063       --Bugfix:4681253
7064       l_batch_id := TO_NUMBER(substr(p_instruction_rec.group_id, 5));
7065       --
7066     END IF;
7067 
7068     --------------------------------------------------------------------------
7069     -- STEP 4. Get Instruction Lines(Retropricing Affected
7070     -- Shipment Lines) for the vendor
7071     --------------------------------------------------------------------------
7072     debug_info := 'Import_Retroprice_Adjustments Step 4.Open instruction_lines';
7073     IF l_instr_status_flag = 'Y' THEN
7074       IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
7075          AP_IMPORT_UTILITIES_PKG.Print(
7076            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
7077       END IF;
7078 
7079       OPEN instruction_lines;
7080       FETCH instruction_lines
7081       BULK COLLECT INTO  l_instruction_lines_list;
7082       CLOSE instruction_lines;
7083 
7084       FOR i IN 1..l_instruction_lines_list.COUNT
7085       LOOP
7086         ----------------------------------------------------------------------
7087         -- STEP 4.1. Get Base Match Lines for the instruction_line(Shipment)
7088         ----------------------------------------------------------------------
7089         debug_info := 'Import_Retroprice_Adjustments Step 4.1. Get Base Match Lines';
7090         IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
7091            AP_IMPORT_UTILITIES_PKG.Print(
7092            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
7093         END IF;
7094 
7095         IF (AP_RETRO_PRICING_UTIL_PKG.Get_Base_Match_Lines(
7096                p_instruction_rec.invoice_id,
7097                l_instruction_lines_list(i).invoice_line_id,
7098                l_base_match_lines_list,
7099                current_calling_sequence) <> TRUE) THEN
7100           --
7101           IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
7102                 AP_IMPORT_UTILITIES_PKG.Print(
7103                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
7104                    'Get_Base_Match_Lines<- '||current_calling_sequence);
7105            END IF;
7106            Raise Import_Retro_Adj_failure;
7107           --
7108         END IF;
7109         --
7110         ----------------------------------------------------------------------
7111         -- STEP 4.2 Process Retroprice Adjustments for the affected Shipment
7112         ----------------------------------------------------------------------
7113         debug_info := 'Import_Retroprice_Adjustments Step 4.2.'
7114                        ||' Process_Retroprice_Adjustments ';
7115         IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
7116            AP_IMPORT_UTILITIES_PKG.Print(
7117            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
7118         END IF;
7119         --
7120 
7121         IF (AP_RETRO_PRICING_PKG.Process_Retroprice_Adjustments(
7122                p_base_currency_code,
7123                l_base_match_lines_list,
7124                p_instruction_rec,
7125                l_instruction_lines_list(i),
7126                l_batch_id,
7127                current_calling_sequence) <> TRUE) THEN
7128           --
7129           IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
7130                 AP_IMPORT_UTILITIES_PKG.Print(
7131                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
7132                    'Process_Retroprice_Adjustments<- '||current_calling_sequence);
7133           END IF;
7134           Raise Import_Retro_Adj_failure;
7135           --
7136         END IF;
7137         --
7138         debug_info := 'Import_Retroprice_Adjustments Step 4.3. Clear Header PL/SQL table';
7139         l_base_match_lines_list.DELETE;
7140         --
7141       END LOOP; --instr lines
7142       --
7143       debug_info := 'Import_Retroprice_Adjustments Step 4.4. Clear Header PL/SQL table';
7144       -- Bug 9010175 - commented l_instruction_lines_list.DELETE as we'll use
7145       -- it again to Insert Zero Amount Adjustments for Original Invoices
7146       --
7147       -- l_instruction_lines_list.DELETE;
7148       --
7149     END IF;  --instr_status_flag
7150 
7151     -----------------------------------------------------------------------
7152     -- STEP 5. Update Invoice Totals for PPA Documents created in the Temp
7153     --         tables.
7154     -----------------------------------------------------------------------
7155     debug_info := 'Import_Retroprice_Adjustments 5. Update Invoice Amounts, p_instruction_rec.invoice_id is'||p_instruction_rec.invoice_id;
7156     IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
7157          AP_IMPORT_UTILITIES_PKG.Print(
7158            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
7159     END IF;
7160 
7161     UPDATE AP_ppa_invoices_gt H
7162        SET invoice_amount = AP_RETRO_PRICING_UTIL_PKG.get_invoice_amount(
7163                                  invoice_id,
7164                                  invoice_currency_code)
7165      WHERE instruction_id = p_instruction_rec.invoice_id;
7166 
7167     --------------------------------------------------------------------------
7168     -- STEP 6. Validate PPA Invoices
7169     --------------------------------------------------------------------------
7170     debug_info := 'Import_Retroprice_Adjustments 6. Validate_Temp_Ppa_Invoices';
7171     IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
7172          AP_IMPORT_UTILITIES_PKG.Print(
7173            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
7174     END IF;
7175 
7176     IF  (Validate_Temp_Ppa_Invoices(
7177             p_instruction_rec.invoice_id,
7178             p_base_currency_code,
7179             p_multi_currency_flag,
7180             p_set_of_books_id,
7181             p_default_exchange_rate_type,
7182             p_make_rate_mandatory_flag,
7183             p_gl_date_from_get_info,
7184             p_gl_date_from_receipt_flag,
7185             p_positive_price_tolerance,
7186             p_pa_installed,
7187             p_qty_tolerance,
7188             p_max_qty_ord_tolerance,
7189             p_base_min_acct_unit,
7190             p_base_precision,
7191             p_chart_of_accounts_id,
7192             p_freight_code_combination_id,
7193             p_purch_encumbrance_flag,
7194             p_calc_user_xrate,
7195             p_default_last_updated_by,
7196             p_default_last_update_login,
7197             l_instr_status_flag1,            --Bug5769161
7198             current_calling_sequence)  <> TRUE) THEN
7199        --
7200        IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
7201            AP_IMPORT_UTILITIES_PKG.Print(
7202            AP_IMPORT_INVOICES_PKG.g_debug_switch,
7203              'Validate_Temp_Ppa_Invoices<- '||current_calling_sequence);
7204        END IF;
7205        RAISE Import_Retro_Adj_failure;
7206        --
7207     END IF;
7208 
7209     --Bug5769161
7210     --Updating the value of the flag l_instr_status_flag
7211     --on the basis of the value returned from the validation
7212     --of temporary ppa invoices. This has to added to take care
7213     --of the case in which any of the checks in steps
7214     --1, 2 or 2.1 fail
7215 
7216     IF l_instr_status_flag1 = 'N' then
7217        l_instr_status_flag := l_instr_status_flag1;
7218     END IF;
7219 
7220     ----------------------------------------------------------------------------
7221     -- STEP 7. Update insr_status in the Global Temp Table
7222     ----------------------------------------------------------------------------
7223     debug_info := 'l_instr_status_flag: '||l_instr_status_flag;
7224     IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
7225          AP_IMPORT_UTILITIES_PKG.Print(
7226            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
7227     END IF;
7228 
7229 
7230     debug_info := 'Import_Retroprice_Adjustments 8. Update Instruction Status';
7231     IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
7232          AP_IMPORT_UTILITIES_PKG.Print(
7233            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
7234     END IF;
7235 
7236     p_instr_status_flag := l_instr_status_flag;
7237 
7238     IF l_instr_status_flag = 'Y' THEN
7239        UPDATE ap_ppa_invoices_gt
7240           SET instr_status_flag = 'Y'
7241         WHERE instruction_id = p_instruction_rec.invoice_id;
7242     ELSE
7243        UPDATE ap_ppa_invoices_gt
7244           SET instr_status_flag = 'N'
7245         WHERE instruction_id = p_instruction_rec.invoice_id;
7246     END IF;
7247 
7248     --------------------------------------------------------------------------
7249     -- STEP 8. Discard all lines for a PPA Header if they add up to zero for
7250     -- a shipment line(There is only one PPA for a Shipment)
7251     --------------------------------------------------------------------------
7252     debug_info := 'Import_Retroprice_Adjustments 8. Discard PPA lines if SUM=0';
7253     IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
7254          AP_IMPORT_UTILITIES_PKG.Print(
7255            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
7256     END IF;
7257 
7258     -- Bug 5469166. Uncomment the following section. In case of wash there is no
7259     -- need to create PPA invoice
7260     IF l_instr_status_flag = 'Y' THEN
7261       --
7262       debug_info := 'Import_Retroprice_Adjustments 8.1. delete from '
7263                     ||'ap_ppa_invoices_gt';
7264        DELETE FROM ap_ppa_invoices_gt apig
7265        WHERE  instruction_id = p_instruction_rec.invoice_id
7266 	 --bug#9573078
7267 	 --AND invoice_amount = 0
7268 	 AND NOT EXISTS (select 'No lines'
7269                          from ap_ppa_invoice_lines_gt apilg
7270                          where apilg.invoice_id = apig.invoice_id
7271 			  and nvl(amount,0) <> 0)
7272         RETURNING invoice_id
7273         BULK COLLECT INTO l_invoice_id_list;
7274 
7275       debug_info := 'Import_Retroprice_Adjustments 8.2. delete from '
7276                      ||'ap_ppa_invoice_lines_gt';
7277       FORALL i IN  l_invoice_id_list.FIRST..l_invoice_id_list.LAST
7278         DELETE FROM ap_ppa_invoice_lines_gt
7279          WHERE invoice_id = l_invoice_id_list(i)
7280            AND instruction_id = p_instruction_rec.invoice_id;
7281 
7282       debug_info := 'Import_Retroprice_Adjustments 8.3. delete from '
7283                      ||'ap_ppa_invoice_dists_gt';
7284       FORALL i IN  l_invoice_id_list.FIRST..l_invoice_id_list.LAST
7285         DELETE FROM ap_ppa_invoice_dists_gt D
7286          WHERE invoice_id = l_invoice_id_list(i)
7287            AND instruction_id = p_instruction_rec.invoice_id;
7288       --
7289     END IF;
7290 
7291     -------------------------------------------------------------------------
7292     -- STEP 9. Insert Zero Amount Adjustments for Original Invoices
7293     -------------------------------------------------------------------------
7294     debug_info := 'Import_Retroprice_Adjustments 9.0. '||
7295                  ' Processing for base match line zero adjustment';
7296     IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
7297          AP_IMPORT_UTILITIES_PKG.Print(
7298            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
7299     END IF;
7300 
7301   FOR i IN 1..l_instruction_lines_list.COUNT
7302   LOOP
7303         -- Bug 9010175 - Added outer loop over l_instruction_lines_list and
7304         -- call to AP_RETRO_PRICING_UTIL_PKG.Get_Base_Match_Lines to fetch
7305         -- l_base_match_lines_list for each instruction lines list.
7306         ----------------------------------------------------------------------
7307         -- STEP 9.1. Get Base Match Lines for the instruction_line(Shipment)
7308         ----------------------------------------------------------------------
7309         debug_info := 'Import_Retroprice_Adjustments Step 9.1. Get Base Match
7310 Lines';
7311         IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
7312            AP_IMPORT_UTILITIES_PKG.Print(
7313            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
7314         END IF;
7315 
7316         IF (AP_RETRO_PRICING_UTIL_PKG.Get_Base_Match_Lines(
7317                p_instruction_rec.invoice_id,
7318                l_instruction_lines_list(i).invoice_line_id,
7319                l_base_match_lines_list,
7320                current_calling_sequence) <> TRUE) THEN
7321           --
7322           IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
7323                 AP_IMPORT_UTILITIES_PKG.Print(
7324                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
7325                    'Get_Base_Match_Lines<- '||current_calling_sequence);
7326            END IF;
7327            Raise Import_Retro_Adj_failure;
7328           --
7329         END IF;
7330 
7331 
7332     FOR i IN 1..l_base_match_lines_list.COUNT
7333     LOOP
7334     --
7335     l_base_match_lines_rec := l_base_match_lines_list(i);
7336 
7337     debug_info := 'Import_Retroprice_Adjustments 9.1 Check PPA already exists';
7338     IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
7339          AP_IMPORT_UTILITIES_PKG.Print(
7340            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
7341     END IF;
7342 
7343     IF (AP_RETRO_PRICING_UTIL_PKG.ppa_already_exists(
7344           l_base_match_lines_rec.invoice_id,
7345           l_base_match_lines_rec.line_number,
7346           l_ppa_exists,         --OUT
7347           l_existing_ppa_inv_id --OUT
7348           ) <> TRUE) THEN
7349 
7350         IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
7351            AP_IMPORT_UTILITIES_PKG.Print(
7352                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
7353                    'ppa_already_exists<- '||current_calling_sequence);
7354         END IF;
7355         Raise Import_Retro_Adj_failure;
7356      END IF;
7357 
7358     debug_info := 'Import_Retroprice_Adjustments 9.2 Zero Amount Adjustments';
7359     IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
7360          AP_IMPORT_UTILITIES_PKG.Print(
7361            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
7362     END IF;
7363 
7364     IF  (l_instr_status_flag = 'Y' AND
7365          l_ppa_exists = 'N')  THEN   -- Bug 5525506
7366       --  my current test code1
7367       IF (Insert_Zero_Amt_Adjustments(
7368                --p_instruction_rec.invoice_id, --Commented for bug#9573078
7369 		 l_base_match_lines_rec.invoice_id, --bug#9573078
7370                  l_base_match_lines_rec.line_number, --bug#9573078
7371 		  --Bug#15996840
7372 		 l_base_match_lines_rec.invoice_id,
7373                  l_base_match_lines_rec.line_number,
7374                  current_calling_sequence) <> TRUE) THEN
7375          --
7376         IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
7377                 AP_IMPORT_UTILITIES_PKG.Print(
7378                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
7379                    'Insert_Zero_Amt_Adjustments<- '||current_calling_sequence);
7380         END IF;
7381         Raise Import_Retro_Adj_failure;
7382         --
7383       END IF;
7384       --Bug#10416960
7385         ----------------------------------------------------------------------
7386          debug_info := 'Import_Retroprice_Adjustments 9.3 Zero Amount Adjustments';
7387          IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
7388           AP_IMPORT_UTILITIES_PKG.Print(
7389            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
7390         END IF;
7391         ----------------------------------------------------------------------
7392         IF (AP_RETRO_PRICING_UTIL_PKG.Tipv_Exists(
7393               l_base_match_lines_rec.invoice_id,
7394               l_base_match_lines_rec.line_number,
7395               l_tax_lines_list,
7396               l_tipv_exists) <> TRUE) THEN
7397            --
7398            IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
7399                AP_IMPORT_UTILITIES_PKG.Print(
7400                AP_IMPORT_INVOICES_PKG.g_debug_switch,
7401               'Tipv_Exists<- '||current_calling_sequence);
7402            END IF;
7403            --
7404            Raise Import_Retro_Adj_failure;
7405            --
7406         END IF;
7407 
7408         IF  l_tipv_exists = 'Y' THEN
7409 
7410          FOR i in 1..l_tax_lines_list.COUNT  LOOP
7411             l_tax_lines_rec :=  l_tax_lines_list(i);
7412 
7413             -----------------------------------------------------------------
7414             debug_info := 'Import_Retroprice_Adjustments 9.4 Zero Amount Adjustments';
7415             IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
7416                AP_IMPORT_UTILITIES_PKG.Print(
7417                  AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
7418             END IF;
7419             -----------------------------------------------------------------
7420              IF (Insert_Zero_Amt_Adjustments(
7421                 l_tax_lines_rec.invoice_id,
7422                 l_tax_lines_rec.line_number,
7423 		--Bug#15996840
7424 		l_base_match_lines_rec.invoice_id,
7425                 l_base_match_lines_rec.line_number,
7426                 current_calling_sequence) <> TRUE) THEN
7427 
7428               IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
7429                 AP_IMPORT_UTILITIES_PKG.Print(
7430                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
7431                    'Insert_Zero_Amt_Adjustments<- '||current_calling_sequence);
7432               END IF;
7433               Raise Import_Retro_Adj_failure;
7434            END IF;
7435        END LOOP;
7436         l_tax_lines_list.DELETE; --Bug#15996840
7437      END IF;
7438       --End Bug#10416960
7439       --Commented below code for bug#9573078 and introduced
7440        --at below location, out of the loop
7441 
7442       --Bug5485084 added following loop to create events
7443     /*  FOR invs in ( SELECT distinct apilg.invoice_id
7444                       FROM ap_ppa_invoice_lines_gt apilg
7445                      WHERE apilg.instruction_id = p_instruction_rec.invoice_id
7446 		     --Bug:9926348 added NOT exists to exclude PPA invoice
7447                       and not exists (select apig.invoice_id
7448                                       from ap_ppa_invoices_gt apig
7449                                       where apig.invoice_id = apilg.invoice_id))
7450       loop
7451 
7452       AP_Accounting_Events_Pkg.Create_Events(
7453             p_event_type => 'INVOICES',
7454             p_doc_type => NULL,
7455             p_doc_id => invs.invoice_id,
7456             p_accounting_date => NULL,
7457             p_accounting_event_id => l_accounting_event_id,
7458             p_checkrun_name => NULL,
7459             p_calling_sequence => current_calling_sequence);
7460       end loop; */
7461       --End bug#9573078
7462 
7463 
7464      --Start bug#9573078
7465     --Introduced below code to insert all adjustment lines/dists of base match lines
7466     --price and quantity corrections, in corresponding actual invoices
7467 
7468     debug_info := 'Import_Retroprice_Adjustments Step 9.4. Get price corrections for base match line';
7469     IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
7470            AP_IMPORT_UTILITIES_PKG.Print(
7471            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
7472      END IF;
7473 
7474     IF (AP_RETRO_PRICING_UTIL_PKG.Corrections_exists(
7475               l_base_match_lines_rec.invoice_id,
7476               l_base_match_lines_rec.line_number,
7477               'PRICE_CORRECTION',
7478               l_pc_lines_list,
7479               l_pc_exists) <> TRUE) THEN
7480 
7481           IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
7482               AP_IMPORT_UTILITIES_PKG.Print(
7483                AP_IMPORT_INVOICES_PKG.g_debug_switch,
7484               'Corrections_exists<- '||current_calling_sequence);
7485           END IF;
7486 
7487           Raise Import_Retro_Adj_failure;
7488 
7489     END IF;
7490     IF  (l_pc_exists = 'Y') THEN
7491       FOR i in 1..l_pc_lines_list.COUNT  LOOP
7492 
7493 	       l_pc_lines_rec :=  l_pc_lines_list(i);
7494          IF (Insert_Zero_Amt_Adjustments(
7495                 l_pc_lines_rec.invoice_id,
7496                 l_pc_lines_rec.line_number,
7497 		--Bug#15996840
7498                  l_pc_lines_rec.invoice_id,
7499                  l_pc_lines_rec.line_number,
7500                 current_calling_sequence) <> TRUE) THEN
7501 
7502               IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
7503                 AP_IMPORT_UTILITIES_PKG.Print(
7504                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
7505                    'Insert_Zero_Amt_Adjustments<- '||current_calling_sequence);
7506               END IF;
7507               Raise Import_Retro_Adj_failure;
7508            END IF;
7509 
7510            --Bug#10416960
7511              IF (AP_RETRO_PRICING_UTIL_PKG.Tipv_Exists(
7512                   l_pc_lines_rec.invoice_id,
7513                   l_pc_lines_rec.line_number,
7514                   l_tax_lines_list,
7515                   l_tipv_exists) <> TRUE) THEN
7516                --
7517                IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
7518                    AP_IMPORT_UTILITIES_PKG.Print(
7519                    AP_IMPORT_INVOICES_PKG.g_debug_switch,
7520                   'Tipv_Exists<- '||current_calling_sequence);
7521                END IF;
7522                --
7523                Raise Import_Retro_Adj_failure;
7524                --
7525            END IF;
7526 
7527            IF  l_tipv_exists = 'Y' THEN
7528              FOR i in 1..l_tax_lines_list.COUNT  LOOP
7529             l_tax_lines_rec :=  l_tax_lines_list(i);
7530 
7531             -----------------------------------------------------------------
7532             debug_info := 'Import_Retroprice_Adjustments 9.4 Zero Amount Adjustments';
7533             IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
7534                AP_IMPORT_UTILITIES_PKG.Print(
7535                  AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
7536             END IF;
7537             -----------------------------------------------------------------
7538              IF (Insert_Zero_Amt_Adjustments(
7539                 l_tax_lines_rec.invoice_id,
7540                 l_tax_lines_rec.line_number,
7541 		--Bug#15996840
7542 		l_pc_lines_rec.invoice_id,
7543                 l_pc_lines_rec.line_number,
7544                 current_calling_sequence) <> TRUE) THEN
7545 
7546               IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
7547                 AP_IMPORT_UTILITIES_PKG.Print(
7548                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
7549                    'Insert_Zero_Amt_Adjustments<- '||current_calling_sequence);
7550               END IF;
7551               Raise Import_Retro_Adj_failure;
7552            END IF;
7553        END LOOP;
7554        l_tax_lines_list.DELETE; --Bug#15996840
7555         END IF;
7556         --Bug#10416960
7557 
7558          END LOOP;
7559     END IF;
7560 
7561 debug_info := 'Import_Retroprice_Adjustments Step 9.5. Get Quantity corrections for base match line';
7562     IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
7563            AP_IMPORT_UTILITIES_PKG.Print(
7564            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
7565     END IF;
7566 
7567     IF (AP_RETRO_PRICING_UTIL_PKG.Corrections_exists(
7568               l_base_match_lines_rec.invoice_id,
7569               l_base_match_lines_rec.line_number,
7570               'QTY_CORRECTION',
7571               l_qc_lines_list,
7572               l_qc_exists) <> TRUE) THEN
7573 
7574          IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
7575             AP_IMPORT_UTILITIES_PKG.Print(
7576              AP_IMPORT_INVOICES_PKG.g_debug_switch,
7577              'Corrections_exists<- '||current_calling_sequence);
7578         END IF;
7579 
7580         Raise Import_Retro_Adj_failure;
7581     END IF;
7582 
7583     IF  (l_qc_exists = 'Y') THEN
7584        FOR i in 1..l_qc_lines_list.COUNT LOOP
7585            l_qc_lines_rec :=  l_qc_lines_list(i);
7586 
7587        IF (Insert_Zero_Amt_Adjustments(
7588                 l_qc_lines_rec.invoice_id,
7589                 l_qc_lines_rec.line_number,
7590 		--Bug#15996840
7591 		l_qc_lines_rec.invoice_id,
7592                 l_qc_lines_rec.line_number,
7593                 current_calling_sequence) <> TRUE) THEN
7594 
7595               IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
7596                 AP_IMPORT_UTILITIES_PKG.Print(
7597                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
7598                    'Insert_Zero_Amt_Adjustments<- '||current_calling_sequence);
7599               END IF;
7600               Raise Import_Retro_Adj_failure;
7601            END IF;
7602         --Bug#10416960
7603              IF (AP_RETRO_PRICING_UTIL_PKG.Tipv_Exists(
7604                   l_qc_lines_rec.invoice_id,
7605                   l_qc_lines_rec.line_number,
7606                   l_tax_lines_list,
7607                   l_tipv_exists) <> TRUE) THEN
7608                --
7609                IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
7610                    AP_IMPORT_UTILITIES_PKG.Print(
7611                    AP_IMPORT_INVOICES_PKG.g_debug_switch,
7612                   'Tipv_Exists<- '||current_calling_sequence);
7613                END IF;
7614                --
7615                Raise Import_Retro_Adj_failure;
7616                --
7617            END IF;
7618 
7619            IF  l_tipv_exists = 'Y' THEN
7620              FOR i in 1..l_tax_lines_list.COUNT  LOOP
7621             l_tax_lines_rec :=  l_tax_lines_list(i);
7622 
7623             -----------------------------------------------------------------
7624             debug_info := 'Import_Retroprice_Adjustments 9.4 Zero Amount Adjustments';
7625             IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
7626                AP_IMPORT_UTILITIES_PKG.Print(
7627                  AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
7628             END IF;
7629             -----------------------------------------------------------------
7630              IF (Insert_Zero_Amt_Adjustments(
7631                 l_tax_lines_rec.invoice_id,
7632                 l_tax_lines_rec.line_number,
7633 		--Bug#15996840
7634 		l_qc_lines_rec.invoice_id,
7635                 l_qc_lines_rec.line_number,
7636                 current_calling_sequence) <> TRUE) THEN
7637 
7638               IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
7639                 AP_IMPORT_UTILITIES_PKG.Print(
7640                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
7641                    'Insert_Zero_Amt_Adjustments<- '||current_calling_sequence);
7642               END IF;
7643               Raise Import_Retro_Adj_failure;
7644            END IF;
7645        END LOOP;
7646         l_tax_lines_list.DELETE; --Bug#15996840
7647         END IF;
7648         --Bug#10416960
7649        END LOOP;
7650     END IF;
7651     --End bug#9573078
7652 
7653     END IF; -- instr_status_flag and ppa_exists check
7654 
7655     END LOOP; -- end loop over l_base_match_lines_list
7656   END LOOP; -- end loop over l_instruction_lines_list -- bug 9010175
7657 
7658     ---------------------------------------------------------------------------
7659     -- STEP 10. Insert PPA Invoices
7660     ---------------------------------------------------------------------------
7661     debug_info := 'Import_Retroprice_Adjustments 10. Insert_Ppa_Invoices l_instr_status_flag is '||l_instr_status_flag;
7662     IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
7663          AP_IMPORT_UTILITIES_PKG.Print(
7664            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
7665     END IF;
7666 
7667     IF l_instr_status_flag = 'Y' THEN
7668       --
7669       IF (Insert_Ppa_Invoices(
7670                 p_instruction_rec.invoice_id,
7671                 p_invoices_count,
7672                 p_invoices_total,
7673                 current_calling_sequence) <> TRUE) THEN
7674         --
7675         IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
7676                 AP_IMPORT_UTILITIES_PKG.Print(
7677                   AP_IMPORT_INVOICES_PKG.g_debug_switch,
7678                    'Insert_Ppa_Invoices<- '||current_calling_sequence);
7679         END IF;
7680         Raise Import_Retro_Adj_failure;
7681         --
7682       END IF;
7683 
7684        --Bug#16022947. Update total_tax_amount, self_assessed_tax_amount
7685        --on new PPA invoices
7686       FOR invs in(SELECT distinct invoice_id
7687                     FROM ap_ppa_invoices_gt apig
7688                     WHERE apig.instruction_id = p_instruction_rec.invoice_id)
7689       LOOP
7690        /*Bug 16450538 Start*/
7691 	  SELECT NVL(sum(NVL(ail.amount,0)),0)
7692 	    INTO l_inv_amount
7693                     FROM ap_invoice_lines_all ail
7694                     WHERE ail.invoice_id = invs.invoice_id
7695                       AND line_type_lookup_code ='RETROTAX';
7696 
7697        /*Bug 16450538 End*/
7698         --We do not have self assessed tax on PPA invoices.
7699 	--hence updating only total tax amount.
7700 
7701         UPDATE ap_invoices_all ai
7702         SET ai.total_tax_amount =
7703                    (l_inv_amount   /*For Bug 16450538*/
7704                      +
7705                     (SELECT
7706 		     NVL(SUM(DECODE(NVL(zls.self_assessed_flag, 'N'),
7707                                'N',
7708 		               case when exists (SELECT 'Prepay App Exists'
7709                                        FROM ap_invoice_lines_all prepay
7710                                        WHERE prepay.invoice_id = zls.trx_id
7711                                          AND prepay.line_type_lookup_code = 'PREPAY'
7712                                          AND prepay.prepay_invoice_id  = zls.applied_from_trx_id
7713                                          AND prepay.prepay_line_number = zls.applied_from_line_id
7714                                          AND prepay.invoice_includes_prepay_flag = 'Y'
7715                                          AND (prepay.discarded_flag is null
7716                                            or prepay.discarded_flag = 'N')) THEN
7717                                      0
7718                                 ELSE NVL(zls.tax_amt, 0)
7719 		                end,
7720                                 0)),0)
7721 		      FROM zx_lines_summary zls
7722                       WHERE zls.application_id = 200
7723                         AND zls.entity_code = 'AP_INVOICES'
7724                         AND zls.event_class_code IN
7725                           ('STANDARD INVOICES', 'PREPAYMENT INVOICES', 'EXPENSE REPORTS')
7726                         AND zls.trx_id   = ai.invoice_id
7727                         AND NVL(zls.reporting_only_flag, 'N') = 'N'))
7728         WHERE ai.invoice_id = invs.invoice_id;
7729 
7730       END LOOP;
7731       --End Bug#16022947
7732 
7733     --Introduced below code for bug#9573078 and commented
7734     --at above location, inside loop
7735 
7736      --Bug5485084 added following loop to create events
7737       FOR invs in(  SELECT distinct invoice_id
7738                     FROM ap_ppa_invoice_lines_gt apilg
7739                     WHERE apilg.instruction_id = p_instruction_rec.invoice_id
7740                      and not exists (select invoice_id
7741                                      from ap_ppa_invoices_gt apig
7742                                      where apig.invoice_id = apilg.invoice_id))
7743       LOOP
7744 
7745       AP_Accounting_Events_Pkg.Create_Events(
7746             p_event_type => 'INVOICES',
7747             p_doc_type => NULL,
7748             p_doc_id => invs.invoice_id,
7749             p_accounting_date => NULL,
7750             p_accounting_event_id => l_accounting_event_id,
7751             p_checkrun_name => NULL,
7752             p_calling_sequence => current_calling_sequence);
7753       END LOOP;
7754       --End bug#9573078
7755 
7756     END IF;
7757 
7758     --------------------------------------------------------------------------
7759     -- STEP 10. Update Amount, Invoice Adjustment Flag in PO Distributions
7760     --------------------------------------------------------------------------
7761     debug_info := 'Import_Retroprice_Adjustments 11. Update PO_DISTRIBUTIONS';
7762     IF AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y' THEN
7763          AP_IMPORT_UTILITIES_PKG.Print(
7764            AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
7765     END IF;
7766 
7767 
7768 
7769     IF l_instr_status_flag = 'Y' THEN
7770 
7771        OPEN po_shipments;
7772 
7773        LOOP
7774 
7775           FETCH po_shipments INTO l_po_line_location_id,
7776 			       l_shipment_amount_billed;
7777 
7778           EXIT WHEN po_shipments%NOTFOUND;
7779 
7780           l_po_ap_dist_rec := PO_AP_DIST_REC_TYPE.create_object();
7781 
7782           OPEN po_dists(l_po_line_location_id);
7783 
7784           LOOP
7785 
7786              FETCH po_dists INTO l_po_distribution_id,
7787 			   l_uom_code,
7788 			   l_amount_billed,
7789 			   l_last_update_login,
7790 			   l_request_id;
7791              EXIT WHEN po_dists%NOTFOUND;
7792 
7793 
7794              l_po_ap_dist_rec.add_change(p_po_distribution_id => l_po_distribution_id,
7795                                 p_uom_code           => l_uom_code,
7796                                 p_quantity_billed    => NULL,
7797                                 p_amount_billed      => l_amount_billed,
7798                                 p_quantity_financed  => NULL,
7799                                 p_amount_financed    => NULL,
7800                                 p_quantity_recouped  => NULL,
7801                                 p_amount_recouped    => NULL,
7802                                 p_retainage_withheld_amt => NULL,
7803                                 p_retainage_released_amt => NULL,
7804 				p_last_update_login  => l_last_update_login,
7805 				p_request_id	     => l_request_id);
7806 
7807           END LOOP;
7808 
7809           CLOSE po_dists;
7810 
7811           l_po_ap_line_loc_rec := PO_AP_LINE_LOC_REC_TYPE.create_object(
7812                                  p_po_line_location_id => l_po_line_location_id,
7813                                  p_uom_code            => l_uom_code,
7814                                  p_quantity_billed     => NULL,
7815                                  p_amount_billed       => l_shipment_amount_billed,
7816                                  p_quantity_financed  => NULL,
7817                                  p_amount_financed    => NULL,
7818                                  p_quantity_recouped  => NULL,
7819                                  p_amount_recouped    => NULL,
7820                                  p_retainage_withheld_amt => NULL,
7821                                  p_retainage_released_amt => NULL,
7822 				 p_last_update_login  => l_last_update_login,
7823 				 p_request_id	      => l_request_id
7824                                 );
7825 
7826           PO_AP_INVOICE_MATCH_GRP.Update_Document_Ap_Values(
7827                                         P_Api_Version => 1.0,
7828                                         P_Line_Loc_Changes_Rec => l_po_ap_line_loc_rec,
7829                                         P_Dist_Changes_Rec     => l_po_ap_dist_rec,
7830                                         X_Return_Status        => l_return_status,
7831                                         X_Msg_Data             => l_msg_data);
7832 
7833        END LOOP;
7834 
7835        CLOSE po_shipments;
7836 
7837        --bug#9573078
7838         --Introduced below UPDATE to NULL invoice_adjustment_flag
7839 	--if all process done successfully.
7840 
7841         UPDATE po_distributions_all pd
7842           SET invoice_adjustment_flag = NULL
7843         WHERE invoice_adjustment_flag = 'S'
7844           AND po_distribution_id IN (
7845                  SELECT d.po_distribution_id
7846                    FROM ap_ppa_invoice_dists_gt d,
7847                         ap_ppa_invoice_lines_gt l
7848                   WHERE d.instruction_id = p_instruction_rec.invoice_id
7849 		  AND   d.po_distribution_id = pd.po_distribution_id
7850 		  AND   l.invoice_id = d.invoice_id
7851                   AND   l.line_number = d.invoice_line_number);
7852 
7853     ELSE
7854 
7855        UPDATE po_distributions_all pd
7856           SET last_update_date  = SYSDATE,
7857               last_updated_by   = FND_GLOBAL.user_id,
7858               last_update_login = FND_GLOBAL.conc_login_id,
7859               request_id        = FND_GLOBAL.conc_request_id,
7860               invoice_adjustment_flag = 'R'
7861         WHERE invoice_adjustment_flag = 'S'
7862           AND po_distribution_id IN (
7863                  SELECT d.po_distribution_id
7864                    FROM ap_ppa_invoice_dists_gt d,
7865                         ap_ppa_invoice_lines_gt l
7866                   WHERE l.instruction_id = d.instruction_id
7867                  -- AND   l.adj_type        = 'PPA' Commented for bug#9573078
7868                   AND   d.instruction_id = p_instruction_rec.invoice_id
7869                   AND   d.po_distribution_id = pd.po_distribution_id);
7870 
7871     END IF;
7872 
7873     IF (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) THEN
7874        FND_LOG.STRING(G_LEVEL_PROCEDURE,G_MODULE_NAME||l_api_name,'AP_RETRO_PRICING_PKG.Import_Retroprice_Adjustments(-)');
7875     END IF;
7876     --
7877     RETURN (TRUE);
7878     --
7879 EXCEPTION
7880  WHEN OTHERS THEN
7881     IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') then
7882       AP_IMPORT_UTILITIES_PKG.Print(
7883         AP_IMPORT_INVOICES_PKG.g_debug_switch,debug_info);
7884 
7885       debug_info := 'In Others Exception';
7886       IF (G_LEVEL_STATEMENT >= G_CURRENT_RUNTIME_LEVEL) THEN
7887         FND_LOG.STRING(G_LEVEL_STATEMENT,G_MODULE_NAME||l_api_name,debug_info);
7888       END IF;
7889 
7890     END IF;
7891 
7892     IF (SQLCODE < 0) then
7893       IF (AP_IMPORT_INVOICES_PKG.g_debug_switch = 'Y') THEN
7894         AP_IMPORT_UTILITIES_PKG.Print(AP_IMPORT_INVOICES_PKG.g_debug_switch,SQLERRM);
7895       END IF;
7896     END IF;
7897     --
7898      --Bug#15996840
7899     IF(p_instruction_rec.invoice_id IS NOT NULL)THEN
7900      UPDATE po_distributions_all pd
7901           SET invoice_adjustment_flag = 'R'
7902         WHERE invoice_adjustment_flag = 'S'
7903           AND line_location_id IN
7904 	       (select po_line_location_id
7905                 FROM   ap_invoice_lines_interface
7906 		WHERE  invoice_id = p_instruction_rec.invoice_id);
7907     END IF;
7908 
7909     IF ( instruction_lines%ISOPEN ) THEN
7910         CLOSE instruction_lines;
7911     END IF;
7912     --
7913     RETURN(FALSE);
7914 
7915 END Import_Retroprice_Adjustments;
7916 
7917 
7918 END AP_RETRO_PRICING_PKG;