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