DBA Data[Home] [Help]

PACKAGE BODY: APPS.JAI_AR_RGM_PROCESSING_PKG

Source


1 PACKAGE BODY jai_ar_rgm_processing_pkg
2 /* $Header: jai_ar_rgm_proc.plb 120.53.12020000.4 2013/01/09 08:10:17 qioliu ship $ */
3 
4 /******************************************************************************************************************************************************
5 Created By       : aiyer
6 Created Date     : 27-jan-2005
7 Enhancement Bug  : 4146634
8 Purpose          : Process the Service Tax AR records (Invoices,Credit memo's and Cash Receipts Applications) and populate
9                    the jai_rgm_trx_refs and jai_rgms_trx_records appropriately.
10 Called From      : jai_rgm_trx_processing.process_batch
11 
12                    Dependency Due To The Current Bug :
13                    This object has been newly created with as a part of the service tax enhancement.
14                    Needs to be always released along with the bug 4146708.
15 
16 Change History: -
17 =================
18 1  20-Feb-2005  aiyer - Bug # 4193633 - File Version# 115.1
19    Issue
20     The tax earned and unearned discount are not getting apportioned properly for the service type of taxes and hence the India - Service Tax concurrent
21     ends up in a warning for records with these issues
22 
23    Fix
24     The procedure get_ar_tax_disc_accnt has been modified for the fix of this bug.
25     Please refer the procedure change history for the details of this bug
26 
27    Dependency Due To This Bug:
28     Dependency exists due to specification change of the current procedure.
29     Always sent the following packages together:-
30 
31     1. jai_rgm_process_ar_taxes_pkg_s.sql          (115.1)
32     2. jai_rgm_process_ar_taxes_pkg_b.sql          (115.1)
33     3. jai_rgm_trx_recording_pkg_s.sql version     (115.1)
34     4. jai_rgm_trx_recording_pkg_b.sql version     (115.1)
35 
36 2. 08-Jun-2005  Version 116.2 jai_ar_rgm_proc -Object is Modified to refer to New DB Entity names in place of Old DB Entity Names
37     as required for CASE COMPLAINCE.
38 
39 3.     14-Jun-2005      rchandan for bug#4428980, Version 116.3
40                         Modified the object to remove literals from DML statements and CURSORS.
41 
42 4.   14-May-2005      CSahoo for bug#5879769. File Version 120.4
43           Forward porting of 11i BUG#5694855
44           SERVICE TAX BY INVENTORY ORGANIZATION AND SERVICE TYPE SOLUTION
45 
46 5.   29-Nov-2006      Walton for inclusive tax computation
47 
48 6.   26-June-2008           Changes by nprashar for bug 6997453
49                     Issue : Invoice date column in 'India - Service tax pending liability' report
50                 is showing creation date. (The reason for making changes in this file is that
51                 the service tax processing program's behavior was also wrong w.r.to the date
52                 paratmeters passed and the way AR invoices are picked up for populating
53                 jai_rgm_trx_refs table
54                     Cause : The query in the report is using jai_rgm_trx_refs.creation_date as
55                 the invoice date. While checking how this field is populated, observed that
56                 the records are populated while running the service tax processor.
57                 In the procedure populate_inv_cm_references, the invoices are picked up
58                 based on the creation_date. This is wrong behavior, and by common sense it
59                 should be picked up on the basis of trx_date of the invoice.
60         Fix : Modified the cursor c_fetch_inv_cm_rec as described above.
61 
62 7.   04/11/2008       Forward Port Bug 6474509
63                 Issue: INDIA TAXES NOT REVERSING ON REVERSING A RECEIPT
64                 Reason: Cursor c_get_refrec_for_upd doesn't fetch invoices to process the receipt reversals
65                       only when there is no discounted amount during receipt application
66                 Fix   : Cursor c_get_refrec_for_upd (Added = condition) is modified to fetch the invoices
67                       "nvl(recoverable_amount,0) - nvl(discounted_amount,0) >= nvl(recovered_amount,0)"
68 
69 8.  17-May-2009 Bug 7522584
70                 Issue : Service Tax entered in foreign currency for AR Invoice
71                 is not converted to Functional Currency
72                 Fix: Modified the code in the procs populate_inv_cm_references, populate_cm_app
73         and populate_receipt_records.
74                 Added a multipier to the tax amount so as to calulate the tax amount in functional currency.
75 
76 9.  23-May-2009 Bug 8294236
77                Issue: Service Tax Transaction created Fr Exchange Balances on Tax Accounts after Settlement
78                Fix: Modified the code in procedures populate_cm_app and populate_receipt_records. Added the call to
79                the procedure JAI_RGM_TRX_RECORDING_PKG.exc_gain_loss_accounting.
80 
81 10.  20/12/2009   Xiao for bug#6773751.
82     Issue:
83     SERVICE TAX DEBIT ENTRIES ARE NOT UPDATED IN THE SERVICE TAX REGISTER
84     Reason:Debit memo is not considered at the time of service tax enhancement
85     Fix:
86     Changes are done to include to process the debit memos.
87      following scenarios are considered:
88       1.Credit memo applied to debit memo
89       2.Cash Receipt applied to debit memo
90       3.Receip unapplication
91 
92 11. 3-Mar-2010   Bug 9432780
93                  Issue - When payment term is not "Immediate" for an AR invoice and a
94                  credit memo is applied instead of cash receipt, service tax accounting
95                  goes wrong for the 2nd (and later) installment.
96                  Fix - In procedure populate_cm_app, the amount to be accounted for the CM
97                  is calculated using ratio of (amount applied / total recoverable amount).
98                  Total recoverable amount is calculated as sum(recoverable amount - recoverable amount),
99                  which gives wrong results for 2nd installment and later.
100                  So modified the cursor c_get_cmref_totrd_amt to calculate this amount as
101                  simply sum(recoverable amount).
102 
103 12  4-Apr-2010  Bo Li for Bug9305067
104                 Replace the old attribute columns of JAI_RGM_TRX_RECORDS with the new meaningful columns
105 
106 13. 27-Jul-2010 Bug 9919612
107                 Issue: If an AR Invoice is created with just Adhoc Service Tax(with no Line Amount) then
108                 divisor by zero error is thrown
109                 Fix: Added a check to set a temporary line amount variable with value 1 if the Original
110                 Line amount is zero
111 
112 14. Oct 03, 2010 Bug 10148245
113                  Description: When Receipt Application is made in detail, the Service Tax
114                  amounts are not prorated correctly across all the Lines
115                  The Tax proration is done on the total tax amount of all lines
116                  Fix: Fetched Tax Applied and Total Tax of individual lines from
117                  ar_activity_details and prorated the taxes accordingly
118 
119 15. Oct 29, 2010 Bug 10230041 fixed by Eric Ma
120 
121                  The credit memo entries are missing in both jai_rgm_trx_records and gl_interface tables
122                  when applying the receipt with CM and Invoice.
123                  Added REVERSAL_FLAG LOGIC  by  3 Ma for bug 10230041 on Oct-28-2010
124 
125 16. Jan 04, 2011 Bug 10623735
126                  Description:
127                  + When Receipt and Invoice currency are different, the Service Tax Repository
128                  is not updated with the correct amounts
129                  + If there are multiple applications and unapplication, repository entries
130                  are corrupt
131                  Fix:
132                  + JAI_RGM_TRX_RECORDS is populated based on line_applied and tax_applied columns
133                  in AR_RECEIVABLE_APPLICATIONS_ALL. In case of currency difference between Invoice and
134                  Receipt, the Invoice currency is used to populate AR_RECEIVABLE_APPLICATIONS_ALL.
135                  This was not considered when calculating Transaction Amount.
136                  Multiplied by Invoice Exchange Rate if Invoice is in foreign currency or divided
137                  by Receipt/Credit Memo exchange rate if Receipt/CM is in foreign currency.
138                  + If there are multiple applications and unapplication, repository entries
139                  need to be created in the same order of application/unapplication.
140                  Hence added order by clause on aral.receivable_application_id in cursors
141                  c_get_cm_rec_app and c_get_rec_app
142 
143 17. 14-Mar-2011 ER 11821537
144                 Description: Notification # 18/2011 proposes change in Point of Taxation for Service Tax.
145                 Service Tax Liability arises on creation of Invoice and not on Receipt of Payment from Customer
146                 i.e. Accounting changes to Accrual basis from Cash Basis
147                 Fix: Commented the following procedures
148                 + delete_non_existant_cm
149                 + populate_cm_app
150                 + populate_receipt_records
151                 Added the following procedure
152                 + populate_repository - To populate Repository Entries for all eligible AR Invoice with
153                 no accounting (Interim Liability(Dr) to Liability(Cr))
154                 Service Tax Processing shall not run unless the From and To Dates are after the Effective Date
155                 for Service Tax Paoint of Taxation(This is to avoid Data corruption)
156 
157 18. 19-May-2011 Bug 11932841
158                 Description: Receipt Tax amount shall be zero in case a Receipt is applied on a AR Document with only Inclusive Taxes
159                 as Inclusive Tax is part of Line amount and there are no separate Tax Distributions.
160                 In those cases ln_eff_cr_tax_amount will be zero as ln_receipt_tax_amt will be zero.
161                 But the Inclusive Tax needs to hit the repository
162                 Fix: Modified cursor cur_get_inv_tottax_amt to get only Exclusive Tax.
163                 Calculated Repository amount separately for Inclusive and Exclusive Taxes.
164 
165 19. 11-Jul-2011 Bug 11787045
166                 Description: Credit Memo unapplication on a Receipt does not work as intended.
167                 Fix: Receipt Application ID corresponding to the unapplication is not picked by cursor c_get_rec_app.
168                 Added abs clause to ensure that Unapplication lines are picked even if tax amounts are negative.
169 
170 20. 26-JUL-2011 By mmurtuza Description: Rolling back the changes done for bug 11787045
171     Porting the changes done in 12.1 branch.
172     Issue:
173                   Service tax pending liability report is showing the post POT invoices which are completed
174                   technical details:
175                   issue is due to the jai_rgm_trx_refs.recovered_amount is not updated with the tax amount
176                   Fix:
177                   made a call to jai_rgm_trx_recording_pkg.update_reference to update the recovered_amoun in
178                   populate_repository procedure
179 
180 21. 24-May-2011 Xiao for POT change, reg bug#12533434.
181                 Fixed: Compare the invoice accounting date with POT implementation date, so that it can process
182                 transactions on cash basis as well as accrual basis.
183                 Recover code for :
184                 + delete_non_existant_cm
185                 + populate_cm_app
186                 + populate_receipt_records
187 
188 22. 29-May-2011 Xiao for POT change, reg bug#12533434.
189                 Fixed: Correct cursor get_trx_gl_date_cur, use link_to_cust_trx_line_id to get REV line accounting date.
190                        Variable ld_st_accrual_date should be initialized correctly.
191 
192 23. 30-May-2011 Xiao for POT change, reg bug#12533434.
193                 Fixed: In procedure populate_repository, correct cursor c_ar_transactions and c_ar_ref_records to fetch.
194                        eligible ar lines to populate into repository.
195                        In cursor c_get_incompleted_cm, add rgtr.line_id in the group clause.
196 
197 24. 08-Jun-2011 Xiao for POT change, reg bug#12631135.
198                 Fixed: In function is_accrual_basis,  jai_ar_trx_tax_lines should be used instead of table ra_customer_trx_lines_all
199                        as for taxes lines, they will be held in jai_ar_trx_tax_lines, not in ra_customer_trx_lines_all.
200 
201 25. 11-Jul-2011 Bug 11787045
202                 Description: Credit Memo unapplication on a Receipt does not work as intended.
203                 Fix: Receipt Application ID corresponding to the unapplication is not picked by cursor c_get_rec_app.
204                 Added abs clause to ensure that Unapplication lines are picked even if tax amounts are negative.
205 26.   21-jul-2011 vkaranam for bug#12777500
206                   Issue:
207                   Service tax pending liability report is showing the post POT invoices which are completed
208                   technical details:
209                   issue is due to the jai_rgm_trx_refs.recovered_amount is not updated with the tax amount
210                   Fix:
211                   made a call to jai_rgm_trx_recording_pkg.update_reference to update the recovered_amoun in
212                   populate_repository procedure
213 
214 27.  22-jul-2011  mmurtuza for Rolling back the changes done for the bug 11787045.
215 
216 28. 22-Aug-2011 Xiao for POT Phase III changes, reg bug#12895841
217                 Fixed: Add logic for showing transactions for AP reversal, AP claim, AR reversal, AR claim.
218 
219 29. 29-Sep-2011 Zhiwei for POT change to Open Interface, reg bug#13023443
220                 Fixed: for Tax line from Open Interface and External and GL date >= POT date should not populate repository
221 30. 21-Nov-2011 Qiong for Advanced Receipts
222 
223 31. 30-Nov-2011 Qinglei for Bug#13531399
224                 Fixed: for both recoverable and non-recovable tax, when poputlate to jai_rgm_trx_refs, use tax amount
225                 ignore the mod_cr_percentage.
226 
227 32. 05-Jan-2012 Qinglei for Bug#13556198
228                 Fixed: Receipt application/unapplication can't updated into Service Tax Repository
229 
230 33. 12-Jan-2012 Qinglei for Bug#13569242
231                 Issue: For Advance Receipt Reversal, GL INTERFACE SHOULD USE RECEIPT REVERSAL GL DATE AS ACCOUNTING DATE
232                 Fixed: Add CURSOR cur_get_rcpt_reversal_gl_date to get receipt reversal gl date.
233 
234 34. 12-Jan-2012 Qinglei for Bug#13557031
235                 Issue: When Advance Receipt Confirmation and Reversal, wrong transacton date inserted into ra_interface_lines_all.
236                 Fixed: When Receipt confirm, use receipt date insert into ar interface table; when receipt reversal, use reversal date
237                 insert into ar interface table.
238                 Rounded the amount inserted into ar interface table.
239 35. 12-Jan-2012 Qiong for bug#13540741 ST repository review shouldn't use transaction date for query
240 
241 36. 12-Jan-2012 Qiong for bug#13555753 ST proceesing report and repository should use reversal trx date and gl date
242 
243 37. 13-Jan-2012 Qinglei for Bug#13557031
244                 Fixed: Rounded the amount insert into repository for receipt apply/unapply.
245 39. 20-Jan-2012 Qiong for Bug#13569249 - TST122.XB18:GL LINE DRILLDOWN FOR RECEIPT REVERSAL HAS ERROR
246 40. 07-Feb-2012 Qiong for Bug#13579826 - TST122.XB18:Applied AR trx with cash basis can not be updated to repository
247 
248 41. 07-Feb-2012 Zhiwei Xin for Bug#13535100
249                 Fixed: AFTER POT DATA WITH EXTERNAL EVENT MUSTN'T BE UPDATED TO REPOSITORY
250 
251 42. 08-Feb-2012 Zhiwei Xin for Bug#13535519
252                 Fixed: Populate AR transaction applied to receipts into repository for pre-POT.
253 43. 09-Feb-2012 Qiong for bug#13690141
254 		Fixed: Tax amount column on ST repository UI is inaccurate
255 44. 22-Feb-2012 Qiong for bug#13730406
256 		Fixed: Document number is not generated in IL cash receipts form.
257 
258 45. 24-Feb-2012 Zhiwei Xin for bug #13777022
259                 Fixed: default ln_external_flag as null.
260 
261 46. 28-Feb-2012 Qinglei for bug#13741544
262                 Fixed: Change jai_constants.trx_type_rct_rvs to jai_constants.trx_type_adv_rvs
263                 For Advance Receipt reversal transaction.
264 
265 Future Dependencies For the release Of this Object:-
266 (Please add a row in the section below only if your bug introduces a dependency due to spec change/ A new call to a object/
267 A datamodel change )
268 ----------------------------------------------------------------------------------------------------------------------------------------------------
269 Current Version       Current Bug    Dependent         Dependency On Files       Version   Author   Date         Remarks
270 Of File                              On Bug/Patchset
271 jai_rgm_process_ar_taxes_pkg_b.sql
272 ----------------------------------------------------------------------------------------------------------------------------------------------------
273 115.0                  4146634       IN60105D2 +                                           Aiyer   27-Jan-2005   4146708 is the release bug
274                                      4146708                                                                     for SERVICE/CESS enhancement release
275 115.1                  4193633                        jai_cmn_rgm_recording_pkg  115.1     Aiyer   23-Feb-2005  Functional dependency due to spec change.
276 
277 ----------------------------------------------------------------------------------------------------------------------------------------------------
278 
279 
280 ********************************************************************************************************************************************************/
281 AS
282 
283 /*csahoo for bug#5879769...start*/
284 
285 lv_service_type_code JAI_PO_LINE_LOCATIONS.service_type_code%TYPE;
286 ln_organization_id   NUMBER;
287 ln_location_id       NUMBER;
288 lv_process_flag      VARCHAR2(15);
289 lv_process_message   VARCHAR2(4000);
290 
291 /*csahoo for bug#5879769...end*/
292 
293 
294 FUNCTION is_accrual_basis(pn_customer_trx_line_id IN NUMBER)
295 RETURN BOOLEAN
296 IS
297 
298 CURSOR get_trx_gl_date_cur IS
299 SELECT rda.gl_date
300   FROM ra_cust_trx_line_gl_dist_all rda,
301        --ra_customer_trx_lines_all    rla
302        jai_ar_trx_tax_lines         rla--Modified by Xiao for bug#12631135.
303  WHERE rda.account_class = 'REV'
304    AND rda.customer_trx_line_id = rla.link_to_cust_trx_line_id
305    -- AND rda.customer_trx_id = rla.customer_trx_id --Commented by Xiao for bug#12631135.
306    AND rla.customer_trx_line_id = pn_customer_trx_line_id;
307 
308 /*Bug 12805386 - Fetch Effective Date of Point of Taxation only if Organization Type is either 'INDIVIDUALS' or 'PARTNERSHIP FIRM'
309 or 'PROPRIETARY FIRM' and if the Service Type is one mentioned in Rule 7 of Point of Taxation Rules 2011 i.e
310 105-p, 105-q, 105-s, 105-t, 105-u, 105-za, 105-zzzzm*/
311 
312 CURSOR c_get_st_accrual_date(p_organization_id NUMBER, p_location_id NUMBER) IS
313 select to_date(attribute_value, 'DD/MM/YYYY')
314 from JAI_RGM_ORG_REGNS_V jrr, jai_rgm_definitions jrd
315 where jrr.regime_id = jrd.regime_id
316 AND jrd.regime_code = jai_constants.service_regime
317 and jrr.organization_id = p_organization_id
318 and jrr.location_id = p_location_id
319 AND jrr.attribute_code = 'EFF_DATE_ST_PT'
320 AND jrr.attribute_type_code = 'OTHERS'
321 AND jrr.registration_type = 'OTHERS'
322 AND (NOT EXISTS
323         (select '1'
324          from JAI_RGM_ORG_REGNS_V jrr, jai_rgm_definitions jrd
325          where jrr.regime_id = jrd.regime_id
326          AND jrd.regime_code = jai_constants.service_regime
327          and jrr.attribute_code IN 'INV_ORG_CLASSIFICATION'
328          and jrr.attribute_value <> 'ORGANIZATION'
329          and jrr.organization_id = p_organization_id
330          and jrr.location_id = p_location_id)
331         OR
332         NOT EXISTS
333         (select '1'
334          from JAI_RGM_ORG_REGNS_V jrr, jai_rgm_definitions jrd
335          where jrr.regime_id = jrd.regime_id
336          AND jrd.regime_code = jai_constants.service_regime
337          and jrr.attribute_code IN 'SERVICE TYPE'
338          and jrr.attribute_value <> 'OTHER'
339          and jrr.organization_id = p_organization_id
340          and jrr.location_id = p_location_id)
341        );
342 
343 
344 --Commented by zhiwei for POT bug#12925963 on 20110908 begin
345 --------------------------------------------------------------
346 /*CURSOR c_get_org_loc
347 IS
348 SELECT organization_id, location_id
349 FROM jai_ar_trxs
350 WHERE customer_trx_id IN(SELECT customer_trx_id
351                          FROM ra_customer_trx_lines
352                          WHERE customer_trx_line_id IN pn_customer_trx_line_id);*/
353 --------------------------------------------------------------
354 --Commented by zhiwei for POT bug#12925963 on 20110908 end
355 --Added by zhiwei for POT bug#12925963 on 20110908 begin
356 --------------------------------------------------------------
357 CURSOR c_get_org_loc
358 IS
359 SELECT organization_id, location_id
360 FROM jai_ar_trxs
361 WHERE customer_trx_id IN(
362                            SELECT customer_trx_id
363                            FROM ra_customer_trx_lines
364                            WHERE customer_trx_line_id IN
365                            (
366                                 select link_to_cust_trx_line_id
367                                 from jai_ar_trx_tax_lines
368                                 where customer_trx_line_id = pn_customer_trx_line_id
369                            )
370                          );
371 --------------------------------------------------------------
372 --Added by zhiwei for POT bug#12925963 on 20110908 end
373 
374 /*Bug 12805386 - End*/
375    ld_st_accrual_date DATE;
376    ld_gl_date DATE;
377    r_get_org_loc c_get_org_loc%ROWTYPE; /*Bug 12805386*/
378 
379 BEGIN
380     OPEN get_trx_gl_date_cur;
381     FETCH get_trx_gl_date_cur INTO ld_gl_date;
382     CLOSE get_trx_gl_date_cur;
383     /*Bug 12805386 -  Derive Inventory Organization ID and Location ID*/
384     OPEN c_get_org_loc;
385     FETCH c_get_org_loc INTO r_get_org_loc;
386     CLOSE c_get_org_loc;
387     /*Bug 12805386 -  Added parameters Inventory Organization ID and Location ID*/
388     OPEN c_get_st_accrual_date(r_get_org_loc.organization_id, r_get_org_loc.location_id);
389     FETCH c_get_st_accrual_date INTO ld_st_accrual_date;
390     CLOSE c_get_st_accrual_date;
391     IF ld_gl_date >= ld_st_accrual_date THEN
392        RETURN TRUE;
393     ELSE
394        RETURN FALSE;
395     END IF;
396 END is_accrual_basis;
397 
398 
399 procedure get_regime_info  (    p_regime_code       JAI_RGM_DEFINITIONS.REGIME_CODE%TYPE   ,
400                                 p_tax_type_code     JAI_CMN_TAXES_ALL.TAX_TYPE%TYPE  ,
401                                 p_regime_id OUT NOCOPY JAI_RGM_DEFINITIONS.REGIME_ID%TYPE     ,
402                                 p_error_flag OUT NOCOPY VARCHAR2                       ,
403                                 p_error_message OUT NOCOPY VARCHAR2
404                             )
405 IS
406 
407   -- Start of bug 4089440
408   /*
409   || Get the regime id based on regime code
410   */
411   CURSOR c_get_regime_id
412   IS
413   SELECT
414          regime_id
415   FROM
416          JAI_RGM_DEFINITIONS
417   WHERE
418          regime_code = p_regime_code;
419    /*
420    ||Get the meaning for a corresponding lookup_type and lookup_code
421    || TBD - Check effectivity for regime end date
422    */
423    CURSOR c_get_lookup_meaning  ( cp_lookup_type  FND_LOOKUP_VALUES.LOOKUP_TYPE%TYPE ,
424                                   cp_lookup_code  FND_LOOKUP_VALUES.LOOKUP_CODE%TYPE
425                                 )
426    IS
427    SELECT
428           meaning
429    FROM
430           fnd_lookup_values
431    WHERE
432           lookup_type   =  cp_lookup_type  AND
433           lookup_code   =  cp_lookup_code;
434 
435    /*
436    || Check whether a tax type exists in the regime registrations table for the
437    || tax types attached to a invoice tax line.
438    */
439    CURSOR c_chk_service_tax ( cp_tax_type    JAI_CMN_TAXES_ALL.TAX_TYPE%TYPE ,
440                               cp_regime_id   JAI_RGM_DEFINITIONS.REGIME_ID%TYPE
441                              )
442    IS
443    SELECT
444           1
445    FROM
446           JAI_RGM_REGISTRATIONS
447    WHERE
448           regime_id                = cp_regime_id                                  AND
449           upper(registration_type) = upper(jai_constants.regn_type_tax_types)  AND
450           upper(attribute_code)    = upper(cp_tax_type);
451 
452    lv_exists        VARCHAR2(2)                     ;
453    ln_regime_id     JAI_RGM_DEFINITIONS.REGIME_ID%TYPE      ;
454    lv_meaning       FND_LOOKUP_VALUES.MEANING%TYPE  ;
455    lv_meaning_rgm   FND_LOOKUP_VALUES.MEANING%TYPE  ;
456 
457 BEGIN
458 
459   /*################################################################################################################
460   || Initialize the variables
461   ################################################################################################################*/
462   p_error_flag    := jai_constants.successful ;
463   p_error_message := NULL                         ;
464   p_regime_id     := NULL                         ;
465 
466   /*################################################################################################################
467   || Validate Regime Setup Information
468   ################################################################################################################*/
469   /*
470   ||Get the regime id info
471   */
472   OPEN  c_get_regime_id                   ;
473   FETCH c_get_regime_id INTO ln_regime_id ;
474 
475   IF c_get_regime_id%NOTFOUND THEN
476     /*
477     ||As regime has not been defined for this tax type hence error out
478     */
479     CLOSE c_get_regime_id;
480 
481     OPEN  c_get_lookup_meaning   ( cp_lookup_type  => jai_constants.lk_type_tax_type ,
482                                    cp_lookup_code  => p_tax_type_code
483                                  );
484     FETCH  c_get_lookup_meaning INTO lv_meaning;
485     CLOSE  c_get_lookup_meaning;
486     p_error_flag     := jai_constants.expected_error;
487     p_error_message  := 'A regime has to be defined for taxes with tax type as '||lv_meaning;
488     return;
489   END IF;
490   CLOSE c_get_regime_id;
491 
492 
493 
494   /*################################################################################################################
495   || Validate Regime Registration Setup Information
496   ################################################################################################################*/
497 
498   /*
499   || Check whether a tax type exists in the regime registrations table for the
500   || tax types attached to a invoice tax line.
501   */
502   OPEN c_chk_service_tax ( cp_tax_type   => p_tax_type_code  ,
503                            cp_regime_id  => ln_regime_id
504                          );
505   FETCH c_chk_service_tax INTO lv_exists;
506   IF c_chk_service_tax%NOTFOUND THEN
507     /*
508     ||As regime has not been defined for this tax type hence raise an error.
509     */
510     CLOSE c_chk_service_tax;
511     /*
512     || Get the meaning from lookup tables for lookup type TAX_TYPE and the lookup code as the current tax type
513     */
514     OPEN  c_get_lookup_meaning   ( cp_lookup_type  => jai_constants.lk_type_tax_type ,
515                                    cp_lookup_code  => p_tax_type_code
516                                  );
517     FETCH  c_get_lookup_meaning INTO lv_meaning;
518     CLOSE  c_get_lookup_meaning;
519     /*
520     || Get the meaning from lookup tables for lookup type 'JAI_INDIA_TAX_REGIMES' and the lookup code as 'SERVICE'
521     */
522     OPEN  c_get_lookup_meaning   ( cp_lookup_type  => jai_constants.lk_type_ind_tax_rgms ,
523                                    cp_lookup_code  => p_tax_type_code
524                                  );
525     FETCH  c_get_lookup_meaning INTO lv_meaning_rgm;
526     CLOSE  c_get_lookup_meaning;
527     p_error_flag     := jai_constants.expected_error;
528     p_error_message  := 'A tax type of '|| lv_meaning ||'should be defined for the regime '||lv_meaning_rgm;
529     return;
530   END IF;
531   CLOSE c_chk_service_tax;
532 
533   p_regime_id := ln_regime_id ;
534 
535 EXCEPTION
536   WHEN OTHERS THEN
537     p_error_flag     := jai_constants.unexpected_error;
538     p_error_message  := 'Unexpected Error Occured in procedure jai_ar_rgm_processing_pkg.get_regime_info - '||substr(sqlerrm,1,300);
539 END get_regime_info;
540 
541 procedure get_ar_tax_disc_accnt  ( p_receivable_application_id             AR_RECEIVABLE_APPLICATIONS_ALL.RECEIVABLE_APPLICATION_ID%TYPE   ,
542                                    p_org_id                                AR_RECEIVABLES_TRX_ALL.ORG_ID%TYPE                              ,
543                                    p_total_disc_amount                     JAI_RGM_TRX_REFS.DISCOUNTED_AMOUNT%TYPE                         ,/*Parameter added for the bug 4193633 */
544                                    p_tax_ediscounted OUT NOCOPY AR_RECEIVABLE_APPLICATIONS_ALL.TAX_EDISCOUNTED%TYPE             ,
545                                    p_earned_disc_ccid OUT NOCOPY AR_RECEIVABLES_TRX_ALL.CODE_COMBINATION_ID%TYPE                 ,
546                                    p_tax_uediscounted OUT NOCOPY AR_RECEIVABLE_APPLICATIONS_ALL.TAX_UEDISCOUNTED%TYPE            ,
547                                    p_unearned_disc_ccid OUT NOCOPY AR_RECEIVABLES_TRX_ALL.CODE_COMBINATION_ID%TYPE                 ,
548                                    p_process_flag OUT NOCOPY VARCHAR2                                                        ,
549                                    p_process_message OUT NOCOPY VARCHAR2
550                                  )
551 
552 /*****************************************************************************************************************************************************************
553 Created By       : aiyer
554 Created Date     : 27-jan-2005
555 Enhancement Bug  : 4146634
556 Purpose          : Gte the tax earned and Unearned discounts associated with the Receivable application
557                    the jai_rgm_trx_refs and jai_rgms_trx_records appropriately.
558 Called From      : jai_cmn_rgm_recording_pkg.insert_reference
559 
560 Change History: -
561 =================
562 1    20-Feb-2005  aiyer - Bug # 4193633 - File Version# 115.1
563    Issue
564     The tax earned and unearned discount are not getting apportioned properly of service type of taxes and hence the India - Service Tax concurrent
565     ends up in a warning for records with these issues
566 
567    Reason:-
568     In case of invoices having Service taxes and other type of taxes, the tax earned and unearned discounts should be approtioned across all the type of taxes
569     (Both Service and Non Service).
570     This apportionment logic was not present initially. This needs to be added
571 
572    Fix: -
573     Modified the procedure. Did the following :-
574     1. Added a extra parameter p_total_disc_amount to the procedure.
575     2. used this parameter to apportion the tax earned discount amount and tax unearned discount amount
576 
577    Dependency Due To This Bug:
578     Dependency exists due to specification change of the current procedure.
579     Always sent the following packages together:-
580 
581       1. jai_rgm_process_ar_taxes_pkg_s.sql          (115.1)
582       2. jai_rgm_process_ar_taxes_pkg_b.sql          (115.1)
583       3. jai_rgm_trx_recording_pkg_s.sql version     (115.1)
584       4. jai_rgm_trx_recording_pkg_b.sql version     (115.1)
585 
586 
587  2.  17/04/2007   Bgowrava for forward porting bug#5989740, 11i BUG#5907436. File Version 120.2
588                  ENH: Handling Secondary and Higher Education Cess
589                 Added a input paramter cp_sh_service_edu_cess to the cursor c_fetch_inv_cm_rec.
590 
591  3.     05/06/2007       sacsethi for bug 6109941
592                          R12RUP03-ST1: CODE REVIEW COMMENTS FOR ENHANCEMENTS
593 
594        Some code was found which missed during fp of bug 5879769
595 
596 *****************************************************************************************************************************************************************/
597 
598 IS
599   CURSOR cur_get_receivable_app
600   IS
601   SELECT
602          nvl(tax_ediscounted,0)  tax_ediscounted  ,
603          nvl(tax_uediscounted,0) tax_uediscounted
604   FROM
605           ar_receivable_applications_all
606   WHERE
607           receivable_application_id = p_receivable_application_id  AND
608           org_id                    = p_org_id;
609 
610    CURSOR cur_get_disc_ccid (cp_type AR_RECEIVABLES_TRX_ALL.TYPE%TYPE,p_lookup_type ar_lookups.lookup_type%type,p_status ar_receivables_trx_all.status%TYPE )--rchandan for bug#4428980
611    IS
612    SELECT
613         code_combination_id
614    FROM
615         ar_receivables_trx_all  rtrx,
616         ar_lookups              lkup
617    WHERE
618         rtrx.type         = lkup.lookup_code        AND
619         lkup.lookup_code  = cp_type                 AND
620         lkup.lookup_type  = p_lookup_type           AND   --rchandan for bug#4428980
621         org_id            = p_org_id                AND
622         status            = p_status;      --rchandan for bug#4428980
623 
624   rec_cur_get_receivable_app    CUR_GET_RECEIVABLE_APP%ROWTYPE      ;
625   rec_cur_get_disc_ccid       CUR_GET_DISC_CCID%ROWTYPE       ;
626   ln_total_rec_disc_amt       JAI_RGM_TRX_REFS.DISCOUNTED_AMOUNT%TYPE ;
627 
628 BEGIN
629 
630  fnd_file.put_line(fnd_file.LOG,'********************* 1 START OF PROCEDURE jai_ar_rgm_processing_pkg.GET_AR_TAX_DISC_ACCNT *********************');
631 
632   /*
633   || Variable Intialization
634   */
635   p_process_flag       := jai_constants.successful     ;
636   p_process_message    := null                         ;
637 
638  fnd_file.put_line(fnd_file.LOG,' 2 Variables Initialised ');
639 
640   IF p_total_disc_amount IS NULL THEN
641     p_process_flag        := jai_constants.expected_error;
642     p_process_message     := 'Service Tax Discounted cannot be NULL ';
643     fnd_file.put_line(fnd_file.LOG,' 3 EXPECTED ERROR - Service Tax Discounted amount cannot be NULL ');
644     return;
645   END IF;
646 
647   /*
648   || Get the receivable_application Tax Earned and Tax Unearned Discount amounts
649   */
650   OPEN  cur_get_receivable_app ;
651   FETCH cur_get_receivable_app into rec_cur_get_receivable_app ;
652   IF CUR_GET_RECEIVABLE_APP%NOTFOUND THEN
653     CLOSE cur_get_receivable_app;
654     p_process_flag        := jai_constants.expected_error;
655     p_process_message     := 'Receivable Application record with the receivable_application_id  -> '||p_receivable_application_id ||' not found ';
656     fnd_file.put_line(fnd_file.LOG,' 4 EXPECTED ERROR -Receivable Application record with the receivable_application_id  -> '||p_receivable_application_id ||' not found ');
657     return;
658   END IF;
659 
660   /*
661   || Start of 4193633
662   || Apportion the discounted tax earned and discounted tax unearned based on the parameter p_total_disc_amount (total discount tax amount
663   || applicable all service taxes for a particular invoice
664   */
665   ln_total_rec_disc_amt := rec_cur_get_receivable_app.tax_ediscounted + rec_cur_get_receivable_app.tax_uediscounted    ;
666   --Added by walton for inclusive tax
667   -------------------------------------------
668   IF nvl(ln_total_rec_disc_amt,0) =0
669   THEN
670     ln_total_rec_disc_amt:=1;
671   END IF;
672   ------------------------------------------
673   p_tax_ediscounted  := ( rec_cur_get_receivable_app.tax_ediscounted  / ln_total_rec_disc_amt ) *  p_total_disc_amount ;
674   p_tax_uediscounted := ( rec_cur_get_receivable_app.tax_uediscounted / ln_total_rec_disc_amt ) *  p_total_disc_amount ;
675 
676   /*
677   || End of 4193633
678   */
679  fnd_file.put_line(fnd_file.LOG,' 5 value of p_tax_ediscounted -> '|| p_tax_ediscounted
680                                  ||', p_tax_uediscounted -> '   || p_tax_uediscounted
681                  ||', ln_total_rec_disc_amt -> '|| ln_total_rec_disc_amt
682                  ||', rec_cur_get_receivable_app.tax_ediscounted ->  '||rec_cur_get_receivable_app.tax_ediscounted
683                  ||', rec_cur_get_receivable_app.tax_uediscounted -> '||rec_cur_get_receivable_app.tax_uediscounted
684            );
685 
686   /*
687   || Get the code combination id for the Earned Discount Account
688   */
689 
690   IF rec_cur_get_receivable_app.tax_ediscounted <> 0 THEN
691 
692     OPEN  cur_get_disc_ccid ('EDISC','RECEIVABLES_TRX','A');  --rchandan for bug#4428980
693     FETCH cur_get_disc_ccid INTO rec_cur_get_disc_ccid;
694 
695     IF CUR_GET_DISC_CCID%NOTFOUND THEN
696       CLOSE cur_get_receivable_app;
697       CLOSE cur_get_disc_ccid;
698       p_process_flag        := jai_constants.expected_error  ;
699       p_process_message     := 'Earned Discount Account Setup not found in ar_receivables_trx_all ';
700       fnd_file.put_line(fnd_file.LOG,' 6 EXPECTED ERROR - Earned Discount Account Setup not found in ar_receivables_trx_all ');
701       return;
702     END IF;
703     p_earned_disc_ccid := rec_cur_get_disc_ccid.code_combination_id ;
704     CLOSE cur_get_disc_ccid;
705   END IF;
706 
707   fnd_file.put_line(fnd_file.LOG,' 6 Earned Discount Account code combination id is  '||rec_cur_get_disc_ccid.code_combination_id);
708   /*
709   || Get the code combination id for the Unearned Discount Account
710   */
711   IF rec_cur_get_receivable_app.tax_uediscounted <> 0 THEN
712 
713     OPEN  cur_get_disc_ccid ('UNEDISC','RECEIVABLES_TRX','A');   --rchandan for bug#4428980
714     FETCH cur_get_disc_ccid INTO rec_cur_get_disc_ccid;
715 
716     IF CUR_GET_DISC_CCID%NOTFOUND THEN
717       CLOSE cur_get_receivable_app;
718       CLOSE cur_get_disc_ccid;
719       p_process_flag        := jai_constants.expected_error   ;
720       p_process_message     := 'UnEarned Discount Account Setup not found in ar_receivables_trx_all ';
721       fnd_file.put_line(fnd_file.LOG,' 8 EXPECTED ERROR - UnEarned Discount Account Setup not found in ar_receivables_trx_all ');
722       return;
723     END IF;
724     p_unearned_disc_ccid := rec_cur_get_disc_ccid.code_combination_id ;
725     CLOSE cur_get_disc_ccid;
726 
727   END IF;
728   fnd_file.put_line(fnd_file.LOG,' 9 Unearned Account CCID is -> '||rec_cur_get_disc_ccid.code_combination_id);
729   CLOSE cur_get_receivable_app;
730  fnd_file.put_line(fnd_file.LOG,' 10 Value of out variables  p_tax_ediscounted       ->' ||   p_tax_ediscounted
731                 ||', p_earned_disc_ccid     ->' ||    p_earned_disc_ccid
732                 ||', p_tax_uediscounted     ->' ||    p_tax_uediscounted
733                 ||', p_unearned_disc_ccid   ->' ||    p_unearned_disc_ccid
734                 ||', p_process_flag         ->' ||    p_process_flag
735                 ||', p_process_message      ->' ||    p_process_message
736           );
737  fnd_file.put_line(fnd_file.LOG,'********************* 10 END OF PROCEDURE jai_ar_rgm_processing_pkg.GET_AR_TAX_DISC_ACCNT *********************');
738 
739 EXCEPTION
740   WHEN OTHERS THEN
741     p_process_flag        := jai_constants.unexpected_error ;
742     p_process_message     := 'Unexpeced error occured in procedure get_ar_tax_disc_accnt  for receivable_application_id -> '||p_receivable_application_id ||substr(SQLERRM,1,300);
743 
744 END get_ar_tax_disc_accnt  ;
745 
746 
747 
748 procedure populate_inv_cm_references   ( p_regime_id         IN  JAI_RGM_DEFINITIONS.REGIME_ID%TYPE                  ,
749                                          p_organization_type IN  JAI_RGM_PARTIES.ORGANIZATION_TYPE%TYPE      ,
750                                          p_from_date         IN  DATE                                        ,
751                                          p_to_date           IN  DATE                                        ,
752                                          p_org_id            IN  RA_CUSTOMER_TRX_ALL.ORG_ID%TYPE             ,
753                                          p_batch_id          IN  JAI_RGM_TRX_REFS.BATCH_ID%TYPE              ,
754                                          p_source            IN  varchar2, --File.Sql.35 Cbabu  jai_constants.SOURCE_AR%TYPE                ,
755                                          p_process_flag OUT NOCOPY VARCHAR2                                    ,
756                                          p_process_message OUT NOCOPY VARCHAR2,
757                                          p_organization_id  IN JAI_RGM_PARTIES.ORGANIZATION_ID%TYPE DEFAULT NULL
758                                        )
759 IS
760   /******************************
761   ||Variable Declaration Section
762   *******************************/
763   ln_reference_id                     JAI_RGM_TRX_REFS.REFERENCE_ID%TYPE                              ;
764   lv_process_flag                     VARCHAR2(2)                                                     ;
765   lv_process_message                  VARCHAR2(1996)                                                  ;
766   ln_invoices_processed               NUMBER(10)                                                      ;
767   ln_receipts_processed               NUMBER(10)                                                      ;
768 
769   /******************************
770   ||Cursor Declarations Section
771   *******************************/
772 
773   CURSOR c_fetch_inv_cm_rec  ( cp_invoice_type     varchar2, --File.Sql.35 Cbabu  jai_constants.AR_INVOICE_TYPE_INV%TYPE           ,
774                                cp_cm_type          varchar2, --File.Sql.35 Cbabu  jai_constants.AR_INVOICE_TYPE_CM%TYPE            ,
775                                cp_dm_type          varchar2, -- Add by Xiao for bug#6773751 on 20-Dec-09
776                                cp_service_tax      varchar2, --File.Sql.35 Cbabu  jai_constants.TAX_TYPE_SERVICE%TYPE              ,
777                                cp_service_edu_cess varchar2, --File.Sql.35 Cbabu  jai_constants.TAX_TYPE_SERVICE_EDU_CESS%TYPE
778                                cp_sh_service_edu_cess  JAI_CONSTANTS.TAX_TYPE_SH_SERVICE_EDU_CESS%TYPE        --Added By Bgowrava for forward porting bug#5989740
779                              )
780   IS
781   SELECT
782       trx.customer_trx_id                                                                             ,
783       trx.invoice_currency_code                                                                       ,
784       trx.exchange_date                                                                               ,
785       trx.exchange_rate                                                                               ,
786       trx.org_id                                                                                      ,
787       trx.cust_trx_type_id                                                                            ,
788       trx.previous_customer_trx_id                                                                    ,
789       nvl(trx.bill_to_customer_id,trx.ship_to_customer_id) customer_id                                ,
790       nvl(trx.bill_to_site_use_id,trx.ship_to_site_use_id) customer_site_id                           ,
791       jtc.tax_type                                         tax_type                                   ,
792       nvl(decode(upper(trx_types.type),cp_cm_type,'Y','N'),'N')   reversal_flag                       ,
793       jtrxl.inventory_item_id                                                                         ,
794       jtrxtl.customer_trx_line_id                                                                     ,
795       jtrxtl.tax_id                                                                                   ,
796       jtrxtl.tax_rate                                                                                 ,
797       jtrxtl.tax_amount                                                                               ,
798       jtrxtl.func_tax_amount                                                                          ,
799       jtrxtl.base_tax_amount                                                                          ,
800       decode(upper(trx_types.type),cp_invoice_type,nvl(jtc.mod_cr_percentage,0),100)  mod_cr_percentage      ,
801       jtrxtl.link_to_cust_trx_line_id,
802       nvl(jtc.reverse_charge_flag,'N') reverse_charge_flag --Added by Qiong for reverse charge bug#16001407
803   FROM
804       ra_customer_trx_all          trx                                                                ,
805       JAI_AR_TRXS        jtrx                                                               ,
806       ra_cust_trx_types_all        trx_types                                                          ,
807       JAI_AR_TRX_LINES  jtrxl                                                              ,
808       JAI_AR_TRX_TAX_LINES  jtrxtl                                                             ,
809       ra_customer_trx_lines_all    rctla                                                              , /*14121914*/
810       JAI_CMN_TAXES_ALL              jtc                                                       ,
811       ra_cust_trx_line_gl_dist_all   gd--Added by Qiong for bug13540741
812   WHERE
813       trx.org_id                   = nvl(p_org_id,trx.org_id)                                         AND
814       trx.complete_flag            ='Y'                                                               AND
815       trx.customer_trx_id          = jtrx.customer_trx_id                                             AND
816       jtrx.organization_id         = p_organization_id                                                AND/*5879769*/
817      /* nvl(jtrx.tax_amount,0)       <> 0                                                               AND *//*Safeguard against invoice tax amount being null or zero and service type of taxes still existing at tax level */
818      /*trunc(trx.creation_date)*/
819       --Modified by qiong for bug13540741Changed trx.trx_date to gd.gl_date
820       trunc(gd.gl_date)    BETWEEN trunc(p_from_date) and trunc(p_to_date) AND  /*Commented by nprashar for bug # 6997453*/
821       upper(trx_types.type)        IN (cp_invoice_type,cp_cm_type,cp_dm_type)                         AND -- Add by Xiao for bug#6773751 on 20-Dec-09
822       trx_types.cust_trx_type_id   = trx.cust_trx_type_id                                             AND
823       trx_types.org_id             = trx.org_id                                                       AND
824       jtrx.customer_trx_id         = jtrxl.customer_trx_id                                            AND
825       jtrxl.customer_trx_line_id   = jtrxtl.link_to_cust_trx_line_id                                  AND
826       /*14121914 - Start*/
827       jtrxl.customer_trx_line_id   = rctla.customer_trx_line_id                                       AND
828       (
829          (rctla.accounting_rule_id is not null
830           AND
831           (rctla.autorule_complete_flag IS NULL
832           OR
833           rctla.autorule_complete_flag = 'Y')
834          )
835          OR
836          rctla.accounting_rule_id is null
837       )                                                                                               AND
838       /*14121914 - End*/
839       jtrxtl.tax_id                = jtc.tax_id                                                       AND
840       --Added by Qiong for bug13540741 begin
841       -----------------------------------------------
842       gd.customer_trx_id           = trx.customer_trx_id                                              AND
843       gd.account_class             = 'REC'                                                            AND
844       gd.latest_rec_flag           = 'Y'                                                              AND
845       -----------------------------------------------
846       --Added by Qiong for bug13540741 end
847       --Commented by zhiwei for POT bug#12925963 on 20110908 begin
848       -----------------------------------------------------------
849       --upper(jtc.tax_type)          IN ( cp_service_tax,cp_service_edu_cess ,cp_sh_service_edu_cess)   AND      -- cp_sh_service_edu_cess Bgowrava for forward porting bug#5989740                       AND
850       -----------------------------------------------------------
851       --Commented by zhiwei for POT bug#12925963 on 20110908 begin
852       --Added by zhiwei for POT bug#12925963 on 20110908 begin
853       -----------------------------------------------------------
854       upper(jtc.tax_type)          IN ( upper(cp_service_tax),cp_service_edu_cess ,cp_sh_service_edu_cess)   AND
855       -----------------------------------------------------------
856       --Added by zhiwei for POT bug#12925963 on 20110908 end
857       (  /**** Check that in case of INV mod_Cr_percentage should be > 0 and no check in case of CM ****/
858         (
859           upper(trx_types.type)    = cp_cm_type
860         )                                                                                             OR
861         (
862           upper(trx_types.type)    = cp_invoice_type                                                  /*AND
863           nvl(jtc.mod_cr_percentage,0) > 0*/--Commented by Qiong for bug13690141
864         )                                                                                       OR
865    /* Added by Xiao for bug#6773751 */
866         (
867           upper(trx_types.type)    = cp_dm_type
868         )
869       )                                                                                              AND
870       NOT EXISTS                   ( SELECT  /*A ref of invoice/cm should not exist in the reference table */
871                                              1
872                                      FROM    jai_rgm_trx_refs  rgtr
873                                      WHERE
874                                              rgtr.source       = p_source                             AND
875                                              rgtr.invoice_id   = trx.customer_trx_id                  AND
876                                              rgtr.line_id      = jtrxtl.customer_trx_line_id          AND
877                                              rgtr.item_line_id = jtrxtl.link_to_cust_trx_line_id      AND
878                                              rgtr.tax_id       = jtrxtl.tax_id
879                                    )
880       --Commented by zhiwei for POT change Bug#13023443 on 20110929 begin
881       -------------------------------------------------------------------------------------------------------------------------------------------
882       --AND    NVL(jtrxl.INTERFACE_EVENT,'####') <> 'EXTERNAL' --Added by zhiwei for Bug#12718676 service tax process should not pick up external
883       -------------------------------------------------------------------------------------------------------------------------------------------
884       --Commented by zhiwei for POT change Bug#13023443 on 20110929 end
885   ORDER BY
886             trx_types.type desc;
887 
888     --Added by zhiwei for POT change Bug#13023443 on 20110929 begin
889     ---------------------------------------------------------------------------
890     CURSOR chk_external_event(cn_customer_trx_line_id in number)
891     IS
892     select 1
893     from JAI_AR_TRX_LINES line
894     where customer_trx_line_id = cn_customer_trx_line_id --rec_c_fetch_inv_cm_rec.link_to_cust_trx_line_id
895     and   NVL(interface_flag, '###') = 'Y'
896     and   NVL(interface_event,'###') = JAI_OPEN_API_PKG.GV_TAXABLE_EVENT_EXTERNAL;
897 
898     ln_external_flag number;
899     -----------------------------------------------------
900     --Added by zhiwei for POT change Bug#13023443 on 20110929 end
901     -----------------------------------------------------
902 
903     --Added by Wenqiong for Advance Rceipt on Service Tax begin
904     ---------------------------------------------------------------------------
905     CURSOR c_fetch_receipt_rec
906     IS
907     SELECT
908       jcra.cash_receipt_id,
909       jcra.currency_code,
910       jcra.exchange_date ,
911       jcra.exchange_rate ,
912       jcra.org_id,
913       jcra.customer_id,
914       acra.customer_site_use_id ,
915       jtc.tax_type ,
916       jdt.doc_tax_id,
917       jdt.tax_id ,
918       jdt.tax_rate,
919       nvl(jtc.mod_cr_percentage,0) mod_cr_percentage ,
920       jdt.tax_amt tax_amount,
921       jdt.func_tax_amt,
922       acra.amount receipt_amount
923     FROM    ar_cash_receipts_all       acra,
924             jai_ar_cash_receipts_all   jcra,
925             jai_cmn_document_taxes     jdt     ,
926             JAI_CMN_TAXES_ALL          jtc     ,
927             jai_regime_tax_types_v     jrttv
928     WHERE acra.cash_receipt_id = jcra.cash_receipt_id
929     AND   jdt.tax_id        = jtc.tax_id
930     AND    jtc.tax_type      = jrttv.tax_type
931     AND    jdt.source_doc_id = jcra.cash_receipt_id
932     AND    jrttv.regime_code = jai_constants.service_regime
933     AND    jdt.source_doc_type  = JAI_CONSTANTS.ar_cash
934     AND    jcra.CONFIRM_FLAG = 'Y'
935     AND    jcra.organization_id = p_organization_id
936     AND    jcra.org_id = nvl(p_org_id,jcra.org_id)
937     AND NOT EXISTS (  SELECT  /*A ref of receipt should not exist in the reference table */
938                                1
939                         FROM   jai_rgm_trx_refs  rgtr
940                        WHERE
941                                rgtr.source       = p_source                             AND
942                                rgtr.invoice_id   = acra.cash_receipt_id                 AND
943                                rgtr.line_id      = jdt.doc_tax_id                       AND
944                                rgtr.tax_id       = jdt.tax_id
945                      );
946     --Added by Wenqiong for Advance Rceipt on Service Tax end
947     ---------------------------------------------------------------------------
948 BEGIN
949 
950 
951   /*****
952   ||Based on the input parameters get invoice details
953   ||for which localization has corresponding service tax
954   ******/
955 
956   fnd_file.put_line(fnd_file.LOG,'1 Entering procedure : jai_ar_rgm_processing_pkg.populate_inv_cm_references' );
957 
958 fnd_file.put_line(fnd_file.LOG,'p_org_id:'||p_org_id );
959   fnd_file.put_line(fnd_file.LOG,'p_organization_id:'||p_organization_id );
960 
961   /*
962   || Variable Initialization
963   */
964 
965   lv_process_flag       := jai_constants.successful  ;
966   lv_process_message    := NULL                      ;
967 
968   p_process_flag        := lv_process_flag           ;
969   p_process_message     := lv_process_message        ;
970 
971   ln_invoices_processed := 0                         ;
972   ln_receipts_processed := 0                         ;--Added by Wenqiong for Advanced Receipt
973 
974   FOR  rec_c_fetch_inv_cm_rec IN c_fetch_inv_cm_rec ( cp_invoice_type     => upper(jai_constants.ar_invoice_type_inv)           ,
975                                                       cp_cm_type          => upper(jai_constants.ar_invoice_type_cm)            ,
976                                                       cp_dm_type          => upper(jai_constants.ar_doc_type_dm)            , --Added by Xiao for bug#6773751
977                                                       cp_service_tax      => upper(jai_constants.tax_type_service)              ,
978                                                       cp_service_edu_cess => upper(jai_constants.tax_type_service_edu_cess),
979                                                       cp_sh_service_edu_cess   => upper(jai_constants.tax_type_sh_service_edu_cess)        --Added by Bgowrava for forward porting bug#5989740
980                                                      )
981   LOOP
982 
983     fnd_file.put_line(fnd_file.LOG,'2 processing record, customer_trx_id '||rec_c_fetch_inv_cm_rec.customer_trx_id
984                       ||', invoice line id '||rec_c_fetch_inv_cm_rec.link_to_cust_trx_line_id
985                       ||', tax line id '||rec_c_fetch_inv_cm_rec.customer_trx_line_id
986                       ||', tax_id '||   rec_c_fetch_inv_cm_rec.tax_id
987                       ||', tax rate '||rec_c_fetch_inv_cm_rec.tax_rate
988                       ||', transactional tax amount '||rec_c_fetch_inv_cm_rec.tax_amount
989                       ||', functional tax amount '||rec_c_fetch_inv_cm_rec.func_tax_amount
990                       ||', currency '||rec_c_fetch_inv_cm_rec.invoice_currency_code
991                       ||', recoverable percentage '||rec_c_fetch_inv_cm_rec.mod_cr_percentage
992                       ||', recoverable_amount '||rec_c_fetch_inv_cm_rec.tax_amount * (rec_c_fetch_inv_cm_rec.mod_cr_percentage/100)
993                      );
994 
995 	--Added by Qiong for reverse charge bug#16001407 start
996 	if rec_c_fetch_inv_cm_rec.reverse_charge_flag = 'Y' then
997 	   null;
998   else
999   --Added by Qiong for reverse charge bug#16001407  end
1000 
1001 
1002     /****
1003     ||insert the invoices and credit memo's into the jai_rgm_trx_refs
1004     ||using the procedure jai_cmn_rgm_recording_pkg.insert_reference
1005     *****/
1006     fnd_file.put_line(fnd_file.LOG,'3 before call to procedure jai_cmn_rgm_recording_pkg.insert_reference ');
1007 
1008     savepoint before_ref_inv_cm;
1009 
1010 	ln_external_flag := null; --Added by zhiwei.xin for bug 13777022 on 24-Feb-2012
1011 
1012     --Added by zhiwei for POT change Bug#13023443 on 20110929 begin
1013     ------------------------------------------------------------------------------
1014     open chk_external_event(rec_c_fetch_inv_cm_rec.link_to_cust_trx_line_id);
1015     fetch chk_external_event into ln_external_flag;
1016     close chk_external_event;
1017 
1018     /*
1019           For Service Tax, if it 1) from Open Interface
1020                                  2) External
1021                                  3) GL >= POT
1022                            Repository should not be populated.
1023     */
1024     IF      nvl(ln_external_flag,0) = 1
1025        and  is_accrual_basis(rec_c_fetch_inv_cm_rec.customer_trx_line_id)
1026       THEN
1027 
1028         --ROLLBACK to before_ref_inv_cm;  comment out by zhiwei.xin for bug 13535100 on 07-Feb-2012
1029         fnd_file.put_line( fnd_file.log, '3.5 continue next line process for line 1)from OpenAPi and 2)External and 3)GL date >= POT date should not populate repo ');
1030       --END IF; comment out by zhiwei.xin for bug 13535100 on 07-Feb-2012
1031     ------------------------------------------------------------------------------
1032     --Added by zhiwei for POT change Bug#13023443 on 20110929 end
1033 
1034     ELSE -- added by zhiwei.xin for bug 13535100 on 07-Feb-2012
1035 
1036     jai_cmn_rgm_recording_pkg.insert_reference (
1037                                                    p_reference_id           =>    ln_reference_id                                                                         ,
1038                                                    p_organization_id        =>    p_organization_id                                                                       ,/*5879769*/
1039                                                    p_source                 =>    p_source                                                                                ,
1040                                                    p_invoice_id             =>    rec_c_fetch_inv_cm_rec.customer_trx_id                                                  ,
1041                                                    p_line_id                =>    rec_c_fetch_inv_cm_rec.customer_trx_line_id                                             ,
1042                                                    p_tax_type               =>    rec_c_fetch_inv_cm_rec.tax_type                                                         ,
1043                                                    p_tax_id                 =>    rec_c_fetch_inv_cm_rec.tax_id                                                           ,
1044                                                    p_tax_rate               =>    rec_c_fetch_inv_cm_rec.tax_rate                                                         ,
1045                                                    p_recoverable_ptg        =>    rec_c_fetch_inv_cm_rec.mod_cr_percentage                                                ,
1046                                                    p_party_type             =>    jai_constants.party_type_customer                                                       ,
1047                                                    p_party_id               =>    rec_c_fetch_inv_cm_rec.customer_id                                                      ,
1048                                                    p_party_site_id          =>    rec_c_fetch_inv_cm_rec.customer_site_id                                                 ,
1049                                                    p_trx_tax_amount         =>    rec_c_fetch_inv_cm_rec.tax_amount                                                       ,
1050                                                    p_trx_currency           =>    rec_c_fetch_inv_cm_rec.invoice_currency_code                                            ,
1051                                                    p_curr_conv_date         =>    rec_c_fetch_inv_cm_rec.exchange_date                                                    ,
1052                                                    p_curr_conv_rate         =>    rec_c_fetch_inv_cm_rec.exchange_rate                                                    ,
1053                            -- Replaced tax_amount by func_tax_amount for Bug 7522584
1054                                                    p_tax_amount             =>    rec_c_fetch_inv_cm_rec.func_tax_amount /** (rec_c_fetch_inv_cm_rec.mod_cr_percentage/100)*/ ,
1055                                                    p_recoverable_amount     =>    rec_c_fetch_inv_cm_rec.tax_amount /** (rec_c_fetch_inv_cm_rec.mod_cr_percentage/100)*/      ,
1056                                                    --Commented by Qinglei on 30-Dec-2011 for bug#13531399
1057                                                    p_recovered_amount       =>    0                                                                                       ,
1058                                                    p_item_line_id           =>    rec_c_fetch_inv_cm_rec.link_to_cust_trx_line_id                                         ,
1059                                                    p_item_id                =>    rec_c_fetch_inv_cm_rec.inventory_item_id                                                ,
1060                                                    p_taxable_basis          =>    rec_c_fetch_inv_cm_rec.base_tax_amount                                                  ,
1061                                                    p_parent_reference_id    =>    NULL                                                                                    ,
1062                                                    p_reversal_flag          =>    rec_c_fetch_inv_cm_rec.reversal_flag                                                    ,
1063                                                    p_batch_id               =>    p_batch_id                                                                              ,
1064                                                    p_process_flag           =>    lv_process_flag                                                                         ,
1065                                                    p_process_message        =>    lv_process_message
1066                                                );
1067 
1068     fnd_file.put_line(fnd_file.LOG,'4 returned from procedure jai_cmn_rgm_recording_pkg.insert_reference, lv_process_flag - '||lv_process_flag
1069                                    ||'lv_process_message - '||lv_process_message);
1070 
1071 
1072       IF lv_process_flag = jai_constants.expected_error    OR
1073          lv_process_flag = jai_constants.unexpected_error
1074       THEN
1075         /*
1076         || as Returned status is an error hence:-
1077         ||1. Rollback to save point
1078         ||2. Set out variables p_process_flag and p_process_message accordingly
1079         */
1080         ROLLBACK to before_ref_inv_cm;
1081         fnd_file.put_line( fnd_file.log, '5 error in call to jai_cmn_rgm_recording_pkg.insert_reference - lv_process_flag '||lv_process_flag
1082                                           ||', lv_process_message'||lv_process_message);
1083         p_process_flag    := lv_process_flag    ;
1084         p_process_message := lv_process_message ;
1085       END IF;
1086 
1087     ln_invoices_processed := ln_invoices_processed   + 1;
1088     fnd_file.put_line(fnd_file.LOG,'6 inserted record in jai_rgm_trx_refs with reference_id '||ln_reference_id  );
1089 
1090     END IF;  -- added by zhiwei.xin for bug 13535100 on 07-Feb-2012
1091     end if;--Added by qiong.liu for bug 16001407
1092 
1093   END LOOP;
1094 
1095   fnd_file.put_line(fnd_file.LOG,'5 End of procedure : jai_rgm_process_ar.populate_inv_cm_references, number of invoices/CM processed '||ln_invoices_processed );
1096 
1097   --Added by Wenqiong begin
1098   FOR  rec_c_fetch_receipt_rec IN c_fetch_receipt_rec
1099   LOOP
1100 
1101     fnd_file.put_line(fnd_file.LOG,'2.1 processing record, cash_receipt_id '||rec_c_fetch_receipt_rec.cash_receipt_id
1102                       ||', tax_id '||   rec_c_fetch_receipt_rec.tax_id
1103                       ||', tax rate '||rec_c_fetch_receipt_rec.tax_rate
1104                       ||', transactional tax amount '||rec_c_fetch_receipt_rec.tax_amount
1105                       ||', currency '||rec_c_fetch_receipt_rec.currency_code
1106                       ||', recoverable percentage '||rec_c_fetch_receipt_rec.mod_cr_percentage
1107                       ||', recoverable_amount '||rec_c_fetch_receipt_rec.tax_amount * (rec_c_fetch_receipt_rec.mod_cr_percentage/100)
1108                      );
1109 
1110     /****
1111     ||insert the receipts into the jai_rgm_trx_refs
1112     ||using the procedure jai_cmn_rgm_recording_pkg.insert_reference
1113     *****/
1114     fnd_file.put_line(fnd_file.LOG,'2.2 before call to procedure jai_cmn_rgm_recording_pkg.insert_reference ');
1115     savepoint before_ref_receipt;
1116     jai_cmn_rgm_recording_pkg.insert_reference (
1117                                                    p_reference_id           =>    ln_reference_id                                                                         ,
1118                                                    p_organization_id        =>    p_organization_id                                                                       ,
1119                                                    p_source                 =>    p_source                                                                                ,
1120                                                    p_invoice_id             =>    rec_c_fetch_receipt_rec.cash_receipt_id                                                  ,
1121                                                    p_line_id                =>    rec_c_fetch_receipt_rec.doc_tax_id                                                     ,
1122                                                    p_tax_type               =>    rec_c_fetch_receipt_rec.tax_type                                                         ,
1123                                                    p_tax_id                 =>    rec_c_fetch_receipt_rec.tax_id                                                           ,
1124                                                    p_tax_rate               =>    rec_c_fetch_receipt_rec.tax_rate                                                         ,
1125                                                    p_recoverable_ptg        =>    rec_c_fetch_receipt_rec.mod_cr_percentage                                                ,
1126                                                    p_party_type             =>    jai_constants.party_type_customer                                                       ,
1127                                                    p_party_id               =>    rec_c_fetch_receipt_rec.customer_id                                                      ,
1128                                                    p_party_site_id          =>    rec_c_fetch_receipt_rec.customer_site_use_id                                                 ,
1129                                                    p_trx_tax_amount         =>    rec_c_fetch_receipt_rec.tax_amount                                                       ,
1130                                                    p_trx_currency           =>    rec_c_fetch_receipt_rec.currency_code                                            ,
1131                                                    p_curr_conv_date         =>    rec_c_fetch_receipt_rec.exchange_date                                                    ,
1132                                                    p_curr_conv_rate         =>    rec_c_fetch_receipt_rec.exchange_rate                                                    ,
1133                                                    p_tax_amount             =>    rec_c_fetch_receipt_rec.func_tax_amt                                                     ,
1134                                                    p_recoverable_amount     =>    0                                                       ,
1135                                                    p_recovered_amount       =>    0
1136                                                    /*
1137                                                    Item_line_id should be checked in jai_trx_repo_extract_pkg.get_doc_from_reference, if it is null,
1138                                                    can't pass it. But, no item exists in receipts.So, assign hardcode 1 to item_line_id.
1139                                                    Added by Qiong
1140                                                    */                                                                                        ,
1141                                                    p_item_line_id           =>    1,
1142                                                    p_item_id                =>    NULL,
1143                                                    p_taxable_basis          =>    rec_c_fetch_receipt_rec.receipt_amount                                                  ,
1144                                                    p_parent_reference_id    =>    NULL                                                                                    ,
1145                                                    p_reversal_flag          =>    NULL,
1146                                                    p_batch_id               =>    p_batch_id                                                                              ,
1147                                                    p_process_flag           =>    lv_process_flag                                                                         ,
1148                                                    p_process_message        =>    lv_process_message
1149                                                );
1150 
1151     fnd_file.put_line(fnd_file.LOG,'2.3 returned from procedure jai_cmn_rgm_recording_pkg.insert_reference, lv_process_flag - '||lv_process_flag
1152                                    ||'lv_process_message - '||lv_process_message);
1153 
1154 
1155       IF lv_process_flag = jai_constants.expected_error    OR
1156          lv_process_flag = jai_constants.unexpected_error
1157       THEN
1158         /*
1159         || as Returned status is an error hence:-
1160         ||1. Rollback to save point
1161         ||2. Set out variables p_process_flag and p_process_message accordingly
1162         */
1163         ROLLBACK to before_ref_receipt;
1164         fnd_file.put_line( fnd_file.log, '2.4 error in call to jai_cmn_rgm_recording_pkg.insert_reference - lv_process_flag '||lv_process_flag
1165                                           ||', lv_process_message'||lv_process_message);
1166         p_process_flag    := lv_process_flag    ;
1167         p_process_message := lv_process_message ;
1168       END IF;
1169 
1170     ln_receipts_processed := ln_receipts_processed   + 1;
1171     fnd_file.put_line(fnd_file.LOG,'2.5 inserted record in jai_rgm_trx_refs with reference_id '||ln_reference_id  );
1172   END LOOP;
1173   --Added by Wenqiong End
1174 EXCEPTION
1175   WHEN OTHERS THEN
1176 
1177     lv_process_flag    := jai_constants.unexpected_error;
1178     lv_process_message := 'Unexpected error occured while processing jai_ar_rgm_processing_pkg.populate_inv_cm_references'||substr(SQLERRM,1,500) ;
1179     ROLLBACK to before_ref_inv_cm;
1180 
1181 END populate_inv_cm_references;
1182 
1183 procedure delete_non_existant_cm ( p_regime_id          IN  JAI_RGM_DEFINITIONS.REGIME_ID%TYPE                  ,
1184                                    p_organization_type  IN  JAI_RGM_PARTIES.ORGANIZATION_TYPE%TYPE      ,
1185                                    p_from_date          IN  DATE                                        ,
1186                                    p_to_date            IN  DATE                                        ,
1187                                    p_org_id             IN  RA_CUSTOMER_TRX_ALL.ORG_ID%TYPE             ,
1188                                    p_source             IN  varchar2, --File.Sql.35 Cbabu  jai_constants.SOURCE_AR%TYPE                ,
1189                                    p_batch_id           IN  JAI_RGM_TRX_REFS.BATCH_ID%TYPE              ,
1190                                    p_process_flag OUT NOCOPY VARCHAR2                                    ,
1191                                    p_process_message OUT NOCOPY VARCHAR2
1192                                  ,p_organization_id    JAI_RGM_PARTIES.ORGANIZATION_ID%TYPE DEFAULT NULL)
1193 IS
1194 
1195   ln_eff_cm_tax_amount              AR_RECEIVABLE_APPLICATIONS_ALL.TAX_APPLIED%TYPE                 ;
1196   ln_repository_id                  JAI_RGM_TRX_RECORDS.REPOSITORY_ID%TYPE                          ;
1197   ln_amount                         JAI_RGM_TRX_RECORDS.DEBIT_AMOUNT%TYPE                           ;
1198   ln_discounted_amount              JAI_RGM_TRX_REFS.DISCOUNTED_AMOUNT%TYPE                         ;
1199   ln_err_cm_customer_trx_id         JAI_RGM_TRX_REFS.INVOICE_ID%TYPE                                ;
1200   ln_set_save_point                 JAI_RGM_TRX_REFS.INVOICE_ID%TYPE                                ;
1201   lv_process_flag                   VARCHAR2(2)                                                     ;
1202   lv_process_message                VARCHAR2(1996)                                                  ;
1203   lv_source_trx_type                VARCHAR2(50)                                                    ;
1204   ln_uncommitted_transactions       NUMBER(10)                                    := 0              ;
1205   lv_attribute_context              jai_rgm_trx_records.attribute_context%TYPE ; --rchandan for bug#4428980
1206   lv_source_table                   jai_rgm_trx_records.source_table_name%TYPE ;  --rchandan for bug#4428980
1207 
1208   /* Added by Ramananda for bug#4407165 */
1209   lv_object_name CONSTANT VARCHAR2(61) := 'jai_ar_rgm_processing_pkg.delete_non_existant_cm';
1210 
1211   /*
1212   || Get all the cm's which exist in the reference table jai_rgm_trx_refs and have been incompleted/incompleted and deleted from AR base table
1213   || IF a CM has been incompleted then it would exist with complete_flag = 'N' . if a CM has been incompleted and deleted then it would not exist
1214   || in the ra_customer_trx_all table.
1215   */
1216   CURSOR c_get_incompleted_cm
1217   IS
1218   SELECT
1219          rgtr.invoice_id    cm_customer_trx_id /*,
1220          rgtr.reference_id  cm_reference_id    */
1221          , rgtr.line_id --Add by Xiao for POT change, reg bug#12533434 on 16-May-2011
1222   FROM
1223          jai_rgm_trx_refs rgtr
1224   WHERE
1225          rgtr.source          = p_source    AND
1226          rgtr.organization_id     = p_organization_id      AND /*5879769*/
1227          rgtr.reversal_flag       = 'Y'         AND
1228      nvl(rgtr.recovered_amount,0)   <> 0          AND
1229          NOT EXISTS ( SELECT
1230                                 1
1231                       FROM
1232                                 ra_customer_trx_all          trx       ,
1233                                 ra_cust_trx_types_all        trx_types
1234                       WHERE
1235                                 trx.customer_trx_id          = rgtr.invoice_id                          AND
1236                                 trx_types.cust_trx_type_id   = trx.cust_trx_type_id                     AND
1237                                 trx_types.org_id             = trx.org_id                               AND
1238                                 upper(trx_types.type)        = upper(jai_constants.ar_invoice_type_cm)  AND
1239                                 trx.complete_flag            = 'Y'
1240                     )
1241   GROUP BY
1242            rgtr.invoice_id
1243          , rgtr.line_id; --Add by Xiao for POT change, reg bug#12533434 on 16-May-2011
1244   /*
1245   || Get all the data of the incompleted CM from the
1246   || repository, so that the same record with an exactly opposite amount can be passed. This would be the CM-CM-REV record
1247   */
1248   CURSOR c_get_cm_cm_app_rec ( /*cp_cm_reference_id       JAI_RGM_TRX_RECORDS.REFERENCE_ID%TYPE ,*/
1249                                cp_cm_customer_trx_id    JAI_RGM_TRX_REFS.INVOICE_ID%TYPE,
1250              cp_attribute_context     jai_rgm_trx_records.trx_reference_context%TYPE  --rchandan for bug#4428980 --Bo Li for Bug9305067 change attribute_cotext to trx_reference_context
1251                              )
1252   IS
1253   SELECT
1254          *
1255   FROM
1256          jai_rgm_trx_records
1257   WHERE
1258          trx_reference_context =  cp_attribute_context                   AND --Bo Li for Bug9305067 change attribute_cotext to trx_reference_context
1259          trx_reference2        =  cp_cm_customer_trx_id    --Bo Li for Bug9305067 change attribute2 to trx_reference2
1260          /*  AND reference_id      =  cp_cm_reference_id    */;
1261 
1262   /*
1263   || Update all the credit memo reference records.
1264   */
1265   CURSOR cur_upd_cm_ref ( cp_cm_customer_trx_id  JAI_RGM_TRX_REFS.INVOICE_ID%TYPE )
1266   IS
1267   SELECT
1268      *
1269   FROM
1270          jai_rgm_trx_refs
1271   WHERE
1272      invoice_id                     = cp_cm_customer_trx_id AND
1273      nvl(recovered_amount,0)   <> 0       ;
1274 
1275   /*
1276   || Get all the data of the CM applied to invoices (i.e CM-INV-APP) from the repository.
1277   || So that the same record with an exactly opposite amount can be passed. This would be the new CM-INV-REV record
1278   */
1279   CURSOR c_get_cm_inv_app_rec ( cp_cm_customer_trx_id  JAI_RGM_TRX_REFS.INVOICE_ID%TYPE
1280                                -- cp_attribute_context     jai_rgm_trx_records.attribute_context%TYPE ) --Commented by Xiao for bug#6773751
1281                                )
1282   IS
1283   SELECT
1284          *
1285   FROM
1286          jai_rgm_trx_records
1287   WHERE
1288          --attribute_context = cp_attribute_context   AND
1289          trx_reference_context in ('CM-INV-APP','CM-DM-APP')           AND  --added CM-DM-APP by Xiao for bug#6773751--Bo Li for Bug9305067 change attribute_cotext to trx_reference_context
1290          trx_reference2        = cp_cm_customer_trx_id ; --Bo Li for Bug9305067 change attribute2 to trx_reference2
1291 
1292 BEGIN
1293 
1294   /*
1295   || Variable Intialization
1296   */
1297   lv_process_flag       := jai_constants.successful     ;
1298   lv_process_message    := null                         ;
1299 
1300   p_process_flag        := lv_process_flag              ;
1301   p_process_message     := lv_process_message           ;
1302 
1303 fnd_file.put_line(fnd_file.LOG,'delete_non_existant_cm p_org_id:'||p_org_id );
1304   fnd_file.put_line(fnd_file.LOG,'p_organization_id:'||p_organization_id );
1305 
1306 /*
1307   || Source type would indicate CREDIT MEMO REVERSAL
1308   */
1309   lv_source_trx_type := jai_constants.trx_type_cm_rvs ;
1310 
1311   /*
1312   || Update all the credit memo from reference, reset recovered_amount = 0
1313   || Insert repository records ('CM-CM-APP') corresponding to the above effect
1314   */
1315   FOR rec_c_get_incompleted_cm IN c_get_incompleted_cm
1316   LOOP
1317      --Add by Xiao for POT change, reg bug#12533434 on 16-May-2011, begin
1318      ----------------------------------------------------------------------------------------------
1319      IF NOT is_accrual_basis(rec_c_get_incompleted_cm.line_id) THEN
1320      ----------------------------------------------------------------------------------------------
1321      --Add by Xiao for POT change, reg bug#12533434 on 16-May-2011, begin
1322 
1323 
1324     /*########################################################################################################
1325     || SET SAVE POINT POINT FOR EACH CM_CUSTOMER_TRX_ID RECORD
1326     ########################################################################################################*/
1327       fnd_file.put_line(fnd_file.LOG,' ********************1 PROCESSING REC_C_GET_INCOMPLETED_CM.CM_CUSTOMER_TRX_ID -> '||rec_c_get_incompleted_cm.cm_customer_trx_id
1328                       ||' ******************** ');
1329 
1330 
1331     /*
1332     || Set the savepoint at the begin of processing every new cm_customer_trx_id from jai_rgm_refs table
1333     || IF an error occurs while processing any reference record of a cm_customer_trx_id then the rollback
1334     || can be done for all the records of that cm_customer_trx_id
1335     */
1336     savepoint roll_to_last_cm;
1337 
1338 
1339 
1340     /*########################################################################################################
1341     || PASS CM-CM-REV RECORD ENTRIES IN REPOSITORY AND UPDATE THE CM REFERENCES
1342     || Insert Credit Memo repository entries to the effect of the CM incompletion.
1343     || This record would be exactly opposite of the earlier CM creation repository record
1344     ########################################################################################################*/
1345 
1346 
1347     IF nvl(ln_err_cm_customer_trx_id,-1) <> nvl(rec_c_get_incompleted_cm.cm_customer_trx_id,-1) THEN
1348 
1349       FOR  rec_c_get_cm_cm_app_rec IN c_get_cm_cm_app_rec   ( /*cp_cm_reference_id     => rec_c_get_incompleted_cm.cm_reference_id    ,*/
1350                                                               cp_cm_customer_trx_id  => rec_c_get_incompleted_cm.cm_customer_trx_id,
1351                     cp_attribute_context   => 'CM-CM-APP'
1352                                                             ) --rchandan for bug#4428980
1353       LOOP
1354 
1355         /*
1356         || Insert a record into the repository corresponding to the 'CM-CM-REV'
1357         */
1358         fnd_file.put_line(fnd_file.LOG,' 3 Passing CM-CM-REV record, for the CM-CM-APP with rec_c_get_cm_cm_app_rec.cm_customer_trx_id -> '||rec_c_get_cm_cm_app_rec.trx_reference2 --Bo Li for Bug9305067 change attribute2 to trx_reference2
1359                                          ||' and reference_id -> '||rec_c_get_cm_cm_app_rec.reference_id
1360                                          ||',repository_id -> '|| rec_c_get_cm_cm_app_rec.repository_id);
1361 
1362 
1363 
1364         ln_amount := nvl(rec_c_get_cm_cm_app_rec.debit_amount,rec_c_get_cm_cm_app_rec.credit_amount) * (-1);
1365 
1366         fnd_file.put_line(fnd_file.LOG,' 3.1 before call to jai_cmn_rgm_recording_pkg.insert_repository_entry original amount -> '||nvl(rec_c_get_cm_cm_app_rec.debit_amount,rec_c_get_cm_cm_app_rec.credit_amount)
1367                                        ||', reversal entry amount -> '||ln_amount       );
1368 
1369         /*csahoo for bug#5879769...start*/
1370 
1371         ln_organization_id   := NULL;
1372         ln_location_id       := NULL;
1373         lv_service_type_code := NULL;
1374         lv_process_flag      := NULL;
1375         lv_process_message   := NULL;
1376 
1377         jai_trx_repo_extract_pkg.get_doc_from_reference(p_reference_id      => rec_c_get_cm_cm_app_rec.reference_id,
1378                                                         p_organization_id   => ln_organization_id,
1379                                                         p_location_id       => ln_location_id,
1380                                                         p_service_type_code => lv_service_type_code,
1381                                                         p_process_flag      => lv_process_flag,
1382                                                         p_process_message   => lv_process_message
1383                                                         );
1384 
1385          IF  lv_process_flag <> jai_constants.successful THEN
1386            FND_FILE.put_line(fnd_file.log, 'Error Flag:'||lv_process_flag||' Error Message:'||lv_process_message);
1387            return;
1388          END IF;
1389 
1390         /*csahoo for bug#5879769...end*/
1391 
1392         lv_attribute_context  := 'CM-CM-REV'; --rchandan for bug#4428980
1393 
1394         jai_cmn_rgm_recording_pkg.insert_repository_entry (
1395                                                             p_repository_id              => ln_repository_id                                                                                  ,
1396                                                             p_regime_id                  => p_regime_id                                                                                       ,
1397                                                             p_tax_type                   => rec_c_get_cm_cm_app_rec.tax_type                                                                  ,
1398                                                             p_organization_type          => p_organization_type                                                                               ,
1399                                                             p_organization_id            => p_organization_id                                                                                  ,/*5879769*/
1400                                                             p_location_id                => ln_location_id                                                                                     ,/*5879769*/
1401                                                             p_service_type_code          => lv_service_type_code                                                                               ,/*5879769*/
1402                                                             p_source                     => p_source                                                                                          ,
1403                                                             p_source_trx_type            => lv_source_trx_type                                                                                ,
1404                                                             p_source_table_name          => UPPER(jai_constants.repository_name)                                                              ,
1405                                                             p_source_document_id         => rec_c_get_cm_cm_app_rec.repository_id                                                             ,
1406                                                             p_transaction_date           => rec_c_get_cm_cm_app_rec.creation_date                                                             ,
1407                                                             p_account_name               => NULL                                                                                              ,
1408                                                             p_charge_account_id          => NULL                                                                                              ,
1409                                                             p_balancing_account_id       => NULL                                                                                              ,
1410                                                             p_amount                     => ln_amount                                                                                         ,
1411                                                             p_assessable_value           => NULL                                                                                              ,
1412                                                             p_tax_rate                   => rec_c_get_cm_cm_app_rec.tax_rate                                                                  ,
1413                                                             p_reference_id               => rec_c_get_cm_cm_app_rec.reference_id                                                              ,
1414                                                             p_batch_id                   => p_batch_id                                                                                        ,
1415                                                             p_called_from                => lv_object_name                                                                                    , --rchandan for bug#4428980
1416                                                             p_process_flag               => lv_process_flag                                                                                   ,
1417                                                             p_process_message            => lv_process_message                                                                                ,
1418                                                             p_discounted_amount          => ln_discounted_amount                                                                              ,
1419                                                             p_inv_organization_id        => rec_c_get_cm_cm_app_rec.inv_organization_id                                                       ,
1420                                                             p_accounting_date            => sysdate                                                                                           ,
1421                                                             p_currency_code              => rec_c_get_cm_cm_app_rec.trx_currency                                                              ,
1422                                                             p_curr_conv_date             => rec_c_get_cm_cm_app_rec.curr_conv_date                                                            ,
1423                                                             p_curr_conv_type             => NULL                                                                                              ,
1424                                                             p_curr_conv_rate             => rec_c_get_cm_cm_app_rec.curr_conv_rate                                                            ,
1425                                                             p_trx_amount                 => ln_amount                                                                                         ,
1426                                                             --Bo Li for Bug9305067 change attribute_cotext to trx_reference_context
1427                                                             -- attribute2 to trx_reference2
1428                                                             -----------------------------------------------------------------------------                                                                                    ,
1429                                                             p_trx_reference_context          => lv_attribute_context                                                                              ,
1430                                                             p_trx_reference2                 => rec_c_get_incompleted_cm.cm_customer_trx_id
1431                                                             ---------------------------------------------------------------------------------
1432                                                             , p_accntg_required_flag    => jai_constants.yes --File.Sql.35 Cbabu
1433                                                           );
1434 
1435         fnd_file.put_line(fnd_file.LOG,' 4 Returned from jai_cmn_rgm_recording_pkg.insert_repository_entry ' );
1436 
1437 
1438         IF lv_process_flag = jai_constants.expected_error    OR
1439            lv_process_flag = jai_constants.unexpected_error
1440         THEN
1441           /*
1442           || as Returned status is an error hence:-
1443           ||1. set the ln_err_cm_customer_trx_id to the cm_customer_Trx_id which errored out
1444           ||2. Rollback to save point
1445           ||3. Set out variables p_process_flag and p_process_message accordingly
1446           ||4. Exit from the loop
1447           */
1448           ln_err_cm_customer_trx_id := rec_c_get_incompleted_cm.cm_customer_trx_id;
1449           ROLLBACK to roll_to_last_cm;
1450           fnd_file.put_line( fnd_file.log, '5 error in call to jai_cmn_rgm_recording_pkg.insert_repository_entry - lv_process_flag '||lv_process_flag
1451                                             ||', lv_process_message'||lv_process_message ||'cm_customer_trx_id -  '||ln_err_cm_customer_trx_id);
1452           p_process_flag    := lv_process_flag    ;
1453           p_process_message := lv_process_message ;
1454           EXIT;
1455         END IF;
1456 
1457       END LOOP; /*End of 'CM-CM-REV' record processing for an incompleted CM*/
1458     END IF;     /* End if of ln_err_cm_customer_trx_id <> rec_c_get_incompleted_cm.cm_customer_trx_id*/
1459 
1460     IF nvl(ln_err_cm_customer_trx_id,-1) <> nvl(rec_c_get_incompleted_cm.cm_customer_trx_id,-1) THEN
1461 
1462       /*########################################################################################################
1463       || Update the Credit Reference and set Recovered Amount to 0 as this credit memo has been incompleted
1464       ########################################################################################################*/
1465     FOR rec_cur_upd_cm_ref IN cur_upd_cm_ref (cp_cm_customer_trx_id  => rec_c_get_incompleted_cm.cm_customer_trx_id)
1466     LOOP
1467         fnd_file.put_line(fnd_file.LOG,' 6 before call to jai_cmn_rgm_recording_pkg.update_reference for updating CM reference to 0-> '||rec_c_get_incompleted_cm.cm_customer_trx_id
1468                                      ||', reference_id -> '||rec_cur_upd_cm_ref.reference_id );
1469 
1470         jai_cmn_rgm_recording_pkg.update_reference (
1471                                                      p_source             => p_source                                        ,
1472                                                      p_reference_id       => rec_cur_upd_cm_ref.reference_id                 ,
1473                                                      p_recovered_amount   => rec_cur_upd_cm_ref.recovered_amount * (-1)      ,
1474                                                      p_process_flag       => lv_process_flag                                 ,
1475                                                      p_process_message    => lv_process_message
1476                                                    );
1477         IF lv_process_flag = jai_constants.expected_error    OR
1478            lv_process_flag = jai_constants.unexpected_error
1479         THEN
1480           /*
1481           || as Returned status is an error hence:-
1482           || Set out variables p_process_flag and p_process_message accordingly
1483           */
1484           ln_err_cm_customer_trx_id := rec_c_get_incompleted_cm.cm_customer_trx_id;
1485           ROLLBACK to roll_to_last_cm;
1486           fnd_file.put_line( fnd_file.log, '7 error in call to jai_cmn_rgm_recording_pkg.update_reference - lv_process_flag '||lv_process_flag
1487                                             ||', lv_process_message'||lv_process_message);
1488           p_process_flag    := lv_process_flag    ;
1489           p_process_message := lv_process_message ;
1490         END IF;
1491 
1492         fnd_file.put_line(fnd_file.LOG,' 8 Returned from jai_cmn_rgm_recording_pkg.update_reference after updating CM reference' );
1493     END LOOP;
1494 
1495     END IF; /* End if of ln_err_cm_customer_trx_id <> rec_c_get_incompleted_cm.cm_customer_trx_id*/
1496 
1497 
1498     /*########################################################################################################
1499     || PASS CM-INV-REV ENTRIES , UPDATE INV REFERENCES
1500     || Reverse CM application to invoices into repository entries to the effect of the CM incompletion.
1501     || This record would be exactly opposite of the earlier CM creation repository record
1502     ########################################################################################################*/
1503     IF nvl(ln_err_cm_customer_trx_id,-1) <> nvl(rec_c_get_incompleted_cm.cm_customer_trx_id,-1) THEN
1504 
1505       FOR  rec_c_get_cm_inv_app_rec IN c_get_cm_inv_app_rec ( cp_cm_customer_trx_id  => rec_c_get_incompleted_cm.cm_customer_trx_id
1506                                                               --, cp_attribute_context   => 'CM-INV-APP'
1507                                                               )
1508       LOOP
1509 
1510         fnd_file.put_line(fnd_file.LOG,' 9 Passing CM-INV-REV record, for the CM-INV-APP with rec_c_get_cm_inv_app_rec.inv_customer_trx_id -> '||rec_c_get_cm_inv_app_rec.attribute1
1511                                          ||', rec_c_get_cm_inv_app_rec.cm_customer_trx_id -> '||rec_c_get_cm_inv_app_rec.attribute2
1512                                          ||'  reference_id -> '||rec_c_get_cm_inv_app_rec.reference_id
1513                                          ||', repository_id -> '|| rec_c_get_cm_inv_app_rec.repository_id);
1514 
1515         /*
1516         || Insert a record into the repository corresponding to the 'CM-CM-REV'
1517         */
1518         fnd_file.put_line(fnd_file.LOG,' 9.1 before call to jai_cmn_rgm_recording_pkg.insert_repository_entry ' );
1519 
1520         ln_amount := nvl(rec_c_get_cm_inv_app_rec.debit_amount,rec_c_get_cm_inv_app_rec.credit_amount) * (-1);
1521 
1522         fnd_file.put_line(fnd_file.LOG,' 10 before call to jai_cmn_rgm_recording_pkg.insert_repository_entry original amount -> '||nvl(rec_c_get_cm_inv_app_rec.debit_amount,rec_c_get_cm_inv_app_rec.credit_amount)
1523                                        ||', reversal entry amount -> '||ln_amount       );
1524 
1525         /*csahoo for bug#5879769...start*/
1526 
1527         ln_organization_id   := NULL;
1528         ln_location_id       := NULL;
1529         lv_service_type_code := NULL;
1530         lv_process_flag      := NULL;
1531         lv_process_message   := NULL;
1532 
1533         jai_trx_repo_extract_pkg.get_doc_from_reference(p_reference_id      => rec_c_get_cm_inv_app_rec.reference_id,
1534                                                         p_organization_id   => ln_organization_id,
1535                                                         p_location_id       => ln_location_id,
1536                                                         p_service_type_code => lv_service_type_code,
1537                                                         p_process_flag      => lv_process_flag,
1538                                                         p_process_message   => lv_process_message
1539                                                         );
1540 
1541          IF  lv_process_flag <> jai_constants.successful THEN
1542            FND_FILE.put_line(fnd_file.log, 'Error Flag:'||lv_process_flag||' Error Message:'||lv_process_message);
1543            return;
1544          END IF;
1545 
1546         /*csahoo for bug#5879769...end*/
1547     /*added by Xiao for bug#6773751*/
1548       lv_attribute_context:=null;
1549     if rec_c_get_cm_inv_app_rec.attribute_context='CM-INV-APP' then
1550        lv_attribute_context:='CM-INV-REV'    ;
1551     else
1552        lv_attribute_context:='CM-DM-REV'    ;
1553     end if;
1554      --end added by Xiao for bug#6773751
1555 
1556 
1557         --lv_attribute_context := 'CM-INV-REV'; --rchandan for bug#4428980  -- commented by Xiao for bug#6773751
1558   lv_source_table      := 'JAI_RGM_TRX_RECORDS'; --rchandan for bug#4428980
1559         jai_cmn_rgm_recording_pkg.insert_repository_entry (
1560                                                             p_repository_id              => ln_repository_id                                                                                  ,
1561                                                             p_regime_id                  => p_regime_id                                                                                       ,
1562                                                             p_tax_type                   => rec_c_get_cm_inv_app_rec.tax_type                                                                  ,
1563                                                             p_organization_type          => p_organization_type                                                                               ,
1564 p_organization_id            => ln_organization_id                                                                                 ,/*5879769*/
1565 p_location_id                => ln_location_id                                                                                    ,/*5879769*/
1566                                                             p_service_type_code          => lv_service_type_code                                                                              ,/*5879769*/
1567                                                             p_source                     => p_source                                                                                          ,
1568                                                             p_source_trx_type            => lv_source_trx_type                                                                                ,
1569                                                             p_source_table_name          => lv_source_table                                                                                   , --rchandan for bug#4428980
1570                                                             p_source_document_id         => rec_c_get_cm_inv_app_rec.repository_id                                                            ,
1571                                                             p_transaction_date           => rec_c_get_cm_inv_app_rec.creation_date                                                            ,
1572                                                             p_account_name               => NULL                                                                                              ,
1573                                                             p_charge_account_id          => NULL                                                                                              ,
1574                                                             p_balancing_account_id       => NULL                                                                                              ,
1575                                                             p_amount                     => ln_amount                                                                                         ,
1576                                                             p_assessable_value           => NULL                                                                                              ,
1577                                                             p_tax_rate                   => rec_c_get_cm_inv_app_rec.tax_rate                                                                 ,
1578                                                             p_reference_id               => rec_c_get_cm_inv_app_rec.reference_id                                                             ,
1579                                                             p_batch_id                   => p_batch_id                                                                                        ,
1580                                                             p_called_from                => lv_object_name                                                                                    ,
1581                                                             p_process_flag               => lv_process_flag                                                                                   ,
1582                                                             p_process_message            => lv_process_message                                                                                ,
1583                                                             p_discounted_amount          => ln_discounted_amount                                                                              ,
1584                                                             p_inv_organization_id        => rec_c_get_cm_inv_app_rec.inv_organization_id                                                      ,
1585                                                             p_accounting_date            => sysdate                                                                                           ,
1586                                                             p_currency_code              => rec_c_get_cm_inv_app_rec.trx_currency                                                             ,
1587                                                             p_curr_conv_date             => rec_c_get_cm_inv_app_rec.curr_conv_date                                                           ,
1588                                                             p_curr_conv_type             => NULL                                                                                              ,
1589                                                             p_curr_conv_rate             => rec_c_get_cm_inv_app_rec.curr_conv_rate                                                           ,
1590                                                             p_trx_amount                 => ln_amount                                                                                         ,
1591                                                             --Bo Li for Bug9305067 change attribute_cotext to trx_reference_context
1592                                                             -- attribute1 to trx_reference1 and attribute2 to trx_reference2
1593                                                             -----------------------------------------------------------------------------                                                 , --rchandan for bug#4428980
1594                                                             p_trx_reference_context          => lv_attribute_context                                                                              ,
1595                                                             p_trx_reference1                 => rec_c_get_cm_inv_app_rec.trx_reference1                                                               ,
1596                                                             p_trx_reference2                 => rec_c_get_cm_inv_app_rec.trx_reference2
1597                                                             ---------------------------------------------------------------------------
1598                                                             , p_accntg_required_flag    => jai_constants.yes --File.Sql.35 Cbabu
1599                                                           );
1600 
1601         fnd_file.put_line(fnd_file.LOG,' 10.1 Returned from jai_cmn_rgm_recording_pkg.insert_repository_entry ' );
1602 
1603 
1604         IF lv_process_flag = jai_constants.expected_error    OR
1605            lv_process_flag = jai_constants.unexpected_error
1606         THEN
1607           /*
1608           || as Returned status is an error hence:-
1609           ||1. Rollback to save point
1610           ||2. Set out variables p_process_flag and p_process_message accordingly
1611           */
1612           ln_err_cm_customer_trx_id := rec_c_get_incompleted_cm.cm_customer_trx_id;
1613           ROLLBACK to roll_to_last_cm;
1614           fnd_file.put_line( fnd_file.log, '11 error in call to jai_cmn_rgm_recording_pkg.insert_repository_entry - lv_process_flag '||lv_process_flag
1615                                             ||', lv_process_message'||lv_process_message);
1616           p_process_flag    := lv_process_flag    ;
1617           p_process_message := lv_process_message ;
1618           EXIT;
1619         END IF;
1620 
1621 
1622         /*########################################################################################################
1623         || Update the Recovered Amount of the Invoice Reference against the CM application
1624         ########################################################################################################*/
1625 
1626         fnd_file.put_line(fnd_file.LOG,' 12 before call to jai_cmn_rgm_recording_pkg.update_reference for updating INV reference_id - '||rec_c_get_cm_inv_app_rec.reference_id
1627                                         ||', amount to be adjusted from recovered_Amount -> '||nvl(rec_c_get_cm_inv_app_rec.debit_amount,rec_c_get_cm_inv_app_rec.credit_amount) * (-1)
1628                           );
1629 
1630 
1631         jai_cmn_rgm_recording_pkg.update_reference (
1632                                                      p_source             => p_source                                                                                 ,
1633                                                      p_reference_id       => rec_c_get_cm_inv_app_rec.reference_id                                                    ,
1634                                                      p_recovered_amount   => ln_amount                                                                                ,
1635                                                      p_process_flag       => lv_process_flag                                                                          ,
1636                                                      p_process_message    => lv_process_message
1637                                                    );
1638         IF lv_process_flag = jai_constants.expected_error    OR
1639            lv_process_flag = jai_constants.unexpected_error
1640         THEN
1641           /*
1642           || as Returned status is an error hence:-
1643           || Set out variables p_process_flag and p_process_message accordingly
1644           */
1645           ln_err_cm_customer_trx_id := rec_c_get_incompleted_cm.cm_customer_trx_id;
1646           ROLLBACK to roll_to_last_cm;
1647           fnd_file.put_line( fnd_file.log, '13 error in call to jai_cmn_rgm_recording_pkg.update_reference - lv_process_flag '||lv_process_flag
1648                                             ||', lv_process_message'||lv_process_message);
1649           p_process_flag    := lv_process_flag    ;
1650           p_process_message := lv_process_message ;
1651           EXIT;
1652         END IF;
1653 
1654         fnd_file.put_line(fnd_file.LOG,' 14 Returned from jai_cmn_rgm_recording_pkg.update_reference after updating invoice reference' );
1655 
1656       END LOOP; /* End of 'CM-INV-APP record processing for an CM application against an invoice' */
1657 
1658     END IF; /* End if of ln_err_cm_customer_trx_id <> rec_c_get_incompleted_cm.cm_customer_trx_id*/
1659 
1660     /*
1661     || Reset the savepoint to the previous cm_customer_trx_id
1662     ln_set_save_point := rec_c_get_incompleted_cm.cm_customer_trx_id;
1663   */
1664    END IF;--Add by Xiao for POT change, reg bug#12533434 on 16-May-2011
1665     fnd_file.put_line(fnd_file.LOG,' 15 ******************** END of one CM processing ******************** ');
1666   END LOOP;    /* End of CM reference processing */
1667 
1668 
1669    /* Added by Ramananda for bug#4407165 */
1670     EXCEPTION
1671      WHEN OTHERS THEN
1672       p_process_flag := null;
1673       p_process_message  := null;
1674       FND_MESSAGE.SET_NAME('JA','JAI_EXCEPTION_OCCURED');
1675       FND_MESSAGE.SET_TOKEN('JAI_PROCESS_MSG', lv_object_name ||'. Err:'||sqlerrm );
1676       app_exception.raise_exception;
1677 
1678 END delete_non_existant_cm;
1679 
1680 
1681 
1682 procedure populate_cm_app   ( p_regime_id          IN  JAI_RGM_DEFINITIONS.REGIME_ID%TYPE                  ,
1683                               p_organization_type  IN  JAI_RGM_PARTIES.ORGANIZATION_TYPE%TYPE      ,
1684                               p_from_date          IN  DATE                                        ,
1685                               p_to_date            IN  DATE                                        ,
1686                               p_org_id             IN  RA_CUSTOMER_TRX_ALL.ORG_ID%TYPE             ,
1687                               p_source             IN  varchar2, --File.Sql.35 Cbabu  jai_constants.SOURCE_AR%TYPE                ,
1688                               p_batch_id           IN  JAI_RGM_TRX_REFS.BATCH_ID%TYPE              ,
1689                               p_process_flag OUT NOCOPY VARCHAR2                                    ,
1690                               p_process_message OUT NOCOPY VARCHAR2
1691                          ,  p_organization_id   JAI_RGM_PARTIES.ORGANIZATION_ID%TYPE DEFAULT NULL )
1692 IS
1693 
1694   ln_eff_cm_tax_amount              AR_RECEIVABLE_APPLICATIONS_ALL.TAX_APPLIED%TYPE                                       ;
1695   ln_discounted_amount              JAI_RGM_TRX_REFS.DISCOUNTED_AMOUNT%TYPE                                               ;
1696   ln_cm_ref_upd                     JAI_RGM_TRX_REFS.RECOVERED_AMOUNT%TYPE                                                ;
1697   ln_repository_id                  JAI_RGM_TRX_RECORDS.REPOSITORY_ID%TYPE                                                ;
1698   lv_source_trx_type                VARCHAR2(50)                                                                          ;
1699   ln_eff_cm_tottax_amt              JAI_RGM_TRX_REFS.RECOVERABLE_AMOUNT%TYPE                                              ;
1700   ln_tot_effcm_rb_amt               JAI_RGM_TRX_REFS.RECOVERABLE_AMOUNT%TYPE                                              ;
1701   ln_inv_tot_tax_amt                JAI_AR_TRX_TAX_LINES.TAX_AMOUNT%TYPE                                           ;
1702   ln_sign_of_credit_memo            NUMBER                                                                                ;
1703   ln_cm_ref_ratio                   NUMBER                                                                                ;
1704   ln_amount                         JAI_RGM_TRX_RECORDS.DEBIT_AMOUNT%TYPE                                                 ;
1705   -- Added ln_func_amount for Bug 7522584
1706   ln_func_amount                    JAI_RGM_TRX_RECORDS.DEBIT_AMOUNT%TYPE                                                 ;
1707   ln_receivable_application_id      AR_RECEIVABLE_APPLICATIONS_ALL.RECEIVABLE_APPLICATION_ID%TYPE              := -1      ;
1708   lv_process_flag                   VARCHAR2(2)                                                                           ;
1709   lv_process_message                VARCHAR2(1996)                                                                        ;
1710   ln_uncommitted_transactions       NUMBER(10)                                                                 := 0       ;
1711   lv_service_type_code              JAI_AR_TRX_LINES.service_type_code%TYPE ;/*5879769*/
1712   lv_application_type               ar_receivable_applications_all.application_type%TYPE  ;--rchandan for bug#4428980
1713   lv_status                         ar_receivable_applications_all.status%TYPE ;--rchandan for bug#4428980
1714   lv_type                           ra_cust_trx_types_all.type%TYPE ;--rchandan for bug#4428980
1715   lv_source_table                   CONSTANT jai_rgm_trx_records.source_table_name%TYPE := 'AR_RECEIVABLE_APPLICATIONS_ALL';--rchandan for bug#4428980
1716   lv_called_from                    CONSTANT varchar2(100) := 'jai_ar_rgm_processing_pkg.POPULATE_CM_APP';--rchandan for bug#4428980
1717   lv_attribute_context              jai_rgm_trx_records.attribute_context%TYPE ;--rchandan for bug#4428980
1718 
1719   ln_total_tax_applied              NUMBER;     --added by walton for inclusive tax 29-Nov-07
1720   ln_line_total_amt                 NUMBER;      --added by walton for inclusive tax 29-Nov-07
1721   ln_inclusive_total_amt            NUMBER;     --added by walton for inclusive tax 29-Nov-07
1722 
1723   ln_exc_gain_loss_amt              NUMBER; -- Added for Bug 8294236
1724   ln_total_tax_amt                  NUMBER; -- Added for Bug 8294236
1725   ln_tmp_line_total_amt             NUMBER; -- Added for Bug 9919612
1726   ln_trx_amount                     NUMBER; -- Added for Bug 10623735
1727 
1728  /*
1729   || Get the credit memo applications to invoices and the total invoice tax amount .
1730   || Consider only those credit memo applications which follow the following conditions:-
1731   || 1. Invoice tax line reference exists in the jai_rgm_trx_refs table for the invoice against which the cash receipt is being applied
1732   || 2. The cash receipt tax line does not already exist in the repository i.e jai_rgm_trx_records.
1733   || 3. Consider cash receipt application against an invoice only
1734   || 4. Invoice tax line has not been fully recovered i.e recovered_amount < recoverable_amount - discounted_amount in references table
1735   || 5. Processes DebitMemo for bug#6773751, add by Xiao on 20-Dec-09.
1736  */
1737 
1738   /*Bug 10623735 - Added order by clause on aral.receivable_application_id*/
1739   CURSOR c_get_cm_rec_app     ( cp_source_ar varchar2) --File.Sql.35 Cbabu  jai_constants.SOURCE_AR%TYPE )
1740   IS
1741   SELECT
1742           aral.customer_trx_id                                    cm_customer_trx_id                              ,
1743           aral.applied_customer_trx_id                            inv_customer_trx_id                             ,
1744           aral.receivable_application_id                                                                          ,
1745           aral.gl_date                                                                                            ,
1746           nvl(aral.tax_applied,0)                                 cm_app_tax_amt                                  ,
1747           nvl(aral.line_applied,0)                                cm_app_line_amt                                , --added by walton for inclusive tax 29-Nov-07
1748           nvl(aral.amount_applied,0)                              cm_app_amount                                   ,
1749           cm_trx.trx_date                                         cm_transaction_date                             ,
1750           cm_trx.invoice_currency_code                            cm_currency_code                                ,
1751           cm_trx.exchange_date                                    cm_exchange_date                                ,
1752           cm_trx.exchange_rate                                    cm_exchange_rate                                ,
1753           cm_trx.exchange_rate_type                               cm_exchange_rate_type                           ,
1754           inv_jtrx.organization_id                                inv_invn_organization_id                        ,
1755           cm_jtrx.organization_id                                 cm_invn_organization_id                         ,
1756           trx_types.type                                          Trx_type, --added by Xiao for bug#6773751
1757         -- Added for Bug 8294236 - Start
1758          inv_trx.invoice_currency_code                           invoice_currency_code                           ,
1759          inv_trx.exchange_rate                                   invoice_exchange_rate                           ,
1760          inv_trx.exchange_date                                   invoice_exchange_date                           ,
1761          inv_trx.exchange_rate_type                              invoice_exchange_rate_type
1762          -- Added for Bug 8294236 - End
1763   FROM
1764           ar_receivable_applications_all  aral                                                                    ,
1765           ra_customer_trx_all             cm_trx                                                                  ,
1766           ra_customer_trx_all             inv_trx                                                                 ,
1767           ra_cust_trx_types_all           trx_types                                                               ,
1768           JAI_AR_TRXS           inv_jtrx                                                                ,
1769           JAI_AR_TRXS           cm_jtrx
1770   WHERE
1771           aral.customer_trx_id         = cm_trx.customer_trx_id                                                   AND
1772           cm_trx.customer_trx_id       = cm_jtrx.customer_trx_id                                                  AND
1773           aral.applied_customer_trx_id = inv_trx.customer_trx_id                                                  AND
1774           trunc(aral.creation_date)    BETWEEN trunc(p_from_date) and trunc(p_to_date)                            AND
1775           aral.application_type        = lv_application_type                                                      AND--rchandan for bug#4428980
1776           aral.status                  = lv_status                                                                AND--rchandan for bug#4428980
1777           /*nvl(aral.tax_applied,0)    <> 0                                                                       AND*/--Commented by walton for inclusive tax 29-Nov-07
1778           inv_trx.org_id               = nvl(p_org_id,inv_trx.org_id)                                             AND
1779           cm_trx.org_id                = nvl(p_org_id,cm_trx.org_id)                                              AND
1780           inv_trx.complete_flag        = 'Y'                                                                      AND
1781           cm_trx.complete_flag         = 'Y'                                                                      AND
1782           trx_types.cust_trx_type_id   = inv_trx.cust_trx_type_id                                                 AND
1783           trx_types.type                in( 'INV', 'DM' )   AND --lv_type   -- modified by Xiao for bug#6773751
1784           trx_types.org_id             = inv_trx.org_id                                                           AND
1785           inv_trx.customer_trx_id      = inv_jtrx.customer_trx_id                                                 AND
1786           inv_jtrx.organization_id     = p_organization_id                                                        AND/*5879769*/
1787           NOT EXISTS                   ( SELECT         /*A credit memo application does not exist in repository */
1788                                                  1
1789                                          FROM
1790                                                  jai_rgm_trx_records  rgtr
1791                                          WHERE
1792                                                  rgtr.source               = cp_source_ar                         AND
1793                                                  rgtr.organization_id      = p_organization_id AND -- Date 05/06/2007 by sacsethi for bug 6109941
1794                                                  rgtr.source_table_name    = lv_source_table                      AND--rchandan for bug#4428980
1795                                                  rgtr.source_document_id   = aral.receivable_application_id
1796                                        )                                                                          AND
1797         EXISTS                         (
1798                                          SELECT        /* A credit memo exists in the reference table with total recoverable amount <> recovered amount*/
1799                                                  1
1800                                          FROM
1801                                                  jai_rgm_trx_refs                rgtf
1802                                          WHERE
1803                                                  rgtf.source                    = cp_source_ar                         AND
1804                                                  rgtf.invoice_id                = aral.customer_trx_id                 AND
1805                                                  nvl(rgtf.recoverable_amount,0) <> nvl(rgtf.recovered_amount,0)
1806                                        )                                                                              AND
1807         EXISTS                         (
1808                                          SELECT        /* A invoice exists in the reference table with total recoverable amount > recovered amount*/
1809                                                  1
1810                                          FROM
1811                                                  jai_rgm_trx_refs                rgtf
1812                                          WHERE
1813                                                  rgtf.source                    = cp_source_ar                         AND
1814                                                  rgtf.invoice_id                = aral.applied_customer_trx_id         AND
1815                                                  nvl(rgtf.recoverable_amount,0) - nvl(discounted_amount,0) > nvl(rgtf.recovered_amount,0)
1816                                        )
1817   order by aral.receivable_application_id;
1818 
1819    /*
1820    || Get the Total transactional tax amount for the invoice
1821    */
1822    CURSOR cur_get_inv_tottax_amt (cp_inv_customer_trx_id JAI_AR_TRXS.CUSTOMER_TRX_ID%TYPE)
1823    IS
1824    SELECT
1825           nvl(sum(jrttl.tax_amount),0)   inv_tot_tax_amount
1826    FROM
1827           JAI_AR_TRX_LINES    jrtl  ,
1828           JAI_AR_TRX_TAX_LINES    jrttl
1829    WHERE
1830           jrtl.customer_trx_line_id   = jrttl.link_to_cust_trx_line_id  AND
1831           jrtl.customer_trx_id        = cp_inv_customer_trx_id ;
1832 
1833 
1834   /*
1835   || Get the sign of the credit memo
1836   */
1837   CURSOR cur_sign_of_cm (cp_cm_customer_trx_id JAI_AR_TRX_LINES.CUSTOMER_TRX_ID%TYPE )
1838   IS
1839   SELECT
1840       sign(nvl(sum(jrttl.tax_amount),0))   sign_of_credit_memo
1841   FROM
1842           JAI_AR_TRX_LINES    jrtl  ,
1843       JAI_AR_TRX_TAX_LINES    jrttl
1844   WHERE
1845          jrtl.customer_trx_line_id   = jrttl.link_to_cust_trx_line_id  AND
1846      jrtl.customer_trx_id        = cp_cm_customer_trx_id;
1847 
1848    --added by walton for inclusive tax 29-Nov-07
1849    -----------------------------------------------------
1850    --Get the Total CM inclusive tax amount for the cm,
1851    CURSOR cur_get_cm_inclusive_tax_amt
1852    ( pn_cm_customer_trx_id JAI_AR_TRXS.CUSTOMER_TRX_ID%TYPE
1853    )
1854    IS
1855    SELECT
1856      nvl(sum(jrttl.tax_amount),0) inv_tot_inclusive_tax_amt
1857    FROM
1858      JAI_AR_TRX_LINES    jrtl
1859    , JAI_AR_TRX_TAX_LINES    jrttl
1860    , jai_cmn_taxes_all    tax
1861    WHERE jrtl.customer_trx_line_id   = jrttl.link_to_cust_trx_line_id
1862      AND jrtl.customer_trx_id        = pn_cm_customer_trx_id
1863      AND jrttl.tax_id                = tax.tax_id
1864      AND NVL(tax.inclusive_tax_flag,'N') = 'Y' ;
1865 
1866    --Get the Total CM line amount,
1867    CURSOR cur_get_cm_line_amt
1868    ( pn_cm_customer_trx_id JAI_AR_TRXS.CUSTOMER_TRX_ID%TYPE
1869    )
1870    IS
1871    SELECT
1872       nvl(sum(jrtl.line_amount),0) inv_tot_line_amt
1873    FROM
1874      JAI_AR_TRX_LINES    jrtl
1875    WHERE jrtl.customer_trx_id        = pn_cm_customer_trx_id;
1876    ----------------------------------------------------------
1877 
1878   /*
1879   || Get the ref tax lines pertaining to the invoice against which the cash receipt has been applied : -
1880 
1881   */
1882   CURSOR c_get_refinvrec_for_upd    ( cp_source_ar            varchar2, --File.Sql.35 Cbabu  jai_constants.SOURCE_AR%TYPE        ,
1883                                       cp_inv_customer_trx_id  RA_CUSTOMER_TRX_ALL.CUSTOMER_TRX_ID%TYPE
1884                                      )
1885   IS
1886   SELECT
1887           reference_id                                                                                       ,
1888           tax_type                                                                                           ,
1889           tax_rate                                                                                           ,
1890           nvl(recoverable_amount,0) - nvl(discounted_amount,0)    recoverable_amount                         ,
1891           nvl(recovered_amount,0)                                 recovered_amount                           ,
1892           recoverable_ptg                                                                                     ,
1893           item_line_id  /*5879769*/
1894           , line_id --Add by Xiao for POT change, reg bug#12533434 on 16-May-2011
1895 
1896   FROM
1897           jai_rgm_trx_refs
1898   WHERE
1899           source                             = cp_source_ar                   AND
1900           invoice_id                           = cp_inv_customer_trx_id         AND
1901           nvl(recoverable_amount,0) - nvl(discounted_amount,0) > nvl(recovered_amount,0)
1902  FOR      UPDATE NOWAIT ;
1903 
1904 
1905   CURSOR c_get_refcmrec_for_upd    ( cp_source_ar            varchar2, --File.Sql.35 Cbabu  jai_constants.SOURCE_AR%TYPE        ,
1906                                      cp_cm_customer_trx_id   RA_CUSTOMER_TRX_ALL.CUSTOMER_TRX_ID%TYPE
1907                                    )
1908   IS
1909   SELECT
1910           reference_id                                                                                       ,
1911           tax_type                                                                                           ,
1912           tax_rate                                                                                           ,
1913           nvl(recoverable_amount,0)   recoverable_amount                                                     ,
1914           nvl(recovered_amount,0)     recovered_amount                                                       ,
1915           item_line_id  /*5879769*/
1916           , line_id --Add by Xiao for POT change, reg bug#12533434 on 16-May-2011
1917   FROM
1918           jai_rgm_trx_refs
1919   WHERE
1920           source                  = cp_source_ar                                                             AND
1921           invoice_id              = cp_cm_customer_trx_id                                                    AND
1922           nvl(recoverable_amount,0) <> nvl(recovered_amount,0)
1923  FOR      UPDATE NOWAIT ;
1924 
1925   /*
1926   || Get the total effective CM amount from the CM lines in reference table against which a CM application has to be recovered.
1927   */
1928   CURSOR c_get_cmref_totrd_amt     ( cp_source_ar            varchar2, --File.Sql.35 Cbabu  jai_constants.SOURCE_AR%TYPE        ,
1929                                      cp_cm_customer_trx_id   RA_CUSTOMER_TRX_ALL.CUSTOMER_TRX_ID%TYPE
1930                                    )
1931   IS
1932   SELECT
1933           nvl(sum(recoverable_amount),0) tot_effcm_rb_amt  /*bug 9432780*/
1934   FROM
1935           jai_rgm_trx_refs
1936   WHERE
1937           source                  = cp_source_ar                                                             AND
1938           invoice_id              = cp_cm_customer_trx_id ;
1939 
1940 BEGIN
1941 
1942   fnd_file.put_line(fnd_file.LOG,'1 Entering procedure  : jai_rgm_process_ar.populate_cm_app' );
1943 fnd_file.put_line(fnd_file.LOG,'p_org_id:'||p_org_id );
1944         fnd_file.put_line(fnd_file.LOG,'p_organization_id:'||p_organization_id );
1945 
1946   p_process_flag     := jai_constants.successful     ;
1947   p_process_message  := null                         ;
1948 
1949   /*****
1950   || Get all the valid CM receivables
1951   *****/
1952   lv_status   := 'APP';
1953   lv_application_type:= 'CM';
1954   --lv_type:= 'INV';   --commented by Xiao for bug#6773751
1955   FOR rec_c_get_cm_rec_app IN c_get_cm_rec_app ( cp_source_ar => p_source  )
1956   LOOP
1957 
1958     fnd_file.put_line(fnd_file.LOG,'2 processing credit memo with receivable_application_id as '||rec_c_get_cm_rec_app.receivable_application_id
1959                                    ||'against invoice with customer_trx_id '||rec_c_get_cm_rec_app.inv_customer_trx_id );
1960 
1961     /*########################################################################################################
1962     || INITIALIZING THE VARIABLES
1963     ########################################################################################################*/
1964     ln_receivable_application_id := null                         ;
1965     ln_amount                    := null                         ;
1966   ln_func_amount               := null                         ; -- Added for Bug 7522584
1967     ln_eff_cm_tax_amount         := null                         ;
1968     ln_eff_cm_tottax_amt         := null                         ;
1969     ln_tot_effcm_rb_amt          := null                         ;
1970     ln_inv_tot_tax_amt           := null                         ;
1971     ln_cm_ref_ratio              := null                         ;
1972     ln_cm_ref_upd                := null                         ;
1973     lv_source_trx_type           := null                         ;
1974     lv_process_flag              := jai_constants.successful     ;
1975     lv_process_message           := null                         ;
1976 
1977     ln_inclusive_total_amt       := null                         ; --added by walton for inclusive tax 29-Nov-07
1978 
1979     fnd_file.put_line(fnd_file.LOG,'3 Variables initialized' );
1980 
1981     /*########################################################################################################
1982     || SET A SAVEPOINT FOR EACH NEW CREDIT MEMO RECEIVABLE APPLICATION
1983     ########################################################################################################*/
1984 
1985     SAVEPOINT roll_to_cm_app;
1986 
1987 
1988     /*########################################################################################################
1989     || GET THE FOLLOWING INFORMATION:-
1990     || 1.INVOICE TOTAL TAX AMOUNT
1991     || 2. SIGN OF CREDIT MEMO
1992     || 3. DETERMINE THE CM RECEIPT APPLICATION TYPE (CREDIT MEMO APPLICATION, CREDIT MEMO REVERSAL)
1993     ########################################################################################################*/
1994 
1995     OPEN  cur_get_inv_tottax_amt (cp_inv_customer_trx_id => rec_c_get_cm_rec_app.inv_customer_trx_id);
1996     FETCH cur_get_inv_tottax_amt INTO ln_inv_tot_tax_amt;
1997     CLOSE cur_get_inv_tottax_amt ;
1998     /*
1999     || get the sign of the credit memo
2000     */
2001     OPEN  cur_sign_of_cm (cp_cm_customer_trx_id => rec_c_get_cm_rec_app.cm_customer_trx_id );
2002     FETCH cur_sign_of_cm INTO ln_sign_of_credit_memo;
2003     CLOSE cur_sign_of_cm;
2004 
2005     --added by walton for inclusive tax  29-Nov-07
2006     ------------------------------------------------------------------------------------------------------
2007     OPEN  cur_get_cm_inclusive_tax_amt( pn_cm_customer_trx_id => rec_c_get_cm_rec_app.cm_customer_trx_id
2008                                       );
2009     FETCH cur_get_cm_inclusive_tax_amt
2010     INTO  ln_inclusive_total_amt;
2011     CLOSE cur_get_cm_inclusive_tax_amt ;
2012 
2013     OPEN  cur_get_cm_line_amt( pn_cm_customer_trx_id => rec_c_get_cm_rec_app.cm_customer_trx_id
2014                              );
2015     FETCH cur_get_cm_line_amt
2016     INTO  ln_line_total_amt;
2017     CLOSE cur_get_cm_line_amt ;
2018 
2019     /*Bug 9919612 - If an AR Invoice is created with just Adhoc Service Tax(with no Line Amount) then
2020     ln_line_total_amt would be zero resulting in ORA-01476 during calculation of ln_total_tax_applied*/
2021     IF ln_line_total_amt = 0 OR ln_line_total_amt IS NULL THEN
2022         ln_tmp_line_total_amt := 1;
2023     ELSE
2024         ln_tmp_line_total_amt := ln_line_total_amt;
2025     END IF;
2026 
2027     ln_total_tax_applied := rec_c_get_cm_rec_app.cm_app_tax_amt + (rec_c_get_cm_rec_app.cm_app_line_amt/ln_tmp_line_total_amt) * ln_inclusive_total_amt;
2028     /*Bug 9919612 - End*/
2029     ------------------------------------------------------------------------------------------------------
2030 
2031     fnd_file.put_line(fnd_file.LOG,'5 start of determine application source type');
2032 
2033     /*
2034     || If the CM tax amount is +ve then it is CREDIT MEMO REVERSAL/UNAPPLICATION
2035     || IF the CM tax receipt amount is -ve then it is CREDIT MEMO APPLICATION
2036     || This info would go into the source_trx_type in the repository table jai_rgm_trx_records .
2037     || This would help distinctly identify a CM application, CREDIT MEMO reversal
2038     || IF sign = 0 i.e CM amount = 0 then skip the cm application and proceed to the next
2039     */
2040 
2041     IF ln_sign_of_credit_memo > 0 THEN
2042       /*
2043       || +ve credit memo hence CREDIT MEMO REVERSAL
2044       */
2045       lv_source_trx_type := jai_constants.trx_type_cm_rvs ;
2046 
2047     ELSIF ln_sign_of_credit_memo < 0 THEN
2048       /*
2049       || -ve credit memo hence CREDIT MEMO APPLICATION
2050       */
2051       lv_source_trx_type := jai_constants.trx_type_cm_app  ;
2052 
2053     END IF;
2054 
2055     fnd_file.put_line(fnd_file.LOG,'7  credit memo type is lv_source_trx_type-> '||lv_source_trx_type);
2056 
2057 
2058     IF ln_sign_of_credit_memo <> 0   THEN
2059       /*########################################################################################################
2060       || Fetch all invoice reference lines for processing/passing CM-INV-APP  entries
2061       ########################################################################################################*/
2062 
2063       FOR  rec_c_get_refinvrec_for_upd IN c_get_refinvrec_for_upd  ( cp_source_ar            => p_source                                  ,
2064                                                                      cp_inv_customer_trx_id  => rec_c_get_cm_rec_app.inv_customer_trx_id
2065                                                                    )
2066       LOOP
2067         --Add by Xiao for POT change, reg bug#12533434 on 16-May-2011, begin
2068         ---------------------------------------------------------------------------------------------
2069          IF NOT is_accrual_basis(rec_c_get_refinvrec_for_upd.line_id) THEN
2070         ----------------------------------------------------------------------------------------------
2071         --Add by Xiao for POT change, reg bug#12533434 on 16-May-2011, end
2072 
2073         fnd_file.put_line(fnd_file.LOG,'4 start of invoice reference tax line, rec_c_get_refinvrec_for_upd.reference_id  -> '|| rec_c_get_refinvrec_for_upd.reference_id  );
2074 
2075 
2076         /*########################################################################################################
2077         || Calculation of the Service Tax Component of Credit Memo Tax Amount which needs to be considered
2078         ########################################################################################################*/
2079 
2080         fnd_file.put_line(fnd_file.LOG,'8 Start of Credit Memo Service Tax Component calculation');
2081 
2082         ln_eff_cm_tax_amount := (abs(ln_total_tax_applied)/ln_inv_tot_tax_amt) * rec_c_get_refinvrec_for_upd.recoverable_amount; --modified by walton for inclusive tax 29-Nov-07
2083 
2084         fnd_file.put_line(fnd_file.LOG,'9 rec_c_get_cm_rec_app.cm_app_tax_amt effective     -> '||rec_c_get_cm_rec_app.cm_app_tax_amt
2085                                        ||', ln_inv_tot_tax_amt -> '||ln_inv_tot_tax_amt
2086                                        ||', rec_c_get_refinvrec_for_upd.recoverable_amount  -> '||rec_c_get_refinvrec_for_upd.recoverable_amount
2087                                        ||', cash receipt tax amount is ln_eff_cm_tax_amount -> '||ln_eff_cm_tax_amount
2088                                        ||', rec_c_get_refinvrec_for_upd.recovered_amount    -> '||rec_c_get_refinvrec_for_upd.recovered_amount);
2089 
2090         /*########################################################################################################
2091         || Validate the effective credit memo tax component does not exceed the invoice recoverable amount on updation
2092         ########################################################################################################*/
2093 
2094 
2095         IF rec_c_get_refinvrec_for_upd.recovered_amount + abs(ln_eff_cm_tax_amount) > rec_c_get_refinvrec_for_upd.recoverable_amount THEN
2096           /*
2097           || +ve Credit Memo
2098           || Check that if recovered amount + credit Memo amount > recoverable amount.
2099           || IF yes then set Credit Memo amount = recoverable amount - recovered amount
2100           || so that the recovered amount never exceeds the recoverable amount
2101           */
2102           ln_eff_cm_tax_amount := (rec_c_get_refinvrec_for_upd.recoverable_amount - rec_c_get_refinvrec_for_upd.recovered_amount) ;
2103           fnd_file.put_line(fnd_file.LOG,'10 rec_c_get_refinvrec_for_upd.recovered_amount  +  ln_eff_cm_tax_amount  -> rec_c_get_refinvrec_for_upd.recoverable_amount hence ln_eff_cm_tax_amount '||ln_eff_cm_tax_amount);
2104         END IF;
2105 
2106 
2107         /*########################################################################################################
2108         || Insert the effective Credit Memo tax amount into the repository
2109         ########################################################################################################*/
2110         /*
2111         || Make an entry into the repository with the apportioned Credit Memo Tax amount to be applied against a reference invoice
2112         */
2113         /* ln_amount := abs(ln_eff_cm_tax_amount) * ln_sign_of_credit_memo ; */
2114         ln_amount := abs(ln_eff_cm_tax_amount) ;
2115         /*Bug 10623735
2116          If Invoice and Credit Memo currency are different, data in AR_RECEIVABLE_APPLICATIONS_ALL is populated
2117          in Invoice Currency. Hence the same cannot be passed as Transaction Amount in JAI_RGM_TRX_RECORDS
2118          Transaction Amount Data in JAI_RGM_TRX_RECORDS must be in Credit Memo Currency for CM Application Event.
2119          Hence multiplied by Invoice Exchange Rate if Invoice is in foreign currency and divided
2120          by CM Exchange Rate if CM is in foreign currency
2121         */
2122         IF (rec_c_get_cm_rec_app.invoice_currency_code <> 'INR'
2123            and rec_c_get_cm_rec_app.cm_currency_code = 'INR') THEN
2124             ln_trx_amount := (ln_amount * nvl(rec_c_get_cm_rec_app.invoice_exchange_rate,1)) ;
2125         ELSIF (rec_c_get_cm_rec_app.invoice_currency_code = 'INR'
2126                and rec_c_get_cm_rec_app.cm_currency_code <> 'INR') THEN
2127             ln_trx_amount := (ln_amount / nvl(rec_c_get_cm_rec_app.cm_exchange_rate,1)) ;
2128         ELSE
2129             ln_trx_amount := ln_amount;
2130         END IF;
2131         fnd_file.put_line(fnd_file.LOG,' 13  rec_c_get_cm_rec_app.invoice_currency_code ' || rec_c_get_cm_rec_app.invoice_currency_code ||
2132                                        ' rec_c_get_cm_rec_app.cm_currency_code ' || rec_c_get_cm_rec_app.cm_currency_code ||
2133                                        ' ln_trx_amount ' || ln_trx_amount ||
2134                                        ' ln_amount ' || ln_amount ||
2135                                        ' rec_c_get_cm_rec_app.invoice_exchange_rate ' || rec_c_get_cm_rec_app.invoice_exchange_rate ||
2136                                        ' rec_c_get_cm_rec_app.cm_exchange_rate ' || rec_c_get_cm_rec_app.cm_exchange_rate );
2137         fnd_file.put_line(fnd_file.LOG,' 14 before call to jai_cmn_rgm_recording_pkg.insert_repository_entry ' );
2138 
2139         /*csahoo for bug#5879769...start*/
2140 
2141         ln_organization_id   := NULL;
2142         ln_location_id       := NULL;
2143         lv_service_type_code := NULL;
2144         lv_process_flag      := NULL;
2145         lv_process_message   := NULL;
2146 
2147         jai_trx_repo_extract_pkg.get_doc_from_reference(p_reference_id      => rec_c_get_refinvrec_for_upd.reference_id,
2148                                                         p_organization_id   => ln_organization_id,
2149                                                         p_location_id       => ln_location_id,
2150                                                         p_service_type_code => lv_service_type_code,
2151                                                         p_process_flag      => lv_process_flag,
2152                                                         p_process_message   => lv_process_message
2153                                                         );
2154 
2155          IF  lv_process_flag <> jai_constants.successful THEN
2156            FND_FILE.put_line(fnd_file.log, 'Error Flag:'||lv_process_flag||' Error Message:'||lv_process_message);
2157            return;
2158          END IF;
2159 
2160         /*csahoo for bug#5879769...end*/
2161     /*added the following if condition by Xiao for bug#6773751*/
2162 
2163      if rec_c_get_cm_rec_app.trx_type ='INV' then
2164         lv_attribute_context := 'CM-INV-APP';
2165      elsif rec_c_get_cm_rec_app.trx_type ='DM' then
2166          lv_attribute_context := 'CM-DM-APP';
2167      end if;
2168      /*Bug 10623735*/
2169      ln_func_amount := (ln_amount * nvl(rec_c_get_cm_rec_app.invoice_exchange_rate,1)) ;
2170         jai_cmn_rgm_recording_pkg.insert_repository_entry (
2171                                                             p_repository_id              => ln_repository_id                                              ,
2172                                                             p_regime_id                  => p_regime_id                                                   ,
2173                                                             p_tax_type                   => rec_c_get_refinvrec_for_upd.tax_type                          ,
2174                                                             p_organization_type          => p_organization_type                                           ,
2175                   p_organization_id            => ln_organization_id                                            ,/*5879769*/
2176                   p_location_id                => ln_location_id                                                ,/*5879769*/
2177                                                             p_service_type_code          => lv_service_type_code                                          ,/*5879769*/
2178                                                             p_source                     => p_source                                                      ,
2179                                                             p_source_trx_type            => lv_source_trx_type                                            ,
2180                                                             p_source_table_name          => lv_source_table                              ,
2181                                                             p_source_document_id         => rec_c_get_cm_rec_app.receivable_application_id                ,
2182                                                             p_transaction_date           => rec_c_get_cm_rec_app.cm_transaction_date                      ,
2183                                                             p_account_name               => NULL                                                          ,
2184                                                             p_charge_account_id          => NULL                                                          ,
2185                                                             p_balancing_account_id       => NULL                                                          ,
2186                               -- Replaced ln_amount by ln_func_amount for Bug 7522584
2187                                                             p_amount                     => ln_func_amount                                                ,
2188                                                             p_assessable_value           => NULL                                                          ,
2189                                                             p_tax_rate                   => rec_c_get_refinvrec_for_upd.tax_rate                          ,
2190                                                             p_reference_id               => rec_c_get_refinvrec_for_upd.reference_id                      ,
2191                                                             p_batch_id                   => p_batch_id                                                    ,
2192                                                             p_called_from                => lv_called_from                    ,
2193                                                             p_process_flag               => lv_process_flag                                               ,
2194                                                             p_process_message            => lv_process_message                                            ,
2195                                                             p_discounted_amount          => ln_discounted_amount                                          ,
2196                                                             p_inv_organization_id        => ln_organization_id                                            ,/*5879769*/
2197                                                             p_accounting_date            => rec_c_get_cm_rec_app.gl_date                                  ,
2198                                                             p_currency_code              => rec_c_get_cm_rec_app.cm_currency_code                         ,
2199                                                             p_curr_conv_date             => rec_c_get_cm_rec_app.cm_exchange_date                         ,
2200                                                             p_curr_conv_type             => rec_c_get_cm_rec_app.cm_exchange_rate_type                    ,
2201                                                             p_curr_conv_rate             => rec_c_get_cm_rec_app.cm_exchange_rate                         ,
2202                                                             p_trx_amount                 => ln_trx_amount /*Bug 10623735*/                                                     ,
2203                                                             --Bo Li for Bug9305067 change attribute_cotext to trx_reference_context
2204                                                             -- attribute1 to trx_reference1 and attribute2 to trx_reference2
2205                                                             -----------------------------------------------------------------------------                                                   ,
2206                                                             p_trx_reference_context          => lv_attribute_context                                              ,
2207                                                             p_trx_reference1                 => rec_c_get_cm_rec_app.inv_customer_trx_id                      ,
2208                                                             p_trx_reference2                 => rec_c_get_cm_rec_app.cm_customer_trx_id
2209                                                             -----------------------------------------------------------------------------
2210                                                             , p_accntg_required_flag    => jai_constants.yes --File.Sql.35 Cbabu
2211                                                           );
2212 
2213         fnd_file.put_line(fnd_file.LOG,' 15 Returned from jai_cmn_rgm_recording_pkg.insert_repository_entry ' );
2214 
2215 
2216         IF lv_process_flag = jai_constants.expected_error    OR
2217            lv_process_flag = jai_constants.unexpected_error
2218         THEN
2219           /*
2220           || As Returned status is an error hence:-
2221           ||1. Rollback to save point
2222           ||2. Set out variables p_process_flag and p_process_message accordingly
2223           */
2224           ln_receivable_application_id := rec_c_get_cm_rec_app.receivable_application_id  ;
2225           ROLLBACK to roll_to_cm_app;
2226           fnd_file.put_line( fnd_file.log, '16 error in call to jai_cmn_rgm_recording_pkg.insert_repository_entry - lv_process_flag '||lv_process_flag
2227                                             ||', lv_process_message'||lv_process_message);
2228           p_process_flag    := lv_process_flag    ;
2229           p_process_message := lv_process_message ;
2230           EXIT;
2231         END IF;
2232 
2233 
2234         /*########################################################################################################
2235         || Update the Invoice Reference Recovered Amount with the effective Credit Memo tax amount
2236         ########################################################################################################*/
2237 
2238         fnd_file.put_line(fnd_file.LOG,' 11 before call to jai_cmn_rgm_recording_pkg.update_reference for updating invoice reference' );
2239 
2240         jai_cmn_rgm_recording_pkg.update_reference (
2241                                                      p_source             => p_source                                 ,
2242                                                      p_reference_id       => rec_c_get_refinvrec_for_upd.reference_id ,
2243                                                      p_recovered_amount   => ln_amount                                ,
2244                                                      p_process_flag       => lv_process_flag                          ,
2245                                                      p_process_message    => lv_process_message
2246                                                    );
2247         IF lv_process_flag = jai_constants.expected_error    OR
2248            lv_process_flag = jai_constants.unexpected_error
2249         THEN
2250           /*
2251           || as Returned status is an error hence:-
2252           || Set out variables p_process_flag and p_process_message accordingly
2253           */
2254           ln_receivable_application_id := rec_c_get_cm_rec_app.receivable_application_id  ;
2255           ROLLBACK to roll_to_cm_app;
2256           p_process_flag    := lv_process_flag    ;
2257           p_process_message := lv_process_message ;
2258           fnd_file.put_line( fnd_file.log, '12 error in call to jai_cmn_rgm_recording_pkg.update_reference - lv_process_flag '||lv_process_flag
2259                                             ||', lv_process_message'||lv_process_message);
2260 
2261           EXIT;
2262         END IF;
2263 
2264 
2265         fnd_file.put_line(fnd_file.LOG,' 13 Returned from jai_cmn_rgm_recording_pkg.update_reference  after updating invoice reference' );
2266 
2267         /*
2268         || Get the sum of the effective cm tax amount applied across all the reference invoice lines
2269         */
2270         ln_eff_cm_tottax_amt := nvl(abs(ln_eff_cm_tottax_amt),0) + abs(ln_eff_cm_tax_amount);
2271 
2272         fnd_file.put_line( fnd_file.log, '17 value of ln_eff_cm_tottax_amt  is - '||ln_eff_cm_tottax_amt );
2273      END IF; --Add by Xiao for POT change, reg bug#12533434 on 16-May-2011
2274       END LOOP; -- End of Invoice Reference line processing
2275 
2276 
2277 
2278       /*########################################################################################################
2279       || INSERT CM-CM-APP ENTRIES IN REPOSITORY AND UPDATE THE CREDIT MEMO REFERENCE RECORDS
2280       ########################################################################################################*/
2281       IF nvl(ln_receivable_application_id ,-1) <> rec_c_get_cm_rec_app.receivable_application_id    THEN
2282         /*
2283         || Get the total effective recoverable credit memo amount from the reference table - CM record
2284         */
2285         OPEN  c_get_cmref_totrd_amt ( cp_source_ar           => p_source                                  ,
2286                                       cp_cm_customer_trx_id  => rec_c_get_cm_rec_app.cm_customer_trx_id
2287                                     );
2288         FETCH c_get_cmref_totrd_amt INTO ln_tot_effcm_rb_amt ;
2289         CLOSE c_get_cmref_totrd_amt;
2290         /*
2291         || ln_eff_cm_tottax_amt - represents the total effective cm amount applied across all the corresponding reference invoice records
2292         || ln_tot_effcm_rb_amt  - represents the sum of the total effective recoverable CM amount from the reference table
2293         || (Recoverable amount = total R.B amt - total R.D amount for all the CM records inref table).
2294         || Get the ratio in which the CM needs to be apportioned
2295         */
2296         ln_cm_ref_ratio := abs(ln_eff_cm_tottax_amt)/abs(ln_tot_effcm_rb_amt);
2297 
2298         fnd_file.put_line(fnd_file.LOG,'18 Total Effective CM recoverable amount ln_tot_effcm_rb_amt - ' || ln_tot_effcm_rb_amt
2299                                        ||', actual total cm apportioned amoungst ln_eff_cm_tottax_amt - '|| ln_eff_cm_tottax_amt
2300                                        ||', cm eff ratio ln_cm_ref_ratio - '||ln_cm_ref_ratio
2301                                        );
2302 
2303 
2304         /*
2305         || Update the credit memo reference lines
2306         */
2307         FOR  rec_c_get_refcmrec_for_upd IN c_get_refcmrec_for_upd  ( cp_source_ar           => p_source                                  ,
2308                                                                      cp_cm_customer_trx_id  => rec_c_get_cm_rec_app.cm_customer_trx_id
2309                                                                    )
2310         LOOP
2311           --Add by Xiao for POT change, reg bug#12533434 on 16-May-2011, begin
2312           ----------------------------------------------------------------------------------------------
2313             IF NOT is_accrual_basis(rec_c_get_refcmrec_for_upd.line_id) THEN
2314           ----------------------------------------------------------------------------------------------
2315           --Add by Xiao for POT change, reg bug#12533434 on 16-May-2011, end
2316 
2317           /*
2318           || Initialize the variable ln_cm_ref_upd
2319           */
2320           ln_cm_ref_upd := null;
2321 
2322           fnd_file.put_line(fnd_file.LOG,'19 Processing Credit memo reference tax line, rec_c_get_refcmrec_for_upd.reference_id -> '|| rec_c_get_refcmrec_for_upd.reference_id  );
2323 
2324           /*
2325           || Calculate the value of ln_cm_ref_upd
2326           */
2327           ln_cm_ref_upd := abs(ln_cm_ref_ratio) * abs(rec_c_get_refcmrec_for_upd.recoverable_amount) * ln_sign_of_credit_memo;
2328 
2329           /*########################################################################################################
2330           || Insert the effective Credit Memo tax amount into the repository
2331           ########################################################################################################*/
2332            /*
2333            || Make an entry into the repository with the apportioned Credit Memo Tax amount to be applied against a reference Credit Memo
2334            */
2335 
2336           /*csahoo for bug#5879769...start*/
2337 
2338           ln_organization_id   := NULL;
2339           ln_location_id       := NULL;
2340           lv_service_type_code := NULL;
2341           lv_process_flag      := NULL;
2342           lv_process_message   := NULL;
2343 
2344           jai_trx_repo_extract_pkg.get_doc_from_reference(p_reference_id      => rec_c_get_refcmrec_for_upd.reference_id,
2345                                                           p_organization_id   => ln_organization_id,
2346                                                           p_location_id       => ln_location_id,
2347                                                           p_service_type_code => lv_service_type_code,
2348                                                           p_process_flag      => lv_process_flag,
2349                                                           p_process_message   => lv_process_message
2350                                                           );
2351 
2352            IF  lv_process_flag <> jai_constants.successful THEN
2353              FND_FILE.put_line(fnd_file.log, 'Error Flag:'||lv_process_flag||' Error Message:'||lv_process_message);
2354              return;
2355            END IF;
2356 
2357           /*csahoo for bug#5879769...end*/
2358 
2359           fnd_file.put_line(fnd_file.LOG,' 23 before call to jai_cmn_rgm_recording_pkg.insert_repository_entry ' );
2360           lv_attribute_context := 'CM-CM-APP';--rchandan for bug#4428980
2361           /*Bug 10623735 - Multiply by Exchange Rate only if Invoice is not in INR*/
2362           IF (rec_c_get_cm_rec_app.invoice_currency_code <> 'INR'
2363               and rec_c_get_cm_rec_app.cm_currency_code = 'INR') THEN
2364               ln_trx_amount := (ln_cm_ref_upd * nvl(rec_c_get_cm_rec_app.invoice_exchange_rate,1)) ;
2365           ELSIF (rec_c_get_cm_rec_app.invoice_currency_code = 'INR'
2366                  and rec_c_get_cm_rec_app.cm_currency_code <> 'INR') THEN
2367               ln_trx_amount := (ln_cm_ref_upd / nvl(rec_c_get_cm_rec_app.cm_exchange_rate,1)) ;
2368           ELSE
2369               ln_trx_amount := ln_cm_ref_upd;
2370           END IF;
2371           fnd_file.put_line(fnd_file.LOG,' rec_c_get_cm_rec_app.invoice_currency_code ' || rec_c_get_cm_rec_app.invoice_currency_code ||
2372                                ' rec_c_get_cm_rec_app.cm_currency_code ' || rec_c_get_cm_rec_app.cm_currency_code ||
2373                                ' ln_trx_amount ' || ln_trx_amount ||
2374                                ' ln_cm_ref_upd ' || ln_cm_ref_upd ||
2375                                ' rec_c_get_cm_rec_app.invoice_exchange_rate ' || rec_c_get_cm_rec_app.invoice_exchange_rate ||
2376                                ' rec_c_get_cm_rec_app.cm_exchange_rate ' || rec_c_get_cm_rec_app.cm_exchange_rate );
2377       ln_func_amount := ln_cm_ref_upd * nvl(rec_c_get_cm_rec_app.invoice_exchange_rate,1); -- Added for Bug 7522584
2378           -- Added for Bug 8294236 - Start
2379          ln_total_tax_amt := abs(ln_cm_ref_upd + nvl(ln_discounted_amount,0));
2380          IF (nvl(rec_c_get_cm_rec_app.cm_exchange_rate,1) <> nvl(rec_c_get_cm_rec_app.invoice_exchange_rate,1)
2381             AND rec_c_get_cm_rec_app.cm_currency_code = rec_c_get_cm_rec_app.invoice_currency_code)
2382          THEN
2383             ln_exc_gain_loss_amt := (ln_total_tax_amt * nvl(rec_c_get_cm_rec_app.cm_exchange_rate,1))
2384                                     - (ln_total_tax_amt * nvl(rec_c_get_cm_rec_app.invoice_exchange_rate,1));
2385          ELSE
2386             ln_exc_gain_loss_amt := 0;
2387          END IF;
2388 
2389          fnd_file.put_line(fnd_file.LOG,'ln_total_tax_amt '|| ln_total_tax_amt ||'  rec_c_get_cm_rec_app.cm_exchange_rate '|| rec_c_get_cm_rec_app.cm_exchange_rate ||
2390                                         ' rec_c_get_cm_rec_app.invoice_exchange_rate '|| rec_c_get_cm_rec_app.invoice_exchange_rate ||
2391                                         ' ln_exc_gain_loss_amt '|| ln_exc_gain_loss_amt || ' ln_cm_ref_upd '|| ln_cm_ref_upd ||
2392                                         ' ln_discounted_amount ' ||ln_discounted_amount);
2393 
2394          -- Added for Bug 8294236 - End
2395           jai_cmn_rgm_recording_pkg.insert_repository_entry (
2396                                                               p_repository_id              => ln_repository_id                                    ,
2397                                                               p_regime_id                  => p_regime_id                                         ,
2398                                                               p_tax_type                   => rec_c_get_refcmrec_for_upd.tax_type                 ,
2399                                                               p_organization_type          => p_organization_type                                 ,
2400                                                               p_organization_id            => ln_organization_id                                  ,/*5879769*/
2401                                                         p_location_id                => ln_location_id                                      ,/*5879769*/
2402                                                               p_service_type_code          => lv_service_type_code                                ,/*5879769*/
2403                                                               p_source                     => p_source                                            ,
2404                                                               p_source_trx_type            => lv_source_trx_type                                  ,
2405                                                               p_source_table_name          => lv_source_table                    ,
2406                                                               p_source_document_id         => rec_c_get_cm_rec_app.receivable_application_id      ,
2407                                                               p_transaction_date           => rec_c_get_cm_rec_app.cm_transaction_date            ,
2408                                                               p_account_name               => NULL                                                ,
2409                                                               p_charge_account_id          => NULL                                                ,
2410                                                               p_balancing_account_id       => NULL                                                ,
2411                                 -- Added ln_func_amount for Bug 7522584
2412                                                               p_amount                     => ln_func_amount                                      ,
2413                                                               p_assessable_value           => NULL                                                ,
2414                                                               p_tax_rate                   => rec_c_get_refcmrec_for_upd.tax_rate                 ,
2415                                                               p_reference_id               => rec_c_get_refcmrec_for_upd.reference_id             ,
2416                                                               p_batch_id                   => p_batch_id                                          ,
2417                                                               p_called_from                => lv_called_from          ,
2418                                                               p_process_flag               => lv_process_flag                                     ,
2419                                                               p_process_message            => lv_process_message                                  ,
2420                                                               p_discounted_amount          => ln_discounted_amount                                ,
2421                                                               p_inv_organization_id        => ln_organization_id                                  ,/*5879769*/
2422                                                               p_accounting_date            => rec_c_get_cm_rec_app.gl_date                        ,
2423                                                               p_currency_code              => rec_c_get_cm_rec_app.cm_currency_code               ,
2424                                                               p_curr_conv_date             => rec_c_get_cm_rec_app.cm_exchange_date               ,
2425                                                               p_curr_conv_type             => rec_c_get_cm_rec_app.cm_exchange_rate_type          ,
2426                                                               p_curr_conv_rate             => rec_c_get_cm_rec_app.cm_exchange_rate               ,
2427                                                               p_trx_amount                 => ln_trx_amount /*10623735*/                                        ,
2428                                                               --Bo Li for Bug9305067 change attribute_cotext to trx_reference_context
2429                                                               -- attribute2 to trx_reference2
2430                                                               -----------------------------------------------------------------------------                                   ,
2431                                                               p_trx_reference_context          => lv_attribute_context                                       ,
2432                                                               p_trx_reference2                 => rec_c_get_cm_rec_app.cm_customer_trx_id
2433                                                              -------------------------------------------------------------------------------
2434                                                               , p_accntg_required_flag    => jai_constants.yes --File.Sql.35 Cbabu
2435                                                             );
2436 
2437           fnd_file.put_line(fnd_file.LOG,' 24 Returned from jai_cmn_rgm_recording_pkg.insert_repository_entry ' );
2438 
2439 
2440           IF lv_process_flag = jai_constants.expected_error    OR
2441              lv_process_flag = jai_constants.unexpected_error
2442           THEN
2443             /*
2444             || as Returned status is an error hence:-
2445             ||1. Rollback to save point
2446             ||2. Set out variables p_process_flag and p_process_message accordingly
2447             */
2448             ROLLBACK to roll_to_cm_app;
2449             fnd_file.put_line( fnd_file.log, '25 error in call to jai_cmn_rgm_recording_pkg.insert_repository_entry - lv_process_flag '||lv_process_flag
2450                                               ||', lv_process_message'||lv_process_message);
2451             p_process_flag    := lv_process_flag    ;
2452             p_process_message := lv_process_message ;
2453             EXIT;
2454           END IF;
2455 
2456       -- Added for Bug 8294236 - Start
2457          IF nvl(ln_exc_gain_loss_amt,0) <> 0 THEN
2458 
2459                 jai_cmn_rgm_recording_pkg.exc_gain_loss_accounting(
2460                                         p_repository_id           =>  ln_repository_id                                     ,
2461                                         p_regime_id               =>  p_regime_id                                         ,
2462                                         p_tax_type                =>  rec_c_get_refcmrec_for_upd.tax_type                 ,
2463                                         p_organization_type       =>  p_organization_type                                 ,
2464                                         p_organization_id         =>  ln_organization_id                                  ,
2465                                         p_location_id             =>  ln_location_id                                      ,
2466                                         p_source                  =>  p_source                                            ,
2467                                         p_source_trx_type         =>  lv_source_trx_type                                  ,
2468                                         p_source_table_name       =>  'AR_RECEIVABLE_APPLICATIONS_ALL'                    ,
2469                                         p_source_document_id      =>  rec_c_get_cm_rec_app.receivable_application_id      ,
2470                                         p_transaction_date        =>  rec_c_get_cm_rec_app.cm_transaction_date            ,
2471                                         p_account_name            =>  NULL                                                ,
2472                                         p_charge_account_id       =>  NULL                                                ,
2473                                         p_balancing_account_id    =>  NULL                                                ,
2474                                         p_exc_gain_loss_amt       =>  ln_exc_gain_loss_amt                                ,
2475                                         p_reference_id            =>  rec_c_get_refcmrec_for_upd.reference_id             ,
2476                                         p_called_from             =>  'JAI_RGM_PROCESS_AR_TAXES.POPULATE_CM_APP'          ,
2477                                         p_process_flag            =>  lv_process_flag                                     ,
2478                                         p_process_message         =>  lv_process_message                                  ,
2479                                         p_accounting_date         =>  rec_c_get_cm_rec_app.gl_date
2480                                       );
2481 
2482                 IF lv_process_flag = jai_constants.expected_error    OR
2483                    lv_process_flag = jai_constants.unexpected_error
2484                 THEN
2485                   ln_receivable_application_id :=  rec_c_get_cm_rec_app.receivable_application_id;
2486                   ROLLBACK to roll_to_last_receivable;
2487                   p_process_flag    := lv_process_flag    ;
2488                   p_process_message := lv_process_message ;
2489                   fnd_file.put_line( fnd_file.log, '16.1 error in call to jai_rgm_trx_recording_pkg.exc_gain_loss_accounting - lv_process_flag '||lv_process_flag
2490                                                     ||', lv_process_message'||lv_process_message);
2491                   EXIT;
2492                 END IF;
2493          END IF;
2494           -- Added for Bug 8294236 - End
2495 
2496           fnd_file.put_line(fnd_file.LOG,' 20 before call to jai_cmn_rgm_recording_pkg.update_reference for credit memo references '
2497                                          ||' ,abs(rec_c_get_refcmrec_for_upd.recoverable_amount) -> '||abs(rec_c_get_refcmrec_for_upd.recoverable_amount)
2498                                          ||' ,recovered amount i.e ln_cm_ref_upd -> '||ln_cm_ref_upd
2499                            );
2500 
2501 
2502           /*
2503           || Update the cm reference line with the amount in ln_cm_ref_upd
2504           */
2505           jai_cmn_rgm_recording_pkg.update_reference (
2506                                                        p_source             => p_source                                                             ,
2507                                                        p_reference_id       => rec_c_get_refcmrec_for_upd.reference_id                              ,
2508                                                        p_recovered_amount   => ln_cm_ref_upd                                                        ,
2509                                                        p_process_flag       => lv_process_flag                                                      ,
2510                                                        p_process_message    => lv_process_message
2511                                                      );
2512           fnd_file.put_line(fnd_file.LOG,' 21 Returned from jai_cmn_rgm_recording_pkg.update_reference for credit memo references' );
2513 
2514           IF lv_process_flag = jai_constants.expected_error    OR
2515              lv_process_flag = jai_constants.unexpected_error
2516           THEN
2517             /*
2518             || As Returned status is an error hence:-
2519             ||1. Rollback to save point
2520             ||2. Set out variables p_process_flag and p_process_message accordingly
2521             */
2522             ROLLBACK to roll_to_cm_app;
2523             fnd_file.put_line( fnd_file.log, '22 error in call to  jai_cmn_rgm_recording_pkg.update_reference - lv_process_flag '||lv_process_flag
2524                                               ||', lv_process_message'||lv_process_message);
2525             p_process_flag    := lv_process_flag    ;
2526             p_process_message := lv_process_message ;
2527             EXIT;
2528           END IF;
2529          END IF;--Add by Xiao for POT change, reg bug#12533434 on 16-May-2011.
2530         END LOOP; /* End of Update Credit Memo references */
2531       END IF; /* END IF of nvl(ln_receivable_application_id,-1) <> rec_c_get_cm_rec_app.receivable_application_id  */
2532 
2533     END IF; /* End if of sign of credit memo*/
2534 
2535     ln_uncommitted_transactions := ln_uncommitted_transactions  + 1;
2536     fnd_file.put_line(fnd_file.LOG,' 26 Finished processing the receivable' );
2537 
2538     IF ln_uncommitted_transactions >= 500 THEN
2539       commit;
2540       ln_uncommitted_transactions := 0;
2541     END IF;
2542   END LOOP; -- End of receivables fetch loop
2543 
2544 EXCEPTION
2545   WHEN OTHERS THEN
2546     fnd_file.put_line(fnd_file.LOG,' 27 In exception section of jai_ar_rgm_processing_pkg.populate_cm_app' );
2547     p_process_flag    := jai_constants.unexpected_error;
2548     p_process_message := 'Unexpected error occured while processing jai_ar_rgm_processing_pkg.populate_cm_app'||SQLERRM ;
2549 END populate_cm_app;
2550 
2551 
2552 procedure populate_receipt_records   ( p_regime_id          IN  JAI_RGM_DEFINITIONS.REGIME_ID%TYPE                  ,
2553                                        p_organization_type  IN  JAI_RGM_PARTIES.ORGANIZATION_TYPE%TYPE      ,
2554                                        p_from_date          IN  DATE                                        ,
2555                                        p_to_date            IN  DATE                                        ,
2556                                        p_org_id             IN  RA_CUSTOMER_TRX_ALL.ORG_ID%TYPE             ,
2557                                        p_source             IN  varchar2, --File.Sql.35 Cbabu  jai_constants.SOURCE_AR%TYPE                ,
2558                                        p_batch_id           IN  JAI_RGM_TRX_REFS.BATCH_ID%TYPE              ,
2559                                        p_process_flag OUT NOCOPY VARCHAR2                                    ,
2560                                        p_process_message OUT NOCOPY VARCHAR2
2561                              ,p_organization_id IN  JAI_RGM_PARTIES.ORGANIZATION_ID%TYPE  DEFAULT NULL        )
2562 IS
2563 
2564   ln_receivable_application_id      AR_RECEIVABLE_APPLICATIONS_ALL.RECEIVABLE_APPLICATION_ID%TYPE         ;
2565   ln_eff_cr_tax_amount              AR_RECEIVABLE_APPLICATIONS_ALL.TAX_APPLIED%TYPE                       ;
2566   ln_eff_cr_disc_amount             AR_RECEIVABLE_APPLICATIONS_ALL.TAX_EDISCOUNTED%TYPE                   ;
2567   ln_repository_id                  JAI_RGM_TRX_RECORDS.REPOSITORY_ID%TYPE                                ;
2568   ln_inv_tot_tax_amt                JAI_AR_TRX_TAX_LINES.TAX_AMOUNT%TYPE                           ;
2569   -- Added for Bug 7522584
2570   ln_func_tax_amt                   NUMBER                                                                ;
2571   lv_process_flag                   VARCHAR2(2)                                                           ;
2572   lv_process_message                VARCHAR2(1996)                                                        ;
2573   lv_source_trx_type                VARCHAR2(50)                                                          ;
2574   lv_attribute_context              VARCHAR2(50)                                                          ;
2575   ln_uncommitted_transactions       NUMBER(10)     := 0                                                   ;
2576   lv_service_type_code              JAI_AR_TRX_LINES.service_type_code%TYPE                    ;/*5879769*/
2577   ln_location_id                    NUMBER(15);/*5879769*/
2578   lv_source_table                   CONSTANT jai_rgm_trx_records.source_table_name%TYPE := 'AR_RECEIVABLE_APPLICATIONS_ALL';--rchandan for bug#4428980
2579   lv_called_from                    CONSTANT varchar2(100) := 'jai_ar_rgm_processing_pkg.POPULATE_RECEIPT_RECORDS';--rchandan for bug#4428980
2580 
2581   ln_total_tax_applied              NUMBER;     --added by walton for inclusive tax 29-Nov-07
2582   ln_line_total_amt                 NUMBER;     --added by walton for inclusive tax 29-Nov-07
2583   ln_inclusive_total_amt            NUMBER;     --added by walton for inclusive tax 29-Nov-07
2584 
2585   ln_exc_gain_loss_amt              NUMBER; -- Added for Bug 8294236
2586   ln_total_tax_amt                  NUMBER; -- Added for Bug 8294236
2587   ln_tmp_line_total_amt             NUMBER; -- Added for Bug 9919612
2588   ln_trx_amount                     NUMBER; -- Added for Bug 10623735
2589  /*
2590   || Get the cash receipt, Invoice and Total Effective Invoice Recoverable Amount for them.
2591   || Consider only those cash receipts which follow the conditions as given below:-
2592   || 1. Invoice tax line reference exists in the jai_rgm_trx_refs table for the invoice against which the cash receipt is being applied
2593   || 2. The cash receipt tax line does not already exist in the repository i.e jai_rgm_trx_records.
2594   || 3. Consider cash receipt application against an invoice only
2595   || 4. Invoice tax line has not been fully recovered i.e recovered_amount < recoverable_amount in references table
2596   || 5.Consider Cash Receipt application for DM ,bug #6773751
2597  */
2598 
2599   /*Bug 10623735 - Added order by clause on aral.receivable_application_id*/
2600   CURSOR c_get_rec_app     ( cp_source_ar varchar2) --File.Sql.35 Cbabu ( jai_constants.SOURCE_AR%TYPE )
2601   IS
2602   SELECT
2603           trx.customer_trx_id                                                                                     ,
2604           acrl.cash_receipt_id                                                                                    ,
2605           aral.receivable_application_id                                                                          ,
2606           aral.gl_date                                                                                            ,
2607           sign(nvl(aral.tax_applied,0))                           sign_of_cash_receipt                            ,
2608           sign(nvl(tax_uediscounted,0) + nvl(tax_ediscounted,0))  sign_of_cr_disc                                 ,
2609           nvl(aral.tax_applied,0)                                 cash_rcpt_tax_amt                               ,
2610           nvl(tax_uediscounted,0) + nvl(tax_ediscounted,0)        cr_tax_disc_amt                                 ,
2611           nvl(aral.amount_applied,0)                              receipt_amount                                  ,
2612           nvl(aral.line_applied,0)                                cash_rcpt_line_amt                              , --added by walton for inclusive tax 29-Nov-07
2613           acrl.receipt_date                                                                                       ,
2614           acrl.currency_code                                      receipt_currency_code                           ,
2615           acrl.exchange_date                                      receipt_exchange_date                           ,
2616           acrl.exchange_rate                                      receipt_exchange_rate                           ,
2617           acrl.exchange_rate_type                                 receipt_exchange_rate_type                      ,
2618           jtrx.organization_id                                    inv_organization_id                             ,
2619           trx_types.type                                          trx_type, --added by Xiao for bug#6773751
2620 
2621       -- Added for Bug 8294236
2622          trx.invoice_currency_code                               invoice_currency_code                           ,
2623          trx.exchange_rate                                       invoice_exchange_rate                           ,
2624          trx.exchange_date                                       invoice_exchange_date                           ,
2625          trx.exchange_rate_type                                  invoice_exchange_rate_type
2626          -- Added for Bug 8294236
2627   FROM
2628           ar_receivable_applications_all  aral                                                                    ,
2629           ar_cash_receipts_all            acrl                                                                    ,
2630           ra_customer_trx_all             trx                                                                     ,
2631           ra_cust_trx_types_all           trx_types                                                               ,
2632           JAI_AR_TRXS           jtrx
2633   WHERE
2634           aral.cash_receipt_id         = acrl.cash_receipt_id                                                                       AND
2635           aral.applied_customer_trx_id = trx.customer_trx_id                                                                        AND
2636           trunc(aral.creation_date)    BETWEEN trunc(p_from_date) and trunc(p_to_date)                                              AND
2637           upper(aral.application_type) = upper(jai_constants.ar_cash)                                                               AND
2638           upper(aral.status)           = upper(jai_constants.ar_status_app)                                                         AND
2639           jtrx.organization_id =p_organization_id                                                                                   AND --Added by kunkumar for forward porting to R12
2640           /*nvl(aral.tax_applied,0)    <> 0                                                                                         AND*/--Modified by walton for inclusive tax 29-Nov-07
2641           trx.org_id                   = nvl(p_org_id,trx.org_id)                                                                   AND
2642           trx.complete_flag            = 'Y'                                                                                        AND
2643           trx_types.cust_trx_type_id   = trx.cust_trx_type_id                                                                       AND
2644           upper(trx_types.type)        IN (upper(jai_constants.ar_invoice_type_inv),upper(jai_constants.ar_invoice_type_cm)
2645                                           ,upper(jai_constants.ar_doc_type_dm))        AND /* Added ar_doc_type_dm for bug# 6773751 */
2646           trx_types.org_id             = trx.org_id                                                                                 AND
2647           trx.customer_trx_id          = jtrx.customer_trx_id                                                                       AND
2648           NOT EXISTS                   ( SELECT         /*A receipt application does not exist in repository */
2649                                                  1
2650                                          FROM
2651                                                  jai_rgm_trx_records  rgtr
2652                                          WHERE
2653                                                  rgtr.source               = cp_source_ar                             AND
2654                                                  rgtr.organization_id      = p_organization_id                        AND/*5879769*/
2655                                                  rgtr.source_table_name    = lv_source_table         AND
2656                                                  --Added by Qiong for bug13579826 begin
2657                                                  rgtr.source_trx_type in (jai_constants.trx_type_rct_app,jai_constants.trx_type_rct_rvs) AND
2658                                                  --Added by Qiong for bug13579826 end
2659                                                  rgtr.source_document_id   = aral.receivable_application_id
2660                                        )                                                                              AND
2661                 EXISTS                (
2662                                         SELECT        /* A invoice exists in the reference table with total recoverable amount - discounted_amount > recovered amount*/
2663                                                 1
2664                                         FROM
2665                                                 jai_rgm_trx_refs                rgtf
2666                                         WHERE
2667                                                 rgtf.source                    =  jai_constants.SOURCE_AR            AND
2668                                                 rgtf.invoice_id                = aral.applied_customer_trx_id         AND
2669                                                 (
2670                           /*Bug 11932841 - Tax applied can be zero if the AR Document has only inclusive Taxes. Replaced > with >= and < with <=*/
2671                           (   /*Scope of recovery is possible for cash receipt application */
2672                             /*Bug 11787045 - In case a Credit Memo is applied on a Receipt and reversal of the same happens,
2673                             the Receipt Application ID is not picked up as recoverable_amount - discounted_amount is not greater than
2674                             recovered_amount. Hence added abs*/
2675                             /*Rolling back the changes done for Bug 11787045 and removing abs by mmurtuza */
2676                             nvl(rgtf.recoverable_amount,0) - nvl(rgtf.discounted_amount,0) > nvl(rgtf.recovered_amount,0) AND
2677                             nvl(aral.tax_applied,0) > 0
2678                           )                                                                                           OR
2679                           ( /* As it is a case of cash receipt reversal hence do not check for recovery. */
2680                                                     nvl(aral.tax_applied,0) <= 0
2681                           )
2682                         )
2683                     )
2684   order by aral.receivable_application_id;
2685 
2686    /*
2687    || Get the Total transactional tax amount for the invoice
2688    */
2689    /*Bug 11932841 - Modified cursor to get only Exclusive Tax amounts, Added parameter cp_inv_cus_trx_line_id and cp_inv_tax_trx_line_id*/
2690    CURSOR cur_get_inv_tottax_amt (cp_inv_customer_trx_id JAI_AR_TRXS.CUSTOMER_TRX_ID%TYPE,
2691                                   cp_inv_cus_trx_line_id NUMBER,
2692                                   cp_inv_tax_trx_line_id NUMBER)
2693    IS
2694    SELECT
2695           nvl(sum(jrttl.tax_amount),0) inv_tot_tax_amount
2696    FROM
2697           JAI_AR_TRX_LINES    jrtl  ,
2698           JAI_AR_TRX_TAX_LINES    jrttl ,
2699           jai_cmn_taxes_all    tax
2700    WHERE
2701           jrtl.customer_trx_line_id   = jrttl.link_to_cust_trx_line_id  AND
2702           jrtl.customer_trx_line_id   = nvl(cp_inv_cus_trx_line_id, jrtl.customer_trx_line_id) AND
2703           jrttl.customer_trx_line_id  = nvl(cp_inv_tax_trx_line_id, jrttl.customer_trx_line_id) AND
2704           jrtl.customer_trx_id        = cp_inv_customer_trx_id AND
2705           jrttl.tax_id                = tax.tax_id AND
2706           NVL(tax.inclusive_tax_flag,'N') = 'N';
2707 
2708    --added by walton for inclusive tax 29-Nov-07
2709    ---------------------------------------------------------------
2710    --Get the Total inclusive tax amount for the invoice,
2711    /*Bug 11932841 - Added parameter pn_inv_cus_trx_line_id and pn_inv_tax_trx_line_id*/
2712    CURSOR cur_get_inv_inclusive_tax_amt
2713    ( pn_inv_customer_trx_id JAI_AR_TRXS.CUSTOMER_TRX_ID%TYPE,
2714      pn_inv_cus_trx_line_id NUMBER,
2715      pn_inv_tax_trx_line_id NUMBER
2716    )
2717    IS
2718    SELECT
2719      nvl(sum(jrttl.tax_amount),0) inv_tot_inclusive_tax_amt
2720    FROM
2721      JAI_AR_TRX_LINES    jrtl
2722    , JAI_AR_TRX_TAX_LINES    jrttl
2723    , jai_cmn_taxes_all    tax
2724    WHERE jrtl.customer_trx_line_id   = jrttl.link_to_cust_trx_line_id
2725      AND jrtl.customer_trx_id        = pn_inv_customer_trx_id
2726      AND jrtl.customer_trx_line_id   = nvl(pn_inv_cus_trx_line_id, jrtl.customer_trx_line_id)
2727      AND jrttl.customer_trx_line_id  = nvl(pn_inv_tax_trx_line_id, jrttl.customer_trx_line_id)
2728      AND jrttl.tax_id                = tax.tax_id
2729      AND NVL(tax.inclusive_tax_flag,'N') = 'Y' ;
2730 
2731    --Get the Total AR transaction line amount,
2732    /*Bug 11932841 - Added parameter pn_inv_cus_trx_line_id*/
2733    CURSOR cur_get_inv_line_amt
2734    ( pn_inv_customer_trx_id JAI_AR_TRXS.CUSTOMER_TRX_ID%TYPE,
2735      pn_inv_cus_trx_line_id NUMBER
2736    )
2737    IS
2738    SELECT
2739      nvl(sum(jrtl.line_amount),0) inv_tot_line_amt
2740    FROM
2741      JAI_AR_TRX_LINES    jrtl
2742    WHERE jrtl.customer_trx_id        = pn_inv_customer_trx_id
2743    AND jrtl.customer_trx_line_id     = nvl(pn_inv_cus_trx_line_id, jrtl.customer_trx_line_id);
2744    --------------------------------------------------------------------------------
2745   /*
2746   || Get the ref tax lines pertaining to the invoice against which the cash receipt has been applied : -
2747   */
2748   CURSOR c_get_refrec_for_upd    ( cp_source_ar        varchar2, --File.Sql.35 Cbabu  jai_constants.SOURCE_AR%TYPE        ,
2749                                    cp_customer_trx_id  RA_CUSTOMER_TRX_ALL.CUSTOMER_TRX_ID%TYPE
2750                                   )
2751   IS
2752   SELECT
2753           reference_id                                                                                       ,
2754           invoice_id                                                                                         ,/*Bug 10148245*/
2755           line_id                                                                                            ,/*Bug 10148245*/
2756           tax_type                                                                                           ,
2757           tax_rate                                                                                           ,
2758           nvl(discounted_amount,0)                                  discounted_amount                        ,
2759           nvl(recoverable_amount,0) - nvl(discounted_amount,0)      recoverable_amount                       ,
2760           nvl(recovered_amount,0)                                   recovered_amount                         ,
2761           item_line_id                                              /*5879769*/                              ,
2762           NVL(reversal_flag,'N')                                    reversal_flag --Added by Eric Ma for bug 10230041 on Oct-28-20100
2763   FROM
2764           jai_rgm_trx_refs
2765   WHERE
2766           source                                                    = cp_source_ar                           AND
2767           invoice_id                                                = cp_customer_trx_id                     AND
2768 
2769          --Commented out  by Eric Ma for bug 10230041 on Oct-28-2010
2770          -- NVL(recoverable_amount,0) - nvl(discounted_amount,0) >= nvl(recovered_amount,0) /*Modified the comparison condition to >= for Bug 6474509*/
2771 
2772          --Added by Eric Ma for bug 10230041 on Oct-28-2010, Begin
2773          ------------------------------------------------------------------------------------------------------------------------
2774          ( ((REVERSAL_FLAG <>'Y') AND (nvl(recoverable_amount, 0) - nvl(discounted_amount, 0) >=nvl(recovered_amount, 0)))
2775          OR
2776            ((REVERSAL_FLAG = 'Y') AND (nvl(ABS(recoverable_amount), 0) - nvl(ABS(discounted_amount), 0) >=nvl(ABS(recovered_amount), 0)))
2777          )
2778          ------------------------------------------------------------------------------------------------------------------------
2779          --Added by Eric Ma for bug 10230041 on Oct-28-2010, End
2780 
2781  FOR      UPDATE NOWAIT ;
2782 
2783  /*Bug 10148245 - Start*/
2784 
2785  CURSOR c_chk_apply_in_detail (cp_cash_receipt_id NUMBER, cp_receivable_appln_id NUMBER)
2786  IS
2787  SELECT 1
2788  FROM ar_activity_details
2789  WHERE cash_receipt_id = cp_cash_receipt_id
2790  AND source_id = cp_receivable_appln_id;
2791 
2792  CURSOR c_line_trx_id (cp_cust_trx_line_id NUMBER)
2793  IS
2794  SELECT link_to_cust_trx_line_id
2795  FROM ra_customer_trx_lines_all
2796  WHERE customer_trx_line_id = cp_cust_trx_line_id
2797  AND line_type = 'TAX';
2798 
2799  CURSOR c_tot_tax_amount (cp_cust_trx_line_id NUMBER)
2800  IS
2801  SELECT sum(extended_amount)
2802  FROM ra_customer_trx_lines_all
2803  WHERE link_to_cust_trx_line_id = cp_cust_trx_line_id
2804  AND line_type = 'TAX';
2805 
2806  CURSOR c_receipt_tax_amount (cp_cust_trx_line_id NUMBER, cp_cash_receipt_id NUMBER, cp_receivable_appln_id NUMBER)
2807  IS
2808  SELECT nvl(sum(tax), 0), nvl(sum(tax_discount), 0)
2809  FROM ar_activity_details
2810  WHERE customer_trx_line_id = cp_cust_trx_line_id
2811  AND cash_receipt_id = cp_cash_receipt_id
2812  AND source_id = cp_receivable_appln_id;
2813 
2814  ln_apply_in_detail             NUMBER;
2815  ln_link_to_cust_trx_id         NUMBER;
2816  ln_receipt_tax_amt             NUMBER;
2817  ln_rcpt_tax_disc_amt           NUMBER;
2818  ln_cash_rcpt_tax_amt           NUMBER;/*Bug 11932841*/
2819  ln_cash_rcpt_line_amt          NUMBER;/*Bug 11932841*/
2820  ln_cr_tax_disc_amt             NUMBER;/*Bug 11932841*/
2821  ln_inv_tax_amt                 NUMBER;/*Bug 11932841*/
2822  ln_inclusive_amt               NUMBER;/*Bug 11932841*/
2823  ln_line_amt                    NUMBER;/*Bug 11932841*/
2824  /*Bug 10148245 - End*/
2825 
2826 
2827 BEGIN
2828 
2829   fnd_file.put_line(fnd_file.LOG,'1 Entering procedure  : jai_rgm_process_ar.populate_receipt_records' );
2830 
2831   /*
2832   ||Initialize the variables
2833   */
2834   p_process_flag     := jai_constants.successful     ;
2835   p_process_message  := null                         ;
2836 
2837 
2838   /*****
2839   || Get all the valid receivables
2840   *****/
2841   FOR rec_c_get_rec_app IN c_get_rec_app ( cp_source_ar => p_source  )
2842   LOOP
2843 
2844     fnd_file.put_line(fnd_file.LOG,'2 processing cash receipt with receivable_application_id as '||rec_c_get_rec_app.receivable_application_id
2845                                    ||'against invoice with customer_trx_id '||rec_c_get_rec_app.customer_trx_id );
2846 
2847     /*########################################################################################################
2848     || Initializing the variables
2849     ########################################################################################################*/
2850     ln_receivable_application_id := null                      ;
2851     ln_eff_cr_tax_amount         := null                      ;
2852     ln_eff_cr_disc_amount        := null                      ;
2853     lv_source_trx_type           := null                      ;
2854     ln_inv_tot_tax_amt           := null                      ;
2855   ln_func_tax_amt              := null                      ; -- Added for Bug 7522584
2856     lv_process_flag              := jai_constants.successful  ;
2857     lv_process_message           := null                      ;
2858 
2859     ln_inclusive_total_amt       := null                      ; --added by walton for inclusive tax 29-Nov-07
2860 
2861     fnd_file.put_line(fnd_file.LOG,'3 Variables initialized' );
2862 
2863     SAVEPOINT roll_to_last_receivable;
2864 
2865     /*Bug 11932841 - Passes NULL to LINE_ID parameters to get the total amount for the transaction - Start*/
2866     OPEN  cur_get_inv_tottax_amt (cp_inv_customer_trx_id => rec_c_get_rec_app.customer_trx_id,
2867                                   cp_inv_cus_trx_line_id => NULL,
2868                                   cp_inv_tax_trx_line_id => NULL);
2869     FETCH cur_get_inv_tottax_amt INTO ln_inv_tax_amt;
2870     CLOSE cur_get_inv_tottax_amt ;
2871 
2872     IF (ln_inv_tax_amt = 0)
2873     THEN
2874         ln_inv_tax_amt := 1;
2875     END IF;
2876 
2877     --added by walton for inclusive tax  29-Nov-07
2878     OPEN  cur_get_inv_inclusive_tax_amt( pn_inv_customer_trx_id => rec_c_get_rec_app.customer_trx_id,
2879                                          pn_inv_cus_trx_line_id => NULL,
2880                                          pn_inv_tax_trx_line_id => NULL
2881                                        );
2882     FETCH cur_get_inv_inclusive_tax_amt
2883     INTO  ln_inclusive_amt;
2884     CLOSE cur_get_inv_inclusive_tax_amt ;
2885 
2886     OPEN  cur_get_inv_line_amt(pn_inv_customer_trx_id => rec_c_get_rec_app.customer_trx_id,
2887                                pn_inv_cus_trx_line_id => NULL
2888                               );
2889     FETCH cur_get_inv_line_amt
2890     INTO  ln_line_amt;
2891     CLOSE cur_get_inv_line_amt ;
2892 
2893     IF (ln_line_amt = 0)
2894     THEN
2895         ln_line_amt := 1;
2896     END IF;
2897     /*Bug 11932841 - End*/
2898 
2899     FOR  rec_c_get_refrec_for_upd IN c_get_refrec_for_upd  ( cp_source_ar        => p_source                           ,
2900                                                              cp_customer_trx_id  => rec_c_get_rec_app.customer_trx_id
2901                                                            )
2902     LOOP
2903          --Add by Xiao for POT change, reg bug#12533434 on 16-May-2011, begin
2904          ----------------------------------------------------------------------------------------------
2905          IF NOT is_accrual_basis(rec_c_get_refrec_for_upd.line_id) THEN
2906          ----------------------------------------------------------------------------------------------
2907          --Add by Xiao for POT change, reg bug#12533434 on 16-May-2011, begin
2908 
2909         /*Bug 11932841 - Calculate Tax Applied amount, Line Applied and Discount per line*/
2910         ln_cash_rcpt_tax_amt := 0;
2911         ln_cash_rcpt_line_amt := 0;
2912         ln_cr_tax_disc_amt := 0;
2913         ln_inv_tot_tax_amt := 0;
2914         ln_inclusive_total_amt := 0;
2915         ln_line_total_amt := 0;
2916 
2917         /*Bug 11932841 - Fetch line and Tax amount inside loop rec_c_get_refrec_for_upd per line - Start*/
2918         OPEN  cur_get_inv_tottax_amt (cp_inv_customer_trx_id => rec_c_get_rec_app.customer_trx_id,
2919                                       cp_inv_cus_trx_line_id => rec_c_get_refrec_for_upd.item_line_id,
2920                                       cp_inv_tax_trx_line_id => rec_c_get_refrec_for_upd.line_id);
2921         FETCH cur_get_inv_tottax_amt INTO ln_inv_tot_tax_amt;
2922         CLOSE cur_get_inv_tottax_amt ;
2923 
2924         OPEN  cur_get_inv_inclusive_tax_amt( pn_inv_customer_trx_id => rec_c_get_rec_app.customer_trx_id,
2925                                              pn_inv_cus_trx_line_id => rec_c_get_refrec_for_upd.item_line_id,
2926                                              pn_inv_tax_trx_line_id => rec_c_get_refrec_for_upd.line_id
2927                                            );
2928         FETCH cur_get_inv_inclusive_tax_amt
2929         INTO  ln_inclusive_total_amt;
2930         CLOSE cur_get_inv_inclusive_tax_amt ;
2931 
2932         OPEN  cur_get_inv_line_amt(pn_inv_customer_trx_id => rec_c_get_rec_app.customer_trx_id,
2933                                    pn_inv_cus_trx_line_id => rec_c_get_refrec_for_upd.item_line_id
2934                                   );
2935         FETCH cur_get_inv_line_amt
2936         INTO  ln_line_total_amt;
2937         CLOSE cur_get_inv_line_amt ;
2938 
2939         /*Bug 9919612 - If an AR Invoice is created with just Adhoc Service Tax(with no Line Amount) then
2940         ln_line_total_amt would be zero resulting in ORA-01476 during calculation of ln_total_tax_applied*/
2941         IF ln_line_total_amt = 0 OR ln_line_total_amt IS NULL THEN
2942             ln_tmp_line_total_amt := 1;
2943         ELSE
2944             ln_tmp_line_total_amt := ln_line_total_amt;
2945         END IF;
2946         ln_cash_rcpt_line_amt := (ln_line_total_amt/ln_line_amt) * rec_c_get_rec_app.cash_rcpt_line_amt;
2947         ln_cash_rcpt_tax_amt := (ln_inv_tot_tax_amt/ln_inv_tax_amt) * rec_c_get_rec_app.cash_rcpt_tax_amt;
2948         ln_cr_tax_disc_amt := (ln_inv_tot_tax_amt/ln_inv_tax_amt) * rec_c_get_rec_app.cr_tax_disc_amt;
2949         fnd_file.put_line(fnd_file.LOG,'3.1 rec_c_get_rec_app.customer_trx_id: '|| rec_c_get_rec_app.customer_trx_id || ' '
2950                          || ' rec_c_get_refrec_for_upd.item_line_id: ' || rec_c_get_refrec_for_upd.item_line_id);
2951         fnd_file.put_line(fnd_file.LOG,'3.2 ln_line_total_amt: '|| ln_line_total_amt || ' ' || ' ln_inv_tot_tax_amt: ' || ln_inv_tot_tax_amt
2952                          || ' ' || 'ln_line_amt: ' || ln_line_amt || ' ' || 'ln_inv_tax_amt: ' || ln_inv_tax_amt);
2953         fnd_file.put_line(fnd_file.LOG,'3.3 ln_cash_rcpt_line_amt: '|| ln_cash_rcpt_line_amt || ' ' || ' ln_cash_rcpt_tax_amt: ' || ln_cash_rcpt_tax_amt
2954                          || ' ' || 'Inclusive amt: ' || (rec_c_get_rec_app.cash_rcpt_line_amt/ln_line_amt) * ln_inclusive_total_amt);
2955         ln_total_tax_applied := ln_cash_rcpt_tax_amt + ((rec_c_get_rec_app.cash_rcpt_line_amt/ln_line_amt) * ln_inclusive_total_amt);
2956         /*Bug 11932841 - End*/
2957       fnd_file.put_line(fnd_file.LOG,'4 start of invoice reference tax line , rec_c_get_refrec_for_upd.reference_id   -> '|| rec_c_get_refrec_for_upd.reference_id  );
2958       lv_attribute_context := NULL;
2959       /*########################################################################################################
2960       || Determine the receipt Application Type (Receipt Application, Receipt Reversal)
2961       ########################################################################################################*/
2962       fnd_file.put_line(fnd_file.LOG,'5 start of determine application source type');
2963 
2964       /*
2965       || If the cash receipt tax amount is -ve then it is RECEIPT REVERSAL/UNAPPLICATION
2966       || IF the cash receipt tax receipt amount is +ve then it is RECEIPT APPLICATION
2967       || This info would go into the source_trx_type in the repository table jai_rgm_trx_records .
2968       || This would help distinctly identify a receipt application, receipt reversal and a credit memo
2969       || IF sign = 0 i.e cash receipt amount = 0 then proceed exit the current loop and proceed with the next receivable application.
2970       */
2971       IF rec_c_get_rec_app.sign_of_cash_receipt > 0  or ln_total_tax_applied >0 THEN --modified by walton for inclusive tax 29-Nov-07
2972         /*
2973         || +ve cash receipt hence RECEIPT APPLICATION
2974         */
2975         lv_source_trx_type   := jai_constants.trx_type_rct_app ;
2976   /*the following conditions added by Xiao for bug#6773751*/
2977   if rec_c_get_rec_app.trx_type='INV'  then
2978           lv_attribute_context := 'CR-INV-APP'           ;
2979   elsif  rec_c_get_rec_app.trx_type='DM'  then
2980        lv_attribute_context := 'CR-DM-APP'           ;
2981   elsif     rec_c_get_rec_app.trx_type='CM'  then
2982        lv_attribute_context := 'CR-CM-APP'           ;
2983    end if;
2984    --end bug#6773751
2985 
2986       ELSIF rec_c_get_rec_app.sign_of_cash_receipt < 0 or ln_total_tax_applied <0 THEN --modified by walton for inclusive tax 29-Nov-07
2987         /*
2988         || -ve cash receipt hence RECEIPT REVERSAL/UNAPPLICATION
2989         */
2990         lv_source_trx_type := jai_constants.trx_type_rct_rvs ;
2991        /*the following conditions added by Xiao for bug#6773751*/
2992   if rec_c_get_rec_app.trx_type='INV'  then
2993           lv_attribute_context := 'CR-INV-REV'           ;
2994   elsif  rec_c_get_rec_app.trx_type='DM'  then
2995        lv_attribute_context := 'CR-DM-REV'           ;
2996   elsif     rec_c_get_rec_app.trx_type='CM'  then
2997        lv_attribute_context := 'CR-CM-REV'           ;
2998   end if;
2999    --end bug#6773751
3000 
3001       ELSE
3002         /*
3003         || cash receipt amount is zero hence exit current loop and process next receipt application
3004         */
3005         fnd_file.put_line(fnd_file.LOG,'6 cash receipt has sign = 0 i.e tax applied amount = 0 hence exit ');
3006         exit;
3007       END IF;
3008 
3009       fnd_file.put_line(fnd_file.LOG,'7  cash receipt is  lv_source_trx_type->'||lv_source_trx_type);
3010 
3011       /*########################################################################################################
3012       || Calculation of the Service Tax Component of Cash Receipt Tax Amount which needs to be considered
3013       ########################################################################################################*/
3014 
3015       fnd_file.put_line(fnd_file.LOG,'8 Start of Cash Receipt Service Tax Component calculation, ln_inv_tot_tax_amt'||ln_inv_tot_tax_amt);
3016 
3017       /*Bug 10148245 - Start*/
3018 
3019       OPEN c_chk_apply_in_detail(rec_c_get_rec_app.cash_receipt_id, rec_c_get_rec_app.receivable_application_id);
3020       FETCH c_chk_apply_in_detail INTO ln_apply_in_detail;
3021       CLOSE c_chk_apply_in_detail;
3022 
3023       IF (ln_apply_in_detail = 1) THEN
3024 
3025         OPEN c_line_trx_id(rec_c_get_refrec_for_upd.line_id);
3026         FETCH c_line_trx_id INTO ln_link_to_cust_trx_id;
3027         CLOSE c_line_trx_id;
3028 
3029         OPEN c_tot_tax_amount(ln_link_to_cust_trx_id);
3030         FETCH c_tot_tax_amount INTO ln_inv_tot_tax_amt;
3031         CLOSE c_tot_tax_amount;
3032 
3033         fnd_file.put_line(fnd_file.LOG,'8.1 Total Invoice Amount for Line ' || ln_link_to_cust_trx_id || ' is ln_inv_tot_tax_amt '||ln_inv_tot_tax_amt);
3034 
3035         OPEN c_receipt_tax_amount(ln_link_to_cust_trx_id, rec_c_get_rec_app.cash_receipt_id, rec_c_get_rec_app.receivable_application_id);
3036         FETCH c_receipt_tax_amount INTO ln_receipt_tax_amt, ln_rcpt_tax_disc_amt;
3037         CLOSE c_receipt_tax_amount;
3038 
3039         fnd_file.put_line(fnd_file.LOG,'8.2 Tax Amount applied ' || ln_receipt_tax_amt || ' Tax Discount Applied '|| ln_rcpt_tax_disc_amt);
3040 
3041         /*Bug 11932841 - If Exclusive Tax amount is zero, then set lv_inv_tot_tax_amt to 1 to avoid Divisor by Zero error in further calculations*/
3042         IF ln_inv_tot_tax_amt = 0 THEN
3043             ln_inv_tot_tax_amt := 1;
3044         END IF;
3045 
3046         /*Bug 11932841 - Receipt Tax amount shall be zero in case a Receipt is applied on a AR Document with only Inclusive Taxes
3047         as Inclusive Tax is part of Line amount and there are no separate Tax Distributions. In those cases ln_eff_cr_tax_amount will be
3048         zero as ln_receipt_tax_amt will be zero. But the Inclusive Tax needs to hit the repository.*/
3049         ln_eff_cr_tax_amount  := (ln_receipt_tax_amt/ln_inv_tot_tax_amt) * rec_c_get_refrec_for_upd.recoverable_amount +
3050                                  nvl((rec_c_get_rec_app.cash_rcpt_line_amt/ln_line_amt) * ln_inclusive_total_amt, 0);
3051 
3052       ELSE
3053         /*Bug 11932841 - If Exclusive Tax amount is zero, then set lv_inv_tot_tax_amt to 1 to avoid Divisor by Zero error in further calculations*/
3054         IF ln_inv_tot_tax_amt = 0 THEN
3055             ln_inv_tot_tax_amt := 1;
3056         END IF;
3057         /*Bug 11932841 - Receipt Tax amount shall be zero in case a Receipt is applied on a AR Document with only Inclusive Taxes
3058         as Inclusive Tax is part of Line amount and there are no separate Tax Distributions. In those cases ln_eff_cr_tax_amount will be
3059         zero as rec_c_get_rec_app.cash_rcpt_line_amt will be zero. But the Inclusive Tax needs to hit the repository.*/
3060         ln_eff_cr_tax_amount  := (ln_cash_rcpt_tax_amt/ln_inv_tot_tax_amt ) * rec_c_get_refrec_for_upd.recoverable_amount +
3061                                  nvl((rec_c_get_rec_app.cash_rcpt_line_amt/ln_line_amt) * ln_inclusive_total_amt, 0);
3062 
3063       END IF;
3064 
3065       /*Moved the following calculation inside the above IF clause*/
3066       --ln_eff_cr_tax_amount  := (rec_c_get_rec_app.cash_rcpt_tax_amt/ln_inv_tot_tax_amt ) * rec_c_get_refrec_for_upd.recoverable_amount ;
3067       /*Bug 10148245 - End*/
3068 
3069       --Added REVERSAL_FLAG LOGIC  by  Eric Ma for bug 10230041 on Oct-28-2010, Begin
3070       -------------------------------------------------------------------------------------
3071       IF NVL(rec_c_get_refrec_for_upd.REVERSAL_FLAG,'N') = 'N'
3072       THEN
3073         IF  rec_c_get_refrec_for_upd.recovered_amount +  ln_eff_cr_tax_amount < 0 THEN
3074           /*
3075           || -ve cash receipt
3076           || Check that if recovered amount + cash receipt amount < 0 then the cash receipt amount should be equal to the recovered amount
3077           || so that the net recovered amount equals zero.
3078           */
3079           ln_eff_cr_tax_amount := rec_c_get_refrec_for_upd.recovered_amount * rec_c_get_rec_app.sign_of_cash_receipt;
3080 
3081           fnd_file.put_line(fnd_file.LOG,'10 rec_c_get_refrec_for_upd.recovered_amount +  ln_eff_cr_tax_amount < 0 , ln_eff_cr_tax_amount '||ln_eff_cr_tax_amount);
3082 
3083 
3084         ELSIF rec_c_get_refrec_for_upd.recovered_amount  +  ln_eff_cr_tax_amount  > rec_c_get_refrec_for_upd.recoverable_amount THEN
3085           /*
3086           || +ve cash receipt
3087           || Check that if recoverd amount + cash receipt amount > recoverable amount.
3088           || IF yes then set cash receipt amount = recoverable amount - recovered amount
3089           || so that the recovered amount never exceeds the recoverable amount
3090           */
3091           ln_eff_cr_tax_amount := rec_c_get_refrec_for_upd.recoverable_amount - rec_c_get_refrec_for_upd.recovered_amount ;
3092           fnd_file.put_line(fnd_file.LOG,'11 rec_c_get_refrec_for_upd.recovered_amount  +  ln_eff_cr_tax_amount  > rec_c_get_refrec_for_upd.recoverable_amount hence ln_eff_cr_tax_amount '||ln_eff_cr_tax_amount);
3093         END IF;
3094       ELSE --(rec_c_get_refrec_for_upd.resersal_flag = 'Y')
3095         /*Bug 11787045 - If Credit Memo application on a Receipt is reversed, then ln_eff_cr_tax_amount needs to be a positive tax amount
3096         Hence the below calculation must be done only in case of CM application on a Receipt or reversal of Invoice application*/
3097        -- IF ln_eff_cr_tax_amount < 0 THEN     Rolling back the changes done for  Bug 11787045 by mmurtuza
3098             ln_eff_cr_tax_amount := rec_c_get_refrec_for_upd.recoverable_amount - rec_c_get_refrec_for_upd.recovered_amount ;
3099             fnd_file.put_line(fnd_file.LOG,'10" Credit Memo entries : ln_eff_cr_tax_amount := rec_c_get_refrec_for_upd.recoverable_amount - rec_c_get_refrec_for_upd.recovered_amount ' || ln_eff_cr_tax_amount );
3100         --END IF;   Rolling back the changes done for  Bug 11787045 by mmurtuza
3101       END IF; --NVL(rec_c_get_refrec_for_upd.REVERSAL_FLAG,'N') = 'N'
3102 
3103       ---------------------------------------------------------------------------------
3104       --Added REVERSAL_FLAG LOGIC  by  Eric Ma for bug 10230041 on Oct-28-2010, End
3105 
3106       /*########################################################################################################
3107       || Calculation of the Service Tax Discount Component of Cash Receipt Discounted Amount which needs to be considered
3108       ########################################################################################################*/
3109 
3110       /*Bug 10148245 - Start*/
3111       IF (ln_apply_in_detail = 1) THEN
3112         ln_eff_cr_disc_amount := (ln_rcpt_tax_disc_amt/ln_inv_tot_tax_amt) * rec_c_get_refrec_for_upd.recoverable_amount ;
3113       ELSE
3114         ln_eff_cr_disc_amount := (ln_cr_tax_disc_amt/ln_inv_tot_tax_amt) * rec_c_get_refrec_for_upd.recoverable_amount ;
3115       END IF;
3116       /*Bug 10148245 - End*/
3117 
3118       fnd_file.put_line(fnd_file.LOG,'9 ln_cr_tax_disc_amt ->'||ln_cr_tax_disc_amt
3119                                      ||', ln_inv_tot_tax_amt ->'||ln_inv_tot_tax_amt
3120                                      ||', rec_c_get_refrec_for_upd.recoverable_amount ->'||rec_c_get_refrec_for_upd.recoverable_amount
3121                                      ||', cash receipt tax amount is ln_eff_cr_tax_amount ->'||ln_eff_cr_tax_amount
3122                                      ||', rec_c_get_refrec_for_upd.recovered_amount ->'||rec_c_get_refrec_for_upd.recovered_amount);
3123 
3124       --Added REVERSAL_FLAG LOGIC  by  Eric Ma for bug 10230041 on Oct-28-2010, Begin
3125       -------------------------------------------------------------------------------------
3126       IF NVL(rec_c_get_refrec_for_upd.REVERSAL_FLAG,'N') = 'N'
3127       THEN
3128         IF  nvl(rec_c_get_refrec_for_upd.recoverable_amount,0) - nvl(ln_eff_cr_disc_amount,0)  <
3129             nvl(rec_c_get_refrec_for_upd.recovered_amount,0)   + nvl(ln_eff_cr_tax_amount,0)
3130         THEN
3131             /*
3132         || +ve discounted amount
3133             || The effective recovered amount portion should never be greater than the effective recoverable_amount
3134         || Keeping this condition in mind , the discounted amount should get adjusted
3135             */
3136             ln_eff_cr_disc_amount := nvl(rec_c_get_refrec_for_upd.recoverable_amount,0) - (nvl(rec_c_get_refrec_for_upd.recovered_amount,0) + nvl(ln_eff_cr_tax_amount,0) );
3137 
3138           fnd_file.put_line(fnd_file.LOG,'10 effective recovered amount > effective recoverable amount hence, ln_eff_cr_disc_amount '||ln_eff_cr_disc_amount);
3139 
3140         ELSIF  rec_c_get_refrec_for_upd.discounted_amount +  ln_eff_cr_disc_amount < 0 THEN
3141         /*
3142         || -ve discounted amount
3143         || The total discounted amount cannot be lesser than 0
3144         */
3145           ln_eff_cr_disc_amount := rec_c_get_refrec_for_upd.discounted_amount * rec_c_get_rec_app.sign_of_cr_disc;
3146 
3147           fnd_file.put_line(fnd_file.LOG,'10 rec_c_get_refrec_for_upd.discounted_amount + ln_eff_cr_disc_amount < 0 , ln_eff_cr_tax_amount '||ln_eff_cr_tax_amount);
3148 
3149         END IF;
3150       END IF; --NVL(rec_c_get_refrec_for_upd.REVERSAL_FLAG,'N') = 'N'
3151 
3152       -------------------------------------------------------------------------------------
3153       --Added REVERSAL_FLAG LOGIC  by  Eric Ma for bug 10230041 on Oct-28-2010, End
3154 
3155       /*########################################################################################################
3156       || Insert the effective cash receipt tax amount into the repository
3157       ########################################################################################################*/
3158        /*
3159        || Make an entry into the repository with the apportioned Cash Receipt Tax amount
3160        */
3161 
3162        /*csahoo for bug#5879769...start*/
3163 
3164       ln_organization_id   := NULL;
3165       ln_location_id       := NULL;
3166       lv_service_type_code := NULL;
3167       lv_process_flag      := NULL;
3168       lv_process_message   := NULL;
3169 
3170       jai_trx_repo_extract_pkg.get_doc_from_reference(p_reference_id      => rec_c_get_refrec_for_upd.reference_id,
3171                                                       p_organization_id   => ln_organization_id,
3172                                                       p_location_id       => ln_location_id,
3173                                                       p_service_type_code => lv_service_type_code,
3174                                                       p_process_flag      => lv_process_flag,
3175                                                       p_process_message   => lv_process_message
3176                                                       );
3177 
3178        IF  lv_process_flag <> jai_constants.successful THEN
3179          FND_FILE.put_line(fnd_file.log, 'Error Flag:'||lv_process_flag||' Error Message:'||lv_process_message);
3180          return;
3181        END IF;
3182 
3183       /*csahoo for bug#5879769...end*/
3184 
3185 
3186        fnd_file.put_line(fnd_file.LOG,' 14 before call to jai_cmn_rgm_recording_pkg.insert_repository_entry ' );
3187        /*Bug 10623735
3188          If Invoice and Receipt currency are different, data in AR_RECEIVABLE_APPLICATIONS_ALL is populated
3189          in Invoice Currency. Hence the same cannot be passed as Transaction Amount in JAI_RGM_TRX_RECORDS
3190          Transaction Amount Data in JAI_RGM_TRX_RECORDS must be in Receipt Currency for Receipt Application Event.
3191          Hence multiplied by Invoice Exchange Rate if Invoice is in foreign currency and divided
3192          by Receipt Exchange Rate if Receipt is in foreign currency
3193        */
3194        IF (rec_c_get_rec_app.invoice_currency_code <> 'INR'
3195           and rec_c_get_rec_app.receipt_currency_code = 'INR') THEN
3196            ln_trx_amount := (ln_eff_cr_tax_amount * nvl(rec_c_get_rec_app.invoice_exchange_rate,1)) ;
3197        ELSIF (rec_c_get_rec_app.invoice_currency_code = 'INR'
3198               and rec_c_get_rec_app.receipt_currency_code <> 'INR') THEN
3199            ln_trx_amount := (ln_eff_cr_tax_amount / nvl(rec_c_get_rec_app.receipt_exchange_rate,1)) ;
3200        ELSE
3201            ln_trx_amount := ln_eff_cr_tax_amount;
3202        END IF;
3203        fnd_file.put_line(fnd_file.LOG,' rec_c_get_rec_app.invoice_currency_code ' || rec_c_get_rec_app.invoice_currency_code ||
3204                        ' rec_c_get_rec_app.receipt_currency_code ' || rec_c_get_rec_app.receipt_currency_code ||
3205                        ' ln_trx_amount ' || ln_trx_amount ||
3206                        ' ln_eff_cr_tax_amount ' || ln_eff_cr_tax_amount ||
3207                        ' rec_c_get_rec_app.invoice_exchange_rate ' || rec_c_get_rec_app.invoice_exchange_rate ||
3208                        ' rec_c_get_rec_app.receipt_exchange_rate ' || rec_c_get_rec_app.receipt_exchange_rate );
3209        ln_func_tax_amt := ln_eff_cr_tax_amount * nvl(rec_c_get_rec_app.invoice_exchange_rate,1); -- Added for Bug 7522584
3210        /*Bug 10623735 - Multiply by Exchange Rate only if Invoice is not in INR - End*/
3211 
3212      -- Added for Bug 8294236 - Start
3213       ln_total_tax_amt := ln_eff_cr_tax_amount + nvl(ln_eff_cr_disc_amount,0);
3214       IF (nvl(rec_c_get_rec_app.receipt_exchange_rate,1) <> nvl(rec_c_get_rec_app.invoice_exchange_rate,1)
3215          AND rec_c_get_rec_app.receipt_currency_code = rec_c_get_rec_app.invoice_currency_code)
3216       THEN
3217         ln_exc_gain_loss_amt := (ln_total_tax_amt * nvl(rec_c_get_rec_app.receipt_exchange_rate,1))
3218                                       - (ln_total_tax_amt * nvl(rec_c_get_rec_app.invoice_exchange_rate,1));
3219       ELSE
3220         ln_exc_gain_loss_amt := 0;
3221       END IF;
3222 
3223      -- Added for Bug 8294236 - End
3224 
3225        jai_cmn_rgm_recording_pkg.insert_repository_entry (
3226                                                           p_repository_id              => ln_repository_id                                    ,
3227                                                           p_regime_id                  => p_regime_id                                         ,
3228                                                           p_tax_type                   => rec_c_get_refrec_for_upd.tax_type                   ,
3229                                                           p_organization_type          => p_organization_type                                 ,
3230                                                           p_organization_id            => ln_organization_id                                  ,/*5879769*/
3231                                                           p_location_id                => ln_location_id                                      ,/*5879769*/
3232                                                           p_service_type_code          => lv_service_type_code                                ,/*5879769*/
3233                                                           p_source                     => p_source                                            ,
3234                                                           p_source_trx_type            => lv_source_trx_type                                  ,
3235                                                           p_source_table_name          => lv_source_table                    ,
3236                                                           p_source_document_id         => rec_c_get_rec_app.receivable_application_id         ,
3237                                                           p_transaction_date           => rec_c_get_rec_app.receipt_date                      ,
3238                                                           p_account_name               => NULL                                                ,
3239                                                           p_charge_account_id          => NULL                                                ,
3240                                                           p_balancing_account_id       => NULL                                                ,
3241                               -- Added ln_func_amount for Bug 7522584
3242                                                          p_amount                     => ln_func_tax_amt                                     ,
3243                                                           p_assessable_value           => NULL                                                ,
3244                                                           p_tax_rate                   => rec_c_get_refrec_for_upd.tax_rate                   ,
3245                                                           p_reference_id               => rec_c_get_refrec_for_upd.reference_id               ,
3246                                                           p_batch_id                   => p_batch_id                                          ,
3247                                                           p_called_from                => lv_called_from                                      ,
3248                                                           p_process_flag               => lv_process_flag                                     ,
3249                                                           p_process_message            => lv_process_message                                  ,
3250                                                           p_discounted_amount          => ln_eff_cr_disc_amount                               ,
3251                                                p_inv_organization_id        => ln_organization_id                                  ,/*5879769*/
3252                                                           p_accounting_date            => rec_c_get_rec_app.gl_date                           ,
3253                                                           p_currency_code              => rec_c_get_rec_app.receipt_currency_code             ,
3254                                                           p_curr_conv_date             => rec_c_get_rec_app.receipt_exchange_date             ,
3255                                                           p_curr_conv_type             => rec_c_get_rec_app.receipt_exchange_rate_type        ,
3256                                                           p_curr_conv_rate             => rec_c_get_rec_app.receipt_exchange_rate             ,
3257                                                           p_trx_amount                 => ln_trx_amount                               , /*Bug 10623735*/
3258                                                            --Bo Li for Bug9305067 change attribute_cotext to trx_reference_context
3259                                                            --attribute1 to trx_reference1 and attribute2 to trx_reference2
3260                                                           -----------------------------------------------------------------------------
3261                                                           p_trx_reference_context          => lv_attribute_context                                ,
3262                                                           p_trx_reference1                 => rec_c_get_rec_app.customer_trx_id                   ,
3263                                                           p_trx_reference2                 => rec_c_get_rec_app.cash_receipt_id
3264                                                           ----------------------------------------------------------------------------
3265                                                           , p_accntg_required_flag    => jai_constants.yes --File.Sql.35 Cbabu
3266                                                         );
3267 
3268       fnd_file.put_line(fnd_file.LOG,' 15 Returned from jai_cmn_rgm_recording_pkg.insert_repository_entry ' );
3269 
3270 
3271       IF lv_process_flag = jai_constants.expected_error    OR
3272          lv_process_flag = jai_constants.unexpected_error
3273       THEN
3274         /*
3275         || as Returned status is an error hence:-
3276         ||1. Rollback to save point
3277         ||2. Set out variables p_process_flag and p_process_message accordingly
3278         */
3279         ln_receivable_application_id :=  rec_c_get_rec_app.receivable_application_id;
3280         ROLLBACK to roll_to_last_receivable;
3281         p_process_flag    := lv_process_flag    ;
3282         p_process_message := lv_process_message ;
3283         fnd_file.put_line( fnd_file.log, '16 error in call to jai_cmn_rgm_recording_pkg.insert_repository_entry - lv_process_flag '||lv_process_flag
3284                                           ||', lv_process_message'||lv_process_message);
3285         EXIT;
3286       END IF;
3287 
3288     -- Added for Bug 8294236 - Start
3289      IF nvl(ln_exc_gain_loss_amt,0) <> 0 THEN
3290             jai_cmn_rgm_recording_pkg.exc_gain_loss_accounting(
3291                                     p_repository_id              => ln_repository_id                                  ,
3292                                     p_regime_id               =>  p_regime_id                                         ,
3293                                     p_tax_type                =>  rec_c_get_refrec_for_upd.tax_type                   ,
3294                                     p_organization_type       =>  p_organization_type                                 ,
3295                                     p_organization_id         =>  ln_organization_id                                  ,
3296                                     p_location_id             =>  ln_location_id                                      ,
3297                                     p_source                  =>  p_source                                            ,
3298                                     p_source_trx_type         =>  lv_source_trx_type                                  ,
3299                                     p_source_table_name       =>  'AR_RECEIVABLE_APPLICATIONS_ALL'                    ,
3300                                     p_source_document_id      =>  rec_c_get_rec_app.receivable_application_id         ,
3301                                     p_transaction_date        =>  rec_c_get_rec_app.receipt_date                      ,
3302                                     p_account_name            =>  NULL                                                ,
3303                                     p_charge_account_id       =>  NULL                                                ,
3304                                     p_balancing_account_id    =>  NULL                                                ,
3305                                     p_exc_gain_loss_amt       =>  ln_exc_gain_loss_amt                                ,
3306                                     p_reference_id            =>  rec_c_get_refrec_for_upd.reference_id               ,
3307                                     p_called_from             =>  'JAI_RGM_PROCESS_AR_TAXES.POPULATE_RECEIPT_RECORDS' ,
3308                                     p_process_flag            =>  lv_process_flag                                     ,
3309                                     p_process_message         =>  lv_process_message                                  ,
3310                                     p_accounting_date         =>  rec_c_get_rec_app.gl_date
3311                                   );
3312 
3313             IF lv_process_flag = jai_constants.expected_error    OR
3314                lv_process_flag = jai_constants.unexpected_error
3315             THEN
3316               ln_receivable_application_id :=  rec_c_get_rec_app.receivable_application_id;
3317               ROLLBACK to roll_to_last_receivable;
3318               p_process_flag    := lv_process_flag    ;
3319               p_process_message := lv_process_message ;
3320               fnd_file.put_line( fnd_file.log, '16.1 error in call to jai_rgm_trx_recording_pkg.exc_gain_loss_accounting - lv_process_flag '||lv_process_flag
3321                                                 ||', lv_process_message'||lv_process_message);
3322               EXIT;
3323             END IF;
3324      END IF;
3325      -- Added for Bug 8294236 - End
3326 
3327       /*########################################################################################################
3328       || update the effective cash receipt tax amount into the reference table
3329       ########################################################################################################*/
3330 
3331       fnd_file.put_line(fnd_file.LOG,' 12 before call to jai_cmn_rgm_recording_pkg.update_reference ' );
3332 
3333       savepoint before_ref_upd;
3334       jai_cmn_rgm_recording_pkg.update_reference (
3335                                                    p_source             => p_source                                ,
3336                                                    p_reference_id       => rec_c_get_refrec_for_upd.reference_id   ,
3337                                                    p_recovered_amount   => ln_eff_cr_tax_amount                    ,
3338                                                    p_discounted_amount  => ln_eff_cr_disc_amount                   ,
3339                                                    p_process_flag       => lv_process_flag                         ,
3340                                                    p_process_message    => lv_process_message
3341                                                  );
3342 
3343 
3344       IF lv_process_flag = jai_constants.expected_error    OR
3345          lv_process_flag = jai_constants.unexpected_error
3346       THEN
3347         /*
3348         || as Returned status is an error hence:-
3349         ||Set out variables p_process_flag and p_process_message accordingly
3350         */
3351         ln_receivable_application_id :=  rec_c_get_rec_app.receivable_application_id;
3352         ROLLBACK to roll_to_last_receivable;
3353         p_process_flag    := lv_process_flag    ;
3354         p_process_message := lv_process_message ;
3355         fnd_file.put_line( fnd_file.log, '12.1 error in call to jai_cmn_rgm_recording_pkg.update_reference - lv_process_flag '||lv_process_flag
3356                                           ||', lv_process_message'||lv_process_message);
3357 
3358         EXIT;
3359       END IF;
3360      END IF;--Add by Xiao for POT change, reg bug#12533434 on 16-May-2011
3361       fnd_file.put_line(fnd_file.LOG,' 13 Returned from jai_cmn_rgm_recording_pkg.update_reference ' );
3362 
3363     END LOOP;
3364 
3365     ln_uncommitted_transactions := ln_uncommitted_transactions  + 1;
3366     fnd_file.put_line(fnd_file.LOG,' 17 Finished processing the cash receipt ' );
3367 
3368     IF ln_uncommitted_transactions >= 50 THEN
3369       commit;
3370       ln_uncommitted_transactions := 0;
3371     END IF;
3372   END LOOP;
3373 
3374 EXCEPTION
3375   WHEN OTHERS THEN
3376     p_process_flag    := jai_constants.unexpected_error;
3377     p_process_message := 'Unexpected error occured while processing jai_ar_rgm_processing_pkg.populate_receipt_records'||SQLERRM ;
3378 
3379 END populate_receipt_records;
3380 
3381 /*Bug 11821537 - Start*/
3382 
3383 PROCEDURE populate_repository (p_regime_id          IN  JAI_RGM_DEFINITIONS.REGIME_ID%TYPE          ,
3384                                p_organization_type  IN  JAI_RGM_PARTIES.ORGANIZATION_TYPE%TYPE      ,
3385                                p_from_date          IN  DATE                                        ,
3386                                p_to_date            IN  DATE                                        ,
3387                                p_org_id             IN  RA_CUSTOMER_TRX_ALL.ORG_ID%TYPE             ,
3388                                p_source             IN  varchar2                                    ,
3389                                p_batch_id           IN  JAI_RGM_TRX_REFS.BATCH_ID%TYPE              ,
3390                                p_process_flag       OUT NOCOPY VARCHAR2                             ,
3391                                p_process_message    OUT NOCOPY VARCHAR2                             ,
3392                                p_organization_id    IN  JAI_RGM_PARTIES.ORGANIZATION_ID%TYPE  DEFAULT NULL)
3393 IS
3394 /*Variables*/
3395 lv_source_table                CONSTANT jai_rgm_trx_records.source_table_name%TYPE := 'CUSTOMER_TRX_LINE_ALL';
3396 lv_called_from                 CONSTANT varchar2(100) := 'jai_ar_rgm_processing_pkg.populate_repository';
3397 ln_repository_id               NUMBER;
3398 ln_discounted_amt              NUMBER := 0;
3399 
3400 /*Cursors*/
3401 /*CURSOR c_ar_transactions(cp_source_ar VARCHAR2)
3402 IS
3403 SELECT trx.customer_trx_id                      ,
3404        trx.trx_date
3405 
3406 FROM
3407     ra_customer_trx_all            trx           ,
3408     ra_cust_trx_types_all          trx_types     ,
3409     JAI_AR_TRXS                    jtrx
3410 WHERE
3411     trx_types.cust_trx_type_id  = trx.cust_trx_type_id                                                                       AND
3412     jtrx.organization_id        = p_organization_id                                                                          AND
3413     trx.org_id                  = nvl(p_org_id,trx.org_id)                                                                   AND
3414     upper(trx_types.type)       IN (jai_constants.ar_invoice_type_inv,
3415                                     jai_constants.ar_invoice_type_cm,
3416                                     jai_constants.ar_doc_type_dm)                                                            AND
3417     trx_types.org_id            = trx.org_id                                                                                 AND
3418     trx.customer_trx_id         = jtrx.customer_trx_id                                                                       AND
3419     trx.trx_date                BETWEEN trunc(p_from_date) and trunc(p_to_date)                                              AND
3420     NOT EXISTS                  (   SELECT  1
3421                                     FROM    jai_rgm_trx_records  rgtr
3422                                     WHERE
3423                                             rgtr.source               = cp_source_ar                                         AND
3424                                             rgtr.organization_id      = p_organization_id                                    AND
3425                                             rgtr.source_table_name    = lv_source_table                                      AND
3426                                             rgtr.source_document_id   = jtrx.customer_trx_id
3427                                 )                                                                                            AND
3428     EXISTS                      (   SELECT  1
3429                                     FROM    jai_rgm_trx_refs                rgtf
3430                                     WHERE
3431                                             rgtf.source                    = cp_source_ar                                    AND
3432                                             rgtf.invoice_id                = jtrx.customer_trx_id                            AND
3433                                             rgtf.party_type                = 'C'
3434                                 );*/--Commented by Zhiwei.hou for POT code port.
3435 
3436 --Added by zhiwei.hou for POT code port begin
3437 ---------------------------------------------------------
3438 CURSOR c_ar_transactions(cp_source_ar VARCHAR2)
3439 IS
3440 SELECT trx.customer_trx_id
3441      , trx.trx_date
3442      , gd.gl_date --Added by Qiong for bug13540741
3443      , jattl.customer_trx_line_id
3444      , jattl.link_to_cust_trx_line_id
3445      , jtrx.st_inv_number             --Added by Qiong for Advanced Receipts
3446   FROM jai_ar_trx_tax_lines      jattl
3447      , ra_customer_trx_lines_all trxl
3448      , ra_customer_trx_all       trx
3449      , ra_cust_trx_types_all     trx_types
3450      , jai_ar_trxs               jtrx
3451      , ra_cust_trx_line_gl_dist_all gd --Added by Qiong for bug13540741
3452  WHERE jattl.link_to_cust_trx_line_id = trxl.customer_trx_line_id
3453    AND trxl.customer_trx_id = trx.customer_trx_id
3454    /*14121914 - Start*/
3455    AND (
3456          (accounting_rule_id is not null
3457           AND
3458           (autorule_complete_flag IS NULL
3459           OR
3460           autorule_complete_flag = 'Y')
3461          )
3462          OR
3463          accounting_rule_id is null
3464        )
3465    /*14121914 - End*/
3466    AND trx_types.cust_trx_type_id  = trx.cust_trx_type_id
3467    AND jtrx.organization_id        = p_organization_id
3468    AND trx.org_id                  = nvl(p_org_id,trx.org_id)
3469    AND upper(trx_types.type) IN (jai_constants.ar_invoice_type_inv
3470                               , jai_constants.ar_invoice_type_cm
3471                               , jai_constants.ar_doc_type_dm
3472                               )
3473    AND trx_types.org_id            = trx.org_id
3474    AND trx.customer_trx_id         = jtrx.customer_trx_id
3475    --AND trx.trx_date BETWEEN trunc(p_from_date) and trunc(p_to_date) commented by qiong for bug13540741
3476    --Added by Qiong for bug13540741 begin
3477    ----------------------------------------------
3478    AND gd.gl_date BETWEEN trunc(p_from_date) and trunc(p_to_date)
3479    AND gd.customer_trx_id = trx.customer_trx_id
3480    AND gd.ACCOUNT_CLASS = 'REC'
3481    AND gd.LATEST_REC_FLAG = 'Y'
3482    ----------------------------------------------
3483    --Added by Qiong for bug13540741 end
3484 
3485    AND NOT EXISTS (SELECT 1
3486                     FROM jai_rgm_trx_records  rgtr
3487                    WHERE rgtr.source             = cp_source_ar
3488                      AND rgtr.organization_id    = p_organization_id
3489                      AND rgtr.source_table_name  = lv_source_table
3490                      AND rgtr.source_document_id = jattl.customer_trx_line_id)
3491    AND EXISTS (SELECT 1
3492                 FROM jai_rgm_trx_refs rgtf
3493                WHERE rgtf.source  = cp_source_ar
3494                  AND rgtf.line_id = jattl.customer_trx_line_id
3495                  AND rgtf.party_type = 'C');
3496 
3497 ---------------------------------------------------------
3498 --Added by zhiwei.hou for POT code port end
3499 
3500 
3501 CURSOR c_ar_ref_records(cp_source_document_id NUMBER, cp_source_ar VARCHAR2)
3502 IS
3503 SELECT *
3504 FROM   jai_rgm_trx_refs                 rgtf
3505 WHERE  line_id                       = cp_source_document_id                                 AND
3506        party_type                       = 'C'                                                   AND
3507        source                           = cp_source_ar;
3508 --Add by Xiao for POT Phase III changes, reg bug#12895841 on 1-Aug-2011, begin
3509 ----------------------------------------------------------------------------------------------
3510     CURSOR get_reversal_entries_cur (pn_invoice_id IN NUMBER, pn_line_number IN NUMBER) IS
3511     SELECT jcta.tax_type, jattl.customer_trx_line_id, trx.trx_date,
3512            jattl.tax_amount * nvl(jcta.mod_cr_percentage,0)/100 tax_amt
3513       FROM jai_ar_trx_tax_lines      jattl
3514          , ra_customer_trx_all       trx
3515          , jai_cmn_taxes_all         jcta
3516      WHERE trx.customer_trx_id = pn_invoice_id
3517        AND jattl.link_to_cust_trx_line_id = pn_line_number
3518        AND jattl.tax_id = jcta.tax_id
3519        AND nvl(jcta.mod_cr_percentage,0) > 0
3520        AND jcta.tax_type IN ('Service', 'SERVICE_EDUCATION_CESS', 'SERVICE_SH_EDU_CESS')
3521        AND NOT EXISTS (SELECT 1
3522                          FROM jai_rgm_trx_records
3523                         WHERE SOURCE = 'AR_REVERSAL'
3524                           AND source_table_name = 'CUSTOMER_TRX_LINE_ALL'
3525                           AND source_document_id = jattl.customer_trx_line_id);
3526 
3527      CURSOR get_reversal_amount_cur(pn_invoice_id IN NUMBER, pn_line_number IN NUMBER) IS
3528      SELECT reversal_amount, date_of_reversal
3529        FROM jai_st_invoice_reversal
3530       WHERE invoice_id = pn_invoice_id
3531         AND customer_trx_line_id = pn_line_number
3532         AND SOURCE = 'AR';
3533 
3534     CURSOR get_total_rec_amt_cur(pn_invoice_id IN NUMBER) IS
3535     SELECT SUM(tax_amount)
3536       FROM jai_rgm_trx_refs jrtr
3537      WHERE jrtr.invoice_id = pn_invoice_id
3538        AND jrtr.source = 'AR'
3539        AND jrtr.tax_type IN (jai_constants.tax_type_service,
3540                              jai_constants.tax_type_service_edu_cess,
3541                              jai_constants.tax_type_sh_service_edu_cess);
3542 
3543     CURSOR get_total_amt_cur (pn_invoice_id IN NUMBER) IS
3544     SELECT SUM(total_amount)
3545       FROM jai_ar_trx_lines
3546      WHERE customer_trx_id = pn_invoice_id;
3547 
3548     CURSOR get_claim_entries_cur(pn_invoice_id IN NUMBER, pn_line_number IN NUMBER) IS
3549     SELECT jcta.tax_type, (jattl.tax_amount * nvl(jcta.mod_cr_percentage,0)/100) tax_amt,
3550            jattl.customer_trx_line_id, trx.trx_date
3551       FROM jai_ar_trx_tax_lines      jattl
3552          , ra_customer_trx_all       trx
3553          , jai_cmn_taxes_all         jcta
3554      WHERE trx.customer_trx_id = pn_invoice_id
3555        AND jattl.link_to_cust_trx_line_id = pn_line_number
3556        AND jattl.tax_id = jcta.tax_id
3557        AND nvl(jcta.mod_cr_percentage,0) > 0
3558        AND jcta.tax_type IN ('Service', 'SERVICE_EDUCATION_CESS', 'SERVICE_SH_EDU_CESS')
3559        AND EXISTS (SELECT 1
3560                          FROM jai_rgm_trx_records
3561                         WHERE SOURCE = 'AR_REVERSAL'
3562                           AND source_table_name = 'CUSTOMER_TRX_LINE_ALL'
3563                           AND source_document_id = jattl.customer_trx_line_id);
3564 
3565   CURSOR get_claim_trxn_cur IS
3566   SELECT jsir.*
3567     FROM jai_st_invoice_reversal  jsir
3568    WHERE jsir.source = 'AR'
3569      AND jsir.tax_to_be_adjusted > 0
3570    ORDER BY jsir.invoice_id;
3571 
3572   CURSOR get_reversal_trxn_cur IS
3573   SELECT jsir.*
3574     FROM jai_st_invoice_reversal  jsir
3575    WHERE jsir.source = 'AR'
3576      AND jsir.date_of_reversal BETWEEN p_from_date AND p_to_date;
3577 
3578   CURSOR get_claim_amt_cur(pn_invoice_id IN NUMBER, pn_line_number IN NUMBER, pn_max_payment_id IN NUMBER) IS
3579   SELECT nvl(amount_applied, 0) claim_amt, receivable_application_id, gl_date
3580     FROM ar_receivable_applications_all araa
3581    WHERE araa.applied_customer_trx_id = pn_invoice_id
3582      AND araa.receivable_application_id > nvl(pn_max_payment_id, 0)
3583      AND trunc(araa.apply_date) <= p_to_date
3584      AND araa.status = 'APP'
3585      AND NOT EXISTS (SELECT 1
3586                        FROM jai_rgm_trx_records jrtr
3587                       WHERE jrtr.source_table_name = 'AR_RECEIVABLE_APPLICATIONS_ALL'
3588                         AND jrtr.source_trx_type = 'CLAIM_ACCOUNTING'
3589                         AND jrtr.source = 'AR_CLAIM'
3590                         AND jrtr.source_document_id = araa.receivable_application_id)
3591      AND EXISTS (SELECT 1
3592                    FROM ra_cust_trx_line_gl_dist_all rg
3593                   WHERE rg.customer_trx_id = pn_invoice_id
3594                     AND rg.customer_trx_line_id = pn_line_number
3595                     AND rg.account_class = 'REV'
3596                     AND jai_st_reversal_extract_pkg.is_paid_after_reversal(araa.apply_date,
3597                            rg.gl_date, 'AR') = 'Y');
3598 
3599 /* Added by Wenqiong for Advanced Receipts on Service Taxes Begin*/
3600 CURSOR c_ar_receipts
3601 IS
3602 SELECT jcra.cash_receipt_id
3603      , acra.receipt_date
3604      , jcra.organization_id
3605      , jcra.location_id
3606      , acra.receipt_number
3607      , jcra.gl_date
3608      , jcra.service_type_code
3609      , jcra.exchange_rate
3610      , jcra.exchange_rate_type
3611   FROM
3612        jai_ar_cash_receipts_all  jcra
3613      , ar_cash_receipts_all      acra
3614  WHERE jcra.cash_receipt_id = acra.cash_receipt_id
3615    AND jcra.confirm_flag = 'Y'
3616    AND jcra.document_type = 'Service'
3617    --AND acra.reversal_date IS NULL
3618    AND jcra.organization_id        = p_organization_id
3619    AND jcra.org_id                  = nvl(p_org_id,jcra.org_id)
3620    AND jcra.gl_date BETWEEN trunc(p_from_date) and trunc(p_to_date)
3621    AND NOT EXISTS (SELECT 1
3622                     FROM jai_rgm_trx_records  rgtr
3623                    WHERE rgtr.source             = 'AR'
3624                      AND rgtr.organization_id    = p_organization_id
3625                      AND rgtr.source_table_name  = 'AR_CASH_RECEIPTS_ALL'
3626                      AND rgtr.source_trx_type = jai_constants.trx_type_adv_rcpts
3627                      AND rgtr.source_document_id = jcra.cash_receipt_id)
3628    AND EXISTS (SELECT 1
3629                 FROM jai_rgm_trx_refs rgtf
3630                WHERE rgtf.source  = 'AR'
3631                  AND rgtf.invoice_id = jcra.cash_receipt_id
3632                  AND rgtf.party_type = 'C');
3633 
3634     CURSOR get_receipt_entries_cur (cn_cash_receipt_id IN NUMBER) IS
3635     SELECT jdt.doc_tax_id,
3636            jdt.tax_type,
3637            jdt.tax_amt,
3638            jdt.func_tax_amt
3639       FROM jai_cmn_document_taxes      jdt
3640          , jai_ar_cash_receipts_all    jacr
3641          , jai_cmn_taxes_all         jcta
3642          , jai_regime_tax_types_v     jrttv
3643      WHERE jacr.cash_receipt_id = cn_cash_receipt_id
3644        AND jdt.source_doc_id = jacr.cash_receipt_id
3645        AND jdt.tax_id = jcta.tax_id
3646        AND jcta.tax_type = jrttv.tax_type
3647        AND jrttv.regime_code = jai_constants.service_regime
3648        AND jdt.source_doc_type  = JAI_CONSTANTS.ar_cash ;
3649 
3650 CURSOR c_rcpt_ref_records(cp_source_document_id NUMBER, cp_source_doc_line_id NUMBER, cp_source_ar VARCHAR2)
3651 IS
3652 SELECT *
3653 FROM   jai_rgm_trx_refs rgtf
3654 WHERE  line_id          = cp_source_doc_line_id   AND
3655        invoice_id       = cp_source_document_id   AND
3656        party_type       = 'C'                     AND
3657        source           = cp_source_ar;
3658 
3659 CURSOR c_ar_receipts_reversal
3660 IS
3661 SELECT jcra.cash_receipt_id
3662      , acra.reversal_date
3663      , jcra.organization_id
3664      , jcra.location_id
3665      , acra.receipt_number
3666      , acrh.gl_date     --Modified by Qiong for bug13555753 change from jcra.gl_date to acrh.gl_date
3667      , jcra.service_type_code
3668      , jcra.exchange_rate
3669      , jcra.exchange_rate_type
3670   FROM
3671        jai_ar_cash_receipts_all			jcra
3672      , ar_cash_receipts_all			acra
3673      , ar_cash_receipt_history_all              acrh  --Added by Qiong for bug13555753
3674  WHERE jcra.cash_receipt_id = acra.cash_receipt_id
3675    AND jcra.confirm_flag = 'Y'
3676    AND jcra.document_type = 'Service'
3677    AND acra.reversal_date IS NOT NULL
3678    AND jcra.organization_id        = p_organization_id
3679    AND jcra.org_id                  = nvl(p_org_id,jcra.org_id)
3680    --AND jcra.gl_date BETWEEN trunc(p_from_date) and trunc(p_to_date) commented by qiong for bug13555753
3681    --Added by qiong for bug13555753 begin
3682    ---------------------------------------
3683    AND acrh.cash_receipt_id = acra.cash_receipt_id
3684    AND acrh.status          = 'REVERSED'
3685    AND acrh.gl_date BETWEEN trunc(p_from_date) and trunc(p_to_date)
3686    ---------------------------------------
3687    --Added by qiong for bug13555753 end
3688    AND NOT EXISTS (SELECT 1
3689                     FROM jai_rgm_trx_records  rgtr
3690                    WHERE rgtr.source             = 'AR'
3691                      AND rgtr.organization_id    = p_organization_id
3692                      AND rgtr.source_table_name  = 'AR_CASH_RECEIPTS_ALL'
3693                      AND rgtr.source_trx_type =  jai_constants.trx_type_adv_rvs
3694                      --Modified jai_constants.trx_type_rct_rvs to jai_constants.trx_type_adv_rvs
3695                      --by Qinglei on 24-Dec-2012 for bug#13741544
3696                      AND rgtr.source_document_id = jcra.cash_receipt_id)
3697    AND EXISTS (SELECT 1
3698                 FROM jai_rgm_trx_refs rgtf
3699                WHERE rgtf.source  = 'AR'
3700                  AND rgtf.invoice_id = jcra.cash_receipt_id
3701                  AND rgtf.party_type = 'C');
3702 
3703 CURSOR c_receipt_application
3704 IS
3705 SELECT araa.receivable_application_id
3706      , jcra.cash_receipt_id
3707      , trx.customer_trx_id
3708      , araa.apply_date
3709      , araa.gl_date
3710      , jcra.organization_id
3711      , jcra.location_id                  location_id
3712      , acra.receipt_number
3713      , jcra.service_type_code
3714      , acra.receipt_date
3715      , jcra.currency_code                receipt_currency_code
3716      , jcra.exchange_date                receipt_exchange_date
3717      , jcra.exchange_rate                receipt_exchange_rate
3718      , jcra.exchange_rate_type           receipt_exchange_rate_type
3719      , trx.invoice_currency_code         invoice_currency_code
3720      , trx.exchange_rate                 invoice_exchange_rate
3721      , trx.exchange_date                 invoice_exchange_date
3722      , trx.exchange_rate_type            invoice_exchange_rate_type
3723      , acra.amount                       amount
3724      , araa.amount_applied               amount_applied
3725      , araa.acctd_amount_applied_from          amount_applied_from
3726      --Modified by Qinglei on 05-Jan-2012 for bug#13556198
3727      , to_number(decode(sign(araa.applied_payment_schedule_id),
3728                   -1,
3729                   null,
3730                   nvl(araa.earned_discount_taken, 0) +
3731                   nvl(araa.unearned_discount_taken, 0))) discount_amount
3732       , acra.set_of_books_id
3733       --Added by Qinglei on 05-Jan-2012 for bug#13556198
3734   FROM
3735        ar_receivable_applications_all araa
3736      , ar_cash_receipts_all      acra
3737      , jai_ar_cash_receipts_all  jcra
3738      , ra_customer_trx_all       trx
3739  WHERE araa.applied_customer_trx_id = trx.customer_trx_id
3740    AND araa.cash_receipt_id = acra.cash_receipt_id
3741    AND jcra.cash_receipt_id = acra.cash_receipt_id
3742    AND trx.complete_flag            = 'Y'
3743    AND upper(araa.application_type) = upper(jai_constants.ar_cash)
3744    AND upper(araa.status)           = upper(jai_constants.ar_status_app)
3745    AND jcra.confirm_flag = 'Y'
3746    AND sign (nvl(acra.amount,0)) = sign(nvl(araa.amount_applied,0))-- same means apply, otherwise means unapply
3747    AND jcra.document_type = 'Service'
3748    --AND acra.reversal_date IS NULL
3749    AND jcra.organization_id        = p_organization_id
3750    AND jcra.org_id                  = nvl(p_org_id,jcra.org_id)
3751    AND araa.gl_date BETWEEN trunc(p_from_date) and trunc(p_to_date)--Need confirm use creation date/apply date
3752    AND NOT EXISTS (SELECT 1
3753                     FROM jai_rgm_trx_records  rgtr
3754                    WHERE rgtr.source             = 'AR'
3755                      AND rgtr.organization_id    = p_organization_id
3756                      AND rgtr.source_table_name  = 'AR_RECEIVABLE_APPLICATIONS_ALL'
3757                      AND rgtr.source_trx_type =  jai_constants.trx_type_adv_app
3758                      AND rgtr.source_document_id = araa.receivable_application_id)
3759    AND EXISTS (SELECT 1
3760                 FROM jai_rgm_trx_refs rgtf
3761                WHERE rgtf.source  = 'AR'
3762                  AND rgtf.invoice_id = jcra.cash_receipt_id
3763                  AND rgtf.party_type = 'C');
3764 
3765 CURSOR c_receipt_unapplication
3766 IS
3767 SELECT araa.receivable_application_id
3768      , jcra.cash_receipt_id
3769      , trx.customer_trx_id
3770      , araa.apply_date
3771      , araa.gl_date
3772      , jcra.organization_id
3773      , jcra.location_id
3774      , acra.receipt_number
3775      , jcra.service_type_code
3776      , acra.receipt_date
3777      , jcra.currency_code                receipt_currency_code
3778      , jcra.exchange_date                receipt_exchange_date
3779      , jcra.exchange_rate                receipt_exchange_rate
3780      , jcra.exchange_rate_type           receipt_exchange_rate_type
3781      , trx.invoice_currency_code         invoice_currency_code
3782      , trx.exchange_rate                 invoice_exchange_rate
3783      , trx.exchange_date                 invoice_exchange_date
3784      , trx.exchange_rate_type            invoice_exchange_rate_type
3785      , acra.amount                       amount
3786      , araa.amount_applied               amount_applied
3787      , araa.acctd_amount_applied_from          amount_applied_from
3788      --Modified by Qinglei on 05-Jan-2012 for bug#13556198
3789      , to_number(decode(sign(araa.applied_payment_schedule_id),
3790                   -1,
3791                   null,
3792                   nvl(araa.earned_discount_taken, 0) +
3793                   nvl(araa.unearned_discount_taken, 0))) discount_amount
3794      , acra.set_of_books_id
3795      --Added by Qinglei on 05-Jan-2012 for bug#13556198
3796   FROM
3797        jai_ar_cash_receipts_all  jcra
3798      , ar_cash_receipts_all      acra
3799      , ar_receivable_applications_all araa
3800      , ra_customer_trx_all       trx
3801  WHERE araa.applied_customer_trx_id = trx.customer_trx_id
3802    AND araa.cash_receipt_id = acra.cash_receipt_id
3803    AND jcra.cash_receipt_id = acra.cash_receipt_id
3804    AND trx.complete_flag            = 'Y'
3805    AND upper(araa.application_type) = upper(jai_constants.ar_cash)
3806    --AND upper(araa.status)           = upper(jai_constants.ar_status_unapp)
3807    AND jcra.confirm_flag = 'Y'
3808    AND sign (nvl(acra.amount,0)) <> sign(nvl(araa.amount_applied,0))-- same means apply, otherwise means unapply
3809    AND jcra.document_type = 'Service'
3810    --AND acra.reversal_date IS NULL
3811    AND jcra.organization_id        = p_organization_id
3812    AND jcra.org_id                  = nvl(p_org_id,jcra.org_id)
3813    AND araa.gl_date BETWEEN trunc(p_from_date) and trunc(p_to_date)
3814    AND NOT EXISTS (SELECT 1
3815                     FROM jai_rgm_trx_records  rgtr
3816                    WHERE rgtr.source             = 'AR'
3817                      AND rgtr.organization_id    = p_organization_id
3818                      AND rgtr.source_table_name  = 'AR_RECEIVABLE_APPLICATIONS_ALL'
3819                      AND rgtr.source_trx_type =  jai_constants.trx_type_adv_unapp
3820                      AND rgtr.source_document_id = araa.receivable_application_id )
3821    AND EXISTS (SELECT 1
3822                 FROM jai_rgm_trx_refs rgtf
3823                WHERE rgtf.source  = 'AR'
3824                  AND rgtf.invoice_id = jcra.cash_receipt_id
3825                  AND rgtf.party_type = 'C');
3826 ln_receipt_amount              NUMBER;
3827 ld_receipt_date                DATE;
3828 
3829 /* Added by Wenqiong for Advanced Receipts on Service Taxes End*/
3830 
3831     lv_accounting_event         VARCHAR2(30);
3832     ln_reversal_amount          NUMBER;
3833     ln_total_rec_amt            NUMBER;
3834     ld_reversal_date            DATE;
3835     ln_tax_amount               NUMBER;
3836     ln_trx_tax_amount           NUMBER;--Added by Wenqiong for Advanced Receipts on Service Taxes
3837     ld_gl_date                  DATE;--Added by Qiong for Advanced Receipts
3838     ld_accounting_date          DATE;
3839     ln_claim_amount             NUMBER;
3840     ln_total_amount             NUMBER;
3841 
3842     lr_ar_ref_records           jai_rgm_trx_refs%ROWTYPE;
3843 
3844 ----------------------------------------------------------------------------------------------
3845 --Add by Xiao for POT Phase III changes, reg bug#12895841 on 1-Aug-2011, end
3846 
3847 --Added by zhiwei for POT 12925963 on 20110908 begin
3848 ----------------------------------------------------------------------------------------------
3849  cursor c_get_params(cp_reference_id number)
3850  is
3851  select
3852            trx.organization_id,
3853            trx.location_id,
3854            line.service_type_code
3855       from jai_rgm_trx_refs refs,
3856            jai_ar_trxs trx,
3857            jai_ar_trx_tax_lines tax,
3858            jai_ar_trx_lines line
3859       where refs.reference_id =  cp_reference_id--r_ar_ref_records.reference_id
3860       and   refs.line_id = tax.customer_trx_line_id
3861       and   refs.invoice_id = trx.customer_trx_id
3862       and   tax.link_to_cust_trx_line_id =   line.customer_trx_line_id
3863       and   line.customer_trx_id = trx.customer_trx_id;
3864 ----------------------------------------------------------------------------------------------
3865 --Added by zhiwei for POT 12925963 on 20110908 end
3866 
3867 ln_converted_rate NUMBER;
3868 --Added by Qinglei on 05-Jan-2012 for bug#13556198
3869 
3870 BEGIN
3871 
3872     FOR r_ar_transactions IN c_ar_transactions(cp_source_ar => p_source)
3873     LOOP
3874 
3875         --FOR r_ar_ref_records IN c_ar_ref_records(cp_source_document_id => r_ar_transactions.customer_trx_id, cp_source_ar => p_source)--Commented by zhiwei.hou for POT code port
3876         --Added by zhiwei.hou for POT code port begin
3877         -------------------------------------------------------
3878         FOR r_ar_ref_records IN c_ar_ref_records(cp_source_document_id => r_ar_transactions.customer_trx_line_id--Modified by Xiao for POT change, reg bug#12533434 on 30-May-11
3879         , cp_source_ar => p_source)
3880         -------------------------------------------------------
3881         --Added by zhiwei.hou for POT code port end
3882         LOOP
3883       --Add by Xiao for POT change, reg bug#12533434 on 16-May-2011, begin
3884       ----------------------------------------------------------------------------------------------
3885          IF is_accrual_basis(r_ar_ref_records.line_id) THEN
3886       ----------------------------------------------------------------------------------------------
3887       --Add by Xiao for POT change, reg bug#12533434 on 16-May-2011, end
3888       --Commented by zhiwei for POT 12925963 on 20110908 begin
3889             ----------------------------------------------------------------------------------------------
3890        /*
3891             jai_trx_repo_extract_pkg.get_doc_from_reference(p_reference_id      => r_ar_ref_records.reference_id,
3892                                                             p_organization_id   => ln_organization_id,
3893                                                             p_location_id       => ln_location_id,
3894                                                             p_service_type_code => lv_service_type_code,
3895                                                             p_process_flag      => lv_process_flag,
3896                                                             p_process_message   => lv_process_message
3897                                                             );
3898 
3899             IF  lv_process_flag <> jai_constants.successful THEN
3900                 FND_FILE.put_line(fnd_file.log, 'Error Flag:'||lv_process_flag||' Error Message:'||lv_process_message);
3901                 return;
3902             END IF; */
3903             ----------------------------------------------------------------------------------------------
3904             --Commented by zhiwei for POT 12925963 on 20110908 end
3905 
3906             --Added by zhiwei for POT 12925963 on 20110908 begin
3907             ----------------------------------------------------------------------------------------------
3908              open c_get_params(r_ar_ref_records.reference_id);
3909              fetch c_get_params into ln_organization_id,ln_location_id,lv_service_type_code;
3910              close c_get_params;
3911             ----------------------------------------------------------------------------------------------
3912             --Added by zhiwei for POT 12925963 on 20110908 end
3913 
3914 
3915 
3916             /*Check on TRX_REFERENCE_CONTEXT and TRX_REFERENCE1..5 fields*/
3917             jai_cmn_rgm_recording_pkg.insert_repository_entry (
3918                                                                 p_repository_id              => ln_repository_id                                                                                  ,
3919                                                                 p_regime_id                  => p_regime_id                                                                                       ,
3920                                                                 p_tax_type                   => r_ar_ref_records.tax_type                                                                         ,
3921                                                                 p_organization_type          => p_organization_type                                                                               ,
3922                                                                 p_organization_id            => ln_organization_id                                                                                ,
3923                                                                 p_location_id                => ln_location_id                                                                                    ,
3924                                                                 p_service_type_code          => lv_service_type_code                                                                              ,
3925                                                                 p_source                     => p_source                                                                                          ,
3926                                                                 p_source_trx_type            => jai_constants.trx_event_inv_save                                                                  ,
3927                                                                 p_source_table_name          => UPPER(jai_constants.tname_cus_trx_lines)                                                          ,
3928                                                                 --p_source_document_id         => r_ar_ref_records.invoice_id --Commented by zhiwei.hou for POT code port
3929                                                                 p_source_document_id         => r_ar_ref_records.line_id,  --Modified by Xiao for POT bug#12533434                                ,
3930                                                                 p_transaction_date           => r_ar_transactions.gl_date, --Changed by Qiong for bug13540741                                                                        ,
3931                                                                 p_account_name               => NULL                                                                                              ,
3932                                                                 p_charge_account_id          => NULL                                                                                              ,
3933                                                                 p_balancing_account_id       => NULL                                                                                              ,
3934                                                                 p_amount                     => r_ar_ref_records.tax_amount                                                                       ,
3935                                                                 p_assessable_value           => NULL                                                                                              ,
3936                                                                 p_tax_rate                   => r_ar_ref_records.tax_rate                                                                         ,
3937                                                                 p_reference_id               => r_ar_ref_records.reference_id                                                                     ,
3938                                                                 p_batch_id                   => p_batch_id                                                                                        ,
3939                                                                 p_called_from                => lv_called_from                                                                                    ,
3940                                                                 p_process_flag               => lv_process_flag                                                                                   ,
3941                                                                 p_process_message            => lv_process_message                                                                                ,
3942                                                                 p_discounted_amount          => ln_discounted_amt                                                                                                 ,
3943                                                                 p_inv_organization_id        => ln_organization_id                                                                                ,
3944                                                                 p_accounting_date            => r_ar_transactions.gl_date, --Changed from sysdate to gl_date by Qiong for bug13540741
3945                                                                 p_currency_code              => r_ar_ref_records.trx_currency                                                                     ,
3946                                                                 p_curr_conv_date             => r_ar_ref_records.curr_conv_date                                                                   ,
3947                                                                 p_curr_conv_type             => NULL                                                                                              ,
3948                                                                 p_curr_conv_rate             => r_ar_ref_records.curr_conv_rate                                                                   ,
3949                                                                 p_trx_amount                 => r_ar_ref_records.trx_tax_amount                                                                   ,
3950                                                                 p_accntg_required_flag       => jai_constants.no,
3951                                                                 p_accrual_basis             => 'Y' ,--Add by Xiao for pot change, reg bug#12533434
3952                                                                 p_invoice_no                => r_ar_transactions.st_inv_number --Added by Qiong for Advanced Receipts
3953                                                               );
3954             IF lv_process_flag = jai_constants.expected_error    OR
3955                lv_process_flag = jai_constants.unexpected_error THEN
3956                fnd_file.put_line( fnd_file.log, '5.1 ERROR IN CALL TO jai_ar_rgm_processing_pkg.populate_repository - lv_process_flag '||lv_process_flag
3957                                       ||', lv_process_message'||lv_process_message);
3958                p_process_flag    := lv_process_flag    ;
3959                p_process_message := lv_process_message ;
3960             END IF;
3961       --start additions by mmurtuza for bug#12777500
3962              jai_cmn_rgm_recording_pkg.update_reference (
3963                                                      p_source             => p_source                                        ,
3964                                                      p_reference_id       => r_ar_ref_records.reference_id                 ,
3965                                                      p_recovered_amount   => r_ar_ref_records.trx_tax_amount    ,
3966                                                      p_process_flag       => lv_process_flag                                 ,
3967                                                      p_process_message    => lv_process_message
3968                                                    );
3969                 IF lv_process_flag = jai_constants.expected_error    OR
3970                    lv_process_flag = jai_constants.unexpected_error THEN
3971                    fnd_file.put_line( fnd_file.log, '6.1 ERROR IN CALL TO jai_ar_rgm_processing_pkg.populate_repository - lv_process_flag '||lv_process_flag
3972                                           ||', lv_process_message'||lv_process_message);
3973                    p_process_flag    := lv_process_flag    ;
3974                    p_process_message := lv_process_message ;
3975                 END IF;
3976        --end additions by mmurtuza for bug#12777500
3977           END IF; --Add by Xiao for POT change, reg bug#12533434 on 16-May-2011
3978         END LOOP;
3979     END LOOP;
3980 --Add by Xiao for POT Phase III changes, reg bug#12895841 on 1-Aug-2011, begin
3981 ----------------------------------------------------------------------------------------------
3982     FOR reversal_trxn_rec IN get_reversal_trxn_cur
3983     LOOP
3984 
3985        ln_organization_id := reversal_trxn_rec.organization_id;
3986        ln_location_id     := reversal_trxn_rec.location_id;
3987        ln_reversal_amount := reversal_trxn_rec.reversal_amount;
3988        ld_accounting_date := reversal_trxn_rec.date_of_reversal;
3989 
3990        OPEN get_total_rec_amt_cur(reversal_trxn_rec.invoice_id);
3991        FETCH get_total_rec_amt_cur INTO ln_total_rec_amt;
3992        CLOSE get_total_rec_amt_cur;
3993 
3994        FOR rec_reversal_entries IN get_reversal_entries_cur(reversal_trxn_rec.invoice_id, reversal_trxn_rec.customer_trx_line_id)
3995        LOOP
3996            OPEN c_ar_ref_records(rec_reversal_entries.customer_trx_line_id, 'AR');
3997            FETCH c_ar_ref_records INTO lr_ar_ref_records;
3998            CLOSE c_ar_ref_records;
3999 
4000            jai_trx_repo_extract_pkg.get_doc_from_reference(p_reference_id      => lr_ar_ref_records.reference_id,
4001                                                           p_organization_id   => ln_organization_id,
4002                                                           p_location_id       => ln_location_id,
4003                                                           p_service_type_code => lv_service_type_code,
4004                                                           p_process_flag      => lv_process_flag,
4005                                                           p_process_message   => lv_process_message);
4006 
4007           IF lv_process_flag <> jai_constants.successful THEN
4008              FND_FILE.put_line(fnd_file.log, 'Error Flag:'||lv_process_flag||' Error Message:'||lv_process_message);
4009              RETURN;
4010           END IF;
4011 
4012           ln_tax_amount := round(ln_reversal_amount * rec_reversal_entries.tax_amt/ln_total_rec_amt,
4013                                  jai_constants.service_rgm_rnd_factor);
4014 
4015           IF nvl(ln_tax_amount, 0) > 0 THEN
4016              jai_cmn_rgm_recording_pkg.insert_repository_entry(
4017                              p_repository_id              => ln_repository_id,
4018                              p_regime_id                  => p_regime_id,
4019                              p_tax_type                   => rec_reversal_entries.tax_type,
4020                              p_organization_type          => p_organization_type,
4021                              p_organization_id            => ln_organization_id,
4022                              p_location_id                => ln_location_id,
4023                              p_service_type_code          => lv_service_type_code,
4024                              p_source                     => 'AR_REVERSAL',
4025                              p_source_trx_type            => 'REVERSAL_ACCOUNTING',
4026                              p_source_table_name          => 'CUSTOMER_TRX_LINE_ALL',
4027                              p_source_document_id         => lr_ar_ref_records.line_id,
4028                              p_transaction_date           => ld_accounting_date,
4029                              p_account_name               => NULL,
4030                              p_charge_account_id          => NULL,
4031                              p_balancing_account_id       => NULL,
4032                              p_amount                     => ln_tax_amount,
4033                              p_assessable_value           => NULL,
4034                              p_tax_rate                   => lr_ar_ref_records.tax_rate,
4035                              p_reference_id               => lr_ar_ref_records.reference_id,
4036                              p_batch_id                   => p_batch_id,
4037                              p_called_from                => lv_called_from,
4038                              p_process_flag               => lv_process_flag,
4039                              p_process_message            => lv_process_message,
4040                              p_discounted_amount          => ln_discounted_amt,
4041                              p_inv_organization_id        => ln_organization_id,
4042                              p_accounting_date            => ld_accounting_date,
4043                              p_currency_code              => lr_ar_ref_records.trx_currency,
4044                              p_curr_conv_date             => lr_ar_ref_records.curr_conv_date,
4045                              p_curr_conv_type             => NULL,
4046                              p_curr_conv_rate             => lr_ar_ref_records.curr_conv_rate,
4047                              p_trx_amount                 => ln_tax_amount,--lr_ar_ref_records.trx_tax_amount,
4048                              p_accntg_required_flag       => jai_constants.no,
4049                              p_accrual_basis              => 'Y');
4050 
4051               IF lv_process_flag = jai_constants.expected_error OR
4052                 lv_process_flag = jai_constants.unexpected_error THEN
4053                 fnd_file.put_line( fnd_file.log,
4054                   '5.1 ERROR IN CALL TO jai_cmn_rgm_processing_pkg.populate_repository - lv_process_flag '||lv_process_flag
4055                   ||', lv_process_message'||lv_process_message);
4056                 p_process_flag    := lv_process_flag;
4057                 p_process_message := lv_process_message;
4058               END IF;
4059            END IF;
4060 
4061        END LOOP;
4062     END LOOP;
4063 
4064 
4065     FOR reversal_trxn_rec IN get_claim_trxn_cur
4066     LOOP
4067 
4068        ln_organization_id := reversal_trxn_rec.organization_id;
4069        ln_location_id     := reversal_trxn_rec.location_id;
4070 
4071        OPEN get_total_amt_cur(reversal_trxn_rec.invoice_id);
4072        FETCH get_total_amt_cur INTO ln_total_amount;
4073        CLOSE get_total_amt_cur;
4074 
4075        FOR rec_claim_line IN get_claim_amt_cur(reversal_trxn_rec.invoice_id, reversal_trxn_rec.customer_trx_line_id,
4076                                                reversal_trxn_rec.max_claim_payment_id)
4077        LOOP
4078 
4079            FOR rec_claim_entries IN get_claim_entries_cur(reversal_trxn_rec.invoice_id, reversal_trxn_rec.customer_trx_line_id)
4080            LOOP
4081               OPEN c_ar_ref_records(rec_claim_entries.customer_trx_line_id, 'AR');
4082               FETCH c_ar_ref_records INTO lr_ar_ref_records;
4083               CLOSE c_ar_ref_records;
4084 
4085               jai_trx_repo_extract_pkg.get_doc_from_reference(p_reference_id      => lr_ar_ref_records.reference_id,
4086                                                             p_organization_id   => ln_organization_id,
4087                                                             p_location_id       => ln_location_id,
4088                                                             p_service_type_code => lv_service_type_code,
4089                                                             p_process_flag      => lv_process_flag,
4090                                                             p_process_message   => lv_process_message);
4091 
4092               IF lv_process_flag <> jai_constants.successful THEN
4093                  FND_FILE.put_line(fnd_file.log, 'Error Flag:'||lv_process_flag||' Error Message:'||lv_process_message);
4094                  RETURN;
4095               END IF;
4096 
4097               ld_accounting_date  := rec_claim_line.gl_date;
4098               ln_tax_amount := round(rec_claim_line.claim_amt * rec_claim_entries.tax_amt/ln_total_amount,
4099                                      jai_constants.service_rgm_rnd_factor);
4100 
4101               IF nvl(ln_tax_amount, 0) > 0 THEN
4102                  jai_cmn_rgm_recording_pkg.insert_repository_entry(
4103                                p_repository_id              => ln_repository_id,
4104                                p_regime_id                  => p_regime_id,
4105                                p_tax_type                   => rec_claim_entries.tax_type,
4106                                p_organization_type          => p_organization_type,
4107                                p_organization_id            => ln_organization_id,
4108                                p_location_id                => ln_location_id,
4109                                p_service_type_code          => lv_service_type_code,
4110                                p_source                     => 'AR_CLAIM',
4111                                p_source_trx_type            => 'CLAIM_ACCOUNTING',
4112                                p_source_table_name          => 'AR_RECEIVABLE_APPLICATIONS_ALL',
4113                                p_source_document_id         => rec_claim_line.receivable_application_id,
4114                                p_transaction_date           => ld_accounting_date,
4115                                p_account_name               => NULL,
4116                                p_charge_account_id          => NULL,
4117                                p_balancing_account_id       => NULL,
4118                                p_amount                     => ln_tax_amount,
4119                                p_assessable_value           => NULL,
4120                                p_tax_rate                   => lr_ar_ref_records.tax_rate,
4121                                p_reference_id               => lr_ar_ref_records.reference_id,
4122                                p_batch_id                   => p_batch_id,
4123                                p_called_from                => lv_called_from,
4124                                p_process_flag               => lv_process_flag,
4125                                p_process_message            => lv_process_message,
4126                                p_discounted_amount          => ln_discounted_amt,
4127                                p_inv_organization_id        => ln_organization_id,
4128                                p_accounting_date            => ld_accounting_date,
4129                                p_currency_code              => lr_ar_ref_records.trx_currency,
4130                                p_curr_conv_date             => lr_ar_ref_records.curr_conv_date,
4131                                p_curr_conv_type             => NULL,
4132                                p_curr_conv_rate             => lr_ar_ref_records.curr_conv_rate,
4133                                p_trx_amount                 => ln_tax_amount,--lr_ar_ref_records.trx_tax_amount,
4134                                p_accntg_required_flag       => jai_constants.no,
4135                                p_accrual_basis              => 'Y');
4136 
4137                   IF lv_process_flag = jai_constants.expected_error OR
4138                     lv_process_flag = jai_constants.unexpected_error THEN
4139                     fnd_file.put_line( fnd_file.log,
4140                       '5.1 ERROR IN CALL TO jai_cmn_rgm_processing_pkg.populate_repository - lv_process_flag '||lv_process_flag
4141                       ||', lv_process_message'||lv_process_message);
4142                     p_process_flag    := lv_process_flag;
4143                     p_process_message := lv_process_message;
4144                   END IF;
4145               END IF;
4146            END LOOP;
4147        END LOOP;
4148     END LOOP;
4149 ----------------------------------------------------------------------------------------------
4150 --Add by Xiao for POT Phase III changes, reg bug#12895841 on 1-Aug-2011, end
4151 --Add by Wenqiong for Advance Receipts Begin
4152 ----------------------------------------------------------------------------------------------
4153     FOR rec_c_ar_receipts IN c_ar_receipts
4154     LOOP
4155 
4156        ln_organization_id := rec_c_ar_receipts.organization_id;
4157        ln_location_id     := rec_c_ar_receipts.location_id;
4158 
4159        ld_receipt_date := rec_c_ar_receipts.receipt_date;
4160        ld_gl_date := rec_c_ar_receipts.gl_date;
4161        ld_accounting_date := rec_c_ar_receipts.gl_date;
4162        lv_service_type_code := rec_c_ar_receipts.service_type_code;
4163 
4164        FOR rec_receipt_entries IN get_receipt_entries_cur(rec_c_ar_receipts.cash_receipt_id)
4165        LOOP
4166 
4167            OPEN c_rcpt_ref_records(rec_c_ar_receipts.cash_receipt_id,rec_receipt_entries.doc_tax_id,'AR');
4168            FETCH c_rcpt_ref_records INTO lr_ar_ref_records;
4169            CLOSE c_rcpt_ref_records;
4170 
4171 
4172            jai_trx_repo_extract_pkg.get_doc_from_reference(p_reference_id      => lr_ar_ref_records.reference_id,
4173                                                           p_source_trx_type   => jai_constants.trx_type_adv_rcpts,
4174                                                           p_organization_id   => ln_organization_id,
4175                                                           p_location_id       => ln_location_id,
4176                                                           p_service_type_code => lv_service_type_code,
4177                                                           p_process_flag      => lv_process_flag,
4178                                                           p_process_message   => lv_process_message);
4179 
4180           IF lv_process_flag <> jai_constants.successful THEN
4181              FND_FILE.put_line(fnd_file.log, 'Error Flag:'||lv_process_flag||' Error Message:'||lv_process_message);
4182              RETURN;
4183           END IF;
4184 
4185           ln_tax_amount := lr_ar_ref_records.TAX_AMOUNT;
4186           ln_trx_tax_amount := lr_ar_ref_records.TRX_TAX_AMOUNT;
4187 
4188           IF nvl(lr_ar_ref_records.tax_amount, 0) > 0 THEN
4189              jai_cmn_rgm_recording_pkg.insert_repository_entry(
4190                              p_repository_id              => ln_repository_id,
4191                              p_regime_id                  => p_regime_id,
4192                              p_tax_type                   => rec_receipt_entries.tax_type,
4193                              p_organization_type          => p_organization_type,
4194                              p_organization_id            => ln_organization_id,
4195                              p_location_id                => ln_location_id,
4196                              p_service_type_code          => lv_service_type_code,
4197                              p_source                     => 'AR',
4198                              p_source_trx_type            => jai_constants.trx_type_adv_rcpts,
4199                              p_source_table_name          => 'AR_CASH_RECEIPTS_ALL',
4200                              p_source_document_id         => rec_c_ar_receipts.cash_receipt_id,
4201                              p_transaction_date           => ld_gl_date,
4202                              p_account_name               => NULL,
4203                              p_charge_account_id          => NULL,
4204                              p_balancing_account_id       => NULL,
4205                              p_amount                     => ln_tax_amount ,
4206                              p_assessable_value           => NULL,
4207                              p_tax_rate                   => lr_ar_ref_records.tax_rate,
4208                              p_reference_id               => lr_ar_ref_records.reference_id,
4209                              p_batch_id                   => p_batch_id,
4210                              p_called_from                => lv_called_from,
4211                              p_process_flag               => lv_process_flag,
4212                              p_process_message            => lv_process_message,
4213                              p_discounted_amount          => ln_discounted_amt,
4214                              p_inv_organization_id        => ln_organization_id,
4215                              p_accounting_date            => ld_accounting_date,
4216                              p_currency_code              => lr_ar_ref_records.trx_currency,
4217                              p_curr_conv_date             => lr_ar_ref_records.curr_conv_date,
4218                              p_curr_conv_type             => rec_c_ar_receipts.exchange_rate_type,
4219                              p_curr_conv_rate             => lr_ar_ref_records.curr_conv_rate,
4220                              p_trx_amount                 => ln_trx_tax_amount,
4221                              p_accntg_required_flag       => jai_constants.no,
4222                              p_accrual_basis              => 'Y');
4223 
4224               IF lv_process_flag = jai_constants.expected_error OR
4225                 lv_process_flag = jai_constants.unexpected_error THEN
4226                 fnd_file.put_line( fnd_file.log,
4227                   '5.1 ERROR IN CALL TO jai_cmn_rgm_processing_pkg.populate_repository - lv_process_flag '||lv_process_flag
4228                   ||', lv_process_message'||lv_process_message);
4229                 p_process_flag    := lv_process_flag;
4230                 p_process_message := lv_process_message;
4231               END IF;
4232            END IF;
4233 
4234        END LOOP;
4235     END LOOP;
4236 
4237 
4238     FOR rec_c_receipts_reversal IN c_ar_receipts_reversal
4239     LOOP
4240 
4241        ln_organization_id := rec_c_receipts_reversal.organization_id;
4242        ln_location_id     := rec_c_receipts_reversal.location_id;
4243 
4244 
4245        --ld_accounting_date := rec_c_receipts_reversal.reversal_date;
4246        ld_accounting_date := rec_c_receipts_reversal.gl_date;
4247        --Modified by Qinglei on 17-Jan-2012 for bug#13569242
4248        ld_gl_date         := rec_c_receipts_reversal.gl_date;
4249 
4250        lv_service_type_code := rec_c_receipts_reversal.service_type_code;
4251 
4252        FOR rec_receipt_entries IN get_receipt_entries_cur(rec_c_receipts_reversal.cash_receipt_id)
4253        LOOP
4254            OPEN c_rcpt_ref_records(rec_c_receipts_reversal.cash_receipt_id,rec_receipt_entries.doc_tax_id,'AR');
4255            FETCH c_rcpt_ref_records INTO lr_ar_ref_records;
4256            CLOSE c_rcpt_ref_records;
4257 
4258           ln_tax_amount := - ABS(lr_ar_ref_records.TAX_AMOUNT);
4259           ln_trx_tax_amount := - ABS(lr_ar_ref_records.TRX_TAX_AMOUNT);
4260 
4261 
4262            jai_trx_repo_extract_pkg.get_doc_from_reference(p_reference_id     => lr_ar_ref_records.reference_id,
4263                                                           p_source_trx_type   => jai_constants.trx_type_adv_rvs,
4264                                                           --Modified jai_constants.trx_type_rct_rvs to jai_constants.trx_type_adv_rvs
4265                                                           --by Qinglei on 28-Dec-2012 for bug#13741544
4266                                                           p_organization_id   => ln_organization_id,
4267                                                           p_location_id       => ln_location_id,
4268                                                           p_service_type_code => lv_service_type_code,
4269                                                           p_process_flag      => lv_process_flag,
4270                                                           p_process_message   => lv_process_message);
4271 
4272           IF lv_process_flag <> jai_constants.successful THEN
4273              FND_FILE.put_line(fnd_file.log, 'Error Flag:'||lv_process_flag||' Error Message:'||lv_process_message);
4274              RETURN;
4275           END IF;
4276 
4277 
4278           IF nvl(lr_ar_ref_records.tax_amount, 0) <> 0 THEN
4279              jai_cmn_rgm_recording_pkg.insert_repository_entry(
4280                              p_repository_id              => ln_repository_id,
4281                              p_regime_id                  => p_regime_id,
4282                              p_tax_type                   => rec_receipt_entries.tax_type,
4283                              p_organization_type          => p_organization_type,
4284                              p_organization_id            => ln_organization_id,
4285                              p_location_id                => ln_location_id,
4286                              p_service_type_code          => lv_service_type_code,
4287                              p_source                     => 'AR',
4288                              p_source_trx_type            => jai_constants.trx_type_adv_rvs,
4289                              --Modified jai_constants.trx_type_rct_rvs to jai_constants.trx_type_adv_rvs
4290                              --by Qinglei on 28-Dec-2012 for bug#13741544
4291                              p_source_table_name          => 'AR_CASH_RECEIPTS_ALL',
4292                              p_source_document_id         => rec_c_receipts_reversal.cash_receipt_id,
4293                              p_transaction_date           => ld_gl_date,
4294                              p_account_name               => NULL,
4295                              p_charge_account_id          => NULL,
4296                              p_balancing_account_id       => NULL,
4297                              p_amount                     => ln_tax_amount ,
4298                              p_assessable_value           => NULL,
4299                              p_tax_rate                   => lr_ar_ref_records.tax_rate,
4300                              p_reference_id               => lr_ar_ref_records.reference_id,
4301                              p_batch_id                   => p_batch_id,
4302                              p_called_from                => lv_called_from,
4303                              p_process_flag               => lv_process_flag,
4304                              p_process_message            => lv_process_message,
4305                              p_discounted_amount          => ln_discounted_amt,
4306                              p_inv_organization_id        => ln_organization_id,
4307                              p_accounting_date            => ld_accounting_date,
4308                              p_currency_code              => lr_ar_ref_records.trx_currency,
4309                              p_curr_conv_date             => lr_ar_ref_records.curr_conv_date,
4310                              p_curr_conv_type             => rec_c_receipts_reversal.exchange_rate_type,
4311                              p_curr_conv_rate             => lr_ar_ref_records.curr_conv_rate,
4312                              p_trx_amount                 => ln_trx_tax_amount,
4313                              p_accntg_required_flag       => jai_constants.no,
4314                              p_accrual_basis              => 'Y');
4315 
4316               IF lv_process_flag = jai_constants.expected_error OR
4317                 lv_process_flag = jai_constants.unexpected_error THEN
4318                 fnd_file.put_line( fnd_file.log,
4319                   '5.1 ERROR IN CALL TO jai_cmn_rgm_processing_pkg.populate_repository - lv_process_flag '||lv_process_flag
4320                   ||', lv_process_message'||lv_process_message);
4321                 p_process_flag    := lv_process_flag;
4322                 p_process_message := lv_process_message;
4323               END IF;
4324            END IF;
4325 
4326        END LOOP;
4327     END LOOP;
4328 
4329     FOR rec_c_receipts_application IN c_receipt_application
4330     LOOP
4331 
4332        ln_organization_id := rec_c_receipts_application.organization_id;
4333        ln_location_id     := rec_c_receipts_application.location_id;
4334        ln_discounted_amt  := rec_c_receipts_application.discount_amount;
4335 
4336        ld_accounting_date := rec_c_receipts_application.gl_date;
4337 
4338        lv_service_type_code := rec_c_receipts_application.service_type_code;
4339 
4340        /*Added by Qinglei on 05-Jan-2012 for bug#13556198 begin*/
4341        ln_converted_rate := jai_cmn_utils_pkg.currency_conversion(rec_c_receipts_application.set_of_books_id,
4342                                           rec_c_receipts_application.receipt_currency_code,
4343                                           rec_c_receipts_application.receipt_exchange_date,
4344                                           rec_c_receipts_application.receipt_exchange_rate_type,
4345                                           rec_c_receipts_application.receipt_exchange_rate);
4346        /*Added by Qinglei on 05-Jan-2012 for bug#13556198 end*/
4347 
4348        FOR rec_receipt_entries IN get_receipt_entries_cur(rec_c_receipts_application.cash_receipt_id)
4349        LOOP
4350            OPEN c_rcpt_ref_records(rec_c_receipts_application.cash_receipt_id,rec_receipt_entries.doc_tax_id,'AR');
4351            FETCH c_rcpt_ref_records INTO lr_ar_ref_records;
4352            CLOSE c_rcpt_ref_records;
4353 
4354            /*
4355              If Invoice and Receipt currency are different, data in AR_RECEIVABLE_APPLICATIONS_ALL is populated
4356              in Invoice Currency. Hence the same cannot be passed as Transaction Amount in JAI_RGM_TRX_RECORDS
4357              Transaction Amount Data in JAI_RGM_TRX_RECORDS must be in Receipt Currency for Receipt Application Event.
4358            */
4359            ln_tax_amount := -ROUND(ABS(rec_receipt_entries.func_tax_amt
4360                                 * (rec_c_receipts_application.AMOUNT_APPLIED_FROM/
4361                                 (rec_c_receipts_application.AMOUNT * NVL(ln_converted_rate,1)))),
4362                                 jai_constants.service_rgm_rnd_factor);
4363                                 --Modified by Qinglei on 05-Jan-2012 for bug#13556198
4364                                 --Added rounding factor by Qinglei on 13-Jan-2012 for bug#13557031
4365            ln_trx_tax_amount := -ROUND(ABS(rec_receipt_entries.tax_amt
4366                                 * (rec_c_receipts_application.AMOUNT_APPLIED_FROM/
4367                                 (rec_c_receipts_application.AMOUNT * NVL(ln_converted_rate,1)))),
4368                                 jai_constants.service_rgm_rnd_factor);
4369                                 --Modified by Qinglei on 05-Jan-2012 for bug#13556198
4370            jai_trx_repo_extract_pkg.get_doc_from_reference(p_reference_id     => lr_ar_ref_records.reference_id,
4371                                                           p_source_trx_type   => jai_constants.trx_type_adv_app,
4372                                                           p_application_id    => rec_c_receipts_application.receivable_application_id,
4373                                                           p_organization_id   => ln_organization_id,
4374                                                           p_location_id       => ln_location_id,
4375                                                           p_service_type_code => lv_service_type_code,
4376                                                           p_process_flag      => lv_process_flag,
4377                                                           p_process_message   => lv_process_message);
4378 
4379           IF lv_process_flag <> jai_constants.successful THEN
4380              FND_FILE.put_line(fnd_file.log, 'Error Flag:'||lv_process_flag||' Error Message:'||lv_process_message);
4381              RETURN;
4382           END IF;
4383 
4384 
4385           IF nvl(ln_tax_amount, 0) <> 0 THEN
4386              jai_cmn_rgm_recording_pkg.insert_repository_entry(
4387                              p_repository_id              => ln_repository_id,
4388                              p_regime_id                  => p_regime_id,
4389                              p_tax_type                   => rec_receipt_entries.tax_type,
4390                              p_organization_type          => p_organization_type,
4391                              p_organization_id            => ln_organization_id,
4392                              p_location_id                => ln_location_id,
4393                              p_service_type_code          => lv_service_type_code,
4394                              p_source                     => 'AR',
4395                              p_source_trx_type            => jai_constants.trx_type_adv_app,
4396                              p_source_table_name          => 'AR_RECEIVABLE_APPLICATIONS_ALL',
4397                              p_source_document_id         => rec_c_receipts_application.receivable_application_id,
4398                              p_transaction_date           => rec_c_receipts_application.gl_date,
4399                              p_account_name               => NULL,
4400                              p_charge_account_id          => NULL,
4401                              p_balancing_account_id       => NULL,
4402                              p_amount                     => ln_tax_amount ,
4403                              p_assessable_value           => NULL,
4404                              p_tax_rate                   => lr_ar_ref_records.tax_rate,
4405                              p_reference_id               => lr_ar_ref_records.reference_id,
4406                              p_batch_id                   => p_batch_id,
4407                              p_called_from                => lv_called_from,
4408                              p_process_flag               => lv_process_flag,
4409                              p_process_message            => lv_process_message,
4410                              p_discounted_amount          => ln_discounted_amt,
4411                              p_inv_organization_id        => ln_organization_id,
4412                              p_accounting_date            => rec_c_receipts_application.gl_date,
4413                              p_currency_code              => lr_ar_ref_records.trx_currency,
4414                              p_curr_conv_date             => lr_ar_ref_records.curr_conv_date,
4415                              p_curr_conv_type             => rec_c_receipts_application.receipt_exchange_rate_type,
4416                              p_curr_conv_rate             => lr_ar_ref_records.curr_conv_rate,
4417                              p_trx_amount                 => ln_trx_tax_amount,
4418                              p_accntg_required_flag       => jai_constants.no,
4419                              p_accrual_basis              => 'Y');
4420 
4421               IF lv_process_flag = jai_constants.expected_error OR
4422                 lv_process_flag = jai_constants.unexpected_error THEN
4423                 fnd_file.put_line( fnd_file.log,
4424                   '5.1 ERROR IN CALL TO jai_cmn_rgm_processing_pkg.populate_repository - lv_process_flag '||lv_process_flag
4425                   ||', lv_process_message'||lv_process_message);
4426                 p_process_flag    := lv_process_flag;
4427                 p_process_message := lv_process_message;
4428               END IF;
4429            END IF;
4430 
4431        END LOOP;
4432     END LOOP;
4433 
4434     FOR rec_c_receipts_unapplication IN c_receipt_unapplication
4435     LOOP
4436 
4437        ln_organization_id := rec_c_receipts_unapplication.organization_id;
4438        ln_location_id     := rec_c_receipts_unapplication.location_id;
4439        ln_discounted_amt  := rec_c_receipts_unapplication.discount_amount;
4440 
4441        ld_accounting_date := rec_c_receipts_unapplication.apply_date;
4442 
4443        lv_service_type_code := rec_c_receipts_unapplication.service_type_code;
4444 
4445        /*Added by Qinglei on 05-Jan-2012 for bug#13556198 begin*/
4446        ln_converted_rate := jai_cmn_utils_pkg.currency_conversion(rec_c_receipts_unapplication.set_of_books_id,
4447                                           rec_c_receipts_unapplication.receipt_currency_code,
4448                                           rec_c_receipts_unapplication.receipt_exchange_date,
4449                                           rec_c_receipts_unapplication.receipt_exchange_rate_type,
4450                                           rec_c_receipts_unapplication.receipt_exchange_rate);
4451        /*Added by Qinglei on 05-Jan-2012 for bug#13556198 end*/
4452 
4453        FOR rec_receipt_entries IN get_receipt_entries_cur(rec_c_receipts_unapplication.cash_receipt_id)
4454        LOOP
4455             OPEN c_rcpt_ref_records(rec_c_receipts_unapplication.cash_receipt_id,rec_receipt_entries.doc_tax_id,'AR');
4456            FETCH c_rcpt_ref_records INTO lr_ar_ref_records;
4457            CLOSE c_rcpt_ref_records;
4458 
4459            /*
4460              If Invoice and Receipt currency are different, data in AR_RECEIVABLE_APPLICATIONS_ALL is populated
4461              in Invoice Currency. Hence the same cannot be passed as Transaction Amount in JAI_RGM_TRX_RECORDS
4462              Transaction Amount Data in JAI_RGM_TRX_RECORDS must be in Receipt Currency for Receipt Application Event.
4463            */
4464            ln_tax_amount := ROUND(ABS(rec_receipt_entries.func_tax_amt
4465                                 * (rec_c_receipts_unapplication.AMOUNT_APPLIED_FROM/
4466                                 (rec_c_receipts_unapplication.AMOUNT * NVL(ln_converted_rate,1)))),
4467                                 jai_constants.service_rgm_rnd_factor);
4468                                 --Modified by Qinglei on 05-Jan-2012 for bug#13556198
4469                                 --Added rounding factor by Qinglei on 13-Jan-2012 for bug#13557031
4470            ln_trx_tax_amount := ROUND(ABS(rec_receipt_entries.tax_amt
4471                                 * (rec_c_receipts_unapplication.AMOUNT_APPLIED_FROM/
4472                                 (rec_c_receipts_unapplication.AMOUNT * NVL(ln_converted_rate,1)))),
4473                                 jai_constants.service_rgm_rnd_factor);
4474                                 --Modified by Qinglei on 05-Jan-2012 for bug#13556198
4475                                 --Added rounding factor by Qinglei on 13-Jan-2012 for bug#13557031
4476 
4477            jai_trx_repo_extract_pkg.get_doc_from_reference(p_reference_id     => lr_ar_ref_records.reference_id,
4478                                                           p_source_trx_type   => jai_constants.trx_type_adv_unapp,
4479                                                           p_application_id    => rec_c_receipts_unapplication.receivable_application_id,
4480                                                           p_organization_id   => ln_organization_id,
4481                                                           p_location_id       => ln_location_id,
4482                                                           p_service_type_code => lv_service_type_code,
4483                                                           p_process_flag      => lv_process_flag,
4484                                                           p_process_message   => lv_process_message);
4485 
4486           IF lv_process_flag <> jai_constants.successful THEN
4487              FND_FILE.put_line(fnd_file.log, 'Error Flag:'||lv_process_flag||' Error Message:'||lv_process_message);
4488              RETURN;
4489           END IF;
4490 
4491 
4492           IF nvl(ln_tax_amount, 0) <> 0 THEN
4493              jai_cmn_rgm_recording_pkg.insert_repository_entry(
4494                              p_repository_id              => ln_repository_id,
4495                              p_regime_id                  => p_regime_id,
4496                              p_tax_type                   => rec_receipt_entries.tax_type,
4497                              p_organization_type          => p_organization_type,
4498                              p_organization_id            => ln_organization_id,
4499                              p_location_id                => ln_location_id,
4500                              p_service_type_code          => lv_service_type_code,
4501                              p_source                     => 'AR',
4502                              p_source_trx_type            => jai_constants.trx_type_adv_unapp,
4503                              p_source_table_name          => 'AR_RECEIVABLE_APPLICATIONS_ALL',
4504                              p_source_document_id         => rec_c_receipts_unapplication.receivable_application_id,
4505                              p_transaction_date           => rec_c_receipts_unapplication.gl_date,
4506                              p_account_name               => NULL,
4507                              p_charge_account_id          => NULL,
4508                              p_balancing_account_id       => NULL,
4509                              p_amount                     => ln_tax_amount ,
4510                              p_assessable_value           => NULL,
4511                              p_tax_rate                   => lr_ar_ref_records.tax_rate,
4512                              p_reference_id               => lr_ar_ref_records.reference_id,
4513                              p_batch_id                   => p_batch_id,
4514                              p_called_from                => lv_called_from,
4515                              p_process_flag               => lv_process_flag,
4516                              p_process_message            => lv_process_message,
4517                              p_discounted_amount          => ln_discounted_amt,
4518                              p_inv_organization_id        => ln_organization_id,
4519                              p_accounting_date            =>  rec_c_receipts_unapplication.gl_date,
4520                              p_currency_code              => lr_ar_ref_records.trx_currency,
4521                              p_curr_conv_date             => lr_ar_ref_records.curr_conv_date,
4522                              p_curr_conv_type             => rec_c_receipts_unapplication.receipt_exchange_rate_type,
4523                              p_curr_conv_rate             => lr_ar_ref_records.curr_conv_rate,
4524                              p_trx_amount                 => ln_trx_tax_amount,
4525                              p_accntg_required_flag       => jai_constants.no,
4526                              p_accrual_basis              => 'Y');
4527 
4528               IF lv_process_flag = jai_constants.expected_error OR
4529                 lv_process_flag = jai_constants.unexpected_error THEN
4530                 fnd_file.put_line( fnd_file.log,
4531                   '5.1 ERROR IN CALL TO jai_cmn_rgm_processing_pkg.populate_repository - lv_process_flag '||lv_process_flag
4532                   ||', lv_process_message'||lv_process_message);
4533                 p_process_flag    := lv_process_flag;
4534                 p_process_message := lv_process_message;
4535               END IF;
4536            END IF;
4537 
4538        END LOOP;
4539     END LOOP;
4540 ----------------------------------------------------------------------------------------------
4541 --Add by Wenqiong for Advance Receipts Begin
4542 
4543 END populate_repository;
4544 
4545 /*Bug 11821537 - End*/
4546 
4547 --Add by Xiao for POT Phase III changes, reg bug#12895841 on 1-Aug-2011, begin
4548 ----------------------------------------------------------------------------------------------
4549 PROCEDURE process_claim(
4550         pn_regime_id            IN     NUMBER,
4551         pn_organization_type    IN     VARCHAR2,
4552         pd_from_date            IN     DATE,
4553         pd_to_date              IN     DATE,
4554         pn_organization_id      IN     NUMBER) IS
4555 
4556   CURSOR get_claim_trxn_cur IS
4557   SELECT jsir.*
4558     FROM jai_st_invoice_reversal  jsir
4559    WHERE jsir.source = 'AR'
4560      AND jsir.tax_to_be_adjusted > 0;
4561 
4562   CURSOR get_claim_amt_cur(pn_invoice_id IN NUMBER, pn_line_number IN NUMBER, pn_max_payment_id IN NUMBER) IS
4563   SELECT nvl(amount_applied, 0) claim_amt, receivable_application_id, gl_date
4564     FROM ar_receivable_applications_all araa
4565    WHERE araa.applied_customer_trx_id = pn_invoice_id
4566      AND araa.apply_date <= pd_to_date
4567      AND araa.status = 'APP'
4568      AND araa.receivable_application_id > nvl(pn_max_payment_id, 0)
4569      AND NOT EXISTS (SELECT 1
4570                        FROM jai_rgm_trx_records jrtr
4571                       WHERE jrtr.source_table_name = 'AR_RECEIVABLE_APPLICATIONS_ALL'
4572                         AND jrtr.source_trx_type = 'CLAIM_ACCOUNTING'
4573                         AND jrtr.source = 'AR_CLAIM'
4574                         AND jrtr.source_document_id = araa.receivable_application_id)
4575      AND EXISTS (SELECT 1
4576                    FROM ra_cust_trx_line_gl_dist_all rg
4577                   WHERE rg.customer_trx_id = pn_invoice_id
4578                     AND rg.customer_trx_line_id = pn_line_number
4579                     AND rg.account_class = 'REV'
4580                     AND jai_st_reversal_extract_pkg.is_paid_after_reversal(araa.apply_date,
4581                            rg.gl_date, 'AR') = 'Y');
4582 
4583     ln_max_payment_id     NUMBER;
4584 
4585  BEGIN
4586 
4587    FOR reversal_trxn_rec IN get_claim_trxn_cur
4588    LOOP
4589        ln_max_payment_id := reversal_trxn_rec.max_claim_payment_id;
4590 
4591       FOR rec_claim_entry IN get_claim_amt_cur(reversal_trxn_rec.invoice_id, reversal_trxn_rec.customer_trx_line_id, ln_max_payment_id)
4592        LOOP
4593 
4594            IF nvl(rec_claim_entry.claim_amt,0) > 0 THEN
4595              jai_cmn_rgm_processing_pkg.claim_reversal_trx_lines(
4596                 pv_source               => 'AR',
4597                 pn_organization_id      => pn_organization_id,
4598                 pn_location_id          => reversal_trxn_rec.location_id,
4599                 pn_invoice_id           => reversal_trxn_rec.invoice_id,
4600                 pn_line_number          => reversal_trxn_rec.customer_trx_line_id,
4601                 pd_date                 => rec_claim_entry.gl_date,
4602                 pn_adjusted_amount      => nvl(rec_claim_entry.claim_amt,0) ,
4603                 pv_action               => 'CLAIM',
4604                 pn_claim_payment_id     => rec_claim_entry.receivable_application_id);
4605            END IF;
4606 
4607            IF rec_claim_entry.receivable_application_id > nvl(ln_max_payment_id, 0) THEN
4608               ln_max_payment_id := rec_claim_entry.receivable_application_id;
4609            END IF;
4610 
4611        END LOOP;
4612        IF nvl(ln_max_payment_id, 0) > nvl(reversal_trxn_rec.max_claim_payment_id, 0) THEN
4613           UPDATE jai_st_invoice_reversal
4614             SET max_claim_payment_id = ln_max_payment_id
4615           WHERE invoice_id = reversal_trxn_rec.invoice_id
4616             AND customer_trx_line_id = reversal_trxn_rec.customer_trx_line_id
4617             AND SOURCE = 'AR';
4618        END IF;
4619    END LOOP;
4620 
4621 END process_claim;
4622 ----------------------------------------------------------------------------------------------
4623 --Add by Xiao for POT Phase III changes, reg bug#12895841 on 1-Aug-2011, end
4624 
4625 
4626 procedure process_records   ( p_regime_id          IN  JAI_RGM_DEFINITIONS.REGIME_ID%TYPE                ,
4627                               p_organization_type  IN  JAI_RGM_PARTIES.ORGANIZATION_TYPE%TYPE    ,
4628                               p_from_date          IN  DATE                                      ,
4629                               p_to_date            IN  DATE                                      ,
4630                               p_org_id             IN  RA_CUSTOMER_TRX_ALL.ORG_ID%TYPE           ,
4631                               p_batch_id           IN  JAI_RGM_TRX_REFS.BATCH_ID%TYPE            ,
4632                               p_process_flag OUT NOCOPY VARCHAR2                                  ,
4633                               p_process_message OUT NOCOPY VARCHAR2                               ,
4634                               p_organization_id     IN JAI_RGM_PARTIES.ORGANIZATION_ID%TYPE DEFAULT NULL /*5879769*/
4635                             )
4636 IS
4637 
4638   lv_source_ar              varchar2(2); --File.Sql.35 Cbabu  jai_constants.SOURCE_AR%TYPE                               ;
4639   lv_process_flag           VARCHAR2(2)                                                ;
4640   lv_process_message        VARCHAR2(1996)                                             ;
4641   lv_organization_id       jai_rgm_parties.organization_id%type;
4642   /*Bug 11821537 - Start*/
4643   /*--Commented by zhiwei.hou for POT code port
4644   CURSOR c_get_st_accrual_date
4645   IS
4646   SELECT to_date(attribute_value, 'DD/MM/YYYY')
4647   FROM jai_rgm_registrations
4648   WHERE regime_id = (select regime_id
4649                      from JAI_RGM_DEFINITIONS
4650                      where regime_code = jai_constants.service_regime)
4651   AND attribute_code = 'EFF_DATE_ST_PT'
4652   AND attribute_type_code = 'OTHERS'
4653   AND registration_type = 'OTHERS';
4654   */
4655   --ld_st_accrual_date          DATE; /*11821537*/ --Commented by zhiwei.hou for POT code port.
4656   /*Bug 11821537 - End*/
4657 BEGIN
4658 
4659   fnd_file.put_line(fnd_file.LOG,'############################## 1 START OF PROCEDURE TO jai_ar_rgm_processing_pkg.PROCESS_RECORDS ############################## ');
4660 
4661   /*########################################################################################################
4662   || VARIABLES INITIALIZATION
4663   ########################################################################################################*/
4664 
4665   lv_source_ar       := jai_constants.source_ar    ;
4666 
4667   lv_process_flag    := jai_constants.successful   ;
4668   lv_process_message := null                       ;
4669 
4670   p_process_flag     := lv_process_flag            ;
4671   p_process_message  := lv_process_message         ;
4672   /*Bug 11821537 - Start*/
4673   /*Bug 12805386 - Accrual Date is fetched in each procedure for each Transaction*/
4674   /*--Commented by zhiwei.hou for POT  code port.
4675   OPEN c_get_st_accrual_date;
4676   FETCH c_get_st_accrual_date INTO ld_st_accrual_date;
4677   CLOSE c_get_st_accrual_date;
4678   */
4679 
4680 /* --This is commented by Xiao for POT changes, reg bug#12533434 on 10-May-2011,
4681    --as ST processor should process for both Accrual/Cash basis.
4682 
4683   IF ld_st_accrual_date > p_from_date OR ld_st_accrual_date > p_to_date THEN
4684     fnd_file.put_line( fnd_file.log, '1.1 From and To Date parameters for Service Tax Processing cannot be less than Effective Date for
4685     Service Tax Point of Taxation in Regime Registration - lv_process_flag '|| jai_constants.unexpected_error
4686                                       ||', lv_process_message Verify Date Parameters');
4687     p_process_flag    := jai_constants.unexpected_error    ;
4688     p_process_message := 'Verify Date Parameters' ;
4689   END IF;*/
4690   /*Bug 11821537 - End*/
4691   fnd_file.put_line(fnd_file.LOG,'2 i/p is p_regime_id ->'||p_regime_id
4692                                   ||', p_regime_id         ->'||p_regime_id
4693                                   ||', p_organization_type ->'||p_organization_type
4694                                   ||', p_from_date         ->'||p_from_date
4695                                   ||', p_to_date           ->'||p_to_date
4696                                   ||', p_org_id            ->'||p_org_id
4697                                   ||', p_batch_id          ->'||p_batch_id
4698                     );
4699 
4700 
4701   /*########################################################################################################
4702   || PROCESS AR INVOICE AND CREDIT MEMO'S FOR REFERENCE ENTRIES
4703   ########################################################################################################*/
4704 
4705   fnd_file.put_line(fnd_file.LOG,'############################## 3 BEFORE CALL TO jai_ar_rgm_processing_pkg.POPULATE_INV_CM_REFERENCES ##############################');
4706 
4707   populate_inv_cm_references  (  p_regime_id         =>  p_regime_id             ,
4708                                  p_organization_type =>  p_organization_type     ,
4709                                  p_organization_id   =>  p_organization_id       ,/*5879769*/
4710                                  p_from_date         =>  p_from_date             ,
4711                                  p_to_date           =>  p_to_date               ,
4712                                  p_org_id            =>  p_org_id                ,
4713                                  p_batch_id          =>  p_batch_id              ,
4714                                  p_source            =>  lv_source_ar            ,
4715                                  p_process_flag      =>  lv_process_flag         ,
4716                                  p_process_message   =>  lv_process_message
4717                                );
4718 
4719   --Add by Xiao for POT Phase III, reg bug#12895841, begin
4720      process_claim(
4721           pn_regime_id              => p_regime_id,
4722           pn_organization_type      => p_organization_type,
4723           pd_from_date              => p_from_date,
4724           pd_to_date                => p_to_date,
4725           pn_organization_id        => p_organization_id);
4726   --Add by Xiao for POT Phase III, reg bug#12895841, end
4727 
4728 
4729   IF lv_process_flag = jai_constants.expected_error    OR
4730      lv_process_flag = jai_constants.unexpected_error
4731   THEN
4732     /*
4733     || As Returned status is an error hence:-
4734     || Set out variables p_process_flag and p_process_message accordingly
4735     */
4736 
4737     fnd_file.put_line( fnd_file.log, '4 ERROR IN CALL TO jai_ar_rgm_processing_pkg.POPULATE_INV_CM_REFERENCES - lv_process_flag '||lv_process_flag
4738                                       ||', lv_process_message'||lv_process_message);
4739     p_process_flag    := lv_process_flag    ;
4740     p_process_message := lv_process_message ;
4741   END IF;
4742 
4743    fnd_file.put_line(fnd_file.LOG,'############################## 5 RETURNED FROM jai_ar_rgm_processing_pkg.POPULATE_INV_CM_REFERENCES'||'lv_process_flag - '||lv_process_flag||
4744                                   ' lv_process_message- '||lv_process_message||'############################## ');
4745 
4746   fnd_file.put_line(fnd_file.LOG,'############################## 6 BEFORE CALL TO jai_ar_rgm_processing_pkg.populate_repository ##############################');
4747 
4748   populate_repository ( p_regime_id          =>  p_regime_id             ,
4749                         p_organization_type  =>  p_organization_type     ,
4750                         p_organization_id    =>  p_organization_id       ,
4751                         p_from_date          =>  p_from_date             ,
4752                         p_to_date            =>  p_to_date               ,
4753                         p_org_id             =>  p_org_id                ,
4754                         p_batch_id           =>  p_batch_id              ,
4755                         p_source             =>  lv_source_ar            ,
4756                         p_process_flag       =>  lv_process_flag         ,
4757                         p_process_message    =>  lv_process_message
4758                       );
4759 
4760   IF lv_process_flag = jai_constants.expected_error    OR
4761      lv_process_flag = jai_constants.unexpected_error
4762   THEN
4763     /*
4764     || As Returned status is an error hence:-
4765     || Set out variables p_process_flag and p_process_message accordingly
4766     */
4767     fnd_file.put_line( fnd_file.log, '7 ERROR IN CALL TO jai_ar_rgm_processing_pkg.populate_repository - lv_process_flag '||lv_process_flag
4768                                       ||', lv_process_message'||lv_process_message);
4769     p_process_flag    := lv_process_flag    ;
4770     p_process_message := lv_process_message ;
4771   END IF;
4772 
4773    fnd_file.put_line(fnd_file.LOG,'############################## 8 RETURNED FROM jai_ar_rgm_processing_pkg.populate_repository'||'lv_process_flag - '||lv_process_flag||
4774                                   ' lv_process_message- '||lv_process_message||'############################## ');
4775   /*########################################################################################################
4776   || DELETE NON INCOMPLETE/NON-EXISTING CREDIT MEMO'S
4777   ########################################################################################################*/
4778 
4779   /*
4780   || Reverse all those AR Credit Memo's which have been incompleted/incompleted
4781   || and deleted from base ar tables
4782   */
4783   /*Bug 11821537 - Update repository entry. Accounting is taken care during AR Invoice Creation or Import
4784   Hence commenting procedures delete_non_existant_cm, populate_cm_app and populate_receipt_records
4785   Code shall be rewritten to pick AR Invoices*/
4786   --/* --Commented comments by zhiwei.hou for POT code port.
4787   fnd_file.put_line(fnd_file.LOG,'############################## 6 BEFORE CALL TO jai_ar_rgm_processing_pkg.DELETE_NON_EXISTANT_CM ############################## ');
4788 
4789   delete_non_existant_cm                (   p_regime_id          =>  p_regime_id             ,
4790                                             p_organization_type  =>  p_organization_type     ,
4791                                             p_organization_id    =>  p_organization_id       ,
4792                                             p_from_date          =>  p_from_date             ,
4793                                             p_to_date            =>  p_to_date               ,
4794                                             p_org_id             =>  p_org_id                ,
4795                                             p_source             =>  lv_source_ar            ,
4796                                             p_batch_id           =>  p_batch_id              ,
4797                                             p_process_flag       =>  lv_process_flag         ,
4798                                             p_process_message    =>  lv_process_message
4799                                         );
4800 
4801   IF lv_process_flag = jai_constants.expected_error    OR
4802      lv_process_flag = jai_constants.unexpected_error
4803   THEN
4804 
4805     fnd_file.put_line( fnd_file.log, '7 ERROR IN CALL TO jai_ar_rgm_processing_pkg.DELETE_NON_EXISTANT_CM - lv_process_flag '||lv_process_flag
4806                                       ||', lv_process_message'||lv_process_message);
4807     p_process_flag    := lv_process_flag    ;
4808     p_process_message := lv_process_message ;
4809   END IF;
4810 
4811 
4812   fnd_file.put_line(fnd_file.LOG,'############################## 8 RETURNED FROM jai_ar_rgm_processing_pkg.DELETE_NON_EXISTANT_CM'||'lv_process_flag - '||lv_process_flag||
4813                                   ' lv_process_message- '||lv_process_message||'############################## ');
4814   --*/--Commented comments by zhiwei.hou for POT code port
4815 
4816   /*########################################################################################################
4817   || PROCESS AR CREDIT MEMO APPLICATIONS AGAINST INVOICES
4818   ########################################################################################################*/
4819 
4820   --/*--Commented comments by zhiwei.hou for POT code port.
4821   fnd_file.put_line(fnd_file.LOG,'############################## 9 BEFORE CALL TO jai_ar_rgm_processing_pkg.POPULATE_CM_APP ############################## ');
4822 
4823   populate_cm_app   ( p_regime_id          =>  p_regime_id             ,
4824                       p_organization_type  =>  p_organization_type     ,
4825                       p_organization_id    =>  p_organization_id       ,
4826                       p_from_date          =>  p_from_date             ,
4827                       p_to_date            =>  p_to_date               ,
4828                       p_org_id             =>  p_org_id                ,
4829                       p_source             =>  lv_source_ar            ,
4830                       p_batch_id           =>  p_batch_id              ,
4831                       p_process_flag       =>  lv_process_flag         ,
4832                       p_process_message    =>  lv_process_message
4833                     );
4834 
4835 
4836   IF lv_process_flag = jai_constants.expected_error    OR
4837      lv_process_flag = jai_constants.unexpected_error
4838   THEN
4839 
4840     fnd_file.put_line( fnd_file.log, '10 error in call to jai_ar_rgm_processing_pkg.populate_cm_app - lv_process_flag '||lv_process_flag
4841                                       ||', lv_process_message'||lv_process_message);
4842     p_process_flag    := lv_process_flag    ;
4843     p_process_message := lv_process_message ;
4844   END IF;
4845 
4846 
4847    fnd_file.put_line(fnd_file.LOG,'############################## 11 RETURNED FROM CALL TO jai_ar_rgm_processing_pkg.POPULATE_CM_APP '||'lv_process_flag - '||lv_process_flag||
4848                                   ' lv_process_message- '||lv_process_message||'##############################');
4849 
4850   --*/--Commented comments by zhiwei.hou for POT code port.
4851   /*########################################################################################################
4852   || PROCESS AR CASH RECEIPT APPLICATIONS AGAINST INVOICES
4853   ########################################################################################################*/
4854 
4855   --/*--Commented comments by zhiwei.hou for POT code port.
4856   fnd_file.put_line(fnd_file.LOG,'############################## 12 BEFORE CALL TO jai_ar_rgm_processing_pkg.POPULATE_RECEIPT_RECORDS ##############################');
4857 
4858   populate_receipt_records   ( p_regime_id          =>  p_regime_id             ,
4859                                p_organization_type  =>  p_organization_type     ,
4860                                p_organization_id    =>  p_organization_id       ,
4861                                p_from_date          =>  p_from_date             ,
4862                                p_to_date            =>  p_to_date               ,
4863                                p_org_id             =>  p_org_id                ,
4864                                p_batch_id           =>  p_batch_id              ,
4865                                p_source             =>  lv_source_ar            ,
4866                                p_process_flag       =>  lv_process_flag         ,
4867                                p_process_message    =>  lv_process_message
4868                              );
4869 
4870   IF lv_process_flag = jai_constants.expected_error    OR
4871      lv_process_flag = jai_constants.unexpected_error
4872   THEN
4873 
4874     fnd_file.put_line( fnd_file.log, '13 ERROR IN CALL TO jai_ar_rgm_processing_pkg.POPULATE_RECEIPT_RECORDS - lv_process_flag '||lv_process_flag
4875                                       ||', lv_process_message'||lv_process_message);
4876     p_process_flag    := lv_process_flag    ;
4877     p_process_message := lv_process_message ;
4878   END IF;
4879 
4880   fnd_file.put_line(fnd_file.LOG,'##############################14 RETURNED FROM jai_ar_rgm_processing_pkg.POPULATE_RECEIPT_RECORDS'||'lv_process_flag - '||lv_process_flag||
4881                                   ' lv_process_message- '||lv_process_message||'##############################');
4882   --*/--Commented comments by zhiwei.hou for POT code port.
4883 
4884   fnd_file.put_line(fnd_file.LOG,'############################## 15 END OF PROCEDURE TO jai_ar_rgm_processing_pkg.PROCESS_RECORDS - ##############################');
4885 
4886 EXCEPTION
4887   WHEN OTHERS THEN
4888     lv_process_flag    := jai_constants.unexpected_error;
4889     lv_process_message := ' 16 Unexpected error occured while processing jai_ar_rgm_processing_pkg.process_records'||SQLERRM ;
4890 
4891 END process_records;
4892 
4893 /*Added by Wenqiong for Advanced Receipt on Service Taxes Begin */
4894 --========================================================================
4895 -- PROCEDURE : process_st_advance_receipts       PUBLIC
4896 -- PARAMETERS: p_event            current event name
4897 --           : p_acra             rowtype of ar_cash_receipts_all
4898 --           : p_araa             rowtype of ar_receivable_applications_all
4899 --           : p_process_flag     the process result
4900 --           : p_process_message  the process message
4901 -- COMMENT   : process service tax on advance receipt according to event
4902 -- PRE-COND  : AR Receipt must attach service tax
4903 -- EXCEPTIONS:
4904 --
4905 --========================================================================
4906 PROCEDURE process_st_advance_receipts
4907 ( p_event             IN          VARCHAR2
4908  ,p_acra              IN          AR_CASH_RECEIPTS_ALL%ROWTYPE            Default NULL
4909  ,p_araa              IN          AR_RECEIVABLE_APPLICATIONS_ALL%ROWTYPE  Default NULL
4910  ,p_process_flag      OUT NOCOPY VARCHAR2
4911  ,p_process_message   OUT NOCOPY VARCHAR2
4912 )
4913 IS
4914 lv_document_type VARCHAR2(80);
4915 r_acra ar_cash_receipts_all%ROWTYPE;
4916 
4917 CURSOR cur_get_app_cash_receipts(cp_cash_receipt_id NUMBER)
4918 IS
4919 SELECT *
4920 FROM ar_cash_receipts_all
4921 WHERE cash_receipt_id = cp_cash_receipt_id;
4922 
4923 BEGIN
4924 
4925   lv_process_flag := jai_constants.successful;
4926   lv_process_message := NULL;
4927 
4928   p_process_flag := lv_process_flag;
4929   p_process_message := lv_process_message;
4930 
4931   IF p_event  IN ( jai_constants.ar_cash_tax_confirmed , --Changed by Qiong from ar_cash_tax_confirmed to trx_type_adv_rcpts for bug13569249
4932 		   --Reverted from trx_type_adv_rcpts to ar_cash_tax_confirmed by Qiong for bug13730406
4933                    jai_constants.trx_type_rct_rvs
4934                  )
4935   THEN
4936     --Added by Qiong for bug13730406 begin
4937     ----------------------------------------------------
4938     IF p_event = jai_constants.ar_cash_tax_confirmed THEN
4939       lv_document_type := jai_constants.trx_type_adv_rcpts;
4940     /*Added by Qinglei on 24-Feb-2012 for bug#13741544 begin */
4941     ELSIF p_event = jai_constants.trx_type_rct_rvs THEN
4942       lv_document_type := jai_constants.trx_type_adv_rvs;
4943     /*Added by Qinglei on 24-Feb-2012 for bug#13741544 end */
4944     ELSE
4945     ----------------------------------------------------
4946     --Added by Qiong for bug13730406 end
4947       lv_document_type := p_event;
4948     END IF;
4949 
4950     /*
4951     ||Check the receipts if contains service tax or not.
4952     ||If not, will return not_applicaable.
4953     */
4954 
4955     validate_receipts ( p_acra             =>  p_acra             ,
4956                         p_document_type    =>  lv_document_type   ,
4957                         p_process_flag     =>  lv_process_flag    ,
4958                         p_process_message  =>  lv_process_message
4959                       );
4960 
4961     IF lv_process_flag = jai_constants.not_applicable THEN
4962       return;
4963     END IF;
4964 
4965     IF lv_process_flag = jai_constants.expected_error    OR                            ---------C2
4966        lv_process_flag = jai_constants.unexpected_error
4967     THEN
4968       /*
4969       || As Returned status is an error/not applicable hence:-
4970       || Set out variables p_process_flag and p_process_message accordingly
4971       */
4972       p_process_flag    := lv_process_flag    ;
4973       p_process_message := lv_process_message ;
4974       return;
4975     END IF;                                                                            ---------C2
4976     generate_documents(p_acra              => p_acra              ,
4977                        --p_document_type     => p_event             ,
4978                        p_document_type     => lv_document_type    ,
4979                        --Changed from p_event to lv_document_type by Qiong for bug13730406
4980                        p_process_flag      => lv_process_flag     ,
4981                        p_process_message   => lv_process_message
4982                        );
4983 
4984 
4985     IF  lv_process_flag = jai_constants.not_applicable THEN
4986       return;
4987     END IF;
4988 
4989     IF lv_process_flag = jai_constants.expected_error    OR                            ---------C3
4990        lv_process_flag = jai_constants.unexpected_error
4991     THEN
4992       /*
4993       || As Returned status is an error/not applicable hence:-
4994       || Set out variables p_process_flag and p_process_message accordingly
4995       */
4996       p_process_flag    := lv_process_flag    ;
4997       p_process_message := lv_process_message ;
4998       return;
4999     END IF;                                                                            ---------C3
5000   END IF;
5001 
5002   IF p_event = jai_constants.ar_cash THEN
5003      validate_app_unapp(p_araa                     => p_araa,
5004                         p_document_type            => lv_document_type,
5005                         p_process_flag             => lv_process_flag,
5006                         p_process_message          => lv_process_message);
5007      IF lv_process_flag = jai_constants.not_applicable THEN
5008       return;
5009     END IF;
5010 
5011     IF lv_process_flag = jai_constants.expected_error    OR                            ---------C2
5012        lv_process_flag = jai_constants.unexpected_error
5013     THEN
5014       /*
5015       || As Returned status is an error/not applicable hence:-
5016       || Set out variables p_process_flag and p_process_message accordingly
5017       */
5018       p_process_flag    := lv_process_flag    ;
5019       p_process_message := lv_process_message ;
5020       return;
5021     END IF;
5022     OPEN cur_get_app_cash_receipts(p_araa.cash_receipt_id);
5023     FETCH cur_get_app_cash_receipts INTO r_acra;
5024     CLOSE cur_get_app_cash_receipts;
5025                                                                              ---------C2
5026     generate_documents(p_acra              => r_acra,
5027                        p_araa              => p_araa,
5028                        p_document_type     => lv_document_type,
5029                        p_process_flag      => lv_process_flag,
5030                        p_process_message   => lv_process_message
5031                        );
5032 
5033 
5034     IF  lv_process_flag = jai_constants.not_applicable THEN
5035       return;
5036     END IF;
5037 
5038     IF lv_process_flag = jai_constants.expected_error    OR                            ---------C3
5039        lv_process_flag = jai_constants.unexpected_error
5040     THEN
5041       /*
5042       || As Returned status is an error/not applicable hence:-
5043       || Set out variables p_process_flag and p_process_message accordingly
5044       */
5045       p_process_flag    := lv_process_flag    ;
5046       p_process_message := lv_process_message ;
5047       return;
5048     END IF;
5049   END IF;
5050 END process_st_advance_receipts;
5051 
5052 --========================================================================
5053 -- PROCEDURE : generate_docuements       PUBLIC
5054 -- PARAMETERS: p_acra             rowtype of ar_cash_receipts_all
5055 --           : p_araa             rowtype of ar_receivable_applications_all
5056 --           : p_document_type
5057 --           : p_process_flag     the process result
5058 --           : p_process_message  the process message
5059 -- COMMENT   : generate documents accroding to document type and inclusive
5060 --             or exclusive tax
5061 -- PRE-COND  : AR Receipt must attach service tax
5062 -- EXCEPTIONS:
5063 --
5064 --========================================================================
5065 PROCEDURE generate_documents
5066 ( p_acra             IN         AR_CASH_RECEIPTS_ALL%ROWTYPE
5067  ,p_araa             IN         AR_RECEIVABLE_APPLICATIONS_ALL%ROWTYPE  Default Null
5068  ,p_document_type    IN         VARCHAR2
5069  ,p_process_flag     OUT NOCOPY VARCHAR2
5070  ,p_process_message  OUT NOCOPY VARCHAR2
5071 )
5072 IS
5073 /*****
5074 || Get the details of the cash_receipts
5075 *****/
5076 CURSOR cur_get_cr_details ( cp_cash_receipt_id JAI_AR_CASH_RECEIPTS_ALL.CASH_RECEIPT_ID%TYPE )
5077 IS
5078 SELECT  *
5079 FROM
5080         jai_ar_cash_receipts_all    jcra
5081 WHERE
5082         jcra.cash_receipt_id      = cp_cash_receipt_id ;
5083 
5084 /*****
5085 ||Get the receipt tax details
5086 *****/
5087 CURSOR cur_get_rcpt_taxes (cp_inclusive_tax_flag VARCHAR2)
5088 IS
5089 SELECT
5090        jtc.tax_type                       ,
5091        jdt.tax_amt   tax_amount           ,
5092        jdt.func_tax_amt
5093 FROM
5094        jai_cmn_document_taxes     jdt     ,
5095        JAI_CMN_TAXES_ALL            jtc     ,
5096        jai_regime_tax_types_v     jrttv
5097 WHERE
5098        jdt.tax_id        = jtc.tax_id
5099 AND    jtc.tax_type      = jrttv.tax_type
5100 AND    jdt.source_doc_id = p_acra.cash_receipt_id
5101 AND    jrttv.regime_code = jai_constants.service_regime
5102 AND    nvl(jtc.INCLUSIVE_TAX_FLAG,'N') = cp_inclusive_tax_flag
5103 AND    jdt.source_doc_type  = JAI_CONSTANTS.ar_cash;
5104 
5105 CURSOR c_get_rgm_attribute (   cp_regime_code           JAI_RGM_DEFINITIONS.REGIME_CODE%TYPE               ,
5106                                cp_attribute_code        JAI_RGM_REGISTRATIONS.ATTRIBUTE_CODE%TYPE          ,
5107                                cp_organization_id       JAI_RGM_PARTIES.ORGANIZATION_ID%TYPE               ,
5108                                cp_location_id           JAI_RGM_PARTIES.LOCATION_ID%TYPE
5109                              )
5110 IS
5111 SELECT
5112        regime_id                   ,
5113        attribute_value org_tan_no
5114 FROM
5115        JAI_RGM_ORG_REGNS_V rgm_attr_v
5116 WHERE
5117        rgm_attr_v.regime_code         =   cp_regime_code
5118 AND    rgm_attr_v.attribute_code      =   cp_attribute_code
5119 AND    rgm_attr_v.organization_id     =   cp_organization_id
5120 AND    rgm_attr_v.location_id         =   cp_location_id ;
5121 
5122 /*
5123 ||Customer would create a batch source with name Service Debit Memo And Service Credit Memo
5124 */
5125 CURSOR cur_get_batch_source ( cp_org_id  NUMBER ,
5126                               cp_name    RA_BATCH_SOURCES_ALL.NAME%TYPE
5127                              )
5128 IS
5129 SELECT
5130        bsa.batch_source_id          ,
5131        bsa.default_inv_trx_type     ,
5132        rctt.type                    ,
5133        rctt.name                    ,
5134        rctt.default_term            ,
5135        rctt.gl_id_rec               ,
5136        rctt.creation_sign
5137 FROM
5138        ra_batch_sources_all   bsa ,
5139        ra_cust_trx_types_all  rctt
5140 WHERE
5141        bsa.default_inv_trx_type = rctt.cust_trx_type_id
5142 AND    bsa.org_id               = rctt.org_id
5143 AND    bsa.org_id               = cp_org_id
5144 AND    bsa.name                 = cp_name  ;
5145 
5146 CURSOR cur_get_part_det ( cp_party_id         NUMBER    ,
5147                           cp_party_site_id    NUMBER
5148                         )
5149 IS
5150 SELECT
5151         hzcas.cust_acct_site_id   bill_to_address_id
5152 FROM
5153         hz_cust_accounts hca         ,
5154         hz_cust_acct_sites_all hzcas ,
5155         hz_cust_site_uses_all  hzcsu
5156 WHERE
5157         hca.cust_account_id       = hzcas.cust_account_id
5158 AND     hzcas.cust_acct_site_id   = hzcsu.cust_acct_site_id
5159 AND     hzcsu.site_use_code       = jai_constants.site_use_bill_to
5160 AND     hca.cust_account_id       = cp_party_id
5161 AND     hzcsu.site_use_id         = cp_party_site_id ;-- site_use_id is the party_site_id ;
5162 
5163 CURSOR cur_get_sob ( cp_org_id NUMBER )
5164 IS
5165 SELECT
5166       set_of_books_id
5167 FROM
5168       hr_operating_units
5169 WHERE
5170       organization_id  = cp_org_id;
5171 
5172 
5173 /****
5174 || Get a sequence number for transaction id
5175 || for the table jai_rgm_trx_refs
5176 *****/
5177 CURSOR cur_get_trx_ref_id
5178 IS
5179 SELECT
5180         jai_rgm_trx_refs_s.nextval
5181 FROM
5182         dual;
5183 
5184 lv_member_name                VARCHAR2(80);
5185 lv_batch_src_dm               JAI_RGM_REGISTRATIONS.ATTRIBUTE_VALUE%TYPE;
5186 lv_batch_src_cm               JAI_RGM_REGISTRATIONS.ATTRIBUTE_VALUE%TYPE;
5187 ln_regime_id                  JAI_RGM_DEFINITIONS.REGIME_ID%TYPE;
5188 lv_batch_src_name             JAI_RGM_REGISTRATIONS.ATTRIBUTE_VALUE%TYPE;
5189 ln_term_id                    RA_CUST_TRX_TYPES_ALL.DEFAULT_TERM%TYPE;
5190 ln_bill_to_address_id         hz_cust_acct_sites_all.cust_acct_site_id%TYPE;
5191 lv_trx_number                 RA_CUSTOMER_TRX_ALL.TRX_NUMBER%TYPE;
5192 lv_trx_number_prefix          RA_CUSTOMER_TRX_ALL.TRX_NUMBER%TYPE;
5193 ln_ccid_tax_type              NUMBER;
5194 rec_cur_get_batch_source      CUR_GET_BATCH_SOURCE%ROWTYPE;
5195 lv_set_of_books_id            HR_OPERATING_UNITS.SET_OF_BOOKS_ID%TYPE;
5196 ln_amount                     NUMBER;
5197 ln_user_id                    ra_interface_lines_all.CREATED_BY%TYPE;
5198 ln_login_id                   ra_interface_lines_all.LAST_UPDATE_LOGIN%TYPE;
5199 lv_process_message            VARCHAR2(4000);
5200 
5201 rec_cur_get_cr_details        JAI_AR_CASH_RECEIPTS_ALL%ROWTYPE;
5202 rec_cur_get_rcpt_taxes        cur_get_rcpt_taxes%ROWTYPE;
5203 rec_ra_interface_lines        ra_interface_lines_all%ROWTYPE;
5204 rec_ra_interface_dist         ra_interface_distributions_all%ROWTYPE;
5205 ln_rcpt_amount                NUMBER;
5206 ln_rcpt_tax_amount            NUMBER;
5207 ln_rcpt_tran_tax_amount       NUMBER;
5208 ln_org_id                     NUMBER;
5209 ln_transaction_id             NUMBER;
5210 ln_organization_id            NUMBER;
5211 ln_location_id                NUMBER;
5212 ld_trx_date                   DATE;
5213 lv_called_from                VARCHAR2(80);
5214 lv_repository_name            VARCHAR2(80);
5215 ln_receipt_unapplied_account  NUMBER;
5216 ln_app_amount                 NUMBER;
5217 ln_tax_app_amount             NUMBER;
5218 ln_converted_rate             NUMBER DEFAULT 1;
5219 BEGIN
5220   /*################################################################################################################
5221   || Initialize the variables
5222   ################################################################################################################*/
5223 
5224   lv_member_name        := 'GENERATE_DOCUMENT';
5225 
5226 
5227   lv_process_message:= null                       ;
5228   p_process_flag    := jai_constants.successful   ;
5229   p_process_message := lv_process_message         ;
5230 
5231   ln_user_id         := fnd_global.user_id         ;
5232   ln_login_id        := fnd_global.login_id        ;
5233   ln_rcpt_amount     := p_acra.amount ;
5234   /*################################################################################################################
5235   ||Skip the transaction if ln_rcpt_amount is 0
5236   ################################################################################################################*/
5237   IF nvl(ln_rcpt_amount,0) = 0 THEN
5238     p_process_flag := jai_constants.not_applicable ;
5239     return;
5240   END IF;
5241 
5242   OPEN  cur_get_cr_details ( cp_cash_receipt_id => p_acra.cash_receipt_id );
5243   FETCH cur_get_cr_details INTO rec_cur_get_cr_details;
5244   CLOSE cur_get_cr_details;
5245 
5246   ln_organization_id  :=   rec_cur_get_cr_details.organization_id;
5247   ln_location_id      :=   rec_cur_get_cr_details.location_id;
5248   ln_org_id           :=   rec_cur_get_cr_details.org_id;
5249 
5250 
5251 
5252   /*################################################################################################################
5253   ||Get batch source information
5254   ################################################################################################################*/
5255 
5256    /*
5257    ||Get the value for the Batch Source Debit Memo
5258    */
5259    OPEN c_get_rgm_attribute (  cp_regime_code           =>   jai_constants.service_regime                ,
5260                                cp_attribute_code        =>   jai_constants.batch_src_dm                  ,
5261                                cp_organization_id       =>   ln_organization_id                          ,
5262                                cp_location_id           =>   ln_location_id
5263                             );
5264   FETCH c_get_rgm_attribute INTO ln_regime_id ,lv_batch_src_dm;
5265   CLOSE c_get_rgm_attribute ;
5266 
5267 
5268   /*
5269   ||Get the value for the Batch Source Credit Memo
5270   */
5271   OPEN c_get_rgm_attribute (  cp_regime_code           =>   jai_constants.service_regime                    ,
5272                               cp_attribute_code        =>   jai_constants.batch_src_cm                  ,
5273                               cp_organization_id       =>   ln_organization_id                          ,
5274                               cp_location_id           =>   ln_location_id
5275                            );
5276   FETCH c_get_rgm_attribute INTO ln_regime_id ,lv_batch_src_cm;
5277   CLOSE c_get_rgm_attribute ;
5278 
5279 
5280   /*################################################################################################################
5281   || Derive the batch source name based on the document type
5282   ################################################################################################################*/
5283 
5284   IF p_document_type IN ( --jai_constants.ar_cash_tax_confirmed , /* Receipt confirmation */
5285                           --Changed by Qiong from ar_cash_tax_confirmed to trx_type_adv_rcpts for bug13569249
5286                           jai_constants.trx_type_adv_rcpts ,
5287                           jai_constants.trx_type_adv_unapp     /* Receipt unapplication*/
5288                          )
5289   THEN
5290     lv_batch_src_name           := lv_batch_src_dm; /* Service Debit Memo */
5291     lv_trx_number_prefix        := 'Service-DM';
5292   ELSIF p_document_type IN ( jai_constants.trx_type_adv_app,
5293                              jai_constants.trx_type_adv_rvs--Modified by Qinglei on 24-Dec-2012 for bug#13741544
5294                            )
5295 
5296   THEN
5297     lv_batch_src_name           := lv_batch_src_cm; /* Service Credit Memo */
5298     lv_trx_number_prefix        := 'Service-CM';
5299   ELSE
5300     /*
5301     ||Skip the transaction
5302     */
5303     p_process_flag    := jai_constants.not_applicable;
5304     p_process_message := null;
5305     return ;
5306   END IF;
5307 
5308   /*################################################################################################################
5309   ||VALIDATE BATCH SOURCES FOR SERVICE
5310   ################################################################################################################*/
5311 
5312   /*
5313   || Error out if the batch source name is null i.e regime party setup for
5314   */
5315   IF lv_batch_src_name IS NULL THEN
5316     p_process_flag    := jai_constants.expected_error;
5317     lv_process_message := 'Batch source definition has not be defined for the inventory organization '||ln_organization_id;
5318     p_process_message := lv_process_message ;
5319     return;
5320   END IF;
5321 
5322   OPEN cur_get_batch_source  (  cp_org_id  => ln_org_id,
5323                                 cp_name    => lv_batch_src_name
5324                              );
5325 
5326   FETCH cur_get_batch_source INTO rec_cur_get_batch_source;
5327   IF cur_get_batch_source%NOTFOUND THEN
5328 
5329     CLOSE cur_get_batch_source  ;
5330     p_process_flag    := jai_constants.expected_error;
5331     lv_process_message := 'Service batch source has not been defined for '||lv_batch_src_name ||'. Cannot process transaction ';
5332     p_process_message := lv_process_message ;
5333     return;
5334   END IF;
5335   CLOSE cur_get_batch_source  ;
5336 
5337 
5338   /*################################################################################################################
5339   || DERIVE THE TERM FOR DM'S ONLY
5340   ################################################################################################################*/
5341 
5342   IF rec_cur_get_batch_source.type = jai_constants.ar_doc_type_dm THEN
5343     ln_term_id := rec_cur_get_batch_source.default_term;
5344     /*
5345     || Throw an error if the term has not been defined for the debit memo Transaction type .
5346     || This check is not required in case of credit memo
5347     */
5348     IF ln_term_id IS NULL  THEN
5349       p_process_flag     := jai_constants.expected_error;
5350       lv_process_message := 'Cannot process transaction. A default term needs to be defined FOR the Transaction TYPE '||rec_cur_get_batch_source.name ;
5351       p_process_message  := lv_process_message ;
5352       return;
5353     END IF;
5354   END IF;
5355 
5356   /*################################################################################################################
5357   || DERIVE THE SOB
5358   ################################################################################################################*/
5359 
5360   OPEN  cur_get_sob ( cp_org_id => ln_org_id );
5361   FETCH cur_get_sob INTO lv_set_of_books_id;
5362   CLOSE cur_get_sob ;
5363   /*
5364   ||Throw an error if the Set of books has not been defined
5365   */
5366   IF lv_set_of_books_id IS NULL THEN
5367     p_process_flag     := jai_constants.expected_error;
5368     lv_process_message := 'Set of books not defined for the org id.';
5369     p_process_message  := lv_process_message ;
5370     return;
5371   END IF;
5372 
5373 
5374   /*################################################################################################################
5375   || DERIVE THE ADDRESS
5376   ################################################################################################################*/
5377 
5378   OPEN  cur_get_part_det ( cp_party_id      =>   rec_cur_get_cr_details.customer_id,
5379                            cp_party_site_id =>   p_acra.customer_site_use_id
5380                            );
5381   FETCH cur_get_part_det INTO ln_bill_to_address_id;
5382   CLOSE cur_get_part_det ;
5383   /*
5384   ||Throw an error if the bill to address has not been defined
5385   */
5386 
5387   IF ln_bill_to_address_id IS NULL THEN
5388     p_process_flag     := jai_constants.expected_error;
5389     lv_process_message := 'Bill to address not defined for the customer id -> '||rec_cur_get_cr_details.customer_id ||' customer site -> '||p_acra.CUSTOMER_SITE_USE_ID;
5390     p_process_message  := lv_process_message ;
5391     return;
5392   END IF;
5393 
5394   ln_converted_rate := jai_cmn_utils_pkg.currency_conversion(lv_set_of_books_id,
5395                                           p_acra.currency_code,
5396                                           p_acra.exchange_date,
5397                                           p_acra.exchange_rate_type,
5398                                           p_acra.exchange_rate);
5399 
5400     /*################################################################################################################
5401     || Generate DM/CM  for exclusive taxes
5402     ################################################################################################################*/
5403      FOR rec_cur_get_rcpt_taxes IN cur_get_rcpt_taxes(cp_inclusive_tax_flag => 'N')
5404      LOOP
5405        IF p_document_type IN ( --jai_constants.ar_cash_tax_confirmed , /* Receipt confirmation */
5406                                --Changed by Qiong from ar_cash_tax_confirmed to trx_type_adv_rcpts for bug13569249
5407                                jai_constants.trx_type_adv_rcpts ,
5408                                jai_constants.trx_type_adv_rvs /* Receipt reversal */
5409                                --Modified by Qinglei on 24-Feb-2012 for bug#13741544
5410                                       ) THEN
5411          ln_rcpt_tax_amount := rec_cur_get_rcpt_taxes.func_tax_amt;
5412          --ld_trx_date    := rec_cur_get_cr_details.gl_date;
5413          /*Commented by Qinglei on 12-Jan-2012 for bug#13557031*/
5414          lv_trx_number := lv_trx_number_prefix||p_acra.CASH_RECEIPT_ID ;
5415 
5416          /*Added by Qinglei on 12-Jan-2012 for bug#13557031 begin*/
5417          --IF p_document_type = jai_constants.ar_cash_tax_confirmed THEN
5418          --Changed by Qiong from ar_cash_tax_confirmed to trx_type_adv_rcpts for bug13569249
5419          IF p_document_type = jai_constants.trx_type_adv_rcpts THEN
5420            ld_trx_date := p_acra.receipt_date;
5421          ELSIF p_document_type = jai_constants.trx_type_adv_rvs THEN
5422          --Modified by Qinglei on 24-Feb-2012 for bug#13741544
5423            ld_trx_date := p_acra.reversal_date;
5424          END IF;
5425          /*Added by Qinglei on 12-Jan-2012 for bug#13557031 end*/
5426        ELSIF p_document_type     IN  ( jai_constants.trx_type_adv_app , /* Receipt application */
5427                                        jai_constants.trx_type_adv_unapp /* Receipt unapplication */
5428                                       ) THEN
5429          /*
5430              For receipt application and un-application event, the amount generated for gl entry is determined by
5431                the allocated receipt amount.
5432            */
5433          lv_trx_number := lv_trx_number_prefix||p_araa.RECEIVABLE_APPLICATION_ID;
5434          ln_rcpt_tax_amount  := round(rec_cur_get_rcpt_taxes.func_tax_amt *
5435                                 (nvl(p_araa.ACCTD_AMOUNT_APPLIED_FROM,0)/
5436                                 (p_acra.AMOUNT * NVL(ln_converted_rate,1))),jai_constants.service_rgm_rnd_factor);
5437                                 /*Added by Qinglei on 12-Jan-2012 for bug#13557031*/
5438          ld_trx_date     := p_araa.apply_date;
5439 
5440        END IF;
5441        OPEN cur_get_trx_ref_id;
5442        FETCH cur_get_trx_ref_id INTO ln_transaction_id;
5443        CLOSE cur_get_trx_ref_id;
5444 
5445       /*################################################################################################################
5446       || DERIVE THE SIGN OF THE APPLICATION AND SECONDARY DOCUMENT VALUE
5447       ################################################################################################################*/
5448 
5449       /*
5450       ||Amount is :-
5451       ||  1.+ve if the creation sign of the document is positive
5452       ||  1.-ve if the creation sign of the document is -ve
5453       || If the sign is any sign then for a DM create a +ve amount and CM would ve created with a -ve amount
5454       */
5455       IF rec_cur_get_batch_source.creation_sign = jai_constants.creation_sign_positive THEN
5456         ln_amount := abs(ln_rcpt_tax_amount) * 1;
5457 
5458       ELSIF rec_cur_get_batch_source.creation_sign = jai_constants.creation_sign_negative    THEN
5459           ln_amount := abs(ln_rcpt_tax_amount) * -1;
5460 
5461       ELSIF rec_cur_get_batch_source.creation_sign = jai_constants.creation_sign_any THEN
5462 
5463         IF rec_cur_get_batch_source.type = jai_constants.ar_doc_type_dm THEN
5464           ln_amount := abs(ln_rcpt_tax_amount) ;
5465 
5466         ELSIF rec_cur_get_batch_source.type = jai_constants.ar_invoice_type_cm THEN
5467           ln_amount := abs(ln_rcpt_tax_amount) * -1;
5468         END IF;
5469       END IF;
5470 
5471 
5472       /*################################################################################################################
5473       || Generate DM/CM
5474       ################################################################################################################*/
5475       rec_ra_interface_lines.interface_line_id                         :=  ln_transaction_id;
5476       rec_ra_interface_lines.amount                                    :=  ln_amount;
5477       rec_ra_interface_lines.description                               :=  lv_batch_src_name;
5478       rec_ra_interface_lines.orig_system_bill_customer_id              :=  rec_cur_get_cr_details.customer_id;
5479       rec_ra_interface_lines.orig_system_bill_address_id               :=  ln_bill_to_address_id;
5480       rec_ra_interface_lines.set_of_books_id                           :=  lv_set_of_books_id;
5481       rec_ra_interface_lines.trx_date                                  :=  ld_trx_date;
5482       rec_ra_interface_lines.trx_number                                :=  lv_trx_number;
5483       rec_ra_interface_lines.batch_source_name                         :=  lv_batch_src_name;
5484       rec_ra_interface_lines.cust_trx_type_name                        :=  rec_cur_get_batch_source.name;
5485       rec_ra_interface_lines.line_type                                 :=  jai_constants.line_type_line;
5486       rec_ra_interface_lines.conversion_rate                           :=  1;
5487       rec_ra_interface_lines.conversion_type                           :=  jai_constants.conversion_type_user;
5488       rec_ra_interface_lines.interface_line_context                    :=  lv_batch_src_name;
5489       rec_ra_interface_lines.interface_line_attribute2                 :=  ln_transaction_id;
5490       rec_ra_interface_lines.currency_code                             :=  jai_constants.func_curr;
5491       rec_ra_interface_lines.primary_salesrep_id                       :=  -3;
5492       rec_ra_interface_lines.tax_code                                  :=  NULL;
5493       rec_ra_interface_lines.term_id                                   :=  ln_term_id;
5494       rec_ra_interface_lines.warehouse_id                              :=  ln_organization_id;
5495       rec_ra_interface_lines.org_id                                    :=  ln_org_id;
5496       rec_ra_interface_lines.quantity                                  :=  1;
5497       rec_ra_interface_lines.unit_selling_price                        :=  ln_amount;
5498       rec_ra_interface_lines.created_by                                :=  ln_user_id;
5499       rec_ra_interface_lines.creation_date                             :=  SYSDATE;
5500       rec_ra_interface_lines.last_updated_by                           :=  ln_user_id;
5501       rec_ra_interface_lines.last_update_date                          :=  SYSDATE;
5502       rec_ra_interface_lines.last_update_login                         :=  ln_login_id;
5503 
5504 
5505       rec_ra_interface_dist.interface_line_id                   := ln_transaction_id;
5506       rec_ra_interface_dist.interface_line_context              := lv_batch_src_name;
5507       rec_ra_interface_dist.interface_line_attribute2           := ln_transaction_id;
5508       rec_ra_interface_dist.account_class                       := jai_constants.account_class_rev;
5509       rec_ra_interface_dist.amount                              := ln_amount;
5510       rec_ra_interface_dist.acctd_amount                        := ln_amount;
5511       rec_ra_interface_dist.created_by                          := ln_user_id;
5512       rec_ra_interface_dist.creation_date                       := SYSDATE;
5513       rec_ra_interface_dist.last_updated_by                     := ln_user_id;
5514       rec_ra_interface_dist.last_update_date                    := SYSDATE;
5515       rec_ra_interface_dist.last_update_login                   := ln_login_id;
5516       rec_ra_interface_dist.org_id                              := ln_org_id;
5517 
5518       generate_cm_dm(    p_rila             => rec_ra_interface_lines,
5519                          p_rida             => rec_ra_interface_dist,
5520                          pn_regime_id       => ln_regime_id,
5521                          pn_organization_id => ln_organization_id,
5522                          pn_location_id     => ln_location_id,
5523                          pv_tax_type        => rec_cur_get_rcpt_taxes.tax_type,
5524                          p_process_flag     =>  lv_process_flag,
5525                          p_process_message  =>  lv_process_message
5526                      );
5527     END LOOP;
5528 
5529     /*################################################################################################################
5530     || Insert GL interface for inclusive taxes
5531     ################################################################################################################*/
5532     FOR rec_cur_get_rcpt_taxes IN cur_get_rcpt_taxes(cp_inclusive_tax_flag => 'Y')
5533     LOOP
5534       IF p_document_type       IN  ( --jai_constants.ar_cash_tax_confirmed , /* Receipt confirmation */
5535                                      --Changed by Qiong from ar_cash_tax_confirmed to trx_type_adv_rcpts for bug13569249
5536                                      jai_constants.trx_type_adv_rcpts,
5537                                      jai_constants.trx_type_adv_rvs /* Receipt reversal */
5538                                       --Modified by Qinglei on 24-Feb-2012 for bug#13741544
5539                                       ) THEN
5540         ln_rcpt_tax_amount := rec_cur_get_rcpt_taxes.func_tax_amt;
5541         ln_rcpt_tran_tax_amount := rec_cur_get_rcpt_taxes.tax_amount;
5542 
5543       ELSIF p_document_type     IN  ( jai_constants.trx_type_adv_app , /* Receipt application */
5544                                        jai_constants.trx_type_adv_unapp /* Receipt unapplication */
5545                                       ) THEN
5546            /*
5547              For receipt application and un-application event, the amount generated for gl entry is determined by
5548                the allocated receipt amount.
5549            */
5550            ln_rcpt_tax_amount := rec_cur_get_rcpt_taxes.func_tax_amt *
5551                                  (nvl(p_araa.ACCTD_AMOUNT_APPLIED_FROM,0) / (p_acra.AMOUNT * NVL(ln_converted_rate,1)));
5552            ln_rcpt_tran_tax_amount := rec_cur_get_rcpt_taxes.tax_amount *
5553                                  (nvl(p_araa.ACCTD_AMOUNT_APPLIED_FROM,0) / (p_acra.AMOUNT * NVL(ln_converted_rate,1)));
5554 
5555       END IF;
5556       generate_gl_entry( p_jacr              => rec_cur_get_cr_details,
5557                          p_acra              => p_acra,
5558                          p_araa              => p_araa,
5559                          p_document_type     => p_document_type,
5560                          pn_regime_id        => ln_regime_id,
5561                          pn_organization_id  => ln_organization_id,
5562                          pn_location_id      => ln_location_id,
5563                          pv_tax_type         => rec_cur_get_rcpt_taxes.tax_type,
5564                          pn_tran_tax_amount  => ln_rcpt_tran_tax_amount,
5565                          pn_func_tax_amount  => ln_rcpt_tax_amount,
5566                          p_process_flag      =>  lv_process_flag,
5567                          p_process_message   =>  lv_process_message
5568                         );
5569     END LOOP;
5570 
5571 EXCEPTION
5572 WHEN OTHERS THEN
5573     p_process_flag    := jai_constants.unexpected_error;
5574     p_process_message := ' Unexpected error occured while processing jai_ar_rgm_processing_pkg.generate_documents'||substr(SQLERRM,1,1500) ;
5575 END generate_documents;
5576 
5577 --========================================================================
5578 -- PROCEDURE : generate_docuements       PUBLIC
5579 -- PARAMETERS: p_rila             rowtype of ra_interface_lines_all
5580 --           : p_rida             rowtype of ra_interface_distributions_all
5581 --           : pn_regime_id
5582 --           : pn_organization_id
5583 --           : pn_location_id
5584 --           : pv_tax_type
5585 --           : p_process_flag     the process result
5586 --           : p_process_message  the process message
5587 -- COMMENT   : generate CM or DM for advance receipt
5588 -- PRE-COND  : AR Receipt must attach service tax
5589 -- EXCEPTIONS:
5590 --
5591 --========================================================================
5592 PROCEDURE generate_cm_dm
5593 ( p_rila             IN ra_interface_lines_all%ROWTYPE
5594  ,p_rida             IN ra_interface_distributions_all%ROWTYPE
5595  ,pn_regime_id       IN  NUMBER
5596  ,pn_organization_id IN  NUMBER
5597  ,pn_location_id     IN  NUMBER
5598  ,pv_tax_type        IN  VARCHAR2
5599  ,p_process_flag     OUT NOCOPY  VARCHAR2
5600  ,p_process_message  OUT NOCOPY  VARCHAR2
5601 )
5602 IS
5603 
5604 ln_ccid_tax_type     NUMBER;
5605 lv_process_flag      VARCHAR2(2);
5606 lv_process_message   VARCHAR2(4000);
5607 
5608 BEGIN
5609 
5610   lv_process_flag       := jai_constants.successful;
5611   lv_process_message    := null;
5612 
5613   p_process_flag        := lv_process_flag;
5614   p_process_message     := lv_process_message;
5615 
5616   /*################################################################################################################
5617   || INSERT INTO RA_INTERFACE_LINES_ALL TABLE
5618   ################################################################################################################*/
5619   INSERT INTO ra_interface_lines_all
5620               (
5621                   interface_line_id,
5622                   amount,
5623                   description,
5624                   orig_system_bill_customer_id,
5625                   orig_system_bill_address_id,
5626                   set_of_books_id,
5627                   trx_date,
5628                   trx_number,
5629                   batch_source_name,
5630                   cust_trx_type_name,
5631                   line_type,
5632                   conversion_rate,
5633                   conversion_type,
5634                   interface_line_context,
5635                   interface_line_attribute2,
5636                   currency_code,
5637                   primary_salesrep_id,
5638                   tax_code,
5639                   term_id,
5640                   warehouse_id,
5641                   org_id,
5642                   quantity,
5643                   unit_selling_price,
5644                   created_by,
5645                   creation_date,
5646                   last_updated_by,
5647                   last_update_date,
5648                   last_update_login
5649               )
5650       VALUES  (
5651                   p_rila.interface_line_id,
5652                   p_rila.amount,
5653                   p_rila.description,
5654                   p_rila.orig_system_bill_customer_id,
5655                   p_rila.orig_system_bill_address_id,
5656                   p_rila.set_of_books_id,
5657                   p_rila.trx_date,
5658                   p_rila.trx_number,
5659                   p_rila.batch_source_name,
5660                   p_rila.cust_trx_type_name,
5661                   p_rila.line_type,
5662                   p_rila.conversion_rate,
5663                   p_rila.conversion_type,
5664                   p_rila.interface_line_context,
5665                   p_rila.interface_line_attribute2,
5666                   p_rila.currency_code,
5667                   p_rila.primary_salesrep_id,
5668                   p_rila.tax_code,
5669                   p_rila.term_id,
5670                   p_rila.warehouse_id,
5671                   p_rila.org_id,
5672                   p_rila.quantity,
5673                   p_rila.unit_selling_price,
5674                   p_rila.created_by,
5675                   p_rila.creation_date,
5676                   p_rila.last_updated_by,
5677                   p_rila.last_update_date,
5678                   p_rila.last_update_login
5679                );
5680 
5681 
5682 
5683   /*********************************************************************************************************
5684   || Get the code combination id from the Organization/Regime Registration setup
5685   || by calling the function jai_cmn_rgm_recording_pkg.get_account
5686   *********************************************************************************************************/
5687 
5688   ln_ccid_tax_type := jai_cmn_rgm_recording_pkg.get_account (
5689                                                                 p_regime_id          => pn_regime_id,
5690                                                                 p_organization_type  => jai_constants.orgn_type_io,
5691                                                                 p_organization_id    => pn_organization_id,
5692                                                                 p_location_id        => pn_location_id,
5693                                                                 p_tax_type           => pv_tax_type,
5694                                                                 p_account_name       => jai_constants.liability
5695                                                               );
5696   IF ln_ccid_tax_type IS NULL THEN
5697   /**********************************************************************************************************
5698   || Code Combination id has been returned as null from the function jai_cmn_rgm_recording_pkg.get_account
5699   || This is an error condition and the current processing has to be stopped
5700   **********************************************************************************************************/
5701 
5702   p_process_flag := jai_constants.expected_error;
5703   lv_process_message  := 'Invalid Code combination ,please check the Service Tax - Tax Accounting Setup';
5704   p_process_message := lv_process_message ;
5705   rollback;
5706   return;
5707   END IF;
5708 
5709 
5710   INSERT INTO ra_interface_distributions_all
5711            (
5712               interface_line_id,
5713               interface_line_context,
5714               interface_line_attribute2,
5715               account_class,
5716               amount,
5717               code_combination_id,
5718               acctd_amount,
5719               created_by,
5720               creation_date,
5721               last_updated_by,
5722               last_update_date,
5723               last_update_login,
5724               org_id
5725            )
5726      VALUES(
5727               p_rida.interface_line_id,
5728               p_rida.interface_line_context,
5729               p_rida.interface_line_attribute2,
5730               p_rida.account_class,
5731               p_rida.amount,
5732               ln_ccid_tax_type,
5733               p_rida.acctd_amount,
5734               p_rida.created_by,
5735               p_rida.creation_date,
5736               p_rida.last_updated_by,
5737               p_rida.last_update_date,
5738               p_rida.last_update_login,
5739               p_rida.org_id
5740           );
5741 END generate_cm_dm;
5742 
5743 --========================================================================
5744 -- PROCEDURE : generate_gl_entry       PUBLIC
5745 -- PARAMETERS: p_jacr               rowtype of jai_ar_cash_receipts_all
5746 --           : p_acra               rowtype of ar_cash_receipts_all
5747 --           : p_araa               rowtype of ar_receivable_applications_all
5748 --           : p_document_type
5749 --           : p_regime_id
5750 --           : pn_organization_id
5751 --           : pn_location_id
5752 --           : pv_tax_type
5753 --           : pn_tran_tax_amount   transactional tax amount
5754 --           : pn_func_tax_amount   funcational tax amount
5755 --           : p_process_flag       the process result
5756 --           : p_process_message    the process message
5757 -- COMMENT   : generate gl entry for advance receipt
5758 -- PRE-COND  : AR Receipt must attach service tax
5759 -- EXCEPTIONS:
5760 --
5761 --========================================================================
5762 PROCEDURE generate_gl_entry
5763 ( p_jacr              IN          JAI_AR_CASH_RECEIPTS_ALL%ROWTYPE
5764  ,p_acra              IN          AR_CASH_RECEIPTS_ALL%ROWTYPE
5765  ,p_araa              IN          AR_RECEIVABLE_APPLICATIONS_ALL%ROWTYPE DEFAULT NULL
5766  ,p_document_type     IN          VARCHAR2
5767  ,pn_regime_id        IN          NUMBER
5768  ,pn_organization_id  IN          NUMBER
5769  ,pn_location_id      IN          NUMBER
5770  ,pv_tax_type         IN          VARCHAR2
5771  ,pn_tran_tax_amount  IN          NUMBER
5772  ,pn_func_tax_amount  IN          NUMBER
5773  ,p_process_flag      OUT NOCOPY  VARCHAR2
5774  ,p_process_message   OUT NOCOPY  VARCHAR2
5775  )
5776 IS
5777 /****
5778 || Get unapplied account
5779 || for advanced receipt
5780 *****/
5781 /*Added by Qinglei on 12-Jan-2012 for bug#13569242 begin*/
5782 CURSOR cur_get_rcpt_reversal_gl_date
5783 IS
5784 SELECT gl_date
5785 FROM ar_cash_receipt_history_all
5786 WHERE cash_receipt_id = p_acra.cash_receipt_id
5787 AND status = 'REVERSED';
5788 /*Added by Qinglei on 12-Jan-2012 for bug#13569242 end*/
5789 CURSOR cur_get_unapplied_account
5790 IS
5791 SELECT unapplied_ccid
5792 FROM AR_RECEIPT_METHOD_ACCOUNTS_all
5793 WHERE receipt_method_id = p_acra.receipt_method_id
5794 AND remit_bank_acct_use_id = p_acra.remit_bank_acct_use_id;
5795 
5796 lv_called_from              VARCHAR2(80);
5797 lv_tax_type                 VARCHAR2(80);
5798 ln_entered_amt              NUMBER;
5799 ln_accounted_amt            NUMBER;
5800 ln_tax_type_ccid            NUMBER;
5801 ln_receipt_unapplied_account NUMBER;
5802 ln_dr_account               NUMBER;
5803 ln_cr_account               NUMBER;
5804 lv_repository_name          VARCHAR2(80);
5805 lv_call_from                VARCHAR2(80);
5806 lv_source_table_name        VARCHAR2(80);
5807 ln_source_document_id       NUMBER;
5808 ld_transaction_date         DATE;
5809 ld_gl_date                  DATE;
5810 lv_reference_name           VARCHAR2(80);
5811 ln_reference_id             NUMBER;
5812 lv_currency_code            VARCHAR2(30);
5813 ld_curr_conv_date           DATE;
5814 lv_curr_conv_type           VARCHAR2(30);
5815 ln_curr_conv_rate           NUMBER;
5816 lv_process_flag             VARCHAR2(2);
5817 lv_process_message          VARCHAR2(4000);
5818 lv_attribute1               VARCHAR2(240);
5819 lv_attribute2               VARCHAR2(240);
5820 
5821 BEGIN
5822   lv_process_flag       := jai_constants.successful;
5823   lv_process_message    := null;
5824 
5825   p_process_flag        := lv_process_flag;
5826   p_process_message     := lv_process_message;
5827   lv_tax_type := pv_tax_type;
5828   ln_entered_amt   := round(abs(pn_tran_tax_amount), jai_constants.service_rgm_rnd_factor);
5829   ln_accounted_amt := round(abs(pn_func_tax_amount), jai_constants.service_rgm_rnd_factor);
5830 
5831   ln_tax_type_ccid := jai_cmn_rgm_recording_pkg.get_account(
5832                                                             p_regime_id          => pn_regime_id,
5833                                                             p_organization_type  => jai_constants.orgn_type_io,
5834                                                             p_organization_id    => pn_organization_id,
5835                                                             p_location_id        => pn_location_id,
5836                                                             p_tax_type           => lv_tax_type,
5837                                                             p_account_name       => jai_constants.liability);
5838   IF ln_tax_type_ccid IS NULL THEN
5839      lv_process_message := 'Liability Account is not defined at the regime registration.';
5840      RAISE_APPLICATION_ERROR(-20007, 'Error - package jai_ar_rgm_processing_pkg.generate_gl_entry: '
5841                                      ||lv_process_message);
5842   END IF;
5843 
5844   OPEN cur_get_unapplied_account;
5845   FETCH cur_get_unapplied_account INTO ln_receipt_unapplied_account;
5846   CLOSE cur_get_unapplied_account;
5847 
5848   lv_currency_code            := p_jacr.currency_code;
5849   ld_curr_conv_date           := p_jacr.EXCHANGE_DATE;
5850   lv_curr_conv_type           := p_jacr.EXCHANGE_RATE_TYPE;
5851   ln_curr_conv_rate           := p_jacr.EXCHANGE_RATE;
5852 
5853   IF p_document_type IN (--jai_constants.ar_cash_tax_confirmed, /* Receipt confirmation */
5854                          --Changed by Qiong from ar_cash_tax_confirmed to trx_type_adv_rcpts for bug13569249
5855                          jai_constants.trx_type_adv_rcpts,
5856                          jai_constants.trx_type_adv_unapp      /* Receipt unapplication*/
5857                          )
5858   THEN
5859       IF p_document_type IN ( jai_constants.trx_type_adv_rcpts)
5860       --Changed by Qiong from ar_cash_tax_confirmed to trx_type_adv_rcpts for bug13569249
5861       THEN
5862           lv_repository_name          := 'cash_receipt_id';
5863           lv_call_from                := 'CONFIRM_ACCOUNTING';
5864           lv_source_table_name        := 'AR_CASH_RECEIPTS_ALL';
5865           ln_source_document_id       := p_jacr.cash_receipt_id;
5866           ld_transaction_date         := p_jacr.CONFIRM_DATE;
5867           ld_gl_date                  := p_jacr.gl_date;
5868           lv_reference_name           := jai_constants.jai_cash_rcpts;
5869           ln_reference_id             := p_jacr.cash_receipt_id;
5870           lv_attribute1               := p_acra.RECEIPT_NUMBER;
5871       ELSE
5872           ln_accounted_amt            := ln_accounted_amt;
5873           lv_repository_name          := 'receivable_application_id';
5874           lv_call_from                := 'UNAPPLY_ACCOUNTING';
5875           lv_source_table_name        := 'AR_RECEIVABLE_APPLICATIONS_ALL';
5876           ln_source_document_id       := p_araa.RECEIVABLE_APPLICATION_ID;
5877           ld_gl_date                  := p_araa.gl_date;
5878           ld_transaction_date         := p_araa.APPLY_DATE;
5879           lv_reference_name           := 'AR_RECEIVABLE_APPLICATIONS_ALL';
5880           ln_reference_id             := p_araa.RECEIVABLE_APPLICATION_ID;
5881           lv_attribute1               := to_char(p_araa.CASH_RECEIPT_ID);
5882           lv_attribute2               := to_char(p_araa.CUSTOMER_TRX_ID);
5883       END IF;
5884       ln_dr_account    := ln_receipt_unapplied_account;
5885       ln_cr_account    := ln_tax_type_ccid;
5886 
5887   ELSIF p_document_type IN (jai_constants.trx_type_adv_app,
5888                             jai_constants.trx_type_adv_rvs
5889                             --Modified by Qinglei on 24-Feb-2012 for bug#13741544
5890                             )
5891   THEN
5892       IF p_document_type IN ( jai_constants.trx_type_adv_rvs)
5893         --Modified by Qinglei on 24-Feb-2012 for bug#13741544
5894       THEN
5895           /*Added by Qinglei on 12-Jan-2012 for bug#13569242 begin*/
5896           OPEN cur_get_rcpt_reversal_gl_date;
5897           FETCH cur_get_rcpt_reversal_gl_date INTO ld_gl_date;
5898           CLOSE cur_get_rcpt_reversal_gl_date;
5899           /*Added by Qinglei on 12-Jan-2012 for bug#13569242 end*/
5900           lv_repository_name          := 'cash_receipt_id';
5901           lv_call_from                := 'REVERSE_ACCOUNTING';
5902           lv_source_table_name        := 'AR_CASH_RECEIPTS_ALL';
5903           ln_source_document_id       := p_jacr.cash_receipt_id;
5904           ld_transaction_date         := p_acra.reversal_date;
5905           --ld_gl_date                  := p_acra.reversal_date;
5906           /*Commented by Qinglei on 12-Jan-2012 for bug#13569242*/
5907           lv_reference_name           := jai_constants.jai_cash_rcpts;
5908           ln_reference_id             := p_jacr.cash_receipt_id;
5909           lv_attribute1               := p_acra.RECEIPT_NUMBER;
5910       ELSE
5911           lv_repository_name          := 'receivable_application_id';
5912           lv_call_from                := 'APPLY_ACCOUNTING';
5913           lv_source_table_name        := 'AR_RECEIVABLE_APPLICATIONS_ALL';
5914           ln_source_document_id       := p_araa.RECEIVABLE_APPLICATION_ID;
5915           ld_gl_date                  := p_araa.gl_date;
5916           ld_transaction_date         := p_araa.APPLY_DATE;
5917           lv_reference_name           := 'AR_RECEIVABLE_APPLICATIONS_ALL';
5918           ln_reference_id             := p_araa.RECEIVABLE_APPLICATION_ID;
5919           lv_attribute1               := to_char(p_araa.CASH_RECEIPT_ID);
5920           lv_attribute2               := to_char(p_araa.CUSTOMER_TRX_ID);
5921       END IF;
5922       ln_entered_amt   := -ln_entered_amt;
5923       ln_accounted_amt := -ln_accounted_amt;
5924       ln_dr_account    := ln_receipt_unapplied_account;
5925       ln_cr_account    := ln_tax_type_ccid;
5926   END IF;
5927 
5928   IF nvl(ln_entered_amt, 0) <> 0 THEN
5929     jai_cmn_rgm_recording_pkg.post_accounting(
5930                       p_regime_code         => jai_constants.service_regime,
5931                       p_tax_type            => lv_tax_type,
5932                       p_organization_type   => jai_constants.service_tax_orgn_type,
5933                       p_organization_id     => pn_organization_id,
5934                       p_source              => jai_constants.source_ar,
5935                       p_source_trx_type     => p_document_type,
5936                       p_source_table_name   => lv_source_table_name,
5937                       p_source_document_id  => ln_source_document_id,
5938                       p_code_combination_id => ln_cr_account,
5939                       p_entered_cr          => ln_entered_amt,
5940                       p_entered_dr          => NULL,
5941                       p_accounted_cr        => ln_accounted_amt,
5942                       p_accounted_dr        => NULL,
5943                       p_accounting_date     => ld_gl_date,
5944                       p_transaction_date    => ld_transaction_date,
5945                       p_calling_object      => lv_called_from,
5946                       p_repository_name     => lv_repository_name,
5947                       p_repository_id       => NULL,
5948                       p_reference_name      => lv_reference_name,
5949                       p_reference_id        => ln_reference_id,
5950                       p_currency_code       => lv_currency_code,
5951                       p_curr_conv_date      => ld_curr_conv_date,
5952                       p_curr_conv_type      => lv_curr_conv_type,
5953                       p_curr_conv_rate      => ln_curr_conv_rate,
5954                       p_attribute1 => lv_attribute1,
5955                       p_attribute2 => lv_attribute2
5956                     );
5957 
5958   jai_cmn_rgm_recording_pkg.post_accounting(
5959                     p_regime_code         => jai_constants.service_regime,
5960                     p_tax_type            => lv_tax_type,
5961                     p_organization_type   => jai_constants.service_tax_orgn_type,
5962                     p_organization_id     => pn_organization_id,
5963                     p_source              => jai_constants.source_ar,
5964                     p_source_trx_type     => p_document_type,
5965                     p_source_table_name   => lv_source_table_name,
5966                     p_source_document_id  => ln_source_document_id,
5967                     p_code_combination_id => ln_dr_account,
5968                     p_entered_cr          => NULL,
5969                     p_entered_dr          => ln_entered_amt,
5970                     p_accounted_cr        => NULL,
5971                     p_accounted_dr        => ln_accounted_amt,
5972                     p_accounting_date     => ld_gl_date,
5973                     p_transaction_date    => ld_transaction_date,
5974                     p_calling_object      => lv_called_from,
5975                     p_repository_name     => lv_repository_name,
5976                     p_repository_id       => NULL,
5977                     p_reference_name      => lv_reference_name,
5978                     p_reference_id        => ln_reference_id,
5979                     p_currency_code       => lv_currency_code,
5980                     p_curr_conv_date      => ld_curr_conv_date,
5981                     p_curr_conv_type      => lv_curr_conv_type,
5982                     p_curr_conv_rate      => ln_curr_conv_rate,
5983                     p_attribute1 => lv_attribute1,
5984                     p_attribute2 => lv_attribute2
5985                   );
5986 
5987   END IF;
5988 END generate_gl_entry;
5989 
5990 --========================================================================
5991 -- PROCEDURE : generate_gl_entry       PUBLIC
5992 -- PARAMETERS: p_acra               rowtype of ar_cash_receipts_all
5993 --           : p_document_type
5994 --           : p_process_flag       the process result
5995 --           : p_process_message    the process message
5996 -- COMMENT   : validate if current receipt attached service tax
5997 -- PRE-COND  :
5998 -- EXCEPTIONS:
5999 --
6000 --========================================================================
6001 PROCEDURE validate_receipts
6002 ( p_acra            IN    ar_cash_receipts_all%ROWTYPE
6003  ,p_document_type   IN    VARCHAR2
6004  ,p_process_flag    OUT NOCOPY  VARCHAR2
6005  ,p_process_message OUT NOCOPY  VARCHAR2)
6006 IS
6007 lv_process_flag VARCHAR2(2);
6008 lv_process_message VARCHAR(2000);
6009 ln_exists NUMBER(2);
6010 
6011 /*
6012 || Check that the document has has Service tax.
6013 */
6014 CURSOR cur_chk_st_applicable (cp_cash_receipt_id JAI_AR_CASH_RECEIPTS_ALL.CASH_RECEIPT_ID%TYPE )
6015 IS
6016 SELECT 1
6017 FROM jai_ar_cash_receipts_all  jcra,
6018      jai_cmn_document_taxes    jdt,
6019      jai_regime_tax_types_v    jrttv
6020 WHERE jcra.cash_receipt_id    =   cp_cash_receipt_id
6021 AND jcra.cash_receipt_id      =   jdt.source_doc_id
6022 AND jdt.tax_type              =   jrttv.tax_type
6023 AND jrttv.regime_code         =   jai_constants.service_regime
6024 AND jcra.confirm_flag         =   jai_constants.yes
6025 AND jdt.source_doc_type       =   jai_constants.ar_cash;
6026 
6027 BEGIN
6028   lv_process_flag := jai_constants.successful;
6029   lv_process_message := NULL;
6030 
6031   p_process_flag := lv_process_flag;
6032   p_process_message := lv_process_message;
6033 
6034   OPEN cur_chk_st_applicable(p_acra.cash_receipt_id);
6035   FETCH cur_chk_st_applicable INTO ln_exists;
6036   CLOSE cur_chk_st_applicable;
6037 
6038   IF nvl(ln_exists,-1) <> 1 THEN
6039      p_process_flag := jai_constants.not_applicable;
6040      RETURN;
6041   END IF;
6042 
6043 END validate_receipts;
6044 
6045 --========================================================================
6046 -- PROCEDURE : generate_gl_entry       PUBLIC
6047 -- PARAMETERS: p_araa               rowtype of ar_receivable_applications_all
6048 --           : p_document_type
6049 --           : p_process_flag       the process result
6050 --           : p_process_message    the process message
6051 -- COMMENT   : validate if current receipt attached sercice tax and determine
6052 --             the docuement type is application or unapplication
6053 -- PRE-COND  :
6054 -- EXCEPTIONS:
6055 --
6056 --========================================================================
6057 PROCEDURE validate_app_unapp
6058 ( p_araa            IN ar_receivable_applications_all%ROWTYPE
6059  ,p_document_type   OUT NOCOPY  VARCHAR2
6060  ,p_process_flag    OUT NOCOPY  VARCHAR2
6061  ,p_process_message OUT NOCOPY  VARCHAR2)
6062 IS
6063 /*
6064 ||Get the sign of the Cash receipt document
6065 */
6066 CURSOR cur_get_cr_sign (cp_cash_receipt_id JAI_AR_CASH_RECEIPTS_ALL.CASH_RECEIPT_ID%TYPE )
6067 IS
6068 SELECT
6069       sign (nvl(amount,0)) app_fr_sign
6070 FROM
6071       ar_cash_receipts_all
6072 WHERE
6073       cash_receipt_id = cp_cash_receipt_id;
6074 
6075 ln_sign_of_app NUMBER(3);
6076 ln_sign_of_app_fr_doc NUMBER(3);
6077 lv_app_doc_type VARCHAR2(100);
6078 lv_process_flag                 VARCHAR2(2);
6079 lv_process_message              VARCHAR2(4000);
6080 
6081 ln_exists NUMBER(2);
6082 
6083 /*
6084 || Check that the document has has Service tax.
6085 */
6086 CURSOR cur_chk_st_applicable (cp_cash_receipt_id JAI_AR_CASH_RECEIPTS_ALL.CASH_RECEIPT_ID%TYPE )
6087 IS
6088 SELECT
6089         1
6090 FROM
6091         jai_ar_cash_receipts_all  jcra   ,
6092         jai_cmn_document_taxes    jdt    ,
6093         jai_regime_tax_types_v    jrttv
6094 WHERE
6095         jcra.cash_receipt_id         =   cp_cash_receipt_id
6096 AND     jcra.cash_receipt_id         =   jdt.source_doc_id
6097 AND     jdt.tax_type                 =   jrttv.tax_type
6098 AND     jrttv.regime_code            =   jai_constants.service_regime
6099 AND     jcra.confirm_flag            =   jai_constants.yes
6100 AND     jdt.source_doc_type  = JAI_CONSTANTS.ar_cash;
6101 BEGIN
6102   ln_sign_of_app := sign(nvl(p_araa.AMOUNT_APPLIED,0));
6103 
6104   IF p_araa.APPLICATION_TYPE IN (jai_constants.ar_cash,
6105                                  jai_constants.ar_status_activity)
6106                                  AND p_araa.cash_receipt_id IS NOT NULL THEN
6107 
6108     OPEN cur_get_cr_sign(p_araa.cash_receipt_id);
6109     FETCH cur_get_cr_sign INTO ln_sign_of_app_fr_doc;
6110     CLOSE cur_get_cr_sign;
6111     IF ln_sign_of_app = ln_sign_of_app_fr_doc THEN
6112       lv_app_doc_type := jai_constants.trx_type_adv_app;
6113     ELSE
6114       lv_app_doc_type := jai_constants.trx_type_adv_unapp;
6115     END IF;
6116 
6117     OPEN cur_chk_st_applicable(p_araa.cash_receipt_id);
6118     FETCH cur_chk_st_applicable INTO ln_exists;
6119     CLOSE cur_chk_st_applicable;
6120     IF nvl(ln_exists,0) <> 1 THEN
6121        p_process_flag := jai_constants.not_applicable;
6122        RETURN;
6123     END IF;
6124   END IF;
6125 
6126   p_document_type := lv_app_doc_type;
6127 
6128 END validate_app_unapp;
6129 /*Added by Wenqiong for Advanced Receipt on Service Taxes End */
6130 END jai_ar_rgm_processing_pkg ;