DBA Data[Home] [Help]

PACKAGE BODY: APPS.JAI_AR_IMPORT_POPU_PKG

Source


1 PACKAGE BODY JAI_AR_IMPORT_POPU_PKG AS
2 /* $Header: jai_ar_import_popu.plb 120.7.12020000.1 2013/03/12 07:16:31 appldev noship $ */
3 
4 
5   G_CURRENT_RUNTIME_LEVEL CONSTANT NUMBER := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
6   G_LEVEL_UNEXPECTED      CONSTANT NUMBER := FND_LOG.LEVEL_UNEXPECTED;
7   G_LEVEL_ERROR           CONSTANT NUMBER := FND_LOG.LEVEL_ERROR;
8   G_LEVEL_EXCEPTION       CONSTANT NUMBER := FND_LOG.LEVEL_EXCEPTION;
9   G_LEVEL_EVENT           CONSTANT NUMBER := FND_LOG.LEVEL_EVENT;
10   G_LEVEL_PROCEDURE       CONSTANT NUMBER := FND_LOG.LEVEL_PROCEDURE;
11   G_LEVEL_STATEMENT       CONSTANT NUMBER := FND_LOG.LEVEL_STATEMENT;
12   G_MODULE_NAME           CONSTANT VARCHAR2(40) := 'JAI.PLSQL.JAI_AR_IMPORT_POPU_PKG.';
13   G_PKG_NAME              CONSTANT VARCHAR2(30) := 'JAI_AR_IMPORT_POPU_PKG';
14 
15 
16 
17 procedure process_order(pr_old t_rec%type ,
18           pr_new t_rec%type ,
19           pv_once_complete_flag varchar2,
20           pv_action varchar2 ,
21           pv_return_code out nocopy varchar2 ,
22           pv_return_message out nocopy varchar2)
23 is
24 --Cursors
25   v_interface_line_attribute3   VARCHAR2(30); -- :=  pr_new.interface_line_attribute3;
26   v_interface_line_attribute6   VARCHAR2(30);
27   v_customer_trx_line_id        number;
28   v_header_id                   number;
29   v_register_code               JAI_OM_OE_BOND_REG_HDRS.REGISTER_CODE%TYPE;
30   CURSOR SO_PICKING_RECORD_CHECK IS
31   SELECT 1
32   FROM   JAI_OM_WSH_LINES_ALL A,WSH_NEW_DELIVERIES B
33   WHERE  A.delivery_id = B.DELIVERY_ID AND
34          B.NAME = v_interface_line_attribute3
35   AND    A.order_line_id         = TO_NUMBER(v_interface_line_attribute6);
36 
37 
38 
39   CURSOR complete_flag_cur IS
40   SELECT complete_flag
41   FROM   ra_customer_trx_all
42   WHERE  customer_trx_id = v_header_id;
43 
44   CURSOR OLD_CUSTOMER_TRX_ID_CUR IS
45   SELECT Customer_Trx_Id
46     FROM   JAI_AR_TRX_LINES
47    WHERE  customer_trx_LINE_id = v_customer_trx_line_id;
48 
49   cursor c_bill_only_invoice(cp_customer_trx_line_id number, cp_process_name oe_wf_line_assign_v.process_name%type ) is
50   select  1
51   from    oe_wf_order_assign_v o_wf_asg
52   where   order_type_name = pr_new.interface_line_attribute2
53   and     exists
54   (
55     select  1
56     from    oe_wf_line_assign_v  l_wf_asg
57     where   assignment_id = o_wf_asg.assignment_id
58     and     process_name  = cp_process_name  /*'R_BILL_ONLY' Ramananda for removal of SQL LITERALs */
59     and     order_type_id = l_wf_asg.order_type_id
60    );
61 
62   cursor c_ont_source_code is
63   select FND_PROFILE.VALUE('ONT_SOURCE_CODE')
64   from   dual;
65 
66   CURSOR JA_SO_TAX_LINES_INFO IS
67   SELECT A.tax_line_no, A.uom, A.tax_id, A.tax_rate, A.qty_rate, A.base_tax_amount,A.tax_amount,
68          A.func_tax_amount,
69    A.precedence_1, A.precedence_2, A.precedence_3, A.precedence_4, A.precedence_5,
70    A.precedence_6, A.precedence_7, A.precedence_8, A.precedence_9, A.precedence_10,
71    c.tax_type
72   FROM   JAI_OM_OE_SO_TAXES A, JAI_OM_OE_SO_LINES  b, JAI_CMN_TAXES_ALL c
73   WHERE  A.line_id = b.line_id
74   AND    b.line_id   = TO_NUMBER(v_interface_line_attribute6)
75   AND    A.tax_id    = c.tax_id
76   and    c.tax_type <> lc_modvat_tax
77   ORDER BY A.tax_line_no;
78 
79   CURSOR c_jai_ar_trx_lines(pn_customer_trx_id NUMBER,pn_customer_trx_line_id NUMBER) IS
80   SELECT *
81   FROM
82         JAI_AR_TRX_LINES
83   WHERE customer_trx_line_id = pn_customer_trx_line_id
84   AND   customer_trx_id      = pn_customer_trx_id;
85   t_jai_line_rec_old    JAI_AR_TRX_LINES%ROWTYPE;
86   t_jai_line_rec_new    JAI_AR_TRX_LINES%ROWTYPE;
87 
88   cursor c_get_rec(cn_customer_trx_id number)
89   is
90   select *
91   from jai_ar_trxs
92   where customer_trx_id = cn_customer_trx_id;
93 
94   t_rec_new jai_ar_trxs%rowtype;
95   t_rec_old jai_ar_trxs%rowtype;
96 
97   CURSOR SO_PICKING_HDR_INFO IS
98   SELECT A.organization_id, A.location_id
99     FROM   JAI_OM_WSH_LINES_ALL A,WSH_NEW_DELIVERIES B
100    WHERE  A.delivery_id = B.DELIVERY_ID AND
101           B.NAME = v_interface_line_attribute3 AND
102           A.order_line_id         = TO_NUMBER(v_interface_line_attribute6);
103 
104   CURSOR so_picking_hdr_info_1 IS
105   SELECT a.organization_id, a.location_id
106   FROM   JAI_OM_WSH_LINES_ALL a
107   WHERE  a.delivery_id = v_interface_line_attribute3
108   AND    a.organization_id IS NOT NULL
109   AND    a.location_id IS NOT NULL
110   AND    rownum=1 ;
111 
112   CURSOR c_ato_order IS
113   SELECT
114          1
115   FROM
116          oe_order_lines_all
117   WHERE
118          item_type_code IN ('CONFIG', 'MODEL', 'OPTION', 'CLASS')
119     AND  line_id        =  (SELECT ato_line_id
120                             FROM   oe_order_lines_all
121                             WHERE  line_id = v_interface_line_attribute6 );
122 
123   CURSOR c_cust_trx_tax_line_amt IS
124   SELECT nvl(SUM(a.tax_amount),0)
125   FROM   JAI_AR_TRX_TAX_LINES a, JAI_CMN_TAXES_ALL b
126   WHERE  link_to_cust_trx_line_id = v_customer_trx_line_id
127   AND    a.tax_id = b.tax_id
128   AND    nvl(b.inclusive_tax_flag, 'N') = 'N';
129 
130   CURSOR c_ato_hdr_info IS
131   SELECT
132          organization_id, location_id
133   FROM
134          JAI_OM_WSH_LINES_ALL
135   WHERE
136          order_line_id IN  (SELECT line_id
137                             FROM  oe_order_lines_all oel2
138                             WHERE oel2.item_type_code = 'CONFIG'
139                              AND  oel2.header_id = (
140                                                 SELECT header_id
141                                                 FROM  oe_order_lines_all oel
142                                                 WHERE oel.line_id = v_interface_line_attribute6)
143                              AND oel2.ato_line_id = (SELECT ato_line_id
144                                                  FROM oe_order_lines_all oel1
145                                                  WHERE oel1.line_id = v_interface_line_attribute6))
146   AND    organization_id is not null
147   AND    location_id     is not null
148   AND    rownum = 1 ;
149 
150 
151 
152   CURSOR c_so_picking_hdr_info_ns
153   IS
154   SELECT jowla.organization_id
155        , jowla.location_id
156   FROM   JAI_OM_WSH_LINES_ALL jowla
157   WHERE  order_line_id = TO_NUMBER(v_interface_line_attribute6)
158     AND  shippable_flag = 'N';
159 
160   CURSOR c_so_picking_tax_record_ns
161   IS
162   SELECT DISTINCT 1
163   FROM   JAI_OM_WSH_LINE_TAXES
164   WHERE  delivery_detail_id IS NULL
165     AND  order_line_id    = TO_NUMBER(v_interface_line_attribute6);
166 
167   CURSOR c_so_picking_tax_lines_info_ns
168   IS
169   SELECT   jowla.quantity
170          , jowlt.tax_line_no
171          , jowlt.uom
172          , jowlt.tax_id
173          , jowlt.tax_rate
174          , jowlt.qty_rate
175          , jowlt.base_tax_amount
176          , jowlt.tax_amount
177          , jcta.tax_type
178          , jowlt.func_tax_amount
179          , jowlt.precedence_1
180          , jowlt.precedence_2
181          , jowlt.precedence_3
182          , jowlt.precedence_4
183          , jowlt.precedence_5
184          , jowlt.precedence_6
185          , jowlt.precedence_7
186          , jowlt.precedence_8
187          , jowlt.precedence_9
188          , jowlt.precedence_10
189          , jowla.vat_invoice_no
190          , jowla.vat_invoice_date
191   FROM     JAI_OM_WSH_LINE_TAXES jowlt
192          , JAI_OM_WSH_LINES_ALL jowla
193          , JAI_CMN_TAXES_ALL jcta
194   WHERE    jowlt.delivery_detail_id IS NULL
195     AND    jowlt.order_line_id = jowla.order_line_id
196     AND    jowlt.tax_id= jcta.tax_id
197     AND    jcta.tax_type <> 'Modvat Recovery'
198     AND    jowlt.order_line_id  = TO_NUMBER(v_interface_line_attribute6);
199 
200   CURSOR DUPLICATE_HDR_CUR IS
201   SELECT 1
202   FROM   JAI_AR_TRXS
203   WHERE  customer_trx_id = pr_new.customer_trx_id;
204 
205   CURSOR CREATED_FROM_CUR IS
206   SELECT created_from, trx_number, batch_source_id, set_of_books_id, primary_salesrep_id,
207          invoice_currency_code, exchange_rate_type, exchange_date, exchange_rate
208     FROM   ra_customer_trx_all
209    WHERE  customer_trx_id = pr_new.customer_trx_id;
210 
211   CURSOR SO_PICKING_TAX_RECORD_CHECK IS
212   SELECT DISTINCT 1
213   FROM   JAI_OM_WSH_LINE_TAXES A, JAI_OM_WSH_LINES_ALL  b, WSH_NEW_DELIVERIES C
214   WHERE  A.delivery_detail_id = b.delivery_detail_id
215   AND    b.delivery_id = C.DELIVERY_ID AND
216          c.NAME = v_interface_line_attribute3
217   AND    b.order_line_id         = TO_NUMBER(v_interface_line_attribute6);  --17-Apr-2002
218 
219   CURSOR SO_PICKING_TAX_LINES_INFO IS
220     SELECT b.quantity ,
221            A.tax_line_no,
222            A.uom,
223            A.tax_id,
224            A.tax_rate,
225            A.qty_rate,
226            A.base_tax_amount,
227            A.tax_amount,
228            c.tax_type,
229            A.func_tax_amount,
230      A.precedence_1, A.precedence_2, A.precedence_3, A.precedence_4, A.precedence_5,
231      A.precedence_6, A.precedence_7, A.precedence_8, A.precedence_9, A.precedence_10,
232            b.vat_invoice_no,b.vat_invoice_date
233     FROM   JAI_OM_WSH_LINE_TAXES A,JAI_OM_WSH_LINES_ALL  b,
234            JAI_CMN_TAXES_ALL c, wsh_new_deliveries D
235     WHERE  A.delivery_detail_id = b.delivery_detail_id
236     AND    A.tax_id=c.tax_id
237     and    c.tax_type <> 'Modvat Recovery'
238     AND    b.delivery_id = D.delivery_id
239     AND    D.name = v_interface_line_attribute3
240     AND    b.order_line_id         = TO_NUMBER(v_interface_line_attribute6);
241 
242   CURSOR SO_PICKING_TAX_AMT(p_tax_id NUMBER) IS
243   SELECT SUM(A.base_tax_amount) base_tax_amount,
244            SUM(A.tax_amount) tax_amount,
245            SUM(A.func_tax_amount) func_tax_amount
246   FROM   JAI_OM_WSH_LINE_TAXES A,JAI_OM_WSH_LINES_ALL b,WSH_NEW_DELIVERIES C
247   WHERE  A.delivery_detail_id = b.delivery_detail_id
248   AND    b.delivery_id = c.delivery_id
249   AND    c.NAME = v_interface_line_attribute3
250   AND    b.order_line_id = TO_NUMBER(v_interface_line_attribute6)
251   AND    A.tax_id = p_tax_id
252   GROUP BY A.tax_id;
253 
254   CURSOR DUPLICATE_TAX_LINES_CUR(v_tax_id NUMBER) IS
255   SELECT 1
256     FROM   JAI_AR_TRX_TAX_LINES
257    WHERE  link_to_cust_trx_line_id = v_customer_trx_line_id
258      AND   tax_id = v_tax_id;
259 
260 
261   CURSOR SO_PICKING_LINES_INFO IS
262   SELECT tax_category_id,Quantity ,
263        (tax_amount/quantity) tax_amount,
264            assessable_value, (basic_excise_duty_amount/quantity) basic_excise_duty_amount,
265          (add_excise_duty_amount/quantity) add_excise_duty_amount,
266            (oth_excise_duty_amount/quantity) oth_excise_duty_amount,
267            register, excise_invoice_no,
268          preprinted_excise_inv_no, excise_invoice_date,
269            excise_exempt_type,  excise_exempt_refno, excise_exempt_date
270                 , ar3_form_no, ar3_form_date,
271          vat_exemption_flag, vat_exemption_type, vat_exemption_date, vat_exemption_refno, vat_assessable_value, vat_invoice_no, vat_invoice_date
272   FROM   JAI_OM_WSH_LINES_ALL a,wsh_new_deliveries b
273   WHERE  A.delivery_id = b.delivery_id AND
274          B.NAME = v_interface_line_attribute3 AND
275          A.order_line_id         = TO_NUMBER(v_interface_line_attribute6);
276 
277   CURSOR C_JA_SO_LINES_ASSESSABLE_VAL IS
278   SELECT assessable_value, service_type_code
279        from JAI_OM_OE_SO_LINES
280        WHERE  line_id         = TO_NUMBER(v_interface_line_attribute6);
281 
282   CURSOR c_so_picking_tax_amt_ns(p_tax_id NUMBER)
283   IS
284   SELECT SUM(base_tax_amount) base_tax_amount
285        , SUM(tax_amount) tax_amount
286        , SUM(func_tax_amount) func_tax_amount
287   FROM   JAI_OM_WSH_LINE_TAXES
288   WHERE  delivery_detail_id IS NULL
289     AND  order_line_id = TO_NUMBER(v_interface_line_attribute6)
290     AND  tax_id = p_tax_id
291   GROUP BY tax_id;
292 
293   CURSOR c_so_picking_lines_info_ns
294   IS
295   SELECT tax_category_id,Quantity
296        , (tax_amount/quantity) tax_amount
297        ,  assessable_value
298        , (basic_excise_duty_amount/quantity) basic_excise_duty_amount
299        , (add_excise_duty_amount/quantity) add_excise_duty_amount
300        , (oth_excise_duty_amount/quantity) oth_excise_duty_amount
301        , register
302        , excise_invoice_no
303        , preprinted_excise_inv_no
304        , excise_invoice_date
305        , excise_exempt_type
306        , excise_exempt_refno
307        , excise_exempt_date
308        , ar3_form_no
309        , ar3_form_date
310        , vat_exemption_flag
311        , vat_exemption_type
312        , vat_exemption_date
313        , vat_exemption_refno
314        , vat_assessable_value
315        , vat_invoice_no
316        , vat_invoice_date
317   FROM   JAI_OM_WSH_LINES_ALL
318   WHERE  delivery_id IS NULL
319     AND  order_line_id  = TO_NUMBER(v_interface_line_attribute6);
320 
321   CURSOR JA_SO_LINES_TAX_RECORD_CHECK IS
322   SELECT DISTINCT 1
323   FROM   JAI_OM_OE_SO_TAXES A, JAI_OM_OE_SO_LINES  b
324   WHERE  A.line_id         = TO_NUMBER(v_interface_line_attribute6)
325   AND    A.line_id         = b.line_id;
326 
327   CURSOR c_duplicate_tax(cp_tax_id JAI_AR_TRX_TAX_LINES.tax_id%TYPE,
328                          cp_link_cust_trx_line_id JAI_AR_TRX_TAX_LINES.link_to_cust_trx_line_id%TYPE)
329   IS
330   SELECT 1
331   FROM  JAI_AR_TRX_TAX_LINES
332   WHERE tax_id = cp_tax_id
333   AND   link_to_cust_trx_line_id = cp_link_cust_trx_line_id ;
334 
335   CURSOR ja_so_lines_info IS
336   SELECT tax_category_id, tax_amount, assessable_value, excise_exempt_type,excise_exempt_refno, excise_exempt_date,
337          vat_exemption_flag, vat_exemption_type, vat_exemption_date, vat_exemption_refno, vat_assessable_value   -- added, Harshita for bug#4245062
338          ,service_type_code
339   FROM   JAI_OM_OE_SO_LINES
340   WHERE  line_id         = to_number(v_interface_line_attribute6);
341   rec_so_lines        JA_SO_LINES_INFO%rowtype;
342 
343   CURSOR DUPLICATE_LINES_CUR IS
344   SELECT 1
345     FROM   JAI_AR_TRX_LINES
346    WHERE  customer_trx_LINE_id = v_customer_trx_line_id;
347 
348 --Variables
349   x                             number;
350   v_exist_flag                  NUMBER;
351   ln_first_time                 NUMBER;
352   ln_bill_only                  number;
353   v_so_tax_amount               NUMBER;
354   ln_threshold_tax_cat_id       jai_ap_tds_thhold_taxes.tax_category_id%type;
355   v_ont_source_code ra_interface_lines_all.interface_line_context%type;
356   v_org_id                      NUMBER; --:=pr_new.org_id;
357   lv_action                     VARCHAR2(20)  ;
358   lv_return_message             VARCHAR2(2000);
359   lv_return_code                VARCHAR2(100) ;
360   le_error                      EXCEPTION     ;
361   ln_order_ato                  NUMBER ;
362   v_organization_id             NUMBER;
363   v_location_id                 NUMBER;
364   v_created_from                VARCHAR2(30);
365   v_trx_number                  VARCHAR2(20);
366   v_batch_source_id             NUMBER ;
367   v_books_id                    NUMBER;
368   v_salesrep_id                 NUMBER;
369   v_old_customer_trx_id          NUMBER;
370   c_from_currency_code          VARCHAR2(15);
371   c_conversion_type             VARCHAR2(30);
372   c_conversion_date             DATE;
373   c_conversion_rate             NUMBER;
374   v_last_update_date            DATE; --   := pr_new.last_update_date;
375   v_last_updated_by             NUMBER; -- := pr_new.last_updated_by;
376   v_creation_date               DATE; --   := pr_new.creation_date;
377   v_created_by                  NUMBER; -- := pr_new.created_by;
378   v_last_update_login           NUMBER; -- := pr_new.last_update_login;
379   ln_legal_entity_id            NUMBER ;
380   v_bond_tax_amt                NUMBER;
381   v_tax_line_count              number;
382   v_base_tax_amount             NUMBER;
383   v_tax_amt                     NUMBER;
384   v_func_tax_amount             NUMBER;
385   v_tax_category_id             NUMBER;
386   v_qty                         NUMBER;
387   v_tax_amount                  NUMBER;
388   v_assessable_value            NUMBER;
389   v_basic_excise_duty_amount    NUMBER; --File.Sql.35 Cbabu  := 0;
390   v_add_excise_duty_amount      NUMBER; --File.Sql.35 Cbabu  := 0;
391   v_oth_excise_duty_amount      NUMBER;
392   v_payment_register            VARCHAR2(15);
393   v_excise_invoice_no           VARCHAR2(200);
394   v_excise_invoice_date         DATE;
395   v_preprinted_excise_inv_no    VARCHAR2(50);
396   v_excise_exempt_type          VARCHAR2(60);
397   v_excise_exempt_refno         VARCHAR2(30);
398   v_excise_exempt_date          DATE;
399   v_ar3_form_no                 JAI_AR_TRX_LINES.ar3_form_no%TYPE;
400   v_ar3_form_date               Date;
401   lv_vat_exemption_flag         JAI_AR_TRX_LINES.vat_exemption_flag%TYPE ;
402   lv_vat_exemption_type         JAI_AR_TRX_LINES.vat_exemption_type%TYPE ;
403   lv_vat_exemption_date         JAI_AR_TRX_LINES.vat_exemption_date%TYPE ;
404   lv_vat_exemption_refno        JAI_AR_TRX_LINES.vat_exemption_refno%TYPE ;
405   ln_vat_assessable_value       JAI_AR_TRX_LINES.vat_assessable_value%TYPE;
406   ln_vat_invoice_no             JAI_AR_TRXS.vat_invoice_no %TYPE ;
407   ln_vat_invoice_date           JAI_AR_TRXS.vat_invoice_date%TYPE;
408   v_service_type                VARCHAR2(30);
409   v_line_count                  NUMBER;
410   ln_tax_exist                  NUMBER;
411   v_calc_tax_amount             NUMBER;
412   v_excise_amount               NUMBER;
413   v_once_completed_flag         VARCHAR2(1);
414   v_line_amount                 NUMBER;
415   v_config_line_id              Number;
416   v_ex_inv_no                   JAI_OM_WSH_LINES_ALL.excise_invoice_no%type;
417   v_pmt_reg                     JAI_OM_WSH_LINES_ALL.register%type;
418   v_pre_prnt_ex_no              JAI_OM_WSH_LINES_ALL.preprinted_excise_inv_no%type;
419   v_ato_tax_amount              Number;
420   v_ato_line_amount             Number;
421   v_ato_total_amount            Number;
422 
423 
424   l_api_name CONSTANT VARCHAR2(30) := 'PROCESS_ORDER()';
425 
426 --Main
427 
428   procedure process_bill_only_invoice
429   is
430     cursor c_check_hdr_exists is
431     select 1
432     from   JAI_AR_TRXS
433     where  customer_trx_id = pr_new.customer_trx_id;
434 
435 
436     cursor c_trx_cur is
437     select trx_number               ,
438            batch_source_id          ,
439            set_of_books_id          ,
440            primary_salesrep_id      ,
441            invoice_currency_Code    ,
442            exchange_rate_type       ,
443            exchange_date            ,
444            exchange_rate            ,
445            created_from ,
446            nvl(bill_to_customer_id,ship_to_customer_id) customer_id ,
447            complete_flag,--Added by Zhiwei for ZX integration
448            trx_date
449     from   ra_customer_trx_all
450     where  customer_trx_id = pr_new.customer_trx_id;
451 
452     CURSOR JA_SO_LINES_INFO IS
453     SELECT tax_category_id     ,
454            tax_amount          ,
455            assessable_value    ,
456            line_amount         ,
457            excise_exempt_type  ,
458            excise_exempt_refno ,
459            excise_exempt_date  ,
460            vat_exemption_flag  ,
461            vat_exemption_type  ,
462            vat_exemption_date  ,
463            vat_exemption_refno ,
464            vat_assessable_value,
465            unit_code           ,
466            inventory_item_id   ,
467            quantity            ,
468            service_type_code
469     FROM   JAI_OM_OE_SO_LINES
470     WHERE  line_id         = TO_NUMBER(v_interface_line_attribute6);
471 
472     cursor  c_get_amounts is
473     select  sum(tax_amount) tax_amt , sum(line_amount) line_amt
474     from    JAI_AR_TRX_LINES
475     where   customer_trx_id = pr_new.customer_trx_id;
476 
477     cursor  c_default_location(CP_ORGANIZATION_ID  number) is
478     select  DEFAULT_LOCATION_BILL_ONLY
479     from    JAI_CMN_INVENTORY_ORGS
480     where   organization_id = cp_organization_id
481     and     location_id = 0;
482 
483     cursor  c_oe_system_params is
484     select  master_organization_id
485     from    oe_system_parameters ;
486 
487 
488     CURSOR c_get_vat_inv_no IS
489     SELECT VAT_INVOICE_NO
490     FROM JAI_OM_WSH_LINES_ALL
491     WHERE DELIVERY_ID IS NULL
492     AND   ORDER_LINE_ID = TO_NUMBER(v_interface_line_attribute6);
493 
494     lv_vat_invoice_no   JAI_OM_WSH_LINES_ALL.VAT_INVOICE_NO%TYPE;
495 
496 
497 
498     lr_trx_rec          c_trx_cur%rowtype;
499     ln_hdr_exists       number; --File.Sql.35 Cbabu  := 0;
500     ln_line_amount      number; --File.Sql.35 Cbabu  :=0;
501     ln_tax_amount       number; --File.Sql.35 Cbabu  :=0;
502     rec_so_lines        JA_SO_LINES_INFO%rowtype;
503     ln_inv_orgn_id      number ;
504     ln_default_locn_id  number;
505 
506     --lv_appl_src  JAI_CMN_ERRORS_T.APPLICATION_SOURCE%type;
507     --lv_err_msg   JAI_CMN_ERRORS_T.ERROR_MESSAGE%type;
508     --lv_addl_msg  JAI_CMN_ERRORS_T.ADDITIONAL_ERROR_MESG%type;
509 
510     ln_legal_entity_id NUMBER ;
511 
512     ln_tcs_exists             number;
513     ln_tcs_regime_id          JAI_RGM_DEFINITIONS.regime_id%type;
514     ln_threshold_slab_id      jai_ap_tds_thhold_slabs.threshold_slab_id%type;
515     ln_last_line_no           number;
516     ln_base_line_no           number;
517     lv_process_flag             VARCHAR2(2);
518     lv_process_message          VARCHAR2(1996);
519     ln_reg_id                   number;
520 
521     cursor c_chk_rgm_tax_exists  ( cp_regime_code          JAI_RGM_DEFINITIONS.regime_code%type
522                                  , cp_cust_trx_line_id     ra_customer_trx_lines_all.customer_trx_line_id%type
523                                  )
524     is
525     select  count(1)
526       from    jai_regime_tax_types_v jrttv
527             , JAI_AR_TRX_TAX_LINES  jrctt
528             , JAI_CMN_TAXES_ALL jtc
529      where   jtc.tax_id     = jrctt.tax_id
530        and     jtc.tax_type  = jrttv.tax_type
531        and     regime_code    = cp_regime_code
532        and     jrctt.link_to_cust_trx_line_id = cp_cust_trx_line_id;
533 
534     cursor c_get_regime_id (cp_regime_code    JAI_RGM_DEFINITIONS.regime_code%type)
535     is
536     select regime_id
537       from   JAI_RGM_DEFINITIONS
538      where  regime_code = cp_regime_code;
539 
540     Cursor get_ar_tax_amount
541     is
542     select sum(tax_amount)
543     from JAI_AR_TRX_TAX_LINES
544     Where link_to_cust_trx_line_id = v_customer_trx_line_id;
545 
546     ln_ar_tax_amount JAI_AR_TRX_TAX_LINES.TAX_AMOUNT%type;
547     l_api_name CONSTANT VARCHAR2(30) := 'process_bill_only_invoice()';
548   begin
549     --FND_FILE.PUT_LINE(FND_FILE.LOG,' Inside Procedure process_bill_only_invoice');
550     if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
551         FND_LOG.STRING(G_LEVEL_PROCEDURE,
552                        G_MODULE_NAME || l_api_name || '.BEGIN',
553                        G_PKG_NAME || ': ' || l_api_name || '()+');
554     end if;
555 
556     ln_hdr_exists        := 0;
557     ln_line_amount       :=0;
558     ln_tax_amount        :=0;
559     /*
560      writing code here to process a bill only invoice.
561      Validations to handle
562      ---------------------
563       a) in case of discounts only one line should have taxes
564       b) in case of bond register excise and excise cess taxes should not flow into AR
565       c) only one record should be inserted in the JAI_AR_TRXS table.
566     */
567 
568     ln_hdr_exists := 0;
569     open c_check_hdr_exists;
570     fetch c_check_hdr_exists into ln_hdr_exists;
571     close c_check_hdr_exists;
572 
573     --FND_FILE.PUT_LINE(FND_FILE.LOG,' Header exists '|| ln_hdr_exists);
574     if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
575         FND_LOG.STRING(G_LEVEL_PROCEDURE,
576                        G_MODULE_NAME || l_api_name || '.IN',
577                        G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
578                        ||' Header exists ='||ln_hdr_exists
579                        );
580     end if;
581     if nvl(ln_hdr_exists,0) = 0 then
582       /*
583       no record exists in the JAI_AR_TRXS table for the customer trx id ,
584       hence insert a record into the table.
585       */
586 
587       open  c_trx_cur;
588       fetch c_trx_cur into lr_trx_rec;
589       close c_trx_cur;
590 
591       if pr_new.warehouse_id is null then
592          open  c_oe_system_params;
593          fetch c_oe_system_params into ln_inv_orgn_id;
594          close c_oe_system_params;
595       else
596          ln_inv_orgn_id := pr_new.warehouse_id ;
597       end if;
598 
599       --FND_FILE.PUT_LINE(FND_FILE.LOG,' Inv orgn id: '|| ln_inv_orgn_id);
600       if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
601           FND_LOG.STRING(G_LEVEL_PROCEDURE,
602                          G_MODULE_NAME || l_api_name || '.IN',
603                          G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
604                          ||' Inv orgn id ='||ln_inv_orgn_id
605                          );
606       end if;
607       open  c_default_location(ln_inv_orgn_id);
608       fetch c_default_location into ln_default_locn_id;
609       close c_default_location;
610 
611       --FND_FILE.PUT_LINE(FND_FILE.LOG,' Default location id '|| ln_default_locn_id);
612       if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
613           FND_LOG.STRING(G_LEVEL_PROCEDURE,
614                          G_MODULE_NAME || l_api_name || '.IN',
615                          G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
616                          ||' Default location id ='||ln_default_locn_id
617                          );
618       end if;
619       /*if ln_default_locn_id is null then
620 
621        \*
622         if the default location setup is not done , log this as a message in the JAI_CMN_ERRORS_T table
623         do not stop the processing.
624         We could still update the organization and location id in the JAI_AR_TRXS table later on (as a datafix)
625        *\
626          lv_appl_src := 'JA_IN_OE_AR_LINES_INSERT_TRG' ;
627          lv_err_msg  := 'Default Location is not setup for Inventory Organization ' ||  pr_new.warehouse_id ;
628          lv_addl_msg := 'Please setup the Default Location in Organization Additional Information Screen for Trx id : ' || pr_new.customer_trx_id ;
629 
630          insert into JAI_CMN_ERRORS_T
631          (
632           APPLICATION_SOURCE          ,
633           ERROR_MESSAGE           ,
634           ADDITIONAL_ERROR_MESG   ,
635           CREATION_DATE           ,
636           CREATED_BY              ,
637           LAST_UPDATED_BY,
638           LAST_UPDATE_DATE
639          )
640          values
641          (
642            lv_appl_src,  \*'JA_IN_OE_AR_LINES_INSERT_TRG', Ramananda for removal of SQL LITERALs *\
643            lv_err_msg,   \* 'Default Location is not setup for Inventory Organization ' ||  pr_new.warehouse_id , *\
644            lv_addl_msg,  \* 'Please setup the Default Location in Organization Additional Information Screen for Trx id : ' || pr_new.customer_trx_id  , *\
645            sysdate,
646            fnd_global.user_id ,
647            fnd_global.user_id,
648            sysdate
649          );
650       end if;*/
651 
652       ln_legal_entity_id := get_legal_entity_id(v_header_id) ;
653 
654       insert into JAI_AR_TRXS    -- bill only invoice
655       (
656       CUSTOMER_TRX_ID                           ,
657       ORGANIZATION_ID                           ,
658       LOCATION_ID                               ,
659       UPDATE_RG_FLAG                            ,
660       ONCE_COMPLETED_FLAG                       ,
661       complete_flag                             ,--Added by Zhiwei for ZX integration
662       --trx_date                                  ,--Added by Zhiwei for ZX integration
663       TOTAL_AMOUNT                              ,
664       LINE_AMOUNT                               ,
665       TAX_AMOUNT                                ,
666       TRX_NUMBER                                ,
667       BATCH_SOURCE_ID                           ,
668       CREATION_DATE                             ,
669       CREATED_BY                                ,
670       LAST_UPDATE_DATE                          ,
671       LAST_UPDATED_BY                           ,
672       LAST_UPDATE_LOGIN                         ,
673       SET_OF_BOOKS_ID                           ,
674       PRIMARY_SALESREP_ID                       ,
675       INVOICE_CURRENCY_CODE                     ,
676       EXCHANGE_RATE_TYPE                        ,
677       EXCHANGE_DATE                             ,
678       EXCHANGE_RATE                             ,
679       CREATED_FROM                              ,
680       UPDATE_RG23D_FLAG                         ,
681       TAX_INVOICE_NO                            ,
682       LEGAL_ENTITY_ID
683       )
684       values
685       (
686       pr_new.customer_trx_id                    ,
687       ln_inv_orgn_id                          ,
688       ln_default_locn_id                      ,
689       'N'                                     ,
690       'N'                                     ,
691       lr_trx_rec.complete_flag                ,--Added by Zhiwei for ZX integration
692       --lr_trx_rec.trx_date                     ,--Added by Zhiwei for ZX integration
693       0                                       ,
694       0                                       ,
695       0                                       ,
696       lr_trx_rec.trx_number                   ,
697       lr_trx_rec.batch_source_id              ,
698       sysdate                                 ,
699       fnd_global.user_id                      ,
700       sysdate                                 ,
701       fnd_global.user_id                      ,
702       fnd_global.login_id                     ,
703       lr_trx_rec.set_of_books_id              ,
704       lr_trx_rec.primary_salesrep_id          ,
705       lr_trx_rec.invoice_currency_code        ,
706       lr_trx_rec.exchange_rate_type           ,
707       lr_trx_rec.exchange_date                ,
708       lr_trx_rec.exchange_rate                ,
709       lr_trx_rec.created_from                 ,
710       'N'                                     ,
711       NULL                                    ,
712       ln_legal_entity_id
713       );
714 
715       --FND_FILE.PUT_LINE(FND_FILE.LOG,' After insert into JAI_AR_TRXS - Bill only invoice');
716       if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
717           FND_LOG.STRING(G_LEVEL_PROCEDURE,
718                          G_MODULE_NAME || l_api_name || '.IN',
719                          G_PKG_NAME || ': ' || l_api_name || '().'||'  After insert into JAI_AR_TRXS - Bill only invoice'
720                          );
721       end if;
722     end if;
723 
724     /*
725      insert into the JAI_AR_TRX_TAX_LINES table and then insert into the JAI_AR_TRX_LINES table.
726      pr_new.interface_line_attribute6 = order_line_id
727      pr_new.interface_line_context    = 'ORDER ENTRY'
728     */
729 
730      open  c_ont_source_code;
731      fetch c_ont_source_code into v_ont_source_code;
732      close c_ont_source_code;
733      --FND_FILE.PUT_LINE(FND_FILE.LOG,' Ont source code: '|| v_ont_source_code);
734      if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
735           FND_LOG.STRING(G_LEVEL_PROCEDURE,
736                          G_MODULE_NAME || l_api_name || '.IN',
737                          G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
738                          ||' Ont source code ='||v_ont_source_code
739                          );
740      end if;
741      v_ont_source_code := ltrim(rtrim(v_ont_source_code));
742 
743      --FND_FILE.PUT_LINE(FND_FILE.LOG,' Ont source code- after trunc: '|| v_ont_source_code);
744      if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
745           FND_LOG.STRING(G_LEVEL_PROCEDURE,
746                          G_MODULE_NAME || l_api_name || '.IN',
747                          G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
748                          ||' Ont source code- after trunc ='||v_ont_source_code
749                          );
750      end if;
751      /*
752       return if it is discount line
753      */
754      if nvl(pr_new.interface_line_attribute11,'0') <> '0' then
755        --FND_FILE.PUT_LINE(FND_FILE.LOG,' Int. line att1: '||pr_new.interface_line_attribute11);
756        if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
757           FND_LOG.STRING(G_LEVEL_PROCEDURE,
758                          G_MODULE_NAME || l_api_name || '.IN',
759                          G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
760                          ||' Int. line att1 ='||pr_new.interface_line_attribute11
761                          );
762        end if;
763        return;
764      end if;
765 
766      for tax_rec in JA_SO_TAX_LINES_INFO
767      Loop
768        --FND_FILE.PUT_LINE(FND_FILE.LOG,' cursor ja_so_tax_lines_info');
769        --FND_FILE.PUT_LINE(FND_FILE.LOG,' Tax type '|| tax_rec.tax_type);
770        if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
771           FND_LOG.STRING(G_LEVEL_PROCEDURE,
772                          G_MODULE_NAME || l_api_name || '.IN',
773                          G_PKG_NAME || ': ' || l_api_name || '().'||' cursor ja_so_tax_lines_info :'
774                          ||' Tax type ='|| tax_rec.tax_type
775                          );
776        end if;
777        if v_register_code = 'BOND_REG' then
778 
779          --FND_FILE.PUT_LINE(FND_FILE.LOG,' Register code '|| v_register_code);
780          if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
781             FND_LOG.STRING(G_LEVEL_PROCEDURE,
782                            G_MODULE_NAME || l_api_name || '.IN',
783                            G_PKG_NAME || ': ' || l_api_name || '().'||' PARAMS INFO :'
784                            ||' Register code ='|| v_register_code
785                            );
786          end if;
787          if upper(tax_rec.tax_type) not in ('EXCISE','OTHER EXCISE','CVD_EDUCATION_CESS','EXCISE_EDUCATION_CESS', jai_constants.tax_type_sh_cvd_edu_cess, jai_constants.tax_type_sh_exc_edu_cess) then
788 
789             /*jai_constants.tax_type_sh_exc_edu_cess, jai_constants.tax_type_sh_cvd_edu_cess added by CSahoo BUG#5989740*/
790             /*
791              This is not a discount line , so taxes can flow. In case it was discount , control would have never come here
792             because of the return statement above.
793             */
794 
795             INSERT INTO JAI_AR_TRX_TAX_LINES
796                       (
797                        tax_line_no                 ,
798                        customer_trx_line_id        ,
799                        link_to_cust_trx_line_id    ,
800                        precedence_1                ,
801                        precedence_2                ,
802                        precedence_3                ,
803                        precedence_4                ,
804                        precedence_5                ,
805                        precedence_6                ,
806                        precedence_7                ,
807                        precedence_8                ,
808                        precedence_9                ,
809                        precedence_10                ,
810                        tax_id                      ,
811                        tax_rate                    ,
812                        qty_rate                    ,
813                        uom                         ,
814                        tax_amount                  ,
815                        func_tax_amount             ,
816                        base_tax_amount             ,
817                        creation_date               ,
818                        created_by                  ,
819                        last_update_date            ,
820                        last_updated_by             ,
821                        last_update_login
822                       )
823                   VALUES
824                       (
825                       tax_rec.tax_line_no               ,
826                       ra_customer_trx_lines_s.nextval   ,
827                       v_customer_trx_line_id            ,
828                       tax_rec.precedence_1              ,
829                       tax_rec.precedence_2              ,
830                       tax_rec.precedence_3              ,
831                       tax_rec.precedence_4              ,
832                       tax_rec.precedence_5              ,
833                       tax_rec.precedence_6              ,
834                       tax_rec.precedence_7              ,
835                       tax_rec.precedence_8              ,
836                       tax_rec.precedence_9              ,
837                       tax_rec.precedence_10              ,
838                       tax_rec.tax_id                    ,
839                       tax_rec.tax_rate                  ,
840                       tax_rec.qty_rate                  ,
841                       tax_rec.uom                       ,
842                       round(tax_rec.tax_amount,2)       ,
843                       round(tax_rec.func_tax_amount,2)  ,
844                       round(tax_rec.base_tax_amount,2)  ,
845                       sysdate                           ,
846                       fnd_global.user_id                ,
847                       sysdate                           ,
848                       fnd_global.user_id                ,
849                       fnd_global.login_id
850                      );
851          end if; /* end if for tax type not in excise .... */
852        else       /* register association is not bond register , so all taxes can flow */
853 
854             INSERT INTO JAI_AR_TRX_TAX_LINES
855                            (
856                             tax_line_no                 ,
857                             customer_trx_line_id        ,
858                             link_to_cust_trx_line_id    ,
859                             precedence_1                ,
860                             precedence_2                ,
861                             precedence_3                ,
862                             precedence_4                ,
863                             precedence_5                ,
864                             precedence_6                ,
865                             precedence_7                ,
866                             precedence_8                ,
867                             precedence_9                ,
868                             precedence_10                ,
869                             tax_id                      ,
870                             tax_rate                    ,
871                             qty_rate                    ,
872                             uom                         ,
873                             tax_amount                  ,
874                             func_tax_amount             ,
875                             base_tax_amount             ,
876                             creation_date               ,
877                             created_by                  ,
878                             last_update_date            ,
879                             last_updated_by             ,
880                             last_update_login
881                            )
882                             VALUES
883                            (
884                             tax_rec.tax_line_no               ,
885                             ra_customer_trx_lines_s.nextval   ,
886                             v_customer_trx_line_id            ,
887                             tax_rec.precedence_1              ,
888                             tax_rec.precedence_2              ,
889                             tax_rec.precedence_3              ,
890                             tax_rec.precedence_4              ,
891                             tax_rec.precedence_5              ,
892                             tax_rec.precedence_6              ,
893                             tax_rec.precedence_7              ,
894                             tax_rec.precedence_8              ,
895                             tax_rec.precedence_9              ,
896                             tax_rec.precedence_10              ,
897                             tax_rec.tax_id                    ,
898                             tax_rec.tax_rate                  ,
899                             tax_rec.qty_rate                  ,
900                             tax_rec.uom                       ,
901                             round(tax_rec.tax_amount,2)       ,
902                             round(tax_rec.func_tax_amount,2)  ,
903                             round(tax_rec.base_tax_amount,2)  ,
904                             sysdate                           ,
905                             fnd_global.user_id                ,
906                             sysdate                           ,
907                             fnd_global.user_id                ,
908                             fnd_global.login_id
909                          );
910 
911        end if;
912      end loop;
913 
914 
915      open   JA_SO_LINES_INFO;
916      fetch  JA_SO_LINES_INFO into rec_so_lines;
917      close  JA_SO_LINES_INFO;
918 
919 
920       ln_tcs_exists  := null;
921       open c_chk_rgm_tax_exists ( cp_regime_code        => jai_constants.tcs_regime
922                                 , cp_cust_trx_line_id   => v_customer_trx_line_id
923                                   );
924       fetch c_chk_rgm_tax_exists into ln_tcs_exists;
925       close c_chk_rgm_tax_exists ;
926 
927     if ln_tcs_exists > 0 then
928            /* TCS type of tax is present */
929            open  c_get_regime_id (cp_regime_code => jai_constants.tcs_regime);
930            fetch c_get_regime_id into ln_tcs_regime_id;
931            close c_get_regime_id;
932            /* Find out what is the current slab */
933            jai_rgm_thhold_proc_pkg.get_threshold_slab_id
934                                     (
935                                         p_regime_id         =>    ln_tcs_regime_id
936                                       , p_organization_id   =>    ln_inv_orgn_id
937                                       , p_party_type        =>    jai_constants.party_type_customer
938                                       , p_party_id          =>    lr_trx_rec.customer_id
939                                       , p_org_id            =>    v_org_id
940                                       , p_source_trx_date   =>    lr_trx_rec.trx_date
941                                       , p_threshold_slab_id =>    ln_threshold_slab_id
942                                       , p_process_flag      =>    lv_process_flag
943                                       , p_process_message   =>    lv_process_message
944                                     );
945 
946            if lv_process_flag <> jai_constants.successful then
947               app_exception.raise_exception
948                           (exception_type   =>    'APP'
949                           ,exception_code   =>    -20275
950                           ,exception_text   =>    lv_process_message
951                           );
952            end if;
953            if ln_threshold_slab_id is not null then
954           /* Threshold level is up.  Surcharge needs to be defaulted , so find out the tax category based on the threshold slab */
955             jai_rgm_thhold_proc_pkg.get_threshold_tax_cat_id
956                                       (
957                                          p_threshold_slab_id    =>    ln_threshold_slab_id
958                                       ,  p_org_id               =>    v_org_id
959                                       ,  p_threshold_tax_cat_id =>    ln_threshold_tax_cat_id
960                                       ,  p_process_flag         =>    lv_process_flag
961                                       ,  p_process_message      =>    lv_process_message
962                                       );
963             if lv_process_flag <> jai_constants.successful then
964               app_exception.raise_exception
965                             (exception_type   =>    'APP'
966                             ,exception_code   =>    -20275
967                             ,exception_text   =>    lv_process_message
968                             );
969             end if;
970             /* Get line number after which threshold taxes needs to be defaulted */
971             select max(tax_line_no)
972             into   ln_last_line_no
973             from   JAI_AR_TRX_TAX_LINES
974             where  link_to_cust_trx_line_id = v_customer_trx_line_id;
975             /* Get line number of the base tax (tax_type=TCS) for calculating the surcharge basically to set a precedence */
976             select max(tax_line_no)
977             into  ln_base_line_no
978             from  JAI_AR_TRX_TAX_LINES jrctt
979                 , JAI_CMN_TAXES_ALL jtc
980             where jrctt.link_to_cust_trx_line_id  = v_customer_trx_line_id
981             and   jrctt.tax_id    = jtc.tax_id
982             and   jtc.tax_type    = jai_constants.tax_type_tcs;
983             /*
984             ||Call the helper method to default surcharge taxes on top of the SO taxes  using the tax category
985             || The api jai_rgm_thhold_proc_pkg.default_thhold_taxes inserts lines as per the same specified in the TCS tax category
986             || into the ja_in_so_picking_tax_lines table
987             */
988             jai_rgm_thhold_proc_pkg.default_thhold_taxes
989                                       (
990                                         p_source_trx_id         => ''
991                                       , p_source_trx_line_id    => v_customer_trx_line_id
992                                       , p_source_event          => jai_constants.bill_only_invoice
993                                       , p_action                => jai_constants.default_taxes
994                                       , p_threshold_tax_cat_id  => ln_threshold_tax_cat_id
995                                       , p_tax_base_line_number  => ln_base_line_no
996                                       , p_last_line_number      => ln_last_line_no
997                                       , p_currency_code         => lr_trx_rec.invoice_currency_code
998                                       , p_currency_conv_rate    => lr_trx_rec.exchange_rate
999                                       , p_quantity              => nvl(rec_so_lines.quantity,0)
1000                                       , p_base_tax_amt          => nvl(rec_so_lines.line_amount,0)
1001                                       , p_assessable_value      => rec_so_lines.assessable_value * rec_so_lines.quantity  --ADDED rec_so_lines.quantity FOR BUG#6498072
1002                                       , p_inventory_item_id     => rec_so_lines.inventory_item_id
1003                                       , p_uom_code              => rec_so_lines.unit_code
1004                                       , p_vat_assessable_value  => rec_so_lines.vat_assessable_value
1005                                       , p_process_flag          => lv_process_flag
1006                                       , p_process_message       => lv_process_message
1007                                       );
1008 
1009             if lv_process_flag <> jai_constants.successful then
1010               app_exception.raise_exception
1011                             (exception_type   =>    'APP'
1012                             ,exception_code   =>    -20275
1013                             ,exception_text   =>    lv_process_message
1014                             );
1015             end if;
1016           end if; /* ln_threshold_slab_id is not null then */
1017         end if;  /** ln_tcs_exists is not null then  */
1018 
1019 
1020 
1021         open get_ar_tax_amount;
1022         FETCH get_ar_tax_amount INTO ln_ar_tax_amount;
1023         CLOSE get_ar_tax_amount;
1024 
1025 
1026 
1027 
1028         INSERT INTO JAI_AR_TRX_LINES (
1029                                                  customer_trx_line_id                         ,
1030                                                  line_number                                  ,
1031                                                  customer_trx_id                              ,
1032                                                  description                                  ,
1033                                                  payment_register                             ,
1034                                                  excise_invoice_no                            ,
1035                                                  preprinted_excise_inv_no                     ,
1036                                                  excise_invoice_date                          ,
1037                                                  inventory_item_id                            ,
1038                                                  unit_code                                    ,
1039                                                  quantity                                     ,
1040                                                  tax_category_id                              ,
1041                                                  auto_invoice_flag                            ,
1042                                                  unit_selling_price                           ,
1043                                                  line_amount                                  ,
1044                                                  tax_amount                                   ,
1045                                                  total_amount                                 ,
1046                                                  assessable_value                             ,
1047                                                  creation_date                                ,
1048                                                  created_by                                   ,
1049                                                  last_update_date                             ,
1050                                                  last_updated_by                              ,
1051                                                  last_update_login                            ,
1052                                                  excise_exempt_type                           ,
1053                                                  excise_exempt_refno                          ,
1054                                                  excise_exempt_date                           ,
1055                                                  ar3_form_no                                  ,
1056                                                  ar3_form_date                                ,
1057                                                  vat_exemption_flag                           ,
1058                                                  vat_exemption_type                           ,
1059                                                  vat_exemption_date                           ,
1060                                                  vat_exemption_refno                                    ,
1061                                                  vat_assessable_value                         ,
1062                                                  service_type_code
1063                                                 )
1064                                         VALUES  (
1065                                                  pr_new.customer_trx_line_id                    ,
1066                                                  pr_new.line_number                             ,
1067                                                  pr_new.customer_trx_id                         ,
1068                                                  pr_new.description                             ,
1069                                                  NULL                                         ,
1070                                                  NULL                                         ,
1071                                                  NULL                                         ,
1072                                                  NULL                                         ,
1073                                                  pr_new.inventory_item_id                       ,
1074                                                  pr_new.uom_code                                ,
1075                                                  pr_new.quantity_invoiced                       ,
1076                                                  rec_so_lines.tax_category_id                 ,
1077                                                  'Y'                                          ,
1078                                                  pr_new.unit_selling_price                      ,
1079                                                  round(nvl(rec_so_lines.line_amount,0),2)     ,
1080                                                  round(nvl(ln_ar_tax_amount,0),2)      ,
1081                                                  round(nvl(rec_so_lines.line_amount,0) +
1082                                                  nvl(ln_ar_tax_amount,0),2)            ,
1083                                                  rec_so_lines.assessable_value                ,
1084                                                  sysdate                                      ,
1085                                                  fnd_global.user_id                           ,
1086                                                  sysdate                                      ,
1087                                                  fnd_global.user_id                           ,
1088                                                  fnd_global.login_id                          ,
1089                                                  rec_so_lines.excise_exempt_type              ,
1090                                                  rec_so_lines.excise_exempt_refno             ,
1091                                                  rec_so_lines.excise_exempt_date              ,
1092                                                  NULL                                         ,
1093                                                  NULL                                         ,
1094                                                  rec_so_lines.vat_exemption_flag              ,
1095                                                  rec_so_lines.vat_exemption_type              ,
1096                                                  rec_so_lines.vat_exemption_date              ,
1097                                                  rec_so_lines.vat_exemption_refno             ,
1098                                                  rec_so_lines.vat_assessable_value            ,
1099                                                  rec_so_lines.service_type_code
1100                                                 );
1101       --Added by Qinglei for JAI Trigger Elimination 11-May-2012 begin
1102       OPEN c_jai_ar_trx_lines(pr_new.customer_trx_id,pr_new.customer_trx_line_id);
1103       FETCH c_jai_ar_trx_lines into t_jai_line_rec_new;
1104       CLOSE c_jai_ar_trx_lines;
1105 
1106       lv_action := jai_constants.INSERTING;
1107 
1108       JAI_AR_TAX_LINES_PKG.POPULATE_TAX_LINES_WRAPPER (
1109                   pr_old            =>  NULL         ,
1110                   pr_new            =>  t_jai_line_rec_new         ,
1111                   pv_action         =>  lv_action         ,
1112                   pv_return_code    =>  lv_return_code    ,
1113                   pv_return_message =>  lv_return_message
1114                 );
1115       IF lv_return_code <> jai_constants.successful   then
1116           RAISE le_error;
1117       END IF;
1118       --Added by Qinglei for JAI Trigger Elimination 11-May-2012 end
1119       open  c_get_amounts;
1120       fetch c_get_amounts into ln_tax_amount, ln_line_amount ;
1121       close c_get_amounts;
1122 
1123       /*FND_FILE.PUT_LINE(FND_FILE.LOG,' Cursor c_get_amounts');
1124       FND_FILE.PUT_LINE(FND_FILE.LOG,' Tax amount '|| ln_tax_amount);
1125       FND_FILE.PUT_LINE(FND_FILE.LOG,' Line amount '|| ln_line_amount);*/
1126       if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1127             FND_LOG.STRING(G_LEVEL_PROCEDURE,
1128                            G_MODULE_NAME || l_api_name || '.IN',
1129                            G_PKG_NAME || ': ' || l_api_name || '().'||' Cursor c_get_amounts :'
1130                            ||' Tax amount ='|| ln_tax_amount
1131                            ||';Line amount ='|| ln_line_amount
1132                            );
1133       end if;
1134       OPEN c_get_vat_inv_no;
1135       FETCH c_get_vat_inv_no INTO lv_vat_invoice_no;
1136       CLOSE c_get_vat_inv_no;
1137       --FND_FILE.PUT_LINE(FND_FILE.LOG,' VAT Invoice Number '|| lv_vat_invoice_no);
1138       if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1139             FND_LOG.STRING(G_LEVEL_PROCEDURE,
1140                            G_MODULE_NAME || l_api_name || '.IN',
1141                            G_PKG_NAME || ': ' || l_api_name || '().'||' PARAMS INFO :'
1142                            ||' VAT Invoice Number ='|| lv_vat_invoice_no
1143                            );
1144       end if;
1145       --Added by Zhiwei for JAI Trigger elimination begin
1146       ---------------------------------------------------------
1147       open c_get_rec(pr_new.customer_trx_id);
1148       fetch c_get_rec into t_rec_old;
1149       close c_get_rec;
1150       ---------------------------------------------------------
1151       --Added by Zhiwei for JAI Trigger elimination end
1152 
1153 
1154       update  JAI_AR_TRXS
1155       set     tax_amount   = ln_tax_amount ,
1156               line_amount  = ln_line_amount,
1157               total_amount = ln_line_amount + ln_tax_amount
1158              ,vat_invoice_no = lv_vat_invoice_no
1159       where   customer_trx_id = pr_new.customer_trx_id;
1160 
1161       --Added by Zhiwei for JAI Trigger elimination begin
1162       ---------------------------------------------------------
1163       open c_get_rec(pr_new.customer_trx_id);
1164       fetch c_get_rec into t_rec_new;
1165       close c_get_rec;
1166 
1167       if(t_rec_new.once_completed_flag = 'Y')then
1168 
1169            lv_action := jai_constants.updating ;
1170 
1171           JAI_AR_TRXS_PKG.UPDATE_EXCISE_REGISTERS(
1172                           pr_old            =>  t_rec_old         ,
1173                           pr_new            =>  t_rec_new         ,
1174                           pv_action         =>  lv_action         ,
1175                           pv_return_code    =>  lv_return_code    ,
1176                           pv_return_message =>  lv_return_message
1177                         );
1178 
1179             IF lv_return_code <> jai_constants.successful   then
1180                RAISE le_error;
1181             END IF;
1182 
1183       end if;
1184       ---------------------------------------------------------
1185       --Added by Zhiwei for JAI Trigger elimination end
1186 
1187 
1188 
1189       jai_ar_tcs_rep_pkg.ar_accounting
1190                             (    p_ractl             =>  pr_new
1191                               ,  p_process_flag      =>  lv_process_flag
1192                               ,  p_process_message   =>  lv_process_message
1193                             );
1194       if lv_process_flag <> jai_constants.successful then
1195         app_exception.raise_exception
1196                       (exception_type   =>    'APP'
1197                       ,exception_code   =>    -20275
1198                       ,exception_text   =>    lv_process_message
1199                       );
1200       end if;
1201 
1202       if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1203         FND_LOG.STRING(G_LEVEL_PROCEDURE,
1204                        G_MODULE_NAME || l_api_name || '.END',
1205                        G_PKG_NAME || ': ' || l_api_name || '()-');
1206       end if;
1207 
1208   end process_bill_only_invoice;
1209 
1210   /*
1211   */
1212   Procedure Process_Taxes_for_ATO_Order
1213   is
1214 
1215     cursor c_item_type is
1216     select item_type_code
1217     from   oe_order_lines_all
1218     where  line_id = v_interface_line_attribute6;
1219 
1220     Cursor c_om_taxes(v_so_config_line_id Number) is
1221     select *
1222     from   JAI_OM_OE_SO_TAXES
1223     where  line_id = v_so_config_line_id;
1224 
1225     Cursor  c_get_config_line_id(v_ato_line_id Varchar2, cp_item_code  oe_order_lines_all.item_type_code%type) is
1226     Select  line_id
1227     from    oe_order_lines_all
1228     where   ato_line_id = v_ato_line_id
1229     and     item_type_code = cp_item_code;
1230 
1231     CURSOR  c_get_om_lines(v_ato_line_id Varchar2) is
1232     SELECT  *
1233     FROM    JAI_OM_OE_SO_LINES
1234     WHERE   line_id = v_ato_line_id;
1235 
1236     cursor  c_so_picking_data (v_ato_line_id Varchar2) is
1237     SELECT  excise_invoice_no , register , preprinted_excise_inv_no , ar3_form_no ,ar3_form_date
1238     FROM    JAI_OM_WSH_LINES_ALL
1239     WHERE   order_line_id = v_ato_line_id;
1240 
1241     CURSOR so_ato_picking_hdr_info(v_ato_line_id Varchar2) IS
1242     SELECT a.organization_id, a.location_id
1243     FROM   JAI_OM_WSH_LINES_ALL a,wsh_new_deliveries b
1244     WHERE  a.delivery_id = b.delivery_id and
1245            B.NAME = v_interface_line_attribute3 AND
1246            A.order_line_id   = TO_NUMBER(v_ato_line_id);
1247 
1248     CURSOR c_get_tax_info(p_tax_id Number) is
1249     SELECT tax_type
1250     FROM   JAI_CMN_TAXES_ALL
1251     WHERE  tax_id = p_tax_id;
1252 
1253     v_tax_type   JAI_CMN_TAXES_ALL.tax_type%type;
1254     v_tax_amount Number; --File.Sql.35 Cbabu  :=0;
1255 
1256     ln_legal_entity_id NUMBER ;
1257     ln_hdr_exist_chk number;
1258     v_item_type        oe_order_lines_all.item_type_code%type;
1259     l_api_name CONSTANT VARCHAR2(30) := 'Process_Taxes_for_ATO_Order()';
1260   BEGIN
1261 
1262     pv_return_code := jai_constants.successful ;
1263 
1264     if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1265         FND_LOG.STRING(G_LEVEL_PROCEDURE,
1266                        G_MODULE_NAME || l_api_name || '.BEGIN',
1267                        G_PKG_NAME || ': ' || l_api_name || '()+');
1268     end if;
1269 
1270     --FND_FILE.PUT_LINE(FND_FILE.LOG, ' Inside Procedure Process_Taxes_for_ATO_Order');
1271     --FND_FILE.PUT_LINE(FND_FILE.LOG,'ATO: v_item_type: ' || v_item_type);
1272     if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1273         FND_LOG.STRING(G_LEVEL_PROCEDURE,
1274                        G_MODULE_NAME || l_api_name || '.IN',
1275                        G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
1276                        ||' ATO: v_item_type ='||v_item_type
1277                        );
1278     end if;
1279     v_tax_amount  :=0;
1280 
1281    open  c_item_type;
1282    fetch c_item_type into v_item_type;
1283    close c_item_type;
1284 
1285    --FND_FILE.PUT_LINE(FND_FILE.LOG, ' Item type'|| v_item_type);
1286    if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1287         FND_LOG.STRING(G_LEVEL_PROCEDURE,
1288                        G_MODULE_NAME || l_api_name || '.IN',
1289                        G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
1290                        ||' Item type ='||v_item_type
1291                        );
1292    end if;
1293    if NVL(v_item_type,'$$$') = 'MODEL' then
1294     IF (NVL(pr_new.Interface_line_attribute11,'0') ='0'
1295         and nvl(v_ont_source_code,'ORDER ENTRY') = ltrim(rtrim(pr_new.interface_line_context))
1296        )
1297     or
1298        (nvl(v_ont_source_code,'ORDER ENTRY') <> nvl(ltrim(rtrim(pr_new.interface_line_context)),'$$$')
1299        )
1300     then
1301       -- using the following cursor , we get the line_id
1302       -- of the 'config' item in oe_order_lines_all
1303       Open    c_get_config_line_id(v_interface_line_attribute6, 'CONFIG');
1304       Fetch   c_get_config_line_id into v_config_line_id;
1305       Close   c_get_config_line_id;
1306 
1307       --FND_FILE.PUT_LINE(FND_FILE.LOG, ' Config line id ' || v_config_line_id);
1308       if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1309           FND_LOG.STRING(G_LEVEL_PROCEDURE,
1310                          G_MODULE_NAME || l_api_name || '.IN',
1311                          G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
1312                          ||' Config line id ='||v_config_line_id
1313                          );
1314       end if;
1315       -- the variable v_config_line_id holds the line_id of the
1316       -- config item corresponding to the 'Model' item being imported.
1317 
1318       For  so_taxes_rec in c_om_taxes(v_config_line_id)
1319       Loop
1320        Open   c_get_tax_info(so_taxes_rec.tax_id);
1321        Fetch  c_get_tax_info into v_tax_type;
1322        close  c_get_tax_info;
1323 
1324        --FND_FILE.PUT_LINE(FND_FILE.LOG, ' Tax Type: '|| v_tax_type);
1325        if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1326           FND_LOG.STRING(G_LEVEL_PROCEDURE,
1327                          G_MODULE_NAME || l_api_name || '.IN',
1328                          G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
1329                          ||' Tax Type ='||v_tax_type
1330                          );
1331        end if;
1332 
1333        IF V_REGISTER_CODE ='BOND_REG' THEN
1334 
1335           --FND_FILE.PUT_LINE(FND_FILE.LOG, ' Inside BOND Register');
1336           if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1337             FND_LOG.STRING(G_LEVEL_PROCEDURE,
1338                            G_MODULE_NAME || l_api_name || '.IN',
1339                            G_PKG_NAME || ': ' || l_api_name || '().'||' Inside BOND Register '
1340                            );
1341           end if;
1342           IF v_tax_type NOT IN ('Excise','Other Excise','CVD_EDUCATION_CESS','EXCISE_EDUCATION_CESS',jai_constants.tax_type_sh_cvd_edu_cess, jai_constants.tax_type_sh_exc_edu_cess) THEN
1343 
1344              INSERT INTO JAI_AR_TRX_TAX_LINES
1345              (
1346                  tax_line_no                    ,
1347                  customer_trx_line_id           ,
1348                  link_to_cust_trx_line_id       ,
1349                  precedence_1                   ,
1350                  precedence_2                   ,
1351                  precedence_3                   ,
1352                  precedence_4                   ,
1353                  precedence_5                   ,
1354                  precedence_6                   ,
1355                  precedence_7                   ,
1356                  precedence_8                   ,
1357                  precedence_9                   ,
1358                  precedence_10                   ,
1359                  tax_id                         ,
1360                  tax_rate                       ,
1361                  qty_rate                       ,
1362                  uom                            ,
1363                  tax_amount                     ,
1364                  func_tax_amount                ,
1365                  base_tax_amount                ,
1366                  creation_date                  ,
1367                  created_by                     ,
1368                  last_update_date               ,
1369                  last_updated_by                ,
1370                  last_update_login
1371              )
1372              VALUES
1373              (
1374                   so_taxes_rec.tax_line_no,
1375                   ra_customer_trx_lines_s.nextval,
1376                   v_customer_trx_line_id,
1377                   so_taxes_rec.precedence_1,
1378                   so_taxes_rec.precedence_2,
1379                   so_taxes_rec.precedence_3,
1380                   so_taxes_rec.precedence_4,
1381                   so_taxes_rec.precedence_5,
1382                   so_taxes_rec.precedence_6,
1383                   so_taxes_rec.precedence_7,
1384                   so_taxes_rec.precedence_8,
1385                   so_taxes_rec.precedence_9,
1386                   so_taxes_rec.precedence_10,
1387                   so_taxes_rec.tax_id,
1388                   so_taxes_rec.tax_rate,
1389                   so_taxes_rec.qty_rate,
1390                   so_taxes_rec.uom,
1391                   so_taxes_rec.tax_amount,
1392                   so_taxes_rec.base_tax_amount,
1393                   so_taxes_rec.func_tax_amount,
1394                   sysdate,
1395                   so_taxes_rec.created_by,
1396                   sysdate,
1397                   so_taxes_rec.last_updated_by,
1398                   so_taxes_rec.last_update_login
1399              );
1400 
1401              --FND_FILE.PUT_LINE(FND_FILE.LOG, ' After insert into JAI_AR_TRX_TAX_LINES');
1402              if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1403                FND_LOG.STRING(G_LEVEL_PROCEDURE,
1404                               G_MODULE_NAME || l_api_name || '.IN',
1405                               G_PKG_NAME || ': ' || l_api_name || '().'||' After insert into JAI_AR_TRX_TAX_LINES '
1406                               );
1407              end if;
1408              v_tax_amount := NVL(v_tax_amount,0) + NVL(so_taxes_rec.tax_amount,0);
1409 
1410              --FND_FILE.PUT_LINE(FND_FILE.LOG,' Tax amount: '|| v_tax_amount);
1411              if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1412                   FND_LOG.STRING(G_LEVEL_PROCEDURE,
1413                                  G_MODULE_NAME || l_api_name || '.IN',
1414                                  G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
1415                                  ||' Tax amount ='||v_tax_amount
1416                                  );
1417              end if;
1418 
1419           end if;  -- for 'tax type'
1420        else
1421 
1422        -- handle for the case where the register code is not BOND_REG
1423 
1424            INSERT INTO JAI_AR_TRX_TAX_LINES
1425                   (
1426                       TAX_LINE_NO                    ,
1427                       CUSTOMER_TRX_LINE_ID           ,
1428                       LINK_TO_CUST_TRX_LINE_ID       ,
1429                       PRECEDENCE_1                   ,
1430                       PRECEDENCE_2                   ,
1431                       PRECEDENCE_3                   ,
1432                       PRECEDENCE_4                   ,
1433                       PRECEDENCE_5                   ,
1434                       PRECEDENCE_6                   ,
1435                       PRECEDENCE_7                   ,
1436                       PRECEDENCE_8                   ,
1437                       PRECEDENCE_9                   ,
1438                       PRECEDENCE_10                   ,
1439                       TAX_ID                         ,
1440                       TAX_RATE                       ,
1441                       QTY_RATE                       ,
1442                       UOM                            ,
1443                       TAX_AMOUNT                     ,
1444                       FUNC_TAX_AMOUNT                ,
1445                       BASE_TAX_AMOUNT                ,
1446                       CREATION_DATE                  ,
1447                       CREATED_BY                     ,
1448                       LAST_UPDATE_DATE               ,
1449                       LAST_UPDATED_BY                ,
1450                       LAST_UPDATE_LOGIN
1451                   )
1452                   values
1453                   (
1454                        so_taxes_rec.tax_line_no,
1455                        ra_customer_trx_lines_s.nextval,
1456                        v_customer_trx_line_id,
1457                        so_taxes_rec.precedence_1,
1458                        so_taxes_rec.precedence_2,
1459                        so_taxes_rec.precedence_3,
1460                        so_taxes_rec.precedence_4,
1461                        so_taxes_rec.precedence_5,
1462                        so_taxes_rec.precedence_6,
1463                        so_taxes_rec.precedence_7,
1464                        so_taxes_rec.precedence_8,
1465                        so_taxes_rec.precedence_9,
1466                        so_taxes_rec.precedence_10,
1467                        so_taxes_rec.tax_id,
1468                        so_taxes_rec.tax_rate,
1469                        so_taxes_rec.qty_rate,
1470                        so_taxes_rec.uom,
1471                        so_taxes_rec.tax_amount,
1472                        so_taxes_rec.base_tax_amount,
1473                        so_taxes_rec.func_tax_amount,
1474                        sysdate,
1475                        so_taxes_rec.created_by,
1476                        sysdate,
1477                        so_taxes_rec.last_updated_by,
1478                        so_taxes_rec.last_update_login
1479                   );
1480 
1481                   /*FND_FILE.PUT_LINE(FND_FILE.LOG,
1482                          ' Else - After insert into JAI_AR_TRX_TAX_LINES');*/
1483                   if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1484                       FND_LOG.STRING(G_LEVEL_PROCEDURE,
1485                                      G_MODULE_NAME || l_api_name || '.IN',
1486                                      G_PKG_NAME || ': ' || l_api_name || '().'||' Else - After insert into JAI_AR_TRX_TAX_LINES '
1487                                      );
1488                   end if;
1489                   v_tax_amount := NVL(v_tax_amount,0) + NVL(so_taxes_rec.tax_amount,0);
1490                   --FND_FILE.PUT_LINE(FND_FILE.LOG, ' Tax amount: '|| v_tax_amount);
1491                   if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1492                       FND_LOG.STRING(G_LEVEL_PROCEDURE,
1493                                      G_MODULE_NAME || l_api_name || '.IN',
1494                                      G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
1495                                      ||' Tax amount ='||v_tax_amount
1496                                      );
1497                   end if;
1498 
1499        end if; -- for 'BOND_REG'
1500       End Loop;
1501 
1502       -- creation of item line equivalent.
1503 
1504 
1505       Open  c_so_picking_data(v_config_line_id);
1506       fetch c_so_picking_data into  v_ex_inv_no , v_pmt_reg , v_pre_prnt_ex_no , v_ar3_form_no , v_ar3_form_date;
1507       close c_so_picking_data;
1508 
1509       /*FND_FILE.PUT_LINE(FND_FILE.LOG, ' Excise invoice number: '|| v_ex_inv_no);
1510       FND_FILE.PUT_LINE(FND_FILE.LOG, ' Pmt reg: ' ||v_pmt_reg);
1511       FND_FILE.PUT_LINE(FND_FILE.LOG, ' Pre print ex. no : '|| v_pre_prnt_ex_no);
1512       FND_FILE.PUT_LINE(FND_FILE.LOG, ' AR3 form no.: '|| v_ar3_form_no);
1513       FND_FILE.PUT_LINE(FND_FILE.LOG, ' AR3 form date '||v_ar3_form_date);*/
1514       if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1515                       FND_LOG.STRING(G_LEVEL_PROCEDURE,
1516                                      G_MODULE_NAME || l_api_name || '.IN',
1517                                      G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
1518                                      ||' Excise invoice number ='||v_ex_inv_no
1519                                      ||' ;Pmt reg ='||v_pmt_reg
1520                                      ||' ;Pre print ex. no ='||v_pre_prnt_ex_no
1521                                      ||' ;AR3 form no. ='||v_ar3_form_no
1522                                      ||' ;AR3 form date ='||v_ar3_form_date
1523                                      );
1524       end if;
1525 
1526       open duplicate_hdr_cur;
1527       fetch duplicate_hdr_cur into ln_hdr_exist_chk;
1528       close duplicate_hdr_cur;
1529 
1530       if nvl(ln_hdr_exist_chk,0) = 1 then
1531          open   so_ato_picking_hdr_info(v_config_line_id);
1532          fetch  so_ato_picking_hdr_info into v_organization_id , v_location_id;
1533          close  so_ato_picking_hdr_info;
1534 
1535          --FND_FILE.PUT_LINE(FND_FILE.LOG, 'ATO: 1. v_organization_id , v_location_id: ' ||  v_organization_id || ' , ' ||  v_location_id );
1536          if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1537                         FND_LOG.STRING(G_LEVEL_PROCEDURE,
1538                                        G_MODULE_NAME || l_api_name || '.IN',
1539                                        G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
1540                                        ||' ATO: 1. v_organization_id ='||v_organization_id
1541                                        ||' ;v_location_id ='||v_location_id
1542                                        );
1543          end if;
1544          IF v_organization_id IS NULL THEN
1545            open   so_picking_hdr_info_1 ;
1546            fetch  so_picking_hdr_info_1 into v_organization_id , v_location_id;
1547            close  so_picking_hdr_info_1;
1548 
1549            --FND_FILE.PUT_LINE(FND_FILE.LOG,'ATO: 2. v_organization_id , v_location_id: ' ||  v_organization_id || ' , ' ||  v_location_id );
1550            if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1551                         FND_LOG.STRING(G_LEVEL_PROCEDURE,
1552                                        G_MODULE_NAME || l_api_name || '.IN',
1553                                        G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
1554                                        ||' ATO: 2. v_organization_id ='||v_organization_id
1555                                        ||' ;v_location_id ='||v_location_id
1556                                        );
1557            end if;
1558          END IF ;
1559 
1560          --Added by Zhiwei for JAI Trigger elimination begin
1561          ---------------------------------------------------------
1562          open c_get_rec(v_header_id);
1563          fetch c_get_rec into t_rec_old;
1564          close c_get_rec;
1565          ---------------------------------------------------------
1566          --Added by Zhiwei for JAI Trigger elimination end
1567 
1568 
1569          update JAI_AR_TRXS
1570          set organization_id    = nvl(organization_id, v_organization_id)
1571             , location_id       = nvl(location_id, v_location_id)
1572             , last_update_date  = sysdate
1573          where customer_trx_id  = v_header_id;
1574 
1575          --Added by Zhiwei for JAI Trigger elimination begin
1576          ---------------------------------------------------------
1577          open c_get_rec(v_header_id);
1578          fetch c_get_rec into t_rec_new;
1579          close c_get_rec;
1580 
1581          if(t_rec_new.once_completed_flag = 'Y')then
1582 
1583                lv_action := jai_constants.updating ;
1584 
1585                JAI_AR_TRXS_PKG.UPDATE_EXCISE_REGISTERS(
1586                               pr_old            =>  t_rec_old         ,
1587                               pr_new            =>  t_rec_new         ,
1588                               pv_action         =>  lv_action         ,
1589                               pv_return_code    =>  lv_return_code    ,
1590                               pv_return_message =>  lv_return_message
1591                             );
1592 
1593                 IF lv_return_code <> jai_constants.successful   then
1594                    RAISE le_error;
1595                 END IF;
1596 
1597          end if;
1598          ---------------------------------------------------------
1599          --Added by Zhiwei for JAI Trigger elimination end
1600 
1601 
1602 
1603        ELSIF nvl(ln_hdr_exist_chk,0) <> 1 THEN
1604 
1605          OPEN   CREATED_FROM_CUR;
1606          FETCH  CREATED_FROM_CUR INTO v_created_from, v_trx_number, v_batch_source_id, v_books_id,
1607                                           v_salesrep_id, c_from_currency_code, c_conversion_type,
1608                                   c_conversion_date, c_conversion_rate ;
1609          CLOSE  CREATED_FROM_CUR;
1610 
1611          /*FND_FILE.PUT_LINE(FND_FILE.LOG,' Created from: '|| v_created_from);
1612          FND_FILE.PUT_LINE(FND_FILE.LOG,' Trx number : ' ||v_trx_number);
1613          FND_FILE.PUT_LINE(FND_FILE.LOG,' Batch source id '|| v_batch_source_id);
1614          FND_FILE.PUT_LINE(FND_FILE.LOG,' SOB id : '|| v_books_id);
1615          FND_FILE.PUT_LINE(FND_FILE.LOG,' Salesrep. id '||v_salesrep_id);
1616          FND_FILE.PUT_LINE(FND_FILE.LOG,' From currency code '||c_from_currency_code);
1617          FND_FILE.PUT_LINE(FND_FILE.LOG,' conversion type: '||c_conversion_type);
1618          FND_FILE.PUT_LINE(FND_FILE.LOG,' conversion date: '||c_conversion_date);
1619          FND_FILE.PUT_LINE(FND_FILE.LOG,' conversion rate: '||c_conversion_rate);*/
1620 
1621          if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1622                         FND_LOG.STRING(G_LEVEL_PROCEDURE,
1623                                        G_MODULE_NAME || l_api_name || '.IN',
1624                                        G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
1625                                        ||' Created from ='||v_created_from
1626                                        ||' ;Trx number ='||v_trx_number
1627                                        ||' ;Batch source id ='||v_batch_source_id
1628                                        ||' ;SOB id ='||v_books_id
1629                                        ||' ;Salesrep. id ='||v_salesrep_id
1630                                        ||' ;From currency code ='||c_from_currency_code
1631                                        ||' ;conversion type ='||c_conversion_type
1632                                        ||' ;conversion date ='||c_conversion_date
1633                                        ||' ;conversion rate ='||c_conversion_rate
1634                                        );
1635          end if;
1636 
1637 
1638          OPEN   SO_ATO_PICKING_HDR_INFO(v_config_line_id);
1639          Fetch  SO_ATO_PICKING_HDR_INFO into v_organization_id , v_location_id;
1640          close  SO_ATO_PICKING_HDR_INFO;
1641 
1642          /*FND_FILE.PUT_LINE(FND_FILE.LOG,' Org id: '|| v_organization_id);
1643          FND_FILE.PUT_LINE(FND_FILE.LOG,' Loc id: '||v_location_id);*/
1644          if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1645                         FND_LOG.STRING(G_LEVEL_PROCEDURE,
1646                                        G_MODULE_NAME || l_api_name || '.IN',
1647                                        G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
1648                                        ||' Org id ='||v_organization_id
1649                                        ||' ;Loc id ='||v_location_id
1650                                        );
1651          end if;
1652          ln_legal_entity_id := get_legal_entity_id(v_header_id) ;
1653 
1654          IF v_organization_id IS NULL THEN
1655            open   so_picking_hdr_info_1 ;
1656            fetch  so_picking_hdr_info_1 into v_organization_id , v_location_id;
1657            close  so_picking_hdr_info_1;
1658            /*FND_FILE.PUT_LINE(FND_FILE.LOG,' ATO: Org id: '|| v_organization_id);
1659            FND_FILE.PUT_LINE(FND_FILE.LOG,' ATO: Loc id: '|| v_location_id);*/
1660            if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1661                           FND_LOG.STRING(G_LEVEL_PROCEDURE,
1662                                          G_MODULE_NAME || l_api_name || '.IN',
1663                                          G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
1664                                          ||' ATO- Org id ='||v_organization_id
1665                                          ||' ;ATO- Loc id ='||v_location_id
1666                                          );
1667            end if;
1668          END IF ;
1669 
1670 
1671          Insert into JAI_AR_TRXS
1672          (
1673            CUSTOMER_TRX_ID                ,
1674            ORGANIZATION_ID                ,
1675            LOCATION_ID                    ,
1676            UPDATE_RG_FLAG                 ,
1677            ONCE_COMPLETED_FLAG            ,
1678            TOTAL_AMOUNT                   ,
1679            LINE_AMOUNT                    ,
1680            TAX_AMOUNT                     ,
1681            TRX_NUMBER                     ,
1682            BATCH_SOURCE_ID                ,
1683            CREATION_DATE                  ,
1684            CREATED_BY                     ,
1685            LAST_UPDATE_DATE               ,
1686            LAST_UPDATED_BY                ,
1687            LAST_UPDATE_LOGIN              ,
1688            SET_OF_BOOKS_ID                ,
1689            PRIMARY_SALESREP_ID            ,
1690            INVOICE_CURRENCY_CODE          ,
1691            EXCHANGE_RATE_TYPE             ,
1692            EXCHANGE_DATE                  ,
1693            EXCHANGE_RATE                  ,
1694            CREATED_FROM                   ,
1695            UPDATE_RG23D_FLAG              ,
1696            LEGAL_ENTITY_ID
1697          )
1698          Values
1699          (
1700            v_header_id,
1701            v_organization_id,
1702            v_location_id,
1703            'Y',
1704            'N',
1705            v_ato_total_amount,
1706            v_ato_line_amount,
1707            v_ato_tax_amount,
1708            v_trx_number,
1709            v_batch_source_id,
1710            sysdate,
1711            uid,
1712            sysdate,
1713            uid,
1714            uid,
1715            v_books_id,
1716            v_salesrep_id,
1717            c_from_currency_code,
1718            c_conversion_type,
1719            c_conversion_date,
1720            c_conversion_rate,
1721            v_created_from,
1722            'Y',
1723            ln_legal_entity_id
1724          );
1725          if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1726             FND_LOG.STRING(G_LEVEL_PROCEDURE,
1727                            G_MODULE_NAME || l_api_name || '.IN',
1728                            G_PKG_NAME || ': ' || l_api_name || '().'||' After insert into JAI_AR_TRXS  '
1729                            );
1730          end if;
1731          --FND_FILE.PUT_LINE(FND_FILE.LOG,'After insert into JAI_AR_TRXS ');
1732 
1733        end if;
1734 
1735 
1736 
1737        OPEN c_get_address_details(v_header_id);
1738        FETCH c_get_address_details into r_add;
1739        CLOSE c_get_address_details;
1740 
1741        v_service_type:=get_service_type( NVL(r_add.SHIP_TO_CUSTOMER_ID ,r_add.BILL_TO_CUSTOMER_ID) ,
1742                               NVL(r_add.SHIP_TO_SITE_USE_ID, r_add.BILL_TO_SITE_USE_ID),'C');
1743 
1744      for so_line_rec in c_get_om_lines(v_config_line_id)
1745       Loop
1746        Insert into JAI_AR_TRX_LINES
1747        (
1748          customer_trx_line_id           ,
1749          customer_trx_id                ,
1750          line_number                    ,
1751          inventory_item_id              ,
1752          description                    ,
1753          unit_code                      ,
1754          quantity                       ,
1755          unit_selling_price             ,
1756          tax_category_id                ,
1757          line_amount                    ,
1758          tax_amount                     ,
1759          total_amount                   ,
1760          auto_invoice_flag              ,
1761          assessable_value               ,
1762          creation_date                  ,
1763          created_by                     ,
1764          last_update_date               ,
1765          last_updated_by                ,
1766          last_update_login              ,
1767          excise_exempt_type             ,
1768          excise_exempt_refno            ,
1769          excise_exempt_date             ,
1770          excise_invoice_no              ,
1771          payment_register               ,
1772          preprinted_excise_inv_no       ,
1773          ar3_form_no                    ,
1774          ar3_form_date                  ,
1775          vat_exemption_flag             ,
1776          vat_exemption_type             ,
1777          vat_exemption_date             ,
1778          vat_exemption_refno            ,
1779          vat_assessable_value           ,
1780          service_type_code
1781        )
1782          Values
1783        (
1784          v_customer_trx_line_id,
1785          v_header_id,
1786          pr_new.line_number,
1787          pr_new.inventory_item_id,
1788          pr_new.description,
1789          so_line_rec.unit_code,
1790          so_line_rec.quantity,
1791          so_line_rec.selling_price,
1792          so_line_rec.tax_category_id,
1793          so_line_rec.line_amount,
1794          v_tax_amount,
1795          so_line_rec.line_amount + v_tax_amount,
1796          'Y',
1797          so_line_rec.assessable_value,
1798          sysdate,
1799          so_line_rec.created_by,
1800          sysdate,
1801          so_line_rec.last_updated_by,
1802          so_line_rec.last_update_login,
1803          so_line_rec.excise_exempt_type,
1804          so_line_rec.excise_exempt_refno,
1805          so_line_rec.excise_exempt_date,
1806          v_ex_inv_no ,
1807          v_pmt_reg ,
1808          v_pre_prnt_ex_no,
1809          v_ar3_form_no ,
1810          v_ar3_form_date,
1811          so_line_rec.vat_exemption_flag,
1812          so_line_rec.vat_exemption_type,
1813          so_line_rec.vat_exemption_date,
1814          so_line_rec.vat_exemption_refno,
1815          so_line_rec.vat_assessable_value,
1816          v_service_type
1817         );
1818 
1819         --Added by Qinglei for JAI Trigger Elimination 11-May-2012 begin
1820         OPEN c_jai_ar_trx_lines(v_header_id,v_customer_trx_line_id);
1821         FETCH c_jai_ar_trx_lines into t_jai_line_rec_new;
1822         CLOSE c_jai_ar_trx_lines;
1823 
1824         lv_action := jai_constants.INSERTING;
1825 
1826         JAI_AR_TAX_LINES_PKG.POPULATE_TAX_LINES_WRAPPER (
1827                     pr_old            =>  NULL         ,
1828                     pr_new            =>  t_jai_line_rec_new         ,
1829                     pv_action         =>  lv_action         ,
1830                     pv_return_code    =>  lv_return_code    ,
1831                     pv_return_message =>  lv_return_message
1832                   );
1833         IF lv_return_code <> jai_constants.successful   then
1834             RAISE le_error;
1835         END IF;
1836         --Added by Qinglei for JAI Trigger Elimination 11-May-2012 end
1837 
1838        --FND_FILE.PUT_LINE(FND_FILE.LOG,' Cursor . c_get_om_lines ');
1839        /*FND_FILE.PUT_LINE(FND_FILE.LOG,
1840                 ' Inserted  jai_ar_trx_lines for TRX LINE ID: '||v_customer_trx_line_id);
1841        FND_FILE.PUT_LINE(FND_FILE.LOG,' Trx id: ' || v_header_id);*/
1842        if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1843                       FND_LOG.STRING(G_LEVEL_PROCEDURE,
1844                                      G_MODULE_NAME || l_api_name || '.IN',
1845                                      G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
1846                                      ||' Inserted  jai_ar_trx_lines for TRX LINE ID ='||v_customer_trx_line_id
1847                                      ||' ;Trx id ='||v_header_id
1848                                      );
1849        end if;
1850 
1851 
1852        v_ato_line_amount := so_line_rec.line_amount;
1853        v_ato_tax_amount := NVL(v_ato_tax_amount,0) + NVL(v_tax_amount,0);
1854        v_ato_total_amount := NVL(v_ato_total_amount,0) + NVL(so_line_rec.line_amount,0) + NVL(v_ato_tax_amount,0);
1855        /*FND_FILE.PUT_LINE(FND_FILE.LOG, ' Ato tax amount: '||v_ato_tax_amount);
1856        FND_FILE.PUT_LINE(FND_FILE.LOG, ' Ato tot amount: '||v_ato_total_amount);*/
1857        if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1858                       FND_LOG.STRING(G_LEVEL_PROCEDURE,
1859                                      G_MODULE_NAME || l_api_name || '.IN',
1860                                      G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
1861                                      ||' Ato tax amount =' ||v_ato_tax_amount
1862                                      ||' ;Ato tot amount ='||v_ato_total_amount
1863                                      );
1864        end if;
1865       end loop;
1866 
1867 
1868      /*FND_FILE.PUT_LINE(FND_FILE.LOG,
1869              'End of Procedure Process_Taxes_for_ATO_Order');*/
1870 
1871     end if; -- end if for IF NVL(pr_new.Interface_line_attribute11,'0') ='0'
1872    end if;
1873 
1874    if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1875         FND_LOG.STRING(G_LEVEL_PROCEDURE,
1876                        G_MODULE_NAME || l_api_name || '.END',
1877                        G_PKG_NAME || ': ' || l_api_name || '()-');
1878    end if;
1879 
1880   End Process_Taxes_for_ATO_Order;
1881 
1882 begin
1883 
1884     if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1885       FND_LOG.STRING(G_LEVEL_PROCEDURE,
1886                      G_MODULE_NAME || l_api_name || '.BEGIN',
1887                      G_PKG_NAME || ': ' || l_api_name || '()+');
1888     end if;
1889 
1890 
1891 
1892 
1893      v_exist_flag                  := 0;
1894      v_interface_line_attribute3   :=  pr_new.interface_line_attribute3;
1895      v_interface_line_attribute6   :=  pr_new.interface_line_attribute6;
1896      ln_bill_only                  := 0;
1897      v_customer_trx_line_id        := pr_new.customer_trx_line_id;
1898      v_header_id                   := pr_new.customer_trx_id;
1899      v_org_id                      := pr_new.org_id;
1900      v_last_update_date            := pr_new.last_update_date;
1901      v_last_updated_by             := pr_new.last_updated_by;
1902      v_creation_date               := pr_new.creation_date;
1903      v_created_by                  := pr_new.created_by;
1904      v_last_update_login           := pr_new.last_update_login;
1905      ln_legal_entity_id            := get_legal_entity_id(v_header_id) ;
1906      v_tax_amount                  := 0;
1907      v_assessable_value            := 0;
1908       v_basic_excise_duty_amount    := 0;
1909       v_add_excise_duty_amount      := 0;
1910       v_oth_excise_duty_amount      := 0;
1911      v_excise_amount               := 0;
1912      v_line_amount                 := nvl(pr_new.quantity_invoiced * pr_new.unit_selling_price, nvl(pr_new.extended_amount,0));      --added  nvl(pr_new.extended_amount,0) for bug#8849775
1913 
1914 
1915 
1916      OPEN  So_picking_record_check;
1917      FETCH So_picking_record_check INTO v_exist_flag;
1918      CLOSE So_picking_record_check;
1919 
1920 
1921      OPEN   created_from_cur;
1922      FETCH  created_from_cur INTO  v_created_from          ,
1923                                   v_trx_number            ,
1924                                   v_batch_source_id       ,
1925                                   v_books_id              ,
1926                                   v_salesrep_id           ,
1927                                   c_from_currency_code    ,
1928                                   c_conversion_type       ,
1929                                   c_conversion_date       ,
1930                                   c_conversion_rate ;
1931      CLOSE  created_from_cur;
1932 
1933 
1934 
1935      ln_first_time := 0;
1936      IF NVL(v_exist_flag,0) <> 1 THEN
1937 
1938        /* Here checking whether the invoice line corresponds to a bill only workflow , and if is true , the control should
1939           not return because there would be no shipment process  */
1940 
1941        ln_bill_only := 0;
1942        open  c_bill_only_invoice(pr_new.customer_trx_line_id, 'R_BILL_ONLY'); /* Modified by Ramananda for removal of SQL LITERALs */
1943        fetch c_bill_only_invoice into ln_bill_only;
1944        close c_bill_only_invoice;
1945 
1946        if ln_bill_only = 1 then
1947           process_bill_only_invoice;
1948           return;
1949        end if;
1950        -- here code returns for an ato imported order because because
1951        -- records do not exist in JAI_OM_WSH_LINES_ALL table
1952        process_taxes_for_ato_order;
1953 
1954 
1955      END IF;
1956 
1957      v_exist_flag := 0;
1958 
1959        OPEN  so_picking_hdr_info;
1960        FETCH so_picking_hdr_info INTO v_organization_id, v_location_id;
1961        CLOSE so_picking_hdr_info;
1962        --FND_FILE.PUT_LINE(FND_FILE.LOG,'v_organization_id= '||v_organization_id||' v_location_id= ' || v_location_id );
1963        if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1964 
1965             FND_LOG.STRING(G_LEVEL_PROCEDURE,
1966                            G_MODULE_NAME || l_api_name || '.IN',
1967                            G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
1968                            ||' v_organization_id =' ||v_organization_id
1969                            ||' v_location_id ='   ||v_location_id
1970                            );
1971        end if;
1972 
1973     IF v_organization_id IS NULL THEN
1974        OPEN  so_picking_hdr_info_1;
1975        FETCH so_picking_hdr_info_1 INTO v_organization_id, v_location_id;
1976        CLOSE so_picking_hdr_info_1;
1977 
1978        --FND_FILE.PUT_LINE(FND_FILE.LOG,'From Cursor so_picking_hdr_info_1: v_organization_id= '||v_organization_id||' v_location_id= ' || v_location_id );
1979        if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
1980 
1981             FND_LOG.STRING(G_LEVEL_PROCEDURE,
1982                            G_MODULE_NAME || l_api_name || '.IN',
1983                            G_PKG_NAME || ': ' || l_api_name || '().'||'From Cursor so_picking_hdr_info_1:'
1984                            ||' v_organization_id =' ||v_organization_id
1985                            ||' v_location_id ='   ||v_location_id
1986                            );
1987        end if;
1988     END IF ;
1989 
1990 
1991     IF v_organization_id IS NULL THEN
1992       ln_order_ato := NULL ;
1993       OPEN  c_ato_order ;
1994       FETCH c_ato_order INTO ln_order_ato ;
1995       CLOSE c_ato_order ;
1996 
1997       IF nvl(ln_order_ato,0) = 1 THEN
1998         open  c_ato_hdr_info;
1999         fetch c_ato_hdr_info into v_organization_id, v_location_id;
2000         close c_ato_hdr_info;
2001       END IF ;
2002 
2003       --FND_FILE.PUT_LINE(FND_FILE.LOG,'ATO Header Info: v_organization_id= '||v_organization_id||' v_location_id= ' || v_location_id );
2004       if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
2005 
2006             FND_LOG.STRING(G_LEVEL_PROCEDURE,
2007                            G_MODULE_NAME || l_api_name || '.IN',
2008                            G_PKG_NAME || ': ' || l_api_name || '().'||' ATO Header Info:'
2009                            ||' v_organization_id =' ||v_organization_id
2010                            ||' v_location_id ='   ||v_location_id
2011                            );
2012       end if;
2013     END IF ;
2014 
2015 
2016     IF v_organization_id IS NULL
2017     THEN
2018       IF((v_interface_line_attribute3 IS NULL OR v_interface_line_attribute3 = '0') -- delivery_id
2019          AND v_interface_line_attribute6 IS NOT NULL -- order_line_id
2020         )
2021       THEN
2022         OPEN c_so_picking_hdr_info_ns;
2023         FETCH c_so_picking_hdr_info_ns INTO v_organization_id, v_location_id;
2024         CLOSE c_so_picking_hdr_info_ns;
2025         --FND_FILE.PUT_LINE(FND_FILE.LOG,'From Cursor c_so_picking_hdr_info_ns: v_organization_id= '||v_organization_id||' v_location_id= ' || v_location_id );
2026         if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
2027 
2028               FND_LOG.STRING(G_LEVEL_PROCEDURE,
2029                              G_MODULE_NAME || l_api_name || '.IN',
2030                              G_PKG_NAME || ': ' || l_api_name || '().'||' From Cursor c_so_picking_hdr_info_ns:'
2031                              ||' v_organization_id =' ||v_organization_id
2032                              ||' v_location_id ='   ||v_location_id
2033                              );
2034         end if;
2035       END IF; -- v_interface_line_attribute3 IS NULL ... ...
2036     END IF; -- v_organization_id IS NULL
2037 
2038 
2039 
2040 
2041 
2042      OPEN   duplicate_hdr_cur;
2043      FETCH  duplicate_hdr_cur INTO x;
2044      CLOSE  duplicate_hdr_cur;
2045 
2046 
2047      IF NVL(x,0) <> 1 THEN
2048 
2049        open  c_ont_source_code;
2050        fetch c_ont_source_code into v_ont_source_code;
2051        close c_ont_source_code;
2052        v_ont_source_code := ltrim(rtrim(v_ont_source_code));
2053 
2054 
2055        INSERT INTO JAI_AR_TRXS
2056                    (Customer_Trx_ID, Organization_ID, Location_ID, Trx_Number,
2057                    Update_RG_Flag,UPDATE_RG23D_FLAG, Once_Completed_Flag, Batch_Source_ID, Set_Of_Books_ID,
2058                    Primary_Salesrep_ID, Invoice_Currency_Code, Exchange_Rate_Type,
2059                    Exchange_Date, Exchange_Rate,
2060                    creation_date, created_by, last_update_date, last_updated_by, last_update_login,
2061                    legal_entity_id
2062                    ,created_from   --zhiwei
2063                    )
2064                    VALUES (
2065                    v_header_id, v_organization_id, v_location_id, v_trx_number,
2066                    'Y', 'Y','N', v_batch_source_id, v_books_id,
2067                    v_salesrep_id, c_from_currency_code, c_conversion_type,
2068                    c_conversion_date, c_conversion_rate,
2069                    v_creation_date, v_created_by, v_last_update_date, v_last_updated_by, v_last_update_login,
2070                    ln_legal_entity_id
2071                    ,v_created_from    --zhiwei
2072                    );
2073      END IF;
2074 
2075 
2076 
2077      OPEN  so_picking_tax_record_check;
2078      FETCH so_picking_tax_record_check INTO v_exist_flag;
2079      CLOSE so_picking_tax_record_check;
2080 
2081      /*
2082        Check if taxes exist in JAI_OM_WSH_LINE_TAXES tables.
2083        IF yes then insert all the taxes into ja_in_ra_cust_trx_lines_all table
2084        IF no then check the same in ja_in_so_tax_lines_table.
2085 
2086      (
2087      */
2088      IF NVL(v_exist_flag,0) = 1 THEN
2089        v_bond_tax_amt := 0;
2090 
2091        FOR tax_rec IN so_picking_tax_lines_info LOOP
2092 
2093 
2094 
2095         v_base_tax_amount :=0;
2096         v_tax_amt :=0;
2097         v_func_tax_amount :=0;
2098 
2099          OPEN  so_picking_tax_amt(Tax_Rec.tax_id);
2100          FETCH so_picking_tax_amt INTO v_base_tax_amount,v_tax_amt,v_func_tax_amount;
2101          CLOSE so_picking_tax_amt;
2102 
2103          v_tax_line_count := 0;
2104          OPEN  duplicate_tax_lines_cur(tax_rec.tax_id);
2105          FETCH duplicate_tax_lines_cur INTO v_tax_line_count;
2106          CLOSE duplicate_tax_lines_cur;
2107 
2108          IF NVL(v_tax_line_count,0) <> 1 THEN
2109            IF v_register_code ='BOND_REG' THEN
2110              IF tax_rec.tax_type NOT IN ('Excise','Other Excise','CVD_EDUCATION_CESS','EXCISE_EDUCATION_CESS',jai_constants.tax_type_sh_cvd_edu_cess, jai_constants.tax_type_sh_exc_edu_cess) THEN
2111              /*jai_constants.tax_type_sh_cvd_edu_cess,jai_constants.tax_type_sh_exc_edu_cess added by CSahoo BUG#5989740*/
2112                IF (NVL(pr_new.Interface_line_attribute11,'0') = '0'
2113                    and nvl(v_ont_source_code,'ORDER ENTRY') = ltrim(rtrim(pr_new.interface_line_context))
2114                   )
2115                or
2116                   (nvl(v_ont_source_code,'ORDER ENTRY') <> nvl(ltrim(rtrim(pr_new.interface_line_context)),'$$$') -- added by sriram - bug# 3607101
2117                   )
2118                then
2119 
2120                       INSERT INTO JAI_AR_TRX_TAX_LINES (
2121                                                            tax_line_no                        ,
2122                                                            customer_trx_line_id               ,
2123                                                            link_to_cust_trx_line_id           ,
2124                                                            precedence_1                       ,
2125                                                            precedence_2                       ,
2126                                                            precedence_3                       ,
2127                                                            precedence_4                       ,
2128                                                            precedence_5                       ,
2129                                                            precedence_6                       ,
2130                                                            precedence_7                       ,
2131                                                            precedence_8                       ,
2132                                                            precedence_9                       ,
2133                                                            precedence_10                       ,
2134                                                            tax_id                             ,
2135                                                            tax_rate                           ,
2136                                                            qty_rate                           ,
2137                                                            uom                                ,
2138                                                            tax_amount                         ,
2139                                                            func_tax_amount                    ,
2140                                                            base_tax_amount                    ,
2141                                                            creation_date                      ,
2142                                                            created_by                         ,
2143                                                            last_update_date                   ,
2144                                                            last_updated_by                    ,
2145                                                            last_update_login
2146                                                           )
2147                                                     VALUES
2148                                                           (
2149                                                             tax_rec.tax_line_no               ,
2150                                                             ra_customer_trx_lines_s.nextval   ,
2151                                                             v_customer_trx_line_id            ,
2152                                                             tax_rec.precedence_1              ,
2153                                                             tax_rec.precedence_2              ,
2154                                                             tax_rec.precedence_3              ,
2155                                                             tax_rec.precedence_4              ,
2156                                                             tax_rec.precedence_5              ,
2157                                                             tax_rec.precedence_6              ,
2158                                                             tax_rec.precedence_7              ,
2159                                                             tax_rec.precedence_8              ,
2160                                                             tax_rec.precedence_9              ,
2161                                                             tax_rec.precedence_10              ,
2162                                                             tax_rec.tax_id                    ,
2163                                                             tax_rec.tax_rate                  ,
2164                                                             tax_rec.qty_rate                  ,
2165                                                             tax_rec.uom                       ,
2166                                                             round(v_tax_amt,2)                ,
2167                                                             round(v_func_tax_amount,2)        ,
2168                                                             round(v_base_tax_amount,2)        ,
2169                                                             v_creation_date                   ,
2170                                                             v_created_by                      ,
2171                                                             v_last_update_date                ,
2172                                                             v_last_updated_by                 ,
2173                                                             v_last_update_login
2174                                                           );
2175 
2176 
2177                   IF tax_rec.tax_type <> 'TDS' THEN
2178                     v_bond_tax_amt := v_bond_tax_amt + nvl(v_tax_amt,0);
2179                   END IF;
2180                END IF;  /* END IF FOR IF (NVL(pr_new.Interface_line_attribute11,'0') = '0'  */
2181              END IF;   /* END IF FOR IF tax_rec.tax_type NOT IN ('Excise','Other Excise')*/
2182 
2183            ELSE   /*  ELSE OF IF v_register_code ='BOND_REG'  */
2184               -- Here trying to conditionally update Ja_in_ra_customer_trx_table
2185               -- For the second line of the same order line in AR , the tax amounts should not be
2186               -- inserted.Hence conditionally executing the insert statement.
2187 
2188 
2189 
2190 
2191 
2192              IF (NVL(pr_new.Interface_line_attribute11,'0') ='0'
2193                  and nvl(v_ont_source_code,'ORDER ENTRY') = ltrim(rtrim(pr_new.interface_line_context))
2194                 )
2195               or
2196              (nvl(v_ont_source_code,'ORDER ENTRY') <> nvl(ltrim(rtrim(pr_new.interface_line_context)),'$$$')
2197              )
2198              then
2199 
2200                              INSERT INTO JAI_AR_TRX_TAX_LINES(
2201                                                        tax_line_no                   ,
2202                                                        customer_trx_line_id          ,
2203                                                        link_to_cust_trx_line_id      ,
2204                                                        precedence_1                  ,
2205                                                        precedence_2                  ,
2206                                                        precedence_3                  ,
2207                                                        precedence_4                  ,
2208                                                        precedence_5                  ,
2209                                                        precedence_6                  ,
2210                                                        precedence_7                  ,
2211                                                        precedence_8                  ,
2212                                                        precedence_9                  ,
2213                                                        precedence_10                  ,
2214                                                        tax_id                        ,
2215                                                        tax_rate                      ,
2216                                                        qty_rate                      ,
2217                                                        uom                           ,
2218                                                        tax_amount                    ,
2219                                                        func_tax_amount               ,
2220                                                        base_tax_amount               ,
2221                                                        creation_date                 ,
2222                                                        created_by                    ,
2223                                                        last_update_date              ,
2224                                                        last_updated_by               ,
2225                                                        last_update_login
2226                                                       )
2227 
2228                                                 VALUES(
2229                                                        tax_rec.tax_line_no                ,
2230                                                        ra_customer_trx_lines_s.nextval    ,
2231                                                        v_customer_trx_line_id             ,
2232                                                        tax_rec.precedence_1               ,
2233                                                        tax_rec.precedence_2               ,
2234                                                        tax_rec.precedence_3               ,
2235                                                        tax_rec.precedence_4               ,
2236                                                        tax_rec.precedence_5               ,
2237                                                        tax_rec.precedence_6               ,
2238                                                        tax_rec.precedence_7               ,
2239                                                        tax_rec.precedence_8               ,
2240                                                        tax_rec.precedence_9               ,
2241                                                        tax_rec.precedence_10               ,
2242                                                        tax_rec.tax_id                     ,
2243                                                        tax_rec.tax_rate                   ,
2244                                                        tax_rec.qty_rate                   ,
2245                                                        tax_rec.uom                        ,
2246                                                        round(v_tax_amt,2)                 ,
2247                                                        round(v_func_tax_amount,2)         ,
2248                                                        round(v_base_tax_amount,2)         ,
2249                                                        v_creation_date                    ,
2250                                                        v_created_by                       ,
2251                                                        v_last_update_date                 ,
2252                                                        v_last_updated_by                  ,
2253                                                        v_last_update_login
2254                                        );
2255 
2256              END IF;
2257            END IF;  /*  END IF OF IF v_register_code ='BOND_REG'  */
2258          END IF; /* END IF OF IF NVL(v_tax_line_count,0) <> 1 */
2259        END LOOP;
2260        OPEN  so_picking_lines_info;
2261        FETCH so_picking_lines_info INTO v_tax_category_id               ,
2262                                         v_qty                           ,
2263                                         v_tax_amount                    ,
2264                                         v_assessable_value              ,
2265                                         v_basic_excise_duty_amount      ,
2266                                         v_add_excise_duty_amount        ,
2267                                         v_oth_excise_duty_amount        ,
2268                                         v_payment_register              ,
2269                                         v_excise_invoice_no             ,
2270                                         v_preprinted_excise_inv_no      ,
2271                                         v_excise_invoice_date           ,
2272                                         v_excise_exempt_type            ,
2273                                         v_excise_exempt_refno           ,
2274                                         v_excise_exempt_date            ,
2275                                         v_ar3_form_no                   ,
2276                                         v_ar3_form_date                 ,
2277                                         lv_vat_exemption_flag           ,
2278                                         lv_vat_exemption_type           ,
2279                                         lv_vat_exemption_date           ,
2280                                         lv_vat_exemption_refno          ,
2281                                         ln_vat_assessable_value         ,
2282                                         ln_vat_invoice_no               ,
2283                                         ln_vat_invoice_date
2284 
2285                                         ;
2286        CLOSE so_picking_lines_info;
2287 
2288        -- this overrides the v_tax_amount fetched from cursor SO_PICKING_LINES_INFO for bond reg transactions
2289        IF v_register_code = 'BOND_REG' THEN
2290          v_tax_amount := v_bond_tax_amt / v_qty;
2291        END IF;
2292 
2293        -- To pick assessable_value from JAI_OM_OE_SO_LINES instead of JAI_OM_WSH_LINES_ALL
2294          OPEN   C_JA_SO_LINES_ASSESSABLE_VAL;
2295          FETCH  C_JA_SO_LINES_ASSESSABLE_VAL into v_assessable_value,v_service_type;
2296          CLOSE  C_JA_SO_LINES_ASSESSABLE_VAL;
2297 
2298 
2299      ELSE  /* ELSE OF IF NVL(v_exist_flag,0) = 1 */
2300        -- for non-shippable items, need to fetch OFI taxes from JAI_OM_WSH_LINE_TAXES
2301        IF((v_interface_line_attribute3 IS NULL OR v_interface_line_attribute3 = '0') -- delivery_id
2302          AND v_interface_line_attribute6 IS NOT NULL -- order_line_id
2303         )
2304        THEN
2305          OPEN  c_so_picking_tax_record_ns;
2306          FETCH c_so_picking_tax_record_ns INTO v_exist_flag;
2307          CLOSE c_so_picking_tax_record_ns;
2308          IF NVL(v_exist_flag,0) = 1
2309          THEN
2310            v_bond_tax_amt := 0;
2311            FOR tax_rec IN c_so_picking_tax_lines_info_ns
2312            LOOP
2313              v_base_tax_amount :=0;
2314              v_tax_amt :=0;
2315              v_func_tax_amount :=0;
2316              OPEN  c_so_picking_tax_amt_ns (Tax_Rec.tax_id);
2317              FETCH c_so_picking_tax_amt_ns INTO v_base_tax_amount,v_tax_amt,v_func_tax_amount;
2318              CLOSE c_so_picking_tax_amt_ns;
2319 
2320              v_tax_line_count := 0;
2321              OPEN  duplicate_tax_lines_cur(tax_rec.tax_id);
2322              FETCH duplicate_tax_lines_cur INTO v_tax_line_count;
2323              CLOSE duplicate_tax_lines_cur;
2324 
2325              IF NVL(v_tax_line_count,0) <> 1 THEN
2326                IF v_register_code ='BOND_REG' THEN
2327                  IF tax_rec.tax_type NOT IN ('Excise','Other Excise','CVD_EDUCATION_CESS',
2328                                             'EXCISE_EDUCATION_CESS',
2329                                             jai_constants.tax_type_sh_cvd_edu_cess,
2330                                             jai_constants.tax_type_sh_exc_edu_cess)
2331                  THEN
2332                    IF (NVL(pr_new.Interface_line_attribute11,'0') = '0'
2333                       AND nvl(v_ont_source_code,'ORDER ENTRY') =
2334                           ltrim(rtrim(pr_new.interface_line_context))
2335                       )
2336                       OR
2337                       (nvl(v_ont_source_code,'ORDER ENTRY') <>
2338                        nvl(ltrim(rtrim(pr_new.interface_line_context)),'$$$')
2339                       )
2340                    THEN
2341                      INSERT INTO JAI_AR_TRX_TAX_LINES (
2342                                                            tax_line_no                    ,
2343                                                            customer_trx_line_id           ,
2344                                                            link_to_cust_trx_line_id       ,
2345                                                            precedence_1                   ,
2346                                                            precedence_2                   ,
2347                                                            precedence_3                   ,
2348                                                            precedence_4                   ,
2349                                                            precedence_5                   ,
2350                                                            precedence_6                   ,
2351                                                            precedence_7                   ,
2352                                                            precedence_8                   ,
2353                                                            precedence_9                   ,
2354                                                            precedence_10                  ,
2355                                                            tax_id                         ,
2356                                                            tax_rate                       ,
2357                                                            qty_rate                       ,
2358                                                            uom                            ,
2359                                                            tax_amount                     ,
2360                                                            func_tax_amount                ,
2361                                                            base_tax_amount                ,
2362                                                            creation_date                  ,
2363                                                            created_by                    ,
2364                                                            last_update_date               ,
2365                                                            last_updated_by                ,
2366                                                            last_update_login
2367                                                        )
2368                                                  VALUES
2369                                                        (
2370                                                             tax_rec.tax_line_no           ,
2371                                                             ra_customer_trx_lines_s.nextval,
2372                                                             v_customer_trx_line_id        ,
2373                                                             tax_rec.precedence_1           ,
2374                                                             tax_rec.precedence_2           ,
2375                                                             tax_rec.precedence_3           ,
2376                                                             tax_rec.precedence_4           ,
2377                                                             tax_rec.precedence_5           ,
2378                                                             tax_rec.precedence_6           ,
2379                                                             tax_rec.precedence_7           ,
2380                                                             tax_rec.precedence_8           ,
2381                                                             tax_rec.precedence_9           ,
2382                                                             tax_rec.precedence_10           ,
2383                                                             tax_rec.tax_id                 ,
2384                                                             tax_rec.tax_rate               ,
2385                                                             tax_rec.qty_rate               ,
2386                                                             tax_rec.uom                    ,
2387                                                             round(v_tax_amt,2)             ,
2388                                                             round(v_func_tax_amount,2)     ,
2389                                                             round(v_base_tax_amount,2)     ,
2390                                                             v_creation_date                ,
2391                                                             v_created_by                   ,
2392                                                             v_last_update_date             ,
2393                                                             v_last_updated_by              ,
2394                                                             v_last_update_login
2395                                                           );
2396                      IF tax_rec.tax_type <> 'TDS' THEN
2397                        v_bond_tax_amt := v_bond_tax_amt + nvl(v_tax_amt,0);
2398                      END IF;
2399                   END IF;  /* END IF FOR IF (NVL(pr_new.Interface_line_attribute11,'0') = '0'  */
2400                END IF;   /* END IF FOR IF tax_rec.tax_type NOT IN ('Excise','Other Excise')*/
2401 
2402            ELSE   /*  ELSE OF IF v_register_code ='BOND_REG'  */
2403               -- Here trying to conditionally update Ja_in_ra_customer_trx_table
2404               -- For the second line of the same order line in AR , the tax amounts should
2405               -- not be
2406               -- inserted.Hence conditionally executing the insert statement.
2407              IF (NVL(pr_new.Interface_line_attribute11,'0') ='0'
2408                  AND nvl(v_ont_source_code,'ORDER ENTRY') =
2409                          ltrim(rtrim(pr_new.interface_line_context))
2410                 )
2411                 OR
2412                 (nvl(v_ont_source_code,'ORDER ENTRY') <>
2413                  nvl(ltrim(rtrim(pr_new.interface_line_context)),'$$$')
2414                 )
2415              THEN
2416                INSERT INTO JAI_AR_TRX_TAX_LINES(
2417                                                        tax_line_no                   ,
2418                                                        customer_trx_line_id          ,
2419                                                        link_to_cust_trx_line_id      ,
2420                                                        precedence_1                  ,
2421                                                        precedence_2                  ,
2422                                                        precedence_3                  ,
2423                                                        precedence_4                  ,
2424                                                        precedence_5                  ,
2425                                                        precedence_6                  ,
2426                                                        precedence_7                  ,
2427                                                        precedence_8                  ,
2428                                                        precedence_9                  ,
2429                                                        precedence_10                  ,
2430                                                        tax_id                        ,
2431                                                        tax_rate                      ,
2432                                                        qty_rate                      ,
2433                                                        uom                           ,
2434                                                        tax_amount                    ,
2435                                                        func_tax_amount               ,
2436                                                        base_tax_amount               ,
2437                                                        creation_date                 ,
2438                                                        created_by                    ,
2439                                                        last_update_date              ,
2440                                                        last_updated_by               ,
2441                                                        last_update_login
2442                                                       )
2443 
2444                                                 VALUES(
2445                                                        tax_rec.tax_line_no                ,
2446                                                        ra_customer_trx_lines_s.nextval    ,
2447                                                        v_customer_trx_line_id             ,
2448                                                        tax_rec.precedence_1               ,
2449                                                        tax_rec.precedence_2               ,
2450                                                        tax_rec.precedence_3               ,
2451                                                        tax_rec.precedence_4               ,
2452                                                        tax_rec.precedence_5               ,
2453                                                        tax_rec.precedence_6               ,
2454                                                        tax_rec.precedence_7               ,
2455                                                        tax_rec.precedence_8               ,
2456                                                        tax_rec.precedence_9               ,
2457                                                        tax_rec.precedence_10               ,
2458                                                        tax_rec.tax_id                     ,
2459                                                        tax_rec.tax_rate                   ,
2460                                                        tax_rec.qty_rate                   ,
2461                                                        tax_rec.uom                        ,
2462                                                        round(v_tax_amt,2)                 ,
2463                                                        round(v_func_tax_amount,2)         ,
2464                                                        round(v_base_tax_amount,2)         ,
2465                                                        v_creation_date                    ,
2466                                                        v_created_by                       ,
2467                                                        v_last_update_date                 ,
2468                                                        v_last_updated_by                  ,
2469                                                        v_last_update_login
2470                                                        );
2471 
2472              END IF;
2473            END IF;  /*  END IF OF IF v_register_code ='BOND_REG'  */
2474          END IF; /* END IF OF IF NVL(v_tax_line_count,0) <> 1 */
2475        END LOOP;
2476        OPEN  c_so_picking_lines_info_ns;
2477        FETCH c_so_picking_lines_info_ns INTO v_tax_category_id               ,
2478                                         v_qty                           ,
2479                                         v_tax_amount                    ,
2480                                         v_assessable_value              ,
2481                                         v_basic_excise_duty_amount      ,
2482                                         v_add_excise_duty_amount        ,
2483                                         v_oth_excise_duty_amount        ,
2484                                         v_payment_register              ,
2485                                         v_excise_invoice_no             ,
2486                                         v_preprinted_excise_inv_no      ,
2487                                         v_excise_invoice_date           ,
2488                                         v_excise_exempt_type            ,
2489                                         v_excise_exempt_refno           ,
2490                                         v_excise_exempt_date            ,
2491                                         v_ar3_form_no                   ,
2492                                         v_ar3_form_date                 ,
2493                                         lv_vat_exemption_flag           ,
2494                                         lv_vat_exemption_type           ,
2495                                         lv_vat_exemption_date           ,
2496                                         lv_vat_exemption_refno          ,
2497                                         ln_vat_assessable_value         ,
2498                                         ln_vat_invoice_no               ,
2499                                         ln_vat_invoice_date
2500                                          ;
2501 
2502        CLOSE c_so_picking_lines_info_ns;
2503 
2504        IF v_register_code = 'BOND_REG' THEN
2505          v_tax_amount := v_bond_tax_amt / v_qty;
2506        END IF;
2507        -- To pick assessable_value from JAI_OM_OE_SO_LINES instead of JAI_OM_WSH_LINES_ALL
2508          OPEN   C_JA_SO_LINES_ASSESSABLE_VAL;
2509          FETCH  C_JA_SO_LINES_ASSESSABLE_VAL into v_assessable_value,v_service_type;
2510          CLOSE  C_JA_SO_LINES_ASSESSABLE_VAL;
2511        END IF; -- NVL(v_exist_flag,0) = 1
2512      ELSE -- ELSE OF IF v_interface_line_attribute3 IS NULL ... ...
2513        -- else, the record is not non-shippable item
2514        -- original logic of getting taxes from JAI_OM_OE_SO_TAXES
2515        /*
2516        If records do not exist in JAI_OM_WSH_LINE_TAXES table then control comes to this portion of the code
2517        */
2518 
2519        OPEN  ja_so_lines_tax_record_check ;
2520        FETCH ja_so_lines_tax_record_check INTO v_exist_flag;
2521        CLOSE ja_so_lines_tax_record_check;
2522        /*
2523        Check if a tax line exists in ja_in_so_tax_lines_table
2524        IF yes then insert all the taxes into JAI_AR_TRX_TAX_LINES table.
2525        */
2526        IF NVL(v_exist_flag,0) = 1 THEN
2527          FOR tax_rec IN ja_so_tax_lines_info LOOP
2528 
2529             ln_tax_exist := 0 ;
2530             OPEN  c_duplicate_tax(tax_rec.tax_id, v_customer_trx_line_id)  ;
2531             FETCH c_duplicate_tax INTO ln_tax_exist ;
2532             CLOSE c_duplicate_tax ;
2533 
2534             IF nvl(ln_tax_exist, 0) <> 1 THEN
2535 
2536               INSERT INTO JAI_AR_TRX_TAX_LINES(
2537                tax_line_no                 ,
2538                customer_trx_line_id        ,
2539                link_to_cust_trx_line_id    ,
2540                precedence_1                ,
2541                precedence_2                ,
2542                precedence_3                ,
2543                precedence_4                ,
2544                precedence_5                ,
2545                precedence_6                ,
2546                precedence_7                ,
2547                precedence_8                ,
2548                precedence_9                ,
2549                precedence_10                ,
2550                tax_id                      ,
2551                tax_rate                    ,
2552                qty_rate                    ,
2553                uom                         ,
2554                tax_amount                  ,
2555                func_tax_amount             ,
2556                base_tax_amount             ,
2557                creation_date               ,
2558                created_by                  ,
2559                last_update_date            ,
2560                last_updated_by             ,
2561                last_update_login
2562               )
2563           VALUES(
2564                tax_rec.tax_line_no               ,
2565                ra_customer_trx_lines_s.nextval   ,
2566                v_customer_trx_line_id            ,
2567                tax_rec.precedence_1              ,
2568                tax_rec.precedence_2              ,
2569                tax_rec.precedence_3              ,
2570                tax_rec.precedence_4              ,
2571                tax_rec.precedence_5              ,
2572                tax_rec.precedence_6              ,
2573                tax_rec.precedence_7              ,
2574                tax_rec.precedence_8              ,
2575                tax_rec.precedence_9              ,
2576                tax_rec.precedence_10              ,
2577          tax_rec.tax_id                    ,
2578                tax_rec.tax_rate                  ,
2579                tax_rec.qty_rate                  ,
2580                tax_rec.uom                       ,
2581                round(tax_rec.tax_amount,2)       ,
2582                round(tax_rec.func_tax_amount,2)  ,
2583                round(tax_rec.base_tax_amount,2)  ,
2584                v_creation_date                   ,
2585                v_created_by                      ,
2586                v_last_update_date                ,
2587                v_last_updated_by                 ,
2588                v_last_update_login
2589                );
2590            END IF ;
2591          END LOOP;
2592          OPEN  ja_so_lines_info;
2593          FETCH ja_so_lines_info INTO    v_tax_category_id       ,
2594                                         v_tax_amount            ,
2595                                         v_assessable_value      ,
2596                                         v_excise_exempt_type    ,
2597                                         v_excise_exempt_refno   ,
2598                                         v_excise_exempt_date    ,
2599                                         lv_vat_exemption_flag   ,
2600                                         lv_vat_exemption_type   ,
2601                                         lv_vat_exemption_date   ,
2602                                         lv_vat_exemption_refno  ,
2603                                         ln_vat_assessable_value,
2604                                         v_service_type
2605 
2606           ;
2607          CLOSE ja_so_lines_info;
2608          ELSE /* ELSE FOR IF NVL(v_exist_flag,0) = 1  */
2609              /*
2610             If taxes do not exist in JAI_OM_WSH_LINE_TAXES and JAI_OM_OE_SO_TAXES then control
2611             would come here.
2612             Code should return only in case of an RMA i.e return only when a line does not exist in JAI_OM_OE_SO_LINES table
2613             for the given line_id.
2614              */
2615            DECLARE
2616              CURSOR c_so_lines_exists
2617              IS
2618              SELECT    1
2619              FROM      JAI_OM_OE_SO_LINES
2620              WHERE     line_id = pr_new.interface_line_attribute6;
2621              lv_exists VARCHAR2(1);
2622            BEGIN
2623     pv_return_code := jai_constants.successful ;
2624              OPEN   c_so_lines_exists ;
2625              FETCH  c_so_lines_exists INTO lv_exists;
2626              IF c_so_lines_exists%NOTFOUND THEN
2627                 CLOSE  c_so_lines_exists;
2628                 --fnd_file.put_line(FND_FILE.LOG,'no lines in jai_om_oe_so_lines -return');
2629                 if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
2630                   FND_LOG.STRING(G_LEVEL_PROCEDURE,
2631                                  G_MODULE_NAME || l_api_name || '.IN',
2632                                  G_PKG_NAME || ': ' || l_api_name || '().'||' no lines in jai_om_oe_so_lines -return '
2633                                  );
2634                 end if;
2635                return;
2636              END IF;
2637              CLOSE  c_so_lines_exists;
2638            END ;
2639        END IF; /* END IF FOR IF NVL(v_exist_flag,0) = 1  */
2640 
2641 
2642        END IF; -- -- v_interface_line_attribute3 IS NULL ... ...
2643 
2644 
2645 
2646 
2647      END IF;
2648      OPEN  duplicate_lines_cur;
2649      FETCH duplicate_lines_cur INTO v_line_count;
2650      CLOSE duplicate_lines_cur;
2651 
2652      --fnd_file.put_line(FND_FILE.LOG, ' Line cnt: '|| v_line_count);
2653      if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
2654         FND_LOG.STRING(G_LEVEL_PROCEDURE,
2655                        G_MODULE_NAME || l_api_name || '.IN',
2656                        G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
2657                        ||' Line cnt ='||v_line_count
2658                        );
2659      end if;
2660      IF NVL(v_line_count,0) <> 1 THEN
2661               -- Here trying to conditionally update Ja_in_ra_customer_trx_table
2662               -- For the second line of the same order line in AR , the tax amount should not be
2663               -- inserted.Hence Setting setting tax amount variable  v_calc_tax_amount to 0.
2664          IF (NVL(pr_new.Interface_line_attribute11,'0') ='0'
2665              and nvl(v_ont_source_code,'ORDER ENTRY') = ltrim(rtrim(pr_new.interface_line_context))
2666             )
2667          or
2668          (nvl(v_ont_source_code,'ORDER ENTRY') <> nvl(ltrim(rtrim(pr_new.interface_line_context)),'$$$')
2669          )
2670          then
2671 
2672            --fnd_file.put_line(FND_FILE.LOG, ' int. line att1 = 0 ');
2673            if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
2674               FND_LOG.STRING(G_LEVEL_PROCEDURE,
2675                              G_MODULE_NAME || l_api_name || '.IN',
2676                              G_PKG_NAME || ': ' || l_api_name || '().'||'int. line att1 = 0'
2677 
2678                              );
2679            end if;
2680 
2681            v_calc_tax_amount := v_tax_amount * v_qty;
2682            OPEN  c_cust_trx_tax_line_amt;
2683            FETCH c_cust_trx_tax_line_amt into v_calc_tax_amount;
2684            CLOSE c_cust_trx_tax_line_amt;
2685          ELSE
2686            v_calc_tax_amount := 0;
2687          END IF;
2688 
2689 
2690          open   JA_SO_LINES_INFO;
2691          fetch  JA_SO_LINES_INFO into rec_so_lines;
2692          close  JA_SO_LINES_INFO;
2693 
2694 
2695          INSERT INTO JAI_AR_TRX_LINES (
2696                                                    customer_trx_line_id                         ,
2697                                                    line_number                                  ,
2698                                                    customer_trx_id                              ,
2699                                                    description                                  ,
2700                                                    payment_register                             ,
2701                                                    excise_invoice_no                            ,
2702                                                    preprinted_excise_inv_no                     ,
2703                                                    excise_invoice_date                          ,
2704                                                    inventory_item_id                            ,
2705                                                    unit_code                                    ,
2706                                                    quantity                                     ,
2707                                                    tax_category_id                              ,
2708                                                    auto_invoice_flag                            ,
2709                                                    unit_selling_price                           ,
2710                                                    line_amount                                  ,
2711                                                    tax_amount                                   ,
2712                                                    total_amount                                 ,
2713                                                    assessable_value                             ,
2714                                                    creation_date                                ,
2715                                                    created_by                                   ,
2716                                                    last_update_date                             ,
2717                                                    last_updated_by                              ,
2718                                                    last_update_login                            ,
2719                                                    excise_exempt_type                           ,
2720                                                    excise_exempt_refno                          ,
2721                                                    excise_exempt_date                           ,
2722                                                    ar3_form_no                                  ,
2723                                                    ar3_form_date                                ,
2724                                                    vat_exemption_flag                           ,
2725                                                    vat_exemption_type                           ,
2726                                                    vat_exemption_date                           ,
2727                                                    vat_exemption_refno                                    ,
2728                                                    vat_assessable_value,
2729                                                    service_type_code
2730 
2731                                                   )
2732                                          VALUES  (
2733                                                    v_customer_trx_line_id                       ,
2734                                                    pr_new.line_number                             ,
2735                                                    v_header_id                                  ,
2736                                                    pr_new.description                             ,
2737                                                    v_payment_register                           ,
2738                                                    v_excise_invoice_no                          ,
2739                                                    v_preprinted_excise_inv_no                   ,
2740                                                    v_excise_invoice_date                        ,
2741                                                    pr_new.inventory_item_id                       ,
2742                                                    pr_new.uom_code                                ,
2743                                                    pr_new.quantity_invoiced                       ,
2744                                                    v_tax_category_id                            ,
2745                                                    'Y'                                          ,
2746                                                    pr_new.unit_selling_price                      ,
2747                                                    round(v_line_amount,2)                       ,
2748                                                    round(v_calc_tax_amount,2)                   ,
2749                                                    round((v_line_amount + v_calc_tax_amount),2) ,
2750                                                    v_assessable_value                           ,
2751                                                    v_creation_date                              ,
2752                                                    v_created_by                                 ,
2753                                                    v_last_update_date                           ,
2754                                                    v_last_updated_by                            ,
2755                                                    v_last_update_login                          ,
2756                                                    v_excise_exempt_type                         ,
2757                                                    v_excise_exempt_refno                        ,
2758                                                    v_excise_exempt_date                         ,
2759                                                    v_ar3_form_no                                ,
2760                                                    v_ar3_form_date                              ,
2761                                                    lv_vat_exemption_flag                        ,
2762                                                    lv_vat_exemption_type                        ,
2763                                                    lv_vat_exemption_date                        ,
2764                                                    lv_vat_exemption_refno                       ,
2765                                                    ln_vat_assessable_value,
2766                                                    rec_so_lines.service_type_code
2767                                                  );
2768     --Added by Qinglei for JAI Trigger Elimination 11-May-2012 begin
2769     OPEN c_jai_ar_trx_lines(v_header_id,v_customer_trx_line_id);
2770     FETCH c_jai_ar_trx_lines into t_jai_line_rec_new;
2771     CLOSE c_jai_ar_trx_lines;
2772 
2773     lv_action := jai_constants.INSERTING;
2774 
2775     JAI_AR_TAX_LINES_PKG.POPULATE_TAX_LINES_WRAPPER (
2776                 pr_old            =>  NULL         ,
2777                 pr_new            =>  t_jai_line_rec_new         ,
2778                 pv_action         =>  lv_action         ,
2779                 pv_return_code    =>  lv_return_code    ,
2780                 pv_return_message =>  lv_return_message
2781               );
2782     IF lv_return_code <> jai_constants.successful   then
2783         RAISE le_error;
2784     END IF;
2785     --Added by Qinglei for JAI Trigger Elimination 11-May-2012 end
2786   ELSE  /* ELSE OF IF NVL(v_line_count,0) <> 1*/
2787     OPEN  old_customer_trx_id_cur;
2788     FETCH old_customer_trx_id_cur INTO v_old_customer_trx_id;
2789     CLOSE old_customer_trx_id_cur;
2790 
2791 
2792     -- commented the following delete statement as this is not required in this trigger.
2793     -- This statement is executed in Concurrent process to default taxes from OM to AR
2794     --DELETE  JAI_AR_TRX_INS_LINES_T
2795     --WHERE   customer_trx_line_id = v_customer_trx_line_id;
2796 
2797     IF v_old_customer_trx_id <> v_header_id THEN
2798       --Added by Qinglei for JAI Trigger Elimination 11-May-2012 begin
2799       OPEN c_jai_ar_trx_lines(v_old_customer_trx_id,v_customer_trx_line_id);
2800       FETCH c_jai_ar_trx_lines INTO t_jai_line_rec_old;
2801       CLOSE c_jai_ar_trx_lines;
2802 
2803       UPDATE  JAI_AR_TRX_LINES
2804       SET     customer_trx_id = v_header_id
2805       WHERE   customer_trx_line_id = v_customer_trx_line_id;
2806 
2807       OPEN c_jai_ar_trx_lines(v_header_id,v_customer_trx_line_id);
2808       FETCH c_jai_ar_trx_lines INTO t_jai_line_rec_new;
2809       CLOSE c_jai_ar_trx_lines;
2810 
2811       lv_action := jai_constants.UPDATING;
2812 
2813       IF ( ( ( t_jai_line_rec_new.AUTO_INVOICE_FLAG <> 'Y'    AND
2814               t_jai_line_rec_old.AUTO_INVOICE_FLAG <> 'Y'
2815              )                                  AND
2816               (t_jai_line_rec_new.Excise_Invoice_No IS NULL)  AND
2817               (t_jai_line_rec_new.payment_Register  IS NULL)  AND
2818               (t_jai_line_rec_new.Excise_Invoice_Date IS NULL)
2819            )                                    OR
2820            (t_jai_line_rec_new.Customer_Trx_Id <> t_jai_line_rec_old.Customer_Trx_Id)
2821          )
2822       THEN
2823          JAI_AR_TAX_LINES_PKG.POPULATE_TAX_LINES_WRAPPER (
2824                                         pr_old            =>  t_jai_line_rec_old,
2825                                         pr_new            =>  t_jai_line_rec_new,
2826                                         pv_action         =>  lv_action,
2827                                         pv_return_code    =>  lv_return_code,
2828                                         pv_return_message =>  lv_return_message
2829                                       );
2830         IF lv_return_code <> jai_constants.successful   then
2831           RAISE le_error;
2832         END IF;
2833       END IF;
2834      --Added by Qinglei for JAI Trigger Elimination 11-May-2012 end
2835       DELETE   JAI_AR_TRXS
2836       WHERE    customer_trx_id = v_old_customer_trx_id;
2837     END IF;
2838   END IF; /* END IF OF IF NVL(v_line_count,0) <> 1 */
2839 
2840   v_excise_amount := round(nvl(v_basic_excise_duty_amount,0) + nvl(v_add_excise_duty_amount,0) + nvl(v_oth_excise_duty_amount,0));
2841 
2842   --fnd_file.put_line(FND_FILE.LOG, ' Excise amount: '|| v_excise_amount);
2843   if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
2844       FND_LOG.STRING(G_LEVEL_PROCEDURE,
2845 
2846                      G_MODULE_NAME || l_api_name || '.IN',
2847                      G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
2848                      ||' Excise amount ='||v_excise_amount
2849                      );
2850   end if;
2851   OPEN  complete_flag_cur;
2852   FETCH complete_flag_cur INTO v_once_completed_flag;
2853   CLOSE complete_flag_cur;
2854 
2855   --fnd_file.put_line(FND_FILE.LOG, ' Once complete flag'|| v_once_completed_flag);
2856   if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
2857       FND_LOG.STRING(G_LEVEL_PROCEDURE,
2858 
2859                      G_MODULE_NAME || l_api_name || '.IN',
2860                      G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
2861                      ||' Once complete flag ='||v_once_completed_flag
2862                      );
2863   end if;
2864   v_so_tax_amount := v_tax_amount * v_qty ;
2865 
2866 
2867 
2868   -- Instead the following update has been modified . It used a new variable
2869   -- called v_so_tax_amount to calculate the tax amount.
2870   -- Here trying to conditionally update Ja_in_ra_customer_trx_table
2871   -- For the second line of the same order line in AR , the tax amount should not be
2872   -- updated.
2873 
2874 
2875   OPEN  c_cust_trx_tax_line_amt;
2876   FETCH c_cust_trx_tax_line_amt into v_so_tax_amount;
2877   CLOSE c_cust_trx_tax_line_amt;
2878 
2879   --fnd_file.put_line(FND_FILE.LOG, ' so tax amount: '|| v_so_tax_amount);
2880   if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
2881       FND_LOG.STRING(G_LEVEL_PROCEDURE,
2882 
2883                      G_MODULE_NAME || l_api_name || '.IN',
2884                      G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
2885                      ||' so tax amount ='||v_so_tax_amount
2886                      );
2887   end if;
2888 
2889 
2890   --Added by Zhiwei for JAI Trigger elimination begin
2891   ---------------------------------------------------------
2892   open c_get_rec(v_header_id);
2893   fetch c_get_rec into t_rec_old;
2894   close c_get_rec;
2895   ---------------------------------------------------------
2896   --Added by Zhiwei for JAI Trigger elimination end
2897 
2898   IF (NVL(pr_new.Interface_line_attribute11,'0') ='0'
2899       and nvl(v_ont_source_code,'ORDER ENTRY') = ltrim(rtrim(pr_new.interface_line_context))
2900      )
2901   or
2902      (nvl(v_ont_source_code,'ORDER ENTRY') <> nvl(ltrim(rtrim(pr_new.interface_line_context)),'$$$')
2903      )
2904   then
2905 
2906     -- instead of the previous if condition because , it may fail in circumstances
2907     -- where line ordering rules are set up in autoinvoice options.
2908 
2909     UPDATE  JAI_AR_TRXS
2910     SET     line_amount  =  round(NVL(line_amount,0) + NVL(v_line_amount,0),2),
2911                tax_amount   =  round(NVL(tax_amount,0) + NVL(v_so_tax_amount,0),2),
2912                total_amount =  round(NVL(total_amount,0) + NVL(v_line_amount,0) + NVL(v_so_tax_amount,0),2),
2913                once_completed_flag = NVL(v_once_completed_flag,'N'),
2914                complete_flag = NVL(v_once_completed_flag,'N'),--added by zhiwei for ZX integration
2915                vat_invoice_no = ln_vat_invoice_no, vat_invoice_date = ln_vat_invoice_date
2916     WHERE   customer_trx_id = v_header_id;
2917   ELSE
2918 
2919     UPDATE  JAI_AR_TRXS
2920     SET     line_amount  =  round(NVL(line_amount, 0 ) + NVL(v_line_amount,0),2),
2921             total_amount =  round(NVL(total_amount,0) + NVL(v_line_amount,0),2),
2922             once_completed_flag = NVL(v_once_completed_flag,'N'),
2923             vat_invoice_no = ln_vat_invoice_no, vat_invoice_date = ln_vat_invoice_date
2924     WHERE   customer_trx_id = v_header_id;
2925   END IF;
2926 
2927     --Added by Zhiwei for JAI Trigger elimination begin
2928     ---------------------------------------------------------
2929     open c_get_rec(v_header_id);
2930     fetch c_get_rec into t_rec_new;
2931     close c_get_rec;
2932 
2933     if(t_rec_new.once_completed_flag = 'Y')then
2934 
2935          lv_action := jai_constants.updating ;
2936 
2937          JAI_AR_TRXS_PKG.UPDATE_EXCISE_REGISTERS(
2938                         pr_old            =>  t_rec_old         ,
2939                         pr_new            =>  t_rec_new         ,
2940                         pv_action         =>  lv_action         ,
2941                         pv_return_code    =>  lv_return_code    ,
2942                         pv_return_message =>  lv_return_message
2943                       );
2944 
2945           IF lv_return_code <> jai_constants.successful   then
2946              RAISE le_error;
2947           END IF;
2948 
2949     end if;
2950     ---------------------------------------------------------
2951     --Added by Zhiwei for JAI Trigger elimination end
2952 
2953     if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
2954       FND_LOG.STRING(G_LEVEL_PROCEDURE,
2955                      G_MODULE_NAME || l_api_name || '.END',
2956                      G_PKG_NAME || ': ' || l_api_name || '()-');
2957     end if;
2958     EXCEPTION
2959      when le_error then
2960        pv_return_code     :=  jai_constants.unexpected_error;
2961        pv_return_message  := 'Encountered an error in JAI_AR_IMPORT_POPU_PKG.PROCESS_ORDER '  || lv_return_message;
2962        fnd_file.put_line(FND_FILE.LOG,'Encountered an error in JAI_AR_IMPORT_POPU_PKG.PROCESS_ORDER '  || lv_return_message);
2963      WHEN OTHERS THEN
2964        pv_return_code     :=  jai_constants.unexpected_error;
2965        pv_return_message  := 'Encountered an error in JAI_AR_IMPORT_POPU_PKG.PROCESS_ORDER '  || substr(sqlerrm,1,1900);
2966        fnd_file.put_line(FND_FILE.LOG,'Encountered an error in JAI_AR_IMPORT_POPU_PKG.PROCESS_ORDER '  || substr(sqlerrm,1,1900));
2967 
2968 end process_order;
2969 
2970 procedure process_supplement(
2971           pr_old t_rec%type ,
2972           pr_new t_rec%type ,
2973           pv_once_complete_flag varchar2,
2974           pv_action varchar2 ,
2975           pv_return_code out nocopy varchar2 ,
2976           pv_return_message out nocopy varchar2)
2977 is
2978   --Cursor
2979   CURSOR GET_EXCHANGE_RATE IS
2980   SELECT exchange_rate
2981     FROM   ra_customer_trx_all
2982    WHERE  customer_trx_id = pr_new.customer_trx_id;
2983 
2984   CURSOR DUPLICATE_HDR_CUR IS
2985   SELECT 1
2986   FROM   JAI_AR_TRXS
2987   WHERE  customer_trx_id = pr_new.customer_trx_id;
2988 
2989   CURSOR SUPPLEMENT_LINES_CHECK_CNSLDT IS
2990   SELECT 1
2991   FROM    JAI_AR_SUP_LINES A,
2992           JAI_AR_SUP_HDRS_ALL B,
2993           RA_CUSTOMER_TRX_ALL C,
2994           RA_CUST_TRX_TYPES_ALL D
2995   WHERE   pr_new.CUSTOMER_TRX_ID = C.customer_trx_id
2996    AND    c.cust_trx_type_id = D.cust_trx_type_id
2997    AND    c.trx_number = TO_CHAR(b.SUPPLEMENTARY_NUM)
2998    AND    b.supp_inv_type = DECODE(D.TYPE,'INV','SI','CM','CR','DM','DB')
2999    AND    b.supp_inv_type = A.sup_inv_type
3000    AND    A.customer_trx_id = b.customer_trx_id;
3001 
3002   CURSOR SUPPLEMENT_LINES_CHECK IS
3003   SELECT 1
3004   FROM   JAI_AR_SUP_LINES
3005   WHERE  customer_trx_line_id = TO_NUMBER(pr_new.interface_line_attribute3);
3006 
3007   CURSOR  JA_IN_RA_CUSTOMER_TRX_INFOCNSL IS
3008   SELECT A.organization_id,
3009          A.Location_id
3010   FROM   JAI_AR_TRXS A,
3011          JAI_AR_SUP_HDRS_ALL B,
3012          RA_CUSTOMER_TRX_ALL C
3013   WHERE  pr_new.customer_trx_id = c.customer_trx_id
3014   AND    c.trx_number = TO_CHAR(B.SUPPLEMENTARY_NUM)
3015   AND    B.customer_trx_id = A.customer_trx_id;
3016 
3017   CURSOR  JA_IN_RA_CUSTOMER_TRX_INFO  IS
3018   SELECT organization_id,
3019          Location_id
3020   FROM   JAI_AR_TRXS
3021   WHERE  customer_trx_id = TO_NUMBER(pr_new.interface_line_attribute4);
3022 
3023 
3024   CURSOR CREATED_FROM_CUR IS
3025   SELECT created_from, trx_number, batch_source_id, set_of_books_id, primary_salesrep_id,
3026          invoice_currency_code, exchange_rate_type, exchange_date, exchange_rate
3027     FROM   ra_customer_trx_all
3028    WHERE  customer_trx_id = pr_new.customer_trx_id;
3029 
3030   v_customer_trx_line_id        NUMBER;
3031 
3032   CURSOR SUPPLEMENT_TAX_LINES_CHECK IS
3033   SELECT 1
3034   FROM   JAI_AR_SUP_TAXES A, JAI_AR_SUP_LINES b
3035   WHERE  A.link_to_cust_trx_line_id = b.customer_trx_line_id
3036   AND    b.customer_trx_line_id = v_customer_Trx_line_id;
3037 
3038   CURSOR SUPPLEMENT_TAX_LINES_CHECK_CNS IS
3039    SELECT 1
3040    FROM   JAI_AR_SUP_LINES A,
3041           JAI_AR_SUP_HDRS_ALL B,
3042           RA_CUSTOMER_TRX_ALL C,
3043           RA_CUST_TRX_TYPES_ALL D,
3044           JAI_AR_SUP_TAXES E
3045   WHERE  pr_new.CUSTOMER_TRX_ID = C.customer_trx_id
3046    AND   c.cust_trx_type_id = D.cust_trx_type_id
3047    AND   c.trx_number = TO_CHAR(b.SUPPLEMENTARY_NUM)
3048    AND   b.supp_inv_type = DECODE(D.TYPE,'INV','SI','CM','CR','DM','DB')
3049    AND   b.supp_inv_type = A.sup_inv_type
3050    AND   A.customer_trx_id = b.customer_trx_id
3051    AND   E.link_to_cust_trx_line_id = A.customer_trx_line_id
3052    AND   A.sup_inv_type = e.sup_inv_type;
3053 
3054   CURSOR SUPPLEMENT_TAX_LINES IS
3055   SELECT DISTINCT A.tax_line_no,
3056          A.new_uom,
3057          A.new_tax_id,
3058          A.new_rate,
3059          A.new_qty_rate,
3060          (NVL(A.new_base_tax_amt,0) - NVL(A.old_base_tax_amt,0)) BASE_TAX_AMT,
3061          A.diff_amt,
3062          A.diff_amt FUNC_TAX_AMT,
3063          t.tax_type,
3064          t.stform_type
3065   FROM   JAI_AR_SUP_TAXES A,
3066          JAI_AR_SUP_LINES  b,
3067          ra_customer_trx_all C,
3068          ra_cust_trx_types_all D,
3069          JAI_CMN_TAXES_ALL t
3070   WHERE  A.link_to_cust_trx_line_id = b.customer_trx_line_id
3071   AND    A.sup_inv_type = b.sup_inv_type
3072   AND    b.customer_Trx_line_id = TO_NUMBER(pr_new.interface_line_attribute3)
3073   AND    pr_new.customer_trx_id = c.customer_trx_id
3074   AND    c.cust_trx_type_id = D.cust_trx_type_id
3075   AND    b.sup_inv_type = DECODE(D.TYPE,'DM','DB','CM','CR','INV','SI')
3076   AND    A.new_tax_id = t.tax_id
3077   ORDER BY A.tax_line_no;
3078 
3079   CURSOR SUPPLEMENT_TAX_LINES_CNSLDT IS
3080   SELECT A.new_uom,
3081          A.new_tax_id,
3082          A.new_rate,
3083          A.new_qty_rate,
3084          SUM(NVL(A.new_base_Tax_amt,0) - NVL(A.old_base_tax_amt,0)) BASE_TAX_AMT,
3085          SUM(A.diff_amt) DIFF_AMT,
3086          SUM(A.diff_amt) FUNC_TAX_AMT,
3087          t.tax_type,
3088          t.stform_type
3089   FROM   JAI_AR_SUP_TAXES A,
3090          JAI_AR_SUP_LINES b,
3091          JAI_AR_SUP_HDRS_ALL c,
3092          ra_customer_trx_all D,
3093          ra_cust_trx_types_all e,
3094            JAI_CMN_TAXES_ALL t
3095   WHERE  pr_new.customer_trx_id = D.customer_trx_id
3096   AND    D.trx_number = TO_CHAR(c.SUPPLEMENTARY_NUM)
3097   AND    c.customer_trx_id = b.customer_trx_id
3098   AND    b.customer_trx_line_id = A.link_to_cust_trx_line_id
3099   AND    b.sup_inv_type = A.sup_inv_type
3100   AND    b.description =  pr_new.description
3101   AND    c.supp_inv_type = b.sup_inv_type
3102   AND    e.cust_trx_type_id = D.cust_trx_type_id
3103   AND    c.supp_inv_type =  DECODE(e.TYPE,'DM','DB','CM','CR','INV','SI')
3104   and    t.tax_type <> lc_modvat_tax
3105   AND    A.new_tax_id = t.tax_id
3106   GROUP BY b.inventory_item_id,
3107            A.new_tax_id,
3108            A.new_uom,
3109            A.new_qty_rate,
3110            A.new_rate,
3111            t.tax_type,
3112            t.stform_type ;
3113 
3114   CURSOR GET_HEADER_DETAILS IS
3115   SELECT bill_to_customer_id,
3116          bill_to_site_use_id,
3117          trx_number,
3118          batch_source_id
3119     FROM RA_CUSTOMER_TRX_ALL
3120    WHERE customer_trx_id = pr_new.customer_trx_id;
3121 
3122   CURSOR GET_SITE(P_TRX_NUMBER VARCHAR2) IS
3123   SELECT customer_site
3124     FROM JAI_AR_SUP_HDRS_ALL
3125   WHERE TO_CHAR(supplementary_num) = p_trx_number;
3126 
3127   CURSOR SUPPLEMENT_LINES_INFO IS
3128   SELECT DISTINCT A.tax_category_id,
3129          (NVL(A.excise_diff_amt,0)+NVL(A.other_diff_amt,0)) TAX_AMT,
3130            NVL(A.excise_diff_amt,0) excise_diff,
3131          A.new_assessable_value
3132   FROM   JAI_AR_SUP_LINES A,
3133          ra_customer_trx_all b,
3134          ra_cust_trx_types_all c
3135   WHERE  B.CUST_TRX_TYPE_ID =  C.CUST_TRX_TYPE_ID
3136   AND    A.SUP_INV_TYPE = DECODE(C.TYPE,'DM','DB','CM','CR','INV','SI')
3137   AND    A.customer_trx_line_id = TO_NUMBER(pr_new.interface_line_attribute3)
3138   AND    pr_new.customer_trx_id = b.customer_trx_id;
3139 
3140   CURSOR SUPPLEMENT_LINES_INFO_CNSLDT IS
3141   SELECT SUM(NVL(A.excise_diff_amt,0))+SUM(NVL(A.other_diff_amt,0)) TAX_AMT,
3142            NVL(A.excise_diff_amt,0) excise_diff,
3143          A.new_assessable_value
3144   FROM   JAI_AR_SUP_LINES A,
3145          JAI_AR_SUP_HDRS_ALL b,
3146          ra_customer_trx_all c,
3147          ra_cust_trx_types_all e
3148   WHERE  pr_new.customer_trx_id = c.customer_trx_id
3149   AND    c.trx_number = TO_CHAR( b.SUPPLEMENTARY_NUM)
3150   AND    B.customer_trx_id = A.customer_trx_id
3151   AND    C.cust_trx_type_id = e.cust_trx_type_id
3152   AND    b.supp_inv_type = DECODE(e.TYPE,'DM','DB','CM','CR','INV','SI')
3153   AND    A.description = pr_new.description
3154   AND    A.sup_inv_type = b.supp_inv_type
3155   GROUP BY A.inventory_item_id ,
3156            A.new_assessable_value,
3157            b.supp_inv_type,
3158      NVL(A.excise_diff_amt,0);
3159 
3160   CURSOR SUPPLEMENT_LINES_INFO_Tax_catg IS
3161   SELECT A.tax_category_id
3162   FROM   JAI_AR_SUP_LINES A,
3163          JAI_AR_SUP_HDRS_ALL b,
3164          ra_customer_trx_all c,
3165          ra_cust_trx_types_all D
3166   WHERE  pr_new.customer_trx_id = c.customer_trx_id
3167   AND    c.trx_number =TO_CHAR( b.SUPPLEMENTARY_NUM)
3168   AND    B.customer_trx_id = A.customer_trx_id
3169   AND    A.sup_inv_type = b.supp_inv_type
3170   AND    C.cust_trx_type_id = D.cust_trx_type_id
3171   AND    b.supp_inv_type = DECODE(D.TYPE,'DM','DB','CM','CR','INV','SI');
3172 
3173   CURSOR c_jai_ar_trx_lines(pn_customer_trx_id NUMBER,pn_customer_trx_line_id NUMBER) IS
3174   SELECT *
3175   FROM
3176         JAI_AR_TRX_LINES
3177   WHERE customer_trx_line_id = pn_customer_trx_line_id
3178   AND   customer_trx_id      = pn_customer_trx_id;
3179 
3180   cursor c_get_rec(cn_customer_trx_id number)
3181   is
3182   select *
3183   from jai_ar_trxs
3184   where customer_trx_id = cn_customer_trx_id;
3185 
3186   t_rec_new jai_ar_trxs%rowtype;
3187   t_rec_old jai_ar_trxs%rowtype;
3188 
3189   CURSOR register_code_cur(p_org_id IN NUMBER,  p_loc_id IN NUMBER, p_batch_source_id IN NUMBER)  IS
3190   SELECT register_code
3191   FROM   JAI_OM_OE_BOND_REG_HDRS
3192   WHERE  organization_id = p_org_id
3193   AND    location_id     = p_loc_id
3194   AND    register_id IN (SELECT register_id FROM   JAI_OM_OE_BOND_REG_DTLS
3195   WHERE  order_type_id = p_batch_source_id AND order_flag ='N');
3196 
3197   CURSOR pref_cur(p_organization_id NUMBER, p_location_id NUMBER)IS
3198   SELECT pref_rg23a, pref_rg23c, pref_pla
3199   FROM JAI_CMN_INVENTORY_ORGS
3200   WHERE organization_id = p_organization_id
3201   AND location_id = p_location_id ;
3202 
3203   CURSOR rg_bal_cur(p_organization_id NUMBER, p_location_id NUMBER)IS
3204   SELECT NVL(rg23a_balance,0) rg23a_balance ,NVL(rg23c_balance,0) rg23c_balance,NVL(pla_balance,0) pla_balance
3205   FROM JAI_CMN_RG_BALANCES
3206   WHERE organization_id = p_organization_id
3207   AND location_id = p_location_id ;
3208 
3209   CURSOR ssi_unit_flag_cur(p_organization_id IN  NUMBER, p_location_id IN NUMBER) IS
3210   SELECT ssi_unit_flag
3211   FROM   JAI_CMN_INVENTORY_ORGS
3212   WHERE  organization_id = p_organization_id AND
3213   location_id = p_location_id;
3214 
3215   CURSOR Register_Code_Meaning_Cur(p_register_code IN VARCHAR2, cp_lookup_type So_lookups.lookup_type%TYPE ) IS
3216   SELECT meaning
3217   FROM   So_lookups
3218   WHERE  lookup_code = p_register_code
3219   AND    lookup_type = cp_lookup_type;
3220 
3221   CURSOR fin_year_cur(p_org_id IN NUMBER) IS
3222   SELECT MAX(A.fin_year)
3223   FROM   JAI_CMN_FIN_YEARS A
3224   WHERE  organization_id = p_org_id AND fin_active_flag = 'Y';
3225 
3226   CURSOR Batch_Source_Name_Cur(p_batch_source_id IN NUMBER) IS
3227   SELECT name
3228   FROM   Ra_Batch_Sources_All
3229   WHERE  batch_source_id = p_batch_source_id
3230   AND    NVL(org_id,0)   = NVL(pr_new.org_id,0);
3231 
3232   CURSOR Def_Excise_Invoice_Cur(p_organization_id IN NUMBER, p_location_id IN NUMBER, p_fin_year IN NUMBER,
3233                                   p_batch_name IN VARCHAR2, p_register_code IN VARCHAR2) IS
3234   SELECT start_number, end_number, jump_by, prefix
3235   FROM   JAI_CMN_RG_EXC_INV_NOS
3236   WHERE  organization_id               = p_organization_id
3237   AND    location_id                   = p_location_id
3238   AND    fin_year                      = p_fin_year
3239   AND    transaction_type               = 'I'
3240   AND    order_invoice_type             = p_batch_name
3241   AND    register_code                 = p_register_code ;
3242 
3243   CURSOR ec_code_cur(p_organization_id IN NUMBER, p_location_id IN NUMBER) IS
3244   SELECT A.Organization_Id, A.Location_Id
3245   FROM   JAI_CMN_INVENTORY_ORGS A
3246   WHERE  A.Ec_Code IN (SELECT B.Ec_Code
3247                          FROM   JAI_CMN_INVENTORY_ORGS B
3248                          WHERE  B.Organization_Id = p_organization_id
3249                          AND    B.Location_Id     = p_location_id);
3250 
3251   CURSOR site_cur IS
3252   SELECT SHIP_TO_CUSTOMER_ID,SHIP_TO_SITE_USE_ID
3253   FROM   ra_customer_trx_all
3254   WHERE  customer_trx_id = pr_new.customer_trx_id
3255   AND    org_id = pr_new.org_id;
3256 
3257   t_jai_line_rec_old    JAI_AR_TRX_LINES%ROWTYPE;
3258   t_jai_line_rec_new    JAI_AR_TRX_LINES%ROWTYPE;
3259 
3260   --Variables
3261   v_exchange_rate               NUMBER;
3262   x                             NUMBER;
3263   v_exist_flag                  NUMBER;
3264   v_organization_id             NUMBER;
3265   v_location_id                 NUMBER;
3266   v_created_from                VARCHAR2(30);
3267   v_trx_number                  VARCHAR2(20);
3268   v_batch_source_id             NUMBER ;
3269   v_books_id                    NUMBER;
3270   v_salesrep_id                 NUMBER;
3271   c_from_currency_code          VARCHAR2(15);
3272   c_conversion_type             VARCHAR2(30);
3273   c_conversion_date             DATE;
3274   c_conversion_rate             NUMBER;
3275 
3276   v_tax_line_no                 NUMBER;
3277   v_bill_to_customer_id         NUMBER;
3278   v_bill_to_site_use_id         NUMBER;
3279   v_tax_amount                  NUMBER;
3280   v_excise_diff                 NUMBER;
3281   v_assessable_value            NUMBER;
3282   v_tax_category_id             NUMBER;
3283   v_service_type                VARCHAR2(30);
3284   v_line_amount                 NUMBER;
3285   v_last_update_date            DATE; --   := pr_new.last_update_date;
3286   v_last_updated_by             NUMBER; -- := pr_new.last_updated_by;
3287   v_creation_date               DATE; --   := pr_new.creation_date;
3288   v_created_by                  NUMBER; -- := pr_new.created_by;
3289   v_last_update_login           NUMBER; -- := pr_new.last_update_login;
3290   v_header_id                   NUMBER;
3291   lv_action                     VARCHAR2(20)  ;
3292   lv_return_message             VARCHAR2(2000);
3293   lv_return_code                VARCHAR2(100) ;
3294   le_error                      EXCEPTION     ;
3295   v_once_completed_flag         VARCHAR2(1);
3296   v_reg_code                    VARCHAR2(30);
3297   v_pref_rg23a                  NUMBER;
3298   v_pref_rg23c                  NUMBER;
3299   v_pref_pla                    NUMBER;
3300   v_rg23a_balance               NUMBER;
3301   v_rg23c_balance               NUMBER;
3302   v_pla_balance                 NUMBER;
3303   v_ssi_unit_flag               VARCHAR2(1);
3304   v_reg_type                    VARCHAR2(10);
3305   v_raise_error_flag            VARCHAR2(1);
3306   v_meaning                               VARCHAR2(80);
3307   v_fin_year                      NUMBER;
3308   v_order_invoice_type            VARCHAR2(50);
3309   v_start_number                  NUMBER;
3310   v_end_number                    NUMBER;
3311   v_jump_by                       NUMBER;
3312   v_prefix                                VARCHAR2(50);
3313   v_exc_invoice_no                        VARCHAR2(200);
3314   v_ship_id                       NUMBER;
3315   v_ship_site_id          NUMBER;
3316   v_part_i_register_id    NUMBER ;
3317   v_source_name           VARCHAR2(100);
3318   v_category_name         VARCHAR2(100);
3319   v_rg23_part_ii_no               NUMBER;
3320   v_rg23_part_i_no        NUMBER;
3321   v_pla_register_no               NUMBER;
3322   ln_legal_entity_id              number;
3323   l_api_name CONSTANT VARCHAR2(30) := 'PROCESS_SUPPLEMENT()';
3324 
3325 --
3326 begin
3327 
3328     v_exist_flag                  := 0;
3329     v_header_id                   := pr_new.customer_trx_id;
3330     v_customer_trx_line_id        := pr_new.customer_trx_line_id;
3331     v_last_update_date            := pr_new.last_update_date;
3332     v_last_updated_by             := pr_new.last_updated_by;
3333     v_creation_date               := pr_new.creation_date;
3334     v_created_by                  := pr_new.created_by;
3335     v_last_update_login           := pr_new.last_update_login;
3336     ln_legal_entity_id            := get_legal_entity_id(v_header_id) ;
3337     v_tax_amount                  := 0;
3338     v_excise_diff                 := 0;
3339     v_assessable_value            := 0;
3340     v_line_amount                 := nvl(pr_new.quantity_invoiced * pr_new.unit_selling_price, nvl(pr_new.extended_amount,0));
3341     v_once_completed_flag         := pv_once_complete_flag;
3342     v_source_name                 := 'Receivables India';
3343     v_category_name               := jai_constants.je_category_rg_entry ;
3344 
3345 
3346 
3347     OPEN  get_exchange_rate;
3348     FETCH get_exchange_rate INTO v_exchange_rate;
3349     CLOSE get_exchange_rate;
3350 
3351     --FND_FILE.PUT_LINE(FND_FILE.LOG, ' Exchange rate '|| v_exchange_rate);
3352     if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
3353       FND_LOG.STRING(G_LEVEL_PROCEDURE,
3354                      G_MODULE_NAME || l_api_name || '.IN',
3355                      G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
3356                      ||' Exchange rate ='||v_exchange_rate
3357                      );
3358     end if;
3359 
3360     OPEN   duplicate_hdr_cur;
3361     FETCH  duplicate_hdr_cur INTO x;
3362     CLOSE  duplicate_hdr_cur;
3363 
3364     OPEN   created_from_cur;
3365     FETCH  created_from_cur INTO  v_created_from          ,
3366                                   v_trx_number            ,
3367                                   v_batch_source_id       ,
3368                                   v_books_id              ,
3369                                   v_salesrep_id           ,
3370                                   c_from_currency_code    ,
3371                                   c_conversion_type       ,
3372                                   c_conversion_date       ,
3373                                   c_conversion_rate ;
3374     CLOSE  created_from_cur;
3375 
3376 
3377     IF pr_new.interface_line_attribute4 IS NULL THEN
3378       --FND_FILE.PUT_LINE(FND_FILE.LOG, ' Interface line att4 is null');
3379 
3380       if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
3381         FND_LOG.STRING(G_LEVEL_PROCEDURE,
3382                        G_MODULE_NAME || l_api_name || '.IN',
3383                        G_PKG_NAME || ': ' || l_api_name || '().'||' Interface line att4 is null '
3384                        );
3385       end if;
3386       OPEN supplement_lines_check_cnsldt;
3387       FETCH supplement_lines_check_cnsldt INTO v_exist_flag;
3388       CLOSE supplement_lines_check_cnsldt;
3389 
3390 
3391     ELSIF pr_new.interface_line_attribute4 IS NOT NULL THEN
3392       --FND_FILE.PUT_LINE(FND_FILE.LOG, ' Interface line att4 is not null');
3393       if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
3394         FND_LOG.STRING(G_LEVEL_PROCEDURE,
3395                        G_MODULE_NAME || l_api_name || '.IN',
3396                        G_PKG_NAME || ': ' || l_api_name || '().'||' Interface line att4 is not null '
3397                        );
3398       end if;
3399       OPEN  supplement_lines_check;
3400       FETCH supplement_lines_check INTO v_exist_flag;
3401       CLOSE supplement_lines_check;
3402     END IF;
3403 
3404     /*FND_FILE.PUT_LINE(FND_FILE.LOG,
3405          ' Supplement lines check exists flag'|| v_exist_flag);*/
3406 
3407     if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
3408       FND_LOG.STRING(G_LEVEL_PROCEDURE,
3409                      G_MODULE_NAME || l_api_name || '.IN',
3410                      G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
3411                      ||' Supplement lines check exists flag ='||v_exist_flag
3412                      );
3413     end if;
3414 
3415 
3416 
3417     IF NVL(v_exist_flag,0) <> 1 THEN
3418       return;
3419     END IF;
3420 
3421     IF NVL(x,0) <> 1 THEN
3422       IF pr_new.interface_line_attribute4 IS NULL THEN
3423         OPEN ja_in_ra_customer_trx_infocnsl;
3424         FETCH ja_in_ra_customer_trx_infocnsl INTO v_organization_id, v_location_id;
3425         CLOSE ja_in_ra_customer_trx_infocnsl;
3426       ELSIF pr_new.interface_line_attribute4 IS NOT NULL THEN
3427         OPEN  ja_in_ra_customer_trx_info;
3428         FETCH ja_in_ra_customer_trx_info INTO v_organization_id, v_location_id;
3429         CLOSE ja_in_ra_customer_trx_info;
3430       END IF;
3431 
3432       INSERT INTO JAI_AR_TRXS (                               -- supplement
3433                                            customer_trx_id         ,
3434                                            organization_id         ,
3435                                            location_id             ,
3436                                            trx_number              ,
3437                                            update_rg_flag          ,
3438                                            update_rg23d_flag       ,
3439                                            once_completed_flag     ,
3440                                            batch_source_id         ,
3441                                            set_of_books_id         ,
3442                                            primary_salesrep_id     ,
3443                                            invoice_currency_code   ,
3444                                            exchange_rate_type      ,
3445                                            exchange_date           ,
3446                                            exchange_rate           ,
3447                                            creation_date           ,
3448                                            created_by              ,
3449                                            last_update_date        ,
3450                                            last_updated_by         ,
3451                                            last_update_login       ,
3452                                            legal_entity_id         ,
3453                                            created_from --zhiwei
3454                                         )
3455                                 VALUES  (
3456                                            v_header_id             ,
3457                                            v_organization_id       ,
3458                                            v_location_id           ,
3459                                            v_trx_number            ,
3460                                            'Y'                     ,
3461                                            'Y'                     ,
3462                                            'N'                     ,
3463                                            v_batch_source_id       ,
3464                                            v_books_id              ,
3465                                            v_salesrep_id           ,
3466                                            c_from_currency_code    ,
3467                                            c_conversion_type       ,
3468                                            c_conversion_date       ,
3469                                            c_conversion_rate       ,
3470                                            v_creation_date         ,
3471                                            v_created_by            ,
3472                                            v_last_update_date      ,
3473                                            v_last_updated_by       ,
3474                                            v_last_update_login     ,
3475                                            ln_legal_entity_id      ,
3476                                            v_created_from --zhiwei
3477                                         );
3478       --FND_FILE.PUT_LINE(FND_FILE.LOG, ' Inside x <> 1: After insert into JAI_AR_TRXS');
3479       if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
3480         FND_LOG.STRING(G_LEVEL_PROCEDURE,
3481                        G_MODULE_NAME || l_api_name || '.IN',
3482                        G_PKG_NAME || ': ' || l_api_name || '().'||' Inside x <> 1: After insert into JAI_AR_TRXS '
3483                        );
3484       end if;
3485     END IF;
3486 
3487     IF pr_new.interface_line_attribute4 IS NULL THEN
3488       OPEN  supplement_tax_lines_check_cns;
3489       FETCH supplement_tax_lines_check_cns INTO v_exist_flag;
3490       CLOSE supplement_tax_lines_check_cns;
3491 
3492     ELSIF pr_new.INTERFACE_LINE_ATTRIBUTE4 IS NOT NULL THEN
3493      OPEN  supplement_tax_lines_check;
3494      FETCH supplement_tax_lines_check INTO v_exist_flag;
3495      CLOSE supplement_tax_lines_check;
3496     END IF;
3497 
3498 
3499     IF NVL(v_exist_flag,0) <> 1 THEN
3500       return;
3501     END IF;
3502 
3503     v_tax_line_no := 0;
3504 
3505     /**************** Start of If Loop pr_new.interface_line_attribute1 IS NULL  **********/
3506     IF pr_new.interface_line_attribute4 IS NULL THEN
3507 
3508       FOR tax_rec IN supplement_tax_lines_cnsldt
3509       LOOP
3510         v_tax_line_no := v_tax_line_no+1;
3511 
3512 
3513             INSERT INTO JAI_AR_TRX_TAX_LINES(
3514                                                           tax_line_no                                     ,
3515                                                           customer_trx_line_id                            ,
3516                                                           link_to_cust_trx_line_id                        ,
3517                                                           precedence_1                                    ,
3518                                                           precedence_2                                    ,
3519                                                           precedence_3                                    ,
3520                                                           precedence_4                                    ,
3521                                                           precedence_5                                    ,
3522                                                           precedence_6                                    ,
3523                                                           precedence_7                                    ,
3524                                                           precedence_8                                    ,
3525                                                           precedence_9                                    ,
3526                                                           precedence_10                                    ,
3527                                                           tax_id                                          ,
3528                                                           tax_rate                                        ,
3529                                                           qty_rate                                        ,
3530                                                           uom                                             ,
3531                                                           tax_amount                                      ,
3532                                                           func_tax_amount                                 ,
3533                                                           base_tax_amount                                 ,
3534                                                           creation_date                                   ,
3535                                                           created_by                                      ,
3536                                                           last_update_date                                ,
3537                                                           last_updated_by                                 ,
3538                                                           last_update_login
3539                                                         )
3540                                                   VALUES(
3541                                                           v_tax_line_no                                   ,
3542                                                           ra_customer_trx_lines_s.NEXTVAL                 ,
3543                                                           v_customer_trx_line_id                          ,
3544                                                           NULL                                            ,
3545                                                           NULL                                            ,
3546                                                           NULL                                            ,
3547                                                           NULL                                            ,
3548                                                           NULL                                            ,
3549                                                           NULL                                            ,
3550                                                           NULL                                            ,
3551                                                           NULL                                            ,
3552                                                           NULL                                            ,
3553                                                           NULL                                            ,
3554                                                           tax_rec.new_tax_id                              ,
3555                                                           tax_rec.new_rate                                ,
3556                                                           tax_rec.new_qty_rate                            ,
3557                                                           tax_rec.new_uom                                 ,
3558                                                           tax_rec.diff_amt                                ,
3559                                                           tax_rec.func_tax_amt*nvl(v_exchange_rate,1)     ,
3560                                                           tax_rec.base_tax_amt                            ,
3561                                                           v_creation_date                                 ,
3562                                                           v_created_by                                    ,
3563                                                           v_last_update_date                              ,
3564                                                           v_last_updated_by                               ,
3565                                                           v_last_update_login
3566                                                         );
3567 
3568             OPEN get_header_details;
3569             FETCH get_header_details INTO v_bill_to_customer_id       ,
3570                                           v_bill_to_site_use_id       , /*Bug 8371741*/
3571                                           v_trx_number                ,
3572                                           v_batch_source_id;
3573 
3574             /*FND_FILE.PUT_LINE(FND_FILE.LOG, ' Bill to cust id:  '|| v_bill_to_customer_id);
3575             FND_FILE.PUT_LINE(FND_FILE.LOG, ' Bill to site use: '|| v_bill_to_site_use_id);
3576             FND_FILE.PUT_LINE(FND_FILE.LOG, ' Trx number: '|| v_trx_number);
3577             FND_FILE.PUT_LINE(FND_FILE.LOG, ' Batch source id: '|| v_batch_source_id);*/
3578 
3579             if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
3580               FND_LOG.STRING(G_LEVEL_PROCEDURE,
3581                              G_MODULE_NAME || l_api_name || '.IN',
3582                              G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
3583                              ||' Bill to cust id ='||v_bill_to_customer_id
3584                              ||' Bill to site id ='||v_bill_to_site_use_id
3585                              ||' Trx number ='||v_trx_number
3586                              ||' Batch source id ='||v_batch_source_id
3587                              );
3588             end if;
3589 
3590             CLOSE get_header_details;
3591 
3592             OPEN  get_site(v_trx_number);
3593             FETCH get_site INTO v_bill_to_site_use_id;
3594             CLOSE get_site;
3595       END LOOP;
3596 
3597     ELSIF pr_new.interface_line_attribute4 IS NOT NULL THEN
3598 
3599       FOR Tax_Rec IN SUPPLEMENT_TAX_LINES
3600       LOOP
3601         v_tax_line_no :=  v_tax_line_no+1;
3602 
3603         INSERT INTO JAI_AR_TRX_TAX_LINES(
3604                                                 tax_line_no                                     ,
3605                                                 customer_trx_line_id                            ,
3606                                                 link_to_cust_trx_line_id                        ,
3607                                                 precedence_1                                    ,
3608                                                 precedence_2                                    ,
3609                                                 precedence_3                                    ,
3610                                                 precedence_4                                    ,
3611                                                 precedence_5                                    ,
3612                                                 precedence_6                                    ,
3613                                                 precedence_7                                    ,
3614                                                 precedence_8                                    ,
3615                                                 precedence_9                                    ,
3616                                                 precedence_10                                    ,
3617                                                 tax_id                                          ,
3618                                                 tax_rate                                        ,
3619                                                 qty_rate                                        ,
3620                                                 uom                                             ,
3621                                                 tax_amount                                      ,
3622                                                 func_tax_amount                                 ,
3623                                                 base_tax_amount                                 ,
3624                                                 creation_date                                   ,
3625                                                 created_by                                      ,
3626                                                 last_update_date                                ,
3627                                                 last_updated_by                                 ,
3628                                                 last_update_login
3629                                              )
3630                                       VALUES (
3631                                                 v_tax_line_no                                   ,
3632                                                 ra_customer_trx_lines_s.nextval                 ,
3633                                                 v_customer_trx_line_id                          ,
3634                                                 NULL                                            ,
3635                                                 NULL                                            ,
3636                                                 NULL                                            ,
3637                                                 NULL                                            ,
3638                                                 NULL                                            ,
3639                                                 NULL                                            ,
3640                                                 NULL                                            ,
3641                                                 NULL                                            ,
3642                                                 NULL                                            ,
3643                                                 NULL                                            ,
3644                                                 tax_rec.new_tax_id                              ,
3645                                                 tax_rec.new_rate                                ,
3646                                                 tax_rec.new_qty_rate                            ,
3647                                                 tax_rec.new_uom                                 ,
3648                                                 tax_rec.diff_amt                                ,
3649                                                 tax_rec.func_tax_amt*nvl(v_exchange_rate,1)     ,
3650                                                 tax_rec.base_tax_amt                            ,
3651                                                 v_creation_date                                 ,
3652                                                 v_created_by                                    ,
3653                                                 v_last_update_date                              ,
3654                                                 v_last_updated_by                               ,
3655                                                 v_last_update_login
3656                                              );
3657 
3658         OPEN get_header_details;
3659         FETCH get_header_details INTO v_bill_to_customer_id,
3660                                     v_bill_to_site_use_id,
3661                                     v_trx_number,
3662                                     v_batch_source_id;
3663         CLOSE get_header_details;
3664         OPEN get_site(v_trx_number);
3665         FETCH get_site INTO v_bill_to_site_use_id;
3666         CLOSE get_site;
3667       END LOOP;
3668 
3669     END IF;
3670 
3671     /**************** End of If Loop pr_new.interface_line_attribute1 IS NULL  **********/
3672 
3673     IF pr_new.interface_line_attribute4 IS NULL THEN
3674 
3675       --FND_FILE.PUT_LINE(FND_FILE.LOG,' Int. line att4 is null');
3676       if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
3677         FND_LOG.STRING(G_LEVEL_PROCEDURE,
3678                        G_MODULE_NAME || l_api_name || '.IN',
3679                        G_PKG_NAME || ': ' || l_api_name || '().'||' Int. line att4 is null '
3680                        );
3681       end if;
3682 
3683       OPEN  supplement_lines_info_cnsldt;
3684       FETCH supplement_lines_info_cnsldt INTO  v_tax_amount,
3685                                              v_excise_diff,
3686                                              v_assessable_value;
3687       CLOSE supplement_lines_info_cnsldt;
3688 
3689       /*FND_FILE.PUT_LINE(FND_FILE.LOG,' Tax amount: '|| v_tax_amount);
3690       FND_FILE.PUT_LINE(FND_FILE.LOG,' Excise diff: '|| v_excise_diff);
3691       FND_FILE.PUT_LINE(FND_FILE.LOG,' Assessable Value: '||v_assessable_value);*/
3692 
3693       if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
3694           FND_LOG.STRING(G_LEVEL_PROCEDURE,
3695                          G_MODULE_NAME || l_api_name || '.IN',
3696                          G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
3697                          ||' Tax amount ='||v_tax_amount
3698                          ||' Excise diff ='||v_excise_diff
3699                          ||' Assessable Value ='||v_assessable_value
3700                          );
3701       end if;
3702 
3703       OPEN  supplement_lines_info_tax_catg;
3704       FETCH supplement_lines_info_tax_catg INTO v_tax_category_id;
3705       CLOSE supplement_lines_info_tax_catg;
3706 
3707       --FND_FILE.PUT_LINE(FND_FILE.LOG,' Tax category: '|| v_tax_category_id);
3708       if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
3709           FND_LOG.STRING(G_LEVEL_PROCEDURE,
3710                          G_MODULE_NAME || l_api_name || '.IN',
3711                          G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
3712                          ||' Tax category ='||v_tax_category_id
3713                          );
3714       end if;
3715 
3716     ELSIF pr_new.INTERFACE_LINE_ATTRIBUTE4 IS NOT NULL  THEN
3717       --FND_FILE.PUT_LINE(FND_FILE.LOG,' Int. line att4 is not  null');
3718       if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
3719         FND_LOG.STRING(G_LEVEL_PROCEDURE,
3720                        G_MODULE_NAME || l_api_name || '.IN',
3721                        G_PKG_NAME || ': ' || l_api_name || '().'||' Int. line att4 is not  null'
3722                        );
3723       end if;
3724       OPEN  supplement_lines_info;
3725       FETCH supplement_lines_info INTO v_tax_category_id,
3726                                      v_tax_amount     ,
3727                                      v_excise_diff    ,
3728                                      v_assessable_value;
3729       CLOSE supplement_lines_info;
3730       --FND_FILE.PUT_LINE(FND_FILE.LOG,' Tax category: '|| v_tax_category_id);
3731       --FND_FILE.PUT_LINE(FND_FILE.LOG,' Tax amount: '|| v_tax_amount);
3732       --FND_FILE.PUT_LINE(FND_FILE.LOG,' Excise diff: '|| v_excise_diff);
3733       --FND_FILE.PUT_LINE(FND_FILE.LOG,' Assessable Value: '||v_assessable_value);
3734       if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
3735           FND_LOG.STRING(G_LEVEL_PROCEDURE,
3736                          G_MODULE_NAME || l_api_name || '.IN',
3737                          G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
3738                          ||' Tax category ='    ||v_tax_category_id
3739                          ||' Tax amount ='      ||v_tax_amount
3740                          ||' Excise diff ='     ||v_excise_diff
3741                          ||' Assessable Value ='||v_assessable_value
3742                          );
3743       end if;
3744 
3745     END IF;
3746 
3747 
3748 
3749     OPEN c_get_address_details(v_header_id);
3750     FETCH c_get_address_details into r_add;
3751     CLOSE c_get_address_details;
3752 
3753     v_service_type:=get_service_type( NVL(r_add.SHIP_TO_CUSTOMER_ID ,r_add.BILL_TO_CUSTOMER_ID) ,
3754                           NVL(r_add.SHIP_TO_SITE_USE_ID, r_add.BILL_TO_SITE_USE_ID),'C');
3755 
3756     INSERT INTO JAI_AR_TRX_LINES (
3757                                               customer_trx_line_id                      ,
3758                                               line_number                               ,
3759                                               customer_trx_id                           ,
3760                                               description                               ,
3761                                               inventory_item_id                         ,
3762                                               unit_code                                 ,
3763                                               quantity                                  ,
3764                                               tax_category_id                           ,
3765                                               auto_invoice_flag                         ,
3766                                               unit_selling_price                        ,
3767                                               line_amount                               ,
3768                                               tax_amount                                ,
3769                                               total_amount                              ,
3770                                               assessable_value                          ,
3771                                               creation_date                             ,
3772                                               created_by                                ,
3773                                               last_update_date                          ,
3774                                               last_updated_by                           ,
3775                                               last_update_login,
3776                                               service_type_code
3777                                            )
3778                                      VALUES(
3779                                               v_customer_trx_line_id                    ,
3780                                               pr_new.line_number                          ,
3781                                               v_header_id                               ,
3782                                               pr_new.description                          ,
3783                                               pr_new.inventory_item_id                    ,
3784                                               pr_new.uom_code                             ,
3785                                               NVL(pr_new.quantity_invoiced,0)             ,
3786                                               v_tax_category_id                         ,
3787                                               'Y'                                       ,
3788                                               NVL(pr_new.unit_selling_price,0)            ,
3789                                               v_line_amount                             ,
3790                                               v_tax_amount                              ,
3791                                               (v_line_amount + v_tax_amount)            ,
3792                                               v_assessable_value                        ,
3793                                               v_creation_date                           ,
3794                                               v_created_by                              ,
3795                                               v_last_update_date                        ,
3796                                               v_last_updated_by                         ,
3797                                               v_last_update_login,
3798                                               v_service_type
3799                                            );
3800     --Added by Qinglei for JAI Trigger Elimination 11-May-2012 begin
3801     OPEN c_jai_ar_trx_lines(v_header_id,v_customer_trx_line_id);
3802     FETCH c_jai_ar_trx_lines into t_jai_line_rec_new;
3803     CLOSE c_jai_ar_trx_lines;
3804 
3805     lv_action := jai_constants.INSERTING;
3806 
3807     JAI_AR_TAX_LINES_PKG.POPULATE_TAX_LINES_WRAPPER (
3808                 pr_old            =>  NULL         ,
3809                 pr_new            =>  t_jai_line_rec_new         ,
3810                 pv_action         =>  lv_action         ,
3811                 pv_return_code    =>  lv_return_code    ,
3812                 pv_return_message =>  lv_return_message
3813               );
3814     IF lv_return_code <> jai_constants.successful   then
3815         RAISE le_error;
3816     END IF;
3817     --Added by Qinglei for JAI Trigger Elimination 11-May-2012 end
3818 
3819     --Added by Zhiwei for JAI Trigger elimination begin
3820     ---------------------------------------------------------
3821     open c_get_rec(v_header_id);
3822     fetch c_get_rec into t_rec_old;
3823     close c_get_rec;
3824     ---------------------------------------------------------
3825     --Added by Zhiwei for JAI Trigger elimination end
3826 
3827 
3828     UPDATE  JAI_AR_TRXS
3829     SET
3830           line_amount             =  NVL(line_amount, 0 ) + NVL(v_line_amount,0),
3831           once_completed_flag     = NVL(v_once_completed_flag,'N')
3832     WHERE
3833           customer_trx_id = v_header_id;
3834 
3835     --Added by Zhiwei for JAI Trigger elimination begin
3836     ---------------------------------------------------------
3837     open c_get_rec(v_header_id);
3838     fetch c_get_rec into t_rec_new;
3839     close c_get_rec;
3840 
3841     if(t_rec_new.once_completed_flag = 'Y')then
3842 
3843          lv_action := jai_constants.updating ;
3844 
3845          JAI_AR_TRXS_PKG.UPDATE_EXCISE_REGISTERS(
3846                         pr_old            =>  t_rec_old         ,
3847                         pr_new            =>  t_rec_new         ,
3848                         pv_action         =>  lv_action         ,
3849                         pv_return_code    =>  lv_return_code    ,
3850                         pv_return_message =>  lv_return_message
3851                       );
3852 
3853           IF lv_return_code <> jai_constants.successful   then
3854              RAISE le_error;
3855           END IF;
3856 
3857     end if;
3858     ---------------------------------------------------------
3859     --Added by Zhiwei for JAI Trigger elimination end
3860 
3861 
3862 
3863     OPEN  register_code_cur(v_organization_id, v_location_id,v_batch_source_id);
3864     FETCH register_code_cur INTO v_reg_code;
3865     CLOSE register_code_cur;
3866 
3867     IF NVL(v_excise_diff ,0) > 0 THEN
3868 
3869       IF NVL(v_reg_code,'N') IN ('DOMESTIC_EXCISE','EXPORT_EXCISE') THEN
3870 
3871         --FND_FILE.PUT_LINE(FND_FILE.LOG,' Register code: '||v_reg_code);
3872         if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
3873             FND_LOG.STRING(G_LEVEL_PROCEDURE,
3874                            G_MODULE_NAME || l_api_name || '.IN',
3875                            G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
3876                            ||' Register code ='    ||v_reg_code
3877                            );
3878         end if;
3879 
3880         OPEN pref_cur(v_organization_id, v_location_id);
3881         FETCH pref_cur INTO  v_pref_rg23a, v_pref_rg23c, v_pref_pla;
3882         CLOSE pref_cur;
3883 
3884         /*FND_FILE.PUT_LINE(FND_FILE.LOG,' Pref RG23A: '|| v_pref_rg23a);
3885         FND_FILE.PUT_LINE(FND_FILE.LOG,' Pref RG23c: '||v_pref_rg23c);
3886         FND_FILE.PUT_LINE(FND_FILE.LOG,' Pref. PLA:  '||v_pref_pla);*/
3887 
3888         if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
3889             FND_LOG.STRING(G_LEVEL_PROCEDURE,
3890                            G_MODULE_NAME || l_api_name || '.IN',
3891                            G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
3892                            ||' Pref RG23A ='    ||v_pref_rg23a
3893                            ||' Pref RG23c ='    ||v_pref_rg23c
3894                            ||' Pref PLA ='      ||v_pref_pla
3895                            );
3896         end if;
3897 
3898         OPEN rg_bal_cur(v_organization_id, v_location_id);
3899         FETCH rg_bal_cur INTO v_rg23a_balance, v_rg23c_balance, v_pla_balance;
3900         CLOSE rg_bal_cur;
3901 
3902         /*FND_FILE.PUT_LINE(FND_FILE.LOG,' Balance: RG23A:'||v_rg23a_balance);
3903         FND_FILE.PUT_LINE(FND_FILE.LOG,' RG23c: '||v_rg23c_balance);
3904         FND_FILE.PUT_LINE(FND_FILE.LOG,' PLA: '|| v_pla_balance);*/
3905         if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
3906             FND_LOG.STRING(G_LEVEL_PROCEDURE,
3907                            G_MODULE_NAME || l_api_name || '.IN',
3908                            G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
3909                            ||' Balance RG23A ='    ||v_rg23a_balance
3910                            ||' Balance RG23c ='    ||v_rg23c_balance
3911                            ||' Balance PLA ='      ||v_pla_balance
3912                            );
3913         end if;
3914 
3915         OPEN  ssi_unit_flag_cur(v_organization_id, v_location_id);
3916         FETCH ssi_unit_flag_cur INTO v_ssi_unit_flag;
3917         CLOSE ssi_unit_flag_cur;
3918 
3919         --FND_FILE.PUT_LINE(FND_FILE.LOG,' SSI unit flag'||v_ssi_unit_flag);
3920         if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
3921             FND_LOG.STRING(G_LEVEL_PROCEDURE,
3922                            G_MODULE_NAME || l_api_name || '.IN',
3923                            G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
3924                            ||' SSI unit flag ='    ||v_ssi_unit_flag
3925                            );
3926         end if;
3927         IF v_pref_rg23a = 1 THEN                             --5
3928           IF v_rg23a_balance >= NVL(v_excise_diff,0) THEN         --6
3929             v_reg_type := 'RG23A';
3930           ELSIF v_pref_rg23c = 2 THEN                                     --6
3931 
3932             IF v_rg23c_balance >= NVL(v_excise_diff,0) THEN        --7
3933                v_reg_type  := 'RG23C';
3934             ELSIF v_pla_balance >= NVL(v_excise_diff,0) THEN  --7
3935                v_reg_type  := 'PLA';
3936             ELSIF NVL(v_ssi_unit_flag,'N') = 'Y' THEN                  --7
3937                v_reg_type  := 'PLA';
3938             ELSE                                                       --7
3939                v_raise_error_flag := 'Y';
3940             END IF;                                                    --7
3941 
3942           ELSIF v_pla_balance >= NVL(v_excise_diff,0) THEN             --6
3943              v_reg_type  := 'PLA';
3944           ELSIF NVL(v_ssi_unit_flag,'N') = 'Y' THEN                    --6
3945              v_reg_type  := 'PLA';
3946           ELSIF v_rg23c_balance >= NVL(v_excise_diff,0) THEN   --6
3947              v_reg_type  := 'RG23C';
3948           ELSE                                                 --6
3949             v_raise_error_flag := 'Y';
3950           END IF;                                                      --6
3951 
3952         ELSIF v_pref_rg23c = 1  THEN                                  --5
3953 
3954           IF v_rg23c_balance >= NVL(v_excise_diff,0) THEN --6
3955             v_reg_type := 'RG23C';
3956 
3957           ELSIF v_pref_rg23a = 2 THEN                                     --6
3958 
3959             IF v_rg23a_balance >= NVL(v_excise_diff,0) THEN        --7
3960               v_reg_type  := 'RG23A';
3961             ELSIF v_pla_balance >= NVL(v_excise_diff,0) THEN   --7
3962               v_reg_type  := 'PLA';
3963             ELSIF NVL(v_ssi_unit_flag,'N') = 'Y' THEN                  --7
3964               v_reg_type  := 'PLA';
3965             ELSE                                                       --7
3966               v_raise_error_flag := 'Y';
3967             END IF;                                                    --7
3968 
3969           ELSIF v_pla_balance >= NVL(v_excise_diff,0) THEN                --6
3970             v_reg_type  := 'PLA';
3971           ELSIF NVL(v_ssi_unit_flag,'N') = 'Y' THEN                       --6
3972             v_reg_type  := 'PLA';
3973           ELSIF v_rg23a_balance >= NVL(v_excise_diff,0) THEN      --6
3974             v_reg_type  := 'RG23A';
3975           ELSE                                                        --6
3976             v_raise_error_flag := 'Y';
3977           END IF;                                                 --6
3978 
3979         ELSIF  v_pla_balance >= NVL(v_excise_diff,0) THEN
3980           v_reg_type  := 'PLA';
3981         ELSIF NVL(v_ssi_unit_flag,'N') = 'Y' THEN                     --6
3982           v_reg_type  := 'PLA';
3983         ELSIF v_pref_rg23a = 2 THEN                                   --6
3984           IF v_rg23a_balance >= NVL(v_excise_diff,0) THEN        --7
3985             v_reg_type  := 'RG23A';
3986           ELSIF  v_rg23c_balance >= NVL(v_excise_diff,0) THEN     --7
3987             v_reg_type  := 'RG23C';
3988           ELSE
3989             v_raise_error_flag := 'Y';
3990           END IF;
3991 
3992         ELSIF v_rg23c_balance >= NVL(v_excise_diff,0) THEN    --7
3993           v_reg_type  := 'RG23C';
3994         ELSIF v_rg23a_balance >= NVL(v_excise_diff,0) THEN    --7
3995           v_reg_type  := 'RG23A';
3996         ELSE
3997           v_raise_error_flag := 'Y';
3998         END IF;                                                       --5
3999 
4000         IF NVL(v_raise_error_flag,'N') = 'Y' THEN
4001           /* RAISE_APPLICATION_ERROR(-20120, 'NONE OF the Register Have Balances Greater OR Equal TO the Excisable Amount ->' || TO_CHAR(v_excise_diff));
4002           */
4003           pv_return_code := jai_constants.expected_error ;
4004           pv_return_message :=  'NONE OF the Register Have Balances Greater OR Equal TO the Excisable Amount ->' || TO_CHAR(v_excise_diff) ; return ;
4005         END IF;
4006 
4007         OPEN  register_code_meaning_cur(v_reg_code, 'REGISTER_TYPE');
4008         FETCH register_code_meaning_cur INTO v_meaning;
4009         CLOSE register_code_meaning_cur;
4010 
4011         --FND_FILE.PUT_LINE(FND_FILE.LOG,' Meaning'|| v_meaning);
4012         if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
4013             FND_LOG.STRING(G_LEVEL_PROCEDURE,
4014                            G_MODULE_NAME || l_api_name || '.IN',
4015                            G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
4016                            ||' Meaning ='    ||v_meaning
4017                            );
4018         end if;
4019         OPEN   fin_year_cur(v_organization_id);
4020         FETCH  fin_year_cur INTO v_fin_year;
4021         CLOSE  fin_year_cur;
4022 
4023         --FND_FILE.PUT_LINE(FND_FILE.LOG,' Fin year '|| v_fin_year);
4024         if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
4025 
4026             FND_LOG.STRING(G_LEVEL_PROCEDURE,
4027                            G_MODULE_NAME || l_api_name || '.IN',
4028                            G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
4029                            ||' Fin year ='    ||v_fin_year
4030                            );
4031         end if;
4032 
4033         OPEN  Batch_Source_Name_Cur(v_batch_source_id);
4034         FETCH Batch_Source_Name_Cur INTO v_order_invoice_type;
4035         CLOSE Batch_Source_Name_Cur;
4036 
4037         --FND_FILE.PUT_LINE(FND_FILE.LOG,' Order invoice type: '|| v_order_invoice_type);
4038         if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
4039 
4040             FND_LOG.STRING(G_LEVEL_PROCEDURE,
4041                            G_MODULE_NAME || l_api_name || '.IN',
4042                            G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
4043                            ||' Order invoice type ='    ||v_order_invoice_type
4044                            );
4045         end if;
4046 
4047         OPEN   Def_Excise_Invoice_Cur(v_organization_id, v_location_id, v_fin_year, v_order_invoice_type, v_meaning);
4048         FETCH  Def_Excise_Invoice_Cur INTO v_start_number, v_end_number, v_jump_by, v_prefix;
4049         CLOSE  Def_Excise_Invoice_Cur;
4050 
4051         /*FND_FILE.PUT_LINE(FND_FILE.LOG,' Start number'|| v_start_number);
4052         FND_FILE.PUT_LINE(FND_FILE.LOG,' End number:'|| v_end_number);
4053         FND_FILE.PUT_LINE(FND_FILE.LOG,' jump by: '|| v_jump_by);
4054         FND_FILE.PUT_LINE(FND_FILE.LOG,' prefix : '|| v_prefix);*/
4055         if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
4056 
4057             FND_LOG.STRING(G_LEVEL_PROCEDURE,
4058                            G_MODULE_NAME || l_api_name || '.IN',
4059                            G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
4060                            ||' Start number =' ||v_start_number
4061                            ||' End number ='   ||v_end_number
4062                            ||' jump by ='      ||v_jump_by
4063                            ||' prefix ='       ||v_prefix
4064                            );
4065         end if;
4066 
4067         IF v_start_number IS NOT NULL THEN                            --2
4068           IF NVL(v_start_number,0) >= NVL(v_end_number,0) AND v_end_number IS NOT NULL THEN
4069                /*             RAISE_APPLICATION_ERROR(-20120, 'Excise Invoice NUMBER has been exhausted. ' ||
4070                ' Increase END NUMBER OR enter fresh START NUMBER AND END NUMBER.'); */
4071                pv_return_code := jai_constants.expected_error ;
4072                pv_return_message :=  'Excise Invoice NUMBER has been exhausted. ' ||' Increase END NUMBER OR enter fresh START NUMBER AND END NUMBER.' ;
4073                return ;
4074           END IF;
4075           v_exc_invoice_no := NVL(v_start_number,0);
4076           v_start_number := NVL(v_start_number,0) + NVL(v_jump_by,0);
4077           IF v_prefix IS NOT NULL THEN
4078             v_exc_invoice_no := v_prefix||'/'||v_exc_invoice_no;
4079           END IF;
4080         END IF;                                                               --2
4081 
4082         IF v_exc_invoice_no IS NOT NULL THEN
4083           IF v_start_number IS NOT NULL THEN
4084             FOR master_org_rec IN ec_code_cur(v_organization_id, v_location_id) LOOP
4085               UPDATE
4086                     JAI_CMN_RG_EXC_INV_NOS
4087               SET
4088                     start_number            = v_start_number,
4089                     last_update_date        = v_last_update_date,
4090                     last_updated_by         = v_last_updated_by,
4091                     last_update_login       = v_last_update_login
4092               WHERE
4093                     organization_id         = master_org_rec.organization_id AND
4094                     location_id             = master_org_rec.location_id     AND
4095                     fin_year                = v_fin_year                     AND
4096                     order_invoice_type      = v_order_invoice_type           AND
4097                     register_code           = v_meaning;
4098 
4099             END LOOP;
4100           END IF;
4101         END IF;
4102 
4103         --Added by Qinglei for JAI Trigger Elimination 11-May-2012 begin
4104         OPEN c_jai_ar_trx_lines(v_header_id,v_customer_trx_line_id);
4105         FETCH c_jai_ar_trx_lines INTO t_jai_line_rec_old;
4106         CLOSE c_jai_ar_trx_lines;
4107 
4108         UPDATE JAI_AR_TRX_LINES
4109         SET    payment_register             =  v_reg_type,
4110                excise_invoice_no            =  v_exc_invoice_no,
4111                excise_invoice_date          =  trunc(sysdate)
4112         WHERE
4113                customer_trx_line_id         = v_customer_trx_line_id AND
4114                inventory_item_id            = pr_new.inventory_item_id AND
4115                customer_trx_id              = v_header_id;
4116 
4117         OPEN c_jai_ar_trx_lines(v_header_id,v_customer_trx_line_id);
4118         FETCH c_jai_ar_trx_lines INTO t_jai_line_rec_new;
4119         CLOSE c_jai_ar_trx_lines;
4120 
4121         lv_action := jai_constants.UPDATING;
4122 
4123         IF ( ( ( t_jai_line_rec_new.AUTO_INVOICE_FLAG <> 'Y'    AND
4124                 t_jai_line_rec_old.AUTO_INVOICE_FLAG <> 'Y'
4125                )                                  AND
4126                 (t_jai_line_rec_new.Excise_Invoice_No IS NULL)  AND
4127                 (t_jai_line_rec_new.payment_Register  IS NULL)  AND
4128                 (t_jai_line_rec_new.Excise_Invoice_Date IS NULL)
4129              )                                    OR
4130              (t_jai_line_rec_new.Customer_Trx_Id <> t_jai_line_rec_old.Customer_Trx_Id)
4131            )
4132         THEN
4133            JAI_AR_TAX_LINES_PKG.POPULATE_TAX_LINES_WRAPPER (
4134                                           pr_old            =>  t_jai_line_rec_old,
4135                                           pr_new            =>  t_jai_line_rec_new,
4136                                           pv_action         =>  lv_action,
4137                                           pv_return_code    =>  lv_return_code,
4138                                           pv_return_message =>  lv_return_message
4139                                         );
4140           IF lv_return_code <> jai_constants.successful   then
4141             RAISE le_error;
4142           END IF;
4143         END IF;
4144        --Added by Qinglei for JAI Trigger Elimination 11-May-2012 end
4145 
4146         OPEN  site_cur;
4147         FETCH site_cur INTO v_ship_id,v_ship_site_id;
4148         CLOSE site_cur;
4149 
4150         IF v_reg_type IN ('RG23A','RG23C') THEN
4151         jai_om_rg_pkg.ja_in_rg23_part_I_entry(
4152                                                   v_reg_type                      ,
4153                                                   v_fin_year                      ,
4154                                                   v_organization_id               ,
4155                                                   v_location_id                   ,
4156                                                   pr_new.inventory_item_id          ,
4157                                                   33                              ,
4158                                                   sysdate                         ,
4159                                                   'I'                             ,
4160                                                   pr_new.quantity_invoiced          ,
4161                                                   pr_new.uom_code                   ,
4162                                                   v_exc_invoice_no                ,
4163                                                   sysdate                         ,
4164                                                   v_excise_diff                   ,
4165                                                   0                               ,
4166                                                   0                               ,
4167                                                   v_ship_id                       ,
4168                                                   v_ship_site_id                  ,
4169                                                   v_header_id                     ,
4170                                                   sysdate                         ,
4171                                                   v_reg_code                      ,
4172                                                   v_creation_date                 ,
4173                                                   v_created_by                    ,
4174                                                   v_last_update_date              ,
4175                                                   v_last_updated_by               ,
4176                                                   v_last_update_login
4177                                              );
4178 
4179         SELECT JAI_CMN_RG_23AC_I_TRXS_S.currval INTO v_part_i_register_id FROM dual;  /* txns changed to trxs by rchandan for  bug#4487676 */
4180 
4181         jai_om_rg_pkg.ja_in_rg23_part_II_entry(
4182                                                   v_reg_code                      ,
4183                                                   v_reg_type                      ,
4184                                                   v_fin_year                      ,
4185                                                   v_organization_id               ,
4186                                                   v_location_id                   ,
4187                                                   pr_new.inventory_item_id          ,
4188                                                   33                              ,
4189                                                   sysdate                         ,
4190                                                   v_part_i_register_id            ,
4191                                                   v_exc_invoice_no                ,
4192                                                   sysdate                         ,
4193                                                   v_excise_diff                   ,
4194                                                   0                               ,
4195                                                   0                               ,
4196                                                   v_ship_id                       ,
4197                                                   v_ship_site_id                  ,
4198                                                   v_source_name                   ,
4199                                                   v_category_name                 ,
4200                                                   v_creation_date                 ,
4201                                                   v_created_by                    ,
4202                                                   v_last_update_date              ,
4203                                                   v_last_updated_by               ,
4204                                                   v_last_update_login             ,
4205                                                   pr_new.customer_trx_line_id       ,
4206                                                   null                            ,
4207                                                   null
4208                                               );
4209 
4210         SELECT JAI_CMN_RG_23AC_I_TRXS_S.currval  INTO v_rg23_part_i_no  FROM dual; /* txns changed to trxs by rchandan for bug# bug#4487676 */
4211 
4212         SELECT JAI_CMN_RG_23AC_II_TRXS_S.currval INTO v_rg23_part_ii_no FROM dual;
4213 
4214         UPDATE  JAI_CMN_RG_23AC_I_TRXS
4215         SET     REGISTER_ID_PART_II = v_rg23_part_ii_no,
4216                 charge_account_id = (
4217                                       SELECT
4218                                               charge_account_id
4219                                       FROM
4220                                               JAI_CMN_RG_23AC_II_TRXS
4221                                       WHERE
4222                                               register_id = v_rg23_part_ii_no
4223                                      )
4224         WHERE  register_id = v_rg23_part_i_no;
4225 
4226       ELSIF v_reg_type = 'PLA' THEN
4227         jai_om_rg_pkg.ja_in_pla_entry(
4228                                       v_organization_id               ,
4229                                       v_location_id                   ,
4230                                       pr_new.inventory_item_id          ,
4231                                       v_fin_year                      ,
4232                                       33                              ,
4233                                       v_header_id                     ,
4234                                       SYSDATE                         ,
4235                                       v_exc_invoice_no                ,
4236                                       SYSDATE                         ,
4237                                       v_excise_diff                   ,
4238                                       0                               ,
4239                                       0                               ,
4240                                       v_ship_id                       ,
4241                                       v_ship_site_id                  ,
4242                                       v_source_name                   ,
4243                                       v_category_name                 ,
4244                                       v_creation_date                 ,
4245                                       v_created_by                    ,
4246                                       v_last_update_date              ,
4247                                       v_last_updated_by               ,
4248                                       v_last_update_login
4249                                     );
4250 
4251 
4252         SELECT  JAI_CMN_RG_PLA_TRXS_S1.currval INTO v_pla_register_no FROM dual;
4253         UPDATE  JAI_CMN_RG_23AC_I_TRXS
4254         SET     REGISTER_ID_PART_II = v_pla_register_no,
4255                 charge_account_id = (SELECT charge_account_id FROM JAI_CMN_RG_PLA_TRXS
4256                                                        WHERE  register_id = v_pla_register_no)
4257         WHERE  register_id = v_rg23_part_i_no;
4258       END IF;
4259 
4260       END IF;
4261     END IF;
4262     EXCEPTION
4263      when le_error then
4264        pv_return_code     :=  jai_constants.unexpected_error;
4265        pv_return_message  := 'Encountered an error in JAI_AR_IMPORT_POPU_PKG.PROCESS_SUPPLEMENT '  || lv_return_message;
4266        fnd_file.put_line(FND_FILE.LOG,'Encountered an error in JAI_AR_IMPORT_POPU_PKG.PROCESS_SUPPLEMENT '  || lv_return_message);
4267      WHEN OTHERS THEN
4268        pv_return_code     :=  jai_constants.unexpected_error;
4269        pv_return_message  := 'Encountered an error in JAI_AR_IMPORT_POPU_PKG.PROCESS_SUPPLEMENT '  || substr(sqlerrm,1,1900);
4270        fnd_file.put_line(FND_FILE.LOG,'Encountered an error in JAI_AR_IMPORT_POPU_PKG.PROCESS_SUPPLEMENT '  || substr(sqlerrm,1,1900));
4271 
4272 end process_supplement;
4273 
4274   /*
4275   REM +======================================================================+
4276   REM NAME          pop_jai_inv_and_suppl renamed from ARI_T2
4277   REM
4278   REM DESCRIPTION   Called from trigger JAI_TAX_PROCESSING_PKG.IMPORT_TAX_WITH_DOCUMENT
4279   REM
4280   REM NOTES         Refers to old trigger JAI_AR_RCTLA_TRIGGER_PKG.ARI_T2
4281   REM
4282   REM +======================================================================+
4283   */
4284 PROCEDURE pop_jai_inv_and_suppl ( pr_old t_rec%type , pr_new t_rec%type , pv_action varchar2 , pv_return_code out nocopy varchar2 , pv_return_message out nocopy varchar2 ) IS
4285 
4286   v_currency_code               gl_sets_of_books.currency_code%TYPE;
4287   v_gl_set_of_bks_id            NUMBER;
4288   v_created_from                VARCHAR2(30);
4289   v_org_id                      NUMBER; --:=pr_new.org_id;
4290   v_last_update_date            DATE; --   := pr_new.last_update_date;
4291   v_last_updated_by             NUMBER; -- := pr_new.last_updated_by;
4292   v_creation_date               DATE; --   := pr_new.creation_date;
4293   v_created_by                  NUMBER; -- := pr_new.created_by;
4294   v_last_update_login           NUMBER; -- := pr_new.last_update_login;
4295   v_interface_line_attribute3   VARCHAR2(30); -- :=  pr_new.interface_line_attribute3;
4296   v_interface_line_attribute6   VARCHAR2(30); -- :=  pr_new.interface_line_attribute6;
4297   v_customer_trx_line_id        NUMBER; -- := pr_new.customer_trx_line_id;
4298   v_header_id                   NUMBER; -- := pr_new.customer_trx_id;
4299   v_line_amount                 NUMBER; -- := nvl(pr_new.quantity_invoiced * pr_new.unit_selling_price,0);
4300 
4301   x                             NUMBER;
4302   v_organization_id             NUMBER;
4303   v_location_id                 NUMBER;
4304   v_once_completed_flag         VARCHAR2(1);
4305   v_excise_amount               NUMBER; --File.Sql.35 Cbabu  := 0;
4306   v_trx_number                  VARCHAR2(20);
4307   v_tax_category_id             NUMBER;
4308   v_payment_register            VARCHAR2(15);
4309   v_excise_invoice_no           VARCHAR2(200);
4310   v_excise_invoice_date         DATE;
4311   v_tax_amount                  NUMBER; --File.Sql.35 Cbabu  := 0;
4312   v_excise_diff                 NUMBER; --File.Sql.35 Cbabu  := 0;
4313   v_assessable_value            NUMBER; --File.Sql.35 Cbabu  := 0;
4314   v_basic_excise_duty_amount    NUMBER; --File.Sql.35 Cbabu  := 0;
4315   v_add_excise_duty_amount      NUMBER; --File.Sql.35 Cbabu  := 0;
4316   v_oth_excise_duty_amount      NUMBER; --File.Sql.35 Cbabu  := 0;
4317 
4318   v_exist_flag                  NUMBER;--File.Sql.35 Cbabu  := 0;
4319   v_batch_source_id             NUMBER ;
4320 
4321   v_books_id                    NUMBER;
4322   v_salesrep_id                 NUMBER;
4323   c_from_currency_code          VARCHAR2(15);
4324   c_conversion_type             VARCHAR2(30);
4325   c_conversion_date             DATE;
4326   c_conversion_rate             NUMBER;
4327 
4328   v_tax_catg_id                 NUMBER;
4329 
4330   v_exchange_rate               NUMBER;
4331 
4332   v_receipt_id                  NUMBER;
4333 
4334   vsqlerrm                      VARCHAR2(240);
4335 
4336 
4337   lv_vat_exemption_flag         JAI_AR_TRX_LINES.vat_exemption_flag%TYPE ;
4338   lv_vat_exemption_type         JAI_AR_TRX_LINES.vat_exemption_type%TYPE ;
4339   lv_vat_exemption_date         JAI_AR_TRX_LINES.vat_exemption_date%TYPE ;
4340   lv_vat_exemption_refno        JAI_AR_TRX_LINES.vat_exemption_refno%TYPE ;
4341   ln_vat_assessable_value       JAI_AR_TRX_LINES.vat_assessable_value%TYPE    ;
4342 
4343 
4344 
4345   --lv_appl_src                   JAI_CMN_ERRORS_T.APPLICATION_SOURCE%type;
4346   --lv_err_msg                    JAI_CMN_ERRORS_T.error_message%type;
4347 
4348   ln_legal_entity_id            NUMBER ;
4349 
4350   v_service_type                VARCHAR2(30);
4351   ln_threshold_tax_cat_id       jai_ap_tds_thhold_taxes.tax_category_id%type;
4352 
4353 
4354 
4355   CURSOR CREATED_FROM_CUR IS
4356   SELECT created_from, trx_number, batch_source_id, set_of_books_id, primary_salesrep_id,
4357          invoice_currency_code, exchange_rate_type, exchange_date, exchange_rate
4358     FROM   ra_customer_trx_all
4359    WHERE  customer_trx_id = v_header_id;
4360 
4361   CURSOR so_picking_hdr_info_1 IS
4362   SELECT a.organization_id, a.location_id
4363   FROM   JAI_OM_WSH_LINES_ALL a
4364   WHERE  a.delivery_id = v_interface_line_attribute3
4365   AND    a.organization_id IS NOT NULL
4366   AND    a.location_id IS NOT NULL
4367   AND    rownum=1 ;
4368 
4369   CURSOR ONCE_COMPLETE_FLAG_CUR IS
4370   SELECT once_completed_flag
4371   FROM   JAI_AR_TRXS
4372   WHERE  customer_trx_id = v_header_id;
4373 
4374   v_trans_type          VARCHAR2(30);
4375 
4376   CURSOR transaction_type_cur IS
4377   SELECT A.TYPE
4378   FROM   RA_CUST_TRX_TYPES_ALL A, RA_CUSTOMER_TRX_ALL b
4379   WHERE  A.cust_trx_type_id = b.cust_trx_type_id
4380   AND    b.customer_trx_id = v_header_id
4381   AND    NVL(A.org_id,0) = NVL(pr_new.org_id,0);
4382 
4383   v_item_type_code                 VARCHAR2(50);
4384   v_serviced_quantity              NUMBER;
4385   v_return_reference_id            NUMBER;
4386   v_original_line_reference        VARCHAR2(50);
4387   v_customer_product_id            NUMBER;
4388   v_warehouse_id                             NUMBER;
4389   v_order_header_id                NUMBER;
4390 
4391   CURSOR So_Lines_Info_Cur IS
4392   SELECT item_type_code, serviced_quantity, return_reference_id, original_system_line_reference, customer_product_id,
4393          warehouse_id, header_id
4394   FROM   So_Lines_All
4395   WHERE  line_id = TO_NUMBER(v_interface_line_attribute6);
4396 
4397   CURSOR JA_SO_LINES_RECORD_CHECK IS
4398   SELECT 1
4399   FROM   JAI_OM_OE_SO_LINES
4400   WHERE  line_id         = TO_NUMBER(v_interface_line_attribute6);
4401 
4402   v_line_count                   NUMBER;
4403   v_tax_line_count               NUMBER;
4404   J_organization_id              NUMBER;
4405   J_location_id                  NUMBER;
4406   J_batch_source_id              NUMBER;
4407   v_register_code                JAI_OM_OE_BOND_REG_HDRS.REGISTER_CODE%TYPE;
4408 
4409   CURSOR get_header_info_cur IS
4410   SELECT A.organization_id,
4411          A.location_id,
4412          A.order_type_id
4413    FROM  JAI_OM_WSH_LINES_ALL A,WSH_NEW_DELIVERIES B
4414   WHERE  A.delivery_id = B.DELIVERY_ID AND
4415          B.NAME = v_interface_line_attribute3 AND
4416          A.order_line_id   = TO_NUMBER(v_interface_line_attribute6);
4417 
4418   CURSOR get_register_code_cur(p_organization_id         NUMBER,
4419                                 p_location_id       NUMBER,
4420                                 p_batch_source_id   NUMBER) IS
4421   SELECT  register_code
4422     FROM  JAI_OM_OE_BOND_REG_HDRS
4423    WHERE  organization_id = p_organization_id
4424      AND  location_id     = p_location_id
4425      AND  register_id IN (SELECT register_id
4426                             FROM   JAI_OM_OE_BOND_REG_DTLS
4427                            WHERE  order_type_id = p_batch_source_id
4428                              AND    order_flag = 'Y');
4429 
4430   v_pref_rg23a            NUMBER;
4431   v_pref_rg23c            NUMBER;
4432   v_pref_pla                      NUMBER;
4433   v_ssi_unit_flag         VARCHAR2(1);
4434   v_raise_error_flag      VARCHAR2(1);
4435   v_fin_year                      NUMBER;
4436   v_rg23a_balance         NUMBER;
4437   v_rg23c_balance         NUMBER;
4438   v_pla_balance           NUMBER;
4439   v_reg_type                      VARCHAR2(10);
4440 
4441   CURSOR Batch_Source_Name_Cur(p_batch_source_id IN NUMBER) IS
4442   SELECT name
4443   FROM   Ra_Batch_Sources_All
4444   WHERE  batch_source_id = p_batch_source_id
4445   AND    NVL(org_id,0)   = NVL(pr_new.org_id,0);
4446 
4447   v_order_invoice_type            VARCHAR2(50);
4448   v_start_number                  NUMBER;
4449   v_end_number                    NUMBER;
4450   v_jump_by                       NUMBER;
4451   v_prefix                                VARCHAR2(50);
4452   v_reg_code                              VARCHAR2(30);
4453   v_source_name           VARCHAR2(100);  --File.Sql.35 Cbabu  := 'Receivables India';
4454   v_category_name         VARCHAR2(100);  --File.Sql.35 Cbabu  := 'RG Register Data Entry';
4455   v_rg23_part_ii_no               NUMBER;
4456   v_rg23_part_i_no        NUMBER;
4457   v_pla_register_no               NUMBER;
4458   V_REGISTER_ID Number;
4459   v_so_line_id       Number;
4460   v_ato_line_id      Varchar2(30);
4461 
4462   cursor c_ont_source_code is
4463   select FND_PROFILE.VALUE('ONT_SOURCE_CODE')
4464   from   dual;
4465   v_ont_source_code ra_interface_lines_all.interface_line_context%type;
4466 
4467 
4468   ln_bill_only number;  --File.Sql.35 Cbabu  := 0;
4469   ln_bill_return number;
4470   --Added by Zhiwei for JAI Trigger elimination begin
4471   ---------------------------------------------------------
4472   cursor c_get_rec(cn_customer_trx_id number)
4473   is
4474   select *
4475   from jai_ar_trxs
4476   where customer_trx_id = cn_customer_trx_id;
4477 
4478   t_rec_new jai_ar_trxs%rowtype;
4479   t_rec_old jai_ar_trxs%rowtype;
4480 
4481   lv_action             VARCHAR2(20)  ;
4482   lv_return_message     VARCHAR2(2000);
4483   lv_return_code        VARCHAR2(100) ;
4484   le_error              EXCEPTION     ;
4485   ---------------------------------------------------------
4486   --Added by Zhiwei for JAI Trigger elimination end
4487 
4488   --Added by Qinglei for JAI Trigger Elimination 11-May_2012 begin
4489   t_jai_line_rec_old    JAI_AR_TRX_LINES%ROWTYPE;
4490   t_jai_line_rec_new    JAI_AR_TRX_LINES%ROWTYPE;
4491 
4492   CURSOR c_jai_ar_trx_lines(pn_customer_trx_id NUMBER,pn_customer_trx_line_id NUMBER) IS
4493   SELECT *
4494   FROM
4495          JAI_AR_TRX_LINES
4496   WHERE customer_trx_line_id = pn_customer_trx_line_id
4497   AND customer_trx_id = pn_customer_trx_id;
4498   --Added by Qinglei for JAI Trigger Elimination 11-May-2012 end
4499 
4500   l_api_name CONSTANT VARCHAR2(30) := 'POP_JAI_INV_AND_SUPPL()';
4501 
4502   BEGIN
4503 
4504     pv_return_code := jai_constants.successful ;
4505 
4506     v_org_id                      := pr_new.org_id;
4507     v_last_update_date            := pr_new.last_update_date;
4508     v_last_updated_by             := pr_new.last_updated_by;
4509     v_creation_date               := pr_new.creation_date;
4510     v_created_by                  := pr_new.created_by;
4511     v_last_update_login           := pr_new.last_update_login;
4512     v_customer_trx_line_id        := pr_new.customer_trx_line_id;
4513     v_header_id                   := pr_new.customer_trx_id;
4514     v_line_amount                 := nvl(pr_new.quantity_invoiced * pr_new.unit_selling_price, nvl(pr_new.extended_amount,0));      --added  nvl(pr_new.extended_amount,0) for bug#8849775
4515     v_interface_line_attribute3   :=  pr_new.interface_line_attribute3;
4516     v_interface_line_attribute6   :=  pr_new.interface_line_attribute6;
4517     v_excise_amount               := 0;
4518     v_tax_amount                  := 0;
4519     v_excise_diff                 := 0;
4520     v_assessable_value            := 0;
4521     v_basic_excise_duty_amount    := 0;
4522     v_add_excise_duty_amount      := 0;
4523     v_oth_excise_duty_amount      := 0;
4524     v_exist_flag                  := 0;
4525     v_source_name                 := 'Receivables India';
4526     v_category_name               := jai_constants.je_category_rg_entry ; -- 'RG Register Data Entry' modified by csahoo for bug#6155839
4527 
4528 
4529     ln_bill_only                  := 0;
4530     ln_bill_return                := 0;
4531     ln_legal_entity_id := get_legal_entity_id(v_header_id) ;
4532 
4533 
4534 
4535     /****************************** Validation 1 ********************************/
4536 
4537     if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
4538       FND_LOG.STRING(G_LEVEL_PROCEDURE,
4539                      G_MODULE_NAME || l_api_name || '.BEGIN',
4540                      G_PKG_NAME || ': ' || l_api_name || '()+');
4541     end if;
4542 
4543     if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
4544       FND_LOG.STRING(G_LEVEL_PROCEDURE,
4545                      G_MODULE_NAME || l_api_name || '.IN',
4546                      G_PKG_NAME || ': ' || l_api_name || '().'||'Org id:'||v_org_id);
4547     end if;
4548 
4549 
4550 
4551     /* Bug 5243532. Added by Lakshmi Gopalsami
4552        Removed the reference to cursor Fetch_Book_Id_Cur
4553        and used the values assigned in trigger to get
4554        the value of SOB.
4555     */
4556     v_gl_set_of_bks_id := pr_new.set_of_books_id;
4557 
4558     --FND_FILE.PUT_LINE(FND_FILE.LOG,' SOB id '|| v_gl_set_of_bks_id);
4559 
4560     if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
4561       FND_LOG.STRING(G_LEVEL_PROCEDURE,
4562                      G_MODULE_NAME || l_api_name || '.IN',
4563                      G_PKG_NAME || ': ' || l_api_name || '().'||'SOB id:'||v_gl_set_of_bks_id);
4564     end if;
4565 
4566 
4567     /****************************** Validation 2 ********************************/
4568 
4569     IF pr_new.interface_line_context in ( 'PROJECTS INVOICES','OKS CONTRACTS', 'LEGACY') THEN
4570       RETURN;
4571     END IF;
4572 
4573 
4574     /****************************** Validation 3 ********************************/
4575     OPEN get_header_info_cur;
4576     FETCH get_header_info_cur INTO    j_organization_id   ,
4577                                       j_location_id       ,
4578                                       j_batch_source_id;
4579     CLOSE get_header_info_cur;
4580 
4581 
4582     if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
4583       FND_LOG.STRING(G_LEVEL_PROCEDURE,
4584                      G_MODULE_NAME || l_api_name || '.IN',
4585                      G_PKG_NAME || ': ' || l_api_name || '().'||'HEADER INFO:'
4586                      ||' org id='||j_organization_id
4587                      ||' loc id='||j_location_id
4588                      ||' Batch source id='||j_batch_source_id);
4589     end if;
4590 
4591     /*FND_FILE.PUT_LINE(FND_FILE.LOG, ' header info.' );
4592     FND_FILE.PUT_LINE(FND_FILE.LOG, ' org id: '|| j_organization_id);
4593     FND_FILE.PUT_LINE(FND_FILE.LOG, ' loc id: '|| j_location_id);
4594     FND_FILE.PUT_LINE(FND_FILE.LOG, ' Batch source id: '|| j_batch_source_id);*/
4595 
4596 
4597 
4598     jai_cmn_bond_register_pkg.get_register_id(
4599                                             j_organization_id      ,
4600                                             j_location_id          ,
4601                                             j_batch_source_id      ,
4602                                             'Y'                    ,
4603                                             v_register_id          ,
4604                                             v_register_code
4605                                       );
4606 
4607     /****************************** Validation 4 ********************************/
4608     OPEN  transaction_type_cur;
4609     FETCH transaction_type_cur INTO v_trans_type;
4610     CLOSE transaction_type_cur;
4611 
4612 
4613     if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
4614       FND_LOG.STRING(G_LEVEL_PROCEDURE,
4615                      G_MODULE_NAME || l_api_name || '.IN',
4616                      G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
4617                      ||' Transaction type ='||v_trans_type
4618                      ||' Interface line attribute1='||pr_new.interface_line_attribute1
4619                      ||' Interface line context='||pr_new.interface_line_context);
4620     end if;
4621 
4622     /*FND_FILE.PUT_LINE(FND_FILE.LOG, ' Transaction type '|| v_trans_type);
4623     FND_FILE.PUT_LINE(FND_FILE.LOG, ' Interface line attribute1 '||pr_new.interface_line_attribute1);
4624     FND_FILE.PUT_LINE(FND_FILE.LOG, ' Interface line context '|| pr_new.interface_line_context);*/
4625 
4626     IF (    v_trans_type = 'CM'
4627         AND pr_new.interface_line_attribute1 = 'SUPPLEMENT CM'
4628         AND pr_new.interface_line_context='INDIA INVOICES'
4629        )
4630        OR
4631        (   v_trans_type = 'DM'
4632        AND pr_new.interface_line_attribute1 = 'SUPPLEMENT DM'
4633        AND pr_new.interface_line_context='INDIA INVOICES'
4634        )
4635        OR
4636        (v_trans_type = 'INV')
4637     THEN
4638        null;
4639     ELSE
4640       return;
4641     END IF;
4642 
4643     /****************************** Validation 5 ********************************/
4644     /*
4645       This is cursor that is commonly used at various places in the code
4646     */
4647     OPEN   created_from_cur;
4648     FETCH  created_from_cur INTO  v_created_from          ,
4649                                   v_trx_number            ,
4650                                   v_batch_source_id       ,
4651                                   v_books_id              ,
4652                                   v_salesrep_id           ,
4653                                   c_from_currency_code    ,
4654                                   c_conversion_type       ,
4655                                   c_conversion_date       ,
4656                                   c_conversion_rate ;
4657     CLOSE  created_from_cur;
4658 
4659     IF v_created_from <> 'RAXTRX' THEN
4660        return;
4661     END IF;
4662 
4663     /****************************** Validation 6 ********************************/
4664     OPEN   once_complete_flag_cur;
4665     FETCH  once_complete_flag_cur INTO v_once_completed_flag;
4666     CLOSE  once_complete_flag_cur;
4667 
4668 
4669     if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
4670       FND_LOG.STRING(G_LEVEL_PROCEDURE,
4671                      G_MODULE_NAME || l_api_name || '.IN',
4672                      G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
4673                      ||' Once completed flag ='||v_once_completed_flag
4674                      );
4675     end if;
4676 
4677     /*FND_FILE.PUT_LINE(FND_FILE.LOG,
4678       ' Once completed flag '|| v_once_completed_flag);*/
4679 
4680 
4681     IF NVL(v_once_completed_flag,'N') = 'Y' THEN
4682       RETURN;
4683     END IF;
4684 
4685   /****************************** Validation 7 ********************************/
4686   /*
4687     Code for handling supplementary invoices
4688   */
4689 
4690   IF pr_new.interface_line_attribute1 IN ('SUPPLEMENT CM'
4691                                          ,'SUPPLEMENT DM'
4692                                          ,'SUPPLEMENT INVOICE'
4693                                         )
4694   AND pr_new.interface_line_context = 'INDIA INVOICES'
4695   THEN
4696 
4697       process_supplement(
4698              pr_old => pr_old,
4699              pr_new => pr_new,
4700              pv_action => pv_action,
4701              pv_once_complete_flag => v_once_completed_flag,
4702              pv_return_code => lv_return_code,
4703              pv_return_message =>lv_return_message
4704              );
4705       IF lv_return_code <> jai_constants.successful   then
4706               RAISE le_error;
4707       END IF;
4708 
4709   ELSE /* ELSIF OF pr_new.interface_line_context IN ('SUPPLEMENT CM','SUPPLEMENT DM','SUPPLEMENT INVOICE')     */
4710     -------------------------------------------------------------------------------------------
4711     /*from here starts the actual coding with respect to what needs to be done for a normal imported invoice
4712       the code segment until now - is for supplementary transactions
4713     */
4714            PROCESS_ORDER(
4715              pr_old => pr_old,
4716              pr_new => pr_new,
4717              pv_action => pv_action,
4718              pv_once_complete_flag => v_once_completed_flag,
4719              pv_return_code => lv_return_code,
4720              pv_return_message =>lv_return_message
4721            );
4722            IF lv_return_code <> jai_constants.successful   then
4723               RAISE le_error;
4724            END IF;
4725 
4726   END IF;
4727 if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
4728       FND_LOG.STRING(G_LEVEL_PROCEDURE,
4729                      G_MODULE_NAME || l_api_name || '.END',
4730                      G_PKG_NAME || ': ' || l_api_name || '()-');
4731 end if;
4732 EXCEPTION
4733   WHEN le_error then
4734      Pv_return_code     :=  jai_constants.unexpected_error;
4735      Pv_return_message  := 'Encountered an error in JAI_AR_IMPORT_POPU_PKG.POP_JAI_INV_AND_SUPPL  '  || lv_return_message;
4736      fnd_file.put_line(FND_FILE.LOG,'Encountered an error in JAI_AR_IMPORT_POPU_PKG.POP_JAI_INV_AND_SUPPL  '  || lv_return_message);
4737   WHEN OTHERS THEN
4738     vsqlerrm := SQLERRM;
4739 
4740 
4741      Pv_return_code     :=  jai_constants.unexpected_error;
4742      Pv_return_message  := 'Encountered an error in JAI_AR_IMPORT_POPU_PKG.POP_JAI_INV_AND_SUPPL  '  || substr(sqlerrm,1,1900);
4743      fnd_file.put_line(FND_FILE.LOG,'Encountered an error in JAI_AR_IMPORT_POPU_PKG.POP_JAI_INV_AND_SUPPL  '  || substr(sqlerrm,1,1900));
4744 
4745      /*lv_appl_src := 'JA_IN_OE_AR_LINES_INSERT_TRG' ;
4746      lv_err_msg  := 'EXCEPTION Occured' ;
4747 
4748     INSERT INTO JAI_CMN_ERRORS_T
4749     (
4750        APPLICATION_SOURCE                  ,
4751        error_message                   ,
4752        additional_error_mesg           ,
4753        creation_date                   ,
4754        created_by                      ,
4755        LAST_UPDATED_BY,
4756        LAST_UPDATE_DATE
4757     )
4758     VALUES
4759     (
4760        lv_appl_src,
4761        lv_err_msg ,
4762        substr(vsqlerrm,1,200)         ,
4763        sysdate                        ,
4764        user     ,
4765        fnd_global.user_id,
4766        sysdate
4767      );*/
4768 
4769   END pop_jai_inv_and_suppl ;
4770 
4771   /*
4772   REM +======================================================================+
4773   REM NAME          pop_jai_rmacm renamed from ARI_T3
4774   REM
4775   REM DESCRIPTION   Called from trigger JAI_TAX_PROCESSING_PKG.IMPORT_TAX_WITH_DOCUMENT
4776   REM
4777   REM NOTES         Refers to old trigger JAI_AR_RCTLA_TRIGGER_PKG.ARI_T3
4778   REM
4779   REM +======================================================================+
4780   */
4781 PROCEDURE pop_jai_rmacm ( pr_old t_rec%type , pr_new t_rec%type , pv_action varchar2 , pv_return_code out nocopy varchar2 , pv_return_message out nocopy varchar2 ) IS
4782 
4783     v_created_from        VARCHAR2(30);
4784 
4785     v_last_update_date    DATE                  := pr_new.last_update_date;
4786     v_last_updated_by     NUMBER                := pr_new.last_updated_by;
4787     v_creation_date       DATE                  := pr_new.creation_date;
4788     v_created_by          NUMBER                := pr_new.created_by;
4789     v_last_update_login   NUMBER                := pr_new.last_update_login;
4790     v_customer_trx_line_id    NUMBER            := pr_new.customer_trx_line_id;
4791     v_header_id           NUMBER                := pr_new.customer_trx_id;
4792     v_line_amount         NUMBER                := NVL(pr_new.quantity_credited * pr_new.unit_selling_price, nvl(pr_new.extended_amount,0));  --added  nvl(pr_new.extended_amount,0) for bug 8849775
4793     v_interface_line_attribute7   VARCHAR2(30)  :=  pr_new.interface_line_attribute6;
4794     v_quantity_credited   NUMBER                := pr_new.quantity_credited;
4795 
4796 
4797     x                     NUMBER := 0;
4798     v_organization_id     NUMBER;
4799     v_location_id         NUMBER;
4800     v_once_completed_flag VARCHAR2(1);
4801     v_excise_amount       NUMBER := 0;
4802     v_trx_number          VARCHAR2(30);
4803     v_tax_category_id     NUMBER;
4804     v_payment_register    VARCHAR2(15);
4805     v_excise_invoice_no   NUMBER;
4806     v_excise_invoice_date DATE;
4807     v_tax_amount          NUMBER := 0;
4808     v_assessable_value    NUMBER := 0;
4809     v_basic_excise_duty_amount    NUMBER := 0;
4810     v_add_excise_duty_amount  NUMBER := 0;
4811     v_oth_excise_duty_amount  NUMBER := 0;
4812     v_exist_flag          NUMBER := 0;
4813     v_quantity            NUMBER := 0;
4814     v_func_tax_amount     NUMBER := 0;
4815     v_batch_source_id     NUMBER ;
4816     v_books_id            NUMBER;
4817     v_salesrep_id         NUMBER;
4818     c_from_currency_code  VARCHAR2(15);
4819     c_conversion_type     VARCHAR2(30);
4820     c_conversion_date     DATE;
4821     c_conversion_rate     NUMBER;
4822     v_old_customer_trx_id NUMBER;
4823     v_line_count          NUMBER := 0;
4824     ln_legal_entity_id    NUMBER ;
4825     v_service_type        VARCHAR2(30);
4826 
4827 
4828 
4829     CURSOR DUPLICATE_LINES_CUR IS
4830     SELECT 1
4831     FROM   JAI_AR_TRX_LINES
4832     WHERE  customer_trx_LINE_id = v_customer_trx_line_id;
4833 
4834     CURSOR OLD_CUSTOMER_TRX_ID_CUR IS
4835     SELECT Customer_Trx_Id
4836     FROM   JAI_AR_TRX_LINES
4837     WHERE  customer_trx_LINE_id = v_customer_trx_line_id;
4838 
4839     CURSOR CREATED_FROM_CUR IS
4840     SELECT created_from, trx_number, batch_source_id, set_of_books_id, primary_salesrep_id,
4841            invoice_currency_code, exchange_rate_type, exchange_date, exchange_rate,
4842            legal_entity_id
4843     FROM   ra_customer_trx_all
4844     WHERE  customer_trx_id = v_header_id;
4845 
4846 
4847 
4848     CURSOR SO_RMA_HDR_INFO IS
4849     SELECT organization_id, location_id
4850     FROM   JAI_OM_WSH_LINES_ALL
4851     WHERE  order_line_id IN
4852     (SELECT reference_line_id
4853      FROM oe_order_lines_all
4854      WHERE line_id = v_interface_line_attribute7
4855     );
4856 
4857 
4858 
4859     Cursor c_get_location is
4860      SELECT organization_id , location_id  FROM JAI_INV_SUBINV_DTLS
4861       WHERE organization_id = pr_new.interface_line_attribute10
4862       AND UPPER(sub_inventory_name) IN
4863       (SELECT UPPER(subinventory)
4864        FROM rcv_transactions
4865        WHERE
4866        organization_id = pr_new.interface_line_attribute10
4867        AND (oe_order_line_id) = v_interface_line_attribute7
4868        AND subinventory IS NOT NULL
4869       );
4870 
4871 
4872     CURSOR SO_RMA_LINES_INFO IS
4873     SELECT tax_category_id, assessable_value, service_type_code
4874     FROM   JAI_OM_OE_RMA_LINES
4875     WHERE  rma_line_id = v_interface_line_attribute7;
4876 
4877     CURSOR SO_RMA_TAX_LINES_INFO IS
4878     SELECT A.tax_line_no, A.uom, A.tax_id, A.tax_rate, A.qty_rate, A.base_tax_amount, A.tax_amount, b.tax_type,
4879            A.func_tax_amount,
4880            A.precedence_1, A.precedence_2, A.precedence_3, A.precedence_4, A.precedence_5 ,
4881            A.precedence_6, A.precedence_7, A.precedence_8, A.precedence_9, A.precedence_10
4882     FROM   JAI_OM_OE_RMA_TAXES A, JAI_CMN_TAXES_ALL b
4883     WHERE  A.rma_line_id = v_interface_line_attribute7
4884     AND    A.tax_id = b.tax_id
4885     and    b.tax_type <> lc_modvat_tax
4886     ORDER BY tax_line_no;
4887 
4888 
4889     CURSOR DUPLICATE_HDR_CUR IS
4890     SELECT 1
4891     FROM   JAI_AR_TRXS
4892     WHERE  customer_trx_id = v_header_id;
4893 
4894 
4895     /* Cursor To Check Whether Interfaced Record Exist in JAI_OM_WSH_LINES_ALL */
4896     CURSOR SO_RMA_RECORD_CHECK IS
4897     SELECT 1
4898     FROM   JAI_OM_OE_RMA_LINES
4899     WHERE  rma_line_id = v_interface_line_attribute7;
4900 
4901 
4902     CURSOR SO_RMA_TAX_RECORD_CHECK IS
4903     SELECT 1
4904     FROM   JAI_OM_OE_RMA_TAXES
4905     WHERE  rma_line_id = v_interface_line_attribute7;
4906 
4907 
4908     v_trans_type      VARCHAR2(30);
4909     CURSOR transaction_type_cur IS
4910     SELECT A.TYPE
4911     FROM   RA_CUST_TRX_TYPES_ALL A, RA_CUSTOMER_TRX_ALL b
4912     WHERE  A.cust_trx_type_id = b.cust_trx_type_id
4913     AND    b.customer_trx_id = v_header_id
4914     AND    NVL(A.org_id,0) = NVL(pr_new.org_id,0);
4915 
4916 
4917     CURSOR SO_RMA_QUANTITY_CHECK IS
4918     SELECT NVL(quantity,0)
4919     FROM   JAI_OM_OE_RMA_LINES
4920     WHERE  rma_line_id = v_interface_line_attribute7;
4921 
4922 
4923     cursor c_ont_source_code is
4924     select FND_PROFILE.VALUE('ONT_SOURCE_CODE')
4925     from   dual;
4926     v_ont_source_code ra_interface_lines_all.interface_line_context%type;
4927 
4928     CURSOR cur_get_transaction_type_id
4929     IS
4930     SELECT
4931           ott.transaction_type_id
4932     FROM
4933           ra_customer_trx_all     rct,
4934           oe_transaction_types_tl ott
4935     WHERE
4936           rct.customer_trx_id = pr_new.previous_customer_trx_id
4937     AND   rct.interface_header_attribute2 = ott.name       ;
4938 
4939 
4940     CURSOR cur_get_cmline_tax_amt
4941     IS
4942     SELECT
4943           nvl(sum(tax_amount),0)
4944     FROM
4945           JAI_AR_TRX_TAX_LINES
4946     WHERE
4947       link_to_cust_trx_line_id = pr_new.customer_trx_line_id ;
4948 
4949 
4950     ln_transaction_type_id        OE_TRANSACTION_TYPES_TL.TRANSACTION_TYPE_ID%TYPE;
4951     ln_register_id                JAI_OM_OE_BOND_REG_HDRS.REGISTER_ID%TYPE;
4952     lv_register_code              JAI_OM_OE_BOND_REG_HDRS.REGISTER_CODE%TYPE;
4953 
4954     CURSOR check_reference_existed
4955     IS
4956     SELECT 1
4957     FROM   OE_ORDER_LINES_ALL
4958     WHERE  LINE_ID = v_interface_line_attribute7
4959     AND    reference_line_id = v_interface_line_attribute7;
4960 
4961 
4962     CURSOR get_non_ship_loc
4963     IS
4964     SELECT ORGANIZATION_ID,LOCATION_ID
4965     FROM  HR_ORGANIZATION_UNITS
4966     WHERE ORGANIZATION_ID = pr_new.interface_line_attribute10;
4967 
4968     ln_ref_existed_flag NUMBER;
4969     ln_location_id   NUMBER;
4970   -----------------------------------------------------------------
4971   --Added by Bo Li for ER VAT non-shippable RMA 2010-4-27 Begin
4972 
4973  /*changes by abezgam for bug 10176878, start */
4974 
4975 cursor c_bill_only_invoice(cp_customer_trx_line_id number, cp_process_name oe_wf_line_assign_v.process_name%type ) is
4976  select  1
4977  from    oe_wf_order_assign_v o_wf_asg
4978  where   order_type_name = pr_new.interface_line_attribute2
4979  and     exists
4980  (
4981   select  1
4982   from    oe_wf_line_assign_v  l_wf_asg
4983   where   assignment_id = o_wf_asg.assignment_id
4984   and     process_name  = cp_process_name  /*'R_BILL_ONLY' Ramananda for removal of SQL LITERALs */
4985   and     order_type_id = l_wf_asg.order_type_id
4986  )
4987 ;
4988      ln_bill_return NUMBER :=0;
4989 
4990     --Added by Zhiwei for JAI Trigger elimination begin
4991     ---------------------------------------------------------
4992     cursor c_get_rec(cn_customer_trx_id number)
4993     is
4994     select *
4995     from jai_ar_trxs
4996     where customer_trx_id = cn_customer_trx_id;
4997 
4998     t_rec_new jai_ar_trxs%rowtype;
4999     t_rec_old jai_ar_trxs%rowtype;
5000 
5001     lv_action             VARCHAR2(20)  ;
5002     lv_return_message     VARCHAR2(2000);
5003     lv_return_code        VARCHAR2(100) ;
5004     le_error              EXCEPTION     ;
5005 
5006     ---------------------------------------------------------
5007     --Added by Zhiwei for JAI Trigger elimination end
5008   --Added by Qinglei for JAI Trigger Elimination 11-May_2012 begin
5009   t_jai_line_rec_old    JAI_AR_TRX_LINES%ROWTYPE;
5010   t_jai_line_rec_new    JAI_AR_TRX_LINES%ROWTYPE;
5011 
5012   CURSOR c_jai_ar_trx_lines(pn_customer_trx_id NUMBER,pn_customer_trx_line_id NUMBER) IS
5013   SELECT *
5014   FROM
5015          JAI_AR_TRX_LINES
5016   WHERE CUSTOMER_TRX_ID = pn_customer_trx_id
5017   AND customer_trx_line_id = pn_customer_trx_line_id;
5018   --Added by Qinglei for JAI Trigger Elimination 11-May-2012 end
5019   l_api_name CONSTANT VARCHAR2(30) := 'POP_JAI_RMACM()';
5020 
5021 procedure process_bill_only_return is
5022 cursor c_check_hdr_exists is
5023   select 1
5024   from   JAI_AR_TRXS
5025   where  customer_trx_id = pr_new.customer_trx_id;
5026 
5027   cursor c_trx_cur is
5028   select trx_number               ,
5029          batch_source_id          ,
5030          set_of_books_id          ,
5031          primary_salesrep_id      ,
5032          invoice_currency_Code    ,
5033          exchange_rate_type       ,
5034          exchange_date            ,
5035          exchange_rate            ,
5036          created_from ,
5037    nvl(bill_to_customer_id,ship_to_customer_id) customer_id ,
5038          trx_date
5039   from   ra_customer_trx_all
5040   where  customer_trx_id = pr_new.customer_trx_id;
5041 
5042   CURSOR SO_RMA_LINES_INFO IS
5043   --added unit_code,line_amount
5044   SELECT tax_category_id, assessable_value, service_type_code, quantity, inventory_item_id ,uom, selling_price
5045   FROM   JAI_OM_OE_RMA_LINES
5046   WHERE  rma_line_id = TO_NUMBER(v_interface_line_attribute7);
5047 
5048   CURSOR SO_RMA_TAX_LINES_INFO IS
5049   SELECT A.tax_line_no, A.uom, A.tax_id, A.tax_rate, A.qty_rate, A.base_tax_amount, A.tax_amount, b.tax_type,
5050          A.func_tax_amount,
5051    A.precedence_1, A.precedence_2, A.precedence_3, A.precedence_4, A.precedence_5 ,
5052    A.precedence_6, A.precedence_7, A.precedence_8, A.precedence_9, A.precedence_10
5053   FROM   JAI_OM_OE_RMA_TAXES A, JAI_CMN_TAXES_ALL b
5054   WHERE  A.rma_line_id = TO_NUMBER(v_interface_line_attribute7)
5055   AND    A.tax_id = b.tax_id
5056 and    b.tax_type <> lc_modvat_tax
5057   ORDER BY tax_line_no;
5058 
5059   cursor  c_get_amounts is
5060   select  sum(tax_amount) tax_amt , sum(line_amount) line_amt
5061   from    JAI_AR_TRX_LINES
5062   where   customer_trx_id = pr_new.customer_trx_id;
5063 
5064   cursor  c_default_location(CP_ORGANIZATION_ID  number) is
5065   select  DEFAULT_LOCATION_BILL_ONLY
5066   from    JAI_CMN_INVENTORY_ORGS
5067   where   organization_id = cp_organization_id
5068   and     location_id = 0;
5069 
5070   cursor  c_oe_system_params is
5071   select  master_organization_id
5072   from    oe_system_parameters ;
5073 
5074   lr_trx_rec          c_trx_cur%rowtype;
5075   ln_hdr_exists       number;
5076   ln_line_amount      number;
5077   ln_tax_amount       number;
5078   rec_so_lines        SO_RMA_LINES_INFO%rowtype;
5079   rec_so_taxes        SO_RMA_TAX_LINES_INFO%rowtype;
5080   ln_inv_orgn_id      number ;
5081   ln_default_locn_id  number;
5082 
5083   /*lv_appl_src  JAI_CMN_ERRORS_T.APPLICATION_SOURCE%type;
5084   lv_err_msg   JAI_CMN_ERRORS_T.ERROR_MESSAGE%type;
5085   lv_addl_msg  JAI_CMN_ERRORS_T.ADDITIONAL_ERROR_MESG%type;*/
5086 
5087   ln_legal_entity_id NUMBER;
5088     ln_tcs_exists             number;
5089     ln_tcs_regime_id          JAI_RGM_DEFINITIONS.regime_id%type;
5090     ln_threshold_slab_id      jai_ap_tds_thhold_slabs.threshold_slab_id%type;
5091     ln_last_line_no           number;
5092     ln_base_line_no           number;
5093     lv_process_flag             VARCHAR2(2);
5094     lv_process_message          VARCHAR2(1996);
5095     ln_reg_id                   number;
5096     ln_threshold_tax_cat_id         jai_ap_tds_thhold_taxes.tax_category_id%type;
5097     cursor c_chk_rgm_tax_exists  ( cp_regime_code          JAI_RGM_DEFINITIONS.regime_code%type
5098                                  , cp_cust_trx_line_id     ra_customer_trx_lines_all.customer_trx_line_id%type
5099                                  )
5100     is
5101       select  count(1)
5102       from    jai_regime_tax_types_v jrttv
5103             , JAI_AR_TRX_TAX_LINES  jrctt
5104             , JAI_CMN_TAXES_ALL jtc
5105       where   jtc.tax_id     = jrctt.tax_id
5106       and     jtc.tax_type  = jrttv.tax_type
5107       and     regime_code    = cp_regime_code
5108       and     jrctt.link_to_cust_trx_line_id = cp_cust_trx_line_id;
5109     cursor c_get_regime_id (cp_regime_code    JAI_RGM_DEFINITIONS.regime_code%type)
5110     is
5111       select regime_id
5112       from   JAI_RGM_DEFINITIONS
5113       where  regime_code = cp_regime_code;
5114 
5115     Cursor get_ar_tax_amount
5116     is
5117     select sum(tax_amount)
5118     from JAI_AR_TRX_TAX_LINES
5119     Where link_to_cust_trx_line_id = v_customer_trx_line_id;
5120 
5121     ln_ar_tax_amount JAI_AR_TRX_TAX_LINES.TAX_AMOUNT%type;
5122 
5123     l_api_name CONSTANT VARCHAR2(30) := 'process_bill_only_return()';
5124 
5125 begin
5126   --FND_FILE.PUT_LINE(FND_FILE.LOG,  'Inside Procedure process_bill_only_return');
5127   if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
5128       FND_LOG.STRING(G_LEVEL_PROCEDURE,
5129                      G_MODULE_NAME || l_api_name || '.BEGIN',
5130                      G_PKG_NAME || ': ' || l_api_name || '()+');
5131   end if;
5132   ln_hdr_exists        := 0;
5133   ln_line_amount       :=0;
5134   ln_tax_amount        :=0;
5135   /*
5136    writing code here to process a bill only invoice.
5137    Validations to handle
5138    ---------------------
5139     a) in case of discounts only one line should have taxes
5140     b) in case of bond register excise and excise cess taxes should not flow into AR
5141     c) only one record should be inserted in the JAI_AR_TRXS table.
5142   */
5143 
5144   ln_hdr_exists := 0;
5145   open c_check_hdr_exists;
5146   fetch c_check_hdr_exists into ln_hdr_exists;
5147   close c_check_hdr_exists;
5148 
5149   if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
5150       FND_LOG.STRING(G_LEVEL_PROCEDURE,
5151                      G_MODULE_NAME || l_api_name || '.IN',
5152                      G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
5153                      ||' Header exists ='||ln_hdr_exists
5154                      );
5155   end if;
5156 
5157   --FND_FILE.PUT_LINE(FND_FILE.LOG,' Header exists '|| ln_hdr_exists);
5158 
5159   if nvl(ln_hdr_exists,0) = 0 then
5160     /*
5161     no record exists in the JAI_AR_TRXS table for the customer trx id ,
5162     hence insert a record into the table.
5163     */
5164 
5165     open  c_trx_cur;
5166     fetch c_trx_cur into lr_trx_rec;
5167     close c_trx_cur;
5168 
5169     if pr_new.warehouse_id is null then
5170        open  c_oe_system_params;
5171        fetch c_oe_system_params into ln_inv_orgn_id;
5172        close c_oe_system_params;
5173     else
5174        ln_inv_orgn_id := pr_new.warehouse_id ;
5175     end if;
5176 
5177     --FND_FILE.PUT_LINE(FND_FILE.LOG,' Inv orgn id: '|| ln_inv_orgn_id);
5178     if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
5179         FND_LOG.STRING(G_LEVEL_PROCEDURE,
5180                        G_MODULE_NAME || l_api_name || '.IN',
5181                        G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
5182                        ||' Inv orgn id ='||ln_inv_orgn_id
5183                        );
5184     end if;
5185     open  c_default_location(ln_inv_orgn_id);
5186     fetch c_default_location into ln_default_locn_id;
5187     close c_default_location;
5188 
5189     --FND_FILE.PUT_LINE(FND_FILE.LOG,' Default location id '|| ln_default_locn_id);
5190     if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
5191         FND_LOG.STRING(G_LEVEL_PROCEDURE,
5192                        G_MODULE_NAME || l_api_name || '.IN',
5193                        G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
5194                        ||' Default location id ='||ln_default_locn_id
5195                        );
5196     end if;
5197     /*if ln_default_locn_id is null then
5198 
5199      \*
5200       if the default location setup is not done , log this as a message in the JAI_CMN_ERRORS_T table
5201       do not stop the processing.
5202       We could still update the organization and location id in the JAI_AR_TRXS table later on (as a datafix)
5203      *\
5204        lv_appl_src := 'JA_IN_OE_AR_LINES_INSERT_TRG' ;
5205        lv_err_msg  := 'Default Location is not setup for Inventory Organization ' ||  pr_new.warehouse_id ;
5206        lv_addl_msg := 'Please setup the Default Location in Organization Additional Information Screen for Trx id : ' || pr_new.customer_trx_id ;
5207 
5208        insert into JAI_CMN_ERRORS_T
5209        (
5210         APPLICATION_SOURCE          ,
5211         ERROR_MESSAGE           ,
5212         ADDITIONAL_ERROR_MESG   ,
5213         CREATION_DATE           ,
5214         CREATED_BY              ,
5215         -- added, Harshita for Bug 4866533
5216         LAST_UPDATED_BY,
5217         LAST_UPDATE_DATE
5218        )
5219        values
5220        (
5221        lv_appl_src,  \*'JA_IN_OE_AR_LINES_INSERT_TRG', Ramananda for removal of SQL LITERALs *\
5222        lv_err_msg,   \* 'Default Location is not setup for Inventory Organization ' ||  pr_new.warehouse_id , *\
5223        lv_addl_msg, \* 'Please setup the Default Location in Organization Additional Information Screen for Trx id : ' || pr_new.customer_trx_id  , *\
5224        sysdate,
5225        fnd_global.user_id ,
5226         -- added, Harshita for Bug 4866533
5227         fnd_global.user_id,
5228         sysdate
5229        );
5230     end if;*/
5231     insert into JAI_AR_TRXS    -- bill only invoice
5232     (
5233     CUSTOMER_TRX_ID                           ,
5234     ORGANIZATION_ID                           ,
5235     LOCATION_ID                               ,
5236     UPDATE_RG_FLAG                            ,
5237     ONCE_COMPLETED_FLAG                       ,
5238     TOTAL_AMOUNT                              ,
5239     LINE_AMOUNT                               ,
5240     TAX_AMOUNT                                ,
5241     TRX_NUMBER                                ,
5242     BATCH_SOURCE_ID                           ,
5243     CREATION_DATE                             ,
5244     CREATED_BY                                ,
5245     LAST_UPDATE_DATE                          ,
5246     LAST_UPDATED_BY                           ,
5247     LAST_UPDATE_LOGIN                         ,
5248     SET_OF_BOOKS_ID                           ,
5249     PRIMARY_SALESREP_ID                       ,
5250     INVOICE_CURRENCY_CODE                     ,
5251     EXCHANGE_RATE_TYPE                        ,
5252     EXCHANGE_DATE                             ,
5253     EXCHANGE_RATE                             ,
5254     CREATED_FROM                              ,
5255     UPDATE_RG23D_FLAG                         ,
5256     TAX_INVOICE_NO                            ,
5257     LEGAL_ENTITY_ID         /* rallamse bug#4448789 */
5258     )
5259     values
5260     (
5261     pr_new.customer_trx_id                    ,
5262     ln_inv_orgn_id                          ,
5263     ln_default_locn_id                      ,
5264     'N'                                     ,
5265     'N'                                     ,/*Bug 4694650 bduvarag*/
5266     0                                       ,
5267     0                                       ,
5268     0                                       ,
5269     lr_trx_rec.trx_number                   ,
5270     lr_trx_rec.batch_source_id              ,
5271     sysdate                                 ,
5272     fnd_global.user_id                      ,
5273     sysdate                                 ,
5274     fnd_global.user_id                      ,
5275     fnd_global.login_id                     ,
5276     lr_trx_rec.set_of_books_id              ,
5277     lr_trx_rec.primary_salesrep_id          ,
5278     lr_trx_rec.invoice_currency_code        ,
5279     lr_trx_rec.exchange_rate_type           ,
5280     lr_trx_rec.exchange_date                ,
5281     lr_trx_rec.exchange_rate                ,
5282     lr_trx_rec.created_from                 ,
5283     'N'                                     ,
5284     NULL                                    ,
5285     ln_legal_entity_id         /* rallamse bug#4448789 */
5286     );
5287     --FND_FILE.PUT_LINE(FND_FILE.LOG, ' After insert into JAI_AR_TRXS - Bill only invoice');
5288     if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
5289         FND_LOG.STRING(G_LEVEL_PROCEDURE,
5290                        G_MODULE_NAME || l_api_name || '.IN',
5291                        G_PKG_NAME || ': ' || l_api_name || '().'||' After insert into JAI_AR_TRXS - Bill only invoice'
5292                        );
5293     end if;
5294   end if;
5295 
5296   /*
5297    insert into the JAI_AR_TRX_TAX_LINES table and then insert into the JAI_AR_TRX_LINES table.
5298    pr_new.interface_line_attribute6 = order_line_id
5299    pr_new.interface_line_context    = 'ORDER ENTRY'
5300   */
5301 
5302    open  c_ont_source_code;
5303    fetch c_ont_source_code into v_ont_source_code;
5304    close c_ont_source_code;
5305    --FND_FILE.PUT_LINE(FND_FILE.LOG,' Ont source code: '|| v_ont_source_code);
5306    if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
5307         FND_LOG.STRING(G_LEVEL_PROCEDURE,
5308                        G_MODULE_NAME || l_api_name || '.IN',
5309                        G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
5310                        ||' Ont source code ='||v_ont_source_code
5311                        );
5312    end if;
5313 
5314    v_ont_source_code := ltrim(rtrim(v_ont_source_code));
5315 
5316    --FND_FILE.PUT_LINE(FND_FILE.LOG,' Ont source code- after trunc: '|| v_ont_source_code);
5317    if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
5318         FND_LOG.STRING(G_LEVEL_PROCEDURE,
5319                        G_MODULE_NAME || l_api_name || '.IN',
5320                        G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
5321                        ||' Ont source code- after trunc ='||v_ont_source_code
5322                        );
5323    end if;
5324    /*
5325     return if it is discount line
5326    */
5327    if nvl(pr_new.interface_line_attribute11,'0') <> '0' then
5328      --FND_FILE.PUT_LINE(FND_FILE.LOG,' Int. line att1: '||pr_new.interface_line_attribute11);
5329      if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
5330           FND_LOG.STRING(G_LEVEL_PROCEDURE,
5331                          G_MODULE_NAME || l_api_name || '.IN',
5332                          G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
5333                          ||' Int. line att1 ='||pr_new.interface_line_attribute11
5334                          );
5335      end if;
5336      return;
5337    end if;
5338 
5339    for tax_rec in  SO_RMA_TAX_LINES_INFO
5340    Loop
5341      --FND_FILE.PUT_LINE(FND_FILE.LOG,' cursor SO_RMA_TAX_LINES_INFO');
5342      --FND_FILE.PUT_LINE(FND_FILE.LOG,' Tax type '|| tax_rec.tax_type);
5343      if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
5344           FND_LOG.STRING(G_LEVEL_PROCEDURE,
5345                          G_MODULE_NAME || l_api_name || '.IN',
5346                          G_PKG_NAME || ': ' || l_api_name || '().'||' IN cursor SO_RMA_TAX_LINES_INFO:'
5347                          ||' Tax type ='||tax_rec.tax_type
5348                          );
5349      end if;
5350 
5351      if lv_register_code = 'BOND_REG' then
5352        --FND_FILE.PUT_LINE(FND_FILE.LOG,' Register code '|| lv_register_code);
5353        if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
5354             FND_LOG.STRING(G_LEVEL_PROCEDURE,
5355                            G_MODULE_NAME || l_api_name || '.IN',
5356                            G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
5357                            ||' Register code ='||lv_register_code
5358                            );
5359        end if;
5360      if upper(tax_rec.tax_type) not in ('EXCISE','OTHER EXCISE','CVD_EDUCATION_CESS','EXCISE_EDUCATION_CESS', jai_constants.tax_type_sh_cvd_edu_cess, jai_constants.tax_type_sh_exc_edu_cess) then
5361 
5362      INSERT INTO JAI_AR_TRX_TAX_LINES
5363               (
5364                tax_line_no                 ,
5365                customer_trx_line_id        ,
5366                link_to_cust_trx_line_id    ,
5367                precedence_1                ,
5368                precedence_2                ,
5369                precedence_3                ,
5370                precedence_4                ,
5371                precedence_5                ,
5372                precedence_6                ,
5373                precedence_7                ,
5374                precedence_8                ,
5375                precedence_9                ,
5376                precedence_10                ,
5377          tax_id                      ,
5378                tax_rate                    ,
5379                qty_rate                    ,
5380                uom                         ,
5381                tax_amount                  ,
5382                func_tax_amount             ,
5383                base_tax_amount             ,
5384                creation_date               ,
5385                created_by                  ,
5386                last_update_date            ,
5387                last_updated_by             ,
5388                last_update_login
5389               )
5390           VALUES
5391               (
5392               tax_rec.tax_line_no               ,
5393               ra_customer_trx_lines_s.nextval   ,
5394               v_customer_trx_line_id            ,
5395               tax_rec.precedence_1              ,
5396               tax_rec.precedence_2              ,
5397               tax_rec.precedence_3              ,
5398               tax_rec.precedence_4              ,
5399               tax_rec.precedence_5              ,
5400               tax_rec.precedence_6              ,
5401               tax_rec.precedence_7              ,
5402               tax_rec.precedence_8              ,
5403               tax_rec.precedence_9              ,
5404               tax_rec.precedence_10              ,
5405         tax_rec.tax_id                    ,
5406               tax_rec.tax_rate                  ,
5407               tax_rec.qty_rate                  ,
5408               tax_rec.uom                       ,
5409               -round(tax_rec.tax_amount,2)       ,
5410               -round(tax_rec.func_tax_amount,2)  ,
5411               -round(tax_rec.base_tax_amount,2)  ,
5412               sysdate                           ,
5413               fnd_global.user_id                ,
5414               sysdate                           ,
5415               fnd_global.user_id                ,
5416               fnd_global.login_id
5417              );
5418         end if;
5419      else
5420 
5421         INSERT INTO JAI_AR_TRX_TAX_LINES
5422                  (
5423                   tax_line_no                 ,
5424                   customer_trx_line_id        ,
5425                   link_to_cust_trx_line_id    ,
5426                   precedence_1                ,
5427                   precedence_2                ,
5428                   precedence_3                ,
5429                   precedence_4                ,
5430                   precedence_5                ,
5431                   precedence_6                ,
5432                   precedence_7                ,
5433                   precedence_8                ,
5434                   precedence_9                ,
5435                   precedence_10                ,
5436                   tax_id                      ,
5437                   tax_rate                    ,
5438                   qty_rate                    ,
5439                   uom                         ,
5440                   tax_amount                  ,
5441                   func_tax_amount             ,
5442                   base_tax_amount             ,
5443                   creation_date               ,
5444                   created_by                  ,
5445                   last_update_date            ,
5446                   last_updated_by             ,
5447                   last_update_login
5448                  )
5449                   VALUES
5450                  (
5451                   tax_rec.tax_line_no               ,
5452                   ra_customer_trx_lines_s.nextval   ,
5453                   v_customer_trx_line_id            ,
5454                   tax_rec.precedence_1              ,
5455                   tax_rec.precedence_2              ,
5456                   tax_rec.precedence_3              ,
5457                   tax_rec.precedence_4              ,
5458                   tax_rec.precedence_5              ,
5459                   tax_rec.precedence_6              ,
5460                   tax_rec.precedence_7              ,
5461                   tax_rec.precedence_8              ,
5462                   tax_rec.precedence_9              ,
5463                   tax_rec.precedence_10              ,
5464                   tax_rec.tax_id                    ,
5465                   tax_rec.tax_rate                  ,
5466                   tax_rec.qty_rate                  ,
5467                   tax_rec.uom                       ,
5468                   -round(tax_rec.tax_amount,2)       ,
5469                   -round(tax_rec.func_tax_amount,2)  ,
5470                   -round(tax_rec.base_tax_amount,2)  ,
5471                   sysdate                           ,
5472                   fnd_global.user_id                ,
5473                   sysdate                           ,
5474                   fnd_global.user_id                ,
5475                   fnd_global.login_id
5476                );
5477 
5478      end if;
5479    end loop;
5480 
5481 
5482    open   SO_RMA_LINES_INFO;
5483    fetch  SO_RMA_LINES_INFO into rec_so_lines;
5484    close  SO_RMA_LINES_INFO;
5485 
5486    open   SO_RMA_TAX_LINES_INFO;
5487    fetch  SO_RMA_TAX_LINES_INFO into rec_so_taxes;
5488    close  SO_RMA_TAX_LINES_INFO;
5489 
5490 
5491     ln_tcs_exists  := null;
5492     open c_chk_rgm_tax_exists ( cp_regime_code        => jai_constants.tcs_regime
5493                               , cp_cust_trx_line_id   => v_customer_trx_line_id
5494                                 );
5495     fetch c_chk_rgm_tax_exists into ln_tcs_exists;
5496     close c_chk_rgm_tax_exists ;
5497 
5498   if ln_tcs_exists > 0 then
5499          /* TCS type of tax is present */
5500          open  c_get_regime_id (cp_regime_code => jai_constants.tcs_regime);
5501          fetch c_get_regime_id into ln_tcs_regime_id;
5502          close c_get_regime_id;
5503          /* Find out what is the current slab */
5504          jai_rgm_thhold_proc_pkg.get_threshold_slab_id
5505                                   (
5506                                       p_regime_id         =>    ln_tcs_regime_id
5507                                     , p_organization_id   =>    ln_inv_orgn_id
5508                                     , p_party_type        =>    jai_constants.party_type_customer
5509                                     , p_party_id          =>    lr_trx_rec.customer_id
5510                                     , p_org_id            =>    pr_new.org_id
5511                                     , p_source_trx_date   =>    lr_trx_rec.trx_date
5512                                     , p_threshold_slab_id =>    ln_threshold_slab_id
5513                                     , p_process_flag      =>    lv_process_flag
5514                                     , p_process_message   =>    lv_process_message
5515                                   );
5516 
5517        if lv_process_flag <> jai_constants.successful then
5518           app_exception.raise_exception
5519                         (exception_type   =>    'APP'
5520                         ,exception_code   =>    -20275
5521                         ,exception_text   =>    lv_process_message
5522                         );
5523         end if;
5524         if ln_threshold_slab_id is not null then
5525         /* Threshold level is up.  Surcharge needs to be defaulted , so find out the tax category based on the threshold slab */
5526           jai_rgm_thhold_proc_pkg.get_threshold_tax_cat_id
5527                                     (
5528                                        p_threshold_slab_id    =>    ln_threshold_slab_id
5529                                     ,  p_org_id               =>    pr_new.org_id
5530                                     ,  p_threshold_tax_cat_id =>    ln_threshold_tax_cat_id
5531                                     ,  p_process_flag         =>    lv_process_flag
5532                                     ,  p_process_message      =>    lv_process_message
5533                                     );
5534           if lv_process_flag <> jai_constants.successful then
5535             app_exception.raise_exception
5536                           (exception_type   =>    'APP'
5537                           ,exception_code   =>    -20275
5538                           ,exception_text   =>    lv_process_message
5539                           );
5540           end if;
5541           /* Get line number after which threshold taxes needs to be defaulted */
5542           select max(tax_line_no)
5543           into   ln_last_line_no
5544           from   JAI_AR_TRX_TAX_LINES
5545           where  link_to_cust_trx_line_id = v_customer_trx_line_id;
5546           /* Get line number of the base tax (tax_type=TCS) for calculating the surcharge basically to set a precedence */
5547           select max(tax_line_no)
5548           into  ln_base_line_no
5549           from  JAI_AR_TRX_TAX_LINES jrctt
5550               , JAI_CMN_TAXES_ALL jtc
5551           where jrctt.link_to_cust_trx_line_id  = v_customer_trx_line_id
5552           and   jrctt.tax_id    = jtc.tax_id
5553           and   jtc.tax_type    = jai_constants.tax_type_tcs;
5554           /*
5555           ||Call the helper method to default surcharge taxes on top of the SO taxes  using the tax category
5556           || The api jai_rgm_thhold_proc_pkg.default_thhold_taxes inserts lines as per the same specified in the TCS tax category
5557           || into the ja_in_so_picking_tax_lines table
5558           */
5559           jai_rgm_thhold_proc_pkg.default_thhold_taxes
5560                                     (
5561                                       p_source_trx_id         => ''
5562                                     , p_source_trx_line_id    => v_customer_trx_line_id
5563                                     , p_source_event          => jai_constants.bill_only_invoice
5564                                     , p_action                => jai_constants.default_taxes
5565                                     , p_threshold_tax_cat_id  => ln_threshold_tax_cat_id
5566                                     , p_tax_base_line_number  => ln_base_line_no
5567                                     , p_last_line_number      => ln_last_line_no
5568                                     , p_currency_code         => lr_trx_rec.invoice_currency_code
5569                                     , p_currency_conv_rate    => lr_trx_rec.exchange_rate
5570                                     , p_quantity              => rec_so_lines.quantity
5571                                     , p_base_tax_amt          => NVL(pr_new.quantity_credited * pr_new.unit_selling_price, nvl(pr_new.extended_amount,0))
5572                                     , p_assessable_value      => rec_so_lines.selling_price * rec_so_lines.quantity
5573                                     , p_inventory_item_id     => rec_so_lines.inventory_item_id
5574                                     , p_uom_code              =>rec_so_lines.uom
5575                                     , p_vat_assessable_value  => rec_so_lines.selling_price * rec_so_lines.quantity
5576                                     , p_process_flag          => lv_process_flag
5577                                     , p_process_message       => lv_process_message
5578                                     );
5579 
5580           if lv_process_flag <> jai_constants.successful then
5581             app_exception.raise_exception
5582                           (exception_type   =>    'APP'
5583                           ,exception_code   =>    -20275
5584                           ,exception_text   =>    lv_process_message
5585                           );
5586           end if;
5587         end if; /* ln_threshold_slab_id is not null then */
5588       end if;  /** ln_tcs_exists is not null then  */
5589 
5590 
5591 
5592       open get_ar_tax_amount;
5593       FETCH get_ar_tax_amount INTO ln_ar_tax_amount;
5594       CLOSE get_ar_tax_amount;
5595 
5596 
5597 
5598 
5599       INSERT INTO JAI_AR_TRX_LINES (
5600                          customer_trx_line_id,
5601                          line_number,
5602                          customer_trx_id,
5603                          description,
5604                          inventory_item_id,
5605                          unit_code,
5606                          quantity,
5607                          tax_category_id,
5608                          auto_invoice_flag,
5609                          unit_selling_price,
5610                          line_amount,
5611                          tax_amount,
5612                          total_amount,
5613                          assessable_value,
5614                          creation_date,
5615                          created_by,
5616                          last_update_date,
5617                          last_updated_by,
5618                          last_update_login,
5619                          service_type_code
5620                                               )
5621                                       VALUES  (
5622                                                pr_new.customer_trx_line_id                    ,
5623                                                pr_new.line_number                             ,
5624                                                pr_new.customer_trx_id                         ,
5625                                                pr_new.description                             ,
5626                                                pr_new.inventory_item_id                       ,
5627                                                pr_new.uom_code                                ,
5628                                                pr_new.quantity_invoiced                       ,
5629                                                rec_so_lines.tax_category_id                 ,
5630                                                'Y'                                          ,
5631                                                pr_new.unit_selling_price                      ,
5632                                                NVL(pr_new.quantity_credited * pr_new.unit_selling_price, nvl(pr_new.extended_amount,0))    ,
5633                                                nvl(ln_ar_tax_amount,0),
5634                                                NVL(pr_new.quantity_credited * pr_new.unit_selling_price, nvl(pr_new.extended_amount,0)) +  nvl(ln_ar_tax_amount,0)   ,
5635                                                rec_so_lines.assessable_value                ,
5636                                                sysdate                                      ,
5637                                                fnd_global.user_id                           ,
5638                                                sysdate                                      ,
5639                                                fnd_global.user_id                           ,
5640                                                fnd_global.login_id                          ,
5641                                                rec_so_lines.service_type_code
5642                                               );
5643     --Added by Qinglei for JAI Trigger Elimination 11-May-2012 begin
5644     OPEN c_jai_ar_trx_lines(pr_new.customer_trx_id,pr_new.customer_trx_line_id);
5645     FETCH c_jai_ar_trx_lines into t_jai_line_rec_new;
5646     CLOSE c_jai_ar_trx_lines;
5647 
5648     lv_action := jai_constants.INSERTING;
5649 
5650     JAI_AR_TAX_LINES_PKG.POPULATE_TAX_LINES_WRAPPER (
5651                 pr_old            =>  NULL         ,
5652                 pr_new            =>  t_jai_line_rec_new         ,
5653                 pv_action         =>  lv_action         ,
5654                 pv_return_code    =>  lv_return_code    ,
5655                 pv_return_message =>  lv_return_message
5656               );
5657     IF lv_return_code <> jai_constants.successful   then
5658         RAISE le_error;
5659     END IF;
5660     --Added by Qinglei for JAI Trigger Elimination 11-May-2012 end
5661 
5662     open  c_get_amounts;
5663     fetch c_get_amounts into ln_tax_amount, ln_line_amount ;
5664     close c_get_amounts;
5665 
5666     /*FND_FILE.PUT_LINE(FND_FILE.LOG,' Cursor c_get_amounts');
5667     FND_FILE.PUT_LINE(FND_FILE.LOG,' Tax amount '|| ln_tax_amount);
5668     FND_FILE.PUT_LINE(FND_FILE.LOG,' Line amount '|| ln_line_amount);*/
5669     if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
5670             FND_LOG.STRING(G_LEVEL_PROCEDURE,
5671                            G_MODULE_NAME || l_api_name || '.IN',
5672                            G_PKG_NAME || ': ' || l_api_name || '().'||' IN Cursor c_get_amounts:'
5673                            ||' Tax amount ='||ln_tax_amount
5674                            ||' Line amount ='||ln_line_amount
5675                            );
5676     end if;
5677     --Added by Zhiwei for JAI Trigger elimination begin
5678     ---------------------------------------------------------
5679     open c_get_rec(v_header_id);
5680     fetch c_get_rec into t_rec_old;
5681     close c_get_rec;
5682     ---------------------------------------------------------
5683     --Added by Zhiwei for JAI Trigger elimination end
5684 
5685 
5686     update  JAI_AR_TRXS
5687     set     tax_amount   = nvl(ln_tax_amount,0) ,
5688             line_amount  = nvl(ln_line_amount,0),
5689             total_amount = nvl(ln_line_amount,0) + nvl(ln_tax_amount,0)
5690     where   customer_trx_id = pr_new.customer_trx_id;
5691 
5692     --Added by Zhiwei for JAI Trigger elimination begin
5693     ---------------------------------------------------------
5694     open c_get_rec(v_header_id);
5695     fetch c_get_rec into t_rec_new;
5696     close c_get_rec;
5697 
5698     if(t_rec_new.once_completed_flag = 'Y')then
5699 
5700          lv_action := jai_constants.updating ;
5701 
5702          JAI_AR_TRXS_PKG.UPDATE_EXCISE_REGISTERS(
5703                         pr_old            =>  t_rec_old         ,
5704                         pr_new            =>  t_rec_new         ,
5705                         pv_action         =>  lv_action         ,
5706                         pv_return_code    =>  lv_return_code    ,
5707                         pv_return_message =>  lv_return_message
5708                       );
5709 
5710           IF lv_return_code <> jai_constants.successful   then
5711              RAISE le_error;
5712           END IF;
5713 
5714     end if;
5715     ---------------------------------------------------------
5716     --Added by Zhiwei for JAI Trigger elimination end
5717 
5718 
5719 
5720     jai_ar_tcs_rep_pkg.ar_accounting
5721                           (    p_ractl             =>  pr_new
5722                             ,  p_process_flag      =>  lv_process_flag
5723                             ,  p_process_message   =>  lv_process_message
5724                           );
5725     if lv_process_flag <> jai_constants.successful then
5726       app_exception.raise_exception
5727                     (exception_type   =>    'APP'
5728                     ,exception_code   =>    -20275
5729                     ,exception_text   =>    lv_process_message
5730                     );
5731     end if;
5732 
5733     if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
5734       FND_LOG.STRING(G_LEVEL_PROCEDURE,
5735                      G_MODULE_NAME || l_api_name || '.END',
5736                      G_PKG_NAME || ': ' || l_api_name || '()-');
5737     end if;
5738 
5739 
5740 end ;
5741 
5742 
5743 
5744   BEGIN
5745 
5746 
5747     if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
5748       FND_LOG.STRING(G_LEVEL_PROCEDURE,
5749                      G_MODULE_NAME || l_api_name || '.BEGIN',
5750                      G_PKG_NAME || ': ' || l_api_name || '()+');
5751     end if;
5752 
5753 
5754     pv_return_code := jai_constants.successful ;
5755 
5756     IF pr_new.interface_line_context IN ('PROJECTS INVOICES','OKS CONTRACTS','LEGACY') THEN
5757       RETURN;
5758     END IF;
5759 
5760 
5761 -------------------------------------------------------------------------------------------------------
5762   OPEN  transaction_type_cur;
5763   FETCH transaction_type_cur INTO v_trans_type;
5764   CLOSE transaction_type_cur;
5765 
5766   IF NVL(v_trans_type,'N') <> 'CM' THEN
5767     RETURN;
5768   END IF;
5769 
5770   OPEN   CREATED_FROM_CUR;
5771   FETCH  CREATED_FROM_CUR INTO v_created_from, v_trx_number, v_batch_source_id, v_books_id,
5772                                v_salesrep_id, c_from_currency_code, c_conversion_type,
5773                                c_conversion_date, c_conversion_rate, ln_legal_entity_id ;
5774   CLOSE  CREATED_FROM_CUR;
5775 
5776   IF v_created_from <> 'RAXTRX' THEN
5777      RETURN;
5778   END IF;
5779 
5780   OPEN   DUPLICATE_HDR_CUR;
5781   FETCH  DUPLICATE_HDR_CUR INTO x;
5782   CLOSE  DUPLICATE_HDR_CUR;
5783 
5784   OPEN  SO_RMA_RECORD_CHECK;
5785   FETCH SO_RMA_RECORD_CHECK INTO v_exist_flag;
5786   CLOSE SO_RMA_RECORD_CHECK;
5787 
5788   if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
5789       FND_LOG.STRING(G_LEVEL_PROCEDURE,
5790                      G_MODULE_NAME || l_api_name || '.IN',
5791                      G_PKG_NAME || ': ' || l_api_name || '().'||' PARAMS INFO:'
5792                      ||' RMA record chk='||v_exist_flag
5793                      );
5794   end if;
5795   --fnd_file.put_line(FND_FILE.LOG, ' RMA record chk:'|| v_exist_flag);
5796 
5797   IF NVL(v_exist_flag,0) <> 1 THEN
5798     RETURN;
5799   END IF;
5800 
5801   IF NVL(x,0) <> 1 THEN
5802 
5803      OPEN  check_reference_existed;
5804      FETCH check_reference_existed
5805      INTO  ln_ref_existed_flag;
5806      CLOSE check_reference_existed;
5807 
5808    IF NVL(ln_ref_existed_flag,0) = 1 THEN
5809 
5810 
5811     OPEN  SO_RMA_HDR_INFO;
5812     FETCH SO_RMA_HDR_INFO INTO v_organization_id, v_location_id;
5813     CLOSE SO_RMA_HDR_INFO;
5814 
5815 
5816     if v_location_id is null then
5817      Begin
5818        Open  c_get_location;
5819        Fetch c_get_location into  v_organization_id, v_location_id;
5820        Close c_get_location;
5821      Exception
5822        -- adding the exception section so that this should not cause control flow
5823        -- to halt.If it hits the exception , the maximum effect is that organization
5824        -- and location id will not get fetched .. that will happen only for
5825        -- rma without refernec .. but for rma with reference it will go thru fine.
5826        When others then
5827          Null;
5828      End;
5829     end if;
5830   ELSE
5831        OPEN  get_non_ship_loc;
5832        FETCH get_non_ship_loc
5833        INTO  v_organization_id, v_location_id;
5834        CLOSE get_non_ship_loc;
5835 
5836   END IF; --NVL(ln_non_ship_flag,0) <> 1
5837 
5838   if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
5839       FND_LOG.STRING(G_LEVEL_PROCEDURE,
5840                      G_MODULE_NAME || l_api_name || '.IN',
5841                      G_PKG_NAME || ': ' || l_api_name || '().'||' PARAMS INFO:'
5842                      ||' before billonly return cursor='||ln_bill_return
5843                      );
5844   end if;
5845   --fnd_file.put_line(fnd_file.log,' before billonly return cursor '|| ln_bill_return);
5846 
5847   ln_bill_return := 0;
5848   open  c_bill_only_invoice(pr_new.customer_trx_line_id, 'R_RMA_FOR_CREDIT_WO_SHIPMENT');
5849   fetch c_bill_only_invoice into ln_bill_return;
5850   close c_bill_only_invoice;
5851 
5852   if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
5853       FND_LOG.STRING(G_LEVEL_PROCEDURE,
5854                      G_MODULE_NAME || l_api_name || '.IN',
5855                      G_PKG_NAME || ': ' || l_api_name || '().'||' PARAMS INFO:'
5856                      ||' after billonly return cursor='||ln_bill_return
5857                      );
5858   end if;
5859   --fnd_file.put_line(fnd_file.log,' after billonly return cursor '|| ln_bill_return);
5860   if ln_bill_return = 1 then
5861      process_bill_only_return; --calling the procedure process_bill_only_return
5862      return;
5863   end if;
5864 
5865 
5866      IF NVL(pr_new.Interface_line_attribute11,'0') = '0'
5867       and (nvl(v_ont_source_code,'ORDER ENTRY') = ltrim(rtrim(pr_new.interface_line_context))
5868           )
5869       or
5870         (nvl(v_ont_source_code,'ORDER ENTRY') <> nvl(ltrim(rtrim(pr_new.interface_line_context)),'$$$')
5871         )
5872       then
5873          INSERT INTO JAI_AR_TRXS
5874                      (
5875                       Customer_Trx_ID,
5876                       Organization_ID,
5877                       Location_ID,
5878                       Trx_Number,
5879                       Update_RG_Flag,
5880                       Once_Completed_Flag,
5881                       Batch_Source_ID,
5882                       Set_Of_Books_ID,
5883                       Primary_Salesrep_ID,
5884                       Invoice_Currency_Code,
5885                       Exchange_Rate_Type,
5886                       Exchange_Date,
5887                       Exchange_Rate,
5888                       Created_From,
5889                       creation_date,
5890                       created_by,
5891                       last_update_date,
5892                       last_updated_by,
5893                       last_update_login,
5894                       legal_entity_id
5895                      )
5896                 VALUES
5897                      (
5898                       v_header_id,
5899                       v_organization_id,
5900                       v_location_id,
5901                       v_trx_number,
5902                       'Y',
5903                       'Y',
5904                       v_batch_source_id,
5905                       v_books_id,
5906                       v_salesrep_id,
5907                       c_from_currency_code,
5908                       c_conversion_type,
5909                       c_conversion_date,
5910                       c_conversion_rate,
5911                       v_created_from,
5912                       v_creation_date,
5913                       v_created_by,
5914                       v_last_update_date,
5915                       v_last_updated_by,
5916                       v_last_update_login,
5917                       ln_legal_entity_id
5918                     );
5919       END If;
5920   END IF;
5921 
5922   OPEN  SO_RMA_TAX_RECORD_CHECK;
5923   FETCH SO_RMA_TAX_RECORD_CHECK INTO v_exist_flag;
5924   CLOSE SO_RMA_TAX_RECORD_CHECK;
5925 
5926 
5927   IF NVL(v_exist_flag,0) <> 1 THEN
5928     RETURN;
5929   END IF;
5930 
5931   OPEN  SO_RMA_QUANTITY_CHECK;
5932   FETCH SO_RMA_QUANTITY_CHECK INTO v_quantity;
5933   CLOSE SO_RMA_QUANTITY_CHECK;
5934 
5935 
5936   OPEN  cur_get_transaction_type_id ;
5937   FETCH cur_get_transaction_type_id INTO ln_transaction_type_id;
5938   CLOSE cur_get_transaction_type_id ;
5939 
5940   jai_cmn_bond_register_pkg.get_register_id(
5941                                       v_organization_id      ,
5942                                       v_location_id          ,
5943                                       ln_transaction_type_id ,
5944                                       'Y'                    ,
5945                                       ln_register_id          ,
5946                                       lv_register_code
5947                                     );
5948 
5949 
5950   FOR Tax_Rec IN SO_RMA_TAX_LINES_INFO LOOP
5951     v_func_tax_amount  := NVL((Tax_Rec.func_tax_amount * v_quantity_credited) / v_quantity,0);
5952 
5953            IF NVL(pr_new.Interface_line_attribute11,'0') ='0'
5954            and (nvl(v_ont_source_code,'ORDER ENTRY') = ltrim(rtrim(pr_new.interface_line_context))
5955                )
5956            or
5957                   (nvl(v_ont_source_code,'ORDER ENTRY') <> nvl(ltrim(rtrim(pr_new.interface_line_context)),'$$$')
5958                   )
5959            then
5960 
5961        INSERT INTO JAI_AR_TRX_TAX_LINES
5962                          (
5963                           tax_line_no,
5964                           customer_trx_line_id,
5965                           link_to_cust_trx_line_id,
5966                           precedence_1,
5967                           precedence_2,
5968                           precedence_3,
5969                           precedence_4,
5970                           precedence_5,
5971                           precedence_6,
5972                           precedence_7,
5973                           precedence_8,
5974                           precedence_9,
5975                           precedence_10,
5976                           tax_id,
5977                           tax_rate,
5978                           qty_rate,
5979                           uom,
5980                           tax_amount,
5981                           func_tax_amount,
5982                           base_tax_amount,
5983                           creation_date,
5984                           created_by,
5985                           last_update_date,
5986                           last_updated_by,
5987                           last_update_login
5988                          )
5989                    VALUES(
5990                           Tax_Rec.tax_line_no,
5991                           ra_customer_trx_lines_s.NEXTVAL,
5992                           v_customer_trx_line_id,
5993                           Tax_Rec.precedence_1,
5994                           Tax_Rec.precedence_2,
5995                           Tax_Rec.precedence_3,
5996                           Tax_Rec.precedence_4,
5997                           Tax_Rec.precedence_5,
5998                           Tax_Rec.precedence_6,
5999                           Tax_Rec.precedence_7,
6000                           Tax_Rec.precedence_8,
6001                           Tax_Rec.precedence_9,
6002                           Tax_Rec.precedence_10,
6003                           Tax_Rec.tax_id,
6004                           Tax_Rec.tax_rate,
6005                           Tax_Rec.qty_rate,
6006                           Tax_Rec.uom,
6007                           NVL((Tax_Rec.tax_amount * v_quantity_credited) / v_quantity,0),
6008                           v_func_tax_amount,
6009                           NVL((Tax_Rec.base_tax_amount * v_quantity_credited) / v_quantity,0),
6010                           v_creation_date,
6011                           v_created_by,
6012                           v_last_update_date,
6013                           v_last_updated_by,
6014                           v_last_update_login
6015                          );
6016            end if;
6017 
6018       v_excise_amount := NVL(v_excise_amount,0) + NVL(v_func_tax_amount,0);
6019   END LOOP;
6020 
6021 
6022   OPEN  DUPLICATE_LINES_CUR;
6023   FETCH DUPLICATE_LINES_CUR INTO v_line_count;
6024   CLOSE DUPLICATE_LINES_CUR;
6025 
6026 
6027   IF NVL(v_line_count,0) <> 1 THEN
6028     OPEN  SO_RMA_LINES_INFO;
6029 
6030     FETCH SO_RMA_LINES_INFO INTO v_tax_category_id, v_assessable_value,v_service_type;
6031     CLOSE SO_RMA_LINES_INFO;
6032 
6033 
6034     OPEN cur_get_cmline_tax_amt;
6035     FETCH cur_get_cmline_tax_amt INTO v_tax_amount;
6036     CLOSE cur_get_cmline_tax_amt ;
6037 
6038 
6039     IF NVL(pr_new.Interface_line_attribute11,'0') ='0'
6040     and (nvl(v_ont_source_code,'ORDER ENTRY') = ltrim(rtrim(pr_new.interface_line_context))
6041            )
6042     or
6043            (nvl(v_ont_source_code,'ORDER ENTRY') <> nvl(ltrim(rtrim(pr_new.interface_line_context)),'$$$')
6044            )
6045     then
6046         v_tax_amount := NVL((v_tax_amount *  abs(v_quantity_credited)) / v_quantity,0);
6047     ELSE
6048         v_tax_amount :=0;
6049     END IF;
6050 
6051 
6052     INSERT INTO JAI_AR_TRX_LINES
6053                 (customer_trx_line_id,
6054                  line_number,
6055                  customer_trx_id,
6056                  description,
6057                  inventory_item_id,
6058                  unit_code,
6059                  quantity,
6060                  tax_category_id,
6061                  auto_invoice_flag,
6062                  unit_selling_price,
6063                  line_amount,
6064                  tax_amount,
6065                  total_amount,
6066                  assessable_value,
6067                  creation_date,
6068                  created_by,
6069                  last_update_date,
6070                  last_updated_by,
6071                  last_update_login,
6072                  service_type_code
6073                 )
6074          VALUES(
6075                 v_customer_trx_line_id,
6076                 pr_new.line_number,
6077                 v_header_id,
6078                 pr_new.description,
6079                 pr_new.inventory_item_id,
6080                 pr_new.uom_code,
6081                 pr_new.quantity_credited,
6082                 v_tax_category_id,
6083                 'Y',
6084                 pr_new.unit_selling_price,
6085                 v_line_amount,
6086                 v_tax_amount,
6087                 (v_line_amount + v_tax_amount),
6088                 v_assessable_value,
6089                 v_creation_date,
6090                 v_created_by,
6091                 v_last_update_date,
6092                 v_last_updated_by,
6093                 v_last_update_login,
6094                 v_service_type
6095                );
6096   --Added by Qinglei for JAI Trigger Elimination 11-May-2012 begin
6097   OPEN c_jai_ar_trx_lines(v_header_id,v_customer_trx_line_id);
6098   FETCH c_jai_ar_trx_lines into t_jai_line_rec_new;
6099   CLOSE c_jai_ar_trx_lines;
6100 
6101   lv_action := jai_constants.INSERTING;
6102 
6103   JAI_AR_TAX_LINES_PKG.POPULATE_TAX_LINES_WRAPPER (
6104               pr_old            =>  NULL         ,
6105               pr_new            =>  t_jai_line_rec_new         ,
6106               pv_action         =>  lv_action         ,
6107               pv_return_code    =>  lv_return_code    ,
6108               pv_return_message =>  lv_return_message
6109             );
6110   IF lv_return_code <> jai_constants.successful   then
6111       RAISE le_error;
6112   END IF;
6113   --Added by Qinglei for JAI Trigger Elimination 11-May-2012 end
6114 
6115   ELSE
6116     OPEN  OLD_CUSTOMER_TRX_ID_CUR;
6117     FETCH OLD_CUSTOMER_TRX_ID_CUR INTO v_old_customer_trx_id;
6118     CLOSE OLD_CUSTOMER_TRX_ID_CUR;
6119 
6120     DELETE  JAI_AR_TRX_INS_LINES_T
6121     WHERE   Customer_Trx_Line_Id = v_customer_trx_line_id;
6122 
6123     IF v_old_customer_trx_id <> v_header_id THEN
6124       --Added by Qinglei for JAI Trigger Elimination 11-May-2012 begin
6125       OPEN c_jai_ar_trx_lines(v_old_customer_trx_id,v_customer_trx_line_id);
6126       FETCH c_jai_ar_trx_lines INTO t_jai_line_rec_old;
6127       CLOSE c_jai_ar_trx_lines;
6128 
6129        UPDATE  JAI_AR_TRX_LINES
6130        SET     Customer_Trx_Id = v_header_id
6131        WHERE   Customer_Trx_Line_Id = v_customer_trx_line_id;
6132 
6133       OPEN c_jai_ar_trx_lines(v_header_id,v_customer_trx_line_id);
6134       FETCH c_jai_ar_trx_lines INTO t_jai_line_rec_new;
6135       CLOSE c_jai_ar_trx_lines;
6136 
6137       lv_action := jai_constants.UPDATING;
6138 
6139       IF ( ( ( t_jai_line_rec_new.AUTO_INVOICE_FLAG <> 'Y'    AND
6140               t_jai_line_rec_old.AUTO_INVOICE_FLAG <> 'Y'
6141              )                                  AND
6142               (t_jai_line_rec_new.Excise_Invoice_No IS NULL)  AND
6143               (t_jai_line_rec_new.payment_Register  IS NULL)  AND
6144               (t_jai_line_rec_new.Excise_Invoice_Date IS NULL)
6145            )                                    OR
6146            (t_jai_line_rec_new.Customer_Trx_Id <> t_jai_line_rec_old.Customer_Trx_Id)
6147          )
6148       THEN
6149          JAI_AR_TAX_LINES_PKG.POPULATE_TAX_LINES_WRAPPER (
6150                                         pr_old            =>  t_jai_line_rec_old,
6151                                         pr_new            =>  t_jai_line_rec_new,
6152                                         pv_action         =>  lv_action,
6153                                         pv_return_code    =>  lv_return_code,
6154                                         pv_return_message =>  lv_return_message
6155                                       );
6156         IF lv_return_code <> jai_constants.successful   then
6157           RAISE le_error;
6158         END IF;
6159       END IF;
6160      --Added by Qinglei for JAI Trigger Elimination 11-May-2012 end
6161 
6162        DELETE   JAI_AR_TRXS
6163        WHERE    customer_trx_id = v_old_customer_trx_id;
6164     END IF;
6165   END IF;
6166 
6167     --Added by Zhiwei for JAI Trigger elimination begin
6168     ---------------------------------------------------------
6169     open c_get_rec(v_header_id);
6170     fetch c_get_rec into t_rec_old;
6171     close c_get_rec;
6172     ---------------------------------------------------------
6173     --Added by Zhiwei for JAI Trigger elimination end
6174 
6175 
6176   UPDATE  JAI_AR_TRXS
6177   SET     line_amount  =  NVL(line_amount, 0 ) + NVL(v_line_amount,0),
6178           tax_amount     =  NVL(tax_amount,0) + NVL(v_excise_amount,0),
6179           total_amount =  NVL(total_amount,0) + NVL(v_line_amount,0) + NVL(v_excise_amount,0)
6180   WHERE   customer_trx_id = v_header_id;
6181 
6182 
6183     --Added by Zhiwei for JAI Trigger elimination begin
6184     ---------------------------------------------------------
6185     open c_get_rec(v_header_id);
6186     fetch c_get_rec into t_rec_new;
6187     close c_get_rec;
6188 
6189     if(t_rec_new.once_completed_flag = 'Y')then
6190 
6191          lv_action := jai_constants.updating ;
6192 
6193          JAI_AR_TRXS_PKG.UPDATE_EXCISE_REGISTERS(
6194                         pr_old            =>  t_rec_old         ,
6195                         pr_new            =>  t_rec_new         ,
6196                         pv_action         =>  lv_action         ,
6197                         pv_return_code    =>  lv_return_code    ,
6198                         pv_return_message =>  lv_return_message
6199                       );
6200 
6201           IF lv_return_code <> jai_constants.successful   then
6202              RAISE le_error;
6203           END IF;
6204 
6205     end if;
6206     ---------------------------------------------------------
6207     --Added by Zhiwei for JAI Trigger elimination end
6208     if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
6209       FND_LOG.STRING(G_LEVEL_PROCEDURE,
6210                      G_MODULE_NAME || l_api_name || '.END',
6211                      G_PKG_NAME || ': ' || l_api_name || '()-');
6212     end if;
6213 
6214    EXCEPTION
6215      WHEN le_error then
6216        Pv_return_code     :=  jai_constants.unexpected_error;
6217        Pv_return_message  := 'Encountered an error in JAI_AR_IMPORT_POPU_PKG.POP_JAI_RMACM  '  || lv_return_message;
6218        fnd_file.put_line(FND_FILE.LOG,'Encountered an error in JAI_AR_IMPORT_POPU_PKG.POP_JAI_RMACM  '  || lv_return_message);
6219 
6220      WHEN OTHERS THEN
6221        Pv_return_code     :=  jai_constants.unexpected_error;
6222        Pv_return_message  := 'Encountered an error in JAI_AR_IMPORT_POPU_PKG.POP_JAI_RMACM  '  || substr(sqlerrm,1,1900);
6223        fnd_file.put_line(FND_FILE.LOG,'Encountered an error in JAI_AR_IMPORT_POPU_PKG.POP_JAI_RMACM  '  || substr(sqlerrm,1,1900));
6224   END pop_jai_rmacm ;
6225 
6226 
6227 
6228   procedure import_projects_taxes
6229           (       r_new         in         ra_customer_trx_lines_all%rowtype
6230               ,   r_old         in         ra_customer_trx_lines_all%rowtype
6231               ,   pv_action     in         varchar2
6232               ,   pv_err_msg    OUT NOCOPY varchar2
6233               ,   pv_err_flg    OUT NOCOPY varchar2
6234           )
6235   is
6236 
6237     cursor c_draft_inv_line_taxes( cpn_draft_invoice_line_id in number ) is
6238       select *
6239       from jai_cmn_document_taxes
6240       where source_doc_type = jai_pa_billing_pkg.gv_source_projects
6241       and source_doc_line_id = cpn_draft_invoice_line_id
6242       order by tax_line_no;
6243 
6244     -- cursor to get transaction type
6245     CURSOR c_transaction_type IS
6246     SELECT A.TYPE
6247     FROM   RA_CUST_TRX_TYPES_ALL A, RA_CUSTOMER_TRX_ALL b
6248     WHERE  A.cust_trx_type_id = b.cust_trx_type_id
6249     AND    b.customer_trx_id = r_new.customer_trx_id
6250     AND    NVL(A.org_id,0) = NVL(r_new.org_id,0);
6251 
6252     r_trx_type  c_transaction_type%rowtype;
6253     ln_quantity                jai_ar_trx_lines.quantity%type;
6254 
6255 
6256     cursor c_ra_customer_trx is
6257       select trx_number, trx_date, batch_source_id, set_of_books_id
6258            , primary_salesrep_id ,invoice_currency_code, exchange_rate_type
6259            , exchange_date, exchange_rate
6260            , legal_entity_id
6261            , complete_flag --Added by Zhiwei for ZX integration of AR
6262       from ra_customer_trx_all
6263       where customer_trx_id = r_new.customer_trx_id;
6264     r_ra_customer_trx       c_ra_customer_trx%rowtype;
6265 
6266     cursor c_jai_ra_customer_trx is
6267       select customer_trx_id
6268       from jai_ar_trxs
6269       where customer_trx_id = r_new.customer_trx_id;
6270     r_jai_ra_customer_trx       c_jai_ra_customer_trx%rowtype;
6271 
6272     cursor c_jai_tax_line(pn_tax_line_no number) is
6273       select customer_trx_line_id
6274       from jai_ar_trx_tax_lines
6275       where 1=1 --customer_trx_id = r_new.customer_trx_id
6276       and   link_to_cust_trx_line_id = r_new.customer_trx_line_id
6277       and   tax_line_no = pn_tax_line_no;
6278     r_jai_tax_line       c_jai_tax_line%rowtype;
6279 
6280     cursor c_jai_ra_customer_trx_line is
6281       select customer_trx_line_id
6282       from jai_ar_trx_lines
6283       where customer_trx_line_id = r_new.customer_trx_line_id;
6284     r_jai_ra_customer_trx_line    c_jai_ra_customer_trx_line%rowtype;
6285 
6286     cursor c_get_project_id(cp_project_code in varchar2) is
6287       select project_id
6288       from pa_projects_all
6289       where segment1 = cp_project_code;
6290 
6291     cursor c_jai_pa_draft_invoice(cpn_project_id in number, cpn_draft_invoice_num in number) is
6292       select draft_invoice_id, organization_id, location_id
6293         , excise_register_type
6294         , excise_invoice_no
6295         , excise_invoice_date
6296         , vat_invoice_no
6297         , vat_invoice_date
6298       from jai_pa_draft_invoices
6299       where project_id = cpn_project_id
6300       and draft_invoice_num = cpn_draft_invoice_num;
6301 
6302     r_jai_pa_draft_invoice      c_jai_pa_draft_invoice%rowtype;
6303 
6304     cursor c_jai_pa_draft_inv_line(
6305         cpn_draft_invoice_id in number,
6306         cpn_line_num in number
6307     ) is
6308       select
6309           draft_invoice_id
6310         , draft_invoice_line_id
6311         , tax_category_id
6312         , service_type_code
6313       from jai_pa_draft_invoice_lines
6314       where draft_invoice_id = cpn_draft_invoice_id
6315       and line_num = cpn_line_num;
6316 
6317     r_jai_pa_draft_inv_line    c_jai_pa_draft_inv_line%rowtype;
6318 
6319 
6320     ln_line_customer_trx_line_id    ra_customer_trx_lines_all.customer_trx_line_id%type;
6321 
6322     ln_project_id           pa_draft_invoices_all.project_id%type;
6323     ln_draft_invoice_num    pa_draft_invoices_all.draft_invoice_num%type;
6324     ln_line_num             pa_draft_invoice_items.line_num%type;
6325     ln_line_amount          number;
6326     ln_tax_amount           number;
6327     ln_func_tax_amount      number;
6328     ln_base_tax_amount      number;
6329 
6330     ln_line_tax_amt            number;
6331 
6332     --Added by Zhiwei for JAI Trigger elimination begin
6333     ---------------------------------------------------------
6334     cursor c_get_rec(cn_customer_trx_id number)
6335     is
6336     select *
6337     from jai_ar_trxs
6338     where customer_trx_id = cn_customer_trx_id;
6339 
6340     t_rec_new jai_ar_trxs%rowtype;
6341     t_rec_old jai_ar_trxs%rowtype;
6342 
6343     lv_action             VARCHAR2(20)  ;
6344     lv_return_message     VARCHAR2(2000);
6345     lv_return_code        VARCHAR2(100) ;
6346     le_error              EXCEPTION     ;
6347 
6348     ---------------------------------------------------------
6349     --Added by Zhiwei for JAI Trigger elimination end
6350   --Added by Qinglei for JAI Trigger Elimination 11-May_2012 begin
6351   t_jai_line_rec_old    JAI_AR_TRX_LINES%ROWTYPE;
6352   t_jai_line_rec_new    JAI_AR_TRX_LINES%ROWTYPE;
6353 
6354   CURSOR c_jai_ar_trx_lines(pn_customer_trx_id NUMBER,pn_customer_trx_line_id NUMBER) IS
6355   SELECT *
6356   FROM
6357          JAI_AR_TRX_LINES
6358   WHERE CUSTOMER_TRX_ID = pn_customer_trx_id
6359   AND customer_trx_line_id = pn_customer_trx_line_id;
6360   --Added by Qinglei for JAI Trigger Elimination 11-May-2012 end
6361   l_api_name CONSTANT VARCHAR2(30) := 'IMPORT_PROJECTS_TAXES()';
6362 
6363   begin
6364 
6365     if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
6366       FND_LOG.STRING(G_LEVEL_PROCEDURE,
6367                      G_MODULE_NAME || l_api_name || '.BEGIN',
6368                      G_PKG_NAME || ': ' || l_api_name || '()+');
6369     end if;
6370 
6371     open  c_transaction_type;
6372     fetch c_transaction_type into r_trx_type;
6373     close c_transaction_type;
6374 
6375     if nvl(r_trx_type.type ,'N') = 'CM' then
6376       ln_quantity := r_new.quantity_credited;
6377     else
6378       ln_quantity := r_new.quantity_invoiced;
6379     end if;
6380 
6381 
6382     ln_line_customer_trx_line_id := r_new.customer_trx_line_id;
6383     ln_line_amount    := round( nvl(ln_quantity * r_new.unit_selling_price, 0), 2); -- Bug 6369471
6384 
6385     open c_get_project_id(r_new.interface_line_attribute1);
6386     fetch c_get_project_id into ln_project_id;
6387     close c_get_project_id;
6388 
6389     ln_draft_invoice_num  := to_number(r_new.interface_line_attribute2);
6390     ln_line_num           := to_number(r_new.interface_line_attribute6);
6391 
6392     open c_jai_pa_draft_invoice(ln_project_id, ln_draft_invoice_num);
6393     fetch c_jai_pa_draft_invoice into r_jai_pa_draft_invoice;
6394     close c_jai_pa_draft_invoice;
6395 
6396     if r_jai_pa_draft_invoice.draft_invoice_id is null then
6397       /* no data exists in JAI_PA tables, so no need to Import taxes from PA to AR  */
6398       return;
6399     end if;
6400 
6401     open c_jai_pa_draft_inv_line(r_jai_pa_draft_invoice.draft_invoice_id, ln_line_num);
6402     fetch c_jai_pa_draft_inv_line into r_jai_pa_draft_inv_line;
6403     close c_jai_pa_draft_inv_line;
6404 
6405     open c_ra_customer_trx;
6406     fetch c_ra_customer_trx into r_ra_customer_trx;
6407     close c_ra_customer_trx;
6408 
6409     open c_jai_ra_customer_trx;
6410     fetch c_jai_ra_customer_trx into r_jai_ra_customer_trx;
6411     close c_jai_ra_customer_trx;
6412 
6413     if r_jai_ra_customer_trx.customer_trx_id is null then
6414       /* insert into jai_ar_trxs */
6415       insert into jai_ar_trxs(
6416             customer_trx_id
6417           , organization_id
6418           , location_id
6419           , trx_number
6420           --, trx_date      --Added by Zhiwei for Trigger Elimination of AutoImport AR
6421           , complete_flag --Added by Zhiwei for Trigger Elimination of AutoImport AR
6422           , update_rg_flag
6423           , update_rg23d_flag
6424           , once_completed_flag
6425           , batch_source_id
6426           , set_of_books_id
6427           , primary_salesrep_id
6428           , invoice_currency_code
6429           , exchange_rate_type
6430           , exchange_date
6431           , exchange_rate
6432           , creation_date
6433           , created_by
6434           , last_update_date
6435           , last_updated_by
6436           , last_update_login
6437           , vat_invoice_no
6438           , vat_invoice_date
6439           , legal_entity_id  -- 6012570
6440       ) values (
6441             r_new.customer_trx_id
6442           , r_jai_pa_draft_invoice.organization_id
6443           , r_jai_pa_draft_invoice.location_id
6444           , r_ra_customer_trx.trx_number
6445           --, r_ra_customer_trx.trx_date        --Added by Zhiwei for Trigger Elimination of AutoImport AR
6446           , r_ra_customer_trx.complete_flag   --Added by Zhiwei for Trigger Elimination of AutoImport AR
6447           , 'Y'
6448           , 'Y'
6449           , 'N'
6450           , r_ra_customer_trx.batch_source_id
6451           , r_ra_customer_trx.set_of_books_id
6452           , r_ra_customer_trx.primary_salesrep_id
6453           , r_ra_customer_trx.invoice_currency_code
6454           , r_ra_customer_trx.exchange_rate_type
6455           , r_ra_customer_trx.exchange_date
6456           , r_ra_customer_trx.exchange_rate
6457           , sysdate
6458           , r_new.created_by
6459           , sysdate
6460           , r_new.last_updated_by
6461           , r_new.last_update_login
6462           , r_jai_pa_draft_invoice.vat_invoice_no
6463           , r_jai_pa_draft_invoice.vat_invoice_date
6464           , r_ra_customer_trx.legal_entity_id
6465       );
6466 
6467     end if;
6468 
6469     /* insert into ja_in_ra_cust_trx_tax_lines */
6470     for tax_rec in c_draft_inv_line_taxes(r_jai_pa_draft_inv_line.draft_invoice_line_id)  loop
6471 
6472       ln_tax_amount         := round(tax_rec.tax_amt,2);
6473       ln_func_tax_amount    := round(tax_rec.func_tax_amt,2);
6474       if tax_rec.tax_rate <> 0 then
6475         ln_base_tax_amount  := tax_rec.tax_amt * 100/ tax_rec.tax_rate;
6476       else
6477         ln_base_tax_amount  := ln_tax_amount;
6478       end if;
6479 
6480     open c_jai_tax_line(tax_rec.tax_line_no);
6481     fetch c_jai_tax_line into r_jai_tax_line;
6482     close c_jai_tax_line;
6483 
6484     if r_jai_tax_line.customer_trx_line_id is null then
6485 
6486       insert into jai_ar_trx_tax_lines(
6487            tax_line_no                   ,
6488            customer_trx_line_id          ,
6489            link_to_cust_trx_line_id      ,
6490            precedence_1                  ,
6491            precedence_2                  ,
6492            precedence_3                  ,
6493            precedence_4                  ,
6494            precedence_5                  ,
6495            precedence_6                  ,
6496            precedence_7                  ,
6497            precedence_8                  ,
6498            precedence_9                  ,
6499            precedence_10                 ,
6500            tax_id                        ,
6501            tax_rate                      ,
6502            qty_rate                      ,
6503            uom                           ,
6504            tax_amount                    ,
6505            func_tax_amount               ,
6506            base_tax_amount               ,
6507            creation_date                 ,
6508            created_by                    ,
6509            last_update_date              ,
6510            last_updated_by               ,
6511            last_update_login
6512       ) values(
6513            tax_rec.tax_line_no                ,
6514            ra_customer_trx_lines_s.nextval    ,
6515            ln_line_customer_trx_line_id       ,
6516            tax_rec.precedence_1               ,
6517            tax_rec.precedence_2               ,
6518            tax_rec.precedence_3               ,
6519            tax_rec.precedence_4               ,
6520            tax_rec.precedence_5               ,
6521            tax_rec.precedence_6               ,
6522            tax_rec.precedence_7               ,
6523            tax_rec.precedence_8               ,
6524            tax_rec.precedence_9               ,
6525            tax_rec.precedence_10              ,
6526            tax_rec.tax_id                     ,
6527            tax_rec.tax_rate                   ,
6528            tax_rec.qty_rate                   ,
6529            tax_rec.uom                        ,
6530            ln_tax_amount                ,
6531            ln_func_tax_amount             ,
6532            ln_base_tax_amount,    /* Complete this round(v_base_tax_amount,2)         , */
6533            sysdate                    ,
6534            r_new.created_by,
6535            sysdate                 ,
6536            r_new.last_updated_by                  ,
6537            r_new.last_update_login
6538       );
6539 
6540       ln_line_tax_amt := nvl(ln_line_tax_amt,0) + ln_tax_amount;
6541       END IF;
6542     end loop;
6543 
6544     /* insert into jai_ar_trx_lines */
6545     open c_jai_ra_customer_trx_line;
6546     fetch c_jai_ra_customer_trx_line into r_jai_ra_customer_trx_line;
6547     close c_jai_ra_customer_trx_line;
6548 
6549     if r_jai_ra_customer_trx_line.customer_trx_line_id is null then
6550       INSERT INTO jai_ar_trx_lines (
6551              customer_trx_line_id                         ,
6552              line_number                                  ,
6553              customer_trx_id                              ,
6554              description                                  ,
6555              payment_register                             ,
6556              excise_invoice_no                            ,
6557              preprinted_excise_inv_no                     ,
6558              excise_invoice_date                          ,
6559              inventory_item_id                            ,
6560              unit_code                                    ,
6561              quantity                                     ,
6562              tax_category_id                              ,
6563              auto_invoice_flag                            ,
6564              unit_selling_price                           ,
6565              line_amount                                  ,
6566              tax_amount                                   ,
6567              total_amount                                 ,
6568              assessable_value                             ,
6569              creation_date                                ,
6570              created_by                                   ,
6571              last_update_date                             ,
6572              last_updated_by                              ,
6573              last_update_login                            ,
6574              excise_exempt_type                           ,
6575              excise_exempt_refno                          ,
6576              excise_exempt_date                           ,
6577              ar3_form_no                                  ,
6578              ar3_form_date                                ,
6579              vat_exemption_flag                           ,
6580              vat_exemption_type                           ,
6581              vat_exemption_date                           ,
6582              vat_exemption_refno                          ,
6583              vat_assessable_value             ,
6584              service_type_code
6585       ) VALUES  (
6586              ln_line_customer_trx_line_id                   ,
6587              r_new.line_number                              ,
6588              r_new.customer_trx_id                          ,
6589              r_new.description                              ,
6590              r_jai_pa_draft_invoice.excise_register_type    ,
6591              r_jai_pa_draft_invoice.excise_invoice_no       ,
6592              null                                           ,       -- v_preprinted_excise_inv_no                   ,
6593              r_jai_pa_draft_invoice.excise_invoice_date     ,
6594              r_new.inventory_item_id                        ,
6595              r_new.uom_code                                 ,
6596              ln_quantity                                    ,
6597              r_jai_pa_draft_inv_line.tax_category_id    ,
6598              'Y'                                            ,
6599              r_new.unit_selling_price                       ,
6600              ln_line_amount                                 ,
6601              ln_line_tax_amt                                ,
6602              (ln_line_amount + ln_line_tax_amt)             ,
6603              null                                           ,       -- v_assessable_value                           ,
6604              sysdate                                        ,
6605              r_new.created_by                               ,
6606              sysdate                                        ,
6607              r_new.last_updated_by                          ,
6608              r_new.last_update_login                        ,
6609              null                                           ,       -- v_excise_exempt_type                         ,
6610              null                                           ,       -- v_excise_exempt_refno                        ,
6611              null                                           ,       -- v_excise_exempt_date                         ,
6612              null                                           ,       -- v_ar3_form_no                                ,
6613              null                                           ,       -- v_ar3_form_date                              ,
6614              null                                           ,       -- lv_vat_exemption_flag                        ,
6615              null                                           ,       -- lv_vat_exemption_type                        ,
6616              null                                           ,       -- lv_vat_exemption_date                        ,
6617              null                                           ,       -- lv_vat_exemption_refno                       ,
6618              null                                           ,        -- ln_vat_assessable_value
6619              r_jai_pa_draft_inv_line.service_type_code
6620       );
6621 
6622        --Added by Qinglei for JAI Trigger Elimination 11-May-2012 begin
6623       OPEN c_jai_ar_trx_lines(r_new.customer_trx_id,ln_line_customer_trx_line_id);
6624       FETCH c_jai_ar_trx_lines into t_jai_line_rec_new;
6625       CLOSE c_jai_ar_trx_lines;
6626 
6627       lv_action := jai_constants.INSERTING;
6628 
6629       JAI_AR_TAX_LINES_PKG.POPULATE_TAX_LINES_WRAPPER (
6630                   pr_old            =>  NULL         ,
6631                   pr_new            =>  t_jai_line_rec_new         ,
6632                   pv_action         =>  lv_action         ,
6633                   pv_return_code    =>  lv_return_code    ,
6634                   pv_return_message =>  lv_return_message
6635                 );
6636       IF lv_return_code <> jai_constants.successful   then
6637           RAISE le_error;
6638       END IF;
6639       --Added by Qinglei for JAI Trigger Elimination 11-May-2012 end
6640       --Added by Zhiwei for JAI Trigger elimination begin
6641       ---------------------------------------------------------
6642       open c_get_rec(r_new.customer_trx_id);
6643       fetch c_get_rec into t_rec_old;
6644       close c_get_rec;
6645       ---------------------------------------------------------
6646       --Added by Zhiwei for JAI Trigger elimination end
6647 
6648 
6649       update  jai_ar_trxs
6650       set     tax_amount   = nvl(tax_amount,0) + round( nvl(ln_line_tax_amt,0), 2)
6651               , line_amount  = nvl(line_amount,0) + round( nvl(ln_line_amount,0), 2)
6652               , total_amount = nvl(total_amount,0) + round(nvl(ln_line_amount,0) + nvl(ln_line_tax_amt,0), 2)
6653               , last_updated_by = r_new.last_updated_by
6654               , last_update_date = sysdate
6655               , last_update_login = r_new.last_update_login
6656       where   customer_trx_id = r_new.customer_trx_id;
6657 
6658     --Added by Zhiwei for JAI Trigger elimination begin
6659     ---------------------------------------------------------
6660     open c_get_rec(r_new.customer_trx_id);
6661     fetch c_get_rec into t_rec_new;
6662     close c_get_rec;
6663 
6664     if(t_rec_new.once_completed_flag = 'Y')then
6665 
6666          lv_action := jai_constants.updating ;
6667 
6668          JAI_AR_TRXS_PKG.UPDATE_EXCISE_REGISTERS(
6669                         pr_old            =>  t_rec_old         ,
6670                         pr_new            =>  t_rec_new         ,
6671                         pv_action         =>  lv_action         ,
6672                         pv_return_code    =>  lv_return_code    ,
6673                         pv_return_message =>  lv_return_message
6674                       );
6675 
6676           IF lv_return_code <> jai_constants.successful   then
6677              RAISE le_error;
6678           END IF;
6679 
6680     end if;
6681     ---------------------------------------------------------
6682     --Added by Zhiwei for JAI Trigger elimination end
6683 
6684 
6685     end if;
6686 
6687     if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
6688       FND_LOG.STRING(G_LEVEL_PROCEDURE,
6689                      G_MODULE_NAME || l_api_name || '.END',
6690                      G_PKG_NAME || ': ' || l_api_name || '()-');
6691     end if;
6692 
6693     --Added by Zhiwei for ZX prototype begin
6694     EXCEPTION
6695      WHEN le_error then
6696           pv_err_flg     :=  jai_constants.unexpected_error;
6697           pv_err_msg  := 'Encountered an error in JAI_AR_IMPORT_POPU_PKG.import_project_taxes  '  || lv_return_message;
6698           fnd_file.put_line(FND_FILE.LOG,'Encountered an error in JAI_AR_IMPORT_POPU_PKG.import_project_taxes  '  || lv_return_message);
6699 
6700      WHEN OTHERS THEN
6701           pv_err_flg     :=  jai_constants.unexpected_error;
6702           pv_err_msg  := 'Encountered an error in JAI_AR_IMPORT_POPU_PKG.import_project_taxes  '  || substr(sqlerrm,1,1900);
6703           fnd_file.put_line(FND_FILE.LOG,'Encountered an error in JAI_AR_IMPORT_POPU_PKG.import_project_taxes  '  || substr(sqlerrm,1,1900));
6704 
6705     --Added by Zhiwei for ZX prototype end
6706 
6707   end import_projects_taxes;
6708 
6709 
6710   function is_this_projects_context(
6711     pv_context          IN  varchar2
6712   ) return boolean is
6713 
6714     lb_return_value boolean;
6715 
6716   begin
6717 
6718     if upper(pv_context) in ('PROJECTS INVOICES', 'PA INVOICES' ) then
6719 
6720       lb_return_value := true;
6721     else
6722       lb_return_value := false;
6723     end if;
6724 
6725     return lb_return_value;
6726 
6727   end is_this_projects_context;
6728 
6729   FUNCTION get_legal_entity_id(pn_customer_trx_id number) RETURN NUMBER
6730   IS
6731 
6732     CURSOR cur_legal_entity_id IS
6733     SELECT legal_entity_id
6734     FROM   RA_CUSTOMER_TRX_ALL
6735     WHERE  customer_trx_id = pn_customer_trx_id;
6736 
6737     ln_legal_entity_id NUMBER ;
6738 
6739     --Added by Zhiwei for JAI Trigger elimination begin
6740     ---------------------------------------------------------
6741     cursor c_get_rec(cn_customer_trx_id number)
6742     is
6743     select *
6744     from jai_ar_trxs
6745     where customer_trx_id = cn_customer_trx_id;
6746 
6747     t_rec_new jai_ar_trxs%rowtype;
6748     t_rec_old jai_ar_trxs%rowtype;
6749 
6750     lv_action             VARCHAR2(20)  ;
6751     lv_return_message     VARCHAR2(2000);
6752     lv_return_code        VARCHAR2(100) ;
6753     le_error              EXCEPTION     ;
6754 
6755     ---------------------------------------------------------
6756     --Added by Zhiwei for JAI Trigger elimination end
6757     --Added by Qinglei for JAI Trigger Elimination 11-May_2012 begin
6758     t_jai_line_rec_old    JAI_AR_TRX_LINES%ROWTYPE;
6759     t_jai_line_rec_new    JAI_AR_TRX_LINES%ROWTYPE;
6760 
6761     CURSOR c_jai_ar_trx_lines(pn_customer_trx_id NUMBER,pn_customer_trx_line_id NUMBER) IS
6762     SELECT *
6763     FROM
6764            JAI_AR_TRX_LINES
6765     WHERE CUSTOMER_TRX_ID = pn_customer_trx_id
6766     AND customer_trx_line_id = pn_customer_trx_line_id;
6767     --Added by Qinglei for JAI Trigger Elimination 11-May-2012 end
6768     l_api_name CONSTANT VARCHAR2(30) := 'GET_LEGAL_ENTITY_ID()';
6769   BEGIN
6770 
6771       --pv_return_code := jai_constants.successful ;
6772       if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
6773         FND_LOG.STRING(G_LEVEL_PROCEDURE,
6774                        G_MODULE_NAME || l_api_name || '.BEGIN',
6775                        G_PKG_NAME || ': ' || l_api_name || '()+');
6776       end if;
6777       --FND_FILE.PUT_LINE(FND_FILE.LOG, '  Function get_legal_entity_id');
6778 
6779       OPEN  cur_legal_entity_id ;
6780       FETCH cur_legal_entity_id INTO ln_legal_entity_id ;
6781       CLOSE cur_legal_entity_id ;
6782 
6783       if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
6784         FND_LOG.STRING(G_LEVEL_PROCEDURE,
6785                        G_MODULE_NAME || l_api_name || '.IN',
6786                        G_PKG_NAME || ': ' || l_api_name || '().'||'PARAMS INFO:'
6787                        ||' Legal Entity Id ='||ln_legal_entity_id
6788                        );
6789       end if;
6790 
6791       --FND_FILE.PUT_LINE(FND_FILE.LOG, ' Legal Entity Id: '|| ln_legal_entity_id);
6792       if (G_LEVEL_PROCEDURE >= G_CURRENT_RUNTIME_LEVEL) then
6793         FND_LOG.STRING(G_LEVEL_PROCEDURE,
6794                        G_MODULE_NAME || l_api_name || '.END',
6795                        G_PKG_NAME || ': ' || l_api_name || '()-');
6796       end if;
6797       RETURN ln_legal_entity_id ;
6798 
6799   END get_legal_entity_id ;
6800 
6801   /* Function to get the service_type*/
6802 
6803   FUNCTION get_service_type(pn_party_id      NUMBER,
6804                             pn_party_site_id NUMBER,
6805                             pv_party_type    VARCHAR2) return VARCHAR2
6806   IS
6807   v_service_type VARCHAR2(30);
6808   ln_address_id   NUMBER;
6809 
6810   CURSOR c_get_address IS
6811   SELECT hzcas.cust_acct_site_id
6812     FROM hz_cust_site_uses_all         hzcsu ,
6813          hz_cust_acct_sites_all        hzcas
6814    WHERE hzcas.cust_acct_site_id   =   hzcsu.cust_acct_site_id
6815      AND hzcsu.site_use_id         =   pn_party_site_id
6816      AND hzcas.cust_account_id     =   pn_party_id ;
6817 
6818   CURSOR cur_get_ser_type_customer(pn_cust_id NUMBER,pn_address_id NUMBER) IS
6819   SELECT service_type_code
6820     FROM JAI_CMN_CUS_ADDRESSES
6821    WHERE customer_id  = pn_cust_id
6822      AND address_id   = pn_address_id;
6823 
6824   CURSOR cur_get_ser_type_vendor(
6825                                    cp_vendor_id IN Po_Headers_All.vendor_id%type,
6826                                    cp_vendor_site_id IN Po_Headers_All.vendor_site_id%type
6827                                  )
6828       IS
6829   SELECT service_type_code
6830     FROM JAI_CMN_VENDOR_SITES
6831    WHERE vendor_id      = cp_vendor_id
6832      AND vendor_site_id = cp_vendor_site_id;
6833 
6834   /*
6835   ||This function is used to retreive the service type based on the customer id and address id
6836   ||passed as parameters.
6837   */
6838 
6839   BEGIN
6840     IF pv_party_type = jai_constants.party_type_customer THEN
6841 
6842       OPEN   c_get_address;
6843       FETCH  c_get_address INTO ln_address_id;
6844       CLOSE  c_get_address;
6845 
6846       OPEN cur_get_ser_type_customer(pn_party_id,ln_address_id);
6847       FETCH cur_get_ser_type_customer INTO v_service_type;
6848       CLOSE cur_get_ser_type_customer;
6849 
6850       IF v_service_type is null THEN
6851 
6852         OPEN cur_get_ser_type_customer(pn_party_id,0);
6853         FETCH cur_get_ser_type_customer INTO v_service_type;
6854         CLOSE cur_get_ser_type_customer;
6855 
6856       END IF;
6857 
6858     ELSIF pv_party_type = jai_constants.party_type_vendor THEN
6859 
6860       OPEN  cur_get_ser_type_vendor(pn_party_id, pn_party_site_id);
6861       FETCH cur_get_ser_type_vendor INTO v_service_type;
6862       CLOSE cur_get_ser_type_vendor;
6863 
6864       IF v_service_type IS NULL THEN
6865         OPEN cur_get_ser_type_vendor(pn_party_id, 0);
6866         FETCH cur_get_ser_type_vendor INTO v_service_type;
6867         CLOSE cur_get_ser_type_vendor;
6868       END IF;
6869 
6870     END IF;
6871 
6872     return v_service_type;
6873 
6874 END;
6875 
6876 END JAI_AR_IMPORT_POPU_PKG;