DBA Data[Home] [Help]

PACKAGE BODY: APPS.JAI_AR_TCS_REP_PKG

Source


1 package body jai_ar_tcs_rep_pkg AS
2 /* $Header: jai_tcs_repo_pkg.plb 120.30.12020000.2 2013/03/18 06:02:20 zxin ship $ */
3 
4 
5   /** Package level variables used in debug package*/
6   lv_object_name      jai_cmn_debug_contexts.LOG_CONTEXT%TYPE DEFAULT 'TCS.JAI_AR_TCS_REP_PKG';
7   lv_member_name      jai_cmn_debug_contexts.LOG_CONTEXT%TYPE;
8   lv_context          jai_cmn_debug_contexts.LOG_CONTEXT%TYPE;
9   /****
10   || Get the primary key
11   || for the table jai_rgm_refs_all
12   *****/
13   CURSOR cur_get_trx_ref_id
14   IS
15   SELECT
16           jai_rgm_refs_all_s1.nextval
17   FROM
18           dual;
19 
20   /****
21   || Get the primary key
22   || for the table jai_rgm_taxes
23   *****/
24   CURSOR cur_get_tax_det_id
25   IS
26   SELECT
27           jai_rgm_taxes_s.nextval
28   FROM
29           dual;
30 
31   /*
32   ||Get the parent reference_id of source document , This gives the last line of the source document. (needs to be discussed )
33   */
34   CURSOR cur_get_parent_transaction  ( cp_source_document_id    JAI_RGM_REFS_ALL.SOURCE_DOCUMENT_ID%TYPE   ,
35                                        cp_source_document_type  JAI_RGM_REFS_ALL.SOURCE_DOCUMENT_TYPE%TYPE
36                                      )
37   IS
38   SELECT
39         max(transaction_id) parent_transaction_id
40   FROM
41         jai_rgm_refs_all
42   WHERE
43         source_document_id    = cp_source_document_id
44   AND   source_document_type  = cp_source_document_type;
45 
46   /*
47   || Generate the transaction_id from the sequence
48   */
49   CURSOR cur_get_transaction_id
50   IS
51   SELECT
52           jai_rgm_refs_all_s2.nextval
53   FROM
54           dual;
55 
56  ln_event              VARCHAR2(100); /*package private variable*/
57  ln_transaction_id     JAI_RGM_REFS_ALL.TRANSACTION_ID%TYPE      ;
58 
59  PROCEDURE set_debug_context
60  IS
61  BEGIN
62    lv_context  := rtrim(lv_object_name || '.'||lv_member_name,'.');
63  END set_debug_context;
64 
65  -- code ported from BL12.1.3 by zhiwei.xin on 15-MAR-2013 begin
66  --Added by Chong for DTC ER 2012/09/11 Get TCS calendar from regime start
67 
68   /*-------------------------------------------------------------------------------------------------------------------------------+
69   | Created By          :  Chong                                                                                                   |
70   | Creation Date       :  11/Sep/2012                                                                                             |
71   | Bug Number/ER Name  :  ZX Dtc                                                                                                  |
72   | SubProgram Name     :  get_tcs_fin_year                                                                                        |
73   | Type                :  PROCEDURE                                                                                               |
74   | Purpose             :  In DTC ER, TCS and TDS financial year defined in regime lever                                           |
75   |                        This funcation get financial year from regime for TDS and TCS module                                    |
76   | TDD Reference       :                                                                                                          |
77   | Assumptions         :                                                                                                          |
78   | Called From         :                                                                                                          |
79   |--------------------------------------------------------------------------------------------------------------------------------|
80   |    parameters                IN/OUT                   Type            Required         Description and Purpose                 |
81   |   ------------              --------                 ------          ----------       -------------------------                |
82   |   cp_attr_code                IN                      varchar2           YES             TDS_CALENDAR / TCS_CALENDAR           |
83   |   cp_rgm_code                 IN                      varchar2           YES             TDS / TCS                             |
84   |   cp_trx_date                 IN                      DATE               YES             transaction date                      |
85   ---------------------------------------------------------------------------------------------------------------------------------*/
86   FUNCTION get_tcs_fin_year(
87                cp_attr_code JAI_RGM_REGISTRATIONS.ATTRIBUTE_CODE%TYPE  --TDS_CALENDAR / TCS_CALENDAR
88              , cp_rgm_code  JAI_RGM_DEFINITIONS.REGIME_CODE%TYPE  --TDS / TCS
89              , cp_trx_date  DATE
90             ) RETURN VARCHAR2
91   IS
92 
93   cursor c_get_tcs_rgm_atrbt
94   is
95   SELECT jrr.ATTRIBUTE_VALUE
96   FROM   JAI_RGM_DEFINITIONS       jrd,
97          JAI_RGM_REGISTRATIONS     jrr
98   WHERE  jrd.REGIME_ID = jrr.REGIME_ID
99   AND    jrr.ATTRIBUTE_CODE = cp_attr_code
100   AND    jrd.REGIME_CODE = cp_rgm_code;
101 
102   -- cursor to get period year from gl_periods
103   CURSOR c_get_period_year(cp_calendar_id      NUMBER,
104                            cp_accounting_date  DATE)
105   IS
106   SELECT a.PERIOD_YEAR
107   FROM   GL_PERIODS a
108        , gl_period_sets b
109   WHERE  a.PERIOD_SET_NAME = b.period_set_name
110   AND    b.period_set_id = cp_calendar_id
111   AND    cp_accounting_date BETWEEN a.START_DATE AND a.END_DATE;
112 
113   lv_gl_calendar_id        number;
114   lv_fin_year              GL_PERIODS.PERIOD_YEAR%TYPE;
115 
116   BEGIN
117 
118     OPEN c_get_tcs_rgm_atrbt;
119     FETCH c_get_tcs_rgm_atrbt INTO lv_gl_calendar_id;
120     CLOSE c_get_tcs_rgm_atrbt;
121 
122     OPEN c_get_period_year(lv_gl_calendar_id,cp_trx_date);
123     FETCH c_get_period_year INTO lv_fin_year;
124     CLOSE c_get_period_year;
125 
126     jai_cmn_utils_pkg.WRITE_FND_LOG(FND_LOG.LEVEL_STATEMENT, 'JAI.PLSQL.JAI_AR_TCS_REP_PKG.get_tcs_fin_year'
127                                    ,'Get ' || cp_rgm_code || ' Finanical year: ' || lv_fin_year);
128     return lv_fin_year;
129 
130   EXCEPTION
131     WHEN OTHERS THEN
132       jai_cmn_utils_pkg.WRITE_FND_LOG(FND_LOG.LEVEL_UNEXPECTED, 'JAI.PLSQL.JAI_AR_TCS_REP_PKG.get_tcs_fin_year.Other_Exception '
133                        , SQLCODE || ':' || SQLERRM
134                        );
135 
136   END get_tcs_fin_year;
137 --Added by Chong for DTC ER 2012/09/11 Get TCS calendar from regime start
138 
139  -- code ported from BL12.1.3 by zhiwei.xin on 15-MAR-2013 end.
140 
141 PROCEDURE wsh_interim_accounting (  p_delivery_id         IN            JAI_OM_WSH_LINES_ALL.DELIVERY_ID%TYPE            ,
142                                     p_delivery_detail_id  IN            JAI_OM_WSH_LINES_ALL.DELIVERY_DETAIL_ID%TYPE     ,
143                                     p_order_header_id     IN            JAI_OM_WSH_LINES_ALL.ORDER_HEADER_ID%TYPE        ,
144                                     p_organization_id     IN            JAI_OM_WSH_LINES_ALL.ORGANIZATION_ID%TYPE        ,
145                                     p_location_id         IN            JAI_OM_WSH_LINES_ALL.LOCATION_ID%TYPE            ,
146                                     p_currency_code       IN            VARCHAR2                                           ,
147                                     p_process_flag        OUT NOCOPY    VARCHAR2                                           ,
148                                     p_process_message     OUT NOCOPY    VARCHAR2
149                                  )
150 
151 IS
152   ln_reg_id           NUMBER;
153   CURSOR cur_get_picking_taxes
154   IS
155   SELECT
156            jsptl.*        ,
157            jrttv.regime_id,
158            jtc.tax_type
159   FROM
160           JAI_OM_WSH_LINES_ALL       jspl ,
161           JAI_OM_WSH_LINE_TAXES   jsptl,
162           JAI_CMN_TAXES_ALL              jtc  ,
163           jai_regime_tax_types_v       jrttv
164   WHERE
165           jspl.delivery_detail_id   = jsptl.delivery_detail_id
166   AND     jspl.delivery_id          = p_delivery_id
167   AND     jspl.delivery_detail_id   = p_delivery_detail_id
168   AND     jsptl.tax_id              = jtc.tax_id
169   AND     jtc.tax_type              = jrttv.tax_type
170   AND     jrttv.regime_code         = jai_constants.tcs_regime;
171 
172   CURSOR cur_get_order_num( cp_hdr_id JAI_OM_WSH_LINES_ALL.ORDER_HEADER_ID%TYPE)
173   IS
174   SELECT
175           order_number
176   FROM
177           oe_order_headers_all
178   WHERE
179           header_id = cp_hdr_id;
180 
181   v_ref_10    GL_INTERFACE.REFERENCE10%TYPE                                                   ;
182   v_std_text  VARCHAR2(50)                     ; -- bug # 3158976
183   v_ref_23    GL_INTERFACE.REFERENCE23%TYPE    ; -- holds the object name -- 'ja_in_wsh_dlry_rg'
184   v_ref_24    GL_INTERFACE.REFERENCE24%TYPE    ; -- holds the table name  -- ' wsh_new_deliveries'
185   v_ref_25    GL_INTERFACE.REFERENCE25%TYPE    ; -- holds the column name -- 'delivery_id'
186   v_ref_26    GL_INTERFACE.REFERENCE26%TYPE                                                   ; -- holds the column value -- eg -- 13645
187 
188   ln_order_number   OE_ORDER_HEADERS_ALL.ORDER_NUMBER%TYPE ;
189   lv_source_name    VARCHAR2(100)                              ;
190   lv_category_name  VARCHAR2(100)                ;
191 
192   v_int_liab_acc_ccid  NUMBER;
193   v_liab_acc_ccid      NUMBER;
194 BEGIN
195   /*########################################################################################################
196   || VARIABLES INITIALIZATION - PART -1
197   ########################################################################################################*/
198    lv_member_name        := 'WSH_INTERIM_ACCOUNTING';
199    v_std_text := 'India Localization Entry for sales order #' ;
200    v_ref_23   := 'jai_ar_tcs_rep_pkg.wsh_interim_accounting';
201    v_ref_24  := 'wsh_new_deliveries';
202    v_ref_25   := 'delivery_id';
203   -- lv_source_name := jai_constants.tcs_source  ;  -- modified by csahoo for bug#6155839
204   -- lv_category_name := 'Receivables India'  ;  -- modified by csahoo for bug#6155839
205 
206  lv_source_name := 'Receivables India'  ; --bug#9587338
207  lv_category_name :=jai_constants.tcs_source; --bug#9587338
208 
209    set_debug_context;
210 
211 
212    /*commented by csahoo for bug# 6401388
213    jai_cmn_debug_contexts_pkg.register ( pv_context => lv_context  ,
214                                 pn_reg_id  => ln_reg_id
215                               );*/
216   p_process_flag         := jai_constants.successful   ;
217   p_process_message      := null                       ;
218 
219   OPEN  cur_get_order_num( cp_hdr_id  => p_order_header_id );
220   FETCH cur_get_order_num INTO ln_order_number;
221   CLOSE cur_get_order_num ;
222 
223   v_ref_26 := p_delivery_id  ;
224 
225 
226   --|| Added the delivery_id to the v_Ref_10 variable so that delivery id can also be seen
227   --|| in the journal screen when the gl import is done.
228 
229   v_ref_10 := v_std_text || ln_order_number || ' and Delivery id :' || p_delivery_id || ' and Delivery Detail id :' || p_delivery_detail_id ;
230 
231 
232   FOR rec_cur_get_picking_taxes IN cur_get_picking_taxes
233   LOOP
234 
235 
236     /*********************************************************************************************************
237     || Get the code combination id from the Organization/Regime Registration setup
238     || by calling the function jai_cmn_rgm_recording_pkg.get_account
239     *********************************************************************************************************/
240 
241 /*commented by csahoo for bug# 6401388
242 jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
243                                         pv_log_msg  =>  'Before call to jai_cmn_rgm_recording_pkg.get_account for int liab'
244                                      );*/
245     v_liab_acc_ccid     := jai_cmn_rgm_recording_pkg.get_account  (
246                                                                     p_regime_id             => rec_cur_get_picking_taxes.regime_id  ,
247                                                                     p_organization_type     => jai_constants.orgn_type_io           ,
248                                                                     p_organization_id       => p_organization_id                    ,
249                                                                     p_location_id           => p_location_id                        ,
250                                                                     p_tax_type              => rec_cur_get_picking_taxes.tax_type   ,
251                                                                     p_account_name          => jai_constants.liability
252                                                                  );
253 
254     /*commented by csahoo for bug# 6401388
255     jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
256                                         pv_log_msg  =>  'Before call to jai_cmn_rgm_recording_pkg.get_account for liab'
257                                      );*/
258     v_int_liab_acc_ccid := jai_cmn_rgm_recording_pkg.get_account (
259                                                                     p_regime_id         => rec_cur_get_picking_taxes.regime_id      ,
260                                                                     p_organization_type => jai_constants.orgn_type_io               ,
261                                                                     p_organization_id   => p_organization_id                        ,
262                                                                     p_location_id       => p_location_id                            ,
263                                                                     p_tax_type          => rec_cur_get_picking_taxes.tax_type       ,
264                                                                     p_account_name      => jai_constants.liability_interim
265                                                                  );
266 
267 
268 
269     IF v_int_liab_acc_ccid IS NULL OR
270        v_liab_acc_ccid     IS NULL
271     THEN
272       /**********************************************************************************************************
273       || Code Combination id has been returned as null from the function jai_cmn_rgm_recording_pkg.get_account
274       || This is an error condition and the current processing has to be stopped
275       **********************************************************************************************************/
276      /*commented by csahoo for bug# 6401388
277      jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
278                                           pv_log_msg  =>  'Error in call to jai_cmn_rgm_recording_pkg.get_account'
279                                        );*/
280       p_process_flag      := jai_constants.expected_error;
281       p_process_message   := 'Invalid Code combination ,please check the TCS Tax - Tax Accounting Setup';
282       return;
283     END IF;
284 
285 
286     /*
287     ||Credit the liability account
288     */
289     /*commented by csahoo for bug# 6401388
290      jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
291                                         PV_LOG_MSG  =>  'CREDIT MEMO ENTRIES GETTING PASSED TO jai_cmn_gl_pkg.create_gl_entry ARE :- ' ||fnd_global.local_chr(10)
292                                         ||', p_organization_id           -> '|| p_organization_id                                     ||fnd_global.local_chr(10)
293                                         ||', p_currency_code             -> '|| p_currency_code                                       ||fnd_global.local_chr(10)
294                                         ||', p_credit_amount             -> '|| round(rec_cur_get_picking_taxes.func_tax_amount)      ||fnd_global.local_chr(10)
295                                         ||', p_debit_amount              -> '|| 0                                                     ||fnd_global.local_chr(10)
296                                         ||', p_cc_id                     -> '|| v_liab_acc_ccid                                       ||fnd_global.local_chr(10)
297                                         ||', p_je_source_name            -> '|| lv_source_name                                        ||fnd_global.local_chr(10)
298                                         ||', p_je_category_name          -> '|| lv_category_name                                      ||fnd_global.local_chr(10)
299                                         ||', p_created_by                -> '|| rec_cur_get_picking_taxes.created_by                  ||fnd_global.local_chr(10)
300                                         ||', p_accounting_date           -> '|| trunc(sysdate)                                        ||fnd_global.local_chr(10)
301                                         ||', p_currency_conversion_date  -> '|| NULL                                                  ||fnd_global.local_chr(10)
302                                         ||', p_currency_conversion_type  -> '|| NULL                                                  ||fnd_global.local_chr(10)
303                                         ||', p_currency_conversion_rate  -> '|| NULL                                                  ||fnd_global.local_chr(10)
304                                         ||', p_reference_10              -> '|| v_ref_10                                              ||fnd_global.local_chr(10)
305                                         ||', p_reference_23              -> '|| v_ref_23                                              ||fnd_global.local_chr(10)
306                                         ||', p_reference_24              -> '|| v_ref_24                                              ||fnd_global.local_chr(10)
307                                         ||', p_reference_25              -> '|| v_ref_25                                              ||fnd_global.local_chr(10)
308                                         ||', p_reference_26              -> '|| v_ref_26
309                                      );*/
310 
311 
312     jai_cmn_gl_pkg.create_gl_entry
313               (p_organization_id              => p_organization_id                                    ,
314                p_currency_code                => p_currency_code                                      ,
315                p_credit_amount                => round(rec_cur_get_picking_taxes.func_tax_amount)     ,
316                p_debit_amount                 => 0                                                    ,
317                p_cc_id                        => v_liab_acc_ccid                                      ,
318                p_je_source_name               => lv_source_name                                       ,
319                p_je_category_name             => lv_category_name                                     ,
320                p_created_by                   => rec_cur_get_picking_taxes.created_by                 ,
321                p_accounting_date              => trunc(sysdate)                                       ,
322                p_currency_conversion_date     => NULL                                                 ,
323                p_currency_conversion_type     => NULL                                                 ,
324                p_currency_conversion_rate     => NULL                                                 ,
325                p_reference_10                 => v_ref_10                                             ,
326                p_reference_23                 => v_ref_23                                             ,
327                p_reference_24                 => v_ref_24                                             ,
328                p_reference_25                 => v_ref_25                                             ,
329                p_reference_26                 => v_ref_26
330                );
331 
332     /*
333     ||Debit the Interim liability account
334     */
335     /*
336     jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
337                                         pv_log_msg  =>  'DEBIT MEMO ENTRIES GETTING PASSED TO jai_cmn_gl_pkg.create_gl_entry ARE :- '||fnd_global.local_chr(10)
338                                         ||', p_organization_id           -> '|| p_organization_id                                   ||fnd_global.local_chr(10)
339                                         ||', p_currency_code             -> '|| p_currency_code                                     ||fnd_global.local_chr(10)
340                                         ||', p_credit_amount             -> '|| 0                                                   ||fnd_global.local_chr(10)
341                                         ||', p_debit_amount              -> '|| round(rec_cur_get_picking_taxes.func_tax_amount)    ||fnd_global.local_chr(10)
342                                         ||', p_cc_id                     -> '|| v_int_liab_acc_ccid                                 ||fnd_global.local_chr(10)
343                                         ||', p_je_source_name            -> '|| lv_source_name                                      ||fnd_global.local_chr(10)
344                                         ||', p_je_category_name          -> '|| lv_category_name                                    ||fnd_global.local_chr(10)
345                                         ||', p_created_by                -> '|| rec_cur_get_picking_taxes.created_by                ||fnd_global.local_chr(10)
346                                         ||', p_accounting_date           -> '|| trunc(sysdate)                                      ||fnd_global.local_chr(10)
347                                         ||', p_currency_conversion_date  -> '|| NULL                                                ||fnd_global.local_chr(10)
348                                         ||', p_currency_conversion_type  -> '|| NULL                                                ||fnd_global.local_chr(10)
349                                         ||', p_currency_conversion_rate  -> '|| NULL                                                ||fnd_global.local_chr(10)
350                                         ||', p_reference_10              -> '|| v_ref_10                                            ||fnd_global.local_chr(10)
351                                         ||', p_reference_23              -> '|| v_ref_23                                            ||fnd_global.local_chr(10)
352                                         ||', p_reference_24              -> '|| v_ref_24                                            ||fnd_global.local_chr(10)
353                                         ||', p_reference_25              -> '|| v_ref_25                                            ||fnd_global.local_chr(10)
354                                         ||', p_reference_26              -> '|| v_ref_26
355                                      );*/
356     jai_cmn_gl_pkg.create_gl_entry
357               (p_organization_id              => p_organization_id                                      ,
358                p_currency_code                => p_currency_code                                        ,
359                p_credit_amount                => 0                                                      ,
360                p_debit_amount                 => round(rec_cur_get_picking_taxes.func_tax_amount)       ,
361                p_cc_id                        => v_int_liab_acc_ccid                                    ,
362                p_je_source_name               => lv_source_name                                         ,
363                p_je_category_name             => lv_category_name                                       ,
364                p_created_by                   => rec_cur_get_picking_taxes.created_by                   ,
365                p_accounting_date              => trunc(sysdate)                                         ,
366                p_currency_conversion_date     => NULL                                                   ,
367                p_currency_conversion_type     => NULL                                                   ,
368                p_currency_conversion_rate     => NULL                                                   ,
369                p_reference_10                 => v_ref_10                                               ,
370                p_reference_23                 => v_ref_23                                               ,
371                p_reference_24                 => v_ref_24                                               ,
372                p_reference_25                 => v_ref_25                                               ,
373                p_reference_26                 => v_ref_26
374                );
375 
376   END LOOP;
377 
378   /*commented by csahoo for bug# 6401388
379   jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
380                                       pv_log_msg  =>  '**************** WSH_INTERIM_ACCOUNTING SUCCESSFULLY COMPLETED ****************'
381                                    );
382   jai_cmn_debug_contexts_pkg.deregister (pn_reg_id => ln_reg_id);*/
383 
384 EXCEPTION
385   WHEN OTHERS THEN
386     p_process_flag      := jai_constants.unexpected_error;
387     p_process_message   := 'Unexpected error in the jai_ar_tcs_rep_pkg.wsh_interim_accounting '||substr(sqlerrm,1,300);
388 
389 END wsh_interim_accounting;
390 
391 /*Bug 12543504 - Fetches the TCS Taxes from RMA Receipt. Moves the TCS Liability to Interim Liability as this is Receipt for RMA Order
392 Inserts entries into GL_INTERFACE, JAI_RCV_JOURNAL_ENTRIES and JAI_RCV_SUBLED_ENTRIES*/
393 
394 PROCEDURE receipt_interim_accounting(p_shipment_line_id         IN         NUMBER,
395                                      p_shipment_header_id       IN         NUMBER,
396                                      p_organization_id          IN         NUMBER,
397                                      p_line_location_id         IN         NUMBER,
398                                      p_currency_conversion_rate IN         NUMBER,
399                                      p_transaction_type         IN         VARCHAR2,
400                                      p_transaction_id           IN         NUMBER,
401                                      v_currency_code            IN         VARCHAR2,
402                                      p_process_flag             OUT NOCOPY VARCHAR2,
403                                      p_process_message          OUT NOCOPY VARCHAR2
404                                  )
405 
406 IS
407   ln_reg_id           NUMBER;
408   CURSOR cur_get_picking_taxes
409   IS
410   SELECT
411            jrtl.shipment_header_id,
412            jrtl.shipment_line_id,
413            jrtl.tax_id,
414            jrtl.tax_type,
415            jrtl.tax_amount * nvl(p_currency_conversion_rate, 1) tax_amount,
416            jrtl.created_by,
417            jrttv.regime_id
418   FROM
419           jai_rcv_line_taxes           jrtl,
420           jai_cmn_taxes_all            jtc  ,
421           jai_regime_tax_types_v       jrttv
422   WHERE
423           jrtl.shipment_line_id     = p_shipment_line_id
424   AND     jrtl.tax_id               = jtc.tax_id
425   AND     jtc.tax_type              = jrttv.tax_type
426   AND     jrttv.regime_code         = jai_constants.tcs_regime;
427 
428   cursor c_fetch_org_information(cp_organization_id in number) is
429   select set_of_books_id, organization_code
430   from   org_organization_definitions
431   where organization_id = cp_organization_id;
432 
433   cursor c_trx(cp_transaction_id in number) IS
434   select *
435   from jai_rcv_transactions
436   where transaction_id = cp_transaction_id;
437 
438   CURSOR rcv_cur IS
439   SELECT *
440   FROM rcv_transactions
441   WHERE transaction_id = p_transaction_id;
442 
443   CURSOR ship_rec IS
444   SELECT item_id
445   FROM rcv_shipment_lines
446   WHERE shipment_line_id = p_shipment_line_id;
447 
448   v_rcv_rec                  rcv_cur%ROWTYPE;
449   v_item_id                  rcv_shipment_lines.item_id%type;
450   lv_organization_code       org_organization_definitions.organization_code%type;
451   ln_set_of_books_id         org_organization_definitions.set_of_books_id%type;
452   r_trx                      c_trx%ROWTYPE;
453   lv_account_nature          VARCHAR2(30)                    := 'REVERSAL';
454   lv_account_type            VARCHAR2(30);
455   lv_period_name             gl_periods.period_name%type;
456   v_process_status           VARCHAR2(30);
457   v_process_message          VARCHAR2(200);
458   v_ref_10                   GL_INTERFACE.REFERENCE10%TYPE                                                       ;
459   v_ref_23                   GL_INTERFACE.REFERENCE23%TYPE   := 'jai_tcs_repp_pkg.receipt_interim_accounting'    ; -- holds the object name
460   v_ref_24                   GL_INTERFACE.REFERENCE24%TYPE   := 'rcv_transactions'                               ; -- holds the table name  -- ' rcv_transactions'
461   v_ref_25                   GL_INTERFACE.REFERENCE25%TYPE   := 'transaction_id'                                 ; -- holds the column name -- 'delivery_id'
462   v_ref_26                   GL_INTERFACE.REFERENCE26%TYPE                                                       ; -- holds the column value -- eg -- 13645
463   ln_order_number            OE_ORDER_HEADERS_ALL.ORDER_NUMBER%TYPE ;
464   lv_source_name             VARCHAR2(100)                   := 'Receivables India'         ;
465   lv_category_name           VARCHAR2(100)                   := jai_constants.tcs_source    ;
466   v_int_liab_acc_ccid        NUMBER;
467   v_liab_acc_ccid            NUMBER;
468   v_receipt_num              NUMBER;
469   p_codepath                 VARCHAR2(2000);
470 BEGIN
471 
472   /*########################################################################################################
473   || VARIABLES INITIALIZATION - PART -1
474   ########################################################################################################*/
475     lv_member_name        := 'RECEIPT_INTERIM_ACCOUNTING';
476     set_debug_context;
477 
478     set_debug_context;
479     p_process_flag         := jai_constants.successful   ;
480     p_process_message      := null                       ;
481 
482     for receipt_rec IN (SELECT receipt_num
483                         FROM rcv_shipment_headers
484                         WHERE shipment_header_id = p_shipment_header_id)
485     loop
486         v_receipt_num := receipt_rec.receipt_num;
487     end loop;
488 
489     v_ref_26 := p_transaction_id  ;
490     v_ref_10 := 'India Local Receiving Entry for the Receipt Number ' || v_receipt_num ||' for the Transaction Type ' ||p_transaction_type; --Reference10
491 
492     open   c_fetch_org_information(p_organization_id);
493     fetch  c_fetch_org_information into ln_set_of_books_id,lv_organization_code;
494     close  c_fetch_org_information;
495 
496     open   c_trx(p_transaction_id);
497     fetch  c_trx into r_trx;
498     close  c_trx;
499 
500     OPEN  rcv_cur;
501     FETCH rcv_cur INTO v_rcv_rec;
502     CLOSE rcv_cur;
503 
504     OPEN ship_rec;
505     FETCH ship_rec INTO v_item_id;
506     CLOSE ship_rec;
507 
508     FOR rec_cur_get_picking_taxes IN cur_get_picking_taxes
509     LOOP
510 
511         /*********************************************************************************************************
512         || Get the code combination id from the Organization/Regime Registration setup
513         || by calling the function jai_cmn_rgm_recording_pkg.get_account
514         *********************************************************************************************************/
515 
516         v_liab_acc_ccid     := jai_cmn_rgm_recording_pkg.get_account  (
517                                                                         p_regime_id             => rec_cur_get_picking_taxes.regime_id  ,
518                                                                         p_organization_type     => jai_constants.orgn_type_io           ,
519                                                                         p_organization_id       => p_organization_id                    ,
520                                                                         p_location_id           => p_line_location_id                        ,
521                                                                         p_tax_type              => rec_cur_get_picking_taxes.tax_type   ,
522                                                                         p_account_name          => jai_constants.liability
523                                                                      );
524 
525         v_int_liab_acc_ccid := jai_cmn_rgm_recording_pkg.get_account (
526                                                                         p_regime_id         => rec_cur_get_picking_taxes.regime_id      ,
527                                                                         p_organization_type => jai_constants.orgn_type_io               ,
528                                                                         p_organization_id   => p_organization_id                        ,
529                                                                         p_location_id       => p_line_location_id                            ,
530                                                                         p_tax_type          => rec_cur_get_picking_taxes.tax_type       ,
531                                                                         p_account_name      => jai_constants.liability_interim
532                                                                      );
533 
534         IF v_int_liab_acc_ccid IS NULL OR
535            v_liab_acc_ccid     IS NULL
536         THEN
537           /**********************************************************************************************************
538           || Code Combination id has been returned as null from the function jai_rgm_trx_recording_pkg.get_account
539           || This is an error condition and the current processing has to be stopped
540           **********************************************************************************************************/
541           p_process_flag      := jai_constants.expected_error;
542           p_process_message   := 'Invalid Code combination ,please check the TCS Tax - Tax Accounting Setup';
543           return;
544         END IF;
545 
546         /*
547         ||Debit the liability account
548         */
549 
550         jai_cmn_gl_pkg.create_gl_entry
551                   (p_organization_id              => p_organization_id                                    ,
552                    p_currency_code                => v_currency_code                                      ,
553                    p_credit_amount                => 0                                                    ,
554                    p_debit_amount                 => round(NVL(rec_cur_get_picking_taxes.tax_amount,0))     ,
555                    p_cc_id                        => v_liab_acc_ccid                                      ,
556                    p_je_source_name               => lv_source_name                                       ,
557                    p_je_category_name             => lv_category_name                                     ,
558                    p_created_by                   => rec_cur_get_picking_taxes.created_by                 ,
559                    p_accounting_date              => trunc(sysdate)                                       ,
560                    p_currency_conversion_date     => NULL                                                 ,
561                    p_currency_conversion_type     => NULL                                                 ,
562                    p_currency_conversion_rate     => NULL                                                 ,
563                    p_reference_10                 => v_ref_10                                             ,
564                    p_reference_23                 => v_ref_23                                             ,
565                    p_reference_24                 => v_ref_24                                             ,
566                    p_reference_25                 => v_ref_25                                             ,
567                    p_reference_26                 => v_ref_26
568                    );
569 
570         /*
571         ||populate the ja_in_rcv_receiving_sub_ledger table
572         */
573 
574         jai_rcv_accounting_pkg.rcv_receiving_sub_ledger_entry(
575             p_transaction_id,
576             p_organization_id,
577             ln_set_of_books_id,
578             v_currency_code,
579             0,
580             round(NVL(rec_cur_get_picking_taxes.tax_amount,0)),
581             v_liab_acc_ccid,
582             rec_cur_get_picking_taxes.shipment_line_id,
583             v_item_id,
584             v_rcv_rec.source_document_code,
585             v_rcv_rec.po_line_location_id,
586             v_rcv_rec.requisition_line_id,
587             trunc(sysdate),
588             v_rcv_rec.transaction_date,
589             v_rcv_rec.currency_conversion_type,
590             gl_currency_api.get_rate_sql (
591                                           x_from_currency   => v_rcv_rec.currency_code,
592                                           x_to_currency		=> 'INR',
593                                           x_conversion_date	=> v_rcv_rec.transaction_date,
594                                           x_conversion_type	=> v_rcv_rec.currency_conversion_type ),
595             p_process_message,
596             v_process_status,
597             p_codepath
598         );
599 
600         /*
601         ||Populate the ja_in_rcv_journals_b table
602         */
603 
604         lv_account_type := 'LIAACNT';
605         v_process_status  := 'X';
606 
607         jai_rcv_journal_pkg.insert_row
608         (
609          p_organization_id            =>       p_organization_id,
610          p_organization_code          =>       lv_organization_code,
611          p_receipt_num                =>       r_trx.receipt_num,
612          p_transaction_id             =>       p_transaction_id,
613          p_transaction_date           =>       r_trx.transaction_date,
614          p_shipment_line_id           =>       r_trx.shipment_line_id,
615          p_acct_type                  =>       lv_account_type,
616          p_acct_nature                =>       lv_account_nature,
617          p_source_name                =>       lv_source_name,
618          p_category_name              =>       lv_category_name,
619          p_code_combination_id        =>       v_liab_acc_ccid,
620          p_entered_dr                 =>       round(NVL(rec_cur_get_picking_taxes.tax_amount,0)) ,
621          p_entered_cr                 =>       0,
622          p_transaction_type           =>       r_trx.transaction_type,
623          p_period_name                =>       lv_period_name,
624          p_currency_code              =>       v_currency_code,
625          p_currency_conversion_type   =>       v_rcv_rec.currency_conversion_type,
626          p_currency_conversion_date   =>       v_rcv_rec.transaction_date,
627          p_currency_conversion_rate   =>       gl_currency_api.get_rate_sql (
628                                                x_from_currency   => v_rcv_rec.currency_code,
629                                                x_to_currency		=> 'INR',
630                                                x_conversion_date	=> v_rcv_rec.transaction_date,
631                                                x_conversion_type	=> v_rcv_rec.currency_conversion_type ),
632          p_simulate_flag              =>       'N',
633          p_process_status             =>       v_process_status,
634          p_process_message            =>       v_process_message,
635          p_reference_name             =>       NULL,
636          p_reference_id               =>       NULL
637         );
638 
639 
640         /*
641         ||credit the Interim liability account
642         */
643 
644         jai_cmn_gl_pkg.create_gl_entry
645                   (p_organization_id              => p_organization_id                                      ,
646                    p_currency_code                => v_currency_code                                        ,
647                    p_credit_amount                => round(NVL(rec_cur_get_picking_taxes.tax_amount,0)),
648                    p_debit_amount                 => 0                                                      ,
649                    p_cc_id                        => v_int_liab_acc_ccid                                    ,
650                    p_je_source_name               => lv_source_name                                         ,
651                    p_je_category_name             => lv_category_name                                       ,
652                    p_created_by                   => rec_cur_get_picking_taxes.created_by                   ,
653                    p_accounting_date              => trunc(sysdate)                                         ,
654                    p_currency_conversion_date     => NULL                                                   ,
655                    p_currency_conversion_type     => NULL                                                   ,
656                    p_currency_conversion_rate     => NULL                                                   ,
657                    p_reference_10                 => v_ref_10                                               ,
658                    p_reference_23                 => v_ref_23                                               ,
659                    p_reference_24                 => v_ref_24                                               ,
660                    p_reference_25                 => v_ref_25                                               ,
661                    p_reference_26                 => v_ref_26
662                    );
663 
664         /*
665         ||populate the ja_in_rcv_receiving_sub_ledger table
666         */
667 
668         jai_rcv_accounting_pkg.rcv_receiving_sub_ledger_entry(
669             p_transaction_id,
670             p_organization_id,
671             ln_set_of_books_id,
672             v_currency_code,
673             round(NVL(rec_cur_get_picking_taxes.tax_amount,0)),
674             0,
675             v_int_liab_acc_ccid,
676             rec_cur_get_picking_taxes.shipment_line_id,
677             v_item_id,
678             v_rcv_rec.source_document_code,
679             v_rcv_rec.po_line_location_id,
680             v_rcv_rec.requisition_line_id,
681             trunc(sysdate),
682             v_rcv_rec.transaction_date,
683             v_rcv_rec.currency_conversion_type,
684             gl_currency_api.get_rate_sql (
685                                           x_from_currency   => v_rcv_rec.currency_code,
686                                           x_to_currency		=> 'INR',
687                                           x_conversion_date	=> v_rcv_rec.transaction_date,
688                                           x_conversion_type	=> v_rcv_rec.currency_conversion_type ),
689             p_process_message,
690             v_process_status,
691             p_codepath
692         );
693 
694         /*
695         ||Populate the ja_in_rcv_journals_b table
696         */
697 
698         lv_account_type := 'INTLIAACT';
699         v_process_status  := 'X';
700 
701         jai_rcv_journal_pkg.insert_row
702         (
703          p_organization_id            =>       p_organization_id,
704          p_organization_code          =>       lv_organization_code,
705          p_receipt_num                =>       r_trx.receipt_num,
706          p_transaction_id             =>       p_transaction_id,
707          p_transaction_date           =>       r_trx.transaction_date,
708          p_shipment_line_id           =>       r_trx.shipment_line_id,
709          p_acct_type                  =>       lv_account_type,
710          p_acct_nature                =>       lv_account_nature,
711          p_source_name                =>       lv_source_name,
712          p_category_name              =>       lv_category_name,
713          p_code_combination_id        =>       v_int_liab_acc_ccid,
714          p_entered_dr                 =>       0,
715          p_entered_cr                 =>       round(NVL(rec_cur_get_picking_taxes.tax_amount,0)),
716          p_transaction_type           =>       r_trx.transaction_type,
717          p_period_name                =>       lv_period_name,
718          p_currency_code              =>       v_currency_code,
719          p_currency_conversion_type   =>       v_rcv_rec.currency_conversion_type,
720          p_currency_conversion_date   =>       v_rcv_rec.transaction_date,
721          p_currency_conversion_rate   =>       gl_currency_api.get_rate_sql (
722                                                x_from_currency   => v_rcv_rec.currency_code,
723                                                x_to_currency		=> 'INR',
724                                                x_conversion_date	=> v_rcv_rec.transaction_date,
725                                                x_conversion_type	=> v_rcv_rec.currency_conversion_type ),
726          p_simulate_flag              =>       'N',
727          p_process_status             =>       v_process_status,
728          p_process_message            =>       v_process_message,
729          p_reference_name             =>       NULL,
730          p_reference_id               =>       NULL
731         );
732 
733     END LOOP;
734 
735 EXCEPTION
736   WHEN OTHERS THEN
737     p_process_flag      := jai_constants.unexpected_error;
738     p_process_message   := 'Unexpected error in the jai_ar_tcs_rep_pkg.receipt_interim_accounting '||substr(sqlerrm,1,300);
739 
740 END receipt_interim_accounting;
741 
742 /*Bug 12543504 - End*/
743 
744 PROCEDURE ar_accounting (     p_ract              IN            RA_CUSTOMER_TRX_ALL%ROWTYPE       DEFAULT NULL  ,
745                               p_ractl             IN            RA_CUSTOMER_TRX_LINES_ALL%ROWTYPE DEFAULT NULL  ,
746                               p_process_flag      OUT NOCOPY    VARCHAR2                                        ,
747                               p_process_message   OUT NOCOPY    VARCHAR2
748                          )
749 
750 IS
751   ln_reg_id           NUMBER;
752   CURSOR cur_get_inv_det   ( cp_customer_trx_id         RA_CUSTOMER_TRX_ALL.CUSTOMER_TRX_ID%TYPE                         ,
753                              cp_customer_trx_line_id    RA_CUSTOMER_TRX_LINES_ALL.CUSTOMER_TRX_LINE_ID%TYPE  DEFAULT NULL
754                            )
755   IS
756   SELECT
757            jrcttl.*              ,
758            jrct.organization_id  ,
759            jrct.location_id      ,
760            jrttv.regime_id       ,
761            jtc.tax_type
762   FROM
763           JAI_AR_TRXS        jrct   ,
764           JAI_AR_TRX_LINES  jrctl  ,
765           JAI_AR_TRX_TAX_LINES  jrcttl ,
766           JAI_CMN_TAXES_ALL              jtc    ,
767           jai_regime_tax_types_v       jrttv
768 
769   WHERE
770           jrct.customer_trx_id        = jrctl.customer_trx_id
771   AND     jrctl.customer_trx_line_id  = jrcttl.link_to_cust_trx_line_id
772   AND     jrcttl.tax_id               = jtc.tax_id
773   AND     jtc.tax_type                = jrttv.tax_type
774   AND     jrttv.regime_code           = jai_constants.tcs_regime
775   AND     jrct.customer_trx_id        = cp_customer_trx_id
776   AND     jrctl.customer_trx_line_id  = nvl( cp_customer_trx_line_id , jrctl.customer_trx_line_id );
777 
778   /* Added for Bug 6734317 - Start */
779   CURSOR c_dist_gl_date(cp_customer_trx_id ra_cust_trx_line_gl_dist_all.customer_trx_id%type)
780   IS
781   SELECT max(gl_date) gl_date
782   FROM   ra_cust_trx_line_gl_dist_all
783   WHERE  customer_trx_id = cp_customer_trx_id ;
784 
785   ld_dist_gl_date   ra_cust_trx_line_gl_dist_all.gl_date%TYPE ;
786   /* Added for Bug 6734317 - End */
787 
788   v_ref_10    GL_INTERFACE.REFERENCE10%TYPE                                                       ;
789   v_std_text  VARCHAR2(50)                                                                        ;
790   v_ref_23    GL_INTERFACE.REFERENCE23%TYPE   ; -- holds the object name
791   v_ref_24    GL_INTERFACE.REFERENCE24%TYPE   ; -- holds the table name
792   v_ref_25    GL_INTERFACE.REFERENCE25%TYPE   ; -- holds the column name
793   v_ref_26    GL_INTERFACE.REFERENCE26%TYPE                                                       ; -- holds the column value -- eg -- 13645
794 
795   lv_source_name    VARCHAR2(100)                        ;
796   lv_category_name  VARCHAR2(100)                         ;
797 
798   v_int_liab_acc_ccid  NUMBER;
799   v_liab_acc_ccid      NUMBER;
800 
801   --Added by Xiao for Open Interface ER bug#11683927 on 10-Feb-2011, begin
802   ------------------------------------------------------------------------------
803   CURSOR get_external_flag_cur IS
804   SELECT COUNT(lines.customer_trx_id)
805     FROM jai_ar_trx_lines        lines,
806          jai_interface_lines_all intfs
807    WHERE lines.customer_trx_line_id = intfs.internal_trx_line_id
808      AND lines.customer_trx_id = p_ract.customer_trx_id
809      AND lines.interface_flag = 'Y'
810      AND intfs.taxable_event = 'EXTERNAL';
811 
812   ln_external_flag  NUMBER;
813   v_external_liab_acc_ccid      NUMBER;
814 
815   ------------------------------------------------------------------------------
816   --Added by Xiao for Open Interface ER bug#11683927 on 10-Feb-2011, end
817 
818 BEGIN
819   /*########################################################################################################
820   || VARIABLES INITIALIZATION - PART -1
821   ########################################################################################################*/
822    lv_member_name        := 'AR_ACCOUNTING';
823    v_ref_23 := 'jai_ar_tcs_rep_pkg.ar_accounting';
824    v_ref_24 := 'ra_customer_trx_all'             ;
825    v_ref_25 := 'customer_trx_id'                 ;
826   -- lv_source_name     := jai_constants.tcs_source   ;    -- modified by csahoo for bug#6155839 commented for bug#9587338
827   -- lv_category_name   := 'Receivables India'  ;   -- modified by csahoo for bug#6155839 commented for bug#9587338
828 
829   lv_source_name := 'Receivables India'  ; --bug#9587338
830   lv_category_name :=jai_constants.tcs_source; --bug#9587338
831 
832    set_debug_context;
833 
834 
835   /* jai_cmn_debug_contexts_pkg.register ( pv_context => lv_context  ,
836                                 pn_reg_id  => ln_reg_id
837                               ); --commmented by CSahoo, BUG#5631784*/
838   p_process_flag         := jai_constants.successful   ;
839   p_process_message      := null                       ;
840 
841   v_ref_26 := nvl(p_ract.customer_trx_id,p_ractl.customer_trx_id)  ;
842 
843   IF p_ract.customer_trx_id IS NOT NULL THEN
844     v_std_text := 'India Localization Entry for Manual Invoices#'  ;
845   ELSIF p_ractl.customer_trx_id IS NOT NULL THEN
846     v_std_text := 'India Localization Entry for Bill Only Invoices#'  ;
847   END IF;
848 
849   v_ref_10 := v_std_text || p_ract.trx_number || ' and customer_trx_id :' || nvl(p_ract.customer_trx_id,p_ractl.customer_trx_id) ;
850 
851 
852   FOR rec_cur_get_inv_det IN cur_get_inv_det (  cp_customer_trx_id       =>  nvl(p_ract.customer_trx_id,p_ractl.customer_trx_id ),
853                                                 cp_customer_trx_line_id  =>  p_ractl.customer_trx_line_id
854                                              )
855 
856   LOOP
857 
858 
859     /*********************************************************************************************************
860     || Get the code combination id from the Organization/Regime Registration setup
861     || by calling the function jai_cmn_rgm_recording_pkg.get_account
862     *********************************************************************************************************/
863 
864     /*commented by csahoo for bug# 6401388
865     jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
866                                         pv_log_msg  =>  'Before call to jai_cmn_rgm_recording_pkg.get_account for int liab'
867                                      );*/
868     v_liab_acc_ccid     := jai_cmn_rgm_recording_pkg.get_account  (
869                                                                     p_regime_id             => rec_cur_get_inv_det.regime_id        ,
870                                                                     p_organization_type     => jai_constants.orgn_type_io           ,
871                                                                     p_organization_id       => rec_cur_get_inv_det.organization_id  ,
872                                                                     p_location_id           => rec_cur_get_inv_det.location_id      ,
873                                                                     p_tax_type              => rec_cur_get_inv_det.tax_type         ,
874                                                                     p_account_name          => jai_constants.liability
875                                                                  );
876 
877    /*commented by csahoo for bug# 6401388
878    jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
879                                         pv_log_msg  =>  'Before call to jai_cmn_rgm_recording_pkg.get_account for liab'
880                                      );*/
881     v_int_liab_acc_ccid := jai_cmn_rgm_recording_pkg.get_account (
882                                                                     p_regime_id         => rec_cur_get_inv_det.regime_id         ,
883                                                                     p_organization_type => jai_constants.orgn_type_io            ,
884                                                                     p_organization_id   => rec_cur_get_inv_det.organization_id   ,
885                                                                     p_location_id       => rec_cur_get_inv_det.location_id       ,
886                                                                     p_tax_type          => rec_cur_get_inv_det.tax_type          ,
887                                                                     p_account_name      => jai_constants.liability_interim
888                                                                  );
889 
890 
891 
892     IF v_int_liab_acc_ccid IS NULL OR
893        v_liab_acc_ccid     IS NULL
894     THEN
895       /**********************************************************************************************************
896       || Code Combination id has been returned as null from the function jai_cmn_rgm_recording_pkg.get_account
897       || This is an error condition and the current processing has to be stopped
898       **********************************************************************************************************/
899       /*commented by csahoo for bug# 6401388
900       jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
901                                           pv_log_msg  =>  'Error in call to jai_cmn_rgm_recording_pkg.get_account'
902                                        );*/
903       p_process_flag      := jai_constants.expected_error;
904       p_process_message   := 'Invalid Code combination ,please check the TCS Tax - Tax Accounting Setup';
905       return;
906     END IF;
907 
908     /* Added for Bug 6734317 */
909     OPEN  c_dist_gl_date(nvl(p_ract.customer_trx_id, p_ractl.customer_trx_id)) ;
910     FETCH c_dist_gl_date INTO ld_dist_gl_date;
911     CLOSE c_dist_gl_date ;
912 
913     /*
914     ||Credit the liability account
915     */
916     /*commented by csahoo for bug# 6401388
917     jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
918                                         PV_LOG_MSG  =>  'CREDIT MEMO ENTRIES GETTING PASSED TO jai_cmn_gl_pkg.create_gl_entry ARE :- ' ||fnd_global.local_chr(10)
919                                         ||', p_organization_id           -> '|| rec_cur_get_inv_det.organization_id                   ||fnd_global.local_chr(10)
920                                         ||', p_currency_code             -> '|| p_ract.invoice_currency_code                          ||fnd_global.local_chr(10)
921                                         ||', p_credit_amount             -> '|| round(rec_cur_get_inv_det.func_tax_amount)            ||fnd_global.local_chr(10)
922                                         ||', p_debit_amount              -> '|| 0                                                     ||fnd_global.local_chr(10)
923                                         ||', p_cc_id                     -> '|| v_liab_acc_ccid                                       ||fnd_global.local_chr(10)
924                                         ||', p_je_source_name            -> '|| lv_source_name                                        ||fnd_global.local_chr(10)
925                                         ||', p_je_category_name          -> '|| lv_category_name                                      ||fnd_global.local_chr(10)
926                                         ||', p_created_by                -> '|| rec_cur_get_inv_det.created_by                        ||fnd_global.local_chr(10)
927                                         ||', p_accounting_date           -> '|| trunc(sysdate)                                        ||fnd_global.local_chr(10)
928                                         ||', p_currency_conversion_date  -> '|| NULL                                                  ||fnd_global.local_chr(10)
929                                         ||', p_currency_conversion_type  -> '|| NULL                                                  ||fnd_global.local_chr(10)
930                                         ||', p_currency_conversion_rate  -> '|| NULL                                                  ||fnd_global.local_chr(10)
931                                         ||', p_reference_10              -> '|| v_ref_10                                              ||fnd_global.local_chr(10)
932                                         ||', p_reference_23              -> '|| v_ref_23                                              ||fnd_global.local_chr(10)
933                                         ||', p_reference_24              -> '|| v_ref_24                                              ||fnd_global.local_chr(10)
934                                         ||', p_reference_25              -> '|| v_ref_25                                              ||fnd_global.local_chr(10)
935                                         ||', p_reference_26              -> '|| v_ref_26
936                                      );*/
937 
938     --Added by Xiao for Open Interface ER bug#11683927 on 03-Mar-2011, begin
939     -----------------------------------------------------------------------------------
940     OPEN get_external_flag_cur;
941     FETCH get_external_flag_cur INTO ln_external_flag;
942     CLOSE get_external_flag_cur;
943 
944 
945     IF ln_external_flag > 0 THEN
946        v_external_liab_acc_ccid := jai_open_api_pkg.get_external_dist_account_ccid(
947                                  p_ract.customer_trx_id
948                                , rec_cur_get_inv_det.link_to_cust_trx_line_id
949                                , rec_cur_get_inv_det.tax_line_no);
950     END IF;
951 
952     v_liab_acc_ccid := nvl(v_external_liab_acc_ccid,v_liab_acc_ccid);--Added by zhiwei for Open Interface bug#11683927
953 
954 
955    -----------------------------------------------------------------------------------
956    --Added by Xiao for Open Interface ER bug#11683927 on 03-Mar-2011, end
957 
958     jai_cmn_gl_pkg.create_gl_entry
959               (p_organization_id              => rec_cur_get_inv_det.organization_id            ,
960                p_currency_code                => p_ract.invoice_currency_code                   ,
961                p_credit_amount                => round(rec_cur_get_inv_det.func_tax_amount)     ,
962                p_debit_amount                 => 0                                              ,
963                p_cc_id                        => v_liab_acc_ccid                                ,
964                p_je_source_name               => lv_source_name                                 ,
965                p_je_category_name             => lv_category_name                               ,
966                p_created_by                   => rec_cur_get_inv_det.created_by                 ,
967                p_accounting_date              => ld_dist_gl_date                                , /*Replaced sysdate with ld_dist_gl_date - Bug 6734317*/
968                p_currency_conversion_date     => NULL                                           ,
969                p_currency_conversion_type     => NULL                                           ,
970                p_currency_conversion_rate     => NULL                                           ,
971                p_reference_10                 => v_ref_10                                       ,
972                p_reference_23                 => v_ref_23                                       ,
973                p_reference_24                 => v_ref_24                                       ,
974                p_reference_25                 => v_ref_25                                       ,
975                p_reference_26                 => v_ref_26
976                );
977 
978 
979     /*
980     ||Debit the Interim liability account
981     */
982     /*commented by csahoo for bug# 6401388
983     jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
984                                         pv_log_msg  =>  'DEBIT MEMO ENTRIES GETTING PASSED TO jai_cmn_gl_pkg.create_gl_entry ARE :- '||fnd_global.local_chr(10)
985                                         ||', p_organization_id           -> '|| rec_cur_get_inv_det.organization_id                 ||fnd_global.local_chr(10)
986                                         ||', p_currency_code             -> '|| p_ract.invoice_currency_code                        ||fnd_global.local_chr(10)
987                                         ||', p_credit_amount             -> '|| 0                                                   ||fnd_global.local_chr(10)
988                                         ||', p_debit_amount              -> '|| round(rec_cur_get_inv_det.func_tax_amount)          ||fnd_global.local_chr(10)
989                                         ||', p_cc_id                     -> '|| v_int_liab_acc_ccid                                 ||fnd_global.local_chr(10)
990                                         ||', p_je_source_name            -> '|| lv_source_name                                      ||fnd_global.local_chr(10)
991                                         ||', p_je_category_name          -> '|| lv_category_name                                    ||fnd_global.local_chr(10)
992                                         ||', p_created_by                -> '|| rec_cur_get_inv_det.created_by                      ||fnd_global.local_chr(10)
993                                         ||', p_accounting_date           -> '|| trunc(sysdate)                                      ||fnd_global.local_chr(10)
994                                         ||', p_currency_conversion_date  -> '|| NULL                                                ||fnd_global.local_chr(10)
995                                         ||', p_currency_conversion_type  -> '|| NULL                                                ||fnd_global.local_chr(10)
996                                         ||', p_currency_conversion_rate  -> '|| NULL                                                ||fnd_global.local_chr(10)
997                                         ||', p_reference_10              -> '|| v_ref_10                                            ||fnd_global.local_chr(10)
998                                         ||', p_reference_23              -> '|| v_ref_23                                            ||fnd_global.local_chr(10)
999                                         ||', p_reference_24              -> '|| v_ref_24                                            ||fnd_global.local_chr(10)
1000                                         ||', p_reference_25              -> '|| v_ref_25                                            ||fnd_global.local_chr(10)
1001                                         ||', p_reference_26              -> '|| v_ref_26
1002                                      );*/
1003 
1004     jai_cmn_gl_pkg.create_gl_entry
1005               (p_organization_id              => rec_cur_get_inv_det.organization_id            ,
1006                p_currency_code                => p_ract.invoice_currency_code                   ,
1007                p_credit_amount                => 0                                              ,
1008                p_debit_amount                 => round(rec_cur_get_inv_det.func_tax_amount)     ,
1009                p_cc_id                        => v_int_liab_acc_ccid                            ,
1010                p_je_source_name               => lv_source_name                                 ,
1011                p_je_category_name             => lv_category_name                               ,
1012                p_created_by                   => rec_cur_get_inv_det.created_by                 ,
1013                p_accounting_date              => ld_dist_gl_date                                , /*Replaced sysdate with ld_dist_gl_date - Bug 6734317*/
1014                p_currency_conversion_date     => NULL                                           ,
1015                p_currency_conversion_type     => NULL                                           ,
1016                p_currency_conversion_rate     => NULL                                           ,
1017                p_reference_10                 => v_ref_10                                       ,
1018                p_reference_23                 => v_ref_23                                       ,
1019                p_reference_24                 => v_ref_24                                       ,
1020                p_reference_25                 => v_ref_25                                       ,
1021                p_reference_26                 => v_ref_26
1022                );
1023 
1024   END LOOP;
1025 
1026  /*commented by csahoo for bug# 6401388
1027  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1028                                       pv_log_msg  =>  '**************** MAN_AR_COMPLETION_ACCOUNTING SUCCESSFULLY COMPLETED ****************'
1029                                    );
1030   jai_cmn_debug_contexts_pkg.deregister (pn_reg_id => ln_reg_id);*/
1031 
1032 EXCEPTION
1033   WHEN OTHERS THEN
1034     p_process_flag      := jai_constants.unexpected_error;
1035     p_process_message   := 'Unexpected error in the jai_ar_tcs_rep_pkg.man_ar_completion_accounting '||substr(sqlerrm,1,300);
1036 
1037 END ar_accounting;
1038 
1039 
1040 PROCEDURE validate_sales_order  (  p_ooh              IN             OE_ORDER_HEADERS_ALL%ROWTYPE ,
1041                                    p_process_flag     OUT NOCOPY     VARCHAR2                     ,
1042                                    p_process_message  OUT NOCOPY     VARCHAR2
1043                                 )
1044 IS
1045    ln_reg_id           NUMBER;
1046 
1047   /*
1048   || Check that the document has has TCS type of tax.
1049   */
1050   CURSOR cur_chk_tcs_applicable ( cp_header_id JAI_OM_OE_SO_LINES.HEADER_ID%TYPE )
1051   IS
1052   SELECT
1053           1
1054   FROM
1055           JAI_OM_OE_SO_LINES           jsl  ,
1056           JAI_OM_OE_SO_TAXES       jstl ,
1057           JAI_CMN_TAXES_ALL          jtc  ,
1058           jai_regime_tax_types_v   jrttv
1059   WHERE
1060           jsl.header_id        =   cp_header_id
1061   AND     jsl.line_id          =   jstl.line_id
1062   AND     jtc.tax_id           =   jstl.tax_id
1063   AND     jtc.tax_type         =   jrttv.tax_type
1064   AND     jrttv.regime_code    =   jai_constants.tcs_regime; /* Applied to doc has got TCS type of tax*/
1065 
1066 
1067 
1068   /*
1069   ||Now that some lines have got TCS type of taxes , check that all lines have got tcs type of taxes
1070   ||if any one line does not have TCS type of tax then throw an error
1071   */
1072    CURSOR cur_chk_tcs_for_all_lines ( cp_header_id JAI_OM_OE_SO_LINES.HEADER_ID%TYPE )
1073    IS
1074    SELECT
1075           1
1076    FROM
1077           JAI_OM_OE_SO_LINES           jsl, oe_order_lines_all oola
1078    WHERE
1079           jsl.header_id        =   cp_header_id
1080    /*9154563 - Added clause to check for canceled_flag also*/
1081    AND    oola.header_id       =   jsl.header_id
1082    AND    oola.line_id         =   jsl.line_id
1083    AND    oola.cancelled_flag  =   'N'
1084    AND   NOT EXISTS  (
1085                         SELECT
1086                                 1
1087                         FROM
1088                                 JAI_OM_OE_SO_TAXES       jstl ,
1089                                 JAI_CMN_TAXES_ALL          jtc  ,
1090                                 jai_regime_tax_types_v   jrttv
1091                         WHERE
1092                                 jsl.line_id          =   jstl.line_id
1093                         AND     jtc.tax_id           =   jstl.tax_id
1094                         AND     jtc.tax_type         =   jrttv.tax_type
1095                         AND     jrttv.regime_code    =   jai_constants.tcs_regime /* Applied to doc has got TCS type of tax*/
1096                      );
1097 
1098   /*******
1099   || Validate that the inventory_items for all lines of the sales order  should have the same TCS item classification
1100   || for the same has already been settled
1101   ********/
1102   CURSOR cur_validate_all_items ( cp_header_id JAI_OM_OE_SO_LINES.HEADER_ID%TYPE )
1103   IS
1104   SELECT
1105          inventory_item_id
1106   FROM
1107           JAI_OM_OE_SO_LINES           jsl
1108    WHERE
1109           jsl.header_id        =   cp_header_id;
1110 
1111   lv_object_name      jai_cmn_debug_contexts.LOG_CONTEXT%TYPE ;
1112   lv_member_name      jai_cmn_debug_contexts.LOG_CONTEXT%TYPE;
1113   lv_context          jai_cmn_debug_contexts.LOG_CONTEXT%TYPE;
1114 
1115   ln_exists                 NUMBER                                          ;
1116   lv_first_itm_class        JAI_RGM_REFS_ALL.ITEM_CLASSIFICATION%TYPE       ;
1117   lv_item_classification    JAI_RGM_REFS_ALL.ITEM_CLASSIFICATION%TYPE       ;
1118 
1119   lv_process_flag           VARCHAR2(2)                                     ;
1120   lv_process_message        VARCHAR2(4000)                                  ;
1121 
1122 BEGIN
1123 
1124   /*########################################################################################################
1125   || VARIABLES INITIALIZATION - PART -1
1126   ########################################################################################################*/
1127    lv_member_name        := 'VALIDATE_SALES_ORDER';
1128    lv_object_name :=  'TCS.JAI_AR_TCS_REP_PKG';
1129    set_debug_context;
1130   /*commented by csahoo for bug# 6401388
1131    jai_cmn_debug_contexts_pkg.register ( pv_context => lv_context  ,
1132                                 pn_reg_id  => ln_reg_id
1133                               );*/
1134   lv_process_flag         := jai_constants.successful   ;
1135   lv_process_message      := null                       ;
1136 
1137   p_process_flag          := lv_process_flag            ;
1138   p_process_message       := lv_process_message         ;
1139 
1140   /*########################################################################################################
1141   || SALES ORDER TCS APPLICABILITY CHECK - PART - 1
1142   ########################################################################################################*/
1143 
1144   /*
1145   ||Check that the order has a flow status code as booked else skip the transaction
1146   */
1147   IF ln_event = jai_constants.order_booked AND
1148      p_ooh.flow_status_code <> jai_constants.order_booked
1149   THEN
1150 /*commented by csahoo for bug# 6401388
1151     jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1152                                pv_log_msg  =>  ' Skip the transaction as order has a flow status code as '|| p_ooh.flow_status_code ||' which is different from BOOKED '
1153                              );*/
1154     p_process_flag := jai_constants.not_applicable ;
1155     return;
1156 
1157   END IF;
1158 
1159   /*
1160   || Check that for sales order TCS applicability
1161   || IF no then return
1162   */
1163 
1164   OPEN cur_chk_tcs_applicable ( cp_header_id => p_ooh.header_id ) ;
1165   FETCH cur_chk_tcs_applicable INTO ln_exists ;
1166   IF CUR_CHK_TCS_APPLICABLE%NOTFOUND THEN
1167   /*commented by csahoo for bug# 6401388
1168     jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1169                                pv_log_msg  =>  ' Skip as order does not have TCS type of taxes '
1170                              );*/
1171     p_process_flag := jai_constants.not_applicable ;
1172     return;
1173   END IF;
1174   CLOSE cur_chk_tcs_applicable;
1175 
1176 
1177 
1178   /*########################################################################################################
1179   || VALIDATE THAT ALL LINES SHOULD HAVE TCS OR NIETHER SHOUD HAVE ANY - PART - 2
1180   ########################################################################################################*/
1181   IF  ln_event IN ( jai_constants.order_booked ,
1182                     jai_constants.wsh_ship_confirm
1183                   )
1184   THEN
1185 
1186     /****************
1187     || Event is Sales Order Booking
1188     || Validate that all lines have TCS type of taxes
1189     || if no then error out
1190     *****************/
1191 
1192     OPEN  cur_chk_tcs_for_all_lines ( cp_header_id => p_ooh.header_id ) ;
1193     FETCH cur_chk_tcs_for_all_lines INTO ln_exists;
1194     IF CUR_CHK_TCS_FOR_ALL_LINES%FOUND THEN
1195       /*
1196       ||Rows with no TCS type of taxes exists hence error out
1197       */
1198       CLOSE cur_chk_tcs_for_all_lines ;
1199      /*commented by csahoo for bug# 6401388
1200      jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1201                                  pv_log_msg  =>  'Error :- Cannot Book/ship the sales order if some lines have TCS type of tax and some dont '||fnd_global.local_chr(10)
1202                                                ||', lv_process_flag     -> '|| lv_process_flag  ||fnd_global.local_chr(10)
1203                                                ||', lv_process_message  -> '||lv_process_message
1204                               );*/
1205       p_process_flag    := jai_constants.expected_error;
1206       p_process_message := 'Cannot Book/Ship the Sales Order as some lines do not have TCS type of taxes ';
1207       return;
1208       CLOSE cur_chk_tcs_for_all_lines ;
1209     END IF;
1210 
1211     /*########################################################################################################
1212     || VALIDATE THAT THE INVENTORY_ITEMS FOR ALL LINES OF THE SALES ORDER
1213     || SHOULD HAVE THE SAME TCS ITEM CLASSIFICATION - PART - 3
1214     ########################################################################################################*/
1215 
1216     lv_first_itm_class     := null;
1217     lv_item_classification := null;
1218     FOR rec_cur_validate_all_items IN cur_validate_all_items ( cp_header_id => p_ooh.header_id )
1219     LOOP
1220 
1221       /*
1222       ||Get the value for the item classification pertaining to the IO and inventory item combination
1223       */
1224       jai_inv_items_pkg.jai_get_attrib (
1225                                        p_regime_code         => jai_constants.tcs_regime                     ,
1226                                        p_organization_id     => p_ooh.ship_from_org_id                       , -- Organization id of the Selling organization (warehouse_id)
1227                                        p_inventory_item_id   => rec_cur_validate_all_items.inventory_item_id ,
1228                                        p_attribute_code      => jai_constants.rgm_attr_cd_itm_class          ,
1229                                        p_attribute_value     => lv_item_classification                       ,
1230                                        p_process_flag        => lv_process_flag                              ,
1231                                        p_process_msg         => lv_process_message
1232                                     );
1233 
1234       IF lv_process_flag = jai_constants.expected_error    OR                      ---------A2
1235          lv_process_flag = jai_constants.unexpected_error
1236       THEN
1237         /*
1238         || As Returned status is an error/not applicable hence:-
1239         || Set out variables p_process_flag and p_process_message accordingly
1240         */
1241         --call to debug package
1242         /*commented by csahoo for bug# 6401388
1243         jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1244                                    pv_log_msg  =>  'Error In processing of jai_inv_items_pkg.jai_get_attrib'||fnd_global.local_chr(10)
1245                                                  ||', lv_process_flag     -> '|| lv_process_flag  ||fnd_global.local_chr(10)
1246                                                  ||', lv_process_message  -> '||lv_process_message
1247                                 );*/
1248 
1249         p_process_flag    := lv_process_flag    ;
1250         p_process_message := lv_process_message ;
1251         return;
1252       END IF;                                                                      ---------A2
1253 
1254       IF lv_first_itm_class IS NULL THEN
1255         /*
1256         ||First time assignment
1257         */
1258         lv_first_itm_class := lv_item_classification;
1259       END IF;
1260 
1261       /*
1262       ||IF any one of the lines do not match with the item TCS classification of the first line
1263       || then stop the transaction and throw an error.
1264       */
1265       IF nvl(lv_first_itm_class,'$$') <> nvl(lv_item_classification,'###') THEN
1266         /*commented by csahoo for bug# 6401388
1267         jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1268                                    pv_log_msg  =>  'Error :- Cannot Book/Ship the sales order as as all lines do not belong to the same Item Classification '||fnd_global.local_chr(10)
1269                                                 ||', lv_process_flag     -> '|| lv_process_flag  ||fnd_global.local_chr(10)
1270                                                 ||', lv_process_message  -> '||lv_process_message
1271                                );*/
1272 
1273         p_process_flag    := jai_constants.expected_error   ;
1274         p_process_message := 'Cannot Book/Ship the sales order. All lines should either have the same TCS item classification or none of the line should have TCS type of taxes' ;
1275         return;
1276       END IF;
1277     END LOOP;
1278 
1279   END IF;
1280 
1281   /*commented by csahoo for bug# 6401388
1282  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1283                              pv_log_msg  =>  '**************** VALIDATE_SALES_ORDER SUCCESSFULLY COMPLETED ****************'
1284                           );
1285   jai_cmn_debug_contexts_pkg.deregister (pn_reg_id => ln_reg_id);*/
1286 
1287 
1288 END validate_sales_order ;
1289 
1290  PROCEDURE validate_invoice      (  p_ract             IN             RA_CUSTOMER_TRX_ALL%ROWTYPE    ,
1291                                     p_document_type    OUT NOCOPY     VARCHAR2                       ,
1292                                     p_process_flag     OUT NOCOPY     VARCHAR2                       ,
1293                                     p_process_message  OUT NOCOPY     VARCHAR2
1294                                  )
1295  IS
1296    ln_reg_id           NUMBER;
1297   /*
1298   || Check that the document has has TCS type of tax.
1299   */
1300   CURSOR cur_chk_tcs_applicable ( cp_customer_trx_id JAI_AR_TRXS.CUSTOMER_TRX_ID%TYPE )
1301   IS
1302   SELECT
1303           gl_dist.gl_date       ,
1304           jrct.organization_id
1305   FROM
1306           ra_cust_trx_line_gl_dist_all gl_dist ,
1307           JAI_AR_TRXS        jrct    ,
1308           JAI_AR_TRX_LINES  jrctl   ,
1309           JAI_AR_TRX_TAX_LINES  jrcttl  ,
1310           JAI_CMN_TAXES_ALL              jtc     ,
1311           jai_regime_tax_types_v       jrttv
1312   WHERE
1313           gl_dist.customer_trx_id      =   jrct.customer_trx_id
1314   AND     gl_dist.account_class        =   jai_constants.account_class_rec
1315   AND     gl_dist.latest_rec_flag      =   jai_constants.yes
1316   AND     jrct.customer_trx_id         =   cp_customer_trx_id
1317   AND     jrct.customer_trx_id         =   jrctl.customer_trx_id
1318   AND     jrctl.customer_trx_line_id   =   jrcttl.link_to_cust_trx_line_id
1319   AND     jtc.tax_id                   =   jrcttl.tax_id
1320   AND     jtc.tax_type                 =   jrttv.tax_type
1321   AND     jrttv.regime_code            =   jai_constants.tcs_regime; /* Applied to doc has got TCS type of tax*/
1322 
1323 
1324  /*
1325  ||Get the trx type of the document
1326  */
1327  CURSOR cur_get_doc_det ( cp_cust_trx_type_id RA_CUST_TRX_TYPES_ALL.CUST_TRX_TYPE_ID%TYPE )
1328  IS
1329  SELECT
1330         type
1331  FROM
1332         ra_cust_trx_types_all
1333  WHERE
1334        cust_trx_type_id   =  cp_cust_trx_type_id;
1335 
1336  /*
1337  ||Check whether TCS on the invoice/DEbit memo have been settled.
1338  */
1339  CURSOR cur_chk_tcs_settlement ( cp_customer_trx_id JAI_AR_TRXS.CUSTOMER_TRX_ID%TYPE )
1340  IS
1341  SELECT
1342         '1'
1343   FROM
1344         jai_rgm_refs_all
1345   WHERE
1346         source_document_id = cp_customer_trx_id
1347   AND   settlement_id      IS NOT NULL            ;
1348 
1349   /*
1350   ||Now that some lines have got TCS type of taxes , check that all lines have got tcs type of taxes
1351   ||if any one line does not have TCS type of tax then throw an error
1352   */
1353    CURSOR cur_chk_tcs_for_all_lines (cp_customer_trx_id JAI_AR_TRXS.CUSTOMER_TRX_ID%TYPE )
1354    IS
1355    SELECT
1356           1
1357    FROM
1358         JAI_AR_TRX_LINES  jrctl,
1359   ra_customer_trx_lines_all rctla  --Added by JMEENA to filter discount line. for bug#8241099
1360    WHERE
1361 jrctl.customer_trx_id  = cp_customer_trx_id
1362 AND jrctl.customer_trx_id = rctla.customer_trx_id
1363 AND jrctl.customer_trx_line_id = rctla.customer_trx_line_id
1364 AND NVL (rctla.interface_line_attribute11,0) = 0 --Added by JMEENA to filter discount line. for bug#8241099, discount line will have interface_line_attribute11 greater than zero.
1365    AND   NOT EXISTS  ( SELECT   /*Check that TCS type of taxes are not  */
1366                               1
1367                        FROM
1368                              JAI_AR_TRX_TAX_LINES  jrcttl  ,
1369                              JAI_CMN_TAXES_ALL              jtc     ,
1370                              jai_regime_tax_types_v       jrttv
1371                        WHERE
1372                              jrctl.customer_trx_line_id   =   jrcttl.link_to_cust_trx_line_id
1373                        AND   jtc.tax_id                   =   jrcttl.tax_id
1374                        AND   jtc.tax_type                 =   jrttv.tax_type
1375                        AND   jrttv.regime_code            =   jai_constants.tcs_regime
1376                      );
1377 
1378 
1379   /*
1380   ||Check tcs surcharge applicability on document
1381   */
1382   CURSOR cur_chk_tcs_sur_tax ( cp_customer_trx_id  JAI_AR_TRX_LINES.CUSTOMER_TRX_ID%TYPE )
1383   IS
1384   SELECT
1385          count(*) surcharge_cnt
1386   FROM
1387          JAI_AR_TRX_LINES jrctl   ,
1388          JAI_AR_TRX_TAX_LINES jrcttl  ,
1389          JAI_CMN_TAXES_ALL              jtc     ,
1390          jai_regime_tax_types_v       jrttv
1391   WHERE
1392          jrctl.customer_trx_id      =  cp_customer_trx_id
1393   AND    jrctl.customer_trx_line_id =  jrcttl.link_to_cust_trx_line_id
1394   AND    jrcttl.tax_id              =  jtc.tax_id
1395   AND    jtc.tax_type               =  jrttv.tax_type
1396   AND    jrttv.tax_type             =  jai_constants.tax_type_tcs_surcharge
1397   AND    jrttv.regime_code          =  jai_constants.tcs_regime;
1398 
1399 
1400 
1401      /*******
1402      || Validate that the inventory_items for all lines of the invoice should have the same TCS item classification
1403      || for the same has already been settled
1404      ********/
1405      CURSOR cur_validate_all_items ( cp_customer_trx_id JAI_AR_TRXS.CUSTOMER_TRX_ID%TYPE )
1406      IS
1407      SELECT
1408             organization_id        ,
1409             inventory_item_id
1410      FROM
1411             JAI_AR_TRXS        jrct  ,
1412             JAI_AR_TRX_LINES  jrctl
1413      WHERE
1414             jrct.customer_trx_id  = cp_customer_trx_id
1415      AND    jrct.customer_trx_id  = jrctl.customer_trx_id;
1416 
1417    lv_trx_type               RA_CUST_TRX_TYPES_ALL.TYPE%TYPE                ;
1418    lv_doc_type               VARCHAR2(100)                                  ;
1419    ln_regime_id              JAI_RGM_DEFINITIONS.REGIME_ID%TYPE                     ;
1420    lv_org_tan_no             JAI_RGM_REGISTRATIONS.ATTRIBUTE_VALUE%TYPE             ;
1421    ln_threshold_slab_id      JAI_RGM_REFS_ALL.THRESHOLD_SLAB_ID%TYPE        ;
1422    ln_exists                 NUMBER(2)                                      ;
1423    lv_first_itm_class        JAI_RGM_REFS_ALL.ITEM_CLASSIFICATION%TYPE      ;
1424    lv_item_classification    JAI_RGM_REFS_ALL.ITEM_CLASSIFICATION%TYPE      ;
1425    lv_process_flag           VARCHAR2(2)                                    ;
1426    lv_process_message        VARCHAR2(4000)                                 ;
1427    ln_organization_id        JAI_RGM_REFS_ALL.ORGANIZATION_ID%TYPE          ;
1428    ln_surcharge_cnt          NUMBER(2)                                := 0  ;
1429    ld_source_doc_date        RA_CUST_TRX_LINE_GL_DIST_ALL.GL_DATE%TYPE      ;
1430 
1431    ln_open_interface         number;--Added by zhiwei for Open Interface bug#11683927
1432  BEGIN
1433 
1434    /*########################################################################################################
1435    || VARIABLES INITIALIZATION - PART -1
1436    ########################################################################################################*/
1437    lv_member_name        := 'VALIDATE_INVOICE';
1438    set_debug_context;
1439 
1440     /*commented by csahoo for bug# 6401388
1441    jai_cmn_debug_contexts_pkg.register ( pv_context => lv_context  ,
1442                                 pn_reg_id  => ln_reg_id
1443                               );*/
1444 
1445    lv_process_flag         := jai_constants.successful   ;
1446    lv_process_message      := null                       ;
1447 
1448    p_process_flag          := lv_process_flag            ;
1449    p_process_message       := lv_process_message         ;
1450 
1451    /*commented by csahoo for bug# 6401388
1452  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1453                                        pv_log_msg  =>  ' Validating the Document '||fnd_global.local_chr(10)
1454                                                      ||', p_ract.trx_number      -> '||p_ract.trx_number  ||fnd_global.local_chr(10)
1455                                                      ||', p_ract.customer_trx_id -> '||p_ract.customer_trx_id
1456                                     );*/
1457    /*########################################################################################################
1458    || CHECK TCS APPLICABILITY PART -2
1459    ########################################################################################################*/
1460     /*commented by csahoo for bug# 6401388
1461  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1462                                pv_log_msg  =>  ' Check TCS applicability '
1463                             );*/
1464    /*
1465    || Check whether the TCS is applicable on the document if no
1466    || do not process
1467    */
1468    OPEN  cur_chk_tcs_applicable ( cp_customer_trx_id => p_ract.customer_trx_id );
1469    FETCH cur_chk_tcs_applicable INTO ld_source_doc_date ,ln_organization_id;
1470    IF cur_chk_tcs_applicable%NOTFOUND THEN
1471   /*commented by csahoo for bug# 6401388
1472  jai_cmn_debug_contexts_pkg.print (  pn_reg_id  =>  ln_reg_id ,
1473                                         pv_log_msg  =>  ' TCS taxes not present on Invoice '
1474                                       );*/
1475      CLOSE cur_chk_tcs_applicable ;
1476      /*
1477      ||Check whether the invoice has been created due to the secondary creation
1478      || IF yes punch/reset to null, the customer_trx_id into jai_rgm_item_gen_docs.generated_doc_id based on the
1479      ||complete flag
1480      */
1481      /*commented by csahoo for bug# 6401388
1482  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1483                                          pv_log_msg  =>  'Call to procedure update_item_gen_docs to check whether this is the TCS secondary document'
1484                                       );*/
1485 
1486      update_item_gen_docs  ( p_trx_number        => p_ract.trx_number      ,
1487                              p_customer_trx_id   => p_ract.customer_trx_id ,
1488                              p_complete_flag     => p_ract.complete_flag   ,
1489                              p_org_id            => p_ract.org_id          ,
1490                              p_process_flag      => lv_process_flag        ,
1491                              p_process_message   => lv_process_message
1492                            );
1493      /*commented by csahoo for bug# 6401388
1494  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1495                                          pv_log_msg  =>  'returned from update_item_gen_docs lv_process_flag -> '||fnd_global.local_chr(10)
1496                                                        ||', lv_process_flag    -> '|| lv_process_flag     || fnd_global.local_chr(10)
1497                                                        ||', lv_process_message -> '|| lv_process_message  || fnd_global.local_chr(10)
1498                                       );*/
1499 
1500      /*commented by csahoo for bug# 6401388
1501  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1502                                          pv_log_msg  =>  'Skip furthur processing as Invoice does not have TCS type of taxes '
1503                                       );*/
1504 
1505      p_process_flag    := lv_process_flag    ;
1506      p_process_message := lv_process_message ;
1507 
1508      return;
1509 
1510    END IF;
1511    CLOSE cur_chk_tcs_applicable ;
1512 
1513     /*commented by csahoo for bug# 6401388
1514  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1515                                pv_log_msg  =>  ' Document Parameters '||fnd_global.local_chr(10)
1516                                               ||', source_doc_date      -> '||ld_source_doc_date  ||fnd_global.local_chr(10)
1517                                               ||', organization_id-> '||ln_organization_id
1518                             );*/
1519    /*########################################################################################################
1520    || SKIP FOR CM PART -3
1521    ########################################################################################################*/
1522 
1523    OPEN  cur_get_doc_det   ( cp_cust_trx_type_id => p_ract.cust_trx_type_id );
1524    FETCH cur_get_doc_det INTO lv_trx_type;
1525    CLOSE cur_get_doc_det ;
1526 
1527     /*commented by csahoo for bug# 6401388
1528  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1529                                pv_log_msg  =>  ' Current document type is '||lv_trx_type
1530                             );*/
1531    /*Bug 10245810 - Repository to be updated during CM Completion also*/
1532    /*
1533    IF lv_trx_type = 'CM' THEN
1534      p_process_flag := jai_constants.not_applicable;
1535      return;
1536    END IF;
1537    */
1538 
1539    /*########################################################################################################
1540    || VALIDATIONS FOR INVOICE COMPLETION PART - 4
1541    ########################################################################################################*/
1542 
1543    IF ln_event = jai_constants.trx_event_completion THEN
1544 
1545      /*commented by csahoo for bug# 6401388
1546  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1547                                 pv_log_msg  =>  ' Event is -> '||ln_event
1548                              );*/
1549 
1550      /*########################################################################################################
1551      || DERIVE DOCUMENT TYPE - PART - 4.1
1552      ########################################################################################################*/
1553 
1554 
1555      IF p_ract.complete_flag = jai_constants.yes THEN
1556 
1557       /*commented by csahoo for bug# 6401388
1558  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1559                                   pv_log_msg  =>  ' Complete -> '||p_ract.complete_flag
1560                                );*/
1561        /*Bug 10245810 - Added CM Type also. Repository to be updated during CM Completion also*/
1562        IF lv_trx_type IN (jai_constants.ar_invoice_type_inv,jai_constants.ar_doc_type_dm,jai_constants.ar_invoice_type_cm) THEN
1563          /*
1564          ||Invoice/DM completion
1565          */
1566          lv_doc_type := jai_constants.trx_type_inv_comp;
1567        ELSE
1568         /*commented by csahoo for bug# 6401388
1569  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1570                                     pv_log_msg  =>  ' Skip as type -> '||lv_trx_type ||' not applicable for TCS processing '
1571                                  );*/
1572          p_process_flag := jai_constants.not_applicable;
1573          return;
1574        END IF;
1575 
1576      ELSIF p_ract.complete_flag = jai_constants.no THEN
1577        /*
1578        ||
1579        */
1580        /*Bug 10245810 - Added CM Type also. Repository to be updated during CM InCompletion also*/
1581        IF lv_trx_type IN (jai_constants.ar_invoice_type_inv,jai_constants.ar_doc_type_dm,jai_constants.ar_invoice_type_cm) THEN
1582          /*
1583          ||Invoice/DM incompletion
1584          */
1585          lv_doc_type := jai_constants.trx_type_inv_incomp;
1586        ELSE
1587        /*commented by csahoo for bug# 6401388
1588  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1589                                     pv_log_msg  =>  ' Skip as type -> '||lv_trx_type ||' not applicable for TCS processing '
1590                                  );*/
1591          p_process_flag := jai_constants.not_applicable;
1592          return;
1593        END IF;
1594      END IF;
1595 
1596 
1597      /*########################################################################################################
1598      || INVOICE INCOMPLETION VALIDATIONS - PART - 4.2
1599      ########################################################################################################*/
1600 
1601 
1602      /*******
1603      || Validate that an invoice cannot be incompleted if the TCS
1604      || for the same has already been settled
1605      ********/
1606      /*Bug 10245810 - Added CM Type also*/
1607      IF  lv_trx_type IN ( jai_constants.ar_invoice_type_inv,
1608                           jai_constants.ar_doc_type_dm,
1609                           jai_constants.ar_invoice_type_cm
1610                         )                                   AND
1611          lv_doc_type  = jai_constants.trx_type_inv_incomp
1612 
1613      THEN                                                                                             -----------------A1
1614        /*
1615        ||Trx type is invoice or Debit memo
1616        */
1617        OPEN  cur_chk_tcs_settlement ( cp_customer_trx_id => p_ract.customer_trx_id );
1618        FETCH cur_chk_tcs_settlement INTO ln_exists;
1619        IF CUR_CHK_TCS_SETTLEMENT%FOUND THEN
1620          /*commented by csahoo for bug# 6401388
1621  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1622                                      pv_log_msg  =>  'Error :- Cannot Incomplete the invoice if it has already been settled  '||fnd_global.local_chr(10)
1623                                                    ||', lv_process_flag     -> '|| lv_process_flag  ||fnd_global.local_chr(10)
1624                                                    ||', lv_process_message  -> '||lv_process_message
1625                                   );*/
1626          p_process_flag    := jai_constants.expected_error;
1627          /*Bug 10245810 - Modified process message*/
1628          p_process_message := 'Cannot incomplete the Invoice/Debit/Credit Memo as TCS taxes have already been settled';
1629          return;
1630        END IF;
1631        CLOSE cur_chk_tcs_settlement;
1632        END IF;                                                                                               -----------------A2
1633      END IF;
1634 
1635 
1636      /*########################################################################################################
1637      || INVOICE COMPLETION VALIDATIONS - PART - 4.2
1638      ########################################################################################################*/
1639      /*Bug 10245810 - Added CM Type also*/
1640      IF  lv_trx_type IN ( jai_constants.ar_invoice_type_inv,
1641                           jai_constants.ar_doc_type_dm,
1642                           jai_constants.ar_invoice_type_cm
1643                         )                                   AND
1644         lv_doc_type  = jai_constants.trx_type_inv_comp
1645      THEN                                              -----------------A3
1646        /****************
1647        || Event is completion
1648        || Validate that all lines have TCS type of taxes
1649        || if no then error out
1650        *****************/
1651 
1652        OPEN  cur_chk_tcs_for_all_lines ( cp_customer_trx_id => p_ract.customer_trx_id );
1653        FETCH cur_chk_tcs_for_all_lines INTO ln_exists;
1654        IF CUR_CHK_TCS_FOR_ALL_LINES%FOUND THEN
1655          /*
1656          ||Rows with no TCS type of taxes exists hence error out
1657          */
1658          CLOSE cur_chk_tcs_for_all_lines ;
1659           /*commented by csahoo for bug# 6401388
1660  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1661                                      pv_log_msg  =>  'Error :- Cannot complete the invoice if some lines have TCS type of tax and some dont '||fnd_global.local_chr(10)
1662                                                    ||', lv_process_flag     -> '|| lv_process_flag  ||fnd_global.local_chr(10)
1663                                                    ||', lv_process_message  -> '||lv_process_message
1664                                   );*/
1665          p_process_flag    := jai_constants.expected_error;
1666          /*Bug 10245810 - Modified process message*/
1667          p_process_message := 'Cannot complete the Invoice/Debit/Credit Memo as some lines of the document do not have TCS type of taxes ';
1668          return;
1669          CLOSE cur_chk_tcs_for_all_lines ;
1670        END IF;
1671 
1672        /*******
1673        || Validate that the inventory_items for all lines of the invoice should have the same TCS item classification
1674        ********/
1675        lv_first_itm_class     := null;
1676        lv_item_classification := null;
1677        FOR rec_cur_validate_all_items IN cur_validate_all_items ( cp_customer_trx_id => p_ract.customer_trx_id )
1678        LOOP
1679 
1680          /*
1681          ||Get the value for the item classification pertaining to the IO and inventory item combination
1682          */
1683          jai_inv_items_pkg.jai_get_attrib (
1684                                           p_regime_code         => jai_constants.tcs_regime                     ,
1685                                           p_organization_id     => rec_cur_validate_all_items.organization_id   ,
1686                                           p_inventory_item_id   => rec_cur_validate_all_items.inventory_item_id ,
1687                                           p_attribute_code      => jai_constants.rgm_attr_cd_itm_class          ,
1688                                           p_attribute_value     => lv_item_classification                       ,
1689                                           p_process_flag        => lv_process_flag                              ,
1690                                           p_process_msg         => lv_process_message
1691                                         );
1692 
1693          IF lv_process_flag = jai_constants.expected_error    OR                      ---------A2
1694             lv_process_flag = jai_constants.unexpected_error
1695          THEN
1696            /*
1697            || As Returned status is an error/not applicable hence:-
1698            || Set out variables p_process_flag and p_process_message accordingly
1699            */
1700            --call to debug package
1701           /*commented by csahoo for bug# 6401388
1702  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1703                                       pv_log_msg  =>  'Error In processing of jai_inv_items_pkg.jai_get_attrib'||fnd_global.local_chr(10)
1704                                                     ||', lv_process_flag     -> '|| lv_process_flag  ||fnd_global.local_chr(10)
1705                                                     ||', lv_process_message  -> '||lv_process_message
1706                                    );*/
1707 
1708            p_process_flag    := lv_process_flag    ;
1709            p_process_message := lv_process_message ;
1710            return;
1711          END IF;                                                                      ---------A2
1712 
1713          IF lv_first_itm_class IS NULL THEN
1714            /*
1715            ||First time assignment
1716            */
1717            lv_first_itm_class := lv_item_classification;
1718          END IF;
1719 
1720          /*
1721          ||IF any one of the lines do not match with the item TCS classification of the first line
1722          || then stop the transaction and throw an error.
1723          */
1724          IF nvl(lv_first_itm_class,'$$') <> nvl(lv_item_classification,'###') THEN
1725            /*commented by csahoo for bug# 6401388
1726  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1727                                       pv_log_msg  =>  'Error :- Cannot complete as all lines do not belong to the same Item Classification '||fnd_global.local_chr(10)
1728                                                     ||', lv_process_flag     -> '|| lv_process_flag  ||fnd_global.local_chr(10)
1729                                                     ||', lv_process_message  -> '||lv_process_message
1730                                    );*/
1731 
1732            p_process_flag    := jai_constants.expected_error   ;
1733            p_process_message := 'Cannot complete invoice. All lines should either have the same TCS item classification or none of the line should have TCS type of taxes' ;
1734            return;
1735          END IF;
1736 
1737        END LOOP;
1738 
1739      /*########################################################################################################
1740      || THRESHOLD VALIDATIONS - PART - 4.3
1741      ########################################################################################################*/
1742 
1743      OPEN  cur_chk_tcs_sur_tax ( cp_customer_trx_id  => p_ract.customer_trx_id );
1744      FETCH cur_chk_tcs_sur_tax INTO ln_surcharge_cnt;
1745      CLOSE cur_chk_tcs_sur_tax ;
1746 
1747      OPEN c_get_rgm_attribute (   cp_regime_code           =>   jai_constants.tcs_regime                  ,
1748                                   cp_attribute_code        =>   jai_constants.rgm_attr_code_org_tan       ,
1749                                   cp_organization_id       =>   ln_organization_id
1750                               ) ;
1751      FETCH c_get_rgm_attribute INTO ln_regime_id, lv_org_tan_no ;
1752      CLOSE c_get_rgm_attribute;
1753 
1754      jai_rgm_thhold_proc_pkg.get_threshold_slab_id   (
1755                                                         p_regime_id           =>    ln_regime_id                                               ,
1756                                                         p_org_tan_no          =>    lv_org_tan_no                                              ,
1757                                                         p_party_type          =>    jai_constants.party_type_customer                          ,
1758                                                         p_party_id            =>    nvl(p_ract.ship_to_customer_id,p_ract.bill_to_customer_id) ,
1759                                                         p_source_trx_date     =>    ld_source_doc_date                                         ,
1760                                                         p_org_id              =>    p_ract.org_id                                              ,
1761                                                         p_threshold_slab_id   =>    ln_threshold_slab_id                                       ,
1762 							p_called_from	      =>    jai_constants.tcs_event_surcharge			       , --Added by mmurtuza for bug#13025013
1763                                                         p_process_flag        =>    lv_process_flag                                            ,
1764                                                         p_process_message     =>    lv_process_message
1765                                                     );
1766 
1767      IF lv_process_flag = jai_constants.expected_error    OR                      ---------A2
1768         lv_process_flag = jai_constants.unexpected_error  OR
1769         lv_process_flag = jai_constants.not_applicable
1770      THEN
1771        /*
1772        || As Returned status is an error/not applicable hence:-
1773        || Set out variables p_process_flag and p_process_message accordingly
1774        */
1775         --call to debug package
1776        p_process_flag    := lv_process_flag    ;
1777        p_process_message := lv_process_message ;
1778        return;
1779      END IF;                                                                      ---------A2
1780 
1781 
1782      --Added by zhiwei for Open Interface bug#11683927 begin
1783      -------------------------------------------------------------------
1784 
1785          SELECT COUNT(CUSTOMER_TRX_ID)
1786          INTO ln_open_interface
1787          FROM ra_customer_trx_lines_all
1788          WHERE CUSTOMER_TRX_ID = p_ract.customer_trx_id
1789          AND   global_attribute_category like '%OFI TAX IMPORT%';
1790 
1791           if( nvl(ln_open_interface,0) > 0 )then
1792               null;
1793           else
1794      -------------------------------------------------------------------
1795      --Added by zhiwei for Open Interface bug#11683927  end
1796 
1797      IF ln_threshold_slab_id IS NOT NULL  THEN
1798        /*
1799        ||IF threshold level is up and surcharge type of taxes not present
1800        || on the invoice line then error
1801        */
1802        IF ln_surcharge_cnt  = 0 THEN        /* Surcharge does not exist */
1803          p_process_flag    := jai_constants.expected_error ;
1804          p_process_message := 'Cannot complete invoice as surcharge is applicable however TCS Surcharge tax is not found on the document' ;
1805          return;
1806        END IF;
1807      ELSE
1808        /*
1809        ||IF threshold level is down and surcharge type of taxes are present
1810        || on the invoice line then error
1811        */
1812        IF ln_surcharge_cnt  = 1 THEN        /* Surcharge exist */
1813          p_process_flag    := jai_constants.expected_error ;
1814          p_process_message := 'Cannot complete invoice as surcharge is not applicable however TCS Surcharge tax is found on the document' ;
1815          return;
1816        END IF;
1817      END IF;
1818      end if;--Added by zhiwei for Open Interface bug#11683927
1819    END IF;   /*Event is Completion  and document type is invoice or DM*/                           -----------------A3
1820 
1821    p_document_type := lv_doc_type;
1822 
1823    /*commented by csahoo for bug# 6401388
1824  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1825                               pv_log_msg  =>  '**************** END OF VALIDATE_INVOICE ****************'
1826                            );
1827    jai_cmn_debug_contexts_pkg.deregister (pn_reg_id => ln_reg_id);*/
1828  END validate_invoice;
1829 
1830 
1831 
1832 PROCEDURE  validate_app_unapp (
1833                                  p_araa                      IN              AR_RECEIVABLE_APPLICATIONS_ALL%ROWTYPE     ,
1834                                  p_document_type OUT NOCOPY VARCHAR2                                   ,
1835                                  p_item_classification       OUT NOCOPY      JAI_RGM_REFS_ALL.ITEM_CLASSIFICATION%TYPE  ,
1836                                  p_process_flag              OUT NOCOPY      VARCHAR2                                   ,
1837                                  p_process_message           OUT NOCOPY      VARCHAR2
1838                               )
1839 AS
1840   ln_reg_id           NUMBER;
1841   /*
1842   || Applied to Document of the receivable application has TCS type of tax.
1843   */
1844   CURSOR cur_chk_tcs_applicable (cp_customer_trx_id RA_CUSTOMER_TRX_ALL.CUSTOMER_TRX_ID%TYPE )
1845   IS
1846   SELECT
1847           jrct.organization_id    ,
1848           jrctl.inventory_item_id ,
1849           gl_dist.amount
1850   FROM
1851           ra_customer_trx_all          rcta    ,
1852           ra_cust_trx_line_gl_dist_all gl_dist ,
1853           JAI_AR_TRXS        jrct    ,
1854           JAI_AR_TRX_LINES  jrctl   ,
1855           JAI_AR_TRX_TAX_LINES  jrcttl  ,
1856           JAI_CMN_TAXES_ALL              jtc     ,
1857           jai_regime_tax_types_v       jrttv
1858   WHERE
1859           rcta.complete_flag           =   jai_constants.yes
1860   AND     rcta.customer_trx_id         =   cp_customer_trx_id
1861   AND     gl_dist.customer_trx_id      =   rcta.customer_trx_id
1862   AND     gl_dist.account_class        =   jai_constants.account_class_rec
1863   AND     gl_dist.latest_rec_flag      =   jai_constants.yes
1864   AND     rcta.customer_trx_id         =   jrct.customer_trx_id
1865   AND     jrct.customer_trx_id         =   jrctl.customer_trx_id
1866   AND     jrctl.customer_trx_line_id   =   jrcttl.link_to_cust_trx_line_id
1867   AND     jtc.tax_id                   =   jrcttl.tax_id
1868   AND     jrttv.tax_type               =   jtc.tax_type /* Applied to doc has got TCS type of tax*/
1869   AND     jrttv.regime_code            =   jai_constants.tcs_regime;
1870 
1871   /*
1872   ||Get the sign of the Cash receipt document
1873   */
1874   CURSOR cur_get_cr_sign (cp_cash_receipt_id JAI_AR_CASH_RECEIPTS_ALL.CASH_RECEIPT_ID%TYPE )
1875   IS
1876   SELECT
1877         sign (nvl(amount,0)) app_fr_sign
1878   FROM
1879         ar_cash_receipts_all
1880   WHERE
1881         cash_receipt_id = cp_cash_receipt_id;
1882 
1883   /*
1884   ||Get the sign of the Credit Memo document
1885   */
1886   CURSOR cur_get_cm_sign (cp_cm_customer_trx_id  RA_CUST_TRX_LINE_GL_DIST_ALL.CUSTOMER_TRX_ID%TYPE )
1887   IS
1888   SELECT
1889         sign(nvl(amount,0)) app_fr_sign
1890   FROM
1891         ra_cust_trx_line_gl_dist_all
1892   WHERE
1893         account_class    = jai_constants.account_class_rec
1894   AND   latest_rec_flag  = jai_constants.yes
1895   AND   customer_trx_id  = cp_cm_customer_trx_id;
1896 
1897   /*
1898   || Check that the Cash receipt has got TCS type of confirmed taxes
1899   */
1900   CURSOR cur_chk_crtcs_applicable ( cp_cash_receipt_id  JAI_AR_CASH_RECEIPTS_ALL.CASH_RECEIPT_ID%TYPE )
1901   IS
1902   SELECT
1903            jcra.item_classification,
1904            acra.amount
1905   FROM
1906            ar_cash_receipts_all     acra ,
1907            jai_ar_cash_receipts_all jcra ,
1908            jai_cmn_document_taxes   jdt  ,
1909            jai_regime_tax_types_v   jrttv
1910   WHERE
1911            jcra.cash_receipt_id   =   acra.cash_receipt_id
1912   AND      jcra.cash_receipt_id   =   cp_cash_receipt_id
1913   AND      jcra.cash_receipt_id   =   jdt.source_doc_id
1914   AND      jdt.source_table_name  =   jai_constants.jai_cash_rcpts   /* 'JAI_AR_CASH_RECEIPTS_ALL' */
1915   AND      jcra.confirm_flag      =   jai_constants.yes
1916   AND      jdt.tax_type           =   jrttv.tax_type /* Applied to doc has got TCS type of tax*/
1917   AND      jrttv.regime_code      = jai_constants.tcs_regime
1918   AND      jdt.source_doc_type  = JAI_CONSTANTS.ar_cash;    --added by eric for a bug
1919 
1920 
1921   /*
1922   || Get the application details for the current unapplications from the repository
1923   */
1924   CURSOR  cur_chk_parent_rec ( cp_applied_fr_doc_id   JAI_RGM_REFS_ALL.APP_FROM_DOCUMENT_ID%TYPE ,
1925                                cp_applied_to_doc_id   JAI_RGM_REFS_ALL.APP_TO_DOCUMENT_ID%TYPE
1926                              )
1927   IS
1928   SELECT
1929         trx_ref_id               ,
1930         settlement_id
1931   FROM
1932         jai_rgm_refs_all
1933   WHERE
1934         app_from_document_id = cp_applied_fr_doc_id
1935   AND   app_to_document_id   = cp_applied_to_doc_id ;
1936 
1937   ln_sign_of_app_fr_doc           NUMBER(3)                                           ;
1938   ln_sign_of_app                  NUMBER(3)                                           ;
1939   lv_exists                       VARCHAR2(1)                                         ;
1940   ln_app_fr_itm_class             JAI_AR_CASH_RECEIPTS_ALL.ITEM_CLASSIFICATION%TYPE   ;
1941   ln_app_fr_organization_id       JAI_AR_TRXS.ORGANIZATION_ID%TYPE          ;
1942   ln_app_fr_inventory_item_id     JAI_AR_TRX_LINES.INVENTORY_ITEM_ID%TYPE  ;
1943   ln_app_fr_amount                RA_CUST_TRX_LINE_GL_DIST_ALL.AMOUNT%TYPE            ;
1944 
1945   ln_app_to_amount                RA_CUST_TRX_LINE_GL_DIST_ALL.AMOUNT%TYPE            ;
1946   ln_app_to_itm_class             JAI_AR_CASH_RECEIPTS_ALL.ITEM_CLASSIFICATION%TYPE   ;
1947   ln_app_to_organization_id       JAI_AR_TRXS.ORGANIZATION_ID%TYPE          ;
1948   ln_app_to_inventory_item_id     JAI_AR_TRX_LINES.INVENTORY_ITEM_ID%TYPE  ;
1949 
1950   lv_app_doc_type                 VARCHAR2(100)                                       ;
1951   rec_cur_chk_parent_rec          CUR_CHK_PARENT_REC%ROWTYPE                          ;
1952   lv_process_flag                 VARCHAR2(2)                                         ;
1953   lv_process_message              VARCHAR2(4000)                                      ;
1954 
1955 
1956 BEGIN
1957 
1958   /*########################################################################################################
1959   || VARIABLES INITIALIZATION - PART -1
1960   ########################################################################################################*/
1961 
1962   lv_member_name        := 'VALIDATE_APP_UNAPP';
1963   set_debug_context;
1964 
1965  /*commented by csahoo for bug# 6401388
1966  jai_cmn_debug_contexts_pkg.register ( pv_context => lv_context  ,
1967                                pn_reg_id  => ln_reg_id
1968                              );*/
1969 
1970   lv_process_flag         := jai_constants.successful   ;
1971   lv_process_message      := null                       ;
1972 
1973   p_process_flag          := lv_process_flag            ;
1974   p_process_message       := lv_process_message         ;
1975   ln_sign_of_app_fr_doc   := null                       ;
1976   ln_sign_of_app          := sign(nvl(p_araa.amount_applied,0));
1977 
1978    /*commented by csahoo for bug# 6401388
1979  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
1980                                       pv_log_msg  =>  ' Processing the APPLICATION record ' ||fnd_global.local_chr(10)
1981                                            ||', receivable_application_id -> '  ||p_araa.receivable_application_id    ||fnd_global.local_chr(10)
1982                                            ||', application_type          -> '  ||p_araa.application_type             ||fnd_global.local_chr(10)
1983                                            ||', status                    -> '  ||p_araa.status                       ||fnd_global.local_chr(10)
1984                                            ||', display                   -> '  ||p_araa.display                      ||fnd_global.local_chr(10)
1985                                            ||', cash_receipt_id           -> '  ||p_araa.cash_receipt_id              ||fnd_global.local_chr(10)
1986                                            ||', amount_applied            -> '  ||p_araa.amount_applied               ||fnd_global.local_chr(10)
1987                                            ||', applied_customer_trx_id   -> '  ||p_araa.applied_customer_trx_id
1988                                     );*/
1989 
1990 
1991   /*########################################################################################################
1992   || CHECK TCS APPLICABILITY ON APPLIED FROM DOCUMENTS AND DERIVE APPLICATION TYPE - PART -2
1993   ########################################################################################################*/
1994 
1995   IF p_araa.application_type IN  (jai_constants.ar_cash              ,
1996                                   jai_constants.ar_status_activity
1997                                  )                                   AND                            -------------A1
1998      p_araa.cash_receipt_id  IS NOT NULL
1999   THEN
2000     /*
2001     || Application is CASH
2002     || Check that cash receipt has tcs type of taxes which have been confirmed
2003     || Exit processing if the same is not found
2004     */
2005     /*commented by csahoo for bug# 6401388
2006  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2007                                         pv_log_msg  =>  ' Checking TCS applicability On applied from documents'
2008                                       );*/
2009     OPEN  cur_chk_crtcs_applicable (cp_cash_receipt_id => p_araa.cash_receipt_id );
2010     FETCH cur_chk_crtcs_applicable INTO ln_app_fr_itm_class, ln_app_fr_amount;
2011 
2012     IF cur_chk_crtcs_applicable%NOTFOUND THEN
2013      /*commented by csahoo for bug# 6401388
2014  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2015                                          pv_log_msg  =>  ' Skip As TCS not applicable ON Derived from documents '
2016                                       );*/
2017       CLOSE cur_chk_crtcs_applicable;
2018       p_process_flag := jai_constants.not_applicable ;
2019       return ;
2020     ELSE
2021       /*
2022       ||Check for ACTIVITY I.E receipt to receipt or receipt to credit memo any other type of application other than Receipt/CM to INV or DM
2023       */
2024        /*commented by csahoo for bug# 6401388
2025  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2026                                            pv_log_msg  =>  'TCS is applicable'
2027                                           );*/
2028 
2029       IF p_araa.status = jai_constants.ar_status_activity THEN
2030         /*
2031         || As current receivable application is an activity indicating a receipt to receipt getting applied to another receipt
2032         || or Credit Memo hence stop this processing as otherwise this would lead to down stream TCS data corruption.
2033         */
2034         /*commented by csahoo for bug# 6401388
2035         jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2036                                             pv_log_msg  =>  'Cannot apply a RECEIPT to any other document (any of them having TCS applicability) other than a Invoice or Debit Memo'
2037                                           );*/
2038         p_process_flag    := jai_constants.expected_error;
2039         p_process_message := 'Cannot apply a RECEIPT to any other document (any of them having TCS applicability) other than a Invoice or Debit Memo';
2040         return;
2041 
2042       END IF;
2043     END IF;
2044 
2045     CLOSE cur_chk_crtcs_applicable;
2046 
2047     /*
2048     ||Get the sign of the cash receipt
2049     */
2050     OPEN  cur_get_cr_sign ( cp_cash_receipt_id  => p_araa.cash_receipt_id );
2051     FETCH cur_get_cr_sign INTO ln_sign_of_app_fr_doc ;
2052     CLOSE cur_get_cr_sign;
2053 
2054     /*
2055     || IF sign of amount field of receivable application is the same as the sign of the cash receipt amount
2056     || then application is receipt application else it would be receipt un application
2057     */
2058     IF ln_sign_of_app = ln_sign_of_app_fr_doc THEN
2059       lv_app_doc_type := jai_constants.trx_type_rct_app ;  /* Event is 'RECEIPT_APPLICATION' */
2060     ELSE
2061       lv_app_doc_type :=  jai_constants.trx_type_rct_unapp ;/* Event is 'RECEIPT_UNAPPLICATION' */
2062     END IF;
2063 
2064 
2065   ELSIF p_araa.application_type  = jai_constants.ar_invoice_type_cm  AND                            -------------A1
2066         p_araa.customer_trx_id  IS NOT NULL
2067   THEN
2068     /*
2069     ||Application is Credit Memo
2070     */
2071     OPEN  cur_chk_tcs_applicable( cp_customer_trx_id => p_araa.customer_trx_id );
2072     FETCH cur_chk_tcs_applicable INTO  ln_app_fr_organization_id, ln_app_fr_inventory_item_id, ln_app_fr_amount;
2073 
2074      /*commented by csahoo for bug# 6401388
2075  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2076                                         pv_log_msg  =>  'Application type is '||p_araa.application_type
2077                                       );*/
2078 
2079     IF CUR_CHK_TCS_APPLICABLE%NOTFOUND THEN
2080      /*commented by csahoo for bug# 6401388
2081  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2082                                         pv_log_msg  =>  ' Skip As TCS not applicable ON Derived from documents '
2083                                       );*/
2084      CLOSE cur_chk_tcs_applicable;
2085      p_process_flag := jai_constants.not_applicable ;
2086      return;
2087     END IF;
2088     CLOSE cur_chk_tcs_applicable;
2089 
2090     /*
2091     ||Get the sign of the Credit Memo
2092     */
2093     OPEN  cur_get_cm_sign ( cp_cm_customer_trx_id  =>  p_araa.customer_trx_id ) ;
2094     FETCH cur_get_cm_sign INTO ln_sign_of_app_fr_doc;
2095     CLOSE cur_get_cm_sign;
2096 
2097     /*
2098     || IF sign of amount field of receivable application is the same as the sign of the Credit Memo amount
2099     || then application is Credit Memo Application else it would be Credit Memo Unapplication
2100     */
2101     IF ln_sign_of_app = ln_sign_of_app_fr_doc THEN
2102       lv_app_doc_type := jai_constants.trx_type_cm_app ;  /* Event is 'CREDIT_MEMO_APPLICATION' */
2103     ELSE
2104       lv_app_doc_type := jai_constants.trx_type_cm_unapp ;/* Event is 'CREDIT_MEMO_UNAPPLICATION' */
2105     END IF;
2106 
2107    /*
2108     ||Get the value for the item classification pertaining to the IO and inventory item combination
2109     */
2110     jai_inv_items_pkg.jai_get_attrib (
2111                                     p_regime_code         => jai_constants.tcs_regime              ,
2112                                     p_organization_id     => ln_app_fr_organization_id             ,
2113                                     p_inventory_item_id   => ln_app_fr_inventory_item_id           ,
2114                                     p_attribute_code      => jai_constants.rgm_attr_cd_itm_class   ,
2115                                     p_attribute_value     => ln_app_fr_itm_class                   ,
2116                                     p_process_flag        => lv_process_flag                       ,
2117                                     p_process_msg         => lv_process_message
2118                                   );
2119 
2120     IF lv_process_flag = jai_constants.expected_error    OR                      ---------A2
2121        lv_process_flag = jai_constants.unexpected_error
2122     THEN
2123       /*
2124       || As Returned status is an error hence:-
2125       || Set out variables p_process_flag and p_process_message accordingly
2126       */
2127       --call to debug package
2128      /*commented by csahoo for bug# 6401388
2129  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2130                                         pv_log_msg  =>  ' Error in getting the item attribute'||fnd_global.local_chr(10)
2131                                         ||', p_process_flag      -> '|| p_process_flag
2132                                         ||', lv_process_message  -> '|| lv_process_message
2133                                       );*/
2134       p_process_flag    := lv_process_flag    ;
2135       p_process_message := lv_process_message ;
2136       return;
2137     END IF;                                                                      ---------A2
2138 
2139   ELSE                                                                -------------A1
2140      /*
2141      ||Return in case the scenario is niether CASH nor CM
2142      */
2143      /*commented by csahoo for bug# 6401388
2144  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2145                                         pv_log_msg  =>  ' Skip as scenario is niether CASH nor CM '
2146                                       );*/
2147      p_process_flag := jai_constants.not_applicable ;
2148      return;
2149   END IF;                                                             -------------A1
2150 
2151   /*commented by csahoo for bug# 6401388
2152  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2153                                       pv_log_msg  =>  ' Application document type is '||lv_app_doc_type
2154                                     );*/
2155 
2156 
2157   /*########################################################################################################
2158   || Derive APPLIED TO DOCUMENT VALUES AND CHECK TCS APPLICABILITY ON APPLIED TO DOCUMENTS - PART -3
2159   ########################################################################################################*/
2160 
2161   /*
2162   || Check that the applied to document has been completed and has got TCS type of taxes.
2163   || IF no then return
2164   */
2165   OPEN  cur_chk_tcs_applicable( cp_customer_trx_id => p_araa.applied_customer_trx_id );
2166   FETCH cur_chk_tcs_applicable INTO ln_app_to_organization_id, ln_app_to_inventory_item_id, ln_app_to_amount ;
2167 
2168   IF CUR_CHK_TCS_APPLICABLE%NOTFOUND THEN
2169    CLOSE cur_chk_tcs_applicable;
2170    p_process_flag := jai_constants.not_applicable ;
2171    /*commented by csahoo for bug# 6401388
2172  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2173                                       pv_log_msg  =>  ' Skip the processing as TCS is not applicable on the transaction'
2174                                     );*/
2175    return;
2176   END IF;
2177   CLOSE cur_chk_tcs_applicable;
2178 
2179   /*
2180   ||Get the value for the item classification pertaining to the IO and inventory item combination
2181   */
2182   jai_inv_items_pkg.jai_get_attrib (
2183                                   p_regime_code         => jai_constants.tcs_regime               ,
2184                                   p_organization_id     => ln_app_to_organization_id              ,
2185                                   p_inventory_item_id   => ln_app_to_inventory_item_id            ,
2186                                   p_attribute_code      => jai_constants.rgm_attr_cd_itm_class    ,
2187                                   p_attribute_value     => ln_app_to_itm_class                    ,
2188                                   p_process_flag        => lv_process_flag                        ,
2189                                   p_process_msg         => lv_process_message
2190                                 );
2191 
2192   IF lv_process_flag = jai_constants.expected_error    OR                      ---------A2
2193      lv_process_flag = jai_constants.unexpected_error
2194   THEN
2195     /*
2196     || As Returned status is an error hence:-
2197     || Set out variables p_process_flag and p_process_message accordingly
2198     */
2199     --call to debug package
2200      /*commented by csahoo for bug# 6401388
2201  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2202                                         pv_log_msg  =>  ' Error in getting the item classification '||fnd_global.local_chr(10)
2203                                         ||', p_process_flag      -> '|| p_process_flag
2204                                         ||', lv_process_message  -> '|| lv_process_message
2205                                       );*/
2206     p_process_flag    := lv_process_flag    ;
2207     p_process_message := lv_process_message ;
2208     return;
2209   END IF;                                                                      ---------A2
2210 
2211 
2212   /*########################################################################################################
2213   || RESTRICTIONS ON APPLICATION - PART -4
2214   ########################################################################################################*/
2215 
2216   /*
2217   || All lines of the applied from and to documents should belong to the same item classification.
2218   || Do not allow a transaction if this rule is not followed.
2219   */
2220 
2221   IF lv_app_doc_type IN  ( jai_constants.trx_type_rct_app ,
2222                            jai_constants.trx_type_cm_app
2223                          )
2224   THEN
2225     IF ln_app_fr_itm_class <> ln_app_to_itm_class THEN
2226 
2227      /*commented by csahoo for bug# 6401388
2228  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2229                                         pv_log_msg  =>  ' Item classification for the application from and to item class do not match hence error '||fnd_global.local_chr(10)
2230                                         ||', p_process_flag      -> '|| p_process_flag
2231                                         ||', lv_process_message  -> '|| lv_process_message
2232                                       );*/
2233       p_process_flag    := jai_constants.expected_error ;
2234       p_process_message := 'Application is not allowed as the APPLIED FROM and TO DOCUMENTS have different item classifications.';
2235       return;
2236     END IF;
2237   END IF;
2238 
2239   /*
2240   ||Do not allow overapplication transactions in case they both have TCS type of taxes
2241   */
2242   IF nvl(p_araa.amount_applied,0) >  nvl(ln_app_to_amount,0) THEN
2243      /*commented by csahoo for bug# 6401388
2244  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2245                                          pv_log_msg  => 'Case for Overapplication detected , Stop '||fnd_global.local_chr(10)
2246                                                       ||', p_araa.amount_applied -> '|| p_araa.amount_applied     ||fnd_global.local_chr(10)
2247                                                       ||', applied to amount     -> '|| ln_app_to_amount          ||fnd_global.local_chr(10)
2248                                                       ||', p_process_flag        -> '|| p_process_flag            ||fnd_global.local_chr(10)
2249                                                       ||', lv_process_message    -> '|| lv_process_message
2250                                       );*/
2251     p_process_flag    := jai_constants.expected_error ;
2252     p_process_message := 'Over Application of a document to other is not allowed if both the taxes have TCS type of taxes .';
2253     return;
2254   END IF;
2255 
2256   /*
2257   ||Do not allow a receipt to be applied to another receipt in case both have TCS type of taxes
2258   */
2259 
2260 
2261   /*########################################################################################################
2262   || RESTRICTIONS ON RECEIPT UNAPPLICATION
2263   ########################################################################################################*/
2264 
2265   IF lv_app_doc_type = jai_constants.trx_type_rct_unapp THEN
2266    /*
2267    ||Validate that the parent receipt application record is present in the repository.
2268    */
2269     OPEN cur_chk_parent_rec ( cp_applied_fr_doc_id  => p_araa.cash_receipt_id ,
2270                               cp_applied_to_doc_id  => p_araa.applied_customer_trx_id
2271                             );
2272     FETCH cur_chk_parent_rec INTO rec_cur_chk_parent_rec;
2273 
2274     IF CUR_CHK_PARENT_REC%NOTFOUND THEN
2275       /*
2276       ||Exit processing as original application did not hit the repository.
2277       */
2278        CLOSE cur_chk_parent_rec;
2279       /*commented by csahoo for bug# 6401388
2280  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2281                                            pv_log_msg  => 'Skip,case For unapplication,  Parent transaction record is not present in the repository'||fnd_global.local_chr(10)
2282                                                         ||', p_process_flag        -> '|| p_process_flag            ||fnd_global.local_chr(10)
2283                                         );*/
2284 
2285        p_process_flag := jai_constants.not_applicable ;
2286        return;
2287     END IF;
2288 
2289     CLOSE cur_chk_parent_rec;
2290 
2291     IF rec_cur_chk_parent_rec.settlement_id is NOT NULL THEN
2292       /*
2293       || Original Application already settled hence
2294       */
2295        p_process_flag    := jai_constants.expected_error ;
2296        p_process_message := 'Parent application has already been settled. hence cannot unapply';
2297        /*commented by csahoo for bug# 6401388
2298  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2299                                           pv_log_msg  =>  ' Error :-Parent application has already been settled. hence cannot unapply'||fnd_global.local_chr(10)
2300                                           ||', p_process_flag      -> '|| p_process_flag
2301                                           ||', lv_process_message  -> '|| lv_process_message
2302                                         );*/
2303 
2304        return;
2305     END IF;
2306 
2307   END IF;
2308 
2309 
2310   /*########################################################################################################
2311   || RESTRICTIONS ON CREDIT MEMO UNAPPLICATION
2312   ########################################################################################################*/
2313 
2314   IF lv_app_doc_type = jai_constants.trx_type_cm_unapp THEN
2315     /*
2316     ||Validate that the parent credit memo application record is present in the repository.
2317     */
2318     OPEN cur_chk_parent_rec ( cp_applied_fr_doc_id  => p_araa.customer_trx_id         ,
2319                               cp_applied_to_doc_id  => p_araa.applied_customer_trx_id
2320                             );
2321     FETCH cur_chk_parent_rec INTO rec_cur_chk_parent_rec;
2322     IF CUR_CHK_PARENT_REC%NOTFOUND THEN
2323       /*
2324       ||Exit processing as original application did not hit the repository.
2325       */
2326      /*commented by csahoo for bug# 6401388
2327  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2328                                         pv_log_msg  =>  ' Skip the processing as TCS is not applicable on the transaction'||fnd_global.local_chr(10)
2329                                         ||', p_process_flag      -> '|| p_process_flag
2330                                       );*/
2331        CLOSE cur_chk_parent_rec;
2332        p_process_flag := jai_constants.not_applicable ;
2333        return;
2334     END IF;
2335 
2336     CLOSE cur_chk_parent_rec;
2337 
2338     IF rec_cur_chk_parent_rec.settlement_id is NOT NULL THEN
2339       /*
2340       || Original Application already settled hence
2341       */
2342        /*commented by csahoo for bug# 6401388
2343  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2344                                           pv_log_msg  =>  ' Error :-Parent application has already been settled. hence cannot unapply'||fnd_global.local_chr(10)
2345                                           ||', p_process_flag      -> '|| p_process_flag
2346                                           ||', lv_process_message  -> '|| lv_process_message
2347                                         );*/
2348        p_process_flag := jai_constants.expected_error ;
2349        p_process_message := 'Parent application has already been settled. hence cannot unapply';
2350        return;
2351     END IF;
2352   END IF;
2353 
2354 
2355   /*########################################################################################################
2356   || Assign values to return variables
2357   ########################################################################################################*/
2358 
2359   p_document_type       := lv_app_doc_type        ;
2360   p_item_classification := ln_app_to_itm_class    ;
2361 
2362   /*commented by csahoo for bug# 6401388
2363  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2364                              pv_log_msg  =>  '**************** PROCEDURE VALIDATE_APP_UNAPP SUCCESSFUL ****************'
2365                           );
2366 
2367   jai_cmn_debug_contexts_pkg.deregister (pn_reg_id => ln_reg_id);*/
2368 
2369 END validate_app_unapp;
2370 
2371 PROCEDURE validate_receipts  (  p_acra             IN            AR_CASH_RECEIPTS_ALL%ROWTYPE  ,
2372                                 p_document_type    IN           VARCHAR2                      ,
2373                                 p_process_flag     OUT NOCOPY    VARCHAR2                     ,
2374                                 p_process_message  OUT NOCOPY    VARCHAR2
2375                                                )
2376 IS
2377   ln_reg_id                 NUMBER;
2378   lv_process_flag           VARCHAR2(2)                               ;
2379   lv_process_message        VARCHAR2(2000)                            ;
2380 
2381   /*
2382   || Check that the document has has TCS type of tax.
2383   */
2384   CURSOR cur_chk_tcs_applicable (cp_cash_receipt_id JAI_AR_CASH_RECEIPTS_ALL.CASH_RECEIPT_ID%TYPE )
2385   IS
2386   SELECT
2387           1
2388   FROM
2389           jai_ar_cash_receipts_all  jcra   ,
2390           jai_cmn_document_taxes    jdt    ,
2391           jai_regime_tax_types_v    jrttv
2392   WHERE
2393           jcra.cash_receipt_id         =   cp_cash_receipt_id
2394   AND     jcra.cash_receipt_id         =   jdt.source_doc_id
2395   AND     jdt.tax_type                 =   jrttv.tax_type  /* Applied to doc has got TCS type of tax*/
2396   AND     jrttv.regime_code            =   jai_constants.tcs_regime
2397   AND     jcra.confirm_flag            =   jai_constants.yes
2398   AND     jdt.source_doc_type  = JAI_CONSTANTS.ar_cash;    --added by eric for a bug
2399 
2400  /*
2401  ||Get the last record pertaining to the cash receipt confirmation
2402  */
2403  CURSOR cur_chk_tcs_settlement ( cp_source_document_id   jai_rgm_refs_all.source_document_id%TYPE   ,
2404                                  cp_source_document_type jai_rgm_refs_all.source_document_type%TYPE
2405                                )
2406   IS
2407  SELECT
2408         trx_ref_id        ,
2409         settlement_id
2410   FROM
2411         jai_rgm_refs_all a
2412   WHERE
2413         trx_ref_id   =  ( SELECT
2414                                  max(trx_ref_id)
2415                           FROM
2416                                  jai_rgm_refs_all b
2417                           WHERE
2418                                  b.source_document_id   = cp_source_document_id
2419                           AND    b.source_document_type = cp_source_document_type
2420                         );
2421 
2422   ln_exists                        NUMBER(2)                           ;
2423   ln_settlement_id                 JAI_RGM_REFS_ALL.SETTLEMENT_ID%TYPE ;
2424   rec_cur_chk_tcs_settlement       CUR_CHK_TCS_SETTLEMENT%ROWTYPE      ;
2425 BEGIN
2426 
2427   /*########################################################################################################
2428   || VARIABLES INITIALIZATION
2429   ########################################################################################################*/
2430 
2431   lv_member_name        := 'VALIDATE_RECEIPTS';
2432   set_debug_context;
2433   /*commented by csahoo for bug# 6401388
2434   jai_cmn_debug_contexts_pkg.register ( pv_context => lv_context
2435                              , pn_reg_id  => ln_reg_id
2436                              ); --commmented by CSahoo, BUG#5631784
2437 
2438 
2439   /*commented by csahoo for bug# 6401388
2440  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2441                              pv_log_msg  =>  ' PARAMETERS VALUES PASSED TO VALIDATE_RECEIPTS : - '   ||fnd_global.local_chr(10)
2442                                            ||', p_acra.receipt_number  -> '||p_acra.receipt_number   ||fnd_global.local_chr(10)
2443                                            ||', p_acra.cash_receipt_id -> '||p_acra.cash_receipt_id  ||fnd_global.local_chr(10)
2444                                            ||', p_acra.amount          -> '||p_acra.amount           ||fnd_global.local_chr(10)
2445                                            ||', p_acra.type            -> '||p_acra.type             ||fnd_global.local_chr(10)
2446                           );*/
2447 
2448   lv_process_flag       := jai_constants.successful   ;
2449   lv_process_message    := null                       ;
2450 
2451   p_process_flag        := lv_process_flag            ;
2452   p_process_message     := lv_process_message         ;
2453 
2454 
2455   /*########################################################################################################
2456   || CHECK TCS APPLICABILITY PART -2
2457   ########################################################################################################*/
2458    /*commented by csahoo for bug# 6401388
2459  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2460                                pv_log_msg  =>  ' Check TCS applicability '
2461                             );*/
2462   /*
2463   || Check whether the TCS is applicable on the document if no
2464   || do not process
2465   */
2466   OPEN  cur_chk_tcs_applicable ( cp_cash_receipt_id => p_acra.cash_receipt_id );
2467   FETCH cur_chk_tcs_applicable INTO ln_exists;
2468   IF cur_chk_tcs_applicable%NOTFOUND THEN
2469     CLOSE cur_chk_tcs_applicable ;
2470     p_process_flag := jai_constants.not_applicable;
2471    return;
2472   END IF;
2473   CLOSE cur_chk_tcs_applicable ;
2474 
2475   /*########################################################################################################
2476   || VALIDATIONS FOR RECEIPT REVERSAL
2477   ########################################################################################################*/
2478 
2479   IF p_document_type = jai_constants.trx_type_rct_rvs THEN  /* 'RECEIPT_REVERSAL' */
2480     /*
2481     || Check that an original record with cash receipt confirmation exists in the repository.
2482     || In case it does not exist, then error out the record as it need not hit the repository
2483     || If it exists then check whether it has been settled . If yes then throw an error
2484     */
2485    /*commented by csahoo for bug# 6401388
2486  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2487                                pv_log_msg  =>  'Validate for Receipt Reversal '
2488                             );*/
2489     OPEN  cur_chk_tcs_settlement ( cp_source_document_id   =>  p_acra.cash_receipt_id                 ,
2490                                    cp_source_document_type =>  jai_constants.ar_cash_tax_confirmed
2491                                  );
2492     FETCH cur_chk_tcs_settlement INTO rec_cur_chk_tcs_settlement;
2493     IF CUR_CHK_TCS_SETTLEMENT%NOTFOUND THEN
2494       /*
2495       ||Original receipt not found in repository hence throw an error
2496       */
2497       /*commented by csahoo for bug# 6401388
2498  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2499                                   pv_log_msg  =>  'Original Receipt Confirmation records does not exists in the repository. Cannot reverse receipt'
2500                                );*/
2501       CLOSE cur_chk_tcs_settlement;
2502       p_process_flag    := jai_constants.expected_error;
2503       p_process_message := 'Cannot reverse the receipt as the receipt confirmation record does not exists in the repository ';
2504       return;
2505     ELSE
2506       /*
2507       ||Check whether the TCS tax has been settled.
2508       */
2509       IF rec_cur_chk_tcs_settlement.settlement_id IS NOT NULL THEN
2510         /*
2511         || TCS taxes pertaining to the receipt have already been settled
2512         || Cannot allow receipt to be reversed.
2513         */
2514         /*commented by csahoo for bug# 6401388
2515  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2516                                     pv_log_msg  =>  ' TCS On the original confirmed receipt has been settled. CAnnot reverse the receipt'
2517                                  );*/
2518         p_process_flag    := jai_constants.expected_error;
2519         p_process_message := 'Cannot reverse the receipt as the tcs taxes pertaining to the receipt have been confirmed ';
2520         return;
2521       END IF;
2522     END IF;
2523     CLOSE cur_chk_tcs_settlement;
2524   END IF; /* Receipt reversal */
2525 
2526   /*commented by csahoo for bug# 6401388
2527  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2528                              pv_log_msg  =>  '**************** END OF VALIDATE_RECEIPTS ****************'
2529                           );
2530 
2531   jai_cmn_debug_contexts_pkg.deregister (pn_reg_id => ln_reg_id);*/
2532 END validate_receipts;
2533 
2534 PROCEDURE process_invoices   (   p_ract             IN             RA_CUSTOMER_TRX_ALL%ROWTYPE               ,
2535                                  p_document_type                   VARCHAR2                                  ,
2536                                  p_process_flag     OUT NOCOPY     VARCHAR2                                  ,
2537                                  p_process_message  OUT NOCOPY     VARCHAR2
2538                                )
2539   IS
2540 
2541     ln_reg_id           NUMBER;
2542     /*****
2543     ||Get the line invoice_total amount
2544     *****/
2545     CURSOR cur_get_inv_amt_date
2546     IS
2547     SELECT
2548       -- nvl(acctd_amount,0)  total_invoice_amount ,  --deleted by eric for inclusive tax
2549       --NVL(jatl.total_amount,0) total_invoice_amount , --added by eric for inclusive tax on 26-dec,2007
2550       NVL(jatl.line_amount,0) total_invoice_amount , --added by Jia Li for inclusive tax on 2008-01-18
2551       rct.gl_date
2552     FROM
2553       ra_cust_trx_line_gl_dist_all rct
2554     , jai_ar_trx_lines             jatl                     --added by eric for inclusive tax
2555     WHERE   jatl.customer_trx_id  =  rct.customer_trx_id    --added by eric for inclusive tax
2556       AND   rct.customer_trx_id   =  p_ract.customer_trx_id
2557       AND   rct.account_class     =  jai_constants.account_class_rec
2558       AND   rct.latest_rec_flag   =  jai_constants.yes;
2559 
2560     -- Added by Jia Li for inclusive taxes on 2008-01-18
2561     ----------------------------------------------------------------
2562     CURSOR cur_get_inv_exclu_amt
2563     IS
2564       SELECT
2565         SUM(a.func_tax_amount)
2566       FROM
2567         jai_ar_trx_tax_lines a
2568       , jai_cmn_taxes_all b
2569       WHERE  link_to_cust_trx_line_id IN
2570         ( SELECT
2571             customer_trx_line_id
2572           FROM
2573             jai_ar_trx_lines
2574           WHERE customer_trx_id = p_ract.customer_trx_id )
2575         AND a.tax_id = b.tax_id
2576         AND NVL(b.inclusive_tax_flag,'N') = 'N';
2577     ln_total_inv_exclu_amt  jai_ar_trx_tax_lines.func_tax_amount%TYPE;
2578     ------------------------------------------------------------------
2579 
2580     /*****
2581     || Get the invoice line details
2582     *****/
2583     CURSOR cur_get_inv_line_det
2584     IS
2585     SELECT
2586            rctla.customer_trx_line_id           ,
2587            jrct.organization_id                 ,
2588            rctla.extended_amount   line_amount  ,
2589            rctla.inventory_item_id
2590     FROM
2591            JAI_AR_TRXS        jrct ,
2592            ra_customer_trx_lines_all    rctla
2593     WHERE
2594           jrct.customer_trx_id  = rctla.customer_trx_id
2595     AND   jrct.customer_trx_id  = p_ract.customer_trx_id
2596     AND   rctla.customer_trx_id = rctla.customer_trx_id
2597     AND   rctla.line_type       = 'LINE';
2598 
2599     /*****
2600     ||Get the Invoice tax details
2601     ******/
2602     CURSOR cur_get_inv_taxes (cp_customer_trx_line_id JAI_AR_TRX_LINES.CUSTOMER_TRX_LINE_ID%TYPE )
2603     IS
2604     SELECT
2605            jrcttl.customer_trx_line_id ,
2606            jrcttl.tax_id               ,
2607            jrcttl.tax_rate             ,
2608            jtc.tax_type                ,
2609            jrcttl.tax_amount           ,
2610            jrcttl.func_tax_amount
2611     FROM
2612            JAI_AR_TRX_TAX_LINES jrcttl ,
2613            JAI_CMN_TAXES_ALL             jtc    ,
2614            jai_regime_tax_types_v      jrttv
2615     WHERE
2616            jrcttl.link_to_cust_trx_line_id  = cp_customer_trx_line_id
2617     AND    jrcttl.tax_id                    = jtc.tax_id
2618     AND    jrttv.tax_type                   = jtc.tax_type
2619     AND    jrttv.regime_code                = jai_constants.tcs_regime;
2620 
2621     CURSOR cur_get_no_of_rows
2622     IS
2623     SELECT
2624           count(*) no_of_rows
2625     FROM
2626           JAI_AR_TRX_LINES
2627     WHERE
2628          customer_trx_id = p_ract.customer_trx_id ;
2629 
2630     ln_row_count              NUMBER(3)                                  ;
2631     ln_line_counter           NUMBER(3) := 0                             ;
2632     ln_last_line_flag         VARCHAR2(1) := jai_constants.no            ;
2633     lv_process_flag           VARCHAR2(2)                                ;
2634     lv_process_message        VARCHAR2(2000)                             ;
2635     lv_document_type          VARCHAR2(100)                              ;
2636     ln_trx_ref_id             JAI_RGM_REFS_ALL.TRX_REF_ID%TYPE           ;
2637     ln_apportion_factor       NUMBER(3)                                  ;
2638     lv_item_classification    JAI_RGM_REFS_ALL.ITEM_CLASSIFICATION%TYPE  ;
2639     ln_total_inv_amount       RA_CUST_TRX_LINE_GL_DIST_ALL.AMOUNT%TYPE   ;
2640     ld_document_date          JAI_RGM_REFS_ALL.SOURCE_DOCUMENT_DATE%TYPE ;
2641   BEGIN
2642     /*########################################################################################################
2643     || VARIABLES INITIALIZATION
2644     ########################################################################################################*/
2645 
2646     lv_member_name        := 'PROCESS_INVOICES';
2647     set_debug_context;
2648     /*commented by csahoo for bug# 6401388
2649    jai_cmn_debug_contexts_pkg.register ( pv_context => lv_context
2650                                , pn_reg_id  => ln_reg_id
2651                                );*/
2652 
2653     lv_process_flag       := jai_constants.successful   ;
2654     lv_process_message    := null                       ;
2655 
2656     p_process_flag        := lv_process_flag            ;
2657     p_process_message     := lv_process_message         ;
2658     ln_apportion_factor   := 1                          ;
2659 
2660     /******
2661     ||Check that the document is of type
2662     ||Confirmed cash receipt
2663     *******/
2664 
2665     IF p_document_type IN ( jai_constants.trx_type_inv_comp   ,
2666                             jai_constants.trx_type_inv_incomp
2667                           )
2668     THEN            ---------A1
2669       /*########################################################################################################
2670       || DERIVE VALUES AND INSERT COMPLETED INVOICES INTO JAI_RGM_TRX_REFS_ALL TABLE  ---- PART -1
2671       ########################################################################################################*/
2672 
2673       /*
2674       ||Get the receivable amount which is the would be the total invoice amount
2675       */
2676       OPEN  cur_get_inv_amt_date ;
2677       FETCH cur_get_inv_amt_date INTO ln_total_inv_amount,ld_document_date;
2678       CLOSE cur_get_inv_amt_date;
2679 
2680       -- Added by Jia Li for inclusive taxes on 2008-01-18
2681       ---------------------------------------------------------------------
2682       OPEN cur_get_inv_exclu_amt;
2683       FETCH cur_get_inv_exclu_amt INTO ln_total_inv_exclu_amt;
2684       CLOSE cur_get_inv_exclu_amt;
2685 
2686       ln_total_inv_amount := ln_total_inv_amount + ln_total_inv_exclu_amt;
2687       ----------------------------------------------------------------------
2688 
2689       IF p_document_type = jai_constants.trx_type_inv_incomp THEN
2690         /*
2691         ||Reverse the sign of the amount if the invoice is gettin incompleted.
2692         */
2693         ln_total_inv_amount := ln_total_inv_amount * (-1);
2694       END IF;
2695 
2696       /*
2697       || Loop through each line and fetch its details
2698       || At this point of time it needs not be checked that a line has TCS type of taxes as it has been already validated in
2699       || the validate_process procedure
2700       */
2701       FOR rec_cur_get_inv_line_det IN cur_get_inv_line_det
2702       LOOP
2703         /*
2704         ||Get the value for the item classification pertaining to the IO and inventory item combination
2705         */
2706         jai_inv_items_pkg.jai_get_attrib (
2707                                         p_regime_code         => jai_constants.tcs_regime                   ,
2708                                         p_organization_id     => rec_cur_get_inv_line_det.organization_id   ,
2709                                         p_inventory_item_id   => rec_cur_get_inv_line_det.inventory_item_id ,
2710                                         p_attribute_code      => jai_constants.rgm_attr_cd_itm_class        ,
2711                                         p_attribute_value     => lv_item_classification                     ,
2712                                         p_process_flag        => lv_process_flag                            ,
2713                                         p_process_msg         => lv_process_message
2714                                       );
2715 
2716 
2717         IF lv_process_flag = jai_constants.expected_error    OR                      ---------A2
2718            lv_process_flag = jai_constants.unexpected_error
2719         THEN
2720           /*
2721           || As Returned status is an error/not applicable hence:-
2722           || Set out variables p_process_flag and p_process_message accordingly
2723         */
2724           --call to debug package
2725           p_process_flag    := lv_process_flag    ;
2726           p_process_message := lv_process_message ;
2727           return;
2728         END IF;                                                                      ---------A2
2729 
2730         IF p_document_type = jai_constants.trx_type_inv_incomp THEN
2731           /*
2732           ||Reverse the sign of the amount if the invoice is getting incompleted.
2733           */
2734           rec_cur_get_inv_line_det.line_amount  := rec_cur_get_inv_line_det.line_amount * (-1);
2735         END IF;
2736 
2737         /*
2738         ||Get the sequence generated unique key for the transaction
2739         */
2740         OPEN  cur_get_transaction_id ;
2741         FETCH cur_get_transaction_id INTO ln_transaction_id ;
2742         CLOSE cur_get_transaction_id ;
2743 
2744         /*
2745         ||Insert into the repository.
2746         */
2747         insert_repository_references (
2748                                         p_regime_id                   =>   NULL                                                                    ,
2749                                         p_transaction_id              =>   ln_transaction_id                                                       ,
2750                                         p_source_ref_document_id      =>   p_ract.customer_trx_id                                                  ,
2751                                         p_source_ref_document_type    =>   p_document_type                                                         ,
2752                                         p_app_from_document_id        =>   NULL                                                                    ,
2753                                         p_app_from_document_type      =>   NULL                                                                    ,
2754                                         p_app_to_document_id          =>   NULL                                                                    ,
2755                                         p_app_to_document_type        =>   NULL                                                                    ,
2756                                         p_parent_transaction_id       =>   NULL                                                                    ,
2757                                         p_org_tan_no                  =>   NULL                                                                    ,
2758                                         p_document_id                 =>   p_ract.customer_trx_id                                                  ,
2759                                         p_document_type               =>   p_document_type                                                         ,
2760                                         p_document_line_id            =>   rec_cur_get_inv_line_det.customer_trx_line_id                           ,
2761                                         p_document_date               =>   ld_document_date                                                        ,
2762                                         p_table_name                  =>   jai_constants.ar_inv_lines_table                                        ,
2763                                         p_line_amount                 =>   rec_cur_get_inv_line_det.line_amount * nvl( p_ract.exchange_rate , 1 )  ,
2764                                         p_document_amount             =>   ln_total_inv_amount                                                     ,
2765                                         p_org_id                      =>   p_ract.org_id                                                           ,
2766                                         p_organization_id             =>   rec_cur_get_inv_line_det.organization_id                                ,
2767                                         p_party_id                    =>   nvl(p_ract.bill_to_customer_id,p_ract.ship_to_customer_id)              ,
2768                                         p_party_site_id               =>   nvl(p_ract.bill_to_site_use_id,p_ract.ship_to_site_use_id)              ,
2769                                         p_item_classification         =>   lv_item_classification                                                  ,
2770                                         p_trx_ref_id                  =>   ln_trx_ref_id                                                           ,
2771                                         p_process_flag                =>   lv_process_flag                                                         ,
2772                                         p_process_message             =>   lv_process_message
2773                                     );
2774 
2775         IF lv_process_flag = jai_constants.expected_error    OR                      ---------A2
2776            lv_process_flag = jai_constants.unexpected_error
2777         THEN
2778           /*
2779           || As Returned status is an error/not applicable hence:-
2780           || Set out variables p_process_flag and p_process_message accordingly
2781           */
2782           --call to debug package
2783           p_process_flag    := lv_process_flag    ;
2784           p_process_message := lv_process_message ;
2785           return;
2786         END IF;                                                                      ---------A2
2787 
2788         /*########################################################################################################
2789         || INSERT INVOICE TAXES INTO JAI_RGM_TAXES TABLE  ---- PART -2
2790         ########################################################################################################*/
2791 
2792         IF p_document_type = jai_constants.trx_type_inv_incomp THEN
2793           /*
2794           ||Reverse the sign of the amount if the invoice is getting incompleted.
2795           */
2796           ln_apportion_factor  := -1;
2797         END IF;
2798         /*
2799         || Copy the taxes from the invoice/DM transaction to the TCS tax repository
2800         */
2801         copy_taxes_from_source  ( p_source_document_type      =>  p_document_type                                 ,
2802                                   p_source_document_id        =>  p_ract.customer_trx_id                          ,
2803                                   p_source_document_line_id   =>  rec_cur_get_inv_line_det.customer_trx_line_id   ,
2804                                   p_apportion_factor          =>  ln_apportion_factor                             ,
2805                                   p_trx_ref_id                =>  ln_trx_ref_id                                   ,
2806                                   p_process_flag              =>  lv_process_flag                                 ,
2807                                   p_process_message           =>  lv_process_message
2808                                 );
2809 
2810         IF lv_process_flag = jai_constants.expected_error    OR                      ---------A2
2811            lv_process_flag = jai_constants.unexpected_error  OR
2812            lv_process_flag = jai_constants.not_applicable
2813         THEN
2814           /*
2815           || As Returned status is an error/not applicable hence:-
2816           || Set out variables p_process_flag and p_process_message accordingly
2817           */
2818           --call to debug package
2819           p_process_flag    := lv_process_flag    ;
2820           p_process_message := lv_process_message ;
2821           return;
2822         END IF;                                                                      ---------A2
2823 
2824       END LOOP;   /*End loop for invoice lines */
2825 
2826     END IF; /* Invoice completion / Incompletion*/
2827 
2828   /*commented by csahoo for bug# 6401388
2829  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2830                              pv_log_msg  =>  '**************** END OF PROCESS_INVOICES ****************'
2831                           );
2832     jai_cmn_debug_contexts_pkg.deregister (pn_reg_id => ln_reg_id);*/
2833   END process_invoices;
2834 
2835 
2836   procedure  process_receipts        (  p_acra                            AR_CASH_RECEIPTS_ALL%ROWTYPE      ,
2837                                         p_document_type                   VARCHAR2                          ,
2838                                         p_process_flag    OUT NOCOPY      VARCHAR2                          ,
2839                                         p_process_message OUT NOCOPY      VARCHAR2
2840                                      )
2841   IS
2842      ln_reg_id           NUMBER;
2843     /*****
2844     || Get the details of the cash_receipts
2845     *****/
2846     CURSOR cur_get_cr_details ( cp_cash_receipt_id JAI_AR_CASH_RECEIPTS_ALL.CASH_RECEIPT_ID%TYPE )
2847     IS
2848     SELECT
2849             jcra.customer_id          ,
2850             jcra.organization_id      ,
2851             jcra.gl_date              ,
2852             jcra.item_classification
2853     FROM
2854             jai_ar_cash_receipts_all    jcra
2855     WHERE
2856             jcra.cash_receipt_id      = cp_cash_receipt_id ;
2857 
2858     /*****
2859     ||Get the receipt tax details
2860     *****/
2861     CURSOR cur_get_rcpt_taxes
2862     IS
2863     SELECT
2864            jdt.tax_id                         ,
2865            jdt.tax_rate                       ,
2866            jtc.tax_type                       ,
2867            jdt.tax_amt                        ,
2868            jdt.func_tax_amt
2869     FROM
2870            jai_cmn_document_taxes     jdt     ,
2871            JAI_CMN_TAXES_ALL            jtc     ,
2872            jai_regime_tax_types_v     jrttv
2873     WHERE
2874            jdt.tax_id        = jtc.tax_id
2875     AND    jtc.tax_type      = jrttv.tax_type
2876     AND    jdt.source_doc_id = p_acra.cash_receipt_id
2877     AND    jrttv.regime_code = jai_constants.tcs_regime
2878     AND    jdt.source_doc_type  = JAI_CONSTANTS.ar_cash;    --added by eric for a bug
2879 
2880     /*****
2881     || Get all the applications for which Cash Receipt has got TCS type of taxes
2882     || and the corresponding invoice also have got TCS type of tax.
2883     || Unapplications needs not be considered as there applications also would not have gone
2884     *****/
2885     CURSOR cur_get_ar_rec_app_all  (cp_cash_receipt_id AR_RECEIVABLE_APPLICATIONS_ALL.CASH_RECEIPT_ID%TYPE)
2886     IS
2887     SELECT
2888            ra.*
2889     FROM
2890            ar_receivable_applications_all ra    ,
2891            jai_ar_cash_receipts_all   jcra
2892     WHERE
2893            ra.cash_receipt_id   = jcra.cash_receipt_id
2894     AND    ra.cash_receipt_id   = cp_cash_receipt_id
2895     AND    ra.status            = 'APP'
2896     AND    ra.application_type  = 'CASH'
2897     AND    ra.display           = jai_constants.yes
2898     AND    jcra.confirm_flag    = jai_constants.yes
2899     AND    exists  ( SELECT     /* TCS type of taxes exist for the receipt */
2900                             1
2901                      FROM
2902                             jai_cmn_document_taxes   jdt  ,
2903                             jai_regime_tax_types_v   jrttv
2904                      WHERE
2905                             jdt.source_doc_id     = jcra.cash_receipt_id
2906                      AND    jdt.source_table_name = jai_constants.jai_cash_rcpts /* 'JAI_AR_CASH_RECEIPTS_ALL' */
2907                      AND    jdt.tax_type          = jrttv.tax_type
2908                      AND    jrttv.regime_code     = jai_constants.tcs_regime
2909                      AND    jdt.source_doc_type  = JAI_CONSTANTS.ar_cash     --added by eric for a bug
2910                   )
2911     AND    exists ( SELECT    /* TCS type of taxes exist for the corresponding Invoice */
2912                             1
2913                     FROM
2914                             JAI_AR_TRX_LINES     jrctl ,
2915                             JAI_AR_TRX_TAX_LINES     jrcttl,
2916                             JAI_CMN_TAXES_ALL                 jtc   ,
2917                             jai_regime_tax_types_v          jrttv
2918                     WHERE
2919                             jrctl.customer_trx_id       = ra.applied_customer_trx_id
2920                     AND     jrctl.customer_trx_line_id  = jrcttl.link_to_cust_trx_line_id
2921                     AND     jrcttl.tax_id               = jtc.tax_id
2922                     AND     jtc.tax_type                = jrttv.tax_type
2923                     AND     jrttv.regime_code           = jai_constants.tcs_regime
2924                   );
2925 
2926     /********
2927     || Get the details of the source receipt
2928     || from the repository
2929     ********/
2930     CURSOR cur_copy_src_rcpt (cp_source_document_id JAI_RGM_REFS_ALL.SOURCE_DOCUMENT_ID%TYPE)
2931     IS
2932     SELECT
2933             *
2934     FROM
2935             jai_rgm_refs_all
2936     WHERE
2937             source_document_id = cp_source_document_id;
2938 
2939     /********
2940     || Get the details of the source receipt taxes
2941     || from the repository
2942     ********/
2943     CURSOR cur_copy_tax_rcpt_rev ( cp_trx_ref_id JAI_RGM_REFS_ALL.TRX_REF_ID%TYPE )
2944     IS
2945     SELECT
2946           *
2947     FROM
2948           jai_rgm_taxes
2949     WHERE
2950           trx_ref_id = cp_trx_ref_id;
2951 
2952     p_araa                    AR_RECEIVABLE_APPLICATIONS_ALL%ROWTYPE      ;
2953     rec_cur_get_cr_details    CUR_GET_CR_DETAILS%ROWTYPE                  ;
2954     rec_cur_copy_src_rcpt     CUR_COPY_SRC_RCPT%ROWTYPE                   ;
2955     ln_rcpt_amount            JAI_RGM_REFS_ALL.SOURCE_DOCUMENT_AMT%TYPE   ;
2956     lv_process_flag           VARCHAR2(2)                                 ;
2957     lv_process_message        VARCHAR2(2000)                              ;
2958     lv_document_type          VARCHAR2(100)                               ;
2959     ln_trx_ref_id             JAI_RGM_REFS_ALL.TRX_REF_ID%TYPE            ;
2960     ln_apportion_factor       NUMBER(3)                                   ;
2961     ln_local_transaction_id   JAI_RGM_REFS_ALL.TRANSACTION_ID%TYPE        ;
2962     ln_parent_transaction_id  JAI_RGM_REFS_ALL.PARENT_TRANSACTION_ID%TYPE ;
2963 
2964   BEGIN
2965 
2966     /*########################################################################################################
2967     || VARIABLES INITIALIZATION
2968     ########################################################################################################*/
2969     lv_member_name        := 'PROCESS_RECEIPTS';
2970     set_debug_context;
2971     /*commented by csahoo for bug# 6401388
2972     jai_cmn_debug_contexts_pkg.register ( pv_context => lv_context
2973                                , pn_reg_id  => ln_reg_id
2974                                );*/
2975 
2976     lv_process_flag       := jai_constants.successful                      ;
2977     lv_process_message    := null                                          ;
2978 
2979     p_process_flag        := lv_process_flag                               ;
2980     p_process_message     := lv_process_message                            ;
2981 
2982     ln_rcpt_amount        := p_acra.amount * nvl(p_acra.exchange_rate ,1 ) ;
2983     ln_apportion_factor   := 1                                             ;
2984 
2985     /******
2986     ||Check that the document is of type
2987     ||Confirmed cash receipt
2988 
2989     *******/
2990 
2991     IF p_document_type = jai_constants.ar_cash_tax_confirmed  THEN            ---------C1
2992       /*commented by csahoo for bug# 6401388
2993  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
2994                                  pv_log_msg  =>  ' Confirmation processing for the receipts with the following details'||fnd_global.local_chr(10)
2995                                               ||', p_acra.receipt_number ->  '||p_acra.receipt_number   ||fnd_global.local_chr(10)
2996                                               ||', p_acra.cash_receipt_id -> '||p_acra.cash_receipt_id  ||fnd_global.local_chr(10)
2997                                               ||', p_acra.amount          -> '||p_acra.amount          ||fnd_global.local_chr(10)
2998                                               ||', p_acra.type            -> '||p_acra.type
2999                              );*/
3000       OPEN  cur_get_cr_details ( cp_cash_receipt_id => p_acra.cash_receipt_id );
3001       FETCH cur_get_cr_details INTO rec_cur_get_cr_details;
3002       CLOSE cur_get_cr_details;
3003 
3004       /*########################################################################################################
3005       || INSERT CASH RECEIPTS INTO JAI_RGM_TRX_REFS_ALL TABLE  ---- PART -1
3006       ########################################################################################################*/
3007 
3008       /*
3009       ||Get the sequence generated unique key for the transaction
3010       */
3011       OPEN  cur_get_transaction_id ;
3012       FETCH cur_get_transaction_id INTO ln_transaction_id ;
3013       CLOSE cur_get_transaction_id ;
3014 
3015       ln_local_transaction_id := ln_transaction_id;
3016 
3017      /*commented by csahoo for bug# 6401388
3018  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3019                                           pv_log_msg  =>  ' Before call to insert_repository_references '
3020                                       );*/
3021 
3022       insert_repository_references (
3023                                       p_regime_id                 =>   NULL                                           ,
3024                                       p_transaction_id            =>   ln_transaction_id                              ,
3025                                       p_source_ref_document_id    =>   p_acra.cash_receipt_id                         ,
3026                                       p_source_ref_document_type  =>   p_document_type                                ,
3027                                       p_app_from_document_id      =>   NULL                                           ,
3028                                       p_app_from_document_type    =>   NULL                                           ,
3029                                       p_app_to_document_id        =>   NULL                                           ,
3030                                       p_app_to_document_type      =>   NULL                                           ,
3031                                       p_parent_transaction_id     =>   NULL                                           ,
3032                                       p_org_tan_no                =>   NULL                                           ,
3033                                       p_document_id               =>   p_acra.cash_receipt_id                         ,
3034                                       p_document_type             =>   p_document_type                                ,
3035                                       p_document_line_id          =>   p_acra.cash_receipt_id                         ,
3036                                       p_document_date             =>   rec_cur_get_cr_details.gl_date                 ,
3037                                       p_table_name                =>   jai_constants.jai_cash_rcpts                   ,
3038                                       p_line_amount               =>   ln_rcpt_amount                                 ,
3039                                       p_document_amount           =>   ln_rcpt_amount                                 ,
3040                                       p_org_id                    =>   p_acra.org_id                                  ,
3041                                       p_organization_id           =>   rec_cur_get_cr_details.organization_id         ,
3042                                       p_party_id                  =>   rec_cur_get_cr_details.customer_id             ,
3043                                       p_party_site_id             =>   p_acra.customer_site_use_id                    ,
3044                                       p_item_classification       =>   rec_cur_get_cr_details.item_classification     ,
3045                                       p_trx_ref_id                =>   ln_trx_ref_id                                  ,
3046                                       p_process_flag              =>   lv_process_flag                                ,
3047                                       p_process_message           =>   lv_process_message
3048                                   );
3049 
3050      /*commented by csahoo for bug# 6401388
3051  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3052                                  pv_log_msg  =>  ' Return from insert_repository_references '
3053                            );*/
3054 
3055       IF lv_process_flag = jai_constants.expected_error    OR                      ---------A2
3056          lv_process_flag = jai_constants.unexpected_error  OR
3057          lv_process_flag = jai_constants.not_applicable
3058       THEN
3059         /*
3060         || As Returned status is an error/not applicable hence:-
3061         || Set out variables p_process_flag and p_process_message accordingly
3062         */
3063         --call to debug package
3064          /*commented by csahoo for bug# 6401388
3065  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3066                                     pv_log_msg  =>  ' Error : - lv_process_flag -> '||lv_process_flag||' - '||lv_process_message
3067                                  );*/
3068 
3069         p_process_flag    := lv_process_flag    ;
3070         p_process_message := lv_process_message ;
3071         return;
3072       END IF;                                                                      ---------A2
3073 
3074 
3075       /*########################################################################################################
3076       || INSERT CASH RECEIPTS INTO JAI_RGM_TAXES TABLE  ---- PART -2
3077       ########################################################################################################*/
3078 
3079       /*
3080       || Copy the taxes from the invoice/DM transaction to the TCS tax repository
3081       */
3082       /*commented by csahoo for bug# 6401388
3083  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3084                                  pv_log_msg  =>  ' Before call to copy_taxes_from_source '
3085                               );*/
3086 
3087       copy_taxes_from_source  ( p_source_document_type      =>  p_document_type         ,
3088                                 p_source_document_id        =>  p_acra.cash_receipt_id  ,
3089                                 p_apportion_factor          =>  ln_apportion_factor     ,
3090                                 p_trx_ref_id                =>  ln_trx_ref_id           ,
3091                                 p_process_flag              =>  lv_process_flag         ,
3092                                 p_process_message           =>  lv_process_message
3093                               );
3094       /*commented by csahoo for bug# 6401388
3095  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3096                                           pv_log_msg  =>  ' Return from copy_taxes_from_source '
3097                                        );*/
3098       IF lv_process_flag = jai_constants.expected_error    OR                      ---------A2
3099          lv_process_flag = jai_constants.unexpected_error  OR
3100          lv_process_flag = jai_constants.not_applicable
3101       THEN
3102         /*
3103         || As Returned status is an error/not applicable hence:-
3104         || Set out variables p_process_flag and p_process_message accordingly
3105         */
3106         --call to debug package
3107         /*commented by csahoo for bug# 6401388
3108  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3109                                    pv_log_msg  =>  ' Error : - lv_process_flag -> '||lv_process_flag||' - '||lv_process_message
3110                                 );*/
3111 
3112         p_process_flag    := lv_process_flag    ;
3113         p_process_message := lv_process_message ;
3114         return;
3115       END IF;
3116 
3117       /*########################################################################################################
3118       || PROCESS AR CASH RECEIPT APPLICATIONS/UNAPPLICATIONS ---- PART -3
3119       ########################################################################################################*/
3120       /*commented by csahoo for bug# 6401388
3121  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3122                                  pv_log_msg  =>  ' Cash receipt application processing '
3123                               );*/
3124       /******
3125       ||Fetch all the latest applications related to the cash receipt
3126       *******/
3127       FOR rec_get_ar_rec_app_all IN cur_get_ar_rec_app_all ( cp_cash_receipt_id => p_acra.cash_receipt_id )
3128       LOOP
3129          p_araa := rec_get_ar_rec_app_all ;
3130         /*********
3131         || Consider only the latest applications
3132         **********/
3133 
3134         /*******************************************
3135         || Call the procedure to process
3136         || cash receipt applications/unapplications
3137         *******************************************/
3138        /*commented by csahoo for bug# 6401388
3139  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3140                                             pv_log_msg  =>  ' before call to jai_ar_tcs_rep_pkg.process_transactions '
3141                                           );*/
3142         jai_ar_tcs_rep_pkg.process_transactions (  p_araa                =>  p_araa                   ,
3143                                                    p_event               =>  p_araa.application_type  ,
3144                                                    -- p_called_from         =>  lv_document_type      ,
3145                                                    p_process_flag        =>  lv_process_flag          ,
3146                                                    p_process_message     =>  lv_process_message
3147                                                 );
3148         /*commented by csahoo for bug# 6401388
3149  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3150                                    pv_log_msg  =>  ' Return from jai_ar_tcs_rep_pkg.process_transactions '
3151                                 );*/
3152         IF lv_process_flag = jai_constants.expected_error    OR                      ---------A2
3153            lv_process_flag = jai_constants.unexpected_error  OR
3154            lv_process_flag = jai_constants.not_applicable
3155         THEN
3156           /*
3157           || As Returned status is an error/not applicable hence:-
3158           || Set out variables p_process_flag and p_process_message accordingly
3159           */
3160           --call to debug package
3161           /*commented by csahoo for bug# 6401388
3162  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3163                                      pv_log_msg  =>  ' Error : - lv_process_flag -> '||lv_process_flag||' - '||lv_process_message
3164                                   );*/
3165 
3166           p_process_flag    := lv_process_flag    ;
3167           p_process_message := lv_process_message ;
3168           return;
3169         END IF;                                                                      ---------A2
3170 
3171 
3172       END LOOP;
3173 
3174       /*
3175       ||Reset the receipt transaction ln_transaction_id from ln_local_transaction_id
3176       */
3177       ln_transaction_id := ln_local_transaction_id ;
3178 
3179       /*########################################################################################################
3180       || PROCESS AR CASH RECEIPT REVERSALS ---- PART -4
3181       ########################################################################################################*/
3182 
3183     ELSIF p_document_type = jai_constants.trx_type_rct_rvs  THEN            ---------C1
3184      /*commented by csahoo for bug# 6401388
3185  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3186                                 pv_log_msg  =>  ' Start of receipt reversal processing '
3187                              );*/
3188       OPEN cur_get_parent_transaction ( cp_source_document_id    => p_acra.cash_receipt_id                ,
3189                                         cp_source_document_type  => jai_constants.ar_cash_tax_confirmed
3190                                       ) ;
3191 
3192       FETCH cur_get_parent_transaction INTO ln_parent_transaction_id;
3193       /*
3194       || Check that the source receipt confirmation record has been found in the TCS repository.
3195       || If not found then receipt reversal also need not hit the repository.
3196       */
3197       IF CUR_GET_PARENT_TRANSACTION%FOUND THEN
3198         /*commented by csahoo for bug# 6401388
3199  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3200                                             pv_log_msg  =>  ' Parent receipt confirmation record found parent_transaction_id -> '||ln_parent_transaction_id
3201                                          );*/
3202 
3203         copy_references (  p_parent_transaction_id   => ln_parent_transaction_id   ,
3204                            p_new_document_id         => p_acra.cash_receipt_id     ,
3205                            p_new_document_type       => p_document_type            ,
3206                            p_new_document_date       => p_acra.reversal_date       ,
3207                            p_apportion_factor        => -1                         ,/* As reversal cannot be partial  */
3208                            p_process_flag            => lv_process_flag            ,
3209                            p_process_message         => lv_process_message
3210                         );
3211         IF lv_process_flag = jai_constants.expected_error    OR                      ---------A2
3212            lv_process_flag = jai_constants.unexpected_error  OR
3213            lv_process_flag = jai_constants.not_applicable
3214         THEN
3215           /*
3216           || As Returned status is an error/not applicable hence:-
3217           || Set out variables p_process_flag and p_process_message accordingly
3218           */
3219           CLOSE cur_get_parent_transaction ;
3220           /*commented by csahoo for bug# 6401388
3221  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3222                                               pv_log_msg  =>  ' Error : - lv_process_flag -> '||lv_process_flag||' - '||lv_process_message
3223                                            );*/
3224 
3225           p_process_flag    := lv_process_flag    ;
3226           p_process_message := lv_process_message ;
3227           return;
3228         END IF;                                                                      ---------A2
3229       END IF;
3230       CLOSE cur_get_parent_transaction ;
3231     END IF;
3232    /*commented by csahoo for bug# 6401388
3233  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3234                                pv_log_msg  =>  '**************** END OF PROCESS_RECEIPTS ****************'
3235                             );
3236 
3237     jai_cmn_debug_contexts_pkg.deregister (pn_reg_id => ln_reg_id);*/
3238   END process_receipts;
3239 
3240 
3241 PROCEDURE process_applications  (   p_araa                            IN              AR_RECEIVABLE_APPLICATIONS_ALL%ROWTYPE      ,
3242                                     p_document_type                   IN              VARCHAR2                                    ,
3243                                     p_item_classification             IN              JAI_RGM_REFS_ALL.ITEM_CLASSIFICATION%TYPE   ,
3244                                     p_process_flag                    OUT NOCOPY      VARCHAR2                                    ,
3245                                     p_process_message                 OUT NOCOPY      VARCHAR2
3246                                   )
3247   IS
3248      ln_reg_id           NUMBER;
3249       /*
3250     ||Get the application and applied to document details
3251     */
3252     CURSOR cur_get_app_to_det (cp_applied_customer_trx_id  AR_RECEIVABLE_APPLICATIONS_ALL.APPLIED_CUSTOMER_TRX_ID%TYPE )
3253     IS
3254     SELECT
3255             trx_types.type                                app_to_doc_type                ,
3256             --nvl(gl_dist.acctd_amount,0)                 app_to_doc_amt                 ,--Deleted by eric for inclusive tax
3257             --NVL(jrctl.total_amount,0)                     app_to_doc_amt                 ,--Added by eric for inclusive tax for 26-Dec,2007
3258             NVL(jrctl.line_amount,0)                      app_to_doc_amt                 , --Added by Jia Li for inclusive tax for 2008-01-18
3259             rcta.org_id                                   app_to_org_id                  ,
3260             jrct.organization_id                          app_to_organization_id         ,
3261 			-- code ported from BL12.1.3 by zhiwei.xin on 15-MAR-2013 begin
3262 			--Modify by Zhiwei for bug#14040855 begin
3263             --nvl(bill_to_customer_id, ship_to_customer_id) app_to_customer_id             , -- Bug 6132484
3264             --nvl(bill_to_site_use_id,ship_to_site_use_id)  app_to_customer_site_use_id      -- Bug 6132484
3265 			nvl(rcta.bill_to_customer_id, rcta.ship_to_customer_id) app_to_customer_id             , -- Bug 6132484
3266             nvl(rcta.bill_to_site_use_id, rcta.ship_to_site_use_id) app_to_customer_site_use_id      -- Bug 6132484
3267 			--Modify by Zhiwei for bug#14040855 end
3268 			-- code ported from BL12.1.3 by zhiwei.xin on 15-MAR-2013 end
3269     FROM
3270             ra_customer_trx_all                 rcta                                    ,
3271             ra_cust_trx_types_all               trx_types                               ,
3272             ra_cust_trx_line_gl_dist_all        gl_dist                                 ,
3273             JAI_AR_TRXS               jrct                                    ,
3274             JAI_AR_TRX_LINES         jrctl                                   ,
3275             JAI_AR_TRX_TAX_LINES         jrcttl                                  ,
3276             JAI_CMN_TAXES_ALL                     jtc                                     ,
3277             jai_regime_tax_types_v              jrttv
3278     WHERE
3279             rcta.customer_trx_id            =   cp_applied_customer_trx_id
3280     AND     rcta.complete_flag              =   jai_constants.yes
3281     AND     rcta.customer_trx_id            =   gl_dist.customer_trx_id
3282     AND     gl_dist.account_class           =   jai_constants.account_class_rec
3283     AND     gl_dist.latest_rec_flag         =   jai_constants.yes
3284     AND     trx_types.cust_trx_type_id      =   rcta.cust_trx_type_id
3285     AND     rcta.customer_trx_id            =   jrct.customer_trx_id
3286     AND     jrct.customer_trx_id            =   jrctl.customer_trx_id
3287     AND     jrctl.customer_trx_line_id      =   jrcttl.link_to_cust_trx_line_id
3288     AND     jrcttl.tax_id                   =   jtc.tax_id
3289     AND     jtc.tax_type                    =   jrttv.tax_type
3290     AND     jrttv.regime_code               =   jai_constants.tcs_regime
3291     AND     trx_types.type                 IN  (  jai_constants.ar_invoice_type_inv      ,  /* Applied to doc has to be either a invoice or DM or CM */
3292                                                   jai_constants.ar_invoice_type_cm       ,
3293                                                   jai_constants.ar_doc_type_dm
3294                                                 );
3295 
3296     -- Added by Jia Li for inclusive taxes on 2008-01-18
3297     ----------------------------------------------------------------
3298     CURSOR cur_get_exclu_amt(cp_customer_trx_id RA_CUST_TRX_LINE_GL_DIST_ALL.CUSTOMER_TRX_ID%TYPE )
3299     IS
3300       SELECT
3301         SUM(a.func_tax_amount)
3302       FROM
3303         jai_ar_trx_tax_lines a
3304       , jai_cmn_taxes_all b
3305       WHERE  link_to_cust_trx_line_id IN
3306         ( SELECT
3307             customer_trx_line_id
3308           FROM
3309             jai_ar_trx_lines
3310           WHERE customer_trx_id = cp_customer_trx_id )
3311         AND a.tax_id = b.tax_id
3312         AND NVL(b.inclusive_tax_flag,'N') = 'N';
3313     ln_total_exclu_amt  jai_ar_trx_tax_lines.func_tax_amount%TYPE;
3314     ------------------------------------------------------------------
3315 
3316     /*
3317     ||Get the type of the document
3318     */
3319     CURSOR cur_get_doc_type ( cp_customer_trx_id  RA_CUSTOMER_TRX_ALL.CUSTOMER_TRX_ID%TYPE )
3320     IS
3321     SELECT
3322            trx_types.type
3323     FROM
3324           ra_customer_trx_all    ract,
3325           ra_cust_trx_types_all  trx_types
3326     WHERE
3327           ract.cust_trx_type_id  =  trx_types.cust_trx_type_id
3328     AND   ract.customer_trx_id   =  cp_customer_trx_id;
3329 
3330     /*
3331     ||Get the applied to document cash receipt details
3332     */
3333     CURSOR cur_get_cr_details ( cp_cash_receipt_id AR_CASH_RECEIPTS_ALL.CASH_RECEIPT_ID%TYPE )
3334     IS
3335     SELECT
3336            nvl(acra.amount,0) * nvl(acra.exchange_rate , 1 )   app_fr_doc_amt           ,
3337            nvl(acra.exchange_rate,1)                           exchange_rate            ,
3338            arps.gl_date                                        app_fr_doc_date          ,
3339            jcra.organization_id                                app_fr_organization_id   ,
3340            acra.pay_from_customer                                                       ,
3341            acra.customer_site_use_id                                                    ,
3342            acra.org_id
3343     FROM
3344            ar_cash_receipts_all     acra ,
3345            ar_payment_schedules_all arps ,
3346            jai_ar_cash_receipts_all jcra ,
3347            jai_cmn_document_taxes   jdt  ,
3348            jai_regime_tax_types_v   jrttv
3349     WHERE
3350            acra.cash_receipt_id = arps.cash_receipt_id
3351     AND    acra.cash_receipt_id = jcra.cash_receipt_id
3352     AND    jcra.cash_receipt_id = jdt.source_doc_id
3353     AND    jdt.tax_type         = jrttv.tax_type
3354     AND    acra.cash_receipt_id = cp_cash_receipt_id
3355     AND    jrttv.regime_code    = jai_constants.tcs_regime
3356     AND    jcra.confirm_flag    = jai_constants.yes
3357     AND    jdt.source_doc_type  = JAI_CONSTANTS.ar_cash;    --added by eric for a bug
3358     /*
3359     ||Get the applied to document CM details
3360     */
3361     CURSOR cur_get_cm_details (cp_customer_trx_id RA_CUST_TRX_LINE_GL_DIST_ALL.CUSTOMER_TRX_ID%TYPE)
3362     IS
3363     SELECT
3364           --nvl(gl_dist.acctd_amount,0)                       app_fr_doc_amt                 ,--Deleted by eric for inclusive tax
3365           --NVL(jrctl.total_amount,0)                         app_fr_doc_amt                   ,--Added by eric for Inclusisve Tax on 26-dec,2007
3366           NVL(jrctl.line_amount,0)                          app_fr_doc_amt                 ,--Added by Jia Li for Inclusisve Tax on 2008-01-18
3367           nvl(rcta.exchange_rate,1)                         exchange_rate                  ,
3368           gl_dist.gl_date                                   app_fr_doc_date                ,
3369           rcta.org_id                                       app_to_org_id                  ,
3370           jrct.organization_id                              app_to_organization_id         ,
3371 		  -- code ported from BL12.1.3 by zhiwei.xin on 15-MAR-2013 begin
3372           --nvl( bill_to_customer_id , ship_to_customer_id )  app_to_customer_id             ,
3373           --nvl( bill_to_site_use_id , ship_to_site_use_id )  app_to_customer_site_use_id
3374 		  nvl( bill_to_customer_id , rcta.ship_to_customer_id )  app_to_customer_id             ,
3375           nvl( bill_to_site_use_id , rcta.ship_to_site_use_id )  app_to_customer_site_use_id
3376 		  -- code ported from BL12.1.3 by zhiwei.xin on 15-MAR-2013 end.
3377     FROM
3378           ra_customer_trx_all                 rcta                                      ,
3379           ra_cust_trx_types_all               trx_types                                 ,
3380           ra_cust_trx_line_gl_dist_all        gl_dist                                   ,
3381           JAI_AR_TRXS               jrct                                      ,
3382           JAI_AR_TRX_LINES         jrctl                                     ,
3383           JAI_AR_TRX_TAX_LINES         jrcttl                                    ,
3384           JAI_CMN_TAXES_ALL                     jtc                                       ,
3385           jai_regime_tax_types_v              jrttv
3386     WHERE
3387           rcta.complete_flag              =   jai_constants.yes
3388     AND   trx_types.cust_trx_type_id      =   rcta.cust_trx_type_id
3389     AND   trx_types.type                  =   jai_constants.ar_invoice_type_cm
3390     AND   rcta.customer_trx_id            =   gl_dist.customer_trx_id
3391     AND   gl_dist.account_class           =   jai_constants.account_class_rec
3392     AND   gl_dist.latest_rec_flag         =   jai_constants.yes
3393     AND   rcta.customer_trx_id            =   jrct.customer_trx_id
3394     AND   jrct.customer_trx_id            =   jrctl.customer_trx_id
3395     AND   jrctl.customer_trx_line_id      =   jrcttl.link_to_cust_trx_line_id
3396     AND   jrcttl.tax_id                   =   jtc.tax_id
3397     AND   jtc.tax_type                    =   jrttv.tax_type
3398     AND   rcta.customer_trx_id            =   cp_customer_trx_id
3399     AND   jrttv.regime_code               =   jai_constants.tcs_regime;
3400 
3401     ln_app_fr_doc_amt             NUMBER                                            ;
3402     ld_app_fr_doc_date            DATE                                              ;
3403     ln_app_fr_doc_id              JAI_RGM_REFS_ALL.SOURCE_DOCUMENT_ID%TYPE          ;
3404     ln_app_fr_doc_type            JAI_RGM_REFS_ALL.SOURCE_DOCUMENT_TYPE%TYPE        ;
3405     ln_app_fr_doc_table           JAI_RGM_REFS_ALL.SOURCE_TABLE_NAME%TYPE           ;
3406     ln_fr_organization_id         JAI_RGM_REFS_ALL.ORGANIZATION_ID%TYPE             ;
3407     ln_fr_party_id                JAI_RGM_REFS_ALL.PARTY_ID%TYPE                    ;
3408     ln_fr_party_site_id           JAI_RGM_REFS_ALL.PARTY_SITE_ID%TYPE               ;
3409 
3410     ln_app_to_doc_amt             NUMBER                                            ;
3411     ld_app_to_doc_date            DATE                                              ;
3412     ln_app_to_doc_id              JAI_RGM_REFS_ALL.SOURCE_DOCUMENT_ID%TYPE          ;
3413     ln_app_to_doc_type            JAI_RGM_REFS_ALL.SOURCE_DOCUMENT_TYPE%TYPE        ;
3414     ln_app_to_doc_table           JAI_RGM_REFS_ALL.SOURCE_TABLE_NAME%TYPE           ;
3415     ln_to_organization_id         JAI_RGM_REFS_ALL.ORGANIZATION_ID%TYPE             ;
3416     ln_to_party_id                JAI_RGM_REFS_ALL.PARTY_ID%TYPE                    ;
3417     ln_to_party_site_id           JAI_RGM_REFS_ALL.PARTY_SITE_ID%TYPE               ;
3418 
3419 
3420     ln_app_amount                 NUMBER                                            ;
3421     ln_apportion_factor           NUMBER                                            ;
3422     ln_app_ref_doc_id             JAI_RGM_REFS_ALL.SOURCE_REF_DOCUMENT_ID%TYPE      ;
3423     ln_app_ref_doc_type           JAI_RGM_REFS_ALL.SOURCE_REF_DOCUMENT_TYPE%TYPE    ;
3424     ln_app_ref_doc_table          JAI_RGM_REFS_ALL.SOURCE_TABLE_NAME%TYPE           ;
3425     ld_source_document_date       JAI_RGM_REFS_ALL.SOURCE_DOCUMENT_DATE%TYPE        ;
3426     ln_parent_transaction_id      JAI_RGM_REFS_ALL.parent_transaction_id%TYPE       ;
3427     ln_organization_id            JAI_RGM_REFS_ALL.ORGANIZATION_ID%TYPE             ;
3428     ln_trx_ref_id                 JAI_RGM_REFS_ALL.TRX_REF_ID%TYPE                  ;
3429     ln_party_id                   JAI_RGM_REFS_ALL.PARTY_ID%TYPE                    ;
3430     ln_party_site_id              JAI_RGM_REFS_ALL.PARTY_SITE_ID%TYPE               ;
3431     ln_exchange_rate              NUMBER                                            ;
3432     rec_cur_get_app_to_det        CUR_GET_APP_TO_DET%ROWTYPE                        ;
3433     rec_cur_get_cr_details        CUR_GET_CR_DETAILS%ROWTYPE                        ;
3434     rec_cur_get_cm_details        CUR_GET_CM_DETAILS%ROWTYPE                        ;
3435     lv_process_flag               VARCHAR2(2)                                       ;
3436     lv_process_message            VARCHAR2(4000)                                    ;
3437 
3438   BEGIN
3439     /*########################################################################################################
3440     || Initialize Variables ---- PART -1
3441     ########################################################################################################*/
3442     lv_member_name        := 'PROCESS_APPLICATIONS';
3443     set_debug_context;
3444     /*commented by csahoo for bug# 6401388
3445     jai_cmn_debug_contexts_pkg.register ( pv_context => lv_context ,
3446                                           pn_reg_id  => ln_reg_id
3447                                         );*/
3448 
3449    /*commented by csahoo for bug# 6401388
3450  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3451                                       pv_log_msg  =>  ' Processing the APPLICATION record ' ||fnd_global.local_chr(10)
3452                                            ||', receivable_application_id -> '  ||p_araa.receivable_application_id    ||fnd_global.local_chr(10)
3453                                            ||', application_type          -> '  ||p_araa.application_type             ||fnd_global.local_chr(10)
3454                                            ||', status                    -> '  ||p_araa.status                       ||fnd_global.local_chr(10)
3455                                            ||', display                   -> '  ||p_araa.display                     ||fnd_global.local_chr(10)
3456                                            ||', cash_receipt_id           -> '  ||p_araa.cash_receipt_id              ||fnd_global.local_chr(10)
3457                                            ||', customer_trx_id           -> '  ||p_araa.customer_trx_id              ||fnd_global.local_chr(10)
3458                                            ||', applied_customer_trx_id   -> '  ||p_araa.applied_customer_trx_id      ||fnd_global.local_chr(10)
3459                                            ||', amount_applied            -> '  ||p_araa.amount_applied               ||fnd_global.local_chr(10)
3460                                            ||', gl_date                   -> '  ||p_araa.gl_date                      ||fnd_global.local_chr(10)
3461                                            ||', org_id                    -> '  ||p_araa.org_id
3462                                     );*/
3463     lv_process_flag               := jai_constants.successful                       ;
3464     lv_process_message            := null                                           ;
3465     p_process_flag                := lv_process_flag                                ;
3466     p_process_message             := lv_process_message                             ;
3467     ln_app_fr_doc_amt             := NULL                                           ;
3468     ld_app_fr_doc_date            := NULL                                           ;
3469     ln_parent_transaction_id      := NULL                                           ;
3470 
3471     /*########################################################################################################
3472     || GET APPLICATION and APPLIED TO DOCUMENT DETAILS ---- PART -2
3473     ########################################################################################################*/
3474     OPEN  cur_get_app_to_det (cp_applied_customer_trx_id => p_araa.applied_customer_trx_id ) ;
3475     FETCH cur_get_app_to_det INTO rec_cur_get_app_to_det                                     ;
3476     CLOSE cur_get_app_to_det                                                                 ;
3477 
3478     -- Added by Jia Li for inclusive taxes on 2008-01-18
3479     ---------------------------------------------------------------------
3480     OPEN cur_get_exclu_amt (cp_customer_trx_id => p_araa.applied_customer_trx_id );
3481     FETCH cur_get_exclu_amt INTO ln_total_exclu_amt;
3482     CLOSE cur_get_exclu_amt;
3483     ----------------------------------------------------------------------
3484 
3485     ln_app_to_doc_amt         := rec_cur_get_app_to_det.app_to_doc_amt + ln_total_exclu_amt  ; -- Modified by Jia Li for inclusive tax on 2008-01-18
3486     ld_app_to_doc_date        := p_araa.gl_date                                              ;
3487     ln_app_to_doc_id          := p_araa.applied_customer_trx_id                              ;
3488 
3489     /*
3490     || Derive the to Document type from the application information
3491     || APPLIED TO DOCUMENT BEING CASH NOT HANDLED CURRENTLY  - STOP AND VALIDATION TIME
3492     */
3493     OPEN  cur_get_doc_type ( cp_customer_trx_id => p_araa.applied_customer_trx_id )          ;
3494     FETCH cur_get_doc_type INTO ln_app_to_doc_type                                           ;
3495     CLOSE cur_get_doc_type                                                                   ;
3496 
3497     ln_app_to_doc_table       := jai_constants.ar_inv_lines_table                            ; /* table JAI_AR_TRX_LINES */
3498     ln_app_to_doc_type        := rec_cur_get_app_to_det.app_to_doc_type                      ;
3499     ln_to_organization_id     := rec_cur_get_app_to_det.app_to_organization_id               ;
3500     ln_to_party_id            := rec_cur_get_app_to_det.app_to_customer_id                   ;
3501     ln_to_party_site_id       := rec_cur_get_app_to_det.app_to_customer_site_use_id          ;
3502 
3503     /*########################################################################################################
3504     || VALIDATE AND GET APPLIED FROM DOCUMENT DETAILS ---- PART -3
3505     ########################################################################################################*/
3506     IF p_document_type = jai_constants.trx_type_rct_app   THEN                                                  -----------A1
3507       /*
3508       ||Applied from document type is Cash Receipt Application
3509       */
3510       OPEN  cur_get_cr_details ( cp_cash_receipt_id => p_araa.cash_receipt_id  )    ;
3511       FETCH cur_get_cr_details INTO rec_cur_get_cr_details                          ;
3512       CLOSE cur_get_cr_details                                                      ;
3513 
3514       ld_app_fr_doc_date      :=  rec_cur_get_cr_details.app_fr_doc_date            ;
3515       ln_app_fr_doc_amt       :=  rec_cur_get_cr_details.app_fr_doc_amt             ;
3516       ln_exchange_rate        :=  rec_cur_get_cr_details.exchange_rate              ;
3517 
3518       ln_app_fr_doc_id        :=  p_araa.cash_receipt_id                            ;
3519       ln_app_fr_doc_type      :=  jai_constants.ar_cash_tax_confirmed               ;  /* Receipt confirmation */
3520       ln_app_fr_doc_table     :=  jai_constants.jai_cash_rcpts                      ; /* table JAI_AR_CASH_RECEIPTS_ALL */
3521 
3522       ln_fr_organization_id  :=  rec_cur_get_cr_details.app_fr_organization_id      ;
3523       ln_fr_party_id         :=  rec_cur_get_cr_details.pay_from_customer           ;
3524       ln_fr_party_site_id    :=  rec_cur_get_cr_details.customer_site_use_id        ;
3525 
3526     ELSIF p_document_type = jai_constants.trx_type_cm_app THEN
3527 
3528       OPEN  cur_get_cm_details ( cp_customer_trx_id => p_araa.customer_trx_id  )    ;
3529       FETCH cur_get_cm_details INTO rec_cur_get_cm_details                          ;
3530       CLOSE cur_get_cm_details                                                      ;
3531 
3532       -- Added by Jia Li for inclusive taxes on 2008-01-18
3533       ---------------------------------------------------------------------
3534       OPEN cur_get_exclu_amt (cp_customer_trx_id => p_araa.customer_trx_id );
3535       FETCH cur_get_exclu_amt INTO ln_total_exclu_amt;
3536       CLOSE cur_get_exclu_amt;
3537       ----------------------------------------------------------------------
3538       ld_app_fr_doc_date     :=   rec_cur_get_cm_details.app_fr_doc_date            ;
3539       ln_app_fr_doc_amt      :=   rec_cur_get_cm_details.app_fr_doc_amt + ln_total_exclu_amt;-- Modified by Jia Li for inclusive tax on 2008-01-18
3540       ln_exchange_rate        :=  rec_cur_get_cm_details.exchange_rate              ;
3541 
3542       ln_app_fr_doc_id       :=   p_araa.customer_trx_id                            ;
3543       ln_app_fr_doc_type     :=   jai_constants.ar_invoice_type_cm                                ;  /* 'CM' */
3544       ln_app_fr_doc_table    :=   jai_constants.ar_inv_lines_table                  ; /* table JAI_AR_CASH_RECEIPTS_ALL */
3545 
3546       ln_fr_organization_id  :=  rec_cur_get_cm_details.app_to_organization_id      ;
3547       ln_fr_party_id         :=  rec_cur_get_cm_details.app_to_customer_id          ;
3548       ln_fr_party_site_id    :=  rec_cur_get_cm_details.app_to_customer_site_use_id ;
3549 
3550     END IF;
3551 
3552     ln_app_amount             := nvl(p_araa.amount_applied,0) * ln_exchange_rate    ;
3553 
3554 
3555    /*commented by csahoo for bug# 6401388
3556  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3557                                        pv_log_msg  =>  ' Values for the Internal FROm and TO variables are ' ||fnd_global.local_chr(10)
3558                                                      ||', ln_app_to_doc_id      -> '  ||ln_app_to_doc_id       ||fnd_global.local_chr(10)
3559                                                      ||', ln_app_to_doc_type    -> '  ||ln_app_to_doc_type     ||fnd_global.local_chr(10)
3560                                                      ||', ln_app_to_doc_table   -> '  ||ln_app_to_doc_table    ||fnd_global.local_chr(10)
3561                                                      ||', ln_app_to_doc_amt     -> '  ||ln_app_to_doc_amt      ||fnd_global.local_chr(10)
3562                                                      ||', ld_app_to_doc_date    -> '  ||ld_app_to_doc_date     ||fnd_global.local_chr(10)
3563                                                      ||', ln_to_organization_id -> '  ||ln_to_organization_id  ||fnd_global.local_chr(10)
3564                                                      ||', ln_to_party_id        -> '  ||ln_to_party_id         ||fnd_global.local_chr(10)
3565                                                      ||', ln_to_party_site_id   -> '  ||ln_to_party_site_id    ||fnd_global.local_chr(10)
3566                                                      ||', ln_app_fr_doc_id      -> '  ||ln_app_fr_doc_id       ||fnd_global.local_chr(10)
3567                                                      ||', ln_app_to_doc_type    -> '  ||ln_app_to_doc_type     ||fnd_global.local_chr(10)
3568                                                      ||', ln_app_fr_doc_table   -> '  ||ln_app_fr_doc_table    ||fnd_global.local_chr(10)
3569                                                      ||', ln_app_fr_doc_amt     -> '  ||ln_app_fr_doc_amt      ||fnd_global.local_chr(10)
3570                                                      ||', ld_app_fr_doc_date    -> '  ||ld_app_fr_doc_date     ||fnd_global.local_chr(10)
3571                                                      ||', ln_fr_organization_id -> '  ||ln_fr_organization_id  ||fnd_global.local_chr(10)
3572                                                      ||', ln_fr_party_id        -> '  ||ln_fr_party_id         ||fnd_global.local_chr(10)
3573                                                      ||', ln_fr_party_site_id   -> '  ||ln_fr_party_site_id    ||fnd_global.local_chr(10)
3574                                                      ||', ln_exchange_rate      -> '  ||ln_exchange_rate       ||fnd_global.local_chr(10)
3575                                                      ||', ln_app_amount         -> '  ||ln_app_amount          ||fnd_global.local_chr(10)
3576                                     );*/
3577 
3578     /*########################################################################################################
3579     || DERIVE DATE AS PER LATER DOCUMENT ---- PART -4
3580     ########################################################################################################*/
3581 
3582     /*****
3583     || Derive the document date based on the date of
3584     || the later document
3585     || Also get the tcs amount based on the later document date
3586     ******/
3587     IF ld_app_fr_doc_date      >=  ld_app_to_doc_date THEN                                 -----------A2
3588      /*commented by csahoo for bug# 6401388
3589  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3590                                           pv_log_msg  => '  From date >= To date '||fnd_global.local_chr(10)
3591                                                        ||', ld_app_fr_doc_date ->  '||ld_app_fr_doc_date
3592                                                        ||', ld_app_to_doc_date =>  '|| ld_app_to_doc_date  ||fnd_global.local_chr(10)
3593                                        );*/
3594       ln_app_ref_doc_id        := ln_app_fr_doc_id                        ;
3595       ln_app_ref_doc_type      := ln_app_fr_doc_type                      ;
3596       ln_app_ref_doc_table     := ln_app_fr_doc_table                     ;
3597       ld_source_document_date  := ld_app_fr_doc_date                      ;
3598       --added the IF block for bug#7393380
3599       -- for receipt application, the apportion factor should be negative.
3600       IF p_document_type = jai_constants.trx_type_rct_app   THEN
3601         ln_apportion_factor      := -1 * ln_app_amount/nvl(ln_app_fr_doc_amt,1)  ;
3602       ELSE
3603         ln_apportion_factor      := ln_app_amount/nvl(ln_app_fr_doc_amt,1)  ;
3604       END IF;
3605 
3606       ln_organization_id       := ln_fr_organization_id                   ;
3607       ln_party_id              := ln_fr_party_id                          ;
3608       ln_party_site_id         := ln_fr_party_site_id                     ;
3609 
3610     ELSE                                                                                            -----------A2
3611       /*
3612       || Applied To document is later
3613       */
3614 
3615      /*commented by csahoo for bug# 6401388
3616  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3617                                           pv_log_msg  => '  To date is greater than from date '||fnd_global.local_chr(10)
3618                                                        ||', ld_app_fr_doc_date ->  '||ld_app_fr_doc_date ||fnd_global.local_chr(10)
3619                                                        ||', ld_app_to_doc_date =>  '|| ld_app_to_doc_date
3620                                        );*/
3621 
3622       ln_app_ref_doc_id        := ln_app_to_doc_id                        ;
3623       ln_app_ref_doc_type      := ln_app_to_doc_type ;
3624       ln_app_ref_doc_table     := ln_app_to_doc_table;
3625       ld_source_document_date  := ld_app_to_doc_date;
3626       --added the IF block for bug#7393380
3627       IF p_document_type = jai_constants.trx_type_rct_app   THEN
3628         ln_apportion_factor      := -1 * (ln_app_amount/nvl(ln_app_to_doc_amt,1)) ;
3629       ELSE
3630         ln_apportion_factor      := ln_app_amount/nvl(ln_app_to_doc_amt,1) ;
3631       END IF;
3632 
3633       ln_organization_id       := ln_to_organization_id ;
3634       ln_party_id              := ln_to_party_id        ;
3635       ln_party_site_id         := ln_to_party_site_id   ;
3636 
3637     END IF;                                                                                         -----------A2
3638 
3639 
3640     /*commented by csahoo for bug# 6401388
3641  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3642                                         pv_log_msg  =>  ' based on later date derivation logic values derived are ' ||fnd_global.local_chr(10)
3643                                                       ||', ln_app_ref_doc_id       -> '  ||ln_app_ref_doc_id        ||fnd_global.local_chr(10)
3644                                                       ||', ln_app_ref_doc_type     -> '  ||ln_app_ref_doc_type      ||fnd_global.local_chr(10)
3645                                                       ||', ln_app_ref_doc_table    -> '  ||ln_app_ref_doc_table     ||fnd_global.local_chr(10)
3646                                                       ||', ld_source_document_date -> '  ||ld_source_document_date  ||fnd_global.local_chr(10)
3647                                                       ||', ln_apportion_factor     -> '  ||ln_apportion_factor      ||fnd_global.local_chr(10)
3648                                                       ||', ln_organization_id      -> '  ||ln_organization_id       ||fnd_global.local_chr(10)
3649                                                       ||', ln_party_id             -> '  ||ln_party_id              ||fnd_global.local_chr(10)
3650                                                       ||', ln_party_site_id        -> '  ||ln_party_site_id
3651                                     );*/
3652 
3653 
3654     /*########################################################################################################
3655     || DERIVE TCS AMOUNT IN CASE OF OVERAPPLICATION ---- CURRENTLY COMMENTED PART -5
3656     ########################################################################################################
3657 
3658 
3659     -- || Derive the tcs tax amount according to the below formula :-
3660     -- || Check if application_amount > applied to amount
3661     -- || IF yes then it indicates overapplication else it is normal application
3662 
3663     IF abs(ln_app_amount) > abs(ln_app_to_doc_amt) THEN
3664 
3665     --      || Case is OVERAPPLICATION
3666     --    || Hence consider the tcs amount from the APPLIED TO DOCUMENT
3667 
3668       ln_app_amount        := ln_app_to_doc_amt;
3669       ln_app_ref_doc_id    := ln_app_to_doc_id   ;
3670       ln_app_ref_doc_type      := ln_app_to_doc_type ;
3671       ln_app_ref_doc_table := ln_app_to_doc_table;
3672       ln_apportion_factor  := 1 ;
3673     END IF;                                                                                         -----------A2
3674   */
3675 
3676     /*########################################################################################################
3677     || DERIVE THE PARENT DOCUMENT REFERENCE ---- PART -5
3678     ########################################################################################################*/
3679 
3680     OPEN cur_get_parent_transaction ( cp_source_document_id    => ln_app_ref_doc_id     ,
3681                                       cp_source_document_type  => ln_app_ref_doc_type
3682                                     );
3683     FETCH cur_get_parent_transaction INTO ln_parent_transaction_id;
3684     CLOSE cur_get_parent_transaction  ;
3685     /*commented by csahoo for bug# 6401388
3686  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3687                                         pv_log_msg  =>  ' parent transaction id is (ln_parent_transaction_id)' ||ln_parent_transaction_id
3688                                      );*/
3689 
3690 
3691     /*########################################################################################################
3692     || INSERT APPLICATION RECORDS  INTO JAI_RGM_TRX_REFS_ALL TABLE  ---- PART -1
3693     ########################################################################################################*/
3694     /*
3695     ||Get the sequence generated unique key for the transaction
3696     */
3697     OPEN  cur_get_transaction_id ;
3698     FETCH cur_get_transaction_id INTO ln_transaction_id ;
3699     CLOSE cur_get_transaction_id ;
3700 
3701    /*commented by csahoo for bug# 6401388
3702  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3703                                         pv_log_msg  =>  ' Before call to insert_repository_references '
3704                                      );*/
3705     insert_repository_references     (
3706                                                       p_regime_id                     =>   NULL                                           ,
3707                                                       p_transaction_id                =>   ln_transaction_id                              ,
3708                                                       p_source_ref_document_id        =>   ln_app_ref_doc_id                              ,
3709                                                       p_source_ref_document_type      =>   ln_app_ref_doc_type                            ,
3710                                                       p_app_from_document_id          =>   ln_app_fr_doc_id                               ,
3711                                                       p_app_from_document_type        =>   ln_app_fr_doc_type                             ,
3712                                                       p_app_to_document_id            =>   ln_app_to_doc_id                               ,
3713                                                       p_app_to_document_type          =>   ln_app_to_doc_type                             ,
3714                                                       p_parent_transaction_id         =>   ln_parent_transaction_id                       ,
3715                                                       p_org_tan_no                    =>   NULL                                           ,
3716                                                       p_document_id                   =>   p_araa.receivable_application_id               ,
3717                                                       p_document_type                 =>   p_document_type                                ,
3718                                                       p_document_line_id              =>   p_araa.receivable_application_id               ,
3719                                                       p_document_date                 =>   ld_source_document_date                        ,
3720                                                       p_table_name                    =>   jai_constants.ar_receipt_app                   ,
3721                                                       p_line_amount                   =>   ln_app_amount                                  ,
3722                                                       p_document_amount               =>   ln_app_amount                                  ,
3723                                                       p_org_id                        =>   p_araa.org_id                                  ,
3724                                                       p_organization_id               =>   ln_organization_id                             ,
3725                                                       p_party_id                      =>   ln_party_id                                    ,
3726                                                       p_party_site_id                 =>   ln_party_site_id                               ,
3727                                                       p_item_classification           =>   p_item_classification                          ,
3728                                                       p_trx_ref_id                    =>   ln_trx_ref_id                                  ,
3729                                                       p_process_flag                  =>   lv_process_flag                                ,
3730                                                       p_process_message               =>   lv_process_message
3731                                        );
3732 
3733 
3734      /*commented by csahoo for bug# 6401388
3735  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3736                                           pv_log_msg  =>  ' Returned from call to insert_repository_references, lv_process_flag -> '||lv_process_flag
3737                                        );*/
3738     IF lv_process_flag = jai_constants.expected_error    OR                      ---------A2
3739        lv_process_flag = jai_constants.unexpected_error  OR
3740        lv_process_flag = jai_constants.not_applicable
3741     THEN
3742       /*
3743       || As Returned status is an error/not applicable hence:-
3744       || Set out variables p_process_flag and p_process_message accordingly
3745       */
3746       --call to debug package
3747     /*commented by csahoo for bug# 6401388
3748  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3749                                         pv_log_msg  =>  ' Error in procedure insert_repository_references '
3750                                         ||', p_process_flag      -> '|| p_process_flag
3751                                         ||', lv_process_message  -> '|| lv_process_message
3752                                       );*/
3753       p_process_flag    := lv_process_flag    ;
3754       p_process_message := lv_process_message ;
3755       return;
3756     END IF;                                                                      ---------A2
3757 
3758     /*########################################################################################################
3759     || COPY APPLICATION TAXES FROM SOURCE TRANSACTION TABLES INTO JAI_RGM_TAXES TABLE  ---- PART -2
3760     ########################################################################################################*/
3761       /*commented by csahoo for bug# 6401388
3762  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3763                                           pv_log_msg  =>  ' Before call to copy_taxes_from_source '
3764                                        );*/
3765     copy_taxes_from_source    (  p_source_document_type  =>  ln_app_ref_doc_type  ,
3766                                  p_source_document_id    =>  ln_app_ref_doc_id    ,
3767                                  p_apportion_factor      =>  ln_apportion_factor  ,
3768                                  p_trx_ref_id            =>  ln_trx_ref_id        ,
3769                                  p_process_flag          =>  lv_process_flag      ,
3770                                  p_process_message       =>  lv_process_message
3771                                );
3772 
3773    /*commented by csahoo for bug# 6401388
3774  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3775                                         pv_log_msg  =>  ' Returned from call to copy_taxes_from_source, lv_process_flag -> '||lv_process_flag
3776                                      );*/
3777     IF lv_process_flag = jai_constants.expected_error    OR                      ---------A2
3778        lv_process_flag = jai_constants.unexpected_error  OR
3779        lv_process_flag = jai_constants.not_applicable
3780     THEN
3781       /*
3782       || As Returned status is an error/not applicable hence:-
3783       || Set out variables p_process_flag and p_process_message accordingly
3784       */
3785       --call to debug package
3786     /*commented by csahoo for bug# 6401388
3787  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3788                                         pv_log_msg  =>  ' Error in procedure copy_taxes_from_source '
3789                                         ||', p_process_flag      -> '|| p_process_flag
3790                                         ||', lv_process_message  -> '|| lv_process_message
3791                                       );*/
3792       p_process_flag    := lv_process_flag    ;
3793       p_process_message := lv_process_message ;
3794       return;
3795     END IF;                                                                      ---------A2
3796   /*commented by csahoo for bug# 6401388
3797  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3798                              pv_log_msg  =>  '**************** PROCEDURE PROCESS_APPLICATIONS SUCCESSFULLY COMPLETED ****************'
3799                           );
3800     jai_cmn_debug_contexts_pkg.deregister (pn_reg_id => ln_reg_id);*/
3801   END process_applications;
3802 
3803 
3804 PROCEDURE process_unapp_rcpt_rev (  p_araa                      IN              AR_RECEIVABLE_APPLICATIONS_ALL%ROWTYPE   DEFAULT NULL  ,
3805                                     p_acra                      IN              AR_CASH_RECEIPTS_ALL%ROWTYPE             DEFAULT NULL  ,
3806                                     p_document_type             IN              VARCHAR2                                               ,
3807                                     p_process_flag              OUT NOCOPY      VARCHAR2                                               ,
3808                                     p_process_message           OUT NOCOPY      VARCHAR2
3809                                   )
3810 
3811 IS
3812 
3813   ln_reg_id           NUMBER;
3814   /*
3815   ||Get the parent record for an type of record
3816   */
3817   CURSOR cur_get_parent_trx ( cp_source_document_id     JAI_RGM_REFS_ALL.SOURCE_DOCUMENT_ID%TYPE   DEFAULT NULL     ,
3818                               cp_source_document_type   JAI_RGM_REFS_ALL.SOURCE_DOCUMENT_TYPE%TYPE                  ,
3819                               cp_app_from_document_id   JAI_RGM_REFS_ALL.APP_FROM_DOCUMENT_ID%TYPE DEFAULT NULL     ,
3820                               cp_app_to_document_id     JAI_RGM_REFS_ALL.APP_TO_DOCUMENT_ID%TYPE   DEFAULT NULL
3821                              )
3822   IS
3823   SELECT
3824           to_number(max(transaction_id)) parent_transaction_id
3825   FROM
3826           jai_rgm_refs_all
3827   WHERE
3828           source_document_id      =  nvl(cp_source_document_id  , source_document_id)
3829   AND     source_document_type    =  cp_source_document_type
3830   AND     app_from_document_id    =  nvl(app_from_document_id   , app_from_document_id )
3831   AND     app_to_document_id      =  nvl(app_to_document_id     , app_to_document_id   ) ;
3832 
3833   ln_new_document_id        JAI_RGM_REFS_ALL.SOURCE_DOCUMENT_ID%TYPE    ;
3834   ln_source_document_id     JAI_RGM_REFS_ALL.SOURCE_DOCUMENT_ID%TYPE    ;
3835   ld_new_document_date      JAI_RGM_REFS_ALL.SOURCE_DOCUMENT_DATE%TYPE  ;
3836   lv_source_document_type   JAI_RGM_REFS_ALL.SOURCE_DOCUMENT_TYPE%TYPE  ;
3837   ln_app_from_document_id   JAI_RGM_REFS_ALL.APP_FROM_DOCUMENT_ID%TYPE  ;
3838   ln_app_to_document_id     JAI_RGM_REFS_ALL.APP_TO_DOCUMENT_ID%TYPE    ;
3839   ln_parent_transaction_id  JAI_RGM_REFS_ALL.TRANSACTION_ID%TYPE        ;
3840   lv_process_flag           VARCHAR2(2)                                 ;
3841   lv_process_message        VARCHAR2(2000)                              ;
3842 
3843 BEGIN
3844   /*########################################################################################################
3845   || VARIABLES INITIALIZATION
3846   ########################################################################################################*/
3847   lv_member_name        := 'PROCESS_UNAPP_RCPT_REV';
3848   set_debug_context;
3849   /*commented by csahoo for bug# 6401388
3850   jai_cmn_debug_contexts_pkg.register ( pv_context => lv_context
3851                              , pn_reg_id  => ln_reg_id
3852                              );*/
3853 
3854   lv_process_flag       := jai_constants.successful   ;
3855   lv_process_message    := null                       ;
3856 
3857   p_process_flag        := lv_process_flag            ;
3858   p_process_message     := lv_process_message         ;
3859 
3860  /* IF p_document_type In ( jai_constants.trx_type_rct_unapp  ,
3861                           jai_constants.trx_type_cm_unapp
3862                         )
3863   THEN
3864    jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3865                                       pv_log_msg  =>  ' Processing the UN APPLICATION record ' ||fnd_global.local_chr(10)
3866                                            ||', p_document_type           -> '  ||p_document_type                     ||fnd_global.local_chr(10)
3867                                            ||', receivable_application_id -> '  ||p_araa.receivable_application_id    ||fnd_global.local_chr(10)
3868                                            ||', application_type          -> '  ||p_araa.application_type             ||fnd_global.local_chr(10)
3869                                            ||', status                    -> '  ||p_araa.status                       ||fnd_global.local_chr(10)
3870                                            ||', display                   -> '  ||p_araa.display                      ||fnd_global.local_chr(10)
3871                                            ||', cash_receipt_id           -> '  ||p_araa.cash_receipt_id              ||fnd_global.local_chr(10)
3872                                            ||', customer_trx_id           -> '  ||p_araa.customer_trx_id              ||fnd_global.local_chr(10)
3873                                            ||', applied_customer_trx_id   -> '  ||p_araa.applied_customer_trx_id      ||fnd_global.local_chr(10)
3874                                            ||', amount_applied            -> '  ||p_araa.amount_applied               ||fnd_global.local_chr(10)
3875                                            ||', gl_date                   -> '  ||p_araa.gl_date                      ||fnd_global.local_chr(10)
3876                                            ||', org_id                    -> '  ||p_araa.org_id
3877                                     );
3878   ELSIF p_document_type  = jai_constants.trx_type_rct_rvs  THEN
3879    jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3880                                        pv_log_msg  =>  ' Processing the Receipt reversal record ' ||fnd_global.local_chr(10)
3881                                            ||', p_document_type           -> '  ||p_document_type                     ||fnd_global.local_chr(10)
3882                                            ||', cash_receipt_id -> '  ||p_acra.cash_receipt_id||fnd_global.local_chr(10)
3883                                            ||', receipt_number  -> '  ||p_acra.cash_receipt_id||fnd_global.local_chr(10)
3884                                            ||', amount          -> '  ||p_acra.cash_receipt_id||fnd_global.local_chr(10)
3885                                     );
3886 
3887   END IF;
3888 
3889 
3890   /*########################################################################################################
3891   || DERIVE VALUES BASED ON APPLICATION TYPE
3892   ########################################################################################################*/
3893 
3894   IF p_document_type  = jai_constants.trx_type_rct_rvs  THEN
3895     ln_new_document_id      := p_acra.cash_receipt_id                 ;  /* New document id to be created */
3896     ld_new_document_date    := p_acra.reversal_date                   ;
3897 
3898     ln_source_document_id   := p_acra.cash_receipt_id                 ;
3899     lv_source_document_type := jai_constants.ar_cash_tax_confirmed    ;  /* Parent Document type */
3900 
3901 
3902   ELSIF  p_document_type = jai_constants.trx_type_rct_unapp  THEN
3903     ln_new_document_id      := p_araa.receivable_application_id       ;  /* New document id to be created */
3904     ld_new_document_date    := p_araa.apply_date                      ;
3905 
3906     lv_source_document_type := jai_constants.trx_type_rct_app         ;  /* Parent Document type */
3907     ln_app_from_document_id := p_araa.cash_receipt_id                 ;
3908     ln_app_to_document_id   := p_araa.applied_customer_trx_id         ;
3909 
3910 
3911   ELSIF  p_document_type = jai_constants.trx_type_cm_unapp THEN
3912     ln_new_document_id      := p_araa.receivable_application_id       ; /* New document id to be created */
3913     ld_new_document_date    := p_araa.apply_date                      ;
3914 
3915     lv_source_document_type := jai_constants.trx_type_cm_app          ; /* Parent Document type */
3916     ln_app_from_document_id := p_araa.customer_trx_id                 ;
3917     ln_app_to_document_id   := p_araa.applied_customer_trx_id        ;
3918 
3919   END IF;
3920 
3921 
3922    /*commented by csahoo for bug# 6401388
3923  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3924                                        pv_log_msg  =>  ' Values derived for an application/reversal are ' ||fnd_global.local_chr(10)
3925                                            ||', ln_new_document_id       -> '  ||ln_new_document_id       ||fnd_global.local_chr(10)
3926                                            ||', ld_new_document_date     -> '  ||ld_new_document_date     ||fnd_global.local_chr(10)
3927                                            ||', ln_source_document_id    -> '  ||ln_source_document_id    ||fnd_global.local_chr(10)
3928                                            ||', lv_source_document_type  -> '  ||lv_source_document_type  ||fnd_global.local_chr(10)
3929                                            ||', ln_app_from_document_id  -> '  ||ln_app_from_document_id  ||fnd_global.local_chr(10)
3930                                            ||', ln_app_to_document_id    -> '  ||ln_app_to_document_id
3931                                     );*/
3932 
3933 
3934   /*
3935   ||Get the parent transaction for the receipt reversal/Unapplication
3936   */
3937 
3938   OPEN cur_get_parent_trx ( cp_source_document_id     => ln_source_document_id   ,
3939                             cp_source_document_type   => lv_source_document_type ,
3940                             cp_app_from_document_id   => ln_app_from_document_id ,
3941                             cp_app_to_document_id     => ln_app_to_document_id
3942                           ) ;
3943   FETCH cur_get_parent_trx INTO ln_parent_transaction_id ;
3944 
3945    /*commented by csahoo for bug# 6401388
3946  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3947                                        pv_log_msg  =>  ' Parent transaction is ->' ||ln_parent_transaction_id
3948                                     );*/
3949   /*
3950   || Check that the source receipt confirmation record has been found in the TCS repository.
3951   || If not found then receipt reversal also need not hit the repository.
3952   */
3953   IF CUR_GET_PARENT_TRX%FOUND THEN
3954     CLOSE  cur_get_parent_trx ;
3955     /*commented by csahoo for bug# 6401388
3956  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3957                                        pv_log_msg  =>  ' Parent transaction found, Before call to copy_references '
3958                                     );*/
3959     copy_references (  p_parent_transaction_id   => ln_parent_transaction_id   ,
3960                        p_new_document_id         => ln_new_document_id         ,
3961                        p_new_document_type       => p_document_type            ,
3962                        p_new_document_date       => ld_new_document_date       ,
3963                        p_apportion_factor        => -1                         ,/* As reversal cannot be partial  */
3964                        p_process_flag            => lv_process_flag            ,
3965                        p_process_message         => lv_process_message
3966                     );
3967 
3968     /*commented by csahoo for bug# 6401388
3969  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3970                                        pv_log_msg  =>  ' returned from call to copy_references lv_process_flag -> '||lv_process_flag
3971                                     );*/
3972     IF lv_process_flag = jai_constants.expected_error    OR                      ---------A2
3973        lv_process_flag = jai_constants.unexpected_error  OR
3974        lv_process_flag = jai_constants.not_applicable
3975     THEN
3976       /*
3977       || As Returned status is an error/not applicable hence:-
3978       || Set out variables p_process_flag and p_process_message accordingly
3979       */
3980       --call to debug package
3981      /*commented by csahoo for bug# 6401388
3982  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3983                                           pv_log_msg  =>  ' Error in processing copy references ' ||fnd_global.local_chr(10)
3984                                                         ||',lv_process_flag -> '||lv_process_flag    ||fnd_global.local_chr(10)
3985                                                         ||',lv_process_message -> '||lv_process_message
3986                                     );*/
3987       p_process_flag    := lv_process_flag    ;
3988       p_process_message := lv_process_message ;
3989       return;
3990     END IF;                                                                      ---------A2
3991   ELSE
3992     /*
3993     ||Base document not found hence skip the document
3994     */
3995      p_process_flag := jai_constants.not_applicable;
3996   END IF; /* Parent transaction found*/
3997  /*commented by csahoo for bug# 6401388
3998  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
3999                              pv_log_msg  =>  '**************** PROCEDURE PROCESS_UNAPP_RCPT_REV SUCCESSFULLY COMPLETED ****************'
4000                           );
4001   jai_cmn_debug_contexts_pkg.deregister (pn_reg_id => ln_reg_id);*/
4002 END process_unapp_rcpt_rev ;
4003 
4004 
4005   procedure insert_repository_references (  p_regime_id                  IN            JAI_RGM_DEFINITIONS.REGIME_ID%TYPE                      DEFAULT NULL    ,
4006                                             p_transaction_id             IN            JAI_RGM_REFS_ALL.TRANSACTION_ID%TYPE                            ,
4007                                             p_source_ref_document_id     IN            JAI_RGM_REFS_ALL.SOURCE_REF_DOCUMENT_ID%TYPE    DEFAULT NULL    ,
4008                                             p_source_ref_document_type   IN            JAI_RGM_REFS_ALL.SOURCE_REF_DOCUMENT_TYPE%TYPE                  ,
4009                                             p_app_from_document_id       IN            JAI_RGM_REFS_ALL.APP_FROM_DOCUMENT_ID%TYPE      DEFAULT NULL    ,
4010                                             p_app_from_document_type     IN            JAI_RGM_REFS_ALL.APP_FROM_DOCUMENT_TYPE%TYPE    DEFAULT NULL    ,
4011                                             p_app_to_document_id         IN            JAI_RGM_REFS_ALL.APP_TO_DOCUMENT_ID%TYPE        DEFAULT NULL    ,
4012                                             p_app_to_document_type       IN            JAI_RGM_REFS_ALL.APP_TO_DOCUMENT_TYPE%TYPE      DEFAULT NULL    ,
4013                                             p_parent_transaction_id      IN            JAI_RGM_REFS_ALL.PARENT_TRANSACTION_ID%TYPE     DEFAULT NULL    ,
4014                                             p_org_tan_no                 IN            JAI_RGM_REFS_ALL.ORG_TAN_NO%TYPE                DEFAULT NULL    ,
4015                                             p_document_id                IN            NUMBER                                                          ,
4016                                             p_document_type              IN            VARCHAR2                                                        ,
4017                                             p_document_line_id           IN            NUMBER                                                          ,
4018                                             p_document_date              IN            DATE                                                            ,
4019                                             p_table_name                 IN            VARCHAR2                                                        ,
4020                                             p_line_amount                IN            NUMBER                                                          ,
4021                                             p_document_amount            IN            NUMBER                                                          ,
4022                                             p_org_id                     IN            NUMBER                                                          ,
4023                                             p_organization_id            IN            NUMBER                                                          ,
4024                                             p_party_id                   IN            NUMBER                                                          ,
4025                                             p_party_site_id              IN            NUMBER                                                          ,
4026                                             p_item_classification        IN            VARCHAR2                                                        ,
4027                                             p_trx_ref_id                 OUT NOCOPY    JAI_RGM_REFS_ALL.TRX_REF_ID%TYPE                                ,
4028                                             p_process_flag               OUT NOCOPY    VARCHAR2                                                        ,
4029                                             p_process_message            OUT NOCOPY    VARCHAR2
4030                                         )
4031 
4032   IS
4033 
4034     /****
4035     ||Get the primary key
4036     || for the table jai_rgm_refs_all
4037     *****/
4038     ln_reg_id           NUMBER;
4039     ln_regime_id                    JAI_RGM_DEFINITIONS.REGIME_ID%TYPE                        ;
4040     lv_org_tan_no                   JAI_RGM_REGISTRATIONS.ATTRIBUTE_VALUE%TYPE                ;
4041     ln_trx_ref_id                   JAI_RGM_REFS_ALL.TRX_REF_ID%TYPE                  ;
4042     lv_process_flag                 VARCHAR2(2)                                       ;
4043     lv_process_message              VARCHAR2(2000)                                    ;
4044     ln_user_id                      JAI_RGM_REFS_ALL.CREATED_BY%TYPE                  ;
4045     ln_login_id                     JAI_RGM_REFS_ALL.LAST_UPDATE_LOGIN%TYPE           ;
4046     ln_fin_year                     JAI_AP_TDS_YEARS.FIN_YEAR%TYPE                 ;
4047     ln_source_ref_document_id       JAI_RGM_REFS_ALL.SOURCE_REF_DOCUMENT_ID%TYPE      ;
4048     ln_source_ref_document_type     JAI_RGM_REFS_ALL.SOURCE_REF_DOCUMENT_TYPE%TYPE    ;
4049     ln_threshold_slab_id            JAI_RGM_REFS_ALL.THRESHOLD_SLAB_ID%TYPE           ;
4050   BEGIN
4051 
4052   /*################################################################################################################
4053   || Initialize the variables
4054   ################################################################################################################*/
4055   lv_member_name        := 'INSERT_REPOSITORY_REFERENCES';
4056   set_debug_context;
4057   /*commented by csahoo for bug# 6401388
4058   jai_cmn_debug_contexts_pkg.register ( pv_context => lv_context
4059                              , pn_reg_id  => ln_reg_id
4060                              );*/
4061 
4062   /*commented by csahoo for bug# 6401388
4063  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
4064                              pv_log_msg  =>  ' PARAMETERS VALUES PASSED TO INSERT_REPOSITORY_REFERENCES : - '  ||fnd_global.local_chr(10)
4065                                            ||', p_regime_id                   -> '||p_regime_id                ||fnd_global.local_chr(10)
4066                                            ||', p_transaction_id              -> '||p_transaction_id           ||fnd_global.local_chr(10)
4067                                            ||', p_source_ref_document_id      -> '||p_source_ref_document_id   ||fnd_global.local_chr(10)
4068                                            ||', p_source_ref_document_type    -> '||p_source_ref_document_type ||fnd_global.local_chr(10)
4069                                            ||', p_app_from_document_id        -> '||p_app_from_document_id     ||fnd_global.local_chr(10)
4070                                            ||', p_app_from_document_type      -> '||p_app_from_document_type   ||fnd_global.local_chr(10)
4071                                            ||', p_app_to_document_id          -> '||p_app_to_document_id       ||fnd_global.local_chr(10)
4072                                            ||', p_app_to_document_type        -> '||p_app_to_document_type     ||fnd_global.local_chr(10)
4073                                            ||', p_parent_transaction_id       -> '||p_parent_transaction_id    ||fnd_global.local_chr(10)
4074                                            ||', p_org_tan_no                  -> '||p_org_tan_no              ||fnd_global.local_chr(10)
4075                                            ||', p_document_id                 -> '||p_document_id              ||fnd_global.local_chr(10)
4076                                            ||', p_document_type               -> '||p_document_type            ||fnd_global.local_chr(10)
4077                                            ||', p_document_line_id            -> '||p_document_line_id         ||fnd_global.local_chr(10)
4078                                            ||', p_document_date               -> '||p_document_date            ||fnd_global.local_chr(10)
4079                                            ||', p_table_name                  -> '||p_table_name               ||fnd_global.local_chr(10)
4080                                            ||', p_line_amount                 -> '||p_line_amount              ||fnd_global.local_chr(10)
4081                                            ||', p_document_amount             -> '||p_document_amount          ||fnd_global.local_chr(10)
4082                                            ||', p_org_id                      -> '||p_org_id                   ||fnd_global.local_chr(10)
4083                                            ||', p_organization_id             -> '||p_organization_id          ||fnd_global.local_chr(10)
4084                                            ||', p_party_id                    -> '||p_party_id                 ||fnd_global.local_chr(10)
4085                                            ||', p_party_site_id               -> '||p_party_site_id            ||fnd_global.local_chr(10)
4086                                            ||', p_item_classification         -> '||p_item_classification      ||fnd_global.local_chr(10)
4087                                            ||', p_trx_ref_id                  -> '||p_trx_ref_id               ||fnd_global.local_chr(10)
4088                           );*/
4089 
4090 
4091     lv_process_flag    := jai_constants.successful   ;
4092     lv_process_message := null                       ;
4093 
4094     p_process_flag     := lv_process_flag            ;
4095     p_process_message  := lv_process_message         ;
4096 
4097     ln_user_id         := fnd_global.user_id         ;
4098     ln_login_id        := fnd_global.login_id        ;
4099 
4100    OPEN  cur_get_trx_ref_id ;
4101    FETCH cur_get_trx_ref_id INTO ln_trx_ref_id ;
4102    CLOSE cur_get_trx_ref_id ;
4103 
4104    /**********
4105    || IF source_ref_document_id and source_ref_document_type  are null then they should be same as
4106    || trx_ref_id.
4107    || The value for source_ref_document_id and source_ref_document_type would be different from trx_ref_id only in case of reversal,application and unapplication
4108    || in which case it would be same as the corresponding source receipt or invoice
4109    ************/
4110    p_trx_ref_id     := ln_trx_ref_id     ;
4111 
4112    ln_source_ref_document_id := NVL(p_source_ref_document_id,p_document_id) ;
4113 -- code ported from BL12.1.3 by zhiwei.xin on 15-MAR-2013 begin
4114 /* --Commented by Chong for DTC ER 2012/09/11 Get TCS calendar from regime start
4115    OPEN  get_tcs_fin_year(  cp_org_id    => p_org_id         ,
4116                             cp_trx_date  => p_document_date
4117                          );
4118 
4119    FETCH get_tcs_fin_year INTO ln_fin_year;
4120    CLOSE get_tcs_fin_year;
4121 */
4122 --Added by Chong for DTC ER 2012/09/11 Get TCS calendar from regime start
4123   ln_fin_year := get_tcs_fin_year('TCS_CALENDAR','TCS', p_document_date);
4124 --Added by Chong for DTC ER 2012/09/11 Get TCS calendar from regime end
4125 -- code ported from BL12.1.3 by zhiwei.xin on 15-MAR-2013 end.
4126   /*********
4127   || Get the regime_id and org_tan_no in case the p_regime_id or p_org_tan_no is null
4128   *********/
4129    IF p_regime_id   IS NULL OR
4130       p_org_tan_no IS NULL
4131    THEN
4132     /*commented by csahoo for bug# 6401388
4133  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
4134                                 pv_log_msg  => ' Derive regime values as they are null  '
4135                               );*/
4136      OPEN c_get_rgm_attribute (   cp_regime_code           =>   jai_constants.tcs_regime                  ,
4137                                   cp_attribute_code        =>   jai_constants.rgm_attr_code_org_tan       ,
4138                                   cp_organization_id       =>   p_organization_id
4139                                ) ;
4140      FETCH c_get_rgm_attribute INTO ln_regime_id, lv_org_tan_no ;
4141      IF C_GET_RGM_ATTRIBUTE%NOTFOUND THEN
4142        CLOSE c_get_rgm_attribute;
4143        p_process_flag     := jai_constants.expected_error;
4144        p_process_message  := 'Org Tan Number needs to be defined for the TCS regime ';
4145        return;
4146      END IF;
4147      CLOSE c_get_rgm_attribute;
4148 
4149      /*commented by csahoo for bug# 6401388
4150  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
4151                                 pv_log_msg  => 'Regime values are '
4152                                 ||', ln_regime_id   -> '||ln_regime_id
4153                                 ||', lv_org_tan_no -> '||lv_org_tan_no
4154                               );*/
4155 
4156     ELSE
4157       ln_regime_id    := p_regime_id   ;
4158       lv_org_tan_no   := p_org_tan_no ;
4159     END IF;
4160 
4161      /*commented by csahoo for bug# 6401388
4162  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
4163                                 pv_log_msg  => 'Regime values are '
4164                                 ||', ln_regime_id   -> '||ln_regime_id
4165                                 ||', lv_org_tan_no -> '||lv_org_tan_no
4166                               );*/
4167 
4168   /*################################################################################################################
4169   || INSERT THE DOCUMENT RECORD INTO THE TCS REPOSITORY
4170   ################################################################################################################*/
4171 
4172 
4173       /*commented by csahoo for bug# 6401388
4174  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
4175                                  pv_log_msg  =>  ' VALUES insert into JAI_RGM_REFS_ALL are : - ' ||fnd_global.local_chr(10)
4176                                                ||', trx_ref_id                -> '||p_trx_ref_id                      ||fnd_global.local_chr(10)
4177                                                ||', regime_id                 -> '||ln_regime_id                      ||fnd_global.local_chr(10)
4178                                                ||', transaction_id            -> '||p_transaction_id                  ||fnd_global.local_chr(10)
4179                                                ||', source_ref_document_id    -> '||ln_source_ref_document_id         ||fnd_global.local_chr(10)
4180                                                ||', source_ref_document_type  -> '||p_source_ref_document_type        ||fnd_global.local_chr(10)
4181                                                ||', app_from_document_id      -> '||p_app_from_document_id            ||fnd_global.local_chr(10)
4182                                                ||', app_from_document_ty      -> '||p_app_from_document_type          ||fnd_global.local_chr(10)
4183                                                ||', app_to_document_id        -> '||p_app_to_document_id              ||fnd_global.local_chr(10)
4184                                                ||', app_to_document_type      -> '||p_app_to_document_type            ||fnd_global.local_chr(10)
4185                                                ||', parent_transaction_id     -> '||p_parent_transaction_id           ||fnd_global.local_chr(10)
4186                                                ||', org_tan_no                -> '||lv_org_tan_no                     ||fnd_global.local_chr(10)
4187                                                ||', source_document_id        -> '||p_document_id                     ||fnd_global.local_chr(10)
4188                                                ||', source_document_line      -> '||p_document_line_id                ||fnd_global.local_chr(10)
4189                                                ||', source_document_type      -> '||p_document_type                   ||fnd_global.local_chr(10)
4190                                                ||', source_document_date      -> '||p_document_date                   ||fnd_global.local_chr(10)
4191                                                ||', source_table_name         -> '||p_table_name                      ||fnd_global.local_chr(10)
4192                                                ||', line_amt                  -> '||p_line_amount                     ||fnd_global.local_chr(10)
4193                                                ||', source_document_amt       -> '||p_document_amount                 ||fnd_global.local_chr(10)
4194                                                ||', total_tax_amt             -> '||NULL                              ||fnd_global.local_chr(10)
4195                                                ||', party_id                  -> '||p_party_id                        ||fnd_global.local_chr(10)
4196                                                ||', party_type                -> '||jai_constants.party_type_customer ||fnd_global.local_chr(10)
4197                                                ||', party_site_id             -> '||p_party_site_id                   ||fnd_global.local_chr(10)
4198                                                ||', item_classification       -> '||p_item_classification             ||fnd_global.local_chr(10)
4199                                                ||', org_id                    -> '||p_org_id                          ||fnd_global.local_chr(10)
4200                                                ||', organization_id           -> '||p_organization_id                 ||fnd_global.local_chr(10)
4201                                                ||', fin_year                  -> '||ln_fin_year                       ||fnd_global.local_chr(10)
4202                                                ||', threshold_slab_id         -> '||ln_threshold_slab_id              ||fnd_global.local_chr(10)
4203                                                ||', created_by                -> '||ln_user_id                        ||fnd_global.local_chr(10)
4204                                                ||', creation_date             -> '||sysdate                           ||fnd_global.local_chr(10)
4205                                                ||', last_updated_by           -> '||ln_user_id                        ||fnd_global.local_chr(10)
4206                                                ||', last_update_date          -> '||sysdate                           ||fnd_global.local_chr(10)
4207                                                ||', last_update_login         -> '||ln_login_id                       ||fnd_global.local_chr(10)
4208                                                ||', settlement_id             -> '||NULL                              ||fnd_global.local_chr(10)
4209                                                ||', certificate_id            -> '||NULL
4210                               );*/
4211 
4212    INSERT into jai_rgm_refs_all (
4213                                     trx_ref_id                                  ,
4214                                     regime_id                                   ,
4215                                     transaction_id                              ,
4216                                     source_ref_document_id                      ,
4217                                     source_ref_document_type                    ,
4218                                     app_from_document_id                        ,
4219                                     app_from_document_type                      ,
4220                                     app_to_document_id                          ,
4221                                     app_to_document_type                        ,
4222                                     parent_transaction_id                       ,
4223                                     org_tan_no                                  ,
4224                                     source_document_id                          ,
4225                                     source_document_line_id                     ,
4226                                     source_document_type                        ,
4227                                     source_document_date                        ,
4228                                     source_table_name                           ,
4229                                     line_amt                                    ,
4230                                     source_document_amt                         ,
4231                                     total_tax_amt                               ,
4232                                     party_id                                    ,
4233                                     party_type                                  ,
4234                                     party_site_id                               ,
4235                                     item_classification                         ,
4236                                     org_id                                      ,
4237                                     organization_id                             ,
4238                                     fin_year                                    ,
4239                                     threshold_slab_id                           ,
4240                                     created_by                                  ,
4241                                     creation_date                               ,
4242                                     last_updated_by                             ,
4243                                     last_update_date                            ,
4244                                     last_update_login                           ,
4245                                     settlement_id                               ,
4246                                     certificate_id
4247                                 )
4248                         VALUES  (
4249                                     p_trx_ref_id                               ,
4250                                     ln_regime_id                               ,
4251                                     p_transaction_id                           ,
4252                                     ln_source_ref_document_id                  ,
4253                                     p_source_ref_document_type                 ,
4254                                     p_app_from_document_id                     ,
4255                                     p_app_from_document_type                   ,
4256                                     p_app_to_document_id                       ,
4257                                     p_app_to_document_type                     ,
4258                                     p_parent_transaction_id                    ,
4259                                     lv_org_tan_no                              ,
4260                                     p_document_id                              ,
4261                                     p_document_line_id                         ,
4262                                     p_document_type                            ,
4263                                     p_document_date                            ,
4264                                     p_table_name                               ,
4265                                     p_line_amount                              ,
4266                                     p_document_amount                          ,
4267                                     NULL                                       ,
4268                                     p_party_id                                 ,
4269                                     jai_constants.party_type_customer          ,
4270                                     p_party_site_id                            ,
4271                                     p_item_classification                      ,
4272                                     p_org_id                                   ,
4273                                     p_organization_id                          ,
4274                                     ln_fin_year                                ,
4275                                     ln_threshold_slab_id                       ,
4276                                     ln_user_id                                 ,
4277                                     sysdate                                    ,
4278                                     ln_user_id                                 ,
4279                                     sysdate                                    ,
4280                                     ln_login_id                                ,
4281                                     NULL                                       ,
4282                                     NULL
4283                                 );
4284 
4285    /*commented by csahoo for bug# 6401388
4286    jai_cmn_debug_contexts_pkg.deregister (pn_reg_id => ln_reg_id);*/
4287   END insert_repository_references;
4288 
4289   procedure insert_repository_taxes (
4290                                      p_trx_ref_id                         JAI_RGM_REFS_ALL.TRX_REF_ID%TYPE                  ,
4291                                      p_tax_id                             JAI_RGM_TAXES.TAX_ID%TYPE                         ,
4292                                      p_tax_type                           JAI_RGM_TAXES.TAX_TYPE%TYPE                       ,
4293                                      p_tax_rate                           JAI_RGM_TAXES.TAX_RATE%TYPE                       ,
4294                                      p_tax_amount                         JAI_RGM_TAXES.TAX_AMT%TYPE                        ,
4295                                      p_func_tax_amount                    JAI_RGM_TAXES.FUNC_TAX_AMT%TYPE                   ,
4296                                      p_tax_modified_by                    JAI_RGM_TAXES.TAX_MODIFIED_BY%TYPE DEFAULT NULL   ,
4297                                      p_currency_code                      JAI_RGM_TAXES.CURRENCY_CODE%TYPE                  ,
4298                                      p_process_flag        OUT NOCOPY     VARCHAR2                                          ,
4299                                      p_process_message     OUT NOCOPY     VARCHAR2
4300                                     )
4301 
4302 
4303   IS
4304      ln_reg_id           NUMBER;
4305     /****
4306     || Get the orig_tax_percenatge for the tax_id
4307     || and determine the exemption flag value
4308     *****/
4309     CURSOR cur_get_exemption_value
4310     IS
4311     SELECT
4312           orig_tax_percentage
4313     FROM
4314           JAI_CMN_TAXES_ALL
4315     WHERE
4316           tax_id  = p_tax_id;
4317 
4318     ln_tax_det_id               JAI_RGM_TAXES.TAX_DET_ID%TYPE             ;
4319     lv_process_flag             VARCHAR2(2)                               ;
4320     lv_process_message          VARCHAR2(2000)                            ;
4321     ln_user_id                  JAI_RGM_TAXES.CREATED_BY%TYPE             ;
4322     ln_login_id                 JAI_RGM_TAXES.LAST_UPDATE_LOGIN%TYPE      ;
4323     ln_orig_tax_rate            JAI_CMN_TAXES_ALL.ORIG_TAX_PERCENTAGE%TYPE  ;
4324     ln_exempted_flag            JAI_RGM_TAXES.EXEMPTED_FLAG%TYPE          ;
4325     ln_tax_modified_by          JAI_RGM_TAXES.TAX_MODIFIED_BY%TYPE        ;
4326   BEGIN
4327 
4328 
4329 
4330 
4331 /***************************************************************************************************
4332 -- #
4333 -- # Change History -
4334 
4335 
4336 1.  01/02/2007   CSahoo for bug#5631784. File Version 120.0
4337                  Forward Porting of 11i BUG#4742259 (TAX COLLECTION AT SOURCE IN RECEIVABLES)
4338 
4339 2.  15/06/2007   brahtod, bug#6132484, File Version 120.1
4340                  Issue: ERROR WHILE TRYING TO APPLY  INVOICE TO CASH RECEIPT .
4341                  Fix:  cursor cur_get_app_to_det is changed in process_application to fetch bill_to_site_use_id
4342 
4343 3.  19/-6/2007   sacsethi , bug 6137956 File version 120.2
4344 
4345      Problem - In Auto Invoice master  program  , Transaction number created for TCS Type of receipt method was
4346                not coming
4347 
4348            Solution -In Table RA_CUSTOMER_TRX_LINES_ALL , Org_id column was null
4349 4.  10-Sep-2007  CSahoo for bug#6401388, File Version120.4.12000000.7
4350                   commented the code related to jai_cmn_debug_context_pkg package.
4351                   removed all the debug messages meant for debugging.
4352 
4353 5.  19-Feb-2009  CSahoo for bug#8214204, File Version 120.11.12010000.3
4354                  added the code to insert into the table ra_interface_salescredits_all
4355                  in the procedure generate_document.
4356 6.  20-Feb-2009  JMEENA for bug#8241099
4357       Modified cursor cur_chk_tcs_for_all_lines in procedure validate_invoice and added condition to filter discount lines while validating TCS taxes.
4358 
4359 7.  23-Feb-2009  CSahoo for bug#8214204, File Version 120.11.12010000.6
4360                  Reverted back the changes made for the bug in file version 120.11.12010000.3
4361 
4362 8.  11-Sep-2008  CSahoo for bug#7393380, File Version 120.11.12010000.7
4363                  ISSUE: TCS CREDIT MEMO IS GETTING DEBITED IN THE SETTLEMENT INSTEAD OF GETTING CREDITED
4364                  FIX:  modified the code in the procedure process_applications. Here the ln_apportion_factor
4365                        should be negative for receipt application
4366 9.  18-May-2009  CSahoo for bug#8517919, File Version 120.11.12010000.8
4367                  Modified the code in generate_document procedure. Replaced the Localization tax_code by NULL
4368                  in the insert statement.
4369 
4370 10.  15-APR-2010 vkaranam for bug#9587338
4371                  issue:
4372                  wrong journal source and category passed for TCS taxes.
4373                  fix:
4374                  changes are done in ar_accounting,wsh_interim_Accounting
4375 
4376                  source='Receivables India'
4377                  category ='India Tax Collected'.
4378 
4379 11.    28-Sep-2011 mmurtuza for bug#13025013
4380 				  Added parameter p_called_from in the call of procedure jai_rgm_thhold_proc_pkg.get_threshold_slab_id
4381 
4382 
4383 *******************************************************************************************************/
4384 
4385     /*################################################################################################################
4386     || Initialize the variables
4387     ################################################################################################################*/
4388     lv_member_name        := 'INSERT_REPOSITORY_TAXES';
4389     set_debug_context;
4390     /*commented by csahoo for bug# 6401388
4391    jai_cmn_debug_contexts_pkg.register ( pv_context => lv_context
4392                                , pn_reg_id  => ln_reg_id
4393                                );*/
4394 
4395   /*commented by csahoo for bug# 6401388
4396  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
4397                              pv_log_msg  =>  ' PARAMETERS VALUES PASSED TO INSERT_REPOSITORY_TAXES : - '||fnd_global.local_chr(10)
4398                                            ||', p_trx_ref_id       -> '||p_trx_ref_id      ||fnd_global.local_chr(10)
4399                                            ||', p_tax_id           -> '||p_tax_id          ||fnd_global.local_chr(10)
4400                                            ||', p_tax_type         -> '||p_tax_type        ||fnd_global.local_chr(10)
4401                                            ||', p_tax_rate         -> '||p_tax_rate        ||fnd_global.local_chr(10)
4402                                            ||', p_tax_amount       -> '||p_tax_amount      ||fnd_global.local_chr(10)
4403                                            ||', p_func_tax_amount  -> '||p_func_tax_amount ||fnd_global.local_chr(10)
4404                                            ||', p_tax_modified_by  -> '||p_tax_modified_by ||fnd_global.local_chr(10)
4405                                            ||', p_currency_code    -> '||p_currency_code   ||fnd_global.local_chr(10)
4406                                            ||', p_process_flag     -> '||p_process_flag    ||fnd_global.local_chr(10)
4407                                            ||', p_process_message  -> '||p_process_message ||fnd_global.local_chr(10)
4408                           );*/
4409 
4410 
4411     lv_process_flag    := jai_constants.successful   ;
4412     lv_process_message := null                       ;
4413 
4414     p_process_flag     := lv_process_flag            ;
4415     p_process_message  := lv_process_message         ;
4416 
4417     ln_user_id         := fnd_global.user_id         ;
4418     ln_login_id        := fnd_global.login_id        ;
4419 
4420     /*******
4421     || Get the tax_det_id - primary key for
4422     || the table jai_rgm_taxes
4423     *******/
4424     OPEN  cur_get_tax_det_id ;
4425     FETCH cur_get_tax_det_id INTO ln_tax_det_id;
4426     CLOSE cur_get_tax_det_id;
4427 
4428     /*################################################################################################################
4429     || DETERMINE THE VALUE FOR TAX EXEMPTION FLAG
4430     ################################################################################################################*/
4431 
4432     /*******
4433     || Get the orig_ta_rate of the tax_id
4434     || the table jai_rgm_taxes
4435     *******/
4436     OPEN  cur_get_exemption_value;
4437     FETCH cur_get_exemption_value INTO ln_orig_tax_rate;
4438     CLOSE cur_get_exemption_value;
4439 
4440     /********************************************************************************************
4441     || Determine the tax exemption flag - this value would classify a tax as being of
4442     || Standard Rate ('SR'), Lower Rate ('LR') or Zero Rate ('ZR').
4443     || TCS reports would group by this and query
4444     ||=====================================================================================
4445     ||                         || RATE  || ORIG_TAX_PERCENTAGE|| Exempted_flag ||
4446     ||                         ||-------||--------------------||---------------||
4447     ||          Standard Rate  ||   10  ||    10 or Null      || 'SR'          ||
4448     ||          Lower Rate     ||   5   ||    10              || 'LR'          ||
4449     ||          Zero Rate      ||   0   ||   Null or not null || 'ZR'          ||
4450     ||=====================================================================================
4451     **********************************************************************************************/
4452     IF ln_orig_tax_rate IS NULL        OR
4453        ln_orig_tax_rate  = p_tax_rate
4454     THEN
4455 
4456       ln_exempted_flag := jai_constants.tax_exmpt_flag_std_rate    ;
4457 
4458     ELSIF ln_orig_tax_rate > p_tax_rate THEN /*rchandan for bug#4742259*/
4459 
4460       ln_exempted_flag := jai_constants.tax_exmpt_flag_lower_rate  ;
4461 
4462     ELSIF p_tax_rate = 0 THEN
4463       ln_exempted_flag := jai_constants.tax_exmpt_flag_zero_rate   ;
4464     END IF;
4465 
4466 
4467 
4468    /*
4469    || Determine the Tax modified by flag flag in case it is null
4470    */
4471    ln_tax_modified_by := nvl(p_tax_modified_by ,jai_constants.tax_modified_by_system );
4472 
4473   /*commented by csahoo for bug# 6401388
4474  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
4475                              pv_log_msg  =>  ' VALUES insert into JAI_RGM_TAXES are : - '                 ||fnd_global.local_chr(10)
4476                                            ||', tax_det_id        -> '||ln_tax_det_id                     ||fnd_global.local_chr(10)
4477                                            ||', trx_ref_id        -> '||p_trx_ref_id                      ||fnd_global.local_chr(10)
4478                                            ||', tax_type          -> '||p_tax_type                        ||fnd_global.local_chr(10)
4479                                            ||', tax_amt           -> '||round(nvl(p_tax_amount,0))        ||fnd_global.local_chr(10)
4480                                            ||', tax_id            -> '||p_tax_id                          ||fnd_global.local_chr(10)
4481                                            ||', func_tax_amt      -> '||round(nvl(p_func_tax_amount,0))   ||fnd_global.local_chr(10)
4482                                            ||', currency_code     -> '||p_currency_code                   ||fnd_global.local_chr(10)
4483                                            ||', exempted_flag     -> '||ln_exempted_flag                  ||fnd_global.local_chr(10)
4484                                            ||', tax_modified_by   -> '||ln_tax_modified_by                ||fnd_global.local_chr(10)
4485                                            ||', created_by        -> '||ln_user_id                        ||fnd_global.local_chr(10)
4486                                            ||', creation_date     -> '||sysdate                           ||fnd_global.local_chr(10)
4487                                            ||', last_updated_by   -> '||ln_user_id                        ||fnd_global.local_chr(10)
4488                                            ||', last_update_date  -> '||sysdate                           ||fnd_global.local_chr(10)
4489                                            ||', last_update_login -> '||ln_login_id
4490                          );*/
4491 
4492 
4493      INSERT into jai_rgm_taxes (
4494                                   tax_det_id                        ,
4495                                   trx_ref_id                        ,
4496                                   tax_type                          ,
4497                                   tax_amt                           ,
4498                                   tax_id                            ,
4499                                   tax_rate                          ,
4500                                   func_tax_amt                      ,
4501                                   currency_code                     ,
4502                                   tax_modified_by                   ,
4503                                   exempted_flag                     ,
4504                                   created_by                        ,
4505                                   creation_date                     ,
4506                                   last_updated_by                   ,
4507                                   last_update_date                  ,
4508                                   last_update_login
4509                                 )
4510                          VALUES (
4511                                   ln_tax_det_id                     ,
4512                                   p_trx_ref_id                      ,
4513                                   p_tax_type                        ,
4514                                   round(nvl(p_tax_amount,0))        ,
4515                                   p_tax_id                          ,
4516                                   p_tax_rate                        ,
4517                                   round(nvl(p_func_tax_amount,0))   ,
4518                                   p_currency_code                   ,
4519                                   ln_tax_modified_by                ,
4520                                   ln_exempted_flag                  ,
4521                                   ln_user_id                        ,
4522                                   sysdate                           ,
4523                                   ln_user_id                        ,
4524                                   sysdate                           ,
4525                                   ln_login_id
4526                                );
4527 
4528    /*commented by csahoo for bug# 6401388
4529  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
4530                                pv_log_msg  =>  ' Insert successful'
4531                             );*/
4532      UPDATE
4533             jai_rgm_refs_all
4534      SET
4535             total_tax_amt  = nvl(total_tax_amt,0) + nvl(p_tax_amount,0)
4536      WHERE
4537             trx_ref_id = p_trx_ref_id;
4538 
4539    /*commented by csahoo for bug# 6401388
4540  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
4541                                pv_log_msg  =>  ' jai_rgm_refs_all.total_tax_amt successfully updated  '
4542                             );*/
4543   /*commented by csahoo for bug# 6401388
4544  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
4545                              pv_log_msg  =>  '**************** END OF INSERT_REPOSITORY_TAXES ****************'
4546                           );
4547     jai_cmn_debug_contexts_pkg.deregister (pn_reg_id => ln_reg_id);*/
4548   END insert_repository_taxes ;
4549 
4550 
4551 PROCEDURE copy_taxes_from_source  ( p_source_document_type    IN            JAI_RGM_REFS_ALL.SOURCE_DOCUMENT_TYPE%TYPE                    ,
4552                                     p_source_document_id      IN            JAI_RGM_REFS_ALL.SOURCE_DOCUMENT_ID%TYPE                      ,
4553                                     p_source_document_line_id IN            JAI_RGM_REFS_ALL.SOURCE_DOCUMENT_LINE_ID%TYPE  DEFAULT NULL   ,
4554                                     p_apportion_factor        IN            NUMBER                                         DEFAULT NULL   ,
4555                                     p_trx_ref_id              IN            JAI_RGM_REFS_ALL.TRX_REF_ID%TYPE                              ,
4556                                     p_process_flag            OUT NOCOPY    VARCHAR2                                                      ,
4557                                     p_process_message         OUT NOCOPY    VARCHAR2
4558                                   )
4559 IS
4560 
4561   ln_reg_id           NUMBER;
4562   /*****
4563   ||Get the Invoice tax details
4564   ******/
4565   CURSOR cur_get_inv_taxes ( cp_customer_trx_id      JAI_AR_TRX_LINES.CUSTOMER_TRX_ID%TYPE     ,
4566                              cp_customer_trx_line_id JAI_AR_TRX_LINES.CUSTOMER_TRX_LINE_ID%TYPE
4567                            )
4568   IS
4569   SELECT
4570          jrcttl.customer_trx_line_id                      ,
4571          jrcttl.tax_id                                    ,
4572          jrcttl.tax_rate                                  ,
4573          jtc.tax_type                                     ,
4574          jrcttl.tax_amount                                ,
4575          jrcttl.func_tax_amount                           ,
4576          jrct.invoice_currency_code       currency_code
4577   FROM
4578          JAI_AR_TRXS            jrct            ,
4579          JAI_AR_TRX_LINES      jrctl           ,
4580          JAI_AR_TRX_TAX_LINES      jrcttl          ,
4581          JAI_CMN_TAXES_ALL                  jtc             ,
4582          jai_regime_tax_types_v           jrttv
4583   WHERE
4584          jrct.customer_trx_id             = cp_customer_trx_id
4585   AND    jrct.customer_trx_id             = jrctl.customer_trx_id
4586   AND    jrctl.customer_trx_line_id       = jrcttl.link_to_cust_trx_line_id
4587   AND    jrctl.customer_trx_line_id       = nvl( cp_customer_trx_line_id , jrctl.customer_trx_line_id )
4588   AND    jrcttl.tax_id                    = jtc.tax_id
4589   AND    jrttv.tax_type                   = jtc.tax_type
4590   AND    jrttv.regime_code                = jai_constants.tcs_regime;
4591 
4592 
4593   /*****
4594   ||Get the receipt tax details
4595   *****/
4596   CURSOR cur_get_rcpt_taxes (cp_source_doc_id  jai_cmn_document_taxes.SOURCE_DOC_ID%TYPE )
4597   IS
4598   SELECT
4599          jdt.tax_id                      ,
4600          jdt.tax_rate                    ,
4601          jdt.tax_type                    ,
4602          jdt.tax_amt                     ,
4603          jdt.func_tax_amt                ,
4604          jdt.currency_code
4605   FROM
4606          jai_cmn_document_taxes     jdt  ,
4607          jai_regime_tax_types_v     jrttv
4608   WHERE
4609          jdt.tax_type      = jrttv.tax_type
4610   AND    jdt.source_doc_id = cp_source_doc_id
4611   AND    jrttv.regime_code = jai_constants.tcs_regime
4612   AND    jdt.source_doc_type = JAI_CONSTANTS.ar_cash;  --added by eric for a bug
4613 
4614   lv_process_flag           VARCHAR2(2)         ;
4615   lv_process_message        VARCHAR2(2000)      ;
4616   ln_apportion_factor       NUMBER              ;
4617 BEGIN
4618   /*########################################################################################################
4619   || VARIABLES INITIALIZATION
4620   ########################################################################################################*/
4621   lv_member_name        := 'COPY_TAXES_FROM_SOURCE';
4622   set_debug_context;
4623   /*commented by csahoo for bug# 6401388
4624   jai_cmn_debug_contexts_pkg.register ( pv_context => lv_context
4625                                , pn_reg_id  => ln_reg_id
4626                                );*/
4627 
4628   /*commented by csahoo for bug# 6401388
4629  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
4630                              pv_log_msg  =>  ' PARAMETERS VALUES PASSED TO COPY_TAXES_FROM_SOURCE : - '  ||fnd_global.local_chr(10)
4631                                            ||', p_source_document_type     -> '||p_source_document_type    ||fnd_global.local_chr(10)
4632                                            ||', p_source_document_id       -> '||p_source_document_id      ||fnd_global.local_chr(10)
4633                                            ||', p_source_document_line_id  -> '||p_source_document_line_id ||fnd_global.local_chr(10)
4634                                            ||', p_apportion_factor         -> '||p_apportion_factor        ||fnd_global.local_chr(10)
4635                                            ||', p_trx_ref_id               -> '||p_trx_ref_id              ||fnd_global.local_chr(10)
4636                           );*/
4637 
4638   lv_process_flag    := jai_constants.successful   ;
4639   lv_process_message := null                       ;
4640 
4641   p_process_flag     := lv_process_flag            ;
4642   p_process_message  := lv_process_message         ;
4643 
4644   ln_apportion_factor:= nvl(p_apportion_factor,1);
4645 
4646 
4647   /*########################################################################################################
4648   || Default taxes from Invoice
4649   ########################################################################################################*/
4650 
4651 
4652   IF p_source_document_type IN (                                                   --------------------------A1
4653                                  jai_constants.trx_type_inv_comp   ,   /* From  Invoice completion */
4654                                  jai_constants.trx_type_inv_incomp ,   /* From  Invoice Incompletion */
4655                                  jai_constants.ar_invoice_type_inv ,   /* From  Application */
4656                                  jai_constants.ar_doc_type_dm      ,  /* From  Application */
4657                                  jai_constants.ar_invoice_type_cm     /* From  Application */
4658                                )
4659   THEN
4660     /***
4661     || Source is INVOICE/DM/CM
4662     || Loop through each tax line of the invoice to hit
4663     || the TCS tax repository table jai_rgm_taxes
4664     ***/
4665 
4666    /*commented by csahoo for bug# 6401388
4667  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
4668                                pv_log_msg  =>  ' copy TAXES From JAI_AR_TRX_TAX_LINES - p_source_document_type -> '||p_source_document_type
4669                             );*/
4670     FOR rec_cur_get_inv_taxes IN cur_get_inv_taxes (  cp_customer_trx_id      => p_source_document_id          ,
4671                                                       cp_customer_trx_line_id => p_source_document_line_id
4672                                                    )
4673     LOOP
4674 
4675       insert_repository_taxes  (
4676                                   p_trx_ref_id          =>  p_trx_ref_id                                                  ,
4677                                   p_tax_id              =>  rec_cur_get_inv_taxes.tax_id                                  ,
4678                                   p_tax_type            =>  rec_cur_get_inv_taxes.tax_type                                ,
4679                                   p_tax_rate            =>  rec_cur_get_inv_taxes.tax_rate                                ,
4680                                   p_tax_amount          =>  rec_cur_get_inv_taxes.tax_amount      * ln_apportion_factor   ,
4681                                   p_func_tax_amount     =>  rec_cur_get_inv_taxes.func_tax_amount * ln_apportion_factor   ,
4682                                   p_currency_code       =>  rec_cur_get_inv_taxes.currency_code                           ,
4683                                   p_process_flag        =>  lv_process_flag                                               ,
4684                                   p_process_message     =>  lv_process_message
4685                                );
4686 
4687       IF lv_process_flag = jai_constants.expected_error    OR                      ---------A2
4688          lv_process_flag = jai_constants.unexpected_error  OR
4689          lv_process_flag = jai_constants.not_applicable
4690       THEN
4691         /*
4692         || As Returned status is an error/not applicable hence:-
4693         || Set out variables p_process_flag and p_process_message accordingly
4694         */
4695         --call to debug package
4696       /*commented by csahoo for bug# 6401388
4697  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
4698                                   pv_log_msg  =>  ' Error : - lv_process_flag -> '||lv_process_flag||' - '||lv_process_message
4699                                 );*/
4700 
4701         p_process_flag    := lv_process_flag    ;
4702         p_process_message := lv_process_message ;
4703         return;
4704       END IF;                                                                      ---------A2
4705     END LOOP;
4706 
4707   ELSIF p_source_document_type = jai_constants.ar_cash_tax_confirmed THEN
4708     /*
4709     ||Source is receipt
4710     */
4711    /*commented by csahoo for bug# 6401388
4712  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
4713                                pv_log_msg  =>  ' copy TAXES From jai_cmn_document_taxes - p_source_document_id -> '||p_source_document_id
4714                                               ||', p_source_document_type -> '||p_source_document_type
4715                             );*/
4716       FOR rec_cur_get_rcpt_taxes IN cur_get_rcpt_taxes (cp_source_doc_id  => p_source_document_id )
4717       LOOP
4718 
4719         insert_repository_taxes  (
4720                                     p_trx_ref_id          =>  p_trx_ref_id                                                  ,
4721                                     p_tax_id              =>  rec_cur_get_rcpt_taxes.tax_id                                 ,
4722                                     p_tax_type            =>  rec_cur_get_rcpt_taxes.tax_type                               ,
4723                                     p_tax_rate            =>  rec_cur_get_rcpt_taxes.tax_rate                               ,
4724                                     p_tax_amount          =>  rec_cur_get_rcpt_taxes.tax_amt      * ln_apportion_factor     ,
4725                                     p_func_tax_amount     =>  rec_cur_get_rcpt_taxes.func_tax_amt * ln_apportion_factor     ,
4726                                     p_currency_code       =>  rec_cur_get_rcpt_taxes.currency_code                          ,
4727                                     p_process_flag        =>  lv_process_flag                                               ,
4728                                     p_process_message     =>  lv_process_message
4729                                  );
4730 
4731         IF lv_process_flag = jai_constants.expected_error    OR                      ---------A3
4732            lv_process_flag = jai_constants.unexpected_error  OR
4733            lv_process_flag = jai_constants.not_applicable
4734         THEN
4735           /*
4736           || As Returned status is an error/not applicable hence:-
4737           || Set out variables p_process_flag and p_process_message accordingly
4738           */
4739           --call to debug package
4740         /*commented by csahoo for bug# 6401388
4741  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
4742                                     pv_log_msg  =>  ' Error : - lv_process_flag -> '||lv_process_flag||' - '||lv_process_message
4743                                   );*/
4744 
4745           p_process_flag    := lv_process_flag    ;
4746           p_process_message := lv_process_message ;
4747           return;
4748         END IF;                                                                      ---------A3
4749       END LOOP;
4750 
4751   END IF;                                                                                --------------------------A1
4752 
4753    /*commented by csahoo for bug# 6401388
4754  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
4755                                pv_log_msg  =>  '****************COPY_TAXES_FROM_SOURCE ENDS SUCCESSFULLY ****************'
4756                           );
4757     jai_cmn_debug_contexts_pkg.deregister (pn_reg_id => ln_reg_id);*/
4758 END copy_taxes_from_source;
4759 
4760 PROCEDURE copy_references (    p_parent_transaction_id   IN               JAI_RGM_REFS_ALL.PARENT_TRANSACTION_ID%TYPE DEFAULT NULL  ,
4761                                p_new_document_id         IN               JAI_RGM_REFS_ALL.SOURCE_DOCUMENT_ID%TYPE                  ,
4762                                p_new_document_type       IN               JAI_RGM_REFS_ALL.SOURCE_DOCUMENT_TYPE%TYPE                ,
4763                                p_new_document_date       IN               DATE                                                      ,
4764                                p_apportion_factor        IN               NUMBER                                     DEFAULT 1      ,
4765                                p_process_flag            OUT NOCOPY       VARCHAR2                                                  ,
4766                                p_process_message         OUT NOCOPY       VARCHAR2
4767                           )
4768   IS
4769 
4770   ln_reg_id           NUMBER;
4771   CURSOR cur_get_refs
4772   IS
4773   SELECT
4774          *
4775   FROM
4776         jai_rgm_refs_all
4777   WHERE
4778        transaction_id = p_parent_transaction_id;
4779 
4780   CURSOR cur_get_rgm_taxes ( cp_trx_ref_id JAI_RGM_TAXES.TRX_REF_ID%TYPE )
4781   IS
4782   SELECT
4783          *
4784   FROM
4785         jai_rgm_taxes
4786   WHERE
4787        trx_ref_id = cp_trx_ref_id;
4788 
4789   rec_cur_get_refs        CUR_GET_REFS%ROWTYPE                     ;
4790   ln_trx_ref_id           JAI_RGM_REFS_ALL.TRX_REF_ID%TYPE         ;
4791   ln_tax_det_id           JAI_RGM_TAXES.TAX_DET_ID%TYPE            ;
4792   ln_apportion_ratio      NUMBER                                   ;
4793   ln_user_id              JAI_RGM_REFS_ALL.CREATED_BY%TYPE         ;
4794   ln_login_id             JAI_RGM_REFS_ALL.LAST_UPDATE_LOGIN%TYPE  ;
4795   ln_regime_id            JAI_RGM_DEFINITIONS.REGIME_ID%TYPE               ;
4796   lv_org_tan_no           JAI_RGM_REGISTRATIONS.ATTRIBUTE_VALUE%TYPE       ;
4797   ln_threshold_slab_id    JAI_RGM_REFS_ALL.THRESHOLD_SLAB_ID%TYPE  ;
4798   lv_process_flag         VARCHAR2(2)                              ;
4799   lv_process_message      VARCHAR2(4000)                           ;
4800 BEGIN
4801 
4802     /*################################################################################################################
4803     || Initialize the variables
4804     ################################################################################################################*/
4805 
4806     lv_member_name        := 'COPY_REFERENCES';
4807     set_debug_context;
4808     /*commented by csahoo for bug# 6401388
4809    jai_cmn_debug_contexts_pkg.register ( pv_context => lv_context
4810                                  , pn_reg_id  => ln_reg_id
4811                                  );*/
4812 
4813    /*commented by csahoo for bug# 6401388
4814  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
4815                               pv_log_msg  =>  ' PARAMETERS VALUES PASSED TO COPY_REFERENCES : - '      ||fnd_global.local_chr(10)
4816                                             ||', p_parent_transaction_id  -> '||p_parent_transaction_id  ||fnd_global.local_chr(10)
4817                                             ||', p_new_document_id        -> '||p_new_document_id        ||fnd_global.local_chr(10)
4818                                             ||', p_new_document_type      -> '||p_new_document_type      ||fnd_global.local_chr(10)
4819                                             ||', p_new_document_date      -> '||p_new_document_date      ||fnd_global.local_chr(10)
4820                                             ||', p_apportion_factor       -> '||p_apportion_factor       ||fnd_global.local_chr(10)
4821                            );*/
4822 
4823     lv_process_flag    := jai_constants.successful   ;
4824     lv_process_message := null                       ;
4825 
4826     p_process_flag     := lv_process_flag            ;
4827     p_process_message  := lv_process_message         ;
4828 
4829     ln_user_id         := fnd_global.user_id         ;
4830     ln_login_id        := fnd_global.login_id        ;
4831 
4832 
4833     /*########################################################################################################
4834     || POPULATE JAI_RGM_REFS_ALL ---- PART -2
4835     ########################################################################################################*/
4836 
4837     /*
4838     ||Get the sequence generated unique key for the transaction
4839     */
4840     OPEN  cur_get_transaction_id ;
4841     FETCH cur_get_transaction_id INTO ln_transaction_id ;
4842     CLOSE cur_get_transaction_id ;
4843 
4844     FOR rec_cur_get_refs IN cur_get_refs
4845     LOOP
4846 
4847       /*
4848       ||Header needs to be copied from source
4849       */
4850       OPEN  cur_get_trx_ref_id ;
4851       FETCH cur_get_trx_ref_id INTO ln_trx_ref_id ;
4852       CLOSE cur_get_trx_ref_id ;
4853 
4854 
4855      /*commented by csahoo for bug# 6401388
4856  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
4857                                           pv_log_msg  =>  ' VALUES insert into JAI_RGM_REFS_ALL are : - '                                       ||fnd_global.local_chr(10)
4858                                                ||', regime_id                   -> '||rec_cur_get_refs.regime_id                                ||fnd_global.local_chr(10)
4859                                                ||', trx_ref_id                  -> '||ln_trx_ref_id                                             ||fnd_global.local_chr(10)
4860                                                ||', transaction_id              -> '||ln_transaction_id                                         ||fnd_global.local_chr(10)
4861                                                ||', parent_transaction_id       -> '||p_parent_transaction_id                                   ||fnd_global.local_chr(10)
4862                                                ||', org_tan_no                  -> '||rec_cur_get_refs.org_tan_no                               ||fnd_global.local_chr(10)
4863                                                ||', source_document_id          -> '||p_new_document_id                                         ||fnd_global.local_chr(10)
4864                                                ||', source_document_line_id     -> '||p_new_document_id                                         ||fnd_global.local_chr(10)
4865                                                ||', source_document_type        -> '||p_new_document_type                                       ||fnd_global.local_chr(10)
4866                                                ||', source_document_date        -> '||p_new_document_date                                       ||fnd_global.local_chr(10)
4867                                                ||', source_table_name           -> '||rec_cur_get_refs.source_table_name                        ||fnd_global.local_chr(10)
4868                                                ||', line_amt                    -> '||p_apportion_factor * rec_cur_get_refs.line_amt            ||fnd_global.local_chr(10)
4869                                                ||', source_document_amt         -> '||p_apportion_factor * rec_cur_get_refs.source_document_amt ||fnd_global.local_chr(10)
4870                                                ||', total_tax_amt               -> '||p_apportion_factor * rec_cur_get_refs.total_tax_amt       ||fnd_global.local_chr(10)
4871                                                ||', source_ref_document_id      -> '||rec_cur_get_refs.source_ref_document_id                   ||fnd_global.local_chr(10)
4872                                                ||', source_ref_document_type    -> '||rec_cur_get_refs.source_ref_document_type                 ||fnd_global.local_chr(10)
4873                                                ||', app_from_document_id        -> '||rec_cur_get_refs.app_from_document_id                     ||fnd_global.local_chr(10)
4874                                                ||', app_from_document_type      -> '||rec_cur_get_refs.app_from_document_type                   ||fnd_global.local_chr(10)
4875                                                ||', app_to_document_id          -> '||rec_cur_get_refs.app_to_document_id                       ||fnd_global.local_chr(10)
4876                                                ||', app_to_document_type        -> '||rec_cur_get_refs.app_to_document_type                     ||fnd_global.local_chr(10)
4877                                                ||', party_id                    -> '||rec_cur_get_refs.party_id                                 ||fnd_global.local_chr(10)
4878                                                ||', party_type                  -> '||rec_cur_get_refs.party_type                               ||fnd_global.local_chr(10)
4879                                                ||', party_site_id               -> '||rec_cur_get_refs.party_site_id                            ||fnd_global.local_chr(10)
4880                                                ||', item_classification         -> '||rec_cur_get_refs.item_classification                      ||fnd_global.local_chr(10)
4881                                                ||', org_id                      -> '||rec_cur_get_refs.org_id                                   ||fnd_global.local_chr(10)
4882                                                ||', organization_id             -> '||rec_cur_get_refs.organization_id                          ||fnd_global.local_chr(10)
4883                                                ||', fin_year                    -> '||rec_cur_get_refs.fin_year                                 ||fnd_global.local_chr(10)
4884                                                ||', threshold_slab_id           -> '||NULL                                                      ||fnd_global.local_chr(10)
4885                                                ||', created_by                  -> '||ln_user_id                                                ||fnd_global.local_chr(10)
4886                                                ||', creation_date               -> '||sysdate                                                   ||fnd_global.local_chr(10)
4887                                                ||', last_updated_by             -> '||ln_user_id                                                ||fnd_global.local_chr(10)
4888                                                ||', last_update_date            -> '||sysdate                                                   ||fnd_global.local_chr(10)
4889                                                ||', last_update_login           -> '||ln_login_id                                               ||fnd_global.local_chr(10)
4890                                                ||', settlement_id               -> '||NULL
4891                                                ||', certificate_id              -> '||NULL
4892                                        );*/
4893 
4894       INSERT into jai_rgm_refs_all (
4895                                        trx_ref_id                                                             ,
4896                                        regime_id                                                              ,
4897                                        transaction_id                                                         ,
4898                                        parent_transaction_id                                                  ,
4899                                        org_tan_no                                                             ,
4900                                        source_document_id                                                     ,
4901                                        source_document_line_id                                                ,
4902                                        source_document_type                                                   ,
4903                                        source_document_date                                                   ,
4904                                        source_table_name                                                      ,
4905                                        line_amt                                                               ,
4906                                        source_document_amt                                                    ,
4907                                        total_tax_amt                                                          ,
4908                                        source_ref_document_id                                                 ,
4909                                        source_ref_document_type                                               ,
4910                                        app_from_document_id                                                   ,
4911                                        app_from_document_type                                                 ,
4912                                        app_to_document_id                                                     ,
4913                                        app_to_document_type                                                   ,
4914                                        party_id                                                               ,
4915                                        party_type                                                             ,
4916                                        party_site_id                                                          ,
4917                                        item_classification                                                    ,
4918                                        org_id                                                                 ,
4919                                        organization_id                                                        ,
4920                                        fin_year                                                               ,
4921                                        threshold_slab_id                                                      ,
4922                                        created_by                                                             ,
4923                                        creation_date                                                          ,
4924                                        last_updated_by                                                        ,
4925                                        last_update_date                                                       ,
4926                                        last_update_login                                                      ,
4927                                        settlement_id                                                          ,
4928                                        certificate_id
4929                                     )
4930                             VALUES  (
4931                                        ln_trx_ref_id                                                          ,
4932                                        rec_cur_get_refs.regime_id                                             ,
4933                                        ln_transaction_id                                                      ,
4934                                        p_parent_transaction_id                                                ,
4935                                        rec_cur_get_refs.org_tan_no                                            ,
4936                                        p_new_document_id                                                      ,
4937                                        p_new_document_id                                                      ,
4938                                        p_new_document_type                                                    ,
4939                                        p_new_document_date                                                    ,
4940                                        rec_cur_get_refs.source_table_name                                     ,
4941                                        p_apportion_factor * rec_cur_get_refs.line_amt                         ,
4942                                        p_apportion_factor * rec_cur_get_refs.source_document_amt              ,
4943                                        p_apportion_factor * rec_cur_get_refs.total_tax_amt                    ,
4944                                        rec_cur_get_refs.source_ref_document_id                                ,
4945                                        rec_cur_get_refs.source_ref_document_type                              ,
4946                                        rec_cur_get_refs.app_from_document_id                                  ,
4947                                        rec_cur_get_refs.app_from_document_type                                ,
4948                                        rec_cur_get_refs.app_to_document_id                                    ,
4949                                        rec_cur_get_refs.app_to_document_type                                  ,
4950                                        rec_cur_get_refs.party_id                                              ,
4951                                        rec_cur_get_refs.party_type                                            ,
4952                                        rec_cur_get_refs.party_site_id                                         ,
4953                                        rec_cur_get_refs.item_classification                                   ,
4954                                        rec_cur_get_refs.org_id                                                ,
4955                                        rec_cur_get_refs.organization_id                                       ,
4956                                        rec_cur_get_refs.fin_year                                              ,
4957                                        NULL                                                                   ,
4958                                        ln_user_id                                                             ,
4959                                        sysdate                                                                ,
4960                                        ln_user_id                                                             ,
4961                                        sysdate                                                                ,
4962                                        ln_login_id                                                            ,
4963                                        NULL                                                                   ,
4964                                        NULL
4965                                     );
4966 
4967       /*commented by csahoo for bug# 6401388
4968  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
4969                                           pv_log_msg  =>  ' After Insert into jai_rgm_refs_all'
4970                                        );*/
4971 
4972       /*
4973       ||Taxes needs to be copied from source
4974       */
4975       FOR rec_cur_get_rgm_taxes IN cur_get_rgm_taxes ( cp_trx_ref_id => rec_cur_get_refs.trx_ref_id )
4976       LOOP
4977         /*******
4978         || Get the tax_det_id - primary key for
4979         || the table jai_rgm_taxes
4980         *******/
4981         OPEN  cur_get_tax_det_id ;
4982         FETCH cur_get_tax_det_id INTO ln_tax_det_id;
4983         CLOSE cur_get_tax_det_id;
4984 
4985      /*commented by csahoo for bug# 6401388
4986  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
4987                                  pv_log_msg  =>  ' VALUES insert into JAI_RGM_REFS_ALL are : - ' ||fnd_global.local_chr(10)
4988                                                ||', tax_det_id          -> '||ln_tax_det_id                                                    ||fnd_global.local_chr(10)
4989                                                ||', trx_ref_id          -> '||ln_trx_ref_id                                                    ||fnd_global.local_chr(10)
4990                                                ||', tax_type            -> '||rec_cur_get_rgm_taxes.tax_type                                   ||fnd_global.local_chr(10)
4991                                                ||', tax_amt             -> '||round( p_apportion_factor * rec_cur_get_rgm_taxes.tax_amt )      ||fnd_global.local_chr(10)
4992                                                ||', tax_id              -> '||rec_cur_get_rgm_taxes.tax_id                                     ||fnd_global.local_chr(10)
4993                                                ||', func_tax_amt        -> '||round(p_apportion_factor * rec_cur_get_rgm_taxes.func_tax_amt )  ||fnd_global.local_chr(10)
4994                                                ||', currency_code       -> '||rec_cur_get_rgm_taxes.currency_code                              ||fnd_global.local_chr(10)
4995                                                ||', exempted_flag       -> '||rec_cur_get_rgm_taxes.exempted_flag                              ||fnd_global.local_chr(10)
4996                                                ||', created_by          -> '||ln_user_id                                                       ||fnd_global.local_chr(10)
4997                                                ||', creation_date       -> '||sysdate                                                          ||fnd_global.local_chr(10)
4998                                                ||', last_updated_by     -> '||ln_user_id                                                       ||fnd_global.local_chr(10)
4999                                                ||', last_update_date    -> '||sysdate                                                          ||fnd_global.local_chr(10)
5000                                                ||', last_update_login   -> '||ln_user_id
5001                               );*/
5002 
5003         INSERT into jai_rgm_taxes  (
5004                                     tax_det_id                                                          ,
5005                                     trx_ref_id                                                          ,
5006                                     tax_type                                                            ,
5007                                     tax_amt                                                             ,
5008                                     tax_id                                                              ,
5009                                     tax_rate                                                            ,
5010                                     func_tax_amt                                                        ,
5011                                     currency_code                                                       ,
5012                                     tax_modified_by                                                     ,
5013                                     exempted_flag                                                       ,
5014                                     created_by                                                          ,
5015                                     creation_date                                                       ,
5016                                     last_updated_by                                                     ,
5017                                     last_update_date                                                    ,
5018                                     last_update_login
5019                                   )
5020                            VALUES (
5021                                     ln_tax_det_id                                                       ,
5022                                     ln_trx_ref_id                                                       ,
5023                                     rec_cur_get_rgm_taxes.tax_type                                      ,
5024                                     round( p_apportion_factor * rec_cur_get_rgm_taxes.tax_amt )         ,
5025                                     rec_cur_get_rgm_taxes.tax_id                                        ,
5026                                     rec_cur_get_rgm_taxes.tax_rate                                      ,
5027                                     round(p_apportion_factor * rec_cur_get_rgm_taxes.func_tax_amt )     ,
5028                                     rec_cur_get_rgm_taxes.currency_code                                 ,
5029                                     rec_cur_get_rgm_taxes.tax_modified_by                               ,
5030                                     rec_cur_get_rgm_taxes.exempted_flag                                 ,
5031                                     ln_user_id                                                          ,
5032                                     sysdate                                                             ,
5033                                     ln_user_id                                                          ,
5034                                     sysdate                                                             ,
5035                                     ln_user_id
5036                                   );
5037 
5038 
5039 
5040       /*commented by csahoo for bug# 6401388
5041  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
5042                                  pv_log_msg  =>  ' After Insert into jai_rgm_taxes '
5043                               );*/
5044       END LOOP; /* End of tax population */
5045     END LOOP; /*End of jai_rgm_refs_all population */
5046 
5047    /*commented by csahoo for bug# 6401388
5048  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
5049                              pv_log_msg  =>  '**************** END OF COPY_REFERENCES ****************'
5050                           );
5051 
5052     jai_cmn_debug_contexts_pkg.deregister (pn_reg_id => ln_reg_id);*/
5053   END copy_references;
5054 
5055 
5056 PROCEDURE   update_item_gen_docs  ( p_trx_number         IN  RA_CUSTOMER_TRX_ALL.TRX_NUMBER%TYPE      ,
5057                                     p_customer_trx_id    IN  RA_CUSTOMER_TRX_ALL.CUSTOMER_TRX_ID%TYPE ,
5058                                     p_complete_flag      IN  RA_CUSTOMER_TRX_ALL.COMPLETE_FLAG%TYPE   ,
5059                                     p_org_id             IN  RA_CUSTOMER_TRX_ALL.ORG_ID%TYPE          ,
5060                                     p_process_flag       OUT NOCOPY     VARCHAR2                      ,
5061                                     p_process_message    OUT NOCOPY     VARCHAR2
5062                                   )
5063 IS
5064   ln_reg_id                 NUMBER             ;
5065 
5066   CURSOR cur_upd_gen_docs
5067   IS
5068   SELECT
5069           jrigd.rowid            ,
5070           jrigd.generated_doc_id
5071   FROM
5072         jai_rgm_item_gen_docs jrigd,
5073         jai_rgm_refs_all      jrra
5074   WHERE
5075         jrigd.generated_doc_trx_number = p_trx_number
5076   AND   jrra.transaction_id            = jrigd.transaction_id
5077   AND   jrra.org_id                    = p_org_id
5078   FOR UPDATE OF jrigd.generated_doc_id NOWAIT;
5079 
5080   lv_rowid              ROWID                                       ;
5081   ln_generated_doc_id   JAI_RGM_ITEM_GEN_DOCS.GENERATED_DOC_ID%TYPE ;
5082   ln_user_id            JAI_RGM_REFS_ALL.CREATED_BY%TYPE            ;
5083   ln_login_id           JAI_RGM_REFS_ALL.LAST_UPDATE_LOGIN%TYPE     ;
5084 
5085 BEGIN
5086 
5087   /*################################################################################################################
5088   || Initialize the variables
5089   ################################################################################################################*/
5090 
5091   lv_member_name        := 'UPDATE_ITEM_GEN_DOCS';
5092   set_debug_context;
5093   /*commented by csahoo for bug# 6401388
5094   jai_cmn_debug_contexts_pkg.register ( pv_context => lv_context ,
5095                                         pn_reg_id  => ln_reg_id
5096                                       );
5097 
5098  jai_cmn_debug_contexts_pkg.print (  pn_reg_id =>  ln_reg_id ,
5099                                       pv_log_msg  =>  'VALUES PASSED TO UPDATE_ITEM_GEN_DOCS ARE :- ' ||fnd_global.local_chr(10)
5100                                                     ||', p_trx_number      -> '||p_trx_number         ||fnd_global.local_chr(10)
5101                                                     ||', p_customer_trx_id -> '||p_customer_trx_id    ||fnd_global.local_chr(10)
5102                                                     ||', p_complete_flag   -> '||p_complete_flag      ||fnd_global.local_chr(10)
5103                                                     ||', p_org_id          -> '||p_org_id
5104                                    );*/
5105 
5106   p_process_flag    := jai_constants.successful   ;
5107   p_process_message := null                       ;
5108 
5109   ln_user_id         := fnd_global.user_id        ;
5110   ln_login_id        := fnd_global.login_id       ;
5111 
5112   /*################################################################################################################
5113   || UPDATE THE TABLE JAI_RGM_ITEM_GEN_DOCS
5114   ################################################################################################################*/
5115 
5116   OPEN  cur_upd_gen_docs ;
5117   FETCH cur_upd_gen_docs INTO lv_rowid, ln_generated_doc_id;
5118 
5119 
5120   IF cur_upd_gen_docs%FOUND THEN
5121     IF p_complete_flag = jai_constants.yes THEN
5122       /*
5123       || Complete flag is 'Y', Invoice is getting COMPLETED
5124       || Set the generated_doc_id to null in case it is not null
5125       */
5126 
5127       ln_generated_doc_id := p_customer_trx_id;
5128     ELSE
5129       /*
5130       || Complete flag is 'N', Invoice is getting INCOMPLETED
5131       */
5132       IF ln_generated_doc_id IS NOT NULL THEN
5133         /*
5134         || Set the generated_doc_id to null in case it is not null
5135         */
5136         ln_generated_doc_id := NULL;
5137       ELSE
5138         /*
5139         ||Do nothing if the generated_doc_id is null and invoice is getting incompleted
5140         */
5141        /*commented by csahoo for bug# 6401388
5142  jai_cmn_debug_contexts_pkg.print (  pn_reg_id =>  ln_reg_id ,
5143                                             pv_log_msg  =>  'Skip the TCS ITEM GENDOCS update as :- '            ||fnd_global.local_chr(10)
5144                                                           ||', p_complete_flag       -> '||p_complete_flag       ||fnd_global.local_chr(10)
5145                                                           ||', ln_generated_doc_id   -> '||ln_generated_doc_id
5146                                          );*/
5147         p_process_flag := jai_constants.not_applicable;
5148         return;
5149       END IF;
5150     END IF;
5151 
5152     /*commented by csahoo for bug# 6401388
5153  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
5154                                         pv_log_msg  =>  'valid transaction record found in table jai_rgm_item_gen_docs. Updating the table jai_rgm_item_gen_docs with ' ||fnd_global.local_chr(10)
5155                                                       ||', generated_doc_id -> '||ln_generated_doc_id
5156                                      );*/
5157     UPDATE jai_rgm_item_gen_docs
5158     SET
5159         generated_doc_id  = ln_generated_doc_id ,
5160         last_updated_by   = ln_user_id          ,
5161         last_update_date  = sysdate             ,
5162         last_update_login = ln_login_id
5163     WHERE
5164         rowid = lv_rowid;
5165   END IF;
5166   CLOSE cur_upd_gen_docs;
5167 
5168  /*commented by csahoo for bug# 6401388
5169  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
5170                                       pv_log_msg  =>  '**************** END OF UPDATE_ITEM_GEN_DOCS ****************'
5171                                    );
5172 
5173   jai_cmn_debug_contexts_pkg.deregister (pn_reg_id => ln_reg_id);*/
5174 
5175 END update_item_gen_docs;
5176 
5177 
5178 PROCEDURE generate_document (
5179                                 p_rgm_ref            IN             JAI_RGM_REFS_ALL%ROWTYPE ,
5180                                 p_total_tax_amt      IN             NUMBER                   ,
5181                                 p_process_flag       OUT NOCOPY     VARCHAR2                 ,
5182                                 p_process_message    OUT NOCOPY     VARCHAR2
5183                             )
5184 IS
5185     ln_reg_id                 NUMBER             ;
5186     ln_msg_count              NUMBER             ;
5187     lv_msg_data               VARCHAR2(2000)     ;
5188     ln_customer_trx_id        NUMBER             ;
5189     lv_return_status          VARCHAR2(80)       ;
5190 
5191   /*
5192   ||Customer would create a batch source with name TCS Debit Memo And TCS Credit Memo
5193   */
5194   CURSOR cur_get_batch_source ( cp_org_id  JAI_RGM_REFS_ALL.ORG_ID%TYPE   ,
5195                                 cp_name    RA_BATCH_SOURCES_ALL.NAME%TYPE
5196                                )
5197   IS
5198   SELECT
5199          bsa.batch_source_id          ,
5200          bsa.default_inv_trx_type     ,
5201          rctt.type                    ,
5202          rctt.name                    ,
5203          rctt.default_term            ,
5204          rctt.gl_id_rec               ,
5205          rctt.creation_sign
5206   FROM
5207          ra_batch_sources_all   bsa ,
5208          ra_cust_trx_types_all  rctt
5209   WHERE
5210          bsa.default_inv_trx_type = rctt.cust_trx_type_id
5211   AND    bsa.org_id               = rctt.org_id
5212   AND    bsa.org_id               = cp_org_id
5213   AND    bsa.name                 = cp_name  ;
5214 
5215   CURSOR cur_get_part_det ( cp_party_id         JAI_RGM_REFS_ALL.PARTY_ID%TYPE      ,
5216                             cp_party_site_id    JAI_RGM_REFS_ALL.PARTY_SITE_ID%TYPE
5217                           )
5218   IS
5219   SELECT
5220           hzcas.cust_acct_site_id   bill_to_address_id
5221   FROM
5222           hz_cust_accounts hca         ,
5223           hz_cust_acct_sites_all hzcas ,
5224           hz_cust_site_uses_all  hzcsu
5225   WHERE
5226           hca.cust_account_id       = hzcas.cust_account_id
5227   AND     hzcas.cust_acct_site_id   = hzcsu.cust_acct_site_id
5228   AND     hzcsu.site_use_code       = jai_constants.site_use_bill_to
5229   AND     hca.cust_account_id       = cp_party_id
5230   AND     hzcsu.site_use_id         = cp_party_site_id ;-- site_use_id is the party_site_id ;
5231 
5232   CURSOR cur_get_sob ( cp_org_id jai_rgm_refs_all.org_id%TYPE )
5233   IS
5234   SELECT
5235         set_of_books_id
5236   FROM
5237         hr_operating_units
5238   WHERE
5239         organization_id  = cp_org_id                                              ;
5240 
5241   /*--added for bug#8214204,start
5242   CURSOR cur_get_salesrep_req_flag (cp_org_id jai_rgm_refs_all.org_id%TYPE )
5243   IS
5244     SELECT  salesrep_required_flag
5245     from    ar_system_parameters_all
5246     where   org_id = cp_org_id;
5247   lv_salesrep_flag              VARCHAR2(1);
5248   --bug#8214204,end*/
5249 
5250   lv_batch_src_dm               JAI_RGM_REGISTRATIONS.ATTRIBUTE_VALUE%TYPE                ;
5251   lv_batch_src_cm               JAI_RGM_REGISTRATIONS.ATTRIBUTE_VALUE%TYPE                ;
5252   ln_regime_id                  JAI_RGM_DEFINITIONS.REGIME_ID%TYPE                        ;
5253   lv_batch_src_name             JAI_RGM_REGISTRATIONS.ATTRIBUTE_VALUE%TYPE                ;
5254   ln_term_id                    RA_CUST_TRX_TYPES_ALL.DEFAULT_TERM%TYPE           ;
5255   ln_bill_to_address_id         hz_cust_acct_sites_all.cust_acct_site_id%type     ;
5256   lv_trx_number                 RA_CUSTOMER_TRX_ALL.TRX_NUMBER%TYPE               ;
5257   ln_ccid_tax_type_tcs          NUMBER                                            ;
5258   rec_cur_get_batch_source      CUR_GET_BATCH_SOURCE%ROWTYPE                      ;
5259   lv_set_of_books_id            HR_OPERATING_UNITS.SET_OF_BOOKS_ID%TYPE           ;
5260   ln_amount                     NUMBER                                            ;
5261   ln_user_id                    JAI_RGM_REFS_ALL.CREATED_BY%TYPE                  ;
5262   ln_login_id                   JAI_RGM_REFS_ALL.LAST_UPDATE_LOGIN%TYPE           ;
5263   lv_process_message            VARCHAR2(4000)                                    ;
5264 
5265 BEGIN
5266 
5267   /*################################################################################################################
5268   || Initialize the variables
5269   ################################################################################################################*/
5270 
5271   lv_member_name        := 'GENERATE_DOCUMENT';
5272   set_debug_context;
5273   /*commented by csahoo for bug# 6401388
5274   jai_cmn_debug_contexts_pkg.register ( pv_context => lv_context    ,
5275                                         pn_reg_id  => ln_reg_id
5276                                        );
5277 
5278    jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
5279                                       pv_log_msg  =>  'Parameter passed to GENERATE_DOCUMENT are -> '                 ||fnd_global.local_chr(10)
5280                                                     ||', transaction_id          -> '||p_rgm_ref.transaction_id       ||fnd_global.local_chr(10)
5281                                                     ||', p_total_tax_amt         -> '||p_total_tax_amt                ||fnd_global.local_chr(10)
5282                                                     ||', source_document_type is -> '||p_rgm_ref.source_document_type ||fnd_global.local_chr(10)
5283                                                     ||', org_id                  -> '||p_rgm_ref.org_id
5284                                   );*/
5285   lv_process_message:= null                       ;
5286   p_process_flag    := jai_constants.successful   ;
5287   p_process_message := lv_process_message         ;
5288 
5289   ln_user_id         := fnd_global.user_id         ;
5290   ln_login_id        := fnd_global.login_id        ;
5291 
5292   /*################################################################################################################
5293   ||Skip the transaction if p_total_tax_amt is 0
5294   ################################################################################################################*/
5295   IF nvl(p_total_tax_amt,0) = 0 THEN
5296     /*commented by csahoo for bug# 6401388
5297  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
5298                                         pv_log_msg  =>  'As transaction total_tax_amt is -> '||p_total_tax_amt||' hence skipping the transaction.'
5299                                       );*/
5300     p_process_flag := jai_constants.not_applicable ;
5301     return;
5302   END IF;
5303 
5304   /*################################################################################################################
5305   ||Get batch source information
5306   ################################################################################################################*/
5307 
5308   /*commented by csahoo for bug# 6401388
5309  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
5310                                       pv_log_msg  =>  ' Deriving the batch source information '
5311                                    );*/
5312 
5313    /*
5314    ||Get the value for the Batch Source Debit Memo
5315    */
5316    OPEN c_get_rgm_attribute (  cp_regime_code           =>   jai_constants.tcs_regime                    ,
5317                                cp_attribute_code        =>   jai_constants.batch_src_dm                  ,
5318                                cp_organization_id       =>   p_rgm_ref.organization_id
5319                             );
5320   FETCH c_get_rgm_attribute INTO ln_regime_id ,lv_batch_src_dm;
5321   CLOSE c_get_rgm_attribute ;
5322 
5323 
5324   /*
5325   ||Get the value for the Batch Source Credit Memo
5326   */
5327   OPEN c_get_rgm_attribute (  cp_regime_code           =>   jai_constants.tcs_regime                    ,
5328                               cp_attribute_code        =>   jai_constants.batch_src_cm                  ,
5329                               cp_organization_id       =>   p_rgm_ref.organization_id
5330                            );
5331   FETCH c_get_rgm_attribute INTO ln_regime_id ,lv_batch_src_cm;
5332   CLOSE c_get_rgm_attribute ;
5333 
5334 
5335   /*################################################################################################################
5336   || Derive the batch source name based on the document type
5337   ################################################################################################################*/
5338 
5339   /*commented by csahoo for bug# 6401388
5340  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
5341                                       pv_log_msg  =>  ' Deriving the batch source name based on the document type'
5342                                    );*/
5343 
5344   IF p_rgm_ref.source_document_type  in  ( jai_constants.ar_cash_tax_confirmed , /* Receipt confirmation */
5345                                            jai_constants.trx_type_rct_unapp    , /* Receipt unapplication*/
5346                                            jai_constants.trx_type_cm_app         /* CM application*/
5347                                           )
5348   THEN
5349     lv_batch_src_name           := lv_batch_src_dm; /* TCS Debit Memo */
5350     lv_trx_number               := jai_constants.tcs_dm_prefix;   --'TCS-DM'                     ;
5351 
5352   ELSIF p_rgm_ref.source_document_type in ( jai_constants.trx_type_rct_app ,
5353                                             jai_constants.trx_type_rct_rvs   ,
5354                                             jai_constants.trx_type_cm_unapp
5355                                            )
5356 
5357   THEN
5358     lv_batch_src_name := lv_batch_src_cm; /* TCS Credit Memo */
5359     lv_trx_number     := jai_constants.tcs_cm_prefix; --'TCS-CM';
5360 
5361   ELSIF p_rgm_ref.source_document_type = jai_constants.tcs_event_surcharge THEN
5362     /*
5363     ||Document generation is invoked by surcharge.  Document type will be derrived from the sign of the document amount.
5364     ||If sign is +VE then it should be a Debit Memo, otherwise it should be a Credit Memo
5365     */
5366 
5367     IF sign (p_total_tax_amt) = -1 THEN
5368 
5369       /* Credit Memo */
5370       lv_batch_src_name := lv_batch_src_cm;
5371       lv_trx_number     := jai_constants.tcs_cm_prefix; --'TCS-CM';
5372     ELSIF sign (p_total_tax_amt) = 1 THEN
5373 
5374       /* Debit Memo */
5375       lv_batch_src_name := lv_batch_src_dm;
5376       lv_trx_number     := jai_constants.tcs_dm_prefix; --'TCS-DM'
5377     END IF;
5378   ELSE
5379     /*
5380     ||Skip the transaction
5381     */
5382     /*commented by csahoo for bug# 6401388
5383  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
5384                                         pv_log_msg  =>  'Skip the transaction '
5385                                   );*/
5386     p_process_flag    := jai_constants.not_applicable;
5387     p_process_message := null;
5388     return ;
5389   END IF;
5390 
5391   /*################################################################################################################
5392   ||VALIDATE BATCH SOURCES FOR TCS
5393   ################################################################################################################*/
5394 
5395   /*
5396   || Error out if the batch source name is null i.e regime party setup for
5397   */
5398   IF lv_batch_src_name IS NULL THEN
5399     /*commented by csahoo for bug# 6401388
5400  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
5401                                         pv_log_msg  =>  'Batch source definition has not be defined for the inventory organization '||p_rgm_ref.organization_id
5402                                       );*/
5403 
5404     p_process_flag    := jai_constants.expected_error;
5405     lv_process_message := 'Batch source definition has not be defined for the inventory organization '||p_rgm_ref.organization_id;
5406     p_process_message := lv_process_message ;
5407     return;
5408   END IF;
5409 
5410 
5411   /*commented by csahoo for bug# 6401388
5412  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
5413                                       pv_log_msg  =>  'Document Type is lv_batch_src_name -> '||lv_batch_src_name
5414                                    );*/
5415 
5416   OPEN cur_get_batch_source  (  cp_org_id  => p_rgm_ref.org_id ,
5417                                 cp_name    => lv_batch_src_name
5418                              );
5419 
5420   FETCH cur_get_batch_source INTO rec_cur_get_batch_source;
5421   IF cur_get_batch_source%NOTFOUND THEN
5422     /*commented by csahoo for bug# 6401388
5423  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
5424                                         pv_log_msg  =>  'TCS batch source has not been defined '
5425                                      );*/
5426     CLOSE cur_get_batch_source  ;
5427     p_process_flag    := jai_constants.expected_error;
5428     lv_process_message := 'TCS batch source has not been defined for '||lv_batch_src_name ||'. Cannot process transaction ';
5429     p_process_message := lv_process_message ;
5430     return;
5431   END IF;
5432   CLOSE cur_get_batch_source  ;
5433 
5434   /*commented by csahoo for bug# 6401388
5435  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
5436                                       pv_log_msg  =>  'batch source details are:- '                                                          ||fnd_global.local_chr(10)
5437                                       ||', lv_batch_src_name                             -> '||lv_batch_src_name                             ||fnd_global.local_chr(10)
5438                                       ||', rec_cur_get_batch_source.batch_source_id      -> '||rec_cur_get_batch_source.batch_source_id      ||fnd_global.local_chr(10)
5439                                       ||', rec_cur_get_batch_source.default_inv_trx_type -> '||rec_cur_get_batch_source.default_inv_trx_type ||fnd_global.local_chr(10)
5440                                       ||', rec_cur_get_batch_source.type                 -> '||rec_cur_get_batch_source.type                 ||fnd_global.local_chr(10)
5441                                       ||', rec_cur_get_batch_source.name                 -> '||rec_cur_get_batch_source.name                 ||fnd_global.local_chr(10)
5442                                       ||', rec_cur_get_batch_source.creation_sign        -> '||rec_cur_get_batch_source.creation_sign
5443                                    );*/
5444   /*################################################################################################################
5445   || DERIVE THE TERM FOR DM'S ONLY
5446   ################################################################################################################*/
5447 
5448   IF rec_cur_get_batch_source.type = jai_constants.ar_doc_type_dm THEN
5449     ln_term_id := rec_cur_get_batch_source.default_term;
5450     /*
5451     || Throw an error if the term has not been defined for the debit memo Transaction type .
5452     || This check is not required in case of credit memo
5453     */
5454     IF ln_term_id        IS NULL  THEN
5455       /*commented by csahoo for bug# 6401388
5456  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
5457                                           pv_log_msg  =>  'Error:- Default term is not defined in the transction type -> '||rec_cur_get_batch_source.name
5458                                        );*/
5459       p_process_flag    := jai_constants.expected_error;
5460       lv_process_message := 'Cannot process transaction. A default term needs to be defined FOR the Transaction TYPE '||rec_cur_get_batch_source.name ;
5461       p_process_message := lv_process_message ;
5462       return;
5463     END IF;
5464   END IF;
5465 
5466   /*################################################################################################################
5467   || DERIVE THE SOB
5468   ################################################################################################################*/
5469 
5470   OPEN  cur_get_sob ( cp_org_id => p_rgm_ref.org_id );
5471   FETCH cur_get_sob INTO lv_set_of_books_id;
5472   CLOSE cur_get_sob ;
5473   /*
5474   ||Throw an error if the Set of books has not been defined
5475   */
5476   IF lv_set_of_books_id IS NULL THEN
5477    /*commented by csahoo for bug# 6401388
5478  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
5479                                         pv_log_msg  =>  'Error:- Set of books not defined for org_id -> '||p_rgm_ref.org_id
5480                                      );*/
5481 
5482     p_process_flag     := jai_constants.expected_error;
5483     lv_process_message := 'Set of books not defined for the org id.';
5484     p_process_message  := lv_process_message ;
5485     return;
5486   END IF;
5487 
5488 
5489   /*################################################################################################################
5490   || DERIVE THE ADDRESS
5491   ################################################################################################################*/
5492 
5493   OPEN  cur_get_part_det ( cp_party_id      =>   p_rgm_ref.party_id              ,
5494                            cp_party_site_id =>   p_rgm_ref.party_site_id
5495                            );
5496   FETCH cur_get_part_det INTO ln_bill_to_address_id;
5497   CLOSE cur_get_part_det ;
5498   /*
5499   ||Throw an error if the bill to address has not been defined
5500   */
5501 
5502   IF ln_bill_to_address_id IS NULL THEN
5503 
5504    /*commented by csahoo for bug# 6401388
5505  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
5506                                         pv_log_msg  =>  'Error:- Bill to address not defined for the customer id -> '||p_rgm_ref.party_id
5507                                                      ||' customer site -> '||p_rgm_ref.party_site_id
5508                                      );*/
5509 
5510     p_process_flag     := jai_constants.expected_error;
5511     lv_process_message := 'Bill to address not defined for the customer id -> '||p_rgm_ref.party_id ||' customer site -> '||p_rgm_ref.party_site_id;
5512     p_process_message  := lv_process_message ;
5513     return;
5514   END IF;
5515 
5516   lv_trx_number := lv_trx_number||p_rgm_ref.transaction_id ;
5517 
5518   /*################################################################################################################
5519   || DERIVE THE SIGN OF THE APPLICATION AND SECONDARY DOCUMENT VALUE
5520   ################################################################################################################*/
5521 
5522   /*
5523   ||Amount is :-
5524   ||  1.+ve if the creation sign of the document is positive
5525   ||  1.-ve if the creation sign of the document is -ve
5526   || If the sign is any sign then for a DM create a +ve amount and CM would ve created with a -ve amount
5527   */
5528   IF rec_cur_get_batch_source.creation_sign = jai_constants.creation_sign_positive THEN
5529     ln_amount := abs(p_total_tax_amt) * 1;
5530 
5531   ELSIF rec_cur_get_batch_source.creation_sign = jai_constants.creation_sign_negative    THEN
5532       ln_amount := abs(p_total_tax_amt) * -1;
5533 
5534   ELSIF rec_cur_get_batch_source.creation_sign = jai_constants.creation_sign_any THEN
5535 
5536     IF rec_cur_get_batch_source.type = jai_constants.ar_doc_type_dm THEN
5537       ln_amount := abs(p_total_tax_amt) ;
5538 
5539     ELSIF rec_cur_get_batch_source.type = jai_constants.ar_invoice_type_cm THEN
5540       ln_amount := abs(p_total_tax_amt) * -1;
5541     END IF;
5542   END IF;
5543 
5544 
5545   /*################################################################################################################
5546   || INSERT INTO RA_INTERFACE_LINES_ALL TABLE
5547   ################################################################################################################*/
5548 
5549  /*commented by csahoo for bug# 6401388
5550  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
5551                                       pv_log_msg  =>  'Before inserting into the interface tables, Values are :- '                     || fnd_global.local_chr(10)
5552                                                     ||', interface_line_id             -> '||  p_rgm_ref.transaction_id                || fnd_global.local_chr(10)
5553                                                     ||', i/p tax amount                -> '||  p_total_tax_amt                         || fnd_global.local_chr(10)
5554                                                     ||', document creation amount      -> '||  ln_amount                               || fnd_global.local_chr(10)
5555                                                     ||', description                   -> '||  lv_batch_src_name                       || fnd_global.local_chr(10)
5556                                                     ||', orig_system_bill_customer_id  -> '||  p_rgm_ref.party_id                      || fnd_global.local_chr(10)
5557                                                     ||', orig_system_bill_address_id   -> '||  ln_bill_to_address_id                   || fnd_global.local_chr(10)
5558                                                     ||', set_of_books_id               -> '||  lv_set_of_books_id                      || fnd_global.local_chr(10)
5559                                                     ||', trx_date                      -> '||  p_rgm_ref.source_document_date          || fnd_global.local_chr(10)
5560                                                     ||', trx_number                    -> '||  lv_trx_number                           || fnd_global.local_chr(10)
5561                                                     ||', batch_source_name             -> '||  lv_batch_src_name                       || fnd_global.local_chr(10)
5562                                                     ||', cust_trx_type_name            -> '||  rec_cur_get_batch_source.name           || fnd_global.local_chr(10)
5563                                                     ||', line_type                     -> '||  jai_constants.line_type_line            || fnd_global.local_chr(10)
5564                                                     ||', conversion_rate               -> '||  1                                       || fnd_global.local_chr(10)
5565                                                     ||', conversion_type               -> '||  jai_constants.conversion_type_user      || fnd_global.local_chr(10)
5566                                                     ||', interface_line_context        -> '||  lv_batch_src_name                       || fnd_global.local_chr(10)
5567                                                     ||', interface_line_attribute2     -> '||  p_rgm_ref.transaction_id                || fnd_global.local_chr(10)
5568                                                     ||', currency_code                 -> '||  jai_constants.func_curr                 || fnd_global.local_chr(10)
5569                                                     ||', primary_salesrep_id           -> '||  -3                                      || fnd_global.local_chr(10)
5570                                                     ||', tax_code                      -> '||  jai_constants.tax_code_localization     || fnd_global.local_chr(10)
5571                                                     ||', term_id                       -> '||  ln_term_id                              || fnd_global.local_chr(10)
5572                                                     ||', warehouse_id                  -> '||  p_rgm_ref.organization_id               || fnd_global.local_chr(10)
5573                                                     ||', quantity                      -> '||  1                                       || fnd_global.local_chr(10)
5574                                                     ||', unit_selling_price            -> '||  ln_amount                               || fnd_global.local_chr(10)
5575                                                     ||', created_by                    -> '||  ln_user_id                              || fnd_global.local_chr(10)
5576                                                     ||', creation_date                 -> '||  sysdate                                 || fnd_global.local_chr(10)
5577                                                     ||', last_updated_by               -> '||  ln_user_id                              || fnd_global.local_chr(10)
5578                                                     ||', last_update_date              -> '||  sysdate                                 || fnd_global.local_chr(10)
5579                                                     ||', last_update_login             -> '||  ln_login_id                             || fnd_global.local_chr(10)
5580                            );*/
5581   INSERT INTO ra_interface_lines_all
5582                   (
5583                       interface_line_id                         ,
5584                       amount                                    ,
5585                       description                               ,
5586                       orig_system_bill_customer_id              ,
5587                       orig_system_bill_address_id               ,
5588                       set_of_books_id                           ,
5589                       trx_date                                  ,
5590                       trx_number                                ,
5591                       batch_source_name                         ,
5592                       cust_trx_type_name                        ,
5593                       line_type                                 ,
5594                       conversion_rate                           ,
5595                       conversion_type                           ,
5596                       interface_line_context                    ,
5597                       interface_line_attribute2                 ,
5598                       currency_code                             ,
5599                       primary_salesrep_id                       ,
5600                       tax_code                                  ,
5601                       term_id                                   ,
5602                       warehouse_id                              ,
5603                       org_id                              ,        -- Date 19-jun-2007 by sacsethi for bug 6137956
5604                       quantity                                  ,
5605                       unit_selling_price                        ,
5606                       created_by                                ,
5607                       creation_date                             ,
5608                       last_updated_by                           ,
5609                       last_update_date                          ,
5610                       last_update_login
5611                   )
5612           VALUES  (
5613                       p_rgm_ref.transaction_id                  ,
5614                       ln_amount                                 ,
5615                       lv_batch_src_name                         ,
5616                       p_rgm_ref.party_id                        ,
5617                       ln_bill_to_address_id                     ,
5618                       lv_set_of_books_id                        ,
5619                       p_rgm_ref.source_document_date            ,
5620                       lv_trx_number                             ,
5621                       lv_batch_src_name                         ,
5622                       rec_cur_get_batch_source.name             ,
5623                       jai_constants.line_type_line              ,
5624                       1                                         ,
5625                       jai_constants.conversion_type_user        ,
5626                       lv_batch_src_name                         ,
5627                       p_rgm_ref.transaction_id                  ,
5628                       jai_constants.func_curr                   ,
5629                       -3                                        ,
5630                       --jai_constants.tax_code_localization       ,
5631                       --commented the above and added the following for bug#8517919
5632                       NULL                                      ,
5633                       ln_term_id                                ,
5634                       p_rgm_ref.organization_id                 ,
5635                       p_rgm_ref.org_id                          , -- Date 19-jun-2007 by sacsethi for bug 6137956
5636                       1                                         ,
5637                       ln_amount                                 ,
5638                       ln_user_id                                ,
5639                       sysdate                                   ,
5640                       ln_user_id                                ,
5641                       sysdate                                   ,
5642                       ln_login_id
5643                    );
5644 
5645   /*--added for bug#8214204 , start
5646   OPEN cur_get_salesrep_req_flag (p_rgm_ref.org_id);
5647   FETCH cur_get_salesrep_req_flag INTO lv_salesrep_flag;
5648   CLOSE cur_get_salesrep_req_flag;
5649 
5650   IF lv_salesrep_flag = 'Y' THEN
5651     INSERT INTO RA_INTERFACE_SALESCREDITS_ALL
5652                      ( interface_salescredit_id,
5653                        interface_line_id,
5654                        sales_credit_percent_split,
5655                        salesrep_id,
5656                        sales_credit_type_id,
5657                        org_id,
5658                        created_by,
5659                        creation_date,
5660                        last_updated_by,
5661                        last_update_date)
5662                values( RA_CUST_TRX_LINE_SALESREPS_S.nextval,
5663                        p_rgm_ref.transaction_id,
5664                        100,
5665                        -3,
5666                        1,
5667                        p_rgm_ref.org_id,
5668                        ln_user_id,
5669                        sysdate,
5670                        ln_user_id,
5671                        sysdate);
5672   END IF;
5673   --bug#8214204,end*/
5674 
5675   /*commented by csahoo for bug# 6401388
5676  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
5677                                       pv_log_msg  =>  'After Insert statement of the interface tables '
5678                                    );*/
5679 
5680 
5681   /*********************************************************************************************************
5682   || Get the code combination id from the Organization/Regime Registration setup
5683   || by calling the function jai_cmn_rgm_recording_pkg.get_account
5684   *********************************************************************************************************/
5685 
5686   ln_ccid_tax_type_tcs := jai_cmn_rgm_recording_pkg.get_account (
5687                                                                   p_regime_id          => p_rgm_ref.regime_id        ,
5688                                                                   p_organization_type  => jai_constants.orgn_type_io ,
5689                                                                   p_organization_id    => p_rgm_ref.organization_id  ,
5690                                                                   p_location_id        => null                       ,
5691                                                                   p_tax_type           => jai_constants.tax_type_tcs ,
5692                                                                   p_account_name       => jai_constants.liability
5693                                                                 );
5694   IF ln_ccid_tax_type_tcs IS NULL THEN
5695     /**********************************************************************************************************
5696     || Code Combination id has been returned as null from the function jai_cmn_rgm_recording_pkg.get_account
5697     || This is an error condition and the current processing has to be stopped
5698     **********************************************************************************************************/
5699    /*commented by csahoo for bug# 6401388
5700  jai_cmn_debug_contexts_pkg.print ( pn_reg_id =>  ln_reg_id ,
5701                               pv_log_msg  =>  'Invalid code combination of TCS tax Accounting'
5702                             );*/
5703     p_process_flag := jai_constants.expected_error;
5704     lv_process_message  := 'Invalid Code combination ,please check the TCS Tax - Tax Accounting Setup';
5705     p_process_message := lv_process_message ;
5706     rollback;
5707     return;
5708   END IF;
5709 
5710     /*commented by csahoo for bug# 6401388
5711  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
5712                                         pv_log_msg  =>  'Inserting values into ra_interface_distributions_all for REV row:- '||fnd_global.local_chr(10)
5713                                                       ||', interface_line_id          -> '|| p_rgm_ref.transaction_id        ||fnd_global.local_chr(10)
5714                                                       ||', interface_line_context     -> '|| lv_batch_src_name               ||fnd_global.local_chr(10)
5715                                                       ||', interface_line_attribute2  -> '|| p_rgm_ref.transaction_id        ||fnd_global.local_chr(10)
5716                                                       ||', account_class              -> '|| jai_constants.account_class_rev ||fnd_global.local_chr(10)
5717                                                       ||', amount                     -> '|| ln_amount                       ||fnd_global.local_chr(10)
5718                                                       ||', code_combination_id        -> '|| ln_ccid_tax_type_tcs            ||fnd_global.local_chr(10)
5719                                                       ||', acctd_amount               -> '|| ln_amount                       ||fnd_global.local_chr(10)
5720                                                       ||', created_by                 -> '|| ln_user_id                      ||fnd_global.local_chr(10)
5721                                                       ||', creation_date              -> '|| sysdate                         ||fnd_global.local_chr(10)
5722                                                       ||', last_updated_by            -> '|| ln_user_id                      ||fnd_global.local_chr(10)
5723                                                       ||', last_update_date           -> '|| sysdate                         ||fnd_global.local_chr(10)
5724                                                       ||', last_update_login          -> '|| ln_login_id                     ||fnd_global.local_chr(10)
5725                                                       ||', org_id                     -> '|| p_rgm_ref.org_id
5726                                      );*/
5727 
5728     INSERT INTO ra_interface_distributions_all
5729                                            (
5730                                               interface_line_id                   ,
5731                                               interface_line_context              ,
5732                                               interface_line_attribute2           ,
5733                                               account_class                       ,
5734                                               amount                              ,
5735                                               code_combination_id                 ,
5736                                               acctd_amount                        ,
5737                                               created_by                          ,
5738                                               creation_date                       ,
5739                                               last_updated_by                     ,
5740                                               last_update_date                    ,
5741                                               last_update_login                   ,
5742                                               org_id
5743                                            )
5744                                    Values  (
5745                                               p_rgm_ref.transaction_id            ,
5746                                               lv_batch_src_name                   ,
5747                                               p_rgm_ref.transaction_id            ,
5748                                               jai_constants.account_class_rev     ,
5749                                               ln_amount                           ,
5750                                               ln_ccid_tax_type_tcs                ,
5751                                               ln_amount                           ,
5752                                               ln_user_id                          ,
5753                                               sysdate                             ,
5754                                               ln_user_id                          ,
5755                                               sysdate                             ,
5756                                               ln_login_id                         ,
5757                                               p_rgm_ref.org_id
5758                                           );
5759 
5760 
5761 
5762     /*commented by csahoo for bug# 6401388
5763  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
5764                                         pv_log_msg  =>  'Inserting values into jai_rgm_item_gen_docs :- '                           ||fnd_global.local_chr(10)
5765                                                       ||', transaction_id             -> '||p_rgm_ref.transaction_id                ||fnd_global.local_chr(10)
5766                                                       ||', source_document_id         -> '||p_rgm_ref.source_document_id            ||fnd_global.local_chr(10)
5767                                                       ||', source_document_type       -> '||p_rgm_ref.source_document_type          ||fnd_global.local_chr(10)
5768                                                       ||', item_classification        -> '||p_rgm_ref.item_classification           ||fnd_global.local_chr(10)
5769                                                       ||', generated_doc_trx_number   -> '||lv_trx_number                           ||fnd_global.local_chr(10)
5770                                                       ||', generated_doc_id           -> '||ln_customer_trx_id                      ||fnd_global.local_chr(10)
5771                                                       ||', generated_doc_type         -> '||rec_cur_get_batch_source.type           ||fnd_global.local_chr(10)
5772                                                       ||', generated_doc_amt          -> '||ln_amount
5773                                      );*/
5774 
5775   /*################################################################################################################
5776   || INSERT INTO JAI_RGM_ITEM_GEN_DOCS TABLE
5777   ################################################################################################################*/
5778 
5779     INSERT INTO jai_rgm_item_gen_docs   ( transaction_id                    ,
5780                                           source_document_id                ,
5781                                           source_document_type              ,
5782                                           item_classification               ,
5783                                           generated_doc_trx_number          ,
5784                                           generated_doc_id                  ,
5785                                           generated_doc_type                ,
5786                                           generated_doc_amt                 ,
5787                                           created_by                        ,
5788                                           creation_date                     ,
5789                                           last_updated_by                   ,
5790                                           last_update_date                  ,
5791                                           last_update_login
5792                                         )
5793                                VALUES   ( p_rgm_ref.transaction_id          ,
5794                                           p_rgm_ref.source_document_id      ,
5795                                           p_rgm_ref.source_document_type    ,
5796                                           p_rgm_ref.item_classification     ,
5797                                           lv_trx_number                     ,
5798                                           ln_customer_trx_id                ,
5799                                           rec_cur_get_batch_source.type     ,
5800                                           ln_amount                         ,
5801                                           ln_user_id                        ,
5802                                           sysdate                           ,
5803                                           ln_user_id                        ,
5804                                           sysdate                           ,
5805                                           ln_login_id
5806                                         );
5807 
5808      /*commented by csahoo for bug# 6401388
5809  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
5810                                           pv_log_msg  =>  'Data successfully inserted into jai_rgm_item_gen_docs'
5811                                        );
5812 
5813 
5814   jai_cmn_debug_contexts_pkg.deregister (pn_reg_id => ln_reg_id);*/
5815 EXCEPTION
5816 WHEN OTHERS THEN
5817     p_process_flag    := jai_constants.unexpected_error;
5818     p_process_message := ' Unexpected error occured while processing jai_ar_tcs_rep_pkg.generate_document'||substr(SQLERRM,1,300) ;
5819    /*commented by csahoo for bug# 6401388
5820  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
5821                                         pv_log_msg  =>  'Unexpected error occured while processing jai_ar_tcs_rep_pkg.generate_document -> '||substr(SQLERRM,1,300)
5822                                      );*/
5823 
5824 END generate_document;
5825 
5826 PROCEDURE process_transactions (
5827                                     p_event            IN           VARCHAR2                                                               ,
5828                                     p_document_type    IN           VARCHAR2                                Default Null                   ,
5829                                     p_ooh              IN           OE_ORDER_HEADERS_ALL%ROWTYPE            Default Null                   ,
5830                                     p_ract             IN           RA_CUSTOMER_TRX_ALL%ROWTYPE             Default Null                   ,
5831                                     p_acra             IN           AR_CASH_RECEIPTS_ALL%ROWTYPE            Default Null                   ,
5832                                     p_araa             IN           AR_RECEIVABLE_APPLICATIONS_ALL%ROWTYPE  Default Null                   ,
5833                                     p_process_flag     OUT NOCOPY   VARCHAR2                                                               ,
5834                                     p_process_message  OUT NOCOPY   VARCHAR2
5835                                )
5836 IS
5837 
5838   ln_reg_id           NUMBER;
5839   /* **************************************************************************
5840    Creation Date          : 09-Sep-2006
5841    Created By             : Aiyer
5842    Bug Number             : 4742259
5843    Purpose                : Validate and insert the TCS repository with appropriate transaction based entries
5844    Called From            :
5845    Parameter Description  :
5846                             p_document_id    - Unique identifier of the document:-
5847                                                 1. customer_trx_id                - Invoice/Credit Memo identifier
5848                                                 2. cash_receipt_id                - cash receipt Identifier
5849                                                 3. ar_receivable_applications_id  - Unique identifier for a Cash receipt /Credit Memo application to an Invoice/DM
5850 
5851                             p_document_type  - Indicates the type of document eg
5852                                                 1. INVOICE_COMPLETION             - Invoice Completion
5853                                                 2. CASH_TAX_CONFIRMED             - Cash Receipt tax Confirmation
5854                                                 3. CREDIT_MEMO_APPLICATION        - CM application to Invoice
5855                                                 4. CREDIT_MEMO_UNAPPLICATION      - CM Invoice Unapplication
5856                                                 5. RECEIPT_APPLICATION            - Cash receipt application to Invoice
5857                                                 6. RECEIPT_UNAPPLICATION          - Cash receipt unapplication to Invoice
5858                                                 7. RECEIPT_REVERSAL               - Cash receipt reversal
5859 
5860                            p_process_flag
5861                            p_process_message
5862 CHANGE HISTORY:
5863 S.No      Date      Author and Details
5864 ========================================
5865 1.      01-AUG-2008 JMEENA for bug#7277211
5866         Created new  procedure process_sales_order and added code to call process_sales_order when p_event is  BOOKED
5867 
5868   ***************************************************************************/
5869 
5870 
5871   CURSOR cur_get_refs (cp_transaction_id JAI_RGM_REFS_ALL.TRANSACTION_ID%TYPE )
5872   IS
5873   SELECT
5874         *
5875   FROM
5876          jai_rgm_refs_all ref
5877   WHERE
5878     transaction_id = cp_transaction_id;
5879 
5880 
5881   CURSOR cur_get_total_tax (cp_transaction_id JAI_RGM_REFS_ALL.TRANSACTION_ID%TYPE )
5882   IS
5883   SELECT
5884         sum(nvl(jrt.func_tax_amt,0)) total_tax_amount
5885   FROM
5886        jai_rgm_refs_all jrra,
5887        jai_rgm_taxes jrt
5888   WHERE
5889            jrra.trx_ref_id = jrt.trx_ref_id
5890 AND         jrra.transaction_id = cp_transaction_id;
5891 
5892   rec_cur_get_refs          CUR_GET_REFS%ROWTYPE                       ;
5893   ln_tax_tot_amt            NUMBER                                     ;
5894   lv_document_type          VARCHAR2(100)                              ;
5895   lv_item_classification    JAI_RGM_REFS_ALL.ITEM_CLASSIFICATION%TYPE  ;
5896   lv_process_flag           VARCHAR2(2)                                ;
5897   lv_process_message        VARCHAR2(2000)                             ;
5898 
5899   /* Added by JMEENA for TCS issue bug#7277211*/
5900 PROCEDURE process_sales_order (p_ooh              IN             OE_ORDER_HEADERS_ALL%ROWTYPE ,
5901                                p_process_flag     OUT NOCOPY     VARCHAR2                     ,
5902                                p_process_message  OUT NOCOPY     VARCHAR2
5903               )
5904 IS
5905 /* **************************************************************************
5906    Creation Date          : 01-Aug-2008
5907    Created By             : JMEENA
5908    Bug Number             : 7277211
5909    Purpose                :  Insert the record in jai_rgm_thresholds while booking the sales order
5910    Called From            :  PROCESS_TRANSACTION when p_event is BOOKED (sales order booked)
5911   CHANGE HISTORY:
5912  S.No      Date      Author and Details
5913 
5914 **************************************************************************/
5915 
5916     cursor c_get_customer_pan (cp_customer_id    JAI_CMN_CUS_ADDRESSES.customer_id%type)
5917     IS
5918     select   pan_no
5919     from     JAI_CMN_CUS_ADDRESSES
5920     where    customer_id = cp_customer_id
5921     and      confirm_pan = jai_constants.yes;
5922 
5923   cursor c_get_cust_typ_lkup_code(cp_customer_id    JAI_CMN_CUS_ADDRESSES.customer_id%type)
5924     IS
5925     select tcs_customer_type
5926     from   JAI_CMN_CUS_ADDRESSES
5927     where  customer_id  = cp_customer_id
5928     AND tcs_customer_type IS NOT NULL;
5929 
5930     cursor c_get_threshold_slab   ( cp_regime_id                   jai_rgm_thresholds.regime_id%type,
5931                 cp_customer_type_lkup_code    JAI_CMN_CUS_ADDRESSES.tcs_customer_type%type,
5932                 cp_source_trx_date        DATE
5933                                )
5934     IS
5935     select
5936             thslbs.threshold_slab_id
5937     from
5938             jai_ap_tds_thhold_slabs thslbs
5939            ,jai_ap_tds_thhold_types thtyps
5940            ,jai_ap_tds_thhold_hdrs  thhdrs
5941      where
5942             thslbs.threshold_type_id  = thtyps.threshold_type_id
5943      and    thtyps.threshold_hdr_id   = thhdrs.threshold_hdr_id
5944      and    thhdrs.regime_id          = cp_regime_id
5945      and    thtyps.threshold_type     = jai_constants.thhold_typ_cumulative
5946      and    thhdrs.customer_type_lookup_code = cp_customer_type_lkup_code
5947      and    trunc(cp_source_trx_date)      between thtyps.from_date
5948                                       and     nvl(thtyps.to_date, trunc(cp_source_trx_date))
5949      and    NVL(thslbs.from_amount,0) = 0;
5950 
5951 
5952   cursor get_jai_rgm_thresholds_count (   cp_fin_year   jai_rgm_thresholds.fin_year%type,
5953                       cp_org_tan_no jai_rgm_thresholds.org_tan_no%type,
5954                                           cp_party_type jai_rgm_thresholds.party_type%type,
5955                                           cp_party_id   jai_rgm_thresholds.party_id%type,
5956                       cp_regime_id  jai_rgm_thresholds.regime_id%type
5957                     )
5958   IS
5959   select count(*)
5960   from jai_rgm_thresholds
5961    where  fin_year     =   cp_fin_year
5962     and    org_tan_no   =   cp_org_tan_no
5963     and    party_type   =   cp_party_type
5964     and    party_id     =   cp_party_id
5965     and    regime_id    =   cp_regime_id;
5966 
5967   lx_row_id                   rowid;
5968   ln_regime_id            jai_rgm_thresholds.regime_id%type;
5969     ln_org_tan_no           jai_rgm_thresholds.org_tan_no%type;
5970     ln_party_id             jai_rgm_thresholds.party_id%type;
5971   ln_party_type       jai_rgm_thresholds.party_type%type;
5972     ln_fin_year             jai_rgm_thresholds.fin_year%type;
5973   ln_party_pan_no       JAI_CMN_CUS_ADDRESSES.pan_no%type;
5974   ln_customer_type_lkup_code  JAI_CMN_CUS_ADDRESSES.tcs_customer_type%type;
5975   ln_threshold_slab_id    jai_ap_tds_thhold_slabs.threshold_slab_id%type default NULL;
5976   lr_hdr_record                   jai_rgm_thresholds%rowtype;
5977   ln_user_id          fnd_user.user_id%type       :=    fnd_global.user_id;
5978     ln_login_id         fnd_logins.login_id%type    :=    fnd_global.login_id;
5979   ln_count      NUMBER;
5980   ln_threshold_id   jai_rgm_thresholds.threshold_id%type default NULL;
5981  BEGIN
5982 -- code ported from BL12.1.3 by zhiwei.xin on 15-MAR-2013 begin
5983 /*--Commented by Chong for DTC ER 2012/09/11 Get TCS calendar from regime start
5984     OPEN  get_tcs_fin_year(  cp_org_id    => p_ooh.org_id  ,
5985                             cp_trx_date  => p_ooh.creation_date
5986                          );
5987 
5988     FETCH get_tcs_fin_year INTO ln_fin_year;
5989     CLOSE get_tcs_fin_year;
5990 */
5991 --Added by Chong for DTC ER 2012/09/11 Get TCS calendar from regime start
5992   ln_fin_year := get_tcs_fin_year('TCS_CALENDAR','TCS', p_ooh.creation_date);
5993 --Added by Chong for DTC ER 2012/09/11 Get TCS calendar from regime end
5994 -- code ported from BL12.1.3 by zhiwei.xin on 15-MAR-2013 end.
5995     OPEN c_get_rgm_attribute (    cp_regime_code           =>   jai_constants.tcs_regime                  ,
5996                                   cp_attribute_code        =>   jai_constants.rgm_attr_code_org_tan       ,
5997                                   cp_organization_id       =>   p_ooh.ship_from_org_id
5998                                ) ;
5999     FETCH c_get_rgm_attribute INTO ln_regime_id, ln_org_tan_no ;
6000     IF C_GET_RGM_ATTRIBUTE%NOTFOUND THEN
6001     CLOSE c_get_rgm_attribute;
6002       p_process_flag     := jai_constants.expected_error;
6003         p_process_message  := 'Org Tan Number needs to be defined for the TCS regime ';
6004         return;
6005     END IF;
6006     CLOSE c_get_rgm_attribute;
6007 
6008   OPEN c_get_customer_pan (cp_customer_id => p_ooh.sold_to_org_id );
6009   FETCH c_get_customer_pan INTO ln_party_pan_no;
6010 
6011   IF c_get_customer_pan%NOTFOUND THEN
6012     CLOSE c_get_customer_pan;
6013       p_process_flag := jai_constants.expected_error;
6014       p_process_message  := 'Party pan no is not available for this party';
6015       RETURN;
6016   END IF;
6017   CLOSE c_get_customer_pan;
6018 
6019 
6020 
6021   OPEN c_get_cust_typ_lkup_code (cp_customer_id => p_ooh.sold_to_org_id );
6022   FETCH c_get_cust_typ_lkup_code INTO ln_customer_type_lkup_code;
6023 
6024   IF c_get_cust_typ_lkup_code%NOTFOUND THEN
6025     CLOSE c_get_cust_typ_lkup_code;
6026       p_process_flag := jai_constants.expected_error;
6027       p_process_message  := 'Customer type lookup code is not available for this party';
6028       RETURN;
6029   END IF;
6030   CLOSE c_get_cust_typ_lkup_code;
6031 
6032   OPEN c_get_threshold_slab (cp_regime_id                => ln_regime_id,
6033                            cp_customer_type_lkup_code  => ln_customer_type_lkup_code,
6034                            cp_source_trx_date     => p_ooh.creation_date
6035 
6036                 );
6037   FETCH c_get_threshold_slab INTO ln_threshold_slab_id;
6038   CLOSE c_get_threshold_slab;
6039 
6040   ln_count :=0;
6041 
6042   OPEN get_jai_rgm_thresholds_count ( cp_fin_year => ln_fin_year,
6043                                       cp_org_tan_no => ln_org_tan_no,
6044                                       cp_party_type => jai_constants.party_type_customer,
6045                                       cp_party_id   => p_ooh.sold_to_org_id ,
6046                                       cp_regime_id  => ln_regime_id
6047                                   );
6048   FETCH get_jai_rgm_thresholds_count INTO ln_count;
6049   CLOSE get_jai_rgm_thresholds_count;
6050 
6051       lr_hdr_record.threshold_id              :=  ln_threshold_id   ;
6052         lr_hdr_record.regime_id                 :=  ln_regime_id;
6053         lr_hdr_record.org_tan_no                :=  ln_org_tan_no ;
6054         lr_hdr_record.party_id                  :=  p_ooh.sold_to_org_id;
6055         lr_hdr_record.party_type                :=  jai_constants.party_type_customer ;
6056         lr_hdr_record.threshold_slab_id         :=  ln_threshold_slab_id    ;
6057         lr_hdr_record.fin_year                  :=  ln_fin_year         ;
6058         lr_hdr_record.total_threshold_amt       :=  null                    ;
6059         lr_hdr_record.total_threshold_base_amt  :=  null                    ;
6060         lr_hdr_record.creation_date             :=  sysdate                 ;
6061         lr_hdr_record.created_by                :=  ln_user_id              ;
6062         lr_hdr_record.last_update_date          :=  sysdate                 ;
6063         lr_hdr_record.last_updated_by           :=  ln_user_id              ;
6064         lr_hdr_record.last_update_login         :=  ln_login_id             ;
6065     lr_hdr_record.party_pan_no        :=ln_party_pan_no;
6066     --Insert in jai_rgm_thresholds only if records does not exists.
6067     IF NVL(ln_count,0) = 0 THEN
6068 
6069         jai_rgm_thhold_proc_pkg.insert_threshold_hdr   (  p_record          =>    lr_hdr_record
6070                                 , p_threshold_id    =>    ln_threshold_id
6071                                 , p_row_id          =>    lx_row_id
6072                                );
6073   END IF;
6074 
6075 END process_sales_order;
6076 ---End of process_sales_order bug#7277211
6077 
6078 BEGIN
6079 
6080   /*########################################################################################################
6081   || VARIABLES INITIALIZATION
6082   ########################################################################################################*/
6083 
6084   /** Register procedure for debuging */
6085 
6086   lv_member_name        := 'PROCESS_TRANSACTIONS';
6087   set_debug_context;
6088   /*commented by csahoo for bug# 6401388
6089    jai_cmn_debug_contexts_pkg.register ( pv_context => lv_context ,
6090                                      pn_reg_id  => ln_reg_id
6091                                    );*/
6092   lv_process_flag    := jai_constants.successful   ;
6093   lv_process_message := null                       ;
6094 
6095   p_process_flag     := lv_process_flag            ;
6096   p_process_message  := lv_process_message         ;
6097   ln_event           := p_event                    ;
6098 
6099 
6100   /*commented by csahoo for bug# 6401388
6101  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6102                              pv_log_msg  =>  '******************Start of JAI_AR_TC_SREP_PKG.PROCESS_TRANSACTIONS***************, Event is '||p_event
6103                           );*/
6104 
6105 
6106   /*########################################################################################################
6107   || PROCESS COMPLETED INVOICES ( DEBIT MEMO'S ALSO INCLUDED)
6108   ########################################################################################################*/
6109   IF p_event  = jai_constants.order_booked  OR
6110      p_event  = jai_constants.wsh_ship_confirm
6111   THEN                   ---------A1
6112    /*commented by csahoo for bug# 6401388
6113  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6114                                         pv_log_msg  =>  '******************Before call to VALIDATE_SALES_ORDER/SHIP CONFIRM  ***************, lv_document_type '||lv_document_type
6115                                      );*/
6116     validate_sales_order  ( p_ooh              =>  p_ooh               ,
6117                             p_process_flag     =>  lv_process_flag     ,
6118                             p_process_message  =>  lv_process_message
6119                           );
6120 
6121     /*commented by csahoo for bug# 6401388
6122  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6123                                         pv_log_msg  =>  '******************Returned from call to VALIDATE_SALES_ORDER/SHIP CONFIRM ***************, lv_process_flag '||lv_process_flag
6124                                      );*/
6125 
6126     IF lv_process_flag = jai_constants.not_applicable THEN
6127       /*commented by csahoo for bug# 6401388
6128  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6129                                           pv_log_msg  =>  'Skip the transaction'
6130                                        );*/
6131       return;
6132     END IF;
6133 
6134     IF lv_process_flag = jai_constants.expected_error    OR                            ---------A2
6135        lv_process_flag = jai_constants.unexpected_error
6136     THEN
6137       /*
6138       || As Returned status is an error/not applicable hence:-
6139       || Set out variables p_process_flag and p_process_message accordingly
6140       */
6141         --call to debug package
6142      /*commented by csahoo for bug# 6401388
6143  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6144                                  pv_log_msg  =>  ' Error : - lv_process_flag -> '||lv_process_flag||' - '||lv_process_message
6145                               );*/
6146 
6147       p_process_flag    := lv_process_flag    ;
6148       p_process_message := lv_process_message ;
6149       return;
6150     END IF;                                                                            --------A1
6151 
6152 --Added by JMEENA for bug#7277211
6153 IF p_event  = jai_constants.order_booked THEN
6154     process_sales_order ( p_ooh              =>  p_ooh               ,
6155                             p_process_flag     =>  lv_process_flag     ,
6156                             p_process_message  =>  lv_process_message
6157                           );
6158 
6159  IF lv_process_flag = jai_constants.expected_error    OR                            ---------A2
6160        lv_process_flag = jai_constants.unexpected_error
6161     THEN
6162       p_process_flag    := lv_process_flag    ;
6163       p_process_message := lv_process_message ;
6164       return;
6165     END IF;
6166 END IF;
6167 --End for bug#7277211
6168 
6169   END IF;
6170 
6171   /*########################################################################################################
6172   || PROCESS COMPLETED INVOICES ( DEBIT MEMO'S ALSO INCLUDED)
6173   ########################################################################################################*/
6174   IF p_event = jai_constants.trx_event_completion  THEN                   ---------B1
6175 
6176   /*commented by csahoo for bug# 6401388
6177  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6178                              pv_log_msg  =>  '******************Before call to VALIDATE_INVOICE ***************, lv_document_type '||lv_document_type
6179                           );*/
6180     validate_invoice      ( p_ract             =>  p_ract              ,
6181                             p_document_type    =>  lv_document_type     ,
6182                             p_process_flag     =>  lv_process_flag     ,
6183                             p_process_message  =>  lv_process_message
6184                           );
6185 
6186   /*commented by csahoo for bug# 6401388
6187  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6188                              pv_log_msg  =>  '******************Returned from call to VALIDATE_INVOICE ***************, lv_process_flag '||lv_process_flag
6189                           );*/
6190     IF lv_process_flag = jai_constants.not_applicable THEN
6191      /*commented by csahoo for bug# 6401388
6192  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6193                                  pv_log_msg  =>  'Skip the transaction'
6194                               );*/
6195       return;
6196     END IF;
6197 
6198     IF lv_process_flag = jai_constants.expected_error    OR                            ---------B2
6199        lv_process_flag = jai_constants.unexpected_error
6200     THEN
6201       /*
6202       || As Returned status is an error/not applicable hence:-
6203       || Set out variables p_process_flag and p_process_message accordingly
6204       */
6205         --call to debug package
6206       /*commented by csahoo for bug# 6401388
6207  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6208                                  pv_log_msg  =>  ' Error : - lv_process_flag -> '||lv_process_flag||' - '||lv_process_message
6209                               );*/
6210 
6211       p_process_flag    := lv_process_flag    ;
6212       p_process_message := lv_process_message ;
6213       return;
6214     END IF;                                                                            ---------B2
6215 
6216     process_invoices      ( p_ract             =>  p_ract              ,
6217                             p_document_type    =>  lv_document_type    ,
6218                             p_process_flag     =>  lv_process_flag     ,
6219                             p_process_message  =>  lv_process_message
6220                           );
6221 
6222     IF lv_process_flag = jai_constants.not_applicable THEN
6223      /*commented by csahoo for bug# 6401388
6224  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6225                                  pv_log_msg  =>  'Skip the transaction'
6226                               );*/
6227 
6228       return;
6229     END IF;
6230 
6231 
6232     IF lv_process_flag = jai_constants.expected_error    OR                            ---------B3
6233        lv_process_flag = jai_constants.unexpected_error
6234     THEN
6235       /*
6236       || As Returned status is an error/not applicable hence:-
6237       || Set out variables p_process_flag and p_process_message accordingly
6238       */
6239       --call to debug package
6240      /*commented by csahoo for bug# 6401388
6241  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6242                                  pv_log_msg  =>  ' Error : - lv_process_flag -> '||lv_process_flag||' - '||lv_process_message
6243                               );*/
6244 
6245       p_process_flag    := lv_process_flag    ;
6246       p_process_message := lv_process_message ;
6247       return;
6248     END IF;                                                                            ---------B3
6249   END IF;                                                                      ---------B1
6250 
6251   /*########################################################################################################
6252   || PROCESS CONFIRMED RECEIPTS HAVING TCS APPLICABILITY
6253   ########################################################################################################*/
6254 
6255   IF p_event  IN ( jai_constants.ar_cash_tax_confirmed ,              ---------C1
6256                    jai_constants.trx_type_rct_rvs
6257                  )
6258   THEN
6259     lv_document_type := p_event;
6260 
6261     /*commented by csahoo for bug# 6401388
6262  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6263                                pv_log_msg  =>  ' Before call to validate_receipts : - p_event -> '||p_event
6264                             );*/
6265     validate_receipts ( p_acra             =>  p_acra             ,
6266                         p_document_type    =>  lv_document_type   ,
6267                         p_process_flag     =>  lv_process_flag    ,
6268                         p_process_message  =>  lv_process_message
6269                       );
6270 
6271    /*commented by csahoo for bug# 6401388
6272  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6273                                pv_log_msg  =>  ' Returned from validate_receipts lv_process_flag: '||lv_process_flag
6274                             );*/
6275     IF lv_process_flag = jai_constants.not_applicable THEN
6276       /*commented by csahoo for bug# 6401388
6277  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6278                                  pv_log_msg  =>  'Skip the transaction'
6279                               );*/
6280 
6281       return;
6282     END IF;
6283 
6284     IF lv_process_flag = jai_constants.expected_error    OR                            ---------C2
6285        lv_process_flag = jai_constants.unexpected_error
6286     THEN
6287       /*
6288       || As Returned status is an error/not applicable hence:-
6289       || Set out variables p_process_flag and p_process_message accordingly
6290       */
6291       --call to debug package
6292       /*commented by csahoo for bug# 6401388
6293  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6294                                  pv_log_msg  =>  ' Error : - lv_process_flag -> '||lv_process_flag||' - '||lv_process_message
6295                               );*/
6296 
6297       p_process_flag    := lv_process_flag    ;
6298       p_process_message := lv_process_message ;
6299       return;
6300     END IF;                                                                            ---------C2
6301     /*commented by csahoo for bug# 6401388
6302  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6303                                pv_log_msg  =>  ' Before call to process_receipts : - p_event -> '||p_event
6304                             );*/
6305 
6306     process_receipts   (  p_acra              =>  p_acra             ,
6307                           p_document_type     =>  p_event            ,
6308                           p_process_flag      =>  lv_process_flag    ,
6309                           p_process_message   =>  lv_process_message
6310                        );
6311     /*commented by csahoo for bug# 6401388
6312  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6313                                pv_log_msg  =>  ' Returned from process_receipts lv_process_flag: '||lv_process_flag
6314                             );*/
6315 
6316     IF  lv_process_flag = jai_constants.not_applicable THEN
6317      /*commented by csahoo for bug# 6401388
6318  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6319                                  pv_log_msg  =>  'Skip the transaction'
6320                               );*/
6321 
6322       return;
6323     END IF;
6324 
6325     IF lv_process_flag = jai_constants.expected_error    OR                            ---------C3
6326        lv_process_flag = jai_constants.unexpected_error
6327     THEN
6328       /*
6329       || As Returned status is an error/not applicable hence:-
6330       || Set out variables p_process_flag and p_process_message accordingly
6331       */
6332       --call to debug package
6333      /*commented by csahoo for bug# 6401388
6334  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6335                                  pv_log_msg  =>  ' Error : - lv_process_flag -> '||lv_process_flag||' - '||lv_process_message
6336                               );*/
6337 
6338       p_process_flag    := lv_process_flag    ;
6339       p_process_message := lv_process_message ;
6340       return;
6341     END IF;                                                                            ---------C3
6342   END IF;                                                                     ---------C1
6343 
6344   /*########################################################################################################
6345   || PROCESS ALL RECEIPT AND CREDIT MEMO APPLICATIONS/UNAPPLICATIONS
6346   ########################################################################################################*/
6347 
6348   IF p_event IN ( jai_constants.ar_cash   ,                                                        ---------D1
6349                   jai_constants.ar_invoice_type_cm
6350                 )
6351   THEN
6352 
6353     /***********
6354     ||Validate application and unapplications
6355     ***********/
6356     /*commented by csahoo for bug# 6401388
6357  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6358                                pv_log_msg  =>  ' Before call to validate_app_unapp : - p_event -> '||p_event
6359                             );*/
6360 
6361     validate_app_unapp (
6362                             p_araa                =>  p_araa                    ,
6363                             p_document_type       =>  lv_document_type          ,
6364                             p_item_classification =>  lv_item_classification    ,
6365                             p_process_flag        =>  lv_process_flag           ,
6366                             p_process_message     =>  lv_process_message
6367                        );
6368 
6369     /*commented by csahoo for bug# 6401388
6370  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6371                                pv_log_msg  =>  ' Returned from validate_app_unapp lv_process_flag: '||lv_process_flag
6372                             );*/
6373 
6374     IF  lv_process_flag = jai_constants.not_applicable THEN
6375      /*commented by csahoo for bug# 6401388
6376  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6377                                  pv_log_msg  =>  'Skip the transaction'
6378                               );*/
6379       return;
6380     END IF;
6381 
6382 
6383     IF lv_process_flag = jai_constants.expected_error    OR                                 ---------D2
6384        lv_process_flag = jai_constants.unexpected_error
6385     THEN
6386       /*
6387       || As Returned status is an error/not applicable hence:-
6388       || Set out variables p_process_flag and p_process_message accordingly
6389       */
6390       --call to debug package
6391       /*commented by csahoo for bug# 6401388
6392  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6393                                  pv_log_msg  =>  ' Error : - lv_process_flag -> '||lv_process_flag||' - '||lv_process_message
6394                               );*/
6395 
6396       p_process_flag    := lv_process_flag    ;
6397       p_process_message := lv_process_message ;
6398       return;
6399     END IF;                                                                                 ---------D2
6400 
6401 
6402     IF lv_document_type IN ( jai_constants.trx_type_rct_app,                                ---------D3
6403                              jai_constants.trx_type_cm_app
6404                            )
6405     THEN
6406    /*commented by csahoo for bug# 6401388
6407  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6408                                pv_log_msg  =>  ' Before call to process_applications : - p_event -> '||p_event
6409                             );*/
6410 
6411       process_applications  ( p_araa                =>  p_araa                    ,
6412                               p_document_type       =>  lv_document_type          ,
6413                               p_item_classification =>  lv_item_classification    ,
6414                               p_process_flag        =>  lv_process_flag           ,
6415                               p_process_message     =>  lv_process_message
6416                            );
6417     /*commented by csahoo for bug# 6401388
6418  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6419                                pv_log_msg  =>  ' Returned from process_applications lv_process_flag: '||lv_process_flag
6420                             );*/
6421 
6422     ELSIF  lv_document_type  IN ( jai_constants.trx_type_rct_unapp,                        ---------D3
6423                                   jai_constants.trx_type_cm_unapp
6424                                 )
6425     THEN
6426     /*commented by csahoo for bug# 6401388
6427  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6428                                pv_log_msg  =>  ' Before call to process_unapp_rcpt_rev : - p_event -> '||p_event
6429                             );*/
6430 
6431       process_unapp_rcpt_rev (  p_araa               =>     p_araa                ,
6432                                 p_acra               =>     p_acra                ,
6433                                 p_document_type      =>     lv_document_type      ,
6434                                 p_process_flag       =>     lv_process_flag       ,
6435                                 p_process_message    =>     lv_process_message
6436                              );
6437 
6438     /*commented by csahoo for bug# 6401388
6439  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6440                                pv_log_msg  =>  ' Returned from process_unapp_rcpt_rev lv_process_flag: '||lv_process_flag
6441                             );*/
6442 
6443     END IF;                                                                               ---------D3
6444 
6445     IF  lv_process_flag = jai_constants.not_applicable THEN                               ---------D4
6446      /*commented by csahoo for bug# 6401388
6447  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6448                                  pv_log_msg  =>  'Skip the transaction'
6449                               );*/
6450 
6451       return;
6452     END IF;                                                                               ---------D4
6453 
6454     IF lv_process_flag = jai_constants.expected_error    OR                                     ---------D2
6455        lv_process_flag = jai_constants.unexpected_error
6456     THEN
6457       /*
6458       || As Returned status is an error/not applicable hence:-
6459       || Set out variables p_process_flag and p_process_message accordingly
6460       */
6461       --call to debug package
6462       /*commented by csahoo for bug# 6401388
6463  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6464                                  pv_log_msg  =>  ' Error : - lv_process_flag -> '||lv_process_flag||' - '||lv_process_message
6465                               );*/
6466 
6467       p_process_flag    := lv_process_flag    ;
6468       p_process_message := lv_process_message ;
6469       return;
6470     END IF;                                                                                     ---------D2
6471   END IF;                                                                                ---------D1
6472 
6473 
6474   /*########################################################################################################
6475   || CALL PROCEDURE TO GENERATE DOCUMENTS FOR ALL EVENTS EXCEPT INVOICE/DEBIT_MEMO/CREDIT_MEMO_APPLICATION
6476   ########################################################################################################*/
6477 
6478   IF p_event IN ( jai_constants.ar_cash_tax_confirmed ,              ---------C1
6479                   jai_constants.trx_type_rct_rvs      ,
6480                   jai_constants.ar_cash               ,                                   ---------D1
6481                   jai_constants.ar_invoice_type_cm
6482                 )
6483   THEN
6484     /*commented by csahoo for bug# 6401388
6485  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6486                                pv_log_msg  =>  ' Before call to generate_document : - p_event -> '||p_event
6487                             );*/
6488 
6489 
6490   OPEN cur_get_refs  (cp_transaction_id => ln_transaction_id );
6491   FETCH cur_get_refs INTO rec_cur_get_refs;
6492   CLOSE cur_get_refs ;
6493 
6494   OPEN  cur_get_total_tax (cp_transaction_id => ln_transaction_id );
6495   FETCH cur_get_total_tax INTO ln_tax_tot_amt;
6496   CLOSE cur_get_total_tax ;
6497 
6498   generate_document (  p_rgm_ref               => rec_cur_get_refs    ,
6499                        p_total_tax_amt         => ln_tax_tot_amt      ,
6500                       --p_transaction_id        => ln_transaction_id   ,
6501                        p_process_flag          => lv_process_flag     ,
6502                        p_process_message       => lv_process_message
6503                     );
6504 
6505     /*commented by csahoo for bug# 6401388
6506  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6507                                         pv_log_msg  =>  ' Returned from generate_document lv_process_flag: '||lv_process_flag
6508                                      );*/
6509 
6510     IF  lv_process_flag = jai_constants.not_applicable THEN                               ---------D4
6511      /*commented by csahoo for bug# 6401388
6512  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6513                                           pv_log_msg  =>  'Skip the transaction'
6514                                         );*/
6515 
6516       return;
6517     END IF;                                                                               ---------D4
6518 
6519     IF lv_process_flag = jai_constants.expected_error    OR                                     ---------D2
6520        lv_process_flag = jai_constants.unexpected_error
6521     THEN
6522       /*
6523       || As Returned status is an error/not applicable hence:-
6524       || Set out variables p_process_flag and p_process_message accordingly
6525       */
6526       --call to debug package
6527      /*commented by csahoo for bug# 6401388
6528  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6529                                           pv_log_msg  =>  ' Error : - lv_process_flag -> '||lv_process_flag||' - '||lv_process_message
6530                                        );*/
6531 
6532       p_process_flag    := lv_process_flag    ;
6533       p_process_message := lv_process_message ;
6534       return;
6535     END IF;                                                                                     ---------D2
6536   END IF;                                                                                ---------D1
6537 
6538   /*########################################################################################################
6539   || CALL FOR SURCHARGE PROCESSING
6540   ########################################################################################################*/
6541 
6542   /*
6543   ||Call to surcharge package to update the threshold level accordingly
6544   */
6545   IF ln_transaction_id IS NOT NULL THEN
6546 
6547     /*commented by csahoo for bug# 6401388
6548  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6549                                pv_log_msg  =>  ' Before call to jai_rgm_thhold_proc_pkg.maintain_threshold : - p_event -> '||p_event
6550                                ||', ln_transaction_id -> '||ln_transaction_id
6551                             );*/
6552 
6553     jai_rgm_thhold_proc_pkg.maintain_threshold ( p_transaction_id   => ln_transaction_id   ,
6554                                                  p_process_flag     => lv_process_flag     ,
6555                                                  p_process_message  => lv_process_message
6556                                                );
6557 
6558     /*commented by csahoo for bug# 6401388
6559  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6560                                pv_log_msg  =>  ' Returned from jai_rgm_thhold_proc_pkg.maintain_threshold lv_process_flag: '||lv_process_flag
6561                             );*/
6562 
6563     IF  lv_process_flag = jai_constants.not_applicable THEN
6564      /*commented by csahoo for bug# 6401388
6565  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6566                                  pv_log_msg  =>  'Skip the transaction'
6567                               );*/
6568 
6569       return;
6570     END IF;
6571 
6572     IF lv_process_flag = jai_constants.expected_error    OR                                     ---------D2
6573        lv_process_flag = jai_constants.unexpected_error
6574     THEN
6575       /*
6576       || As Returned status is an error/not applicable hence:-
6577       || Set out variables p_process_flag and p_process_message accordingly
6578       */
6579       --call to debug package
6580      /*commented by csahoo for bug# 6401388
6581  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6582                                  pv_log_msg  =>  ' Error : - lv_process_flag -> '||lv_process_flag||' - '||lv_process_message
6583                               );*/
6584 
6585       p_process_flag    := lv_process_flag    ;
6586       p_process_message := lv_process_message ;
6587       return;
6588     END IF;                                                                                     ---------D2
6589   END IF;
6590 
6591  /*commented by csahoo for bug# 6401388
6592  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6593                              pv_log_msg  =>  '**************** END OF PROCESS TRANSACTION ****************'
6594                           );
6595 
6596 
6597   jai_cmn_debug_contexts_pkg.deregister (pn_reg_id => ln_reg_id);*/
6598 EXCEPTION
6599   WHEN OTHERS THEN
6600     p_process_flag    := jai_constants.unexpected_error;
6601     p_process_message := lv_context||' Unexpected error occured while processing jai_ar_tcs_rep_pkg.process_transactions'||SQLERRM ;
6602     /*commented by csahoo for bug# 6401388
6603  jai_cmn_debug_contexts_pkg.print( pn_reg_id           => ln_reg_id                   ,
6604                                       pv_log_msg          => p_process_message           ,
6605                                       pn_statement_level  => jai_cmn_debug_contexts_pkg.summary
6606                                    );
6607 
6608  jai_cmn_debug_contexts_pkg.print_stack;*/
6609 
6610 END process_transactions ;
6611 
6612   PROCEDURE update_pan_for_tcs          ( p_return_code             OUT NOCOPY          VARCHAR2                                                          ,
6613                                           p_errbuf                  OUT NOCOPY          VARCHAR2                                                          ,
6614                                           p_party_id                IN                  JAI_RGM_REFS_ALL.PARTY_ID%TYPE                                    ,
6615                                           p_old_pan_no              IN                  JAI_CMN_CUS_ADDRESSES.PAN_NO%TYPE                              ,
6616                                           p_new_pan_no              IN                  JAI_CMN_CUS_ADDRESSES.PAN_NO%TYPE
6617 
6618                                         )
6619   AS
6620    ln_reg_id                 NUMBER             ;
6621 
6622    ln_request_id             NUMBER             ;
6623 BEGIN
6624 
6625   /*################################################################################################################
6626   || Initialize the variables
6627   ################################################################################################################*/
6628 
6629   lv_member_name        := 'UPDATE_PAN_FOR_TCS';
6630   set_debug_context;
6631   /*commented by csahoo for bug# 6401388
6632   jai_cmn_debug_contexts_pkg.register ( pv_context => lv_context    ,
6633                                         pn_reg_id  => ln_reg_id
6634                                        );
6635 
6636    jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6637                                       pv_log_msg  =>  'Parameter passed to update_pan_for_tcs are -> '    ||fnd_global.local_chr(10)
6638                                                     ||', p_party_id        -> '||p_party_id               ||fnd_global.local_chr(10)
6639                                                     ||', p_old_pan_no      -> '||p_old_pan_no             ||fnd_global.local_chr(10)
6640                                                     ||', p_new_pan_no      -> '||p_new_pan_no
6641                                    );*/
6642 
6643   p_return_code := 0                         ;
6644   p_errbuf      := Null                      ;
6645   ln_request_id := fnd_global.conc_request_id;
6646 
6647 
6648   /*commented by csahoo for bug# 6401388
6649  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6650                                       pv_log_msg  =>  'Updating the table JAI_CMN_CUS_ADDRESSES'
6651                                     );*/
6652 
6653   /*
6654   ||Update the JAI_CMN_CUS_ADDRESSES table . Set the Old pan number with the new pan number as specified in the input.
6655   */
6656   UPDATE
6657         JAI_CMN_CUS_ADDRESSES
6658   SET
6659         pan_no = p_new_pan_no
6660   WHERE
6661         customer_id = p_party_id
6662   AND   pan_no      = p_old_pan_no ;
6663 
6664   /*commented by csahoo for bug# 6401388
6665  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6666                                       pv_log_msg  =>  'Update to table JAI_CMN_CUS_ADDRESSES successful. Now updating the table jai_rgm_thresholds '
6667                                    );*/
6668 
6669 
6670   /*
6671   ||Update the JAI_CMN_CUS_ADDRESSES table . Set the Old pan number with the new pan number as specified in the input.
6672   */
6673   UPDATE
6674         jai_rgm_thresholds
6675   SET
6676         party_pan_no  = p_new_pan_no
6677   WHERE
6678         party_id      = p_party_id
6679   AND   party_pan_no  = p_old_pan_no ;
6680 
6681 
6682   /*commented by csahoo for bug# 6401388
6683  jai_cmn_debug_contexts_pkg.print (  pn_reg_id   =>  ln_reg_id ,
6684                                       pv_log_msg  =>  '**************** UPDATE_PAN_FOR_TCS SUCCESSFULLY COMPLETED ****************'
6685                                    );
6686   jai_cmn_debug_contexts_pkg.deregister (pn_reg_id => ln_reg_id);*/
6687 
6688 EXCEPTION
6689   WHEN OTHERS THEN
6690     p_return_code := 2;
6691     p_errbuf      := 'Unexpected error in the jai_ar_tcs_rep_pkg.update_pan_for_tcs '||substr(sqlerrm,1,300);
6692 
6693 END update_pan_for_tcs;
6694 END jai_ar_tcs_rep_pkg;