DBA Data[Home] [Help]

PACKAGE BODY: APPS.JAI_AP_MATCH_TAX_PKG

Source


1 PACKAGE BODY jai_ap_match_tax_pkg
2 /* $Header: jai_ap_match_tax.plb 120.67.12020000.5 2013/03/08 08:52:48 anupgupt ship $ */
3 AS
4 
5 /*  Bug 5401111. Added by Lakshmi Gopalsami
6  |  Created private procedure for getting Account type.
7  |  This procedure call will have performance issue. Later we need
8  |  to implement this call using caching logic.
9  */
10 FUNCTION get_gl_account_type
11  ( cp_code_combination_id IN NUMBER  )
12  RETURN VARCHAR2
13 IS
14   CURSOR get_account_type IS
15   SELECT account_type
16     FROM gl_code_combinations
17    WHERE code_combination_id = cp_code_combination_id ;
18 
19   lv_account_type gl_code_combinations.account_type%TYPE;
20 BEGIN
21  OPEN get_account_type;
22   FETCH get_account_type INTO lv_account_type;
23  CLOSE get_account_type;
24  RETURN lv_account_type;
25 EXCEPTION
26   WHEN OTHERS THEN
27     RETURN 'X'; -- if required exception can be handled.
28 END ;
29 
30 /*Removed the function check_service_interim_account from process_online and process_batch_record procedure
31   and added here for bug#6833506 by JMEENA */
32 
33  function check_service_interim_account /* Service */
34   (
35     p_accrue_on_receipt_flag     varchar2,
36     p_accounting_method_option   varchar2,
37     p_is_item_an_expense         varchar2,
38     p_service_regime_id          number,
39     p_org_id                     number,
40     p_rematch                    varchar2,
41     p_tax_type                   varchar2,
42     po_dist_id       NUMBER   --Added by JMEENA for bug#6833506
43   )
44   return number is
45 
46     lb_charge_service_interim         boolean;
47     ln_service_interim_account        number;
48 
49 
50  /*5694855..start* kunkumar for forward porting to R12*/
51                         CURSOR cur_fetch_io IS
52                         SELECT ship_to_organization_id,
53                                                  ship_to_location_id
54                                 FROM po_line_locations_all
55                          WHERE line_location_id IN ( SELECT line_location_id
56 
57                       FROM po_distributions_all
58 WHERE po_distribution_id = po_dist_id) ;
59 
60     ln_organization_id                NUMBER;
61     ln_location_id                    NUMBER;
62 
63     /*5694855..end*/
64 
65 
66 
67   begin
68 
69     /* Bug 5358788. Added by Lakshmi Gopalsami
70      * Commented out the check for p_rematch = 'PO_MATCHING'
71      */
72 
73  --added the if condition for bug#12813762
74     if p_rematch='INCLUSIVE'
75     then
76         if p_accrue_on_receipt_flag = 'N'
77           then
78   --      lb_charge_service_interim := true;/*accounting shall not be generated if the accrue on receipt flag is set to 'n',variables are assigned wrongly,11936596*/
79           lb_charge_service_interim :=false;--11936596
80       else
81        -- lb_charge_service_interim := false;
82           lb_charge_service_interim :=true; --11936596
83       end if;
84     else
85         if p_accrue_on_receipt_flag = 'N'
86           then
87            lb_charge_service_interim := true;
88       else
89            lb_charge_service_interim := false;
90 
91       end if;
92 END IF;
93 
94 
95     if lb_charge_service_interim then
96 
97       OPEN cur_fetch_io;/*5694855*/
98       FETCH cur_fetch_io INTO ln_organization_id,ln_location_id;
99       CLOSE cur_fetch_io;
100 
101       ln_service_interim_account :=
102       jai_cmn_rgm_recording_pkg.get_account
103       (
104        p_regime_id            =>      p_service_regime_id,
105   p_organization_type    =>      jai_constants.orgn_type_io,/*5694855*/
106          p_organization_id      =>      ln_organization_id,/*5694855*/
107          p_location_id          =>      ln_location_id, /*5694855*/
108 p_tax_type             =>      p_tax_type,
109        p_account_name         =>      jai_constants.recovery_interim
110       );
111 
112       return ln_service_interim_account;
113 
114     else
115       return null;
116     end if;
117 
118 END check_service_interim_account; /* Service */
119 
120 PROCEDURE process_batch
121 (
122 p_errbuf OUT NOCOPY VARCHAR2,
123 p_retcode OUT NOCOPY VARCHAR2,
124 p_org_id              IN        number, -- added by bug#3218695
125 p_process_all_org     IN        varchar2 -- added by bug#3218695
126 )
127 IS
128   v_errbuf                VARCHAR2(1996);
129   v_retcode               VARCHAR2(1996);
130   v_prev_invoice_id       NUMBER(15);
131   v_rowid                 ROWID;
132   v_error_invoice         CHAR(1);
133   error_from_called_unit  EXCEPTION;
134   v_error_mesg            VARCHAR2(200);
135   v_invoice_number        VARCHAR2(50);
136   v_org_id                number; -- added by bug#3218695
137 
138 
139 
140 /* ------------------------------------------------------------------------------------------------------------------------
141 FILE NAME     : jai_ap_match_tax_pkg.process_batch_p.sql
142 CHANGE HISTORY:
143 S.No      DATE                Author AND Details
144 ---------------------------------------------------------------------------------------------------------------------------
145 1         05-may-2002         Aparajita, Created this procedure. bug # 2359737 (enhancement for ti)
146                               This trigger was created to move the processing of invoice lines while invoices get generated
147                               from purchasing with pay on receipt option. the concurrent was getting submitted for every item
148                               lines of the invoice, to generate associate tax lines. With this procedure all such invoice lines
149                               will be processed as 1 concurrent. The temporary table used here is being populated from the
150                               trigger ja_in_tds_temp_after_insert_trg on ap_invoice_distributions_all. The main concepts
151                               used here are,
152                               - if an error occurs while processing a particular record, the program will skip processing
153                                 all other distributions lines of the error invoice.
154                               - the program will continue to process other invoices.
155                               - the program will update the temporary table for an error record, with error flag time and error
156                               - if a particular line has error flag set to 'Y', this program will not pick up that record and
157                                 also all other lines pertaining to such an error invoice.
158 
159 2.      30-oct-2003           Aparajita. bug#3218695. Version#616.1.
160                               Intrduced parameters p_org_id(current operating unit) and p_process_all_org(Y/N).
161 
162                               If p_process_all_org is Y, this program processed records of all operating units. When
163                               p_process_all_org is 'N', records pertaining to org id p_org_id is only peocessed.
164 
165 3.    08-Jun-2005             Version 116.2 jai_ap_match_tax -Object is Modified to refer to New DB Entity names in place of Old DB Entity Names
166                                as required for CASE COMPLAINCE.
167 
168 4.    22-Jun-05               rallamse bug#4448789 116.3 Pefomed LE changes by adding legal_entity_id at the required places
169 
170 5.    03-Feb-2006         avallabh for bug 4926094. Version 120.4.
171         Modified the cursor c_dist_reversal_cnt to check for rownum=1, to enhance performance.
172         Removed the default initialization of v_dist_reversal_cnt to 0 and added it just before
173         opening the cursor and assigning the value to the variable.
174 
175 
176 6.    01/11/2006    SACSETHI for bug 5228046, File version 120.8
177                           Forward porting the change in 11i bug 5365523 (Additional CVD Enhancement).
178                     This bug has datamodel and spec changes.
179 
180 7.    13-April-2007   ssawant for bug 5989740 ,File version 120.13
181                       Forward porting Budget07-08 changes of handling secondary and
182                 Higher Secondary Education Cess from 11.5( bug no 5907436) to R12 (bug no 5989740).
183           Changes were done for following files
184           ja_in_pay_on_recpt_dist_match_p.sql;
185 8.   27-jun-2007      kunkumar for made changes for bug#5593895.File version 120.14
186                        removed project changes for RUP03
187 
188 9.   05/07/2007      brathod, for Bug# 5763527, File Version 120.19
189                      FP: 11.5.9 - 12.0:FORWARD PORTING FROM 115 BUG 5763527
190 10.  16-07-2007      Changed shipment_num to shipment_number
191                              excise_inv_num to excise_inv_number in jai_cmn_lines table
192 11.   24-oct-2007     vkaranam for bug#6457733,File version 120.25
193                      Issue:
194          UNABLE TO FIND LOV VALUES IN INDIA - TO INSERT TAXES  FOR PAY ON RECEIPT"
195          Changes are done in  process_batch procedure
196 
197 12. 31-oct-2007 Bug 6595773 File version 120.7.12000000.5
198         Forward ported the changes done for bug 5416515 - AP ACCRUAL ACCOUNT IS NOT DEBITED FOR
199         VAT TAXES IN INVOICE DISTRIBUTION
200 
201 13.   20-Dec-2007   Eric modified and added code for the inclusive tax
202 
203 14.   09-JAN-2008   Jason Liu
204                     Modified for Retroactive Price
205 
206 15. 18-Feb-2010 CSahoo for bug#9386496, File Version 120.34.12010000.13
207                 Issue: TAXES ARE GETTING INSERTED WITH PO EXPENSE ACCOUNT INSTEAD OF SERVICE TAX INTERI
208                 Fix: Added NVl condition to the parameter po_dist_id in the call to the function
209                      check_service_interim_account.
210 16  07-jun-2011 vkaranam for bug#11936596
211                 Issue:  R12:POT - INCLUSIVE TAX ACCOUNTING NOT HAPPENING FOR PO MATCHED INVOICES
212                 Fix details:
213                 Following accounting entry to be passed for PO/Receipt Match Inclusive Taxes
214 at time of PO/Receipt Match event :
215 
216  The accounting entries for these Misc lines will be as follows:-
217  Misc line :  +1364      --   Interim Recovery (Interim Recovery)
218  Misc line :  -1364     --   ACC1 (AP Accrual)
219 
220  Changes are done in process_online  procedure for po_matching,rcv_matching.
221 
222 18  16-mar-2012  vkaranam for bug#12434160
223                 Issue:Tax amount appear on the PO AP Accrual Reconcilliation report if the
224   Purchase Order has multiple distribution lines.
225   Issue details:
226   Anything that is not linked to a po_distribution_id would be processed as
227 miscellaneous accrual and not as AP-PO accrual. Since po_distribution_id
228 for tax lines in AP is null in case of po with mulitple distributions, its  being processed as Miscellaneous reconciliation record.
229 Fix:
230 ap_invoice_distributions_all.po_distribution_id shall be populated with the po_distribution_id of
231 the item distribution line.
232 Changes are done in process_batch_records procedure.
233 
234 61. 25-Jan-2013    amandali for bug 16220700
235                    Issue: Discarded line has posted flag 'Y' eventhough not accounted, if the parent distribution is accounted.
236                    Fix: Updated the posted flag, cash posted flag, accrual posted flag to 'N' for the discarded lines.
237                         Also, added the updation of who columns
238 
239 Future Dependencies For the release Of this Object:-
240 ==================================================
241 (Please add a row in the section below only if your bug introduces a dependency due to spec change/
242 A new call to a object/A datamodel change )
243 
244 ------------------------------------------------------------------------------------------------------
245 Version       Bug       Dependencies (including other objects like files if any)
246 -------------------------------------------------------------------------------------------------------
247 616.1         3218695   This fix introduced two input parameters p_org_id and p_process_all_org.
248                         The associated ldt for concurrent registration is the dependency..
249 
250 
251 ------------------------------------------------------------------------------------------------------------------------ */
252 BEGIN
253 
254 
255   Fnd_File.put_line(Fnd_File.LOG, 'Start procedure - jai_ap_match_tax_pkg.process_batch');
256 
257   -- this cursor picks up the invoice lines from the temporary table but does not pick up such invoices which have
258   -- error_flag set to 'Y' for any of it's distribution lines.
259 
260   -- start added by bug#3218695
261   if p_process_all_org = 'Y' then
262     v_org_id := null;
263   else
264     v_org_id := p_org_id;
265   end if;
266   -- end added by bug#3218695
267 
268   FOR  c_rec IN
269   (
270 SELECT
271     temp.ROWID,
272     temp.invoice_id,
273     temp.invoice_line_number,
274     temp.po_distribution_id,
275     temp.quantity_invoiced,
276     temp.shipment_header_id,
277     temp.receipt_num,
278     temp.receipt_code,
279     temp.rematching,
280     temp.rcv_transaction_id,
281     temp.amount,
282     --project_id,
283     --task_id,
284     --expenditure_type,
285     --expenditure_organization_id,
286     --expenditure_item_date,
287     temp.org_id
288     /* 5763527 */
289     ,apla.project_id
290     ,apla.task_id
291     ,apla.expenditure_type
292     ,apla.expenditure_organization_id
293     ,apla.expenditure_item_date
294     /* End 5763527 */
295    FROM JAI_AP_MATCH_ERS_T          temp
296        ,ap_invoice_lines_all apla  -- 5763527
297    WHERE  temp.invoice_id NOT IN
298      (SELECT invoice_id
299       FROM   JAI_AP_MATCH_ERS_T
300       WHERE  error_flag = 'Y'
301       )
302    AND ( (v_org_id is null and mo_global.check_access(temp.org_id)='Y' )
303          OR ( (v_org_id is not null) and  (temp.org_id = v_org_id) )
304        ) ----added and mo_global.check_access(org_id)='Y' for bug#6457733, added by bug#3218695
305    /* 5763527 */
306    and  apla.invoice_id = temp.invoice_id
307    and  apla.line_number = temp.invoice_line_number
308    --and  aida.invoice_distribution_id = temp.invoice_distribution_id
309    /* End 5763527 */
310    ORDER BY  invoice_id, invoice_line_number
311   )
312   LOOP
313 
314    BEGIN
315 
316     v_rowid := c_rec.ROWID;
317     v_errbuf := NULL;
318     v_retcode := NULL;
319     v_error_mesg := NULL;
320 
321 
322     -- Check if the first record from the cursor is being processed.
323 
324     IF v_prev_invoice_id IS NULL THEN
325 
326      -- processing first invoice,
327       -- assign current to previous, no need to check change in invoice and initialize eror invoice flag.
328 
329       v_prev_invoice_id :=  c_rec.invoice_id;
330       v_error_invoice := 'N';
331 
332     ELSE
333 
334       -- not processing the first record.
335       -- check if there is a change in invoice, that is a new invoice line is being picked up for processing.
336 
337       IF  v_prev_invoice_id = c_rec.invoice_id THEN
338 
339         -- different line of the same invoice is being picked up for processing.
340         -- check if an error has alreay happened for any previous lines of the invoice skip the record.
341 
342         IF  v_error_invoice = 'Y' THEN
343           -- error has already happened for an earlier distribution line of the same invoice, skip the record.
344           GOTO continue_with_next;
345         END IF;
346 
347       ELSE
348 
349         -- a different invoice is picked up for processing,
350         -- commit for the previous invoice and also delete from temp for the previous invoice if no error has happened.
351 
352         IF  v_error_invoice <> 'Y' THEN
353 
354           -- not an error invoice, do the invoice level processing.
355           DELETE  JAI_AP_MATCH_ERS_T
356           WHERE   invoice_id = v_prev_invoice_id;
357 
358           COMMIT;
359           -- get the invoice number for writing onto the log file.
360           SELECT invoice_num
361           INTO   v_invoice_number
362           FROM   ap_invoices_all
363           WHERE  invoice_id = v_prev_invoice_id;
364 
365           Fnd_File.put_line(Fnd_File.LOG, ' Processed invoice number(id) : ' || v_invoice_number || '(' || v_prev_invoice_id || ')' );
366 
367         END IF;
368 
369         -- set the prev to current.
370         v_prev_invoice_id :=  c_rec.invoice_id;
371         v_error_invoice := 'N';
372 
373       END IF; -- different invoice
374 
375     END IF; -- not first invoice
376 
377     -- the control comes here only when no error has happened.
378     -- for each record call distribution matching procedure to process the invoice.
379     -- invoice has not hit error for earlier lines if any.
380 
381 
382     jai_ap_match_tax_pkg.process_batch_record
383     (
384     v_errbuf,
385     c_rec.invoice_id,
386     c_rec.invoice_line_number,
387     c_rec.po_distribution_id,
388     c_rec.quantity_invoiced,
389     c_rec.shipment_header_id,
390     c_rec.receipt_num,
391     c_rec.receipt_code,
392     c_rec.rematching,
393     c_rec.rcv_transaction_id,
394     c_rec.amount,
395     --c_rec.project_id,
396     --c_rec.task_id,
397     --c_rec.expenditure_type,
398     --c_rec.expenditure_organization_id,
399     --c_rec.expenditure_item_date,
400     c_rec.org_id
401     /* 5763527 */
402     ,c_rec.project_id
403     ,c_rec.task_id
404     ,c_rec.expenditure_type
405     ,c_rec.expenditure_organization_id
406     ,c_rec.expenditure_item_date
407     /* End 5763527 */
408     );
409 
410     IF ( v_errbuf IS NOT NULL) THEN
411 
412       -- Called unit has returned error.
413       v_error_mesg := 'Error from called unit jai_ap_match_tax_pkg.process_batch_record, ';
414       RAISE  error_from_called_unit;
415 
416     END IF; -- error return from distribution matching
417 
418 
419    EXCEPTION
420 
421     WHEN OTHERS THEN
422 
423      IF v_error_mesg IS NULL  THEN
424        -- the exception condition is not because of returned error from jai_ap_match_tax_pkg.process_batch_record.
425        v_errbuf  := SQLERRM;
426          v_error_mesg := 'Error in loop(not jai_ap_match_tax_pkg.process_batch_record), ';
427      END IF;
428 
429      ROLLBACK;
430 
431       -- update the record for error
432       UPDATE JAI_AP_MATCH_ERS_T
433       SET    error_flag = 'Y',
434              processing_time = SYSDATE,
435              error_message = v_errbuf
436       WHERE  ROWID = v_rowid;
437 
438       COMMIT;
439 
440       -- get the invoice number for writing onto the log file.
441       SELECT invoice_num
442     INTO   v_invoice_number
443     FROM   ap_invoices_all
444     WHERE  invoice_id = c_rec.invoice_id;
445 
446 
447       Fnd_File.put_line(Fnd_File.LOG, v_error_mesg || ' FOR invoice number(id)/line id : ' ||  v_invoice_number
448                                       || '(' || c_rec.invoice_id || ')/' || c_rec.invoice_line_number );
449       Fnd_File.put_line(Fnd_File.LOG, 'Error message - ' || v_errbuf);
450 
451       v_prev_invoice_id :=  c_rec.invoice_id;
452       v_error_invoice := 'Y';
453 
454    END;
455 
456    <<continue_with_next>>
457 
458    NULL;
459 
460   END LOOP; -- CURSOR FOR
461 
462   -- do the processing for the last invoice that was picked up and processed by the cursor if it has not hit any error yet.
463 
464   IF ( v_prev_invoice_id IS NOT NULL AND v_error_invoice <> 'Y') THEN
465 
466     DELETE  JAI_AP_MATCH_ERS_T
467     WHERE   invoice_id = v_prev_invoice_id;
468 
469     COMMIT;
470     -- get the invoice number for writing onto the log file.
471     SELECT invoice_num
472     INTO   v_invoice_number
473     FROM   ap_invoices_all
474     WHERE  invoice_id = v_prev_invoice_id;
475 
476     Fnd_File.put_line(Fnd_File.LOG, ' Processed invoice number(id) : ' || v_invoice_number || '(' || v_prev_invoice_id || ')' );
477 
478   END IF;
479 
480 
481   Fnd_File.put_line(Fnd_File.LOG, 'SUCCESSFUL END PROCEDURE - jai_ap_match_tax_pkg.process_batch');
482 
483 EXCEPTION
484 
485   WHEN OTHERS THEN
486 
487     p_errbuf := SQLERRM;
488     p_retcode := 2;
489 
490     -- rollback for all the lines of the invoice which have already been processed.
491     ROLLBACK;
492 
493     -- update the record for error
494     UPDATE JAI_AP_MATCH_ERS_T
495     SET    error_flag = 'Y',
496            processing_time = SYSDATE,
497            error_message = p_errbuf
498     WHERE  ROWID = v_rowid;
499 
500     COMMIT;
501 
502     Fnd_File.put_line(Fnd_File.LOG, 'Error END PROCEDURE - jai_ap_match_tax_pkg.process_batch');
503     Fnd_File.put_line(Fnd_File.LOG, 'Error message - ' || p_errbuf);
504     RETURN;
505 
506 END process_batch;
507 
508 PROCEDURE process_online
509 (
510 errbuf                        OUT  NOCOPY       VARCHAR2,
511 retcode                       OUT  NOCOPY       VARCHAR2,
512 inv_id                                IN        NUMBER,
513 pn_invoice_line_number                IN        NUMBER, -- Using pn_invoice_line_number instead of dist_line_no for Bug#4445989
514 po_dist_id                            IN        NUMBER,
515 qty_inv                               IN        NUMBER,
516 p_shipment_header_id                  IN        NUMBER,
517 p_packing_slip_num                    IN        VARCHAR2,
518 p_receipt_code                                  VARCHAR2,
519 p_rematch                                       VARCHAR2,
520 rcv_tran_id                           IN        NUMBER,
521 v_dist_amount                         IN        NUMBER,
522 --p_project_id                                    NUMBER, Obsoleted  Bug# 4445989
523 --p_task_id                                       NUMBER, Obsoleted  Bug# 4445989
524 --p_expenditure_type                              VARCHAR2, Obsoleted , Bug# 4445989
525 --p_expenditure_organization_id                   NUMBER, Obsoleted , Bug# 4445989
526 --p_expenditure_item_date                         DATE, Obsoleted , Bug# 4445989
527 v_org_id                              IN        NUMBER
528  /* 5763527, Introduced for Project implementation */
529  , p_project_id                                    NUMBER
530  , p_task_id                                       NUMBER
531  , p_expenditure_type                              VARCHAR2
532  , p_expenditure_organization_id                   NUMBER
533  , p_expenditure_item_date                         DATE
534   /* End 5763527 */
535 )
536 IS
537 CURSOR get_ven_info(v_invoice_id NUMBER) IS
538     SELECT vendor_id, vendor_site_id, org_id, cancelled_date, invoice_num, set_of_books_id,  -- added for bug#3354932
539            legal_entity_id /* rallamse bug#4448789 */
540     FROM   ap_invoices_all
541     WHERE  invoice_id = v_invoice_id;
542 
543 	Cursor get_discard_info (ln_invoice_id number) is /*Added by nprashar for bug # 12832424*/
544 Select nvl(discarded_flag,'N') from ap_invoice_lines_all
545 where invoice_id = ln_invoice_id
546 and  line_number = pn_invoice_line_number
547 and  po_distribution_id = po_dist_id;
548 
549 Cursor c_match_tax_lines (p_invoice_id number) is /*Added by nprashar for bug # 12832424*/
550 select invoice_id, po_distribution_id, invoice_distribution_id, rcv_transaction_id,invoice_line_number,
551 parent_invoice_distribution_id
552 from  jai_ap_match_inv_taxes
553 where line_type_lookup_code = 'MISCELLANEOUS'
554 and invoice_id = p_invoice_id
555 and parent_invoice_line_number = pn_invoice_line_number
556 and po_distribution_id = po_dist_id;
557 
558 Cursor c_inv_dist_tax_lines (ln_invoice_id number, ln_invoice_distribution_id number) is /*Added by nprashar for bug # 12832424*/
559 select accounting_date,
560         accrual_posted_flag,
561         assets_addition_flag,
562         assets_tracking_flag,
563         cash_posted_flag,
564         distribution_line_number,
565         dist_code_combination_id,
566         invoice_id,
567         line_type_lookup_code,
568         period_name,
569         set_of_books_id ,
570         amount,
571         base_amount,
572         batch_id,
573         description,
574         exchange_rate_variance,
575         last_update_login,
576         match_status_flag,
577         posted_flag,
578         rate_var_code_combination_id ,
579         reversal_flag ,
580         program_application_id,
581         program_id,
582         program_update_date,
583         accts_pay_code_combination_id,
584         invoice_distribution_id,
585         quantity_invoiced,
586         po_distribution_id ,
587         rcv_transaction_id,
588         price_var_code_combination_id,
589         invoice_price_variance,
590         base_invoice_price_variance,
591         matched_uom_lookup_code,
592         invoice_line_number,
593         org_id ,
594         charge_applicable_to_dist_id ,
595         project_id,
596         task_id,
597         expenditure_type,
598         expenditure_item_date,
599         expenditure_organization_id,
600         project_accounting_context,
601         pa_addition_flag,
602         distribution_class,
603         dist_match_type
604 from ap_invoice_distributions_all
605 where invoice_id = ln_invoice_id
606 and   invoice_distribution_id = ln_invoice_distribution_id;
607 
608 Cursor c_inv_dist_tax_lines_ers (ln_invoice_id number, ln_po_distribution_id number) is /*Added by nprashar for bug # 12832424*/
609 select accounting_date,
610         accrual_posted_flag,
611         assets_addition_flag,
612         assets_tracking_flag,
613         cash_posted_flag,
614         distribution_line_number,
615         dist_code_combination_id,
616         invoice_id,
617         line_type_lookup_code,
618         period_name,
619         set_of_books_id ,
620         amount,
621         base_amount,
622         batch_id,
623         description,
624         exchange_rate_variance,
625         last_update_login,
626         match_status_flag,
627         posted_flag,
628         rate_var_code_combination_id ,
629         reversal_flag ,
630         program_application_id,
631         program_id,
632         program_update_date,
633         accts_pay_code_combination_id,
634         invoice_distribution_id,
635         quantity_invoiced,
636         po_distribution_id ,
637         rcv_transaction_id,
638         price_var_code_combination_id,
639         invoice_price_variance,
640         base_invoice_price_variance,
641         matched_uom_lookup_code,
642         invoice_line_number,
643         org_id ,
644         charge_applicable_to_dist_id ,
645         project_id,
646         task_id,
647         expenditure_type,
648         expenditure_item_date,
649         expenditure_organization_id,
650         project_accounting_context,
651         pa_addition_flag,
652         distribution_class,
653         dist_match_type
654 from ap_invoice_distributions_all
655 where invoice_id = ln_invoice_id
656 and   po_distribution_id  = ln_po_distribution_id
657 and   line_type_lookup_code = 'MISCELLANEOUS';
658 
659 
660   CURSOR c_functional_currency(p_sob NUMBER) IS -- cursor added for bug#3354932
661     SELECT currency_code
662     FROM   gl_sets_of_books
663     WHERE  set_of_books_id = p_sob;
664 
665   CURSOR checking_for_packing_slip(ven_id NUMBER, ven_site_id NUMBER, v_org_id NUMBER) IS
666     SELECT pay_on_code, pay_on_receipt_summary_code
667     FROM   po_vendor_sites_all
668     WHERE  vendor_id = ven_id
669     AND    vendor_site_id = ven_site_id
670     AND    NVL(org_id, -1) = NVL(v_org_id, -1);
671 
672   /*below cursor added for bug 6595773*/
673   cursor c_get_ship_to_org_loc (cpn_line_location_id po_line_locations_all.line_location_id%type)
674   is
675   select ship_to_organization_id
676      ,ship_to_location_id
677   from  po_line_locations_all plla
678   where plla.line_location_id = cpn_line_location_id;
679 
680 
681    -- Cursor modified to use pn_invoice_line_number instead of distribution_line_number, bug#4445989
682    CURSOR cur_items(inv_id IN NUMBER, line_no IN NUMBER, cpn_min_dist_line_no number) IS
683      SELECT pod.po_header_id,
684       pod.po_line_id,
685       pod.line_location_id,
686       pod.set_of_books_id,
687       pod.org_id,
688       poh.rate,
689       poh.rate_type,
690       pod.rate_date,
691       poh.currency_code,
692       api.last_update_login,
693       apd.dist_code_combination_id,
694       api.creation_date,
695       api.created_by,
696       api.last_update_date,
697       api.last_updated_by,
698       api.invoice_date
699     FROM  ap_invoices_all api,
700       ap_invoice_distributions_all apd,
701       po_distributions_all pod,
702       po_headers_all poh
703      WHERE  apd.invoice_id               = api.invoice_id
704      AND    pod.po_header_id             = poh.po_header_id
705      AND    apd.po_distribution_id       = pod.po_distribution_id
706      AND    apd.invoice_line_number      = line_no
707      AND    api.invoice_id               = inv_id
708      AND    apd.distribution_line_number = cpn_min_dist_line_no;
709      -- Added by Brathod to get minimum invoice line number from invoice distributions, Bug#4445989
710      CURSOR cur_get_min_dist_linenum
711            ( cpn_invoice_id AP_INVOICE_DISTRIBUTIONS_ALL.INVOICE_ID%TYPE
712             ,cpn_invoice_line_number AP_INVOICE_DISTRIBUTIONS_ALL.INVOICE_LINE_NUMBER%TYPE
713            )
714      IS
715      SELECT min(distribution_line_number)
716      FROM   ap_invoice_distributions_all apid
717      WHERE  apid.invoice_id = cpn_invoice_id
718      AND    apid.invoice_line_number = cpn_invoice_line_number;
719 
720 
721    CURSOR from_po_distributions(po_dist_id NUMBER) IS
722     SELECT line_location_id, po_line_id
723     FROM   po_distributions_all
724     WHERE  po_distribution_id = po_dist_id;
725 
726     ln_min_dist_line_num  NUMBER ;
727     -- End of Bug # 4445989
728 
729 
730     /*bug 9346307*/
731     CURSOR c_po_details IS
732     SELECT po_line_id, po_line_location_id
733     FROM ap_invoice_lines_all
734     WHERE invoice_id = inv_id
735     AND line_number = pn_invoice_line_number;
736 
737     CURSOR c_ap_dist(cp_invoice_id NUMBER, cp_line_num NUMBER) IS
738     SELECT a.accounting_date,
739            a.accrual_posted_flag,
740            a.assets_addition_flag,
741            a.assets_tracking_flag,
742            a.cash_posted_flag,
743            a.dist_code_combination_id,
744            a.last_updated_by,
745            a.last_update_date,
746            a.line_type_lookup_code,
747            a.period_name,
748            a.set_of_books_id,
749            a.amount,
750            a.base_amount,
751            a.batch_id,
752            a.created_by,
753            a.creation_date,
754            a.description,
755            a.accts_pay_code_combination_id,
756            a.exchange_rate_variance,
757            a.last_update_login,
758            a.match_status_flag,
759            a.posted_flag,
760            a.rate_var_code_combination_id,
761            a.reversal_flag,
762            a.vat_code,
763            a.exchange_date,
764            a.exchange_rate,
765            a.exchange_rate_type,
766            a.price_adjustment_flag,
767            a.program_application_id,
768            a.program_id,
769            a.program_update_date,
770            a.global_attribute1,
771            a.global_attribute2,
772            a.global_attribute3,
773            a.po_distribution_id,--rchandan for bug#4333488
774            a.project_id,
775            a.task_id,
776            a.expenditure_type,
777            a.expenditure_item_date,
778            a.expenditure_organization_id,
779            a.quantity_invoiced,
780            Nvl(a.quantity_invoiced,1)/Nvl(b.quantity_invoiced,1) qty_apportion_factor,
781            a.unit_price,
782            price_var_code_combination_id,
783            invoice_distribution_id,
784            matched_uom_lookup_code,
785            invoice_price_variance,
786            a.distribution_line_number,
787            a.org_id -- Test for Bug 4863208
788            /* 5763527 */
789            ,project_accounting_context
790            ,pa_addition_flag
791            /* End 5763527 */
792       ,a.dist_match_type --Added by nprashar for bug # 12832424
793      FROM   ap_invoice_distributions_all a,
794             ap_invoice_lines_all b
795      WHERE  a.invoice_id = cp_invoice_id
796      AND    a.invoice_line_number = cp_line_num
797      AND    b.invoice_id = cp_invoice_id
798      AND    b.line_number = cp_line_num
799      AND    a.invoice_id = b.invoice_id
800      AND    a.invoice_line_number = b.line_number
801      ORDER BY a.distribution_line_number;
802      r_ap_dist c_ap_dist%ROWTYPE;
803      v_tax_amt_dist NUMBER;
804 
805 
806     ln_po_line_id ap_invoice_lines_all.po_line_id%TYPE;
807     ln_po_line_location_id ap_invoice_lines_all.po_line_location_id%TYPE;
808     ln_po_dist_id ap_invoice_lines_all.po_distribution_id%TYPE;
809     /*end bug 9346307*/
810 
811   /* Added by LGOPALSa. Bug 4210102.
812    * Added CVD and Customs Education Cess
813    * */
814   /* commented out by eric for inclusive tax
815   CURSOR from_line_location_taxes(p_line_location_id NUMBER, vend_id NUMBER) IS
816     SELECT tax_id, tax_amount, currency, tax_target_amount, nvl(modvat_flag,'Y') modvat_flag,
817     tax_type, tax_line_no -- added by kunkumar for bug 5593895
818     FROM   JAI_PO_TAXES
819     -- WHERE line_focus_id = focus_id
820     WHERE line_location_id = p_line_location_id   -- 3096578
821     AND    NVL(upper(tax_type), 'A') NOT IN
822                               ('TDS',
823              'CVD',
824              jai_constants.tax_type_add_cvd ,     -- Date 31/10/2006 Bug 5228046 added by SACSETHI
825              'CUSTOMS',
826                     jai_constants.tax_type_cvd_edu_cess,
827               jai_constants.tax_type_customs_edu_cess,
828               jai_constants.tax_type_sh_cvd_edu_cess,jai_constants.tax_type_sh_customs_edu_cess --Added higher education cess by csahoo for bug#5989740
829               )
830     AND    NVL(vendor_id, -1) = vend_id --Modified by kunkumar for bug 5593895
831     order by  tax_line_no; -- added bug#3038566
832     -- AND    tax_amount  <> 0 ; -- commented by aparajita on 10/03/2003 for bug # 2841363
833   */
834 
835   CURSOR from_line_location_taxes
836   ( p_line_location_id NUMBER
837   , vend_id NUMBER
838   , p_source VARCHAR2 -- Added by Jason Liu for bug#6918386
839   )
840   IS
841     SELECT
842       jpt.tax_id
843     , jpt.tax_amount
844     , jpt.currency
845     , jpt.tax_target_amount
846     , nvl(jpt.modvat_flag,'Y') modvat_flag
847     , jpt.tax_type
848     , jpt.tax_line_no -- added by kunkumar for bug 5593895
849     , NVL(jcta.inclusive_tax_flag,'N') inc_tax_flag --Added by Eric for Inclusive Tax
850     ,nvl(jcta.vat_flag,'N') VAT_FLAG
851     ,nvl(jcta.adhoc_flag,'N') adhoc_flag
852     FROM
853       JAI_PO_TAXES jpt
854     , JAI_CMN_TAXES_ALL  jcta              --Added by Eric for Inclusive Tax
855     -- WHERE line_focus_id = focus_id
856     WHERE line_location_id = p_line_location_id   -- 3096578
857       AND NVL(UPPER(jpt.tax_type), 'A')
858           NOT IN( 'TDS'
859                 , 'CVD'
860                 , jai_constants.tax_type_add_cvd      -- Date 31/10/2006 Bug 5228046 added by SACSETHI
861                 , 'CUSTOMS'
862                 , jai_constants.tax_type_cvd_edu_cess
863                 , jai_constants.tax_type_customs_edu_cess
864                 , jai_constants.tax_type_sh_cvd_edu_cess
865                 , jai_constants.tax_type_sh_customs_edu_cess --Added higher education cess by csahoo for bug#5989740
866                 )
867       AND jcta.tax_id   = jpt.tax_id
868       AND NVL(jpt.vendor_id, -1) = vend_id --Modified by kunkumar for bug 5593895
869       AND p_source <> 'PPA'
870       --Added by Qiong for reverse charge bug#16001407 start
871       ---------------------------------------------------------------------
872       AND NVL(jcta.reverse_charge_flag,'N') = 'N'
873       ---------------------------------------------------------------------
874       --Added by Qiong for reverse charge bug#16001407 end
875     -- Added by Jason Liu for bug#6918386
876     ---------------------------------------------------------------------
877     UNION
878 
879     SELECT
880       jrl.tax_id
881     , (jrl.modified_tax_amount - jrl.original_tax_amount) tax_amount
882     , jrl.currency_code currency
883     , (jrl.modified_tax_amount - jrl.original_tax_amount) tax_target_amount
884     , jrl.recoverable_flag modvat_flag
885     , jrl.tax_type tax_type
886     , jrl.tax_line_no tax_line_no
887     , NVL(jcta.inclusive_tax_flag,'N') inc_tax_flag
888     ,nvl(jcta.vat_flag,'N') VAT_FLAG
889     ,nvl(jcta.adhoc_flag,'N') adhoc_flag
890     FROM
891       jai_retro_tax_changes  jrl
892     , jai_retro_line_changes jrlc
893     , jai_cmn_taxes_all      jcta
894     WHERE jrlc.line_location_id = p_line_location_id
895       AND jrlc.line_change_id = jrl.line_change_id
896       AND jrlc.doc_version_number = (SELECT max(doc_version_number)
897                                      FROM jai_retro_line_changes
898                                      WHERE doc_type IN ('STANDARD PO', 'RELEASE')
899                                        AND doc_line_id = jrlc.doc_line_id
900                                      )
901       AND jrlc.doc_type IN ('STANDARD PO', 'RELEASE')
902       AND jrl.tax_id = jcta.tax_id
903       AND NVL(jrlc.vendor_id, -1) = vend_id
904       AND p_source = 'PPA'
905       AND NVL(upper(jrl.tax_type),'TDS') NOT IN ( jai_constants.tax_type_tds
906                                                 , jai_constants.tax_type_cvd
907                                                 , jai_constants.tax_type_add_cvd
908                                                 , jai_constants.tax_type_customs
909                                                 , jai_constants.tax_type_sh_customs_edu_cess
910                                                 , jai_constants.tax_type_customs_edu_cess
911                                                 , jai_constants.tax_type_sh_cvd_edu_cess
912                                                 , jai_constants.tax_type_cvd_edu_cess
913                                                 )
914       AND NVL(jrl.third_party_flag, 'N') = 'N' -- Added by Jason Liu for bug#6936416
915       ---------------------------------------------------------------------
916       --Added by Qiong for reverse charge bug#16001407 start
917       ---------------------------------------------------------------------
918       AND NVL(jcta.reverse_charge_flag,'N') = 'N'
919       ---------------------------------------------------------------------
920       --Added by Qiong for reverse charge bug#16001407 end
921     ORDER BY  tax_line_no; -- added bug#3038566
922     -- AND    tax_amount  <> 0 ; -- commented by aparajita on 10/03/2003 for bug # 284136
923 
924     CURSOR c_tax(t_id NUMBER) IS
925      SELECT tax_name,
926         tax_account_id,
927         mod_cr_percentage, -- bug 3051828
928         adhoc_flag,  -- added by aparajita on 23/01/2003 for bug # 2694011
929         nvl(tax_rate,-1) tax_rate,  --Modified by kunkumar for bug 5593895
930         tax_type,
931     NVL(rounding_factor,0) rounding_factor /* added by vumaasha for bug 6761425 */
932      FROM   JAI_CMN_TAXES_ALL
933      WHERE  tax_id = t_id;
934 
935     CURSOR c_inv(inv_id NUMBER) IS
936       SELECT batch_id,source
937       FROM   ap_invoices_all
938       WHERE  invoice_id = inv_id;
939 
940     CURSOR for_org_id(inv_id NUMBER) IS
941      SELECT org_id, vendor_id, NVL(exchange_rate, 1) exchange_rate, invoice_currency_code
942      FROM   ap_invoices_all
943      WHERE  invoice_id = inv_id;
944 
945   CURSOR for_acct_id(orgn_id NUMBER) IS
946      SELECT accts_pay_code_combination_id
947      FROM   ap_system_parameters_all
948      WHERE  NVL(org_id, -1) = NVL(orgn_id, -1);--uncommented and modified by kunkumar for bug 5593895
949      --commented the above by Sanjikum For Bug#4474501, as this cursor is not being used anywhere
950 
951     CURSOR for_dist_insertion(cpn_invoice_id NUMBER, cpn_inv_line_num NUMBER,cpn_min_dist_line_num NUMBER) IS  /* Picks up dtls from std apps inserted line */
952     SELECT a.accounting_date,a.accrual_posted_flag,
953                 a.assets_addition_flag,a.assets_tracking_flag,
954     a.cash_posted_flag, a.dist_code_combination_id,
955     a.last_updated_by,a.last_update_date,
956     a.line_type_lookup_code,  a.period_name,
957     a.set_of_books_id,a.amount,a.base_amount,
958     a.batch_id,a.created_by,a.creation_date,
959     a.description,a.accts_pay_code_combination_id,
960     a.exchange_rate_variance,a.last_update_login,
961     a.match_status_flag,a.posted_flag, a.rate_var_code_combination_id,
962     a.reversal_flag,a.vat_code,a.exchange_date,a.exchange_rate,
963     a.exchange_rate_type,a.price_adjustment_flag,
964     a.program_application_id,a.program_id,
965     a.program_update_date,a.global_attribute1,
966     a.global_attribute2,
967     a.global_attribute3, a.po_distribution_id,--rchandan for bug#4333488
968                 a.project_id,a.task_id,a.expenditure_type,a.expenditure_item_date,
969     a.expenditure_organization_id,     quantity_invoiced,
970     a.unit_price, price_var_code_combination_id,
971     invoice_distribution_id, matched_uom_lookup_code, invoice_price_variance,
972     org_id -- Test for Bug 4863208
973      /* 5763527 */
974      ,project_accounting_context
975      ,pa_addition_flag
976    /* End 5763527 */
977         , dist_match_type --Added by nprashar for bug # 12832424
978      FROM   ap_invoice_distributions_all a
979      WHERE  invoice_id               = cpn_invoice_id
980      AND    invoice_line_number      = cpn_inv_line_num
981      AND    distribution_line_number = cpn_min_dist_line_num;
982 
983 
984    /* Added by LGOPALSa. Bug 4210102.
985     * Added CVD and Customs Education Cess
986     * */
987 
988   --deleted by eric for inclusive tax on 20-dec-2007,begin
989   ---------------------------------------------------------------------------
990   /*
991   CURSOR tax_lines1_cur(tran_id NUMBER,ven_id NUMBER)
992   IS
993     SELECT jrl.tax_amount tax_amount,
994     jrl.tax_id,
995     jrl.currency,
996     jrl.tax_type tax_type,
997     jrl.modvat_flag,
998     jrl.tax_line_no tax_line_no --modified by kunkumar for bug 5593895
999     , NVL(jcta.inclusive_tax_flag,'N') inc_tax_flag --Added by Eric for Inclusive Tax
1000     FROM JAI_RCV_LINE_TAXES jrl,rcv_shipment_lines rsl,rcv_transactions rt
1001     WHERE jrl.shipment_line_id = rsl.shipment_line_id
1002     AND rt.shipment_line_id = rsl.shipment_line_id
1003     AND rt.transaction_id = tran_id
1004     AND jrl.vendor_id = ven_id
1005     AND NVL(upper(jrl.tax_type),'TDS') NOT IN (
1006                                                  'TDS',
1007              'CVD',
1008              jai_constants.tax_type_add_cvd ,     -- Date 31/10/2006 Bug 5228046 added by SACSETHI
1009              'CUSTOMS',
1010                         JAI_CONSTANTS.TAX_TYPE_SH_CUSTOMS_EDU_CESS,jai_constants.tax_type_customs_edu_cess, -- added by ssawant for bug 5989740
1011             JAI_CONSTANTS.TAX_TYPE_SH_CVD_EDU_CESS,jai_constants.tax_type_cvd_edu_cess --added by ssawant for bug 5989740
1012                  )
1013     order by  tax_line_no -- added bug#3038566
1014     ;
1015    */
1016    ---------------------------------------------------------------------------
1017    --deleted  by eric for inclusive tax on 20-dec-2007,end
1018 
1019 
1020    CURSOR tax_lines1_cur(tran_id NUMBER,ven_id NUMBER)
1021    IS
1022    SELECT
1023      jrl.tax_amount tax_amount
1024    , jrl.tax_id
1025    , jrl.currency
1026    , jrl.tax_type tax_type
1027    , jrl.modvat_flag
1028    , jrl.tax_line_no tax_line_no --modified by kunkumar for bug 5593895
1029    , NVL(jcta.inclusive_tax_flag,'N') inc_tax_flag --Added by Eric for Inclusive Tax
1030    ,jcta.vat_flag vat_flag
1031    ,nvl(jcta.adhoc_flag,'N') adhoc_flag --12697697
1032 
1033   FROM
1034     JAI_RCV_LINE_TAXES jrl
1035   , RCV_SHIPMENT_LINES RSL
1036   , RCV_TRANSACTIONS RT
1037   , jai_cmn_taxes_all  jcta
1038   WHERE jrl.shipment_line_id = rsl.shipment_line_id
1039     AND rt.shipment_line_id  = rsl.shipment_line_id
1040     AND rt.transaction_id    = tran_id
1041     AND jrl.vendor_id        = ven_id
1042     AND jcta.tax_id          = jrl.tax_id  --Added by Eric for Inclusive Tax
1043     AND NVL(upper(jrl.tax_type),'TDS')
1044         NOT IN ( 'TDS'
1045                , 'CVD'
1046                , jai_constants.tax_type_add_cvd      -- Date 31/10/2006 Bug 5228046 added by SACSETHI
1047                , 'CUSTOMS'
1048                , JAI_CONSTANTS.TAX_TYPE_SH_CUSTOMS_EDU_CESS
1049                , jai_constants.tax_type_customs_edu_cess /* added by ssawant for bug 5989740 */
1050                , JAI_CONSTANTS.TAX_TYPE_SH_CVD_EDU_CESS
1051                , jai_constants.tax_type_cvd_edu_cess /* added by ssawant for bug 5989740 */
1052                )
1053      --Added by Qiong for reverse charge bug#16001407 start
1054      ---------------------------------------------------------------------
1055      AND NVL(jcta.reverse_charge_flag,'N') = 'N'
1056      ---------------------------------------------------------------------
1057      --Added by Qiong for reverse charge bug#16001407 end
1058     order by  tax_line_no; -- added
1059 
1060 
1061   ----------------------------------------------- Receipt Matching Cursors ----------------------------
1062   /* Added by LGOPALSa. Bug 4210102.
1063    * Added CVD and Customs Education Cess
1064    * */
1065 
1066   -- Modified by Jason Liu for retroactive price on 2008/01/09
1067   -- Added the parameter p_source, added the UNION(p_source = 'PPA')
1068   CURSOR r_tax_lines_cur(tran_id NUMBER,ven_id NUMBER,p_source IN VARCHAR2) IS
1069     SELECT
1070       jrl.tax_amount tax_amount
1071     , jrl.tax_id
1072     , jrl.currency
1073     , jrl.tax_type tax_type
1074     , jrl.modvat_flag
1075     , jrl.tax_line_no tax_line_no --Added by kunkumar for bug 5593895
1076     , NVL(jcta.inclusive_tax_flag,'N') inc_tax_flag --Added by Eric for Inclusive Tax
1077     ,jcta.vat_flag vat_flag
1078    ,nvl(jcta.adhoc_flag,'N') ADHOC_FLAG --12697697
1079     FROM
1080       jai_rcv_line_taxes jrl
1081     , rcv_shipment_lines rsl
1082     , rcv_transactions rt
1083     , jai_cmn_taxes_all jcta          --Added by Eric for Inclusive Tax
1084     WHERE jrl.shipment_line_id = rsl.shipment_line_id
1085       AND rt.shipment_line_id = rsl.shipment_line_id
1086       AND rt.transaction_id = tran_id
1087       AND jrl.vendor_id = ven_id
1088       AND jcta.tax_id   = jrl.tax_id  --Added by Eric for Inclusive Tax
1089       AND NVL(upper(jrl.tax_type),'TDS') NOT IN
1090                                              ('TDS',
1091                 'CVD',
1092                        jai_constants.tax_type_add_cvd ,     -- Date 31/10/2006 Bug 5228046 added by SACSETHI
1093                 'CUSTOMS',
1094                                               JAI_CONSTANTS.TAX_TYPE_SH_CUSTOMS_EDU_CESS,jai_constants.tax_type_customs_edu_cess, /* added by ssawant for bug 5989740 */
1095                 JAI_CONSTANTS.TAX_TYPE_SH_CVD_EDU_CESS,jai_constants.tax_type_cvd_edu_cess /* added by ssawant for bug 5989740 */
1096        )
1097       AND p_source <> 'PPA'
1098       --Added by Qiong for reverse charge bug#16001407 start
1099       ---------------------------------------------------------------------
1100       AND NVL(jcta.reverse_charge_flag,'N') = 'N'
1101       ---------------------------------------------------------------------
1102       --Added by Qiong for reverse charge bug#16001407 end
1103     UNION
1104 
1105     SELECT
1106       (jrl.modified_tax_amount - jrl.original_tax_amount) tax_amount
1107     , jrl.tax_id
1108     , jrl.currency_code currency
1109     , jrl.tax_type tax_type
1110     , jrl.recoverable_flag modvat_flag
1111     , jrl.tax_line_no tax_line_no
1112     , NVL(jcta.inclusive_tax_flag,'N') inc_tax_flag
1113     ,jcta.vat_flag vat_flag
1114        ,nvl(jcta.adhoc_flag,'N') ADHOC_FLAG --12697697
1115     FROM
1116       jai_retro_tax_changes  jrl
1117     , rcv_shipment_lines     rsl
1118     , rcv_transactions       rt
1119     , jai_retro_line_changes jrlc
1120     , jai_cmn_taxes_all      jcta
1121     WHERE jrlc.doc_line_id = rsl.shipment_line_id
1122       AND jrlc.line_change_id = jrl.line_change_id
1123       AND jrlc.doc_version_number = (SELECT max(doc_version_number)
1124                                      FROM jai_retro_line_changes
1125                                      WHERE doc_type = 'RECEIPT'
1126                                        AND doc_line_id = jrlc.doc_line_id
1127                                      )
1128       AND jrlc.doc_type = 'RECEIPT'
1129       AND jrl.tax_id = jcta.tax_id
1130       AND rt.shipment_line_id = rsl.shipment_line_id
1131       AND rt.transaction_id = tran_id
1132       AND jrlc.vendor_id = ven_id
1133       AND p_source = 'PPA'
1134       AND NVL(upper(jrl.tax_type),'TDS') NOT IN ( jai_constants.tax_type_tds
1135                                                 , jai_constants.tax_type_cvd
1136                                                 , jai_constants.tax_type_add_cvd
1137                                                 , jai_constants.tax_type_customs
1138                                                 , jai_constants.tax_type_sh_customs_edu_cess
1139                                                 , jai_constants.tax_type_customs_edu_cess
1140                                                 , jai_constants.tax_type_sh_cvd_edu_cess
1141                                                 , jai_constants.tax_type_cvd_edu_cess
1142                                                 )
1143       AND NVL(jrl.third_party_flag, 'N') = 'N' -- Added by Jason Liu for bug#6936416
1144       --Added by Qiong for reverse charge bug#16001407 start
1145       ---------------------------------------------------------------------
1146       AND NVL(jcta.reverse_charge_flag,'N') = 'N'
1147       ---------------------------------------------------------------------
1148       --Added by Qiong for reverse charge bug#16001407 end
1149     -- GROUP BY jrl.tax_id,jrl.currency,jrl.tax_type, jrl.modvat_flag; commented by bug#3038566
1150     order by tax_line_no; -- added bug#3038566
1151 
1152 
1153     -- Start add for bug#3632810
1154   CURSOR c_get_invoice_distribution  is -- bug#3332988
1155   select ap_invoice_distributions_s.nextval
1156   from   dual;
1157 
1158  -- start addition by ssumaith -bug# 3127834
1159 
1160  cursor c_fnd_curr_precision(cp_currency_code fnd_currencies.currency_code%type) is
1161  select precision
1162  from   fnd_currencies
1163  where  currency_code = cp_currency_code;
1164 
1165   -- ends here additions by ssumaith -bug# 3127834
1166 --Added by kunkumar for porting 4454499
1167   Cursor c_tax_curr_prec(cp_tax_id jai_cmn_taxes_all.tax_id%type) is
1168  --select NVL(rounding_factor,-1) -- for bug 13954092 by anupgupt
1169  select NVL(rounding_factor,0) -- for bug 13954092 by anupgupt
1170  from jai_cmn_taxes_all
1171  where tax_id = cp_tax_id  ;
1172 
1173   /*Bug 8866084 - Added cursor to fetch Source of Invoice*/
1174   cursor c_get_ap_invoice_src (p_inv_id NUMBER)
1175   is
1176   select source
1177   from ap_invoices_all
1178   where invoice_id = p_inv_id;
1179 
1180   ln_source                           VARCHAR2(50);
1181 
1182   ln_precision                        fnd_currencies.precision%type; -- added by sssumaith - bug# 3127834
1183   ln_tax_precision                    jai_cmn_taxes_all.rounding_factor%type;
1184   v_po_unit_meas_lookup_code          po_lines_all.unit_meas_lookup_code%type;
1185   r_tax_lines_rec                     r_tax_lines_cur%ROWTYPE;
1186   /* r_cur_items_rec                  r_cur_items%ROWTYPE; Bug#4095234*/
1187   vPoUomCode                          mtl_units_of_measure.unit_of_measure%TYPE;
1188   vReceiptUomCode                     mtl_units_of_measure.unit_of_measure%TYPE;
1189   vReceiptToPoUomConv                 NUMBER;
1190   v_attribute12                       VARCHAR2(150);
1191   v_ATTRIBUTE2                        VARCHAR2(150);
1192   v_sup_po_header_id                  NUMBER;
1193   v_sup_po_line_id                    NUMBER;
1194   v_sup_line_location_id              NUMBER;
1195   v_sup_set_of_books_id               NUMBER;
1196   v_sup_org_id                        NUMBER;
1197   v_sup_conversion_rate               NUMBER;
1198   v_sup_tax_date                      DATE;
1199   v_sup_currency                      VARCHAR2(10);
1200   v_sup_invoice_date                  DATE;
1201   v_invoice_num                       VARCHAR2(50);
1202   v_sup_inv_type                      VARCHAR2(25);
1203   get_ven_info_rec                    get_ven_info%ROWTYPE;
1204   chk_for_pcking_slip_rec             checking_for_packing_slip%ROWTYPE;
1205   v_invoice_num_for_log               VARCHAR2(50);
1206   v_tax_variance_inv_cur              number; -- bug # 2775043
1207   v_tax_variance_fun_cur              number; -- bug # 2775043
1208   v_price_var_accnt                   ap_invoice_distributions_all.price_var_code_combination_id%type;
1209   v_distribution_no                   ap_invoice_distributions_all.distribution_line_number%type;
1210   v_assets_tracking_flag              ap_invoice_distributions_all.assets_tracking_flag%type;
1211   v_dist_code_combination_id          ap_invoice_distributions_all.dist_code_combination_id%type;--bug#367196
1212   v_update_payment_schedule           boolean; -- bug#3218978
1213   v_invoice_distribution_id           ap_invoice_distributions_all.invoice_distribution_id%type; -- bug#3332988
1214   v_functional_currency               gl_sets_of_books.currency_code%type; -- bug#3354932
1215   v_batch_id                          NUMBER;
1216   c_tax_rec                           c_tax%ROWTYPE;
1217   v_source                            VARCHAR2(25);
1218   apccid                              ap_invoices_all.ACCTS_PAY_CODE_COMBINATION_ID%TYPE;
1219   shpmnt_ln_id                        rcv_shipment_lines.shipment_line_id%TYPE;
1220   cur_items_rec                       cur_items%ROWTYPE;
1221   caid                                gl_sets_of_books.chart_of_accounts_id%TYPE;
1222   from_po_distributions_rec           from_po_distributions%ROWTYPE;
1223   for_dist_insertion_rec              for_dist_insertion%ROWTYPE;
1224   cum_tax_amt                         NUMBER := -1;--modified by kunkumar for 5593895
1225   v_tax_amount                        NUMBER;
1226   result                              BOOLEAN;
1227   req_id                              NUMBER;
1228   for_org_id_rec                      for_org_id%ROWTYPE;
1229   -- for_acct_id_rec                     for_acct_id%ROWTYPE;
1230   --commented the above by Sanjikum For Bug#4474501, as this variable is not being used anywhere
1231   v_cor_factor                        NUMBER;
1232   v_tax_type                          VARCHAR2(50);
1233   v_apportn_factor_for_item_line      NUMBER; -- Bug#3752887
1234   v_apportn_factor_for_adhoc          NUMBER; --Added by nprashar for bug # 13363329
1235 
1236 /*following variables added for bug 6595773*/
1237   ln_vat_regime_id                    jai_rgm_definitions.regime_id%type;
1238   ln_ship_to_organization_id          po_line_locations_all.ship_to_organization_id%type;
1239   ln_ship_to_location_id              po_line_locations_all.ship_to_location_id%type;
1240 
1241 
1242    /* 5763527 , Project costing Fwd porting*/
1243   ln_project_id                       ap_invoice_distributions_all.project_id%TYPE;
1244   ln_task_id                          ap_invoice_distributions_all.task_id%TYPE;
1245   lv_exp_type                         ap_invoice_distributions_all.expenditure_type%TYPE;
1246   ld_exp_item_date                    ap_invoice_distributions_all.expenditure_item_date%TYPE;
1247   ln_exp_organization_id              ap_invoice_distributions_all.expenditure_organization_id%TYPE;
1248   lv_project_accounting_context       ap_invoice_distributions_all.project_accounting_context%TYPE;
1249   lv_pa_addition_flag                 ap_invoice_distributions_all.pa_addition_flag%TYPE;
1250 
1251   ln_lines_to_insert                  number;
1252   ln_nrec_tax_amt                     number;
1253   ln_rec_tax_amt                      number;
1254   lv_modvat_flag                      jai_ap_match_inv_taxes.recoverable_flag%type;
1255 
1256   lv_excise_costing_flag              varchar2 (1);
1257 
1258   lv_tax_type                         varchar2(5); --added by eric for inclusive tax
1259   lv_tax_line_amount                  NUMBER := 0; --added by eric for inclusive tax
1260   lv_ap_line_to_inst_flag             VARCHAR2(1); --added by eric for inclusive tax
1261   lv_tax_line_to_inst_flag            VARCHAR2(1); --added by eric for inclusive tax
1262    ln_inclu_serv_amt                   NUMBER := 0; --11936596
1263   ln_inclurec_tax_amt                 NUMBER; --11936596
1264   lv_inclu_tax_flag                  varchar2 (1); --11936596
1265 
1266     lv_discard_flag                    varchar2(1); --Added by nprashar for bug # 12832424
1267   ln_invoice_dist_id_rev            number := 0; --Added by nprashar for bug # 12832424
1268   ln_invoice_distribution_no        number; --Added by nprashar for bug # 12832424
1269 
1270 
1271   /*11936596*/
1272   CURSOR inclu_flag_cur(cn_org_id IN NUMBER)
1273   IS
1274   SELECT
1275     NVL(ja.inclusive_tax_flag, 'N')  inclusive_tax_flag
1276   FROM
1277     jai_ap_tds_years ja
1278   WHERE ja.legal_entity_id = cn_org_id
1279   AND sysdate between ja.start_date and ja.end_date;
1280 
1281 
1282 
1283   cursor c_get_excise_costing_flag (cp_rcv_transaction_id   rcv_transactions.transaction_id%type,
1284                                      cp_organization_id      JAI_RCV_TRANSACTIONS.organization_id%type,  --Added by Bgowrava for Bug#7503308
1285                                     cp_shipment_header_id    JAI_RCV_TRANSACTIONS.shipment_header_id%type,  --Added by Bgowrava for Bug#7503308
1286                   cp_txn_type             JAI_RCV_TRANSACTIONS.transaction_type%type,  --Added by Bgowrava for Bug#7503308
1287                                     cp_attribute1           JAI_RCV_TRANSACTIONS.attribute1%type)  --Added by Bgowrava for Bug#7503308
1288   is
1289     select attribute2 excise_costing_flag
1290     from   jai_rcv_transactions jrcvt
1291     where  jrcvt.parent_transaction_id = cp_rcv_transaction_id
1292   and    jrcvt.organization_id  = cp_organization_id --Added by Bgowrava for Bug#7503308
1293     and jrcvt.shipment_header_id = cp_shipment_header_id --Added by Bgowrava for Bug#7503308
1294     and    jrcvt.transaction_type = cp_txn_type  --'DELIVER'   --Modified by Bgowrava for Bug#7503308
1295     and    jrcvt.attribute1= cp_attribute1 ; --'CENVAT_COSTED_FLAG';  ----Modified by Bgowrava for Bug#7503308
1296 
1297   /* End 5763527 */
1298 
1299 
1300   -- Bug 5401111. Added by Lakshmi Gopalsami
1301   lv_account_type gl_code_combinations.account_type%TYPE;
1302 
1303   /* Service Tax */
1304   cursor c_rcv_transactions(p_transaction_id  number) is
1305      select po_line_id, organization_id
1306      from   rcv_transactions
1307      where  transaction_id = p_transaction_id;
1308 
1309 
1310    cursor c_po_lines_all(p_po_line_id number) is
1311      select item_id
1312      from   po_lines_all
1313      where  po_line_id = p_po_line_id;
1314 
1315    /* Bug 5358788. Added by Lakshmi Gopalsami
1316     * Added parameter cp_regime_code instead of
1317     * hardcoding the value for service. This is required
1318     * for re-using the same cursor to avoid performance issue
1319     * in using jai_regime_tax_types_v.
1320     */
1321    cursor c_jai_regimes(cp_regime_code IN  JAI_RGM_DEFINITIONS.regime_code%TYPE)
1322    IS
1323      select regime_id
1324      from   JAI_RGM_DEFINITIONS
1325      where  regime_code = cp_regime_code ; /* SERVICE or VAT */
1326 
1327    cursor c_regime_tax_type(cp_regime_id  number, cp_tax_type varchar2) is
1328     select attribute_code tax_type
1329     from   JAI_RGM_REGISTRATIONS
1330     where  regime_id = cp_regime_id
1331     and    registration_type = jai_constants.regn_type_tax_types  /* TAX_TYPES */
1332     and    attribute_code = cp_tax_type;
1333 
1334 
1335    r_rcv_transactions               c_rcv_transactions%rowtype;
1336    r_po_lines_all                   c_po_lines_all%rowtype;
1337    r_jai_regimes                    c_jai_regimes%rowtype;
1338    r_service_regime_tax_type        c_regime_tax_type%rowtype;
1339    lv_is_item_an_expense            varchar2(20); /* Service */
1340    lv_accounting_method_option      varchar2(20); /* Service */
1341    lv_accrue_on_receipt_flag        po_distributions_all.accrue_on_receipt_flag%type;
1342   /* Service Tax */
1343    ln_base_amount number ;  -- kunkumar for bug  5593895
1344   /* Bug 5358788. Added by Lakshmi Gopalsami
1345    * Defined variable for fetching VAT regime/
1346    */
1347 
1348   r_VAT_regime_tax_type        c_regime_tax_type%rowtype;
1349   r_vat_regimes                 c_jai_regimes%rowtype;
1350 
1351   /* Added by Brathod, Bug# 4445989 */
1352   --> Cursor to fetch the maximum line_number for current invoice, Bug# 4445989
1353   CURSOR cur_get_max_line_number
1354   IS
1355   SELECT max (line_number)
1356   FROM   ap_invoice_lines_all
1357   WHERE  invoice_id = inv_id;
1358 
1359   --> Cursor to fetch maximum line from ap_invoice_lines_all for current invoice, Bug# 4445989
1360   CURSOR cur_get_max_ap_inv_line (cpn_max_line_num AP_INVOICE_LINES_ALL.LINE_NUMBER%TYPE)
1361   IS
1362   SELECT   accounting_date
1363           ,period_name
1364           ,deferred_acctg_flag
1365           ,def_acctg_start_date
1366           ,def_acctg_end_date
1367           ,def_acctg_number_of_periods
1368           ,def_acctg_period_type
1369           ,set_of_books_id
1370     ,wfapproval_status -- Bug 4863208
1371   FROM    ap_invoice_lines_all
1372   WHERE   invoice_id = inv_id
1373   AND     line_number = cpn_max_line_num;
1374 
1375   -- Added by Jason Liu for retroactive price on 2008/01/09
1376   -----------------------------------------------------------
1377   CURSOR get_source_csr IS
1378   SELECT aia.source
1379   FROM ap_invoices_all aia
1380   WHERE aia.invoice_id = inv_id;
1381   lv_source  ap_invoices_all.source%TYPE;
1382   -----------------------------------------------------------
1383   rec_max_ap_lines_all    CUR_GET_MAX_AP_INV_LINE%ROWTYPE;
1384 
1385   ln_max_lnno  NUMBER;
1386   ln_inv_line_num AP_INVOICE_LINES_ALL.LINE_NUMBER%TYPE;
1387   ln_user_id  NUMBER;
1388   ln_login_id NUMBER;
1389   lv_misc     VARCHAR2 (15);
1390   lv_match_type VARCHAR2(15);
1391   /* End of Bug# 4445989 */
1392   -- Bug 7249100. Added by Lakshmi Gopalsami
1393   lv_dist_class VARCHAR2(30);
1394 
1395 
1396   FUNCTION update_payment_schedule (p_total_tax NUMBER) RETURN boolean IS -- bug # 3218978
1397 
1398     v_total_tax_in_payment  number;
1399     v_tax_installment       number;
1400     v_payment_num           ap_payment_schedules_all.payment_num%type;
1401     v_total_payment_amt     number;
1402     v_diff_tax_amount       number;
1403 
1404     cursor  c_total_payment_amt is
1405     select  sum(gross_amount)
1406     from    ap_payment_schedules_all
1407     where   invoice_id = inv_id;
1408 
1409   begin
1410 
1411   Fnd_File.put_line(Fnd_File.LOG, 'Start of function  update_payment_schedule');
1412 
1413     open  c_total_payment_amt;
1414     fetch   c_total_payment_amt into v_total_payment_amt;
1415     close   c_total_payment_amt;
1416 
1417   if nvl(v_total_payment_amt, -1) = -1 then --Modified by kunkumar for 5593895
1418     Fnd_File.put_line(Fnd_File.LOG, 'Cannot update payment schedule, total payment amount :'
1419                                     || to_char(v_total_payment_amt));
1420     return false;
1421 Elsif 	v_total_payment_amt = 0 Then
1422          v_total_payment_amt := 1;
1423   end if;
1424 
1425     v_total_tax_in_payment := 0;
1426 
1427     for c_installments in
1428     (
1429     select  gross_amount,
1430         payment_num
1431     from    ap_payment_schedules_all
1432     where   invoice_id = inv_id
1433     order by payment_num
1434     )
1435     loop
1436 
1437       v_tax_installment := 0;
1438       v_payment_num  :=  c_installments.payment_num;
1439 
1440       v_tax_installment := p_total_tax * (c_installments.gross_amount / nvl(v_total_payment_amt,1));
1441 
1442       v_tax_installment := round(v_tax_installment, ln_precision);
1443 
1444       update ap_payment_schedules_all
1445       set    gross_amount        = gross_amount          + v_tax_installment,
1446          amount_remaining      = amount_remaining      + v_tax_installment,
1447          inv_curr_gross_amount = inv_curr_gross_amount + v_tax_installment,
1448             payment_status_flag = decode(payment_status_flag, 'Y', 'P', payment_status_flag)
1449             -- bug#3624898
1450       where  invoice_id = inv_id
1451       and    payment_num = v_payment_num;
1452 
1453       v_total_tax_in_payment := v_total_tax_in_payment + v_tax_installment;
1454 
1455     end loop;
1456 
1457     -- any difference in tax because of rounding has to be added to the last installment.
1458     if v_total_tax_in_payment <> p_total_tax then
1459 
1460       v_diff_tax_amount := round( p_total_tax - v_total_tax_in_payment,ln_precision);
1461 
1462       update ap_payment_schedules_all
1463       set    gross_amount        = gross_amount          + v_diff_tax_amount,
1464          amount_remaining      = amount_remaining      + v_diff_tax_amount,
1465          inv_curr_gross_amount = inv_curr_gross_amount + v_diff_tax_amount
1466       where  invoice_id = inv_id
1467       and    payment_num = v_payment_num;
1468     end if;
1469 
1470     return true;
1471 
1472   exception
1473     when others then
1474       Fnd_File.put_line(Fnd_File.LOG, 'exception from function  update_payment_schedule');
1475       Fnd_File.put_line(Fnd_File.LOG, sqlerrm);
1476       return false;
1477   end update_payment_schedule; -- bug # 3218978
1478 
1479   -- Start added for bug#3332988
1480   procedure insert_mrc_data (p_invoice_distribution_id number) is
1481     -- Vijay Shankar for bug#3461030
1482     v_mrc_string VARCHAR2(10000);
1483   begin
1484 
1485        v_mrc_string := 'BEGIN AP_MRC_ENGINE_PKG.Maintain_MRC_Data (
1486       p_operation_mode    => ''INSERT'',
1487       p_table_name        => ''AP_INVOICE_DISTRIBUTIONS_ALL'',
1488       p_key_value         => :a,
1489       p_key_value_list    => NULL,
1490       p_calling_sequence  =>
1491       ''India Local Tax line as Miscellaneous distribution line (jai_ap_match_tax_pkg.process_online procedure)''
1492        ); END;';
1493 
1494       -- Vijay Shankar for bug#3461030
1495       EXECUTE IMMEDIATE v_mrc_string USING p_invoice_distribution_id;
1496 
1497   -- Vijay Shankar for bug#3461030
1498   exception
1499     when others then
1500     IF SQLCODE = -6550 THEN
1501       -- object referred in EXECUTE IMMEDIATE is not available in the database
1502       null;
1503       FND_FILE.put_line(FND_FILE.log, '*** MRC API is not existing(insert)');
1504     ELSE
1505       FND_FILE.put_line(FND_FILE.log, 'MRC API exists and different err(insert)->'||SQLERRM);
1506       RAISE;
1507     END IF;
1508   end;
1509 
1510 
1511   procedure update_mrc_data is
1512     -- Vijay Shankar for bug#3461030
1513     v_mrc_string VARCHAR2(10000);
1514   begin
1515 
1516        v_mrc_string := 'BEGIN AP_MRC_ENGINE_PKG.Maintain_MRC_Data (
1517       p_operation_mode    => ''UPDATE'',
1518       p_table_name        => ''AP_INVOICES_ALL'',
1519       p_key_value         => :a,
1520       p_key_value_list    => NULL,
1521       p_calling_sequence  =>
1522       ''India Local Tax amount added to invoice header (jai_ap_match_tax_pkg.process_online procedure)''
1523        ); END;';
1524 
1525       -- Vijay Shankar for bug#3461030
1526       EXECUTE IMMEDIATE v_mrc_string USING inv_id;
1527 
1528   -- Vijay Shankar for bug#3461030
1529   exception
1530     when others then
1531     IF SQLCODE = -6551 THEN --Modified by kunkumar for  5593895
1532       -- object referred in EXECUTE IMMEDIATE is not available in the database
1533       null;
1534       FND_FILE.put_line(FND_FILE.log, 'MRC API is not existing(update)');
1535     ELSE
1536       FND_FILE.put_line(FND_FILE.log, 'MRC API exists and different err(update)->'||SQLERRM);
1537       RAISE;
1538     END IF;
1539   end;
1540   /* End added for bug#3332988 */
1541 
1542   -- start bug#4103473
1543   FUNCTION apportion_tax_4_price_cor_inv (p_tax_amount  number, p_tax_id number) return number is
1544 
1545       -- get the corresponding base line amount in original invoice
1546     cursor c_check_orig_item_line(p_price_correct_inv_id number, p_po_distribution_id number) is
1547     select amount, invoice_distribution_id
1548     from   ap_invoice_distributions_all
1549   WHERE  invoice_id = p_price_correct_inv_id
1550   and    po_distribution_id = p_po_distribution_id
1551   AND    line_type_lookup_code = 'ITEM';
1552 
1553 
1554   --  get the corresponding tax lin amount in the original invoice
1555    cursor c_get_orig_tax_line
1556   (p_orig_invoice_id number, p_orig_invoice_dist_id number, p_tax_id number) is
1557   -- 5763527 , Added SUM as there can be two tax lines against the same tax_id because of partialy recoverable taxes
1558   select  sum(jam.tax_amount) tax_amount-- project costing fwd porting
1559   from    JAI_AP_MATCH_INV_TAXES jam,jai_cmn_taxes_all jct
1560   where   p_orig_invoice_id = p_orig_invoice_id
1561   and   parent_invoice_distribution_id = p_orig_invoice_dist_id
1562   and   jam.tax_id = p_tax_id
1563   --start additions for bug#10167393
1564   /*adhoc tax shall not be apportioned on PRICE CORRECTION*/
1565   and jam.tax_id=jct.tax_id
1566   and (
1567   nvl(jct.adhoc_flag,'N')='Y'
1568        or
1569      --( jct.adhoc_flag='Y' and jct.tax_type not in  ('Freight','Insurance','Octrai','Other','PURCHASE TAX','ENTRY TAX')));
1570 	 ( jct.adhoc_flag='Y' and jct.tax_type not in  ('Freight','Insurance','Octrai','Other','PURCHASE TAX','ENTRY TAX','Excise','EXCISE_EDUCATION_CESS','EXCISE_SH_EDU_CESS'))); -- for bug 16195110 by anupgupt
1571 
1572   c_check_orig_item_line_rec  c_check_orig_item_line%rowtype;
1573   c_get_orig_tax_line_rec   c_get_orig_tax_line%rowtype;
1574   v_amount          number;
1575   cur_price_correct_inv_id  NUMBER(15);
1576   cur_po_distribution_id    NUMBER(15);
1577   cur_amount                NUMBER;
1578   cur_quantity_invoiced     NUMBER;
1579   v_count NUMBER;
1580   begin
1581 
1582      Fnd_File.put_line(Fnd_File.LOG, 'Start of function apportion_tax_4_price_cor_inv');
1583 
1584     begin
1585       execute immediate 'select /* price_correct_inv_id */, po_distribution_id, amount, quantity_invoiced
1586                          from   ap_invoice_lines_all
1587                          where  invoice_id = :inv_id
1588                          and    line_number = :inv_line_num'
1589                          into cur_price_correct_inv_id, cur_po_distribution_id, cur_amount, cur_quantity_invoiced
1590                          USING inv_id, pn_invoice_line_number;  -- Using pn_invoice_line_number instead of dist_line_no for Bug#4445989
1591 
1592 
1593      exception
1594       when others  then
1595         return p_tax_amount;
1596     end;
1597 
1598   -- control comes here when it is a case of price correction
1599    open c_check_orig_item_line
1600    (cur_price_correct_inv_id, cur_po_distribution_id);
1601    fetch c_check_orig_item_line into c_check_orig_item_line_rec;
1602    close c_check_orig_item_line;
1603 
1604    open c_get_orig_tax_line
1605    (
1606    cur_price_correct_inv_id,
1607    c_check_orig_item_line_rec.invoice_distribution_id,
1608    p_tax_id
1609    );
1610    fetch c_get_orig_tax_line into c_get_orig_tax_line_rec;
1611    close c_get_orig_tax_line;
1612 
1613      if c_check_orig_item_line_rec.amount = -1 then --Modified by kunkumar for  5593895
1614      Fnd_File.put_line(Fnd_File.LOG, 'Original item line has -1 amount, cannot apportion tax');--Modified by kunkumar for  5593895
1615        return p_tax_amount;
1616      end if;
1617 
1618    v_amount := ( c_get_orig_tax_line_rec.tax_amount / c_check_orig_item_line_rec.amount)
1619          * cur_amount;
1620 
1621    Fnd_File.put_line(Fnd_File.LOG, ' original amount / tax :' || c_check_orig_item_line_rec.amount
1622                    || '/' || c_get_orig_tax_line_rec.tax_amount);
1623 
1624    return round(v_amount, 2);
1625 
1626 
1627   exception
1628     when others then
1629       Fnd_File.put_line(Fnd_File.LOG, 'Exception function apportion_tax_4_price_cor_inv :' || sqlerrm);
1630       return p_tax_amount;
1631   end apportion_tax_4_price_cor_inv;
1632 
1633 
1634   -- End bug#4103473
1635 
1636 
1637 BEGIN
1638 
1639 /*------------------------------------------------------------------------------------------
1640  FILENAME: ja_in_jai_ap_match_tax_pkg.process_online_p.sql
1641  CHANGE HISTORY:
1642 S.No      Date          Author and Details
1643 1.       20-mar-01      Subbu and Pavan for AP to FA issue added
1644                         insert statemnt for temp table JAI_CMN_FA_INV_DIST_ALL
1645 
1646 2.       29-mar-01      Subramanyam S.K added modifications for STForm
1647                         Tracking at AP level. Insert statments for JA_in_po_st_forms_hdr
1648                         and ja_in_po_st_forms_dtl after validating whether
1649                         the tax line is Sales Tax or CST and for these taxes
1650                         stform_type(JAI_CMN_TAXES_ALL) is not null.
1651 
1652 3.       20-apr-01      Modifications done by Ajay Sharma for correct calculation
1653                         of taxes and invoice amount at the time of RCV_MATCHING
1654 
1655 4.       17-aug-01      Code Added by Vijay for Multi-Org support of ST Form Tracking
1656 
1657 5.       24-Aug-01      Modification done by Ajay Sharma to insert records in case of
1658                         running of pay on receipt for subsequent receipt against same PO
1659 
1660 6.       08-Sep-01      Modifications done by Pavan to care of the split payment terms
1661 
1662 7.       09-Mar-02      RPK: for BUG#2255404
1663                         Code modified to facilitate the invoice matching with more than
1664                         one receipt.When the invoice is matched to more than one receipts
1665                         getting the error 'Unique constraint(AP.AP_INVOICE_DITRIBUTIONS_U1) violated
1666 
1667 8.       22-may-2002    Aparajita for bug # 2387481.
1668                         The calculation of tax amount for RCV_MATCHING case when there exists
1669                         some return to vendor for the receipt, was not correct.
1670 
1671                         Corrected it by using quantity_shipped in the r_for_shipment_line
1672                         cursor instead of quantity_received.
1673                         Quantity_shipped gives the original quantity of the receipt.
1674 
1675                         Added the Fnd_File.put_line(Fnd_File.LOG,'') statements to
1676                         generate the log also.
1677 
1678 9.       09/10/2002     Aparajita for bug # 2483164 Version#615.1
1679                         Populate the po distribution id and rcv_transactions id for the tax
1680                         lines for backtracking tax to purchasing side.
1681 
1682 
1683 10.       03/11/2002    Aparajita for bug # 2567799  Version#615.2
1684                        Added a function apportion_tax_pay_on_rect_f and using it to apportion
1685                        the tax amount. This is useful when there are multiple distributions for the PO.
1686 
1687                        Deleted the code for supplementary invoices as this is not being used.
1688 
1689 11.       05/11/2002    Aparajita for bug # 2506453, Version#615.3
1690 
1691                        Changed the cursor definition get_tax_ln_no, to look into the receipt tax
1692                        instead of the po tax table for getting the line number.
1693                        This was running into error when no taxes exist at po and have been
1694                        added at receipt.
1695 
1696                        Added the cursor get_tax_ln_no_po, to fetch the tax line number from PO
1697                        if it is the case of PO matching.
1698 
1699                        Also Changed the cursor definition get_sales_tax_dtl to fetch on the
1700                        basis of tax id instead of name.
1701 
1702 12.      02/12/2002    Aparajita for bug # 2689826 Version # 615.4
1703 
1704                        The distribution line numbers were getting  skipped every time the tax is
1705                        inserted for a 2nd item line onwards. This was happening because the
1706                        distribution line number is calulated by taking the max distribution
1707                        line number and also the no of tax lines which have a shipment line id
1708                        less than corresponding shipment line id for the same shipment header.
1709 
1710                        This logic is not required as the distribution line number should
1711                        always be the next number. commented the cursor count_tax_lines.
1712 
1713 13.      14/01/2003     Aparajita for bug # 2694011. Version # 615.5
1714 
1715                        The taxes in case of PO matching were wrong when the price is changed
1716                        at PO matching. Added code to check the price at invoice to the price
1717                        at PO and apportioning the tax amount in case of a price change.
1718                        This is applicable to non-adhoc taxes only as they depend on the po amount.
1719 
1720                        Added unit price in cursor for_dist_insertion to get invoice price.
1721                        Added  price_override in cursor for_line_qty to get po price.
1722                        Added code to compare the above prices and apportion the calculated
1723                        tax in case of change.
1724 
1725                        Used the cursor for_line_qty in case of receipt matching with the
1726                        line location id from rcv_transactions to get the price.
1727                        Price of a receipt cannot be changed, so same as po price.
1728                        Logic of apportioning was also added in receipt matching cases.
1729 
1730 14       31/01/2003    Aparajita for bug # 2775043. Version # 615.6
1731                        When price is changed at the time of po/receipt matching the tax amount is
1732                        apportioned appropriately. This should also hit the invoice price variance
1733                        account, but this is not happening as base apps expects the variance amount
1734                        and account code to be loaded into the following fields.
1735 
1736                         - price_var_code_combination_id
1737                         - invoice_price_variance
1738                         - base_invoice_price_variance (in functional currency)
1739 
1740 15       21/02/2003     Aparajita for bug # 2805527. Version # 615.7
1741                        This procedure is run as a concurrent, this concurrent should be
1742                        incompatible to itself as more than 1 request may be entering tax lines for
1743                        the same invoice. For some reason if the incompatibility is not set or
1744                        does not work, this concurrent runs into error.
1745 
1746                        Added the concept of locking the distribution lines so that even if they
1747                        run concurrently there should not be any error.
1748                        The code was using two variables dno and dlno for distribution
1749                        number,
1750 
1751                        stream lines it to consider only v_distribution_no and changed the related logic.
1752 
1753 16.      14/03/2003    Aparajita for bug # 2841363. Version # 615.8
1754                        Taxes having 0 amount were not considered, changed the cursor
1755                        from_line_location_taxes to consider such tax lines for propagation into AP.
1756 
1757 17.      23/03/2003    Vijay Shankar for Bug# 2827356. Version # 615.9
1758                         When Receipt line Uom is different from PO shipment line Uom,
1759                         then Invoice Matching with Receipt is calculating taxes wrongly for distribution
1760                         item lines. This is resolved by incorporating Receipt Line to PO Shipment
1761                         line UOM converion rate to be multiplied with PO Shipment price (used to proportionate the receipt tax along with invoice distribution price).
1762                         This issue happens only during receipt matching.
1763 
1764 18.      07/04/2003    Aparajita for bug # 2851123. Version # 615.10
1765                        The assets_tracking_flag in ap_invoice_distributions_all,
1766                        JAI_AP_MATCH_INV_TAXES and JAI_CMN_FA_INV_DIST_ALL should
1767                        be set as 'N' if the tax line against which the line is being generated
1768                        is excise type of tax and is modvatable. By default this flag gets the value
1769                        from the corresponding item line for which the tax is attached.
1770 
1771                        Introduced a variable v_assets_tracking_flag.
1772                        Modified the following cursors to fetch modvat_flag.
1773                        - from_line_location_taxes
1774                        - tax_lines1_cur
1775                        - r_tax_lines_cur
1776 
1777 19      17/07/2003     Aparajita for bug#3038566. Version#616.1
1778 
1779                        Introduced a new function getSTformsTaxBaseAmount to calculate the
1780                        tax base amount to be populated into the table ja_in_po_st_forms_dtl.
1781                        Calculating the tax base amount from the tax amount and percentage was
1782                        giving problem because of rounding.
1783 
1784 20     14/08/2003     kpvs for bug # 3051828, version # 616.2
1785                       OPM code merged into this version of the procedure.
1786                       Changes made to pull in the OPM code fixes done for bugs 2616100
1787                       and 2616107 Used 'v_opm_flag' to check for the process enabled flag of the
1788                       organization.
1789 
1790 21.    17/08/2003     Aparajita for bug#3094025. Generic for bug#3054140. version # 616.3
1791                       There may be taxes at receipt without having any tax at PO.
1792                       For such cases, the st forms population has to cater.
1793                       Precedences are not available, so calculation of tax base is not possible.
1794                       Added the backward calcultion for such cases.
1795 
1796                       Changes in the function getSTformsTaxBaseAmount.
1797                       Introduced two new parameters tax amount and rate to calculate backward
1798                       in case PO tax details are not found.
1799 
1800                       It was observed that the function getSTformsTaxBaseAmount was always
1801                       considering the precedence 0 amount irrespective of the valus of the precendences.
1802                       This has been corrected.
1803 
1804 22.   22/08/2003    Aparajita for bug# 2828928. Version # 616.4
1805                       Added the condition to consider null vendor id and modvat = 'N'taxes in the following
1806                       cursors. Also added the code to consider mod_cr_percentage.
1807 
1808                       - C_third_party_tax_recipt
1809                       - C_third_party_tax_po
1810 
1811 
1812 23.     27/08/2003    Aparajita for bug#3116659. Version#616.5
1813                       Projects clean up.
1814 
1815 24.   27/08/2003     Vijay Shankar for bug# 3096578. Version # 616.6
1816                        All taxes are not picked from PO Shipment if the taxes are selected based on
1817                        line_focus_id as there are different line_focus_id's for same line_location_id
1818                        which is wrong.Also we should use line_location_id to fetch the taxes instead of
1819                        line_focus_id as line_location_id refers to the PO Shipments in which taxes are
1820                        attached and line_focus_id is just a unique key for JAI_PO_LINE_LOCATIONS table.
1821                        Modified the cursor from_line_locations_taxes to fetch the taxes based on
1822                        line_location_id.
1823 
1824 25      16/10/2003    Aparajita for bug#3193849. Version#616.7
1825 
1826                       Removed the ST forms functionality from here as ST forms population is now being
1827                       handled through a concurrent.
1828 
1829 26    28/10/2003      Aparajita for bug#3206083. Version#616.7
1830 
1831                       If the base invoice is cancelled, there is no need to bring the tax lines.
1832                       Added code to check the cancelled date and if populated, returning from the proc.
1833 
1834 27    28/10/2003    Aparajita for bug#3218978. Version#616.7
1835 
1836                       Tax amount is apportioned properly between the installments.
1837                       Differential tax amounts if any because of rounding is added to the last installment.
1838 
1839                       Code is not reading anything from terms master, instead based on the
1840                       distribution of the amount before tax, tax amount is distributed. coded in
1841                       in line function update_payment_schedule.
1842 
1843 
1844 28      23/12/2003    Aparajita for bug#3306090. Version 618.1
1845                       Tax amount was calculated wrongly when tax currency is different from
1846                       invoice currency. This was not handled in the function
1847                       apportion_tax_pay_on_rect_f. Added code for the same.
1848 
1849 29    07/01/2003    Aparajita for bug#3354932. Version#618.2
1850                     When the invoice is in foreign currency, for ERS invoice, base_amount in
1851                     ap_invoices_all should be updated to include the loc taxes as otherwise
1852                     it is creating wrong accounting entries.
1853 
1854                     This update needs to be done only for ERS invoices as other cases localization
1855                     does not update the invoice header amounts. p_rematch = 'PAY_ON_RECEIPT'.
1856                     Additionally this should be done only when the invoice currency is not
1857                     the functional currency.
1858 
1859 30      27/01/2004   Aparajita for bug#3332988. Version#618.3
1860 
1861                     Call to AP_MRC_ENGINE_PKG.Maintain_MRC_Data for MRC functionality.
1862 
1863                     This call is made after every distribution line is inserted.
1864                     This has been implemented as a in line procedure insert_mrc_data.
1865 
1866                     The procedure is called after every insert into ap_invoice_distributions_all.
1867 
1868                     Procedure update_mrc_data has been created and invoked whereever invoice
1869                     header is being updated.
1870 
1871 31     03/02/2004   Aparajita for bug#3015112. Version#618.4.
1872                     Removed the OPM costing functionality.
1873                     This was updating quantity invoiced and unit price for miscellaneous lines
1874                     and this was creating problem for subsequent transactions against the same
1875                     PO / Receipt.
1876 
1877                     The changes removed were brought in for bug#3051828.
1878 
1879 32     24/02/2004   Vijay Shankar for bug#3461030. Version#618.5
1880                      Modified the MRC call to be dynamic by using EXECUTE IMMEDIATE.
1881                      This is done to take care of Ct.s who are on base version less than 11.5.7
1882 
1883 33    25/02/2004   Aparajita for bug#3315162. Version#618.6
1884 
1885                     Base application gives problem while reversing localization tax lines.
1886                     This was analyzed with base and found out that the problem was because of
1887                     null value for matched_uom_lookup_code field of the miscellaneous tax distribution.
1888                     Further null value of quantity also creats problem as the billed quantity
1889                     on PO/receipt gets updated to null when the loc taxes does get reversed with
1890                     value populated for UOM.
1891 
1892                     Changes have been made to populate matched_uom_lookup_code same as item line
1893                     and quantity as 0.
1894 
1895 34    30/03/2004   Aparajita for bug#3448803. Version#619.1.
1896 
1897                      track as asset flag for the distribution for tax line in payable invoice now
1898                      considers partially recoverable taxes. The logic is now as follows,
1899 
1900                      - default value of this flag is same as that of the corresponding item line.
1901                      - if the corresponding tax is modvatable to the extent of 100% then the value of
1902                      the flag is over written to 'N' irrespective of the value of the corresponding
1903                      base item line.
1904 
1905 35      15/05/2004   Aparajita for bug#3624898. Version#619.2
1906                      Whenever the tax lines are populated, if the invoice is already fully paid
1907                      the paid amount is displayed wrongly to be including the tax amount.
1908 
1909                      Fixed the problem by updating the payment_status_flag in ap_invoices_all and
1910                      ap_payment_shedules_all to 'P' in case it was Y.
1911 
1912 36      21/05/2004   Aparajita for bug#3632810. Version#115.1
1913 
1914                      Tax lines are apportioned when being brought over to AP for changes like
1915                      price, quantity etc. In cases where the UOM has been changed, it triggers a
1916                      change in price too. In such cases, UOM is also taken into account for changes
1917                      in quantity and price.
1918 
1919                      UOM at PO was being checked always from po_line_locations. But there are cases
1920                      like BPA, where the UOM is in po_lines_all in stead of po_line_locations_all.
1921                      Changed the code to check from po_lines_all whenever UOM does not exist in
1922                      Po_line_locations_all.
1923 
1924 37     11/06/2004    Aparajita for bug#3671967. Version#115.2
1925 
1926                      When the tax line being brought over from PO by way of PO matching,
1927                      for completely recoverable taxes and a tax account has been defined for the tax
1928                      then tax account should be used.
1929 
1930                      This is applicable for PO matching only.
1931 
1932 38    08/07/2004     Aparajita for bug#3752887. Version#115.3. Clean up.
1933 
1934                      Function apportion_tax_pay_on_rect_f was considering the quantity
1935                      from rcv_transactions and  taxes from JAI_RCV_LINE_TAXES.
1936                      This fails when there have been corrections as taxes are modified as
1937                      per the corrected quantity.
1938 
1939                      Cleaned the entire logic of apportioning of tax from PO to AP.
1940                      Used the function jai_ap_interface_pkg.get_apportion_factor which has been
1941                      designed new to find out the factor that needs to be applied. All Logic
1942                      of apportioning is now in the external function and removed a lot of code
1943                      from here which was trying to do the same here at more than one place.
1944                      Also rmoved the in line function function apportion_tax_pay_on_rect_f.
1945 
1946                      Function jai_ap_interface_pkg.get_apportion_factor was designed for purchase register report
1947                      through bug#3633078. Added the procedure here also in the patch as it is not
1948                      required to have the report to have the function. So no dependency.
1949 
1950                      Changes done via bug#2775043 earlier in this procedure for IPV for tax
1951                      line  was redone as it would again involve details of apportion.
1952                      Instead used the following logic to arrive at IPV amount.
1953 
1954                      tax invoice price variance :=
1955                      tax amount * (base item line variance amount / base item line amount).
1956 
1957 39. 01/11/2003       ssumaith - bug# 3127834 - file version 115.4
1958 
1959                      currency precision was not used for rounding the tax amounts when inserting / updating
1960                      records into  ap_invoices_all , ap_invoices_distributions_all , ap_payment_Schedules_all
1961                      tables. As a result the invoice was going into automatic hold because the invoice amount
1962                      does not equal sum of distributions amount.
1963 
1964                      This has been resolved in the bug by getting the currency precision for the invoice
1965                      currency from FND_CURRENCIES table and using it in all the inserts and updates which
1966                      involve the tax amounts.
1967 
1968 40. 30/12/2004       Aparajita - Bug#4095234. Version#115.5
1969 
1970                      Cursor r_cur_items_rec was getting used without being opened. There is no need to use this,
1971                      replaced with cur_items_rec. Removed the cursor r_cur_items.
1972 
1973 41. 21/01/2005       Aparajita - Bug#4078546 Service Tax. Version#115.6
1974 
1975                      4106633  Base Bug For Education Cess on Excise, CVD and Customs.
1976                      4059774  Base Bug For Service Tax Regime Implementation.
1977                      4078546  Modification in receive and rtv accounting for service tax
1978 
1979 
1980                      Service Tax interim account needs to be populated as the charge account,
1981                      if the following condition is true.
1982 
1983                      Case of PO Matching or Accrue on receipt is No.
1984 
1985 42. 11/02/2005      Aparajita - Bug#4177452. Version#115.7
1986 
1987                     Charge account for non-recoverable taxes were going was null.
1988                     This was because of the default assignment of,
1989                       v_dist_code_combination_id := for_dist_insertion_rec.dist_code_combination_id;
1990 
1991                     inside the if of recoverable taxes.
1992 
1993                     Changed it to after the end if.
1994 
1995 43  03/03/2005      Sanjikum for bug#4103473 Version 115.8
1996 
1997                     Price Correction. Added an in line function apportion_tax_4_price_cor_inv for
1998                     calculating the tax amount for price correction cases. This is being called before
1999                     each insertion of tax line if the tax amount is 0.
2000 
2001 
2002 44  12/03/2005      Bug 4210102. Added by LGOPALSA Version 115.9
2003                     (1) Added CVD, Excise and Customs education cess
2004         (2) Added Check file syntax in dbdrv command
2005         (3) Added NOCOPY for OUT variables
2006 
2007 45 8/05/2005        rchandan for bug#4333488. Version 116.1
2008                     The Invoice Distribution DFF is eliminated and a new global DFF is used to
2009                     maintain the functionality. From now the TDS tax, WCT tax and ESSI will not
2010                     be populated in the attribute columns of ap_invoice_distributions_all table
2011                     instead these will be populated in the global attribute columns. So the code changes are
2012                     made accordingly.
2013 
2014 46 10-Jun-2005      rallamse for bug#  Version 116.2
2015                     All inserts into JAI_AP_MATCH_INV_TAXES table now include legal_entity_id as
2016                     part of R12 LE Initiative. The legal_entity_id is selected from ap_invoices_all in
2017                     cursor get_ven_info.
2018 
2019 47  23-Jun-2005     Brathod for Bug# 4445989, Version 120.0
2020                     Issue: Impact uptake on IL Product for AP_INVOICE_LINES_ALL Uptake
2021                     Resolution:- Code modified to consider ap_invoice_lines_all instead of
2022                     ap_invoice_distributions_all.
2023                     Following changes done
2024                       -  Code refering to ap_invoice_distributions_all modified to consider
2025                          invoice_id and invoice_line_number as unique combination
2026                       -  invoice line record is created in ap_invoice_lines_all where ever previously
2027                          distribution was created
2028                       -  Obsoleted JAI_CMN_FA_INV_DIST_ALL
2029                       -  Modified structure of JAI_AP_MATCH_INV_TAXES to incorporate invoice lines
2030                          and also the fields from JAI_CMN_FA_INV_DIST_ALL
2031                       -  Code modfied to insert appropriate fields in JAI_AP_MATCH_INV_TAXES
2032 
2033 48. 06-Jul-2005       Sanjikum for Bug#4474501
2034                       1) Commented the cursor - for_acct_id and corresponding open/fetch/close for the same.
2035                       2) Commented the call to function - jai_general_pkg.get_accounting_method
2036 
2037 49. 25-Apr-2007       CSahoo for bug#5989740, File Version 120.14
2038                       Forward Porting of 11i bug#5907436
2039                       ENH: Handling of secondary and higher education cess.
2040                       Added the sh cess tax types.
2041 
2042 50. 20-dec-2007      eric modified and added code for inclusive tax
2043 
2044 51. 09-Jan-2008      Modifed by Jason Liu for retroactive price
2045 
2046 52. 04-Feb-2008      JMEENA for bug#6780154
2047 
2048          Modify the insert statement of ap_invoice_lines_all to populate the po_distribution_id.
2049 
2050 
2051 53. 21-Feb-2008      JMEENA for bug#6835548
2052          Changed the value of lv_match_type from NOT MATCHED to NOT_MATCHED.
2053 
2054 54. 03-APR-2008      Jason Liu for bug#6918386
2055          Modified the cursor from_line_location_taxes
2056          1) Added the parameter p_source
2057          2) Added the UNION for PPA invoice
2058 55.  03-APR-2008      Jason Liu for bug#66936416
2059          Added a condition to exclude the third party taxes
2060 
2061 56. 19-May-2008     JMEENA for bug#7008161
2062         Modified the insert statement of ap_invoice_distributions_all to populate the charge_applicable_to_dist_id
2063         based on v_assets_tracking_flag and removed the condition of account_type.
2064 
2065  57.  12-Dec-2008  Added by Bgowrava for Bug#7503308, File Version 120.7.12000000.15, 120.34.12010000.4, 120.38
2066                   Issue: INDIA - TO INSERT TAXES FOR PAY ON RECEIPT REQUESTPERFORMANCE ISSUE
2067                   Reason: full table scan on JAI_RCV_TRANSACTIONS table in cursor c_get_excise_costing_flag
2068                   Fix:   Added organization_id and shipment_headder_id columns
2069 
2070 58.  06-Apr-2010   Bgowrava For Bug#9499146, File Version 120.34.12010000.14,
2071                                  Issue: WRONG AP ACCRUAL ACCOUNT TAKING FOR RECOVERABLE TAXES LIKE EXCISE IN AP INVOICE
2072                                  Fix: Modified code so that all taxes will hit the same account as that of the  item line (except service type of taxes)
2073 
2074 59. 07-DEC-2010  Abezgam for Bug#10375894 ,
2075 	Issue: Fixed Assets Mass Addition report is Erroring out.
2076 	Fix: Set the value for variable v_assets_tracking_flag to 'N'.
2077 
2078 60. 10-Feb-2012 Qinglei for Bug#13589613, Bug#13561173, Bug#13696344
2079 
2080 61. 16-Feb-2012 Qinglei for Bug#13561173
2081     Bug#13561173 is same as bug#13513039. Ported code change in 13513039.
2082 
2083 Future Dependencies For the release Of this Object:-
2084 ==================================================
2085 (Please add a row in the section below only if your bug introduces a dependency due to spec change/
2086 A new call to a object/A datamodel change )
2087 
2088 -------------------------------------------------------------------------------------------------------
2089 Version       Bug       Dependencies (including other objects like files if any)
2090 --------------------------------------------------------------------------------------------------------
2091 616.1         3038566   ALTER Script  JAI_AP_MATCH_INV_TAXES Table is altered.
2092 
2093 115.3         3752887   Dependency on function jai_ap_interface_pkg.get_apportion_factor which is also added
2094                         in this patch. So no pre - req.
2095 
2096 115.6         4146708   Base Bug for Service + Cess Release.
2097 
2098                         Variable usage of jai_constants package.
2099 
2100                         Call to following,
2101                         jai_general_pkg.is_item_an_expense
2102                         jai_general_pkg.get_accounting_method
2103                         jai_rcv_trx_processing_pkg.get_accrue_on_receipt
2104                         jai_cmn_rgm_recording_pkg.get_account
2105 
2106 
2107 ------------------------------------------------------------------------------------------------------- */
2108 
2109 
2110   Fnd_File.put_line(Fnd_File.LOG, 'Start procedure - jai_ap_match_tax_pkg.process_online ');
2111 
2112   OPEN  get_ven_info(inv_id);
2113   FETCH get_ven_info INTO get_ven_info_rec;
2114   CLOSE get_ven_info;
2115   -- Using pn_invoice_line_number instead of dist_line_no for Bug#4445989
2116   Fnd_File.put_line(Fnd_File.LOG,
2117   'Input parameters - invoice number(id)/ invoice line no/Matching option :'
2118   || get_ven_info_rec.invoice_num  || '(' || inv_id || ')/' || pn_invoice_line_number || '/' || p_rematch);
2119 
2120 
2121   -- start added by bug#3206083
2122   if get_ven_info_rec.cancelled_date is not null then
2123     -- base invoice has been cancelled, no need to process tax.
2124     Fnd_File.put_line(Fnd_File.LOG, 'Base invoice has been cancelled, no need to process tax');
2125     return;
2126   end if;
2127   -- end added by bug#3206083
2128 
2129   ln_user_id  := fnd_global.user_id;
2130   ln_login_id := fnd_global.login_id;
2131 jai_cmn_utils_pkg.write_fnd_log_msg('Match Tax Code','Inside procedure process online');
2132   jai_cmn_utils_pkg.write_fnd_log_msg('Match Tax Code','Value of invoice_id is '||inv_id);
2133   jai_cmn_utils_pkg.write_fnd_log_msg('Match Tax Code','Value of line number is '||pn_invoice_line_number);
2134 
2135   Open get_discard_info (inv_id);  --Added by nprashar for bug # 12832424
2136   Fetch get_discard_info into lv_discard_flag;
2137   Close get_discard_info;
2138 
2139   Open c_get_ap_invoice_src (inv_id); --Added by nprashar for bug # 12832424 to check value of source
2140   Fetch c_get_ap_invoice_src into ln_source;
2141   Close c_get_ap_invoice_src;
2142 
2143   jai_cmn_utils_pkg.write_fnd_log_msg('Match Tax Code','Value of disacrd flag is'||lv_discard_flag);
2144 
2145  If lv_discard_flag = 'Y' and ln_source <> 'ERS' then -- Main code starts for bug # 12832424
2146   jai_cmn_utils_pkg.write_fnd_log_msg('Match Tax Code','Inside condition of the discard flag');
2147   For ident_rec in c_match_tax_lines ( inv_id)
2148    Loop
2149       jai_cmn_utils_pkg.write_fnd_log_msg('Match Tax Code','Working on the follwoing tax lines');
2150           jai_cmn_utils_pkg.write_fnd_log_msg('Match Tax Code','Value of invoice_id'||ident_rec.invoice_id);
2151           jai_cmn_utils_pkg.write_fnd_log_msg('Match Tax Code','Value of distribution_id'||ident_rec.po_distribution_id);
2152           jai_cmn_utils_pkg.write_fnd_log_msg('Match Tax Code','Value of line_number '||ident_rec.invoice_line_number);
2153           jai_cmn_utils_pkg.write_fnd_log_msg('Match Tax Code','Value of invoice_distribution_id '||ident_rec.invoice_distribution_id);
2154 
2155     select ap_invoice_distributions_s.nextval into ln_invoice_dist_id_rev from dual;
2156 
2157 For ins_reverse_dist_lines in c_inv_dist_tax_lines ( ident_rec.invoice_id, ident_rec.invoice_distribution_id)
2158       Loop
2159          jai_cmn_utils_pkg.write_fnd_log_msg('Match Tax Code','Going inside Loop of reversal');
2160                  jai_cmn_utils_pkg.write_fnd_log_msg('Match Tax Code','Value of invoice_id'||ident_rec.invoice_id);
2161          jai_cmn_utils_pkg.write_fnd_log_msg('Match Tax Code','Value of invoice_distribution_id '||ident_rec.invoice_distribution_id);
2162 
2163 
2164          ln_invoice_distribution_no := ins_reverse_dist_lines.distribution_line_number + 1;
2165 
2166         INSERT INTO ap_invoice_distributions_all
2167         (
2168         accounting_date,
2169         accrual_posted_flag,
2170         assets_addition_flag,
2171         assets_tracking_flag,
2172         cash_posted_flag,
2173         distribution_line_number,
2174         dist_code_combination_id,
2175         invoice_id,
2176         line_type_lookup_code,
2177         period_name,
2178         set_of_books_id ,
2179         amount,
2180         base_amount,
2181         batch_id,
2182         description,
2183         exchange_rate_variance,
2184         match_status_flag,
2185         posted_flag,
2186         rate_var_code_combination_id ,
2187         reversal_flag ,
2188         program_application_id,
2189         program_id,
2190         program_update_date,
2191         accts_pay_code_combination_id,
2192         invoice_distribution_id,
2193         quantity_invoiced,
2194         po_distribution_id ,
2195         rcv_transaction_id,
2196         parent_reversal_id,
2197         price_var_code_combination_id,
2198         invoice_price_variance,
2199         base_invoice_price_variance,
2200         matched_uom_lookup_code,
2201         invoice_line_number,
2202         org_id,
2203         charge_applicable_to_dist_id ,
2204         project_id,
2205         task_id,
2206         expenditure_type,
2207         expenditure_item_date,
2208         expenditure_organization_id,
2209         project_accounting_context,
2210         pa_addition_flag,
2211         distribution_class,
2212         dist_match_type,
2213         LAST_UPDATED_BY,
2214         LAST_UPDATE_DATE,
2215        created_by, /* Bug 16220700 */
2216 		creation_Date /* Bug 16220700 */
2217         )
2218         VALUES
2219         (
2220         ins_reverse_dist_lines.accounting_date,
2221         'N', --ins_reverse_dist_lines.accrual_posted_flag, /* Bug 16220700 */
2222         ins_reverse_dist_lines.assets_addition_flag,
2223         ins_reverse_dist_lines.assets_tracking_flag,
2224         'N', --ins_reverse_dist_lines.cash_posted_flag, /* Bug 16220700 */
2225         ln_invoice_distribution_no,
2226         ins_reverse_dist_lines.dist_code_combination_id,
2227         ins_reverse_dist_lines.invoice_id,
2228         ins_reverse_dist_lines.line_type_lookup_code,
2229         ins_reverse_dist_lines.period_name,
2230         ins_reverse_dist_lines.set_of_books_id,
2231         -ins_reverse_dist_lines.amount,
2232         ins_reverse_dist_lines.base_amount,
2233         ins_reverse_dist_lines.batch_id,
2234         ins_reverse_dist_lines.description,
2235         ins_reverse_dist_lines.exchange_rate_variance,
2236         'N',
2237         'N', --ins_reverse_dist_lines.posted_flag, /* Bug 16220700 */
2238         ins_reverse_dist_lines.rate_var_code_combination_id,
2239         'Y',
2240         -1,
2241         -1,
2242         sysdate,
2243         ins_reverse_dist_lines.accts_pay_code_combination_id,
2244         ln_invoice_dist_id_rev,
2245         NULL,
2246         ins_reverse_dist_lines.po_distribution_id,
2247         ins_reverse_dist_lines.rcv_transaction_id,
2248         ins_reverse_dist_lines.invoice_distribution_id, --parent_reversal_id
2249         ins_reverse_dist_lines.price_var_code_combination_id,
2250         ins_reverse_dist_lines.invoice_price_variance,
2251         ins_reverse_dist_lines.base_invoice_price_variance,
2252         ins_reverse_dist_lines.matched_uom_lookup_code,
2253         ins_reverse_dist_lines.invoice_line_number,
2254         ins_reverse_dist_lines.org_id,
2255         ins_reverse_dist_lines.charge_applicable_to_dist_id ,
2256         ins_reverse_dist_lines.project_id,
2257         ins_reverse_dist_lines.task_id,
2258         ins_reverse_dist_lines.expenditure_type,
2259         ins_reverse_dist_lines.expenditure_item_date,
2260         ins_reverse_dist_lines.expenditure_organization_id,
2261         ins_reverse_dist_lines.project_accounting_context,
2262         ins_reverse_dist_lines.pa_addition_flag,
2263         ins_reverse_dist_lines.distribution_class,
2264         ins_reverse_dist_lines.dist_match_type,
2265         ln_user_id,
2266         sysdate,
2267         ln_user_id, /* Bug 16220700 */
2268 		sysdate /* Bug 16220700 */);
2269       End Loop;
2270 
2271 
2272      Update ap_invoice_lines_all
2273      set  discarded_flag = 'Y',
2274           amount = 0,
2275           last_update_date = sysdate,
2276           last_updated_by = -1
2277     Where invoice_id =  ident_rec.invoice_id
2278     and line_number = ident_rec.invoice_line_number
2279     and line_type_lookup_code = 'MISCELLANEOUS';
2280 
2281         jai_cmn_utils_pkg.write_fnd_log_msg('Match Tax Code','processed line numbebr'||ident_rec.invoice_line_number);
2282 
2283     Commit;
2284 End Loop;
2285 jai_cmn_utils_pkg.write_fnd_log_msg('Match Tax Code','Going to end of program'); --Added by nprashar for bug# 13446095
2286 goto end_of_program ;  --code should be exited from this point, as further processing is not required --Added by nprashar for bug # 13446095
2287 Elsif lv_discard_flag = 'Y' and ln_source = 'ERS' Then
2288 For ins_reverse_dist_lines_ers in c_inv_dist_tax_lines_ers ( inv_id , po_dist_id)
2289       Loop
2290          ln_invoice_distribution_no := ins_reverse_dist_lines_ers.distribution_line_number + 1;
2291           select ap_invoice_distributions_s.nextval into ln_invoice_dist_id_rev from dual;
2292  INSERT INTO ap_invoice_distributions_all
2293         (
2294         accounting_date,
2295         accrual_posted_flag,
2296         assets_addition_flag,
2297         assets_tracking_flag,
2298         cash_posted_flag,
2299         distribution_line_number,
2300         dist_code_combination_id,
2301         invoice_id,
2302         line_type_lookup_code,
2303         period_name,
2304         set_of_books_id ,
2305         amount,
2306         base_amount,
2307         batch_id,
2308         description,
2309         exchange_rate_variance,
2310         match_status_flag,
2311         posted_flag,
2312         rate_var_code_combination_id ,
2313         reversal_flag ,
2314         program_application_id,
2315         program_id,
2316         program_update_date,
2317         accts_pay_code_combination_id,
2318         invoice_distribution_id,
2319         quantity_invoiced,
2320         po_distribution_id ,
2321         rcv_transaction_id,
2322         parent_reversal_id,
2323         price_var_code_combination_id,
2324         invoice_price_variance,
2325         base_invoice_price_variance,
2326         matched_uom_lookup_code,
2327         invoice_line_number,
2328         org_id,
2329         charge_applicable_to_dist_id ,
2330         project_id,
2331         task_id,
2332         expenditure_type,
2333         expenditure_item_date,
2334         expenditure_organization_id,
2335         project_accounting_context,
2336         pa_addition_flag,
2337         distribution_class,
2338         dist_match_type,
2339         LAST_UPDATED_BY,
2340         LAST_UPDATE_DATE,
2341 		created_by, /* Bug 16220700 */
2342 		creation_date /* Bug 16220700 */
2343         )
2344         VALUES
2345         (
2346         ins_reverse_dist_lines_ers.accounting_date,
2347         'N', --ins_reverse_dist_lines_ers.accrual_posted_flag, /* Bug 16220700 */
2348         ins_reverse_dist_lines_ers.assets_addition_flag,
2349         ins_reverse_dist_lines_ers.assets_tracking_flag,
2350         'N', --ins_reverse_dist_lines_ers.cash_posted_flag, /* Bug 16220700 */
2351         ln_invoice_distribution_no,
2352         ins_reverse_dist_lines_ers.dist_code_combination_id,
2353         ins_reverse_dist_lines_ers.invoice_id,
2354         ins_reverse_dist_lines_ers.line_type_lookup_code,
2355         ins_reverse_dist_lines_ers.period_name,
2356         ins_reverse_dist_lines_ers.set_of_books_id,
2357         -ins_reverse_dist_lines_ers.amount,
2358         ins_reverse_dist_lines_ers.base_amount,
2359         ins_reverse_dist_lines_ers.batch_id,
2360         ins_reverse_dist_lines_ers.description,
2361         ins_reverse_dist_lines_ers.exchange_rate_variance,
2362         'N',
2363         'N', --ins_reverse_dist_lines_ers.posted_flag, /* Bug 16220700 */
2364         ins_reverse_dist_lines_ers.rate_var_code_combination_id,
2365         'Y',
2366         -1,
2367         -1,
2368         sysdate,
2369         ins_reverse_dist_lines_ers.accts_pay_code_combination_id,
2370         ln_invoice_dist_id_rev,
2371         NULL,
2372         ins_reverse_dist_lines_ers.po_distribution_id,
2373         ins_reverse_dist_lines_ers.rcv_transaction_id,
2374         ins_reverse_dist_lines_ers.invoice_distribution_id, --parent_reversal_id
2375         ins_reverse_dist_lines_ers.price_var_code_combination_id,
2376         ins_reverse_dist_lines_ers.invoice_price_variance,
2377         ins_reverse_dist_lines_ers.base_invoice_price_variance,
2378         ins_reverse_dist_lines_ers.matched_uom_lookup_code,
2379         ins_reverse_dist_lines_ers.invoice_line_number,
2380         ins_reverse_dist_lines_ers.org_id,
2381         ins_reverse_dist_lines_ers.charge_applicable_to_dist_id ,
2382         ins_reverse_dist_lines_ers.project_id,
2383         ins_reverse_dist_lines_ers.task_id,
2384         ins_reverse_dist_lines_ers.expenditure_type,
2385         ins_reverse_dist_lines_ers.expenditure_item_date,
2386         ins_reverse_dist_lines_ers.expenditure_organization_id,
2387         ins_reverse_dist_lines_ers.project_accounting_context,
2388         ins_reverse_dist_lines_ers.pa_addition_flag,
2389         ins_reverse_dist_lines_ers.distribution_class,
2390         ins_reverse_dist_lines_ers.dist_match_type,
2391         ln_user_id,
2392         sysdate,
2393 		ln_user_id, /* Bug 16220700 */
2394 		sysdate /* Bug 16220700 */);
2395 
2396         Update ap_invoice_lines_all
2397         set  discarded_flag = 'Y',
2398         amount = 0,
2399         last_update_date = sysdate,
2400         last_updated_by = -1
2401         Where invoice_id =  ins_reverse_dist_lines_ers.invoice_id
2402         and line_number = ins_reverse_dist_lines_ers.invoice_line_number
2403         and line_type_lookup_code = 'MISCELLANEOUS';
2404 
2405         Commit;
2406 End Loop;
2407 jai_cmn_utils_pkg.write_fnd_log_msg('Match Tax Code','Going to end of program');
2408 goto end_of_program ;  --code should be exited from this point, as further processing is not required
2409 End If;
2410 
2411 
2412   -- Start added for bug#3354932
2413   open c_functional_currency(get_ven_info_rec.set_of_books_id);
2414   fetch c_functional_currency into v_functional_currency;
2415   close c_functional_currency;
2416   -- End added for bug#3354932
2417 
2418   OPEN  checking_for_packing_slip(get_ven_info_rec.vendor_id, get_ven_info_rec.vendor_site_id, get_ven_info_rec.org_id);
2419   FETCH checking_for_packing_slip INTO chk_for_pcking_slip_rec;
2420   CLOSE checking_for_packing_slip;
2421 
2422 
2423   OPEN  c_inv(inv_id);
2424   FETCH c_inv INTO v_batch_id, v_source;
2425   CLOSE c_inv;
2426 
2427   -- Get mininum invoice distribution line number, Brathod, Bug# 4445989
2428   lv_match_type := 'NOT_MATCHED'; --Changed from NOT MATCHED to NOT_MATCHED for bug#6835548 by JMEENA
2429   lv_misc := 'MISCELLANEOUS';
2430   -- Bug 7249100. Added by Lakshmi Gopalsami
2431   lv_dist_class := 'PERMANENT';
2432 
2433   OPEN  cur_get_min_dist_linenum (inv_id, pn_invoice_line_number);
2434   FETCH cur_get_min_dist_linenum INTO ln_min_dist_line_num;
2435   CLOSE cur_get_min_dist_linenum;
2436 
2437   -- Added by Brathod for Bug# 4445989
2438   OPEN  cur_get_max_line_number;
2439   FETCH cur_get_max_line_number INTO ln_max_lnno;
2440   CLOSE cur_get_max_line_number;
2441 
2442   OPEN  cur_get_max_ap_inv_line (cpn_max_line_num => ln_max_lnno );
2443   FETCH cur_get_max_ap_inv_line INTO rec_max_ap_lines_all;
2444   CLOSE cur_get_max_ap_inv_line;
2445   -- End of Bug 4445989
2446 
2447 
2448   IF v_source <> 'SUPPLEMENT' THEN
2449     -- Using pn_invoice_line_number instead of dist_line_no for Bug#4445989
2450     OPEN  cur_items(inv_id,pn_invoice_line_number,ln_min_dist_line_num);
2451     FETCH cur_items INTO cur_items_rec;
2452     CLOSE cur_items;
2453   END IF;
2454 
2455   OPEN  for_org_id(inv_id);
2456   FETCH for_org_id INTO for_org_id_rec;
2457   CLOSE for_org_id;
2458 
2459 /* added by ssumaith  */
2460 
2461   open  c_fnd_curr_precision(for_org_id_rec.invoice_currency_code);
2462   fetch c_fnd_curr_precision into ln_precision;
2463   close c_fnd_curr_precision;
2464 
2465   if ln_precision is null then
2466      ln_precision := -1;--Modified by kunkumar for  5593895
2467   end if;
2468 
2469  /* ends here addition by ssumaith */
2470 
2471   /*OPEN  for_acct_id(NVL(for_org_id_rec.org_id, -1));
2472   FETCH for_acct_id INTO for_acct_id_rec;
2473   CLOSE for_acct_id;*/
2474   --commented the above by Sanjikum For Bug#4474501, as this cursor is not being used anywhere
2475 
2476   IF v_source <> 'SUPPLEMENT' THEN
2477 
2478     BEGIN  --06-Mar-2002
2479       SELECT chart_of_accounts_id INTO caid
2480       FROM   gl_sets_of_books
2481       WHERE  set_of_books_id = cur_items_rec.set_of_books_id;
2482     EXCEPTION
2483       WHEN OTHERS THEN
2484       ERRBUF := SQLERRM;
2485       RETCODE := 2;
2486     END ;-- end 06-Mar-2002
2487 
2488   END IF;
2489 
2490   begin
2491     -- following update statement is to just get a lock on the distribution lines of the invoice.
2492     -- the distribution line numbers should be unique and in sequence.
2493     update ap_invoice_distributions_all
2494     set    last_update_date = last_update_date
2495     where  invoice_id = inv_id;
2496 
2497     -- Added by Brathod for Bug# 4445989
2498     update ap_invoice_lines_all
2499     set    last_update_date = last_update_date
2500     where  invoice_id = inv_id;
2501     -- Cursor to select maximum line number from invoice lines for particular invoice
2502     SELECT max(line_number)
2503     INTO   ln_inv_line_num
2504     FROM   ap_invoice_lines_all
2505     WHERE  invoice_id = inv_id;
2506     -- End of Bug# 4445989
2507 
2508     /*
2509     Commented by Brathod for Bug# 4445989
2510     select max(DISTRIBUTION_LINE_NUMBER)
2511     into   v_distribution_no
2512     from   ap_invoice_distributions_all
2513     where  invoice_id = inv_id;
2514     */
2515 
2516   exception
2517     when others then
2518     errbuf := 'Unable to lock the distributions to get the next distributions number';
2519     retcode := 2;
2520     Return;
2521   end ;
2522   -- End: bug # 2805527
2523 
2524   SELECT accts_pay_code_combination_id
2525   INTO   apccid
2526   FROM   ap_invoices_all
2527   WHERE  invoice_id = inv_id;
2528 
2529   -- Using pn_invoice_line_number instead of dist_line_no for Bug#4445989
2530   OPEN  for_dist_insertion(inv_id,pn_invoice_line_number, ln_min_dist_line_num);
2531   FETCH for_dist_insertion INTO for_dist_insertion_rec;
2532   CLOSE for_dist_insertion;
2533 
2534   -- deleted supplementary invoice block by Aparajita on 03/11/2002 for bug # 2567799.
2535   -- supplemntary invoice not being used.
2536 
2537   v_apportn_factor_for_item_line :=
2538   jai_ap_utils_pkg.get_apportion_factor(inv_id, pn_invoice_line_number);
2539   -- bug#3752887
2540 
2541   /* service Start */
2542 
2543   open c_rcv_transactions(rcv_tran_id);
2544   fetch c_rcv_transactions into r_rcv_transactions;
2545   close c_rcv_transactions;
2546 
2547 /*following fetches added/modified for bug 6595773*/
2548   OPEN  from_po_distributions(po_dist_id);
2549   FETCH from_po_distributions INTO from_po_distributions_rec;
2550   CLOSE from_po_distributions;
2551 
2552 
2553   open c_po_lines_all(from_po_distributions_rec.po_line_id);
2554   fetch c_po_lines_all into r_po_lines_all;
2555   close c_po_lines_all;
2556 
2557   lv_is_item_an_expense := null;
2558   lv_is_item_an_expense := jai_general_pkg.is_item_an_expense
2559                            (
2560                               p_organization_id   =>     r_rcv_transactions.organization_id,
2561                               p_item_id           =>     r_po_lines_all.item_id
2562                            );
2563 
2564   /*lv_accounting_method_option := jai_general_pkg.get_accounting_method
2565                           (
2566                              p_org_id            =>    v_org_id,
2567                              p_sob_id            =>    for_dist_insertion_rec.set_of_books_id,
2568                              p_organization_id   =>    r_rcv_transactions.organization_id
2569                           );*/
2570 
2571   /*bug 9346307*/
2572     OPEN c_po_details;
2573     FETCH c_po_details INTO ln_po_line_id, ln_po_line_location_id;
2574     CLOSE c_po_details;
2575 
2576   IF po_dist_id IS NULL
2577   THEN
2578    SELECT Min(po_distribution_id)
2579    INTO ln_po_dist_id
2580    FROM po_distributions_all
2581    WHERE po_line_id = ln_po_line_id
2582    AND line_location_id = ln_po_line_location_id;
2583   END IF;
2584   /*end bug 9346307*/
2585 
2586   -- commented the above by Sanjikum for Bug#4474501
2587   lv_accrue_on_receipt_flag := jai_rcv_trx_processing_pkg.get_accrue_on_receipt
2588                          (
2589                             p_po_distribution_id => Nvl(po_dist_id,ln_po_dist_id)
2590                          );
2591 
2592   Fnd_File.put_line(Fnd_File.LOG, 'lv_accrue_on_receipt_flag='||lv_accrue_on_receipt_flag);
2593 
2594   open c_jai_regimes(jai_constants.service_regime);
2595   fetch c_jai_regimes into r_jai_regimes;
2596   close c_jai_regimes;
2597 
2598   /* service End */
2599 
2600   /* Bug 5358788. Added by Lakshmi Gopalsami
2601    * Fetch VAT Regime id
2602    */
2603 
2604   OPEN  c_jai_regimes(jai_constants.vat_regime);
2605   FETCH  c_jai_regimes INTO  r_vat_regimes;
2606   CLOSE  c_jai_regimes;
2607 
2608   /* Bug 5401111. Added by Lakshmi Gopalsami
2609    | Get the account type for the distribution.
2610    | This is required to find out whether charge_applicable_to_dist
2611    */
2612    lv_account_type := get_gl_account_type
2613                     (for_dist_insertion_rec.dist_code_combination_id);
2614 
2615   IF  p_rematch = 'PO_MATCHING' THEN
2616 
2617     Fnd_File.put_line(Fnd_File.LOG, 'inside  p_rematch = PO_MATCHING ');
2618     ---commented as receipt not required for pure po match
2619 /*following fetch commented out for bug 6595773*/
2620     /*OPEN  from_po_distributions(po_dist_id);
2621     FETCH from_po_distributions INTO from_po_distributions_rec;
2622     CLOSE from_po_distributions;*/
2623 
2624       -- Added by Jason Liu for retroactive price on 2008/03/26
2625       -----------------------------------------------------------
2626       OPEN get_source_csr;
2627       FETCH get_source_csr INTO lv_source;
2628       CLOSE get_source_csr;
2629       -----------------------------------------------------------
2630       FOR i IN
2631       from_line_location_taxes(ln_po_line_location_id, for_org_id_rec.vendor_id, lv_source)/*bug 9346307*/
2632       LOOP  --L2
2633 
2634         /*5763527 */
2635         ln_project_id           := null;
2636         ln_task_id              := null;
2637         lv_exp_type             := null;
2638         ld_exp_item_date        := null;
2639         ln_exp_organization_id  := null;
2640         lv_project_accounting_context := null;
2641         lv_pa_addition_flag           := null;
2642         /* END 5763527 */
2643 
2644        -- Added by Brathod for Bug# 4445989
2645        /* v_distribution_no := v_distribution_no + 1; */
2646        v_distribution_no :=1;
2647        -- ln_inv_line_num := ln_inv_line_num + 1; -- 5763527, moved increment just before insert statement
2648        -- End Bug# 4445989
2649 
2650       -- start bug # 2775043
2651       v_tax_variance_inv_cur := null;
2652       v_tax_variance_fun_cur := null;
2653       v_price_var_accnt := null;
2654       v_tax_amount := -1;--Modified by kunkumar for  5593895
2655       r_service_regime_tax_type := null;
2656       -- end bug # 2775043
2657 
2658       -- Bug 5358788. Added by Lakshmi Gopalsami
2659       r_VAT_regime_tax_type := null;
2660 
2661       -- Start for bug#3752887
2662       --start additions for bug#12697697
2663          Fnd_File.put_line(Fnd_File.LOG, ' po match i.adhoc_flag = '||i.adhoc_flag);
2664          Fnd_File.put_line(Fnd_File.LOG, ' po match qty rate flag i.vat_flag = '||i.vat_flag);
2665          Fnd_File.put_line(Fnd_File.LOG, 'po match  i.tax_type = '||i.tax_type);
2666         if i.adhoc_flag='Y' then
2667 	 -- start bug # 13024213
2668          --if i.tax_type in ('Freight','Insurance','Octrai','Other','PURCHASE TAX','ENTRY TAX') and i.vat_flag = 'Q'  --Added IN clause for bug#13363329
2669 		 if i.tax_type in ('Freight','Insurance','Octrai','Other','PURCHASE TAX','ENTRY TAX','Excise','EXCISE_EDUCATION_CESS','EXCISE_SH_EDU_CESS')  and i.vat_flag = 'Q' -- for bug 16195110 by anupgupt
2670           then
2671               --Changes by nprashar for bug # 13363329
2672 		   v_apportn_factor_for_adhoc:= jai_ap_utils_pkg.get_apportion_factor(inv_id, pn_invoice_line_number,'QUANTITY');
2673                    v_tax_amount := i.tax_amount * v_apportn_factor_for_adhoc;
2674               -- end bug # 13024213
2675           --elsif i.tax_type not in ('Freight','Insurance','Octrai','Other','PURCHASE TAX','ENTRY TAX')
2676 		  elsif i.tax_type not in ('Freight','Insurance','Octrai','Other','PURCHASE TAX','ENTRY TAX','Excise','EXCISE_EDUCATION_CESS','EXCISE_SH_EDU_CESS') -- for bug 16195110 by anupgupt
2677          then
2678            v_tax_amount := i.tax_amount * v_apportn_factor_for_item_line;
2679          else
2680            v_tax_amount := i.tax_amount ;
2681          end if;
2682        else
2683            v_tax_amount := i.tax_amount * v_apportn_factor_for_item_line;
2684         end if;
2685 
2686         Fnd_File.put_line(Fnd_File.LOG, 'v_tax_amount after apportioning = '|| v_tax_amount);
2687         --end additions for bug#12697697
2688 
2689       if i.currency <> for_org_id_rec.invoice_currency_code then
2690           v_tax_amount := v_tax_amount / for_org_id_rec.exchange_rate;
2691       end if;
2692 
2693       fnd_file.put_line(FND_FILE.LOG, ' Value of tax amount  -> '|| v_tax_amount);
2694 
2695       OPEN  c_tax(i.tax_id);
2696       FETCH c_tax INTO c_tax_rec;
2697       CLOSE c_tax;
2698 
2699 	   --Added by nprashar for bug # 13653889
2700 	  OPEN c_tax_curr_prec( i.tax_id ) ;
2701       FETCH c_tax_curr_prec INTO ln_tax_precision ;
2702       CLOSE c_tax_curr_prec ;
2703 
2704 
2705       /* Service Start */
2706       v_assets_tracking_flag := for_dist_insertion_rec.assets_tracking_flag;
2707       v_dist_code_combination_id := null;
2708 
2709       --initial the tax_type
2710       lv_tax_type := NULL;--added by eric for inclusive tax on 20-Dec,2007
2711 
2712       -- 5763527, Brathod
2713       -- Modified the if condition to consider partial recoverable lines previous logic was hadling only when it is 100
2714       if i.modvat_flag = jai_constants.YES
2715       and nvl(c_tax_rec.mod_cr_percentage, -1) > 0 then
2716 
2717         --recoverable tax
2718         lv_tax_type := 'RE'; --added by eric for inclusive tax on 20-dec,2007
2719 
2720        /*  recoverable tax */
2721         v_assets_tracking_flag := 'N';
2722 
2723         open c_regime_tax_type(r_jai_regimes.regime_id, c_tax_rec.tax_type);
2724         fetch c_regime_tax_type into r_service_regime_tax_type;
2725         close c_regime_tax_type;
2726 
2727         fnd_file.put_line(FND_FILE.LOG,
2728                    ' Service regime: '||r_service_regime_tax_type.tax_type);
2729 
2730   /* Bug 5358788. Added by Lakshmi Gopalsami
2731    * Fetched the details of VAT regime
2732    */
2733 
2734         open c_regime_tax_type(r_vat_regimes.regime_id, c_tax_rec.tax_type);
2735         fetch c_regime_tax_type into r_vat_regime_tax_type;
2736         close c_regime_tax_type;
2737 
2738         fnd_file.put_line(FND_FILE.LOG,
2739                    ' VAT regime: '||r_vat_regime_tax_type.tax_type);
2740 
2741 /*following code added for bug 6595773*/
2742 /*6595773 start*/
2743         /* Fetch ship_to_location_id and ship_to_organization_id from po_line_locations */
2744         ln_ship_to_organization_id := null;
2745         ln_ship_to_location_id := null;
2746         open c_get_ship_to_org_loc (cur_items_rec.line_location_id);
2747         fetch c_get_ship_to_org_loc  into ln_ship_to_organization_id
2748                                          ,ln_ship_to_location_id;
2749         close c_get_ship_to_org_loc;
2750 
2751 
2752        Fnd_File.put_line(Fnd_File.LOG, 'ln_ship_to_organization_id='||ln_ship_to_organization_id
2753                                      ||', r_po_lines_all.item_id='||r_po_lines_all.item_id
2754                                      );
2755        lv_is_item_an_expense := jai_general_pkg.is_item_an_expense
2756                            (
2757                               p_organization_id   =>     ln_ship_to_organization_id,
2758                               p_item_id           =>     r_po_lines_all.item_id
2759                            );
2760 /*6595773 end*/
2761         if r_service_regime_tax_type.tax_type is not null then
2762           /* Service type of tax */
2763           /*Commented by qinglei on 16-Feb-2012 for bug#13561173*/
2764           /*v_dist_code_combination_id := check_service_interim_account
2765                                         (
2766                                           lv_accrue_on_receipt_flag,
2767                                           lv_accounting_method_option,
2768                                           lv_is_item_an_expense,
2769                                           r_jai_regimes.regime_id,
2770                                           v_org_id,
2771                                           p_rematch,
2772                                           c_tax_rec.tax_type,
2773                                           --added nvl for bug#9386496
2774                                           Nvl(po_dist_id,ln_po_dist_id) --Added by JMEENA for bug#6833506
2775                                         );*/
2776         /*Added by qinglei on 16-Feb-2012 for bug#13561173 begin*/
2777         v_dist_code_combination_id := jai_cmn_rgm_recording_pkg.get_account
2778 						                            (
2779 						                               p_regime_id            =>      r_jai_regimes.regime_id,
2780 					                                 p_organization_type    =>      jai_constants.orgn_type_io,
2781 							                             p_organization_id      =>      ln_ship_to_organization_id,
2782 							                             p_location_id          =>      ln_ship_to_location_id,
2783 					                                 p_tax_type             =>      c_tax_rec.tax_type,
2784 						                               p_account_name         =>      jai_constants.recovery_interim
2785 						                             );
2786         /*Added by qinglei on 16-Feb-2012 for bug#13561173 end*/
2787         fnd_file.put_line(FND_FILE.LOG,
2788                    ' Regime type , CCID '
2789        || r_service_regime_tax_type.tax_type
2790              ||', ' || v_dist_code_combination_id);
2791         /* Bug 5358788. Added by Lakshmi Gopalsami
2792    * Commented p_rematch and added validation for
2793    * VAT regime.
2794    */
2795 /*following elsif and else modified / added for bug 6595773*/
2796         ELSIF r_vat_regime_tax_type.tax_type IS NOT NULL THEN
2797         -- p_rematch = 'PO_MATCHING' then
2798       Fnd_File.put_line(Fnd_File.LOG, 'lv_is_item_an_expense='||lv_is_item_an_expense);
2799     /*  v_dist_code_combination_id := jai_cmn_rgm_recording_pkg.get_account
2800               (
2801                p_regime_id            =>      ln_vat_regime_id,
2802                p_organization_type    =>      jai_constants.orgn_type_io,
2803                p_organization_id      =>      ln_ship_to_organization_id ,
2804                p_location_id          =>      ln_ship_to_location_id ,
2805                p_tax_type             =>      c_tax_rec.tax_type,
2806                p_account_name         =>      jai_constants.recovery --RECOVERY
2807               );*/  --commented by Bgowrava for Bug#9499146
2808      v_dist_code_combination_id := for_dist_insertion_rec.dist_code_combination_id;  --Added by Bgowrava for Bug#9499146
2809     ELSE
2810           /*v_dist_code_combination_id := c_tax_rec.tax_account_id;*/ --commented by Bgowrava for Bug#9499146
2811           v_dist_code_combination_id := for_dist_insertion_rec.dist_code_combination_id; --Added by Bgowrava for Bug#9499146
2812           fnd_file.put_line(FND_FILE.LOG,
2813                  ' Regime type , CCID '
2814      || r_vat_regime_tax_type.tax_type
2815            ||', ' || v_dist_code_combination_id);
2816 
2817         end if;
2818       else -- NR tax ,project infor is same as the parent line
2819         /* 5763527 introduced for PROJETCS COSTING Impl */
2820         /* 5763527 */
2821         ln_project_id           := p_project_id;
2822         ln_task_id              := p_task_id;
2823         lv_exp_type             := p_expenditure_type;
2824         ld_exp_item_date        := p_expenditure_item_date;
2825         ln_exp_organization_id  := p_expenditure_organization_id;
2826 
2827         --non recoverable
2828         lv_tax_type := 'NR';  --added by eric for inclusive tax on 20-dec,2007
2829       end if; /*nvl(c_tax_rec.mod_cr_percentage, 0) = 100  and c_tax_rec.tax_account_id is not null*/
2830 
2831 
2832       /* Service End */
2833 
2834       -- Start bug#4103473
2835       if nvl(v_tax_amount,-1) = -1 then --Modified by kunkumar for bug 5593895
2836         v_tax_amount := apportion_tax_4_price_cor_inv(v_tax_amount,  i.tax_id);
2837       END IF;
2838       v_tax_amount := nvl(v_tax_amount,-1); --Modified by kunkumar for bug 5593895
2839       -- End bug#4103473
2840 
2841       fnd_file.put_line(FND_FILE.LOG, ' Value of tax amount  after apportion -> '|| v_tax_amount);
2842 --start additions for bug#13422310
2843 	  fnd_file.put_line(FND_FILE.LOG, 'Before calling JAI_CALC_APMATCH_TAX  parameters passed are ');
2844 	  fnd_file.put_line(FND_FILE.LOG, ' P_MATCH_TYPE -> '||'PO_MATCHING' ||' P_INVOICE_ID -> '||inv_id||
2845 	  ' P_INVOICE_line_no-> '||pn_invoice_line_number||' P_TAX_ID-> '||i.tax_id||' P_line_locaction_id-> '||ln_po_line_location_id||' P_tax_amount        -> '|| v_tax_amount);
2846 	  jai_aptax_pkg_client_extn.JAI_CALC_APMATCH_TAX (P_MATCH_TYPE        => 'PO_MATCHING',
2847 	                        P_INVOICE_ID        =>inv_id,
2848 							P_INVOICE_line_no   =>pn_invoice_line_number,
2849 							P_TAX_ID            => i.tax_id ,
2850 							P_line_location_id => ln_po_line_location_id,
2851 							P_tax_amount        => v_tax_amount
2852 							);
2853 		fnd_file.put_line(FND_FILE.LOG, 'the tax amount after the JAI_CALC_APMATCH_TAX call is  '||v_tax_amount);
2854       --end additions for bug#13422310
2855       --
2856       -- Begin 5763527
2857       --
2858       ln_rec_tax_amt  := null;
2859       ln_nrec_tax_amt := null;
2860 
2861       --added by Eric for inclusive tax on 20-dec-2007,begin
2862       -------------------------------------------------------------------------------------
2863 
2864       --1.If tax is FR (fully recoverable) or NR tax,one line need to be inserted into tax table
2865       --  For inclusive taxes without project info,there are no lines in the ap/dist line tables for a tax line.
2866       --  For inclusive taxes with project info,there are two lines in the ap/dist line tables for a tax line.
2867       --  For exclusive taxes, one tax  line maps to one corresponding ap/dist line in AP table.
2868       --
2869 
2870       --2.If tax is exclusive Partially Recoverable tax, two lines will be inserted into all 3 tables
2871       --  one is for NR and another is for R tax
2872 
2873 
2874       --3.If tax is inclusive Partially Recoverable tax,
2875 
2876       --3.1 with project information:  2 lines will be inserted into tax tables;2 lines will be inserted into AP lines
2877       --    the tax table store the NR and R tax,while Ap line/dist lines stores 2 new inserted line. One line is with
2878       --    recoverable amount/null project information and another is with negative recoverable amount/item project information
2879 
2880 
2881       --3.2 without project information: only two lines,one NR and one R tax line need to be inserted into tax table only
2882       --    no Ap line/dist lines need to be inserted into Ap line/dist lines
2883 
2884       -- So exclusive tax will be inserted into ap line /dist line and inclusive tax with prj infor
2885 
2886       -------------------------------------------------------------------------------------
2887       --added by Eric for inclusive tax on 20-dec-2007,end
2888 
2889 
2890 
2891       ln_lines_to_insert := 1;
2892 
2893 
2894 
2895       --added by Eric for inclusive tax on 20-dec-2007,begin
2896       -------------------------------------------------------------------------------------
2897       --exclusive tax or inlcusive tax without project info,  ln_lines_to_insert := 1;
2898       --inclusive recoverable tax with project info,  ln_lines_to_insert := 2;
2899       --PR(partially recoverable) tax is processed in another logic
2900 
2901       IF ( NVL(i.inc_tax_flag,'N') = 'N'
2902            OR ( NVL(i.inc_tax_flag,'N') = 'Y'
2903                 AND p_project_id IS NULL
2904               )
2905          )
2906       THEN
2907         ln_lines_to_insert := 1;
2908       ELSIF ( NVL(i.inc_tax_flag,'N') = 'Y'
2909               AND p_project_id IS NOT NULL
2910               AND lv_tax_type = 'RE'
2911             )
2912       THEN
2913         ln_lines_to_insert := 2;
2914       END IF;--( NVL(i.inc_tax_flag,'N') = 'N' OR ( NVL(i.inc_tax_flag,'N'))
2915       -------------------------------------------------------------------------------------
2916       --added by Eric for inclusive tax on 20-dec-2007,end
2917 
2918       Fnd_File.put_line(Fnd_File.LOG, 'i.modvat_flag ='||i.modvat_flag ||',c_tax_rec.mod_cr_percentage='||c_tax_rec.mod_cr_percentage);
2919 
2920       if i.modvat_flag = jai_constants.YES
2921       and nvl(c_tax_rec.mod_cr_percentage, -1) > 0
2922       and nvl(c_tax_rec.mod_cr_percentage, -1) < 100
2923       then
2924         --
2925         -- Tax line is for partial Recoverable tax.  Hence split amount into two parts, Recoverable and Non-Recoverable
2926         -- and instead of one line, two lines needs to be inserted.
2927         -- For ordinary lines (with modvat_flag = 'N' or with mod_cr_percentage = 100) there will be only one line inserted
2928         --
2929         ln_lines_to_insert := 2 *ln_lines_to_insert; --changed by eric for inclusive tax on Jan 4,2008
2930         ln_rec_tax_amt  :=  round ( nvl(v_tax_amount,0) * (c_tax_rec.mod_cr_percentage/100) , c_tax_rec.rounding_factor) ;
2931         ln_nrec_tax_amt :=  round ( nvl(v_tax_amount,0) - nvl(ln_rec_tax_amt,0) , c_tax_rec.rounding_factor) ;
2932     /* Added Tax rounding based on tax rounding factor for bug 6761425 by vumaasha */
2933 
2934         lv_tax_type := 'PR';  --changed by eric for inclusive tax on Jan 4,2008
2935 
2936       end if;
2937        --start additions for bug#11936596
2938   if i.modvat_flag = jai_constants.YES
2939   then
2940    ln_inclurec_tax_amt := round ( nvl(v_tax_amount,0) * (c_tax_rec.mod_cr_percentage/100) , c_tax_rec.rounding_factor) ;
2941   end if;
2942 --end additions for bug#11936596
2943 
2944 
2945       fnd_file.put_line(fnd_file.log, 'ln_lines_to_insert='||ln_lines_to_insert||
2946                                       ',ln_rec_tax_amt='||ln_rec_tax_amt          ||
2947                                       ',ln_nrec_tax_amt='||ln_nrec_tax_amt||' ln_inclurec_tax_amt '||ln_inclurec_tax_amt
2948                        );
2949       --
2950       --  If a line has a partially recoverable tax the following loop will be executed twice.  First line will always be for a
2951       --  non recoverable tax amount and the second line will be for a recoverable tax amount
2952       --  For ordinary lines (with modvat_flag = 'N' or with mod_cr_percentage = 100 fully recoverable) the variable
2953       --  ln_lines_to_insert will have value of 1 and hence only one line will be inserted with full tax amount
2954       --
2955 
2956       for line in 1..ln_lines_to_insert
2957       loop
2958 
2959         --Commented out the existing partially tax processing logic
2960         --replace it with new logic to cover inclusive tax case
2961         /*
2962         if line = 1 then
2963 
2964           v_tax_amount  := nvl(ln_rec_tax_amt, v_tax_amount);
2965           lv_modvat_flag := i.modvat_flag ;
2966 
2967         elsif line = 2 then
2968 
2969           v_tax_amount := ln_nrec_tax_amt;
2970 
2971           if for_dist_insertion_rec.assets_tracking_flag = jai_constants.YES then
2972             v_assets_tracking_flag := jai_constants.YES;
2973           end if;
2974 
2975           lv_modvat_flag := jai_constants.NO ;
2976 
2977           --
2978           -- This is a non recoverable line hence the tax amounts should be added into the project costs by populating
2979           -- projects related columns so that PROJECTS can consider this line for Project Costing
2980           --
2981 
2982           ln_project_id           := p_project_id;
2983           ln_task_id              := p_task_id;
2984           lv_exp_type             := p_expenditure_type;
2985           ld_exp_item_date        := p_expenditure_item_date;
2986           ln_exp_organization_id  := p_expenditure_organization_id;
2987           lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
2988           lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
2989 
2990           -- For non recoverable line charge account should be same as of the parent line
2991           v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id;
2992 
2993         end if;
2994         */
2995 
2996         --added by Eric for inclusive tax on 20-dec-2007,begin
2997         -------------------------------------------------------------------------------------
2998         -- v_tax_amount is for storing the tax amount and  saving into the those 2 ap tables.
2999         -- lv_tax_line_amount is for storing the tax amount and saving into the tax table.
3000 
3001         -----------Exclusive Tax :
3002 
3003         -- *In FR/NR tax case, ln_lines_to_insert  =1
3004         -- v_tax_amount :=v_tax_amount; ( ln_rec_tax_amt IS Null)
3005         -- lv_tax_line_amount:=  v_tax_amount
3006         -- this line need to be inserted into 2 ap tables and 1 jai tax table
3007 
3008         -- *In PR tax case, ln_lines_to_insert   =2
3009         -- line 1:
3010         -- v_tax_amount :=ln_rec_tax_amt;
3011         -- lv_tax_line_amount:=  v_tax_amount
3012         -- this line need to be inserted into 2 ap tables and 1 jai tax table
3013 
3014         -- line2:
3015         -- v_tax_amount :=ln_nrec_tax_amt;
3016         -- lv_tax_line_amount:=  v_tax_amount
3017         -- this line need to be inserted into 2 ap tables and 1 jai tax table
3018 
3019 
3020         -----------Inclusive Tax :
3021 
3022         -- *In FR/NR tax without project case, ln_lines_to_insert  =1
3023         -- v_tax_amount :=v_tax_amount;
3024         -- lv_tax_line_amount:=  v_tax_amount
3025         -- this line need to be inserted into the jai tax table
3026 
3027 
3028         -- *In FR/NR tax with project case, ln_lines_to_insert is =2
3029         -- line 1:
3030         -- v_tax_amount       :=  v_tax_amount;
3031         -- lv_tax_line_amount :=  v_tax_amount;
3032         -- no project information
3033         -- this line need to be inserted into the jai tax table
3034 
3035         -- line2:
3036         -- v_tax_amount       := -v_tax_amount;
3037         -- lv_tax_line_amount :=  v_tax_amount;
3038         -- project information is same as parent line
3039         -- this line need to be inserted into 2 ap tables
3040 
3041 
3042         --* In PR tax without project case, ln_lines_to_insert =2
3043         -- line 1:
3044         -- v_tax_amount       :=  ln_rec_tax_amt;
3045         -- lv_tax_line_amount :=  ln_rec_tax_amt;
3046         -- no project information
3047         -- this line need to be inserted the jai tax table
3048 
3049         -- line2:
3050         -- v_tax_amount       :=  ln_nrec_tax_amt;
3051         -- lv_tax_line_amount :=  ln_nrec_tax_amt
3052         -- no project information
3053         -- this line need to be inserted the jai tax table
3054 
3055 
3056         --* In PR tax with project case, ln_lines_to_insert  =4
3057         -- line 1:
3058         -- v_tax_amount       :=  ln_rec_tax_amt;
3059         -- lv_tax_line_amount :=  ln_rec_tax_amt;
3060         -- no project information
3061         -- this line need to be inserted into the jai tax table only
3062 
3063         -- line 2:
3064         -- v_tax_amount       := ln_nrec_tax_amt;
3065         -- lv_tax_line_amount:=  ln_nrec_tax_amt;
3066         -- project information is same as parent line
3067         -- this line need to be inserted into the jai tax table only
3068 
3069         -- line 3:
3070         -- v_tax_amount       :=  ln_rec_tax_amt;
3071         -- lv_tax_line_amount :=  ln_rec_tax_amt;
3072         -- no project information
3073         -- this line need to be inserted into 2 ap tables only
3074 
3075         -- line 4:
3076         -- v_tax_amount       := -ln_rec_tax_amt;
3077         -- lv_tax_line_amount:=  ln_rec_tax_amt;
3078         -- project information is same as parent line
3079          -- this line need to be inserted into 2 ap tables only
3080 
3081 
3082 
3083 
3084         -- v_tax_amount is for storing the tax amount saving into the ap  tax table.
3085         --
3086 
3087         IF (NVL(i.inc_tax_flag,'N') = 'N')--exclusive case
3088         THEN
3089           IF line = 1 then
3090 
3091             v_tax_amount  := nvl(ln_rec_tax_amt, v_tax_amount);
3092             lv_tax_line_amount:=  v_tax_amount       ;   --added by eric for inclusive tax
3093             lv_modvat_flag := i.modvat_flag ;
3094 
3095             lv_ap_line_to_inst_flag  := 'Y'; --added by eric for inclusive tax
3096             lv_tax_line_to_inst_flag := 'Y'; --added by eric for inclusive tax
3097           ELSIF line = 2 then
3098 
3099             v_tax_amount             := ln_nrec_tax_amt;
3100             lv_tax_line_amount       :=  v_tax_amount  ; --added by eric for inclusive tax
3101             lv_ap_line_to_inst_flag  := 'Y';             --added by eric for inclusive tax
3102             lv_tax_line_to_inst_flag := 'Y';             --added by eric for inclusive tax
3103 
3104 
3105             IF for_dist_insertion_rec.assets_tracking_flag = jai_constants.YES THEN
3106               v_assets_tracking_flag := jai_constants.YES;
3107             END IF;
3108 
3109             lv_modvat_flag := jai_constants.NO ;
3110 
3111             --
3112             -- This is a non recoverable line hence the tax amounts should be added into the project costs by populating
3113             -- projects related columns so that PROJECTS can consider this line for Project Costing
3114             --
3115             IF nvl(lv_accrue_on_receipt_flag,'#') <> 'Y' THEN -- Bug 6338371
3116               ln_project_id           := p_project_id;
3117               ln_task_id              := p_task_id;
3118               lv_exp_type             := p_expenditure_type;
3119               ld_exp_item_date        := p_expenditure_item_date;
3120               ln_exp_organization_id  := p_expenditure_organization_id;
3121               lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
3122               lv_pa_addition_flag         := for_dist_insertion_rec.pa_addition_flag;
3123             END if;
3124 
3125             -- For non recoverable line charge account should be same as of the parent line
3126             v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id;
3127 
3128           END IF; --line = 1
3129         ELSIF (NVL(i.inc_tax_flag,'N') = 'Y')       --inclusive case
3130         THEN
3131           IF( lv_tax_type ='PR')
3132           THEN
3133             IF ( line = 1 )
3134             THEN
3135               --recoverable part
3136               v_tax_amount       := ln_rec_tax_amt ;
3137               lv_tax_line_amount := v_tax_amount   ;   --added by eric for inclusive tax
3138               lv_modvat_flag     := i.modvat_flag  ;
3139               lv_ap_line_to_inst_flag  := 'N';         --added by eric for inclusive tax
3140               lv_tax_line_to_inst_flag := 'Y';         --added by eric for inclusive tax
3141 
3142               --non recoverable part
3143             ELSIF ( line = 2 )
3144             THEN
3145               v_tax_amount       := ln_nrec_tax_amt  ;
3146               lv_tax_line_amount :=  v_tax_amount    ;   --added by eric for inclusive tax
3147               lv_modvat_flag     := jai_constants.NO ;
3148               lv_ap_line_to_inst_flag  := 'N';           --added by eric for inclusive tax
3149               lv_tax_line_to_inst_flag := 'Y';           --added by eric for inclusive tax
3150               v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id ;--13561173
3151               --recoverable part without project infor
3152             ELSIF ( line = 3 )
3153             THEN
3154               v_tax_amount                  := ln_rec_tax_amt;
3155               lv_tax_line_amount            := NULL;
3156 
3157               lv_ap_line_to_inst_flag  := 'Y';             --added by eric for inclusive tax
3158               lv_tax_line_to_inst_flag := 'N';             --added by eric for inclusive tax
3159 
3160               --Project information
3161               ln_project_id                 := NULL;
3162               ln_task_id                    := NULL;
3163               lv_exp_type                   := NULL;
3164               ld_exp_item_date              := NULL;
3165               ln_exp_organization_id        := NULL;
3166               lv_project_accounting_context := NULL;
3167               lv_pa_addition_flag           := NULL;
3168 
3169               -- For inclusive recoverable line charge account should be same as of the parent line
3170               v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id ;
3171 
3172               --recoverable part in negative amount with project infor
3173             ELSIF ( line = 4 )
3174             THEN
3175               v_tax_amount                  := NVL(ln_rec_tax_amt, v_tax_amount)* -1;
3176               lv_tax_line_amount            := NULL;
3177               lv_ap_line_to_inst_flag  := 'Y';             --added by eric for inclusive tax
3178               lv_tax_line_to_inst_flag := 'N';             --added by eric for inclusive tax
3179 
3180               --Project information
3181               ln_project_id                 := p_project_id;
3182               ln_task_id                    := p_task_id;
3183               lv_exp_type                   := p_expenditure_type;
3184               ld_exp_item_date              := p_expenditure_item_date;
3185               ln_exp_organization_id        := p_expenditure_organization_id;
3186               lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
3187               lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
3188 
3189               -- For inclusive recoverable line charge account should be same as of the parent line
3190               v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id ;
3191             END IF; --line = 1
3192           ELSIF ( lv_tax_type = 'RE'
3193                   AND p_project_id IS NOT NULL
3194                 )
3195           THEN
3196             --recoverable tax without project infor
3197             IF ( line = 1 )
3198             THEN
3199               v_tax_amount       :=  v_tax_amount  ;
3200               lv_tax_line_amount :=  v_tax_amount  ;    --added by eric for inclusive tax
3201               lv_modvat_flag     :=  i.modvat_flag ;
3202               lv_ap_line_to_inst_flag  := 'Y';          --added by eric for inclusive tax
3203               lv_tax_line_to_inst_flag := 'Y';          --added by eric for inclusive tax
3204 
3205               ln_project_id                 := NULL;
3206               ln_task_id                    := NULL;
3207               lv_exp_type                   := NULL;
3208               ld_exp_item_date              := NULL;
3209               ln_exp_organization_id        := NULL;
3210               lv_project_accounting_context := NULL;
3211               lv_pa_addition_flag           := NULL;
3212               v_dist_code_combination_id    := for_dist_insertion_rec.dist_code_combination_id ;
3213             --recoverable tax in negative amount with project infor
3214             ELSIF ( line = 2 )
3215             THEN
3216               v_tax_amount       :=  v_tax_amount * -1;
3217               lv_tax_line_amount :=  NULL ;   --added by eric for inclusive tax
3218               lv_modvat_flag     :=  i.modvat_flag  ;
3219 
3220               ln_project_id                 := p_project_id;
3221               ln_task_id                    := p_task_id;
3222               lv_exp_type                   := p_expenditure_type;
3223               ld_exp_item_date              := p_expenditure_item_date;
3224               ln_exp_organization_id        := p_expenditure_organization_id;
3225               lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
3226               lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
3227               v_dist_code_combination_id    := for_dist_insertion_rec.dist_code_combination_id ;
3228             END IF;
3229           --  ELSIF ( lv_tax_type <> 'PR' AND p_project_id IS NULL )
3230           --  THEN
3231           ELSE -- eric removed the above criteria for bug 6888665 and 6888209
3232             Fnd_File.put_line(Fnd_File.LOG, 'NOT Inclusive PR Tax,NOT Inclusive RE for project ');
3233             --The case process the inclusive NR tax and inclusive RE tax not for project
3234 
3235             lv_tax_line_amount := v_tax_amount   ;   --added by eric for inclusive tax
3236             lv_modvat_flag     := i.modvat_flag  ;
3237             lv_ap_line_to_inst_flag  := 'N';         --added by eric for inclusive tax
3238             lv_tax_line_to_inst_flag := 'Y';         --added by eric for inclusive tax
3239 
3240           END IF;--( tax_type ='PR' and (ln_lines_to_insert =4 OR )
3241         END IF; --(NVL(r_tax_lines_r ec.inc_tax_flag,'N') = 'N')
3242         -------------------------------------------------------------------------------------------
3243         --added by Eric for inclusive tax on 20-dec-2007,end
3244 
3245 
3246 
3247         --
3248         -- End 5763527
3249         --
3250 
3251       --ln_inv_line_num := ln_inv_line_num + 1; commented out by eric for inclusive tax
3252 
3253       -- Added by Brathod for Bug#4445989
3254       Fnd_File.put_line(Fnd_File.LOG,
3255       'Before inserting into ap_invoice_lines_all for line no :' || ln_inv_line_num );
3256 
3257       --insert exclusive tax to the ap tables
3258       --or insert recoverable inclusive tax with project information the ap tables
3259 
3260 
3261       --1.Only exlusive taxes or inclusive taxes with project information
3262       --  are inserted into Ap Lines and Dist Lines
3263 
3264       --2.All taxes need to be inserted into jai tax tables. Futher the
3265       --  partially recoverable tax need to be splitted into 2 lines
3266 
3267       --added by Eric for inclusive tax on 20-dec-2007,begin
3268       ------------------------------------------------------------------------
3269       IF ( NVL(lv_ap_line_to_inst_flag,'N')='Y' OR NVL(lv_tax_line_to_inst_flag,'N')='Y')
3270       THEN
3271         --ln_inv_line_num := ln_inv_line_num + 1; --added by eric for inlcusive tax
3272         --Commented by qinglei on 10-Feb-2012 for bug#13696344
3273         --Should not add ln_inv_line_num here, or the invoice line number will be
3274         --discontinuous.
3275       ---------------------------------------------------------------------------
3276       --added by Eric for inclusive tax on 20-dec-2007,end
3277        IF  NVL(lv_ap_line_to_inst_flag,'N')='Y'
3278        THEN
3279         ln_inv_line_num := ln_inv_line_num + 1;
3280         --Should add ln_inv_line_num here, when it really generate invoice line.
3281         --Added by Qinglei for bug#13696344
3282         INSERT INTO ap_invoice_lines_all
3283         (
3284           INVOICE_ID
3285         , LINE_NUMBER
3286         , LINE_TYPE_LOOKUP_CODE
3287         , DESCRIPTION
3288         , ORG_ID
3289         , MATCH_TYPE
3290   , DEFAULT_DIST_CCID --Changes by nprashar for bug #6995437
3291         , ACCOUNTING_DATE
3292         , PERIOD_NAME
3293         , DEFERRED_ACCTG_FLAG
3294         , DEF_ACCTG_START_DATE
3295         , DEF_ACCTG_END_DATE
3296         , DEF_ACCTG_NUMBER_OF_PERIODS
3297         , DEF_ACCTG_PERIOD_TYPE
3298         , SET_OF_BOOKS_ID
3299         , AMOUNT
3300         , WFAPPROVAL_STATUS
3301         , CREATION_DATE
3302         , CREATED_BY
3303         , LAST_UPDATED_BY
3304         , LAST_UPDATE_DATE
3305         , LAST_UPDATE_LOGIN
3306         /* 5763527 */
3307         , project_id
3308         , task_id
3309         , expenditure_type
3310         , expenditure_item_date
3311         , expenditure_organization_id
3312         /* End 5763527 */
3313         ,po_distribution_id --Added for bug#6780154
3314          )
3315         VALUES
3316         (
3317           inv_id
3318         , ln_inv_line_num
3319         , lv_misc
3320         , c_tax_rec.tax_name
3321         , v_org_id
3322         , lv_match_type
3323   , v_dist_code_combination_id  --Changes by nprashar for bug #6995437
3324         , rec_max_ap_lines_all.accounting_date
3325         , rec_max_ap_lines_all.period_name
3326         , rec_max_ap_lines_all.deferred_acctg_flag
3327         , rec_max_ap_lines_all.def_acctg_start_date
3328         , rec_max_ap_lines_all.def_acctg_end_date
3329         , rec_max_ap_lines_all.def_acctg_number_of_periods
3330         , rec_max_ap_lines_all.def_acctg_period_type
3331         , rec_max_ap_lines_all.set_of_books_id
3332         , ROUND(ROUND(v_tax_amount,ln_tax_precision),ln_precision) --Added by nprashar for bug # 13653889
3333         , rec_max_ap_lines_all.wfapproval_status -- Bug 4863208
3334         , sysdate
3335         , ln_user_id
3336         , ln_user_id
3337         , sysdate
3338         , ln_login_id
3339         /* 5763527 */
3340         , ln_project_id
3341         , ln_task_id
3342         , lv_exp_type
3343         , ld_exp_item_date
3344         , ln_exp_organization_id
3345         /* End 5763527 */
3346         ,po_dist_id --Added for bug#6780154
3347         );
3348        END IF;
3349 
3350      /*bug 9346307 - new LOOP added to insert multiple distributions for
3351      same line, if the matched PO shipment has multiple distributions*/
3352      v_distribution_no := 1;
3353 
3354      FOR r_ap_dist IN c_ap_dist(inv_id, pn_invoice_line_number)
3355      LOOP
3356       v_tax_amt_dist := v_tax_amount * r_ap_dist.qty_apportion_factor;
3357 
3358       IF nvl(r_ap_dist.invoice_price_variance, -1) <> -1 AND
3359          nvl(r_ap_dist.amount, -1) <> -1
3360       THEN
3361         v_price_var_accnt := r_ap_dist.price_var_code_combination_id;
3362         v_tax_variance_inv_cur := v_tax_amt_dist * (r_ap_dist.invoice_price_variance / r_ap_dist.amount);
3363         v_tax_variance_fun_cur := v_tax_variance_inv_cur * nvl(for_org_id_rec.exchange_rate, 1);
3364       END IF;
3365 
3366       IF i.modvat_flag = jai_constants.no OR line = 2 OR line = 4
3367       THEN
3368         v_assets_tracking_flag := r_ap_dist.assets_tracking_flag;
3369         lv_project_accounting_context := r_ap_dist.project_accounting_context;
3370         lv_pa_addition_flag := r_ap_dist.pa_addition_flag;
3371       END IF;
3372 
3373       v_dist_code_combination_id := Nvl(v_dist_code_combination_id,r_ap_dist.dist_code_combination_id);
3374 
3375       --Added by kunkumar for bug 5593895
3376        ln_base_amount := null ;
3377       if i.tax_type IN ( jai_constants.tax_type_value_added, jai_constants.tax_type_cst )  then
3378         ln_base_amount := jai_ap_utils_pkg.fetch_tax_target_amt(
3379                             p_invoice_id          =>   inv_id ,
3380                             p_line_location_id    =>   ln_po_line_location_id ,
3381                             p_transaction_id       =>   null  ,
3382                             p_parent_dist_id      =>   r_ap_dist.invoice_distribution_id ,
3383                             p_tax_id              =>   i.tax_id
3384                           ) ;
3385       end if ;
3386 
3387       --End; --Added by kunkumar for bug 5593895
3388         Fnd_File.put_line(Fnd_File.LOG,
3389         'Before inserting into ap_invoice_distributions_all for distribution line no :'
3390         ||v_distribution_no  );
3391 
3392         -- Start bug#3332988
3393         open c_get_invoice_distribution;
3394         fetch c_get_invoice_distribution into v_invoice_distribution_id;
3395         close c_get_invoice_distribution;
3396         -- End bug#3332988
3397 
3398         fnd_file.put_line(FND_FILE.LOG, ' Invoice distribution id '|| v_invoice_distribution_id);
3399 
3400        IF  NVL(lv_ap_line_to_inst_flag,'N')='Y'
3401        THEN
3402 
3403 	   -- start for bug 13906303 by anupgupt
3404 	       IF i.modvat_flag = 'Y' THEN -- if tax is recoverable
3405 	         DECLARE
3406               CURSOR c_tax_account_id -- tax_account_id of tax_id and tax type Other
3407               IS
3408                SELECT tax_account_id
3409                FROM jai_cmn_taxes_all
3410                WHERE tax_id = i.tax_id
3411                AND tax_type = 'Other';
3412 
3413               v_tax_account_id jai_cmn_taxes_all.tax_account_id%type := NULL;
3414              BEGIN
3415               OPEN c_tax_account_id;
3416               FETCH c_tax_account_id INTO v_tax_account_id;
3417               CLOSE c_tax_account_id;
3418 
3419               IF v_tax_account_id IS NOT NULL THEN -- overwrite tax_account_id if tax is recoverable and of type Other
3420                v_dist_code_combination_id := v_tax_account_id;
3421               END IF;
3422              END;
3423 		    END IF;
3424 		-- end for bug 13906303 by anupgupt
3425 
3426         INSERT INTO ap_invoice_distributions_all
3427         (
3428         accounting_date,
3429         accrual_posted_flag,
3430         assets_addition_flag,
3431         assets_tracking_flag,
3432         cash_posted_flag,
3433         distribution_line_number,
3434         dist_code_combination_id,
3435         invoice_id,
3436         last_updated_by,
3437         last_update_date,
3438         line_type_lookup_code,
3439         period_name,
3440         set_of_books_id ,
3441         amount,
3442         base_amount,
3443         batch_id,
3444         created_by,
3445         creation_date,
3446         description,
3447         exchange_rate_variance,
3448         last_update_login,
3449         match_status_flag,
3450         posted_flag,
3451         rate_var_code_combination_id ,
3452         reversal_flag ,
3453         program_application_id,
3454         program_id,
3455         program_update_date,
3456         accts_pay_code_combination_id,
3457         invoice_distribution_id,     /*  Added on 11-sep-00  */
3458         quantity_invoiced,
3459         po_distribution_id , -- added for bug # 2483164
3460         rcv_transaction_id, -- added for bug # 2483164
3461         price_var_code_combination_id, -- following three lines added for bug # 2775043.
3462         invoice_price_variance,
3463         base_invoice_price_variance,
3464         matched_uom_lookup_code  -- bug#3315162
3465         ,invoice_line_number -- Added by Brathod for Bug# 4445989
3466         ,org_id   -- Bug 4863208
3467         ,charge_applicable_to_dist_id -- Bug 5401111. Added by Lakshmi Gopalsami
3468         /* 5763527 */
3469         , project_id
3470         , task_id
3471         , expenditure_type
3472         , expenditure_item_date
3473         , expenditure_organization_id
3474         , project_accounting_context
3475         , pa_addition_flag
3476         -- Bug 7249100. Added by Lakshmi Gopalsami
3477           ,distribution_class
3478 		   , dist_match_type --Added by nprashar for bug # 12832424
3479         )
3480         VALUES
3481         (
3482         rec_max_ap_lines_all.accounting_date,
3483         'N',
3484       r_ap_dist.assets_addition_flag,
3485         v_assets_tracking_flag,
3486         'N',
3487         v_distribution_no,
3488         v_dist_code_combination_id,
3489         inv_id,
3490         ln_user_id,
3491         sysdate,
3492         lv_misc,
3493         rec_max_ap_lines_all.period_name,
3494         rec_max_ap_lines_all.set_of_books_id ,
3495       ROUND(ROUND(v_tax_amt_dist,ln_tax_precision),ln_precision), --Added by nprashar for bug # 13653889
3496       ROUND(ROUND(v_tax_amt_dist * r_ap_dist.exchange_rate,ln_tax_precision), ln_precision), --Added by nprashar for bug # 13653889
3497         v_batch_id,
3498         ln_user_id,
3499         sysdate,
3500         c_tax_rec.tax_name,
3501         null,--kunkumar for forward porting to R12
3502         ln_login_id,
3503       r_ap_dist.match_status_flag , -- Bug 4863208
3504         'N',
3505         NULL,
3506       r_ap_dist.reversal_flag,  -- Bug 4863208
3507       r_ap_dist.program_application_id,
3508       r_ap_dist.program_id,
3509       r_ap_dist.program_update_date,
3510       r_ap_dist.accts_pay_code_combination_id,
3511         v_invoice_distribution_id,
3512         -1, --Modified by kunkumar for bug# 5593895
3513       r_ap_dist.po_distribution_id ,
3514         rcv_tran_id,
3515         v_price_var_accnt,
3516         v_tax_variance_inv_cur,
3517         v_tax_variance_fun_cur,
3518       r_ap_dist.matched_uom_lookup_code,
3519         ln_inv_line_num,
3520       r_ap_dist.org_id, -- Bug 4863208
3521         -- Bug 5401111. Added by Lakshmi Gopalsami
3522         decode(v_assets_tracking_flag,'N',
3523              NULL,r_ap_dist.invoice_distribution_id)
3524           /* 5763527 */
3525         , ln_project_id
3526         , ln_task_id
3527         , lv_exp_type
3528         , ld_exp_item_date
3529         , ln_exp_organization_id
3530         , lv_project_accounting_context
3531         , lv_pa_addition_flag
3532         /* End 5763527 */
3533         -- Bug 7249100. Added by Lakshmi Gopalsami
3534         ,lv_dist_class
3535         , r_ap_dist.dist_match_type  --Added by nprashar for bug # 12832424
3536         );
3537 
3538       --added by Eric for inclusive tax on 20-dec-2007,begin
3539       -----------------------------------------------------------------
3540       END IF;--( NVL(lv_ap_line_to_inst_flag,'N')='Y')
3541       -----------------------------------------------------------------
3542       --added by Eric for inclusive tax on 20-dec-2007,end
3543 
3544 
3545       Fnd_File.put_line(Fnd_File.LOG, 'Before inserting into JAI_AP_MATCH_INV_TAXES ');
3546 
3547       --added by Eric for inclusive tax on 20-dec-2007,begin
3548       -----------------------------------------------------------------
3549       IF  (NVL(lv_tax_line_to_inst_flag,'N') = 'Y')
3550       THEN
3551       -----------------------------------------------------------------
3552       --added by Eric for inclusive tax on 20-dec-2007,end
3553 
3554         --Added by Qinglei on 10-Feb-2012 for bug#13589613 begin
3555         IF NVL(i.inc_tax_flag,'N') = 'Y' and NVL(lv_modvat_flag,'N') = 'Y' THEN
3556           ln_inv_line_num := ln_inv_line_num + 1;
3557         END IF;
3558         --For inclusive tax, should ln_inv_line_num here to ensure the column invoice_line_number
3559         --is the same as line number of ap_invoice_lines_all.
3560         --Added by Qinglei on 10-Feb-2012 for bug#13589613 end
3561 
3562         INSERT INTO JAI_AP_MATCH_INV_TAXES
3563         (
3564         tax_distribution_id,
3565 
3566         assets_tracking_flag,
3567         invoice_id,
3568         po_header_id,
3569         po_line_id,
3570         line_location_id,
3571         set_of_books_id,
3572         --org_id,
3573         exchange_rate,
3574         exchange_rate_type,
3575         exchange_date,
3576         currency_code,
3577         code_combination_id,
3578         last_update_login,
3579         creation_date,
3580         created_by,
3581         last_update_date,
3582         last_updated_by,
3583         acct_pay_code_combination_id,
3584         accounting_date,
3585         tax_id,
3586         tax_amount,
3587         base_amount,
3588         chart_of_accounts_id,
3589         distribution_line_number,
3590         po_distribution_id,  -- added  by bug#3038566
3591         parent_invoice_distribution_id,  -- added  by bug#3038566
3592         legal_entity_id -- added by rallamse bug#
3593         ,INVOICE_LINE_NUMBER  --    Added by Brathod, Bug# 4445989
3594         ,INVOICE_DISTRIBUTION_ID ------------|
3595         ,PARENT_INVOICE_LINE_NUMBER----------|
3596         ,RCV_TRANSACTION_ID
3597         ,LINE_TYPE_LOOKUP_CODE
3598         /* 5763527 */
3599         , recoverable_flag
3600         , line_no            -- Bug 5553150, 5593895
3601         )
3602         -- End Bug# 4445989
3603         VALUES
3604         (
3605         JAI_AP_MATCH_INV_TAXES_S.NEXTVAL,
3606 
3607         v_assets_tracking_flag, -- 'N', Commented by Aparajita for bug # 2851123
3608         inv_id,
3609         cur_items_rec.po_header_id,
3610         cur_items_rec.po_line_id,
3611         cur_items_rec.line_location_id,
3612         cur_items_rec.set_of_books_id,
3613         --cur_items_rec.org_id,
3614         cur_items_rec.rate,
3615         cur_items_rec.rate_type,
3616         cur_items_rec.rate_date,
3617         cur_items_rec.currency_code,
3618         v_dist_code_combination_id,
3619         cur_items_rec.last_update_login,
3620         cur_items_rec.creation_date,
3621         cur_items_rec.created_by,
3622         cur_items_rec.last_update_date,
3623         cur_items_rec.last_updated_by,
3624         apccid,
3625         cur_items_rec.invoice_date,
3626         i.tax_id,
3627         --ROUND(v_tax_amount, ln_precision), deleted by eric for inclusvice tax on 20-dec-2007
3628         ROUND(ROUND(lv_tax_line_amount * r_ap_dist.qty_apportion_factor, ln_tax_precision),ln_precision),
3629         nvl(ln_base_amount, ROUND(ROUND(i.tax_amount*r_ap_dist.qty_apportion_factor, ln_tax_precision), ln_precision)),
3630         caid,
3631         v_distribution_no,
3632       r_ap_dist.po_distribution_id, -- added  by bug#3038566
3633       r_ap_dist.invoice_distribution_id, -- added  by bug#3038566
3634         get_ven_info_rec.legal_entity_id -- added by rallamse bug#
3635         --, ln_inv_line_num , modified by eric for inclusvice tax
3636         --v_invoice_distribution_id,deleted by eric for inclusvice tax on 20-dec-2007
3637 
3638         --added by eric for inclusvice tax on 20-dec-2007,begin
3639         ---------------------------------------------------------------
3640         --Modified by Qinglei on 10-Feb-2012 for bug#13589613 begin
3641         /*, DECODE ( NVL(i.inc_tax_flag,'N')
3642                  , 'N',ln_inv_line_num
3643                  , 'Y',pn_invoice_line_number
3644                  )*/
3645         , DECODE ( NVL(i.inc_tax_flag,'N')
3646                  , 'N',ln_inv_line_num
3647                  , 'Y',DECODE(lv_modvat_flag,'Y',ln_inv_line_num,'N',pn_invoice_line_number)
3648                  )
3649         --For inclusive tax recoverable line, the invoice_line_number should the same as MISC line of ap_invoice_lines_all.
3650         --To ensure that it can generate journal entries in gl interface after invoice create accounting.
3651         --Modified by Qinglei on 10-Feb-2012 for bug#13589613 begin
3652 
3653         , NVL(v_invoice_distribution_id,for_dist_insertion_rec.invoice_distribution_id)
3654         ---------------------------------------------------------------
3655         --added by eric for inclusvice tax on 20-dec-2007,end
3656         , pn_invoice_line_number
3657         , rcv_tran_id
3658         , lv_misc
3659         , lv_modvat_flag -- 5763527
3660         , i.tax_line_no  -- Bug 5553150, 5593895
3661         );
3662 
3663       --added by Eric for inclusive tax on 20-dec-2007,begin
3664       -----------------------------------------------------------------
3665       END IF;--(NVL(lv_tax_line_to_inst_flag,'N') = 'Y')
3666       -----------------------------------------------------------------
3667       --added by Eric for inclusive tax on 20-dec-2007,end
3668 
3669 
3670       /*  Bug 46863208. Added by Lakshmi Gopalsami
3671            Commented the MRC call
3672       insert_mrc_data(v_invoice_distribution_id); -- bug#3332988
3673       */
3674 
3675       v_distribution_no := v_distribution_no +1;
3676 
3677       END LOOP;  /*bug 9346307*/
3678 
3679       /* Commented out by Eric for bug 8345512
3680       cum_tax_amt := cum_tax_amt + ROUND(v_tax_amount, ln_precision);
3681       */
3682 
3683       --Modified by Eric for bug 8345512 on 13-Jul-2009,begin
3684       ------------------------------------------------------------------
3685       IF NVL(i.inc_tax_flag,'N') ='N'
3686       THEN
3687         cum_tax_amt := cum_tax_amt + ROUND(round(v_tax_amount,ln_tax_precision), ln_precision); /* Bug 14386866 */
3688       END IF;
3689       ------------------------------------------------------------------
3690       --Modified by Eric for bug 8345512 on 13-Jul-2009,end
3691       END IF;
3692       /* Obsoleted as a part of R12 , Bug# 4445989
3693       Fnd_File.put_line(Fnd_File.LOG, 'Before inserting into JAI_CMN_FA_INV_DIST_ALL');
3694       INSERT INTO JAI_CMN_FA_INV_DIST_ALL
3695       (
3696       invoice_id,
3697       invoice_distribution_id,
3698       set_of_books_id,
3699       batch_id,
3700       po_distribution_id,
3701       rcv_transaction_id,
3702       dist_code_combination_id,
3703       accounting_date,
3704       assets_addition_flag,
3705       assets_tracking_flag,
3706       distribution_line_number,
3707       line_type_lookup_code,
3708       amount,
3709       description,
3710       match_status_flag,
3711       quantity_invoiced
3712       )
3713       VALUES
3714       (
3715       inv_id,
3716       ap_invoice_distributions_s.CURRVAL,
3717       for_dist_insertion_rec.set_of_books_id,
3718       v_batch_id,
3719       for_dist_insertion_rec.po_distribution_id,
3720       rcv_tran_id,
3721       v_dist_code_combination_id,
3722       for_dist_insertion_rec.accounting_date,
3723       for_dist_insertion_rec.assets_addition_flag,
3724       v_assets_tracking_flag, -- for_dist_insertion_rec.assets_tracking_flag, bug # 2851123
3725       v_distribution_no,
3726       'MISCELLANEOUS',
3727       ROUND(v_tax_amount, ln_precision),
3728       c_tax_rec.tax_name,
3729       NULL,
3730       NULL);
3731       */
3732       end loop ;--> for line in 1 to ln_lines_to_insert Brathod, 5763527
3733       --start additions by vkaranam for bug 11936596
3734 
3735  OPEN inclu_flag_cur(v_org_id);
3736         FETCH inclu_flag_cur INTO lv_inclu_tax_flag;
3737         CLOSE inclu_flag_cur;
3738  fnd_file.put_line(fnd_file.log ,'separate accounting for inclusive taxes option is '||lv_inclu_tax_flag );
3739 
3740 
3741 If nvl(lv_accrue_on_receipt_flag ,'N')='Y'
3742 --and nvl(lv_inclu_tax_flag,'N')='Y'
3743 then
3744    fnd_file.put_line(fnd_file.log ,'Start the Insertion of Inclusive Service tax lines for POT recoverable service amount is '||ln_inclurec_tax_amt);
3745    lv_tax_line_amount := null;
3746    lv_ap_line_to_inst_flag := null;
3747    ln_inclu_serv_amt := null;
3748    v_dist_code_combination_id := null;
3749 
3750 for inclu_Serv_line in 1..2
3751 loop
3752       ln_inclu_serv_amt := ln_inclurec_tax_amt;
3753 
3754       if ln_inclu_serv_amt <>0
3755          --Modified by Qinglei on 10-Feb-2012 for bug#13696344 begin
3756          and NVL(i.inc_tax_flag,'N') = 'Y'
3757          and i.tax_type IN ( 'Service','SERVICE_EDUCATION_CESS','SERVICE_SH_EDU_CESS')
3758          ----Modified by Qinglei on 10-Feb-2012 for bug#13696344 end
3759       then
3760         if inclu_Serv_line =1
3761         then
3762 
3763            lv_tax_line_amount:=  ln_inclu_serv_amt       ;
3764            lv_ap_line_to_inst_flag  := 'Y';
3765            fnd_file.put_line(fnd_file.log ,'Inclusive Service tax lines for POT tax type '||c_tax_rec.tax_type ||' v_org_id '||v_org_id );
3766            v_dist_code_combination_id := check_service_interim_account
3767                                                     (
3768                                                       lv_accrue_on_receipt_flag,
3769                                                       lv_accounting_method_option,
3770                                                       lv_is_item_an_expense,
3771                                                       r_jai_regimes.regime_id,
3772                                                       v_org_id,
3773                                                       'INCLUSIVE',--12813762
3774                                                       c_tax_rec.tax_type,
3775                                                       Nvl(po_dist_id,ln_po_dist_id)
3776                                                     );
3777            fnd_file.put_line(fnd_file.log ,'Inclusive Service tax lines for positve recovery line POT interim recovery account is '||v_dist_code_combination_id);
3778 
3779         elsif inclu_Serv_line =2
3780         then
3781            lv_tax_line_amount :=  ln_inclu_serv_amt *-1      ;
3782            lv_ap_line_to_inst_flag  := 'Y';
3783            v_dist_code_combination_id := for_dist_insertion_rec.dist_code_combination_id;
3784            fnd_file.put_line(fnd_file.log ,'Inclusive Service tax  POT lines for negative recovery line  ap accrual account is '||v_dist_code_combination_id);
3785          end if;
3786       end if;-- pot ln_inclu_serv_amt <>0
3787              IF ( NVL(lv_ap_line_to_inst_flag,'N')='Y')
3788                   THEN
3789                     --Modified by Qinglei on 10-Feb-2012 for bug#13589613 begin
3790                     IF inclu_Serv_line =2 THEN
3791                        ln_inv_line_num := ln_inv_line_num + 1;
3792                     END IF;
3793                     --For inclusive tax recoverable part, the positive invoice line should use invoice_line_number
3794                     --when insert jai_ap_match_inv_taxes used; for the negative line, will use ln_inv_line_num + 1 as
3795                     --line number.
3796                     --Modified by Qinglei on 10-Feb-2012 for bug#13589613 end
3797                     Fnd_File.put_line(Fnd_File.LOG,'Before inserting Inclusive Service tax lines for POT into ap_invoice_lines_all for line no :'|| ln_inv_line_num ||'  tax amount =  '||lv_tax_line_amount);
3798 
3799                            INSERT INTO ap_invoice_lines_all
3800                           (
3801                             INVOICE_ID
3802                           , LINE_NUMBER
3803                           , LINE_TYPE_LOOKUP_CODE
3804                           , DESCRIPTION
3805                           , ORG_ID
3806                           , MATCH_TYPE
3807                           , DEFAULT_DIST_CCID
3808                           , ACCOUNTING_DATE
3809                           , PERIOD_NAME
3810                           , DEFERRED_ACCTG_FLAG
3811                           , DEF_ACCTG_START_DATE
3812                           , DEF_ACCTG_END_DATE
3813                           , DEF_ACCTG_NUMBER_OF_PERIODS
3814                           , DEF_ACCTG_PERIOD_TYPE
3815                           , SET_OF_BOOKS_ID
3816                           , AMOUNT
3817                           , WFAPPROVAL_STATUS
3818                           , CREATION_DATE
3819                           , CREATED_BY
3820                           , LAST_UPDATED_BY
3821                           , LAST_UPDATE_DATE
3822                           , LAST_UPDATE_LOGIN
3823                           , project_id
3824                           , task_id
3825                           , expenditure_type
3826                           , expenditure_item_date
3827                           , expenditure_organization_id
3828                           ,po_distribution_id
3829                            )
3830                           VALUES
3831                           (
3832                             inv_id
3833                           , ln_inv_line_num
3834                           , lv_misc
3835                           , c_tax_rec.tax_name
3836                           , v_org_id
3837                           , lv_match_type
3838                           , v_dist_code_combination_id
3839                           , rec_max_ap_lines_all.accounting_date
3840                           , rec_max_ap_lines_all.period_name
3841                           , rec_max_ap_lines_all.deferred_acctg_flag
3842                           , rec_max_ap_lines_all.def_acctg_start_date
3843                           , rec_max_ap_lines_all.def_acctg_end_date
3844                           , rec_max_ap_lines_all.def_acctg_number_of_periods
3845                           , rec_max_ap_lines_all.def_acctg_period_type
3846                           , rec_max_ap_lines_all.set_of_books_id
3847                           , ROUND(ROUND(lv_tax_line_amount,ln_tax_precision),ln_precision) --Added by nprashar for bug # 13653889
3848                           , rec_max_ap_lines_all.wfapproval_status -- Bug 4863208
3849                           , sysdate
3850                           , ln_user_id
3851                           , ln_user_id
3852                           , sysdate
3853                           , ln_login_id
3854                           /* 5763527 */
3855                           , ln_project_id
3856                           , ln_task_id
3857                           , lv_exp_type
3858                           , ld_exp_item_date
3859                           , ln_exp_organization_id
3860                           /* End 5763527 */
3861                           ,po_dist_id --Added for bug#6780154
3862                           );
3863 
3864                     Fnd_File.put_line(Fnd_File.LOG,
3865                     'Before inserting  Inclusive Service tax lines for POT into ap_invoice_distributions_all for distribution line no :' || v_distribution_no);
3866 
3867 
3868                     open c_get_invoice_distribution;
3869                     fetch c_get_invoice_distribution into v_invoice_distribution_id;
3870                     close c_get_invoice_distribution;
3871 
3872 
3873                     fnd_file.put_line(FND_FILE.LOG, 'Inclusive Service tax lines for POT Invoice distribution id '|| v_invoice_distribution_id);
3874 
3875                  INSERT INTO ap_invoice_distributions_all
3876                              (accounting_date,
3877                               accrual_posted_flag,
3878                               assets_addition_flag,
3879                               assets_tracking_flag,
3880                               cash_posted_flag,
3881                               distribution_line_number,
3882                               dist_code_combination_id,
3883                               invoice_id,
3884                               last_updated_by,
3885                               last_update_date,
3886                               line_type_lookup_code,
3887                               period_name,
3888                               set_of_books_id ,
3889                               amount,
3890                               base_amount,
3891                               batch_id,
3892                               created_by,
3893                               creation_date,
3894                               description,
3895                               exchange_rate_variance,
3896                               last_update_login,
3897                               match_status_flag,
3898                               posted_flag,
3899                               rate_var_code_combination_id ,
3900                               reversal_flag ,
3901                               program_application_id,
3902                               program_id,
3903                               program_update_date,
3904                               accts_pay_code_combination_id,
3905                               invoice_distribution_id,
3906                               quantity_invoiced,
3907                               po_distribution_id ,
3908                               rcv_transaction_id,
3909                               price_var_code_combination_id,
3910                               invoice_price_variance,
3911                               base_invoice_price_variance,
3912                               matched_uom_lookup_code
3913                               , invoice_line_number
3914                               , org_id
3915                               , charge_applicable_to_dist_id
3916                               , project_id
3917                               , task_id
3918                               , expenditure_type
3919                               , expenditure_item_date
3920                               , expenditure_organization_id
3921                               , project_accounting_context
3922                               , pa_addition_flag
3923                               , distribution_class
3924                               , dist_match_type  --Added by nprashar for bug # 12832424
3925                               )
3926                               VALUES
3927                               (
3928                               rec_max_ap_lines_all.accounting_date,
3929                               'N',
3930                               --r_ap_dist.assets_addition_flag,
3931                               for_dist_insertion_rec.assets_addition_flag,
3932                               --Modified by Qinglei on 10-Feb-2012 for bug#13696344
3933                               v_assets_tracking_flag,
3934                               'N',
3935                               v_distribution_no,
3936                               v_dist_code_combination_id,
3937                               inv_id,
3938                               ln_user_id,
3939                               sysdate,
3940                               lv_misc,
3941                               rec_max_ap_lines_all.period_name,
3942                               rec_max_ap_lines_all.set_of_books_id ,
3943                               ROUND(ROUND(lv_tax_line_amount,ln_tax_precision),ln_precision),  --Added by nprashar for bug # 13653889
3944                               ROUND(ROUND(lv_tax_line_amount * r_ap_dist.exchange_rate,ln_tax_precision), ln_precision), --Added by nprashar for bug # 13653889
3945                               v_batch_id,
3946                               ln_user_id,
3947                               sysdate,
3948                               c_tax_rec.tax_name,
3949                               null,
3950                               ln_login_id,
3951                               --r_ap_dist.match_status_flag ,
3952                               for_dist_insertion_rec.match_status_flag,
3953                               --Modified by Qinglei on 10-Feb-2012 for bug#13696344
3954                               'N',
3955                               NULL,
3956                               /*r_ap_dist.reversal_flag,
3957                               r_ap_dist.program_application_id,
3958                               r_ap_dist.program_id,
3959                               r_ap_dist.program_update_date,
3960                               r_ap_dist.accts_pay_code_combination_id,*/
3961                               for_dist_insertion_rec.reversal_flag,
3962                               for_dist_insertion_rec.program_application_id,
3963                               for_dist_insertion_rec.program_id,
3964                               for_dist_insertion_rec.program_update_date,
3965                               for_dist_insertion_rec.accts_pay_code_combination_id,
3966                               --Modified by Qinglei on 10-Feb-2012 for bug#13696344
3967                               v_invoice_distribution_id,
3968                               -1,
3969                               --r_ap_dist.po_distribution_id ,
3970                               for_dist_insertion_rec.po_distribution_id,
3971                               rcv_tran_id,
3972                               v_price_var_accnt,
3973                               v_tax_variance_inv_cur,
3974                               v_tax_variance_fun_cur,
3975                               --r_ap_dist.matched_uom_lookup_code,
3976                               for_dist_insertion_rec.matched_uom_lookup_code,
3977                               ln_inv_line_num,
3978                               --r_ap_dist.org_id,
3979                               for_dist_insertion_rec.org_id,
3980                               --Modified by Qinglei on 10-Feb-2012 for bug#13696344
3981                               decode(v_assets_tracking_flag,'N',
3982                                    NULL,/*r_ap_dist.invoice_distribution_id*/for_dist_insertion_rec.invoice_distribution_id)
3983                                    --Modified by Qinglei on 10-Feb-2012 for bug#13696344
3984                               , ln_project_id
3985                               , ln_task_id
3986                               , lv_exp_type
3987                               , ld_exp_item_date
3988                               , ln_exp_organization_id
3989                               , lv_project_accounting_context
3990                               , lv_pa_addition_flag
3991                               , lv_dist_class
3992                               --, r_ap_dist.dist_match_type --Added by nprashar for bug # 12832424
3993                               , for_dist_insertion_rec.dist_match_type
3994                               --Modified by Qinglei on 10-Feb-2012 for bug#13696344
3995                               );
3996 
3997            --Added by Qiong for bug13579239  begin
3998            --------------------------------
3999            UPDATE JAI_AP_MATCH_INV_TAXES jat
4000            SET    jat.invoice_distribution_id = v_invoice_distribution_id
4001            WHERE  jat.invoice_id = inv_id
4002            AND    jat.po_header_id = cur_items_rec.po_header_id
4003            AND    jat.po_line_id = cur_items_rec.po_line_id
4004            AND    jat.line_location_id = cur_items_rec.line_location_id
4005            AND    jat.set_of_books_id = cur_items_rec.set_of_books_id
4006            AND    jat.line_no = i.tax_line_no
4007            AND    jat.tax_id = i.tax_id
4008            AND    jat.recoverable_flag = decode(inclu_Serv_line,1,'Y',2,'N','N');
4009 
4010            --------------------------------
4011            --Added by Qiong for bug13579239  end
4012 
4013 
4014         END IF;  --POT ( NVL(lv_ap_line_to_inst_flag,'N')='Y')
4015 
4016  end loop; --end positive negative service line loop
4017 
4018 end if; --end accrue one receipt flag condition
4019 
4020 --end additions by vkaranam for bug 11936596
4021 
4022 
4023     END LOOP;
4024 
4025     /* Bug 5358788. Added by Lakshmi Gopalsami
4026      * Removed the update which is updating ap_invoices_all
4027      * and ap_payment_schedules_all
4028      */
4029 
4030     /* Bug 8866084 - Start
4031      * Added the update which is updating ap_invoices_all
4032      * and ap_payment_schedules_all
4033      * Needs to be updated for Invoices created from iSupplier
4034     */
4035 
4036      open c_get_ap_invoice_src (inv_id);
4037      fetch c_get_ap_invoice_src into ln_source;
4038      close c_get_ap_invoice_src;
4039 
4040      if (ln_source = 'ISP') then
4041 
4042          /*
4043          cum_tax_amt is initialized with -1 for Bug 5593895. Bug updates are not clear as to why it was done
4044          This causes Tax Amount to be less by 1 when PO Matched Invoice is created from iSupplier
4045          Hence incrementing Tax Amount by 1 for this specific scenario as modifying initialization to 0 may cause regression
4046          */
4047          cum_tax_amt := cum_tax_amt + 1;
4048 
4049          v_update_payment_schedule:=update_payment_schedule(cum_tax_amt);
4050 
4051          UPDATE ap_invoices_all
4052          SET   invoice_amount       =  invoice_amount   + cum_tax_amt,
4053                approved_amount      =  approved_amount  + cum_tax_amt,
4054                pay_curr_invoice_amount =  pay_curr_invoice_amount + cum_tax_amt,
4055                amount_applicable_to_discount =  amount_applicable_to_discount + cum_tax_amt,
4056                payment_status_flag = decode(payment_status_flag, 'Y', 'P', payment_status_flag)
4057          WHERE  invoice_id = inv_id;
4058 
4059          if for_org_id_rec.invoice_currency_code <> v_functional_currency then
4060             -- invoice currency is not the functional currency.
4061             update ap_invoices_all
4062             set    base_amount = invoice_amount  * exchange_rate
4063             where  invoice_id = inv_id;
4064          end if;
4065 
4066      end if;
4067 
4068      /*Bug 8866084 - End*/
4069 
4070   ELSIF p_rematch = 'PAY_ON_RECEIPT' THEN
4071 
4072     Fnd_File.put_line(Fnd_File.LOG, 'inside  p_rematch = PAY_ON_RECEIPT ');
4073 
4074     IF p_receipt_code = 'RECEIPT' THEN
4075 
4076       Fnd_File.put_line(Fnd_File.LOG, 'inside  p_receipt_code = RECEIPT ');
4077 
4078       /* Begin 5763527 */
4079       fnd_file.put_line(fnd_file.log, 'rcv_tran_id='||rcv_tran_id);
4080       open  c_get_excise_costing_flag (cp_rcv_transaction_id => rcv_tran_id
4081                                     ,cp_organization_id =>  r_rcv_transactions.organization_id   --Added by Bgowrava for Bug#7503308
4082                                       ,cp_shipment_header_id     =>  p_shipment_header_id          --Added by Bgowrava for Bug#7503308
4083                     ,cp_txn_type        =>  'DELIVER'                           --Added by Bgowrava for Bug#7503308
4084                                       ,cp_attribute1      =>  'CENVAT_COSTED_FLAG');              --Added by Bgowrava for Bug#7503308
4085       fetch c_get_excise_costing_flag into lv_excise_costing_flag;
4086       close c_get_excise_costing_flag ;
4087       /* End 5763527 */
4088 
4089       FOR tax_lines1_rec IN tax_lines1_cur(rcv_tran_id,for_org_id_rec.vendor_id)  LOOP
4090 
4091         -- Added By Brathod, Bug# 44459989
4092         /* v_distribution_no := v_distribution_no + 1 ; */
4093         -- ln_inv_line_num := ln_inv_line_num + 1; -- 5763527 moved the increment to just before insert statement
4094         v_distribution_no  := 1;
4095         -- End Bug# 4445989
4096 
4097         r_service_regime_tax_type := null;
4098 
4099         -- Bug 5358788. Added by Lakshmi Gopalsami
4100         r_VAT_regime_tax_type := null;
4101 
4102         /* 5763527 */
4103         ln_project_id           := null;
4104         ln_task_id              := null;
4105         lv_exp_type             := null;
4106         ld_exp_item_date        := null;
4107         ln_exp_organization_id  := null;
4108         lv_project_accounting_context := null;
4109         lv_pa_addition_flag := null;
4110 
4111 
4112         OPEN  c_tax(tax_lines1_rec.tax_id);
4113         FETCH c_tax INTO c_tax_rec;
4114         CLOSE c_tax;
4115 
4116 		 --Added by nprashar for bug # 13653889
4117 	  OPEN c_tax_curr_prec( tax_lines1_rec.tax_id ) ;
4118       FETCH c_tax_curr_prec INTO ln_tax_precision ;
4119       CLOSE c_tax_curr_prec ;
4120 
4121 
4122    -- added, kunkumar for 5593895
4123         ln_base_amount := null ;
4124         if tax_lines1_rec.tax_type IN ( jai_constants.tax_type_value_added, jai_constants.tax_type_cst )  then
4125           ln_base_amount := jai_ap_utils_pkg.fetch_tax_target_amt
4126                             (
4127                               p_invoice_id          =>   inv_id ,
4128                               p_line_location_id    =>   null ,
4129                               p_transaction_id      =>   rcv_tran_id  ,
4130                               p_parent_dist_id      =>   for_dist_insertion_rec.invoice_distribution_id ,
4131                               p_tax_id              =>   tax_lines1_rec.tax_id
4132                             ) ;
4133         end if ;
4134         -- ended, Harshita for Bug 5593895
4135 
4136 
4137         /* Service Start */
4138         v_assets_tracking_flag := for_dist_insertion_rec.assets_tracking_flag;
4139         v_dist_code_combination_id := null;
4140 
4141         --initial the tax_type
4142         lv_tax_type := NULL;--added by eric for inclusive tax on 20-Dec,2007
4143 
4144 
4145         -- 5763527, Brathod
4146         -- Modified the if condition to consider partial recoverable lines previous logic was hadling only when it is 100
4147         if tax_lines1_rec.modvat_flag = jai_constants.YES
4148         and nvl(c_tax_rec.mod_cr_percentage, -1) > 0
4149         then
4150 
4151 
4152          /*  recoverable tax */
4153           v_assets_tracking_flag := 'N';
4154 
4155           --recoverable tax
4156           lv_tax_type := 'RE'; --added by eric for inclusive tax on 20-dec,2007
4157 
4158           open c_regime_tax_type(r_jai_regimes.regime_id, c_tax_rec.tax_type);
4159           fetch c_regime_tax_type into r_service_regime_tax_type;
4160           close c_regime_tax_type;
4161 
4162     fnd_file.put_line(FND_FILE.LOG,
4163                    ' Service regime: '||r_service_regime_tax_type.tax_type);
4164 
4165      /* Bug 5358788. Added by Lakshmi Gopalsami
4166     * Fetched the details of VAT regime
4167     */
4168 
4169          OPEN  c_regime_tax_type(r_vat_regimes.regime_id, c_tax_rec.tax_type);
4170           FETCH  c_regime_tax_type INTO  r_vat_regime_tax_type;
4171          CLOSE  c_regime_tax_type;
4172 
4173          fnd_file.put_line(FND_FILE.LOG,
4174                    ' VAT regime: '||r_vat_regime_tax_type.tax_type);
4175 
4176           if r_service_regime_tax_type.tax_type is not null then
4177             /* Service type of tax */
4178             v_dist_code_combination_id := check_service_interim_account
4179                                           (
4180                                             lv_accrue_on_receipt_flag,
4181                                             lv_accounting_method_option,
4182                                             lv_is_item_an_expense,
4183                                             r_jai_regimes.regime_id,
4184                                             v_org_id,
4185                                             p_rematch,
4186                                             c_tax_rec.tax_type,
4187               po_dist_id  --Added by JMEENA for bug#6833506
4188                                           );
4189     fnd_file.put_line(FND_FILE.LOG,
4190                  ' Regime type , CCID '
4191      || r_service_regime_tax_type.tax_type
4192            ||', ' || v_dist_code_combination_id);
4193 
4194          /* Bug 5358788. Added by Lakshmi Gopalsami
4195     * Commented p_rematch and added validation for
4196     * VAT regime.
4197     */
4198   /*following elsif block modified for bug 6595773*/
4199           ELSIF r_vat_regime_tax_type.tax_type IS NOT NULL THEN
4200             --p_rematch = 'PO_MATCHING' then
4201 
4202             v_dist_code_combination_id := for_dist_insertion_rec.dist_code_combination_id;
4203             fnd_file.put_line(FND_FILE.LOG,
4204                  ' Regime type , CCID '
4205                  || r_vat_regime_tax_type.tax_type
4206                  ||', ' || v_dist_code_combination_id);
4207            END IF;
4208 
4209         ELSE  /* 5763527 introduced for PROJETCS COSTING Impl */
4210           -- Bug 6338371
4211           -- Added if condition.  Project details should be populated only if
4212           -- the accrue_on_receipt is not checked on po shipment
4213           IF nvl(lv_accrue_on_receipt_flag,'#') <> 'Y' THEN
4214             ln_project_id           := p_project_id;
4215             ln_task_id              := p_task_id;
4216             lv_exp_type             := p_expenditure_type;
4217             ld_exp_item_date        := p_expenditure_item_date;
4218             ln_exp_organization_id  := p_expenditure_organization_id;
4219             lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
4220             lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
4221     END IF;          /* End 5763527 */
4222 
4223           --non recoverable
4224           lv_tax_type := 'NR';  --added by eric for inclusive tax on 20-dec,2007
4225         END IF; /*nvl(c_tax_rec.mod_cr_percentage, 0) = 100  and c_tax_rec.tax_account_id is not null*/
4226 
4227 
4228         /* Bug#4177452*/
4229         if v_dist_code_combination_id is null then
4230           v_dist_code_combination_id := for_dist_insertion_rec.dist_code_combination_id;
4231         end if;
4232 
4233 
4234         /* Service End */
4235 
4236 
4237         -- Start for bug#3752887
4238         --start additions for bug#12697697
4239          Fnd_File.put_line(Fnd_File.LOG, 'tax_lines1_rec.adhoc_flag = '||tax_lines1_rec.adhoc_flag);
4240          Fnd_File.put_line(Fnd_File.LOG, 'tax_lines1_rec.vat_flag = '||tax_lines1_rec.vat_flag);
4241          Fnd_File.put_line(Fnd_File.LOG, ' tax_lines1_rec.tax_type = '||tax_lines1_rec.tax_type);
4242         if tax_lines1_rec.adhoc_flag='Y'  then
4243           -- start bug # 13024213
4244 	  --if tax_lines1_rec.tax_type in ('Freight','Insurance','Octrai','Other','PURCHASE TAX','ENTRY TAX') and tax_lines1_rec.vat_flag = 'Q'  --Added IN clause for bug  13363329
4245 	  if tax_lines1_rec.tax_type in ('Freight','Insurance','Octrai','Other','PURCHASE TAX','ENTRY TAX','Excise','EXCISE_EDUCATION_CESS','EXCISE_SH_EDU_CESS') and tax_lines1_rec.vat_flag = 'Q' -- for bug 16195110 by anupgupt
4246           then
4247               --Changes by nprashar for bug # 13363329
4248 		   v_apportn_factor_for_adhoc:= jai_ap_utils_pkg.get_apportion_factor(inv_id, pn_invoice_line_number,'QUANTITY');
4249                    v_tax_amount := tax_lines1_rec.tax_amount * v_apportn_factor_for_adhoc;
4250 	  -- end bug # 13024213
4251           --elsif r_tax_lines_rec.tax_type not in ('Freight','Insurance','Octrai','Other','PURCHASE TAX','ENTRY TAX')
4252 		  elsif r_tax_lines_rec.tax_type not in ('Freight','Insurance','Octrai','Other','PURCHASE TAX','ENTRY TAX','Excise','EXCISE_EDUCATION_CESS','EXCISE_SH_EDU_CESS') -- for bug 16195110 by anupgupt
4253          then
4254            v_tax_amount := tax_lines1_rec.tax_amount * v_apportn_factor_for_item_line;
4255          else
4256            v_tax_amount := tax_lines1_rec.tax_amount ;
4257          end if;
4258        else
4259            v_tax_amount := tax_lines1_rec.tax_amount * v_apportn_factor_for_item_line;
4260         end if;
4261         Fnd_File.put_line(Fnd_File.LOG, 'v_tax_amount after apportiong '||v_tax_amount);
4262 
4263         --end additions for bug#12697697
4264 
4265         if for_org_id_rec.invoice_currency_code <> tax_lines1_rec.currency then
4266           v_tax_amount := v_tax_amount / for_org_id_rec.exchange_rate;
4267         end if;
4268         -- End for bug#3752887
4269 
4270 
4271         -- Start bug#4103473
4272         if nvl(v_tax_amount,-1) = -1 then
4273           v_tax_amount := apportion_tax_4_price_cor_inv(v_tax_amount,  tax_lines1_rec.tax_id);
4274         END IF;
4275         v_tax_amount := nvl(v_tax_amount,-1);
4276         -- End bug#4103473
4277 
4278         /* Bug#5763527 */
4279         Fnd_File.put_line(Fnd_File.LOG, 'c_tax_rec.tax_type='||c_tax_rec.tax_type  ||',lv_excise_costing_flag='||lv_excise_costing_flag);
4280         if UPPER(c_tax_rec.tax_type)  like  '%EXCISE%' then
4281           if lv_excise_costing_flag = 'Y' then
4282             IF  nvl(lv_accrue_on_receipt_flag,'#') <> 'Y' -- Bug 6338371
4283             then
4284               ln_project_id           := p_project_id;
4285               ln_task_id              := p_task_id;
4286               lv_exp_type             := p_expenditure_type;
4287               ld_exp_item_date        := p_expenditure_item_date;
4288               ln_exp_organization_id  := p_expenditure_organization_id;
4289               lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
4290               lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
4291             end if;
4292             v_dist_code_combination_id    := for_dist_insertion_rec.dist_code_combination_id;
4293           END if;
4294         end if;
4295         /* End# 5763527 */
4296 
4297         --
4298         -- Begin 5763527
4299         --
4300         ln_rec_tax_amt  := null;
4301         ln_nrec_tax_amt := null;
4302         ln_lines_to_insert := 1; -- Loop controller to insert more than one lines for partially recoverable tax lines in PO
4303 
4304         --added by Eric for inclusive tax on 20-dec-2007,begin
4305         -------------------------------------------------------------------------------------
4306         --exclusive tax or inlcusive tax without project info,  ln_lines_to_insert := 1;
4307         --inclusive recoverable tax with project info,  ln_lines_to_insert := 2;
4308         --PR(partially recoverable) tax is processed in another logic
4309 
4310         IF ( NVL(tax_lines1_rec.inc_tax_flag,'N') = 'N'
4311              OR ( NVL(tax_lines1_rec.inc_tax_flag,'N') = 'Y'
4312                   AND p_project_id IS NULL
4313                 )
4314            )
4315         THEN
4316           ln_lines_to_insert := 1;
4317         ELSIF ( NVL(tax_lines1_rec.inc_tax_flag,'N') = 'Y'
4318                 AND p_project_id IS NOT NULL
4319                 AND lv_tax_type = 'RE'
4320               )
4321         THEN
4322           ln_lines_to_insert := 2;
4323         END IF;--( NVL(tax_lines1_recinc_tax_flag,'N') = 'N')
4324         -------------------------------------------------------------------------------------
4325         --added by Eric for inclusive tax on 20-dec-2007,end
4326 
4327         Fnd_File.put_line(Fnd_File.LOG, 'tax_lines1_rec.modvat_flag ='||tax_lines1_rec.modvat_flag ||',c_tax_rec.mod_cr_percentage='||c_tax_rec.mod_cr_percentage);
4328 
4329         if tax_lines1_rec.modvat_flag = jai_constants.YES
4330         and nvl(c_tax_rec.mod_cr_percentage, -1) < 100
4331         and nvl(c_tax_rec.mod_cr_percentage, -1) > 0
4332         then
4333           --
4334           -- Tax line is for partial Recoverable tax.  Hence split amount into two parts, Recoverable and Non-Recoverable
4335           -- and instead of one line, two lines needs to be inserted.
4336           -- For ordinary lines (with modvat_flag = 'N' or with mod_cr_percentage = 100) there will be only one line inserted
4337           --
4338           ln_lines_to_insert := 2 *ln_lines_to_insert; --changed by eric for inclusive tax on Jan 4,2008
4339       ln_rec_tax_amt  :=  round ( nvl(v_tax_amount,0) * (c_tax_rec.mod_cr_percentage/100) , c_tax_rec.rounding_factor) ;
4340           ln_nrec_tax_amt :=  round ( nvl(v_tax_amount,0) - nvl(ln_rec_tax_amt,0) , c_tax_rec.rounding_factor) ;
4341       /* Added Tax rounding based on tax rounding factor for bug 6761425 by vumaasha */
4342           lv_tax_type := 'PR';  --changed by eric for inclusive tax on Jan 4,2008
4343         end if;
4344         fnd_file.put_line(fnd_file.log, 'ln_lines_to_insert='||ln_lines_to_insert||
4345                                         ',ln_rec_tax_amt='||ln_rec_tax_amt          ||
4346                                         ',ln_nrec_tax_amt='||ln_nrec_tax_amt
4347                          );
4348 
4349         --
4350         --  If a line has a partially recoverable tax the following loop will be executed twice.  First line will always be for a
4351         --  non recoverable tax amount and the second line will be for a recoverable tax amount
4352         --  For ordinary lines (with modvat_flag = 'N' or with mod_cr_percentage = 100 fully recoverable) the variable
4353         --  ln_lines_to_insert will have value of 1 and hence only one line will be inserted with full tax amount
4354         --
4355 
4356         for line in 1..ln_lines_to_insert
4357         loop
4358           --commented out by eric for inclusive tax on 20-dec-2007,begin
4359           /*
4360           if line = 1 then
4361 
4362             v_tax_amount  := nvl(ln_rec_tax_amt, v_tax_amount);
4363             lv_modvat_flag := tax_lines1_rec.modvat_flag ;
4364 
4365           elsif line = 2 then
4366 
4367             v_tax_amount := ln_nrec_tax_amt;
4368 
4369             if for_dist_insertion_rec.assets_tracking_flag = jai_constants.YES then
4370               v_assets_tracking_flag := jai_constants.YES;
4371             end if;
4372 
4373             lv_modvat_flag := jai_constants.NO ;
4374 
4375             --
4376             -- This is a non recoverable line hence the tax amounts should be added into the project costs by populating
4377             -- projects related columns so that PROJECTS can consider this line for Project Costing
4378             --
4379             IF nvl(lv_accrue_on_receipt_flag,'#') <> 'Y' THEN -- Bug 6338371
4380               ln_project_id           := p_project_id;
4381               ln_task_id              := p_task_id;
4382               lv_exp_type             := p_expenditure_type;
4383               ld_exp_item_date        := p_expenditure_item_date;
4384               ln_exp_organization_id  := p_expenditure_organization_id;
4385               lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
4386               lv_pa_addition_flag         := for_dist_insertion_rec.pa_addition_flag;
4387             END if;
4388             -- For non recoverable line charge account should be same as of the parent line
4389             v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id ;
4390 
4391           end if;
4392           */
4393           --commented out by eric for inclusive tax on 20-dec-2007,end
4394 
4395           --added by Eric for inclusive tax on 20-dec-2007,begin
4396           -------------------------------------------------------------------------------------
4397           IF (NVL(tax_lines1_rec.inc_tax_flag,'N') = 'N')--exclusive case
4398           THEN
4399             IF line = 1 then
4400 
4401               v_tax_amount  := nvl(ln_rec_tax_amt, v_tax_amount);
4402               lv_tax_line_amount:=  v_tax_amount       ;   --added by eric for inclusive tax
4403               lv_modvat_flag := tax_lines1_rec.modvat_flag ;
4404 
4405               lv_ap_line_to_inst_flag  := 'Y'; --added by eric for inclusive tax
4406               lv_tax_line_to_inst_flag := 'Y'; --added by eric for inclusive tax
4407             ELSIF line = 2 then
4408 
4409               v_tax_amount             := ln_nrec_tax_amt;
4410               lv_tax_line_amount       :=  v_tax_amount  ; --added by eric for inclusive tax
4411               lv_ap_line_to_inst_flag  := 'Y';             --added by eric for inclusive tax
4412               lv_tax_line_to_inst_flag := 'Y';             --added by eric for inclusive tax
4413 
4414 
4415               IF for_dist_insertion_rec.assets_tracking_flag = jai_constants.YES THEN
4416                 v_assets_tracking_flag := jai_constants.YES;
4417               END IF;
4418 
4419               lv_modvat_flag := jai_constants.NO ;
4420 
4421               --
4422               -- This is a non recoverable line hence the tax amounts should be added into the project costs by populating
4423               -- projects related columns so that PROJECTS can consider this line for Project Costing
4424               --
4425               IF nvl(lv_accrue_on_receipt_flag,'#') <> 'Y' THEN -- Bug 6338371
4426                 ln_project_id           := p_project_id;
4427                 ln_task_id              := p_task_id;
4428                 lv_exp_type             := p_expenditure_type;
4429                 ld_exp_item_date        := p_expenditure_item_date;
4430                 ln_exp_organization_id  := p_expenditure_organization_id;
4431                 lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
4432                 lv_pa_addition_flag         := for_dist_insertion_rec.pa_addition_flag;
4433               END if;
4434 
4435               -- For non recoverable line charge account should be same as of the parent line
4436               v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id;
4437 
4438             END IF; --line = 1
4439           ELSIF (NVL(tax_lines1_rec.inc_tax_flag,'N') = 'Y')       --inclusive case
4440           THEN
4441             IF( lv_tax_type ='PR')
4442             THEN
4443               IF ( line = 1 )
4444               THEN
4445                 --recoverable part
4446                 v_tax_amount       := ln_rec_tax_amt ;
4447                 lv_tax_line_amount := v_tax_amount   ;   --added by eric for inclusive tax
4448                 lv_modvat_flag     := tax_lines1_rec.modvat_flag  ;
4449                 lv_ap_line_to_inst_flag  := 'N';         --added by eric for inclusive tax
4450                 lv_tax_line_to_inst_flag := 'Y';         --added by eric for inclusive tax
4451 
4452                 --non recoverable part
4453               ELSIF ( line = 2 )
4454               THEN
4455                 v_tax_amount       := ln_nrec_tax_amt  ;
4456                 lv_tax_line_amount :=  v_tax_amount    ;   --added by eric for inclusive tax
4457                 lv_modvat_flag     := jai_constants.NO ;
4458                 lv_ap_line_to_inst_flag  := 'N';           --added by eric for inclusive tax
4459                 lv_tax_line_to_inst_flag := 'Y';           --added by eric for inclusive tax
4460 
4461                 --recoverable part without project infor
4462               ELSIF ( line = 3 )
4463               THEN
4464                 v_tax_amount                  := ln_rec_tax_amt;
4465                 lv_tax_line_amount            := NULL;
4466 
4467                 lv_ap_line_to_inst_flag  := 'Y';             --added by eric for inclusive tax
4468                 lv_tax_line_to_inst_flag := 'N';             --added by eric for inclusive tax
4469 
4470                 --Project information
4471                 ln_project_id                 := NULL;
4472                 ln_task_id                    := NULL;
4473                 lv_exp_type                   := NULL;
4474                 ld_exp_item_date              := NULL;
4475                 ln_exp_organization_id        := NULL;
4476                 lv_project_accounting_context := NULL;
4477                 lv_pa_addition_flag           := NULL;
4478 
4479                 -- For inclusive recoverable line charge account should be same as of the parent line
4480                 v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id ;
4481 
4482                 --recoverable part in negative amount with project infor
4483               ELSIF ( line = 4 )
4484               THEN
4485                 v_tax_amount                  := NVL(ln_rec_tax_amt, v_tax_amount)* -1;
4486                 lv_tax_line_amount            := NULL;
4487                 lv_ap_line_to_inst_flag  := 'Y';             --added by eric for inclusive tax
4488                 lv_tax_line_to_inst_flag := 'N';             --added by eric for inclusive tax
4489 
4490                 --Project information
4491                 ln_project_id                 := p_project_id;
4492                 ln_task_id                    := p_task_id;
4493                 lv_exp_type                   := p_expenditure_type;
4494                 ld_exp_item_date              := p_expenditure_item_date;
4495                 ln_exp_organization_id        := p_expenditure_organization_id;
4496                 lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
4497                 lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
4498 
4499                 -- For inclusive recoverable line charge account should be same as of the parent line
4500                 v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id ;
4501               END IF; --line = 1
4502             ELSIF ( lv_tax_type = 'RE'
4503                     AND p_project_id IS NOT NULL
4504                   )
4505             THEN
4506               --recoverable tax without project infor
4507               IF ( line = 1 )
4508               THEN
4509                 v_tax_amount       :=  v_tax_amount  ;
4510                 lv_tax_line_amount :=  v_tax_amount  ;    --added by eric for inclusive tax
4511                 lv_modvat_flag     :=  tax_lines1_rec.modvat_flag ;
4512                 lv_ap_line_to_inst_flag  := 'Y';          --added by eric for inclusive tax
4513                 lv_tax_line_to_inst_flag := 'Y';          --added by eric for inclusive tax
4514 
4515                 ln_project_id                 := NULL;
4516                 ln_task_id                    := NULL;
4517                 lv_exp_type                   := NULL;
4518                 ld_exp_item_date              := NULL;
4519                 ln_exp_organization_id        := NULL;
4520                 lv_project_accounting_context := NULL;
4521                 lv_pa_addition_flag           := NULL;
4522                 v_dist_code_combination_id    := for_dist_insertion_rec.dist_code_combination_id ;
4523               --recoverable tax in negative amount with project infor
4524               ELSIF ( line = 2 )
4525               THEN
4526                 v_tax_amount       :=  v_tax_amount * -1;
4527                 lv_tax_line_amount :=  NULL ;   --added by eric for inclusive tax
4528                 lv_modvat_flag     :=  tax_lines1_rec.modvat_flag  ;
4529 
4530                 ln_project_id                 := p_project_id;
4531                 ln_task_id                    := p_task_id;
4532                 lv_exp_type                   := p_expenditure_type;
4533                 ld_exp_item_date              := p_expenditure_item_date;
4534                 ln_exp_organization_id        := p_expenditure_organization_id;
4535                 lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
4536                 lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
4537                 v_dist_code_combination_id    := for_dist_insertion_rec.dist_code_combination_id ;
4538               END IF;
4539             --  ELSIF ( lv_tax_type <> 'PR' AND p_project_id IS NULL )
4540             --  THEN
4541             ELSE -- eric removed the above criteria for bug 6888665 and 6888209
4542               Fnd_File.put_line(Fnd_File.LOG, 'NOT Inclusive PR Tax,NOT Inclusive RE for project ');
4543               --The case process the inclusive NR tax and inclusive RE tax not for project
4544 
4545               lv_tax_line_amount := v_tax_amount   ;   --added by eric for inclusive tax
4546               lv_modvat_flag     := tax_lines1_rec.modvat_flag  ;
4547               lv_ap_line_to_inst_flag  := 'N';         --added by eric for inclusive tax
4548               lv_tax_line_to_inst_flag := 'Y';         --added by eric for inclusive tax
4549 
4550             END IF;--( tax_type ='PR' and (ln_lines_to_insert =4 OR )
4551           END IF; --(NVL(tax_lines1_rec.inc_tax_flag,'N') = 'N')
4552           -------------------------------------------------------------------------------------------
4553           --added by Eric for inclusive tax on 20-dec-2007,end
4554 
4555 
4556 
4557 
4558           fnd_file.put_line(fnd_file.log, 'line='||line||', v_tax_amount='|| v_tax_amount
4559                                           ||'lv_modvat_flag='||lv_modvat_flag
4560                                           ||'v_assets_tracking_flag='||v_assets_tracking_flag
4561                               );
4562 
4563           --ln_inv_line_num := ln_inv_line_num + 1;commented out by eric for inclusive tax
4564           --
4565           -- End 5763527
4566           --
4567 
4568           Fnd_File.put_line(Fnd_File.LOG,
4569           'Before inserting into ap_invoice_lines_all for line no :'|| ln_inv_line_num  );
4570 
4571 
4572           --insert exclusive tax to the ap tables
4573           --or insert recoverable inclusive tax with project information the ap tables
4574 
4575 
4576           --1.Only exlusive taxes or inclusive taxes with project information
4577           --  are  inserted into Ap Lines and Dist Lines
4578 
4579           --2.All taxes need to be inserted into jai tax tables. Futher the
4580           --  partially recoverable tax need to be splitted into 2 lines
4581 
4582           --added by Eric for inclusive tax on 20-dec-2007,begin
4583           ------------------------------------------------------------------------
4584           IF ( NVL(lv_ap_line_to_inst_flag,'N')='Y')
4585           THEN
4586             ln_inv_line_num := ln_inv_line_num + 1; --added by eric for inlcusive tax
4587           ---------------------------------------------------------------------------
4588           --added by Eric for inclusive tax on 20-dec-2007,end
4589 
4590             INSERT INTO ap_invoice_lines_all
4591             (
4592               INVOICE_ID
4593             , LINE_NUMBER
4594             , LINE_TYPE_LOOKUP_CODE
4595             , DESCRIPTION
4596             , ORG_ID
4597             , MATCH_TYPE
4598       , DEFAULT_DIST_CCID --Changes by nprashar for bug #6995437
4599             , ACCOUNTING_DATE
4600             , PERIOD_NAME
4601             , DEFERRED_ACCTG_FLAG
4602             , DEF_ACCTG_START_DATE
4603             , DEF_ACCTG_END_DATE
4604             , DEF_ACCTG_NUMBER_OF_PERIODS
4605             , DEF_ACCTG_PERIOD_TYPE
4606             , SET_OF_BOOKS_ID
4607             , AMOUNT
4608             , WFAPPROVAL_STATUS
4609             , CREATION_DATE
4610             , CREATED_BY
4611             , LAST_UPDATED_BY
4612             , LAST_UPDATE_DATE
4613             , LAST_UPDATE_LOGIN
4614               /* 5763527 */
4615             , project_id
4616             , task_id
4617             , expenditure_type
4618             , expenditure_item_date
4619             , expenditure_organization_id
4620             /* End 5763527 */
4621             ,po_distribution_id --Added for bug#6780154
4622 
4623             )
4624             VALUES
4625             (
4626               inv_id
4627             , ln_inv_line_num
4628             , lv_misc
4629             , c_tax_rec.tax_name
4630             , v_org_id
4631             , NULL
4632       ,v_dist_code_combination_id
4633             , rec_max_ap_lines_all.accounting_date
4634             , rec_max_ap_lines_all.period_name
4635             , rec_max_ap_lines_all.deferred_acctg_flag
4636             , rec_max_ap_lines_all.def_acctg_start_date
4637             , rec_max_ap_lines_all.def_acctg_end_date
4638             , rec_max_ap_lines_all.def_acctg_number_of_periods
4639             , rec_max_ap_lines_all.def_acctg_period_type
4640             , rec_max_ap_lines_all.set_of_books_id
4641             , ROUND(ROUND(v_tax_amount,ln_tax_precision),ln_precision) --Added by nprashar for bug # 13653889
4642             , rec_max_ap_lines_all.wfapproval_status -- Bug 4863208
4643             , sysdate
4644             , ln_user_id
4645             , ln_user_id
4646             , sysdate
4647             , ln_login_id
4648               /* 5763527 */
4649               , ln_project_id
4650               , ln_task_id
4651               , lv_exp_type
4652               , ld_exp_item_date
4653               , ln_exp_organization_id
4654               /* End 5763527 */
4655            ,po_dist_id  --Added for bug#6780154
4656             );
4657 
4658             Fnd_File.put_line(Fnd_File.LOG,
4659             'Before inserting into ap_invoice_distributions_all for distribution line no : '
4660             ||v_distribution_no);
4661 
4662             -- Start bug#3332988
4663             open c_get_invoice_distribution;
4664             fetch c_get_invoice_distribution into v_invoice_distribution_id;
4665             close c_get_invoice_distribution;
4666             -- End bug#3332988
4667 
4668             fnd_file.put_line(FND_FILE.LOG, ' Invoice distribution id '|| v_invoice_distribution_id);
4669 
4670             INSERT INTO ap_invoice_distributions_all
4671             (
4672             accounting_date,
4673             accrual_posted_flag,
4674             assets_addition_flag,
4675             assets_tracking_flag,
4676             cash_posted_flag,
4677             distribution_line_number,
4678             dist_code_combination_id,
4679             invoice_id,
4680             last_updated_by,
4681             last_update_date,
4682             line_type_lookup_code,
4683             period_name,
4684             set_of_books_id,
4685             amount,
4686             base_amount,
4687             batch_id,
4688             created_by,
4689             creation_date,
4690             description,
4691             exchange_rate_variance,
4692             last_update_login,
4693             match_status_flag,
4694             posted_flag,
4695             rate_var_code_combination_id,
4696             reversal_flag,
4697             program_application_id,
4698             program_id,
4699             program_update_date,
4700             accts_pay_code_combination_id,
4701             invoice_distribution_id,
4702             quantity_invoiced,
4703             po_distribution_id ,
4704             rcv_transaction_id,
4705             matched_uom_lookup_code
4706             ,INVOICE_LINE_NUMBER
4707             ,org_id -- Bug 4863208
4708             ,charge_applicable_to_dist_id -- Bug 5401111. Added by Lakshmi Gopalsami
4709             /* 5763527 */
4710             , project_id
4711             , task_id
4712             , expenditure_type
4713             , expenditure_item_date
4714             , expenditure_organization_id
4715             , project_accounting_context
4716             , pa_addition_flag
4717             /* End 5763527 */
4718            -- Bug 7249100. Added by Lakshmi Gopalsami
4719             ,distribution_class
4720               , dist_match_type --Added by nprashar for bug # 12832424
4721             )
4722             VALUES
4723             (
4724             rec_max_ap_lines_all.accounting_date,
4725             'N', --for_dist_insertion_rec.accrual_posted_flag,
4726             for_dist_insertion_rec.assets_addition_flag,
4727             v_assets_tracking_flag, -- for_dist_insertion_rec.assets_tracking_flag, bug # 2851123
4728             'N',
4729             v_distribution_no,
4730             /* Bug 5358788. Added by Lakshmi Gopalsami. Commented
4731                    * for_dist_insertion_rec.dist_code_combination_id
4732              * and v_dist_code_combination_id
4733              */
4734             v_dist_code_combination_id,
4735             inv_id,
4736             ln_user_id,
4737             sysdate,
4738             lv_misc,
4739             rec_max_ap_lines_all.period_name,
4740             rec_max_ap_lines_all.set_of_books_id,
4741             ROUND(round(v_tax_amount,ln_tax_precision),ln_precision), --Added by nprashar for bug # 13653889
4742             ROUND(ROUND(v_tax_amount * for_dist_insertion_rec.exchange_rate, ln_tax_precision),ln_precision), --Added by nprashar for bug # 13653889
4743             v_batch_id,
4744             ln_user_id,
4745             sysdate,
4746             c_tax_rec.tax_name,
4747             null,--kunkumar for forward porting to R12
4748             ln_login_id,
4749             for_dist_insertion_rec.match_status_flag , -- Bug 4863208
4750             'N',
4751             NULL,
4752             -- 'N',
4753             for_dist_insertion_rec.reversal_flag,  -- Bug 4863208
4754             for_dist_insertion_rec.program_application_id,
4755             for_dist_insertion_rec.program_id,
4756             for_dist_insertion_rec.program_update_date,
4757             for_dist_insertion_rec.accts_pay_code_combination_id,
4758             v_invoice_distribution_id,
4759             decode(v_assets_tracking_flag, 'Y', NULL, 0), /* Modified for bug 8406404 by vumaasha */
4760              --po_dist_id,12434160
4761 			for_dist_insertion_rec.po_distribution_id, --12434160
4762             rcv_tran_id,
4763             for_dist_insertion_rec.matched_uom_lookup_code
4764             ,ln_inv_line_num
4765             ,for_dist_insertion_rec.org_id -- Bug 4863208
4766             -- Bug 5401111. Added by Lakshmi Gopalsami
4767             ,decode(v_assets_tracking_flag,'N',
4768                     NULL,for_dist_insertion_rec.invoice_distribution_id)
4769 /*Commented the account_type condition for bug#7008161 by JMEENA
4770        decode(lv_account_type, 'A',
4771               for_dist_insertion_rec.invoice_distribution_id,NULL)
4772             )
4773 */
4774             /* 5763527 */
4775             , ln_project_id
4776             , ln_task_id
4777             , lv_exp_type
4778             , ld_exp_item_date
4779             , ln_exp_organization_id
4780             , lv_project_accounting_context
4781             , lv_pa_addition_flag
4782             -- Bug 7249100. Added by Lakshmi Gopalsami
4783             ,lv_dist_class
4784             , for_dist_insertion_rec.dist_match_type --Added by nprashar for bug # 12832424
4785             );
4786 
4787           --added by Eric for inclusive tax on 20-dec-2007,begin
4788           ------------------------------------------------------
4789           END IF; --( NVL(lv_ap_line_to_inst_flag,'N')='Y')
4790           ------------------------------------------------------
4791           --added by Eric for inclusive tax on 20-dec-2007,end
4792 
4793         Fnd_File.put_line(Fnd_File.LOG, 'Before inserting into JAI_AP_MATCH_INV_TAXES ');
4794         INSERT INTO JAI_AP_MATCH_INV_TAXES
4795         (
4796         tax_distribution_id,
4797         exchange_rate_variance,
4798         assets_tracking_flag,
4799         invoice_id,
4800         po_header_id,
4801         po_line_id,
4802         line_location_id,
4803         set_of_books_id,
4804         --org_id,
4805         exchange_rate,
4806         exchange_rate_type,
4807         exchange_date,
4808         currency_code,
4809         code_combination_id,
4810         last_update_login,
4811         creation_date,
4812         created_by,
4813         last_update_date,
4814         last_updated_by,
4815         acct_pay_code_combination_id,
4816         accounting_date,
4817         tax_id,
4818         tax_amount,
4819         base_amount,
4820         chart_of_accounts_id,
4821         distribution_line_number,
4822         --project_id,
4823         --task_id,
4824         po_distribution_id,  -- added  by bug#3038566
4825         parent_invoice_distribution_id,  -- added  by bug#3038566
4826         legal_entity_id -- added by rallamse bug#
4827         -- Added by Brathod, Bug# 4445989
4828         ,invoice_line_number
4829         ,INVOICE_DISTRIBUTION_ID
4830         ,PARENT_INVOICE_LINE_NUMBER
4831         ,RCV_TRANSACTION_ID
4832         ,LINE_TYPE_LOOKUP_CODE
4833         -- End Bug# 4445989
4834         ,recoverable_flag -- 5763527
4835         ,line_no          -- Bug 5553150, 5593895
4836         )
4837         VALUES
4838         (
4839         JAI_AP_MATCH_INV_TAXES_S.NEXTVAL,
4840        null,--kunkumar for forward porting to R12
4841         v_assets_tracking_flag, -- 'N', bug # 2851123
4842         inv_id,
4843         cur_items_rec.po_header_id,
4844         cur_items_rec.po_line_id,
4845         cur_items_rec.line_location_id,
4846         cur_items_rec.set_of_books_id,
4847         --cur_items_rec.org_id,
4848         cur_items_rec.rate,
4849         cur_items_rec.rate_type,
4850         cur_items_rec.rate_date,
4851         cur_items_rec.currency_code,
4852   /* Bug 5358788. Added by Lakshmi Gopalsami. Commented
4853          * c_tax_rec.tax_account_id and v_dist_code_combination_id
4854    */
4855         v_dist_code_combination_id,
4856         cur_items_rec.last_update_login,
4857         cur_items_rec.creation_date,
4858         cur_items_rec.created_by,
4859         cur_items_rec.last_update_date,
4860         cur_items_rec.last_updated_by,
4861         apccid,
4862         cur_items_rec.invoice_date,
4863         tax_lines1_rec.tax_id,
4864         /*commented out by eric for inclusive tax
4865         round(v_tax_amount,ln_precision), -- ROUND(v_tax_amount, 2), by Aparajita bug#2567799
4866         */
4867         ROUND(ROUND(lv_tax_line_amount,ln_tax_precision),ln_precision),  --Added by nprashar for bug # 13653889
4868         ROUND(ROUND(tax_lines1_rec.tax_amount, ln_tax_precision),ln_precision), --Added by nprashar for bug # 13653889
4869         caid,
4870         v_distribution_no,
4871         --p_project_id,
4872         --p_task_id ,
4873         po_dist_id, -- added  by bug#3038566
4874         for_dist_insertion_rec.invoice_distribution_id, -- added  by bug#3038566
4875         get_ven_info_rec.legal_entity_id, -- added by rallamse bug#
4876         --, ln_inv_line_num , modified by eric for inclusvice tax
4877         --, v_invoice_distribution_id , modified by eric for inclusvice tax
4878         --added by eric for inclusvice tax on 20-dec-2007,begin
4879         --------------------------------------------------------------------------
4880         DECODE ( NVL(tax_lines1_rec.inc_tax_flag,'N')
4881                , 'N',ln_inv_line_num
4882                , 'Y',pn_invoice_line_number
4883                )
4884         , NVL(v_invoice_distribution_id,for_dist_insertion_rec.invoice_distribution_id)
4885         --------------------------------------------------------------------------
4886         --added by eric for inclusvice tax on 20-dec-2007,end
4887         , pn_invoice_line_number
4888        , rcv_tran_id
4889        , lv_misc
4890        , lv_modvat_flag -- 5763527
4891        , tax_lines1_rec.tax_line_no  -- Bug 5553150, 5593895
4892 
4893         );
4894 
4895         /*  Bug 46863208. Added by Lakshmi Gopalsami
4896          Commented the MRC call
4897         insert_mrc_data(v_invoice_distribution_id); -- bug#3332988
4898        */
4899        /* commented out by for bug 8345512 on 13-Jul-2009
4900           cum_tax_amt := cum_tax_amt + round(v_tax_amount, ln_precision);
4901        */
4902 
4903         --Modified by Eric for bug 8345512 on 13-Jul-2009,begin
4904         ------------------------------------------------------------------
4905         IF NVL(tax_lines1_rec.inc_tax_flag,'N') ='N'
4906         THEN
4907           cum_tax_amt := cum_tax_amt + round(round(v_tax_amount,ln_tax_precision), ln_precision); -- ROUND(v_tax_amount, 2),bug#2567799 /* Bug 14386866 */
4908         END IF;
4909         ------------------------------------------------------------------
4910         --Modified by Eric for bug 8345512 on 13-Jul-2009,end
4911 
4912 
4913         /* Obsoleted as part of R12 , Bug# 4445989
4914         Fnd_File.put_line(Fnd_File.LOG, 'Before inserting into JAI_CMN_FA_INV_DIST_ALL ');
4915 
4916         INSERT INTO JAI_CMN_FA_INV_DIST_ALL
4917         (
4918         invoice_id,
4919         invoice_distribution_id,
4920         set_of_books_id,
4921         batch_id,
4922         po_distribution_id,
4923         rcv_transaction_id,
4924         dist_code_combination_id,
4925         accounting_date,
4926         assets_addition_flag,
4927         assets_tracking_flag,
4928         distribution_line_number,
4929         line_type_lookup_code,
4930         amount,
4931         description,
4932         match_status_flag,
4933         quantity_invoiced
4934         )
4935         VALUES
4936         (
4937         inv_id,
4938         ap_invoice_distributions_s.CURRVAL,
4939         for_dist_insertion_rec.set_of_books_id,
4940         v_batch_id,
4941         for_dist_insertion_rec.po_distribution_id,
4942         rcv_tran_id,
4943         for_dist_insertion_rec.dist_code_combination_id,
4944         for_dist_insertion_rec.accounting_date,
4945         for_dist_insertion_rec.assets_addition_flag,
4946         v_assets_tracking_flag, -- for_dist_insertion_rec.assets_tracking_flag, bug # 2851123
4947         v_distribution_no,
4948         'MISCELLANEOUS',
4949         round(v_tax_amount,ln_precision), -- ROUND(v_tax_amount, 2), by Aparajita bug#2567799,
4950         c_tax_rec.tax_name,
4951         NULL,
4952         NULL
4953         );
4954         */
4955         end loop ;--> for line in 1 to ln_lines_to_insert Brathod, 5763527
4956 
4957       END LOOP; --tax_lines1_rec IN tax_lines1_cur(rcv_tran_id,for_org_id_rec.vendor_id)
4958 
4959 
4960       v_update_payment_schedule:=update_payment_schedule(cum_tax_amt); -- bug#3218978
4961 
4962 
4963       UPDATE ap_invoices_all
4964       SET   invoice_amount       =  invoice_amount   + cum_tax_amt,
4965         approved_amount      =  approved_amount  + cum_tax_amt,
4966         pay_curr_invoice_amount =  pay_curr_invoice_amount + cum_tax_amt,
4967         amount_applicable_to_discount =  amount_applicable_to_discount + cum_tax_amt,
4968         payment_status_flag = decode(payment_status_flag, 'Y', 'P', payment_status_flag)
4969         -- bug#3624898
4970       WHERE  invoice_id = inv_id;
4971 
4972       -- start added for bug#3354932
4973       if for_org_id_rec.invoice_currency_code <> v_functional_currency then
4974         -- invoice currency is not the functional currency.
4975         update ap_invoices_all
4976         set    base_amount = invoice_amount  * exchange_rate
4977         where  invoice_id = inv_id;
4978       end if;
4979       -- end added for bug#3354932
4980 
4981      /* Bug 4863208. Added by Lakshmi Gopalsami
4982          Commented the MRC call
4983      update_mrc_data; -- bug#3332988
4984      */
4985 
4986     ELSIF p_receipt_code = 'PACKING_SLIP' THEN
4987 
4988       Fnd_File.put_line(Fnd_File.LOG, 'inside  p_receipt_code = PACKING_SLIP');
4989 
4990       /* Begin 5763527 */
4991       fnd_file.put_line(fnd_file.log, 'rcv_tran_id='||rcv_tran_id);
4992        open  c_get_excise_costing_flag (cp_rcv_transaction_id => rcv_tran_id
4993                                     ,cp_organization_id =>  r_rcv_transactions.organization_id    --Added by Bgowrava for Bug#7503308
4994                                       ,cp_shipment_header_id   =>  p_shipment_header_id                    --Added by Bgowrava for Bug#7503308
4995                     ,cp_txn_type        =>  'DELIVER'                             --Added by Bgowrava for Bug#7503308
4996                                       ,cp_attribute1      =>  'CENVAT_COSTED_FLAG');                --Added by Bgowrava for Bug#7503308
4997       fetch c_get_excise_costing_flag into lv_excise_costing_flag;
4998       close c_get_excise_costing_flag ;
4999       /* End 5763527 */
5000 
5001 
5002       FOR tax_lines1_rec IN tax_lines1_cur(rcv_tran_id,for_org_id_rec.vendor_id)  LOOP
5003 
5004         -- Added By Brathod, Bug# 44459989
5005         /* v_distribution_no := v_distribution_no + 1 ; */
5006         -- ln_inv_line_num := ln_inv_line_num + 1; -- 5763527, moved increment to just before insert statment
5007         v_distribution_no  := 1;
5008         -- End Bug# 4445989
5009 
5010         r_service_regime_tax_type := null;
5011 
5012         -- Bug 5358788. Added by Lakshmi Gopalsami
5013         r_VAT_regime_tax_type := null;
5014 
5015         /* 5763527 */
5016         ln_project_id           := null;
5017         ln_task_id              := null;
5018         lv_exp_type             := null;
5019         ld_exp_item_date        := null;
5020         ln_exp_organization_id  := null;
5021         lv_project_accounting_context := null;
5022         lv_pa_addition_flag := null;
5023 
5024 
5025         OPEN  c_tax(tax_lines1_rec.tax_id);
5026         FETCH c_tax INTO c_tax_rec;
5027         CLOSE c_tax;
5028 
5029 		--Added by nprashar for bug # 13653889
5030 	  OPEN c_tax_curr_prec(tax_lines1_rec.tax_id) ;
5031       FETCH c_tax_curr_prec INTO ln_tax_precision ;
5032       CLOSE c_tax_curr_prec ;
5033 
5034        -- added, kunkumar for bug 5593895
5035         ln_base_amount := null ;
5036         if tax_lines1_rec.tax_type IN ( jai_constants.tax_type_value_added, jai_constants.tax_type_cst )  then
5037           ln_base_amount := jai_ap_utils_pkg.fetch_tax_target_amt
5038                             (
5039                               p_invoice_id          =>   inv_id ,
5040                               p_line_location_id    =>   null ,
5041                               p_transaction_id      =>   rcv_tran_id  ,
5042                               p_parent_dist_id      =>   for_dist_insertion_rec.invoice_distribution_id ,
5043                               p_tax_id              =>   tax_lines1_rec.tax_id
5044                             ) ;
5045         end if ;
5046         -- ended,  kunkumar for bug 5593895
5047 
5048 
5049         /* Service Start */
5050         v_assets_tracking_flag := for_dist_insertion_rec.assets_tracking_flag;
5051         v_dist_code_combination_id := null;
5052 
5053         --initial the tax_type
5054         lv_tax_type := NULL;--added by eric for inclusive tax on 20-Dec,2007
5055 
5056         -- 5763527, Brathod
5057         -- Modified the if condition to consider partial recoverable lines previous logic was hadling only when it is 100
5058         if tax_lines1_rec.modvat_flag = jai_constants.YES
5059         and nvl(c_tax_rec.mod_cr_percentage, -1) > 0
5060         then
5061           --recoverable tax
5062           lv_tax_type := 'RE'; --added by eric for inclusive tax on 20-dec,2007
5063 
5064 
5065          /*  recoverable tax */
5066           v_assets_tracking_flag := 'N';
5067 
5068           open c_regime_tax_type(r_jai_regimes.regime_id, c_tax_rec.tax_type);
5069           fetch c_regime_tax_type into r_service_regime_tax_type;
5070           close c_regime_tax_type;
5071 
5072     fnd_file.put_line(FND_FILE.LOG,
5073                    ' Service regime: '||r_service_regime_tax_type.tax_type);
5074 
5075      /* Bug 5358788. Added by Lakshmi Gopalsami
5076     * Fetched the details of VAT regime
5077     */
5078 
5079          OPEN  c_regime_tax_type(r_vat_regimes.regime_id, c_tax_rec.tax_type);
5080           FETCH  c_regime_tax_type INTO  r_vat_regime_tax_type;
5081          CLOSE  c_regime_tax_type;
5082 
5083          fnd_file.put_line(FND_FILE.LOG,
5084                    ' VAT regime: '||r_vat_regime_tax_type.tax_type);
5085 
5086           if r_service_regime_tax_type.tax_type is not null then
5087             /* Service type of tax */
5088             v_dist_code_combination_id := check_service_interim_account
5089                                           (
5090                                             lv_accrue_on_receipt_flag,
5091                                             lv_accounting_method_option,
5092                                             lv_is_item_an_expense,
5093                                             r_jai_regimes.regime_id,
5094                                             v_org_id,
5095                                             p_rematch,
5096                                             c_tax_rec.tax_type,
5097               po_dist_id  --Added by JMEENA for bug#6833506
5098                                           );
5099 
5100              fnd_file.put_line(FND_FILE.LOG,
5101                  ' Regime type , CCID '
5102      || r_service_regime_tax_type.tax_type
5103            ||', ' || v_dist_code_combination_id);
5104 
5105             /* Bug 5358788. Added by Lakshmi Gopalsami
5106            * Commented p_rematch and added validation for
5107        * VAT regime.
5108        */
5109   /* following elsif block modified for bug 6595773*/
5110           ELSIF r_vat_regime_tax_type.tax_type IS NOT NULL THEN
5111            --p_rematch = 'PO_MATCHING' then
5112 
5113             v_dist_code_combination_id := for_dist_insertion_rec.dist_code_combination_id;
5114       fnd_file.put_line(FND_FILE.LOG,
5115                  ' Regime type , CCID '
5116      || r_vat_regime_tax_type.tax_type
5117            ||', ' || v_dist_code_combination_id);
5118 
5119           end if;
5120         else  /* 5763527 introduced for PROJETCS COSTING Impl */
5121           /* 5763527 */
5122           IF nvl(lv_accrue_on_receipt_flag,'#') <> 'Y' THEN -- Bug 6338371
5123             ln_project_id           := p_project_id;
5124             ln_task_id              := p_task_id;
5125             lv_exp_type             := p_expenditure_type;
5126             ld_exp_item_date        := p_expenditure_item_date;
5127             ln_exp_organization_id  := p_expenditure_organization_id;
5128             lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
5129             lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
5130           END if;
5131           --non recoverable
5132           lv_tax_type := 'NR';  --added by eric for inclusive tax on 20-dec,2007
5133 
5134         end if; /*nvl(c_tax_rec.mod_cr_percentage, 0) = 100  and c_tax_rec.tax_account_id is not null*/
5135 
5136         /* Bug#4177452*/
5137         if v_dist_code_combination_id is null then
5138           v_dist_code_combination_id := for_dist_insertion_rec.dist_code_combination_id;
5139         end if;
5140 
5141         /* Service End */
5142 
5143         /* Bug#5763527 */
5144         Fnd_File.put_line(Fnd_File.LOG, 'c_tax_rec.tax_type='||c_tax_rec.tax_type  ||',lv_excise_costing_flag='||lv_excise_costing_flag);
5145         if UPPER(c_tax_rec.tax_type) like  '%EXCISE%' then
5146           if lv_excise_costing_flag = 'Y' then
5147             IF  nvl(lv_accrue_on_receipt_flag,'#') <> 'Y' -- Bug 6338371
5148             then
5149               ln_project_id           := p_project_id;
5150               ln_task_id              := p_task_id;
5151               lv_exp_type             := p_expenditure_type;
5152               ld_exp_item_date        := p_expenditure_item_date;
5153               ln_exp_organization_id  := p_expenditure_organization_id;
5154               lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
5155               lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
5156             end if;
5157             v_dist_code_combination_id    := for_dist_insertion_rec.dist_code_combination_id;
5158           END if;
5159         end if;
5160         /* End# 5763527 */
5161 
5162 
5163         -- Start for bug#3752887
5164 
5165 
5166 
5167         if for_org_id_rec.invoice_currency_code <> tax_lines1_rec.currency then
5168           v_tax_amount := v_tax_amount / for_org_id_rec.exchange_rate;
5169         end if;
5170         -- End for bug#3752887
5171 
5172         -- Start bug#4103473
5173         if nvl(v_tax_amount,-1) = -1 then --Modified by kunkumar for bug#5593895
5174           v_tax_amount := apportion_tax_4_price_cor_inv(v_tax_amount,  tax_lines1_rec.tax_id);
5175         END IF;
5176         v_tax_amount := nvl(v_tax_amount,-1); --Modified by kunkumar for bug#5593895
5177         -- End bug#4103473
5178         --
5179         -- Begin 5763527
5180         --
5181         ln_rec_tax_amt  := null;
5182         ln_nrec_tax_amt := null;
5183         ln_lines_to_insert := 1; -- Loop controller to insert more than one lines for partially recoverable tax lines in PO
5184 
5185         --added by Eric for inclusive tax on 20-dec-2007,begin
5186         -------------------------------------------------------------------------------------
5187         --exclusive tax or inlcusive tax without project info,  ln_lines_to_insert := 1;
5188         --inclusive recoverable tax with project info,  ln_lines_to_insert := 2;
5189         --PR(partially recoverable) tax is processed in another logic
5190 
5191         IF ( NVL(tax_lines1_rec.inc_tax_flag,'N') = 'N'
5192              OR ( NVL(tax_lines1_rec.inc_tax_flag,'N') = 'Y'
5193                   AND p_project_id IS NULL
5194                 )
5195            )
5196         THEN
5197           ln_lines_to_insert := 1;
5198         ELSIF ( NVL(tax_lines1_rec.inc_tax_flag,'N') = 'Y'
5199                 AND p_project_id IS NOT NULL
5200                 AND lv_tax_type = 'RE'
5201               )
5202         THEN
5203           ln_lines_to_insert := 2;
5204         END IF;--( NVL(tax_lines1_rec.inc_tax_flag,'N') = 'N' OR )
5205         -------------------------------------------------------------------------------------
5206         --added by Eric for inclusive tax on 20-dec-2007,end
5207 
5208 
5209       Fnd_File.put_line(Fnd_File.LOG, 'tax_lines1_rec.modvat_flag ='||tax_lines1_rec.modvat_flag ||',c_tax_rec.mod_cr_percentage='||c_tax_rec.mod_cr_percentage);
5210 
5211       if tax_lines1_rec.modvat_flag = jai_constants.YES
5212       and nvl(c_tax_rec.mod_cr_percentage, -1) > 0
5213       and nvl(c_tax_rec.mod_cr_percentage, -1) < 100
5214       then
5215         --
5216         -- Tax line is for partial Recoverable tax.  Hence split amount into two parts, Recoverable and Non-Recoverable
5217         -- and instead of one line, two lines needs to be inserted.
5218         -- For ordinary lines (with modvat_flag = 'N' or with mod_cr_percentage = 100) there will be only one line inserted
5219         --
5220         ln_lines_to_insert := 2 *ln_lines_to_insert; --changed by eric for inclusive tax on Jan 4,2008
5221         ln_rec_tax_amt  :=  round ( nvl(v_tax_amount,0) * (c_tax_rec.mod_cr_percentage/100) , c_tax_rec.rounding_factor) ;
5222         ln_nrec_tax_amt :=  round ( nvl(v_tax_amount,0) - nvl(ln_rec_tax_amt,0) , c_tax_rec.rounding_factor) ;
5223     /* Added Tax rounding based on tax rounding factor for bug 6761425 by vumaasha */
5224 
5225          lv_tax_type := 'PR';  --changed by eric for inclusive tax on Jan 4,2008
5226 
5227       end if;
5228 
5229         --start additions for bug#11936596
5230   if tax_lines1_rec.modvat_flag = jai_constants.YES
5231   then
5232    ln_inclurec_tax_amt := round ( nvl(v_tax_amount,0) * (c_tax_rec.mod_cr_percentage/100) , c_tax_rec.rounding_factor) ;
5233   end if;
5234 --end additions for bug#11936596
5235 
5236 
5237       fnd_file.put_line(fnd_file.log, 'ln_lines_to_insert='||ln_lines_to_insert||
5238                                       ',ln_rec_tax_amt='||ln_rec_tax_amt          ||
5239                                       ',ln_nrec_tax_amt='||ln_nrec_tax_amt ||' ln_inclurec_tax_amt '||ln_inclurec_tax_amt
5240                        );
5241 
5242 
5243 
5244       --
5245       --  If a line has a partially recoverable tax the following loop will be executed twice.  First line will always be for a
5246       --  non recoverable tax amount and the second line will be for a recoverable tax amount
5247       --  For ordinary lines (with modvat_flag = 'N' or with mod_cr_percentage = 100 fully recoverable) the variable
5248       --  ln_lines_to_insert will have value of 1 and hence only one line will be inserted with full tax amount
5249       --
5250 
5251       for line in 1..ln_lines_to_insert
5252       loop
5253 
5254         --commented out by eric for inclusive tax
5255         /*
5256         if line = 1 then
5257 
5258           v_tax_amount  := nvl(ln_rec_tax_amt, v_tax_amount);
5259           lv_modvat_flag := tax_lines1_rec.modvat_flag ;
5260 
5261         elsif line = 2 then
5262 
5263           v_tax_amount := ln_nrec_tax_amt;
5264 
5265           if for_dist_insertion_rec.assets_tracking_flag = jai_constants.YES then
5266             v_assets_tracking_flag := jai_constants.YES;
5267           end if;
5268 
5269           lv_modvat_flag := jai_constants.NO ;
5270 
5271           --
5272           -- This is a non recoverable line hence the tax amounts should be added into the project costs by populating
5273           -- projects related columns so that PROJECTS can consider this line for Project Costing
5274           --
5275           IF nvl(lv_accrue_on_receipt_flag,'#') <> 'Y' THEN -- Bug 6338371
5276 
5277             ln_project_id           := p_project_id;
5278             ln_task_id              := p_task_id;
5279             lv_exp_type             := p_expenditure_type;
5280             ld_exp_item_date        := p_expenditure_item_date;
5281             ln_exp_organization_id  := p_expenditure_organization_id;
5282             lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
5283             lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
5284           END if;          -- For non recoverable line charge account should be same as of the parent line
5285           v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id ;
5286         end if;
5287         */
5288       --ln_inv_line_num := ln_inv_line_num + 1;commented out by eric for inclusive tax
5289       --
5290       -- End 5763527
5291       --
5292 
5293         --added by Eric for inclusive tax on 20-dec-2007,begin
5294         -------------------------------------------------------------------------------------
5295         IF (NVL(tax_lines1_rec.inc_tax_flag,'N') = 'N')--exclusive case
5296         THEN
5297           IF line = 1 then
5298 
5299             v_tax_amount  := nvl(ln_rec_tax_amt, v_tax_amount);
5300             lv_tax_line_amount:=  v_tax_amount       ;   --added by eric for inclusive tax
5301             lv_modvat_flag := tax_lines1_rec.modvat_flag ;
5302 
5303             lv_ap_line_to_inst_flag  := 'Y'; --added by eric for inclusive tax
5304             lv_tax_line_to_inst_flag := 'Y'; --added by eric for inclusive tax
5305           ELSIF line = 2 then
5306 
5307             v_tax_amount             := ln_nrec_tax_amt;
5308             lv_tax_line_amount       :=  v_tax_amount  ; --added by eric for inclusive tax
5309             lv_ap_line_to_inst_flag  := 'Y';             --added by eric for inclusive tax
5310             lv_tax_line_to_inst_flag := 'Y';             --added by eric for inclusive tax
5311 
5312 
5313             IF for_dist_insertion_rec.assets_tracking_flag = jai_constants.YES THEN
5314               v_assets_tracking_flag := jai_constants.YES;
5315             END IF;
5316 
5317             lv_modvat_flag := jai_constants.NO ;
5318 
5319             --
5320             -- This is a non recoverable line hence the tax amounts should be added into the project costs by populating
5321             -- projects related columns so that PROJECTS can consider this line for Project Costing
5322             --
5323             IF nvl(lv_accrue_on_receipt_flag,'#') <> 'Y' THEN -- Bug 6338371
5324               ln_project_id           := p_project_id;
5325               ln_task_id              := p_task_id;
5326               lv_exp_type             := p_expenditure_type;
5327               ld_exp_item_date        := p_expenditure_item_date;
5328               ln_exp_organization_id  := p_expenditure_organization_id;
5329               lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
5330               lv_pa_addition_flag         := for_dist_insertion_rec.pa_addition_flag;
5331             END if;
5332 
5333             -- For non recoverable line charge account should be same as of the parent line
5334             v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id;
5335 
5336           END IF; --line = 1
5337         ELSIF (NVL(tax_lines1_rec.inc_tax_flag,'N') = 'Y')       --inclusive case
5338         THEN
5339           IF( lv_tax_type ='PR')
5340           THEN
5341             IF ( line = 1 )
5342             THEN
5343               --recoverable part
5344               v_tax_amount       := ln_rec_tax_amt ;
5345               lv_tax_line_amount := v_tax_amount   ;   --added by eric for inclusive tax
5346               lv_modvat_flag     := tax_lines1_rec.modvat_flag  ;
5347               lv_ap_line_to_inst_flag  := 'N';         --added by eric for inclusive tax
5348               lv_tax_line_to_inst_flag := 'Y';         --added by eric for inclusive tax
5349 
5350               --non recoverable part
5351             ELSIF ( line = 2 )
5352             THEN
5353               v_tax_amount       := ln_nrec_tax_amt  ;
5354               lv_tax_line_amount :=  v_tax_amount    ;   --added by eric for inclusive tax
5355               lv_modvat_flag     := jai_constants.NO ;
5356               lv_ap_line_to_inst_flag  := 'N';           --added by eric for inclusive tax
5357               lv_tax_line_to_inst_flag := 'Y';           --added by eric for inclusive tax
5358 
5359               --recoverable part without project infor
5360             ELSIF ( line = 3 )
5361             THEN
5362               v_tax_amount                  := ln_rec_tax_amt;
5363               lv_tax_line_amount            := NULL;
5364 
5365               lv_ap_line_to_inst_flag  := 'Y';             --added by eric for inclusive tax
5366               lv_tax_line_to_inst_flag := 'N';             --added by eric for inclusive tax
5367 
5368               --Project information
5369               ln_project_id                 := NULL;
5370               ln_task_id                    := NULL;
5371               lv_exp_type                   := NULL;
5372               ld_exp_item_date              := NULL;
5373               ln_exp_organization_id        := NULL;
5374               lv_project_accounting_context := NULL;
5375               lv_pa_addition_flag           := NULL;
5376 
5377               -- For inclusive recoverable line charge account should be same as of the parent line
5378               v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id ;
5379 
5380               --recoverable part in negative amount with project infor
5381             ELSIF ( line = 4 )
5382             THEN
5383               v_tax_amount                  := NVL(ln_rec_tax_amt, v_tax_amount)* -1;
5384               lv_tax_line_amount            := NULL;
5385               lv_ap_line_to_inst_flag  := 'Y';             --added by eric for inclusive tax
5386               lv_tax_line_to_inst_flag := 'N';             --added by eric for inclusive tax
5387 
5388               --Project information
5389               ln_project_id                 := p_project_id;
5390               ln_task_id                    := p_task_id;
5391               lv_exp_type                   := p_expenditure_type;
5392               ld_exp_item_date              := p_expenditure_item_date;
5393               ln_exp_organization_id        := p_expenditure_organization_id;
5394               lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
5395               lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
5396 
5397               -- For inclusive recoverable line charge account should be same as of the parent line
5398               v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id ;
5399             END IF; --line = 1
5400           ELSIF ( lv_tax_type = 'RE'
5401                   AND p_project_id IS NOT NULL
5402                 )
5403           THEN
5404             --recoverable tax without project infor
5405             IF ( line = 1 )
5406             THEN
5407               v_tax_amount       :=  v_tax_amount  ;
5408               lv_tax_line_amount :=  v_tax_amount  ;    --added by eric for inclusive tax
5409               lv_modvat_flag     :=  tax_lines1_rec.modvat_flag ;
5410               lv_ap_line_to_inst_flag  := 'Y';          --added by eric for inclusive tax
5411               lv_tax_line_to_inst_flag := 'Y';          --added by eric for inclusive tax
5412 
5413               ln_project_id                 := NULL;
5414               ln_task_id                    := NULL;
5415               lv_exp_type                   := NULL;
5416               ld_exp_item_date              := NULL;
5417               ln_exp_organization_id        := NULL;
5418               lv_project_accounting_context := NULL;
5419               lv_pa_addition_flag           := NULL;
5420               v_dist_code_combination_id    := for_dist_insertion_rec.dist_code_combination_id ;
5421             --recoverable tax in negative amount with project infor
5422             ELSIF ( line = 2 )
5423             THEN
5424               v_tax_amount       :=  v_tax_amount * -1;
5425               lv_tax_line_amount :=  NULL ;   --added by eric for inclusive tax
5426               lv_modvat_flag     :=  tax_lines1_rec.modvat_flag  ;
5427 
5428               ln_project_id                 := p_project_id;
5429               ln_task_id                    := p_task_id;
5430               lv_exp_type                   := p_expenditure_type;
5431               ld_exp_item_date              := p_expenditure_item_date;
5432               ln_exp_organization_id        := p_expenditure_organization_id;
5433               lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
5434               lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
5435               v_dist_code_combination_id    := for_dist_insertion_rec.dist_code_combination_id ;
5436             END IF;
5437           -- ELSIF ( lv_tax_type <> 'PR' AND p_project_id IS NULL )
5438           -- THEN
5439           ELSE -- eric removed the above criteria for bug 6888665 and 6888209
5440             Fnd_File.put_line(Fnd_File.LOG, 'NOT Inclusive PR Tax,NOT Inclusive RE for project ');
5441             --The case process the inclusive NR tax and inclusive RE tax not for project
5442 
5443             lv_tax_line_amount := v_tax_amount   ;   --added by eric for inclusive tax
5444             lv_modvat_flag     := tax_lines1_rec.modvat_flag  ;
5445             lv_ap_line_to_inst_flag  := 'N';         --added by eric for inclusive tax
5446             lv_tax_line_to_inst_flag := 'Y';         --added by eric for inclusive tax
5447 
5448           END IF;--( tax_type ='PR' and (ln_lines_to_insert =4 OR )
5449         END IF; --(NVL(r_tax_lines_r ec.inc_tax_flag,'N') = 'N')
5450         -------------------------------------------------------------------------------------------
5451         --added by Eric for inclusive tax on 20-dec-2007,end
5452 
5453 
5454 
5455       Fnd_File.put_line(Fnd_File.LOG,'Before inserting into ap_invoice_lines_all for line no :'
5456       || ln_inv_line_num  );
5457 
5458 
5459       --insert exclusive tax to the ap tables
5460       --or insert recoverable inclusive tax with project information the ap tables
5461 
5462 
5463       --1.Only exlusive taxes or inclusive recoverable taxes with project information
5464       --  are  inserted into Ap Lines and Dist Lines
5465 
5466       --2.All taxes need to be inserted into jai tax tables. Futher the
5467       --  partially recoverable tax need to be splitted into 2 lines
5468 
5469       --added by Eric for inclusive tax on 20-dec-2007,begin
5470       ------------------------------------------------------------------------
5471       IF ( NVL(lv_ap_line_to_inst_flag,'N')='Y')
5472       THEN
5473         ln_inv_line_num := ln_inv_line_num + 1; --added by eric for inlcusive tax
5474       ---------------------------------------------------------------------------
5475       --added by Eric for inclusive tax on 20-dec-2007,end
5476 
5477           INSERT INTO ap_invoice_lines_all
5478           (
5479             INVOICE_ID
5480           , LINE_NUMBER
5481           , LINE_TYPE_LOOKUP_CODE
5482           , DESCRIPTION
5483           , ORG_ID
5484           , MATCH_TYPE
5485     , DEFAULT_DIST_CCID --Changes by nprashar for bug #6995437
5486           , ACCOUNTING_DATE
5487           , PERIOD_NAME
5488           , DEFERRED_ACCTG_FLAG
5489           , DEF_ACCTG_START_DATE
5490           , DEF_ACCTG_END_DATE
5491           , DEF_ACCTG_NUMBER_OF_PERIODS
5492           , DEF_ACCTG_PERIOD_TYPE
5493           , SET_OF_BOOKS_ID
5494           , AMOUNT
5495           , WFAPPROVAL_STATUS
5496           , CREATION_DATE
5497           , CREATED_BY
5498           , LAST_UPDATED_BY
5499           , LAST_UPDATE_DATE
5500           , LAST_UPDATE_LOGIN
5501           /* 5763527 */
5502           , project_id
5503           , task_id
5504           , expenditure_type
5505           , expenditure_item_date
5506           , expenditure_organization_id
5507           /* End 5763527 */
5508          ,po_distribution_id --Added for bug#6780154
5509 
5510           )
5511           VALUES
5512           (
5513             inv_id
5514           , ln_inv_line_num
5515           , lv_misc
5516           , c_tax_rec.tax_name
5517           , v_org_id
5518           , lv_match_type
5519     , v_dist_code_combination_id
5520           , rec_max_ap_lines_all.accounting_date
5521           , rec_max_ap_lines_all.period_name
5522           , rec_max_ap_lines_all.deferred_acctg_flag
5523           , rec_max_ap_lines_all.def_acctg_start_date
5524           , rec_max_ap_lines_all.def_acctg_end_date
5525           , rec_max_ap_lines_all.def_acctg_number_of_periods
5526           , rec_max_ap_lines_all.def_acctg_period_type
5527           , rec_max_ap_lines_all.set_of_books_id
5528           , ROUND(ROUND(v_tax_amount,ln_tax_precision),ln_precision) --Added by nprashar for bug # 13653889
5529           , rec_max_ap_lines_all.wfapproval_status -- Bug 4863208
5530           , sysdate
5531           , ln_user_id
5532           , ln_user_id
5533           , sysdate
5534           , ln_login_id
5535           /* 5763527 */
5536           , ln_project_id
5537           , ln_task_id
5538           , lv_exp_type
5539           , ld_exp_item_date
5540           , ln_exp_organization_id
5541           /* End 5763527 */
5542           ,po_dist_id --Added for bug#6780154
5543           );
5544 
5545           Fnd_File.put_line(Fnd_File.LOG,
5546           'Before inserting into ap_invoice_distributions_all for distribution line no : ' || v_distribution_no);
5547 
5548           -- Start bug#3332988
5549           open c_get_invoice_distribution;
5550           fetch c_get_invoice_distribution into v_invoice_distribution_id;
5551           close c_get_invoice_distribution;
5552           -- End bug#3332988
5553 
5554           fnd_file.put_line(FND_FILE.LOG, ' Invoice distribution id '|| v_invoice_distribution_id);
5555 
5556           INSERT INTO ap_invoice_distributions_all
5557           (
5558           accounting_date,
5559           accrual_posted_flag,
5560           assets_addition_flag,
5561           assets_tracking_flag,
5562           cash_posted_flag,
5563           distribution_line_number,
5564           dist_code_combination_id,
5565           invoice_id,
5566           last_updated_by,
5567           last_update_date,
5568           line_type_lookup_code,
5569           period_name,
5570           set_of_books_id,
5571           amount,
5572           base_amount,
5573           batch_id,
5574           created_by,
5575           creation_date,
5576           description,
5577           exChange_rate_variance,
5578           last_update_login,
5579           match_status_flag,
5580           posted_flag,
5581           rate_var_code_combination_id,
5582           reversal_flag,
5583           program_application_id,
5584           program_id,
5585           program_update_date,
5586           accts_pay_code_combination_id,
5587           invoice_distribution_id,
5588           quantity_invoiced,
5589           po_distribution_id ,
5590           rcv_transaction_id,
5591           matched_uom_lookup_code,
5592           invoice_line_number,
5593           org_id -- Bug 4863208
5594           ,charge_applicable_to_dist_id -- Bug 5401111. Added by Lakshmi Gopalsami
5595           /* 5763527 */
5596           , project_id
5597           , task_id
5598           , expenditure_type
5599           , expenditure_item_date
5600           , expenditure_organization_id
5601           , project_accounting_context
5602           , pa_addition_flag
5603           /* End 5763527 */
5604           -- Bug 7249100. Added by Lakshmi Gopalsami
5605           ,distribution_class
5606            ,dist_match_type --Added by nprashar for bug #  12832424
5607           )
5608           VALUES
5609           (
5610           rec_max_ap_lines_all.accounting_date,
5611           'N', --for_dist_insertion_rec.accrual_posted_flag,
5612           for_dist_insertion_rec.assets_addition_flag,
5613           v_assets_tracking_flag, -- for_dist_insertion_rec.assets_tracking_flag, bug # 2851123
5614           'N',
5615           v_distribution_no,
5616     /* Bug 5358788. Added by Lakshmi Gopalsami. Commented
5617            * for_dist_insertion_rec.dist_code_combination_id
5618      * and v_dist_code_combination_id
5619      */
5620           v_dist_code_combination_id,
5621           inv_id,
5622           ln_user_id,
5623           sysdate,
5624           lv_misc,
5625           rec_max_ap_lines_all.period_name,
5626           rec_max_ap_lines_all.set_of_books_id,
5627           ROUND(round(v_tax_amount,ln_tax_precision),ln_precision), --Added by nprashar for bug # 13653889
5628           ROUND(ROUND(v_tax_amount * for_dist_insertion_rec.exchange_rate,ln_tax_precision), ln_precision), --Added by nprashar for bug # 13653889
5629           v_batch_id,
5630           ln_user_id,
5631           sysdate,
5632           c_tax_rec.tax_name,
5633           null,--kunkumar for forward porting to R12
5634           ln_login_id,
5635           for_dist_insertion_rec.match_status_flag , -- Bug 4863208
5636           'N',
5637           NULL,
5638           -- 'N',
5639           for_dist_insertion_rec.reversal_flag,  -- Bug 4863208
5640           for_dist_insertion_rec.program_application_id,
5641           for_dist_insertion_rec.program_id,
5642           for_dist_insertion_rec.program_update_date,
5643           for_dist_insertion_rec.accts_pay_code_combination_id,
5644           v_invoice_distribution_id,
5645           decode(v_assets_tracking_flag, 'Y', NULL, 0), /* Modified for bug 8406404 by vumaasha */
5646           --po_dist_id,12434160
5647 			for_dist_insertion_rec.po_distribution_id, --12434160
5648           rcv_tran_id,
5649           for_dist_insertion_rec.matched_uom_lookup_code,
5650           ln_inv_line_num,
5651           for_dist_insertion_rec.org_id    -- Bug 4863208
5652           -- Bug 5401111. Added by Lakshmi Gopalsami
5653           ,decode(v_assets_tracking_flag,'N',
5654                   NULL,for_dist_insertion_rec.invoice_distribution_id)
5655 /*Commented the account_type condition for bug#7008161 by JMEENA
5656        decode(lv_account_type, 'A',
5657               for_dist_insertion_rec.invoice_distribution_id,NULL)
5658             )
5659 */
5660           /* 5763527 */
5661           , ln_project_id
5662           , ln_task_id
5663           , lv_exp_type
5664           , ld_exp_item_date
5665           , ln_exp_organization_id
5666           , lv_project_accounting_context
5667           , lv_pa_addition_flag
5668           /* End 5763527 */
5669           -- Bug 7249100. Added by Lakshmi Gopalsami
5670           , lv_dist_class
5671            , for_dist_insertion_rec.dist_match_type --Added by nprashar for bug # 12832424
5672           );
5673 
5674 
5675 
5676         --added by Eric for inclusive tax on 20-dec-2007,begin
5677         ----------------------------------------------------------------
5678         END IF; --( NVL(lv_ap_line_to_inst_flag,'N')='Y')
5679         ----------------------------------------------------------------
5680         --added by Eric for inclusive tax on 20-dec-2007,end
5681 
5682 
5683         Fnd_File.put_line(Fnd_File.LOG, 'Before inserting into JAI_AP_MATCH_INV_TAXES ');
5684 
5685         --added by Eric for inclusive tax on 20-dec-2007,begin
5686         -----------------------------------------------------------------
5687         IF  (NVL(lv_tax_line_to_inst_flag,'N') = 'Y')
5688         THEN
5689         -----------------------------------------------------------------
5690         --added by Eric for inclusive tax on 20-dec-2007,end
5691 
5692           INSERT INTO JAI_AP_MATCH_INV_TAXES
5693           (
5694           tax_distribution_id,
5695           exchange_rate_variance,
5696           assets_tracking_flag,
5697           invoice_id,
5698           po_header_id,
5699           po_line_id,
5700           line_location_id,
5701           set_of_books_id,
5702           --org_id,
5703           exchange_rate,
5704           exchange_rate_type,
5705           exchange_date,
5706           currency_code,
5707           code_combination_id,
5708           last_update_login,
5709           creation_date,
5710           created_by,
5711           last_update_date,
5712           last_updated_by,
5713           acct_pay_code_combination_id,
5714           accounting_date,
5715           tax_id,
5716           tax_amount,
5717           base_amount,
5718           chart_of_accounts_id,
5719           distribution_line_number,
5720           --project_id,
5721           --task_id,
5722           po_distribution_id,  -- added  by bug#3038566
5723           parent_invoice_distribution_id,  -- added  by bug#3038566
5724           legal_entity_id -- added by rallamse bug#
5725           -- Added by Brathod, Bug# 4445989
5726           ,invoice_line_number
5727           ,INVOICE_DISTRIBUTION_ID
5728           ,PARENT_INVOICE_LINE_NUMBER
5729           ,RCV_TRANSACTION_ID
5730           ,LINE_TYPE_LOOKUP_CODE
5731           -- End Bug# 4445989
5732           /*5763527 */
5733           ,recoverable_flag
5734           /* End 5763527 */
5735           ,line_no  -- Bug 5553150, 5593895
5736           )
5737           VALUES
5738           (
5739           JAI_AP_MATCH_INV_TAXES_S.NEXTVAL,
5740           null,--kunkumar for forward porting to R12
5741           v_assets_tracking_flag, -- 'N', bug # 2851123
5742           inv_id,
5743           cur_items_rec.po_header_id,
5744           cur_items_rec.po_line_id,
5745           cur_items_rec.line_location_id,
5746           cur_items_rec.set_of_books_id,
5747           --cur_items_rec.org_id,
5748           cur_items_rec.rate,
5749           cur_items_rec.rate_type,
5750           cur_items_rec.rate_date,
5751           cur_items_rec.currency_code,
5752           /* Bug 5358788. Added by Lakshmi Gopalsami. Commented
5753                 * c_tax_rec.tax_account_id and v_dist_code_combination_id
5754           */
5755           v_dist_code_combination_id,
5756           cur_items_rec.last_update_login,
5757           cur_items_rec.creation_date,
5758           cur_items_rec.created_by,
5759           cur_items_rec.last_update_date,
5760           cur_items_rec.last_updated_by,
5761           apccid,
5762           cur_items_rec.invoice_date,
5763           tax_lines1_rec.tax_id,
5764           /*commented out by eric for inclusive tax
5765           round(v_tax_amount,ln_precision), -- ROUND(v_tax_amount, 2), by Aparajita bug#2567799
5766           */
5767           ROUND(ROUND(lv_tax_line_amount,ln_tax_precision),ln_precision), --added by eric for inclusive tax
5768           ROUND(ROUND(tax_lines1_rec.tax_amount, ln_tax_precision),ln_precision),
5769           caid,
5770           v_distribution_no,
5771           --p_project_id,
5772           --p_task_id,
5773           po_dist_id, -- added  by bug#3038566
5774           for_dist_insertion_rec.invoice_distribution_id, -- added  by bug#3038566
5775           get_ven_info_rec.legal_entity_id,-- added by rallamse bug#
5776           --, ln_inv_line_num , commented out by eric for inclusvice tax
5777           --, v_invoice_distribution_id , commented out by eric for inclusvice tax
5778           --added by eric for inclusvice tax on 20-dec-2007,begin
5779           ---------------------------------------------------------------
5780           DECODE ( NVL(tax_lines1_rec.inc_tax_flag,'N')
5781                  , 'N',ln_inv_line_num
5782                  , 'Y',pn_invoice_line_number
5783                  )
5784           ,NVL(v_invoice_distribution_id,for_dist_insertion_rec.invoice_distribution_id)
5785           ---------------------------------------------------------------
5786           --added by eric for inclusvice tax on 20-dec-2007,end
5787           , pn_invoice_line_number
5788           , rcv_tran_id
5789           , lv_misc
5790           /* 5763527 */
5791           ,lv_modvat_flag
5792           /* End 5763527 */
5793           ,tax_lines1_rec.tax_line_no  -- --Bug 5553150, 5593895
5794           );
5795         --added by Eric for inclusive tax on 20-dec-2007,begin
5796         -----------------------------------------------------------------
5797         END IF;--  (NVL(lv_tax_line_to_inst_flag,'N') = 'Y')
5798         -----------------------------------------------------------------
5799         --added by Eric for inclusive tax on 20-dec-2007,end
5800 
5801         /*  Bug 46863208. Added by Lakshmi Gopalsami
5802          Commented the MRC call
5803         insert_mrc_data(v_invoice_distribution_id); -- bug#3332988
5804         */
5805         /*Commented out by Eric for bug 8345512 on 13-Jul-2009
5806           cum_tax_amt := cum_tax_amt + round(v_tax_amount,ln_precision);
5807         */
5808 
5809         --Modified by Eric for bug 8345512 on 13-Jul-2009,begin
5810         ------------------------------------------------------------------
5811         IF NVL(tax_lines1_rec.inc_tax_flag,'N') ='N'
5812         THEN
5813           cum_tax_amt := cum_tax_amt + round(round(v_tax_amount,ln_tax_precision),ln_precision); -- ROUND(v_tax_amount, 2) bug#2567799; /* Bug 14386866 */
5814         END IF;
5815         ------------------------------------------------------------------
5816         --Modified by Eric for bug 8345512 on 13-Jul-2009,end
5817 
5818         /* Obsoleted as part of R12 , Bug# 4445989
5819         Fnd_File.put_line(Fnd_File.LOG, 'Before inserting into JAI_CMN_FA_INV_DIST_ALL ');
5820         INSERT INTO JAI_CMN_FA_INV_DIST_ALL
5821         (
5822         invoice_id,
5823         invoice_distribution_id,
5824         set_of_books_id,
5825         batch_id,
5826         po_distribution_id,
5827         rcv_transaction_id,
5828         dist_code_combination_id,
5829         accounting_date,
5830         assets_addition_flag,
5831         assets_tracking_flag,
5832         distribution_line_number,
5833         line_type_lookup_code,
5834         amount,
5835         description,
5836         match_status_flag,
5837         quantity_invoiced
5838         )
5839         VALUES
5840         (
5841         inv_id,
5842         ap_invoice_distributions_s.CURRVAL,
5843         for_dist_insertion_rec.set_of_books_id,
5844         v_batch_id,
5845         for_dist_insertion_rec.po_distribution_id,
5846         rcv_tran_id,
5847         for_dist_insertion_rec.dist_code_combination_id,
5848         for_dist_insertion_rec.accounting_date,
5849         for_dist_insertion_rec.assets_addition_flag,
5850         v_assets_tracking_flag, -- for_dist_insertion_rec.assets_tracking_flag, bug # 2851123
5851         v_distribution_no,
5852         'MISCELLANEOUS',
5853         round(v_tax_amount,ln_precision), -- ROUND(v_tax_amount, 2), by Aparajita bug#2567799
5854         c_tax_rec.tax_name,
5855         NULL,
5856         NULL
5857         );*/
5858 
5859         end loop; --> for line in 1..ln_lines_to_insert; -- Brathod, 5763527
5860       END LOOP; --tax_lines1_rec IN tax_lines1_cur(rcv_tran_id,for_org_id_rec.vendor_id)  LOOP
5861 
5862 
5863 
5864       v_update_payment_schedule:=update_payment_schedule(cum_tax_amt); -- bug#3218978
5865 
5866       UPDATE ap_invoices_all
5867       SET     invoice_amount       =  invoice_amount   + cum_tax_amt,
5868           approved_amount      =  approved_amount  + cum_tax_amt,
5869           pay_curr_invoice_amount =  pay_curr_invoice_amount + cum_tax_amt,
5870           amount_applicable_to_discount =  amount_applicable_to_discount + cum_tax_amt,
5871           payment_status_flag = decode(payment_status_flag, 'Y', 'P', payment_status_flag)
5872           -- bug#3624898
5873       WHERE  invoice_id = inv_id;
5874 
5875 
5876       -- start added for bug#3354932
5877       if for_org_id_rec.invoice_currency_code <> v_functional_currency then
5878         -- invoice currency is not the functional currency.
5879         update ap_invoices_all
5880         set    base_amount = invoice_amount  * exchange_rate
5881         where  invoice_id = inv_id;
5882       end if;
5883       -- end added for bug#3354932
5884 
5885      /* Bug 4863208. Added by Lakshmi Gopalsami
5886          Commented the MRC call
5887       update_mrc_data; -- bug#3332988
5888       */
5889 
5890 
5891     END IF;    --  p_receipt_code = 'PACKING_SLIP' THEN
5892 
5893 
5894   ELSIF p_rematch = 'RCV_MATCHING' THEN
5895 
5896 
5897     Fnd_File.put_line(Fnd_File.LOG, 'inside  p_receipt_code = RCV_MATCHING');
5898 
5899     /* Begin 5763527 */
5900     fnd_file.put_line(fnd_file.log, 'rcv_tran_id='||rcv_tran_id);
5901     open  c_get_excise_costing_flag (cp_rcv_transaction_id => rcv_tran_id
5902                                    ,cp_organization_id =>  r_rcv_transactions.organization_id     --Added by Bgowrava for Bug#7503308
5903                                      ,cp_shipment_header_id     =>  p_shipment_header_id            --Added by Bgowrava for Bug#7503308
5904                    ,cp_txn_type        =>  'DELIVER'                              --Added by Bgowrava for Bug#7503308
5905                                       ,cp_attribute1      =>  'CENVAT_COSTED_FLAG');                --Added by Bgowrava for Bug#7503308
5906     fetch c_get_excise_costing_flag into lv_excise_costing_flag;
5907     close c_get_excise_costing_flag ;
5908     /* End 5763527 */
5909 
5910 
5911     OPEN  for_org_id(inv_id);
5912     FETCH for_org_id INTO for_org_id_rec;
5913     CLOSE for_org_id;
5914 
5915     -- Added by Jason Liu for retroactive price on 2008/01/09
5916     -----------------------------------------------------------
5917     OPEN get_source_csr;
5918     FETCH get_source_csr INTO lv_source;
5919     CLOSE get_source_csr;
5920     -----------------------------------------------------------
5921     -- Modified by Jason Liu for retroactive price on 2008/01/09
5922     -- Added the parameter lv_source
5923     FOR r_tax_lines_rec IN r_tax_lines_cur(rcv_tran_id,for_org_id_rec.vendor_id,lv_source)  LOOP
5924       Fnd_File.put_line(Fnd_File.LOG, ' ');
5925         /* 5763527 */
5926         ln_project_id           := null;
5927         ln_task_id              := null;
5928         lv_exp_type             := null;
5929         ld_exp_item_date        := null;
5930         ln_exp_organization_id  := null;
5931         lv_project_accounting_context := null;
5932         lv_pa_addition_flag := null;
5933 
5934 
5935         -- Added by Brathod, For Bug# 4445989
5936         /* v_distribution_no:= v_distribution_no+1; */
5937         v_distribution_no := 1;
5938         -- ln_inv_line_num := ln_inv_line_num + 1; -- 5763527, moved the increment to just before insert statement
5939         -- End Bug# 4445989
5940 
5941         -- start added by Aparajita for bug # 2775043 on 30/01/2003
5942         v_tax_variance_inv_cur := null;
5943         v_tax_variance_fun_cur := null;
5944         v_price_var_accnt := null;
5945         r_service_regime_tax_type := null;
5946         -- end added by Aparajita for bug # 2775043 on 30/01/2003
5947 
5948         -- Bug 5358788. Added by Lakshmi Gopalsami
5949         r_VAT_regime_tax_type := null;
5950 
5951         BEGIN
5952 
5953         OPEN c_tax(r_tax_lines_rec.tax_id);
5954         FETCH c_tax INTO c_tax_rec;
5955         CLOSE c_tax;
5956 
5957 	  OPEN c_tax_curr_prec(r_tax_lines_rec.tax_id) ;
5958       FETCH c_tax_curr_prec INTO ln_tax_precision ;
5959       CLOSE c_tax_curr_prec ;
5960 
5961              -- added, kunkumar for bug#5593895
5962         ln_base_amount := null ;
5963         if r_tax_lines_rec.tax_type IN ( jai_constants.tax_type_value_added, jai_constants.tax_type_cst )  then
5964           ln_base_amount := jai_ap_utils_pkg.fetch_tax_target_amt
5965                             (
5966                               p_invoice_id          =>   inv_id ,
5967                               p_line_location_id    =>   null ,
5968                               p_transaction_id      =>   rcv_tran_id  ,
5969                               p_parent_dist_id      =>   for_dist_insertion_rec.invoice_distribution_id ,
5970                               p_tax_id              =>   r_tax_lines_rec.tax_id
5971                             ) ;
5972         end if ;
5973          -- added, kunkumar for bug#5593895
5974         /* Service Start */
5975         v_assets_tracking_flag := for_dist_insertion_rec.assets_tracking_flag;
5976         v_dist_code_combination_id := null;
5977         --initial the tax_type
5978         lv_tax_type := NULL;--added by eric for inclusive tax on 20-Dec,2007
5979 
5980         Fnd_File.put_line(Fnd_File.LOG, 'c_tax_rec.mod_cr_percentage = '||c_tax_rec.mod_cr_percentage);
5981         -- 5763527, Brathod
5982         -- Modified the if condition to consider partial recoverable lines previous logic was hadling only when it is 100
5983         if r_tax_lines_rec.modvat_flag = jai_constants.YES
5984         and nvl(c_tax_rec.mod_cr_percentage, -1) > 0
5985         then
5986          /*  recoverable tax */
5987           lv_tax_type := 'RE'; --added by eric for inclusive tax on 20-dec,2007
5988           v_assets_tracking_flag := 'N';
5989 
5990           Fnd_File.put_line(Fnd_File.LOG, 'lv_tax_type = '||lv_tax_type);
5991 
5992           open c_regime_tax_type(r_jai_regimes.regime_id, c_tax_rec.tax_type);
5993           fetch c_regime_tax_type into r_service_regime_tax_type;
5994           close c_regime_tax_type;
5995 
5996           fnd_file.put_line(FND_FILE.LOG,
5997                      ' Service regime: '||r_service_regime_tax_type.tax_type);
5998 
5999       /* Bug 5358788. Added by Lakshmi Gopalsami
6000      * Fetched the details of VAT regime
6001      */
6002 
6003           OPEN  c_regime_tax_type(r_vat_regimes.regime_id, c_tax_rec.tax_type);
6004            FETCH  c_regime_tax_type INTO  r_vat_regime_tax_type;
6005           CLOSE  c_regime_tax_type;
6006 
6007           fnd_file.put_line(FND_FILE.LOG,
6008                      ' VAT regime: '||r_vat_regime_tax_type.tax_type);
6009 
6010           if r_service_regime_tax_type.tax_type is not null then
6011             /* Service type of tax */
6012             v_dist_code_combination_id := check_service_interim_account
6013                                           (
6014                                             lv_accrue_on_receipt_flag,
6015                                             lv_accounting_method_option,
6016                                             lv_is_item_an_expense,
6017                                             r_jai_regimes.regime_id,
6018                                             v_org_id,
6019                                             p_rematch,
6020                                             c_tax_rec.tax_type,
6021               po_dist_id  --Added by JMEENA for bug#6833506
6022                                           );
6023  v_assets_tracking_flag := 'N'; /*Added by abezgam Bug# 10375894  */
6024                fnd_file.put_line(FND_FILE.LOG,
6025                    ' Regime type , CCID '
6026        || r_service_regime_tax_type.tax_type
6027              ||', ' || v_dist_code_combination_id);
6028 
6029               /* Bug 5358788. Added by Lakshmi Gopalsami
6030              * Commented p_rematch and added validation for
6031          * VAT regime.
6032          */
6033     /*following elsif block modified for bug 6595773*/
6034             ELSIF r_vat_regime_tax_type.tax_type IS NOT NULL THEN
6035              --p_rematch = 'RCV_MATCHING' then
6036 
6037               v_dist_code_combination_id := for_dist_insertion_rec.dist_code_combination_id;
6038        v_assets_tracking_flag := 'N'; /*Added by abezgam Bug# 10375894  */
6039  fnd_file.put_line(FND_FILE.LOG,
6040                    ' Regime type , CCID '
6041        || r_vat_regime_tax_type.tax_type
6042              ||', ' || v_dist_code_combination_id);
6043 
6044           end if;
6045 
6046         else  /* 5763527 introduced for PROJETCS COSTING Impl */
6047           /* 5763527 */
6048           IF nvl(lv_accrue_on_receipt_flag , '#') <> 'Y' -- Bug 6338371
6049           then
6050             ln_project_id           := p_project_id;
6051             ln_task_id              := p_task_id;
6052             lv_exp_type             := p_expenditure_type;
6053             ld_exp_item_date        := p_expenditure_item_date;
6054             ln_exp_organization_id  := p_expenditure_organization_id;
6055             lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
6056             lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
6057           END if;
6058 
6059           --non recoverable
6060           lv_tax_type := 'NR';  --added by eric for inclusive tax on 20-dec,2007
6061           Fnd_File.put_line(Fnd_File.LOG, 'lv_tax_type = '||lv_tax_type);
6062           Fnd_File.put_line(Fnd_File.LOG, 'lv_accrue_on_receipt_flag = '||lv_accrue_on_receipt_flag);
6063         end if; /*nvl(c_tax_rec.mod_cr_percentage, 0) = 100  and c_tax_rec.tax_account_id is not null*/
6064 
6065 
6066         /* Bug#4177452*/
6067         if v_dist_code_combination_id is null then
6068           v_dist_code_combination_id := for_dist_insertion_rec.dist_code_combination_id;
6069         end if;
6070 
6071         /*following line added for bug 6595773*/
6072         Fnd_File.put_line(Fnd_File.LOG, 'v_dist_code_combination_id : ' || v_dist_code_combination_id ) ;
6073         /* Service End */
6074 
6075         -- Start for bug#3752887
6076 
6077         Fnd_File.put_line(Fnd_File.LOG, 'v_apportn_factor_for_item_line = '||v_apportn_factor_for_item_line);
6078         Fnd_File.put_line(Fnd_File.LOG, 'v_tax_amount = '||v_tax_amount);
6079 
6080 	--start additions for bug#12697697
6081          Fnd_File.put_line(Fnd_File.LOG, 'r_tax_lines_rec.adhoc_flag = '||r_tax_lines_rec.adhoc_flag);
6082           Fnd_File.put_line(Fnd_File.LOG, 'r_tax_lines_rec.vat_flag = '||r_tax_lines_rec.vat_flag);
6083          Fnd_File.put_line(Fnd_File.LOG, ' r_tax_lines_rec.tax_type = '||r_tax_lines_rec.tax_type);
6084         if r_tax_lines_rec.adhoc_flag='Y'   then
6085 	  -- start bug # 13024213
6086           --if r_tax_lines_rec.tax_type in ('Freight','Insurance','Octrai','Other','PURCHASE TAX','ENTRY TAX') and r_tax_lines_rec.vat_flag = 'Q' ---Added In clause for bug# 13363329
6087 		  if r_tax_lines_rec.tax_type in ('Freight','Insurance','Octrai','Other','PURCHASE TAX','ENTRY TAX','Excise','EXCISE_EDUCATION_CESS','EXCISE_SH_EDU_CESS')  and r_tax_lines_rec.vat_flag = 'Q' -- for bug 16195110 by anupgupt
6088           then
6089             --Changes by nprashar for bug # 13363329
6090             v_apportn_factor_for_adhoc:= jai_ap_utils_pkg.get_apportion_factor(inv_id, pn_invoice_line_number,'QUANTITY');
6091             v_tax_amount := r_tax_lines_rec.tax_amount * v_apportn_factor_for_adhoc;
6092 	  -- end bug # 13024213
6093           --elsif r_tax_lines_rec.tax_type not in ('Freight','Insurance','Octrai','Other','PURCHASE TAX','ENTRY TAX')
6094 		  elsif r_tax_lines_rec.tax_type not in ('Freight','Insurance','Octrai','Other','PURCHASE TAX','ENTRY TAX','Excise','EXCISE_EDUCATION_CESS','EXCISE_SH_EDU_CESS') -- for bug 16195110 by anupgupt
6095          then
6096            v_tax_amount := r_tax_lines_rec.tax_amount * v_apportn_factor_for_item_line;
6097          else
6098            v_tax_amount := r_tax_lines_rec.tax_amount ;
6099          end if;
6100        else
6101            v_tax_amount := r_tax_lines_rec.tax_amount * v_apportn_factor_for_item_line;
6102         end if;
6103         Fnd_File.put_line(Fnd_File.LOG, 'v_tax_amount after apportioning = '||v_tax_amount);
6104         --end additions for bug#12697697
6105 
6106         if for_org_id_rec.invoice_currency_code <> r_tax_lines_rec.currency then
6107           v_tax_amount := v_tax_amount / for_org_id_rec.exchange_rate;
6108         end if;
6109 
6110         /* Bug#5763527 */
6111         Fnd_File.put_line(Fnd_File.LOG, 'c_tax_rec.tax_type='||c_tax_rec.tax_type  ||',lv_excise_costing_flag='||lv_excise_costing_flag);
6112         if UPPER(c_tax_rec.tax_type) like  '%EXCISE%' then
6113 
6114           if lv_excise_costing_flag = 'Y'  THEN
6115             IF  nvl(lv_accrue_on_receipt_flag , '#') <> 'Y' -- Bug 6338371
6116             then
6117               ln_project_id           := p_project_id;
6118               ln_task_id              := p_task_id;
6119               lv_exp_type             := p_expenditure_type;
6120               ld_exp_item_date        := p_expenditure_item_date;
6121               ln_exp_organization_id  := p_expenditure_organization_id;
6122               lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
6123               lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
6124       END if;
6125             v_dist_code_combination_id    := for_dist_insertion_rec.dist_code_combination_id;
6126           end if;
6127         end if;
6128         /* End# 5763527 */
6129 
6130         if nvl(for_dist_insertion_rec.invoice_price_variance, -1) <>-1 --Modified by kunkumar for bug#5593895
6131            and
6132            nvl(for_dist_insertion_rec.amount, -1) <> -1 then  --Modified by kunkumar for bug#5593895
6133 
6134           v_tax_variance_inv_cur :=
6135           v_tax_amount *
6136           (for_dist_insertion_rec.invoice_price_variance / for_dist_insertion_rec.amount);
6137 
6138           if nvl(v_tax_variance_inv_cur, -1) <> -1 then --Modified by kunkumar for bug#5593895
6139             v_tax_variance_fun_cur := v_tax_variance_inv_cur * nvl(for_org_id_rec.exchange_rate, 1);
6140           end if;
6141 
6142           v_price_var_accnt := for_dist_insertion_rec.price_var_code_combination_id;
6143 
6144         end if;
6145         -- End for bug#3752887
6146 
6147         -- Start bug#4103473
6148         if nvl(v_tax_amount,-1) = -1 then --Modified by kunkumar for bug#5593895
6149           v_tax_amount := apportion_tax_4_price_cor_inv(v_tax_amount,  r_tax_lines_rec.tax_id);
6150         END IF;
6151         v_tax_amount := nvl(v_tax_amount, -1); --Modified by kunkumar for bug#5593895
6152         -- End bug#4103473
6153 
6154 		--start additions for bug#13422310
6155 	  fnd_file.put_line(FND_FILE.LOG, 'Before calling JAI_CALC_APMATCH_TAX  parameters passed are ');
6156 	  fnd_file.put_line(FND_FILE.LOG, ' P_MATCH_TYPE -> '||'RCV_MATCHING'||' P_INVOICE_ID -> '||inv_id||
6157 	  ' P_INVOICE_line_no-> '||pn_invoice_line_number||' P_TAX_ID-> '||r_tax_lines_rec.tax_id||'p_rcv_transaction_id -> '|| rcv_tran_id||' P_tax_amount        -> '|| v_tax_amount);
6158 	  jai_aptax_pkg_client_extn.JAI_CALC_APMATCH_TAX (P_MATCH_TYPE        => 'RCV_MATCHING',
6159 	                        P_INVOICE_ID        =>inv_id,
6160 							P_INVOICE_line_no   =>pn_invoice_line_number,
6161 							P_TAX_ID            => r_tax_lines_rec.tax_id,
6162 							p_rcv_transaction_id => rcv_tran_id,
6163 							P_tax_amount        => v_tax_amount
6164 							);
6165 		fnd_file.put_line(FND_FILE.LOG, 'the tax amount after the JAI_CALC_APMATCH_TAX call is  '||v_tax_amount);
6166       --end additions for bug#13422310
6167 
6168 
6169         --
6170         -- Begin 5763527
6171         --
6172         ln_rec_tax_amt  := null;
6173         ln_nrec_tax_amt := null;
6174         ln_lines_to_insert := 1; -- Loop controller to insert more than one lines for partially recoverable tax lines in PO
6175 
6176         --added by Eric for inclusive tax on 20-dec-2007,begin
6177         -------------------------------------------------------------------------------------
6178         --exclusive tax or inlcusive tax without project info,  ln_lines_to_insert := 1;
6179         --inclusive recoverable tax with project info,  ln_lines_to_insert := 2;
6180         --PR(partially recoverable) tax is processed in another logic
6181 
6182         IF ( NVL(r_tax_lines_rec.inc_tax_flag,'N') = 'N'
6183              OR ( NVL(r_tax_lines_rec.inc_tax_flag,'N') = 'Y'
6184                   AND p_project_id IS NULL
6185                 )
6186            )
6187         THEN
6188           ln_lines_to_insert := 1;
6189         ELSIF ( NVL(r_tax_lines_rec.inc_tax_flag,'N') = 'Y'
6190                 AND p_project_id IS NOT NULL
6191                 AND lv_tax_type = 'RE'
6192               )
6193         THEN
6194           ln_lines_to_insert := 2;
6195         END IF;--( NVL(i.inc_tax_flag,'N') = 'N' OR ( NVL(i.inc_tax_flag,'N'))
6196       -------------------------------------------------------------------------------------
6197       --added by Eric for inclusive tax on 20-dec-2007,end
6198 
6199 
6200 
6201 
6202         Fnd_File.put_line(Fnd_File.LOG, 'r_tax_lines_rec.modvat_flag ='||r_tax_lines_rec.modvat_flag ||',c_tax_rec.mod_cr_percentage='||c_tax_rec.mod_cr_percentage);
6203 
6204         if r_tax_lines_rec.modvat_flag = jai_constants.YES
6205         and nvl(c_tax_rec.mod_cr_percentage, -1) > 0
6206         and nvl(c_tax_rec.mod_cr_percentage, -1) < 100
6207         then
6208           --
6209           -- Tax line is for partial Recoverable tax.  Hence split amount into two parts, Recoverable and Non-Recoverable
6210           -- and instead of one line, two lines needs to be inserted.
6211           -- For ordinary lines (with modvat_flag = 'N' or with mod_cr_percentage = 100) there will be only one line inserted
6212           --
6213 
6214           --ln_lines_to_insert := 2;--commented eric for inclusive tax on Jan 4,2008
6215           ln_lines_to_insert := 2 *ln_lines_to_insert; --changed by eric for inclusive tax on Jan 4,2008
6216           ln_rec_tax_amt  :=  round ( nvl(v_tax_amount,0) * (c_tax_rec.mod_cr_percentage/100) , c_tax_rec.rounding_factor) ;
6217           ln_nrec_tax_amt :=  round ( nvl(v_tax_amount,0) - nvl(ln_rec_tax_amt,0) , c_tax_rec.rounding_factor) ;
6218       /* Added Tax rounding based on tax rounding factor for bug 6761425 by vumaasha */
6219 
6220           lv_tax_type := 'PR';  --added by eric for inclusive tax on Jan 4,2008
6221         end if;
6222           --start additions for bug#11936596
6223   if r_tax_lines_rec.modvat_flag = jai_constants.YES
6224   then
6225    ln_inclurec_tax_amt := round ( nvl(v_tax_amount,0) * (c_tax_rec.mod_cr_percentage/100) , c_tax_rec.rounding_factor) ;
6226   end if;
6227 --end additions for bug#11936596
6228 
6229         fnd_file.put_line(fnd_file.log, 'ln_lines_to_insert='||ln_lines_to_insert||
6230                                         ',ln_rec_tax_amt='||ln_rec_tax_amt          ||
6231                                         ',ln_nrec_tax_amt='||ln_nrec_tax_amt ||' ln_inclurec_tax_amt  '||ln_inclurec_tax_amt
6232                          );
6233 
6234 
6235         --
6236         --  If a line has a partially recoverable tax the following loop will be executed twice.  First line will always be for a
6237         --  non recoverable tax amount and the second line will be for a recoverable tax amount
6238         --  For ordinary lines (with modvat_flag = 'N' or with mod_cr_percentage = 100 fully recoverable) the variable
6239         --  ln_lines_to_insert will have value of 1 and hence only one line will be inserted with full tax amount
6240         --
6241 
6242       Fnd_File.put_line(Fnd_File.LOG, 'ln_lines_to_insert ='|| ln_lines_to_insert);
6243       for line in 1..ln_lines_to_insert
6244       loop
6245         --deleted by eric for inclusive tax on 04-Jan-2008,begin
6246         ---------------------------------------------------------------------------
6247         /*
6248         if line = 1 then
6249 
6250           v_tax_amount  := nvl(ln_rec_tax_amt, v_tax_amount);
6251           lv_modvat_flag := r_tax_lines_rec.modvat_flag ;
6252 
6253         elsif line = 2 then
6254 
6255           v_tax_amount := ln_nrec_tax_amt;
6256 
6257           if for_dist_insertion_rec.assets_tracking_flag = jai_constants.YES then
6258             v_assets_tracking_flag := jai_constants.YES;
6259           end if;
6260 
6261           lv_modvat_flag := jai_constants.NO ;
6262 
6263           --
6264           -- This is a non recoverable line hence the tax amounts should be added into the project costs by populating
6265           -- projects related columns so that PROJECTS can consider this line for Project Costing
6266           --
6267           IF nvl(lv_accrue_on_receipt_flag , '#') <> 'Y' THEN -- Bug 6338371
6268             ln_project_id           := p_project_id;
6269             ln_task_id              := p_task_id;
6270             lv_exp_type             := p_expenditure_type;
6271             ld_exp_item_date        := p_expenditure_item_date;
6272             ln_exp_organization_id  := p_expenditure_organization_id;
6273             lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
6274             lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
6275           END if;          -- For non recoverable line charge account should be same as of the parent line
6276           v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id ;
6277 
6278         end if;
6279 
6280         ln_inv_line_num := ln_inv_line_num + 1;
6281           --
6282           -- End 5763527
6283           --
6284 
6285         */
6286         ---------------------------------------------------------------------------
6287         --deleted by eric for inclusive tax on 04-Jan-2008,end
6288 
6289         --added by Eric for inclusive tax on 20-dec-2007,begin
6290         ---------------------------------------------------------------------------
6291         Fnd_File.put_line(Fnd_File.LOG, 'line = '|| line);
6292         Fnd_File.put_line(Fnd_File.LOG, 'inc_tax_flag = '|| NVL(r_tax_lines_rec.inc_tax_flag,'N'));
6293         Fnd_File.put_line(Fnd_File.LOG, 'lv_tax_type = '|| lv_tax_type);
6294         Fnd_File.put_line(Fnd_File.LOG, 'v_tax_amount = '|| v_tax_amount);
6295         Fnd_File.put_line(Fnd_File.LOG, 'ln_nrec_tax_amt = '|| ln_nrec_tax_amt);
6296         Fnd_File.put_line(Fnd_File.LOG, 'ln_rec_tax_amt  = '|| ln_rec_tax_amt );
6297 
6298         IF (NVL(r_tax_lines_rec.inc_tax_flag,'N') = 'N')--exclusive case
6299         THEN
6300           Fnd_File.put_line(Fnd_File.LOG, 'Exclusive Branch');
6301           IF line = 1 then
6302 
6303             v_tax_amount  := nvl(ln_rec_tax_amt, v_tax_amount);
6304             lv_tax_line_amount:=  v_tax_amount       ;   --added by eric for inclusive tax
6305             lv_modvat_flag := r_tax_lines_rec.modvat_flag ;
6306 
6307             lv_ap_line_to_inst_flag  := 'Y'; --added by eric for inclusive tax
6308             lv_tax_line_to_inst_flag := 'Y'; --added by eric for inclusive tax
6309           ELSIF line = 2 then
6310 
6311             v_tax_amount             := ln_nrec_tax_amt;
6312             lv_tax_line_amount       :=  v_tax_amount  ; --added by eric for inclusive tax
6313             lv_ap_line_to_inst_flag  := 'Y';             --added by eric for inclusive tax
6314             lv_tax_line_to_inst_flag := 'Y';             --added by eric for inclusive tax
6315 
6316 
6317             IF for_dist_insertion_rec.assets_tracking_flag = jai_constants.YES THEN
6318               v_assets_tracking_flag := jai_constants.YES;
6319             END IF;
6320 
6321             lv_modvat_flag := jai_constants.NO ;
6322 
6323             --
6324             -- This is a non recoverable line hence the tax amounts should be added into the project costs by populating
6325             -- projects related columns so that PROJECTS can consider this line for Project Costing
6326             --
6327             IF nvl(lv_accrue_on_receipt_flag,'#') <> 'Y' THEN -- Bug 6338371
6328               ln_project_id           := p_project_id;
6329               ln_task_id              := p_task_id;
6330               lv_exp_type             := p_expenditure_type;
6331               ld_exp_item_date        := p_expenditure_item_date;
6332               ln_exp_organization_id  := p_expenditure_organization_id;
6333               lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
6334               lv_pa_addition_flag         := for_dist_insertion_rec.pa_addition_flag;
6335             END if;
6336 
6337             -- For non recoverable line charge account should be same as of the parent line
6338             v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id;
6339 
6340           END IF; --line = 1
6341         ELSIF (NVL(r_tax_lines_rec.inc_tax_flag,'N') = 'Y')       --inclusive case
6342         THEN
6343           Fnd_File.put_line(Fnd_File.LOG, 'Inclusive Branch');
6344           IF( lv_tax_type ='PR')
6345           THEN
6346             Fnd_File.put_line(Fnd_File.LOG, 'PR Tax');
6347             IF ( line = 1 )
6348             THEN
6349               --recoverable part
6350               v_tax_amount       := ln_rec_tax_amt ;
6351               lv_tax_line_amount := v_tax_amount   ;   --added by eric for inclusive tax
6352               lv_modvat_flag     := r_tax_lines_rec.modvat_flag  ;
6353               lv_ap_line_to_inst_flag  := 'N';         --added by eric for inclusive tax
6354               lv_tax_line_to_inst_flag := 'Y';         --added by eric for inclusive tax
6355 
6356             ELSIF ( line = 2 )
6357             THEN
6358               --non recoverable part
6359               v_tax_amount       := ln_nrec_tax_amt  ;
6360               lv_tax_line_amount :=  v_tax_amount    ;   --added by eric for inclusive tax
6361               lv_modvat_flag     := jai_constants.NO ;
6362               lv_ap_line_to_inst_flag  := 'N';           --added by eric for inclusive tax
6363               lv_tax_line_to_inst_flag := 'Y';           --added by eric for inclusive tax
6364 
6365               --recoverable part without project infor
6366             ELSIF ( line = 3 )
6367             THEN
6368               v_tax_amount                  := ln_rec_tax_amt;
6369               lv_tax_line_amount            := NULL;
6370 
6371               lv_ap_line_to_inst_flag  := 'Y';             --added by eric for inclusive tax
6372               lv_tax_line_to_inst_flag := 'N';             --added by eric for inclusive tax
6373 
6374               --Project information
6375               ln_project_id                 := NULL;
6376               ln_task_id                    := NULL;
6377               lv_exp_type                   := NULL;
6378               ld_exp_item_date              := NULL;
6379               ln_exp_organization_id        := NULL;
6380               lv_project_accounting_context := NULL;
6381               lv_pa_addition_flag           := NULL;
6382 
6383               -- For inclusive recoverable line charge account should be same as of the parent line
6384               v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id ;
6385 
6386               --recoverable part in negative amount with project infor
6387             ELSIF ( line = 4 )
6388             THEN
6389               v_tax_amount                  := NVL(ln_rec_tax_amt, v_tax_amount)* -1;
6390               lv_tax_line_amount            := NULL;
6391               lv_ap_line_to_inst_flag  := 'Y';             --added by eric for inclusive tax
6392               lv_tax_line_to_inst_flag := 'N';             --added by eric for inclusive tax
6393 
6394               --Project information
6395               ln_project_id                 := p_project_id;
6396               ln_task_id                    := p_task_id;
6397               lv_exp_type                   := p_expenditure_type;
6398               ld_exp_item_date              := p_expenditure_item_date;
6399               ln_exp_organization_id        := p_expenditure_organization_id;
6400               lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
6401               lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
6402 
6403               -- For inclusive recoverable line charge account should be same as of the parent line
6404               v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id ;
6405             END IF; --line = 1
6406           ELSIF ( lv_tax_type = 'RE'
6407                   AND p_project_id IS NOT NULL
6408                 )
6409           THEN
6410             Fnd_File.put_line(Fnd_File.LOG, 'RE Tax ,p_project_id IS NOT NULL');
6411             --recoverable tax without project infor
6412             IF ( line = 1 )
6413             THEN
6414               v_tax_amount       :=  v_tax_amount  ;
6415               lv_tax_line_amount :=  v_tax_amount  ;    --added by eric for inclusive tax
6416               lv_modvat_flag     :=  r_tax_lines_rec.modvat_flag ;
6417               lv_ap_line_to_inst_flag  := 'Y';          --added by eric for inclusive tax
6418               lv_tax_line_to_inst_flag := 'Y';          --added by eric for inclusive tax
6419 
6420               ln_project_id                 := NULL;
6421               ln_task_id                    := NULL;
6422               lv_exp_type                   := NULL;
6423               ld_exp_item_date              := NULL;
6424               ln_exp_organization_id        := NULL;
6425               lv_project_accounting_context := NULL;
6426               lv_pa_addition_flag           := NULL;
6427               v_dist_code_combination_id    := for_dist_insertion_rec.dist_code_combination_id ;
6428             --recoverable tax in negative amount with project infor
6429             ELSIF ( line = 2 )
6430             THEN
6431               v_tax_amount       :=  v_tax_amount * -1;
6432               lv_tax_line_amount :=  NULL ;   --added by eric for inclusive tax
6433               lv_modvat_flag     :=  r_tax_lines_rec.modvat_flag  ;
6434 
6435               ln_project_id                 := p_project_id;
6436               ln_task_id                    := p_task_id;
6437               lv_exp_type                   := p_expenditure_type;
6438               ld_exp_item_date              := p_expenditure_item_date;
6439               ln_exp_organization_id        := p_expenditure_organization_id;
6440               lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
6441               lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
6442               v_dist_code_combination_id    := for_dist_insertion_rec.dist_code_combination_id ;
6443             END IF;
6444           --  ELSIF ( lv_tax_type <> 'PR' AND p_project_id IS NULL )
6445           --  THEN
6446           ELSE -- eric removed the above criteria for bug 6888665 and 6888209
6447             Fnd_File.put_line(Fnd_File.LOG, 'NOT Inclusive PR Tax,NOT Inclusive RE for project ');
6448             --The case process the inclusive NR tax and inclusive RE tax not for project
6449 
6450             lv_tax_line_amount := v_tax_amount   ;   --added by eric for inclusive tax
6451             lv_modvat_flag     := r_tax_lines_rec.modvat_flag  ;
6452             lv_ap_line_to_inst_flag  := 'N';         --added by eric for inclusive tax
6453             lv_tax_line_to_inst_flag := 'Y';         --added by eric for inclusive tax
6454 
6455           END IF;--( tax_type ='PR' and (ln_lines_to_insert =4 OR )
6456         END IF; --(NVL(r_tax_lines_rec.inc_tax_flag,'N') = 'N')
6457         -------------------------------------------------------------------------------------------
6458         --added by Eric for inclusive tax on 20-dec-2007,end
6459 
6460 
6461         --insert exclusive tax to the ap tables
6462         --or insert recoverable inclusive tax with project information the ap tables
6463 
6464         Fnd_File.put_line(Fnd_File.LOG,'r_tax_lines_rec.tax_id :'|| r_tax_lines_rec.tax_id );
6465         Fnd_File.put_line(Fnd_File.LOG,'line  :'|| line );
6466         Fnd_File.put_line(Fnd_File.LOG,'lv_ap_line_to_inst_flag   :'|| lv_ap_line_to_inst_flag );
6467         Fnd_File.put_line(Fnd_File.LOG,'lv_tax_line_to_inst_flag  :'|| lv_tax_line_to_inst_flag );
6468 
6469         --1.Only exlusive taxes or inclusive recoverable taxes with project information
6470         --  are  inserted into Ap Lines and Dist Lines
6471 
6472         --2.All taxes need to be inserted into jai tax tables. Futher the
6473         --  partially recoverable tax need to be splitted into 2 lines
6474 
6475         --added by Eric for inclusive tax on 20-dec-2007,begin
6476         ------------------------------------------------------------------------
6477         IF ( NVL(lv_ap_line_to_inst_flag,'N')='Y')
6478         THEN
6479           ln_inv_line_num := ln_inv_line_num + 1; --added by eric for inlcusive tax
6480         ---------------------------------------------------------------------------
6481         --added by Eric for inclusive tax on 20-dec-2007,end
6482 
6483           Fnd_File.put_line(Fnd_File.LOG,'Before inserting into ap_invoice_lines_all for line no :'|| ln_inv_line_num );
6484 
6485           INSERT INTO ap_invoice_lines_all
6486           (
6487             INVOICE_ID
6488           , LINE_NUMBER
6489           , LINE_TYPE_LOOKUP_CODE
6490           , DESCRIPTION
6491           , ORG_ID
6492           , MATCH_TYPE
6493     , DEFAULT_DIST_CCID ----Changes by nprashar for bug #6995437
6494           , ACCOUNTING_DATE
6495           , PERIOD_NAME
6496           , DEFERRED_ACCTG_FLAG
6497           , DEF_ACCTG_START_DATE
6498           , DEF_ACCTG_END_DATE
6499           , DEF_ACCTG_NUMBER_OF_PERIODS
6500           , DEF_ACCTG_PERIOD_TYPE
6501           , SET_OF_BOOKS_ID
6502           , AMOUNT
6503           , WFAPPROVAL_STATUS
6504           , CREATION_DATE
6505           , CREATED_BY
6506           , LAST_UPDATED_BY
6507           , LAST_UPDATE_DATE
6508           , LAST_UPDATE_LOGIN
6509           /* 5763527 */
6510           , project_id
6511           , task_id
6512           , expenditure_type
6513           , expenditure_item_date
6514           , expenditure_organization_id
6515           ,po_distribution_id --Added for bug#6780154
6516           )
6517           VALUES
6518           (
6519               inv_id
6520           , ln_inv_line_num
6521           , lv_misc
6522           , c_tax_rec.tax_name
6523           , v_org_id
6524           , lv_match_type
6525     , v_dist_code_combination_id
6526           , rec_max_ap_lines_all.accounting_date
6527           , rec_max_ap_lines_all.period_name
6528           , rec_max_ap_lines_all.deferred_acctg_flag
6529           , rec_max_ap_lines_all.def_acctg_start_date
6530           , rec_max_ap_lines_all.def_acctg_end_date
6531           , rec_max_ap_lines_all.def_acctg_number_of_periods
6532           , rec_max_ap_lines_all.def_acctg_period_type
6533           , rec_max_ap_lines_all.set_of_books_id
6534           , ROUND(ROUND(v_tax_amount,ln_tax_precision),ln_precision) ----Added by nprashar for bug # 13653889
6535           , rec_max_ap_lines_all.wfapproval_status  -- Bug 4863208
6536           , sysdate
6537           , ln_user_id
6538           , ln_user_id
6539           , sysdate
6540           , ln_login_id
6541           /* 5763527 */
6542           , ln_project_id
6543           , ln_task_id
6544           , lv_exp_type
6545           , ld_exp_item_date
6546           , ln_exp_organization_id
6547           /* End 5763527 */
6548           ,po_dist_id --Added for bug#6780154
6549            );
6550 
6551 
6552           Fnd_File.put_line(Fnd_File.LOG,
6553           'Before inserting into ap_invoice_distributions_all for distribution line no :' || v_distribution_no);
6554 
6555           -- Start bug#3332988
6556           open c_get_invoice_distribution;
6557           fetch c_get_invoice_distribution into v_invoice_distribution_id;
6558           close c_get_invoice_distribution;
6559           -- End bug#3332988
6560 
6561           fnd_file.put_line(FND_FILE.LOG, ' Invoice distribution id '|| v_invoice_distribution_id);
6562 
6563           INSERT INTO ap_invoice_distributions_all
6564           (
6565           accounting_date,
6566           accrual_posted_flag,
6567           assets_addition_flag,
6568           assets_tracking_flag,
6569           cash_posted_flag,
6570           distribution_line_number,
6571           dist_code_combination_id,
6572           invoice_id,
6573           last_updated_by,
6574           last_update_date,
6575           line_type_lookup_code,
6576           period_name,
6577           set_of_books_id,
6578           amount,
6579           base_amount,
6580           batch_id,
6581           created_by,
6582           creation_date,
6583           description,
6584           exchange_rate_variance,
6585           last_update_login,
6586           match_status_flag,
6587           posted_flag,
6588           rate_var_code_combination_id,
6589           reversal_flag,
6590           program_application_id,
6591           program_id,
6592           program_update_date,
6593           accts_pay_code_combination_id,
6594           invoice_distribution_id,
6595           quantity_invoiced,
6596           po_distribution_id ,
6597           rcv_transaction_id,
6598           price_var_code_combination_id,
6599           invoice_price_variance,
6600           base_invoice_price_variance,
6601           matched_uom_lookup_code,
6602           INVOICE_LINE_NUMBER,
6603           org_id    -- Bug 4863208
6604           ,charge_applicable_to_dist_id -- Bug 5401111. Added by Lakshmi Gopalsami
6605             /* 5763527 */
6606             , project_id
6607             , task_id
6608             , expenditure_type
6609             , expenditure_item_date
6610             , expenditure_organization_id
6611             , project_accounting_context
6612             , pa_addition_flag
6613             -- Bug 7249100. Added by Lakshmi Gopalsami
6614             ,distribution_class
6615       ,dist_match_type --Added by nprashar for bug # 12832424
6616       )
6617           VALUES
6618           (
6619           for_dist_insertion_rec.accounting_date,
6620           'N', --for_dist_insertion_rec.accrual_posted_flag,
6621           for_dist_insertion_rec.assets_addition_flag,
6622           v_assets_tracking_flag, -- for_dist_insertion_rec.assets_tracking_flag, bug # 2851123
6623           'N',
6624           v_distribution_no,
6625           /* Bug 5358788. Added by Lakshmi Gopalsami. Commented
6626            * for_dist_insertion_rec.dist_code_combination_id
6627            * and v_dist_code_combination_id
6628            */
6629           v_dist_code_combination_id,
6630           inv_id,
6631           for_dist_insertion_rec.last_updated_by,
6632           for_dist_insertion_rec.last_update_date,
6633           lv_misc,
6634           for_dist_insertion_rec.period_name,
6635           for_dist_insertion_rec.set_of_books_id,
6636           ROUND(ROUND(v_tax_amount, ln_tax_precision),ln_precision), --Added by nprashar for bug # 13653889
6637           ROUND(ROUND(v_tax_amount * for_dist_insertion_rec.exchange_rate, ln_tax_precision),ln_precision),  --Added by nprashar for bug # 13653889
6638           v_batch_id,
6639           for_dist_insertion_rec.created_by,
6640           for_dist_insertion_rec.creation_date,
6641           c_tax_rec.tax_name,
6642           null,--kunkumar for forwad porting to R12
6643           for_dist_insertion_rec.last_update_login,
6644           for_dist_insertion_rec.match_status_flag , -- Bug 4863208
6645           'N',
6646           NULL,
6647           --'N',
6648           for_dist_insertion_rec.reversal_flag,  -- Bug 4863208
6649           for_dist_insertion_rec.program_application_id,
6650           for_dist_insertion_rec.program_id,
6651           for_dist_insertion_rec.program_update_date,
6652           for_dist_insertion_rec.accts_pay_code_combination_id,
6653           v_invoice_distribution_id,
6654           decode(v_assets_tracking_flag, 'Y', NULL, 0),/* Modified for bug 8406404 by vumaasha */
6655            --po_dist_id,12434160
6656 			for_dist_insertion_rec.po_distribution_id, --12434160
6657           rcv_tran_id,
6658           v_price_var_accnt,
6659           v_tax_variance_inv_cur,
6660           v_tax_variance_fun_cur,
6661           for_dist_insertion_rec.matched_uom_lookup_code,
6662           ln_inv_line_num,
6663           for_dist_insertion_rec.org_id   -- bug 4863208
6664           -- Bug 5401111. Added by Lakshmi Gopalsami
6665           ,decode(v_assets_tracking_flag,'N',
6666                   NULL,for_dist_insertion_rec.invoice_distribution_id)
6667 /*Commented the account_type condition for bug#7008161 by JMEENA
6668        decode(lv_account_type, 'A',
6669               for_dist_insertion_rec.invoice_distribution_id,NULL)
6670             )
6671 */
6672             /* 5763527 */
6673             , ln_project_id
6674             , ln_task_id
6675             , lv_exp_type
6676             , ld_exp_item_date
6677             , ln_exp_organization_id
6678             , lv_project_accounting_context
6679             , lv_pa_addition_flag
6680             -- Bug 7249100. Added by Lakshmi Gopalsami
6681             , lv_dist_class
6682          ,for_dist_insertion_rec.dist_match_type --Added by nprashar for bug # 12832424
6683 );
6684 
6685         --added by Eric for inclusive tax on 20-dec-2007,begin
6686         ---------------------------------------------------------------
6687         END IF;  --( NVL(lv_ap_line_to_inst_flag,'N')='Y')
6688         ---------------------------------------------------------------
6689         --added by Eric for inclusive tax on 20-dec-2007,end
6690 
6691         --added by Eric for inclusive tax on 20-dec-2007,begin
6692         -----------------------------------------------------------------
6693         IF  (NVL(lv_tax_line_to_inst_flag,'N') = 'Y')
6694         THEN
6695         -----------------------------------------------------------------
6696         --added by Eric for inclusive tax on 20-dec-2007,end
6697           --Added by Qinglei on 10-Feb-2012 for bug#13589613 begin
6698           IF NVL(r_tax_lines_rec.inc_tax_flag,'N') = 'Y' AND NVL(lv_modvat_flag,'N') = 'Y' THEN
6699              ln_inv_line_num := ln_inv_line_num + 1;
6700           END IF;
6701           --Added by Qinglei on 10-Feb-2012 for bug#13589613 end
6702           Fnd_File.put_line(Fnd_File.LOG, 'Before inserting into JAI_AP_MATCH_INV_TAXES ');
6703           INSERT INTO JAI_AP_MATCH_INV_TAXES
6704           (
6705           tax_distribution_id,
6706           --shipment_line_id,
6707           exchange_rate_variance,
6708           assets_tracking_flag,
6709           invoice_id,
6710           po_header_id,
6711           po_line_id,
6712           line_location_id,
6713           set_of_books_id,
6714           --org_id,
6715           exchange_rate,
6716           exchange_rate_type,
6717           exchange_date,
6718           currency_code,
6719           code_combination_id,
6720           last_update_login,
6721           creation_date,
6722           created_by,
6723           last_update_date,
6724           last_updated_by,
6725           acct_pay_code_combination_id,
6726           accounting_date,
6727           tax_id,
6728           tax_amount,
6729           base_amount,
6730           chart_of_accounts_id,
6731           distribution_line_number,
6732           --project_id,
6733           --task_id,
6734           po_distribution_id,  -- added  by bug#3038566
6735           parent_invoice_distribution_id,  -- added  by bug#3038566
6736           legal_entity_id -- added by rallamse bug#
6737           -- Added by Brathod, Bug# 4445989
6738           ,invoice_line_number
6739           ,INVOICE_DISTRIBUTION_ID
6740           ,PARENT_INVOICE_LINE_NUMBER
6741           ,RCV_TRANSACTION_ID
6742           ,LINE_TYPE_LOOKUP_CODE
6743           -- End Bug# 4445989
6744           /* 5763527 */
6745           , recoverable_flag
6746           /* End 5763527 */
6747           ,line_no    -- Bug 5553150, 5593895
6748           )
6749 
6750           VALUES
6751           (
6752           JAI_AP_MATCH_INV_TAXES_S.NEXTVAL,
6753           null,--kunkumar for forward porting to R12
6754           v_assets_tracking_flag, -- 'N', bug # 2851123
6755           inv_id,
6756           cur_items_rec.po_header_id, /* All references to r_cur_items_rec have been replaced by cur_items_rec */
6757           cur_items_rec.po_line_id,
6758           cur_items_rec.line_location_id,
6759           cur_items_rec.set_of_books_id,
6760           --cur_items_rec.org_id,
6761           cur_items_rec.rate,
6762           cur_items_rec.rate_type,
6763           cur_items_rec.rate_date,
6764           cur_items_rec.currency_code,
6765           /* Bug 5358788. Added by Lakshmi Gopalsami. Commented
6766            * c_tax_rec.tax_account_id and v_dist_code_combination_id
6767            */
6768           v_dist_code_combination_id,
6769           cur_items_rec.last_update_login,
6770           cur_items_rec.creation_date,
6771           cur_items_rec.created_by,
6772           cur_items_rec.last_update_date,
6773           cur_items_rec.last_updated_by,
6774           apccid,
6775           cur_items_rec.invoice_date,
6776           r_tax_lines_rec.tax_id,
6777           /*commented out by eric for inclusive tax
6778           round(v_tax_amount,ln_precision), -- ROUND(v_tax_amount, 2), by Aparajita bug#2567799
6779           */
6780           ROUND(ROUND(lv_tax_line_amount,ln_tax_precision),ln_precision), --Added by nprashar for bug # 13653889
6781           ROUND(ROUND(r_tax_lines_rec.tax_amount, ln_tax_precision),ln_precision), --Added by nprashar for bug # 13653889
6782           caid,
6783           v_distribution_no,
6784           --p_project_id,
6785           --p_task_id,
6786           po_dist_id, -- added  by bug#3038566
6787           for_dist_insertion_rec.invoice_distribution_id, -- added  by bug#3038566
6788           get_ven_info_rec.legal_entity_id -- added by rallamse bug#
6789           --, ln_inv_line_num , commented out by eric for inclusvice tax
6790           --, v_invoice_distribution_id
6791           --added by Eric for inclusive tax on 20-dec-2007,begin
6792           --------------------------------------------------------------
6793           /*, DECODE ( NVL(r_tax_lines_rec.inc_tax_flag,'N')
6794                    , 'N',ln_inv_line_num
6795                    , 'Y',pn_invoice_line_number
6796                    )*/
6797           --Added by Qinglei on 10-Feb-2012 for bug#13589613 begin
6798           , DECODE ( NVL(r_tax_lines_rec.inc_tax_flag,'N')
6799                  , 'N',ln_inv_line_num
6800                  , 'Y',DECODE(lv_modvat_flag,'Y',ln_inv_line_num,'N',pn_invoice_line_number)
6801                  )
6802           --Added by Qinglei on 10-Feb-2012 for bug#13589613 end
6803           , NVL(v_invoice_distribution_id,for_dist_insertion_rec.invoice_distribution_id)
6804           --------------------------------------------------------------
6805           --added by Eric for inclusive tax on 20-dec-2007,end
6806           , pn_invoice_line_number
6807           , rcv_tran_id
6808           , lv_misc
6809           /* 5763527 */
6810           , lv_modvat_flag
6811           /* End 5763527 */
6812           ,r_tax_lines_rec.tax_line_no  -- Bug 5553150, 5593895
6813           );
6814         --added by Eric for inclusive tax on 20-dec-2007,begin
6815         -----------------------------------------------------------------
6816         END IF;--(NVL(lv_tax_line_to_inst_flag,'N') = 'Y')
6817         -----------------------------------------------------------------
6818         --added by Eric for inclusive tax on 20-dec-2007,end
6819 
6820         /*  Bug 46863208. Added by Lakshmi Gopalsami
6821            Commented the MRC call
6822         insert_mrc_data(v_invoice_distribution_id); -- bug#3332988
6823         */
6824 
6825         /* Commented out  by Eric for bug 8345512 on 13-Jul-2009
6826             cum_tax_amt := cum_tax_amt + ROUND(v_tax_amount, ln_precision);
6827         */
6828 
6829 
6830         --Modified by Eric for bug 8345512 on 13-Jul-2009,begin
6831         ------------------------------------------------------------------
6832         IF NVL(r_tax_lines_rec.inc_tax_flag,'N') ='N'
6833         THEN
6834           cum_tax_amt := cum_tax_amt + ROUND(round(v_tax_amount,ln_tax_precision), ln_precision); /* Bug 14386866 */
6835         END IF;
6836         ------------------------------------------------------------------
6837         --Modified by Eric for bug 8345512 on 13-Jul-2009,end
6838 
6839         /* Obsoleted as part of R12 , Bug# 4445989
6840         Fnd_File.put_line(Fnd_File.LOG, 'Before inserting into JAI_CMN_FA_INV_DIST_ALL ');
6841         INSERT INTO JAI_CMN_FA_INV_DIST_ALL
6842         (
6843         invoice_id,
6844         invoice_distribution_id,
6845         set_of_books_id,
6846         batch_id,
6847         po_distribution_id,
6848         rcv_transaction_id,
6849         dist_code_combination_id,
6850         accounting_date,
6851         assets_addition_flag,
6852         assets_tracking_flag,
6853         distribution_line_number,
6854         line_type_lookup_code,
6855         amount,
6856         description,
6857         match_status_flag,
6858         quantity_invoiced
6859         )
6860         VALUES
6861         (
6862         inv_id,
6863         ap_invoice_distributions_s.CURRVAL,
6864         for_dist_insertion_rec.set_of_books_id,
6865         v_batch_id,
6866         for_dist_insertion_rec.po_distribution_id,
6867         rcv_tran_id,
6868         for_dist_insertion_rec.dist_code_combination_id,
6869         for_dist_insertion_rec.accounting_date,
6870         for_dist_insertion_rec.assets_addition_flag,
6871         v_assets_tracking_flag, -- for_dist_insertion_rec.assets_tracking_flag, bug # 2851123
6872         v_distribution_no,
6873         'MISCELLANEOUS',
6874         ROUND(v_tax_amount, ln_precision),
6875         c_tax_rec.tax_name,
6876         NULL,
6877         NULL
6878         );*/
6879 
6880         /* end modification for ap to fa modavatable taxes issue. on 19-mar-01 by subbu and pavan*/
6881         -----------------------------------------------------------------------------------------------
6882 
6883           end loop ;  --> for line in 1 to ln_lines_to_insert Brathod, 5763527
6884           --start additions by vkaranam for bug 11936596
6885 OPEN inclu_flag_cur(v_org_id);
6886         FETCH inclu_flag_cur INTO lv_inclu_tax_flag;
6887         CLOSE inclu_flag_cur;
6888  fnd_file.put_line(fnd_file.log ,'separate accounting for inclusive taxes option is '||lv_inclu_tax_flag );
6889 If nvl(lv_accrue_on_receipt_flag ,'N')='Y'
6890 --and nvl(lv_inclu_tax_flag,'N')='Y'
6891 then
6892    fnd_file.put_line(fnd_file.log ,'Start the Insertion of Inclusive Service tax lines for POT recoverable service amount is '||ln_inclurec_tax_amt);
6893    lv_tax_line_amount := null;
6894    lv_ap_line_to_inst_flag := null;
6895    ln_inclu_serv_amt := null;
6896    v_dist_code_combination_id := null;
6897 
6898 for inclu_Serv_line in 1..2
6899 loop
6900       ln_inclu_serv_amt := ln_inclurec_tax_amt;
6901 
6902       if ln_inclu_serv_amt <>0
6903          and NVL(r_tax_lines_rec.inc_tax_flag,'N') = 'Y'
6904          and r_tax_lines_rec.tax_type IN ( 'Service','SERVICE_EDUCATION_CESS','SERVICE_SH_EDU_CESS')
6905 
6906       then
6907         if inclu_Serv_line =1
6908         then
6909 
6910            lv_tax_line_amount:=  ln_inclu_serv_amt       ;
6911            lv_ap_line_to_inst_flag  := 'Y';
6912            fnd_file.put_line(fnd_file.log ,'Inclusive Service tax lines for **** tax type '||c_tax_rec.tax_type ||' v_org_id '||v_org_id ||' ln_po_dist_id  '||ln_po_dist_id||' r_jai_regimes.regime_id '||r_jai_regimes.regime_id );
6913              fnd_file.put_line(fnd_file.log ,'c_tax_rec.tax_type  '||c_tax_rec.tax_type||' po_dist_id '||po_dist_id);
6914            v_dist_code_combination_id := check_service_interim_account
6915                                                     (
6916                                                       lv_accrue_on_receipt_flag,
6917                                                       lv_accounting_method_option,
6918                                                       lv_is_item_an_expense,
6919                                                       r_jai_regimes.regime_id,
6920                                                       v_org_id,
6921                                                       'INCLUSIVE',--12813762
6922                                                       c_tax_rec.tax_type,
6923                                                        Nvl(po_dist_id,ln_po_dist_id)
6924                                                     );
6925            fnd_file.put_line(fnd_file.log ,'Inclusive Service tax lines for positve recovery line POT interim recovery account is '||v_dist_code_combination_id);
6926 
6927         elsif inclu_Serv_line =2
6928         then
6929            lv_tax_line_amount:=  ln_inclu_serv_amt *-1      ;
6930            lv_ap_line_to_inst_flag  := 'Y';
6931            v_dist_code_combination_id := for_dist_insertion_rec.dist_code_combination_id;
6932            fnd_file.put_line(fnd_file.log ,'Inclusive Service tax  POT lines for negative recovery line  ap accrual account is '||v_dist_code_combination_id);
6933          end if;
6934       end if;-- pot ln_inclu_serv_amt <>0
6935              IF ( NVL(lv_ap_line_to_inst_flag,'N')='Y')
6936                   THEN
6937                     --Modified by Qinglei on 10-Feb-2012 for bug#13589613 begin
6938                     IF inclu_Serv_line =2 THEN
6939                        ln_inv_line_num := ln_inv_line_num + 1;
6940                     END IF;
6941                     --Modified by Qinglei on 10-Feb-2012 for bug#13589613 end
6942                     Fnd_File.put_line(Fnd_File.LOG,'Before inserting Inclusive Service tax lines for POT into ap_invoice_lines_all for line no :'|| ln_inv_line_num ||'  tax amount =  '||lv_tax_line_amount);
6943 
6944                     INSERT INTO ap_invoice_lines_all
6945                     (
6946                       INVOICE_ID
6947                     , LINE_NUMBER
6948                     , LINE_TYPE_LOOKUP_CODE
6949                     , DESCRIPTION
6950                     , ORG_ID
6951                     , MATCH_TYPE
6952                     , DEFAULT_DIST_CCID
6953                     , ACCOUNTING_DATE
6954                     , PERIOD_NAME
6955                     , DEFERRED_ACCTG_FLAG
6956                     , DEF_ACCTG_START_DATE
6957                     , DEF_ACCTG_END_DATE
6958                     , DEF_ACCTG_NUMBER_OF_PERIODS
6959                     , DEF_ACCTG_PERIOD_TYPE
6960                     , SET_OF_BOOKS_ID
6961                     , AMOUNT
6962                     , WFAPPROVAL_STATUS
6963                     , CREATION_DATE
6964                     , CREATED_BY
6965                     , LAST_UPDATED_BY
6966                     , LAST_UPDATE_DATE
6967                     , LAST_UPDATE_LOGIN
6968                     , project_id
6969                     , task_id
6970                     , expenditure_type
6971                     , expenditure_item_date
6972                     , expenditure_organization_id
6973                     ,po_distribution_id
6974                     )
6975                     VALUES
6976                     (
6977                      inv_id
6978                     , ln_inv_line_num
6979                     , lv_misc
6980                     , c_tax_rec.tax_name
6981                     , v_org_id
6982                     , lv_match_type
6983                     , v_dist_code_combination_id
6984                     , rec_max_ap_lines_all.accounting_date
6985                     , rec_max_ap_lines_all.period_name
6986                     , rec_max_ap_lines_all.deferred_acctg_flag
6987                     , rec_max_ap_lines_all.def_acctg_start_date
6988                     , rec_max_ap_lines_all.def_acctg_end_date
6989                     , rec_max_ap_lines_all.def_acctg_number_of_periods
6990                     , rec_max_ap_lines_all.def_acctg_period_type
6991                     , rec_max_ap_lines_all.set_of_books_id
6992                     , ROUND(ROUND(lv_tax_line_amount,ln_tax_precision),ln_precision) --Added by nprashar for bug # 13653889
6993                     , rec_max_ap_lines_all.wfapproval_status
6994                     , sysdate
6995                     , ln_user_id
6996                     , ln_user_id
6997                     , sysdate
6998                     , ln_login_id
6999                     , ln_project_id
7000                     , ln_task_id
7001                     , lv_exp_type
7002                     , ld_exp_item_date
7003                     , ln_exp_organization_id
7004                     ,po_dist_id
7005                      );
7006 
7007 
7008                     Fnd_File.put_line(Fnd_File.LOG,
7009                     'Before inserting  Inclusive Service tax lines for POT into ap_invoice_distributions_all for distribution line no :' || v_distribution_no);
7010 
7011 
7012                     open c_get_invoice_distribution;
7013                     fetch c_get_invoice_distribution into v_invoice_distribution_id;
7014                     close c_get_invoice_distribution;
7015 
7016 
7017                     fnd_file.put_line(FND_FILE.LOG, 'Inclusive Service tax lines for POT Invoice distribution id '|| v_invoice_distribution_id);
7018 
7019                     INSERT INTO ap_invoice_distributions_all
7020                     (
7021                     accounting_date,
7022                     accrual_posted_flag,
7023                     assets_addition_flag,
7024                     assets_tracking_flag,
7025                     cash_posted_flag,
7026                     distribution_line_number,
7027                     dist_code_combination_id,
7028                     invoice_id,
7029                     last_updated_by,
7030                     last_update_date,
7031                     line_type_lookup_code,
7032                     period_name,
7033                     set_of_books_id,
7034                     amount,
7035                     base_amount,
7036                     batch_id,
7037                     created_by,
7038                     creation_date,
7039                     description,
7040                     exchange_rate_variance,
7041                     last_update_login,
7042                     match_status_flag,
7043                     posted_flag,
7044                     rate_var_code_combination_id,
7045                     reversal_flag,
7046                     program_application_id,
7047                     program_id,
7048                     program_update_date,
7049                     accts_pay_code_combination_id,
7050                     invoice_distribution_id,
7051                     quantity_invoiced,
7052                     po_distribution_id ,
7053                     rcv_transaction_id,
7054                     price_var_code_combination_id,
7055                     invoice_price_variance,
7056                     base_invoice_price_variance,
7057                     matched_uom_lookup_code,
7058                     INVOICE_LINE_NUMBER,
7059                     org_id
7060                     ,charge_applicable_to_dist_id
7061                       , project_id
7062                       , task_id
7063                       , expenditure_type
7064                       , expenditure_item_date
7065                       , expenditure_organization_id
7066                       , project_accounting_context
7067                       , pa_addition_flag
7068                       ,distribution_class
7069                   , dist_match_type --Added by nprashar for bug # 12832424
7070                 )
7071                     VALUES
7072                     (
7073                     for_dist_insertion_rec.accounting_date,
7074                     'N',
7075                     for_dist_insertion_rec.assets_addition_flag,
7076                     v_assets_tracking_flag,
7077                     'N',
7078                     v_distribution_no,
7079                     v_dist_code_combination_id,
7080                     inv_id,
7081                     for_dist_insertion_rec.last_updated_by,
7082                     for_dist_insertion_rec.last_update_date,
7083                     lv_misc,
7084                     for_dist_insertion_rec.period_name,
7085                     for_dist_insertion_rec.set_of_books_id,
7086                     ROUND(ROUND(lv_tax_line_amount,ln_tax_precision), ln_precision), --Added by nprashar for bug # 13653889
7087                     ROUND(ROUND(lv_tax_line_amount * for_dist_insertion_rec.exchange_rate, ln_tax_precision),ln_precision), --Added by nprashar for bug # 13653889
7088                     v_batch_id,
7089                     for_dist_insertion_rec.created_by,
7090                     for_dist_insertion_rec.creation_date,
7091                     c_tax_rec.tax_name,
7092                     null,
7093                     for_dist_insertion_rec.last_update_login,
7094                     for_dist_insertion_rec.match_status_flag ,
7095                     'N',
7096                     NULL,
7097                     for_dist_insertion_rec.reversal_flag,
7098                     for_dist_insertion_rec.program_application_id,
7099                     for_dist_insertion_rec.program_id,
7100                     for_dist_insertion_rec.program_update_date,
7101                     for_dist_insertion_rec.accts_pay_code_combination_id,
7102                     v_invoice_distribution_id,
7103                     decode(v_assets_tracking_flag, 'Y', NULL, 0),
7104                      --po_dist_id,12434160
7105 			for_dist_insertion_rec.po_distribution_id, --12434160
7106                     rcv_tran_id,
7107                     v_price_var_accnt,
7108                     v_tax_variance_inv_cur,
7109                     v_tax_variance_fun_cur,
7110                     for_dist_insertion_rec.matched_uom_lookup_code,
7111                     ln_inv_line_num,
7112                     for_dist_insertion_rec.org_id
7113                     ,decode(v_assets_tracking_flag,'N',
7114                             NULL,for_dist_insertion_rec.invoice_distribution_id)
7115                       , ln_project_id
7116                       , ln_task_id
7117                       , lv_exp_type
7118                       , ld_exp_item_date
7119                       , ln_exp_organization_id
7120                       , lv_project_accounting_context
7121                       , lv_pa_addition_flag
7122 
7123                       , lv_dist_class
7124                   ,for_dist_insertion_rec.dist_match_type --Added by nprashar for bug # 12832424
7125 
7126                     );
7127 
7128            --Added by Qiong for bug13579239  begin
7129            --------------------------------
7130            UPDATE JAI_AP_MATCH_INV_TAXES jat
7131            SET    jat.invoice_distribution_id = v_invoice_distribution_id
7132            WHERE  jat.invoice_id = inv_id
7133            AND    jat.po_header_id = cur_items_rec.po_header_id
7134            AND    jat.po_line_id = cur_items_rec.po_line_id
7135            AND    jat.line_location_id = cur_items_rec.line_location_id
7136            AND    jat.set_of_books_id = cur_items_rec.set_of_books_id
7137            AND    jat.line_no = r_tax_lines_rec.tax_line_no
7138            AND    jat.tax_id = r_tax_lines_rec.tax_id
7139            AND    jat.recoverable_flag = decode(inclu_Serv_line,1,'Y',2,'N','N');
7140 
7141            --------------------------------
7142            --Added by Qiong for bug13579239  end
7143 
7144                  END IF;  --POT ( NVL(lv_ap_line_to_inst_flag,'N')='Y')
7145 
7146             eNd loop; --end positive negative service line loop
7147 
7148 end if; --end accrue one receipt flag condition
7149 
7150 --end additions by vkaranam for bug 11936596
7151 
7152 
7153         END;
7154     END LOOP; -- r_tax_lines_rec IN r_tax_lines_cur(rcv_tran_id,for_org_id_rec.vendor_id)  LOOP
7155 
7156 
7157   END IF; -- p_rematch = 'RCV_MATCHING'
7158 
7159   -- Added by Qiong for reverse charge bug#16001407, Start
7160   ----------------------------------------------------------------------
7161 
7162 	  JAI_AP_ST_REVERSE_PROCESS.Populate_MTCH_ST_RVRS_Dist(
7163      errbuf        => errbuf
7164     ,retcode       => retcode
7165 	,inv_id        => inv_id
7166 	,pn_invoice_line_number     => pn_invoice_line_number
7167 	,po_dist_id                   => po_dist_id
7168 	,qty_inv                       => qty_inv
7169 	,p_shipment_header_id       => p_shipment_header_id
7170 	,p_packing_slip_num          => p_packing_slip_num
7171 	,p_receipt_code              =>  p_receipt_code
7172 	,p_rematch                    =>  p_rematch
7173 	,rcv_tran_id                 =>  rcv_tran_id
7174 	,v_dist_amount               =>  v_dist_amount
7175 	,v_org_id                     =>  v_org_id
7176 	, p_project_id               =>  p_project_id
7177 	 , p_task_id                  =>  p_task_id
7178 	 , p_expenditure_type       =>  p_expenditure_type
7179 	 , p_expenditure_organization_id   =>  p_expenditure_organization_id
7180 	 , p_expenditure_item_date          =>  p_expenditure_item_date
7181      , p_caid                           => caid
7182 	);
7183   ----------------------------------------------------------------------
7184   -- Added by Qiong for reverse charge bug#16001407, End
7185 
7186 <<end_of_program>>
7187 Fnd_File.put_line(Fnd_File.LOG, 'SUCCESSFUL END PROCEDURE - jai_ap_match_tax_pkg.process_online');
7188 EXCEPTION
7189     WHEN OTHERS THEN
7190       ERRBUF := SQLERRM;
7191       RETCODE := 2;
7192       Fnd_File.put_line(Fnd_File.LOG, 'EXCEPTION END PROCEDURE - jai_ap_match_tax_pkg.process_online');
7193       Fnd_File.put_line(Fnd_File.LOG, 'Error : ' || ERRBUF);
7194 END process_online;
7195 
7196 PROCEDURE process_batch_record (
7197   err_mesg OUT NOCOPY VARCHAR2,
7198   inv_id                          IN      NUMBER,
7199   pn_invoice_line_number          IN      NUMBER,  -- Using pn_invoice_line_number instead of dist_line_no for Bug#4445989
7200   po_dist_id                      IN      NUMBER,
7201   qty_inv                         IN      NUMBER,
7202   p_shipment_header_id            IN      NUMBER,
7203   p_packing_slip_num              IN      VARCHAR2,
7204   p_receipt_code                          VARCHAR2,
7205   p_rematch                               VARCHAR2,
7206   rcv_tran_id                     IN      NUMBER,
7207   v_dist_amount                   IN      NUMBER,
7208   --p_project_id                            NUMBER,
7209   --p_task_id                               NUMBER,
7210   --p_expenditure_type                      VARCHAR2,
7211   --p_expenditure_organization_id           NUMBER,
7212   --p_expenditure_item_date                 DATE,
7213   v_org_id                        IN      NUMBER
7214   /* 5763527, Introduced parameters for Project Implementation */
7215   ,p_project_id                            NUMBER
7216   ,p_task_id                               NUMBER
7217   ,p_expenditure_type                      VARCHAR2
7218   ,p_expenditure_organization_id           NUMBER
7219   ,p_expenditure_item_date                 DATE
7220   /* End 5763527 */
7221 
7222 )
7223 IS
7224 
7225 CURSOR get_ven_info(v_invoice_id NUMBER) IS
7226     SELECT vendor_id, vendor_site_id, org_id, cancelled_date -- cancelled date added by bug#3206083
7227          ,set_of_books_id -- added for bug#3354932
7228          ,legal_entity_id -- added rallamse for bug#
7229      ,invoice_num -- added by pramasub FP
7230     FROM   ap_invoices_all
7231     WHERE  invoice_id = v_invoice_id;
7232 
7233   CURSOR c_functional_currency(p_sob NUMBER) IS -- cursor added for bug#3354932
7234     SELECT currency_code
7235     FROM   gl_sets_of_books
7236     WHERE  set_of_books_id = p_sob;
7237 
7238    CURSOR checking_for_packing_slip(ven_id NUMBER, ven_site_id NUMBER, v_org_id NUMBER) IS
7239     SELECT pay_on_code, pay_on_receipt_summary_code
7240     FROM   po_vendor_sites_all
7241     WHERE  vendor_id = ven_id
7242     AND    vendor_site_id = ven_site_id
7243     AND    NVL(org_id, 0) = NVL(v_org_id, 0);
7244 
7245    -- Modified by Brathod for BUg# 4445989
7246    CURSOR cur_items(inv_id IN NUMBER, line_no IN NUMBER, cpn_max_dist_line_num NUMBER) IS
7247      SELECT pod.po_header_id,
7248       pod.po_line_id,
7249       pod.line_location_id,
7250       pod.set_of_books_id,
7251       pod.org_id,
7252       poh.rate,
7253       poh.rate_type,
7254       pod.rate_date,
7255       poh.currency_code,
7256       api.last_update_login,
7257       apd.dist_code_combination_id,
7258       api.creation_date,
7259       api.created_by,
7260       api.last_update_date,
7261       api.last_updated_by,
7262       api.invoice_date
7263     FROM  ap_invoices_all api,
7264       ap_invoice_distributions_all apd,
7265       po_distributions_all pod,
7266       po_headers_all poh
7267      WHERE  api.invoice_id               = inv_id
7268      AND    api.invoice_id               = apd.invoice_id
7269      AND    pod.po_header_id             = poh.po_header_id
7270      AND    apd.po_distribution_id       = pod.po_distribution_id
7271      AND    apd.invoice_line_number      = line_no
7272      AND    apd.distribution_line_number = cpn_max_dist_line_num;
7273 
7274     -- Added by Brathod to get minimum invoice line number from invoice distributions, Bug#4445989
7275      CURSOR cur_get_min_dist_linenum
7276            ( cpn_invoice_id AP_INVOICE_DISTRIBUTIONS_ALL.INVOICE_ID%TYPE
7277             ,cpn_invoice_line_number AP_INVOICE_DISTRIBUTIONS_ALL.INVOICE_LINE_NUMBER%TYPE
7278            )
7279      IS
7280      SELECT min(distribution_line_number)
7281      FROM   ap_invoice_distributions_all apid
7282      WHERE  apid.invoice_id = cpn_invoice_id
7283      AND    apid.invoice_line_number = cpn_invoice_line_number;
7284 
7285      --> Cursor to fetch the maximum line_number for current invoice, Bug# 4445989
7286      CURSOR cur_get_max_line_number
7287      IS
7288       SELECT max (line_number)
7289       FROM   ap_invoice_lines_all
7290       WHERE  invoice_id = inv_id;
7291 
7292      ln_min_dist_line_num NUMBER;
7293      lv_match_type varchar2(15);
7294      ln_max_lnno  NUMBER;
7295      ln_inv_line_num AP_INVOICE_LINES_ALL.LINE_NUMBER%TYPE;
7296      ln_user_id  NUMBER;
7297      ln_login_id NUMBER;
7298      lv_misc     VARCHAR2 (15);
7299      -- Bug 7249100. Added by Lakshmi Gopalsami
7300      lv_dist_class VARCHAR2(30);
7301 
7302        --> Cursor to fetch maximum line from ap_invoice_lines_all for current invoice, Bug# 4445989
7303       CURSOR cur_get_max_ap_inv_line (cpn_max_line_num AP_INVOICE_LINES_ALL.LINE_NUMBER%TYPE)
7304       IS
7305       SELECT   accounting_date
7306               ,period_name
7307               ,deferred_acctg_flag
7308               ,def_acctg_start_date
7309               ,def_acctg_end_date
7310               ,def_acctg_number_of_periods
7311               ,def_acctg_period_type
7312               ,set_of_books_id
7313         , wfapproval_status
7314       FROM    ap_invoice_lines_all
7315       WHERE   invoice_id = inv_id
7316       AND     line_number = cpn_max_line_num;
7317 
7318       rec_max_ap_lines_all    CUR_GET_MAX_AP_INV_LINE%ROWTYPE;
7319 
7320       -- End Bug# 4445989
7321 
7322     CURSOR c_tax(t_id NUMBER) IS
7323      SELECT tax_name, tax_account_id, mod_cr_percentage, tax_type,
7324    NVL(rounding_factor,0) rounding_factor /* added by vumaasha for bug 6761425 */
7325      -- bug 3051832, mod_cr_percentage added by aparajita on 10/10/2002 for bug # 2616100
7326      FROM   JAI_CMN_TAXES_ALL
7327      WHERE  tax_id = t_id;
7328 
7329     CURSOR c_inv(inv_id NUMBER) IS
7330       SELECT batch_id,source
7331       FROM   ap_invoices_all
7332       WHERE  invoice_id = inv_id;
7333 
7334     CURSOR for_org_id(inv_id NUMBER) IS
7335      SELECT org_id, vendor_id, NVL(exchange_rate, 1) exchange_rate, invoice_currency_code
7336      FROM   ap_invoices_all
7337      WHERE  invoice_id = inv_id;
7338 
7339     /*CURSOR for_acct_id(orgn_id NUMBER) IS
7340      SELECT accts_pay_code_combination_id
7341      FROM   ap_system_parameters_all
7342      WHERE  NVL(org_id, 0) = NVL(orgn_id, 0);
7343      */
7344      --commented the above by Sanjikum For Bug#4474501, as this cursor is not being used anywhere
7345 
7346     CURSOR for_dist_insertion(inv_id NUMBER, inv_line_num NUMBER, cpn_min_dist_line_num NUMBER) IS  /* Picks up dtls from std apps inserted line */
7347      SELECT a.accounting_date,a.accrual_posted_flag,
7348                 a.assets_addition_flag,a.assets_tracking_flag,
7349     a.cash_posted_flag,a.dist_code_combination_id,
7350     a.last_updated_by,a.last_update_date,
7351     a.line_type_lookup_code,a.period_name,
7352     a.set_of_books_id,a.amount,a.base_amount,a.batch_id,
7353     a.created_by,a.creation_date,a.description,
7354     a.accts_pay_code_combination_id,
7355     a.exchange_rate_variance,
7356     a.last_update_login,a.match_status_flag,
7357     a.posted_flag, a.rate_var_code_combination_id,
7358     a.reversal_flag,a.vat_code,a.exchange_date,
7359     a.exchange_rate, a.exchange_rate_type,
7360     a.price_adjustment_flag,a.program_application_id,
7361     a.program_id, a.program_update_date,
7362     a.global_attribute1, a.global_attribute2, a.global_attribute3,
7363     a.po_distribution_id,  a.project_id,a.task_id,a.expenditure_type,
7364     a.expenditure_item_date,a.expenditure_organization_id,
7365     quantity_invoiced,
7366     invoice_distribution_id,
7367     matched_uom_lookup_code,
7368     org_id -- Bug 4863208
7369     , project_accounting_context    /* 5763527 */
7370     , pa_addition_flag    /* 5763527 */
7371   , asset_book_type_code /* 8406404 */
7372      FROM   ap_invoice_distributions_all a
7373      WHERE  invoice_id               = inv_id
7374      AND    invoice_line_number      = inv_line_num
7375      AND    distribution_line_number = cpn_min_dist_line_num;
7376 
7377   /* Added by LGOPALSA. Bug 4210102
7378    * Added CVD and Customs education cess */
7379    CURSOR tax_lines1_cur(tran_id NUMBER,ven_id NUMBER)
7380    IS
7381    SELECT
7382      jrl.tax_amount tax_amount
7383    , jrl.tax_id
7384    , jrl.currency
7385    , jrl.tax_type tax_type
7386    , jrl.modvat_flag
7387    , jrl.tax_line_no --Added by kunkumar for bug#5593895
7388    , NVL(jcta.inclusive_tax_flag,'N') inc_tax_flag --Added by Eric for Inclusive Tax
7389    FROM
7390      JAI_RCV_LINE_TAXES jrl
7391    , rcv_shipment_lines rsl
7392    , rcv_transactions   rt
7393    , jai_cmn_taxes_all   jcta
7394    WHERE jrl.shipment_line_id = rsl.shipment_line_id
7395      AND rt.shipment_line_id = rsl.shipment_line_id
7396      AND rt.transaction_id = tran_id
7397      AND jrl.vendor_id = ven_id
7398      AND jcta.tax_id   = jrl.tax_id  --Added by Eric for Inclusive Tax
7399      AND NVL(upper(jrl.tax_type),'TDS') NOT IN ('TDS',
7400                                                'CVD',
7401                         jai_constants.tax_type_add_cvd ,     -- Date 31/10/2006 Bug 5228046 added by SACSETHI
7402                  'CUSTOMS',
7403                                                 jai_constants.tax_type_customs_edu_cess,
7404                                                 jai_constants.tax_type_cvd_edu_cess)
7405     -- GROUP BY jrl.tax_id,jrl.currency,jrl.tax_type, jrl.modvat_flag  -- commented bug#3038566
7406     order by  tax_line_no -- added bug#3038566
7407     ;
7408 
7409   /* Added by LGOPALSA. Bug 4210102
7410    * Added CVD and Customs Education Cess */
7411   --CURSOR from_line_location_taxes(p_line_location_id NUMBER, vend_id NUMBER) IS  | pramasub commented and modified for FP
7412   CURSOR from_line_location_taxes
7413   ( p_line_location_id NUMBER
7414   , vend_id NUMBER
7415   , cp_source VARCHAR2  --pramasub FP
7416   , cp_shipment_num VARCHAR2
7417   ) --pramasub FP
7418   IS
7419   SELECT * FROM( --PRAMASUB FP
7420       SELECT
7421         jpt.tax_id
7422       , jpt.tax_amount
7423       , jpt.currency
7424       , jpt.tax_target_amount
7425       , jpt.modvat_flag
7426       , jpt.tax_type
7427       , jpt.tax_line_no --Added by kunkumar for bug#5593895
7428       , NVL(jcta.inclusive_tax_flag,'N') inc_tax_flag --Added by Eric for Inclusive Tax
7429       FROM
7430         jai_po_taxes          jpt
7431       , jai_cmn_taxes_all     jcta           --Added by Eric for Inclusive Tax
7432       -- WHERE line_focus_id = focus_id
7433       WHERE jpt.line_location_id = p_line_location_id   -- 3096578
7434         AND jcta.tax_id   = jpt.tax_id                  --Added by Eric for Inclusive Tax
7435         AND NVL(upper(jpt.tax_type), 'A') NOT IN ('TDS',
7436                                             'CVD',
7437                jai_constants.tax_type_add_cvd ,     -- Date 31/10/2006 Bug 5228046 added by SACSETHI
7438               'CUSTOMS',
7439                                             jai_constants.tax_type_customs_edu_cess,
7440                                             jai_constants.tax_type_cvd_edu_cess
7441              )
7442         AND NVL(jpt.vendor_id, 0) = vend_id
7443         AND cp_source <> 'ASBN'
7444       --ORDER BY  tax_line_no; -- added bug#3038566  --pramasub FP  | commented and order by is moved to end of the qry
7445       -- AND tax_amount  <> 0 ;-- commented by aparajita on 10/03/2003 for bug # 2841363
7446       UNION -- added by pramasub for ISupp IL FP start
7447   SELECT
7448     taxes.tax_id
7449   , taxes.TAX_AMT       tax_amount
7450   , taxes.CURRENCY_CODE currency
7451   , taxes.TAX_AMT       tax_target_amount
7452   , taxes.modvat_flag
7453   , taxes.tax_type
7454   , taxes.tax_line_no -- added, Harshita for Bug 5553150
7455   , NVL(jcta.inclusive_tax_flag,'N') inc_tax_flag --Added by Eric for Inclusive Tax
7456   FROM
7457     jai_cmn_lines lines
7458   , jai_cmn_document_Taxes taxes
7459   , jai_cmn_taxes_all     jcta           --Added by Eric for Inclusive Tax
7460   WHERE lines.cmn_line_id = taxes.source_doc_line_id
7461     AND taxes.source_doc_type = 'ASBN'
7462     AND lines.po_line_location_id = p_line_location_id
7463     AND lines.shipment_number = cp_shipment_num
7464     AND NVL(taxes.vendor_id, 0) = vend_id
7465     AND jcta.tax_id   = taxes.tax_id   --Added by Eric for Inclusive Tax
7466     AND cp_source = 'ASBN')
7467   ORDER BY  tax_line_no; --pramasub FP end
7468 
7469   CURSOR c_dist_reversal_cnt(p_invoice_id IN NUMBER) IS
7470       SELECT 1
7471       FROM ap_invoice_distributions_all
7472       WHERE invoice_id = p_invoice_id
7473       AND reversal_flag = 'Y'
7474       AND rownum = 1; -- Added by avallabh for bug 4926094 on 03-Feb-2006
7475 
7476   CURSOR count_dist_no(inv_id NUMBER) IS
7477     SELECT NVL(DISTRIBUTION_LINE_NUMBER,1) FROM
7478      AP_INVOICE_DISTRIBUTIONS_ALL WHERE
7479      INVOICE_ID = inv_id
7480      AND DISTRIBUTION_LINE_NUMBER =
7481        (SELECT MAX(DISTRIBUTION_LINE_NUMBER)
7482         FROM AP_INVOICE_DISTRIBUTIONS_ALL
7483         WHERE INVOICE_ID = inv_id)
7484         FOR UPDATE OF DISTRIBUTION_LINE_NUMBER;
7485 
7486   cursor c_get_invoice_distribution  is -- bug#3332988
7487     select ap_invoice_distributions_s.nextval
7488     from   dual;
7489 
7490   -- start addition by ssumaith -bug# 3127834
7491 
7492   cursor c_fnd_curr_precision(cp_currency_code fnd_currencies.currency_code%type) is
7493   select precision
7494   from   fnd_currencies
7495   where  currency_code = cp_currency_code;
7496 
7497   -- ends here additions by ssumaith -bug# 3127834
7498 
7499   ln_precision                         fnd_currencies.precision%type; -- added by sssumaith - bug# 3127834
7500   v_statement_no                      VARCHAR2(6);
7501   v_dist_reversal_cnt                 NUMBER;
7502   v_batch_id                          NUMBER;
7503   c_tax_rec                           c_tax%ROWTYPE;
7504   v_source                            VARCHAR2(25);
7505   shpmnt_ln_id                        rcv_shipment_lines.shipment_line_id%TYPE;
7506   cur_items_rec                       cur_items%ROWTYPE;
7507   caid                                gl_sets_of_books.chart_of_accounts_id%TYPE;
7508   for_dist_insertion_rec              for_dist_insertion%ROWTYPE;
7509   cum_tax_amt                         NUMBER := 0;
7510   v_distribution_no                   NUMBER;
7511   v_tax_amount                        NUMBER;
7512   result                              BOOLEAN;
7513   req_id                              NUMBER;
7514   for_org_id_rec                      for_org_id%ROWTYPE;
7515   -- for_acct_id_rec                     for_acct_id%ROWTYPE;
7516   --commented the above by Sanjikum For Bug#4474501, as this variable is not being used anywhere
7517   v_cor_factor                        NUMBER;
7518   v_functional_currency               gl_sets_of_books.currency_code%type; -- added for bug#3354932
7519   count_dist_no_rec                   NUMBER;
7520   v_attribute12                       VARCHAR2(150);
7521   v_ATTRIBUTE2                        VARCHAR2(150);
7522   v_sup_po_header_id                  NUMBER;
7523   v_sup_po_line_id                    NUMBER;
7524   v_sup_line_location_id              NUMBER;
7525   v_sup_set_of_books_id               NUMBER;
7526   v_sup_org_id                        NUMBER;
7527   v_sup_conversion_rate               NUMBER;
7528   v_sup_tax_date                      DATE;
7529   v_sup_currency                      VARCHAR2(10);
7530   v_sup_invoice_date                  DATE;
7531   v_invoice_num                       VARCHAR2(50);
7532   v_sup_inv_type                      VARCHAR2(25);
7533   v_ship_num                          VARCHAR2(30); -- added pramasub FP
7534   get_ven_info_rec                    get_ven_info%ROWTYPE;
7535   chk_for_pcking_slip_rec             checking_for_packing_slip%ROWTYPE;
7536   v_assets_tracking_flag              ap_invoice_distributions_all.assets_tracking_flag%type;
7537   v_update_payment_schedule           boolean; -- bug#3206083
7538   v_invoice_distribution_id           ap_invoice_distributions_all.invoice_distribution_id%type; -- bug#3332988
7539   v_apportn_factor_for_item_line      number;
7540   v_dist_code_combination_id          ap_invoice_distributions_all.dist_code_combination_id%type;--bug#367196
7541 
7542   -- Bug 5401111. Added by Lakshmi Gopalsami
7543   lv_account_type gl_code_combinations.account_type%TYPE;
7544 
7545   /* 5763527 */
7546   ln_project_id           ap_invoice_distributions_all.project_id%TYPE;
7547   ln_task_id              ap_invoice_distributions_all.task_id%TYPE;
7548   lv_exp_type             ap_invoice_distributions_all.expenditure_type%TYPE;
7549   ld_exp_item_date        ap_invoice_distributions_all.expenditure_item_date%TYPE;
7550   ln_exp_organization_id  ap_invoice_distributions_all.expenditure_organization_id%TYPE;
7551   lv_project_accounting_context       ap_invoice_distributions_all.project_accounting_context%TYPE;
7552   lv_pa_addition_flag                 ap_invoice_distributions_all.pa_addition_flag%TYPE;
7553 
7554   ln_lines_to_insert                  number;
7555   ln_nrec_tax_amt                     number;
7556   ln_rec_tax_amt                      number;
7557   lv_modvat_flag                      jai_ap_match_inv_taxes.recoverable_flag%type;
7558 
7559   lv_excise_costing_flag              varchar2 (1);
7560   lv_tax_type                         varchar2(5); --added by eric for inclusive tax
7561   lv_tax_line_amount                  NUMBER := 0; --added by eric for inclusive tax
7562   lv_ap_line_to_inst_flag             VARCHAR2(1); --added by eric for inclusive tax
7563   lv_tax_line_to_inst_flag            VARCHAR2(1); --added by eric for inclusive tax
7564 
7565 
7566 
7567   cursor c_get_excise_costing_flag (cp_rcv_transaction_id   rcv_transactions.transaction_id%type,
7568                                     cp_organization_id      JAI_RCV_TRANSACTIONS.organization_id%type,      --Added by Bgowrava for Bug#7503308
7569                                     cp_shipment_header_id   JAI_RCV_TRANSACTIONS.shipment_header_id%type,          --Added by Bgowrava for Bug#7503308
7570                                     cp_txn_type             JAI_RCV_TRANSACTIONS.transaction_type%type,      --Added by Bgowrava for Bug#7503308
7571                                     cp_attribute1           JAI_RCV_TRANSACTIONS.attribute1%type)           --Added by Bgowrava for Bug#7503308
7572   is
7573     select attribute2 excise_costing_flag
7574     from   jai_rcv_transactions jrcvt
7575     where  jrcvt.parent_transaction_id = cp_rcv_transaction_id
7576   and    jrcvt.organization_id  = cp_organization_id --Added by Bgowrava for Bug#7503308
7577     and    jrcvt.shipment_header_id = cp_shipment_header_id
7578     and    jrcvt.transaction_type = cp_txn_type --'DELIVER'     --Modified by Bgowrava for Bug#7503308
7579     and    jrcvt.attribute1= cp_attribute1 ; --'CENVAT_COSTED_FLAG';    --Modified by Bgowrava for Bug#7503308
7580    /* End 5763527 */
7581 
7582   /* Service Tax */
7583   cursor c_rcv_transactions(p_transaction_id  number) is
7584      select po_line_id, organization_id
7585      from   rcv_transactions
7586      where  transaction_id = p_transaction_id;
7587 
7588    cursor c_po_lines_all(p_po_line_id number) is
7589      select item_id
7590      from   po_lines_all
7591      where  po_line_id = p_po_line_id;
7592 
7593    /* Bug 5358788. Added by Lakshmi Gopalsami
7594     * Added parameter cp_regime_code instead of
7595     * hardcoding the value for service. This is required
7596     * for re-using the same cursor to avoid performance issue
7597     * in using jai_regime_tax_types_v.
7598     */
7599    cursor c_jai_regimes(cp_regime_code IN  JAI_RGM_DEFINITIONS.regime_code%TYPE)
7600    IS
7601      select regime_id
7602      from   JAI_RGM_DEFINITIONS
7603      where  regime_code = cp_regime_code ; /* SERVICE or VAT */
7604 
7605    cursor c_regime_tax_type(cp_regime_id  number, cp_tax_type varchar2) is
7606     select attribute_code tax_type
7607     from   JAI_RGM_REGISTRATIONS
7608     where  regime_id = cp_regime_id
7609     and    registration_type = jai_constants.regn_type_tax_types  /* TAX_TYPES */
7610     and    attribute_code = cp_tax_type;
7611 
7612    /*Added by kunkumar for bug#5593895*/
7613   Cursor c_tax_curr_prec(cp_tax_id jai_cmn_taxes_all.tax_id%type) is
7614   --select NVL(rounding_factor,-1) -- for bug 13954092 by anupgupt
7615   select NVL(rounding_factor,0) -- for bug 13954092 by anupgupt
7616   from jai_cmn_taxes_all
7617   where tax_id = cp_tax_id  ;
7618   /*End, Added by kunkumar for bug#5593895*/
7619 
7620    r_rcv_transactions               c_rcv_transactions%rowtype;
7621    r_po_lines_all                   c_po_lines_all%rowtype;
7622    r_jai_regimes                    c_jai_regimes%rowtype;
7623    r_service_regime_tax_type            c_regime_tax_type%rowtype;
7624    lv_is_item_an_expense            varchar2(20); /* Service */
7625    lv_accounting_method_option      varchar2(20); /* Service */
7626    lv_accrue_on_receipt_flag        po_distributions_all.accrue_on_receipt_flag%type;
7627   /* Service Tax */
7628 
7629   /* Bug 5358788. Added by Lakshmi Gopalsami
7630    * Defined variable for fetching VAT regime/
7631    */
7632    r_vat_regimes                    c_jai_regimes%rowtype;
7633    r_vat_regime_tax_type            c_regime_tax_type%rowtype;
7634 
7635    ln_base_amount number ; --Added by kunkumar for bug#5593895
7636    ln_tax_precision                    JAI_CMN_TAXES_ALL.rounding_factor%type;--Added by kunkumar for bug#5593895
7637 
7638   FUNCTION update_payment_schedule (p_total_tax NUMBER) RETURN boolean IS -- bug # 3218978
7639 
7640     v_total_tax_in_payment            number;
7641     v_tax_installment                 number;
7642     v_payment_num                     ap_payment_schedules_all.payment_num%type;
7643     v_total_payment_amt               number;
7644     v_diff_tax_amount                 number;
7645 
7646     cursor  c_total_payment_amt is
7647     select  sum(gross_amount)
7648     from    ap_payment_schedules_all
7649     where   invoice_id = inv_id;
7650 
7651   begin
7652 
7653   Fnd_File.put_line(Fnd_File.LOG, 'Start of function  update_payment_schedule');
7654 
7655     open  c_total_payment_amt;
7656     fetch   c_total_payment_amt into v_total_payment_amt;
7657     close   c_total_payment_amt;
7658 
7659 If nvl(v_total_payment_amt, -1) = -1 then --Added by nprashar for bug # 13854408
7660   Fnd_File.put_line(Fnd_File.LOG, 'Cannot update payment schedule, total payment amount :'
7661                                     || to_char(v_total_payment_amt));
7662     return false;
7663  Elsif 	v_total_payment_amt = 0 Then
7664          v_total_payment_amt := 1;
7665   end if;
7666 
7667     v_total_tax_in_payment := 0;
7668 
7669     for c_installments in
7670     (
7671     select  gross_amount,
7672         payment_num
7673     from    ap_payment_schedules_all
7674     where   invoice_id = inv_id
7675     order by payment_num
7676     )
7677     loop
7678 
7679       v_tax_installment := 0 ;
7680       v_payment_num  :=  c_installments.payment_num;
7681 
7682       v_tax_installment := p_total_tax * (c_installments.gross_amount / nvl(v_total_payment_amt,1));
7683 
7684       v_tax_installment := round(v_tax_installment, ln_precision);
7685 
7686       update ap_payment_schedules_all
7687       set    gross_amount        = gross_amount          + v_tax_installment,
7688          amount_remaining      = amount_remaining      + v_tax_installment,
7689          inv_curr_gross_amount = inv_curr_gross_amount + v_tax_installment,
7690             payment_status_flag = decode(payment_status_flag, 'Y', 'P', payment_status_flag)
7691             -- bug#3624898
7692       where  invoice_id = inv_id
7693       and    payment_num = v_payment_num;
7694 
7695       v_total_tax_in_payment := v_total_tax_in_payment + v_tax_installment;
7696 
7697     end loop;
7698 
7699     -- any difference in tax because of rounding has to be added to the last installment.
7700     if v_total_tax_in_payment <> p_total_tax then
7701 
7702       v_diff_tax_amount := p_total_tax - v_total_tax_in_payment;
7703 
7704       v_diff_tax_amount := round(v_diff_tax_amount,ln_precision);
7705 
7706       update ap_payment_schedules_all
7707       set    gross_amount        = gross_amount          + v_diff_tax_amount,
7708          amount_remaining      = amount_remaining      + v_diff_tax_amount,
7709          inv_curr_gross_amount = inv_curr_gross_amount + v_diff_tax_amount,
7710             payment_status_flag = decode(payment_status_flag, 'Y', 'P', payment_status_flag)
7711             -- bug#3624898
7712       where  invoice_id = inv_id
7713       and    payment_num = v_payment_num;
7714     end if;
7715 
7716     return true;
7717 
7718   exception
7719     when others then
7720       Fnd_File.put_line(Fnd_File.LOG, 'exception from function  update_payment_schedule');
7721       Fnd_File.put_line(Fnd_File.LOG, sqlerrm);
7722       return false;
7723 
7724   end update_payment_schedule; -- bug # 3218978
7725 
7726   -- Start added for bug#3332988
7727   procedure insert_mrc_data (p_invoice_distribution_id number) is
7728     -- Vijay Shankar for bug#3461030
7729     v_mrc_string VARCHAR2(10000);
7730   begin
7731 
7732        v_mrc_string := 'BEGIN AP_MRC_ENGINE_PKG.Maintain_MRC_Data (
7733       p_operation_mode    => ''INSERT'',
7734       p_table_name        => ''AP_INVOICE_DISTRIBUTIONS_ALL'',
7735       p_key_value         => :a,
7736       p_key_value_list    => NULL,
7737       p_calling_sequence  =>
7738       ''India Local Tax line as Miscellaneous distribution line (jai_ap_match_tax_pkg.process_batch_record procedure)''
7739        ); END;';
7740 
7741       -- Vijay Shankar for bug#3461030
7742       EXECUTE IMMEDIATE v_mrc_string USING p_invoice_distribution_id;
7743 
7744   -- Vijay Shankar for bug#3461030
7745   exception
7746     when others then
7747     IF SQLCODE = -6550 THEN
7748       -- object referred in EXECUTE IMMEDIATE is not available in the database
7749       null;
7750       FND_FILE.put_line(FND_FILE.log, '*** MRC API is not existing(insert)');
7751     ELSE
7752       FND_FILE.put_line(FND_FILE.log, 'MRC API exists and different err(insert)->'||SQLERRM);
7753       RAISE;
7754     END IF;
7755   end;
7756 
7757   procedure update_mrc_data is
7758     -- Vijay Shankar for bug#3461030
7759     v_mrc_string VARCHAR2(10000);
7760   begin
7761 
7762        v_mrc_string := 'BEGIN AP_MRC_ENGINE_PKG.Maintain_MRC_Data
7763        (
7764       p_operation_mode    => ''UPDATE'',
7765       p_table_name        => ''AP_INVOICES_ALL'',
7766       p_key_value         => :a,
7767       p_key_value_list    => NULL,
7768       p_calling_sequence  =>
7769       ''India Local Tax amount added to invoice header (jai_ap_match_tax_pkg.process_batch_record procedure)''
7770        ); END;';
7771 
7772       -- Vijay Shankar for bug#3461030
7773       EXECUTE IMMEDIATE v_mrc_string USING inv_id;
7774 
7775   -- Vijay Shankar for bug#3461030
7776   exception
7777     when others then
7778     IF SQLCODE = -6550 THEN
7779       -- object referred in EXECUTE IMMEDIATE is not available in the database
7780       null;
7781       FND_FILE.put_line(FND_FILE.log, '*** MRC API is not existing(update)');
7782     ELSE
7783       FND_FILE.put_line(FND_FILE.log, 'MRC API exists and different err(update)->'||SQLERRM);
7784       RAISE;
7785     END IF;
7786   end;
7787 
7788   -- End added for bug#3332988
7789 
7790 BEGIN
7791 
7792 /*------------------------------------------------------------------------------------------------------
7793 CHANGE HISTORY:            FILENAME: jai_ap_match_tax_pkg.process_batch_record_p.sql
7794 S.No     dd/mm/yyyy    Author and Details
7795 ----------------------------------------------------------------------------------------------------------
7796 1.       23/05/2002    Aparajita for bug # 2359737 Version# 614.1
7797                        this procedure is extracted from jai_ap_match_tax_pkg.process_online and contains
7798                        only the pay on receipt functionality which will be now used by the
7799                        concurrent for pay on receipt which will call this procedure.
7800 
7801 2.     21/06/2002      Aparajita for bug # 2428264 Version# 614.2
7802                        In the insertion to ap_invoice_distributions_all, populated the
7803                        org id that is supplied as a parameter to this program. Without this
7804                        the org id gets defaulted from the session variables and may go wrong.
7805 
7806 3.       09/10/2002    Aparajita for bug # 2483164 Version# 615.1
7807                        Populate the po distribution id and rcv_transactions id for the tax
7808                        lines for backtracking tax to purchasing side.
7809 
7810                        Deleted the code for supplementary invoices as this is only for pay on
7811                        receipt. This code was here as this procedure was extracted from
7812                        distribution matching.
7813 
7814 4.       03/11/2002    Aparajita for bug # 2567799 Version# 615.2
7815                        Added a function apportion_tax_pay_on_rect_f and using it to apportion the
7816                        tax amount. This is useful when there are multiple distributions for the PO.
7817 
7818 
7819 5.       05/11/2002    Aparajita for bug # 2506453, Version# 615.3
7820                        Changed the cursor definition get_tax_ln_no, to look into the receipt tax
7821                        instead of the po tax table for getting the line number. This was running
7822                        into error when no taxes exist at po and have been added at receipt.
7823 
7824                        Also Changed the cursor definition get_sales_tax_dtl to fetch on the
7825                        basis of tax id instead of name.
7826 
7827 6.       15/11/2002    cbabu for bug # 2665306, Version# 615.4
7828                        Added the code when rcv_tran_id is NULL.
7829 
7830                        the code that is added was copied from jai_ap_match_tax_pkg.process_online code with
7831                        p_rematch = 'PO_MATCHING' Now the taxes for ERS invoice will be defaulted
7832                        here and header amount is updated if the invoice is created for the first
7833                        time. If any reversal is happened for distribution lines, then invoice
7834                        header amount is not modified.
7835 
7836 7.       02/12/2002    Aparajita for bug # 2689834 Version # 615.5
7837                        In the insertion to ap_invoice_distributions_all, populated the org id
7838                        that is supplied as a parameter to this program. Without this the org id
7839                        gets defaulted from the session variables and may go wrong.
7840 
7841                        Bug # 2428264 had addressed this issue before but that was only for
7842                        pay on receipt scenario. Bug # 2665306  added the PO setup where the
7843                        rcv_transaction id goes as blank. In this scenario, the org id was
7844                        not getting populated, have done the same changes for that scenario.
7845 
7846 8.       16/12/2002    cbabu for bug# 2713601, FileVersion # 615.6
7847                        AP_PAYMENTS_SCHEDULES_ALL is not getting updated with the tax amount
7848                        as soon as AP_INVOICES_ALL isgetting updated. Update statement for
7849                        ap_payment_schedules_all is written for scenario where rcv_transaction_id
7850                        in ap_invoice_distributions_all is NULL. Thsi happens in PO matching
7851                        setup for ERS invoices.
7852 
7853 9.      20/12/2002    Aparajita for bug # 2689826 Version # 615.7
7854 
7855                        The distribution line numbers were getting  skipped every time the tax
7856                        is inserted for a 2nd item line onwards. This was happening because the
7857                        distribution line number is calulated by taking the max distribution
7858                        line number and also the no of tax lines which have a shipment line id
7859                        less than corresponding shipment line id for the same shipment header.
7860                        This logic is not required as the distribution line number should always
7861                        be the next number. commented the cursor count_tax_lines.
7862 
7863 
7864 10.      14/03/2003    Aparajita for bug # 2841363. Version # 615.8
7865                        Taxes having 0 amount were not considered, changed the cursor
7866                        from_line_location_taxes to conside such tax lines for propagation into AP.
7867 
7868 11.      15/03/2003    Aparajita for bug # 2851123. Version # 615.9
7869                        The assets_tracking_flag in ap_invoice_distributions_all,
7870                        JAI_AP_MATCH_INV_TAXES and JAI_CMN_FA_INV_DIST_ALL should be set as 'N'
7871                        if the tax line against which the line is being generated is excise type of
7872                        tax and is modvatable. By default this flag gets the value from the
7873                        corresponding item line for which the tax is attached.
7874 
7875                        Introduced a variable v_assets_tracking_flag.
7876                        Modified the following cursors to fetch modvat_flag.
7877                        - from_line_location_taxes
7878                        - tax_lines1_cur
7879 
7880 12.      13/04/2003    bug#2799217. Version # 615.10
7881                        line focus id from JAI_PO_TAXES instead of
7882                        JAI_PO_LINE_LOCATIONS, changed cursor get_focus_id.
7883 
7884                        This was done as in some cases data does not exist in
7885                        JAI_PO_LINE_LOCATIONS but exists in JAI_PO_TAXES.
7886                        The root cause of this will be looked into separately.
7887 
7888 13      17/07/2003     Aparajita for bug#3038566, version # 616.1
7889 
7890                        Introduced a new function getSTformsTaxBaseAmount to calculate the
7891                        tax base amount to be populated into the table ja_in_po_st_forms_dtl.
7892                        Calculating the tax base amount from the tax amount and percentage was
7893                        giving problem because of rounding.
7894 
7895 14     14/08/2003      kpvs for bug # 3051832, version # 616.2
7896                        OPM code merged into this version of the procedure.
7897                        Changes made to pull in the OPM code fixes done for bugs 2616100 and 2616107
7898                        Used 'v_opm_flag' to check for the process enabled flag of the
7899                        organization.
7900 
7901 
7902 15.    17/08/2003     Aparajita for bug#3094025. Generic for bug#3054140. version # 616.3
7903                       There may be taxes at receipt without having any tax at PO.
7904                       For such cases, the st forms population has to cater.
7905                       Precedences are not available, so calculation of tax base is not possible.
7906                       Added the backward calcultion for such cases.
7907 
7908                       Changes in the function getSTformsTaxBaseAmount.
7909                       Introduced two new parameters tax amount and rate to calculate backward
7910                       in case PO tax details are not found. It was observed that the function
7911                       getSTformsTaxBaseAmount was always considering the precedence
7912                       0 amount irrespective of the valus of the precendences.
7913                       This has been corrected.
7914 
7915 16.   22/08/2003    Aparajita for bug# 2828928. Version # 616.4 .
7916                       Added the condition to consider null vendor id
7917                       and modvat = 'N' taxes in the following
7918                       cursor. Also added the code to consider mod_cr_percentage.
7919                        - C_third_party_tax cursor definition modified.
7920 
7921 
7922 17.     27/08/2003    Aparajita for bug#3116659. Version#616.5
7923                       Projects clean up.
7924 
7925 18.   27/08/2003    Vijay Shankar for bug# 3096578. Version # 616.6
7926                        All taxes are not picked from PO Shipment if the taxes are selected based on
7927                        line_focus_id as there are different line_focus_id's for same
7928                        line_location_id which is wrong. Also we should use line_location_id
7929                        to fetch the taxes instead of line_focus_id as line_location_id
7930                        refers to the PO Shipments in which taxes are attached and line_focus_id
7931                        is just a unique key for JAI_PO_LINE_LOCATIONS table.
7932                        Modified the cursor from_line_locations_taxes to fetch the taxes
7933                        based on line_location_id
7934 
7935             Bug# 3114596, Version same as bug# 3096578 (616.6)
7936                        When DEBIT MEMO created for RTS transaction, then if Supplier setup has
7937                        PO Matching, then dist_match_type is coming as ITEM_TO_RECEIPT
7938                        but rcv_transaction_id is not getting populated because of which taxes
7939                        are not getting defaulted by giving an error which is written in default
7940                        ELSIf. Code is modified to take the route of PO matching
7941                        if rcv_transaction_id is not populated for DEBIT MEMO
7942                        i.e source = 'RTS'
7943 
7944 
7945 19      16/10/2003    Aparajita for bug#3193849. Version#616.7
7946 
7947                       Removed the ST forms functionality from here as ST forms population is now being
7948                       handled through a concurrent.
7949 
7950 20    28/10/2003    Aparajita for bug#3206083. Version#616.7
7951 
7952                       If the base invoice is cancelled, there is no need to bring the tax lines.
7953                       Added code to check the cancelled date and if populated, returning from the proc.
7954 
7955 21    28/10/2003    Aparajita for bug#3218978. Version#616.7
7956 
7957                       Tax amount is apportioned properly between the installments.Differential
7958                       tax amounts if any because of rounding is added to the last installment.
7959 
7960                       Code is not reading anything from terms master, instead based on
7961                       the distribution of the amount before tax, tax amount is distributed.
7962                       coded in in line function update_payment_schedule.
7963 
7964 22      23/12/2003    Aparajita for bug#3306090. Version 618.1
7965                       Tax amount was calculated wrongly when tax currency is different from
7966                       invoice currency. This was not handled in the function
7967                       apportion_tax_pay_on_rect_f. Added code for the same.
7968 
7969 29    07/01/2003    Aparajita for bug#3354932. Version#618.2
7970                     When the invoice is in foreign currency, for ERS invoice, base_amount in
7971                     ap_invoices_all should be updated to include the loc taxes as otherwise
7972                     it is creating wrong accounting entries.
7973 
7974                     This update needs to be done only for ERS invoices as other cases
7975                     localization does not update the
7976                     invoice header amounts. p_rematch = 'PAY_ON_RECEIPT'.
7977                     Additionally this should be done only when the invoice currency
7978                     is not the functional currency.
7979 
7980 30      27/01/2004   Aparajita for bug#3332988. Version#618.3
7981 
7982                      Call to AP_MRC_ENGINE_PKG.Maintain_MRC_Data for MRC functionality.
7983 
7984                      This call is made after every distribution line is inserted.
7985                      This has been implemented as a in line procedure insert_mrc_data.
7986                      The procedure is called after every insert into
7987                      ap_invoice_distributions_all.
7988 
7989                     Procedure update_mrc_data has been created and invoked whereever
7990                     invoice header is being updated.
7991 
7992 
7993 31     03/02/2004   Aparajita for bug#3015112. Version#618.4
7994                     Removed the OPM costing functionality. This was updating quantity invoiced
7995                     and unit price for miscellaneous lines and this was creating problem
7996                     for subsequent transactions against the same PO / Receipt.
7997 
7998                     The changes removed were brought in for bug#3051828.
7999 
8000 32     24/02/2004   Vijay Shankar for bug#3461030. Version#618.5
8001                      Modified the MRC call to be dynamic by using EXECUTE IMMEDIATE.
8002                      This is done to take care of Ct.s who are on base version less than 11.5.7
8003 
8004 33    25/02/2004   Aparajita for bug#3315162. Version#618.6
8005 
8006                      Base application gives problem while reversing localization tax lines.
8007                      This was analyzed with base and found out that the problem was because of
8008                      null value for matched_uom_lookup_code field of the miscellaneous
8009                      tax distribution.  Further null value of quantity also creats problem as
8010                      the billed quantity on PO/receipt gets updated to null when the loc taxes
8011                      does get reversed with value populated for UOM.
8012 
8013                      Changes have been made to populate matched_uom_lookup_code same
8014                      as item line and quantity as 0.
8015 
8016 34    30/03/2004   Aparajita for bug#3448803. Version#619.1.
8017                      track as asset flag for the distribution for tax line in payable invoice now
8018                      considers partially recoverable taxes. The logic is now as follows,
8019 
8020                      - default value of this flag is same as that of the corresponding item line.
8021                      - if the corresponding tax is modvatable to teh extent of 100% then the value
8022                      of the flag is over written to 'N' irrespective of the value of the
8023                      corresponding base item line.
8024 
8025 35      15/05/2004   Aparajita for bug#3624898. Version#619.2
8026                      Whenever the tax lines are populated, if the invoice is already fully paid
8027                      the paid amount is displayed wrongly to be including the tax amount.
8028 
8029                      Fixed the problem by updating the payment_status_flag in ap_invoices)_all and
8030                      ap_payment_shedules_all to 'P' in case it was Y.
8031 
8032 36    08/07/2004     Aparajita for bug#3752887. Version#115.1, Clean up.
8033 
8034                      Function apportion_tax_pay_on_rect_f was considering the quantity
8035                      from rcv_transactions and  taxes from JAI_RCV_LINE_TAXES.
8036                      This fails when there have been corrections as taxes are modified as
8037                      per the corrected quantity.
8038 
8039                      Cleaned the entire logic of apportioning of tax from PO to AP.
8040                      Used the function jai_ap_interface_pkg.get_apportion_factor which has been
8041                      designed new to find out the factor that needs to be applied. All Logic
8042                      of apportioning is now in the external function and removed a lot of code
8043                      from here which was trying to do the same here at more than one place.
8044                      Also rmoved the in line function function apportion_tax_pay_on_rect_f.
8045 
8046                      Function jai_ap_interface_pkg.get_apportion_factor was designed for purchase register report
8047                      through bug#3633078. Added the procedure here also in the patch as it is not
8048                      required to have the report to have the function. So no dependency.
8049 
8050                      tax invoice price variance :=
8051                      tax amount * (base item line variance amount / base item line amount).
8052 
8053 
8054 37. 01/11/2003       ssumaith - bug# 3127834 - file version 115.2
8055 
8056                      currency precision was not used for rounding the tax amounts when inserting / updating
8057                      records into  ap_invoices_all , ap_invoices_distributions_all , ap_payment_Schedules_all
8058                      tables. As a result the invoice was going into automatic hold because the invoice amount
8059                      does not equal sum of distributions amount.
8060 
8061                      This has been resolved in the bug by getting the currency precision for the invoice
8062                      currency from FND_CURRENCIES table and using it in all the inserts and updates which
8063                      involve the tax amounts.
8064 
8065 38. 21/01/2005       Aparajita - Bug#4078546 Service Tax. Version#115.3
8066 
8067                     4106633    Base Bug For Education Cess on Excise, CVD and Customs.
8068                     4059774    Base Bug For Service Tax Regime Implementation.
8069                     4078546    Modification in receive and rtv accounting for service tax
8070 
8071 
8072                      Service Tax interim account needs to be populated as the charge account,
8073                      if the following condition is true.
8074 
8075                      Case of PO Matching or Accrue on receipt is No.
8076 
8077 
8078 39. 11/02/2005      Aparajita - Bug#4177452. Version#115.4
8079 
8080                     Charge account for non-recoverable taxes were going was null.
8081                     This was because of the default assignment of,
8082                       v_dist_code_combination_id := for_dist_insertion_rec.dist_code_combination_id;
8083 
8084                     inside the if of recoverable taxes.
8085 
8086                     Changed it to after the end if.
8087 
8088 40.  24/02/2005     Aparajita - Bug#4204600. Version#115.5.
8089 
8090                     from_po_distributions was getting used without being opened.
8091                     Changed it to access cur_items_rec.line_location_id.
8092 
8093 41. 12/03/2005      Bug 4210102. Added by LGOPALSA  Version - 115.6
8094                     (1) Added CVD and Customs education cess
8095 
8096 42. 8/05/2005        rchandan for bug#4333488. Version 116.1
8097                     The Invoice Distribution DFF is eliminated and a new global DFF is used to
8098                     maintain the functionality. From now the TDS tax, WCT tax and ESSI will not
8099                     be populated in the attribute columns of ap_invoice_distributions_all table
8100                     instead these will be populated in the global attribute columns. So the code changes are
8101                     made accordingly.
8102 
8103 43. 10-Jun-2005      rallamse for bug#  Version 116.2
8104                     All inserts into JAI_AP_MATCH_INV_TAXES table now include legal_entity_id as
8105                     part of R12 LE Initiative. The legal_entity_id is selected from ap_invoices_all in
8106                     cursor get_ven_info.
8107 
8108 44  23-Jun-2005     Brathod for Bug# 4445989, Version 120.0
8109                     Issue: Impact uptake on IL Product for AP_INVOICE_LINES_ALL Uptake
8110                     Resolution:- Code modified to consider ap_invoice_lines_all instead of
8111                     ap_invoice_distributions_all.
8112                     Following changes done
8113                       -  Code refering to ap_invoice_distributions_all modified to consider
8114                          invoice_id and invoice_line_number as unique combination
8115                       -  invoice line record is created in ap_invoice_lines_all where ever previously
8116                          distribution was created
8117                       -  Obsoleted JAI_CMN_FA_INV_DIST_ALL
8118                       -  Modified structure of JAI_AP_MATCH_INV_TAXES to incorporate invoice lines
8119                          and also the fields from JAI_CMN_FA_INV_DIST_ALL
8120                       -  Code modfied to insert appropriate fields in JAI_AP_MATCH_INV_TAXES
8121 
8122 45. 06-Jul-2005       Sanjikum for Bug#4474501
8123                       1) Commented the cursor - for_acct_id and corresponding open/fetch/close for the same.
8124                       2) Commented the call to function - jai_general_pkg.get_accounting_method
8125 
8126 46.  08-Dec-2005  Bug 4863208. Added by Lakshmi Gopalsami Version 120.2
8127                           (1) Added wfapproval_Status in insert to ap_invoice_lines_all
8128         (2) Derived the org_id for inserting into ap_invoice_distributions_all
8129              This is added in CURSOR for_dist_insertion
8130                           (3) Added values for match_status_flag, reversal_flag in insert
8131 
8132 47. 04-Feb-2008   JMEENA for bug#6780154
8133 
8134           Modify the insert statement of ap_invoice_lines_all to populate the po_distribution_id.
8135 
8136 
8137 48.  21-Feb-2008  JMEENA for bug#6835548
8138           Changed the value of lv_match_type from NOT MATCHED to NOT_MATCHED.
8139 
8140 49.  19-May-2008    JMEENA for bug#7008161
8141         Modified the insert statement of ap_invoice_distributions_all to populate the charge_applicable_to_dist_id
8142         based on v_assets_tracking_flag and removed the condition of account_type.
8143 
8144 
8145 Future Dependencies For the release Of this Object:-
8146 ==================================================
8147 (Please add a row in the section below only if your bug introduces a dependency due to spec change/
8148 A new call to a object/A datamodel change )
8149 
8150 ------------------------------------------------------------------------------------------------------
8151 Version       Bug       Dependencies (including other objects like files if any)
8152 -------------------------------------------------------------------------------------------------------
8153 616.1         3038566   ALTER Script  JAI_AP_MATCH_INV_TAXES Table is altered.
8154 
8155 115.1         3752887   Dependency on function jai_ap_interface_pkg.get_apportion_factor which is also added
8156                         in this patch. So no pre - req.
8157 
8158 115.3         4146708   Base Bug for Service + Cess Release.
8159 
8160                         Variable usage of jai_constants package.
8161 
8162                         Call to following,
8163                         jai_general_pkg.is_item_an_expense
8164                         jai_general_pkg.get_accounting_method
8165                         jai_rcv_trx_processing_pkg.get_accrue_on_receipt
8166                         jai_cmn_rgm_recording_pkg.get_account
8167 
8168 ------------------------------------------------------------------------------------------------------*/
8169 
8170 
8171   ln_user_id := fnd_global.user_id;
8172   ln_login_id := fnd_global.login_id;
8173 
8174   OPEN  get_ven_info(inv_id);
8175   FETCH get_ven_info INTO get_ven_info_rec;
8176   CLOSE get_ven_info;
8177 
8178 -- start added by bug#3206083
8179   if get_ven_info_rec.cancelled_date is not null then
8180   -- base invoice has been cancelled, no need to process tax.
8181   Fnd_File.put_line(Fnd_File.LOG, 'Base invoice has been cancelled, no need to process tax');
8182   return;
8183   end if;
8184   -- end added by bug#3206083
8185 
8186   -- Start added for bug#3354932
8187   open c_functional_currency(get_ven_info_rec.set_of_books_id);
8188   fetch c_functional_currency into v_functional_currency;
8189   close c_functional_currency;
8190   -- End added for bug#3354932
8191 
8192   OPEN  checking_for_packing_slip(get_ven_info_rec.vendor_id, get_ven_info_rec.vendor_site_id,
8193   get_ven_info_rec.org_id);
8194   FETCH checking_for_packing_slip INTO chk_for_pcking_slip_rec;
8195   CLOSE checking_for_packing_slip;
8196 
8197 
8198   OPEN  c_inv(inv_id);
8199   FETCH c_inv INTO v_batch_id, v_source;
8200   CLOSE c_inv;
8201 
8202   -- Get mininum invoice distribution line number, Brathod, Bug# 4445989
8203   lv_match_type := 'NOT_MATCHED'; --Changed from NOT MATCHED to NOT_MATCHED for bug#6835548 by JMEENA
8204   lv_misc       := 'MISCELLANEOUS';
8205   -- Bug 7249100. Added by Lakshmi Gopalsami
8206   lv_dist_class := 'PERMANENT';
8207 
8208   OPEN cur_get_min_dist_linenum (inv_id, pn_invoice_line_number);
8209   FETCH cur_get_min_dist_linenum INTO ln_min_dist_line_num;
8210   CLOSE cur_get_min_dist_linenum;
8211 
8212   -- Added by Brathod for Bug# 4445989
8213   OPEN  cur_get_max_line_number;
8214   FETCH cur_get_max_line_number INTO ln_max_lnno;
8215   CLOSE cur_get_max_line_number;
8216 
8217   OPEN  cur_get_max_ap_inv_line (cpn_max_line_num => ln_max_lnno );
8218   FETCH cur_get_max_ap_inv_line INTO rec_max_ap_lines_all;
8219   CLOSE cur_get_max_ap_inv_line;
8220   -- End of Bug 4445989
8221 
8222   IF v_source <> 'SUPPLEMENT' THEN
8223    -- Using pn_invoice_line_number instead of dist_line_no for Bug#4445989
8224   OPEN  cur_items(inv_id,pn_invoice_line_number,ln_min_dist_line_num);
8225   FETCH cur_items INTO cur_items_rec;
8226   CLOSE cur_items;
8227   END IF;
8228 
8229   OPEN  for_org_id(inv_id);
8230   FETCH for_org_id INTO for_org_id_rec;
8231   CLOSE for_org_id;
8232 
8233   /* added by ssumaith  */
8234 
8235   open  c_fnd_curr_precision(for_org_id_rec.invoice_currency_code);
8236   fetch c_fnd_curr_precision into ln_precision;
8237   close c_fnd_curr_precision;
8238 
8239   if ln_precision is null then
8240      ln_precision := 0;
8241   end if;
8242 
8243   /* ends here addition by ssumaith */
8244 
8245 
8246 
8247   /*OPEN  for_acct_id(NVL(for_org_id_rec.org_id, 0));
8248   FETCH for_acct_id INTO for_acct_id_rec;
8249   CLOSE for_acct_id;
8250   */
8251   --commented the above by Sanjikum For Bug#4474501, as this cursor is not being used anywhere
8252 
8253   IF v_source <> 'SUPPLEMENT' THEN
8254 
8255     BEGIN  --06-Mar-2002
8256       SELECT chart_of_accounts_id INTO caid
8257       FROM   gl_sets_of_books
8258       WHERE  set_of_books_id = cur_items_rec.set_of_books_id;
8259     EXCEPTION
8260       WHEN OTHERS THEN
8261         NULL;
8262     END ;-- end 06-Mar-2002
8263 
8264   END IF;
8265 
8266 
8267   BEGIN  --06-Mar-2002
8268 
8269 
8270     -- Cursor to select maximum line number from invoice lines for particular invoice
8271     -- Lock ap_invoice_lines_all
8272     UPDATE ap_invoice_lines_all
8273     SET    last_update_date = last_update_date
8274     WHERE invoice_id = inv_id;
8275 
8276     SELECT max(line_number)
8277     INTO   ln_inv_line_num
8278     FROM   ap_invoice_lines_all
8279     WHERE  invoice_id = inv_id;
8280     -- End of Bug# 4445989
8281     /*
8282     Commented by Brathod for Bug# 4445989
8283     SELECT NVL(DISTRIBUTION_LINE_NUMBER,1)
8284     INTO v_distribution_no
8285     FROM   AP_INVOICE_DISTRIBUTIONS_ALL
8286     WHERE  INVOICE_ID = inv_id
8287     AND    DISTRIBUTION_LINE_NUMBER =
8288       (
8289       SELECT MAX(DISTRIBUTION_LINE_NUMBER)
8290       FROM   AP_INVOICE_DISTRIBUTIONS_ALL
8291       WHERE  INVOICE_ID = inv_id
8292       )
8293     FOR UPDATE OF DISTRIBUTION_LINE_NUMBER;
8294     */
8295 
8296   EXCEPTION
8297     WHEN OTHERS THEN
8298     NULL;
8299   END ;--end 06-Mar-2002
8300 
8301   -- Using pn_invoice_line_number instead of dist_line_no for Bug#4445989
8302   OPEN  for_dist_insertion(inv_id,pn_invoice_line_number, ln_min_dist_line_num);
8303   FETCH for_dist_insertion INTO for_dist_insertion_rec;
8304   CLOSE for_dist_insertion;
8305 
8306 
8307   v_apportn_factor_for_item_line :=
8308   jai_ap_utils_pkg.get_apportion_factor(inv_id, pn_invoice_line_number);
8309   -- bug#3752887
8310 
8311 /* service Start */
8312 
8313   open c_rcv_transactions(rcv_tran_id);
8314   fetch c_rcv_transactions into r_rcv_transactions;
8315   close c_rcv_transactions;
8316 
8317   open c_po_lines_all(po_dist_id);
8318   fetch c_po_lines_all into r_po_lines_all;
8319   close c_po_lines_all;
8320 
8321   lv_is_item_an_expense :=
8322   jai_general_pkg.is_item_an_expense
8323   (
8324   p_organization_id   =>     r_rcv_transactions.organization_id,
8325   p_item_id           =>     r_po_lines_all.item_id
8326   );
8327 
8328   /*lv_accounting_method_option :=
8329   jai_general_pkg.get_accounting_method
8330   (
8331    p_org_id            =>    v_org_id,
8332    p_organization_id   =>    r_rcv_transactions.organization_id,
8333    p_sob_id            =>    for_dist_insertion_rec.set_of_books_id
8334   );*/
8335   --commented the above by Sanjikum for Bug#4474501
8336 
8337   lv_accrue_on_receipt_flag :=
8338   jai_rcv_trx_processing_pkg.get_accrue_on_receipt(p_po_distribution_id => po_dist_id);
8339 
8340   open c_jai_regimes(jai_constants.service_regime);
8341   fetch c_jai_regimes into r_jai_regimes;
8342   close c_jai_regimes;
8343 
8344   /* service End */
8345 
8346  /* Bug 5358788. Added by Lakshmi Gopalsami
8347   * Fetch VAT Regime id
8348   */
8349 
8350   OPEN  c_jai_regimes(jai_constants.vat_regime);
8351   FETCH  c_jai_regimes INTO  r_vat_regimes;
8352   CLOSE  c_jai_regimes;
8353 
8354   /* Bug 5401111. Added by Lakshmi Gopalsami
8355    | Get the account type for the distribution.
8356    | This is required to find out whether charge_applicable_to_dist
8357    */
8358    lv_account_type := get_gl_account_type
8359                     (for_dist_insertion_rec.dist_code_combination_id);
8360 
8361   IF p_rematch = 'PAY_ON_RECEIPT' THEN
8362 
8363     IF rcv_tran_id IS NOT NULL THEN
8364 
8365       /* Begin 5763527 */
8366       open  c_get_excise_costing_flag (cp_rcv_transaction_id => rcv_tran_id
8367                                      ,cp_organization_id =>  r_rcv_transactions.organization_id          --Added by Bgowrava for Bug#7503308
8368                                       ,cp_shipment_header_id     =>  p_shipment_header_id                           --Added by Bgowrava for Bug#7503308
8369                                       ,cp_txn_type        =>  'DELIVER'                                    --Added by Bgowrava for Bug#7503308
8370                                       ,cp_attribute1      =>  'CENVAT_COSTED_FLAG');                       --Added by Bgowrava for Bug#7503308
8371       fetch c_get_excise_costing_flag into lv_excise_costing_flag;
8372       close c_get_excise_costing_flag ;
8373       fnd_file.put_line(fnd_file.log, 'rcv_tran_id='||rcv_tran_id||',lv_excise_costing_flag='||lv_excise_costing_flag);
8374       /* End 5763527 */
8375 
8376 
8377       IF p_receipt_code  = 'RECEIPT' THEN
8378 
8379         OPEN count_dist_no(inv_id);
8380         FETCH count_dist_no INTO count_dist_no_rec;
8381         CLOSE count_dist_no;
8382 
8383         v_distribution_no := nvl(count_dist_no_rec, 0);
8384 
8385 
8386         FOR tax_lines1_rec IN tax_lines1_cur(rcv_tran_id,for_org_id_rec.vendor_id)  LOOP
8387             -- Added by Brathod for Bug# 4445989
8388             /* v_distribution_no := v_distribution_no + 1; */
8389             v_distribution_no :=1;
8390             -- ln_inv_line_num := ln_inv_line_num + 1;  -- 5763527, moved the increment to just before insert statement
8391             -- End Bug# 4445989
8392 
8393             r_service_regime_tax_type := null;
8394 
8395             /* 5763527 */
8396             ln_project_id           := null;
8397             ln_task_id              := null;
8398             lv_exp_type             := null;
8399             ld_exp_item_date        := null;
8400             ln_exp_organization_id  := null;
8401             lv_project_accounting_context := null;
8402             lv_pa_addition_flag := null;
8403 
8404 
8405       -- Bug 5358788. Added by Lakshmi Gopalsami
8406       r_vat_regime_tax_type := null;
8407 
8408        -- added, Kunkumar for Bug#5593895
8409             ln_base_amount := null ;
8410             if tax_lines1_rec.tax_type IN ( jai_constants.tax_type_value_added, jai_constants.tax_type_cst )  then
8411               ln_base_amount := jai_ap_utils_pkg.fetch_tax_target_amt
8412                                 (
8413                                   p_invoice_id          =>   inv_id ,
8414                                   p_line_location_id    =>   null ,
8415                                   p_transaction_id      =>   rcv_tran_id  ,
8416                                   p_parent_dist_id      =>   for_dist_insertion_rec.invoice_distribution_id ,
8417                                   p_tax_id              =>   tax_lines1_rec.tax_id
8418                                 ) ;
8419             end if ;
8420             -- ended,Kunkumar for Bug#5593895
8421 
8422             OPEN  c_tax(tax_lines1_rec.tax_id);
8423             FETCH c_tax INTO c_tax_rec;
8424             CLOSE c_tax;
8425 
8426 			  --Added by nprashar for bug # 13653889
8427 			  OPEN c_tax_curr_prec( tax_lines1_rec.tax_id ) ;
8428               FETCH c_tax_curr_prec INTO ln_tax_precision ;
8429               CLOSE c_tax_curr_prec ;
8430 
8431 
8432             /* Service Start */
8433             v_assets_tracking_flag := for_dist_insertion_rec.assets_tracking_flag;
8434             v_dist_code_combination_id := null;
8435 
8436             --initial the tax_type
8437             lv_tax_type := NULL;--added by eric for inclusive tax on 20-Dec,2007
8438 
8439             -- 5763527, Brathod
8440             -- Modified the if condition to consider partial recoverable lines previous logic was hadling only when it is 100
8441 
8442             if tax_lines1_rec.modvat_flag = 'Y'
8443             and nvl(c_tax_rec.mod_cr_percentage, 0) > 0 then
8444 
8445               --recoverable tax
8446               lv_tax_type := 'RE'; --added by eric for inclusive tax on 20-dec,2007
8447 
8448              /*  recoverable tax */
8449               v_assets_tracking_flag := 'N';
8450 
8451               open c_regime_tax_type(r_jai_regimes.regime_id, c_tax_rec.tax_type);
8452               fetch c_regime_tax_type into r_service_regime_tax_type;
8453               close c_regime_tax_type;
8454 
8455                 fnd_file.put_line(FND_FILE.LOG,
8456                   ' Service regime: '||r_service_regime_tax_type.tax_type);
8457 
8458               /* Bug 5358788. Added by Lakshmi Gopalsami
8459          * Fetched the details of VAT regime
8460          */
8461 
8462               OPEN  c_regime_tax_type(r_vat_regimes.regime_id, c_tax_rec.tax_type);
8463                 FETCH  c_regime_tax_type into r_vat_regime_tax_type;
8464               CLOSE  c_regime_tax_type;
8465 
8466             fnd_file.put_line(FND_FILE.LOG,
8467                        ' VAT regime: '||r_vat_regime_tax_type.tax_type);
8468 
8469               if r_service_regime_tax_type.tax_type is not null then
8470                 /* Service type of tax */
8471                 v_dist_code_combination_id := check_service_interim_account
8472                                               (
8473                                                 lv_accrue_on_receipt_flag,
8474                                                 lv_accounting_method_option,
8475                                                 lv_is_item_an_expense,
8476                                                 r_jai_regimes.regime_id,
8477                                                 v_org_id,
8478                                                 p_rematch,
8479                                                 c_tax_rec.tax_type,
8480             po_dist_id  --Added by JMEENA for bug#6833506
8481                                               );
8482                  fnd_file.put_line(FND_FILE.LOG,
8483                   ' Regime type , CCID '
8484             || r_service_regime_tax_type.tax_type
8485             ||', ' || v_dist_code_combination_id);
8486                /* Bug 5358788. Added by Lakshmi Gopalsami
8487           * Commented p_rematch and added validation for
8488           * VAT regime.
8489           */
8490       /*following elsif block modified for bug 6595773*/
8491             ELSIF r_vat_regime_tax_type.tax_type IS NOT NULL THEN
8492             -- p_rematch = 'PO_MATCHING' then
8493 
8494               v_dist_code_combination_id := for_dist_insertion_rec.dist_code_combination_id;
8495               fnd_file.put_line(FND_FILE.LOG,
8496                      ' Regime type , CCID '
8497          || r_vat_regime_tax_type.tax_type
8498                ||', ' || v_dist_code_combination_id);
8499 
8500               end if;
8501 
8502             else  /* 5763527 introduced for PROJETCS COSTING Impl */
8503               /* 5763527 */
8504               IF nvl(lv_accrue_on_receipt_flag , '#') <> 'Y' THEN -- Bug 6338371
8505                 ln_project_id           := p_project_id;
8506                 ln_task_id              := p_task_id;
8507                 lv_exp_type             := p_expenditure_type;
8508                 ld_exp_item_date        := p_expenditure_item_date;
8509                 ln_exp_organization_id  := p_expenditure_organization_id;
8510                 lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
8511                 lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
8512               END if;
8513 
8514               --non recoverable
8515               lv_tax_type := 'NR';  --added by eric for inclusive tax on 20-dec,2007
8516             end if; /*nvl(c_tax_rec.mod_cr_percentage, 0) = 100  and c_tax_rec.tax_account_id is not null*/
8517 
8518             /* Bug#4177452*/
8519             if v_dist_code_combination_id is null then
8520               v_dist_code_combination_id := for_dist_insertion_rec.dist_code_combination_id;
8521             end if;
8522 
8523             /* Bug#5763527 */
8524             Fnd_File.put_line(Fnd_File.LOG, 'c_tax_rec.tax_type='||c_tax_rec.tax_type  ||',lv_excise_costing_flag='||lv_excise_costing_flag);
8525             if upper(c_tax_rec.tax_type) like  '%EXCISE%' then
8526 
8527               if lv_excise_costing_flag = 'Y' then
8528                 IF nvl(lv_accrue_on_receipt_flag , '#') <> 'Y' -- Bug 6338371
8529                 then
8530                   ln_project_id           := p_project_id;
8531                   ln_task_id              := p_task_id;
8532                   lv_exp_type             := p_expenditure_type;
8533                   ld_exp_item_date        := p_expenditure_item_date;
8534                   ln_exp_organization_id  := p_expenditure_organization_id;
8535                   lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
8536                   lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
8537                 END if;
8538                 v_dist_code_combination_id    := for_dist_insertion_rec.dist_code_combination_id;
8539               end if;
8540             end if;
8541 
8542             /* End# 5763527 */
8543             /* Service End */
8544 
8545               -- Start for bug#3752887
8546               v_tax_amount := tax_lines1_rec.tax_amount * v_apportn_factor_for_item_line;
8547 
8548 
8549               if for_org_id_rec.invoice_currency_code <> tax_lines1_rec.currency then
8550                 v_tax_amount := v_tax_amount / for_org_id_rec.exchange_rate;
8551               end if;
8552               -- End for bug#3752887
8553 
8554             --
8555             -- Begin 5763527
8556             --
8557             ln_rec_tax_amt  := null;
8558             ln_nrec_tax_amt := null;
8559             ln_lines_to_insert := 1; -- Loop controller to insert more than one lines for partially recoverable tax lines in PO
8560 
8561             --added by Eric for inclusive tax on 20-dec-2007,begin
8562             -------------------------------------------------------------------------------------
8563             --exclusive tax or inlcusive tax without project info,  ln_lines_to_insert := 1;
8564             --inclusive recoverable tax with project info,  ln_lines_to_insert := 2;
8565             --PR(partially recoverable) tax is processed in another logic
8566 
8567             IF ( NVL(tax_lines1_rec.inc_tax_flag,'N') = 'N'
8568                  OR ( NVL(tax_lines1_rec.inc_tax_flag,'N') = 'Y'
8569                       AND p_project_id IS NULL
8570                     )
8571                )
8572             THEN
8573               ln_lines_to_insert := 1;
8574             ELSIF ( NVL(tax_lines1_rec.inc_tax_flag,'N') = 'Y'
8575                     AND p_project_id IS NOT NULL
8576                     AND lv_tax_type = 'RE'
8577                   )
8578             THEN
8579               ln_lines_to_insert := 2;
8580             END IF;--( NVL(tax_lines1_rec.inc_tax_flag,'N') = 'N' OR )
8581             -------------------------------------------------------------------------------------
8582             --added by Eric for inclusive tax on 20-dec-2007,end
8583 
8584             Fnd_File.put_line(Fnd_File.LOG, 'tax_lines1_rec.modvat_flag ='||tax_lines1_rec.modvat_flag ||',c_tax_rec.mod_cr_percentage='||c_tax_rec.mod_cr_percentage);
8585 
8586             if tax_lines1_rec.modvat_flag = jai_constants.YES
8587             and nvl(c_tax_rec.mod_cr_percentage, -1) > 0
8588             and nvl(c_tax_rec.mod_cr_percentage, -1) < 100
8589             then
8590               --
8591               -- Tax line is for partial Recoverable tax.  Hence split amount into two parts, Recoverable and Non-Recoverable
8592               -- and instead of one line, two lines needs to be inserted.
8593               -- For ordinary lines (with modvat_flag = 'N' or with mod_cr_percentage = 100) there will be only one line inserted
8594               --
8595               ln_lines_to_insert := 2 *ln_lines_to_insert; --changed by eric for inclusive tax on Jan 4,2008
8596         ln_rec_tax_amt  :=  round ( nvl(v_tax_amount,0) * (c_tax_rec.mod_cr_percentage/100) , c_tax_rec.rounding_factor) ;
8597         ln_nrec_tax_amt :=  round ( nvl(v_tax_amount,0) - nvl(ln_rec_tax_amt,0) , c_tax_rec.rounding_factor) ;
8598         /* Added Tax rounding based on tax rounding factor for bug 6761425 by vumaasha */
8599               lv_tax_type := 'PR';  --changed by eric for inclusive tax on Jan 4,2008
8600 
8601             end if;
8602             fnd_file.put_line(fnd_file.log, 'ln_lines_to_insert='||ln_lines_to_insert||
8603                                             ',ln_rec_tax_amt='||ln_rec_tax_amt          ||
8604                                             ',ln_nrec_tax_amt='||ln_nrec_tax_amt
8605                              );
8606 
8607             --
8608             --  If a line has a partially recoverable tax the following loop will be executed twice.  First line will always be for a
8609             --  non recoverable tax amount and the second line will be for a recoverable tax amount
8610             --  For ordinary lines (with modvat_flag = 'N' or with mod_cr_percentage = 100 fully recoverable) the variable
8611             --  ln_lines_to_insert will have value of 1 and hence only one line will be inserted with full tax amount
8612             --
8613 
8614             for line in 1..ln_lines_to_insert
8615             loop
8616               /* commented out by eric for inclusive tax
8617               if line = 1 then
8618 
8619                 v_tax_amount  := nvl(ln_rec_tax_amt, v_tax_amount);
8620                 lv_modvat_flag := tax_lines1_rec.modvat_flag ;
8621 
8622               elsif line = 2 then
8623 
8624                 v_tax_amount := ln_nrec_tax_amt;
8625                 v_assets_tracking_flag := jai_constants.YES;
8626                 lv_modvat_flag := jai_constants.NO ;
8627 
8628                 --
8629                 -- This is a non recoverable line hence the tax amounts should be added into the project costs by populating
8630                 -- projects related columns so that PROJECTS can consider this line for Project Costing
8631                 --
8632                 IF nvl(lv_accrue_on_receipt_flag , '#') <> 'Y' then -- Bug 6338371
8633                   ln_project_id           := p_project_id;
8634                   ln_task_id              := p_task_id;
8635                   lv_exp_type             := p_expenditure_type;
8636                   ld_exp_item_date        := p_expenditure_item_date;
8637                   ln_exp_organization_id  := p_expenditure_organization_id;
8638                   lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
8639                   lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
8640                 END if;
8641                 -- For non recoverable line charge account should be same as of the parent line
8642                 v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id;
8643 
8644               end if;
8645               */
8646               --
8647               -- End 5763527
8648               --
8649 
8650               --added by Eric for inclusive tax on 20-dec-2007,begin
8651               -------------------------------------------------------------------------------------
8652               IF (NVL(tax_lines1_rec.inc_tax_flag,'N') = 'N')--exclusive case
8653               THEN
8654                 IF line = 1 then
8655 
8656                   v_tax_amount  := nvl(ln_rec_tax_amt, v_tax_amount);
8657                   lv_tax_line_amount:=  v_tax_amount       ;   --added by eric for inclusive tax
8658                   lv_modvat_flag := tax_lines1_rec.modvat_flag ;
8659 
8660                   lv_ap_line_to_inst_flag  := 'Y'; --added by eric for inclusive tax
8661                   lv_tax_line_to_inst_flag := 'Y'; --added by eric for inclusive tax
8662                 ELSIF line = 2 then
8663 
8664                   v_tax_amount             := ln_nrec_tax_amt;
8665                   lv_tax_line_amount       :=  v_tax_amount  ; --added by eric for inclusive tax
8666                   lv_ap_line_to_inst_flag  := 'Y';             --added by eric for inclusive tax
8667                   lv_tax_line_to_inst_flag := 'Y';             --added by eric for inclusive tax
8668 
8669 
8670                   IF for_dist_insertion_rec.assets_tracking_flag = jai_constants.YES THEN
8671                     v_assets_tracking_flag := jai_constants.YES;
8672                   END IF;
8673 
8674                   lv_modvat_flag := jai_constants.NO ;
8675 
8676                   --
8677                   -- This is a non recoverable line hence the tax amounts should be added into the project costs by populating
8678                   -- projects related columns so that PROJECTS can consider this line for Project Costing
8679                   --
8680                   IF nvl(lv_accrue_on_receipt_flag,'#') <> 'Y' THEN -- Bug 6338371
8681                     ln_project_id           := p_project_id;
8682                     ln_task_id              := p_task_id;
8683                     lv_exp_type             := p_expenditure_type;
8684                     ld_exp_item_date        := p_expenditure_item_date;
8685                     ln_exp_organization_id  := p_expenditure_organization_id;
8686                     lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
8687                     lv_pa_addition_flag         := for_dist_insertion_rec.pa_addition_flag;
8688                   END if;
8689 
8690                   -- For non recoverable line charge account should be same as of the parent line
8691                   v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id;
8692 
8693                 END IF; --line = 1
8694               ELSIF (NVL(tax_lines1_rec.inc_tax_flag,'N') = 'Y')       --inclusive case
8695               THEN
8696                 IF( lv_tax_type ='PR')
8697                 THEN
8698                   IF ( line = 1 )
8699                   THEN
8700                     --recoverable part
8701                     v_tax_amount       := ln_rec_tax_amt ;
8702                     lv_tax_line_amount := v_tax_amount   ;   --added by eric for inclusive tax
8703                     lv_modvat_flag     := tax_lines1_rec.modvat_flag  ;
8704                     lv_ap_line_to_inst_flag  := 'N';         --added by eric for inclusive tax
8705                     lv_tax_line_to_inst_flag := 'Y';         --added by eric for inclusive tax
8706 
8707                     --non recoverable part
8708                   ELSIF ( line = 2 )
8709                   THEN
8710                     v_tax_amount       := ln_nrec_tax_amt  ;
8711                     lv_tax_line_amount :=  v_tax_amount    ;   --added by eric for inclusive tax
8712                     lv_modvat_flag     := jai_constants.NO ;
8713                     lv_ap_line_to_inst_flag  := 'N';           --added by eric for inclusive tax
8714                     lv_tax_line_to_inst_flag := 'Y';           --added by eric for inclusive tax
8715 
8716                     --recoverable part without project infor
8717                   ELSIF ( line = 3 )
8718                   THEN
8719                     v_tax_amount                  := ln_rec_tax_amt;
8720                     lv_tax_line_amount            := NULL;
8721 
8722                     lv_ap_line_to_inst_flag  := 'Y';             --added by eric for inclusive tax
8723                     lv_tax_line_to_inst_flag := 'N';             --added by eric for inclusive tax
8724 
8725                     --Project information
8726                     ln_project_id                 := NULL;
8727                     ln_task_id                    := NULL;
8728                     lv_exp_type                   := NULL;
8729                     ld_exp_item_date              := NULL;
8730                     ln_exp_organization_id        := NULL;
8731                     lv_project_accounting_context := NULL;
8732                     lv_pa_addition_flag           := NULL;
8733 
8734                     -- For inclusive recoverable line charge account should be same as of the parent line
8735                     v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id ;
8736 
8737                     --recoverable part in negative amount with project infor
8738                   ELSIF ( line = 4 )
8739                   THEN
8740                     v_tax_amount                  := NVL(ln_rec_tax_amt, v_tax_amount)* -1;
8741                     lv_tax_line_amount            := NULL;
8742                     lv_ap_line_to_inst_flag  := 'Y';             --added by eric for inclusive tax
8743                     lv_tax_line_to_inst_flag := 'N';             --added by eric for inclusive tax
8744 
8745                     --Project information
8746                     ln_project_id                 := p_project_id;
8747                     ln_task_id                    := p_task_id;
8748                     lv_exp_type                   := p_expenditure_type;
8749                     ld_exp_item_date              := p_expenditure_item_date;
8750                     ln_exp_organization_id        := p_expenditure_organization_id;
8751                     lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
8752                     lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
8753 
8754                     -- For inclusive recoverable line charge account should be same as of the parent line
8755                     v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id ;
8756                   END IF; --line = 1
8757                 ELSIF ( lv_tax_type = 'RE'
8758                         AND p_project_id IS NOT NULL
8759                       )
8760                 THEN
8761                   --recoverable tax without project infor
8762                   IF ( line = 1 )
8763                   THEN
8764                     v_tax_amount       :=  v_tax_amount  ;
8765                     lv_tax_line_amount :=  v_tax_amount  ;    --added by eric for inclusive tax
8766                     lv_modvat_flag     :=  tax_lines1_rec.modvat_flag ;
8767                     lv_ap_line_to_inst_flag  := 'Y';          --added by eric for inclusive tax
8768                     lv_tax_line_to_inst_flag := 'Y';          --added by eric for inclusive tax
8769 
8770                     ln_project_id                 := NULL;
8771                     ln_task_id                    := NULL;
8772                     lv_exp_type                   := NULL;
8773                     ld_exp_item_date              := NULL;
8774                     ln_exp_organization_id        := NULL;
8775                     lv_project_accounting_context := NULL;
8776                     lv_pa_addition_flag           := NULL;
8777                     v_dist_code_combination_id    := for_dist_insertion_rec.dist_code_combination_id ;
8778                   --recoverable tax in negative amount with project infor
8779                   ELSIF ( line = 2 )
8780                   THEN
8781                     v_tax_amount       :=  v_tax_amount * -1;
8782                     lv_tax_line_amount :=  NULL ;   --added by eric for inclusive tax
8783                     lv_modvat_flag     :=  tax_lines1_rec.modvat_flag  ;
8784 
8785                     ln_project_id                 := p_project_id;
8786                     ln_task_id                    := p_task_id;
8787                     lv_exp_type                   := p_expenditure_type;
8788                     ld_exp_item_date              := p_expenditure_item_date;
8789                     ln_exp_organization_id        := p_expenditure_organization_id;
8790                     lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
8791                     lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
8792                     v_dist_code_combination_id    := for_dist_insertion_rec.dist_code_combination_id ;
8793                   END IF;
8794                 --  ELSIF ( lv_tax_type <> 'PR' AND p_project_id IS NULL )
8795                 --  THEN
8796                 ELSE -- eric removed the above criteria for bug 6888665 and 6888209
8797                   Fnd_File.put_line(Fnd_File.LOG, 'NOT Inclusive PR Tax,NOT Inclusive RE for project ');
8798                   --The case process the inclusive NR tax and inclusive RE tax not for project
8799 
8800                   lv_tax_line_amount := v_tax_amount   ;   --added by eric for inclusive tax
8801                   lv_modvat_flag     := tax_lines1_rec.modvat_flag  ;
8802                   lv_ap_line_to_inst_flag  := 'N';         --added by eric for inclusive tax
8803                   lv_tax_line_to_inst_flag := 'Y';         --added by eric for inclusive tax
8804 
8805                 END IF;--( tax_type ='PR' and (ln_lines_to_insert =4 OR )
8806               END IF; --(NVL(r_tax_lines_r ec.inc_tax_flag,'N') = 'N')
8807               -------------------------------------------------------------------------------------------
8808               --added by Eric for inclusive tax on 20-dec-2007,end
8809 
8810               --insert exclusive tax to the ap tables
8811               --or insert recoverable inclusive tax with project information the ap tables
8812 
8813 
8814               --1.Only exlusive taxes or inclusive taxes with project information
8815               --  are allowed to be inserted into Ap Lines and Dist Lines
8816 
8817               --2.All taxes need to be inserted into jai tax tables. Futher the
8818               --  partially recoverable tax need to be splitted into 2 lines
8819 
8820               --added by Eric for inclusive tax on 20-dec-2007,begin
8821               ------------------------------------------------------------------------
8822               IF ( NVL(lv_ap_line_to_inst_flag,'N')='Y')
8823               THEN
8824                 ln_inv_line_num := ln_inv_line_num + 1; --added by eric for inlcusive tax
8825               ---------------------------------------------------------------------------
8826               --added by Eric for inclusive tax on 20-dec-2007,end
8827 
8828                 INSERT INTO ap_invoice_lines_all
8829                 (
8830                   INVOICE_ID
8831                 , LINE_NUMBER
8832                 , LINE_TYPE_LOOKUP_CODE
8833                 , DESCRIPTION
8834                 , ORG_ID
8835                 , MATCH_TYPE
8836     , DEFAULT_DIST_CCID --Changes by nprashar for bug #6995437
8837                 , ACCOUNTING_DATE
8838                 , PERIOD_NAME
8839                 , DEFERRED_ACCTG_FLAG
8840                 , DEF_ACCTG_START_DATE
8841                 , DEF_ACCTG_END_DATE
8842                 , DEF_ACCTG_NUMBER_OF_PERIODS
8843                 , DEF_ACCTG_PERIOD_TYPE
8844                 , SET_OF_BOOKS_ID
8845                 , AMOUNT
8846                 , WFAPPROVAL_STATUS
8847                 , CREATION_DATE
8848                 , CREATED_BY
8849                 , LAST_UPDATED_BY
8850                 , LAST_UPDATE_DATE
8851                 , LAST_UPDATE_LOGIN
8852                 /* 5763527 */
8853                 , project_id
8854                 , task_id
8855                 , expenditure_type
8856                 , expenditure_item_date
8857                 , expenditure_organization_id
8858                 /* End 5763527 */
8859                 ,po_distribution_id --Added for bug#6780154
8860                 )
8861                 VALUES
8862                 (
8863                   inv_id
8864                 , ln_inv_line_num
8865                 , lv_misc
8866                 , c_tax_rec.tax_name
8867                 , v_org_id
8868                 , lv_match_type
8869     , v_dist_code_combination_id
8870                 , rec_max_ap_lines_all.accounting_date
8871                 , rec_max_ap_lines_all.period_name
8872                 , rec_max_ap_lines_all.deferred_acctg_flag
8873                 , rec_max_ap_lines_all.def_acctg_start_date
8874                 , rec_max_ap_lines_all.def_acctg_end_date
8875                 , rec_max_ap_lines_all.def_acctg_number_of_periods
8876                 , rec_max_ap_lines_all.def_acctg_period_type
8877                 , rec_max_ap_lines_all.set_of_books_id
8878                 , ROUND(ROUND(v_tax_amount,ln_tax_precision),ln_precision) --Added by nprashar for bug # 13653889
8879                 , rec_max_ap_lines_all.wfapproval_status  -- Bug 4863208
8880                 , sysdate
8881                 , ln_user_id
8882                 , ln_user_id
8883                 , sysdate
8884                 , ln_login_id
8885                 /* 5763527 */
8886                 , ln_project_id
8887                 , ln_task_id
8888                 , lv_exp_type
8889                 , ld_exp_item_date
8890                 , ln_exp_organization_id
8891           ,po_dist_id --Added for bug#6780154
8892                 );
8893 
8894 
8895                 -- Start bug#3332988
8896                 open c_get_invoice_distribution;
8897                 fetch c_get_invoice_distribution into v_invoice_distribution_id;
8898                 close c_get_invoice_distribution;
8899                 -- End bug#3332988
8900                 fnd_file.put_line(FND_FILE.LOG, ' Invoice distribution id '|| v_invoice_distribution_id);
8901 
8902                 INSERT INTO ap_invoice_distributions_all
8903                 (
8904                 accounting_date,
8905                 accrual_posted_flag,
8906                 assets_addition_flag,
8907                 assets_tracking_flag,
8908                 cash_posted_flag,
8909                 distribution_line_number,
8910                 dist_code_combination_id,
8911                 invoice_id,
8912                 last_updated_by,
8913                 last_update_date,
8914                 line_type_lookup_code,
8915                 period_name,
8916                 set_of_books_id,
8917                 amount,
8918                 base_amount,
8919                 batch_id,
8920                 created_by,
8921                 creation_date,
8922                 description,
8923                 last_update_login,
8924                 match_status_flag,
8925                 posted_flag,
8926                 rate_var_code_combination_id,
8927                 reversal_flag,
8928                 program_application_id,
8929                 program_id,
8930                 program_update_date,
8931                 accts_pay_code_combination_id,
8932                 invoice_distribution_id,
8933                 quantity_invoiced,
8934                 org_id,
8935                 po_distribution_id ,
8936                 rcv_transaction_id,
8937                 matched_uom_lookup_code,
8938                 invoice_line_number
8939                 -- Bug 5401111. Added by Lakshmi Gopalsami
8940                 ,charge_applicable_to_dist_id --5763527
8941                 , project_id
8942                 , task_id
8943                 , expenditure_type
8944                 , expenditure_item_date
8945                 , expenditure_organization_id
8946                 , project_accounting_context
8947                 , pa_addition_flag
8948                -- Bug 7249100. Added by Lakshmi Gopalsami
8949                 ,distribution_class
8950         ,asset_book_type_code /* added for bug 8406404 by vumaasha */
8951                 )
8952                 VALUES
8953                 (
8954                 rec_max_ap_lines_all.accounting_date,
8955                 'N', --for_dist_insertion_rec.accrual_posted_flag,
8956                 for_dist_insertion_rec.assets_addition_flag,
8957                 v_assets_tracking_flag, -- for_dist_insertion_rec.assets_tracking_flag, bug#2851123
8958                 'N',
8959                 v_distribution_no,
8960                 v_dist_code_combination_id,
8961                 inv_id,
8962                 ln_user_id,
8963                 SYSDATE,
8964                 lv_misc,
8965                 rec_max_ap_lines_all.period_name,
8966                 rec_max_ap_lines_all.set_of_books_id,
8967                 round(round(v_tax_amount,ln_tax_precision),ln_precision), --Added by nprashar for bug # 13653889
8968                 ROUND(ROUND(v_tax_amount * for_dist_insertion_rec.exchange_rate,ln_tax_precision), ln_precision), --Added by nprashar for bug # 13653889
8969                 v_batch_id,
8970                 ln_user_id,
8971                 SYSDATE,
8972                 c_tax_rec.tax_name,
8973                 ln_login_id,
8974                 for_dist_insertion_rec.match_status_flag , -- Bug 4863208
8975                 'N',
8976                 NULL,
8977                 for_dist_insertion_rec.reversal_flag,  -- Bug 4863208
8978                 for_dist_insertion_rec.program_application_id,
8979                 for_dist_insertion_rec.program_id,
8980                 for_dist_insertion_rec.program_update_date,
8981                 for_dist_insertion_rec.accts_pay_code_combination_id,
8982                 v_invoice_distribution_id, -- BUG#3332988 ap_invoice_distributions_s.NEXTVAL,
8983                decode(v_assets_tracking_flag, 'Y', NULL, 0),/* Modified for bug 8406404 by vumaasha */
8984                 for_dist_insertion_rec.org_id, -- Bug 4863208
8985                  --po_dist_id,12434160
8986 			for_dist_insertion_rec.po_distribution_id, --12434160
8987                 rcv_tran_id,
8988                 for_dist_insertion_rec.matched_uom_lookup_code,
8989                 ln_inv_line_num
8990                 /* Bug 5361931. Added by Lakshmi Gopalsami
8991                  * Passed ln_inv_line_num instead of
8992                  * pn_invoice_line_number
8993                  */
8994                  -- Bug 5401111. Added by Lakshmi Gopalsami
8995                  ,decode(v_assets_tracking_flag,'N',
8996                          NULL,for_dist_insertion_rec.invoice_distribution_id)
8997 /*Commented the account_type condition for bug#7008161 by JMEENA
8998        decode(lv_account_type, 'A',
8999               for_dist_insertion_rec.invoice_distribution_id,NULL)
9000             )
9001 */
9002                 /* 5763527 */
9003                 , ln_project_id
9004                 , ln_task_id
9005                 , lv_exp_type
9006                 , ld_exp_item_date
9007                 , ln_exp_organization_id
9008                 , lv_project_accounting_context
9009                 , lv_pa_addition_flag
9010                 -- Bug 7249100. Added by Lakshmi Gopalsami
9011                ,lv_dist_class
9012         , decode(v_assets_tracking_flag,'Y',for_dist_insertion_rec.asset_book_type_code,NULL) /* added for bug 8406404 by vumaasha */
9013                 );
9014               --added by Eric for inclusive tax on 20-dec-2007,begin
9015               -------------------------------------------------------------
9016               END IF; --( NVL(r_tax_lines_rec.inc_tax_flag,'N') = 'N' or )
9017               -------------------------------------------------------------
9018               --added by Eric for inclusive tax on 20-dec-2007,end
9019 
9020 
9021 
9022               --added by Eric for inclusive tax on 20-dec-2007,begin
9023               -----------------------------------------------------------------
9024               IF  (NVL(lv_tax_line_to_inst_flag,'N') = 'Y')
9025               THEN
9026               -----------------------------------------------------------------
9027               --added by Eric for inclusive tax on 20-dec-2007,end
9028 
9029                 INSERT INTO JAI_AP_MATCH_INV_TAXES
9030                 (
9031                 tax_distribution_id,
9032                 exchange_rate_variance,
9033                 assets_tracking_flag,
9034                 invoice_id,
9035                 po_header_id,
9036                 po_line_id,
9037                 line_location_id,
9038                 set_of_books_id,
9039                 --org_id,
9040                 exchange_rate,
9041                 exchange_rate_type,
9042                 exchange_date,
9043                 currency_code,
9044                 code_combination_id,
9045                 last_update_login,
9046                 creation_date,
9047                 created_by,
9048                 last_update_date,
9049                 last_updated_by,
9050                 acct_pay_code_combination_id,
9051                 accounting_date,
9052                 tax_id,
9053                 tax_amount,
9054                 base_amount,
9055                 chart_of_accounts_id,
9056                 distribution_line_number,
9057                 --project_id,
9058                 --task_id,
9059                 po_distribution_id,  -- added  by bug#3038566
9060                 parent_invoice_distribution_id,  -- added  by bug#3038566
9061                 legal_entity_id -- added by rallamse bug#
9062                 ,INVOICE_LINE_NUMBER  -- Added by Brathod, Bug# 4445989
9063                 ,INVOICE_DISTRIBUTION_ID -------|
9064                 ,PARENT_INVOICE_LINE_NUMBER-----|
9065                 ,RCV_TRANSACTION_ID
9066                 ,LINE_TYPE_LOOKUP_CODE
9067                 , recoverable_flag -- 5763527
9068                 ,line_no            -- Bug 5553150
9069 
9070                 )
9071                 VALUES
9072                 (
9073                 JAI_AP_MATCH_INV_TAXES_S.NEXTVAL,
9074                 null,--kunkumar for forward porting to R12
9075                 v_assets_tracking_flag , -- 'N', bug#2851123
9076                 inv_id,
9077                 cur_items_rec.po_header_id,
9078                 cur_items_rec.po_line_id,
9079                 cur_items_rec.line_location_id,
9080                 cur_items_rec.set_of_books_id,
9081                 --cur_items_rec.org_id,
9082                 cur_items_rec.rate,
9083                 cur_items_rec.rate_type,
9084                 cur_items_rec.rate_date,
9085                 cur_items_rec.currency_code,
9086                 /* Bug 5358788. Added by Lakshmi Gopalsami. Commented
9087                  * c_tax_rec.tax_account_id and v_dist_code_combination_id
9088                 */
9089                 v_dist_code_combination_id,
9090                 cur_items_rec.last_update_login,
9091                 cur_items_rec.creation_date,
9092                 cur_items_rec.created_by,
9093                 cur_items_rec.last_update_date,
9094                 cur_items_rec.last_updated_by,
9095                 v_dist_code_combination_id,
9096                 cur_items_rec.invoice_date,
9097                 tax_lines1_rec.tax_id,
9098                 /*commented out by eric for inclusive tax
9099                 round(v_tax_amount,ln_precision), -- ROUND(v_tax_amount, 2), by Aparajita bug#2567799
9100                 */
9101                 ROUND(ROUND(lv_tax_line_amount,ln_tax_precision),ln_precision), --Added by nprashar for bug # 13653889
9102                 nvl(ln_base_amount,ROUND(ROUND(tax_lines1_rec.tax_amount,ln_tax_precision), ln_precision)), --Added by nprashar for bug # 13653889
9103                 caid,
9104                 v_distribution_no,
9105                 --p_project_id,
9106                 --p_task_id,
9107                 po_dist_id, -- added  by bug#3038566
9108                 for_dist_insertion_rec.invoice_distribution_id, -- added  by bug#3038566
9109                 get_ven_info_rec.legal_entity_id -- added by rallamse bug#
9110                 --ln_inv_line_num,deleted by Eric for inclusive tax
9111                 --, v_invoice_distribution_id,deleted by Eric for inclusive tax
9112 
9113                 --added by Eric for inclusive tax on 20-dec-2007,begin
9114                 ----------------------------------------------------------------
9115                 , DECODE ( NVL(tax_lines1_rec.inc_tax_flag,'N')
9116                          , 'N',ln_inv_line_num
9117                          , 'Y',pn_invoice_line_number
9118                          )
9119                , NVL(v_invoice_distribution_id,for_dist_insertion_rec.invoice_distribution_id)
9120                 -----------------------------------------------------------------
9121                 --added by Eric for inclusive tax on 20-dec-2007,end
9122 
9123                 , pn_invoice_line_number
9124                 , rcv_tran_id
9125                 , lv_misc
9126                 , lv_modvat_flag -- 5763527
9127                 , tax_lines1_rec.tax_line_no  -- added, Harshita for Bug 5553150
9128                 );
9129              --added by Eric for inclusive tax on 20-dec-2007,begin
9130              -----------------------------------------------------------------
9131              END IF;--  (NVL(lv_tax_line_to_inst_flag,'N') = 'Y')
9132              -----------------------------------------------------------------
9133              --added by Eric for inclusive tax on 20-dec-2007,end
9134 
9135 
9136 
9137               /*  Bug 46863208. Added by Lakshmi Gopalsami
9138              Commented the MRC call
9139               insert_mrc_data(v_invoice_distribution_id); -- bug#3332988
9140               */
9141 
9142               /* Commented out by Eric for bug 8345512 on 13-Jul-2009
9143 
9144                 cum_tax_amt := cum_tax_amt + round(v_tax_amount, ln_precision)
9145               */
9146 
9147               --Modified by Eric for bug 8345512 on 13-Jul-2009,begin
9148               ------------------------------------------------------------------
9149               IF NVL(tax_lines1_rec.inc_tax_flag,'N') ='N'
9150               THEN
9151                 cum_tax_amt := cum_tax_amt + round(round(v_tax_amount,ln_tax_precision), ln_precision);  -- ROUND(v_tax_amount, 2),bug#2567799  /*Bug 14386866 */
9152               END IF;
9153              ------------------------------------------------------------------
9154              --Modified by Eric for bug 8345512 on 13-Jul-2009,end
9155 
9156               /* Obsoleted as part of R12 , Bug# 4445989
9157               INSERT INTO JAI_CMN_FA_INV_DIST_ALL
9158               (
9159               invoice_id,
9160               invoice_distribution_id,
9161               set_of_books_id,
9162               batch_id,
9163               po_distribution_id,
9164               rcv_transaction_id,
9165               dist_code_combination_id,
9166               accounting_date,
9167               assets_addition_flag,
9168               assets_tracking_flag,
9169               distribution_line_number,
9170               line_type_lookup_code,
9171               amount,
9172               description,
9173               match_status_flag,
9174               quantity_invoiced
9175               )
9176               VALUES
9177               (
9178               inv_id,
9179               ap_invoice_distributions_s.CURRVAL,
9180               for_dist_insertion_rec.set_of_books_id,
9181               v_batch_id,
9182               for_dist_insertion_rec.po_distribution_id,
9183               rcv_tran_id,
9184               v_dist_code_combination_id,
9185               for_dist_insertion_rec.accounting_date,
9186               for_dist_insertion_rec.assets_addition_flag,
9187               v_assets_tracking_flag , -- for_dist_insertion_rec.assets_tracking_flag, bug#2851123
9188               v_distribution_no,
9189               'MISCELLANEOUS',
9190               round(v_tax_amount,ln_precision), -- ROUND(v_tax_amount, 2), by Aparajita bug#2567799
9191               c_tax_rec.tax_name,
9192               NULL,
9193               NULL
9194               ); */
9195 
9196               END LOOP; --> for line in ... 5763527
9197         END LOOP; --tax_lines1_rec IN tax_lines1_cur(rcv_tran_id,for_org_id_rec.vendor_id)
9198 
9199         v_update_payment_schedule:=update_payment_schedule(cum_tax_amt); -- bug#3218978
9200 
9201 --        cum_tax_amt := round(cum_Tax_amt,ln_precision);
9202         UPDATE  ap_invoices_all
9203         SET   invoice_amount = invoice_amount   + cum_tax_amt,
9204             approved_amount = approved_amount  + cum_tax_amt,
9205             pay_curr_invoice_amount =  pay_curr_invoice_amount + cum_tax_amt,
9206             amount_applicable_to_discount =  amount_applicable_to_discount + cum_tax_amt,
9207             payment_status_flag = decode(payment_status_flag, 'Y', 'P', payment_status_flag)
9208         WHERE  invoice_id = inv_id;
9209 
9210         -- start added for bug#3354932
9211         if for_org_id_rec.invoice_currency_code <> v_functional_currency then
9212           -- invoice currency is not the functional currency.
9213           update ap_invoices_all
9214           set    base_amount = invoice_amount  * exchange_rate
9215           where  invoice_id = inv_id;
9216         end if;
9217         -- end added for bug#3354932
9218 
9219      /* Bug 4863208. Added by Lakshmi Gopalsami
9220          Commented the MRC call
9221         update_mrc_data; -- bug#3332988
9222      */
9223 
9224 
9225       ELSIF p_receipt_code = 'PACKING_SLIP' THEN  -- IF p_receipt_code  = 'RECEIPT' THEN
9226 
9227 
9228         OPEN count_dist_no(inv_id);
9229         FETCH count_dist_no INTO count_dist_no_rec;
9230         CLOSE count_dist_no;
9231 
9232         v_distribution_no := nvl(count_dist_no_rec, 0);
9233 
9234 
9235         FOR tax_lines1_rec IN tax_lines1_cur(rcv_tran_id,for_org_id_rec.vendor_id)  LOOP
9236 
9237            -- Added by Brathod for Bug# 4445989
9238            /* v_distribution_no := v_distribution_no + 1; */
9239            v_distribution_no :=1;
9240            -- ln_inv_line_num := ln_inv_line_num + 1; -- 5763527, moved the increment to just before the insert statement
9241            -- End Bug# 4445989
9242 
9243           r_service_regime_tax_type := null;
9244 
9245           /* 5763527 */
9246           ln_project_id           := null;
9247           ln_task_id              := null;
9248           lv_exp_type             := null;
9249           ld_exp_item_date        := null;
9250           ln_exp_organization_id  := null;
9251           lv_project_accounting_context := null;
9252           lv_pa_addition_flag := null;
9253 
9254 
9255     -- Bug 5358788. Added by Lakshmi Gopalsami
9256     r_vat_regime_tax_type := null;
9257 
9258           -- added, Kunkumar for bug#5593895
9259           ln_base_amount := null ;
9260           if tax_lines1_rec.tax_type IN ( jai_constants.tax_type_value_added, jai_constants.tax_type_cst )  then
9261             ln_base_amount := jai_ap_utils_pkg.fetch_tax_target_amt
9262                               (
9263                                 p_invoice_id          =>   inv_id ,
9264                                 p_line_location_id    =>   null ,
9265                                 p_transaction_id      =>   rcv_tran_id  ,
9266                                 p_parent_dist_id      =>   for_dist_insertion_rec.invoice_distribution_id ,
9267                                 p_tax_id              =>   tax_lines1_rec.tax_id
9268                               ) ;
9269           end if ;
9270           -- ended, Kunkumar for bug#5593895
9271           OPEN  c_tax(tax_lines1_rec.tax_id);
9272           FETCH c_tax INTO c_tax_rec;
9273           CLOSE c_tax;
9274 
9275 		  --Added by nprashar for bug # 13653889
9276           OPEN c_tax_curr_prec( tax_lines1_rec.tax_id ) ;
9277           FETCH c_tax_curr_prec INTO ln_tax_precision ;
9278           CLOSE c_tax_curr_prec ;
9279 
9280           /* Service Start */
9281           v_assets_tracking_flag := for_dist_insertion_rec.assets_tracking_flag;
9282           v_dist_code_combination_id := null;
9283 
9284 
9285           --initial the tax_type
9286           lv_tax_type := NULL;--added by eric for inclusive tax on 20-Dec,2007
9287 
9288           if tax_lines1_rec.modvat_flag = 'Y'
9289           and nvl(c_tax_rec.mod_cr_percentage, 0) > 0 then -- 5763527
9290 
9291             --recoverable tax
9292             lv_tax_type := 'RE'; --added by eric for inclusive tax on 20-dec,2007
9293 
9294            /*  recoverable tax */
9295             v_assets_tracking_flag := 'N';
9296 
9297             open c_regime_tax_type(r_jai_regimes.regime_id, c_tax_rec.tax_type);
9298             fetch c_regime_tax_type into r_service_regime_tax_type;
9299             close c_regime_tax_type;
9300 
9301             fnd_file.put_line(FND_FILE.LOG,
9302                    ' Service regime: '||r_service_regime_tax_type.tax_type);
9303 
9304             /* Bug 5358788. Added by Lakshmi Gopalsami
9305        * Fetched the details of VAT regime
9306        */
9307 
9308             OPEN  c_regime_tax_type(r_vat_regimes.regime_id, c_tax_rec.tax_type);
9309               FETCH  c_regime_tax_type INTO  r_vat_regime_tax_type;
9310             CLOSE  c_regime_tax_type;
9311 
9312             fnd_file.put_line(FND_FILE.LOG,
9313                    ' VAT regime: '||r_vat_regime_tax_type.tax_type);
9314 
9315             if r_service_regime_tax_type.tax_type is not null then
9316               /* Service type of tax */
9317               v_dist_code_combination_id := check_service_interim_account
9318                                             (
9319                                               lv_accrue_on_receipt_flag,
9320                                               lv_accounting_method_option,
9321                                               lv_is_item_an_expense,
9322                                               r_jai_regimes.regime_id,
9323                                               v_org_id,
9324                                               p_rematch,
9325                                               c_tax_rec.tax_type,
9326                 po_dist_id  --Added by JMEENA for bug#6833506
9327                                             );
9328                fnd_file.put_line(FND_FILE.LOG,
9329                  ' Regime type , CCID '
9330      || r_service_regime_tax_type.tax_type
9331            ||', ' || v_dist_code_combination_id);
9332 
9333                /* Bug 5358788. Added by Lakshmi Gopalsami
9334           * Commented p_rematch and added validation for
9335     * VAT regime.
9336     */
9337     /*following elsif block modified for bug 6595773*/
9338              ELSIF r_vat_regime_tax_type.tax_type IS NOT NULL THEN
9339            --p_rematch = 'PO_MATCHING' then
9340 
9341                v_dist_code_combination_id := for_dist_insertion_rec.dist_code_combination_id;
9342          fnd_file.put_line(FND_FILE.LOG,
9343                    ' Regime type , CCID '
9344        || r_vat_regime_tax_type.tax_type
9345        ||', ' || v_dist_code_combination_id);
9346 
9347             end if;
9348 
9349           else  /* 5763527 introduced for PROJETCS COSTING Impl */
9350             /* 5763527 */
9351             IF nvl(lv_accrue_on_receipt_flag , '#') <> 'Y' THEN -- Bug 6338371
9352               ln_project_id           := p_project_id;
9353               ln_task_id              := p_task_id;
9354               lv_exp_type             := p_expenditure_type;
9355               ld_exp_item_date        := p_expenditure_item_date;
9356               ln_exp_organization_id  := p_expenditure_organization_id;
9357               lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
9358               lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
9359             END if;
9360 
9361             --non recoverable
9362             lv_tax_type := 'NR';  --added by eric for inclusive tax on 20-dec,2007
9363           end if; /*nvl(c_tax_rec.mod_cr_percentage, 0) = 100  and c_tax_rec.tax_account_id is not null*/
9364 
9365           /* Bug#4177452*/
9366           if v_dist_code_combination_id is null then
9367             v_dist_code_combination_id := for_dist_insertion_rec.dist_code_combination_id;
9368           end if;
9369 
9370 
9371           /* Bug#5763527 */
9372           Fnd_File.put_line(Fnd_File.LOG, 'c_tax_rec.tax_type='||c_tax_rec.tax_type  ||',lv_excise_costing_flag='||lv_excise_costing_flag);
9373           if upper(c_tax_rec.tax_type) like  '%EXCISE%' then
9374 
9375             if lv_excise_costing_flag = 'Y' THEN
9376               IF nvl(lv_accrue_on_receipt_flag , '#') <> 'Y' THEN -- Bug 6338371
9377                 ln_project_id           := p_project_id;
9378                 ln_task_id              := p_task_id;
9379                 lv_exp_type             := p_expenditure_type;
9380                 ld_exp_item_date        := p_expenditure_item_date;
9381                 ln_exp_organization_id  := p_expenditure_organization_id;
9382                 lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
9383                 lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
9384               END if;
9385               v_dist_code_combination_id    := for_dist_insertion_rec.dist_code_combination_id;
9386             end if;
9387           end if;
9388           /* End# 5763527 */
9389 
9390           /* Service End */
9391 
9392           -- Start for bug#3752887
9393           v_tax_amount := tax_lines1_rec.tax_amount * v_apportn_factor_for_item_line;
9394 
9395 
9396           if for_org_id_rec.invoice_currency_code <> tax_lines1_rec.currency then
9397             v_tax_amount := v_tax_amount / for_org_id_rec.exchange_rate;
9398           end if;
9399           -- End for bug#3752887
9400 
9401           --
9402           -- Begin 5763527
9403           --
9404           ln_rec_tax_amt  := null;
9405           ln_nrec_tax_amt := null;
9406           ln_lines_to_insert := 1; -- Loop controller to insert more than one lines for partially recoverable tax lines in PO
9407 
9408           --added by Eric for inclusive tax on 20-dec-2007,begin
9409           -------------------------------------------------------------------------------------
9410           --exclusive tax or inlcusive tax without project info,  ln_lines_to_insert := 1;
9411           --inclusive recoverable tax with project info,  ln_lines_to_insert := 2;
9412           --PR(partially recoverable) tax is processed in another logic
9413 
9414           IF ( NVL(tax_lines1_rec.inc_tax_flag,'N') = 'N'
9415                OR ( NVL(tax_lines1_rec.inc_tax_flag,'N') = 'Y'
9416                     AND p_project_id IS NULL
9417                   )
9418              )
9419           THEN
9420             ln_lines_to_insert := 1;
9421           ELSIF ( NVL(tax_lines1_rec.inc_tax_flag,'N') = 'Y'
9422                   AND p_project_id IS NOT NULL
9423                   AND lv_tax_type = 'RE'
9424                 )
9425           THEN
9426             ln_lines_to_insert := 2;
9427           END IF;--( NVL(tax_lines1_rec.inc_tax_flag,'N') = 'N' OR )
9428           -------------------------------------------------------------------------------------
9429           --added by Eric for inclusive tax on 20-dec-2007,end
9430 
9431 
9432 
9433           Fnd_File.put_line(Fnd_File.LOG, 'tax_lines1_rec.modvat_flag ='||tax_lines1_rec.modvat_flag ||',c_tax_rec.mod_cr_percentage='||c_tax_rec.mod_cr_percentage);
9434 
9435           if tax_lines1_rec.modvat_flag = jai_constants.YES
9436           and nvl(c_tax_rec.mod_cr_percentage, -1) < 100
9437           and nvl(c_tax_rec.mod_cr_percentage, -1) > 0
9438           then
9439             --
9440             -- Tax line is for partial Recoverable tax.  Hence split amount into two parts, Recoverable and Non-Recoverable
9441             -- and instead of one line, two lines needs to be inserted.
9442             -- For ordinary lines (with modvat_flag = 'N' or with mod_cr_percentage = 100) there will be only one line inserted
9443             --
9444             ln_lines_to_insert := 2 *ln_lines_to_insert; --changed by eric for inclusive tax on Jan 4,2008
9445             ln_rec_tax_amt  :=  round ( nvl(v_tax_amount,0) * (c_tax_rec.mod_cr_percentage/100) , c_tax_rec.rounding_factor) ;
9446             ln_nrec_tax_amt :=  round ( nvl(v_tax_amount,0) - nvl(ln_rec_tax_amt,0) , c_tax_rec.rounding_factor) ;
9447         /* Added Tax rounding based on tax rounding factor for bug 6761425 by vumaasha */
9448 
9449             lv_tax_type := 'PR';  --changed by eric for inclusive tax on Jan 4,2008
9450           end if;
9451           fnd_file.put_line(fnd_file.log, 'ln_lines_to_insert='||ln_lines_to_insert||
9452                                           ',ln_rec_tax_amt='||ln_rec_tax_amt          ||
9453                                           ',ln_nrec_tax_amt='||ln_nrec_tax_amt
9454                            );
9455 
9456           --
9457           --  If a line has a partially recoverable tax the following loop will be executed twice.  First line will always be for a
9458           --  non recoverable tax amount and the second line will be for a recoverable tax amount
9459           --  For ordinary lines (with modvat_flag = 'N' or with mod_cr_percentage = 100 fully recoverable) the variable
9460           --  ln_lines_to_insert will have value of 1 and hence only one line will be inserted with full tax amount
9461           --
9462 
9463           for line in 1..ln_lines_to_insert
9464           loop
9465 
9466             --deleted by Eric for inclusive tax on 20-dec-2007,begin
9467             -------------------------------------------------------------------------------------
9468             /* commented out by eric for inclusive tax
9469             if line = 1 then
9470 
9471               v_tax_amount  := nvl(ln_rec_tax_amt, v_tax_amount);
9472               lv_modvat_flag := tax_lines1_rec.modvat_flag ;
9473 
9474             elsif line = 2 then
9475 
9476               v_tax_amount := ln_nrec_tax_amt;
9477               v_assets_tracking_flag := jai_constants.YES;
9478               lv_modvat_flag := jai_constants.NO ;
9479               --
9480               -- This is a non recoverable line hence the tax amounts should be added into the project costs by populating
9481               -- projects related columns so that PROJECTS can consider this line for Project Costing
9482               --
9483               IF  nvl(lv_accrue_on_receipt_flag , '#') <> 'Y' then-- Bug 6338371
9484                 ln_project_id           := p_project_id;
9485                 ln_task_id              := p_task_id;
9486                 lv_exp_type             := p_expenditure_type;
9487                 ld_exp_item_date        := p_expenditure_item_date;
9488                 ln_exp_organization_id  := p_expenditure_organization_id;
9489                 lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
9490                 lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
9491               END IF;
9492               -- For non recoverable line charge account should be same as of the parent line
9493               v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id;
9494 
9495             end if;
9496 
9497             --ln_inv_line_num := ln_inv_line_num + 1;
9498             -- End 5763527
9499             --
9500             */
9501             -------------------------------------------------------------------------------------
9502             --deleted by Eric for inclusive tax on 20-dec-2007,end
9503 
9504             --added by Eric for inclusive tax on 20-dec-2007,begin
9505             -------------------------------------------------------------------------------------
9506             IF (NVL(tax_lines1_rec.inc_tax_flag,'N') = 'N')--exclusive case
9507             THEN
9508               IF line = 1 then
9509 
9510                 v_tax_amount  := nvl(ln_rec_tax_amt, v_tax_amount);
9511                 lv_tax_line_amount:=  v_tax_amount       ;   --added by eric for inclusive tax
9512                 lv_modvat_flag := tax_lines1_rec.modvat_flag ;
9513 
9514                 lv_ap_line_to_inst_flag  := 'Y'; --added by eric for inclusive tax
9515                 lv_tax_line_to_inst_flag := 'Y'; --added by eric for inclusive tax
9516               ELSIF line = 2 then
9517 
9518                 v_tax_amount             := ln_nrec_tax_amt;
9519                 lv_tax_line_amount       :=  v_tax_amount  ; --added by eric for inclusive tax
9520                 lv_ap_line_to_inst_flag  := 'Y';             --added by eric for inclusive tax
9521                 lv_tax_line_to_inst_flag := 'Y';             --added by eric for inclusive tax
9522 
9523 
9524                 IF for_dist_insertion_rec.assets_tracking_flag = jai_constants.YES THEN
9525                   v_assets_tracking_flag := jai_constants.YES;
9526                 END IF;
9527 
9528                 lv_modvat_flag := jai_constants.NO ;
9529 
9530                 --
9531                 -- This is a non recoverable line hence the tax amounts should be added into the project costs by populating
9532                 -- projects related columns so that PROJECTS can consider this line for Project Costing
9533                 --
9534                 IF nvl(lv_accrue_on_receipt_flag,'#') <> 'Y' THEN -- Bug 6338371
9535                   ln_project_id           := p_project_id;
9536                   ln_task_id              := p_task_id;
9537                   lv_exp_type             := p_expenditure_type;
9538                   ld_exp_item_date        := p_expenditure_item_date;
9539                   ln_exp_organization_id  := p_expenditure_organization_id;
9540                   lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
9541                   lv_pa_addition_flag         := for_dist_insertion_rec.pa_addition_flag;
9542                 END if;
9543 
9544                 -- For non recoverable line charge account should be same as of the parent line
9545                 v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id;
9546 
9547               END IF; --line = 1
9548             ELSIF (NVL(tax_lines1_rec.inc_tax_flag,'N') = 'Y')       --inclusive case
9549             THEN
9550               IF( lv_tax_type ='PR')
9551               THEN
9552                 IF ( line = 1 )
9553                 THEN
9554                   --recoverable part
9555                   v_tax_amount       := ln_rec_tax_amt ;
9556                   lv_tax_line_amount := v_tax_amount   ;   --added by eric for inclusive tax
9557                   lv_modvat_flag     := tax_lines1_rec.modvat_flag  ;
9558                   lv_ap_line_to_inst_flag  := 'N';         --added by eric for inclusive tax
9559                   lv_tax_line_to_inst_flag := 'Y';         --added by eric for inclusive tax
9560 
9561                   --non recoverable part
9562                 ELSIF ( line = 2 )
9563                 THEN
9564                   v_tax_amount       := ln_nrec_tax_amt  ;
9565                   lv_tax_line_amount :=  v_tax_amount    ;   --added by eric for inclusive tax
9566                   lv_modvat_flag     := jai_constants.NO ;
9567                   lv_ap_line_to_inst_flag  := 'N';           --added by eric for inclusive tax
9568                   lv_tax_line_to_inst_flag := 'Y';           --added by eric for inclusive tax
9569 
9570                   --recoverable part without project infor
9571                 ELSIF ( line = 3 )
9572                 THEN
9573                   v_tax_amount                  := ln_rec_tax_amt;
9574                   lv_tax_line_amount            := NULL;
9575 
9576                   lv_ap_line_to_inst_flag  := 'Y';             --added by eric for inclusive tax
9577                   lv_tax_line_to_inst_flag := 'N';             --added by eric for inclusive tax
9578 
9579                   --Project information
9580                   ln_project_id                 := NULL;
9581                   ln_task_id                    := NULL;
9582                   lv_exp_type                   := NULL;
9583                   ld_exp_item_date              := NULL;
9584                   ln_exp_organization_id        := NULL;
9585                   lv_project_accounting_context := NULL;
9586                   lv_pa_addition_flag           := NULL;
9587 
9588                   -- For inclusive recoverable line charge account should be same as of the parent line
9589                   v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id ;
9590 
9591                   --recoverable part in negative amount with project infor
9592                 ELSIF ( line = 4 )
9593                 THEN
9594                   v_tax_amount                  := NVL(ln_rec_tax_amt, v_tax_amount)* -1;
9595                   lv_tax_line_amount            := NULL;
9596                   lv_ap_line_to_inst_flag  := 'Y';             --added by eric for inclusive tax
9597                   lv_tax_line_to_inst_flag := 'N';             --added by eric for inclusive tax
9598 
9599                   --Project information
9600                   ln_project_id                 := p_project_id;
9601                   ln_task_id                    := p_task_id;
9602                   lv_exp_type                   := p_expenditure_type;
9603                   ld_exp_item_date              := p_expenditure_item_date;
9604                   ln_exp_organization_id        := p_expenditure_organization_id;
9605                   lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
9606                   lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
9607 
9608                   -- For inclusive recoverable line charge account should be same as of the parent line
9609                   v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id ;
9610                 END IF; --line = 1
9611               ELSIF ( lv_tax_type = 'RE'
9612                       AND p_project_id IS NOT NULL
9613                     )
9614               THEN
9615                 --recoverable tax without project infor
9616                 IF ( line = 1 )
9617                 THEN
9618                   v_tax_amount       :=  v_tax_amount  ;
9619                   lv_tax_line_amount :=  v_tax_amount  ;    --added by eric for inclusive tax
9620                   lv_modvat_flag     :=  tax_lines1_rec.modvat_flag ;
9621                   lv_ap_line_to_inst_flag  := 'Y';          --added by eric for inclusive tax
9622                   lv_tax_line_to_inst_flag := 'Y';          --added by eric for inclusive tax
9623 
9624                   ln_project_id                 := NULL;
9625                   ln_task_id                    := NULL;
9626                   lv_exp_type                   := NULL;
9627                   ld_exp_item_date              := NULL;
9628                   ln_exp_organization_id        := NULL;
9629                   lv_project_accounting_context := NULL;
9630                   lv_pa_addition_flag           := NULL;
9631                   v_dist_code_combination_id    := for_dist_insertion_rec.dist_code_combination_id ;
9632                 --recoverable tax in negative amount with project infor
9633                 ELSIF ( line = 2 )
9634                 THEN
9635                   v_tax_amount       :=  v_tax_amount * -1;
9636                   lv_tax_line_amount :=  NULL ;   --added by eric for inclusive tax
9637                   lv_modvat_flag     :=  tax_lines1_rec.modvat_flag  ;
9638 
9639                   ln_project_id                 := p_project_id;
9640                   ln_task_id                    := p_task_id;
9641                   lv_exp_type                   := p_expenditure_type;
9642                   ld_exp_item_date              := p_expenditure_item_date;
9643                   ln_exp_organization_id        := p_expenditure_organization_id;
9644                   lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
9645                   lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
9646                   v_dist_code_combination_id    := for_dist_insertion_rec.dist_code_combination_id ;
9647                 END IF;
9648               --ELSIF ( lv_tax_type <> 'PR' AND p_project_id IS NULL )
9649               --THEN
9650               ELSE -- eric removed the above criteria for bug 6888665 and 6888209
9651                 Fnd_File.put_line(Fnd_File.LOG, 'NOT Inclusive PR Tax,NOT Inclusive RE for project ');
9652                 --The case process the inclusive NR tax and inclusive RE tax not for project
9653 
9654                 lv_tax_line_amount := v_tax_amount   ;   --added by eric for inclusive tax
9655                 lv_modvat_flag     := tax_lines1_rec.modvat_flag  ;
9656                 lv_ap_line_to_inst_flag  := 'N';         --added by eric for inclusive tax
9657                 lv_tax_line_to_inst_flag := 'Y';         --added by eric for inclusive tax
9658 
9659               END IF;--( tax_type ='PR' and (ln_lines_to_insert =4 OR )
9660             END IF; --(NVL(r_tax_lines_r ec.inc_tax_flag,'N') = 'N')
9661             -------------------------------------------------------------------------------------------
9662             --added by Eric for inclusive tax on 20-dec-2007,end
9663 
9664 
9665 
9666 
9667 
9668           fnd_file.put_line(fnd_file.log, 'line='||line||', v_tax_amount='|| v_tax_amount
9669                                           ||'lv_modvat_flag='||lv_modvat_flag
9670                                           ||'v_assets_tracking_flag='||v_assets_tracking_flag
9671                               );
9672 
9673 
9674 
9675           --insert exclusive tax to the ap tables
9676           --or insert recoverable inclusive tax with project information the ap tables
9677 
9678 
9679           --1.Only exlusive taxes or inclusive taxes with project information
9680           --  are allowed to be inserted into Ap Lines and Dist Lines
9681 
9682           --2.All taxes need to be inserted into jai tax tables. Futher the
9683           --  partially recoverable tax need to be splitted into 2 lines
9684 
9685           --added by Eric for inclusive tax on 20-dec-2007,begin
9686           ------------------------------------------------------------------------
9687           IF ( NVL(lv_ap_line_to_inst_flag,'N')='Y')
9688           THEN
9689             ln_inv_line_num := ln_inv_line_num + 1; --added by eric for inlcusive tax
9690           ---------------------------------------------------------------------------
9691           --added by Eric for inclusive tax on 20-dec-2007,end
9692 
9693 
9694             INSERT INTO ap_invoice_lines_all
9695             (
9696               INVOICE_ID
9697             , LINE_NUMBER
9698             , LINE_TYPE_LOOKUP_CODE
9699             , DESCRIPTION
9700             , ORG_ID
9701             , MATCH_TYPE
9702       , DEFAULT_DIST_CCID --Changes by nprashar for bug #6995437
9703             , ACCOUNTING_DATE
9704             , PERIOD_NAME
9705             , DEFERRED_ACCTG_FLAG
9706             , DEF_ACCTG_START_DATE
9707             , DEF_ACCTG_END_DATE
9708             , DEF_ACCTG_NUMBER_OF_PERIODS
9709             , DEF_ACCTG_PERIOD_TYPE
9710             , SET_OF_BOOKS_ID
9711             , AMOUNT
9712             , WFAPPROVAL_STATUS
9713             , CREATION_DATE
9714             , CREATED_BY
9715             , LAST_UPDATED_BY
9716             , LAST_UPDATE_DATE
9717             , LAST_UPDATE_LOGIN
9718             /* 5763527 */
9719             , project_id
9720             , task_id
9721             , expenditure_type
9722             , expenditure_item_date
9723             , expenditure_organization_id
9724       ,po_distribution_id --Added for bug#6780154
9725             )
9726             VALUES
9727             (
9728                 inv_id
9729               , ln_inv_line_num
9730               , lv_misc
9731               , c_tax_rec.tax_name
9732               , v_org_id
9733               , lv_match_type
9734         , v_dist_code_combination_id
9735               , rec_max_ap_lines_all.accounting_date
9736               , rec_max_ap_lines_all.period_name
9737               , rec_max_ap_lines_all.deferred_acctg_flag
9738               , rec_max_ap_lines_all.def_acctg_start_date
9739               , rec_max_ap_lines_all.def_acctg_end_date
9740               , rec_max_ap_lines_all.def_acctg_number_of_periods
9741               , rec_max_ap_lines_all.def_acctg_period_type
9742               , rec_max_ap_lines_all.set_of_books_id
9743               , ROUND(ROUND(v_tax_amount,ln_tax_precision),ln_precision) --Added by nprashar for bug # 13653889
9744               , rec_max_ap_lines_all.wfapproval_status  -- Bug 4863208
9745               , sysdate
9746               , ln_user_id
9747               , ln_user_id
9748               , sysdate
9749               , ln_login_id
9750               /* 5763527 */
9751               , ln_project_id
9752               , ln_task_id
9753               , lv_exp_type
9754               , ld_exp_item_date
9755               , ln_exp_organization_id
9756         ,po_dist_id --Added for bug#6780154
9757             );
9758 
9759 
9760             -- Start bug#3332988
9761             open c_get_invoice_distribution;
9762             fetch c_get_invoice_distribution into v_invoice_distribution_id;
9763             close c_get_invoice_distribution;
9764             -- End bug#3332988
9765             fnd_file.put_line(FND_FILE.LOG, ' Invoice distribution id '|| v_invoice_distribution_id);
9766 
9767             INSERT INTO ap_invoice_distributions_all
9768             (
9769             accounting_date,
9770             accrual_posted_flag,
9771             assets_addition_flag,
9772             assets_tracking_flag,
9773             cash_posted_flag,
9774             distribution_line_number,
9775             dist_code_combination_id,
9776             invoice_id,
9777             last_updated_by,
9778             last_update_date,
9779             line_type_lookup_code,
9780             period_name,
9781             set_of_books_id,
9782             amount,
9783             base_amount,
9784             batch_id,
9785             created_by,
9786             creation_date,
9787             description,
9788              exchange_rate_variance,
9789             last_update_login,
9790             match_status_flag,
9791             posted_flag,
9792             rate_var_code_combination_id,
9793             reversal_flag,
9794             program_application_id,
9795             program_id,
9796             program_update_date,
9797             accts_pay_code_combination_id,
9798             invoice_distribution_id,
9799             quantity_invoiced,
9800             org_id,
9801             po_distribution_id ,
9802             rcv_transaction_id,
9803             matched_uom_lookup_code,
9804             invoice_line_number
9805             ,charge_applicable_to_dist_id -- Bug 5401111. Added by Lakshmi Gopalsami
9806             /* 5763527 */
9807             , project_id
9808             , task_id
9809             , expenditure_type
9810             , expenditure_item_date
9811             , expenditure_organization_id
9812             , project_accounting_context
9813             , pa_addition_flag
9814             -- Bug 7249100. Added by Lakshmi Gopalsami
9815             ,distribution_class
9816       ,asset_book_type_code /* added for bug 8406404 by vumaasha */
9817             )
9818             VALUES
9819             (
9820             rec_max_ap_lines_all.accounting_date,
9821             'N', --for_dist_insertion_rec.accrual_posted_flag,
9822             for_dist_insertion_rec.assets_addition_flag,
9823             v_assets_tracking_flag ,
9824             'N',
9825             v_distribution_no,
9826             v_dist_code_combination_id,
9827             inv_id,
9828             ln_user_id,
9829             sysdate,
9830             lv_misc,
9831             rec_max_ap_lines_all.period_name,
9832             rec_max_ap_lines_all.set_of_books_id,
9833             round(v_tax_amount,ln_precision),
9834             ROUND(v_tax_amount * for_dist_insertion_rec.exchange_rate, ln_precision),
9835             v_batch_id,
9836             ln_user_id,
9837             sysdate,
9838             c_tax_rec.tax_name,
9839               null,--kunkumar for forward porting to R12
9840             ln_login_id,
9841             for_dist_insertion_rec.match_status_flag , -- Bug 4863208
9842             'N',
9843             NULL,
9844             for_dist_insertion_rec.reversal_flag,  -- Bug 4863208
9845             for_dist_insertion_rec.program_application_id,
9846             for_dist_insertion_rec.program_id,
9847             for_dist_insertion_rec.program_update_date,
9848             for_dist_insertion_rec.accts_pay_code_combination_id,
9849             v_invoice_distribution_id,
9850             decode(v_assets_tracking_flag, 'Y', NULL, 0), /* Modified for bug 8406404 by vumaasha */
9851             for_dist_insertion_rec.org_id,  -- bug 4863208
9852              --po_dist_id,12434160
9853 			for_dist_insertion_rec.po_distribution_id, --12434160
9854             rcv_tran_id,
9855             for_dist_insertion_rec.matched_uom_lookup_code,
9856             ln_inv_line_num
9857             /* Bug 5361931. Added by Lakshmi Gopalsami
9858              * Passed ln_inv_line_num instead of
9859              * pn_invoice_line_number
9860              */
9861             -- Bug 5401111. Added by Lakshmi Gopalsami
9862             ,decode(v_assets_tracking_flag,'N',
9863                     NULL,for_dist_insertion_rec.invoice_distribution_id)
9864 /*Commented the account_type condition for bug#7008161 by JMEENA
9865        decode(lv_account_type, 'A',
9866               for_dist_insertion_rec.invoice_distribution_id,NULL)
9867             )
9868 */
9869             /* 5763527 */
9870             , ln_project_id
9871             , ln_task_id
9872             , lv_exp_type
9873             , ld_exp_item_date
9874             , ln_exp_organization_id
9875             , lv_project_accounting_context
9876             , lv_pa_addition_flag
9877             -- Bug 7249100. Added by Lakshmi Gopalsami
9878             , lv_dist_class
9879       , decode(v_assets_tracking_flag,'Y',for_dist_insertion_rec.asset_book_type_code,NULL) /* added for bug 8406404 by vumaasha */
9880             );
9881           --added by Eric for inclusive tax on 20-dec-2007,begin
9882           ------------------------------------------------------------
9883           END IF; --( NVL(lv_ap_line_to_inst_flag,'N')='Y') )
9884           ------------------------------------------------------------
9885           --added by Eric for inclusive tax on 20-dec-2007,end
9886 
9887          --added by Eric for inclusive tax on 20-dec-2007,begin
9888           -----------------------------------------------------------------
9889           IF  (NVL(lv_tax_line_to_inst_flag,'N') = 'Y')
9890           THEN
9891           -----------------------------------------------------------------
9892           --added by Eric for inclusive tax on 20-dec-2007,end
9893 
9894             INSERT INTO JAI_AP_MATCH_INV_TAXES
9895             (
9896             tax_distribution_id,
9897             exchange_rate_variance,
9898             assets_tracking_flag,
9899             invoice_id,
9900             po_header_id,
9901             po_line_id,
9902             line_location_id,
9903             set_of_books_id,
9904             --org_id,
9905             exchange_rate,
9906             exchange_rate_type,
9907             exchange_date,
9908             currency_code,
9909             code_combination_id,
9910             last_update_login,
9911             creation_date,
9912             created_by,
9913             last_update_date,
9914             last_updated_by,
9915             acct_pay_code_combination_id,
9916             accounting_date,
9917             tax_id,
9918             tax_amount,
9919             base_amount,
9920             chart_of_accounts_id,
9921             distribution_line_number,
9922             --project_id,
9923             --task_id,
9924             po_distribution_id,  -- added  by bug#3038566
9925             parent_invoice_distribution_id,  -- added  by bug#3038566
9926             legal_entity_id -- added by rallamse bug#
9927             ,INVOICE_LINE_NUMBER  --    Added by Brathod, Bug# 4445989
9928             ,INVOICE_DISTRIBUTION_ID ------------|
9929             ,PARENT_INVOICE_LINE_NUMBER----------|
9930             ,RCV_TRANSACTION_ID
9931             ,LINE_TYPE_LOOKUP_CODE
9932             /* 5763527*/
9933             , recoverable_flag
9934             , line_no                          -- added, Harshita for Bug 5553150
9935             /* End 5763527 */
9936             )
9937             VALUES
9938             (
9939             JAI_AP_MATCH_INV_TAXES_S.NEXTVAL,
9940             null,--kunkumar for forward porting to R12
9941             v_assets_tracking_flag , -- 'N', bug#2851123
9942             inv_id,
9943             cur_items_rec.po_header_id,
9944             cur_items_rec.po_line_id,
9945             cur_items_rec.line_location_id,
9946             cur_items_rec.set_of_books_id,
9947             --cur_items_rec.org_id,
9948             cur_items_rec.rate,
9949             cur_items_rec.rate_type,
9950             cur_items_rec.rate_date,
9951             cur_items_rec.currency_code,
9952             /* Bug 5358788. Added by Lakshmi Gopalsami. Commented
9953                    * c_tax_rec.tax_account_id and v_dist_code_combination_id
9954              */
9955             v_dist_code_combination_id,
9956             cur_items_rec.last_update_login,
9957             cur_items_rec.creation_date,
9958             cur_items_rec.created_by,
9959             cur_items_rec.last_update_date,
9960             cur_items_rec.last_updated_by,
9961             v_dist_code_combination_id,
9962             cur_items_rec.invoice_date,
9963             tax_lines1_rec.tax_id,
9964             /*commented out by eric for inclusive tax
9965             round(v_tax_amount,ln_precision), -- ROUND(v_tax_amount, 2), by Aparajita bug#2567799
9966             */
9967             ROUND(ROUND(lv_tax_line_amount,ln_tax_precision),ln_precision), --added by eric for inclusive tax
9968             nvl(ln_base_amount,ROUND(ROUND(tax_lines1_rec.tax_amount,ln_tax_precision), ln_precision)), --Kunkumar for Bug#5593895
9969             caid,
9970             v_distribution_no,
9971             --p_project_id,
9972             --p_task_id,
9973             po_dist_id, -- added  by bug#3038566
9974             for_dist_insertion_rec.invoice_distribution_id, -- added  by bug#3038566
9975             get_ven_info_rec.legal_entity_id -- added by rallamse bug#
9976             -- ln_inv_line_num, deleted by Eric for inclusive tax
9977             --, v_invoice_distribution_id ,deleted by Eric for inclusive tax on 20-dec-2007
9978             --added by Eric for inclusive tax on 20-dec-2007,begin
9979             ---------------------------------------------------------------
9980             , DECODE ( NVL( tax_lines1_rec.inc_tax_flag,'N')
9981                      , 'N',ln_inv_line_num
9982                      , 'Y',pn_invoice_line_number
9983                      )
9984             , NVL(v_invoice_distribution_id,for_dist_insertion_rec.invoice_distribution_id)
9985             ---------------------------------------------------------------
9986             --added by Eric for inclusive tax on 20-dec-2007,end
9987             , pn_invoice_line_number
9988             , rcv_tran_id
9989             , lv_misc
9990             /* 5763527*/
9991             , lv_modvat_flag
9992             , tax_lines1_rec.tax_line_no  -- added, Harshita for Bug 5553150
9993             /* End of 5763527 */
9994             );
9995 
9996 
9997             /*  Bug 46863208. Added by Lakshmi Gopalsami
9998             Commented the MRC call
9999             insert_mrc_data(v_invoice_distribution_id); -- bug#3332988
10000             */
10001             --cum_tax_amt stores total exclusive amount
10002             /* commented out by Eric for bug 8345512 on 13-Jul-2009
10003                cum_tax_amt := cum_tax_amt + ROUND(v_tax_amount, ln_precision);
10004             */
10005 
10006             --Modified by Eric for bug 8345512 on 13-Jul-2009,begin
10007             ------------------------------------------------------------------
10008             IF NVL(tax_lines1_rec.inc_tax_flag,'N') ='N'
10009             THEN
10010               cum_tax_amt := cum_tax_amt + ROUND(round(v_tax_amount,ln_tax_precision), ln_precision); /* Bug 14386866 */
10011             END IF;
10012             ------------------------------------------------------------------
10013             --Modified by Eric for bug 8345512 on 13-Jul-2009,end
10014             /* Obsoleted as part of R12 , Bug# 4445989
10015             INSERT INTO JAI_CMN_FA_INV_DIST_ALL
10016             (
10017             invoice_id,
10018             invoice_distribution_id,
10019             set_of_books_id,
10020             batch_id,
10021             po_distribution_id,
10022             rcv_transaction_id,
10023             dist_code_combination_id,
10024             accounting_date,
10025             assets_addition_flag,
10026             assets_tracking_flag,
10027             distribution_line_number,
10028             line_type_lookup_code,
10029             amount,
10030             description,
10031             match_status_flag,
10032             quantity_invoiced
10033             )
10034             VALUES
10035             (
10036             inv_id,
10037             ap_invoice_distributions_s.CURRVAL,
10038             for_dist_insertion_rec.set_of_books_id,
10039             v_batch_id,
10040             for_dist_insertion_rec.po_distribution_id,
10041             rcv_tran_id,
10042             v_dist_code_combination_id,
10043             for_dist_insertion_rec.accounting_date,
10044             for_dist_insertion_rec.assets_addition_flag,
10045             v_assets_tracking_flag, -- for_dist_insertion_rec.assets_tracking_flag, bug#2851123
10046             v_distribution_no,
10047             'MISCELLANEOUS',
10048             round(v_tax_amount,ln_precision), -- ROUND(v_tax_amount, 2), by Aparajita bug#2567799
10049             c_tax_rec.tax_name,
10050             NULL,
10051             NULL
10052             );
10053             */
10054             --added by Eric for inclusive tax on 20-dec-2007,begin
10055             -----------------------------------------------------------------
10056             END IF;--  (NVL(lv_tax_line_to_inst_flag,'N') = 'Y')
10057             -----------------------------------------------------------------
10058             --added by Eric for inclusive tax on 20-dec-2007,end
10059 
10060 
10061           end loop; --> for line in ... 5763527
10062         END LOOP; --tax_lines1_rec IN tax_lines1_cur(rcv_tran_id,for_org_id_rec.vendor_id)  LOOP
10063 
10064         v_update_payment_schedule:=update_payment_schedule(cum_tax_amt); -- added by bug 321978.
10065 
10066 --        cum_tax_amt := round(cum_tax_amt,ln_precision);
10067         UPDATE  ap_invoices_all
10068         SET     invoice_amount = invoice_amount   + cum_tax_amt,
10069             approved_amount      =  approved_amount  + cum_tax_amt,
10070             pay_curr_invoice_amount =  pay_curr_invoice_amount + cum_tax_amt,
10071             amount_applicable_to_discount =  amount_applicable_to_discount + cum_tax_amt,
10072             payment_status_flag = decode(payment_status_flag, 'Y', 'P', payment_status_flag)
10073         WHERE  invoice_id = inv_id;
10074 
10075         -- start added for bug#3354932
10076         if for_org_id_rec.invoice_currency_code <> v_functional_currency then
10077           update ap_invoices_all
10078           set    base_amount = invoice_amount  * exchange_rate
10079           where  invoice_id = inv_id;
10080         end if;
10081         -- end added for bug#3354932
10082 
10083      /* Bug 4863208. Added by Lakshmi Gopalsami
10084          Commented the MRC call
10085         update_mrc_data; -- bug#3332988
10086      */
10087 
10088       END IF;    --  p_receipt_code = 'PACKING_SLIP' or  p_receipt_code  = 'RECEIPT'
10089 
10090 
10091 
10092     ELSIF rcv_tran_id IS NULL and v_source IN ('ERS', 'ASBN', 'RTS') THEN
10093 
10094       --IF rcv_tran_id IS NOT NULL THEN
10095       -- to be fired when po_line_locations_all.match_option = 'P'
10096 
10097       Fnd_File.put_line(Fnd_File.LOG, 'Fired when rcv_tran_id IS NULL, match_option is P');
10098 
10099     -- fetch shipment_num from rcv_headers_interface based on invoice_num
10100     -- pramasub start for isupplier IL FP
10101     -- local cursor to fetch shipment number
10102     For rcv_hdr_intf_rec in
10103     (Select shipment_num
10104     From   rcv_headers_interface
10105     Where  invoice_num = get_ven_info_rec.invoice_num)
10106     Loop
10107       v_ship_num := rcv_hdr_intf_rec.shipment_num;
10108     End loop;
10109     -- pramasub end FP
10110 
10111 
10112       FOR i IN
10113       from_line_location_taxes
10114       (
10115       cur_items_rec.line_location_id, /* from_po_distributions_rec.line_location_id, bug # 4204600 */
10116       for_org_id_rec.vendor_id,
10117     v_source, -- added by pramasub Isupplier IL FP
10118     v_ship_num -- added pramasub FP
10119       )LOOP
10120           -- Added by Brathod for Bug# 4445989
10121           /* v_distribution_no := v_distribution_no + 1; */
10122           v_distribution_no :=1;
10123           -- ln_inv_line_num := ln_inv_line_num + 1; -- 5763527, moved the increment to just beofore the insert statement
10124           -- End Bug# 4445989
10125 
10126           r_service_regime_tax_type := null;
10127           -- Bug 5358788. Added by Lakshmi Gopalsami
10128           r_VAT_regime_tax_type := null;
10129 
10130           /* 5763527 */
10131           ln_project_id           := null;
10132           ln_task_id              := null;
10133           lv_exp_type             := null;
10134           ld_exp_item_date        := null;
10135           ln_exp_organization_id  := null;
10136           lv_project_accounting_context := null;
10137           lv_pa_addition_flag := null;
10138 
10139 
10140           -- Start for bug#3752887
10141           v_tax_amount := i.tax_amount * v_apportn_factor_for_item_line;
10142 
10143 
10144           if i.currency <> for_org_id_rec.invoice_currency_code then
10145               v_tax_amount := v_tax_amount / for_org_id_rec.exchange_rate;
10146           end if;
10147           -- End for bug#3752887
10148 
10149           OPEN  c_tax(i.tax_id);
10150           FETCH c_tax INTO c_tax_rec;
10151           CLOSE c_tax;
10152 
10153 		   --Added by nprashar for bug # 13653889
10154 		   OPEN c_tax_curr_prec( i.tax_id ) ;
10155            FETCH c_tax_curr_prec INTO ln_tax_precision ;
10156            CLOSE c_tax_curr_prec ;
10157 
10158             -- added, kunkumar for Bug#5593895
10159           ln_base_amount := null ;
10160           if i.tax_type IN ( jai_constants.tax_type_value_added, jai_constants.tax_type_cst )  then
10161             ln_base_amount := jai_ap_utils_pkg.fetch_tax_target_amt
10162                               (
10163                                 p_invoice_id          =>   inv_id ,
10164                                 p_line_location_id    =>   cur_items_rec.line_location_id ,
10165                                 p_transaction_id       =>   null  ,
10166                                 p_parent_dist_id      =>   for_dist_insertion_rec.invoice_distribution_id ,
10167                                 p_tax_id              =>   i.tax_id
10168                               ) ;
10169           end if ;
10170                 -- ended, kunkumar for Bug#5593895
10171 
10172           /* Service Start */
10173           v_assets_tracking_flag := for_dist_insertion_rec.assets_tracking_flag;
10174           v_dist_code_combination_id := null;
10175 
10176           --initial the tax_type
10177           lv_tax_type := NULL;--added by eric for inclusive tax on 20-Dec,2007
10178 
10179           if i.modvat_flag = 'Y'
10180           and nvl(c_tax_rec.mod_cr_percentage, 0) > 0
10181           then  -- 5763527
10182 
10183             --recoverable tax
10184             lv_tax_type := 'RE'; --added by eric for inclusive tax on 20-dec,2007
10185 
10186 
10187            /*  recoverable tax */
10188             v_assets_tracking_flag := 'N';
10189 
10190             open c_regime_tax_type(r_jai_regimes.regime_id, c_tax_rec.tax_type);
10191             fetch c_regime_tax_type into r_service_regime_tax_type;
10192             close c_regime_tax_type;
10193 
10194             fnd_file.put_line(FND_FILE.LOG,
10195                      ' Service regime: '||r_service_regime_tax_type.tax_type);
10196 
10197        /* Bug 5358788. Added by Lakshmi Gopalsami
10198       * Fetched the details of VAT regime
10199       */
10200 
10201            OPEN  c_regime_tax_type(r_vat_regimes.regime_id, c_tax_rec.tax_type);
10202             FETCH  c_regime_tax_type INTO  r_vat_regime_tax_type;
10203            CLOSE  c_regime_tax_type;
10204 
10205            fnd_file.put_line(FND_FILE.LOG,
10206                      ' VAT regime: '||r_vat_regime_tax_type.tax_type);
10207 
10208             if r_service_regime_tax_type.tax_type is not null then
10209               /* Service type of tax */
10210               v_dist_code_combination_id := check_service_interim_account
10211                                             (
10212                                               lv_accrue_on_receipt_flag,
10213                                               lv_accounting_method_option,
10214                                               lv_is_item_an_expense,
10215                                               r_jai_regimes.regime_id,
10216                                               v_org_id,
10217                                               p_rematch,
10218                                               c_tax_rec.tax_type,
10219                 po_dist_id  --Added by JMEENA for bug#6833506
10220                                             );
10221               fnd_file.put_line(FND_FILE.LOG,
10222                    ' Regime type , CCID '
10223        || r_service_regime_tax_type.tax_type
10224              ||', ' || v_dist_code_combination_id);
10225 
10226               /* Bug 5358788. Added by Lakshmi Gopalsami
10227              * Commented p_rematch and added validation for
10228          * VAT regime.
10229          */
10230       /*following elsif block modified for bug 6595773*/
10231              ELSIF r_vat_regime_tax_type.tax_type IS NOT NULL THEN
10232              --p_rematch = 'PO_MATCHING' then
10233 
10234               v_dist_code_combination_id := for_dist_insertion_rec.dist_code_combination_id;
10235         fnd_file.put_line(FND_FILE.LOG,
10236                    ' Regime type , CCID '
10237        || r_vat_regime_tax_type.tax_type
10238              ||', ' || v_dist_code_combination_id);
10239 
10240             end if;
10241 
10242           else  /* 5763527 introduced for PROJETCS COSTING Impl */
10243             /* 5763527 */
10244             ln_project_id           := p_project_id;
10245             ln_task_id              := p_task_id;
10246             lv_exp_type             := p_expenditure_type;
10247             ld_exp_item_date        := p_expenditure_item_date;
10248             ln_exp_organization_id  := p_expenditure_organization_id;
10249             lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
10250             lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
10251 
10252             --non recoverable
10253             lv_tax_type := 'NR';  --added by eric for inclusive tax on 20-dec,2007
10254           end if; /*nvl(c_tax_rec.mod_cr_percentage, 0) = 100  and c_tax_rec.tax_account_id is not null*/
10255 
10256 
10257           /* Bug#4177452*/
10258           if v_dist_code_combination_id is null then
10259             v_dist_code_combination_id := for_dist_insertion_rec.dist_code_combination_id;
10260           end if;
10261 
10262           /* Service End */
10263 
10264           --
10265           -- Begin 5763527
10266           --
10267           ln_rec_tax_amt  := null;
10268           ln_nrec_tax_amt := null;
10269           ln_lines_to_insert := 1; -- Loop controller to insert more than one lines for partially recoverable tax lines in PO
10270 
10271           --added by Eric for inclusive tax on 20-dec-2007,begin
10272           -------------------------------------------------------------------------------------
10273           --exclusive tax or inlcusive tax without project info,  ln_lines_to_insert := 1;
10274           --inclusive recoverable tax with project info,  ln_lines_to_insert := 2;
10275           --PR(partially recoverable) tax is processed in another logic
10276 
10277           IF ( NVL(i.inc_tax_flag,'N') = 'N'
10278                OR ( NVL(i.inc_tax_flag,'N') = 'Y'
10279                     AND p_project_id IS NULL
10280                   )
10281              )
10282           THEN
10283             ln_lines_to_insert := 1;
10284           ELSIF ( NVL(i.inc_tax_flag,'N') = 'Y'
10285                   AND p_project_id IS NOT NULL
10286                   AND lv_tax_type = 'RE'
10287                 )
10288           THEN
10289             ln_lines_to_insert := 2;
10290           END IF;--( NVL(i.inc_tax_flag,'N') = 'N' OR ( NVL(i.inc_tax_flag,'N'))
10291           -------------------------------------------------------------------------------------
10292           --added by Eric for inclusive tax on 20-dec-2007,end
10293 
10294           Fnd_File.put_line(Fnd_File.LOG, 'i.modvat_flag ='||i.modvat_flag ||',c_tax_rec.mod_cr_percentage='||c_tax_rec.mod_cr_percentage);
10295 
10296           if i.modvat_flag = jai_constants.YES
10297           and nvl(c_tax_rec.mod_cr_percentage, -1) > 0
10298           and nvl(c_tax_rec.mod_cr_percentage, -1) < 100
10299           then
10300             --
10301             -- Tax line is for partial Recoverable tax.  Hence split amount into two parts, Recoverable and Non-Recoverable
10302             -- and instead of one line, two lines needs to be inserted.
10303             -- For ordinary lines (with modvat_flag = 'N' or with mod_cr_percentage = 100) there will be only one line inserted
10304             --
10305 
10306             ln_lines_to_insert := 2 *ln_lines_to_insert; --changed by eric for inclusive tax on Jan 4,2008
10307       ln_rec_tax_amt  :=  round ( nvl(v_tax_amount,0) * (c_tax_rec.mod_cr_percentage/100) , c_tax_rec.rounding_factor) ;
10308       ln_nrec_tax_amt :=  round ( nvl(v_tax_amount,0) - nvl(ln_rec_tax_amt,0) , c_tax_rec.rounding_factor) ;
10309       /* Added Tax rounding based on tax rounding factor for bug 6761425 by vumaasha */
10310 
10311             lv_tax_type := 'PR';  --changed by eric for inclusive tax on Jan 4,2008
10312           end if;
10313 
10314           fnd_file.put_line(fnd_file.log, 'ln_lines_to_insert='||ln_lines_to_insert||
10315                                         ',ln_rec_tax_amt='||ln_rec_tax_amt          ||
10316                                         ',ln_nrec_tax_amt='||ln_nrec_tax_amt
10317                          );
10318 
10319           --
10320           --  If a line has a partially recoverable tax the following loop will be executed twice.  First line will always be for a
10321           --  non recoverable tax amount and the second line will be for a recoverable tax amount
10322           --  For ordinary lines (with modvat_flag = 'N' or with mod_cr_percentage = 100 fully recoverable) the variable
10323           --  ln_lines_to_insert will have value of 1 and hence only one line will be inserted with full tax amount
10324           --
10325 
10326           for line in 1..ln_lines_to_insert
10327           loop
10328           /*commented out by eric for inclusive tax
10329 
10330           if line = 1 then
10331 
10332             v_tax_amount  := nvl(ln_rec_tax_amt, v_tax_amount);
10333             lv_modvat_flag := i.modvat_flag ;
10334 
10335           elsif line = 2 then
10336 
10337             v_tax_amount := ln_nrec_tax_amt;
10338             v_assets_tracking_flag := jai_constants.YES;
10339             lv_modvat_flag := jai_constants.NO ;
10340 
10341             --
10342             -- This is a non recoverable line hence the tax amounts should be added into the project costs by populating
10343             -- projects related columns so that PROJECTS can consider this line for Project Costing
10344             --
10345 
10346             ln_project_id           := p_project_id;
10347             ln_task_id              := p_task_id;
10348             lv_exp_type             := p_expenditure_type;
10349             ld_exp_item_date        := p_expenditure_item_date;
10350             ln_exp_organization_id  := p_expenditure_organization_id;
10351             lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
10352             lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
10353 
10354             -- For non recoverable line charge account should be same as of the parent line
10355             v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id;
10356 
10357             v_distribution_no := v_distribution_no + 1;
10358 
10359           end if;
10360 
10361           ln_inv_line_num := ln_inv_line_num + 1;
10362 
10363           --
10364           -- End 5763527
10365           --
10366           */
10367 
10368           --added by Eric for inclusive tax on 20-dec-2007,begin
10369           -------------------------------------------------------------------------------------
10370           IF (NVL(i.inc_tax_flag,'N') = 'N')--exclusive case
10371           THEN
10372             IF line = 1 then
10373 
10374               v_tax_amount  := nvl(ln_rec_tax_amt, v_tax_amount);
10375               lv_tax_line_amount:=  v_tax_amount       ;   --added by eric for inclusive tax
10376               lv_modvat_flag := i.modvat_flag ;
10377 
10378               lv_ap_line_to_inst_flag  := 'Y'; --added by eric for inclusive tax
10379               lv_tax_line_to_inst_flag := 'Y'; --added by eric for inclusive tax
10380             ELSIF line = 2 then
10381 
10382               v_tax_amount             := ln_nrec_tax_amt;
10383               lv_tax_line_amount       :=  v_tax_amount  ; --added by eric for inclusive tax
10384               lv_ap_line_to_inst_flag  := 'Y';             --added by eric for inclusive tax
10385               lv_tax_line_to_inst_flag := 'Y';             --added by eric for inclusive tax
10386 
10387 
10388               IF for_dist_insertion_rec.assets_tracking_flag = jai_constants.YES THEN
10389                 v_assets_tracking_flag := jai_constants.YES;
10390               END IF;
10391 
10392               lv_modvat_flag := jai_constants.NO ;
10393 
10394               --
10395               -- This is a non recoverable line hence the tax amounts should be added into the project costs by populating
10396               -- projects related columns so that PROJECTS can consider this line for Project Costing
10397               --
10398               IF nvl(lv_accrue_on_receipt_flag,'#') <> 'Y' THEN -- Bug 6338371
10399                 ln_project_id           := p_project_id;
10400                 ln_task_id              := p_task_id;
10401                 lv_exp_type             := p_expenditure_type;
10402                 ld_exp_item_date        := p_expenditure_item_date;
10403                 ln_exp_organization_id  := p_expenditure_organization_id;
10404                 lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
10405                 lv_pa_addition_flag         := for_dist_insertion_rec.pa_addition_flag;
10406               END if;
10407 
10408               -- For non recoverable line charge account should be same as of the parent line
10409               v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id;
10410 
10411             END IF; --line = 1
10412           ELSIF (NVL(i.inc_tax_flag,'N') = 'Y')       --inclusive case
10413           THEN
10414             IF( lv_tax_type ='PR')
10415             THEN
10416               IF ( line = 1 )
10417               THEN
10418                 --recoverable part
10419                 v_tax_amount       := ln_rec_tax_amt ;
10420                 lv_tax_line_amount := v_tax_amount   ;   --added by eric for inclusive tax
10421                 lv_modvat_flag     := i.modvat_flag  ;
10422                 lv_ap_line_to_inst_flag  := 'N';         --added by eric for inclusive tax
10423                 lv_tax_line_to_inst_flag := 'Y';         --added by eric for inclusive tax
10424 
10425                 --non recoverable part
10426               ELSIF ( line = 2 )
10427               THEN
10428                 v_tax_amount       := ln_nrec_tax_amt  ;
10429                 lv_tax_line_amount :=  v_tax_amount    ;   --added by eric for inclusive tax
10430                 lv_modvat_flag     := jai_constants.NO ;
10431                 lv_ap_line_to_inst_flag  := 'N';           --added by eric for inclusive tax
10432                 lv_tax_line_to_inst_flag := 'Y';           --added by eric for inclusive tax
10433 
10434                 --recoverable part without project infor
10435               ELSIF ( line = 3 )
10436               THEN
10437                 v_tax_amount                  := ln_rec_tax_amt;
10438                 lv_tax_line_amount            := NULL;
10439 
10440                 lv_ap_line_to_inst_flag  := 'Y';             --added by eric for inclusive tax
10441                 lv_tax_line_to_inst_flag := 'N';             --added by eric for inclusive tax
10442 
10443                 --Project information
10444                 ln_project_id                 := NULL;
10445                 ln_task_id                    := NULL;
10446                 lv_exp_type                   := NULL;
10447                 ld_exp_item_date              := NULL;
10448                 ln_exp_organization_id        := NULL;
10449                 lv_project_accounting_context := NULL;
10450                 lv_pa_addition_flag           := NULL;
10451 
10452                 -- For inclusive recoverable line charge account should be same as of the parent line
10453                 v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id ;
10454 
10455                 --recoverable part in negative amount with project infor
10456               ELSIF ( line = 4 )
10457               THEN
10458                 v_tax_amount                  := NVL(ln_rec_tax_amt, v_tax_amount)* -1;
10459                 lv_tax_line_amount            := NULL;
10460                 lv_ap_line_to_inst_flag  := 'Y';             --added by eric for inclusive tax
10461                 lv_tax_line_to_inst_flag := 'N';             --added by eric for inclusive tax
10462 
10463                 --Project information
10464                 ln_project_id                 := p_project_id;
10465                 ln_task_id                    := p_task_id;
10466                 lv_exp_type                   := p_expenditure_type;
10467                 ld_exp_item_date              := p_expenditure_item_date;
10468                 ln_exp_organization_id        := p_expenditure_organization_id;
10469                 lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
10470                 lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
10471 
10472                 -- For inclusive recoverable line charge account should be same as of the parent line
10473                 v_dist_code_combination_id  :=  for_dist_insertion_rec.dist_code_combination_id ;
10474               END IF; --line = 1
10475             ELSIF ( lv_tax_type = 'RE'
10476                     AND p_project_id IS NOT NULL
10477                   )
10478             THEN
10479               --recoverable tax without project infor
10480               IF ( line = 1 )
10481               THEN
10482                 v_tax_amount       :=  v_tax_amount  ;
10483                 lv_tax_line_amount :=  v_tax_amount  ;    --added by eric for inclusive tax
10484                 lv_modvat_flag     :=  i.modvat_flag ;
10485                 lv_ap_line_to_inst_flag  := 'Y';          --added by eric for inclusive tax
10486                 lv_tax_line_to_inst_flag := 'Y';          --added by eric for inclusive tax
10487 
10488                 ln_project_id                 := NULL;
10489                 ln_task_id                    := NULL;
10490                 lv_exp_type                   := NULL;
10491                 ld_exp_item_date              := NULL;
10492                 ln_exp_organization_id        := NULL;
10493                 lv_project_accounting_context := NULL;
10494                 lv_pa_addition_flag           := NULL;
10495                 v_dist_code_combination_id    := for_dist_insertion_rec.dist_code_combination_id ;
10496               --recoverable tax in negative amount with project infor
10497               ELSIF ( line = 2 )
10498               THEN
10499                 v_tax_amount       :=  v_tax_amount * -1;
10500                 lv_tax_line_amount :=  NULL ;   --added by eric for inclusive tax
10501                 lv_modvat_flag     :=  i.modvat_flag  ;
10502 
10503                 ln_project_id                 := p_project_id;
10504                 ln_task_id                    := p_task_id;
10505                 lv_exp_type                   := p_expenditure_type;
10506                 ld_exp_item_date              := p_expenditure_item_date;
10507                 ln_exp_organization_id        := p_expenditure_organization_id;
10508                 lv_project_accounting_context := for_dist_insertion_rec.project_accounting_context;
10509                 lv_pa_addition_flag           := for_dist_insertion_rec.pa_addition_flag;
10510                 v_dist_code_combination_id    := for_dist_insertion_rec.dist_code_combination_id ;
10511               END IF;
10512             --ELSIF ( lv_tax_type <> 'PR' AND p_project_id IS NULL )
10513             --THEN
10514             ELSE -- eric removed the above criteria for bug 6888665 and 6888209
10515               Fnd_File.put_line(Fnd_File.LOG, 'NOT Inclusive PR Tax,NOT Inclusive RE for project ');
10516               --The case process the inclusive NR tax and inclusive RE tax not for project
10517 
10518               lv_tax_line_amount := v_tax_amount   ;   --added by eric for inclusive tax
10519               lv_modvat_flag     := i.modvat_flag  ;
10520               lv_ap_line_to_inst_flag  := 'N';         --added by eric for inclusive tax
10521               lv_tax_line_to_inst_flag := 'Y';         --added by eric for inclusive tax
10522 
10523             END IF;--( tax_type ='PR' and (ln_lines_to_insert =4 OR )
10524           END IF; --(NVL(r_tax_lines_r ec.inc_tax_flag,'N') = 'N')
10525           -------------------------------------------------------------------------------------------
10526           --added by Eric for inclusive tax on 20-dec-2007,end
10527 
10528 
10529           Fnd_File.put_line(Fnd_File.LOG,
10530           'Before inserting into ap_invoice_lines_all for line no :' || ln_inv_line_num );
10531 
10532           --insert exclusive tax to the ap tables
10533           --or insert recoverable inclusive tax with project information the ap tables
10534 
10535 
10536           --1.Only exlusive taxes or inclusive recoverable taxes with project information
10537           --  are  inserted into Ap Lines and Dist Lines
10538 
10539           --2.All taxes need to be inserted into jai tax tables. Futher the
10540           --  partially recoverable tax need to be splitted into 2 lines
10541 
10542           --added by Eric for inclusive tax on 20-dec-2007,begin
10543           ------------------------------------------------------------------------
10544           IF ( NVL(lv_ap_line_to_inst_flag,'N')='Y')
10545           THEN
10546             ln_inv_line_num := ln_inv_line_num + 1; --added by eric for inlcusive tax
10547           ---------------------------------------------------------------------------
10548           --added by Eric for inclusive tax on 20-dec-2007,end
10549 
10550             INSERT INTO ap_invoice_lines_all
10551             (
10552                 INVOICE_ID
10553               , LINE_NUMBER
10554               , LINE_TYPE_LOOKUP_CODE
10555               , DESCRIPTION
10556               , ORG_ID
10557               , MATCH_TYPE
10558         , DEFAULT_DIST_CCID --Changes by nprashar for bug #6995437
10559               , ACCOUNTING_DATE
10560               , PERIOD_NAME
10561               , DEFERRED_ACCTG_FLAG
10562               , DEF_ACCTG_START_DATE
10563               , DEF_ACCTG_END_DATE
10564               , DEF_ACCTG_NUMBER_OF_PERIODS
10565               , DEF_ACCTG_PERIOD_TYPE
10566               , SET_OF_BOOKS_ID
10567               , AMOUNT
10568               , WFAPPROVAL_STATUS
10569               , CREATION_DATE
10570               , CREATED_BY
10571               , LAST_UPDATED_BY
10572               , LAST_UPDATE_DATE
10573               , LAST_UPDATE_LOGIN
10574               /* 5763527 */
10575               , project_id
10576               , task_id
10577               , expenditure_type
10578               , expenditure_item_date
10579               , expenditure_organization_id
10580               /* End 5763527 */
10581         ,po_distribution_id --Added for bug#6780154
10582 
10583             )
10584             VALUES
10585             (
10586                 inv_id
10587               , ln_inv_line_num
10588               , lv_misc
10589               , c_tax_rec.tax_name
10590               , v_org_id
10591               , lv_match_type
10592         , v_dist_code_combination_id
10593               , rec_max_ap_lines_all.accounting_date
10594               , rec_max_ap_lines_all.period_name
10595               , rec_max_ap_lines_all.deferred_acctg_flag
10596               , rec_max_ap_lines_all.def_acctg_start_date
10597               , rec_max_ap_lines_all.def_acctg_end_date
10598               , rec_max_ap_lines_all.def_acctg_number_of_periods
10599               , rec_max_ap_lines_all.def_acctg_period_type
10600               , rec_max_ap_lines_all.set_of_books_id
10601               , ROUND(ROUND(v_tax_amount,ln_tax_precision),ln_precision) --Added by nprashar for bug # 13653889
10602               , rec_max_ap_lines_all.wfapproval_status  -- Bug 4863208
10603               , sysdate
10604               , ln_user_id
10605               , ln_user_id
10606               , sysdate
10607               , ln_login_id
10608               /* 5763527 */
10609               , ln_project_id
10610               , ln_task_id
10611               , lv_exp_type
10612               , ld_exp_item_date
10613               , ln_exp_organization_id
10614               /* End 5763527 */
10615           ,po_dist_id --Added for bug#6780154
10616              );
10617 
10618             Fnd_File.put_line(Fnd_File.LOG,
10619             'Before inserting into ap_invoice_distributions_all for distribution line no :'
10620             || v_distribution_no);
10621 
10622             -- Start bug#3332988
10623             open c_get_invoice_distribution;
10624             fetch c_get_invoice_distribution into v_invoice_distribution_id;
10625             close c_get_invoice_distribution;
10626             -- End bug#3332988
10627             fnd_file.put_line(FND_FILE.LOG, ' Invoice distribution id '|| v_invoice_distribution_id);
10628 
10629             INSERT INTO ap_invoice_distributions_all
10630             (
10631             accounting_date,
10632             accrual_posted_flag,
10633             assets_addition_flag,
10634             assets_tracking_flag,
10635             cash_posted_flag,
10636             distribution_line_number,
10637             dist_code_combination_id,
10638             invoice_id,
10639             last_updated_by,
10640             last_update_date,
10641             line_type_lookup_code,
10642             period_name,
10643             set_of_books_id ,
10644             amount,
10645             base_amount,
10646             batch_id,
10647             created_by,
10648             creation_date,
10649             description,
10650              exChange_rate_variance,
10651             last_update_login,
10652             match_status_flag,
10653             posted_flag,
10654             rate_var_code_combination_id ,
10655             reversal_flag ,
10656             program_application_id,
10657             program_id,
10658             program_update_date,
10659             accts_pay_code_combination_id,
10660             invoice_distribution_id,
10661             quantity_invoiced,
10662             po_distribution_id ,
10663             rcv_transaction_id,
10664             org_id,
10665             matched_uom_lookup_code
10666             ,invoice_line_number
10667       ,charge_applicable_to_dist_id -- Bug 5401111. Added by Lakshmi Gopalsami
10668             /* 5763527 */
10669             , project_id
10670             , task_id
10671             , expenditure_type
10672             , expenditure_item_date
10673             , expenditure_organization_id
10674             , project_accounting_context
10675             , pa_addition_flag
10676             /* End of 5763527 */
10677             -- Bug 7249100. Added by Lakshmi Gopalsami
10678             ,distribution_class
10679       ,asset_book_type_code /* added for bug 8406404 by vumaasha */
10680             )
10681             VALUES
10682             (
10683             rec_max_ap_lines_all.accounting_date,
10684             'N', --for_dist_insertion_rec.accrual_posted_flag,
10685             for_dist_insertion_rec.assets_addition_flag,
10686             v_assets_tracking_flag , -- for_dist_insertion_rec.assets_tracking_flag, bug#2851123
10687             'N',
10688             -- dln,
10689             v_distribution_no,
10690             v_dist_code_combination_id,
10691             inv_id,
10692             ln_user_id,
10693             sysdate,
10694             lv_misc,
10695             rec_max_ap_lines_all.period_name,
10696             rec_max_ap_lines_all.set_of_books_id ,
10697             ROUND(ROUND(v_tax_amount,ln_tax_precision),ln_precision), --Added by nprashar for bug # 13653889
10698             ROUND(ROUND(v_tax_amount * for_dist_insertion_rec.exchange_rate, ln_tax_precision),ln_precision), --Added by nprashar for bug # 13653889
10699             v_batch_id,
10700             ln_user_id,
10701             sysdate,
10702             c_tax_rec.tax_name,
10703             null,--kunkumar for forward porting to R12
10704             ln_login_id,
10705             for_dist_insertion_rec.match_status_flag , -- Bug 4863208
10706             'N',
10707             NULL,
10708             for_dist_insertion_rec.reversal_flag,  -- Bug 4863208
10709             for_dist_insertion_rec.program_application_id,
10710             for_dist_insertion_rec.program_id,
10711             for_dist_insertion_rec.program_update_date,
10712             for_dist_insertion_rec.accts_pay_code_combination_id,
10713             v_invoice_distribution_id,
10714             decode(v_assets_tracking_flag, 'Y', NULL, 0), /* Modified for bug 8406404 by vumaasha */
10715             po_dist_id ,
10716             rcv_tran_id,
10717             for_dist_insertion_rec.org_id,  -- Bug 4863208
10718             for_dist_insertion_rec.matched_uom_lookup_code,
10719             ln_inv_line_num
10720       -- Bug 5401111. Added by Lakshmi Gopalsami
10721       ,decode(v_assets_tracking_flag,'N',
10722               NULL,for_dist_insertion_rec.invoice_distribution_id)
10723 /*Commented the account_type condition for bug#7008161 by JMEENA
10724        decode(lv_account_type, 'A',
10725               for_dist_insertion_rec.invoice_distribution_id,NULL)
10726             )
10727 */
10728             /* 5763527 */
10729             , ln_project_id
10730             , ln_task_id
10731             , lv_exp_type
10732             , ld_exp_item_date
10733             , ln_exp_organization_id
10734             , lv_project_accounting_context
10735             , lv_pa_addition_flag
10736             /* End Of 5763527 */
10737             -- Bug 7249100. Added by Lakshmi Gopalsami
10738             , lv_dist_class
10739       , decode(v_assets_tracking_flag,'Y',for_dist_insertion_rec.asset_book_type_code,NULL) /* added for bug 8406404 by vumaasha */
10740             );
10741           --added by Eric for inclusive tax on 20-dec-2007,begin
10742           -----------------------------------------------------------------
10743           END IF;--( NVL(lv_ap_line_to_inst_flag,'N')='Y') )
10744           -----------------------------------------------------------------
10745           --added by Eric for inclusive tax on 20-dec-2007,end
10746 
10747 
10748             Fnd_File.put_line(Fnd_File.LOG,
10749             'Before inserting into JAI_AP_MATCH_INV_TAXES tax id ' || i.tax_id );
10750           --added by Eric for inclusive tax on 20-dec-2007,begin
10751           -----------------------------------------------------------------
10752           IF  (NVL(lv_tax_line_to_inst_flag,'N') = 'Y')
10753           THEN
10754           -----------------------------------------------------------------
10755           --added by Eric for inclusive tax on 20-dec-2007,end
10756             INSERT INTO JAI_AP_MATCH_INV_TAXES
10757             (
10758             tax_distribution_id,
10759             exchange_rate_variance,
10760             assets_tracking_flag,
10761             invoice_id,
10762             po_header_id,
10763             po_line_id,
10764             line_location_id,
10765             set_of_books_id,
10766             --org_id,
10767             exchange_rate,
10768             exchange_rate_type,
10769             exchange_date,
10770             currency_code,
10771             code_combination_id,
10772             last_update_login,
10773             creation_date,
10774             created_by,
10775             last_update_date,
10776             last_updated_by,
10777             acct_pay_code_combination_id,
10778             accounting_date,
10779             tax_id,
10780             tax_amount,
10781             base_amount,
10782             chart_of_accounts_id,
10783             distribution_line_number,
10784             po_distribution_id,
10785             parent_invoice_distribution_id,
10786             legal_entity_id
10787             ,INVOICE_LINE_NUMBER
10788             ,INVOICE_DISTRIBUTION_ID
10789             ,PARENT_INVOICE_LINE_NUMBER
10790             ,RCV_TRANSACTION_ID
10791             ,LINE_TYPE_LOOKUP_CODE
10792             ,recoverable_flag -- 5763527
10793             ,line_no            -- added, Harshita for Bug 5553150
10794             )
10795             VALUES
10796             (
10797             JAI_AP_MATCH_INV_TAXES_S.NEXTVAL,
10798             null,--kunkumar for forward porting to R12
10799             v_assets_tracking_flag , -- 'N', bug#2851123
10800             inv_id,
10801             cur_items_rec.po_header_id,
10802             cur_items_rec.po_line_id,
10803             cur_items_rec.line_location_id,
10804             cur_items_rec.set_of_books_id,
10805             --cur_items_rec.org_id,
10806             cur_items_rec.rate,
10807             cur_items_rec.rate_type,
10808             cur_items_rec.rate_date,
10809             cur_items_rec.currency_code,
10810       /* Bug 5358788. Added by Lakshmi Gopalsami. Commented
10811              * c_tax_rec.tax_account_id and v_dist_code_combination_id
10812        */
10813             v_dist_code_combination_id,
10814             cur_items_rec.last_update_login,
10815             cur_items_rec.creation_date,
10816             cur_items_rec.created_by,
10817             cur_items_rec.last_update_date,
10818             cur_items_rec.last_updated_by,
10819             v_dist_code_combination_id,
10820             cur_items_rec.invoice_date,
10821             i.tax_id,
10822             /*commented out by eric for inclusive tax
10823             round(v_tax_amount,ln_precision),
10824             */
10825             ROUND(ROUND(lv_tax_line_amount,ln_tax_precision),ln_precision), --Added by nprashar for bug # 13653889
10826             ROUND(ROUND(i.tax_amount, ln_tax_precision),ln_precision), --Added by nprashar for bug # 13653889
10827             caid,
10828             -- dln,
10829             v_distribution_no,
10830             po_dist_id, -- added  by bug#3038566
10831             for_dist_insertion_rec.invoice_distribution_id, -- added  by bug#3038566
10832             get_ven_info_rec.legal_entity_id  -- added by rallamse bug#
10833             --ln_inv_line_num, deletedt by eric for inclusive tax
10834             --, v_invoice_distribution_id, deletedt by eric for inclusive tax
10835             --added by Eric for inclusive tax on 20-dec-2007,begin
10836             --------------------------------------------------------------
10837             , DECODE ( NVL(i.inc_tax_flag,'N')
10838                      , 'N',ln_inv_line_num
10839                      , 'Y',pn_invoice_line_number
10840                      )
10841             ,NVL(v_invoice_distribution_id,for_dist_insertion_rec.invoice_distribution_id)
10842             --------------------------------------------------------------
10843             --added by Eric for inclusive tax on 20-dec-2007,end
10844             , pn_invoice_line_number
10845             , rcv_tran_id
10846             , lv_misc
10847             , lv_modvat_flag -- 5763527
10848             , i.tax_line_no  -- added, Harshita for Bug 5553150
10849             );
10850           --added by Eric for inclusive tax on 20-dec-2007,begin
10851           -----------------------------------------------------------------
10852           END IF;--  (NVL(lv_tax_line_to_inst_flag,'N') = 'Y')
10853           -----------------------------------------------------------------
10854           --added by Eric for inclusive tax on 20-dec-2007,end
10855 
10856             /* Bug 46863208. Added by Lakshmi Gopalsami
10857              Commented the MRC call
10858             insert_mrc_data(v_invoice_distribution_id); -- bug#3332988
10859       */
10860       /* commented out by Eric for bug 8345512 on 13-Jul-2009
10861         cum_tax_amt := cum_tax_amt + ROUND(v_tax_amount, ln_precision);
10862       */
10863 
10864 
10865           --Modified by Eric for bug 8345512 on 13-Jul-2009,begin
10866           ------------------------------------------------------------------
10867           IF NVL(i.inc_tax_flag,'N') ='N'
10868           THEN
10869             cum_tax_amt := cum_tax_amt + ROUND(round(v_tax_amount,ln_tax_precision), ln_precision); /* Bug 14386866 */
10870           END IF;
10871           ------------------------------------------------------------------
10872           --Modified by Eric for bug 8345512 on 13-Jul-2009,end
10873 
10874             ---------------------------------------------------------------------------------------------
10875 
10876             v_statement_no := '50';
10877 
10878             /* Obsoleted as part of R12 , Bug# 4445989
10879             Fnd_File.put_line(Fnd_File.LOG, 'Before inserting into JAI_CMN_FA_INV_DIST_ALL');
10880             INSERT INTO JAI_CMN_FA_INV_DIST_ALL
10881             (
10882             invoice_id,
10883             invoice_distribution_id,
10884             set_of_books_id,
10885             batch_id,
10886             po_distribution_id,
10887             rcv_transaction_id,
10888             dist_code_combination_id,
10889             accounting_date,
10890             assets_addition_flag,
10891             assets_tracking_flag,
10892             distribution_line_number,
10893             line_type_lookup_code,
10894             amount,
10895             description,
10896             match_status_flag,
10897             quantity_invoiced
10898             )
10899             VALUES
10900             (
10901             inv_id,
10902             ap_invoice_distributions_s.CURRVAL,
10903             for_dist_insertion_rec.set_of_books_id,
10904             v_batch_id,
10905             for_dist_insertion_rec.po_distribution_id,
10906             rcv_tran_id,
10907             v_dist_code_combination_id,
10908             for_dist_insertion_rec.accounting_date,
10909             for_dist_insertion_rec.assets_addition_flag,
10910             v_assets_tracking_flag , -- for_dist_insertion_rec.assets_tracking_flag, bug#2851123
10911             v_distribution_no,
10912             'MISCELLANEOUS',
10913             ROUND(v_tax_amount, ln_precision),
10914             c_tax_rec.tax_name,
10915             NULL,
10916             NULL);*/
10917 
10918             --  end modification for ap to fa modavatable taxes issue. on 19-mar-01 by subbu and pavan
10919 
10920           END LOOP; --> for line in  -- 5763527
10921       END LOOP;
10922 
10923       -- check to avoid amount updation in the invoice header when any of the dist lines are reversed
10924       v_dist_reversal_cnt := 0; -- Added by avallabh for bug 4926094 on 03-Feb-2006
10925       OPEN c_dist_reversal_cnt(inv_id);
10926       FETCH c_dist_reversal_cnt INTO v_dist_reversal_cnt;
10927       CLOSE c_dist_reversal_cnt;
10928 
10929       if v_dist_reversal_cnt = 0 then
10930         v_statement_no := '66';
10931 
10932         v_update_payment_schedule:=update_payment_schedule(cum_tax_amt); -- bug#3218978
10933 --        cum_tax_amt := round(cum_Tax_amt,ln_precision);
10934         UPDATE ap_invoices_all
10935         SET invoice_amount = invoice_amount + cum_tax_amt,
10936         approved_amount = approved_amount + cum_tax_amt,
10937         pay_curr_invoice_amount = pay_curr_invoice_amount + cum_tax_amt,
10938         amount_applicable_to_discount = amount_applicable_to_discount + cum_tax_amt,
10939         payment_status_flag = decode(payment_status_flag, 'Y', 'P', payment_status_flag)
10940         -- bug#3624898
10941         WHERE invoice_id = inv_id;
10942 
10943         -- start added for bug#3354932
10944         if for_org_id_rec.invoice_currency_code <> v_functional_currency then
10945           -- invoice currency is not the functional currency.
10946           update ap_invoices_all
10947           set    base_amount = invoice_amount  * exchange_rate
10948           where  invoice_id = inv_id;
10949         end if;
10950         -- end added for bug#3354932
10951 
10952       /* Bug 4863208. Added by Lakshmi Gopalsami
10953          Commented the MRC call
10954         update_mrc_data; -- bug#3332988
10955      */
10956 
10957       end if; -- v_dist_reversal_cnt = 0
10958 
10959     ELSE
10960 
10961         err_mesg :=
10962         'This procedure should be called for RCV_MATCHING only, the input parameter is : '
10963         || p_rematch;
10964 
10965       RETURN;
10966 
10967     END IF; -- if rcv_tran_id is not null
10968 
10969   END IF; -- 'PAY_ON_RECEIPT'
10970 
10971 
10972   EXCEPTION
10973       WHEN OTHERS THEN
10974       err_mesg := SQLERRM;
10975       Fnd_File.put_line(Fnd_File.LOG, 'SQLERRM -> '|| SQLERRM ||', SQLCODE -> '||SQLCODE);
10976       Fnd_File.put_line(Fnd_File.LOG, 'Statement -> '|| v_statement_no);
10977       RETURN;
10978   END process_batch_record;
10979 
10980 END;