DBA Data[Home] [Help]

PACKAGE BODY: APPS.AR_CMGT_DATA_POINTS_PKG

Source


4 pg_wf_debug VARCHAR2(1) := ar_cmgt_util.get_wf_debug_flag;
1 Package BODY AR_CMGT_DATA_POINTS_PKG AS
2 /* $Header: ARCMGDPB.pls 120.45.12020000.2 2012/07/09 22:03:12 rravikir ship $ */
3 
5 
6 /* 9560367 */
7 g_intended_limit_currency  ar_cmgt_credit_requests.limit_currency%type;
8 g_conversion_type          ar_cmgt_setup_options.default_exchange_rate_type%type;
9 
10 FUNCTION get_conversion_type( p_credit_request_id IN NUMBER)
11     return VARCHAR2 IS
12 
13     CURSOR getCreditRequestDetails (p_cr_req_id IN NUMBER)
14     IS
15         select credit_check_rule_id,
16         source_name
17         from   ar_cmgt_credit_requests
18         where  credit_request_id = p_cr_req_id;
19 
20 
21     CURSOR getRateTYpeFromCreditChkRule(p_cr_chk_rule_id IN NUMBER) IS
22         select conversion_type
23         from  oe_credit_check_rules
24         where credit_check_rule_id = p_cr_chk_rule_id;
25 
26     CURSOR getRateTypeFromSetup
27     IS
28         SELECT default_exchange_rate_type
29         FROM ar_cmgt_setup_options;
30 
31     l_rule_id               NUMBER;
32     l_source_name           VARCHAR2(50);
33     l_exchange_rate_type    VARCHAR2(50);
34 
35 begin
36 
37     IF (p_credit_request_id IS NOT NULL )
38     THEN
39 
40         OPEN getCreditRequestDetails(p_credit_request_id );
41 
42         FETCH getCreditRequestDetails  INTO l_rule_id, l_source_name;
43         IF NVL(l_source_name,'X') = 'OM' AND
44                 (l_rule_id IS NOT NULL) THEN
45 
46             OPEN getRateTYpeFromCreditChkRule(l_rule_id);
47                 FETCH getRateTYpeFromCreditChkRule INTO l_exchange_rate_type;
48             CLOSE getRateTYpeFromCreditChkRule;
52 
49         END IF;
50 
51         IF l_exchange_rate_type IS NULL  THEN
53             OPEN getRateTypeFromSetup;
54             FETCH getRateTypeFromSetup INTO l_exchange_rate_type;
55 
56             CLOSE getRateTypeFromSetup;
57         END IF;
58 
59     END IF;
60 
61     RETURN l_exchange_rate_type;
62 
63 END;
64 
65 PROCEDURE build_case_folder_details(
66         p_case_folder_id            IN      NUMBER,
67         p_data_point_id             IN      NUMBER,
68         p_data_point_value          IN      VARCHAR2 default NULL,
69         p_mode                      IN      VARCHAR2 default 'CREATE',
70         p_error_msg                 OUT nocopy     VARCHAR2,
71         p_resultout                 OUT nocopy     VARCHAR2) AS
72 
73 l_included_in_check_list                VARCHAR2(1) := 'N';
74 l_cnt                                   NUMBER;
75 l_errmsg                                VARCHAR2(4000);
76 l_resultout                             VARCHAR2(1);
77 BEGIN
78     IF pg_wf_debug = 'Y'
79     THEN
80     --   ar_cmgt_util.wf_debug(p_case_folder_id,
81     --          'ar_cmgt_data_points_pkg.build_case_folder_details()+');
82          ar_cmgt_util.wf_debug(p_case_folder_id,
83             'DP:' || p_data_point_id || ' = ' || p_data_point_value);
84     END IF;
85 
86     p_resultout := 0;
87 
88        -- this will generate case folder details of type 'CASE'
89        IF p_mode = 'CREATE'
90        THEN
91             AR_CMGT_CONTROLS.populate_case_folder_details(
92                 p_case_folder_id            =>  p_case_folder_id,
93                 p_data_point_id             =>  p_data_point_id,
94                 p_data_point_value          =>  p_data_point_value,
95                 p_included_in_check_list    =>  l_included_in_check_list,
96                 p_errmsg                    =>  l_errmsg,
97                 p_resultout                 =>  l_resultout);
98 
99             -- this will generate case folder details of type 'DATA'
100             IF g_data_case_folder_exists = 'Y'
101             THEN
102                 AR_CMGT_CONTROLS.UPDATE_CASE_FOLDER_DETAILS (
103                     p_case_folder_id        =>  g_data_case_folder_id,
104                     p_data_point_id         =>  p_data_point_id,
105                     p_data_point_value      =>  p_data_point_value,
106                     p_score                 =>  NULL,
107                     p_errmsg                =>  l_errmsg,
108                     p_resultout             =>  l_resultout);
109             ELSIF g_data_case_folder_exists = 'N'
110             THEN
111                 AR_CMGT_CONTROLS.populate_case_folder_details(
112                     p_case_folder_id            =>  g_data_case_folder_id,
113                     p_data_point_id             =>  p_data_point_id,
114                     p_data_point_value          =>  p_data_point_value,
115                     p_included_in_check_list    =>  l_included_in_check_list,
116                     p_errmsg                    =>  l_errmsg,
117                     p_resultout                 =>  l_resultout);
118             END IF;
119         ELSIF p_mode = 'REFRESH'
120         THEN
121                 AR_CMGT_CONTROLS.UPDATE_CASE_FOLDER_DETAILS (
122                     p_case_folder_id        =>  p_case_folder_id,
123                     p_data_point_id         =>  p_data_point_id,
124                     p_data_point_value      =>  p_data_point_value,
125                     p_score                 =>  NULL,
126                     p_errmsg                =>  l_errmsg,
127                     p_resultout             =>  l_resultout);
128         END IF;
129 
130 --   IF pg_wf_debug = 'Y'
131 --   THEN
132 --     ar_cmgt_util.wf_debug(p_case_folder_id,
133 --             'ar_cmgt_data_points_pkg.build_case_folder_details()-');
134 --   END IF;
135 
136     EXCEPTION
137         WHEN OTHERS THEN
138             p_resultout := 1;
139             p_error_msg := 'Unable to create/Update records in AR_CMGT_CF_DTLS for Data Point Id: '||p_data_point_id;
140             ar_cmgt_util.wf_debug(p_case_folder_id, p_error_msg);
141 END build_case_folder_details;
142 
143 PROCEDURE build_case_folder_adp_details(
144         p_case_folder_detail_id     IN          NUMBER,
145         p_case_folder_id            IN          NUMBER,
146         p_data_point_id             IN          NUMBER,
147         p_sequence_number           IN          NUMBER,
148         p_parent_data_point_id      IN          NUMBER,
149         p_parent_cf_detail_id       IN          NUMBER,
150         p_data_point_value          IN          VARCHAR2 default NULL,
151         p_mode                      IN          VARCHAR2 default 'CREATE',
152         x_error_msg                 OUT nocopy  VARCHAR2,
153         x_resultout                 OUT nocopy  VARCHAR2)
154 AS
155    l_included_in_check_list                VARCHAR2(1) := 'N';
156    l_cnt                                   NUMBER;
157    l_errmsg                                VARCHAR2(4000);
158    l_resultout                             VARCHAR2(1);
159 BEGIN
160     IF pg_wf_debug = 'Y'
161     THEN
162        ar_cmgt_util.wf_debug(p_case_folder_id,
163               'ar_cmgt_data_points_pkg.build_case_folder_adp_details()+');
164     -- ar_cmgt_util.wf_debug(p_case_folder_id,
165     --        p_data_point_id || '=' || p_data_point_value);
166     END IF;
167 
168    x_resultout := 0;
169 
170     -- this will generate case folder details of type 'CASE'
174                 p_case_folder_id            =>  p_case_folder_id,
171    IF p_mode = 'CREATE'
172    THEN
173       AR_CMGT_CONTROLS.populate_case_folder_details(
175                 p_data_point_id             =>  p_data_point_id,
176                 p_data_point_value          =>  p_data_point_value,
177                 p_included_in_check_list    =>  l_included_in_check_list,
178                 p_errmsg                    =>  x_error_msg,
179                 p_resultout                 =>  x_resultout);
180 
181             -- this will generate case folder details of type 'DATA'
182       IF g_data_case_folder_exists = 'Y'
183       THEN
184          AR_CMGT_CONTROLS.UPDATE_CASE_FOLDER_DETAILS (
185                     p_case_folder_id        =>  g_data_case_folder_id,
186                     p_data_point_id         =>  p_data_point_id,
187                     p_data_point_value      =>  p_data_point_value,
188                     p_score                 =>  NULL,
189                     p_errmsg                =>  x_error_msg,
190                     p_resultout             =>  x_resultout);
191       ELSIF g_data_case_folder_exists = 'N'
192       THEN
193          AR_CMGT_CONTROLS.populate_case_folder_details(
194                     p_case_folder_id            =>  g_data_case_folder_id,
195                     p_data_point_id             =>  p_data_point_id,
196                     p_data_point_value          =>  p_data_point_value,
197                     p_included_in_check_list    =>  l_included_in_check_list,
198                     p_errmsg                    =>  x_error_msg,
199                     p_resultout                 =>  x_resultout);
200       END IF;
201    ELSIF p_mode = 'REFRESH'
202    THEN
203       AR_CMGT_CONTROLS.UPDATE_CASE_FOLDER_DETAILS (
204                     p_case_folder_id        =>  p_case_folder_id,
205                     p_data_point_id         =>  p_data_point_id,
206                     p_data_point_value      =>  p_data_point_value,
207                     p_score                 =>  NULL,
208                     p_errmsg                =>  x_error_msg,
209                     p_resultout             =>  x_resultout);
210    END IF;
211 
212     IF pg_wf_debug = 'Y'
213     THEN
214        ar_cmgt_util.wf_debug(p_case_folder_id,
215               'ar_cmgt_data_points_pkg.build_case_folder_adp_details()-');
216     -- ar_cmgt_util.wf_debug(p_case_folder_id,
217     --        p_data_point_id || '=' || p_data_point_value);
218     END IF;
219 EXCEPTION
220    WHEN OTHERS THEN
221       x_resultout := 1;
222       x_error_msg := 'Unable to create/Update records in AR_CMGT_CF_DTLS for Data Point Id: '||p_data_point_id;
223       ar_cmgt_util.wf_debug(p_case_folder_id, x_error_msg);
224 END build_case_folder_adp_details;
225 
226 PROCEDURE GetDeductionDataPoints ( -- bug 3691676
227             p_credit_request_id         IN          NUMBER,
228             p_case_folder_id            IN          NUMBER,
229             p_period                    IN          NUMBER,
230             p_party_id                  IN          NUMBER,
231             p_cust_account_id           IN          NUMBER,
232             p_site_use_id               IN          NUMBER,
233             p_analysis_level            IN          VARCHAR2,
234             p_org_id                    IN          NUMBER,
235             p_mode                      IN          VARCHAR2 default 'CREATE',
236             p_limit_currency            IN          VARCHAR2,
237             p_exchange_rate_type        IN          VARCHAR2,
238             p_global_exposure_flag      IN          VARCHAR2,
239             p_error_msg                 OUT NOCOPY  VARCHAR2,
240             p_resultout                 OUT NOCOPY  VARCHAR2 ) IS
241 
242             l_deduction_amount          NUMBER;
243             l_deduction_count           NUMBER;
244 BEGIN
245     IF pg_wf_debug = 'Y'
246     THEN
247        ar_cmgt_util.wf_debug(p_case_folder_id,
248               'ar_cmgt_data_points_pkg.get_deduction_data_points()+');
249     END IF;
250         -- first get the deduction amount
251         p_resultout := 0;
252         IF p_analysis_level = 'P'
253         THEN
254             BEGIN
255                 SELECT sum(amount_settled), sum(ded_count)
256                 INTO   l_deduction_amount, l_deduction_count
257                 FROM (
258                     SELECT round(gl_currency_api.convert_amount(currency_code,
259                             p_limit_currency, sysdate,
260                             p_exchange_rate_type,
261                             sum(amount_settled)),2) amount_settled,
262                             count(*) ded_count
263                     FROM   ozf_claims_all
264                     WHERE  status_code = 'CLOSED'
265                     AND    claim_class = 'DEDUCTION'
266                     AND    settled_date  >= ADD_MONTHS(sysdate,(-p_period))
267                     AND    cust_account_id IN (
268                             select TO_NUMBER(cust_account_id)  -- Fix for Bug 12670874
269                             FROM   hz_cust_accounts
270                             WHERE  party_id in
271                                   ( SELECT child_id
272                                     from hz_hierarchy_nodes
273                                     where parent_object_type = 'ORGANIZATION'
274                                     and parent_table_name = 'HZ_PARTIES'
275                                     and child_object_type = 'ORGANIZATION'
276                                     and parent_id = p_party_id
277                                     and effective_start_date <= sysdate
278                                     and effective_end_date >= sysdate
279                                     and  hierarchy_type =
283                     select p_party_id from dual
280                                           FND_PROFILE.VALUE('AR_CMGT_HIERARCHY_TYPE')
281                                     and  g_source_name <> 'LNS'
282                                     UNION
284                                     UNION
285                     select hz_party_id
286                     from LNS_LOAN_PARTICIPANTS_V
287                     where loan_id = g_source_id
288                     and   participant_type_code = 'COBORROWER'
289                     and   g_source_name = 'LNS'
290                     and (end_date_active is null OR
291                           (sysdate between start_date_active and end_date_active)
292                           )
293                                         ))
294                     AND   currency_code IN ( SELECT CURRENCY FROM
295                                          ar_cmgt_curr_usage_gt
296                                          WHERE nvl(credit_request_id,p_credit_request_id)
297                                                            = p_credit_request_id)
298                     group by currency_code);
299             EXCEPTION
300                 WHEN NO_DATA_FOUND
301                 THEN
302                     l_deduction_amount := null;
303                     l_deduction_count := null;
304                 WHEN OTHERS THEN
305                     p_error_msg := 'Error While getting Deduction for Party, SqlError: '||sqlerrm;
306                     p_resultout := 1;
307                     ar_cmgt_util.wf_debug(p_case_folder_id, p_error_msg);
308                     return;
309             END;
310         ELSIF p_analysis_level = 'A'
311         THEN
312           BEGIN
313             SELECT sum(amount_settled), sum(ded_count)
314                 INTO   l_deduction_amount, l_deduction_count
315                 FROM (
316                     SELECT round(gl_currency_api.convert_amount(currency_code,
317                             p_limit_currency, sysdate,
318                             p_exchange_rate_type,
319                             sum(amount_settled)),2) amount_settled,
320                             count(*) ded_count
321                     FROM   ozf_claims_all
322                     WHERE  status_code = 'CLOSED'
323                     AND    claim_class = 'DEDUCTION'
324                     AND    cust_account_id = p_cust_account_id
325                     AND    org_id = decode(p_global_exposure_flag,'Y', org_id, 'N',
326                                     decode(p_org_id,null, org_id, p_org_id), null,
327                                     decode(p_org_id,null, org_id, p_org_id))
328                     AND    currency_code IN ( SELECT CURRENCY FROM
329                                          ar_cmgt_curr_usage_gt
330                                          WHERE nvl(credit_request_id,p_credit_request_id)
331                                                            = p_credit_request_id)
332                     AND    settled_date  >= ADD_MONTHS(sysdate,(-p_period))
333                     group by currency_code );
334           EXCEPTION
335                 WHEN NO_DATA_FOUND
336                 THEN
337                     l_deduction_amount := null;
338                     l_deduction_count := null;
339                 WHEN OTHERS THEN
340                     p_error_msg := 'Error While getting Deduction for Account, SqlError: '||sqlerrm;
341                     p_resultout := 1;
342                     ar_cmgt_util.wf_debug(p_case_folder_id, p_error_msg);
343                     return;
344           END;
345         ELSIF p_analysis_level = 'S'
346         THEN
347             BEGIN
348                 SELECT sum(amount_settled), sum(ded_count)
349                 INTO   l_deduction_amount, l_deduction_count
350                 FROM (
351                     SELECT round(gl_currency_api.convert_amount(currency_code,
352                             p_limit_currency, sysdate,
353                             p_exchange_rate_type,
354                             sum(amount_settled)),2) amount_settled,
355                             count(*) ded_count
356                     FROM   ozf_claims_all
357                     WHERE  status_code = 'CLOSED'
358                     AND    claim_class = 'DEDUCTION'
359                     AND    settled_date  >= ADD_MONTHS(sysdate,(-p_period))
360                     AND    cust_account_id = p_cust_account_id
361                     AND    cust_billto_acct_site_id = p_site_use_id
362                     AND    currency_code IN ( SELECT CURRENCY FROM
363                                          ar_cmgt_curr_usage_gt
364                                          WHERE nvl(credit_request_id,p_credit_request_id)
365                                                            = p_credit_request_id)
366                     group by currency_code );
367             EXCEPTION
368                 WHEN NO_DATA_FOUND
369                 THEN
370                     l_deduction_amount := null;
371                     l_deduction_count := null;
372                 WHEN OTHERS THEN
373                     p_error_msg := 'Error While getting Deduction for Site, SqlError: '||sqlerrm;
374                     p_resultout := 1;
375                     ar_cmgt_util.wf_debug(p_case_folder_id, p_error_msg);
376                     return;
377             END;
378         END IF;
379         -- Now call build case folder to insert into case folder details
380         build_case_folder_details(
381             p_case_folder_id        => p_case_folder_id,
382             p_data_point_id         => 211, -- deduction amount
383             p_data_point_value      => fnd_number.number_to_canonical(l_deduction_amount),
384             p_mode                  => p_mode,
385             p_error_msg             => p_error_msg,
386             p_resultout             => p_resultout);
387         IF p_resultout <> 0
388         THEN
389             return;
390         END IF;
394             p_data_point_value      => l_deduction_count,
391         build_case_folder_details(
392             p_case_folder_id        => p_case_folder_id,
393             p_data_point_id         => 212, -- deduction count
395             p_mode                  => p_mode,
396             p_error_msg             => p_error_msg,
397             p_resultout             => p_resultout);
398         IF p_resultout <> 0
399         THEN
400             return;
401         END IF;
402     IF pg_wf_debug = 'Y'
403     THEN
404        ar_cmgt_util.wf_debug(p_case_folder_id,
405               'ar_cmgt_data_points_pkg.get_deduction_data_points()-');
406     END IF;
407 
408 END;
409 
410 
411 PROCEDURE GetManualDataPoints(
412             p_credit_request_id         IN          NUMBER,
413             p_case_folder_id            IN          NUMBER,
414             p_check_list_id             IN          NUMBER,
415             p_mode                      IN          VARCHAR2 default 'CREATE',
416             x_error_msg                 OUT NOCOPY  VARCHAR2,
417             x_resultout                 OUT NOCOPY  VARCHAR2) IS
418 
419 
420 BEGIN
421     IF pg_wf_debug = 'Y'
422     THEN
423        ar_cmgt_util.wf_debug(p_case_folder_id,
424               'ar_cmgt_data_points_pkg.getmanualdatapoints()+');
425     END IF;
426 
427    x_resultout := 0;
428    ocm_add_data_points.getadditionalDataPoints (
429     p_credit_request_id   =>  p_credit_request_id,
430     p_case_folder_id    =>  p_case_folder_id,
431     p_mode          =>  p_mode,
432     p_error_msg       =>  x_error_msg,
433     p_resultout       =>  x_resultout );
434 
435     IF pg_wf_debug = 'Y'
436     THEN
437        ar_cmgt_util.wf_debug(p_case_folder_id,
438               'ar_cmgt_data_points_pkg.getmanualdatapoints()-');
439     END IF;
440 END GetManualDataPoints;
441 
442 
443 PROCEDURE GetAnalyticalData (
444             p_credit_request_id        IN   NUMBER,
445             p_party_id                 IN   NUMBER,
446             p_cust_account_id          IN   NUMBER,
447             p_site_use_id              IN   NUMBER,
448             p_case_folder_id           IN   NUMBER,
449             p_analysis_level           IN   VARCHAR2,
450             p_org_id                   IN   NUMBER,
451             p_period                   IN   NUMBER,
452             p_global_exposure_flag     IN   VARCHAR2,
453             p_limit_currency           IN   VARCHAR2,
454             p_exchange_rate_type       IN   VARCHAR2,
455             p_mode                     IN   VARCHAR2 default 'CREATE',
456             p_errormsg                 out nocopy  VARCHAR2,
457             p_resultout                out nocopy  VARCHAR2) IS
458 
459 CURSOR c_currency IS
460     SELECT currency from ar_cmgt_curr_usage_gt;
461 
462 l_largest_inv_date                      ar_trx_summary.largest_inv_date%type;
463 l_largest_inv_amount                    ar_trx_summary.largest_inv_amount%type;
464 l_final_inv_amount                      ar_trx_summary.largest_inv_amount%type;
465 l_final_inv_date                        ar_trx_summary.largest_inv_date%type;
466 l_high_watermark_date                   ar_trx_summary.op_bal_high_watermark_date%type;
467 l_high_watermark                        ar_trx_summary.op_bal_high_watermark%type;
468 l_final_high_watermark                  ar_trx_summary.op_bal_high_watermark%type;
469 l_final_high_watermark_date             ar_trx_summary.as_of_date%type;
470 l_last_payment_amount                   ar_trx_bal_summary.last_payment_amount%type;
471 l_last_payment_date                     ar_trx_bal_summary.last_payment_date%type;
472 l_last_payment_number                   ar_trx_bal_summary.last_payment_number%type;
473 l_last_payment_currency                 ar_trx_bal_summary.currency%type;
474 l_last_payment_amount_conv              ar_trx_bal_summary.last_payment_amount%type;
475 
476 l_result          VARCHAR2(150); -- 6513911
477 
478 l_party_sql       VARCHAR2(4000) :='select LARGEST_INV_DATE,
479                                            largest_inv_amount
480                                      from ( select as_of_date LARGEST_INV_DATE,
481                                             gl_currency_api.convert_amount(currency,
482                                             :1,sysdate,
483                                             :2,
484                                             largest_inv_amount)largest_inv_amount,
485                                      RANK() OVER (PARTITION BY currency
486                                      ORDER BY largest_inv_amount desc,
487                                      largest_inv_cust_trx_id desc) rank_amount
488                                      FROM AR_TRX_SUMMARY
489                                      where cust_account_id in (
490                                                select to_number(cust_account_id)
491                                                 FROM   hz_cust_accounts
492                                                 WHERE  party_id in
493                                                 ( SELECT child_id
494                                                   from hz_hierarchy_nodes
495                                                   where parent_object_type = ''ORGANIZATION''
496                                                   and parent_table_name = ''HZ_PARTIES''
497                                                   and child_object_type = ''ORGANIZATION''
498                                                   and parent_id = :3
499                                                   and effective_start_date <= sysdate
500                                                   and effective_end_date >= sysdate
504                                                 union select :5 from dual
501                                                   and  hierarchy_type =
502                                                     FND_PROFILE.VALUE(''AR_CMGT_HIERARCHY_TYPE'')
503                                                    and  :4 <> ''LNS''
505                                                 union
506                                                 select hz_party_id
507                         from LNS_LOAN_PARTICIPANTS_V
508                         where loan_id = :6
509                         and   participant_type_code = ''COBORROWER''
510                         and   :7 = ''LNS''
511                         and (end_date_active is null OR
512                               (sysdate between start_date_active and end_date_active)
513                             )
514                                           ))
515                                      and currency = :8
516                                      and largest_inv_cust_trx_id is not null
517                                      and    as_of_date  >= ADD_MONTHS(sysdate,(-:9)) )
518                                      Where rank_amount = 1 ';
519 
520 l_account_sql       VARCHAR2(4000) :='select LARGEST_INV_DATE,
521                                              largest_inv_amount
522                                      from ( select as_of_date LARGEST_INV_DATE,
523                                             gl_currency_api.convert_amount(currency,
524                                             :1,sysdate,
525                                             :2,
526                                             largest_inv_amount)largest_inv_amount,
527                                      RANK() OVER (PARTITION BY cust_account_id,currency
528                                      ORDER BY largest_inv_amount desc,
529                                      largest_inv_cust_trx_id desc) rank_amount
530                                      FROM AR_TRX_SUMMARY
531                                      where cust_account_id = :3
532                                      and   org_id = decode(:4,''Y'', org_id, ''N'',
533                                                     decode(:5,null, org_id, :6), null,
534                                                     decode(:7,null, org_id, :8))
535                                      and currency = :9
536                                      and largest_inv_cust_trx_id is not null
537                                      and    as_of_date  >= ADD_MONTHS(sysdate,(-:10)) )
538                                      Where rank_amount = 1';
539 
540 l_site_sql       VARCHAR2(4000) :='select LARGEST_INV_DATE,
541                                         largest_inv_amount
542                                      from ( select as_of_date LARGEST_INV_DATE,
543                                             gl_currency_api.convert_amount(currency,
544                                             :1,sysdate,
545                                             :2,
546                                             largest_inv_amount)largest_inv_amount,
547                                      RANK() OVER (PARTITION BY cust_account_id, site_use_id,currency
548                                      ORDER BY largest_inv_amount desc,
549                                      largest_inv_cust_trx_id desc) rank_amount
550                                      FROM AR_TRX_SUMMARY
551                                      where cust_account_id = :3
552                                      and   site_use_id = :4
553                                      and currency = :5
554                                      and largest_inv_cust_trx_id is not null
555                                      and    as_of_date  >= ADD_MONTHS(sysdate,(-:6))  )
556                                      Where rank_amount = 1 ';
557 
558 /*  Not using Analytical function to derive high credit amount as
559     part of bug fix 3557539
560 l_high_credit_party_sql  VARCHAR2(4000) :='select op_bal_high_watermark_date,
561                                                   op_bal_high_watermark
562                                      from ( select as_of_date op_bal_high_watermark_date,
563                                           gl_currency_api.convert_amount(currency,
564                                             :1,sysdate,
565                                             :2,
566                                             op_bal_high_watermark)op_bal_high_watermark,
567                                      RANK() OVER (PARTITION BY currency
568                                      ORDER BY op_bal_high_watermark desc,
569                                      largest_inv_cust_trx_id desc) high_amount
570                                      FROM AR_TRX_SUMMARY
571                                      where cust_account_id in (
572                                                select to_number(cust_account_id)
573                                                 FROM   hz_cust_accounts
574                                                 WHERE  party_id in
575                                                 ( SELECT child_id
576                                                   from hz_hierarchy_nodes
577                                                   where parent_object_type = ''ORGANIZATION''
578                                                   and parent_table_name = ''HZ_PARTIES''
579                                                   and child_object_type = ''ORGANIZATION''
580                                                   and parent_id = :3
581                                                   and effective_start_date <= sysdate
582                                                   and effective_end_date >= sysdate
583                                                   and  hierarchy_type =
584                                                     FND_PROFILE.VALUE(''AR_CMGT_HIERARCHY_TYPE'')
585                                                 union select :4 from dual
586                                           ))
587                                      and currency = :5
588                                      and largest_inv_cust_trx_id is not null
592 l_high_credit_account_sql  VARCHAR2(4000) :='select op_bal_high_watermark_date,
589                                      and    as_of_date  >= ADD_MONTHS(sysdate,(-:6)) )
590                                      Where  high_amount = 1';
591 
593                                                     op_bal_high_watermark
594                                      from ( select as_of_date op_bal_high_watermark_date,
595                                             gl_currency_api.convert_amount(currency,
596                                             :1,sysdate,
597                                             :2,
598                                             op_bal_high_watermark)op_bal_high_watermark,
599                                      RANK() OVER (PARTITION BY cust_account_id,currency,org_id
600                                      ORDER BY op_bal_high_watermark desc,
601                                      largest_inv_cust_trx_id desc) high_amount
602                                      FROM AR_TRX_SUMMARY
603                                      where cust_account_id = :3
604                                      and   org_id = decode(:4,''Y'', org_id, ''N'',
605                                                     decode(:5,null, org_id, :6), null,
606                                                     decode(:7,null, org_id, :8))
607                                      and currency = :9
608                                      and largest_inv_cust_trx_id is not null
609                                      and    as_of_date  >= ADD_MONTHS(sysdate,(-:10)) )
610                                      Where high_amount = 1';
611 
612 l_high_credit_site_sql     VARCHAR2(4000) :='select op_bal_high_watermark_date,
613                                                     op_bal_high_watermark
614                                      from ( select as_of_date op_bal_high_watermark_date,
615                                           gl_currency_api.convert_amount(currency,
616                                             :1,sysdate,
617                                             :2,
618                                             op_bal_high_watermark)op_bal_high_watermark,
619                                      RANK() OVER (PARTITION BY cust_account_id, site_use_id,currency
620                                      ORDER BY op_bal_high_watermark desc,
621                                      largest_inv_cust_trx_id desc) high_amount
622                                      FROM AR_TRX_SUMMARY
623                                      where cust_account_id = :3
624                                      and   site_use_id = :4
625                                      and currency = :5
626                                      and largest_inv_cust_trx_id is not null
627                                      and    as_of_date  >= ADD_MONTHS(sysdate,(-:6))  )
628                                      Where  high_amount = 1';
629 
630 ************************************************************************************/
631 
632 BEGIN
633     IF pg_wf_debug = 'Y'
634     THEN
635        ar_cmgt_util.wf_debug(p_case_folder_id,
636               'ar_cmgt_data_points_pkg.getanalyticaldata()+');
637     END IF;
638 
639        p_resultout := 0;
640        -- get largest in amount
641        FOR c_currency_rec IN c_currency
642        LOOP
643         BEGIN
644            IF p_analysis_level = 'P'
645            THEN
646              EXECUTE IMMEDIATE l_party_sql INTO
647                      l_largest_inv_date,
648                      l_largest_inv_amount
649              USING p_limit_currency,p_exchange_rate_type,
650                    p_party_id, g_source_name, p_party_id, g_source_id,
651            g_source_name, c_currency_rec.currency, p_period;
652 
653            ELSIF p_analysis_level = 'A'
654            THEN
655                 EXECUTE IMMEDIATE l_account_sql INTO
656                         l_largest_inv_date,
657                         l_largest_inv_amount
658                 USING p_limit_currency,p_exchange_rate_type,
659                       p_cust_account_id,p_global_exposure_flag,p_org_id,
660                       p_org_id, p_org_id, p_org_id,
661                       c_currency_rec.currency, p_period;
662            ELSIF p_analysis_level = 'S'
663            THEN
664                 EXECUTE IMMEDIATE l_site_sql INTO
665                         l_largest_inv_date,
666                         l_largest_inv_amount
667                 USING p_limit_currency,p_exchange_rate_type,
668                       p_cust_account_id,p_site_use_id,
669                       c_currency_rec.currency, p_period;
670            END IF;
671         EXCEPTION
672             WHEN NO_DATA_FOUND THEN
673                 l_largest_inv_date := null;
674                 l_largest_inv_amount := null;
675 
676         END;
677            IF l_largest_inv_date IS NOT NULL AND l_largest_inv_amount IS NOT NULL
678              THEN
679                     IF nvl(l_final_inv_amount,0) <  l_largest_inv_amount
680                     THEN
681                         l_final_inv_amount := l_largest_inv_amount;
682                         l_final_inv_date := l_largest_inv_date;
683                     END IF;
684              END IF;
685 
686        END LOOP;
687 
688        /*-- repaet the same process for High water mark
689        FOR c_currency_rec IN c_currency
690        LOOP
691         BEGIN
692            IF p_analysis_level = 'P'
693            THEN
694 
695              EXECUTE IMMEDIATE l_high_credit_party_sql INTO
696                      l_high_watermark_date,
697                      l_high_watermark
698              USING p_limit_currency,p_exchange_rate_type,
699                    p_party_id, p_party_id,c_currency_rec.currency, p_period;
700 
701            ELSIF p_analysis_level = 'A'
702            THEN
703                 EXECUTE IMMEDIATE l_high_credit_account_sql INTO
707                       p_cust_account_id,p_global_exposure_flag,p_org_id,
704                         l_high_watermark_date,
705                         l_high_watermark
706                 USING p_limit_currency,p_exchange_rate_type,
708                       p_org_id, p_org_id, p_org_id,
709                       c_currency_rec.currency, p_period;
710 
711            ELSIF p_analysis_level = 'S'
712            THEN
713                 EXECUTE IMMEDIATE l_high_credit_site_sql INTO
714                         l_high_watermark_date,
715                         l_high_watermark
716                 USING p_limit_currency,p_exchange_rate_type,
717                       p_cust_account_id,p_site_use_id,
718                       c_currency_rec.currency, p_period;
719            END IF;
720         EXCEPTION
721             WHEN NO_DATA_FOUND THEN
722                 l_high_watermark_date := null;
723                 l_high_watermark := null;
724 
725         END;
726            IF l_high_watermark_date IS NOT NULL AND l_high_watermark IS NOT NULL
727              THEN
728                     IF l_final_high_watermark <  l_high_watermark
729                     THEN
730                         l_final_high_watermark  := l_high_watermark;
731                         l_final_high_watermark_date := l_high_watermark_date;
732                     END IF;
733              END IF;
734 
735        END LOOP;  */
736        -- Get the High Watermark and date (Bug fix 3557539)
737        -- Not using the above analytical select statement
738        IF p_analysis_level = 'P'
739        THEN
740             BEGIN
741 
742                 /* 6513911 - revised sql to not require all currencies
743                      This code now selects the converted amount and date
744                      (max) at one time, then substr them out to individual
745                      fields for the datapoint(s).  Incidentally, the original
746                      logic returned the raw amount (in AR_TRX_SUMMARY currency)
747                      rather than the converted amount -- I considered this a bug
748                      and now return the converted amount (to CF currency) */
749                 SELECT max(ltrim(
750                     to_char( gl_currency_api.convert_amount(currency,
751                                             p_limit_currency,sysdate,
752                                             p_exchange_rate_type,
753                                             NVL(op_bal_high_watermark,0)),
754                                             '0999999999999999999D00')) || '~' ||
755                                  to_char(as_of_date, 'YYYYMMDD'))
756                 INTO   l_result
757                 FROM   ar_trx_summary
758                 WHERE  op_bal_high_watermark IS NOT NULL
759                 AND    as_of_date  >= ADD_MONTHS(sysdate,(-p_period))
760                 AND    cust_account_id IN (
761                             SELECT TO_NUMBER(cust_account_id)  -- Fix for Bug 12670874
762                             FROM   hz_cust_accounts
763                             WHERE  party_id in
764                                   ( SELECT child_id
765                                     FROM   hz_hierarchy_nodes
766                                     WHERE  parent_object_type = 'ORGANIZATION'
767                                     AND parent_table_name = 'HZ_PARTIES'
768                                     AND child_object_type = 'ORGANIZATION'
769                                     AND parent_id = p_party_id
770                                     AND effective_start_date <= sysdate
771                                     AND effective_end_date >= sysdate
772                                     AND hierarchy_type =
773                                            FND_PROFILE.VALUE('AR_CMGT_HIERARCHY_TYPE')
774                                     AND  g_source_name <> 'LNS'
775                                     UNION
776                                     SELECT p_party_id FROM DUAL
777                                     UNION
778                                     SELECT hz_party_id
779                                     FROM   LNS_LOAN_PARTICIPANTS_V
780                                     WHERE  loan_id = g_source_id
781                                     AND    participant_type_code = 'COBORROWER'
782                                     AND    g_source_name = 'LNS'
783                                     AND    (end_date_active is null OR
784                                          (sysdate between start_date_active
785                                                        and end_date_active))))
786                 AND   currency IN ( SELECT CURRENCY
787                                     FROM   ar_cmgt_curr_usage_gt
788                                     WHERE nvl(credit_request_id,p_credit_request_id)
789                                                         = p_credit_request_id);
790 
791                 /* Now extract results, '~' is separator */
792                 l_final_high_watermark := to_number(substr(l_result,0,instr(l_result,'~')-1));
793                 l_final_high_watermark_date := to_date(substr(l_result,instr(l_result,'~')+1),'YYYYMMDD');
794 
795                 EXCEPTION
796                     WHEN NO_DATA_FOUND THEN
797                         l_final_high_watermark := NULL;
798                         l_final_high_watermark_date := NULL;
799                     when others THEN
800                       p_errormsg := 'Error While getting High Credit Amount for Party, SqlError: <'||l_result||'>'|| sqlerrm;
801                       p_resultout := 1;
802                       ar_cmgt_util.wf_debug(p_case_folder_id, p_errormsg);
803                       return;
804 
805             END;
806        ELSE -- p_analysis_level A or S (both)
807             BEGIN
808                 SELECT max(ltrim(
809                     to_char( gl_currency_api.convert_amount(currency,
810                                             p_limit_currency,sysdate,
814                                  to_char(as_of_date, 'YYYYMMDD'))
811                                             p_exchange_rate_type,
812                                             NVL(op_bal_high_watermark,0)),
813                                             '0999999999999999999D00')) || '~' ||
815                 INTO   l_result
816                 FROM   ar_trx_summary
817                 WHERE  op_bal_high_watermark IS NOT NULL
818                 AND    as_of_date  >= ADD_MONTHS(sysdate,(-p_period))
819                 AND    cust_account_id = p_cust_account_id
820                 AND    site_use_id = DECODE(p_analysis_level, 'S', p_site_use_id,
821                                               site_use_id)
822                 AND    org_id = decode(p_global_exposure_flag,'Y', org_id, 'N',
823                                   decode(p_org_id,null, org_id, p_org_id), null,
824                                   decode(p_org_id,null, org_id, p_org_id))
825                 AND    currency IN ( SELECT CURRENCY
826                                      FROM   ar_cmgt_curr_usage_gt
827                                      WHERE nvl(credit_request_id,p_credit_request_id)
828                                                         = p_credit_request_id);
829 
830                 /* Now extract results, '~' is separator */
831                 l_final_high_watermark := to_number(substr(l_result,0,instr(l_result,'~')-1));
832                 l_final_high_watermark_date := to_date(substr(l_result,instr(l_result,'~')+1),'YYYYMMDD');
833 
834             EXCEPTION
835                 WHEN no_data_found then
836                     l_final_high_watermark := NULL;
837                     l_final_high_watermark_date := NULL;
838                 when others then
839                     p_errormsg := 'Error While getting High Credit Amount for Account/Site, SqlError: <'||l_result||'>'|| sqlerrm;
840                     p_resultout := 1;
841                     ar_cmgt_util.wf_debug(p_case_folder_id, p_errormsg);
842                     return;
843 
844 
845             END;
846        END IF;
847 
848        build_case_folder_details(
849                 p_case_folder_id            =>  p_case_folder_id,
850                 p_data_point_id             =>  19,
851                 p_data_point_value          =>  fnd_number.number_to_canonical(l_final_inv_amount),
852                 p_mode                      => p_mode,
853                 p_error_msg                 =>  p_errormsg ,
854                 p_resultout                 =>  p_resultout);
855        build_case_folder_details(
856                 p_case_folder_id            =>  p_case_folder_id,
857                 p_data_point_id             =>  20,
858                 p_data_point_value          =>  l_final_inv_date,
859                 p_mode                      =>  p_mode,
860                 p_error_msg                 =>  p_errormsg ,
861                 p_resultout                 =>  p_resultout);
862         build_case_folder_details(
863                 p_case_folder_id            =>  p_case_folder_id,
864                 p_data_point_id             =>  6,
865                 p_data_point_value          =>  fnd_number.number_to_canonical(l_final_high_watermark),
866                 p_mode                      => p_mode,
867                 p_error_msg                 =>  p_errormsg ,
868                 p_resultout                 =>  p_resultout);
869        build_case_folder_details(
870                 p_case_folder_id            =>  p_case_folder_id,
871                 p_data_point_id             =>  7,
872                 p_data_point_value          =>  l_final_high_watermark_date,
873                 p_mode                      =>  p_mode,
874                 p_error_msg                 =>  p_errormsg ,
875                 p_resultout                 =>  p_resultout);
876       -- Get the last payment Info.
877       BEGIN
878            IF p_analysis_level = 'P'
879            THEN
880                 SELECT last_payment_amount, last_payment_date,
881                        last_payment_number, currency
882                 INTO   l_last_payment_amount, l_last_payment_date,
883                        l_last_payment_number, l_last_payment_currency
884                 FROM AR_TRX_BAL_SUMMARY
885                 WHERE cust_account_id  in (select TO_NUMBER(cust_account_id)  -- Fix for Bug 12670874
886                                     FROM   hz_cust_accounts
887                                     WHERE  party_id in
888                                     ( SELECT child_id
889                                         from hz_hierarchy_nodes
890                                         where parent_object_type = 'ORGANIZATION'
891                                         and parent_table_name = 'HZ_PARTIES'
892                                         and child_object_type = 'ORGANIZATION'
893                                         and parent_id = p_party_id
894                                         and effective_start_date <= sysdate
895                                         and effective_end_date >= sysdate
896                                         and  hierarchy_type = FND_PROFILE.VALUE('AR_CMGT_HIERARCHY_TYPE')
897                                         and  g_source_name <> 'LNS'
898                                     union select p_party_id from dual
899                                     UNION
900                     select hz_party_id
901                     from LNS_LOAN_PARTICIPANTS_V
902                     where loan_id = g_source_id
903                     and   participant_type_code = 'COBORROWER'
904                     and   g_source_name = 'LNS'
905                     and (end_date_active is null OR
906                           (sysdate between start_date_active and end_date_active)
907                           )
908                                ))
909                 and    CURRENCY   IN   ( SELECT CURRENCY FROM
910                                        ar_cmgt_curr_usage_gt
911                                        WHERE nvl(credit_request_id,p_credit_request_id)
915                 and    last_payment_date = ( select max(last_payment_date) from
912                                                 = p_credit_request_id)
913                 and    last_payment_number IS NOT NULL
914                 and    last_payment_date IS NOT NULL
916                                              ar_trx_bal_summary
917                                              where  cust_account_id  in
918                                                     (select TO_NUMBER(cust_account_id)  -- Fix for Bug 12670874
919                                                      FROM   hz_cust_accounts
920                                                      WHERE  party_id in
921                                                         ( SELECT child_id
922                                                           from hz_hierarchy_nodes
923                                                           where parent_object_type = 'ORGANIZATION'
924                                                           and parent_table_name = 'HZ_PARTIES'
925                                                           and child_object_type = 'ORGANIZATION'
926                                                           and parent_id = p_party_id
927                                                           and effective_start_date <= sysdate
928                                                           and effective_end_date >= sysdate
929                                                           and  hierarchy_type = FND_PROFILE.VALUE('AR_CMGT_HIERARCHY_TYPE')
930                                                           and  g_source_name <> 'LNS'
931                                                      union select p_party_id from dual
932                            UNION
933                             select hz_party_id
934                             from LNS_LOAN_PARTICIPANTS_V
935                             where loan_id = g_source_id
936                             and   participant_type_code = 'COBORROWER'
937                             and   g_source_name = 'LNS'
938                             and (end_date_active is null OR
939                                   (sysdate between start_date_active and end_date_active)
940                                 )
941                            ))
942                                               and    CURRENCY   IN   ( SELECT CURRENCY FROM
943                                                                 ar_cmgt_curr_usage_gt
944                                                                 WHERE nvl(credit_request_id,p_credit_request_id) =
945                                                                 p_credit_request_id)
946                                               and    last_payment_date is not null
947                                               and    last_payment_number is not null)
948                 and   rownum = 1;
949            ELSIF p_analysis_level = 'A'
950            THEN
951                 SELECT last_payment_amount, last_payment_date,
952                        last_payment_number, currency
953                 INTO   l_last_payment_amount, l_last_payment_date,
954                        l_last_payment_number, l_last_payment_currency
955                 FROM AR_TRX_BAL_SUMMARY
956                 where cust_account_id = p_cust_account_id
957                 and   last_payment_date IS NOT NULL
958                 and   last_payment_number IS NOT NULL
959                 and   org_id = decode(p_global_exposure_flag,'Y', org_id, 'N',
960                                decode(p_org_id,null, p_org_id, p_org_id), null,
961                                decode(p_org_id,null, org_id, p_org_id))
962                 and   currency in  ( SELECT CURRENCY FROM
963                                        ar_cmgt_curr_usage_gt
964                                        WHERE nvl(credit_request_id,p_credit_request_id) = p_credit_request_id)
965                 and   last_payment_date = (  select max(last_payment_date) from
966                                              ar_trx_bal_summary
967                                              where  cust_account_id = p_cust_account_id
968                                              and    last_payment_date IS NOT NULL
969                                              and    last_payment_number IS NOT NULL
970                                              and    currency in ( SELECT CURRENCY FROM
971                                                         ar_cmgt_curr_usage_gt
972                                                         WHERE nvl(credit_request_id,p_credit_request_id) =
973                                                           p_credit_request_id))
974                 and    rownum = 1;
975            ELSIF p_analysis_level = 'S'
976            THEN
977                 SELECT last_payment_amount, last_payment_date,
978                        last_payment_number, currency
979                 INTO   l_last_payment_amount, l_last_payment_date,
980                        l_last_payment_number, l_last_payment_currency
981                 FROM AR_TRX_BAL_SUMMARY
982                 where cust_account_id = p_cust_account_id
983                 and   site_use_id     = p_site_use_id
984                 and   last_payment_date IS NOT NULL
985                 and   last_payment_number IS NOT NULL
986                 and   currency in  ( SELECT CURRENCY FROM
987                                        ar_cmgt_curr_usage_gt
988                                        WHERE nvl(credit_request_id,p_credit_request_id) = p_credit_request_id)
989                 and   last_payment_date = (  select max(last_payment_date) from
990                                              ar_trx_bal_summary
991                                              where  cust_account_id = p_cust_account_id
992                                              and    site_use_id     = p_site_use_id
993                                              and    last_payment_date IS NOT NULL
994                                              and    last_payment_number IS NOT NULL
998                                                           p_credit_request_id))
995                                              and    currency in ( SELECT CURRENCY FROM
996                                                         ar_cmgt_curr_usage_gt
997                                                         WHERE nvl(credit_request_id,p_credit_request_id) =
999                 and   rownum = 1;
1000            END IF;
1001 
1002            EXCEPTION
1003                 WHEN no_data_found then
1004                      l_last_payment_number := null;
1005                      l_last_payment_date := null;
1006                      l_last_payment_amount := null;
1007                 when others then
1008                       p_errormsg := 'Error While getting Last payment info., SqlError: '||sqlerrm;
1009                       p_resultout := 1;
1010                       ar_cmgt_util.wf_debug(p_case_folder_id, p_errormsg);
1011                       return;
1012       END;
1013       BEGIN
1014       -- if everything is ok then create record in case folder tables
1015            -- convert last payment amount to  limit currency amount
1016           IF l_last_payment_currency IS NOT NULL THEN
1017              l_last_payment_amount_conv := gl_currency_api.convert_amount
1018                             (l_last_payment_currency,
1019                              p_limit_currency,sysdate,
1020                              p_exchange_rate_type,
1021                              l_last_payment_amount);
1022           END IF;
1023            build_case_folder_details(
1024                 p_case_folder_id            =>  p_case_folder_id,
1025                 p_data_point_id             =>  11,
1026                 p_data_point_value          =>  fnd_number.number_to_canonical(l_last_payment_amount_conv),
1027                 p_mode                      =>  p_mode,
1028                 p_error_msg                 =>  p_errormsg ,
1029                 p_resultout                 =>  p_resultout);
1030            build_case_folder_details(
1031                 p_case_folder_id            =>  p_case_folder_id,
1032                 p_data_point_id             =>  207,
1033                 p_data_point_value          =>  fnd_number.number_to_canonical(l_last_payment_amount),
1034                 p_mode                      =>  p_mode,
1035                 p_error_msg                 =>  p_errormsg ,
1036                 p_resultout                 =>  p_resultout);
1037            build_case_folder_details(
1038                 p_case_folder_id            =>  p_case_folder_id,
1039                 p_data_point_id             =>  12,
1040                 p_data_point_value          =>  l_last_payment_date,
1041                 p_mode                      =>  p_mode,
1042                 p_error_msg                 =>  p_errormsg ,
1043                 p_resultout                 =>  p_resultout);
1044            build_case_folder_details(
1045                 p_case_folder_id            =>  p_case_folder_id,
1046                 p_data_point_id             =>  205,
1047                 p_data_point_value          =>  l_last_payment_number,
1048                 p_mode                      =>  p_mode,
1049                 p_error_msg                 =>  p_errormsg ,
1050                 p_resultout                 =>  p_resultout);
1051            build_case_folder_details(
1052                 p_case_folder_id            =>  p_case_folder_id,
1053                 p_data_point_id             =>  206,
1054                 p_data_point_value          =>  l_last_payment_currency,
1055                 p_mode                      =>  p_mode,
1056                 p_error_msg                 =>  p_errormsg ,
1057                 p_resultout                 =>  p_resultout);
1058       EXCEPTION
1059             when others then
1060                 p_errormsg := 'Error While inserting Last payment info., SqlError: '||sqlerrm;
1061                 p_resultout := 1;
1062                 ar_cmgt_util.wf_debug(p_case_folder_id, p_errormsg);
1063                 return;
1064       END;
1065 
1066     IF pg_wf_debug = 'Y'
1067     THEN
1068        ar_cmgt_util.wf_debug(p_case_folder_id,
1069               'ar_cmgt_data_points_pkg.getanalyticaldata()-');
1070     END IF;
1071 
1072       EXCEPTION
1073         WHEN OTHERS THEN
1074             p_errormsg := 'Error While getting Largest Invvoice and High Credit Amount, SqlError: '||sqlerrm;
1075             p_resultout := 1;
1076             ar_cmgt_util.wf_debug(p_case_folder_id, p_errormsg);
1077 END;
1078 
1079 
1080 PROCEDURE GetDunning(
1081             p_credit_request_id        IN   NUMBER,
1082             p_party_id                 IN   NUMBER,
1083             p_cust_account_id          IN   NUMBER,
1084             p_site_use_id              IN   NUMBER,
1085             p_org_id                   IN   NUMBER,
1086             p_case_folder_id           IN   NUMBER,
1087             p_period                   IN   NUMBER,
1088             p_analysis_level           IN   VARCHAR2,
1089             p_global_exposure_flag     IN   VARCHAR2,
1090             p_mode                     IN   VARCHAR2 default 'CREATE',
1091             p_errormsg                 OUT  nocopy VARCHAR2,
1092             p_resultout                out nocopy  VARCHAR2) IS
1093 
1094   l_correspondence_date             ar_correspondences_all.correspondence_date%type;
1095   l_dunning_count                   NUMBER;
1096 BEGIN
1097     IF pg_wf_debug = 'Y'
1098     THEN
1099        ar_cmgt_util.wf_debug(p_case_folder_id,
1100               'ar_cmgt_data_points_pkg.getdunning()+');
1101     END IF;
1102 
1103     p_resultout := 0;
1104     IF p_analysis_level = 'A'
1105     THEN
1106         BEGIN
1107             SELECT max(correspondence_date), count(*)
1108             INTO   l_correspondence_date, l_dunning_count
1109             FROM  ar_correspondences_all
1110             WHERE correspondence_type = 'DUNNING'
1111             AND   customer_id = p_cust_account_id
1112             AND   org_id      = decode(p_global_exposure_flag,'Y', org_id, 'N',
1113                                      decode(p_org_id,null, org_id, p_org_id), null,
1114                                      decode(p_org_id,null, org_id, p_org_id))
1115             AND   correspondence_date >= ADD_MONTHS(sysdate,(-p_period));
1116         END;
1117     ELSIF p_analysis_level = 'S'
1118     THEN
1119         BEGIN
1120             SELECT max(correspondence_date), count(*)
1121             INTO   l_correspondence_date, l_dunning_count
1122             FROM  ar_correspondences_all
1123             WHERE correspondence_type = 'DUNNING'
1124             AND   customer_id = p_cust_account_id
1125             AND   site_use_id     = p_site_use_id
1126             AND   correspondence_date >= ADD_MONTHS(sysdate,(-p_period));
1127         END;
1128     ELSIF p_analysis_level = 'P'
1129     THEN
1130        BEGIN
1131             SELECT max(correspondence_date), count(*)
1132             INTO   l_correspondence_date, l_dunning_count
1133             FROM  ar_correspondences_all
1134             WHERE correspondence_type = 'DUNNING'
1135             AND   customer_id in  (
1136                                   select TO_NUMBER(cust_account_id)  -- Fix for Bug 12670874
1137                                   FROM   hz_cust_accounts
1138                                   WHERE  party_id in
1139                                         ( SELECT child_id
1140                                           from hz_hierarchy_nodes
1141                                           where parent_object_type = 'ORGANIZATION'
1142                                             and parent_table_name = 'HZ_PARTIES'
1143                                             and child_object_type = 'ORGANIZATION'
1144                                             and parent_id = p_party_id
1145                                             and effective_start_date <= sysdate
1146                                             and effective_end_date >= sysdate
1147                                             and  hierarchy_type =
1148                                              FND_PROFILE.VALUE('AR_CMGT_HIERARCHY_TYPE')
1149                                              and  g_source_name <> 'LNS'
1150                                             union select p_party_id from dual
1151                                             UNION
1152                         select hz_party_id
1153                         from LNS_LOAN_PARTICIPANTS_V
1154                         where loan_id = g_source_id
1155                         and   participant_type_code = 'COBORROWER'
1156                         and   g_source_name = 'LNS'
1157                         and (end_date_active is null OR
1158                               (sysdate between start_date_active and end_date_active)
1159                               )
1160                                           ))
1161             AND   correspondence_date >= ADD_MONTHS(sysdate,(-p_period));
1162         END;
1163     END IF;
1164     build_case_folder_details(
1165                 p_case_folder_id            =>  p_case_folder_id,
1166                 p_data_point_id             =>  57,
1167                 p_data_point_value          =>  to_char(l_correspondence_date),
1168                 p_mode                      => p_mode,
1169                 p_error_msg                 =>  p_errormsg ,
1170                 p_resultout                 =>  p_resultout);
1171     IF p_resultout <> 0
1172     THEN
1173         return;
1174     END IF;
1175     build_case_folder_details(
1176                 p_case_folder_id            =>  p_case_folder_id,
1177                 p_data_point_id             =>  59,
1178                 p_data_point_value          =>  l_dunning_count,
1179                 p_mode                      => p_mode,
1180                 p_error_msg                 =>  p_errormsg ,
1181                 p_resultout                 =>  p_resultout);
1182     IF p_resultout <> 0
1183     THEN
1184         return;
1185     END IF;
1186 
1187     IF pg_wf_debug = 'Y'
1188     THEN
1189        ar_cmgt_util.wf_debug(p_case_folder_id,
1190               'ar_cmgt_data_points_pkg.getdunning()-');
1191     END IF;
1192 
1193     EXCEPTION
1194         WHEN OTHERS THEN
1195             p_resultout := 1;
1196             p_errormsg := 'Error While getting Dunning Letter Count, Sql Error:'||sqlerrm;
1197             ar_cmgt_util.wf_debug(p_case_folder_id, p_errormsg);
1198 END;
1199 
1200 
1201 PROCEDURE GetOMDataPoints(
1202             p_credit_request_id        IN   NUMBER,
1203             p_party_id                 IN   NUMBER,
1204             p_cust_account_id          IN   NUMBER,
1205             p_site_use_id              IN   NUMBER,
1206             p_case_folder_id           IN   NUMBER,
1207             p_analysis_level           IN   VARCHAR2,
1208             p_limit_currency_code      IN   VARCHAR2,
1209             p_mode                     IN   VARCHAR2 default 'CREATE',
1210             p_errormsg                 OUT nocopy  VARCHAR2,
1211             p_resultout                OUT nocopy  VARCHAR2) IS
1212 
1213 l_status                    varchar2(1) ;
1214 l_industry                  VARCHAR2(1) ;
1215 l_return                    BOOLEAN ;
1216 l_application_id            NUMBER;
1217 l_credit_check_rule_id      NUMBER;
1218 l_total_exposure            NUMBER;
1219 l_order_hold_amount         NUMBER;
1220 l_order_amount              NUMBER;
1221 l_ar_cmount                 NUMBER;
1222 l_external_amount           NUMBER;
1223 l_return_status             VARCHAR2(1);
1224 l_errmsg                    VARCHAR2(2000);
1225 l_resultout                 VARCHAR2(1);
1226 l_om_installed_flag         VARCHAR2(1) := 'Y';
1227 l_om_exposure               NUMBER;
1228 l_credit_exposure           NUMBER;
1229 l_rec_bal                   NUMBER := 0;
1230 l_file_val       			VARCHAR2(60);
1231 /* 9560367 */
1232 l_limit_currency_overridden boolean;
1233 l_limit_currency_code       ar_cmgt_credit_requests.limit_currency%type;
1234 
1235 CURSOR RecBalC IS
1236     SELECT data_point_value
1237     FROM   ar_cmgt_cf_dtls
1238     WHERE  case_folder_id = p_case_folder_id
1239     AND    data_point_id = 34; -- receivable balance
1240 BEGIN
1241     IF pg_wf_debug = 'Y'
1242     THEN
1243        ar_cmgt_util.wf_debug(p_case_folder_id,
1244               'ar_cmgt_data_points_pkg.getomdatapoints()+');
1245     END IF;
1246 
1247     -- first check if OM is installed, if OM is installed then get oM data points
1248     p_resultout := 0;
1249 
1250     BEGIN
1251         select  application_id
1252         into    l_application_id
1253         from    fnd_application
1254         where application_short_name = 'ONT' ;
1255 
1256         IF fnd_installation.get(l_application_id,l_application_id
1257                             ,l_status,l_industry) then
1258 
1259             IF l_status NOT IN ('I','S')
1260             THEN
1261                 l_om_installed_flag := 'N';
1262             END IF;
1263 
1264         ELSE
1265             l_om_installed_flag := 'N';
1266         END IF;
1267     EXCEPTION
1268         WHEN NO_DATA_FOUND THEN
1269             l_om_installed_flag := 'N';
1270         WHEN OTHERS THEN
1271             p_resultout := 1;
1272             p_errormsg := 'Error While Checking if ONT is installed or not, Sqlerror '||sqlerrm;
1273             ar_cmgt_util.wf_debug(p_case_folder_id, p_errormsg);
1274             return;
1275     END;
1276     BEGIN
1277         --  Now if OM is installed get credit_check_rule_id
1278         SELECT credit_check_rule_id
1279         INTO   l_credit_check_rule_id
1280         FROM   ar_cmgt_credit_requests
1281         WHERE  credit_request_id = p_credit_request_id;
1282 
1283         IF l_credit_check_rule_id IS NULL
1284         THEN
1285             l_credit_check_rule_id := -999; -- use seeded credit check rule id
1286         END IF;
1287         EXCEPTION
1288             WHEN NO_DATA_FOUND THEN
1289                 l_om_installed_flag := 'N';
1290             WHEN OTHERS THEN
1291                 p_resultout := 1;
1292                 p_errormsg := 'Error While getting Credit Check rule Id, Sqlerror '||sqlerrm;
1293                 ar_cmgt_util.wf_debug(p_case_folder_id, p_errormsg);
1294                 return;
1295     END;
1296     IF l_om_installed_flag = 'N'
1300                 p_data_point_id             =>  15,
1297     THEN
1298         build_case_folder_details(
1299                 p_case_folder_id            =>  p_case_folder_id,
1301                 p_data_point_value          =>  null,
1302                 p_mode                      =>  p_mode,
1303                 p_error_msg                 =>  l_errmsg,
1304                 p_resultout                 =>  l_resultout);
1305 
1306         build_case_folder_details(
1307                 p_case_folder_id            =>  p_case_folder_id,
1308                 p_data_point_id             =>  26,
1309                 p_mode                      =>  p_mode,
1310                 p_data_point_value          =>  null,
1311                 p_error_msg                    =>  l_errmsg,
1312                 p_resultout                 =>  l_resultout);
1313        return; -- no need to continue if OM is not installed
1314     END IF;
1315 
1316     -- Added for bug 9560367 -- Start
1317     -- Enable the debug of Order Management to record logging, if the FND: Debug
1318     -- is set
1319     IF pg_wf_debug = 'Y'
1320     THEN
1321       oe_debug_pub.debug_on;
1322   	  oe_debug_pub.initialize;
1323 	  l_file_val := oe_debug_pub.set_debug_mode('FILE');
1324 	  oe_debug_pub.setdebuglevel(5);
1325       ar_cmgt_util.wf_debug(p_case_folder_id,
1326               'OM Output Debug File is stored at : '||OE_DEBUG_PUB.G_DIR||'/'||OE_DEBUG_PUB.G_FILE);
1327       ar_cmgt_util.wf_debug(p_case_folder_id, 'File --> : '||l_file_val);
1328     END IF;
1329 
1330     IF pg_wf_debug = 'Y'
1331     THEN
1332       ar_cmgt_util.wf_debug(p_case_folder_id, 'p_analysis_level --> : '||p_analysis_level);
1333     END IF;
1334     -- Added for bug 9560367 -- End
1335 
1336     /* 9560367 - OM expects the limit currency to be static based
1337        on usage rules.  We have made provisions to not use the expected
1338        limit currency for some OKL-related activities.  In those cases,
1339        we need to let OM believe we are using the real limit currency and
1340        convert back to our currency afterwards. */
1341     IF   g_intended_limit_currency <> p_limit_currency_code
1342     THEN
1343        /* we have overridden the limit currency for this folder
1344           so we'll need to convert numbers from l_limit_currency
1345           to p_limit_currency after fetch */
1346        l_limit_currency_overridden := TRUE;
1347        l_limit_currency_code := g_intended_limit_currency;
1348 
1349        IF pg_wf_debug = 'Y'
1350        THEN
1351          ar_cmgt_util.wf_debug(p_case_folder_id, 'Overridden limit_currency, using ' || l_limit_currency_code || ' for OM call');
1352        END IF;
1353     ELSE
1354        /* we have NOT overridden the limit currency.  No
1355           conversion required */
1356        l_limit_currency_overridden := FALSE;
1357        l_limit_currency_code := p_limit_currency_code;
1358 
1359        IF pg_wf_debug = 'Y'
1360        THEN
1361          ar_cmgt_util.wf_debug(p_case_folder_id, 'No override: using ' || l_limit_currency_code || ' for OM call');
1362        END IF;
1363     END IF;
1364 
1365     -- check what is the analysis level
1366     IF p_analysis_level in ('P')
1367     THEN
1368         OE_Credit_Exposure_PUB.Get_customer_exposure
1369             ( p_party_id                => p_party_id,
1370               p_customer_id             => NULL,
1371               p_site_id                 => NULL,
1372               p_limit_curr_code         => l_limit_currency_code,
1373               p_credit_check_rule_id    => l_credit_check_rule_id,
1374               x_total_exposure          => l_total_exposure,
1375               x_order_hold_amount       => l_order_hold_amount,
1376               x_order_amount            => l_order_amount,
1377               x_ar_amount               => l_ar_cmount,
1378               x_external_amount         => l_external_amount,
1379               x_return_status           => l_return_status);
1380     ELSIF p_analysis_level in ('A')
1381     THEN
1382         OE_Credit_Exposure_PUB.Get_customer_exposure
1383             ( p_party_id                => NULL,      -- Added for bug 10433328
1384               p_customer_id             => p_cust_account_id,
1385               p_site_id                 => NULL,
1386               p_limit_curr_code         => l_limit_currency_code,
1387               p_credit_check_rule_id    => l_credit_check_rule_id,
1388               x_total_exposure          => l_total_exposure,
1389               x_order_hold_amount       => l_order_hold_amount,
1390               x_order_amount            => l_order_amount,
1391               x_ar_amount               => l_ar_cmount,
1392               x_external_amount         => l_external_amount,
1393               x_return_status           => l_return_status);
1394 
1395     ELSIF p_analysis_level = 'S'
1396     THEN
1397         OE_Credit_Exposure_PUB.Get_customer_exposure
1398             ( p_party_id                => p_party_id,
1399               p_customer_id             => p_cust_account_id,
1400               p_site_id                 => p_site_use_id,
1401               p_limit_curr_code         => l_limit_currency_code,
1402               p_credit_check_rule_id    => l_credit_check_rule_id,
1403               x_total_exposure          => l_total_exposure,
1404               x_order_hold_amount       => l_order_hold_amount,
1405               x_order_amount            => l_order_amount,
1406               x_ar_amount               => l_ar_cmount,
1407               x_external_amount         => l_external_amount,
1408               x_return_status           => l_return_status);
1409 
1410     END IF;
1411 
1412 	-- Added for bug 9560367
1413     IF pg_wf_debug = 'Y'
1414     THEN
1415       ar_cmgt_util.wf_debug(p_case_folder_id, 'Return Status after OE_Credit_Exposure_PUB.Get_customer_exposure --> : '||l_return_status);
1416     END IF;
1417 
1418     IF l_return_status = FND_API.G_RET_STS_SUCCESS
1419     THEN
1420        IF l_limit_currency_overridden
1421        THEN
1422          IF g_conversion_type IS NULL
1423          THEN
1424             g_conversion_type := get_conversion_type(p_credit_request_id);
1425          END IF;
1426 
1427          l_total_exposure :=
1428               gl_currency_api.convert_amount(l_limit_currency_code,
1429                       p_limit_currency_code, sysdate,
1430                       g_conversion_type,
1431                       l_total_exposure);
1432 
1433          l_ar_cmount :=
1434               gl_currency_api.convert_amount(l_limit_currency_code,
1435                       p_limit_currency_code, sysdate,
1436                       g_conversion_type,
1437                       l_ar_cmount);
1438 
1439          l_order_hold_amount :=
1440               gl_currency_api.convert_amount(l_limit_currency_code,
1441                       p_limit_currency_code, sysdate,
1442                       g_conversion_type,
1443                       l_order_hold_amount);
1444        END IF;
1445 
1446             build_case_folder_details(
1447                 p_case_folder_id            =>  p_case_folder_id,
1448                 p_data_point_id             =>  15,
1449                 p_data_point_value          =>  fnd_number.number_to_canonical((l_total_exposure - l_ar_cmount)),
1450                 p_mode                      =>  p_mode,
1451                 p_error_msg                 =>  l_errmsg,
1452                 p_resultout                 =>  l_resultout);
1453 
1454             build_case_folder_details(
1455                 p_case_folder_id            =>  p_case_folder_id,
1456                 p_data_point_id             =>  26,
1457                 p_data_point_value          =>  fnd_number.number_to_canonical(l_order_hold_amount),
1458                 p_mode                      =>  p_mode,
1459                 p_error_msg                 =>  l_errmsg,
1460                 p_resultout                 =>  l_resultout);
1461 
1462             -- Now Populate Credit exposure
1463             /* 9560367 - Added value back into itself.. the loop didnt make sense before! */
1464             FOR RecBalRec IN RecBalC
1465             LOOP
1466                  l_rec_bal := l_rec_bal +
1467                     NVL(fnd_number.canonical_to_number(RecBalRec.data_point_value),0);
1468             END LOOP;
1469 
1470             l_credit_exposure :=
1471                 nvl(l_rec_bal,0) + (nvl(l_total_exposure,0) - nvl(l_ar_cmount,0)) +
1472                 nvl(l_order_hold_amount,0);
1473 
1474             build_case_folder_details(
1475                 p_case_folder_id            =>  p_case_folder_id,
1476                 p_data_point_id             =>  213,
1477                 p_data_point_value          =>  fnd_number.number_to_canonical(l_credit_exposure),
1478                 p_mode                      =>  p_mode,
1479                 p_error_msg                 =>  l_errmsg,
1480                 p_resultout                 =>  l_resultout);
1481     ELSE
1482            p_resultout := 1;
1483            p_errormsg := 'Error While getting OM Exposure, Sqlerror '||sqlerrm;
1484            ar_cmgt_util.wf_debug(p_case_folder_id, p_errormsg);
1485            return;
1486     END IF;
1487 
1488     IF pg_wf_debug = 'Y'
1489     THEN
1490        ar_cmgt_util.wf_debug(p_case_folder_id,
1491               'ar_cmgt_data_points_pkg.getomdatapoints()-');
1492     END IF;
1493 
1494 END;
1495 
1496 /* 6335440 - this procedure did not work prior to this bug.
1497    The reason was that the financial data (ar_cmgt_financial_data)
1498    had cust_acct_site_id in the site_use_id field.  Additionally,
1499    prior to this bug, the procedure relied on completely un-indexed
1503    To correct these issues, we did the following:
1500    joins and multiple reads of the ar_cmgt_financial_data table
1501    which (for production data) would bave performed awful.
1502 
1504 
1505      o revised initial select to fetch cust_acct_site_id
1506         instead of site_use_id.  This is used to correctly
1507         join to the errant ar_cmgt_financial_data table.
1508 
1509      o revised second select to only join to
1510         ar_cmgt_financial_data once (was joining twice)
1511 
1512      o Added index ar_cmgt_financial_data_n2 that contains following:
1513          1) party_id
1514          2) reporting_currency
1515          3) cust_account_id
1516          4) site_use_id
1517 
1518        The reason the index is in this particular order is that party_id
1519        and currency are guaranteed.  cust and/or site are optional and
1520        may be provided as cust or cust + site.
1521 
1522 */
1523 
1524 PROCEDURE GetFinancialData (
1525         p_credit_request_id         IN            NUMBER,
1526         p_case_folder_id            IN            NUMBER,
1527         p_mode                      IN            VARCHAR2,
1528         p_resultout                 OUT NOCOPY    VARCHAR2,
1529         p_errmsg                  OUT NOCOPY    VARCHAR2) IS
1530 
1531         l_reporting_currency        ar_cmgt_financial_data.reporting_currency%type;
1532         l_monetary_unit             ar_cmgt_financial_data.monetary_unit%type;
1533         l_curr_fin_st_date          ar_cmgt_financial_data.curr_fin_st_date%type;
1534         l_reporting_period          ar_cmgt_financial_data.reporting_period%type;
1535         l_cash                      ar_cmgt_financial_data.cash%type;
1536         l_net_receivables           ar_cmgt_financial_data.net_receivables%type;
1537         l_inventories               ar_cmgt_financial_data.inventories%type;
1538         l_other_cur_assets          ar_cmgt_financial_data.other_cur_assets%type;
1539         l_total_cur_assets          ar_cmgt_financial_data.total_cur_assets%type;
1540         l_net_fixed_assets          ar_cmgt_financial_data.net_fixed_assets%type;
1541         l_other_non_cur_assets      ar_cmgt_financial_data.net_fixed_assets%type;
1542         l_total_assets              ar_cmgt_financial_data.total_assets%type;
1543         l_accounts_payable          ar_cmgt_financial_data.accounts_payable%type;
1544         l_short_term_debt           ar_cmgt_financial_data.short_term_debt%type;
1545         l_other_cur_liabilities     ar_cmgt_financial_data.other_cur_liabilities%type;
1546         l_total_cur_liabilities     ar_cmgt_financial_data.total_cur_liabilities%type;
1547         l_long_term_debt            ar_cmgt_financial_data.long_term_debt%type;
1548         l_other_non_cur_liabilities ar_cmgt_financial_data.other_non_cur_liabilities%type;
1549         l_total_liabilities         ar_cmgt_financial_data.total_liabilities%type;
1550         l_stockholder_equity        ar_cmgt_financial_data.stockholder_equity%type;
1551         l_total_liabilities_equity  ar_cmgt_financial_data.total_liabilities_equity%type;
1552         l_revenue                   ar_cmgt_financial_data.revenue%type;
1553         l_cost_of_goods_sold        ar_cmgt_financial_data.cost_of_goods_sold%type;
1554         l_sga_expenses              ar_cmgt_financial_data.sga_expenses%type;
1555         l_operating_income          ar_cmgt_financial_data.operating_income%type;
1556         l_operating_margin          ar_cmgt_financial_data.operating_margin%type;
1557         l_non_operating_income      ar_cmgt_financial_data.non_operating_income%type;
1558         l_non_operating_expenses    ar_cmgt_financial_data.non_operating_expenses%type;
1559         l_pre_tax_net_income        ar_cmgt_financial_data.pre_tax_net_income%type;
1560         l_income_taxes              ar_cmgt_financial_data.income_taxes%type;
1561         l_net_income                ar_cmgt_financial_data.net_income%type;
1562         l_earnings_per_share        ar_cmgt_financial_data.earnings_per_share%type;
1563         l_financial_data_id         ar_cmgt_financial_data.financial_data_id%type;
1564         l_party_id                  ar_cmgt_case_folders.party_id%type;
1565         l_cust_account_id           ar_cmgt_case_folders.cust_account_id%type;
1566         l_site_use_id               ar_cmgt_case_folders.site_use_id%type;
1567         l_limit_currency            ar_cmgt_case_folders.limit_currency%type;
1568 
1569 BEGIN
1570     IF pg_wf_debug = 'Y'
1571     THEN
1572        ar_cmgt_util.wf_debug(p_case_folder_id,
1573               'ar_cmgt_data_points_pkg.getfinancialdata()+');
1574     END IF;
1575 
1576     p_resultout := 0;
1577     BEGIN
1578         BEGIN
1579           -- first get the maximum data for a party
1580           /* 6335440 - modified to fetch correct site for fin data */
1581           SELECT cmcf.party_id,
1582                  cmcf.cust_account_id,
1583                  nvl(hzs.cust_acct_site_id,cmcf.site_use_id),
1584                  cmcf.limit_currency
1585           INTO   l_party_id, l_cust_account_id, l_site_use_id, l_limit_currency
1586           FROM   ar_cmgt_case_folders      cmcf,
1587                  hz_cust_site_uses_all     hzs
1588           WHERE  cmcf.case_folder_id = p_case_folder_id
1589           AND    cmcf.site_use_id = hzs.site_use_id (+);
1590 
1591         EXCEPTION
1592             WHEN OTHERS THEN
1593                       p_resultout := 1;
1594                       p_errmsg := 'Error while getting party  information from ar_cmgt_case_folders'||
1595                           ' SqlError '|| sqlerrm;
1596                       ar_cmgt_util.wf_debug(p_case_folder_id, p_errmsg);
1597                       return;
1598         END;
1599 
1600 
1601         BEGIN
1602           /* 6335440 - restructured this sql to use a trick
1603              that ultimately distills down to the maximum
1604              financial_data_id on the maximum date that matches
1605              the parameters (currency,party,customer,site).
1606 
1607              This sql has same result as predecessor only it
1611           SELECT to_number(
1608              reduces the number of reads on ar_cmgt_financial_data
1609              to a single read */
1610 
1612                      substr(
1613                        max(to_char(curr_fin_st_date, 'YYYYMMDD') ||
1614                        ltrim(to_char(financial_data_id,
1615                               '0999999999999999999999')))
1616                             ,9))
1617           INTO   l_financial_data_id
1618           FROM   ar_cmgt_financial_data
1619           WHERE  reporting_currency = l_limit_currency
1620           AND    party_id           = l_party_id
1621           AND    cust_account_id    = l_cust_account_id
1622           AND    site_use_id        = l_site_use_id;
1623 
1624           EXCEPTION
1625                   WHEN NO_DATA_FOUND THEN
1626                     NULL;
1627                   WHEN TOO_MANY_ROWS THEN
1628                     NULL;
1629                   WHEN OTHERS THEN
1630                       p_resultout := 1;
1631                       p_errmsg := 'Error while getting max data from ar_cmgt_financial_data '||
1632                           ' SqlError '|| sqlerrm;
1633                       ar_cmgt_util.wf_debug(p_case_folder_id, p_errmsg);
1634                       return;
1635 
1636         END;
1637 
1638         SELECT reporting_currency,
1639                monetary_unit,
1640                curr_fin_st_date,
1641                reporting_period,
1642                cash,
1643                net_receivables,
1644                inventories,
1645                other_cur_assets,
1646                total_cur_assets,
1647                net_fixed_assets,
1648                other_non_cur_assets,
1649                total_assets,
1650                accounts_payable,
1651                short_term_debt,
1652                other_cur_liabilities,
1653                total_cur_liabilities,
1654                long_term_debt,
1655                other_non_cur_liabilities,
1656                total_liabilities,
1657                stockholder_equity,
1658                total_liabilities_equity,
1659                revenue,
1660                cost_of_goods_sold,
1661                sga_expenses,
1662                operating_income,
1663                operating_margin,
1664                non_operating_income,
1665                non_operating_expenses,
1666                pre_tax_net_income,
1667                income_taxes,
1668                net_income,
1669                earnings_per_share
1670            INTO
1671                 l_reporting_currency,
1672                 l_monetary_unit     ,
1673                 l_curr_fin_st_date  ,
1674                 l_reporting_period  ,
1675                 l_cash              ,
1676                 l_net_receivables   ,
1677                 l_inventories       ,
1678                 l_other_cur_assets  ,
1679                 l_total_cur_assets,
1680                 l_net_fixed_assets,
1681                 l_other_non_cur_assets,
1682                 l_total_assets      ,
1683                 l_accounts_payable  ,
1684                 l_short_term_debt   ,
1685                 l_other_cur_liabilities,
1686                 l_total_cur_liabilities ,
1687                 l_long_term_debt        ,
1688                 l_other_non_cur_liabilities,
1689                 l_total_liabilities        ,
1690                 l_stockholder_equity       ,
1691                 l_total_liabilities_equity ,
1692                 l_revenue                  ,
1693                 l_cost_of_goods_sold       ,
1694                 l_sga_expenses             ,
1695                 l_operating_income         ,
1696                 l_operating_margin         ,
1697                 l_non_operating_income     ,
1698                 l_non_operating_expenses   ,
1699                 l_pre_tax_net_income       ,
1700                 l_income_taxes             ,
1701                 l_net_income               ,
1702                 l_earnings_per_share
1703            FROM ar_cmgt_financial_data
1704            WHERE financial_data_id = l_financial_data_id;
1705 
1706    EXCEPTION
1707         WHEN NO_DATA_FOUND THEN
1708             NULL;
1709         WHEN TOO_MANY_ROWS THEN
1710             NULL;
1711         WHEN OTHERS THEN
1712             p_resultout := 1;
1713             p_errmsg := 'Error while getting records from ar_cmgt_financial_data '||
1714                           ' SqlError '|| sqlerrm;
1715             ar_cmgt_util.wf_debug(p_case_folder_id, p_errmsg);
1716             return;
1717 
1718     END;
1719         build_case_folder_details(
1720                 p_case_folder_id            =>  p_case_folder_id,
1721                 p_data_point_id             =>  111,
1722                 p_data_point_value          =>  l_curr_fin_st_date,
1723                 p_mode                      =>  p_mode,
1724                 p_error_msg                 =>  p_errmsg,
1725                 p_resultout                 =>  p_resultout);
1726         build_case_folder_details(
1727                 p_case_folder_id            =>  p_case_folder_id,
1728                 p_data_point_id             =>  110,
1729                 p_data_point_value          =>  l_monetary_unit,
1730                 p_mode                      =>  p_mode,
1731                 p_error_msg                 =>  p_errmsg,
1732                 p_resultout                 =>  p_resultout);
1733         build_case_folder_details(
1734                 p_case_folder_id            =>  p_case_folder_id,
1735                 p_data_point_id             =>  109,
1736                 p_data_point_value          =>  l_reporting_currency,
1737                 p_mode                      =>  p_mode,
1738                 p_error_msg                 =>  p_errmsg,
1739                 p_resultout                 =>  p_resultout);
1743                 p_data_point_value          =>  l_reporting_period,
1740         build_case_folder_details(
1741                 p_case_folder_id            =>  p_case_folder_id,
1742                 p_data_point_id             =>  112,
1744                 p_mode                      =>  p_mode,
1745                 p_error_msg                 =>  p_errmsg,
1746                 p_resultout                 =>  p_resultout);
1747         build_case_folder_details(
1748                 p_case_folder_id            =>  p_case_folder_id,
1749                 p_data_point_id             =>  113,
1750                 p_data_point_value          =>  fnd_number.number_to_canonical(l_cash),
1751                 p_mode                      =>  p_mode,
1752                 p_error_msg                 =>  p_errmsg,
1753                 p_resultout                 =>  p_resultout);
1754         build_case_folder_details(
1755                 p_case_folder_id            =>  p_case_folder_id,
1756                 p_data_point_id             =>  114,
1757                 p_data_point_value          =>  fnd_number.number_to_canonical(l_net_receivables),
1758                 p_mode                      =>  p_mode,
1759                 p_error_msg                 =>  p_errmsg,
1760                 p_resultout                 =>  p_resultout);
1761         build_case_folder_details(
1762                 p_case_folder_id            =>  p_case_folder_id,
1763                 p_data_point_id             =>  115,
1764                 p_data_point_value          =>  fnd_number.number_to_canonical(l_inventories),
1765                 p_mode                      => p_mode,
1766                 p_error_msg                 =>  p_errmsg,
1767                 p_resultout                 =>  p_resultout);
1768         build_case_folder_details(
1769                 p_case_folder_id            =>  p_case_folder_id,
1770                 p_data_point_id             =>  116,
1771                 p_data_point_value          =>  fnd_number.number_to_canonical(l_other_cur_assets),
1772                 p_mode                      => p_mode,
1773                 p_error_msg                 =>  p_errmsg,
1774                 p_resultout                 =>  p_resultout);
1775         build_case_folder_details(
1776                 p_case_folder_id            =>  p_case_folder_id,
1777                 p_data_point_id             =>  117,
1778                 p_data_point_value          =>  fnd_number.number_to_canonical(l_total_cur_assets),
1779                 p_mode                      => p_mode,
1780                 p_error_msg                 =>  p_errmsg,
1781                 p_resultout                 =>  p_resultout);
1782         build_case_folder_details(
1783                 p_case_folder_id            =>  p_case_folder_id,
1784                 p_data_point_id             =>  118,
1785                 p_data_point_value          =>  fnd_number.number_to_canonical(l_net_fixed_assets),
1786                 p_mode                      => p_mode,
1787                 p_error_msg                 =>  p_errmsg,
1788                 p_resultout                 =>  p_resultout);
1789         build_case_folder_details(
1790                 p_case_folder_id            =>  p_case_folder_id,
1791                 p_data_point_id             =>  119,
1792                 p_data_point_value          =>  fnd_number.number_to_canonical(l_other_non_cur_assets),
1793                 p_mode                      =>  p_mode,
1794                 p_error_msg                 =>  p_errmsg,
1795                 p_resultout                 =>  p_resultout);
1796         build_case_folder_details(
1797                 p_case_folder_id            =>  p_case_folder_id,
1798                 p_data_point_id             =>  120,
1799                 p_data_point_value          =>  fnd_number.number_to_canonical(l_total_assets),
1800                 p_mode                      => p_mode,
1801                 p_error_msg                 =>  p_errmsg,
1802                 p_resultout                 =>  p_resultout);
1803         build_case_folder_details(
1804                 p_case_folder_id            =>  p_case_folder_id,
1805                 p_data_point_id             =>  121,
1806                 p_data_point_value          =>  fnd_number.number_to_canonical(l_accounts_payable),
1807                 p_mode                      => p_mode,
1808                 p_error_msg                 =>  p_errmsg,
1809                 p_resultout                 =>  p_resultout);
1810         build_case_folder_details(
1811                 p_case_folder_id            =>  p_case_folder_id,
1812                 p_data_point_id             =>  122,
1813                 p_data_point_value          =>  fnd_number.number_to_canonical(l_short_term_debt),
1814                 p_mode                      => p_mode,
1815                 p_error_msg                 =>  p_errmsg,
1816                 p_resultout                 =>  p_resultout);
1817         build_case_folder_details(
1818                 p_case_folder_id            =>  p_case_folder_id,
1819                 p_data_point_id             =>  123,
1820                 p_data_point_value          =>  fnd_number.number_to_canonical(l_other_cur_liabilities),
1821                 p_mode                      => p_mode,
1822                 p_error_msg                 =>  p_errmsg,
1823                 p_resultout                 =>  p_resultout);
1824         build_case_folder_details(
1825                 p_case_folder_id            =>  p_case_folder_id,
1826                 p_data_point_id             =>  124,
1827                 p_data_point_value          =>  fnd_number.number_to_canonical(l_total_cur_liabilities),
1828                 p_mode                      => p_mode,
1829                 p_error_msg                 =>  p_errmsg,
1830                 p_resultout                 =>  p_resultout);
1831         build_case_folder_details(
1832                 p_case_folder_id            =>  p_case_folder_id,
1833                 p_data_point_id             =>  125,
1837                 p_resultout                 =>  p_resultout);
1834                 p_data_point_value          =>  fnd_number.number_to_canonical(l_long_term_debt),
1835                 p_mode                      => p_mode,
1836                 p_error_msg                 =>  p_errmsg,
1838         build_case_folder_details(
1839                 p_case_folder_id            =>  p_case_folder_id,
1840                 p_data_point_id             =>  126,
1841                 p_data_point_value          =>  fnd_number.number_to_canonical(l_other_non_cur_liabilities),
1842                 p_mode                      => p_mode,
1843                 p_error_msg                 =>  p_errmsg,
1844                 p_resultout                 =>  p_resultout);
1845         build_case_folder_details(
1846                 p_case_folder_id            =>  p_case_folder_id,
1847                 p_data_point_id             =>  127,
1848                 p_data_point_value          =>  fnd_number.number_to_canonical(l_total_liabilities),
1849                 p_mode                      => p_mode,
1850                 p_error_msg                 =>  p_errmsg,
1851                 p_resultout                 =>  p_resultout);
1852         build_case_folder_details(
1853                 p_case_folder_id            =>  p_case_folder_id,
1854                 p_data_point_id             =>  128,
1855                 p_data_point_value          =>  fnd_number.number_to_canonical(l_stockholder_equity),
1856                 p_mode                      => p_mode,
1857                 p_error_msg                 =>  p_errmsg,
1858                 p_resultout                 =>  p_resultout);
1859         build_case_folder_details(
1860                 p_case_folder_id            =>  p_case_folder_id,
1861                 p_data_point_id             =>  129,
1862                 p_data_point_value          =>  fnd_number.number_to_canonical(l_total_liabilities_equity),
1863                 p_mode                      => p_mode,
1864                 p_error_msg                 =>  p_errmsg,
1865                 p_resultout                 =>  p_resultout);
1866         build_case_folder_details(
1867                 p_case_folder_id            =>  p_case_folder_id,
1868                 p_data_point_id             =>  130,
1869                 p_data_point_value          =>  fnd_number.number_to_canonical(l_revenue),
1870                 p_mode                      => p_mode,
1871                 p_error_msg                 =>  p_errmsg,
1872                 p_resultout                 =>  p_resultout);
1873         build_case_folder_details(
1874                 p_case_folder_id            =>  p_case_folder_id,
1875                 p_data_point_id             =>  131,
1876                 p_data_point_value          =>  fnd_number.number_to_canonical(l_cost_of_goods_sold),
1877                 p_mode                      => p_mode,
1878                 p_error_msg                 =>  p_errmsg,
1879                 p_resultout                 =>  p_resultout);
1880         build_case_folder_details(
1881                 p_case_folder_id            =>  p_case_folder_id,
1882                 p_data_point_id             =>  132,
1883                 p_data_point_value          =>  fnd_number.number_to_canonical(l_sga_expenses),
1884                 p_mode                      => p_mode,
1885                 p_error_msg                 =>  p_errmsg,
1886                 p_resultout                 =>  p_resultout);
1887         build_case_folder_details(
1888                 p_case_folder_id            =>  p_case_folder_id,
1889                 p_data_point_id             =>  133,
1890                 p_data_point_value          =>  fnd_number.number_to_canonical(l_operating_income),
1891                 p_mode                      => p_mode,
1892                 p_error_msg                 =>  p_errmsg,
1893                 p_resultout                 =>  p_resultout);
1894         build_case_folder_details(
1895                 p_case_folder_id            =>  p_case_folder_id,
1896                 p_data_point_id             =>  134,
1897                 p_data_point_value          =>  fnd_number.number_to_canonical(l_operating_margin),
1898                 p_mode                      => p_mode,
1899                 p_error_msg                 =>  p_errmsg,
1900                 p_resultout                 =>  p_resultout);
1901         build_case_folder_details(
1902                 p_case_folder_id            =>  p_case_folder_id,
1903                 p_data_point_id             =>  135,
1904                 p_data_point_value          =>  fnd_number.number_to_canonical(l_non_operating_income),
1905                 p_mode                      => p_mode,
1906                 p_error_msg                 =>  p_errmsg,
1907                 p_resultout                 =>  p_resultout);
1908         build_case_folder_details(
1909                 p_case_folder_id            =>  p_case_folder_id,
1910                 p_data_point_id             =>  136,
1911                 p_data_point_value          =>  fnd_number.number_to_canonical(l_non_operating_expenses),
1912                 p_mode                      => p_mode,
1913                 p_error_msg                 =>  p_errmsg,
1914                 p_resultout                 =>  p_resultout);
1915         build_case_folder_details(
1916                 p_case_folder_id            =>  p_case_folder_id,
1917                 p_data_point_id             =>  137,
1918                 p_data_point_value          =>  fnd_number.number_to_canonical(l_pre_tax_net_income),
1919                 p_mode                      => p_mode,
1920                 p_error_msg                 =>  p_errmsg,
1921                 p_resultout                 =>  p_resultout);
1922         build_case_folder_details(
1923                 p_case_folder_id            =>  p_case_folder_id,
1924                 p_data_point_id             =>  138,
1925                 p_data_point_value          =>  fnd_number.number_to_canonical(l_income_taxes),
1926                 p_mode                      => p_mode,
1927                 p_error_msg                 =>  p_errmsg,
1931                 p_data_point_id             =>  139,
1928                 p_resultout                 =>  p_resultout);
1929         build_case_folder_details(
1930                 p_case_folder_id            =>  p_case_folder_id,
1932                 p_data_point_value          =>  fnd_number.number_to_canonical(l_net_income),
1933                 p_mode                      => p_mode,
1934                 p_error_msg                 =>  p_errmsg,
1935                 p_resultout                 =>  p_resultout);
1936         build_case_folder_details(
1937                 p_case_folder_id            =>  p_case_folder_id,
1938                 p_data_point_id             =>  140,
1939                 p_data_point_value          =>  fnd_number.number_to_canonical(l_earnings_per_share),
1940                 p_mode                      => p_mode,
1941                 p_error_msg                 =>  p_errmsg,
1942                 p_resultout                 =>  p_resultout);
1943     IF pg_wf_debug = 'Y'
1944     THEN
1945        ar_cmgt_util.wf_debug(p_case_folder_id,
1946               'ar_cmgt_data_points_pkg.getfinancialdata()-');
1947     END IF;
1948 
1949 END;
1950 
1951 
1952 
1953 PROCEDURE GetOtherDataPoints (
1954         p_credit_request_id         IN      NUMBER,
1955         p_party_id                  IN      NUMBER,
1956         p_cust_account_id           IN      NUMBER,
1957         p_site_use_id               IN      NUMBER,
1958         p_cust_acct_profile_amt_id  IN      NUMBER,
1959         p_case_folder_id            IN      NUMBER ,
1960         p_mode                      IN      VARCHAR2 default 'CREATE') IS
1961 
1962     l_case_folder_id            NUMBER;
1963     l_errmsg                    VARCHAR2(2000);
1964     l_resultout                 VARCHAR2(1);
1965 
1966     CURSOR c_hz_cust_prof_amts IS
1967         SELECT trx_credit_limit, overall_credit_limit
1968         FROM hz_cust_profile_amts
1969         WHERE cust_acct_profile_amt_id = p_cust_acct_profile_amt_id;
1970 
1971     CURSOR c_hz_parties IS
1972         SELECT tax_name, year_established,
1973                sic_code, -- industrial code
1974                sic_code_type, -- industrial code type
1975                url,
1976                employees_total,
1977                duns_number
1978         FROM hz_parties
1979         WHERE party_id = p_party_id;
1980 
1981 
1982     CURSOR c_credit_requests IS
1983         SELECT bond_rating,
1984                pending_litigations,
1985                entity_type,
1986                stock_exchange,
1987                current_stock_price,
1988                market_capitalization,
1989                nvl(limit_amount, trx_amount) requested_amount,
1990                market_cap_monetary_unit,
1991                legal_entity_name
1992         FROM   ar_cmgt_credit_requests
1993         WHERE  credit_request_id = p_credit_request_id;
1994 
1995     CURSOR c_case_folder IS
1996         SELECT last_updated, -- last_credit_review_date
1997                case_folder_number,
1998                status,
1999                check_list_id
2000         FROM   ar_cmgt_case_folders
2001         WHERE  party_id = p_party_id
2002         AND    cust_account_id = p_cust_account_id
2003         AND    site_use_id = p_site_use_id
2004         AND    type = 'DATA';
2005 
2006     CURSOR c_case_folder_score IS
2007         SELECT SUM(b.score) score
2008         FROM   ar_cmgt_case_folders a, ar_cmgt_cf_dtls b
2009         WHERE  a.party_id = p_party_id
2010         AND    a.cust_account_id = p_cust_account_id
2011         AND    a.site_use_id = p_site_use_id
2012         AND    a.type = 'DATA'
2013         AND    a.case_folder_id = b.case_folder_id;
2014 
2015 
2016 BEGIN
2017     IF pg_wf_debug = 'Y'
2018     THEN
2019        ar_cmgt_util.wf_debug(p_case_folder_id,
2020               'ar_cmgt_data_points_pkg.getotherdatapoints()+');
2021     END IF;
2022 
2023     FOR c_hz_cust_prof_amts_rec IN c_hz_cust_prof_amts
2024     LOOP
2025         build_case_folder_details(
2026                 p_case_folder_id            =>  p_case_folder_id,
2027                 p_data_point_id             =>  95,
2028                 p_data_point_value          =>  fnd_number.number_to_canonical(c_hz_cust_prof_amts_rec.trx_credit_limit),
2029                 p_mode                      => p_mode,
2030                 p_error_msg                 =>  l_errmsg,
2031                 p_resultout                 =>  l_resultout);
2032 
2033        build_case_folder_details(
2034                 p_case_folder_id            =>  p_case_folder_id,
2035                 p_data_point_id             =>  96,
2036                 p_data_point_value          =>  fnd_number.number_to_canonical(c_hz_cust_prof_amts_rec.overall_credit_limit),
2037                 p_mode                      => p_mode,
2038                 p_error_msg                 =>  l_errmsg,
2039                 p_resultout                 =>  l_resultout);
2040        exit;
2041     END LOOP;
2042 
2043     FOR c_hz_parties_rec IN c_hz_parties
2044     LOOP
2045 
2049                 p_data_point_value          =>  c_hz_parties_rec.tax_name,
2046         build_case_folder_details(
2047                 p_case_folder_id            =>  p_case_folder_id,
2048                 p_data_point_id             =>  89,
2050                 p_mode                      => p_mode,
2051                 p_error_msg                 =>  l_errmsg,
2052                 p_resultout                 =>  l_resultout);
2053 
2054        build_case_folder_details(
2055                 p_case_folder_id            =>  p_case_folder_id,
2056                 p_data_point_id             =>  93,
2057                 p_data_point_value          =>  c_hz_parties_rec.sic_code_type,
2058                 p_mode                      =>  p_mode,
2059                 p_error_msg                 =>  l_errmsg,
2060                 p_resultout                 =>  l_resultout);
2061        build_case_folder_details(
2062                 p_case_folder_id            =>  p_case_folder_id,
2063                 p_data_point_id             =>  92,
2064                 p_data_point_value          =>  c_hz_parties_rec.sic_code,
2065                 p_mode                      =>  p_mode,
2066                 p_error_msg                 =>  l_errmsg,
2067                 p_resultout                 =>  l_resultout);
2068        build_case_folder_details(
2069                 p_case_folder_id            =>  p_case_folder_id,
2070                 p_data_point_id             =>  100,
2071                 p_data_point_value          =>  c_hz_parties_rec.url,
2072                 p_mode                      => p_mode,
2073                 p_error_msg                 =>  l_errmsg,
2074                 p_resultout                 =>  l_resultout);
2075        build_case_folder_details(
2076                 p_case_folder_id            =>  p_case_folder_id,
2077                 p_data_point_id             =>  101,
2078                 p_data_point_value          =>  c_hz_parties_rec.employees_total,
2079                 p_mode                      => p_mode,
2080                 p_error_msg                 =>  l_errmsg,
2081                 p_resultout                 =>  l_resultout);
2082        build_case_folder_details(
2083                 p_case_folder_id            =>  p_case_folder_id,
2084                 p_data_point_id             =>  183,
2085                 p_data_point_value          =>  c_hz_parties_rec.duns_number,
2086                 p_mode                      => p_mode,
2087                 p_error_msg                 =>  l_errmsg,
2088                 p_resultout                 =>  l_resultout);
2089        exit;
2090     END LOOP;
2091 
2092     FOR c_credit_requests_rec IN c_credit_requests
2093     LOOP
2094         build_case_folder_details(
2095                 p_case_folder_id            =>  p_case_folder_id,
2096                 p_data_point_id             =>  103,
2097                 p_data_point_value          =>  c_credit_requests_rec.bond_rating,
2098                 p_mode                      => p_mode,
2099                 p_error_msg                 =>  l_errmsg,
2100                 p_resultout                 =>  l_resultout);
2101         build_case_folder_details(
2102                 p_case_folder_id            =>  p_case_folder_id,
2103                 p_data_point_id             =>  94,
2104                 p_data_point_value          =>  c_credit_requests_rec.pending_litigations,
2105                 p_mode                      => p_mode,
2106                 p_error_msg                 =>  l_errmsg,
2107                 p_resultout                 =>  l_resultout);
2111                 p_data_point_value          =>  c_credit_requests_rec.entity_type,
2108         build_case_folder_details(
2109                 p_case_folder_id            =>  p_case_folder_id,
2110                 p_data_point_id             =>  90,
2112                 p_mode                      => p_mode,
2113                 p_error_msg                 =>  l_errmsg,
2114                 p_resultout                 =>  l_resultout);
2115         build_case_folder_details(
2116                 p_case_folder_id            =>  p_case_folder_id,
2117                 p_data_point_id             =>  155,
2118                 p_data_point_value          =>  c_credit_requests_rec.stock_exchange,
2119                 p_mode                      => p_mode,
2120                 p_error_msg                 =>  l_errmsg,
2121                 p_resultout                 =>  l_resultout);
2122         build_case_folder_details(
2123                 p_case_folder_id            =>  p_case_folder_id,
2124                 p_data_point_id             =>  98,
2125                 p_data_point_value          =>  fnd_number.number_to_canonical(c_credit_requests_rec.current_stock_price),
2126                 p_mode                      => p_mode,
2127                 p_error_msg                 =>  l_errmsg,
2128                 p_resultout                 =>  l_resultout);
2129         build_case_folder_details(
2130                 p_case_folder_id            =>  p_case_folder_id,
2131                 p_data_point_id             =>  99,
2132                 p_data_point_value          =>  fnd_number.number_to_canonical(c_credit_requests_rec.market_capitalization),
2133                 p_mode                      => p_mode,
2134                 p_error_msg                 =>  l_errmsg,
2135                 p_resultout                 =>  l_resultout);
2136         build_case_folder_details(
2137                 p_case_folder_id            =>  p_case_folder_id,
2138                 p_data_point_id             =>  108,
2139                 p_data_point_value          =>  fnd_number.number_to_canonical(c_credit_requests_rec.requested_amount),
2140                 p_mode                      => p_mode,
2141                 p_error_msg                 =>  l_errmsg,
2142                 p_resultout                 =>  l_resultout);
2143         build_case_folder_details(
2144                 p_case_folder_id            =>  p_case_folder_id,
2145                 p_data_point_id             =>  208,
2146                 p_data_point_value          =>  c_credit_requests_rec.legal_entity_name,
2147                 p_mode                      => p_mode,
2148                 p_error_msg                 =>  l_errmsg,
2149                 p_resultout                 =>  l_resultout);
2150         exit;
2151     END LOOP;
2152 
2153 
2154     FOR c_case_folder_rec IN c_case_folder
2155     LOOP
2156          build_case_folder_details(
2157                 p_case_folder_id            =>  p_case_folder_id,
2158                 p_data_point_id             =>  105, -- last credit review date
2159                 p_data_point_value          =>  c_case_folder_rec.last_updated,
2160                 p_mode                      =>  p_mode,
2161                 p_error_msg                 =>  l_errmsg,
2162                 p_resultout                 =>  l_resultout);
2163         build_case_folder_details(
2164                 p_case_folder_id            =>  p_case_folder_id,
2165                 p_data_point_id             =>  106, -- last check list used
2166                 p_data_point_value          =>  c_case_folder_rec.check_list_id,
2167                 p_mode                      =>  p_mode,
2168                 p_error_msg                 =>  l_errmsg,
2169                 p_resultout                 =>  l_resultout);
2170         build_case_folder_details(
2171                 p_case_folder_id            =>  p_case_folder_id,
2172                 p_data_point_id             =>  107, -- last case folder
2173                 p_data_point_value          =>  c_case_folder_rec.case_folder_number,
2174                 p_mode                      =>  p_mode,
2175                 p_error_msg                 =>  l_errmsg,
2176                 p_resultout                 =>  l_resultout);
2177         exit;
2178    END LOOP;
2179     FOR c_case_folder_score_rec IN c_case_folder_score
2180     LOOP
2181          build_case_folder_details(
2182                 p_case_folder_id            =>  p_case_folder_id,
2183                 p_data_point_id             =>  30, -- last calculated credit score
2184                 p_data_point_value          =>  fnd_number.number_to_canonical(c_case_folder_score_rec.score),
2185                 p_mode                      =>  p_mode,
2186                 p_error_msg                 =>  l_errmsg,
2187                 p_resultout                 =>  l_resultout);
2188         exit;
2189     END LOOP;
2190     IF pg_wf_debug = 'Y'
2191     THEN
2192        ar_cmgt_util.wf_debug(p_case_folder_id,
2193               'ar_cmgt_data_points_pkg.getotherdatapoints()-');
2194     END IF;
2195 END;
2196 
2197 
2198 PROCEDURE populate_aging_data
2199         (p_case_folder_id       IN          NUMBER,
2200          p_mode                 IN          VARCHAR2 default 'CREATE',
2201          p_error_msg            OUT NOCOPY  VARCHAR2,
2202          p_resultout            OUT NOCOPY  VARCHAR2) IS
2203 
2204 CURSOR aging_cur  IS
2205   SELECT age.aging_bucket_id,age.aging_bucket_line_id
2206   FROM   AR_CMGT_SETUP_OPTIONS sys,
2207          ar_aging_bucket_lines age
2208   WHERE  sys.aging_bucket_id = age.aging_bucket_id;
2209 
2210   l_outstanding_balance NUMBER;
2211   l_dispute_amount   NUMBER;
2212   l_customer_id number;
2213   l_party_id number;
2214   l_cust_account_id number;
2215   l_customer_site_use_id number;
2216   l_as_of_date date;
2217   l_currency_code fnd_currencies.currency_code%TYPE;
2218   l_format_currency varchar2(15);
2219   l_credit_option varchar2(16);
2220   l_invoice_type_low ra_cust_trx_types.name%TYPE;
2224   l_bucket_titlebottom_0 ar_aging_bucket_lines.report_heading2%TYPE;
2221   l_invoice_type_high ra_cust_trx_types.name%TYPE;
2222   l_bucket_name ar_aging_buckets.bucket_name%TYPE;
2223   l_bucket_titletop_0 ar_aging_bucket_lines.report_heading1%TYPE;
2225   l_bucket_amount_0 number;
2226   l_bucket_titletop_1 ar_aging_bucket_lines.report_heading1%TYPE;
2227   l_bucket_titlebottom_1 ar_aging_bucket_lines.report_heading2%TYPE;
2228   l_bucket_amount_1 number;
2229   l_bucket_titletop_2 ar_aging_bucket_lines.report_heading1%TYPE;
2230   l_bucket_titlebottom_2 ar_aging_bucket_lines.report_heading2%TYPE;
2231   l_bucket_amount_2 number;
2232   l_bucket_titletop_3 ar_aging_bucket_lines.report_heading1%TYPE;
2233   l_bucket_titlebottom_3 ar_aging_bucket_lines.report_heading2%TYPE;
2234   l_bucket_amount_3 number;
2235   l_bucket_titletop_4 ar_aging_bucket_lines.report_heading1%TYPE;
2236   l_bucket_titlebottom_4 ar_aging_bucket_lines.report_heading2%TYPE;
2237   l_bucket_amount_4 number;
2238   l_bucket_titletop_5 ar_aging_bucket_lines.report_heading1%TYPE;
2239   l_bucket_titlebottom_5 ar_aging_bucket_lines.report_heading2%TYPE;
2240   l_bucket_amount_5 number;
2241   l_bucket_titletop_6 ar_aging_bucket_lines.report_heading1%TYPE;
2242   l_bucket_titlebottom_6 ar_aging_bucket_lines.report_heading2%TYPE;
2243   l_bucket_amount_6 number;
2244   l_outstanding_amount number;
2245 
2246   l_check_flag VARCHAR2(1);
2247   l_counter  NUMBER;
2248 
2249   l_credit_request_id  NUMBER;
2250   l_exchange_rate_type VARCHAR2(50);
2251 
2252 BEGIN
2253     IF pg_wf_debug = 'Y'
2254     THEN
2255        ar_cmgt_util.wf_debug(p_case_folder_id,
2256               'ar_cmgt_data_points_pkg.populate_aging_data()+');
2257     END IF;
2258 
2259   l_check_flag := 'Y';
2260 
2261   BEGIN
2262     SELECT DECODE(party_id,-99,NULL,party_id),
2263            DECODE(cust_account_id,-99,NULL,cust_account_id),
2264            DECODE(site_use_id,-99,NULL,site_use_id),
2265            limit_currency,
2266            credit_request_id
2267     INTO   l_party_id,
2268            l_cust_account_id,
2269            l_customer_site_use_id,
2270            l_currency_code,
2271            l_credit_request_id
2272     FROM   ar_cmgt_case_folders
2273   WHERE  case_folder_id = p_case_folder_id;
2274    EXCEPTION
2275      WHEN others THEN
2276      l_check_flag := 'N';
2277   END;
2278 
2279   BEGIN
2280       SELECT age.bucket_name,
2281              default_exchange_rate_type
2282       INTO   l_bucket_name,
2283              l_exchange_rate_type
2284       FROM   AR_CMGT_SETUP_OPTIONS sys,
2285              ar_aging_buckets age
2286       WHERE  sys.aging_bucket_id = age.aging_bucket_id;
2287    EXCEPTION
2288    WHEN others THEN
2289      l_check_flag := 'N';
2290   END;
2291 
2292   IF l_check_flag = 'Y' THEN
2293       AR_CMGT_AGING.calc_aging_buckets (
2294         p_party_id              => l_party_id,
2295         p_customer_id           => l_cust_account_id,
2296         p_site_use_id           => l_customer_site_use_id,
2297         p_currency_code         => l_currency_code,
2298         p_credit_option         => 'AGE',
2299         p_bucket_name           => l_bucket_name,
2300         p_org_id                => NULL,
2301         p_exchange_rate_type    => l_exchange_rate_type,
2302         p_outstanding_balance   => l_outstanding_balance,
2303         p_bucket_titletop_0     => l_bucket_titletop_0,
2304         p_bucket_titlebottom_0  => l_bucket_titlebottom_0,
2305         p_bucket_amount_0       => l_bucket_amount_0,
2306         p_bucket_titletop_1     => l_bucket_titletop_1,
2307         p_bucket_titlebottom_1  => l_bucket_titlebottom_1,
2308         p_bucket_amount_1       => l_bucket_amount_1,
2309         p_bucket_titletop_2     => l_bucket_titletop_2,
2310         p_bucket_titlebottom_2  => l_bucket_titlebottom_2,
2311         p_bucket_amount_2       => l_bucket_amount_2,
2312         p_bucket_titletop_3     => l_bucket_titletop_3,
2313         p_bucket_titlebottom_3  => l_bucket_titlebottom_3,
2314         p_bucket_amount_3       => l_bucket_amount_3,
2315         p_bucket_titletop_4     => l_bucket_titletop_4,
2316         p_bucket_titlebottom_4  => l_bucket_titlebottom_4,
2317         p_bucket_amount_4       => l_bucket_amount_4,
2318         p_bucket_titletop_5     => l_bucket_titletop_5,
2319         p_bucket_titlebottom_5  => l_bucket_titlebottom_5,
2320         p_bucket_amount_5       => l_bucket_amount_5,
2321         p_bucket_titletop_6     => l_bucket_titletop_6,
2322         p_bucket_titlebottom_6  => l_bucket_titlebottom_6,
2323         p_bucket_amount_6       => l_bucket_amount_6
2324        );
2325       l_counter := 0;
2326 
2327       FOR aging_rec IN aging_cur LOOP
2328 
2329         IF l_counter = 0  THEN
2330          IF p_mode = 'CREATE'
2331          THEN
2332             ar_cmgt_controls.populate_aging_dtls
2333                 (p_case_folder_id   => p_case_folder_id,
2334                  p_aging_bucket_id  => aging_rec.aging_bucket_id,
2335                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2336                  p_amount              =>  l_bucket_amount_0,
2337                  p_error_msg           => p_error_msg,
2338                  p_resultout           => p_resultout);
2339             IF p_resultout <> 0
2340             THEN
2341                 return;
2342             END IF;
2343             IF g_data_case_folder_exists = 'Y' -- for data record
2344             THEN
2345                 ar_cmgt_controls.update_aging_dtls
2346                 (p_case_folder_id   => g_data_case_folder_id,
2347                  p_aging_bucket_id  => aging_rec.aging_bucket_id,
2348                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2349                  p_amount              =>  l_bucket_amount_0,
2350                  p_error_msg           => p_error_msg,
2351                  p_resultout           => p_resultout);
2352             ELSIF g_data_case_folder_exists = 'N'
2353             THEN
2354                 ar_cmgt_controls.populate_aging_dtls
2355                 (p_case_folder_id   => g_data_case_folder_id,
2356                  p_aging_bucket_id  => aging_rec.aging_bucket_id,
2357                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2358                  p_amount              =>  l_bucket_amount_0,
2359                  p_error_msg           => p_error_msg,
2360                  p_resultout           => p_resultout);
2361             END IF;
2362             IF p_resultout <> 0
2363             THEN
2364                 return;
2365             END IF;
2366          ELSIF p_mode = 'REFRESH'
2367          THEN
2368                 ar_cmgt_controls.update_aging_dtls
2369                 (p_case_folder_id       => p_case_folder_id,
2370                  p_aging_bucket_id      => aging_rec.aging_bucket_id,
2371                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2372                  p_amount               =>  l_bucket_amount_0,
2373                  p_error_msg            => p_error_msg,
2374                  p_resultout            => p_resultout);
2375 
2376                 IF p_resultout <> 0
2377                 THEN
2378                     return;
2379                 END IF;
2380 
2381          END IF; -- end of p_mode if
2382         END IF;
2383         IF l_counter = 1  THEN
2384          IF p_mode = 'CREATE'
2385          THEN
2386             ar_cmgt_controls.populate_aging_dtls
2387                 (p_case_folder_id   => p_case_folder_id,
2388                  p_aging_bucket_id  => aging_rec.aging_bucket_id,
2389                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2390                  p_amount              =>  l_bucket_amount_1,
2391                  p_error_msg           => p_error_msg,
2392                  p_resultout           => p_resultout);
2393             IF p_resultout <> 0
2394             THEN
2395                 return;
2396             END IF;
2397             IF g_data_case_folder_exists = 'Y' -- for data record
2398             THEN
2399                 ar_cmgt_controls.update_aging_dtls
2400                 (p_case_folder_id   => g_data_case_folder_id,
2401                  p_aging_bucket_id  => aging_rec.aging_bucket_id,
2402                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2403                  p_amount              =>  l_bucket_amount_1,
2404                  p_error_msg           => p_error_msg,
2405                  p_resultout           => p_resultout);
2406             ELSIF g_data_case_folder_exists = 'N'
2407             THEN
2408                 ar_cmgt_controls.populate_aging_dtls
2409                 (p_case_folder_id   => g_data_case_folder_id,
2410                  p_aging_bucket_id  => aging_rec.aging_bucket_id,
2411                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2412                  p_amount              =>  l_bucket_amount_1,
2413                  p_error_msg           => p_error_msg,
2414                  p_resultout           => p_resultout);
2415             END IF;
2416             IF p_resultout <> 0
2417             THEN
2418                 return;
2419             END IF;
2420          ELSIF p_mode = 'REFRESH'
2421          THEN
2422                 ar_cmgt_controls.update_aging_dtls
2423                 (p_case_folder_id       => p_case_folder_id,
2424                  p_aging_bucket_id      => aging_rec.aging_bucket_id,
2425                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2426                  p_amount               =>  l_bucket_amount_1,
2427                  p_error_msg            => p_error_msg,
2428                  p_resultout            => p_resultout);
2429 
2430                 IF p_resultout <> 0
2431                 THEN
2432                     return;
2433                 END IF;
2434 
2435          END IF; -- end of p_mode if
2436         END IF;
2437         IF l_counter = 2  THEN
2438          IF p_mode = 'CREATE'
2439          THEN
2440             ar_cmgt_controls.populate_aging_dtls
2441                 (p_case_folder_id   => p_case_folder_id,
2442                  p_aging_bucket_id  => aging_rec.aging_bucket_id,
2443                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2444                  p_amount              =>  l_bucket_amount_2,
2445                  p_error_msg           => p_error_msg,
2446                  p_resultout           => p_resultout);
2447             IF p_resultout <> 0
2448             THEN
2449                 return;
2450             END IF;
2451             IF g_data_case_folder_exists = 'Y' -- for data record
2452             THEN
2453                 ar_cmgt_controls.update_aging_dtls
2454                 (p_case_folder_id   => g_data_case_folder_id,
2455                  p_aging_bucket_id  => aging_rec.aging_bucket_id,
2456                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2457                  p_amount              =>  l_bucket_amount_2,
2458                  p_error_msg           => p_error_msg,
2459                  p_resultout           => p_resultout);
2460             ELSIF g_data_case_folder_exists = 'N'
2461             THEN
2462                 ar_cmgt_controls.populate_aging_dtls
2463                 (p_case_folder_id   => g_data_case_folder_id,
2464                  p_aging_bucket_id  => aging_rec.aging_bucket_id,
2465                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2466                  p_amount              =>  l_bucket_amount_2,
2467                  p_error_msg           => p_error_msg,
2468                  p_resultout           => p_resultout);
2469             END IF;
2470             IF p_resultout <> 0
2471             THEN
2472                 return;
2473             END IF;
2474          ELSIF p_mode = 'REFRESH'
2475          THEN
2476                 ar_cmgt_controls.update_aging_dtls
2477                 (p_case_folder_id       => p_case_folder_id,
2478                  p_aging_bucket_id      => aging_rec.aging_bucket_id,
2479                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2480                  p_amount               =>  l_bucket_amount_2,
2481                  p_error_msg            => p_error_msg,
2482                  p_resultout            => p_resultout);
2483 
2484                 IF p_resultout <> 0
2485                 THEN
2486                     return;
2487                 END IF;
2488 
2489          END IF; -- end of p_mode if
2490         END IF;
2491         IF l_counter = 3  THEN
2492          IF p_mode = 'CREATE'
2493          THEN
2494             ar_cmgt_controls.populate_aging_dtls
2495                 (p_case_folder_id   => p_case_folder_id,
2496                  p_aging_bucket_id  => aging_rec.aging_bucket_id,
2497                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2498                  p_amount              =>  l_bucket_amount_3,
2499                  p_error_msg           => p_error_msg,
2500                  p_resultout           => p_resultout);
2501             IF p_resultout <> 0
2502             THEN
2503                 return;
2504             END IF;
2505             IF g_data_case_folder_exists = 'Y' -- for data record
2506             THEN
2507                 ar_cmgt_controls.update_aging_dtls
2508                 (p_case_folder_id   => g_data_case_folder_id,
2509                  p_aging_bucket_id  => aging_rec.aging_bucket_id,
2510                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2511                  p_amount              =>  l_bucket_amount_3,
2512                  p_error_msg           => p_error_msg,
2513                  p_resultout           => p_resultout);
2514             ELSIF g_data_case_folder_exists = 'N'
2515             THEN
2516                 ar_cmgt_controls.populate_aging_dtls
2517                 (p_case_folder_id   => g_data_case_folder_id,
2518                  p_aging_bucket_id  => aging_rec.aging_bucket_id,
2519                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2520                  p_amount              =>  l_bucket_amount_3,
2521                  p_error_msg           => p_error_msg,
2522                  p_resultout           => p_resultout);
2523             END IF;
2524             IF p_resultout <> 0
2525             THEN
2526                 return;
2527             END IF;
2528          ELSIF p_mode = 'REFRESH'
2529          THEN
2530                 ar_cmgt_controls.update_aging_dtls
2531                 (p_case_folder_id       => p_case_folder_id,
2532                  p_aging_bucket_id      => aging_rec.aging_bucket_id,
2533                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2534                  p_amount               =>  l_bucket_amount_3,
2535                  p_error_msg            => p_error_msg,
2536                  p_resultout            => p_resultout);
2537 
2538                 IF p_resultout <> 0
2539                 THEN
2540                     return;
2541                 END IF;
2542 
2543          END IF; -- end of p_mode if
2544         END IF;
2545         IF l_counter = 4  THEN
2546          IF p_mode = 'CREATE'
2547          THEN
2548             ar_cmgt_controls.populate_aging_dtls
2549                 (p_case_folder_id   => p_case_folder_id,
2550                  p_aging_bucket_id  => aging_rec.aging_bucket_id,
2551                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2552                  p_amount              =>  l_bucket_amount_4,
2553                  p_error_msg           => p_error_msg,
2554                  p_resultout           => p_resultout);
2555             IF p_resultout <> 0
2556             THEN
2557                 return;
2558             END IF;
2559             IF g_data_case_folder_exists = 'Y' -- for data record
2560             THEN
2561                 ar_cmgt_controls.update_aging_dtls
2562                 (p_case_folder_id   => g_data_case_folder_id,
2563                  p_aging_bucket_id  => aging_rec.aging_bucket_id,
2564                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2565                  p_amount              =>  l_bucket_amount_4,
2566                  p_error_msg           => p_error_msg,
2567                  p_resultout           => p_resultout);
2568             ELSIF g_data_case_folder_exists = 'N'
2569             THEN
2570                 ar_cmgt_controls.populate_aging_dtls
2571                 (p_case_folder_id   => g_data_case_folder_id,
2572                  p_aging_bucket_id  => aging_rec.aging_bucket_id,
2573                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2574                  p_amount              =>  l_bucket_amount_4,
2575                  p_error_msg           => p_error_msg,
2576                  p_resultout           => p_resultout);
2577             END IF;
2578             IF p_resultout <> 0
2579             THEN
2580                 return;
2581             END IF;
2582          ELSIF p_mode = 'REFRESH'
2583          THEN
2584                 ar_cmgt_controls.update_aging_dtls
2585                 (p_case_folder_id       => p_case_folder_id,
2586                  p_aging_bucket_id      => aging_rec.aging_bucket_id,
2587                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2588                  p_amount               =>  l_bucket_amount_4,
2589                  p_error_msg            => p_error_msg,
2590                  p_resultout            => p_resultout);
2591 
2592                 IF p_resultout <> 0
2593                 THEN
2594                     return;
2595                 END IF;
2596 
2597          END IF; -- end of p_mode if
2598         END IF;
2599         IF l_counter = 5  THEN
2600          IF p_mode = 'CREATE'
2601          THEN
2602             ar_cmgt_controls.populate_aging_dtls
2603                 (p_case_folder_id   => p_case_folder_id,
2604                  p_aging_bucket_id  => aging_rec.aging_bucket_id,
2605                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2606                  p_amount              =>  l_bucket_amount_5,
2607                  p_error_msg           => p_error_msg,
2608                  p_resultout           => p_resultout);
2609             IF p_resultout <> 0
2610             THEN
2611                 return;
2612             END IF;
2613             IF g_data_case_folder_exists = 'Y' -- for data record
2614             THEN
2615                 ar_cmgt_controls.update_aging_dtls
2616                 (p_case_folder_id   => g_data_case_folder_id,
2617                  p_aging_bucket_id  => aging_rec.aging_bucket_id,
2618                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2619                  p_amount              =>  l_bucket_amount_5,
2620                  p_error_msg           => p_error_msg,
2621                  p_resultout           => p_resultout);
2622             ELSIF g_data_case_folder_exists = 'N'
2623             THEN
2624                 ar_cmgt_controls.populate_aging_dtls
2625                 (p_case_folder_id   => g_data_case_folder_id,
2626                  p_aging_bucket_id  => aging_rec.aging_bucket_id,
2627                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2628                  p_amount              =>  l_bucket_amount_5,
2629                  p_error_msg           => p_error_msg,
2630                  p_resultout           => p_resultout);
2631             END IF;
2632             IF p_resultout <> 0
2633             THEN
2634                 return;
2635             END IF;
2636          ELSIF p_mode = 'REFRESH'
2637          THEN
2638                 ar_cmgt_controls.update_aging_dtls
2639                 (p_case_folder_id       => p_case_folder_id,
2640                  p_aging_bucket_id      => aging_rec.aging_bucket_id,
2641                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2642                  p_amount               =>  l_bucket_amount_5,
2643                  p_error_msg            => p_error_msg,
2644                  p_resultout            => p_resultout);
2645 
2646                 IF p_resultout <> 0
2647                 THEN
2648                     return;
2649                 END IF;
2650 
2651          END IF; -- end of p_mode if
2652         END IF;
2653         IF l_counter = 6  THEN
2654          IF p_mode = 'CREATE'
2655          THEN
2656             ar_cmgt_controls.populate_aging_dtls
2657                 (p_case_folder_id   => p_case_folder_id,
2658                  p_aging_bucket_id  => aging_rec.aging_bucket_id,
2659                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2660                  p_amount              =>  l_bucket_amount_6,
2661                  p_error_msg           => p_error_msg,
2662                  p_resultout           => p_resultout);
2663             IF p_resultout <> 0
2667             IF g_data_case_folder_exists = 'Y' -- for data record
2664             THEN
2665                 return;
2666             END IF;
2668             THEN
2669                 ar_cmgt_controls.update_aging_dtls
2670                 (p_case_folder_id   => g_data_case_folder_id,
2671                  p_aging_bucket_id  => aging_rec.aging_bucket_id,
2672                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2673                  p_amount              =>  l_bucket_amount_6,
2674                  p_error_msg           => p_error_msg,
2675                  p_resultout           => p_resultout);
2676             ELSIF g_data_case_folder_exists = 'N'
2677             THEN
2678                 ar_cmgt_controls.populate_aging_dtls
2679                 (p_case_folder_id   => g_data_case_folder_id,
2680                  p_aging_bucket_id  => aging_rec.aging_bucket_id,
2681                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2682                  p_amount              =>  l_bucket_amount_6,
2683                  p_error_msg           => p_error_msg,
2684                  p_resultout           => p_resultout);
2685             END IF;
2686             IF p_resultout <> 0
2687             THEN
2688                 return;
2689             END IF;
2690          ELSIF p_mode = 'REFRESH'
2691          THEN
2692                 ar_cmgt_controls.update_aging_dtls
2693                 (p_case_folder_id       => p_case_folder_id,
2694                  p_aging_bucket_id      => aging_rec.aging_bucket_id,
2695                  p_aging_bucket_line_id => aging_rec.aging_bucket_line_id,
2696                  p_amount               =>  l_bucket_amount_6,
2697                  p_error_msg            => p_error_msg,
2698                  p_resultout            => p_resultout);
2699 
2700                 IF p_resultout <> 0
2701                 THEN
2702                     return;
2703                 END IF;
2704 
2705          END IF; -- end of p_mode if
2706         END IF;
2707         l_counter := l_counter + 1;
2708       END LOOP;
2709      END IF;
2710 
2711     IF pg_wf_debug = 'Y'
2712     THEN
2713        ar_cmgt_util.wf_debug(p_case_folder_id,
2714               'ar_cmgt_data_points_pkg.populate_aging_data()-');
2715     END IF;
2716 end populate_aging_data;
2717 
2718 
2719 PROCEDURE populate_dnb_data(
2720             p_case_folder_id            IN      NUMBER,
2721             p_source_table_name         IN      VARCHAR2,
2722             p_source_key                IN      VARCHAR2,
2723             p_source_key_column_name    IN      VARCHAR2,
2724             p_mode                      IN      VARCHAR2 default 'CREATE',
2725             p_source_key_type           IN      VARCHAR2 default NULL,
2726             p_source_key_column_type    IN      VARCHAR2 default NULL) IS
2727 
2728 l_errmsg        VARCHAR2(4000);
2729 l_resultout     VARCHAR2(1);
2730 
2731 BEGIN
2732     IF pg_wf_debug = 'Y'
2733     THEN
2734        ar_cmgt_util.wf_debug(p_case_folder_id,
2735               'ar_cmgt_data_points_pkg.populate_dnb_data()+');
2736     END IF;
2737 
2738     -- call table handler to insert into ar_cmgt_cf_dnb_dtls.
2739     IF p_mode = 'CREATE'
2740     THEN
2741           AR_CMGT_CONTROLS.populate_dnb_data(
2742                   p_case_folder_id          => p_case_folder_id,
2743                   p_source_table_name       => p_source_table_name,
2744                   p_source_key              => p_source_key,
2745                   p_source_key_type         => p_source_key_type,
2746                   p_source_key_column_name  => p_source_key_column_name,
2747                   p_source_key_column_type  => p_source_key_column_type,
2748                   p_errmsg                  => l_errmsg,
2749                   p_resultout               => l_resultout);
2750 
2751     ELSIF p_mode = 'REFRESH'
2752     THEN
2753             UPDATE ar_cmgt_cf_dnb_dtls
2754                 SET  source_key  =  p_source_key,
2755                      last_updated_by = fnd_global.user_id,
2756                      last_update_date = sysdate,
2757                      last_update_login = fnd_global.login_id
2758             WHERE case_folder_id = p_case_folder_id
2759             AND   source_table_name = p_source_table_name
2760             AND   nvl(source_key,'X')        = nvl(p_source_key,'X')
2761             AND   nvl(source_key_type,'X') = nvl(p_source_key_type,'X')
2762             AND   nvl(source_key_column_name,'X') = nvl(p_source_key_column_name,'X')
2763             AND   nvl(source_key_column_type_name,'X') = nvl(p_source_key_column_type,'X');
2764             -- Bug fix for 3566584
2765             -- Dnb data won't be updated if it was not created
2766             -- as part of case folder generation. So if user
2767             -- purchased dnb data leter on then it will not be updated.
2768             IF ( SQL%NOTFOUND )
2769             THEN
2770                 AR_CMGT_CONTROLS.populate_dnb_data(
2771                     p_case_folder_id          => p_case_folder_id,
2772                     p_source_table_name       => p_source_table_name,
2773                     p_source_key              => p_source_key,
2774                     p_source_key_type         => p_source_key_type,
2775                     p_source_key_column_name  => p_source_key_column_name,
2776                     p_source_key_column_type  => p_source_key_column_type,
2777                     p_errmsg                  => l_errmsg,
2778                     p_resultout               => l_resultout);
2779             END IF;
2780 
2781 
2782     END IF;
2783     IF pg_wf_debug = 'Y'
2784     THEN
2785        ar_cmgt_util.wf_debug(p_case_folder_id,
2786               'ar_cmgt_data_points_pkg.populate_dnb_data()-');
2787     END IF;
2788 END;
2789 
2790 
2791 PROCEDURE  build_case_folder
2792             (p_party_id                 IN      NUMBER,
2793              p_cust_account_id          IN      NUMBER,
2794              p_cust_acct_site_id        IN      NUMBER,
2795              p_limit_currency           IN      VARCHAR2,
2796              p_exchange_rate_type       IN      VARCHAR2,
2797              p_check_list_id            IN      NUMBER default NULL,
2798              p_credit_request_id        IN      NUMBER default NULL,
2799              p_score_model_id           IN      NUMBER default NULL,
2800              p_credit_classification    IN      VARCHAR2 default NULL,
2801              p_review_type              IN      VARCHAR2 default NULL,
2802              p_case_folder_number       IN      VARCHAR2 default NULL,
2803              p_case_folder_id           OUT nocopy     NUMBER,
2804              p_error_msg                OUT nocopy     VARCHAR2,
2805              p_resultout                OUT nocopy     VARCHAR2) AS
2806 
2807 l_case_folder_number            ar_cmgt_case_folders.case_folder_number%TYPE;
2811         g_data_case_folder_exists := 'N';
2808 l_case_folder_id                ar_cmgt_case_folders.case_folder_id%TYPE;
2809 BEGIN
2810         p_resultout := 0;
2812         -- Get id from sequence
2813         SELECT  ar_cmgt_case_folders_s.nextval
2814         INTO    p_case_folder_id
2815         FROM    dual;
2816 
2817     IF pg_wf_debug = 'Y'
2818     THEN
2819        ar_cmgt_util.wf_debug(p_case_folder_id,
2820               'ar_cmgt_data_points_pkg.build_case_folder()+');
2821     END IF;
2822 
2823         -- check if case folder number is passed by calling program
2824         IF p_case_folder_number IS NULL
2825         THEN
2826              SELECT  ar_cmgt_case_folder_number_s.nextval
2827              INTO    l_case_folder_number
2828              FROM    dual;
2829         ELSE
2830             l_case_folder_number := p_case_folder_number;
2831         END IF;
2832         -- insert into case_folder table
2833         AR_CMGT_CONTROLS.populate_case_folder (
2834                    p_case_folder_id         =>  p_case_folder_id,
2835                    p_case_folder_number     =>  l_case_folder_number,
2836                    p_credit_request_id      =>  p_credit_request_id,
2837                    p_check_list_id          =>  p_check_list_id,
2838                    p_status                 =>  'O',
2839                    p_cust_account_id        =>  nvl(p_cust_account_id,-99),
2840                    p_party_id               =>  p_party_id,
2841                    p_cust_acct_site_id      =>  nvl(p_cust_acct_site_id,-99),
2842                    p_score_model_id         =>  p_score_model_id,
2843                    p_credit_classification  =>  p_credit_classification,
2844                    p_review_type            =>  p_review_type,
2845                    p_limit_currency         =>  p_limit_currency,
2846                    p_exchange_rate_type     =>  p_exchange_rate_type,
2847                    p_type                   =>  'CASE',
2848                    p_errmsg                 =>   p_error_msg,
2849                    p_resultout              =>   p_resultout);
2850         IF p_resultout <> 0
2851         THEN
2852             return;
2853         END IF;
2854         -- check whether there are any 'DATA' records for the party,account and site
2855         -- combination. If not exist insert a new record.
2856 
2857         BEGIN
2858             SELECT  case_folder_id
2859             INTO    g_data_case_folder_id
2860             FROM    ar_cmgt_case_folders
2861             WHERE   party_id = p_party_id
2862             AND     cust_account_id = p_cust_account_id
2863             AND     site_use_id     = p_cust_acct_site_id
2864             AND     credit_request_id = p_credit_request_id	-- Fix for bug 12613103
2865             -- AND     limit_currency  = p_limit_currency
2866             AND     type            = 'DATA';
2867 
2868             g_data_case_folder_exists := 'Y';
2869         -- update case folder number in case data exists
2870 
2871            UPDATE   ar_cmgt_case_folders
2872                set  case_folder_number = l_case_folder_number,
2873                     check_list_id = p_check_list_id,
2874                     score_model_id = p_score_model_id,
2875                     limit_currency = p_limit_currency,
2876                     exchange_rate_type = p_exchange_rate_type,
2877                     credit_classification = p_credit_classification,
2878                     last_update_date = SYSDATE,
2879                     last_updated_by = fnd_global.user_id,
2880                     last_update_login = fnd_global.login_id,
2881                     last_updated = sysdate,
2882                     credit_request_id = p_credit_request_id
2883             WHERE   party_id = p_party_id
2884             AND     cust_account_id = p_cust_account_id
2885             AND     site_use_id     = p_cust_acct_site_id
2886             AND     type            = 'DATA';
2887 
2888             g_data_case_folder_exists := 'Y';
2889         EXCEPTION
2890             WHEN NO_DATA_FOUND THEN
2891                BEGIN
2892                     SELECT  ar_cmgt_case_folders_s.nextval
2893                     INTO    g_data_case_folder_id
2894                     FROM    dual;
2895                     g_data_case_folder_exists := 'N';
2896                 AR_CMGT_CONTROLS.populate_case_folder (
2897                      p_case_folder_id       => g_data_case_folder_id,
2898                      p_case_folder_number   => l_case_folder_number,
2899                      p_credit_request_id    =>  p_credit_request_id,
2900                      p_check_list_id          =>  p_check_list_id,
2901                      p_party_id             => p_party_id,
2902                      p_cust_account_id      => p_cust_account_id,
2903                      p_cust_acct_site_id    => p_cust_acct_site_id,
2904                      p_score_model_id         =>  p_score_model_id,
2905                      p_credit_classification => p_credit_classification,
2906                      p_type                 => 'DATA',
2907                      p_limit_currency       =>  p_limit_currency,
2908                      p_exchange_rate_type   =>  p_exchange_rate_type,
2909                      p_errmsg               => p_error_msg,
2910                      p_resultout            => p_resultout);
2911 
2912 
2913                     IF p_resultout <> 0
2914                     THEN
2915                         return;
2916                     END IF;
2917                END;
2918             WHEN OTHERS THEN
2919                  p_error_msg := 'Unable to create case folder DATA records for' ||
2920                                 'party id '|| to_char(p_party_id) ||' Cust Account Id '||
2921                                 to_char(p_cust_account_id)||' Cust Account Site Use Id '||
2922                                 to_char(p_cust_acct_site_id) ||'Sql Error '||sqlerrm;
2923                  p_resultout := '1';
2924                  ar_cmgt_util.wf_debug(p_case_folder_id, p_error_msg);
2925         END;
2926 
2927     IF pg_wf_debug = 'Y'
2928     THEN
2929        ar_cmgt_util.wf_debug(p_case_folder_id,
2930                       'CR:' || p_credit_request_id || ' => CF:' ||
2931                             p_case_folder_id);
2932        ar_cmgt_util.wf_debug(p_case_folder_id,
2933               'ar_cmgt_data_points_pkg.build_case_folder()-');
2934     END IF;
2935 
2936 END build_case_folder;
2937 
2938 PROCEDURE BUILD_DNB_SCORABLE_FIN_DATA (
2939   p_case_folder_id      IN    NUMBER,
2940   p_mode            IN    VARCHAR2 default 'CREATE',
2941   p_financial_report_id   IN    NUMBER,
2942   p_financial_report_type   IN    VARCHAR2,
2943   p_error_msg             OUT nocopy     VARCHAR2,
2944   p_resultout             OUT nocopy     VARCHAR2)  IS
2945 
2946   CURSOR cFinancialReports IS
2947         SELECT  ESTIMATED_IND,
2948           CONSOLIDATED_IND,
2949           REPORT_START_DATE,
2950           REPORT_END_DATE,
2951         DATE_REPORT_ISSUED,
2952         AUDIT_IND,
2953         FORECAST_IND,
2954         FISCAL_IND,
2955         FINAL_IND,
2956         SIGNED_BY_PRINCIPALS_IND,
2957         RESTATED_IND,
2958         UNBALANCED_IND,
2959         QUALIFIED_IND,
2960         OPENING_IND,
2961         PROFORMA_IND,
2962         TRIAL_BALANCE_IND
2963     FROM hz_financial_reports
2964     WHERE  financial_report_id = p_financial_report_id;
2965 
2966   l_ESTIMATED_IND     hz_financial_reports.ESTIMATED_IND%type;
2967   l_CONSOLIDATED_IND    hz_financial_reports.CONSOLIDATED_IND%type;
2968   l_REPORT_START_DATE   hz_financial_reports.REPORT_START_DATE%type;
2969   l_REPORT_END_DATE     hz_financial_reports.REPORT_END_DATE%type;
2970   l_DATE_REPORT_ISSUED    hz_financial_reports.DATE_REPORT_ISSUED%type;
2971   l_AUDIT_IND       hz_financial_reports.AUDIT_IND%type;
2972   l_FORECAST_IND      hz_financial_reports.FORECAST_IND%type;
2973   l_FISCAL_IND        hz_financial_reports.FISCAL_IND%type;
2974   l_FINAL_IND       hz_financial_reports.FINAL_IND%type;
2975   l_SIGNED_BY_PRINCIPALS_IND  hz_financial_reports.SIGNED_BY_PRINCIPALS_IND%type;
2976   l_RESTATED_IND      hz_financial_reports.RESTATED_IND%type;
2977   l_UNBALANCED_IND      hz_financial_reports.UNBALANCED_IND%type;
2978   l_QUALIFIED_IND     hz_financial_reports.QUALIFIED_IND%type;
2979   l_OPENING_IND       hz_financial_reports.OPENING_IND%type;
2980   l_PROFORMA_IND      hz_financial_reports.PROFORMA_IND%type;
2981   l_TRIAL_BALANCE_IND   hz_financial_reports.TRIAL_BALANCE_IND%type;
2982 
2983 BEGIN
2984     IF pg_wf_debug = 'Y'
2985     THEN
2986        ar_cmgt_util.wf_debug(p_case_folder_id,
2987               'ar_cmgt_data_points_pkg.build_dnb_scorable_fin_data()+');
2988     END IF;
2989 
2990     FOR cFinancialReportsRec IN cFinancialReports
2991     LOOP
2992       l_ESTIMATED_IND     := cFinancialReportsRec.ESTIMATED_IND;
2993       l_CONSOLIDATED_IND    := cFinancialReportsRec.CONSOLIDATED_IND;
2994       l_REPORT_START_DATE   := cFinancialReportsRec.REPORT_START_DATE;
2995       l_REPORT_END_DATE     := cFinancialReportsRec.REPORT_END_DATE;
2996       l_DATE_REPORT_ISSUED    := cFinancialReportsRec.DATE_REPORT_ISSUED;
2997       l_AUDIT_IND       := cFinancialReportsRec.AUDIT_IND;
2998       l_FORECAST_IND      := cFinancialReportsRec.FORECAST_IND;
2999       l_FISCAL_IND        := cFinancialReportsRec.FISCAL_IND;
3000       l_FINAL_IND       := cFinancialReportsRec.FINAL_IND;
3001       l_SIGNED_BY_PRINCIPALS_IND  := cFinancialReportsRec.SIGNED_BY_PRINCIPALS_IND;
3002       l_RESTATED_IND      := cFinancialReportsRec.RESTATED_IND;
3003       l_UNBALANCED_IND      := cFinancialReportsRec.UNBALANCED_IND;
3004       l_QUALIFIED_IND     := cFinancialReportsRec.QUALIFIED_IND;
3005       l_OPENING_IND       := cFinancialReportsRec.OPENING_IND;
3006       l_PROFORMA_IND      := cFinancialReportsRec.PROFORMA_IND;
3007       l_TRIAL_BALANCE_IND   := cFinancialReportsRec.TRIAL_BALANCE_IND;
3008     END LOOP;
3009 
3010     IF p_financial_report_type = 'BALANCE_SHEET'
3011     THEN
3012       build_case_folder_details(
3013             p_case_folder_id  => p_case_folder_id,
3014             p_data_point_id   => 11099,
3015             p_data_point_value => l_AUDIT_IND,
3016             p_mode             => p_mode,
3017             p_error_msg   => p_error_msg,
3018             p_resultout   => p_resultout);
3019 
3020       build_case_folder_details(
3021             p_case_folder_id  => p_case_folder_id,
3022             p_data_point_id   => 11100,
3023             p_data_point_value => l_CONSOLIDATED_IND,
3024             p_mode             => p_mode,
3025             p_error_msg   => p_error_msg,
3026             p_resultout   => p_resultout);
3027 
3028           build_case_folder_details(
3029             p_case_folder_id  => p_case_folder_id,
3030             p_data_point_id   => 11101,
3031             p_data_point_value => l_ESTIMATED_IND,
3032             p_mode             => p_mode,
3033             p_error_msg   => p_error_msg,
3034             p_resultout   => p_resultout);
3035 
3036           build_case_folder_details(
3037             p_case_folder_id  => p_case_folder_id,
3038             p_data_point_id   => 11103,
3039             p_data_point_value => l_FISCAL_IND,
3040             p_mode             => p_mode,
3041             p_error_msg   => p_error_msg,
3042             p_resultout   => p_resultout);
3043 
3044       build_case_folder_details(
3045             p_case_folder_id  => p_case_folder_id,
3046             p_data_point_id   => 11102,
3047             p_data_point_value => l_FORECAST_IND,
3048             p_mode             => p_mode,
3049             p_error_msg   => p_error_msg,
3050             p_resultout   => p_resultout);
3051 
3052           build_case_folder_details(
3053             p_case_folder_id  => p_case_folder_id,
3054             p_data_point_id   => 11109,
3055             p_data_point_value => l_OPENING_IND,
3056             p_mode             => p_mode,
3057             p_error_msg   => p_error_msg,
3058             p_resultout   => p_resultout);
3059 
3060           build_case_folder_details(
3061             p_case_folder_id  => p_case_folder_id,
3062             p_data_point_id   => 11110,
3063             p_data_point_value => l_PROFORMA_IND,
3064             p_mode             => p_mode,
3065             p_error_msg   => p_error_msg,
3066             p_resultout   => p_resultout);
3067 
3068           build_case_folder_details(
3069             p_case_folder_id  => p_case_folder_id,
3070             p_data_point_id   => 11108,
3071             p_data_point_value => l_QUALIFIED_IND,
3072             p_mode             => p_mode,
3073             p_error_msg   => p_error_msg,
3074             p_resultout   => p_resultout);
3075 
3076       build_case_folder_details(
3077             p_case_folder_id  => p_case_folder_id,
3078             p_data_point_id   => 11106,
3079             p_data_point_value => l_RESTATED_IND,
3080             p_mode             => p_mode,
3081             p_error_msg   => p_error_msg,
3082             p_resultout   => p_resultout);
3083 
3084           build_case_folder_details(
3085             p_case_folder_id  => p_case_folder_id,
3086             p_data_point_id   => 11105,
3087             p_data_point_value => l_SIGNED_BY_PRINCIPALS_IND,
3088             p_mode             => p_mode,
3089             p_error_msg   => p_error_msg,
3090             p_resultout   => p_resultout);
3091 
3092           build_case_folder_details(
3093             p_case_folder_id  => p_case_folder_id,
3094             p_data_point_id   => 11111,
3095             p_data_point_value => l_TRIAL_BALANCE_IND,
3096             p_mode             => p_mode,
3097             p_error_msg   => p_error_msg,
3098             p_resultout   => p_resultout);
3099 
3100           build_case_folder_details(
3101             p_case_folder_id  => p_case_folder_id,
3102             p_data_point_id   => 11107,
3103             p_data_point_value => l_UNBALANCED_IND,
3104             p_mode             => p_mode,
3105             p_error_msg   => p_error_msg,
3106             p_resultout   => p_resultout);
3107         ELSIF p_financial_report_type = 'INCOME'
3108         THEN
3109             build_case_folder_details(
3110             p_case_folder_id  => p_case_folder_id,
3111             p_data_point_id   => 11096,
3112             p_data_point_value => l_DATE_REPORT_ISSUED,
3113             p_mode             => p_mode,
3114             p_error_msg   => p_error_msg,
3115             p_resultout   => p_resultout);
3116 
3117             build_case_folder_details(
3118             p_case_folder_id  => p_case_folder_id,
3119             p_data_point_id   => 11094,
3120             p_data_point_value => l_REPORT_START_DATE,
3121             p_mode             => p_mode,
3122             p_error_msg   => p_error_msg,
3123             p_resultout   => p_resultout);
3124 
3125             build_case_folder_details(
3126             p_case_folder_id  => p_case_folder_id,
3127             p_data_point_id   => 11095,
3128             p_data_point_value => l_REPORT_END_DATE,
3129             p_mode             => p_mode,
3130             p_error_msg   => p_error_msg,
3131             p_resultout   => p_resultout);
3132         ELSIF p_financial_report_type = 'ANNUAL_SALES'
3133         THEN
3134             build_case_folder_details(
3135             p_case_folder_id  => p_case_folder_id,
3136             p_data_point_id   => 11087,
3137             p_data_point_value => l_CONSOLIDATED_IND,
3138             p_mode             => p_mode,
3139             p_error_msg   => p_error_msg,
3140             p_resultout   => p_resultout);
3141 
3142             build_case_folder_details(
3143             p_case_folder_id  => p_case_folder_id,
3144             p_data_point_id   => 11086,
3145             p_data_point_value => l_ESTIMATED_IND,
3146             p_mode             => p_mode,
3147             p_error_msg   => p_error_msg,
3148             p_resultout   => p_resultout);
3149 
3150     ELSIF p_financial_report_type = 'TANGIBLE'
3151         THEN
3152             build_case_folder_details(
3153             p_case_folder_id  => p_case_folder_id,
3154             p_data_point_id   => 11080,
3155             p_data_point_value => l_ESTIMATED_IND,
3156             p_mode             => p_mode,
3157             p_error_msg   => p_error_msg,
3158             p_resultout   => p_resultout);
3159 
3160     END IF;
3161     IF pg_wf_debug = 'Y'
3162     THEN
3163        ar_cmgt_util.wf_debug(p_case_folder_id,
3164               'ar_cmgt_data_points_pkg.build_dnb_scorable_fin_data()-');
3165     END IF;
3166 END;
3167 
3168 PROCEDURE BUILD_DNB_SCOREABLE_DATA (
3169     p_case_folder_id    IN      NUMBER,
3170     p_mode              IN      VARCHAR2 default 'CREATE',
3171     p_org_profile_id    IN      NUMBER,
3172     p_credit_rating_id  IN      NUMBER,
3173     p_location_id   IN    NUMBER,
3174     p_balance_sheet_id  IN      NUMBER,
3175     p_income_statement_id IN    NUMBER,
3176     p_tangible_net_worth_id IN  NUMBER,
3177     p_annual_sales_volume_id  IN    NUMBER,
3178     p_resultout         OUT nocopy     VARCHAR2,
3179     p_error_msg         OUT nocopy     VARCHAR2) IS
3180 
3181     l_control_yr            hz_organization_profiles.control_yr%type;
3182     l_incorp_year           hz_organization_profiles.incorp_year%type;
3183     l_year_established      hz_organization_profiles.year_established%type;
3184     l_employees_total       hz_organization_profiles.employees_total%type;
3188     l_TOTAL_EMPLOYEES_IND hz_organization_profiles.TOTAL_EMPLOYEES_IND%type;
3185     l_total_payments        hz_organization_profiles.total_payments%type;
3186     l_maximum_credit_reco   hz_organization_profiles.maximum_credit_recommendation%type;
3187     l_oob_ind               hz_organization_profiles.oob_ind%type;
3189         l_SIC_CODE        hz_organization_profiles.SIC_CODE%type;
3190         l_RENT_OWN_IND      hz_organization_profiles.RENT_OWN_IND%type;
3191         l_REGISTRATION_TYPE   hz_organization_profiles.REGISTRATION_TYPE%type;
3192         l_LEGAL_STATUS      hz_organization_profiles.LEGAL_STATUS%type;
3193         l_HQ_BRANCH_IND     hz_organization_profiles.HQ_BRANCH_IND%type;
3194         l_BRANCH_FLAG     hz_organization_profiles.BRANCH_FLAG%type;
3195         l_LOCAL_ACTIVITY_CODE_TYPE  hz_organization_profiles.LOCAL_ACTIVITY_CODE_TYPE%type;
3196         l_LOCAL_ACTIVITY_CODE hz_organization_profiles.LOCAL_ACTIVITY_CODE%type;
3197         l_SIC_CODE_TYPE     hz_organization_profiles.SIC_CODE_TYPE%type;
3198         l_GLOBAL_FAILURE_SCORE  hz_organization_profiles.GLOBAL_FAILURE_SCORE%type;
3199         l_IMPORT_IND      hz_organization_profiles.IMPORT_IND%type;
3200         l_DUNS_NUMBER_C     hz_organization_profiles.DUNS_NUMBER_C%type;
3201         l_PARENT_SUB_IND    hz_organization_profiles.PARENT_SUB_IND%type;
3202         l_FAILURE_SCORE     hz_organization_profiles.FAILURE_SCORE%type;
3203         l_FAILURE_SCORE_COMMENTARY  hz_organization_profiles.FAILURE_SCORE_COMMENTARY%type;
3204         l_TOTAL_EMP_EST_IND   hz_organization_profiles.TOTAL_EMP_EST_IND%type;
3205 
3206     l_PAYDEX_SCORE                      Number;
3207     l_PAYDEX_THREE_MONTHS_AGO           Number;
3208     l_AVG_HIGH_CREDIT                   HZ_CREDIT_RATINGS.AVG_HIGH_CREDIT%type;
3209     l_HIGH_CREDIT                       HZ_CREDIT_RATINGS.HIGH_CREDIT%type;
3210     l_CREDIT_SCORE_CLASS                HZ_CREDIT_RATINGS.CREDIT_SCORE_CLASS%type;
3211     l_CREDIT_SCORE_NATL_PERCENTILE      HZ_CREDIT_RATINGS.CREDIT_SCORE_NATL_PERCENTILE%type;
3212     l_CREDIT_SCORE_INCD_DEFAULT         HZ_CREDIT_RATINGS.CREDIT_SCORE_INCD_DEFAULT%type;
3213     l_CREDIT_SCORE_AGE                  HZ_CREDIT_RATINGS.CREDIT_SCORE_AGE%type;
3214     l_FAILURE_SCORE_CLASS               HZ_CREDIT_RATINGS.FAILURE_SCORE_CLASS%type;
3215     l_FAILURE_SCORE_NATNL_PERCENT       HZ_CREDIT_RATINGS.FAILURE_SCORE_NATNL_PERCENTILE%type;
3216     l_FAILURE_SCORE_INCD_DEFAULT        HZ_CREDIT_RATINGS.FAILURE_SCORE_INCD_DEFAULT%type;
3217     l_FAILURE_SCORE_AGE                 HZ_CREDIT_RATINGS.FAILURE_SCORE_AGE%type;
3218     l_LOW_RNG_DELQ_SCR                  HZ_CREDIT_RATINGS.LOW_RNG_DELQ_SCR%type;
3219     l_HIGH_RNG_DELQ_SCR                 HZ_CREDIT_RATINGS.HIGH_RNG_DELQ_SCR%type;
3220     l_DELQ_PMT_RNG_PRCNT                HZ_CREDIT_RATINGS.DELQ_PMT_RNG_PRCNT%type;
3221     l_DELQ_PMT_PCTG_FOR_ALL_FIRMS       HZ_CREDIT_RATINGS.DELQ_PMT_PCTG_FOR_ALL_FIRMS%type;
3222     l_NUM_TRADE_EXPERIENCES             HZ_CREDIT_RATINGS.NUM_TRADE_EXPERIENCES%type;
3223     l_DEBARMENTS_COUNT                  HZ_CREDIT_RATINGS.NUM_SPCL_EVENT%type;
3224     l_BANKRUPTCY_IND                    HZ_CREDIT_RATINGS.BANKRUPTCY_IND%type;
3225     l_DEBARMENT_IND                     HZ_CREDIT_RATINGS.DEBARMENT_IND%type;
3226     l_BUSINESS_DISCONTINUED       HZ_CREDIT_RATINGS.BUSINESS_DISCONTINUED%type;
3227   l_NUM_SPCL_EVENT          HZ_CREDIT_RATINGS.NUM_SPCL_EVENT%type;
3228   l_MAXIMUM_CREDIT_CURRENCY_CODE    HZ_CREDIT_RATINGS.MAXIMUM_CREDIT_CURRENCY_CODE%type;
3229   l_CREDIT_SCORE            HZ_CREDIT_RATINGS.CREDIT_SCORE%type;
3230   l_CREDIT_SCORE_OVERRIDE_CODE    HZ_CREDIT_RATINGS.CREDIT_SCORE_OVERRIDE_CODE%type;
3231   l_PRNT_BKCY_CHAPTER_CONV      HZ_CREDIT_RATINGS.PRNT_BKCY_CHAPTER_CONV%type;
3232   l_NUM_PRNT_BKCY_CONVS       HZ_CREDIT_RATINGS.NUM_PRNT_BKCY_CONVS%type;
3233   l_PRNT_BKCY_FILG_CHAPTER      HZ_CREDIT_RATINGS.PRNT_BKCY_FILG_CHAPTER%type;
3234   l_PRNT_BKCY_FILG_TYPE       HZ_CREDIT_RATINGS.PRNT_BKCY_FILG_TYPE%type;
3235   l_NUM_PRNT_BKCY_FILING        HZ_CREDIT_RATINGS.NUM_PRNT_BKCY_FILING%type;
3236   l_NO_TRADE_IND            HZ_CREDIT_RATINGS.NO_TRADE_IND%type;
3237   l_JUDGEMENT_IND           HZ_CREDIT_RATINGS.JUDGEMENT_IND%type;
3238   l_LIEN_IND              HZ_CREDIT_RATINGS.LIEN_IND%type;
3239   l_SUIT_IND              HZ_CREDIT_RATINGS.SUIT_IND%type;
3240   l_PAYDEX_INDUSTRY_DAYS        HZ_CREDIT_RATINGS.PAYDEX_INDUSTRY_DAYS%type;
3241   l_FINCL_LGL_EVENT_IND       HZ_CREDIT_RATINGS.FINCL_LGL_EVENT_IND%type;
3242   l_DISASTER_IND            HZ_CREDIT_RATINGS.DISASTER_IND%type;
3243   l_CRIMINAL_PROCEEDING_IND     HZ_CREDIT_RATINGS.CRIMINAL_PROCEEDING_IND%type;
3244   l_FINCL_EMBT_IND          HZ_CREDIT_RATINGS.FINCL_EMBT_IND%type;
3245   l_PAYDEX_NORM           HZ_CREDIT_RATINGS.PAYDEX_NORM%type;
3246   l_RATING              HZ_CREDIT_RATINGS.RATING%type;
3247   l_SECURED_FLNG_IND          HZ_CREDIT_RATINGS.SECURED_FLNG_IND%type;
3248   l_CLAIMS_IND            HZ_CREDIT_RATINGS.CLAIMS_IND%type;
3249   l_SUIT_JUDGE_IND          HZ_CREDIT_RATINGS.SUIT_JUDGE_IND%type;
3250   l_DET_HISTORY_IND         HZ_CREDIT_RATINGS.DET_HISTORY_IND%type;
3251   l_OTHER_SPEC_EVNT_IND       HZ_CREDIT_RATINGS.OTHER_SPEC_EVNT_IND%type;
3252   l_OPRG_SPEC_EVNT_IND        HZ_CREDIT_RATINGS.OPRG_SPEC_EVNT_IND%type;
3253   l_CREDIT_SCORE_COMMENTARY     HZ_CREDIT_RATINGS.CREDIT_SCORE_COMMENTARY%type;
3254   l_country             hz_locations.country%type;
3255 
3256 
3257     l_cash_liqu_asset                   NUMBER;
3258     l_ar                                NUMBER;
3259     l_ap                                NUMBER;
3260     l_an_sales_vol                      NUMBER;
3261     l_auth_cap                          NUMBER;
3262     l_cost_of_sales                     NUMBER;
3263     l_current_ratio                     NUMBER;
3264     l_dividends                         NUMBER;
3268     l_inventory                         NUMBER;
3265     l_fixed_assets                      NUMBER;
3266     l_gross_inc                         NUMBER;
3267     l_intg_asset                        NUMBER;
3269     l_iss_capital                       NUMBER;
3270     l_long_debt                         NUMBER;
3271     l_net_income                        NUMBER;
3272     l_net_worth                         NUMBER;
3273     l_nom_capital                       NUMBER;
3274     l_paid_in_capital                   NUMBER;
3275     l_prev_sales                        NUMBER;
3276     l_prev_net_worth                    NUMBER;
3277     l_prev_work_cap                     NUMBER;
3278     l_profit_bef_tax                    NUMBER;
3279     l_quick_ratio                       NUMBER;
3280     l_ret_earnings                      NUMBER;
3281     l_sales                             NUMBER;
3282     l_tang_net_worth                    NUMBER;
3283     l_tot_assets                        NUmBER;
3284     l_tot_curr_assets                   NUMBER;
3285     l_tot_curr_liab                     NUMBER;
3286     l_tot_liab                          NUMBER;
3287     l_tot_liab_equ                      NUMBER;
3288     l_tot_long_liab                     NUMBER;
3289 
3290 
3291     CURSOR cDNBSore IS
3292         SELECT data_element_id, source_table_name, source_column_name
3293         FROM ar_cmgt_dnb_elements_vl
3294         WHERE scorable_flag = 'Y';
3295 
3296     CURSOR cOrgProf IS
3297         SELECT control_yr,
3298                incorp_year,
3299                year_established,
3300                employees_total,
3301                total_payments,
3302                maximum_credit_recommendation,
3303                oob_ind,
3304                TOTAL_EMP_EST_IND
3305                TOTAL_EMPLOYEES_IND,
3306                SIC_CODE,
3307                RENT_OWN_IND,
3308                REGISTRATION_TYPE,
3309                LEGAL_STATUS,
3310                HQ_BRANCH_IND,
3311                BRANCH_FLAG,
3312                LOCAL_ACTIVITY_CODE_TYPE,
3313                LOCAL_ACTIVITY_CODE,
3314                SIC_CODE_TYPE,
3315                GLOBAL_FAILURE_SCORE,
3316                IMPORT_IND,
3317                DUNS_NUMBER_C,
3318                TOTAL_EMP_EST_IND,
3319                PARENT_SUB_IND,
3320                FAILURE_SCORE,
3321                FAILURE_SCORE_COMMENTARY
3322 
3323         FROM   hz_organization_profiles
3324         WHERE  organization_profile_id = p_org_profile_id;
3325 
3326     CURSOR cCrRatings IS
3327         SELECT  decode(PAYDEX_SCORE,'UN', null, paydex_score ) paydex_score,
3328                 decode(paydex_three_months_ago, 'UN', null, paydex_three_months_ago ) paydex_three_months_ago,
3329                 AVG_HIGH_CREDIT,
3330                 HIGH_CREDIT,
3331                 CREDIT_SCORE_NATL_PERCENTILE,
3332                 CREDIT_SCORE_INCD_DEFAULT,
3333                 CREDIT_SCORE_AGE,
3334                 FAILURE_SCORE_CLASS,
3335                 FAILURE_SCORE_NATNL_PERCENTILE,
3336                 FAILURE_SCORE_INCD_DEFAULT,
3337                 FAILURE_SCORE_AGE,
3338                 LOW_RNG_DELQ_SCR,
3339                 HIGH_RNG_DELQ_SCR,
3340                 DELQ_PMT_RNG_PRCNT,
3341                 DELQ_PMT_PCTG_FOR_ALL_FIRMS,
3342                 NUM_TRADE_EXPERIENCES,
3343                 DEBARMENTS_COUNT,
3344                 BANKRUPTCY_IND,
3345                 DEBARMENT_IND,
3346                 BUSINESS_DISCONTINUED,
3347         NUM_SPCL_EVENT,
3348         MAXIMUM_CREDIT_CURRENCY_CODE,
3349         CREDIT_SCORE,
3350         CREDIT_SCORE_CLASS,
3351         CREDIT_SCORE_OVERRIDE_CODE,
3352         PRNT_BKCY_CHAPTER_CONV,
3353         NUM_PRNT_BKCY_CONVS,
3354         PRNT_BKCY_FILG_CHAPTER,
3355         PRNT_BKCY_FILG_TYPE,
3356         NUM_PRNT_BKCY_FILING,
3357         NO_TRADE_IND,
3358         JUDGEMENT_IND,
3359         LIEN_IND,
3360         SUIT_IND,
3361         PAYDEX_INDUSTRY_DAYS,
3362         FINCL_LGL_EVENT_IND,
3363         DISASTER_IND,
3364         CRIMINAL_PROCEEDING_IND,
3365         FINCL_EMBT_IND,
3366         PAYDEX_NORM,
3367         RATING,
3368         SECURED_FLNG_IND,
3369         CLAIMS_IND,
3370         SUIT_JUDGE_IND,
3371         DET_HISTORY_IND,
3372         OTHER_SPEC_EVNT_IND,
3373         OPRG_SPEC_EVNT_IND,
3374         CREDIT_SCORE_COMMENTARY
3375         FROM HZ_CREDIT_RATINGS
3376         WHERE  credit_rating_id = p_credit_rating_id;
3377 
3378     CURSOR cFinNum  IS
3379         SELECT FINANCIAL_number, financial_number_name
3380         FROM   hz_financial_numbers
3381         WHERE  financial_report_id in ( SELECT source_key
3382                             from ar_cmgt_cf_dnb_dtls
3383                             WHERE  source_table_name = 'HZ_FINANCIAL_REPORTS'
3384                             and    case_folder_id = p_case_folder_id);
3385 
3386     CURSOR cGetLocation IS
3387       SELECT COUNTRY
3388       FROM   hz_locations
3389       where  location_id = p_location_id;
3390 
3391 BEGIN
3392     IF pg_wf_debug = 'Y'
3393     THEN
3394        ar_cmgt_util.wf_debug(p_case_folder_id,
3395               'ar_cmgt_data_points_pkg.build_dnb_scorable_data()+');
3396     END IF;
3397 
3398    FOR cOrgProfRec IN cOrgProf
3399    LOOP
3400         l_control_yr            := cOrgProfRec.control_yr;
3401         l_incorp_year           := cOrgProfRec.incorp_year;
3402         l_year_established      := cOrgProfRec.year_established;
3403         l_employees_total       := cOrgProfRec.employees_total;
3404         l_total_payments        := cOrgProfRec.total_payments;
3408 
3405         l_maximum_credit_reco   := cOrgProfRec.maximum_credit_recommendation;
3406         l_oob_ind               := cOrgProfRec.oob_ind;
3407 
3409         l_TOTAL_EMPLOYEES_IND := cOrgProfRec.TOTAL_EMPLOYEES_IND;
3410         l_SIC_CODE        := cOrgProfRec.SIC_CODE;
3411         l_RENT_OWN_IND      := cOrgProfRec.RENT_OWN_IND;
3412         l_REGISTRATION_TYPE   := cOrgProfRec.REGISTRATION_TYPE;
3413         l_LEGAL_STATUS      := cOrgProfRec.LEGAL_STATUS;
3414         l_HQ_BRANCH_IND     := cOrgProfRec.HQ_BRANCH_IND;
3415         l_BRANCH_FLAG     := cOrgProfRec.BRANCH_FLAG;
3416         l_LOCAL_ACTIVITY_CODE_TYPE  := cOrgProfRec.LOCAL_ACTIVITY_CODE_TYPE;
3417         l_LOCAL_ACTIVITY_CODE := cOrgProfRec.LOCAL_ACTIVITY_CODE;
3418         l_SIC_CODE_TYPE     := cOrgProfRec.SIC_CODE_TYPE;
3419         l_GLOBAL_FAILURE_SCORE  := cOrgProfRec.GLOBAL_FAILURE_SCORE;
3420         l_IMPORT_IND      := cOrgProfRec.IMPORT_IND;
3421         l_DUNS_NUMBER_C     := cOrgProfRec.DUNS_NUMBER_C;
3422         l_PARENT_SUB_IND    := cOrgProfRec.PARENT_SUB_IND;
3423         l_FAILURE_SCORE     := cOrgProfRec.FAILURE_SCORE;
3424         l_FAILURE_SCORE_COMMENTARY  := cOrgProfRec.FAILURE_SCORE_COMMENTARY;
3425         l_TOTAL_EMP_EST_IND   := cOrgProfRec.TOTAL_EMP_EST_IND;
3426 
3427    END LOOP;
3428 
3429     build_case_folder_details(
3430           p_case_folder_id  => p_case_folder_id,
3431           p_data_point_id   => 11022,
3432           p_data_point_value => l_TOTAL_EMPLOYEES_IND,
3433           p_mode             => p_mode,
3434           p_error_msg   => p_error_msg,
3435           p_resultout   => p_resultout);
3436 
3437       build_case_folder_details(
3438           p_case_folder_id  => p_case_folder_id,
3439           p_data_point_id   => 11029,
3440           p_data_point_value => l_SIC_CODE,
3441           p_mode             => p_mode,
3442           p_error_msg   => p_error_msg,
3443           p_resultout   => p_resultout);
3444 
3445         build_case_folder_details(
3446           p_case_folder_id  => p_case_folder_id,
3447           p_data_point_id   => 11038,
3448           p_data_point_value => l_RENT_OWN_IND,
3449           p_mode             => p_mode,
3450           p_error_msg   => p_error_msg,
3451           p_resultout   => p_resultout);
3452 
3453         build_case_folder_details(
3454           p_case_folder_id  => p_case_folder_id,
3455           p_data_point_id   => 11037,
3456           p_data_point_value => l_REGISTRATION_TYPE,
3457           p_mode             => p_mode,
3458           p_error_msg   => p_error_msg,
3459           p_resultout   => p_resultout);
3460 
3461         build_case_folder_details(
3462           p_case_folder_id  => p_case_folder_id,
3463           p_data_point_id   => 11036,
3464           p_data_point_value => l_LEGAL_STATUS,
3465           p_mode             => p_mode,
3466           p_error_msg   => p_error_msg,
3467           p_resultout   => p_resultout);
3468 
3469         build_case_folder_details(
3470           p_case_folder_id  => p_case_folder_id,
3471           p_data_point_id   => 11035,
3472           p_data_point_value => l_HQ_BRANCH_IND,
3473           p_mode             => p_mode,
3474           p_error_msg   => p_error_msg,
3475           p_resultout   => p_resultout);
3476 
3477         build_case_folder_details(
3478           p_case_folder_id  => p_case_folder_id,
3479           p_data_point_id   => 11034,
3480           p_data_point_value => l_BRANCH_FLAG,
3481           p_mode             => p_mode,
3482           p_error_msg   => p_error_msg,
3483           p_resultout   => p_resultout);
3484 
3485         build_case_folder_details(
3486           p_case_folder_id  => p_case_folder_id,
3487           p_data_point_id   => 11032,
3488           p_data_point_value => l_LOCAL_ACTIVITY_CODE_TYPE,
3489           p_mode             => p_mode,
3490           p_error_msg   => p_error_msg,
3491           p_resultout   => p_resultout);
3492 
3493         build_case_folder_details(
3494           p_case_folder_id  => p_case_folder_id,
3495           p_data_point_id   => 11031,
3496           p_data_point_value => l_LOCAL_ACTIVITY_CODE,
3497           p_mode             => p_mode,
3498           p_error_msg   => p_error_msg,
3499           p_resultout   => p_resultout);
3500 
3501         build_case_folder_details(
3502           p_case_folder_id  => p_case_folder_id,
3503           p_data_point_id   => 11030,
3504           p_data_point_value => l_SIC_CODE_TYPE,
3505           p_mode             => p_mode,
3506           p_error_msg   => p_error_msg,
3507           p_resultout   => p_resultout);
3508 
3509         build_case_folder_details(
3510           p_case_folder_id  => p_case_folder_id,
3511           p_data_point_id   => 11117,
3512           p_data_point_value => l_GLOBAL_FAILURE_SCORE,
3513           p_mode             => p_mode,
3514           p_error_msg   => p_error_msg,
3515           p_resultout   => p_resultout);
3516 
3517         build_case_folder_details(
3518           p_case_folder_id  => p_case_folder_id,
3519           p_data_point_id   => 11114,
3520           p_data_point_value => l_IMPORT_IND,
3521           p_mode             => p_mode,
3522           p_error_msg   => p_error_msg,
3523           p_resultout   => p_resultout);
3524 
3525         build_case_folder_details(
3526           p_case_folder_id  => p_case_folder_id,
3527           p_data_point_id   => 11001,
3528           p_data_point_value => l_DUNS_NUMBER_C,
3529           p_mode             => p_mode,
3530           p_error_msg   => p_error_msg,
3531           p_resultout   => p_resultout);
3532 
3533         build_case_folder_details(
3534           p_case_folder_id  => p_case_folder_id,
3538           p_error_msg   => p_error_msg,
3535           p_data_point_id   => 11131,
3536           p_data_point_value => l_PARENT_SUB_IND,
3537           p_mode             => p_mode,
3539           p_resultout   => p_resultout);
3540 
3541         build_case_folder_details(
3542           p_case_folder_id  => p_case_folder_id,
3543           p_data_point_id   => 11152,
3544           p_data_point_value => l_FAILURE_SCORE,
3545           p_mode             => p_mode,
3546           p_error_msg   => p_error_msg,
3547           p_resultout   => p_resultout);
3548 
3549         build_case_folder_details(
3550           p_case_folder_id  => p_case_folder_id,
3551           p_data_point_id   => 11153,
3552           p_data_point_value => l_FAILURE_SCORE_COMMENTARY,
3553           p_mode             => p_mode,
3554           p_error_msg   => p_error_msg,
3555           p_resultout   => p_resultout);
3556 
3557         build_case_folder_details(
3558           p_case_folder_id  => p_case_folder_id,
3559           p_data_point_id   => 11023,
3560           p_data_point_value => l_TOTAL_EMP_EST_IND,
3561           p_mode             => p_mode,
3562           p_error_msg   => p_error_msg,
3563           p_resultout   => p_resultout);
3564 
3565    /* Noticed this weird logic.  probably a bug
3566       if there are multiple countries */
3567    FOR cGetLocationRec in cGetLocation
3568    LOOP
3569       l_country := cGetLocationRec.country;
3570 
3571    END LOOP;
3572       build_case_folder_details(
3573           p_case_folder_id  => p_case_folder_id,
3574           p_data_point_id   => 11010,
3575           p_data_point_value => l_country,
3576           p_mode             => p_mode,
3577           p_error_msg   => p_error_msg,
3578           p_resultout   => p_resultout);
3579 
3580    FOR cCrRatingsRec IN cCrRatings
3581    LOOP
3582         l_PAYDEX_SCORE              := cCrRatingsRec.PAYDEX_SCORE;
3583         l_PAYDEX_THREE_MONTHS_AGO   := cCrRatingsRec.PAYDEX_THREE_MONTHS_AGO;
3584         l_AVG_HIGH_CREDIT           := cCrRatingsRec.AVG_HIGH_CREDIT;
3585         l_HIGH_CREDIT               := cCrRatingsRec.HIGH_CREDIT;
3586         l_CREDIT_SCORE_CLASS        := cCrRatingsRec.CREDIT_SCORE_CLASS;
3587         l_CREDIT_SCORE_NATL_PERCENTILE := cCrRatingsRec.CREDIT_SCORE_NATL_PERCENTILE;
3588         l_CREDIT_SCORE_INCD_DEFAULT := cCrRatingsRec.CREDIT_SCORE_INCD_DEFAULT;
3589         l_CREDIT_SCORE_AGE          := cCrRatingsRec.CREDIT_SCORE_AGE;
3590         l_FAILURE_SCORE_CLASS       := cCrRatingsRec.FAILURE_SCORE_CLASS;
3591         l_FAILURE_SCORE_NATNL_PERCENT  := cCrRatingsRec.FAILURE_SCORE_NATNL_PERCENTILE;
3592         l_FAILURE_SCORE_INCD_DEFAULT := cCrRatingsRec.FAILURE_SCORE_INCD_DEFAULT;
3593         l_FAILURE_SCORE_AGE         := cCrRatingsRec.FAILURE_SCORE_AGE;
3594         l_LOW_RNG_DELQ_SCR          := cCrRatingsRec.LOW_RNG_DELQ_SCR;
3595         l_HIGH_RNG_DELQ_SCR         := cCrRatingsRec.HIGH_RNG_DELQ_SCR;
3596         l_DELQ_PMT_RNG_PRCNT        := cCrRatingsRec.DELQ_PMT_RNG_PRCNT;
3597         l_DELQ_PMT_PCTG_FOR_ALL_FIRMS   := cCrRatingsRec.DELQ_PMT_PCTG_FOR_ALL_FIRMS;
3598         l_NUM_TRADE_EXPERIENCES         := cCrRatingsRec.NUM_TRADE_EXPERIENCES;
3599         l_NUM_PRNT_BKCY_FILING          := cCrRatingsRec.NUM_PRNT_BKCY_FILING;
3600         l_DEBARMENTS_COUNT              := cCrRatingsRec.DEBARMENTS_COUNT;
3601         l_BANKRUPTCY_IND                := cCrRatingsRec.BANKRUPTCY_IND;
3602         l_DEBARMENT_IND                 := cCrRatingsRec.DEBARMENT_IND;
3603       l_BUSINESS_DISCONTINUED     := cCrRatingsRec.BUSINESS_DISCONTINUED;
3604     l_NUM_SPCL_EVENT        := cCrRatingsRec.NUM_SPCL_EVENT;
3605     l_MAXIMUM_CREDIT_CURRENCY_CODE  := cCrRatingsRec.MAXIMUM_CREDIT_CURRENCY_CODE;
3606     l_CREDIT_SCORE          := cCrRatingsRec.CREDIT_SCORE;
3607     l_CREDIT_SCORE_OVERRIDE_CODE  := cCrRatingsRec.CREDIT_SCORE_OVERRIDE_CODE;
3608     l_PRNT_BKCY_CHAPTER_CONV    := cCrRatingsRec.PRNT_BKCY_CHAPTER_CONV;
3609     l_NUM_PRNT_BKCY_CONVS     := cCrRatingsRec.NUM_PRNT_BKCY_CONVS;
3610     l_PRNT_BKCY_FILG_CHAPTER    := cCrRatingsRec.PRNT_BKCY_FILG_CHAPTER;
3611     l_PRNT_BKCY_FILG_TYPE     := cCrRatingsRec.PRNT_BKCY_FILG_TYPE;
3612     l_NO_TRADE_IND          := cCrRatingsRec.NO_TRADE_IND;
3613     l_JUDGEMENT_IND         := cCrRatingsRec.JUDGEMENT_IND;
3614     l_LIEN_IND            := cCrRatingsRec.LIEN_IND;
3615     l_SUIT_IND            := cCrRatingsRec.SUIT_IND;
3616     l_PAYDEX_INDUSTRY_DAYS      := cCrRatingsRec.PAYDEX_INDUSTRY_DAYS;
3617     l_FINCL_LGL_EVENT_IND     := cCrRatingsRec.FINCL_LGL_EVENT_IND;
3618     l_DISASTER_IND          := cCrRatingsRec.DISASTER_IND;
3619     l_CRIMINAL_PROCEEDING_IND   := cCrRatingsRec.CRIMINAL_PROCEEDING_IND;
3620     l_FINCL_EMBT_IND        := cCrRatingsRec.FINCL_EMBT_IND;
3621     l_PAYDEX_NORM         := cCrRatingsRec.PAYDEX_NORM;
3622     l_RATING            := cCrRatingsRec.RATING;
3623     l_SECURED_FLNG_IND        := cCrRatingsRec.SECURED_FLNG_IND;
3624     l_CLAIMS_IND          := cCrRatingsRec.CLAIMS_IND;
3625     l_SUIT_JUDGE_IND        := cCrRatingsRec.SUIT_JUDGE_IND;
3626     l_DET_HISTORY_IND       := cCrRatingsRec.DET_HISTORY_IND;
3627     l_OTHER_SPEC_EVNT_IND     := cCrRatingsRec.OTHER_SPEC_EVNT_IND;
3628     l_OPRG_SPEC_EVNT_IND      := cCrRatingsRec.OPRG_SPEC_EVNT_IND;
3629     l_CREDIT_SCORE_COMMENTARY   := cCrRatingsRec.CREDIT_SCORE_COMMENTARY;
3630 
3631    END LOOP;
3632 
3633    -- Now populate the case folder
3634    build_case_folder_details(
3635        p_case_folder_id  => p_case_folder_id,
3636        p_data_point_id   => 11017,
3637        p_data_point_value => l_control_yr,
3638        p_mode             => p_mode,
3639        p_error_msg   => p_error_msg,
3640        p_resultout   => p_resultout);
3641 
3642    build_case_folder_details(
3643        p_case_folder_id  => p_case_folder_id,
3644        p_data_point_id   => 11018,
3648        p_resultout   => p_resultout);
3645        p_data_point_value => l_incorp_year,
3646        p_mode             => p_mode,
3647        p_error_msg   => p_error_msg,
3649 
3650    build_case_folder_details(
3651        p_case_folder_id  => p_case_folder_id,
3652        p_data_point_id   => 11019,
3653        p_data_point_value => l_year_established,
3654        p_mode             => p_mode,
3655        p_error_msg   => p_error_msg,
3656        p_resultout   => p_resultout);
3657 
3658    build_case_folder_details(
3659        p_case_folder_id  => p_case_folder_id,
3660        p_data_point_id   => 11020,
3661        p_data_point_value => l_employees_total ,
3662        p_mode             => p_mode,
3663        p_error_msg   => p_error_msg,
3664        p_resultout   => p_resultout);
3665 
3666    build_case_folder_details(
3667        p_case_folder_id  => p_case_folder_id,
3668        p_data_point_id   => 11048,
3669        p_data_point_value => l_total_payments,
3670        p_mode             => p_mode,
3671        p_error_msg   => p_error_msg,
3672        p_resultout   => p_resultout);
3673 
3674    build_case_folder_details(
3675        p_case_folder_id  => p_case_folder_id,
3676        p_data_point_id   => 11118,
3677        p_data_point_value => l_maximum_credit_reco,
3678        p_mode             => p_mode,
3679        p_error_msg   => p_error_msg,
3680        p_resultout   => p_resultout);
3681 
3682     build_case_folder_details(
3683        p_case_folder_id  => p_case_folder_id,
3684        p_data_point_id   => 11043,
3685        p_data_point_value => l_PAYDEX_SCORE,
3686        p_mode             => p_mode,
3687        p_error_msg   => p_error_msg,
3688        p_resultout   => p_resultout);
3689 
3690     build_case_folder_details(
3691        p_case_folder_id  => p_case_folder_id,
3692        p_data_point_id   => 11044,
3693        p_data_point_value => l_PAYDEX_THREE_MONTHS_AGO,
3694        p_mode             => p_mode,
3695        p_error_msg   => p_error_msg,
3696        p_resultout   => p_resultout);
3697 
3698     build_case_folder_details(
3699        p_case_folder_id  => p_case_folder_id,
3700        p_data_point_id   => 11046,
3701        p_data_point_value => l_AVG_HIGH_CREDIT,
3702        p_mode             => p_mode,
3703        p_error_msg   => p_error_msg,
3704        p_resultout   => p_resultout);
3705 
3706     build_case_folder_details(
3707        p_case_folder_id  => p_case_folder_id,
3708        p_data_point_id   => 11047,
3709        p_data_point_value => l_HIGH_CREDIT,
3710        p_mode             => p_mode,
3711        p_error_msg   => p_error_msg,
3712        p_resultout   => p_resultout);
3713 
3714     build_case_folder_details(
3715        p_case_folder_id  => p_case_folder_id,
3716        p_data_point_id   => 11143,
3717        p_data_point_value => l_CREDIT_SCORE_CLASS,
3718        p_mode             => p_mode,
3719        p_error_msg   => p_error_msg,
3720        p_resultout   => p_resultout);
3721 
3722     build_case_folder_details(
3723        p_case_folder_id  => p_case_folder_id,
3724        p_data_point_id   => 11144,
3725        p_data_point_value => l_CREDIT_SCORE_NATL_PERCENTILE,
3726        p_mode             => p_mode,
3727        p_error_msg   => p_error_msg,
3728        p_resultout   => p_resultout);
3729 
3730     build_case_folder_details(
3731        p_case_folder_id  => p_case_folder_id,
3732        p_data_point_id   => 11145,
3733        p_data_point_value => l_CREDIT_SCORE_INCD_DEFAULT,
3734        p_mode             => p_mode,
3735        p_error_msg   => p_error_msg,
3736        p_resultout   => p_resultout);
3737     build_case_folder_details(
3738        p_case_folder_id  => p_case_folder_id,
3739        p_data_point_id   => 11146,
3740        p_data_point_value => l_CREDIT_SCORE_AGE,
3741        p_mode             => p_mode,
3742        p_error_msg   => p_error_msg,
3743        p_resultout   => p_resultout);
3744 
3745     build_case_folder_details(
3746        p_case_folder_id  => p_case_folder_id,
3747        p_data_point_id   => 11154,
3748        p_data_point_value => l_FAILURE_SCORE_CLASS,
3749        p_mode             => p_mode,
3750        p_error_msg   => p_error_msg,
3751        p_resultout   => p_resultout);
3752 
3753     build_case_folder_details(
3754        p_case_folder_id  => p_case_folder_id,
3755        p_data_point_id   => 11155,
3756        p_data_point_value => l_FAILURE_SCORE_NATNL_PERCENT,
3757        p_mode             => p_mode,
3758        p_error_msg   => p_error_msg,
3759        p_resultout   => p_resultout);
3760 
3761     build_case_folder_details(
3762        p_case_folder_id  => p_case_folder_id,
3763        p_data_point_id   => 11156,
3764        p_data_point_value => l_FAILURE_SCORE_INCD_DEFAULT,
3765        p_mode             => p_mode,
3766        p_error_msg   => p_error_msg,
3767        p_resultout   => p_resultout);
3768 
3769     build_case_folder_details(
3770        p_case_folder_id  => p_case_folder_id,
3771        p_data_point_id   => 11157,
3772        p_data_point_value => l_FAILURE_SCORE_AGE,
3773        p_mode             => p_mode,
3774        p_error_msg   => p_error_msg,
3775        p_resultout   => p_resultout);
3776 
3777     build_case_folder_details(
3778        p_case_folder_id  => p_case_folder_id,
3779        p_data_point_id   => 11162,
3780        p_data_point_value => l_LOW_RNG_DELQ_SCR,
3781        p_mode             => p_mode,
3782        p_error_msg   => p_error_msg,
3783        p_resultout   => p_resultout);
3784 
3785     build_case_folder_details(
3786        p_case_folder_id  => p_case_folder_id,
3790        p_error_msg   => p_error_msg,
3787        p_data_point_id   => 11163,
3788        p_data_point_value => l_HIGH_RNG_DELQ_SCR ,
3789        p_mode             => p_mode,
3791        p_resultout   => p_resultout);
3792 
3793      build_case_folder_details(
3794        p_case_folder_id  => p_case_folder_id,
3795        p_data_point_id   => 11164,
3796        p_data_point_value => l_DELQ_PMT_RNG_PRCNT ,
3797        p_mode             => p_mode,
3798        p_error_msg   => p_error_msg,
3799        p_resultout   => p_resultout);
3800 
3801      build_case_folder_details(
3802        p_case_folder_id  => p_case_folder_id,
3803        p_data_point_id   => 11165,
3804        p_data_point_value => l_DELQ_PMT_PCTG_FOR_ALL_FIRMS ,
3805        p_mode             => p_mode,
3806        p_error_msg   => p_error_msg,
3807        p_resultout   => p_resultout);
3808 
3809      build_case_folder_details(
3810        p_case_folder_id  => p_case_folder_id,
3811        p_data_point_id   => 11166,
3812        p_data_point_value => l_NUM_TRADE_EXPERIENCES ,
3813        p_mode             => p_mode,
3814        p_error_msg   => p_error_msg,
3815        p_resultout   => p_resultout);
3816 
3817 
3818     build_case_folder_details(
3819        p_case_folder_id  => p_case_folder_id,
3820        p_data_point_id   => 11195,
3821        p_data_point_value => l_DEBARMENTS_COUNT,
3822        p_mode             => p_mode,
3823        p_error_msg   => p_error_msg,
3824        p_resultout   => p_resultout);
3825 
3826    build_case_folder_details(
3827        p_case_folder_id  => p_case_folder_id,
3828        p_data_point_id   => 11175,
3829        p_data_point_value => l_BANKRUPTCY_IND,
3830        p_mode             => p_mode,
3831        p_error_msg      => p_error_msg,
3832        p_resultout   => p_resultout);
3833 
3834     build_case_folder_details(
3835        p_case_folder_id  => p_case_folder_id,
3836        p_data_point_id   => 11194,
3837        p_data_point_value => l_DEBARMENT_IND,
3838        p_mode             => p_mode,
3839        p_error_msg      => p_error_msg,
3840        p_resultout   => p_resultout);
3841 
3842     build_case_folder_details(
3843        p_case_folder_id  => p_case_folder_id,
3844        p_data_point_id   => 11050,
3845        p_data_point_value => l_oob_IND,
3846        p_mode             => p_mode,
3847        p_error_msg      => p_error_msg,
3848        p_resultout   => p_resultout);
3849        ------------------------
3850     build_case_folder_details(
3851        p_case_folder_id  => p_case_folder_id,
3852        p_data_point_id   => 11188,
3853        p_data_point_value => l_BUSINESS_DISCONTINUED,
3854        p_mode             => p_mode,
3855        p_error_msg      => p_error_msg,
3856        p_resultout   => p_resultout);
3857 
3858   build_case_folder_details(
3859        p_case_folder_id  => p_case_folder_id,
3860        p_data_point_id   => 11190,
3861        p_data_point_value => l_NUM_SPCL_EVENT,
3862        p_mode             => p_mode,
3863        p_error_msg      => p_error_msg,
3864        p_resultout   => p_resultout);
3865 
3866     build_case_folder_details(
3867        p_case_folder_id  => p_case_folder_id,
3868        p_data_point_id   => 11119,
3869        p_data_point_value => l_MAXIMUM_CREDIT_CURRENCY_CODE,
3870        p_mode             => p_mode,
3871        p_error_msg      => p_error_msg,
3872        p_resultout   => p_resultout);
3873 
3874     build_case_folder_details(
3875        p_case_folder_id  => p_case_folder_id,
3876        p_data_point_id   => 11141,
3877        p_data_point_value => l_CREDIT_SCORE,
3878        p_mode             => p_mode,
3879        p_error_msg      => p_error_msg,
3880        p_resultout   => p_resultout);
3881 
3882 
3883     build_case_folder_details(
3884        p_case_folder_id  => p_case_folder_id,
3885        p_data_point_id   => 11160,
3886        p_data_point_value => l_CREDIT_SCORE_OVERRIDE_CODE,
3887        p_mode             => p_mode,
3888        p_error_msg      => p_error_msg,
3889        p_resultout   => p_resultout);
3890 
3891     build_case_folder_details(
3892        p_case_folder_id  => p_case_folder_id,
3893        p_data_point_id   => 11184,
3894        p_data_point_value => l_PRNT_BKCY_CHAPTER_CONV,
3895        p_mode             => p_mode,
3896        p_error_msg      => p_error_msg,
3897        p_resultout   => p_resultout);
3898 
3899     build_case_folder_details(
3900        p_case_folder_id  => p_case_folder_id,
3901        p_data_point_id   => 11182,
3902        p_data_point_value => l_NUM_PRNT_BKCY_CONVS,
3903        p_mode             => p_mode,
3904        p_error_msg      => p_error_msg,
3905        p_resultout   => p_resultout);
3906 
3907     build_case_folder_details(
3908        p_case_folder_id  => p_case_folder_id,
3909        p_data_point_id   => 11180,
3913        p_resultout   => p_resultout);
3910        p_data_point_value => l_PRNT_BKCY_FILG_CHAPTER,
3911        p_mode             => p_mode,
3912        p_error_msg      => p_error_msg,
3914 
3915     build_case_folder_details(
3916        p_case_folder_id  => p_case_folder_id,
3917        p_data_point_id   => 11179,
3918        p_data_point_value => l_PRNT_BKCY_FILG_TYPE,
3919        p_mode             => p_mode,
3920        p_error_msg      => p_error_msg,
3921        p_resultout   => p_resultout);
3922 
3923     build_case_folder_details(
3924        p_case_folder_id  => p_case_folder_id,
3925        p_data_point_id   => 11178,
3926        p_data_point_value => l_NUM_PRNT_BKCY_FILING,
3927        p_mode             => p_mode,
3928        p_error_msg      => p_error_msg,
3929        p_resultout   => p_resultout);
3930 
3931     build_case_folder_details(
3932        p_case_folder_id  => p_case_folder_id,
3933        p_data_point_id   => 11176,
3934        p_data_point_value => l_NO_TRADE_IND,
3935        p_mode             => p_mode,
3936        p_error_msg      => p_error_msg,
3937        p_resultout   => p_resultout);
3938 
3939     build_case_folder_details(
3940        p_case_folder_id  => p_case_folder_id,
3941        p_data_point_id   => 11174,
3942        p_data_point_value => l_JUDGEMENT_IND,
3943        p_mode             => p_mode,
3944        p_error_msg      => p_error_msg,
3945        p_resultout   => p_resultout);
3946 
3947     build_case_folder_details(
3948        p_case_folder_id  => p_case_folder_id,
3949        p_data_point_id   => 11173,
3950        p_data_point_value => l_LIEN_IND,
3951        p_mode             => p_mode,
3952        p_error_msg      => p_error_msg,
3953        p_resultout   => p_resultout);
3954 
3955     build_case_folder_details(
3956        p_case_folder_id  => p_case_folder_id,
3957        p_data_point_id   => 11172,
3958        p_data_point_value => l_SUIT_IND,
3959        p_mode             => p_mode,
3960        p_error_msg      => p_error_msg,
3961        p_resultout   => p_resultout);
3962 
3963     build_case_folder_details(
3964        p_case_folder_id  => p_case_folder_id,
3965        p_data_point_id   => 11169,
3966        p_data_point_value => l_PAYDEX_INDUSTRY_DAYS,
3967        p_mode             => p_mode,
3968        p_error_msg      => p_error_msg,
3969        p_resultout   => p_resultout);
3970 
3971     build_case_folder_details(
3972        p_case_folder_id  => p_case_folder_id,
3973        p_data_point_id   => 11054,
3974        p_data_point_value => l_FINCL_LGL_EVENT_IND,
3975        p_mode             => p_mode,
3976        p_error_msg      => p_error_msg,
3977        p_resultout   => p_resultout);
3978 
3979     build_case_folder_details(
3980        p_case_folder_id  => p_case_folder_id,
3981        p_data_point_id   => 11052,
3982        p_data_point_value => l_DISASTER_IND,
3983        p_mode             => p_mode,
3984        p_error_msg      => p_error_msg,
3985        p_resultout   => p_resultout);
3986 
3987     build_case_folder_details(
3988        p_case_folder_id  => p_case_folder_id,
3989        p_data_point_id   => 11051,
3990        p_data_point_value => l_CRIMINAL_PROCEEDING_IND,
3991        p_mode             => p_mode,
3992        p_error_msg      => p_error_msg,
3993        p_resultout   => p_resultout);
3994 
3995     build_case_folder_details(
3996        p_case_folder_id  => p_case_folder_id,
3997        p_data_point_id   => 11049,
3998        p_data_point_value => l_FINCL_EMBT_IND,
3999        p_mode             => p_mode,
4000        p_error_msg      => p_error_msg,
4001        p_resultout   => p_resultout);
4002 
4003     build_case_folder_details(
4004        p_case_folder_id  => p_case_folder_id,
4005        p_data_point_id   => 11045,
4006        p_data_point_value => l_PAYDEX_NORM,
4007        p_mode             => p_mode,
4008        p_error_msg      => p_error_msg,
4009        p_resultout   => p_resultout);
4010 
4011     build_case_folder_details(
4015        p_mode             => p_mode,
4012        p_case_folder_id  => p_case_folder_id,
4013        p_data_point_id   => 11116,
4014        p_data_point_value => l_RATING,
4016        p_error_msg      => p_error_msg,
4017        p_resultout   => p_resultout);
4018 
4019     build_case_folder_details(
4020        p_case_folder_id  => p_case_folder_id,
4021        p_data_point_id   => 11060,
4022        p_data_point_value => l_SECURED_FLNG_IND,
4023        p_mode             => p_mode,
4024        p_error_msg      => p_error_msg,
4025        p_resultout   => p_resultout);
4026 
4027     build_case_folder_details(
4028        p_case_folder_id  => p_case_folder_id,
4029        p_data_point_id   => 11059,
4030        p_data_point_value => l_CLAIMS_IND,
4031        p_mode             => p_mode,
4032        p_error_msg      => p_error_msg,
4033        p_resultout   => p_resultout);
4034 
4035     build_case_folder_details(
4036        p_case_folder_id  => p_case_folder_id,
4037        p_data_point_id   => 11058,
4038        p_data_point_value => l_SUIT_JUDGE_IND,
4039        p_mode             => p_mode,
4040        p_error_msg      => p_error_msg,
4041        p_resultout   => p_resultout);
4042 
4043     build_case_folder_details(
4044        p_case_folder_id  => p_case_folder_id,
4045        p_data_point_id   => 11057,
4046        p_data_point_value => l_DET_HISTORY_IND,
4047        p_mode             => p_mode,
4048        p_error_msg      => p_error_msg,
4049        p_resultout   => p_resultout);
4050 
4051     build_case_folder_details(
4052        p_case_folder_id  => p_case_folder_id,
4053        p_data_point_id   => 11056,
4054        p_data_point_value => l_OTHER_SPEC_EVNT_IND,
4055        p_mode             => p_mode,
4056        p_error_msg      => p_error_msg,
4057        p_resultout   => p_resultout);
4058 
4059     build_case_folder_details(
4060        p_case_folder_id  => p_case_folder_id,
4061        p_data_point_id   => 11055,
4062        p_data_point_value => l_OPRG_SPEC_EVNT_IND,
4063        p_mode             => p_mode,
4064        p_error_msg      => p_error_msg,
4065        p_resultout   => p_resultout);
4066 
4067   build_case_folder_details(
4068        p_case_folder_id  => p_case_folder_id,
4069        p_data_point_id   => 11142,
4070        p_data_point_value => l_CREDIT_SCORE_COMMENTARY,
4071        p_mode             => p_mode,
4072        p_error_msg      => p_error_msg,
4073        p_resultout   => p_resultout);
4074 
4075 
4076     FOR cFinNumRec IN cFinNum
4077     LOOP
4078         IF cFinNumRec.financial_number_name = 'CASH_LIQ_ASSETS'
4079         THEN
4080             l_cash_liqu_asset := cFinNumRec.financial_number;
4081         ELSIF cFinNumRec.financial_number_name = 'ACCOUNTS_RECEIVABLE'
4082         THEN
4083             l_ar := cFinNumRec.financial_number;
4084         ELSIF cFinNumRec.financial_number_name = 'ACCOUNTS_PAYABLE'
4085         THEN
4086             l_ap := cFinNumRec.financial_number;
4087         ELSIF cFinNumRec.financial_number_name = 'ANNUAL_SALES_VOLUME'
4088         THEN
4089             l_an_sales_vol := cFinNumRec.financial_number;
4090         ELSIF cFinNumRec.financial_number_name = 'AUTHORIZED_CAPITAL'
4091         THEN
4092             l_auth_cap := cFinNumRec.financial_number;
4093         ELSIF cFinNumRec.financial_number_name = 'COST_OF_SALES'
4094         THEN
4095             l_cost_of_sales := cFinNumRec.financial_number;
4096         ELSIF cFinNumRec.financial_number_name = 'CURRENT_RATIO'
4097         THEN
4098             l_current_ratio := cFinNumRec.financial_number;
4099         ELSIF cFinNumRec.financial_number_name = 'DIVIDENDS'
4100         THEN
4101             l_dividends := cFinNumRec.financial_number;
4102         ELSIF cFinNumRec.financial_number_name = 'FIXED_ASSETS'
4103         THEN
4104             l_fixed_assets := cFinNumRec.financial_number;
4105         ELSIF cFinNumRec.financial_number_name = 'GROSS_INCOME'
4106         THEN
4107             l_gross_inc := cFinNumRec.financial_number;
4108         ELSIF cFinNumRec.financial_number_name = 'INTANGIBLE_ASSETS'
4109         THEN
4110             l_intg_asset := cFinNumRec.financial_number;
4111         ELSIF cFinNumRec.financial_number_name = 'INVENTORY'
4112         THEN
4113             l_inventory := cFinNumRec.financial_number;
4114         ELSIF cFinNumRec.financial_number_name = 'ISSUED_CAPITAL'
4115         THEN
4116             l_iss_capital := cFinNumRec.financial_number;
4117         ELSIF cFinNumRec.financial_number_name = 'LONG_TERM_DEBT'
4118         THEN
4119             l_long_debt := cFinNumRec.financial_number;
4120         ELSIF cFinNumRec.financial_number_name = 'NET_INCOME'
4121         THEN
4122             l_net_income := cFinNumRec.financial_number;
4123         ELSIF cFinNumRec.financial_number_name = 'NET_WORTH'
4124         THEN
4125             l_net_worth := cFinNumRec.financial_number;
4126         ELSIF cFinNumRec.financial_number_name = 'NOMINAL_CAPITAL'
4127         THEN
4128             l_nom_capital := cFinNumRec.financial_number;
4132         ELSIF cFinNumRec.financial_number_name = 'PREVIOUS_SALES'
4129         ELSIF cFinNumRec.financial_number_name = 'PAID_IN_CAPITAL'
4130         THEN
4131             l_paid_in_capital := cFinNumRec.financial_number;
4133         THEN
4134             l_prev_sales := cFinNumRec.financial_number;
4135         ELSIF cFinNumRec.financial_number_name = 'PREV_NET_WORTH'
4136         THEN
4137             l_prev_net_worth := cFinNumRec.financial_number;
4138         ELSIF cFinNumRec.financial_number_name = 'PREV_WORKING_CAPITAL'
4139         THEN
4140             l_prev_work_cap := cFinNumRec.financial_number;
4141         ELSIF cFinNumRec.financial_number_name = 'PROFIT_BEFORE_TAX'
4142         THEN
4143             l_profit_bef_tax := cFinNumRec.financial_number;
4144         ELSIF cFinNumRec.financial_number_name = 'QUICK_RATIO'
4145         THEN
4146             l_quick_ratio := cFinNumRec.financial_number;
4147         ELSIF cFinNumRec.financial_number_name = 'RETAINED_EARNINGS'
4148         THEN
4149             l_ret_earnings := cFinNumRec.financial_number;
4150         ELSIF cFinNumRec.financial_number_name = 'SALES'
4151         THEN
4152             l_sales := cFinNumRec.financial_number;
4153         ELSIF cFinNumRec.financial_number_name = 'TANGIBLE_NET_WORTH'
4154         THEN
4155             l_tang_net_worth := cFinNumRec.financial_number;
4156         ELSIF cFinNumRec.financial_number_name = 'TOTAL_ASSETS'
4157         THEN
4158             l_tot_assets := cFinNumRec.financial_number;
4159         ELSIF cFinNumRec.financial_number_name = 'TOTAL_CURRENT_ASSETS'
4160         THEN
4161             l_tot_curr_assets := cFinNumRec.financial_number;
4162         ELSIF cFinNumRec.financial_number_name = 'TOTAL_CURR_LIABILITIES'
4163         THEN
4164             l_tot_curr_liab := cFinNumRec.financial_number;
4165         ELSIF cFinNumRec.financial_number_name = 'TOTAL_LIABILITIES'
4166         THEN
4167             l_tot_liab := cFinNumRec.financial_number;
4168         ELSIF cFinNumRec.financial_number_name = 'TOTAL_LIAB_EQUITY'
4169         THEN
4170             l_tot_liab_equ := cFinNumRec.financial_number;
4171         ELSIF cFinNumRec.financial_number_name = 'TOT_LONG_TERM_LIAB'
4172         THEN
4173             l_tot_long_liab := cFinNumRec.financial_number;
4174         END IF;
4175     END LOOP;
4176     build_case_folder_details(
4177                 p_case_folder_id  => p_case_folder_id,
4178                 p_data_point_id   => 11061,
4179                 p_data_point_value => l_cash_liqu_asset,
4180                 p_mode             => p_mode,
4181                 p_error_msg   => p_error_msg,
4182                 p_resultout   => p_resultout);
4183     build_case_folder_details(
4184                 p_case_folder_id  => p_case_folder_id,
4185                 p_data_point_id   => 11062,
4186                 p_data_point_value => l_ar,
4187                 p_mode             => p_mode,
4188                 p_error_msg   => p_error_msg,
4189                 p_resultout   => p_resultout);
4190     build_case_folder_details(
4191                 p_case_folder_id  => p_case_folder_id,
4192                 p_data_point_id   => 11063,
4193                 p_data_point_value => l_ap,
4194                 p_mode             => p_mode,
4195                 p_error_msg   => p_error_msg,
4196                 p_resultout   => p_resultout);
4197     build_case_folder_details(
4198                 p_case_folder_id  => p_case_folder_id,
4199                 p_data_point_id   => 11067,
4200                 p_data_point_value => l_inventory,
4201                 p_mode             => p_mode,
4202                 p_error_msg   => p_error_msg,
4203                 p_resultout   => p_resultout);
4204     build_case_folder_details(
4205                 p_case_folder_id  => p_case_folder_id,
4206                 p_data_point_id   => 11068,
4207                 p_data_point_value => l_fixed_assets,
4208                 p_mode             => p_mode,
4209                 p_error_msg   => p_error_msg,
4210                 p_resultout   => p_resultout);
4211     build_case_folder_details(
4212                 p_case_folder_id  => p_case_folder_id,
4213                 p_data_point_id   => 11069,
4214                 p_data_point_value => l_tot_curr_assets,
4215                 p_mode             => p_mode,
4216                 p_error_msg   => p_error_msg,
4217                 p_resultout   => p_resultout);
4218     build_case_folder_details(
4219                 p_case_folder_id  => p_case_folder_id,
4220                 p_data_point_id   => 11070,
4221                 p_data_point_value => l_tot_curr_liab,
4222                 p_mode             => p_mode,
4223                 p_error_msg   => p_error_msg,
4224                 p_resultout   => p_resultout);
4225     build_case_folder_details(
4226                 p_case_folder_id  => p_case_folder_id,
4227                 p_data_point_id   => 11071,
4228                 p_data_point_value => l_tot_assets,
4229                 p_mode             => p_mode,
4230                 p_error_msg   => p_error_msg,
4231                 p_resultout   => p_resultout);
4232     build_case_folder_details(
4233                 p_case_folder_id  => p_case_folder_id,
4234                 p_data_point_id   => 11072,
4235                 p_data_point_value => l_intg_asset,
4236                 p_mode             => p_mode,
4237                 p_error_msg   => p_error_msg,
4238                 p_resultout   => p_resultout);
4239     build_case_folder_details(
4240                 p_case_folder_id  => p_case_folder_id,
4241                 p_data_point_id   => 11073,
4242                 p_data_point_value => l_long_debt,
4243                 p_mode             => p_mode,
4244                 p_error_msg   => p_error_msg,
4245                 p_resultout   => p_resultout);
4246     build_case_folder_details(
4247                 p_case_folder_id  => p_case_folder_id,
4248                 p_data_point_id   => 11074,
4252                 p_resultout   => p_resultout);
4249                 p_data_point_value => l_tot_long_liab,
4250                 p_mode             => p_mode,
4251                 p_error_msg   => p_error_msg,
4253     build_case_folder_details(
4254                 p_case_folder_id  => p_case_folder_id,
4255                 p_data_point_id   => 11075,
4256                 p_data_point_value => l_tot_liab,
4257                 p_mode             => p_mode,
4258                 p_error_msg   => p_error_msg,
4259                 p_resultout   => p_resultout);
4260     build_case_folder_details(
4261                 p_case_folder_id  => p_case_folder_id,
4262                 p_data_point_id   => 11076,
4263                 p_data_point_value => l_ret_earnings,
4264                 p_mode             => p_mode,
4265                 p_error_msg   => p_error_msg,
4266                 p_resultout   => p_resultout);
4267     build_case_folder_details(
4268                 p_case_folder_id  => p_case_folder_id,
4269                 p_data_point_id   => 11077,
4270                 p_data_point_value => l_dividends,
4271                 p_mode             => p_mode,
4272                 p_error_msg   => p_error_msg,
4273                 p_resultout   => p_resultout);
4274     build_case_folder_details(
4275                 p_case_folder_id  => p_case_folder_id,
4276                 p_data_point_id   => 11078,
4277                 p_data_point_value => l_net_worth,
4278                 p_mode             => p_mode,
4279                 p_error_msg   => p_error_msg,
4280                 p_resultout   => p_resultout);
4281     build_case_folder_details(
4282                 p_case_folder_id  => p_case_folder_id,
4283                 p_data_point_id   => 11079,
4284                 p_data_point_value => l_tang_net_worth,
4285                 p_mode             => p_mode,
4286                 p_error_msg   => p_error_msg,
4287                 p_resultout   => p_resultout);
4288     build_case_folder_details(
4289                 p_case_folder_id  => p_case_folder_id,
4290                 p_data_point_id   => 11082,
4291                 p_data_point_value =>l_prev_net_worth ,
4292                 p_mode             => p_mode,
4293                 p_error_msg   => p_error_msg,
4294                 p_resultout   => p_resultout);
4295     build_case_folder_details(
4296                 p_case_folder_id  => p_case_folder_id,
4297                 p_data_point_id   => 11083,
4298                 p_data_point_value => l_tot_liab_equ,
4299                 p_mode             => p_mode,
4300                 p_error_msg   => p_error_msg,
4301                 p_resultout   => p_resultout);
4302     build_case_folder_details(
4303                 p_case_folder_id  => p_case_folder_id,
4304                 p_data_point_id   => 11084,
4305                 p_data_point_value => l_sales,
4306                 p_mode             => p_mode,
4307                 p_error_msg   => p_error_msg,
4308                 p_resultout   => p_resultout);
4309     build_case_folder_details(
4310                 p_case_folder_id  => p_case_folder_id,
4311                 p_data_point_id   => 11085,
4312                 p_data_point_value => l_an_sales_vol,
4313                 p_mode             => p_mode,
4314                 p_error_msg   => p_error_msg,
4315                 p_resultout   => p_resultout);
4316     build_case_folder_details(
4317                 p_case_folder_id  => p_case_folder_id,
4318                 p_data_point_id   => 11089,
4319                 p_data_point_value => l_prev_sales,
4320                 p_mode             => p_mode,
4321                 p_error_msg   => p_error_msg,
4322                 p_resultout   => p_resultout);
4323     build_case_folder_details(
4324                 p_case_folder_id  => p_case_folder_id,
4325                 p_data_point_id   => 11090,
4326                 p_data_point_value => l_cost_of_sales,
4327                 p_mode             => p_mode,
4328                 p_error_msg   => p_error_msg,
4329                 p_resultout   => p_resultout);
4330     build_case_folder_details(
4331                 p_case_folder_id  => p_case_folder_id,
4332                 p_data_point_id   => 11091,
4333                 p_data_point_value => l_gross_inc,
4334                 p_mode             => p_mode,
4335                 p_error_msg   => p_error_msg,
4336                 p_resultout   => p_resultout);
4337     build_case_folder_details(
4338                 p_case_folder_id  => p_case_folder_id,
4339                 p_data_point_id   => 11092,
4340                 p_data_point_value => l_net_income,
4341                 p_mode             => p_mode,
4342                 p_error_msg   => p_error_msg,
4343                 p_resultout   => p_resultout);
4344     build_case_folder_details(
4345                 p_case_folder_id  => p_case_folder_id,
4346                 p_data_point_id   => 11112,
4347                 p_data_point_value => l_current_ratio,
4348                 p_mode             => p_mode,
4349                 p_error_msg   => p_error_msg,
4350                 p_resultout   => p_resultout);
4351     build_case_folder_details(
4352                 p_case_folder_id  => p_case_folder_id,
4353                 p_data_point_id   => 11113,
4354                 p_data_point_value => l_quick_ratio,
4355                 p_mode             => p_mode,
4356                 p_error_msg   => p_error_msg,
4357                 p_resultout   => p_resultout);
4358 
4359   -- populate all financial report releated data
4360   BUILD_DNB_SCORABLE_FIN_DATA (
4361       p_case_folder_id    => p_case_folder_id,
4362       p_mode          => p_mode,
4363       p_financial_report_id   => p_balance_sheet_id,
4364       p_financial_report_type => 'BALANCE_SHEET' ,
4365       p_error_msg       => p_error_msg,
4366       p_resultout     => p_resultout);
4367 
4368   BUILD_DNB_SCORABLE_FIN_DATA (
4369       p_case_folder_id    => p_case_folder_id,
4370       p_mode          => p_mode,
4371       p_financial_report_id   => p_income_statement_id,
4372       p_financial_report_type => 'INCOME' ,
4373       p_error_msg       => p_error_msg,
4374       p_resultout     => p_resultout);
4375 
4376   BUILD_DNB_SCORABLE_FIN_DATA (
4377       p_case_folder_id    => p_case_folder_id,
4378       p_mode          => p_mode,
4379       p_financial_report_id   => p_tangible_net_worth_id,
4380       p_financial_report_type => 'TANGIBLE' ,
4381       p_error_msg       => p_error_msg,
4382       p_resultout     => p_resultout);
4383 
4384   BUILD_DNB_SCORABLE_FIN_DATA (
4385       p_case_folder_id    => p_case_folder_id,
4386       p_mode          => p_mode,
4387       p_financial_report_id   => p_annual_sales_volume_id,
4388       p_financial_report_type => 'ANNUAL_SALES' ,
4389       p_error_msg       => p_error_msg,
4390       p_resultout     => p_resultout);
4391 
4392     IF pg_wf_debug = 'Y'
4393     THEN
4394        ar_cmgt_util.wf_debug(p_case_folder_id,
4395               'ar_cmgt_data_points_pkg.build_dnb_scorable_data()-');
4396     END IF;
4397 END BUILD_DNB_SCOREABLE_DATA;
4398 
4399 
4400 PROCEDURE BUILD_DNB_CASE_FOLDER(
4401         p_party_id          IN      NUMBER,
4402         p_check_list_id     IN      NUMBER,
4403         p_case_folder_id    IN      NUMBER,
4404         p_mode              IN      VARCHAR2 default 'CREATE',
4405         p_resultout         OUT nocopy     VARCHAR2,
4406         p_error_msg         OUT nocopy     VARCHAR2) IS
4407 
4408 l_organization_profile_id           hz_organization_profiles.organization_profile_id%TYPE;
4409 l_location_id                       hz_locations.location_id%TYPE;
4410 l_contact_point_id                  hz_contact_points.contact_point_id%TYPE;
4411 l_relationship_id                   hz_relationships.relationship_id%TYPE;
4412 l_report_type                       hz_financial_reports.type_of_financial_report%TYPE := 'BALANCE_SHEET';
4413 l_credit_rating_id                  NUMBER;
4414 l_financial_report_id               NUMBER;
4415 l_hq_branch_ind                     hz_parties.hq_branch_ind%type;
4416 l_balance_sheet_id                  NUMBER;
4417 l_income_statement_id               NUMBER;
4418 l_tangible_net_worth_id             NUMBER;
4419 l_annual_sales_volume_id            NUMBER;
4420 
4421 CURSOR c_relationships IS
4422        SELECT  rel.relationship_id, rel.relationship_code
4423         FROM    hz_relationships rel
4424         WHERE   rel.object_id = p_party_id
4425          AND    rel.relationship_code in ('HEADQUARTERS_OF','PARENT_OF',
4426                                           'DOMESTIC_ULTIMATE_OF','GLOBAL_ULTIMATE_OF')
4427          AND    rel.actual_content_source = 'DNB'
4428          AND    rel.start_date <= sysdate
4429          AND    NVL(rel.end_date, to_date('12/31/4712','MM/DD/YYYY')) > sysdate
4430          AND    rel.subject_table_name = 'HZ_PARTIES'
4431          AND    rel.object_table_name = 'HZ_PARTIES';
4432 
4433 CURSOR c_contact_point IS
4434     SELECT  contact_point_id, phone_line_type
4435     FROM    hz_contact_points
4436     WHERE   owner_table_name = 'HZ_PARTIES'
4437     AND     owner_table_id   = p_party_id
4438     AND     contact_point_type = 'PHONE'
4439     AND     actual_content_source = 'DNB'
4440     AND     phone_line_type in ('FAX','GEN')
4441     AND     status = 'A';
4442 
4443 CURSOR c_dnb_data_point IS
4444     SELECT data_point_id
4445     FROM   ar_cmgt_data_points_vl
4446     WHERE  data_point_category = 'DNB';
4447 
4448 
4449 
4450 BEGIN
4451     IF pg_wf_debug = 'Y'
4452     THEN
4453        ar_cmgt_util.wf_debug(p_case_folder_id,
4454               'ar_cmgt_data_points_pkg.build_dnb_case_folder()+');
4455     END IF;
4456     p_resultout := 0;
4457     -- build the case folder details table for DNB data. In case of
4458     -- dnb data datpoint value will be null.
4459     FOR c_dnb_data_point_rec IN c_dnb_data_point
4460     LOOP
4461         build_case_folder_details(
4462                 p_case_folder_id  => p_case_folder_id,
4463                 p_data_point_id   => c_dnb_data_point_rec.data_point_id,
4464                 p_data_point_value => NULL,
4465                 p_mode             => p_mode,
4466                 p_error_msg   => p_error_msg,
4467                 p_resultout   => p_resultout);
4468         IF p_resultout <> 0
4469         THEN
4470             p_error_msg := 'Error while populating DNB data, Data Points Id: '||
4471                             c_dnb_data_point_rec.data_point_id;
4472             return;
4473         END IF;
4474 
4475     END LOOP;
4476 
4477     -- disable the policy function in hz tables
4478     -- so that Enable the visibility of DNB data
4479     hz_common_pub.disable_cont_source_security;
4480     -- Get all primary keys from HZ table for DNB reports
4481 
4482     -- Get information from hz_party
4483     BEGIN
4484         SELECT hq_branch_ind
4485         INTO   l_hq_branch_ind
4486         FROM   hz_parties
4487         WHERE  party_id = p_party_id;
4488 
4489         populate_dnb_data(
4490             p_case_folder_id        =>      p_case_folder_id,
4491             p_source_table_name     =>      'HZ_PARTIES',
4492             p_source_key            =>      p_party_id,
4493             p_source_key_column_name =>     'PARTY_ID',
4494             p_mode                   =>     p_mode);
4495     EXCEPTION
4496         WHEN NO_DATA_FOUND THEN
4497             NULL;
4498         WHEN OTHERS THEN
4499             p_resultout := 1;
4503     END;
4500             p_error_msg := 'SqlError While retrieving Data from HZ_PARTIES '||sqlerrm;
4501             ar_cmgt_util.wf_debug(p_case_folder_id, p_error_msg);
4502             return;
4504 
4505     -- Get organization profile ID
4506 
4507     BEGIN
4508         SELECT  ORGANIZATION_PROFILE_ID
4509         INTO    l_organization_profile_id
4510         FROM    HZ_ORGANIZATION_PROFILES
4511         WHERE   party_id = p_party_id
4512         AND     effective_end_date IS NULL
4513         AND     ACTUAL_CONTENT_SOURCE = 'DNB';
4514 
4515         populate_dnb_data(
4516                 p_case_folder_id            =>  p_case_folder_id,
4517                 p_source_table_name         =>  'HZ_ORGANIZATION_PROFILES',
4518                 p_source_key                =>  l_organization_profile_id,
4519                 p_source_key_column_name    =>  'ORGANIZATION_PROFILE_ID',
4520                 p_mode                      =>  p_mode);
4521     EXCEPTION
4522         WHEN NO_DATA_FOUND THEN
4523             NULL;
4524         WHEN OTHERS THEN
4525             p_resultout := 1;
4526             p_error_msg := 'SqlError While retrieving Data from HZ_ORGANIZATION_PROFILES '||sqlerrm;
4527             ar_cmgt_util.wf_debug(p_case_folder_id, p_error_msg);
4528             return;
4529     END;
4530     -- Get location id
4531     l_location_id := hz_dnbui_pvt.get_location_id(p_party_id, 'DNB');
4532     IF ( l_location_id IS NOT NULL )
4533     THEN
4534 
4535         populate_dnb_data(
4536                 p_case_folder_id        =>  p_case_folder_id,
4537                 p_source_table_name     =>  'HZ_LOCATIONS',
4538                 p_source_key            =>  l_location_id,
4539                 p_source_key_column_name => 'LOCATION_ID',
4540                 p_mode                  =>  p_mode);
4541     END IF;
4542 
4543     -- Get contact point id for phone line type 'GEN'
4544 
4545     FOR c_contact_point_rec IN c_contact_point
4546     LOOP
4547 
4548         populate_dnb_data(
4549              p_case_folder_id           =>  p_case_folder_id,
4550              p_source_table_name        =>  'HZ_CONTACT_POINTS',
4551              p_source_key               =>  c_contact_point_rec.contact_point_id,
4552              p_source_key_column_name   =>  'CONTACT_POINT_ID',
4553              p_mode                     =>  p_mode,
4554              p_source_key_type          =>  c_contact_point_rec.phone_line_type,
4555              p_source_key_column_type   =>  'CONTACT_POINT_TYPE');
4556     END LOOP;
4557 
4558     -- get credit ratings
4559     l_credit_rating_id := hz_dnbui_pvt.get_max_credit_rating_id(p_party_id, 'DNB');
4560 
4561     IF l_credit_rating_id IS NOT NULL
4562     THEN
4563         populate_dnb_data(
4564             p_case_folder_id        =>  p_case_folder_id,
4565             p_source_table_name     =>  'HZ_CREDIT_RATINGS',
4566             p_source_key            =>  l_credit_rating_id,
4567             p_source_key_column_name => 'CREDIT_RATING_ID',
4568             p_mode                  =>  p_mode
4569             );
4570     END IF;
4571     -- get financial report id for Balance Sheet
4572     l_financial_report_id :=
4573             hz_dnbui_pvt.get_max_financial_report_id(p_party_id,'BALANCE_SHEET','DNB');
4574     l_balance_sheet_id := l_financial_report_id;
4575     IF l_financial_report_id IS NOT NULL
4576     THEN
4577             populate_dnb_data(
4578                 p_case_folder_id            =>  p_case_folder_id,
4579                 p_source_table_name         =>  'HZ_FINANCIAL_REPORTS',
4580                 p_source_key                =>  l_financial_report_id,
4581                 p_source_key_column_name    =>  'FINANCIAL_REPORT_ID',
4582                 p_mode                      =>  p_mode,
4583                 p_source_key_type           =>  'BALANCE_SHEET',
4584                 p_source_key_column_type    =>  'TYPE_OF_FINANCIAL_REPORT');
4585 
4586     END IF;
4587 
4588     l_financial_report_id :=
4589             hz_dnbui_pvt.get_max_financial_report_id(p_party_id,'INCOME_STATEMENT','DNB');
4590     l_income_statement_id := l_financial_report_id;
4591     IF l_financial_report_id IS NOT NULL
4592     THEN
4593             populate_dnb_data(
4594                 p_case_folder_id            =>  p_case_folder_id,
4595                 p_source_table_name         =>  'HZ_FINANCIAL_REPORTS',
4596                 p_source_key                =>  l_financial_report_id,
4597                 p_source_key_column_name    =>  'FINANCIAL_REPORT_ID',
4598                 p_mode                      =>  p_mode,
4599                 p_source_key_type           =>  'INCOME_STATEMENT',
4600                 p_source_key_column_type    =>  'TYPE_OF_FINANCIAL_REPORT');
4601 
4602     END IF;
4603 
4604     l_financial_report_id :=
4605             hz_dnbui_pvt.get_max_financial_report_id(p_party_id,'TANGIBLE_NET_WORTH','DNB');
4609     THEN
4606     l_tangible_net_worth_id := l_financial_report_id;
4607 
4608     IF l_financial_report_id IS NOT NULL
4610             populate_dnb_data(
4611                 p_case_folder_id            =>  p_case_folder_id,
4612                 p_source_table_name         =>  'HZ_FINANCIAL_REPORTS',
4613                 p_source_key                =>  l_financial_report_id,
4614                 p_source_key_column_name    =>  'FINANCIAL_REPORT_ID',
4615                 p_mode                      =>  p_mode,
4616                 p_source_key_type           =>  'TANGIBLE_NET_WORTH',
4617                 p_source_key_column_type    =>  'TYPE_OF_FINANCIAL_REPORT');
4618 
4619     END IF;
4620 
4621     -- Build all the scorable data points which is releated to Tangible_net_worth
4622 
4623 
4624     l_financial_report_id :=
4625             hz_dnbui_pvt.get_max_financial_report_id(p_party_id,'ANNUAL_SALES_VOLUME','DNB');
4626     l_annual_sales_volume_id := l_financial_report_id;
4627     IF l_financial_report_id IS NOT NULL
4628     THEN
4629             populate_dnb_data(
4630                 p_case_folder_id            =>  p_case_folder_id,
4631                 p_source_table_name         =>  'HZ_FINANCIAL_REPORTS',
4632                 p_source_key                =>  l_financial_report_id,
4633                 p_source_key_column_name    =>  'FINANCIAL_REPORT_ID',
4634                 p_mode                      =>  p_mode,
4635                 p_source_key_type           =>  'ANNUAL_SALES_VOLUME',
4636                 p_source_key_column_type    =>  'TYPE_OF_FINANCIAL_REPORT');
4637 
4638     END IF;
4639 
4640 
4641    /* -- populate relationship model
4642     FOR c_relationship_rec IN c_relationships
4643     LOOP
4644             populate_dnb_data(
4645                 p_case_folder_id            =>  p_case_folder_id,
4646                 p_source_table_name         =>  'HZ_RELATIONSHIPS',
4647                 p_source_key                =>  c_relationship_rec.relationship_id,
4648                 p_source_key_column_name    =>  'RELATIONSHIP_ID',
4649                 p_mode                      =>  p_mode,
4650                 p_source_key_type           =>  c_relationship_rec.relationship_code,
4651                 p_source_key_column_type    =>  'RELATIONSHIP_CODE'
4652                 );
4653     END LOOP;
4654     */
4655     -- Now create the scorable data into case folder table
4656 
4657     BUILD_DNB_SCOREABLE_DATA
4658             (p_case_folder_id   => p_case_folder_id,
4659              p_mode             => p_mode,
4660              p_org_profile_id   => l_organization_profile_id,
4661              p_credit_rating_id => l_credit_rating_id,
4662              p_location_id    => l_location_id,
4663              p_balance_sheet_id => l_balance_sheet_id,
4664              p_income_statement_id   => l_income_statement_id,
4665              p_tangible_net_worth_id   => l_tangible_net_worth_id,
4666              p_annual_sales_volume_id  => l_annual_sales_volume_id,
4667              p_resultout        =>  p_resultout,
4668              p_error_msg        =>  p_error_msg );
4669 
4670     -- disable the visibility of DNB data
4671     hz_common_pub.enable_cont_source_security;
4672     IF pg_wf_debug = 'Y'
4673     THEN
4674        ar_cmgt_util.wf_debug(p_case_folder_id,
4675               'ar_cmgt_data_points_pkg.build_dnb_case_folder()-');
4676     END IF;
4677 
4678     EXCEPTION
4679         WHEN OTHERS THEN
4680             p_resultout := 1;
4681             p_error_msg := 'Error while populating DNB data '||sqlerrm;
4682             ar_cmgt_util.wf_debug(p_case_folder_id, p_error_msg);
4683 END;
4684 
4685 /* kosrinv ..Changing The procedure to store the data point values in a proper format ..
4686              bug 5525814     ...............*/
4687 PROCEDURE GATHER_DATA_POINTS(
4688             p_party_id              IN   NUMBER,
4689             p_cust_account_id       IN   NUMBER,
4690             p_cust_acct_site_id     IN   NUMBER,
4691             p_trx_currency          IN   VARCHAR2,
4692             p_org_id                IN   NUMBER,
4693             p_check_list_id         IN   NUMBER,
4694             p_credit_request_id     IN   NUMBER,
4695             p_score_model_id        IN   NUMBER,
4696             p_credit_classification IN   VARCHAR2,
4697             p_review_type           IN   VARCHAR2,
4698             p_case_folder_number    IN   VARCHAR2,
4699             p_mode                  IN   VARCHAR2,
4700             p_limit_currency        OUT nocopy  VARCHAR2,
4701             p_case_folder_id        IN OUT nocopy  NUMBER,
4702             p_error_msg             OUT nocopy  VARCHAR2,
4703             p_resultout             OUT nocopy  VARCHAR2) IS
4704 
4705 
4706 
4707 l_curr_array_list               curr_array_type;
4708 l_check_list_id                 ar_cmgt_check_lists.check_list_id%type;
4709 summary_rec_str                 VARCHAR2(4000);
4710 l_certified_dso_days            ar_cmgt_setup_options.cer_dso_days%type;
4711 l_data_point_id                 NUMBER;
4712 l_data_point_name               ar_cmgt_data_points_vl.data_point_name%type;
4713 l_case_folder_id                NUMBER;
4714 l_error_msg                     VARCHAR2(4000);
4715 l_resultout                     VARCHAR2(2000);
4716 l_period                        ar_cmgt_setup_options.period%type;
4717 l_data_point_value              ar_cmgt_cf_dtls.data_point_value%type;
4718 l_start_pos                     NUMBER;
4719 l_end_pos                       NUMBER;
4720 l_combo_string                  VARCHAR2(100);
4721 l_data_point_id_end_pos         NUMBER;
4725 l_limit_currency                VARCHAR2(30);
4722 l_global_exposure_flag          hz_credit_usage_rule_sets_b.global_exposure_flag%type;
4723 l_analysis_level                VARCHAR2(1);
4724 l_trx_limit                     NUMBER;
4726 l_include_all_flag              VARCHAR2(1);
4727 l_curr_tbl                      HZ_CREDIT_USAGES_PKG.curr_tbl_type;
4728 l_excl_curr_list                VARCHAR2(2000);
4729 l_exchange_rate_type            ar_cmgt_setup_options.default_exchange_rate_type%type;
4730 l_overall_limit                 NUMBER;
4731 l_cust_acct_profile_amt_id      NUMBER;
4732 l_case_folder_id_1              number;
4733 l_dso                           number;
4734 l_ddso                          number;
4735 l_deno_dso                      number := 1;
4736 l_deno_ddso                     number := 1;
4737 l_numerator_dso                 number;
4738 l_numerator_ddso                number;
4739 l_party_number                  hz_parties.party_number%type;
4740 l_score                         number;
4741 
4742 
4743 
4744 -- No. of coulmns in each cursor. if select columns in any of the cursor we need to
4745 -- change these numberes.
4746 summary_rec_col_count           NUMBER := 30;
4747 bal_summary_rec_col_count       NUMBER := 23;
4748 
4749 
4750 /*********** CAUTIONS **********************************************
4751 -- if you are adding/removing  an column in this cursor please
4752 -- update the summary_rec_col_count varibale with proper number.
4753 -- case receipts amount (156) is stored in summary tables as -ve
4754 -- So we are multiplying to -1 to show the opposite value as part of
4755 -- bug fixes 2824382.
4756 ********************************************************************/
4757 CURSOR c_party_summary IS
4758     SELECT
4759            '8'||'^'||fnd_number.number_to_canonical(SUM(days_credit_granted)) days_credit_granted,
4760            '13'||'^'||fnd_number.number_to_canonical(SUM(nsf_payment_count)) nsf_payment_count,
4761            '14'||'^'||fnd_number.number_to_canonical(SUM(nsf_payment_amount)) nsf_payment_amount,
4762            '17'||'^'||fnd_number.number_to_canonical(SUM(credit_memo_value)) credit_memo_value,
4763            '18'||'^'||fnd_number.number_to_canonical(SUM(credit_memo_count)) credit_memo_count,
4764            '21'||'^'||fnd_number.number_to_canonical(
4765                          ROUND((SUM(x_tot_inv_inst_paid)-
4766                                 SUM(x_tot_inv_inst_paid_late)) * 100 /
4767                          DECODE(SUM(x_tot_inv_inst_paid),0,1,SUM(x_tot_inv_inst_paid)),2)
4768                       ) per_inv_paid_promptly,
4769            '22'||'^'||fnd_number.number_to_canonical(
4770                          ROUND((SUM(x_tot_inv_inst_paid_late)) * 100 /
4771                          DECODE(SUM(x_tot_inv_inst_paid),0,1,SUM(x_tot_inv_inst_paid)),2)
4772                       ) per_inv_paid_late,
4773            '23'||'^'||fnd_number.number_to_canonical(
4774                          ROUND((SUM(x_tot_disc_inv_inst)) * 100 /
4775                          DECODE(SUM(x_tot_inv_inst_paid),0,1,SUM(x_tot_inv_inst_paid)),2)
4776                       ) per_inv_with_discount,
4777            '41'||'^'||fnd_number.number_to_canonical(SUM(inv_paid_amount)) inv_paid_amount,
4778            '42'||'^'||fnd_number.number_to_canonical(SUM(inv_paid_count)) inv_paid_count,
4779            '43'||'^'||fnd_number.number_to_canonical(SUM(earned_disc_value)) earned_disc_value,
4780            '44'||'^'||fnd_number.number_to_canonical(SUM(earned_disc_count)) earned_disc_count,
4781            '45'||'^'||fnd_number.number_to_canonical(SUM(unearned_disc_value)) unearned_disc_value,
4782            '46'||'^'||fnd_number.number_to_canonical(SUM(unearned_disc_count)) unearned_disc_count,
4783            '156'||'^'||fnd_number.number_to_canonical(SUM(total_cash_receipts_value)) total_cash_receipts_value,
4784            '157'||'^'||fnd_number.number_to_canonical(SUM(total_cash_receipts_count)) total_cash_receipts_count,
4785            '158'||'^'||fnd_number.number_to_canonical(SUM(total_invoices_value)) total_invoices_value,
4786            '159'||'^'||fnd_number.number_to_canonical(SUM(total_invoices_count)) total_invoices_count,
4787            '160'||'^'||fnd_number.number_to_canonical(SUM(total_bills_receivables_value)) total_bills_receivables_value,
4788            '161'||'^'||fnd_number.number_to_canonical(SUM(total_bills_receivables_count)) total_bills_receivables_count,
4789            '162'||'^'||fnd_number.number_to_canonical(SUM(total_debit_memos_value)) total_debit_memos_value,
4790            '163'||'^'||fnd_number.number_to_canonical(SUM(total_debit_memos_count)) total_debit_memos_count,
4791            '164'||'^'||fnd_number.number_to_canonical(SUM(total_chargeback_value)) total_chargeback_value,
4792            '165'||'^'||fnd_number.number_to_canonical(SUM(total_chargeback_count)) total_chargeback_count,
4793            '166'||'^'||fnd_number.number_to_canonical(SUM(total_adjustments_value)) total_adjustments_value,
4794            '167'||'^'||fnd_number.number_to_canonical(SUM(total_adjustments_count)) total_adjustments_count,
4795            '168'||'^'||fnd_number.number_to_canonical(SUM(total_deposits_value)) total_deposits_value,
4796            '169'||'^'||fnd_number.number_to_canonical(SUM(total_deposits_count)) total_deposits_count
4797       FROM ( SELECT
4798             round(SUM(NVL(DAYS_CREDIT_GRANTED_SUM,0))/
4799                  decode(SUM(NVL(TOTAL_INVOICES_VALUE,0)),0,1,
4800                         SUM(NVL(TOTAL_INVOICES_VALUE,0))),2) days_credit_granted, -- days credit granted
4801             SUM(NVL(NSF_STOP_PAYMENT_COUNT,0)) nsf_payment_count, -- NSF/Stop Payment Count
4802             round(gl_currency_api.convert_amount(currency,
4803                 l_limit_currency,sysdate,
4804                 l_exchange_rate_type,
4805                 SUM(NVL(NSF_STOP_PAYMENT_AMOUNT,0))),2) nsf_payment_amount, -- NSF/Stop Payment Amount
4806             round(gl_currency_api.convert_amount(currency,
4807                 l_limit_currency,sysdate,
4808                 l_exchange_rate_type,
4812               up above */
4809                 SUM(NVL(TOTAL_CREDIT_MEMOS_VALUE,0))),2) credit_memo_value, -- Credit memos value
4810             SUM(NVL(TOTAL_CREDIT_MEMOS_COUNT,0)) credit_memo_count, -- Credit memos count
4811            /* 8692948 - sum the counts (by currency) here but do the math
4813             SUM(NVL(count_of_tot_inv_inst_paid,0))  x_tot_inv_inst_paid,
4814             SUM(NVL(count_of_inv_inst_paid_late,0)) x_tot_inv_inst_paid_late,
4815             SUM(NVL(count_of_disc_inv_inst,0))      x_tot_disc_inv_inst,
4816            /* End 8692948 */
4817             round(gl_currency_api.convert_amount(currency,
4818                 l_limit_currency,sysdate,
4819                 l_exchange_rate_type,
4820                 SUM(NVL(INV_PAID_AMOUNT,0))),2) inv_paid_amount, -- invoices paid amount
4821             SUM(NVL(count_of_tot_inv_inst_paid,0)) inv_paid_count, -- invoices paid count
4822             round(gl_currency_api.convert_amount(currency,
4823                 l_limit_currency,sysdate,
4824                 l_exchange_rate_type,
4825                 SUM(NVL(TOTAL_EARNED_DISC_VALUE,0))),2) earned_disc_value, -- Earned Dscount Value
4826             SUM(NVL(TOTAL_EARNED_DISC_COUNT,0)) earned_disc_count, -- Earned Dscount count
4827             round(gl_currency_api.convert_amount(currency,
4828                 l_limit_currency,sysdate,
4829                 l_exchange_rate_type,
4830                 SUM(NVL(TOTAL_UNEARNED_DISC_VALUE,0))),2) unearned_disc_value, -- UnEarned Dscount Value
4831             SUM(NVL(TOTAL_UNEARNED_DISC_COUNT,0)) unearned_disc_count, -- UnEarned Dscount count
4832             (round(gl_currency_api.convert_amount(currency,
4833                 l_limit_currency,sysdate,
4834                 l_exchange_rate_type,
4835                 SUM(NVL(TOTAL_CASH_RECEIPTS_VALUE,0))),2) * -1) total_cash_receipts_value, -- see the comment above
4836             SUM(NVL(TOTAL_CASH_RECEIPTS_COUNT,0)) total_cash_receipts_count,
4837             round(gl_currency_api.convert_amount(currency,
4838                 l_limit_currency,sysdate,
4839                 l_exchange_rate_type,
4840                 SUM(NVL(TOTAL_INVOICES_VALUE,0))),2) total_invoices_value,
4841              SUM(TOTAL_INVOICES_COUNT) total_invoices_count,
4842              round(gl_currency_api.convert_amount(currency,
4843                 l_limit_currency,sysdate,
4844                 l_exchange_rate_type,
4848                 l_limit_currency,sysdate,
4845                 SUM(NVL(TOTAL_BILLS_RECEIVABLES_VALUE,0))),2) total_bills_receivables_value,
4846              SUM(NVL(TOTAL_BILLS_RECEIVABLES_COUNT,0)) TOTAL_BILLS_RECEIVABLES_count,
4847              round(gl_currency_api.convert_amount(currency,
4849                 l_exchange_rate_type,
4850                 SUM(NVL(TOTAL_DEBIT_MEMOS_VALUE,0))),2) total_debit_memos_value,
4851              SUM(NVL(TOTAL_DEBIT_MEMOS_COUNT,0)) TOTAL_debit_memos_count,
4852              round(gl_currency_api.convert_amount(currency,
4853                 l_limit_currency,sysdate,
4854                 l_exchange_rate_type,
4855                 SUM(NVL(TOTAL_CHARGEBACK_VALUE,0))),2) total_chargeback_value,
4856              SUM(NVL(TOTAL_chargeback_COUNT,0)) TOTAL_chargeback_count ,
4857              round(gl_currency_api.convert_amount(currency,
4858                 l_limit_currency,sysdate,
4859                 l_exchange_rate_type,
4860                 SUM(NVL(TOTAL_ADJUSTMENTS_VALUE,0))),2) total_adjustments_value,
4861              SUM(NVL(TOTAL_adjustments_COUNT,0)) TOTAL_adjustments_count ,
4862              round(gl_currency_api.convert_amount(currency,
4863                 l_limit_currency,sysdate,
4864                 l_exchange_rate_type,
4865                 SUM(NVL(TOTAL_DEPOSITS_VALUE,0))),2) total_deposits_value,
4866              SUM(NVL(TOTAL_deposits_COUNT,0)) TOTAL_deposits_count
4867              FROM   AR_TRX_SUMMARY
4868              WHERE  CUST_ACCOUNT_ID in (select TO_NUMBER(cust_account_id)  -- Fix for Bug 12670874
4869                                FROM   hz_cust_accounts
4870                                WHERE  party_id in
4871                                 ( SELECT child_id
4872                                   from hz_hierarchy_nodes
4873                                   where parent_object_type = 'ORGANIZATION'
4874                                   and parent_table_name = 'HZ_PARTIES'
4875                                   and child_object_type = 'ORGANIZATION'
4876                                   and parent_id = p_party_id
4877                                   and effective_start_date <= sysdate
4878                                   and effective_end_date >= sysdate
4879                                   and  hierarchy_type = FND_PROFILE.VALUE('AR_CMGT_HIERARCHY_TYPE')
4880                                   and  g_source_name <> 'LNS'
4881                                   union select p_party_id from dual
4882                                   UNION
4883                     select hz_party_id
4884                     from LNS_LOAN_PARTICIPANTS_V
4885                     where loan_id = g_source_id
4889                           (sysdate between start_date_active and end_date_active)
4886                     and   participant_type_code = 'COBORROWER'
4887                     and   g_source_name = 'LNS'
4888                     and (end_date_active is null OR
4890                           )
4891                              ))
4892               and   CURRENCY     IN  ( SELECT CURRENCY FROM
4893                                        ar_cmgt_curr_usage_gt
4894                                        WHERE nvl(credit_request_id,p_credit_request_id) = p_credit_request_id)
4895                  /*( SELECT * FROM
4896                                       TABLE(CAST(l_curr_array_list AS curr_array_type))) */
4897              and    as_of_date  >= ADD_MONTHS(sysdate,(-l_period))
4898              group by currency );
4899 
4900 
4901 CURSOR c_account_summary IS
4902     SELECT
4903            '8'||'^'||fnd_number.number_to_canonical(SUM(days_credit_granted)) days_credit_granted,
4904            '13'||'^'||fnd_number.number_to_canonical(SUM(nsf_payment_count)) nsf_payment_count,
4905            '14'||'^'||fnd_number.number_to_canonical(SUM(nsf_payment_amount)) nsf_payment_amount,
4906            '17'||'^'||fnd_number.number_to_canonical(SUM(credit_memo_value)) credit_memo_value,
4907            '18'||'^'||fnd_number.number_to_canonical(SUM(credit_memo_count)) credit_memo_count,
4908            '21'||'^'||fnd_number.number_to_canonical(
4909                          ROUND((SUM(x_tot_inv_inst_paid)-
4910                                 SUM(x_tot_inv_inst_paid_late)) * 100 /
4911                          DECODE(SUM(x_tot_inv_inst_paid),0,1,SUM(x_tot_inv_inst_paid)),2)
4912                       ) per_inv_paid_promptly,
4913            '22'||'^'||fnd_number.number_to_canonical(
4914                          ROUND((SUM(x_tot_inv_inst_paid_late)) * 100 /
4915                          DECODE(SUM(x_tot_inv_inst_paid),0,1,SUM(x_tot_inv_inst_paid)),2)
4916                       ) per_inv_paid_late,
4917            '23'||'^'||fnd_number.number_to_canonical(
4918                          ROUND((SUM(x_tot_disc_inv_inst)) * 100 /
4919                          DECODE(SUM(x_tot_inv_inst_paid),0,1,SUM(x_tot_inv_inst_paid)),2)
4920                       ) per_inv_with_discount,
4921            '41'||'^'||fnd_number.number_to_canonical(SUM(inv_paid_amount)) inv_paid_amount,
4922            '42'||'^'||fnd_number.number_to_canonical(SUM(inv_paid_count)) inv_paid_count,
4923            '43'||'^'||fnd_number.number_to_canonical(SUM(earned_disc_value)) earned_disc_value,
4924            '44'||'^'||fnd_number.number_to_canonical(SUM(earned_disc_count)) earned_disc_count,
4925            '45'||'^'||fnd_number.number_to_canonical(SUM(unearned_disc_value)) unearned_disc_value,
4926            '46'||'^'||fnd_number.number_to_canonical(SUM(unearned_disc_count)) unearned_disc_count,
4927            '156'||'^'||fnd_number.number_to_canonical(SUM(total_cash_receipts_value)) total_cash_receipts_value,
4928            '157'||'^'||fnd_number.number_to_canonical(SUM(total_cash_receipts_count)) total_cash_receipts_count,
4929            '158'||'^'||fnd_number.number_to_canonical(SUM(total_invoices_value)) total_invoices_value,
4930            '159'||'^'||fnd_number.number_to_canonical(SUM(total_invoices_count)) total_invoices_count,
4931            '160'||'^'||fnd_number.number_to_canonical(SUM(total_bills_receivables_value)) total_bills_receivables_value,
4932            '161'||'^'||fnd_number.number_to_canonical(SUM(total_bills_receivables_count)) total_bills_receivables_count,
4933            '162'||'^'||fnd_number.number_to_canonical(SUM(total_debit_memos_value)) total_debit_memos_value,
4934            '163'||'^'||fnd_number.number_to_canonical(SUM(total_debit_memos_count)) total_debit_memos_count,
4935            '164'||'^'||fnd_number.number_to_canonical(SUM(total_chargeback_value)) total_chargeback_value,
4936            '165'||'^'||fnd_number.number_to_canonical(SUM(total_chargeback_count)) total_chargeback_count,
4937            '166'||'^'||fnd_number.number_to_canonical(SUM(total_adjustments_value)) total_adjustments_value,
4938            '167'||'^'||fnd_number.number_to_canonical(SUM(total_adjustments_count)) total_adjustments_count,
4939            '168'||'^'||fnd_number.number_to_canonical(SUM(total_deposits_value)) total_deposits_value,
4940            '169'||'^'||fnd_number.number_to_canonical(SUM(total_deposits_count)) total_deposits_count
4941       FROM ( SELECT
4942             round(SUM(NVL(DAYS_CREDIT_GRANTED_SUM,0))/
4943                  decode(SUM(NVL(TOTAL_INVOICES_VALUE,0)),0,1,
4947                 l_limit_currency,sysdate,
4944                         SUM(NVL(TOTAL_INVOICES_VALUE,0))),2) days_credit_granted, -- days credit granted
4945             SUM(NVL(NSF_STOP_PAYMENT_COUNT,0)) nsf_payment_count, -- NSF/Stop Payment Count
4946             round(gl_currency_api.convert_amount(currency,
4948                 l_exchange_rate_type,
4949                 SUM(NVL(NSF_STOP_PAYMENT_AMOUNT,0))),2) nsf_payment_amount, -- NSF/Stop Payment Amount
4950             round(gl_currency_api.convert_amount(currency,
4951                 l_limit_currency,sysdate,
4952                 l_exchange_rate_type,
4953                 SUM(NVL(TOTAL_CREDIT_MEMOS_VALUE,0))),2) credit_memo_value, -- Credit memos value
4954             SUM(NVL(TOTAL_CREDIT_MEMOS_COUNT,0)) credit_memo_count, -- Credit memos count
4955            /* 8692948 - sum the counts (by currency) here but do the math
4956               up above */
4957             SUM(NVL(count_of_tot_inv_inst_paid,0))  x_tot_inv_inst_paid,
4958             SUM(NVL(count_of_inv_inst_paid_late,0)) x_tot_inv_inst_paid_late,
4959             SUM(NVL(count_of_disc_inv_inst,0))      x_tot_disc_inv_inst,
4960            /* End 8692948 */
4961             round(gl_currency_api.convert_amount(currency,
4962                 l_limit_currency,sysdate,
4963                 l_exchange_rate_type,
4964                 SUM(NVL(INV_PAID_AMOUNT,0))),2) inv_paid_amount, -- invoices paid amount
4965             SUM(NVL(count_of_tot_inv_inst_paid,0)) inv_paid_count, -- invoices paid count
4966             round(gl_currency_api.convert_amount(currency,
4967                 l_limit_currency,sysdate,
4968                 l_exchange_rate_type,
4969                 SUM(NVL(TOTAL_EARNED_DISC_VALUE,0))),2) earned_disc_value, -- Earned Dscount Value
4970             SUM(NVL(TOTAL_EARNED_DISC_COUNT,0)) earned_disc_count, -- Earned Dscount count
4971             round(gl_currency_api.convert_amount(currency,
4972                 l_limit_currency,sysdate,
4973                 l_exchange_rate_type,
4974                 SUM(NVL(TOTAL_UNEARNED_DISC_VALUE,0))),2) unearned_disc_value, -- UnEarned Dscount Value
4975             SUM(NVL(TOTAL_UNEARNED_DISC_COUNT,0)) unearned_disc_count, -- UnEarned Dscount count
4976             (round(gl_currency_api.convert_amount(currency,
4977                 l_limit_currency,sysdate,
4978                 l_exchange_rate_type,
4979                 SUM(NVL(TOTAL_CASH_RECEIPTS_VALUE,0))),2) * -1) total_cash_receipts_value, -- see the comments above
4980             SUM(NVL(TOTAL_CASH_RECEIPTS_COUNT,0)) total_cash_receipts_count,
4981             round(gl_currency_api.convert_amount(currency,
4982                 l_limit_currency,sysdate,
4983                 l_exchange_rate_type,
4987                 l_limit_currency,sysdate,
4984                 SUM(NVL(TOTAL_INVOICES_VALUE,0))),2) total_invoices_value,
4985              SUM(NVL(TOTAL_INVOICES_COUNT,0)) total_invoices_count,
4986              round(gl_currency_api.convert_amount(currency,
4988                 l_exchange_rate_type,
4989                 SUM(NVL(TOTAL_BILLS_RECEIVABLES_VALUE,0))),2) total_bills_receivables_value,
4990              SUM(NVL(TOTAL_BILLS_RECEIVABLES_COUNT,0)) TOTAL_BILLS_RECEIVABLES_count,
4991              round(gl_currency_api.convert_amount(currency,
4992                 l_limit_currency,sysdate,
4993                 l_exchange_rate_type,
4994                 SUM(NVL(TOTAL_DEBIT_MEMOS_VALUE,0))),2) total_debit_memos_value,
4995              SUM(NVL(TOTAL_DEBIT_MEMOS_COUNT,0)) TOTAL_debit_memos_count,
4996              round(gl_currency_api.convert_amount(currency,
4997                 l_limit_currency,sysdate,
4998                 l_exchange_rate_type,
4999                 SUM(NVL(TOTAL_CHARGEBACK_VALUE,0))),2) total_chargeback_value,
5000              SUM(NVL(TOTAL_chargeback_COUNT,0)) TOTAL_chargeback_count ,
5001              round(gl_currency_api.convert_amount(currency,
5002                 l_limit_currency,sysdate,
5003                 l_exchange_rate_type,
5004                 SUM(NVL(TOTAL_ADJUSTMENTS_VALUE,0))),2) total_adjustments_value,
5005              SUM(NVL(TOTAL_adjustments_COUNT,0)) TOTAL_adjustments_count ,
5006              round(gl_currency_api.convert_amount(currency,
5007                 l_limit_currency,sysdate,
5008                 l_exchange_rate_type,
5009                 SUM(NVL(TOTAL_DEPOSITS_VALUE,0))),2) total_deposits_value,
5010              SUM(NVL(TOTAL_deposits_COUNT,0)) TOTAL_deposits_count
5011              FROM   AR_TRX_SUMMARY
5012              WHERE  org_id          = decode(l_global_exposure_flag,'Y', org_id, 'N',
5013                                      decode(p_org_id,null, org_id, p_org_id), null,
5014                                      decode(p_org_id,null, org_id, p_org_id))
5015              and    CUST_ACCOUNT_ID = p_cust_account_id
5016               and   CURRENCY     IN  ( SELECT CURRENCY FROM
5017                                        ar_cmgt_curr_usage_gt
5018                                        WHERE nvl(credit_request_id,p_credit_request_id) = p_credit_request_id)
5019              and    as_of_date  >= ADD_MONTHS(sysdate,(-l_period))
5020              group by currency );
5021 
5022 CURSOR c_site_summary IS
5023     SELECT
5024            '8'||'^'||fnd_number.number_to_canonical(SUM(days_credit_granted)) days_credit_granted,
5025            '13'||'^'||fnd_number.number_to_canonical(SUM(nsf_payment_count)) nsf_payment_count,
5026            '14'||'^'||fnd_number.number_to_canonical(SUM(nsf_payment_amount)) nsf_payment_amount,
5027            '17'||'^'||fnd_number.number_to_canonical(SUM(credit_memo_value)) credit_memo_value,
5028            '18'||'^'||fnd_number.number_to_canonical(SUM(credit_memo_count)) credit_memo_count,
5029            '21'||'^'||fnd_number.number_to_canonical(
5030                          ROUND((SUM(x_tot_inv_inst_paid)-
5031                                 SUM(x_tot_inv_inst_paid_late)) * 100 /
5032                          DECODE(SUM(x_tot_inv_inst_paid),0,1,SUM(x_tot_inv_inst_paid)),2)
5033                       ) per_inv_paid_promptly,
5034            '22'||'^'||fnd_number.number_to_canonical(
5035                          ROUND((SUM(x_tot_inv_inst_paid_late)) * 100 /
5036                          DECODE(SUM(x_tot_inv_inst_paid),0,1,SUM(x_tot_inv_inst_paid)),2)
5037                       ) per_inv_paid_late,
5038            '23'||'^'||fnd_number.number_to_canonical(
5039                          ROUND((SUM(x_tot_disc_inv_inst)) * 100 /
5040                          DECODE(SUM(x_tot_inv_inst_paid),0,1,SUM(x_tot_inv_inst_paid)),2)
5041                       ) per_inv_with_discount,
5042            '41'||'^'||fnd_number.number_to_canonical(SUM(inv_paid_amount)) inv_paid_amount,
5043            '42'||'^'||fnd_number.number_to_canonical(SUM(inv_paid_count)) inv_paid_count,
5044            '43'||'^'||fnd_number.number_to_canonical(SUM(earned_disc_value)) earned_disc_value,
5045            '44'||'^'||fnd_number.number_to_canonical(SUM(earned_disc_count)) earned_disc_count,
5046            '45'||'^'||fnd_number.number_to_canonical(SUM(unearned_disc_value)) unearned_disc_value,
5047            '46'||'^'||fnd_number.number_to_canonical(SUM(unearned_disc_count)) unearned_disc_count,
5048            '156'||'^'||fnd_number.number_to_canonical(SUM(total_cash_receipts_value)) total_cash_receipts_value,
5049            '157'||'^'||fnd_number.number_to_canonical(SUM(total_cash_receipts_count)) total_cash_receipts_count,
5050            '158'||'^'||fnd_number.number_to_canonical(SUM(total_invoices_value)) total_invoices_value,
5051            '159'||'^'||fnd_number.number_to_canonical(SUM(total_invoices_count)) total_invoices_count,
5052            '160'||'^'||fnd_number.number_to_canonical(SUM(total_bills_receivables_value)) total_bills_receivables_value,
5053            '161'||'^'||fnd_number.number_to_canonical(SUM(total_bills_receivables_count)) total_bills_receivables_count,
5054            '162'||'^'||fnd_number.number_to_canonical(SUM(total_debit_memos_value)) total_debit_memos_value,
5055            '163'||'^'||fnd_number.number_to_canonical(SUM(total_debit_memos_count)) total_debit_memos_count,
5056            '164'||'^'||fnd_number.number_to_canonical(SUM(total_chargeback_value)) total_chargeback_value,
5057            '165'||'^'||fnd_number.number_to_canonical(SUM(total_chargeback_count)) total_chargeback_count,
5058            '166'||'^'||fnd_number.number_to_canonical(SUM(total_adjustments_value)) total_adjustments_value,
5059            '167'||'^'||fnd_number.number_to_canonical(SUM(total_adjustments_count)) total_adjustments_count,
5060            '168'||'^'||fnd_number.number_to_canonical(SUM(total_deposits_value)) total_deposits_value,
5061            '169'||'^'||fnd_number.number_to_canonical(SUM(total_deposits_count)) total_deposits_count
5062       FROM ( SELECT
5063             round(SUM(NVL(DAYS_CREDIT_GRANTED_SUM,0))/
5064                  decode(SUM(NVL(TOTAL_INVOICES_VALUE,0)),0,1,
5068                 l_limit_currency,sysdate,
5065                         SUM(NVL(TOTAL_INVOICES_VALUE,0))),2) days_credit_granted, -- days credit granted
5066             SUM(NVL(NSF_STOP_PAYMENT_COUNT,0)) nsf_payment_count, -- NSF/Stop Payment Count
5067             round(gl_currency_api.convert_amount(currency,
5069                 l_exchange_rate_type,
5070                 SUM(NVL(NSF_STOP_PAYMENT_AMOUNT,0))),2) nsf_payment_amount, -- NSF/Stop Payment Amount
5071             round(gl_currency_api.convert_amount(currency,
5072                 l_limit_currency,sysdate,
5073                 l_exchange_rate_type,
5074                 SUM(NVL(TOTAL_CREDIT_MEMOS_VALUE,0))),2) credit_memo_value, -- Credit memos value
5075             SUM(NVL(TOTAL_CREDIT_MEMOS_COUNT,0)) credit_memo_count, -- Credit memos count
5076            /* 8692948 - sum the counts (by currency) here but do the math
5077               up above */
5078             SUM(NVL(count_of_tot_inv_inst_paid,0))  x_tot_inv_inst_paid,
5079             SUM(NVL(count_of_inv_inst_paid_late,0)) x_tot_inv_inst_paid_late,
5080             SUM(NVL(count_of_disc_inv_inst,0))      x_tot_disc_inv_inst,
5081            /* End 8692948 */
5082             round(gl_currency_api.convert_amount(currency,
5083                 l_limit_currency,sysdate,
5084                 l_exchange_rate_type,
5085                 SUM(NVL(INV_PAID_AMOUNT,0))),2) inv_paid_amount, -- invoices paid amount
5086             SUM(NVL(count_of_tot_inv_inst_paid,0)) inv_paid_count, -- invoices paid count
5087             round(gl_currency_api.convert_amount(currency,
5088                 l_limit_currency,sysdate,
5089                 l_exchange_rate_type,
5090                 SUM(NVL(TOTAL_EARNED_DISC_VALUE,0))),2) earned_disc_value, -- Earned Dscount Value
5091             SUM(NVL(TOTAL_EARNED_DISC_COUNT,0)) earned_disc_count, -- Earned Dscount count
5092             round(gl_currency_api.convert_amount(currency,
5093                 l_limit_currency,sysdate,
5094                 l_exchange_rate_type,
5095                 SUM(NVL(TOTAL_UNEARNED_DISC_VALUE,0))),2) unearned_disc_value, -- UnEarned Dscount Value
5096             SUM(NVL(TOTAL_UNEARNED_DISC_COUNT,0)) unearned_disc_count, -- UnEarned Dscount count
5097             (round(gl_currency_api.convert_amount(currency,
5098                 l_limit_currency,sysdate,
5099                 l_exchange_rate_type,
5100                 SUM(NVL(TOTAL_CASH_RECEIPTS_VALUE,0))),2) * -1) total_cash_receipts_value, -- see the comments above
5101             SUM(TOTAL_CASH_RECEIPTS_COUNT) total_cash_receipts_count,
5102             round(gl_currency_api.convert_amount(currency,
5103                 l_limit_currency,sysdate,
5104                 l_exchange_rate_type,
5105                 SUM(NVL(TOTAL_INVOICES_VALUE,0))),2) total_invoices_value,
5106              SUM(NVL(TOTAL_INVOICES_COUNT,0)) total_invoices_count,
5107              round(gl_currency_api.convert_amount(currency,
5108                 l_limit_currency,sysdate,
5109                 l_exchange_rate_type,
5110                 SUM(NVL(TOTAL_BILLS_RECEIVABLES_VALUE,0))),2) total_bills_receivables_value,
5111              SUM(NVL(TOTAL_BILLS_RECEIVABLES_COUNT,0)) TOTAL_BILLS_RECEIVABLES_count,
5112              round(gl_currency_api.convert_amount(currency,
5113                 l_limit_currency,sysdate,
5114                 l_exchange_rate_type,
5115                 SUM(NVL(TOTAL_DEBIT_MEMOS_VALUE,0))),2) total_debit_memos_value,
5116              SUM(NVL(TOTAL_DEBIT_MEMOS_COUNT,0)) TOTAL_debit_memos_count,
5117              round(gl_currency_api.convert_amount(currency,
5118                 l_limit_currency,sysdate,
5119                 l_exchange_rate_type,
5120                 SUM(NVL(TOTAL_CHARGEBACK_VALUE,0))),2) total_chargeback_value,
5121              SUM(TOTAL_chargeback_COUNT) TOTAL_chargeback_count ,
5122              round(gl_currency_api.convert_amount(currency,
5123                 l_limit_currency,sysdate,
5124                 l_exchange_rate_type,
5125                 SUM(NVL(TOTAL_ADJUSTMENTS_VALUE,0))),2) total_adjustments_value,
5126              SUM(NVL(TOTAL_adjustments_COUNT,0)) TOTAL_adjustments_count ,
5127              round(gl_currency_api.convert_amount(currency,
5128                 l_limit_currency,sysdate,
5129                 l_exchange_rate_type,
5130                 SUM(NVL(TOTAL_DEPOSITS_VALUE,0))),2) total_deposits_value,
5131              SUM(NVL(TOTAL_deposits_COUNT,0)) TOTAL_deposits_count
5132              FROM   AR_TRX_SUMMARY
5133              WHERE  CUST_ACCOUNT_ID = p_cust_account_id
5134               and   CURRENCY     IN  ( SELECT a.CURRENCY FROM
5135                                        ar_cmgt_curr_usage_gt a
5136                                        WHERE nvl(credit_request_id,p_credit_request_id) = p_credit_request_id)
5137              and    as_of_date  >= ADD_MONTHS(sysdate,(-l_period))
5138              and    site_use_id  = p_cust_acct_site_id
5139              group by currency );
5140 
5141 
5142 
5143 
5144 /*********** CAUTIONS **********************************************
5145 -- if you are adding/removing  an column in this cursor please
5146 -- update the bal_summary_rec_col_count varibale with proper number.
5147 ********************************************************************/
5148 CURSOR c_party_bal_summary IS
5149     SELECT '34'||'^'||fnd_number.number_to_canonical(SUM(current_receivable_balance)) current_receivable_balance,
5150            '9'||'^'|| fnd_number.number_to_canonical(SUM(unapplied_cash_amount)) unapplied_cash_amount, -- unapplied case amount
5151            '10'||'^'||fnd_number.number_to_canonical(SUM(unapplied_cash_count)) unapplied_cash_count, -- unapplied cash count
5152            '48'||'^'||fnd_number.number_to_canonical(SUM(past_due_inv_value)) past_due_inv_value,
5153            '49'||'^'||fnd_number.number_to_canonical(SUM(past_due_inv_inst_count)) past_due_inv_inst_count,
5154            '50'||'^'||fnd_number.number_to_canonical(SUM(inv_amt_in_dispute)) inv_amt_in_dispute,
5155            '51'||'^'||fnd_number.number_to_canonical(SUM(disputed_inv_count)) disputed_inv_count,
5159            '171'||'^'||fnd_number.number_to_canonical(SUM(op_invoices_count)) op_invoices_count,
5156            '56'||'^'||fnd_number.number_to_canonical(SUM(pending_adj_value)) pending_adj_value,
5157            '58'||'^'||fnd_number.number_to_canonical(SUM(total_receipts_at_risk_value)) total_receipts_at_risk_value,
5158            '170'||'^'||fnd_number.number_to_canonical(SUM(op_invoices_value)) op_invoices_value,
5160            '172'||'^'||fnd_number.number_to_canonical(SUM(op_debit_memos_value)) op_debit_memos_value,
5161            '173'||'^'||fnd_number.number_to_canonical(SUM(op_debit_memos_count)) op_debit_memos_count,
5162            '174'||'^'||fnd_number.number_to_canonical(SUM(op_deposits_value)) op_deposits_value,
5163            '175'||'^'||fnd_number.number_to_canonical(SUM(op_deposits_count)) op_deposits_count,
5164            '176'||'^'||fnd_number.number_to_canonical(SUM(op_bills_receivables_value)) op_bills_receivables_value,
5165            '177'||'^'||fnd_number.number_to_canonical(SUM(op_bills_receivables_count)) op_bills_receivables_count,
5166            '178'||'^'||fnd_number.number_to_canonical(SUM(op_chargeback_value)) op_chargeback_value,
5167            '179'||'^'||fnd_number.number_to_canonical(SUM(op_chargeback_count)) op_chargeback_count,
5168            '180'||'^'||fnd_number.number_to_canonical(SUM(op_credit_memos_value)) op_credit_memos_value,
5169            '181'||'^'||fnd_number.number_to_canonical(SUM(op_credit_memos_count)) op_credit_memos_count,
5170            '209'||'^'||fnd_number.number_to_canonical(SUM(current_invoice_value)) current_invoice_value,
5171            '210'||'^'||fnd_number.number_to_canonical(SUM(current_invoice_count)) current_invoice_count
5172     FROM (
5173          SELECT
5174            gl_currency_api.convert_amount(currency,
5175                 l_limit_currency,sysdate,
5176                 l_exchange_rate_type,
5177                 SUM((nvl(OP_INVOICES_VALUE,0) + nvl(OP_DEBIT_MEMOS_VALUE,0) +
5178                    nvl(OP_DEPOSITS_VALUE,0) + nvl(OP_BILLS_RECEIVABLES_VALUE,0) +
5179                    nvl(OP_CHARGEBACK_VALUE,0) + nvl(OP_CREDIT_MEMOS_VALUE,0) +
5180                    nvl(UNRESOLVED_CASH_VALUE,0) ))) current_receivable_balance,  -- Current Receivables Balance (Opening balance)
5181           gl_currency_api.convert_amount(currency,
5182                 l_limit_currency,sysdate,
5183                 l_exchange_rate_type,
5184                 SUM(NVL(UNRESOLVED_CASH_VALUE,0))) unapplied_cash_amount, -- unapplied case amount
5185           SUM(UNRESOLVED_CASH_COUNT) unapplied_cash_count, -- unapplied cash count
5186           gl_currency_api.convert_amount(currency,
5187                 l_limit_currency,sysdate,
5188                 l_exchange_rate_type,
5189                 SUM(past_due_inv_value)) past_due_inv_value,
5190           SUM(past_due_inv_inst_count) past_due_inv_inst_count,
5191           gl_currency_api.convert_amount(currency,
5192                 l_limit_currency,sysdate,
5193                 l_exchange_rate_type,
5194                 SUM(inv_amt_in_dispute)) inv_amt_in_dispute,
5195           SUM(disputed_inv_count) disputed_inv_count,
5196           gl_currency_api.convert_amount(currency,
5197                 l_limit_currency,sysdate,
5198                 l_exchange_rate_type,
5199                 SUM(pending_adj_value)) pending_adj_value,
5200           gl_currency_api.convert_amount(currency,
5201                 l_limit_currency,sysdate,
5202                 l_exchange_rate_type,
5203                 SUM(receipts_at_risk_value)) total_receipts_at_risk_value,
5204           gl_currency_api.convert_amount(currency,
5205                 l_limit_currency,sysdate,
5206                 l_exchange_rate_type,
5207                 SUM(op_invoices_value)) op_invoices_value,
5208           SUM(op_invoices_count) op_invoices_count,
5209           gl_currency_api.convert_amount(currency,
5210                 l_limit_currency,sysdate,
5211                 l_exchange_rate_type,
5212                 SUM(op_debit_memos_value)) op_debit_memos_value,
5213           SUM(op_debit_memos_count) op_debit_memos_count,
5214           gl_currency_api.convert_amount(currency,
5215                 l_limit_currency,sysdate,
5216                 l_exchange_rate_type,
5217                 SUM(op_deposits_value)) op_deposits_value,
5218           SUM(op_deposits_count) op_deposits_count,
5219           gl_currency_api.convert_amount(currency,
5220                 l_limit_currency,sysdate,
5221                 l_exchange_rate_type,
5222                 SUM(op_bills_receivables_value)) op_bills_receivables_value,
5223           SUM(op_bills_receivables_count) op_bills_receivables_count,
5224           gl_currency_api.convert_amount(currency,
5225                 l_limit_currency,sysdate,
5226                 l_exchange_rate_type,
5227                 SUM(op_chargeback_value)) op_chargeback_value,
5228           SUM(op_chargeback_count) op_chargeback_count,
5229           gl_currency_api.convert_amount(currency,
5230                 l_limit_currency,sysdate,
5231                 l_exchange_rate_type,
5232                 SUM(op_credit_memos_value)) op_credit_memos_value,
5233           SUM(op_credit_memos_count) op_credit_memos_count,
5234           gl_currency_api.convert_amount(currency,
5235                 l_limit_currency,sysdate,
5236                 l_exchange_rate_type,
5237                 SUM(nvl(op_invoices_value,0) - nvl(past_due_inv_value,0))) current_invoice_value,
5238           SUM(nvl(op_invoices_count,0) - nvl(past_due_inv_inst_count,0)) current_invoice_count
5239           FROM AR_TRX_BAL_SUMMARY
5240           WHERE cust_account_id  in (select TO_NUMBER(cust_account_id)  -- Fix for Bug 12670874
5241                                 FROM   hz_cust_accounts
5242                                 WHERE  party_id in
5243                                 ( SELECT child_id
5244                                   from hz_hierarchy_nodes
5248                                   and parent_id = p_party_id
5245                                   where parent_object_type = 'ORGANIZATION'
5246                                   and parent_table_name = 'HZ_PARTIES'
5247                                   and child_object_type = 'ORGANIZATION'
5249                                   and effective_start_date <= sysdate
5250                                   and effective_end_date >= sysdate
5251                                   and  hierarchy_type = FND_PROFILE.VALUE('AR_CMGT_HIERARCHY_TYPE')
5252                                    and  g_source_name <> 'LNS'
5253                                   union select p_party_id from dual
5254                                   UNION
5255                     select hz_party_id
5256                     from LNS_LOAN_PARTICIPANTS_V
5257                     where loan_id = g_source_id
5258                     and   participant_type_code = 'COBORROWER'
5259                     and   g_source_name = 'LNS'
5260                     and (end_date_active is null OR
5261                           (sysdate between start_date_active and end_date_active)
5262                           )
5263                             ))
5264           and    CURRENCY   IN   ( SELECT CURRENCY FROM
5265                                        ar_cmgt_curr_usage_gt
5266                                        WHERE nvl(credit_request_id,p_credit_request_id) = p_credit_request_id)
5267           group by currency);
5268 
5269 
5270 CURSOR c_account_bal_summary IS
5271     SELECT '34'||'^'||fnd_number.number_to_canonical(SUM(current_receivable_balance)) current_receivable_balance,
5272            '9'||'^'|| fnd_number.number_to_canonical(SUM(unapplied_cash_amount)) unapplied_cash_amount, -- unapplied case amount
5273            '10'||'^'||fnd_number.number_to_canonical(SUM(unapplied_cash_count)) unapplied_cash_count, -- unapplied cash count
5274            '48'||'^'||fnd_number.number_to_canonical(SUM(past_due_inv_value)) past_due_inv_value,
5275            '49'||'^'||fnd_number.number_to_canonical(SUM(past_due_inv_inst_count)) past_due_inv_inst_count,
5276            '50'||'^'||fnd_number.number_to_canonical(SUM(inv_amt_in_dispute)) inv_amt_in_dispute,
5277            '51'||'^'||fnd_number.number_to_canonical(SUM(disputed_inv_count)) disputed_inv_count,
5278            '56'||'^'||fnd_number.number_to_canonical(SUM(pending_adj_value)) pending_adj_value,
5279            '58'||'^'||fnd_number.number_to_canonical(SUM(total_receipts_at_risk_value)) total_receipts_at_risk_value,
5280            '170'||'^'||fnd_number.number_to_canonical(SUM(op_invoices_value)) op_invoices_value,
5281            '171'||'^'||fnd_number.number_to_canonical(SUM(op_invoices_count)) op_invoices_count,
5282            '172'||'^'||fnd_number.number_to_canonical(SUM(op_debit_memos_value)) op_debit_memos_value,
5283            '173'||'^'||fnd_number.number_to_canonical(SUM(op_debit_memos_count)) op_debit_memos_count,
5284            '174'||'^'||fnd_number.number_to_canonical(SUM(op_deposits_value)) op_deposits_value,
5285            '175'||'^'||fnd_number.number_to_canonical(SUM(op_deposits_count)) op_deposits_count,
5286            '176'||'^'||fnd_number.number_to_canonical(SUM(op_bills_receivables_value)) op_bills_receivables_value,
5287            '177'||'^'||fnd_number.number_to_canonical(SUM(op_bills_receivables_count)) op_bills_receivables_count,
5288            '178'||'^'||fnd_number.number_to_canonical(SUM(op_chargeback_value)) op_chargeback_value,
5289            '179'||'^'||fnd_number.number_to_canonical(SUM(op_chargeback_count)) op_chargeback_count,
5290            '180'||'^'||fnd_number.number_to_canonical(SUM(op_credit_memos_value)) op_credit_memos_value,
5291            '181'||'^'||fnd_number.number_to_canonical(SUM(op_credit_memos_count)) op_credit_memos_count,
5292            '209'||'^'||fnd_number.number_to_canonical(SUM(current_invoice_value)) current_invoice_value,
5293            '210'||'^'||fnd_number.number_to_canonical(SUM(current_invoice_count)) current_invoice_count
5294     FROM (
5295          SELECT
5296            gl_currency_api.convert_amount(currency,
5297                 l_limit_currency,sysdate,
5298                 l_exchange_rate_type,
5299                 SUM((nvl(OP_INVOICES_VALUE,0) + nvl(OP_DEBIT_MEMOS_VALUE,0) +
5300                    nvl(OP_DEPOSITS_VALUE,0) + nvl(OP_BILLS_RECEIVABLES_VALUE,0) +
5301                    nvl(OP_CHARGEBACK_VALUE,0) + nvl(OP_CREDIT_MEMOS_VALUE,0) +
5302                    nvl(UNRESOLVED_CASH_VALUE,0) ))) current_receivable_balance,  -- Current Receivables Balance (Opening balance)
5303           gl_currency_api.convert_amount(currency,
5304                 l_limit_currency,sysdate,
5305                 l_exchange_rate_type,
5306                 SUM(NVL(UNRESOLVED_CASH_VALUE,0))) unapplied_cash_amount, -- unapplied case amount
5307           SUM(UNRESOLVED_CASH_COUNT) unapplied_cash_count, -- unapplied cash count
5308           gl_currency_api.convert_amount(currency,
5309                 l_limit_currency,sysdate,
5310                 l_exchange_rate_type,
5311                 SUM(past_due_inv_value)) past_due_inv_value,
5312           SUM(past_due_inv_inst_count) past_due_inv_inst_count,
5313           gl_currency_api.convert_amount(currency,
5314                 l_limit_currency,sysdate,
5315                 l_exchange_rate_type,
5316                 SUM(inv_amt_in_dispute)) inv_amt_in_dispute,
5317           SUM(disputed_inv_count) disputed_inv_count,
5318           gl_currency_api.convert_amount(currency,
5319                 l_limit_currency,sysdate,
5320                 l_exchange_rate_type,
5321                 SUM(pending_adj_value)) pending_adj_value,
5322           gl_currency_api.convert_amount(currency,
5323                 l_limit_currency,sysdate,
5324                 l_exchange_rate_type,
5325                 SUM(receipts_at_risk_value)) total_receipts_at_risk_value,
5326           gl_currency_api.convert_amount(currency,
5327                 l_limit_currency,sysdate,
5328                 l_exchange_rate_type,
5329                 SUM(op_invoices_value)) op_invoices_value,
5330           SUM(op_invoices_count) op_invoices_count,
5334                 SUM(op_debit_memos_value)) op_debit_memos_value,
5331           gl_currency_api.convert_amount(currency,
5332                 l_limit_currency,sysdate,
5333                 l_exchange_rate_type,
5335           SUM(op_debit_memos_count) op_debit_memos_count,
5336           gl_currency_api.convert_amount(currency,
5337                 l_limit_currency,sysdate,
5338                 l_exchange_rate_type,
5339                 SUM(op_deposits_value)) op_deposits_value,
5340           SUM(op_deposits_count) op_deposits_count,
5341           gl_currency_api.convert_amount(currency,
5342                 l_limit_currency,sysdate,
5343                 l_exchange_rate_type,
5344                 SUM(op_bills_receivables_value)) op_bills_receivables_value,
5345           SUM(op_bills_receivables_count) op_bills_receivables_count,
5346           gl_currency_api.convert_amount(currency,
5347                 l_limit_currency,sysdate,
5348                 l_exchange_rate_type,
5349                 SUM(op_chargeback_value)) op_chargeback_value,
5350           SUM(op_chargeback_count) op_chargeback_count,
5351           gl_currency_api.convert_amount(currency,
5352                 l_limit_currency,sysdate,
5353                 l_exchange_rate_type,
5354                 SUM(op_credit_memos_value)) op_credit_memos_value,
5355           SUM(op_credit_memos_count) op_credit_memos_count,
5356           gl_currency_api.convert_amount(currency,
5357                 l_limit_currency,sysdate,
5358                 l_exchange_rate_type,
5359                 SUM(nvl(op_invoices_value,0) - nvl(past_due_inv_value,0))) current_invoice_value,
5360           SUM(nvl(op_invoices_count,0) - nvl(past_due_inv_inst_count,0)) current_invoice_count
5361           FROM AR_TRX_BAL_SUMMARY
5362           WHERE  org_id          = decode(l_global_exposure_flag,'Y', org_id, 'N',
5363                                      decode(p_org_id,null, org_id, p_org_id), null,
5364                                      decode(p_org_id,null, org_id, p_org_id))
5365           and    cust_account_id = p_cust_account_id
5366           and    CURRENCY   IN   ( SELECT CURRENCY FROM
5367                                        ar_cmgt_curr_usage_gt
5368                                        WHERE nvl(credit_request_id,p_credit_request_id) = p_credit_request_id)
5369             /* ( SELECT * FROM
5370                                  TABLE(CAST(l_curr_array_list AS curr_array_type))) */
5371           --and    site_use_id  = decode(l_analysis_level,'S',p_cust_acct_site_id,site_use_id)
5372           group by currency);
5373 
5374 CURSOR c_site_bal_summary IS
5375     SELECT '34'||'^'||fnd_number.number_to_canonical(SUM(current_receivable_balance)) current_receivable_balance,
5376            '9'||'^'|| fnd_number.number_to_canonical(SUM(unapplied_cash_amount)) unapplied_cash_amount, -- unapplied case amount
5377            '10'||'^'||fnd_number.number_to_canonical(SUM(unapplied_cash_count)) unapplied_cash_count, -- unapplied cash count
5378            '48'||'^'||fnd_number.number_to_canonical(SUM(past_due_inv_value)) past_due_inv_value,
5379            '49'||'^'||fnd_number.number_to_canonical(SUM(past_due_inv_inst_count)) past_due_inv_inst_count,
5380            '50'||'^'||fnd_number.number_to_canonical(SUM(inv_amt_in_dispute)) inv_amt_in_dispute,
5381            '51'||'^'||fnd_number.number_to_canonical(SUM(disputed_inv_count)) disputed_inv_count,
5382            '56'||'^'||fnd_number.number_to_canonical(SUM(pending_adj_value)) pending_adj_value,
5383            '58'||'^'||fnd_number.number_to_canonical(SUM(total_receipts_at_risk_value)) total_receipts_at_risk_value,
5384            '170'||'^'||fnd_number.number_to_canonical(SUM(op_invoices_value)) op_invoices_value,
5385            '171'||'^'||fnd_number.number_to_canonical(SUM(op_invoices_count)) op_invoices_count,
5386            '172'||'^'||fnd_number.number_to_canonical(SUM(op_debit_memos_value)) op_debit_memos_value,
5387            '173'||'^'||fnd_number.number_to_canonical(SUM(op_debit_memos_count)) op_debit_memos_count,
5388            '174'||'^'||fnd_number.number_to_canonical(SUM(op_deposits_value)) op_deposits_value,
5389            '175'||'^'||fnd_number.number_to_canonical(SUM(op_deposits_count)) op_deposits_count,
5390            '176'||'^'||fnd_number.number_to_canonical(SUM(op_bills_receivables_value)) op_bills_receivables_value,
5391            '177'||'^'||fnd_number.number_to_canonical(SUM(op_bills_receivables_count)) op_bills_receivables_count,
5392            '178'||'^'||fnd_number.number_to_canonical(SUM(op_chargeback_value)) op_chargeback_value,
5393            '179'||'^'||fnd_number.number_to_canonical(SUM(op_chargeback_count)) op_chargeback_count,
5394            '180'||'^'||fnd_number.number_to_canonical(SUM(op_credit_memos_value)) op_credit_memos_value,
5395            '181'||'^'||fnd_number.number_to_canonical(SUM(op_credit_memos_count)) op_credit_memos_count,
5396            '209'||'^'||fnd_number.number_to_canonical(SUM(current_invoice_value)) current_invoice_value,
5397            '210'||'^'||fnd_number.number_to_canonical(SUM(current_invoice_count)) current_invoice_count
5398     FROM (
5399          SELECT
5400            gl_currency_api.convert_amount(currency,
5401                 l_limit_currency,sysdate,
5402                 l_exchange_rate_type,
5403                 SUM((nvl(OP_INVOICES_VALUE,0) + nvl(OP_DEBIT_MEMOS_VALUE,0) +
5404                    nvl(OP_DEPOSITS_VALUE,0) + nvl(OP_BILLS_RECEIVABLES_VALUE,0) +
5405                    nvl(OP_CHARGEBACK_VALUE,0) + nvl(OP_CREDIT_MEMOS_VALUE,0) +
5406                    nvl(UNRESOLVED_CASH_VALUE,0) ))) current_receivable_balance,  -- Current Receivables Balance (Opening balance)
5407           gl_currency_api.convert_amount(currency,
5408                 l_limit_currency,sysdate,
5409                 l_exchange_rate_type,
5410                 SUM(NVL(UNRESOLVED_CASH_VALUE,0))) unapplied_cash_amount, -- unapplied case amount
5411           SUM(UNRESOLVED_CASH_COUNT) unapplied_cash_count, -- unapplied cash count
5412           gl_currency_api.convert_amount(currency,
5413                 l_limit_currency,sysdate,
5414                 l_exchange_rate_type,
5415                 SUM(past_due_inv_value)) past_due_inv_value,
5416           SUM(past_due_inv_inst_count) past_due_inv_inst_count,
5420                 SUM(inv_amt_in_dispute)) inv_amt_in_dispute,
5417           gl_currency_api.convert_amount(currency,
5418                 l_limit_currency,sysdate,
5419                 l_exchange_rate_type,
5421           SUM(disputed_inv_count) disputed_inv_count,
5422           gl_currency_api.convert_amount(currency,
5423                 l_limit_currency,sysdate,
5424                 l_exchange_rate_type,
5425                 SUM(pending_adj_value)) pending_adj_value,
5426           gl_currency_api.convert_amount(currency,
5427                 l_limit_currency,sysdate,
5428                 l_exchange_rate_type,
5429                 SUM(receipts_at_risk_value)) total_receipts_at_risk_value,
5430           gl_currency_api.convert_amount(currency,
5431                 l_limit_currency,sysdate,
5432                 l_exchange_rate_type,
5433                 SUM(op_invoices_value)) op_invoices_value,
5434           SUM(op_invoices_count) op_invoices_count,
5435           gl_currency_api.convert_amount(currency,
5436                 l_limit_currency,sysdate,
5437                 l_exchange_rate_type,
5438                 SUM(op_debit_memos_value)) op_debit_memos_value,
5439           SUM(op_debit_memos_count) op_debit_memos_count,
5440           gl_currency_api.convert_amount(currency,
5441                 l_limit_currency,sysdate,
5442                 l_exchange_rate_type,
5443                 SUM(op_deposits_value)) op_deposits_value,
5444           SUM(op_deposits_count) op_deposits_count,
5445           gl_currency_api.convert_amount(currency,
5446                 l_limit_currency,sysdate,
5447                 l_exchange_rate_type,
5448                 SUM(op_bills_receivables_value)) op_bills_receivables_value,
5449           SUM(op_bills_receivables_count) op_bills_receivables_count,
5450           gl_currency_api.convert_amount(currency,
5451                 l_limit_currency,sysdate,
5452                 l_exchange_rate_type,
5453                 SUM(op_chargeback_value)) op_chargeback_value,
5454           SUM(op_chargeback_count) op_chargeback_count,
5455           gl_currency_api.convert_amount(currency,
5456                 l_limit_currency,sysdate,
5457                 l_exchange_rate_type,
5458                 SUM(op_credit_memos_value)) op_credit_memos_value,
5459           SUM(op_credit_memos_count) op_credit_memos_count,
5460           gl_currency_api.convert_amount(currency,
5461                 l_limit_currency,sysdate,
5462                 l_exchange_rate_type,
5463                 SUM(nvl(op_invoices_value,0) - nvl(past_due_inv_value,0))) current_invoice_value,
5464           SUM(nvl(op_invoices_count,0) - nvl(past_due_inv_inst_count,0)) current_invoice_count
5465           FROM AR_TRX_BAL_SUMMARY
5466           WHERE  cust_account_id = p_cust_account_id
5467           and    CURRENCY   IN   ( SELECT CURRENCY FROM
5468                                        ar_cmgt_curr_usage_gt
5469                                        WHERE nvl(credit_request_id,p_credit_request_id) = p_credit_request_id)
5470             /* ( SELECT * FROM
5471                                  TABLE(CAST(l_curr_array_list AS curr_array_type))) */
5472           and    site_use_id  = p_cust_acct_site_id
5473           group by currency);
5474 
5475 CURSOR c_party_numerator_dso IS
5476     SELECT  SUM(dso) dso,
5477             SUM(delinquent_dso) delinquent_dso
5478     FROM (
5479         SELECT gl_currency_api.convert_amount(currency,
5480                 l_limit_currency,sysdate,
5481                 l_exchange_rate_type,
5482                 (SUM(nvl(OP_INVOICES_VALUE,0) + nvl(OP_DEBIT_MEMOS_VALUE,0) +
5483                    nvl(OP_DEPOSITS_VALUE,0) + nvl(OP_BILLS_RECEIVABLES_VALUE,0) +
5484                    nvl(OP_CHARGEBACK_VALUE,0) + nvl(OP_CREDIT_MEMOS_VALUE,0)
5485                     )*l_certified_dso_days)) dso,
5486                 gl_currency_api.convert_amount(currency,
5487                 l_limit_currency,sysdate,
5488                 l_exchange_rate_type,
5489                    (SUM(nvl(OP_INVOICES_VALUE,0) + nvl(OP_DEBIT_MEMOS_VALUE,0) +
5490                    nvl(OP_DEPOSITS_VALUE,0) + nvl(OP_BILLS_RECEIVABLES_VALUE,0) +
5491                    nvl(OP_CHARGEBACK_VALUE,0) + nvl(OP_CREDIT_MEMOS_VALUE,0)
5492                     - nvl(BEST_CURRENT_RECEIVABLES,0))*l_certified_dso_days)) delinquent_dso
5493          FROM   ar_trx_bal_summary
5494          WHERE  cust_account_id  in (select TO_NUMBER(cust_account_id)  -- Fix for Bug 12670874
5495                                 FROM   hz_cust_accounts
5496                                 WHERE  party_id in
5497                                 ( SELECT child_id
5498                                   from hz_hierarchy_nodes
5499                                   where parent_object_type = 'ORGANIZATION'
5500                                   and parent_table_name = 'HZ_PARTIES'
5501                                   and child_object_type = 'ORGANIZATION'
5502                                   and parent_id = p_party_id
5503                                   and effective_start_date <= sysdate
5504                                   and effective_end_date >= sysdate
5505                                   and  hierarchy_type = FND_PROFILE.VALUE('AR_CMGT_HIERARCHY_TYPE')
5506                                   and  g_source_name <> 'LNS'
5510                     from LNS_LOAN_PARTICIPANTS_V
5507                                   union select p_party_id from dual
5508                                   UNION
5509                     select hz_party_id
5511                     where loan_id = g_source_id
5512                     and   participant_type_code = 'COBORROWER'
5513                     and   g_source_name = 'LNS'
5514                     and (end_date_active is null OR
5515                           (sysdate between start_date_active and end_date_active)
5516                           )
5517                             ))
5518           and    CURRENCY   IN   ( SELECT CURRENCY FROM
5519                                        ar_cmgt_curr_usage_gt
5520                                        WHERE nvl(credit_request_id,p_credit_request_id) = p_credit_request_id)
5521           group by currency);
5522 
5523 CURSOR c_account_numerator_dso IS
5524     SELECT SUM(dso) dso,
5525            SUM(delinquent_dso) delinquent_dso
5526     FROM (
5527         SELECT gl_currency_api.convert_amount (currency,
5528                 l_limit_currency,sysdate,
5529                 l_exchange_rate_type,
5530                 (SUM(nvl(OP_INVOICES_VALUE,0) + nvl(OP_DEBIT_MEMOS_VALUE,0) +
5531                    nvl(OP_DEPOSITS_VALUE,0) + nvl(OP_BILLS_RECEIVABLES_VALUE,0) +
5532                    nvl(OP_CHARGEBACK_VALUE,0) + nvl(OP_CREDIT_MEMOS_VALUE,0)
5533                     )*l_certified_dso_days)) dso,
5534                 gl_currency_api.convert_amount(currency,
5535                 l_limit_currency,sysdate,
5536                 l_exchange_rate_type,
5537                    (SUM(nvl(OP_INVOICES_VALUE,0) + nvl(OP_DEBIT_MEMOS_VALUE,0) +
5538                    nvl(OP_DEPOSITS_VALUE,0) + nvl(OP_BILLS_RECEIVABLES_VALUE,0) +
5539                    nvl(OP_CHARGEBACK_VALUE,0) + nvl(OP_CREDIT_MEMOS_VALUE,0)
5540                     - nvl(BEST_CURRENT_RECEIVABLES,0))*l_certified_dso_days)) delinquent_dso
5541          FROM   ar_trx_bal_summary
5542          WHERE  cust_account_id  = p_cust_account_id
5543          and    org_id          = decode(l_global_exposure_flag,'Y', org_id, 'N',
5544                                      decode(p_org_id,null, org_id, p_org_id), null,
5545                                      decode(p_org_id,null, org_id, p_org_id))
5546          and   CURRENCY   IN   ( SELECT CURRENCY FROM
5547                                        ar_cmgt_curr_usage_gt
5548                                        WHERE nvl(credit_request_id,p_credit_request_id) = p_credit_request_id)
5549          group by currency);
5550 
5551 CURSOR c_site_numerator_dso IS
5552     SELECT SUM(dso) dso,
5553            SUM(delinquent_dso) delinquent_dso
5554     FROM (
5555         SELECT gl_currency_api.convert_amount(currency,
5556                 l_limit_currency,sysdate,
5557                 l_exchange_rate_type,
5558                 (SUM(nvl(OP_INVOICES_VALUE,0) + nvl(OP_DEBIT_MEMOS_VALUE,0) +
5559                    nvl(OP_DEPOSITS_VALUE,0) + nvl(OP_BILLS_RECEIVABLES_VALUE,0) +
5560                    nvl(OP_CHARGEBACK_VALUE,0) + nvl(OP_CREDIT_MEMOS_VALUE,0) +
5561                    nvl(UNRESOLVED_CASH_VALUE,0) )*l_certified_dso_days)) dso,
5562                 gl_currency_api.convert_amount(currency,
5563                 l_limit_currency,sysdate,
5564                 l_exchange_rate_type,
5565                    (SUM(nvl(OP_INVOICES_VALUE,0) + nvl(OP_DEBIT_MEMOS_VALUE,0) +
5566                    nvl(OP_DEPOSITS_VALUE,0) + nvl(OP_BILLS_RECEIVABLES_VALUE,0) +
5567                    nvl(OP_CHARGEBACK_VALUE,0) + nvl(OP_CREDIT_MEMOS_VALUE,0)
5568                     - nvl(BEST_CURRENT_RECEIVABLES,0))*l_certified_dso_days)) delinquent_dso
5569          FROM   ar_trx_bal_summary
5570          WHERE  cust_account_id  = p_cust_account_id
5571          and    site_use_id  = p_cust_acct_site_id
5572          and    CURRENCY   IN   ( SELECT CURRENCY FROM
5573                                        ar_cmgt_curr_usage_gt
5574                                        WHERE nvl(credit_request_id,p_credit_request_id) = p_credit_request_id)
5575          group by currency);
5576 
5577 CURSOR c_party_deno_dso IS
5578     SELECT SUM(dso) dso,
5579            SUM(delinquent_dso) delinquent_dso
5580     FROM (
5581         SELECT gl_currency_api.convert_amount(currency,
5582                 l_limit_currency,sysdate,
5583                 l_exchange_rate_type,
5584                 (SUM(nvl(TOTAL_INVOICES_VALUE,0) + nvl(TOTAL_DEBIT_MEMOS_VALUE,0) +
5585                    nvl(TOTAL_DEPOSITS_VALUE,0) + nvl(TOTAL_BILLS_RECEIVABLES_VALUE,0) +
5586                    nvl(TOTAL_CHARGEBACK_VALUE,0) + nvl(TOTAL_CREDIT_MEMOS_VALUE,0) +
5587                    nvl(TOTAL_ADJUSTMENTS_VALUE,0))
5588                    )) dso,
5589                 gl_currency_api.convert_amount(currency,
5590                 l_limit_currency,sysdate,
5591                 l_exchange_rate_type,
5592                    ((SUM(nvl(TOTAL_INVOICES_VALUE,0) + nvl(TOTAL_DEBIT_MEMOS_VALUE,0) +
5593                    nvl(TOTAL_DEPOSITS_VALUE,0) + nvl(TOTAL_BILLS_RECEIVABLES_VALUE,0) +
5594                    nvl(TOTAL_CHARGEBACK_VALUE,0) + nvl(TOTAL_CREDIT_MEMOS_VALUE,0) +
5595                    nvl(TOTAL_ADJUSTMENTS_VALUE,0)
5596                    )))) delinquent_dso
5597          FROM   ar_trx_summary
5598          WHERE  cust_account_id  in (select TO_NUMBER(cust_account_id)  -- Fix for Bug 12670874
5599                                 FROM   hz_cust_accounts
5600                                 WHERE  party_id in
5601                                 ( SELECT child_id
5602                                   from hz_hierarchy_nodes
5603                                   where parent_object_type = 'ORGANIZATION'
5604                                   and parent_table_name = 'HZ_PARTIES'
5605                                   and child_object_type = 'ORGANIZATION'
5606                                   and parent_id = p_party_id
5607                                   and effective_start_date <= sysdate
5608                                   and effective_end_date >= sysdate
5612                                   UNION
5609                                   and  hierarchy_type = FND_PROFILE.VALUE('AR_CMGT_HIERARCHY_TYPE')
5610                                   and  g_source_name <> 'LNS'
5611                                   union select p_party_id from dual
5613                     select hz_party_id
5614                     from LNS_LOAN_PARTICIPANTS_V
5615                     where loan_id = g_source_id
5616                     and   participant_type_code = 'COBORROWER'
5617                     and   g_source_name = 'LNS'
5618                     and (end_date_active is null OR
5619                           (sysdate between start_date_active and end_date_active)
5620                           )
5621                             ))
5622           and    CURRENCY   IN   ( SELECT CURRENCY FROM
5623                                        ar_cmgt_curr_usage_gt
5624                                        WHERE nvl(credit_request_id,p_credit_request_id) = p_credit_request_id)
5625           and    as_of_date  >= (sysdate -l_certified_dso_days)
5626           group by currency);
5627 
5628 CURSOR c_account_deno_dso IS
5629     SELECT SUM(dso) dso,
5630            SUM(delinquent_dso) delinquent_dso
5631     FROM (
5632         SELECT gl_currency_api.convert_amount(currency,
5633                 l_limit_currency,sysdate,
5634                 l_exchange_rate_type,
5635                 (SUM(nvl(TOTAL_INVOICES_VALUE,0) + nvl(TOTAL_DEBIT_MEMOS_VALUE,0) +
5636                    nvl(TOTAL_DEPOSITS_VALUE,0) + nvl(TOTAL_BILLS_RECEIVABLES_VALUE,0) +
5637                    nvl(TOTAL_CHARGEBACK_VALUE,0) + nvl(TOTAL_CREDIT_MEMOS_VALUE,0) +
5638                    nvl(TOTAL_ADJUSTMENTS_VALUE,0))
5639                    )) dso,
5640                 gl_currency_api.convert_amount(currency,
5641                 l_limit_currency,sysdate,
5642                 l_exchange_rate_type,
5643                    (SUM(nvl(TOTAL_INVOICES_VALUE,0) + nvl(TOTAL_DEBIT_MEMOS_VALUE,0) +
5644                    nvl(TOTAL_DEPOSITS_VALUE,0) + nvl(TOTAL_BILLS_RECEIVABLES_VALUE,0) +
5645                    nvl(TOTAL_CHARGEBACK_VALUE,0) + nvl(TOTAL_CREDIT_MEMOS_VALUE,0) +
5646                    nvl(TOTAL_ADJUSTMENTS_VALUE,0)
5647                    ))) delinquent_dso
5648          FROM   ar_trx_summary
5649          WHERE  cust_account_id = p_cust_account_id
5650           and    CURRENCY   IN   ( SELECT CURRENCY FROM
5651                                        ar_cmgt_curr_usage_gt
5652                                        WHERE nvl(credit_request_id,p_credit_request_id) = p_credit_request_id)
5653           and    org_id          = decode(l_global_exposure_flag,'Y', org_id, 'N',
5654                                      decode(p_org_id,null, org_id, p_org_id), null,
5655                                      decode(p_org_id,null, org_id, p_org_id))
5656           and    as_of_date  >= (sysdate -l_certified_dso_days)
5657           group by currency);
5658 
5659 CURSOR c_site_deno_dso IS
5660     SELECT SUM(dso) dso,
5661            SUM(delinquent_dso) delinquent_dso
5662     FROM (
5663         SELECT gl_currency_api.convert_amount(currency,
5664                 l_limit_currency,sysdate,
5665                 l_exchange_rate_type,
5666                 (SUM(nvl(TOTAL_INVOICES_VALUE,0) + nvl(TOTAL_DEBIT_MEMOS_VALUE,0) +
5667                    nvl(TOTAL_DEPOSITS_VALUE,0) + nvl(TOTAL_BILLS_RECEIVABLES_VALUE,0) +
5668                    nvl(TOTAL_CHARGEBACK_VALUE,0) + nvl(TOTAL_CREDIT_MEMOS_VALUE,0) +
5669                    nvl(TOTAL_ADJUSTMENTS_VALUE,0))
5670                    )) dso,
5671                 gl_currency_api.convert_amount(currency,
5672                 l_limit_currency,sysdate,
5673                 l_exchange_rate_type,
5674                    (SUM(nvl(TOTAL_INVOICES_VALUE,0) + nvl(TOTAL_DEBIT_MEMOS_VALUE,0) +
5675                    nvl(TOTAL_DEPOSITS_VALUE,0) + nvl(TOTAL_BILLS_RECEIVABLES_VALUE,0) +
5676                    nvl(TOTAL_CHARGEBACK_VALUE,0) + nvl(TOTAL_CREDIT_MEMOS_VALUE,0) +
5677                    nvl(TOTAL_ADJUSTMENTS_VALUE,0)
5678                    ))) delinquent_dso
5679          FROM   ar_trx_summary
5680          WHERE  cust_account_id = p_cust_account_id
5681           and    CURRENCY   IN   ( SELECT CURRENCY FROM
5682                                        ar_cmgt_curr_usage_gt
5683                                        WHERE nvl(credit_request_id,p_credit_request_id) = p_credit_request_id)
5684           and    site_use_id = p_cust_acct_site_id
5685           and    as_of_date  >= (sysdate -l_certified_dso_days)
5686           group by currency);
5687 
5688    l_tag VARCHAR2(50);
5689    l_orig_limit_currency  ar_cmgt_credit_requests.limit_currency%type;
5690    l_wadpl NUMBER;
5691    l_apd   NUMBER;
5692 
5693    -- Added by rravikir (Bug 8581475)
5694    CURSOR c_party_currencies IS
5695    SELECT distinct currency
5696    FROM ar_trx_bal_summary
5697    WHERE cust_account_id IN (SELECT cust_account_id
5698                              FROM hz_cust_accounts_all
5699                              WHERE party_id = p_party_id
5700                              AND status = 'A');
5701 
5702    CURSOR c_checklist_currency_def IS
5703    SELECT include_all_currencies
5704    FROM ar_cmgt_check_lists
5705    WHERE check_list_id = p_check_list_id;
5706 
5707    l_checklist_currency_def VARCHAR2(1);
5708    -- End (Bug 8581475)
5709 
5710 
5711 BEGIN
5712     IF pg_wf_debug = 'Y'
5713     THEN
5714        IF p_case_folder_id IS NULL
5715        THEN
5716           l_tag := 'CR:' || p_credit_request_id;
5717        ELSE
5718           l_tag := 'CF:' || p_case_folder_id;
5719        END IF;
5720        ar_cmgt_util.wf_debug(l_tag,
5721               'ar_cmgt_data_points_pkg.gather_data_points()+');
5722        ar_cmgt_util.wf_debug(l_tag,
5723               '  p_mode = ' || p_mode);
5727               '  p_cust_account_id = ' || p_cust_account_id);
5724        ar_cmgt_util.wf_debug(l_tag,
5725               '  p_party_id = ' || p_party_id);
5726        ar_cmgt_util.wf_debug(l_tag,
5728        ar_cmgt_util.wf_debug(l_tag,
5729               '  p_cust_acct_site_id = ' || p_cust_acct_site_id);
5730        ar_cmgt_util.wf_debug(l_tag,
5731               '  p_trx_currency = ' || p_trx_currency);
5732        ar_cmgt_util.wf_debug(l_tag,
5733               '  p_org_id = ' || p_org_id);
5734        ar_cmgt_util.wf_debug(l_tag,
5735               '  p_check_list_id = ' || p_check_list_id);
5736        ar_cmgt_util.wf_debug(l_tag,
5737               '  p_credit_request_id = ' || p_credit_request_id);
5738        ar_cmgt_util.wf_debug(l_tag,
5739               '  p_score_model_id = ' || p_score_model_id);
5740        ar_cmgt_util.wf_debug(l_tag,
5741               '  p_credit_classification = ' || p_credit_classification);
5742        ar_cmgt_util.wf_debug(l_tag,
5743               '  p_review_type = ' || p_review_type);
5744        ar_cmgt_util.wf_debug(l_tag,
5745               '  p_case_folder_number = ' || p_case_folder_number);
5746     END IF;
5747 
5748     -- dbms_session.set_sql_trace(true);
5749     p_resultout := 0;
5750 
5751 
5752     -- p_case_folder_is is required in case of mode = 'REFRESH'
5753 
5754     IF p_mode = 'REFRESH' and p_case_folder_id IS NULL
5755     THEN
5756         p_resultout := 1;
5757         p_error_msg := 'Case Folder Id is required for Refresh Operation';
5758         return;
5759     END IF;
5760 
5761     -- Get values from system parameters
5762     BEGIN
5763         SELECT period, cer_dso_days
5764         INTO   l_period, l_certified_dso_days
5765         FROM   ar_cmgt_setup_options;
5766 
5767         EXCEPTION
5768             WHEN OTHERS
5769             THEN
5770                 p_resultout := 2;
5771                 p_error_msg := 'Please go to the System Options Page and setup system parameters '||sqlerrm;
5772                 ar_cmgt_util.wf_debug(p_case_folder_id, p_error_msg);
5773                 return;
5774 
5775     END;
5776     BEGIN
5777     -- Get source_information
5778       SELECT nvl(source_name, 'OCM'), nvl(source_column1, -99), limit_currency
5779       INTO   g_source_name, g_source_id, l_orig_limit_currency
5780       FROM   ar_cmgt_credit_requests
5781       WHERE  credit_request_id = p_credit_request_id;
5782 
5783       EXCEPTION
5784         WHEN OTHERS THEN
5785         p_resultout := 2;
5786                 p_error_msg := 'Unable to get the credit request record '||sqlerrm;
5787                 ar_cmgt_util.wf_debug(p_case_folder_id, p_error_msg);
5788                 return;
5789     END;
5790 
5791     -- check which level we need to do analysis
5792     l_analysis_level := AR_CMGT_UTIL.find_analysis_level(p_party_id, p_cust_account_id, p_cust_acct_site_id);
5793 
5794     AR_CMGT_UTIL.get_limit_currency(
5795                 p_party_id              =>  p_party_id,
5796                 p_cust_account_id       =>  p_cust_account_id,
5797                 p_cust_acct_site_id     =>  p_cust_acct_site_id,
5798                 p_trx_currency_code     =>  p_trx_currency,
5799                 p_limit_curr_code       =>  l_limit_currency,
5800                 p_trx_limit             =>  l_trx_limit,
5801                 p_overall_limit         =>  l_overall_limit,
5802                 p_cust_acct_profile_amt_id => l_cust_acct_profile_amt_id,
5803                 p_global_exposure_flag  =>  l_global_exposure_flag,
5804                 p_include_all_flag      =>  l_include_all_flag,
5805                 p_usage_curr_tbl        =>  l_curr_tbl,
5806                 p_excl_curr_list        =>  l_excl_curr_list);
5807 
5808     -- get the conversion type
5809     IF g_conversion_type IS NULL
5810     THEN
5811        g_conversion_type := get_conversion_type( p_credit_request_id);
5812     END IF;
5813     l_exchange_rate_type := g_conversion_type;
5814 
5815     IF l_exchange_rate_type IS NULL
5816     THEN
5817         p_resultout := 2;
5818         p_error_msg := 'Please define an exchange rate before processing this application.';
5819         return;
5820     END IF;
5821 
5822 
5823     IF pg_wf_debug = 'Y'
5824     THEN
5825        ar_cmgt_util.wf_debug(l_tag,
5826               '  l_include_all_flag = ' || l_include_all_flag);
5827        ar_cmgt_util.wf_debug(l_tag,
5828               '  l_global_exposure_flag = ' || l_global_exposure_flag);
5829        ar_cmgt_util.wf_debug(l_tag,
5830               '  l_analysis_level = ' || l_analysis_level);
5831        ar_cmgt_util.wf_debug(l_tag,
5832               '  l_orig_limit_currency = ' || l_orig_limit_currency);
5833        ar_cmgt_util.wf_debug(l_tag,
5834               '  l_limit_currency = ' || l_limit_currency);
5835        ar_cmgt_util.wf_debug(l_tag,
5836               '  l_exchange_rate_type = ' || l_exchange_rate_type);
5837        ar_cmgt_util.wf_debug(l_tag,
5838               '     included currencies:');
5839     END IF;
5840 
5841     -- Added by rravikir (Bug 8581475)
5842     OPEN c_checklist_currency_def;
5843     FETCH c_checklist_currency_def INTO l_checklist_currency_def;
5844     CLOSE c_checklist_currency_def;
5845 
5846     -- If the 'Include All Currencies' is set to 'Y' in Checklist, the
5847     -- value at usage rule level is overridden, though its value is 'N'
5848     IF ( nvl(l_checklist_currency_def,'N') = 'Y' ) THEN
5849       l_include_all_flag := 'Y';
5850     END IF;
5851     -- End (Bug 8581475)
5852 
5853     IF ( nvl(l_include_all_flag,'N') = 'N' )
5854     THEN
5855         for  i in 1..l_curr_tbl.COUNT
5856         LOOP
5857            IF pg_wf_debug = 'Y'
5861                         l_curr_tbl(i).usage_curr_code,
5858            THEN
5859                IF gl_currency_api.rate_exists(
5860                         l_limit_currency,
5862                         sysdate,
5863                         g_conversion_type) = 'Y'
5864                THEN
5865                   ar_cmgt_util.wf_debug(l_tag,
5866                       l_curr_tbl(i).usage_curr_code);
5867                ELSE
5868                   ar_cmgt_util.wf_debug(l_tag,
5869                       l_curr_tbl(i).usage_curr_code || ' MISSING RATE');
5870                END IF;
5871            END IF;
5872 
5873             INSERT INTO ar_cmgt_curr_usage_gt ( credit_request_id, currency) values
5874                 ( p_credit_request_id, l_curr_tbl(i).usage_curr_code);
5875         END LOOP;
5876     ELSE
5877       -- Populate the GT table with the currencies of the Party active
5878       -- accounts
5879       -- Added by rravikir (Bug 8581475)
5880       FOR c_party_currencies_rec IN c_party_currencies
5881       LOOP
5882         INSERT INTO ar_cmgt_curr_usage_gt(credit_request_id, currency) VALUES
5883               (p_credit_request_id, c_party_currencies_rec.currency);
5884       END LOOP;
5885       -- End (Bug 8581475)
5886 
5887       IF pg_wf_debug = 'Y'
5888       THEN
5889           ar_cmgt_util.wf_debug(l_tag,
5890               '    ALL currencies from ar_trx_bal_summary');
5891       END IF;
5892     END IF;
5893 
5894     /* 7032417 - set outbound limit currency */
5895     IF g_source_name = 'OKL'
5896     THEN
5897        g_intended_limit_currency := l_limit_currency;
5898        l_limit_currency := nvl(l_orig_limit_currency, l_limit_currency);
5899     END IF;
5900 
5901 
5902     p_limit_currency := l_limit_currency;
5903     IF  l_limit_currency IS NULL
5904     THEN
5905        IF pg_wf_debug = 'Y'
5906        THEN
5907           ar_cmgt_util.wf_debug(l_tag,
5908               'Credit Usage Rule has not been setup');
5909        END IF;
5910 
5911         p_resultout := 2;
5912         p_error_msg := 'Credit Usage Rule has not been setup';
5913         return;
5914     END IF;
5915 
5916     IF l_analysis_level in ( 'P','A')
5917     THEN
5918         IF nvl(l_global_exposure_flag,'N') = 'N'
5919         THEN
5920             p_resultout := 2;
5921             p_error_msg := 'Global Exposure Flag must be set to Y for Party and Account Level Analysis';
5922             return;
5923         END IF;
5924     END IF;
5925 
5926     -- update credit request with the limit currency
5927     update ar_cmgt_credit_requests
5928         set  limit_currency = l_limit_currency,
5929              LAST_UPDATE_DATE  = sysdate,
5930              LAST_UPDATED_BY   = FND_GLOBAL.user_id,
5931              LAST_UPDATE_LOGIN = FND_GLOBAL.login_id
5932     WHERE  credit_request_id = p_credit_request_id;
5933     --AND    limit_currency IS NULL;
5934 
5935 
5936     IF p_mode = 'CREATE'
5937     THEN
5938             build_case_folder(p_party_id,
5939                       p_cust_account_id,
5940                       p_cust_acct_site_id,
5941                       p_limit_currency,
5942                       l_exchange_rate_type,
5943                       p_check_list_id,
5944                       p_credit_request_id,
5945                       p_score_model_id,
5946                       p_credit_classification,
5947                       p_review_type,
5948                       p_case_folder_number,
5949                       p_case_folder_id,
5950                       l_error_msg,
5951                       l_resultout);
5952 
5953             IF l_resultout <> 0
5954             THEN
5955                     p_resultout := l_resultout;
5956                     p_error_msg := l_error_msg;
5957                     return;
5958             END IF;
5959     ELSIF p_mode = 'REFRESH'
5960     THEN
5961             -- Referesh case
5962             UPDATE ar_cmgt_case_folders
5963                set  last_updated = SYSDATE,
5964                     last_update_date = SYSDATE,
5965                     last_updated_by  = fnd_global.user_id,
5966                     last_update_login = fnd_global.login_id
5967            WHERE   case_folder_id = p_case_folder_id;
5968     END IF;
5969 
5970 
5971 
5972 
5973     BEGIN
5974       IF l_analysis_level = 'P'
5975       THEN
5976         FOR c_summary_rec IN c_party_summary
5977         LOOP
5978             summary_rec_str := summary_rec_str||'{'||
5979                             c_summary_rec.days_credit_granted||'{'||
5980                             c_summary_rec.nsf_payment_count||'{'||
5981                             c_summary_rec.nsf_payment_amount||'{'||
5982                             c_summary_rec.credit_memo_value||'{'||
5983                             c_summary_rec.credit_memo_count||'{'||
5984                             c_summary_rec.per_inv_paid_promptly||'{'||
5985                             c_summary_rec.per_inv_paid_late||'{'||
5986                             c_summary_rec.per_inv_with_discount||'{'||
5987                             c_summary_rec.inv_paid_amount||'{'||
5988                             c_summary_rec.inv_paid_count||'{'||
5989                             c_summary_rec.earned_disc_value||'{'||
5990                             c_summary_rec.earned_disc_count||'{'||
5991                             c_summary_rec.unearned_disc_value||'{'||
5992                             c_summary_rec.unearned_disc_count||'{'||
5993                             c_summary_rec.total_cash_receipts_value||'{'||
5994                             c_summary_rec.total_cash_receipts_count||'{'||
5995                             c_summary_rec.total_invoices_value||'{'||
5996                             c_summary_rec.total_invoices_count||'{'||
6000                             c_summary_rec.total_debit_memos_count||'{'||
5997                             c_summary_rec.total_bills_receivables_value||'{'||
5998                             c_summary_rec.total_bills_receivables_count||'{'||
5999                             c_summary_rec.total_debit_memos_value||'{'||
6001                             c_summary_rec.total_chargeback_value||'{'||
6002                             c_summary_rec.total_chargeback_count||'{'||
6003                             c_summary_rec.total_adjustments_value||'{'||
6004                             c_summary_rec.total_adjustments_count||'{'||
6005                             c_summary_rec.total_deposits_value||'{'||
6006                             c_summary_rec.total_deposits_count;
6007 
6008         END LOOP;
6009       ELSIF l_analysis_level = 'A'
6010       THEN
6011         FOR c_summary_rec IN c_account_summary
6012         LOOP
6013             summary_rec_str := summary_rec_str||'{'||
6014                             c_summary_rec.days_credit_granted||'{'||
6015                             c_summary_rec.nsf_payment_count||'{'||
6016                             c_summary_rec.nsf_payment_amount||'{'||
6017                             c_summary_rec.credit_memo_value||'{'||
6018                             c_summary_rec.credit_memo_count||'{'||
6019                             c_summary_rec.per_inv_paid_promptly||'{'||
6020                             c_summary_rec.per_inv_paid_late||'{'||
6021                             c_summary_rec.per_inv_with_discount||'{'||
6022                             c_summary_rec.inv_paid_amount||'{'||
6023                             c_summary_rec.inv_paid_count||'{'||
6024                             c_summary_rec.earned_disc_value||'{'||
6025                             c_summary_rec.earned_disc_count||'{'||
6026                             c_summary_rec.unearned_disc_value||'{'||
6027                             c_summary_rec.unearned_disc_count||'{'||
6028                             c_summary_rec.total_cash_receipts_value||'{'||
6029                             c_summary_rec.total_cash_receipts_count||'{'||
6030                             c_summary_rec.total_invoices_value||'{'||
6031                             c_summary_rec.total_invoices_count||'{'||
6032                             c_summary_rec.total_bills_receivables_value||'{'||
6033                             c_summary_rec.total_bills_receivables_count||'{'||
6034                             c_summary_rec.total_debit_memos_value||'{'||
6035                             c_summary_rec.total_debit_memos_count||'{'||
6036                             c_summary_rec.total_chargeback_value||'{'||
6037                             c_summary_rec.total_chargeback_count||'{'||
6038                             c_summary_rec.total_adjustments_value||'{'||
6039                             c_summary_rec.total_adjustments_count||'{'||
6040                             c_summary_rec.total_deposits_value||'{'||
6041                             c_summary_rec.total_deposits_count;
6042 
6043         END LOOP;
6044       ELSIF l_analysis_level = 'S'
6045       THEN
6046         FOR c_summary_rec IN c_site_summary
6047         LOOP
6048             summary_rec_str := summary_rec_str||'{'||
6049                             c_summary_rec.days_credit_granted||'{'||
6050                             c_summary_rec.nsf_payment_count||'{'||
6051                             c_summary_rec.nsf_payment_amount||'{'||
6052                             c_summary_rec.credit_memo_value||'{'||
6053                             c_summary_rec.credit_memo_count||'{'||
6054                             c_summary_rec.per_inv_paid_promptly||'{'||
6055                             c_summary_rec.per_inv_paid_late||'{'||
6056                             c_summary_rec.per_inv_with_discount||'{'||
6057                             c_summary_rec.inv_paid_amount||'{'||
6058                             c_summary_rec.inv_paid_count||'{'||
6059                             c_summary_rec.earned_disc_value||'{'||
6060                             c_summary_rec.earned_disc_count||'{'||
6061                             c_summary_rec.unearned_disc_value||'{'||
6062                             c_summary_rec.unearned_disc_count||'{'||
6063                             c_summary_rec.total_cash_receipts_value||'{'||
6064                             c_summary_rec.total_cash_receipts_count||'{'||
6065                             c_summary_rec.total_invoices_value||'{'||
6066                             c_summary_rec.total_invoices_count||'{'||
6067                             c_summary_rec.total_bills_receivables_value||'{'||
6068                             c_summary_rec.total_bills_receivables_count||'{'||
6069                             c_summary_rec.total_debit_memos_value||'{'||
6070                             c_summary_rec.total_debit_memos_count||'{'||
6071                             c_summary_rec.total_chargeback_value||'{'||
6072                             c_summary_rec.total_chargeback_count||'{'||
6073                             c_summary_rec.total_adjustments_value||'{'||
6074                             c_summary_rec.total_adjustments_count||'{'||
6075                             c_summary_rec.total_deposits_value||'{'||
6076                             c_summary_rec.total_deposits_count;
6077 
6078         END LOOP;
6079       END IF;
6080     EXCEPTION
6081         WHEN OTHERS THEN
6082             p_error_msg := 'Error While Getting Data from AR_TRX_SUMMARY, Probably exchange rate'||
6083                                  ' is not set correctly '||'Sql Error:'||sqlerrm;
6084             p_resultout := 1;
6085             ar_cmgt_util.wf_debug(p_case_folder_id, p_error_msg || '1');
6086             return;
6087     END;
6088     summary_rec_str := summary_rec_str||'{'; -- adding for instrb call to make life easier
6089     -- Assign the number of columns fetched in the cursor
6090 
6091     for i IN 1..summary_rec_col_count
6092     LOOP
6093         l_start_pos := instrb(summary_rec_str,'{',1,i);
6094         l_end_pos   := instrb(summary_rec_str,'{',1,i+1);
6095         l_combo_string := substrb(summary_rec_str,l_start_pos+1,
6099         l_data_point_id := substrb(l_combo_string,1,l_data_point_id_end_pos-1);
6096                                 l_end_pos-(l_start_pos+1));
6097         l_data_point_id_end_pos := instrb(l_combo_string,'^',1,1);
6098 
6100         l_data_point_value := substrb(l_combo_string,l_data_point_id_end_pos+1,20);
6101 
6102 
6103         build_case_folder_details(
6104                     p_case_folder_id,
6105                     l_data_point_id,
6106                     l_data_point_value,
6107                     p_mode,
6108                     p_error_msg,
6109                     p_resultout);
6110         IF p_resultout <> 0
6111         THEN
6112             return;
6113         END IF;
6114      END LOOP;
6115      summary_rec_str := NULL;
6116     BEGIN
6117       IF l_analysis_level = 'P'
6118       THEN
6119         FOR c_bal_summary_rec IN c_party_bal_summary
6120         LOOP
6121             summary_rec_str := summary_rec_str||'{'||
6122                             c_bal_summary_rec.current_receivable_balance||'{'||
6123                             c_bal_summary_rec.unapplied_cash_amount||'{'||
6124                             c_bal_summary_rec.unapplied_cash_count||'{'||
6125                             c_bal_summary_rec.past_due_inv_value||'{'||
6126                             c_bal_summary_rec.past_due_inv_inst_count||'{'||
6127                             c_bal_summary_rec.inv_amt_in_dispute||'{'||
6128                             c_bal_summary_rec.disputed_inv_count||'{'||
6129                             c_bal_summary_rec.pending_adj_value||'{'||
6130                             c_bal_summary_rec.total_receipts_at_risk_value||'{'||
6131                             c_bal_summary_rec.op_invoices_value||'{'||
6132                             c_bal_summary_rec.op_invoices_count||'{'||
6133                             c_bal_summary_rec.op_debit_memos_value||'{'||
6134                             c_bal_summary_rec.op_debit_memos_count||'{'||
6135                             c_bal_summary_rec.op_deposits_value||'{'||
6136                             c_bal_summary_rec.op_deposits_count||'{'||
6137                             c_bal_summary_rec.op_bills_receivables_value||'{'||
6138                             c_bal_summary_rec.op_bills_receivables_count||'{'||
6139                             c_bal_summary_rec.op_chargeback_value||'{'||
6140                             c_bal_summary_rec.op_chargeback_count||'{'||
6141                             c_bal_summary_rec.op_credit_memos_value||'{'||
6142                             c_bal_summary_rec.op_credit_memos_count||'{'||
6143                             c_bal_summary_rec.current_invoice_value||'{'||
6144                             c_bal_summary_rec.current_invoice_count;
6145         END LOOP;
6146       ELSIF l_analysis_level = 'A'
6147       THEN
6148         FOR c_bal_summary_rec IN c_account_bal_summary
6149         LOOP
6150             summary_rec_str := summary_rec_str||'{'||
6151                             c_bal_summary_rec.current_receivable_balance||'{'||
6152                             c_bal_summary_rec.unapplied_cash_amount||'{'||
6153                             c_bal_summary_rec.unapplied_cash_count||'{'||
6154                             c_bal_summary_rec.past_due_inv_value||'{'||
6155                             c_bal_summary_rec.past_due_inv_inst_count||'{'||
6156                             c_bal_summary_rec.inv_amt_in_dispute||'{'||
6157                             c_bal_summary_rec.disputed_inv_count||'{'||
6158                             c_bal_summary_rec.pending_adj_value||'{'||
6159                             c_bal_summary_rec.total_receipts_at_risk_value||'{'||
6160                             c_bal_summary_rec.op_invoices_value||'{'||
6161                             c_bal_summary_rec.op_invoices_count||'{'||
6162                             c_bal_summary_rec.op_debit_memos_value||'{'||
6163                             c_bal_summary_rec.op_debit_memos_count||'{'||
6164                             c_bal_summary_rec.op_deposits_value||'{'||
6165                             c_bal_summary_rec.op_deposits_count||'{'||
6166                             c_bal_summary_rec.op_bills_receivables_value||'{'||
6167                             c_bal_summary_rec.op_bills_receivables_count||'{'||
6168                             c_bal_summary_rec.op_chargeback_value||'{'||
6169                             c_bal_summary_rec.op_chargeback_count||'{'||
6170                             c_bal_summary_rec.op_credit_memos_value||'{'||
6171                             c_bal_summary_rec.op_credit_memos_count||'{'||
6172                             c_bal_summary_rec.current_invoice_value||'{'||
6173                             c_bal_summary_rec.current_invoice_count;
6174         END LOOP;
6175       ELSIF l_analysis_level = 'S'
6176       THEN
6177         FOR c_bal_summary_rec IN c_site_bal_summary
6178         LOOP
6179             summary_rec_str := summary_rec_str||'{'||
6180                             c_bal_summary_rec.current_receivable_balance||'{'||
6181                             c_bal_summary_rec.unapplied_cash_amount||'{'||
6182                             c_bal_summary_rec.unapplied_cash_count||'{'||
6183                             c_bal_summary_rec.past_due_inv_value||'{'||
6184                             c_bal_summary_rec.past_due_inv_inst_count||'{'||
6185                             c_bal_summary_rec.inv_amt_in_dispute||'{'||
6186                             c_bal_summary_rec.disputed_inv_count||'{'||
6187                             c_bal_summary_rec.pending_adj_value||'{'||
6188                             c_bal_summary_rec.total_receipts_at_risk_value||'{'||
6189                             c_bal_summary_rec.op_invoices_value||'{'||
6190                             c_bal_summary_rec.op_invoices_count||'{'||
6191                             c_bal_summary_rec.op_debit_memos_value||'{'||
6192                             c_bal_summary_rec.op_debit_memos_count||'{'||
6193                             c_bal_summary_rec.op_deposits_value||'{'||
6194                             c_bal_summary_rec.op_deposits_count||'{'||
6198                             c_bal_summary_rec.op_chargeback_count||'{'||
6195                             c_bal_summary_rec.op_bills_receivables_value||'{'||
6196                             c_bal_summary_rec.op_bills_receivables_count||'{'||
6197                             c_bal_summary_rec.op_chargeback_value||'{'||
6199                             c_bal_summary_rec.op_credit_memos_value||'{'||
6200                             c_bal_summary_rec.op_credit_memos_count||'{'||
6201                             c_bal_summary_rec.current_invoice_value||'{'||
6202                             c_bal_summary_rec.current_invoice_count;
6203         END LOOP;
6204       END IF;
6205     EXCEPTION
6206         WHEN OTHERS THEN
6207             p_error_msg := 'Error While Getting Data from AR_TRX_BAL_SUMMARY, Probably exchange rate '
6208                            ||'is not set correctly '||'Sql Error:'||sqlerrm;
6209             p_resultout := 1;
6210             ar_cmgt_util.wf_debug(p_case_folder_id, p_error_msg || '2');
6211             return;
6212     END;
6213     summary_rec_str := summary_rec_str||'{'; -- adding for instrb call to make life easier
6214 
6215     -- Assign the number of columns fetched in the cursor
6216     for i IN 1..bal_summary_rec_col_count
6217     LOOP
6218         l_start_pos := instrb(summary_rec_str,'{',1,i);
6219         l_end_pos   := instrb(summary_rec_str,'{',1,i+1);
6220         l_combo_string := substrb(summary_rec_str,l_start_pos+1,
6221                                 l_end_pos-(l_start_pos+1));
6222         l_data_point_id_end_pos := instrb(l_combo_string,'^',1,1);
6223 
6224         l_data_point_id := substrb(l_combo_string,1,l_data_point_id_end_pos-1);
6225         l_data_point_value := substrb(l_combo_string,l_data_point_id_end_pos+1,20);
6226 
6227         build_case_folder_details(
6228                     p_case_folder_id,
6229                     l_data_point_id,
6230                     l_data_point_value,
6231                     p_mode,
6232                     p_error_msg,
6233                     p_resultout);
6234         IF p_resultout <> 0
6235         THEN
6236             return;
6237         END IF;
6238     END LOOP;
6239     summary_rec_str := NULL;
6240     -- Calculate DSO, WADPL (5), and average_payment_days (4)
6241     IF l_analysis_level = 'P'
6242     THEN
6243         -- the cursor will always return one row
6244         FOR c_party_numerator_dso_rec IN c_party_numerator_dso
6245         LOOP
6246             l_numerator_dso := c_party_numerator_dso_rec.dso;
6247             l_numerator_ddso := c_party_numerator_dso_rec.delinquent_dso;
6248         END LOOP;
6249         FOR c_party_deno_dso_rec IN c_party_deno_dso
6250         LOOP
6251             l_deno_dso := c_party_deno_dso_rec.dso;
6252             l_deno_ddso := c_party_deno_dso_rec.delinquent_dso;
6253         END LOOP;
6254 
6255         /* 8661054 - DSO and DDSO should return null if there is no
6256            activity within the specified number of days */
6257 
6258         -- Weighted average days paid late (5) and average payment days (4)
6259         SELECT Round(
6260              SUM(gl_currency_api.convert_amount(currency,l_limit_currency,
6261                                                 SYSDATE, l_exchange_rate_type,
6262                 (NVL(SUM_APP_AMT_DAYS_LATE,0)))) /
6263                    Decode(SUM(gl_currency_api.convert_amount(currency,l_limit_currency,
6264                                                              SYSDATE,l_exchange_rate_type,
6265                          (NVL(SUM_APP_AMT,0)))),0,1,
6266                           SUM(gl_currency_api.convert_amount(currency,l_limit_currency,
6267                                                              SYSDATE,l_exchange_rate_type,
6268                              (NVL(SUM_APP_AMT,0))))),2) wt_average_days_paid_late,
6269                Round(
6270              SUM(gl_currency_api.convert_amount(currency,l_limit_currency,
6271                                                 SYSDATE, l_exchange_rate_type,
6272                 (NVL(INV_INST_PMT_DAYS_SUM,0)))) /
6273                    Decode(SUM(gl_currency_api.convert_amount(currency,l_limit_currency,
6274                                                              SYSDATE,l_exchange_rate_type,
6275                          (NVL(SUM_APP_AMT,0)))),0,1,
6276                           SUM(gl_currency_api.convert_amount(currency,l_limit_currency,
6277                                                              SYSDATE,l_exchange_rate_type,
6278                              (NVL(SUM_APP_AMT,0))))),2) wt_average_pmt_days
6279         INTO   l_wadpl,
6280                l_apd
6281         FROM   ar_trx_summary
6282         WHERE  CUST_ACCOUNT_ID in (select TO_NUMBER(cust_account_id)  -- Fix for Bug 12670874
6283                                FROM   hz_cust_accounts
6284                                WHERE  party_id in
6285                                 ( SELECT child_id
6286                                   from hz_hierarchy_nodes
6287                                   where parent_object_type = 'ORGANIZATION'
6288                                   and parent_table_name = 'HZ_PARTIES'
6289                                   and child_object_type = 'ORGANIZATION'
6290                                   and parent_id = p_party_id
6291                                   and effective_start_date <= sysdate
6292                                   and effective_end_date >= sysdate
6293                                   and  hierarchy_type = FND_PROFILE.VALUE('AR_CMGT_HIERARCHY_TYPE')
6294                                   and  g_source_name <> 'LNS'
6295                                   union select p_party_id from dual
6296                                   UNION
6297                     select hz_party_id
6298                     from LNS_LOAN_PARTICIPANTS_V
6299                     where loan_id = g_source_id
6300                     and   participant_type_code = 'COBORROWER'
6301                     and   g_source_name = 'LNS'
6302                     and (end_date_active is null OR
6306         AND    CURRENCY     IN  ( SELECT CURRENCY
6303                           (sysdate between start_date_active and end_date_active)
6304                           )
6305                              ))
6307                                   FROM   ar_cmgt_curr_usage_gt
6308                                   WHERE  nvl(credit_request_id,p_credit_request_id) =
6309                                          p_credit_request_id)
6310         AND    AS_OF_DATE  >= ADD_MONTHS(sysdate,(-l_period));
6311 
6312     ELSIF l_analysis_level = 'A'
6313     THEN
6314         -- the cursor will always return one row
6315         FOR c_account_numerator_dso_rec IN c_account_numerator_dso
6316         LOOP
6317             l_numerator_dso := c_account_numerator_dso_rec.dso;
6318             l_numerator_ddso := c_account_numerator_dso_rec.delinquent_dso;
6319         END LOOP;
6320 
6321         FOR c_account_deno_dso_rec IN c_account_deno_dso
6322         LOOP
6323             l_deno_dso := c_account_deno_dso_rec.dso;
6324             l_deno_ddso := c_account_deno_dso_rec.delinquent_dso;
6325         END LOOP;
6326 
6327         /* 8661054 - DSO and DDSO should return null if there is no
6328            activity within the specified number of days */
6329 
6330         -- Weighted average days paid late (5) and average payment days (4)
6331         SELECT Round(
6332              SUM(gl_currency_api.convert_amount(currency,l_limit_currency,
6333                                                 SYSDATE, l_exchange_rate_type,
6334                 (NVL(SUM_APP_AMT_DAYS_LATE,0)))) /
6335                    Decode(SUM(gl_currency_api.convert_amount(currency,l_limit_currency,
6336                                                              SYSDATE,l_exchange_rate_type,
6337                          (NVL(SUM_APP_AMT,0)))),0,1,
6338                           SUM(gl_currency_api.convert_amount(currency,l_limit_currency,
6339                                                              SYSDATE,l_exchange_rate_type,
6340                              (NVL(SUM_APP_AMT,0))))),2) wt_average_days_paid_late,
6341                Round(
6342              SUM(gl_currency_api.convert_amount(currency,l_limit_currency,
6343                                                 SYSDATE, l_exchange_rate_type,
6344                 (NVL(INV_INST_PMT_DAYS_SUM,0)))) /
6345                    Decode(SUM(gl_currency_api.convert_amount(currency,l_limit_currency,
6346                                                              SYSDATE,l_exchange_rate_type,
6347                          (NVL(SUM_APP_AMT,0)))),0,1,
6348                           SUM(gl_currency_api.convert_amount(currency,l_limit_currency,
6349                                                              SYSDATE,l_exchange_rate_type,
6350                              (NVL(SUM_APP_AMT,0))))),2) wt_average_pmt_days
6351         INTO   l_wadpl,
6352                l_apd
6353         FROM   ar_trx_summary
6354         WHERE  ORG_ID       = decode(l_global_exposure_flag,'Y', org_id, 'N',
6355                                 decode(p_org_id,null, org_id, p_org_id), null,
6356                                   decode(p_org_id,null, org_id, p_org_id))
6357         AND    CUST_ACCOUNT_ID = p_cust_account_id
6358         AND    CURRENCY     IN  ( SELECT CURRENCY
6359                                   FROM   ar_cmgt_curr_usage_gt
6360                                   WHERE nvl(credit_request_id,p_credit_request_id) =
6361                                           p_credit_request_id)
6362         AND    AS_OF_DATE  >= ADD_MONTHS(sysdate,(-l_period));
6363 
6364     ELSIF l_analysis_level = 'S'
6365     THEN
6366         -- the cursor will always return one row
6367         FOR c_site_numerator_dso_rec IN c_site_numerator_dso
6368         LOOP
6369             l_numerator_dso := c_site_numerator_dso_rec.dso;
6370             l_numerator_ddso := c_site_numerator_dso_rec.delinquent_dso;
6371         END LOOP;
6372         FOR c_site_deno_dso_rec IN c_site_deno_dso
6373         LOOP
6374             l_deno_dso := c_site_deno_dso_rec.dso;
6375             l_deno_ddso := c_site_deno_dso_rec.delinquent_dso;
6376         END LOOP;
6377 
6378         /* 8661054 - DSO and DDSO should return null if there is no
6379            activity within the specified number of days */
6380 
6381         -- Weighted average days paid late (5) and average payment days (4)
6382         SELECT Round(
6383              SUM(gl_currency_api.convert_amount(currency,l_limit_currency,
6384                                                 SYSDATE, l_exchange_rate_type,
6385                 (NVL(SUM_APP_AMT_DAYS_LATE,0)))) /
6386                    Decode(SUM(gl_currency_api.convert_amount(currency,l_limit_currency,
6387                                                              SYSDATE,l_exchange_rate_type,
6388                          (NVL(SUM_APP_AMT,0)))),0,1,
6389                           SUM(gl_currency_api.convert_amount(currency,l_limit_currency,
6390                                                              SYSDATE,l_exchange_rate_type,
6391                              (NVL(SUM_APP_AMT,0))))),2) wt_average_days_paid_late,
6392                Round(
6393              SUM(gl_currency_api.convert_amount(currency,l_limit_currency,
6394                                                 SYSDATE, l_exchange_rate_type,
6395                 (NVL(INV_INST_PMT_DAYS_SUM,0)))) /
6396                    Decode(SUM(gl_currency_api.convert_amount(currency,l_limit_currency,
6397                                                              SYSDATE,l_exchange_rate_type,
6398                          (NVL(SUM_APP_AMT,0)))),0,1,
6399                           SUM(gl_currency_api.convert_amount(currency,l_limit_currency,
6400                                                              SYSDATE,l_exchange_rate_type,
6401                              (NVL(SUM_APP_AMT,0))))),2) wt_average_pmt_days
6402         INTO   l_wadpl,
6403                l_apd
6404         FROM   ar_trx_summary
6405         WHERE  CUST_ACCOUNT_ID = p_cust_account_id
6406         AND    CURRENCY     IN  ( SELECT a.CURRENCY
6410         AND    AS_OF_DATE  >= ADD_MONTHS(sysdate,(-l_period))
6407                                   FROM   ar_cmgt_curr_usage_gt a
6408                                   WHERE  nvl(credit_request_id,p_credit_request_id) =
6409                                          p_credit_request_id)
6411         AND    SITE_USE_ID  = p_cust_acct_site_id;
6412 
6413     END IF;
6414 
6415         /* 8661054 - Moved dso and ddso calcs to after IF,
6416             corrected misuse of l_deno_dso in ddso calc */
6417     IF pg_wf_debug = 'Y'
6418     THEN
6419        ar_cmgt_util.wf_debug(l_tag,
6420           'DSO:  numerator = ' || l_numerator_dso || '  denom = ' ||
6421                      l_deno_dso);
6422        ar_cmgt_util.wf_debug(l_tag,
6423            'DDSO:  numerator = ' || l_numerator_ddso || '  denom = ' ||
6424                      l_deno_ddso);
6425     END IF;
6426 
6427     /* 8661054 - DSO and DDSO should return null if there is no
6428        activity within the specified number of days */
6429     IF l_deno_dso is NOT NULL AND l_deno_dso <> 0
6430     THEN
6431        l_dso := round((l_numerator_dso/l_deno_dso),2);
6432     ELSE
6433        l_dso := NULL;
6434     END IF;
6435     IF l_deno_ddso IS NOT NULL AND l_deno_ddso <> 0
6436     THEN
6437        l_ddso := round((l_numerator_ddso/l_deno_ddso),2);
6438     ELSE
6439        l_ddso := NULL;
6440     END IF;
6441 
6442     IF pg_wf_debug = 'Y'
6443     THEN
6444        ar_cmgt_util.wf_debug(l_tag,
6445               'DSO (2)  = ' || l_dso);
6446        ar_cmgt_util.wf_debug(l_tag,
6447               'DDSO (3) = ' || l_ddso);
6448        ar_cmgt_util.wf_debug(l_tag,
6449               'APD (4)  = ' || l_apd);
6450        ar_cmgt_util.wf_debug(l_tag,
6451               'WADPL (5) = ' || l_wadpl);
6452 
6453     END IF;
6454 
6455         build_case_folder_details(
6456                     p_case_folder_id,
6457                     2, --dso
6458                     fnd_number.number_to_canonical(l_dso),
6459                     p_mode,
6460                     p_error_msg,
6461                     p_resultout);
6462 
6463         IF p_resultout <> 0
6464         THEN
6465             return;
6466         END IF;
6467 
6468         build_case_folder_details(
6469                     p_case_folder_id,
6470                     3, -- deliquent dso
6471                     fnd_number.number_to_canonical(l_ddso),
6472                     p_mode,
6473                     p_error_msg,
6474                     p_resultout);
6475 
6476         IF p_resultout <> 0
6477         THEN
6478             return;
6479         END IF;
6480 
6481         build_case_folder_details(
6482                     p_case_folder_id,
6483                     4,
6484                     fnd_number.number_to_canonical(l_apd),
6485                     p_mode,
6486                     p_error_msg,
6487                     p_resultout);
6488 
6489         IF p_resultout <> 0
6490         THEN
6491             return;
6492         END IF;
6493 
6494         build_case_folder_details(
6495                     p_case_folder_id,
6496                     5,
6497                     fnd_number.number_to_canonical(l_wadpl),
6498                     p_mode,
6499                     p_error_msg,
6500                     p_resultout);
6501 
6502         IF p_resultout <> 0
6503         THEN
6504             return;
6505         END IF;
6506 
6507     --kosriniv number format needs to be handled in GetAnalytical Data for invoice Amounts..
6508     -- get analytic data
6509     GetAnalyticalData (
6510             p_credit_request_id        => p_credit_request_id,
6511             p_party_id                 => p_party_id,
6512             p_cust_account_id          => p_cust_account_id,
6513             p_site_use_id              => p_cust_acct_site_id,
6514             p_case_folder_id           => p_case_folder_id,
6515             p_analysis_level           => l_analysis_level,
6516             p_org_id                   => p_org_id,
6517             p_period                   => l_period,
6518             p_global_exposure_flag     => l_global_exposure_flag,
6519             p_limit_currency           => l_limit_currency,
6520             p_exchange_rate_type       => l_exchange_rate_type,
6521             p_mode                     => p_mode,
6522             p_errormsg                 => p_error_msg,
6523             p_resultout                => p_resultout);
6524     IF ( p_resultout <> 0 )
6525     THEN
6526         return;
6527     END IF;
6528     -- kosriniv ... Dunning only includes the dunning count and last dunning date.
6529     GetDunning(
6530             p_credit_request_id        => p_credit_request_id,
6531             p_party_id                 => p_party_id,
6532             p_cust_account_id          => p_cust_account_id,
6533             p_site_use_id              => p_cust_acct_site_id,
6534             p_org_id                   => p_org_id,
6535             p_case_folder_id           => p_case_folder_id,
6536             p_period                   => l_period,
6537             p_analysis_level           => l_analysis_level,
6538             p_global_exposure_flag     => l_global_exposure_flag,
6539             p_mode                     => p_mode,
6540             p_errormsg                 => p_error_msg,
6541             p_resultout                => p_resultout);
6542     IF p_resultout <> 0
6543     THEN
6544         return;
6545     END IF;
6546     -- kosriniv...Change the Number format of 95,96,98,99,108,30.
6547     -- this procedure will populate all data points which is not in summary tables.
6548     GetOtherDataPoints (
6549             p_credit_request_id         => p_credit_request_id,
6550             p_party_id                  => p_party_id,
6554             p_case_folder_id            => p_case_folder_id,
6551             p_cust_account_id           => p_cust_account_id,
6552             p_site_use_id               => p_cust_acct_site_id,
6553             p_cust_acct_profile_amt_id  => l_cust_acct_profile_amt_id,
6555             p_mode                      => p_mode);
6556     -- kosriniv .. Change the Number format of 15,26,213
6557     GetOMDataPoints(
6558             p_credit_request_id         => p_credit_request_id,
6559             p_party_id                  => p_party_id,
6560             p_cust_account_id           => p_cust_account_id,
6561             p_site_use_id               => p_cust_acct_site_id,
6562             p_case_folder_id            => p_case_folder_id,
6563             p_analysis_level            => l_analysis_level,
6564             p_limit_currency_code       => p_limit_currency,
6565             p_mode                      => p_mode,
6566             p_errormsg                  => p_error_msg,
6567             p_resultout                 => p_resultout);
6568     IF (p_resultout <> 0)
6569     THEN
6570         return;
6571     END IF;
6572     -- ko   Need to Look into this further...
6573     BUILD_DNB_CASE_FOLDER(
6574             p_party_id,
6575             p_check_list_id,
6576             p_case_folder_id,
6577             p_mode,
6578             l_error_msg,
6579             l_resultout);
6580 
6581 
6582 
6583      --kosriniv .. No need of changing the Number format.. The stored data type Number only.
6584       populate_aging_data(
6585                 p_case_folder_id    => p_case_folder_id,
6586                 p_mode              => p_mode,
6587                 p_error_msg         => p_error_msg,
6588                 p_resultout         => p_resultout);
6589       IF ( p_resultout <> 0 )
6590       THEN
6591         return;
6592       END IF;
6593 
6594      -- ko   Need to Look into this further...
6595      GetManualDataPoints(
6596             p_credit_request_id        => p_credit_request_id,
6597             p_case_folder_id        => p_case_folder_id,
6598             p_check_list_id         => p_check_list_id,
6599             p_mode                  => p_mode,
6600             x_error_msg             => p_error_msg,
6601             x_resultout             => p_resultout);
6602      IF p_resultout <> 0
6603      THEN
6604         return;
6605      END IF;
6606 
6607       -- kosriniv .. Change Number format of data points ..113 ..140.
6608      GetFinancialData (
6609         p_credit_request_id         => p_credit_request_id,
6610         p_case_folder_id            => p_case_folder_id,
6611         p_mode                      => p_mode,
6612         p_resultout                 => p_resultout,
6613         p_errmsg                    => p_error_msg);
6614      IF p_resultout <> 0
6615      THEN
6616         return;
6617      END IF;
6618 
6619      -- kosriniv .. Change Numebr For Data points 211.
6620      -- call deduction procedure to populate deduction data points
6621      GetDeductionDataPoints ( -- bug 3691676
6622             p_credit_request_id         => p_credit_request_id,
6623             p_case_folder_id            => p_case_folder_id,
6624             p_period                    => l_period,
6625             p_party_id                  => p_party_id,
6626             p_cust_account_id           => p_cust_account_id,
6627             p_site_use_id               => p_cust_acct_site_id,
6628             p_analysis_level            => l_analysis_level,
6629             p_org_id                    => p_org_id,
6630             p_mode                      => p_mode,
6631             p_limit_currency            => l_limit_currency,
6632             p_exchange_rate_type        => l_exchange_rate_type,
6633             p_global_exposure_flag      => l_global_exposure_flag,
6634             p_error_msg                 => p_error_msg,
6635             p_resultout                 => p_resultout );
6636 
6637      IF p_resultout <> 0
6638      THEN
6639         return;
6640      END IF;
6641      -- in case of Refresh update the status back to save in case folder.
6642      IF p_mode = 'REFRESH'
6643      THEN
6644            -- since case folder got refreshed, need to refresh the score too
6645            -- call scoring engine
6646            ar_cmgt_scoring_engine.generate_score(
6647                     p_case_folder_id    =>  p_case_folder_id,
6648                     p_score             =>  l_score,
6649                     p_error_msg         =>  p_error_msg,
6650                     p_resultout         =>  p_resultout);
6651            IF p_resultout <> 0
6652            THEN
6653       -- error in generating the score, so update the score to null
6654     p_resultout := 0;
6655     UPDATE  ar_cmgt_cf_dtls
6656         SET     score = null,
6657                 last_updated_by = fnd_global.user_id,
6658                 last_update_date = sysdate,
6659                 last_update_login = fnd_global.login_id
6660         WHERE   case_folder_id = p_case_folder_id;
6661            END IF;
6662            UPDATE ar_cmgt_case_folders
6663            SET  status   = 'SAVED',
6664                     last_updated = SYSDATE,
6665                     last_update_date = SYSDATE,
6666                     last_updated_by  = fnd_global.user_id,
6667                     last_update_login = fnd_global.login_id
6668            WHERE   case_folder_id = p_case_folder_id
6669            AND     type  = 'CASE';
6670 
6671 
6672     END IF;
6673 
6674     -- update all data points value in case folder details which is included in
6675     -- checklist for type 'CASE'.
6676     IF ( p_check_list_id IS NOT NULL )
6677     THEN
6678         UPDATE ar_cmgt_cf_dtls
6679             set included_in_checklist = 'Y'
6680         WHERE  case_folder_id = p_case_folder_id
6681         AND    data_point_id in (
6682                 SELECT data_point_id
6686     IF pg_wf_debug = 'Y'
6683                     FROM ar_cmgt_check_list_dtls
6684                 WHERE check_list_id = p_check_list_id);
6685     END IF;
6687     THEN
6688        ar_cmgt_util.wf_debug(l_tag,
6689               'ar_cmgt_data_points_pkg.gather_data_points()-');
6690     END IF;
6691 END GATHER_DATA_POINTS;
6692 
6693 
6694 END AR_CMGT_DATA_POINTS_PKG;