DBA Data[Home] [Help]

PACKAGE BODY: APPS.OE_CREDIT_EXPOSURE_PVT

Source


1 PACKAGE BODY OE_CREDIT_EXPOSURE_PVT AS
2 -- $Header: OEXVCRXB.pls 120.9.12010000.3 2009/01/08 13:05:36 msundara ship $
3 
4 --+=======================================================================+
5 --|               Copyright (c) 1998 Oracle Corporation                   |
6 --|                       Redwood Shores, CA, USA                         |
7 --|                         All rights reserved.                          |
8 --+=======================================================================+
9 --| FILENAME                                                              |
10 --|    OEXVCRXB.pls                                                       |
11 --|                                                                       |
12 --| DESCRIPTION                                                           |
13 --|    Body of OE_CREDIT_EXPOSURE_PVT                                     |
14 --|                                                                       |
15 --| HISTORY                                                               |
16 --|     06/05/2001 Rene Schaub     Created                                |
17 --|     02/04/2002 Rajesh Krishnan ontdev => 115.10 2001/09/01 00:53:53   |
18 --|                                multi org FEB-04-2002 7PM              |
19 --|     03/15/2002 Vanessa To      modified get_exposure - include        |
20 --|                                external exposure.                     |
21 --|     05/01/2002 rajkrish        Bug 2352020                            |
22 --|     06/18/2002 vto             renee -- init summary only             |
23 --|     Aug-2002  rajkrish - Implement MUlti buckets algorithm            |
24 --|               ontdev==> 115.14 2002/05/02                             |
25 --|     August-29-2002 rajkrish 6PM                                       |
26 --|     12/20/2002 vto             Added NOCOPY to OUT variables          |
27 --|     23-DEc-2002 - BR Issue                                            |
28 --|     07/02/2003 tsimmond     Added code for the FPJ Returns project in |
29 --|                             Init_Summary_Table,balance types proc.,   |
30 --|                             Retrieve_Exposure                         |
31 --|     08/27/2003 vto          Modification for Partial payments support |
32 --|                                                                       |
33 --|     12/23/2003 tsimmond     Modified insert for G_ORDER_RETURN_HOLDS, |
34 --|                             G_ORDER_RETURN_TAX_HOLDS,                 |
35 --|                             G_LINE_RETURN_TAX_HOLDS,                  |
36 --|                             and G_LINE_RETURN_HOLDS, bug fix 3223770  |
37 --|     01/06/2004 vto          3320260. Modified to handle non-multiorg  |
38 --|     08/06/2004 vto          3818562. Add NVL on h.request_date        |
39 --+======================================================================*/
40 
41 
42 --===================
43 -- CONSTANTS
44 --===================
45 G_PKG_NAME CONSTANT VARCHAR2(30) := 'OE_CREDIT_EXPOSURE_PVT';
46 G_debug_flag        VARCHAR2(1) :=
47  NVL(oe_credit_check_util.check_debug_flag , 'N' ) ;
48 --===================
49 -- GLOBAL VARIABLES
50 --===================
51   b1              NUMBER;
52   b2              NUMBER;
53   b3              NUMBER;
54   b4              NUMBER;
55   b5              NUMBER;
56   b6              NUMBER;
57   b7              NUMBER;
58   b8              NUMBER;
59   b9              NUMBER;
60   b10             NUMBER;
61   b11             NUMBER;
62   b12             NUMBER;
63   b13             NUMBER;
64   b14             NUMBER;
65   b15             NUMBER;
66   b16             NUMBER;
67   b17             NUMBER;
68   b18             NUMBER;
69   b21             NUMBER;
70   b22             NUMBER;
71 
72 ----added for the RETURNS-------
73   b23             NUMBER;
74   b24             NUMBER;
75   b25             NUMBER;
76   b26             NUMBER;
77   b27             NUMBER;
78   b28             NUMBER;
79   b29             NUMBER;
80   b30             NUMBER;
81   b31             NUMBER;
82   b32             NUMBER;
83   b33             NUMBER;
84   b34             NUMBER;
85   b35             NUMBER;
86   b36             NUMBER;
87 
88   g_error_curr_tbl        OE_CREDIT_CHECK_UTIL.curr_tbl_type;
89   g_conversion_type       OE_Credit_Check_Rules.conversion_type%TYPE;
90   g_functional_currency   FND_CURRENCIES.Currency_code%TYPE;
91   g_use_party_hierarchy   VARCHAR2(1) ;
92 
93   --g_external_exposure    NUMBER ;
94 
95 --===================
96 -- PRIVATE PROCEDURES
97 --===================
98 
99 ------------------------------------------------------
100 -- Function used for debug log printing the time execution
101 -------------------------------------------------------
102 
103 FUNCTION Do_Time RETURN VARCHAR2 IS
104   t   VARCHAR2(30);
105 BEGIN
106 
107  t := TO_CHAR( SYSDATE, 'YYYY-MON-HH-MI-SS' ) ;
108   RETURN t;
109 
110 END;
111 
112 
113 ------------------------------------------------------
114 -- PROCEDURE  : get_invoices_over_duedate
115 -- DESCRIPTION: Returns Y if Invoices exist past due dates
116 -- HISTORY:
117 -- 01/06/04  3320260: Modified to handle non-multiorg setup
118 -- 12/20/05  4514215: Modified all the cursors, replaced the condition
119 -- bucket  < l_jdate with   bucket  <= l_jdate
120 --------------------------------------------------------
121 PROCEDURE get_invoices_over_duedate
122 ( p_customer_id          IN   NUMBER
123 , p_site_use_id          IN   NUMBER
124 , p_party_id             IN   NUMBER
125 , p_credit_check_rule_rec IN
126              OE_CREDIT_CHECK_UTIL.OE_credit_rules_rec_type
127 , p_credit_level         IN   VARCHAR2
128 , p_usage_curr           IN   oe_credit_check_util.curr_tbl_type
129 , p_include_all_flag     IN   VARCHAR2
130 , p_global_exposure_flag IN   VARCHAR2 := 'N'
131 , p_org_id               IN   NUMBER
132 , x_exist_flag           OUT  NOCOPY VARCHAR2
133 )
134 IS
135 
136 l_jdate NUMBER;
137 l_return VARCHAR2(1) ;
138 
139  CURSOR chk_inv_past_due_cust(p_curr_code IN VARCHAR2 ) IS
140   SELECT 'Y'
141   FROM    OE_credit_summaries
142   WHERE   cust_account_id = p_customer_id
143    AND  currency_code   = p_curr_code
144    AND  bucket  <= l_jdate
145    AND  bucket_duration = 1
146    AND  ((org_id             =  p_org_id)
147          OR
148         (org_id IS NULL AND p_org_id IS NULL))
149    AND  balance_type    = 20 ;
150 
151  CURSOR chk_inv_past_due_cust_gl(p_curr_code IN VARCHAR2 ) IS
152   SELECT 'Y'
153   FROM    OE_credit_summaries
154   WHERE   cust_account_id = p_customer_id
155    AND  currency_code   = p_curr_code
156    AND  bucket  <= l_jdate
157    AND  bucket_duration = 1
158    AND  balance_type    = 20 ;
159 
160 
161  CURSOR chk_inv_past_due_site(p_curr_code IN VARCHAR2 ) IS
162   SELECT 'Y'
163   FROM    OE_credit_summaries
164   WHERE   site_use_id    = p_site_use_id
165    AND   currency_code  = p_curr_code
166    AND  bucket  <= l_jdate
167    AND  bucket_duration = 1
168    AND  balance_type    = 20 ;
169 
170  CURSOR chk_inv_past_due_party(p_curr_code IN VARCHAR2 ) IS
171    SELECT 'Y'
172   FROM    OE_credit_summaries oes
173      ,    hz_hierarchy_nodes hn
174   WHERE   hn.parent_id                 = p_party_id
175   AND  hn.parent_object_type           = 'ORGANIZATION'
176   and  hn.parent_table_name            = 'HZ_PARTIES'
177   and  hn.child_object_type            = 'ORGANIZATION'
178   and  hn.effective_start_date  <=  sysdate
179   and  hn.effective_end_date    >= SYSDATE
180   and  hn.hierarchy_type     =
181             OE_CREDIT_CHECK_UTIL.G_hierarchy_type
182   AND  oes.party_id                    =  hn.child_id
183   AND  oes.currency_code               = p_curr_code
184   AND  oes.bucket                 <= l_jdate
185   AND  oes.balance_type    = 20
186   AND  oes.bucket_duration = 1 ;
187 
188  --Bug 4991241
189  CURSOR chk_inv_past_due_single_party(p_curr_code IN VARCHAR2 ) IS
190   SELECT 'Y'
191   FROM    OE_credit_summaries
192   WHERE   party_id    = p_party_id
193    AND   currency_code  = p_curr_code
194    AND  bucket  <= l_jdate
195    AND  bucket_duration = 1
196    AND  balance_type    = 20 ;
197 
198  CURSOR chk_inv_past_due_cust_all IS
199   SELECT 'Y'
200   FROM    OE_credit_summaries
201   WHERE cust_account_id = p_customer_id
202    AND  bucket  <= l_jdate
203    AND  bucket_duration = 1
204    AND  ((org_id              =  p_org_id)
205           OR
206          (org_id IS NULL AND p_org_id IS NULL))
207    AND  balance_type    = 20 ;
208 
209  CURSOR chk_inv_past_due_cust_all_gl IS
210   SELECT 'Y'
211   FROM    OE_credit_summaries
212   WHERE cust_account_id = p_customer_id
213    AND  bucket  <= l_jdate
214    AND  bucket_duration = 1
215    AND  balance_type    = 20 ;
216 
217 
218  CURSOR chk_inv_past_due_site_all IS
219   SELECT 'Y'
220   FROM    OE_credit_summaries
221   WHERE site_use_id    = p_site_use_id
222    AND  bucket  <= l_jdate
223    AND  bucket_duration = 1
224    AND  balance_type    = 20 ;
225 
226  CURSOR chk_inv_past_due_party_all IS
227   SELECT 'Y'
228   FROM    OE_credit_summaries oes
229      ,    hz_hierarchy_nodes hn
230   WHERE   hn.parent_id                 = p_party_id
231   AND  hn.parent_object_type           = 'ORGANIZATION'
232   and  hn.parent_table_name            = 'HZ_PARTIES'
233   and  hn.child_object_type            = 'ORGANIZATION'
234   and  hn.effective_start_date  <=  sysdate
235   and  hn.effective_end_date    >= SYSDATE
236   and  hn.hierarchy_type     =
237             OE_CREDIT_CHECK_UTIL.G_hierarchy_type
238   AND  oes.party_id                      =  hn.child_id
239   AND  oes.bucket  <= l_jdate
240   AND  oes.balance_type    = 20
241   AND  oes.bucket_duration = 1;
242 
243  --Bug 4991241
244  CURSOR chk_inv_past_party_single_all IS
245   SELECT 'Y'
246   FROM    OE_credit_summaries
247   WHERE party_id    = p_party_id
248    AND  bucket  <= l_jdate
249    AND  bucket_duration = 1
250    AND  balance_type    = 20 ;
251 
252 BEGIN
253 
254   l_jdate := TO_NUMBER( TO_CHAR( ( SYSDATE -
255               p_credit_check_rule_rec.maximum_days_past_due ), 'J' ) );
256 
257   x_exist_flag := 'N' ;
258 
259   IF G_debug_flag = 'Y'
260   THEN
261     oe_debug_pub.add(' l_jdate => '|| l_jdate );
262     oe_debug_pub.add(' p_credit_level => '|| p_credit_level );
263     oe_debug_pub.add(' p_site_use_id => '|| p_site_use_id );
264     oe_debug_pub.add(' p_customer_id => '|| p_customer_id );
265     oe_debug_pub.add(' p_party_id => '|| p_party_id );
266   END IF;
267 
268  BEGIN
269 
270   IF p_credit_level = 'CUSTOMER'
271   THEN
272     IF p_include_all_flag = 'N'
273     THEN
274       FOR i IN 1..p_usage_curr.count
275       LOOP
276         IF p_global_exposure_flag = 'N'
277         THEN
278           OPEN chk_inv_past_due_cust (p_usage_curr(i).usage_curr_code ) ;
279           FETCH chk_inv_past_due_cust
280           INTO x_exist_flag ;
281 
282           IF chk_inv_past_due_cust%NOTFOUND
283           THEN
284             x_exist_flag := 'N' ;
285           ELSE
286             CLOSE chk_inv_past_due_cust ;
287             EXIT ;
288           END IF;
289           CLOSE chk_inv_past_due_cust ;
290 
291         ELSE
292          OPEN chk_inv_past_due_cust_gl (p_usage_curr(i).usage_curr_code ) ;
293          FETCH chk_inv_past_due_cust_gl INTO x_exist_flag ;
294          IF chk_inv_past_due_cust_gl%NOTFOUND
295          THEN
296            x_exist_flag := 'N' ;
297          ELSE
298            CLOSE chk_inv_past_due_cust_gl ;
299            EXIT;
300          END IF;
301          CLOSE chk_inv_past_due_cust_gl ;
302        END IF; -- global
303       END LOOP ;
304 
305     ELSE -- al currency
306       IF p_global_exposure_flag = 'N'
307       THEN
308         OPEN chk_inv_past_due_cust_all ;
309         FETCH chk_inv_past_due_cust_all
310         INTO x_exist_flag ;
311 
312           IF chk_inv_past_due_cust_all%NOTFOUND
313           THEN
314             x_exist_flag := 'N' ;
315           END IF;
316         CLOSE chk_inv_past_due_cust_all ;
317       ELSE
318         OPEN chk_inv_past_due_cust_all_gl  ;
319         FETCH chk_inv_past_due_cust_all_gl INTO x_exist_flag ;
320         IF chk_inv_past_due_cust_all_gl%NOTFOUND
321         THEN
322            x_exist_flag := 'N' ;
323         END IF;
324 
325         CLOSE chk_inv_past_due_cust_all_gl ;
326       END IF ; -- global;
327     END IF; -- all curr
328 
329   ELSIF p_credit_level = 'SITE'
330   THEN
331    IF p_include_all_flag = 'N'
332    THEN
333      FOR i IN 1..p_usage_curr.count
334      LOOP
335 
336       OPEN chk_inv_past_due_site(p_usage_curr(i).usage_curr_code) ;
337       FETCH chk_inv_past_due_site
338       INTO x_exist_flag ;
339 
340         IF chk_inv_past_due_site%NOTFOUND
341         THEN
342           x_exist_flag := 'N' ;
343         ELSE
344           CLOSE chk_inv_past_due_site ;
345           EXIT ;
346         END IF;
347 
348        CLOSE chk_inv_past_due_site ;
349      END LOOP;
350    ELSE -- all curr
351      OPEN chk_inv_past_due_site_all ;
352      FETCH chk_inv_past_due_site_all
353      INTO x_exist_flag ;
354 
355        IF chk_inv_past_due_site_all%NOTFOUND
356         THEN
357           x_exist_flag := 'N' ;
358        END IF;
359 
360       CLOSE chk_inv_past_due_site_all ;
361    END IF;
362 
363  ELSIF p_credit_level = 'PARTY'
364  THEN
365    IF p_include_all_flag = 'N'
366    THEN
367      FOR i IN 1..p_usage_curr.count
368      LOOP
369        --Bug 4991241
370        OPEN chk_inv_past_due_single_party(p_usage_curr(i).usage_curr_code ) ;
371        FETCH chk_inv_past_due_single_party
372        INTO x_exist_flag ;
373        IF chk_inv_past_due_single_party%NOTFOUND
374        THEN
375          x_exist_flag := 'N' ;
376        ELSE
377          CLOSE chk_inv_past_due_single_party ;
378          EXIT ;
379        END IF;
380        CLOSE chk_inv_past_due_single_party ;
381 
382        OPEN chk_inv_past_due_party(p_usage_curr(i).usage_curr_code ) ;
383        FETCH chk_inv_past_due_party
384        INTO x_exist_flag ;
385 
386        IF chk_inv_past_due_party%NOTFOUND
387        THEN
388          x_exist_flag := 'N' ;
389        ELSE
390          CLOSE chk_inv_past_due_party ;
391          EXIT ;
392        END IF;
393        CLOSE chk_inv_past_due_party ;
394      END LOOP;
395    ELSE
396      --Bug 4991241
397      OPEN chk_inv_past_party_single_all ;
398      FETCH  chk_inv_past_party_single_all INTO x_exist_flag ;
399 
400       IF chk_inv_past_party_single_all%NOTFOUND
401       THEN
402         x_exist_flag := 'N' ;
403       END IF;
404      CLOSE chk_inv_past_party_single_all ;
405 
406      IF x_exist_flag = 'N' THEN
407      OPEN chk_inv_past_due_party_all ;
408      FETCH  chk_inv_past_due_party_all INTO x_exist_flag ;
409 
410       IF chk_inv_past_due_party_all%NOTFOUND
411       THEN
412         x_exist_flag := 'N' ;
413       END IF;
414      CLOSE chk_inv_past_due_party_all ;
415      END IF;
416    END IF;
417 
418 
419  END IF; -- credit level
420 
421  END ;
422 
423 END get_invoices_over_duedate ;
424 
425 ------------------------------------------------------------------
426 -- This procedure will populate the PL/SQL table
427 -- used for storing the currencies that failed the
428 -- conversion to the credit limit currencies
429 -----------------------------------------------------
430 PROCEDURE add_error_currency
431 ( x_error_curr_tbl     IN OUT  NOCOPY  OE_CREDIT_CHECK_UTIL.curr_tbl_type
432 , p_error_currency     IN           HZ_CREDIT_PROFILE_AMTS.currency_code%TYPE
433 )
434 IS
435   i  BINARY_INTEGER;
436 BEGIN
437   i  := x_error_curr_tbl.FIRST;
438   WHILE  i  IS NOT NULL  LOOP
439     IF
440       p_error_currency  =  x_error_curr_tbl(i).usage_curr_code
441     THEN
442       EXIT;
443     END IF;
444     i := x_error_curr_tbl.NEXT(i);
445   END LOOP;
446 
447   IF  i  IS NULL THEN
448     x_error_curr_tbl( NVL(x_error_curr_tbl.LAST, 0) + 1 ).usage_curr_code  :=
449       p_error_currency;
450   END IF;
451 END;
452 
453 ------------------------------------------------------
454 --- This function will return the OM Horizon Date
455 --- in the Juilan format
456 ---------------------------------------------
457 
458 FUNCTION ship_date( p_date  IN  DATE )
459 RETURN NUMBER
460 IS
461   l_date   NUMBER;
462 BEGIN
463   IF p_date  IS NULL  THEN
464     l_date  := TO_NUMBER( TO_CHAR( (SYSDATE + 1000 ), 'J' ) );
465     -- last day of a max bucket in the far future
466     l_date  := l_date - MOD( l_date, G_MAX_BUCKET_LENGTH )  - 1;
467   ELSE
468     l_date  := TO_NUMBER( TO_CHAR( p_date, 'J' ) );
469   END IF;
470 
471   IF G_debug_flag = 'Y'
472   THEN
473     oe_debug_pub.add( 'Shipping Horizon Julian Date is:' || l_date, 2 );
474   END IF;
475 
476   RETURN l_date;
477 
478 END ship_date;
479 
480 -----------------------------------------------------
481 -- This function will return the AR Horizon date
482 -- in Juilan format
483 --------------------------------------------------------
484 FUNCTION open_date( p_days  IN  NUMBER )
485 RETURN NUMBER
486 IS
487   l_date   NUMBER;
488 BEGIN
489   IF  p_days  IS NULL  THEN
490     l_date  := TO_NUMBER( TO_CHAR( ( SYSDATE + 1000 ), 'J' ) );
491     -- last day of a max bucket in the far future
492     l_date  := l_date - MOD( l_date, G_MAX_BUCKET_LENGTH )  - 1;
493 
494   ELSE
495     l_date  := TO_NUMBER( TO_CHAR( ( SYSDATE - p_days ), 'J' ) );
496   END IF;
497 
498   IF G_debug_flag = 'Y'
499   THEN
500      oe_debug_pub.add( 'Open Receivables Horizon Julian is:' || l_date, 2 );
501   END IF;
502 
503   RETURN l_date;
504 
505 END open_date;
506 
507 
508 PROCEDURE balance_types_om_and_ar
509 ( p_credit_check_rule_rec   IN    OE_CREDIT_CHECK_UTIL.oe_credit_rules_rec_type
510 )
511 IS
512 BEGIN
513   b1 := -1;
514   b2 := -1;
515   b3 := -1;
516   b4 := -1;
517   b5 := -1;
518   b6 := -1;
519   b7 := -1;
520   b8 := -1;
521   b9 := -1;
522   b10 := -1;
523   b11 := -1;
524   b12 := -1;
525   b13 := -1;
526   b14 := -1;
527   b15 := -1;
528   b16 := -1;
529   b17 := -1;
530   b18 := -1;
531   b21 := -1;
532   b22 := -1;
533 
534   ----added for the RETURNS
535   b23 := -1;
536   b24 := -1;
537   b25 := -1;
538   b26 := -1;
539   b27 := -1;
540   b28 := -1;
541   b29 := -1;
542   b30 := -1;
543   b31 := -1;
544   b32 := -1;
545   b33 := -1;
546   b34 := -1;
547   b35 := -1;
548   b36 := -1;
549 
550 
551 
552   -- Determine which balance types to select:
553   -- Chosing a balance type is indicated by setting the corresponding
554   -- variable to a balance type global constant
555   -- (e.g. b4 := G_ORDER_HOLDS)
556 
557   IF  p_credit_check_rule_rec.uninvoiced_orders_flag     =  'Y'
558   THEN
559     IF p_credit_check_rule_rec.credit_check_level_code  = 'LINE'
560     THEN
561       b3 := G_LINE_UNINVOICED_ORDERS;
562 
563       -----added for the Returns project
564       IF p_credit_check_rule_rec.include_returns_flag = 'Y'
565       THEN
566         b24 := G_LINE_RETURN_UNINV_ORDERS;
567       END IF;
568 
569       IF p_credit_check_rule_rec.orders_on_hold_flag  =  'N'
570       THEN
571         b11 := G_LINE_HOLDS;
572 
573         -----added for the Returns project
574         IF p_credit_check_rule_rec.include_returns_flag = 'Y'
575         THEN
576           b31 := G_LINE_RETURN_HOLDS;
577         END IF;
578 
579       END IF;
580     ELSE
581       b1 := G_HEADER_UNINVOICED_ORDERS;
582 
583       -----added for the Returns project
584       IF p_credit_check_rule_rec.include_returns_flag = 'Y'
585       THEN
586         b23 := G_HEAD_RETURN_UNINV_ORDERS;
587       END IF;
588 
589       IF p_credit_check_rule_rec.orders_on_hold_flag  =  'N'
590       THEN
591         b10 := G_ORDER_HOLDS;
592 
593         -----added for the Returns project
594         IF p_credit_check_rule_rec.include_returns_flag = 'Y'
595         THEN
596           b30 := G_ORDER_RETURN_HOLDS;
597         END IF;
598 
599       END IF;
600     END IF;
601 
602   END IF;
603 
604   IF  p_credit_check_rule_rec.include_tax_flag           =  'Y'
605   THEN
606     IF p_credit_check_rule_rec.credit_check_level_code  = 'LINE'
607     THEN
608       b4 := G_LINE_UNINVOICED_ORDERS_TAX;
609 
610       -----added for the Returns project
611       IF p_credit_check_rule_rec.include_returns_flag = 'Y'
612       THEN
613         b26:= G_LINE_RETURN_UNINV_ORD_TAX;
614       END IF;
615 
616       IF p_credit_check_rule_rec.orders_on_hold_flag  =  'N'
617       THEN
618         b14 := G_LINE_TAX_HOLDS;
619 
620         -----added for the Returns project
621         IF p_credit_check_rule_rec.include_returns_flag = 'Y'
622         THEN
623           b33:= G_LINE_RETURN_TAX_HOLDS;
624         END IF;
625 
626       END IF;
627     ELSE
628       b2 := G_HEADER_UNINVOICED_ORDERS_TAX;
629 
630       -----added for the Returns project
631       IF p_credit_check_rule_rec.include_returns_flag = 'Y'
632       THEN
633         b25:= G_HEAD_RETURN_UNINV_ORD_TAX;
634       END IF;
635 
636       IF p_credit_check_rule_rec.orders_on_hold_flag  =  'N'
637       THEN
638         b13 := G_ORDER_TAX_HOLDS;
639 
640         -----added for the Returns project
641         IF p_credit_check_rule_rec.include_returns_flag = 'Y'
642         THEN
643           b32:= G_ORDER_RETURN_TAX_HOLDS;
644         END IF;
645 
646       END IF;
647     END IF;
648   END IF;
649 
650   IF  p_credit_check_rule_rec.incl_freight_charges_flag  =  'Y'
651   THEN
652     IF p_credit_check_rule_rec.credit_check_level_code  = 'LINE'
653     THEN
654       b5 := G_LINE_UNINVOICED_FREIGHT;
655 
656       -----added for the Returns project
657       IF p_credit_check_rule_rec.include_returns_flag = 'Y'
658       THEN
659         b28:= G_LINE_RETURN_UNINV_FREIGHT;
660       END IF;
661 
662       IF p_credit_check_rule_rec.orders_on_hold_flag  =  'N'
663       THEN
664         b16 := G_LINE_FREIGHT_HOLDS;
665 
666         -----added for the Returns project
667         IF p_credit_check_rule_rec.include_returns_flag = 'Y'
668         THEN
669           b35:= G_LINE_RETURN_FREIGHT_HOLDS;
670         END IF;
671 
672       END IF;
673     ELSE
674       b6 := G_HEADER_UNINVOICED_FREIGHT;
675 
676       -----added for the Returns project
677       IF p_credit_check_rule_rec.include_returns_flag = 'Y'
678       THEN
679         b27:= G_HEAD_RETURN_UNINV_FREIGHT;
680       END IF;
681 
682      IF p_credit_check_rule_rec.orders_on_hold_flag  =  'N'
683      THEN
684         b15 := G_ORDER_FREIGHT_HOLDS;
685 
686         -----added for the Returns project
687         IF p_credit_check_rule_rec.include_returns_flag = 'Y'
688         THEN
689           b34:= G_ORDER_RETURN_FREIGHT_HOLDS;
690         END IF;
691 
692       END IF;
693     END IF;
694 
695     b7 := G_HEADER_AND_LINE_FREIGHT;
696 
697     -----added for the Returns project
698     IF p_credit_check_rule_rec.include_returns_flag = 'Y'
699     THEN
700       b29:= G_HEAD_LINE_RETURN_FREIGHT;
701     END IF;
702 
703     IF p_credit_check_rule_rec.orders_on_hold_flag  =  'N'
704     THEN
705       b17 := G_HEADER_LINE_FREIGHT_HOLDS;
706 
707       -----added for the Returns project
708       IF p_credit_check_rule_rec.include_returns_flag = 'Y'
709       THEN
710         b36:= G_H_L_RETURN_FREIGHT_HOLDS;
711       END IF;
712 
713     END IF;
714   END IF;
715 
716   -- Set value for external exposure
717   IF  p_credit_check_rule_rec.include_external_exposure_flag  =  'Y'  THEN
718     b18 := G_EXTERNAL_EXPOSURE;
719   END IF;
720 
721   IF  p_credit_check_rule_rec.include_payments_at_risk_flag  =  'Y'  THEN
722     b9 := G_PAYMENTS_AT_RISK;
723 
724     IF p_credit_check_rule_rec.open_ar_days is NULL
725     THEN
726       b22 := G_BR_PAYMENTS_AT_RISK ;
727     END IF;
728 
729   END IF;
730   IF  p_credit_check_rule_rec.open_ar_balance_flag  =  'Y'  THEN
731     b8 := G_INVOICES;
732 
733     IF p_credit_check_rule_rec.open_ar_days is NULL
734     THEN
735       b21 := G_BR_INVOICES ;
736     END IF;
737   END IF;
738 
739 
740   IF G_debug_flag = 'Y'
741   THEN
742     oe_debug_pub.add
743   (b1 || ' ' || b2 || ' ' || b3 || ' ' || b4 || ' ' || b5 || ' ' || b6 || ' '
744    || b7 || ' ' || b8 || ' ' || b9 || ' ' || b10 || ' ' || b11 || ' ' || b12 ||
745 ' '
746    || b13 || ' ' || b14 || ' ' || b15 || ' ' || b16 || ' ' || b17 || ' ' || b18||
747    ' '|| b23 || ' ' || b24 || ' ' || b25 || ' ' || b26 || ' ' || b27 || ' ' || b28||
748    ' '|| b29 || ' ' || b30 || ' ' || b31 || ' ' || b32 || ' ' || b33 || ' ' || b34||
749    ' '|| b35 || ' ' || b36,
750  2 );
751 
752     oe_debug_pub.add( ' Out from balance_types_om_and_ar ');
753   END IF;
754 
755 END balance_types_om_and_ar ;
756 
757 -----------------------------------------------------------
758 
759 -----------------------------------------------------------
760 -- This procedure will set the balance types for the
761 -- OM exposure based on the credit check rules setup
762 --  This procedure will also set the external exposure
763 -- balance types
764 -----------------------------------------------------------
765 
766 PROCEDURE balance_types_om_nohold
767 (p_credit_check_rule_rec   IN    OE_CREDIT_CHECK_UTIL.oe_credit_rules_rec_type
768 )
769 IS
770 BEGIN
771   b1 := -1;
772   b2 := -1;
773   b3 := -1;
774   b4 := -1;
775   b5 := -1;
776   b6 := -1;
777   b7 := -1;
778   b8 := -1;
779   b9 := -1;
780   b10 := -1;
781   b11 := -1;
782   b12 := -1;
783   b13 := -1;
784   b14 := -1;
785   b15 := -1;
786   b16 := -1;
787   b17 := -1;
788   b18 := -1;
789   b21 := -1;
790   b22 := -1;
791 
792 ----added for the RETURNS
793   b23 := -1;
794   b24 := -1;
795   b25 := -1;
796   b26 := -1;
797   b27 := -1;
798   b28 := -1;
799   b29 := -1;
800   b30 := -1;
801   b31 := -1;
802   b32 := -1;
803   b33 := -1;
804   b34 := -1;
805   b35 := -1;
806   b36 := -1;
807 
808 
809   -- Determine which balance types to select:
810   -- Chosing a balance type is indicated by setting the corresponding
811   -- variable to a balance type global constant
812   -- (e.g. b4 := G_ORDER_HOLDS)
813 
814 
815   IF  p_credit_check_rule_rec.uninvoiced_orders_flag     =  'Y'  THEN
816     IF p_credit_check_rule_rec.credit_check_level_code  = 'LINE' THEN
817       b3 := G_LINE_UNINVOICED_ORDERS;
818 
819       -----added for the Returns project
820       IF p_credit_check_rule_rec.include_returns_flag = 'Y'
821       THEN
822         b24 := G_LINE_RETURN_UNINV_ORDERS;
823       END IF;
824     ELSE
825       b1 := G_HEADER_UNINVOICED_ORDERS;
826 
827       -----added for the Returns project
828       IF p_credit_check_rule_rec.include_returns_flag = 'Y'
829       THEN
830         b23 := G_HEAD_RETURN_UNINV_ORDERS;
831       END IF;
832     END IF;
833   END IF;
834 
835 
836   IF  p_credit_check_rule_rec.include_tax_flag           =  'Y'  THEN
837     IF p_credit_check_rule_rec.credit_check_level_code  = 'LINE' THEN
838       b4 := G_LINE_UNINVOICED_ORDERS_TAX;
839 
840       -----added for the Returns project
841       IF p_credit_check_rule_rec.include_returns_flag = 'Y'
842       THEN
843         b26:= G_LINE_RETURN_UNINV_ORD_TAX;
844       END IF;
845     ELSE
846       b2 := G_HEADER_UNINVOICED_ORDERS_TAX;
847 
848       -----added for the Returns project
849       IF p_credit_check_rule_rec.include_returns_flag = 'Y'
850       THEN
851         b25:= G_HEAD_RETURN_UNINV_ORD_TAX;
852       END IF;
853 
854     END IF;
855   END IF;
856 
857   IF  p_credit_check_rule_rec.incl_freight_charges_flag  =  'Y'  THEN
858     IF p_credit_check_rule_rec.credit_check_level_code  = 'LINE' THEN
859       b5 := G_LINE_UNINVOICED_FREIGHT;
860 
861       -----added for the Returns project
862       IF p_credit_check_rule_rec.include_returns_flag = 'Y'
863       THEN
864         b28:= G_LINE_RETURN_UNINV_FREIGHT;
865       END IF;
866 
867     ELSE
868       b6 := G_HEADER_UNINVOICED_FREIGHT;
869 
870       -----added for the Returns project
871       IF p_credit_check_rule_rec.include_returns_flag = 'Y'
872       THEN
873         b27:= G_HEAD_RETURN_UNINV_FREIGHT;
874       END IF;
875 
876     END IF;
877     b7 := G_HEADER_AND_LINE_FREIGHT;
878 
879     -----added for the Returns project
880     IF p_credit_check_rule_rec.include_returns_flag = 'Y'
881     THEN
882       b29:= G_HEAD_LINE_RETURN_FREIGHT;
883     END IF;
884 
885   END IF;
886 
887   IF G_debug_flag = 'Y'
888   THEN
889     oe_debug_pub.add
890     (b1 || ' ' || b2 || ' ' || b3 || ' ' || b4 || ' ' || b5 || ' ' || b6 || ' '
891    || b7 || ' ' || b8 || ' ' || b9 || ' ' || b10 || ' ' || b11 || ' ' || b12 || ' '
892    || b13 || ' ' || b14 || ' ' || b15 || ' ' || b16 || ' ' || b17 || ' ' || b18||' '
893    || b23 || ' ' || b24 || ' ' || b25 || ' ' || b26 || ' ' || b27 || ' ' || b28||' '||b29, 2 );
894 
895     oe_debug_pub.add( ' Out from balance_types_om_nohold ' );
896   END IF;
897 END balance_types_om_nohold;
898 ---------------------------------------------------------------
899 
900 PROCEDURE balance_types_om_hold
901 (p_credit_check_rule_rec   IN    OE_CREDIT_CHECK_UTIL.oe_credit_rules_rec_type
902 )
903 IS
904 BEGIN
905   b1 := -1;
906   b2 := -1;
907   b3 := -1;
908   b4 := -1;
909   b5 := -1;
910   b6 := -1;
911   b7 := -1;
912   b8 := -1;
913   b9 := -1;
914   b10 := -1;
915   b11 := -1;
916   b12 := -1;
917   b13 := -1;
918   b14 := -1;
919   b15 := -1;
920   b16 := -1;
921   b17 := -1;
922   b18 := -1;
923   b21 := -1;
924   b22 := -1;
925 ----added for the RETURNS
926   b23 := -1;
927   b24 := -1;
928   b25 := -1;
929   b26 := -1;
930   b27 := -1;
931   b28 := -1;
932   b29 := -1;
933   b30 := -1;
934   b31 := -1;
935   b32 := -1;
936   b33 := -1;
937   b34 := -1;
938   b35 := -1;
939   b36 := -1;
940 
941 
942   -- Determine which balance types to select:
943   -- Chosing a balance type is indicated by setting the corresponding
944   -- variable to a balance type global constant
945   -- (e.g. b4 := G_ORDER_HOLDS)
946 
947   IF  p_credit_check_rule_rec.uninvoiced_orders_flag     =  'Y'  THEN
948     IF p_credit_check_rule_rec.credit_check_level_code  = 'LINE' THEN
949       IF p_credit_check_rule_rec.orders_on_hold_flag  =  'N'  THEN
950         b11 := G_LINE_HOLDS;
951 
952         -----added for the Returns project
953         IF p_credit_check_rule_rec.include_returns_flag = 'Y'
954         THEN
955           b31 := G_LINE_RETURN_HOLDS;
956         END IF;
957 
958       END IF;
959     ELSE
960       IF p_credit_check_rule_rec.orders_on_hold_flag  =  'N'  THEN
961         b10 := G_ORDER_HOLDS;
962 
963         -----added for the Returns project
964         IF p_credit_check_rule_rec.include_returns_flag = 'Y'
965         THEN
966           b30 := G_ORDER_RETURN_HOLDS;
967         END IF;
968 
969       END IF;
970     END IF;
971   END IF;
972 
973   IF  p_credit_check_rule_rec.include_tax_flag           =  'Y'  THEN
974     IF p_credit_check_rule_rec.credit_check_level_code  = 'LINE' THEN
975       IF p_credit_check_rule_rec.orders_on_hold_flag  =  'N'  THEN
976         b14 := G_LINE_TAX_HOLDS;
977 
978         -----added for the Returns project
979         IF p_credit_check_rule_rec.include_returns_flag = 'Y'
980         THEN
981           b33:= G_LINE_RETURN_TAX_HOLDS;
982         END IF;
983 
984       END IF;
985     ELSE
986       IF p_credit_check_rule_rec.orders_on_hold_flag  =  'N'  THEN
987 	b13 := G_ORDER_TAX_HOLDS;
988 
989         -----added for the Returns project
990         IF p_credit_check_rule_rec.include_returns_flag = 'Y'
991         THEN
992           b32:= G_ORDER_RETURN_TAX_HOLDS;
993         END IF;
994 
995       END IF;
996     END IF;
997   END IF;
998 
999   IF  p_credit_check_rule_rec.incl_freight_charges_flag  =  'Y'  THEN
1000     IF p_credit_check_rule_rec.credit_check_level_code  = 'LINE' THEN
1001       IF p_credit_check_rule_rec.orders_on_hold_flag  =  'N'  THEN
1002 	b16 := G_LINE_FREIGHT_HOLDS;
1003 
1004         -----added for the Returns project
1005         IF p_credit_check_rule_rec.include_returns_flag = 'Y'
1006         THEN
1007           b35:= G_LINE_RETURN_FREIGHT_HOLDS;
1008         END IF;
1009 
1010       END IF;
1011     ELSE
1012       IF p_credit_check_rule_rec.orders_on_hold_flag  =  'N'  THEN
1013         b15 := G_ORDER_FREIGHT_HOLDS;
1014 
1015         -----added for the Returns project
1016         IF p_credit_check_rule_rec.include_returns_flag = 'Y'
1017         THEN
1018           b34:= G_ORDER_RETURN_FREIGHT_HOLDS;
1019         END IF;
1020 
1021       END IF;
1022     END IF;
1023 
1024     IF p_credit_check_rule_rec.orders_on_hold_flag  =  'N'  THEN
1025       b17 := G_HEADER_LINE_FREIGHT_HOLDS;
1026 
1027       -----added for the Returns project
1028       IF p_credit_check_rule_rec.include_returns_flag = 'Y'
1029       THEN
1030         b36:= G_H_L_RETURN_FREIGHT_HOLDS;
1031       END IF;
1032 
1033     END IF;
1034   END IF;
1035 
1036   IF G_debug_flag = 'Y'
1037   THEN
1038     oe_debug_pub.add
1039   (b1 || ' ' || b2 || ' ' || b3 || ' ' || b4 || ' ' || b5 || ' ' || b6 || ' '
1040    || b7 || ' ' || b8 || ' ' || b9 || ' ' || b10 || ' ' || b11 || ' ' || b12 || ' '
1041    || b13 || ' ' || b14 || ' ' || b15 || ' ' || b16 || ' ' || b17 || ' ' || b18|| ' '
1042    || b30 || ' ' || b31 || ' ' || b32 || ' ' || b33 || ' ' || b34 || ' ' || b35|| ' '
1043    || b36, 2 );
1044 
1045     oe_debug_pub.add( ' Out from balance_types_om_hold ' );
1046   END IF;
1047 END balance_types_om_hold;
1048 
1049 PROCEDURE balance_types_om
1050 ( p_credit_check_rule_rec   IN    OE_CREDIT_CHECK_UTIL.oe_credit_rules_rec_type
1051 )
1052 IS
1053 BEGIN
1054   b1 := -1;
1055   b2 := -1;
1056   b3 := -1;
1057   b4 := -1;
1058   b5 := -1;
1059   b6 := -1;
1060   b7 := -1;
1061   b8 := -1;
1062   b9 := -1;
1063   b10 := -1;
1064   b11 := -1;
1065   b12 := -1;
1066   b13 := -1;
1067   b14 := -1;
1068   b15 := -1;
1069   b16 := -1;
1070   b17 := -1;
1071   b18 := -1;
1072   b21 := -1;
1073   b22 := -1;
1074 
1075 ----added for the RETURNS
1076   b23 := -1;
1077   b24 := -1;
1078   b25 := -1;
1079   b26 := -1;
1080   b27 := -1;
1081   b28 := -1;
1082   b29 := -1;
1083   b30 := -1;
1084   b31 := -1;
1085   b32 := -1;
1086   b33 := -1;
1087   b34 := -1;
1088   b35 := -1;
1089   b36 := -1;
1090 
1091 
1092   -- Determine which balance types to select:
1093   -- Chosing a balance type is indicated by setting the corresponding
1094   -- variable to a balance type global constant
1095   -- (e.g. b4 := G_ORDER_HOLDS)
1096 
1097   IF  p_credit_check_rule_rec.uninvoiced_orders_flag     =  'Y'  THEN
1098     IF p_credit_check_rule_rec.credit_check_level_code  = 'LINE' THEN
1099       b3 := G_LINE_UNINVOICED_ORDERS;
1100 
1101       -----added for the Returns project
1102       IF p_credit_check_rule_rec.include_returns_flag = 'Y'
1103       THEN
1104         b24 := G_LINE_RETURN_UNINV_ORDERS;
1105       END IF;
1106 
1107       IF p_credit_check_rule_rec.orders_on_hold_flag  =  'N'  THEN
1108         b11 := G_LINE_HOLDS;
1109 
1110         -----added for the Returns project
1111         IF p_credit_check_rule_rec.include_returns_flag = 'Y'
1112         THEN
1113           b31 := G_LINE_RETURN_HOLDS;
1114         END IF;
1115 
1116       END IF;
1117     ELSE
1118       b1 := G_HEADER_UNINVOICED_ORDERS;
1119 
1120       -----added for the Returns project
1121       IF p_credit_check_rule_rec.include_returns_flag = 'Y'
1122       THEN
1123         b23 := G_HEAD_RETURN_UNINV_ORDERS;
1124       END IF;
1125 
1126       IF p_credit_check_rule_rec.orders_on_hold_flag  =  'N'
1127       THEN
1128         b10 := G_ORDER_HOLDS;
1129 
1130         -----added for the Returns project
1131         IF p_credit_check_rule_rec.include_returns_flag = 'Y'
1132         THEN
1133           b30 := G_ORDER_RETURN_HOLDS;
1134         END IF;
1135 
1136       END IF;
1137     END IF;
1138   END IF;
1139   IF  p_credit_check_rule_rec.include_tax_flag           =  'Y'
1140   THEN
1141     IF p_credit_check_rule_rec.credit_check_level_code  = 'LINE'
1142     THEN
1143       b4 := G_LINE_UNINVOICED_ORDERS_TAX;
1144 
1145       -----added for the Returns project
1146       IF p_credit_check_rule_rec.include_returns_flag = 'Y'
1147       THEN
1148         b26:= G_LINE_RETURN_UNINV_ORD_TAX;
1149       END IF;
1150 
1151       IF p_credit_check_rule_rec.orders_on_hold_flag  =  'N'
1152       THEN
1153         b14 := G_LINE_TAX_HOLDS;
1154 
1155         -----added for the Returns project
1156         IF p_credit_check_rule_rec.include_returns_flag = 'Y'
1157         THEN
1158           b33:= G_LINE_RETURN_TAX_HOLDS;
1159         END IF;
1160 
1161       END IF;
1162     ELSE
1163       b2 := G_HEADER_UNINVOICED_ORDERS_TAX;
1164 
1165       -----added for the Returns project
1166       IF p_credit_check_rule_rec.include_returns_flag = 'Y'
1167       THEN
1168         b25:= G_HEAD_RETURN_UNINV_ORD_TAX;
1169       END IF;
1170 
1171       IF p_credit_check_rule_rec.orders_on_hold_flag  =  'N'
1172       THEN
1173 	b13 := G_ORDER_TAX_HOLDS;
1174 
1175         -----added for the Returns project
1176         IF p_credit_check_rule_rec.include_returns_flag = 'Y'
1177         THEN
1178           b32:= G_ORDER_RETURN_TAX_HOLDS;
1179         END IF;
1180 
1181       END IF;
1182     END IF;
1183   END IF;
1184   IF  p_credit_check_rule_rec.incl_freight_charges_flag  =  'Y'
1185   THEN
1186     IF p_credit_check_rule_rec.credit_check_level_code  = 'LINE'
1187     THEN
1188       b5 := G_LINE_UNINVOICED_FREIGHT;
1189 
1190       -----added for the Returns project
1191       IF p_credit_check_rule_rec.include_returns_flag = 'Y'
1192       THEN
1193         b28:= G_LINE_RETURN_UNINV_FREIGHT;
1194       END IF;
1195 
1196       IF p_credit_check_rule_rec.orders_on_hold_flag  =  'N'
1197       THEN
1198 	b16 := G_LINE_FREIGHT_HOLDS;
1199 
1200         -----added for the Returns project
1201         IF p_credit_check_rule_rec.include_returns_flag = 'Y'
1202         THEN
1203           b35:= G_LINE_RETURN_FREIGHT_HOLDS;
1204         END IF;
1205 
1206       END IF;
1207     ELSE
1208       b6 := G_HEADER_UNINVOICED_FREIGHT;
1209 
1210       -----added for the Returns project
1211       IF p_credit_check_rule_rec.include_returns_flag = 'Y'
1212       THEN
1213         b27:= G_HEAD_RETURN_UNINV_FREIGHT;
1214       END IF;
1215 
1216       IF p_credit_check_rule_rec.orders_on_hold_flag  =  'N'
1217       THEN
1218         b15 := G_ORDER_FREIGHT_HOLDS;
1219 
1220         -----added for the Returns project
1221         IF p_credit_check_rule_rec.include_returns_flag = 'Y'
1222         THEN
1223           b34:= G_ORDER_RETURN_FREIGHT_HOLDS;
1224         END IF;
1225 
1226       END IF;
1227     END IF;
1228     b7 := G_HEADER_AND_LINE_FREIGHT;
1229 
1230     -----added for the Returns project
1231     IF p_credit_check_rule_rec.include_returns_flag = 'Y'
1232     THEN
1233       b29:= G_HEAD_LINE_RETURN_FREIGHT;
1234     END IF;
1235 
1236     IF p_credit_check_rule_rec.orders_on_hold_flag  =  'N'
1237     THEN
1238       b17 := G_HEADER_LINE_FREIGHT_HOLDS;
1239 
1240       -----added for the Returns project
1241       IF p_credit_check_rule_rec.include_returns_flag = 'Y'
1242       THEN
1243         b36:= G_H_L_RETURN_FREIGHT_HOLDS;
1244       END IF;
1245 
1246     END IF;
1247   END IF;
1248 
1249   -- Set value for external exposure
1250   IF  p_credit_check_rule_rec.include_external_exposure_flag  =  'Y'
1251   THEN
1252     b18 := G_EXTERNAL_EXPOSURE;
1253   END IF;
1254 
1255   IF G_debug_flag = 'Y'
1256   THEN
1257     oe_debug_pub.add
1258   (b1 || ' ' || b2 || ' ' || b3 || ' ' || b4 || ' ' || b5 || ' ' || b6 || ' '
1259    || b7 || ' ' || b8 || ' ' || b9 || ' ' || b10 || ' ' || b11 || ' ' || b12 || ' '
1260    || b13 || ' ' || b14 || ' ' || b15 || ' ' || b16 || ' ' || b17 || ' ' || b18 || ' '
1261    || b23 || ' ' || b24 || ' ' || b25 || ' ' || b26 || ' ' || b27 || ' ' || b28|| ' '
1262    || b29 || ' ' || b30 || ' ' || b31 || ' ' || b32 || ' ' || b33 || ' ' || b34|| ' '
1263    || b35 || ' ' || b36, 2 );
1264 
1265     oe_debug_pub.add( ' Out from balance_types_om ' );
1266   END IF;
1267 END balance_types_om;
1268 
1269 -----------------------------------------------------------
1270 -- This procedure will set the balance types for the
1271 -- AR exposure based on the credit check rules setup
1272 ----------------------------------------------------------
1273 PROCEDURE balance_types_ar
1274 ( p_credit_check_rule_rec   IN    OE_CREDIT_CHECK_UTIL.oe_credit_rules_rec_type
1275 )
1276 IS
1277 BEGIN
1278   b1 := -1;
1279   b2 := -1;
1280   b3 := -1;
1281   b4 := -1;
1282   b5 := -1;
1283   b6 := -1;
1284   b7 := -1;
1285   b8 := -1;
1286   b9 := -1;
1287   b10 := -1;
1288   b11 := -1;
1289   b12 := -1;
1290   b13 := -1;
1291   b14 := -1;
1292   b15 := -1;
1293   b16 := -1;
1294   b17 := -1;
1295   b18 := -1;
1296   b21 := -1 ;
1297   b22 := -1;
1298 
1299 ----added for the RETURNS
1300   b23 := -1;
1301   b24 := -1;
1302   b25 := -1;
1303   b26 := -1;
1304   b27 := -1;
1305   b28 := -1;
1306   b29 := -1;
1307   b30 := -1;
1308   b31 := -1;
1309   b32 := -1;
1310   b33 := -1;
1311   b34 := -1;
1312   b35 := -1;
1313   b36 := -1;
1314 
1315   IF  p_credit_check_rule_rec.include_payments_at_risk_flag  =  'Y'  THEN
1316     b9 := G_PAYMENTS_AT_RISK;
1317 
1318     IF p_credit_check_rule_rec.open_ar_days is NULL
1319     THEN
1320       b22 := G_BR_PAYMENTS_AT_RISK ;
1321     END IF;
1322 
1323   END IF;
1324   IF  p_credit_check_rule_rec.open_ar_balance_flag  =  'Y'  THEN
1325     b8 := G_INVOICES;
1326 
1327     IF p_credit_check_rule_rec.open_ar_days is NULL
1328     THEN
1329       b21 := G_BR_INVOICES ;
1330     END IF;
1331   END IF;
1332 
1333 
1334   IF G_debug_flag = 'Y'
1335   THEN
1336     oe_debug_pub.add
1337     (b1 || ' ' || b2 || ' ' || b3 || ' ' || b4 || ' ' || b5 || ' ' || b6 || ' '
1338         || b7 || ' ' || b8 || ' ' || b9 || ' ' || b10 || ' ' || b11 || ' ' || b12 || ' '
1339         || b13 || ' ' || b14 || ' ' || b15 || ' ' || b16 || ' ' || b17 || ' ' || b18 || ' ' || b21 || ' ' ||b22,2 );
1340 
1341     oe_debug_pub.add( ' Out from balance_types_ar ');
1342   END IF;
1343 
1344 END balance_types_ar;
1345 
1346 
1347 ------------------------------------------------------------------
1348 -- COMMENTS: This procedure will calculate all the
1349 --           buckets that are required to be summed
1350 --           to arive at the exposure balance
1351 --           The driving date for the calculate buckets
1352 --           will be the Horizon date ( both OM and AR )
1353 --          as the Main starting Bucket will be the largest
1354 --          bucket that includes this Horizon date
1355 --          Once the Main Bucket is calculated, all the
1356 --          Buckets to the LEFT of the Main Bucket is
1357 --         SUMMED UP.
1358 --         Now inside the Main Bucket, the remaining Buckets
1359 --         constitute the Fractional Amount and this
1360 --         procedure wil identify those Buckets as well.
1361 
1362 --        p_interval parameter is used to identify the cases
1363 --        where the credit check rule was not setup to use
1364 --        horizon days.
1365 --        In such cases, the exposure can be recovered by
1366 --        SUMMING overal exposure for all the largest
1367 --        buckets , as we need to use all the data
1368 -----------------------------------------------------------------
1369 PROCEDURE calculate_buckets
1370 ( p_date         IN   NUMBER
1371 , p_interval     IN   NUMBER
1372 , x_main_bucket  OUT  NOCOPY NUMBER
1373 , x_binary_tbl   OUT  NOCOPY oe_credit_exposure_pvt.Binary_tbl_type
1374 )
1375 IS
1376   l_date                NUMBER;
1377   l_level               NUMBER;
1378   l_bucket_length       NUMBER;
1379   l_bucket              NUMBER;
1380   l_main_bucket_length  NUMBER;
1381   i                     BINARY_INTEGER;
1382 BEGIN
1383   l_date  := p_date;
1384 
1385   IF G_debug_flag = 'Y'
1386   THEN
1387     oe_debug_pub.add( 'Into calculate_buckets ');
1388     oe_debug_pub.add( 'p_date =>'|| p_date );
1389     oe_debug_pub.add( 'p_interval =>' || p_interval );
1390   END IF;
1391 
1392   -- the Main Bucket
1393   l_level             :=  G_MAX_BUCKET_LEVEL;
1394   l_bucket_length     := POWER( 2, l_level );
1395   l_bucket            := l_date  -  MOD( l_date, l_bucket_length );
1396 
1397   l_main_bucket_length := l_bucket_length;
1398   x_main_bucket        := l_bucket;
1399 
1400   IF G_debug_flag = 'Y'
1401   THEN
1402     oe_debug_pub.add( 'l_date ==> '|| l_date);
1403     oe_debug_pub.add( 'l_bucket_length ==> '|| l_bucket_length );
1404     oe_debug_pub.add( 'Main Bucket ==> '|| x_main_bucket ,1);
1405   END IF;
1406 
1407   IF p_interval is NOT NULL
1408   THEN
1409     -- The Buckets need to be identified as the Horizon interval
1410     -- is included.
1411     BEGIN
1412   -- the fractional amount of the Main Bucket
1413   -- Start from the Main Bucket and navigate to
1414   -- the right of the bucket
1415  --  by decreasing the level for each iteration and thereby
1416   -- reducing the bucket lengths as well as the bucket lengths as
1417   -- well. Include the buckets where the buckets comprise
1418   -- data for the dates <= than the horizon date and continue from
1419   -- there
1420 
1421      WHILE  l_bucket  - 1  <>  l_date
1422      LOOP
1423       IF l_bucket + l_bucket_length - 1  <=  l_date
1424       THEN
1425       --  include Bucket
1426 
1427           x_binary_tbl( l_level ).bucket        := l_bucket;
1428           x_binary_tbl( l_level ).bucket_length := l_bucket_length;
1429 
1430          l_bucket  := l_bucket + l_bucket_length;
1431 
1432       END IF;
1433 
1434       l_level  := l_level - 1;
1435       l_bucket_length  := l_bucket_length / 2;
1436 
1437      END LOOP;
1438 
1439      IF G_debug_flag = 'Y'
1440      THEN
1441        oe_debug_pub.add( 'Out of the Main LOOP');
1442        oe_debug_pub.add( '-------------------------------------- ');
1443        oe_debug_pub.add( 'selected buckets: ' );
1444      END IF;
1445 
1446       i  :=  x_binary_tbl.FIRST;
1447       WHILE i IS NOT NULL LOOP
1448 
1449      IF G_debug_flag = 'Y'
1450      THEN
1451       oe_debug_pub.add
1452       ( 'Bucket Number: ' || x_binary_tbl(i).bucket || ', '
1453           || 'Bucket Length:' || x_binary_tbl(i).bucket_length, 2 );
1454      END IF;
1455 
1456         i :=  x_binary_tbl.NEXT(i);
1457       END LOOP;
1458 
1459       oe_debug_pub.add( '-------------------------------------- ');
1460 
1461     END;
1462    ELSE
1463     oe_debug_pub.add( 'No need to calculate Buckets, no Horizon  ');
1464    END IF;
1465 
1466   IF G_debug_flag = 'Y'
1467   THEN
1468     oe_debug_pub.add( 'Out calculate_buckets ');
1469   END IF;
1470 
1471 
1472 END calculate_buckets;
1473 
1474 
1475 
1476 -----------------------------------------------------------------
1477 --- API to convert the amount using GL currency API
1478 -- with Traingulation
1479 ----------------------------------------------------------------
1480 FUNCTION convert_currency_amount
1481 ( p_amount                   IN   NUMBER
1482 , p_transactional_currency   IN   VARCHAR2
1483 , p_limit_currency           IN   VARCHAR2
1484 )
1485 RETURN NUMBER
1486 IS
1487   l_term     NUMBER;
1488 BEGIN
1489   BEGIN
1490     l_term :=
1491       OE_CREDIT_CHECK_UTIL.convert_currency_amount
1492       ( p_amount                  => p_amount
1493       , p_transactional_currency  => p_transactional_currency
1494       , p_limit_currency          => p_limit_currency
1495       , p_functional_currency     => g_functional_currency
1496       , p_conversion_date         => SYSDATE
1497       , p_conversion_type         => g_conversion_type
1498       );
1499   EXCEPTION
1500     WHEN  GL_CURRENCY_API.NO_RATE  OR
1501      GL_CURRENCY_API.INVALID_CURRENCY
1502     THEN
1503      oe_debug_pub.add( 'conversion exception for '
1504              || p_transactional_currency, 1 );
1505       add_error_currency( g_error_curr_tbl, p_transactional_currency );
1506   END;
1507 
1508   RETURN NVL( l_term, 0 );
1509 
1510 END convert_currency_amount;
1511 
1512 ----------------------------------------------------------
1513 ---- COMMENTS: Fucntion to select the overall exposure from the
1514 --             OE_CREDIT_SUMMARIES table for the balance types
1515 --             This function will be called from the main
1516 --              GET_EXPOSURE procedure both for AR and OM exposure
1517 --             as the exposure balance is governed by the
1518 --             balance types used
1519 --------------------------------------------------------------
1520 FUNCTION retrieve_exposure
1521 ( p_binary_tbl           IN  oe_credit_exposure_pvt.Binary_tbl_type
1522 , p_site_use_id          IN  NUMBER
1523 , p_customer_id          IN  NUMBER
1524 , p_party_id             IN  NUMBER
1525 , p_org_id               IN  NUMBER
1526 , p_include_all_flag     IN  VARCHAR2
1527 , p_usage_curr_tbl       IN  OE_CREDIT_CHECK_UTIL.curr_tbl_type
1528 , p_limit_curr_code      IN  HZ_CREDIT_PROFILE_AMTS.currency_code%TYPE
1529 , p_main_bucket          IN  NUMBER
1530 , p_global_exposure_flag IN  VARCHAR2
1531 , p_credit_check_rule_rec IN
1532      OE_CREDIT_CHECK_UTIL.oe_credit_rules_rec_type
1533 , x_error_curr_tbl       IN  OUT NOCOPY  OE_CREDIT_CHECK_UTIL.curr_tbl_type
1534 )
1535 RETURN NUMBER
1536 IS
1537   i                  BINARY_INTEGER;
1538   l_site_use_id      NUMBER;
1539   l_br_site_use_id   NUMBER;
1540   l_currency_code HZ_CREDIT_PROFILE_AMTS.currency_code%TYPE;
1541 
1542   l_balance          NUMBER := 0 ;
1543   l_br_balance       NUMBER := 0 ;
1544   l_term             NUMBER := 0 ;
1545   l_total            NUMBER := 0 ;
1546 
1547   l_bucket          NUMBER;
1548   l_bucket_length   NUMBER;
1549   j                 NUMBER;
1550 
1551   l_br_bucket          NUMBER;
1552   l_br_bucket_length   NUMBER;
1553 
1554   CURSOR site_balance_csr IS
1555   SELECT SUM( balance )
1556   FROM   oe_credit_summaries
1557   WHERE  balance_type  IN
1558  	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
1559 	  b10, b11, b12, b13, b14, b15, b16, b17, b18)
1560   AND  site_use_id           =  l_site_use_id
1561   AND  currency_code         =  l_currency_code
1562   AND  bucket                =  l_bucket
1563   AND  bucket_duration       =  l_bucket_length
1564   ;
1565 
1566   ---added for the Returns project
1567   ---the same as above including Returns
1568   CURSOR site_balance_ret_csr IS
1569   SELECT NVL(SUM( balance ),0)
1570   FROM   oe_credit_summaries
1571   WHERE  balance_type  IN
1572  	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
1573 	  b10, b11, b12, b13, b14, b15, b16, b17, b18, b23, b24,
1574         b25, b26, b27, b28, b29, b30, b31, b32, b33, b34, b35, b36)
1575   AND  site_use_id           =  l_site_use_id
1576   AND  currency_code         =  l_currency_code
1577   AND  bucket                =  l_bucket
1578   AND  bucket_duration       =  l_bucket_length;
1579 
1580 
1581   CURSOR site_balance_stub_csr IS
1582   SELECT SUM( balance )
1583   FROM   oe_credit_summaries
1584   WHERE  balance_type  IN
1585 	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
1586 	  b10, b11, b12, b13, b14, b15, b16, b17, b18)
1587   AND  site_use_id           =  l_site_use_id
1588   AND  currency_code         =  l_currency_code
1589   AND  bucket                <  l_bucket
1590   AND  bucket_duration       =  l_bucket_length
1591   ;
1592 
1593   ---added for the Returns project
1594   ---the same as above including Returns
1595   CURSOR site_balance_stub_ret_csr IS
1596   SELECT SUM( balance )
1597   FROM   oe_credit_summaries
1598   WHERE  balance_type  IN
1599 	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
1600 	  b10, b11, b12, b13, b14, b15, b16, b17, b18, b23, b24,
1601         b25, b26, b27, b28, b29, b30, b31, b32, b33, b34, b35, b36)
1602   AND  site_use_id           =  l_site_use_id
1603   AND  currency_code         =  l_currency_code
1604   AND  bucket                <  l_bucket
1605   AND  bucket_duration       =  l_bucket_length;
1606 
1607 
1608   CURSOR site_br_stub_csr IS
1609   SELECT SUM( balance )
1610   FROM   oe_credit_summaries
1611   WHERE  balance_type  IN
1612           (21,22)
1613   AND  site_use_id           =  l_br_site_use_id
1614   AND  currency_code         =  l_currency_code
1615   AND  bucket                <  l_br_bucket
1616   AND bucket_duration        =  l_br_bucket_length
1617   ;
1618 
1619 
1620   CURSOR customer_balance_csr IS
1621   SELECT SUM( balance )
1622   FROM   oe_credit_summaries
1623   WHERE  balance_type  IN
1624 	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
1625           b10, b11, b12, b13, b14, b15, b16, b17, b18, b21,b22)
1626   AND  cust_account_id       =  p_customer_id
1627   AND  ((org_id              =  p_org_id)
1628          OR
1629         (org_id IS NULL AND p_org_id IS NULL))
1630   AND  currency_code         =  l_currency_code
1631   AND  bucket                =  l_bucket
1632   AND bucket_duration        =  l_bucket_length
1633   ;
1634 
1635   ---added for the Returns project
1636   ---the same as above including Returns
1637   CURSOR customer_balance_ret_csr IS
1638   SELECT SUM( balance )
1639   FROM   oe_credit_summaries
1640   WHERE  balance_type  IN
1641 	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
1642           b10, b11, b12, b13, b14, b15, b16, b17, b18, b21,b22, b23, b24,
1643         b25, b26, b27, b28, b29, b30, b31, b32, b33, b34, b35, b36)
1644   AND  cust_account_id       =  p_customer_id
1645   AND  ((org_id              =  p_org_id)
1646          OR
1647         (org_id IS NULL AND p_org_id IS NULL))
1648   AND  currency_code         =  l_currency_code
1649   AND  bucket                =  l_bucket
1650   AND bucket_duration         =  l_bucket_length;
1651 
1652 
1653   CURSOR customer_balance_stub_csr IS
1654   SELECT SUM( balance )
1655   FROM   oe_credit_summaries
1656   WHERE  balance_type  IN
1657 	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
1658 	  b10, b11, b12, b13, b14, b15, b16, b17, b18, b21, b22)
1659   AND  cust_account_id       =  p_customer_id
1660   AND  ((org_id              =  p_org_id)
1661          OR
1662         (org_id IS NULL AND p_org_id IS NULL))
1663   AND  currency_code         =  l_currency_code
1664   AND  bucket                <  l_bucket
1665   AND bucket_duration         =  l_bucket_length
1666   ;
1667 
1668   ---added for the Returns project
1669   ---the same as above including Returns
1670   CURSOR customer_balance_stub_ret_csr IS
1671   SELECT SUM( balance )
1672   FROM   oe_credit_summaries
1673   WHERE  balance_type  IN
1674 	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
1675 	  b10, b11, b12, b13, b14, b15, b16, b17, b18, b21, b22, b23, b24,
1676         b25, b26, b27, b28, b29, b30, b31, b32, b33, b34, b35, b36)
1677   AND  cust_account_id       =  p_customer_id
1678   AND  ((org_id              =  p_org_id)
1679          OR
1680         (org_id IS NULL AND p_org_id IS NULL))
1681   AND  currency_code         =  l_currency_code
1682   AND  bucket                <  l_bucket
1683   AND bucket_duration         =  l_bucket_length
1684   ;
1685 
1686 
1687   CURSOR customer_br_stub_csr IS
1688   SELECT SUM( balance )
1689   FROM   oe_credit_summaries
1690   WHERE  balance_type  IN
1691           (21,22)
1692   AND  cust_account_id       =  p_customer_id
1693   AND  ((org_id              =  p_org_id)
1694          OR
1695         (org_id IS NULL AND p_org_id IS NULL))
1696   AND  currency_code         =  l_currency_code
1697   AND  bucket                <  l_br_bucket
1698   AND bucket_duration         =  l_br_bucket_length
1699   ;
1700 
1701 
1702   CURSOR cust_balance_csr_global IS
1703   SELECT SUM( balance )
1704   FROM   oe_credit_summaries
1705   WHERE  balance_type  IN
1706          (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10,
1707   	  b11, b12, b13, b14, b15, b16, b17, b18, b21, b22)
1708   AND  cust_account_id       =  p_customer_id
1709   AND  currency_code         =  l_currency_code
1710   AND  bucket                =  l_bucket
1711   AND bucket_duration         =  l_bucket_length
1712   ;
1713 
1714   ---added for the Returns project
1715   ---the same as above including Returns
1716 
1717   CURSOR cust_balance_ret_csr_global IS
1718   SELECT SUM( balance )
1719   FROM   oe_credit_summaries
1720   WHERE  balance_type  IN
1721          (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10,
1722   	  b11, b12, b13, b14, b15, b16, b17, b18, b21, b22,
1723           b23, b24, b25, b26, b27, b28, b29, b30, b31,
1724           b32, b33, b34, b35, b36)
1725   AND  cust_account_id       =  p_customer_id
1726   AND  currency_code         =  l_currency_code
1727   AND  bucket                =  l_bucket
1728   AND bucket_duration        =  l_bucket_length
1729   ;
1730 
1731   CURSOR cust_balance_stub_csr_global IS
1732   SELECT SUM( balance )
1733   FROM   oe_credit_summaries
1734   WHERE  balance_type  IN
1735          (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10,
1736 	  b11, b12, b13, b14, b15, b16, b17, b18, b21, b22)
1737   AND  cust_account_id       =  p_customer_id
1738   AND  currency_code         =  l_currency_code
1739   AND  bucket                <  l_bucket
1740   AND bucket_duration         =  l_bucket_length
1741   ;
1742 
1743   ---added for the Returns project
1744   ---the same as above including Returns
1745   CURSOR cust_bal_stub_ret_csr_global IS
1746   SELECT SUM( balance )
1747   FROM   oe_credit_summaries
1748   WHERE  balance_type  IN
1749          (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10,
1750 	  b11, b12, b13, b14, b15, b16, b17, b18, b21, b22,
1751           b23, b24, b25, b26, b27, b28, b29, b30, b31,
1752           b32, b33, b34, b35, b36)
1753   AND  cust_account_id       =  p_customer_id
1754   AND  currency_code         =  l_currency_code
1755   AND  bucket                <  l_bucket
1756   AND bucket_duration         =  l_bucket_length
1757   ;
1758 
1759   CURSOR cust_br_stub_csr_global IS
1760   SELECT SUM( balance )
1761   FROM   oe_credit_summaries
1762   WHERE  balance_type  IN
1763           (21,22)
1764   AND  cust_account_id       =  p_customer_id
1765   AND  currency_code         =  l_currency_code
1766   AND  bucket                <  l_br_bucket
1767   AND bucket_duration         =  l_br_bucket_length
1768   ;
1769 
1770 
1771 --------------------------------------------------------------
1772 
1773   CURSOR party_balance_csr_global IS
1774   SELECT SUM( balance )
1775   FROM   oe_credit_summaries
1776   WHERE  balance_type  IN
1777          (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10,
1778   	  b11, b12, b13, b14, b15, b16, b17, b18, b21, b22)
1779   AND  party_id              =  p_party_id
1780   AND  currency_code         =  l_currency_code
1781   AND  bucket                =  l_bucket
1782   AND bucket_duration         =  l_bucket_length
1783   ;
1784 
1785   ---added for the Returns project
1786   ---the same as above including Returns
1787 
1788   CURSOR party_balance_ret_csr_global IS
1789   SELECT SUM( balance )
1790   FROM   oe_credit_summaries
1791   WHERE  balance_type  IN
1792          (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10,
1793   	  b11, b12, b13, b14, b15, b16, b17, b18, b21, b22,
1794           b23, b24, b25, b26, b27, b28, b29, b30, b31,
1795           b32, b33, b34, b35, b36)
1796   AND  party_id              =  p_party_id
1797   AND  currency_code         =  l_currency_code
1798   AND  bucket                =  l_bucket
1799   AND bucket_duration         =  l_bucket_length
1800   ;
1801 
1802 
1803   CURSOR party_balance_stub_csr_global IS
1804   SELECT SUM( balance )
1805   FROM   oe_credit_summaries
1806   WHERE  balance_type  IN
1807          (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10,
1808 	  b11, b12, b13, b14, b15, b16, b17, b18, b21, b22)
1809   AND  party_id              =  p_party_id
1810   AND  currency_code         =  l_currency_code
1811   AND  bucket                <  l_bucket
1812   AND bucket_duration         =  l_bucket_length
1813   ;
1814 
1815   ---added for the Returns project
1816   ---the same as above including Returns
1817   CURSOR party_bal_stub_ret_csr_global IS
1818   SELECT SUM( balance )
1819   FROM   oe_credit_summaries
1820   WHERE  balance_type  IN
1821          (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10,
1822 	  b11, b12, b13, b14, b15, b16, b17, b18, b21, b22,
1823            b23, b24, b25, b26, b27, b28, b29, b30, b31,
1824           b32, b33, b34, b35, b36)
1825   AND  party_id              =  p_party_id
1826   AND  currency_code         =  l_currency_code
1827   AND  bucket                <  l_bucket
1828   AND bucket_duration         =  l_bucket_length
1829   ;
1830 
1831   CURSOR party_br_stub_csr_global IS
1832   SELECT SUM( balance )
1833   FROM   oe_credit_summaries
1834   WHERE  balance_type  IN
1835           (21,22)
1836   AND  party_id              =  p_party_id
1837   AND  currency_code         =  l_currency_code
1838   AND  bucket                <  l_br_bucket
1839   AND bucket_duration         =  l_br_bucket_length
1840   ;
1841 
1842 
1843 -------------------------------------------------------------
1844   CURSOR party_h_balance_csr_global IS
1845   SELECT SUM( oes.balance )
1846   FROM   oe_credit_summaries oes
1847      ,   hz_hierarchy_nodes hn
1848   WHERE  oes.balance_type  IN
1849          (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10,
1850   	  b11, b12, b13, b14, b15, b16, b17, b18, b21, b22)
1851   AND  hn.parent_id                    = p_party_id
1852   AND  hn.parent_object_type           = 'ORGANIZATION'
1853   and  hn.parent_table_name            = 'HZ_PARTIES'
1854   and  hn.child_object_type            = 'ORGANIZATION'
1855   and  hn.effective_start_date  <=  sysdate
1856   and  hn.effective_end_date    >= SYSDATE
1857   and  hn.hierarchy_type
1858                 = OE_CREDIT_CHECK_UTIL.G_hierarchy_type
1859   AND  oes.party_id                        =  hn.child_id
1860   AND  oes.currency_code                   =  l_currency_code
1861   AND  oes.bucket                          =  l_bucket
1862   AND  oes.bucket_duration                   =  l_bucket_length
1863   ;
1864 
1865 
1866   ---added for the Returns project
1867   ---the same as above including Returns
1868   CURSOR party_h_bal_ret_csr_global IS
1869   SELECT SUM( oes.balance )
1870   FROM   oe_credit_summaries oes
1871      ,   hz_hierarchy_nodes hn
1872   WHERE  oes.balance_type  IN
1873          (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10,
1874   	  b11, b12, b13, b14, b15, b16, b17, b18, b21, b22,
1875           b23, b24, b25, b26, b27, b28, b29, b30, b31,
1876           b32, b33, b34, b35, b36)
1877   AND  hn.parent_id                    = p_party_id
1878   AND  hn.parent_object_type           = 'ORGANIZATION'
1879   and  hn.parent_table_name            = 'HZ_PARTIES'
1880   and  hn.child_object_type            = 'ORGANIZATION'
1881   and  hn.effective_start_date  <=  sysdate
1882   and  hn.effective_end_date    >= SYSDATE
1883   and  hn.hierarchy_type
1884                 = OE_CREDIT_CHECK_UTIL.G_hierarchy_type
1885   AND  oes.party_id                        =  hn.child_id
1886   AND  oes.currency_code                   =  l_currency_code
1887   AND  oes.bucket                          =  l_bucket
1888   AND  oes.bucket_duration                   =  l_bucket_length
1889   ;
1890 
1891 
1892   CURSOR party_h_bal_stub_csr_gl IS
1893   SELECT SUM( oes.balance )
1894   FROM   oe_credit_summaries oes
1895      ,   hz_hierarchy_nodes hn
1896   WHERE  oes.balance_type  IN
1897          (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10,
1898 	  b11, b12, b13, b14, b15, b16, b17, b18, b21, b22)
1899   AND  hn.parent_id                  = p_party_id
1900   AND  hn.parent_object_type           = 'ORGANIZATION'
1901   and  hn.parent_table_name            = 'HZ_PARTIES'
1902   and  hn.child_object_type            = 'ORGANIZATION'
1903   and  hn.effective_start_date  <=  sysdate
1904   and  hn.effective_end_date    >= SYSDATE
1905   and  hn.hierarchy_type
1906                 = OE_CREDIT_CHECK_UTIL.G_hierarchy_type
1907   AND  oes.party_id              =  hn.child_id
1908   AND  oes.currency_code         =  l_currency_code
1909   AND  oes.bucket                <  l_bucket
1910   AND  oes.bucket_duration         =  l_bucket_length
1911   ;
1912 
1913   ---added for the Returns project
1914   ---the same as above including Returns
1915   CURSOR party_h_bal_ret_stub_csr_gl IS
1916   SELECT SUM( oes.balance )
1917   FROM   oe_credit_summaries oes
1918      ,   hz_hierarchy_nodes hn
1919   WHERE  oes.balance_type  IN
1920          (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10,
1921 	  b11, b12, b13, b14, b15, b16, b17, b18, b21, b22,
1922           b23, b24, b25, b26, b27, b28, b29, b30, b31,
1923           b32, b33, b34, b35, b36)
1924   AND  hn.parent_id                  = p_party_id
1925   AND  hn.parent_object_type           = 'ORGANIZATION'
1926   and  hn.parent_table_name            = 'HZ_PARTIES'
1927   and  hn.child_object_type            = 'ORGANIZATION'
1928   and  hn.effective_start_date  <=  sysdate
1929   and  hn.effective_end_date    >= SYSDATE
1930   and  hn.hierarchy_type
1931                 = OE_CREDIT_CHECK_UTIL.G_hierarchy_type
1932   AND  oes.party_id              =  hn.child_id
1933   AND  oes.currency_code         =  l_currency_code
1934   AND  oes.bucket                <  l_bucket
1935   AND  oes.bucket_duration         =  l_bucket_length
1936   ;
1937 
1938 
1939   CURSOR party_br_h_stub_csr_gl IS
1940   SELECT SUM( oes.balance )
1941   FROM   oe_credit_summaries oes
1942      ,   hz_hierarchy_nodes hn
1943   WHERE  oes.balance_type  IN
1944           (21,22)
1945   AND  hn.parent_id                  = p_party_id
1946   AND  hn.parent_object_type           = 'ORGANIZATION'
1947   and  hn.parent_table_name            = 'HZ_PARTIES'
1948   and  hn.child_object_type            = 'ORGANIZATION'
1949   and  hn.effective_start_date  <=  sysdate
1950   and  hn.effective_end_date    >= SYSDATE
1951   and  hn.hierarchy_type
1952                 = OE_CREDIT_CHECK_UTIL.G_hierarchy_type
1953   AND  oes.party_id              =  hn.child_id
1954   AND  oes.currency_code         =  l_currency_code
1955   AND  oes.bucket                <  l_br_bucket
1956   AND  oes.bucket_duration         =  l_br_bucket_length
1957   ;
1958 
1959 -------------------------------------------------------------
1960 
1961 
1962   CURSOR site_balance_all_curr_csr IS
1963   SELECT SUM( balance )
1964        , currency_code
1965   FROM   oe_credit_summaries
1966   WHERE  balance_type  IN
1967 	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
1968 	  b10, b11, b12, b13, b14, b15, b16, b17, b18)
1969   AND  site_use_id      =  l_site_use_id
1970   AND  bucket                =  l_bucket
1971   AND bucket_duration         =  l_bucket_length
1972   GROUP BY  currency_code
1973   ;
1974 
1975   ---added for the Returns project
1976   ---the same as above including Returns
1977 
1978   CURSOR site_bal_all_curr_ret_csr IS
1979   SELECT SUM( balance )
1980     , currency_code
1981   FROM   oe_credit_summaries
1982   WHERE  balance_type  IN
1983  	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
1984 	  b10, b11, b12, b13, b14, b15, b16, b17, b18,
1985           b23, b24, b25, b26, b27, b28, b29, b30, b31,
1986           b32, b33, b34, b35, b36)
1987   AND  site_use_id           =  l_site_use_id
1988   AND  bucket                =  l_bucket
1989   AND  bucket_duration       =  l_bucket_length
1990   GROUP BY  currency_code
1991   ;
1992 
1993 
1994   CURSOR site_balance_all_curr_stub_csr IS
1995   SELECT SUM( balance )
1996        , currency_code
1997   FROM   oe_credit_summaries
1998   WHERE  balance_type  IN
1999 	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
2000 	  b10, b11, b12, b13, b14, b15, b16, b17, b18)
2001   AND  site_use_id      =  l_site_use_id
2002   AND  bucket           <  l_bucket
2003   AND bucket_duration   =  l_bucket_length
2004   GROUP BY  currency_code
2005   ;
2006 
2007   ---added for the Returns project
2008   ---the same as above including Returns
2009   CURSOR site_bal_all_curr_stub_ret_csr IS
2010   SELECT SUM( balance )
2011        , currency_code
2012   FROM   oe_credit_summaries
2013   WHERE  balance_type  IN
2014 	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
2015 	  b10, b11, b12, b13, b14, b15, b16, b17, b18,
2016           b23, b24, b25, b26, b27, b28, b29, b30, b31,
2017           b32, b33, b34, b35, b36)
2018   AND  site_use_id      =  l_site_use_id
2019   AND  bucket           <  l_bucket
2020   AND bucket_duration   =  l_bucket_length
2021   GROUP BY  currency_code
2022   ;
2023 
2024   CURSOR site_br_all_curr_stub_csr IS
2025   SELECT SUM( balance )
2026        , currency_code
2027   FROM   oe_credit_summaries
2028   WHERE  balance_type  IN
2029           (21,22)
2030   AND  site_use_id      =  l_br_site_use_id
2031   AND  bucket           <  l_br_bucket
2032   AND bucket_duration   =  l_br_bucket_length
2033   GROUP BY  currency_code
2034   ;
2035 
2036 
2037   CURSOR customer_balance_all_curr_csr IS
2038   SELECT SUM( balance )
2039        , currency_code
2040   FROM   oe_credit_summaries
2041   WHERE  balance_type  IN
2042          (b1, b2, b3, b4, b5, b6, b7, b8, b9,
2043 	  b10, b11, b12, b13, b14, b15, b16, b17, b18, b21, b22 )
2044   AND  cust_account_id      =  p_customer_id
2045   AND  ((org_id             =  p_org_id)
2046          OR
2047         (org_id IS NULL AND p_org_id IS NULL))
2048   AND  bucket               =  l_bucket
2049   AND bucket_duration       =  l_bucket_length
2050   GROUP BY currency_code
2051   ;
2052 
2053   ---added for the Returns project
2054   ---the same as above including Returns
2055   CURSOR customer_bal_all_curr_ret_csr IS
2056   SELECT SUM( balance )
2057        , currency_code
2058   FROM   oe_credit_summaries
2059   WHERE  balance_type  IN
2060          (b1, b2, b3, b4, b5, b6, b7, b8, b9,
2061 	  b10, b11, b12, b13, b14, b15, b16, b17, b18, b21, b22,
2062           b23, b24, b25, b26, b27, b28, b29, b30, b31,
2063           b32, b33, b34, b35, b36)
2064   AND  cust_account_id      =  p_customer_id
2065   AND  ((org_id             =  p_org_id)
2066          OR
2067         (org_id IS NULL AND p_org_id IS NULL))
2068   AND  bucket               =  l_bucket
2069   AND bucket_duration       =  l_bucket_length
2070   GROUP BY currency_code
2071   ;
2072 
2073   CURSOR customer_all_curr_stub_csr IS
2074   SELECT SUM( balance )
2075        , currency_code
2076   FROM   oe_credit_summaries
2077   WHERE  balance_type  IN
2078 	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
2079 	  b10, b11, b12, b13, b14, b15, b16, b17, b18, b21, b22)
2080   AND  cust_account_id      =  p_customer_id
2081   AND  ((org_id             =  p_org_id)
2082          OR
2083         (org_id IS NULL AND p_org_id IS NULL))
2084   AND  bucket               <  l_bucket
2085   AND bucket_duration       =  l_bucket_length
2086   GROUP BY currency_code
2087   ;
2088 
2089   ---added for the Returns project
2090   ---the same as above including Returns
2091   CURSOR customer_all_curr_stub_ret_csr IS
2092   SELECT SUM( balance )
2093        , currency_code
2094   FROM   oe_credit_summaries
2095   WHERE  balance_type  IN
2096 	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
2097 	  b10, b11, b12, b13, b14, b15, b16, b17, b18, b21, b22,
2098           b23, b24, b25, b26, b27, b28, b29, b30, b31,
2099           b32, b33, b34, b35, b36)
2100   AND  cust_account_id      =  p_customer_id
2101   AND  ((org_id             =  p_org_id)
2102          OR
2103         (org_id IS NULL AND p_org_id IS NULL))
2104   AND  bucket               <  l_bucket
2105   AND bucket_duration       =  l_bucket_length
2106   GROUP BY currency_code
2107   ;
2108 
2109   CURSOR customer_br_all_curr_stub_csr IS
2110   SELECT SUM( balance )
2111        , currency_code
2112   FROM   oe_credit_summaries
2113   WHERE  balance_type  IN
2114           (21,22)
2115   AND  cust_account_id      =  p_customer_id
2116   AND  ((org_id             =  p_org_id)
2117          OR
2118         (org_id IS NULL AND p_org_id IS NULL))
2119   AND  bucket               <  l_br_bucket
2120   AND bucket_duration       =  l_br_bucket_length
2121   GROUP BY currency_code
2122   ;
2123 
2124 
2125   CURSOR cust_all_curr_csr_global IS
2126   SELECT SUM( balance )
2127        , currency_code
2128   FROM   oe_credit_summaries
2129   WHERE  balance_type  IN
2130 	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
2131 	  b10, b11, b12, b13, b14, b15, b16, b17, b18 , b21, b22)
2132   AND    cust_account_id     =  p_customer_id
2133   AND  bucket                =  l_bucket
2134   AND bucket_duration        =  l_bucket_length
2135   GROUP BY currency_code
2136   ;
2137 
2138   ---added for the Returns project
2139   ---the same as above including Returns
2140   CURSOR cust_all_curr_ret_csr_global IS
2141   SELECT SUM( balance )
2142        , currency_code
2143   FROM   oe_credit_summaries
2144   WHERE  balance_type  IN
2145 	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
2146 	  b10, b11, b12, b13, b14, b15, b16, b17, b18 , b21, b22,
2147           b23, b24, b25, b26, b27, b28, b29, b30, b31,
2148           b32, b33, b34, b35, b36)
2149   AND    cust_account_id      =  p_customer_id
2150   AND  bucket                 =  l_bucket
2151   AND bucket_duration         =  l_bucket_length
2152   GROUP BY currency_code
2153   ;
2154 
2155 
2156   CURSOR cust_all_curr_stub_csr_global IS
2157   SELECT SUM( balance )
2158        , currency_code
2159   FROM   oe_credit_summaries
2160   WHERE  balance_type  IN
2161 	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
2162 	  b10, b11, b12, b13, b14, b15, b16, b17, b18 , b21, b22)
2163   AND    cust_account_id      =  p_customer_id
2164   AND  bucket                 <  l_bucket
2165   AND bucket_duration         =  l_bucket_length
2166   GROUP BY currency_code
2167   ;
2168 
2169   ---added for the Returns project
2170   ---the same as above including Returns
2171   CURSOR cust_all_curr_stub_ret_csr_gl IS
2172   SELECT SUM( balance )
2173        , currency_code
2174   FROM   oe_credit_summaries
2175   WHERE  balance_type  IN
2176 	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
2177 	  b10, b11, b12, b13, b14, b15, b16, b17, b18 , b21, b22,
2178           b23, b24, b25, b26, b27, b28, b29, b30, b31,
2179           b32, b33, b34, b35, b36)
2180   AND    cust_account_id      =  p_customer_id
2181   AND  bucket                 <  l_bucket
2182   AND bucket_duration         =  l_bucket_length
2183   GROUP BY currency_code
2184   ;
2185 
2186   CURSOR cust_br_all_curr_stub_csr_gl IS
2187   SELECT SUM( balance )
2188        , currency_code
2189   FROM   oe_credit_summaries
2190   WHERE  balance_type  IN
2191           (21,22)
2192   AND    cust_account_id      =  p_customer_id
2193   AND  bucket                 <  l_br_bucket
2194   AND bucket_duration         =  l_br_bucket_length
2195   GROUP BY currency_code
2196   ;
2197 
2198 
2199 ----------------------------------------------------------
2200 
2201   CURSOR party_all_curr_csr_global IS
2202   SELECT SUM( balance )
2203        , currency_code
2204   FROM   oe_credit_summaries
2205   WHERE  balance_type  IN
2206 	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
2207 	  b10, b11, b12, b13, b14, b15, b16, b17, b18 , b21, b22)
2208   AND  party_id              = p_party_id
2209   AND  bucket                =  l_bucket
2210   AND bucket_duration        =  l_bucket_length
2211   GROUP BY currency_code
2212   ;
2213 
2214   ---added for the Returns project
2215   ---the same as above including Returns
2216   CURSOR party_all_curr_ret_csr_gl IS
2217   SELECT SUM( balance )
2218        , currency_code
2219   FROM   oe_credit_summaries
2220   WHERE  balance_type  IN
2221 	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
2222 	  b10, b11, b12, b13, b14, b15, b16, b17, b18 , b21, b22,
2223           b23, b24, b25, b26, b27, b28, b29, b30, b31,
2224           b32, b33, b34, b35, b36)
2225   AND  party_id              = p_party_id
2226   AND  bucket                =  l_bucket
2227   AND bucket_duration        =  l_bucket_length
2228   GROUP BY currency_code
2229   ;
2230 
2231   CURSOR party_all_curr_stub_csr_global IS
2232   SELECT SUM( balance )
2233        , currency_code
2234   FROM   oe_credit_summaries
2235   WHERE  balance_type  IN
2236 	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
2237 	  b10, b11, b12, b13, b14, b15, b16, b17, b18 , b21, b22)
2238   AND  party_id              = p_party_id
2239   AND  bucket                <  l_bucket
2240   AND bucket_duration        =  l_bucket_length
2241   GROUP BY currency_code
2242   ;
2243 
2244   ---added for the Returns project
2245   ---the same as above including Returns
2246   CURSOR party_all_curr_stub_ret_csr_gl IS
2247   SELECT SUM( balance )
2248        , currency_code
2249   FROM   oe_credit_summaries
2250   WHERE  balance_type  IN
2251 	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
2252 	  b10, b11, b12, b13, b14, b15, b16, b17, b18 , b21, b22,
2253           b23, b24, b25, b26, b27, b28, b29, b30, b31,
2254           b32, b33, b34, b35, b36)
2255   AND  party_id              = p_party_id
2256   AND  bucket                <  l_bucket
2257   AND bucket_duration        =  l_bucket_length
2258   GROUP BY currency_code
2259   ;
2260 
2261   CURSOR party_br_all_curr_stub_csr_gl IS
2262   SELECT SUM( balance )
2263        , currency_code
2264   FROM   oe_credit_summaries
2265   WHERE  balance_type  IN
2266           (21,22)
2267   AND  party_id              = p_party_id
2268   AND  bucket                <  l_br_bucket
2269   AND bucket_duration        =  l_br_bucket_length
2270   GROUP BY currency_code
2271   ;
2272 
2273 
2274 ---------------------------------------------------
2275 
2276   CURSOR party_h_all_curr_csr_global IS
2277   SELECT SUM( oes.balance )
2278        , oes.currency_code
2279   FROM   oe_credit_summaries oes
2280      ,   hz_hierarchy_nodes hn
2281   WHERE  oes.balance_type  IN
2282 	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
2283 	  b10, b11, b12, b13, b14, b15, b16, b17, b18 , b21, b22)
2284   AND  hn.parent_id                  = p_party_id
2285   AND  hn.parent_object_type           = 'ORGANIZATION'
2286   and  hn.parent_table_name            = 'HZ_PARTIES'
2287   and  hn.child_object_type            = 'ORGANIZATION'
2288   and  hn.effective_start_date  <=  sysdate
2289   and  hn.effective_end_date    >= SYSDATE
2290   and  hn.hierarchy_type
2291                 = OE_CREDIT_CHECK_UTIL.G_hierarchy_type
2292   AND  oes.party_id              =  hn.child_id
2293   AND  oes.bucket                =  l_bucket
2294   AND  oes.bucket_duration         =  l_bucket_length
2295   GROUP BY oes.currency_code
2296   ;
2297 
2298   ---added for the Returns project
2299   ---the same as above including Returns
2300   CURSOR party_h_all_curr_ret_csr_gl IS
2301   SELECT SUM( oes.balance )
2302        , oes.currency_code
2303   FROM   oe_credit_summaries oes
2304      ,   hz_hierarchy_nodes hn
2305   WHERE  oes.balance_type  IN
2306 	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
2307 	  b10, b11, b12, b13, b14, b15, b16, b17, b18 , b21, b22,
2308           b23, b24, b25, b26, b27, b28, b29, b30, b31,
2309           b32, b33, b34, b35, b36)
2310   AND  hn.parent_id                  = p_party_id
2311   AND  hn.parent_object_type           = 'ORGANIZATION'
2312   and  hn.parent_table_name            = 'HZ_PARTIES'
2313   and  hn.child_object_type            = 'ORGANIZATION'
2314   and  hn.effective_start_date  <=  sysdate
2315   and  hn.effective_end_date    >= SYSDATE
2316   and  hn.hierarchy_type
2317                 = OE_CREDIT_CHECK_UTIL.G_hierarchy_type
2318   AND  oes.party_id              =  hn.child_id
2319   AND  oes.bucket                =  l_bucket
2320   AND  oes.bucket_duration         =  l_bucket_length
2321   GROUP BY oes.currency_code
2322   ;
2323 
2324   CURSOR party_h_all_curr_stub_csr_gl IS
2325   SELECT SUM( oes.balance )
2326        , oes.currency_code
2327     FROM   oe_credit_summaries oes
2328      ,   hz_hierarchy_nodes hn
2329   WHERE  oes.balance_type  IN
2330 	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
2331 	  b10, b11, b12, b13, b14, b15, b16, b17, b18 , b21, b22)
2332   AND  hn.parent_id                  = p_party_id
2333   AND  hn.parent_object_type           = 'ORGANIZATION'
2334   and  hn.parent_table_name            = 'HZ_PARTIES'
2335   and  hn.child_object_type            = 'ORGANIZATION'
2336   and  hn.effective_start_date  <=  sysdate
2337   and  hn.effective_end_date    >= SYSDATE
2338   and  hn.hierarchy_type
2339                 = OE_CREDIT_CHECK_UTIL.G_hierarchy_type
2340   AND  oes.party_id              = hn.child_id
2341   AND  oes.bucket                <  l_bucket
2342   AND  oes.bucket_duration         =  l_bucket_length
2343   GROUP BY oes.currency_code
2344   ;
2345 
2346   ---added for the Returns project
2347   ---the same as above including Returns
2348   CURSOR p_h_all_curr_stub_ret_csr_gl IS
2349   SELECT SUM( oes.balance )
2350        , oes.currency_code
2351     FROM   oe_credit_summaries oes
2352      ,   hz_hierarchy_nodes hn
2353   WHERE  oes.balance_type  IN
2354 	 (b1, b2, b3, b4, b5, b6, b7, b8, b9,
2355 	  b10, b11, b12, b13, b14, b15, b16, b17, b18 , b21, b22,
2356           b23, b24, b25, b26, b27, b28, b29, b30, b31,
2357           b32, b33, b34, b35, b36)
2358   AND  hn.parent_id                  = p_party_id
2359   AND  hn.parent_object_type           = 'ORGANIZATION'
2360   and  hn.parent_table_name            = 'HZ_PARTIES'
2361   and  hn.child_object_type            = 'ORGANIZATION'
2362   and  hn.effective_start_date  <=  sysdate
2363   and  hn.effective_end_date    >= SYSDATE
2364   and  hn.hierarchy_type
2365                 = OE_CREDIT_CHECK_UTIL.G_hierarchy_type
2366   AND  oes.party_id              = hn.child_id
2367   AND  oes.bucket                <  l_bucket
2368   AND  oes.bucket_duration         =  l_bucket_length
2369   GROUP BY oes.currency_code
2370   ;
2371 
2372   CURSOR party_br_h_all_curr_stub_gl IS
2373   SELECT SUM( oes.balance )
2374        , oes.currency_code
2375     FROM   oe_credit_summaries oes
2376      ,   hz_hierarchy_nodes hn
2377   WHERE  oes.balance_type  IN
2378           (21,22)
2379   AND  hn.parent_id                  = p_party_id
2380   AND  hn.parent_object_type           = 'ORGANIZATION'
2381   and  hn.parent_table_name            = 'HZ_PARTIES'
2382   and  hn.child_object_type            = 'ORGANIZATION'
2383   and  hn.effective_start_date  <=  sysdate
2384   and  hn.effective_end_date    >= SYSDATE
2385   and  hn.hierarchy_type
2386                 = OE_CREDIT_CHECK_UTIL.G_hierarchy_type
2387   AND  oes.party_id              = hn.child_id
2388   AND  oes.bucket                <  l_br_bucket
2389   AND  oes.bucket_duration         =  l_br_bucket_length
2390   GROUP BY oes.currency_code
2391   ;
2392 
2393 BEGIN
2394   --  We are determining now which summary cursor to open depending on
2395   -- whether the exposure is a site or customer level, and
2396   -- whether it is for all currencies or a list of currencies.
2397   --  The choice between order or line bill-to-site has already been
2398   -- made above by selecting the right balance types.
2399 
2400   -- for each of the for cases, there are nested loops.
2401   -- The only difference between the two site and customer level cases
2402   -- are the cursor names.
2403 
2404   IF G_debug_flag = 'Y'
2405   THEN
2406     oe_debug_pub.add( 'Into retrieve_exposure ');
2407     oe_debug_pub.add( '+++++++++++++++++++++++++++++++++++');
2408     oe_debug_pub.add( 'p_site_use_id          ==> '|| p_site_use_id );
2409     oe_debug_pub.add( 'p_customer_id          ==> '|| p_customer_id );
2410     oe_debug_pub.add( 'p_party_id             ==> ' || p_party_id );
2411     oe_debug_pub.add( 'p_org_id               ==> '|| p_org_id);
2412     oe_debug_pub.add( 'p_include_all_flag     ==> '|| p_include_all_flag );
2413     oe_debug_pub.add( 'p_main_bucket          ==> '|| p_main_bucket );
2414     oe_debug_pub.add( 'p_global_exposure_flag ==> '|| p_global_exposure_flag );
2415     oe_debug_pub.add( 'p_limit_curr_code      ==> '|| p_limit_curr_code );
2416     oe_debug_pub.add( 'include_returns_flag   ==> '|| p_credit_check_rule_rec.include_returns_flag);
2417     oe_debug_pub.add( '+++++++++++++++++++++++++++++++++++');
2418   END IF;
2419 
2420   l_total := 0;
2421   l_br_site_use_id := NULL ;
2422 
2423   IF  p_credit_check_rule_rec.open_ar_balance_flag = 'Y'
2424   THEN
2425    l_br_bucket_length  := POWER( 2, G_MAX_BUCKET_LEVEL );
2426    l_br_bucket         := open_date( p_days  => NULL);
2427 
2428   END IF;
2429 
2430   IF  p_site_use_id  IS NOT NULL
2431   THEN
2432    ----------------------------------------------------------------
2433    ---------------------- Site Level Exposure ----------------------
2434 
2435     IF G_debug_flag = 'Y'
2436     THEN
2437       oe_debug_pub.add( 'exposure at site level ',1);
2438     END IF;
2439 
2440     l_site_use_id := p_site_use_id;
2441 
2442     IF G_debug_flag = 'Y'
2443     THEN
2444       oe_debug_pub.add( 'l_site_use_id => '|| l_site_use_id );
2445     END IF;
2446 
2447     IF  NVL(p_include_all_flag,'N')  =  'N'
2448     THEN
2449      IF G_debug_flag = 'Y'
2450      THEN
2451        oe_debug_pub.add('site - NOT all currencies');
2452      END IF;
2453       -- exposure is at site level and for a list of currencies
2454 
2455       -- for each currency in the list, loop
2456       i  := p_usage_curr_tbl.FIRST;
2457 
2458       WHILE  i  IS NOT NULL
2459       LOOP
2460 
2461         l_currency_code  := p_usage_curr_tbl(i).usage_curr_code;
2462 
2463 	l_bucket         :=  p_main_bucket;
2464 	l_bucket_length  :=  G_MAX_BUCKET_LENGTH;
2465 
2466 
2467         ----change for the Return project
2468         ---- if Returns are included, the cursor site_balance_stub_ret_csr
2469         ---- is used, if Returns are not included, then the old logic and
2470         ---- site_balance_stub_csr is used.
2471 
2472         IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
2473         THEN
2474           OPEN site_balance_stub_csr;
2475  	  FETCH site_balance_stub_csr
2476           INTO l_balance;
2477         ELSE
2478           IF G_debug_flag = 'Y'
2479           THEN
2480             oe_debug_pub.add('OPEN site_balance_stub_ret_csr');
2481           END IF;
2482 
2483           OPEN site_balance_stub_ret_csr;
2484  	  FETCH site_balance_stub_ret_csr
2485           INTO l_balance;
2486 
2487           IF G_debug_flag = 'Y'
2488           THEN
2489             oe_debug_pub.add('l_balance='||TO_CHAR(l_balance));
2490           END IF;
2491         END IF;
2492 
2493         IF p_credit_check_rule_rec.open_ar_balance_flag = 'Y'
2494         THEN
2495            l_br_site_use_id :=
2496            oe_credit_check_util.get_drawee_site_use_id ( p_site_use_id);
2497         END IF;
2498 
2499         IF b8 = G_INVOICES
2500         THEN
2501           IF l_br_site_use_id IS NOT NULL
2502           THEN
2503             OPEN   site_br_stub_csr;
2504             FETCH  site_br_stub_csr
2505             INTO   l_br_balance;
2506             CLOSE site_br_stub_csr ;
2507           END IF;
2508         END IF;
2509 
2510         IF G_debug_flag = 'Y'
2511         THEN
2512 	  oe_debug_pub.add
2513 	    ( 'stub cursor at ' || l_currency_code || ' currency with balance '
2514 	      || l_balance || ' and bucket/length ' || l_bucket
2515               || '/' || l_bucket_length, 2 );
2516 
2517 	  oe_debug_pub.add
2518 	    ( 'stub cursor at ' || l_currency_code
2519                  || ' currency with BR balance '
2520 	      || l_br_balance || ' and bucket/length ' || l_bucket
2521               || '/' || l_bucket_length, 2 );
2522 
2523          END IF;
2524 
2525 	  l_total  := l_total +  convert_currency_amount
2526 	  	      ( p_amount                  =>
2527                               l_balance + NVL(l_br_balance,0)
2528 		      , p_transactional_currency  => l_currency_code
2529 		      , p_limit_currency          => p_limit_curr_code
2530 		      );
2531 
2532         ----change for the Return project
2533         ---- closing appropriate cursor: if Returns are included,
2534         ---- the cursor site_balance_stub_ret_csr, if Returns are not included,
2535         ---- then the cursor site_balance_stub_csr is used.
2536 
2537         IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
2538         THEN
2539           CLOSE site_balance_stub_csr;
2540         ELSE
2541           CLOSE site_balance_stub_ret_csr;
2542         END IF;
2543 
2544         -----------------------------
2545         -- for fraction of Main Bucket
2546 
2547         j  :=  p_binary_tbl.FIRST;
2548 	WHILE j IS NOT NULL LOOP
2549  	  l_bucket         :=  p_binary_tbl(j).bucket;
2550 	  l_bucket_length  :=  p_binary_tbl(j).bucket_length;
2551 
2552           ----change for the Return project
2553           IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
2554           THEN
2555    	    OPEN  site_balance_csr;
2556 	    FETCH  site_balance_csr
2557             INTO  l_balance;
2558           ELSE
2559             IF G_debug_flag = 'Y'
2560             THEN
2561               oe_debug_pub.add('OPEN site_balance_ret_csr');
2562             END IF;
2563 
2564             OPEN  site_balance_ret_csr;
2565 	    FETCH  site_balance_ret_csr
2566             INTO  l_balance;
2567           END IF;
2568 
2569 
2570           IF G_debug_flag = 'Y'
2571           THEN
2572 	    oe_debug_pub.add
2573 	    ( 'cursor at ' || l_currency_code || ' currency with balance '
2574 	      || l_balance || ' and bucket/length ' || l_bucket || '/'
2575               || l_bucket_length, 2 );
2576           END IF;
2577 
2578 	  BEGIN
2579 	    l_term :=
2580 	      OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
2581 	      ( p_amount                  => l_balance
2582 	      , p_transactional_currency  => l_currency_code
2583 	      , p_limit_currency          => p_limit_curr_code
2584               , p_functional_currency     => g_functional_currency
2585 	      , p_conversion_date         => SYSDATE
2586 	      , p_conversion_type         => g_conversion_type
2587 	      );
2588 	    l_total := l_total + NVL( l_term, 0 );
2589 
2590 	  EXCEPTION
2591 	    WHEN  GL_CURRENCY_API.NO_RATE  OR
2592                   GL_CURRENCY_API.INVALID_CURRENCY THEN
2593  	      add_error_currency( x_error_curr_tbl, l_currency_code );
2594 
2595               IF G_debug_flag = 'Y'
2596               THEN
2597 	        oe_debug_pub.add( 'conversion exception for '
2598                 || l_currency_code, 1 );
2599               END IF;
2600 	  END;
2601           ----change for the Return project
2602           IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
2603           THEN
2604 	    CLOSE site_balance_csr;
2605           ELSE
2606             CLOSE site_balance_ret_csr;
2607           END IF;
2608 
2609 	  j  :=  p_binary_tbl.NEXT(j);
2610         END LOOP;
2611         i := p_usage_curr_tbl.NEXT(i);
2612       END LOOP;
2613 
2614     ELSE
2615       -- exposure is at site level and for all currencies
2616      IF G_debug_flag = 'Y'
2617      THEN
2618       oe_debug_pub.add( 'SITE, ALL currencies ');
2619      END IF;
2620       i := 0;
2621 
2622       --------------------------
2623       -- for left of Main Bucket
2624       l_bucket         :=  p_main_bucket;
2625       l_bucket_length  :=  G_MAX_BUCKET_LENGTH;
2626 
2627       ----change for the Return project
2628       IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
2629       THEN
2630         OPEN site_balance_all_curr_stub_csr;
2631         FETCH site_balance_all_curr_stub_csr
2632         INTO l_balance, l_currency_code;
2633 
2634         -- For each currency in the cursor
2635       WHILE NOT site_balance_all_curr_stub_csr%NOTFOUND
2636       LOOP
2637         IF G_debug_flag = 'Y'
2638         THEN
2639 	  oe_debug_pub.add
2640    ( '(all) stub cursor at ' || l_currency_code || ' currency with balance '
2641 	      || l_balance || ' and bucket/length '
2642             || l_bucket || '/' || l_bucket_length, 2 );
2643         END IF;
2644 
2645 
2646           IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
2647                      l_currency_code ,1,1),0) = 0 )
2648 	    OR
2649 	     ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
2650                      l_currency_code ,1,1),0) > 0 )
2651 	      AND ( l_currency_code = p_limit_curr_code )
2652 	     )
2653 	  THEN
2654 	    BEGIN
2655  	      l_term :=
2656 	        OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
2657 	        ( p_amount                  => l_balance
2658                 , p_transactional_currency  => l_currency_code
2659 	        , p_limit_currency          => p_limit_curr_code
2660 	        , p_functional_currency     => g_functional_currency
2661 	        , p_conversion_date         => SYSDATE
2662 	        , p_conversion_type         => g_conversion_type
2663 	        );
2664 	      l_total  := l_total  +  NVL( l_term, 0 );
2665 
2666 	    EXCEPTION
2667 	      WHEN  GL_CURRENCY_API.NO_RATE  OR
2668                     GL_CURRENCY_API.INVALID_CURRENCY THEN
2669 	        oe_debug_pub.add( 'conversion exception for '
2670                    || l_currency_code, 1 );
2671 	        add_error_currency( x_error_curr_tbl, l_currency_code );
2672 	    END;
2673           ELSE
2674 	    oe_debug_pub.add( 'Currency excluded from usages '
2675               || l_currency_code);
2676 	  END IF;  -- exclude curr list
2677 
2678 	  FETCH  site_balance_all_curr_stub_csr
2679           INTO  l_balance, l_currency_code;
2680 
2681 	  i := i + 1;
2682 
2683       END LOOP;
2684 
2685      CLOSE site_balance_all_curr_stub_csr;
2686 
2687 
2688      ----change for the Return project
2689      --- the code is the same as above but using cursor
2690      --- site_bal_all_curr_stub_ret_csr instead site_balance_all_curr_stub_csr
2691 
2692      ELSE
2693        IF G_debug_flag = 'Y'
2694        THEN
2695          oe_debug_pub.add('OPEN site_bal_all_curr_stub_ret_csr');
2696        END IF;
2697 
2698        OPEN site_bal_all_curr_stub_ret_csr;
2699        FETCH site_bal_all_curr_stub_ret_csr
2700        INTO l_balance, l_currency_code;
2701 
2702        -- For each currency in the cursor
2703       WHILE NOT site_bal_all_curr_stub_ret_csr%NOTFOUND
2704       LOOP
2705         IF G_debug_flag = 'Y'
2706         THEN
2707 	  oe_debug_pub.add
2708    ( '(all) stub cursor at ' || l_currency_code || ' currency with balance '
2709 	      || l_balance || ' and bucket/length '
2710             || l_bucket || '/' || l_bucket_length, 2 );
2711         END IF;
2712 
2713 
2714           IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
2715                      l_currency_code ,1,1),0) = 0 )
2716 	    OR
2717 	     ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
2718                      l_currency_code ,1,1),0) > 0 )
2719 	      AND ( l_currency_code = p_limit_curr_code )
2720 	     )
2721 	  THEN
2722 	    BEGIN
2723  	      l_term :=
2724 	        OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
2725 	        ( p_amount                  => l_balance
2726                 , p_transactional_currency  => l_currency_code
2727 	        , p_limit_currency          => p_limit_curr_code
2728 	        , p_functional_currency     => g_functional_currency
2729 	        , p_conversion_date         => SYSDATE
2730 	        , p_conversion_type         => g_conversion_type
2731 	        );
2732 	      l_total  := l_total  +  NVL( l_term, 0 );
2733 
2734 	    EXCEPTION
2735 	      WHEN  GL_CURRENCY_API.NO_RATE  OR
2736                     GL_CURRENCY_API.INVALID_CURRENCY THEN
2737 	        oe_debug_pub.add( 'conversion exception for '
2738                    || l_currency_code, 1 );
2739 	        add_error_currency( x_error_curr_tbl, l_currency_code );
2740 	    END;
2741           ELSE
2742 	    oe_debug_pub.add( 'Currency excluded from usages '
2743               || l_currency_code);
2744 	  END IF;  -- exclude curr list
2745 
2746 	  FETCH  site_bal_all_curr_stub_ret_csr
2747           INTO  l_balance, l_currency_code;
2748 
2749 	  i := i + 1;
2750 
2751         END LOOP;
2752 
2753        CLOSE site_bal_all_curr_stub_ret_csr;
2754 
2755      END IF; ----end of checking for include returns
2756 
2757       ---------------------start BR -------------------
2758      IF p_credit_check_rule_rec.open_ar_balance_flag = 'Y'
2759      THEN
2760         l_br_site_use_id :=
2761            oe_credit_check_util.get_drawee_site_use_id ( p_site_use_id);
2762      END IF;
2763 
2764      IF l_br_site_use_id IS NOT NULL
2765      AND b8 = G_INVOICES
2766      THEN
2767          i := 0;
2768 
2769       --------------------------
2770       -- for left of Main Bucket
2771          l_bucket         :=  p_main_bucket;
2772          l_bucket_length  :=  G_MAX_BUCKET_LENGTH;
2773 
2774          OPEN  site_br_all_curr_stub_csr;
2775          FETCH site_br_all_curr_stub_csr
2776          INTO l_br_balance, l_currency_code;
2777 
2778         -- For each currency in the cursor
2779         WHILE NOT site_br_all_curr_stub_csr%NOTFOUND
2780         LOOP
2781           IF G_debug_flag = 'Y'
2782           THEN
2783 	    oe_debug_pub.add
2784               ( '(all) stub cursor at ' || l_currency_code
2785                 || ' currency with BR  balance '
2786 	      || l_br_balance || ' and bucket/length '
2787             || l_bucket || '/' || l_bucket_length, 2 );
2788           END IF;
2789 
2790             IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
2791                      l_currency_code ,1,1),0) = 0 )
2792 	    OR
2793 	     ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
2794                      l_currency_code ,1,1),0) > 0 )
2795 	      AND ( l_currency_code = p_limit_curr_code )
2796 	     )
2797 	    THEN
2798 	      BEGIN
2799  	      l_term :=
2800 	        OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
2801 	        ( p_amount                  => l_br_balance
2802                 , p_transactional_currency  => l_currency_code
2803 	        , p_limit_currency          => p_limit_curr_code
2804 	        , p_functional_currency     => g_functional_currency
2805 	        , p_conversion_date         => SYSDATE
2806 	        , p_conversion_type         => g_conversion_type
2807 	        );
2808 	        l_total  := l_total  +  NVL( l_term, 0 );
2809 
2810 	      EXCEPTION
2811 	      WHEN  GL_CURRENCY_API.NO_RATE  OR
2812                     GL_CURRENCY_API.INVALID_CURRENCY THEN
2813 	        oe_debug_pub.add( 'conversion exception for '
2814                    || l_currency_code, 1 );
2815 	        add_error_currency( x_error_curr_tbl, l_currency_code );
2816 	    END;
2817           ELSE
2818 	    oe_debug_pub.add( 'Currency excluded from usages '
2819               || l_currency_code);
2820 	  END IF;  -- exclude curr list
2821 
2822 	  FETCH  site_br_all_curr_stub_csr
2823           INTO  l_balance, l_currency_code;
2824 
2825 	  i := i + 1;
2826         END LOOP;
2827 
2828        CLOSE site_br_all_curr_stub_csr ;
2829      END IF;
2830 ---------------------------- end BR ------------------
2831       -- for fraction of Main Bucket
2832 
2833       j  :=  p_binary_tbl.FIRST;
2834       WHILE j IS NOT NULL LOOP
2835         l_bucket         :=  p_binary_tbl(j).bucket;
2836         l_bucket_length  :=  p_binary_tbl(j).bucket_length;
2837 
2838         ----change for the Return project
2839         IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
2840         THEN
2841 
2842           -- this cursor deals with one site and all currencies
2843           OPEN site_balance_all_curr_csr;
2844           FETCH site_balance_all_curr_csr
2845           INTO l_balance, l_currency_code;
2846 
2847           -- For each currency that exists, the cursor returns a row.
2848           -- Convert the balance and sum up to total exposure
2849 
2850           WHILE NOT site_balance_all_curr_csr%NOTFOUND
2851           LOOP
2852            IF G_debug_flag = 'Y'
2853            THEN
2854             oe_debug_pub.add
2855               ( 'cursor at ' || l_currency_code || ' currency with balance '
2856 	        || l_balance || ' and bucket/length ' || l_bucket
2857                    || '/' || l_bucket_length, 2 );
2858             END IF;
2859 
2860             IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
2861                      l_currency_code ,1,1),0) = 0 )
2862         	    OR
2863 	       ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
2864                     l_currency_code ,1,1),0) > 0 )
2865 	        AND ( l_currency_code = p_limit_curr_code )
2866         	     )
2867 	    THEN
2868               BEGIN
2869 	        l_term :=
2870 	          OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
2871 	          ( p_amount                  => l_balance
2872                   , p_transactional_currency  => l_currency_code
2873 	          , p_limit_currency          => p_limit_curr_code
2874 	          , p_functional_currency     => g_functional_currency
2875 	          , p_conversion_date         => SYSDATE
2876 	          , p_conversion_type         => g_conversion_type
2877 	          );
2878 	        l_total := l_total + NVL( l_term, 0 );
2879 
2880               EXCEPTION
2881                 WHEN  GL_CURRENCY_API.NO_RATE  OR
2882                       GL_CURRENCY_API.INVALID_CURRENCY THEN
2883                 oe_debug_pub.add( 'conversion exception for '
2884                   || l_currency_code, 1 );
2885                 add_error_currency( x_error_curr_tbl, l_currency_code );
2886 	      END;
2887             ELSE
2888               oe_debug_pub.add( 'Currency excluded from usages ');
2889 	    END IF;  -- exclude curr list
2890 
2891             FETCH  site_balance_all_curr_csr
2892             INTO  l_balance, l_currency_code;
2893             i := i + 1;
2894           END LOOP;
2895           CLOSE site_balance_all_curr_csr;
2896 
2897         -----if returns are included use appropriate cursor
2898         ----- the rest code is the same as above
2899         ELSE
2900           -- this cursor deals with one site and all currencies
2901           OPEN site_bal_all_curr_ret_csr;
2902           FETCH site_bal_all_curr_ret_csr
2903           INTO l_balance, l_currency_code;
2904 
2905           -- For each currency that exists, the cursor returns a row.
2906           -- Convert the balance and sum up to total exposure
2907 
2908           WHILE NOT site_bal_all_curr_ret_csr%NOTFOUND
2909           LOOP
2910            IF G_debug_flag = 'Y'
2911            THEN
2912             oe_debug_pub.add
2913               ( 'cursor at ' || l_currency_code || ' currency with balance '
2914 	        || l_balance || ' and bucket/length ' || l_bucket
2915                    || '/' || l_bucket_length, 2 );
2916             END IF;
2917 
2918             IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
2919                      l_currency_code ,1,1),0) = 0 )
2920         	    OR
2921 	       ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
2922                     l_currency_code ,1,1),0) > 0 )
2923 	        AND ( l_currency_code = p_limit_curr_code )
2924         	     )
2925 	    THEN
2926               BEGIN
2927 	        l_term :=
2928 	          OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
2929 	          ( p_amount                  => l_balance
2930                   , p_transactional_currency  => l_currency_code
2931 	          , p_limit_currency          => p_limit_curr_code
2932 	          , p_functional_currency     => g_functional_currency
2933 	          , p_conversion_date         => SYSDATE
2934 	          , p_conversion_type         => g_conversion_type
2935 	          );
2936 	        l_total := l_total + NVL( l_term, 0 );
2937 
2938               EXCEPTION
2939                 WHEN  GL_CURRENCY_API.NO_RATE  OR
2940                       GL_CURRENCY_API.INVALID_CURRENCY THEN
2941                 oe_debug_pub.add( 'conversion exception for '
2942                   || l_currency_code, 1 );
2943                 add_error_currency( x_error_curr_tbl, l_currency_code );
2944 	      END;
2945             ELSE
2946               oe_debug_pub.add( 'Currency excluded from usages ');
2947 	    END IF;  -- exclude curr list
2948 
2949             FETCH  site_bal_all_curr_ret_csr
2950             INTO  l_balance, l_currency_code;
2951             i := i + 1;
2952           END LOOP;
2953           CLOSE site_bal_all_curr_ret_csr;
2954 
2955         END IF; ---end of checking for include returns
2956         j  :=  p_binary_tbl.NEXT(j);
2957       END LOOP;
2958     END IF;  -- site level currency - all/single
2959 
2960 ----------------- End site level -----------------------------------
2961 --------------------------------------------------------------------------
2962 
2963 ------------------------- Start customer level --------------------
2964   ELSIF p_customer_id IS NOT NULL
2965   THEN
2966    IF G_debug_flag = 'Y'
2967    THEN
2968      oe_debug_pub.add( 'exposure at customer level', 1 );
2969    END IF;
2970 
2971     IF  NVL(p_include_all_flag,'N')  =  'N'
2972     THEN
2973       -- exposure is at customer level and for a list of currencies
2974       -- for each currency in the list
2975 
2976       IF G_debug_flag = 'Y'
2977       THEN
2978         oe_debug_pub.add( 'NOT ALL currencies ');
2979       END IF;
2980 
2981       i  := p_usage_curr_tbl.FIRST;
2982       WHILE  i  IS NOT NULL
2983       LOOP
2984         l_currency_code := p_usage_curr_tbl(i).usage_curr_code;
2985         --------------------------
2986         -- for left of Main Bucket
2987 	l_bucket         :=  p_main_bucket;
2988 	l_bucket_length  :=  G_MAX_BUCKET_LENGTH;
2989 
2990         IF p_global_exposure_flag = 'Y'
2991         THEN
2992           IF G_debug_flag = 'Y'
2993           THEN
2994             oe_debug_pub.add( ' Into CUST global ');
2995           END IF;
2996 
2997           ----change for the Return project
2998           IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
2999           THEN
3000 	    OPEN cust_balance_stub_csr_global;
3001 	    FETCH cust_balance_stub_csr_global
3002             INTO l_balance;
3003           ELSE
3004             OPEN cust_bal_stub_ret_csr_global;
3005 	    FETCH cust_bal_stub_ret_csr_global
3006             INTO l_balance;
3007           END IF;
3008 
3009           IF p_credit_check_rule_rec.open_ar_days IS NOT NULL
3010              AND p_credit_check_rule_rec.open_ar_balance_flag = 'Y'
3011              AND b8 = G_INVOICES
3012           THEN
3013            OPEN  cust_br_stub_csr_global ;
3014            FETCH cust_br_stub_csr_global
3015            INTO  l_br_balance ;
3016            CLOSE cust_br_stub_csr_global ;
3017           END IF;
3018 
3019            IF G_debug_flag = 'Y'
3020            THEN
3021 	    oe_debug_pub.add
3022 	      ( 'global stub cursor at ' || l_currency_code
3023 	        || ' currency with balance ' || l_balance, 2 );
3024 	    oe_debug_pub.add
3025 	      ( 'global stub cursor at ' || l_currency_code
3026 	        || ' currency with BR balance ' || l_br_balance, 2 );
3027           END IF;
3028             BEGIN
3029 	      l_term :=
3030 	        OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
3031 	        ( p_amount                  => l_balance +
3032                              NVL(l_br_balance,0)
3033 	        , p_transactional_currency  => l_currency_code
3034 	        , p_limit_currency          => p_limit_curr_code
3035 	        , p_functional_currency     => g_functional_currency
3036 	        , p_conversion_date         => SYSDATE
3037 	        , p_conversion_type         => g_conversion_type
3038 	        );
3039   	      l_total := l_total + NVL( l_term, 0 );
3040 
3041        	    EXCEPTION
3042 	      WHEN  GL_CURRENCY_API.NO_RATE  OR
3043                     GL_CURRENCY_API.INVALID_CURRENCY THEN
3044 	        oe_debug_pub.add( 'conversion exception for '
3045                  || l_currency_code, 1 );
3046 	        add_error_currency( x_error_curr_tbl, l_currency_code );
3047              END;
3048              ----change for the Return project
3049              IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
3050              THEN
3051 	       CLOSE cust_balance_stub_csr_global;
3052              ELSE
3053                CLOSE cust_bal_stub_ret_csr_global;
3054              END IF;
3055 
3056 	    -----------------------------
3057 	    -- for fraction of Main Bucket
3058 
3059 	   j  :=  p_binary_tbl.FIRST;
3060 	  WHILE  j  IS NOT NULL
3061           LOOP
3062 	    l_bucket         :=  p_binary_tbl(j).bucket;
3063 	    l_bucket_length  :=  p_binary_tbl(j).bucket_length;
3064 
3065             ----change for the Return project
3066             IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
3067             THEN
3068 	      OPEN cust_balance_csr_global;
3069 	      FETCH cust_balance_csr_global
3070               INTO l_balance;
3071             ELSE
3072               OPEN cust_balance_ret_csr_global;
3073 	      FETCH cust_balance_ret_csr_global
3074               INTO l_balance;
3075             END IF;
3076 
3077             IF G_debug_flag = 'Y'
3078             THEN
3079 	      oe_debug_pub.add
3080 	      ( 'cust global cursor at ' || l_currency_code
3081 	       || ' currency with balance ' || l_balance, 2 );
3082             END IF;
3083 	    BEGIN
3084 	      l_term :=
3085 	        OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
3086 	        ( p_amount                  => l_balance
3087 	        , p_transactional_currency  => l_currency_code
3088 	        , p_limit_currency          => p_limit_curr_code
3089 		, p_functional_currency     => g_functional_currency
3090 		, p_conversion_date         => SYSDATE
3091 		, p_conversion_type         => g_conversion_type
3092 		);
3093 	      l_total := l_total + NVL( l_term, 0 );
3094 
3095 	     EXCEPTION
3096 	      WHEN  GL_CURRENCY_API.NO_RATE  OR
3097                     GL_CURRENCY_API.INVALID_CURRENCY THEN
3098 	        oe_debug_pub.add( 'conversion exception for '
3099                     || l_currency_code, 1 );
3100 	        add_error_currency( x_error_curr_tbl, l_currency_code );
3101 	      END;
3102 
3103             ----change for the Return project
3104             IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
3105             THEN
3106               CLOSE cust_balance_csr_global;
3107             ELSE
3108               CLOSE cust_balance_ret_csr_global;
3109             END IF;
3110 
3111             j  :=  p_binary_tbl.NEXT(j);
3112 	   END LOOP;
3113 
3114          ELSE  -- not global cck
3115           IF G_debug_flag = 'Y'
3116           THEN
3117             oe_debug_pub.add( 'Into cust NOT global ');
3118           END IF;
3119 
3120           ----change for the Return project
3121           IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
3122           THEN
3123 
3124             OPEN customer_balance_stub_csr;
3125 	    FETCH customer_balance_stub_csr
3126             INTO l_balance;
3127           ELSE
3128             OPEN customer_balance_stub_ret_csr;
3129 	    FETCH customer_balance_stub_ret_csr
3130             INTO l_balance;
3131           END IF;
3132 
3133           IF p_credit_check_rule_rec.open_ar_days IS NOT NULL
3134             AND p_credit_check_rule_rec.open_ar_balance_flag = 'Y'
3135             AND b8 = G_INVOICES
3136           THEN
3137            OPEN customer_br_stub_csr ;
3138            FETCH customer_br_stub_csr
3139            INTO l_br_balance ;
3140            CLOSE customer_br_stub_csr ;
3141           END IF;
3142 
3143           IF G_debug_flag = 'Y'
3144           THEN
3145 	    oe_debug_pub.add
3146 	      ( 'cust stub cursor at ' || l_currency_code
3147 	       || ' currency with balance ' || l_balance, 2 );
3148 	    oe_debug_pub.add
3149 	      ( 'cust stub cursor at ' || l_currency_code
3150 	       || ' currency with BR balance ' || l_br_balance, 2 );
3151           END IF;
3152 
3153 	    BEGIN
3154 	      l_term :=
3155 	        OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
3156 	        ( p_amount                  => l_balance +
3157                              NVL(l_br_balance,0)
3158 	        , p_transactional_currency  => l_currency_code
3159 	        , p_limit_currency          => p_limit_curr_code
3160 	        , p_functional_currency     => g_functional_currency
3161 	        , p_conversion_date         => SYSDATE
3162 	        , p_conversion_type         => g_conversion_type
3163 	        );
3164 	      l_total := l_total + NVL( l_term, 0 );
3165 	      EXCEPTION
3166 	      WHEN  GL_CURRENCY_API.NO_RATE  OR
3167                      GL_CURRENCY_API.INVALID_CURRENCY THEN
3168 	        oe_debug_pub.add( 'conversion exception for '
3169                   || l_currency_code, 1 );
3170 		add_error_currency( x_error_curr_tbl, l_currency_code );
3171 	     END;
3172 
3173              ----change for the Return project
3174              IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
3175              THEN
3176                CLOSE customer_balance_stub_csr ;
3177              ELSE
3178                CLOSE customer_balance_stub_ret_csr;
3179              END IF;
3180 
3181 	  -----------------------------
3182 	  -- for fraction of Main Bucket
3183 
3184 	    j  :=  p_binary_tbl.FIRST;
3185 	    WHILE  j  IS NOT NULL
3186             LOOP
3187 	     l_bucket         :=  p_binary_tbl(j).bucket;
3188 	     l_bucket_length  :=  p_binary_tbl(j).bucket_length;
3189 
3190               ----change for the Return project
3191               IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
3192               THEN
3193 	        OPEN customer_balance_csr;
3194 	        FETCH customer_balance_csr
3195                 INTO l_balance;
3196               ELSE
3197                 OPEN customer_balance_ret_csr;
3198 	        FETCH customer_balance_ret_csr
3199                 INTO l_balance;
3200               END IF;
3201 
3202               IF G_debug_flag = 'Y'
3203               THEN
3204 	        oe_debug_pub.add
3205 	        ( 'cust cursor at ' || l_currency_code
3206 	       || ' currency with balance ' || l_balance, 2 );
3207               END IF;
3208 
3209 	      BEGIN
3210               l_term :=
3211 	        OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
3212 	        ( p_amount                  => l_balance
3213 	        , p_transactional_currency  => l_currency_code
3214 	        , p_limit_currency          => p_limit_curr_code
3215                 , p_functional_currency     => g_functional_currency
3216 	        , p_conversion_date         => SYSDATE
3217 	        , p_conversion_type         => g_conversion_type
3218 	        );
3219 	      l_total := l_total + NVL( l_term, 0 );
3220 
3221 	      EXCEPTION
3222 	        WHEN  GL_CURRENCY_API.NO_RATE  OR
3223                      GL_CURRENCY_API.INVALID_CURRENCY THEN
3224 	        oe_debug_pub.add( 'conversion exception for '
3225                 || l_currency_code, 1 );
3226 	        add_error_currency( x_error_curr_tbl, l_currency_code );
3227 	      END;
3228 
3229               ----change for the Return project
3230               IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
3231               THEN
3232 	        CLOSE customer_balance_csr;
3233               ELSE
3234                 CLOSE customer_balance_ret_csr;
3235               END IF;
3236 
3237               j  :=  p_binary_tbl.NEXT(j);
3238 	    END LOOP;
3239           END IF; -- global
3240           i := p_usage_curr_tbl.NEXT(i);
3241         END LOOP;  -- i loop
3242 
3243     ELSE -- customer level all currencies
3244       -- exposure is at customer level and for all currencies
3245       -- for each currency in the list
3246 
3247       IF G_debug_flag = 'Y'
3248       THEN
3249         oe_debug_pub.add(' Into ALL currencies ');
3250       END IF;
3251 
3252 
3253       i  := 0;
3254       --------------------------
3255       -- for left of Main Bucket
3256       l_bucket         :=  p_main_bucket;
3257       l_bucket_length  :=  G_MAX_BUCKET_LENGTH;
3258 
3259       IF p_global_exposure_flag = 'Y'
3260       THEN
3261         IF G_debug_flag = 'Y'
3262         THEN
3263           oe_debug_pub.add( ' Into cust GLOBAL');
3264         END IF;
3265 
3266        ----change for the Return project
3267        IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
3268        THEN
3269          -----returns are not included
3270          OPEN cust_all_curr_stub_csr_global;
3271          FETCH cust_all_curr_stub_csr_global
3272          INTO l_balance, l_currency_code;
3273             -- For each currency in the cursor
3274 
3275             WHILE NOT cust_all_curr_stub_csr_global%NOTFOUND
3276             LOOP
3277               IF G_debug_flag = 'Y'
3278               THEN
3279                 oe_debug_pub.add
3280 	        ( '(all) stub global cursor at ' || l_currency_code
3281                    || ' currency with balance '
3282 	        || l_balance || ' and bucket/length '
3283                 || l_bucket || '/' || l_bucket_length, 2 );
3284               END IF;
3285 
3286 	      IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
3287                       l_currency_code ,1,1),0) = 0 )
3288 	        OR
3289 	        ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
3290                     l_currency_code ,1,1),0) > 0 )
3291 	          AND ( l_currency_code = p_limit_curr_code )
3292 	        )
3293               THEN
3294                 BEGIN
3295 	          l_term :=
3296 	            OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
3297 	            ( p_amount                  => l_balance
3298 	            , p_transactional_currency  => l_currency_code
3299 	            , p_limit_currency          => p_limit_curr_code
3300 	            , p_functional_currency     => g_functional_currency
3301 		    , p_conversion_date         => SYSDATE
3302 		    , p_conversion_type         => g_conversion_type
3303 		    );
3304 	          l_total := l_total + NVL( l_term, 0 );
3305 
3306 	        EXCEPTION
3307 	          WHEN  GL_CURRENCY_API.NO_RATE
3308                        OR  GL_CURRENCY_API.INVALID_CURRENCY THEN
3309 	            oe_debug_pub.add( 'conversion exception for '
3310                        || l_currency_code, 1 );
3311 	            add_error_currency( x_error_curr_tbl, l_currency_code );
3312 	        END;
3313               ELSE
3314 	        oe_debug_pub.add( 'Currency excluded from usages ');
3315               END IF; -- exclude curr list
3316               FETCH cust_all_curr_stub_csr_global
3317               INTO l_balance, l_currency_code;
3318               i := i + 1;
3319             END LOOP;
3320 
3321             CLOSE cust_all_curr_stub_csr_global;
3322 
3323           -----returns are included
3324           ELSE
3325             OPEN cust_all_curr_stub_ret_csr_gl;
3326             FETCH cust_all_curr_stub_ret_csr_gl
3327             INTO l_balance, l_currency_code;
3328               -- For each currency in the cursor
3329 
3330               WHILE NOT cust_all_curr_stub_ret_csr_gl%NOTFOUND
3331               LOOP
3332                 IF G_debug_flag = 'Y'
3333                 THEN
3334                   oe_debug_pub.add
3335 	          ( '(all) stub global cursor at ' || l_currency_code
3336                      || ' currency with balance '
3337 	          || l_balance || ' and bucket/length '
3338                   || l_bucket || '/' || l_bucket_length, 2 );
3339                 END IF;
3340 
3341 	        IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
3342                         l_currency_code ,1,1),0) = 0 )
3343 	          OR
3344 	          ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
3345                       l_currency_code ,1,1),0) > 0 )
3346 	            AND ( l_currency_code = p_limit_curr_code )
3347 	          )
3348                 THEN
3349                   BEGIN
3350 	            l_term :=
3351 	              OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
3352 	              ( p_amount                  => l_balance
3353 	              , p_transactional_currency  => l_currency_code
3354 	              , p_limit_currency          => p_limit_curr_code
3355 	              , p_functional_currency     => g_functional_currency
3356 		      , p_conversion_date         => SYSDATE
3357 		      , p_conversion_type         => g_conversion_type
3358 		      );
3359 	            l_total := l_total + NVL( l_term, 0 );
3360 
3361 	          EXCEPTION
3362 	            WHEN  GL_CURRENCY_API.NO_RATE
3363                          OR  GL_CURRENCY_API.INVALID_CURRENCY THEN
3364 	              oe_debug_pub.add( 'conversion exception for '
3365                          || l_currency_code, 1 );
3366 	              add_error_currency( x_error_curr_tbl, l_currency_code );
3367 	          END;
3368                 ELSE
3369 	          oe_debug_pub.add( 'Currency excluded from usages ');
3370                 END IF; -- exclude curr list
3371 
3372                 FETCH cust_all_curr_stub_ret_csr_gl
3373                 INTO l_balance, l_currency_code;
3374                 i := i + 1;
3375               END LOOP;
3376 
3377               CLOSE cust_all_curr_stub_ret_csr_gl;
3378 
3379           END IF;-----end of checking if returns are included
3380 
3381 ------------------------start  BR --------------------
3382           IF p_credit_check_rule_rec.open_ar_days IS NOT NULL
3383              AND p_credit_check_rule_rec.open_ar_balance_flag = 'Y'
3384              AND b8 = G_INVOICES
3385           THEN
3386 
3387            i  := 0;
3388                --------------------------
3389             -- for left of Main Bucket
3390             l_bucket         :=  p_main_bucket;
3391             l_bucket_length  :=  G_MAX_BUCKET_LENGTH;
3392 
3393            IF G_debug_flag = 'Y'
3394            THEN
3395              oe_debug_pub.add( ' Into cust GLOBAL');
3396            END IF;
3397 
3398             OPEN cust_br_all_curr_stub_csr_gl;
3399             FETCH cust_br_all_curr_stub_csr_gl
3400             INTO l_br_balance, l_currency_code;
3401 
3402           -- For each currency in the cursor
3403 
3404               WHILE NOT cust_br_all_curr_stub_csr_gl%NOTFOUND
3405               LOOP
3406               IF G_debug_flag = 'Y'
3407               THEN
3408                 oe_debug_pub.add
3409 	      ( '(all) stub global cursor at ' || l_currency_code
3410                  || ' currency with BR balance '
3411 	      || l_br_balance || ' and bucket/length '
3412               || l_bucket || '/' || l_bucket_length, 2 );
3416 	       IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
3413               END IF;
3414 
3415 
3417                     l_currency_code ,1,1),0) = 0 )
3418 	      OR
3419 	      ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
3420                   l_currency_code ,1,1),0) > 0 )
3421 	        AND ( l_currency_code = p_limit_curr_code )
3422 	      )
3423               THEN
3424                 BEGIN
3425 	        l_term :=
3426 	          OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
3427 	          ( p_amount                  => l_br_balance
3428 	          , p_transactional_currency  => l_currency_code
3429 	          , p_limit_currency          => p_limit_curr_code
3430 	          , p_functional_currency     => g_functional_currency
3431 		  , p_conversion_date         => SYSDATE
3432 		  , p_conversion_type         => g_conversion_type
3433 		  );
3434 	        l_total := l_total + NVL( l_term, 0 );
3435 
3436 	        EXCEPTION
3437 	          WHEN  GL_CURRENCY_API.NO_RATE
3438                      OR  GL_CURRENCY_API.INVALID_CURRENCY THEN
3439 	          oe_debug_pub.add( 'conversion exception for '
3440                      || l_currency_code, 1 );
3441 	          add_error_currency( x_error_curr_tbl, l_currency_code );
3442 	        END;
3443               ELSE
3444 	        oe_debug_pub.add( 'Currency excluded from usages ');
3445               END IF; -- exclude curr list
3446               FETCH cust_br_all_curr_stub_csr_gl
3447                INTO l_br_balance, l_currency_code;
3448                  i := i + 1;
3449             END LOOP;
3450 
3451           CLOSE cust_br_all_curr_stub_csr_gl ;
3452        END IF;
3453 ---------------------------- end  BR ------------------------
3454 
3455         -----------------------------
3456 	-- for fraction of Main Bucket
3457 
3458         j  :=  p_binary_tbl.FIRST;
3459         WHILE  j  IS NOT NULL LOOP
3460 	  l_bucket         :=  p_binary_tbl(j).bucket;
3461 	  l_bucket_length  :=  p_binary_tbl(j).bucket_length;
3462 
3463           ----change for the Return project
3464           IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
3465           THEN
3466             -----returns are not included
3467 	    OPEN cust_all_curr_csr_global;
3468   	    FETCH cust_all_curr_csr_global
3469             INTO l_balance, l_currency_code;
3470 
3471 	    -- For each currency that exists, the cursor returns a row.
3472 	    -- Convert the balance and sum up to total exposure
3473 
3474 	    WHILE NOT cust_all_curr_csr_global%NOTFOUND
3475             LOOP
3476               IF G_debug_flag = 'Y'
3477               THEN
3478 	       oe_debug_pub.add
3479 	        ( 'cust all global cursor at ' || l_currency_code
3480                    || ' currency with balance '
3481 	          || l_balance || ' and bucket/length '
3482                   || l_bucket || '/' || l_bucket_length, 2 );
3483 
3484               END IF;
3485 
3486 	      IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
3487                   l_currency_code ,1,1),0) = 0 )
3488                 OR
3489 	         ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
3490                   l_currency_code ,1,1),0) > 0 )
3491 	           AND ( l_currency_code = p_limit_curr_code )
3492 	         )
3493 	      THEN
3494 	        BEGIN
3495 	          l_term :=
3496                     OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
3497 	            ( p_amount                  => l_balance
3498 	            , p_transactional_currency  => l_currency_code
3499 	            , p_limit_currency          => p_limit_curr_code
3500  	            , p_functional_currency     => g_functional_currency
3501 	            , p_conversion_date         => SYSDATE
3502 	            , p_conversion_type         => g_conversion_type
3503 	            );
3504 	          l_total := l_total + NVL( l_term, 0 );
3505 
3506 	        EXCEPTION
3507 	          WHEN  GL_CURRENCY_API.NO_RATE  OR
3508                          GL_CURRENCY_API.INVALID_CURRENCY THEN
3509 	            oe_debug_pub.add( 'conversion exception for ' ||
3510                       l_currency_code, 1 );
3511 	            add_error_currency( x_error_curr_tbl, l_currency_code );
3512 	        END;
3513               ELSE
3514 	        oe_debug_pub.add( 'Currency excluded from usages ');
3515 	      END IF;  -- exclude curr list
3516               FETCH cust_all_curr_csr_global INTO l_balance, l_currency_code;
3517               i := i + 1;
3518             END LOOP;
3519 	    CLOSE cust_all_curr_csr_global;
3520 
3521           -----returns are included
3522           ELSE
3523             OPEN cust_all_curr_ret_csr_global;
3524   	    FETCH cust_all_curr_ret_csr_global
3525             INTO l_balance, l_currency_code;
3526 
3527 	    -- For each currency that exists, the cursor returns a row.
3528 	    -- Convert the balance and sum up to total exposure
3529 
3530 	    WHILE NOT cust_all_curr_ret_csr_global%NOTFOUND
3531             LOOP
3532               IF G_debug_flag = 'Y'
3533               THEN
3534 	       oe_debug_pub.add
3535 	        ( 'cust all global cursor at ' || l_currency_code
3536                    || ' currency with balance '
3537 	          || l_balance || ' and bucket/length '
3538                   || l_bucket || '/' || l_bucket_length, 2 );
3539 
3543                   l_currency_code ,1,1),0) = 0 )
3540               END IF;
3541 
3542 	      IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
3544                 OR
3545 	         ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
3546                   l_currency_code ,1,1),0) > 0 )
3547 	           AND ( l_currency_code = p_limit_curr_code )
3548 	         )
3549 	      THEN
3550 	        BEGIN
3551 	          l_term :=
3552                     OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
3553 	            ( p_amount                  => l_balance
3554 	            , p_transactional_currency  => l_currency_code
3555 	            , p_limit_currency          => p_limit_curr_code
3556  	            , p_functional_currency     => g_functional_currency
3557 	            , p_conversion_date         => SYSDATE
3558 	            , p_conversion_type         => g_conversion_type
3559 	            );
3560 	          l_total := l_total + NVL( l_term, 0 );
3561 
3562 	        EXCEPTION
3563 	          WHEN  GL_CURRENCY_API.NO_RATE  OR
3564                          GL_CURRENCY_API.INVALID_CURRENCY THEN
3565 	            oe_debug_pub.add( 'conversion exception for ' ||
3566                       l_currency_code, 1 );
3567 	            add_error_currency( x_error_curr_tbl, l_currency_code );
3568 	        END;
3569               ELSE
3570 	        oe_debug_pub.add( 'Currency excluded from usages ');
3571 	      END IF;  -- exclude curr list
3572 
3573               FETCH cust_all_curr_ret_csr_global
3574               INTO l_balance, l_currency_code;
3575               i := i + 1;
3576             END LOOP;
3577 
3578 	    CLOSE cust_all_curr_ret_csr_global;
3579           END IF; ----end of checking if returns are included
3580 
3581           j  :=  p_binary_tbl.NEXT(j);
3582         END LOOP;  -- J
3583 
3584 
3585       ELSE  -- not global cck
3586         IF G_debug_flag = 'Y'
3587         THEN
3588          oe_debug_pub.add(' Into cust NOT  global ');
3589         END IF;
3590 
3591         ----change for the Return project
3592         IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
3593         THEN
3594           ----returns are not included, old code
3595           OPEN customer_all_curr_stub_csr;
3596 	  FETCH customer_all_curr_stub_csr
3597           INTO l_balance, l_currency_code;
3598             -- For each currency in the cursor
3599 
3600    	    WHILE NOT customer_all_curr_stub_csr%NOTFOUND
3601              LOOP
3602               IF G_debug_flag = 'Y'
3603               THEN
3604 	        oe_debug_pub.add
3605 	        ( '(all) stub cursor at ' || l_currency_code
3606                        || ' currency with balance '
3607 	          || l_balance || ' and bucket/length '
3608                     || l_bucket || '/' || l_bucket_length, 2 );
3609               END IF;
3610 
3611 	      IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
3612                     l_currency_code ,1,1),0) = 0 )
3613 	          OR
3614 	         ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
3615                        l_currency_code ,1,1),0) > 0 )
3616 		  AND ( l_currency_code = p_limit_curr_code )
3617 	         )
3618 	      THEN
3619 	        BEGIN
3620 	          l_term :=
3621 	            OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
3622 	            ( p_amount                  => l_balance
3623 	            , p_transactional_currency  => l_currency_code
3624 	            , p_limit_currency          => p_limit_curr_code
3625 	            , p_functional_currency     => g_functional_currency
3626 	            , p_conversion_date         => SYSDATE
3627 	            , p_conversion_type         => g_conversion_type
3628 	            );
3629 	          l_total := l_total + NVL( l_term, 0 );
3630 
3631 	       EXCEPTION
3632 	          WHEN  GL_CURRENCY_API.NO_RATE
3633                        OR  GL_CURRENCY_API.INVALID_CURRENCY THEN
3634 	            oe_debug_pub.add( 'conversion exception for '
3635                       || l_currency_code, 1 );
3636 	            add_error_currency( x_error_curr_tbl, l_currency_code );
3637   	        END;
3638 	      ELSE
3639 	        oe_debug_pub.add(' Currency excluded from usages list ');
3640 	      END IF;
3641 
3642                 FETCH customer_all_curr_stub_csr
3643                 INTO l_balance, l_currency_code;
3644                 i := i + 1;
3645               END LOOP;
3646 
3647               CLOSE customer_all_curr_stub_csr;
3648 
3649             ------returns are included, the same code as above but
3650             ------ using different cursors
3651             ELSE
3652               OPEN customer_all_curr_stub_ret_csr;
3653 	      FETCH customer_all_curr_stub_ret_csr
3654               INTO l_balance, l_currency_code;
3655 
3656               -- For each currency in the cursor
3657 
3658    	      WHILE NOT customer_all_curr_stub_ret_csr%NOTFOUND
3659                LOOP
3660                 IF G_debug_flag = 'Y'
3661                 THEN
3662 	          oe_debug_pub.add
3663 	          ( '(all) stub cursor at ' || l_currency_code
3664                          || ' currency with balance '
3665 	            || l_balance || ' and bucket/length '
3666                       || l_bucket || '/' || l_bucket_length, 2 );
3667                 END IF;
3668 
3669 	        IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
3673                          l_currency_code ,1,1),0) > 0 )
3670                       l_currency_code ,1,1),0) = 0 )
3671 	            OR
3672 	           ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
3674 		    AND ( l_currency_code = p_limit_curr_code )
3675 	           )
3676 	        THEN
3677 	          BEGIN
3678 	            l_term :=
3679 	              OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
3680 	              ( p_amount                  => l_balance
3681 	              , p_transactional_currency  => l_currency_code
3682 	              , p_limit_currency          => p_limit_curr_code
3683 	              , p_functional_currency     => g_functional_currency
3684 	              , p_conversion_date         => SYSDATE
3685 	              , p_conversion_type         => g_conversion_type
3686 	              );
3687 	            l_total := l_total + NVL( l_term, 0 );
3688 
3689 	         EXCEPTION
3690 	            WHEN  GL_CURRENCY_API.NO_RATE
3691                          OR  GL_CURRENCY_API.INVALID_CURRENCY THEN
3692 	              oe_debug_pub.add( 'conversion exception for '
3693                         || l_currency_code, 1 );
3694 	              add_error_currency( x_error_curr_tbl, l_currency_code );
3695   	          END;
3696 	        ELSE
3697 	          oe_debug_pub.add(' Currency excluded from usages list ');
3698 	        END IF;
3699 
3700                 FETCH customer_all_curr_stub_ret_csr
3701                 INTO l_balance, l_currency_code;
3702                 i := i + 1;
3703               END LOOP;
3704 
3705               CLOSE customer_all_curr_stub_ret_csr;
3706 
3707             END IF;  ---end of checking if returns are include
3708 
3709 ----------------------- start BR ------------------
3710 
3711         IF p_credit_check_rule_rec.open_ar_days IS NOT NULL
3712          AND p_credit_check_rule_rec.open_ar_balance_flag = 'Y'
3713          AND b8 = G_INVOICES
3714         THEN
3715           OPEN customer_br_all_curr_stub_csr;
3716           FETCH customer_br_all_curr_stub_csr
3717           INTO l_br_balance, l_currency_code;
3718 
3719           -- For each currency in the cursor
3720 
3721 	  WHILE NOT customer_br_all_curr_stub_csr%NOTFOUND
3722           LOOP
3723             IF G_debug_flag = 'Y'
3724             THEN
3725 	     oe_debug_pub.add
3726 	      ( '(all) stub cursor at ' || l_currency_code
3727                      || ' currency with BR balance '
3728 	        || l_br_balance || ' and bucket/length '
3729                   || l_bucket || '/' || l_bucket_length, 2 );
3730             END IF;
3731 
3732 	       IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
3733                   l_currency_code ,1,1),0) = 0 )
3734 	        OR
3735 	       ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
3736                      l_currency_code ,1,1),0) > 0 )
3737 		AND ( l_currency_code = p_limit_curr_code )
3738 	       )
3739 	      THEN
3740 	        BEGIN
3741 	        l_term :=
3742 	          OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
3743 	          ( p_amount                  => l_br_balance
3744 	          , p_transactional_currency  => l_currency_code
3745 	          , p_limit_currency          => p_limit_curr_code
3746 	          , p_functional_currency     => g_functional_currency
3747 	          , p_conversion_date         => SYSDATE
3748 	          , p_conversion_type         => g_conversion_type
3749 	          );
3750 	        l_total := l_total + NVL( l_term, 0 );
3751 
3752 	      EXCEPTION
3753 	        WHEN  GL_CURRENCY_API.NO_RATE
3754                      OR  GL_CURRENCY_API.INVALID_CURRENCY THEN
3755 	          oe_debug_pub.add( 'conversion exception for '
3756                     || l_currency_code, 1 );
3757 	          add_error_currency( x_error_curr_tbl, l_currency_code );
3758   	      END;
3759 	    ELSE
3760 	      oe_debug_pub.add(' Currency excluded from usages list ');
3761 	    END IF;
3762 
3763             FETCH customer_br_all_curr_stub_csr
3764             INTO l_br_balance, l_currency_code;
3765             i := i + 1;
3766           END LOOP;
3767           CLOSE customer_br_all_curr_stub_csr ;
3768         END IF;
3769 
3770 -------------------------- end BR --------------------------
3771 
3772         -----------------------------
3773         -- for fraction of Main Bucket
3774 
3775         j  :=  p_binary_tbl.FIRST;
3776 	WHILE  j  IS NOT NULL
3777         LOOP
3778  	  l_bucket         :=  p_binary_tbl(j).bucket;
3779 	  l_bucket_length  :=  p_binary_tbl(j).bucket_length;
3780 
3781           ----change for the Return project
3782           IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
3783           THEN
3784             ----returns are not included, old code
3785 	    OPEN customer_balance_all_curr_csr;
3786 	    FETCH customer_balance_all_curr_csr
3787             INTO l_balance, l_currency_code;
3788 
3789 	    -- For each currency in the cursor
3790 	    WHILE NOT customer_balance_all_curr_csr%NOTFOUND
3791             LOOP
3792               IF G_debug_flag = 'Y'
3793               THEN
3794 	        oe_debug_pub.add
3795 	        ( '(all) cursor at ' || l_currency_code
3796                     || ' currency with balance '
3797 	          || l_balance || ' and bucket/length '
3798                   || l_bucket || '/' || l_bucket_length, 2 );
3799               END IF;
3800 
3804 	         ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
3801 	      IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
3802                     l_currency_code ,1,1),0) = 0 )
3803 	        OR
3805                      l_currency_code ,1,1),0) > 0 )
3806 	          AND ( l_currency_code = p_limit_curr_code )
3807 	         )
3808 	      THEN
3809 	        BEGIN
3810 	          l_term :=
3811 	            OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
3812 	            ( p_amount                  => l_balance
3813 	            , p_transactional_currency  => l_currency_code
3814 	            , p_limit_currency          => p_limit_curr_code
3815 	            , p_functional_currency     => g_functional_currency
3816 	            , p_conversion_date         => SYSDATE
3817 	            , p_conversion_type         => g_conversion_type
3818 	            );
3819                   l_total := l_total + NVL( l_term, 0 );
3820 
3821 	        EXCEPTION
3822 	          WHEN  GL_CURRENCY_API.NO_RATE  OR
3823                         GL_CURRENCY_API.INVALID_CURRENCY THEN
3824 
3825 	            oe_debug_pub.add( 'conversion exception for '
3826                     || l_currency_code, 1 );
3827 	            add_error_currency( x_error_curr_tbl, l_currency_code );
3828  	        END;
3829               ELSE
3830 	        oe_debug_pub.add(' Currency excluded from usages list ');
3831 	      END IF;
3832 
3833               FETCH customer_balance_all_curr_csr
3834               INTO l_balance, l_currency_code;
3835 
3836               i := i + 1;
3837             END LOOP;
3838             CLOSE customer_balance_all_curr_csr;
3839 
3840             ------returns are included
3841             ELSE
3842               OPEN customer_bal_all_curr_ret_csr;
3843 	      FETCH customer_bal_all_curr_ret_csr
3844               INTO l_balance, l_currency_code;
3845 
3846 	      -- For each currency in the cursor
3847 	      WHILE NOT customer_bal_all_curr_ret_csr%NOTFOUND
3848               LOOP
3849                 IF G_debug_flag = 'Y'
3850                 THEN
3851 	          oe_debug_pub.add
3852 	          ( '(all) cursor at ' || l_currency_code
3853                       || ' currency with balance '
3854 	            || l_balance || ' and bucket/length '
3855                     || l_bucket || '/' || l_bucket_length, 2 );
3856                 END IF;
3857 
3858 	        IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
3859                       l_currency_code ,1,1),0) = 0 )
3860 	          OR
3861 	           ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
3862                        l_currency_code ,1,1),0) > 0 )
3863 	            AND ( l_currency_code = p_limit_curr_code )
3864 	           )
3865 	        THEN
3866 	          BEGIN
3867 	            l_term :=
3868 	              OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
3869 	              ( p_amount                  => l_balance
3870 	              , p_transactional_currency  => l_currency_code
3871 	              , p_limit_currency          => p_limit_curr_code
3872 	              , p_functional_currency     => g_functional_currency
3873 	              , p_conversion_date         => SYSDATE
3874 	              , p_conversion_type         => g_conversion_type
3875 	              );
3876                     l_total := l_total + NVL( l_term, 0 );
3877 
3878 	          EXCEPTION
3879 	            WHEN  GL_CURRENCY_API.NO_RATE  OR
3880                           GL_CURRENCY_API.INVALID_CURRENCY THEN
3881 
3882 	              oe_debug_pub.add( 'conversion exception for '
3883                       || l_currency_code, 1 );
3884 	              add_error_currency( x_error_curr_tbl, l_currency_code );
3885  	          END;
3886                 ELSE
3887 	          oe_debug_pub.add(' Currency excluded from usages list ');
3888 	        END IF;
3889 
3890                 FETCH customer_bal_all_curr_ret_csr
3891                 INTO l_balance, l_currency_code;
3892 
3893                 i := i + 1;
3894               END LOOP;
3895 
3896               CLOSE customer_bal_all_curr_ret_csr;
3897 
3898             END IF; ------end of checking if returns are included
3899 
3900 	    j  :=  p_binary_tbl.NEXT(j);
3901           END LOOP; -- J
3902 
3903       END IF; -- global/not global
3904     END IF; -- all currency
3905 
3906 ----------------------- END customer -----------------------------
3907 ------------------------------------------------------------------
3908 --------------------------Party-----------------------------------
3909 -------------------------------------------------------------------
3910   ELSIF p_party_id is NOT NULL
3911   THEN
3912 
3913     IF G_debug_flag = 'Y'
3914     THEN
3915       oe_debug_pub.add( 'exposure at Party level ',1);
3916       oe_debug_pub.add( 'g_use_party_hierarchy => '|| g_use_party_hierarchy );
3917     END IF;
3918 
3919     IF  NVL(p_include_all_flag,'N')  =  'N'
3920     THEN
3921       IF G_debug_flag = 'Y'
3922       THEN
3923         oe_debug_pub.add( 'NOT ALL currencies ');
3924       END IF;
3925 
3926       IF g_use_party_hierarchy = 'N'
3927       THEN
3928         i  := p_usage_curr_tbl.FIRST;
3929         WHILE  i  IS NOT NULL
3930         LOOP
3931           l_currency_code := p_usage_curr_tbl(i).usage_curr_code;
3932         --------------------------
3936 
3933         -- for left of Main Bucket
3934 	  l_bucket         :=  p_main_bucket;
3935 	  l_bucket_length  :=  G_MAX_BUCKET_LENGTH;
3937           ----change for the Return project
3938           IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
3939           THEN
3940             -----returns are not included
3941 	    OPEN party_balance_stub_csr_global ;
3942 	    FETCH party_balance_stub_csr_global
3943             INTO l_balance;
3944 
3945           -----returns are included
3946           ELSE
3947             OPEN party_bal_stub_ret_csr_global ;
3948 	    FETCH party_bal_stub_ret_csr_global
3949             INTO l_balance;
3950           END IF;
3951 
3952            IF p_credit_check_rule_rec.open_ar_days IS NOT NULL
3953                AND p_credit_check_rule_rec.open_ar_balance_flag = 'Y'
3954                AND b8 = G_INVOICES
3955            THEN
3956              OPEN party_br_stub_csr_global ;
3957              FETCH party_br_stub_csr_global
3958              INTO l_br_balance ;
3959 
3960              CLOSE party_br_stub_csr_global ;
3961            END IF;
3962 
3963           IF G_debug_flag = 'Y'
3964           THEN
3965 	    oe_debug_pub.add
3966            ( 'global stub cursor at ' || l_currency_code
3967 	        || ' currency with balance ' || l_balance, 2 );
3968 	    oe_debug_pub.add
3969            ( 'global stub cursor at ' || l_currency_code
3970 	        || ' currency with  BR balance ' || l_br_balance, 2 );
3971            END IF;
3972             BEGIN
3973 	      l_term :=
3974 	        OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
3975 	        ( p_amount                  => l_balance +
3976                                NVL(l_br_balance,0)
3977 	        , p_transactional_currency  => l_currency_code
3978 	        , p_limit_currency          => p_limit_curr_code
3979 	        , p_functional_currency     => g_functional_currency
3980 	        , p_conversion_date         => SYSDATE
3981 	        , p_conversion_type         => g_conversion_type
3982 	        );
3983   	      l_total := l_total + NVL( l_term, 0 );
3984 
3985 	    EXCEPTION
3986 	      WHEN  GL_CURRENCY_API.NO_RATE  OR
3987                     GL_CURRENCY_API.INVALID_CURRENCY THEN
3988 	        oe_debug_pub.add( 'conversion exception for '
3989                  || l_currency_code, 1 );
3990 	        add_error_currency( x_error_curr_tbl, l_currency_code );
3991             END;
3992 
3993             ----change for the Return project
3994             IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
3995             THEN
3996               CLOSE party_balance_stub_csr_global;
3997             ELSE
3998               CLOSE party_bal_stub_ret_csr_global;
3999             END IF;
4000 
4001 	  -----------------------------
4002 	  -- for fraction of Main Bucket
4003 
4004 	  j  :=  p_binary_tbl.FIRST;
4005 	  WHILE  j  IS NOT NULL
4006           LOOP
4007 	    l_bucket         :=  p_binary_tbl(j).bucket;
4008 	    l_bucket_length  :=  p_binary_tbl(j).bucket_length;
4009 
4010 
4011             ----change for the Return project
4012             IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
4013             THEN
4014 	      OPEN party_balance_csr_global ;
4015 
4016 	      FETCH party_balance_csr_global
4017               INTO l_balance;
4018 
4019             ---include returns
4020             ELSE
4021               OPEN party_balance_ret_csr_global ;
4022 
4023 	      FETCH party_balance_ret_csr_global
4024               INTO l_balance;
4025             END IF;
4026 
4027             IF G_debug_flag = 'Y'
4028             THEN
4029         	    oe_debug_pub.add
4030 	      ( 'global cursor at ' || l_currency_code
4031 	       || ' currency with balance ' || l_balance, 2 );
4032             END IF;
4033 	    BEGIN
4034 	      l_term :=
4035 	        OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
4036 	        ( p_amount                  => l_balance
4037 	        , p_transactional_currency  => l_currency_code
4038 	        , p_limit_currency          => p_limit_curr_code
4039 		, p_functional_currency     => g_functional_currency
4040 		, p_conversion_date         => SYSDATE
4041 		, p_conversion_type         => g_conversion_type
4042 		);
4043 	      l_total := l_total + NVL( l_term, 0 );
4044 
4045 	    EXCEPTION
4046 	      WHEN  GL_CURRENCY_API.NO_RATE  OR
4047                     GL_CURRENCY_API.INVALID_CURRENCY THEN
4048 	        oe_debug_pub.add( 'conversion exception for '
4049                     || l_currency_code, 1 );
4050 	        add_error_currency( x_error_curr_tbl, l_currency_code );
4051 	    END;
4052 
4053             ----change for the Return project
4054             IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
4055             THEN
4056 
4057 	      CLOSE party_balance_csr_global ;
4058             ELSE
4059               CLOSE party_balance_ret_csr_global ;
4060             END IF;
4061 
4062             j  :=  p_binary_tbl.NEXT(j);
4063 
4064 	  END LOOP;
4065 
4066         i := p_usage_curr_tbl.NEXT(i);
4067       END LOOP;  -- i loop
4068 
4069      ELSE --- Hierarchy usages
4070        IF G_debug_flag = 'Y'
4071        THEN
4072          oe_debug_pub.add(' Hierarchy Usages ',1);
4076             G_MAX_BUCKET_LENGTH);
4073          oe_debug_pub.add(' OE_CREDIT_CHECK_UTIL.G_hierarchy_type => '||
4074           OE_CREDIT_CHECK_UTIL.G_hierarchy_type );
4075          oe_debug_pub.add(' G_MAX_BUCKET_LENGTH => '||
4077         END IF;
4078 
4079         i  := p_usage_curr_tbl.FIRST;
4080         WHILE  i  IS NOT NULL
4081         LOOP
4082           l_currency_code := p_usage_curr_tbl(i).usage_curr_code;
4083         --------------------------
4084         -- for left of Main Bucket
4085 	  l_bucket         :=  p_main_bucket;
4086 	  l_bucket_length  :=  G_MAX_BUCKET_LENGTH;
4087 
4088           ---change for the Return project
4089           IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
4090           THEN
4091 	    OPEN party_h_bal_stub_csr_gl ;
4092 	    FETCH party_h_bal_stub_csr_gl
4093             INTO l_balance;
4094           ELSE
4095             OPEN party_h_bal_ret_stub_csr_gl ;
4096 	    FETCH party_h_bal_ret_stub_csr_gl
4097             INTO l_balance;
4098           END IF;
4099 
4100           IF p_credit_check_rule_rec.open_ar_days IS NOT NULL
4101             AND p_credit_check_rule_rec.open_ar_balance_flag = 'Y'
4102             AND b8 = G_INVOICES
4103           THEN
4104             OPEN party_br_h_stub_csr_gl ;
4105             FETCH party_br_h_stub_csr_gl
4106             INTO l_br_balance ;
4107             CLOSE party_br_h_stub_csr_gl ;
4108           END IF;
4109 
4110           IF G_debug_flag = 'Y'
4111           THEN
4112 	   oe_debug_pub.add
4113            ( 'global stub cursor at ' || l_currency_code
4114 	        || ' currency with balance ' || l_balance, 2 );
4115           END IF;
4116 
4117             BEGIN
4118 	      l_term :=
4119 	        OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
4120 	        ( p_amount                  => l_balance
4121                                  + nvl(l_br_balance ,0)
4122 	        , p_transactional_currency  => l_currency_code
4123 	        , p_limit_currency          => p_limit_curr_code
4124 	        , p_functional_currency     => g_functional_currency
4125 	        , p_conversion_date         => SYSDATE
4126 	        , p_conversion_type         => g_conversion_type
4127 	        );
4128   	      l_total := l_total + NVL( l_term, 0 );
4129 
4130 	    EXCEPTION
4131 	      WHEN  GL_CURRENCY_API.NO_RATE  OR
4132                     GL_CURRENCY_API.INVALID_CURRENCY THEN
4133 	        oe_debug_pub.add( 'conversion exception for '
4134                  || l_currency_code, 1 );
4135 	        add_error_currency( x_error_curr_tbl, l_currency_code );
4136             END;
4137 
4138             ---change for the Return project
4139             IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
4140             THEN
4141 	      CLOSE party_h_bal_stub_csr_gl ;
4142             ELSE
4143               CLOSE party_h_bal_ret_stub_csr_gl ;
4144             END IF;
4145 
4146 	  -----------------------------
4147 	  -- for fraction of Main Bucket
4148 
4149 	  j  :=  p_binary_tbl.FIRST;
4150 	  WHILE  j  IS NOT NULL
4151           LOOP
4152 	    l_bucket         :=  p_binary_tbl(j).bucket;
4153 	    l_bucket_length  :=  p_binary_tbl(j).bucket_length;
4154 
4155             ---change for the Return project
4156             IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
4157             THEN
4158 
4159   	      OPEN party_h_balance_csr_global ;
4160 	      FETCH party_h_balance_csr_global
4161               INTO l_balance;
4162             ELSE
4163               OPEN party_h_bal_ret_csr_global ;
4164 	      FETCH party_h_bal_ret_csr_global
4165               INTO l_balance;
4166             END IF;
4167 
4168             IF G_debug_flag = 'Y'
4169             THEN
4170          	    oe_debug_pub.add
4171 	      ( 'global cursor at ' || l_currency_code
4172 	       || ' currency with balance ' || l_balance, 2 );
4173             END IF;
4174 
4175 	    BEGIN
4176 	      l_term :=
4177 	        OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
4178 	        ( p_amount                  => l_balance
4179 	        , p_transactional_currency  => l_currency_code
4180 	        , p_limit_currency          => p_limit_curr_code
4181 		, p_functional_currency     => g_functional_currency
4182 		, p_conversion_date         => SYSDATE
4183 		, p_conversion_type         => g_conversion_type
4184 		);
4185 	      l_total := l_total + NVL( l_term, 0 );
4186 
4187 	    EXCEPTION
4188 	      WHEN  GL_CURRENCY_API.NO_RATE  OR
4189                     GL_CURRENCY_API.INVALID_CURRENCY THEN
4190 	        oe_debug_pub.add( 'conversion exception for '
4191                     || l_currency_code, 1 );
4192 	        add_error_currency( x_error_curr_tbl, l_currency_code );
4193 	    END;
4194 
4195             ---change for the Return project
4196             IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
4197             THEN
4198 	      CLOSE party_h_balance_csr_global ;
4199             ELSE
4200               CLOSE party_h_bal_ret_csr_global ;
4201             END IF;
4202 
4203             j  :=  p_binary_tbl.NEXT(j);
4204 
4205 	  END LOOP;
4206 
4207         i := p_usage_curr_tbl.NEXT(i);
4208       END LOOP;  -- i loop
4209 
4210 
4211      END IF; -- hierarchy check
4215       THEN
4212 
4213     ELSE --  level all currencies
4214       IF G_debug_flag = 'Y'
4216         oe_debug_pub.add(' Into ALL currencies  party ');
4217       END IF;
4218 
4219 
4220       i  := 0;
4221       --------------------------
4222       -- for left of Main Bucket
4223       l_bucket         :=  p_main_bucket;
4224       l_bucket_length  :=  G_MAX_BUCKET_LENGTH;
4225 
4226       IF g_use_party_hierarchy = 'N'
4227       THEN
4228        IF G_debug_flag = 'Y'
4229        THEN
4230          oe_debug_pub.add( ' Into party all curr ');
4231        END IF;
4232 
4233        ---change for the Return project
4234        IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
4235        THEN
4236          ---returns are not included
4237          OPEN party_all_curr_stub_csr_global ;
4238          FETCH party_all_curr_stub_csr_global
4239          INTO l_balance, l_currency_code;
4240             -- For each currency in the cursor
4241 
4242           WHILE NOT party_all_curr_stub_csr_global%NOTFOUND
4243           LOOP
4244             IF G_debug_flag = 'Y'
4245             THEN
4246               oe_debug_pub.add
4247 	        ( '(all) stub global cursor at ' || l_currency_code
4248                    || ' currency with balance '
4249 	        || l_balance || ' and bucket/length '
4250                 || l_bucket || '/' || l_bucket_length, 2 );
4251             END IF;
4252 
4253 	      IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
4254                       l_currency_code ,1,1),0) = 0 )
4255 	        OR
4256 	        ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
4257                     l_currency_code ,1,1),0) > 0 )
4258 	          AND ( l_currency_code = p_limit_curr_code )
4259 	        )
4260               THEN
4261                 BEGIN
4262 	          l_term :=
4263 	            OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
4264 	            ( p_amount                  => l_balance
4265 	            , p_transactional_currency  => l_currency_code
4266 	            , p_limit_currency          => p_limit_curr_code
4267 	            , p_functional_currency     => g_functional_currency
4268 		    , p_conversion_date         => SYSDATE
4269 		    , p_conversion_type         => g_conversion_type
4270 		    );
4271 	          l_total := l_total + NVL( l_term, 0 );
4272 
4273 	        EXCEPTION
4274 	          WHEN  GL_CURRENCY_API.NO_RATE
4275                        OR  GL_CURRENCY_API.INVALID_CURRENCY THEN
4276 	            oe_debug_pub.add( 'conversion exception for '
4277                        || l_currency_code, 1 );
4278 	            add_error_currency( x_error_curr_tbl, l_currency_code );
4279 	        END;
4280               ELSE
4281 	        oe_debug_pub.add( 'Currency excluded from usages ');
4282               END IF; -- exclude curr list
4283 
4284               FETCH party_all_curr_stub_csr_global
4285               INTO l_balance, l_currency_code;
4286 
4287               i := i + 1;
4288           END LOOP;
4289 
4290           CLOSE party_all_curr_stub_csr_global ;
4291 
4292         -----returns are included
4293         ELSE
4294           OPEN party_all_curr_stub_ret_csr_gl ;
4295           FETCH party_all_curr_stub_ret_csr_gl
4296           INTO l_balance, l_currency_code;
4297             -- For each currency in the cursor
4298 
4299           WHILE NOT party_all_curr_stub_ret_csr_gl%NOTFOUND
4300           LOOP
4301             IF G_debug_flag = 'Y'
4302             THEN
4303               oe_debug_pub.add
4304 	        ( '(all) stub global cursor at ' || l_currency_code
4305                    || ' currency with balance '
4306 	        || l_balance || ' and bucket/length '
4307                 || l_bucket || '/' || l_bucket_length, 2 );
4308             END IF;
4309 
4310 	      IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
4311                       l_currency_code ,1,1),0) = 0 )
4312 	        OR
4313 	        ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
4314                     l_currency_code ,1,1),0) > 0 )
4315 	          AND ( l_currency_code = p_limit_curr_code )
4316 	        )
4317               THEN
4318                 BEGIN
4319 	          l_term :=
4320 	            OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
4321 	            ( p_amount                  => l_balance
4322 	            , p_transactional_currency  => l_currency_code
4323 	            , p_limit_currency          => p_limit_curr_code
4324 	            , p_functional_currency     => g_functional_currency
4325 		    , p_conversion_date         => SYSDATE
4326 		    , p_conversion_type         => g_conversion_type
4327 		    );
4328 	          l_total := l_total + NVL( l_term, 0 );
4329 
4330 	        EXCEPTION
4331 	          WHEN  GL_CURRENCY_API.NO_RATE
4332                        OR  GL_CURRENCY_API.INVALID_CURRENCY THEN
4333 	            oe_debug_pub.add( 'conversion exception for '
4334                        || l_currency_code, 1 );
4335 	            add_error_currency( x_error_curr_tbl, l_currency_code );
4336 	        END;
4337               ELSE
4338 	        oe_debug_pub.add( 'Currency excluded from usages ');
4339               END IF; -- exclude curr list
4340 
4341               FETCH party_all_curr_stub_ret_csr_gl
4342               INTO l_balance, l_currency_code;
4343 
4347           CLOSE party_all_curr_stub_ret_csr_gl ;
4344               i := i + 1;
4345           END LOOP;
4346 
4348 
4349         END IF; ---end of checking if returns are included
4350 
4351     --------------------- start  BR ---------------
4352        IF p_credit_check_rule_rec.open_ar_days IS NOT NULL
4353          AND p_credit_check_rule_rec.open_ar_balance_flag = 'Y'
4354         AND b8 = G_INVOICES
4355        THEN
4356 
4357           OPEN party_br_all_curr_stub_csr_gl ;
4358           FETCH party_br_all_curr_stub_csr_gl
4359           INTO l_br_balance, l_currency_code;
4360           -- For each currency in the cursor
4361 
4362           WHILE NOT party_br_all_curr_stub_csr_gl%NOTFOUND
4363           LOOP
4364 
4365             IF G_debug_flag = 'Y'
4366             THEN
4367              oe_debug_pub.add
4368 	      ( '(all) stub global cursor at ' || l_currency_code
4369                  || ' currency with BR balance '
4370 	      || l_br_balance || ' and bucket/length '
4371               || l_bucket || '/' || l_bucket_length, 2 );
4372             END IF;
4373 
4374 	     IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
4375                     l_currency_code ,1,1),0) = 0 )
4376 	      OR
4377 	      ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
4378                   l_currency_code ,1,1),0) > 0 )
4379 	        AND ( l_currency_code = p_limit_curr_code )
4380 	      )
4381             THEN
4382               BEGIN
4383 	        l_term :=
4384 	          OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
4385 	          ( p_amount                  => l_br_balance
4386 	          , p_transactional_currency  => l_currency_code
4387 	          , p_limit_currency          => p_limit_curr_code
4388 	          , p_functional_currency     => g_functional_currency
4389 		  , p_conversion_date         => SYSDATE
4390 		  , p_conversion_type         => g_conversion_type
4391 		  );
4392 	        l_total := l_total + NVL( l_term, 0 );
4393 
4394 	      EXCEPTION
4395 	        WHEN  GL_CURRENCY_API.NO_RATE
4396                      OR  GL_CURRENCY_API.INVALID_CURRENCY THEN
4397 	          oe_debug_pub.add( 'conversion exception for '
4398                      || l_currency_code, 1 );
4399 	          add_error_currency( x_error_curr_tbl, l_currency_code );
4400 	      END;
4401             ELSE
4402 	      oe_debug_pub.add( 'Currency excluded from usages ');
4403             END IF; -- exclude curr list
4404 
4405             FETCH party_br_all_curr_stub_csr_gl
4406             INTO l_br_balance, l_currency_code;
4407 
4408             i := i + 1;
4409         END LOOP;
4410 
4411         CLOSE party_br_all_curr_stub_csr_gl ;
4412 
4413       END IF;
4414 
4415  -----------------------------end BR ------------
4416 	-- for fraction of Main Bucket
4417 
4418         j  :=  p_binary_tbl.FIRST;
4419         WHILE  j  IS NOT NULL LOOP
4420 	  l_bucket         :=  p_binary_tbl(j).bucket;
4421 	  l_bucket_length  :=  p_binary_tbl(j).bucket_length;
4422 
4423           ---change for the Return project
4424           IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
4425           THEN
4426             ---returns are not included
4427 	    OPEN party_all_curr_csr_global ;
4428   	    FETCH party_all_curr_csr_global
4429             INTO l_balance, l_currency_code;
4430 
4431 	    -- For each currency that exists, the cursor returns a row.
4432 	    -- Convert the balance and sum up to total exposure
4433 
4434 	    WHILE NOT party_all_curr_csr_global%NOTFOUND
4435             LOOP
4436 
4437               IF G_debug_flag = 'Y'
4438               THEN
4439 	        oe_debug_pub.add
4440 	        ( 'party all global cursor at ' || l_currency_code
4441                    || ' currency with balance '
4442 	          || l_balance || ' and bucket/length '
4443                   || l_bucket || '/' || l_bucket_length, 2 );
4444               END IF;
4445 
4446 	      IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
4447                   l_currency_code ,1,1),0) = 0 )
4448                 OR
4449 	         ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
4450                   l_currency_code ,1,1),0) > 0 )
4451 	           AND ( l_currency_code = p_limit_curr_code )
4452 	         )
4453 	      THEN
4454 	        BEGIN
4455 	          l_term :=
4456                     OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
4457 	            ( p_amount                  => l_balance
4458 	            , p_transactional_currency  => l_currency_code
4459 	            , p_limit_currency          => p_limit_curr_code
4460  	            , p_functional_currency     => g_functional_currency
4461 	            , p_conversion_date         => SYSDATE
4462 	            , p_conversion_type         => g_conversion_type
4463 	            );
4464 	          l_total := l_total + NVL( l_term, 0 );
4465 
4466 	        EXCEPTION
4467 	          WHEN  GL_CURRENCY_API.NO_RATE  OR
4468                          GL_CURRENCY_API.INVALID_CURRENCY THEN
4469 	            oe_debug_pub.add( 'conversion exception for ' ||
4470                       l_currency_code, 1 );
4471 	            add_error_currency( x_error_curr_tbl, l_currency_code );
4472 	        END;
4473               ELSE
4474 	        oe_debug_pub.add( 'Currency excluded from usages ');
4475 	      END IF;  -- exclude curr list
4476 
4480             END LOOP;
4477               FETCH party_all_curr_csr_global INTO l_balance, l_currency_code;
4478 
4479               i := i + 1;
4481 	    CLOSE party_all_curr_csr_global;
4482 
4483           ----returns are included
4484           ELSE
4485 
4486 	    OPEN party_all_curr_ret_csr_gl ;
4487   	    FETCH party_all_curr_ret_csr_gl
4488             INTO l_balance, l_currency_code;
4489 
4490 	    -- For each currency that exists, the cursor returns a row.
4491 	    -- Convert the balance and sum up to total exposure
4492 
4493 	    WHILE NOT party_all_curr_ret_csr_gl%NOTFOUND
4494             LOOP
4495 
4496               IF G_debug_flag = 'Y'
4497               THEN
4498 	        oe_debug_pub.add
4499 	        ( 'party all global cursor at ' || l_currency_code
4500                    || ' currency with balance '
4501 	          || l_balance || ' and bucket/length '
4502                   || l_bucket || '/' || l_bucket_length, 2 );
4503               END IF;
4504 
4505 	      IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
4506                   l_currency_code ,1,1),0) = 0 )
4507                 OR
4508 	         ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
4509                   l_currency_code ,1,1),0) > 0 )
4510 	           AND ( l_currency_code = p_limit_curr_code )
4511 	         )
4512 	      THEN
4513 	        BEGIN
4514 	          l_term :=
4515                     OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
4516 	            ( p_amount                  => l_balance
4517 	            , p_transactional_currency  => l_currency_code
4518 	            , p_limit_currency          => p_limit_curr_code
4519  	            , p_functional_currency     => g_functional_currency
4520 	            , p_conversion_date         => SYSDATE
4521 	            , p_conversion_type         => g_conversion_type
4522 	            );
4523 	          l_total := l_total + NVL( l_term, 0 );
4524 
4525 	        EXCEPTION
4526 	          WHEN  GL_CURRENCY_API.NO_RATE  OR
4527                          GL_CURRENCY_API.INVALID_CURRENCY THEN
4528 	            oe_debug_pub.add( 'conversion exception for ' ||
4529                       l_currency_code, 1 );
4530 	            add_error_currency( x_error_curr_tbl, l_currency_code );
4531 	        END;
4532               ELSE
4533 	        oe_debug_pub.add( 'Currency excluded from usages ');
4534 	      END IF;  -- exclude curr list
4535 
4536               FETCH party_all_curr_ret_csr_gl
4537               INTO l_balance, l_currency_code;
4538 
4539               i := i + 1;
4540             END LOOP;
4541 
4542 	    CLOSE party_all_curr_ret_csr_gl;
4543 
4544           END IF;  ---end of checking if returns are included
4545 
4546           j  :=  p_binary_tbl.NEXT(j);
4547         END LOOP;  -- J
4548 
4549       ELSE  -- Hierarchy
4550 
4551         IF G_debug_flag = 'Y'
4552         THEN
4553           oe_debug_pub.add(' Into party Hierarchy ',1);
4554         END IF;
4555 
4556         ---change for the Return project
4557         IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
4558         THEN
4559           ---returns are not included
4560           OPEN party_h_all_curr_stub_csr_gl ;
4561 
4562           FETCH party_h_all_curr_stub_csr_gl
4563           INTO l_balance, l_currency_code;
4564 
4565           -- For each currency in the cursor
4566 
4567           WHILE NOT party_h_all_curr_stub_csr_gl%NOTFOUND
4568           LOOP
4569             IF G_debug_flag = 'Y'
4570             THEN
4571               oe_debug_pub.add
4572 	      ( '(all) stub cursor at ' || l_currency_code
4573                       || ' currency with balance '
4574 	      || l_balance || ' and bucket/length '
4575                      || l_bucket || '/' || l_bucket_length, 2 );
4576             END IF;
4577 
4578 	    IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
4579                l_currency_code ,1,1),0) = 0 )
4580 	       OR
4581 	       ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
4582                   l_currency_code ,1,1),0) > 0 )
4583 	       AND ( l_currency_code = p_limit_curr_code )
4584 	        )
4585 	    THEN
4586 	      BEGIN
4587 	        l_term :=
4588 	        OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
4589 	        ( p_amount                  => l_balance
4590 	        , p_transactional_currency  => l_currency_code
4591 	        , p_limit_currency          => p_limit_curr_code
4592 	        , p_functional_currency     => g_functional_currency
4593 	        , p_conversion_date         => SYSDATE
4594 	        , p_conversion_type         => g_conversion_type
4595 	        );
4596 	        l_total := l_total + NVL( l_term, 0 );
4597 
4598 	      EXCEPTION
4599 	        WHEN  GL_CURRENCY_API.NO_RATE
4600                   OR  GL_CURRENCY_API.INVALID_CURRENCY
4601                 THEN
4602 	          oe_debug_pub.add( 'conversion exception for '
4603                   || l_currency_code, 1 );
4604 	          add_error_currency( x_error_curr_tbl, l_currency_code );
4605   	      END;
4606 	    ELSE
4607 	      oe_debug_pub.add(' Currency excluded from usages list ');
4608 	    END IF;
4609 
4610             FETCH party_h_all_curr_stub_csr_gl
4611             INTO l_balance, l_currency_code;
4612             i := i + 1;
4613           END LOOP;
4617         -----returns are included
4614 
4615           CLOSE party_h_all_curr_stub_csr_gl ;
4616 
4618         ELSE
4619           OPEN p_h_all_curr_stub_ret_csr_gl ;
4620 
4621           FETCH p_h_all_curr_stub_ret_csr_gl
4622           INTO l_balance, l_currency_code;
4623 
4624           -- For each currency in the cursor
4625 
4626           WHILE NOT p_h_all_curr_stub_ret_csr_gl%NOTFOUND
4627           LOOP
4628             IF G_debug_flag = 'Y'
4629             THEN
4630               oe_debug_pub.add
4631               ( '(all) stub cursor at ' || l_currency_code
4632                  || ' currency with balance '
4633                  || l_balance || ' and bucket/length '
4634                  || l_bucket || '/' || l_bucket_length, 2 );
4635             END IF;
4636 
4637 	    IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
4638                l_currency_code ,1,1),0) = 0 )
4639 	       OR
4640 	       ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
4641                 l_currency_code ,1,1),0) > 0 )
4642 	       AND ( l_currency_code = p_limit_curr_code )
4643 	        )
4644 	    THEN
4645 	      BEGIN
4646 	        l_term :=
4647 	        OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
4648 	        ( p_amount                  => l_balance
4649 	        , p_transactional_currency  => l_currency_code
4650 	        , p_limit_currency          => p_limit_curr_code
4651 	        , p_functional_currency     => g_functional_currency
4652 	        , p_conversion_date         => SYSDATE
4653 	        , p_conversion_type         => g_conversion_type
4654 	        );
4655 	        l_total := l_total + NVL( l_term, 0 );
4656 
4657 	      EXCEPTION
4658 	        WHEN  GL_CURRENCY_API.NO_RATE
4659                   OR  GL_CURRENCY_API.INVALID_CURRENCY
4660                 THEN
4661 	          oe_debug_pub.add( 'conversion exception for '
4662                   || l_currency_code, 1 );
4663 	           add_error_currency( x_error_curr_tbl, l_currency_code );
4664   	      END;
4665 	    ELSE
4666               oe_debug_pub.add(' Currency excluded from usages list ');
4667             END IF;
4668 
4669             FETCH p_h_all_curr_stub_ret_csr_gl
4670             INTO l_balance, l_currency_code;
4671             i := i + 1;
4672           END LOOP;
4673 
4674           CLOSE p_h_all_curr_stub_ret_csr_gl ;
4675 
4676         END IF; ----end of checking if retruns are included
4677 
4678 ------------------------ BR -------------
4679          IF p_credit_check_rule_rec.open_ar_days IS NOT NULL
4680           AND p_credit_check_rule_rec.open_ar_balance_flag = 'Y'
4681           AND b8 = G_INVOICES
4682          THEN
4683            OPEN party_br_h_all_curr_stub_gl ;
4684 	   FETCH party_br_h_all_curr_stub_gl
4685            INTO l_br_balance, l_currency_code;
4686           -- For each currency in the cursor
4687 
4688 	   WHILE NOT party_br_h_all_curr_stub_gl%NOTFOUND
4689            LOOP
4690              IF G_debug_flag = 'Y'
4691              THEN
4692 	       oe_debug_pub.add
4693 	       ( '(all) stub cursor at ' || l_currency_code
4694                      || ' currency with BR balance '
4695 	        || l_BR_balance || ' and bucket/length '
4696                   || l_bucket || '/' || l_bucket_length, 2 );
4697               END IF;
4698 
4699 	      IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
4700                   l_currency_code ,1,1),0) = 0 )
4701 	        OR
4702 	       ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
4703                      l_currency_code ,1,1),0) > 0 )
4704 		AND ( l_currency_code = p_limit_curr_code )
4705 	       )
4706 	      THEN
4707 	        BEGIN
4708 	        l_term :=
4709 	          OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
4710 	          ( p_amount                  => l_br_balance
4711 	          , p_transactional_currency  => l_currency_code
4712 	          , p_limit_currency          => p_limit_curr_code
4713 	          , p_functional_currency     => g_functional_currency
4714 	          , p_conversion_date         => SYSDATE
4715 	          , p_conversion_type         => g_conversion_type
4716 	          );
4717 	        l_total := l_total + NVL( l_term, 0 );
4718 
4719 	      EXCEPTION
4720 	        WHEN  GL_CURRENCY_API.NO_RATE
4721                      OR  GL_CURRENCY_API.INVALID_CURRENCY THEN
4722 	          oe_debug_pub.add( 'conversion exception for '
4723                     || l_currency_code, 1 );
4724 	          add_error_currency( x_error_curr_tbl, l_currency_code );
4725   	      END;
4726 	    ELSE
4727 	      oe_debug_pub.add(' Currency excluded from usages list ');
4728 	    END IF;
4729 
4730             FETCH party_br_h_all_curr_stub_gl
4731             INTO l_br_balance, l_currency_code;
4732             i := i + 1;
4733           END LOOP;
4734 
4735           CLOSE party_br_h_all_curr_stub_gl ;
4736        END IF;
4737 --------------------- end BR ------------
4738         -- for fraction of Main Bucket
4739 
4740         j  :=  p_binary_tbl.FIRST;
4741 	WHILE  j  IS NOT NULL
4742         LOOP
4743  	  l_bucket         :=  p_binary_tbl(j).bucket;
4744 	  l_bucket_length  :=  p_binary_tbl(j).bucket_length;
4745 
4746           IF NVL(p_credit_check_rule_rec.include_returns_flag,'N') = 'N'
4747           THEN
4748             ---returns are not included
4752             INTO l_balance, l_currency_code;
4749 	    OPEN party_h_all_curr_csr_global ;
4750 
4751 	    FETCH party_h_all_curr_csr_global
4753 
4754 	    -- For each currency in the cursor
4755 	    WHILE NOT party_h_all_curr_csr_global%NOTFOUND
4756             LOOP
4757               IF G_debug_flag = 'Y'
4758               THEN
4759 	        oe_debug_pub.add
4760 	        ( '(all) cursor at ' || l_currency_code
4761                   || ' currency with balance '
4762 	          || l_balance || ' and bucket/length '
4763                   || l_bucket || '/' || l_bucket_length, 2 );
4764               END IF;
4765 
4766 	      IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
4767                   l_currency_code ,1,1),0) = 0 )
4768 	         OR
4769 	         ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
4770                   l_currency_code ,1,1),0) > 0 )
4771 	         AND ( l_currency_code = p_limit_curr_code )
4772 	         )
4773 	      THEN
4774 	        BEGIN
4775 	          l_term :=
4776 	          OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
4777 	          ( p_amount                  => l_balance
4778 	          , p_transactional_currency  => l_currency_code
4779 	          , p_limit_currency          => p_limit_curr_code
4780 	          , p_functional_currency     => g_functional_currency
4781 	          , p_conversion_date         => SYSDATE
4782 	          , p_conversion_type         => g_conversion_type
4783 	          );
4784                   l_total := l_total + NVL( l_term, 0 );
4785 
4786 	        EXCEPTION
4787 	          WHEN  GL_CURRENCY_API.NO_RATE  OR
4788                         GL_CURRENCY_API.INVALID_CURRENCY
4789                   THEN
4790 
4791    	            oe_debug_pub.add( 'conversion exception for '
4792                     || l_currency_code, 1 );
4793 	            add_error_currency( x_error_curr_tbl, l_currency_code );
4794  	        END;
4795               ELSE
4796 	        oe_debug_pub.add(' Currency excluded from usages list ');
4797 	      END IF;
4798 
4799               FETCH party_h_all_curr_csr_global
4800               INTO l_balance, l_currency_code;
4801 
4802               i := i + 1;
4803             END LOOP;
4804             CLOSE party_h_all_curr_csr_global ;
4805 
4806           ---returns are included
4807           ELSE
4808             OPEN party_h_all_curr_ret_csr_gl ;
4809 
4810 	    FETCH party_h_all_curr_ret_csr_gl
4811             INTO l_balance, l_currency_code;
4812 
4813 	    -- For each currency in the cursor
4814 	    WHILE NOT party_h_all_curr_csr_global%NOTFOUND
4815             LOOP
4816               IF G_debug_flag = 'Y'
4817               THEN
4818 	        oe_debug_pub.add
4819 	        ( '(all) cursor at ' || l_currency_code
4820                   || ' currency with balance '
4821 	          || l_balance || ' and bucket/length '
4822                   || l_bucket || '/' || l_bucket_length, 2 );
4823               END IF;
4824 
4825 	      IF ( NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
4826                   l_currency_code ,1,1),0) = 0 )
4827 	         OR
4828 	         ((NVL(INSTRB(OE_CREDIT_CHECK_UTIL.G_excl_curr_list,
4829                   l_currency_code ,1,1),0) > 0 )
4830 	         AND ( l_currency_code = p_limit_curr_code )
4831 	         )
4832 	      THEN
4833 	        BEGIN
4834 	          l_term :=
4835 	          OE_CREDIT_CHECK_UTIL.CONVERT_CURRENCY_AMOUNT
4836 	          ( p_amount                  => l_balance
4837 	          , p_transactional_currency  => l_currency_code
4838 	          , p_limit_currency          => p_limit_curr_code
4839 	          , p_functional_currency     => g_functional_currency
4840 	          , p_conversion_date         => SYSDATE
4841 	          , p_conversion_type         => g_conversion_type
4842 	          );
4843                   l_total := l_total + NVL( l_term, 0 );
4844 
4845 	        EXCEPTION
4846 	          WHEN  GL_CURRENCY_API.NO_RATE  OR
4847                         GL_CURRENCY_API.INVALID_CURRENCY
4848                   THEN
4849 
4850    	            oe_debug_pub.add( 'conversion exception for '
4851                     || l_currency_code, 1 );
4852 	            add_error_currency( x_error_curr_tbl, l_currency_code );
4853  	        END;
4854               ELSE
4855 	        oe_debug_pub.add(' Currency excluded from usages list ');
4856 	      END IF;
4857 
4858               FETCH party_h_all_curr_ret_csr_gl
4859               INTO l_balance, l_currency_code;
4860 
4861               i := i + 1;
4862             END LOOP;
4863             CLOSE party_h_all_curr_ret_csr_gl ;
4864 
4865           END IF; ---end of checking if returns are included
4866 
4867 	  j  :=  p_binary_tbl.NEXT(j);
4868         END LOOP; -- J
4869       END IF; -- global/not global
4870     END IF;
4871 ------------------------- end party ---------------------
4872 --------------------------------------------------------
4873 
4874   END IF; -- top IF level
4875 
4876 
4877   IF G_debug_flag = 'Y'
4878   THEN
4879     oe_debug_pub.add( ' -------------------------------------');
4880     oe_debug_pub.add( ' Final l_total ===> ' || l_total,1 );
4881     oe_debug_pub.add( ' Return out from  retrieve_exposure ');
4882   END IF;
4883 
4884   return NVL(l_total,0) ;
4885 
4886 EXCEPTION
4887 
4891     IF
4888   WHEN OTHERS THEN
4889     oe_debug_pub.add(SQLERRM );
4890 
4892       FND_MSG_PUB.Check_Msg_Level( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )
4893     THEN
4894       FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME, 'retrieve_exposure');
4895     END IF;
4896 
4897 END retrieve_exposure;
4898 
4899 
4900 --========================================================================
4901 -- PROCEDURE : Init_Summary_Table     PUBLIC
4902 -- PARAMETERS: x_retcode              0 success, 1 warning, 2 error
4903 --             x_errbuf               error buffer
4904 --             p_lock_tables          'Y' or 'N' for all transaction tables
4905 ---
4906 -- COMMENT   : This is the concurrent program body for
4907 --             Initialize Credit Summaries Table
4908 --             which will repopulate oe_credit_summaries table with summarized
4909 --             credit exposure information.
4910 --             The p_lock_tables flag specifies if
4911 --             the oe_order_lines_all, oe_order_headers_all,
4912 --             oe_cash_adjustments, ar_payment_schedules_all,
4913 --           ar_cash_receipts_all tables should all be locked in exclusive mode
4914 --             until all of the summary data is obtained.
4915 --             If the flag is not set to 'Y', none of the tables is locked.
4916 -- 08-22-03    Modified to only select lines with payment type cc flag = Y
4917 --=======================================================================--
4918 PROCEDURE  Init_Summary_Table
4919 ( x_retcode        OUT NOCOPY VARCHAR2
4920 , x_errbuf         OUT NOCOPY VARCHAR2
4921 , p_lock_tables    IN  VARCHAR2
4922 )
4923 IS
4924 
4925 l_return_status          VARCHAR2(1);
4926 l_msg_count              NUMBER;
4927 l_msg_data               VARCHAR2(100);
4928 
4929 l_created_by             NUMBER;
4930 l_last_updated_by        NUMBER;
4931 l_last_update_login      NUMBER;
4932 l_request_id             NUMBER;
4933 l_program_application_id NUMBER;
4934 l_program_id             NUMBER;
4935 
4936 l_level                  NUMBER;
4937 l_bucket_length          NUMBER;
4938 
4939 l_ret                    BOOLEAN; --4219133
4940 l_status                 VARCHAR2(30);
4941 l_ont_schema             VARCHAR2(30);
4942 l_industry               VARCHAR2(30);
4943 l_sql_stmnt              VARCHAR2(200);
4944 l_past_due_bal           VARCHAR2(1);
4945 l_order_hold_bal         VARCHAR2(1);
4946 l_freight_bal            VARCHAR2(1);
4947 
4948 CURSOR cc_precalc_rules
4949 IS
4950 SELECT
4951   orders_on_hold_flag
4952 , maximum_days_past_due
4953 , nvl(incl_freight_charges_flag, 'N') incl_freight_charges_flag
4954 FROM oe_credit_check_rules
4955 WHERE nvl(quick_cr_check_flag, 'N') = 'Y'
4956 AND   sysdate between nvl(start_date_active, sysdate)
4957                   and nvl(end_date_active, sysdate);
4958 
4959 
4960 BEGIN
4961 
4962   IF G_debug_flag = 'Y'
4963   THEN
4964     oe_debug_pub.add( 'In OEXVCRXB.Init_Summary_Table ', 1 );
4965   END IF;
4966 
4967 -----------
4968 -- Locks needed for periodic operation (no triggers/table handlers):
4969 --
4970 -- Goal: Read-consistent view accross transaction tables
4971 -- Means: set transaction level read consistency, and only commit after all
4972 --   changes.
4973 --   This can result in ORA-1555: snapshot too old (rollback segment too small)
4974 --   because the rollback segments of all transactions occurring
4975 --   while initialization
4976 --   is in progress are used to reconstruct old (read-consistent) data.
4977 --   See "Multiversion Concurrency Control" in Oracle Concepts manual
4978 --
4979 -- (Note: When periodic initialization occurs
4980 --  while many transactions take place,
4981 -- the rollback segment for the initialization transaction will be hot
4982 -- because it is accessed to reconstruct old data in the summary table.)
4983 -- (Note: Due to the full delete, the rollback segment needs to be at least
4984 --  twice the size of the summary table. The summary table is small however.)
4985 --
4986 -- Compromise: Read-consistent view per transaction tables seperately
4987 -- Tradeoffs:  +rollback segment +(potentially) contention
4988 --             -inconsistencies e.g. for payrisk vs. invoices atomic booking
4989 -- Means: Nothing required
4990 --
4991 -- Goal: consistent state of summary table
4992 -- Means: no commit until all balance types have been updated
4993 
4994 
4995 
4996 -----------
4997 -- Locks needed for init only operation:
4998 -- Goal: No updates to transaction tables so that triggers don't interfere with
4999 --       initialization
5000 -- Means: Exclusive Locks on all transaction tables
5001 -- Compromise 1: Read-consistency or compromise as for periodic operation, while
5002 --             Triggers are switched off
5003 -- Tradeoffs:  Turning triggers on is not guaranteed to
5004 --    coincide with new update,
5005 --             but quite likely
5006 -- Compromise 2: No considerations
5007 -- Tradeoffs:  Triggers fire during the initialization: double counting
5008 
5009 
5010 
5011 
5012 --  TODO: Periodic operation only. Change this once we have triggers/handlers.
5013 
5014 IF p_lock_tables  =  'Y'  THEN
5015 --  SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
5016 
5017 
5018   IF G_debug_flag = 'Y'
5019   THEN
5020     oe_debug_pub.add( 'starting to lock tables', 2 );
5021   END IF;
5022 
5026   LOCK TABLE ar_payment_schedules_all    IN EXCLUSIVE MODE;
5023   LOCK TABLE oe_order_lines_all          IN EXCLUSIVE MODE;
5024   LOCK TABLE oe_order_headers_all        IN EXCLUSIVE MODE;
5025   LOCK TABLE oe_price_adjustments        IN EXCLUSIVE MODE;
5027   LOCK TABLE ar_cash_receipts_all        IN EXCLUSIVE MODE;
5028   LOCK TABLE ar_cash_receipt_history_all IN EXCLUSIVE MODE;
5029 
5030   IF G_debug_flag = 'Y'
5031   THEN
5032     oe_debug_pub.add( 'locked tables', 2 );
5033   END IF;
5034 END IF;
5035 
5036 
5037 
5038 l_created_by                := fnd_global.USER_ID;
5039 l_last_updated_by           := fnd_global.USER_ID;
5040 l_last_update_login         := fnd_global.LOGIN_ID;
5041 
5042 l_request_id                := fnd_global.CONC_REQUEST_ID;
5043 l_program_application_id    := fnd_global.PROG_APPL_ID;
5044 l_program_id                := fnd_global.CONC_PROGRAM_ID;
5045 
5046 
5047 --Bug 4219133:
5048 --If the Profile 'OM: Preserve External Credit Balances' is set to
5049 --Yes,application will use 'delete' and preserve external credit exposure.
5050 --When this profile is set to 'No', application will truncate the table
5051 --before populating the credit exposure.
5052 
5053   IF G_debug_flag = 'Y'
5054   THEN
5055     oe_debug_pub.add( 'Value of the profile OM: Preserve External Credit Balances is  :' || NVL(FND_PROFILE.VALUE('ONT_PRESERVE_EXT_CR_BAL'),'Y'),2);
5056   END IF;
5057 
5058   IF NVL(FND_PROFILE.VALUE('ONT_PRESERVE_EXT_CR_BAL'),'Y') = 'Y' THEN
5059      DELETE FROM oe_credit_summaries
5060      WHERE       balance_type <> 18;
5061   ELSE
5062      l_ret := FND_INSTALLATION.get_app_info('ONT',l_status,l_industry,l_ont_schema); --This API is used to get schema name
5063      l_sql_stmnt := 'truncate table '||l_ont_schema ||'.oe_credit_summaries';
5064      EXECUTE IMMEDIATE l_sql_stmnt;
5065   END IF;
5066 
5067 /* comment out the following code for bug 4219133
5068 -- Do not delete external exposure data
5069 DELETE FROM oe_credit_summaries
5070 WHERE       balance_type <> 18;
5071 */
5072 
5073 
5074   IF G_debug_flag = 'Y'
5075   THEN
5076     oe_debug_pub.add( 'purged summary table at ' || DO_TIME, 2 );
5077   END IF;
5078 
5079   -- Bug 3960616 : Scan all PreCalc CC Rules to find required Balances.
5080   l_past_due_bal := 'N';
5081   l_order_hold_bal := 'N';
5082   l_freight_bal := 'N';
5083 
5084   FOR cc_rules_rec in cc_precalc_rules LOOP
5085 
5086     --If Max Days Past Due set on any Rule, store Past Due Invoice Balance.
5087 
5088     IF cc_rules_rec.maximum_days_past_due is NOT NULL THEN
5089       l_past_due_bal := 'Y';
5090     END IF;
5091 
5092     -- If Orders on Hold set to be excluded on any Rule, store Hold Balances.
5093 
5094     IF cc_rules_rec.orders_on_hold_flag = 'N' THEN
5095       l_order_hold_bal := 'Y';
5096     END IF;
5097 
5098     -- If Freight Charges set to be included on any Rule, store Freight Balances.
5099 
5100     IF cc_rules_rec.incl_freight_charges_flag = 'Y' THEN
5101       l_freight_bal := 'Y';
5102     END IF;
5103 
5104   END LOOP;
5105 
5106   oe_debug_pub.add( 'Create Past Due Invoice Balance : '||l_past_due_bal);
5107   oe_debug_pub.add( 'Create Orders on Hold Balance : '||l_order_hold_bal);
5108   oe_debug_pub.add( 'Create Freight Charges Balance : '||l_freight_bal);
5109 
5110 INSERT INTO OE_INIT_CREDIT_SUMM_GTT
5111 (line_ordered_quantity  ,
5112 line_unit_selling_price,
5113 order_invoice_to_org_id,
5114 rl_amount,
5115 rl_quantity_ordered,
5116 order_org_id ,
5117 order_transactional_curr_code,
5118 line_schedule_ship_date ,
5119 line_request_date  ,
5120 order_request_date,
5121 order_creation_date,
5122 line_tax_value    ,
5123 line_invoice_to_org_id,
5124 line_line_category_code ,
5125 line_invoiced_quantity  ,
5126 line_payment_type_code ,
5127 order_payment_type_code,
5128 order_order_number    ,
5129 line_line_id  ,
5130 order_header_id,
5131 line_header_id ,
5132 order_cust_account_id,
5133 order_party_id,
5134 line_cust_account_id,
5135 line_party_id)
5136 select
5137 l.ordered_quantity line_ordered_quantity,
5138 l.unit_selling_price line_unit_selling_price,
5139 h.invoice_to_org_id   order_invoice_to_org_id,
5140 0  rl_amount,
5141 0  rl_quantity_ordered,
5142 h.org_id  order_org_id,
5143 h.transactional_curr_code order_transactional_curr_code,
5144 l.schedule_ship_date  line_schedule_ship_date,
5145 l.request_date  line_request_date,
5146 h.request_date  order_request_date,
5147 h.creation_date  order_creation_date,
5148 l.tax_value  line_tax_value,
5149 l.invoice_to_org_id  line_invoice_to_org_id,
5150 l.line_category_code  line_line_category_code,
5151 l.invoiced_quantity  line_invoiced_quantity,
5152 l.payment_type_code  line_payment_type_code,
5153 h.payment_type_code  order_payment_type_code,
5154 h.order_number  order_order_number,
5155 l.line_id  line_line_id,
5156 h.header_id  order_header_id,
5157 l.header_id  line_header_id,
5158 s.cust_account_id  order_cust_account_id,
5159 ca.party_id   order_party_id,
5160 s_l.cust_account_id  line_cust_account_id,
5161 ca_l.party_id   line_party_id
5162 from  oe_order_lines_all       l
5163     , oe_order_headers_all     h
5164     , hz_cust_site_uses_all          su
5165     , hz_cust_acct_sites_all         s
5169     , hz_cust_accounts               ca_l
5166     , hz_cust_accounts               ca
5167     , hz_cust_site_uses_all          su_l
5168     , hz_cust_acct_sites_all         s_l
5170 where  h.header_id                    =  l.header_id
5171     AND    h.booked_flag                  =  'Y'
5172     AND    h.open_flag                    =  'Y'
5173     AND    l.open_flag                    =  'Y'
5174     AND    NVL( l.invoiced_quantity, 0 )  =  0
5175     AND    su.site_use_id                 =  h.invoice_to_org_id
5176     AND    su.cust_acct_site_id           =  s.cust_acct_site_id
5177     AND    ca.cust_account_id             =  s.cust_account_id
5178     AND    su_l.site_use_id                 =  l.invoice_to_org_id
5179     AND    su_l.cust_acct_site_id           =  s_l.cust_acct_site_id
5180     AND    ca_l.cust_account_id             =  s_l.cust_account_id
5181     AND    EXISTS
5182            ( SELECT  NULL
5183              FROM  oe_payment_types_all t
5184              WHERE t.credit_check_flag = 'Y'
5185              AND   NVL(t.org_id,-99) = NVL(h.org_id, -99)
5186              AND   l.header_id = h.header_id
5187              AND   t.payment_type_code =
5188                    DECODE(l.payment_type_code, NULL,
5189                      DECODE(h.payment_type_code, NULL, t.payment_type_code,
5190                             h.payment_type_code),
5191                           l.payment_type_code)
5192            )
5193 UNION ALL
5194 select
5195 /*+ cardinality ( rl 10 ) leading(rl h l)  */ 0 line_ordered_quantity,
5196 0 line_unit_selling_price,
5197 h.invoice_to_org_id   order_invoice_to_org_id,
5198 rl.amount  rl_amount,
5199 rl.quantity_ordered rl_quantity_ordered,
5200 h.org_id  order_org_id,
5201 h.transactional_curr_code order_transactional_curr_code,
5202 l.schedule_ship_date  line_schedule_ship_date,
5203 l.request_date  line_request_date,
5204 h.request_date  order_request_date,
5205 h.creation_date  order_creation_date,
5206 l.tax_value  line_tax_value,
5207 l.invoice_to_org_id  line_invoice_to_org_id,
5208 l.line_category_code  line_line_category_code,
5209 l.invoiced_quantity  line_invoiced_quantity,
5210 l.payment_type_code  line_payment_type_code,
5211 h.payment_type_code  order_payment_type_code,
5212 h.order_number  order_order_number,
5213 l.line_id  line_line_id,
5214 h.header_id  order_header_id,
5215 l.header_id  line_header_id,
5216 s.cust_account_id  order_cust_account_id,
5217 ca.party_id   order_party_id,
5218 s_l.cust_account_id  line_cust_account_id,
5219 ca_l.party_id   line_party_id
5220 from  oe_order_lines_all       l
5221     , oe_order_headers_all     h
5222     , hz_cust_site_uses_all          su
5223     , hz_cust_acct_sites_all         s
5224     , hz_cust_accounts               ca
5225     , hz_cust_site_uses_all          su_l
5226     , hz_cust_acct_sites_all         s_l
5227     , hz_cust_accounts               ca_l
5228     , ra_interface_lines_all         rl
5229 where      h.header_id                    =  l.header_id
5230     AND    h.booked_flag                  =  'Y'
5231     AND    rl.orig_system_bill_customer_id = ca.cust_account_id
5232     AND    nvl(rl.interface_status, '~')  <> 'P'
5233     AND    rl.interface_line_context      = 'ORDER ENTRY'
5234     AND    rl.interface_line_attribute1   = h.order_number
5235     AND    rl.interface_line_attribute6   = l.line_id
5236     AND    NVL( l.invoiced_quantity, 0 )  <>  0
5237     AND    su.site_use_id                 =  h.invoice_to_org_id
5238     AND    su.cust_acct_site_id           =  s.cust_acct_site_id
5239     AND    ca.cust_account_id             =  s.cust_account_id
5240     AND    su_l.site_use_id                 =  l.invoice_to_org_id
5241     AND    su_l.cust_acct_site_id           =  s_l.cust_acct_site_id
5242     AND    ca_l.cust_account_id             =  s_l.cust_account_id
5243     AND    EXISTS
5244            ( SELECT  NULL
5245              FROM  oe_payment_types_all t
5246              WHERE t.credit_check_flag = 'Y'
5247              AND   NVL(t.org_id,-99) = NVL(h.org_id, -99)
5248              AND   l.header_id = h.header_id
5249              AND   t.payment_type_code =
5250                    DECODE(l.payment_type_code, NULL,
5251                      DECODE(h.payment_type_code, NULL, t.payment_type_code,
5252                             h.payment_type_code),
5253                           l.payment_type_code)
5254            );
5255 
5256 
5257 --  For each balance type, a distinct insert statement
5258 --  is run against the relevant OM/AR transaction tables
5259 --  The summary table is populated with precalculated summaries
5260 --  (the SELECT subquery of each INSERT statent) which are
5261 --  then used in the Get_Exposure procedure to quickly
5262 --  determine the total exposure depending on the credit
5263 --  check rule and other parameters.
5264 
5265 
5266 --------------------------------------------------------
5267 --  Insert Tax Summaries into oe_credit_summaries
5268 --  for uninvoiced orders and tax balance types at
5269 --  header bill-to-site level
5270 
5271 
5272   IF G_debug_flag = 'Y'
5273   THEN
5274     oe_debug_pub.add( 'Start Inserting into summary tables ',1);
5275   END IF;
5276 
5277 -- balance type 1
5278   INSERT INTO oe_credit_summaries
5279   ( balance
5280   , balance_type
5281   , site_use_id
5282   , cust_account_id
5283   , party_id
5284   , org_id
5285   , currency_code
5286   , bucket
5287   , creation_date
5288   , created_by
5292   , program_application_id
5289   , last_update_date
5290   , last_updated_by
5291   , last_update_login
5293   , program_id
5294   , program_update_date
5295   , request_id
5296   , bucket_duration
5297   )
5298 
5299 
5300   SELECT
5301       SUM( NVL( m.line_ordered_quantity, 0 )
5302          * NVL( m.line_unit_selling_price, 0 ) )
5303     - SUM( NVL( p.commitment_applied_amount, 0 ) )
5304     + SUM( NVL( m.rl_amount, 0 ))
5305     , G_HEADER_UNINVOICED_ORDERS
5306     , m.order_invoice_to_org_id
5307     , order_cust_account_id
5308     , order_party_id
5309     , m.order_org_id
5310     , m.order_transactional_curr_code
5311     , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
5312                           NVL( m.line_request_date,
5313                           NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) )
5314     , SYSDATE
5315     , l_created_by
5316     , SYSDATE
5317     , l_last_updated_by
5318     , l_last_update_login
5319     , l_program_application_id
5320     , l_program_id
5321     , SYSDATE
5322     , l_request_id
5323     , 1
5324 
5325   FROM
5326       OE_INIT_CREDIT_SUMM_GTT  m
5327     , oe_payments                    p
5328   WHERE
5329          m.line_line_category_code           =  'ORDER'
5330     AND    p.header_id  (+)               =  m.line_header_id
5331     AND    p.line_id    (+)               =  m.line_line_id
5332   GROUP BY
5333     m.order_invoice_to_org_id
5334   , m.order_transactional_curr_code
5335   , order_cust_account_id
5336   , order_party_id
5337   , m.order_org_id
5338   , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
5339                         NVL( m.line_request_date,
5340                         NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) );
5341 
5342 
5343   IF G_debug_flag = 'Y'
5344   THEN
5345     oe_debug_pub.add( 'G_HEADER_UNINVOICED_ORDERS done at ' || DO_TIME, 2);
5346   END IF;
5347 
5348 
5349 -- balance type 3
5350   INSERT INTO OE_CREDIT_SUMMARIES
5351   ( balance
5352   , balance_type
5353   , site_use_id
5354   , cust_account_id
5355   , party_id
5356   , org_id
5357   , currency_code
5358   , bucket
5359   , creation_date
5360   , created_by
5361   , last_update_date
5362   , last_updated_by
5363   , last_update_login
5364   , program_application_id
5365   , program_id
5366   , program_update_date
5367   , request_id
5368   , bucket_duration
5369   )
5370 
5371 
5372   SELECT
5373       SUM( NVL( m.line_tax_value, 0 ) )
5374     , G_HEADER_UNINVOICED_ORDERS_TAX
5375     , m.order_invoice_to_org_id
5376     , order_cust_account_id
5377     , order_party_id
5378     , m.order_org_id
5379     , m.order_transactional_curr_code
5380     , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
5381                           NVL( m.line_request_date,
5382                           NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) )
5383     , SYSDATE
5384     , l_created_by
5385     , SYSDATE
5386     , l_last_updated_by
5387     , l_last_update_login
5388     , l_program_application_id
5389     , l_program_id
5390     , SYSDATE
5391     , l_request_id
5392     , 1
5393 
5394   FROM
5395           OE_INIT_CREDIT_SUMM_GTT  m
5396   WHERE
5397            m.line_line_category_code          =  'ORDER'
5398   GROUP BY
5399     m.order_invoice_to_org_id
5400   , m.order_transactional_curr_code
5401   , order_cust_account_id
5402   , order_party_id
5403   , m.order_org_id
5404   , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
5405                         NVL( m.line_request_date,
5406                         NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) );
5407 
5408 
5409   IF G_debug_flag = 'Y'
5410   THEN
5411     oe_debug_pub.add( 'G_HEADER_UNINVOICED_ORDERS_TAX done at ' || DO_TIME, 2);
5412   END IF;
5413 
5414 ----------------------------------------------
5415 ---------- RETURNS ---------------------------
5416 --- For uninvoiced orders and tax returns
5417 --- balance types at header bill-to-site level
5418 ----------------------------------------------
5419 -- balance type 23
5420   INSERT INTO oe_credit_summaries
5421   ( balance
5422   , balance_type
5423   , site_use_id
5424   , cust_account_id
5425   , party_id
5426   , org_id
5427   , currency_code
5428   , bucket
5429   , bucket_duration
5430   , creation_date
5431   , created_by
5432   , last_update_date
5433   , last_updated_by
5434   , last_update_login
5435   , program_application_id
5436   , program_id
5437   , program_update_date
5438   , request_id
5439   )
5440 
5441 
5442   SELECT
5443     - SUM( NVL( m.line_ordered_quantity, 0 )
5444          * NVL( m.line_unit_selling_price, 0 ) )
5445     + SUM( NVL( p.commitment_applied_amount, 0 ) )
5446     + SUM( DECODE( SIGN (NVL( m.rl_quantity_ordered, 0 )), -1, (+1), (-1) ) * NVL( m.rl_amount, 0 ) )
5447     , G_HEAD_RETURN_UNINV_ORDERS
5448     , m.order_invoice_to_org_id
5449     , order_cust_account_id
5450     , order_party_id
5451     , m.order_org_id
5452     , m.order_transactional_curr_code
5453     , -2
5457     , SYSDATE
5454     , OE_CREDIT_EXPOSURE_PVT.G_MAX_BUCKET_LENGTH
5455     , sysdate
5456     , l_created_by
5458     , l_last_updated_by
5459     , l_last_update_login
5460     , l_program_application_id
5461     , l_program_id
5462     , SYSDATE
5463     , l_request_id
5464 
5465   FROM
5466       OE_INIT_CREDIT_SUMM_GTT   m
5467     , oe_payments                    p
5468   WHERE
5469            m.line_line_category_code           =  'RETURN'
5470     AND    p.header_id  (+)               =  m.line_header_id
5471     AND    p.line_id    (+)               =  m.line_line_id
5472   GROUP BY
5473     m.order_invoice_to_org_id
5474   , m.order_transactional_curr_code
5475   , order_cust_account_id
5476   , order_party_id
5477   , m.order_org_id;
5478 
5479 
5480   IF G_debug_flag = 'Y'
5481   THEN
5482     oe_debug_pub.add( 'G_HEAD_RETURN_UNINV_ORDERS done at ' || DO_TIME, 2);
5483   END IF;
5484 
5485 -- balance type 25
5486 
5487   INSERT INTO OE_CREDIT_SUMMARIES
5488   ( balance
5489   , balance_type
5490   , site_use_id
5491   , cust_account_id
5492   , party_id
5493   , org_id
5494   , currency_code
5495   , bucket
5496   , bucket_duration
5497   , creation_date
5498   , created_by
5499   , last_update_date
5500   , last_updated_by
5501   , last_update_login
5502   , program_application_id
5503   , program_id
5504   , program_update_date
5505   , request_id
5506   )
5507 
5508 
5509   SELECT
5510      - SUM( NVL( m.line_tax_value, 0 ) )
5511     , G_HEAD_RETURN_UNINV_ORD_TAX
5512     , m.order_invoice_to_org_id
5513     , order_cust_account_id
5514     , order_party_id
5515     , m.order_org_id
5516     , m.order_transactional_curr_code
5517     , -2
5518     , OE_CREDIT_EXPOSURE_PVT.G_MAX_BUCKET_LENGTH
5519     , sysdate
5520     , l_created_by
5521     , sysdate
5522     , l_last_updated_by
5523     , l_last_update_login
5524     , l_program_application_id
5525     , l_program_id
5526     , SYSDATE
5527     , l_request_id
5528 
5529   FROM
5530          OE_INIT_CREDIT_SUMM_GTT   m
5531   WHERE
5532            m.line_line_category_code          =  'RETURN'
5533   GROUP BY
5534     m.order_invoice_to_org_id
5535   , m.order_transactional_curr_code
5536   , order_cust_account_id
5537   , order_party_id
5538   , m.order_org_id;
5539 
5540   IF G_debug_flag = 'Y'
5541   THEN
5542     oe_debug_pub.add( 'G_HEAD_RETURN_UNINV_ORD_TAX done at ' || DO_TIME, 2);
5543   END IF;
5544 
5545 
5546 --------------------------
5547 --  Line Uninvoiced Orders
5548 --------------------------
5549 --  Note this is identical code except that
5550 --  h.invoice_to_org_id is replaced with l.invoice_to_org_id
5551 
5552 -- balance type 2
5553 
5554   INSERT INTO OE_CREDIT_SUMMARIES
5555   ( balance
5556   , balance_type
5557   , site_use_id
5558   , cust_account_id
5559   , party_id
5560   , org_id
5561   , currency_code
5562   , bucket
5563   , creation_date
5564   , created_by
5565   , last_update_date
5566   , last_updated_by
5567   , last_update_login
5568   , program_application_id
5569   , program_id
5570   , program_update_date
5571   , request_id
5572   , bucket_duration
5573   )
5574 
5575   SELECT
5576     SUM( NVL( m.line_ordered_quantity, 0 )
5577        * NVL( m.line_unit_selling_price, 0 )
5578        )
5579     - SUM( NVL( p.commitment_applied_amount, 0 ) )
5580     + SUM( NVL( m.rl_amount, 0 ))
5581     , G_LINE_UNINVOICED_ORDERS
5582     , m.line_invoice_to_org_id
5583     , line_cust_account_id
5584     , line_party_id
5585     , m.order_org_id
5586     , m.order_transactional_curr_code
5587     , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
5588                           NVL( m.line_request_date,
5589                           NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) )
5590     , SYSDATE
5591     , l_created_by
5592     , SYSDATE
5593     , l_last_updated_by
5594     , l_last_update_login
5595     , l_program_application_id
5596     , l_program_id
5597     , SYSDATE
5598     , l_request_id
5599     , 1
5600 
5601   FROM
5602          OE_INIT_CREDIT_SUMM_GTT   m
5603        , oe_payments                    p
5604   WHERE
5605           m.line_line_category_code           =  'ORDER'
5606     AND    p.header_id  (+)               =  m.line_header_id
5607     AND    p.line_id    (+)               =  m.line_line_id
5608   GROUP BY
5609      m.line_invoice_to_org_id
5610    , m.order_transactional_curr_code
5611    , line_cust_account_id
5612    , line_party_id
5613    , m.order_org_id
5614    , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
5615                          NVL( m.line_request_date,
5616                          NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) );
5617 
5618 
5619   IF G_debug_flag = 'Y'
5620   THEN
5621    oe_debug_pub.add( 'G_LINE_UNINVOICED_ORDERS done at ' || DO_TIME, 2);
5622   END IF;
5623 
5624 -- balance type 4
5625 
5626   INSERT INTO OE_CREDIT_SUMMARIES
5627   ( balance
5631   , party_id
5628   , balance_type
5629   , site_use_id
5630   , cust_account_id
5632   , org_id
5633   , currency_code
5634   , bucket
5635   , creation_date
5636   , created_by
5637   , last_update_date
5638   , last_updated_by
5639   , last_update_login
5640   , program_application_id
5641   , program_id
5642   , program_update_date
5643   , request_id
5644   , bucket_duration
5645   )
5646 
5647 
5648   SELECT
5649     SUM( NVL( m.line_tax_value, 0 ) )
5650     , G_LINE_UNINVOICED_ORDERS_TAX
5651     , m.line_invoice_to_org_id
5652     , line_cust_account_id
5653     , line_party_id
5654     , m.order_org_id
5655     , m.order_transactional_curr_code
5656     , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
5657                           NVL( m.line_request_date,
5658                           NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) )
5659     , SYSDATE
5660     , l_created_by
5661     , SYSDATE
5662     , l_last_updated_by
5663     , l_last_update_login
5664     , l_program_application_id
5665     , l_program_id
5666     , SYSDATE
5667     , l_request_id
5668     , 1
5669 
5670   FROM
5671       OE_INIT_CREDIT_SUMM_GTT   m
5672   WHERE
5673            m.line_line_category_code           =  'ORDER'
5674   GROUP BY
5675      m.line_invoice_to_org_id
5676    , m.order_transactional_curr_code
5677    , line_cust_account_id
5678    , line_party_id
5679    , m.order_org_id
5680    , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
5681                          NVL( m.line_request_date,
5682                          NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) );
5683 
5684 
5685   IF G_debug_flag = 'Y'
5686   THEN
5687     oe_debug_pub.add( 'G_LINE_UNINVOICED_ORDERS_TAX done at ' || DO_TIME, 2);
5688   END IF;
5689 
5690 ----------------------------------
5691 ------ RETURNS--------------------
5692 --  Line Uninvoiced Orders returns
5693 ----------------------------------
5694 --  Note this is identical code except that
5695 --  h.invoice_to_org_id is replaced with l.invoice_to_org_id
5696 
5697 -- balance type 24
5698 
5699   INSERT INTO OE_CREDIT_SUMMARIES
5700   ( balance
5701   , balance_type
5702   , site_use_id
5703   , cust_account_id
5704   , party_id
5705   , org_id
5706   , currency_code
5707   , bucket
5708   , bucket_duration
5709   , creation_date
5710   , created_by
5711   , last_update_date
5712   , last_updated_by
5713   , last_update_login
5714   , program_application_id
5715   , program_id
5716   , program_update_date
5717   , request_id
5718   )
5719 
5720 
5721   SELECT
5722     - SUM( NVL(m.line_ordered_quantity, 0 )
5723          * NVL( m.line_unit_selling_price, 0 )
5724          )
5725     + SUM( NVL( p.commitment_applied_amount, 0 ) )
5726     + SUM( DECODE( SIGN (NVL( m.rl_quantity_ordered, 0 )), -1, (+1), (-1) ) * NVL( m.rl_amount, 0 ) )
5727     , G_LINE_RETURN_UNINV_ORDERS
5728     , m.line_invoice_to_org_id
5729     , line_cust_account_id
5730     , line_party_id
5731     , m.order_org_id
5732     , m.order_transactional_curr_code
5733     , -2
5734     , OE_CREDIT_EXPOSURE_PVT.G_MAX_BUCKET_LENGTH
5735     , sysdate
5736     , l_created_by
5737     , sysdate
5738     , l_last_updated_by
5739     , l_last_update_login
5740     , l_program_application_id
5741     , l_program_id
5742     , SYSDATE
5743     , l_request_id
5744 
5745   FROM
5746          OE_INIT_CREDIT_SUMM_GTT   m
5747        , oe_payments                    p
5748   WHERE
5749            m.line_line_category_code           =  'RETURN'
5750     AND    p.header_id  (+)               =  m.line_header_id
5751     AND    p.line_id    (+)               =  m.line_line_id
5752   GROUP BY
5753      m.line_invoice_to_org_id
5754    , m.order_transactional_curr_code
5755    , line_cust_account_id
5756    , line_party_id
5757    , m.order_org_id;
5758 
5759 
5760   IF G_debug_flag = 'Y'
5761   THEN
5762    oe_debug_pub.add( 'G_LINE_RETURN_UNINV_ORDERS done at ' || DO_TIME, 2);
5763   END IF;
5764 
5765 -- balance type 26
5766 
5767   INSERT INTO OE_CREDIT_SUMMARIES
5768   ( balance
5769   , balance_type
5770   , site_use_id
5771   , cust_account_id
5772   , party_id
5773   , org_id
5774   , currency_code
5775   , bucket
5776   , bucket_duration
5777   , creation_date
5778   , created_by
5779   , last_update_date
5780   , last_updated_by
5781   , last_update_login
5782   , program_application_id
5783   , program_id
5784   , program_update_date
5785   , request_id
5786   )
5787 
5788 
5789   SELECT
5790     - SUM( NVL(m.line_tax_value, 0 ) )
5791     , G_LINE_RETURN_UNINV_ORD_TAX
5792     , m.line_invoice_to_org_id
5793     , line_cust_account_id
5794     , line_party_id
5795     , m.order_org_id
5796     , m.order_transactional_curr_code
5797     , -2
5798     , OE_CREDIT_EXPOSURE_PVT.G_MAX_BUCKET_LENGTH
5799     , sysdate
5800     , l_created_by
5801     , sysdate
5802     , l_last_updated_by
5803     , l_last_update_login
5807     , l_request_id
5804     , l_program_application_id
5805     , l_program_id
5806     , SYSDATE
5808 
5809   FROM
5810 	OE_INIT_CREDIT_SUMM_GTT   m
5811   WHERE
5812            m.line_line_category_code           =  'RETURN'
5813   GROUP BY
5814      m.line_invoice_to_org_id
5815    , m.order_transactional_curr_code
5816    , line_cust_account_id
5817    , line_party_id
5818    , m.order_org_id;
5819 
5820 
5821 
5822   IF G_debug_flag = 'Y'
5823   THEN
5824     oe_debug_pub.add( 'G_LINE_RETURN_UNINV_ORD_TAX done at ' || DO_TIME, 2);
5825   END IF;
5826 
5827 -- Bug 4219133 : Insert Freight Balances only if Freight Charges to be Included
5828 IF l_freight_bal = 'Y' THEN
5829 --------------------------
5830 --  Line Freight
5831 --------------------------
5832 
5833 -- balance type 6
5834   INSERT INTO OE_CREDIT_SUMMARIES
5835   ( balance
5836   , balance_type
5837   , site_use_id
5838   , cust_account_id
5839   , party_id
5840   , org_id
5841   , currency_code
5842   , bucket
5843   , creation_date
5844   , created_by
5845   , last_update_date
5846   , last_updated_by
5847   , last_update_login
5848   , program_application_id
5849   , program_id
5850   , program_update_date
5851   , request_id
5852   , bucket_duration
5853   )
5854 
5855   SELECT
5856       SUM
5857       ( DECODE( p.credit_or_charge_flag, 'C', (-1), (+1) )
5858       * DECODE( p.arithmetic_operator, 'LUMPSUM',			--bug 4295298
5859                 p.operand, (m.line_ordered_quantity * p.adjusted_amount))
5860       )
5861       + SUM( NVL( m.rl_amount, 0 ))
5862     , G_LINE_UNINVOICED_FREIGHT
5863     , m.line_invoice_to_org_id
5864     , line_cust_account_id
5865     , line_party_id
5866     , m.order_org_id
5867     , m.order_transactional_curr_code
5868     , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
5869                           NVL( m.line_request_date,
5870                           NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) )
5871     , SYSDATE
5872     , l_created_by
5873     , SYSDATE
5874     , l_last_updated_by
5875     , l_last_update_login
5876     , l_program_application_id
5877     , l_program_id
5878     , SYSDATE
5879     , l_request_id
5880     , 1
5881 
5882   FROM
5883       OE_INIT_CREDIT_SUMM_GTT  m,
5884       oe_price_adjustments     p
5885   WHERE
5886          p.line_id             =  m.line_line_id
5887     AND  p.header_id           =  m.line_header_id
5888     AND  p.header_id           =  m.order_header_id
5889     AND  m.line_line_category_code           =  'ORDER'
5890     AND  p.applied_flag        =  'Y'
5891     AND  p.list_line_type_code =  'FREIGHT_CHARGE'
5892     AND  NVL( p.invoiced_flag, 'N' )  =  'N'
5893   GROUP BY
5894       m.line_invoice_to_org_id
5895     , m.order_transactional_curr_code
5896     , line_cust_account_id
5897     , line_party_id
5898     , m.order_org_id
5899     , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
5900                           NVL( m.line_request_date,
5901                           NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) );
5902 
5903 
5904   IF G_debug_flag = 'Y'
5905   THEN
5906     oe_debug_pub.add( 'G_LINE_UNINVOICED_FREIGHT done at ' || DO_TIME, 2);
5907   END IF;
5908 END IF; -- l_freight_bal
5909 
5910 --------------------------
5911 ------- RETURNS-----------
5912 --------------------------
5913 --  Line Freight
5914 --------------------------
5915 
5916 -- balance type 28
5917   INSERT INTO OE_CREDIT_SUMMARIES
5918   ( balance
5919   , balance_type
5920   , site_use_id
5921   , cust_account_id
5922   , party_id
5923   , org_id
5924   , currency_code
5925   , bucket
5926   , bucket_duration
5927   , creation_date
5928   , created_by
5929   , last_update_date
5930   , last_updated_by
5931   , last_update_login
5932   , program_application_id
5933   , program_id
5934   , program_update_date
5935   , request_id
5936   )
5937 
5938   SELECT
5939      SUM
5940       ( DECODE( p.credit_or_charge_flag, 'C', (-1), (+1) )
5941       * DECODE( p.arithmetic_operator, 'LUMPSUM',			--bug 4295298
5942                 p.operand, (m.line_ordered_quantity * p.adjusted_amount))
5943       )
5944       + SUM( NVL( m.rl_amount, 0 ))
5945     , G_LINE_RETURN_UNINV_FREIGHT
5946     , m.line_invoice_to_org_id
5947     , line_cust_account_id
5948     , line_party_id
5949     , m.order_org_id
5950     , m.order_transactional_curr_code
5951     , -2
5952     , OE_CREDIT_EXPOSURE_PVT.G_MAX_BUCKET_LENGTH
5953     , sysdate
5954     , l_created_by
5955     , sysdate
5956     , l_last_updated_by
5957     , l_last_update_login
5958     , l_program_application_id
5959     , l_program_id
5960     , SYSDATE
5961     , l_request_id
5962 
5963   FROM
5964       oe_price_adjustments     p,
5965       OE_INIT_CREDIT_SUMM_GTT    m
5966   WHERE
5967          p.line_id             =  m.line_line_id
5968     AND  p.header_id           =  m.line_header_id
5969     AND  p.header_id           =  m.order_header_id
5970     AND  m.line_line_category_code           =  'RETURN'
5974   GROUP BY
5971     AND  p.applied_flag        =  'Y'
5972     AND  p.list_line_type_code =  'FREIGHT_CHARGE'
5973     AND  NVL( p.invoiced_flag, 'N' )  =  'N'
5975       m.line_invoice_to_org_id
5976     , m.order_transactional_curr_code
5977     , line_cust_account_id
5978     , line_party_id
5979     , m.order_org_id;
5980 
5981 
5982 
5983   IF G_debug_flag = 'Y'
5984   THEN
5985     oe_debug_pub.add( 'G_LINE_RETURN_UNINV_FREIGHT done at ' || DO_TIME, 2);
5986   END IF;
5987 
5988 -- Bug 4219133 : Insert Freight Balances only if Freight Charges to be Included.
5989 
5990 IF l_freight_bal = 'Y' THEN
5991 
5992 --------------------------
5993 --  Header Freight
5994 --------------------------
5995 
5996 -- balance type 5
5997 
5998   INSERT INTO OE_CREDIT_SUMMARIES
5999   ( balance
6000   , balance_type
6001   , site_use_id
6002   , cust_account_id
6003   , party_id
6004   , org_id
6005   , currency_code
6006   , bucket
6007   , creation_date
6008   , created_by
6009   , last_update_date
6010   , last_updated_by
6011   , last_update_login
6012   , program_application_id
6013   , program_id
6014   , program_update_date
6015   , request_id
6016   , bucket_duration
6017   )
6018 
6019 SELECT
6020       SUM
6021       ( DECODE( p.credit_or_charge_flag, 'C', (-1), (+1) )
6022       * DECODE( p.arithmetic_operator, 'LUMPSUM',			--bug 4295298
6023                 p.operand, (m.line_ordered_quantity * p.adjusted_amount))
6024       )
6025       + SUM( NVL( m.rl_amount, 0 ))
6026     , G_HEADER_UNINVOICED_FREIGHT
6027     , m.order_invoice_to_org_id
6028     , order_cust_account_id
6029     , order_party_id
6030     , m.order_org_id
6031     , m.order_transactional_curr_code
6032     , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
6033                           NVL( m.line_request_date,
6034                           NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) )
6035     , SYSDATE
6036     , l_created_by
6037     , SYSDATE
6038     , l_last_updated_by
6039     , l_last_update_login
6040     , l_program_application_id
6041     , l_program_id
6042     , SYSDATE
6043     , l_request_id
6044     , 1
6045 
6046 FROM   oe_price_adjustments     p,
6047       OE_INIT_CREDIT_SUMM_GTT    m
6048 WHERE
6049        p.line_id             =  m.line_line_id
6050   AND  p.header_id           =  m.line_header_id
6051   AND  p.header_id           =  m.order_header_id
6052   AND  m.line_line_category_code           =  'ORDER'
6053   AND  p.applied_flag                 =  'Y'
6054   AND  p.list_line_type_code          =  'FREIGHT_CHARGE'
6055   AND  NVL( p.invoiced_flag, 'N' )    =  'N'
6056 GROUP BY
6057     m.order_invoice_to_org_id
6058   , m.order_transactional_curr_code
6059   , order_cust_account_id
6060   , order_party_id
6061   , m.order_org_id
6062   , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
6063                         NVL( m.line_request_date,
6064                         NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) );
6065 
6066 
6067   IF G_debug_flag = 'Y'
6068   THEN
6069     oe_debug_pub.add( 'G_HEADER_UNINVOICED_FREIGHT done at ' || DO_TIME, 2 );
6070   END IF;
6071 
6072 END IF;  -- l_freight_bal
6073 
6074 --------------------------------
6075 --------- RETURNS --------------
6076 --  Header Freight
6077 --------------------------------
6078 
6079 -- balance type 27
6080 
6081   INSERT INTO OE_CREDIT_SUMMARIES
6082   ( balance
6083   , balance_type
6084   , site_use_id
6085   , cust_account_id
6086   , party_id
6087   , org_id
6088   , currency_code
6089   , bucket
6090   , bucket_duration
6091   , creation_date
6092   , created_by
6093   , last_update_date
6094   , last_updated_by
6095   , last_update_login
6096   , program_application_id
6097   , program_id
6098   , program_update_date
6099   , request_id
6100   )
6101 
6102 
6103 SELECT
6104       SUM
6105       ( DECODE( p.credit_or_charge_flag, 'C', (-1), (+1) )
6106       * DECODE( p.arithmetic_operator, 'LUMPSUM',			--bug 4295298
6107                 p.operand, (m.line_ordered_quantity * p.adjusted_amount))
6108       )
6109       + SUM( NVL( m.rl_amount, 0 ))
6110     , G_HEAD_RETURN_UNINV_FREIGHT
6111     , m.order_invoice_to_org_id
6112     , order_cust_account_id
6113     , order_party_id
6114     , m.order_org_id
6115     , m.order_transactional_curr_code
6116     , -2
6117     , OE_CREDIT_EXPOSURE_PVT.G_MAX_BUCKET_LENGTH
6118     , sysdate
6119     , l_created_by
6120     , sysdate
6121     , l_last_updated_by
6122     , l_last_update_login
6123     , l_program_application_id
6124     , l_program_id
6125     , SYSDATE
6126     , l_request_id
6127 
6128 FROM   oe_price_adjustments     p,
6129       OE_INIT_CREDIT_SUMM_GTT    m
6130 WHERE
6131        p.line_id             =  m.line_line_id
6132   AND  p.header_id           =  m.line_header_id
6133   AND  p.header_id           =  m.order_header_id
6134   AND  m.line_line_category_code           =  'RETURN'
6135   AND  p.applied_flag                 =  'Y'
6136   AND  p.list_line_type_code          =  'FREIGHT_CHARGE'
6137   AND  NVL( p.invoiced_flag, 'N' )    =  'N'
6138 GROUP BY
6139     m.order_invoice_to_org_id
6140   , m.order_transactional_curr_code
6141   , order_cust_account_id
6142   , order_party_id
6143   , m.order_org_id;
6144 
6145 
6146   IF G_debug_flag = 'Y'
6147   THEN
6148     oe_debug_pub.add( 'G_HEAD_RETURN_UNINV_FREIGHT done at ' || DO_TIME, 2 );
6149   END IF;
6150 
6151 -- Bug 4219133 : Insert Freight Balances only if Freight Charges to be Included.
6152 
6153 IF l_freight_bal = 'Y' THEN
6154 --------------------------
6155 --  Header + Line Fright Part 2
6156 --------------------------
6157 --  This cost is specified at the order header and thus
6158 --  the bill-to site to be used is always the header bill-to site.
6159 --  Therefore for both line and header bill-to site level exposures,
6160 --  this same balance type summary is added
6161 
6162 -- balance type 7
6163 
6164   INSERT INTO OE_CREDIT_SUMMARIES
6165   ( balance
6166   , balance_type
6167   , site_use_id
6168   , cust_account_id
6169   , party_id
6170   , org_id
6171   , currency_code
6172   , bucket
6173   , creation_date
6174   , created_by
6175   , last_update_date
6176   , last_updated_by
6177   , last_update_login
6178   , program_application_id
6179   , program_id
6180   , program_update_date
6181   , request_id
6182   , bucket_duration
6183   )
6184 
6185 
6186 SELECT
6187       SUM( DECODE( p.credit_or_charge_flag, 'C', (-1), (+1) ) * p.operand )
6188     , G_HEADER_AND_LINE_FREIGHT
6189     , h.invoice_to_org_id
6190     , s.cust_account_id
6191     , ca.party_id
6192     , h.org_id
6193     , h.transactional_curr_code
6194     , TO_NUMBER( TO_CHAR( NVL( h.request_date, h.creation_date ), 'J' ) )
6195     , SYSDATE
6196     , l_created_by
6197     , SYSDATE
6198     , l_last_updated_by
6199     , l_last_update_login
6200     , l_program_application_id
6201     , l_program_id
6202     , SYSDATE
6203     , l_request_id
6204     , 1
6205 
6206 FROM
6207     oe_price_adjustments      p
6208   , oe_order_headers_all      h
6209   , hz_cust_site_uses_all     su
6210   , hz_cust_acct_sites_all    s
6211   , hz_cust_accounts          ca
6212 WHERE
6213        p.line_id IS NULL
6214   AND  p.header_id           =  h.header_id
6215   AND  h.order_category_code IN ('ORDER','MIXED')
6216   AND  h.open_flag           =  'Y'
6217   AND  h.booked_flag         =  'Y'
6218   AND  p.applied_flag        =  'Y'
6219   AND  p.list_line_type_code = 'FREIGHT_CHARGE'
6220   AND  NVL( p.invoiced_flag, 'N' )  =  'N'
6221   AND  su.site_use_id        =  h.invoice_to_org_id
6222   AND  su.cust_acct_site_id  =  s.cust_acct_site_id
6223   AND  ca.cust_account_id  =  s.cust_account_id
6224   AND  EXISTS
6225          ( SELECT  NULL
6226              FROM  oe_payment_types_all t,
6227                    oe_order_lines_all l
6228              WHERE t.credit_check_flag = 'Y'
6229              AND   NVL(t.org_id,-99) = NVL(h.org_id, -99)
6230              AND   l.header_id = h.header_id
6231              AND   t.payment_type_code =
6232                    DECODE(l.payment_type_code, NULL,
6233                      DECODE(h.payment_type_code, NULL, t.payment_type_code,
6234                             h.payment_type_code),
6235                           l.payment_type_code)
6236          )
6237 GROUP BY
6238       h.invoice_to_org_id
6239     , h.transactional_curr_code
6240     , s.cust_account_id
6241     , ca.party_id
6242     , h.org_id
6243     , TO_NUMBER( TO_CHAR( NVL( h.request_date, h.creation_date ), 'J' ) )
6244 UNION ALL  --bug# 2714553
6245 SELECT
6246       SUM( NVL( rl.amount, 0 ))
6247     , G_HEADER_AND_LINE_FREIGHT
6248     , h.invoice_to_org_id
6249     , s.cust_account_id
6250     , ca.party_id
6251     , h.org_id
6252     , h.transactional_curr_code
6253     , TO_NUMBER( TO_CHAR( NVL( h.request_date, h.creation_date ), 'J' ) )
6254     , SYSDATE
6255     , l_created_by
6256     , SYSDATE
6257     , l_last_updated_by
6258     , l_last_update_login
6259     , l_program_application_id
6260     , l_program_id
6261     , SYSDATE
6262     , l_request_id
6263     , 1
6264 
6265 FROM
6266     oe_price_adjustments      p
6267   , oe_order_headers_all      h
6268   , hz_cust_site_uses_all     su
6269   , hz_cust_acct_sites_all    s
6270   , hz_cust_accounts          ca
6271   , ra_interface_lines_all    rl
6272 WHERE
6273        p.line_id IS NULL
6274   AND  p.header_id           =  h.header_id
6275   AND  h.order_category_code IN ('ORDER','MIXED')
6276   AND  h.booked_flag         =  'Y'
6277   AND  p.applied_flag        =  'Y'
6278   AND  p.list_line_type_code = 'FREIGHT_CHARGE'
6279   AND  NVL( p.invoiced_flag, 'N' )  =  'Y'
6280   AND  su.site_use_id        =  h.invoice_to_org_id
6281   AND  su.cust_acct_site_id  =  s.cust_acct_site_id
6282   AND  ca.cust_account_id  =  s.cust_account_id
6283   AND  rl.orig_system_bill_customer_id = ca.cust_account_id
6284   AND  nvl(rl.interface_status, '~')  <> 'P'
6285   AND  rl.interface_line_context      = 'ORDER ENTRY'
6286   AND  rl.interface_line_attribute1   = h.order_number
6287   AND  rl.interface_line_attribute6   = p.price_adjustment_id
6288   AND  EXISTS
6289          ( SELECT  NULL
6290              FROM  oe_payment_types_all t,
6291                    oe_order_lines_all l
6292              WHERE t.credit_check_flag = 'Y'
6293              AND   NVL(t.org_id,-99) = NVL(h.org_id, -99)
6294              AND   l.header_id = h.header_id
6295              AND   t.payment_type_code =
6296                    DECODE(l.payment_type_code, NULL,
6297                      DECODE(h.payment_type_code, NULL, t.payment_type_code,
6298                             h.payment_type_code),
6299                           l.payment_type_code)
6300          )
6301 GROUP BY
6302       h.invoice_to_org_id
6303     , h.transactional_curr_code
6304     , s.cust_account_id
6305     , ca.party_id
6306     , h.org_id
6307     , TO_NUMBER( TO_CHAR( NVL( h.request_date, h.creation_date ), 'J' ) )
6308 ;
6309 
6310 
6311 
6312   IF G_debug_flag = 'Y'
6313   THEN
6314     oe_debug_pub.add( 'G_HEADER_AND_LINE_FREIGHT done at ' || DO_TIME, 2 );
6315   END IF;
6316 END IF;  -- l_freight_bal
6317 
6318 -------------------------------
6319 ----- RETURNS -----------------
6320 --  Header + Line Freight Part 2
6321 -------------------------------
6322 --  This cost is specified at the order header and thus
6323 --  the bill-to site to be used is always the header bill-to site.
6324 --  Therefore for both line and header bill-to site level exposures,
6325 --  this same balance type summary is added
6326 
6327 -- balance type 29
6328 
6329   INSERT INTO OE_CREDIT_SUMMARIES
6330   ( balance
6331   , balance_type
6332   , site_use_id
6333   , cust_account_id
6334   , party_id
6335   , org_id
6336   , currency_code
6337   , bucket
6338   , bucket_duration
6339   , creation_date
6340   , created_by
6341   , last_update_date
6342   , last_updated_by
6343   , last_update_login
6344   , program_application_id
6345   , program_id
6346   , program_update_date
6347   , request_id
6348   )
6349 
6350 
6351 SELECT
6352       SUM( DECODE( p.credit_or_charge_flag, 'C', (-1), (+1) ) * p.operand )
6353     , G_HEAD_LINE_RETURN_FREIGHT
6354     , h.invoice_to_org_id
6355     , s.cust_account_id
6356     , ca.party_id
6357     , h.org_id
6358     , h.transactional_curr_code
6359     , -2
6360     , OE_CREDIT_EXPOSURE_PVT.G_MAX_BUCKET_LENGTH
6361     , sysdate
6362     , l_created_by
6363     , sysdate
6364     , l_last_updated_by
6365     , l_last_update_login
6366     , l_program_application_id
6367     , l_program_id
6368     , SYSDATE
6369     , l_request_id
6370 
6371 FROM
6372     oe_price_adjustments      p
6373   , oe_order_headers_all      h
6374   , hz_cust_site_uses_all     su
6375   , hz_cust_acct_sites_all    s
6376   , hz_cust_accounts          ca
6377 WHERE
6378        p.line_id IS NULL
6379   AND  p.header_id           =  h.header_id
6380   AND  h.order_category_code ='RETURN'
6381   AND  h.open_flag           =  'Y'
6382   AND  h.booked_flag         =  'Y'
6383   AND  p.applied_flag        =  'Y'
6384   AND  p.list_line_type_code = 'FREIGHT_CHARGE'
6385   AND  NVL( p.invoiced_flag, 'N' )  =  'N'
6386   AND  su.site_use_id        =  h.invoice_to_org_id
6387   AND  su.cust_acct_site_id  =  s.cust_acct_site_id
6388   AND  ca.cust_account_id    =  s.cust_account_id
6389   AND  EXISTS
6390          ( SELECT  NULL
6391              FROM  oe_payment_types_all t,
6392                    oe_order_lines_all l
6393              WHERE t.credit_check_flag = 'Y'
6394              AND   NVL(t.org_id,-99) = NVL(h.org_id, -99)
6395              AND   l.header_id = h.header_id
6396              AND   t.payment_type_code =
6397                    DECODE(l.payment_type_code, NULL,
6398                      DECODE(h.payment_type_code, NULL, t.payment_type_code,
6399                             h.payment_type_code),
6400                           l.payment_type_code)
6401          )
6402 GROUP BY
6403       h.invoice_to_org_id
6404     , h.transactional_curr_code
6405     , s.cust_account_id
6406     , ca.party_id
6407     , h.org_id
6408 UNION ALL  --bug# 2714553
6409 SELECT
6410       SUM( NVL( rl.amount, 0 ))
6411     , G_HEAD_LINE_RETURN_FREIGHT
6412     , h.invoice_to_org_id
6413     , s.cust_account_id
6414     , ca.party_id
6415     , h.org_id
6416     , h.transactional_curr_code
6417     , -2
6418     , OE_CREDIT_EXPOSURE_PVT.G_MAX_BUCKET_LENGTH
6419     , sysdate
6420     , l_created_by
6421     , sysdate
6422     , l_last_updated_by
6423     , l_last_update_login
6424     , l_program_application_id
6425     , l_program_id
6426     , SYSDATE
6427     , l_request_id
6428 
6429 FROM
6430     oe_price_adjustments      p
6431   , oe_order_headers_all      h
6432   , hz_cust_site_uses_all     su
6433   , hz_cust_acct_sites_all    s
6434   , hz_cust_accounts          ca
6435   , ra_interface_lines_all    rl
6436 WHERE
6437        p.line_id IS NULL
6438   AND  p.header_id           =  h.header_id
6439   AND  h.order_category_code ='RETURN'
6440   AND  h.booked_flag         =  'Y'
6441   AND  p.applied_flag        =  'Y'
6442   AND  p.list_line_type_code = 'FREIGHT_CHARGE'
6443   AND  NVL( p.invoiced_flag, 'N' )  =  'Y'
6444   AND  su.site_use_id        =  h.invoice_to_org_id
6445   AND  su.cust_acct_site_id  =  s.cust_acct_site_id
6446   AND  ca.cust_account_id    =  s.cust_account_id
6447   AND  rl.orig_system_bill_customer_id = ca.cust_account_id
6448   AND  nvl(rl.interface_status, '~')  <> 'P'
6449   AND  rl.interface_line_context      = 'ORDER ENTRY'
6450   AND  rl.interface_line_attribute1   = h.order_number
6451   AND  rl.interface_line_attribute6   = p.price_adjustment_id
6452   AND  EXISTS
6453          ( SELECT  NULL
6454              FROM  oe_payment_types_all t,
6455                    oe_order_lines_all l
6456              WHERE t.credit_check_flag = 'Y'
6457              AND   NVL(t.org_id,-99) = NVL(h.org_id, -99)
6458              AND   l.header_id = h.header_id
6459              AND   t.payment_type_code =
6460                    DECODE(l.payment_type_code, NULL,
6461                      DECODE(h.payment_type_code, NULL, t.payment_type_code,
6462                             h.payment_type_code),
6463                           l.payment_type_code)
6464          )
6465 GROUP BY
6466       h.invoice_to_org_id
6467     , h.transactional_curr_code
6468     , s.cust_account_id
6469     , ca.party_id
6470     , h.org_id
6471 ;
6472 
6473 
6474 
6475   IF G_debug_flag = 'Y'
6476   THEN
6477     oe_debug_pub.add( 'G_HEAD_LINE_RETURN_FREIGHT done at ' || DO_TIME, 2 );
6478   END IF;
6479 
6480 --------------------------
6481 --  Holds
6482 --------------------------
6483 --  This is a repeat of all the order balance types, but this
6484 --  time qualified by existing holds.
6485 --  In the Get_Exposure procedure, this negative balance
6486 --  is added if the Include Orders on Hold box is checked in the credit
6487 --  check rule
6488 
6489 -- Bug 4219133:Insert Hold Balances only if Orders on Hold Need to be Excluded.
6490 
6491 IF l_order_hold_bal = 'Y' THEN
6492 
6493 -- balance type 10
6494 
6495   INSERT INTO OE_CREDIT_SUMMARIES
6496   ( balance
6497   , balance_type
6498   , site_use_id
6499   , cust_account_id
6500   , party_id
6501   , org_id
6502   , currency_code
6503   , bucket
6504   , creation_date
6505   , created_by
6506   , last_update_date
6507   , last_updated_by
6508   , last_update_login
6509   , program_application_id
6510   , program_id
6511   , program_update_date
6512   , request_id
6513   , bucket_duration
6514   )
6515 
6516 
6517   SELECT
6518     - SUM( NVL( m.line_ordered_quantity, 0 )
6519          * NVL( m.line_unit_selling_price, 0 ) )
6520     + SUM( NVL( p.commitment_applied_amount, 0 ) )
6521     , G_ORDER_HOLDS
6522     , m.order_invoice_to_org_id
6523     , order_cust_account_id
6524     , order_party_id
6525     , m.order_org_id
6526     , m.order_transactional_curr_code
6527     , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
6528                           NVL( m.line_request_date,
6529                           NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) )
6530     , SYSDATE
6531     , l_created_by
6532     , SYSDATE
6533     , l_last_updated_by
6534     , l_last_update_login
6535     , l_program_application_id
6536     , l_program_id
6537     , SYSDATE
6538     , l_request_id
6539     , 1
6540 
6541   FROM
6542       OE_INIT_CREDIT_SUMM_GTT    m,
6543       oe_payments                    p
6544   WHERE
6545            m.line_line_category_code           =  'ORDER'
6546     AND    p.header_id  (+)               =  m.line_header_id
6547     AND    p.line_id    (+)               =  m.line_line_id
6548     AND    EXISTS
6549            ( SELECT  1
6550              FROM
6551                      oe_order_holds_all  oh
6552              WHERE
6553                      oh.header_id         =  m.order_header_id
6554              AND   ( oh.line_id           =  m.line_line_id
6555                      OR  oh.line_id IS NULL
6556                    )
6557              AND     oh.hold_release_id  IS NULL
6558            )
6559   GROUP BY
6560     m.order_invoice_to_org_id
6561   , m.order_transactional_curr_code
6562   , order_cust_account_id
6563   , order_party_id
6564   , m.order_org_id
6565   , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
6566                         NVL( m.line_request_date,
6567                         NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) )
6568   ;
6569 
6570 
6571 
6572   IF G_debug_flag = 'Y'
6573   THEN
6574     oe_debug_pub.add( 'G_ORDER_HOLDS done at ' || DO_TIME, 2 );
6575   END IF;
6576 
6577 -- balance type 13
6578 
6579   INSERT INTO OE_CREDIT_SUMMARIES
6580   ( balance
6581   , balance_type
6582   , site_use_id
6583   , cust_account_id
6584   , party_id
6585   , org_id
6586   , currency_code
6587   , bucket
6588   , creation_date
6589   , created_by
6590   , last_update_date
6591   , last_updated_by
6592   , last_update_login
6593   , program_application_id
6594   , program_id
6595   , program_update_date
6596   , request_id
6597   , bucket_duration
6598   )
6599 
6600 
6601   SELECT
6602     - SUM( NVL( m.line_tax_value, 0 ) )
6603     , G_ORDER_TAX_HOLDS
6604     , m.order_invoice_to_org_id
6605     , order_cust_account_id
6606     , order_party_id
6607     , m.order_org_id
6608     , m.order_transactional_curr_code
6609     , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
6610                           NVL( m.line_request_date,
6611                           NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) )
6612     , SYSDATE
6613     , l_created_by
6614     , SYSDATE
6615     , l_last_updated_by
6616     , l_last_update_login
6617     , l_program_application_id
6618     , l_program_id
6619     , SYSDATE
6620     , l_request_id
6621     , 1
6622 
6623   FROM
6624       OE_INIT_CREDIT_SUMM_GTT    m
6625   WHERE
6626   	   m.line_line_category_code           =  'ORDER'
6627   AND    EXISTS
6628             ( SELECT  1
6629               FROM    oe_order_holds_all  oh
6630               WHERE   oh.header_id          =  m.order_header_id
6631               AND    (  oh.line_id        =  m.line_line_id
6632                       OR  oh.line_id IS NULL
6633                      )
6634               AND     oh.hold_release_id  IS NULL
6635             )
6636   GROUP BY
6637     m.order_invoice_to_org_id
6638   , m.order_transactional_curr_code
6639   , order_cust_account_id
6640   , order_party_id
6641   , m.order_org_id
6642   , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
6643                         NVL( m.line_request_date,
6644                         NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) )
6645   ;
6646 
6647 
6648   IF G_debug_flag = 'Y'
6649   THEN
6650     oe_debug_pub.add( 'G_ORDER_TAX_HOLDS done at ' || DO_TIME, 2 );
6651   END IF;
6652 END IF; -- IF l_order_hold_bal
6653 
6654 -------------------------------
6655 ---------- RETURNS-------------
6656 --  Return Holds --------------
6657 -------------------------------
6658 --  This is a repeat of all the order balance types, but this
6659 --  time qualified by existing holds.
6660 --  In the Get_Exposure procedure, this negative balance
6661 --  is added if the Include Orders on Hold box is checked in the credit
6662 --  check rule
6663 
6664 -- balance type 30
6665 
6666   INSERT INTO OE_CREDIT_SUMMARIES
6667   ( balance
6668   , balance_type
6669   , site_use_id
6670   , cust_account_id
6671   , party_id
6672   , org_id
6673   , currency_code
6674   , bucket
6675   , bucket_duration
6676   , creation_date
6677   , created_by
6678   , last_update_date
6679   , last_updated_by
6680   , last_update_login
6681   , program_application_id
6682   , program_id
6683   , program_update_date
6684   , request_id
6685   )
6686 
6687 
6688   SELECT
6689     SUM( NVL( l.ordered_quantity, 0 )
6690          * NVL( l.unit_selling_price, 0 ) )
6691     - SUM( NVL( p.commitment_applied_amount, 0 ) )
6692     , G_ORDER_RETURN_HOLDS
6693     , h.invoice_to_org_id
6694     , s.cust_account_id
6695     , ca.party_id
6696     , h.org_id
6697     , h.transactional_curr_code
6698     , -2
6699     , OE_CREDIT_EXPOSURE_PVT.G_MAX_BUCKET_LENGTH
6700     , sysdate
6701     , l_created_by
6702     , sysdate
6703     , l_last_updated_by
6704     , l_last_update_login
6705     , l_program_application_id
6706     , l_program_id
6707     , SYSDATE
6708     , l_request_id
6709 
6710   FROM
6711          oe_order_lines_all             l
6712        , oe_order_headers_all           h
6713        , oe_payments                    p
6714        , hz_cust_site_uses_all          su
6715        , hz_cust_acct_sites_all         s
6716        , hz_cust_accounts               ca
6717   WHERE
6718            h.header_id                    =  l.header_id
6719 
6720     AND    h.booked_flag                  =  'Y'
6721     AND    h.open_flag                    =  'Y'
6722     AND    l.open_flag                    =  'Y'
6723     AND    l.line_category_code           =  'RETURN'
6724     AND    NVL( l.invoiced_quantity, 0 )  =  0
6725 
6726     AND    p.header_id  (+)               =  l.header_id
6727     AND    p.line_id    (+)               =  l.line_id
6728 
6729     AND    su.site_use_id                 =  h.invoice_to_org_id
6730     AND    su.cust_acct_site_id           =  s.cust_acct_site_id
6731     AND    ca.cust_account_id             =  s.cust_account_id
6732     AND    EXISTS
6733             ( SELECT  1
6734               FROM    oe_order_holds_all  oh
6735               WHERE   oh.header_id          =  h.header_id
6736               AND    (  oh.line_id        =  l.line_id
6737                       OR  oh.line_id IS NULL
6738                      )
6739               AND     oh.hold_release_id  IS NULL
6740             )
6741     AND    EXISTS
6742            ( SELECT  NULL
6743              FROM  oe_payment_types_all t
6744              WHERE t.credit_check_flag = 'Y'
6745              AND   NVL(t.org_id,-99) = NVL(h.org_id, -99)
6746              AND   l.header_id = h.header_id
6747              AND   t.payment_type_code =
6748                    DECODE(l.payment_type_code, NULL,
6749                      DECODE(h.payment_type_code, NULL, t.payment_type_code,
6750                             h.payment_type_code),
6751                           l.payment_type_code)
6752            )
6753 
6754 
6755 
6756   GROUP BY
6757     h.invoice_to_org_id
6758   , h.transactional_curr_code
6759   , s.cust_account_id
6760   , ca.party_id
6761   , h.org_id
6762   ;
6763 
6764 
6765   IF G_debug_flag = 'Y'
6766   THEN
6767     oe_debug_pub.add( 'G_ORDER_RETURN_HOLDS done at ' || DO_TIME, 2 );
6768   END IF;
6769 
6770 -- balance type 32
6771 
6772   INSERT INTO OE_CREDIT_SUMMARIES
6773   ( balance
6774   , balance_type
6775   , site_use_id
6776   , cust_account_id
6777   , party_id
6778   , org_id
6779   , currency_code
6780   , bucket
6781   , bucket_duration
6782   , creation_date
6783   , created_by
6784   , last_update_date
6785   , last_updated_by
6786   , last_update_login
6787   , program_application_id
6788   , program_id
6789   , program_update_date
6790   , request_id
6791   )
6792 
6793 
6794   SELECT
6795     SUM( NVL( m.line_tax_value, 0 ) )
6796     , G_ORDER_RETURN_TAX_HOLDS
6797     , m.order_invoice_to_org_id
6798     , order_cust_account_id
6799     , order_party_id
6800     , m.order_org_id
6801     , m.order_transactional_curr_code
6802     , -2
6803     , OE_CREDIT_EXPOSURE_PVT.G_MAX_BUCKET_LENGTH
6804     , sysdate
6805     , l_created_by
6806     , sysdate
6807     , l_last_updated_by
6808     , l_last_update_login
6809     , l_program_application_id
6810     , l_program_id
6811     , SYSDATE
6812     , l_request_id
6813 
6814   FROM
6815 	OE_INIT_CREDIT_SUMM_GTT		m
6816   WHERE
6817 	   m.line_line_category_code           =  'RETURN'
6818     AND    EXISTS
6819             ( SELECT  1
6820               FROM    oe_order_holds_all  oh
6821               WHERE   oh.header_id          =  m.order_header_id
6822               AND    (  oh.line_id        =  m.line_line_id
6823                       OR  oh.line_id IS NULL
6824                      )
6825               AND     oh.hold_release_id  IS NULL
6826             )
6827   GROUP BY
6828     m.order_invoice_to_org_id
6829   , m.order_transactional_curr_code
6830   , order_cust_account_id
6831   , order_party_id
6832   , m.order_org_id
6833   ;
6834 
6835 
6836   IF G_debug_flag = 'Y'
6837   THEN
6838     oe_debug_pub.add( 'G_ORDER_RETURN_TAX_HOLDS  done at ' || DO_TIME, 2 );
6839   END IF;
6840 
6841 IF l_order_hold_bal = 'Y' THEN
6842 --------------------------
6843 --  Line Uninvoiced Orders Holds
6844 --------------------------
6845 --  Note this is identical code except that
6846 --  h.invoice_to_org_id is replaced with l.invoice_to_org_id
6847 
6848 -- balance type 11
6849 
6850   INSERT INTO OE_CREDIT_SUMMARIES
6851   ( balance
6852   , balance_type
6853   , site_use_id
6854   , cust_account_id
6855   , party_id
6856   , org_id
6857   , currency_code
6858   , bucket
6859   , creation_date
6860   , created_by
6861   , last_update_date
6862   , last_updated_by
6863   , last_update_login
6864   , program_application_id
6865   , program_id
6866   , program_update_date
6867   , request_id
6868   , bucket_duration
6869   )
6870 
6871 
6872   SELECT
6873     - SUM( NVL( m.line_ordered_quantity, 0 )
6874          * NVL( m.line_unit_selling_price, 0 ) )
6875     + SUM( NVL( p.commitment_applied_amount, 0 ) )
6876     , G_LINE_HOLDS
6877     , m.line_invoice_to_org_id
6878     , line_cust_account_id
6879     , line_party_id
6880     , m.order_org_id
6881     , m.order_transactional_curr_code
6882     , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
6883                           NVL( m.line_request_date,
6884                           NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) )
6885     , SYSDATE
6886     , l_created_by
6887     , SYSDATE
6888     , l_last_updated_by
6889     , l_last_update_login
6890     , l_program_application_id
6891     , l_program_id
6892     , SYSDATE
6893     , l_request_id
6894     , 1
6895 
6896   FROM
6897          OE_INIT_CREDIT_SUMM_GTT        m
6898        , oe_payments                    p
6899   WHERE
6900            m.line_line_category_code           =  'ORDER'
6901     AND    p.header_id  (+)               =  m.line_header_id
6902     AND    p.line_id    (+)               =  m.line_line_id
6903     AND    EXISTS
6904             ( SELECT  1
6905               FROM    oe_order_holds_all  oh
6906               WHERE   oh.header_id          =  m.order_header_id
6907               AND    (  oh.line_id        =  m.line_line_id
6908                       OR  oh.line_id IS NULL
6909                      )
6910               AND     oh.hold_release_id  IS NULL
6911             )
6912   GROUP BY
6913      m.line_invoice_to_org_id
6914    , m.order_transactional_curr_code
6915    , line_cust_account_id
6916    , line_party_id
6917    , m.order_org_id
6918    , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
6919                          NVL( m.line_request_date,
6920                          NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) )
6921   ;
6922 
6923 
6924   IF G_debug_flag = 'Y'
6925   THEN
6926     oe_debug_pub.add( 'G_LINE_HOLDS done at ' || DO_TIME, 2 );
6927   END IF;
6928 
6929 -- balance type 14
6930 
6931   INSERT INTO OE_CREDIT_SUMMARIES
6932   ( balance
6933   , balance_type
6934   , site_use_id
6935   , cust_account_id
6936   , party_id
6937   , org_id
6938   , currency_code
6939   , bucket
6940   , creation_date
6941   , created_by
6942   , last_update_date
6943   , last_updated_by
6944   , last_update_login
6945   , program_application_id
6946   , program_id
6947   , program_update_date
6948   , request_id
6949   , bucket_duration
6950   )
6951 
6952 
6953   SELECT
6954     - SUM( NVL( m.line_tax_value, 0 ) )
6955     , G_LINE_TAX_HOLDS
6956     , m.line_invoice_to_org_id
6957     , line_cust_account_id
6958     , line_party_id
6959     , m.order_org_id
6960     , m.order_transactional_curr_code
6961     , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
6962                           NVL( m.line_request_date,
6963                           NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) )
6964     , SYSDATE
6965     , l_created_by
6966     , SYSDATE
6967     , l_last_updated_by
6968     , l_last_update_login
6969     , l_program_application_id
6970     , l_program_id
6971     , SYSDATE
6972     , l_request_id
6973     , 1
6974 
6975   FROM
6976          OE_INIT_CREDIT_SUMM_GTT	m
6977   WHERE
6978            m.line_line_category_code           =  'ORDER'
6979     AND    EXISTS
6980             ( SELECT  1
6981               FROM    oe_order_holds_all  oh
6982               WHERE   oh.header_id          =  m.order_header_id
6983               AND    (  oh.line_id        =  m.line_line_id
6984                       OR  oh.line_id IS NULL
6985                      )
6986               AND     oh.hold_release_id  IS NULL
6987             )
6988   GROUP BY
6989      m.line_invoice_to_org_id
6990    , m.order_transactional_curr_code
6991    , line_cust_account_id
6992    , line_party_id
6993    , m.order_org_id
6994    , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
6995                          NVL( m.line_request_date,
6996                          NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) )
6997   ;
6998 
6999 
7000   IF G_debug_flag = 'Y'
7001   THEN
7002     oe_debug_pub.add( 'G_LINE_TAX_HOLDS done at ' || DO_TIME, 2 );
7003   END IF;
7004 END IF; -- IF l_order_hold_bal
7005 
7006 --------------------------------
7007 ----- RETURNS ------------------
7008 --  Line Uninvoiced Orders Holds
7009 --------------------------------
7010 --  Note this is identical code except that
7011 --  h.invoice_to_org_id is replaced with l.invoice_to_org_id
7012 
7013 -- balance type 31
7014 
7015   INSERT INTO OE_CREDIT_SUMMARIES
7016   ( balance
7017   , balance_type
7018   , site_use_id
7019   , cust_account_id
7020   , party_id
7021   , org_id
7022   , currency_code
7023   , bucket
7024   , bucket_duration
7025   , creation_date
7026   , created_by
7027   , last_update_date
7028   , last_updated_by
7029   , last_update_login
7030   , program_application_id
7031   , program_id
7032   , program_update_date
7033   , request_id
7034   )
7035 
7036 
7037   SELECT
7038     SUM( NVL( m.line_ordered_quantity, 0 )
7039          * NVL( m.line_unit_selling_price, 0 ) )
7040     - SUM( NVL( p.commitment_applied_amount, 0 ) )
7041     , G_LINE_RETURN_HOLDS
7042     , m.line_invoice_to_org_id
7043     , line_cust_account_id
7044     , line_party_id
7045     , m.order_org_id
7046     , m.order_transactional_curr_code
7047     , -2
7048     , OE_CREDIT_EXPOSURE_PVT.G_MAX_BUCKET_LENGTH
7049     , sysdate
7050     , l_created_by
7051     , sysdate
7052     , l_last_updated_by
7053     , l_last_update_login
7054     , l_program_application_id
7055     , l_program_id
7056     , SYSDATE
7057     , l_request_id
7058 
7059   FROM
7060   	 OE_INIT_CREDIT_SUMM_GTT	m
7061        , oe_payments                    p
7062   WHERE
7063            m.line_line_category_code           =  'RETURN'
7064     AND    p.header_id  (+)               =  m.line_header_id
7065     AND    p.line_id    (+)               =  m.line_line_id
7066     AND    EXISTS
7067              ( SELECT  1
7068               FROM    oe_order_holds_all  oh
7069               WHERE   oh.header_id          =  m.order_header_id
7070               AND    (  oh.line_id        =  m.line_line_id
7071                       OR  oh.line_id IS NULL
7072                      )
7073               AND     oh.hold_release_id  IS NULL
7074             )
7075   GROUP BY
7076      m.line_invoice_to_org_id
7077    , m.order_transactional_curr_code
7078    , line_cust_account_id
7079    , line_party_id
7080    , m.order_org_id
7081   ;
7082 
7083 
7084   IF G_debug_flag = 'Y'
7085   THEN
7086     oe_debug_pub.add( 'G_LINE_RETURN_HOLDS done at ' || DO_TIME, 2 );
7087   END IF;
7088 
7089 -- balance type 33
7090 
7091   INSERT INTO OE_CREDIT_SUMMARIES
7092   ( balance
7093   , balance_type
7094   , site_use_id
7095   , cust_account_id
7096   , party_id
7097   , org_id
7098   , currency_code
7099   , bucket
7100   , bucket_duration
7101   , creation_date
7102   , created_by
7103   , last_update_date
7104   , last_updated_by
7105   , last_update_login
7106   , program_application_id
7107   , program_id
7108   , program_update_date
7109   , request_id
7110   )
7111 
7112 
7113   SELECT
7114     SUM( NVL( m.line_tax_value, 0 ) )
7115     , G_LINE_RETURN_TAX_HOLDS
7116     , m.line_invoice_to_org_id
7117     , line_cust_account_id
7118     , line_party_id
7119     , m.order_org_id
7120     , m.order_transactional_curr_code
7121     , -2
7122     , OE_CREDIT_EXPOSURE_PVT.G_MAX_BUCKET_LENGTH
7123     , sysdate
7124     , l_created_by
7125     , sysdate
7126     , l_last_updated_by
7127     , l_last_update_login
7128     , l_program_application_id
7129     , l_program_id
7130     , SYSDATE
7131     , l_request_id
7132 
7133   FROM
7134 	OE_INIT_CREDIT_SUMM_GTT		m
7135   WHERE
7136            m.line_line_category_code           =  'RETURN'
7137     AND    EXISTS
7138             ( SELECT  1
7139               FROM    oe_order_holds_all  oh
7140               WHERE   oh.header_id          =  m.order_header_id
7141               AND    (  oh.line_id        =  m.line_line_id
7142                       OR  oh.line_id IS NULL
7143                      )
7144               AND     oh.hold_release_id  IS NULL
7145             )
7146   GROUP BY
7147      m.line_invoice_to_org_id
7148    , m.order_transactional_curr_code
7149    , line_cust_account_id
7150    , line_party_id
7151    , m.order_org_id
7152   ;
7153 
7154   IF G_debug_flag = 'Y'
7155   THEN
7156     oe_debug_pub.add( 'G_LINE_RETURN_TAX_HOLDS done at ' || DO_TIME, 2 );
7157   END IF;
7158 
7159 
7160 IF l_order_hold_bal = 'Y' THEN
7161 --------------------------
7162 --  Line Freight Holds
7163 --------------------------
7164 
7165 -- balance type 16
7166 
7167   INSERT INTO OE_CREDIT_SUMMARIES
7168   ( balance
7169   , balance_type
7170   , site_use_id
7171   , cust_account_id
7172   , party_id
7173   , org_id
7174   , currency_code
7175   , bucket
7176   , creation_date
7177   , created_by
7178   , last_update_date
7179   , last_updated_by
7180   , last_update_login
7181   , program_application_id
7182   , program_id
7183   , program_update_date
7184   , request_id
7185   , bucket_duration
7186   )
7187 
7188   SELECT
7189     - SUM
7190       ( DECODE( p.credit_or_charge_flag, 'C', (-1), (+1) )
7191       * DECODE( p.arithmetic_operator, 'LUMPSUM',			--bug 4295298
7192                 p.operand, (m.line_ordered_quantity * p.adjusted_amount))
7193       )
7194     , G_LINE_FREIGHT_HOLDS
7195     , m.line_invoice_to_org_id
7196     , line_cust_account_id
7197     , line_party_id
7198     , m.order_org_id
7199     , m.order_transactional_curr_code
7200     , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
7201                           NVL( m.line_request_date,
7202                           NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) )
7203     , SYSDATE
7204     , l_created_by
7205     , SYSDATE
7206     , l_last_updated_by
7207     , l_last_update_login
7208     , l_program_application_id
7209     , l_program_id
7210     , SYSDATE
7211     , l_request_id
7212     , 1
7213 
7214   FROM
7215       oe_price_adjustments     p,
7216       OE_INIT_CREDIT_SUMM_GTT	m
7217   WHERE
7218          p.line_id             =  m.line_line_id
7219     AND  p.header_id           =  m.line_header_id
7220     AND  p.header_id           =  m.order_header_id
7221     AND  m.line_line_category_code           =  'ORDER'
7222     AND  p.applied_flag        =  'Y'
7223     AND  p.list_line_type_code =  'FREIGHT_CHARGE'
7224     AND  NVL( p.invoiced_flag, 'N' )  =  'N'
7225     AND    EXISTS
7226             ( SELECT  1
7227               FROM    oe_order_holds_all  oh
7228               WHERE   oh.header_id          =  m.order_header_id
7229               AND    (  oh.line_id        =  m.line_line_id
7230                       OR  oh.line_id IS NULL
7231                      )
7232               AND     oh.hold_release_id  IS NULL
7233             )
7234   GROUP BY
7235       m.line_invoice_to_org_id
7236     , m.order_transactional_curr_code
7237     , line_cust_account_id
7238     , line_party_id
7239     , m.order_org_id
7240     , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
7241                           NVL( m.line_request_date,
7242                           NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) )
7243   ;
7244 
7245 
7246   IF G_debug_flag = 'Y'
7247   THEN
7248     oe_debug_pub.add( 'G_LINE_FREIGHT_HOLDS done at ' || DO_TIME, 2 );
7249   END IF;
7250 END IF; -- IF l_order_hold_bal
7251 
7252 ---------------------------------
7253 --------- RETURNS ---------------
7254 --  Line Freight Holds ----------
7255 ---------------------------------
7256 
7257 -- balance type 35
7258 
7259   INSERT INTO OE_CREDIT_SUMMARIES
7260   ( balance
7261   , balance_type
7262   , site_use_id
7263   , cust_account_id
7264   , party_id
7265   , org_id
7266   , currency_code
7267   , bucket
7268   , bucket_duration
7269   , creation_date
7270   , created_by
7271   , last_update_date
7272   , last_updated_by
7273   , last_update_login
7274   , program_application_id
7275   , program_id
7276   , program_update_date
7277   , request_id
7278   )
7279 
7280   SELECT
7281     - SUM
7282       ( DECODE( p.credit_or_charge_flag, 'C', (-1), (+1) )
7283       * DECODE( p.arithmetic_operator, 'LUMPSUM',			--bug 4295298
7284                 p.operand, (m.line_ordered_quantity * p.adjusted_amount))
7285       )
7286     , G_LINE_RETURN_FREIGHT_HOLDS
7287     , m.line_invoice_to_org_id
7288     , line_cust_account_id
7289     , line_party_id
7290     , m.order_org_id
7291     , m.order_transactional_curr_code
7292     , -2
7293     , OE_CREDIT_EXPOSURE_PVT.G_MAX_BUCKET_LENGTH
7294     , sysdate
7295     , l_created_by
7296     , sysdate
7297     , l_last_updated_by
7298     , l_last_update_login
7299     , l_program_application_id
7300     , l_program_id
7301     , SYSDATE
7302     , l_request_id
7303 
7304   FROM
7305       oe_price_adjustments     p,
7306       OE_INIT_CREDIT_SUMM_GTT  m
7307   WHERE
7308          p.line_id             =  m.line_line_id
7309     AND  p.header_id           =  m.line_header_id
7310     AND  p.header_id           =  m.order_header_id
7311     AND  m.line_line_category_code           =  'RETURN'
7312     AND  p.applied_flag        =  'Y'
7313     AND  p.list_line_type_code =  'FREIGHT_CHARGE'
7314     AND  NVL( p.invoiced_flag, 'N' )  =  'N'
7315     AND    EXISTS
7316             ( SELECT  1
7317               FROM    oe_order_holds_all  oh
7318               WHERE   oh.header_id          =  m.order_header_id
7319               AND    (  oh.line_id        =  m.line_line_id
7320                       OR  oh.line_id IS NULL
7321                      )
7322               AND     oh.hold_release_id  IS NULL
7323             )
7324   GROUP BY
7325       m.line_invoice_to_org_id
7326     , m.order_transactional_curr_code
7327     , line_cust_account_id
7328     , line_party_id
7329     , m.order_org_id
7330   ;
7331 
7332 
7333   IF G_debug_flag = 'Y'
7334   THEN
7335     oe_debug_pub.add( 'G_LINE_RETURN_FREIGHT_HOLDS done at ' || DO_TIME, 2 );
7336   END IF;
7337 
7338 
7339 IF l_order_hold_bal = 'Y' THEN
7340 --------------------------
7341 --  Header Freight Holds
7342 --------------------------
7343 
7344 -- balance type 15
7345 
7346   INSERT INTO OE_CREDIT_SUMMARIES
7347   ( balance
7348   , balance_type
7349   , site_use_id
7350   , cust_account_id
7351   , party_id
7352   , org_id
7353   , currency_code
7354   , bucket
7355   , creation_date
7356   , created_by
7357   , last_update_date
7358   , last_updated_by
7359   , last_update_login
7360   , program_application_id
7361   , program_id
7362   , program_update_date
7363   , request_id
7364   , bucket_duration
7365   )
7366 
7367 
7368 SELECT
7369     - SUM
7370       ( DECODE( p.credit_or_charge_flag, 'C', (-1), (+1) )
7371       * DECODE( p.arithmetic_operator, 'LUMPSUM',			--bug 4295298
7372                 p.operand, (m.line_ordered_quantity * p.adjusted_amount))
7373       )
7374     , G_ORDER_FREIGHT_HOLDS
7375     , m.order_invoice_to_org_id
7376     , order_cust_account_id
7377     , order_party_id
7378     , m.order_org_id
7379     , m.order_transactional_curr_code
7380     , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
7381                           NVL( m.line_request_date,
7382                           NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) )
7383     , SYSDATE
7384     , l_created_by
7385     , SYSDATE
7386     , l_last_updated_by
7387     , l_last_update_login
7388     , l_program_application_id
7389     , l_program_id
7390     , SYSDATE
7391     , l_request_id
7392     , 1
7393 
7394 FROM   oe_price_adjustments     p,
7395 	OE_INIT_CREDIT_SUMM_GTT  m
7396 WHERE
7397        p.line_id             =  m.line_line_id
7398   AND  p.header_id           =  m.line_header_id
7399   AND  p.header_id           =  m.order_header_id
7400   AND  m.line_line_category_code           =  'ORDER'
7401   AND  p.applied_flag                 =  'Y'
7402   AND  p.list_line_type_code          =  'FREIGHT_CHARGE'
7403   AND  NVL( p.invoiced_flag, 'N' )    =  'N'
7404   AND  EXISTS
7405         ( SELECT  1
7406           FROM    oe_order_holds_all  oh
7407           WHERE   oh.header_id          =  m.order_header_id
7408           AND    (  oh.line_id        =  m.line_line_id
7409                   OR  oh.line_id IS NULL
7410                  )
7411           AND     oh.hold_release_id  IS NULL
7412         )
7413 GROUP BY
7414     m.order_invoice_to_org_id
7415   , m.order_transactional_curr_code
7416   , order_cust_account_id
7417   , order_party_id
7418   , m.order_org_id
7419   , TO_NUMBER( TO_CHAR( NVL( m.line_schedule_ship_date,
7420                         NVL( m.line_request_date,
7421                         NVL( m.order_request_date, m.order_creation_date) ) ), 'J' ) )
7422 ;
7423 
7424 
7425   IF G_debug_flag = 'Y'
7426   THEN
7427     oe_debug_pub.add( 'G_ORDER_FREIGHT_HOLDS done at ' || DO_TIME, 2 );
7428   END IF;
7429 END IF; -- IF l_order_hold_bal
7430 
7431 --------------------------------------
7432 -------- RETURNS ---------------------
7433 --  Header Freight Holds -------------
7434 --------------------------------------
7435 
7436 -- balance type 34
7437 
7438   INSERT INTO OE_CREDIT_SUMMARIES
7439   ( balance
7440   , balance_type
7441   , site_use_id
7442   , cust_account_id
7443   , party_id
7444   , org_id
7445   , currency_code
7446   , bucket
7447   , bucket_duration
7448   , creation_date
7449   , created_by
7450   , last_update_date
7451   , last_updated_by
7452   , last_update_login
7453   , program_application_id
7454   , program_id
7455   , program_update_date
7456   , request_id
7457   )
7458 
7459 
7460 SELECT
7461     - SUM
7462       ( DECODE( p.credit_or_charge_flag, 'C', (-1), (+1) )
7463       * DECODE( p.arithmetic_operator, 'LUMPSUM',			--bug 4295298
7464                 p.operand, (m.line_ordered_quantity * p.adjusted_amount))
7465       )
7466     , G_ORDER_RETURN_FREIGHT_HOLDS
7467     , m.order_invoice_to_org_id
7468     , order_cust_account_id
7469     , order_party_id
7470     , m.order_org_id
7471     , m.order_transactional_curr_code
7472     , -2
7473     , OE_CREDIT_EXPOSURE_PVT.G_MAX_BUCKET_LENGTH
7474     , sysdate
7475     , l_created_by
7476     , sysdate
7477     , l_last_updated_by
7478     , l_last_update_login
7479     , l_program_application_id
7480     , l_program_id
7481     , SYSDATE
7482     , l_request_id
7483 
7484 FROM   oe_price_adjustments     p,
7485 	OE_INIT_CREDIT_SUMM_GTT	m
7486 WHERE
7487        p.line_id             =  m.line_line_id
7488   AND  p.header_id           =  m.line_header_id
7489   AND  p.header_id           =  m.order_header_id
7490   AND  m.line_line_category_code           =  'RETURN'
7491   AND  p.applied_flag                 =  'Y'
7492   AND  p.list_line_type_code          =  'FREIGHT_CHARGE'
7493   AND  NVL( p.invoiced_flag, 'N' )    =  'N'
7494   AND  EXISTS
7495         ( SELECT  1
7496           FROM    oe_order_holds_all  oh
7497           WHERE   oh.header_id          =  m.order_header_id
7498           AND    (  oh.line_id        =  m.line_line_id
7499                   OR  oh.line_id IS NULL
7500                  )
7501           AND     oh.hold_release_id  IS NULL
7502         )
7503 GROUP BY
7504     m.order_invoice_to_org_id
7505   , m.order_transactional_curr_code
7506   , order_cust_account_id
7507   , order_party_id
7508   , m.order_org_id
7509 ;
7510 
7511 
7512   IF G_debug_flag = 'Y'
7513   THEN
7514     oe_debug_pub.add( 'G_ORDER_RETURN_FREIGHT_HOLDS done at ' || DO_TIME, 2 );
7515   END IF;
7516 
7517 
7518 IF l_order_hold_bal = 'Y' THEN
7519 --------------------------
7520 --  Header + Line Freight Part 2 Holds
7521 --------------------------
7522 
7523 -- balance type 17
7524 
7525   INSERT INTO OE_CREDIT_SUMMARIES
7526   ( balance
7527   , balance_type
7528   , site_use_id
7529   , cust_account_id
7530   , party_id
7531   , org_id
7532   , currency_code
7533   , bucket
7534   , creation_date
7535   , created_by
7536   , last_update_date
7537   , last_updated_by
7538   , last_update_login
7539   , program_application_id
7540   , program_id
7541   , program_update_date
7542   , request_id
7543   , bucket_duration
7544   )
7545 
7546 
7547 SELECT
7548     - SUM( DECODE( p.credit_or_charge_flag, 'C', (-1), (+1) ) * p.operand )
7549     , G_HEADER_LINE_FREIGHT_HOLDS
7550     , h.invoice_to_org_id
7551     , s.cust_account_id
7552     , ca.party_id
7553     , h.org_id
7554     , h.transactional_curr_code
7555     , TO_NUMBER( TO_CHAR( NVL( h.request_date, h.creation_date ), 'J' ) )
7556     , SYSDATE
7557     , l_created_by
7558     , SYSDATE
7559     , l_last_updated_by
7560     , l_last_update_login
7561     , l_program_application_id
7562     , l_program_id
7563     , SYSDATE
7564     , l_request_id
7565     , 1
7566 
7567 FROM
7568     oe_price_adjustments      p
7569   , oe_order_headers_all      h
7570   , hz_cust_site_uses_all     su
7571   , hz_cust_acct_sites_all    s
7572   , hz_cust_accounts          ca
7573 WHERE
7574        p.line_id IS NULL
7575   AND  p.header_id           =  h.header_id
7576   AND  h.order_category_code IN ('ORDER','MIXED')
7577   AND  h.open_flag           =  'Y'
7578   AND  h.booked_flag         =  'Y'
7579   AND  p.applied_flag        =  'Y'
7580   AND  p.list_line_type_code = 'FREIGHT_CHARGE'
7581   AND  NVL( p.invoiced_flag, 'N' )  =  'N'
7582   AND  su.site_use_id        =  h.invoice_to_org_id
7583   AND  su.cust_acct_site_id  =  s.cust_acct_site_id
7584   AND  ca.cust_account_id    =  s.cust_account_id
7585   AND  EXISTS
7586         ( SELECT  1
7587           FROM    oe_order_holds_all  oh
7588           WHERE   oh.header_id            =  h.header_id
7589           AND     oh.hold_release_id  IS NULL
7590         )
7591   AND  EXISTS
7592          ( SELECT  NULL
7593              FROM  oe_payment_types_all t,
7594                    oe_order_lines_all l
7595              WHERE t.credit_check_flag = 'Y'
7596              AND   NVL(t.org_id,-99) = NVL(h.org_id, -99)
7597              AND   l.header_id = h.header_id
7598              AND   t.payment_type_code =
7599                    DECODE(l.payment_type_code, NULL,
7600                      DECODE(h.payment_type_code, NULL, t.payment_type_code,
7601                             h.payment_type_code),
7602                           l.payment_type_code)
7603          )
7604 GROUP BY
7605       h.invoice_to_org_id
7606     , h.transactional_curr_code
7607     , s.cust_account_id
7608     , ca.party_id
7609     , h.org_id
7610     , TO_NUMBER( TO_CHAR( NVL( h.request_date, h.creation_date ), 'J' ) )
7611 ;
7612 
7613   IF G_debug_flag = 'Y'
7614   THEN
7615     oe_debug_pub.add( 'G_HEADER_LINE_FREIGHT_HOLDS done at ' || DO_TIME, 2 );
7616   END IF;
7617 END IF; -- IF l_order_hold_bal
7618 
7619 --------------------------------------
7620 ------------- RETURNS ----------------
7621 --  Header + Line Fright Part 2 Holds
7622 --------------------------------------
7623 
7624 -- balance type 36
7625 
7626   INSERT INTO OE_CREDIT_SUMMARIES
7627   ( balance
7628   , balance_type
7629   , site_use_id
7630   , cust_account_id
7631   , party_id
7632   , org_id
7633   , currency_code
7634   , bucket
7635   , bucket_duration
7636   , creation_date
7637   , created_by
7638   , last_update_date
7639   , last_updated_by
7640   , last_update_login
7641   , program_application_id
7642   , program_id
7643   , program_update_date
7644   , request_id
7645   )
7646 
7647 
7648 SELECT
7649     - SUM( DECODE( p.credit_or_charge_flag, 'C', (-1), (+1) ) * p.operand )
7650     , G_H_L_RETURN_FREIGHT_HOLDS
7651     , h.invoice_to_org_id
7652     , s.cust_account_id
7653     , ca.party_id
7654     , h.org_id
7655     , h.transactional_curr_code
7656     , -2
7657     , OE_CREDIT_EXPOSURE_PVT.G_MAX_BUCKET_LENGTH
7658     , sysdate
7659     , l_created_by
7660     , sysdate
7661     , l_last_updated_by
7662     , l_last_update_login
7663     , l_program_application_id
7664     , l_program_id
7665     , SYSDATE
7666     , l_request_id
7667 
7668 FROM
7669     oe_price_adjustments      p
7670   , oe_order_headers_all      h
7671   , hz_cust_site_uses_all     su
7672   , hz_cust_acct_sites_all    s
7673   , hz_cust_accounts          ca
7674 WHERE
7675        p.line_id IS NULL
7676   AND  p.header_id           =  h.header_id
7677   AND  h.order_category_code = 'RETURN'
7678   AND  h.open_flag           =  'Y'
7679   AND  h.booked_flag         =  'Y'
7680   AND  p.applied_flag        =  'Y'
7681   AND  p.list_line_type_code = 'FREIGHT_CHARGE'
7682   AND  NVL( p.invoiced_flag, 'N' )  =  'N'
7683   AND  su.site_use_id        =  h.invoice_to_org_id
7684   AND  su.cust_acct_site_id  =  s.cust_acct_site_id
7685   AND  ca.cust_account_id    =  s.cust_account_id
7686   AND  EXISTS
7687         ( SELECT  1
7688           FROM    oe_order_holds_all  oh
7689           WHERE   oh.header_id     =  h.header_id
7690           AND     oh.hold_release_id  IS NULL
7691         )
7692   AND  EXISTS
7693          ( SELECT  NULL
7694              FROM  oe_payment_types_all t,
7695                    oe_order_lines_all l
7696              WHERE t.credit_check_flag = 'Y'
7697              AND   NVL(t.org_id,-99) = NVL(h.org_id, -99)
7698              AND   l.header_id = h.header_id
7699              AND   t.payment_type_code =
7700                    DECODE(l.payment_type_code, NULL,
7701                      DECODE(h.payment_type_code, NULL, t.payment_type_code,
7702                             h.payment_type_code),
7703                           l.payment_type_code)
7704          )
7705 GROUP BY
7706       h.invoice_to_org_id
7707     , h.transactional_curr_code
7708     , s.cust_account_id
7709     , ca.party_id
7710     , h.org_id
7711 ;
7712 
7713   IF G_debug_flag = 'Y'
7714   THEN
7715     oe_debug_pub.add( 'G_H_L_RETURN_FREIGHT_HOLDS done at ' || DO_TIME, 2 );
7716   END IF;
7717 
7718 
7719 
7720 --------------------------
7721 --  Invoices
7722 --------------------------
7723 -- The term in ther where clause containing the hard-coded
7724 -- GL date was recommended for performance reasons
7725 -- by AR
7726 
7727 -- balance type 8
7728 
7729   INSERT INTO OE_CREDIT_SUMMARIES
7730   ( balance
7731   , balance_type
7732   , site_use_id
7733   , cust_account_id
7734   , party_id
7735   , org_id
7736   , currency_code
7737   , bucket
7738   , creation_date
7739   , created_by
7740   , last_update_date
7741   , last_updated_by
7742   , last_update_login
7743   , program_application_id
7744   , program_id
7745   , program_update_date
7746   , request_id
7747   , bucket_duration
7748   )
7749 
7750   SELECT
7751     SUM( sch.amount_due_remaining )
7752     , G_INVOICES
7753     , sch.customer_site_use_id
7754     , sch.customer_id
7755     , ca.party_id
7756     , sch.org_id
7757     , sch.invoice_currency_code
7758     , TO_NUMBER( TO_CHAR( sch.trx_date, 'J' ) )
7759     , sysdate
7760     , l_created_by
7761     , sysdate
7762     , l_last_updated_by
7763     , l_last_update_login
7764     , l_program_application_id
7765     , l_program_id
7766     , SYSDATE
7767     , l_request_id
7768     , 1
7769 
7770   FROM
7771     ar_payment_schedules_all     sch ,
7772     hz_cust_accounts             ca
7773     , hz_cust_site_uses_all      su
7774   WHERE
7775          NVL( receipt_confirmed_flag, 'Y' )  =  'Y'
7776     AND  gl_date_closed = to_date( '31-12-4712', 'DD-MM-YYYY')
7777     AND  ca.cust_account_id             =  sch.customer_id
7778     AND  su.site_use_id                 = sch.customer_site_use_id
7779     AND  su.site_use_code               <> 'DRAWEE'
7780   GROUP BY
7781       sch.customer_site_use_id
7782     , sch.invoice_currency_code
7783     , sch.customer_id
7784     , ca.party_id
7785     , sch.org_id
7786     , TO_NUMBER( TO_CHAR( sch.trx_date, 'J' ) )
7787   ;
7788 
7789 
7790   IF G_debug_flag = 'Y'
7791   THEN
7792     oe_debug_pub.add( 'G_INVOICES done at ' || DO_TIME, 2 );
7793   END IF;
7794 
7795 
7796 -----------------------------------------
7797 --  Pay Risk
7798 -----------------------------------------
7799 
7800 -- balance type 9
7801 
7802   INSERT INTO OE_CREDIT_SUMMARIES
7803   ( balance
7804   , balance_type
7805   , site_use_id
7806   , cust_account_id
7807   , party_id
7808   , org_id
7809   , currency_code
7810   , bucket
7811   , creation_date
7812   , created_by
7813   , last_update_date
7814   , last_updated_by
7815   , last_update_login
7816 
7817   , program_application_id
7818   , program_id
7819   , program_update_date
7820   , request_id
7821   , bucket_duration
7822   )
7823 
7824 
7825   SELECT
7826     SUM( crh.amount )  pay_risk
7827     , G_PAYMENTS_AT_RISK
7828     , cr.customer_site_use_id
7829     , cr.pay_from_customer
7830     , ca.party_id
7831     , cr.org_id
7832     , cr.currency_code
7833     , TO_NUMBER( TO_CHAR( cr.receipt_date, 'J' ) )
7834     , sysdate
7835     , l_created_by
7836     , sysdate
7837     , l_last_updated_by
7838     , l_last_update_login
7839 
7840     , l_program_application_id
7841     , l_program_id
7842     , SYSDATE
7843     , l_request_id
7844     , 1
7845 
7846   FROM
7847     ar_cash_receipts_all          cr
7848   , ar_cash_receipt_history_all   crh
7849   , hz_cust_accounts              ca
7850   , hz_cust_site_uses_all         su
7851   WHERE
7852          cr.cash_receipt_id             =  crh.cash_receipt_id
7853     AND  crh.current_record_flag        =  'Y'
7854     AND  NVL( cr.confirmed_flag, 'Y' )  =  'Y'
7855     AND  NVL( cr.reversal_category, cr.status || 'X' )  <>  cr.status
7856     AND  crh.status NOT IN
7857          (
7858            DECODE( crh.factor_flag
7859                  , 'Y', 'RISK_ELIMINATED'
7860                  , 'N', 'CLEARED'
7861                  )
7862          , 'REVERSED'
7863          )
7864     AND  NOT EXISTS
7865          (
7866            SELECT
7867              'X'
7868            FROM
7869              ar_receivable_applications_all        rap
7870            WHERE
7871                   rap.cash_receipt_id              =  cr.cash_receipt_id
7872              AND  rap.applied_payment_schedule_id  =  -2
7873              AND  rap.display                      =  'Y'
7874          )
7875   AND  ca.cust_account_id             =  cr.pay_from_customer
7876   AND  su.site_use_id                 = cr.customer_site_use_id
7877   AND  su.site_use_code               <> 'DRAWEE'
7878   GROUP BY
7879     cr.customer_site_use_id
7880   , cr.currency_code
7881   , cr.pay_from_customer
7882   , ca.party_id
7883   , cr.org_id
7884   , TO_NUMBER( TO_CHAR( cr.receipt_date, 'J' ) )
7885   ;
7886 
7887   IF G_debug_flag = 'Y'
7888   THEN
7889    oe_debug_pub.add( 'G_PAYMENTS_AT_RISK done at ' || DO_TIME, 2 );
7890   END IF;
7891 
7892 ---------------------- BR ------------------
7893 
7894 -- balance type 21
7895 
7896   INSERT INTO OE_CREDIT_SUMMARIES
7897   ( balance
7898   , balance_type
7899   , site_use_id
7900   , cust_account_id
7901   , party_id
7902   , org_id
7903   , currency_code
7904   , bucket
7905   , creation_date
7906   , created_by
7907   , last_update_date
7908   , last_updated_by
7909   , last_update_login
7910 
7911   , program_application_id
7912   , program_id
7913   , program_update_date
7914   , request_id
7915   , bucket_duration
7916   )
7917 
7918   SELECT
7919     SUM( sch.amount_due_remaining )
7920     , G_BR_INVOICES
7921     , sch.customer_site_use_id
7922     , sch.customer_id
7923     , ca.party_id
7924     , sch.org_id
7925     , sch.invoice_currency_code
7926     , TO_NUMBER( TO_CHAR( sch.trx_date, 'J' ) )
7927     , sysdate
7928     , l_created_by
7929     , sysdate
7930     , l_last_updated_by
7931     , l_last_update_login
7932 
7933     , l_program_application_id
7934     , l_program_id
7935     , SYSDATE
7936     , l_request_id
7937     , 1
7938 
7939   FROM
7940     ar_payment_schedules_all     sch ,
7941     hz_cust_accounts             ca
7942     , hz_cust_site_uses_all      su
7943   WHERE
7944          NVL( receipt_confirmed_flag, 'Y' )  =  'Y'
7945     AND  gl_date_closed = to_date( '31-12-4712', 'DD-MM-YYYY')
7946     AND  ca.cust_account_id             =  sch.customer_id
7947     AND  su.site_use_id                 = sch.customer_site_use_id
7948     AND  su.site_use_code               = 'DRAWEE'
7949   GROUP BY
7950       sch.customer_site_use_id
7951     , sch.invoice_currency_code
7952     , sch.customer_id
7953     , ca.party_id
7954     , sch.org_id
7955     , TO_NUMBER( TO_CHAR( sch.trx_date, 'J' ) )
7956   ;
7957 
7958 
7959   IF G_debug_flag = 'Y'
7960   THEN
7961     oe_debug_pub.add( 'G_BR_INVOICES done at ' || DO_TIME, 2 );
7962   END IF;
7963 
7964 
7965 -----------------------------------------
7966 --  Pay Risk
7967 -----------------------------------------
7968 
7969 -- balance type 22
7970 
7971   INSERT INTO OE_CREDIT_SUMMARIES
7972   ( balance
7973   , balance_type
7974   , site_use_id
7975   , cust_account_id
7976   , party_id
7977   , org_id
7978   , currency_code
7979   , bucket
7980   , creation_date
7981   , created_by
7982   , last_update_date
7983   , last_updated_by
7984   , last_update_login
7985 
7986   , program_application_id
7987   , program_id
7988   , program_update_date
7989   , request_id
7990   , bucket_duration
7991   )
7992 
7993 
7994   SELECT
7995     SUM( crh.amount )  pay_risk
7996     , G_BR_PAYMENTS_AT_RISK
7997     , cr.customer_site_use_id
7998     , cr.pay_from_customer
7999     , ca.party_id
8000     , cr.org_id
8001     , cr.currency_code
8002     , TO_NUMBER( TO_CHAR( cr.receipt_date, 'J' ) )
8003     , sysdate
8004     , l_created_by
8005     , sysdate
8006     , l_last_updated_by
8007     , l_last_update_login
8008 
8009     , l_program_application_id
8010     , l_program_id
8011     , SYSDATE
8012     , l_request_id
8013     , 1
8014 
8015   FROM
8016     ar_cash_receipts_all          cr
8017   , ar_cash_receipt_history_all   crh
8018   , hz_cust_accounts              ca
8019   , hz_cust_site_uses_all         su
8020   WHERE
8021          cr.cash_receipt_id             =  crh.cash_receipt_id
8022     AND  crh.current_record_flag        =  'Y'
8023     AND  NVL( cr.confirmed_flag, 'Y' )  =  'Y'
8024     AND  NVL( cr.reversal_category, cr.status || 'X' )  <>  cr.status
8025     AND  crh.status NOT IN
8026          (
8027            DECODE( crh.factor_flag
8028                  , 'Y', 'RISK_ELIMINATED'
8029                  , 'N', 'CLEARED'
8030                  )
8031          , 'REVERSED'
8032          )
8033     AND  NOT EXISTS
8034          (
8035            SELECT
8036              'X'
8037            FROM
8038              ar_receivable_applications_all        rap
8039            WHERE
8040                   rap.cash_receipt_id              =  cr.cash_receipt_id
8041              AND  rap.applied_payment_schedule_id  =  -2
8042              AND  rap.display                      =  'Y'
8043          )
8044   AND  ca.cust_account_id             =  cr.pay_from_customer
8045   AND  su.site_use_id                 = cr.customer_site_use_id
8046   AND  su.site_use_code               = 'DRAWEE'
8047   GROUP BY
8048     cr.customer_site_use_id
8049   , cr.currency_code
8050   , cr.pay_from_customer
8051   , ca.party_id
8052   , cr.org_id
8053   , TO_NUMBER( TO_CHAR( cr.receipt_date, 'J' ) )
8054   ;
8055 
8056   IF G_debug_flag = 'Y'
8057   THEN
8058     oe_debug_pub.add( 'G_PAYMENTS_AT_RISK done at ' || DO_TIME, 2 );
8059   END IF;
8060 
8061 -- Bug 4219133 : Insert Past Due Inv Balance only if needed.
8062 
8063 IF l_past_due_bal = 'Y' THEN
8064 
8065 ----------------------- past due inv -------------
8066 
8067  -- balance type 20
8068 
8069  INSERT INTO OE_CREDIT_SUMMARIES
8070   ( balance
8071   , balance_type
8072   , site_use_id
8073   , cust_account_id
8074   , party_id
8075   , org_id
8076   , currency_code
8077   , bucket
8078   , creation_date
8079   , created_by
8080   , last_update_date
8081   , last_updated_by
8082   , last_update_login
8083   , program_application_id
8084   , program_id
8085   , program_update_date
8086   , request_id
8087   , bucket_duration
8088   )
8089 
8090   SELECT
8091     COUNT(payment_schedule_id)
8092     , G_past_due_invoices
8093     , sch.customer_site_use_id
8094     , sch.customer_id
8095     , ca.party_id
8096     , sch.org_id
8097     , sch.invoice_currency_code
8098     , TO_NUMBER( TO_CHAR( sch.due_date, 'J' ) )
8099     , sysdate
8100     , l_created_by
8101     , sysdate
8102     , l_last_updated_by
8103     , l_last_update_login
8104     , l_program_application_id
8105     , l_program_id
8106     , SYSDATE
8107     , l_request_id
8108     , 1
8109 
8110   FROM
8111     ar_payment_schedules_all     sch ,
8112     hz_cust_accounts             ca
8113   WHERE
8114          NVL( receipt_confirmed_flag, 'Y' )  =  'Y'
8115     AND  gl_date_closed = to_date( '31-12-4712', 'DD-MM-YYYY')
8116     AND  ca.cust_account_id             =  sch.customer_id
8117     AND  amount_due_remaining > 0
8118   GROUP BY
8119       sch.customer_site_use_id
8120     , sch.invoice_currency_code
8121     , sch.customer_id
8122     , ca.party_id
8123     , sch.org_id
8124     , TO_NUMBER( TO_CHAR( sch.due_date, 'J' ) )
8125   ;
8126 
8127   IF G_debug_flag = 'Y'
8128   THEN
8129     oe_debug_pub.add( 'past due invt ' || DO_TIME, 2 );
8130   END IF;
8131 
8132 END IF; -- IF l_past_due_bal
8133 
8134   IF G_debug_flag = 'Y'
8135   THEN
8136     oe_debug_pub.add( ' End of first phase ');
8137   END IF;
8138 
8139 
8140   ----------------------
8141   --- second phase -----
8142   ----------------------
8143   l_level          := 0;
8144   l_bucket_length  := POWER( 2, l_level );
8145 
8146   -- Commented for thr bug 7688615 to improve the performance.
8147   /*UPDATE
8148     oe_credit_summaries
8149   SET
8150     bucket_duration  = l_bucket_length
8151     WHERE balance_type NOT IN (18,23,24,25,26,27,28,29,30,31,32,33,34,35,36); ---change for Returns
8152 
8153   */
8154   IF NVL(FND_PROFILE.VALUE('ONT_PRESERVE_EXT_CR_BAL'),'Y') = 'N' THEN
8155        UPDATE
8156          oe_credit_summaries
8157        SET
8158         bucket_duration  = l_bucket_length
8159        WHERE balance_type =18;
8160   END IF;
8161 
8162   IF G_debug_flag = 'Y'
8163   THEN
8164     oe_debug_pub.add( 'level 0 records updated at ' || DO_TIME, 2 );
8165   END IF;
8166 
8167   l_level          := 1;
8168   l_bucket_length  := POWER( 2, l_level );
8169 
8170 
8171   WHILE  l_level  <=  G_MAX_BUCKET_LEVEL  LOOP
8172 
8173     -- hints are introduced as part of the performance bug 7688615
8174 
8175     INSERT /*+ parallel */ INTO OE_CREDIT_SUMMARIES
8176     ( balance
8177     , balance_type
8178     , site_use_id
8179     , cust_account_id
8180     , party_id
8181     , org_id
8182     , currency_code
8183     , bucket
8184     ,bucket_duration
8185     , creation_date
8186     , created_by
8187     , last_update_date
8188     , last_updated_by
8189     , last_update_login
8190      , program_application_id
8191     , program_id
8192     , program_update_date
8193     , request_id
8194     )
8195 
8196     SELECT /*+ parallel(OE_CREDIT_SUMMARIES) */
8197       SUM( balance )
8198     , balance_type
8199     , site_use_id
8200     , cust_account_id
8201     , party_id
8202     , org_id
8203     , currency_code
8204     , bucket - MOD( bucket, l_bucket_length )
8205     , l_bucket_length
8206     , SYSDATE
8207     , l_created_by
8208     , SYSDATE
8209     , l_last_updated_by
8210     , l_last_update_login
8211     , l_program_application_id
8212     , l_program_id
8213     , SYSDATE
8214     , l_request_id
8215 
8216     FROM
8217       oe_credit_summaries
8218     WHERE
8219      bucket_duration  =  l_bucket_length / 2
8220     AND balance_type NOT IN (18,23,24,25,26,27,28,29,30,31,32,33,34,35,36) ---change for Returns
8221     GROUP BY
8222       balance_type
8223     , site_use_id
8224     , cust_account_id
8225     , party_id
8226     , org_id
8227     , currency_code
8228     , bucket - MOD( bucket, l_bucket_length )
8229     ;
8230 
8231   IF G_debug_flag = 'Y'
8232   THEN
8233     oe_debug_pub.add( 'level ' || l_level || ' records done at '||DO_TIME, 2 );
8234   END IF;
8235 
8236     l_level          := l_level + 1;
8237     l_bucket_length  := POWER( 2, l_level );
8238   END LOOP;
8239 
8240   IF G_debug_flag = 'Y'
8241   THEN
8242    oe_debug_pub.add( ' Done Inserting into summary table, about to COMMIT');
8243   END IF;
8244 
8245   COMMIT;
8246 
8247   x_retcode  := 0;
8248 
8249   IF G_debug_flag = 'Y'
8250   THEN
8251    oe_debug_pub.add( ' after COMMIT  command');
8252    oe_debug_pub.add( 'Exiting OE_CREDIT_EXPOSURE_PVT.Init_Summary_Table ', 1 );
8253   END IF;
8254 
8255 EXCEPTION
8256 
8257   WHEN OTHERS THEN
8258 
8259     IF
8260       FND_MSG_PUB.Check_Msg_Level( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )
8261     THEN
8262       FND_MSG_PUB.Add_Exc_Msg
8263       ( G_PKG_NAME , 'Init_Summary_Table' );
8264     END IF;
8265 
8266    x_errbuf   := FND_MSG_PUB.Get( p_encoded  =>  FND_API.G_FALSE );
8267    x_retcode  := 2;
8268 
8269 
8270 END Init_Summary_Table;
8271 
8272 --========================================================================
8273 -- PROCEDURE : Get_Exposure            PUBLIC
8274 -- PARAMETERS: x_retcode               0 success, 1 warning, 2 error
8275 --             x_errbuf                error buffer
8276 --             p_customer_id           not null  (tca: cust_account_id)
8277 --             p_site_use_id           can be null
8278 --             p_header_id             order header
8279 --             p_credit_check_rule_rec
8280 --             p_system_parameters_rec for org id
8281 --             p_limit_curr_code       currency in which to show the exposure
8282 --             p_usage_curr_tbl        only include tran in these currencies
8283 --             p_include_all_flag      include transactions in any currency
8284 --             x_total_exposure
8285 --             x_return_status
8286 --             x_error_curr_tbl        contains currencies with no rates
8287 
8288 ---
8289 -- COMMENT   : This returns the total exposure for a customer or customer site
8290 --             using precalculated data.
8291 --  rajkrish Jan-23-2002 included the logic for p_global_exposure_flag
8292 --=======================================================================--
8293 
8294 PROCEDURE  Get_Exposure
8295 ( p_customer_id             IN    NUMBER
8296 , p_site_use_id             IN    NUMBER
8297 , p_party_id                IN    NUMBER := NULL
8298 , p_header_id               IN    NUMBER
8299 , p_credit_check_rule_rec IN
8300           OE_CREDIT_CHECK_UTIL.oe_credit_rules_rec_type
8301 , p_system_parameters_rec IN
8302           OE_CREDIT_CHECK_UTIL.oe_systems_param_rec_type
8303 , p_limit_curr_code         IN
8304            HZ_CREDIT_PROFILE_AMTS.currency_code%TYPE
8305 , p_usage_curr_tbl          IN  OE_CREDIT_CHECK_UTIL.curr_tbl_type
8306 , p_include_all_flag        IN    VARCHAR2
8307 , p_global_exposure_flag    IN    VARCHAR2 := 'N'
8308 , p_need_exposure_details   IN    VARCHAR2 := 'N'
8309 , x_total_exposure          OUT   NOCOPY NUMBER
8310 , x_order_amount            OUT   NOCOPY NUMBER
8311 , x_order_hold_amount       OUT   NOCOPY NUMBER
8312 , x_ar_amount               OUT   NOCOPY NUMBER
8313 , x_return_status           OUT   NOCOPY VARCHAR2
8314 , x_error_curr_tbl          OUT   NOCOPY OE_CREDIT_CHECK_UTIL.curr_tbl_type
8315 )
8316 IS
8317 
8318   i                        BINARY_INTEGER;
8319   l_site_use_id            NUMBER;
8320   l_org_id                 NUMBER;
8321   l_currency_code          HZ_CREDIT_PROFILE_AMTS.currency_code%TYPE;
8322   l_balance                NUMBER;
8323   l_term                   NUMBER;
8324   l_total                  NUMBER;
8325 
8326   l_bucket                 NUMBER;
8327   l_bucket_length          NUMBER;
8328   l_main_bucket            NUMBER;
8329   l_main_bucket_length     NUMBER;
8330   l_level                  NUMBER;
8331   l_binary_tbl             oe_credit_exposure_pvt.Binary_tbl_type;
8332   j                        NUMBER;
8333   l_ship                   NUMBER;
8334   l_both_exposure          VARCHAR2(1) ;
8335 
8336   CURSOR C_g_use_party_hierarchy IS
8337   SELECT
8338     'Y'
8339    FROM
8340     hz_hierarchy_nodes hn
8341    WHERE  hn.parent_id                     = p_party_id
8342   AND     hn.parent_object_type           = 'ORGANIZATION'
8343   and     hn.parent_table_name            = 'HZ_PARTIES'
8344   and     hn.child_object_type            = 'ORGANIZATION'
8345   and     hn.effective_start_date  <=  sysdate
8346   and     hn.effective_end_date    >= SYSDATE
8347   and     hn.hierarchy_type
8348                 = OE_CREDIT_CHECK_UTIL.G_hierarchy_type ;
8349 
8350 BEGIN
8351   g_use_party_hierarchy := 'N' ;
8352 
8353 
8354   IF G_debug_flag = 'Y'
8355   THEN
8356     oe_debug_pub.add( 'IN OEXVCRXB.OE_CREDIT_EXPOSURE_PVT.Get_Exposure', 1 );
8357     oe_debug_pub.add( '+++++++++++++++++++++++++++++++++++++++++++');
8358     oe_debug_pub.add( 'p_customer_id : ' || p_customer_id, 2 );
8359     oe_debug_pub.add( 'p_site_use_id : ' || p_site_use_id, 2 );
8360     oe_debug_pub.add( 'p_party_id    : '|| p_party_id ,2);
8361     oe_debug_pub.add( 'p_header_id : '   || p_header_id, 2 );
8362     oe_debug_pub.add( 'g_use_party_hierarchy : '|| g_use_party_hierarchy );
8363     oe_debug_pub.add( ' p_include_all_flag   => '||
8364            p_include_all_flag );
8365     oe_debug_pub.add('  p_global_exposure_flag    => '||
8366            p_global_exposure_flag );
8367     oe_debug_pub.add('  p_need_exposure_details   => '||
8368            p_need_exposure_details );
8369     oe_debug_pub.add
8370        ( 'p_credit_check_rule_rec.credit_check_rule_id: '
8371        || p_credit_check_rule_rec.credit_check_rule_id, 2 );
8372     oe_debug_pub.add
8373      ( 'p_credit_check_rule_rec.name: '
8374      ||  p_credit_check_rule_rec.name, 2 );
8375     oe_debug_pub.add
8376     (  'p_credit_check_rule_rec.failure_result_code: '
8377     ||  p_credit_check_rule_rec.failure_result_code, 2 );
8378     oe_debug_pub.add
8379     (  'p_credit_check_rule_rec.open_ar_balance_flag : '
8380     ||  p_credit_check_rule_rec.open_ar_balance_flag, 2 );
8381     oe_debug_pub.add
8382     ( 'p_credit_check_rule_rec.uninvoiced_orders_flag : '
8383      || p_credit_check_rule_rec.uninvoiced_orders_flag, 2 );
8384     oe_debug_pub.add
8385      ( 'p_credit_check_rule_rec.orders_on_hold_flag : '
8386       || p_credit_check_rule_rec.orders_on_hold_flag, 2 );
8387      oe_debug_pub.add
8388      ( 'p_credit_check_rule_rec.shipping_interval : '
8389      || p_credit_check_rule_rec.shipping_interval, 2 );
8390      oe_debug_pub.add
8391       ( 'p_credit_check_rule_rec.open_ar_days : '
8392       || p_credit_check_rule_rec.open_ar_days, 2 );
8393      oe_debug_pub.add
8394      ( 'p_credit_check_rule_rec.start_date_active : '
8395      || p_credit_check_rule_rec.start_date_active, 2 );
8396      oe_debug_pub.add
8397      ( 'p_credit_check_rule_rec.end_date_active : '
8398       || p_credit_check_rule_rec.end_date_active, 2 );
8399      oe_debug_pub.add
8400      ( 'p_credit_check_rule_rec.include_payments_at_risk_flag : '
8401      || p_credit_check_rule_rec.include_payments_at_risk_flag, 2 );
8402      oe_debug_pub.add
8403      ( 'p_credit_check_rule_rec.include_tax_flag : '
8404      || p_credit_check_rule_rec.include_tax_flag, 2 );
8405      oe_debug_pub.add
8406      ( 'p_credit_check_rule_rec.maximum_days_past_due : '
8407      || p_credit_check_rule_rec.maximum_days_past_due, 2 );
8408      oe_debug_pub.add
8409      ( 'p_credit_check_rule_rec.QUICK_CR_CHECK_FLAG : '
8410      || p_credit_check_rule_rec.QUICK_CR_CHECK_FLAG, 2 );
8411      oe_debug_pub.add
8412      ( 'p_credit_check_rule_rec.incl_freight_charges_flag : '
8413       || p_credit_check_rule_rec.incl_freight_charges_flag, 2 );
8414      oe_debug_pub.add
8415       ( 'p_credit_check_rule_rec.shipping_horizon : '
8416      || p_credit_check_rule_rec.shipping_horizon, 2 );
8417      oe_debug_pub.add
8418      ( 'p_credit_check_rule_rec.include_external_exposure_flag : '
8419      || p_credit_check_rule_rec.include_external_exposure_flag, 2 );
8420      oe_debug_pub.add
8421      ( 'p_credit_check_rule_rec.include_returns_flag : '
8422      || p_credit_check_rule_rec.include_returns_flag, 2 );
8423      oe_debug_pub.add
8424      ( 'p_system_parameters_rec.org_id : '
8425      || p_system_parameters_rec.org_id, 2 );
8426      oe_debug_pub.add
8427      ( 'p_system_parameters_rec.master_organization_id : '
8428      || p_system_parameters_rec.master_organization_id, 2 );
8429      oe_debug_pub.add( 'p_limit_curr_code: '  ||  p_limit_curr_code, 2 );
8430      oe_debug_pub.add( 'p_include_all_flag: ' ||  p_include_all_flag, 2 );
8431      oe_debug_pub.add( 'p_global_exposure_flag = '|| p_global_exposure_flag );
8432 
8433   END IF;
8434 
8435   ------------------------------------------------------------------
8436 
8437   g_use_party_hierarchy := 'N' ;
8438 
8439     IF p_party_id is not NULL
8440     THEN
8441        OPEN C_g_use_party_hierarchy ;
8442 
8443        FETCH C_g_use_party_hierarchy
8444        INTO g_use_party_hierarchy ;
8445 
8446        CLOSE C_g_use_party_hierarchy ;
8447     ELSE
8448      g_use_party_hierarchy := 'N' ;
8449     END IF;
8450 
8451 
8452     IF G_debug_flag = 'Y'
8453     THEN
8454       oe_debug_pub.add( 'final g_use_party_hierarchy ==> '||
8455          g_use_party_hierarchy );
8456     END IF;
8457   ------------------------------------------------------------------
8458 
8459   i  :=  p_usage_curr_tbl.FIRST;
8460 
8461   WHILE i IS NOT NULL LOOP
8462   IF G_debug_flag = 'Y'
8463   THEN
8464     oe_debug_pub.add( 'usage curr: '
8465        || p_usage_curr_tbl(i).usage_curr_code, 2 );
8466   END IF;
8467     i  :=  p_usage_curr_tbl.NEXT(i);
8468   END LOOP;
8469 
8470   g_conversion_type  := p_credit_check_rule_rec.conversion_type;
8471 
8472 
8473 -----------
8474 -- Locks needed for consistency
8475 -- Goal: read-consistency of oe_credit_summaries during exposure
8476 -- Means: SHARE lock or read-only transaction.
8477 --        SHARE: Need to verify that update overrides share.
8478 --        read-only: May slow down query due to reconstruction using
8479 --          rollback segment w.r.t SCN
8480 -- Compromise: do nothing
8481 -- Tradeoffs: (consistent periodic) inconsistency extremely unlikely
8482 --            (triggers/table handlers) inconsistency slightly more likely
8483 --            since seperate exposure and update transactions may be correlated
8484 
8485   -- Get the functional currency for use in currency conversion
8486 --  using triangulation
8487 
8488   g_functional_currency := oe_credit_check_util.Get_GL_Currency;
8489 
8490   l_org_id        := OE_CREDIT_CHECK_UTIL.G_org_id;  --bug# 5031301
8491   l_total         := 0;
8492   l_both_exposure := 'N' ;
8493 
8494   x_ar_amount       := NULL;
8495   x_order_amount      := NULL ;
8496   x_order_hold_amount := NULL ;
8497   -----------------------------
8498 
8499   IF NVL(p_need_exposure_details,'N') = 'N'
8500   THEN
8501   -- Order Management Summaries
8502     IF G_debug_flag = 'Y'
8503     THEN
8504      oe_debug_pub.add(' OM Balances ');
8505      oe_debug_pub.add(' call balance_types_om ');
8506    END IF;
8507 
8508      balance_types_om( p_credit_check_rule_rec );
8509 
8510      calculate_buckets
8511      ( ship_date( p_credit_check_rule_rec.shipping_horizon )
8512      , p_credit_check_rule_rec.shipping_interval
8513      , l_main_bucket
8514      , l_binary_tbl
8515      );
8516 
8517 
8518   ---------------------
8519   -- IF no OM or AR Horizon days setup, then the exposure
8520   -- can be calculated in one execution from the
8521   -- summary table both for OM and AR exposure as we need
8522   -- all the data
8523 
8524     IF p_credit_check_rule_rec.open_ar_days IS NULL
8525        AND p_credit_check_rule_rec.shipping_interval IS NULL
8526     THEN
8527       IF G_debug_flag = 'Y'
8528       THEN
8529         oe_debug_pub.add(' Call balance_types_om_and_ar ');
8530       END IF;
8531 
8532        balance_types_om_and_ar( p_credit_check_rule_rec );
8533 
8534        l_both_exposure := 'Y' ;
8535 
8536     ELSE
8537       IF G_debug_flag = 'Y'
8538       THEN
8539         oe_debug_pub.add(' Select OM and AR exposure separately ');
8540       END IF;
8541     END IF;
8542 
8543     IF G_debug_flag = 'Y'
8544     THEN
8545       oe_debug_pub.add(' l_both_exposure ==> '|| l_both_exposure );
8546       oe_debug_pub.add(' Call retrieve_exposure ');
8547     END IF;
8548 
8549         l_total  := l_total  +  retrieve_exposure
8550         ( p_binary_tbl           =>  l_binary_tbl
8551         , p_site_use_id          =>  p_site_use_id
8552         , p_customer_id          =>  p_customer_id
8553         , p_party_id             => p_party_id
8554         , p_org_id               =>  l_org_id
8555         , p_include_all_flag     =>  p_include_all_flag
8556         , p_usage_curr_tbl       =>  p_usage_curr_tbl
8557         , p_limit_curr_code      =>  p_limit_curr_code
8558         , p_main_bucket          =>  l_main_bucket
8559         , p_global_exposure_flag =>  p_global_exposure_flag
8560         , p_credit_check_rule_rec => p_credit_check_rule_rec
8561         , x_error_curr_tbl       =>  x_error_curr_tbl
8562         );
8563 
8564       IF G_debug_flag = 'Y'
8565       THEN
8566         oe_debug_pub.add(' Out of Retreive exposure with total ==>  '
8567             || l_total );
8568       END IF;
8569 
8570   ---------------------------------
8571   -- Accounts Receivables Summaries
8572 
8573       IF l_both_exposure = 'N'
8574       THEN
8575         IF G_debug_flag = 'Y'
8576         THEN
8577           oe_debug_pub.add(' AR exposure ');
8578           oe_debug_pub.add(' Call balance_types_ar ');
8579         END IF;
8580 
8581          balance_types_ar( p_credit_check_rule_rec );
8582 
8583 
8584          calculate_buckets
8585          ( open_date( p_credit_check_rule_rec.open_ar_days )
8586          , p_credit_check_rule_rec.open_ar_days
8587          , l_main_bucket
8588          , l_binary_tbl
8589           );
8590 
8591          oe_debug_pub.add(' Call retrieve_exposure for AR balance ');
8592 
8593          l_total  := l_total  +  retrieve_exposure
8594          ( p_binary_tbl           =>  l_binary_tbl
8595          , p_site_use_id          =>  p_site_use_id
8596          , p_customer_id          =>  p_customer_id
8597          , p_party_id             => p_party_id
8598          , p_org_id               =>  l_org_id
8599          , p_include_all_flag     =>  p_include_all_flag
8600          , p_usage_curr_tbl       =>  p_usage_curr_tbl
8601          , p_limit_curr_code      =>  p_limit_curr_code
8602          , p_main_bucket          =>  l_main_bucket
8603          , p_global_exposure_flag =>  p_global_exposure_flag
8604          , p_credit_check_rule_rec => p_credit_check_rule_rec
8605          , x_error_curr_tbl       =>  x_error_curr_tbl
8606          );
8607 
8608         IF G_debug_flag = 'Y'
8609         THEN
8610            oe_debug_pub.add(' out of AR  retrieve_exposure ==> '||
8611          l_total );
8612         END IF;
8613 
8614       ELSE
8615         IF G_debug_flag = 'Y'
8616         THEN
8617           oe_debug_pub.add(' NO need for AR expe as both exp calculated = '
8618                 || l_both_exposure );
8619         END IF;
8620       END IF;
8621 
8622       x_total_exposure := l_total;
8623 
8624    ELSE
8625 
8626   ------------------------------ support detail exposure ------------
8627       IF G_debug_flag = 'Y'
8628       THEN
8629         oe_debug_pub.add(' support detail exposure ',1);
8630       END IF;
8631 
8632       balance_types_om_hold( p_credit_check_rule_rec );
8633 
8634       calculate_buckets
8635       ( ship_date( p_credit_check_rule_rec.shipping_horizon )
8636        , p_credit_check_rule_rec.shipping_interval
8637        , l_main_bucket
8638        , l_binary_tbl
8639        );
8640 
8641        x_order_hold_amount :=  retrieve_exposure
8642        ( p_binary_tbl            =>  l_binary_tbl
8643        , p_site_use_id           =>  p_site_use_id
8644        , p_customer_id           =>  p_customer_id
8645        , p_party_id              => p_party_id
8646        , p_org_id                =>  l_org_id
8647        , p_include_all_flag      =>  p_include_all_flag
8648        , p_usage_curr_tbl        =>  p_usage_curr_tbl
8649        , p_limit_curr_code       =>  p_limit_curr_code
8650        , p_main_bucket           =>  l_main_bucket
8651        , p_global_exposure_flag  =>  p_global_exposure_flag
8652        , p_credit_check_rule_rec => p_credit_check_rule_rec
8653        , x_error_curr_tbl        =>  x_error_curr_tbl
8654        );
8655 
8656       IF G_debug_flag = 'Y'
8657       THEN
8658         oe_debug_pub.add(' x_order_hold_amount => '||
8659          x_order_hold_amount );
8660 
8661         oe_debug_pub.add(' x_order_amount => '|| x_order_amount );
8662 
8663         oe_debug_pub.add(' x_ar_amount ' || x_ar_amount );
8664       END IF;
8665      --------------------------------------------------------
8666       balance_types_om_nohold ( p_credit_check_rule_rec );
8667 
8668       calculate_buckets
8669       ( ship_date( p_credit_check_rule_rec.shipping_horizon )
8670        , p_credit_check_rule_rec.shipping_interval
8671        , l_main_bucket
8672        , l_binary_tbl
8673        );
8674 
8675        x_order_amount :=  retrieve_exposure
8676        ( p_binary_tbl            =>  l_binary_tbl
8677        , p_site_use_id           =>  p_site_use_id
8678        , p_customer_id           =>  p_customer_id
8679        , p_party_id              => p_party_id
8680        , p_org_id                =>  l_org_id
8681        , p_include_all_flag      =>  p_include_all_flag
8682        , p_usage_curr_tbl        =>  p_usage_curr_tbl
8683        , p_limit_curr_code       =>  p_limit_curr_code
8684        , p_main_bucket           =>  l_main_bucket
8685        , p_global_exposure_flag  =>  p_global_exposure_flag
8686        , p_credit_check_rule_rec => p_credit_check_rule_rec
8687        , x_error_curr_tbl        =>  x_error_curr_tbl
8688        );
8689 
8690       IF G_debug_flag = 'Y'
8691       THEN
8692         oe_debug_pub.add(' x_order_amount => '|| x_order_amount );
8693         oe_debug_pub.add(' AR exposure ');
8694         oe_debug_pub.add(' Call balance_types_ar ');
8695       END IF;
8696 
8697          balance_types_ar( p_credit_check_rule_rec );
8698 
8699 
8700          calculate_buckets
8701          ( open_date( p_credit_check_rule_rec.open_ar_days )
8702          , p_credit_check_rule_rec.open_ar_days
8703          , l_main_bucket
8704          , l_binary_tbl
8705           );
8706 
8707          oe_debug_pub.add(' Call retrieve_exposure for AR balance ');
8708 
8709          x_ar_amount :=   retrieve_exposure
8710          ( p_binary_tbl           =>  l_binary_tbl
8711          , p_site_use_id          =>  p_site_use_id
8712          , p_customer_id          =>  p_customer_id
8713          , p_party_id             => p_party_id
8714          , p_org_id               =>  l_org_id
8715          , p_include_all_flag     =>  p_include_all_flag
8716          , p_usage_curr_tbl       =>  p_usage_curr_tbl
8717          , p_limit_curr_code      =>  p_limit_curr_code
8718          , p_main_bucket          =>  l_main_bucket
8719          , p_global_exposure_flag =>  p_global_exposure_flag
8720          , p_credit_check_rule_rec => p_credit_check_rule_rec
8721          , x_error_curr_tbl       =>  x_error_curr_tbl
8722          );
8723 
8724         IF G_debug_flag = 'Y'
8725         THEN
8726           oe_debug_pub.add(' out of AR  retrieve_exposure ==> '||
8727          x_ar_amount );
8728        END IF;
8729 
8730        --bug# 5597791
8731        IF G_debug_flag = 'Y'
8732        THEN
8733          oe_debug_pub.add(' x_order_hold_amount => '||
8734           x_order_hold_amount );
8735 
8736          oe_debug_pub.add(' x_order_amount => '|| x_order_amount );
8737 
8738          oe_debug_pub.add(' x_ar_amount ' || x_ar_amount );
8739        END IF;
8740 
8741        x_total_exposure := nvl(x_order_hold_amount,0) + nvl(x_order_amount,0) + nvl(x_ar_amount,0);
8742 
8743  END IF; -- exposure details
8744 
8745 --------------------------------------------------------------
8746   x_return_status  := FND_API.G_RET_STS_SUCCESS;
8747 
8748   IF G_debug_flag = 'Y'
8749   THEN
8750     oe_debug_pub.add(' ');
8751     oe_debug_pub.add(' Total FINAL exposure ==> '|| x_total_exposure,1 );
8752     oe_debug_pub.add(' ');
8753     oe_debug_pub.add('*************************************************');
8754     oe_debug_pub.add( 'Exiting OE_CREDIT_EXPOSURE_PVT.Get_Exposure', 1 );
8755   END IF;
8756 
8757 
8758   l_total   := 0;
8759   l_both_exposure := 'N' ;
8760 
8761 
8762 
8763 EXCEPTION
8764 
8765   WHEN FND_API.G_EXC_ERROR THEN
8766     x_return_status := FND_API.G_RET_STS_ERROR;
8767 
8768   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8769     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8770     oe_debug_pub.add(SQLERRM );
8771 
8772   WHEN OTHERS THEN
8773     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8774     oe_debug_pub.add(SQLERRM );
8775     IF
8776       FND_MSG_PUB.Check_Msg_Level( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR )
8777     THEN
8778 --        FND_MSG_PUB.Add_Exc_Msg( l_api_name, 'Get_Exposure' );
8779       FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME, 'Get_Exposure' );
8780     END IF;
8781 
8782 END Get_Exposure;
8783 
8784 
8785 END OE_CREDIT_EXPOSURE_PVT;