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