DBA Data[Home] [Help]

PACKAGE BODY: APPS.AR_BUS_EVENT_SUB_PVT

Source


1 PACKAGE BODY AR_BUS_EVENT_SUB_PVT AS
2 /* $Header: ARBESUBB.pls 120.32.12020000.9 2013/03/16 02:36:00 naneja ship $*/
3 
4 TYPE ps_tab_type IS TABLE OF ar_payment_schedules%rowtype
5  INDEX BY BINARY_INTEGER;
6 pg_cer_dso_days  NUMBER;
7 
8 CURSOR get_trx_info (p_cust_acct_id NUMBER,
9                      p_site_use_id  NUMBER,
10                      p_currency     VARCHAR2,
11                      p_date         DATE) IS
12 SELECT largest_inv_amount
13 FROM   ar_trx_summary
14 WHERE  cust_account_id = p_cust_acct_id
15   AND  site_use_id = nvl(p_site_use_id,-99)
16   AND  currency = p_currency
17   AND  as_of_date = p_date
18  FOR UPDATE;
19 
20 CURSOR get_last_payment_info(p_cust_acct_id NUMBER,
21                              p_site_use_id  NUMBER,
22                              p_currency     VARCHAR2) IS
23  SELECT last_payment_date,last_payment_number
24    FROM   ar_trx_bal_summary
25    WHERE  cust_account_id = p_cust_acct_id
26      AND  site_use_id = nvl(p_site_use_id,-9999)
27      AND  currency = p_currency
28 FOR UPDATE;
29 pg_debug VARCHAR2(1) := nvl(fnd_profile.value('AFLOG_ENABLED'),'N');
30 
31 PROCEDURE debug (
32         p_message_name          IN      VARCHAR2 ) IS
33 BEGIN
34     ar_cmgt_util.debug (p_message_name, 'ar.cmgt.plsql.AR_BUS_EVENT_SUB_PVT');
35 END;
36 
37 PROCEDURE Update_recapp_info(l_trx_class IN VARCHAR2,
38                            l_trx_customer_id IN NUMBER,
39                            l_trx_site_use_id IN NUMBER,
40                            l_trx_currency_code IN VARCHAR2,
41                            l_trx_amt      IN NUMBER,
42                            l_op_trx_count IN NUMBER,
43                            l_rcpt_customer_id IN NUMBER,
44                            l_rcpt_site_use_id IN NUMBER,
45                            l_rcpt_currency_code IN VARCHAR2,
46                            l_rcpt_amt       IN NUMBER,
47                            l_apply_date  IN DATE,
48                            l_edisc_value IN NUMBER,
49                            l_edisc_count IN NUMBER,
50                            l_uedisc_value IN NUMBER,
51                            l_uedisc_count IN NUMBER,
52                            l_inv_paid_amt IN NUMBER,
53                            l_inv_inst_pmt_days_sum IN NUMBER,
54                            l_sum_app_amt_days_late IN NUMBER,
55                            l_sum_app_amt IN NUMBER,
56                            l_count_of_tot_inv_inst_paid IN NUMBER,
57                            l_count_of_inv_inst_paid_late IN NUMBER,
58                            l_count_of_disc_inv_inst IN NUMBER,
59                            l_unresolved_cash_value  IN NUMBER,
60                            l_unresolved_cash_count  IN NUMBER,
61                            l_op_cm_count IN NUMBER , --this is relevant to credit memo applications
62                            l_app_type IN VARCHAR2,
63                            l_past_due_inv_value   IN NUMBER,
64                            l_past_due_inv_inst_count IN NUMBER,
65                            l_org_id  IN NUMBER
66                            ) IS
67 
68 BEGIN
69     IF pg_debug = 'Y'
70     THEN
71     	debug ('AR_BUS_EVENT_SUB_PVT.Update_recapp_info(+)');
72     	debug ('cust_account_id ='||l_trx_customer_id);
73     	debug ('site_use_id ='||l_trx_site_use_id);
74     	debug ('currency ='||l_trx_currency_code);
75     	debug ('org_id ='||l_org_id);
76    END IF;
77 
78 
79         UPDATE ar_trx_bal_summary
80           set OP_CREDIT_MEMOS_VALUE
81                        = nvl(OP_CREDIT_MEMOS_VALUE,0)
82                                      + DECODE(l_trx_class,'CM',
83                                               nvl(l_trx_amt,0),0)
84                                      + DECODE(l_app_type,'CM',l_trx_amt,0),
88                                          - DECODE(l_app_type,'CM',l_op_cm_count,0),
85               OP_CREDIT_MEMOS_COUNT = nvl(OP_CREDIT_MEMOS_COUNT,0)
86                                          - DECODE(l_trx_class,'CM',
87                                                nvl(l_op_trx_count,0),0)
89               OP_INVOICES_VALUE = nvl(OP_INVOICES_VALUE,0)
90                                         - DECODE(l_trx_class,'INV',
91                                         nvl(l_trx_amt,0),0),
92               OP_INVOICES_COUNT = nvl(OP_INVOICES_COUNT,0)
93                                           - DECODE(l_trx_class, 'INV',
94                                                nvl(l_op_trx_count,0),0),
95               OP_DEBIT_MEMOS_VALUE = nvl(OP_DEBIT_MEMOS_VALUE,0)
96                                         - DECODE(l_trx_class,'DM',
97                                         nvl(l_trx_amt,0),0),
98               OP_DEBIT_MEMOS_COUNT =  nvl(OP_DEBIT_MEMOS_COUNT,0)
99                                           - DECODE(l_trx_class, 'DM',
100                                                nvl(l_op_trx_count,0),0),
101               OP_DEPOSITS_VALUE = nvl(OP_DEPOSITS_VALUE,0)
102                                         - DECODE(l_trx_class,'DEP',
103                                         nvl(l_trx_amt,0),0),
104               OP_DEPOSITS_COUNT =  nvl(OP_DEPOSITS_COUNT,0)
105                                           - DECODE(l_trx_class, 'DEP',
106                                                  nvl(l_op_trx_count,0),0),
107               OP_CHARGEBACK_VALUE = nvl(OP_CHARGEBACK_VALUE,0)
108                                         - DECODE(l_trx_class,'CB',
109                                                nvl(l_trx_amt,0),0),
110               OP_CHARGEBACK_COUNT =  nvl(OP_CHARGEBACK_COUNT,0)
111                                           - DECODE(l_trx_class, 'CB',
112                                                  nvl(l_op_trx_count,0),0),
113               PAST_DUE_INV_VALUE = nvl(PAST_DUE_INV_VALUE,0) -
114                                       DECODE(l_trx_class,'INV',
115                                              nvl(l_past_due_inv_value,0),0),
116               PAST_DUE_INV_INST_COUNT = nvl(PAST_DUE_INV_INST_COUNT,0)
117                                          - DECODE(l_trx_class,'INV',
118                                              nvl(l_past_due_inv_inst_count,0),0),
119               LAST_UPDATE_DATE  = sysdate,
120               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
121               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
122          WHERE cust_account_id = l_trx_customer_id
123            and site_use_id = l_trx_site_use_id
124            and currency = l_trx_currency_code
125            and NVL(org_id,'-99') = NVL(l_org_id,-99);
126 
127       IF l_app_type = 'CASH'  THEN
128         UPDATE ar_trx_bal_summary
129           set UNRESOLVED_CASH_VALUE = nvl(UNRESOLVED_CASH_VALUE,0)
130                                           + nvl(l_unresolved_cash_value,0),
131               UNRESOLVED_CASH_COUNT = nvl(UNRESOLVED_CASH_COUNT,0) -
132                                           nvl(l_unresolved_cash_count,0),
133               LAST_UPDATE_DATE  = sysdate,
134               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
135               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
136          WHERE cust_account_id = l_rcpt_customer_id
137            and site_use_id = l_rcpt_site_use_id
138            and currency = l_rcpt_currency_code
139            and NVL(org_id,'-99') = NVL(l_org_id,-99);
140       END IF;
141 
142       IF l_app_type = 'CASH'  THEN
143         UPDATE ar_trx_summary
144           set INV_PAID_AMOUNT = nvl(INV_PAID_AMOUNT,0)
145                                               + nvl(l_inv_paid_amt,0),
146               INV_INST_PMT_DAYS_SUM = nvl(INV_INST_PMT_DAYS_SUM,0)
147                                              + nvl(l_inv_inst_pmt_days_sum,0),
148               TOTAL_EARNED_DISC_VALUE = nvl(TOTAL_EARNED_DISC_VALUE,0)
149                                              + nvl(l_edisc_value,0),
150               TOTAL_EARNED_DISC_COUNT = nvl(TOTAL_EARNED_DISC_COUNT,0)
151                                              + nvl(l_edisc_count,0),
152               TOTAL_UNEARNED_DISC_VALUE = nvl(TOTAL_UNEARNED_DISC_VALUE,0)
153                                              + nvl(l_uedisc_value,0),
154               TOTAL_UNEARNED_DISC_COUNT = nvl(TOTAL_UNEARNED_DISC_COUNT,0)
155                                              + nvl(l_uedisc_count,0),
156               SUM_APP_AMT_DAYS_LATE  = nvl(SUM_APP_AMT_DAYS_LATE,0)
157                                              + nvl(l_SUM_APP_AMT_DAYS_LATE,0),
158               SUM_APP_AMT = nvl(SUM_APP_AMT,0) + nvl(l_sum_app_amt,0),
159               COUNT_OF_TOT_INV_INST_PAID = nvl(COUNT_OF_TOT_INV_INST_PAID,0)
160                                             + nvl(l_count_of_tot_inv_inst_paid,0),
161               COUNT_OF_INV_INST_PAID_LATE = nvl(COUNT_OF_INV_INST_PAID_LATE,0)
162                                             + nvl(l_count_of_inv_inst_paid_late,0),
163               COUNT_OF_DISC_INV_INST = nvl(COUNT_OF_DISC_INV_INST,0) +
164                                              nvl(l_count_of_disc_inv_inst,0),
165               LAST_UPDATE_DATE  = sysdate,
166               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
167               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
168            -- DAYS_CREDIT_GRANTED_SUM = nvl(DAYS_CREDIT_GRANTED_SUM)
169          WHERE cust_account_id = l_rcpt_customer_id
170            and site_use_id = l_rcpt_site_use_id
171            and currency = l_rcpt_currency_code
172            and as_of_date = l_apply_date
173            and NVL(org_id,'-99') = NVL(l_org_id,-99);
174       END IF;
175     IF pg_debug = 'Y'
176     THEN
177     	debug ('AR_BUS_EVENT_SUB_PVT.Update_recapp_info(-)');
178     END IF;
179 END Update_recapp_info;
180 
181 PROCEDURE Update_summary_for_request_id (p_request_id IN NUMBER)
182 IS
183 CURSOR get_req_run_data(p_req_id IN NUMBER) IS
184   Select ps.class,
185          ps.customer_id,
186          ps.customer_site_use_id,
190          ps.due_date,
187          ps.trx_date,
188          ps.invoice_currency_code,
189          ps.org_id,
191          ps.customer_trx_id ,
192          trx.previous_customer_trx_id,
193          ctt.type prev_trx_type,
194          ps.terms_sequence_number,
195          ps.amount_due_original,
196          trx_sum.largest_inv_amount largest_inv_amount,
197          trx_sum.largest_inv_date largest_inv_date,
198          trx_sum.largest_inv_cust_trx_id largest_inv_cust_trx_id,
199          count(nvl(rtl.term_id,1)) installment_count,
200          sum(decode(sign(ra_cm.amount_applied),0,null,
201                 decode(ctt.type,'INV',
202                     decode(cm_app_ps.status,'CL',1,null))))
203                                    cm_closed_inv_count,
204          sum(decode(sign(ra_cm.amount_applied),0,null,
205               decode(ctt.type,'DM',
206                     decode(cm_app_ps.status,'CL',1,null))))
207                                    cm_closed_dm_count,
208          sum(decode(sign(ra_cm.amount_applied),0,null,
209               decode(ctt.type,'CM',
210                     decode(cm_app_ps.status,'CL',1,null))))
211                                    cm_closed_cm_count,
212          sum(decode(sign(ra_cm.amount_applied),0,null,
213               decode(ctt.type,'INV',
214                 decode(cm_app_ps.status,'CL',
215                     decode(sign( cm_app_ps.due_date - trunc(sysdate)),-1,1,null)
216                        )
217                      )
218                    ))              cm_cl_past_due_inv_ct,
219          sum(decode(sign(ra_cm.amount_applied),0,null,
220               decode(ctt.type,'INV',
221                 decode(cm_app_ps.status,'CL',
222                     decode(sign( cm_app_ps.due_date - trunc(sysdate)),-1,
223                         ra_cm.amount_applied,null)
224                        )
225                      )
226                    ))              cm_cl_past_due_inv_amt
227   from ra_customer_trx trx,
228        ar_payment_schedules ps,
229        ra_customer_trx prev_trx,
230        ra_cust_trx_types ctt,
231        ra_terms rt,
232        ra_terms_lines rtl,
233        ar_receivable_applications_all ra_cm,
234        ar_payment_schedules_all cm_app_ps,
235        ar_trx_summary trx_sum
236   where trx.customer_trx_id = ps.customer_trx_id
237     and trx.request_id = p_req_id
238     and trx.previous_customer_trx_id = prev_trx.customer_trx_id(+)
239     and prev_trx.cust_trx_type_id = ctt.cust_trx_type_id(+)
240     and rt.term_id(+) = ps.term_id
241     and rt.term_id = rtl.term_id(+)
242     and trx.customer_trx_id = ra_cm.customer_trx_id(+)
243     and ra_cm.applied_payment_schedule_id = cm_app_ps.payment_schedule_id(+)
244     and trx_sum.cust_account_id(+) = trx.bill_to_customer_id
245     and trx_sum.site_use_id(+) = trx.bill_to_site_use_id
246     and trx_sum.currency(+) = trx.invoice_currency_code
247     and trx_sum.as_of_date(+) = trx.trx_date
248     and trx_sum.org_id (+) = trx.org_id
249   group by ps.class,
250          ps.customer_id,
251          ps.customer_site_use_id,
252          ps.trx_date,
253          ps.invoice_currency_code,
254          ps.org_id,
255          ps.due_date,
256          ps.customer_trx_id ,
257          trx.previous_customer_trx_id,
258          ctt.type,
259          ps.terms_sequence_number,
260          ps.amount_due_original,
261          trx_sum.largest_inv_amount,
262          trx_sum.largest_inv_date ,
263          trx_sum.largest_inv_cust_trx_id
264   order by ps.customer_trx_id,ps.terms_sequence_number;
265 
266   l_prev_customer_trx_id   NUMBER;
267   l_inst_counter           NUMBER;
268   l_trx_amount             NUMBER;
269 
270 BEGIN
271     IF pg_debug = 'Y'
272     THEN
273     	debug ('AR_BUS_EVENT_SUB_PVT.Update_summary_for_request_id(+)');
274     	debug ('p_request_id ='||p_request_id);
275    END IF;
276  IF p_request_id IS NOT NULL  THEN
277   For rec in get_req_run_data(p_request_id) LOOP
278    IF  rec.installment_count > 1 and
279        l_prev_customer_trx_id = rec.customer_trx_id
280     THEN
281      l_inst_counter := nvl(l_inst_counter,0) + 1;
282      l_trx_amount := nvl(l_trx_amount,0) + rec.amount_due_original;
283 
284    ELSE
285      --reset the installment counter for a new transaction
286      l_inst_counter := 1;
287      l_trx_amount :=  rec.amount_due_original;
288    END IF;
289 
290    --
291    -- l_tot_inv_amt := 0;
292 
293 
294          UPDATE ar_trx_bal_summary
295           set BEST_CURRENT_RECEIVABLES
296                        = nvl(BEST_CURRENT_RECEIVABLES,0)
297                               +DECODE(sign(rec.due_date - trunc(sysdate)),-1,0,
298                                rec.amount_due_original),
299               OP_INVOICES_VALUE
300                        = nvl(OP_INVOICES_VALUE,0)
301                              + DECODE(rec.class , 'INV' , rec.amount_due_original,
302                                  'CM', decode(rec.previous_customer_trx_id, null,0,
303                                       decode(rec.prev_trx_type,'INV',
304                                                  rec.amount_due_original,0)),0),
305               OP_INVOICES_COUNT
306                        = nvl(OP_INVOICES_COUNT,0) +
307                              DECODE(rec.class,'INV',1,'CM',
308                                    decode(rec.previous_customer_trx_id, null,0,
309                                      decode(rec.prev_trx_type,'INV',
310                                                  -rec.cm_closed_inv_count)),0),
311               PAST_DUE_INV_VALUE
312                        = nvl(PAST_DUE_INV_VALUE,0) +  decode(rec.class , 'INV',
313                                 decode(sign(rec.due_date - trunc(sysdate)),-1,
314                                    rec.amount_due_original,0),'CM',
318               PAST_DUE_INV_INST_COUNT
315                                     decode(rec.previous_customer_trx_id, null,0,
316                                        decode(rec.prev_trx_type,'INV',
317                                                rec.cm_cl_past_due_inv_amt,0)),0),
319                       = nvl(PAST_DUE_INV_INST_COUNT,0) + decode(rec.class,'INV',
320                              decode(sign(rec.due_date - trunc(sysdate)),-1,1,0),
321                               'CM', decode(rec.previous_customer_trx_id, null,0,
322                                   decode(rec.prev_trx_type,'INV',
323                                             -rec.cm_cl_past_due_inv_ct,0)),0),
324               OP_CREDIT_MEMOS_VALUE
325                      = nvl(OP_CREDIT_MEMOS_VALUE,0) + DECODE(rec.class,'CM',
326                                decode(rec.previous_customer_trx_id,  null,
327                                         rec.amount_due_original),0),
328               OP_CREDIT_MEMOS_COUNT
329                      = nvl(OP_CREDIT_MEMOS_COUNT,0) +
330                               DECODE(rec.class,'CM',
331                                      DECODE(rec.previous_customer_trx_id, null,1,
332                                         DECODE(rec.prev_trx_type,'CM',
333                                                      -rec.cm_closed_cm_count,0)),0),
334               OP_DEBIT_MEMOS_VALUE
335                      = nvl(OP_DEBIT_MEMOS_VALUE,0) + DECODE(rec.class , 'DM',
336                             rec.amount_due_original,'CM',
337                                 DECODE(rec.previous_customer_trx_id, null,0,
338                                     DECODE(rec.prev_trx_type,'DM',
339                                                  rec.amount_due_original,0)),0),
340               OP_DEBIT_MEMOS_COUNT
341                      =  nvl(OP_DEBIT_MEMOS_COUNT,0)+ DECODE(rec.class,'DM',1,'CM',
342                                  DECODE(rec.previous_customer_trx_id, null,0,
343                                      DECODE(rec.prev_trx_type,'DM',
344                                                  -rec.cm_closed_dm_count,0)),0),
345               LAST_UPDATE_DATE  = sysdate,
346               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
347               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
348          WHERE cust_account_id = rec.customer_id
349            and site_use_id = nvl(rec.customer_site_use_id,-99)
350            and currency = rec.invoice_currency_code
351            and NVL(org_id,'-99') = NVL(rec.org_id,-99);
352 
353 
354          IF SQL%NOTFOUND THEN
355 /* bug4403146 : Modified the decode for getting OP_DEBIT_MEMOS_COUNT */
356            INSERT INTO ar_trx_bal_summary
357             (CUST_ACCOUNT_ID,
358              SITE_USE_ID,
359              ORG_ID,
360              CURRENCY,
361              last_update_date,
362              last_updated_by,
363              creation_date,
364              created_by,
365              last_update_login,
366              BEST_CURRENT_RECEIVABLES,
367              OP_INVOICES_VALUE,
368              OP_INVOICES_COUNT,
369              PAST_DUE_INV_VALUE,
370              PAST_DUE_INV_INST_COUNT,
371              OP_CREDIT_MEMOS_VALUE,
372              OP_CREDIT_MEMOS_COUNT,
373              OP_DEBIT_MEMOS_VALUE,
374              OP_DEBIT_MEMOS_COUNT)
375              VALUES
376             ( rec.customer_id,
377               nvl(rec.customer_site_use_id,-99),
378               rec.org_id,
379               rec.invoice_currency_code,
380               sysdate,
381               fnd_global.user_id,
382               sysdate,
383               fnd_global.user_id,
384               fnd_global.login_id,
385               DECODE(sign(rec.due_date - trunc(sysdate)),-1,0,
386                          rec.amount_due_original),
387               DECODE(rec.class , 'INV' , rec.amount_due_original,
388                                  'CM', decode(rec.previous_customer_trx_id, null,0,
389                                       decode(rec.prev_trx_type,'INV',
390                                                  rec.amount_due_original,0)),0),
391               DECODE(rec.class,'INV',1,'CM',
392                                    decode(rec.previous_customer_trx_id, null,0,
393                                      decode(rec.prev_trx_type,'INV',
394                                                  -rec.cm_closed_inv_count)),0),
395               -decode(rec.class , 'INV',
396                                 decode(sign(rec.due_date - trunc(sysdate)),-1,
397                                    rec.amount_due_original,0),'CM',
398                                     decode(rec.previous_customer_trx_id, null,0,
399                                        decode(rec.prev_trx_type,'INV',
400                                                rec.cm_cl_past_due_inv_amt,0)),0),
401                decode(rec.class,'INV',
402                              decode(sign(rec.due_date - trunc(sysdate)),-1,1,0),
403                               'CM', decode(rec.previous_customer_trx_id, null,0,
404                                   decode(rec.prev_trx_type,'INV',
405                                             -rec.cm_cl_past_due_inv_ct,0)),0),
406                DECODE(rec.class,'CM',
407                                decode(rec.previous_customer_trx_id,  null,
408                                         rec.amount_due_original),0),
409                DECODE(rec.previous_customer_trx_id, null,1,
410                                         DECODE(rec.prev_trx_type,'CM',
411                                                      -rec.cm_closed_cm_count,0)),
412                DECODE(rec.class , 'DM',
413                             rec.amount_due_original,'CM',
414                                 DECODE(rec.previous_customer_trx_id, null,0,
415                                     DECODE(rec.prev_trx_type,'DM',
416                                                  rec.amount_due_original,0)),0),
417                DECODE(rec.class,'DM',1,'CM'
418                                 , DECODE(rec.previous_customer_trx_id, null,0,
422 
419                                      DECODE(rec.prev_trx_type,'DM',
420                                                  -rec.cm_closed_dm_count,0)),0)
421                 );
423             END IF;
424 
425         UPDATE ar_trx_summary
426           SET OP_BAL_HIGH_WATERMARK = nvl(OP_BAL_HIGH_WATERMARK,0) +
427                                              nvl(rec.amount_due_original,0),
428               TOTAL_INVOICES_VALUE
429                        = DECODE(rec.class , 'INV',
430                            (nvl(TOTAL_INVOICES_VALUE,0)
431                                  + nvl(rec.amount_due_original,0)),
432                                        TOTAL_INVOICES_VALUE),
433               TOTAL_INVOICES_COUNT
434                        = DECODE(rec.class,'INV',nvl(TOTAL_INVOICES_COUNT,0)+1,
435                               TOTAL_INVOICES_COUNT),
436               LARGEST_INV_AMOUNT
437                        = DECODE(rec.class , 'INV',
438                           DECODE(sign(rec.installment_count - l_inst_counter),0,
439                             DECODE(sign(l_trx_amount-nvl(LARGEST_INV_AMOUNT,0)),
440                                1,l_trx_amount,LARGEST_INV_AMOUNT),
441                                     LARGEST_INV_AMOUNT), LARGEST_INV_AMOUNT),
442               LARGEST_INV_DATE = rec.trx_date,
443               LARGEST_INV_CUST_TRX_ID
444                        = DECODE(rec.class , 'INV',
445                           DECODE(sign(rec.installment_count - l_inst_counter),0,
446                            DECODE(sign(l_trx_amount-nvl(LARGEST_INV_AMOUNT,0)),1,
447                              rec.customer_trx_id,LARGEST_INV_CUST_TRX_ID),
448                                LARGEST_INV_CUST_TRX_ID),LARGEST_INV_CUST_TRX_ID),
449               TOTAL_CREDIT_MEMOS_VALUE
450                        = DECODE(rec.class,'CM',
451                            nvl(TOTAL_CREDIT_MEMOS_VALUE,0)+rec.amount_due_original,
452                              TOTAL_CREDIT_MEMOS_VALUE),
453               TOTAL_CREDIT_MEMOS_COUNT
454                        = DECODE(rec.class,'CM',nvl(TOTAL_CREDIT_MEMOS_COUNT,0)+1,
455                            TOTAL_CREDIT_MEMOS_COUNT),
456               TOTAL_DEBIT_MEMOS_VALUE
457                         = DECODE(rec.class,'DM',
458                            nvl(TOTAL_DEBIT_MEMOS_VALUE,0)+rec.amount_due_original,
459                               TOTAL_DEBIT_MEMOS_VALUE),
460               TOTAL_DEBIT_MEMOS_COUNT
461                          = DECODE(rec.class,'DM',nvl(TOTAL_DEBIT_MEMOS_COUNT,0)+1,
462                               TOTAL_DEBIT_MEMOS_COUNT),
463               DAYS_CREDIT_GRANTED_SUM
464                          = DECODE(rec.class,'INV',
465                               nvl(DAYS_CREDIT_GRANTED_SUM,0) +
466                                   (rec.amount_due_original *
467                                        (rec.due_date - rec.trx_date)),
468                                                DAYS_CREDIT_GRANTED_SUM),
469               LAST_UPDATE_DATE  = sysdate,
470               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
471               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
472          WHERE cust_account_id = rec.customer_id
473            AND site_use_id = nvl(rec.customer_site_use_id,-99)
474            AND currency = rec.invoice_currency_code
475            AND NVL(org_id,'-99') = NVL(rec.org_id,-99)
476            AND as_of_date = rec.trx_date;
477 
478          IF SQL%NOTFOUND THEN
479           INSERT INTO ar_trx_summary
480             ( CUST_ACCOUNT_ID,
481               SITE_USE_ID,
482               ORG_ID,
483               CURRENCY,
484               AS_OF_DATE,
485               last_update_date,
486               last_updated_by,
487               creation_date,
488               created_by,
489               last_update_login,
490               OP_BAL_HIGH_WATERMARK,
491               TOTAL_INVOICES_VALUE,
492               TOTAL_INVOICES_COUNT,
493               LARGEST_INV_AMOUNT,
494               LARGEST_INV_DATE,
495               LARGEST_INV_CUST_TRX_ID ,
496               TOTAL_CREDIT_MEMOS_VALUE ,
497               TOTAL_CREDIT_MEMOS_COUNT ,
498               TOTAL_DEBIT_MEMOS_VALUE,
499               TOTAL_DEBIT_MEMOS_COUNT ,
500               DAYS_CREDIT_GRANTED_SUM)
501           VALUES
502             ( rec.customer_id,
503               nvl(rec.customer_site_use_id,-99),
504               rec.org_id,
505               rec.invoice_currency_code,
506               rec.trx_date,
507               sysdate,
508               fnd_global.user_id,
509               sysdate,
510               fnd_global.user_id,
511               fnd_global.login_id,
512               rec.amount_due_original,
513               rec.amount_due_original,
514               DECODE(rec.class , 'INV',1,null),
515               DECODE(rec.class , 'INV',
516                 DECODE(sign(rec.installment_count - l_inst_counter),0,
517                        l_trx_amount,null),null),
518               rec.trx_date,
519               DECODE(rec.class , 'INV',
520                 DECODE(sign(rec.installment_count - l_inst_counter),0,
521                              rec.customer_trx_id,null),null),
522               DECODE(rec.class,'CM', rec.amount_due_original,null),
523               DECODE(rec.class,'CM',1,null),
524               DECODE(rec.class,'DM',rec.amount_due_original,null),
525               DECODE(rec.class,'DM',1, null),
526               DECODE(rec.class,'INV',
527                  (rec.amount_due_original * (rec.due_date - rec.trx_date)),
528                                                null));
529          END IF;
530 
531       l_prev_customer_trx_id := rec.customer_trx_id;
532    END LOOP;
533   END IF;
534     IF pg_debug = 'Y'
535     THEN
536     	debug ('AR_BUS_EVENT_SUB_PVT.Update_summary_for_request_id(-)');
537    END IF;
538 END Update_summary_for_request_id;
539 
543               l_org_id       IN NUMBER,
540 PROCEDURE Update_Adj_info (
541               l_customer_id  IN NUMBER,
542               l_site_use_id  IN NUMBER,
544               l_currency_code IN VARCHAR2,
545               l_adj_amount    IN NUMBER,
546               l_op_trx_count  IN NUMBER,
547               l_apply_date    IN DATE,
548               l_pending_adj_amount  IN NUMBER,
549               l_class         IN VARCHAR2,
550               l_special_adj   IN VARCHAR2 DEFAULT null,
551               l_past_due_inv_inst_count  IN NUMBER,
552               l_past_due_inv_value IN NUMBER
553                            ) IS
554 BEGIN
555     IF pg_debug = 'Y'
556     THEN
557     	debug ('AR_BUS_EVENT_SUB_PVT.Update_Adj_info(+)');
558         debug ('cust_account_id ='||l_customer_id);
559         debug ('site_use_id ='||l_site_use_id);
560         debug ('currency ='||l_currency_code);
561         debug ('org_id ='||l_org_id);
562    END IF;
563         UPDATE ar_trx_bal_summary
564           set BEST_CURRENT_RECEIVABLES
565                        = nvl(BEST_CURRENT_RECEIVABLES,0)
566                               + nvl(l_adj_amount,0),
567               OP_CREDIT_MEMOS_VALUE
568                        = nvl(OP_CREDIT_MEMOS_VALUE,0)
569                                         + DECODE(l_class,'CM',
570                                         nvl(l_adj_amount,0),0),
571               OP_CREDIT_MEMOS_COUNT = nvl(OP_CREDIT_MEMOS_COUNT,0)
572                                          + DECODE(l_class, 'CM',
573                                                nvl(l_op_trx_count,0),0),
574               OP_INVOICES_VALUE = nvl(OP_INVOICES_VALUE,0)
575                                         + DECODE(l_class,'INV',
576                                         nvl(l_adj_amount,0),0),
577               OP_INVOICES_COUNT = nvl(OP_INVOICES_COUNT,0)
578                                           + DECODE(l_class, 'INV',
579                                                nvl(l_op_trx_count,0),0),
580               OP_DEBIT_MEMOS_VALUE = nvl(OP_DEBIT_MEMOS_VALUE,0)
581                                         + DECODE(l_class,'DM',
582                                         nvl(l_adj_amount,0),0),
583               OP_DEBIT_MEMOS_COUNT =  nvl(OP_DEBIT_MEMOS_COUNT,0)
584                                           + DECODE(l_class, 'DM',
585                                                nvl(l_op_trx_count,0),0),
586               OP_DEPOSITS_VALUE = nvl(OP_DEPOSITS_VALUE,0)
587                                         + DECODE(l_class,'DEP',
588                                         nvl(l_adj_amount,0),0),
589               OP_DEPOSITS_COUNT =  nvl(OP_DEPOSITS_COUNT,0)
590                                           + DECODE(l_class, 'DEP',
591                                                nvl(l_op_trx_count,0),0),
592               PAST_DUE_INV_VALUE = nvl(PAST_DUE_INV_VALUE,0) +
593                                       DECODE(l_class,'INV',
594                                              nvl(l_past_due_inv_value,0),0),
595               PAST_DUE_INV_INST_COUNT = nvl(PAST_DUE_INV_INST_COUNT,0)
596                                          + DECODE(l_class,'INV',
597                                              nvl(l_past_due_inv_inst_count,0),0),
598               PENDING_ADJ_VALUE  = nvl(PENDING_ADJ_VALUE,0)
599                                           + DECODE(l_special_adj, 'Y', 0, nvl(l_pending_adj_amount,0)),
600               LAST_UPDATE_DATE  = sysdate,
601               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
602               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
603          WHERE cust_account_id = l_customer_id
604            and site_use_id = l_site_use_id
605            and currency = l_currency_code
606            and NVL(org_id,'-99') = NVL(l_org_id,-99);
607 
608            IF sql%NOTFOUND  THEN
609 
610             INSERT INTO ar_trx_bal_summary
611             (CUST_ACCOUNT_ID,
612              SITE_USE_ID,
613              ORG_ID,
614              CURRENCY,
615              last_update_date,
616              last_updated_by,
617              creation_date,
618              created_by,
619              last_update_login,
620              BEST_CURRENT_RECEIVABLES,
621              OP_CREDIT_MEMOS_VALUE,
622              OP_CREDIT_MEMOS_COUNT,
623              OP_INVOICES_VALUE,
624              OP_INVOICES_COUNT,
625              OP_DEBIT_MEMOS_VALUE,
626              OP_DEBIT_MEMOS_COUNT,
627              OP_DEPOSITS_VALUE,
628              OP_DEPOSITS_COUNT,
629              PENDING_ADJ_VALUE)
630              VALUES
631             ( l_customer_id,
632               l_site_use_id,
633               l_org_id,
634               l_currency_code,
635               sysdate,
636               fnd_global.user_id,
637               sysdate,
638               fnd_global.user_id,
639               fnd_global.login_id,
640               +nvl(l_adj_amount,0),
641               + DECODE(l_class,'CM',nvl(l_adj_amount,0),0),
642                 DECODE(l_class, 'CM', nvl(l_op_trx_count,0),0),
643               + DECODE(l_class,'INV',nvl(l_adj_amount,0),0),
644               + DECODE(l_class, 'INV', nvl(l_op_trx_count,0),0),
645               + DECODE(l_class,'DM', nvl(l_adj_amount,0),0),
646               + DECODE(l_class, 'DM',  nvl(l_op_trx_count,0),0),
647               + DECODE(l_class,'DEP', nvl(l_adj_amount,0),0),
648               + DECODE(l_class, 'DEP',  nvl(l_op_trx_count,0),0),
649               + DECODE(l_special_adj, 'Y',null,nvl(l_pending_adj_amount,0))
650                 );
651 
652            END IF;
653 
654         UPDATE ar_trx_summary
655         set  OP_BAL_HIGH_WATERMARK = nvl(OP_BAL_HIGH_WATERMARK,0)
659           TOTAL_ADJUSTMENTS_COUNT = nvl(TOTAL_ADJUSTMENTS_COUNT,0)
656                                         + nvl(l_adj_amount,0),
657           TOTAL_ADJUSTMENTS_VALUE = nvl(TOTAL_ADJUSTMENTS_VALUE,0)
658                                        + DECODE(l_special_adj, 'Y',0, nvl(l_adj_amount,0)),
660                                        + DECODE(l_special_adj, 'Y',0,nvl(l_op_trx_count,0)),
661           LAST_UPDATE_DATE  = sysdate,
662           LAST_UPDATED_BY   = FND_GLOBAL.user_id,
663           LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
664         where cust_account_id = l_customer_id
665            and site_use_id = l_site_use_id
666            and currency = l_currency_code
667            and as_of_date = l_apply_date
668            and NVL(org_id,'-99') = NVL(l_org_id,-99);
669 
670            IF sql%NOTFOUND  THEN
671 
672             INSERT INTO ar_trx_summary
673             (CUST_ACCOUNT_ID,
674              SITE_USE_ID,
675              ORG_ID,
676              CURRENCY,
677              AS_OF_DATE,
678              last_update_date,
679              last_updated_by,
680              creation_date,
681              created_by,
682              last_update_login,
683              TOTAL_ADJUSTMENTS_VALUE,
684              TOTAL_ADJUSTMENTS_COUNT,
685              OP_BAL_HIGH_WATERMARK
686              )
687              VALUES
688             ( l_customer_id,
689               l_site_use_id,
690               l_org_id,
691               l_currency_code,
692               l_apply_date,
693               sysdate,
694               fnd_global.user_id,
695               sysdate,
696               fnd_global.user_id,
697               fnd_global.login_id,
698               nvl(l_adj_amount,0),
699               nvl(l_op_trx_count,0),
700               nvl(l_adj_amount,0)  );
701            END IF;
702     IF pg_debug = 'Y'
703     THEN
704     	debug ('AR_BUS_EVENT_SUB_PVT.Update_Adj_info(-)');
705    END IF;
706 END Update_Adj_info;
707 
708 PROCEDURE Update_rcpt_app_info_for_req(p_req_id in number,
709                                        p_org_id in number)
710 IS
711 cursor create_recept_info(p_req_id IN NUMBER) IS
712 select rps.customer_id                    customer_id,
713        nvl(rps.customer_site_use_id, -99) site_use_id,
714        rps.invoice_currency_code          rcpt_currency,
715        cr.receipt_date                    as_of_date,
716        sum(nvl(ra.amount_applied_from,ra.amount_applied)) receipt_amount
717 from ar_receivable_applications ra,
718      ar_payment_schedules rps,
719      ar_cash_receipts cr
720 where ra.request_id = p_req_id
721   and ra.status = 'UNAPP'
722   and sign(ra.amount_applied) = 1
723   and rps.payment_schedule_id = ra.payment_schedule_id
724   and cr.cash_receipt_id = ra.cash_receipt_id
725   group by rps.customer_id,
726        nvl(rps.customer_site_use_id, -99),
727        rps.invoice_currency_code,
728        cr.receipt_date,
729        ra.cash_receipt_id
730   order by rps.customer_id,
731        site_use_id,
732        rps.invoice_currency_code,
733        cr.receipt_date;
734 /* bug4335997 : Modified the where clause of cursor get_last_payment_info
735                 to get the correct last payment info */
736 cursor get_last_payment_info(p_req_id IN NUMBER) IS
737 select c.customer_id          customer_id,
738        c.customer_site_use_id site_use_id,
739        c.currency             rcpt_currency,
740        cr1.amount             last_payment_amount,
741        cr1.receipt_date       last_payment_date,
742        cr1.receipt_number     last_payment_number
743 from (
744 select a.customer_id,
745        a.customer_site_use_id,
746        a.currency,
747        max(b.cash_receipt_id) cash_receipt_id
748 from (
749 select cr.pay_from_customer customer_id,
750        nvl(cr.customer_site_use_id,-99) customer_site_use_id,
751        cr.currency_code currency,
752        cr.org_id,
753        max(cr.receipt_date) last_cash_receipt_date
754  from ar_cash_receipts cr,
755       ar_receivable_applications ra
756  where ra.request_id  = p_req_id
757    and ra.status = 'UNAPP'
758    and sign(ra.amount_applied) = 1
759    and cr.cash_receipt_id = ra.cash_receipt_id
760  group by cr.pay_from_customer,
761           nvl(cr.customer_site_use_id,-99),
762           cr.currency_code,
763           cr.org_id) a,
764       ar_cash_receipts b
765 where a.last_cash_receipt_date  = b.receipt_date
766  and   a.customer_id = b.pay_from_customer
767  and   a.customer_site_use_id = nvl(b.customer_site_use_id,-99)
768  and   a.org_id = b.org_id
769  and   a.currency  = b.currency_code
770 group by a.customer_id,
771        a.customer_site_use_id,
772        a.currency) c,
773       ar_cash_receipts cr1
774 WHERE cr1.cash_receipt_id = c.cash_receipt_id;
775 
776 cursor application_info(p_req_id IN NUMBER) IS
777 select ps.customer_id                            trx_customer_id,
778        nvl(ps.customer_site_use_id, -99)         trx_site_use_id,
779        ps.invoice_currency_code                  trx_currency,
780        ps.class                                  trx_class,
781        ra.apply_date                             apply_date,
782        sum(decode(sign(ps.due_date - ra.apply_date),-1,
783                     (ra.apply_date -
784                        nvl(ps.due_date ,ra.apply_date))
785                                * ra.amount_applied,null)) sum_app_amt_days_late,
786        sum(ra.earned_discount_taken)                      edisc_value,
787  	   sum(ra.unearned_discount_taken)                    uedisc_value,
788        sum(decode(sign(nvl(ra.earned_discount_taken,0)),
789 		                                    -1,-1,0,0,1)) edisc_count,
790 	   sum(decode(sign(nvl(ra.unearned_discount_taken,
794 		        (ps.due_date + nvl(rt.printing_lead_days,0)))
791 		                               0)),-1,-1,0,0,1))  uedisc_count,
792        sum(ra.amount_applied)                             amt_applied,
793        sum((ra.apply_date -
795                             *ra.amount_applied)           inv_inst_pmt_days_sum,
796        sum(DECODE(ps.class,'INV',
797                  DECODE((nvl(ps.discount_taken_earned,0)
798               + nvl(ps.discount_taken_unearned,0)),0,0,1),0))
799                                                          count_of_disc_inv_inst,
800        count(DECODE(ps.class,'INV',
801 	            ps.payment_schedule_id, null))   count_of_tot_inv_inst_paid,
802        count(decode(sign(ps.due_date-ra.apply_date),-1,
803                      ps.payment_schedule_id, null))  count_of_inv_inst_paid_late
804   from  ar_receivable_applications ra,
805         ar_payment_schedules ps,
806         ra_terms_b rt
807   where ra.request_id = p_req_id
808     and ra.status = 'APP'
809     and ps.payment_schedule_id = ra.applied_payment_schedule_id
810     and rt.term_id(+) = ps.term_id
811   group by ps.customer_id,
812            nvl(ps.customer_site_use_id, -99),
813            ps.invoice_currency_code,
814            ra.apply_date,
815            ps.class;
816 
817  /*
818    These variables are used  to maintain the cumulative totals for the
819    data in application_info cursor, which will be used for updating the
820    ar_trx_bal_summary column.
821   */
822 l_prev_trx_customer_id    number;
823 l_prev_trx_site_use_id    number;
824 l_prev_trx_currency       varchar2(30);
825 
826   /* 5690748 - keep lists of customers, sites, orgs, currencies
827      for later use in refresh_counts call */
828   l_customer_id_tab generic_id_type;
829   l_site_use_id_tab generic_id_type;
830   l_org_id_tab      generic_id_type;
831   l_currency_tab    currency_type;
832   l_row_counter     NUMBER := 0;
833   l_max_rows_per_update NUMBER := 1000;
834 BEGIN
835 
836    IF pg_debug = 'Y'
837    THEN
838     	debug ('AR_BUS_EVENT_SUB_PVT.Update_rcpt_app_info_for_req(+)');
839         debug ('p_req_id ='||p_req_id);
840    END IF;
841      FOR i in create_recept_info(p_req_id)
842       LOOP
843        -- AR_TRX_SUMMARY
844          UPDATE ar_trx_summary
845            set total_cash_receipts_value = nvl(total_cash_receipts_value,0) +
846                                                            i.receipt_amount,
847                total_cash_receipts_count = nvl(total_cash_receipts_count,0) + 1,
848                LAST_UPDATE_DATE  = sysdate,
849                LAST_UPDATED_BY   = FND_GLOBAL.user_id,
850                LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
851          where cust_account_id = i.customer_id
852            and site_use_id = nvl(i.site_use_id,-99)
853            and NVL(org_id,'-99') = NVL(p_org_id,-99)
854            and currency =   i.rcpt_currency
855            and as_of_date = i.as_of_date;
856 
857            IF sql%notfound then
858              INSERT INTO ar_trx_summary
859        	       (CUST_ACCOUNT_ID,
860                 SITE_USE_ID,
861                 ORG_ID,
862                 CURRENCY,
863                 AS_OF_DATE,
864                 last_update_date,
865                 last_updated_by,
866                 creation_date,
867                 created_by,
868                 last_update_login,
869                 total_cash_receipts_value,
870                 total_cash_receipts_count
871                 ) VALUES
872                (i.customer_id,
873                 nvl(i.site_use_id,-99),
874                 p_org_id,
875                 i.rcpt_currency,
876                 i.as_of_date,
877                 sysdate,
878                 fnd_global.user_id,
879                 sysdate,
880                 fnd_global.user_id,
881                 fnd_global.login_id,
882                 i.receipt_amount,
883                 1);
884             END IF;
885 
886       END LOOP;
887 
888 
889   FOR i in get_last_payment_info(p_req_id)
890        LOOP
891   UPDATE ar_trx_bal_summary
892           set last_payment_amount = decode(sign(i.last_payment_date-last_payment_date),
893                                          -1,last_payment_amount,i.last_payment_amount),
894               last_payment_date =decode(sign(i.last_payment_date-last_payment_date),
895                                             -1,last_payment_date,i.last_payment_date),
896               last_payment_number = decode(sign(i.last_payment_date-last_payment_date),
897                                             -1,last_payment_number,i.last_payment_number),
898               LAST_UPDATE_DATE  = sysdate,
899               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
900               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
901            where cust_account_id = i.customer_id
902              and site_use_id =  nvl(i.site_use_id,-99)
903              and NVL(org_id,'-99') = NVL(p_org_id,-99)
904              and currency = i.rcpt_currency;
905 
906              IF sql%notfound then
907                INSERT into  ar_trx_bal_summary
908                  (cust_account_id,
909                   site_use_id,
910                   org_id,
911                   currency,
912                   last_update_date,
913                   last_updated_by,
914                   creation_date,
915                   created_by,
916                   last_update_login,
917                   last_payment_amount,
918                   last_payment_date,
919                   last_payment_number
920                  )VALUES
921                  (i.customer_id,
922                   nvl(i.site_use_id,-99),
923                   p_org_id,
924                   i.rcpt_currency,
925                   sysdate,
929                   fnd_global.login_id,
926                   fnd_global.user_id,
927                   sysdate,
928                   fnd_global.user_id,
930                   i.last_payment_amount,
931                   i.last_payment_date,
932                   i.last_payment_number
933                   );
934              END IF;
935 
936 
937        END LOOP;
938 
939     FOR i IN application_info(p_req_id) LOOP
940 
941           UPDATE ar_trx_summary
942 	  SET           inv_paid_amount         = nvl(inv_paid_amount,0)
943 	                                                + nvl(i.amt_applied,0),
944 	                inv_inst_pmt_days_sum   = nvl(inv_inst_pmt_days_sum,0)
945 	                                               + nvl(i.inv_inst_pmt_days_sum,0),
946 	                total_earned_disc_value = nvl(total_earned_disc_value,0)
947 	                                               + nvl(i.edisc_value,0),
948 	                total_earned_disc_count = nvl(total_earned_disc_count,0)
949 	                                               + nvl(i.edisc_count,0),
950 	                total_unearned_disc_value = nvl(total_unearned_disc_value,0)
951 	                                               + nvl(i.uedisc_value,0),
952 	                total_unearned_disc_count = nvl(total_unearned_disc_count,0)
953 	                                               + nvl(i.uedisc_count,0),
954 	                sum_app_amt_days_late     = nvl(sum_app_amt_days_late,0)
955 	                                               + nvl(i.sum_app_amt_days_late,0),
956 	                sum_app_amt               = nvl(sum_app_amt,0) +
957 	                                                       nvl(i.amt_applied,0),
958 	                count_of_tot_inv_inst_paid = nvl(count_of_tot_inv_inst_paid,0)
959 	                                              + nvl(i.count_of_tot_inv_inst_paid,0),
960 	                count_of_inv_inst_paid_late = nvl(count_of_inv_inst_paid_late,0)
961 	                                              + nvl(i.count_of_inv_inst_paid_late,0),
962 	                count_of_disc_inv_inst = nvl(count_of_disc_inv_inst,0) +
963 	                                               nvl(i.count_of_disc_inv_inst,0),
964                         LAST_UPDATE_DATE  = sysdate,
965                         LAST_UPDATED_BY   = FND_GLOBAL.user_id,
966                         LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
967 	           WHERE cust_account_id = i.trx_customer_id
968 	             and site_use_id = i.trx_site_use_id
969 	             and currency = i.trx_currency
970 	             and as_of_date = i.apply_date
971                      and NVL(org_id,'-99') = NVL(p_org_id,-99);
972 
973         /* 5690748 - storing customer, site, currency, and org
974            for use in refresh_counts */
975 
976       IF   ((nvl(l_prev_trx_customer_id,-99) <> i.trx_customer_id)
977          OR (nvl(l_prev_trx_site_use_id,-999) <> i.trx_site_use_id)
978          OR (nvl(l_prev_trx_currency,'XYZ~') <> i.trx_currency))
979       THEN
980         l_customer_id_tab(l_row_counter) := i.trx_customer_id;
981         l_site_use_id_tab(l_row_counter) := i.trx_site_use_id;
982         l_currency_tab(l_row_counter)    := i.trx_currency;
986         IF pg_debug = 'Y'
983         l_org_id_tab(l_row_counter)      := p_org_id;
984         l_row_counter := l_row_counter + 1;
985 
987         THEN
988            debug('new row: ' || i.trx_customer_id || ':' ||
989                i.trx_site_use_id || ':' || i.trx_currency);
990         END IF;
991 
992         /* 5690748 check if l_row_counter exceeds max (from profile) */
993         IF l_row_counter >= l_max_rows_per_update
994         THEN
995            IF pg_debug = 'Y'
996            THEN
997               debug('total rows exceeds threshold.. executing update for block');
998            END IF;
999 
1000            refresh_counts(l_customer_id_tab,
1001                           l_site_use_id_tab,
1002                           l_currency_tab,
1003                           l_org_id_tab);
1004 
1005 
1006            l_row_counter := 0;
1007            l_customer_id_tab.delete;
1008            l_site_use_id_tab.delete;
1009            l_currency_tab.delete;
1010            l_org_id_tab.delete;
1011 
1012         END IF;
1013       END IF;
1014 
1015         l_prev_trx_customer_id   := i.trx_customer_id;
1016         l_prev_trx_site_use_id   := i.trx_site_use_id;
1017         l_prev_trx_currency      := i.trx_currency;
1018 
1019     END LOOP;
1020 
1021     /* 5690748 correct open and past_due columns */
1022     IF l_row_counter > 0
1023     THEN
1024        refresh_counts(l_customer_id_tab,
1025                       l_site_use_id_tab,
1026                       l_currency_tab,
1027                       l_org_id_tab);
1028 
1029        l_row_counter := 0;
1030        l_customer_id_tab.delete;
1031        l_site_use_id_tab.delete;
1032        l_currency_tab.delete;
1033        l_org_id_tab.delete;
1034     END IF;
1035 
1036     IF pg_debug = 'Y'
1037     THEN
1038     	debug ('AR_BUS_EVENT_SUB_PVT.Update_rcpt_app_info_for_req(-)');
1039    END IF;
1040 END Update_rcpt_app_info_for_req;
1041 
1042 FUNCTION Inv_Complete
1043 ( p_subscription_guid In RAW
1044 , p_event IN OUT NOCOPY WF_EVENT_T
1045 )
1046 RETURN VARCHAR2 IS
1047 
1048   l_ps_tab ps_tab_type;
1049   l_trx_summary_hist AR_TRX_SUMMARY_HIST%rowtype;
1050   CURSOR get_trx_history(p_cust_trx_id  IN NUMBER) IS
1051   SELECT *
1052   FROM AR_TRX_SUMMARY_HIST
1053   WHERE customer_trx_id = p_cust_trx_id
1054     and nvl(complete_flag,'N') = 'N'
1055     and amount_due_original is not null
1056     for update;
1057 
1058   CURSOR lock_ps (cust_trx_id IN NUMBER) IS
1059   SELECT * from ar_payment_schedules
1060   WHERE customer_trx_id = cust_trx_id
1061   FOR UPDATE;
1062 
1063   i                  INTEGER;
1064   l_key         VARCHAR2(240) := p_event.GetEventKey();
1065   l_payment_schedule_id   NUMBER(15);
1066   l_customer_trx_id  NUMBER;
1067   l_org_id           NUMBER;
1068   l_user_id          NUMBER;
1069   l_resp_id          NUMBER;
1070   l_application_id   NUMBER;
1071   l_security_gr_id   NUMBER;
1072   l_counter          NUMBER;
1073   l_history_exists_flag  BOOLEAN;
1074   l_ps_exists         BOOLEAN;
1075   l_history_rec      ar_trx_summary_hist%rowtype;
1076   l_tot_inv_amt      NUMBER;
1077   l_inv_inst_count   NUMBER;
1078  BEGIN
1079 
1083    END IF;
1080     IF pg_debug = 'Y'
1081     THEN
1082     	debug ('AR_BUS_EVENT_SUB_PVT.Inv_Complete(+)');
1084   l_customer_trx_id := p_event.GetValueForParameter('CUSTOMER_TRX_ID');
1085   l_org_id          := p_event.GetValueForParameter('ORG_ID');
1086   l_user_id         := p_event.GetValueForParameter('USER_ID');
1087   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
1088   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
1089   l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
1090    IF pg_debug = 'Y'
1091    THEN
1092         debug ('l_customer_trx_id ='||l_customer_trx_id);
1093         debug ('l_org_id ='||l_org_id);
1094         debug ('l_user_id ='||l_user_id);
1095         debug ('l_resp_id ='||l_resp_id);
1096         debug ('l_application_id ='||l_application_id);
1097         debug ('l_security_gr_id ='||l_security_gr_id);
1098    END IF;
1099    SAVEPOINT  Inv_Complete_Event;
1100    --
1101    --set the application context.
1102    --
1103   fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
1104   mo_global.init('AR');
1105   mo_global.set_policy_context('S',l_org_id);
1106    --
1107    -- Acquire locks on the payment schedule record so that
1108    -- the record is not changed while this subscription is
1109    -- executed.
1110    --
1111   OPEN lock_ps (l_customer_trx_id);
1112     i := 1;
1113     l_counter := 0;
1114    LOOP
1115    FETCH lock_ps INTO  l_ps_tab(i);
1116 
1117    IF lock_ps%NOTFOUND  THEN
1118      IF l_counter = 0 THEN
1119        l_ps_exists := FALSE;
1120      ELSE
1121        l_ps_exists := TRUE;
1122      END IF;
1123 
1124      EXIT;
1125    END IF;
1126        i := i + 1;
1127        l_counter := l_counter + 1;
1128    END LOOP;
1129   CLOSE lock_ps;
1130 
1131    --
1132    --Update the transaction history table
1133    --set the complete_flag = 'Y'
1134    --if history records exist.
1135    --
1136    UPDATE ar_trx_summary_hist
1137       set complete_flag = 'Y',
1138           LAST_UPDATE_DATE  = sysdate,
1139           LAST_UPDATED_BY   = FND_GLOBAL.user_id,
1140           LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
1141    WHERE customer_trx_id = l_customer_trx_id
1142      and nvl(complete_flag,'N') = 'N';
1143 
1144 
1145    IF SQL%NOTFOUND THEN
1146       l_history_exists_flag := FALSE;
1147    ELSE
1148       l_history_exists_flag := TRUE;
1149    END IF;
1150 
1151 
1152    IF l_ps_exists  THEN
1153     --Sweep thru the l_ps_tab table to update the summary table
1154     --and ignore the history.
1155     l_tot_inv_amt := 0;
1156     l_inv_inst_count :=  l_ps_tab.COUNT;
1157 
1158      FOR j in 1..l_ps_tab.COUNT
1159        LOOP
1160        --
1161        -- Update the AR_TRX_BAL_SUMMARY table
1162        --
1163         UPDATE ar_trx_bal_summary
1164           set BEST_CURRENT_RECEIVABLES
1165                        = nvl(BEST_CURRENT_RECEIVABLES,0)
1166                               +DECODE(sign(l_ps_tab(j).due_date - sysdate),-1,0,
1167                               (l_ps_tab(j).amount_due_original
1171                              +l_ps_tab(j).amount_due_original
1168                                    +nvl(l_ps_tab(j).amount_adjusted,0))),
1169               OP_INVOICES_VALUE
1170                        = nvl(OP_INVOICES_VALUE,0)
1172                                    +nvl(l_ps_tab(j).amount_adjusted,0),
1173               OP_INVOICES_COUNT = nvl(OP_INVOICES_COUNT,0) + 1,
1174               PAST_DUE_INV_VALUE = nvl(PAST_DUE_INV_VALUE,0) +
1175                                    decode(sign(l_ps_tab(j).due_date - trunc(sysdate)),-1,
1176                                          (l_ps_tab(j).amount_due_original
1177                                        +nvl(l_ps_tab(j).amount_adjusted,0)),0),
1178               PAST_DUE_INV_INST_COUNT = nvl(PAST_DUE_INV_INST_COUNT,0) +
1179                                    decode(sign(l_ps_tab(j).due_date - trunc(sysdate)),-1,1,0),
1180               LAST_UPDATE_DATE  = sysdate,
1181               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
1182               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
1183          WHERE cust_account_id = l_ps_tab(j).customer_id
1184            and site_use_id = nvl(l_ps_tab(j).customer_site_use_id,-99)
1185            and currency = l_ps_tab(j).invoice_currency_code
1186            and NVL(org_id,'-99') = NVL(l_org_id,-99);
1187 
1188          IF SQL%NOTFOUND THEN
1189 
1190            INSERT INTO ar_trx_bal_summary
1191             (CUST_ACCOUNT_ID,
1192              SITE_USE_ID,
1193              ORG_ID,
1194              CURRENCY,
1195              last_update_date,
1196              last_updated_by,
1197              creation_date,
1198              created_by,
1199              last_update_login,
1200              BEST_CURRENT_RECEIVABLES,
1201              OP_INVOICES_VALUE,
1202              OP_INVOICES_COUNT,
1203              PAST_DUE_INV_VALUE,
1204              PAST_DUE_INV_INST_COUNT)
1205              VALUES
1206             ( l_ps_tab(j).customer_id,
1207               nvl(l_ps_tab(j).customer_site_use_id,-99),
1208               l_org_id,
1209               l_ps_tab(j).invoice_currency_code,
1210               sysdate,
1211               fnd_global.user_id,
1212               sysdate,
1213               fnd_global.user_id,
1214               fnd_global.login_id,
1215               DECODE(sign(l_ps_tab(j).due_date - sysdate),-1,0,
1216                 (l_ps_tab(j).amount_due_original
1217                   +nvl(l_ps_tab(j).amount_adjusted,0))),
1218               l_ps_tab(j).amount_due_original+nvl(l_ps_tab(j).amount_adjusted,0),
1219               1,
1220               decode(sign(l_ps_tab(j).due_date - trunc(sysdate)),-1,
1221                                          (l_ps_tab(j).amount_due_original
1222                                        +nvl(l_ps_tab(j).amount_adjusted,0)),0),
1223               decode(sign(l_ps_tab(j).due_date - trunc(sysdate)),-1,1,null));
1224 
1225            END IF;
1226        --
1227        -- Update the AR_TRX_SUMMARY table
1228        --
1229         l_tot_inv_amt := l_tot_inv_amt +  l_ps_tab(j).amount_due_original;
1230 
1231         UPDATE ar_trx_summary
1232           set OP_BAL_HIGH_WATERMARK = nvl(OP_BAL_HIGH_WATERMARK,0) +
1233                                     l_ps_tab(j).amount_due_original+
1234                                     nvl(l_ps_tab(j).amount_adjusted,0),
1235               TOTAL_INVOICES_VALUE = nvl(TOTAL_INVOICES_VALUE,0) +
1236                                      l_ps_tab(j).amount_due_original,
1237               TOTAL_INVOICES_COUNT = nvl(TOTAL_INVOICES_COUNT,0) + 1,
1238               LARGEST_INV_AMOUNT = DECODE(sign(l_inv_inst_count -j),0,
1239                                      DECODE(sign(l_tot_inv_amt- nvl(LARGEST_INV_AMOUNT,0)),
1240                                              1,l_tot_inv_amt,LARGEST_INV_AMOUNT),LARGEST_INV_AMOUNT),
1241               LARGEST_INV_DATE = l_ps_tab(j).trx_date,
1242               LARGEST_INV_CUST_TRX_ID = DECODE(sign(l_inv_inst_count -j),0,
1243                                      DECODE(sign(l_tot_inv_amt- nvl(LARGEST_INV_AMOUNT,0)),
1244                                              1,l_ps_tab(j).customer_trx_id,LARGEST_INV_CUST_TRX_ID),
1245                                                 LARGEST_INV_CUST_TRX_ID),
1246               DAYS_CREDIT_GRANTED_SUM = nvl(DAYS_CREDIT_GRANTED_SUM,0) +
1247                                             ((l_ps_tab(j).amount_due_original
1248                                              + nvl(l_ps_tab(j).amount_adjusted,0))
1249                                                 * (l_ps_tab(j).due_date
1250                                                     - l_ps_tab(j).trx_date)),
1251               LAST_UPDATE_DATE  = sysdate,
1252               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
1253               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
1254          WHERE cust_account_id = l_ps_tab(j).customer_id
1255            and site_use_id = nvl(l_ps_tab(j).customer_site_use_id,-99)
1256            and currency = l_ps_tab(j).invoice_currency_code
1257            and NVL(org_id,'-99') = NVL(l_org_id,-99)
1258            and as_of_date = l_ps_tab(j).trx_date;
1259 
1260         IF SQL%NOTFOUND THEN
1261 
1262           INSERT INTO ar_trx_summary
1263             (CUST_ACCOUNT_ID,
1264              SITE_USE_ID,
1265              ORG_ID,
1266              CURRENCY,
1267              AS_OF_DATE,
1268              last_update_date,
1269              last_updated_by,
1270              creation_date,
1271              created_by,
1272              last_update_login,
1273              OP_BAL_HIGH_WATERMARK,
1274              TOTAL_INVOICES_VALUE,
1275              TOTAL_INVOICES_COUNT,
1276              LARGEST_INV_AMOUNT,
1277              LARGEST_INV_DATE,
1278              LARGEST_INV_CUST_TRX_ID,
1279              DAYS_CREDIT_GRANTED_SUM)
1280              VALUES
1281              (l_ps_tab(j).customer_id,
1282               nvl(l_ps_tab(j).customer_site_use_id,-99),
1283               l_org_id,
1284               l_ps_tab(j).invoice_currency_code,
1288               sysdate,
1285               l_ps_tab(j).trx_date,
1286               sysdate,
1287               fnd_global.user_id,
1289               fnd_global.user_id,
1290               fnd_global.login_id,
1291               l_ps_tab(j).amount_due_original+
1292                                     nvl(l_ps_tab(j).amount_adjusted,0),
1293               l_ps_tab(j).amount_due_original,
1294               1,
1295               DECODE(sign(l_inv_inst_count -j),0,l_tot_inv_amt,null),
1296               l_ps_tab(j).trx_date,
1297               DECODE(sign(l_inv_inst_count -j),0,l_ps_tab(j).customer_trx_id,null),
1298               ((l_ps_tab(j).amount_due_original+ nvl(l_ps_tab(j).amount_adjusted,0))
1299                                 * (l_ps_tab(j).due_date - l_ps_tab(j).trx_date))
1300               );
1301            END IF;
1302       END LOOP;
1303 
1304    ELSE --l_ps_exists
1305      --if no payment schedule exits for the given customer_trx_id
1306      --then we do not update the summary table.
1307      null;
1308    END IF; --l_ps_exists
1309 
1310     IF pg_debug = 'Y'
1311     THEN
1312         debug ('AR_BUS_EVENT_SUB_PVT.Inv_Complete(-)');
1313     END IF;
1314  RETURN 'SUCCESS';
1315 
1316  EXCEPTION
1317     WHEN OTHERS  THEN
1318      ROLLBACK TO Inv_Complete_Event;
1319 
1320      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
1321      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
1322      FND_MSG_PUB.ADD;
1323 
1324      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'INV_COMPLETE', p_event.getEventName(), p_subscription_guid);
1325      WF_EVENT.setErrorInfo(p_event, 'ERROR');
1326 
1327      RETURN 'ERROR';
1328 
1329  END Inv_Complete;
1330 
1331 
1332 FUNCTION Inv_InComplete
1333 ( p_subscription_guid In RAW
1334 , p_event IN OUT NOCOPY WF_EVENT_T
1335 )
1336 RETURN VARCHAR2 IS
1337   l_ps_tab ps_tab_type;
1338   l_trx_summary_hist AR_TRX_SUMMARY_HIST%rowtype;
1339 
1340   CURSOR get_trx_history(p_hist_id  IN NUMBER) IS
1341   SELECT *
1342   FROM AR_TRX_SUMMARY_HIST
1343   WHERE history_id = p_hist_id;
1344    -- and nvl(complete_flag,'N') = 'N';
1345 
1346   CURSOR get_curr_larg_inv_info(p_cust_account_id IN NUMBER,
1347                                 p_site_use_id IN NUMBER,
1348                                 p_currency IN VARCHAR2,
1349                                 p_as_of_date IN DATE) IS
1350   SELECT LARGEST_INV_CUST_TRX_ID
1351   FROM ar_trx_summary
1352   WHERE cust_account_id = p_cust_account_id
1353     and site_use_id = p_site_use_id
1354     and currency = p_currency
1355     and as_of_date = p_as_of_date
1356    FOR UPDATE;
1357 
1358   i                  INTEGER;
1359   l_key         VARCHAR2(240) := p_event.GetEventKey();
1360   l_payment_schedule_id   NUMBER(15);
1361   l_customer_trx_id  NUMBER;
1362   l_org_id           NUMBER;
1363   l_user_id          NUMBER;
1364   l_resp_id          NUMBER;
1365   l_application_id   NUMBER;
1366   l_security_gr_id   NUMBER;
1367   l_counter          NUMBER;
1368   l_history_exists_flag  BOOLEAN;
1369   l_ps_exists        BOOLEAN;
1370   l_history_rec      ar_trx_summary_hist%rowtype;
1371   l_tot_inv_amt      NUMBER;
1372   l_inv_inst_count   NUMBER;
1373   l_larg_inv_cust_trx_id  NUMBER;
1374   l_text         VARCHAR2(2000);
1375   l_history_id     NUMBER(15);
1376   v_cursor1  NUMBER;
1377   l_larg_inv_amt    NUMBER;
1378   v_return_code   NUMBER;
1379   v_NumRows       NUMBER;
1380 BEGIN
1381 
1382     IF pg_debug = 'Y'
1383     THEN
1384         debug ('AR_BUS_EVENT_SUB_PVT.Inv_InComplete(+)');
1385     END IF;
1386   l_customer_trx_id := p_event.GetValueForParameter('CUSTOMER_TRX_ID');
1387   l_payment_schedule_id := p_event.GetValueForParameter('PAYMENT_SCHEDULE_ID');
1388   l_history_id      := p_event.GetValueForParameter('HISTORY_ID');
1389   l_org_id          := p_event.GetValueForParameter('ORG_ID');
1390   l_user_id         := p_event.GetValueForParameter('USER_ID');
1391   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
1392   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
1393   l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
1394    SAVEPOINT  Inv_InComplete_Event;
1395     IF pg_debug = 'Y'
1396     THEN
1397        debug ('l_customer_trx_id= '||l_customer_trx_id);
1398        debug ('l_payment_schedule_id= '||l_payment_schedule_id);
1399        debug ('l_history_id= '||l_history_id);
1400        debug ('l_org_id= '||l_org_id);
1401        debug ('l_user_id= '||l_user_id);
1402        debug ('l_resp_id= '||l_resp_id);
1403        debug ('l_application_id= '||l_application_id);
1404        debug ('l_security_gr_id= '||l_security_gr_id);
1405     END IF;
1406    --
1407    --set the application context.
1408    --
1409   fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
1410   mo_global.init('AR');
1411   mo_global.set_policy_context('S',l_org_id);
1412    --
1413    -- No need to acquire locks on the payment schedule record
1414    -- because it would not exist in the database as the Incomplete
1415    -- event deletes the payment schedules.
1416    --
1417 
1418    -- In case of the Incomplete event on a PS of an inv, there will be
1419    -- no future events(or in other words history) on this payment schedule
1420    -- as this payment schedule would have been deleted. And subscriptions
1421    -- for all the earlier events on this PS would have been executed by now.
1422    --
1423    -- Update the transaction history table set the complete_flag = 'Y'
1424    -- if history records exist.
1425    --
1426     UPDATE ar_trx_summary_hist
1427       set complete_flag = 'Y',
1428           LAST_UPDATE_DATE  = sysdate,
1429           LAST_UPDATED_BY   = FND_GLOBAL.user_id,
1430           LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
1434 
1431     WHERE payment_schedule_id = l_payment_schedule_id
1432       and history_id = l_history_id
1433       and nvl(complete_flag,'N') = 'N';
1435 
1436     IF SQL%NOTFOUND THEN
1437        l_history_exists_flag := FALSE;
1438     ELSE
1439        l_history_exists_flag := TRUE;
1440     END IF;
1441 
1442    IF l_history_exists_flag  THEN
1443 
1444     OPEN get_trx_history(l_history_id) ;
1445     FETCH get_trx_history INTO l_history_rec;
1446     CLOSE get_trx_history ;
1447 
1448     l_tot_inv_amt := 0;
1449 
1450        --
1451        -- Update the AR_TRX_BAL_SUMMARY table
1452        --
1453         UPDATE ar_trx_bal_summary
1454           set BEST_CURRENT_RECEIVABLES
1455                        = nvl(BEST_CURRENT_RECEIVABLES,0)
1456                               -DECODE(sign(l_history_rec.due_date - sysdate),-1,0,
1457                               (l_history_rec.amount_due_original
1458                                    +nvl(l_history_rec.amount_adjusted,0))),
1459               OP_INVOICES_VALUE
1460                        = nvl(OP_INVOICES_VALUE,0)
1461                              -l_history_rec.amount_due_original
1462                                    -nvl(l_history_rec.amount_adjusted,0),
1463               OP_INVOICES_COUNT = nvl(OP_INVOICES_COUNT,0) - 1,
1464               PAST_DUE_INV_VALUE = nvl(PAST_DUE_INV_VALUE,0) -
1465                                    (decode(sign(l_history_rec.due_date - trunc(sysdate)),-1,
1466                                          (l_history_rec.amount_due_original
1467                                        +nvl(l_history_rec.amount_adjusted,0)),0)),
1468               PAST_DUE_INV_INST_COUNT = nvl(PAST_DUE_INV_INST_COUNT,0) -
1469                                    decode(sign(l_history_rec.due_date - trunc(sysdate)),-1,1,0),
1470               LAST_UPDATE_DATE  = sysdate,
1471               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
1472               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
1473          WHERE cust_account_id = l_history_rec.customer_id
1474            and site_use_id = nvl(l_history_rec.site_use_id,-99)
1475            and currency = l_history_rec.currency_code
1476            and NVL(org_id,'-99') = NVL(l_org_id,-99);
1477 
1478        -- No need to check the SQL%NOTFOUND case because Incomplete
1479        -- event would fire only after a  complete event has fired.
1480 
1481 
1482        --
1483        -- Update the AR_TRX_SUMMARY table
1484        --
1485 
1486        --
1487        --Get the info about the largest invoice
1488        --
1489        --We need to do this calc only for one installment of
1490        --invoice. Since the l_history_rec.installments will always have a non-zero
1491        --value only for the first installment so we are using it to identify the
1492        --the first installment and do the largest inv calc.
1493        --
1494       IF nvl(l_history_rec.installments,0) > 0  THEN
1495 
1496        OPEN get_curr_larg_inv_info(l_history_rec.customer_id,
1497                                 nvl(l_history_rec.site_use_id,-99),
1498                                 l_history_rec.currency_code,
1499                                 l_history_rec.trx_date);
1500          FETCH get_curr_larg_inv_info INTO l_larg_inv_cust_trx_id;
1501          IF   nvl(l_larg_inv_cust_trx_id,0) = l_history_rec.customer_trx_id
1502            THEN
1503            --get the new largest invoice by hitting the
1504            --payment schedule table
1505            l_text :=   'SELECT CUSTOMER_TRX_ID, inv_amount
1506                         FROM (
1507                         Select trx_date,customer_trx_id,
1508                                sum(amount_due_original) inv_amount,
1509                         RANK() OVER (ORDER BY sum(amount_due_original) desc,
1510                                customer_trx_id desc) rank_amt
1511                         FROM ar_payment_schedules
1512                         WHERE customer_id = :customer_id_bind
1513                            and customer_site_use_id = :customer_site_use_id_bind
1514                            and invoice_currency_code = :invoice_currency_code_bind
1515                            and trx_date = :trx_date_bind
1516                           group by trx_date,customer_trx_id)
1517                          where rank_amt = 1 ';
1518             v_cursor1 := dbms_sql.open_cursor;
1519             dbms_sql.parse(v_cursor1,l_text,DBMS_SQL.V7);
1520 
1521             -- 5217077 (One-off:5096808). Bind the bind-variables here.
1522             dbms_sql.bind_variable(v_cursor1, ':customer_id_bind', l_history_rec.customer_id);
1523             dbms_sql.bind_variable(v_cursor1, ':customer_site_use_id_bind', l_history_rec.site_use_id);
1524             dbms_sql.bind_variable(v_cursor1, ':invoice_currency_code_bind', l_history_rec.currency_code);
1525             dbms_sql.bind_variable(v_cursor1, ':trx_date_bind', l_history_rec.trx_date);
1526 
1527             dbms_sql.define_column(v_cursor1,1,l_larg_inv_cust_trx_id);
1528             dbms_sql.define_column(v_cursor1,2,l_larg_inv_amt);
1529             v_return_code := dbms_sql.execute(v_cursor1);
1530 
1531             v_NumRows := DBMS_SQL.FETCH_ROWS(v_cursor1);
1532             DBMS_SQL.COLUMN_VALUE(v_cursor1,1,l_larg_inv_cust_trx_id);
1533             DBMS_SQL.COLUMN_VALUE(v_cursor1,2,l_larg_inv_amt);
1534             dbms_sql.close_cursor(v_cursor1);
1535 
1536          END IF;
1537         CLOSE get_curr_larg_inv_info;
1538       END IF;
1539 
1540      UPDATE ar_trx_summary
1541         set OP_BAL_HIGH_WATERMARK = nvl(OP_BAL_HIGH_WATERMARK,0) -
1542                                         (l_history_rec.amount_due_original+
1543                                            nvl(l_history_rec.amount_adjusted,0)),
1544            TOTAL_INVOICES_VALUE = nvl(TOTAL_INVOICES_VALUE,0) -
1545                                        l_history_rec.amount_due_original,
1546            TOTAL_INVOICES_COUNT = nvl(TOTAL_INVOICES_COUNT,0) - 1,
1547            LARGEST_INV_AMOUNT = DECODE(sign(nvl(l_history_rec.installments,0)),
1551            LARGEST_INV_DATE = LARGEST_INV_DATE,
1548                                  1, DECODE(sign(nvl(LARGEST_INV_CUST_TRX_ID,0)-
1549                                                 l_history_rec.customer_trx_id),
1550                                        0,l_larg_inv_amt,LARGEST_INV_AMOUNT),LARGEST_INV_AMOUNT),
1552            LARGEST_INV_CUST_TRX_ID = DECODE(sign(nvl(l_history_rec.installments,0)),
1553                                     1, DECODE(sign(nvl(LARGEST_INV_CUST_TRX_ID,0)-
1554                                                    l_history_rec.customer_trx_id),
1555                                        0,l_larg_inv_cust_trx_id,
1556                                             LARGEST_INV_CUST_TRX_ID),LARGEST_INV_AMOUNT),
1557            DAYS_CREDIT_GRANTED_SUM = nvl(DAYS_CREDIT_GRANTED_SUM,0) -
1558                                             ((l_history_rec.amount_due_original
1559                                              + l_history_rec.amount_adjusted)
1560                                                 * (l_history_rec.due_date
1561                                                     - l_history_rec.trx_date)),
1562               LAST_UPDATE_DATE  = sysdate,
1563               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
1564               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
1565          WHERE cust_account_id = l_history_rec.customer_id
1566            and site_use_id = nvl(l_history_rec.site_use_id,-99)
1567            and currency = l_history_rec.currency_code
1568            and NVL(org_id,'-99') = NVL(l_org_id,-99)
1569            and as_of_date = l_history_rec.trx_date;
1570 
1571    ELSE  --l_history_exists_flag is false
1572      null;
1573    END IF;
1574 
1575     IF pg_debug = 'Y'
1576     THEN
1577         debug ('AR_BUS_EVENT_SUB_PVT.Inv_InComplete(-)');
1578     END IF;
1579   Return 'SUCCESS';
1580 EXCEPTION
1581     WHEN OTHERS  THEN
1582      ROLLBACK TO Inv_InComplete_Event;
1583 
1584      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
1585      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
1586      FND_MSG_PUB.ADD;
1587 
1588      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'INV_INCOMPLETE', p_event.getEventName(), p_subscription_guid);
1589      WF_EVENT.setErrorInfo(p_event, 'ERROR');
1590 
1591      RETURN 'ERROR';
1592 
1593 END Inv_InComplete;
1594 
1595 FUNCTION Inv_Modify
1596 ( p_subscription_guid In RAW
1597 , p_event IN OUT NOCOPY WF_EVENT_T
1598 )
1599 RETURN VARCHAR2 IS
1600 
1601 CURSOR get_trx_history(p_hist_id  IN NUMBER) IS
1602  SELECT *
1603  FROM AR_TRX_SUMMARY_HIST
1604  WHERE history_id = p_hist_id;
1605 --  and nvl(complete_flag,'N') = 'N';
1606 
1607 CURSOR get_trx_history2 (p_history_id IN NUMBER) IS
1608 select *
1609 from ar_trx_summary_hist
1610 where previous_history_id = p_history_id;
1611 
1612 CURSOR get_ps_info (p_ps_id IN NUMBER) IS
1613 select due_date, amount_in_dispute
1614 from ar_payment_schedules
1615 where payment_schedule_id = p_ps_id;
1616 
1617   l_key         VARCHAR2(240) := p_event.GetEventKey();
1618   l_payment_schedule_id   NUMBER(15);
1619   l_org_id           NUMBER;
1620   l_user_id          NUMBER;
1621   l_resp_id          NUMBER;
1622   l_application_id   NUMBER;
1623   l_security_gr_id   NUMBER;
1624   l_history_exists_flag  BOOLEAN;
1625   l_ps_exists        BOOLEAN;
1626   l_history_rec      ar_trx_summary_hist%rowtype;
1627   l_history_rec2     ar_trx_summary_hist%rowtype;
1628   l_tot_inv_amt      NUMBER;
1629   l_history_id       NUMBER(15);
1630 
1631   l_due_date_change  VARCHAR2(1);
1632   l_inv_dispute_count  NUMBER;
1633 BEGIN
1634 
1635     IF pg_debug = 'Y'
1636     THEN
1637         debug ('AR_BUS_EVENT_SUB_PVT.Inv_Modify(+)');
1638     END IF;
1639   l_payment_schedule_id := p_event.GetValueForParameter('PAYMENT_SCHEDULE_ID');
1640   l_history_id      := p_event.GetValueForParameter('HISTORY_ID');
1641   l_org_id          := p_event.GetValueForParameter('ORG_ID');
1642   l_user_id         := p_event.GetValueForParameter('USER_ID');
1643   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
1644   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
1645   l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
1646 
1647     IF pg_debug = 'Y'
1648     THEN
1649        debug ('l_payment_schedule_id= '||l_payment_schedule_id);
1650        debug ('l_history_id= '||l_history_id);
1651        debug ('l_org_id= '||l_org_id);
1652        debug ('l_user_id= '||l_user_id);
1653        debug ('l_resp_id= '||l_resp_id);
1654        debug ('l_application_id= '||l_application_id);
1655        debug ('l_security_gr_id= '||l_security_gr_id);
1656     END IF;
1657   Savepoint Inv_Modify_Event;
1658    --
1659    --set the application context.
1660    --
1661   fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
1662   mo_global.init('AR');
1663   mo_global.set_policy_context('S',l_org_id);
1664    --Stamp the history record for the modification.
1665     /*bug#5484606-------------------------------------------------------------------+
1666 |cuddagir Added Exception Handling for update statement and moved the logic for   |
1667 |setting the flag inside the exception handling.                                |
1668 |Have modified the earlier logic to make sure that the exceptions raised prior  |
1669 |to calling the update statement are not trapped in "IF SQL%NOTFOUND" condition |
1670 |Changes Start                                                                  |
1671 +------------------------------------------------------------------------------*/
1672 
1673   BEGIN
1674 
1675    --Stamp the history record for the modification.
1676     UPDATE ar_trx_summary_hist
1677       set complete_flag = 'Y',
1678           LAST_UPDATE_DATE  = sysdate,
1679           LAST_UPDATED_BY   = FND_GLOBAL.user_id,
1680           LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
1684 
1681     WHERE payment_schedule_id = l_payment_schedule_id
1682       and history_id = l_history_id
1683       and nvl(complete_flag,'N') = 'N';
1685        l_history_exists_flag := TRUE;
1686 
1687   EXCEPTION
1688 
1689    WHEN NO_DATA_FOUND THEN
1690     l_history_exists_flag := FALSE;
1691 
1692   END;
1693 
1694    IF l_history_exists_flag  THEN
1695 
1696       OPEN get_trx_history(l_history_id) ;
1697       FETCH get_trx_history INTO l_history_rec;
1698       CLOSE get_trx_history ;
1699 
1700 /*bug#5484606-------------------------------------------------------------------+
1701 |cuddagir changed the condition for opening the cursor "get_trx_history2" with    |
1702 |to get the data always from payment schedule                                   |
1703 |Changes Start                                                                  |
1704 +------------------------------------------------------------------------------*/
1705 
1706            OPEN get_ps_info(l_payment_schedule_id);
1707            FETCH get_ps_info INTO
1708                    l_history_rec2.due_date,
1709                    l_history_rec2.amount_in_dispute;
1710                l_ps_exists := true;
1711              IF get_ps_info%NOTFOUND THEN
1712                l_ps_exists := false;
1713              END IF;
1714 
1715            CLOSE get_ps_info;
1716 
1717 
1718       IF l_history_rec.due_date >= sysdate and
1719           l_history_rec2.due_date < sysdate THEN
1720          l_due_date_change :=  '-';
1721       ELSIF l_history_rec.due_date < sysdate and
1722           l_history_rec2.due_date >= sysdate THEN
1723           l_due_date_change :=  '+';
1724       END IF;
1725 
1726       IF nvl(l_history_rec2.amount_in_dispute,0) > 0
1727        AND nvl(l_history_rec.amount_in_dispute,0) = 0 THEN
1728            l_inv_dispute_count := 1;
1729       ELSIF nvl(l_history_rec2.amount_in_dispute,0) = 0
1730        AND nvl(l_history_rec.amount_in_dispute,0) > 0  THEN
1731          l_inv_dispute_count := -1;
1732       END IF;
1733 
1734         UPDATE ar_trx_bal_summary
1735           set BEST_CURRENT_RECEIVABLES
1736                       = nvl(BEST_CURRENT_RECEIVABLES,0)
1737                               -DECODE(l_due_date_change,'+',
1738                                   (l_history_rec.amount_due_original
1739                                    +nvl(l_history_rec.amount_adjusted,0)),
1740                                     '-',
1741                                     -(l_history_rec.amount_due_original
1742                                    +nvl(l_history_rec.amount_adjusted,0)),0),
1743               PAST_DUE_INV_VALUE = nvl(PAST_DUE_INV_VALUE,0)
1744                                    - DECODE(l_due_date_change,'+',
1745                                       -(l_history_rec.amount_due_remaining),
1746                                        '-',
1747                                       (l_history_rec.amount_due_remaining),0),
1748               PAST_DUE_INV_INST_COUNT = nvl(PAST_DUE_INV_INST_COUNT,0) -
1749                                         - DECODE(l_due_date_change,'+',
1750                                                      -1, '-',1,0),
1751               INV_AMT_IN_DISPUTE = nvl(INV_AMT_IN_DISPUTE,0)
1752                                         +(nvl(l_history_rec2.amount_in_dispute,0)
1753                                             - nvl(l_history_rec.amount_in_dispute,0)),
1754               DISPUTED_INV_COUNT = nvl(DISPUTED_INV_COUNT,0)
1755                                      + nvl(l_inv_dispute_count,0),
1756               LAST_UPDATE_DATE  = sysdate,
1757               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
1758               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
1759          WHERE cust_account_id = l_history_rec.customer_id
1760            and site_use_id = nvl(l_history_rec.site_use_id,-99)
1761            and currency = l_history_rec.currency_code
1762            and NVL(org_id,'-99') = NVL(l_org_id,-99);
1763 
1764      UPDATE ar_trx_summary
1765        set  DAYS_CREDIT_GRANTED_SUM = nvl(DAYS_CREDIT_GRANTED_SUM,0) +
1766                                             ((l_history_rec.amount_due_original
1767                                              + l_history_rec.amount_adjusted)
1768                                                 * (l_history_rec2.due_date -l_history_rec.due_date)),
1769               LAST_UPDATE_DATE  = sysdate,
1770               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
1771               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
1772          WHERE cust_account_id = l_history_rec.customer_id
1773            and site_use_id = nvl(l_history_rec.site_use_id,-99)
1774            and currency = l_history_rec.currency_code
1775            and NVL(org_id,'-99') = NVL(l_org_id,-99)
1776            and as_of_date = l_history_rec.trx_date;
1777 
1778    END IF; --if history record has already been processed then we dont need
1779            --to do anything.
1780 
1781     IF pg_debug = 'Y'
1782     THEN
1783         debug ('AR_BUS_EVENT_SUB_PVT.Inv_Modify(-)');
1784     END IF;
1785   Return 'SUCCESS';
1786 EXCEPTION
1787     WHEN OTHERS  THEN
1788      ROLLBACK TO Inv_Modify_Event;
1789 
1790      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
1791      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
1792      FND_MSG_PUB.ADD;
1793 
1794      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'INV_MODIFY', p_event.getEventName(), p_subscription_guid);
1795      WF_EVENT.setErrorInfo(p_event, 'ERROR');
1796 
1797      RETURN 'ERROR';
1798 
1799 END Inv_Modify;
1800 
1801 FUNCTION Inv_DepositApply
1802 ( p_subscription_guid In RAW
1803 , p_event IN OUT NOCOPY WF_EVENT_T
1804 )
1805 RETURN VARCHAR2 IS
1806 
1807 BEGIN
1808 
1809     IF pg_debug = 'Y'
1810     THEN
1811         debug ('AR_BUS_EVENT_SUB_PVT.Inv_DepositApply(+)');
1812     END IF;
1813   Return 'SUCCESS';
1814     IF pg_debug = 'Y'
1815     THEN
1819 
1816         debug ('AR_BUS_EVENT_SUB_PVT.Inv_DepositApply(-)');
1817     END IF;
1818 END Inv_DepositApply;
1820 FUNCTION CM_Complete
1821 ( p_subscription_guid In RAW
1822 , p_event IN OUT NOCOPY WF_EVENT_T
1823 )
1824 RETURN VARCHAR2 IS
1825   l_ps_rec AR_PAYMENT_SCHEDULES%rowtype;
1826   l_trx_summary_hist AR_TRX_SUMMARY_HIST%rowtype;
1827   CURSOR get_trx_history(p_cust_trx_id  IN NUMBER) IS
1828   SELECT *
1829   FROM AR_TRX_SUMMARY_HIST
1830   WHERE customer_trx_id = p_cust_trx_id
1831     and nvl(complete_flag,'N') = 'N'
1832     and amount_due_original is not null
1833     for update;
1834 
1835   CURSOR lock_ps (cust_trx_id IN NUMBER) IS
1836   SELECT * from ar_payment_schedules
1837   WHERE customer_trx_id = cust_trx_id
1838   FOR UPDATE;
1839 
1840   CURSOR get_prev_ctx_id (ctx_id IN NUMBER) IS
1841   select ct.previous_customer_trx_id , ctt.type
1842   from ra_customer_trx ct,
1843        ra_customer_trx prev_ct,
1844        ra_cust_trx_types ctt
1845   where ct.customer_trx_id = ctx_id
1846     and prev_ct.customer_trx_id = ct.previous_customer_trx_id
1847     and prev_ct.cust_trx_type_id = ctt.cust_trx_type_id;
1848 
1849   i                  INTEGER;
1850   l_key         VARCHAR2(240) := p_event.GetEventKey();
1851   l_payment_schedule_id   NUMBER(15);
1852   l_customer_trx_id  NUMBER;
1853   l_org_id           NUMBER;
1854   l_user_id          NUMBER;
1855   l_resp_id          NUMBER;
1856   l_application_id   NUMBER;
1857   l_security_gr_id   NUMBER;
1858   l_counter          NUMBER;
1859   l_history_exists_flag  BOOLEAN;
1860   l_ps_exists         BOOLEAN;
1861   l_history_rec      ar_trx_summary_hist%rowtype;
1862   l_tot_inv_amt      NUMBER;
1863   l_inv_inst_count   NUMBER;
1864   l_prev_trx_op_count  NUMBER;
1865   l_prev_trx_app_amt   NUMBER;
1866   l_prev_ctx_id      NUMBER;
1867   l_prev_trx_class   VARCHAR2(10);
1868   l_past_due_inv_amt    NUMBER;
1869   l_past_due_inv_count  NUMBER;
1870 BEGIN
1871     IF pg_debug = 'Y'
1872     THEN
1873         debug ('AR_BUS_EVENT_SUB_PVT.CM_Complete(+)');
1874     END IF;
1875   l_customer_trx_id := p_event.GetValueForParameter('CUSTOMER_TRX_ID');
1876   l_prev_trx_op_count := p_event.GetValueForParameter('PREV_TRX_OP_COUNT');
1877   l_prev_trx_app_amt  := fnd_number.canonical_to_number(p_event.GetValueForParameter('PREV_TRX_APP_AMT'));
1878   l_past_due_inv_amt  := fnd_number.canonical_to_number(p_event.GetValueForParameter('PAST_DUE_INV_AMT'));
1879   l_past_due_inv_count  := p_event.GetValueForParameter('PAST_DUE_INV_COUNT');
1880   l_org_id          := p_event.GetValueForParameter('ORG_ID');
1881   l_user_id         := p_event.GetValueForParameter('USER_ID');
1882   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
1883   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
1884   l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
1885   SAVEPOINT CM_Complete_Event;
1886     IF pg_debug = 'Y'
1887     THEN
1888        debug ('l_customer_trx_id= '||l_customer_trx_id);
1889        debug ('l_prev_trx_op_count= '||l_prev_trx_op_count);
1890        debug ('l_prev_trx_app_amt= '||l_prev_trx_app_amt);
1891        debug ('l_past_due_inv_amt= '||l_past_due_inv_amt);
1892        debug ('l_past_due_inv_count= '||l_past_due_inv_count);
1893        debug ('l_org_id= '||l_org_id);
1894        debug ('l_user_id= '||l_user_id);
1895        debug ('l_resp_id= '||l_resp_id);
1896        debug ('l_application_id= '||l_application_id);
1897        debug ('l_security_gr_id= '||l_security_gr_id);
1898     END IF;
1899    --
1900    --set the application context.
1901    --
1902   fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
1903   mo_global.init('AR');
1904   mo_global.set_policy_context('S',l_org_id);
1905    --
1906    -- Acquire locks on the payment schedule record so that
1907    -- the record is not changed while this subscription is
1908    -- executed.
1909    --
1910   OPEN lock_ps (l_customer_trx_id);
1911 /* bug4537412 	: Initialized the value of i as i:= 0 to prevent ORA-01400 error*/
1912     i := 0;
1913 
1914    LOOP
1915    FETCH lock_ps INTO  l_ps_rec;
1916 
1917    IF lock_ps%NOTFOUND  THEN
1918      IF i = 0 THEN
1919        l_ps_exists := FALSE;
1920      ELSE
1921        l_ps_exists := TRUE;
1922      END IF;
1923 
1924      EXIT;
1925    END IF;
1926        i := i + 1;
1927    END LOOP;
1928   CLOSE lock_ps;
1929 
1930    --
1931    --Update the transaction history table
1932    --set the complete_flag = 'Y'
1933    --if history records exist.
1934    --
1935    UPDATE ar_trx_summary_hist
1936       set complete_flag = 'Y',
1937           LAST_UPDATE_DATE  = sysdate,
1938           LAST_UPDATED_BY   = FND_GLOBAL.user_id,
1939           LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
1940    WHERE customer_trx_id = l_customer_trx_id
1941      and nvl(complete_flag,'N') = 'N';
1942 
1943 
1944    IF SQL%NOTFOUND THEN
1945       l_history_exists_flag := FALSE;
1946    ELSE
1947       l_history_exists_flag := TRUE;
1948    END IF;
1949 
1950 
1951    IF l_ps_exists  THEN
1952 
1953        OPEN get_prev_ctx_id(l_customer_trx_id);
1954         FETCH get_prev_ctx_id INTO l_prev_ctx_id, l_prev_trx_class;
1955        CLOSE get_prev_ctx_id;
1956 
1957        --l_prev_ctx_id is NOT NULL for a regular credit memo
1958        --and NULL for an On-Account Credit memo.
1959 
1960        --this is a credit memo so only one ps exists
1961 
1962        --
1963        -- Update the AR_TRX_BAL_SUMMARY table
1964        --
1965         UPDATE ar_trx_bal_summary
1966           set BEST_CURRENT_RECEIVABLES
1970                                    +nvl(l_ps_rec.amount_adjusted,0))),
1967                        = nvl(BEST_CURRENT_RECEIVABLES,0)
1968                               + DECODE(sign(l_ps_rec.due_date - sysdate),-1,0,
1969                               (l_ps_rec.amount_due_original
1971               OP_CREDIT_MEMOS_VALUE
1972                        = nvl(OP_CREDIT_MEMOS_VALUE,0)
1973                              + DECODE(l_prev_ctx_id, null,
1974                                      l_ps_rec.amount_due_original,
1975                                     DECODE(l_prev_trx_class,'CM',
1976                                                  l_ps_rec.amount_due_original,0)),
1977               OP_CREDIT_MEMOS_COUNT = nvl(OP_CREDIT_MEMOS_COUNT,0)
1978                                     + DECODE(l_prev_ctx_id, null,1,
1979                                         DECODE(l_prev_trx_class,'CM',
1980                                                      l_prev_trx_op_count,0)),
1981               OP_INVOICES_VALUE = nvl(OP_INVOICES_VALUE,0)
1982                                      + DECODE(l_prev_ctx_id, null,0,
1983                                          DECODE(l_prev_trx_class,'INV',
1984                                                  l_ps_rec.amount_due_original,0)),
1985               OP_INVOICES_COUNT =  nvl(OP_INVOICES_COUNT,0)
1986                                      + DECODE(l_prev_ctx_id, null,0,
1987                                           DECODE(l_prev_trx_class,'INV',
1988                                                           l_prev_trx_op_count,0)),
1989               OP_DEBIT_MEMOS_VALUE = nvl(OP_DEBIT_MEMOS_VALUE,0)
1990                                      + DECODE(l_prev_ctx_id, null,0,
1991                                          DECODE(l_prev_trx_class,'DM',
1992                                                  l_ps_rec.amount_due_original,0)),
1993               OP_DEBIT_MEMOS_COUNT =  nvl(OP_DEBIT_MEMOS_COUNT,0)
1994                                      + DECODE(l_prev_ctx_id, null,0,
1995                                           DECODE(l_prev_trx_class,'DM',
1996                                                           l_prev_trx_op_count,0)),
1997               OP_DEPOSITS_VALUE = nvl(OP_DEPOSITS_VALUE,0)
1998                                      + DECODE(l_prev_ctx_id, null,0,
1999                                          DECODE(l_prev_trx_class,'DEP',
2000                                                  l_ps_rec.amount_due_original,0)),
2001               OP_DEPOSITS_COUNT =  nvl(OP_DEPOSITS_COUNT,0)
2002                                      + DECODE(l_prev_ctx_id, null,0,
2003                                           DECODE(l_prev_trx_class,'DEP',
2004                                                           l_prev_trx_op_count,0)),
2005               OP_CHARGEBACK_VALUE = nvl(OP_CHARGEBACK_VALUE,0)
2006                                      + DECODE(l_prev_ctx_id, null,0,
2007                                          DECODE(l_prev_trx_class,'CB',
2008                                                  l_ps_rec.amount_due_original,0)),
2009               OP_CHARGEBACK_COUNT =  nvl(OP_CHARGEBACK_COUNT,0)
2010                                      + DECODE(l_prev_ctx_id, null,0,
2011                                           DECODE(l_prev_trx_class,'CB',
2012                                                           l_prev_trx_op_count,0)),
2013               PAST_DUE_INV_VALUE  = nvl(PAST_DUE_INV_VALUE,0)
2014                                       + nvl(l_past_due_inv_amt,0),
2015               PAST_DUE_INV_INST_COUNT = nvl(PAST_DUE_INV_INST_COUNT,0)
2016                                            + nvl(l_past_due_inv_count,0),
2017               LAST_UPDATE_DATE  = sysdate,
2018               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
2019               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
2020          WHERE cust_account_id = l_ps_rec.customer_id
2021            and site_use_id = nvl(l_ps_rec.customer_site_use_id,-99)
2022            and currency = l_ps_rec.invoice_currency_code
2023            and NVL(org_id,'-99') = NVL(l_org_id,-99);
2024 
2025          IF SQL%NOTFOUND THEN
2026 
2027            INSERT INTO ar_trx_bal_summary
2028             (CUST_ACCOUNT_ID,
2029              SITE_USE_ID,
2030              ORG_ID,
2031              CURRENCY,
2032              last_update_date,
2033              last_updated_by,
2034              creation_date,
2035              created_by,
2036              last_update_login,
2037              BEST_CURRENT_RECEIVABLES,
2038              OP_CREDIT_MEMOS_VALUE,
2039              OP_CREDIT_MEMOS_COUNT,
2040              OP_INVOICES_VALUE,
2041              OP_INVOICES_COUNT,
2042              OP_DEBIT_MEMOS_VALUE,
2043              OP_DEBIT_MEMOS_COUNT,
2044              OP_CHARGEBACK_VALUE,
2045              OP_CHARGEBACK_COUNT,
2046              PAST_DUE_INV_VALUE,
2047              PAST_DUE_INV_INST_COUNT,
2048              OP_DEPOSITS_VALUE,
2049              OP_DEPOSITS_COUNT
2050              )
2051              VALUES
2052             ( l_ps_rec.customer_id,
2053               nvl(l_ps_rec.customer_site_use_id,-99),
2054               l_org_id,
2055               l_ps_rec.invoice_currency_code,
2056               sysdate,
2057               fnd_global.user_id,
2058               sysdate,
2059               fnd_global.user_id,
2060               fnd_global.login_id,
2061               DECODE(sign(l_ps_rec.due_date - sysdate),-1,0,
2062                                l_ps_rec.amount_due_original),
2063               DECODE(l_prev_ctx_id, null, l_ps_rec.amount_due_original,
2064                  DECODE(l_prev_trx_class,'CM',l_ps_rec.amount_due_original,0)),
2065               DECODE(l_prev_ctx_id, null,1,
2066                    DECODE(l_prev_trx_class,'CM', l_prev_trx_op_count,0)),
2067               DECODE(l_prev_ctx_id, null,0,
2068                  DECODE(l_prev_trx_class,'INV',l_ps_rec.amount_due_original,0)),
2069               DECODE(l_prev_ctx_id, null,0,
2070                  DECODE(l_prev_trx_class,'INV', l_prev_trx_op_count,0)),
2071               DECODE(l_prev_ctx_id, null,0,
2075               DECODE(l_prev_ctx_id, null,0,
2072                  DECODE(l_prev_trx_class,'DM',l_ps_rec.amount_due_original,0)),
2073               DECODE(l_prev_ctx_id, null,0,
2074                  DECODE(l_prev_trx_class,'DM', l_prev_trx_op_count,0)),
2076                  DECODE(l_prev_trx_class,'CB',l_ps_rec.amount_due_original,0)),
2077               DECODE(l_prev_ctx_id, null,0,
2078                  DECODE(l_prev_trx_class,'CB', l_prev_trx_op_count,0)),
2079               l_past_due_inv_amt,
2080               l_past_due_inv_count,
2081               DECODE(l_prev_ctx_id, null,0,
2082                  DECODE(l_prev_trx_class,'DEP',l_ps_rec.amount_due_original,0)),
2083               DECODE(l_prev_ctx_id, null,0,
2084                  DECODE(l_prev_trx_class,'DEP', l_prev_trx_op_count,0))
2085               );
2086 
2087            END IF;
2088        --
2089        -- Update the AR_TRX_SUMMARY table
2090        --
2091 
2092         UPDATE ar_trx_summary
2093           set OP_BAL_HIGH_WATERMARK = nvl(OP_BAL_HIGH_WATERMARK,0) +
2094                                                l_ps_rec.amount_due_original,
2095               TOTAL_CREDIT_MEMOS_VALUE = nvl(TOTAL_CREDIT_MEMOS_VALUE,0) +
2096                                                l_ps_rec.amount_due_original,
2097               TOTAL_CREDIT_MEMOS_COUNT = nvl(TOTAL_CREDIT_MEMOS_COUNT,0) + 1,
2098               LAST_UPDATE_DATE  = sysdate,
2099               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
2100               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
2101          WHERE cust_account_id = l_ps_rec.customer_id
2102            and site_use_id = nvl(l_ps_rec.customer_site_use_id,-99)
2103            and currency = l_ps_rec.invoice_currency_code
2104            and NVL(org_id,'-99') = NVL(l_org_id,-99)
2105            and as_of_date = l_ps_rec.trx_date;
2106 
2107         IF SQL%NOTFOUND THEN
2108 
2109           INSERT INTO ar_trx_summary
2110             (CUST_ACCOUNT_ID,
2111              SITE_USE_ID,
2112              ORG_ID,
2113              CURRENCY,
2114              AS_OF_DATE,
2115              last_update_date,
2116              last_updated_by,
2117              creation_date,
2118              created_by,
2119              last_update_login,
2120              OP_BAL_HIGH_WATERMARK,
2121              TOTAL_CREDIT_MEMOS_VALUE,
2122              TOTAL_CREDIT_MEMOS_COUNT
2123              )
2124              VALUES
2125              (l_ps_rec.customer_id,
2126               nvl(l_ps_rec.customer_site_use_id,-99),
2127               l_org_id,
2128               l_ps_rec.invoice_currency_code,
2129               l_ps_rec.trx_date,
2130               sysdate,
2131               fnd_global.user_id,
2132               sysdate,
2133               fnd_global.user_id,
2134               fnd_global.login_id,
2135               l_ps_rec.amount_due_original+
2136                                     nvl(l_ps_rec.amount_adjusted,0),
2137               l_ps_rec.amount_due_original,
2138               1
2139               );
2140            END IF;
2141 
2142    ELSE --l_ps_exists
2143      --if no payment schedule exits for the given customer_trx_id
2144      --then we do not update the summary table.
2145      null;
2146    END IF; --l_ps_exists
2147 
2148     IF pg_debug = 'Y'
2149     THEN
2150         debug ('AR_BUS_EVENT_SUB_PVT.CM_Complete(-)');
2151     END IF;
2152   Return 'SUCCESS';
2153 EXCEPTION
2154     WHEN OTHERS  THEN
2155      ROLLBACK TO CM_Complete_Event;
2156 
2157      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
2158      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
2159      FND_MSG_PUB.ADD;
2160 
2161      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'CM_COMPLETE', p_event.getEventName(), p_subscription_guid);
2162      WF_EVENT.setErrorInfo(p_event, 'ERROR');
2163 
2164      RETURN 'ERROR';
2165 
2166 
2167 END CM_Complete;
2168 
2169 FUNCTION CM_InComplete
2170 ( p_subscription_guid In RAW
2171 , p_event IN OUT NOCOPY WF_EVENT_T
2172 )
2173 RETURN VARCHAR2 IS
2174 
2175   l_ps_tab ps_tab_type;
2176   l_trx_summary_hist AR_TRX_SUMMARY_HIST%rowtype;
2177 
2178   CURSOR get_trx_history(p_hist_id  IN NUMBER) IS
2179   SELECT *
2180   FROM AR_TRX_SUMMARY_HIST
2181   WHERE history_id = p_hist_id;
2182 
2183   CURSOR get_prev_ctx_id (ctx_id IN NUMBER) IS
2184   select previous_customer_trx_id
2185   from ra_customer_trx
2186   where customer_trx_id = ctx_id;
2187 
2188   i                  INTEGER;
2189   l_key         VARCHAR2(240) := p_event.GetEventKey();
2190   l_payment_schedule_id   NUMBER(15);
2191   l_customer_trx_id  NUMBER;
2192   l_org_id           NUMBER;
2193   l_user_id          NUMBER;
2194   l_resp_id          NUMBER;
2195   l_application_id   NUMBER;
2196   l_security_gr_id   NUMBER;
2197   l_counter          NUMBER;
2198   l_history_exists_flag  BOOLEAN;
2199   l_ps_exists        BOOLEAN;
2200   l_history_rec      ar_trx_summary_hist%rowtype;
2201   l_tot_inv_amt      NUMBER;
2202   l_inv_inst_count   NUMBER;
2203   l_larg_inv_cust_trx_id  NUMBER;
2204   l_text         VARCHAR2(2000);
2205   l_history_id     NUMBER(15);
2206   v_cursor1  NUMBER;
2207   l_larg_inv_amt    NUMBER;
2208   v_return_code   NUMBER;
2209   v_NumRows       NUMBER;
2210   l_prev_trx_op_count  NUMBER;
2211   l_prev_trx_app_amt   NUMBER;
2212   l_prev_ctx_id      NUMBER;
2213   l_prev_trx_class   VARCHAR2(10);
2214   l_past_due_inv_amt    NUMBER;
2215   l_past_due_inv_count  NUMBER;
2216 BEGIN
2217 
2218     IF pg_debug = 'Y'
2219     THEN
2220         debug ('AR_BUS_EVENT_SUB_PVT.CM_InComplete(+)');
2221     END IF;
2225   l_past_due_inv_amt  := fnd_number.canonical_to_number(p_event.GetValueForParameter('PAST_DUE_INV_AMT'));
2222   l_customer_trx_id := p_event.GetValueForParameter('CUSTOMER_TRX_ID');
2223   l_prev_trx_op_count := p_event.GetValueForParameter('PREV_TRX_OP_COUNT');
2224   l_prev_trx_app_amt  := fnd_number.canonical_to_number(p_event.GetValueForParameter('PREV_TRX_APP_AMT'));
2226   l_past_due_inv_count  := p_event.GetValueForParameter('PAST_DUE_INV_COUNT');
2227   l_prev_trx_class  := p_event.GetValueForParameter('PREV_TRX_TYPE');
2228   l_prev_ctx_id    := p_event.GetValueForParameter('PREV_TRX_ID');
2229   l_payment_schedule_id := p_event.GetValueForParameter('PAYMENT_SCHEDULE_ID');
2230   l_history_id      := p_event.GetValueForParameter('HISTORY_ID');
2231   l_org_id          := p_event.GetValueForParameter('ORG_ID');
2232   l_user_id         := p_event.GetValueForParameter('USER_ID');
2233   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
2234   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
2235   l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
2236     IF pg_debug = 'Y'
2237     THEN
2238        debug ('l_customer_trx_id= '||l_customer_trx_id);
2239        debug ('l_prev_trx_op_count= '||l_prev_trx_op_count);
2240        debug ('l_prev_trx_app_amt= '||l_prev_trx_app_amt);
2241        debug ('l_past_due_inv_amt= '||l_past_due_inv_amt);
2242        debug ('l_past_due_inv_count= '||l_past_due_inv_count);
2243        debug ('l_prev_trx_class= '||l_prev_trx_class);
2244        debug ('l_prev_ctx_id= '||l_prev_ctx_id);
2245        debug ('l_payment_schedule_id= '||l_payment_schedule_id);
2246        debug ('l_history_id= '||l_history_id);
2247        debug ('l_org_id= '||l_org_id);
2248        debug ('l_user_id= '||l_user_id);
2249        debug ('l_resp_id= '||l_resp_id);
2250        debug ('l_application_id= '||l_application_id);
2251        debug ('l_security_gr_id= '||l_security_gr_id);
2252     END IF;
2253    SAVEPOINT  CM_InComplete_Event;
2254    --
2255    --set the application context.
2256    --
2257   fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
2258   mo_global.init('AR');
2259   mo_global.set_policy_context('S',l_org_id);
2260 
2261    --
2262    -- No need to acquire locks on the payment schedule record
2263    -- because it would not exist in the database as the Incomplete
2264    -- event deletes the payment schedules.
2265    --
2266 
2267    -- In case of the Incomplete event on a PS of an inv, there will be
2268    -- no future events(or in other words history) on this payment schedule
2269    -- as this payment schedule would have been deleted. And subscriptions
2270    -- for all the earlier events on this PS would have been executed by now.
2271    --
2272    -- Update the transaction history table set the complete_flag = 'Y'
2273    -- if history records exist.
2274    --
2275     UPDATE ar_trx_summary_hist
2276       set complete_flag = 'Y',
2277           LAST_UPDATE_DATE  = sysdate,
2278           LAST_UPDATED_BY   = FND_GLOBAL.user_id,
2279           LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
2280     WHERE payment_schedule_id = l_payment_schedule_id
2281       and history_id = l_history_id
2282       and nvl(complete_flag,'N') = 'N';
2283 
2284     IF SQL%NOTFOUND THEN
2285        l_history_exists_flag := FALSE;
2286     ELSE
2287        l_history_exists_flag := TRUE;
2288     END IF;
2289 
2290    IF l_history_exists_flag  THEN
2291 
2292     OPEN get_trx_history(l_history_id) ;
2293     FETCH get_trx_history INTO l_history_rec;
2294     CLOSE get_trx_history ;
2295 
2296        --
2297        -- Update the AR_TRX_BAL_SUMMARY table
2298        --
2299         UPDATE ar_trx_bal_summary
2300           set BEST_CURRENT_RECEIVABLES
2301                        = nvl(BEST_CURRENT_RECEIVABLES,0)
2302                               - DECODE(sign(l_history_rec.due_date - sysdate),-1,0,
2303                               (l_history_rec.amount_due_original
2304                                    +nvl(l_history_rec.amount_adjusted,0))),
2305               OP_CREDIT_MEMOS_VALUE
2306                        = nvl(OP_CREDIT_MEMOS_VALUE,0)
2307                              - DECODE(l_prev_ctx_id, null,
2308                                      l_history_rec.amount_due_original,
2309                                     DECODE(l_prev_trx_class,'CM',
2310                                                  l_history_rec.amount_due_original,0)),
2311               OP_CREDIT_MEMOS_COUNT = nvl(OP_CREDIT_MEMOS_COUNT,0)
2312                                     + DECODE(l_prev_ctx_id, null,1,
2313                                         DECODE(l_prev_trx_class,'CM',
2314                                                      l_prev_trx_op_count,0)),
2315               OP_INVOICES_VALUE = nvl(OP_INVOICES_VALUE,0)
2316                                      - DECODE(l_prev_ctx_id, null,0,
2317                                          DECODE(l_prev_trx_class,'INV',
2318                                                  l_history_rec.amount_due_original,0)),
2319               OP_INVOICES_COUNT =  nvl(OP_INVOICES_COUNT,0)
2320                                      + DECODE(l_prev_ctx_id, null,0,
2321                                           DECODE(l_prev_trx_class,'INV',
2322                                                           l_prev_trx_op_count,0)),
2323               OP_DEBIT_MEMOS_VALUE = nvl(OP_DEBIT_MEMOS_VALUE,0)
2324                                      - DECODE(l_prev_ctx_id, null,0,
2325                                          DECODE(l_prev_trx_class,'DM',
2326                                                  l_history_rec.amount_due_original,0)),
2327               OP_DEBIT_MEMOS_COUNT =  nvl(OP_DEBIT_MEMOS_COUNT,0)
2328                                      + DECODE(l_prev_ctx_id, null,0,
2329                                           DECODE(l_prev_trx_class,'DM',
2330                                                           l_prev_trx_op_count,0)),
2331               OP_DEPOSITS_VALUE = nvl(OP_DEPOSITS_VALUE,0)
2335               OP_DEPOSITS_COUNT =  nvl(OP_DEPOSITS_COUNT,0)
2332                                      - DECODE(l_prev_ctx_id, null,0,
2333                                          DECODE(l_prev_trx_class,'DEP',
2334                                                  l_history_rec.amount_due_original,0)),
2336                                      + DECODE(l_prev_ctx_id, null,0,
2337                                           DECODE(l_prev_trx_class,'DEP',
2338                                                           l_prev_trx_op_count,0)),
2339               OP_CHARGEBACK_VALUE = nvl(OP_CHARGEBACK_VALUE,0)
2340                                      - DECODE(l_prev_ctx_id, null,0,
2341                                          DECODE(l_prev_trx_class,'CB',
2342                                                  l_history_rec.amount_due_original,0)),
2343               OP_CHARGEBACK_COUNT =  nvl(OP_CHARGEBACK_COUNT,0)
2344                                      + DECODE(l_prev_ctx_id, null,0,
2345                                           DECODE(l_prev_trx_class,'CB',
2346                                                           l_prev_trx_op_count,0)),
2347               PAST_DUE_INV_VALUE  = nvl(PAST_DUE_INV_VALUE,0)
2348                                       - nvl(l_past_due_inv_amt,0),
2349               PAST_DUE_INV_INST_COUNT = nvl(PAST_DUE_INV_INST_COUNT,0)
2350                                            - nvl(l_past_due_inv_count,0),
2351               LAST_UPDATE_DATE  = sysdate,
2352               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
2353               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
2354          WHERE cust_account_id = l_history_rec.customer_id
2355            and site_use_id = nvl(l_history_rec.site_use_id,-99)
2356            and currency = l_history_rec.currency_code
2357            and NVL(org_id,'-99') = NVL(l_org_id,-99);
2358 
2359        --
2360        -- Update the AR_TRX_SUMMARY table
2361        --
2362      UPDATE ar_trx_summary
2363        set OP_BAL_HIGH_WATERMARK = nvl(OP_BAL_HIGH_WATERMARK,0) -
2367            TOTAL_CREDIT_MEMOS_COUNT = nvl(TOTAL_CREDIT_MEMOS_COUNT,0) - 1,
2364                                  l_history_rec.amount_due_original,
2365            TOTAL_CREDIT_MEMOS_VALUE = nvl(TOTAL_CREDIT_MEMOS_VALUE,0) -
2366                                   l_history_rec.amount_due_original,
2368            LAST_UPDATE_DATE  = sysdate,
2369            LAST_UPDATED_BY   = FND_GLOBAL.user_id,
2370            LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
2371          WHERE cust_account_id = l_history_rec.customer_id
2372            and site_use_id = nvl(l_history_rec.site_use_id,-99)
2373            and currency = l_history_rec.currency_code
2374            and NVL(org_id,'-99') = NVL(l_org_id,-99)
2375            and as_of_date = l_history_rec.trx_date;
2376 
2377    ELSE  --l_history_exists_flag is false
2378      null;
2379    END IF;
2380 
2381     IF pg_debug = 'Y'
2382     THEN
2383         debug ('AR_BUS_EVENT_SUB_PVT.CM_InComplete(-)');
2384     END IF;
2385   Return 'SUCCESS';
2386 
2387 END CM_InComplete;
2388 
2389 FUNCTION CM_Modify
2390 ( p_subscription_guid In RAW
2391 , p_event IN OUT NOCOPY WF_EVENT_T
2392 )
2393 RETURN VARCHAR2 IS
2394 
2395 BEGIN
2396 
2397     IF pg_debug = 'Y'
2398     THEN
2399         debug ('AR_BUS_EVENT_SUB_PVT.CM_Modify(+)');
2400     END IF;
2401   Return 'SUCCESS';
2402     IF pg_debug = 'Y'
2403     THEN
2404         debug ('AR_BUS_EVENT_SUB_PVT.CM_Modify(-)');
2405     END IF;
2406 END CM_Modify;
2407 
2408 FUNCTION DM_Complete
2409 ( p_subscription_guid In RAW
2410 , p_event IN OUT NOCOPY WF_EVENT_T
2411 ) RETURN VARCHAR2 IS
2412   l_ps_tab ps_tab_type;
2413   l_trx_summary_hist AR_TRX_SUMMARY_HIST%rowtype;
2414 
2415   CURSOR get_trx_history(p_cust_trx_id  IN NUMBER) IS
2416   SELECT *
2417   FROM AR_TRX_SUMMARY_HIST
2418   WHERE customer_trx_id = p_cust_trx_id
2419     and nvl(complete_flag,'N') = 'N'
2420     and amount_due_original is not null
2421     for update;
2422 
2423   CURSOR lock_ps (cust_trx_id IN NUMBER) IS
2424   SELECT * from ar_payment_schedules
2425   WHERE customer_trx_id = cust_trx_id
2426   FOR UPDATE;
2427 
2428   i                  INTEGER;
2429   l_key         VARCHAR2(240) := p_event.GetEventKey();
2430   l_payment_schedule_id   NUMBER(15);
2431   l_customer_trx_id  NUMBER;
2432   l_org_id           NUMBER;
2433   l_user_id          NUMBER;
2434   l_resp_id          NUMBER;
2435   l_application_id   NUMBER;
2436   l_security_gr_id   NUMBER;
2437   l_counter          NUMBER;
2438   l_history_exists_flag  BOOLEAN;
2439   l_ps_exists         BOOLEAN;
2440   l_history_rec      ar_trx_summary_hist%rowtype;
2441  BEGIN
2442 
2443     IF pg_debug = 'Y'
2444     THEN
2445         debug ('AR_BUS_EVENT_SUB_PVT.DM_Complete(+)');
2446     END IF;
2447   l_customer_trx_id := p_event.GetValueForParameter('CUSTOMER_TRX_ID');
2448   l_org_id          := p_event.GetValueForParameter('ORG_ID');
2449   l_user_id         := p_event.GetValueForParameter('USER_ID');
2450   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
2451   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
2452   l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
2453     IF pg_debug = 'Y'
2454     THEN
2455        debug ('l_customer_trx_id= '||l_customer_trx_id);
2456        debug ('l_org_id= '||l_org_id);
2457        debug ('l_user_id= '||l_user_id);
2458        debug ('l_resp_id= '||l_resp_id);
2459        debug ('l_application_id= '||l_application_id);
2460        debug ('l_security_gr_id= '||l_security_gr_id);
2461     END IF;
2462    SAVEPOINT  DM_Complete_Event;
2463    --
2464    --set the application context.
2465    --
2466   fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
2467   mo_global.init('AR');
2468   mo_global.set_policy_context('S',l_org_id);
2469 
2470    --
2471    -- Acquire locks on the payment schedule record so that
2472    -- the record is not changed while this subscription is
2473    -- executed.
2474    --
2475   OPEN lock_ps (l_customer_trx_id);
2476     i := 1;
2477     l_counter := 0;
2478    LOOP
2479    FETCH lock_ps INTO  l_ps_tab(i);
2480 
2481    IF lock_ps%NOTFOUND  THEN
2482      IF l_counter = 0 THEN
2483        l_ps_exists := FALSE;
2484      ELSE
2485        l_ps_exists := TRUE;
2486      END IF;
2487 
2488      EXIT;
2489    END IF;
2490        i := i + 1;
2491        l_counter := l_counter + 1;
2492    END LOOP;
2493   CLOSE lock_ps;
2494 
2495    --
2496    --Update the transaction history table
2497    --set the complete_flag = 'Y'
2498    --if history records exist.
2499    --
2500    UPDATE ar_trx_summary_hist
2501      set complete_flag = 'Y',
2502          LAST_UPDATE_DATE  = sysdate,
2503          LAST_UPDATED_BY   = FND_GLOBAL.user_id,
2504          LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
2505    WHERE customer_trx_id = l_customer_trx_id
2506      and nvl(complete_flag,'N') = 'N';
2507 
2508 
2509    IF SQL%NOTFOUND THEN
2510       l_history_exists_flag := FALSE;
2511    ELSE
2512       l_history_exists_flag := TRUE;
2513    END IF;
2514 
2515 
2516    IF l_ps_exists  THEN
2520      FOR j in 1..l_ps_tab.COUNT
2517     --Sweep thru the l_ps_tab table to update the summary table
2518     --and ignore the history.
2519 
2521        LOOP
2522        --
2523        -- Update the AR_TRX_BAL_SUMMARY table
2524        --
2525         UPDATE ar_trx_bal_summary
2526           set BEST_CURRENT_RECEIVABLES
2527                        = nvl(BEST_CURRENT_RECEIVABLES,0)
2528                               +DECODE(sign(l_ps_tab(j).due_date - sysdate),-1,0,
2529                                         l_ps_tab(j).amount_due_original),
2530               OP_DEBIT_MEMOS_VALUE
2531                        = nvl(OP_DEBIT_MEMOS_VALUE,0)
2532                              +l_ps_tab(j).amount_due_original,
2533               OP_DEBIT_MEMOS_COUNT = nvl(OP_DEBIT_MEMOS_COUNT,0) + 1,
2534               LAST_UPDATE_DATE  = sysdate,
2535               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
2536               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
2537          WHERE cust_account_id = l_ps_tab(j).customer_id
2538            and site_use_id = nvl(l_ps_tab(j).customer_site_use_id,-99)
2539            and currency = l_ps_tab(j).invoice_currency_code
2540            and NVL(org_id,'-99') = NVL(l_org_id,-99);
2541 
2542          IF SQL%NOTFOUND THEN
2543 
2544            INSERT INTO ar_trx_bal_summary
2545             (CUST_ACCOUNT_ID,
2546              SITE_USE_ID,
2547              ORG_ID,
2548              CURRENCY,
2549              last_update_date,
2550              last_updated_by,
2551              creation_date,
2552              created_by,
2553              last_update_login,
2554              BEST_CURRENT_RECEIVABLES,
2555              OP_DEBIT_MEMOS_VALUE,
2556              OP_DEBIT_MEMOS_COUNT
2557              )
2558              VALUES
2559             ( l_ps_tab(j).customer_id,
2560               nvl(l_ps_tab(j).customer_site_use_id,-99),
2561               l_org_id,
2562               l_ps_tab(j).invoice_currency_code,
2563               sysdate,
2564               fnd_global.user_id,
2565               sysdate,
2566               fnd_global.user_id,
2567               fnd_global.login_id,
2568               DECODE(sign(l_ps_tab(j).due_date - sysdate),-1,0,
2569                       l_ps_tab(j).amount_due_original),
2570               l_ps_tab(j).amount_due_original,
2571               1
2572               );
2573 
2574            END IF;
2575        --
2576        -- Update the AR_TRX_SUMMARY table
2577        --
2578 
2579         UPDATE ar_trx_summary
2580           set OP_BAL_HIGH_WATERMARK = nvl(OP_BAL_HIGH_WATERMARK,0) +
2581                                        l_ps_tab(j).amount_due_original,
2582               TOTAL_DEBIT_MEMOS_VALUE = nvl(TOTAL_DEBIT_MEMOS_VALUE,0) +
2583                                           l_ps_tab(j).amount_due_original,
2584                TOTAL_DEBIT_MEMOS_COUNT = nvl(TOTAL_DEBIT_MEMOS_COUNT,0) + 1,
2585                LAST_UPDATE_DATE  = sysdate,
2586                LAST_UPDATED_BY   = FND_GLOBAL.user_id,
2587                LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
2588          WHERE cust_account_id = l_ps_tab(j).customer_id
2589            and site_use_id = nvl(l_ps_tab(j).customer_site_use_id,-99)
2590            and currency = l_ps_tab(j).invoice_currency_code
2591            and NVL(org_id,'-99') = NVL(l_org_id,-99)
2592            and as_of_date = l_ps_tab(j).trx_date;
2593 
2594         IF SQL%NOTFOUND THEN
2595 
2596           INSERT INTO ar_trx_summary
2597             (CUST_ACCOUNT_ID,
2598              SITE_USE_ID,
2599              ORG_ID,
2600              CURRENCY,
2601              AS_OF_DATE,
2602              last_update_date,
2603              last_updated_by,
2604              creation_date,
2605              created_by,
2606              last_update_login,
2607              OP_BAL_HIGH_WATERMARK,
2608              TOTAL_DEBIT_MEMOS_VALUE,
2609              TOTAL_DEBIT_MEMOS_COUNT
2610              )
2611              VALUES
2612              (l_ps_tab(j).customer_id,
2613               nvl(l_ps_tab(j).customer_site_use_id,-99),
2614               l_org_id,
2615               l_ps_tab(j).invoice_currency_code,
2616               l_ps_tab(j).trx_date,
2617               sysdate,
2618               fnd_global.user_id,
2619               sysdate,
2620               fnd_global.user_id,
2621               fnd_global.login_id,
2622               l_ps_tab(j).amount_due_original,
2623               l_ps_tab(j).amount_due_original,
2624               1
2625               );
2626            END IF;
2627       END LOOP;
2628 
2629    ELSE --l_ps_exists
2630      --if no payment schedule exits for the given customer_trx_id
2631      --then we do not update the summary table.
2632      null;
2633    END IF; --l_ps_exists
2634 
2635     IF pg_debug = 'Y'
2636     THEN
2637         debug ('AR_BUS_EVENT_SUB_PVT.DM_Complete(-)');
2638     END IF;
2639   Return 'SUCCESS';
2640 EXCEPTION
2641     WHEN OTHERS  THEN
2642      ROLLBACK TO DM_Complete_Event;
2643 
2644      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
2645      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
2646      FND_MSG_PUB.ADD;
2647 
2648      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'DM_COMPLETE', p_event.getEventName(), p_subscription_guid);
2649      WF_EVENT.setErrorInfo(p_event, 'ERROR');
2650 
2651      RETURN 'ERROR';
2652 
2653 
2654 
2655 END DM_Complete;
2656 
2657 FUNCTION DM_InComplete
2658 ( p_subscription_guid In RAW
2659 , p_event IN OUT NOCOPY WF_EVENT_T
2660 )
2661 RETURN VARCHAR2 IS
2662   l_ps_tab ps_tab_type;
2663   l_trx_summary_hist AR_TRX_SUMMARY_HIST%rowtype;
2664 
2665   CURSOR get_trx_history(p_hist_id  IN NUMBER) IS
2666   SELECT *
2667   FROM AR_TRX_SUMMARY_HIST
2668   WHERE history_id = p_hist_id;
2672   i                  INTEGER;
2669    -- and nvl(complete_flag,'N') = 'N';
2670 
2671 
2673   l_key         VARCHAR2(240) := p_event.GetEventKey();
2674   l_payment_schedule_id   NUMBER(15);
2675   l_customer_trx_id  NUMBER;
2676   l_org_id           NUMBER;
2677   l_user_id          NUMBER;
2678   l_resp_id          NUMBER;
2679   l_application_id   NUMBER;
2680   l_security_gr_id   NUMBER;
2681   l_counter          NUMBER;
2682   l_history_exists_flag  BOOLEAN;
2683   l_ps_exists        BOOLEAN;
2684   l_history_rec      ar_trx_summary_hist%rowtype;
2685   l_tot_inv_amt      NUMBER;
2686   l_inv_inst_count   NUMBER;
2687   l_larg_inv_cust_trx_id  NUMBER;
2688   l_text         VARCHAR2(2000);
2689   l_history_id     NUMBER(15);
2690 BEGIN
2691 
2692     IF pg_debug = 'Y'
2693     THEN
2694         debug ('AR_BUS_EVENT_SUB_PVT.DM_InComplete(+)');
2695     END IF;
2696   l_customer_trx_id := p_event.GetValueForParameter('CUSTOMER_TRX_ID');
2697   l_payment_schedule_id := p_event.GetValueForParameter('PAYMENT_SCHEDULE_ID');
2698   l_history_id      := p_event.GetValueForParameter('HISTORY_ID');
2699   l_org_id          := p_event.GetValueForParameter('ORG_ID');
2700   l_user_id         := p_event.GetValueForParameter('USER_ID');
2701   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
2702   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
2703   l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
2704    SAVEPOINT  DM_InComplete_Event;
2705     IF pg_debug = 'Y'
2706     THEN
2707        debug ('l_customer_trx_id= '||l_customer_trx_id);
2708        debug ('l_payment_schedule_id= '||l_payment_schedule_id);
2709        debug ('l_history_id= '||l_history_id);
2710        debug ('l_org_id= '||l_org_id);
2711        debug ('l_user_id= '||l_user_id);
2712        debug ('l_resp_id= '||l_resp_id);
2713        debug ('l_application_id= '||l_application_id);
2714        debug ('l_security_gr_id= '||l_security_gr_id);
2715     END IF;
2716    --
2717    --set the application context.
2718    --
2719   fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
2720   mo_global.init('AR');
2721   mo_global.set_policy_context('S',l_org_id);
2722    --
2723    -- No need to acquire locks on the payment schedule record
2724    -- because it would not exist in the database as the Incomplete
2725    -- event deletes the payment schedules.
2726    --
2727 
2728    -- In case of the Incomplete event on a PS of a trx, there will be
2729    -- no future events(or in other words history) on this payment schedule
2730    -- as this payment schedule would have been deleted. And subscriptions
2731    -- for all the earlier events on this PS would have been executed by now.
2732    --
2733    -- Update the transaction history table set the complete_flag = 'Y'
2734    -- if history records exist.
2735    --
2736     UPDATE ar_trx_summary_hist
2737       set complete_flag = 'Y',
2738           LAST_UPDATE_DATE  = sysdate,
2739           LAST_UPDATED_BY   = FND_GLOBAL.user_id,
2740           LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
2741     WHERE payment_schedule_id = l_payment_schedule_id
2742       and history_id = l_history_id
2743       and nvl(complete_flag,'N') = 'N';
2744 
2745 
2746     IF SQL%NOTFOUND THEN
2747        l_history_exists_flag := FALSE;
2748     ELSE
2749        l_history_exists_flag := TRUE;
2750     END IF;
2751 
2752    IF l_history_exists_flag  THEN
2753 
2754     OPEN get_trx_history(l_history_id) ;
2755     FETCH get_trx_history INTO l_history_rec;
2756     CLOSE get_trx_history ;
2757 
2758     l_tot_inv_amt := 0;
2759 
2760        --
2761        -- Update the AR_TRX_BAL_SUMMARY table
2762        --
2763         UPDATE ar_trx_bal_summary
2764           set BEST_CURRENT_RECEIVABLES
2765                        = nvl(BEST_CURRENT_RECEIVABLES,0)
2766                               -DECODE(sign(l_history_rec.due_date - sysdate),-1,0,
2767                                                  l_history_rec.amount_due_original),
2768               OP_DEBIT_MEMOS_VALUE
2769                        = nvl(OP_DEBIT_MEMOS_VALUE,0)
2770                              -l_history_rec.amount_due_original,
2771               OP_DEBIT_MEMOS_COUNT = nvl(OP_DEBIT_MEMOS_COUNT,0) - 1,
2772               LAST_UPDATE_DATE  = sysdate,
2773               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
2774               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
2775          WHERE cust_account_id = l_history_rec.customer_id
2776            and site_use_id = nvl(l_history_rec.site_use_id,-99)
2777            and currency = l_history_rec.currency_code
2778            and NVL(org_id,'-99') = NVL(l_org_id,-99);
2779 
2780        -- No need to check the SQL%NOTFOUND case because Incomplete
2781        -- event would fire only after a  complete event has fired.
2782 
2783 
2784        --
2785        -- Update the AR_TRX_SUMMARY table
2786        --
2787 
2788      UPDATE ar_trx_summary
2789        set OP_BAL_HIGH_WATERMARK = nvl(OP_BAL_HIGH_WATERMARK,0) -
2790                                  l_history_rec.amount_due_original+
2791                                     nvl(l_history_rec.amount_adjusted,0),
2792            TOTAL_DEBIT_MEMOS_VALUE = nvl(TOTAL_DEBIT_MEMOS_VALUE,0) -
2793                                   l_history_rec.amount_due_original,
2794            TOTAL_DEBIT_MEMOS_COUNT = nvl(TOTAL_DEBIT_MEMOS_COUNT,0) - 1,
2795            LAST_UPDATE_DATE  = sysdate,
2796            LAST_UPDATED_BY   = FND_GLOBAL.user_id,
2797            LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
2798          WHERE cust_account_id = l_history_rec.customer_id
2799            and site_use_id = nvl(l_history_rec.site_use_id,-99)
2800            and currency = l_history_rec.currency_code
2801            and NVL(org_id,'-99') = NVL(l_org_id,-99)
2802            and as_of_date = l_history_rec.trx_date;
2803 
2807 
2804    ELSE  --l_history_exists_flag is false
2805      null;
2806    END IF;
2808     IF pg_debug = 'Y'
2809     THEN
2810         debug ('AR_BUS_EVENT_SUB_PVT.DM_InComplete(-)');
2811     END IF;
2812   Return 'SUCCESS';
2813 EXCEPTION
2814     WHEN OTHERS  THEN
2815      ROLLBACK TO DM_InComplete_Event;
2816 
2817      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
2818      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
2819      FND_MSG_PUB.ADD;
2820 
2821      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'DEP_INCOMPLETE', p_event.getEventName(), p_subscription_guid);
2822      WF_EVENT.setErrorInfo(p_event, 'ERROR');
2823 
2824      RETURN 'ERROR';
2825 
2826 END DM_InComplete;
2827 
2828 FUNCTION DM_Modify
2829 ( p_subscription_guid In RAW
2830 , p_event IN OUT NOCOPY WF_EVENT_T
2831 )
2832 RETURN VARCHAR2 IS
2833 
2834 BEGIN
2835 
2836     IF pg_debug = 'Y'
2837     THEN
2838         debug ('AR_BUS_EVENT_SUB_PVT.DM_Modify(+)');
2839     END IF;
2840   Return 'SUCCESS';
2841     IF pg_debug = 'Y'
2842     THEN
2843         debug ('AR_BUS_EVENT_SUB_PVT.DM_Modify(-)');
2844     END IF;
2845 END DM_Modify;
2846 
2847 FUNCTION Dep_Complete
2848 ( p_subscription_guid In RAW
2849 , p_event IN OUT NOCOPY WF_EVENT_T
2850 )
2851 RETURN VARCHAR2 IS
2852 
2853   l_ps_tab ps_tab_type;
2854   l_trx_summary_hist AR_TRX_SUMMARY_HIST%rowtype;
2855 
2856   CURSOR get_trx_history(p_cust_trx_id  IN NUMBER) IS
2857   SELECT *
2858   FROM AR_TRX_SUMMARY_HIST
2859   WHERE customer_trx_id = p_cust_trx_id
2860     and nvl(complete_flag,'N') = 'N'
2861     and amount_due_original is not null
2862     for update;
2863 
2864   CURSOR lock_ps (cust_trx_id IN NUMBER) IS
2865   SELECT * from ar_payment_schedules
2866   WHERE customer_trx_id = cust_trx_id
2867   FOR UPDATE;
2868 
2869   i                  INTEGER;
2870   l_key         VARCHAR2(240) := p_event.GetEventKey();
2871   l_payment_schedule_id   NUMBER(15);
2872   l_customer_trx_id  NUMBER;
2873   l_org_id           NUMBER;
2874   l_user_id          NUMBER;
2875   l_resp_id          NUMBER;
2876   l_application_id   NUMBER;
2877   l_security_gr_id   NUMBER;
2878   l_counter          NUMBER;
2879   l_history_exists_flag  BOOLEAN;
2880   l_ps_exists         BOOLEAN;
2881   l_history_rec      ar_trx_summary_hist%rowtype;
2882  BEGIN
2883 
2884     IF pg_debug = 'Y'
2885     THEN
2886         debug ('AR_BUS_EVENT_SUB_PVT.Dep_Complete(+)');
2887     END IF;
2888   l_customer_trx_id := p_event.GetValueForParameter('CUSTOMER_TRX_ID');
2889   l_org_id          := p_event.GetValueForParameter('ORG_ID');
2890   l_user_id         := p_event.GetValueForParameter('USER_ID');
2891   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
2892   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
2893   l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
2894    SAVEPOINT  Dep_Complete_Event;
2895     IF pg_debug = 'Y'
2896     THEN
2897        debug ('l_customer_trx_id= '||l_customer_trx_id);
2898        debug ('l_org_id= '||l_org_id);
2899        debug ('l_user_id= '||l_user_id);
2900        debug ('l_resp_id= '||l_resp_id);
2901        debug ('l_application_id= '||l_application_id);
2902        debug ('l_security_gr_id= '||l_security_gr_id);
2903     END IF;
2904    --
2905    --set the application context.
2906    --
2907   fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
2908   mo_global.init('AR');
2909   mo_global.set_policy_context('S',l_org_id);
2910 
2911    --
2912    -- Acquire locks on the payment schedule record so that
2913    -- the record is not changed while this subscription is
2914    -- executed.
2915    --
2916   OPEN lock_ps (l_customer_trx_id);
2917     i := 1;
2918     l_counter := 0;
2919    LOOP
2920    FETCH lock_ps INTO  l_ps_tab(i);
2921 
2922    IF lock_ps%NOTFOUND  THEN
2923      IF l_counter = 0 THEN
2924        l_ps_exists := FALSE;
2925      ELSE
2926        l_ps_exists := TRUE;
2927      END IF;
2928 
2929      EXIT;
2930    END IF;
2931        i := i + 1;
2932        l_counter := l_counter + 1;
2933    END LOOP;
2934   CLOSE lock_ps;
2935 
2936    --
2937    --Update the transaction history table
2938    --set the complete_flag = 'Y'
2939    --if history records exist.
2940    --
2941    UPDATE ar_trx_summary_hist
2942       set complete_flag = 'Y',
2943           LAST_UPDATE_DATE  = sysdate,
2944           LAST_UPDATED_BY   = FND_GLOBAL.user_id,
2945           LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
2946    WHERE customer_trx_id = l_customer_trx_id
2947      and nvl(complete_flag,'N') = 'N';
2948 
2949 
2950    IF SQL%NOTFOUND THEN
2951       l_history_exists_flag := FALSE;
2952    ELSE
2953       l_history_exists_flag := TRUE;
2954    END IF;
2955 
2956 
2957    IF l_ps_exists  THEN
2958     --Sweep thru the l_ps_tab table to update the summary table
2959     --and ignore the history.
2960 
2961      FOR j in 1..l_ps_tab.COUNT
2962        LOOP
2963        --
2964        -- Update the AR_TRX_BAL_SUMMARY table
2965        --
2966         UPDATE ar_trx_bal_summary
2967           set BEST_CURRENT_RECEIVABLES
2968                        = nvl(BEST_CURRENT_RECEIVABLES,0)
2969                               +DECODE(sign(l_ps_tab(j).due_date - sysdate),-1,0,
2970                                         l_ps_tab(j).amount_due_original),
2971               OP_DEPOSITS_VALUE
2972                        = nvl(OP_DEPOSITS_VALUE,0)
2973                              +l_ps_tab(j).amount_due_original
2977               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
2974                                    +nvl(l_ps_tab(j).amount_adjusted,0),
2975               OP_DEPOSITS_COUNT = nvl(OP_DEPOSITS_COUNT,0) + 1,
2976               LAST_UPDATE_DATE  = sysdate,
2978               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
2979          WHERE cust_account_id = l_ps_tab(j).customer_id
2980            and site_use_id = nvl(l_ps_tab(j).customer_site_use_id,-99)
2981            and currency = l_ps_tab(j).invoice_currency_code
2982            and NVL(org_id,'-99') = NVL(l_org_id,-99);
2983 
2984          IF SQL%NOTFOUND THEN
2985 
2986            INSERT INTO ar_trx_bal_summary
2987             (CUST_ACCOUNT_ID,
2988              SITE_USE_ID,
2989              ORG_ID,
2990              CURRENCY,
2991              last_update_date,
2992              last_updated_by,
2993              creation_date,
2994              created_by,
2995              last_update_login,
2996              BEST_CURRENT_RECEIVABLES,
2997              OP_INVOICES_VALUE,
2998              OP_INVOICES_COUNT
2999              )
3000              VALUES
3001             ( l_ps_tab(j).customer_id,
3002               nvl(l_ps_tab(j).customer_site_use_id,-99),
3003               l_org_id,
3004               l_ps_tab(j).invoice_currency_code,
3005               sysdate,
3006               fnd_global.user_id,
3007               sysdate,
3008               fnd_global.user_id,
3009               fnd_global.login_id,
3010               DECODE(sign(l_ps_tab(j).due_date - sysdate),-1,0,
3011                       l_ps_tab(j).amount_due_original),
3012               l_ps_tab(j).amount_due_original,
3013               1
3014               );
3015 
3016            END IF;
3017        --
3018        -- Update the AR_TRX_SUMMARY table
3019        --
3020 
3021         UPDATE ar_trx_summary
3022           set OP_BAL_HIGH_WATERMARK = nvl(OP_BAL_HIGH_WATERMARK,0) +
3023                                     l_ps_tab(j).amount_due_original+
3024                                     nvl(l_ps_tab(j).amount_adjusted,0),
3025               TOTAL_DEPOSITS_VALUE = nvl(TOTAL_DEPOSITS_VALUE,0) +
3026                                      l_ps_tab(j).amount_due_original,
3027               TOTAL_DEPOSITS_COUNT = nvl(TOTAL_DEPOSITS_COUNT,0) + 1,
3028               LAST_UPDATE_DATE  = sysdate,
3029               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
3030               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
3031          WHERE cust_account_id = l_ps_tab(j).customer_id
3032            and site_use_id = nvl(l_ps_tab(j).customer_site_use_id,-99)
3033            and currency = l_ps_tab(j).invoice_currency_code
3034            and NVL(org_id,'-99') = NVL(l_org_id,-99)
3035            and as_of_date = l_ps_tab(j).trx_date;
3036 
3037         IF SQL%NOTFOUND THEN
3038 
3039           INSERT INTO ar_trx_summary
3040             (CUST_ACCOUNT_ID,
3041              SITE_USE_ID,
3042              ORG_ID,
3043              CURRENCY,
3044              AS_OF_DATE,
3045              last_update_date,
3046              last_updated_by,
3047              creation_date,
3048              created_by,
3049              last_update_login,
3050              OP_BAL_HIGH_WATERMARK,
3051              TOTAL_DEPOSITS_VALUE,
3052              TOTAL_DEPOSITS_COUNT
3053              )
3054              VALUES
3055              (l_ps_tab(j).customer_id,
3056               nvl(l_ps_tab(j).customer_site_use_id,-99),
3057               l_org_id,
3058               l_ps_tab(j).invoice_currency_code,
3059               l_ps_tab(j).trx_date,
3060               sysdate,
3061               fnd_global.user_id,
3062               sysdate,
3063               fnd_global.user_id,
3064               fnd_global.login_id,
3065               l_ps_tab(j).amount_due_original+
3066                                     nvl(l_ps_tab(j).amount_adjusted,0),
3067               l_ps_tab(j).amount_due_original,
3068               1
3069               );
3070            END IF;
3071       END LOOP;
3072 
3073    ELSE --l_ps_exists
3074      --if no payment schedule exits for the given customer_trx_id
3075      --then we do not update the summary table.
3076      null;
3077    END IF; --l_ps_exists
3078 
3079     IF pg_debug = 'Y'
3080     THEN
3081         debug ('AR_BUS_EVENT_SUB_PVT.Dep_Complete(-)');
3082     END IF;
3083 
3084   Return 'SUCCESS';
3085 EXCEPTION
3086     WHEN OTHERS  THEN
3087      ROLLBACK TO Dep_Complete_Event;
3088 
3089      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
3090      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
3091      FND_MSG_PUB.ADD;
3092 
3093      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'DEP_COMPLETE', p_event.getEventName(), p_subscription_guid);
3094      WF_EVENT.setErrorInfo(p_event, 'ERROR');
3095 
3096      RETURN 'ERROR';
3097 END Dep_Complete;
3098 
3099 FUNCTION Dep_InComplete
3100 ( p_subscription_guid In RAW
3101 , p_event IN OUT NOCOPY WF_EVENT_T
3102 )
3103 RETURN VARCHAR2 IS
3104   l_ps_tab ps_tab_type;
3105   l_trx_summary_hist AR_TRX_SUMMARY_HIST%rowtype;
3106 
3107   CURSOR get_trx_history(p_hist_id  IN NUMBER) IS
3108   SELECT *
3109   FROM AR_TRX_SUMMARY_HIST
3110   WHERE history_id = p_hist_id;
3111    -- and nvl(complete_flag,'N') = 'N';
3112 
3113 
3114   i                  INTEGER;
3115   l_key         VARCHAR2(240) := p_event.GetEventKey();
3116   l_payment_schedule_id   NUMBER(15);
3117   l_customer_trx_id  NUMBER;
3118   l_org_id           NUMBER;
3119   l_user_id          NUMBER;
3120   l_resp_id          NUMBER;
3121   l_application_id   NUMBER;
3122   l_security_gr_id   NUMBER;
3123   l_counter          NUMBER;
3124   l_history_exists_flag  BOOLEAN;
3125   l_ps_exists        BOOLEAN;
3129   l_larg_inv_cust_trx_id  NUMBER;
3126   l_history_rec      ar_trx_summary_hist%rowtype;
3127   l_tot_inv_amt      NUMBER;
3128   l_inv_inst_count   NUMBER;
3130   l_text         VARCHAR2(2000);
3131   l_history_id     NUMBER(15);
3132 BEGIN
3133 
3134     IF pg_debug = 'Y'
3135     THEN
3136         debug ('AR_BUS_EVENT_SUB_PVT.Dep_InComplete(+)');
3137     END IF;
3138   l_customer_trx_id := p_event.GetValueForParameter('CUSTOMER_TRX_ID');
3139   l_payment_schedule_id := p_event.GetValueForParameter('PAYMENT_SCHEDULE_ID');
3140   l_history_id      := p_event.GetValueForParameter('HISTORY_ID');
3141   l_org_id          := p_event.GetValueForParameter('ORG_ID');
3142   l_user_id         := p_event.GetValueForParameter('USER_ID');
3143   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
3144   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
3145   l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
3146     IF pg_debug = 'Y'
3147     THEN
3148        debug ('l_customer_trx_id= '||l_customer_trx_id);
3149        debug ('l_payment_schedule_id= '||l_payment_schedule_id);
3150        debug ('l_history_id= '||l_history_id);
3151        debug ('l_org_id= '||l_org_id);
3152        debug ('l_user_id= '||l_user_id);
3153        debug ('l_resp_id= '||l_resp_id);
3154        debug ('l_application_id= '||l_application_id);
3155        debug ('l_security_gr_id= '||l_security_gr_id);
3156     END IF;
3157    --
3158    SAVEPOINT  Dep_InComplete_Event;
3159    --
3160    --set the application context.
3161    --
3162   fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
3163   mo_global.init('AR');
3164   mo_global.set_policy_context('S',l_org_id);
3165 
3166    --
3167    -- No need to acquire locks on the payment schedule record
3168    -- because it would not exist in the database as the Incomplete
3169    -- event deletes the payment schedules.
3170    --
3171 
3172    -- In case of the Incomplete event on a PS of a trx, there will be
3173    -- no future events(or in other words history) on this payment schedule
3174    -- as this payment schedule would have been deleted. And subscriptions
3175    -- for all the earlier events on this PS would have been executed by now.
3176    --
3177    -- Update the transaction history table set the complete_flag = 'Y'
3178    -- if history records exist.
3179    --
3180     UPDATE ar_trx_summary_hist
3181       set complete_flag = 'Y',
3182           LAST_UPDATE_DATE  = sysdate,
3183           LAST_UPDATED_BY   = FND_GLOBAL.user_id,
3184           LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
3185     WHERE payment_schedule_id = l_payment_schedule_id
3186       and history_id = l_history_id
3187       and nvl(complete_flag,'N') = 'N';
3188 
3189 
3190     IF SQL%NOTFOUND THEN
3191        l_history_exists_flag := FALSE;
3192     ELSE
3193        l_history_exists_flag := TRUE;
3194     END IF;
3195 
3196    IF l_history_exists_flag  THEN
3197 
3198     OPEN get_trx_history(l_history_id) ;
3199     FETCH get_trx_history INTO l_history_rec;
3200     CLOSE get_trx_history ;
3201 
3202     l_tot_inv_amt := 0;
3203 
3204        --
3205        -- Update the AR_TRX_BAL_SUMMARY table
3206        --
3207         UPDATE ar_trx_bal_summary
3208           set BEST_CURRENT_RECEIVABLES
3209                        = nvl(BEST_CURRENT_RECEIVABLES,0)
3210                               -DECODE(sign(l_history_rec.due_date - sysdate),-1,0,
3211                                                  l_history_rec.amount_due_original),
3212               OP_DEPOSITS_VALUE
3213                        = nvl(OP_DEPOSITS_VALUE,0)
3214                              -l_history_rec.amount_due_original,
3215               OP_DEPOSITS_COUNT = nvl(OP_DEPOSITS_COUNT,0) - 1,
3216               LAST_UPDATE_DATE  = sysdate,
3217               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
3218               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
3219          WHERE cust_account_id = l_history_rec.customer_id
3220            and site_use_id = nvl(l_history_rec.site_use_id,-99)
3221            and currency = l_history_rec.currency_code
3222            and NVL(org_id,'-99') = NVL(l_org_id,-99);
3223 
3224        -- No need to check the SQL%NOTFOUND case because Incomplete
3225        -- event would fire only after a  complete event has fired.
3226 
3227 
3228        --
3229        -- Update the AR_TRX_SUMMARY table
3230        --
3231 
3232      UPDATE ar_trx_summary
3233        set OP_BAL_HIGH_WATERMARK = nvl(OP_BAL_HIGH_WATERMARK,0) -
3234                                  l_history_rec.amount_due_original+
3235                                     nvl(l_history_rec.amount_adjusted,0),
3236            TOTAL_DEPOSITS_VALUE = nvl(TOTAL_DEPOSITS_VALUE,0) -
3237                                   l_history_rec.amount_due_original,
3238            TOTAL_DEPOSITS_COUNT = nvl(TOTAL_DEPOSITS_COUNT,0) - 1,
3239            LAST_UPDATE_DATE  = sysdate,
3240            LAST_UPDATED_BY   = FND_GLOBAL.user_id,
3241            LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
3242          WHERE cust_account_id = l_history_rec.customer_id
3243            and site_use_id = nvl(l_history_rec.site_use_id,-99)
3244            and currency = l_history_rec.currency_code
3245            and NVL(org_id,'-99') = NVL(l_org_id,-99)
3246            and as_of_date = l_history_rec.trx_date;
3247 
3248    ELSE  --l_history_exists_flag is false
3249      null;
3250    END IF;
3251 
3252     IF pg_debug = 'Y'
3253     THEN
3254         debug ('AR_BUS_EVENT_SUB_PVT.Dep_InComplete(-)');
3255     END IF;
3256   Return 'SUCCESS';
3257 EXCEPTION
3258     WHEN OTHERS  THEN
3262      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
3259      ROLLBACK TO Dep_InComplete_Event;
3260 
3261      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
3263      FND_MSG_PUB.ADD;
3264 
3265      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'DEP_INCOMPLETE', p_event.getEventName(), p_subscription_guid);
3266      WF_EVENT.setErrorInfo(p_event, 'ERROR');
3267 
3268      RETURN 'ERROR';
3269 
3270 END Dep_InComplete;
3271 
3272 FUNCTION Dep_Modify
3273 ( p_subscription_guid In RAW
3274 , p_event IN OUT NOCOPY WF_EVENT_T
3275 )
3276 RETURN VARCHAR2 IS
3277 
3278 BEGIN
3279 
3280     IF pg_debug = 'Y'
3281     THEN
3282         debug ('AR_BUS_EVENT_SUB_PVT.Dep_Modify(+)');
3283     END IF;
3284   Return 'SUCCESS';
3285     IF pg_debug = 'Y'
3286     THEN
3287         debug ('AR_BUS_EVENT_SUB_PVT.Dep_Modify(-)');
3288     END IF;
3289 END Dep_Modify;
3290 
3291 FUNCTION CB_Create
3292 ( p_subscription_guid In RAW
3293 , p_event IN OUT NOCOPY WF_EVENT_T
3294 )
3295 RETURN VARCHAR2 IS
3296 
3297   l_ps_rec AR_PAYMENT_SCHEDULES%rowtype;
3298   l_trx_summary_hist AR_TRX_SUMMARY_HIST%rowtype;
3299 
3300   CURSOR get_trx_history(p_cust_trx_id  IN NUMBER) IS
3301   SELECT *
3302   FROM AR_TRX_SUMMARY_HIST
3303   WHERE customer_trx_id = p_cust_trx_id
3304     and nvl(complete_flag,'N') = 'N'
3305     and amount_due_original is not null
3306     for update;
3307 
3308   CURSOR lock_ps (cust_trx_id IN NUMBER) IS
3309   SELECT *
3310   FROM ar_payment_schedules
3311   WHERE customer_trx_id = cust_trx_id
3312   FOR UPDATE;
3313 
3314   i                     INTEGER;
3315   l_key                 VARCHAR2(240) := p_event.GetEventKey();
3316   l_payment_schedule_id NUMBER(15);
3317   l_customer_trx_id     NUMBER;
3318   l_org_id              NUMBER;
3319   l_user_id             NUMBER;
3320   l_resp_id             NUMBER;
3321   l_application_id      NUMBER;
3322   l_security_gr_id      NUMBER;
3323   l_counter             NUMBER;
3324   l_history_exists_flag BOOLEAN;
3325   l_ps_exists           BOOLEAN;
3326   l_history_rec         ar_trx_summary_hist%rowtype;
3327   l_tot_inv_amt         NUMBER;
3328   l_inv_inst_count      NUMBER;
3329 BEGIN
3330     IF pg_debug = 'Y'
3331     THEN
3332         debug ('AR_BUS_EVENT_SUB_PVT.CB_Create(+)');
3333     END IF;
3334   l_customer_trx_id := p_event.GetValueForParameter('CUSTOMER_TRX_ID');
3335   l_org_id          := p_event.GetValueForParameter('ORG_ID');
3336   l_user_id         := p_event.GetValueForParameter('USER_ID');
3337   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
3338   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
3339   l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
3340     IF pg_debug = 'Y'
3341     THEN
3342        debug ('l_customer_trx_id= '||l_customer_trx_id);
3343        debug ('l_org_id= '||l_org_id);
3344        debug ('l_user_id= '||l_user_id);
3345        debug ('l_resp_id= '||l_resp_id);
3346        debug ('l_application_id= '||l_application_id);
3347        debug ('l_security_gr_id= '||l_security_gr_id);
3348     END IF;
3349   SAVEPOINT CB_Create;
3350    --
3351    --set the application context.
3352    --
3353   fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
3354   mo_global.init('AR');
3355   mo_global.set_policy_context('S',l_org_id);
3356    --
3357    -- Acquire locks on the payment schedule record so that
3358    -- the record is not changed while this subscription is
3359    -- executed.
3360    --
3361   OPEN lock_ps (l_customer_trx_id);
3362     i := 1;
3363     l_counter := 0;
3364    LOOP
3365    FETCH lock_ps INTO  l_ps_rec;
3366 
3367    IF lock_ps%NOTFOUND  THEN
3368      IF l_counter = 0 THEN
3369        l_ps_exists := FALSE;
3370      ELSE
3371        l_ps_exists := TRUE;
3372      END IF;
3373 
3374      EXIT;
3375    END IF;
3376        i := i + 1;
3377        l_counter := l_counter + 1;
3378    END LOOP;
3379   CLOSE lock_ps;
3380 
3381    --
3382    --Update the transaction history table
3383    --set the complete_flag = 'Y'
3384    --if history records exist.
3385    --
3386    UPDATE ar_trx_summary_hist
3387       set complete_flag = 'Y',
3388           LAST_UPDATE_DATE  = sysdate,
3389           LAST_UPDATED_BY   = FND_GLOBAL.user_id,
3390           LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
3391    WHERE customer_trx_id = l_customer_trx_id
3392      and nvl(complete_flag,'N') = 'N';
3393 
3394 
3395    IF SQL%NOTFOUND THEN
3396       l_history_exists_flag := FALSE;
3397    ELSE
3398       l_history_exists_flag := TRUE;
3399    END IF;
3400 
3401 
3402    IF l_ps_exists  THEN
3403     --this is a chargeback so only one ps exists
3404 
3405        --
3406        -- Update the AR_TRX_BAL_SUMMARY table
3407        --
3408 
3409         UPDATE ar_trx_bal_summary
3410           set BEST_CURRENT_RECEIVABLES
3411                        = nvl(BEST_CURRENT_RECEIVABLES,0)
3412                               +DECODE(sign(l_ps_rec.due_date - sysdate),-1,0,
3413                                                  l_ps_rec.amount_due_original),
3414               OP_CHARGEBACK_VALUE
3415                        = nvl(OP_CHARGEBACK_VALUE,0)
3416                              +l_ps_rec.amount_due_original,
3417               OP_CHARGEBACK_COUNT = nvl(OP_CHARGEBACK_COUNT,0) + 1,
3418               LAST_UPDATE_DATE  = sysdate,
3419               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
3420               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
3424            and NVL(org_id,'-99') = NVL(l_org_id,-99);
3421          WHERE cust_account_id = l_ps_rec.customer_id
3422            and site_use_id = nvl(l_ps_rec.customer_site_use_id,-99)
3423            and currency = l_ps_rec.invoice_currency_code
3425 
3426          IF SQL%NOTFOUND THEN
3427 
3428            INSERT INTO ar_trx_bal_summary
3429             (CUST_ACCOUNT_ID,
3430              SITE_USE_ID,
3431              ORG_ID,
3432              CURRENCY,
3433              last_update_date,
3434              last_updated_by,
3435              creation_date,
3436              created_by,
3437              last_update_login,
3438              BEST_CURRENT_RECEIVABLES,
3439              OP_CHARGEBACK_VALUE,
3440              OP_CHARGEBACK_COUNT
3441              )
3442              VALUES
3443             ( l_ps_rec.customer_id,
3444               nvl(l_ps_rec.customer_site_use_id,-99),
3445               l_org_id,
3446               l_ps_rec.invoice_currency_code,
3447               sysdate,
3448               fnd_global.user_id,
3449               sysdate,
3450               fnd_global.user_id,
3451               fnd_global.login_id,
3452               DECODE(sign(l_ps_rec.due_date - sysdate),-1,0,
3453                                   l_ps_rec.amount_due_original),
3454               l_ps_rec.amount_due_original,
3455               1
3456               );
3457 
3458            END IF;
3459        --
3460        -- Update the AR_TRX_SUMMARY table
3461        --
3462 
3463         UPDATE ar_trx_summary
3464           set OP_BAL_HIGH_WATERMARK = nvl(OP_BAL_HIGH_WATERMARK,0) +
3465                                                   l_ps_rec.amount_due_original,
3466               TOTAL_CHARGEBACK_VALUE = nvl(TOTAL_CHARGEBACK_VALUE,0) +
3467                                                    l_ps_rec.amount_due_original,
3468               TOTAL_CHARGEBACK_COUNT = nvl(TOTAL_CHARGEBACK_COUNT,0) + 1,
3469               LAST_UPDATE_DATE  = sysdate,
3470               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
3471               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
3472          WHERE cust_account_id = l_ps_rec.customer_id
3473            and site_use_id = nvl(l_ps_rec.customer_site_use_id,-99)
3474            and currency = l_ps_rec.invoice_currency_code
3475            and NVL(org_id,'-99') = NVL(l_org_id,-99)
3476            and as_of_date = l_ps_rec.trx_date;
3477 
3478         IF SQL%NOTFOUND THEN
3479 
3480           INSERT INTO ar_trx_summary
3481             (CUST_ACCOUNT_ID,
3482              SITE_USE_ID,
3483              ORG_ID,
3484              CURRENCY,
3485              AS_OF_DATE,
3486              last_update_date,
3487              last_updated_by,
3488              creation_date,
3489              created_by,
3490              last_update_login,
3491              OP_BAL_HIGH_WATERMARK,
3492              TOTAL_CHARGEBACK_VALUE,
3493              TOTAL_CHARGEBACK_COUNT
3494              )
3495              VALUES
3496              (l_ps_rec.customer_id,
3497               nvl(l_ps_rec.customer_site_use_id,-99),
3498               l_org_id,
3499               l_ps_rec.invoice_currency_code,
3500               l_ps_rec.trx_date,
3501               sysdate,
3502               fnd_global.user_id,
3503               sysdate,
3504               fnd_global.user_id,
3505               fnd_global.login_id,
3506               l_ps_rec.amount_due_original,
3507               l_ps_rec.amount_due_original,
3508               1
3509               );
3510            END IF;
3511 
3512    ELSE --l_ps_exists
3513      --if no payment schedule exits for the given customer_trx_id
3514      --then we do not update the summary table.
3515      null;
3516    END IF; --l_ps_exists
3517 
3518     IF pg_debug = 'Y'
3519     THEN
3520         debug ('AR_BUS_EVENT_SUB_PVT.CB_Create(-)');
3521     END IF;
3522   Return 'SUCCESS';
3523 EXCEPTION
3524     WHEN OTHERS  THEN
3525      ROLLBACK TO CB_Create;
3526 
3527      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
3528      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
3529      FND_MSG_PUB.ADD;
3530 
3531      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'CB_CREATE', p_event.getEventName(), p_subscription_guid);
3532      WF_EVENT.setErrorInfo(p_event, 'ERROR');
3533 
3534      RETURN 'ERROR';
3535 
3536 END CB_Create;
3537 
3538 FUNCTION CB_Modify
3539 ( p_subscription_guid In RAW
3540 , p_event IN OUT NOCOPY WF_EVENT_T
3541 )
3542 RETURN VARCHAR2 IS
3543 
3544 BEGIN
3545 
3546     IF pg_debug = 'Y'
3547     THEN
3548         debug ('AR_BUS_EVENT_SUB_PVT.CB_Modify(+)');
3549     END IF;
3550   Return 'SUCCESS';
3551     IF pg_debug = 'Y'
3552     THEN
3553         debug ('AR_BUS_EVENT_SUB_PVT.CB_Modify(-)');
3554     END IF;
3555 END CB_Modify;
3556 
3557 FUNCTION Guar_Complete
3558 ( p_subscription_guid In RAW
3559 , p_event IN OUT NOCOPY WF_EVENT_T
3560 )
3561 RETURN VARCHAR2 IS
3562 
3563 BEGIN
3564 
3565     IF pg_debug = 'Y'
3566     THEN
3567         debug ('AR_BUS_EVENT_SUB_PVT.Guar_Complete(+)');
3568     END IF;
3569   Return 'SUCCESS';
3570     IF pg_debug = 'Y'
3571     THEN
3572         debug ('AR_BUS_EVENT_SUB_PVT.Guar_Complete(-)');
3573     END IF;
3574 END Guar_Complete;
3575 
3576 FUNCTION Guar_InComplete
3577 ( p_subscription_guid In RAW
3578 , p_event IN OUT NOCOPY WF_EVENT_T
3579 )
3580 RETURN VARCHAR2 IS
3581 
3582 BEGIN
3583 
3584     IF pg_debug = 'Y'
3585     THEN
3586         debug ('AR_BUS_EVENT_SUB_PVT.Guar_InComplete(+)');
3587     END IF;
3588   Return 'SUCCESS';
3592     END IF;
3589     IF pg_debug = 'Y'
3590     THEN
3591         debug ('AR_BUS_EVENT_SUB_PVT.Guar_InComplete(-)');
3593 END Guar_InComplete;
3594 
3595 FUNCTION Guar_Modify
3596 ( p_subscription_guid In RAW
3597 , p_event IN OUT NOCOPY WF_EVENT_T
3598 )
3599 RETURN VARCHAR2 IS
3600 
3601 BEGIN
3602 
3603     IF pg_debug = 'Y'
3604     THEN
3605         debug ('AR_BUS_EVENT_SUB_PVT.Guar_Modify(+)');
3606     END IF;
3607   Return 'SUCCESS';
3608     IF pg_debug = 'Y'
3609     THEN
3610         debug ('AR_BUS_EVENT_SUB_PVT.Guar_Modify(-)');
3611     END IF;
3612 END Guar_Modify;
3613 
3614 FUNCTION CashReceipt_Create
3615 ( p_subscription_guid In RAW
3616 , p_event IN OUT NOCOPY WF_EVENT_T
3617 )
3618 RETURN VARCHAR2 IS
3619 
3620 l_cash_receipt_id  NUMBER;
3621 l_receipt_date   DATE ;
3622 l_receipt_amount  NUMBER;
3623 l_receipt_number  VARCHAR2(30);
3624 l_customer_id  NUMBER;
3625 l_customer_site_use_id NUMBER;
3626 l_currency_code VARCHAR2(30);
3627 
3628   l_payment_schedule_id NUMBER;
3629   l_org_id          NUMBER;
3630   l_user_id         NUMBER;
3631   l_resp_id         NUMBER;
3632   l_application_id  NUMBER;
3633   l_security_gr_id  NUMBER;
3634   -- bug 3979914
3635   -- don't raise the BE in case of unidentified receipts
3636   l_cust_account_id	ar_payment_schedules.customer_id%type;
3637 
3638   /* 9363502 - define tables used by refresh_at_risk_value */
3639   l_customer_id_tab generic_id_type;
3640   l_site_use_id_tab generic_id_type;
3641   l_org_id_tab      generic_id_type;
3642   l_currency_tab    currency_type;
3643   /* end 9363502 */
3644 
3645 CURSOR get_receipt_details (p_ps_id  IN NUMBER ) IS
3646 SELECT cash_receipt_id, trx_date, amount_due_original * -1,
3647        trx_number, customer_id, customer_site_use_id,
3648        invoice_currency_code
3649 FROM ar_payment_schedules ps
3650 WHERE payment_schedule_id = p_ps_id;
3651 
3652 l_receipt_exists   BOOLEAN := FALSE;
3653 BEGIN
3654     IF pg_debug = 'Y'
3655     THEN
3656         debug ('AR_BUS_EVENT_SUB_PVT.CashReceipt_Create(+)');
3657     END IF;
3658   l_payment_schedule_id := p_event.GetValueForParameter('PAYMENT_SCHEDULE_ID');
3659   l_org_id          := p_event.GetValueForParameter('ORG_ID');
3660   l_user_id         := p_event.GetValueForParameter('USER_ID');
3661   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
3662   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
3663   l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
3664   l_cust_account_id := p_event.GetValueForParameter('CUST_ACCOUNT_ID');
3665 
3666     IF pg_debug = 'Y'
3667     THEN
3668        debug ('l_payment_schedule_id= '||l_payment_schedule_id);
3669        debug ('l_org_id= '||l_org_id);
3670        debug ('l_user_id= '||l_user_id);
3671        debug ('l_resp_id= '||l_resp_id);
3672        debug ('l_application_id= '||l_application_id);
3673        debug ('l_security_gr_id= '||l_security_gr_id);
3674        debug ('l_cust_account_id= '||l_cust_account_id);
3675     END IF;
3676   -- bug -- bug 3979914
3677   -- don't raise the BE in case of unidentified receipts
3678   IF l_cust_account_id <> -99 -- means cust_account_id is null
3679   THEN
3680    SAVEPOINT  CR_Create_Event;
3681    --
3682    --set the application context.
3683    --
3684   fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
3685   mo_global.init('AR');
3686   mo_global.set_policy_context('S',l_org_id);
3687 
3688    --we would stamp the complete_flag on the ar_trx_summary_hist
3689    --for any records for this cash receipt. They could be due to
3690    -- receipt deletion or modification.
3691      UPDATE ar_trx_summary_hist
3692       set complete_flag = 'Y',
3693           LAST_UPDATE_DATE  = sysdate,
3694           LAST_UPDATED_BY   = FND_GLOBAL.user_id,
3695           LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
3696       WHERE payment_schedule_id = l_payment_schedule_id;
3697 
3698   OPEN get_receipt_details (l_payment_schedule_id);
3699 
3700   FETCH get_receipt_details
3701     INTO l_cash_receipt_id,
3702          l_receipt_date   ,
3703          l_receipt_amount ,
3704          l_receipt_number ,
3705          l_customer_id,
3706          l_customer_site_use_id,
3707          l_currency_code ;
3708 
3709     IF get_receipt_details%NOTFOUND THEN
3710       --this would happen if the receipt got deleted before the subscription
3711       --for the create fires.
3712       --We have already stamped the history record for the deletion with
3713       --complete = Y so that the delete subscription does not fire
3714        null;
3715     ELSE
3716       l_receipt_exists := TRUE;
3717     END IF;
3718 
3719   CLOSE get_receipt_details;
3720 
3721   IF (l_receipt_exists)
3722   THEN
3723 
3724          UPDATE ar_trx_bal_summary
3725            set unresolved_cash_value = nvl(unresolved_cash_value,0) +
3726                                                                l_receipt_amount,
3727                unresolved_cash_count = nvl(unresolved_cash_count,0) + 1,
3728                last_payment_amount = DECODE(sign(l_receipt_date-last_payment_date),
3729                                           -1,last_payment_amount,l_receipt_amount),
3730                last_payment_date =DECODE(sign(l_receipt_date-last_payment_date),
3731                                           -1,last_payment_date,l_receipt_date),
3732                last_payment_number = DECODE(sign(l_receipt_date-last_payment_date),
3733                                           -1,last_payment_number,l_receipt_number),
3734                LAST_UPDATE_DATE  = sysdate,
3738            and site_use_id =  nvl(l_customer_site_use_id,-99)
3735                LAST_UPDATED_BY   = FND_GLOBAL.user_id,
3736                LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
3737          where cust_account_id = l_customer_id
3739            and NVL(org_id,'-99') = NVL(l_org_id,-99)
3740            and currency = l_currency_code;
3741 
3742            IF sql%notfound then
3743              INSERT INTO  ar_trx_bal_summary
3744                (CUST_ACCOUNT_ID,
3745                 SITE_USE_ID,
3746                 ORG_ID,
3747                 CURRENCY,
3748                 last_update_date,
3749                 last_updated_by,
3750                 creation_date,
3751                 created_by,
3752                 last_update_login,
3753                 unresolved_cash_value,
3754                 unresolved_cash_count,
3755                 last_payment_amount,
3756                 last_payment_date,
3757                 last_payment_number
3758                )VALUES
3759                (l_customer_id,
3760                 nvl(l_customer_site_use_id,-99),
3761                 l_org_id,
3762                 l_currency_code,
3763                 sysdate,
3764                 fnd_global.user_id,
3765                 sysdate,
3766                 fnd_global.user_id,
3767                 fnd_global.login_id,
3768                 l_receipt_amount,
3769                 1,
3770                 l_receipt_amount,
3771                 l_receipt_date,
3772                 l_receipt_number
3773                 );
3774            END IF;
3775 
3776     --Update ar_trx_summary
3777          UPDATE ar_trx_summary
3778            set total_cash_receipts_value = nvl(total_cash_receipts_value,0) +
3779                                                            l_receipt_amount,
3780                total_cash_receipts_count = nvl(total_cash_receipts_count,0) + 1,
3781                LAST_UPDATE_DATE  = sysdate,
3782                LAST_UPDATED_BY   = FND_GLOBAL.user_id,
3783                LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
3784          where cust_account_id = l_customer_id
3785            and site_use_id = nvl(l_customer_site_use_id,-99)
3786            and NVL(org_id,'-99') = NVL(l_org_id,-99)
3787            and currency =   l_currency_code
3788            and as_of_date = l_receipt_date;
3789 
3790            IF sql%notfound then
3791              INSERT INTO ar_trx_summary
3792                (CUST_ACCOUNT_ID,
3793                 SITE_USE_ID,
3794                 ORG_ID,
3795                 CURRENCY,
3796                 AS_OF_DATE,
3797                 last_update_date,
3798                 last_updated_by,
3799                 creation_date,
3800                 created_by,
3801                 last_update_login,
3802                 total_cash_receipts_value,
3803                 total_cash_receipts_count
3804                 ) VALUES
3805                (l_customer_id,
3806                 nvl(l_customer_site_use_id,-99),
3807                 l_org_id,
3808                 l_currency_code,
3809                 l_receipt_date,
3810                 sysdate,
3811                 fnd_global.user_id,
3812                 sysdate,
3813                 fnd_global.user_id,
3814                 fnd_global.login_id,
3815                 l_receipt_amount,
3816                 1);
3817 
3818            END IF;
3819 
3820     END IF;
3821 
3822   /* 9363502 - call refresh_at_risk_value() */
3823   l_customer_id_tab(0) := l_customer_id;
3824   l_site_use_id_tab(0) := l_customer_site_use_id;
3825   l_currency_tab(0) := l_currency_code;
3826   l_org_id_tab(0) := l_org_id;
3827 
3828   refresh_at_risk_value(l_customer_id_tab,
3829                         l_site_use_id_tab,
3830                         l_currency_tab,
3831                         l_org_id_tab);
3832 
3833   END IF;  -- end of l_cust-account_id <> -99
3834     IF pg_debug = 'Y'
3835     THEN
3836         debug ('AR_BUS_EVENT_SUB_PVT.CashReceipt_Create(-)');
3837     END IF;
3838   Return 'SUCCESS';
3839 EXCEPTION
3840     WHEN OTHERS  THEN
3841      ROLLBACK TO CR_Create_Event;
3842 
3843      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
3844      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
3845      FND_MSG_PUB.ADD;
3846 
3847      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'CashReceipt_Create', p_event.getEventName(), p_subscription_guid);
3848      WF_EVENT.setErrorInfo(p_event, 'ERROR');
3849 
3850      RETURN 'ERROR';
3851 END CashReceipt_Create;
3852 
3853 FUNCTION CashReceipt_Reverse
3854 ( p_subscription_guid In RAW
3855 , p_event IN OUT NOCOPY WF_EVENT_T
3856 ) RETURN VARCHAR2 IS
3857 l_cash_receipt_id      NUMBER;
3858 l_reversal_date         DATE ;
3859 l_receipt_amount       NUMBER;
3860 l_receipt_number       VARCHAR2(30);
3861 l_customer_id          NUMBER;
3862 l_customer_site_use_id NUMBER;
3863 l_currency_code        VARCHAR2(30);
3864 l_history_id           NUMBER;
3865 l_payment_schedule_id  NUMBER;
3866 l_org_id               NUMBER;
3867 l_user_id              NUMBER;
3868 l_resp_id              NUMBER;
3869 l_application_id       NUMBER;
3870 l_security_gr_id       NUMBER;
3871 l_reversal_category    VARCHAR2(20);
3872 l_last_receipt_number  VARCHAR2(30);
3873 l_last_receipt_amount  NUMBER;
3874 l_last_receipt_date    DATE;
3875 is_last_payment        VARCHAR2(1);
3876 l_unresolved_cash      NUMBER;
3877 l_receipt_date         DATE;
3878 
3879 CURSOR get_receipt_details (p_ps_id  IN NUMBER ) IS
3880 SELECT ps.amount_due_original * -1,
3881        ps.trx_number, ps.customer_id, ps.customer_site_use_id,
3882        ps.invoice_currency_code,
3883        cr.reversal_category,
3884        cr.reversal_date,
3885        cr.receipt_date,
3889            'OTHER ACC',nvl(ra.amount_applied_from,ra.amount_applied),
3886        sum(DECODE(ra.status,
3887            'UNAPP', nvl(ra.amount_applied_from,ra.amount_applied),
3888            'ACC', nvl(ra.amount_applied_from,ra.amount_applied),
3890                              null)) unresolved_cash
3891 FROM ar_payment_schedules ps,
3892      ar_cash_receipts cr,
3893      ar_cash_receipt_history crh,
3894      ar_receivable_applications ra
3895 WHERE ps.payment_schedule_id = p_ps_id
3896   and ps.cash_receipt_id = cr.cash_receipt_id
3897   and crh.cash_receipt_id = cr.cash_receipt_id
3898   and crh.cash_receipt_history_id = ra.cash_receipt_history_id -- apandit
3899   and crh.status = 'REVERSED'
3900   and ra.cash_receipt_id = cr.cash_receipt_id
3901 group by ps.amount_due_original,
3902        ps.trx_number, ps.customer_id, ps.customer_site_use_id,
3903        ps.invoice_currency_code,
3904        cr.reversal_category,
3905        cr.reversal_date,
3906        cr.receipt_date;
3907 
3908 CURSOR is_this_last_payment (p_customer_id IN NUMBER,
3909                              p_site_use_id IN NUMBER,
3910                              p_currency    IN VARCHAR2,
3911                              p_pmt_number  IN VARCHAR2,
3912                              p_pmt_date    IN DATE,
3913                              p_org_id      IN NUMBER)IS
3914 select 'Y'
3915 from   ar_trx_bal_summary
3916 where  cust_account_id = p_customer_id
3917   and  site_use_id = p_site_use_id
3918   and  currency = p_currency
3919   and  last_payment_number = p_pmt_number
3920   and  last_payment_date = p_pmt_date
3921   and  NVL(org_id,'-99') = NVL(p_org_id,-99);
3922 
3923 CURSOR get_last_pmt (p_customer_id IN NUMBER,
3924                      p_site_use_id IN NUMBER,
3925                      p_currency    IN VARCHAR2) IS
3926 select receipt_number, amount, receipt_date
3927 from ar_cash_receipts
3928 where cash_receipt_id =
3929      (select max(cr.cash_receipt_id)
3930       from ar_cash_receipts cr,
3931            ar_cash_receipt_history crh --apandit
3932       where cr.pay_from_customer = p_customer_id
3933         and cr.cash_receipt_id = crh.cash_receipt_id --apandit
3934         and crh.current_record_flag = 'Y'
3935         and crh.status <> 'REVERSED'
3936         and nvl(cr.customer_site_use_id,-99) = nvl(p_site_use_id, -99)
3937         and cr.currency_code = p_currency);
3938 
3939 BEGIN
3940     IF pg_debug = 'Y'
3941     THEN
3942         debug ('AR_BUS_EVENT_SUB_PVT.CashReceipt_Reverse(+)');
3943     END IF;
3944   l_payment_schedule_id := p_event.GetValueForParameter('PAYMENT_SCHEDULE_ID');
3945   l_cash_receipt_id     := p_event.GetValueForParameter('CASH_RECEIPT_ID');
3946   l_history_id          := p_event.GetValueForParameter('HISTORY_ID');
3947   l_org_id          := p_event.GetValueForParameter('ORG_ID');
3948   l_user_id         := p_event.GetValueForParameter('USER_ID');
3949   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
3950   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
3951   l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
3952     IF pg_debug = 'Y'
3953     THEN
3954        debug ('l_payment_schedule_id= '||l_payment_schedule_id);
3955        debug ('l_cash_receipt_id= '||l_cash_receipt_id);
3956        debug ('l_history_id= '||l_history_id);
3957        debug ('l_org_id= '||l_org_id);
3958        debug ('l_user_id= '||l_user_id);
3959        debug ('l_resp_id= '||l_resp_id);
3960        debug ('l_application_id= '||l_application_id);
3961        debug ('l_security_gr_id= '||l_security_gr_id);
3962     END IF;
3963    SAVEPOINT  CR_Reverse;
3964    --
3965    --set the application context.
3966    --
3967   fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
3968   mo_global.init('AR');
3969   mo_global.set_policy_context('S',l_org_id);
3970 
3971      UPDATE ar_trx_summary_hist
3972       set complete_flag = 'Y',
3973           LAST_UPDATE_DATE  = sysdate,
3974           LAST_UPDATED_BY   = FND_GLOBAL.user_id,
3975           LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
3976       WHERE payment_schedule_id = l_payment_schedule_id;
3977 
3978   OPEN get_receipt_details (l_payment_schedule_id);
3979 
3980   FETCH get_receipt_details
3981     INTO l_receipt_amount ,
3982          l_receipt_number ,
3983          l_customer_id,
3984          l_customer_site_use_id,
3985          l_currency_code,
3986          l_reversal_category,
3987          l_reversal_date,
3988          l_receipt_date,
3989          l_unresolved_cash;
3990 
3991   -- both l_receipt_amount and l_unresolved_cash have the -ve sign.
3992 
3993   CLOSE get_receipt_details;
3994 
3995  IF l_customer_id IS NOT NULL THEN
3996 
3997   OPEN is_this_last_payment (l_customer_id,
3998                              l_customer_site_use_id,
3999                              l_currency_code,
4000                              l_receipt_number,
4001                              l_receipt_date,
4002                              l_org_id);
4003 
4004    FETCH is_this_last_payment INTO is_last_payment;
4005      IF nvl(is_last_payment,'N') = 'Y' THEN
4006        OPEN get_last_pmt (l_customer_id,
4007                           l_customer_site_use_id,
4008                           l_currency_code);
4009         FETCH get_last_pmt INTO
4010                    l_last_receipt_number,
4011                    l_last_receipt_amount,
4012                    l_last_receipt_date;
4013 
4014        CLOSE get_last_pmt;
4015      END IF;
4016 
4017   CLOSE is_this_last_payment;
4018 
4019     --Update ar_trx_bal_summary
4020          UPDATE ar_trx_bal_summary
4021            set unresolved_cash_value = nvl(unresolved_cash_value,0) -
4025                last_payment_date = nvl(l_last_receipt_date,last_payment_date),
4022                                                                l_unresolved_cash,
4023                unresolved_cash_count = nvl(unresolved_cash_count,0) - 1,
4024                last_payment_amount = nvl(l_last_receipt_amount,last_payment_amount),
4026                last_payment_number = nvl(l_last_receipt_number,last_payment_number),
4027               LAST_UPDATE_DATE  = sysdate,
4028               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
4029               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
4030          where cust_account_id = l_customer_id
4031            and site_use_id =  nvl(l_customer_site_use_id,-99)
4032            and NVL(org_id,'-99') = NVL(l_org_id,-99)
4033            and currency = l_currency_code;
4034 
4035 
4036     --Update ar_trx_summary
4037          UPDATE ar_trx_summary
4038            set total_cash_receipts_value = nvl(total_cash_receipts_value,0) -
4039                                                            nvl(l_receipt_amount,0),
4040                total_cash_receipts_count = nvl(total_cash_receipts_count,0) - 1,
4041                nsf_stop_payment_amount = nvl(nsf_stop_payment_amount,0)
4042                                                          - nvl(l_receipt_amount,0),
4043                nsf_stop_payment_count  = nvl(nsf_stop_payment_count,0) + 1,
4044               LAST_UPDATE_DATE  = sysdate,
4045               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
4046               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
4047          where cust_account_id = l_customer_id
4048            and site_use_id = nvl(l_customer_site_use_id,-99)
4049            and NVL(org_id,'-99') = NVL(l_org_id,-99)
4050            and currency =   l_currency_code
4051            and as_of_date = l_reversal_date;
4052 
4053    END IF;
4054 
4055     IF pg_debug = 'Y'
4056     THEN
4057         debug ('AR_BUS_EVENT_SUB_PVT.CashReceipt_Reverse(-)');
4058     END IF;
4059   Return 'SUCCESS';
4060 EXCEPTION
4061     WHEN OTHERS  THEN
4062      ROLLBACK TO CR_Reverse;
4063 
4064      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
4065      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
4066      FND_MSG_PUB.ADD;
4067 
4068      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'CashReceipt_Reverse', p_event.getEventName(), p_subscription_guid);
4069      WF_EVENT.setErrorInfo(p_event, 'ERROR');
4070 
4071      RETURN 'ERROR';
4072 END CashReceipt_Reverse;
4073 
4074 FUNCTION CashReceipt_Modify
4075 ( p_subscription_guid In RAW
4076 , p_event IN OUT NOCOPY WF_EVENT_T
4077 )
4078 RETURN VARCHAR2 IS
4079 
4080 l_cash_receipt_id  NUMBER;
4081 l_receipt_date   DATE ;
4082 l_receipt_amount  NUMBER;
4083 l_receipt_number  VARCHAR2(30);
4084 l_customer_id  NUMBER;
4085 l_customer_site_use_id NUMBER;
4086 l_currency_code VARCHAR2(30);
4087 
4088   l_payment_schedule_id NUMBER;
4089   l_org_id          NUMBER;
4090   l_user_id         NUMBER;
4091   l_resp_id         NUMBER;
4092   l_application_id  NUMBER;
4093   l_security_gr_id  NUMBER;
4094 
4095 CURSOR get_receipt_details (p_ps_id  IN NUMBER ) IS
4096 SELECT cash_receipt_id, trx_date, amount_due_original * -1,
4097        trx_number, customer_id, customer_site_use_id,
4098        invoice_currency_code
4099 FROM ar_payment_schedules ps
4100 WHERE payment_schedule_id = p_ps_id;
4101 
4102 CURSOR get_receipt_hist (p_hist_id  IN NUMBER) IS
4103  select *
4104  from ar_trx_summary_hist
4105  where history_id = p_hist_id
4106    and nvl(complete_flag ,'N') = 'N'
4107  for update;
4108 
4109 CURSOR get_receipt_hist2 (p_hist_id  IN NUMBER) IS
4110  select ps.cash_receipt_id, ps.trx_date, hist.amount_due_original * -1,
4111        ps.trx_number, hist.customer_id, hist.site_use_id,
4112        ps.invoice_currency_code
4113  from ar_trx_summary_hist hist,
4114       ar_payment_schedules ps
4115  where previous_history_id = p_hist_id
4116    and ps.payment_schedule_id = hist.payment_schedule_id;
4117 
4118 l_hist_rec ar_trx_summary_hist%rowtype;
4119 l_hist_rec2 ar_trx_summary_hist%rowtype;
4120 l_receipt_exists   VARCHAR2(10);
4121 l_history_id   NUMBER;
4122 l_history2_exists_flag  VARCHAR2(1);
4123 l_history_exists_flag   VARCHAR2(1);
4124 BEGIN
4125     IF pg_debug = 'Y'
4126     THEN
4127         debug ('AR_BUS_EVENT_SUB_PVT.CashReceipt_Modify(+)');
4128     END IF;
4129   l_payment_schedule_id := p_event.GetValueForParameter('PAYMENT_SCHEDULE_ID');
4130   l_history_id      := p_event.GetValueForParameter('HISTORY_ID');
4131   l_cash_receipt_id := p_event.GetValueForParameter('CASH_RECEIPT_ID');
4132   l_org_id          := p_event.GetValueForParameter('ORG_ID');
4133   l_user_id         := p_event.GetValueForParameter('USER_ID');
4134   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
4135   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
4136   l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
4137     IF pg_debug = 'Y'
4138     THEN
4139        debug ('l_payment_schedule_id= '||l_payment_schedule_id);
4140        debug ('l_history_id= '||l_history_id);
4141        debug ('l_cash_receipt_id= '||l_cash_receipt_id);
4142        debug ('l_org_id= '||l_org_id);
4143        debug ('l_user_id= '||l_user_id);
4144        debug ('l_resp_id= '||l_resp_id);
4145        debug ('l_application_id= '||l_application_id);
4146        debug ('l_security_gr_id= '||l_security_gr_id);
4147     END IF;
4148    SAVEPOINT  CR_Modify;
4149    --
4150    --set the application context.
4151    --
4152   fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
4153   mo_global.init('AR');
4154   mo_global.set_policy_context('S',l_org_id);
4155 
4156    OPEN get_receipt_hist(l_history_id);
4157     FETCH get_receipt_hist INTO l_hist_rec;
4158 
4159    IF get_receipt_hist%NOTFOUND THEN
4163     OPEN get_receipt_hist2(l_history_id);
4160     l_history_exists_flag  := 'N';
4161    ELSE
4162     l_history_exists_flag  := 'Y';
4164 
4165      FETCH get_receipt_hist2 INTO
4166                   l_cash_receipt_id ,
4167                   l_receipt_date ,
4168                   l_receipt_amount ,
4169                   l_receipt_number ,
4170                   l_customer_id ,
4171                   l_customer_site_use_id ,
4172                   l_currency_code ;
4173 
4174      IF get_receipt_hist2%notfound THEN
4175        --get the data from the
4176         l_history2_exists_flag := 'N';
4177          OPEN get_receipt_details (l_payment_schedule_id);
4178 
4179           FETCH get_receipt_details
4180              INTO l_cash_receipt_id ,
4181          	  l_receipt_date ,
4182        		  l_receipt_amount ,
4183               l_receipt_number ,
4184          	  l_customer_id ,
4185         	  l_customer_site_use_id ,
4186        		  l_currency_code ;
4187 
4188             IF get_receipt_details%NOTFOUND THEN
4189                null;
4190               l_receipt_exists := 'N';
4191             ELSE
4192               l_receipt_exists := 'Y';
4193             END IF;
4194 
4195          CLOSE get_receipt_details;
4196      ELSE
4197         l_history2_exists_flag := 'Y';
4198      END IF;
4199 
4200     CLOSE get_receipt_hist2;
4201 
4202    END IF;
4203   CLOSE get_receipt_hist;
4204 
4205      UPDATE ar_trx_summary_hist
4206       set complete_flag = 'Y',
4207           LAST_UPDATE_DATE  = sysdate,
4208           LAST_UPDATED_BY   = FND_GLOBAL.user_id,
4209           LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
4210       WHERE history_id  = l_history_id
4211        and  nvl(complete_flag ,'N') = 'N';
4212 
4213 
4214   IF (l_history_exists_flag = 'Y') AND
4215       ((l_history2_exists_flag = 'Y') OR (l_receipt_exists = 'Y'))
4216     THEN
4217 
4218     IF l_customer_id =  l_hist_rec.customer_id THEN
4219     --Update ar_trx_bal_summary
4220          UPDATE ar_trx_bal_summary
4221            set unresolved_cash_value = nvl(unresolved_cash_value,0) +
4222                                            (l_receipt_amount -
4223                                             nvl(l_hist_rec.amount_due_original,0)),
4224                unresolved_cash_count = nvl(unresolved_cash_count,0),
4225 			   last_payment_amount =  l_receipt_amount,
4226 			   last_payment_date = l_receipt_date,
4227 			   last_payment_number = l_receipt_number,
4228               LAST_UPDATE_DATE  = sysdate,
4229               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
4230               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
4231          where cust_account_id = l_customer_id
4232            and site_use_id =  nvl(l_customer_site_use_id,-99)
4233            and NVL(org_id,'-99') = NVL(l_org_id,-99)
4234            and currency = l_currency_code;
4235 
4236          IF sql%notfound then
4237              INSERT INTO  ar_trx_bal_summary
4238                (CUST_ACCOUNT_ID,
4239                 SITE_USE_ID,
4240                 ORG_ID,
4241                 CURRENCY,
4242                 last_update_date,
4243                 last_updated_by,
4244                 creation_date,
4245                 created_by,
4246                 last_update_login,
4247                 unresolved_cash_value,
4248                 unresolved_cash_count,
4249                 last_payment_amount,
4250                 last_payment_date,
4251                 last_payment_number
4252                )VALUES
4253                (l_customer_id,
4254                 nvl(l_customer_site_use_id,-99),
4255                 l_org_id,
4256                 l_currency_code,
4257                 sysdate,
4258                 fnd_global.user_id,
4259                 sysdate,
4260                 fnd_global.user_id,
4261                 fnd_global.login_id,
4262                 (l_receipt_amount - nvl(l_hist_rec.amount_due_original,0)),
4263                 1,
4264                 (l_receipt_amount - nvl(l_hist_rec.amount_due_original,0)),
4265                 l_receipt_date,
4266                 l_receipt_number
4267                 );
4268            END IF;
4269 
4270 
4271 
4272     --Update ar_trx_summary
4273          UPDATE ar_trx_summary
4274            set total_cash_receipts_value = nvl(total_cash_receipts_value,0) +
4275                                              (l_receipt_amount - nvl(l_hist_rec.amount_due_original,0)),
4276                total_cash_receipts_count = nvl(total_cash_receipts_count,0),
4277               LAST_UPDATE_DATE  = sysdate,
4278               LAST_UPDATED_BY   = FND_GLOBAL.user_id,
4279               LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
4280          where cust_account_id = l_customer_id
4281            and site_use_id = nvl(l_customer_site_use_id,-99)
4282            and NVL(org_id,'-99') = NVL(l_org_id,-99)
4283            and currency =   l_currency_code
4284            and as_of_date = l_receipt_date;
4285 
4286 
4287          IF sql%notfound then
4288              INSERT INTO ar_trx_summary
4289                (CUST_ACCOUNT_ID,
4290                 SITE_USE_ID,
4291                 ORG_ID,
4292                 CURRENCY,
4293                 AS_OF_DATE,
4294                 last_update_date,
4295                 last_updated_by,
4296                 creation_date,
4297                 created_by,
4298                 last_update_login,
4299                 total_cash_receipts_value,
4300                 total_cash_receipts_count
4301                 ) VALUES
4302                (l_customer_id,
4303                 nvl(l_customer_site_use_id,-99),
4307                 sysdate,
4304                 l_org_id,
4305                 l_currency_code,
4306                 l_receipt_date,
4308                 fnd_global.user_id,
4309                 sysdate,
4310                 fnd_global.user_id,
4311                 fnd_global.login_id,
4312                 (l_receipt_amount - nvl(l_hist_rec.amount_due_original,0)),
4313                 1);
4314 
4315            END IF;
4316      ELSIF l_customer_id IS NULL AND  l_hist_rec.customer_id IS NOT NULL THEN
4317              --Update ar_trx_bal_summary
4318          UPDATE ar_trx_bal_summary
4319            set unresolved_cash_value = nvl(unresolved_cash_value,0)
4320                                                - nvl(l_hist_rec.amount_due_original,0),
4321                unresolved_cash_count = nvl(unresolved_cash_count,0) -1,
4322                last_payment_amount =  nvl(l_hist_rec.amount_due_original,0),
4323 	       last_payment_date = l_receipt_date,
4324 	       last_payment_number = l_receipt_number,
4325                LAST_UPDATE_DATE  = sysdate,
4326                LAST_UPDATED_BY   = FND_GLOBAL.user_id,
4327                LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
4328          where cust_account_id = l_hist_rec.customer_id
4329            and site_use_id =  nvl(l_hist_rec.site_use_id,-99)
4330            and NVL(org_id,'-99') = NVL(l_org_id,-99)
4331            and currency = l_currency_code;
4332 
4333          IF sql%notfound then
4334              INSERT INTO  ar_trx_bal_summary
4335                (CUST_ACCOUNT_ID,
4336                 SITE_USE_ID,
4337                 ORG_ID,
4338                 CURRENCY,
4339                 last_update_date,
4340                 last_updated_by,
4341                 creation_date,
4342                 created_by,
4343                 last_update_login,
4344                 unresolved_cash_value,
4345                 unresolved_cash_count,
4346                 last_payment_amount,
4347                 last_payment_date,
4348                 last_payment_number
4349                )VALUES
4350                (l_hist_rec.customer_id,
4351                 nvl(l_hist_rec.site_use_id,-99),
4352                 l_org_id,
4353                 l_currency_code,
4354                 sysdate,
4355                 fnd_global.user_id,
4356                 sysdate,
4357                 fnd_global.user_id,
4358                 fnd_global.login_id,
4359                 l_hist_rec.amount_due_original,
4360                 1,
4361                 l_hist_rec.amount_due_original,
4362                 l_receipt_date,
4363                 l_receipt_number
4364                 );
4365            END IF;
4366 
4367     --Update ar_trx_summary
4368          UPDATE ar_trx_summary
4369            set total_cash_receipts_value = nvl(total_cash_receipts_value,0)
4370                                                  - nvl(l_hist_rec.amount_due_original,0) ,
4371                total_cash_receipts_count = nvl(total_cash_receipts_count,0) -1,
4372                LAST_UPDATE_DATE  = sysdate,
4373                LAST_UPDATED_BY   = FND_GLOBAL.user_id,
4374                LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
4375          where cust_account_id = l_hist_rec.customer_id
4376            and site_use_id = nvl(l_hist_rec.site_use_id,-99)
4377            and NVL(org_id,'-99') = NVL(l_org_id,-99)
4378            and currency =   l_currency_code
4379            and as_of_date = l_receipt_date;
4380 
4381           IF sql%notfound then
4382              INSERT INTO ar_trx_summary
4383                (CUST_ACCOUNT_ID,
4384                 SITE_USE_ID,
4385                 ORG_ID,
4386                 CURRENCY,
4387                 AS_OF_DATE,
4388                 last_update_date,
4389                 last_updated_by,
4390                 creation_date,
4391                 created_by,
4392                 last_update_login,
4393                 total_cash_receipts_value,
4394                 total_cash_receipts_count
4395                 ) VALUES
4396                (l_hist_rec.customer_id,
4397                 nvl(l_hist_rec.site_use_id,-99),
4398                 l_org_id,
4399                 l_currency_code,
4400                 l_receipt_date,
4401                 sysdate,
4402                 fnd_global.user_id,
4403                 sysdate,
4404                 fnd_global.user_id,
4405                 fnd_global.login_id,
4406                 l_hist_rec.amount_due_original,
4407                 1);
4408  		END IF;
4409      ELSIF l_customer_id IS NOT NULL AND  l_hist_rec.customer_id IS  NULL THEN
4410              --Update ar_trx_bal_summary
4411          UPDATE ar_trx_bal_summary
4412            set unresolved_cash_value = nvl(unresolved_cash_value,0)
4413                                                +l_receipt_amount ,
4414                unresolved_cash_count = nvl(unresolved_cash_count,0) +1,
4415                last_payment_amount =  l_receipt_amount,
4416 	       last_payment_date = l_receipt_date,
4417 	       last_payment_number = l_receipt_number,
4418                LAST_UPDATE_DATE  = sysdate,
4419                LAST_UPDATED_BY   = FND_GLOBAL.user_id,
4420                LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
4421          where cust_account_id = l_customer_id
4422            and site_use_id =  nvl(l_customer_site_use_id,-99)
4423            and NVL(org_id,'-99') = NVL(l_org_id,-99)
4424            and currency = l_currency_code;
4425 
4426          IF sql%notfound then
4427              INSERT INTO  ar_trx_bal_summary
4428                (CUST_ACCOUNT_ID,
4429                 SITE_USE_ID,
4430                 ORG_ID,
4431                 CURRENCY,
4432                 last_update_date,
4433                 last_updated_by,
4434                 creation_date,
4435                 created_by,
4436                 last_update_login,
4437                 unresolved_cash_value,
4441                 last_payment_number
4438                 unresolved_cash_count,
4439                 last_payment_amount,
4440                 last_payment_date,
4442                )VALUES
4443                (l_customer_id,
4444                 nvl(l_customer_site_use_id,-99),
4445                 l_org_id,
4446                 l_currency_code,
4447                 sysdate,
4448                 fnd_global.user_id,
4449                 sysdate,
4450                 fnd_global.user_id,
4451                 fnd_global.login_id,
4452                 l_receipt_amount,
4453                 1,
4454                 l_receipt_amount,
4455                 l_receipt_date,
4456                 l_receipt_number
4457                 );
4458            END IF;
4459 
4460     --Update ar_trx_summary
4461          UPDATE ar_trx_summary
4462            set total_cash_receipts_value = nvl(total_cash_receipts_value,0)
4463                                                  + l_receipt_amount ,
4464                total_cash_receipts_count = nvl(total_cash_receipts_count,0)+1,
4465                LAST_UPDATE_DATE  = sysdate,
4466                LAST_UPDATED_BY   = FND_GLOBAL.user_id,
4467                LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
4468          where cust_account_id = l_customer_id
4469            and site_use_id = nvl(l_customer_site_use_id,-99)
4470            and NVL(org_id,'-99') = NVL(l_org_id,-99)
4471            and currency =   l_currency_code
4472            and as_of_date = l_receipt_date;
4473 
4474          IF sql%notfound then
4475              INSERT INTO ar_trx_summary
4476                (CUST_ACCOUNT_ID,
4477                 SITE_USE_ID,
4478                 ORG_ID,
4479                 CURRENCY,
4480                 AS_OF_DATE,
4481                 last_update_date,
4482                 last_updated_by,
4483                 creation_date,
4484                 created_by,
4485                 last_update_login,
4486                 total_cash_receipts_value,
4487                 total_cash_receipts_count
4488                 ) VALUES
4489                (l_customer_id,
4490                 nvl(l_customer_site_use_id,-99),
4491                 l_org_id,
4492                 l_currency_code,
4493                 l_receipt_date,
4494                 sysdate,
4495                 fnd_global.user_id,
4496                 sysdate,
4497                 fnd_global.user_id,
4498                 fnd_global.login_id,
4499                 l_receipt_amount,
4500                 1);
4501  		END IF;
4502      ELSIF nvl(l_customer_id,0) <> nvl(l_hist_rec.customer_id,0)  THEN
4503 
4504       --Update ar_trx_bal_summary
4505          UPDATE ar_trx_bal_summary
4506            set unresolved_cash_value = nvl(unresolved_cash_value,0)
4507                                                - nvl(l_hist_rec.amount_due_original,0),
4508                unresolved_cash_count = nvl(unresolved_cash_count,0) -1,
4509                last_payment_amount =  nvl(l_hist_rec.amount_due_original,0),
4510 			   last_payment_date = l_receipt_date,
4511 			   last_payment_number = l_receipt_number,
4512                LAST_UPDATE_DATE  = sysdate,
4513                LAST_UPDATED_BY   = FND_GLOBAL.user_id,
4514                LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
4515          where cust_account_id = l_hist_rec.customer_id
4516            and site_use_id =  nvl(l_hist_rec.site_use_id,-99)
4517            and NVL(org_id,'-99') = NVL(l_org_id,-99)
4518            and currency = l_currency_code;
4519 
4520           IF sql%notfound then
4521              INSERT INTO  ar_trx_bal_summary
4522                (CUST_ACCOUNT_ID,
4523                 SITE_USE_ID,
4524                 ORG_ID,
4525                 CURRENCY,
4526                 last_update_date,
4527                 last_updated_by,
4528                 creation_date,
4529                 created_by,
4530                 last_update_login,
4531                 unresolved_cash_value,
4532                 unresolved_cash_count,
4533                 last_payment_amount,
4534                 last_payment_date,
4535                 last_payment_number
4536                )VALUES
4537                (l_hist_rec.customer_id,
4538                 nvl(l_hist_rec.site_use_id,-99),
4539                 l_org_id,
4540                 l_currency_code,
4541                 sysdate,
4542                 fnd_global.user_id,
4543                 sysdate,
4544                 fnd_global.user_id,
4545                 fnd_global.login_id,
4546                 l_hist_rec.amount_due_original,
4547                 1,
4548                 l_hist_rec.amount_due_original,
4549                 l_receipt_date,
4550                 l_receipt_number
4551                 );
4552            END IF;
4553 
4554      --Update ar_trx_summary
4555          UPDATE ar_trx_summary
4556            set total_cash_receipts_value = nvl(total_cash_receipts_value,0)
4557                                                  - nvl(l_hist_rec.amount_due_original,0) ,
4558                total_cash_receipts_count = nvl(total_cash_receipts_count,0),
4559                LAST_UPDATE_DATE  = sysdate,
4560                LAST_UPDATED_BY   = FND_GLOBAL.user_id,
4561                LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
4562          where cust_account_id = l_hist_rec.customer_id
4563            and site_use_id = nvl(l_hist_rec.site_use_id,-99)
4564            and NVL(org_id,'-99') = NVL(l_org_id,-99)
4565            and currency =   l_currency_code
4566            and as_of_date = l_receipt_date;
4567 
4568           IF sql%notfound then
4569              INSERT INTO ar_trx_summary
4570                (CUST_ACCOUNT_ID,
4571                 SITE_USE_ID,
4572                 ORG_ID,
4573                 CURRENCY,
4574                 AS_OF_DATE,
4575                 last_update_date,
4579                 last_update_login,
4576                 last_updated_by,
4577                 creation_date,
4578                 created_by,
4580                 total_cash_receipts_value,
4581                 total_cash_receipts_count
4582                 ) VALUES
4583                (l_hist_rec.customer_id,
4584                 nvl(l_hist_rec.site_use_id,-99),
4585                 l_org_id,
4586                 l_currency_code,
4587                 l_receipt_date,
4588                 sysdate,
4589                 fnd_global.user_id,
4590                 sysdate,
4591                 fnd_global.user_id,
4592                 fnd_global.login_id,
4593                 l_hist_rec.amount_due_original,
4594                 1);
4595  		END IF;
4596 
4597       --Update ar_trx_bal_summary
4598          UPDATE ar_trx_bal_summary
4599            set unresolved_cash_value = nvl(unresolved_cash_value,0)
4600                                                +l_receipt_amount ,
4601                unresolved_cash_count = nvl(unresolved_cash_count,0)+1,
4602                last_payment_amount =  l_receipt_amount,
4603 			   last_payment_date = l_receipt_date,
4604 			   last_payment_number = l_receipt_number,
4605                LAST_UPDATE_DATE  = sysdate,
4606                LAST_UPDATED_BY   = FND_GLOBAL.user_id,
4607                LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
4608          where cust_account_id = l_customer_id
4609            and site_use_id =  nvl(l_customer_site_use_id,-99)
4610            and NVL(org_id,'-99') = NVL(l_org_id,-99)
4611            and currency = l_currency_code;
4612 
4613          IF sql%notfound then
4614              INSERT INTO  ar_trx_bal_summary
4615                (CUST_ACCOUNT_ID,
4616                 SITE_USE_ID,
4617                 ORG_ID,
4618                 CURRENCY,
4619                 last_update_date,
4620                 last_updated_by,
4621                 creation_date,
4622                 created_by,
4623                 last_update_login,
4624                 unresolved_cash_value,
4625                 unresolved_cash_count,
4626                 last_payment_amount,
4627                 last_payment_date,
4628                 last_payment_number
4629                )VALUES
4630                (l_customer_id,
4631                 nvl(l_customer_site_use_id,-99),
4632                 l_org_id,
4633                 l_currency_code,
4634                 sysdate,
4635                 fnd_global.user_id,
4636                 sysdate,
4637                 fnd_global.user_id,
4638                 fnd_global.login_id,
4639                 l_receipt_amount,
4640                 1,
4641                 l_receipt_amount,
4642                 l_receipt_date,
4643                 l_receipt_number
4644                 );
4645            END IF;
4646 
4647 
4648     --Update ar_trx_summary
4649          UPDATE ar_trx_summary
4650            set total_cash_receipts_value = nvl(total_cash_receipts_value,0)
4651                                                  + l_receipt_amount ,
4652                total_cash_receipts_count = nvl(total_cash_receipts_count,0)+1,
4653                LAST_UPDATE_DATE  = sysdate,
4654                LAST_UPDATED_BY   = FND_GLOBAL.user_id,
4655                LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
4656          where cust_account_id = l_customer_id
4657            and site_use_id = nvl(l_customer_site_use_id,-99)
4658            and NVL(org_id,'-99') = NVL(l_org_id,-99)
4659            and currency =   l_currency_code
4660            and as_of_date = l_receipt_date;
4661 
4662          IF sql%notfound then
4663              INSERT INTO ar_trx_summary
4664                (CUST_ACCOUNT_ID,
4665                 SITE_USE_ID,
4666                 ORG_ID,
4667                 CURRENCY,
4668                 AS_OF_DATE,
4669                 last_update_date,
4670                 last_updated_by,
4671                 creation_date,
4672                 created_by,
4673                 last_update_login,
4674                 total_cash_receipts_value,
4675                 total_cash_receipts_count
4676                 ) VALUES
4677                (l_customer_id,
4678                 nvl(l_customer_site_use_id,-99),
4679                 l_org_id,
4680                 l_currency_code,
4681                 l_receipt_date,
4682                 sysdate,
4683                 fnd_global.user_id,
4684                 sysdate,
4685                 fnd_global.user_id,
4686                 fnd_global.login_id,
4687                 l_receipt_amount ,
4688                 1);
4689  		END IF;
4690 
4691      END IF;
4692 
4693   END IF;
4694     IF pg_debug = 'Y'
4695     THEN
4696         debug ('AR_BUS_EVENT_SUB_PVT.CashReceipt_Modify(-)');
4697     END IF;
4698 
4699   Return 'SUCCESS';
4700 EXCEPTION
4701     WHEN OTHERS  THEN
4702      ROLLBACK TO CR_Modify;
4703 
4704      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
4705      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
4706      FND_MSG_PUB.ADD;
4707 
4708      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'CashReceipt_Modify', p_event.getEventName(), p_subscription_guid);
4709      WF_EVENT.setErrorInfo(p_event, 'ERROR');
4710 
4711      RETURN 'ERROR';
4712 
4713 END CashReceipt_Modify;
4714 
4715 FUNCTION CashReceipt_Approve
4716 ( p_subscription_guid In RAW
4717 , p_event IN OUT NOCOPY WF_EVENT_T
4718 )
4719 RETURN VARCHAR2 IS
4720 
4721 BEGIN
4722 
4723     IF pg_debug = 'Y'
4724     THEN
4725         debug ('AR_BUS_EVENT_SUB_PVT.CashReceipt_Approve(+)');
4726     END IF;
4727   Return 'SUCCESS';
4728     IF pg_debug = 'Y'
4729     THEN
4730         debug ('AR_BUS_EVENT_SUB_PVT.CashReceipt_Approve(-)');
4731     END IF;
4735 ( p_subscription_guid In RAW
4732 END CashReceipt_Approve;
4733 
4734 FUNCTION CashReceipt_Confirm
4736 , p_event IN OUT NOCOPY WF_EVENT_T
4737 )
4738 RETURN VARCHAR2 IS
4739 
4740 BEGIN
4741 
4742     IF pg_debug = 'Y'
4743     THEN
4744         debug ('AR_BUS_EVENT_SUB_PVT.CashReceipt_Confirm(+)');
4745     END IF;
4746   Return 'SUCCESS';
4747     IF pg_debug = 'Y'
4748     THEN
4749         debug ('AR_BUS_EVENT_SUB_PVT.CashReceipt_Confirm(-)');
4750     END IF;
4751 END CashReceipt_Confirm;
4752 
4753 FUNCTION CashReceipt_Unconfirm
4754 ( p_subscription_guid In RAW
4755 , p_event IN OUT NOCOPY WF_EVENT_T
4756 )
4757 RETURN VARCHAR2 IS
4758 
4759 BEGIN
4760 
4761     IF pg_debug = 'Y'
4762     THEN
4763         debug ('AR_BUS_EVENT_SUB_PVT.CashReceipt_Unconfirm(+)');
4764     END IF;
4765   Return 'SUCCESS';
4766     IF pg_debug = 'Y'
4767     THEN
4768         debug ('AR_BUS_EVENT_SUB_PVT.CashReceipt_Unconfirm(-)');
4769     END IF;
4770 END CashReceipt_Unconfirm;
4771 
4772 FUNCTION CashReceipt_DMReversal
4773 ( p_subscription_guid In RAW
4774 , p_event IN OUT NOCOPY WF_EVENT_T
4775 )
4776 RETURN VARCHAR2 IS
4777 
4778 BEGIN
4779 
4780     IF pg_debug = 'Y'
4781     THEN
4782         debug ('AR_BUS_EVENT_SUB_PVT.CashReceipt_DMReversal(+)');
4783     END IF;
4784   Return 'SUCCESS';
4785     IF pg_debug = 'Y'
4786     THEN
4787         debug ('AR_BUS_EVENT_SUB_PVT.CashReceipt_DMReversal(-)');
4788     END IF;
4789 END CashReceipt_DMReversal;
4790 
4791 /* Bug 4173339 */
4792 FUNCTION CashReceipt_Delete
4793 ( p_subscription_guid In RAW
4794 , p_event IN OUT NOCOPY WF_EVENT_T
4795 )
4796 RETURN VARCHAR2
4797 IS
4798 CURSOR hist(ps_id in number) IS
4799 	SELECT *
4800 	  FROM ar_trx_summary_hist
4801 	 WHERE payment_schedule_id = ps_id;
4802 
4803 CURSOR is_this_last_payment (p_customer_id IN NUMBER,
4804                              p_site_use_id IN NUMBER,
4805                              p_currency    IN VARCHAR2,
4806                              p_pmt_number  IN VARCHAR2,
4807                              p_pmt_date    IN DATE,
4808                              p_org_id      IN NUMBER) IS
4809         SELECT 'Y'
4810          FROM  ar_trx_bal_summary
4811         WHERE  cust_account_id = p_customer_id
4812           AND  site_use_id = p_site_use_id
4813           AND  currency = p_currency
4814           AND  last_payment_number = p_pmt_number
4815           AND  last_payment_date = p_pmt_date
4816           AND  NVL(org_id,'-99') = NVL(p_org_id,-99);
4817 
4818 CURSOR get_last_pmt (p_customer_id IN NUMBER,
4819                      p_site_use_id IN NUMBER,
4820                      p_currency    IN VARCHAR2) IS
4821 	SELECT receipt_number, amount, receipt_date
4822 	  FROM ar_cash_receipts
4823 	 WHERE cash_receipt_id =
4824 	        (SELECT MAX(cr.cash_receipt_id)
4825 	         FROM ar_cash_receipts cr,
4826        	              ar_cash_receipt_history crh
4827 	         WHERE cr.pay_from_customer = p_customer_id
4828         	   AND cr.cash_receipt_id = crh.cash_receipt_id
4829         	   AND crh.current_record_flag = 'Y'
4830         	   AND crh.status <> 'REVERSED'
4831         	   AND NVL(cr.customer_site_use_id,-99) = NVL(p_site_use_id, -99)
4832         	   AND cr.currency_code = p_currency
4833 		);
4834 
4835 l_cash_receipt_id  	NUMBER;
4836 l_receipt_date   	DATE ;
4837 l_receipt_amount  	NUMBER;
4838 l_receipt_number  	VARCHAR2(30);
4839 l_customer_id  		NUMBER;
4840 l_customer_site_use_id 	NUMBER;
4841 l_currency_code 	VARCHAR2(30);
4842 is_last_payment        	VARCHAR2(1);
4843 l_last_receipt_number	VARCHAR2(30);
4844 l_last_receipt_amount	NUMBER;
4845 l_last_receipt_date     DATE;
4846 l_payment_schedule_id 	NUMBER;
4847 l_org_id          	NUMBER;
4848 l_user_id         	NUMBER;
4849 l_resp_id         	NUMBER;
4850 l_application_id  	NUMBER;
4851 l_security_gr_id  	NUMBER;
4852 l_deletion_date   	DATE;
4853 
4854 BEGIN
4855 
4856     IF pg_debug = 'Y'
4857     THEN
4858         debug ('AR_BUS_EVENT_SUB_PVT.CashReceipt_Delete(+)');
4859     END IF;
4860    l_payment_schedule_id := p_event.GetValueForParameter('PAYMENT_SCHEDULE_ID');
4861    l_cash_receipt_id     := p_event.GetValueForParameter('CASH_RECEIPT_ID');
4862    l_receipt_number      := p_event.GetValueForParameter('RECEIPT_NUMBER');
4863    l_receipt_date        := p_event.GetValueForParameter('RECEIPT_DATE');
4864    l_deletion_date       := p_event.GetValueForParameter('DELETION_DATE');
4865    -- l_history_id          := p_event.GetValueForParameter('HISTORY_ID');
4866    l_org_id          := p_event.GetValueForParameter('ORG_ID');
4867    l_user_id         := p_event.GetValueForParameter('USER_ID');
4868    l_resp_id         := p_event.GetValueForParameter('RESP_ID');
4869    l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
4870    l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
4871 
4872     IF pg_debug = 'Y'
4873     THEN
4874        debug ('l_payment_schedule_id= '||l_payment_schedule_id);
4875        debug ('l_cash_receipt_id= '||l_cash_receipt_id);
4876        debug ('l_receipt_number= '||l_receipt_number);
4877        debug ('l_receipt_date= '||l_receipt_date);
4878        debug ('l_deletion_date= '||l_deletion_date);
4879        debug ('l_org_id= '||l_org_id);
4880        debug ('l_user_id= '||l_user_id);
4881        debug ('l_resp_id= '||l_resp_id);
4882        debug ('l_application_id= '||l_application_id);
4883        debug ('l_security_gr_id= '||l_security_gr_id);
4884     END IF;
4885    SAVEPOINT  CR_Delete;
4886    --
4887    --set the application context.
4888    --
4892 
4889    fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
4890    mo_global.init('AR');
4891    mo_global.set_policy_context('S',l_org_id);
4893    UPDATE ar_trx_summary_hist
4894       SET complete_flag = 'Y',
4895           LAST_UPDATE_DATE  = sysdate,
4896           LAST_UPDATED_BY   = FND_GLOBAL.user_id,
4897           LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
4898     WHERE payment_schedule_id = l_payment_schedule_id;
4899 
4900    FOR k IN hist(l_payment_schedule_id) LOOP
4901       IF k.customer_id IS NOT NULL THEN
4902 
4903          OPEN is_this_last_payment (k.customer_id,
4904                                     k.site_use_id,
4905                                     k.currency_code,
4906                                     l_receipt_number,
4907                                     l_receipt_date,
4908                                     l_org_id);
4909 
4910          FETCH is_this_last_payment INTO is_last_payment;
4911 
4912          IF NVL(is_last_payment,'N') = 'Y' THEN
4913 
4914             OPEN get_last_pmt (k.customer_id,
4915                           k.site_use_id,
4916                           k.currency_code);
4917             FETCH get_last_pmt INTO
4918                    l_last_receipt_number,
4919                    l_last_receipt_amount,
4920                    l_last_receipt_date;
4921 
4922             CLOSE get_last_pmt;
4923          END IF;
4924 
4925          CLOSE is_this_last_payment;
4926 
4927          --Update ar_trx_bal_summary
4928          UPDATE ar_trx_bal_summary
4929             SET unresolved_cash_value = NVL(unresolved_cash_value,0) -
4930                                                 NVL(k.amount_due_original * -1, 0),
4931                 unresolved_cash_count = NVL(unresolved_cash_count,0) - 1,
4932                 last_payment_amount = NVL(l_last_receipt_amount,last_payment_amount),
4933                 last_payment_date = NVL(l_last_receipt_date,last_payment_date),
4934                 last_payment_number = NVL(l_last_receipt_number,last_payment_number),
4935                 LAST_UPDATE_DATE  = sysdate,
4936                 LAST_UPDATED_BY   = FND_GLOBAL.user_id,
4937                 LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
4938           WHERE cust_account_id = k.customer_id
4939             AND site_use_id =  nvl(k.site_use_id,-99)
4940             AND NVL(org_id,'-99') = NVL(l_org_id,-99)
4941             AND currency = k.currency_code;
4942 
4943 
4944          --Update ar_trx_summary
4945          UPDATE ar_trx_summary
4946            SET  total_cash_receipts_value = nvl(total_cash_receipts_value,0) -
4947                                                 nvl(k.amount_due_original * -1, 0),
4948                 total_cash_receipts_count = nvl(total_cash_receipts_count,0) - 1,
4949                 LAST_UPDATE_DATE  = sysdate,
4950                 LAST_UPDATED_BY   = FND_GLOBAL.user_id,
4951                 LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
4952          WHERE cust_account_id = k.customer_id
4953            AND site_use_id = nvl(k.site_use_id,-99)
4954            AND NVL(org_id,'-99') = NVL(l_org_id,-99)
4955            AND currency =   k.currency_code
4956            AND as_of_date = l_deletion_date;
4957       END IF;
4958    END LOOP;
4959 
4960     IF pg_debug = 'Y'
4961     THEN
4962         debug ('AR_BUS_EVENT_SUB_PVT.CashReceipt_Delete(-)');
4963     END IF;
4964    RETURN 'SUCCESS';
4965 EXCEPTION
4966     WHEN OTHERS  THEN
4967      ROLLBACK TO CR_Delete;
4968 
4969      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
4970      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
4971      FND_MSG_PUB.ADD;
4972 
4973      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'CashReceipt_Delete', p_event.getEventName(), p_subscription_guid);
4974      WF_EVENT.setErrorInfo(p_event, 'ERROR');
4975 
4976      RETURN 'ERROR';
4977 END CashReceipt_Delete;
4978 
4979 FUNCTION CreditMemoApp_Apply
4980 ( p_subscription_guid In RAW
4981 , p_event IN OUT NOCOPY WF_EVENT_T
4982 )
4983 RETURN VARCHAR2 IS
4984 CURSOR get_recapp_details (p_ra_id  IN NUMBER ) IS
4985 SELECT trx_ps.customer_id,
4986        trx_ps.customer_site_use_id,
4987        trx_ps.invoice_currency_code,
4988        trx_ps.class,
4989        ra.amount_applied,
4990        rcpt_ps.customer_id,
4991        rcpt_ps.customer_site_use_id,
4992        rcpt_ps.invoice_currency_code,
4993        nvl(ra.amount_applied_from,ra.amount_applied),
4994        ra.apply_date
4995 FROM  ar_payment_schedules trx_ps,
4996       ar_receivable_applications ra,
4997       ar_payment_schedules rcpt_ps
4998 WHERE ra.receivable_application_id = p_ra_id
4999 and   ra.status in ('APP')
5000 and   ra.payment_schedule_id = rcpt_ps.payment_schedule_id
5001 and   ra.applied_payment_schedule_id = trx_ps.payment_schedule_id
5002 ;
5003   l_org_id          NUMBER;
5004   l_user_id         NUMBER;
5005   l_resp_id         NUMBER;
5006   l_application_id  NUMBER;
5007   l_security_gr_id  NUMBER;
5008   l_receivable_application_id  NUMBER;
5009   l_trx_customer_id     NUMBER;
5010   l_trx_site_use_id     NUMBER;
5011   l_trx_currency_code   VARCHAR2(30);
5012   l_trx_amt             NUMBER;
5013   l_trx_due_date        DATE;
5014   l_cm_customer_id      NUMBER;
5015   l_cm_site_use_id      NUMBER;
5016   l_cm_currency_code    VARCHAR2(30);
5017   l_cm_amt              NUMBER;
5018   l_apply_date          DATE;
5019   l_op_trx_count        NUMBER;
5020   l_trx_ado             NUMBER;
5021   l_trx_app_amt         NUMBER;
5022   l_trx_class           VARCHAR2(10);
5023   l_trx_ps_status       VARCHAR2(10);
5024   l_cm_ps_status        VARCHAR2(10);
5025   l_due_date_str        VARCHAR2(30);
5026   l_op_cm_count         NUMBER;
5027   l_past_due_inv_value       NUMBER;
5028   l_past_due_inv_inst_count  NUMBER;
5029 
5033     IF pg_debug = 'Y'
5030 BEGIN
5031   SAVEPOINT  CM_Apply_Event;
5032 
5034     THEN
5035         debug ('AR_BUS_EVENT_SUB_PVT.CreditMemoApp_Apply(+)');
5036     END IF;
5037   l_receivable_application_id :=
5038                   p_event.GetValueForParameter('RECEIVABLE_APPLICATION_ID');
5039   l_trx_ps_status      := p_event.GetValueForParameter('TRX_PS_STATUS');
5040   l_cm_ps_status  := p_event.GetValueForParameter('CM_PS_STATUS');
5041   l_due_date_str    := p_event.GetValueForParameter('TRX_DUE_DATE');
5042   l_trx_app_amt     := fnd_number.canonical_to_number(p_event.GetValueForParameter('TRX_APP_AMT'));
5043   l_org_id          := p_event.GetValueForParameter('ORG_ID');
5044   l_user_id         := p_event.GetValueForParameter('USER_ID');
5045   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
5046   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
5047   l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
5048 
5049     IF pg_debug = 'Y'
5050     THEN
5051        debug ('l_receivable_application_id= '||l_receivable_application_id);
5052        debug ('l_trx_ps_status= '||l_trx_ps_status);
5053        debug ('l_cm_ps_status= '||l_cm_ps_status);
5054        debug ('l_due_date_str= '||l_due_date_str);
5055        debug ('l_trx_app_amt= '||l_trx_app_amt);
5056        debug ('l_org_id= '||l_org_id);
5057        debug ('l_user_id= '||l_user_id);
5058        debug ('l_resp_id= '||l_resp_id);
5059        debug ('l_application_id= '||l_application_id);
5060        debug ('l_security_gr_id= '||l_security_gr_id);
5061     END IF;
5062    --
5063    --set the application context.
5064    --
5065    fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
5066    mo_global.init('AR');
5067    mo_global.set_policy_context('S',l_org_id);
5068 
5069    select to_date(l_due_date_str, 'J')
5070    into  l_trx_due_date
5071    from dual;
5072 
5073    OPEN get_recapp_details(l_receivable_application_id);
5074 
5075      FETCH get_recapp_details INTO  l_trx_customer_id,
5076                                     l_trx_site_use_id,
5077                                     l_trx_currency_code,
5078                                     l_trx_class,
5079                                     l_trx_amt ,
5080                                     l_cm_customer_id ,
5081                                     l_cm_site_use_id ,
5082                                     l_cm_currency_code,
5083                                     l_cm_amt,
5084                                     l_apply_date
5085                                     ;
5086 	IF get_recapp_details%NOTFOUND THEN
5087 	 CLOSE get_recapp_details;
5088          Return 'SUCCESS';
5089         END IF;
5090 
5091       IF  l_cm_ps_status = 'CL' THEN
5092         l_op_cm_count := 1;
5093       END IF;
5094 
5095       IF l_trx_ps_status = 'CL' THEN
5096        l_op_trx_count := 1;
5097       END IF;
5098 
5099       IF l_trx_class = 'INV' THEN
5100 
5101         IF  l_trx_due_date < sysdate THEN
5102 
5103              l_past_due_inv_value := l_trx_app_amt;
5104              IF l_trx_ps_status = 'CL' THEN
5105                l_past_due_inv_inst_count := 1;
5106              END IF;
5107         END IF;
5108 
5109       END IF;
5110 
5111       /*****************************************************************
5112         Columns that need to be updated in the summary tables due to a
5113         credit memo application
5114         AR_TRX_SUMMARY
5115         ==================
5116 
5117         AR_TRX_BAL_SUMMARY
5118         ==================
5119         1) OP_INVOICES_VALUE
5120         2) OP_INVOICES_COUNT
5121         3) OP_DEBIT_MEMOS_VALUE
5122         4) OP_DEBIT_MEMOS_COUNT
5123         5) OP_DEPOSITS_VALUE
5124         6) OP_DEPOSITS_COUNT
5125         7) OP_CHARGEBACK_VALUE
5126         8) OP_CHARGEBACK_COUNT
5127         9) OP_CREDIT_MEMOS_VALUE
5128         10)OP_CREDIT_MEMOS_COUNT
5129         11)PAST_DUE_INV_VALUE
5130         12)PAST_DUE_INV_INST_COUNT
5131 
5132         *****************************************************************/
5133 
5134         Update_recapp_info(l_trx_class,
5135                            l_trx_customer_id,
5136                            l_trx_site_use_id,
5137                            l_trx_currency_code,
5138                            l_trx_amt,
5139                            l_op_trx_count,
5140                            l_cm_customer_id ,
5141                            l_cm_site_use_id ,
5142                            l_cm_currency_code,
5143                            l_cm_amt,
5144                            l_apply_date ,
5145                            null, --l_edisc_value,
5146                            null, --l_edisc_count,
5147                            null, --l_uedisc_value,
5148                            null, --l_uedisc_count,
5149                            null, --l_inv_paid_amt,
5150                            null, --l_inv_inst_pmt_days_sum,
5151                            null, --l_sum_app_amt_days_late,
5152                            null, --l_sum_app_amt,
5153                            null, --l_count_of_tot_inv_inst_paid,
5154                            null, --l_count_of_inv_inst_paid_late,
5155                            null, --l_count_of_disc_inv_inst,
5156                            null, --l_unresolved_cash_value,
5157                            null, --l_unresolved_cash_count,
5158                            l_op_cm_count,
5159                            'CM',
5160                            l_past_due_inv_value,
5161                            l_past_due_inv_inst_count,
5162                            l_org_id
5163                            );
5164 
5165      --
5166    CLOSE get_recapp_details;
5167     IF pg_debug = 'Y'
5168     THEN
5172 EXCEPTION
5169         debug ('AR_BUS_EVENT_SUB_PVT.CreditMemoApp_Apply(-)');
5170     END IF;
5171   Return 'SUCCESS';
5173     WHEN OTHERS  THEN
5174      ROLLBACK TO CM_Apply_Event;
5175 
5176      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
5177      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
5178      FND_MSG_PUB.ADD;
5179 
5180      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'CREDITMEMOAPP_APPLY', p_event.getEventName(), p_subscription_guid);
5181      WF_EVENT.setErrorInfo(p_event, 'ERROR');
5182 
5183      RETURN 'ERROR';
5184 
5185 END CreditMemoApp_Apply;
5186 
5187 
5188 FUNCTION CreditMemoApp_UnApply
5189 ( p_subscription_guid In RAW
5190 , p_event IN OUT NOCOPY WF_EVENT_T
5191 )
5192 RETURN VARCHAR2 IS
5193 
5194 CURSOR get_recapp_details (p_ra_id  IN NUMBER ) IS
5195 SELECT trx_ps.customer_id,
5196        trx_ps.customer_site_use_id,
5197        trx_ps.invoice_currency_code,
5198        trx_ps.class,
5199        ra.amount_applied,
5200        rcpt_ps.customer_id,
5201        rcpt_ps.customer_site_use_id,
5202        rcpt_ps.invoice_currency_code,
5203        nvl(ra.amount_applied_from,ra.amount_applied),
5204        ra.apply_date
5205 FROM  ar_payment_schedules trx_ps,
5206       ar_receivable_applications ra,
5207       ar_payment_schedules rcpt_ps
5208 WHERE ra.receivable_application_id = p_ra_id
5209 and   ra.status in ('APP')
5210 and   ra.payment_schedule_id = rcpt_ps.payment_schedule_id
5211 and   ra.applied_payment_schedule_id = trx_ps.payment_schedule_id
5212 ;
5213   l_org_id          NUMBER;
5214   l_user_id         NUMBER;
5215   l_resp_id         NUMBER;
5216   l_application_id  NUMBER;
5217   l_security_gr_id  NUMBER;
5218   l_receivable_application_id  NUMBER;
5219   l_trx_customer_id     NUMBER;
5220   l_trx_site_use_id     NUMBER;
5221   l_trx_currency_code   VARCHAR2(30);
5222   l_trx_amt             NUMBER;
5223   l_trx_due_date        DATE;
5224   l_cm_customer_id      NUMBER;
5225   l_cm_site_use_id      NUMBER;
5226   l_cm_currency_code    VARCHAR2(30);
5227   l_cm_amt              NUMBER;
5228   l_apply_date          DATE;
5229   l_op_trx_count        NUMBER;
5230   l_trx_ado             NUMBER;
5231   l_trx_app_amt         NUMBER;
5232   l_trx_class           VARCHAR2(10);
5233   l_trx_ps_status       VARCHAR2(10);
5234   l_cm_ps_status        VARCHAR2(10);
5235   l_due_date_str        VARCHAR2(30);
5236   l_op_cm_count         NUMBER;
5237   l_past_due_inv_value       NUMBER;
5238   l_past_due_inv_inst_count  NUMBER;
5239 
5240 BEGIN
5241     IF pg_debug = 'Y'
5242     THEN
5243         debug ('AR_BUS_EVENT_SUB_PVT.CreditMemoApp_UnApply(+)');
5244     END IF;
5245   l_receivable_application_id :=
5246                   p_event.GetValueForParameter('RECEIVABLE_APPLICATION_ID');
5247   l_trx_ps_status      := p_event.GetValueForParameter('TRX_PS_STATUS');
5248   l_cm_ps_status  := p_event.GetValueForParameter('CM_PS_STATUS');
5249   l_due_date_str    := p_event.GetValueForParameter('TRX_DUE_DATE');
5250   l_trx_app_amt     := fnd_number.canonical_to_number(p_event.GetValueForParameter('TRX_APP_AMT'));
5251   l_org_id          := p_event.GetValueForParameter('ORG_ID');
5252   l_user_id         := p_event.GetValueForParameter('USER_ID');
5253   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
5254   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
5255   l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
5256     IF pg_debug = 'Y'
5257     THEN
5258        debug ('l_receivable_application_id= '||l_receivable_application_id);
5259        debug ('l_trx_ps_status= '||l_trx_ps_status);
5260        debug ('l_cm_ps_status= '||l_cm_ps_status);
5261        debug ('l_due_date_str= '||l_due_date_str);
5262        debug ('l_trx_app_amt= '||l_trx_app_amt);
5263        debug ('l_org_id= '||l_org_id);
5264        debug ('l_user_id= '||l_user_id);
5265        debug ('l_resp_id= '||l_resp_id);
5266        debug ('l_application_id= '||l_application_id);
5267        debug ('l_security_gr_id= '||l_security_gr_id);
5268     END IF;
5269 
5270    SAVEPOINT  CM_UnApply_Event;
5271    --
5272    --set the application context.
5273    --
5274    fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
5275    mo_global.init('AR');
5276    mo_global.set_policy_context('S',l_org_id);
5277 
5278    select to_date(l_due_date_str, 'J')
5279    into  l_trx_due_date
5280    from dual;
5281 
5282    OPEN get_recapp_details(l_receivable_application_id);
5283 
5284      FETCH get_recapp_details INTO  l_trx_customer_id,
5285                                     l_trx_site_use_id,
5286                                     l_trx_currency_code,
5287                                     l_trx_class,
5288                                     l_trx_amt ,
5289                                     l_cm_customer_id ,
5290                                     l_cm_site_use_id ,
5291                                     l_cm_currency_code,
5292                                     l_cm_amt,
5293                                     l_apply_date
5294                                     ;
5295 	IF get_recapp_details%NOTFOUND THEN
5296          CLOSE get_recapp_details;
5297          Return 'SUCCESS';
5298         END IF;
5299       IF  l_cm_ps_status = 'CL' THEN
5300         l_op_cm_count := -1;
5301       END IF;
5302 
5303       IF l_trx_ps_status = 'CL' THEN
5304        l_op_trx_count := -1;
5305       END IF;
5306 
5307       IF l_trx_class = 'INV' THEN
5308 
5309         IF  l_trx_due_date < sysdate THEN
5310 
5311              l_past_due_inv_value := l_trx_app_amt;
5312              IF l_trx_ps_status = 'CL' THEN
5313                l_past_due_inv_inst_count := -1;
5314              END IF;
5318 
5315         END IF;
5316 
5317       END IF;
5319       /*****************************************************************
5320         Columns that need to be updated in the summary tables due to a
5321         credit memo application
5322         AR_TRX_SUMMARY
5323         ==================
5324 
5325         AR_TRX_BAL_SUMMARY
5326         ==================
5327         1) OP_INVOICES_VALUE
5328         2) OP_INVOICES_COUNT
5329         3) OP_DEBIT_MEMOS_VALUE
5330         4) OP_DEBIT_MEMOS_COUNT
5331         5) OP_DEPOSITS_VALUE
5332         6) OP_DEPOSITS_COUNT
5333         7) OP_CHARGEBACK_VALUE
5334         8) OP_CHARGEBACK_COUNT
5335         9) OP_CREDIT_MEMOS_VALUE
5336         10)OP_CREDIT_MEMOS_COUNT
5337         11)PAST_DUE_INV_VALUE
5338         12)PAST_DUE_INV_INST_COUNT
5339 
5340         *****************************************************************/
5341 
5342         Update_recapp_info(l_trx_class,
5343                            l_trx_customer_id,
5344                            l_trx_site_use_id,
5345                            l_trx_currency_code,
5346                            l_trx_amt,
5347                            l_op_trx_count,
5348                            l_cm_customer_id ,
5349                            l_cm_site_use_id ,
5350                            l_cm_currency_code,
5351                            l_cm_amt,
5352                            l_apply_date ,
5353                            null, --l_edisc_value,
5354                            null, --l_edisc_count,
5355                            null, --l_uedisc_value,
5356                            null, --l_uedisc_count,
5357                            null, --l_inv_paid_amt,
5358                            null, --l_inv_inst_pmt_days_sum,
5359                            null, --l_sum_app_amt_days_late,
5360                            null, --l_sum_app_amt,
5361                            null, --l_count_of_tot_inv_inst_paid,
5362                            null, --l_count_of_inv_inst_paid_late,
5363                            null, --l_count_of_disc_inv_inst,
5364                            null, --l_unresolved_cash_value,
5365                            null, --l_unresolved_cash_count,
5366                            l_op_cm_count,
5367                            'CM',
5368                            l_past_due_inv_value,
5369                            l_past_due_inv_inst_count,
5370                            l_org_id
5371                            );
5372 
5373      --
5374    CLOSE get_recapp_details;
5375     IF pg_debug = 'Y'
5376     THEN
5377         debug ('AR_BUS_EVENT_SUB_PVT.CreditMemoApp_UnApply(-)');
5378     END IF;
5379   Return 'SUCCESS';
5380 EXCEPTION
5381     WHEN OTHERS  THEN
5382      ROLLBACK TO CM_UnApply_Event;
5383 
5384      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
5385      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
5386      FND_MSG_PUB.ADD;
5387 
5388      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'CREDITMEMOAPP_UNAPPLY', p_event.getEventName(), p_subscription_guid);
5389      WF_EVENT.setErrorInfo(p_event, 'ERROR');
5390 
5391      RETURN 'ERROR';
5392 
5393 END CreditMemoApp_UnApply;
5394 
5395 FUNCTION CashApp_Apply
5396 ( p_subscription_guid In RAW
5397 , p_event IN OUT NOCOPY WF_EVENT_T
5398 )
5399 RETURN VARCHAR2 IS
5400 
5401   l_org_id          NUMBER;
5402   l_user_id         NUMBER;
5403   l_resp_id         NUMBER;
5404   l_application_id  NUMBER;
5405   l_security_gr_id  NUMBER;
5406 
5407 CURSOR get_recapp_details (ra_id  IN NUMBER ) IS
5408 SELECT trx_ps.customer_id,
5409        trx_ps.customer_site_use_id,
5410        trx_ps.invoice_currency_code,
5411        trx_ps.class,
5412        DECODE(trx_ps.class,'INV',
5413           DECODE((nvl(trx_ps.discount_taken_earned,0)
5414               + nvl(trx_ps.discount_taken_unearned,0)),0,0,1),0) disc_inv_inst_count,
5415        rt.printing_lead_days,
5416        ra.amount_applied,
5417        rcpt_ps.customer_id,
5418        rcpt_ps.customer_site_use_id,
5419        rcpt_ps.invoice_currency_code,
5420        nvl(ra.amount_applied_from,ra.amount_applied),
5421        ra.apply_date,
5422        ra.earned_discount_taken,
5423        ra.unearned_discount_taken,
5424        decode(sign(nvl(ra.earned_discount_taken,0)),-1,-1,0,0,1) count_of_edisc,
5425        decode(sign(nvl(ra.unearned_discount_taken,0)),-1,-1,0,0,1) count_of_uedisc
5426 FROM  ar_payment_schedules trx_ps,
5427       ar_receivable_applications ra,
5428       ar_payment_schedules rcpt_ps,
5429       ra_terms_b rt
5430 WHERE ra.receivable_application_id = ra_id
5431 and   ra.status in ('APP','ACTIVITY')
5432 and   ra.payment_schedule_id = rcpt_ps.payment_schedule_id
5433 and   ra.applied_payment_schedule_id = trx_ps.payment_schedule_id
5434 and   trx_ps.term_id =  rt.term_id(+);
5435 
5436 l_receivable_application_id  NUMBER;
5437 l_disc_inv_inst_count NUMBER;
5438 l_trx_customer_id     NUMBER;
5439 l_trx_site_use_id     NUMBER;
5440 l_trx_currency_code   VARCHAR2(30);
5441 l_trx_amt             NUMBER;
5442 l_trx_due_date        DATE;
5443 l_rcpt_customer_id    NUMBER;
5444 l_rcpt_site_use_id    NUMBER;
5445 l_rcpt_currency_code  VARCHAR2(30);
5446 l_rcpt_amt            NUMBER;
5447 l_apply_date          DATE;
5448 l_edisc_count         NUMBER;
5449 l_edisc_value         NUMBER;
5450 l_uedisc_count       NUMBER;
5451 l_uedisc_value       NUMBER;
5452 l_op_trx_count        NUMBER;
5453 l_inv_paid_amt         NUMBER;
5454 l_inv_inst_pmt_days_sum NUMBER;
5455 l_sum_app_amt_days_late NUMBER;
5456 l_sum_app_amt           NUMBER;
5457 l_count_of_tot_inv_inst_paid NUMBER;
5458 l_count_of_inv_inst_paid_late NUMBER;
5459 l_count_of_disc_inv_inst  NUMBER;
5460 l_trx_ado                 NUMBER;
5461 l_trx_app_amt             NUMBER;
5465 l_rcpt_ps_status          VARCHAR2(10);
5462 l_printing_lead_days      NUMBER;
5463 l_trx_class               VARCHAR2(10);
5464 l_trx_ps_status           VARCHAR2(10);
5466 l_due_date_str            VARCHAR2(30);
5467 l_unresolved_cash_value   NUMBER;
5468 l_unresolved_cash_count   NUMBER;
5469 
5470 l_past_due_inv_value    NUMBER;
5471 l_past_due_inv_inst_count NUMBER;
5472 BEGIN
5473   SAVEPOINT  CashApp_Apply_pvt;
5474 
5475     IF pg_debug = 'Y'
5476     THEN
5477         debug ('AR_BUS_EVENT_SUB_PVT.CashApp_Apply(+)');
5478     END IF;
5479   l_receivable_application_id :=
5480                   p_event.GetValueForParameter('RECEIVABLE_APPLICATION_ID');
5481   l_trx_ps_status      := p_event.GetValueForParameter('TRX_PS_STATUS');
5482   l_rcpt_ps_status  := p_event.GetValueForParameter('RCPT_PS_STATUS');
5483   l_due_date_str    := p_event.GetValueForParameter('TRX_DUE_DATE');
5484   l_trx_app_amt     := fnd_number.canonical_to_number(p_event.GetValueForParameter('TRX_APP_AMT'));
5485   l_org_id          := p_event.GetValueForParameter('ORG_ID');
5486   l_user_id         := p_event.GetValueForParameter('USER_ID');
5487   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
5488   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
5489   l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
5490 
5491     IF pg_debug = 'Y'
5492     THEN
5493        debug ('l_receivable_application_id= '||l_receivable_application_id);
5494        debug ('l_trx_ps_status= '||l_trx_ps_status);
5495        debug ('l_rcpt_ps_status= '||l_rcpt_ps_status);
5496        debug ('l_due_date_str= '||l_due_date_str);
5497        debug ('l_trx_app_amt= '||l_trx_app_amt);
5498        debug ('l_org_id= '||l_org_id);
5499        debug ('l_user_id= '||l_user_id);
5500        debug ('l_resp_id= '||l_resp_id);
5501        debug ('l_application_id= '||l_application_id);
5502        debug ('l_security_gr_id= '||l_security_gr_id);
5503     END IF;
5504    --
5505    --set the application context.
5506    --
5507   fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
5508   mo_global.init('AR');
5509   mo_global.set_policy_context('S',l_org_id);
5510 
5511    select to_date(l_due_date_str, 'J')
5512    into  l_trx_due_date
5513    from dual;
5514 
5515    OPEN get_recapp_details(l_receivable_application_id);
5516 
5517      FETCH get_recapp_details INTO  l_trx_customer_id,
5518                                     l_trx_site_use_id,
5519                                     l_trx_currency_code,
5520                                     l_trx_class,
5521                                     l_disc_inv_inst_count,
5522                                     l_printing_lead_days,
5523                                     l_trx_amt          ,
5524                                     l_rcpt_customer_id ,
5525                                     l_rcpt_site_use_id ,
5526                                     l_rcpt_currency_code,
5527                                     l_rcpt_amt          ,
5528                                     l_apply_date     ,
5529                                     l_edisc_value,
5530                                     l_uedisc_value,
5531                                     l_edisc_count,
5532                                     l_uedisc_count;
5533 
5534 	IF get_recapp_details%NOTFOUND THEN
5535           CLOSE get_recapp_details;
5536           Return 'SUCCESS';
5537         END IF;
5538 
5539         l_trx_amt := l_trx_amt + nvl(l_edisc_value,0) +  nvl(l_uedisc_value,0);
5540       --populating the remaining variables
5541         l_unresolved_cash_value := l_rcpt_amt;
5542 
5543       IF  l_rcpt_ps_status = 'CL' THEN
5544         l_unresolved_cash_count := 1;
5545       END IF;
5546 
5547       IF l_trx_ps_status = 'CL' THEN
5548        l_op_trx_count := 1;
5549       END IF;
5550 
5551       IF l_trx_class = 'INV' THEN
5552 
5553          l_sum_app_amt := l_trx_amt;
5554 
5555          l_inv_inst_pmt_days_sum :=
5556               (l_apply_date - (l_trx_due_date + nvl(l_printing_lead_days,0)))
5557                      * l_trx_amt;
5558 
5559         IF l_trx_ps_status = 'CL' THEN
5560             l_inv_paid_amt := l_trx_app_amt;
5561             l_count_of_tot_inv_inst_paid := 1;
5562             l_count_of_disc_inv_inst := l_disc_inv_inst_count;
5563         END IF;
5564 
5565         IF  l_trx_ps_status = 'CL' and
5566             l_apply_date > nvl(l_trx_due_date,l_apply_date) THEN
5567             l_count_of_inv_inst_paid_late := 1;
5568         END IF;
5569 
5570         IF  l_trx_due_date < sysdate THEN
5571 
5572              l_past_due_inv_value := l_trx_app_amt;
5573              IF l_trx_ps_status = 'CL' THEN
5574                l_past_due_inv_inst_count := 1;
5575              END IF;
5576         END IF;
5577 
5578         IF l_apply_date > nvl(l_trx_due_date,l_apply_date) THEN
5579            l_sum_app_amt_days_late :=
5580                         (l_apply_date - nvl(l_trx_due_date ,l_apply_date))
5581                                * l_trx_amt;
5582         END IF;
5583 
5584       END IF;
5585 
5586       /*****************************************************************
5587         Columns that need to be updated in the summary tables due to a
5588         receipt application
5589         AR_TRX_SUMMARY
5590         ==================
5591         1) INV_PAID_AMOUNT
5592         2) INV_INST_PMT_DAYS_SUM
5593         3) TOTAL_EARNED_DISC_VALUE
5594         4) TOTAL_EARNED_DISC_COUNT
5595         5) TOTAL_UNEARNED_DISC_VALUE
5596         6) TOTAL_UNEARNED_DISC_COUNT
5597         7) SUM_APP_AMT_DAYS_LATE
5598         8) SUM_APP_AMT
5599         9) COUNT_OF_TOT_INV_INST_PAID
5600         10)COUNT_OF_INV_INST_PAID_LATE
5601         11)COUNT_OF_DISC_INV_INST
5602         12)DAYS_CREDIT_GRANTED_SUM
5603 
5607         2) OP_INVOICES_COUNT
5604         AR_TRX_BAL_SUMMARY
5605         ==================
5606         1) OP_INVOICES_VALUE
5608         3) OP_DEBIT_MEMOS_VALUE
5609         4) OP_DEBIT_MEMOS_COUNT
5610         5) OP_DEPOSITS_VALUE
5611         6) OP_DEPOSITS_COUNT
5612         7) OP_CHARGEBACK_VALUE
5613         8) OP_CHARGEBACK_COUNT
5614         9) OP_CREDIT_MEMOS_VALUE
5615         10)OP_CREDIT_MEMOS_COUNT
5616         11)UNRESOLVED_CASH_VALUE  l_rcp_cash_amt
5617         12)UNRESOLVED_CASH_COUNT
5618         13)PAST_DUE_INV_VALUE
5619         14)PAST_DUE_INV_INST_COUNT
5620 
5621         *****************************************************************/
5622 
5623         Update_recapp_info(l_trx_class,
5624                            l_trx_customer_id,
5625                            l_trx_site_use_id,
5626                            l_trx_currency_code,
5627                            l_trx_amt          ,
5628                            l_op_trx_count,
5629                            l_rcpt_customer_id ,
5630                            l_rcpt_site_use_id ,
5631                            l_rcpt_currency_code,
5632                            l_rcpt_amt          ,
5633                            l_apply_date ,
5634                            l_edisc_value,
5635                            l_edisc_count,
5636                            l_uedisc_value,
5637                            l_uedisc_count,
5638                            l_inv_paid_amt,
5639                            l_inv_inst_pmt_days_sum,
5640                            l_sum_app_amt_days_late,
5641                            l_sum_app_amt,
5642                            l_count_of_tot_inv_inst_paid,
5643                            l_count_of_inv_inst_paid_late,
5644                            l_count_of_disc_inv_inst,
5645                            l_unresolved_cash_value,
5646                            l_unresolved_cash_count,
5647                            null,
5648                            'CASH',
5649                            l_past_due_inv_value,
5650                            l_past_due_inv_inst_count,
5651                            l_org_id
5652                            );
5653 
5654      --
5655    CLOSE get_recapp_details;
5656 
5657     IF pg_debug = 'Y'
5658     THEN
5659         debug ('AR_BUS_EVENT_SUB_PVT.CashApp_Apply(-)');
5660     END IF;
5661   Return 'SUCCESS';
5662 EXCEPTION
5663     WHEN OTHERS  THEN
5664      ROLLBACK TO CashApp_Apply_pvt;
5665 
5666      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
5667      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
5668      FND_MSG_PUB.ADD;
5669 
5670      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'CashApp_Apply', p_event.getEventName(), p_subscription_guid);
5671      WF_EVENT.setErrorInfo(p_event, 'ERROR');
5672 
5673      RETURN 'ERROR';
5674 
5675 END CashApp_Apply;
5676 
5677 FUNCTION CashApp_UnApply
5678 ( p_subscription_guid In RAW
5679 , p_event IN OUT NOCOPY WF_EVENT_T
5680 )
5681 RETURN VARCHAR2 IS
5682 
5683   l_org_id          NUMBER;
5684   l_user_id         NUMBER;
5685   l_resp_id         NUMBER;
5686   l_application_id  NUMBER;
5687   l_security_gr_id  NUMBER;
5688 
5689 CURSOR get_recapp_details (ra_id  IN NUMBER ) IS
5690 SELECT trx_ps.customer_id,
5691        trx_ps.customer_site_use_id,
5692        trx_ps.invoice_currency_code,
5693        trx_ps.class,
5694        DECODE(trx_ps.class,'INV',
5695         DECODE((nvl(trx_ps.discount_taken_earned,0)
5696           + nvl(trx_ps.discount_taken_unearned,0)),0,0,1),0) disc_inv_inst_count,
5697        rt.printing_lead_days,
5698        ra.amount_applied,
5699        rcpt_ps.customer_id,
5700        rcpt_ps.customer_site_use_id,
5701        rcpt_ps.invoice_currency_code,
5702        nvl(ra.amount_applied_from,ra.amount_applied),
5703        ra.apply_date,
5704        ra.earned_discount_taken,
5705        ra.unearned_discount_taken,
5706        decode(sign(nvl(ra.earned_discount_taken,0)),-1,-1,0,0,1) count_of_edisc,
5707        decode(sign(nvl(ra.unearned_discount_taken,0)),-1,-1,0,0,1) count_of_uedisc
5708 FROM  ar_payment_schedules trx_ps,
5709       ar_receivable_applications ra,
5710       ar_payment_schedules rcpt_ps,
5711       ra_terms_b rt
5712 WHERE ra.receivable_application_id = ra_id
5713 and   ra.status in ('APP','ACTIVITY')
5714 and   ra.payment_schedule_id = rcpt_ps.payment_schedule_id
5715 and   ra.applied_payment_schedule_id = trx_ps.payment_schedule_id
5716 and   trx_ps.term_id =  rt.term_id(+);
5717 
5718 /* bug number :4387571
5719    Modified cursor get_inv_disc_info for receivable_application_id condition */
5720 Cursor get_inv_disc_info(p_rec_app_id IN NUMBER) IS
5721 select sum(  nvl(ra.earned_discount_taken,0)
5722            + nvl(ra.unearned_discount_taken,0)
5723           ) total_disc
5724 from ar_receivable_applications ra
5725 where receivable_application_id = p_rec_app_id
5726 and  status = 'APP'
5727 and  display = 'Y';
5728 
5729 l_receivable_application_id  NUMBER;
5730 l_disc_inv_inst_count NUMBER;
5731 l_trx_customer_id     NUMBER;
5732 l_trx_site_use_id     NUMBER;
5733 l_trx_currency_code   VARCHAR2(30);
5734 l_trx_amt             NUMBER;
5735 l_trx_due_date        DATE;
5736 l_rcpt_customer_id    NUMBER;
5737 l_rcpt_site_use_id    NUMBER;
5738 l_rcpt_currency_code  VARCHAR2(30);
5739 l_rcpt_amt            NUMBER;
5740 l_apply_date          DATE;
5741 l_edisc_count         NUMBER;
5742 l_edisc_value         NUMBER;
5743 l_unedisc_count       NUMBER;
5744 l_unedisc_value       NUMBER;
5745 l_op_trx_count        NUMBER;
5746 l_inv_paid_amt         NUMBER;
5747 l_inv_inst_pmt_days_sum NUMBER;
5748 l_sum_app_amt_days_late NUMBER;
5749 l_sum_app_amt           NUMBER;
5750 l_count_of_tot_inv_inst_paid NUMBER;
5754 l_trx_app_amt             NUMBER;
5751 l_count_of_inv_inst_paid_late NUMBER;
5752 l_count_of_disc_inv_inst  NUMBER;
5753 l_trx_ado                 NUMBER;
5755 l_printing_lead_days      NUMBER;
5756 l_trx_class               VARCHAR2(10);
5757 l_old_trx_ps_status       VARCHAR2(10);
5758 l_old_rcpt_ps_status      VARCHAR2(10);
5759 l_new_trx_ps_status       VARCHAR2(10);
5760 l_new_rcpt_ps_status      VARCHAR2(10);
5761 l_due_date_str            VARCHAR2(30);
5762 l_unresolved_cash_value   NUMBER;
5763 l_unresolved_cash_count   NUMBER;
5764 l_past_due_inv_value      NUMBER;
5765 l_past_due_inv_inst_count NUMBER;
5766 l_prior_disc_amt          NUMBER;
5767 BEGIN
5768      SAVEPOINT  CashApp_UnApply_pvt;
5769     IF pg_debug = 'Y'
5770     THEN
5771         debug ('AR_BUS_EVENT_SUB_PVT.CashApp_UnApply(+)');
5772     END IF;
5773   l_receivable_application_id :=
5774                   p_event.GetValueForParameter('RECEIVABLE_APPLICATION_ID');
5775   l_old_trx_ps_status      := p_event.GetValueForParameter('OLD_TRX_PS_STATUS');
5776   l_new_trx_ps_status      := p_event.GetValueForParameter('NEW_TRX_PS_STATUS');
5777   l_old_rcpt_ps_status  := p_event.GetValueForParameter('OLD_RCPT_PS_STATUS');
5778   l_new_rcpt_ps_status  := p_event.GetValueForParameter('NEW_RCPT_PS_STATUS');
5779   l_due_date_str        := p_event.GetValueForParameter('TRX_DUE_DATE');
5780   l_trx_app_amt         := fnd_number.canonical_to_number(p_event.GetValueForParameter('TRX_APP_AMT'));
5781  -- l_trx_ps_adr          := p_event.GetValueForParameter('AMT_DUE_REMAINING');
5782  -- l_trx_ps_old_adr      := p_event.GetValueForParameter('OLD_AMT_DUE_REMAINING');
5783   l_org_id          := p_event.GetValueForParameter('ORG_ID');
5784   l_user_id         := p_event.GetValueForParameter('USER_ID');
5785   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
5786   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
5787   l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
5788     IF pg_debug = 'Y'
5789     THEN
5790        debug ('l_receivable_application_id= '||l_receivable_application_id);
5791        debug ('l_new_trx_ps_status= '||l_new_trx_ps_status);
5792        debug ('l_old_rcpt_ps_status= '||l_old_rcpt_ps_status);
5793        debug ('l_old_rcpt_ps_status= '||l_old_rcpt_ps_status);
5794        debug ('l_new_rcpt_ps_status= '||l_new_rcpt_ps_status);
5795        debug ('l_due_date_str= '||l_due_date_str);
5796        debug ('l_trx_app_amt= '||l_trx_app_amt);
5797        debug ('l_org_id= '||l_org_id);
5798        debug ('l_user_id= '||l_user_id);
5799        debug ('l_resp_id= '||l_resp_id);
5800        debug ('l_application_id= '||l_application_id);
5801        debug ('l_security_gr_id= '||l_security_gr_id);
5802     END IF;
5803    --
5804    --set the application context.
5805    --
5806   fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
5807   mo_global.init('AR');
5808   mo_global.set_policy_context('S',l_org_id);
5809 
5810    select to_date(l_due_date_str, 'J')
5811    into  l_trx_due_date
5812    from dual;
5813 
5814    OPEN get_recapp_details(l_receivable_application_id);
5815 
5816      FETCH get_recapp_details INTO  l_trx_customer_id,
5817                                     l_trx_site_use_id,
5818                                     l_trx_currency_code,
5819                                     l_trx_class,
5820                                     l_disc_inv_inst_count,
5821                                     l_printing_lead_days,
5822                                     l_trx_amt          ,
5823                                     l_rcpt_customer_id ,
5824                                     l_rcpt_site_use_id ,
5825                                     l_rcpt_currency_code,
5826                                     l_rcpt_amt          ,
5827                                     l_apply_date     ,
5828                                     l_edisc_value,
5829                                     l_unedisc_value,
5830                                     l_edisc_count,
5831                                     l_unedisc_count;
5832 
5833 	IF get_recapp_details%NOTFOUND THEN
5834          CLOSE get_recapp_details;
5835          Return 'SUCCESS';
5836         END IF;
5837 
5838         l_trx_amt := l_trx_amt + nvl(l_edisc_value,0) +  nvl(l_unedisc_value,0);
5839       --populating the remaining variables
5840         l_unresolved_cash_value := l_rcpt_amt;
5841 
5842       IF  l_old_rcpt_ps_status = 'CL'  AND
5843           l_new_rcpt_ps_status = 'OP' THEN
5844         l_unresolved_cash_count := -1;
5845       END IF;
5846 
5847 
5848       IF l_new_trx_ps_status = 'CL' AND
5849          l_old_trx_ps_status = 'OP' THEN
5850        l_op_trx_count := 1;
5851       ELSIF l_new_trx_ps_status = 'OP' AND
5852          l_old_trx_ps_status = 'CL' THEN
5853        l_op_trx_count := -1;
5854       END IF;
5855 
5856 
5857 
5858 
5859       IF l_trx_class = 'INV' THEN
5860 
5861          l_sum_app_amt := l_trx_amt;
5862 
5863          l_inv_inst_pmt_days_sum :=
5864               (l_apply_date - (l_trx_due_date + nvl(l_printing_lead_days,0)))
5865                      * l_trx_amt;
5866 
5867        --
5868        --Impact on following columns
5869        --  INV_PAID_AMT
5870        --  COUNT_OF_TOT_INV_INST_PAID
5871        --  COUNT_OF_DISC_INV_INST
5872        --  COUNT_OF_INV_INST_PAID_LATE
5873        --
5874        -- To get the impact of the application reversal on the
5875        -- count_of_inv_inst_paid_late, we need to find if there were
5876        -- any discounted applications against this specific payment schedule
5877        -- prior to this current APP reversal RA record.
5878        -- We can check that by looking at all RA records with receivable_application_id
5879        -- less than that of current record.
5880 
5884             nvl(l_unedisc_value,0) <> 0)
5881        --If the current application that is being reversed has discount on it then only
5882        --we need the info to update the COUNT_OF_DISC_INV_INST.
5883         IF (nvl(l_edisc_value,0) <> 0 OR
5885          THEN
5886             OPEN get_inv_disc_info(l_receivable_application_id);
5887               FETCH get_inv_disc_info INTO l_prior_disc_amt;
5888             CLOSE get_inv_disc_info;
5889         END IF;
5890 
5891 
5892         IF l_new_trx_ps_status = 'OP'  AND
5893            l_old_trx_ps_status = 'CL'
5894          THEN
5895             l_inv_paid_amt := -nvl(l_trx_app_amt,0) + l_trx_amt;
5896             l_count_of_tot_inv_inst_paid := -1;
5897 
5898           --If the only discount on the trx is due to current application
5899           --which is being reversed then count_of_disc_inv_inst needs
5900           --to be bumped up by 1.
5901           IF l_prior_disc_amt <> 0 AND
5902              (l_prior_disc_amt =
5903                    (nvl(l_edisc_value,0) + nvl(l_unedisc_value,0)))
5904            THEN
5905             l_count_of_disc_inv_inst := -1;
5906           END IF;
5907 
5908           IF l_apply_date > nvl(l_trx_due_date,l_apply_date) THEN
5909              l_count_of_inv_inst_paid_late := -1;
5910           END IF;
5911 
5912         ELSIF l_new_trx_ps_status = 'CL'  AND
5913            l_old_trx_ps_status = 'OP' THEN
5914              --
5915              --This is an overapplication case
5916              --
5917             l_inv_paid_amt :=  l_trx_amt;
5918             l_count_of_tot_inv_inst_paid := 1;
5919 
5920           --If the only discount on the trx is due to current application
5921           --which is being reversed then count_of_disc_inv_inst needs
5922           --to be bumped up by 1.
5923           IF l_prior_disc_amt <> 0 AND
5924              (l_prior_disc_amt =
5925                    (nvl(l_edisc_value,0) + nvl(l_unedisc_value,0)))
5926            THEN
5927             l_count_of_disc_inv_inst := 1;
5928           END IF;
5929 
5930 
5931            IF l_apply_date > nvl(l_trx_due_date,l_apply_date) THEN
5932              l_count_of_inv_inst_paid_late := 1;
5933           END IF;
5934         END IF;
5935 
5936 
5937        --
5938        --Impact on following columns
5939        --  PAST_DUE_INV_INST_COUNT
5940        --
5941        -- If due_date on the inv for which the application is being reversed
5942        -- is past sysdate then bump up the past_due_inv_value. Based on if the
5943        -- status of the inv is being changed from CL to OP bump up the
5944        -- past_due_inv_inst_count.
5945 
5946         IF  l_trx_due_date < sysdate THEN
5947           IF l_old_trx_ps_status = 'CL' AND
5948             l_new_trx_ps_status = 'OP'
5949            THEN
5950              l_past_due_inv_inst_count := -1;
5951           END IF;
5952              l_past_due_inv_value := -l_trx_app_amt;
5953         END IF;
5954 
5955        --
5956        --Impact on following columns
5957        --  SUM_APP_AMT_DAYS_LATE
5958        --
5959        -- If the apply_date on the application that is being reversed is
5960        -- greater than the due_date on the invoice installment then we
5961        -- need to reduce the sum_app_amt_days_late by the sum of the
5962        -- amount applied.
5963 
5964         IF l_apply_date > nvl(l_trx_due_date,l_apply_date) THEN
5965            l_sum_app_amt_days_late :=
5966                         (l_apply_date - nvl(l_trx_due_date ,l_apply_date))
5967                                * l_trx_amt;
5968         END IF;
5969 
5970       END IF;
5971 
5972       /*****************************************************************
5973         Columns that need to be updated in the summary tables due to a
5974         receipt application
5975         AR_TRX_SUMMARY
5976         ==================
5977         1) INV_PAID_AMOUNT
5978         2) INV_INST_PMT_DAYS_SUM
5979         3) TOTAL_EARNED_DISC_VALUE
5980         4) TOTAL_EARNED_DISC_COUNT
5981         5) TOTAL_UNEARNED_DISC_VALUE
5982         6) TOTAL_UNEARNED_DISC_COUNT
5983         7) SUM_APP_AMT_DAYS_LATE
5984         8) SUM_APP_AMT
5985         9) COUNT_OF_TOT_INV_INST_PAID
5986         10)COUNT_OF_INV_INST_PAID_LATE
5987         11)COUNT_OF_DISC_INV_INST
5988         12)DAYS_CREDIT_GRANTED_SUM
5989 
5990         AR_TRX_BAL_SUMMARY
5991         ==================
5992         1) OP_INVOICES_VALUE
5993         2) OP_INVOICES_COUNT
5994         3) OP_DEBIT_MEMOS_VALUE
5995         4) OP_DEBIT_MEMOS_COUNT
5996         5) OP_DEPOSITS_VALUE
5997         6) OP_DEPOSITS_COUNT
5998         7) OP_CHARGEBACK_VALUE
5999         8) OP_CHARGEBACK_COUNT
6000         9) OP_CREDIT_MEMOS_VALUE
6001         10)OP_CREDIT_MEMOS_COUNT
6002         11)UNRESOLVED_CASH_VALUE  l_rcp_cash_amt
6003         12)UNRESOLVED_CASH_COUNT
6004         13)PAST_DUE_INV_VALUE
6005         14)PAST_DUE_INV_INST_COUNT
6006 
6007         *****************************************************************/
6008 
6009         Update_recapp_info(l_trx_class,
6010                            l_trx_customer_id,
6011                            l_trx_site_use_id,
6012                            l_trx_currency_code,
6013                            l_trx_amt          ,
6014                            l_op_trx_count,
6015                            l_rcpt_customer_id ,
6016                            l_rcpt_site_use_id ,
6017                            l_rcpt_currency_code,
6018                            l_rcpt_amt          ,
6019                            l_apply_date ,
6020                            l_edisc_value,
6024                            l_inv_paid_amt,
6021                            l_edisc_count,
6022                            l_unedisc_value,
6023                            l_unedisc_count,
6025                            l_inv_inst_pmt_days_sum,
6026                            l_sum_app_amt_days_late,
6027                            l_sum_app_amt,
6028                            l_count_of_tot_inv_inst_paid,
6029                            l_count_of_inv_inst_paid_late,
6030                            l_count_of_disc_inv_inst,
6031                            l_unresolved_cash_value,
6032                            l_unresolved_cash_count,
6033                            null,
6034                            'CASH',
6035                            l_past_due_inv_value,
6036                            l_past_due_inv_inst_count,
6037                            l_org_id
6038                            );
6039 
6040      --
6041    CLOSE get_recapp_details;
6042 
6043     IF pg_debug = 'Y'
6044     THEN
6045         debug ('AR_BUS_EVENT_SUB_PVT.CashApp_UnApply(-)');
6046     END IF;
6047   Return 'SUCCESS';
6048 EXCEPTION
6049     WHEN OTHERS  THEN
6050      ROLLBACK TO CashApp_UnApply_pvt;
6051 
6052      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
6053      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
6054      FND_MSG_PUB.ADD;
6055 
6056      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'CashApp_UnApply', p_event.getEventName(), p_subscription_guid);
6057      WF_EVENT.setErrorInfo(p_event, 'ERROR');
6058 
6059      RETURN 'ERROR';
6060 
6061 END CashApp_UnApply;
6062 
6063 --This function is not being used by ant BE currently.
6064 FUNCTION Adjustment
6065 ( p_subscription_guid In RAW
6066 , p_event IN OUT NOCOPY WF_EVENT_T
6067 )
6068 RETURN VARCHAR2 IS
6069 
6070 BEGIN
6071 
6072     IF pg_debug = 'Y'
6073     THEN
6074         debug ('AR_BUS_EVENT_SUB_PVT.Adjustment(+)');
6075     END IF;
6076   Return 'SUCCESS';
6077     IF pg_debug = 'Y'
6078     THEN
6079         debug ('AR_BUS_EVENT_SUB_PVT.Adjustment(-)');
6080     END IF;
6081 END Adjustment;
6082 
6083 FUNCTION AutoInv_Run
6084 ( p_subscription_guid In RAW
6085 , p_event IN OUT NOCOPY WF_EVENT_T
6086 )
6087 RETURN VARCHAR2 IS
6088 
6089 CURSOR get_AI_run_data(p_req_id IN NUMBER) IS
6090   Select ps.class,
6091          ps.customer_id,
6092          ps.customer_site_use_id,
6093          ps.trx_date,
6094          ps.invoice_currency_code,
6095          ps.org_id,
6096          ps.due_date,
6097          ps.customer_trx_id ,
6098          trx.previous_customer_trx_id,
6099          ctt.type prev_trx_type,
6100          ps.terms_sequence_number,
6101          ps.amount_due_original,
6102          trx_sum.largest_inv_amount largest_inv_amount,
6103          trx_sum.largest_inv_date largest_inv_date,
6104          trx_sum.largest_inv_cust_trx_id largest_inv_cust_trx_id,
6105          count(nvl(rtl.term_id,1)) installment_count,
6106          sum(decode(sign(ra_cm.amount_applied),0,null,
6107                 decode(ctt.type,'INV',
6108                     decode(cm_app_ps.status,'CL',1,null))))
6109                                    cm_closed_inv_count,
6110          sum(decode(sign(ra_cm.amount_applied),0,null,
6111               decode(ctt.type,'DM',
6112                     decode(cm_app_ps.status,'CL',1,null))))
6113                                    cm_closed_dm_count,
6114          sum(decode(sign(ra_cm.amount_applied),0,null,
6115               decode(ctt.type,'CM',
6116                     decode(cm_app_ps.status,'CL',1,null))))
6117                                    cm_closed_cm_count,
6118          sum(decode(sign(ra_cm.amount_applied),0,null,
6119               decode(ctt.type,'INV',
6120                 decode(cm_app_ps.status,'CL',
6121                     decode(sign( cm_app_ps.due_date - trunc(sysdate)),-1,1,null)
6122                        )
6123                      )
6124                    ))              cm_cl_past_due_inv_ct,
6125          sum(decode(sign(ra_cm.amount_applied),0,null,
6126               decode(ctt.type,'INV',
6127                 decode(cm_app_ps.status,'CL',
6128                     decode(sign( cm_app_ps.due_date - trunc(sysdate)),-1,
6129                         ra_cm.amount_applied,null)
6130                        )
6131                      )
6132                    ))              cm_cl_past_due_inv_amt
6133   from ra_customer_trx trx,
6134        ar_payment_schedules ps,
6135        ra_customer_trx prev_trx,
6136        ra_cust_trx_types ctt,
6137        ra_terms rt,
6138        ra_terms_lines rtl,
6139        ar_receivable_applications_all ra_cm,
6140        ar_payment_schedules_all cm_app_ps,
6141        ar_trx_summary trx_sum
6142   where trx.customer_trx_id = ps.customer_trx_id
6143     and trx.request_id = p_req_id
6144     and trx.previous_customer_trx_id = prev_trx.customer_trx_id(+)
6145     and prev_trx.cust_trx_type_id = ctt.cust_trx_type_id(+)
6146     and rt.term_id(+) = ps.term_id
6147     and rt.term_id = rtl.term_id(+)
6148     and trx.customer_trx_id = ra_cm.customer_trx_id(+)
6149     and ra_cm.applied_payment_schedule_id = cm_app_ps.payment_schedule_id(+)
6150     and trx_sum.cust_account_id(+) = trx.bill_to_customer_id
6151     and trx_sum.site_use_id(+) = trx.bill_to_site_use_id
6152     and trx_sum.currency(+) = trx.invoice_currency_code
6153     and trx_sum.as_of_date(+) = trx.trx_date
6154     and trx_sum.org_id (+) = trx.org_id
6155   group by ps.class,
6156          ps.customer_id,
6157          ps.customer_site_use_id,
6158          ps.trx_date,
6159          ps.invoice_currency_code,
6160          ps.org_id,
6161          ps.due_date,
6162          ps.customer_trx_id ,
6163          trx.previous_customer_trx_id,
6164          ctt.type,
6168          trx_sum.largest_inv_date ,
6165          ps.terms_sequence_number,
6166          ps.amount_due_original,
6167          trx_sum.largest_inv_amount,
6169          trx_sum.largest_inv_cust_trx_id
6170   order by ps.customer_trx_id,ps.terms_sequence_number;
6171 
6172   l_key         VARCHAR2(240) := p_event.GetEventKey();
6173   l_payment_schedule_id   NUMBER(15);
6174   l_request_id       NUMBER;
6175   l_org_id           NUMBER;
6176   l_user_id          NUMBER;
6177   l_resp_id          NUMBER;
6178   l_application_id   NUMBER;
6179   l_security_gr_id   NUMBER;
6180 
6181 BEGIN
6182 
6183     IF pg_debug = 'Y'
6184     THEN
6185         debug ('AR_BUS_EVENT_SUB_PVT.AutoInv_Run(+)');
6186     END IF;
6187   l_request_id := p_event.GetValueForParameter('REQUEST_ID');
6188   l_org_id          := p_event.GetValueForParameter('ORG_ID');
6189   l_user_id         := p_event.GetValueForParameter('USER_ID');
6190   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
6191   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
6192   l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
6193     IF pg_debug = 'Y'
6194     THEN
6195        debug ('l_request_id= '||l_request_id);
6196        debug ('l_org_id= '||l_org_id);
6197        debug ('l_user_id= '||l_user_id);
6198        debug ('l_resp_id= '||l_resp_id);
6199        debug ('l_application_id= '||l_application_id);
6200        debug ('l_security_gr_id= '||l_security_gr_id);
6201     END IF;
6202    SAVEPOINT  AutoInv_Run_Event;
6203    --
6204    --set the application context.
6205    --
6206   fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
6207   mo_global.init('AR');
6208   mo_global.set_policy_context('S',l_org_id);
6209 
6210   Update_summary_for_request_id(l_request_id);
6211     IF pg_debug = 'Y'
6212     THEN
6213         debug ('AR_BUS_EVENT_SUB_PVT.AutoInv_Run(-)');
6214     END IF;
6215 
6216   Return 'SUCCESS';
6217  EXCEPTION
6218     WHEN OTHERS  THEN
6219      ROLLBACK TO AutoInv_Run_Event;
6220 
6221      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
6222      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
6223      FND_MSG_PUB.ADD;
6224 
6225      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'AUTOINV_RUN', p_event.getEventName(), p_subscription_guid);
6226      WF_EVENT.setErrorInfo(p_event, 'ERROR');
6227 
6228      RETURN 'ERROR';
6229 
6230 END AutoInv_Run;
6231 
6232 FUNCTION AutoRcpt_Run
6233 ( p_subscription_guid In RAW
6234 , p_event IN OUT NOCOPY WF_EVENT_T
6235 )
6236 RETURN VARCHAR2 IS
6237   l_key         VARCHAR2(240) := p_event.GetEventKey();
6238   l_request_id       NUMBER;
6239   l_org_id           NUMBER;
6240   l_user_id          NUMBER;
6241   l_resp_id          NUMBER;
6242   l_application_id   NUMBER;
6243   l_security_gr_id   NUMBER;
6244 
6245 BEGIN
6246 
6247     IF pg_debug = 'Y'
6248     THEN
6249         debug ('AR_BUS_EVENT_SUB_PVT.AutoRcpt_Run(+)');
6250     END IF;
6251   l_request_id      := p_event.GetValueForParameter('REQUEST_ID');
6252   l_org_id          := p_event.GetValueForParameter('ORG_ID');
6253   l_user_id         := p_event.GetValueForParameter('USER_ID');
6254   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
6255   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
6256   l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
6257     IF pg_debug = 'Y'
6258     THEN
6259        debug ('l_request_id= '||l_request_id);
6260        debug ('l_org_id= '||l_org_id);
6261        debug ('l_user_id= '||l_user_id);
6262        debug ('l_resp_id= '||l_resp_id);
6263        debug ('l_application_id= '||l_application_id);
6264        debug ('l_security_gr_id= '||l_security_gr_id);
6265     END IF;
6266    SAVEPOINT  AutoRcpt_Run_Event;
6267    --
6268    --set the application context.
6269    --
6270    fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
6271    mo_global.init('AR');
6272    mo_global.set_policy_context('S',l_org_id);
6273 
6274    Update_rcpt_app_info_for_req(l_request_id, l_org_id);
6275     IF pg_debug = 'Y'
6276     THEN
6277         debug ('AR_BUS_EVENT_SUB_PVT.AutoRcpt_Run(-)');
6278     END IF;
6279   Return 'SUCCESS';
6280  EXCEPTION
6281     WHEN OTHERS  THEN
6282      ROLLBACK TO AutoRcpt_Run_Event;
6283 
6284      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
6285      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
6286      FND_MSG_PUB.ADD;
6287 
6288      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'AUTORCPT_RUN', p_event.getEventName(), p_subscription_guid);
6289      WF_EVENT.setErrorInfo(p_event, 'ERROR');
6290 
6291      RETURN 'ERROR';
6292 END AutoRcpt_Run;
6293 
6294 FUNCTION AutoAdj_Run
6295 ( p_subscription_guid In RAW
6296 , p_event IN OUT NOCOPY WF_EVENT_T
6297 )
6298 RETURN VARCHAR2 IS
6299   l_org_id          NUMBER;
6300   l_user_id         NUMBER;
6301   l_resp_id         NUMBER;
6302   l_application_id  NUMBER;
6303   l_security_gr_id  NUMBER;
6304   l_request_id          NUMBER;
6305 
6306   l_customer_id         NUMBER;
6307   l_site_use_id         NUMBER;
6308   l_currency_code       VARCHAR2(30);
6309   l_apply_date          DATE;
6310   l_class               VARCHAR2(10);
6311   l_due_date            DATE;
6312   l_adj_status      VARCHAR2(20);
6313 
6314   l_adj_amount          NUMBER;
6315   l_pending_adj_amount  NUMBER;
6316   l_adj_count           NUMBER;
6317   l_past_due_inv_inst_count  NUMBER;
6318   l_past_due_inv_value  NUMBER;
6319 
6320   CURSOR get_adj (p_req_id  IN NUMBER) IS
6321    SELECT sum(amount),count(adj.adjustment_id) adj_count,
6322           ps.customer_id, ps.customer_site_use_id,
6326         ar_payment_schedules ps
6323           ps.invoice_currency_code, adj.apply_date,
6324           ps.class, ps.due_date, adj.status
6325    FROM ar_adjustments adj,
6327    WHERE adj.request_id = p_req_id
6328      and adj.payment_schedule_id = ps.payment_schedule_id
6329    group by ps.customer_id,
6330             ps.customer_site_use_id,
6331             ps.invoice_currency_code,
6332             adj.apply_date,
6333             ps.class,
6334             ps.due_date,
6335 	    adj.status;
6336 
6337 BEGIN
6338     IF pg_debug = 'Y'
6339     THEN
6340         debug ('AR_BUS_EVENT_SUB_PVT.AutoAdj_Run(+)');
6341     END IF;
6342   l_request_id      := p_event.GetValueForParameter('REQUEST_ID');
6343   l_org_id          := p_event.GetValueForParameter('ORG_ID');
6344   l_user_id         := p_event.GetValueForParameter('USER_ID');
6345   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
6346   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
6347 
6348     IF pg_debug = 'Y'
6349     THEN
6350        debug ('l_request_id= '||l_request_id);
6351        debug ('l_org_id= '||l_org_id);
6352        debug ('l_user_id= '||l_user_id);
6353        debug ('l_resp_id= '||l_resp_id);
6354        debug ('l_application_id= '||l_application_id);
6355        debug ('l_security_gr_id= '||l_security_gr_id);
6356     END IF;
6357   SAVEPOINT AutoADJ_Run_Event;
6358    --
6359    --set the application context.
6360    --
6361   fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
6362   mo_global.init('AR');
6363   mo_global.set_policy_context('S',l_org_id);
6364 
6365   OPEN get_adj(l_request_id);
6366 
6367   LOOP
6368      FETCH get_adj  INTO l_adj_amount,
6369                          l_adj_count,
6370                          l_customer_id,
6371                          l_site_use_id,
6372                          l_currency_code,
6373                          l_apply_date,
6374                          l_class,
6375                          l_due_date,
6376 		         l_adj_status;
6377 
6378      IF get_adj%NOTFOUND THEN
6379         EXIT;
6380      END IF;
6381 
6382      IF  l_adj_status = 'A'
6383      THEN
6384 	l_pending_adj_amount := 0;
6385 	IF l_due_date < sysdate
6386         THEN
6387            l_past_due_inv_inst_count  := -1;
6388            l_past_due_inv_value  := l_adj_amount;
6389         END IF;
6390      ELSE
6391         l_pending_adj_amount := l_adj_amount;
6392 	l_adj_amount := 0;
6393 	l_adj_count := 0;
6394      END IF;
6395 
6396      Update_Adj_info (
6397               l_customer_id,
6398               l_site_use_id,
6399               l_org_id    ,
6400               l_currency_code,
6401               l_adj_amount   ,
6402               l_adj_count ,
6403               l_apply_date   ,
6404               l_pending_adj_amount,
6405               l_class,
6406               null,
6407               l_past_due_inv_inst_count,
6408               l_past_due_inv_value );
6409   END LOOP;
6410 
6411   CLOSE get_adj;
6412 
6413     IF pg_debug = 'Y'
6414     THEN
6415         debug ('AR_BUS_EVENT_SUB_PVT.AutoAdj_Run(-)');
6416     END IF;
6417   Return 'SUCCESS';
6418 EXCEPTION
6419 WHEN OTHERS  THEN
6420      ROLLBACK TO AutoADJ_Run_Event;
6421 
6422      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
6423      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
6424      FND_MSG_PUB.ADD;
6425 
6426      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'AUTO_ADJ_RUN', p_event.getEventName(), p_subscription_guid);
6427      WF_EVENT.setErrorInfo(p_event, 'ERROR');
6428 
6429      RETURN 'ERROR';
6430 
6431 END AutoAdj_Run;
6432 
6433 FUNCTION QuickCash_PostBatch
6434 ( p_subscription_guid In RAW
6435 , p_event IN OUT NOCOPY WF_EVENT_T
6436 )
6437 RETURN VARCHAR2 IS
6438   l_key         VARCHAR2(240) := p_event.GetEventKey();
6439   l_request_id       NUMBER;
6440   l_org_id           NUMBER;
6441   l_user_id          NUMBER;
6442   l_resp_id          NUMBER;
6443   l_application_id   NUMBER;
6444   l_security_gr_id   NUMBER;
6445 
6446 BEGIN
6447 
6448     IF pg_debug = 'Y'
6449     THEN
6450         debug ('AR_BUS_EVENT_SUB_PVT.QuickCash_PostBatch(+)');
6451     END IF;
6452   l_request_id      := p_event.GetValueForParameter('REQUEST_ID');
6453   l_org_id          := p_event.GetValueForParameter('ORG_ID');
6454   l_user_id         := p_event.GetValueForParameter('USER_ID');
6455   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
6456   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
6457   l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
6458     IF pg_debug = 'Y'
6459     THEN
6460        debug ('l_request_id= '||l_request_id);
6461        debug ('l_org_id= '||l_org_id);
6462        debug ('l_user_id= '||l_user_id);
6463        debug ('l_resp_id= '||l_resp_id);
6464        debug ('l_application_id= '||l_application_id);
6465        debug ('l_security_gr_id= '||l_security_gr_id);
6466     END IF;
6467    SAVEPOINT  QuickCash_PostBatch_pvt;
6468    --
6469    --set the application context.
6470    --
6471    fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
6472    mo_global.init('AR');
6473    mo_global.set_policy_context('S',l_org_id);
6474 
6475    Update_rcpt_app_info_for_req(l_request_id, l_org_id);
6476     IF pg_debug = 'Y'
6477     THEN
6478         debug ('AR_BUS_EVENT_SUB_PVT.QuickCash_PostBatch(-)');
6479     END IF;
6480 
6481   Return 'SUCCESS';
6482 
6483 EXCEPTION
6484     WHEN OTHERS  THEN
6485      ROLLBACK TO QuickCash_PostBatch_pvt;
6486 
6490 
6487      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
6488      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
6489      FND_MSG_PUB.ADD;
6491      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'QuickCash_PostBatch', p_event.getEventName(), p_subscription_guid);
6492      WF_EVENT.setErrorInfo(p_event, 'ERROR');
6493 
6494      RETURN 'ERROR';
6495 
6496 END QuickCash_PostBatch;
6497 
6498 FUNCTION Aging_PastDue
6499 ( p_subscription_guid In RAW
6500 , p_event IN OUT NOCOPY WF_EVENT_T
6501 )
6502 RETURN VARCHAR2
6503 IS
6504 
6505 BEGIN
6506 
6507     IF pg_debug = 'Y'
6508     THEN
6509         debug ('AR_BUS_EVENT_SUB_PVT.Aging_PastDue(+)');
6510     END IF;
6511   Return 'SUCCESS';
6512     IF pg_debug = 'Y'
6513     THEN
6514         debug ('AR_BUS_EVENT_SUB_PVT.Aging_PastDue(-)');
6515     END IF;
6516 END Aging_PastDue;
6517 
6518 FUNCTION AdjCreate
6519 ( p_subscription_guid In RAW
6520 , p_event IN OUT NOCOPY WF_EVENT_T
6521 )
6522 RETURN VARCHAR2
6523 IS
6524   l_adjustment_id   NUMBER;
6525   l_app_ps_status   VARCHAR2(10);
6526   l_org_id          NUMBER;
6527   l_user_id         NUMBER;
6528   l_resp_id         NUMBER;
6529   l_application_id  NUMBER;
6530   l_security_gr_id  NUMBER;
6531   l_adj_status      VARCHAR2(20);
6532 
6533   l_amount              NUMBER;
6534   l_apply_date          DATE;
6535   l_receivables_trx_id  NUMBER;
6536   l_customer_id         NUMBER;
6537   l_site_use_id         NUMBER;
6538   l_currency_code       VARCHAR2(30);
6539   l_class               VARCHAR2(10);
6540   l_pending_adj_amount  NUMBER;
6541   l_adj_amount          NUMBER;
6542   l_op_trx_count        NUMBER;
6543   l_special_adj         VARCHAR2(10);
6544   l_due_date            DATE;
6545   CURSOR get_adj_details (p_adj_id IN NUMBER) IS
6546   SELECT adj.amount, adj.apply_date, adj.receivables_trx_id,
6547          ps.customer_id, ps.customer_site_use_id, ps.invoice_currency_code,
6548          ps.class, ps.due_date
6549   FROM ar_adjustments adj,
6550        ar_payment_schedules ps
6551   WHERE adj.payment_schedule_id = ps.payment_schedule_id
6552    and  adj.adjustment_id = p_adj_id ;
6553 l_past_due_inv_inst_count  NUMBER;
6554 l_past_due_inv_value       NUMBER;
6555 BEGIN
6556     IF pg_debug = 'Y'
6557     THEN
6558         debug ('AR_BUS_EVENT_SUB_PVT.AdjCreate(+)');
6559     END IF;
6560   l_adjustment_id   := p_event.GetValueForParameter('ADJUSTMENT_ID');
6561   l_adj_status      := p_event.GetValueForParameter('ADJ_STATUS');
6562   l_app_ps_status   := p_event.GetValueForParameter('APPLIED_PS_STATUS');
6563   l_org_id          := p_event.GetValueForParameter('ORG_ID');
6564   l_user_id         := p_event.GetValueForParameter('USER_ID');
6565   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
6566   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
6567   l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
6568 
6569     IF pg_debug = 'Y'
6570     THEN
6571        debug ('l_adjustment_id= '||l_adjustment_id);
6572        debug ('l_adj_status= '||l_adj_status);
6573        debug ('l_app_ps_status= '||l_app_ps_status);
6574        debug ('l_org_id= '||l_org_id);
6575        debug ('l_user_id= '||l_user_id);
6576        debug ('l_resp_id= '||l_resp_id);
6577        debug ('l_application_id= '||l_application_id);
6578        debug ('l_security_gr_id= '||l_security_gr_id);
6579     END IF;
6580   SAVEPOINT ADJCreate_Event;
6581    --
6582    --set the application context.
6583    --
6584   fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
6585   mo_global.init('AR');
6586   mo_global.set_policy_context('S',l_org_id);
6587 
6588   OPEN  get_adj_details (l_adjustment_id);
6589 
6590   FETCH get_adj_details
6591               INTO l_amount,
6592                    l_apply_date,
6593                    l_receivables_trx_id,
6594                    l_customer_id,
6595                    l_site_use_id,
6596                    l_currency_code,
6597                    l_class,
6598                    l_due_date;
6599 
6600 	IF get_adj_details%NOTFOUND THEN
6601 	   CLOSE get_adj_details;
6602            Return 'SUCCESS';
6603         END IF;
6604   CLOSE get_adj_details;
6605 
6606   IF l_receivables_trx_id > 0 and
6607      l_adj_status = 'A' THEN
6608      l_adj_amount := l_amount;
6609   ELSIF l_receivables_trx_id > 0 and
6610      l_adj_status <> 'A' THEN
6611      l_pending_adj_amount := l_amount;
6612   ELSIF l_receivables_trx_id in ( -12,-11)  THEN
6613      --chargeback adjustment and its reversal
6614      l_adj_amount := l_amount;
6615      l_special_adj := 'Y';
6616   ELSIF l_receivables_trx_id = -1 THEN
6617      --commitment
6618      l_adj_amount := l_amount;
6619      l_special_adj := 'Y';
6620   ELSIF l_receivables_trx_id = -15 THEN
6621      --br creation closing inv
6622      l_adj_amount := l_amount;
6623      l_special_adj := 'Y';
6624   END IF;
6625 
6626 
6627   IF l_app_ps_status = 'CL'  THEN
6628        l_op_trx_count := -1;
6629 
6630     IF l_due_date < sysdate THEN
6631        l_past_due_inv_inst_count  := -1;
6632        l_past_due_inv_value  := l_adj_amount;
6633     END IF;
6634 
6635   ELSIF l_app_ps_status = 'OP'  THEN
6636       l_op_trx_count := 1;
6637     IF l_due_date < sysdate THEN
6638        l_past_due_inv_inst_count  := 1;
6639        l_past_due_inv_value  := l_adj_amount;
6640     END IF;
6641  -- Begin Bug 13072018 26-Mar-2012 barathsr
6642  -- Adjustments where the status is neither OP nor CL and it is a line adjustment
6643   ELSIF l_app_ps_status = 'NO_CHANGE' THEN
6647         END IF;
6644         IF l_due_date < sysdate THEN
6645           l_past_due_inv_value  := l_adj_amount;
6646 
6648   --End Bug 13072018 26-Mar-2012 barathsr
6649   END IF;
6650 
6651         Update_Adj_info (
6652               l_customer_id,
6653               l_site_use_id,
6654               l_org_id    ,
6655               l_currency_code,
6656               l_adj_amount   ,
6657               l_op_trx_count ,
6658               l_apply_date   ,
6659               l_pending_adj_amount,
6660               l_class ,
6661               l_special_adj,
6662               l_past_due_inv_inst_count,
6663               l_past_due_inv_value);
6664 
6665     IF pg_debug = 'Y'
6666     THEN
6667         debug ('AR_BUS_EVENT_SUB_PVT.AdjCreate(-)');
6668     END IF;
6669   Return 'SUCCESS';
6670 EXCEPTION
6671     WHEN OTHERS  THEN
6672      ROLLBACK TO ADJCreate_Event;
6673 
6674      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
6675      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
6676      FND_MSG_PUB.ADD;
6677 
6678      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'ADJ_CREATE', p_event.getEventName(), p_subscription_guid);
6679      WF_EVENT.setErrorInfo(p_event, 'ERROR');
6680 
6681      RETURN 'ERROR';
6682 
6683 
6684 END AdjCreate;
6685 
6686 FUNCTION AdjApprove
6687 ( p_subscription_guid In RAW
6688 , p_event IN OUT NOCOPY WF_EVENT_T
6689 )
6690 RETURN VARCHAR2
6691 IS
6692   l_adjustment_id   NUMBER;
6693   l_app_ps_status   VARCHAR2(10);
6694   l_org_id          NUMBER;
6695   l_user_id         NUMBER;
6696   l_resp_id         NUMBER;
6697   l_application_id  NUMBER;
6698   l_security_gr_id  NUMBER;
6699   l_adj_status      VARCHAR2(20);
6700 
6701   l_amount              NUMBER;
6702   l_apply_date          DATE;
6703   l_receivables_trx_id  NUMBER;
6704   l_customer_id         NUMBER;
6705   l_site_use_id         NUMBER;
6706   l_currency_code       VARCHAR2(30);
6707   l_class               VARCHAR2(10);
6708   l_pending_adj_amount  NUMBER;
6709   l_adj_amount          NUMBER;
6710   l_op_trx_count        NUMBER;
6711   l_past_due_inv_inst_count NUMBER;
6712   l_past_due_inv_value  NUMBER;
6713   l_due_date   DATE;
6714   CURSOR get_adj_details (p_adj_id IN NUMBER) IS
6715   SELECT adj.amount, adj.apply_date, adj.receivables_trx_id,
6716          ps.customer_id, ps.customer_site_use_id, ps.invoice_currency_code,
6717          ps.class, ps.due_date
6718   FROM ar_adjustments adj,
6719        ar_payment_schedules ps
6720   WHERE adj.payment_schedule_id = ps.payment_schedule_id
6721    and  adj.adjustment_id = p_adj_id ;
6722 
6723 BEGIN
6724     IF pg_debug = 'Y'
6725     THEN
6726         debug ('AR_BUS_EVENT_SUB_PVT.AdjApprove(+)');
6727     END IF;
6728   l_adjustment_id   := p_event.GetValueForParameter('ADJUSTMENT_ID');
6729   l_adj_status      := p_event.GetValueForParameter('APPROVAL_ACTN_HIST_ID');
6730   l_app_ps_status   := p_event.GetValueForParameter('APPLIED_PS_STATUS');
6731   l_org_id          := p_event.GetValueForParameter('ORG_ID');
6732   l_user_id         := p_event.GetValueForParameter('USER_ID');
6733   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
6734   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
6735   l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
6736     IF pg_debug = 'Y'
6737     THEN
6738        debug ('l_adjustment_id= '||l_adjustment_id);
6739        debug ('l_adj_status= '||l_adj_status);
6740        debug ('l_app_ps_status= '||l_app_ps_status);
6741        debug ('l_org_id= '||l_org_id);
6742        debug ('l_user_id= '||l_user_id);
6743        debug ('l_resp_id= '||l_resp_id);
6744        debug ('l_application_id= '||l_application_id);
6745        debug ('l_security_gr_id= '||l_security_gr_id);
6746      END IF;
6747   SAVEPOINT ADJApprove_Event;
6748    --
6749    --set the application context.
6750    --
6751   fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
6752   mo_global.init('AR');
6753   mo_global.set_policy_context('S',l_org_id);
6754 
6755   OPEN  get_adj_details (l_adjustment_id);
6756   FETCH get_adj_details
6757               INTO l_amount,
6758                    l_apply_date,
6759                    l_receivables_trx_id,
6760                    l_customer_id,
6761                    l_site_use_id,
6762                    l_currency_code,
6763                    l_class,
6764                    l_due_date;
6765 
6766 	IF get_adj_details%NOTFOUND THEN
6767 	 CLOSE get_adj_details;
6768 	 RETURN 'SUCCESS';
6769        END IF;
6770   CLOSE get_adj_details;
6771 
6772   IF l_receivables_trx_id > 0 THEN
6773      l_adj_amount := l_amount;
6774      l_pending_adj_amount := l_amount;
6775   END IF;
6776 
6777 
6778   IF l_app_ps_status = 'CL'  THEN
6779       l_op_trx_count := -1;
6780     IF l_due_date < sysdate THEN
6781        l_past_due_inv_inst_count  := -1;
6782        l_past_due_inv_value  := l_adj_amount;
6783     END IF;
6784 
6785   ELSIF l_app_ps_status = 'OP'  THEN
6786       l_op_trx_count := 1;
6787     IF l_due_date < sysdate THEN
6788        l_past_due_inv_inst_count  := 1;
6789        l_past_due_inv_value  := l_adj_amount;
6790     END IF;
6791 
6792   END IF;
6793 
6794 
6795         Update_Adj_info (
6796               l_customer_id,
6797               l_site_use_id,
6798               l_org_id    ,
6799               l_currency_code,
6800               l_adj_amount   ,
6801               l_op_trx_count ,
6802               l_apply_date   ,
6803               -l_pending_adj_amount,
6807               l_past_due_inv_value);
6804               l_class,
6805               null,
6806               l_past_due_inv_inst_count,
6808 
6809     IF pg_debug = 'Y'
6810     THEN
6811         debug ('AR_BUS_EVENT_SUB_PVT.AdjApprove(-)');
6812     END IF;
6813   Return 'SUCCESS';
6814 EXCEPTION
6815     WHEN OTHERS  THEN
6816      ROLLBACK TO ADJApprove_Event;
6817 
6818      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
6819      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
6820      FND_MSG_PUB.ADD;
6821 
6822      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'ADJ_APPROVE', p_event.getEventName(), p_subscription_guid);
6823      WF_EVENT.setErrorInfo(p_event, 'ERROR');
6824 
6825      RETURN 'ERROR';
6826 
6827 END AdjApprove;
6828 
6829 FUNCTION Recurr_Invoice
6830 (p_subscription_guid In RAW
6831 ,p_event IN OUT NOCOPY WF_EVENT_T
6832 )
6833 RETURN VARCHAR2
6834 IS
6835 
6836 BEGIN
6837 
6838     IF pg_debug = 'Y'
6839     THEN
6840         debug ('AR_BUS_EVENT_SUB_PVT.Recurr_Invoice(+)');
6841     END IF;
6842   Return 'SUCCESS';
6843 
6844     IF pg_debug = 'Y'
6845     THEN
6846         debug ('AR_BUS_EVENT_SUB_PVT.Recurr_Invoice(-)');
6847     END IF;
6848 END Recurr_Invoice;
6849 
6850 FUNCTION CopyInv_Run
6851 ( p_subscription_guid In RAW
6852 , p_event IN OUT NOCOPY WF_EVENT_T
6853 )
6854 RETURN VARCHAR2 IS
6855   i                  INTEGER;
6856   l_key         VARCHAR2(240) := p_event.GetEventKey();
6857   l_request_id       NUMBER;
6858   l_org_id           NUMBER;
6859   l_user_id          NUMBER;
6860   l_resp_id          NUMBER;
6861   l_application_id   NUMBER;
6862   l_security_gr_id   NUMBER;
6863 BEGIN
6864     IF pg_debug = 'Y'
6865     THEN
6866         debug ('AR_BUS_EVENT_SUB_PVT.CopyInv_Run(+)');
6867     END IF;
6868   l_request_id := p_event.GetValueForParameter('REQUEST_ID');
6869   l_org_id          := p_event.GetValueForParameter('ORG_ID');
6870   l_user_id         := p_event.GetValueForParameter('USER_ID');
6871   l_resp_id         := p_event.GetValueForParameter('RESP_ID');
6872   l_application_id  := p_event.GetValueForParameter('RESP_APPL_ID');
6873   l_security_gr_id  := p_event.GetValueForParameter('SECURITY_GROUP_ID');
6874     IF pg_debug = 'Y'
6875     THEN
6876        debug ('l_request_id= '||l_request_id);
6877        debug ('l_org_id= '||l_org_id);
6878        debug ('l_user_id= '||l_user_id);
6879        debug ('l_resp_id= '||l_resp_id);
6880        debug ('l_application_id= '||l_application_id);
6881        debug ('l_security_gr_id= '||l_security_gr_id);
6882      END IF;
6883    SAVEPOINT  CopyInv_Run_Event;
6884    --
6885    --set the application context.
6886    --
6887   fnd_global.apps_initialize(l_user_id,l_resp_id,l_application_id);
6888   mo_global.init('AR');
6889   mo_global.set_policy_context('S',l_org_id);
6890 
6891   Update_summary_for_request_id(l_request_id);
6892 
6893     IF pg_debug = 'Y'
6894     THEN
6895         debug ('AR_BUS_EVENT_SUB_PVT.CopyInv_Run(-)');
6896     END IF;
6897   Return 'SUCCESS';
6898 EXCEPTION
6899     WHEN OTHERS  THEN
6900      ROLLBACK TO CopyInv_Run_Event;
6901 
6902      FND_MESSAGE.SET_NAME( 'AR', 'GENERIC_MESSAGE' );
6903      FND_MESSAGE.SET_TOKEN( 'GENERIC_TEXT' ,SQLERRM );
6904      FND_MSG_PUB.ADD;
6905 
6906      WF_CORE.CONTEXT('AR_BUS_EVENT_SUB_PVT', 'COPYINV_RUN', p_event.getEventName(), p_subscription_guid);
6907      WF_EVENT.setErrorInfo(p_event, 'ERROR');
6908 
6909      RETURN 'ERROR';
6910 
6911 END CopyInv_Run;
6912 
6913 -- The following subroutines have been added for RAMC
6914 -- Please refer to Bug # 3085672 for details.
6915 
6916 FUNCTION raise_revenue_event (
6917   p_customer_trx_id NUMBER,
6918   p_customer_trx_line_id NUMBER,
6919   p_amount NUMBER,
6920   p_acctd_amount NUMBER)
6921   RETURN VARCHAR2 IS
6922 
6923 
6924 /*-----------------------------------------------------------------------+
6925  | Local Variable Declarations and initializations                       |
6926  +-----------------------------------------------------------------------*/
6927 
6928   l_item_key         wf_items.ITEM_KEY%TYPE;
6929   l_parameter_list  wf_parameter_list_t := wf_parameter_list_t();
6930 
6931 
6932 BEGIN
6933 
6934     IF pg_debug = 'Y'
6935     THEN
6936         debug ('AR_BUS_EVENT_SUB_PVT.raise_revenue_event(+)');
6937     END IF;
6938   -- This function simply raises revenue related business events.
6939 
6940 
6941   l_item_key := ar_revenue_management_pvt.c_revenue_deferral_event ||
6942                 '_'||
6943                 to_char(p_customer_trx_line_id) || '_'||
6944                 to_char(sysdate,'DD-MON-YYYY HH24:MI:SS');
6945 
6946   wf_event.addParameterToList(p_name => 'customer_trx_id',
6947     p_value => p_customer_trx_id,
6948     p_parameterlist => l_parameter_list);
6949 
6950   wf_event.addParameterToList(p_name => 'customer_trx_line_id',
6951     p_value => p_customer_trx_line_id,
6952     p_parameterlist => l_parameter_list);
6953 
6954   wf_event.addParameterToList(p_name => 'amount',
6955     p_value => p_amount,
6956     p_parameterlist => l_parameter_list);
6957 
6958   wf_event.addParameterToList(p_name => 'accounted_amount',
6959     p_value => p_acctd_amount,
6960     p_parameterlist => l_parameter_list);
6961 
6962   wf_event.raise(
6963     p_event_name     => ar_revenue_management_pvt.c_revenue_deferral_event,
6964     p_event_key      => l_item_key,
6965     p_parameters     => l_parameter_list);
6966 
6967 
6971     END IF;
6968     IF pg_debug = 'Y'
6969     THEN
6970         debug ('AR_BUS_EVENT_SUB_PVT.raise_revenue_event(-)');
6972   RETURN l_item_key;
6973 
6974 EXCEPTION
6975 
6976   WHEN NO_DATA_FOUND THEN
6977     RAISE;
6978 
6979   WHEN OTHERS THEN
6980     RAISE;
6981 
6982 END raise_revenue_event;
6983 
6984 
6985 /*========================================================================
6986  | PUBLIC FUNCTION events_manager
6987  |
6988  | DESCRIPTION
6989  |   This is a subscription program to the business event
6990  |   oracle.apps.ar.batch.AutoInvoice.run.  For the lines that were considered
6991  |   uncollectible during this autoinvoice run, we must raise business event.
6992  |
6993  | CALLED FROM PROCEDURES/FUNCTIONS
6994  |   As a subscription to the the buisness event:
6995  |     oracle.apps.ar.batch.AutoInvoice.run.:
6996  |
6997  | CALLS PROCEDURES/FUNCTIONS (local to this package body)
6998  |
6999  | PARAMETERS
7000  |   request_id
7001  |
7002  | NOTES
7003  |   None.
7004  |
7005  | MODIFICATION HISTORY
7006  | Date                  Author            Description of Changes
7007  | 30-JUL-2003           ORASHID           Subroutine Created
7008  |
7009  *=======================================================================*/
7010 
7011 FUNCTION events_manager (
7012   p_subscription_guid in raw,
7013   p_event IN OUT NOCOPY WF_EVENT_T)
7014   RETURN VARCHAR2 IS
7015 
7016   -- This cursor fetches all rows given a request id
7017   -- from the audit trail table, so that business events
7018   -- can be raised.
7019 
7020   CURSOR lines (p_request_id NUMBER) IS
7021     SELECT rowid,
7022            customer_trx_id,
7023            customer_trx_line_id,
7024            amount_due_original,
7025            acctd_amount_due_original
7026     FROM   ar_ramc_audit_trail
7027     WHERE  request_id = p_request_id
7028     AND    original_collectibility_flag = 'N';
7029 
7030   l_request_id 			NUMBER;
7031   l_last_fetch                  BOOLEAN;
7032   l_item_key                    wf_items.ITEM_KEY%TYPE;
7033   l_rowid_table                 ar_revenue_management_pvt.varchar_table;
7034   l_customer_trx_id_table 	ar_revenue_management_pvt.number_table;
7035   l_customer_trx_line_id_table	ar_revenue_management_pvt.number_table;
7036   l_line_collectible_table 	ar_revenue_management_pvt.varchar_table;
7037   l_amount_due_original_table 	ar_revenue_management_pvt.number_table;
7038   l_acctd_amount_due_orig_table	ar_revenue_management_pvt.number_table;
7039   l_acctd_amount_pending_table 	ar_revenue_management_pvt.number_table;
7040 
7041 BEGIN
7042 
7043     IF pg_debug = 'Y'
7044     THEN
7045         debug ('AR_BUS_EVENT_SUB_PVT.events_manager(+)');
7046     END IF;
7047   l_request_id := p_event.GetValueForParameter('REQUEST_ID');
7048 
7049     IF pg_debug = 'Y'
7050     THEN
7051        debug ('l_request_id= '||l_request_id);
7052     END IF;
7053   OPEN lines(l_request_id);
7054   LOOP
7055 
7056     -- this table must be deleted for re-entry
7057     -- otherwise the row count may not be zero
7058     -- and we will be stuck in an infinite loop.
7059 
7060     l_rowid_table.delete;
7061 
7062     FETCH lines BULK COLLECT INTO
7063       l_rowid_table,
7064       l_customer_trx_id_table,
7065       l_customer_trx_line_id_table,
7066       l_amount_due_original_table,
7067       l_acctd_amount_due_orig_table
7068     LIMIT ar_revenue_management_pvt.c_max_bulk_fetch_size;
7069 
7070     IF lines%NOTFOUND THEN
7071       fnd_file.put_line(fnd_file.log, 'last fetch');
7072       l_last_fetch := TRUE;
7073     END IF;
7074 
7075     fnd_file.put_line(fnd_file.log, 'Count: ' || to_char(l_rowid_table.COUNT));
7076 
7077     IF l_rowid_table.COUNT = 0 AND l_last_fetch THEN
7078       fnd_file.put_line(fnd_file.log, 'last fetch and COUNT is zero');
7079       EXIT;
7080     END IF;
7081 
7082     FOR i IN l_rowid_table.FIRST .. l_rowid_table.LAST LOOP
7083 
7084         l_item_key := raise_revenue_event (
7085           p_customer_trx_id      => l_customer_trx_id_table(i),
7086           p_customer_trx_line_id => l_customer_trx_line_id_table(i),
7087           p_amount               => l_amount_due_original_table(i),
7088           p_acctd_amount         => l_acctd_amount_due_orig_table(i));
7089 
7090         fnd_file.put_line(fnd_file.log, 'Raising Buisness Event For ' ||
7091           l_customer_trx_line_id_table(i));
7092 
7093     END LOOP;
7094 
7095   END LOOP;
7096   CLOSE Lines;
7097 
7098     IF pg_debug = 'Y'
7099     THEN
7100         debug ('AR_BUS_EVENT_SUB_PVT.events_manager(-)');
7101     END IF;
7102 
7103   RETURN 'SUCCESS';
7104 
7105 
7106 EXCEPTION
7107 
7108   WHEN OTHERS THEN
7109      WF_CORE.CONTEXT(
7110        'ar_revenue_management_pvt',
7111        'events_manager',
7112        p_event.getEventName( ),
7113        p_subscription_guid);
7114      WF_EVENT.setErrorInfo(p_event, 'ERROR');
7115      RETURN 'ERROR';
7116 
7117 END events_manager;
7118 
7119 /* 5690748 - procedure for setting the following fields after
7120     mass updates (receipt applications):
7121       op_invoices_count
7122       op_invoices_value
7123       past_due_inv_inst_count
7124       past_due_inv_value
7125 
7126     In the original bug, lockbox receipts were corrupting these columns in
7127     ar_trx_bal_summary for partial receipts, multiple applications to one
7131    p_customer_id_tab IN ar_bus_event_sub_pvt.generic_id_type,
7128     trx from different receipts, and receipts with apply_dates that
7129     occurr before the trx.due_date. */
7130 PROCEDURE refresh_counts(
7132    p_site_use_id_tab IN ar_bus_event_sub_pvt.generic_id_type,
7133    p_currency_tab    IN ar_bus_event_sub_pvt.currency_type,
7134    p_org_id_tab      IN ar_bus_event_sub_pvt.generic_id_type ) IS
7135 
7136    l_rows NUMBER;
7137 
7138 BEGIN
7139    IF pg_debug = 'Y'
7140    THEN
7141       debug ('ar_bus_event_sub_pvt.refresh_counts()+');
7142    END IF;
7143 
7144    FORALL i IN p_customer_id_tab.FIRST .. p_customer_id_tab.LAST
7145       update ar_trx_bal_summary main_sum
7146       set (op_invoices_count,
7147            op_invoices_value,
7148            past_due_inv_inst_count,
7149            past_due_inv_value,
7150            op_credit_memos_count,
7151            op_credit_memos_value,
7152            op_debit_memos_count,
7153            op_debit_memos_value,
7154            op_deposits_count,
7155            op_deposits_value,
7156            op_chargeback_count,
7157            op_chargeback_value ) =
7158       (select
7159               /* OP invoices */
7160               count(decode(trx_ps.class,'INV',trx_ps.payment_schedule_id, null)),
7161               sum(decode(trx_ps.class,'INV',trx_ps.amount_due_remaining,0)),
7162               /* past due invoices */
7163               count(decode(trx_ps.class,'INV',
7164                   decode(sign(trx_ps.due_date - trunc(sysdate)),
7165                      -1, trx_ps.payment_schedule_id,null),null)),
7166               sum(decode(trx_ps.class,'INV',
7167                   decode(sign(trx_ps.due_date - trunc(sysdate)),
7168                      -1, decode(trx_ps.class,'INV',trx_ps.amount_due_remaining,0),0),0)),
7169               /* OP credit memos */
7170               count(decode(trx_ps.class,'CM',trx_ps.payment_schedule_id, null)),
7171               sum(decode(trx_ps.class,'CM',trx_ps.amount_due_remaining,0)),
7172               /* OP debit memos */
7173               count(decode(trx_ps.class,'DM',trx_ps.payment_schedule_id, null)),
7174               sum(decode(trx_ps.class,'DM',trx_ps.amount_due_remaining,0)),
7175               /* OP deposits */
7176               count(decode(trx_ps.class,'DEP',trx_ps.payment_schedule_id, null)),
7177               sum(decode(trx_ps.class,'DEP',trx_ps.amount_due_remaining,0)),
7178               /* OP chargebacks */
7179               count(decode(trx_ps.class,'CB',trx_ps.payment_schedule_id, null)),
7180               sum(decode(trx_ps.class,'CB',trx_ps.amount_due_remaining,0))
7181        from ar_payment_schedules_all trx_ps
7182        where trx_ps.status = 'OP'
7183        and trx_ps.customer_id = main_sum.cust_account_id
7184        and trx_ps.customer_site_use_id = decode(main_sum.site_use_id,-99,
7185              trx_ps.customer_site_use_id,
7186                 main_sum.site_use_id)
7187        and trx_ps.org_id = main_sum.org_id
7188        and trx_ps.invoice_currency_code = main_sum.currency
7189        group by trx_ps.customer_id, trx_ps.customer_site_use_id,
7190                trx_ps.invoice_currency_code, trx_ps.org_id),
7191            /* WHO columns */
7192            last_update_date = sysdate,
7193            last_updated_by  = fnd_global.user_id,
7194            last_update_login= fnd_global.login_id
7195       where cust_account_id = p_customer_id_tab(i)
7196       and   site_use_id     = p_site_use_id_tab(i)
7197       and   currency        = p_currency_tab(i)
7198       and   NVL(org_id,'-99') = NVL(p_org_id_tab(i),-99);
7199 
7200     l_rows := SQL%ROWCOUNT;
7201 
7202    IF pg_debug = 'Y'
7203    THEN
7204       debug ('  updated row(s) = ' || l_rows);
7205    END IF;
7206 
7207    /* 9363502 - Set receipts_at_risk_value */
7208    refresh_at_risk_value(p_customer_id_tab,
7209                          p_site_use_id_tab,
7210                          p_currency_tab,
7211                          p_org_id_tab);
7212 
7213    IF pg_debug = 'Y'
7214    THEN
7215       debug ('ar_bus_event_sub_pvt.refresh_counts()-');
7216    END IF;
7217 END refresh_counts;
7218 
7219 /* 5690748 - procedure for setting the receipt_at_risk_value column
7220    after mass updates (receipt applications)
7221 */
7222 PROCEDURE refresh_at_risk_value(
7223    p_customer_id_tab IN ar_bus_event_sub_pvt.generic_id_type,
7224    p_site_use_id_tab IN ar_bus_event_sub_pvt.generic_id_type,
7225    p_currency_tab    IN ar_bus_event_sub_pvt.currency_type,
7226    p_org_id_tab      IN ar_bus_event_sub_pvt.generic_id_type,
7227    p_cash_receipt_id IN ar_cash_receipts.cash_receipt_id%TYPE default null ) IS
7228 
7229    l_rows NUMBER;
7230 
7231 BEGIN
7232    IF pg_debug = 'Y'
7233    THEN
7234       debug ('ar_bus_event_sub_pvt.refresh_at_risk_value()+');
7235    END IF;
7236    /*bug 13858491 */
7237    If (p_cash_receipt_id is null) then
7238 
7239     FORALL i IN p_customer_id_tab.FIRST .. p_customer_id_tab.LAST
7240       UPDATE ar_trx_bal_summary main_sum
7241       SET receipts_at_risk_value =
7242         (SELECT SUM(DECODE(rap.applied_payment_schedule_id, -2, 0,
7243                        crh.amount))
7244          FROM   ar_cash_receipts_all cr,
7245                 ar_cash_receipt_history_all crh,
7246                 ar_receivable_applications_all rap
7247          WHERE  nvl(cr.confirmed_flag, 'Y') = 'Y'
7248          AND    cr.reversal_date IS NULL
7249          AND    cr.cash_receipt_id = crh.cash_receipt_id
7250          AND    crh.current_record_flag = 'Y'
7251          AND    crh.status NOT IN ('REVERSED',
7252                   DECODE(crh.factor_flag, 'Y', 'RISK_ELIMINATED',
7253                                           'N', 'CLEARED'))
7257          AND    nvl(cr.customer_site_use_id,-99) = decode(main_sum.site_use_id,-99,
7254          AND    cr.cash_receipt_id = rap.cash_receipt_id (+)
7255          AND    rap.applied_payment_schedule_id (+) = -2
7256          AND    cr.pay_from_customer = main_sum.cust_account_id
7258                                                           -99,
7259                                                           main_sum.site_use_id)
7260          AND    cr.org_id = main_sum.org_id
7261          AND    cr.currency_code = main_sum.currency
7262          GROUP BY cr.pay_from_customer, nvl(cr.customer_site_use_id,-99),
7263                   cr.currency_code, cr.org_id),
7264            /* WHO columns */
7265            last_update_date = sysdate,
7266            last_updated_by  = fnd_global.user_id,
7267            last_update_login= fnd_global.login_id
7268       WHERE cust_account_id = p_customer_id_tab(i)
7269       AND   site_use_id     = p_site_use_id_tab(i)
7270       AND   currency        = p_currency_tab(i)
7271       AND   NVL(org_id,'-99') = NVL(p_org_id_tab(i),-99);
7272 
7273      l_rows := SQL%ROWCOUNT;
7274 /*bug 13858491 Starts*/
7275    else
7276 
7277    IF pg_debug = 'Y'
7278    THEN
7279      debug ('ar_bus_event_sub_pvt.refresh_at_risk_value() Call from CE_AUTO_BANK_REC');
7280    END IF;
7281 
7282      UPDATE ar_trx_bal_summary main_sum
7283       SET receipts_at_risk_value =  receipts_at_risk_value -
7284         (SELECT SUM(DECODE(rap.applied_payment_schedule_id, -2, 0,
7285                        crh.amount))
7286          FROM   ar_cash_receipts_all cr,
7287                 ar_cash_receipt_history_all crh,
7288                 ar_receivable_applications_all rap
7289          WHERE  nvl(cr.confirmed_flag, 'Y') = 'Y'
7290          AND    cr.reversal_date IS NULL
7291          AND    cr.cash_receipt_id = crh.cash_receipt_id
7292          AND    crh.current_record_flag = 'Y'
7293          AND    crh.status IN ('REVERSED',
7294                   DECODE(crh.factor_flag, 'Y', 'RISK_ELIMINATED',
7295                                           'N', 'CLEARED'))
7296          AND    cr.cash_receipt_id = rap.cash_receipt_id (+)
7297          AND    rap.applied_payment_schedule_id (+) = -2
7298          AND    cr.pay_from_customer = main_sum.cust_account_id
7299          AND    cr.customer_site_use_id = decode(main_sum.site_use_id,-99,
7303          AND    cr.currency_code = main_sum.currency
7300                                                           cr.customer_site_use_id,
7301                                                           main_sum.site_use_id)
7302          AND    cr.org_id = main_sum.org_id
7304 	 AND    cr.cash_receipt_id = p_cash_receipt_id
7305          GROUP BY cr.pay_from_customer, cr.customer_site_use_id,
7306                   cr.currency_code, cr.org_id),
7307            /* WHO columns */
7308            last_update_date = sysdate,
7309            last_updated_by  = fnd_global.user_id,
7310            last_update_login= fnd_global.login_id
7311       WHERE cust_account_id = p_customer_id_tab(0)
7312       AND   site_use_id     = p_site_use_id_tab(0)
7313       AND   currency        = p_currency_tab(0)
7314       AND   NVL(org_id,'-99') = NVL(p_org_id_tab(0),-99);
7315 
7316       l_rows := SQL%ROWCOUNT;
7317 
7318     END if;
7319     /*bug 13858491 Ends*/
7320    IF pg_debug = 'Y'
7321    THEN
7322       debug ('  updated row(s) = ' || l_rows);
7323       debug ('ar_bus_event_sub_pvt.refresh_at_risk_value()-');
7324    END IF;
7325 END refresh_at_risk_value;
7326 
7327 END AR_BUS_EVENT_SUB_PVT; -- Package spec